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