2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 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/rtnetlink.h>
33 #include <linux/sockios.h>
34 #include <sys/types.h>
35 #include <sys/ioctl.h>
36 #include <sys/socket.h>
37 #include <sys/utsname.h>
38 #include <netpacket/packet.h>
40 #include <net/if_arp.h>
41 #include <net/if_packet.h>
42 #include <net/route.h>
43 #include <netinet/in.h>
50 #include "dp-packet.h"
51 #include "dpif-netlink.h"
52 #include "dpif-netdev.h"
53 #include "openvswitch/dynamic-string.h"
54 #include "fatal-signal.h"
56 #include "openvswitch/hmap.h"
57 #include "netdev-provider.h"
58 #include "netdev-tc-offloads.h"
59 #include "netdev-vport.h"
60 #include "netlink-notifier.h"
61 #include "netlink-socket.h"
63 #include "openvswitch/ofpbuf.h"
64 #include "openflow/openflow.h"
65 #include "ovs-atomic.h"
67 #include "openvswitch/poll-loop.h"
68 #include "rtnetlink.h"
69 #include "openvswitch/shash.h"
70 #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 * https://mail.openvswitch.org/pipermail/ovs-dev/2014-October/291521.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 * https://mail.openvswitch.org/pipermail/ovs-dev/2014-October/291521.html).
186 * Maybe 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_ticks_to_bytes(unsigned int rate
, unsigned int ticks
);
438 static unsigned int tc_bytes_to_ticks(unsigned int rate
, unsigned int size
);
439 static unsigned int tc_buffer_per_jiffy(unsigned int rate
);
441 static struct tcmsg
*netdev_linux_tc_make_request(const struct netdev
*,
445 static int tc_add_policer(struct netdev
*,
446 uint32_t kbits_rate
, uint32_t kbits_burst
);
448 static int tc_parse_qdisc(const struct ofpbuf
*, const char **kind
,
449 struct nlattr
**options
);
450 static int tc_parse_class(const struct ofpbuf
*, unsigned int *queue_id
,
451 struct nlattr
**options
,
452 struct netdev_queue_stats
*);
453 static int tc_query_class(const struct netdev
*,
454 unsigned int handle
, unsigned int parent
,
455 struct ofpbuf
**replyp
);
456 static int tc_delete_class(const struct netdev
*, unsigned int handle
);
458 static int tc_del_qdisc(struct netdev
*netdev
);
459 static int tc_query_qdisc(const struct netdev
*netdev
);
461 static int tc_calc_cell_log(unsigned int mtu
);
462 static void tc_fill_rate(struct tc_ratespec
*rate
, uint64_t bps
, int mtu
);
463 static void tc_put_rtab(struct ofpbuf
*, uint16_t type
,
464 const struct tc_ratespec
*rate
);
465 static int tc_calc_buffer(unsigned int Bps
, int mtu
, uint64_t burst_bytes
);
467 struct netdev_linux
{
470 /* Protects all members below. */
471 struct ovs_mutex mutex
;
473 unsigned int cache_valid
;
475 bool miimon
; /* Link status of last poll. */
476 long long int miimon_interval
; /* Miimon Poll rate. Disabled if <= 0. */
477 struct timer miimon_timer
;
479 /* The following are figured out "on demand" only. They are only valid
480 * when the corresponding VALID_* bit in 'cache_valid' is set. */
482 struct eth_addr etheraddr
;
484 unsigned int ifi_flags
;
485 long long int carrier_resets
;
486 uint32_t kbits_rate
; /* Policing data. */
487 uint32_t kbits_burst
;
488 int vport_stats_error
; /* Cached error code from vport_get_stats().
489 0 or an errno value. */
490 int netdev_mtu_error
; /* Cached error code from SIOCGIFMTU or SIOCSIFMTU. */
491 int ether_addr_error
; /* Cached error code from set/get etheraddr. */
492 int netdev_policing_error
; /* Cached error code from set policing. */
493 int get_features_error
; /* Cached error code from ETHTOOL_GSET. */
494 int get_ifindex_error
; /* Cached error code from SIOCGIFINDEX. */
496 enum netdev_features current
; /* Cached from ETHTOOL_GSET. */
497 enum netdev_features advertised
; /* Cached from ETHTOOL_GSET. */
498 enum netdev_features supported
; /* Cached from ETHTOOL_GSET. */
500 struct ethtool_drvinfo drvinfo
; /* Cached from ETHTOOL_GDRVINFO. */
503 /* For devices of class netdev_tap_class only. */
507 struct netdev_rxq_linux
{
508 struct netdev_rxq up
;
513 /* This is set pretty low because we probably won't learn anything from the
514 * additional log messages. */
515 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 20);
517 /* Polling miimon status for all ports causes performance degradation when
518 * handling a large number of ports. If there are no devices using miimon, then
519 * we skip netdev_linux_miimon_run() and netdev_linux_miimon_wait().
521 * Readers do not depend on this variable synchronizing with the related
522 * changes in the device miimon status, so we can use atomic_count. */
523 static atomic_count miimon_cnt
= ATOMIC_COUNT_INIT(0);
525 static void netdev_linux_run(const struct netdev_class
*);
527 static int netdev_linux_do_ethtool(const char *name
, struct ethtool_cmd
*,
528 int cmd
, const char *cmd_name
);
529 static int get_flags(const struct netdev
*, unsigned int *flags
);
530 static int set_flags(const char *, unsigned int flags
);
531 static int update_flags(struct netdev_linux
*netdev
, enum netdev_flags off
,
532 enum netdev_flags on
, enum netdev_flags
*old_flagsp
)
533 OVS_REQUIRES(netdev
->mutex
);
534 static int get_ifindex(const struct netdev
*, int *ifindexp
);
535 static int do_set_addr(struct netdev
*netdev
,
536 int ioctl_nr
, const char *ioctl_name
,
537 struct in_addr addr
);
538 static int get_etheraddr(const char *netdev_name
, struct eth_addr
*ea
);
539 static int set_etheraddr(const char *netdev_name
, const struct eth_addr
);
540 static int get_stats_via_netlink(const struct netdev
*, struct netdev_stats
*);
541 static int af_packet_sock(void);
542 static bool netdev_linux_miimon_enabled(void);
543 static void netdev_linux_miimon_run(void);
544 static void netdev_linux_miimon_wait(void);
545 static int netdev_linux_get_mtu__(struct netdev_linux
*netdev
, int *mtup
);
548 is_netdev_linux_class(const struct netdev_class
*netdev_class
)
550 return netdev_class
->run
== netdev_linux_run
;
554 is_tap_netdev(const struct netdev
*netdev
)
556 return netdev_get_class(netdev
) == &netdev_tap_class
;
559 static struct netdev_linux
*
560 netdev_linux_cast(const struct netdev
*netdev
)
562 ovs_assert(is_netdev_linux_class(netdev_get_class(netdev
)));
564 return CONTAINER_OF(netdev
, struct netdev_linux
, up
);
567 static struct netdev_rxq_linux
*
568 netdev_rxq_linux_cast(const struct netdev_rxq
*rx
)
570 ovs_assert(is_netdev_linux_class(netdev_get_class(rx
->netdev
)));
571 return CONTAINER_OF(rx
, struct netdev_rxq_linux
, up
);
574 static void netdev_linux_update(struct netdev_linux
*netdev
,
575 const struct rtnetlink_change
*)
576 OVS_REQUIRES(netdev
->mutex
);
577 static void netdev_linux_changed(struct netdev_linux
*netdev
,
578 unsigned int ifi_flags
, unsigned int mask
)
579 OVS_REQUIRES(netdev
->mutex
);
581 /* Returns a NETLINK_ROUTE socket listening for RTNLGRP_LINK,
582 * RTNLGRP_IPV4_IFADDR and RTNLGRP_IPV6_IFADDR changes, or NULL
583 * if no such socket could be created. */
584 static struct nl_sock
*
585 netdev_linux_notify_sock(void)
587 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
588 static struct nl_sock
*sock
;
589 unsigned int mcgroups
[] = {RTNLGRP_LINK
, RTNLGRP_IPV4_IFADDR
,
590 RTNLGRP_IPV6_IFADDR
, RTNLGRP_IPV6_IFINFO
};
592 if (ovsthread_once_start(&once
)) {
595 error
= nl_sock_create(NETLINK_ROUTE
, &sock
);
599 for (i
= 0; i
< ARRAY_SIZE(mcgroups
); i
++) {
600 error
= nl_sock_join_mcgroup(sock
, mcgroups
[i
]);
602 nl_sock_destroy(sock
);
608 ovsthread_once_done(&once
);
615 netdev_linux_miimon_enabled(void)
617 return atomic_count_get(&miimon_cnt
) > 0;
621 netdev_linux_run(const struct netdev_class
*netdev_class OVS_UNUSED
)
623 struct nl_sock
*sock
;
626 if (netdev_linux_miimon_enabled()) {
627 netdev_linux_miimon_run();
630 sock
= netdev_linux_notify_sock();
636 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
637 uint64_t buf_stub
[4096 / 8];
640 ofpbuf_use_stub(&buf
, buf_stub
, sizeof buf_stub
);
641 error
= nl_sock_recv(sock
, &buf
, false);
643 struct rtnetlink_change change
;
645 if (rtnetlink_parse(&buf
, &change
)) {
646 struct netdev
*netdev_
= NULL
;
647 char dev_name
[IFNAMSIZ
];
649 if (!change
.ifname
) {
650 change
.ifname
= if_indextoname(change
.if_index
, dev_name
);
654 netdev_
= netdev_from_name(change
.ifname
);
656 if (netdev_
&& is_netdev_linux_class(netdev_
->netdev_class
)) {
657 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
659 ovs_mutex_lock(&netdev
->mutex
);
660 netdev_linux_update(netdev
, &change
);
661 ovs_mutex_unlock(&netdev
->mutex
);
663 netdev_close(netdev_
);
665 } else if (error
== ENOBUFS
) {
666 struct shash device_shash
;
667 struct shash_node
*node
;
671 shash_init(&device_shash
);
672 netdev_get_devices(&netdev_linux_class
, &device_shash
);
673 SHASH_FOR_EACH (node
, &device_shash
) {
674 struct netdev
*netdev_
= node
->data
;
675 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
678 ovs_mutex_lock(&netdev
->mutex
);
679 get_flags(netdev_
, &flags
);
680 netdev_linux_changed(netdev
, flags
, 0);
681 ovs_mutex_unlock(&netdev
->mutex
);
683 netdev_close(netdev_
);
685 shash_destroy(&device_shash
);
686 } else if (error
!= EAGAIN
) {
687 VLOG_WARN_RL(&rl
, "error reading or parsing netlink (%s)",
688 ovs_strerror(error
));
695 netdev_linux_wait(const struct netdev_class
*netdev_class OVS_UNUSED
)
697 struct nl_sock
*sock
;
699 if (netdev_linux_miimon_enabled()) {
700 netdev_linux_miimon_wait();
702 sock
= netdev_linux_notify_sock();
704 nl_sock_wait(sock
, POLLIN
);
709 netdev_linux_changed(struct netdev_linux
*dev
,
710 unsigned int ifi_flags
, unsigned int mask
)
711 OVS_REQUIRES(dev
->mutex
)
713 netdev_change_seq_changed(&dev
->up
);
715 if ((dev
->ifi_flags
^ ifi_flags
) & IFF_RUNNING
) {
716 dev
->carrier_resets
++;
718 dev
->ifi_flags
= ifi_flags
;
720 dev
->cache_valid
&= mask
;
721 if (!(mask
& VALID_IN
)) {
722 netdev_get_addrs_list_flush();
727 netdev_linux_update(struct netdev_linux
*dev
,
728 const struct rtnetlink_change
*change
)
729 OVS_REQUIRES(dev
->mutex
)
731 if (rtnetlink_type_is_rtnlgrp_link(change
->nlmsg_type
)){
732 if (change
->nlmsg_type
== RTM_NEWLINK
) {
733 /* Keep drv-info, and ip addresses. */
734 netdev_linux_changed(dev
, change
->ifi_flags
,
735 VALID_DRVINFO
| VALID_IN
);
737 /* Update netdev from rtnl-change msg. */
739 dev
->mtu
= change
->mtu
;
740 dev
->cache_valid
|= VALID_MTU
;
741 dev
->netdev_mtu_error
= 0;
744 if (!eth_addr_is_zero(change
->mac
)) {
745 dev
->etheraddr
= change
->mac
;
746 dev
->cache_valid
|= VALID_ETHERADDR
;
747 dev
->ether_addr_error
= 0;
750 dev
->ifindex
= change
->if_index
;
751 dev
->cache_valid
|= VALID_IFINDEX
;
752 dev
->get_ifindex_error
= 0;
754 netdev_linux_changed(dev
, change
->ifi_flags
, 0);
756 } else if (rtnetlink_type_is_rtnlgrp_addr(change
->nlmsg_type
)) {
757 /* Invalidates in4, in6. */
758 netdev_linux_changed(dev
, dev
->ifi_flags
, ~VALID_IN
);
764 static struct netdev
*
765 netdev_linux_alloc(void)
767 struct netdev_linux
*netdev
= xzalloc(sizeof *netdev
);
772 netdev_linux_common_construct(struct netdev
*netdev_
)
774 /* Prevent any attempt to create (or open) a network device named "default"
775 * or "all". These device names are effectively reserved on Linux because
776 * /proc/sys/net/ipv4/conf/ always contains directories by these names. By
777 * itself this wouldn't call for any special treatment, but in practice if
778 * a program tries to create devices with these names, it causes the kernel
779 * to fire a "new device" notification event even though creation failed,
780 * and in turn that causes OVS to wake up and try to create them again,
781 * which ends up as a 100% CPU loop. */
782 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
783 const char *name
= netdev_
->name
;
784 if (!strcmp(name
, "default") || !strcmp(name
, "all")) {
785 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 1);
786 VLOG_WARN_RL(&rl
, "%s: Linux forbids network device with this name",
791 ovs_mutex_init(&netdev
->mutex
);
795 /* Creates system and internal devices. */
797 netdev_linux_construct(struct netdev
*netdev_
)
799 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
800 int error
= netdev_linux_common_construct(netdev_
);
805 error
= get_flags(&netdev
->up
, &netdev
->ifi_flags
);
806 if (error
== ENODEV
) {
807 if (netdev
->up
.netdev_class
!= &netdev_internal_class
) {
808 /* The device does not exist, so don't allow it to be opened. */
811 /* "Internal" netdevs have to be created as netdev objects before
812 * they exist in the kernel, because creating them in the kernel
813 * happens by passing a netdev object to dpif_port_add().
814 * Therefore, ignore the error. */
821 /* For most types of netdevs we open the device for each call of
822 * netdev_open(). However, this is not the case with tap devices,
823 * since it is only possible to open the device once. In this
824 * situation we share a single file descriptor, and consequently
825 * buffers, across all readers. Therefore once data is read it will
826 * be unavailable to other reads for tap devices. */
828 netdev_linux_construct_tap(struct netdev
*netdev_
)
830 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
831 static const char tap_dev
[] = "/dev/net/tun";
832 const char *name
= netdev_
->name
;
835 int error
= netdev_linux_common_construct(netdev_
);
840 /* Open tap device. */
841 netdev
->tap_fd
= open(tap_dev
, O_RDWR
);
842 if (netdev
->tap_fd
< 0) {
844 VLOG_WARN("opening \"%s\" failed: %s", tap_dev
, ovs_strerror(error
));
848 /* Create tap device. */
849 get_flags(&netdev
->up
, &netdev
->ifi_flags
);
850 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
851 ovs_strzcpy(ifr
.ifr_name
, name
, sizeof ifr
.ifr_name
);
852 if (ioctl(netdev
->tap_fd
, TUNSETIFF
, &ifr
) == -1) {
853 VLOG_WARN("%s: creating tap device failed: %s", name
,
854 ovs_strerror(errno
));
859 /* Make non-blocking. */
860 error
= set_nonblocking(netdev
->tap_fd
);
865 if (ioctl(netdev
->tap_fd
, TUNSETPERSIST
, 1)) {
866 VLOG_WARN("%s: creating tap device failed (persist): %s", name
,
867 ovs_strerror(errno
));
875 close(netdev
->tap_fd
);
880 netdev_linux_destruct(struct netdev
*netdev_
)
882 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
884 if (netdev
->tc
&& netdev
->tc
->ops
->tc_destroy
) {
885 netdev
->tc
->ops
->tc_destroy(netdev
->tc
);
888 if (netdev_get_class(netdev_
) == &netdev_tap_class
889 && netdev
->tap_fd
>= 0)
891 ioctl(netdev
->tap_fd
, TUNSETPERSIST
, 0);
892 close(netdev
->tap_fd
);
895 if (netdev
->miimon_interval
> 0) {
896 atomic_count_dec(&miimon_cnt
);
899 ovs_mutex_destroy(&netdev
->mutex
);
903 netdev_linux_dealloc(struct netdev
*netdev_
)
905 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
909 static struct netdev_rxq
*
910 netdev_linux_rxq_alloc(void)
912 struct netdev_rxq_linux
*rx
= xzalloc(sizeof *rx
);
917 netdev_linux_rxq_construct(struct netdev_rxq
*rxq_
)
919 struct netdev_rxq_linux
*rx
= netdev_rxq_linux_cast(rxq_
);
920 struct netdev
*netdev_
= rx
->up
.netdev
;
921 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
924 ovs_mutex_lock(&netdev
->mutex
);
925 rx
->is_tap
= is_tap_netdev(netdev_
);
927 rx
->fd
= netdev
->tap_fd
;
929 struct sockaddr_ll sll
;
931 /* Result of tcpdump -dd inbound */
932 static const struct sock_filter filt
[] = {
933 { 0x28, 0, 0, 0xfffff004 }, /* ldh [0] */
934 { 0x15, 0, 1, 0x00000004 }, /* jeq #4 jt 2 jf 3 */
935 { 0x6, 0, 0, 0x00000000 }, /* ret #0 */
936 { 0x6, 0, 0, 0x0000ffff } /* ret #65535 */
938 static const struct sock_fprog fprog
= {
939 ARRAY_SIZE(filt
), (struct sock_filter
*) filt
942 /* Create file descriptor. */
943 rx
->fd
= socket(PF_PACKET
, SOCK_RAW
, 0);
946 VLOG_ERR("failed to create raw socket (%s)", ovs_strerror(error
));
951 if (setsockopt(rx
->fd
, SOL_PACKET
, PACKET_AUXDATA
, &val
, sizeof val
)) {
953 VLOG_ERR("%s: failed to mark socket for auxdata (%s)",
954 netdev_get_name(netdev_
), ovs_strerror(error
));
958 /* Set non-blocking mode. */
959 error
= set_nonblocking(rx
->fd
);
964 /* Get ethernet device index. */
965 error
= get_ifindex(&netdev
->up
, &ifindex
);
970 /* Bind to specific ethernet device. */
971 memset(&sll
, 0, sizeof sll
);
972 sll
.sll_family
= AF_PACKET
;
973 sll
.sll_ifindex
= ifindex
;
974 sll
.sll_protocol
= htons(ETH_P_ALL
);
975 if (bind(rx
->fd
, (struct sockaddr
*) &sll
, sizeof sll
) < 0) {
977 VLOG_ERR("%s: failed to bind raw socket (%s)",
978 netdev_get_name(netdev_
), ovs_strerror(error
));
982 /* Filter for only inbound packets. */
983 error
= setsockopt(rx
->fd
, SOL_SOCKET
, SO_ATTACH_FILTER
, &fprog
,
987 VLOG_ERR("%s: failed to attach filter (%s)",
988 netdev_get_name(netdev_
), ovs_strerror(error
));
992 ovs_mutex_unlock(&netdev
->mutex
);
1000 ovs_mutex_unlock(&netdev
->mutex
);
1005 netdev_linux_rxq_destruct(struct netdev_rxq
*rxq_
)
1007 struct netdev_rxq_linux
*rx
= netdev_rxq_linux_cast(rxq_
);
1015 netdev_linux_rxq_dealloc(struct netdev_rxq
*rxq_
)
1017 struct netdev_rxq_linux
*rx
= netdev_rxq_linux_cast(rxq_
);
1023 auxdata_to_vlan_tpid(const struct tpacket_auxdata
*aux
, bool double_tagged
)
1025 if (aux
->tp_status
& TP_STATUS_VLAN_TPID_VALID
) {
1026 return htons(aux
->tp_vlan_tpid
);
1027 } else if (double_tagged
) {
1028 return htons(ETH_TYPE_VLAN_8021AD
);
1030 return htons(ETH_TYPE_VLAN_8021Q
);
1035 auxdata_has_vlan_tci(const struct tpacket_auxdata
*aux
)
1037 return aux
->tp_vlan_tci
|| aux
->tp_status
& TP_STATUS_VLAN_VALID
;
1041 netdev_linux_rxq_recv_sock(int fd
, struct dp_packet
*buffer
)
1046 struct cmsghdr
*cmsg
;
1048 struct cmsghdr cmsg
;
1049 char buffer
[CMSG_SPACE(sizeof(struct tpacket_auxdata
))];
1053 /* Reserve headroom for a single VLAN tag */
1054 dp_packet_reserve(buffer
, VLAN_HEADER_LEN
);
1055 size
= dp_packet_tailroom(buffer
);
1057 iov
.iov_base
= dp_packet_data(buffer
);
1059 msgh
.msg_name
= NULL
;
1060 msgh
.msg_namelen
= 0;
1061 msgh
.msg_iov
= &iov
;
1062 msgh
.msg_iovlen
= 1;
1063 msgh
.msg_control
= &cmsg_buffer
;
1064 msgh
.msg_controllen
= sizeof cmsg_buffer
;
1068 retval
= recvmsg(fd
, &msgh
, MSG_TRUNC
);
1069 } while (retval
< 0 && errno
== EINTR
);
1073 } else if (retval
> size
) {
1077 dp_packet_set_size(buffer
, dp_packet_size(buffer
) + retval
);
1079 for (cmsg
= CMSG_FIRSTHDR(&msgh
); cmsg
; cmsg
= CMSG_NXTHDR(&msgh
, cmsg
)) {
1080 const struct tpacket_auxdata
*aux
;
1082 if (cmsg
->cmsg_level
!= SOL_PACKET
1083 || cmsg
->cmsg_type
!= PACKET_AUXDATA
1084 || cmsg
->cmsg_len
< CMSG_LEN(sizeof(struct tpacket_auxdata
))) {
1088 aux
= ALIGNED_CAST(struct tpacket_auxdata
*, CMSG_DATA(cmsg
));
1089 if (auxdata_has_vlan_tci(aux
)) {
1090 struct eth_header
*eth
;
1093 if (retval
< ETH_HEADER_LEN
) {
1097 eth
= dp_packet_data(buffer
);
1098 double_tagged
= eth
->eth_type
== htons(ETH_TYPE_VLAN_8021Q
);
1100 eth_push_vlan(buffer
, auxdata_to_vlan_tpid(aux
, double_tagged
),
1101 htons(aux
->tp_vlan_tci
));
1110 netdev_linux_rxq_recv_tap(int fd
, struct dp_packet
*buffer
)
1113 size_t size
= dp_packet_tailroom(buffer
);
1116 retval
= read(fd
, dp_packet_data(buffer
), size
);
1117 } while (retval
< 0 && errno
== EINTR
);
1123 dp_packet_set_size(buffer
, dp_packet_size(buffer
) + retval
);
1128 netdev_linux_rxq_recv(struct netdev_rxq
*rxq_
, struct dp_packet_batch
*batch
)
1130 struct netdev_rxq_linux
*rx
= netdev_rxq_linux_cast(rxq_
);
1131 struct netdev
*netdev
= rx
->up
.netdev
;
1132 struct dp_packet
*buffer
;
1136 if (netdev_linux_get_mtu__(netdev_linux_cast(netdev
), &mtu
)) {
1137 mtu
= ETH_PAYLOAD_MAX
;
1140 /* Assume Ethernet port. No need to set packet_type. */
1141 buffer
= dp_packet_new_with_headroom(VLAN_ETH_HEADER_LEN
+ mtu
,
1142 DP_NETDEV_HEADROOM
);
1143 retval
= (rx
->is_tap
1144 ? netdev_linux_rxq_recv_tap(rx
->fd
, buffer
)
1145 : netdev_linux_rxq_recv_sock(rx
->fd
, buffer
));
1148 if (retval
!= EAGAIN
&& retval
!= EMSGSIZE
) {
1149 VLOG_WARN_RL(&rl
, "error receiving Ethernet packet on %s: %s",
1150 netdev_rxq_get_name(rxq_
), ovs_strerror(errno
));
1152 dp_packet_delete(buffer
);
1154 dp_packet_batch_init_packet(batch
, buffer
);
1161 netdev_linux_rxq_wait(struct netdev_rxq
*rxq_
)
1163 struct netdev_rxq_linux
*rx
= netdev_rxq_linux_cast(rxq_
);
1164 poll_fd_wait(rx
->fd
, POLLIN
);
1168 netdev_linux_rxq_drain(struct netdev_rxq
*rxq_
)
1170 struct netdev_rxq_linux
*rx
= netdev_rxq_linux_cast(rxq_
);
1173 int error
= af_inet_ifreq_ioctl(netdev_rxq_get_name(rxq_
), &ifr
,
1174 SIOCGIFTXQLEN
, "SIOCGIFTXQLEN");
1178 drain_fd(rx
->fd
, ifr
.ifr_qlen
);
1181 return drain_rcvbuf(rx
->fd
);
1186 netdev_linux_sock_batch_send(int sock
, int ifindex
,
1187 struct dp_packet_batch
*batch
)
1189 const size_t size
= dp_packet_batch_size(batch
);
1190 /* We don't bother setting most fields in sockaddr_ll because the
1191 * kernel ignores them for SOCK_RAW. */
1192 struct sockaddr_ll sll
= { .sll_family
= AF_PACKET
,
1193 .sll_ifindex
= ifindex
};
1195 struct mmsghdr
*mmsg
= xmalloc(sizeof(*mmsg
) * size
);
1196 struct iovec
*iov
= xmalloc(sizeof(*iov
) * size
);
1198 struct dp_packet
*packet
;
1199 DP_PACKET_BATCH_FOR_EACH (packet
, batch
) {
1200 iov
[i
].iov_base
= dp_packet_data(packet
);
1201 iov
[i
].iov_len
= dp_packet_get_send_len(packet
);
1202 mmsg
[i
].msg_hdr
= (struct msghdr
) { .msg_name
= &sll
,
1203 .msg_namelen
= sizeof sll
,
1209 for (uint32_t ofs
= 0; ofs
< size
; ) {
1212 retval
= sendmmsg(sock
, mmsg
+ ofs
, size
- ofs
, 0);
1213 error
= retval
< 0 ? errno
: 0;
1214 } while (error
== EINTR
);
1226 /* Use the tap fd to send 'batch' to tap device 'netdev'. Using the tap fd is
1227 * essential, because packets sent to a tap device with an AF_PACKET socket
1228 * will loop back to be *received* again on the tap device. This doesn't occur
1229 * on other interface types because we attach a socket filter to the rx
1232 netdev_linux_tap_batch_send(struct netdev
*netdev_
,
1233 struct dp_packet_batch
*batch
)
1235 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
1236 struct dp_packet
*packet
;
1237 DP_PACKET_BATCH_FOR_EACH (packet
, batch
) {
1238 size_t size
= dp_packet_get_send_len(packet
);
1243 retval
= write(netdev
->tap_fd
, dp_packet_data(packet
), size
);
1244 error
= retval
< 0 ? errno
: 0;
1245 } while (error
== EINTR
);
1248 /* The Linux tap driver returns EIO if the device is not up. From
1249 * the OVS side this is not an error, so we ignore it; otherwise,
1250 * return the erro. */
1254 } else if (retval
!= size
) {
1255 VLOG_WARN_RL(&rl
, "sent partial Ethernet packet (%"PRIuSIZE
" "
1256 "bytes of %"PRIuSIZE
") on %s",
1257 retval
, size
, netdev_get_name(netdev_
));
1264 /* Sends 'batch' on 'netdev'. Returns 0 if successful, otherwise a positive
1265 * errno value. Returns EAGAIN without blocking if the packet cannot be queued
1266 * immediately. Returns EMSGSIZE if a partial packet was transmitted or if
1267 * the packet is too big or too small to transmit on the device.
1269 * The kernel maintains a packet transmission queue, so the caller is not
1270 * expected to do additional queuing of packets. */
1272 netdev_linux_send(struct netdev
*netdev_
, int qid OVS_UNUSED
,
1273 struct dp_packet_batch
*batch
, bool may_steal
,
1274 bool concurrent_txq OVS_UNUSED
)
1279 if (!is_tap_netdev(netdev_
)) {
1280 sock
= af_packet_sock();
1286 int ifindex
= netdev_get_ifindex(netdev_
);
1292 error
= netdev_linux_sock_batch_send(sock
, ifindex
, batch
);
1294 error
= netdev_linux_tap_batch_send(netdev_
, batch
);
1297 if (error
== ENOBUFS
) {
1298 /* The Linux AF_PACKET implementation never blocks waiting
1299 * for room for packets, instead returning ENOBUFS.
1300 * Translate this into EAGAIN for the caller. */
1303 VLOG_WARN_RL(&rl
, "error sending Ethernet packet on %s: %s",
1304 netdev_get_name(netdev_
), ovs_strerror(error
));
1309 dp_packet_delete_batch(batch
, may_steal
);
1313 /* Registers with the poll loop to wake up from the next call to poll_block()
1314 * when the packet transmission queue has sufficient room to transmit a packet
1315 * with netdev_send().
1317 * The kernel maintains a packet transmission queue, so the client is not
1318 * expected to do additional queuing of packets. Thus, this function is
1319 * unlikely to ever be used. It is included for completeness. */
1321 netdev_linux_send_wait(struct netdev
*netdev
, int qid OVS_UNUSED
)
1323 if (is_tap_netdev(netdev
)) {
1324 /* TAP device always accepts packets.*/
1325 poll_immediate_wake();
1329 /* Attempts to set 'netdev''s MAC address to 'mac'. Returns 0 if successful,
1330 * otherwise a positive errno value. */
1332 netdev_linux_set_etheraddr(struct netdev
*netdev_
, const struct eth_addr mac
)
1334 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
1335 enum netdev_flags old_flags
= 0;
1338 ovs_mutex_lock(&netdev
->mutex
);
1340 if (netdev
->cache_valid
& VALID_ETHERADDR
) {
1341 error
= netdev
->ether_addr_error
;
1342 if (error
|| eth_addr_equals(netdev
->etheraddr
, mac
)) {
1345 netdev
->cache_valid
&= ~VALID_ETHERADDR
;
1348 /* Tap devices must be brought down before setting the address. */
1349 if (is_tap_netdev(netdev_
)) {
1350 update_flags(netdev
, NETDEV_UP
, 0, &old_flags
);
1352 error
= set_etheraddr(netdev_get_name(netdev_
), mac
);
1353 if (!error
|| error
== ENODEV
) {
1354 netdev
->ether_addr_error
= error
;
1355 netdev
->cache_valid
|= VALID_ETHERADDR
;
1357 netdev
->etheraddr
= mac
;
1361 if (is_tap_netdev(netdev_
) && old_flags
& NETDEV_UP
) {
1362 update_flags(netdev
, 0, NETDEV_UP
, &old_flags
);
1366 ovs_mutex_unlock(&netdev
->mutex
);
1370 /* Copies 'netdev''s MAC address to 'mac' which is passed as param. */
1372 netdev_linux_get_etheraddr(const struct netdev
*netdev_
, struct eth_addr
*mac
)
1374 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
1377 ovs_mutex_lock(&netdev
->mutex
);
1378 if (!(netdev
->cache_valid
& VALID_ETHERADDR
)) {
1379 netdev
->ether_addr_error
= get_etheraddr(netdev_get_name(netdev_
),
1380 &netdev
->etheraddr
);
1381 netdev
->cache_valid
|= VALID_ETHERADDR
;
1384 error
= netdev
->ether_addr_error
;
1386 *mac
= netdev
->etheraddr
;
1388 ovs_mutex_unlock(&netdev
->mutex
);
1394 netdev_linux_get_mtu__(struct netdev_linux
*netdev
, int *mtup
)
1398 if (!(netdev
->cache_valid
& VALID_MTU
)) {
1401 netdev
->netdev_mtu_error
= af_inet_ifreq_ioctl(
1402 netdev_get_name(&netdev
->up
), &ifr
, SIOCGIFMTU
, "SIOCGIFMTU");
1403 netdev
->mtu
= ifr
.ifr_mtu
;
1404 netdev
->cache_valid
|= VALID_MTU
;
1407 error
= netdev
->netdev_mtu_error
;
1409 *mtup
= netdev
->mtu
;
1415 /* Returns the maximum size of transmitted (and received) packets on 'netdev',
1416 * in bytes, not including the hardware header; thus, this is typically 1500
1417 * bytes for Ethernet devices. */
1419 netdev_linux_get_mtu(const struct netdev
*netdev_
, int *mtup
)
1421 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
1424 ovs_mutex_lock(&netdev
->mutex
);
1425 error
= netdev_linux_get_mtu__(netdev
, mtup
);
1426 ovs_mutex_unlock(&netdev
->mutex
);
1431 /* Sets the maximum size of transmitted (MTU) for given device using linux
1432 * networking ioctl interface.
1435 netdev_linux_set_mtu(struct netdev
*netdev_
, int mtu
)
1437 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
1441 ovs_mutex_lock(&netdev
->mutex
);
1442 if (netdev
->cache_valid
& VALID_MTU
) {
1443 error
= netdev
->netdev_mtu_error
;
1444 if (error
|| netdev
->mtu
== mtu
) {
1447 netdev
->cache_valid
&= ~VALID_MTU
;
1450 error
= af_inet_ifreq_ioctl(netdev_get_name(netdev_
), &ifr
,
1451 SIOCSIFMTU
, "SIOCSIFMTU");
1452 if (!error
|| error
== ENODEV
) {
1453 netdev
->netdev_mtu_error
= error
;
1454 netdev
->mtu
= ifr
.ifr_mtu
;
1455 netdev
->cache_valid
|= VALID_MTU
;
1458 ovs_mutex_unlock(&netdev
->mutex
);
1462 /* Returns the ifindex of 'netdev', if successful, as a positive number.
1463 * On failure, returns a negative errno value. */
1465 netdev_linux_get_ifindex(const struct netdev
*netdev_
)
1467 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
1470 ovs_mutex_lock(&netdev
->mutex
);
1471 error
= get_ifindex(netdev_
, &ifindex
);
1472 ovs_mutex_unlock(&netdev
->mutex
);
1474 return error
? -error
: ifindex
;
1478 netdev_linux_get_carrier(const struct netdev
*netdev_
, bool *carrier
)
1480 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
1482 ovs_mutex_lock(&netdev
->mutex
);
1483 if (netdev
->miimon_interval
> 0) {
1484 *carrier
= netdev
->miimon
;
1486 *carrier
= (netdev
->ifi_flags
& IFF_RUNNING
) != 0;
1488 ovs_mutex_unlock(&netdev
->mutex
);
1493 static long long int
1494 netdev_linux_get_carrier_resets(const struct netdev
*netdev_
)
1496 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
1497 long long int carrier_resets
;
1499 ovs_mutex_lock(&netdev
->mutex
);
1500 carrier_resets
= netdev
->carrier_resets
;
1501 ovs_mutex_unlock(&netdev
->mutex
);
1503 return carrier_resets
;
1507 netdev_linux_do_miimon(const char *name
, int cmd
, const char *cmd_name
,
1508 struct mii_ioctl_data
*data
)
1513 memset(&ifr
, 0, sizeof ifr
);
1514 memcpy(&ifr
.ifr_data
, data
, sizeof *data
);
1515 error
= af_inet_ifreq_ioctl(name
, &ifr
, cmd
, cmd_name
);
1516 memcpy(data
, &ifr
.ifr_data
, sizeof *data
);
1522 netdev_linux_get_miimon(const char *name
, bool *miimon
)
1524 struct mii_ioctl_data data
;
1529 memset(&data
, 0, sizeof data
);
1530 error
= netdev_linux_do_miimon(name
, SIOCGMIIPHY
, "SIOCGMIIPHY", &data
);
1532 /* data.phy_id is filled out by previous SIOCGMIIPHY miimon call. */
1533 data
.reg_num
= MII_BMSR
;
1534 error
= netdev_linux_do_miimon(name
, SIOCGMIIREG
, "SIOCGMIIREG",
1538 *miimon
= !!(data
.val_out
& BMSR_LSTATUS
);
1542 struct ethtool_cmd ecmd
;
1544 VLOG_DBG_RL(&rl
, "%s: failed to query MII, falling back to ethtool",
1547 COVERAGE_INC(netdev_get_ethtool
);
1548 memset(&ecmd
, 0, sizeof ecmd
);
1549 error
= netdev_linux_do_ethtool(name
, &ecmd
, ETHTOOL_GLINK
,
1552 struct ethtool_value eval
;
1554 memcpy(&eval
, &ecmd
, sizeof eval
);
1555 *miimon
= !!eval
.data
;
1557 VLOG_WARN_RL(&rl
, "%s: ethtool link status failed", name
);
1565 netdev_linux_set_miimon_interval(struct netdev
*netdev_
,
1566 long long int interval
)
1568 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
1570 ovs_mutex_lock(&netdev
->mutex
);
1571 interval
= interval
> 0 ? MAX(interval
, 100) : 0;
1572 if (netdev
->miimon_interval
!= interval
) {
1573 if (interval
&& !netdev
->miimon_interval
) {
1574 atomic_count_inc(&miimon_cnt
);
1575 } else if (!interval
&& netdev
->miimon_interval
) {
1576 atomic_count_dec(&miimon_cnt
);
1579 netdev
->miimon_interval
= interval
;
1580 timer_set_expired(&netdev
->miimon_timer
);
1582 ovs_mutex_unlock(&netdev
->mutex
);
1588 netdev_linux_miimon_run(void)
1590 struct shash device_shash
;
1591 struct shash_node
*node
;
1593 shash_init(&device_shash
);
1594 netdev_get_devices(&netdev_linux_class
, &device_shash
);
1595 SHASH_FOR_EACH (node
, &device_shash
) {
1596 struct netdev
*netdev
= node
->data
;
1597 struct netdev_linux
*dev
= netdev_linux_cast(netdev
);
1600 ovs_mutex_lock(&dev
->mutex
);
1601 if (dev
->miimon_interval
> 0 && timer_expired(&dev
->miimon_timer
)) {
1602 netdev_linux_get_miimon(dev
->up
.name
, &miimon
);
1603 if (miimon
!= dev
->miimon
) {
1604 dev
->miimon
= miimon
;
1605 netdev_linux_changed(dev
, dev
->ifi_flags
, 0);
1608 timer_set_duration(&dev
->miimon_timer
, dev
->miimon_interval
);
1610 ovs_mutex_unlock(&dev
->mutex
);
1611 netdev_close(netdev
);
1614 shash_destroy(&device_shash
);
1618 netdev_linux_miimon_wait(void)
1620 struct shash device_shash
;
1621 struct shash_node
*node
;
1623 shash_init(&device_shash
);
1624 netdev_get_devices(&netdev_linux_class
, &device_shash
);
1625 SHASH_FOR_EACH (node
, &device_shash
) {
1626 struct netdev
*netdev
= node
->data
;
1627 struct netdev_linux
*dev
= netdev_linux_cast(netdev
);
1629 ovs_mutex_lock(&dev
->mutex
);
1630 if (dev
->miimon_interval
> 0) {
1631 timer_wait(&dev
->miimon_timer
);
1633 ovs_mutex_unlock(&dev
->mutex
);
1634 netdev_close(netdev
);
1636 shash_destroy(&device_shash
);
1640 swap_uint64(uint64_t *a
, uint64_t *b
)
1647 /* Copies 'src' into 'dst', performing format conversion in the process.
1649 * 'src' is allowed to be misaligned. */
1651 netdev_stats_from_ovs_vport_stats(struct netdev_stats
*dst
,
1652 const struct ovs_vport_stats
*src
)
1654 dst
->rx_packets
= get_32aligned_u64(&src
->rx_packets
);
1655 dst
->tx_packets
= get_32aligned_u64(&src
->tx_packets
);
1656 dst
->rx_bytes
= get_32aligned_u64(&src
->rx_bytes
);
1657 dst
->tx_bytes
= get_32aligned_u64(&src
->tx_bytes
);
1658 dst
->rx_errors
= get_32aligned_u64(&src
->rx_errors
);
1659 dst
->tx_errors
= get_32aligned_u64(&src
->tx_errors
);
1660 dst
->rx_dropped
= get_32aligned_u64(&src
->rx_dropped
);
1661 dst
->tx_dropped
= get_32aligned_u64(&src
->tx_dropped
);
1663 dst
->collisions
= 0;
1664 dst
->rx_length_errors
= 0;
1665 dst
->rx_over_errors
= 0;
1666 dst
->rx_crc_errors
= 0;
1667 dst
->rx_frame_errors
= 0;
1668 dst
->rx_fifo_errors
= 0;
1669 dst
->rx_missed_errors
= 0;
1670 dst
->tx_aborted_errors
= 0;
1671 dst
->tx_carrier_errors
= 0;
1672 dst
->tx_fifo_errors
= 0;
1673 dst
->tx_heartbeat_errors
= 0;
1674 dst
->tx_window_errors
= 0;
1678 get_stats_via_vport__(const struct netdev
*netdev
, struct netdev_stats
*stats
)
1680 struct dpif_netlink_vport reply
;
1684 error
= dpif_netlink_vport_get(netdev_get_name(netdev
), &reply
, &buf
);
1687 } else if (!reply
.stats
) {
1692 netdev_stats_from_ovs_vport_stats(stats
, reply
.stats
);
1700 get_stats_via_vport(const struct netdev
*netdev_
,
1701 struct netdev_stats
*stats
)
1703 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
1705 if (!netdev
->vport_stats_error
||
1706 !(netdev
->cache_valid
& VALID_VPORT_STAT_ERROR
)) {
1709 error
= get_stats_via_vport__(netdev_
, stats
);
1710 if (error
&& error
!= ENOENT
&& error
!= ENODEV
) {
1711 VLOG_WARN_RL(&rl
, "%s: obtaining netdev stats via vport failed "
1713 netdev_get_name(netdev_
), ovs_strerror(error
));
1715 netdev
->vport_stats_error
= error
;
1716 netdev
->cache_valid
|= VALID_VPORT_STAT_ERROR
;
1720 /* Retrieves current device stats for 'netdev-linux'. */
1722 netdev_linux_get_stats(const struct netdev
*netdev_
,
1723 struct netdev_stats
*stats
)
1725 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
1726 struct netdev_stats dev_stats
;
1729 ovs_mutex_lock(&netdev
->mutex
);
1730 get_stats_via_vport(netdev_
, stats
);
1731 error
= get_stats_via_netlink(netdev_
, &dev_stats
);
1733 if (!netdev
->vport_stats_error
) {
1736 } else if (netdev
->vport_stats_error
) {
1737 /* stats not available from OVS then use netdev stats. */
1740 /* Use kernel netdev's packet and byte counts since vport's counters
1741 * do not reflect packet counts on the wire when GSO, TSO or GRO are
1743 stats
->rx_packets
= dev_stats
.rx_packets
;
1744 stats
->rx_bytes
= dev_stats
.rx_bytes
;
1745 stats
->tx_packets
= dev_stats
.tx_packets
;
1746 stats
->tx_bytes
= dev_stats
.tx_bytes
;
1748 stats
->rx_errors
+= dev_stats
.rx_errors
;
1749 stats
->tx_errors
+= dev_stats
.tx_errors
;
1750 stats
->rx_dropped
+= dev_stats
.rx_dropped
;
1751 stats
->tx_dropped
+= dev_stats
.tx_dropped
;
1752 stats
->multicast
+= dev_stats
.multicast
;
1753 stats
->collisions
+= dev_stats
.collisions
;
1754 stats
->rx_length_errors
+= dev_stats
.rx_length_errors
;
1755 stats
->rx_over_errors
+= dev_stats
.rx_over_errors
;
1756 stats
->rx_crc_errors
+= dev_stats
.rx_crc_errors
;
1757 stats
->rx_frame_errors
+= dev_stats
.rx_frame_errors
;
1758 stats
->rx_fifo_errors
+= dev_stats
.rx_fifo_errors
;
1759 stats
->rx_missed_errors
+= dev_stats
.rx_missed_errors
;
1760 stats
->tx_aborted_errors
+= dev_stats
.tx_aborted_errors
;
1761 stats
->tx_carrier_errors
+= dev_stats
.tx_carrier_errors
;
1762 stats
->tx_fifo_errors
+= dev_stats
.tx_fifo_errors
;
1763 stats
->tx_heartbeat_errors
+= dev_stats
.tx_heartbeat_errors
;
1764 stats
->tx_window_errors
+= dev_stats
.tx_window_errors
;
1766 ovs_mutex_unlock(&netdev
->mutex
);
1771 /* Retrieves current device stats for 'netdev-tap' netdev or
1772 * netdev-internal. */
1774 netdev_tap_get_stats(const struct netdev
*netdev_
, struct netdev_stats
*stats
)
1776 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
1777 struct netdev_stats dev_stats
;
1780 ovs_mutex_lock(&netdev
->mutex
);
1781 get_stats_via_vport(netdev_
, stats
);
1782 error
= get_stats_via_netlink(netdev_
, &dev_stats
);
1784 if (!netdev
->vport_stats_error
) {
1787 } else if (netdev
->vport_stats_error
) {
1788 /* Transmit and receive stats will appear to be swapped relative to the
1789 * other ports since we are the one sending the data, not a remote
1790 * computer. For consistency, we swap them back here. This does not
1791 * apply if we are getting stats from the vport layer because it always
1792 * tracks stats from the perspective of the switch. */
1795 swap_uint64(&stats
->rx_packets
, &stats
->tx_packets
);
1796 swap_uint64(&stats
->rx_bytes
, &stats
->tx_bytes
);
1797 swap_uint64(&stats
->rx_errors
, &stats
->tx_errors
);
1798 swap_uint64(&stats
->rx_dropped
, &stats
->tx_dropped
);
1799 stats
->rx_length_errors
= 0;
1800 stats
->rx_over_errors
= 0;
1801 stats
->rx_crc_errors
= 0;
1802 stats
->rx_frame_errors
= 0;
1803 stats
->rx_fifo_errors
= 0;
1804 stats
->rx_missed_errors
= 0;
1805 stats
->tx_aborted_errors
= 0;
1806 stats
->tx_carrier_errors
= 0;
1807 stats
->tx_fifo_errors
= 0;
1808 stats
->tx_heartbeat_errors
= 0;
1809 stats
->tx_window_errors
= 0;
1811 /* Use kernel netdev's packet and byte counts since vport counters
1812 * do not reflect packet counts on the wire when GSO, TSO or GRO
1814 stats
->rx_packets
= dev_stats
.tx_packets
;
1815 stats
->rx_bytes
= dev_stats
.tx_bytes
;
1816 stats
->tx_packets
= dev_stats
.rx_packets
;
1817 stats
->tx_bytes
= dev_stats
.rx_bytes
;
1819 stats
->rx_dropped
+= dev_stats
.tx_dropped
;
1820 stats
->tx_dropped
+= dev_stats
.rx_dropped
;
1822 stats
->rx_errors
+= dev_stats
.tx_errors
;
1823 stats
->tx_errors
+= dev_stats
.rx_errors
;
1825 stats
->multicast
+= dev_stats
.multicast
;
1826 stats
->collisions
+= dev_stats
.collisions
;
1828 ovs_mutex_unlock(&netdev
->mutex
);
1834 netdev_internal_get_stats(const struct netdev
*netdev_
,
1835 struct netdev_stats
*stats
)
1837 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
1840 ovs_mutex_lock(&netdev
->mutex
);
1841 get_stats_via_vport(netdev_
, stats
);
1842 error
= netdev
->vport_stats_error
;
1843 ovs_mutex_unlock(&netdev
->mutex
);
1849 netdev_linux_read_features(struct netdev_linux
*netdev
)
1851 struct ethtool_cmd ecmd
;
1855 if (netdev
->cache_valid
& VALID_FEATURES
) {
1859 COVERAGE_INC(netdev_get_ethtool
);
1860 memset(&ecmd
, 0, sizeof ecmd
);
1861 error
= netdev_linux_do_ethtool(netdev
->up
.name
, &ecmd
,
1862 ETHTOOL_GSET
, "ETHTOOL_GSET");
1867 /* Supported features. */
1868 netdev
->supported
= 0;
1869 if (ecmd
.supported
& SUPPORTED_10baseT_Half
) {
1870 netdev
->supported
|= NETDEV_F_10MB_HD
;
1872 if (ecmd
.supported
& SUPPORTED_10baseT_Full
) {
1873 netdev
->supported
|= NETDEV_F_10MB_FD
;
1875 if (ecmd
.supported
& SUPPORTED_100baseT_Half
) {
1876 netdev
->supported
|= NETDEV_F_100MB_HD
;
1878 if (ecmd
.supported
& SUPPORTED_100baseT_Full
) {
1879 netdev
->supported
|= NETDEV_F_100MB_FD
;
1881 if (ecmd
.supported
& SUPPORTED_1000baseT_Half
) {
1882 netdev
->supported
|= NETDEV_F_1GB_HD
;
1884 if ((ecmd
.supported
& SUPPORTED_1000baseT_Full
) ||
1885 (ecmd
.supported
& SUPPORTED_1000baseKX_Full
)) {
1886 netdev
->supported
|= NETDEV_F_1GB_FD
;
1888 if ((ecmd
.supported
& SUPPORTED_10000baseT_Full
) ||
1889 (ecmd
.supported
& SUPPORTED_10000baseKX4_Full
) ||
1890 (ecmd
.supported
& SUPPORTED_10000baseKR_Full
) ||
1891 (ecmd
.supported
& SUPPORTED_10000baseR_FEC
)) {
1892 netdev
->supported
|= NETDEV_F_10GB_FD
;
1894 if ((ecmd
.supported
& SUPPORTED_40000baseKR4_Full
) ||
1895 (ecmd
.supported
& SUPPORTED_40000baseCR4_Full
) ||
1896 (ecmd
.supported
& SUPPORTED_40000baseSR4_Full
) ||
1897 (ecmd
.supported
& SUPPORTED_40000baseLR4_Full
)) {
1898 netdev
->supported
|= NETDEV_F_40GB_FD
;
1900 if (ecmd
.supported
& SUPPORTED_TP
) {
1901 netdev
->supported
|= NETDEV_F_COPPER
;
1903 if (ecmd
.supported
& SUPPORTED_FIBRE
) {
1904 netdev
->supported
|= NETDEV_F_FIBER
;
1906 if (ecmd
.supported
& SUPPORTED_Autoneg
) {
1907 netdev
->supported
|= NETDEV_F_AUTONEG
;
1909 if (ecmd
.supported
& SUPPORTED_Pause
) {
1910 netdev
->supported
|= NETDEV_F_PAUSE
;
1912 if (ecmd
.supported
& SUPPORTED_Asym_Pause
) {
1913 netdev
->supported
|= NETDEV_F_PAUSE_ASYM
;
1916 /* Advertised features. */
1917 netdev
->advertised
= 0;
1918 if (ecmd
.advertising
& ADVERTISED_10baseT_Half
) {
1919 netdev
->advertised
|= NETDEV_F_10MB_HD
;
1921 if (ecmd
.advertising
& ADVERTISED_10baseT_Full
) {
1922 netdev
->advertised
|= NETDEV_F_10MB_FD
;
1924 if (ecmd
.advertising
& ADVERTISED_100baseT_Half
) {
1925 netdev
->advertised
|= NETDEV_F_100MB_HD
;
1927 if (ecmd
.advertising
& ADVERTISED_100baseT_Full
) {
1928 netdev
->advertised
|= NETDEV_F_100MB_FD
;
1930 if (ecmd
.advertising
& ADVERTISED_1000baseT_Half
) {
1931 netdev
->advertised
|= NETDEV_F_1GB_HD
;
1933 if ((ecmd
.advertising
& ADVERTISED_1000baseT_Full
) ||
1934 (ecmd
.advertising
& ADVERTISED_1000baseKX_Full
)) {
1935 netdev
->advertised
|= NETDEV_F_1GB_FD
;
1937 if ((ecmd
.advertising
& ADVERTISED_10000baseT_Full
) ||
1938 (ecmd
.advertising
& ADVERTISED_10000baseKX4_Full
) ||
1939 (ecmd
.advertising
& ADVERTISED_10000baseKR_Full
) ||
1940 (ecmd
.advertising
& ADVERTISED_10000baseR_FEC
)) {
1941 netdev
->advertised
|= NETDEV_F_10GB_FD
;
1943 if ((ecmd
.advertising
& ADVERTISED_40000baseKR4_Full
) ||
1944 (ecmd
.advertising
& ADVERTISED_40000baseCR4_Full
) ||
1945 (ecmd
.advertising
& ADVERTISED_40000baseSR4_Full
) ||
1946 (ecmd
.advertising
& ADVERTISED_40000baseLR4_Full
)) {
1947 netdev
->advertised
|= NETDEV_F_40GB_FD
;
1949 if (ecmd
.advertising
& ADVERTISED_TP
) {
1950 netdev
->advertised
|= NETDEV_F_COPPER
;
1952 if (ecmd
.advertising
& ADVERTISED_FIBRE
) {
1953 netdev
->advertised
|= NETDEV_F_FIBER
;
1955 if (ecmd
.advertising
& ADVERTISED_Autoneg
) {
1956 netdev
->advertised
|= NETDEV_F_AUTONEG
;
1958 if (ecmd
.advertising
& ADVERTISED_Pause
) {
1959 netdev
->advertised
|= NETDEV_F_PAUSE
;
1961 if (ecmd
.advertising
& ADVERTISED_Asym_Pause
) {
1962 netdev
->advertised
|= NETDEV_F_PAUSE_ASYM
;
1965 /* Current settings. */
1966 speed
= ethtool_cmd_speed(&ecmd
);
1967 if (speed
== SPEED_10
) {
1968 netdev
->current
= ecmd
.duplex
? NETDEV_F_10MB_FD
: NETDEV_F_10MB_HD
;
1969 } else if (speed
== SPEED_100
) {
1970 netdev
->current
= ecmd
.duplex
? NETDEV_F_100MB_FD
: NETDEV_F_100MB_HD
;
1971 } else if (speed
== SPEED_1000
) {
1972 netdev
->current
= ecmd
.duplex
? NETDEV_F_1GB_FD
: NETDEV_F_1GB_HD
;
1973 } else if (speed
== SPEED_10000
) {
1974 netdev
->current
= NETDEV_F_10GB_FD
;
1975 } else if (speed
== 40000) {
1976 netdev
->current
= NETDEV_F_40GB_FD
;
1977 } else if (speed
== 100000) {
1978 netdev
->current
= NETDEV_F_100GB_FD
;
1979 } else if (speed
== 1000000) {
1980 netdev
->current
= NETDEV_F_1TB_FD
;
1982 netdev
->current
= 0;
1985 if (ecmd
.port
== PORT_TP
) {
1986 netdev
->current
|= NETDEV_F_COPPER
;
1987 } else if (ecmd
.port
== PORT_FIBRE
) {
1988 netdev
->current
|= NETDEV_F_FIBER
;
1992 netdev
->current
|= NETDEV_F_AUTONEG
;
1996 netdev
->cache_valid
|= VALID_FEATURES
;
1997 netdev
->get_features_error
= error
;
2000 /* Stores the features supported by 'netdev' into of '*current', '*advertised',
2001 * '*supported', and '*peer'. Each value is a bitmap of NETDEV_* bits.
2002 * Returns 0 if successful, otherwise a positive errno value. */
2004 netdev_linux_get_features(const struct netdev
*netdev_
,
2005 enum netdev_features
*current
,
2006 enum netdev_features
*advertised
,
2007 enum netdev_features
*supported
,
2008 enum netdev_features
*peer
)
2010 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
2013 ovs_mutex_lock(&netdev
->mutex
);
2014 netdev_linux_read_features(netdev
);
2015 if (!netdev
->get_features_error
) {
2016 *current
= netdev
->current
;
2017 *advertised
= netdev
->advertised
;
2018 *supported
= netdev
->supported
;
2019 *peer
= 0; /* XXX */
2021 error
= netdev
->get_features_error
;
2022 ovs_mutex_unlock(&netdev
->mutex
);
2027 /* Set the features advertised by 'netdev' to 'advertise'. */
2029 netdev_linux_set_advertisements(struct netdev
*netdev_
,
2030 enum netdev_features advertise
)
2032 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
2033 struct ethtool_cmd ecmd
;
2036 ovs_mutex_lock(&netdev
->mutex
);
2038 COVERAGE_INC(netdev_get_ethtool
);
2039 memset(&ecmd
, 0, sizeof ecmd
);
2040 error
= netdev_linux_do_ethtool(netdev_get_name(netdev_
), &ecmd
,
2041 ETHTOOL_GSET
, "ETHTOOL_GSET");
2046 ecmd
.advertising
= 0;
2047 if (advertise
& NETDEV_F_10MB_HD
) {
2048 ecmd
.advertising
|= ADVERTISED_10baseT_Half
;
2050 if (advertise
& NETDEV_F_10MB_FD
) {
2051 ecmd
.advertising
|= ADVERTISED_10baseT_Full
;
2053 if (advertise
& NETDEV_F_100MB_HD
) {
2054 ecmd
.advertising
|= ADVERTISED_100baseT_Half
;
2056 if (advertise
& NETDEV_F_100MB_FD
) {
2057 ecmd
.advertising
|= ADVERTISED_100baseT_Full
;
2059 if (advertise
& NETDEV_F_1GB_HD
) {
2060 ecmd
.advertising
|= ADVERTISED_1000baseT_Half
;
2062 if (advertise
& NETDEV_F_1GB_FD
) {
2063 ecmd
.advertising
|= ADVERTISED_1000baseT_Full
;
2065 if (advertise
& NETDEV_F_10GB_FD
) {
2066 ecmd
.advertising
|= ADVERTISED_10000baseT_Full
;
2068 if (advertise
& NETDEV_F_COPPER
) {
2069 ecmd
.advertising
|= ADVERTISED_TP
;
2071 if (advertise
& NETDEV_F_FIBER
) {
2072 ecmd
.advertising
|= ADVERTISED_FIBRE
;
2074 if (advertise
& NETDEV_F_AUTONEG
) {
2075 ecmd
.advertising
|= ADVERTISED_Autoneg
;
2077 if (advertise
& NETDEV_F_PAUSE
) {
2078 ecmd
.advertising
|= ADVERTISED_Pause
;
2080 if (advertise
& NETDEV_F_PAUSE_ASYM
) {
2081 ecmd
.advertising
|= ADVERTISED_Asym_Pause
;
2083 COVERAGE_INC(netdev_set_ethtool
);
2084 error
= netdev_linux_do_ethtool(netdev_get_name(netdev_
), &ecmd
,
2085 ETHTOOL_SSET
, "ETHTOOL_SSET");
2088 ovs_mutex_unlock(&netdev
->mutex
);
2092 /* Attempts to set input rate limiting (policing) policy. Returns 0 if
2093 * successful, otherwise a positive errno value. */
2095 netdev_linux_set_policing(struct netdev
*netdev_
,
2096 uint32_t kbits_rate
, uint32_t kbits_burst
)
2098 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
2099 const char *netdev_name
= netdev_get_name(netdev_
);
2103 if (netdev_is_flow_api_enabled()) {
2105 VLOG_WARN_RL(&rl
, "%s: policing with offload isn't supported",
2111 kbits_burst
= (!kbits_rate
? 0 /* Force to 0 if no rate specified. */
2112 : !kbits_burst
? 8000 /* Default to 8000 kbits if 0. */
2113 : kbits_burst
); /* Stick with user-specified value. */
2115 ovs_mutex_lock(&netdev
->mutex
);
2116 if (netdev
->cache_valid
& VALID_POLICING
) {
2117 error
= netdev
->netdev_policing_error
;
2118 if (error
|| (netdev
->kbits_rate
== kbits_rate
&&
2119 netdev
->kbits_burst
== kbits_burst
)) {
2120 /* Assume that settings haven't changed since we last set them. */
2123 netdev
->cache_valid
&= ~VALID_POLICING
;
2126 error
= get_ifindex(netdev_
, &ifindex
);
2131 COVERAGE_INC(netdev_set_policing
);
2132 /* Remove any existing ingress qdisc. */
2133 error
= tc_add_del_ingress_qdisc(ifindex
, false);
2135 VLOG_WARN_RL(&rl
, "%s: removing policing failed: %s",
2136 netdev_name
, ovs_strerror(error
));
2141 error
= tc_add_del_ingress_qdisc(ifindex
, true);
2143 VLOG_WARN_RL(&rl
, "%s: adding policing qdisc failed: %s",
2144 netdev_name
, ovs_strerror(error
));
2148 error
= tc_add_policer(netdev_
, kbits_rate
, kbits_burst
);
2150 VLOG_WARN_RL(&rl
, "%s: adding policing action failed: %s",
2151 netdev_name
, ovs_strerror(error
));
2156 netdev
->kbits_rate
= kbits_rate
;
2157 netdev
->kbits_burst
= kbits_burst
;
2160 if (!error
|| error
== ENODEV
) {
2161 netdev
->netdev_policing_error
= error
;
2162 netdev
->cache_valid
|= VALID_POLICING
;
2164 ovs_mutex_unlock(&netdev
->mutex
);
2169 netdev_linux_get_qos_types(const struct netdev
*netdev OVS_UNUSED
,
2172 const struct tc_ops
*const *opsp
;
2173 for (opsp
= tcs
; *opsp
!= NULL
; opsp
++) {
2174 const struct tc_ops
*ops
= *opsp
;
2175 if (ops
->tc_install
&& ops
->ovs_name
[0] != '\0') {
2176 sset_add(types
, ops
->ovs_name
);
2182 static const struct tc_ops
*
2183 tc_lookup_ovs_name(const char *name
)
2185 const struct tc_ops
*const *opsp
;
2187 for (opsp
= tcs
; *opsp
!= NULL
; opsp
++) {
2188 const struct tc_ops
*ops
= *opsp
;
2189 if (!strcmp(name
, ops
->ovs_name
)) {
2196 static const struct tc_ops
*
2197 tc_lookup_linux_name(const char *name
)
2199 const struct tc_ops
*const *opsp
;
2201 for (opsp
= tcs
; *opsp
!= NULL
; opsp
++) {
2202 const struct tc_ops
*ops
= *opsp
;
2203 if (ops
->linux_name
&& !strcmp(name
, ops
->linux_name
)) {
2210 static struct tc_queue
*
2211 tc_find_queue__(const struct netdev
*netdev_
, unsigned int queue_id
,
2214 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
2215 struct tc_queue
*queue
;
2217 HMAP_FOR_EACH_IN_BUCKET (queue
, hmap_node
, hash
, &netdev
->tc
->queues
) {
2218 if (queue
->queue_id
== queue_id
) {
2225 static struct tc_queue
*
2226 tc_find_queue(const struct netdev
*netdev
, unsigned int queue_id
)
2228 return tc_find_queue__(netdev
, queue_id
, hash_int(queue_id
, 0));
2232 netdev_linux_get_qos_capabilities(const struct netdev
*netdev OVS_UNUSED
,
2234 struct netdev_qos_capabilities
*caps
)
2236 const struct tc_ops
*ops
= tc_lookup_ovs_name(type
);
2240 caps
->n_queues
= ops
->n_queues
;
2245 netdev_linux_get_qos(const struct netdev
*netdev_
,
2246 const char **typep
, 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 *typep
= netdev
->tc
->ops
->ovs_name
;
2255 error
= (netdev
->tc
->ops
->qdisc_get
2256 ? netdev
->tc
->ops
->qdisc_get(netdev_
, details
)
2259 ovs_mutex_unlock(&netdev
->mutex
);
2265 netdev_linux_set_qos(struct netdev
*netdev_
,
2266 const char *type
, const struct smap
*details
)
2268 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
2269 const struct tc_ops
*new_ops
;
2272 new_ops
= tc_lookup_ovs_name(type
);
2273 if (!new_ops
|| !new_ops
->tc_install
) {
2277 if (new_ops
== &tc_ops_noop
) {
2278 return new_ops
->tc_install(netdev_
, details
);
2281 ovs_mutex_lock(&netdev
->mutex
);
2282 error
= tc_query_qdisc(netdev_
);
2287 if (new_ops
== netdev
->tc
->ops
) {
2288 error
= new_ops
->qdisc_set
? new_ops
->qdisc_set(netdev_
, details
) : 0;
2290 /* Delete existing qdisc. */
2291 error
= tc_del_qdisc(netdev_
);
2295 ovs_assert(netdev
->tc
== NULL
);
2297 /* Install new qdisc. */
2298 error
= new_ops
->tc_install(netdev_
, details
);
2299 ovs_assert((error
== 0) == (netdev
->tc
!= NULL
));
2303 ovs_mutex_unlock(&netdev
->mutex
);
2308 netdev_linux_get_queue(const struct netdev
*netdev_
,
2309 unsigned int queue_id
, struct smap
*details
)
2311 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
2314 ovs_mutex_lock(&netdev
->mutex
);
2315 error
= tc_query_qdisc(netdev_
);
2317 struct tc_queue
*queue
= tc_find_queue(netdev_
, queue_id
);
2319 ? netdev
->tc
->ops
->class_get(netdev_
, queue
, details
)
2322 ovs_mutex_unlock(&netdev
->mutex
);
2328 netdev_linux_set_queue(struct netdev
*netdev_
,
2329 unsigned int queue_id
, const struct smap
*details
)
2331 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
2334 ovs_mutex_lock(&netdev
->mutex
);
2335 error
= tc_query_qdisc(netdev_
);
2337 error
= (queue_id
< netdev
->tc
->ops
->n_queues
2338 && netdev
->tc
->ops
->class_set
2339 ? netdev
->tc
->ops
->class_set(netdev_
, queue_id
, details
)
2342 ovs_mutex_unlock(&netdev
->mutex
);
2348 netdev_linux_delete_queue(struct netdev
*netdev_
, unsigned int queue_id
)
2350 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
2353 ovs_mutex_lock(&netdev
->mutex
);
2354 error
= tc_query_qdisc(netdev_
);
2356 if (netdev
->tc
->ops
->class_delete
) {
2357 struct tc_queue
*queue
= tc_find_queue(netdev_
, queue_id
);
2359 ? netdev
->tc
->ops
->class_delete(netdev_
, queue
)
2365 ovs_mutex_unlock(&netdev
->mutex
);
2371 netdev_linux_get_queue_stats(const struct netdev
*netdev_
,
2372 unsigned int queue_id
,
2373 struct netdev_queue_stats
*stats
)
2375 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_stats
) {
2382 const struct tc_queue
*queue
= tc_find_queue(netdev_
, queue_id
);
2384 stats
->created
= queue
->created
;
2385 error
= netdev
->tc
->ops
->class_get_stats(netdev_
, queue
,
2394 ovs_mutex_unlock(&netdev
->mutex
);
2399 struct queue_dump_state
{
2400 struct nl_dump dump
;
2405 start_queue_dump(const struct netdev
*netdev
, struct queue_dump_state
*state
)
2407 struct ofpbuf request
;
2408 struct tcmsg
*tcmsg
;
2410 tcmsg
= netdev_linux_tc_make_request(netdev
, RTM_GETTCLASS
, 0, &request
);
2414 tcmsg
->tcm_parent
= 0;
2415 nl_dump_start(&state
->dump
, NETLINK_ROUTE
, &request
);
2416 ofpbuf_uninit(&request
);
2418 ofpbuf_init(&state
->buf
, NL_DUMP_BUFSIZE
);
2423 finish_queue_dump(struct queue_dump_state
*state
)
2425 ofpbuf_uninit(&state
->buf
);
2426 return nl_dump_done(&state
->dump
);
2429 struct netdev_linux_queue_state
{
2430 unsigned int *queues
;
2436 netdev_linux_queue_dump_start(const struct netdev
*netdev_
, void **statep
)
2438 const struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
2441 ovs_mutex_lock(&netdev
->mutex
);
2442 error
= tc_query_qdisc(netdev_
);
2444 if (netdev
->tc
->ops
->class_get
) {
2445 struct netdev_linux_queue_state
*state
;
2446 struct tc_queue
*queue
;
2449 *statep
= state
= xmalloc(sizeof *state
);
2450 state
->n_queues
= hmap_count(&netdev
->tc
->queues
);
2451 state
->cur_queue
= 0;
2452 state
->queues
= xmalloc(state
->n_queues
* sizeof *state
->queues
);
2455 HMAP_FOR_EACH (queue
, hmap_node
, &netdev
->tc
->queues
) {
2456 state
->queues
[i
++] = queue
->queue_id
;
2462 ovs_mutex_unlock(&netdev
->mutex
);
2468 netdev_linux_queue_dump_next(const struct netdev
*netdev_
, void *state_
,
2469 unsigned int *queue_idp
, struct smap
*details
)
2471 const struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
2472 struct netdev_linux_queue_state
*state
= state_
;
2475 ovs_mutex_lock(&netdev
->mutex
);
2476 while (state
->cur_queue
< state
->n_queues
) {
2477 unsigned int queue_id
= state
->queues
[state
->cur_queue
++];
2478 struct tc_queue
*queue
= tc_find_queue(netdev_
, queue_id
);
2481 *queue_idp
= queue_id
;
2482 error
= netdev
->tc
->ops
->class_get(netdev_
, queue
, details
);
2486 ovs_mutex_unlock(&netdev
->mutex
);
2492 netdev_linux_queue_dump_done(const struct netdev
*netdev OVS_UNUSED
,
2495 struct netdev_linux_queue_state
*state
= state_
;
2497 free(state
->queues
);
2503 netdev_linux_dump_queue_stats(const struct netdev
*netdev_
,
2504 netdev_dump_queue_stats_cb
*cb
, void *aux
)
2506 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
2509 ovs_mutex_lock(&netdev
->mutex
);
2510 error
= tc_query_qdisc(netdev_
);
2512 struct queue_dump_state state
;
2514 if (!netdev
->tc
->ops
->class_dump_stats
) {
2516 } else if (!start_queue_dump(netdev_
, &state
)) {
2522 while (nl_dump_next(&state
.dump
, &msg
, &state
.buf
)) {
2523 retval
= netdev
->tc
->ops
->class_dump_stats(netdev_
, &msg
,
2530 retval
= finish_queue_dump(&state
);
2536 ovs_mutex_unlock(&netdev
->mutex
);
2542 netdev_linux_set_in4(struct netdev
*netdev_
, struct in_addr address
,
2543 struct in_addr netmask
)
2545 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
2548 ovs_mutex_lock(&netdev
->mutex
);
2549 error
= do_set_addr(netdev_
, SIOCSIFADDR
, "SIOCSIFADDR", address
);
2551 if (address
.s_addr
!= INADDR_ANY
) {
2552 error
= do_set_addr(netdev_
, SIOCSIFNETMASK
,
2553 "SIOCSIFNETMASK", netmask
);
2557 ovs_mutex_unlock(&netdev
->mutex
);
2562 /* If 'netdev' has an assigned IPv6 address, sets '*in6' to that address.
2563 * Otherwise, sets '*in6' to 'in6addr_any' and returns the corresponding
2566 netdev_linux_get_addr_list(const struct netdev
*netdev_
,
2567 struct in6_addr
**addr
, struct in6_addr
**mask
, int *n_cnt
)
2569 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
2572 ovs_mutex_lock(&netdev
->mutex
);
2573 error
= netdev_get_addrs(netdev_get_name(netdev_
), addr
, mask
, n_cnt
);
2574 ovs_mutex_unlock(&netdev
->mutex
);
2580 make_in4_sockaddr(struct sockaddr
*sa
, struct in_addr addr
)
2582 struct sockaddr_in sin
;
2583 memset(&sin
, 0, sizeof sin
);
2584 sin
.sin_family
= AF_INET
;
2585 sin
.sin_addr
= addr
;
2588 memset(sa
, 0, sizeof *sa
);
2589 memcpy(sa
, &sin
, sizeof sin
);
2593 do_set_addr(struct netdev
*netdev
,
2594 int ioctl_nr
, const char *ioctl_name
, struct in_addr addr
)
2598 make_in4_sockaddr(&ifr
.ifr_addr
, addr
);
2599 return af_inet_ifreq_ioctl(netdev_get_name(netdev
), &ifr
, ioctl_nr
,
2603 /* Adds 'router' as a default IP gateway. */
2605 netdev_linux_add_router(struct netdev
*netdev OVS_UNUSED
, struct in_addr router
)
2607 struct in_addr any
= { INADDR_ANY
};
2611 memset(&rt
, 0, sizeof rt
);
2612 make_in4_sockaddr(&rt
.rt_dst
, any
);
2613 make_in4_sockaddr(&rt
.rt_gateway
, router
);
2614 make_in4_sockaddr(&rt
.rt_genmask
, any
);
2615 rt
.rt_flags
= RTF_UP
| RTF_GATEWAY
;
2616 error
= af_inet_ioctl(SIOCADDRT
, &rt
);
2618 VLOG_WARN("ioctl(SIOCADDRT): %s", ovs_strerror(error
));
2624 netdev_linux_get_next_hop(const struct in_addr
*host
, struct in_addr
*next_hop
,
2627 static const char fn
[] = "/proc/net/route";
2632 *netdev_name
= NULL
;
2633 stream
= fopen(fn
, "r");
2634 if (stream
== NULL
) {
2635 VLOG_WARN_RL(&rl
, "%s: open failed: %s", fn
, ovs_strerror(errno
));
2640 while (fgets(line
, sizeof line
, stream
)) {
2643 ovs_be32 dest
, gateway
, mask
;
2644 int refcnt
, metric
, mtu
;
2645 unsigned int flags
, use
, window
, irtt
;
2648 "%16s %"SCNx32
" %"SCNx32
" %04X %d %u %d %"SCNx32
2650 iface
, &dest
, &gateway
, &flags
, &refcnt
,
2651 &use
, &metric
, &mask
, &mtu
, &window
, &irtt
)) {
2652 VLOG_WARN_RL(&rl
, "%s: could not parse line %d: %s",
2656 if (!(flags
& RTF_UP
)) {
2657 /* Skip routes that aren't up. */
2661 /* The output of 'dest', 'mask', and 'gateway' were given in
2662 * network byte order, so we don't need need any endian
2663 * conversions here. */
2664 if ((dest
& mask
) == (host
->s_addr
& mask
)) {
2666 /* The host is directly reachable. */
2667 next_hop
->s_addr
= 0;
2669 /* To reach the host, we must go through a gateway. */
2670 next_hop
->s_addr
= gateway
;
2672 *netdev_name
= xstrdup(iface
);
2684 netdev_linux_get_status(const struct netdev
*netdev_
, struct smap
*smap
)
2686 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
2689 ovs_mutex_lock(&netdev
->mutex
);
2690 if (!(netdev
->cache_valid
& VALID_DRVINFO
)) {
2691 struct ethtool_cmd
*cmd
= (struct ethtool_cmd
*) &netdev
->drvinfo
;
2693 COVERAGE_INC(netdev_get_ethtool
);
2694 memset(&netdev
->drvinfo
, 0, sizeof netdev
->drvinfo
);
2695 error
= netdev_linux_do_ethtool(netdev
->up
.name
,
2698 "ETHTOOL_GDRVINFO");
2700 netdev
->cache_valid
|= VALID_DRVINFO
;
2705 smap_add(smap
, "driver_name", netdev
->drvinfo
.driver
);
2706 smap_add(smap
, "driver_version", netdev
->drvinfo
.version
);
2707 smap_add(smap
, "firmware_version", netdev
->drvinfo
.fw_version
);
2709 ovs_mutex_unlock(&netdev
->mutex
);
2715 netdev_internal_get_status(const struct netdev
*netdev OVS_UNUSED
,
2718 smap_add(smap
, "driver_name", "openvswitch");
2722 /* Looks up the ARP table entry for 'ip' on 'netdev'. If one exists and can be
2723 * successfully retrieved, it stores the corresponding MAC address in 'mac' and
2724 * returns 0. Otherwise, it returns a positive errno value; in particular,
2725 * ENXIO indicates that there is not ARP table entry for 'ip' on 'netdev'. */
2727 netdev_linux_arp_lookup(const struct netdev
*netdev
,
2728 ovs_be32 ip
, struct eth_addr
*mac
)
2731 struct sockaddr_in sin
;
2734 memset(&r
, 0, sizeof r
);
2735 memset(&sin
, 0, sizeof sin
);
2736 sin
.sin_family
= AF_INET
;
2737 sin
.sin_addr
.s_addr
= ip
;
2739 memcpy(&r
.arp_pa
, &sin
, sizeof sin
);
2740 r
.arp_ha
.sa_family
= ARPHRD_ETHER
;
2742 ovs_strzcpy(r
.arp_dev
, netdev_get_name(netdev
), sizeof r
.arp_dev
);
2743 COVERAGE_INC(netdev_arp_lookup
);
2744 retval
= af_inet_ioctl(SIOCGARP
, &r
);
2746 memcpy(mac
, r
.arp_ha
.sa_data
, ETH_ADDR_LEN
);
2747 } else if (retval
!= ENXIO
) {
2748 VLOG_WARN_RL(&rl
, "%s: could not look up ARP entry for "IP_FMT
": %s",
2749 netdev_get_name(netdev
), IP_ARGS(ip
),
2750 ovs_strerror(retval
));
2756 nd_to_iff_flags(enum netdev_flags nd
)
2759 if (nd
& NETDEV_UP
) {
2762 if (nd
& NETDEV_PROMISC
) {
2765 if (nd
& NETDEV_LOOPBACK
) {
2766 iff
|= IFF_LOOPBACK
;
2772 iff_to_nd_flags(int iff
)
2774 enum netdev_flags nd
= 0;
2778 if (iff
& IFF_PROMISC
) {
2779 nd
|= NETDEV_PROMISC
;
2781 if (iff
& IFF_LOOPBACK
) {
2782 nd
|= NETDEV_LOOPBACK
;
2788 update_flags(struct netdev_linux
*netdev
, enum netdev_flags off
,
2789 enum netdev_flags on
, enum netdev_flags
*old_flagsp
)
2790 OVS_REQUIRES(netdev
->mutex
)
2792 int old_flags
, new_flags
;
2795 old_flags
= netdev
->ifi_flags
;
2796 *old_flagsp
= iff_to_nd_flags(old_flags
);
2797 new_flags
= (old_flags
& ~nd_to_iff_flags(off
)) | nd_to_iff_flags(on
);
2798 if (new_flags
!= old_flags
) {
2799 error
= set_flags(netdev_get_name(&netdev
->up
), new_flags
);
2800 get_flags(&netdev
->up
, &netdev
->ifi_flags
);
2807 netdev_linux_update_flags(struct netdev
*netdev_
, enum netdev_flags off
,
2808 enum netdev_flags on
, enum netdev_flags
*old_flagsp
)
2810 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
2813 ovs_mutex_lock(&netdev
->mutex
);
2814 error
= update_flags(netdev
, off
, on
, old_flagsp
);
2815 ovs_mutex_unlock(&netdev
->mutex
);
2820 #define NETDEV_LINUX_CLASS(NAME, CONSTRUCT, GET_STATS, \
2821 GET_FEATURES, GET_STATUS, \
2825 false, /* is_pmd */ \
2829 netdev_linux_wait, \
2831 netdev_linux_alloc, \
2833 netdev_linux_destruct, \
2834 netdev_linux_dealloc, \
2835 NULL, /* get_config */ \
2836 NULL, /* set_config */ \
2837 NULL, /* get_tunnel_config */ \
2838 NULL, /* build header */ \
2839 NULL, /* push header */ \
2840 NULL, /* pop header */ \
2841 NULL, /* get_numa_id */ \
2842 NULL, /* set_tx_multiq */ \
2844 netdev_linux_send, \
2845 netdev_linux_send_wait, \
2847 netdev_linux_set_etheraddr, \
2848 netdev_linux_get_etheraddr, \
2849 netdev_linux_get_mtu, \
2850 netdev_linux_set_mtu, \
2851 netdev_linux_get_ifindex, \
2852 netdev_linux_get_carrier, \
2853 netdev_linux_get_carrier_resets, \
2854 netdev_linux_set_miimon_interval, \
2858 netdev_linux_set_advertisements, \
2859 NULL, /* get_pt_mode */ \
2861 netdev_linux_set_policing, \
2862 netdev_linux_get_qos_types, \
2863 netdev_linux_get_qos_capabilities, \
2864 netdev_linux_get_qos, \
2865 netdev_linux_set_qos, \
2866 netdev_linux_get_queue, \
2867 netdev_linux_set_queue, \
2868 netdev_linux_delete_queue, \
2869 netdev_linux_get_queue_stats, \
2870 netdev_linux_queue_dump_start, \
2871 netdev_linux_queue_dump_next, \
2872 netdev_linux_queue_dump_done, \
2873 netdev_linux_dump_queue_stats, \
2875 netdev_linux_set_in4, \
2876 netdev_linux_get_addr_list, \
2877 netdev_linux_add_router, \
2878 netdev_linux_get_next_hop, \
2880 netdev_linux_arp_lookup, \
2882 netdev_linux_update_flags, \
2883 NULL, /* reconfigure */ \
2885 netdev_linux_rxq_alloc, \
2886 netdev_linux_rxq_construct, \
2887 netdev_linux_rxq_destruct, \
2888 netdev_linux_rxq_dealloc, \
2889 netdev_linux_rxq_recv, \
2890 netdev_linux_rxq_wait, \
2891 netdev_linux_rxq_drain, \
2896 const struct netdev_class netdev_linux_class
=
2899 netdev_linux_construct
,
2900 netdev_linux_get_stats
,
2901 netdev_linux_get_features
,
2902 netdev_linux_get_status
,
2903 LINUX_FLOW_OFFLOAD_API
);
2905 const struct netdev_class netdev_tap_class
=
2908 netdev_linux_construct_tap
,
2909 netdev_tap_get_stats
,
2910 netdev_linux_get_features
,
2911 netdev_linux_get_status
,
2914 const struct netdev_class netdev_internal_class
=
2917 netdev_linux_construct
,
2918 netdev_internal_get_stats
,
2919 NULL
, /* get_features */
2920 netdev_internal_get_status
,
2924 #define CODEL_N_QUEUES 0x0000
2926 /* In sufficiently new kernel headers these are defined as enums in
2927 * <linux/pkt_sched.h>. Define them here as macros to help out with older
2928 * kernels. (This overrides any enum definition in the header file but that's
2930 #define TCA_CODEL_TARGET 1
2931 #define TCA_CODEL_LIMIT 2
2932 #define TCA_CODEL_INTERVAL 3
2941 static struct codel
*
2942 codel_get__(const struct netdev
*netdev_
)
2944 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
2945 return CONTAINER_OF(netdev
->tc
, struct codel
, tc
);
2949 codel_install__(struct netdev
*netdev_
, uint32_t target
, uint32_t limit
,
2952 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
2953 struct codel
*codel
;
2955 codel
= xmalloc(sizeof *codel
);
2956 tc_init(&codel
->tc
, &tc_ops_codel
);
2957 codel
->target
= target
;
2958 codel
->limit
= limit
;
2959 codel
->interval
= interval
;
2961 netdev
->tc
= &codel
->tc
;
2965 codel_setup_qdisc__(struct netdev
*netdev
, uint32_t target
, uint32_t limit
,
2969 struct ofpbuf request
;
2970 struct tcmsg
*tcmsg
;
2971 uint32_t otarget
, olimit
, ointerval
;
2974 tc_del_qdisc(netdev
);
2976 tcmsg
= netdev_linux_tc_make_request(netdev
, RTM_NEWQDISC
,
2977 NLM_F_EXCL
| NLM_F_CREATE
, &request
);
2981 tcmsg
->tcm_handle
= tc_make_handle(1, 0);
2982 tcmsg
->tcm_parent
= TC_H_ROOT
;
2984 otarget
= target
? target
: 5000;
2985 olimit
= limit
? limit
: 10240;
2986 ointerval
= interval
? interval
: 100000;
2988 nl_msg_put_string(&request
, TCA_KIND
, "codel");
2989 opt_offset
= nl_msg_start_nested(&request
, TCA_OPTIONS
);
2990 nl_msg_put_u32(&request
, TCA_CODEL_TARGET
, otarget
);
2991 nl_msg_put_u32(&request
, TCA_CODEL_LIMIT
, olimit
);
2992 nl_msg_put_u32(&request
, TCA_CODEL_INTERVAL
, ointerval
);
2993 nl_msg_end_nested(&request
, opt_offset
);
2995 error
= tc_transact(&request
, NULL
);
2997 VLOG_WARN_RL(&rl
, "failed to replace %s qdisc, "
2998 "target %u, limit %u, interval %u error %d(%s)",
2999 netdev_get_name(netdev
),
3000 otarget
, olimit
, ointerval
,
3001 error
, ovs_strerror(error
));
3007 codel_parse_qdisc_details__(struct netdev
*netdev OVS_UNUSED
,
3008 const struct smap
*details
, struct codel
*codel
)
3010 codel
->target
= smap_get_ullong(details
, "target", 0);
3011 codel
->limit
= smap_get_ullong(details
, "limit", 0);
3012 codel
->interval
= smap_get_ullong(details
, "interval", 0);
3014 if (!codel
->target
) {
3015 codel
->target
= 5000;
3017 if (!codel
->limit
) {
3018 codel
->limit
= 10240;
3020 if (!codel
->interval
) {
3021 codel
->interval
= 100000;
3026 codel_tc_install(struct netdev
*netdev
, const struct smap
*details
)
3031 codel_parse_qdisc_details__(netdev
, details
, &codel
);
3032 error
= codel_setup_qdisc__(netdev
, codel
.target
, codel
.limit
,
3035 codel_install__(netdev
, codel
.target
, codel
.limit
, codel
.interval
);
3041 codel_parse_tca_options__(struct nlattr
*nl_options
, struct codel
*codel
)
3043 static const struct nl_policy tca_codel_policy
[] = {
3044 [TCA_CODEL_TARGET
] = { .type
= NL_A_U32
},
3045 [TCA_CODEL_LIMIT
] = { .type
= NL_A_U32
},
3046 [TCA_CODEL_INTERVAL
] = { .type
= NL_A_U32
}
3049 struct nlattr
*attrs
[ARRAY_SIZE(tca_codel_policy
)];
3051 if (!nl_parse_nested(nl_options
, tca_codel_policy
,
3052 attrs
, ARRAY_SIZE(tca_codel_policy
))) {
3053 VLOG_WARN_RL(&rl
, "failed to parse CoDel class options");
3057 codel
->target
= nl_attr_get_u32(attrs
[TCA_CODEL_TARGET
]);
3058 codel
->limit
= nl_attr_get_u32(attrs
[TCA_CODEL_LIMIT
]);
3059 codel
->interval
= nl_attr_get_u32(attrs
[TCA_CODEL_INTERVAL
]);
3064 codel_tc_load(struct netdev
*netdev
, struct ofpbuf
*nlmsg
)
3066 struct nlattr
*nlattr
;
3071 error
= tc_parse_qdisc(nlmsg
, &kind
, &nlattr
);
3076 error
= codel_parse_tca_options__(nlattr
, &codel
);
3081 codel_install__(netdev
, codel
.target
, codel
.limit
, codel
.interval
);
3087 codel_tc_destroy(struct tc
*tc
)
3089 struct codel
*codel
= CONTAINER_OF(tc
, struct codel
, tc
);
3095 codel_qdisc_get(const struct netdev
*netdev
, struct smap
*details
)
3097 const struct codel
*codel
= codel_get__(netdev
);
3098 smap_add_format(details
, "target", "%u", codel
->target
);
3099 smap_add_format(details
, "limit", "%u", codel
->limit
);
3100 smap_add_format(details
, "interval", "%u", codel
->interval
);
3105 codel_qdisc_set(struct netdev
*netdev
, const struct smap
*details
)
3109 codel_parse_qdisc_details__(netdev
, details
, &codel
);
3110 codel_install__(netdev
, codel
.target
, codel
.limit
, codel
.interval
);
3111 codel_get__(netdev
)->target
= codel
.target
;
3112 codel_get__(netdev
)->limit
= codel
.limit
;
3113 codel_get__(netdev
)->interval
= codel
.interval
;
3117 static const struct tc_ops tc_ops_codel
= {
3118 "codel", /* linux_name */
3119 "linux-codel", /* ovs_name */
3120 CODEL_N_QUEUES
, /* n_queues */
3133 /* FQ-CoDel traffic control class. */
3135 #define FQCODEL_N_QUEUES 0x0000
3137 /* In sufficiently new kernel headers these are defined as enums in
3138 * <linux/pkt_sched.h>. Define them here as macros to help out with older
3139 * kernels. (This overrides any enum definition in the header file but that's
3141 #define TCA_FQ_CODEL_TARGET 1
3142 #define TCA_FQ_CODEL_LIMIT 2
3143 #define TCA_FQ_CODEL_INTERVAL 3
3144 #define TCA_FQ_CODEL_ECN 4
3145 #define TCA_FQ_CODEL_FLOWS 5
3146 #define TCA_FQ_CODEL_QUANTUM 6
3157 static struct fqcodel
*
3158 fqcodel_get__(const struct netdev
*netdev_
)
3160 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
3161 return CONTAINER_OF(netdev
->tc
, struct fqcodel
, tc
);
3165 fqcodel_install__(struct netdev
*netdev_
, uint32_t target
, uint32_t limit
,
3166 uint32_t interval
, uint32_t flows
, uint32_t quantum
)
3168 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
3169 struct fqcodel
*fqcodel
;
3171 fqcodel
= xmalloc(sizeof *fqcodel
);
3172 tc_init(&fqcodel
->tc
, &tc_ops_fqcodel
);
3173 fqcodel
->target
= target
;
3174 fqcodel
->limit
= limit
;
3175 fqcodel
->interval
= interval
;
3176 fqcodel
->flows
= flows
;
3177 fqcodel
->quantum
= quantum
;
3179 netdev
->tc
= &fqcodel
->tc
;
3183 fqcodel_setup_qdisc__(struct netdev
*netdev
, uint32_t target
, uint32_t limit
,
3184 uint32_t interval
, uint32_t flows
, uint32_t quantum
)
3187 struct ofpbuf request
;
3188 struct tcmsg
*tcmsg
;
3189 uint32_t otarget
, olimit
, ointerval
, oflows
, oquantum
;
3192 tc_del_qdisc(netdev
);
3194 tcmsg
= netdev_linux_tc_make_request(netdev
, RTM_NEWQDISC
,
3195 NLM_F_EXCL
| NLM_F_CREATE
, &request
);
3199 tcmsg
->tcm_handle
= tc_make_handle(1, 0);
3200 tcmsg
->tcm_parent
= TC_H_ROOT
;
3202 otarget
= target
? target
: 5000;
3203 olimit
= limit
? limit
: 10240;
3204 ointerval
= interval
? interval
: 100000;
3205 oflows
= flows
? flows
: 1024;
3206 oquantum
= quantum
? quantum
: 1514; /* fq_codel default quantum is 1514
3209 nl_msg_put_string(&request
, TCA_KIND
, "fq_codel");
3210 opt_offset
= nl_msg_start_nested(&request
, TCA_OPTIONS
);
3211 nl_msg_put_u32(&request
, TCA_FQ_CODEL_TARGET
, otarget
);
3212 nl_msg_put_u32(&request
, TCA_FQ_CODEL_LIMIT
, olimit
);
3213 nl_msg_put_u32(&request
, TCA_FQ_CODEL_INTERVAL
, ointerval
);
3214 nl_msg_put_u32(&request
, TCA_FQ_CODEL_FLOWS
, oflows
);
3215 nl_msg_put_u32(&request
, TCA_FQ_CODEL_QUANTUM
, oquantum
);
3216 nl_msg_end_nested(&request
, opt_offset
);
3218 error
= tc_transact(&request
, NULL
);
3220 VLOG_WARN_RL(&rl
, "failed to replace %s qdisc, "
3221 "target %u, limit %u, interval %u, flows %u, quantum %u error %d(%s)",
3222 netdev_get_name(netdev
),
3223 otarget
, olimit
, ointerval
, oflows
, oquantum
,
3224 error
, ovs_strerror(error
));
3230 fqcodel_parse_qdisc_details__(struct netdev
*netdev OVS_UNUSED
,
3231 const struct smap
*details
, struct fqcodel
*fqcodel
)
3233 fqcodel
->target
= smap_get_ullong(details
, "target", 0);
3234 fqcodel
->limit
= smap_get_ullong(details
, "limit", 0);
3235 fqcodel
->interval
= smap_get_ullong(details
, "interval", 0);
3236 fqcodel
->flows
= smap_get_ullong(details
, "flows", 0);
3237 fqcodel
->quantum
= smap_get_ullong(details
, "quantum", 0);
3239 if (!fqcodel
->target
) {
3240 fqcodel
->target
= 5000;
3242 if (!fqcodel
->limit
) {
3243 fqcodel
->limit
= 10240;
3245 if (!fqcodel
->interval
) {
3246 fqcodel
->interval
= 1000000;
3248 if (!fqcodel
->flows
) {
3249 fqcodel
->flows
= 1024;
3251 if (!fqcodel
->quantum
) {
3252 fqcodel
->quantum
= 1514;
3257 fqcodel_tc_install(struct netdev
*netdev
, const struct smap
*details
)
3260 struct fqcodel fqcodel
;
3262 fqcodel_parse_qdisc_details__(netdev
, details
, &fqcodel
);
3263 error
= fqcodel_setup_qdisc__(netdev
, fqcodel
.target
, fqcodel
.limit
,
3264 fqcodel
.interval
, fqcodel
.flows
,
3267 fqcodel_install__(netdev
, fqcodel
.target
, fqcodel
.limit
,
3268 fqcodel
.interval
, fqcodel
.flows
, fqcodel
.quantum
);
3274 fqcodel_parse_tca_options__(struct nlattr
*nl_options
, struct fqcodel
*fqcodel
)
3276 static const struct nl_policy tca_fqcodel_policy
[] = {
3277 [TCA_FQ_CODEL_TARGET
] = { .type
= NL_A_U32
},
3278 [TCA_FQ_CODEL_LIMIT
] = { .type
= NL_A_U32
},
3279 [TCA_FQ_CODEL_INTERVAL
] = { .type
= NL_A_U32
},
3280 [TCA_FQ_CODEL_FLOWS
] = { .type
= NL_A_U32
},
3281 [TCA_FQ_CODEL_QUANTUM
] = { .type
= NL_A_U32
}
3284 struct nlattr
*attrs
[ARRAY_SIZE(tca_fqcodel_policy
)];
3286 if (!nl_parse_nested(nl_options
, tca_fqcodel_policy
,
3287 attrs
, ARRAY_SIZE(tca_fqcodel_policy
))) {
3288 VLOG_WARN_RL(&rl
, "failed to parse FQ_CoDel class options");
3292 fqcodel
->target
= nl_attr_get_u32(attrs
[TCA_FQ_CODEL_TARGET
]);
3293 fqcodel
->limit
= nl_attr_get_u32(attrs
[TCA_FQ_CODEL_LIMIT
]);
3294 fqcodel
->interval
=nl_attr_get_u32(attrs
[TCA_FQ_CODEL_INTERVAL
]);
3295 fqcodel
->flows
= nl_attr_get_u32(attrs
[TCA_FQ_CODEL_FLOWS
]);
3296 fqcodel
->quantum
= nl_attr_get_u32(attrs
[TCA_FQ_CODEL_QUANTUM
]);
3301 fqcodel_tc_load(struct netdev
*netdev
, struct ofpbuf
*nlmsg
)
3303 struct nlattr
*nlattr
;
3306 struct fqcodel fqcodel
;
3308 error
= tc_parse_qdisc(nlmsg
, &kind
, &nlattr
);
3313 error
= fqcodel_parse_tca_options__(nlattr
, &fqcodel
);
3318 fqcodel_install__(netdev
, fqcodel
.target
, fqcodel
.limit
, fqcodel
.interval
,
3319 fqcodel
.flows
, fqcodel
.quantum
);
3324 fqcodel_tc_destroy(struct tc
*tc
)
3326 struct fqcodel
*fqcodel
= CONTAINER_OF(tc
, struct fqcodel
, tc
);
3332 fqcodel_qdisc_get(const struct netdev
*netdev
, struct smap
*details
)
3334 const struct fqcodel
*fqcodel
= fqcodel_get__(netdev
);
3335 smap_add_format(details
, "target", "%u", fqcodel
->target
);
3336 smap_add_format(details
, "limit", "%u", fqcodel
->limit
);
3337 smap_add_format(details
, "interval", "%u", fqcodel
->interval
);
3338 smap_add_format(details
, "flows", "%u", fqcodel
->flows
);
3339 smap_add_format(details
, "quantum", "%u", fqcodel
->quantum
);
3344 fqcodel_qdisc_set(struct netdev
*netdev
, const struct smap
*details
)
3346 struct fqcodel fqcodel
;
3348 fqcodel_parse_qdisc_details__(netdev
, details
, &fqcodel
);
3349 fqcodel_install__(netdev
, fqcodel
.target
, fqcodel
.limit
, fqcodel
.interval
,
3350 fqcodel
.flows
, fqcodel
.quantum
);
3351 fqcodel_get__(netdev
)->target
= fqcodel
.target
;
3352 fqcodel_get__(netdev
)->limit
= fqcodel
.limit
;
3353 fqcodel_get__(netdev
)->interval
= fqcodel
.interval
;
3354 fqcodel_get__(netdev
)->flows
= fqcodel
.flows
;
3355 fqcodel_get__(netdev
)->quantum
= fqcodel
.quantum
;
3359 static const struct tc_ops tc_ops_fqcodel
= {
3360 "fq_codel", /* linux_name */
3361 "linux-fq_codel", /* ovs_name */
3362 FQCODEL_N_QUEUES
, /* n_queues */
3375 /* SFQ traffic control class. */
3377 #define SFQ_N_QUEUES 0x0000
3386 sfq_get__(const struct netdev
*netdev_
)
3388 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
3389 return CONTAINER_OF(netdev
->tc
, struct sfq
, tc
);
3393 sfq_install__(struct netdev
*netdev_
, uint32_t quantum
, uint32_t perturb
)
3395 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
3398 sfq
= xmalloc(sizeof *sfq
);
3399 tc_init(&sfq
->tc
, &tc_ops_sfq
);
3400 sfq
->perturb
= perturb
;
3401 sfq
->quantum
= quantum
;
3403 netdev
->tc
= &sfq
->tc
;
3407 sfq_setup_qdisc__(struct netdev
*netdev
, uint32_t quantum
, uint32_t perturb
)
3409 struct tc_sfq_qopt opt
;
3410 struct ofpbuf request
;
3411 struct tcmsg
*tcmsg
;
3413 int mtu_error
, error
;
3414 mtu_error
= netdev_linux_get_mtu__(netdev_linux_cast(netdev
), &mtu
);
3416 tc_del_qdisc(netdev
);
3418 tcmsg
= netdev_linux_tc_make_request(netdev
, RTM_NEWQDISC
,
3419 NLM_F_EXCL
| NLM_F_CREATE
, &request
);
3423 tcmsg
->tcm_handle
= tc_make_handle(1, 0);
3424 tcmsg
->tcm_parent
= TC_H_ROOT
;
3426 memset(&opt
, 0, sizeof opt
);
3429 opt
.quantum
= mtu
; /* if we cannot find mtu, use default */
3432 opt
.quantum
= quantum
;
3436 opt
.perturb_period
= 10;
3438 opt
.perturb_period
= perturb
;
3441 nl_msg_put_string(&request
, TCA_KIND
, "sfq");
3442 nl_msg_put_unspec(&request
, TCA_OPTIONS
, &opt
, sizeof opt
);
3444 error
= tc_transact(&request
, NULL
);
3446 VLOG_WARN_RL(&rl
, "failed to replace %s qdisc, "
3447 "quantum %u, perturb %u error %d(%s)",
3448 netdev_get_name(netdev
),
3449 opt
.quantum
, opt
.perturb_period
,
3450 error
, ovs_strerror(error
));
3456 sfq_parse_qdisc_details__(struct netdev
*netdev
,
3457 const struct smap
*details
, struct sfq
*sfq
)
3459 sfq
->perturb
= smap_get_ullong(details
, "perturb", 0);
3460 sfq
->quantum
= smap_get_ullong(details
, "quantum", 0);
3462 if (!sfq
->perturb
) {
3466 if (!sfq
->quantum
) {
3468 if (!netdev_linux_get_mtu__(netdev_linux_cast(netdev
), &mtu
)) {
3471 VLOG_WARN_RL(&rl
, "when using SFQ, you must specify quantum on a "
3472 "device without mtu");
3478 sfq_tc_install(struct netdev
*netdev
, const struct smap
*details
)
3483 sfq_parse_qdisc_details__(netdev
, details
, &sfq
);
3484 error
= sfq_setup_qdisc__(netdev
, sfq
.quantum
, sfq
.perturb
);
3486 sfq_install__(netdev
, sfq
.quantum
, sfq
.perturb
);
3492 sfq_tc_load(struct netdev
*netdev
, struct ofpbuf
*nlmsg
)
3494 const struct tc_sfq_qopt
*sfq
;
3495 struct nlattr
*nlattr
;
3499 error
= tc_parse_qdisc(nlmsg
, &kind
, &nlattr
);
3501 sfq
= nl_attr_get(nlattr
);
3502 sfq_install__(netdev
, sfq
->perturb_period
, sfq
->quantum
);
3510 sfq_tc_destroy(struct tc
*tc
)
3512 struct sfq
*sfq
= CONTAINER_OF(tc
, struct sfq
, tc
);
3518 sfq_qdisc_get(const struct netdev
*netdev
, struct smap
*details
)
3520 const struct sfq
*sfq
= sfq_get__(netdev
);
3521 smap_add_format(details
, "quantum", "%u", sfq
->quantum
);
3522 smap_add_format(details
, "perturb", "%u", sfq
->perturb
);
3527 sfq_qdisc_set(struct netdev
*netdev
, const struct smap
*details
)
3531 sfq_parse_qdisc_details__(netdev
, details
, &sfq
);
3532 sfq_install__(netdev
, sfq
.quantum
, sfq
.perturb
);
3533 sfq_get__(netdev
)->quantum
= sfq
.quantum
;
3534 sfq_get__(netdev
)->perturb
= sfq
.perturb
;
3538 static const struct tc_ops tc_ops_sfq
= {
3539 "sfq", /* linux_name */
3540 "linux-sfq", /* ovs_name */
3541 SFQ_N_QUEUES
, /* n_queues */
3554 /* HTB traffic control class. */
3556 #define HTB_N_QUEUES 0xf000
3557 #define HTB_RATE2QUANTUM 10
3561 unsigned int max_rate
; /* In bytes/s. */
3565 struct tc_queue tc_queue
;
3566 unsigned int min_rate
; /* In bytes/s. */
3567 unsigned int max_rate
; /* In bytes/s. */
3568 unsigned int burst
; /* In bytes. */
3569 unsigned int priority
; /* Lower values are higher priorities. */
3573 htb_get__(const struct netdev
*netdev_
)
3575 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
3576 return CONTAINER_OF(netdev
->tc
, struct htb
, tc
);
3580 htb_install__(struct netdev
*netdev_
, uint64_t max_rate
)
3582 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
3585 htb
= xmalloc(sizeof *htb
);
3586 tc_init(&htb
->tc
, &tc_ops_htb
);
3587 htb
->max_rate
= max_rate
;
3589 netdev
->tc
= &htb
->tc
;
3592 /* Create an HTB qdisc.
3594 * Equivalent to "tc qdisc add dev <dev> root handle 1: htb default 1". */
3596 htb_setup_qdisc__(struct netdev
*netdev
)
3599 struct tc_htb_glob opt
;
3600 struct ofpbuf request
;
3601 struct tcmsg
*tcmsg
;
3603 tc_del_qdisc(netdev
);
3605 tcmsg
= netdev_linux_tc_make_request(netdev
, RTM_NEWQDISC
,
3606 NLM_F_EXCL
| NLM_F_CREATE
, &request
);
3610 tcmsg
->tcm_handle
= tc_make_handle(1, 0);
3611 tcmsg
->tcm_parent
= TC_H_ROOT
;
3613 nl_msg_put_string(&request
, TCA_KIND
, "htb");
3615 memset(&opt
, 0, sizeof opt
);
3616 opt
.rate2quantum
= HTB_RATE2QUANTUM
;
3620 opt_offset
= nl_msg_start_nested(&request
, TCA_OPTIONS
);
3621 nl_msg_put_unspec(&request
, TCA_HTB_INIT
, &opt
, sizeof opt
);
3622 nl_msg_end_nested(&request
, opt_offset
);
3624 return tc_transact(&request
, NULL
);
3627 /* Equivalent to "tc class replace <dev> classid <handle> parent <parent> htb
3628 * rate <min_rate>bps ceil <max_rate>bps burst <burst>b prio <priority>". */
3630 htb_setup_class__(struct netdev
*netdev
, unsigned int handle
,
3631 unsigned int parent
, struct htb_class
*class)
3634 struct tc_htb_opt opt
;
3635 struct ofpbuf request
;
3636 struct tcmsg
*tcmsg
;
3640 error
= netdev_linux_get_mtu__(netdev_linux_cast(netdev
), &mtu
);
3642 VLOG_WARN_RL(&rl
, "cannot set up HTB on device %s that lacks MTU",
3643 netdev_get_name(netdev
));
3647 memset(&opt
, 0, sizeof opt
);
3648 tc_fill_rate(&opt
.rate
, class->min_rate
, mtu
);
3649 tc_fill_rate(&opt
.ceil
, class->max_rate
, mtu
);
3650 /* Makes sure the quantum is at least MTU. Setting quantum will
3651 * make htb ignore the r2q for this class. */
3652 if ((class->min_rate
/ HTB_RATE2QUANTUM
) < mtu
) {
3655 opt
.buffer
= tc_calc_buffer(opt
.rate
.rate
, mtu
, class->burst
);
3656 opt
.cbuffer
= tc_calc_buffer(opt
.ceil
.rate
, mtu
, class->burst
);
3657 opt
.prio
= class->priority
;
3659 tcmsg
= netdev_linux_tc_make_request(netdev
, RTM_NEWTCLASS
, NLM_F_CREATE
,
3664 tcmsg
->tcm_handle
= handle
;
3665 tcmsg
->tcm_parent
= parent
;
3667 nl_msg_put_string(&request
, TCA_KIND
, "htb");
3668 opt_offset
= nl_msg_start_nested(&request
, TCA_OPTIONS
);
3669 nl_msg_put_unspec(&request
, TCA_HTB_PARMS
, &opt
, sizeof opt
);
3670 tc_put_rtab(&request
, TCA_HTB_RTAB
, &opt
.rate
);
3671 tc_put_rtab(&request
, TCA_HTB_CTAB
, &opt
.ceil
);
3672 nl_msg_end_nested(&request
, opt_offset
);
3674 error
= tc_transact(&request
, NULL
);
3676 VLOG_WARN_RL(&rl
, "failed to replace %s class %u:%u, parent %u:%u, "
3677 "min_rate=%u max_rate=%u burst=%u prio=%u (%s)",
3678 netdev_get_name(netdev
),
3679 tc_get_major(handle
), tc_get_minor(handle
),
3680 tc_get_major(parent
), tc_get_minor(parent
),
3681 class->min_rate
, class->max_rate
,
3682 class->burst
, class->priority
, ovs_strerror(error
));
3687 /* Parses Netlink attributes in 'options' for HTB parameters and stores a
3688 * description of them into 'details'. The description complies with the
3689 * specification given in the vswitch database documentation for linux-htb
3692 htb_parse_tca_options__(struct nlattr
*nl_options
, struct htb_class
*class)
3694 static const struct nl_policy tca_htb_policy
[] = {
3695 [TCA_HTB_PARMS
] = { .type
= NL_A_UNSPEC
, .optional
= false,
3696 .min_len
= sizeof(struct tc_htb_opt
) },
3699 struct nlattr
*attrs
[ARRAY_SIZE(tca_htb_policy
)];
3700 const struct tc_htb_opt
*htb
;
3702 if (!nl_parse_nested(nl_options
, tca_htb_policy
,
3703 attrs
, ARRAY_SIZE(tca_htb_policy
))) {
3704 VLOG_WARN_RL(&rl
, "failed to parse HTB class options");
3708 htb
= nl_attr_get(attrs
[TCA_HTB_PARMS
]);
3709 class->min_rate
= htb
->rate
.rate
;
3710 class->max_rate
= htb
->ceil
.rate
;
3711 class->burst
= tc_ticks_to_bytes(htb
->rate
.rate
, htb
->buffer
);
3712 class->priority
= htb
->prio
;
3717 htb_parse_tcmsg__(struct ofpbuf
*tcmsg
, unsigned int *queue_id
,
3718 struct htb_class
*options
,
3719 struct netdev_queue_stats
*stats
)
3721 struct nlattr
*nl_options
;
3722 unsigned int handle
;
3725 error
= tc_parse_class(tcmsg
, &handle
, &nl_options
, stats
);
3726 if (!error
&& queue_id
) {
3727 unsigned int major
= tc_get_major(handle
);
3728 unsigned int minor
= tc_get_minor(handle
);
3729 if (major
== 1 && minor
> 0 && minor
<= HTB_N_QUEUES
) {
3730 *queue_id
= minor
- 1;
3735 if (!error
&& options
) {
3736 error
= htb_parse_tca_options__(nl_options
, options
);
3742 htb_parse_qdisc_details__(struct netdev
*netdev_
,
3743 const struct smap
*details
, struct htb_class
*hc
)
3745 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
3747 hc
->max_rate
= smap_get_ullong(details
, "max-rate", 0) / 8;
3748 if (!hc
->max_rate
) {
3749 enum netdev_features current
;
3751 netdev_linux_read_features(netdev
);
3752 current
= !netdev
->get_features_error
? netdev
->current
: 0;
3753 hc
->max_rate
= netdev_features_to_bps(current
, 100 * 1000 * 1000) / 8;
3755 hc
->min_rate
= hc
->max_rate
;
3761 htb_parse_class_details__(struct netdev
*netdev
,
3762 const struct smap
*details
, struct htb_class
*hc
)
3764 const struct htb
*htb
= htb_get__(netdev
);
3766 unsigned long long int max_rate_bit
;
3768 error
= netdev_linux_get_mtu__(netdev_linux_cast(netdev
), &mtu
);
3770 VLOG_WARN_RL(&rl
, "cannot parse HTB class on device %s that lacks MTU",
3771 netdev_get_name(netdev
));
3775 /* HTB requires at least an mtu sized min-rate to send any traffic even
3776 * on uncongested links. */
3777 hc
->min_rate
= smap_get_ullong(details
, "min-rate", 0) / 8;
3778 hc
->min_rate
= MAX(hc
->min_rate
, mtu
);
3779 hc
->min_rate
= MIN(hc
->min_rate
, htb
->max_rate
);
3782 max_rate_bit
= smap_get_ullong(details
, "max-rate", 0);
3783 hc
->max_rate
= max_rate_bit
? max_rate_bit
/ 8 : htb
->max_rate
;
3784 hc
->max_rate
= MAX(hc
->max_rate
, hc
->min_rate
);
3785 hc
->max_rate
= MIN(hc
->max_rate
, htb
->max_rate
);
3789 * According to hints in the documentation that I've read, it is important
3790 * that 'burst' be at least as big as the largest frame that might be
3791 * transmitted. Also, making 'burst' a bit bigger than necessary is OK,
3792 * but having it a bit too small is a problem. Since netdev_get_mtu()
3793 * doesn't include the Ethernet header, we need to add at least 14 (18?) to
3794 * the MTU. We actually add 64, instead of 14, as a guard against
3795 * additional headers get tacked on somewhere that we're not aware of. */
3796 hc
->burst
= smap_get_ullong(details
, "burst", 0) / 8;
3797 hc
->burst
= MAX(hc
->burst
, mtu
+ 64);
3800 hc
->priority
= smap_get_ullong(details
, "priority", 0);
3806 htb_query_class__(const struct netdev
*netdev
, unsigned int handle
,
3807 unsigned int parent
, struct htb_class
*options
,
3808 struct netdev_queue_stats
*stats
)
3810 struct ofpbuf
*reply
;
3813 error
= tc_query_class(netdev
, handle
, parent
, &reply
);
3815 error
= htb_parse_tcmsg__(reply
, NULL
, options
, stats
);
3816 ofpbuf_delete(reply
);
3822 htb_tc_install(struct netdev
*netdev
, const struct smap
*details
)
3826 error
= htb_setup_qdisc__(netdev
);
3828 struct htb_class hc
;
3830 htb_parse_qdisc_details__(netdev
, details
, &hc
);
3831 error
= htb_setup_class__(netdev
, tc_make_handle(1, 0xfffe),
3832 tc_make_handle(1, 0), &hc
);
3834 htb_install__(netdev
, hc
.max_rate
);
3840 static struct htb_class
*
3841 htb_class_cast__(const struct tc_queue
*queue
)
3843 return CONTAINER_OF(queue
, struct htb_class
, tc_queue
);
3847 htb_update_queue__(struct netdev
*netdev
, unsigned int queue_id
,
3848 const struct htb_class
*hc
)
3850 struct htb
*htb
= htb_get__(netdev
);
3851 size_t hash
= hash_int(queue_id
, 0);
3852 struct tc_queue
*queue
;
3853 struct htb_class
*hcp
;
3855 queue
= tc_find_queue__(netdev
, queue_id
, hash
);
3857 hcp
= htb_class_cast__(queue
);
3859 hcp
= xmalloc(sizeof *hcp
);
3860 queue
= &hcp
->tc_queue
;
3861 queue
->queue_id
= queue_id
;
3862 queue
->created
= time_msec();
3863 hmap_insert(&htb
->tc
.queues
, &queue
->hmap_node
, hash
);
3866 hcp
->min_rate
= hc
->min_rate
;
3867 hcp
->max_rate
= hc
->max_rate
;
3868 hcp
->burst
= hc
->burst
;
3869 hcp
->priority
= hc
->priority
;
3873 htb_tc_load(struct netdev
*netdev
, struct ofpbuf
*nlmsg OVS_UNUSED
)
3876 struct queue_dump_state state
;
3877 struct htb_class hc
;
3879 /* Get qdisc options. */
3881 htb_query_class__(netdev
, tc_make_handle(1, 0xfffe), 0, &hc
, NULL
);
3882 htb_install__(netdev
, hc
.max_rate
);
3885 if (!start_queue_dump(netdev
, &state
)) {
3888 while (nl_dump_next(&state
.dump
, &msg
, &state
.buf
)) {
3889 unsigned int queue_id
;
3891 if (!htb_parse_tcmsg__(&msg
, &queue_id
, &hc
, NULL
)) {
3892 htb_update_queue__(netdev
, queue_id
, &hc
);
3895 finish_queue_dump(&state
);
3901 htb_tc_destroy(struct tc
*tc
)
3903 struct htb
*htb
= CONTAINER_OF(tc
, struct htb
, tc
);
3904 struct htb_class
*hc
;
3906 HMAP_FOR_EACH_POP (hc
, tc_queue
.hmap_node
, &htb
->tc
.queues
) {
3914 htb_qdisc_get(const struct netdev
*netdev
, struct smap
*details
)
3916 const struct htb
*htb
= htb_get__(netdev
);
3917 smap_add_format(details
, "max-rate", "%llu", 8ULL * htb
->max_rate
);
3922 htb_qdisc_set(struct netdev
*netdev
, const struct smap
*details
)
3924 struct htb_class hc
;
3927 htb_parse_qdisc_details__(netdev
, details
, &hc
);
3928 error
= htb_setup_class__(netdev
, tc_make_handle(1, 0xfffe),
3929 tc_make_handle(1, 0), &hc
);
3931 htb_get__(netdev
)->max_rate
= hc
.max_rate
;
3937 htb_class_get(const struct netdev
*netdev OVS_UNUSED
,
3938 const struct tc_queue
*queue
, struct smap
*details
)
3940 const struct htb_class
*hc
= htb_class_cast__(queue
);
3942 smap_add_format(details
, "min-rate", "%llu", 8ULL * hc
->min_rate
);
3943 if (hc
->min_rate
!= hc
->max_rate
) {
3944 smap_add_format(details
, "max-rate", "%llu", 8ULL * hc
->max_rate
);
3946 smap_add_format(details
, "burst", "%llu", 8ULL * hc
->burst
);
3948 smap_add_format(details
, "priority", "%u", hc
->priority
);
3954 htb_class_set(struct netdev
*netdev
, unsigned int queue_id
,
3955 const struct smap
*details
)
3957 struct htb_class hc
;
3960 error
= htb_parse_class_details__(netdev
, details
, &hc
);
3965 error
= htb_setup_class__(netdev
, tc_make_handle(1, queue_id
+ 1),
3966 tc_make_handle(1, 0xfffe), &hc
);
3971 htb_update_queue__(netdev
, queue_id
, &hc
);
3976 htb_class_delete(struct netdev
*netdev
, struct tc_queue
*queue
)
3978 struct htb_class
*hc
= htb_class_cast__(queue
);
3979 struct htb
*htb
= htb_get__(netdev
);
3982 error
= tc_delete_class(netdev
, tc_make_handle(1, queue
->queue_id
+ 1));
3984 hmap_remove(&htb
->tc
.queues
, &hc
->tc_queue
.hmap_node
);
3991 htb_class_get_stats(const struct netdev
*netdev
, const struct tc_queue
*queue
,
3992 struct netdev_queue_stats
*stats
)
3994 return htb_query_class__(netdev
, tc_make_handle(1, queue
->queue_id
+ 1),
3995 tc_make_handle(1, 0xfffe), NULL
, stats
);
3999 htb_class_dump_stats(const struct netdev
*netdev OVS_UNUSED
,
4000 const struct ofpbuf
*nlmsg
,
4001 netdev_dump_queue_stats_cb
*cb
, void *aux
)
4003 struct netdev_queue_stats stats
;
4004 unsigned int handle
, major
, minor
;
4007 error
= tc_parse_class(nlmsg
, &handle
, NULL
, &stats
);
4012 major
= tc_get_major(handle
);
4013 minor
= tc_get_minor(handle
);
4014 if (major
== 1 && minor
> 0 && minor
<= HTB_N_QUEUES
) {
4015 (*cb
)(minor
- 1, &stats
, aux
);
4020 static const struct tc_ops tc_ops_htb
= {
4021 "htb", /* linux_name */
4022 "linux-htb", /* ovs_name */
4023 HTB_N_QUEUES
, /* n_queues */
4032 htb_class_get_stats
,
4033 htb_class_dump_stats
4036 /* "linux-hfsc" traffic control class. */
4038 #define HFSC_N_QUEUES 0xf000
4046 struct tc_queue tc_queue
;
4051 static struct hfsc
*
4052 hfsc_get__(const struct netdev
*netdev_
)
4054 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
4055 return CONTAINER_OF(netdev
->tc
, struct hfsc
, tc
);
4058 static struct hfsc_class
*
4059 hfsc_class_cast__(const struct tc_queue
*queue
)
4061 return CONTAINER_OF(queue
, struct hfsc_class
, tc_queue
);
4065 hfsc_install__(struct netdev
*netdev_
, uint32_t max_rate
)
4067 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
4070 hfsc
= xmalloc(sizeof *hfsc
);
4071 tc_init(&hfsc
->tc
, &tc_ops_hfsc
);
4072 hfsc
->max_rate
= max_rate
;
4073 netdev
->tc
= &hfsc
->tc
;
4077 hfsc_update_queue__(struct netdev
*netdev
, unsigned int queue_id
,
4078 const struct hfsc_class
*hc
)
4082 struct hfsc_class
*hcp
;
4083 struct tc_queue
*queue
;
4085 hfsc
= hfsc_get__(netdev
);
4086 hash
= hash_int(queue_id
, 0);
4088 queue
= tc_find_queue__(netdev
, queue_id
, hash
);
4090 hcp
= hfsc_class_cast__(queue
);
4092 hcp
= xmalloc(sizeof *hcp
);
4093 queue
= &hcp
->tc_queue
;
4094 queue
->queue_id
= queue_id
;
4095 queue
->created
= time_msec();
4096 hmap_insert(&hfsc
->tc
.queues
, &queue
->hmap_node
, hash
);
4099 hcp
->min_rate
= hc
->min_rate
;
4100 hcp
->max_rate
= hc
->max_rate
;
4104 hfsc_parse_tca_options__(struct nlattr
*nl_options
, struct hfsc_class
*class)
4106 const struct tc_service_curve
*rsc
, *fsc
, *usc
;
4107 static const struct nl_policy tca_hfsc_policy
[] = {
4109 .type
= NL_A_UNSPEC
,
4111 .min_len
= sizeof(struct tc_service_curve
),
4114 .type
= NL_A_UNSPEC
,
4116 .min_len
= sizeof(struct tc_service_curve
),
4119 .type
= NL_A_UNSPEC
,
4121 .min_len
= sizeof(struct tc_service_curve
),
4124 struct nlattr
*attrs
[ARRAY_SIZE(tca_hfsc_policy
)];
4126 if (!nl_parse_nested(nl_options
, tca_hfsc_policy
,
4127 attrs
, ARRAY_SIZE(tca_hfsc_policy
))) {
4128 VLOG_WARN_RL(&rl
, "failed to parse HFSC class options");
4132 rsc
= nl_attr_get(attrs
[TCA_HFSC_RSC
]);
4133 fsc
= nl_attr_get(attrs
[TCA_HFSC_FSC
]);
4134 usc
= nl_attr_get(attrs
[TCA_HFSC_USC
]);
4136 if (rsc
->m1
!= 0 || rsc
->d
!= 0 ||
4137 fsc
->m1
!= 0 || fsc
->d
!= 0 ||
4138 usc
->m1
!= 0 || usc
->d
!= 0) {
4139 VLOG_WARN_RL(&rl
, "failed to parse HFSC class options. "
4140 "Non-linear service curves are not supported.");
4144 if (rsc
->m2
!= fsc
->m2
) {
4145 VLOG_WARN_RL(&rl
, "failed to parse HFSC class options. "
4146 "Real-time service curves are not supported ");
4150 if (rsc
->m2
> usc
->m2
) {
4151 VLOG_WARN_RL(&rl
, "failed to parse HFSC class options. "
4152 "Min-rate service curve is greater than "
4153 "the max-rate service curve.");
4157 class->min_rate
= fsc
->m2
;
4158 class->max_rate
= usc
->m2
;
4163 hfsc_parse_tcmsg__(struct ofpbuf
*tcmsg
, unsigned int *queue_id
,
4164 struct hfsc_class
*options
,
4165 struct netdev_queue_stats
*stats
)
4168 unsigned int handle
;
4169 struct nlattr
*nl_options
;
4171 error
= tc_parse_class(tcmsg
, &handle
, &nl_options
, stats
);
4177 unsigned int major
, minor
;
4179 major
= tc_get_major(handle
);
4180 minor
= tc_get_minor(handle
);
4181 if (major
== 1 && minor
> 0 && minor
<= HFSC_N_QUEUES
) {
4182 *queue_id
= minor
- 1;
4189 error
= hfsc_parse_tca_options__(nl_options
, options
);
4196 hfsc_query_class__(const struct netdev
*netdev
, unsigned int handle
,
4197 unsigned int parent
, struct hfsc_class
*options
,
4198 struct netdev_queue_stats
*stats
)
4201 struct ofpbuf
*reply
;
4203 error
= tc_query_class(netdev
, handle
, parent
, &reply
);
4208 error
= hfsc_parse_tcmsg__(reply
, NULL
, options
, stats
);
4209 ofpbuf_delete(reply
);
4214 hfsc_parse_qdisc_details__(struct netdev
*netdev_
, const struct smap
*details
,
4215 struct hfsc_class
*class)
4217 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
4219 uint32_t max_rate
= smap_get_ullong(details
, "max-rate", 0) / 8;
4221 enum netdev_features current
;
4223 netdev_linux_read_features(netdev
);
4224 current
= !netdev
->get_features_error
? netdev
->current
: 0;
4225 max_rate
= netdev_features_to_bps(current
, 100 * 1000 * 1000) / 8;
4228 class->min_rate
= max_rate
;
4229 class->max_rate
= max_rate
;
4233 hfsc_parse_class_details__(struct netdev
*netdev
,
4234 const struct smap
*details
,
4235 struct hfsc_class
* class)
4237 const struct hfsc
*hfsc
;
4238 uint32_t min_rate
, max_rate
;
4240 hfsc
= hfsc_get__(netdev
);
4242 min_rate
= smap_get_ullong(details
, "min-rate", 0) / 8;
4243 min_rate
= MAX(min_rate
, 1);
4244 min_rate
= MIN(min_rate
, hfsc
->max_rate
);
4246 max_rate
= smap_get_ullong(details
, "max-rate", hfsc
->max_rate
* 8) / 8;
4247 max_rate
= MAX(max_rate
, min_rate
);
4248 max_rate
= MIN(max_rate
, hfsc
->max_rate
);
4250 class->min_rate
= min_rate
;
4251 class->max_rate
= max_rate
;
4256 /* Create an HFSC qdisc.
4258 * Equivalent to "tc qdisc add dev <dev> root handle 1: hfsc default 1". */
4260 hfsc_setup_qdisc__(struct netdev
* netdev
)
4262 struct tcmsg
*tcmsg
;
4263 struct ofpbuf request
;
4264 struct tc_hfsc_qopt opt
;
4266 tc_del_qdisc(netdev
);
4268 tcmsg
= netdev_linux_tc_make_request(netdev
, RTM_NEWQDISC
,
4269 NLM_F_EXCL
| NLM_F_CREATE
, &request
);
4275 tcmsg
->tcm_handle
= tc_make_handle(1, 0);
4276 tcmsg
->tcm_parent
= TC_H_ROOT
;
4278 memset(&opt
, 0, sizeof opt
);
4281 nl_msg_put_string(&request
, TCA_KIND
, "hfsc");
4282 nl_msg_put_unspec(&request
, TCA_OPTIONS
, &opt
, sizeof opt
);
4284 return tc_transact(&request
, NULL
);
4287 /* Create an HFSC class.
4289 * Equivalent to "tc class add <dev> parent <parent> classid <handle> hfsc
4290 * sc rate <min_rate> ul rate <max_rate>" */
4292 hfsc_setup_class__(struct netdev
*netdev
, unsigned int handle
,
4293 unsigned int parent
, struct hfsc_class
*class)
4297 struct tcmsg
*tcmsg
;
4298 struct ofpbuf request
;
4299 struct tc_service_curve min
, max
;
4301 tcmsg
= netdev_linux_tc_make_request(netdev
, RTM_NEWTCLASS
, NLM_F_CREATE
,
4308 tcmsg
->tcm_handle
= handle
;
4309 tcmsg
->tcm_parent
= parent
;
4313 min
.m2
= class->min_rate
;
4317 max
.m2
= class->max_rate
;
4319 nl_msg_put_string(&request
, TCA_KIND
, "hfsc");
4320 opt_offset
= nl_msg_start_nested(&request
, TCA_OPTIONS
);
4321 nl_msg_put_unspec(&request
, TCA_HFSC_RSC
, &min
, sizeof min
);
4322 nl_msg_put_unspec(&request
, TCA_HFSC_FSC
, &min
, sizeof min
);
4323 nl_msg_put_unspec(&request
, TCA_HFSC_USC
, &max
, sizeof max
);
4324 nl_msg_end_nested(&request
, opt_offset
);
4326 error
= tc_transact(&request
, NULL
);
4328 VLOG_WARN_RL(&rl
, "failed to replace %s class %u:%u, parent %u:%u, "
4329 "min-rate %ubps, max-rate %ubps (%s)",
4330 netdev_get_name(netdev
),
4331 tc_get_major(handle
), tc_get_minor(handle
),
4332 tc_get_major(parent
), tc_get_minor(parent
),
4333 class->min_rate
, class->max_rate
, ovs_strerror(error
));
4340 hfsc_tc_install(struct netdev
*netdev
, const struct smap
*details
)
4343 struct hfsc_class
class;
4345 error
= hfsc_setup_qdisc__(netdev
);
4351 hfsc_parse_qdisc_details__(netdev
, details
, &class);
4352 error
= hfsc_setup_class__(netdev
, tc_make_handle(1, 0xfffe),
4353 tc_make_handle(1, 0), &class);
4359 hfsc_install__(netdev
, class.max_rate
);
4364 hfsc_tc_load(struct netdev
*netdev
, struct ofpbuf
*nlmsg OVS_UNUSED
)
4367 struct queue_dump_state state
;
4368 struct hfsc_class hc
;
4371 hfsc_query_class__(netdev
, tc_make_handle(1, 0xfffe), 0, &hc
, NULL
);
4372 hfsc_install__(netdev
, hc
.max_rate
);
4374 if (!start_queue_dump(netdev
, &state
)) {
4378 while (nl_dump_next(&state
.dump
, &msg
, &state
.buf
)) {
4379 unsigned int queue_id
;
4381 if (!hfsc_parse_tcmsg__(&msg
, &queue_id
, &hc
, NULL
)) {
4382 hfsc_update_queue__(netdev
, queue_id
, &hc
);
4386 finish_queue_dump(&state
);
4391 hfsc_tc_destroy(struct tc
*tc
)
4394 struct hfsc_class
*hc
, *next
;
4396 hfsc
= CONTAINER_OF(tc
, struct hfsc
, tc
);
4398 HMAP_FOR_EACH_SAFE (hc
, next
, tc_queue
.hmap_node
, &hfsc
->tc
.queues
) {
4399 hmap_remove(&hfsc
->tc
.queues
, &hc
->tc_queue
.hmap_node
);
4408 hfsc_qdisc_get(const struct netdev
*netdev
, struct smap
*details
)
4410 const struct hfsc
*hfsc
;
4411 hfsc
= hfsc_get__(netdev
);
4412 smap_add_format(details
, "max-rate", "%llu", 8ULL * hfsc
->max_rate
);
4417 hfsc_qdisc_set(struct netdev
*netdev
, const struct smap
*details
)
4420 struct hfsc_class
class;
4422 hfsc_parse_qdisc_details__(netdev
, details
, &class);
4423 error
= hfsc_setup_class__(netdev
, tc_make_handle(1, 0xfffe),
4424 tc_make_handle(1, 0), &class);
4427 hfsc_get__(netdev
)->max_rate
= class.max_rate
;
4434 hfsc_class_get(const struct netdev
*netdev OVS_UNUSED
,
4435 const struct tc_queue
*queue
, struct smap
*details
)
4437 const struct hfsc_class
*hc
;
4439 hc
= hfsc_class_cast__(queue
);
4440 smap_add_format(details
, "min-rate", "%llu", 8ULL * hc
->min_rate
);
4441 if (hc
->min_rate
!= hc
->max_rate
) {
4442 smap_add_format(details
, "max-rate", "%llu", 8ULL * hc
->max_rate
);
4448 hfsc_class_set(struct netdev
*netdev
, unsigned int queue_id
,
4449 const struct smap
*details
)
4452 struct hfsc_class
class;
4454 error
= hfsc_parse_class_details__(netdev
, details
, &class);
4459 error
= hfsc_setup_class__(netdev
, tc_make_handle(1, queue_id
+ 1),
4460 tc_make_handle(1, 0xfffe), &class);
4465 hfsc_update_queue__(netdev
, queue_id
, &class);
4470 hfsc_class_delete(struct netdev
*netdev
, struct tc_queue
*queue
)
4474 struct hfsc_class
*hc
;
4476 hc
= hfsc_class_cast__(queue
);
4477 hfsc
= hfsc_get__(netdev
);
4479 error
= tc_delete_class(netdev
, tc_make_handle(1, queue
->queue_id
+ 1));
4481 hmap_remove(&hfsc
->tc
.queues
, &hc
->tc_queue
.hmap_node
);
4488 hfsc_class_get_stats(const struct netdev
*netdev
, const struct tc_queue
*queue
,
4489 struct netdev_queue_stats
*stats
)
4491 return hfsc_query_class__(netdev
, tc_make_handle(1, queue
->queue_id
+ 1),
4492 tc_make_handle(1, 0xfffe), NULL
, stats
);
4496 hfsc_class_dump_stats(const struct netdev
*netdev OVS_UNUSED
,
4497 const struct ofpbuf
*nlmsg
,
4498 netdev_dump_queue_stats_cb
*cb
, void *aux
)
4500 struct netdev_queue_stats stats
;
4501 unsigned int handle
, major
, minor
;
4504 error
= tc_parse_class(nlmsg
, &handle
, NULL
, &stats
);
4509 major
= tc_get_major(handle
);
4510 minor
= tc_get_minor(handle
);
4511 if (major
== 1 && minor
> 0 && minor
<= HFSC_N_QUEUES
) {
4512 (*cb
)(minor
- 1, &stats
, aux
);
4517 static const struct tc_ops tc_ops_hfsc
= {
4518 "hfsc", /* linux_name */
4519 "linux-hfsc", /* ovs_name */
4520 HFSC_N_QUEUES
, /* n_queues */
4521 hfsc_tc_install
, /* tc_install */
4522 hfsc_tc_load
, /* tc_load */
4523 hfsc_tc_destroy
, /* tc_destroy */
4524 hfsc_qdisc_get
, /* qdisc_get */
4525 hfsc_qdisc_set
, /* qdisc_set */
4526 hfsc_class_get
, /* class_get */
4527 hfsc_class_set
, /* class_set */
4528 hfsc_class_delete
, /* class_delete */
4529 hfsc_class_get_stats
, /* class_get_stats */
4530 hfsc_class_dump_stats
/* class_dump_stats */
4533 /* "linux-noop" traffic control class. */
4536 noop_install__(struct netdev
*netdev_
)
4538 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
4539 static const struct tc tc
= TC_INITIALIZER(&tc
, &tc_ops_default
);
4541 netdev
->tc
= CONST_CAST(struct tc
*, &tc
);
4545 noop_tc_install(struct netdev
*netdev
,
4546 const struct smap
*details OVS_UNUSED
)
4548 noop_install__(netdev
);
4553 noop_tc_load(struct netdev
*netdev
, struct ofpbuf
*nlmsg OVS_UNUSED
)
4555 noop_install__(netdev
);
4559 static const struct tc_ops tc_ops_noop
= {
4560 NULL
, /* linux_name */
4561 "linux-noop", /* ovs_name */
4565 NULL
, /* tc_destroy */
4566 NULL
, /* qdisc_get */
4567 NULL
, /* qdisc_set */
4568 NULL
, /* class_get */
4569 NULL
, /* class_set */
4570 NULL
, /* class_delete */
4571 NULL
, /* class_get_stats */
4572 NULL
/* class_dump_stats */
4575 /* "linux-default" traffic control class.
4577 * This class represents the default, unnamed Linux qdisc. It corresponds to
4578 * the "" (empty string) QoS type in the OVS database. */
4581 default_install__(struct netdev
*netdev_
)
4583 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
4584 static const struct tc tc
= TC_INITIALIZER(&tc
, &tc_ops_default
);
4586 /* Nothing but a tc class implementation is allowed to write to a tc. This
4587 * class never does that, so we can legitimately use a const tc object. */
4588 netdev
->tc
= CONST_CAST(struct tc
*, &tc
);
4592 default_tc_install(struct netdev
*netdev
,
4593 const struct smap
*details OVS_UNUSED
)
4595 default_install__(netdev
);
4600 default_tc_load(struct netdev
*netdev
, struct ofpbuf
*nlmsg OVS_UNUSED
)
4602 default_install__(netdev
);
4606 static const struct tc_ops tc_ops_default
= {
4607 NULL
, /* linux_name */
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 /* "linux-other" traffic control class.
4627 other_tc_load(struct netdev
*netdev_
, struct ofpbuf
*nlmsg OVS_UNUSED
)
4629 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
4630 static const struct tc tc
= TC_INITIALIZER(&tc
, &tc_ops_other
);
4632 /* Nothing but a tc class implementation is allowed to write to a tc. This
4633 * class never does that, so we can legitimately use a const tc object. */
4634 netdev
->tc
= CONST_CAST(struct tc
*, &tc
);
4638 static const struct tc_ops tc_ops_other
= {
4639 NULL
, /* linux_name */
4640 "linux-other", /* ovs_name */
4642 NULL
, /* tc_install */
4644 NULL
, /* tc_destroy */
4645 NULL
, /* qdisc_get */
4646 NULL
, /* qdisc_set */
4647 NULL
, /* class_get */
4648 NULL
, /* class_set */
4649 NULL
, /* class_delete */
4650 NULL
, /* class_get_stats */
4651 NULL
/* class_dump_stats */
4654 /* Traffic control. */
4656 /* Number of kernel "tc" ticks per second. */
4657 static double ticks_per_s
;
4659 /* Number of kernel "jiffies" per second. This is used for the purpose of
4660 * computing buffer sizes. Generally kernel qdiscs need to be able to buffer
4661 * one jiffy's worth of data.
4663 * There are two possibilities here:
4665 * - 'buffer_hz' is the kernel's real timer tick rate, a small number in the
4666 * approximate range of 100 to 1024. That means that we really need to
4667 * make sure that the qdisc can buffer that much data.
4669 * - 'buffer_hz' is an absurdly large number. That means that the kernel
4670 * has finely granular timers and there's no need to fudge additional room
4671 * for buffers. (There's no extra effort needed to implement that: the
4672 * large 'buffer_hz' is used as a divisor, so practically any number will
4673 * come out as 0 in the division. Small integer results in the case of
4674 * really high dividends won't have any real effect anyhow.)
4676 static unsigned int buffer_hz
;
4678 static struct tcmsg
*
4679 netdev_linux_tc_make_request(const struct netdev
*netdev
, int type
,
4680 unsigned int flags
, struct ofpbuf
*request
)
4685 error
= get_ifindex(netdev
, &ifindex
);
4690 return tc_make_request(ifindex
, type
, flags
, request
);
4693 /* Adds a policer to 'netdev' with a rate of 'kbits_rate' and a burst size
4696 * This function is equivalent to running:
4697 * /sbin/tc filter add dev <devname> parent ffff: protocol all prio 49
4698 * basic police rate <kbits_rate>kbit burst <kbits_burst>k
4701 * The configuration and stats may be seen with the following command:
4702 * /sbin/tc -s filter show dev <devname> parent ffff:
4704 * Returns 0 if successful, otherwise a positive errno value.
4707 tc_add_policer(struct netdev
*netdev
,
4708 uint32_t kbits_rate
, uint32_t kbits_burst
)
4710 struct tc_police tc_police
;
4711 struct ofpbuf request
;
4712 struct tcmsg
*tcmsg
;
4713 size_t basic_offset
;
4714 size_t police_offset
;
4718 memset(&tc_police
, 0, sizeof tc_police
);
4719 tc_police
.action
= TC_POLICE_SHOT
;
4720 tc_police
.mtu
= mtu
;
4721 tc_fill_rate(&tc_police
.rate
, ((uint64_t) kbits_rate
* 1000)/8, mtu
);
4723 /* The following appears wrong in one way: In networking a kilobit is
4724 * usually 1000 bits but this uses 1024 bits.
4726 * However if you "fix" those problems then "tc filter show ..." shows
4727 * "125000b", meaning 125,000 bits, when OVS configures it for 1000 kbit ==
4728 * 1,000,000 bits, whereas this actually ends up doing the right thing from
4729 * tc's point of view. Whatever. */
4730 tc_police
.burst
= tc_bytes_to_ticks(
4731 tc_police
.rate
.rate
, MIN(UINT32_MAX
/ 1024, kbits_burst
) * 1024 / 8);
4733 tcmsg
= netdev_linux_tc_make_request(netdev
, RTM_NEWTFILTER
,
4734 NLM_F_EXCL
| NLM_F_CREATE
, &request
);
4738 tcmsg
->tcm_parent
= tc_make_handle(0xffff, 0);
4739 tcmsg
->tcm_info
= tc_make_handle(49,
4740 (OVS_FORCE
uint16_t) htons(ETH_P_ALL
));
4742 nl_msg_put_string(&request
, TCA_KIND
, "basic");
4743 basic_offset
= nl_msg_start_nested(&request
, TCA_OPTIONS
);
4744 police_offset
= nl_msg_start_nested(&request
, TCA_BASIC_POLICE
);
4745 nl_msg_put_unspec(&request
, TCA_POLICE_TBF
, &tc_police
, sizeof tc_police
);
4746 tc_put_rtab(&request
, TCA_POLICE_RATE
, &tc_police
.rate
);
4747 nl_msg_end_nested(&request
, police_offset
);
4748 nl_msg_end_nested(&request
, basic_offset
);
4750 error
= tc_transact(&request
, NULL
);
4761 /* The values in psched are not individually very meaningful, but they are
4762 * important. The tables below show some values seen in the wild.
4766 * - "c" has always been a constant 1000000 since at least Linux 2.4.14.
4767 * (Before that, there are hints that it was 1000000000.)
4769 * - "d" can be unrealistically large, see the comment on 'buffer_hz'
4773 * -----------------------------------
4774 * [1] 000c8000 000f4240 000f4240 00000064
4775 * [2] 000003e8 00000400 000f4240 3b9aca00
4776 * [3] 000003e8 00000400 000f4240 3b9aca00
4777 * [4] 000003e8 00000400 000f4240 00000064
4778 * [5] 000003e8 00000040 000f4240 3b9aca00
4779 * [6] 000003e8 00000040 000f4240 000000f9
4781 * a b c d ticks_per_s buffer_hz
4782 * ------- --------- ---------- ------------- ----------- -------------
4783 * [1] 819,200 1,000,000 1,000,000 100 819,200 100
4784 * [2] 1,000 1,024 1,000,000 1,000,000,000 976,562 1,000,000,000
4785 * [3] 1,000 1,024 1,000,000 1,000,000,000 976,562 1,000,000,000
4786 * [4] 1,000 1,024 1,000,000 100 976,562 100
4787 * [5] 1,000 64 1,000,000 1,000,000,000 15,625,000 1,000,000,000
4788 * [6] 1,000 64 1,000,000 249 15,625,000 249
4790 * [1] 2.6.18-128.1.6.el5.xs5.5.0.505.1024xen from XenServer 5.5.0-24648p
4791 * [2] 2.6.26-1-686-bigmem from Debian lenny
4792 * [3] 2.6.26-2-sparc64 from Debian lenny
4793 * [4] 2.6.27.42-0.1.1.xs5.6.810.44.111163xen from XenServer 5.6.810-31078p
4794 * [5] 2.6.32.21.22 (approx.) from Ubuntu 10.04 on VMware Fusion
4795 * [6] 2.6.34 from kernel.org on KVM
4797 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
4798 static const char fn
[] = "/proc/net/psched";
4799 unsigned int a
, b
, c
, d
;
4802 if (!ovsthread_once_start(&once
)) {
4809 stream
= fopen(fn
, "r");
4811 VLOG_WARN("%s: open failed: %s", fn
, ovs_strerror(errno
));
4815 if (fscanf(stream
, "%x %x %x %x", &a
, &b
, &c
, &d
) != 4) {
4816 VLOG_WARN("%s: read failed", fn
);
4820 VLOG_DBG("%s: psched parameters are: %u %u %u %u", fn
, a
, b
, c
, d
);
4824 VLOG_WARN("%s: invalid scheduler parameters", fn
);
4828 ticks_per_s
= (double) a
* c
/ b
;
4832 VLOG_WARN("%s: unexpected psched parameters: %u %u %u %u",
4835 VLOG_DBG("%s: ticks_per_s=%f buffer_hz=%u", fn
, ticks_per_s
, buffer_hz
);
4838 ovsthread_once_done(&once
);
4841 /* Returns the number of bytes that can be transmitted in 'ticks' ticks at a
4842 * rate of 'rate' bytes per second. */
4844 tc_ticks_to_bytes(unsigned int rate
, unsigned int ticks
)
4847 return (rate
* ticks
) / ticks_per_s
;
4850 /* Returns the number of ticks that it would take to transmit 'size' bytes at a
4851 * rate of 'rate' bytes per second. */
4853 tc_bytes_to_ticks(unsigned int rate
, unsigned int size
)
4856 return rate
? ((unsigned long long int) ticks_per_s
* size
) / rate
: 0;
4859 /* Returns the number of bytes that need to be reserved for qdisc buffering at
4860 * a transmission rate of 'rate' bytes per second. */
4862 tc_buffer_per_jiffy(unsigned int rate
)
4865 return rate
/ buffer_hz
;
4868 /* Given Netlink 'msg' that describes a qdisc, extracts the name of the qdisc,
4869 * e.g. "htb", into '*kind' (if it is nonnull). If 'options' is nonnull,
4870 * extracts 'msg''s TCA_OPTIONS attributes into '*options' if it is present or
4871 * stores NULL into it if it is absent.
4873 * '*kind' and '*options' point into 'msg', so they are owned by whoever owns
4876 * Returns 0 if successful, otherwise a positive errno value. */
4878 tc_parse_qdisc(const struct ofpbuf
*msg
, const char **kind
,
4879 struct nlattr
**options
)
4881 static const struct nl_policy tca_policy
[] = {
4882 [TCA_KIND
] = { .type
= NL_A_STRING
, .optional
= false },
4883 [TCA_OPTIONS
] = { .type
= NL_A_NESTED
, .optional
= true },
4885 struct nlattr
*ta
[ARRAY_SIZE(tca_policy
)];
4887 if (!nl_policy_parse(msg
, NLMSG_HDRLEN
+ sizeof(struct tcmsg
),
4888 tca_policy
, ta
, ARRAY_SIZE(ta
))) {
4889 VLOG_WARN_RL(&rl
, "failed to parse qdisc message");
4894 *kind
= nl_attr_get_string(ta
[TCA_KIND
]);
4898 *options
= ta
[TCA_OPTIONS
];
4913 /* Given Netlink 'msg' that describes a class, extracts the queue ID (e.g. the
4914 * minor number of its class ID) into '*queue_id', its TCA_OPTIONS attribute
4915 * into '*options', and its queue statistics into '*stats'. Any of the output
4916 * arguments may be null.
4918 * Returns 0 if successful, otherwise a positive errno value. */
4920 tc_parse_class(const struct ofpbuf
*msg
, unsigned int *handlep
,
4921 struct nlattr
**options
, struct netdev_queue_stats
*stats
)
4923 static const struct nl_policy tca_policy
[] = {
4924 [TCA_OPTIONS
] = { .type
= NL_A_NESTED
, .optional
= false },
4925 [TCA_STATS2
] = { .type
= NL_A_NESTED
, .optional
= false },
4927 struct nlattr
*ta
[ARRAY_SIZE(tca_policy
)];
4929 if (!nl_policy_parse(msg
, NLMSG_HDRLEN
+ sizeof(struct tcmsg
),
4930 tca_policy
, ta
, ARRAY_SIZE(ta
))) {
4931 VLOG_WARN_RL(&rl
, "failed to parse class message");
4936 struct tcmsg
*tc
= ofpbuf_at_assert(msg
, NLMSG_HDRLEN
, sizeof *tc
);
4937 *handlep
= tc
->tcm_handle
;
4941 *options
= ta
[TCA_OPTIONS
];
4945 const struct gnet_stats_queue
*gsq
;
4946 struct gnet_stats_basic gsb
;
4948 static const struct nl_policy stats_policy
[] = {
4949 [TCA_STATS_BASIC
] = { .type
= NL_A_UNSPEC
, .optional
= false,
4950 .min_len
= sizeof gsb
},
4951 [TCA_STATS_QUEUE
] = { .type
= NL_A_UNSPEC
, .optional
= false,
4952 .min_len
= sizeof *gsq
},
4954 struct nlattr
*sa
[ARRAY_SIZE(stats_policy
)];
4956 if (!nl_parse_nested(ta
[TCA_STATS2
], stats_policy
,
4957 sa
, ARRAY_SIZE(sa
))) {
4958 VLOG_WARN_RL(&rl
, "failed to parse class stats");
4962 /* Alignment issues screw up the length of struct gnet_stats_basic on
4963 * some arch/bitsize combinations. Newer versions of Linux have a
4964 * struct gnet_stats_basic_packed, but we can't depend on that. The
4965 * easiest thing to do is just to make a copy. */
4966 memset(&gsb
, 0, sizeof gsb
);
4967 memcpy(&gsb
, nl_attr_get(sa
[TCA_STATS_BASIC
]),
4968 MIN(nl_attr_get_size(sa
[TCA_STATS_BASIC
]), sizeof gsb
));
4969 stats
->tx_bytes
= gsb
.bytes
;
4970 stats
->tx_packets
= gsb
.packets
;
4972 gsq
= nl_attr_get(sa
[TCA_STATS_QUEUE
]);
4973 stats
->tx_errors
= gsq
->drops
;
4983 memset(stats
, 0, sizeof *stats
);
4988 /* Queries the kernel for class with identifier 'handle' and parent 'parent'
4991 tc_query_class(const struct netdev
*netdev
,
4992 unsigned int handle
, unsigned int parent
,
4993 struct ofpbuf
**replyp
)
4995 struct ofpbuf request
;
4996 struct tcmsg
*tcmsg
;
4999 tcmsg
= netdev_linux_tc_make_request(netdev
, RTM_GETTCLASS
, NLM_F_ECHO
,
5004 tcmsg
->tcm_handle
= handle
;
5005 tcmsg
->tcm_parent
= parent
;
5007 error
= tc_transact(&request
, replyp
);
5009 VLOG_WARN_RL(&rl
, "query %s class %u:%u (parent %u:%u) failed (%s)",
5010 netdev_get_name(netdev
),
5011 tc_get_major(handle
), tc_get_minor(handle
),
5012 tc_get_major(parent
), tc_get_minor(parent
),
5013 ovs_strerror(error
));
5018 /* Equivalent to "tc class del dev <name> handle <handle>". */
5020 tc_delete_class(const struct netdev
*netdev
, unsigned int handle
)
5022 struct ofpbuf request
;
5023 struct tcmsg
*tcmsg
;
5026 tcmsg
= netdev_linux_tc_make_request(netdev
, RTM_DELTCLASS
, 0, &request
);
5030 tcmsg
->tcm_handle
= handle
;
5031 tcmsg
->tcm_parent
= 0;
5033 error
= tc_transact(&request
, NULL
);
5035 VLOG_WARN_RL(&rl
, "delete %s class %u:%u failed (%s)",
5036 netdev_get_name(netdev
),
5037 tc_get_major(handle
), tc_get_minor(handle
),
5038 ovs_strerror(error
));
5043 /* Equivalent to "tc qdisc del dev <name> root". */
5045 tc_del_qdisc(struct netdev
*netdev_
)
5047 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
5048 struct ofpbuf request
;
5049 struct tcmsg
*tcmsg
;
5052 tcmsg
= netdev_linux_tc_make_request(netdev_
, RTM_DELQDISC
, 0, &request
);
5056 tcmsg
->tcm_handle
= tc_make_handle(1, 0);
5057 tcmsg
->tcm_parent
= TC_H_ROOT
;
5059 error
= tc_transact(&request
, NULL
);
5060 if (error
== EINVAL
) {
5061 /* EINVAL probably means that the default qdisc was in use, in which
5062 * case we've accomplished our purpose. */
5065 if (!error
&& netdev
->tc
) {
5066 if (netdev
->tc
->ops
->tc_destroy
) {
5067 netdev
->tc
->ops
->tc_destroy(netdev
->tc
);
5075 getqdisc_is_safe(void)
5077 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
5078 static bool safe
= false;
5080 if (ovsthread_once_start(&once
)) {
5081 struct utsname utsname
;
5084 if (uname(&utsname
) == -1) {
5085 VLOG_WARN("uname failed (%s)", ovs_strerror(errno
));
5086 } else if (!ovs_scan(utsname
.release
, "%d.%d", &major
, &minor
)) {
5087 VLOG_WARN("uname reported bad OS release (%s)", utsname
.release
);
5088 } else if (major
< 2 || (major
== 2 && minor
< 35)) {
5089 VLOG_INFO("disabling unsafe RTM_GETQDISC in Linux kernel %s",
5094 ovsthread_once_done(&once
);
5099 /* If 'netdev''s qdisc type and parameters are not yet known, queries the
5100 * kernel to determine what they are. Returns 0 if successful, otherwise a
5101 * positive errno value. */
5103 tc_query_qdisc(const struct netdev
*netdev_
)
5105 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
5106 struct ofpbuf request
, *qdisc
;
5107 const struct tc_ops
*ops
;
5108 struct tcmsg
*tcmsg
;
5116 /* This RTM_GETQDISC is crafted to avoid OOPSing kernels that do not have
5117 * commit 53b0f08 "net_sched: Fix qdisc_notify()", which is anything before
5118 * 2.6.35 without that fix backported to it.
5120 * To avoid the OOPS, we must not make a request that would attempt to dump
5121 * a "built-in" qdisc, that is, the default pfifo_fast qdisc or one of a
5122 * few others. There are a few ways that I can see to do this, but most of
5123 * them seem to be racy (and if you lose the race the kernel OOPSes). The
5124 * technique chosen here is to assume that any non-default qdisc that we
5125 * create will have a class with handle 1:0. The built-in qdiscs only have
5126 * a class with handle 0:0.
5128 * On Linux 2.6.35+ we use the straightforward method because it allows us
5129 * to handle non-builtin qdiscs without handle 1:0 (e.g. codel). However,
5130 * in such a case we get no response at all from the kernel (!) if a
5131 * builtin qdisc is in use (which is later caught by "!error &&
5132 * !qdisc->size"). */
5133 tcmsg
= netdev_linux_tc_make_request(netdev_
, RTM_GETQDISC
, NLM_F_ECHO
,
5138 tcmsg
->tcm_handle
= tc_make_handle(getqdisc_is_safe() ? 0 : 1, 0);
5139 tcmsg
->tcm_parent
= getqdisc_is_safe() ? TC_H_ROOT
: 0;
5141 /* Figure out what tc class to instantiate. */
5142 error
= tc_transact(&request
, &qdisc
);
5143 if (!error
&& qdisc
->size
) {
5146 error
= tc_parse_qdisc(qdisc
, &kind
, NULL
);
5148 ops
= &tc_ops_other
;
5150 ops
= tc_lookup_linux_name(kind
);
5152 static struct vlog_rate_limit rl2
= VLOG_RATE_LIMIT_INIT(1, 1);
5153 VLOG_DBG_RL(&rl2
, "unknown qdisc \"%s\"", kind
);
5155 ops
= &tc_ops_other
;
5158 } else if ((!error
&& !qdisc
->size
) || error
== ENOENT
) {
5159 /* Either it's a built-in qdisc, or (on Linux pre-2.6.35) it's a qdisc
5160 * set up by some other entity that doesn't have a handle 1:0. We will
5161 * assume that it's the system default qdisc. */
5162 ops
= &tc_ops_default
;
5165 /* Who knows? Maybe the device got deleted. */
5166 VLOG_WARN_RL(&rl
, "query %s qdisc failed (%s)",
5167 netdev_get_name(netdev_
), ovs_strerror(error
));
5168 ops
= &tc_ops_other
;
5171 /* Instantiate it. */
5172 load_error
= ops
->tc_load(CONST_CAST(struct netdev
*, netdev_
), qdisc
);
5173 ovs_assert((load_error
== 0) == (netdev
->tc
!= NULL
));
5174 ofpbuf_delete(qdisc
);
5176 return error
? error
: load_error
;
5179 /* Linux traffic control uses tables with 256 entries ("rtab" tables) to
5180 approximate the time to transmit packets of various lengths. For an MTU of
5181 256 or less, each entry is exact; for an MTU of 257 through 512, each entry
5182 represents two possible packet lengths; for a MTU of 513 through 1024, four
5183 possible lengths; and so on.
5185 Returns, for the specified 'mtu', the number of bits that packet lengths
5186 need to be shifted right to fit within such a 256-entry table. */
5188 tc_calc_cell_log(unsigned int mtu
)
5193 mtu
= ETH_PAYLOAD_MAX
;
5195 mtu
+= ETH_HEADER_LEN
+ VLAN_HEADER_LEN
;
5197 for (cell_log
= 0; mtu
>= 256; cell_log
++) {
5204 /* Initializes 'rate' properly for a rate of 'Bps' bytes per second with an MTU
5207 tc_fill_rate(struct tc_ratespec
*rate
, uint64_t Bps
, int mtu
)
5209 memset(rate
, 0, sizeof *rate
);
5210 rate
->cell_log
= tc_calc_cell_log(mtu
);
5211 /* rate->overhead = 0; */ /* New in 2.6.24, not yet in some */
5212 /* rate->cell_align = 0; */ /* distro headers. */
5213 rate
->mpu
= ETH_TOTAL_MIN
;
5217 /* Appends to 'msg' an "rtab" table for the specified 'rate' as a Netlink
5218 * attribute of the specified "type".
5220 * See tc_calc_cell_log() above for a description of "rtab"s. */
5222 tc_put_rtab(struct ofpbuf
*msg
, uint16_t type
, const struct tc_ratespec
*rate
)
5227 rtab
= nl_msg_put_unspec_uninit(msg
, type
, TC_RTAB_SIZE
);
5228 for (i
= 0; i
< TC_RTAB_SIZE
/ sizeof *rtab
; i
++) {
5229 unsigned packet_size
= (i
+ 1) << rate
->cell_log
;
5230 if (packet_size
< rate
->mpu
) {
5231 packet_size
= rate
->mpu
;
5233 rtab
[i
] = tc_bytes_to_ticks(rate
->rate
, packet_size
);
5237 /* Calculates the proper value of 'buffer' or 'cbuffer' in HTB options given a
5238 * rate of 'Bps' bytes per second, the specified 'mtu', and a user-requested
5239 * burst size of 'burst_bytes'. (If no value was requested, a 'burst_bytes' of
5242 tc_calc_buffer(unsigned int Bps
, int mtu
, uint64_t burst_bytes
)
5244 unsigned int min_burst
= tc_buffer_per_jiffy(Bps
) + mtu
;
5245 return tc_bytes_to_ticks(Bps
, MAX(burst_bytes
, min_burst
));
5248 /* Linux-only functions declared in netdev-linux.h */
5250 /* Modifies the 'flag' bit in ethtool's flags field for 'netdev'. If
5251 * 'enable' is true, the bit is set. Otherwise, it is cleared. */
5253 netdev_linux_ethtool_set_flag(struct netdev
*netdev
, uint32_t flag
,
5254 const char *flag_name
, bool enable
)
5256 const char *netdev_name
= netdev_get_name(netdev
);
5257 struct ethtool_value evalue
;
5261 COVERAGE_INC(netdev_get_ethtool
);
5262 memset(&evalue
, 0, sizeof evalue
);
5263 error
= netdev_linux_do_ethtool(netdev_name
,
5264 (struct ethtool_cmd
*)&evalue
,
5265 ETHTOOL_GFLAGS
, "ETHTOOL_GFLAGS");
5270 COVERAGE_INC(netdev_set_ethtool
);
5271 new_flags
= (evalue
.data
& ~flag
) | (enable
? flag
: 0);
5272 if (new_flags
== evalue
.data
) {
5275 evalue
.data
= new_flags
;
5276 error
= netdev_linux_do_ethtool(netdev_name
,
5277 (struct ethtool_cmd
*)&evalue
,
5278 ETHTOOL_SFLAGS
, "ETHTOOL_SFLAGS");
5283 COVERAGE_INC(netdev_get_ethtool
);
5284 memset(&evalue
, 0, sizeof evalue
);
5285 error
= netdev_linux_do_ethtool(netdev_name
,
5286 (struct ethtool_cmd
*)&evalue
,
5287 ETHTOOL_GFLAGS
, "ETHTOOL_GFLAGS");
5292 if (new_flags
!= evalue
.data
) {
5293 VLOG_WARN_RL(&rl
, "attempt to %s ethtool %s flag on network "
5294 "device %s failed", enable
? "enable" : "disable",
5295 flag_name
, netdev_name
);
5302 /* Utility functions. */
5304 /* Copies 'src' into 'dst', performing format conversion in the process. */
5306 netdev_stats_from_rtnl_link_stats(struct netdev_stats
*dst
,
5307 const struct rtnl_link_stats
*src
)
5309 dst
->rx_packets
= src
->rx_packets
;
5310 dst
->tx_packets
= src
->tx_packets
;
5311 dst
->rx_bytes
= src
->rx_bytes
;
5312 dst
->tx_bytes
= src
->tx_bytes
;
5313 dst
->rx_errors
= src
->rx_errors
;
5314 dst
->tx_errors
= src
->tx_errors
;
5315 dst
->rx_dropped
= src
->rx_dropped
;
5316 dst
->tx_dropped
= src
->tx_dropped
;
5317 dst
->multicast
= src
->multicast
;
5318 dst
->collisions
= src
->collisions
;
5319 dst
->rx_length_errors
= src
->rx_length_errors
;
5320 dst
->rx_over_errors
= src
->rx_over_errors
;
5321 dst
->rx_crc_errors
= src
->rx_crc_errors
;
5322 dst
->rx_frame_errors
= src
->rx_frame_errors
;
5323 dst
->rx_fifo_errors
= src
->rx_fifo_errors
;
5324 dst
->rx_missed_errors
= src
->rx_missed_errors
;
5325 dst
->tx_aborted_errors
= src
->tx_aborted_errors
;
5326 dst
->tx_carrier_errors
= src
->tx_carrier_errors
;
5327 dst
->tx_fifo_errors
= src
->tx_fifo_errors
;
5328 dst
->tx_heartbeat_errors
= src
->tx_heartbeat_errors
;
5329 dst
->tx_window_errors
= src
->tx_window_errors
;
5332 /* Copies 'src' into 'dst', performing format conversion in the process. */
5334 netdev_stats_from_rtnl_link_stats64(struct netdev_stats
*dst
,
5335 const struct rtnl_link_stats64
*src
)
5337 dst
->rx_packets
= src
->rx_packets
;
5338 dst
->tx_packets
= src
->tx_packets
;
5339 dst
->rx_bytes
= src
->rx_bytes
;
5340 dst
->tx_bytes
= src
->tx_bytes
;
5341 dst
->rx_errors
= src
->rx_errors
;
5342 dst
->tx_errors
= src
->tx_errors
;
5343 dst
->rx_dropped
= src
->rx_dropped
;
5344 dst
->tx_dropped
= src
->tx_dropped
;
5345 dst
->multicast
= src
->multicast
;
5346 dst
->collisions
= src
->collisions
;
5347 dst
->rx_length_errors
= src
->rx_length_errors
;
5348 dst
->rx_over_errors
= src
->rx_over_errors
;
5349 dst
->rx_crc_errors
= src
->rx_crc_errors
;
5350 dst
->rx_frame_errors
= src
->rx_frame_errors
;
5351 dst
->rx_fifo_errors
= src
->rx_fifo_errors
;
5352 dst
->rx_missed_errors
= src
->rx_missed_errors
;
5353 dst
->tx_aborted_errors
= src
->tx_aborted_errors
;
5354 dst
->tx_carrier_errors
= src
->tx_carrier_errors
;
5355 dst
->tx_fifo_errors
= src
->tx_fifo_errors
;
5356 dst
->tx_heartbeat_errors
= src
->tx_heartbeat_errors
;
5357 dst
->tx_window_errors
= src
->tx_window_errors
;
5361 get_stats_via_netlink(const struct netdev
*netdev_
, struct netdev_stats
*stats
)
5363 struct ofpbuf request
;
5364 struct ofpbuf
*reply
;
5367 /* Filtering all counters by default */
5368 memset(stats
, 0xFF, sizeof(struct netdev_stats
));
5370 ofpbuf_init(&request
, 0);
5371 nl_msg_put_nlmsghdr(&request
,
5372 sizeof(struct ifinfomsg
) + NL_ATTR_SIZE(IFNAMSIZ
),
5373 RTM_GETLINK
, NLM_F_REQUEST
);
5374 ofpbuf_put_zeros(&request
, sizeof(struct ifinfomsg
));
5375 nl_msg_put_string(&request
, IFLA_IFNAME
, netdev_get_name(netdev_
));
5376 error
= nl_transact(NETLINK_ROUTE
, &request
, &reply
);
5377 ofpbuf_uninit(&request
);
5382 if (ofpbuf_try_pull(reply
, NLMSG_HDRLEN
+ sizeof(struct ifinfomsg
))) {
5383 const struct nlattr
*a
= nl_attr_find(reply
, 0, IFLA_STATS64
);
5384 if (a
&& nl_attr_get_size(a
) >= sizeof(struct rtnl_link_stats64
)) {
5385 netdev_stats_from_rtnl_link_stats64(stats
, nl_attr_get(a
));
5388 a
= nl_attr_find(reply
, 0, IFLA_STATS
);
5389 if (a
&& nl_attr_get_size(a
) >= sizeof(struct rtnl_link_stats
)) {
5390 netdev_stats_from_rtnl_link_stats(stats
, nl_attr_get(a
));
5393 VLOG_WARN_RL(&rl
, "RTM_GETLINK reply lacks stats");
5398 VLOG_WARN_RL(&rl
, "short RTM_GETLINK reply");
5403 ofpbuf_delete(reply
);
5408 get_flags(const struct netdev
*dev
, unsigned int *flags
)
5414 error
= af_inet_ifreq_ioctl(dev
->name
, &ifr
, SIOCGIFFLAGS
, "SIOCGIFFLAGS");
5416 *flags
= ifr
.ifr_flags
;
5422 set_flags(const char *name
, unsigned int flags
)
5426 ifr
.ifr_flags
= flags
;
5427 return af_inet_ifreq_ioctl(name
, &ifr
, SIOCSIFFLAGS
, "SIOCSIFFLAGS");
5431 linux_get_ifindex(const char *netdev_name
)
5436 ovs_strzcpy(ifr
.ifr_name
, netdev_name
, sizeof ifr
.ifr_name
);
5437 COVERAGE_INC(netdev_get_ifindex
);
5439 error
= af_inet_ioctl(SIOCGIFINDEX
, &ifr
);
5441 /* ENODEV probably means that a vif disappeared asynchronously and
5442 * hasn't been removed from the database yet, so reduce the log level
5443 * to INFO for that case. */
5444 VLOG_RL(&rl
, error
== ENODEV
? VLL_INFO
: VLL_ERR
,
5445 "ioctl(SIOCGIFINDEX) on %s device failed: %s",
5446 netdev_name
, ovs_strerror(error
));
5449 return ifr
.ifr_ifindex
;
5453 get_ifindex(const struct netdev
*netdev_
, int *ifindexp
)
5455 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
5457 if (!(netdev
->cache_valid
& VALID_IFINDEX
)) {
5458 int ifindex
= linux_get_ifindex(netdev_get_name(netdev_
));
5461 netdev
->get_ifindex_error
= -ifindex
;
5462 netdev
->ifindex
= 0;
5464 netdev
->get_ifindex_error
= 0;
5465 netdev
->ifindex
= ifindex
;
5467 netdev
->cache_valid
|= VALID_IFINDEX
;
5470 *ifindexp
= netdev
->ifindex
;
5471 return netdev
->get_ifindex_error
;
5475 get_etheraddr(const char *netdev_name
, struct eth_addr
*ea
)
5481 memset(&ifr
, 0, sizeof ifr
);
5482 ovs_strzcpy(ifr
.ifr_name
, netdev_name
, sizeof ifr
.ifr_name
);
5483 COVERAGE_INC(netdev_get_hwaddr
);
5484 error
= af_inet_ioctl(SIOCGIFHWADDR
, &ifr
);
5486 /* ENODEV probably means that a vif disappeared asynchronously and
5487 * hasn't been removed from the database yet, so reduce the log level
5488 * to INFO for that case. */
5489 VLOG(error
== ENODEV
? VLL_INFO
: VLL_ERR
,
5490 "ioctl(SIOCGIFHWADDR) on %s device failed: %s",
5491 netdev_name
, ovs_strerror(error
));
5494 hwaddr_family
= ifr
.ifr_hwaddr
.sa_family
;
5495 if (hwaddr_family
!= AF_UNSPEC
&& hwaddr_family
!= ARPHRD_ETHER
&&
5496 hwaddr_family
!= ARPHRD_NONE
) {
5497 VLOG_INFO("%s device has unknown hardware address family %d",
5498 netdev_name
, hwaddr_family
);
5501 memcpy(ea
, ifr
.ifr_hwaddr
.sa_data
, ETH_ADDR_LEN
);
5506 set_etheraddr(const char *netdev_name
, const struct eth_addr mac
)
5511 memset(&ifr
, 0, sizeof ifr
);
5512 ovs_strzcpy(ifr
.ifr_name
, netdev_name
, sizeof ifr
.ifr_name
);
5513 ifr
.ifr_hwaddr
.sa_family
= ARPHRD_ETHER
;
5514 memcpy(ifr
.ifr_hwaddr
.sa_data
, &mac
, ETH_ADDR_LEN
);
5515 COVERAGE_INC(netdev_set_hwaddr
);
5516 error
= af_inet_ioctl(SIOCSIFHWADDR
, &ifr
);
5518 VLOG_ERR("ioctl(SIOCSIFHWADDR) on %s device failed: %s",
5519 netdev_name
, ovs_strerror(error
));
5525 netdev_linux_do_ethtool(const char *name
, struct ethtool_cmd
*ecmd
,
5526 int cmd
, const char *cmd_name
)
5531 memset(&ifr
, 0, sizeof ifr
);
5532 ovs_strzcpy(ifr
.ifr_name
, name
, sizeof ifr
.ifr_name
);
5533 ifr
.ifr_data
= (caddr_t
) ecmd
;
5536 error
= af_inet_ioctl(SIOCETHTOOL
, &ifr
);
5538 if (error
!= EOPNOTSUPP
) {
5539 VLOG_WARN_RL(&rl
, "ethtool command %s on network device %s "
5540 "failed: %s", cmd_name
, name
, ovs_strerror(error
));
5542 /* The device doesn't support this operation. That's pretty
5543 * common, so there's no point in logging anything. */
5549 /* Returns an AF_PACKET raw socket or a negative errno value. */
5551 af_packet_sock(void)
5553 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
5556 if (ovsthread_once_start(&once
)) {
5557 sock
= socket(AF_PACKET
, SOCK_RAW
, 0);
5559 int error
= set_nonblocking(sock
);
5566 VLOG_ERR("failed to create packet socket: %s",
5567 ovs_strerror(errno
));
5569 ovsthread_once_done(&once
);