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