ae0e5a0b76ce2275b14998154c962c2899e139f0
[cascardo/ovs.git] / lib / netdev-linux.c
1 /*
2  * Copyright (c) 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18
19 #include "netdev-linux.h"
20
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <arpa/inet.h>
24 #include <inttypes.h>
25 #include <linux/filter.h>
26 #include <linux/gen_stats.h>
27 #include <linux/if_ether.h>
28 #include <linux/if_tun.h>
29 #include <linux/types.h>
30 #include <linux/ethtool.h>
31 #include <linux/mii.h>
32 #include <linux/pkt_cls.h>
33 #include <linux/pkt_sched.h>
34 #include <linux/rtnetlink.h>
35 #include <linux/sockios.h>
36 #include <linux/version.h>
37 #include <sys/types.h>
38 #include <sys/ioctl.h>
39 #include <sys/socket.h>
40 #include <netpacket/packet.h>
41 #include <net/if.h>
42 #include <net/if_arp.h>
43 #include <net/if_packet.h>
44 #include <net/route.h>
45 #include <netinet/in.h>
46 #include <poll.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50
51 #include "coverage.h"
52 #include "dpif-linux.h"
53 #include "dynamic-string.h"
54 #include "fatal-signal.h"
55 #include "hash.h"
56 #include "hmap.h"
57 #include "netdev-provider.h"
58 #include "netdev-vport.h"
59 #include "netlink-notifier.h"
60 #include "netlink-socket.h"
61 #include "netlink.h"
62 #include "ofpbuf.h"
63 #include "openflow/openflow.h"
64 #include "ovs-atomic.h"
65 #include "packets.h"
66 #include "poll-loop.h"
67 #include "rtnetlink-link.h"
68 #include "shash.h"
69 #include "socket-util.h"
70 #include "sset.h"
71 #include "timer.h"
72 #include "unaligned.h"
73 #include "vlog.h"
74
75 VLOG_DEFINE_THIS_MODULE(netdev_linux);
76
77 COVERAGE_DEFINE(netdev_set_policing);
78 COVERAGE_DEFINE(netdev_arp_lookup);
79 COVERAGE_DEFINE(netdev_get_ifindex);
80 COVERAGE_DEFINE(netdev_get_hwaddr);
81 COVERAGE_DEFINE(netdev_set_hwaddr);
82 COVERAGE_DEFINE(netdev_get_ethtool);
83 COVERAGE_DEFINE(netdev_set_ethtool);
84
85 \f
86 /* These were introduced in Linux 2.6.14, so they might be missing if we have
87  * old headers. */
88 #ifndef ADVERTISED_Pause
89 #define ADVERTISED_Pause                (1 << 13)
90 #endif
91 #ifndef ADVERTISED_Asym_Pause
92 #define ADVERTISED_Asym_Pause           (1 << 14)
93 #endif
94
95 /* These were introduced in Linux 2.6.24, so they might be missing if we
96  * have old headers. */
97 #ifndef ETHTOOL_GFLAGS
98 #define ETHTOOL_GFLAGS       0x00000025 /* Get flags bitmap(ethtool_value) */
99 #endif
100 #ifndef ETHTOOL_SFLAGS
101 #define ETHTOOL_SFLAGS       0x00000026 /* Set flags bitmap(ethtool_value) */
102 #endif
103
104 /* This was introduced in Linux 2.6.25, so it might be missing if we have old
105  * headers. */
106 #ifndef TC_RTAB_SIZE
107 #define TC_RTAB_SIZE 1024
108 #endif
109
110 enum {
111     VALID_IFINDEX           = 1 << 0,
112     VALID_ETHERADDR         = 1 << 1,
113     VALID_IN4               = 1 << 2,
114     VALID_IN6               = 1 << 3,
115     VALID_MTU               = 1 << 4,
116     VALID_POLICING          = 1 << 5,
117     VALID_VPORT_STAT_ERROR  = 1 << 6,
118     VALID_DRVINFO           = 1 << 7,
119     VALID_FEATURES          = 1 << 8,
120 };
121 \f
122 /* Traffic control. */
123
124 /* An instance of a traffic control class.  Always associated with a particular
125  * network device.
126  *
127  * Each TC implementation subclasses this with whatever additional data it
128  * needs. */
129 struct tc {
130     const struct tc_ops *ops;
131     struct hmap queues;         /* Contains "struct tc_queue"s.
132                                  * Read by generic TC layer.
133                                  * Written only by TC implementation. */
134 };
135
136 #define TC_INITIALIZER(TC, OPS) { OPS, HMAP_INITIALIZER(&(TC)->queues) }
137
138 /* One traffic control queue.
139  *
140  * Each TC implementation subclasses this with whatever additional data it
141  * needs. */
142 struct tc_queue {
143     struct hmap_node hmap_node; /* In struct tc's "queues" hmap. */
144     unsigned int queue_id;      /* OpenFlow queue ID. */
145     long long int created;      /* Time queue was created, in msecs. */
146 };
147
148 /* A particular kind of traffic control.  Each implementation generally maps to
149  * one particular Linux qdisc class.
150  *
151  * The functions below return 0 if successful or a positive errno value on
152  * failure, except where otherwise noted.  All of them must be provided, except
153  * where otherwise noted. */
154 struct tc_ops {
155     /* Name used by kernel in the TCA_KIND attribute of tcmsg, e.g. "htb".
156      * This is null for tc_ops_default and tc_ops_other, for which there are no
157      * appropriate values. */
158     const char *linux_name;
159
160     /* Name used in OVS database, e.g. "linux-htb".  Must be nonnull. */
161     const char *ovs_name;
162
163     /* Number of supported OpenFlow queues, 0 for qdiscs that have no
164      * queues.  The queues are numbered 0 through n_queues - 1. */
165     unsigned int n_queues;
166
167     /* Called to install this TC class on 'netdev'.  The implementation should
168      * make the Netlink calls required to set up 'netdev' with the right qdisc
169      * and configure it according to 'details'.  The implementation may assume
170      * that the current qdisc is the default; that is, there is no need for it
171      * to delete the current qdisc before installing itself.
172      *
173      * The contents of 'details' should be documented as valid for 'ovs_name'
174      * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml
175      * (which is built as ovs-vswitchd.conf.db(8)).
176      *
177      * This function must return 0 if and only if it sets 'netdev->tc' to an
178      * initialized 'struct tc'.
179      *
180      * (This function is null for tc_ops_other, which cannot be installed.  For
181      * other TC classes it should always be nonnull.) */
182     int (*tc_install)(struct netdev *netdev, const struct smap *details);
183
184     /* Called when the netdev code determines (through a Netlink query) that
185      * this TC class's qdisc is installed on 'netdev', but we didn't install
186      * it ourselves and so don't know any of the details.
187      *
188      * 'nlmsg' is the kernel reply to a RTM_GETQDISC Netlink message for
189      * 'netdev'.  The TCA_KIND attribute of 'nlmsg' is 'linux_name'.  The
190      * implementation should parse the other attributes of 'nlmsg' as
191      * necessary to determine its configuration.  If necessary it should also
192      * use Netlink queries to determine the configuration of queues on
193      * 'netdev'.
194      *
195      * This function must return 0 if and only if it sets 'netdev->tc' to an
196      * initialized 'struct tc'. */
197     int (*tc_load)(struct netdev *netdev, struct ofpbuf *nlmsg);
198
199     /* Destroys the data structures allocated by the implementation as part of
200      * 'tc'.  (This includes destroying 'tc->queues' by calling
201      * tc_destroy(tc).
202      *
203      * The implementation should not need to perform any Netlink calls.  If
204      * desirable, the caller is responsible for deconfiguring the kernel qdisc.
205      * (But it may not be desirable.)
206      *
207      * This function may be null if 'tc' is trivial. */
208     void (*tc_destroy)(struct tc *tc);
209
210     /* Retrieves details of 'netdev->tc' configuration into 'details'.
211      *
212      * The implementation should not need to perform any Netlink calls, because
213      * the 'tc_install' or 'tc_load' that instantiated 'netdev->tc' should have
214      * cached the configuration.
215      *
216      * The contents of 'details' should be documented as valid for 'ovs_name'
217      * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml
218      * (which is built as ovs-vswitchd.conf.db(8)).
219      *
220      * This function may be null if 'tc' is not configurable.
221      */
222     int (*qdisc_get)(const struct netdev *netdev, struct smap *details);
223
224     /* Reconfigures 'netdev->tc' according to 'details', performing any
225      * required Netlink calls to complete the reconfiguration.
226      *
227      * The contents of 'details' should be documented as valid for 'ovs_name'
228      * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml
229      * (which is built as ovs-vswitchd.conf.db(8)).
230      *
231      * This function may be null if 'tc' is not configurable.
232      */
233     int (*qdisc_set)(struct netdev *, const struct smap *details);
234
235     /* Retrieves details of 'queue' on 'netdev->tc' into 'details'.  'queue' is
236      * one of the 'struct tc_queue's within 'netdev->tc->queues'.
237      *
238      * The contents of 'details' should be documented as valid for 'ovs_name'
239      * in the "other_config" column in the "Queue" table in
240      * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
241      *
242      * The implementation should not need to perform any Netlink calls, because
243      * the 'tc_install' or 'tc_load' that instantiated 'netdev->tc' should have
244      * cached the queue configuration.
245      *
246      * This function may be null if 'tc' does not have queues ('n_queues' is
247      * 0). */
248     int (*class_get)(const struct netdev *netdev, const struct tc_queue *queue,
249                      struct smap *details);
250
251     /* Configures or reconfigures 'queue_id' on 'netdev->tc' according to
252      * 'details', perfoming any required Netlink calls to complete the
253      * reconfiguration.  The caller ensures that 'queue_id' is less than
254      * 'n_queues'.
255      *
256      * The contents of 'details' should be documented as valid for 'ovs_name'
257      * in the "other_config" column in the "Queue" table in
258      * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
259      *
260      * This function may be null if 'tc' does not have queues or its queues are
261      * not configurable. */
262     int (*class_set)(struct netdev *, unsigned int queue_id,
263                      const struct smap *details);
264
265     /* Deletes 'queue' from 'netdev->tc'.  'queue' is one of the 'struct
266      * tc_queue's within 'netdev->tc->queues'.
267      *
268      * This function may be null if 'tc' does not have queues or its queues
269      * cannot be deleted. */
270     int (*class_delete)(struct netdev *, struct tc_queue *queue);
271
272     /* Obtains stats for 'queue' from 'netdev->tc'.  'queue' is one of the
273      * 'struct tc_queue's within 'netdev->tc->queues'.
274      *
275      * On success, initializes '*stats'.
276      *
277      * This function may be null if 'tc' does not have queues or if it cannot
278      * report queue statistics. */
279     int (*class_get_stats)(const struct netdev *netdev,
280                            const struct tc_queue *queue,
281                            struct netdev_queue_stats *stats);
282
283     /* Extracts queue stats from 'nlmsg', which is a response to a
284      * RTM_GETTCLASS message, and passes them to 'cb' along with 'aux'.
285      *
286      * This function may be null if 'tc' does not have queues or if it cannot
287      * report queue statistics. */
288     int (*class_dump_stats)(const struct netdev *netdev,
289                             const struct ofpbuf *nlmsg,
290                             netdev_dump_queue_stats_cb *cb, void *aux);
291 };
292
293 static void
294 tc_init(struct tc *tc, const struct tc_ops *ops)
295 {
296     tc->ops = ops;
297     hmap_init(&tc->queues);
298 }
299
300 static void
301 tc_destroy(struct tc *tc)
302 {
303     hmap_destroy(&tc->queues);
304 }
305
306 static const struct tc_ops tc_ops_htb;
307 static const struct tc_ops tc_ops_hfsc;
308 static const struct tc_ops tc_ops_default;
309 static const struct tc_ops tc_ops_other;
310
311 static const struct tc_ops *const tcs[] = {
312     &tc_ops_htb,                /* Hierarchy token bucket (see tc-htb(8)). */
313     &tc_ops_hfsc,               /* Hierarchical fair service curve. */
314     &tc_ops_default,            /* Default qdisc (see tc-pfifo_fast(8)). */
315     &tc_ops_other,              /* Some other qdisc. */
316     NULL
317 };
318
319 static unsigned int tc_make_handle(unsigned int major, unsigned int minor);
320 static unsigned int tc_get_major(unsigned int handle);
321 static unsigned int tc_get_minor(unsigned int handle);
322
323 static unsigned int tc_ticks_to_bytes(unsigned int rate, unsigned int ticks);
324 static unsigned int tc_bytes_to_ticks(unsigned int rate, unsigned int size);
325 static unsigned int tc_buffer_per_jiffy(unsigned int rate);
326
327 static struct tcmsg *tc_make_request(const struct netdev *, int type,
328                                      unsigned int flags, struct ofpbuf *);
329 static int tc_transact(struct ofpbuf *request, struct ofpbuf **replyp);
330 static int tc_add_del_ingress_qdisc(struct netdev *netdev, bool add);
331 static int tc_add_policer(struct netdev *netdev, int kbits_rate,
332                           int kbits_burst);
333
334 static int tc_parse_qdisc(const struct ofpbuf *, const char **kind,
335                           struct nlattr **options);
336 static int tc_parse_class(const struct ofpbuf *, unsigned int *queue_id,
337                           struct nlattr **options,
338                           struct netdev_queue_stats *);
339 static int tc_query_class(const struct netdev *,
340                           unsigned int handle, unsigned int parent,
341                           struct ofpbuf **replyp);
342 static int tc_delete_class(const struct netdev *, unsigned int handle);
343
344 static int tc_del_qdisc(struct netdev *netdev);
345 static int tc_query_qdisc(const struct netdev *netdev);
346
347 static int tc_calc_cell_log(unsigned int mtu);
348 static void tc_fill_rate(struct tc_ratespec *rate, uint64_t bps, int mtu);
349 static void tc_put_rtab(struct ofpbuf *, uint16_t type,
350                         const struct tc_ratespec *rate);
351 static int tc_calc_buffer(unsigned int Bps, int mtu, uint64_t burst_bytes);
352 \f
353 struct netdev_linux {
354     struct netdev up;
355
356     /* Protects all members below. */
357     struct ovs_mutex mutex;
358
359     unsigned int cache_valid;
360     unsigned int change_seq;
361
362     bool miimon;                    /* Link status of last poll. */
363     long long int miimon_interval;  /* Miimon Poll rate. Disabled if <= 0. */
364     struct timer miimon_timer;
365
366     /* The following are figured out "on demand" only.  They are only valid
367      * when the corresponding VALID_* bit in 'cache_valid' is set. */
368     int ifindex;
369     uint8_t etheraddr[ETH_ADDR_LEN];
370     struct in_addr address, netmask;
371     struct in6_addr in6;
372     int mtu;
373     unsigned int ifi_flags;
374     long long int carrier_resets;
375     uint32_t kbits_rate;        /* Policing data. */
376     uint32_t kbits_burst;
377     int vport_stats_error;      /* Cached error code from vport_get_stats().
378                                    0 or an errno value. */
379     int netdev_mtu_error;       /* Cached error code from SIOCGIFMTU or SIOCSIFMTU. */
380     int ether_addr_error;       /* Cached error code from set/get etheraddr. */
381     int netdev_policing_error;  /* Cached error code from set policing. */
382     int get_features_error;     /* Cached error code from ETHTOOL_GSET. */
383     int get_ifindex_error;      /* Cached error code from SIOCGIFINDEX. */
384
385     enum netdev_features current;    /* Cached from ETHTOOL_GSET. */
386     enum netdev_features advertised; /* Cached from ETHTOOL_GSET. */
387     enum netdev_features supported;  /* Cached from ETHTOOL_GSET. */
388
389     struct ethtool_drvinfo drvinfo;  /* Cached from ETHTOOL_GDRVINFO. */
390     struct tc *tc;
391
392     /* For devices of class netdev_tap_class only. */
393     int tap_fd;
394 };
395
396 struct netdev_rx_linux {
397     struct netdev_rx up;
398     bool is_tap;
399     int fd;
400 };
401
402 /* This is set pretty low because we probably won't learn anything from the
403  * additional log messages. */
404 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
405
406 /* Polling miimon status for all ports causes performance degradation when
407  * handling a large number of ports. If there are no devices using miimon, then
408  * we skip netdev_linux_miimon_run() and netdev_linux_miimon_wait(). */
409 static atomic_int miimon_cnt = ATOMIC_VAR_INIT(0);
410
411 static void netdev_linux_run(void);
412
413 static int netdev_linux_do_ethtool(const char *name, struct ethtool_cmd *,
414                                    int cmd, const char *cmd_name);
415 static int netdev_linux_get_ipv4(const struct netdev *, struct in_addr *,
416                                  int cmd, const char *cmd_name);
417 static int get_flags(const struct netdev *, unsigned int *flags);
418 static int set_flags(const char *, unsigned int flags);
419 static int update_flags(struct netdev_linux *netdev, enum netdev_flags off,
420                         enum netdev_flags on, enum netdev_flags *old_flagsp)
421     OVS_REQUIRES(netdev->mutex);
422 static int do_get_ifindex(const char *netdev_name);
423 static int get_ifindex(const struct netdev *, int *ifindexp);
424 static int do_set_addr(struct netdev *netdev,
425                        int ioctl_nr, const char *ioctl_name,
426                        struct in_addr addr);
427 static int get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN]);
428 static int set_etheraddr(const char *netdev_name, const uint8_t[ETH_ADDR_LEN]);
429 static int get_stats_via_netlink(int ifindex, struct netdev_stats *stats);
430 static int get_stats_via_proc(const char *netdev_name, struct netdev_stats *stats);
431 static int af_packet_sock(void);
432 static bool netdev_linux_miimon_enabled(void);
433 static void netdev_linux_miimon_run(void);
434 static void netdev_linux_miimon_wait(void);
435
436 static bool
437 is_netdev_linux_class(const struct netdev_class *netdev_class)
438 {
439     return netdev_class->run == netdev_linux_run;
440 }
441
442 static bool
443 is_tap_netdev(const struct netdev *netdev)
444 {
445     return netdev_get_class(netdev) == &netdev_tap_class;
446 }
447
448 static struct netdev_linux *
449 netdev_linux_cast(const struct netdev *netdev)
450 {
451     ovs_assert(is_netdev_linux_class(netdev_get_class(netdev)));
452
453     return CONTAINER_OF(netdev, struct netdev_linux, up);
454 }
455
456 static struct netdev_rx_linux *
457 netdev_rx_linux_cast(const struct netdev_rx *rx)
458 {
459     ovs_assert(is_netdev_linux_class(netdev_get_class(rx->netdev)));
460     return CONTAINER_OF(rx, struct netdev_rx_linux, up);
461 }
462 \f
463 static void netdev_linux_update(struct netdev_linux *netdev,
464                                 const struct rtnetlink_link_change *)
465     OVS_REQUIRES(netdev->mutex);
466 static void netdev_linux_changed(struct netdev_linux *netdev,
467                                  unsigned int ifi_flags, unsigned int mask)
468     OVS_REQUIRES(netdev->mutex);
469
470 /* Returns a NETLINK_ROUTE socket listening for RTNLGRP_LINK changes, or NULL
471  * if no such socket could be created. */
472 static struct nl_sock *
473 netdev_linux_notify_sock(void)
474 {
475     static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
476     static struct nl_sock *sock;
477
478     if (ovsthread_once_start(&once)) {
479         int error;
480
481         error = nl_sock_create(NETLINK_ROUTE, &sock);
482         if (!error) {
483             error = nl_sock_join_mcgroup(sock, RTNLGRP_LINK);
484             if (error) {
485                 nl_sock_destroy(sock);
486                 sock = NULL;
487             }
488         }
489         ovsthread_once_done(&once);
490     }
491
492     return sock;
493 }
494
495 static bool
496 netdev_linux_miimon_enabled(void)
497 {
498     int miimon;
499
500     atomic_read(&miimon_cnt, &miimon);
501     return miimon > 0;
502 }
503
504 static void
505 netdev_linux_run(void)
506 {
507     struct nl_sock *sock;
508     int error;
509
510     if (netdev_linux_miimon_enabled()) {
511         netdev_linux_miimon_run();
512     }
513
514     sock = netdev_linux_notify_sock();
515     if (!sock) {
516         return;
517     }
518
519     do {
520         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
521         uint64_t buf_stub[4096 / 8];
522         struct ofpbuf buf;
523
524         ofpbuf_use_stub(&buf, buf_stub, sizeof buf_stub);
525         error = nl_sock_recv(sock, &buf, false);
526         if (!error) {
527             struct rtnetlink_link_change change;
528
529             if (rtnetlink_link_parse(&buf, &change)) {
530                 struct netdev *netdev_ = netdev_from_name(change.ifname);
531                 if (netdev_ && is_netdev_linux_class(netdev_->netdev_class)) {
532                     struct netdev_linux *netdev = netdev_linux_cast(netdev_);
533
534                     ovs_mutex_lock(&netdev->mutex);
535                     netdev_linux_update(netdev, &change);
536                     ovs_mutex_unlock(&netdev->mutex);
537                 }
538                 netdev_close(netdev_);
539             }
540         } else if (error == ENOBUFS) {
541             struct shash device_shash;
542             struct shash_node *node;
543
544             nl_sock_drain(sock);
545
546             shash_init(&device_shash);
547             netdev_get_devices(&netdev_linux_class, &device_shash);
548             SHASH_FOR_EACH (node, &device_shash) {
549                 struct netdev *netdev_ = node->data;
550                 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
551                 unsigned int flags;
552
553                 ovs_mutex_lock(&netdev->mutex);
554                 get_flags(netdev_, &flags);
555                 netdev_linux_changed(netdev, flags, 0);
556                 ovs_mutex_unlock(&netdev->mutex);
557
558                 netdev_close(netdev_);
559             }
560             shash_destroy(&device_shash);
561         } else if (error != EAGAIN) {
562             VLOG_WARN_RL(&rl, "error reading or parsing netlink (%s)",
563                          ovs_strerror(error));
564         }
565         ofpbuf_uninit(&buf);
566     } while (!error);
567 }
568
569 static void
570 netdev_linux_wait(void)
571 {
572     struct nl_sock *sock;
573
574     if (netdev_linux_miimon_enabled()) {
575         netdev_linux_miimon_wait();
576     }
577     sock = netdev_linux_notify_sock();
578     if (sock) {
579         nl_sock_wait(sock, POLLIN);
580     }
581 }
582
583 static void
584 netdev_linux_changed(struct netdev_linux *dev,
585                      unsigned int ifi_flags, unsigned int mask)
586     OVS_REQUIRES(dev->mutex)
587 {
588     dev->change_seq++;
589     if (!dev->change_seq) {
590         dev->change_seq++;
591     }
592
593     if ((dev->ifi_flags ^ ifi_flags) & IFF_RUNNING) {
594         dev->carrier_resets++;
595     }
596     dev->ifi_flags = ifi_flags;
597
598     dev->cache_valid &= mask;
599 }
600
601 static void
602 netdev_linux_update(struct netdev_linux *dev,
603                     const struct rtnetlink_link_change *change)
604     OVS_REQUIRES(dev->mutex)
605 {
606     if (change->nlmsg_type == RTM_NEWLINK) {
607         /* Keep drv-info */
608         netdev_linux_changed(dev, change->ifi_flags, VALID_DRVINFO);
609
610         /* Update netdev from rtnl-change msg. */
611         if (change->mtu) {
612             dev->mtu = change->mtu;
613             dev->cache_valid |= VALID_MTU;
614             dev->netdev_mtu_error = 0;
615         }
616
617         if (!eth_addr_is_zero(change->addr)) {
618             memcpy(dev->etheraddr, change->addr, ETH_ADDR_LEN);
619             dev->cache_valid |= VALID_ETHERADDR;
620             dev->ether_addr_error = 0;
621         }
622
623         dev->ifindex = change->ifi_index;
624         dev->cache_valid |= VALID_IFINDEX;
625         dev->get_ifindex_error = 0;
626
627     } else {
628         netdev_linux_changed(dev, change->ifi_flags, 0);
629     }
630 }
631
632 static struct netdev *
633 netdev_linux_alloc(void)
634 {
635     struct netdev_linux *netdev = xzalloc(sizeof *netdev);
636     return &netdev->up;
637 }
638
639 static void
640 netdev_linux_common_construct(struct netdev_linux *netdev)
641 {
642     ovs_mutex_init(&netdev->mutex);
643     netdev->change_seq = 1;
644 }
645
646 /* Creates system and internal devices. */
647 static int
648 netdev_linux_construct(struct netdev *netdev_)
649 {
650     struct netdev_linux *netdev = netdev_linux_cast(netdev_);
651     int error;
652
653     netdev_linux_common_construct(netdev);
654
655     error = get_flags(&netdev->up, &netdev->ifi_flags);
656     if (error == ENODEV) {
657         if (netdev->up.netdev_class != &netdev_internal_class) {
658             /* The device does not exist, so don't allow it to be opened. */
659             return ENODEV;
660         } else {
661             /* "Internal" netdevs have to be created as netdev objects before
662              * they exist in the kernel, because creating them in the kernel
663              * happens by passing a netdev object to dpif_port_add().
664              * Therefore, ignore the error. */
665         }
666     }
667
668     return 0;
669 }
670
671 /* For most types of netdevs we open the device for each call of
672  * netdev_open().  However, this is not the case with tap devices,
673  * since it is only possible to open the device once.  In this
674  * situation we share a single file descriptor, and consequently
675  * buffers, across all readers.  Therefore once data is read it will
676  * be unavailable to other reads for tap devices. */
677 static int
678 netdev_linux_construct_tap(struct netdev *netdev_)
679 {
680     struct netdev_linux *netdev = netdev_linux_cast(netdev_);
681     static const char tap_dev[] = "/dev/net/tun";
682     const char *name = netdev_->name;
683     struct ifreq ifr;
684     int error;
685
686     netdev_linux_common_construct(netdev);
687
688     /* Open tap device. */
689     netdev->tap_fd = open(tap_dev, O_RDWR);
690     if (netdev->tap_fd < 0) {
691         error = errno;
692         VLOG_WARN("opening \"%s\" failed: %s", tap_dev, ovs_strerror(error));
693         return error;
694     }
695
696     /* Create tap device. */
697     ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
698     ovs_strzcpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
699     if (ioctl(netdev->tap_fd, TUNSETIFF, &ifr) == -1) {
700         VLOG_WARN("%s: creating tap device failed: %s", name,
701                   ovs_strerror(errno));
702         error = errno;
703         goto error_close;
704     }
705
706     /* Make non-blocking. */
707     error = set_nonblocking(netdev->tap_fd);
708     if (error) {
709         goto error_close;
710     }
711
712     return 0;
713
714 error_close:
715     close(netdev->tap_fd);
716     return error;
717 }
718
719 static void
720 netdev_linux_destruct(struct netdev *netdev_)
721 {
722     struct netdev_linux *netdev = netdev_linux_cast(netdev_);
723
724     if (netdev->tc && netdev->tc->ops->tc_destroy) {
725         netdev->tc->ops->tc_destroy(netdev->tc);
726     }
727
728     if (netdev_get_class(netdev_) == &netdev_tap_class
729         && netdev->tap_fd >= 0)
730     {
731         close(netdev->tap_fd);
732     }
733
734     if (netdev->miimon_interval > 0) {
735         int junk;
736         atomic_sub(&miimon_cnt, 1, &junk);
737     }
738
739     ovs_mutex_destroy(&netdev->mutex);
740 }
741
742 static void
743 netdev_linux_dealloc(struct netdev *netdev_)
744 {
745     struct netdev_linux *netdev = netdev_linux_cast(netdev_);
746     free(netdev);
747 }
748
749 static struct netdev_rx *
750 netdev_linux_rx_alloc(void)
751 {
752     struct netdev_rx_linux *rx = xzalloc(sizeof *rx);
753     return &rx->up;
754 }
755
756 static int
757 netdev_linux_rx_construct(struct netdev_rx *rx_)
758 {
759     struct netdev_rx_linux *rx = netdev_rx_linux_cast(rx_);
760     struct netdev *netdev_ = rx->up.netdev;
761     struct netdev_linux *netdev = netdev_linux_cast(netdev_);
762     int error;
763
764     ovs_mutex_lock(&netdev->mutex);
765     rx->is_tap = is_tap_netdev(netdev_);
766     if (rx->is_tap) {
767         rx->fd = netdev->tap_fd;
768     } else {
769         struct sockaddr_ll sll;
770         int ifindex;
771         /* Result of tcpdump -dd inbound */
772         static const struct sock_filter filt[] = {
773             { 0x28, 0, 0, 0xfffff004 }, /* ldh [0] */
774             { 0x15, 0, 1, 0x00000004 }, /* jeq #4     jt 2  jf 3 */
775             { 0x6, 0, 0, 0x00000000 },  /* ret #0 */
776             { 0x6, 0, 0, 0x0000ffff }   /* ret #65535 */
777         };
778         static const struct sock_fprog fprog = {
779             ARRAY_SIZE(filt), (struct sock_filter *) filt
780         };
781
782         /* Create file descriptor. */
783         rx->fd = socket(PF_PACKET, SOCK_RAW, 0);
784         if (rx->fd < 0) {
785             error = errno;
786             VLOG_ERR("failed to create raw socket (%s)", ovs_strerror(error));
787             goto error;
788         }
789
790         /* Set non-blocking mode. */
791         error = set_nonblocking(rx->fd);
792         if (error) {
793             goto error;
794         }
795
796         /* Get ethernet device index. */
797         error = get_ifindex(&netdev->up, &ifindex);
798         if (error) {
799             goto error;
800         }
801
802         /* Bind to specific ethernet device. */
803         memset(&sll, 0, sizeof sll);
804         sll.sll_family = AF_PACKET;
805         sll.sll_ifindex = ifindex;
806         sll.sll_protocol = (OVS_FORCE unsigned short int) htons(ETH_P_ALL);
807         if (bind(rx->fd, (struct sockaddr *) &sll, sizeof sll) < 0) {
808             error = errno;
809             VLOG_ERR("%s: failed to bind raw socket (%s)",
810                      netdev_get_name(netdev_), ovs_strerror(error));
811             goto error;
812         }
813
814         /* Filter for only inbound packets. */
815         error = setsockopt(rx->fd, SOL_SOCKET, SO_ATTACH_FILTER, &fprog,
816                            sizeof fprog);
817         if (error) {
818             error = errno;
819             VLOG_ERR("%s: failed to attach filter (%s)",
820                      netdev_get_name(netdev_), ovs_strerror(error));
821             goto error;
822         }
823     }
824     ovs_mutex_unlock(&netdev->mutex);
825
826     return 0;
827
828 error:
829     if (rx->fd >= 0) {
830         close(rx->fd);
831     }
832     ovs_mutex_unlock(&netdev->mutex);
833     return error;
834 }
835
836 static void
837 netdev_linux_rx_destruct(struct netdev_rx *rx_)
838 {
839     struct netdev_rx_linux *rx = netdev_rx_linux_cast(rx_);
840
841     if (!rx->is_tap) {
842         close(rx->fd);
843     }
844 }
845
846 static void
847 netdev_linux_rx_dealloc(struct netdev_rx *rx_)
848 {
849     struct netdev_rx_linux *rx = netdev_rx_linux_cast(rx_);
850
851     free(rx);
852 }
853
854 static int
855 netdev_linux_rx_recv(struct netdev_rx *rx_, void *data, size_t size)
856 {
857     struct netdev_rx_linux *rx = netdev_rx_linux_cast(rx_);
858     ssize_t retval;
859
860     do {
861         retval = (rx->is_tap
862                   ? read(rx->fd, data, size)
863                   : recv(rx->fd, data, size, MSG_TRUNC));
864     } while (retval < 0 && errno == EINTR);
865
866     if (retval >= 0) {
867         return retval > size ? -EMSGSIZE : retval;
868     } else {
869         if (errno != EAGAIN) {
870             VLOG_WARN_RL(&rl, "error receiving Ethernet packet on %s: %s",
871                          ovs_strerror(errno), netdev_rx_get_name(rx_));
872         }
873         return -errno;
874     }
875 }
876
877 static void
878 netdev_linux_rx_wait(struct netdev_rx *rx_)
879 {
880     struct netdev_rx_linux *rx = netdev_rx_linux_cast(rx_);
881     poll_fd_wait(rx->fd, POLLIN);
882 }
883
884 static int
885 netdev_linux_rx_drain(struct netdev_rx *rx_)
886 {
887     struct netdev_rx_linux *rx = netdev_rx_linux_cast(rx_);
888     if (rx->is_tap) {
889         struct ifreq ifr;
890         int error = af_inet_ifreq_ioctl(netdev_rx_get_name(rx_), &ifr,
891                                         SIOCGIFTXQLEN, "SIOCGIFTXQLEN");
892         if (error) {
893             return error;
894         }
895         drain_fd(rx->fd, ifr.ifr_qlen);
896         return 0;
897     } else {
898         return drain_rcvbuf(rx->fd);
899     }
900 }
901
902 /* Sends 'buffer' on 'netdev'.  Returns 0 if successful, otherwise a positive
903  * errno value.  Returns EAGAIN without blocking if the packet cannot be queued
904  * immediately.  Returns EMSGSIZE if a partial packet was transmitted or if
905  * the packet is too big or too small to transmit on the device.
906  *
907  * The caller retains ownership of 'buffer' in all cases.
908  *
909  * The kernel maintains a packet transmission queue, so the caller is not
910  * expected to do additional queuing of packets. */
911 static int
912 netdev_linux_send(struct netdev *netdev_, const void *data, size_t size)
913 {
914     for (;;) {
915         ssize_t retval;
916
917         if (!is_tap_netdev(netdev_)) {
918             /* Use our AF_PACKET socket to send to this device. */
919             struct sockaddr_ll sll;
920             struct msghdr msg;
921             struct iovec iov;
922             int ifindex;
923             int sock;
924
925             sock = af_packet_sock();
926             if (sock < 0) {
927                 return -sock;
928             }
929
930             ifindex = netdev_get_ifindex(netdev_);
931             if (ifindex < 0) {
932                 return -ifindex;
933             }
934
935             /* We don't bother setting most fields in sockaddr_ll because the
936              * kernel ignores them for SOCK_RAW. */
937             memset(&sll, 0, sizeof sll);
938             sll.sll_family = AF_PACKET;
939             sll.sll_ifindex = ifindex;
940
941             iov.iov_base = CONST_CAST(void *, data);
942             iov.iov_len = size;
943
944             msg.msg_name = &sll;
945             msg.msg_namelen = sizeof sll;
946             msg.msg_iov = &iov;
947             msg.msg_iovlen = 1;
948             msg.msg_control = NULL;
949             msg.msg_controllen = 0;
950             msg.msg_flags = 0;
951
952             retval = sendmsg(sock, &msg, 0);
953         } else {
954             /* Use the tap fd to send to this device.  This is essential for
955              * tap devices, because packets sent to a tap device with an
956              * AF_PACKET socket will loop back to be *received* again on the
957              * tap device.  This doesn't occur on other interface types
958              * because we attach a socket filter to the rx socket. */
959             struct netdev_linux *netdev = netdev_linux_cast(netdev_);
960
961             retval = write(netdev->tap_fd, data, size);
962         }
963
964         if (retval < 0) {
965             /* The Linux AF_PACKET implementation never blocks waiting for room
966              * for packets, instead returning ENOBUFS.  Translate this into
967              * EAGAIN for the caller. */
968             if (errno == ENOBUFS) {
969                 return EAGAIN;
970             } else if (errno == EINTR) {
971                 continue;
972             } else if (errno != EAGAIN) {
973                 VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: %s",
974                              netdev_get_name(netdev_), ovs_strerror(errno));
975             }
976             return errno;
977         } else if (retval != size) {
978             VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%zd bytes of "
979                          "%zu) on %s", retval, size, netdev_get_name(netdev_));
980             return EMSGSIZE;
981         } else {
982             return 0;
983         }
984     }
985 }
986
987 /* Registers with the poll loop to wake up from the next call to poll_block()
988  * when the packet transmission queue has sufficient room to transmit a packet
989  * with netdev_send().
990  *
991  * The kernel maintains a packet transmission queue, so the client is not
992  * expected to do additional queuing of packets.  Thus, this function is
993  * unlikely to ever be used.  It is included for completeness. */
994 static void
995 netdev_linux_send_wait(struct netdev *netdev)
996 {
997     if (is_tap_netdev(netdev)) {
998         /* TAP device always accepts packets.*/
999         poll_immediate_wake();
1000     }
1001 }
1002
1003 /* Attempts to set 'netdev''s MAC address to 'mac'.  Returns 0 if successful,
1004  * otherwise a positive errno value. */
1005 static int
1006 netdev_linux_set_etheraddr(struct netdev *netdev_,
1007                            const uint8_t mac[ETH_ADDR_LEN])
1008 {
1009     struct netdev_linux *netdev = netdev_linux_cast(netdev_);
1010     enum netdev_flags old_flags = 0;
1011     int error;
1012
1013     ovs_mutex_lock(&netdev->mutex);
1014
1015     if (netdev->cache_valid & VALID_ETHERADDR) {
1016         error = netdev->ether_addr_error;
1017         if (error || eth_addr_equals(netdev->etheraddr, mac)) {
1018             goto exit;
1019         }
1020         netdev->cache_valid &= ~VALID_ETHERADDR;
1021     }
1022
1023     /* Tap devices must be brought down before setting the address. */
1024     if (is_tap_netdev(netdev_)) {
1025         update_flags(netdev, NETDEV_UP, 0, &old_flags);
1026     }
1027     error = set_etheraddr(netdev_get_name(netdev_), mac);
1028     if (!error || error == ENODEV) {
1029         netdev->ether_addr_error = error;
1030         netdev->cache_valid |= VALID_ETHERADDR;
1031         if (!error) {
1032             memcpy(netdev->etheraddr, mac, ETH_ADDR_LEN);
1033         }
1034     }
1035
1036     if (is_tap_netdev(netdev_) && old_flags & NETDEV_UP) {
1037         update_flags(netdev, 0, NETDEV_UP, &old_flags);
1038     }
1039
1040 exit:
1041     ovs_mutex_unlock(&netdev->mutex);
1042     return error;
1043 }
1044
1045 /* Copies 'netdev''s MAC address to 'mac' which is passed as param. */
1046 static int
1047 netdev_linux_get_etheraddr(const struct netdev *netdev_,
1048                            uint8_t mac[ETH_ADDR_LEN])
1049 {
1050     struct netdev_linux *netdev = netdev_linux_cast(netdev_);
1051     int error;
1052
1053     ovs_mutex_lock(&netdev->mutex);
1054     if (!(netdev->cache_valid & VALID_ETHERADDR)) {
1055         netdev->ether_addr_error = get_etheraddr(netdev_get_name(netdev_),
1056                                                  netdev->etheraddr);
1057         netdev->cache_valid |= VALID_ETHERADDR;
1058     }
1059
1060     error = netdev->ether_addr_error;
1061     if (!error) {
1062         memcpy(mac, netdev->etheraddr, ETH_ADDR_LEN);
1063     }
1064     ovs_mutex_unlock(&netdev->mutex);
1065
1066     return error;
1067 }
1068
1069 static int
1070 netdev_linux_get_mtu__(struct netdev_linux *netdev, int *mtup)
1071 {
1072     int error;
1073
1074     if (!(netdev->cache_valid & VALID_MTU)) {
1075         struct ifreq ifr;
1076
1077         netdev->netdev_mtu_error = af_inet_ifreq_ioctl(
1078             netdev_get_name(&netdev->up), &ifr, SIOCGIFMTU, "SIOCGIFMTU");
1079         netdev->mtu = ifr.ifr_mtu;
1080         netdev->cache_valid |= VALID_MTU;
1081     }
1082
1083     error = netdev->netdev_mtu_error;
1084     if (!error) {
1085         *mtup = netdev->mtu;
1086     }
1087
1088     return error;
1089 }
1090
1091 /* Returns the maximum size of transmitted (and received) packets on 'netdev',
1092  * in bytes, not including the hardware header; thus, this is typically 1500
1093  * bytes for Ethernet devices. */
1094 static int
1095 netdev_linux_get_mtu(const struct netdev *netdev_, int *mtup)
1096 {
1097     struct netdev_linux *netdev = netdev_linux_cast(netdev_);
1098     int error;
1099
1100     ovs_mutex_lock(&netdev->mutex);
1101     error = netdev_linux_get_mtu__(netdev, mtup);
1102     ovs_mutex_unlock(&netdev->mutex);
1103
1104     return error;
1105 }
1106
1107 /* Sets the maximum size of transmitted (MTU) for given device using linux
1108  * networking ioctl interface.
1109  */
1110 static int
1111 netdev_linux_set_mtu(const struct netdev *netdev_, int mtu)
1112 {
1113     struct netdev_linux *netdev = netdev_linux_cast(netdev_);
1114     struct ifreq ifr;
1115     int error;
1116
1117     ovs_mutex_lock(&netdev->mutex);
1118     if (netdev->cache_valid & VALID_MTU) {
1119         error = netdev->netdev_mtu_error;
1120         if (error || netdev->mtu == mtu) {
1121             goto exit;
1122         }
1123         netdev->cache_valid &= ~VALID_MTU;
1124     }
1125     ifr.ifr_mtu = mtu;
1126     error = af_inet_ifreq_ioctl(netdev_get_name(netdev_), &ifr,
1127                                 SIOCSIFMTU, "SIOCSIFMTU");
1128     if (!error || error == ENODEV) {
1129         netdev->netdev_mtu_error = error;
1130         netdev->mtu = ifr.ifr_mtu;
1131         netdev->cache_valid |= VALID_MTU;
1132     }
1133 exit:
1134     ovs_mutex_unlock(&netdev->mutex);
1135     return error;
1136 }
1137
1138 /* Returns the ifindex of 'netdev', if successful, as a positive number.
1139  * On failure, returns a negative errno value. */
1140 static int
1141 netdev_linux_get_ifindex(const struct netdev *netdev_)
1142 {
1143     struct netdev_linux *netdev = netdev_linux_cast(netdev_);
1144     int ifindex, error;
1145
1146     ovs_mutex_lock(&netdev->mutex);
1147     error = get_ifindex(netdev_, &ifindex);
1148     ovs_mutex_unlock(&netdev->mutex);
1149
1150     return error ? -error : ifindex;
1151 }
1152
1153 static int
1154 netdev_linux_get_carrier(const struct netdev *netdev_, bool *carrier)
1155 {
1156     struct netdev_linux *netdev = netdev_linux_cast(netdev_);
1157
1158     ovs_mutex_lock(&netdev->mutex);
1159     if (netdev->miimon_interval > 0) {
1160         *carrier = netdev->miimon;
1161     } else {
1162         *carrier = (netdev->ifi_flags & IFF_RUNNING) != 0;
1163     }
1164     ovs_mutex_unlock(&netdev->mutex);
1165
1166     return 0;
1167 }
1168
1169 static long long int
1170 netdev_linux_get_carrier_resets(const struct netdev *netdev_)
1171 {
1172     struct netdev_linux *netdev = netdev_linux_cast(netdev_);
1173     long long int carrier_resets;
1174
1175     ovs_mutex_lock(&netdev->mutex);
1176     carrier_resets = netdev->carrier_resets;
1177     ovs_mutex_unlock(&netdev->mutex);
1178
1179     return carrier_resets;
1180 }
1181
1182 static int
1183 netdev_linux_do_miimon(const char *name, int cmd, const char *cmd_name,
1184                        struct mii_ioctl_data *data)
1185 {
1186     struct ifreq ifr;
1187     int error;
1188
1189     memset(&ifr, 0, sizeof ifr);
1190     memcpy(&ifr.ifr_data, data, sizeof *data);
1191     error = af_inet_ifreq_ioctl(name, &ifr, cmd, cmd_name);
1192     memcpy(data, &ifr.ifr_data, sizeof *data);
1193
1194     return error;
1195 }
1196
1197 static int
1198 netdev_linux_get_miimon(const char *name, bool *miimon)
1199 {
1200     struct mii_ioctl_data data;
1201     int error;
1202
1203     *miimon = false;
1204
1205     memset(&data, 0, sizeof data);
1206     error = netdev_linux_do_miimon(name, SIOCGMIIPHY, "SIOCGMIIPHY", &data);
1207     if (!error) {
1208         /* data.phy_id is filled out by previous SIOCGMIIPHY miimon call. */
1209         data.reg_num = MII_BMSR;
1210         error = netdev_linux_do_miimon(name, SIOCGMIIREG, "SIOCGMIIREG",
1211                                        &data);
1212
1213         if (!error) {
1214             *miimon = !!(data.val_out & BMSR_LSTATUS);
1215         } else {
1216             VLOG_WARN_RL(&rl, "%s: failed to query MII", name);
1217         }
1218     } else {
1219         struct ethtool_cmd ecmd;
1220
1221         VLOG_DBG_RL(&rl, "%s: failed to query MII, falling back to ethtool",
1222                     name);
1223
1224         COVERAGE_INC(netdev_get_ethtool);
1225         memset(&ecmd, 0, sizeof ecmd);
1226         error = netdev_linux_do_ethtool(name, &ecmd, ETHTOOL_GLINK,
1227                                         "ETHTOOL_GLINK");
1228         if (!error) {
1229             struct ethtool_value eval;
1230
1231             memcpy(&eval, &ecmd, sizeof eval);
1232             *miimon = !!eval.data;
1233         } else {
1234             VLOG_WARN_RL(&rl, "%s: ethtool link status failed", name);
1235         }
1236     }
1237
1238     return error;
1239 }
1240
1241 static int
1242 netdev_linux_set_miimon_interval(struct netdev *netdev_,
1243                                  long long int interval)
1244 {
1245     struct netdev_linux *netdev = netdev_linux_cast(netdev_);
1246
1247     ovs_mutex_lock(&netdev->mutex);
1248     interval = interval > 0 ? MAX(interval, 100) : 0;
1249     if (netdev->miimon_interval != interval) {
1250         int junk;
1251
1252         if (interval && !netdev->miimon_interval) {
1253             atomic_add(&miimon_cnt, 1, &junk);
1254         } else if (!interval && netdev->miimon_interval) {
1255             atomic_sub(&miimon_cnt, 1, &junk);
1256         }
1257
1258         netdev->miimon_interval = interval;
1259         timer_set_expired(&netdev->miimon_timer);
1260     }
1261     ovs_mutex_unlock(&netdev->mutex);
1262
1263     return 0;
1264 }
1265
1266 static void
1267 netdev_linux_miimon_run(void)
1268 {
1269     struct shash device_shash;
1270     struct shash_node *node;
1271
1272     shash_init(&device_shash);
1273     netdev_get_devices(&netdev_linux_class, &device_shash);
1274     SHASH_FOR_EACH (node, &device_shash) {
1275         struct netdev *netdev = node->data;
1276         struct netdev_linux *dev = netdev_linux_cast(netdev);
1277         bool miimon;
1278
1279         ovs_mutex_lock(&dev->mutex);
1280         if (dev->miimon_interval > 0 && timer_expired(&dev->miimon_timer)) {
1281             netdev_linux_get_miimon(dev->up.name, &miimon);
1282             if (miimon != dev->miimon) {
1283                 dev->miimon = miimon;
1284                 netdev_linux_changed(dev, dev->ifi_flags, 0);
1285             }
1286
1287             timer_set_duration(&dev->miimon_timer, dev->miimon_interval);
1288         }
1289         ovs_mutex_unlock(&dev->mutex);
1290         netdev_close(netdev);
1291     }
1292
1293     shash_destroy(&device_shash);
1294 }
1295
1296 static void
1297 netdev_linux_miimon_wait(void)
1298 {
1299     struct shash device_shash;
1300     struct shash_node *node;
1301
1302     shash_init(&device_shash);
1303     netdev_get_devices(&netdev_linux_class, &device_shash);
1304     SHASH_FOR_EACH (node, &device_shash) {
1305         struct netdev *netdev = node->data;
1306         struct netdev_linux *dev = netdev_linux_cast(netdev);
1307
1308         ovs_mutex_lock(&dev->mutex);
1309         if (dev->miimon_interval > 0) {
1310             timer_wait(&dev->miimon_timer);
1311         }
1312         ovs_mutex_unlock(&dev->mutex);
1313         netdev_close(netdev);
1314     }
1315     shash_destroy(&device_shash);
1316 }
1317
1318 /* Check whether we can we use RTM_GETLINK to get network device statistics.
1319  * In pre-2.6.19 kernels, this was only available if wireless extensions were
1320  * enabled. */
1321 static bool
1322 check_for_working_netlink_stats(void)
1323 {
1324     /* Decide on the netdev_get_stats() implementation to use.  Netlink is
1325      * preferable, so if that works, we'll use it. */
1326     int ifindex = do_get_ifindex("lo");
1327     if (ifindex < 0) {
1328         VLOG_WARN("failed to get ifindex for lo, "
1329                   "obtaining netdev stats from proc");
1330         return false;
1331     } else {
1332         struct netdev_stats stats;
1333         int error = get_stats_via_netlink(ifindex, &stats);
1334         if (!error) {
1335             VLOG_DBG("obtaining netdev stats via rtnetlink");
1336             return true;
1337         } else {
1338             VLOG_INFO("RTM_GETLINK failed (%s), obtaining netdev stats "
1339                       "via proc (you are probably running a pre-2.6.19 "
1340                       "kernel)", ovs_strerror(error));
1341             return false;
1342         }
1343     }
1344 }
1345
1346 static void
1347 swap_uint64(uint64_t *a, uint64_t *b)
1348 {
1349     uint64_t tmp = *a;
1350     *a = *b;
1351     *b = tmp;
1352 }
1353
1354 /* Copies 'src' into 'dst', performing format conversion in the process.
1355  *
1356  * 'src' is allowed to be misaligned. */
1357 static void
1358 netdev_stats_from_ovs_vport_stats(struct netdev_stats *dst,
1359                                   const struct ovs_vport_stats *src)
1360 {
1361     dst->rx_packets = get_unaligned_u64(&src->rx_packets);
1362     dst->tx_packets = get_unaligned_u64(&src->tx_packets);
1363     dst->rx_bytes = get_unaligned_u64(&src->rx_bytes);
1364     dst->tx_bytes = get_unaligned_u64(&src->tx_bytes);
1365     dst->rx_errors = get_unaligned_u64(&src->rx_errors);
1366     dst->tx_errors = get_unaligned_u64(&src->tx_errors);
1367     dst->rx_dropped = get_unaligned_u64(&src->rx_dropped);
1368     dst->tx_dropped = get_unaligned_u64(&src->tx_dropped);
1369     dst->multicast = 0;
1370     dst->collisions = 0;
1371     dst->rx_length_errors = 0;
1372     dst->rx_over_errors = 0;
1373     dst->rx_crc_errors = 0;
1374     dst->rx_frame_errors = 0;
1375     dst->rx_fifo_errors = 0;
1376     dst->rx_missed_errors = 0;
1377     dst->tx_aborted_errors = 0;
1378     dst->tx_carrier_errors = 0;
1379     dst->tx_fifo_errors = 0;
1380     dst->tx_heartbeat_errors = 0;
1381     dst->tx_window_errors = 0;
1382 }
1383
1384 static int
1385 get_stats_via_vport__(const struct netdev *netdev, struct netdev_stats *stats)
1386 {
1387     struct dpif_linux_vport reply;
1388     struct ofpbuf *buf;
1389     int error;
1390
1391     error = dpif_linux_vport_get(netdev_get_name(netdev), &reply, &buf);
1392     if (error) {
1393         return error;
1394     } else if (!reply.stats) {
1395         ofpbuf_delete(buf);
1396         return EOPNOTSUPP;
1397     }
1398
1399     netdev_stats_from_ovs_vport_stats(stats, reply.stats);
1400
1401     ofpbuf_delete(buf);
1402
1403     return 0;
1404 }
1405
1406 static void
1407 get_stats_via_vport(const struct netdev *netdev_,
1408                     struct netdev_stats *stats)
1409 {
1410     struct netdev_linux *netdev = netdev_linux_cast(netdev_);
1411
1412     if (!netdev->vport_stats_error ||
1413         !(netdev->cache_valid & VALID_VPORT_STAT_ERROR)) {
1414         int error;
1415
1416         error = get_stats_via_vport__(netdev_, stats);
1417         if (error && error != ENOENT) {
1418             VLOG_WARN_RL(&rl, "%s: obtaining netdev stats via vport failed "
1419                          "(%s)",
1420                          netdev_get_name(netdev_), ovs_strerror(error));
1421         }
1422         netdev->vport_stats_error = error;
1423         netdev->cache_valid |= VALID_VPORT_STAT_ERROR;
1424     }
1425 }
1426
1427 static int
1428 netdev_linux_sys_get_stats(const struct netdev *netdev_,
1429                            struct netdev_stats *stats)
1430 {
1431     static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
1432     static int use_netlink_stats;
1433     int error;
1434
1435     if (ovsthread_once_start(&once)) {
1436         use_netlink_stats = check_for_working_netlink_stats();
1437         ovsthread_once_done(&once);
1438     }
1439
1440     if (use_netlink_stats) {
1441         int ifindex;
1442
1443         error = get_ifindex(netdev_, &ifindex);
1444         if (!error) {
1445             error = get_stats_via_netlink(ifindex, stats);
1446         }
1447     } else {
1448         error = get_stats_via_proc(netdev_get_name(netdev_), stats);
1449     }
1450
1451     if (error) {
1452         VLOG_WARN_RL(&rl, "%s: linux-sys get stats failed %d",
1453                       netdev_get_name(netdev_), error);
1454     }
1455     return error;
1456
1457 }
1458
1459 /* Retrieves current device stats for 'netdev-linux'. */
1460 static int
1461 netdev_linux_get_stats(const struct netdev *netdev_,
1462                        struct netdev_stats *stats)
1463 {
1464     struct netdev_linux *netdev = netdev_linux_cast(netdev_);
1465     struct netdev_stats dev_stats;
1466     int error;
1467
1468     ovs_mutex_lock(&netdev->mutex);
1469     get_stats_via_vport(netdev_, stats);
1470     error = netdev_linux_sys_get_stats(netdev_, &dev_stats);
1471     if (error) {
1472         if (!netdev->vport_stats_error) {
1473             error = 0;
1474         }
1475     } else if (netdev->vport_stats_error) {
1476         /* stats not available from OVS then use ioctl stats. */
1477         *stats = dev_stats;
1478     } else {
1479         stats->rx_errors           += dev_stats.rx_errors;
1480         stats->tx_errors           += dev_stats.tx_errors;
1481         stats->rx_dropped          += dev_stats.rx_dropped;
1482         stats->tx_dropped          += dev_stats.tx_dropped;
1483         stats->multicast           += dev_stats.multicast;
1484         stats->collisions          += dev_stats.collisions;
1485         stats->rx_length_errors    += dev_stats.rx_length_errors;
1486         stats->rx_over_errors      += dev_stats.rx_over_errors;
1487         stats->rx_crc_errors       += dev_stats.rx_crc_errors;
1488         stats->rx_frame_errors     += dev_stats.rx_frame_errors;
1489         stats->rx_fifo_errors      += dev_stats.rx_fifo_errors;
1490         stats->rx_missed_errors    += dev_stats.rx_missed_errors;
1491         stats->tx_aborted_errors   += dev_stats.tx_aborted_errors;
1492         stats->tx_carrier_errors   += dev_stats.tx_carrier_errors;
1493         stats->tx_fifo_errors      += dev_stats.tx_fifo_errors;
1494         stats->tx_heartbeat_errors += dev_stats.tx_heartbeat_errors;
1495         stats->tx_window_errors    += dev_stats.tx_window_errors;
1496     }
1497     ovs_mutex_unlock(&netdev->mutex);
1498
1499     return error;
1500 }
1501
1502 /* Retrieves current device stats for 'netdev-tap' netdev or
1503  * netdev-internal. */
1504 static int
1505 netdev_tap_get_stats(const struct netdev *netdev_, struct netdev_stats *stats)
1506 {
1507     struct netdev_linux *netdev = netdev_linux_cast(netdev_);
1508     struct netdev_stats dev_stats;
1509     int error;
1510
1511     ovs_mutex_lock(&netdev->mutex);
1512     get_stats_via_vport(netdev_, stats);
1513     error = netdev_linux_sys_get_stats(netdev_, &dev_stats);
1514     if (error) {
1515         if (!netdev->vport_stats_error) {
1516             error = 0;
1517         }
1518     } else if (netdev->vport_stats_error) {
1519         /* Transmit and receive stats will appear to be swapped relative to the
1520          * other ports since we are the one sending the data, not a remote
1521          * computer.  For consistency, we swap them back here. This does not
1522          * apply if we are getting stats from the vport layer because it always
1523          * tracks stats from the perspective of the switch. */
1524
1525         *stats = dev_stats;
1526         swap_uint64(&stats->rx_packets, &stats->tx_packets);
1527         swap_uint64(&stats->rx_bytes, &stats->tx_bytes);
1528         swap_uint64(&stats->rx_errors, &stats->tx_errors);
1529         swap_uint64(&stats->rx_dropped, &stats->tx_dropped);
1530         stats->rx_length_errors = 0;
1531         stats->rx_over_errors = 0;
1532         stats->rx_crc_errors = 0;
1533         stats->rx_frame_errors = 0;
1534         stats->rx_fifo_errors = 0;
1535         stats->rx_missed_errors = 0;
1536         stats->tx_aborted_errors = 0;
1537         stats->tx_carrier_errors = 0;
1538         stats->tx_fifo_errors = 0;
1539         stats->tx_heartbeat_errors = 0;
1540         stats->tx_window_errors = 0;
1541     } else {
1542         stats->rx_dropped          += dev_stats.tx_dropped;
1543         stats->tx_dropped          += dev_stats.rx_dropped;
1544
1545         stats->rx_errors           += dev_stats.tx_errors;
1546         stats->tx_errors           += dev_stats.rx_errors;
1547
1548         stats->multicast           += dev_stats.multicast;
1549         stats->collisions          += dev_stats.collisions;
1550     }
1551     ovs_mutex_unlock(&netdev->mutex);
1552
1553     return error;
1554 }
1555
1556 static int
1557 netdev_internal_get_stats(const struct netdev *netdev_,
1558                           struct netdev_stats *stats)
1559 {
1560     struct netdev_linux *netdev = netdev_linux_cast(netdev_);
1561     int error;
1562
1563     ovs_mutex_lock(&netdev->mutex);
1564     get_stats_via_vport(netdev_, stats);
1565     error = netdev->vport_stats_error;
1566     ovs_mutex_unlock(&netdev->mutex);
1567
1568     return error;
1569 }
1570
1571 static int
1572 netdev_internal_set_stats(struct netdev *netdev,
1573                           const struct netdev_stats *stats)
1574 {
1575     struct ovs_vport_stats vport_stats;
1576     struct dpif_linux_vport vport;
1577     int err;
1578
1579     vport_stats.rx_packets = stats->rx_packets;
1580     vport_stats.tx_packets = stats->tx_packets;
1581     vport_stats.rx_bytes = stats->rx_bytes;
1582     vport_stats.tx_bytes = stats->tx_bytes;
1583     vport_stats.rx_errors = stats->rx_errors;
1584     vport_stats.tx_errors = stats->tx_errors;
1585     vport_stats.rx_dropped = stats->rx_dropped;
1586     vport_stats.tx_dropped = stats->tx_dropped;
1587
1588     dpif_linux_vport_init(&vport);
1589     vport.cmd = OVS_VPORT_CMD_SET;
1590     vport.name = netdev_get_name(netdev);
1591     vport.stats = &vport_stats;
1592
1593     err = dpif_linux_vport_transact(&vport, NULL, NULL);
1594
1595     /* If the vport layer doesn't know about the device, that doesn't mean it
1596      * doesn't exist (after all were able to open it when netdev_open() was
1597      * called), it just means that it isn't attached and we'll be getting
1598      * stats a different way. */
1599     if (err == ENODEV) {
1600         err = EOPNOTSUPP;
1601     }
1602
1603     return err;
1604 }
1605
1606 static void
1607 netdev_linux_read_features(struct netdev_linux *netdev)
1608 {
1609     struct ethtool_cmd ecmd;
1610     uint32_t speed;
1611     int error;
1612
1613     if (netdev->cache_valid & VALID_FEATURES) {
1614         return;
1615     }
1616
1617     COVERAGE_INC(netdev_get_ethtool);
1618     memset(&ecmd, 0, sizeof ecmd);
1619     error = netdev_linux_do_ethtool(netdev->up.name, &ecmd,
1620                                     ETHTOOL_GSET, "ETHTOOL_GSET");
1621     if (error) {
1622         goto out;
1623     }
1624
1625     /* Supported features. */
1626     netdev->supported = 0;
1627     if (ecmd.supported & SUPPORTED_10baseT_Half) {
1628         netdev->supported |= NETDEV_F_10MB_HD;
1629     }
1630     if (ecmd.supported & SUPPORTED_10baseT_Full) {
1631         netdev->supported |= NETDEV_F_10MB_FD;
1632     }
1633     if (ecmd.supported & SUPPORTED_100baseT_Half)  {
1634         netdev->supported |= NETDEV_F_100MB_HD;
1635     }
1636     if (ecmd.supported & SUPPORTED_100baseT_Full) {
1637         netdev->supported |= NETDEV_F_100MB_FD;
1638     }
1639     if (ecmd.supported & SUPPORTED_1000baseT_Half) {
1640         netdev->supported |= NETDEV_F_1GB_HD;
1641     }
1642     if (ecmd.supported & SUPPORTED_1000baseT_Full) {
1643         netdev->supported |= NETDEV_F_1GB_FD;
1644     }
1645     if (ecmd.supported & SUPPORTED_10000baseT_Full) {
1646         netdev->supported |= NETDEV_F_10GB_FD;
1647     }
1648     if (ecmd.supported & SUPPORTED_TP) {
1649         netdev->supported |= NETDEV_F_COPPER;
1650     }
1651     if (ecmd.supported & SUPPORTED_FIBRE) {
1652         netdev->supported |= NETDEV_F_FIBER;
1653     }
1654     if (ecmd.supported & SUPPORTED_Autoneg) {
1655         netdev->supported |= NETDEV_F_AUTONEG;
1656     }
1657     if (ecmd.supported & SUPPORTED_Pause) {
1658         netdev->supported |= NETDEV_F_PAUSE;
1659     }
1660     if (ecmd.supported & SUPPORTED_Asym_Pause) {
1661         netdev->supported |= NETDEV_F_PAUSE_ASYM;
1662     }
1663
1664     /* Advertised features. */
1665     netdev->advertised = 0;
1666     if (ecmd.advertising & ADVERTISED_10baseT_Half) {
1667         netdev->advertised |= NETDEV_F_10MB_HD;
1668     }
1669     if (ecmd.advertising & ADVERTISED_10baseT_Full) {
1670         netdev->advertised |= NETDEV_F_10MB_FD;
1671     }
1672     if (ecmd.advertising & ADVERTISED_100baseT_Half) {
1673         netdev->advertised |= NETDEV_F_100MB_HD;
1674     }
1675     if (ecmd.advertising & ADVERTISED_100baseT_Full) {
1676         netdev->advertised |= NETDEV_F_100MB_FD;
1677     }
1678     if (ecmd.advertising & ADVERTISED_1000baseT_Half) {
1679         netdev->advertised |= NETDEV_F_1GB_HD;
1680     }
1681     if (ecmd.advertising & ADVERTISED_1000baseT_Full) {
1682         netdev->advertised |= NETDEV_F_1GB_FD;
1683     }
1684     if (ecmd.advertising & ADVERTISED_10000baseT_Full) {
1685         netdev->advertised |= NETDEV_F_10GB_FD;
1686     }
1687     if (ecmd.advertising & ADVERTISED_TP) {
1688         netdev->advertised |= NETDEV_F_COPPER;
1689     }
1690     if (ecmd.advertising & ADVERTISED_FIBRE) {
1691         netdev->advertised |= NETDEV_F_FIBER;
1692     }
1693     if (ecmd.advertising & ADVERTISED_Autoneg) {
1694         netdev->advertised |= NETDEV_F_AUTONEG;
1695     }
1696     if (ecmd.advertising & ADVERTISED_Pause) {
1697         netdev->advertised |= NETDEV_F_PAUSE;
1698     }
1699     if (ecmd.advertising & ADVERTISED_Asym_Pause) {
1700         netdev->advertised |= NETDEV_F_PAUSE_ASYM;
1701     }
1702
1703     /* Current settings. */
1704     speed = ecmd.speed;
1705     if (speed == SPEED_10) {
1706         netdev->current = ecmd.duplex ? NETDEV_F_10MB_FD : NETDEV_F_10MB_HD;
1707     } else if (speed == SPEED_100) {
1708         netdev->current = ecmd.duplex ? NETDEV_F_100MB_FD : NETDEV_F_100MB_HD;
1709     } else if (speed == SPEED_1000) {
1710         netdev->current = ecmd.duplex ? NETDEV_F_1GB_FD : NETDEV_F_1GB_HD;
1711     } else if (speed == SPEED_10000) {
1712         netdev->current = NETDEV_F_10GB_FD;
1713     } else if (speed == 40000) {
1714         netdev->current = NETDEV_F_40GB_FD;
1715     } else if (speed == 100000) {
1716         netdev->current = NETDEV_F_100GB_FD;
1717     } else if (speed == 1000000) {
1718         netdev->current = NETDEV_F_1TB_FD;
1719     } else {
1720         netdev->current = 0;
1721     }
1722
1723     if (ecmd.port == PORT_TP) {
1724         netdev->current |= NETDEV_F_COPPER;
1725     } else if (ecmd.port == PORT_FIBRE) {
1726         netdev->current |= NETDEV_F_FIBER;
1727     }
1728
1729     if (ecmd.autoneg) {
1730         netdev->current |= NETDEV_F_AUTONEG;
1731     }
1732
1733 out:
1734     netdev->cache_valid |= VALID_FEATURES;
1735     netdev->get_features_error = error;
1736 }
1737
1738 /* Stores the features supported by 'netdev' into of '*current', '*advertised',
1739  * '*supported', and '*peer'.  Each value is a bitmap of NETDEV_* bits.
1740  * Returns 0 if successful, otherwise a positive errno value. */
1741 static int
1742 netdev_linux_get_features(const struct netdev *netdev_,
1743                           enum netdev_features *current,
1744                           enum netdev_features *advertised,
1745                           enum netdev_features *supported,
1746                           enum netdev_features *peer)
1747 {
1748     struct netdev_linux *netdev = netdev_linux_cast(netdev_);
1749     int error;
1750
1751     ovs_mutex_lock(&netdev->mutex);
1752     netdev_linux_read_features(netdev);
1753     if (!netdev->get_features_error) {
1754         *current = netdev->current;
1755         *advertised = netdev->advertised;
1756         *supported = netdev->supported;
1757         *peer = 0;              /* XXX */
1758     }
1759     error = netdev->get_features_error;
1760     ovs_mutex_unlock(&netdev->mutex);
1761
1762     return error;
1763 }
1764
1765 /* Set the features advertised by 'netdev' to 'advertise'. */
1766 static int
1767 netdev_linux_set_advertisements(struct netdev *netdev_,
1768                                 enum netdev_features advertise)
1769 {
1770     struct netdev_linux *netdev = netdev_linux_cast(netdev_);
1771     struct ethtool_cmd ecmd;
1772     int error;
1773
1774     ovs_mutex_lock(&netdev->mutex);
1775
1776     COVERAGE_INC(netdev_get_ethtool);
1777     memset(&ecmd, 0, sizeof ecmd);
1778     error = netdev_linux_do_ethtool(netdev_get_name(netdev_), &ecmd,
1779                                     ETHTOOL_GSET, "ETHTOOL_GSET");
1780     if (error) {
1781         goto exit;
1782     }
1783
1784     ecmd.advertising = 0;
1785     if (advertise & NETDEV_F_10MB_HD) {
1786         ecmd.advertising |= ADVERTISED_10baseT_Half;
1787     }
1788     if (advertise & NETDEV_F_10MB_FD) {
1789         ecmd.advertising |= ADVERTISED_10baseT_Full;
1790     }
1791     if (advertise & NETDEV_F_100MB_HD) {
1792         ecmd.advertising |= ADVERTISED_100baseT_Half;
1793     }
1794     if (advertise & NETDEV_F_100MB_FD) {
1795         ecmd.advertising |= ADVERTISED_100baseT_Full;
1796     }
1797     if (advertise & NETDEV_F_1GB_HD) {
1798         ecmd.advertising |= ADVERTISED_1000baseT_Half;
1799     }
1800     if (advertise & NETDEV_F_1GB_FD) {
1801         ecmd.advertising |= ADVERTISED_1000baseT_Full;
1802     }
1803     if (advertise & NETDEV_F_10GB_FD) {
1804         ecmd.advertising |= ADVERTISED_10000baseT_Full;
1805     }
1806     if (advertise & NETDEV_F_COPPER) {
1807         ecmd.advertising |= ADVERTISED_TP;
1808     }
1809     if (advertise & NETDEV_F_FIBER) {
1810         ecmd.advertising |= ADVERTISED_FIBRE;
1811     }
1812     if (advertise & NETDEV_F_AUTONEG) {
1813         ecmd.advertising |= ADVERTISED_Autoneg;
1814     }
1815     if (advertise & NETDEV_F_PAUSE) {
1816         ecmd.advertising |= ADVERTISED_Pause;
1817     }
1818     if (advertise & NETDEV_F_PAUSE_ASYM) {
1819         ecmd.advertising |= ADVERTISED_Asym_Pause;
1820     }
1821     COVERAGE_INC(netdev_set_ethtool);
1822     error = netdev_linux_do_ethtool(netdev_get_name(netdev_), &ecmd,
1823                                     ETHTOOL_SSET, "ETHTOOL_SSET");
1824
1825 exit:
1826     ovs_mutex_unlock(&netdev->mutex);
1827     return error;
1828 }
1829
1830 /* Attempts to set input rate limiting (policing) policy.  Returns 0 if
1831  * successful, otherwise a positive errno value. */
1832 static int
1833 netdev_linux_set_policing(struct netdev *netdev_,
1834                           uint32_t kbits_rate, uint32_t kbits_burst)
1835 {
1836     struct netdev_linux *netdev = netdev_linux_cast(netdev_);
1837     const char *netdev_name = netdev_get_name(netdev_);
1838     int error;
1839
1840     kbits_burst = (!kbits_rate ? 0       /* Force to 0 if no rate specified. */
1841                    : !kbits_burst ? 1000 /* Default to 1000 kbits if 0. */
1842                    : kbits_burst);       /* Stick with user-specified value. */
1843
1844     ovs_mutex_lock(&netdev->mutex);
1845     if (netdev->cache_valid & VALID_POLICING) {
1846         error = netdev->netdev_policing_error;
1847         if (error || (netdev->kbits_rate == kbits_rate &&
1848                       netdev->kbits_burst == kbits_burst)) {
1849             /* Assume that settings haven't changed since we last set them. */
1850             goto out;
1851         }
1852         netdev->cache_valid &= ~VALID_POLICING;
1853     }
1854
1855     COVERAGE_INC(netdev_set_policing);
1856     /* Remove any existing ingress qdisc. */
1857     error = tc_add_del_ingress_qdisc(netdev_, false);
1858     if (error) {
1859         VLOG_WARN_RL(&rl, "%s: removing policing failed: %s",
1860                      netdev_name, ovs_strerror(error));
1861         goto out;
1862     }
1863
1864     if (kbits_rate) {
1865         error = tc_add_del_ingress_qdisc(netdev_, true);
1866         if (error) {
1867             VLOG_WARN_RL(&rl, "%s: adding policing qdisc failed: %s",
1868                          netdev_name, ovs_strerror(error));
1869             goto out;
1870         }
1871
1872         error = tc_add_policer(netdev_, kbits_rate, kbits_burst);
1873         if (error){
1874             VLOG_WARN_RL(&rl, "%s: adding policing action failed: %s",
1875                     netdev_name, ovs_strerror(error));
1876             goto out;
1877         }
1878     }
1879
1880     netdev->kbits_rate = kbits_rate;
1881     netdev->kbits_burst = kbits_burst;
1882
1883 out:
1884     if (!error || error == ENODEV) {
1885         netdev->netdev_policing_error = error;
1886         netdev->cache_valid |= VALID_POLICING;
1887     }
1888     ovs_mutex_unlock(&netdev->mutex);
1889     return error;
1890 }
1891
1892 static int
1893 netdev_linux_get_qos_types(const struct netdev *netdev OVS_UNUSED,
1894                            struct sset *types)
1895 {
1896     const struct tc_ops *const *opsp;
1897
1898     for (opsp = tcs; *opsp != NULL; opsp++) {
1899         const struct tc_ops *ops = *opsp;
1900         if (ops->tc_install && ops->ovs_name[0] != '\0') {
1901             sset_add(types, ops->ovs_name);
1902         }
1903     }
1904     return 0;
1905 }
1906
1907 static const struct tc_ops *
1908 tc_lookup_ovs_name(const char *name)
1909 {
1910     const struct tc_ops *const *opsp;
1911
1912     for (opsp = tcs; *opsp != NULL; opsp++) {
1913         const struct tc_ops *ops = *opsp;
1914         if (!strcmp(name, ops->ovs_name)) {
1915             return ops;
1916         }
1917     }
1918     return NULL;
1919 }
1920
1921 static const struct tc_ops *
1922 tc_lookup_linux_name(const char *name)
1923 {
1924     const struct tc_ops *const *opsp;
1925
1926     for (opsp = tcs; *opsp != NULL; opsp++) {
1927         const struct tc_ops *ops = *opsp;
1928         if (ops->linux_name && !strcmp(name, ops->linux_name)) {
1929             return ops;
1930         }
1931     }
1932     return NULL;
1933 }
1934
1935 static struct tc_queue *
1936 tc_find_queue__(const struct netdev *netdev_, unsigned int queue_id,
1937                 size_t hash)
1938 {
1939     struct netdev_linux *netdev = netdev_linux_cast(netdev_);
1940     struct tc_queue *queue;
1941
1942     HMAP_FOR_EACH_IN_BUCKET (queue, hmap_node, hash, &netdev->tc->queues) {
1943         if (queue->queue_id == queue_id) {
1944             return queue;
1945         }
1946     }
1947     return NULL;
1948 }
1949
1950 static struct tc_queue *
1951 tc_find_queue(const struct netdev *netdev, unsigned int queue_id)
1952 {
1953     return tc_find_queue__(netdev, queue_id, hash_int(queue_id, 0));
1954 }
1955
1956 static int
1957 netdev_linux_get_qos_capabilities(const struct netdev *netdev OVS_UNUSED,
1958                                   const char *type,
1959                                   struct netdev_qos_capabilities *caps)
1960 {
1961     const struct tc_ops *ops = tc_lookup_ovs_name(type);
1962     if (!ops) {
1963         return EOPNOTSUPP;
1964     }
1965     caps->n_queues = ops->n_queues;
1966     return 0;
1967 }
1968
1969 static int
1970 netdev_linux_get_qos(const struct netdev *netdev_,
1971                      const char **typep, struct smap *details)
1972 {
1973     struct netdev_linux *netdev = netdev_linux_cast(netdev_);
1974     int error;
1975
1976     ovs_mutex_lock(&netdev->mutex);
1977     error = tc_query_qdisc(netdev_);
1978     if (!error) {
1979         *typep = netdev->tc->ops->ovs_name;
1980         error = (netdev->tc->ops->qdisc_get
1981                  ? netdev->tc->ops->qdisc_get(netdev_, details)
1982                  : 0);
1983     }
1984     ovs_mutex_unlock(&netdev->mutex);
1985
1986     return error;
1987 }
1988
1989 static int
1990 netdev_linux_set_qos(struct netdev *netdev_,
1991                      const char *type, const struct smap *details)
1992 {
1993     struct netdev_linux *netdev = netdev_linux_cast(netdev_);
1994     const struct tc_ops *new_ops;
1995     int error;
1996
1997     new_ops = tc_lookup_ovs_name(type);
1998     if (!new_ops || !new_ops->tc_install) {
1999         return EOPNOTSUPP;
2000     }
2001
2002     ovs_mutex_lock(&netdev->mutex);
2003     error = tc_query_qdisc(netdev_);
2004     if (error) {
2005         goto exit;
2006     }
2007
2008     if (new_ops == netdev->tc->ops) {
2009         error = new_ops->qdisc_set ? new_ops->qdisc_set(netdev_, details) : 0;
2010     } else {
2011         /* Delete existing qdisc. */
2012         error = tc_del_qdisc(netdev_);
2013         if (error) {
2014             goto exit;
2015         }
2016         ovs_assert(netdev->tc == NULL);
2017
2018         /* Install new qdisc. */
2019         error = new_ops->tc_install(netdev_, details);
2020         ovs_assert((error == 0) == (netdev->tc != NULL));
2021     }
2022
2023 exit:
2024     ovs_mutex_unlock(&netdev->mutex);
2025     return error;
2026 }
2027
2028 static int
2029 netdev_linux_get_queue(const struct netdev *netdev_,
2030                        unsigned int queue_id, struct smap *details)
2031 {
2032     struct netdev_linux *netdev = netdev_linux_cast(netdev_);
2033     int error;
2034
2035     ovs_mutex_lock(&netdev->mutex);
2036     error = tc_query_qdisc(netdev_);
2037     if (!error) {
2038         struct tc_queue *queue = tc_find_queue(netdev_, queue_id);
2039         error = (queue
2040                 ? netdev->tc->ops->class_get(netdev_, queue, details)
2041                 : ENOENT);
2042     }
2043     ovs_mutex_unlock(&netdev->mutex);
2044
2045     return error;
2046 }
2047
2048 static int
2049 netdev_linux_set_queue(struct netdev *netdev_,
2050                        unsigned int queue_id, const struct smap *details)
2051 {
2052     struct netdev_linux *netdev = netdev_linux_cast(netdev_);
2053     int error;
2054
2055     ovs_mutex_lock(&netdev->mutex);
2056     error = tc_query_qdisc(netdev_);
2057     if (!error) {
2058         error = (queue_id < netdev->tc->ops->n_queues
2059                  && netdev->tc->ops->class_set
2060                  ? netdev->tc->ops->class_set(netdev_, queue_id, details)
2061                  : EINVAL);
2062     }
2063     ovs_mutex_unlock(&netdev->mutex);
2064
2065     return error;
2066 }
2067
2068 static int
2069 netdev_linux_delete_queue(struct netdev *netdev_, unsigned int queue_id)
2070 {
2071     struct netdev_linux *netdev = netdev_linux_cast(netdev_);
2072     int error;
2073
2074     ovs_mutex_lock(&netdev->mutex);
2075     error = tc_query_qdisc(netdev_);
2076     if (!error) {
2077         if (netdev->tc->ops->class_delete) {
2078             struct tc_queue *queue = tc_find_queue(netdev_, queue_id);
2079             error = (queue
2080                      ? netdev->tc->ops->class_delete(netdev_, queue)
2081                      : ENOENT);
2082         } else {
2083             error = EINVAL;
2084         }
2085     }
2086     ovs_mutex_unlock(&netdev->mutex);
2087
2088     return error;
2089 }
2090
2091 static int
2092 netdev_linux_get_queue_stats(const struct netdev *netdev_,
2093                              unsigned int queue_id,
2094                              struct netdev_queue_stats *stats)
2095 {
2096     struct netdev_linux *netdev = netdev_linux_cast(netdev_);
2097     int error;
2098
2099     ovs_mutex_lock(&netdev->mutex);
2100     error = tc_query_qdisc(netdev_);
2101     if (!error) {
2102         if (netdev->tc->ops->class_get_stats) {
2103             const struct tc_queue *queue = tc_find_queue(netdev_, queue_id);
2104             if (queue) {
2105                 stats->created = queue->created;
2106                 error = netdev->tc->ops->class_get_stats(netdev_, queue,
2107                                                          stats);
2108             } else {
2109                 error = ENOENT;
2110             }
2111         } else {
2112             error = EOPNOTSUPP;
2113         }
2114     }
2115     ovs_mutex_unlock(&netdev->mutex);
2116
2117     return error;
2118 }
2119
2120 static bool
2121 start_queue_dump(const struct netdev *netdev, struct nl_dump *dump)
2122 {
2123     struct ofpbuf request;
2124     struct tcmsg *tcmsg;
2125
2126     tcmsg = tc_make_request(netdev, RTM_GETTCLASS, 0, &request);
2127     if (!tcmsg) {
2128         return false;
2129     }
2130     tcmsg->tcm_parent = 0;
2131     nl_dump_start(dump, NETLINK_ROUTE, &request);
2132     ofpbuf_uninit(&request);
2133     return true;
2134 }
2135
2136 struct netdev_linux_queue_state {
2137     unsigned int *queues;
2138     size_t cur_queue;
2139     size_t n_queues;
2140 };
2141
2142 static int
2143 netdev_linux_queue_dump_start(const struct netdev *netdev_, void **statep)
2144 {
2145     const struct netdev_linux *netdev = netdev_linux_cast(netdev_);
2146     int error;
2147
2148     ovs_mutex_lock(&netdev->mutex);
2149     error = tc_query_qdisc(netdev_);
2150     if (!error) {
2151         if (netdev->tc->ops->class_get) {
2152             struct netdev_linux_queue_state *state;
2153             struct tc_queue *queue;
2154             size_t i;
2155
2156             *statep = state = xmalloc(sizeof *state);
2157             state->n_queues = hmap_count(&netdev->tc->queues);
2158             state->cur_queue = 0;
2159             state->queues = xmalloc(state->n_queues * sizeof *state->queues);
2160
2161             i = 0;
2162             HMAP_FOR_EACH (queue, hmap_node, &netdev->tc->queues) {
2163                 state->queues[i++] = queue->queue_id;
2164             }
2165         } else {
2166             error = EOPNOTSUPP;
2167         }
2168     }
2169     ovs_mutex_unlock(&netdev->mutex);
2170
2171     return error;
2172 }
2173
2174 static int
2175 netdev_linux_queue_dump_next(const struct netdev *netdev_, void *state_,
2176                              unsigned int *queue_idp, struct smap *details)
2177 {
2178     const struct netdev_linux *netdev = netdev_linux_cast(netdev_);
2179     struct netdev_linux_queue_state *state = state_;
2180     int error = EOF;
2181
2182     ovs_mutex_lock(&netdev->mutex);
2183     while (state->cur_queue < state->n_queues) {
2184         unsigned int queue_id = state->queues[state->cur_queue++];
2185         struct tc_queue *queue = tc_find_queue(netdev_, queue_id);
2186
2187         if (queue) {
2188             *queue_idp = queue_id;
2189             error = netdev->tc->ops->class_get(netdev_, queue, details);
2190             break;
2191         }
2192     }
2193     ovs_mutex_unlock(&netdev->mutex);
2194
2195     return error;
2196 }
2197
2198 static int
2199 netdev_linux_queue_dump_done(const struct netdev *netdev OVS_UNUSED,
2200                              void *state_)
2201 {
2202     struct netdev_linux_queue_state *state = state_;
2203
2204     free(state->queues);
2205     free(state);
2206     return 0;
2207 }
2208
2209 static int
2210 netdev_linux_dump_queue_stats(const struct netdev *netdev_,
2211                               netdev_dump_queue_stats_cb *cb, void *aux)
2212 {
2213     struct netdev_linux *netdev = netdev_linux_cast(netdev_);
2214     int error;
2215
2216     ovs_mutex_lock(&netdev->mutex);
2217     error = tc_query_qdisc(netdev_);
2218     if (!error) {
2219         struct nl_dump dump;
2220
2221         if (!netdev->tc->ops->class_dump_stats) {
2222             error = EOPNOTSUPP;
2223         } else if (!start_queue_dump(netdev_, &dump)) {
2224             error = ENODEV;
2225         } else {
2226             struct ofpbuf msg;
2227             int retval;
2228
2229             while (nl_dump_next(&dump, &msg)) {
2230                 retval = netdev->tc->ops->class_dump_stats(netdev_, &msg,
2231                                                            cb, aux);
2232                 if (retval) {
2233                     error = retval;
2234                 }
2235             }
2236
2237             retval = nl_dump_done(&dump);
2238             if (retval) {
2239                 error = retval;
2240             }
2241         }
2242     }
2243     ovs_mutex_unlock(&netdev->mutex);
2244
2245     return error;
2246 }
2247
2248 static int
2249 netdev_linux_get_in4(const struct netdev *netdev_,
2250                      struct in_addr *address, struct in_addr *netmask)
2251 {
2252     struct netdev_linux *netdev = netdev_linux_cast(netdev_);
2253     int error;
2254
2255     ovs_mutex_lock(&netdev->mutex);
2256     if (!(netdev->cache_valid & VALID_IN4)) {
2257         error = netdev_linux_get_ipv4(netdev_, &netdev->address,
2258                                       SIOCGIFADDR, "SIOCGIFADDR");
2259         if (!error) {
2260             error = netdev_linux_get_ipv4(netdev_, &netdev->netmask,
2261                                           SIOCGIFNETMASK, "SIOCGIFNETMASK");
2262             if (!error) {
2263                 netdev->cache_valid |= VALID_IN4;
2264             }
2265         }
2266     } else {
2267         error = 0;
2268     }
2269
2270     if (!error) {
2271         if (netdev->address.s_addr != INADDR_ANY) {
2272             *address = netdev->address;
2273             *netmask = netdev->netmask;
2274         } else {
2275             error = EADDRNOTAVAIL;
2276         }
2277     }
2278     ovs_mutex_unlock(&netdev->mutex);
2279
2280     return error;
2281 }
2282
2283 static int
2284 netdev_linux_set_in4(struct netdev *netdev_, struct in_addr address,
2285                      struct in_addr netmask)
2286 {
2287     struct netdev_linux *netdev = netdev_linux_cast(netdev_);
2288     int error;
2289
2290     ovs_mutex_lock(&netdev->mutex);
2291     error = do_set_addr(netdev_, SIOCSIFADDR, "SIOCSIFADDR", address);
2292     if (!error) {
2293         netdev->cache_valid |= VALID_IN4;
2294         netdev->address = address;
2295         netdev->netmask = netmask;
2296         if (address.s_addr != INADDR_ANY) {
2297             error = do_set_addr(netdev_, SIOCSIFNETMASK,
2298                                 "SIOCSIFNETMASK", netmask);
2299         }
2300     }
2301     ovs_mutex_unlock(&netdev->mutex);
2302
2303     return error;
2304 }
2305
2306 static bool
2307 parse_if_inet6_line(const char *line,
2308                     struct in6_addr *in6, char ifname[16 + 1])
2309 {
2310     uint8_t *s6 = in6->s6_addr;
2311 #define X8 "%2"SCNx8
2312     return ovs_scan(line,
2313                     " "X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8
2314                     "%*x %*x %*x %*x %16s\n",
2315                     &s6[0], &s6[1], &s6[2], &s6[3],
2316                     &s6[4], &s6[5], &s6[6], &s6[7],
2317                     &s6[8], &s6[9], &s6[10], &s6[11],
2318                     &s6[12], &s6[13], &s6[14], &s6[15],
2319                     ifname);
2320 }
2321
2322 /* If 'netdev' has an assigned IPv6 address, sets '*in6' to that address (if
2323  * 'in6' is non-null) and returns true.  Otherwise, returns false. */
2324 static int
2325 netdev_linux_get_in6(const struct netdev *netdev_, struct in6_addr *in6)
2326 {
2327     struct netdev_linux *netdev = netdev_linux_cast(netdev_);
2328
2329     ovs_mutex_lock(&netdev->mutex);
2330     if (!(netdev->cache_valid & VALID_IN6)) {
2331         FILE *file;
2332         char line[128];
2333
2334         netdev->in6 = in6addr_any;
2335
2336         file = fopen("/proc/net/if_inet6", "r");
2337         if (file != NULL) {
2338             const char *name = netdev_get_name(netdev_);
2339             while (fgets(line, sizeof line, file)) {
2340                 struct in6_addr in6_tmp;
2341                 char ifname[16 + 1];
2342                 if (parse_if_inet6_line(line, &in6_tmp, ifname)
2343                     && !strcmp(name, ifname))
2344                 {
2345                     netdev->in6 = in6_tmp;
2346                     break;
2347                 }
2348             }
2349             fclose(file);
2350         }
2351         netdev->cache_valid |= VALID_IN6;
2352     }
2353     *in6 = netdev->in6;
2354     ovs_mutex_unlock(&netdev->mutex);
2355
2356     return 0;
2357 }
2358
2359 static void
2360 make_in4_sockaddr(struct sockaddr *sa, struct in_addr addr)
2361 {
2362     struct sockaddr_in sin;
2363     memset(&sin, 0, sizeof sin);
2364     sin.sin_family = AF_INET;
2365     sin.sin_addr = addr;
2366     sin.sin_port = 0;
2367
2368     memset(sa, 0, sizeof *sa);
2369     memcpy(sa, &sin, sizeof sin);
2370 }
2371
2372 static int
2373 do_set_addr(struct netdev *netdev,
2374             int ioctl_nr, const char *ioctl_name, struct in_addr addr)
2375 {
2376     struct ifreq ifr;
2377
2378     make_in4_sockaddr(&ifr.ifr_addr, addr);
2379     return af_inet_ifreq_ioctl(netdev_get_name(netdev), &ifr, ioctl_nr,
2380                                ioctl_name);
2381 }
2382
2383 /* Adds 'router' as a default IP gateway. */
2384 static int
2385 netdev_linux_add_router(struct netdev *netdev OVS_UNUSED, struct in_addr router)
2386 {
2387     struct in_addr any = { INADDR_ANY };
2388     struct rtentry rt;
2389     int error;
2390
2391     memset(&rt, 0, sizeof rt);
2392     make_in4_sockaddr(&rt.rt_dst, any);
2393     make_in4_sockaddr(&rt.rt_gateway, router);
2394     make_in4_sockaddr(&rt.rt_genmask, any);
2395     rt.rt_flags = RTF_UP | RTF_GATEWAY;
2396     error = af_inet_ioctl(SIOCADDRT, &rt);
2397     if (error) {
2398         VLOG_WARN("ioctl(SIOCADDRT): %s", ovs_strerror(error));
2399     }
2400     return error;
2401 }
2402
2403 static int
2404 netdev_linux_get_next_hop(const struct in_addr *host, struct in_addr *next_hop,
2405                           char **netdev_name)
2406 {
2407     static const char fn[] = "/proc/net/route";
2408     FILE *stream;
2409     char line[256];
2410     int ln;
2411
2412     *netdev_name = NULL;
2413     stream = fopen(fn, "r");
2414     if (stream == NULL) {
2415         VLOG_WARN_RL(&rl, "%s: open failed: %s", fn, ovs_strerror(errno));
2416         return errno;
2417     }
2418
2419     ln = 0;
2420     while (fgets(line, sizeof line, stream)) {
2421         if (++ln >= 2) {
2422             char iface[17];
2423             ovs_be32 dest, gateway, mask;
2424             int refcnt, metric, mtu;
2425             unsigned int flags, use, window, irtt;
2426
2427             if (!ovs_scan(line,
2428                           "%16s %"SCNx32" %"SCNx32" %04X %d %u %d %"SCNx32
2429                           " %d %u %u\n",
2430                           iface, &dest, &gateway, &flags, &refcnt,
2431                           &use, &metric, &mask, &mtu, &window, &irtt)) {
2432                 VLOG_WARN_RL(&rl, "%s: could not parse line %d: %s",
2433                         fn, ln, line);
2434                 continue;
2435             }
2436             if (!(flags & RTF_UP)) {
2437                 /* Skip routes that aren't up. */
2438                 continue;
2439             }
2440
2441             /* The output of 'dest', 'mask', and 'gateway' were given in
2442              * network byte order, so we don't need need any endian
2443              * conversions here. */
2444             if ((dest & mask) == (host->s_addr & mask)) {
2445                 if (!gateway) {
2446                     /* The host is directly reachable. */
2447                     next_hop->s_addr = 0;
2448                 } else {
2449                     /* To reach the host, we must go through a gateway. */
2450                     next_hop->s_addr = gateway;
2451                 }
2452                 *netdev_name = xstrdup(iface);
2453                 fclose(stream);
2454                 return 0;
2455             }
2456         }
2457     }
2458
2459     fclose(stream);
2460     return ENXIO;
2461 }
2462
2463 static int
2464 netdev_linux_get_status(const struct netdev *netdev_, struct smap *smap)
2465 {
2466     struct netdev_linux *netdev = netdev_linux_cast(netdev_);
2467     int error = 0;
2468
2469     ovs_mutex_lock(&netdev->mutex);
2470     if (!(netdev->cache_valid & VALID_DRVINFO)) {
2471         struct ethtool_cmd *cmd = (struct ethtool_cmd *) &netdev->drvinfo;
2472
2473         COVERAGE_INC(netdev_get_ethtool);
2474         memset(&netdev->drvinfo, 0, sizeof netdev->drvinfo);
2475         error = netdev_linux_do_ethtool(netdev->up.name,
2476                                         cmd,
2477                                         ETHTOOL_GDRVINFO,
2478                                         "ETHTOOL_GDRVINFO");
2479         if (!error) {
2480             netdev->cache_valid |= VALID_DRVINFO;
2481         }
2482     }
2483
2484     if (!error) {
2485         smap_add(smap, "driver_name", netdev->drvinfo.driver);
2486         smap_add(smap, "driver_version", netdev->drvinfo.version);
2487         smap_add(smap, "firmware_version", netdev->drvinfo.fw_version);
2488     }
2489     ovs_mutex_unlock(&netdev->mutex);
2490
2491     return error;
2492 }
2493
2494 static int
2495 netdev_internal_get_status(const struct netdev *netdev OVS_UNUSED,
2496                            struct smap *smap)
2497 {
2498     smap_add(smap, "driver_name", "openvswitch");
2499     return 0;
2500 }
2501
2502 /* Looks up the ARP table entry for 'ip' on 'netdev'.  If one exists and can be
2503  * successfully retrieved, it stores the corresponding MAC address in 'mac' and
2504  * returns 0.  Otherwise, it returns a positive errno value; in particular,
2505  * ENXIO indicates that there is not ARP table entry for 'ip' on 'netdev'. */
2506 static int
2507 netdev_linux_arp_lookup(const struct netdev *netdev,
2508                         ovs_be32 ip, uint8_t mac[ETH_ADDR_LEN])
2509 {
2510     struct arpreq r;
2511     struct sockaddr_in sin;
2512     int retval;
2513
2514     memset(&r, 0, sizeof r);
2515     memset(&sin, 0, sizeof sin);
2516     sin.sin_family = AF_INET;
2517     sin.sin_addr.s_addr = ip;
2518     sin.sin_port = 0;
2519     memcpy(&r.arp_pa, &sin, sizeof sin);
2520     r.arp_ha.sa_family = ARPHRD_ETHER;
2521     r.arp_flags = 0;
2522     ovs_strzcpy(r.arp_dev, netdev_get_name(netdev), sizeof r.arp_dev);
2523     COVERAGE_INC(netdev_arp_lookup);
2524     retval = af_inet_ioctl(SIOCGARP, &r);
2525     if (!retval) {
2526         memcpy(mac, r.arp_ha.sa_data, ETH_ADDR_LEN);
2527     } else if (retval != ENXIO) {
2528         VLOG_WARN_RL(&rl, "%s: could not look up ARP entry for "IP_FMT": %s",
2529                      netdev_get_name(netdev), IP_ARGS(ip),
2530                      ovs_strerror(retval));
2531     }
2532     return retval;
2533 }
2534
2535 static int
2536 nd_to_iff_flags(enum netdev_flags nd)
2537 {
2538     int iff = 0;
2539     if (nd & NETDEV_UP) {
2540         iff |= IFF_UP;
2541     }
2542     if (nd & NETDEV_PROMISC) {
2543         iff |= IFF_PROMISC;
2544     }
2545     if (nd & NETDEV_LOOPBACK) {
2546         iff |= IFF_LOOPBACK;
2547     }
2548     return iff;
2549 }
2550
2551 static int
2552 iff_to_nd_flags(int iff)
2553 {
2554     enum netdev_flags nd = 0;
2555     if (iff & IFF_UP) {
2556         nd |= NETDEV_UP;
2557     }
2558     if (iff & IFF_PROMISC) {
2559         nd |= NETDEV_PROMISC;
2560     }
2561     if (iff & IFF_LOOPBACK) {
2562         nd |= NETDEV_LOOPBACK;
2563     }
2564     return nd;
2565 }
2566
2567 static int
2568 update_flags(struct netdev_linux *netdev, enum netdev_flags off,
2569              enum netdev_flags on, enum netdev_flags *old_flagsp)
2570     OVS_REQUIRES(netdev->mutex)
2571 {
2572     int old_flags, new_flags;
2573     int error = 0;
2574
2575     old_flags = netdev->ifi_flags;
2576     *old_flagsp = iff_to_nd_flags(old_flags);
2577     new_flags = (old_flags & ~nd_to_iff_flags(off)) | nd_to_iff_flags(on);
2578     if (new_flags != old_flags) {
2579         error = set_flags(netdev_get_name(&netdev->up), new_flags);
2580         get_flags(&netdev->up, &netdev->ifi_flags);
2581     }
2582
2583     return error;
2584 }
2585
2586 static int
2587 netdev_linux_update_flags(struct netdev *netdev_, enum netdev_flags off,
2588                           enum netdev_flags on, enum netdev_flags *old_flagsp)
2589 {
2590     struct netdev_linux *netdev = netdev_linux_cast(netdev_);
2591     int error;
2592
2593     ovs_mutex_lock(&netdev->mutex);
2594     error = update_flags(netdev, off, on, old_flagsp);
2595     ovs_mutex_unlock(&netdev->mutex);
2596
2597     return error;
2598 }
2599
2600 static unsigned int
2601 netdev_linux_change_seq(const struct netdev *netdev_)
2602 {
2603     struct netdev_linux *netdev = netdev_linux_cast(netdev_);
2604     unsigned int change_seq;
2605
2606     ovs_mutex_lock(&netdev->mutex);
2607     change_seq = netdev->change_seq;
2608     ovs_mutex_unlock(&netdev->mutex);
2609
2610     return change_seq;
2611 }
2612
2613 #define NETDEV_LINUX_CLASS(NAME, CONSTRUCT, GET_STATS, SET_STATS,  \
2614                            GET_FEATURES, GET_STATUS)            \
2615 {                                                               \
2616     NAME,                                                       \
2617                                                                 \
2618     NULL,                                                       \
2619     netdev_linux_run,                                           \
2620     netdev_linux_wait,                                          \
2621                                                                 \
2622     netdev_linux_alloc,                                         \
2623     CONSTRUCT,                                                  \
2624     netdev_linux_destruct,                                      \
2625     netdev_linux_dealloc,                                       \
2626     NULL,                       /* get_config */                \
2627     NULL,                       /* set_config */                \
2628     NULL,                       /* get_tunnel_config */         \
2629                                                                 \
2630     netdev_linux_send,                                          \
2631     netdev_linux_send_wait,                                     \
2632                                                                 \
2633     netdev_linux_set_etheraddr,                                 \
2634     netdev_linux_get_etheraddr,                                 \
2635     netdev_linux_get_mtu,                                       \
2636     netdev_linux_set_mtu,                                       \
2637     netdev_linux_get_ifindex,                                   \
2638     netdev_linux_get_carrier,                                   \
2639     netdev_linux_get_carrier_resets,                            \
2640     netdev_linux_set_miimon_interval,                           \
2641     GET_STATS,                                                  \
2642     SET_STATS,                                                  \
2643                                                                 \
2644     GET_FEATURES,                                               \
2645     netdev_linux_set_advertisements,                            \
2646                                                                 \
2647     netdev_linux_set_policing,                                  \
2648     netdev_linux_get_qos_types,                                 \
2649     netdev_linux_get_qos_capabilities,                          \
2650     netdev_linux_get_qos,                                       \
2651     netdev_linux_set_qos,                                       \
2652     netdev_linux_get_queue,                                     \
2653     netdev_linux_set_queue,                                     \
2654     netdev_linux_delete_queue,                                  \
2655     netdev_linux_get_queue_stats,                               \
2656     netdev_linux_queue_dump_start,                              \
2657     netdev_linux_queue_dump_next,                               \
2658     netdev_linux_queue_dump_done,                               \
2659     netdev_linux_dump_queue_stats,                              \
2660                                                                 \
2661     netdev_linux_get_in4,                                       \
2662     netdev_linux_set_in4,                                       \
2663     netdev_linux_get_in6,                                       \
2664     netdev_linux_add_router,                                    \
2665     netdev_linux_get_next_hop,                                  \
2666     GET_STATUS,                                                 \
2667     netdev_linux_arp_lookup,                                    \
2668                                                                 \
2669     netdev_linux_update_flags,                                  \
2670                                                                 \
2671     netdev_linux_change_seq,                                    \
2672                                                                 \
2673     netdev_linux_rx_alloc,                                      \
2674     netdev_linux_rx_construct,                                  \
2675     netdev_linux_rx_destruct,                                   \
2676     netdev_linux_rx_dealloc,                                    \
2677     netdev_linux_rx_recv,                                       \
2678     netdev_linux_rx_wait,                                       \
2679     netdev_linux_rx_drain,                                      \
2680 }
2681
2682 const struct netdev_class netdev_linux_class =
2683     NETDEV_LINUX_CLASS(
2684         "system",
2685         netdev_linux_construct,
2686         netdev_linux_get_stats,
2687         NULL,                    /* set_stats */
2688         netdev_linux_get_features,
2689         netdev_linux_get_status);
2690
2691 const struct netdev_class netdev_tap_class =
2692     NETDEV_LINUX_CLASS(
2693         "tap",
2694         netdev_linux_construct_tap,
2695         netdev_tap_get_stats,
2696         NULL,                   /* set_stats */
2697         netdev_linux_get_features,
2698         netdev_linux_get_status);
2699
2700 const struct netdev_class netdev_internal_class =
2701     NETDEV_LINUX_CLASS(
2702         "internal",
2703         netdev_linux_construct,
2704         netdev_internal_get_stats,
2705         netdev_internal_set_stats,
2706         NULL,                  /* get_features */
2707         netdev_internal_get_status);
2708 \f
2709 /* HTB traffic control class. */
2710
2711 #define HTB_N_QUEUES 0xf000
2712
2713 struct htb {
2714     struct tc tc;
2715     unsigned int max_rate;      /* In bytes/s. */
2716 };
2717
2718 struct htb_class {
2719     struct tc_queue tc_queue;
2720     unsigned int min_rate;      /* In bytes/s. */
2721     unsigned int max_rate;      /* In bytes/s. */
2722     unsigned int burst;         /* In bytes. */
2723     unsigned int priority;      /* Lower values are higher priorities. */
2724 };
2725
2726 static struct htb *
2727 htb_get__(const struct netdev *netdev_)
2728 {
2729     struct netdev_linux *netdev = netdev_linux_cast(netdev_);
2730     return CONTAINER_OF(netdev->tc, struct htb, tc);
2731 }
2732
2733 static void
2734 htb_install__(struct netdev *netdev_, uint64_t max_rate)
2735 {
2736     struct netdev_linux *netdev = netdev_linux_cast(netdev_);
2737     struct htb *htb;
2738
2739     htb = xmalloc(sizeof *htb);
2740     tc_init(&htb->tc, &tc_ops_htb);
2741     htb->max_rate = max_rate;
2742
2743     netdev->tc = &htb->tc;
2744 }
2745
2746 /* Create an HTB qdisc.
2747  *
2748  * Equivalent to "tc qdisc add dev <dev> root handle 1: htb default 1". */
2749 static int
2750 htb_setup_qdisc__(struct netdev *netdev)
2751 {
2752     size_t opt_offset;
2753     struct tc_htb_glob opt;
2754     struct ofpbuf request;
2755     struct tcmsg *tcmsg;
2756
2757     tc_del_qdisc(netdev);
2758
2759     tcmsg = tc_make_request(netdev, RTM_NEWQDISC,
2760                             NLM_F_EXCL | NLM_F_CREATE, &request);
2761     if (!tcmsg) {
2762         return ENODEV;
2763     }
2764     tcmsg->tcm_handle = tc_make_handle(1, 0);
2765     tcmsg->tcm_parent = TC_H_ROOT;
2766
2767     nl_msg_put_string(&request, TCA_KIND, "htb");
2768
2769     memset(&opt, 0, sizeof opt);
2770     opt.rate2quantum = 10;
2771     opt.version = 3;
2772     opt.defcls = 1;
2773
2774     opt_offset = nl_msg_start_nested(&request, TCA_OPTIONS);
2775     nl_msg_put_unspec(&request, TCA_HTB_INIT, &opt, sizeof opt);
2776     nl_msg_end_nested(&request, opt_offset);
2777
2778     return tc_transact(&request, NULL);
2779 }
2780
2781 /* Equivalent to "tc class replace <dev> classid <handle> parent <parent> htb
2782  * rate <min_rate>bps ceil <max_rate>bps burst <burst>b prio <priority>". */
2783 static int
2784 htb_setup_class__(struct netdev *netdev, unsigned int handle,
2785                   unsigned int parent, struct htb_class *class)
2786 {
2787     size_t opt_offset;
2788     struct tc_htb_opt opt;
2789     struct ofpbuf request;
2790     struct tcmsg *tcmsg;
2791     int error;
2792     int mtu;
2793
2794     error = netdev_linux_get_mtu__(netdev_linux_cast(netdev), &mtu);
2795     if (error) {
2796         VLOG_WARN_RL(&rl, "cannot set up HTB on device %s that lacks MTU",
2797                      netdev_get_name(netdev));
2798         return error;
2799     }
2800
2801     memset(&opt, 0, sizeof opt);
2802     tc_fill_rate(&opt.rate, class->min_rate, mtu);
2803     tc_fill_rate(&opt.ceil, class->max_rate, mtu);
2804     opt.buffer = tc_calc_buffer(opt.rate.rate, mtu, class->burst);
2805     opt.cbuffer = tc_calc_buffer(opt.ceil.rate, mtu, class->burst);
2806     opt.prio = class->priority;
2807
2808     tcmsg = tc_make_request(netdev, RTM_NEWTCLASS, NLM_F_CREATE, &request);
2809     if (!tcmsg) {
2810         return ENODEV;
2811     }
2812     tcmsg->tcm_handle = handle;
2813     tcmsg->tcm_parent = parent;
2814
2815     nl_msg_put_string(&request, TCA_KIND, "htb");
2816     opt_offset = nl_msg_start_nested(&request, TCA_OPTIONS);
2817     nl_msg_put_unspec(&request, TCA_HTB_PARMS, &opt, sizeof opt);
2818     tc_put_rtab(&request, TCA_HTB_RTAB, &opt.rate);
2819     tc_put_rtab(&request, TCA_HTB_CTAB, &opt.ceil);
2820     nl_msg_end_nested(&request, opt_offset);
2821
2822     error = tc_transact(&request, NULL);
2823     if (error) {
2824         VLOG_WARN_RL(&rl, "failed to replace %s class %u:%u, parent %u:%u, "
2825                      "min_rate=%u max_rate=%u burst=%u prio=%u (%s)",
2826                      netdev_get_name(netdev),
2827                      tc_get_major(handle), tc_get_minor(handle),
2828                      tc_get_major(parent), tc_get_minor(parent),
2829                      class->min_rate, class->max_rate,
2830                      class->burst, class->priority, ovs_strerror(error));
2831     }
2832     return error;
2833 }
2834
2835 /* Parses Netlink attributes in 'options' for HTB parameters and stores a
2836  * description of them into 'details'.  The description complies with the
2837  * specification given in the vswitch database documentation for linux-htb
2838  * queue details. */
2839 static int
2840 htb_parse_tca_options__(struct nlattr *nl_options, struct htb_class *class)
2841 {
2842     static const struct nl_policy tca_htb_policy[] = {
2843         [TCA_HTB_PARMS] = { .type = NL_A_UNSPEC, .optional = false,
2844                             .min_len = sizeof(struct tc_htb_opt) },
2845     };
2846
2847     struct nlattr *attrs[ARRAY_SIZE(tca_htb_policy)];
2848     const struct tc_htb_opt *htb;
2849
2850     if (!nl_parse_nested(nl_options, tca_htb_policy,
2851                          attrs, ARRAY_SIZE(tca_htb_policy))) {
2852         VLOG_WARN_RL(&rl, "failed to parse HTB class options");
2853         return EPROTO;
2854     }
2855
2856     htb = nl_attr_get(attrs[TCA_HTB_PARMS]);
2857     class->min_rate = htb->rate.rate;
2858     class->max_rate = htb->ceil.rate;
2859     class->burst = tc_ticks_to_bytes(htb->rate.rate, htb->buffer);
2860     class->priority = htb->prio;
2861     return 0;
2862 }
2863
2864 static int
2865 htb_parse_tcmsg__(struct ofpbuf *tcmsg, unsigned int *queue_id,
2866                   struct htb_class *options,
2867                   struct netdev_queue_stats *stats)
2868 {
2869     struct nlattr *nl_options;
2870     unsigned int handle;
2871     int error;
2872
2873     error = tc_parse_class(tcmsg, &handle, &nl_options, stats);
2874     if (!error && queue_id) {
2875         unsigned int major = tc_get_major(handle);
2876         unsigned int minor = tc_get_minor(handle);
2877         if (major == 1 && minor > 0 && minor <= HTB_N_QUEUES) {
2878             *queue_id = minor - 1;
2879         } else {
2880             error = EPROTO;
2881         }
2882     }
2883     if (!error && options) {
2884         error = htb_parse_tca_options__(nl_options, options);
2885     }
2886     return error;
2887 }
2888
2889 static void
2890 htb_parse_qdisc_details__(struct netdev *netdev_,
2891                           const struct smap *details, struct htb_class *hc)
2892 {
2893     struct netdev_linux *netdev = netdev_linux_cast(netdev_);
2894     const char *max_rate_s;
2895
2896     max_rate_s = smap_get(details, "max-rate");
2897     hc->max_rate = max_rate_s ? strtoull(max_rate_s, NULL, 10) / 8 : 0;
2898     if (!hc->max_rate) {
2899         enum netdev_features current;
2900
2901         netdev_linux_read_features(netdev);
2902         current = !netdev->get_features_error ? netdev->current : 0;
2903         hc->max_rate = netdev_features_to_bps(current, 100 * 1000 * 1000) / 8;
2904     }
2905     hc->min_rate = hc->max_rate;
2906     hc->burst = 0;
2907     hc->priority = 0;
2908 }
2909
2910 static int
2911 htb_parse_class_details__(struct netdev *netdev,
2912                           const struct smap *details, struct htb_class *hc)
2913 {
2914     const struct htb *htb = htb_get__(netdev);
2915     const char *min_rate_s = smap_get(details, "min-rate");
2916     const char *max_rate_s = smap_get(details, "max-rate");
2917     const char *burst_s = smap_get(details, "burst");
2918     const char *priority_s = smap_get(details, "priority");
2919     int mtu, error;
2920
2921     error = netdev_linux_get_mtu__(netdev_linux_cast(netdev), &mtu);
2922     if (error) {
2923         VLOG_WARN_RL(&rl, "cannot parse HTB class on device %s that lacks MTU",
2924                      netdev_get_name(netdev));
2925         return error;
2926     }
2927
2928     /* HTB requires at least an mtu sized min-rate to send any traffic even
2929      * on uncongested links. */
2930     hc->min_rate = min_rate_s ? strtoull(min_rate_s, NULL, 10) / 8 : 0;
2931     hc->min_rate = MAX(hc->min_rate, mtu);
2932     hc->min_rate = MIN(hc->min_rate, htb->max_rate);
2933
2934     /* max-rate */
2935     hc->max_rate = (max_rate_s
2936                     ? strtoull(max_rate_s, NULL, 10) / 8
2937                     : htb->max_rate);
2938     hc->max_rate = MAX(hc->max_rate, hc->min_rate);
2939     hc->max_rate = MIN(hc->max_rate, htb->max_rate);
2940
2941     /* burst
2942      *
2943      * According to hints in the documentation that I've read, it is important
2944      * that 'burst' be at least as big as the largest frame that might be
2945      * transmitted.  Also, making 'burst' a bit bigger than necessary is OK,
2946      * but having it a bit too small is a problem.  Since netdev_get_mtu()
2947      * doesn't include the Ethernet header, we need to add at least 14 (18?) to
2948      * the MTU.  We actually add 64, instead of 14, as a guard against
2949      * additional headers get tacked on somewhere that we're not aware of. */
2950     hc->burst = burst_s ? strtoull(burst_s, NULL, 10) / 8 : 0;
2951     hc->burst = MAX(hc->burst, mtu + 64);
2952
2953     /* priority */
2954     hc->priority = priority_s ? strtoul(priority_s, NULL, 10) : 0;
2955
2956     return 0;
2957 }
2958
2959 static int
2960 htb_query_class__(const struct netdev *netdev, unsigned int handle,
2961                   unsigned int parent, struct htb_class *options,
2962                   struct netdev_queue_stats *stats)
2963 {
2964     struct ofpbuf *reply;
2965     int error;
2966
2967     error = tc_query_class(netdev, handle, parent, &reply);
2968     if (!error) {
2969         error = htb_parse_tcmsg__(reply, NULL, options, stats);
2970         ofpbuf_delete(reply);
2971     }
2972     return error;
2973 }
2974
2975 static int
2976 htb_tc_install(struct netdev *netdev, const struct smap *details)
2977 {
2978     int error;
2979
2980     error = htb_setup_qdisc__(netdev);
2981     if (!error) {
2982         struct htb_class hc;
2983
2984         htb_parse_qdisc_details__(netdev, details, &hc);
2985         error = htb_setup_class__(netdev, tc_make_handle(1, 0xfffe),
2986                                   tc_make_handle(1, 0), &hc);
2987         if (!error) {
2988             htb_install__(netdev, hc.max_rate);
2989         }
2990     }
2991     return error;
2992 }
2993
2994 static struct htb_class *
2995 htb_class_cast__(const struct tc_queue *queue)
2996 {
2997     return CONTAINER_OF(queue, struct htb_class, tc_queue);
2998 }
2999
3000 static void
3001 htb_update_queue__(struct netdev *netdev, unsigned int queue_id,
3002                    const struct htb_class *hc)
3003 {
3004     struct htb *htb = htb_get__(netdev);
3005     size_t hash = hash_int(queue_id, 0);
3006     struct tc_queue *queue;
3007     struct htb_class *hcp;
3008
3009     queue = tc_find_queue__(netdev, queue_id, hash);
3010     if (queue) {
3011         hcp = htb_class_cast__(queue);
3012     } else {
3013         hcp = xmalloc(sizeof *hcp);
3014         queue = &hcp->tc_queue;
3015         queue->queue_id = queue_id;
3016         queue->created = time_msec();
3017         hmap_insert(&htb->tc.queues, &queue->hmap_node, hash);
3018     }
3019
3020     hcp->min_rate = hc->min_rate;
3021     hcp->max_rate = hc->max_rate;
3022     hcp->burst = hc->burst;
3023     hcp->priority = hc->priority;
3024 }
3025
3026 static int
3027 htb_tc_load(struct netdev *netdev, struct ofpbuf *nlmsg OVS_UNUSED)
3028 {
3029     struct ofpbuf msg;
3030     struct nl_dump dump;
3031     struct htb_class hc;
3032
3033     /* Get qdisc options. */
3034     hc.max_rate = 0;
3035     htb_query_class__(netdev, tc_make_handle(1, 0xfffe), 0, &hc, NULL);
3036     htb_install__(netdev, hc.max_rate);
3037
3038     /* Get queues. */
3039     if (!start_queue_dump(netdev, &dump)) {
3040         return ENODEV;
3041     }
3042     while (nl_dump_next(&dump, &msg)) {
3043         unsigned int queue_id;
3044
3045         if (!htb_parse_tcmsg__(&msg, &queue_id, &hc, NULL)) {
3046             htb_update_queue__(netdev, queue_id, &hc);
3047         }
3048     }
3049     nl_dump_done(&dump);
3050
3051     return 0;
3052 }
3053
3054 static void
3055 htb_tc_destroy(struct tc *tc)
3056 {
3057     struct htb *htb = CONTAINER_OF(tc, struct htb, tc);
3058     struct htb_class *hc, *next;
3059
3060     HMAP_FOR_EACH_SAFE (hc, next, tc_queue.hmap_node, &htb->tc.queues) {
3061         hmap_remove(&htb->tc.queues, &hc->tc_queue.hmap_node);
3062         free(hc);
3063     }
3064     tc_destroy(tc);
3065     free(htb);
3066 }
3067
3068 static int
3069 htb_qdisc_get(const struct netdev *netdev, struct smap *details)
3070 {
3071     const struct htb *htb = htb_get__(netdev);
3072     smap_add_format(details, "max-rate", "%llu", 8ULL * htb->max_rate);
3073     return 0;
3074 }
3075
3076 static int
3077 htb_qdisc_set(struct netdev *netdev, const struct smap *details)
3078 {
3079     struct htb_class hc;
3080     int error;
3081
3082     htb_parse_qdisc_details__(netdev, details, &hc);
3083     error = htb_setup_class__(netdev, tc_make_handle(1, 0xfffe),
3084                               tc_make_handle(1, 0), &hc);
3085     if (!error) {
3086         htb_get__(netdev)->max_rate = hc.max_rate;
3087     }
3088     return error;
3089 }
3090
3091 static int
3092 htb_class_get(const struct netdev *netdev OVS_UNUSED,
3093               const struct tc_queue *queue, struct smap *details)
3094 {
3095     const struct htb_class *hc = htb_class_cast__(queue);
3096
3097     smap_add_format(details, "min-rate", "%llu", 8ULL * hc->min_rate);
3098     if (hc->min_rate != hc->max_rate) {
3099         smap_add_format(details, "max-rate", "%llu", 8ULL * hc->max_rate);
3100     }
3101     smap_add_format(details, "burst", "%llu", 8ULL * hc->burst);
3102     if (hc->priority) {
3103         smap_add_format(details, "priority", "%u", hc->priority);
3104     }
3105     return 0;
3106 }
3107
3108 static int
3109 htb_class_set(struct netdev *netdev, unsigned int queue_id,
3110               const struct smap *details)
3111 {
3112     struct htb_class hc;
3113     int error;
3114
3115     error = htb_parse_class_details__(netdev, details, &hc);
3116     if (error) {
3117         return error;
3118     }
3119
3120     error = htb_setup_class__(netdev, tc_make_handle(1, queue_id + 1),
3121                               tc_make_handle(1, 0xfffe), &hc);
3122     if (error) {
3123         return error;
3124     }
3125
3126     htb_update_queue__(netdev, queue_id, &hc);
3127     return 0;
3128 }
3129
3130 static int
3131 htb_class_delete(struct netdev *netdev, struct tc_queue *queue)
3132 {
3133     struct htb_class *hc = htb_class_cast__(queue);
3134     struct htb *htb = htb_get__(netdev);
3135     int error;
3136
3137     error = tc_delete_class(netdev, tc_make_handle(1, queue->queue_id + 1));
3138     if (!error) {
3139         hmap_remove(&htb->tc.queues, &hc->tc_queue.hmap_node);
3140         free(hc);
3141     }
3142     return error;
3143 }
3144
3145 static int
3146 htb_class_get_stats(const struct netdev *netdev, const struct tc_queue *queue,
3147                     struct netdev_queue_stats *stats)
3148 {
3149     return htb_query_class__(netdev, tc_make_handle(1, queue->queue_id + 1),
3150                              tc_make_handle(1, 0xfffe), NULL, stats);
3151 }
3152
3153 static int
3154 htb_class_dump_stats(const struct netdev *netdev OVS_UNUSED,
3155                      const struct ofpbuf *nlmsg,
3156                      netdev_dump_queue_stats_cb *cb, void *aux)
3157 {
3158     struct netdev_queue_stats stats;
3159     unsigned int handle, major, minor;
3160     int error;
3161
3162     error = tc_parse_class(nlmsg, &handle, NULL, &stats);
3163     if (error) {
3164         return error;
3165     }
3166
3167     major = tc_get_major(handle);
3168     minor = tc_get_minor(handle);
3169     if (major == 1 && minor > 0 && minor <= HTB_N_QUEUES) {
3170         (*cb)(minor - 1, &stats, aux);
3171     }
3172     return 0;
3173 }
3174
3175 static const struct tc_ops tc_ops_htb = {
3176     "htb",                      /* linux_name */
3177     "linux-htb",                /* ovs_name */
3178     HTB_N_QUEUES,               /* n_queues */
3179     htb_tc_install,
3180     htb_tc_load,
3181     htb_tc_destroy,
3182     htb_qdisc_get,
3183     htb_qdisc_set,
3184     htb_class_get,
3185     htb_class_set,
3186     htb_class_delete,
3187     htb_class_get_stats,
3188     htb_class_dump_stats
3189 };
3190 \f
3191 /* "linux-hfsc" traffic control class. */
3192
3193 #define HFSC_N_QUEUES 0xf000
3194
3195 struct hfsc {
3196     struct tc tc;
3197     uint32_t max_rate;
3198 };
3199
3200 struct hfsc_class {
3201     struct tc_queue tc_queue;
3202     uint32_t min_rate;
3203     uint32_t max_rate;
3204 };
3205
3206 static struct hfsc *
3207 hfsc_get__(const struct netdev *netdev_)
3208 {
3209     struct netdev_linux *netdev = netdev_linux_cast(netdev_);
3210     return CONTAINER_OF(netdev->tc, struct hfsc, tc);
3211 }
3212
3213 static struct hfsc_class *
3214 hfsc_class_cast__(const struct tc_queue *queue)
3215 {
3216     return CONTAINER_OF(queue, struct hfsc_class, tc_queue);
3217 }
3218
3219 static void
3220 hfsc_install__(struct netdev *netdev_, uint32_t max_rate)
3221 {
3222     struct netdev_linux *netdev = netdev_linux_cast(netdev_);
3223     struct hfsc *hfsc;
3224
3225     hfsc = xmalloc(sizeof *hfsc);
3226     tc_init(&hfsc->tc, &tc_ops_hfsc);
3227     hfsc->max_rate = max_rate;
3228     netdev->tc = &hfsc->tc;
3229 }
3230
3231 static void
3232 hfsc_update_queue__(struct netdev *netdev, unsigned int queue_id,
3233                     const struct hfsc_class *hc)
3234 {
3235     size_t hash;
3236     struct hfsc *hfsc;
3237     struct hfsc_class *hcp;
3238     struct tc_queue *queue;
3239
3240     hfsc = hfsc_get__(netdev);
3241     hash = hash_int(queue_id, 0);
3242
3243     queue = tc_find_queue__(netdev, queue_id, hash);
3244     if (queue) {
3245         hcp = hfsc_class_cast__(queue);
3246     } else {
3247         hcp             = xmalloc(sizeof *hcp);
3248         queue           = &hcp->tc_queue;
3249         queue->queue_id = queue_id;
3250         queue->created  = time_msec();
3251         hmap_insert(&hfsc->tc.queues, &queue->hmap_node, hash);
3252     }
3253
3254     hcp->min_rate = hc->min_rate;
3255     hcp->max_rate = hc->max_rate;
3256 }
3257
3258 static int
3259 hfsc_parse_tca_options__(struct nlattr *nl_options, struct hfsc_class *class)
3260 {
3261     const struct tc_service_curve *rsc, *fsc, *usc;
3262     static const struct nl_policy tca_hfsc_policy[] = {
3263         [TCA_HFSC_RSC] = {
3264             .type      = NL_A_UNSPEC,
3265             .optional  = false,
3266             .min_len   = sizeof(struct tc_service_curve),
3267         },
3268         [TCA_HFSC_FSC] = {
3269             .type      = NL_A_UNSPEC,
3270             .optional  = false,
3271             .min_len   = sizeof(struct tc_service_curve),
3272         },
3273         [TCA_HFSC_USC] = {
3274             .type      = NL_A_UNSPEC,
3275             .optional  = false,
3276             .min_len   = sizeof(struct tc_service_curve),
3277         },
3278     };
3279     struct nlattr *attrs[ARRAY_SIZE(tca_hfsc_policy)];
3280
3281     if (!nl_parse_nested(nl_options, tca_hfsc_policy,
3282                          attrs, ARRAY_SIZE(tca_hfsc_policy))) {
3283         VLOG_WARN_RL(&rl, "failed to parse HFSC class options");
3284         return EPROTO;
3285     }
3286
3287     rsc = nl_attr_get(attrs[TCA_HFSC_RSC]);
3288     fsc = nl_attr_get(attrs[TCA_HFSC_FSC]);
3289     usc = nl_attr_get(attrs[TCA_HFSC_USC]);
3290
3291     if (rsc->m1 != 0 || rsc->d != 0 ||
3292         fsc->m1 != 0 || fsc->d != 0 ||
3293         usc->m1 != 0 || usc->d != 0) {
3294         VLOG_WARN_RL(&rl, "failed to parse HFSC class options. "
3295                      "Non-linear service curves are not supported.");
3296         return EPROTO;
3297     }
3298
3299     if (rsc->m2 != fsc->m2) {
3300         VLOG_WARN_RL(&rl, "failed to parse HFSC class options. "
3301                      "Real-time service curves are not supported ");
3302         return EPROTO;
3303     }
3304
3305     if (rsc->m2 > usc->m2) {
3306         VLOG_WARN_RL(&rl, "failed to parse HFSC class options. "
3307                      "Min-rate service curve is greater than "
3308                      "the max-rate service curve.");
3309         return EPROTO;
3310     }
3311
3312     class->min_rate = fsc->m2;
3313     class->max_rate = usc->m2;
3314     return 0;
3315 }
3316
3317 static int
3318 hfsc_parse_tcmsg__(struct ofpbuf *tcmsg, unsigned int *queue_id,
3319                    struct hfsc_class *options,
3320                    struct netdev_queue_stats *stats)
3321 {
3322     int error;
3323     unsigned int handle;
3324     struct nlattr *nl_options;
3325
3326     error = tc_parse_class(tcmsg, &handle, &nl_options, stats);
3327     if (error) {
3328         return error;
3329     }
3330
3331     if (queue_id) {
3332         unsigned int major, minor;
3333
3334         major = tc_get_major(handle);
3335         minor = tc_get_minor(handle);
3336         if (major == 1 && minor > 0 && minor <= HFSC_N_QUEUES) {
3337             *queue_id = minor - 1;
3338         } else {
3339             return EPROTO;
3340         }
3341     }
3342
3343     if (options) {
3344         error = hfsc_parse_tca_options__(nl_options, options);
3345     }
3346
3347     return error;
3348 }
3349
3350 static int
3351 hfsc_query_class__(const struct netdev *netdev, unsigned int handle,
3352                    unsigned int parent, struct hfsc_class *options,
3353                    struct netdev_queue_stats *stats)
3354 {
3355     int error;
3356     struct ofpbuf *reply;
3357
3358     error = tc_query_class(netdev, handle, parent, &reply);
3359     if (error) {
3360         return error;
3361     }
3362
3363     error = hfsc_parse_tcmsg__(reply, NULL, options, stats);
3364     ofpbuf_delete(reply);
3365     return error;
3366 }
3367
3368 static void
3369 hfsc_parse_qdisc_details__(struct netdev *netdev_, const struct smap *details,
3370                            struct hfsc_class *class)
3371 {
3372     struct netdev_linux *netdev = netdev_linux_cast(netdev_);
3373     uint32_t max_rate;
3374     const char *max_rate_s;
3375
3376     max_rate_s = smap_get(details, "max-rate");
3377     max_rate   = max_rate_s ? strtoull(max_rate_s, NULL, 10) / 8 : 0;
3378
3379     if (!max_rate) {
3380         enum netdev_features current;
3381
3382         netdev_linux_read_features(netdev);
3383         current = !netdev->get_features_error ? netdev->current : 0;
3384         max_rate = netdev_features_to_bps(current, 100 * 1000 * 1000) / 8;
3385     }
3386
3387     class->min_rate = max_rate;
3388     class->max_rate = max_rate;
3389 }
3390
3391 static int
3392 hfsc_parse_class_details__(struct netdev *netdev,
3393                            const struct smap *details,
3394                            struct hfsc_class * class)
3395 {
3396     const struct hfsc *hfsc;
3397     uint32_t min_rate, max_rate;
3398     const char *min_rate_s, *max_rate_s;
3399
3400     hfsc       = hfsc_get__(netdev);
3401     min_rate_s = smap_get(details, "min-rate");
3402     max_rate_s = smap_get(details, "max-rate");
3403
3404     min_rate = min_rate_s ? strtoull(min_rate_s, NULL, 10) / 8 : 0;
3405     min_rate = MAX(min_rate, 1);
3406     min_rate = MIN(min_rate, hfsc->max_rate);
3407
3408     max_rate = (max_rate_s
3409                 ? strtoull(max_rate_s, NULL, 10) / 8
3410                 : hfsc->max_rate);
3411     max_rate = MAX(max_rate, min_rate);
3412     max_rate = MIN(max_rate, hfsc->max_rate);
3413
3414     class->min_rate = min_rate;
3415     class->max_rate = max_rate;
3416
3417     return 0;
3418 }
3419
3420 /* Create an HFSC qdisc.
3421  *
3422  * Equivalent to "tc qdisc add dev <dev> root handle 1: hfsc default 1". */
3423 static int
3424 hfsc_setup_qdisc__(struct netdev * netdev)
3425 {
3426     struct tcmsg *tcmsg;
3427     struct ofpbuf request;
3428     struct tc_hfsc_qopt opt;
3429
3430     tc_del_qdisc(netdev);
3431
3432     tcmsg = tc_make_request(netdev, RTM_NEWQDISC,
3433                             NLM_F_EXCL | NLM_F_CREATE, &request);
3434
3435     if (!tcmsg) {
3436         return ENODEV;
3437     }
3438
3439     tcmsg->tcm_handle = tc_make_handle(1, 0);
3440     tcmsg->tcm_parent = TC_H_ROOT;
3441
3442     memset(&opt, 0, sizeof opt);
3443     opt.defcls = 1;
3444
3445     nl_msg_put_string(&request, TCA_KIND, "hfsc");
3446     nl_msg_put_unspec(&request, TCA_OPTIONS, &opt, sizeof opt);
3447
3448     return tc_transact(&request, NULL);
3449 }
3450
3451 /* Create an HFSC class.
3452  *
3453  * Equivalent to "tc class add <dev> parent <parent> classid <handle> hfsc
3454  * sc rate <min_rate> ul rate <max_rate>" */
3455 static int
3456 hfsc_setup_class__(struct netdev *netdev, unsigned int handle,
3457                    unsigned int parent, struct hfsc_class *class)
3458 {
3459     int error;
3460     size_t opt_offset;
3461     struct tcmsg *tcmsg;
3462     struct ofpbuf request;
3463     struct tc_service_curve min, max;
3464
3465     tcmsg = tc_make_request(netdev, RTM_NEWTCLASS, NLM_F_CREATE, &request);
3466
3467     if (!tcmsg) {
3468         return ENODEV;
3469     }
3470
3471     tcmsg->tcm_handle = handle;
3472     tcmsg->tcm_parent = parent;
3473
3474     min.m1 = 0;
3475     min.d  = 0;
3476     min.m2 = class->min_rate;
3477
3478     max.m1 = 0;
3479     max.d  = 0;
3480     max.m2 = class->max_rate;
3481
3482     nl_msg_put_string(&request, TCA_KIND, "hfsc");
3483     opt_offset = nl_msg_start_nested(&request, TCA_OPTIONS);
3484     nl_msg_put_unspec(&request, TCA_HFSC_RSC, &min, sizeof min);
3485     nl_msg_put_unspec(&request, TCA_HFSC_FSC, &min, sizeof min);
3486     nl_msg_put_unspec(&request, TCA_HFSC_USC, &max, sizeof max);
3487     nl_msg_end_nested(&request, opt_offset);
3488
3489     error = tc_transact(&request, NULL);
3490     if (error) {
3491         VLOG_WARN_RL(&rl, "failed to replace %s class %u:%u, parent %u:%u, "
3492                      "min-rate %ubps, max-rate %ubps (%s)",
3493                      netdev_get_name(netdev),
3494                      tc_get_major(handle), tc_get_minor(handle),
3495                      tc_get_major(parent), tc_get_minor(parent),
3496                      class->min_rate, class->max_rate, ovs_strerror(error));
3497     }
3498
3499     return error;
3500 }
3501
3502 static int
3503 hfsc_tc_install(struct netdev *netdev, const struct smap *details)
3504 {
3505     int error;
3506     struct hfsc_class class;
3507
3508     error = hfsc_setup_qdisc__(netdev);
3509
3510     if (error) {
3511         return error;
3512     }
3513
3514     hfsc_parse_qdisc_details__(netdev, details, &class);
3515     error = hfsc_setup_class__(netdev, tc_make_handle(1, 0xfffe),
3516                                tc_make_handle(1, 0), &class);
3517
3518     if (error) {
3519         return error;
3520     }
3521
3522     hfsc_install__(netdev, class.max_rate);
3523     return 0;
3524 }
3525
3526 static int
3527 hfsc_tc_load(struct netdev *netdev, struct ofpbuf *nlmsg OVS_UNUSED)
3528 {
3529     struct ofpbuf msg;
3530     struct nl_dump dump;
3531     struct hfsc_class hc;
3532
3533     hc.max_rate = 0;
3534     hfsc_query_class__(netdev, tc_make_handle(1, 0xfffe), 0, &hc, NULL);
3535     hfsc_install__(netdev, hc.max_rate);
3536
3537     if (!start_queue_dump(netdev, &dump)) {
3538         return ENODEV;
3539     }
3540
3541     while (nl_dump_next(&dump, &msg)) {
3542         unsigned int queue_id;
3543
3544         if (!hfsc_parse_tcmsg__(&msg, &queue_id, &hc, NULL)) {
3545             hfsc_update_queue__(netdev, queue_id, &hc);
3546         }
3547     }
3548
3549     nl_dump_done(&dump);
3550     return 0;
3551 }
3552
3553 static void
3554 hfsc_tc_destroy(struct tc *tc)
3555 {
3556     struct hfsc *hfsc;
3557     struct hfsc_class *hc, *next;
3558
3559     hfsc = CONTAINER_OF(tc, struct hfsc, tc);
3560
3561     HMAP_FOR_EACH_SAFE (hc, next, tc_queue.hmap_node, &hfsc->tc.queues) {
3562         hmap_remove(&hfsc->tc.queues, &hc->tc_queue.hmap_node);
3563         free(hc);
3564     }
3565
3566     tc_destroy(tc);
3567     free(hfsc);
3568 }
3569
3570 static int
3571 hfsc_qdisc_get(const struct netdev *netdev, struct smap *details)
3572 {
3573     const struct hfsc *hfsc;
3574     hfsc = hfsc_get__(netdev);
3575     smap_add_format(details, "max-rate", "%llu", 8ULL * hfsc->max_rate);
3576     return 0;
3577 }
3578
3579 static int
3580 hfsc_qdisc_set(struct netdev *netdev, const struct smap *details)
3581 {
3582     int error;
3583     struct hfsc_class class;
3584
3585     hfsc_parse_qdisc_details__(netdev, details, &class);
3586     error = hfsc_setup_class__(netdev, tc_make_handle(1, 0xfffe),
3587                                tc_make_handle(1, 0), &class);
3588
3589     if (!error) {
3590         hfsc_get__(netdev)->max_rate = class.max_rate;
3591     }
3592
3593     return error;
3594 }
3595
3596 static int
3597 hfsc_class_get(const struct netdev *netdev OVS_UNUSED,
3598               const struct tc_queue *queue, struct smap *details)
3599 {
3600     const struct hfsc_class *hc;
3601
3602     hc = hfsc_class_cast__(queue);
3603     smap_add_format(details, "min-rate", "%llu", 8ULL * hc->min_rate);
3604     if (hc->min_rate != hc->max_rate) {
3605         smap_add_format(details, "max-rate", "%llu", 8ULL * hc->max_rate);
3606     }
3607     return 0;
3608 }
3609
3610 static int
3611 hfsc_class_set(struct netdev *netdev, unsigned int queue_id,
3612                const struct smap *details)
3613 {
3614     int error;
3615     struct hfsc_class class;
3616
3617     error = hfsc_parse_class_details__(netdev, details, &class);
3618     if (error) {
3619         return error;
3620     }
3621
3622     error = hfsc_setup_class__(netdev, tc_make_handle(1, queue_id + 1),
3623                                tc_make_handle(1, 0xfffe), &class);
3624     if (error) {
3625         return error;
3626     }
3627
3628     hfsc_update_queue__(netdev, queue_id, &class);
3629     return 0;
3630 }
3631
3632 static int
3633 hfsc_class_delete(struct netdev *netdev, struct tc_queue *queue)
3634 {
3635     int error;
3636     struct hfsc *hfsc;
3637     struct hfsc_class *hc;
3638
3639     hc   = hfsc_class_cast__(queue);
3640     hfsc = hfsc_get__(netdev);
3641
3642     error = tc_delete_class(netdev, tc_make_handle(1, queue->queue_id + 1));
3643     if (!error) {
3644         hmap_remove(&hfsc->tc.queues, &hc->tc_queue.hmap_node);
3645         free(hc);
3646     }
3647     return error;
3648 }
3649
3650 static int
3651 hfsc_class_get_stats(const struct netdev *netdev, const struct tc_queue *queue,
3652                      struct netdev_queue_stats *stats)
3653 {
3654     return hfsc_query_class__(netdev, tc_make_handle(1, queue->queue_id + 1),
3655                              tc_make_handle(1, 0xfffe), NULL, stats);
3656 }
3657
3658 static int
3659 hfsc_class_dump_stats(const struct netdev *netdev OVS_UNUSED,
3660                       const struct ofpbuf *nlmsg,
3661                       netdev_dump_queue_stats_cb *cb, void *aux)
3662 {
3663     struct netdev_queue_stats stats;
3664     unsigned int handle, major, minor;
3665     int error;
3666
3667     error = tc_parse_class(nlmsg, &handle, NULL, &stats);
3668     if (error) {
3669         return error;
3670     }
3671
3672     major = tc_get_major(handle);
3673     minor = tc_get_minor(handle);
3674     if (major == 1 && minor > 0 && minor <= HFSC_N_QUEUES) {
3675         (*cb)(minor - 1, &stats, aux);
3676     }
3677     return 0;
3678 }
3679
3680 static const struct tc_ops tc_ops_hfsc = {
3681     "hfsc",                     /* linux_name */
3682     "linux-hfsc",               /* ovs_name */
3683     HFSC_N_QUEUES,              /* n_queues */
3684     hfsc_tc_install,            /* tc_install */
3685     hfsc_tc_load,               /* tc_load */
3686     hfsc_tc_destroy,            /* tc_destroy */
3687     hfsc_qdisc_get,             /* qdisc_get */
3688     hfsc_qdisc_set,             /* qdisc_set */
3689     hfsc_class_get,             /* class_get */
3690     hfsc_class_set,             /* class_set */
3691     hfsc_class_delete,          /* class_delete */
3692     hfsc_class_get_stats,       /* class_get_stats */
3693     hfsc_class_dump_stats       /* class_dump_stats */
3694 };
3695 \f
3696 /* "linux-default" traffic control class.
3697  *
3698  * This class represents the default, unnamed Linux qdisc.  It corresponds to
3699  * the "" (empty string) QoS type in the OVS database. */
3700
3701 static void
3702 default_install__(struct netdev *netdev_)
3703 {
3704     struct netdev_linux *netdev = netdev_linux_cast(netdev_);
3705     static const struct tc tc = TC_INITIALIZER(&tc, &tc_ops_default);
3706
3707     /* Nothing but a tc class implementation is allowed to write to a tc.  This
3708      * class never does that, so we can legitimately use a const tc object. */
3709     netdev->tc = CONST_CAST(struct tc *, &tc);
3710 }
3711
3712 static int
3713 default_tc_install(struct netdev *netdev,
3714                    const struct smap *details OVS_UNUSED)
3715 {
3716     default_install__(netdev);
3717     return 0;
3718 }
3719
3720 static int
3721 default_tc_load(struct netdev *netdev, struct ofpbuf *nlmsg OVS_UNUSED)
3722 {
3723     default_install__(netdev);
3724     return 0;
3725 }
3726
3727 static const struct tc_ops tc_ops_default = {
3728     NULL,                       /* linux_name */
3729     "",                         /* ovs_name */
3730     0,                          /* n_queues */
3731     default_tc_install,
3732     default_tc_load,
3733     NULL,                       /* tc_destroy */
3734     NULL,                       /* qdisc_get */
3735     NULL,                       /* qdisc_set */
3736     NULL,                       /* class_get */
3737     NULL,                       /* class_set */
3738     NULL,                       /* class_delete */
3739     NULL,                       /* class_get_stats */
3740     NULL                        /* class_dump_stats */
3741 };
3742 \f
3743 /* "linux-other" traffic control class.
3744  *
3745  * */
3746
3747 static int
3748 other_tc_load(struct netdev *netdev_, struct ofpbuf *nlmsg OVS_UNUSED)
3749 {
3750     struct netdev_linux *netdev = netdev_linux_cast(netdev_);
3751     static const struct tc tc = TC_INITIALIZER(&tc, &tc_ops_other);
3752
3753     /* Nothing but a tc class implementation is allowed to write to a tc.  This
3754      * class never does that, so we can legitimately use a const tc object. */
3755     netdev->tc = CONST_CAST(struct tc *, &tc);
3756     return 0;
3757 }
3758
3759 static const struct tc_ops tc_ops_other = {
3760     NULL,                       /* linux_name */
3761     "linux-other",              /* ovs_name */
3762     0,                          /* n_queues */
3763     NULL,                       /* tc_install */
3764     other_tc_load,
3765     NULL,                       /* tc_destroy */
3766     NULL,                       /* qdisc_get */
3767     NULL,                       /* qdisc_set */
3768     NULL,                       /* class_get */
3769     NULL,                       /* class_set */
3770     NULL,                       /* class_delete */
3771     NULL,                       /* class_get_stats */
3772     NULL                        /* class_dump_stats */
3773 };
3774 \f
3775 /* Traffic control. */
3776
3777 /* Number of kernel "tc" ticks per second. */
3778 static double ticks_per_s;
3779
3780 /* Number of kernel "jiffies" per second.  This is used for the purpose of
3781  * computing buffer sizes.  Generally kernel qdiscs need to be able to buffer
3782  * one jiffy's worth of data.
3783  *
3784  * There are two possibilities here:
3785  *
3786  *    - 'buffer_hz' is the kernel's real timer tick rate, a small number in the
3787  *      approximate range of 100 to 1024.  That means that we really need to
3788  *      make sure that the qdisc can buffer that much data.
3789  *
3790  *    - 'buffer_hz' is an absurdly large number.  That means that the kernel
3791  *      has finely granular timers and there's no need to fudge additional room
3792  *      for buffers.  (There's no extra effort needed to implement that: the
3793  *      large 'buffer_hz' is used as a divisor, so practically any number will
3794  *      come out as 0 in the division.  Small integer results in the case of
3795  *      really high dividends won't have any real effect anyhow.)
3796  */
3797 static unsigned int buffer_hz;
3798
3799 /* Returns tc handle 'major':'minor'. */
3800 static unsigned int
3801 tc_make_handle(unsigned int major, unsigned int minor)
3802 {
3803     return TC_H_MAKE(major << 16, minor);
3804 }
3805
3806 /* Returns the major number from 'handle'. */
3807 static unsigned int
3808 tc_get_major(unsigned int handle)
3809 {
3810     return TC_H_MAJ(handle) >> 16;
3811 }
3812
3813 /* Returns the minor number from 'handle'. */
3814 static unsigned int
3815 tc_get_minor(unsigned int handle)
3816 {
3817     return TC_H_MIN(handle);
3818 }
3819
3820 static struct tcmsg *
3821 tc_make_request(const struct netdev *netdev, int type, unsigned int flags,
3822                 struct ofpbuf *request)
3823 {
3824     struct tcmsg *tcmsg;
3825     int ifindex;
3826     int error;
3827
3828     error = get_ifindex(netdev, &ifindex);
3829     if (error) {
3830         return NULL;
3831     }
3832
3833     ofpbuf_init(request, 512);
3834     nl_msg_put_nlmsghdr(request, sizeof *tcmsg, type, NLM_F_REQUEST | flags);
3835     tcmsg = ofpbuf_put_zeros(request, sizeof *tcmsg);
3836     tcmsg->tcm_family = AF_UNSPEC;
3837     tcmsg->tcm_ifindex = ifindex;
3838     /* Caller should fill in tcmsg->tcm_handle. */
3839     /* Caller should fill in tcmsg->tcm_parent. */
3840
3841     return tcmsg;
3842 }
3843
3844 static int
3845 tc_transact(struct ofpbuf *request, struct ofpbuf **replyp)
3846 {
3847     int error = nl_transact(NETLINK_ROUTE, request, replyp);
3848     ofpbuf_uninit(request);
3849     return error;
3850 }
3851
3852 /* Adds or deletes a root ingress qdisc on 'netdev'.  We use this for
3853  * policing configuration.
3854  *
3855  * This function is equivalent to running the following when 'add' is true:
3856  *     /sbin/tc qdisc add dev <devname> handle ffff: ingress
3857  *
3858  * This function is equivalent to running the following when 'add' is false:
3859  *     /sbin/tc qdisc del dev <devname> handle ffff: ingress
3860  *
3861  * The configuration and stats may be seen with the following command:
3862  *     /sbin/tc -s qdisc show dev <devname>
3863  *
3864  * Returns 0 if successful, otherwise a positive errno value.
3865  */
3866 static int
3867 tc_add_del_ingress_qdisc(struct netdev *netdev, bool add)
3868 {
3869     struct ofpbuf request;
3870     struct tcmsg *tcmsg;
3871     int error;
3872     int type = add ? RTM_NEWQDISC : RTM_DELQDISC;
3873     int flags = add ? NLM_F_EXCL | NLM_F_CREATE : 0;
3874
3875     tcmsg = tc_make_request(netdev, type, flags, &request);
3876     if (!tcmsg) {
3877         return ENODEV;
3878     }
3879     tcmsg->tcm_handle = tc_make_handle(0xffff, 0);
3880     tcmsg->tcm_parent = TC_H_INGRESS;
3881     nl_msg_put_string(&request, TCA_KIND, "ingress");
3882     nl_msg_put_unspec(&request, TCA_OPTIONS, NULL, 0);
3883
3884     error = tc_transact(&request, NULL);
3885     if (error) {
3886         /* If we're deleting the qdisc, don't worry about some of the
3887          * error conditions. */
3888         if (!add && (error == ENOENT || error == EINVAL)) {
3889             return 0;
3890         }
3891         return error;
3892     }
3893
3894     return 0;
3895 }
3896
3897 /* Adds a policer to 'netdev' with a rate of 'kbits_rate' and a burst size
3898  * of 'kbits_burst'.
3899  *
3900  * This function is equivalent to running:
3901  *     /sbin/tc filter add dev <devname> parent ffff: protocol all prio 49
3902  *              basic police rate <kbits_rate>kbit burst <kbits_burst>k
3903  *              mtu 65535 drop
3904  *
3905  * The configuration and stats may be seen with the following command:
3906  *     /sbin/tc -s filter show <devname> eth0 parent ffff:
3907  *
3908  * Returns 0 if successful, otherwise a positive errno value.
3909  */
3910 static int
3911 tc_add_policer(struct netdev *netdev, int kbits_rate, int kbits_burst)
3912 {
3913     struct tc_police tc_police;
3914     struct ofpbuf request;
3915     struct tcmsg *tcmsg;
3916     size_t basic_offset;
3917     size_t police_offset;
3918     int error;
3919     int mtu = 65535;
3920
3921     memset(&tc_police, 0, sizeof tc_police);
3922     tc_police.action = TC_POLICE_SHOT;
3923     tc_police.mtu = mtu;
3924     tc_fill_rate(&tc_police.rate, (kbits_rate * 1000)/8, mtu);
3925     tc_police.burst = tc_bytes_to_ticks(tc_police.rate.rate,
3926                                         kbits_burst * 1024);
3927
3928     tcmsg = tc_make_request(netdev, RTM_NEWTFILTER,
3929                             NLM_F_EXCL | NLM_F_CREATE, &request);
3930     if (!tcmsg) {
3931         return ENODEV;
3932     }
3933     tcmsg->tcm_parent = tc_make_handle(0xffff, 0);
3934     tcmsg->tcm_info = tc_make_handle(49,
3935                                      (OVS_FORCE uint16_t) htons(ETH_P_ALL));
3936
3937     nl_msg_put_string(&request, TCA_KIND, "basic");
3938     basic_offset = nl_msg_start_nested(&request, TCA_OPTIONS);
3939     police_offset = nl_msg_start_nested(&request, TCA_BASIC_POLICE);
3940     nl_msg_put_unspec(&request, TCA_POLICE_TBF, &tc_police, sizeof tc_police);
3941     tc_put_rtab(&request, TCA_POLICE_RATE, &tc_police.rate);
3942     nl_msg_end_nested(&request, police_offset);
3943     nl_msg_end_nested(&request, basic_offset);
3944
3945     error = tc_transact(&request, NULL);
3946     if (error) {
3947         return error;
3948     }
3949
3950     return 0;
3951 }
3952
3953 static void
3954 read_psched(void)
3955 {
3956     /* The values in psched are not individually very meaningful, but they are
3957      * important.  The tables below show some values seen in the wild.
3958      *
3959      * Some notes:
3960      *
3961      *   - "c" has always been a constant 1000000 since at least Linux 2.4.14.
3962      *     (Before that, there are hints that it was 1000000000.)
3963      *
3964      *   - "d" can be unrealistically large, see the comment on 'buffer_hz'
3965      *     above.
3966      *
3967      *                        /proc/net/psched
3968      *     -----------------------------------
3969      * [1] 000c8000 000f4240 000f4240 00000064
3970      * [2] 000003e8 00000400 000f4240 3b9aca00
3971      * [3] 000003e8 00000400 000f4240 3b9aca00
3972      * [4] 000003e8 00000400 000f4240 00000064
3973      * [5] 000003e8 00000040 000f4240 3b9aca00
3974      * [6] 000003e8 00000040 000f4240 000000f9
3975      *
3976      *           a         b          c             d ticks_per_s     buffer_hz
3977      *     ------- --------- ---------- ------------- ----------- -------------
3978      * [1] 819,200 1,000,000  1,000,000           100     819,200           100
3979      * [2]   1,000     1,024  1,000,000 1,000,000,000     976,562 1,000,000,000
3980      * [3]   1,000     1,024  1,000,000 1,000,000,000     976,562 1,000,000,000
3981      * [4]   1,000     1,024  1,000,000           100     976,562           100
3982      * [5]   1,000        64  1,000,000 1,000,000,000  15,625,000 1,000,000,000
3983      * [6]   1,000        64  1,000,000           249  15,625,000           249
3984      *
3985      * [1] 2.6.18-128.1.6.el5.xs5.5.0.505.1024xen from XenServer 5.5.0-24648p
3986      * [2] 2.6.26-1-686-bigmem from Debian lenny
3987      * [3] 2.6.26-2-sparc64 from Debian lenny
3988      * [4] 2.6.27.42-0.1.1.xs5.6.810.44.111163xen from XenServer 5.6.810-31078p
3989      * [5] 2.6.32.21.22 (approx.) from Ubuntu 10.04 on VMware Fusion
3990      * [6] 2.6.34 from kernel.org on KVM
3991      */
3992     static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
3993     static const char fn[] = "/proc/net/psched";
3994     unsigned int a, b, c, d;
3995     FILE *stream;
3996
3997     if (!ovsthread_once_start(&once)) {
3998         return;
3999     }
4000
4001     ticks_per_s = 1.0;
4002     buffer_hz = 100;
4003
4004     stream = fopen(fn, "r");
4005     if (!stream) {
4006         VLOG_WARN("%s: open failed: %s", fn, ovs_strerror(errno));
4007         goto exit;
4008     }
4009
4010     if (fscanf(stream, "%x %x %x %x", &a, &b, &c, &d) != 4) {
4011         VLOG_WARN("%s: read failed", fn);
4012         fclose(stream);
4013         goto exit;
4014     }
4015     VLOG_DBG("%s: psched parameters are: %u %u %u %u", fn, a, b, c, d);
4016     fclose(stream);
4017
4018     if (!a || !c) {
4019         VLOG_WARN("%s: invalid scheduler parameters", fn);
4020         goto exit;
4021     }
4022
4023     ticks_per_s = (double) a * c / b;
4024     if (c == 1000000) {
4025         buffer_hz = d;
4026     } else {
4027         VLOG_WARN("%s: unexpected psched parameters: %u %u %u %u",
4028                   fn, a, b, c, d);
4029     }
4030     VLOG_DBG("%s: ticks_per_s=%f buffer_hz=%u", fn, ticks_per_s, buffer_hz);
4031
4032 exit:
4033     ovsthread_once_done(&once);
4034 }
4035
4036 /* Returns the number of bytes that can be transmitted in 'ticks' ticks at a
4037  * rate of 'rate' bytes per second. */
4038 static unsigned int
4039 tc_ticks_to_bytes(unsigned int rate, unsigned int ticks)
4040 {
4041     read_psched();
4042     return (rate * ticks) / ticks_per_s;
4043 }
4044
4045 /* Returns the number of ticks that it would take to transmit 'size' bytes at a
4046  * rate of 'rate' bytes per second. */
4047 static unsigned int
4048 tc_bytes_to_ticks(unsigned int rate, unsigned int size)
4049 {
4050     read_psched();
4051     return rate ? ((unsigned long long int) ticks_per_s * size) / rate : 0;
4052 }
4053
4054 /* Returns the number of bytes that need to be reserved for qdisc buffering at
4055  * a transmission rate of 'rate' bytes per second. */
4056 static unsigned int
4057 tc_buffer_per_jiffy(unsigned int rate)
4058 {
4059     read_psched();
4060     return rate / buffer_hz;
4061 }
4062
4063 /* Given Netlink 'msg' that describes a qdisc, extracts the name of the qdisc,
4064  * e.g. "htb", into '*kind' (if it is nonnull).  If 'options' is nonnull,
4065  * extracts 'msg''s TCA_OPTIONS attributes into '*options' if it is present or
4066  * stores NULL into it if it is absent.
4067  *
4068  * '*kind' and '*options' point into 'msg', so they are owned by whoever owns
4069  * 'msg'.
4070  *
4071  * Returns 0 if successful, otherwise a positive errno value. */
4072 static int
4073 tc_parse_qdisc(const struct ofpbuf *msg, const char **kind,
4074                struct nlattr **options)
4075 {
4076     static const struct nl_policy tca_policy[] = {
4077         [TCA_KIND] = { .type = NL_A_STRING, .optional = false },
4078         [TCA_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
4079     };
4080     struct nlattr *ta[ARRAY_SIZE(tca_policy)];
4081
4082     if (!nl_policy_parse(msg, NLMSG_HDRLEN + sizeof(struct tcmsg),
4083                          tca_policy, ta, ARRAY_SIZE(ta))) {
4084         VLOG_WARN_RL(&rl, "failed to parse qdisc message");
4085         goto error;
4086     }
4087
4088     if (kind) {
4089         *kind = nl_attr_get_string(ta[TCA_KIND]);
4090     }
4091
4092     if (options) {
4093         *options = ta[TCA_OPTIONS];
4094     }
4095
4096     return 0;
4097
4098 error:
4099     if (kind) {
4100         *kind = NULL;
4101     }
4102     if (options) {
4103         *options = NULL;
4104     }
4105     return EPROTO;
4106 }
4107
4108 /* Given Netlink 'msg' that describes a class, extracts the queue ID (e.g. the
4109  * minor number of its class ID) into '*queue_id', its TCA_OPTIONS attribute
4110  * into '*options', and its queue statistics into '*stats'.  Any of the output
4111  * arguments may be null.
4112  *
4113  * Returns 0 if successful, otherwise a positive errno value. */
4114 static int
4115 tc_parse_class(const struct ofpbuf *msg, unsigned int *handlep,
4116                struct nlattr **options, struct netdev_queue_stats *stats)
4117 {
4118     static const struct nl_policy tca_policy[] = {
4119         [TCA_OPTIONS] = { .type = NL_A_NESTED, .optional = false },
4120         [TCA_STATS2] = { .type = NL_A_NESTED, .optional = false },
4121     };
4122     struct nlattr *ta[ARRAY_SIZE(tca_policy)];
4123
4124     if (!nl_policy_parse(msg, NLMSG_HDRLEN + sizeof(struct tcmsg),
4125                          tca_policy, ta, ARRAY_SIZE(ta))) {
4126         VLOG_WARN_RL(&rl, "failed to parse class message");
4127         goto error;
4128     }
4129
4130     if (handlep) {
4131         struct tcmsg *tc = ofpbuf_at_assert(msg, NLMSG_HDRLEN, sizeof *tc);
4132         *handlep = tc->tcm_handle;
4133     }
4134
4135     if (options) {
4136         *options = ta[TCA_OPTIONS];
4137     }
4138
4139     if (stats) {
4140         const struct gnet_stats_queue *gsq;
4141         struct gnet_stats_basic gsb;
4142
4143         static const struct nl_policy stats_policy[] = {
4144             [TCA_STATS_BASIC] = { .type = NL_A_UNSPEC, .optional = false,
4145                                   .min_len = sizeof gsb },
4146             [TCA_STATS_QUEUE] = { .type = NL_A_UNSPEC, .optional = false,
4147                                   .min_len = sizeof *gsq },
4148         };
4149         struct nlattr *sa[ARRAY_SIZE(stats_policy)];
4150
4151         if (!nl_parse_nested(ta[TCA_STATS2], stats_policy,
4152                              sa, ARRAY_SIZE(sa))) {
4153             VLOG_WARN_RL(&rl, "failed to parse class stats");
4154             goto error;
4155         }
4156
4157         /* Alignment issues screw up the length of struct gnet_stats_basic on
4158          * some arch/bitsize combinations.  Newer versions of Linux have a
4159          * struct gnet_stats_basic_packed, but we can't depend on that.  The
4160          * easiest thing to do is just to make a copy. */
4161         memset(&gsb, 0, sizeof gsb);
4162         memcpy(&gsb, nl_attr_get(sa[TCA_STATS_BASIC]),
4163                MIN(nl_attr_get_size(sa[TCA_STATS_BASIC]), sizeof gsb));
4164         stats->tx_bytes = gsb.bytes;
4165         stats->tx_packets = gsb.packets;
4166
4167         gsq = nl_attr_get(sa[TCA_STATS_QUEUE]);
4168         stats->tx_errors = gsq->drops;
4169     }
4170
4171     return 0;
4172
4173 error:
4174     if (options) {
4175         *options = NULL;
4176     }
4177     if (stats) {
4178         memset(stats, 0, sizeof *stats);
4179     }
4180     return EPROTO;
4181 }
4182
4183 /* Queries the kernel for class with identifier 'handle' and parent 'parent'
4184  * on 'netdev'. */
4185 static int
4186 tc_query_class(const struct netdev *netdev,
4187                unsigned int handle, unsigned int parent,
4188                struct ofpbuf **replyp)
4189 {
4190     struct ofpbuf request;
4191     struct tcmsg *tcmsg;
4192     int error;
4193
4194     tcmsg = tc_make_request(netdev, RTM_GETTCLASS, NLM_F_ECHO, &request);
4195     if (!tcmsg) {
4196         return ENODEV;
4197     }
4198     tcmsg->tcm_handle = handle;
4199     tcmsg->tcm_parent = parent;
4200
4201     error = tc_transact(&request, replyp);
4202     if (error) {
4203         VLOG_WARN_RL(&rl, "query %s class %u:%u (parent %u:%u) failed (%s)",
4204                      netdev_get_name(netdev),
4205                      tc_get_major(handle), tc_get_minor(handle),
4206                      tc_get_major(parent), tc_get_minor(parent),
4207                      ovs_strerror(error));
4208     }
4209     return error;
4210 }
4211
4212 /* Equivalent to "tc class del dev <name> handle <handle>". */
4213 static int
4214 tc_delete_class(const struct netdev *netdev, unsigned int handle)
4215 {
4216     struct ofpbuf request;
4217     struct tcmsg *tcmsg;
4218     int error;
4219
4220     tcmsg = tc_make_request(netdev, RTM_DELTCLASS, 0, &request);
4221     if (!tcmsg) {
4222         return ENODEV;
4223     }
4224     tcmsg->tcm_handle = handle;
4225     tcmsg->tcm_parent = 0;
4226
4227     error = tc_transact(&request, NULL);
4228     if (error) {
4229         VLOG_WARN_RL(&rl, "delete %s class %u:%u failed (%s)",
4230                      netdev_get_name(netdev),
4231                      tc_get_major(handle), tc_get_minor(handle),
4232                      ovs_strerror(error));
4233     }
4234     return error;
4235 }
4236
4237 /* Equivalent to "tc qdisc del dev <name> root". */
4238 static int
4239 tc_del_qdisc(struct netdev *netdev_)
4240 {
4241     struct netdev_linux *netdev = netdev_linux_cast(netdev_);
4242     struct ofpbuf request;
4243     struct tcmsg *tcmsg;
4244     int error;
4245
4246     tcmsg = tc_make_request(netdev_, RTM_DELQDISC, 0, &request);
4247     if (!tcmsg) {
4248         return ENODEV;
4249     }
4250     tcmsg->tcm_handle = tc_make_handle(1, 0);
4251     tcmsg->tcm_parent = TC_H_ROOT;
4252
4253     error = tc_transact(&request, NULL);
4254     if (error == EINVAL) {
4255         /* EINVAL probably means that the default qdisc was in use, in which
4256          * case we've accomplished our purpose. */
4257         error = 0;
4258     }
4259     if (!error && netdev->tc) {
4260         if (netdev->tc->ops->tc_destroy) {
4261             netdev->tc->ops->tc_destroy(netdev->tc);
4262         }
4263         netdev->tc = NULL;
4264     }
4265     return error;
4266 }
4267
4268 /* If 'netdev''s qdisc type and parameters are not yet known, queries the
4269  * kernel to determine what they are.  Returns 0 if successful, otherwise a
4270  * positive errno value. */
4271 static int
4272 tc_query_qdisc(const struct netdev *netdev_)
4273 {
4274     struct netdev_linux *netdev = netdev_linux_cast(netdev_);
4275     struct ofpbuf request, *qdisc;
4276     const struct tc_ops *ops;
4277     struct tcmsg *tcmsg;
4278     int load_error;
4279     int error;
4280
4281     if (netdev->tc) {
4282         return 0;
4283     }
4284
4285     /* This RTM_GETQDISC is crafted to avoid OOPSing kernels that do not have
4286      * commit 53b0f08 "net_sched: Fix qdisc_notify()", which is anything before
4287      * 2.6.35 without that fix backported to it.
4288      *
4289      * To avoid the OOPS, we must not make a request that would attempt to dump
4290      * a "built-in" qdisc, that is, the default pfifo_fast qdisc or one of a
4291      * few others.  There are a few ways that I can see to do this, but most of
4292      * them seem to be racy (and if you lose the race the kernel OOPSes).  The
4293      * technique chosen here is to assume that any non-default qdisc that we
4294      * create will have a class with handle 1:0.  The built-in qdiscs only have
4295      * a class with handle 0:0.
4296      *
4297      * We could check for Linux 2.6.35+ and use a more straightforward method
4298      * there. */
4299     tcmsg = tc_make_request(netdev_, RTM_GETQDISC, NLM_F_ECHO, &request);
4300     if (!tcmsg) {
4301         return ENODEV;
4302     }
4303     tcmsg->tcm_handle = tc_make_handle(1, 0);
4304     tcmsg->tcm_parent = 0;
4305
4306     /* Figure out what tc class to instantiate. */
4307     error = tc_transact(&request, &qdisc);
4308     if (!error) {
4309         const char *kind;
4310
4311         error = tc_parse_qdisc(qdisc, &kind, NULL);
4312         if (error) {
4313             ops = &tc_ops_other;
4314         } else {
4315             ops = tc_lookup_linux_name(kind);
4316             if (!ops) {
4317                 static struct vlog_rate_limit rl2 = VLOG_RATE_LIMIT_INIT(1, 1);
4318                 VLOG_INFO_RL(&rl2, "unknown qdisc \"%s\"", kind);
4319
4320                 ops = &tc_ops_other;
4321             }
4322         }
4323     } else if (error == ENOENT) {
4324         /* Either it's a built-in qdisc, or it's a qdisc set up by some
4325          * other entity that doesn't have a handle 1:0.  We will assume
4326          * that it's the system default qdisc. */
4327         ops = &tc_ops_default;
4328         error = 0;
4329     } else {
4330         /* Who knows?  Maybe the device got deleted. */
4331         VLOG_WARN_RL(&rl, "query %s qdisc failed (%s)",
4332                      netdev_get_name(netdev_), ovs_strerror(error));
4333         ops = &tc_ops_other;
4334     }
4335
4336     /* Instantiate it. */
4337     load_error = ops->tc_load(CONST_CAST(struct netdev *, netdev_), qdisc);
4338     ovs_assert((load_error == 0) == (netdev->tc != NULL));
4339     ofpbuf_delete(qdisc);
4340
4341     return error ? error : load_error;
4342 }
4343
4344 /* Linux traffic control uses tables with 256 entries ("rtab" tables) to
4345    approximate the time to transmit packets of various lengths.  For an MTU of
4346    256 or less, each entry is exact; for an MTU of 257 through 512, each entry
4347    represents two possible packet lengths; for a MTU of 513 through 1024, four
4348    possible lengths; and so on.
4349
4350    Returns, for the specified 'mtu', the number of bits that packet lengths
4351    need to be shifted right to fit within such a 256-entry table. */
4352 static int
4353 tc_calc_cell_log(unsigned int mtu)
4354 {
4355     int cell_log;
4356
4357     if (!mtu) {
4358         mtu = ETH_PAYLOAD_MAX;
4359     }
4360     mtu += ETH_HEADER_LEN + VLAN_HEADER_LEN;
4361
4362     for (cell_log = 0; mtu >= 256; cell_log++) {
4363         mtu >>= 1;
4364     }
4365
4366     return cell_log;
4367 }
4368
4369 /* Initializes 'rate' properly for a rate of 'Bps' bytes per second with an MTU
4370  * of 'mtu'. */
4371 static void
4372 tc_fill_rate(struct tc_ratespec *rate, uint64_t Bps, int mtu)
4373 {
4374     memset(rate, 0, sizeof *rate);
4375     rate->cell_log = tc_calc_cell_log(mtu);
4376     /* rate->overhead = 0; */           /* New in 2.6.24, not yet in some */
4377     /* rate->cell_align = 0; */         /* distro headers. */
4378     rate->mpu = ETH_TOTAL_MIN;
4379     rate->rate = Bps;
4380 }
4381
4382 /* Appends to 'msg' an "rtab" table for the specified 'rate' as a Netlink
4383  * attribute of the specified "type".
4384  *
4385  * See tc_calc_cell_log() above for a description of "rtab"s. */
4386 static void
4387 tc_put_rtab(struct ofpbuf *msg, uint16_t type, const struct tc_ratespec *rate)
4388 {
4389     uint32_t *rtab;
4390     unsigned int i;
4391
4392     rtab = nl_msg_put_unspec_uninit(msg, type, TC_RTAB_SIZE);
4393     for (i = 0; i < TC_RTAB_SIZE / sizeof *rtab; i++) {
4394         unsigned packet_size = (i + 1) << rate->cell_log;
4395         if (packet_size < rate->mpu) {
4396             packet_size = rate->mpu;
4397         }
4398         rtab[i] = tc_bytes_to_ticks(rate->rate, packet_size);
4399     }
4400 }
4401
4402 /* Calculates the proper value of 'buffer' or 'cbuffer' in HTB options given a
4403  * rate of 'Bps' bytes per second, the specified 'mtu', and a user-requested
4404  * burst size of 'burst_bytes'.  (If no value was requested, a 'burst_bytes' of
4405  * 0 is fine.) */
4406 static int
4407 tc_calc_buffer(unsigned int Bps, int mtu, uint64_t burst_bytes)
4408 {
4409     unsigned int min_burst = tc_buffer_per_jiffy(Bps) + mtu;
4410     return tc_bytes_to_ticks(Bps, MAX(burst_bytes, min_burst));
4411 }
4412 \f
4413 /* Linux-only functions declared in netdev-linux.h  */
4414
4415 /* Modifies the 'flag' bit in ethtool's flags field for 'netdev'.  If
4416  * 'enable' is true, the bit is set.  Otherwise, it is cleared. */
4417 int
4418 netdev_linux_ethtool_set_flag(struct netdev *netdev, uint32_t flag,
4419                               const char *flag_name, bool enable)
4420 {
4421     const char *netdev_name = netdev_get_name(netdev);
4422     struct ethtool_value evalue;
4423     uint32_t new_flags;
4424     int error;
4425
4426     COVERAGE_INC(netdev_get_ethtool);
4427     memset(&evalue, 0, sizeof evalue);
4428     error = netdev_linux_do_ethtool(netdev_name,
4429                                     (struct ethtool_cmd *)&evalue,
4430                                     ETHTOOL_GFLAGS, "ETHTOOL_GFLAGS");
4431     if (error) {
4432         return error;
4433     }
4434
4435     COVERAGE_INC(netdev_set_ethtool);
4436     evalue.data = new_flags = (evalue.data & ~flag) | (enable ? flag : 0);
4437     error = netdev_linux_do_ethtool(netdev_name,
4438                                     (struct ethtool_cmd *)&evalue,
4439                                     ETHTOOL_SFLAGS, "ETHTOOL_SFLAGS");
4440     if (error) {
4441         return error;
4442     }
4443
4444     COVERAGE_INC(netdev_get_ethtool);
4445     memset(&evalue, 0, sizeof evalue);
4446     error = netdev_linux_do_ethtool(netdev_name,
4447                                     (struct ethtool_cmd *)&evalue,
4448                                     ETHTOOL_GFLAGS, "ETHTOOL_GFLAGS");
4449     if (error) {
4450         return error;
4451     }
4452
4453     if (new_flags != evalue.data) {
4454         VLOG_WARN_RL(&rl, "attempt to %s ethtool %s flag on network "
4455                      "device %s failed", enable ? "enable" : "disable",
4456                      flag_name, netdev_name);
4457         return EOPNOTSUPP;
4458     }
4459
4460     return 0;
4461 }
4462 \f
4463 /* Utility functions. */
4464
4465 /* Copies 'src' into 'dst', performing format conversion in the process. */
4466 static void
4467 netdev_stats_from_rtnl_link_stats(struct netdev_stats *dst,
4468                                   const struct rtnl_link_stats *src)
4469 {
4470     dst->rx_packets = src->rx_packets;
4471     dst->tx_packets = src->tx_packets;
4472     dst->rx_bytes = src->rx_bytes;
4473     dst->tx_bytes = src->tx_bytes;
4474     dst->rx_errors = src->rx_errors;
4475     dst->tx_errors = src->tx_errors;
4476     dst->rx_dropped = src->rx_dropped;
4477     dst->tx_dropped = src->tx_dropped;
4478     dst->multicast = src->multicast;
4479     dst->collisions = src->collisions;
4480     dst->rx_length_errors = src->rx_length_errors;
4481     dst->rx_over_errors = src->rx_over_errors;
4482     dst->rx_crc_errors = src->rx_crc_errors;
4483     dst->rx_frame_errors = src->rx_frame_errors;
4484     dst->rx_fifo_errors = src->rx_fifo_errors;
4485     dst->rx_missed_errors = src->rx_missed_errors;
4486     dst->tx_aborted_errors = src->tx_aborted_errors;
4487     dst->tx_carrier_errors = src->tx_carrier_errors;
4488     dst->tx_fifo_errors = src->tx_fifo_errors;
4489     dst->tx_heartbeat_errors = src->tx_heartbeat_errors;
4490     dst->tx_window_errors = src->tx_window_errors;
4491 }
4492
4493 static int
4494 get_stats_via_netlink(int ifindex, struct netdev_stats *stats)
4495 {
4496     /* Policy for RTNLGRP_LINK messages.
4497      *
4498      * There are *many* more fields in these messages, but currently we only
4499      * care about these fields. */
4500     static const struct nl_policy rtnlgrp_link_policy[] = {
4501         [IFLA_IFNAME] = { .type = NL_A_STRING, .optional = false },
4502         [IFLA_STATS] = { .type = NL_A_UNSPEC, .optional = true,
4503                          .min_len = sizeof(struct rtnl_link_stats) },
4504     };
4505
4506     struct ofpbuf request;
4507     struct ofpbuf *reply;
4508     struct ifinfomsg *ifi;
4509     struct nlattr *attrs[ARRAY_SIZE(rtnlgrp_link_policy)];
4510     int error;
4511
4512     ofpbuf_init(&request, 0);
4513     nl_msg_put_nlmsghdr(&request, sizeof *ifi, RTM_GETLINK, NLM_F_REQUEST);
4514     ifi = ofpbuf_put_zeros(&request, sizeof *ifi);
4515     ifi->ifi_family = PF_UNSPEC;
4516     ifi->ifi_index = ifindex;
4517     error = nl_transact(NETLINK_ROUTE, &request, &reply);
4518     ofpbuf_uninit(&request);
4519     if (error) {
4520         return error;
4521     }
4522
4523     if (!nl_policy_parse(reply, NLMSG_HDRLEN + sizeof(struct ifinfomsg),
4524                          rtnlgrp_link_policy,
4525                          attrs, ARRAY_SIZE(rtnlgrp_link_policy))) {
4526         ofpbuf_delete(reply);
4527         return EPROTO;
4528     }
4529
4530     if (!attrs[IFLA_STATS]) {
4531         VLOG_WARN_RL(&rl, "RTM_GETLINK reply lacks stats");
4532         ofpbuf_delete(reply);
4533         return EPROTO;
4534     }
4535
4536     netdev_stats_from_rtnl_link_stats(stats, nl_attr_get(attrs[IFLA_STATS]));
4537
4538     ofpbuf_delete(reply);
4539
4540     return 0;
4541 }
4542
4543 static int
4544 get_stats_via_proc(const char *netdev_name, struct netdev_stats *stats)
4545 {
4546     static const char fn[] = "/proc/net/dev";
4547     char line[1024];
4548     FILE *stream;
4549     int ln;
4550
4551     stream = fopen(fn, "r");
4552     if (!stream) {
4553         VLOG_WARN_RL(&rl, "%s: open failed: %s", fn, ovs_strerror(errno));
4554         return errno;
4555     }
4556
4557     ln = 0;
4558     while (fgets(line, sizeof line, stream)) {
4559         if (++ln >= 3) {
4560             char devname[16];
4561 #define X64 "%"SCNu64
4562             if (!ovs_scan(line,
4563                           " %15[^:]:"
4564                           X64 X64 X64 X64 X64 X64 X64 "%*u"
4565                           X64 X64 X64 X64 X64 X64 X64 "%*u",
4566                           devname,
4567                           &stats->rx_bytes,
4568                           &stats->rx_packets,
4569                           &stats->rx_errors,
4570                           &stats->rx_dropped,
4571                           &stats->rx_fifo_errors,
4572                           &stats->rx_frame_errors,
4573                           &stats->multicast,
4574                           &stats->tx_bytes,
4575                           &stats->tx_packets,
4576                           &stats->tx_errors,
4577                           &stats->tx_dropped,
4578                           &stats->tx_fifo_errors,
4579                           &stats->collisions,
4580                           &stats->tx_carrier_errors)) {
4581                 VLOG_WARN_RL(&rl, "%s:%d: parse error", fn, ln);
4582             } else if (!strcmp(devname, netdev_name)) {
4583                 stats->rx_length_errors = UINT64_MAX;
4584                 stats->rx_over_errors = UINT64_MAX;
4585                 stats->rx_crc_errors = UINT64_MAX;
4586                 stats->rx_missed_errors = UINT64_MAX;
4587                 stats->tx_aborted_errors = UINT64_MAX;
4588                 stats->tx_heartbeat_errors = UINT64_MAX;
4589                 stats->tx_window_errors = UINT64_MAX;
4590                 fclose(stream);
4591                 return 0;
4592             }
4593         }
4594     }
4595     VLOG_WARN_RL(&rl, "%s: no stats for %s", fn, netdev_name);
4596     fclose(stream);
4597     return ENODEV;
4598 }
4599
4600 static int
4601 get_flags(const struct netdev *dev, unsigned int *flags)
4602 {
4603     struct ifreq ifr;
4604     int error;
4605
4606     *flags = 0;
4607     error = af_inet_ifreq_ioctl(dev->name, &ifr, SIOCGIFFLAGS, "SIOCGIFFLAGS");
4608     if (!error) {
4609         *flags = ifr.ifr_flags;
4610     }
4611     return error;
4612 }
4613
4614 static int
4615 set_flags(const char *name, unsigned int flags)
4616 {
4617     struct ifreq ifr;
4618
4619     ifr.ifr_flags = flags;
4620     return af_inet_ifreq_ioctl(name, &ifr, SIOCSIFFLAGS, "SIOCSIFFLAGS");
4621 }
4622
4623 static int
4624 do_get_ifindex(const char *netdev_name)
4625 {
4626     struct ifreq ifr;
4627     int error;
4628
4629     ovs_strzcpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
4630     COVERAGE_INC(netdev_get_ifindex);
4631
4632     error = af_inet_ioctl(SIOCGIFINDEX, &ifr);
4633     if (error) {
4634         VLOG_WARN_RL(&rl, "ioctl(SIOCGIFINDEX) on %s device failed: %s",
4635                      netdev_name, ovs_strerror(error));
4636         return -error;
4637     }
4638     return ifr.ifr_ifindex;
4639 }
4640
4641 static int
4642 get_ifindex(const struct netdev *netdev_, int *ifindexp)
4643 {
4644     struct netdev_linux *netdev = netdev_linux_cast(netdev_);
4645
4646     if (!(netdev->cache_valid & VALID_IFINDEX)) {
4647         int ifindex = do_get_ifindex(netdev_get_name(netdev_));
4648
4649         if (ifindex < 0) {
4650             netdev->get_ifindex_error = -ifindex;
4651             netdev->ifindex = 0;
4652         } else {
4653             netdev->get_ifindex_error = 0;
4654             netdev->ifindex = ifindex;
4655         }
4656         netdev->cache_valid |= VALID_IFINDEX;
4657     }
4658
4659     *ifindexp = netdev->ifindex;
4660     return netdev->get_ifindex_error;
4661 }
4662
4663 static int
4664 get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN])
4665 {
4666     struct ifreq ifr;
4667     int hwaddr_family;
4668     int error;
4669
4670     memset(&ifr, 0, sizeof ifr);
4671     ovs_strzcpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
4672     COVERAGE_INC(netdev_get_hwaddr);
4673     error = af_inet_ioctl(SIOCGIFHWADDR, &ifr);
4674     if (error) {
4675         /* ENODEV probably means that a vif disappeared asynchronously and
4676          * hasn't been removed from the database yet, so reduce the log level
4677          * to INFO for that case. */
4678         VLOG(error == ENODEV ? VLL_INFO : VLL_ERR,
4679              "ioctl(SIOCGIFHWADDR) on %s device failed: %s",
4680              netdev_name, ovs_strerror(error));
4681         return error;
4682     }
4683     hwaddr_family = ifr.ifr_hwaddr.sa_family;
4684     if (hwaddr_family != AF_UNSPEC && hwaddr_family != ARPHRD_ETHER) {
4685         VLOG_WARN("%s device has unknown hardware address family %d",
4686                   netdev_name, hwaddr_family);
4687     }
4688     memcpy(ea, ifr.ifr_hwaddr.sa_data, ETH_ADDR_LEN);
4689     return 0;
4690 }
4691
4692 static int
4693 set_etheraddr(const char *netdev_name,
4694               const uint8_t mac[ETH_ADDR_LEN])
4695 {
4696     struct ifreq ifr;
4697     int error;
4698
4699     memset(&ifr, 0, sizeof ifr);
4700     ovs_strzcpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
4701     ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
4702     memcpy(ifr.ifr_hwaddr.sa_data, mac, ETH_ADDR_LEN);
4703     COVERAGE_INC(netdev_set_hwaddr);
4704     error = af_inet_ioctl(SIOCSIFHWADDR, &ifr);
4705     if (error) {
4706         VLOG_ERR("ioctl(SIOCSIFHWADDR) on %s device failed: %s",
4707                  netdev_name, ovs_strerror(error));
4708     }
4709     return error;
4710 }
4711
4712 static int
4713 netdev_linux_do_ethtool(const char *name, struct ethtool_cmd *ecmd,
4714                         int cmd, const char *cmd_name)
4715 {
4716     struct ifreq ifr;
4717     int error;
4718
4719     memset(&ifr, 0, sizeof ifr);
4720     ovs_strzcpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
4721     ifr.ifr_data = (caddr_t) ecmd;
4722
4723     ecmd->cmd = cmd;
4724     error = af_inet_ioctl(SIOCETHTOOL, &ifr);
4725     if (error) {
4726         if (error != EOPNOTSUPP) {
4727             VLOG_WARN_RL(&rl, "ethtool command %s on network device %s "
4728                          "failed: %s", cmd_name, name, ovs_strerror(error));
4729         } else {
4730             /* The device doesn't support this operation.  That's pretty
4731              * common, so there's no point in logging anything. */
4732         }
4733     }
4734     return error;
4735 }
4736
4737 static int
4738 netdev_linux_get_ipv4(const struct netdev *netdev, struct in_addr *ip,
4739                       int cmd, const char *cmd_name)
4740 {
4741     struct ifreq ifr;
4742     int error;
4743
4744     ifr.ifr_addr.sa_family = AF_INET;
4745     error = af_inet_ifreq_ioctl(netdev_get_name(netdev), &ifr, cmd, cmd_name);
4746     if (!error) {
4747         const struct sockaddr_in *sin = ALIGNED_CAST(struct sockaddr_in *,
4748                                                      &ifr.ifr_addr);
4749         *ip = sin->sin_addr;
4750     }
4751     return error;
4752 }
4753
4754 /* Returns an AF_PACKET raw socket or a negative errno value. */
4755 static int
4756 af_packet_sock(void)
4757 {
4758     static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
4759     static int sock;
4760
4761     if (ovsthread_once_start(&once)) {
4762         sock = socket(AF_PACKET, SOCK_RAW, 0);
4763         if (sock >= 0) {
4764             int error = set_nonblocking(sock);
4765             if (error) {
4766                 close(sock);
4767                 sock = -error;
4768             }
4769         } else {
4770             sock = -errno;
4771             VLOG_ERR("failed to create packet socket: %s",
4772                      ovs_strerror(errno));
4773         }
4774         ovsthread_once_done(&once);
4775     }
4776
4777     return sock;
4778 }