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