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