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