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