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