2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "netdev-linux.h"
23 #include <arpa/inet.h>
25 #include <linux/filter.h>
26 #include <linux/gen_stats.h>
27 #include <linux/if_ether.h>
28 #include <linux/if_tun.h>
29 #include <linux/types.h>
30 #include <linux/ethtool.h>
31 #include <linux/mii.h>
32 #include <linux/pkt_cls.h>
33 #include <linux/pkt_sched.h>
34 #include <linux/rtnetlink.h>
35 #include <linux/sockios.h>
36 #include <sys/types.h>
37 #include <sys/ioctl.h>
38 #include <sys/socket.h>
39 #include <sys/utsname.h>
40 #include <netpacket/packet.h>
42 #include <net/if_arp.h>
43 #include <net/if_packet.h>
44 #include <net/route.h>
45 #include <netinet/in.h>
52 #include "dp-packet.h"
53 #include "dpif-netlink.h"
54 #include "dpif-netdev.h"
55 #include "openvswitch/dynamic-string.h"
56 #include "fatal-signal.h"
58 #include "openvswitch/hmap.h"
59 #include "netdev-provider.h"
60 #include "netdev-vport.h"
61 #include "netlink-notifier.h"
62 #include "netlink-socket.h"
64 #include "openvswitch/ofpbuf.h"
65 #include "openflow/openflow.h"
66 #include "ovs-atomic.h"
68 #include "poll-loop.h"
69 #include "rtnetlink.h"
70 #include "openvswitch/shash.h"
71 #include "socket-util.h"
74 #include "unaligned.h"
75 #include "openvswitch/vlog.h"
78 VLOG_DEFINE_THIS_MODULE(netdev_linux);
80 COVERAGE_DEFINE(netdev_set_policing);
81 COVERAGE_DEFINE(netdev_arp_lookup);
82 COVERAGE_DEFINE(netdev_get_ifindex);
83 COVERAGE_DEFINE(netdev_get_hwaddr);
84 COVERAGE_DEFINE(netdev_set_hwaddr);
85 COVERAGE_DEFINE(netdev_get_ethtool);
86 COVERAGE_DEFINE(netdev_set_ethtool);
89 /* These were introduced in Linux 2.6.14, so they might be missing if we have
91 #ifndef ADVERTISED_Pause
92 #define ADVERTISED_Pause (1 << 13)
94 #ifndef ADVERTISED_Asym_Pause
95 #define ADVERTISED_Asym_Pause (1 << 14)
98 /* These were introduced in Linux 2.6.24, so they might be missing if we
99 * have old headers. */
100 #ifndef ETHTOOL_GFLAGS
101 #define ETHTOOL_GFLAGS 0x00000025 /* Get flags bitmap(ethtool_value) */
103 #ifndef ETHTOOL_SFLAGS
104 #define ETHTOOL_SFLAGS 0x00000026 /* Set flags bitmap(ethtool_value) */
107 /* This was introduced in Linux 2.6.25, so it might be missing if we have old
110 #define TC_RTAB_SIZE 1024
113 /* Linux 2.6.21 introduced struct tpacket_auxdata.
114 * Linux 2.6.27 added the tp_vlan_tci member.
115 * Linux 3.0 defined TP_STATUS_VLAN_VALID.
116 * Linux 3.13 repurposed a padding member for tp_vlan_tpid and defined
117 * TP_STATUS_VLAN_TPID_VALID.
119 * With all this churn it's easiest to unconditionally define a replacement
120 * structure that has everything we want.
122 #ifndef PACKET_AUXDATA
123 #define PACKET_AUXDATA 8
125 #ifndef TP_STATUS_VLAN_VALID
126 #define TP_STATUS_VLAN_VALID (1 << 4)
128 #ifndef TP_STATUS_VLAN_TPID_VALID
129 #define TP_STATUS_VLAN_TPID_VALID (1 << 6)
131 #undef tpacket_auxdata
132 #define tpacket_auxdata rpl_tpacket_auxdata
133 struct tpacket_auxdata {
139 uint16_t tp_vlan_tci;
140 uint16_t tp_vlan_tpid;
143 /* Linux 2.6.27 introduced ethtool_cmd_speed
145 * To avoid revisiting problems reported with using configure to detect
146 * compatibility (see report at
147 * http://openvswitch.org/pipermail/dev/2014-October/047978.html)
148 * unconditionally replace ethtool_cmd_speed. */
149 #define ethtool_cmd_speed rpl_ethtool_cmd_speed
150 static inline uint32_t rpl_ethtool_cmd_speed(const struct ethtool_cmd *ep)
152 return ep->speed | (ep->speed_hi << 16);
155 /* Linux 2.6.30 introduced supported and advertised flags for
156 * 1G base KX, and 10G base KX4, KR and R. */
157 #ifndef SUPPORTED_1000baseKX_Full
158 #define SUPPORTED_1000baseKX_Full (1 << 17)
159 #define SUPPORTED_10000baseKX4_Full (1 << 18)
160 #define SUPPORTED_10000baseKR_Full (1 << 19)
161 #define SUPPORTED_10000baseR_FEC (1 << 20)
162 #define ADVERTISED_1000baseKX_Full (1 << 17)
163 #define ADVERTISED_10000baseKX4_Full (1 << 18)
164 #define ADVERTISED_10000baseKR_Full (1 << 19)
165 #define ADVERTISED_10000baseR_FEC (1 << 20)
168 /* Linux 3.5 introduced supported and advertised flags for
169 * 40G base KR4, CR4, SR4 and LR4. */
170 #ifndef SUPPORTED_40000baseKR4_Full
171 #define SUPPORTED_40000baseKR4_Full (1 << 23)
172 #define SUPPORTED_40000baseCR4_Full (1 << 24)
173 #define SUPPORTED_40000baseSR4_Full (1 << 25)
174 #define SUPPORTED_40000baseLR4_Full (1 << 26)
175 #define ADVERTISED_40000baseKR4_Full (1 << 23)
176 #define ADVERTISED_40000baseCR4_Full (1 << 24)
177 #define ADVERTISED_40000baseSR4_Full (1 << 25)
178 #define ADVERTISED_40000baseLR4_Full (1 << 26)
181 /* Linux 2.6.35 introduced IFLA_STATS64 and rtnl_link_stats64.
183 * Tests for rtnl_link_stats64 don't seem to consistently work, e.g. on
184 * 2.6.32-431.29.2.el6.x86_64 (see report at
185 * http://openvswitch.org/pipermail/dev/2014-October/047978.html). Maybe
186 * if_link.h is not self-contained on those kernels. It is easiest to
187 * unconditionally define a replacement. */
189 #define IFLA_STATS64 23
191 #define rtnl_link_stats64 rpl_rtnl_link_stats64
192 struct rtnl_link_stats64 {
204 uint64_t rx_length_errors;
205 uint64_t rx_over_errors;
206 uint64_t rx_crc_errors;
207 uint64_t rx_frame_errors;
208 uint64_t rx_fifo_errors;
209 uint64_t rx_missed_errors;
211 uint64_t tx_aborted_errors;
212 uint64_t tx_carrier_errors;
213 uint64_t tx_fifo_errors;
214 uint64_t tx_heartbeat_errors;
215 uint64_t tx_window_errors;
217 uint64_t rx_compressed;
218 uint64_t tx_compressed;
222 VALID_IFINDEX = 1 << 0,
223 VALID_ETHERADDR = 1 << 1,
226 VALID_POLICING = 1 << 4,
227 VALID_VPORT_STAT_ERROR = 1 << 5,
228 VALID_DRVINFO = 1 << 6,
229 VALID_FEATURES = 1 << 7,
232 /* Traffic control. */
234 /* An instance of a traffic control class. Always associated with a particular
237 * Each TC implementation subclasses this with whatever additional data it
240 const struct tc_ops *ops;
241 struct hmap queues; /* Contains "struct tc_queue"s.
242 * Read by generic TC layer.
243 * Written only by TC implementation. */
246 #define TC_INITIALIZER(TC, OPS) { OPS, HMAP_INITIALIZER(&(TC)->queues) }
248 /* One traffic control queue.
250 * Each TC implementation subclasses this with whatever additional data it
253 struct hmap_node hmap_node; /* In struct tc's "queues" hmap. */
254 unsigned int queue_id; /* OpenFlow queue ID. */
255 long long int created; /* Time queue was created, in msecs. */
258 /* A particular kind of traffic control. Each implementation generally maps to
259 * one particular Linux qdisc class.
261 * The functions below return 0 if successful or a positive errno value on
262 * failure, except where otherwise noted. All of them must be provided, except
263 * where otherwise noted. */
265 /* Name used by kernel in the TCA_KIND attribute of tcmsg, e.g. "htb".
266 * This is null for tc_ops_default and tc_ops_other, for which there are no
267 * appropriate values. */
268 const char *linux_name;
270 /* Name used in OVS database, e.g. "linux-htb". Must be nonnull. */
271 const char *ovs_name;
273 /* Number of supported OpenFlow queues, 0 for qdiscs that have no
274 * queues. The queues are numbered 0 through n_queues - 1. */
275 unsigned int n_queues;
277 /* Called to install this TC class on 'netdev'. The implementation should
278 * make the Netlink calls required to set up 'netdev' with the right qdisc
279 * and configure it according to 'details'. The implementation may assume
280 * that the current qdisc is the default; that is, there is no need for it
281 * to delete the current qdisc before installing itself.
283 * The contents of 'details' should be documented as valid for 'ovs_name'
284 * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml
285 * (which is built as ovs-vswitchd.conf.db(8)).
287 * This function must return 0 if and only if it sets 'netdev->tc' to an
288 * initialized 'struct tc'.
290 * (This function is null for tc_ops_other, which cannot be installed. For
291 * other TC classes it should always be nonnull.) */
292 int (*tc_install)(struct netdev *netdev, const struct smap *details);
294 /* Called when the netdev code determines (through a Netlink query) that
295 * this TC class's qdisc is installed on 'netdev', but we didn't install
296 * it ourselves and so don't know any of the details.
298 * 'nlmsg' is the kernel reply to a RTM_GETQDISC Netlink message for
299 * 'netdev'. The TCA_KIND attribute of 'nlmsg' is 'linux_name'. The
300 * implementation should parse the other attributes of 'nlmsg' as
301 * necessary to determine its configuration. If necessary it should also
302 * use Netlink queries to determine the configuration of queues on
305 * This function must return 0 if and only if it sets 'netdev->tc' to an
306 * initialized 'struct tc'. */
307 int (*tc_load)(struct netdev *netdev, struct ofpbuf *nlmsg);
309 /* Destroys the data structures allocated by the implementation as part of
310 * 'tc'. (This includes destroying 'tc->queues' by calling
313 * The implementation should not need to perform any Netlink calls. If
314 * desirable, the caller is responsible for deconfiguring the kernel qdisc.
315 * (But it may not be desirable.)
317 * This function may be null if 'tc' is trivial. */
318 void (*tc_destroy)(struct tc *tc);
320 /* Retrieves details of 'netdev->tc' configuration into 'details'.
322 * The implementation should not need to perform any Netlink calls, because
323 * the 'tc_install' or 'tc_load' that instantiated 'netdev->tc' should have
324 * cached the configuration.
326 * The contents of 'details' should be documented as valid for 'ovs_name'
327 * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml
328 * (which is built as ovs-vswitchd.conf.db(8)).
330 * This function may be null if 'tc' is not configurable.
332 int (*qdisc_get)(const struct netdev *netdev, struct smap *details);
334 /* Reconfigures 'netdev->tc' according to 'details', performing any
335 * required Netlink calls to complete the reconfiguration.
337 * The contents of 'details' should be documented as valid for 'ovs_name'
338 * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml
339 * (which is built as ovs-vswitchd.conf.db(8)).
341 * This function may be null if 'tc' is not configurable.
343 int (*qdisc_set)(struct netdev *, const struct smap *details);
345 /* Retrieves details of 'queue' on 'netdev->tc' into 'details'. 'queue' is
346 * one of the 'struct tc_queue's within 'netdev->tc->queues'.
348 * The contents of 'details' should be documented as valid for 'ovs_name'
349 * in the "other_config" column in the "Queue" table in
350 * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
352 * The implementation should not need to perform any Netlink calls, because
353 * the 'tc_install' or 'tc_load' that instantiated 'netdev->tc' should have
354 * cached the queue configuration.
356 * This function may be null if 'tc' does not have queues ('n_queues' is
358 int (*class_get)(const struct netdev *netdev, const struct tc_queue *queue,
359 struct smap *details);
361 /* Configures or reconfigures 'queue_id' on 'netdev->tc' according to
362 * 'details', perfoming any required Netlink calls to complete the
363 * reconfiguration. The caller ensures that 'queue_id' is less than
366 * The contents of 'details' should be documented as valid for 'ovs_name'
367 * in the "other_config" column in the "Queue" table in
368 * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
370 * This function may be null if 'tc' does not have queues or its queues are
371 * not configurable. */
372 int (*class_set)(struct netdev *, unsigned int queue_id,
373 const struct smap *details);
375 /* Deletes 'queue' from 'netdev->tc'. 'queue' is one of the 'struct
376 * tc_queue's within 'netdev->tc->queues'.
378 * This function may be null if 'tc' does not have queues or its queues
379 * cannot be deleted. */
380 int (*class_delete)(struct netdev *, struct tc_queue *queue);
382 /* Obtains stats for 'queue' from 'netdev->tc'. 'queue' is one of the
383 * 'struct tc_queue's within 'netdev->tc->queues'.
385 * On success, initializes '*stats'.
387 * This function may be null if 'tc' does not have queues or if it cannot
388 * report queue statistics. */
389 int (*class_get_stats)(const struct netdev *netdev,
390 const struct tc_queue *queue,
391 struct netdev_queue_stats *stats);
393 /* Extracts queue stats from 'nlmsg', which is a response to a
394 * RTM_GETTCLASS message, and passes them to 'cb' along with 'aux'.
396 * This function may be null if 'tc' does not have queues or if it cannot
397 * report queue statistics. */
398 int (*class_dump_stats)(const struct netdev *netdev,
399 const struct ofpbuf *nlmsg,
400 netdev_dump_queue_stats_cb *cb, void *aux);
404 tc_init(struct tc *tc, const struct tc_ops *ops)
407 hmap_init(&tc->queues);
411 tc_destroy(struct tc *tc)
413 hmap_destroy(&tc->queues);
416 static const struct tc_ops tc_ops_htb;
417 static const struct tc_ops tc_ops_hfsc;
418 static const struct tc_ops tc_ops_codel;
419 static const struct tc_ops tc_ops_fqcodel;
420 static const struct tc_ops tc_ops_sfq;
421 static const struct tc_ops tc_ops_default;
422 static const struct tc_ops tc_ops_noop;
423 static const struct tc_ops tc_ops_other;
425 static const struct tc_ops *const tcs[] = {
426 &tc_ops_htb, /* Hierarchy token bucket (see tc-htb(8)). */
427 &tc_ops_hfsc, /* Hierarchical fair service curve. */
428 &tc_ops_codel, /* Controlled delay */
429 &tc_ops_fqcodel, /* Fair queue controlled delay */
430 &tc_ops_sfq, /* Stochastic fair queueing */
431 &tc_ops_noop, /* Non operating qos type. */
432 &tc_ops_default, /* Default qdisc (see tc-pfifo_fast(8)). */
433 &tc_ops_other, /* Some other qdisc. */
437 static unsigned int tc_make_handle(unsigned int major, unsigned int minor);
438 static unsigned int tc_get_major(unsigned int handle);
439 static unsigned int tc_get_minor(unsigned int handle);
441 static unsigned int tc_ticks_to_bytes(unsigned int rate, unsigned int ticks);
442 static unsigned int tc_bytes_to_ticks(unsigned int rate, unsigned int size);
443 static unsigned int tc_buffer_per_jiffy(unsigned int rate);
445 static struct tcmsg *tc_make_request(const struct netdev *, int type,
446 unsigned int flags, struct ofpbuf *);
447 static int tc_transact(struct ofpbuf *request, struct ofpbuf **replyp);
448 static int tc_add_del_ingress_qdisc(struct netdev *netdev, bool add);
449 static int tc_add_policer(struct netdev *,
450 uint32_t kbits_rate, uint32_t kbits_burst);
452 static int tc_parse_qdisc(const struct ofpbuf *, const char **kind,
453 struct nlattr **options);
454 static int tc_parse_class(const struct ofpbuf *, unsigned int *queue_id,
455 struct nlattr **options,
456 struct netdev_queue_stats *);
457 static int tc_query_class(const struct netdev *,
458 unsigned int handle, unsigned int parent,
459 struct ofpbuf **replyp);
460 static int tc_delete_class(const struct netdev *, unsigned int handle);
462 static int tc_del_qdisc(struct netdev *netdev);
463 static int tc_query_qdisc(const struct netdev *netdev);
465 static int tc_calc_cell_log(unsigned int mtu);
466 static void tc_fill_rate(struct tc_ratespec *rate, uint64_t bps, int mtu);
467 static void tc_put_rtab(struct ofpbuf *, uint16_t type,
468 const struct tc_ratespec *rate);
469 static int tc_calc_buffer(unsigned int Bps, int mtu, uint64_t burst_bytes);
471 struct netdev_linux {
474 /* Protects all members below. */
475 struct ovs_mutex mutex;
477 unsigned int cache_valid;
479 bool miimon; /* Link status of last poll. */
480 long long int miimon_interval; /* Miimon Poll rate. Disabled if <= 0. */
481 struct timer miimon_timer;
483 /* The following are figured out "on demand" only. They are only valid
484 * when the corresponding VALID_* bit in 'cache_valid' is set. */
486 struct eth_addr etheraddr;
488 unsigned int ifi_flags;
489 long long int carrier_resets;
490 uint32_t kbits_rate; /* Policing data. */
491 uint32_t kbits_burst;
492 int vport_stats_error; /* Cached error code from vport_get_stats().
493 0 or an errno value. */
494 int netdev_mtu_error; /* Cached error code from SIOCGIFMTU or SIOCSIFMTU. */
495 int ether_addr_error; /* Cached error code from set/get etheraddr. */
496 int netdev_policing_error; /* Cached error code from set policing. */
497 int get_features_error; /* Cached error code from ETHTOOL_GSET. */
498 int get_ifindex_error; /* Cached error code from SIOCGIFINDEX. */
500 enum netdev_features current; /* Cached from ETHTOOL_GSET. */
501 enum netdev_features advertised; /* Cached from ETHTOOL_GSET. */
502 enum netdev_features supported; /* Cached from ETHTOOL_GSET. */
504 struct ethtool_drvinfo drvinfo; /* Cached from ETHTOOL_GDRVINFO. */
507 /* For devices of class netdev_tap_class only. */
511 struct netdev_rxq_linux {
512 struct netdev_rxq up;
517 /* This is set pretty low because we probably won't learn anything from the
518 * additional log messages. */
519 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
521 /* Polling miimon status for all ports causes performance degradation when
522 * handling a large number of ports. If there are no devices using miimon, then
523 * we skip netdev_linux_miimon_run() and netdev_linux_miimon_wait().
525 * Readers do not depend on this variable synchronizing with the related
526 * changes in the device miimon status, so we can use atomic_count. */
527 static atomic_count miimon_cnt = ATOMIC_COUNT_INIT(0);
529 static void netdev_linux_run(void);
531 static int netdev_linux_do_ethtool(const char *name, struct ethtool_cmd *,
532 int cmd, const char *cmd_name);
533 static int get_flags(const struct netdev *, unsigned int *flags);
534 static int set_flags(const char *, unsigned int flags);
535 static int update_flags(struct netdev_linux *netdev, enum netdev_flags off,
536 enum netdev_flags on, enum netdev_flags *old_flagsp)
537 OVS_REQUIRES(netdev->mutex);
538 static int do_get_ifindex(const char *netdev_name);
539 static int get_ifindex(const struct netdev *, int *ifindexp);
540 static int do_set_addr(struct netdev *netdev,
541 int ioctl_nr, const char *ioctl_name,
542 struct in_addr addr);
543 static int get_etheraddr(const char *netdev_name, struct eth_addr *ea);
544 static int set_etheraddr(const char *netdev_name, const struct eth_addr);
545 static int get_stats_via_netlink(const struct netdev *, struct netdev_stats *);
546 static int af_packet_sock(void);
547 static bool netdev_linux_miimon_enabled(void);
548 static void netdev_linux_miimon_run(void);
549 static void netdev_linux_miimon_wait(void);
550 static int netdev_linux_get_mtu__(struct netdev_linux *netdev, int *mtup);
553 is_netdev_linux_class(const struct netdev_class *netdev_class)
555 return netdev_class->run == netdev_linux_run;
559 is_tap_netdev(const struct netdev *netdev)
561 return netdev_get_class(netdev) == &netdev_tap_class;
564 static struct netdev_linux *
565 netdev_linux_cast(const struct netdev *netdev)
567 ovs_assert(is_netdev_linux_class(netdev_get_class(netdev)));
569 return CONTAINER_OF(netdev, struct netdev_linux, up);
572 static struct netdev_rxq_linux *
573 netdev_rxq_linux_cast(const struct netdev_rxq *rx)
575 ovs_assert(is_netdev_linux_class(netdev_get_class(rx->netdev)));
576 return CONTAINER_OF(rx, struct netdev_rxq_linux, up);
579 static void netdev_linux_update(struct netdev_linux *netdev,
580 const struct rtnetlink_change *)
581 OVS_REQUIRES(netdev->mutex);
582 static void netdev_linux_changed(struct netdev_linux *netdev,
583 unsigned int ifi_flags, unsigned int mask)
584 OVS_REQUIRES(netdev->mutex);
586 /* Returns a NETLINK_ROUTE socket listening for RTNLGRP_LINK,
587 * RTNLGRP_IPV4_IFADDR and RTNLGRP_IPV6_IFADDR changes, or NULL
588 * if no such socket could be created. */
589 static struct nl_sock *
590 netdev_linux_notify_sock(void)
592 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
593 static struct nl_sock *sock;
594 unsigned int mcgroups[] = {RTNLGRP_LINK, RTNLGRP_IPV4_IFADDR,
595 RTNLGRP_IPV6_IFADDR, RTNLGRP_IPV6_IFINFO};
597 if (ovsthread_once_start(&once)) {
600 error = nl_sock_create(NETLINK_ROUTE, &sock);
604 for (i = 0; i < ARRAY_SIZE(mcgroups); i++) {
605 error = nl_sock_join_mcgroup(sock, mcgroups[i]);
607 nl_sock_destroy(sock);
613 ovsthread_once_done(&once);
620 netdev_linux_miimon_enabled(void)
622 return atomic_count_get(&miimon_cnt) > 0;
626 netdev_linux_run(void)
628 struct nl_sock *sock;
631 if (netdev_linux_miimon_enabled()) {
632 netdev_linux_miimon_run();
635 sock = netdev_linux_notify_sock();
641 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
642 uint64_t buf_stub[4096 / 8];
645 ofpbuf_use_stub(&buf, buf_stub, sizeof buf_stub);
646 error = nl_sock_recv(sock, &buf, false);
648 struct rtnetlink_change change;
650 if (rtnetlink_parse(&buf, &change)) {
651 struct netdev *netdev_ = NULL;
652 char dev_name[IFNAMSIZ];
654 if (!change.ifname) {
655 change.ifname = if_indextoname(change.if_index, dev_name);
659 netdev_ = netdev_from_name(change.ifname);
661 if (netdev_ && is_netdev_linux_class(netdev_->netdev_class)) {
662 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
664 ovs_mutex_lock(&netdev->mutex);
665 netdev_linux_update(netdev, &change);
666 ovs_mutex_unlock(&netdev->mutex);
668 netdev_close(netdev_);
670 } else if (error == ENOBUFS) {
671 struct shash device_shash;
672 struct shash_node *node;
676 shash_init(&device_shash);
677 netdev_get_devices(&netdev_linux_class, &device_shash);
678 SHASH_FOR_EACH (node, &device_shash) {
679 struct netdev *netdev_ = node->data;
680 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
683 ovs_mutex_lock(&netdev->mutex);
684 get_flags(netdev_, &flags);
685 netdev_linux_changed(netdev, flags, 0);
686 ovs_mutex_unlock(&netdev->mutex);
688 netdev_close(netdev_);
690 shash_destroy(&device_shash);
691 } else if (error != EAGAIN) {
692 VLOG_WARN_RL(&rl, "error reading or parsing netlink (%s)",
693 ovs_strerror(error));
700 netdev_linux_wait(void)
702 struct nl_sock *sock;
704 if (netdev_linux_miimon_enabled()) {
705 netdev_linux_miimon_wait();
707 sock = netdev_linux_notify_sock();
709 nl_sock_wait(sock, POLLIN);
714 netdev_linux_changed(struct netdev_linux *dev,
715 unsigned int ifi_flags, unsigned int mask)
716 OVS_REQUIRES(dev->mutex)
718 netdev_change_seq_changed(&dev->up);
720 if ((dev->ifi_flags ^ ifi_flags) & IFF_RUNNING) {
721 dev->carrier_resets++;
723 dev->ifi_flags = ifi_flags;
725 dev->cache_valid &= mask;
726 if (!(mask & VALID_IN)) {
727 netdev_get_addrs_list_flush();
732 netdev_linux_update(struct netdev_linux *dev,
733 const struct rtnetlink_change *change)
734 OVS_REQUIRES(dev->mutex)
736 if (rtnetlink_type_is_rtnlgrp_link(change->nlmsg_type)){
737 if (change->nlmsg_type == RTM_NEWLINK) {
738 /* Keep drv-info, and ip addresses. */
739 netdev_linux_changed(dev, change->ifi_flags,
740 VALID_DRVINFO | VALID_IN);
742 /* Update netdev from rtnl-change msg. */
744 dev->mtu = change->mtu;
745 dev->cache_valid |= VALID_MTU;
746 dev->netdev_mtu_error = 0;
749 if (!eth_addr_is_zero(change->mac)) {
750 dev->etheraddr = change->mac;
751 dev->cache_valid |= VALID_ETHERADDR;
752 dev->ether_addr_error = 0;
755 dev->ifindex = change->if_index;
756 dev->cache_valid |= VALID_IFINDEX;
757 dev->get_ifindex_error = 0;
759 netdev_linux_changed(dev, change->ifi_flags, 0);
761 } else if (rtnetlink_type_is_rtnlgrp_addr(change->nlmsg_type)) {
762 /* Invalidates in4, in6. */
763 netdev_linux_changed(dev, dev->ifi_flags, ~VALID_IN);
769 static struct netdev *
770 netdev_linux_alloc(void)
772 struct netdev_linux *netdev = xzalloc(sizeof *netdev);
777 netdev_linux_common_construct(struct netdev_linux *netdev)
779 ovs_mutex_init(&netdev->mutex);
782 /* Creates system and internal devices. */
784 netdev_linux_construct(struct netdev *netdev_)
786 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
789 netdev_linux_common_construct(netdev);
791 error = get_flags(&netdev->up, &netdev->ifi_flags);
792 if (error == ENODEV) {
793 if (netdev->up.netdev_class != &netdev_internal_class) {
794 /* The device does not exist, so don't allow it to be opened. */
797 /* "Internal" netdevs have to be created as netdev objects before
798 * they exist in the kernel, because creating them in the kernel
799 * happens by passing a netdev object to dpif_port_add().
800 * Therefore, ignore the error. */
807 /* For most types of netdevs we open the device for each call of
808 * netdev_open(). However, this is not the case with tap devices,
809 * since it is only possible to open the device once. In this
810 * situation we share a single file descriptor, and consequently
811 * buffers, across all readers. Therefore once data is read it will
812 * be unavailable to other reads for tap devices. */
814 netdev_linux_construct_tap(struct netdev *netdev_)
816 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
817 static const char tap_dev[] = "/dev/net/tun";
818 const char *name = netdev_->name;
822 netdev_linux_common_construct(netdev);
824 /* Open tap device. */
825 netdev->tap_fd = open(tap_dev, O_RDWR);
826 if (netdev->tap_fd < 0) {
828 VLOG_WARN("opening \"%s\" failed: %s", tap_dev, ovs_strerror(error));
832 /* Create tap device. */
833 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
834 ovs_strzcpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
835 if (ioctl(netdev->tap_fd, TUNSETIFF, &ifr) == -1) {
836 VLOG_WARN("%s: creating tap device failed: %s", name,
837 ovs_strerror(errno));
842 /* Make non-blocking. */
843 error = set_nonblocking(netdev->tap_fd);
851 close(netdev->tap_fd);
856 netdev_linux_destruct(struct netdev *netdev_)
858 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
860 if (netdev->tc && netdev->tc->ops->tc_destroy) {
861 netdev->tc->ops->tc_destroy(netdev->tc);
864 if (netdev_get_class(netdev_) == &netdev_tap_class
865 && netdev->tap_fd >= 0)
867 close(netdev->tap_fd);
870 if (netdev->miimon_interval > 0) {
871 atomic_count_dec(&miimon_cnt);
874 ovs_mutex_destroy(&netdev->mutex);
878 netdev_linux_dealloc(struct netdev *netdev_)
880 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
884 static struct netdev_rxq *
885 netdev_linux_rxq_alloc(void)
887 struct netdev_rxq_linux *rx = xzalloc(sizeof *rx);
892 netdev_linux_rxq_construct(struct netdev_rxq *rxq_)
894 struct netdev_rxq_linux *rx = netdev_rxq_linux_cast(rxq_);
895 struct netdev *netdev_ = rx->up.netdev;
896 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
899 ovs_mutex_lock(&netdev->mutex);
900 rx->is_tap = is_tap_netdev(netdev_);
902 rx->fd = netdev->tap_fd;
904 struct sockaddr_ll sll;
906 /* Result of tcpdump -dd inbound */
907 static const struct sock_filter filt[] = {
908 { 0x28, 0, 0, 0xfffff004 }, /* ldh [0] */
909 { 0x15, 0, 1, 0x00000004 }, /* jeq #4 jt 2 jf 3 */
910 { 0x6, 0, 0, 0x00000000 }, /* ret #0 */
911 { 0x6, 0, 0, 0x0000ffff } /* ret #65535 */
913 static const struct sock_fprog fprog = {
914 ARRAY_SIZE(filt), (struct sock_filter *) filt
917 /* Create file descriptor. */
918 rx->fd = socket(PF_PACKET, SOCK_RAW, 0);
921 VLOG_ERR("failed to create raw socket (%s)", ovs_strerror(error));
926 if (setsockopt(rx->fd, SOL_PACKET, PACKET_AUXDATA, &val, sizeof val)) {
928 VLOG_ERR("%s: failed to mark socket for auxdata (%s)",
929 netdev_get_name(netdev_), ovs_strerror(error));
933 /* Set non-blocking mode. */
934 error = set_nonblocking(rx->fd);
939 /* Get ethernet device index. */
940 error = get_ifindex(&netdev->up, &ifindex);
945 /* Bind to specific ethernet device. */
946 memset(&sll, 0, sizeof sll);
947 sll.sll_family = AF_PACKET;
948 sll.sll_ifindex = ifindex;
949 sll.sll_protocol = htons(ETH_P_ALL);
950 if (bind(rx->fd, (struct sockaddr *) &sll, sizeof sll) < 0) {
952 VLOG_ERR("%s: failed to bind raw socket (%s)",
953 netdev_get_name(netdev_), ovs_strerror(error));
957 /* Filter for only inbound packets. */
958 error = setsockopt(rx->fd, SOL_SOCKET, SO_ATTACH_FILTER, &fprog,
962 VLOG_ERR("%s: failed to attach filter (%s)",
963 netdev_get_name(netdev_), ovs_strerror(error));
967 ovs_mutex_unlock(&netdev->mutex);
975 ovs_mutex_unlock(&netdev->mutex);
980 netdev_linux_rxq_destruct(struct netdev_rxq *rxq_)
982 struct netdev_rxq_linux *rx = netdev_rxq_linux_cast(rxq_);
990 netdev_linux_rxq_dealloc(struct netdev_rxq *rxq_)
992 struct netdev_rxq_linux *rx = netdev_rxq_linux_cast(rxq_);
998 auxdata_to_vlan_tpid(const struct tpacket_auxdata *aux)
1000 if (aux->tp_status & TP_STATUS_VLAN_TPID_VALID) {
1001 return htons(aux->tp_vlan_tpid);
1003 return htons(ETH_TYPE_VLAN);
1008 auxdata_has_vlan_tci(const struct tpacket_auxdata *aux)
1010 return aux->tp_vlan_tci || aux->tp_status & TP_STATUS_VLAN_VALID;
1014 netdev_linux_rxq_recv_sock(int fd, struct dp_packet *buffer)
1019 struct cmsghdr *cmsg;
1021 struct cmsghdr cmsg;
1022 char buffer[CMSG_SPACE(sizeof(struct tpacket_auxdata))];
1026 /* Reserve headroom for a single VLAN tag */
1027 dp_packet_reserve(buffer, VLAN_HEADER_LEN);
1028 size = dp_packet_tailroom(buffer);
1030 iov.iov_base = dp_packet_data(buffer);
1032 msgh.msg_name = NULL;
1033 msgh.msg_namelen = 0;
1034 msgh.msg_iov = &iov;
1035 msgh.msg_iovlen = 1;
1036 msgh.msg_control = &cmsg_buffer;
1037 msgh.msg_controllen = sizeof cmsg_buffer;
1041 retval = recvmsg(fd, &msgh, MSG_TRUNC);
1042 } while (retval < 0 && errno == EINTR);
1046 } else if (retval > size) {
1050 dp_packet_set_size(buffer, dp_packet_size(buffer) + retval);
1052 for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg; cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
1053 const struct tpacket_auxdata *aux;
1055 if (cmsg->cmsg_level != SOL_PACKET
1056 || cmsg->cmsg_type != PACKET_AUXDATA
1057 || cmsg->cmsg_len < CMSG_LEN(sizeof(struct tpacket_auxdata))) {
1061 aux = ALIGNED_CAST(struct tpacket_auxdata *, CMSG_DATA(cmsg));
1062 if (auxdata_has_vlan_tci(aux)) {
1063 if (retval < ETH_HEADER_LEN) {
1067 eth_push_vlan(buffer, auxdata_to_vlan_tpid(aux),
1068 htons(aux->tp_vlan_tci));
1077 netdev_linux_rxq_recv_tap(int fd, struct dp_packet *buffer)
1080 size_t size = dp_packet_tailroom(buffer);
1083 retval = read(fd, dp_packet_data(buffer), size);
1084 } while (retval < 0 && errno == EINTR);
1090 dp_packet_set_size(buffer, dp_packet_size(buffer) + retval);
1095 netdev_linux_rxq_recv(struct netdev_rxq *rxq_, struct dp_packet_batch *batch)
1097 struct netdev_rxq_linux *rx = netdev_rxq_linux_cast(rxq_);
1098 struct netdev *netdev = rx->up.netdev;
1099 struct dp_packet *buffer;
1103 if (netdev_linux_get_mtu__(netdev_linux_cast(netdev), &mtu)) {
1104 mtu = ETH_PAYLOAD_MAX;
1107 buffer = dp_packet_new_with_headroom(VLAN_ETH_HEADER_LEN + mtu,
1108 DP_NETDEV_HEADROOM);
1109 retval = (rx->is_tap
1110 ? netdev_linux_rxq_recv_tap(rx->fd, buffer)
1111 : netdev_linux_rxq_recv_sock(rx->fd, buffer));
1114 if (retval != EAGAIN && retval != EMSGSIZE) {
1115 VLOG_WARN_RL(&rl, "error receiving Ethernet packet on %s: %s",
1116 netdev_rxq_get_name(rxq_), ovs_strerror(errno));
1118 dp_packet_delete(buffer);
1120 dp_packet_pad(buffer);
1121 batch->packets[0] = buffer;
1129 netdev_linux_rxq_wait(struct netdev_rxq *rxq_)
1131 struct netdev_rxq_linux *rx = netdev_rxq_linux_cast(rxq_);
1132 poll_fd_wait(rx->fd, POLLIN);
1136 netdev_linux_rxq_drain(struct netdev_rxq *rxq_)
1138 struct netdev_rxq_linux *rx = netdev_rxq_linux_cast(rxq_);
1141 int error = af_inet_ifreq_ioctl(netdev_rxq_get_name(rxq_), &ifr,
1142 SIOCGIFTXQLEN, "SIOCGIFTXQLEN");
1146 drain_fd(rx->fd, ifr.ifr_qlen);
1149 return drain_rcvbuf(rx->fd);
1153 /* Sends 'buffer' on 'netdev'. Returns 0 if successful, otherwise a positive
1154 * errno value. Returns EAGAIN without blocking if the packet cannot be queued
1155 * immediately. Returns EMSGSIZE if a partial packet was transmitted or if
1156 * the packet is too big or too small to transmit on the device.
1158 * The caller retains ownership of 'buffer' in all cases.
1160 * The kernel maintains a packet transmission queue, so the caller is not
1161 * expected to do additional queuing of packets. */
1163 netdev_linux_send(struct netdev *netdev_, int qid OVS_UNUSED,
1164 struct dp_packet_batch *batch, bool may_steal)
1169 /* 'i' is incremented only if there's no error */
1170 for (i = 0; i < batch->count;) {
1171 const void *data = dp_packet_data(batch->packets[i]);
1172 size_t size = dp_packet_size(batch->packets[i]);
1175 /* Truncate the packet if it is configured. */
1176 size -= dp_packet_get_cutlen(batch->packets[i]);
1178 if (!is_tap_netdev(netdev_)) {
1179 /* Use our AF_PACKET socket to send to this device. */
1180 struct sockaddr_ll sll;
1186 sock = af_packet_sock();
1191 ifindex = netdev_get_ifindex(netdev_);
1196 /* We don't bother setting most fields in sockaddr_ll because the
1197 * kernel ignores them for SOCK_RAW. */
1198 memset(&sll, 0, sizeof sll);
1199 sll.sll_family = AF_PACKET;
1200 sll.sll_ifindex = ifindex;
1202 iov.iov_base = CONST_CAST(void *, data);
1205 msg.msg_name = &sll;
1206 msg.msg_namelen = sizeof sll;
1209 msg.msg_control = NULL;
1210 msg.msg_controllen = 0;
1213 retval = sendmsg(sock, &msg, 0);
1215 /* Use the tap fd to send to this device. This is essential for
1216 * tap devices, because packets sent to a tap device with an
1217 * AF_PACKET socket will loop back to be *received* again on the
1218 * tap device. This doesn't occur on other interface types
1219 * because we attach a socket filter to the rx socket. */
1220 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
1222 retval = write(netdev->tap_fd, data, size);
1226 if (errno == EINTR) {
1227 /* The send was interrupted by a signal. Retry the packet by
1228 * continuing without incrementing 'i'.*/
1230 } else if (errno == EIO && is_tap_netdev(netdev_)) {
1231 /* The Linux tap driver returns EIO if the device is not up.
1232 * From the OVS side this is not an error, so ignore it. */
1234 /* The Linux AF_PACKET implementation never blocks waiting for
1235 * room for packets, instead returning ENOBUFS. Translate this
1236 * into EAGAIN for the caller. */
1237 error = errno == ENOBUFS ? EAGAIN : errno;
1240 } else if (retval != size) {
1241 VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%"PRIuSIZE" bytes"
1242 " of %"PRIuSIZE") on %s", retval, size,
1243 netdev_get_name(netdev_));
1248 /* Process the next packet in the batch */
1252 dp_packet_delete_batch(batch, may_steal);
1254 if (error && error != EAGAIN) {
1255 VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: %s",
1256 netdev_get_name(netdev_), ovs_strerror(error));
1263 /* Registers with the poll loop to wake up from the next call to poll_block()
1264 * when the packet transmission queue has sufficient room to transmit a packet
1265 * with netdev_send().
1267 * The kernel maintains a packet transmission queue, so the client is not
1268 * expected to do additional queuing of packets. Thus, this function is
1269 * unlikely to ever be used. It is included for completeness. */
1271 netdev_linux_send_wait(struct netdev *netdev, int qid OVS_UNUSED)
1273 if (is_tap_netdev(netdev)) {
1274 /* TAP device always accepts packets.*/
1275 poll_immediate_wake();
1279 /* Attempts to set 'netdev''s MAC address to 'mac'. Returns 0 if successful,
1280 * otherwise a positive errno value. */
1282 netdev_linux_set_etheraddr(struct netdev *netdev_, const struct eth_addr mac)
1284 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
1285 enum netdev_flags old_flags = 0;
1288 ovs_mutex_lock(&netdev->mutex);
1290 if (netdev->cache_valid & VALID_ETHERADDR) {
1291 error = netdev->ether_addr_error;
1292 if (error || eth_addr_equals(netdev->etheraddr, mac)) {
1295 netdev->cache_valid &= ~VALID_ETHERADDR;
1298 /* Tap devices must be brought down before setting the address. */
1299 if (is_tap_netdev(netdev_)) {
1300 update_flags(netdev, NETDEV_UP, 0, &old_flags);
1302 error = set_etheraddr(netdev_get_name(netdev_), mac);
1303 if (!error || error == ENODEV) {
1304 netdev->ether_addr_error = error;
1305 netdev->cache_valid |= VALID_ETHERADDR;
1307 netdev->etheraddr = mac;
1311 if (is_tap_netdev(netdev_) && old_flags & NETDEV_UP) {
1312 update_flags(netdev, 0, NETDEV_UP, &old_flags);
1316 ovs_mutex_unlock(&netdev->mutex);
1320 /* Copies 'netdev''s MAC address to 'mac' which is passed as param. */
1322 netdev_linux_get_etheraddr(const struct netdev *netdev_, struct eth_addr *mac)
1324 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
1327 ovs_mutex_lock(&netdev->mutex);
1328 if (!(netdev->cache_valid & VALID_ETHERADDR)) {
1329 netdev->ether_addr_error = get_etheraddr(netdev_get_name(netdev_),
1330 &netdev->etheraddr);
1331 netdev->cache_valid |= VALID_ETHERADDR;
1334 error = netdev->ether_addr_error;
1336 *mac = netdev->etheraddr;
1338 ovs_mutex_unlock(&netdev->mutex);
1344 netdev_linux_get_mtu__(struct netdev_linux *netdev, int *mtup)
1348 if (!(netdev->cache_valid & VALID_MTU)) {
1351 netdev->netdev_mtu_error = af_inet_ifreq_ioctl(
1352 netdev_get_name(&netdev->up), &ifr, SIOCGIFMTU, "SIOCGIFMTU");
1353 netdev->mtu = ifr.ifr_mtu;
1354 netdev->cache_valid |= VALID_MTU;
1357 error = netdev->netdev_mtu_error;
1359 *mtup = netdev->mtu;
1365 /* Returns the maximum size of transmitted (and received) packets on 'netdev',
1366 * in bytes, not including the hardware header; thus, this is typically 1500
1367 * bytes for Ethernet devices. */
1369 netdev_linux_get_mtu(const struct netdev *netdev_, int *mtup)
1371 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
1374 ovs_mutex_lock(&netdev->mutex);
1375 error = netdev_linux_get_mtu__(netdev, mtup);
1376 ovs_mutex_unlock(&netdev->mutex);
1381 /* Sets the maximum size of transmitted (MTU) for given device using linux
1382 * networking ioctl interface.
1385 netdev_linux_set_mtu(const struct netdev *netdev_, int mtu)
1387 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
1391 ovs_mutex_lock(&netdev->mutex);
1392 if (netdev->cache_valid & VALID_MTU) {
1393 error = netdev->netdev_mtu_error;
1394 if (error || netdev->mtu == mtu) {
1397 netdev->cache_valid &= ~VALID_MTU;
1400 error = af_inet_ifreq_ioctl(netdev_get_name(netdev_), &ifr,
1401 SIOCSIFMTU, "SIOCSIFMTU");
1402 if (!error || error == ENODEV) {
1403 netdev->netdev_mtu_error = error;
1404 netdev->mtu = ifr.ifr_mtu;
1405 netdev->cache_valid |= VALID_MTU;
1408 ovs_mutex_unlock(&netdev->mutex);
1412 /* Returns the ifindex of 'netdev', if successful, as a positive number.
1413 * On failure, returns a negative errno value. */
1415 netdev_linux_get_ifindex(const struct netdev *netdev_)
1417 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
1420 ovs_mutex_lock(&netdev->mutex);
1421 error = get_ifindex(netdev_, &ifindex);
1422 ovs_mutex_unlock(&netdev->mutex);
1424 return error ? -error : ifindex;
1428 netdev_linux_get_carrier(const struct netdev *netdev_, bool *carrier)
1430 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
1432 ovs_mutex_lock(&netdev->mutex);
1433 if (netdev->miimon_interval > 0) {
1434 *carrier = netdev->miimon;
1436 *carrier = (netdev->ifi_flags & IFF_RUNNING) != 0;
1438 ovs_mutex_unlock(&netdev->mutex);
1443 static long long int
1444 netdev_linux_get_carrier_resets(const struct netdev *netdev_)
1446 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
1447 long long int carrier_resets;
1449 ovs_mutex_lock(&netdev->mutex);
1450 carrier_resets = netdev->carrier_resets;
1451 ovs_mutex_unlock(&netdev->mutex);
1453 return carrier_resets;
1457 netdev_linux_do_miimon(const char *name, int cmd, const char *cmd_name,
1458 struct mii_ioctl_data *data)
1463 memset(&ifr, 0, sizeof ifr);
1464 memcpy(&ifr.ifr_data, data, sizeof *data);
1465 error = af_inet_ifreq_ioctl(name, &ifr, cmd, cmd_name);
1466 memcpy(data, &ifr.ifr_data, sizeof *data);
1472 netdev_linux_get_miimon(const char *name, bool *miimon)
1474 struct mii_ioctl_data data;
1479 memset(&data, 0, sizeof data);
1480 error = netdev_linux_do_miimon(name, SIOCGMIIPHY, "SIOCGMIIPHY", &data);
1482 /* data.phy_id is filled out by previous SIOCGMIIPHY miimon call. */
1483 data.reg_num = MII_BMSR;
1484 error = netdev_linux_do_miimon(name, SIOCGMIIREG, "SIOCGMIIREG",
1488 *miimon = !!(data.val_out & BMSR_LSTATUS);
1490 VLOG_WARN_RL(&rl, "%s: failed to query MII", name);
1493 struct ethtool_cmd ecmd;
1495 VLOG_DBG_RL(&rl, "%s: failed to query MII, falling back to ethtool",
1498 COVERAGE_INC(netdev_get_ethtool);
1499 memset(&ecmd, 0, sizeof ecmd);
1500 error = netdev_linux_do_ethtool(name, &ecmd, ETHTOOL_GLINK,
1503 struct ethtool_value eval;
1505 memcpy(&eval, &ecmd, sizeof eval);
1506 *miimon = !!eval.data;
1508 VLOG_WARN_RL(&rl, "%s: ethtool link status failed", name);
1516 netdev_linux_set_miimon_interval(struct netdev *netdev_,
1517 long long int interval)
1519 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
1521 ovs_mutex_lock(&netdev->mutex);
1522 interval = interval > 0 ? MAX(interval, 100) : 0;
1523 if (netdev->miimon_interval != interval) {
1524 if (interval && !netdev->miimon_interval) {
1525 atomic_count_inc(&miimon_cnt);
1526 } else if (!interval && netdev->miimon_interval) {
1527 atomic_count_dec(&miimon_cnt);
1530 netdev->miimon_interval = interval;
1531 timer_set_expired(&netdev->miimon_timer);
1533 ovs_mutex_unlock(&netdev->mutex);
1539 netdev_linux_miimon_run(void)
1541 struct shash device_shash;
1542 struct shash_node *node;
1544 shash_init(&device_shash);
1545 netdev_get_devices(&netdev_linux_class, &device_shash);
1546 SHASH_FOR_EACH (node, &device_shash) {
1547 struct netdev *netdev = node->data;
1548 struct netdev_linux *dev = netdev_linux_cast(netdev);
1551 ovs_mutex_lock(&dev->mutex);
1552 if (dev->miimon_interval > 0 && timer_expired(&dev->miimon_timer)) {
1553 netdev_linux_get_miimon(dev->up.name, &miimon);
1554 if (miimon != dev->miimon) {
1555 dev->miimon = miimon;
1556 netdev_linux_changed(dev, dev->ifi_flags, 0);
1559 timer_set_duration(&dev->miimon_timer, dev->miimon_interval);
1561 ovs_mutex_unlock(&dev->mutex);
1562 netdev_close(netdev);
1565 shash_destroy(&device_shash);
1569 netdev_linux_miimon_wait(void)
1571 struct shash device_shash;
1572 struct shash_node *node;
1574 shash_init(&device_shash);
1575 netdev_get_devices(&netdev_linux_class, &device_shash);
1576 SHASH_FOR_EACH (node, &device_shash) {
1577 struct netdev *netdev = node->data;
1578 struct netdev_linux *dev = netdev_linux_cast(netdev);
1580 ovs_mutex_lock(&dev->mutex);
1581 if (dev->miimon_interval > 0) {
1582 timer_wait(&dev->miimon_timer);
1584 ovs_mutex_unlock(&dev->mutex);
1585 netdev_close(netdev);
1587 shash_destroy(&device_shash);
1591 swap_uint64(uint64_t *a, uint64_t *b)
1598 /* Copies 'src' into 'dst', performing format conversion in the process.
1600 * 'src' is allowed to be misaligned. */
1602 netdev_stats_from_ovs_vport_stats(struct netdev_stats *dst,
1603 const struct ovs_vport_stats *src)
1605 dst->rx_packets = get_32aligned_u64(&src->rx_packets);
1606 dst->tx_packets = get_32aligned_u64(&src->tx_packets);
1607 dst->rx_bytes = get_32aligned_u64(&src->rx_bytes);
1608 dst->tx_bytes = get_32aligned_u64(&src->tx_bytes);
1609 dst->rx_errors = get_32aligned_u64(&src->rx_errors);
1610 dst->tx_errors = get_32aligned_u64(&src->tx_errors);
1611 dst->rx_dropped = get_32aligned_u64(&src->rx_dropped);
1612 dst->tx_dropped = get_32aligned_u64(&src->tx_dropped);
1614 dst->collisions = 0;
1615 dst->rx_length_errors = 0;
1616 dst->rx_over_errors = 0;
1617 dst->rx_crc_errors = 0;
1618 dst->rx_frame_errors = 0;
1619 dst->rx_fifo_errors = 0;
1620 dst->rx_missed_errors = 0;
1621 dst->tx_aborted_errors = 0;
1622 dst->tx_carrier_errors = 0;
1623 dst->tx_fifo_errors = 0;
1624 dst->tx_heartbeat_errors = 0;
1625 dst->tx_window_errors = 0;
1629 get_stats_via_vport__(const struct netdev *netdev, struct netdev_stats *stats)
1631 struct dpif_netlink_vport reply;
1635 error = dpif_netlink_vport_get(netdev_get_name(netdev), &reply, &buf);
1638 } else if (!reply.stats) {
1643 netdev_stats_from_ovs_vport_stats(stats, reply.stats);
1651 get_stats_via_vport(const struct netdev *netdev_,
1652 struct netdev_stats *stats)
1654 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
1656 if (!netdev->vport_stats_error ||
1657 !(netdev->cache_valid & VALID_VPORT_STAT_ERROR)) {
1660 error = get_stats_via_vport__(netdev_, stats);
1661 if (error && error != ENOENT && error != ENODEV) {
1662 VLOG_WARN_RL(&rl, "%s: obtaining netdev stats via vport failed "
1664 netdev_get_name(netdev_), ovs_strerror(error));
1666 netdev->vport_stats_error = error;
1667 netdev->cache_valid |= VALID_VPORT_STAT_ERROR;
1671 /* Retrieves current device stats for 'netdev-linux'. */
1673 netdev_linux_get_stats(const struct netdev *netdev_,
1674 struct netdev_stats *stats)
1676 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
1677 struct netdev_stats dev_stats;
1680 ovs_mutex_lock(&netdev->mutex);
1681 get_stats_via_vport(netdev_, stats);
1682 error = get_stats_via_netlink(netdev_, &dev_stats);
1684 if (!netdev->vport_stats_error) {
1687 } else if (netdev->vport_stats_error) {
1688 /* stats not available from OVS then use netdev stats. */
1691 /* Use kernel netdev's packet and byte counts since vport's counters
1692 * do not reflect packet counts on the wire when GSO, TSO or GRO are
1694 stats->rx_packets = dev_stats.rx_packets;
1695 stats->rx_bytes = dev_stats.rx_bytes;
1696 stats->tx_packets = dev_stats.tx_packets;
1697 stats->tx_bytes = dev_stats.tx_bytes;
1699 stats->rx_errors += dev_stats.rx_errors;
1700 stats->tx_errors += dev_stats.tx_errors;
1701 stats->rx_dropped += dev_stats.rx_dropped;
1702 stats->tx_dropped += dev_stats.tx_dropped;
1703 stats->multicast += dev_stats.multicast;
1704 stats->collisions += dev_stats.collisions;
1705 stats->rx_length_errors += dev_stats.rx_length_errors;
1706 stats->rx_over_errors += dev_stats.rx_over_errors;
1707 stats->rx_crc_errors += dev_stats.rx_crc_errors;
1708 stats->rx_frame_errors += dev_stats.rx_frame_errors;
1709 stats->rx_fifo_errors += dev_stats.rx_fifo_errors;
1710 stats->rx_missed_errors += dev_stats.rx_missed_errors;
1711 stats->tx_aborted_errors += dev_stats.tx_aborted_errors;
1712 stats->tx_carrier_errors += dev_stats.tx_carrier_errors;
1713 stats->tx_fifo_errors += dev_stats.tx_fifo_errors;
1714 stats->tx_heartbeat_errors += dev_stats.tx_heartbeat_errors;
1715 stats->tx_window_errors += dev_stats.tx_window_errors;
1717 ovs_mutex_unlock(&netdev->mutex);
1722 /* Retrieves current device stats for 'netdev-tap' netdev or
1723 * netdev-internal. */
1725 netdev_tap_get_stats(const struct netdev *netdev_, struct netdev_stats *stats)
1727 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
1728 struct netdev_stats dev_stats;
1731 ovs_mutex_lock(&netdev->mutex);
1732 get_stats_via_vport(netdev_, stats);
1733 error = get_stats_via_netlink(netdev_, &dev_stats);
1735 if (!netdev->vport_stats_error) {
1738 } else if (netdev->vport_stats_error) {
1739 /* Transmit and receive stats will appear to be swapped relative to the
1740 * other ports since we are the one sending the data, not a remote
1741 * computer. For consistency, we swap them back here. This does not
1742 * apply if we are getting stats from the vport layer because it always
1743 * tracks stats from the perspective of the switch. */
1746 swap_uint64(&stats->rx_packets, &stats->tx_packets);
1747 swap_uint64(&stats->rx_bytes, &stats->tx_bytes);
1748 swap_uint64(&stats->rx_errors, &stats->tx_errors);
1749 swap_uint64(&stats->rx_dropped, &stats->tx_dropped);
1750 stats->rx_length_errors = 0;
1751 stats->rx_over_errors = 0;
1752 stats->rx_crc_errors = 0;
1753 stats->rx_frame_errors = 0;
1754 stats->rx_fifo_errors = 0;
1755 stats->rx_missed_errors = 0;
1756 stats->tx_aborted_errors = 0;
1757 stats->tx_carrier_errors = 0;
1758 stats->tx_fifo_errors = 0;
1759 stats->tx_heartbeat_errors = 0;
1760 stats->tx_window_errors = 0;
1762 /* Use kernel netdev's packet and byte counts since vport counters
1763 * do not reflect packet counts on the wire when GSO, TSO or GRO
1765 stats->rx_packets = dev_stats.tx_packets;
1766 stats->rx_bytes = dev_stats.tx_bytes;
1767 stats->tx_packets = dev_stats.rx_packets;
1768 stats->tx_bytes = dev_stats.rx_bytes;
1770 stats->rx_dropped += dev_stats.tx_dropped;
1771 stats->tx_dropped += dev_stats.rx_dropped;
1773 stats->rx_errors += dev_stats.tx_errors;
1774 stats->tx_errors += dev_stats.rx_errors;
1776 stats->multicast += dev_stats.multicast;
1777 stats->collisions += dev_stats.collisions;
1779 ovs_mutex_unlock(&netdev->mutex);
1785 netdev_internal_get_stats(const struct netdev *netdev_,
1786 struct netdev_stats *stats)
1788 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
1791 ovs_mutex_lock(&netdev->mutex);
1792 get_stats_via_vport(netdev_, stats);
1793 error = netdev->vport_stats_error;
1794 ovs_mutex_unlock(&netdev->mutex);
1800 netdev_linux_read_features(struct netdev_linux *netdev)
1802 struct ethtool_cmd ecmd;
1806 if (netdev->cache_valid & VALID_FEATURES) {
1810 COVERAGE_INC(netdev_get_ethtool);
1811 memset(&ecmd, 0, sizeof ecmd);
1812 error = netdev_linux_do_ethtool(netdev->up.name, &ecmd,
1813 ETHTOOL_GSET, "ETHTOOL_GSET");
1818 /* Supported features. */
1819 netdev->supported = 0;
1820 if (ecmd.supported & SUPPORTED_10baseT_Half) {
1821 netdev->supported |= NETDEV_F_10MB_HD;
1823 if (ecmd.supported & SUPPORTED_10baseT_Full) {
1824 netdev->supported |= NETDEV_F_10MB_FD;
1826 if (ecmd.supported & SUPPORTED_100baseT_Half) {
1827 netdev->supported |= NETDEV_F_100MB_HD;
1829 if (ecmd.supported & SUPPORTED_100baseT_Full) {
1830 netdev->supported |= NETDEV_F_100MB_FD;
1832 if (ecmd.supported & SUPPORTED_1000baseT_Half) {
1833 netdev->supported |= NETDEV_F_1GB_HD;
1835 if ((ecmd.supported & SUPPORTED_1000baseT_Full) ||
1836 (ecmd.supported & SUPPORTED_1000baseKX_Full)) {
1837 netdev->supported |= NETDEV_F_1GB_FD;
1839 if ((ecmd.supported & SUPPORTED_10000baseT_Full) ||
1840 (ecmd.supported & SUPPORTED_10000baseKX4_Full) ||
1841 (ecmd.supported & SUPPORTED_10000baseKR_Full) ||
1842 (ecmd.supported & SUPPORTED_10000baseR_FEC)) {
1843 netdev->supported |= NETDEV_F_10GB_FD;
1845 if ((ecmd.supported & SUPPORTED_40000baseKR4_Full) ||
1846 (ecmd.supported & SUPPORTED_40000baseCR4_Full) ||
1847 (ecmd.supported & SUPPORTED_40000baseSR4_Full) ||
1848 (ecmd.supported & SUPPORTED_40000baseLR4_Full)) {
1849 netdev->supported |= NETDEV_F_40GB_FD;
1851 if (ecmd.supported & SUPPORTED_TP) {
1852 netdev->supported |= NETDEV_F_COPPER;
1854 if (ecmd.supported & SUPPORTED_FIBRE) {
1855 netdev->supported |= NETDEV_F_FIBER;
1857 if (ecmd.supported & SUPPORTED_Autoneg) {
1858 netdev->supported |= NETDEV_F_AUTONEG;
1860 if (ecmd.supported & SUPPORTED_Pause) {
1861 netdev->supported |= NETDEV_F_PAUSE;
1863 if (ecmd.supported & SUPPORTED_Asym_Pause) {
1864 netdev->supported |= NETDEV_F_PAUSE_ASYM;
1867 /* Advertised features. */
1868 netdev->advertised = 0;
1869 if (ecmd.advertising & ADVERTISED_10baseT_Half) {
1870 netdev->advertised |= NETDEV_F_10MB_HD;
1872 if (ecmd.advertising & ADVERTISED_10baseT_Full) {
1873 netdev->advertised |= NETDEV_F_10MB_FD;
1875 if (ecmd.advertising & ADVERTISED_100baseT_Half) {
1876 netdev->advertised |= NETDEV_F_100MB_HD;
1878 if (ecmd.advertising & ADVERTISED_100baseT_Full) {
1879 netdev->advertised |= NETDEV_F_100MB_FD;
1881 if (ecmd.advertising & ADVERTISED_1000baseT_Half) {
1882 netdev->advertised |= NETDEV_F_1GB_HD;
1884 if ((ecmd.advertising & ADVERTISED_1000baseT_Full) ||
1885 (ecmd.advertising & ADVERTISED_1000baseKX_Full)) {
1886 netdev->advertised |= NETDEV_F_1GB_FD;
1888 if ((ecmd.advertising & ADVERTISED_10000baseT_Full) ||
1889 (ecmd.advertising & ADVERTISED_10000baseKX4_Full) ||
1890 (ecmd.advertising & ADVERTISED_10000baseKR_Full) ||
1891 (ecmd.advertising & ADVERTISED_10000baseR_FEC)) {
1892 netdev->advertised |= NETDEV_F_10GB_FD;
1894 if ((ecmd.advertising & ADVERTISED_40000baseKR4_Full) ||
1895 (ecmd.advertising & ADVERTISED_40000baseCR4_Full) ||
1896 (ecmd.advertising & ADVERTISED_40000baseSR4_Full) ||
1897 (ecmd.advertising & ADVERTISED_40000baseLR4_Full)) {
1898 netdev->advertised |= NETDEV_F_40GB_FD;
1900 if (ecmd.advertising & ADVERTISED_TP) {
1901 netdev->advertised |= NETDEV_F_COPPER;
1903 if (ecmd.advertising & ADVERTISED_FIBRE) {
1904 netdev->advertised |= NETDEV_F_FIBER;
1906 if (ecmd.advertising & ADVERTISED_Autoneg) {
1907 netdev->advertised |= NETDEV_F_AUTONEG;
1909 if (ecmd.advertising & ADVERTISED_Pause) {
1910 netdev->advertised |= NETDEV_F_PAUSE;
1912 if (ecmd.advertising & ADVERTISED_Asym_Pause) {
1913 netdev->advertised |= NETDEV_F_PAUSE_ASYM;
1916 /* Current settings. */
1917 speed = ethtool_cmd_speed(&ecmd);
1918 if (speed == SPEED_10) {
1919 netdev->current = ecmd.duplex ? NETDEV_F_10MB_FD : NETDEV_F_10MB_HD;
1920 } else if (speed == SPEED_100) {
1921 netdev->current = ecmd.duplex ? NETDEV_F_100MB_FD : NETDEV_F_100MB_HD;
1922 } else if (speed == SPEED_1000) {
1923 netdev->current = ecmd.duplex ? NETDEV_F_1GB_FD : NETDEV_F_1GB_HD;
1924 } else if (speed == SPEED_10000) {
1925 netdev->current = NETDEV_F_10GB_FD;
1926 } else if (speed == 40000) {
1927 netdev->current = NETDEV_F_40GB_FD;
1928 } else if (speed == 100000) {
1929 netdev->current = NETDEV_F_100GB_FD;
1930 } else if (speed == 1000000) {
1931 netdev->current = NETDEV_F_1TB_FD;
1933 netdev->current = 0;
1936 if (ecmd.port == PORT_TP) {
1937 netdev->current |= NETDEV_F_COPPER;
1938 } else if (ecmd.port == PORT_FIBRE) {
1939 netdev->current |= NETDEV_F_FIBER;
1943 netdev->current |= NETDEV_F_AUTONEG;
1947 netdev->cache_valid |= VALID_FEATURES;
1948 netdev->get_features_error = error;
1951 /* Stores the features supported by 'netdev' into of '*current', '*advertised',
1952 * '*supported', and '*peer'. Each value is a bitmap of NETDEV_* bits.
1953 * Returns 0 if successful, otherwise a positive errno value. */
1955 netdev_linux_get_features(const struct netdev *netdev_,
1956 enum netdev_features *current,
1957 enum netdev_features *advertised,
1958 enum netdev_features *supported,
1959 enum netdev_features *peer)
1961 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
1964 ovs_mutex_lock(&netdev->mutex);
1965 netdev_linux_read_features(netdev);
1966 if (!netdev->get_features_error) {
1967 *current = netdev->current;
1968 *advertised = netdev->advertised;
1969 *supported = netdev->supported;
1970 *peer = 0; /* XXX */
1972 error = netdev->get_features_error;
1973 ovs_mutex_unlock(&netdev->mutex);
1978 /* Set the features advertised by 'netdev' to 'advertise'. */
1980 netdev_linux_set_advertisements(struct netdev *netdev_,
1981 enum netdev_features advertise)
1983 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
1984 struct ethtool_cmd ecmd;
1987 ovs_mutex_lock(&netdev->mutex);
1989 COVERAGE_INC(netdev_get_ethtool);
1990 memset(&ecmd, 0, sizeof ecmd);
1991 error = netdev_linux_do_ethtool(netdev_get_name(netdev_), &ecmd,
1992 ETHTOOL_GSET, "ETHTOOL_GSET");
1997 ecmd.advertising = 0;
1998 if (advertise & NETDEV_F_10MB_HD) {
1999 ecmd.advertising |= ADVERTISED_10baseT_Half;
2001 if (advertise & NETDEV_F_10MB_FD) {
2002 ecmd.advertising |= ADVERTISED_10baseT_Full;
2004 if (advertise & NETDEV_F_100MB_HD) {
2005 ecmd.advertising |= ADVERTISED_100baseT_Half;
2007 if (advertise & NETDEV_F_100MB_FD) {
2008 ecmd.advertising |= ADVERTISED_100baseT_Full;
2010 if (advertise & NETDEV_F_1GB_HD) {
2011 ecmd.advertising |= ADVERTISED_1000baseT_Half;
2013 if (advertise & NETDEV_F_1GB_FD) {
2014 ecmd.advertising |= ADVERTISED_1000baseT_Full;
2016 if (advertise & NETDEV_F_10GB_FD) {
2017 ecmd.advertising |= ADVERTISED_10000baseT_Full;
2019 if (advertise & NETDEV_F_COPPER) {
2020 ecmd.advertising |= ADVERTISED_TP;
2022 if (advertise & NETDEV_F_FIBER) {
2023 ecmd.advertising |= ADVERTISED_FIBRE;
2025 if (advertise & NETDEV_F_AUTONEG) {
2026 ecmd.advertising |= ADVERTISED_Autoneg;
2028 if (advertise & NETDEV_F_PAUSE) {
2029 ecmd.advertising |= ADVERTISED_Pause;
2031 if (advertise & NETDEV_F_PAUSE_ASYM) {
2032 ecmd.advertising |= ADVERTISED_Asym_Pause;
2034 COVERAGE_INC(netdev_set_ethtool);
2035 error = netdev_linux_do_ethtool(netdev_get_name(netdev_), &ecmd,
2036 ETHTOOL_SSET, "ETHTOOL_SSET");
2039 ovs_mutex_unlock(&netdev->mutex);
2043 /* Attempts to set input rate limiting (policing) policy. Returns 0 if
2044 * successful, otherwise a positive errno value. */
2046 netdev_linux_set_policing(struct netdev *netdev_,
2047 uint32_t kbits_rate, uint32_t kbits_burst)
2049 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
2050 const char *netdev_name = netdev_get_name(netdev_);
2053 kbits_burst = (!kbits_rate ? 0 /* Force to 0 if no rate specified. */
2054 : !kbits_burst ? 8000 /* Default to 8000 kbits if 0. */
2055 : kbits_burst); /* Stick with user-specified value. */
2057 ovs_mutex_lock(&netdev->mutex);
2058 if (netdev->cache_valid & VALID_POLICING) {
2059 error = netdev->netdev_policing_error;
2060 if (error || (netdev->kbits_rate == kbits_rate &&
2061 netdev->kbits_burst == kbits_burst)) {
2062 /* Assume that settings haven't changed since we last set them. */
2065 netdev->cache_valid &= ~VALID_POLICING;
2068 COVERAGE_INC(netdev_set_policing);
2069 /* Remove any existing ingress qdisc. */
2070 error = tc_add_del_ingress_qdisc(netdev_, false);
2072 VLOG_WARN_RL(&rl, "%s: removing policing failed: %s",
2073 netdev_name, ovs_strerror(error));
2078 error = tc_add_del_ingress_qdisc(netdev_, true);
2080 VLOG_WARN_RL(&rl, "%s: adding policing qdisc failed: %s",
2081 netdev_name, ovs_strerror(error));
2085 error = tc_add_policer(netdev_, kbits_rate, kbits_burst);
2087 VLOG_WARN_RL(&rl, "%s: adding policing action failed: %s",
2088 netdev_name, ovs_strerror(error));
2093 netdev->kbits_rate = kbits_rate;
2094 netdev->kbits_burst = kbits_burst;
2097 if (!error || error == ENODEV) {
2098 netdev->netdev_policing_error = error;
2099 netdev->cache_valid |= VALID_POLICING;
2101 ovs_mutex_unlock(&netdev->mutex);
2106 netdev_linux_get_qos_types(const struct netdev *netdev OVS_UNUSED,
2109 const struct tc_ops *const *opsp;
2110 for (opsp = tcs; *opsp != NULL; opsp++) {
2111 const struct tc_ops *ops = *opsp;
2112 if (ops->tc_install && ops->ovs_name[0] != '\0') {
2113 sset_add(types, ops->ovs_name);
2119 static const struct tc_ops *
2120 tc_lookup_ovs_name(const char *name)
2122 const struct tc_ops *const *opsp;
2124 for (opsp = tcs; *opsp != NULL; opsp++) {
2125 const struct tc_ops *ops = *opsp;
2126 if (!strcmp(name, ops->ovs_name)) {
2133 static const struct tc_ops *
2134 tc_lookup_linux_name(const char *name)
2136 const struct tc_ops *const *opsp;
2138 for (opsp = tcs; *opsp != NULL; opsp++) {
2139 const struct tc_ops *ops = *opsp;
2140 if (ops->linux_name && !strcmp(name, ops->linux_name)) {
2147 static struct tc_queue *
2148 tc_find_queue__(const struct netdev *netdev_, unsigned int queue_id,
2151 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
2152 struct tc_queue *queue;
2154 HMAP_FOR_EACH_IN_BUCKET (queue, hmap_node, hash, &netdev->tc->queues) {
2155 if (queue->queue_id == queue_id) {
2162 static struct tc_queue *
2163 tc_find_queue(const struct netdev *netdev, unsigned int queue_id)
2165 return tc_find_queue__(netdev, queue_id, hash_int(queue_id, 0));
2169 netdev_linux_get_qos_capabilities(const struct netdev *netdev OVS_UNUSED,
2171 struct netdev_qos_capabilities *caps)
2173 const struct tc_ops *ops = tc_lookup_ovs_name(type);
2177 caps->n_queues = ops->n_queues;
2182 netdev_linux_get_qos(const struct netdev *netdev_,
2183 const char **typep, struct smap *details)
2185 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
2188 ovs_mutex_lock(&netdev->mutex);
2189 error = tc_query_qdisc(netdev_);
2191 *typep = netdev->tc->ops->ovs_name;
2192 error = (netdev->tc->ops->qdisc_get
2193 ? netdev->tc->ops->qdisc_get(netdev_, details)
2196 ovs_mutex_unlock(&netdev->mutex);
2202 netdev_linux_set_qos(struct netdev *netdev_,
2203 const char *type, const struct smap *details)
2205 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
2206 const struct tc_ops *new_ops;
2209 new_ops = tc_lookup_ovs_name(type);
2210 if (!new_ops || !new_ops->tc_install) {
2214 if (new_ops == &tc_ops_noop) {
2215 return new_ops->tc_install(netdev_, details);
2218 ovs_mutex_lock(&netdev->mutex);
2219 error = tc_query_qdisc(netdev_);
2224 if (new_ops == netdev->tc->ops) {
2225 error = new_ops->qdisc_set ? new_ops->qdisc_set(netdev_, details) : 0;
2227 /* Delete existing qdisc. */
2228 error = tc_del_qdisc(netdev_);
2232 ovs_assert(netdev->tc == NULL);
2234 /* Install new qdisc. */
2235 error = new_ops->tc_install(netdev_, details);
2236 ovs_assert((error == 0) == (netdev->tc != NULL));
2240 ovs_mutex_unlock(&netdev->mutex);
2245 netdev_linux_get_queue(const struct netdev *netdev_,
2246 unsigned int queue_id, struct smap *details)
2248 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
2251 ovs_mutex_lock(&netdev->mutex);
2252 error = tc_query_qdisc(netdev_);
2254 struct tc_queue *queue = tc_find_queue(netdev_, queue_id);
2256 ? netdev->tc->ops->class_get(netdev_, queue, details)
2259 ovs_mutex_unlock(&netdev->mutex);
2265 netdev_linux_set_queue(struct netdev *netdev_,
2266 unsigned int queue_id, const struct smap *details)
2268 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
2271 ovs_mutex_lock(&netdev->mutex);
2272 error = tc_query_qdisc(netdev_);
2274 error = (queue_id < netdev->tc->ops->n_queues
2275 && netdev->tc->ops->class_set
2276 ? netdev->tc->ops->class_set(netdev_, queue_id, details)
2279 ovs_mutex_unlock(&netdev->mutex);
2285 netdev_linux_delete_queue(struct netdev *netdev_, unsigned int queue_id)
2287 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
2290 ovs_mutex_lock(&netdev->mutex);
2291 error = tc_query_qdisc(netdev_);
2293 if (netdev->tc->ops->class_delete) {
2294 struct tc_queue *queue = tc_find_queue(netdev_, queue_id);
2296 ? netdev->tc->ops->class_delete(netdev_, queue)
2302 ovs_mutex_unlock(&netdev->mutex);
2308 netdev_linux_get_queue_stats(const struct netdev *netdev_,
2309 unsigned int queue_id,
2310 struct netdev_queue_stats *stats)
2312 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
2315 ovs_mutex_lock(&netdev->mutex);
2316 error = tc_query_qdisc(netdev_);
2318 if (netdev->tc->ops->class_get_stats) {
2319 const struct tc_queue *queue = tc_find_queue(netdev_, queue_id);
2321 stats->created = queue->created;
2322 error = netdev->tc->ops->class_get_stats(netdev_, queue,
2331 ovs_mutex_unlock(&netdev->mutex);
2336 struct queue_dump_state {
2337 struct nl_dump dump;
2342 start_queue_dump(const struct netdev *netdev, struct queue_dump_state *state)
2344 struct ofpbuf request;
2345 struct tcmsg *tcmsg;
2347 tcmsg = tc_make_request(netdev, RTM_GETTCLASS, 0, &request);
2351 tcmsg->tcm_parent = 0;
2352 nl_dump_start(&state->dump, NETLINK_ROUTE, &request);
2353 ofpbuf_uninit(&request);
2355 ofpbuf_init(&state->buf, NL_DUMP_BUFSIZE);
2360 finish_queue_dump(struct queue_dump_state *state)
2362 ofpbuf_uninit(&state->buf);
2363 return nl_dump_done(&state->dump);
2366 struct netdev_linux_queue_state {
2367 unsigned int *queues;
2373 netdev_linux_queue_dump_start(const struct netdev *netdev_, void **statep)
2375 const struct netdev_linux *netdev = netdev_linux_cast(netdev_);
2378 ovs_mutex_lock(&netdev->mutex);
2379 error = tc_query_qdisc(netdev_);
2381 if (netdev->tc->ops->class_get) {
2382 struct netdev_linux_queue_state *state;
2383 struct tc_queue *queue;
2386 *statep = state = xmalloc(sizeof *state);
2387 state->n_queues = hmap_count(&netdev->tc->queues);
2388 state->cur_queue = 0;
2389 state->queues = xmalloc(state->n_queues * sizeof *state->queues);
2392 HMAP_FOR_EACH (queue, hmap_node, &netdev->tc->queues) {
2393 state->queues[i++] = queue->queue_id;
2399 ovs_mutex_unlock(&netdev->mutex);
2405 netdev_linux_queue_dump_next(const struct netdev *netdev_, void *state_,
2406 unsigned int *queue_idp, struct smap *details)
2408 const struct netdev_linux *netdev = netdev_linux_cast(netdev_);
2409 struct netdev_linux_queue_state *state = state_;
2412 ovs_mutex_lock(&netdev->mutex);
2413 while (state->cur_queue < state->n_queues) {
2414 unsigned int queue_id = state->queues[state->cur_queue++];
2415 struct tc_queue *queue = tc_find_queue(netdev_, queue_id);
2418 *queue_idp = queue_id;
2419 error = netdev->tc->ops->class_get(netdev_, queue, details);
2423 ovs_mutex_unlock(&netdev->mutex);
2429 netdev_linux_queue_dump_done(const struct netdev *netdev OVS_UNUSED,
2432 struct netdev_linux_queue_state *state = state_;
2434 free(state->queues);
2440 netdev_linux_dump_queue_stats(const struct netdev *netdev_,
2441 netdev_dump_queue_stats_cb *cb, void *aux)
2443 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
2446 ovs_mutex_lock(&netdev->mutex);
2447 error = tc_query_qdisc(netdev_);
2449 struct queue_dump_state state;
2451 if (!netdev->tc->ops->class_dump_stats) {
2453 } else if (!start_queue_dump(netdev_, &state)) {
2459 while (nl_dump_next(&state.dump, &msg, &state.buf)) {
2460 retval = netdev->tc->ops->class_dump_stats(netdev_, &msg,
2467 retval = finish_queue_dump(&state);
2473 ovs_mutex_unlock(&netdev->mutex);
2479 netdev_linux_set_in4(struct netdev *netdev_, struct in_addr address,
2480 struct in_addr netmask)
2482 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
2485 ovs_mutex_lock(&netdev->mutex);
2486 error = do_set_addr(netdev_, SIOCSIFADDR, "SIOCSIFADDR", address);
2488 if (address.s_addr != INADDR_ANY) {
2489 error = do_set_addr(netdev_, SIOCSIFNETMASK,
2490 "SIOCSIFNETMASK", netmask);
2494 ovs_mutex_unlock(&netdev->mutex);
2499 /* If 'netdev' has an assigned IPv6 address, sets '*in6' to that address.
2500 * Otherwise, sets '*in6' to 'in6addr_any' and returns the corresponding
2503 netdev_linux_get_addr_list(const struct netdev *netdev_,
2504 struct in6_addr **addr, struct in6_addr **mask, int *n_cnt)
2506 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
2509 ovs_mutex_lock(&netdev->mutex);
2510 error = netdev_get_addrs(netdev_get_name(netdev_), addr, mask, n_cnt);
2511 ovs_mutex_unlock(&netdev->mutex);
2517 make_in4_sockaddr(struct sockaddr *sa, struct in_addr addr)
2519 struct sockaddr_in sin;
2520 memset(&sin, 0, sizeof sin);
2521 sin.sin_family = AF_INET;
2522 sin.sin_addr = addr;
2525 memset(sa, 0, sizeof *sa);
2526 memcpy(sa, &sin, sizeof sin);
2530 do_set_addr(struct netdev *netdev,
2531 int ioctl_nr, const char *ioctl_name, struct in_addr addr)
2535 make_in4_sockaddr(&ifr.ifr_addr, addr);
2536 return af_inet_ifreq_ioctl(netdev_get_name(netdev), &ifr, ioctl_nr,
2540 /* Adds 'router' as a default IP gateway. */
2542 netdev_linux_add_router(struct netdev *netdev OVS_UNUSED, struct in_addr router)
2544 struct in_addr any = { INADDR_ANY };
2548 memset(&rt, 0, sizeof rt);
2549 make_in4_sockaddr(&rt.rt_dst, any);
2550 make_in4_sockaddr(&rt.rt_gateway, router);
2551 make_in4_sockaddr(&rt.rt_genmask, any);
2552 rt.rt_flags = RTF_UP | RTF_GATEWAY;
2553 error = af_inet_ioctl(SIOCADDRT, &rt);
2555 VLOG_WARN("ioctl(SIOCADDRT): %s", ovs_strerror(error));
2561 netdev_linux_get_next_hop(const struct in_addr *host, struct in_addr *next_hop,
2564 static const char fn[] = "/proc/net/route";
2569 *netdev_name = NULL;
2570 stream = fopen(fn, "r");
2571 if (stream == NULL) {
2572 VLOG_WARN_RL(&rl, "%s: open failed: %s", fn, ovs_strerror(errno));
2577 while (fgets(line, sizeof line, stream)) {
2580 ovs_be32 dest, gateway, mask;
2581 int refcnt, metric, mtu;
2582 unsigned int flags, use, window, irtt;
2585 "%16s %"SCNx32" %"SCNx32" %04X %d %u %d %"SCNx32
2587 iface, &dest, &gateway, &flags, &refcnt,
2588 &use, &metric, &mask, &mtu, &window, &irtt)) {
2589 VLOG_WARN_RL(&rl, "%s: could not parse line %d: %s",
2593 if (!(flags & RTF_UP)) {
2594 /* Skip routes that aren't up. */
2598 /* The output of 'dest', 'mask', and 'gateway' were given in
2599 * network byte order, so we don't need need any endian
2600 * conversions here. */
2601 if ((dest & mask) == (host->s_addr & mask)) {
2603 /* The host is directly reachable. */
2604 next_hop->s_addr = 0;
2606 /* To reach the host, we must go through a gateway. */
2607 next_hop->s_addr = gateway;
2609 *netdev_name = xstrdup(iface);
2621 netdev_linux_get_status(const struct netdev *netdev_, struct smap *smap)
2623 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
2626 ovs_mutex_lock(&netdev->mutex);
2627 if (!(netdev->cache_valid & VALID_DRVINFO)) {
2628 struct ethtool_cmd *cmd = (struct ethtool_cmd *) &netdev->drvinfo;
2630 COVERAGE_INC(netdev_get_ethtool);
2631 memset(&netdev->drvinfo, 0, sizeof netdev->drvinfo);
2632 error = netdev_linux_do_ethtool(netdev->up.name,
2635 "ETHTOOL_GDRVINFO");
2637 netdev->cache_valid |= VALID_DRVINFO;
2642 smap_add(smap, "driver_name", netdev->drvinfo.driver);
2643 smap_add(smap, "driver_version", netdev->drvinfo.version);
2644 smap_add(smap, "firmware_version", netdev->drvinfo.fw_version);
2646 ovs_mutex_unlock(&netdev->mutex);
2652 netdev_internal_get_status(const struct netdev *netdev OVS_UNUSED,
2655 smap_add(smap, "driver_name", "openvswitch");
2659 /* Looks up the ARP table entry for 'ip' on 'netdev'. If one exists and can be
2660 * successfully retrieved, it stores the corresponding MAC address in 'mac' and
2661 * returns 0. Otherwise, it returns a positive errno value; in particular,
2662 * ENXIO indicates that there is not ARP table entry for 'ip' on 'netdev'. */
2664 netdev_linux_arp_lookup(const struct netdev *netdev,
2665 ovs_be32 ip, struct eth_addr *mac)
2668 struct sockaddr_in sin;
2671 memset(&r, 0, sizeof r);
2672 memset(&sin, 0, sizeof sin);
2673 sin.sin_family = AF_INET;
2674 sin.sin_addr.s_addr = ip;
2676 memcpy(&r.arp_pa, &sin, sizeof sin);
2677 r.arp_ha.sa_family = ARPHRD_ETHER;
2679 ovs_strzcpy(r.arp_dev, netdev_get_name(netdev), sizeof r.arp_dev);
2680 COVERAGE_INC(netdev_arp_lookup);
2681 retval = af_inet_ioctl(SIOCGARP, &r);
2683 memcpy(mac, r.arp_ha.sa_data, ETH_ADDR_LEN);
2684 } else if (retval != ENXIO) {
2685 VLOG_WARN_RL(&rl, "%s: could not look up ARP entry for "IP_FMT": %s",
2686 netdev_get_name(netdev), IP_ARGS(ip),
2687 ovs_strerror(retval));
2693 nd_to_iff_flags(enum netdev_flags nd)
2696 if (nd & NETDEV_UP) {
2699 if (nd & NETDEV_PROMISC) {
2702 if (nd & NETDEV_LOOPBACK) {
2703 iff |= IFF_LOOPBACK;
2709 iff_to_nd_flags(int iff)
2711 enum netdev_flags nd = 0;
2715 if (iff & IFF_PROMISC) {
2716 nd |= NETDEV_PROMISC;
2718 if (iff & IFF_LOOPBACK) {
2719 nd |= NETDEV_LOOPBACK;
2725 update_flags(struct netdev_linux *netdev, enum netdev_flags off,
2726 enum netdev_flags on, enum netdev_flags *old_flagsp)
2727 OVS_REQUIRES(netdev->mutex)
2729 int old_flags, new_flags;
2732 old_flags = netdev->ifi_flags;
2733 *old_flagsp = iff_to_nd_flags(old_flags);
2734 new_flags = (old_flags & ~nd_to_iff_flags(off)) | nd_to_iff_flags(on);
2735 if (new_flags != old_flags) {
2736 error = set_flags(netdev_get_name(&netdev->up), new_flags);
2737 get_flags(&netdev->up, &netdev->ifi_flags);
2744 netdev_linux_update_flags(struct netdev *netdev_, enum netdev_flags off,
2745 enum netdev_flags on, enum netdev_flags *old_flagsp)
2747 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
2750 ovs_mutex_lock(&netdev->mutex);
2751 error = update_flags(netdev, off, on, old_flagsp);
2752 ovs_mutex_unlock(&netdev->mutex);
2757 #define NETDEV_LINUX_CLASS(NAME, CONSTRUCT, GET_STATS, \
2758 GET_FEATURES, GET_STATUS) \
2761 false, /* is_pmd */ \
2765 netdev_linux_wait, \
2767 netdev_linux_alloc, \
2769 netdev_linux_destruct, \
2770 netdev_linux_dealloc, \
2771 NULL, /* get_config */ \
2772 NULL, /* set_config */ \
2773 NULL, /* get_tunnel_config */ \
2774 NULL, /* build header */ \
2775 NULL, /* push header */ \
2776 NULL, /* pop header */ \
2777 NULL, /* get_numa_id */ \
2778 NULL, /* set_tx_multiq */ \
2780 netdev_linux_send, \
2781 netdev_linux_send_wait, \
2783 netdev_linux_set_etheraddr, \
2784 netdev_linux_get_etheraddr, \
2785 netdev_linux_get_mtu, \
2786 netdev_linux_set_mtu, \
2787 netdev_linux_get_ifindex, \
2788 netdev_linux_get_carrier, \
2789 netdev_linux_get_carrier_resets, \
2790 netdev_linux_set_miimon_interval, \
2794 netdev_linux_set_advertisements, \
2796 netdev_linux_set_policing, \
2797 netdev_linux_get_qos_types, \
2798 netdev_linux_get_qos_capabilities, \
2799 netdev_linux_get_qos, \
2800 netdev_linux_set_qos, \
2801 netdev_linux_get_queue, \
2802 netdev_linux_set_queue, \
2803 netdev_linux_delete_queue, \
2804 netdev_linux_get_queue_stats, \
2805 netdev_linux_queue_dump_start, \
2806 netdev_linux_queue_dump_next, \
2807 netdev_linux_queue_dump_done, \
2808 netdev_linux_dump_queue_stats, \
2810 netdev_linux_set_in4, \
2811 netdev_linux_get_addr_list, \
2812 netdev_linux_add_router, \
2813 netdev_linux_get_next_hop, \
2815 netdev_linux_arp_lookup, \
2817 netdev_linux_update_flags, \
2818 NULL, /* reconfigure */ \
2820 netdev_linux_rxq_alloc, \
2821 netdev_linux_rxq_construct, \
2822 netdev_linux_rxq_destruct, \
2823 netdev_linux_rxq_dealloc, \
2824 netdev_linux_rxq_recv, \
2825 netdev_linux_rxq_wait, \
2826 netdev_linux_rxq_drain, \
2829 const struct netdev_class netdev_linux_class =
2832 netdev_linux_construct,
2833 netdev_linux_get_stats,
2834 netdev_linux_get_features,
2835 netdev_linux_get_status);
2837 const struct netdev_class netdev_tap_class =
2840 netdev_linux_construct_tap,
2841 netdev_tap_get_stats,
2842 netdev_linux_get_features,
2843 netdev_linux_get_status);
2845 const struct netdev_class netdev_internal_class =
2848 netdev_linux_construct,
2849 netdev_internal_get_stats,
2850 NULL, /* get_features */
2851 netdev_internal_get_status);
2854 #define CODEL_N_QUEUES 0x0000
2856 /* In sufficiently new kernel headers these are defined as enums in
2857 * <linux/pkt_sched.h>. Define them here as macros to help out with older
2858 * kernels. (This overrides any enum definition in the header file but that's
2860 #define TCA_CODEL_TARGET 1
2861 #define TCA_CODEL_LIMIT 2
2862 #define TCA_CODEL_INTERVAL 3
2871 static struct codel *
2872 codel_get__(const struct netdev *netdev_)
2874 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
2875 return CONTAINER_OF(netdev->tc, struct codel, tc);
2879 codel_install__(struct netdev *netdev_, uint32_t target, uint32_t limit,
2882 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
2883 struct codel *codel;
2885 codel = xmalloc(sizeof *codel);
2886 tc_init(&codel->tc, &tc_ops_codel);
2887 codel->target = target;
2888 codel->limit = limit;
2889 codel->interval = interval;
2891 netdev->tc = &codel->tc;
2895 codel_setup_qdisc__(struct netdev *netdev, uint32_t target, uint32_t limit,
2899 struct ofpbuf request;
2900 struct tcmsg *tcmsg;
2901 uint32_t otarget, olimit, ointerval;
2904 tc_del_qdisc(netdev);
2906 tcmsg = tc_make_request(netdev, RTM_NEWQDISC,
2907 NLM_F_EXCL | NLM_F_CREATE, &request);
2911 tcmsg->tcm_handle = tc_make_handle(1, 0);
2912 tcmsg->tcm_parent = TC_H_ROOT;
2914 otarget = target ? target : 5000;
2915 olimit = limit ? limit : 10240;
2916 ointerval = interval ? interval : 100000;
2918 nl_msg_put_string(&request, TCA_KIND, "codel");
2919 opt_offset = nl_msg_start_nested(&request, TCA_OPTIONS);
2920 nl_msg_put_u32(&request, TCA_CODEL_TARGET, otarget);
2921 nl_msg_put_u32(&request, TCA_CODEL_LIMIT, olimit);
2922 nl_msg_put_u32(&request, TCA_CODEL_INTERVAL, ointerval);
2923 nl_msg_end_nested(&request, opt_offset);
2925 error = tc_transact(&request, NULL);
2927 VLOG_WARN_RL(&rl, "failed to replace %s qdisc, "
2928 "target %u, limit %u, interval %u error %d(%s)",
2929 netdev_get_name(netdev),
2930 otarget, olimit, ointerval,
2931 error, ovs_strerror(error));
2937 codel_parse_qdisc_details__(struct netdev *netdev OVS_UNUSED,
2938 const struct smap *details, struct codel *codel)
2940 const char *target_s;
2941 const char *limit_s;
2942 const char *interval_s;
2944 target_s = smap_get(details, "target");
2945 limit_s = smap_get(details, "limit");
2946 interval_s = smap_get(details, "interval");
2948 codel->target = target_s ? strtoull(target_s, NULL, 10) : 0;
2949 codel->limit = limit_s ? strtoull(limit_s, NULL, 10) : 0;
2950 codel->interval = interval_s ? strtoull(interval_s, NULL, 10) : 0;
2952 if (!codel->target) {
2953 codel->target = 5000;
2955 if (!codel->limit) {
2956 codel->limit = 10240;
2958 if (!codel->interval) {
2959 codel->interval = 100000;
2964 codel_tc_install(struct netdev *netdev, const struct smap *details)
2969 codel_parse_qdisc_details__(netdev, details, &codel);
2970 error = codel_setup_qdisc__(netdev, codel.target, codel.limit,
2973 codel_install__(netdev, codel.target, codel.limit, codel.interval);
2979 codel_parse_tca_options__(struct nlattr *nl_options, struct codel *codel)
2981 static const struct nl_policy tca_codel_policy[] = {
2982 [TCA_CODEL_TARGET] = { .type = NL_A_U32 },
2983 [TCA_CODEL_LIMIT] = { .type = NL_A_U32 },
2984 [TCA_CODEL_INTERVAL] = { .type = NL_A_U32 }
2987 struct nlattr *attrs[ARRAY_SIZE(tca_codel_policy)];
2989 if (!nl_parse_nested(nl_options, tca_codel_policy,
2990 attrs, ARRAY_SIZE(tca_codel_policy))) {
2991 VLOG_WARN_RL(&rl, "failed to parse CoDel class options");
2995 codel->target = nl_attr_get_u32(attrs[TCA_CODEL_TARGET]);
2996 codel->limit = nl_attr_get_u32(attrs[TCA_CODEL_LIMIT]);
2997 codel->interval = nl_attr_get_u32(attrs[TCA_CODEL_INTERVAL]);
3002 codel_tc_load(struct netdev *netdev, struct ofpbuf *nlmsg)
3004 struct nlattr *nlattr;
3009 error = tc_parse_qdisc(nlmsg, &kind, &nlattr);
3014 error = codel_parse_tca_options__(nlattr, &codel);
3019 codel_install__(netdev, codel.target, codel.limit, codel.interval);
3025 codel_tc_destroy(struct tc *tc)
3027 struct codel *codel = CONTAINER_OF(tc, struct codel, tc);
3033 codel_qdisc_get(const struct netdev *netdev, struct smap *details)
3035 const struct codel *codel = codel_get__(netdev);
3036 smap_add_format(details, "target", "%u", codel->target);
3037 smap_add_format(details, "limit", "%u", codel->limit);
3038 smap_add_format(details, "interval", "%u", codel->interval);
3043 codel_qdisc_set(struct netdev *netdev, const struct smap *details)
3047 codel_parse_qdisc_details__(netdev, details, &codel);
3048 codel_install__(netdev, codel.target, codel.limit, codel.interval);
3049 codel_get__(netdev)->target = codel.target;
3050 codel_get__(netdev)->limit = codel.limit;
3051 codel_get__(netdev)->interval = codel.interval;
3055 static const struct tc_ops tc_ops_codel = {
3056 "codel", /* linux_name */
3057 "linux-codel", /* ovs_name */
3058 CODEL_N_QUEUES, /* n_queues */
3071 /* FQ-CoDel traffic control class. */
3073 #define FQCODEL_N_QUEUES 0x0000
3075 /* In sufficiently new kernel headers these are defined as enums in
3076 * <linux/pkt_sched.h>. Define them here as macros to help out with older
3077 * kernels. (This overrides any enum definition in the header file but that's
3079 #define TCA_FQ_CODEL_TARGET 1
3080 #define TCA_FQ_CODEL_LIMIT 2
3081 #define TCA_FQ_CODEL_INTERVAL 3
3082 #define TCA_FQ_CODEL_ECN 4
3083 #define TCA_FQ_CODEL_FLOWS 5
3084 #define TCA_FQ_CODEL_QUANTUM 6
3095 static struct fqcodel *
3096 fqcodel_get__(const struct netdev *netdev_)
3098 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
3099 return CONTAINER_OF(netdev->tc, struct fqcodel, tc);
3103 fqcodel_install__(struct netdev *netdev_, uint32_t target, uint32_t limit,
3104 uint32_t interval, uint32_t flows, uint32_t quantum)
3106 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
3107 struct fqcodel *fqcodel;
3109 fqcodel = xmalloc(sizeof *fqcodel);
3110 tc_init(&fqcodel->tc, &tc_ops_fqcodel);
3111 fqcodel->target = target;
3112 fqcodel->limit = limit;
3113 fqcodel->interval = interval;
3114 fqcodel->flows = flows;
3115 fqcodel->quantum = quantum;
3117 netdev->tc = &fqcodel->tc;
3121 fqcodel_setup_qdisc__(struct netdev *netdev, uint32_t target, uint32_t limit,
3122 uint32_t interval, uint32_t flows, uint32_t quantum)
3125 struct ofpbuf request;
3126 struct tcmsg *tcmsg;
3127 uint32_t otarget, olimit, ointerval, oflows, oquantum;
3130 tc_del_qdisc(netdev);
3132 tcmsg = tc_make_request(netdev, RTM_NEWQDISC,
3133 NLM_F_EXCL | NLM_F_CREATE, &request);
3137 tcmsg->tcm_handle = tc_make_handle(1, 0);
3138 tcmsg->tcm_parent = TC_H_ROOT;
3140 otarget = target ? target : 5000;
3141 olimit = limit ? limit : 10240;
3142 ointerval = interval ? interval : 100000;
3143 oflows = flows ? flows : 1024;
3144 oquantum = quantum ? quantum : 1514; /* fq_codel default quantum is 1514
3147 nl_msg_put_string(&request, TCA_KIND, "fq_codel");
3148 opt_offset = nl_msg_start_nested(&request, TCA_OPTIONS);
3149 nl_msg_put_u32(&request, TCA_FQ_CODEL_TARGET, otarget);
3150 nl_msg_put_u32(&request, TCA_FQ_CODEL_LIMIT, olimit);
3151 nl_msg_put_u32(&request, TCA_FQ_CODEL_INTERVAL, ointerval);
3152 nl_msg_put_u32(&request, TCA_FQ_CODEL_FLOWS, oflows);
3153 nl_msg_put_u32(&request, TCA_FQ_CODEL_QUANTUM, oquantum);
3154 nl_msg_end_nested(&request, opt_offset);
3156 error = tc_transact(&request, NULL);
3158 VLOG_WARN_RL(&rl, "failed to replace %s qdisc, "
3159 "target %u, limit %u, interval %u, flows %u, quantum %u error %d(%s)",
3160 netdev_get_name(netdev),
3161 otarget, olimit, ointerval, oflows, oquantum,
3162 error, ovs_strerror(error));
3168 fqcodel_parse_qdisc_details__(struct netdev *netdev OVS_UNUSED,
3169 const struct smap *details, struct fqcodel *fqcodel)
3171 const char *target_s;
3172 const char *limit_s;
3173 const char *interval_s;
3174 const char *flows_s;
3175 const char *quantum_s;
3177 target_s = smap_get(details, "target");
3178 limit_s = smap_get(details, "limit");
3179 interval_s = smap_get(details, "interval");
3180 flows_s = smap_get(details, "flows");
3181 quantum_s = smap_get(details, "quantum");
3182 fqcodel->target = target_s ? strtoull(target_s, NULL, 10) : 0;
3183 fqcodel->limit = limit_s ? strtoull(limit_s, NULL, 10) : 0;
3184 fqcodel->interval = interval_s ? strtoull(interval_s, NULL, 10) : 0;
3185 fqcodel->flows = flows_s ? strtoull(flows_s, NULL, 10) : 0;
3186 fqcodel->quantum = quantum_s ? strtoull(quantum_s, NULL, 10) : 0;
3187 if (!fqcodel->target) {
3188 fqcodel->target = 5000;
3190 if (!fqcodel->limit) {
3191 fqcodel->limit = 10240;
3193 if (!fqcodel->interval) {
3194 fqcodel->interval = 1000000;
3196 if (!fqcodel->flows) {
3197 fqcodel->flows = 1024;
3199 if (!fqcodel->quantum) {
3200 fqcodel->quantum = 1514;
3205 fqcodel_tc_install(struct netdev *netdev, const struct smap *details)
3208 struct fqcodel fqcodel;
3210 fqcodel_parse_qdisc_details__(netdev, details, &fqcodel);
3211 error = fqcodel_setup_qdisc__(netdev, fqcodel.target, fqcodel.limit,
3212 fqcodel.interval, fqcodel.flows,
3215 fqcodel_install__(netdev, fqcodel.target, fqcodel.limit,
3216 fqcodel.interval, fqcodel.flows, fqcodel.quantum);
3222 fqcodel_parse_tca_options__(struct nlattr *nl_options, struct fqcodel *fqcodel)
3224 static const struct nl_policy tca_fqcodel_policy[] = {
3225 [TCA_FQ_CODEL_TARGET] = { .type = NL_A_U32 },
3226 [TCA_FQ_CODEL_LIMIT] = { .type = NL_A_U32 },
3227 [TCA_FQ_CODEL_INTERVAL] = { .type = NL_A_U32 },
3228 [TCA_FQ_CODEL_FLOWS] = { .type = NL_A_U32 },
3229 [TCA_FQ_CODEL_QUANTUM] = { .type = NL_A_U32 }
3232 struct nlattr *attrs[ARRAY_SIZE(tca_fqcodel_policy)];
3234 if (!nl_parse_nested(nl_options, tca_fqcodel_policy,
3235 attrs, ARRAY_SIZE(tca_fqcodel_policy))) {
3236 VLOG_WARN_RL(&rl, "failed to parse FQ_CoDel class options");
3240 fqcodel->target = nl_attr_get_u32(attrs[TCA_FQ_CODEL_TARGET]);
3241 fqcodel->limit = nl_attr_get_u32(attrs[TCA_FQ_CODEL_LIMIT]);
3242 fqcodel->interval =nl_attr_get_u32(attrs[TCA_FQ_CODEL_INTERVAL]);
3243 fqcodel->flows = nl_attr_get_u32(attrs[TCA_FQ_CODEL_FLOWS]);
3244 fqcodel->quantum = nl_attr_get_u32(attrs[TCA_FQ_CODEL_QUANTUM]);
3249 fqcodel_tc_load(struct netdev *netdev, struct ofpbuf *nlmsg)
3251 struct nlattr *nlattr;
3254 struct fqcodel fqcodel;
3256 error = tc_parse_qdisc(nlmsg, &kind, &nlattr);
3261 error = fqcodel_parse_tca_options__(nlattr, &fqcodel);
3266 fqcodel_install__(netdev, fqcodel.target, fqcodel.limit, fqcodel.interval,
3267 fqcodel.flows, fqcodel.quantum);
3272 fqcodel_tc_destroy(struct tc *tc)
3274 struct fqcodel *fqcodel = CONTAINER_OF(tc, struct fqcodel, tc);
3280 fqcodel_qdisc_get(const struct netdev *netdev, struct smap *details)
3282 const struct fqcodel *fqcodel = fqcodel_get__(netdev);
3283 smap_add_format(details, "target", "%u", fqcodel->target);
3284 smap_add_format(details, "limit", "%u", fqcodel->limit);
3285 smap_add_format(details, "interval", "%u", fqcodel->interval);
3286 smap_add_format(details, "flows", "%u", fqcodel->flows);
3287 smap_add_format(details, "quantum", "%u", fqcodel->quantum);
3292 fqcodel_qdisc_set(struct netdev *netdev, const struct smap *details)
3294 struct fqcodel fqcodel;
3296 fqcodel_parse_qdisc_details__(netdev, details, &fqcodel);
3297 fqcodel_install__(netdev, fqcodel.target, fqcodel.limit, fqcodel.interval,
3298 fqcodel.flows, fqcodel.quantum);
3299 fqcodel_get__(netdev)->target = fqcodel.target;
3300 fqcodel_get__(netdev)->limit = fqcodel.limit;
3301 fqcodel_get__(netdev)->interval = fqcodel.interval;
3302 fqcodel_get__(netdev)->flows = fqcodel.flows;
3303 fqcodel_get__(netdev)->quantum = fqcodel.quantum;
3307 static const struct tc_ops tc_ops_fqcodel = {
3308 "fq_codel", /* linux_name */
3309 "linux-fq_codel", /* ovs_name */
3310 FQCODEL_N_QUEUES, /* n_queues */
3323 /* SFQ traffic control class. */
3325 #define SFQ_N_QUEUES 0x0000
3334 sfq_get__(const struct netdev *netdev_)
3336 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
3337 return CONTAINER_OF(netdev->tc, struct sfq, tc);
3341 sfq_install__(struct netdev *netdev_, uint32_t quantum, uint32_t perturb)
3343 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
3346 sfq = xmalloc(sizeof *sfq);
3347 tc_init(&sfq->tc, &tc_ops_sfq);
3348 sfq->perturb = perturb;
3349 sfq->quantum = quantum;
3351 netdev->tc = &sfq->tc;
3355 sfq_setup_qdisc__(struct netdev *netdev, uint32_t quantum, uint32_t perturb)
3357 struct tc_sfq_qopt opt;
3358 struct ofpbuf request;
3359 struct tcmsg *tcmsg;
3361 int mtu_error, error;
3362 mtu_error = netdev_linux_get_mtu__(netdev_linux_cast(netdev), &mtu);
3364 tc_del_qdisc(netdev);
3366 tcmsg = tc_make_request(netdev, RTM_NEWQDISC,
3367 NLM_F_EXCL | NLM_F_CREATE, &request);
3371 tcmsg->tcm_handle = tc_make_handle(1, 0);
3372 tcmsg->tcm_parent = TC_H_ROOT;
3374 memset(&opt, 0, sizeof opt);
3377 opt.quantum = mtu; /* if we cannot find mtu, use default */
3380 opt.quantum = quantum;
3384 opt.perturb_period = 10;
3386 opt.perturb_period = perturb;
3389 nl_msg_put_string(&request, TCA_KIND, "sfq");
3390 nl_msg_put_unspec(&request, TCA_OPTIONS, &opt, sizeof opt);
3392 error = tc_transact(&request, NULL);
3394 VLOG_WARN_RL(&rl, "failed to replace %s qdisc, "
3395 "quantum %u, perturb %u error %d(%s)",
3396 netdev_get_name(netdev),
3397 opt.quantum, opt.perturb_period,
3398 error, ovs_strerror(error));
3404 sfq_parse_qdisc_details__(struct netdev *netdev,
3405 const struct smap *details, struct sfq *sfq)
3407 const char *perturb_s;
3408 const char *quantum_s;
3412 perturb_s = smap_get(details, "perturb");
3413 quantum_s = smap_get(details, "quantum");
3414 sfq->perturb = perturb_s ? strtoull(perturb_s, NULL, 10) : 0;
3415 sfq->quantum = quantum_s ? strtoull(quantum_s, NULL, 10) : 0;
3416 if (!sfq->perturb) {
3420 if (!sfq->quantum) {
3421 mtu_error = netdev_linux_get_mtu__(netdev_linux_cast(netdev), &mtu);
3425 VLOG_WARN_RL(&rl, "when using SFQ, you must specify quantum on a "
3426 "device without mtu");
3433 sfq_tc_install(struct netdev *netdev, const struct smap *details)
3438 sfq_parse_qdisc_details__(netdev, details, &sfq);
3439 error = sfq_setup_qdisc__(netdev, sfq.quantum, sfq.perturb);
3441 sfq_install__(netdev, sfq.quantum, sfq.perturb);
3447 sfq_tc_load(struct netdev *netdev, struct ofpbuf *nlmsg)
3449 const struct tc_sfq_qopt *sfq;
3450 struct nlattr *nlattr;
3454 error = tc_parse_qdisc(nlmsg, &kind, &nlattr);
3456 sfq = nl_attr_get(nlattr);
3457 sfq_install__(netdev, sfq->perturb_period, sfq->quantum);
3465 sfq_tc_destroy(struct tc *tc)
3467 struct sfq *sfq = CONTAINER_OF(tc, struct sfq, tc);
3473 sfq_qdisc_get(const struct netdev *netdev, struct smap *details)
3475 const struct sfq *sfq = sfq_get__(netdev);
3476 smap_add_format(details, "quantum", "%u", sfq->quantum);
3477 smap_add_format(details, "perturb", "%u", sfq->perturb);
3482 sfq_qdisc_set(struct netdev *netdev, const struct smap *details)
3486 sfq_parse_qdisc_details__(netdev, details, &sfq);
3487 sfq_install__(netdev, sfq.quantum, sfq.perturb);
3488 sfq_get__(netdev)->quantum = sfq.quantum;
3489 sfq_get__(netdev)->perturb = sfq.perturb;
3493 static const struct tc_ops tc_ops_sfq = {
3494 "sfq", /* linux_name */
3495 "linux-sfq", /* ovs_name */
3496 SFQ_N_QUEUES, /* n_queues */
3509 /* HTB traffic control class. */
3511 #define HTB_N_QUEUES 0xf000
3512 #define HTB_RATE2QUANTUM 10
3516 unsigned int max_rate; /* In bytes/s. */
3520 struct tc_queue tc_queue;
3521 unsigned int min_rate; /* In bytes/s. */
3522 unsigned int max_rate; /* In bytes/s. */
3523 unsigned int burst; /* In bytes. */
3524 unsigned int priority; /* Lower values are higher priorities. */
3528 htb_get__(const struct netdev *netdev_)
3530 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
3531 return CONTAINER_OF(netdev->tc, struct htb, tc);
3535 htb_install__(struct netdev *netdev_, uint64_t max_rate)
3537 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
3540 htb = xmalloc(sizeof *htb);
3541 tc_init(&htb->tc, &tc_ops_htb);
3542 htb->max_rate = max_rate;
3544 netdev->tc = &htb->tc;
3547 /* Create an HTB qdisc.
3549 * Equivalent to "tc qdisc add dev <dev> root handle 1: htb default 1". */
3551 htb_setup_qdisc__(struct netdev *netdev)
3554 struct tc_htb_glob opt;
3555 struct ofpbuf request;
3556 struct tcmsg *tcmsg;
3558 tc_del_qdisc(netdev);
3560 tcmsg = tc_make_request(netdev, RTM_NEWQDISC,
3561 NLM_F_EXCL | NLM_F_CREATE, &request);
3565 tcmsg->tcm_handle = tc_make_handle(1, 0);
3566 tcmsg->tcm_parent = TC_H_ROOT;
3568 nl_msg_put_string(&request, TCA_KIND, "htb");
3570 memset(&opt, 0, sizeof opt);
3571 opt.rate2quantum = HTB_RATE2QUANTUM;
3575 opt_offset = nl_msg_start_nested(&request, TCA_OPTIONS);
3576 nl_msg_put_unspec(&request, TCA_HTB_INIT, &opt, sizeof opt);
3577 nl_msg_end_nested(&request, opt_offset);
3579 return tc_transact(&request, NULL);
3582 /* Equivalent to "tc class replace <dev> classid <handle> parent <parent> htb
3583 * rate <min_rate>bps ceil <max_rate>bps burst <burst>b prio <priority>". */
3585 htb_setup_class__(struct netdev *netdev, unsigned int handle,
3586 unsigned int parent, struct htb_class *class)
3589 struct tc_htb_opt opt;
3590 struct ofpbuf request;
3591 struct tcmsg *tcmsg;
3595 error = netdev_linux_get_mtu__(netdev_linux_cast(netdev), &mtu);
3597 VLOG_WARN_RL(&rl, "cannot set up HTB on device %s that lacks MTU",
3598 netdev_get_name(netdev));
3602 memset(&opt, 0, sizeof opt);
3603 tc_fill_rate(&opt.rate, class->min_rate, mtu);
3604 tc_fill_rate(&opt.ceil, class->max_rate, mtu);
3605 /* Makes sure the quantum is at least MTU. Setting quantum will
3606 * make htb ignore the r2q for this class. */
3607 if ((class->min_rate / HTB_RATE2QUANTUM) < mtu) {
3610 opt.buffer = tc_calc_buffer(opt.rate.rate, mtu, class->burst);
3611 opt.cbuffer = tc_calc_buffer(opt.ceil.rate, mtu, class->burst);
3612 opt.prio = class->priority;
3614 tcmsg = tc_make_request(netdev, RTM_NEWTCLASS, NLM_F_CREATE, &request);
3618 tcmsg->tcm_handle = handle;
3619 tcmsg->tcm_parent = parent;
3621 nl_msg_put_string(&request, TCA_KIND, "htb");
3622 opt_offset = nl_msg_start_nested(&request, TCA_OPTIONS);
3623 nl_msg_put_unspec(&request, TCA_HTB_PARMS, &opt, sizeof opt);
3624 tc_put_rtab(&request, TCA_HTB_RTAB, &opt.rate);
3625 tc_put_rtab(&request, TCA_HTB_CTAB, &opt.ceil);
3626 nl_msg_end_nested(&request, opt_offset);
3628 error = tc_transact(&request, NULL);
3630 VLOG_WARN_RL(&rl, "failed to replace %s class %u:%u, parent %u:%u, "
3631 "min_rate=%u max_rate=%u burst=%u prio=%u (%s)",
3632 netdev_get_name(netdev),
3633 tc_get_major(handle), tc_get_minor(handle),
3634 tc_get_major(parent), tc_get_minor(parent),
3635 class->min_rate, class->max_rate,
3636 class->burst, class->priority, ovs_strerror(error));
3641 /* Parses Netlink attributes in 'options' for HTB parameters and stores a
3642 * description of them into 'details'. The description complies with the
3643 * specification given in the vswitch database documentation for linux-htb
3646 htb_parse_tca_options__(struct nlattr *nl_options, struct htb_class *class)
3648 static const struct nl_policy tca_htb_policy[] = {
3649 [TCA_HTB_PARMS] = { .type = NL_A_UNSPEC, .optional = false,
3650 .min_len = sizeof(struct tc_htb_opt) },
3653 struct nlattr *attrs[ARRAY_SIZE(tca_htb_policy)];
3654 const struct tc_htb_opt *htb;
3656 if (!nl_parse_nested(nl_options, tca_htb_policy,
3657 attrs, ARRAY_SIZE(tca_htb_policy))) {
3658 VLOG_WARN_RL(&rl, "failed to parse HTB class options");
3662 htb = nl_attr_get(attrs[TCA_HTB_PARMS]);
3663 class->min_rate = htb->rate.rate;
3664 class->max_rate = htb->ceil.rate;
3665 class->burst = tc_ticks_to_bytes(htb->rate.rate, htb->buffer);
3666 class->priority = htb->prio;
3671 htb_parse_tcmsg__(struct ofpbuf *tcmsg, unsigned int *queue_id,
3672 struct htb_class *options,
3673 struct netdev_queue_stats *stats)
3675 struct nlattr *nl_options;
3676 unsigned int handle;
3679 error = tc_parse_class(tcmsg, &handle, &nl_options, stats);
3680 if (!error && queue_id) {
3681 unsigned int major = tc_get_major(handle);
3682 unsigned int minor = tc_get_minor(handle);
3683 if (major == 1 && minor > 0 && minor <= HTB_N_QUEUES) {
3684 *queue_id = minor - 1;
3689 if (!error && options) {
3690 error = htb_parse_tca_options__(nl_options, options);
3696 htb_parse_qdisc_details__(struct netdev *netdev_,
3697 const struct smap *details, struct htb_class *hc)
3699 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
3700 const char *max_rate_s;
3702 max_rate_s = smap_get(details, "max-rate");
3703 hc->max_rate = max_rate_s ? strtoull(max_rate_s, NULL, 10) / 8 : 0;
3704 if (!hc->max_rate) {
3705 enum netdev_features current;
3707 netdev_linux_read_features(netdev);
3708 current = !netdev->get_features_error ? netdev->current : 0;
3709 hc->max_rate = netdev_features_to_bps(current, 100 * 1000 * 1000) / 8;
3711 hc->min_rate = hc->max_rate;
3717 htb_parse_class_details__(struct netdev *netdev,
3718 const struct smap *details, struct htb_class *hc)
3720 const struct htb *htb = htb_get__(netdev);
3721 const char *min_rate_s = smap_get(details, "min-rate");
3722 const char *max_rate_s = smap_get(details, "max-rate");
3723 const char *burst_s = smap_get(details, "burst");
3724 const char *priority_s = smap_get(details, "priority");
3727 error = netdev_linux_get_mtu__(netdev_linux_cast(netdev), &mtu);
3729 VLOG_WARN_RL(&rl, "cannot parse HTB class on device %s that lacks MTU",
3730 netdev_get_name(netdev));
3734 /* HTB requires at least an mtu sized min-rate to send any traffic even
3735 * on uncongested links. */
3736 hc->min_rate = min_rate_s ? strtoull(min_rate_s, NULL, 10) / 8 : 0;
3737 hc->min_rate = MAX(hc->min_rate, mtu);
3738 hc->min_rate = MIN(hc->min_rate, htb->max_rate);
3741 hc->max_rate = (max_rate_s
3742 ? strtoull(max_rate_s, NULL, 10) / 8
3744 hc->max_rate = MAX(hc->max_rate, hc->min_rate);
3745 hc->max_rate = MIN(hc->max_rate, htb->max_rate);
3749 * According to hints in the documentation that I've read, it is important
3750 * that 'burst' be at least as big as the largest frame that might be
3751 * transmitted. Also, making 'burst' a bit bigger than necessary is OK,
3752 * but having it a bit too small is a problem. Since netdev_get_mtu()
3753 * doesn't include the Ethernet header, we need to add at least 14 (18?) to
3754 * the MTU. We actually add 64, instead of 14, as a guard against
3755 * additional headers get tacked on somewhere that we're not aware of. */
3756 hc->burst = burst_s ? strtoull(burst_s, NULL, 10) / 8 : 0;
3757 hc->burst = MAX(hc->burst, mtu + 64);
3760 hc->priority = priority_s ? strtoul(priority_s, NULL, 10) : 0;
3766 htb_query_class__(const struct netdev *netdev, unsigned int handle,
3767 unsigned int parent, struct htb_class *options,
3768 struct netdev_queue_stats *stats)
3770 struct ofpbuf *reply;
3773 error = tc_query_class(netdev, handle, parent, &reply);
3775 error = htb_parse_tcmsg__(reply, NULL, options, stats);
3776 ofpbuf_delete(reply);
3782 htb_tc_install(struct netdev *netdev, const struct smap *details)
3786 error = htb_setup_qdisc__(netdev);
3788 struct htb_class hc;
3790 htb_parse_qdisc_details__(netdev, details, &hc);
3791 error = htb_setup_class__(netdev, tc_make_handle(1, 0xfffe),
3792 tc_make_handle(1, 0), &hc);
3794 htb_install__(netdev, hc.max_rate);
3800 static struct htb_class *
3801 htb_class_cast__(const struct tc_queue *queue)
3803 return CONTAINER_OF(queue, struct htb_class, tc_queue);
3807 htb_update_queue__(struct netdev *netdev, unsigned int queue_id,
3808 const struct htb_class *hc)
3810 struct htb *htb = htb_get__(netdev);
3811 size_t hash = hash_int(queue_id, 0);
3812 struct tc_queue *queue;
3813 struct htb_class *hcp;
3815 queue = tc_find_queue__(netdev, queue_id, hash);
3817 hcp = htb_class_cast__(queue);
3819 hcp = xmalloc(sizeof *hcp);
3820 queue = &hcp->tc_queue;
3821 queue->queue_id = queue_id;
3822 queue->created = time_msec();
3823 hmap_insert(&htb->tc.queues, &queue->hmap_node, hash);
3826 hcp->min_rate = hc->min_rate;
3827 hcp->max_rate = hc->max_rate;
3828 hcp->burst = hc->burst;
3829 hcp->priority = hc->priority;
3833 htb_tc_load(struct netdev *netdev, struct ofpbuf *nlmsg OVS_UNUSED)
3836 struct queue_dump_state state;
3837 struct htb_class hc;
3839 /* Get qdisc options. */
3841 htb_query_class__(netdev, tc_make_handle(1, 0xfffe), 0, &hc, NULL);
3842 htb_install__(netdev, hc.max_rate);
3845 if (!start_queue_dump(netdev, &state)) {
3848 while (nl_dump_next(&state.dump, &msg, &state.buf)) {
3849 unsigned int queue_id;
3851 if (!htb_parse_tcmsg__(&msg, &queue_id, &hc, NULL)) {
3852 htb_update_queue__(netdev, queue_id, &hc);
3855 finish_queue_dump(&state);
3861 htb_tc_destroy(struct tc *tc)
3863 struct htb *htb = CONTAINER_OF(tc, struct htb, tc);
3864 struct htb_class *hc;
3866 HMAP_FOR_EACH_POP (hc, tc_queue.hmap_node, &htb->tc.queues) {
3874 htb_qdisc_get(const struct netdev *netdev, struct smap *details)
3876 const struct htb *htb = htb_get__(netdev);
3877 smap_add_format(details, "max-rate", "%llu", 8ULL * htb->max_rate);
3882 htb_qdisc_set(struct netdev *netdev, const struct smap *details)
3884 struct htb_class hc;
3887 htb_parse_qdisc_details__(netdev, details, &hc);
3888 error = htb_setup_class__(netdev, tc_make_handle(1, 0xfffe),
3889 tc_make_handle(1, 0), &hc);
3891 htb_get__(netdev)->max_rate = hc.max_rate;
3897 htb_class_get(const struct netdev *netdev OVS_UNUSED,
3898 const struct tc_queue *queue, struct smap *details)
3900 const struct htb_class *hc = htb_class_cast__(queue);
3902 smap_add_format(details, "min-rate", "%llu", 8ULL * hc->min_rate);
3903 if (hc->min_rate != hc->max_rate) {
3904 smap_add_format(details, "max-rate", "%llu", 8ULL * hc->max_rate);
3906 smap_add_format(details, "burst", "%llu", 8ULL * hc->burst);
3908 smap_add_format(details, "priority", "%u", hc->priority);
3914 htb_class_set(struct netdev *netdev, unsigned int queue_id,
3915 const struct smap *details)
3917 struct htb_class hc;
3920 error = htb_parse_class_details__(netdev, details, &hc);
3925 error = htb_setup_class__(netdev, tc_make_handle(1, queue_id + 1),
3926 tc_make_handle(1, 0xfffe), &hc);
3931 htb_update_queue__(netdev, queue_id, &hc);
3936 htb_class_delete(struct netdev *netdev, struct tc_queue *queue)
3938 struct htb_class *hc = htb_class_cast__(queue);
3939 struct htb *htb = htb_get__(netdev);
3942 error = tc_delete_class(netdev, tc_make_handle(1, queue->queue_id + 1));
3944 hmap_remove(&htb->tc.queues, &hc->tc_queue.hmap_node);
3951 htb_class_get_stats(const struct netdev *netdev, const struct tc_queue *queue,
3952 struct netdev_queue_stats *stats)
3954 return htb_query_class__(netdev, tc_make_handle(1, queue->queue_id + 1),
3955 tc_make_handle(1, 0xfffe), NULL, stats);
3959 htb_class_dump_stats(const struct netdev *netdev OVS_UNUSED,
3960 const struct ofpbuf *nlmsg,
3961 netdev_dump_queue_stats_cb *cb, void *aux)
3963 struct netdev_queue_stats stats;
3964 unsigned int handle, major, minor;
3967 error = tc_parse_class(nlmsg, &handle, NULL, &stats);
3972 major = tc_get_major(handle);
3973 minor = tc_get_minor(handle);
3974 if (major == 1 && minor > 0 && minor <= HTB_N_QUEUES) {
3975 (*cb)(minor - 1, &stats, aux);
3980 static const struct tc_ops tc_ops_htb = {
3981 "htb", /* linux_name */
3982 "linux-htb", /* ovs_name */
3983 HTB_N_QUEUES, /* n_queues */
3992 htb_class_get_stats,
3993 htb_class_dump_stats
3996 /* "linux-hfsc" traffic control class. */
3998 #define HFSC_N_QUEUES 0xf000
4006 struct tc_queue tc_queue;
4011 static struct hfsc *
4012 hfsc_get__(const struct netdev *netdev_)
4014 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
4015 return CONTAINER_OF(netdev->tc, struct hfsc, tc);
4018 static struct hfsc_class *
4019 hfsc_class_cast__(const struct tc_queue *queue)
4021 return CONTAINER_OF(queue, struct hfsc_class, tc_queue);
4025 hfsc_install__(struct netdev *netdev_, uint32_t max_rate)
4027 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
4030 hfsc = xmalloc(sizeof *hfsc);
4031 tc_init(&hfsc->tc, &tc_ops_hfsc);
4032 hfsc->max_rate = max_rate;
4033 netdev->tc = &hfsc->tc;
4037 hfsc_update_queue__(struct netdev *netdev, unsigned int queue_id,
4038 const struct hfsc_class *hc)
4042 struct hfsc_class *hcp;
4043 struct tc_queue *queue;
4045 hfsc = hfsc_get__(netdev);
4046 hash = hash_int(queue_id, 0);
4048 queue = tc_find_queue__(netdev, queue_id, hash);
4050 hcp = hfsc_class_cast__(queue);
4052 hcp = xmalloc(sizeof *hcp);
4053 queue = &hcp->tc_queue;
4054 queue->queue_id = queue_id;
4055 queue->created = time_msec();
4056 hmap_insert(&hfsc->tc.queues, &queue->hmap_node, hash);
4059 hcp->min_rate = hc->min_rate;
4060 hcp->max_rate = hc->max_rate;
4064 hfsc_parse_tca_options__(struct nlattr *nl_options, struct hfsc_class *class)
4066 const struct tc_service_curve *rsc, *fsc, *usc;
4067 static const struct nl_policy tca_hfsc_policy[] = {
4069 .type = NL_A_UNSPEC,
4071 .min_len = sizeof(struct tc_service_curve),
4074 .type = NL_A_UNSPEC,
4076 .min_len = sizeof(struct tc_service_curve),
4079 .type = NL_A_UNSPEC,
4081 .min_len = sizeof(struct tc_service_curve),
4084 struct nlattr *attrs[ARRAY_SIZE(tca_hfsc_policy)];
4086 if (!nl_parse_nested(nl_options, tca_hfsc_policy,
4087 attrs, ARRAY_SIZE(tca_hfsc_policy))) {
4088 VLOG_WARN_RL(&rl, "failed to parse HFSC class options");
4092 rsc = nl_attr_get(attrs[TCA_HFSC_RSC]);
4093 fsc = nl_attr_get(attrs[TCA_HFSC_FSC]);
4094 usc = nl_attr_get(attrs[TCA_HFSC_USC]);
4096 if (rsc->m1 != 0 || rsc->d != 0 ||
4097 fsc->m1 != 0 || fsc->d != 0 ||
4098 usc->m1 != 0 || usc->d != 0) {
4099 VLOG_WARN_RL(&rl, "failed to parse HFSC class options. "
4100 "Non-linear service curves are not supported.");
4104 if (rsc->m2 != fsc->m2) {
4105 VLOG_WARN_RL(&rl, "failed to parse HFSC class options. "
4106 "Real-time service curves are not supported ");
4110 if (rsc->m2 > usc->m2) {
4111 VLOG_WARN_RL(&rl, "failed to parse HFSC class options. "
4112 "Min-rate service curve is greater than "
4113 "the max-rate service curve.");
4117 class->min_rate = fsc->m2;
4118 class->max_rate = usc->m2;
4123 hfsc_parse_tcmsg__(struct ofpbuf *tcmsg, unsigned int *queue_id,
4124 struct hfsc_class *options,
4125 struct netdev_queue_stats *stats)
4128 unsigned int handle;
4129 struct nlattr *nl_options;
4131 error = tc_parse_class(tcmsg, &handle, &nl_options, stats);
4137 unsigned int major, minor;
4139 major = tc_get_major(handle);
4140 minor = tc_get_minor(handle);
4141 if (major == 1 && minor > 0 && minor <= HFSC_N_QUEUES) {
4142 *queue_id = minor - 1;
4149 error = hfsc_parse_tca_options__(nl_options, options);
4156 hfsc_query_class__(const struct netdev *netdev, unsigned int handle,
4157 unsigned int parent, struct hfsc_class *options,
4158 struct netdev_queue_stats *stats)
4161 struct ofpbuf *reply;
4163 error = tc_query_class(netdev, handle, parent, &reply);
4168 error = hfsc_parse_tcmsg__(reply, NULL, options, stats);
4169 ofpbuf_delete(reply);
4174 hfsc_parse_qdisc_details__(struct netdev *netdev_, const struct smap *details,
4175 struct hfsc_class *class)
4177 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
4179 const char *max_rate_s;
4181 max_rate_s = smap_get(details, "max-rate");
4182 max_rate = max_rate_s ? strtoull(max_rate_s, NULL, 10) / 8 : 0;
4185 enum netdev_features current;
4187 netdev_linux_read_features(netdev);
4188 current = !netdev->get_features_error ? netdev->current : 0;
4189 max_rate = netdev_features_to_bps(current, 100 * 1000 * 1000) / 8;
4192 class->min_rate = max_rate;
4193 class->max_rate = max_rate;
4197 hfsc_parse_class_details__(struct netdev *netdev,
4198 const struct smap *details,
4199 struct hfsc_class * class)
4201 const struct hfsc *hfsc;
4202 uint32_t min_rate, max_rate;
4203 const char *min_rate_s, *max_rate_s;
4205 hfsc = hfsc_get__(netdev);
4206 min_rate_s = smap_get(details, "min-rate");
4207 max_rate_s = smap_get(details, "max-rate");
4209 min_rate = min_rate_s ? strtoull(min_rate_s, NULL, 10) / 8 : 0;
4210 min_rate = MAX(min_rate, 1);
4211 min_rate = MIN(min_rate, hfsc->max_rate);
4213 max_rate = (max_rate_s
4214 ? strtoull(max_rate_s, NULL, 10) / 8
4216 max_rate = MAX(max_rate, min_rate);
4217 max_rate = MIN(max_rate, hfsc->max_rate);
4219 class->min_rate = min_rate;
4220 class->max_rate = max_rate;
4225 /* Create an HFSC qdisc.
4227 * Equivalent to "tc qdisc add dev <dev> root handle 1: hfsc default 1". */
4229 hfsc_setup_qdisc__(struct netdev * netdev)
4231 struct tcmsg *tcmsg;
4232 struct ofpbuf request;
4233 struct tc_hfsc_qopt opt;
4235 tc_del_qdisc(netdev);
4237 tcmsg = tc_make_request(netdev, RTM_NEWQDISC,
4238 NLM_F_EXCL | NLM_F_CREATE, &request);
4244 tcmsg->tcm_handle = tc_make_handle(1, 0);
4245 tcmsg->tcm_parent = TC_H_ROOT;
4247 memset(&opt, 0, sizeof opt);
4250 nl_msg_put_string(&request, TCA_KIND, "hfsc");
4251 nl_msg_put_unspec(&request, TCA_OPTIONS, &opt, sizeof opt);
4253 return tc_transact(&request, NULL);
4256 /* Create an HFSC class.
4258 * Equivalent to "tc class add <dev> parent <parent> classid <handle> hfsc
4259 * sc rate <min_rate> ul rate <max_rate>" */
4261 hfsc_setup_class__(struct netdev *netdev, unsigned int handle,
4262 unsigned int parent, struct hfsc_class *class)
4266 struct tcmsg *tcmsg;
4267 struct ofpbuf request;
4268 struct tc_service_curve min, max;
4270 tcmsg = tc_make_request(netdev, RTM_NEWTCLASS, NLM_F_CREATE, &request);
4276 tcmsg->tcm_handle = handle;
4277 tcmsg->tcm_parent = parent;
4281 min.m2 = class->min_rate;
4285 max.m2 = class->max_rate;
4287 nl_msg_put_string(&request, TCA_KIND, "hfsc");
4288 opt_offset = nl_msg_start_nested(&request, TCA_OPTIONS);
4289 nl_msg_put_unspec(&request, TCA_HFSC_RSC, &min, sizeof min);
4290 nl_msg_put_unspec(&request, TCA_HFSC_FSC, &min, sizeof min);
4291 nl_msg_put_unspec(&request, TCA_HFSC_USC, &max, sizeof max);
4292 nl_msg_end_nested(&request, opt_offset);
4294 error = tc_transact(&request, NULL);
4296 VLOG_WARN_RL(&rl, "failed to replace %s class %u:%u, parent %u:%u, "
4297 "min-rate %ubps, max-rate %ubps (%s)",
4298 netdev_get_name(netdev),
4299 tc_get_major(handle), tc_get_minor(handle),
4300 tc_get_major(parent), tc_get_minor(parent),
4301 class->min_rate, class->max_rate, ovs_strerror(error));
4308 hfsc_tc_install(struct netdev *netdev, const struct smap *details)
4311 struct hfsc_class class;
4313 error = hfsc_setup_qdisc__(netdev);
4319 hfsc_parse_qdisc_details__(netdev, details, &class);
4320 error = hfsc_setup_class__(netdev, tc_make_handle(1, 0xfffe),
4321 tc_make_handle(1, 0), &class);
4327 hfsc_install__(netdev, class.max_rate);
4332 hfsc_tc_load(struct netdev *netdev, struct ofpbuf *nlmsg OVS_UNUSED)
4335 struct queue_dump_state state;
4336 struct hfsc_class hc;
4339 hfsc_query_class__(netdev, tc_make_handle(1, 0xfffe), 0, &hc, NULL);
4340 hfsc_install__(netdev, hc.max_rate);
4342 if (!start_queue_dump(netdev, &state)) {
4346 while (nl_dump_next(&state.dump, &msg, &state.buf)) {
4347 unsigned int queue_id;
4349 if (!hfsc_parse_tcmsg__(&msg, &queue_id, &hc, NULL)) {
4350 hfsc_update_queue__(netdev, queue_id, &hc);
4354 finish_queue_dump(&state);
4359 hfsc_tc_destroy(struct tc *tc)
4362 struct hfsc_class *hc, *next;
4364 hfsc = CONTAINER_OF(tc, struct hfsc, tc);
4366 HMAP_FOR_EACH_SAFE (hc, next, tc_queue.hmap_node, &hfsc->tc.queues) {
4367 hmap_remove(&hfsc->tc.queues, &hc->tc_queue.hmap_node);
4376 hfsc_qdisc_get(const struct netdev *netdev, struct smap *details)
4378 const struct hfsc *hfsc;
4379 hfsc = hfsc_get__(netdev);
4380 smap_add_format(details, "max-rate", "%llu", 8ULL * hfsc->max_rate);
4385 hfsc_qdisc_set(struct netdev *netdev, const struct smap *details)
4388 struct hfsc_class class;
4390 hfsc_parse_qdisc_details__(netdev, details, &class);
4391 error = hfsc_setup_class__(netdev, tc_make_handle(1, 0xfffe),
4392 tc_make_handle(1, 0), &class);
4395 hfsc_get__(netdev)->max_rate = class.max_rate;
4402 hfsc_class_get(const struct netdev *netdev OVS_UNUSED,
4403 const struct tc_queue *queue, struct smap *details)
4405 const struct hfsc_class *hc;
4407 hc = hfsc_class_cast__(queue);
4408 smap_add_format(details, "min-rate", "%llu", 8ULL * hc->min_rate);
4409 if (hc->min_rate != hc->max_rate) {
4410 smap_add_format(details, "max-rate", "%llu", 8ULL * hc->max_rate);
4416 hfsc_class_set(struct netdev *netdev, unsigned int queue_id,
4417 const struct smap *details)
4420 struct hfsc_class class;
4422 error = hfsc_parse_class_details__(netdev, details, &class);
4427 error = hfsc_setup_class__(netdev, tc_make_handle(1, queue_id + 1),
4428 tc_make_handle(1, 0xfffe), &class);
4433 hfsc_update_queue__(netdev, queue_id, &class);
4438 hfsc_class_delete(struct netdev *netdev, struct tc_queue *queue)
4442 struct hfsc_class *hc;
4444 hc = hfsc_class_cast__(queue);
4445 hfsc = hfsc_get__(netdev);
4447 error = tc_delete_class(netdev, tc_make_handle(1, queue->queue_id + 1));
4449 hmap_remove(&hfsc->tc.queues, &hc->tc_queue.hmap_node);
4456 hfsc_class_get_stats(const struct netdev *netdev, const struct tc_queue *queue,
4457 struct netdev_queue_stats *stats)
4459 return hfsc_query_class__(netdev, tc_make_handle(1, queue->queue_id + 1),
4460 tc_make_handle(1, 0xfffe), NULL, stats);
4464 hfsc_class_dump_stats(const struct netdev *netdev OVS_UNUSED,
4465 const struct ofpbuf *nlmsg,
4466 netdev_dump_queue_stats_cb *cb, void *aux)
4468 struct netdev_queue_stats stats;
4469 unsigned int handle, major, minor;
4472 error = tc_parse_class(nlmsg, &handle, NULL, &stats);
4477 major = tc_get_major(handle);
4478 minor = tc_get_minor(handle);
4479 if (major == 1 && minor > 0 && minor <= HFSC_N_QUEUES) {
4480 (*cb)(minor - 1, &stats, aux);
4485 static const struct tc_ops tc_ops_hfsc = {
4486 "hfsc", /* linux_name */
4487 "linux-hfsc", /* ovs_name */
4488 HFSC_N_QUEUES, /* n_queues */
4489 hfsc_tc_install, /* tc_install */
4490 hfsc_tc_load, /* tc_load */
4491 hfsc_tc_destroy, /* tc_destroy */
4492 hfsc_qdisc_get, /* qdisc_get */
4493 hfsc_qdisc_set, /* qdisc_set */
4494 hfsc_class_get, /* class_get */
4495 hfsc_class_set, /* class_set */
4496 hfsc_class_delete, /* class_delete */
4497 hfsc_class_get_stats, /* class_get_stats */
4498 hfsc_class_dump_stats /* class_dump_stats */
4501 /* "linux-noop" traffic control class. */
4504 noop_install__(struct netdev *netdev_)
4506 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
4507 static const struct tc tc = TC_INITIALIZER(&tc, &tc_ops_default);
4509 netdev->tc = CONST_CAST(struct tc *, &tc);
4513 noop_tc_install(struct netdev *netdev,
4514 const struct smap *details OVS_UNUSED)
4516 noop_install__(netdev);
4521 noop_tc_load(struct netdev *netdev, struct ofpbuf *nlmsg OVS_UNUSED)
4523 noop_install__(netdev);
4527 static const struct tc_ops tc_ops_noop = {
4528 NULL, /* linux_name */
4529 "linux-noop", /* ovs_name */
4533 NULL, /* tc_destroy */
4534 NULL, /* qdisc_get */
4535 NULL, /* qdisc_set */
4536 NULL, /* class_get */
4537 NULL, /* class_set */
4538 NULL, /* class_delete */
4539 NULL, /* class_get_stats */
4540 NULL /* class_dump_stats */
4543 /* "linux-default" traffic control class.
4545 * This class represents the default, unnamed Linux qdisc. It corresponds to
4546 * the "" (empty string) QoS type in the OVS database. */
4549 default_install__(struct netdev *netdev_)
4551 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
4552 static const struct tc tc = TC_INITIALIZER(&tc, &tc_ops_default);
4554 /* Nothing but a tc class implementation is allowed to write to a tc. This
4555 * class never does that, so we can legitimately use a const tc object. */
4556 netdev->tc = CONST_CAST(struct tc *, &tc);
4560 default_tc_install(struct netdev *netdev,
4561 const struct smap *details OVS_UNUSED)
4563 default_install__(netdev);
4568 default_tc_load(struct netdev *netdev, struct ofpbuf *nlmsg OVS_UNUSED)
4570 default_install__(netdev);
4574 static const struct tc_ops tc_ops_default = {
4575 NULL, /* linux_name */
4580 NULL, /* tc_destroy */
4581 NULL, /* qdisc_get */
4582 NULL, /* qdisc_set */
4583 NULL, /* class_get */
4584 NULL, /* class_set */
4585 NULL, /* class_delete */
4586 NULL, /* class_get_stats */
4587 NULL /* class_dump_stats */
4590 /* "linux-other" traffic control class.
4595 other_tc_load(struct netdev *netdev_, struct ofpbuf *nlmsg OVS_UNUSED)
4597 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
4598 static const struct tc tc = TC_INITIALIZER(&tc, &tc_ops_other);
4600 /* Nothing but a tc class implementation is allowed to write to a tc. This
4601 * class never does that, so we can legitimately use a const tc object. */
4602 netdev->tc = CONST_CAST(struct tc *, &tc);
4606 static const struct tc_ops tc_ops_other = {
4607 NULL, /* linux_name */
4608 "linux-other", /* ovs_name */
4610 NULL, /* tc_install */
4612 NULL, /* tc_destroy */
4613 NULL, /* qdisc_get */
4614 NULL, /* qdisc_set */
4615 NULL, /* class_get */
4616 NULL, /* class_set */
4617 NULL, /* class_delete */
4618 NULL, /* class_get_stats */
4619 NULL /* class_dump_stats */
4622 /* Traffic control. */
4624 /* Number of kernel "tc" ticks per second. */
4625 static double ticks_per_s;
4627 /* Number of kernel "jiffies" per second. This is used for the purpose of
4628 * computing buffer sizes. Generally kernel qdiscs need to be able to buffer
4629 * one jiffy's worth of data.
4631 * There are two possibilities here:
4633 * - 'buffer_hz' is the kernel's real timer tick rate, a small number in the
4634 * approximate range of 100 to 1024. That means that we really need to
4635 * make sure that the qdisc can buffer that much data.
4637 * - 'buffer_hz' is an absurdly large number. That means that the kernel
4638 * has finely granular timers and there's no need to fudge additional room
4639 * for buffers. (There's no extra effort needed to implement that: the
4640 * large 'buffer_hz' is used as a divisor, so practically any number will
4641 * come out as 0 in the division. Small integer results in the case of
4642 * really high dividends won't have any real effect anyhow.)
4644 static unsigned int buffer_hz;
4646 /* Returns tc handle 'major':'minor'. */
4648 tc_make_handle(unsigned int major, unsigned int minor)
4650 return TC_H_MAKE(major << 16, minor);
4653 /* Returns the major number from 'handle'. */
4655 tc_get_major(unsigned int handle)
4657 return TC_H_MAJ(handle) >> 16;
4660 /* Returns the minor number from 'handle'. */
4662 tc_get_minor(unsigned int handle)
4664 return TC_H_MIN(handle);
4667 static struct tcmsg *
4668 tc_make_request(const struct netdev *netdev, int type, unsigned int flags,
4669 struct ofpbuf *request)
4671 struct tcmsg *tcmsg;
4675 error = get_ifindex(netdev, &ifindex);
4680 ofpbuf_init(request, 512);
4681 nl_msg_put_nlmsghdr(request, sizeof *tcmsg, type, NLM_F_REQUEST | flags);
4682 tcmsg = ofpbuf_put_zeros(request, sizeof *tcmsg);
4683 tcmsg->tcm_family = AF_UNSPEC;
4684 tcmsg->tcm_ifindex = ifindex;
4685 /* Caller should fill in tcmsg->tcm_handle. */
4686 /* Caller should fill in tcmsg->tcm_parent. */
4692 tc_transact(struct ofpbuf *request, struct ofpbuf **replyp)
4694 int error = nl_transact(NETLINK_ROUTE, request, replyp);
4695 ofpbuf_uninit(request);
4699 /* Adds or deletes a root ingress qdisc on 'netdev'. We use this for
4700 * policing configuration.
4702 * This function is equivalent to running the following when 'add' is true:
4703 * /sbin/tc qdisc add dev <devname> handle ffff: ingress
4705 * This function is equivalent to running the following when 'add' is false:
4706 * /sbin/tc qdisc del dev <devname> handle ffff: ingress
4708 * The configuration and stats may be seen with the following command:
4709 * /sbin/tc -s qdisc show dev <devname>
4711 * Returns 0 if successful, otherwise a positive errno value.
4714 tc_add_del_ingress_qdisc(struct netdev *netdev, bool add)
4716 struct ofpbuf request;
4717 struct tcmsg *tcmsg;
4719 int type = add ? RTM_NEWQDISC : RTM_DELQDISC;
4720 int flags = add ? NLM_F_EXCL | NLM_F_CREATE : 0;
4722 tcmsg = tc_make_request(netdev, type, flags, &request);
4726 tcmsg->tcm_handle = tc_make_handle(0xffff, 0);
4727 tcmsg->tcm_parent = TC_H_INGRESS;
4728 nl_msg_put_string(&request, TCA_KIND, "ingress");
4729 nl_msg_put_unspec(&request, TCA_OPTIONS, NULL, 0);
4731 error = tc_transact(&request, NULL);
4733 /* If we're deleting the qdisc, don't worry about some of the
4734 * error conditions. */
4735 if (!add && (error == ENOENT || error == EINVAL)) {
4744 /* Adds a policer to 'netdev' with a rate of 'kbits_rate' and a burst size
4747 * This function is equivalent to running:
4748 * /sbin/tc filter add dev <devname> parent ffff: protocol all prio 49
4749 * basic police rate <kbits_rate>kbit burst <kbits_burst>k
4752 * The configuration and stats may be seen with the following command:
4753 * /sbin/tc -s filter show dev <devname> parent ffff:
4755 * Returns 0 if successful, otherwise a positive errno value.
4758 tc_add_policer(struct netdev *netdev,
4759 uint32_t kbits_rate, uint32_t kbits_burst)
4761 struct tc_police tc_police;
4762 struct ofpbuf request;
4763 struct tcmsg *tcmsg;
4764 size_t basic_offset;
4765 size_t police_offset;
4769 memset(&tc_police, 0, sizeof tc_police);
4770 tc_police.action = TC_POLICE_SHOT;
4771 tc_police.mtu = mtu;
4772 tc_fill_rate(&tc_police.rate, ((uint64_t) kbits_rate * 1000)/8, mtu);
4774 /* The following appears wrong in one way: In networking a kilobit is
4775 * usually 1000 bits but this uses 1024 bits.
4777 * However if you "fix" those problems then "tc filter show ..." shows
4778 * "125000b", meaning 125,000 bits, when OVS configures it for 1000 kbit ==
4779 * 1,000,000 bits, whereas this actually ends up doing the right thing from
4780 * tc's point of view. Whatever. */
4781 tc_police.burst = tc_bytes_to_ticks(
4782 tc_police.rate.rate, MIN(UINT32_MAX / 1024, kbits_burst) * 1024 / 8);
4784 tcmsg = tc_make_request(netdev, RTM_NEWTFILTER,
4785 NLM_F_EXCL | NLM_F_CREATE, &request);
4789 tcmsg->tcm_parent = tc_make_handle(0xffff, 0);
4790 tcmsg->tcm_info = tc_make_handle(49,
4791 (OVS_FORCE uint16_t) htons(ETH_P_ALL));
4793 nl_msg_put_string(&request, TCA_KIND, "basic");
4794 basic_offset = nl_msg_start_nested(&request, TCA_OPTIONS);
4795 police_offset = nl_msg_start_nested(&request, TCA_BASIC_POLICE);
4796 nl_msg_put_unspec(&request, TCA_POLICE_TBF, &tc_police, sizeof tc_police);
4797 tc_put_rtab(&request, TCA_POLICE_RATE, &tc_police.rate);
4798 nl_msg_end_nested(&request, police_offset);
4799 nl_msg_end_nested(&request, basic_offset);
4801 error = tc_transact(&request, NULL);
4812 /* The values in psched are not individually very meaningful, but they are
4813 * important. The tables below show some values seen in the wild.
4817 * - "c" has always been a constant 1000000 since at least Linux 2.4.14.
4818 * (Before that, there are hints that it was 1000000000.)
4820 * - "d" can be unrealistically large, see the comment on 'buffer_hz'
4824 * -----------------------------------
4825 * [1] 000c8000 000f4240 000f4240 00000064
4826 * [2] 000003e8 00000400 000f4240 3b9aca00
4827 * [3] 000003e8 00000400 000f4240 3b9aca00
4828 * [4] 000003e8 00000400 000f4240 00000064
4829 * [5] 000003e8 00000040 000f4240 3b9aca00
4830 * [6] 000003e8 00000040 000f4240 000000f9
4832 * a b c d ticks_per_s buffer_hz
4833 * ------- --------- ---------- ------------- ----------- -------------
4834 * [1] 819,200 1,000,000 1,000,000 100 819,200 100
4835 * [2] 1,000 1,024 1,000,000 1,000,000,000 976,562 1,000,000,000
4836 * [3] 1,000 1,024 1,000,000 1,000,000,000 976,562 1,000,000,000
4837 * [4] 1,000 1,024 1,000,000 100 976,562 100
4838 * [5] 1,000 64 1,000,000 1,000,000,000 15,625,000 1,000,000,000
4839 * [6] 1,000 64 1,000,000 249 15,625,000 249
4841 * [1] 2.6.18-128.1.6.el5.xs5.5.0.505.1024xen from XenServer 5.5.0-24648p
4842 * [2] 2.6.26-1-686-bigmem from Debian lenny
4843 * [3] 2.6.26-2-sparc64 from Debian lenny
4844 * [4] 2.6.27.42-0.1.1.xs5.6.810.44.111163xen from XenServer 5.6.810-31078p
4845 * [5] 2.6.32.21.22 (approx.) from Ubuntu 10.04 on VMware Fusion
4846 * [6] 2.6.34 from kernel.org on KVM
4848 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
4849 static const char fn[] = "/proc/net/psched";
4850 unsigned int a, b, c, d;
4853 if (!ovsthread_once_start(&once)) {
4860 stream = fopen(fn, "r");
4862 VLOG_WARN("%s: open failed: %s", fn, ovs_strerror(errno));
4866 if (fscanf(stream, "%x %x %x %x", &a, &b, &c, &d) != 4) {
4867 VLOG_WARN("%s: read failed", fn);
4871 VLOG_DBG("%s: psched parameters are: %u %u %u %u", fn, a, b, c, d);
4875 VLOG_WARN("%s: invalid scheduler parameters", fn);
4879 ticks_per_s = (double) a * c / b;
4883 VLOG_WARN("%s: unexpected psched parameters: %u %u %u %u",
4886 VLOG_DBG("%s: ticks_per_s=%f buffer_hz=%u", fn, ticks_per_s, buffer_hz);
4889 ovsthread_once_done(&once);
4892 /* Returns the number of bytes that can be transmitted in 'ticks' ticks at a
4893 * rate of 'rate' bytes per second. */
4895 tc_ticks_to_bytes(unsigned int rate, unsigned int ticks)
4898 return (rate * ticks) / ticks_per_s;
4901 /* Returns the number of ticks that it would take to transmit 'size' bytes at a
4902 * rate of 'rate' bytes per second. */
4904 tc_bytes_to_ticks(unsigned int rate, unsigned int size)
4907 return rate ? ((unsigned long long int) ticks_per_s * size) / rate : 0;
4910 /* Returns the number of bytes that need to be reserved for qdisc buffering at
4911 * a transmission rate of 'rate' bytes per second. */
4913 tc_buffer_per_jiffy(unsigned int rate)
4916 return rate / buffer_hz;
4919 /* Given Netlink 'msg' that describes a qdisc, extracts the name of the qdisc,
4920 * e.g. "htb", into '*kind' (if it is nonnull). If 'options' is nonnull,
4921 * extracts 'msg''s TCA_OPTIONS attributes into '*options' if it is present or
4922 * stores NULL into it if it is absent.
4924 * '*kind' and '*options' point into 'msg', so they are owned by whoever owns
4927 * Returns 0 if successful, otherwise a positive errno value. */
4929 tc_parse_qdisc(const struct ofpbuf *msg, const char **kind,
4930 struct nlattr **options)
4932 static const struct nl_policy tca_policy[] = {
4933 [TCA_KIND] = { .type = NL_A_STRING, .optional = false },
4934 [TCA_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
4936 struct nlattr *ta[ARRAY_SIZE(tca_policy)];
4938 if (!nl_policy_parse(msg, NLMSG_HDRLEN + sizeof(struct tcmsg),
4939 tca_policy, ta, ARRAY_SIZE(ta))) {
4940 VLOG_WARN_RL(&rl, "failed to parse qdisc message");
4945 *kind = nl_attr_get_string(ta[TCA_KIND]);
4949 *options = ta[TCA_OPTIONS];
4964 /* Given Netlink 'msg' that describes a class, extracts the queue ID (e.g. the
4965 * minor number of its class ID) into '*queue_id', its TCA_OPTIONS attribute
4966 * into '*options', and its queue statistics into '*stats'. Any of the output
4967 * arguments may be null.
4969 * Returns 0 if successful, otherwise a positive errno value. */
4971 tc_parse_class(const struct ofpbuf *msg, unsigned int *handlep,
4972 struct nlattr **options, struct netdev_queue_stats *stats)
4974 static const struct nl_policy tca_policy[] = {
4975 [TCA_OPTIONS] = { .type = NL_A_NESTED, .optional = false },
4976 [TCA_STATS2] = { .type = NL_A_NESTED, .optional = false },
4978 struct nlattr *ta[ARRAY_SIZE(tca_policy)];
4980 if (!nl_policy_parse(msg, NLMSG_HDRLEN + sizeof(struct tcmsg),
4981 tca_policy, ta, ARRAY_SIZE(ta))) {
4982 VLOG_WARN_RL(&rl, "failed to parse class message");
4987 struct tcmsg *tc = ofpbuf_at_assert(msg, NLMSG_HDRLEN, sizeof *tc);
4988 *handlep = tc->tcm_handle;
4992 *options = ta[TCA_OPTIONS];
4996 const struct gnet_stats_queue *gsq;
4997 struct gnet_stats_basic gsb;
4999 static const struct nl_policy stats_policy[] = {
5000 [TCA_STATS_BASIC] = { .type = NL_A_UNSPEC, .optional = false,
5001 .min_len = sizeof gsb },
5002 [TCA_STATS_QUEUE] = { .type = NL_A_UNSPEC, .optional = false,
5003 .min_len = sizeof *gsq },
5005 struct nlattr *sa[ARRAY_SIZE(stats_policy)];
5007 if (!nl_parse_nested(ta[TCA_STATS2], stats_policy,
5008 sa, ARRAY_SIZE(sa))) {
5009 VLOG_WARN_RL(&rl, "failed to parse class stats");
5013 /* Alignment issues screw up the length of struct gnet_stats_basic on
5014 * some arch/bitsize combinations. Newer versions of Linux have a
5015 * struct gnet_stats_basic_packed, but we can't depend on that. The
5016 * easiest thing to do is just to make a copy. */
5017 memset(&gsb, 0, sizeof gsb);
5018 memcpy(&gsb, nl_attr_get(sa[TCA_STATS_BASIC]),
5019 MIN(nl_attr_get_size(sa[TCA_STATS_BASIC]), sizeof gsb));
5020 stats->tx_bytes = gsb.bytes;
5021 stats->tx_packets = gsb.packets;
5023 gsq = nl_attr_get(sa[TCA_STATS_QUEUE]);
5024 stats->tx_errors = gsq->drops;
5034 memset(stats, 0, sizeof *stats);
5039 /* Queries the kernel for class with identifier 'handle' and parent 'parent'
5042 tc_query_class(const struct netdev *netdev,
5043 unsigned int handle, unsigned int parent,
5044 struct ofpbuf **replyp)
5046 struct ofpbuf request;
5047 struct tcmsg *tcmsg;
5050 tcmsg = tc_make_request(netdev, RTM_GETTCLASS, NLM_F_ECHO, &request);
5054 tcmsg->tcm_handle = handle;
5055 tcmsg->tcm_parent = parent;
5057 error = tc_transact(&request, replyp);
5059 VLOG_WARN_RL(&rl, "query %s class %u:%u (parent %u:%u) failed (%s)",
5060 netdev_get_name(netdev),
5061 tc_get_major(handle), tc_get_minor(handle),
5062 tc_get_major(parent), tc_get_minor(parent),
5063 ovs_strerror(error));
5068 /* Equivalent to "tc class del dev <name> handle <handle>". */
5070 tc_delete_class(const struct netdev *netdev, unsigned int handle)
5072 struct ofpbuf request;
5073 struct tcmsg *tcmsg;
5076 tcmsg = tc_make_request(netdev, RTM_DELTCLASS, 0, &request);
5080 tcmsg->tcm_handle = handle;
5081 tcmsg->tcm_parent = 0;
5083 error = tc_transact(&request, NULL);
5085 VLOG_WARN_RL(&rl, "delete %s class %u:%u failed (%s)",
5086 netdev_get_name(netdev),
5087 tc_get_major(handle), tc_get_minor(handle),
5088 ovs_strerror(error));
5093 /* Equivalent to "tc qdisc del dev <name> root". */
5095 tc_del_qdisc(struct netdev *netdev_)
5097 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
5098 struct ofpbuf request;
5099 struct tcmsg *tcmsg;
5102 tcmsg = tc_make_request(netdev_, RTM_DELQDISC, 0, &request);
5106 tcmsg->tcm_handle = tc_make_handle(1, 0);
5107 tcmsg->tcm_parent = TC_H_ROOT;
5109 error = tc_transact(&request, NULL);
5110 if (error == EINVAL) {
5111 /* EINVAL probably means that the default qdisc was in use, in which
5112 * case we've accomplished our purpose. */
5115 if (!error && netdev->tc) {
5116 if (netdev->tc->ops->tc_destroy) {
5117 netdev->tc->ops->tc_destroy(netdev->tc);
5125 getqdisc_is_safe(void)
5127 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
5128 static bool safe = false;
5130 if (ovsthread_once_start(&once)) {
5131 struct utsname utsname;
5134 if (uname(&utsname) == -1) {
5135 VLOG_WARN("uname failed (%s)", ovs_strerror(errno));
5136 } else if (!ovs_scan(utsname.release, "%d.%d", &major, &minor)) {
5137 VLOG_WARN("uname reported bad OS release (%s)", utsname.release);
5138 } else if (major < 2 || (major == 2 && minor < 35)) {
5139 VLOG_INFO("disabling unsafe RTM_GETQDISC in Linux kernel %s",
5144 ovsthread_once_done(&once);
5149 /* If 'netdev''s qdisc type and parameters are not yet known, queries the
5150 * kernel to determine what they are. Returns 0 if successful, otherwise a
5151 * positive errno value. */
5153 tc_query_qdisc(const struct netdev *netdev_)
5155 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
5156 struct ofpbuf request, *qdisc;
5157 const struct tc_ops *ops;
5158 struct tcmsg *tcmsg;
5166 /* This RTM_GETQDISC is crafted to avoid OOPSing kernels that do not have
5167 * commit 53b0f08 "net_sched: Fix qdisc_notify()", which is anything before
5168 * 2.6.35 without that fix backported to it.
5170 * To avoid the OOPS, we must not make a request that would attempt to dump
5171 * a "built-in" qdisc, that is, the default pfifo_fast qdisc or one of a
5172 * few others. There are a few ways that I can see to do this, but most of
5173 * them seem to be racy (and if you lose the race the kernel OOPSes). The
5174 * technique chosen here is to assume that any non-default qdisc that we
5175 * create will have a class with handle 1:0. The built-in qdiscs only have
5176 * a class with handle 0:0.
5178 * On Linux 2.6.35+ we use the straightforward method because it allows us
5179 * to handle non-builtin qdiscs without handle 1:0 (e.g. codel). However,
5180 * in such a case we get no response at all from the kernel (!) if a
5181 * builtin qdisc is in use (which is later caught by "!error &&
5182 * !qdisc->size"). */
5183 tcmsg = tc_make_request(netdev_, RTM_GETQDISC, NLM_F_ECHO, &request);
5187 tcmsg->tcm_handle = tc_make_handle(getqdisc_is_safe() ? 0 : 1, 0);
5188 tcmsg->tcm_parent = getqdisc_is_safe() ? TC_H_ROOT : 0;
5190 /* Figure out what tc class to instantiate. */
5191 error = tc_transact(&request, &qdisc);
5192 if (!error && qdisc->size) {
5195 error = tc_parse_qdisc(qdisc, &kind, NULL);
5197 ops = &tc_ops_other;
5199 ops = tc_lookup_linux_name(kind);
5201 static struct vlog_rate_limit rl2 = VLOG_RATE_LIMIT_INIT(1, 1);
5202 VLOG_DBG_RL(&rl2, "unknown qdisc \"%s\"", kind);
5204 ops = &tc_ops_other;
5207 } else if ((!error && !qdisc->size) || error == ENOENT) {
5208 /* Either it's a built-in qdisc, or (on Linux pre-2.6.35) it's a qdisc
5209 * set up by some other entity that doesn't have a handle 1:0. We will
5210 * assume that it's the system default qdisc. */
5211 ops = &tc_ops_default;
5214 /* Who knows? Maybe the device got deleted. */
5215 VLOG_WARN_RL(&rl, "query %s qdisc failed (%s)",
5216 netdev_get_name(netdev_), ovs_strerror(error));
5217 ops = &tc_ops_other;
5220 /* Instantiate it. */
5221 load_error = ops->tc_load(CONST_CAST(struct netdev *, netdev_), qdisc);
5222 ovs_assert((load_error == 0) == (netdev->tc != NULL));
5223 ofpbuf_delete(qdisc);
5225 return error ? error : load_error;
5228 /* Linux traffic control uses tables with 256 entries ("rtab" tables) to
5229 approximate the time to transmit packets of various lengths. For an MTU of
5230 256 or less, each entry is exact; for an MTU of 257 through 512, each entry
5231 represents two possible packet lengths; for a MTU of 513 through 1024, four
5232 possible lengths; and so on.
5234 Returns, for the specified 'mtu', the number of bits that packet lengths
5235 need to be shifted right to fit within such a 256-entry table. */
5237 tc_calc_cell_log(unsigned int mtu)
5242 mtu = ETH_PAYLOAD_MAX;
5244 mtu += ETH_HEADER_LEN + VLAN_HEADER_LEN;
5246 for (cell_log = 0; mtu >= 256; cell_log++) {
5253 /* Initializes 'rate' properly for a rate of 'Bps' bytes per second with an MTU
5256 tc_fill_rate(struct tc_ratespec *rate, uint64_t Bps, int mtu)
5258 memset(rate, 0, sizeof *rate);
5259 rate->cell_log = tc_calc_cell_log(mtu);
5260 /* rate->overhead = 0; */ /* New in 2.6.24, not yet in some */
5261 /* rate->cell_align = 0; */ /* distro headers. */
5262 rate->mpu = ETH_TOTAL_MIN;
5266 /* Appends to 'msg' an "rtab" table for the specified 'rate' as a Netlink
5267 * attribute of the specified "type".
5269 * See tc_calc_cell_log() above for a description of "rtab"s. */
5271 tc_put_rtab(struct ofpbuf *msg, uint16_t type, const struct tc_ratespec *rate)
5276 rtab = nl_msg_put_unspec_uninit(msg, type, TC_RTAB_SIZE);
5277 for (i = 0; i < TC_RTAB_SIZE / sizeof *rtab; i++) {
5278 unsigned packet_size = (i + 1) << rate->cell_log;
5279 if (packet_size < rate->mpu) {
5280 packet_size = rate->mpu;
5282 rtab[i] = tc_bytes_to_ticks(rate->rate, packet_size);
5286 /* Calculates the proper value of 'buffer' or 'cbuffer' in HTB options given a
5287 * rate of 'Bps' bytes per second, the specified 'mtu', and a user-requested
5288 * burst size of 'burst_bytes'. (If no value was requested, a 'burst_bytes' of
5291 tc_calc_buffer(unsigned int Bps, int mtu, uint64_t burst_bytes)
5293 unsigned int min_burst = tc_buffer_per_jiffy(Bps) + mtu;
5294 return tc_bytes_to_ticks(Bps, MAX(burst_bytes, min_burst));
5297 /* Linux-only functions declared in netdev-linux.h */
5299 /* Modifies the 'flag' bit in ethtool's flags field for 'netdev'. If
5300 * 'enable' is true, the bit is set. Otherwise, it is cleared. */
5302 netdev_linux_ethtool_set_flag(struct netdev *netdev, uint32_t flag,
5303 const char *flag_name, bool enable)
5305 const char *netdev_name = netdev_get_name(netdev);
5306 struct ethtool_value evalue;
5310 COVERAGE_INC(netdev_get_ethtool);
5311 memset(&evalue, 0, sizeof evalue);
5312 error = netdev_linux_do_ethtool(netdev_name,
5313 (struct ethtool_cmd *)&evalue,
5314 ETHTOOL_GFLAGS, "ETHTOOL_GFLAGS");
5319 COVERAGE_INC(netdev_set_ethtool);
5320 new_flags = (evalue.data & ~flag) | (enable ? flag : 0);
5321 if (new_flags == evalue.data) {
5324 evalue.data = new_flags;
5325 error = netdev_linux_do_ethtool(netdev_name,
5326 (struct ethtool_cmd *)&evalue,
5327 ETHTOOL_SFLAGS, "ETHTOOL_SFLAGS");
5332 COVERAGE_INC(netdev_get_ethtool);
5333 memset(&evalue, 0, sizeof evalue);
5334 error = netdev_linux_do_ethtool(netdev_name,
5335 (struct ethtool_cmd *)&evalue,
5336 ETHTOOL_GFLAGS, "ETHTOOL_GFLAGS");
5341 if (new_flags != evalue.data) {
5342 VLOG_WARN_RL(&rl, "attempt to %s ethtool %s flag on network "
5343 "device %s failed", enable ? "enable" : "disable",
5344 flag_name, netdev_name);
5351 /* Utility functions. */
5353 /* Copies 'src' into 'dst', performing format conversion in the process. */
5355 netdev_stats_from_rtnl_link_stats(struct netdev_stats *dst,
5356 const struct rtnl_link_stats *src)
5358 dst->rx_packets = src->rx_packets;
5359 dst->tx_packets = src->tx_packets;
5360 dst->rx_bytes = src->rx_bytes;
5361 dst->tx_bytes = src->tx_bytes;
5362 dst->rx_errors = src->rx_errors;
5363 dst->tx_errors = src->tx_errors;
5364 dst->rx_dropped = src->rx_dropped;
5365 dst->tx_dropped = src->tx_dropped;
5366 dst->multicast = src->multicast;
5367 dst->collisions = src->collisions;
5368 dst->rx_length_errors = src->rx_length_errors;
5369 dst->rx_over_errors = src->rx_over_errors;
5370 dst->rx_crc_errors = src->rx_crc_errors;
5371 dst->rx_frame_errors = src->rx_frame_errors;
5372 dst->rx_fifo_errors = src->rx_fifo_errors;
5373 dst->rx_missed_errors = src->rx_missed_errors;
5374 dst->tx_aborted_errors = src->tx_aborted_errors;
5375 dst->tx_carrier_errors = src->tx_carrier_errors;
5376 dst->tx_fifo_errors = src->tx_fifo_errors;
5377 dst->tx_heartbeat_errors = src->tx_heartbeat_errors;
5378 dst->tx_window_errors = src->tx_window_errors;
5381 /* Copies 'src' into 'dst', performing format conversion in the process. */
5383 netdev_stats_from_rtnl_link_stats64(struct netdev_stats *dst,
5384 const struct rtnl_link_stats64 *src)
5386 dst->rx_packets = src->rx_packets;
5387 dst->tx_packets = src->tx_packets;
5388 dst->rx_bytes = src->rx_bytes;
5389 dst->tx_bytes = src->tx_bytes;
5390 dst->rx_errors = src->rx_errors;
5391 dst->tx_errors = src->tx_errors;
5392 dst->rx_dropped = src->rx_dropped;
5393 dst->tx_dropped = src->tx_dropped;
5394 dst->multicast = src->multicast;
5395 dst->collisions = src->collisions;
5396 dst->rx_length_errors = src->rx_length_errors;
5397 dst->rx_over_errors = src->rx_over_errors;
5398 dst->rx_crc_errors = src->rx_crc_errors;
5399 dst->rx_frame_errors = src->rx_frame_errors;
5400 dst->rx_fifo_errors = src->rx_fifo_errors;
5401 dst->rx_missed_errors = src->rx_missed_errors;
5402 dst->tx_aborted_errors = src->tx_aborted_errors;
5403 dst->tx_carrier_errors = src->tx_carrier_errors;
5404 dst->tx_fifo_errors = src->tx_fifo_errors;
5405 dst->tx_heartbeat_errors = src->tx_heartbeat_errors;
5406 dst->tx_window_errors = src->tx_window_errors;
5410 get_stats_via_netlink(const struct netdev *netdev_, struct netdev_stats *stats)
5412 struct ofpbuf request;
5413 struct ofpbuf *reply;
5416 /* Filtering all counters by default */
5417 memset(stats, 0xFF, sizeof(struct netdev_stats));
5419 ofpbuf_init(&request, 0);
5420 nl_msg_put_nlmsghdr(&request,
5421 sizeof(struct ifinfomsg) + NL_ATTR_SIZE(IFNAMSIZ),
5422 RTM_GETLINK, NLM_F_REQUEST);
5423 ofpbuf_put_zeros(&request, sizeof(struct ifinfomsg));
5424 nl_msg_put_string(&request, IFLA_IFNAME, netdev_get_name(netdev_));
5425 error = nl_transact(NETLINK_ROUTE, &request, &reply);
5426 ofpbuf_uninit(&request);
5431 if (ofpbuf_try_pull(reply, NLMSG_HDRLEN + sizeof(struct ifinfomsg))) {
5432 const struct nlattr *a = nl_attr_find(reply, 0, IFLA_STATS64);
5433 if (a && nl_attr_get_size(a) >= sizeof(struct rtnl_link_stats64)) {
5434 netdev_stats_from_rtnl_link_stats64(stats, nl_attr_get(a));
5437 const struct nlattr *a = nl_attr_find(reply, 0, IFLA_STATS);
5438 if (a && nl_attr_get_size(a) >= sizeof(struct rtnl_link_stats)) {
5439 netdev_stats_from_rtnl_link_stats(stats, nl_attr_get(a));
5442 VLOG_WARN_RL(&rl, "RTM_GETLINK reply lacks stats");
5447 VLOG_WARN_RL(&rl, "short RTM_GETLINK reply");
5452 ofpbuf_delete(reply);
5457 get_flags(const struct netdev *dev, unsigned int *flags)
5463 error = af_inet_ifreq_ioctl(dev->name, &ifr, SIOCGIFFLAGS, "SIOCGIFFLAGS");
5465 *flags = ifr.ifr_flags;
5471 set_flags(const char *name, unsigned int flags)
5475 ifr.ifr_flags = flags;
5476 return af_inet_ifreq_ioctl(name, &ifr, SIOCSIFFLAGS, "SIOCSIFFLAGS");
5480 do_get_ifindex(const char *netdev_name)
5485 ovs_strzcpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
5486 COVERAGE_INC(netdev_get_ifindex);
5488 error = af_inet_ioctl(SIOCGIFINDEX, &ifr);
5490 VLOG_WARN_RL(&rl, "ioctl(SIOCGIFINDEX) on %s device failed: %s",
5491 netdev_name, ovs_strerror(error));
5494 return ifr.ifr_ifindex;
5498 get_ifindex(const struct netdev *netdev_, int *ifindexp)
5500 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
5502 if (!(netdev->cache_valid & VALID_IFINDEX)) {
5503 int ifindex = do_get_ifindex(netdev_get_name(netdev_));
5506 netdev->get_ifindex_error = -ifindex;
5507 netdev->ifindex = 0;
5509 netdev->get_ifindex_error = 0;
5510 netdev->ifindex = ifindex;
5512 netdev->cache_valid |= VALID_IFINDEX;
5515 *ifindexp = netdev->ifindex;
5516 return netdev->get_ifindex_error;
5520 get_etheraddr(const char *netdev_name, struct eth_addr *ea)
5526 memset(&ifr, 0, sizeof ifr);
5527 ovs_strzcpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
5528 COVERAGE_INC(netdev_get_hwaddr);
5529 error = af_inet_ioctl(SIOCGIFHWADDR, &ifr);
5531 /* ENODEV probably means that a vif disappeared asynchronously and
5532 * hasn't been removed from the database yet, so reduce the log level
5533 * to INFO for that case. */
5534 VLOG(error == ENODEV ? VLL_INFO : VLL_ERR,
5535 "ioctl(SIOCGIFHWADDR) on %s device failed: %s",
5536 netdev_name, ovs_strerror(error));
5539 hwaddr_family = ifr.ifr_hwaddr.sa_family;
5540 if (hwaddr_family != AF_UNSPEC && hwaddr_family != ARPHRD_ETHER) {
5541 VLOG_INFO("%s device has unknown hardware address family %d",
5542 netdev_name, hwaddr_family);
5545 memcpy(ea, ifr.ifr_hwaddr.sa_data, ETH_ADDR_LEN);
5550 set_etheraddr(const char *netdev_name, const struct eth_addr mac)
5555 memset(&ifr, 0, sizeof ifr);
5556 ovs_strzcpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
5557 ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
5558 memcpy(ifr.ifr_hwaddr.sa_data, &mac, ETH_ADDR_LEN);
5559 COVERAGE_INC(netdev_set_hwaddr);
5560 error = af_inet_ioctl(SIOCSIFHWADDR, &ifr);
5562 VLOG_ERR("ioctl(SIOCSIFHWADDR) on %s device failed: %s",
5563 netdev_name, ovs_strerror(error));
5569 netdev_linux_do_ethtool(const char *name, struct ethtool_cmd *ecmd,
5570 int cmd, const char *cmd_name)
5575 memset(&ifr, 0, sizeof ifr);
5576 ovs_strzcpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
5577 ifr.ifr_data = (caddr_t) ecmd;
5580 error = af_inet_ioctl(SIOCETHTOOL, &ifr);
5582 if (error != EOPNOTSUPP) {
5583 VLOG_WARN_RL(&rl, "ethtool command %s on network device %s "
5584 "failed: %s", cmd_name, name, ovs_strerror(error));
5586 /* The device doesn't support this operation. That's pretty
5587 * common, so there's no point in logging anything. */
5593 /* Returns an AF_PACKET raw socket or a negative errno value. */
5595 af_packet_sock(void)
5597 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
5600 if (ovsthread_once_start(&once)) {
5601 sock = socket(AF_PACKET, SOCK_RAW, 0);
5603 int error = set_nonblocking(sock);
5610 VLOG_ERR("failed to create packet socket: %s",
5611 ovs_strerror(errno));
5613 ovsthread_once_done(&once);