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