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