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