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