2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "netdev-linux.h"
23 #include <arpa/inet.h>
25 #include <linux/filter.h>
26 #include <linux/gen_stats.h>
27 #include <linux/if_ether.h>
28 #include <linux/if_tun.h>
29 #include <linux/types.h>
30 #include <linux/ethtool.h>
31 #include <linux/mii.h>
32 #include <linux/pkt_cls.h>
33 #include <linux/pkt_sched.h>
34 #include <linux/rtnetlink.h>
35 #include <linux/sockios.h>
36 #include <sys/types.h>
37 #include <sys/ioctl.h>
38 #include <sys/socket.h>
39 #include <sys/utsname.h>
40 #include <netpacket/packet.h>
42 #include <net/if_arp.h>
43 #include <net/if_packet.h>
44 #include <net/route.h>
45 #include <netinet/in.h>
52 #include "dp-packet.h"
53 #include "dpif-netlink.h"
54 #include "dpif-netdev.h"
55 #include "openvswitch/dynamic-string.h"
56 #include "fatal-signal.h"
58 #include "openvswitch/hmap.h"
59 #include "netdev-provider.h"
60 #include "netdev-vport.h"
61 #include "netlink-notifier.h"
62 #include "netlink-socket.h"
64 #include "openvswitch/ofpbuf.h"
65 #include "openflow/openflow.h"
66 #include "ovs-atomic.h"
68 #include "poll-loop.h"
69 #include "rtnetlink.h"
70 #include "openvswitch/shash.h"
71 #include "socket-util.h"
74 #include "unaligned.h"
75 #include "openvswitch/vlog.h"
78 VLOG_DEFINE_THIS_MODULE(netdev_linux
);
80 COVERAGE_DEFINE(netdev_set_policing
);
81 COVERAGE_DEFINE(netdev_arp_lookup
);
82 COVERAGE_DEFINE(netdev_get_ifindex
);
83 COVERAGE_DEFINE(netdev_get_hwaddr
);
84 COVERAGE_DEFINE(netdev_set_hwaddr
);
85 COVERAGE_DEFINE(netdev_get_ethtool
);
86 COVERAGE_DEFINE(netdev_set_ethtool
);
89 /* These were introduced in Linux 2.6.14, so they might be missing if we have
91 #ifndef ADVERTISED_Pause
92 #define ADVERTISED_Pause (1 << 13)
94 #ifndef ADVERTISED_Asym_Pause
95 #define ADVERTISED_Asym_Pause (1 << 14)
98 /* These were introduced in Linux 2.6.24, so they might be missing if we
99 * have old headers. */
100 #ifndef ETHTOOL_GFLAGS
101 #define ETHTOOL_GFLAGS 0x00000025 /* Get flags bitmap(ethtool_value) */
103 #ifndef ETHTOOL_SFLAGS
104 #define ETHTOOL_SFLAGS 0x00000026 /* Set flags bitmap(ethtool_value) */
107 /* This was introduced in Linux 2.6.25, so it might be missing if we have old
110 #define TC_RTAB_SIZE 1024
113 /* Linux 2.6.21 introduced struct tpacket_auxdata.
114 * Linux 2.6.27 added the tp_vlan_tci member.
115 * Linux 3.0 defined TP_STATUS_VLAN_VALID.
116 * Linux 3.13 repurposed a padding member for tp_vlan_tpid and defined
117 * TP_STATUS_VLAN_TPID_VALID.
119 * With all this churn it's easiest to unconditionally define a replacement
120 * structure that has everything we want.
122 #ifndef PACKET_AUXDATA
123 #define PACKET_AUXDATA 8
125 #ifndef TP_STATUS_VLAN_VALID
126 #define TP_STATUS_VLAN_VALID (1 << 4)
128 #ifndef TP_STATUS_VLAN_TPID_VALID
129 #define TP_STATUS_VLAN_TPID_VALID (1 << 6)
131 #undef tpacket_auxdata
132 #define tpacket_auxdata rpl_tpacket_auxdata
133 struct tpacket_auxdata
{
139 uint16_t tp_vlan_tci
;
140 uint16_t tp_vlan_tpid
;
143 /* Linux 2.6.27 introduced ethtool_cmd_speed
145 * To avoid revisiting problems reported with using configure to detect
146 * compatibility (see report at
147 * http://openvswitch.org/pipermail/dev/2014-October/047978.html)
148 * unconditionally replace ethtool_cmd_speed. */
149 #define ethtool_cmd_speed rpl_ethtool_cmd_speed
150 static inline uint32_t rpl_ethtool_cmd_speed(const struct ethtool_cmd
*ep
)
152 return ep
->speed
| (ep
->speed_hi
<< 16);
155 /* Linux 2.6.30 introduced supported and advertised flags for
156 * 1G base KX, and 10G base KX4, KR and R. */
157 #ifndef SUPPORTED_1000baseKX_Full
158 #define SUPPORTED_1000baseKX_Full (1 << 17)
159 #define SUPPORTED_10000baseKX4_Full (1 << 18)
160 #define SUPPORTED_10000baseKR_Full (1 << 19)
161 #define SUPPORTED_10000baseR_FEC (1 << 20)
162 #define ADVERTISED_1000baseKX_Full (1 << 17)
163 #define ADVERTISED_10000baseKX4_Full (1 << 18)
164 #define ADVERTISED_10000baseKR_Full (1 << 19)
165 #define ADVERTISED_10000baseR_FEC (1 << 20)
168 /* Linux 3.5 introduced supported and advertised flags for
169 * 40G base KR4, CR4, SR4 and LR4. */
170 #ifndef SUPPORTED_40000baseKR4_Full
171 #define SUPPORTED_40000baseKR4_Full (1 << 23)
172 #define SUPPORTED_40000baseCR4_Full (1 << 24)
173 #define SUPPORTED_40000baseSR4_Full (1 << 25)
174 #define SUPPORTED_40000baseLR4_Full (1 << 26)
175 #define ADVERTISED_40000baseKR4_Full (1 << 23)
176 #define ADVERTISED_40000baseCR4_Full (1 << 24)
177 #define ADVERTISED_40000baseSR4_Full (1 << 25)
178 #define ADVERTISED_40000baseLR4_Full (1 << 26)
181 /* Linux 2.6.35 introduced IFLA_STATS64 and rtnl_link_stats64.
183 * Tests for rtnl_link_stats64 don't seem to consistently work, e.g. on
184 * 2.6.32-431.29.2.el6.x86_64 (see report at
185 * http://openvswitch.org/pipermail/dev/2014-October/047978.html). Maybe
186 * if_link.h is not self-contained on those kernels. It is easiest to
187 * unconditionally define a replacement. */
189 #define IFLA_STATS64 23
191 #define rtnl_link_stats64 rpl_rtnl_link_stats64
192 struct rtnl_link_stats64
{
204 uint64_t rx_length_errors
;
205 uint64_t rx_over_errors
;
206 uint64_t rx_crc_errors
;
207 uint64_t rx_frame_errors
;
208 uint64_t rx_fifo_errors
;
209 uint64_t rx_missed_errors
;
211 uint64_t tx_aborted_errors
;
212 uint64_t tx_carrier_errors
;
213 uint64_t tx_fifo_errors
;
214 uint64_t tx_heartbeat_errors
;
215 uint64_t tx_window_errors
;
217 uint64_t rx_compressed
;
218 uint64_t tx_compressed
;
222 VALID_IFINDEX
= 1 << 0,
223 VALID_ETHERADDR
= 1 << 1,
226 VALID_POLICING
= 1 << 4,
227 VALID_VPORT_STAT_ERROR
= 1 << 5,
228 VALID_DRVINFO
= 1 << 6,
229 VALID_FEATURES
= 1 << 7,
232 /* Traffic control. */
234 /* An instance of a traffic control class. Always associated with a particular
237 * Each TC implementation subclasses this with whatever additional data it
240 const struct tc_ops
*ops
;
241 struct hmap queues
; /* Contains "struct tc_queue"s.
242 * Read by generic TC layer.
243 * Written only by TC implementation. */
246 #define TC_INITIALIZER(TC, OPS) { OPS, HMAP_INITIALIZER(&(TC)->queues) }
248 /* One traffic control queue.
250 * Each TC implementation subclasses this with whatever additional data it
253 struct hmap_node hmap_node
; /* In struct tc's "queues" hmap. */
254 unsigned int queue_id
; /* OpenFlow queue ID. */
255 long long int created
; /* Time queue was created, in msecs. */
258 /* A particular kind of traffic control. Each implementation generally maps to
259 * one particular Linux qdisc class.
261 * The functions below return 0 if successful or a positive errno value on
262 * failure, except where otherwise noted. All of them must be provided, except
263 * where otherwise noted. */
265 /* Name used by kernel in the TCA_KIND attribute of tcmsg, e.g. "htb".
266 * This is null for tc_ops_default and tc_ops_other, for which there are no
267 * appropriate values. */
268 const char *linux_name
;
270 /* Name used in OVS database, e.g. "linux-htb". Must be nonnull. */
271 const char *ovs_name
;
273 /* Number of supported OpenFlow queues, 0 for qdiscs that have no
274 * queues. The queues are numbered 0 through n_queues - 1. */
275 unsigned int n_queues
;
277 /* Called to install this TC class on 'netdev'. The implementation should
278 * make the Netlink calls required to set up 'netdev' with the right qdisc
279 * and configure it according to 'details'. The implementation may assume
280 * that the current qdisc is the default; that is, there is no need for it
281 * to delete the current qdisc before installing itself.
283 * The contents of 'details' should be documented as valid for 'ovs_name'
284 * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml
285 * (which is built as ovs-vswitchd.conf.db(8)).
287 * This function must return 0 if and only if it sets 'netdev->tc' to an
288 * initialized 'struct tc'.
290 * (This function is null for tc_ops_other, which cannot be installed. For
291 * other TC classes it should always be nonnull.) */
292 int (*tc_install
)(struct netdev
*netdev
, const struct smap
*details
);
294 /* Called when the netdev code determines (through a Netlink query) that
295 * this TC class's qdisc is installed on 'netdev', but we didn't install
296 * it ourselves and so don't know any of the details.
298 * 'nlmsg' is the kernel reply to a RTM_GETQDISC Netlink message for
299 * 'netdev'. The TCA_KIND attribute of 'nlmsg' is 'linux_name'. The
300 * implementation should parse the other attributes of 'nlmsg' as
301 * necessary to determine its configuration. If necessary it should also
302 * use Netlink queries to determine the configuration of queues on
305 * This function must return 0 if and only if it sets 'netdev->tc' to an
306 * initialized 'struct tc'. */
307 int (*tc_load
)(struct netdev
*netdev
, struct ofpbuf
*nlmsg
);
309 /* Destroys the data structures allocated by the implementation as part of
310 * 'tc'. (This includes destroying 'tc->queues' by calling
313 * The implementation should not need to perform any Netlink calls. If
314 * desirable, the caller is responsible for deconfiguring the kernel qdisc.
315 * (But it may not be desirable.)
317 * This function may be null if 'tc' is trivial. */
318 void (*tc_destroy
)(struct tc
*tc
);
320 /* Retrieves details of 'netdev->tc' configuration into 'details'.
322 * The implementation should not need to perform any Netlink calls, because
323 * the 'tc_install' or 'tc_load' that instantiated 'netdev->tc' should have
324 * cached the configuration.
326 * The contents of 'details' should be documented as valid for 'ovs_name'
327 * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml
328 * (which is built as ovs-vswitchd.conf.db(8)).
330 * This function may be null if 'tc' is not configurable.
332 int (*qdisc_get
)(const struct netdev
*netdev
, struct smap
*details
);
334 /* Reconfigures 'netdev->tc' according to 'details', performing any
335 * required Netlink calls to complete the reconfiguration.
337 * The contents of 'details' should be documented as valid for 'ovs_name'
338 * in the "other_config" column in the "QoS" table in vswitchd/vswitch.xml
339 * (which is built as ovs-vswitchd.conf.db(8)).
341 * This function may be null if 'tc' is not configurable.
343 int (*qdisc_set
)(struct netdev
*, const struct smap
*details
);
345 /* Retrieves details of 'queue' on 'netdev->tc' into 'details'. 'queue' is
346 * one of the 'struct tc_queue's within 'netdev->tc->queues'.
348 * The contents of 'details' should be documented as valid for 'ovs_name'
349 * in the "other_config" column in the "Queue" table in
350 * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
352 * The implementation should not need to perform any Netlink calls, because
353 * the 'tc_install' or 'tc_load' that instantiated 'netdev->tc' should have
354 * cached the queue configuration.
356 * This function may be null if 'tc' does not have queues ('n_queues' is
358 int (*class_get
)(const struct netdev
*netdev
, const struct tc_queue
*queue
,
359 struct smap
*details
);
361 /* Configures or reconfigures 'queue_id' on 'netdev->tc' according to
362 * 'details', perfoming any required Netlink calls to complete the
363 * reconfiguration. The caller ensures that 'queue_id' is less than
366 * The contents of 'details' should be documented as valid for 'ovs_name'
367 * in the "other_config" column in the "Queue" table in
368 * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
370 * This function may be null if 'tc' does not have queues or its queues are
371 * not configurable. */
372 int (*class_set
)(struct netdev
*, unsigned int queue_id
,
373 const struct smap
*details
);
375 /* Deletes 'queue' from 'netdev->tc'. 'queue' is one of the 'struct
376 * tc_queue's within 'netdev->tc->queues'.
378 * This function may be null if 'tc' does not have queues or its queues
379 * cannot be deleted. */
380 int (*class_delete
)(struct netdev
*, struct tc_queue
*queue
);
382 /* Obtains stats for 'queue' from 'netdev->tc'. 'queue' is one of the
383 * 'struct tc_queue's within 'netdev->tc->queues'.
385 * On success, initializes '*stats'.
387 * This function may be null if 'tc' does not have queues or if it cannot
388 * report queue statistics. */
389 int (*class_get_stats
)(const struct netdev
*netdev
,
390 const struct tc_queue
*queue
,
391 struct netdev_queue_stats
*stats
);
393 /* Extracts queue stats from 'nlmsg', which is a response to a
394 * RTM_GETTCLASS message, and passes them to 'cb' along with 'aux'.
396 * This function may be null if 'tc' does not have queues or if it cannot
397 * report queue statistics. */
398 int (*class_dump_stats
)(const struct netdev
*netdev
,
399 const struct ofpbuf
*nlmsg
,
400 netdev_dump_queue_stats_cb
*cb
, void *aux
);
404 tc_init(struct tc
*tc
, const struct tc_ops
*ops
)
407 hmap_init(&tc
->queues
);
411 tc_destroy(struct tc
*tc
)
413 hmap_destroy(&tc
->queues
);
416 static const struct tc_ops tc_ops_htb
;
417 static const struct tc_ops tc_ops_hfsc
;
418 static const struct tc_ops tc_ops_codel
;
419 static const struct tc_ops tc_ops_fqcodel
;
420 static const struct tc_ops tc_ops_sfq
;
421 static const struct tc_ops tc_ops_default
;
422 static const struct tc_ops tc_ops_noop
;
423 static const struct tc_ops tc_ops_other
;
425 static const struct tc_ops
*const tcs
[] = {
426 &tc_ops_htb
, /* Hierarchy token bucket (see tc-htb(8)). */
427 &tc_ops_hfsc
, /* Hierarchical fair service curve. */
428 &tc_ops_codel
, /* Controlled delay */
429 &tc_ops_fqcodel
, /* Fair queue controlled delay */
430 &tc_ops_sfq
, /* Stochastic fair queueing */
431 &tc_ops_noop
, /* Non operating qos type. */
432 &tc_ops_default
, /* Default qdisc (see tc-pfifo_fast(8)). */
433 &tc_ops_other
, /* Some other qdisc. */
437 static unsigned int tc_make_handle(unsigned int major
, unsigned int minor
);
438 static unsigned int tc_get_major(unsigned int handle
);
439 static unsigned int tc_get_minor(unsigned int handle
);
441 static unsigned int tc_ticks_to_bytes(unsigned int rate
, unsigned int ticks
);
442 static unsigned int tc_bytes_to_ticks(unsigned int rate
, unsigned int size
);
443 static unsigned int tc_buffer_per_jiffy(unsigned int rate
);
445 static struct tcmsg
*tc_make_request(const struct netdev
*, int type
,
446 unsigned int flags
, struct ofpbuf
*);
447 static int tc_transact(struct ofpbuf
*request
, struct ofpbuf
**replyp
);
448 static int tc_add_del_ingress_qdisc(struct netdev
*netdev
, bool add
);
449 static int tc_add_policer(struct netdev
*,
450 uint32_t kbits_rate
, uint32_t kbits_burst
);
452 static int tc_parse_qdisc(const struct ofpbuf
*, const char **kind
,
453 struct nlattr
**options
);
454 static int tc_parse_class(const struct ofpbuf
*, unsigned int *queue_id
,
455 struct nlattr
**options
,
456 struct netdev_queue_stats
*);
457 static int tc_query_class(const struct netdev
*,
458 unsigned int handle
, unsigned int parent
,
459 struct ofpbuf
**replyp
);
460 static int tc_delete_class(const struct netdev
*, unsigned int handle
);
462 static int tc_del_qdisc(struct netdev
*netdev
);
463 static int tc_query_qdisc(const struct netdev
*netdev
);
465 static int tc_calc_cell_log(unsigned int mtu
);
466 static void tc_fill_rate(struct tc_ratespec
*rate
, uint64_t bps
, int mtu
);
467 static void tc_put_rtab(struct ofpbuf
*, uint16_t type
,
468 const struct tc_ratespec
*rate
);
469 static int tc_calc_buffer(unsigned int Bps
, int mtu
, uint64_t burst_bytes
);
471 struct netdev_linux
{
474 /* Protects all members below. */
475 struct ovs_mutex mutex
;
477 unsigned int cache_valid
;
479 bool miimon
; /* Link status of last poll. */
480 long long int miimon_interval
; /* Miimon Poll rate. Disabled if <= 0. */
481 struct timer miimon_timer
;
483 /* The following are figured out "on demand" only. They are only valid
484 * when the corresponding VALID_* bit in 'cache_valid' is set. */
486 struct eth_addr etheraddr
;
488 unsigned int ifi_flags
;
489 long long int carrier_resets
;
490 uint32_t kbits_rate
; /* Policing data. */
491 uint32_t kbits_burst
;
492 int vport_stats_error
; /* Cached error code from vport_get_stats().
493 0 or an errno value. */
494 int netdev_mtu_error
; /* Cached error code from SIOCGIFMTU or SIOCSIFMTU. */
495 int ether_addr_error
; /* Cached error code from set/get etheraddr. */
496 int netdev_policing_error
; /* Cached error code from set policing. */
497 int get_features_error
; /* Cached error code from ETHTOOL_GSET. */
498 int get_ifindex_error
; /* Cached error code from SIOCGIFINDEX. */
500 enum netdev_features current
; /* Cached from ETHTOOL_GSET. */
501 enum netdev_features advertised
; /* Cached from ETHTOOL_GSET. */
502 enum netdev_features supported
; /* Cached from ETHTOOL_GSET. */
504 struct ethtool_drvinfo drvinfo
; /* Cached from ETHTOOL_GDRVINFO. */
507 /* For devices of class netdev_tap_class only. */
511 struct netdev_rxq_linux
{
512 struct netdev_rxq up
;
517 /* This is set pretty low because we probably won't learn anything from the
518 * additional log messages. */
519 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 20);
521 /* Polling miimon status for all ports causes performance degradation when
522 * handling a large number of ports. If there are no devices using miimon, then
523 * we skip netdev_linux_miimon_run() and netdev_linux_miimon_wait().
525 * Readers do not depend on this variable synchronizing with the related
526 * changes in the device miimon status, so we can use atomic_count. */
527 static atomic_count miimon_cnt
= ATOMIC_COUNT_INIT(0);
529 static void netdev_linux_run(const struct netdev_class
*);
531 static int netdev_linux_do_ethtool(const char *name
, struct ethtool_cmd
*,
532 int cmd
, const char *cmd_name
);
533 static int get_flags(const struct netdev
*, unsigned int *flags
);
534 static int set_flags(const char *, unsigned int flags
);
535 static int update_flags(struct netdev_linux
*netdev
, enum netdev_flags off
,
536 enum netdev_flags on
, enum netdev_flags
*old_flagsp
)
537 OVS_REQUIRES(netdev
->mutex
);
538 static int do_get_ifindex(const char *netdev_name
);
539 static int get_ifindex(const struct netdev
*, int *ifindexp
);
540 static int do_set_addr(struct netdev
*netdev
,
541 int ioctl_nr
, const char *ioctl_name
,
542 struct in_addr addr
);
543 static int get_etheraddr(const char *netdev_name
, struct eth_addr
*ea
);
544 static int set_etheraddr(const char *netdev_name
, const struct eth_addr
);
545 static int get_stats_via_netlink(const struct netdev
*, struct netdev_stats
*);
546 static int af_packet_sock(void);
547 static bool netdev_linux_miimon_enabled(void);
548 static void netdev_linux_miimon_run(void);
549 static void netdev_linux_miimon_wait(void);
550 static int netdev_linux_get_mtu__(struct netdev_linux
*netdev
, int *mtup
);
553 is_netdev_linux_class(const struct netdev_class
*netdev_class
)
555 return netdev_class
->run
== netdev_linux_run
;
559 is_tap_netdev(const struct netdev
*netdev
)
561 return netdev_get_class(netdev
) == &netdev_tap_class
;
564 static struct netdev_linux
*
565 netdev_linux_cast(const struct netdev
*netdev
)
567 ovs_assert(is_netdev_linux_class(netdev_get_class(netdev
)));
569 return CONTAINER_OF(netdev
, struct netdev_linux
, up
);
572 static struct netdev_rxq_linux
*
573 netdev_rxq_linux_cast(const struct netdev_rxq
*rx
)
575 ovs_assert(is_netdev_linux_class(netdev_get_class(rx
->netdev
)));
576 return CONTAINER_OF(rx
, struct netdev_rxq_linux
, up
);
579 static void netdev_linux_update(struct netdev_linux
*netdev
,
580 const struct rtnetlink_change
*)
581 OVS_REQUIRES(netdev
->mutex
);
582 static void netdev_linux_changed(struct netdev_linux
*netdev
,
583 unsigned int ifi_flags
, unsigned int mask
)
584 OVS_REQUIRES(netdev
->mutex
);
586 /* Returns a NETLINK_ROUTE socket listening for RTNLGRP_LINK,
587 * RTNLGRP_IPV4_IFADDR and RTNLGRP_IPV6_IFADDR changes, or NULL
588 * if no such socket could be created. */
589 static struct nl_sock
*
590 netdev_linux_notify_sock(void)
592 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
593 static struct nl_sock
*sock
;
594 unsigned int mcgroups
[] = {RTNLGRP_LINK
, RTNLGRP_IPV4_IFADDR
,
595 RTNLGRP_IPV6_IFADDR
, RTNLGRP_IPV6_IFINFO
};
597 if (ovsthread_once_start(&once
)) {
600 error
= nl_sock_create(NETLINK_ROUTE
, &sock
);
604 for (i
= 0; i
< ARRAY_SIZE(mcgroups
); i
++) {
605 error
= nl_sock_join_mcgroup(sock
, mcgroups
[i
]);
607 nl_sock_destroy(sock
);
613 ovsthread_once_done(&once
);
620 netdev_linux_miimon_enabled(void)
622 return atomic_count_get(&miimon_cnt
) > 0;
626 netdev_linux_run(const struct netdev_class
*netdev_class OVS_UNUSED
)
628 struct nl_sock
*sock
;
631 if (netdev_linux_miimon_enabled()) {
632 netdev_linux_miimon_run();
635 sock
= netdev_linux_notify_sock();
641 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
642 uint64_t buf_stub
[4096 / 8];
645 ofpbuf_use_stub(&buf
, buf_stub
, sizeof buf_stub
);
646 error
= nl_sock_recv(sock
, &buf
, false);
648 struct rtnetlink_change change
;
650 if (rtnetlink_parse(&buf
, &change
)) {
651 struct netdev
*netdev_
= NULL
;
652 char dev_name
[IFNAMSIZ
];
654 if (!change
.ifname
) {
655 change
.ifname
= if_indextoname(change
.if_index
, dev_name
);
659 netdev_
= netdev_from_name(change
.ifname
);
661 if (netdev_
&& is_netdev_linux_class(netdev_
->netdev_class
)) {
662 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
664 ovs_mutex_lock(&netdev
->mutex
);
665 netdev_linux_update(netdev
, &change
);
666 ovs_mutex_unlock(&netdev
->mutex
);
668 netdev_close(netdev_
);
670 } else if (error
== ENOBUFS
) {
671 struct shash device_shash
;
672 struct shash_node
*node
;
676 shash_init(&device_shash
);
677 netdev_get_devices(&netdev_linux_class
, &device_shash
);
678 SHASH_FOR_EACH (node
, &device_shash
) {
679 struct netdev
*netdev_
= node
->data
;
680 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
683 ovs_mutex_lock(&netdev
->mutex
);
684 get_flags(netdev_
, &flags
);
685 netdev_linux_changed(netdev
, flags
, 0);
686 ovs_mutex_unlock(&netdev
->mutex
);
688 netdev_close(netdev_
);
690 shash_destroy(&device_shash
);
691 } else if (error
!= EAGAIN
) {
692 VLOG_WARN_RL(&rl
, "error reading or parsing netlink (%s)",
693 ovs_strerror(error
));
700 netdev_linux_wait(const struct netdev_class
*netdev_class OVS_UNUSED
)
702 struct nl_sock
*sock
;
704 if (netdev_linux_miimon_enabled()) {
705 netdev_linux_miimon_wait();
707 sock
= netdev_linux_notify_sock();
709 nl_sock_wait(sock
, POLLIN
);
714 netdev_linux_changed(struct netdev_linux
*dev
,
715 unsigned int ifi_flags
, unsigned int mask
)
716 OVS_REQUIRES(dev
->mutex
)
718 netdev_change_seq_changed(&dev
->up
);
720 if ((dev
->ifi_flags
^ ifi_flags
) & IFF_RUNNING
) {
721 dev
->carrier_resets
++;
723 dev
->ifi_flags
= ifi_flags
;
725 dev
->cache_valid
&= mask
;
726 if (!(mask
& VALID_IN
)) {
727 netdev_get_addrs_list_flush();
732 netdev_linux_update(struct netdev_linux
*dev
,
733 const struct rtnetlink_change
*change
)
734 OVS_REQUIRES(dev
->mutex
)
736 if (rtnetlink_type_is_rtnlgrp_link(change
->nlmsg_type
)){
737 if (change
->nlmsg_type
== RTM_NEWLINK
) {
738 /* Keep drv-info, and ip addresses. */
739 netdev_linux_changed(dev
, change
->ifi_flags
,
740 VALID_DRVINFO
| VALID_IN
);
742 /* Update netdev from rtnl-change msg. */
744 dev
->mtu
= change
->mtu
;
745 dev
->cache_valid
|= VALID_MTU
;
746 dev
->netdev_mtu_error
= 0;
749 if (!eth_addr_is_zero(change
->mac
)) {
750 dev
->etheraddr
= change
->mac
;
751 dev
->cache_valid
|= VALID_ETHERADDR
;
752 dev
->ether_addr_error
= 0;
755 dev
->ifindex
= change
->if_index
;
756 dev
->cache_valid
|= VALID_IFINDEX
;
757 dev
->get_ifindex_error
= 0;
759 netdev_linux_changed(dev
, change
->ifi_flags
, 0);
761 } else if (rtnetlink_type_is_rtnlgrp_addr(change
->nlmsg_type
)) {
762 /* Invalidates in4, in6. */
763 netdev_linux_changed(dev
, dev
->ifi_flags
, ~VALID_IN
);
769 static struct netdev
*
770 netdev_linux_alloc(void)
772 struct netdev_linux
*netdev
= xzalloc(sizeof *netdev
);
777 netdev_linux_common_construct(struct netdev_linux
*netdev
)
779 ovs_mutex_init(&netdev
->mutex
);
782 /* Creates system and internal devices. */
784 netdev_linux_construct(struct netdev
*netdev_
)
786 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
789 netdev_linux_common_construct(netdev
);
791 error
= get_flags(&netdev
->up
, &netdev
->ifi_flags
);
792 if (error
== ENODEV
) {
793 if (netdev
->up
.netdev_class
!= &netdev_internal_class
) {
794 /* The device does not exist, so don't allow it to be opened. */
797 /* "Internal" netdevs have to be created as netdev objects before
798 * they exist in the kernel, because creating them in the kernel
799 * happens by passing a netdev object to dpif_port_add().
800 * Therefore, ignore the error. */
807 /* For most types of netdevs we open the device for each call of
808 * netdev_open(). However, this is not the case with tap devices,
809 * since it is only possible to open the device once. In this
810 * situation we share a single file descriptor, and consequently
811 * buffers, across all readers. Therefore once data is read it will
812 * be unavailable to other reads for tap devices. */
814 netdev_linux_construct_tap(struct netdev
*netdev_
)
816 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
817 static const char tap_dev
[] = "/dev/net/tun";
818 const char *name
= netdev_
->name
;
822 netdev_linux_common_construct(netdev
);
824 /* Open tap device. */
825 netdev
->tap_fd
= open(tap_dev
, O_RDWR
);
826 if (netdev
->tap_fd
< 0) {
828 VLOG_WARN("opening \"%s\" failed: %s", tap_dev
, ovs_strerror(error
));
832 /* Create tap device. */
833 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
834 ovs_strzcpy(ifr
.ifr_name
, name
, sizeof ifr
.ifr_name
);
835 if (ioctl(netdev
->tap_fd
, TUNSETIFF
, &ifr
) == -1) {
836 VLOG_WARN("%s: creating tap device failed: %s", name
,
837 ovs_strerror(errno
));
842 /* Make non-blocking. */
843 error
= set_nonblocking(netdev
->tap_fd
);
851 close(netdev
->tap_fd
);
856 netdev_linux_destruct(struct netdev
*netdev_
)
858 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
860 if (netdev
->tc
&& netdev
->tc
->ops
->tc_destroy
) {
861 netdev
->tc
->ops
->tc_destroy(netdev
->tc
);
864 if (netdev_get_class(netdev_
) == &netdev_tap_class
865 && netdev
->tap_fd
>= 0)
867 close(netdev
->tap_fd
);
870 if (netdev
->miimon_interval
> 0) {
871 atomic_count_dec(&miimon_cnt
);
874 ovs_mutex_destroy(&netdev
->mutex
);
878 netdev_linux_dealloc(struct netdev
*netdev_
)
880 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
884 static struct netdev_rxq
*
885 netdev_linux_rxq_alloc(void)
887 struct netdev_rxq_linux
*rx
= xzalloc(sizeof *rx
);
892 netdev_linux_rxq_construct(struct netdev_rxq
*rxq_
)
894 struct netdev_rxq_linux
*rx
= netdev_rxq_linux_cast(rxq_
);
895 struct netdev
*netdev_
= rx
->up
.netdev
;
896 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
899 ovs_mutex_lock(&netdev
->mutex
);
900 rx
->is_tap
= is_tap_netdev(netdev_
);
902 rx
->fd
= netdev
->tap_fd
;
904 struct sockaddr_ll sll
;
906 /* Result of tcpdump -dd inbound */
907 static const struct sock_filter filt
[] = {
908 { 0x28, 0, 0, 0xfffff004 }, /* ldh [0] */
909 { 0x15, 0, 1, 0x00000004 }, /* jeq #4 jt 2 jf 3 */
910 { 0x6, 0, 0, 0x00000000 }, /* ret #0 */
911 { 0x6, 0, 0, 0x0000ffff } /* ret #65535 */
913 static const struct sock_fprog fprog
= {
914 ARRAY_SIZE(filt
), (struct sock_filter
*) filt
917 /* Create file descriptor. */
918 rx
->fd
= socket(PF_PACKET
, SOCK_RAW
, 0);
921 VLOG_ERR("failed to create raw socket (%s)", ovs_strerror(error
));
926 if (setsockopt(rx
->fd
, SOL_PACKET
, PACKET_AUXDATA
, &val
, sizeof val
)) {
928 VLOG_ERR("%s: failed to mark socket for auxdata (%s)",
929 netdev_get_name(netdev_
), ovs_strerror(error
));
933 /* Set non-blocking mode. */
934 error
= set_nonblocking(rx
->fd
);
939 /* Get ethernet device index. */
940 error
= get_ifindex(&netdev
->up
, &ifindex
);
945 /* Bind to specific ethernet device. */
946 memset(&sll
, 0, sizeof sll
);
947 sll
.sll_family
= AF_PACKET
;
948 sll
.sll_ifindex
= ifindex
;
949 sll
.sll_protocol
= htons(ETH_P_ALL
);
950 if (bind(rx
->fd
, (struct sockaddr
*) &sll
, sizeof sll
) < 0) {
952 VLOG_ERR("%s: failed to bind raw socket (%s)",
953 netdev_get_name(netdev_
), ovs_strerror(error
));
957 /* Filter for only inbound packets. */
958 error
= setsockopt(rx
->fd
, SOL_SOCKET
, SO_ATTACH_FILTER
, &fprog
,
962 VLOG_ERR("%s: failed to attach filter (%s)",
963 netdev_get_name(netdev_
), ovs_strerror(error
));
967 ovs_mutex_unlock(&netdev
->mutex
);
975 ovs_mutex_unlock(&netdev
->mutex
);
980 netdev_linux_rxq_destruct(struct netdev_rxq
*rxq_
)
982 struct netdev_rxq_linux
*rx
= netdev_rxq_linux_cast(rxq_
);
990 netdev_linux_rxq_dealloc(struct netdev_rxq
*rxq_
)
992 struct netdev_rxq_linux
*rx
= netdev_rxq_linux_cast(rxq_
);
998 auxdata_to_vlan_tpid(const struct tpacket_auxdata
*aux
, bool double_tagged
)
1000 if (aux
->tp_status
& TP_STATUS_VLAN_TPID_VALID
) {
1001 return htons(aux
->tp_vlan_tpid
);
1002 } else if (double_tagged
) {
1003 return htons(ETH_TYPE_VLAN_8021AD
);
1005 return htons(ETH_TYPE_VLAN_8021Q
);
1010 auxdata_has_vlan_tci(const struct tpacket_auxdata
*aux
)
1012 return aux
->tp_vlan_tci
|| aux
->tp_status
& TP_STATUS_VLAN_VALID
;
1016 netdev_linux_rxq_recv_sock(int fd
, struct dp_packet
*buffer
)
1021 struct cmsghdr
*cmsg
;
1023 struct cmsghdr cmsg
;
1024 char buffer
[CMSG_SPACE(sizeof(struct tpacket_auxdata
))];
1028 /* Reserve headroom for a single VLAN tag */
1029 dp_packet_reserve(buffer
, VLAN_HEADER_LEN
);
1030 size
= dp_packet_tailroom(buffer
);
1032 iov
.iov_base
= dp_packet_data(buffer
);
1034 msgh
.msg_name
= NULL
;
1035 msgh
.msg_namelen
= 0;
1036 msgh
.msg_iov
= &iov
;
1037 msgh
.msg_iovlen
= 1;
1038 msgh
.msg_control
= &cmsg_buffer
;
1039 msgh
.msg_controllen
= sizeof cmsg_buffer
;
1043 retval
= recvmsg(fd
, &msgh
, MSG_TRUNC
);
1044 } while (retval
< 0 && errno
== EINTR
);
1048 } else if (retval
> size
) {
1052 dp_packet_set_size(buffer
, dp_packet_size(buffer
) + retval
);
1054 for (cmsg
= CMSG_FIRSTHDR(&msgh
); cmsg
; cmsg
= CMSG_NXTHDR(&msgh
, cmsg
)) {
1055 const struct tpacket_auxdata
*aux
;
1057 if (cmsg
->cmsg_level
!= SOL_PACKET
1058 || cmsg
->cmsg_type
!= PACKET_AUXDATA
1059 || cmsg
->cmsg_len
< CMSG_LEN(sizeof(struct tpacket_auxdata
))) {
1063 aux
= ALIGNED_CAST(struct tpacket_auxdata
*, CMSG_DATA(cmsg
));
1064 if (auxdata_has_vlan_tci(aux
)) {
1065 struct eth_header
*eth
;
1068 if (retval
< ETH_HEADER_LEN
) {
1072 eth
= dp_packet_data(buffer
);
1073 double_tagged
= eth
->eth_type
== htons(ETH_TYPE_VLAN_8021Q
);
1075 eth_push_vlan(buffer
, auxdata_to_vlan_tpid(aux
, double_tagged
),
1076 htons(aux
->tp_vlan_tci
));
1085 netdev_linux_rxq_recv_tap(int fd
, struct dp_packet
*buffer
)
1088 size_t size
= dp_packet_tailroom(buffer
);
1091 retval
= read(fd
, dp_packet_data(buffer
), size
);
1092 } while (retval
< 0 && errno
== EINTR
);
1098 dp_packet_set_size(buffer
, dp_packet_size(buffer
) + retval
);
1103 netdev_linux_rxq_recv(struct netdev_rxq
*rxq_
, struct dp_packet_batch
*batch
)
1105 struct netdev_rxq_linux
*rx
= netdev_rxq_linux_cast(rxq_
);
1106 struct netdev
*netdev
= rx
->up
.netdev
;
1107 struct dp_packet
*buffer
;
1111 if (netdev_linux_get_mtu__(netdev_linux_cast(netdev
), &mtu
)) {
1112 mtu
= ETH_PAYLOAD_MAX
;
1115 buffer
= dp_packet_new_with_headroom(VLAN_ETH_HEADER_LEN
+ mtu
,
1116 DP_NETDEV_HEADROOM
);
1117 retval
= (rx
->is_tap
1118 ? netdev_linux_rxq_recv_tap(rx
->fd
, buffer
)
1119 : netdev_linux_rxq_recv_sock(rx
->fd
, buffer
));
1122 if (retval
!= EAGAIN
&& retval
!= EMSGSIZE
) {
1123 VLOG_WARN_RL(&rl
, "error receiving Ethernet packet on %s: %s",
1124 netdev_rxq_get_name(rxq_
), ovs_strerror(errno
));
1126 dp_packet_delete(buffer
);
1128 batch
->packets
[0] = buffer
;
1136 netdev_linux_rxq_wait(struct netdev_rxq
*rxq_
)
1138 struct netdev_rxq_linux
*rx
= netdev_rxq_linux_cast(rxq_
);
1139 poll_fd_wait(rx
->fd
, POLLIN
);
1143 netdev_linux_rxq_drain(struct netdev_rxq
*rxq_
)
1145 struct netdev_rxq_linux
*rx
= netdev_rxq_linux_cast(rxq_
);
1148 int error
= af_inet_ifreq_ioctl(netdev_rxq_get_name(rxq_
), &ifr
,
1149 SIOCGIFTXQLEN
, "SIOCGIFTXQLEN");
1153 drain_fd(rx
->fd
, ifr
.ifr_qlen
);
1156 return drain_rcvbuf(rx
->fd
);
1160 /* Sends 'buffer' on 'netdev'. Returns 0 if successful, otherwise a positive
1161 * errno value. Returns EAGAIN without blocking if the packet cannot be queued
1162 * immediately. Returns EMSGSIZE if a partial packet was transmitted or if
1163 * the packet is too big or too small to transmit on the device.
1165 * The caller retains ownership of 'buffer' in all cases.
1167 * The kernel maintains a packet transmission queue, so the caller is not
1168 * expected to do additional queuing of packets. */
1170 netdev_linux_send(struct netdev
*netdev_
, int qid OVS_UNUSED
,
1171 struct dp_packet_batch
*batch
, bool may_steal
,
1172 bool concurrent_txq OVS_UNUSED
)
1177 /* 'i' is incremented only if there's no error */
1178 for (i
= 0; i
< batch
->count
;) {
1179 const void *data
= dp_packet_data(batch
->packets
[i
]);
1180 size_t size
= dp_packet_size(batch
->packets
[i
]);
1183 /* Truncate the packet if it is configured. */
1184 size
-= dp_packet_get_cutlen(batch
->packets
[i
]);
1186 if (!is_tap_netdev(netdev_
)) {
1187 /* Use our AF_PACKET socket to send to this device. */
1188 struct sockaddr_ll sll
;
1194 sock
= af_packet_sock();
1199 ifindex
= netdev_get_ifindex(netdev_
);
1204 /* We don't bother setting most fields in sockaddr_ll because the
1205 * kernel ignores them for SOCK_RAW. */
1206 memset(&sll
, 0, sizeof sll
);
1207 sll
.sll_family
= AF_PACKET
;
1208 sll
.sll_ifindex
= ifindex
;
1210 iov
.iov_base
= CONST_CAST(void *, data
);
1213 msg
.msg_name
= &sll
;
1214 msg
.msg_namelen
= sizeof sll
;
1217 msg
.msg_control
= NULL
;
1218 msg
.msg_controllen
= 0;
1221 retval
= sendmsg(sock
, &msg
, 0);
1223 /* Use the tap fd to send to this device. This is essential for
1224 * tap devices, because packets sent to a tap device with an
1225 * AF_PACKET socket will loop back to be *received* again on the
1226 * tap device. This doesn't occur on other interface types
1227 * because we attach a socket filter to the rx socket. */
1228 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
1230 retval
= write(netdev
->tap_fd
, data
, size
);
1234 if (errno
== EINTR
) {
1235 /* The send was interrupted by a signal. Retry the packet by
1236 * continuing without incrementing 'i'.*/
1238 } else if (errno
== EIO
&& is_tap_netdev(netdev_
)) {
1239 /* The Linux tap driver returns EIO if the device is not up.
1240 * From the OVS side this is not an error, so ignore it. */
1242 /* The Linux AF_PACKET implementation never blocks waiting for
1243 * room for packets, instead returning ENOBUFS. Translate this
1244 * into EAGAIN for the caller. */
1245 error
= errno
== ENOBUFS
? EAGAIN
: errno
;
1248 } else if (retval
!= size
) {
1249 VLOG_WARN_RL(&rl
, "sent partial Ethernet packet (%"PRIuSIZE
" bytes"
1250 " of %"PRIuSIZE
") on %s", retval
, size
,
1251 netdev_get_name(netdev_
));
1256 /* Process the next packet in the batch */
1260 dp_packet_delete_batch(batch
, may_steal
);
1262 if (error
&& error
!= EAGAIN
) {
1263 VLOG_WARN_RL(&rl
, "error sending Ethernet packet on %s: %s",
1264 netdev_get_name(netdev_
), ovs_strerror(error
));
1271 /* Registers with the poll loop to wake up from the next call to poll_block()
1272 * when the packet transmission queue has sufficient room to transmit a packet
1273 * with netdev_send().
1275 * The kernel maintains a packet transmission queue, so the client is not
1276 * expected to do additional queuing of packets. Thus, this function is
1277 * unlikely to ever be used. It is included for completeness. */
1279 netdev_linux_send_wait(struct netdev
*netdev
, int qid OVS_UNUSED
)
1281 if (is_tap_netdev(netdev
)) {
1282 /* TAP device always accepts packets.*/
1283 poll_immediate_wake();
1287 /* Attempts to set 'netdev''s MAC address to 'mac'. Returns 0 if successful,
1288 * otherwise a positive errno value. */
1290 netdev_linux_set_etheraddr(struct netdev
*netdev_
, const struct eth_addr mac
)
1292 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
1293 enum netdev_flags old_flags
= 0;
1296 ovs_mutex_lock(&netdev
->mutex
);
1298 if (netdev
->cache_valid
& VALID_ETHERADDR
) {
1299 error
= netdev
->ether_addr_error
;
1300 if (error
|| eth_addr_equals(netdev
->etheraddr
, mac
)) {
1303 netdev
->cache_valid
&= ~VALID_ETHERADDR
;
1306 /* Tap devices must be brought down before setting the address. */
1307 if (is_tap_netdev(netdev_
)) {
1308 update_flags(netdev
, NETDEV_UP
, 0, &old_flags
);
1310 error
= set_etheraddr(netdev_get_name(netdev_
), mac
);
1311 if (!error
|| error
== ENODEV
) {
1312 netdev
->ether_addr_error
= error
;
1313 netdev
->cache_valid
|= VALID_ETHERADDR
;
1315 netdev
->etheraddr
= mac
;
1319 if (is_tap_netdev(netdev_
) && old_flags
& NETDEV_UP
) {
1320 update_flags(netdev
, 0, NETDEV_UP
, &old_flags
);
1324 ovs_mutex_unlock(&netdev
->mutex
);
1328 /* Copies 'netdev''s MAC address to 'mac' which is passed as param. */
1330 netdev_linux_get_etheraddr(const struct netdev
*netdev_
, struct eth_addr
*mac
)
1332 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
1335 ovs_mutex_lock(&netdev
->mutex
);
1336 if (!(netdev
->cache_valid
& VALID_ETHERADDR
)) {
1337 netdev
->ether_addr_error
= get_etheraddr(netdev_get_name(netdev_
),
1338 &netdev
->etheraddr
);
1339 netdev
->cache_valid
|= VALID_ETHERADDR
;
1342 error
= netdev
->ether_addr_error
;
1344 *mac
= netdev
->etheraddr
;
1346 ovs_mutex_unlock(&netdev
->mutex
);
1352 netdev_linux_get_mtu__(struct netdev_linux
*netdev
, int *mtup
)
1356 if (!(netdev
->cache_valid
& VALID_MTU
)) {
1359 netdev
->netdev_mtu_error
= af_inet_ifreq_ioctl(
1360 netdev_get_name(&netdev
->up
), &ifr
, SIOCGIFMTU
, "SIOCGIFMTU");
1361 netdev
->mtu
= ifr
.ifr_mtu
;
1362 netdev
->cache_valid
|= VALID_MTU
;
1365 error
= netdev
->netdev_mtu_error
;
1367 *mtup
= netdev
->mtu
;
1373 /* Returns the maximum size of transmitted (and received) packets on 'netdev',
1374 * in bytes, not including the hardware header; thus, this is typically 1500
1375 * bytes for Ethernet devices. */
1377 netdev_linux_get_mtu(const struct netdev
*netdev_
, int *mtup
)
1379 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
1382 ovs_mutex_lock(&netdev
->mutex
);
1383 error
= netdev_linux_get_mtu__(netdev
, mtup
);
1384 ovs_mutex_unlock(&netdev
->mutex
);
1389 /* Sets the maximum size of transmitted (MTU) for given device using linux
1390 * networking ioctl interface.
1393 netdev_linux_set_mtu(struct netdev
*netdev_
, int mtu
)
1395 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
1399 ovs_mutex_lock(&netdev
->mutex
);
1400 if (netdev
->cache_valid
& VALID_MTU
) {
1401 error
= netdev
->netdev_mtu_error
;
1402 if (error
|| netdev
->mtu
== mtu
) {
1405 netdev
->cache_valid
&= ~VALID_MTU
;
1408 error
= af_inet_ifreq_ioctl(netdev_get_name(netdev_
), &ifr
,
1409 SIOCSIFMTU
, "SIOCSIFMTU");
1410 if (!error
|| error
== ENODEV
) {
1411 netdev
->netdev_mtu_error
= error
;
1412 netdev
->mtu
= ifr
.ifr_mtu
;
1413 netdev
->cache_valid
|= VALID_MTU
;
1416 ovs_mutex_unlock(&netdev
->mutex
);
1420 /* Returns the ifindex of 'netdev', if successful, as a positive number.
1421 * On failure, returns a negative errno value. */
1423 netdev_linux_get_ifindex(const struct netdev
*netdev_
)
1425 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
1428 ovs_mutex_lock(&netdev
->mutex
);
1429 error
= get_ifindex(netdev_
, &ifindex
);
1430 ovs_mutex_unlock(&netdev
->mutex
);
1432 return error
? -error
: ifindex
;
1436 netdev_linux_get_carrier(const struct netdev
*netdev_
, bool *carrier
)
1438 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
1440 ovs_mutex_lock(&netdev
->mutex
);
1441 if (netdev
->miimon_interval
> 0) {
1442 *carrier
= netdev
->miimon
;
1444 *carrier
= (netdev
->ifi_flags
& IFF_RUNNING
) != 0;
1446 ovs_mutex_unlock(&netdev
->mutex
);
1451 static long long int
1452 netdev_linux_get_carrier_resets(const struct netdev
*netdev_
)
1454 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
1455 long long int carrier_resets
;
1457 ovs_mutex_lock(&netdev
->mutex
);
1458 carrier_resets
= netdev
->carrier_resets
;
1459 ovs_mutex_unlock(&netdev
->mutex
);
1461 return carrier_resets
;
1465 netdev_linux_do_miimon(const char *name
, int cmd
, const char *cmd_name
,
1466 struct mii_ioctl_data
*data
)
1471 memset(&ifr
, 0, sizeof ifr
);
1472 memcpy(&ifr
.ifr_data
, data
, sizeof *data
);
1473 error
= af_inet_ifreq_ioctl(name
, &ifr
, cmd
, cmd_name
);
1474 memcpy(data
, &ifr
.ifr_data
, sizeof *data
);
1480 netdev_linux_get_miimon(const char *name
, bool *miimon
)
1482 struct mii_ioctl_data data
;
1487 memset(&data
, 0, sizeof data
);
1488 error
= netdev_linux_do_miimon(name
, SIOCGMIIPHY
, "SIOCGMIIPHY", &data
);
1490 /* data.phy_id is filled out by previous SIOCGMIIPHY miimon call. */
1491 data
.reg_num
= MII_BMSR
;
1492 error
= netdev_linux_do_miimon(name
, SIOCGMIIREG
, "SIOCGMIIREG",
1496 *miimon
= !!(data
.val_out
& BMSR_LSTATUS
);
1500 struct ethtool_cmd ecmd
;
1502 VLOG_DBG_RL(&rl
, "%s: failed to query MII, falling back to ethtool",
1505 COVERAGE_INC(netdev_get_ethtool
);
1506 memset(&ecmd
, 0, sizeof ecmd
);
1507 error
= netdev_linux_do_ethtool(name
, &ecmd
, ETHTOOL_GLINK
,
1510 struct ethtool_value eval
;
1512 memcpy(&eval
, &ecmd
, sizeof eval
);
1513 *miimon
= !!eval
.data
;
1515 VLOG_WARN_RL(&rl
, "%s: ethtool link status failed", name
);
1523 netdev_linux_set_miimon_interval(struct netdev
*netdev_
,
1524 long long int interval
)
1526 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
1528 ovs_mutex_lock(&netdev
->mutex
);
1529 interval
= interval
> 0 ? MAX(interval
, 100) : 0;
1530 if (netdev
->miimon_interval
!= interval
) {
1531 if (interval
&& !netdev
->miimon_interval
) {
1532 atomic_count_inc(&miimon_cnt
);
1533 } else if (!interval
&& netdev
->miimon_interval
) {
1534 atomic_count_dec(&miimon_cnt
);
1537 netdev
->miimon_interval
= interval
;
1538 timer_set_expired(&netdev
->miimon_timer
);
1540 ovs_mutex_unlock(&netdev
->mutex
);
1546 netdev_linux_miimon_run(void)
1548 struct shash device_shash
;
1549 struct shash_node
*node
;
1551 shash_init(&device_shash
);
1552 netdev_get_devices(&netdev_linux_class
, &device_shash
);
1553 SHASH_FOR_EACH (node
, &device_shash
) {
1554 struct netdev
*netdev
= node
->data
;
1555 struct netdev_linux
*dev
= netdev_linux_cast(netdev
);
1558 ovs_mutex_lock(&dev
->mutex
);
1559 if (dev
->miimon_interval
> 0 && timer_expired(&dev
->miimon_timer
)) {
1560 netdev_linux_get_miimon(dev
->up
.name
, &miimon
);
1561 if (miimon
!= dev
->miimon
) {
1562 dev
->miimon
= miimon
;
1563 netdev_linux_changed(dev
, dev
->ifi_flags
, 0);
1566 timer_set_duration(&dev
->miimon_timer
, dev
->miimon_interval
);
1568 ovs_mutex_unlock(&dev
->mutex
);
1569 netdev_close(netdev
);
1572 shash_destroy(&device_shash
);
1576 netdev_linux_miimon_wait(void)
1578 struct shash device_shash
;
1579 struct shash_node
*node
;
1581 shash_init(&device_shash
);
1582 netdev_get_devices(&netdev_linux_class
, &device_shash
);
1583 SHASH_FOR_EACH (node
, &device_shash
) {
1584 struct netdev
*netdev
= node
->data
;
1585 struct netdev_linux
*dev
= netdev_linux_cast(netdev
);
1587 ovs_mutex_lock(&dev
->mutex
);
1588 if (dev
->miimon_interval
> 0) {
1589 timer_wait(&dev
->miimon_timer
);
1591 ovs_mutex_unlock(&dev
->mutex
);
1592 netdev_close(netdev
);
1594 shash_destroy(&device_shash
);
1598 swap_uint64(uint64_t *a
, uint64_t *b
)
1605 /* Copies 'src' into 'dst', performing format conversion in the process.
1607 * 'src' is allowed to be misaligned. */
1609 netdev_stats_from_ovs_vport_stats(struct netdev_stats
*dst
,
1610 const struct ovs_vport_stats
*src
)
1612 dst
->rx_packets
= get_32aligned_u64(&src
->rx_packets
);
1613 dst
->tx_packets
= get_32aligned_u64(&src
->tx_packets
);
1614 dst
->rx_bytes
= get_32aligned_u64(&src
->rx_bytes
);
1615 dst
->tx_bytes
= get_32aligned_u64(&src
->tx_bytes
);
1616 dst
->rx_errors
= get_32aligned_u64(&src
->rx_errors
);
1617 dst
->tx_errors
= get_32aligned_u64(&src
->tx_errors
);
1618 dst
->rx_dropped
= get_32aligned_u64(&src
->rx_dropped
);
1619 dst
->tx_dropped
= get_32aligned_u64(&src
->tx_dropped
);
1621 dst
->collisions
= 0;
1622 dst
->rx_length_errors
= 0;
1623 dst
->rx_over_errors
= 0;
1624 dst
->rx_crc_errors
= 0;
1625 dst
->rx_frame_errors
= 0;
1626 dst
->rx_fifo_errors
= 0;
1627 dst
->rx_missed_errors
= 0;
1628 dst
->tx_aborted_errors
= 0;
1629 dst
->tx_carrier_errors
= 0;
1630 dst
->tx_fifo_errors
= 0;
1631 dst
->tx_heartbeat_errors
= 0;
1632 dst
->tx_window_errors
= 0;
1636 get_stats_via_vport__(const struct netdev
*netdev
, struct netdev_stats
*stats
)
1638 struct dpif_netlink_vport reply
;
1642 error
= dpif_netlink_vport_get(netdev_get_name(netdev
), &reply
, &buf
);
1645 } else if (!reply
.stats
) {
1650 netdev_stats_from_ovs_vport_stats(stats
, reply
.stats
);
1658 get_stats_via_vport(const struct netdev
*netdev_
,
1659 struct netdev_stats
*stats
)
1661 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
1663 if (!netdev
->vport_stats_error
||
1664 !(netdev
->cache_valid
& VALID_VPORT_STAT_ERROR
)) {
1667 error
= get_stats_via_vport__(netdev_
, stats
);
1668 if (error
&& error
!= ENOENT
&& error
!= ENODEV
) {
1669 VLOG_WARN_RL(&rl
, "%s: obtaining netdev stats via vport failed "
1671 netdev_get_name(netdev_
), ovs_strerror(error
));
1673 netdev
->vport_stats_error
= error
;
1674 netdev
->cache_valid
|= VALID_VPORT_STAT_ERROR
;
1678 /* Retrieves current device stats for 'netdev-linux'. */
1680 netdev_linux_get_stats(const struct netdev
*netdev_
,
1681 struct netdev_stats
*stats
)
1683 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
1684 struct netdev_stats dev_stats
;
1687 ovs_mutex_lock(&netdev
->mutex
);
1688 get_stats_via_vport(netdev_
, stats
);
1689 error
= get_stats_via_netlink(netdev_
, &dev_stats
);
1691 if (!netdev
->vport_stats_error
) {
1694 } else if (netdev
->vport_stats_error
) {
1695 /* stats not available from OVS then use netdev stats. */
1698 /* Use kernel netdev's packet and byte counts since vport's counters
1699 * do not reflect packet counts on the wire when GSO, TSO or GRO are
1701 stats
->rx_packets
= dev_stats
.rx_packets
;
1702 stats
->rx_bytes
= dev_stats
.rx_bytes
;
1703 stats
->tx_packets
= dev_stats
.tx_packets
;
1704 stats
->tx_bytes
= dev_stats
.tx_bytes
;
1706 stats
->rx_errors
+= dev_stats
.rx_errors
;
1707 stats
->tx_errors
+= dev_stats
.tx_errors
;
1708 stats
->rx_dropped
+= dev_stats
.rx_dropped
;
1709 stats
->tx_dropped
+= dev_stats
.tx_dropped
;
1710 stats
->multicast
+= dev_stats
.multicast
;
1711 stats
->collisions
+= dev_stats
.collisions
;
1712 stats
->rx_length_errors
+= dev_stats
.rx_length_errors
;
1713 stats
->rx_over_errors
+= dev_stats
.rx_over_errors
;
1714 stats
->rx_crc_errors
+= dev_stats
.rx_crc_errors
;
1715 stats
->rx_frame_errors
+= dev_stats
.rx_frame_errors
;
1716 stats
->rx_fifo_errors
+= dev_stats
.rx_fifo_errors
;
1717 stats
->rx_missed_errors
+= dev_stats
.rx_missed_errors
;
1718 stats
->tx_aborted_errors
+= dev_stats
.tx_aborted_errors
;
1719 stats
->tx_carrier_errors
+= dev_stats
.tx_carrier_errors
;
1720 stats
->tx_fifo_errors
+= dev_stats
.tx_fifo_errors
;
1721 stats
->tx_heartbeat_errors
+= dev_stats
.tx_heartbeat_errors
;
1722 stats
->tx_window_errors
+= dev_stats
.tx_window_errors
;
1724 ovs_mutex_unlock(&netdev
->mutex
);
1729 /* Retrieves current device stats for 'netdev-tap' netdev or
1730 * netdev-internal. */
1732 netdev_tap_get_stats(const struct netdev
*netdev_
, struct netdev_stats
*stats
)
1734 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
1735 struct netdev_stats dev_stats
;
1738 ovs_mutex_lock(&netdev
->mutex
);
1739 get_stats_via_vport(netdev_
, stats
);
1740 error
= get_stats_via_netlink(netdev_
, &dev_stats
);
1742 if (!netdev
->vport_stats_error
) {
1745 } else if (netdev
->vport_stats_error
) {
1746 /* Transmit and receive stats will appear to be swapped relative to the
1747 * other ports since we are the one sending the data, not a remote
1748 * computer. For consistency, we swap them back here. This does not
1749 * apply if we are getting stats from the vport layer because it always
1750 * tracks stats from the perspective of the switch. */
1753 swap_uint64(&stats
->rx_packets
, &stats
->tx_packets
);
1754 swap_uint64(&stats
->rx_bytes
, &stats
->tx_bytes
);
1755 swap_uint64(&stats
->rx_errors
, &stats
->tx_errors
);
1756 swap_uint64(&stats
->rx_dropped
, &stats
->tx_dropped
);
1757 stats
->rx_length_errors
= 0;
1758 stats
->rx_over_errors
= 0;
1759 stats
->rx_crc_errors
= 0;
1760 stats
->rx_frame_errors
= 0;
1761 stats
->rx_fifo_errors
= 0;
1762 stats
->rx_missed_errors
= 0;
1763 stats
->tx_aborted_errors
= 0;
1764 stats
->tx_carrier_errors
= 0;
1765 stats
->tx_fifo_errors
= 0;
1766 stats
->tx_heartbeat_errors
= 0;
1767 stats
->tx_window_errors
= 0;
1769 /* Use kernel netdev's packet and byte counts since vport counters
1770 * do not reflect packet counts on the wire when GSO, TSO or GRO
1772 stats
->rx_packets
= dev_stats
.tx_packets
;
1773 stats
->rx_bytes
= dev_stats
.tx_bytes
;
1774 stats
->tx_packets
= dev_stats
.rx_packets
;
1775 stats
->tx_bytes
= dev_stats
.rx_bytes
;
1777 stats
->rx_dropped
+= dev_stats
.tx_dropped
;
1778 stats
->tx_dropped
+= dev_stats
.rx_dropped
;
1780 stats
->rx_errors
+= dev_stats
.tx_errors
;
1781 stats
->tx_errors
+= dev_stats
.rx_errors
;
1783 stats
->multicast
+= dev_stats
.multicast
;
1784 stats
->collisions
+= dev_stats
.collisions
;
1786 ovs_mutex_unlock(&netdev
->mutex
);
1792 netdev_internal_get_stats(const struct netdev
*netdev_
,
1793 struct netdev_stats
*stats
)
1795 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
1798 ovs_mutex_lock(&netdev
->mutex
);
1799 get_stats_via_vport(netdev_
, stats
);
1800 error
= netdev
->vport_stats_error
;
1801 ovs_mutex_unlock(&netdev
->mutex
);
1807 netdev_linux_read_features(struct netdev_linux
*netdev
)
1809 struct ethtool_cmd ecmd
;
1813 if (netdev
->cache_valid
& VALID_FEATURES
) {
1817 COVERAGE_INC(netdev_get_ethtool
);
1818 memset(&ecmd
, 0, sizeof ecmd
);
1819 error
= netdev_linux_do_ethtool(netdev
->up
.name
, &ecmd
,
1820 ETHTOOL_GSET
, "ETHTOOL_GSET");
1825 /* Supported features. */
1826 netdev
->supported
= 0;
1827 if (ecmd
.supported
& SUPPORTED_10baseT_Half
) {
1828 netdev
->supported
|= NETDEV_F_10MB_HD
;
1830 if (ecmd
.supported
& SUPPORTED_10baseT_Full
) {
1831 netdev
->supported
|= NETDEV_F_10MB_FD
;
1833 if (ecmd
.supported
& SUPPORTED_100baseT_Half
) {
1834 netdev
->supported
|= NETDEV_F_100MB_HD
;
1836 if (ecmd
.supported
& SUPPORTED_100baseT_Full
) {
1837 netdev
->supported
|= NETDEV_F_100MB_FD
;
1839 if (ecmd
.supported
& SUPPORTED_1000baseT_Half
) {
1840 netdev
->supported
|= NETDEV_F_1GB_HD
;
1842 if ((ecmd
.supported
& SUPPORTED_1000baseT_Full
) ||
1843 (ecmd
.supported
& SUPPORTED_1000baseKX_Full
)) {
1844 netdev
->supported
|= NETDEV_F_1GB_FD
;
1846 if ((ecmd
.supported
& SUPPORTED_10000baseT_Full
) ||
1847 (ecmd
.supported
& SUPPORTED_10000baseKX4_Full
) ||
1848 (ecmd
.supported
& SUPPORTED_10000baseKR_Full
) ||
1849 (ecmd
.supported
& SUPPORTED_10000baseR_FEC
)) {
1850 netdev
->supported
|= NETDEV_F_10GB_FD
;
1852 if ((ecmd
.supported
& SUPPORTED_40000baseKR4_Full
) ||
1853 (ecmd
.supported
& SUPPORTED_40000baseCR4_Full
) ||
1854 (ecmd
.supported
& SUPPORTED_40000baseSR4_Full
) ||
1855 (ecmd
.supported
& SUPPORTED_40000baseLR4_Full
)) {
1856 netdev
->supported
|= NETDEV_F_40GB_FD
;
1858 if (ecmd
.supported
& SUPPORTED_TP
) {
1859 netdev
->supported
|= NETDEV_F_COPPER
;
1861 if (ecmd
.supported
& SUPPORTED_FIBRE
) {
1862 netdev
->supported
|= NETDEV_F_FIBER
;
1864 if (ecmd
.supported
& SUPPORTED_Autoneg
) {
1865 netdev
->supported
|= NETDEV_F_AUTONEG
;
1867 if (ecmd
.supported
& SUPPORTED_Pause
) {
1868 netdev
->supported
|= NETDEV_F_PAUSE
;
1870 if (ecmd
.supported
& SUPPORTED_Asym_Pause
) {
1871 netdev
->supported
|= NETDEV_F_PAUSE_ASYM
;
1874 /* Advertised features. */
1875 netdev
->advertised
= 0;
1876 if (ecmd
.advertising
& ADVERTISED_10baseT_Half
) {
1877 netdev
->advertised
|= NETDEV_F_10MB_HD
;
1879 if (ecmd
.advertising
& ADVERTISED_10baseT_Full
) {
1880 netdev
->advertised
|= NETDEV_F_10MB_FD
;
1882 if (ecmd
.advertising
& ADVERTISED_100baseT_Half
) {
1883 netdev
->advertised
|= NETDEV_F_100MB_HD
;
1885 if (ecmd
.advertising
& ADVERTISED_100baseT_Full
) {
1886 netdev
->advertised
|= NETDEV_F_100MB_FD
;
1888 if (ecmd
.advertising
& ADVERTISED_1000baseT_Half
) {
1889 netdev
->advertised
|= NETDEV_F_1GB_HD
;
1891 if ((ecmd
.advertising
& ADVERTISED_1000baseT_Full
) ||
1892 (ecmd
.advertising
& ADVERTISED_1000baseKX_Full
)) {
1893 netdev
->advertised
|= NETDEV_F_1GB_FD
;
1895 if ((ecmd
.advertising
& ADVERTISED_10000baseT_Full
) ||
1896 (ecmd
.advertising
& ADVERTISED_10000baseKX4_Full
) ||
1897 (ecmd
.advertising
& ADVERTISED_10000baseKR_Full
) ||
1898 (ecmd
.advertising
& ADVERTISED_10000baseR_FEC
)) {
1899 netdev
->advertised
|= NETDEV_F_10GB_FD
;
1901 if ((ecmd
.advertising
& ADVERTISED_40000baseKR4_Full
) ||
1902 (ecmd
.advertising
& ADVERTISED_40000baseCR4_Full
) ||
1903 (ecmd
.advertising
& ADVERTISED_40000baseSR4_Full
) ||
1904 (ecmd
.advertising
& ADVERTISED_40000baseLR4_Full
)) {
1905 netdev
->advertised
|= NETDEV_F_40GB_FD
;
1907 if (ecmd
.advertising
& ADVERTISED_TP
) {
1908 netdev
->advertised
|= NETDEV_F_COPPER
;
1910 if (ecmd
.advertising
& ADVERTISED_FIBRE
) {
1911 netdev
->advertised
|= NETDEV_F_FIBER
;
1913 if (ecmd
.advertising
& ADVERTISED_Autoneg
) {
1914 netdev
->advertised
|= NETDEV_F_AUTONEG
;
1916 if (ecmd
.advertising
& ADVERTISED_Pause
) {
1917 netdev
->advertised
|= NETDEV_F_PAUSE
;
1919 if (ecmd
.advertising
& ADVERTISED_Asym_Pause
) {
1920 netdev
->advertised
|= NETDEV_F_PAUSE_ASYM
;
1923 /* Current settings. */
1924 speed
= ethtool_cmd_speed(&ecmd
);
1925 if (speed
== SPEED_10
) {
1926 netdev
->current
= ecmd
.duplex
? NETDEV_F_10MB_FD
: NETDEV_F_10MB_HD
;
1927 } else if (speed
== SPEED_100
) {
1928 netdev
->current
= ecmd
.duplex
? NETDEV_F_100MB_FD
: NETDEV_F_100MB_HD
;
1929 } else if (speed
== SPEED_1000
) {
1930 netdev
->current
= ecmd
.duplex
? NETDEV_F_1GB_FD
: NETDEV_F_1GB_HD
;
1931 } else if (speed
== SPEED_10000
) {
1932 netdev
->current
= NETDEV_F_10GB_FD
;
1933 } else if (speed
== 40000) {
1934 netdev
->current
= NETDEV_F_40GB_FD
;
1935 } else if (speed
== 100000) {
1936 netdev
->current
= NETDEV_F_100GB_FD
;
1937 } else if (speed
== 1000000) {
1938 netdev
->current
= NETDEV_F_1TB_FD
;
1940 netdev
->current
= 0;
1943 if (ecmd
.port
== PORT_TP
) {
1944 netdev
->current
|= NETDEV_F_COPPER
;
1945 } else if (ecmd
.port
== PORT_FIBRE
) {
1946 netdev
->current
|= NETDEV_F_FIBER
;
1950 netdev
->current
|= NETDEV_F_AUTONEG
;
1954 netdev
->cache_valid
|= VALID_FEATURES
;
1955 netdev
->get_features_error
= error
;
1958 /* Stores the features supported by 'netdev' into of '*current', '*advertised',
1959 * '*supported', and '*peer'. Each value is a bitmap of NETDEV_* bits.
1960 * Returns 0 if successful, otherwise a positive errno value. */
1962 netdev_linux_get_features(const struct netdev
*netdev_
,
1963 enum netdev_features
*current
,
1964 enum netdev_features
*advertised
,
1965 enum netdev_features
*supported
,
1966 enum netdev_features
*peer
)
1968 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
1971 ovs_mutex_lock(&netdev
->mutex
);
1972 netdev_linux_read_features(netdev
);
1973 if (!netdev
->get_features_error
) {
1974 *current
= netdev
->current
;
1975 *advertised
= netdev
->advertised
;
1976 *supported
= netdev
->supported
;
1977 *peer
= 0; /* XXX */
1979 error
= netdev
->get_features_error
;
1980 ovs_mutex_unlock(&netdev
->mutex
);
1985 /* Set the features advertised by 'netdev' to 'advertise'. */
1987 netdev_linux_set_advertisements(struct netdev
*netdev_
,
1988 enum netdev_features advertise
)
1990 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
1991 struct ethtool_cmd ecmd
;
1994 ovs_mutex_lock(&netdev
->mutex
);
1996 COVERAGE_INC(netdev_get_ethtool
);
1997 memset(&ecmd
, 0, sizeof ecmd
);
1998 error
= netdev_linux_do_ethtool(netdev_get_name(netdev_
), &ecmd
,
1999 ETHTOOL_GSET
, "ETHTOOL_GSET");
2004 ecmd
.advertising
= 0;
2005 if (advertise
& NETDEV_F_10MB_HD
) {
2006 ecmd
.advertising
|= ADVERTISED_10baseT_Half
;
2008 if (advertise
& NETDEV_F_10MB_FD
) {
2009 ecmd
.advertising
|= ADVERTISED_10baseT_Full
;
2011 if (advertise
& NETDEV_F_100MB_HD
) {
2012 ecmd
.advertising
|= ADVERTISED_100baseT_Half
;
2014 if (advertise
& NETDEV_F_100MB_FD
) {
2015 ecmd
.advertising
|= ADVERTISED_100baseT_Full
;
2017 if (advertise
& NETDEV_F_1GB_HD
) {
2018 ecmd
.advertising
|= ADVERTISED_1000baseT_Half
;
2020 if (advertise
& NETDEV_F_1GB_FD
) {
2021 ecmd
.advertising
|= ADVERTISED_1000baseT_Full
;
2023 if (advertise
& NETDEV_F_10GB_FD
) {
2024 ecmd
.advertising
|= ADVERTISED_10000baseT_Full
;
2026 if (advertise
& NETDEV_F_COPPER
) {
2027 ecmd
.advertising
|= ADVERTISED_TP
;
2029 if (advertise
& NETDEV_F_FIBER
) {
2030 ecmd
.advertising
|= ADVERTISED_FIBRE
;
2032 if (advertise
& NETDEV_F_AUTONEG
) {
2033 ecmd
.advertising
|= ADVERTISED_Autoneg
;
2035 if (advertise
& NETDEV_F_PAUSE
) {
2036 ecmd
.advertising
|= ADVERTISED_Pause
;
2038 if (advertise
& NETDEV_F_PAUSE_ASYM
) {
2039 ecmd
.advertising
|= ADVERTISED_Asym_Pause
;
2041 COVERAGE_INC(netdev_set_ethtool
);
2042 error
= netdev_linux_do_ethtool(netdev_get_name(netdev_
), &ecmd
,
2043 ETHTOOL_SSET
, "ETHTOOL_SSET");
2046 ovs_mutex_unlock(&netdev
->mutex
);
2050 /* Attempts to set input rate limiting (policing) policy. Returns 0 if
2051 * successful, otherwise a positive errno value. */
2053 netdev_linux_set_policing(struct netdev
*netdev_
,
2054 uint32_t kbits_rate
, uint32_t kbits_burst
)
2056 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
2057 const char *netdev_name
= netdev_get_name(netdev_
);
2060 kbits_burst
= (!kbits_rate
? 0 /* Force to 0 if no rate specified. */
2061 : !kbits_burst
? 8000 /* Default to 8000 kbits if 0. */
2062 : kbits_burst
); /* Stick with user-specified value. */
2064 ovs_mutex_lock(&netdev
->mutex
);
2065 if (netdev
->cache_valid
& VALID_POLICING
) {
2066 error
= netdev
->netdev_policing_error
;
2067 if (error
|| (netdev
->kbits_rate
== kbits_rate
&&
2068 netdev
->kbits_burst
== kbits_burst
)) {
2069 /* Assume that settings haven't changed since we last set them. */
2072 netdev
->cache_valid
&= ~VALID_POLICING
;
2075 COVERAGE_INC(netdev_set_policing
);
2076 /* Remove any existing ingress qdisc. */
2077 error
= tc_add_del_ingress_qdisc(netdev_
, false);
2079 VLOG_WARN_RL(&rl
, "%s: removing policing failed: %s",
2080 netdev_name
, ovs_strerror(error
));
2085 error
= tc_add_del_ingress_qdisc(netdev_
, true);
2087 VLOG_WARN_RL(&rl
, "%s: adding policing qdisc failed: %s",
2088 netdev_name
, ovs_strerror(error
));
2092 error
= tc_add_policer(netdev_
, kbits_rate
, kbits_burst
);
2094 VLOG_WARN_RL(&rl
, "%s: adding policing action failed: %s",
2095 netdev_name
, ovs_strerror(error
));
2100 netdev
->kbits_rate
= kbits_rate
;
2101 netdev
->kbits_burst
= kbits_burst
;
2104 if (!error
|| error
== ENODEV
) {
2105 netdev
->netdev_policing_error
= error
;
2106 netdev
->cache_valid
|= VALID_POLICING
;
2108 ovs_mutex_unlock(&netdev
->mutex
);
2113 netdev_linux_get_qos_types(const struct netdev
*netdev OVS_UNUSED
,
2116 const struct tc_ops
*const *opsp
;
2117 for (opsp
= tcs
; *opsp
!= NULL
; opsp
++) {
2118 const struct tc_ops
*ops
= *opsp
;
2119 if (ops
->tc_install
&& ops
->ovs_name
[0] != '\0') {
2120 sset_add(types
, ops
->ovs_name
);
2126 static const struct tc_ops
*
2127 tc_lookup_ovs_name(const char *name
)
2129 const struct tc_ops
*const *opsp
;
2131 for (opsp
= tcs
; *opsp
!= NULL
; opsp
++) {
2132 const struct tc_ops
*ops
= *opsp
;
2133 if (!strcmp(name
, ops
->ovs_name
)) {
2140 static const struct tc_ops
*
2141 tc_lookup_linux_name(const char *name
)
2143 const struct tc_ops
*const *opsp
;
2145 for (opsp
= tcs
; *opsp
!= NULL
; opsp
++) {
2146 const struct tc_ops
*ops
= *opsp
;
2147 if (ops
->linux_name
&& !strcmp(name
, ops
->linux_name
)) {
2154 static struct tc_queue
*
2155 tc_find_queue__(const struct netdev
*netdev_
, unsigned int queue_id
,
2158 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
2159 struct tc_queue
*queue
;
2161 HMAP_FOR_EACH_IN_BUCKET (queue
, hmap_node
, hash
, &netdev
->tc
->queues
) {
2162 if (queue
->queue_id
== queue_id
) {
2169 static struct tc_queue
*
2170 tc_find_queue(const struct netdev
*netdev
, unsigned int queue_id
)
2172 return tc_find_queue__(netdev
, queue_id
, hash_int(queue_id
, 0));
2176 netdev_linux_get_qos_capabilities(const struct netdev
*netdev OVS_UNUSED
,
2178 struct netdev_qos_capabilities
*caps
)
2180 const struct tc_ops
*ops
= tc_lookup_ovs_name(type
);
2184 caps
->n_queues
= ops
->n_queues
;
2189 netdev_linux_get_qos(const struct netdev
*netdev_
,
2190 const char **typep
, struct smap
*details
)
2192 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
2195 ovs_mutex_lock(&netdev
->mutex
);
2196 error
= tc_query_qdisc(netdev_
);
2198 *typep
= netdev
->tc
->ops
->ovs_name
;
2199 error
= (netdev
->tc
->ops
->qdisc_get
2200 ? netdev
->tc
->ops
->qdisc_get(netdev_
, details
)
2203 ovs_mutex_unlock(&netdev
->mutex
);
2209 netdev_linux_set_qos(struct netdev
*netdev_
,
2210 const char *type
, const struct smap
*details
)
2212 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
2213 const struct tc_ops
*new_ops
;
2216 new_ops
= tc_lookup_ovs_name(type
);
2217 if (!new_ops
|| !new_ops
->tc_install
) {
2221 if (new_ops
== &tc_ops_noop
) {
2222 return new_ops
->tc_install(netdev_
, details
);
2225 ovs_mutex_lock(&netdev
->mutex
);
2226 error
= tc_query_qdisc(netdev_
);
2231 if (new_ops
== netdev
->tc
->ops
) {
2232 error
= new_ops
->qdisc_set
? new_ops
->qdisc_set(netdev_
, details
) : 0;
2234 /* Delete existing qdisc. */
2235 error
= tc_del_qdisc(netdev_
);
2239 ovs_assert(netdev
->tc
== NULL
);
2241 /* Install new qdisc. */
2242 error
= new_ops
->tc_install(netdev_
, details
);
2243 ovs_assert((error
== 0) == (netdev
->tc
!= NULL
));
2247 ovs_mutex_unlock(&netdev
->mutex
);
2252 netdev_linux_get_queue(const struct netdev
*netdev_
,
2253 unsigned int queue_id
, struct smap
*details
)
2255 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
2258 ovs_mutex_lock(&netdev
->mutex
);
2259 error
= tc_query_qdisc(netdev_
);
2261 struct tc_queue
*queue
= tc_find_queue(netdev_
, queue_id
);
2263 ? netdev
->tc
->ops
->class_get(netdev_
, queue
, details
)
2266 ovs_mutex_unlock(&netdev
->mutex
);
2272 netdev_linux_set_queue(struct netdev
*netdev_
,
2273 unsigned int queue_id
, const struct smap
*details
)
2275 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
2278 ovs_mutex_lock(&netdev
->mutex
);
2279 error
= tc_query_qdisc(netdev_
);
2281 error
= (queue_id
< netdev
->tc
->ops
->n_queues
2282 && netdev
->tc
->ops
->class_set
2283 ? netdev
->tc
->ops
->class_set(netdev_
, queue_id
, details
)
2286 ovs_mutex_unlock(&netdev
->mutex
);
2292 netdev_linux_delete_queue(struct netdev
*netdev_
, unsigned int queue_id
)
2294 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
2297 ovs_mutex_lock(&netdev
->mutex
);
2298 error
= tc_query_qdisc(netdev_
);
2300 if (netdev
->tc
->ops
->class_delete
) {
2301 struct tc_queue
*queue
= tc_find_queue(netdev_
, queue_id
);
2303 ? netdev
->tc
->ops
->class_delete(netdev_
, queue
)
2309 ovs_mutex_unlock(&netdev
->mutex
);
2315 netdev_linux_get_queue_stats(const struct netdev
*netdev_
,
2316 unsigned int queue_id
,
2317 struct netdev_queue_stats
*stats
)
2319 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
2322 ovs_mutex_lock(&netdev
->mutex
);
2323 error
= tc_query_qdisc(netdev_
);
2325 if (netdev
->tc
->ops
->class_get_stats
) {
2326 const struct tc_queue
*queue
= tc_find_queue(netdev_
, queue_id
);
2328 stats
->created
= queue
->created
;
2329 error
= netdev
->tc
->ops
->class_get_stats(netdev_
, queue
,
2338 ovs_mutex_unlock(&netdev
->mutex
);
2343 struct queue_dump_state
{
2344 struct nl_dump dump
;
2349 start_queue_dump(const struct netdev
*netdev
, struct queue_dump_state
*state
)
2351 struct ofpbuf request
;
2352 struct tcmsg
*tcmsg
;
2354 tcmsg
= tc_make_request(netdev
, RTM_GETTCLASS
, 0, &request
);
2358 tcmsg
->tcm_parent
= 0;
2359 nl_dump_start(&state
->dump
, NETLINK_ROUTE
, &request
);
2360 ofpbuf_uninit(&request
);
2362 ofpbuf_init(&state
->buf
, NL_DUMP_BUFSIZE
);
2367 finish_queue_dump(struct queue_dump_state
*state
)
2369 ofpbuf_uninit(&state
->buf
);
2370 return nl_dump_done(&state
->dump
);
2373 struct netdev_linux_queue_state
{
2374 unsigned int *queues
;
2380 netdev_linux_queue_dump_start(const struct netdev
*netdev_
, void **statep
)
2382 const struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
2385 ovs_mutex_lock(&netdev
->mutex
);
2386 error
= tc_query_qdisc(netdev_
);
2388 if (netdev
->tc
->ops
->class_get
) {
2389 struct netdev_linux_queue_state
*state
;
2390 struct tc_queue
*queue
;
2393 *statep
= state
= xmalloc(sizeof *state
);
2394 state
->n_queues
= hmap_count(&netdev
->tc
->queues
);
2395 state
->cur_queue
= 0;
2396 state
->queues
= xmalloc(state
->n_queues
* sizeof *state
->queues
);
2399 HMAP_FOR_EACH (queue
, hmap_node
, &netdev
->tc
->queues
) {
2400 state
->queues
[i
++] = queue
->queue_id
;
2406 ovs_mutex_unlock(&netdev
->mutex
);
2412 netdev_linux_queue_dump_next(const struct netdev
*netdev_
, void *state_
,
2413 unsigned int *queue_idp
, struct smap
*details
)
2415 const struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
2416 struct netdev_linux_queue_state
*state
= state_
;
2419 ovs_mutex_lock(&netdev
->mutex
);
2420 while (state
->cur_queue
< state
->n_queues
) {
2421 unsigned int queue_id
= state
->queues
[state
->cur_queue
++];
2422 struct tc_queue
*queue
= tc_find_queue(netdev_
, queue_id
);
2425 *queue_idp
= queue_id
;
2426 error
= netdev
->tc
->ops
->class_get(netdev_
, queue
, details
);
2430 ovs_mutex_unlock(&netdev
->mutex
);
2436 netdev_linux_queue_dump_done(const struct netdev
*netdev OVS_UNUSED
,
2439 struct netdev_linux_queue_state
*state
= state_
;
2441 free(state
->queues
);
2447 netdev_linux_dump_queue_stats(const struct netdev
*netdev_
,
2448 netdev_dump_queue_stats_cb
*cb
, void *aux
)
2450 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
2453 ovs_mutex_lock(&netdev
->mutex
);
2454 error
= tc_query_qdisc(netdev_
);
2456 struct queue_dump_state state
;
2458 if (!netdev
->tc
->ops
->class_dump_stats
) {
2460 } else if (!start_queue_dump(netdev_
, &state
)) {
2466 while (nl_dump_next(&state
.dump
, &msg
, &state
.buf
)) {
2467 retval
= netdev
->tc
->ops
->class_dump_stats(netdev_
, &msg
,
2474 retval
= finish_queue_dump(&state
);
2480 ovs_mutex_unlock(&netdev
->mutex
);
2486 netdev_linux_set_in4(struct netdev
*netdev_
, struct in_addr address
,
2487 struct in_addr netmask
)
2489 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
2492 ovs_mutex_lock(&netdev
->mutex
);
2493 error
= do_set_addr(netdev_
, SIOCSIFADDR
, "SIOCSIFADDR", address
);
2495 if (address
.s_addr
!= INADDR_ANY
) {
2496 error
= do_set_addr(netdev_
, SIOCSIFNETMASK
,
2497 "SIOCSIFNETMASK", netmask
);
2501 ovs_mutex_unlock(&netdev
->mutex
);
2506 /* If 'netdev' has an assigned IPv6 address, sets '*in6' to that address.
2507 * Otherwise, sets '*in6' to 'in6addr_any' and returns the corresponding
2510 netdev_linux_get_addr_list(const struct netdev
*netdev_
,
2511 struct in6_addr
**addr
, struct in6_addr
**mask
, int *n_cnt
)
2513 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
2516 ovs_mutex_lock(&netdev
->mutex
);
2517 error
= netdev_get_addrs(netdev_get_name(netdev_
), addr
, mask
, n_cnt
);
2518 ovs_mutex_unlock(&netdev
->mutex
);
2524 make_in4_sockaddr(struct sockaddr
*sa
, struct in_addr addr
)
2526 struct sockaddr_in sin
;
2527 memset(&sin
, 0, sizeof sin
);
2528 sin
.sin_family
= AF_INET
;
2529 sin
.sin_addr
= addr
;
2532 memset(sa
, 0, sizeof *sa
);
2533 memcpy(sa
, &sin
, sizeof sin
);
2537 do_set_addr(struct netdev
*netdev
,
2538 int ioctl_nr
, const char *ioctl_name
, struct in_addr addr
)
2542 make_in4_sockaddr(&ifr
.ifr_addr
, addr
);
2543 return af_inet_ifreq_ioctl(netdev_get_name(netdev
), &ifr
, ioctl_nr
,
2547 /* Adds 'router' as a default IP gateway. */
2549 netdev_linux_add_router(struct netdev
*netdev OVS_UNUSED
, struct in_addr router
)
2551 struct in_addr any
= { INADDR_ANY
};
2555 memset(&rt
, 0, sizeof rt
);
2556 make_in4_sockaddr(&rt
.rt_dst
, any
);
2557 make_in4_sockaddr(&rt
.rt_gateway
, router
);
2558 make_in4_sockaddr(&rt
.rt_genmask
, any
);
2559 rt
.rt_flags
= RTF_UP
| RTF_GATEWAY
;
2560 error
= af_inet_ioctl(SIOCADDRT
, &rt
);
2562 VLOG_WARN("ioctl(SIOCADDRT): %s", ovs_strerror(error
));
2568 netdev_linux_get_next_hop(const struct in_addr
*host
, struct in_addr
*next_hop
,
2571 static const char fn
[] = "/proc/net/route";
2576 *netdev_name
= NULL
;
2577 stream
= fopen(fn
, "r");
2578 if (stream
== NULL
) {
2579 VLOG_WARN_RL(&rl
, "%s: open failed: %s", fn
, ovs_strerror(errno
));
2584 while (fgets(line
, sizeof line
, stream
)) {
2587 ovs_be32 dest
, gateway
, mask
;
2588 int refcnt
, metric
, mtu
;
2589 unsigned int flags
, use
, window
, irtt
;
2592 "%16s %"SCNx32
" %"SCNx32
" %04X %d %u %d %"SCNx32
2594 iface
, &dest
, &gateway
, &flags
, &refcnt
,
2595 &use
, &metric
, &mask
, &mtu
, &window
, &irtt
)) {
2596 VLOG_WARN_RL(&rl
, "%s: could not parse line %d: %s",
2600 if (!(flags
& RTF_UP
)) {
2601 /* Skip routes that aren't up. */
2605 /* The output of 'dest', 'mask', and 'gateway' were given in
2606 * network byte order, so we don't need need any endian
2607 * conversions here. */
2608 if ((dest
& mask
) == (host
->s_addr
& mask
)) {
2610 /* The host is directly reachable. */
2611 next_hop
->s_addr
= 0;
2613 /* To reach the host, we must go through a gateway. */
2614 next_hop
->s_addr
= gateway
;
2616 *netdev_name
= xstrdup(iface
);
2628 netdev_linux_get_status(const struct netdev
*netdev_
, struct smap
*smap
)
2630 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
2633 ovs_mutex_lock(&netdev
->mutex
);
2634 if (!(netdev
->cache_valid
& VALID_DRVINFO
)) {
2635 struct ethtool_cmd
*cmd
= (struct ethtool_cmd
*) &netdev
->drvinfo
;
2637 COVERAGE_INC(netdev_get_ethtool
);
2638 memset(&netdev
->drvinfo
, 0, sizeof netdev
->drvinfo
);
2639 error
= netdev_linux_do_ethtool(netdev
->up
.name
,
2642 "ETHTOOL_GDRVINFO");
2644 netdev
->cache_valid
|= VALID_DRVINFO
;
2649 smap_add(smap
, "driver_name", netdev
->drvinfo
.driver
);
2650 smap_add(smap
, "driver_version", netdev
->drvinfo
.version
);
2651 smap_add(smap
, "firmware_version", netdev
->drvinfo
.fw_version
);
2653 ovs_mutex_unlock(&netdev
->mutex
);
2659 netdev_internal_get_status(const struct netdev
*netdev OVS_UNUSED
,
2662 smap_add(smap
, "driver_name", "openvswitch");
2666 /* Looks up the ARP table entry for 'ip' on 'netdev'. If one exists and can be
2667 * successfully retrieved, it stores the corresponding MAC address in 'mac' and
2668 * returns 0. Otherwise, it returns a positive errno value; in particular,
2669 * ENXIO indicates that there is not ARP table entry for 'ip' on 'netdev'. */
2671 netdev_linux_arp_lookup(const struct netdev
*netdev
,
2672 ovs_be32 ip
, struct eth_addr
*mac
)
2675 struct sockaddr_in sin
;
2678 memset(&r
, 0, sizeof r
);
2679 memset(&sin
, 0, sizeof sin
);
2680 sin
.sin_family
= AF_INET
;
2681 sin
.sin_addr
.s_addr
= ip
;
2683 memcpy(&r
.arp_pa
, &sin
, sizeof sin
);
2684 r
.arp_ha
.sa_family
= ARPHRD_ETHER
;
2686 ovs_strzcpy(r
.arp_dev
, netdev_get_name(netdev
), sizeof r
.arp_dev
);
2687 COVERAGE_INC(netdev_arp_lookup
);
2688 retval
= af_inet_ioctl(SIOCGARP
, &r
);
2690 memcpy(mac
, r
.arp_ha
.sa_data
, ETH_ADDR_LEN
);
2691 } else if (retval
!= ENXIO
) {
2692 VLOG_WARN_RL(&rl
, "%s: could not look up ARP entry for "IP_FMT
": %s",
2693 netdev_get_name(netdev
), IP_ARGS(ip
),
2694 ovs_strerror(retval
));
2700 nd_to_iff_flags(enum netdev_flags nd
)
2703 if (nd
& NETDEV_UP
) {
2706 if (nd
& NETDEV_PROMISC
) {
2709 if (nd
& NETDEV_LOOPBACK
) {
2710 iff
|= IFF_LOOPBACK
;
2716 iff_to_nd_flags(int iff
)
2718 enum netdev_flags nd
= 0;
2722 if (iff
& IFF_PROMISC
) {
2723 nd
|= NETDEV_PROMISC
;
2725 if (iff
& IFF_LOOPBACK
) {
2726 nd
|= NETDEV_LOOPBACK
;
2732 update_flags(struct netdev_linux
*netdev
, enum netdev_flags off
,
2733 enum netdev_flags on
, enum netdev_flags
*old_flagsp
)
2734 OVS_REQUIRES(netdev
->mutex
)
2736 int old_flags
, new_flags
;
2739 old_flags
= netdev
->ifi_flags
;
2740 *old_flagsp
= iff_to_nd_flags(old_flags
);
2741 new_flags
= (old_flags
& ~nd_to_iff_flags(off
)) | nd_to_iff_flags(on
);
2742 if (new_flags
!= old_flags
) {
2743 error
= set_flags(netdev_get_name(&netdev
->up
), new_flags
);
2744 get_flags(&netdev
->up
, &netdev
->ifi_flags
);
2751 netdev_linux_update_flags(struct netdev
*netdev_
, enum netdev_flags off
,
2752 enum netdev_flags on
, enum netdev_flags
*old_flagsp
)
2754 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
2757 ovs_mutex_lock(&netdev
->mutex
);
2758 error
= update_flags(netdev
, off
, on
, old_flagsp
);
2759 ovs_mutex_unlock(&netdev
->mutex
);
2764 #define NETDEV_LINUX_CLASS(NAME, CONSTRUCT, GET_STATS, \
2765 GET_FEATURES, GET_STATUS) \
2768 false, /* is_pmd */ \
2772 netdev_linux_wait, \
2774 netdev_linux_alloc, \
2776 netdev_linux_destruct, \
2777 netdev_linux_dealloc, \
2778 NULL, /* get_config */ \
2779 NULL, /* set_config */ \
2780 NULL, /* get_tunnel_config */ \
2781 NULL, /* build header */ \
2782 NULL, /* push header */ \
2783 NULL, /* pop header */ \
2784 NULL, /* get_numa_id */ \
2785 NULL, /* set_tx_multiq */ \
2787 netdev_linux_send, \
2788 netdev_linux_send_wait, \
2790 netdev_linux_set_etheraddr, \
2791 netdev_linux_get_etheraddr, \
2792 netdev_linux_get_mtu, \
2793 netdev_linux_set_mtu, \
2794 netdev_linux_get_ifindex, \
2795 netdev_linux_get_carrier, \
2796 netdev_linux_get_carrier_resets, \
2797 netdev_linux_set_miimon_interval, \
2801 netdev_linux_set_advertisements, \
2803 netdev_linux_set_policing, \
2804 netdev_linux_get_qos_types, \
2805 netdev_linux_get_qos_capabilities, \
2806 netdev_linux_get_qos, \
2807 netdev_linux_set_qos, \
2808 netdev_linux_get_queue, \
2809 netdev_linux_set_queue, \
2810 netdev_linux_delete_queue, \
2811 netdev_linux_get_queue_stats, \
2812 netdev_linux_queue_dump_start, \
2813 netdev_linux_queue_dump_next, \
2814 netdev_linux_queue_dump_done, \
2815 netdev_linux_dump_queue_stats, \
2817 netdev_linux_set_in4, \
2818 netdev_linux_get_addr_list, \
2819 netdev_linux_add_router, \
2820 netdev_linux_get_next_hop, \
2822 netdev_linux_arp_lookup, \
2824 netdev_linux_update_flags, \
2825 NULL, /* reconfigure */ \
2827 netdev_linux_rxq_alloc, \
2828 netdev_linux_rxq_construct, \
2829 netdev_linux_rxq_destruct, \
2830 netdev_linux_rxq_dealloc, \
2831 netdev_linux_rxq_recv, \
2832 netdev_linux_rxq_wait, \
2833 netdev_linux_rxq_drain, \
2836 const struct netdev_class netdev_linux_class
=
2839 netdev_linux_construct
,
2840 netdev_linux_get_stats
,
2841 netdev_linux_get_features
,
2842 netdev_linux_get_status
);
2844 const struct netdev_class netdev_tap_class
=
2847 netdev_linux_construct_tap
,
2848 netdev_tap_get_stats
,
2849 netdev_linux_get_features
,
2850 netdev_linux_get_status
);
2852 const struct netdev_class netdev_internal_class
=
2855 netdev_linux_construct
,
2856 netdev_internal_get_stats
,
2857 NULL
, /* get_features */
2858 netdev_internal_get_status
);
2861 #define CODEL_N_QUEUES 0x0000
2863 /* In sufficiently new kernel headers these are defined as enums in
2864 * <linux/pkt_sched.h>. Define them here as macros to help out with older
2865 * kernels. (This overrides any enum definition in the header file but that's
2867 #define TCA_CODEL_TARGET 1
2868 #define TCA_CODEL_LIMIT 2
2869 #define TCA_CODEL_INTERVAL 3
2878 static struct codel
*
2879 codel_get__(const struct netdev
*netdev_
)
2881 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
2882 return CONTAINER_OF(netdev
->tc
, struct codel
, tc
);
2886 codel_install__(struct netdev
*netdev_
, uint32_t target
, uint32_t limit
,
2889 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
2890 struct codel
*codel
;
2892 codel
= xmalloc(sizeof *codel
);
2893 tc_init(&codel
->tc
, &tc_ops_codel
);
2894 codel
->target
= target
;
2895 codel
->limit
= limit
;
2896 codel
->interval
= interval
;
2898 netdev
->tc
= &codel
->tc
;
2902 codel_setup_qdisc__(struct netdev
*netdev
, uint32_t target
, uint32_t limit
,
2906 struct ofpbuf request
;
2907 struct tcmsg
*tcmsg
;
2908 uint32_t otarget
, olimit
, ointerval
;
2911 tc_del_qdisc(netdev
);
2913 tcmsg
= tc_make_request(netdev
, RTM_NEWQDISC
,
2914 NLM_F_EXCL
| NLM_F_CREATE
, &request
);
2918 tcmsg
->tcm_handle
= tc_make_handle(1, 0);
2919 tcmsg
->tcm_parent
= TC_H_ROOT
;
2921 otarget
= target
? target
: 5000;
2922 olimit
= limit
? limit
: 10240;
2923 ointerval
= interval
? interval
: 100000;
2925 nl_msg_put_string(&request
, TCA_KIND
, "codel");
2926 opt_offset
= nl_msg_start_nested(&request
, TCA_OPTIONS
);
2927 nl_msg_put_u32(&request
, TCA_CODEL_TARGET
, otarget
);
2928 nl_msg_put_u32(&request
, TCA_CODEL_LIMIT
, olimit
);
2929 nl_msg_put_u32(&request
, TCA_CODEL_INTERVAL
, ointerval
);
2930 nl_msg_end_nested(&request
, opt_offset
);
2932 error
= tc_transact(&request
, NULL
);
2934 VLOG_WARN_RL(&rl
, "failed to replace %s qdisc, "
2935 "target %u, limit %u, interval %u error %d(%s)",
2936 netdev_get_name(netdev
),
2937 otarget
, olimit
, ointerval
,
2938 error
, ovs_strerror(error
));
2944 codel_parse_qdisc_details__(struct netdev
*netdev OVS_UNUSED
,
2945 const struct smap
*details
, struct codel
*codel
)
2947 codel
->target
= smap_get_ullong(details
, "target", 0);
2948 codel
->limit
= smap_get_ullong(details
, "limit", 0);
2949 codel
->interval
= smap_get_ullong(details
, "interval", 0);
2951 if (!codel
->target
) {
2952 codel
->target
= 5000;
2954 if (!codel
->limit
) {
2955 codel
->limit
= 10240;
2957 if (!codel
->interval
) {
2958 codel
->interval
= 100000;
2963 codel_tc_install(struct netdev
*netdev
, const struct smap
*details
)
2968 codel_parse_qdisc_details__(netdev
, details
, &codel
);
2969 error
= codel_setup_qdisc__(netdev
, codel
.target
, codel
.limit
,
2972 codel_install__(netdev
, codel
.target
, codel
.limit
, codel
.interval
);
2978 codel_parse_tca_options__(struct nlattr
*nl_options
, struct codel
*codel
)
2980 static const struct nl_policy tca_codel_policy
[] = {
2981 [TCA_CODEL_TARGET
] = { .type
= NL_A_U32
},
2982 [TCA_CODEL_LIMIT
] = { .type
= NL_A_U32
},
2983 [TCA_CODEL_INTERVAL
] = { .type
= NL_A_U32
}
2986 struct nlattr
*attrs
[ARRAY_SIZE(tca_codel_policy
)];
2988 if (!nl_parse_nested(nl_options
, tca_codel_policy
,
2989 attrs
, ARRAY_SIZE(tca_codel_policy
))) {
2990 VLOG_WARN_RL(&rl
, "failed to parse CoDel class options");
2994 codel
->target
= nl_attr_get_u32(attrs
[TCA_CODEL_TARGET
]);
2995 codel
->limit
= nl_attr_get_u32(attrs
[TCA_CODEL_LIMIT
]);
2996 codel
->interval
= nl_attr_get_u32(attrs
[TCA_CODEL_INTERVAL
]);
3001 codel_tc_load(struct netdev
*netdev
, struct ofpbuf
*nlmsg
)
3003 struct nlattr
*nlattr
;
3008 error
= tc_parse_qdisc(nlmsg
, &kind
, &nlattr
);
3013 error
= codel_parse_tca_options__(nlattr
, &codel
);
3018 codel_install__(netdev
, codel
.target
, codel
.limit
, codel
.interval
);
3024 codel_tc_destroy(struct tc
*tc
)
3026 struct codel
*codel
= CONTAINER_OF(tc
, struct codel
, tc
);
3032 codel_qdisc_get(const struct netdev
*netdev
, struct smap
*details
)
3034 const struct codel
*codel
= codel_get__(netdev
);
3035 smap_add_format(details
, "target", "%u", codel
->target
);
3036 smap_add_format(details
, "limit", "%u", codel
->limit
);
3037 smap_add_format(details
, "interval", "%u", codel
->interval
);
3042 codel_qdisc_set(struct netdev
*netdev
, const struct smap
*details
)
3046 codel_parse_qdisc_details__(netdev
, details
, &codel
);
3047 codel_install__(netdev
, codel
.target
, codel
.limit
, codel
.interval
);
3048 codel_get__(netdev
)->target
= codel
.target
;
3049 codel_get__(netdev
)->limit
= codel
.limit
;
3050 codel_get__(netdev
)->interval
= codel
.interval
;
3054 static const struct tc_ops tc_ops_codel
= {
3055 "codel", /* linux_name */
3056 "linux-codel", /* ovs_name */
3057 CODEL_N_QUEUES
, /* n_queues */
3070 /* FQ-CoDel traffic control class. */
3072 #define FQCODEL_N_QUEUES 0x0000
3074 /* In sufficiently new kernel headers these are defined as enums in
3075 * <linux/pkt_sched.h>. Define them here as macros to help out with older
3076 * kernels. (This overrides any enum definition in the header file but that's
3078 #define TCA_FQ_CODEL_TARGET 1
3079 #define TCA_FQ_CODEL_LIMIT 2
3080 #define TCA_FQ_CODEL_INTERVAL 3
3081 #define TCA_FQ_CODEL_ECN 4
3082 #define TCA_FQ_CODEL_FLOWS 5
3083 #define TCA_FQ_CODEL_QUANTUM 6
3094 static struct fqcodel
*
3095 fqcodel_get__(const struct netdev
*netdev_
)
3097 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
3098 return CONTAINER_OF(netdev
->tc
, struct fqcodel
, tc
);
3102 fqcodel_install__(struct netdev
*netdev_
, uint32_t target
, uint32_t limit
,
3103 uint32_t interval
, uint32_t flows
, uint32_t quantum
)
3105 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
3106 struct fqcodel
*fqcodel
;
3108 fqcodel
= xmalloc(sizeof *fqcodel
);
3109 tc_init(&fqcodel
->tc
, &tc_ops_fqcodel
);
3110 fqcodel
->target
= target
;
3111 fqcodel
->limit
= limit
;
3112 fqcodel
->interval
= interval
;
3113 fqcodel
->flows
= flows
;
3114 fqcodel
->quantum
= quantum
;
3116 netdev
->tc
= &fqcodel
->tc
;
3120 fqcodel_setup_qdisc__(struct netdev
*netdev
, uint32_t target
, uint32_t limit
,
3121 uint32_t interval
, uint32_t flows
, uint32_t quantum
)
3124 struct ofpbuf request
;
3125 struct tcmsg
*tcmsg
;
3126 uint32_t otarget
, olimit
, ointerval
, oflows
, oquantum
;
3129 tc_del_qdisc(netdev
);
3131 tcmsg
= tc_make_request(netdev
, RTM_NEWQDISC
,
3132 NLM_F_EXCL
| NLM_F_CREATE
, &request
);
3136 tcmsg
->tcm_handle
= tc_make_handle(1, 0);
3137 tcmsg
->tcm_parent
= TC_H_ROOT
;
3139 otarget
= target
? target
: 5000;
3140 olimit
= limit
? limit
: 10240;
3141 ointerval
= interval
? interval
: 100000;
3142 oflows
= flows
? flows
: 1024;
3143 oquantum
= quantum
? quantum
: 1514; /* fq_codel default quantum is 1514
3146 nl_msg_put_string(&request
, TCA_KIND
, "fq_codel");
3147 opt_offset
= nl_msg_start_nested(&request
, TCA_OPTIONS
);
3148 nl_msg_put_u32(&request
, TCA_FQ_CODEL_TARGET
, otarget
);
3149 nl_msg_put_u32(&request
, TCA_FQ_CODEL_LIMIT
, olimit
);
3150 nl_msg_put_u32(&request
, TCA_FQ_CODEL_INTERVAL
, ointerval
);
3151 nl_msg_put_u32(&request
, TCA_FQ_CODEL_FLOWS
, oflows
);
3152 nl_msg_put_u32(&request
, TCA_FQ_CODEL_QUANTUM
, oquantum
);
3153 nl_msg_end_nested(&request
, opt_offset
);
3155 error
= tc_transact(&request
, NULL
);
3157 VLOG_WARN_RL(&rl
, "failed to replace %s qdisc, "
3158 "target %u, limit %u, interval %u, flows %u, quantum %u error %d(%s)",
3159 netdev_get_name(netdev
),
3160 otarget
, olimit
, ointerval
, oflows
, oquantum
,
3161 error
, ovs_strerror(error
));
3167 fqcodel_parse_qdisc_details__(struct netdev
*netdev OVS_UNUSED
,
3168 const struct smap
*details
, struct fqcodel
*fqcodel
)
3170 fqcodel
->target
= smap_get_ullong(details
, "target", 0);
3171 fqcodel
->limit
= smap_get_ullong(details
, "limit", 0);
3172 fqcodel
->interval
= smap_get_ullong(details
, "interval", 0);
3173 fqcodel
->flows
= smap_get_ullong(details
, "flows", 0);
3174 fqcodel
->quantum
= smap_get_ullong(details
, "quantum", 0);
3176 if (!fqcodel
->target
) {
3177 fqcodel
->target
= 5000;
3179 if (!fqcodel
->limit
) {
3180 fqcodel
->limit
= 10240;
3182 if (!fqcodel
->interval
) {
3183 fqcodel
->interval
= 1000000;
3185 if (!fqcodel
->flows
) {
3186 fqcodel
->flows
= 1024;
3188 if (!fqcodel
->quantum
) {
3189 fqcodel
->quantum
= 1514;
3194 fqcodel_tc_install(struct netdev
*netdev
, const struct smap
*details
)
3197 struct fqcodel fqcodel
;
3199 fqcodel_parse_qdisc_details__(netdev
, details
, &fqcodel
);
3200 error
= fqcodel_setup_qdisc__(netdev
, fqcodel
.target
, fqcodel
.limit
,
3201 fqcodel
.interval
, fqcodel
.flows
,
3204 fqcodel_install__(netdev
, fqcodel
.target
, fqcodel
.limit
,
3205 fqcodel
.interval
, fqcodel
.flows
, fqcodel
.quantum
);
3211 fqcodel_parse_tca_options__(struct nlattr
*nl_options
, struct fqcodel
*fqcodel
)
3213 static const struct nl_policy tca_fqcodel_policy
[] = {
3214 [TCA_FQ_CODEL_TARGET
] = { .type
= NL_A_U32
},
3215 [TCA_FQ_CODEL_LIMIT
] = { .type
= NL_A_U32
},
3216 [TCA_FQ_CODEL_INTERVAL
] = { .type
= NL_A_U32
},
3217 [TCA_FQ_CODEL_FLOWS
] = { .type
= NL_A_U32
},
3218 [TCA_FQ_CODEL_QUANTUM
] = { .type
= NL_A_U32
}
3221 struct nlattr
*attrs
[ARRAY_SIZE(tca_fqcodel_policy
)];
3223 if (!nl_parse_nested(nl_options
, tca_fqcodel_policy
,
3224 attrs
, ARRAY_SIZE(tca_fqcodel_policy
))) {
3225 VLOG_WARN_RL(&rl
, "failed to parse FQ_CoDel class options");
3229 fqcodel
->target
= nl_attr_get_u32(attrs
[TCA_FQ_CODEL_TARGET
]);
3230 fqcodel
->limit
= nl_attr_get_u32(attrs
[TCA_FQ_CODEL_LIMIT
]);
3231 fqcodel
->interval
=nl_attr_get_u32(attrs
[TCA_FQ_CODEL_INTERVAL
]);
3232 fqcodel
->flows
= nl_attr_get_u32(attrs
[TCA_FQ_CODEL_FLOWS
]);
3233 fqcodel
->quantum
= nl_attr_get_u32(attrs
[TCA_FQ_CODEL_QUANTUM
]);
3238 fqcodel_tc_load(struct netdev
*netdev
, struct ofpbuf
*nlmsg
)
3240 struct nlattr
*nlattr
;
3243 struct fqcodel fqcodel
;
3245 error
= tc_parse_qdisc(nlmsg
, &kind
, &nlattr
);
3250 error
= fqcodel_parse_tca_options__(nlattr
, &fqcodel
);
3255 fqcodel_install__(netdev
, fqcodel
.target
, fqcodel
.limit
, fqcodel
.interval
,
3256 fqcodel
.flows
, fqcodel
.quantum
);
3261 fqcodel_tc_destroy(struct tc
*tc
)
3263 struct fqcodel
*fqcodel
= CONTAINER_OF(tc
, struct fqcodel
, tc
);
3269 fqcodel_qdisc_get(const struct netdev
*netdev
, struct smap
*details
)
3271 const struct fqcodel
*fqcodel
= fqcodel_get__(netdev
);
3272 smap_add_format(details
, "target", "%u", fqcodel
->target
);
3273 smap_add_format(details
, "limit", "%u", fqcodel
->limit
);
3274 smap_add_format(details
, "interval", "%u", fqcodel
->interval
);
3275 smap_add_format(details
, "flows", "%u", fqcodel
->flows
);
3276 smap_add_format(details
, "quantum", "%u", fqcodel
->quantum
);
3281 fqcodel_qdisc_set(struct netdev
*netdev
, const struct smap
*details
)
3283 struct fqcodel fqcodel
;
3285 fqcodel_parse_qdisc_details__(netdev
, details
, &fqcodel
);
3286 fqcodel_install__(netdev
, fqcodel
.target
, fqcodel
.limit
, fqcodel
.interval
,
3287 fqcodel
.flows
, fqcodel
.quantum
);
3288 fqcodel_get__(netdev
)->target
= fqcodel
.target
;
3289 fqcodel_get__(netdev
)->limit
= fqcodel
.limit
;
3290 fqcodel_get__(netdev
)->interval
= fqcodel
.interval
;
3291 fqcodel_get__(netdev
)->flows
= fqcodel
.flows
;
3292 fqcodel_get__(netdev
)->quantum
= fqcodel
.quantum
;
3296 static const struct tc_ops tc_ops_fqcodel
= {
3297 "fq_codel", /* linux_name */
3298 "linux-fq_codel", /* ovs_name */
3299 FQCODEL_N_QUEUES
, /* n_queues */
3312 /* SFQ traffic control class. */
3314 #define SFQ_N_QUEUES 0x0000
3323 sfq_get__(const struct netdev
*netdev_
)
3325 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
3326 return CONTAINER_OF(netdev
->tc
, struct sfq
, tc
);
3330 sfq_install__(struct netdev
*netdev_
, uint32_t quantum
, uint32_t perturb
)
3332 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
3335 sfq
= xmalloc(sizeof *sfq
);
3336 tc_init(&sfq
->tc
, &tc_ops_sfq
);
3337 sfq
->perturb
= perturb
;
3338 sfq
->quantum
= quantum
;
3340 netdev
->tc
= &sfq
->tc
;
3344 sfq_setup_qdisc__(struct netdev
*netdev
, uint32_t quantum
, uint32_t perturb
)
3346 struct tc_sfq_qopt opt
;
3347 struct ofpbuf request
;
3348 struct tcmsg
*tcmsg
;
3350 int mtu_error
, error
;
3351 mtu_error
= netdev_linux_get_mtu__(netdev_linux_cast(netdev
), &mtu
);
3353 tc_del_qdisc(netdev
);
3355 tcmsg
= tc_make_request(netdev
, RTM_NEWQDISC
,
3356 NLM_F_EXCL
| NLM_F_CREATE
, &request
);
3360 tcmsg
->tcm_handle
= tc_make_handle(1, 0);
3361 tcmsg
->tcm_parent
= TC_H_ROOT
;
3363 memset(&opt
, 0, sizeof opt
);
3366 opt
.quantum
= mtu
; /* if we cannot find mtu, use default */
3369 opt
.quantum
= quantum
;
3373 opt
.perturb_period
= 10;
3375 opt
.perturb_period
= perturb
;
3378 nl_msg_put_string(&request
, TCA_KIND
, "sfq");
3379 nl_msg_put_unspec(&request
, TCA_OPTIONS
, &opt
, sizeof opt
);
3381 error
= tc_transact(&request
, NULL
);
3383 VLOG_WARN_RL(&rl
, "failed to replace %s qdisc, "
3384 "quantum %u, perturb %u error %d(%s)",
3385 netdev_get_name(netdev
),
3386 opt
.quantum
, opt
.perturb_period
,
3387 error
, ovs_strerror(error
));
3393 sfq_parse_qdisc_details__(struct netdev
*netdev
,
3394 const struct smap
*details
, struct sfq
*sfq
)
3396 sfq
->perturb
= smap_get_ullong(details
, "perturb", 0);
3397 sfq
->quantum
= smap_get_ullong(details
, "quantum", 0);
3399 if (!sfq
->perturb
) {
3403 if (!sfq
->quantum
) {
3405 if (!netdev_linux_get_mtu__(netdev_linux_cast(netdev
), &mtu
)) {
3408 VLOG_WARN_RL(&rl
, "when using SFQ, you must specify quantum on a "
3409 "device without mtu");
3415 sfq_tc_install(struct netdev
*netdev
, const struct smap
*details
)
3420 sfq_parse_qdisc_details__(netdev
, details
, &sfq
);
3421 error
= sfq_setup_qdisc__(netdev
, sfq
.quantum
, sfq
.perturb
);
3423 sfq_install__(netdev
, sfq
.quantum
, sfq
.perturb
);
3429 sfq_tc_load(struct netdev
*netdev
, struct ofpbuf
*nlmsg
)
3431 const struct tc_sfq_qopt
*sfq
;
3432 struct nlattr
*nlattr
;
3436 error
= tc_parse_qdisc(nlmsg
, &kind
, &nlattr
);
3438 sfq
= nl_attr_get(nlattr
);
3439 sfq_install__(netdev
, sfq
->perturb_period
, sfq
->quantum
);
3447 sfq_tc_destroy(struct tc
*tc
)
3449 struct sfq
*sfq
= CONTAINER_OF(tc
, struct sfq
, tc
);
3455 sfq_qdisc_get(const struct netdev
*netdev
, struct smap
*details
)
3457 const struct sfq
*sfq
= sfq_get__(netdev
);
3458 smap_add_format(details
, "quantum", "%u", sfq
->quantum
);
3459 smap_add_format(details
, "perturb", "%u", sfq
->perturb
);
3464 sfq_qdisc_set(struct netdev
*netdev
, const struct smap
*details
)
3468 sfq_parse_qdisc_details__(netdev
, details
, &sfq
);
3469 sfq_install__(netdev
, sfq
.quantum
, sfq
.perturb
);
3470 sfq_get__(netdev
)->quantum
= sfq
.quantum
;
3471 sfq_get__(netdev
)->perturb
= sfq
.perturb
;
3475 static const struct tc_ops tc_ops_sfq
= {
3476 "sfq", /* linux_name */
3477 "linux-sfq", /* ovs_name */
3478 SFQ_N_QUEUES
, /* n_queues */
3491 /* HTB traffic control class. */
3493 #define HTB_N_QUEUES 0xf000
3494 #define HTB_RATE2QUANTUM 10
3498 unsigned int max_rate
; /* In bytes/s. */
3502 struct tc_queue tc_queue
;
3503 unsigned int min_rate
; /* In bytes/s. */
3504 unsigned int max_rate
; /* In bytes/s. */
3505 unsigned int burst
; /* In bytes. */
3506 unsigned int priority
; /* Lower values are higher priorities. */
3510 htb_get__(const struct netdev
*netdev_
)
3512 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
3513 return CONTAINER_OF(netdev
->tc
, struct htb
, tc
);
3517 htb_install__(struct netdev
*netdev_
, uint64_t max_rate
)
3519 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
3522 htb
= xmalloc(sizeof *htb
);
3523 tc_init(&htb
->tc
, &tc_ops_htb
);
3524 htb
->max_rate
= max_rate
;
3526 netdev
->tc
= &htb
->tc
;
3529 /* Create an HTB qdisc.
3531 * Equivalent to "tc qdisc add dev <dev> root handle 1: htb default 1". */
3533 htb_setup_qdisc__(struct netdev
*netdev
)
3536 struct tc_htb_glob opt
;
3537 struct ofpbuf request
;
3538 struct tcmsg
*tcmsg
;
3540 tc_del_qdisc(netdev
);
3542 tcmsg
= tc_make_request(netdev
, RTM_NEWQDISC
,
3543 NLM_F_EXCL
| NLM_F_CREATE
, &request
);
3547 tcmsg
->tcm_handle
= tc_make_handle(1, 0);
3548 tcmsg
->tcm_parent
= TC_H_ROOT
;
3550 nl_msg_put_string(&request
, TCA_KIND
, "htb");
3552 memset(&opt
, 0, sizeof opt
);
3553 opt
.rate2quantum
= HTB_RATE2QUANTUM
;
3557 opt_offset
= nl_msg_start_nested(&request
, TCA_OPTIONS
);
3558 nl_msg_put_unspec(&request
, TCA_HTB_INIT
, &opt
, sizeof opt
);
3559 nl_msg_end_nested(&request
, opt_offset
);
3561 return tc_transact(&request
, NULL
);
3564 /* Equivalent to "tc class replace <dev> classid <handle> parent <parent> htb
3565 * rate <min_rate>bps ceil <max_rate>bps burst <burst>b prio <priority>". */
3567 htb_setup_class__(struct netdev
*netdev
, unsigned int handle
,
3568 unsigned int parent
, struct htb_class
*class)
3571 struct tc_htb_opt opt
;
3572 struct ofpbuf request
;
3573 struct tcmsg
*tcmsg
;
3577 error
= netdev_linux_get_mtu__(netdev_linux_cast(netdev
), &mtu
);
3579 VLOG_WARN_RL(&rl
, "cannot set up HTB on device %s that lacks MTU",
3580 netdev_get_name(netdev
));
3584 memset(&opt
, 0, sizeof opt
);
3585 tc_fill_rate(&opt
.rate
, class->min_rate
, mtu
);
3586 tc_fill_rate(&opt
.ceil
, class->max_rate
, mtu
);
3587 /* Makes sure the quantum is at least MTU. Setting quantum will
3588 * make htb ignore the r2q for this class. */
3589 if ((class->min_rate
/ HTB_RATE2QUANTUM
) < mtu
) {
3592 opt
.buffer
= tc_calc_buffer(opt
.rate
.rate
, mtu
, class->burst
);
3593 opt
.cbuffer
= tc_calc_buffer(opt
.ceil
.rate
, mtu
, class->burst
);
3594 opt
.prio
= class->priority
;
3596 tcmsg
= tc_make_request(netdev
, RTM_NEWTCLASS
, NLM_F_CREATE
, &request
);
3600 tcmsg
->tcm_handle
= handle
;
3601 tcmsg
->tcm_parent
= parent
;
3603 nl_msg_put_string(&request
, TCA_KIND
, "htb");
3604 opt_offset
= nl_msg_start_nested(&request
, TCA_OPTIONS
);
3605 nl_msg_put_unspec(&request
, TCA_HTB_PARMS
, &opt
, sizeof opt
);
3606 tc_put_rtab(&request
, TCA_HTB_RTAB
, &opt
.rate
);
3607 tc_put_rtab(&request
, TCA_HTB_CTAB
, &opt
.ceil
);
3608 nl_msg_end_nested(&request
, opt_offset
);
3610 error
= tc_transact(&request
, NULL
);
3612 VLOG_WARN_RL(&rl
, "failed to replace %s class %u:%u, parent %u:%u, "
3613 "min_rate=%u max_rate=%u burst=%u prio=%u (%s)",
3614 netdev_get_name(netdev
),
3615 tc_get_major(handle
), tc_get_minor(handle
),
3616 tc_get_major(parent
), tc_get_minor(parent
),
3617 class->min_rate
, class->max_rate
,
3618 class->burst
, class->priority
, ovs_strerror(error
));
3623 /* Parses Netlink attributes in 'options' for HTB parameters and stores a
3624 * description of them into 'details'. The description complies with the
3625 * specification given in the vswitch database documentation for linux-htb
3628 htb_parse_tca_options__(struct nlattr
*nl_options
, struct htb_class
*class)
3630 static const struct nl_policy tca_htb_policy
[] = {
3631 [TCA_HTB_PARMS
] = { .type
= NL_A_UNSPEC
, .optional
= false,
3632 .min_len
= sizeof(struct tc_htb_opt
) },
3635 struct nlattr
*attrs
[ARRAY_SIZE(tca_htb_policy
)];
3636 const struct tc_htb_opt
*htb
;
3638 if (!nl_parse_nested(nl_options
, tca_htb_policy
,
3639 attrs
, ARRAY_SIZE(tca_htb_policy
))) {
3640 VLOG_WARN_RL(&rl
, "failed to parse HTB class options");
3644 htb
= nl_attr_get(attrs
[TCA_HTB_PARMS
]);
3645 class->min_rate
= htb
->rate
.rate
;
3646 class->max_rate
= htb
->ceil
.rate
;
3647 class->burst
= tc_ticks_to_bytes(htb
->rate
.rate
, htb
->buffer
);
3648 class->priority
= htb
->prio
;
3653 htb_parse_tcmsg__(struct ofpbuf
*tcmsg
, unsigned int *queue_id
,
3654 struct htb_class
*options
,
3655 struct netdev_queue_stats
*stats
)
3657 struct nlattr
*nl_options
;
3658 unsigned int handle
;
3661 error
= tc_parse_class(tcmsg
, &handle
, &nl_options
, stats
);
3662 if (!error
&& queue_id
) {
3663 unsigned int major
= tc_get_major(handle
);
3664 unsigned int minor
= tc_get_minor(handle
);
3665 if (major
== 1 && minor
> 0 && minor
<= HTB_N_QUEUES
) {
3666 *queue_id
= minor
- 1;
3671 if (!error
&& options
) {
3672 error
= htb_parse_tca_options__(nl_options
, options
);
3678 htb_parse_qdisc_details__(struct netdev
*netdev_
,
3679 const struct smap
*details
, struct htb_class
*hc
)
3681 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
3683 hc
->max_rate
= smap_get_ullong(details
, "max-rate", 0) / 8;
3684 if (!hc
->max_rate
) {
3685 enum netdev_features current
;
3687 netdev_linux_read_features(netdev
);
3688 current
= !netdev
->get_features_error
? netdev
->current
: 0;
3689 hc
->max_rate
= netdev_features_to_bps(current
, 100 * 1000 * 1000) / 8;
3691 hc
->min_rate
= hc
->max_rate
;
3697 htb_parse_class_details__(struct netdev
*netdev
,
3698 const struct smap
*details
, struct htb_class
*hc
)
3700 const struct htb
*htb
= htb_get__(netdev
);
3703 error
= netdev_linux_get_mtu__(netdev_linux_cast(netdev
), &mtu
);
3705 VLOG_WARN_RL(&rl
, "cannot parse HTB class on device %s that lacks MTU",
3706 netdev_get_name(netdev
));
3710 /* HTB requires at least an mtu sized min-rate to send any traffic even
3711 * on uncongested links. */
3712 hc
->min_rate
= smap_get_ullong(details
, "min-rate", 0) / 8;
3713 hc
->min_rate
= MAX(hc
->min_rate
, mtu
);
3714 hc
->min_rate
= MIN(hc
->min_rate
, htb
->max_rate
);
3717 hc
->max_rate
= smap_get_ullong(details
, "max-rate", 0) / 8;
3718 if (!hc
->max_rate
) {
3719 hc
->max_rate
= htb
->max_rate
;
3721 hc
->max_rate
= MAX(hc
->max_rate
, hc
->min_rate
);
3722 hc
->max_rate
= MIN(hc
->max_rate
, htb
->max_rate
);
3726 * According to hints in the documentation that I've read, it is important
3727 * that 'burst' be at least as big as the largest frame that might be
3728 * transmitted. Also, making 'burst' a bit bigger than necessary is OK,
3729 * but having it a bit too small is a problem. Since netdev_get_mtu()
3730 * doesn't include the Ethernet header, we need to add at least 14 (18?) to
3731 * the MTU. We actually add 64, instead of 14, as a guard against
3732 * additional headers get tacked on somewhere that we're not aware of. */
3733 hc
->burst
= smap_get_ullong(details
, "burst", 0) / 8;
3734 hc
->burst
= MAX(hc
->burst
, mtu
+ 64);
3737 hc
->priority
= smap_get_ullong(details
, "priority", 0);
3743 htb_query_class__(const struct netdev
*netdev
, unsigned int handle
,
3744 unsigned int parent
, struct htb_class
*options
,
3745 struct netdev_queue_stats
*stats
)
3747 struct ofpbuf
*reply
;
3750 error
= tc_query_class(netdev
, handle
, parent
, &reply
);
3752 error
= htb_parse_tcmsg__(reply
, NULL
, options
, stats
);
3753 ofpbuf_delete(reply
);
3759 htb_tc_install(struct netdev
*netdev
, const struct smap
*details
)
3763 error
= htb_setup_qdisc__(netdev
);
3765 struct htb_class hc
;
3767 htb_parse_qdisc_details__(netdev
, details
, &hc
);
3768 error
= htb_setup_class__(netdev
, tc_make_handle(1, 0xfffe),
3769 tc_make_handle(1, 0), &hc
);
3771 htb_install__(netdev
, hc
.max_rate
);
3777 static struct htb_class
*
3778 htb_class_cast__(const struct tc_queue
*queue
)
3780 return CONTAINER_OF(queue
, struct htb_class
, tc_queue
);
3784 htb_update_queue__(struct netdev
*netdev
, unsigned int queue_id
,
3785 const struct htb_class
*hc
)
3787 struct htb
*htb
= htb_get__(netdev
);
3788 size_t hash
= hash_int(queue_id
, 0);
3789 struct tc_queue
*queue
;
3790 struct htb_class
*hcp
;
3792 queue
= tc_find_queue__(netdev
, queue_id
, hash
);
3794 hcp
= htb_class_cast__(queue
);
3796 hcp
= xmalloc(sizeof *hcp
);
3797 queue
= &hcp
->tc_queue
;
3798 queue
->queue_id
= queue_id
;
3799 queue
->created
= time_msec();
3800 hmap_insert(&htb
->tc
.queues
, &queue
->hmap_node
, hash
);
3803 hcp
->min_rate
= hc
->min_rate
;
3804 hcp
->max_rate
= hc
->max_rate
;
3805 hcp
->burst
= hc
->burst
;
3806 hcp
->priority
= hc
->priority
;
3810 htb_tc_load(struct netdev
*netdev
, struct ofpbuf
*nlmsg OVS_UNUSED
)
3813 struct queue_dump_state state
;
3814 struct htb_class hc
;
3816 /* Get qdisc options. */
3818 htb_query_class__(netdev
, tc_make_handle(1, 0xfffe), 0, &hc
, NULL
);
3819 htb_install__(netdev
, hc
.max_rate
);
3822 if (!start_queue_dump(netdev
, &state
)) {
3825 while (nl_dump_next(&state
.dump
, &msg
, &state
.buf
)) {
3826 unsigned int queue_id
;
3828 if (!htb_parse_tcmsg__(&msg
, &queue_id
, &hc
, NULL
)) {
3829 htb_update_queue__(netdev
, queue_id
, &hc
);
3832 finish_queue_dump(&state
);
3838 htb_tc_destroy(struct tc
*tc
)
3840 struct htb
*htb
= CONTAINER_OF(tc
, struct htb
, tc
);
3841 struct htb_class
*hc
;
3843 HMAP_FOR_EACH_POP (hc
, tc_queue
.hmap_node
, &htb
->tc
.queues
) {
3851 htb_qdisc_get(const struct netdev
*netdev
, struct smap
*details
)
3853 const struct htb
*htb
= htb_get__(netdev
);
3854 smap_add_format(details
, "max-rate", "%llu", 8ULL * htb
->max_rate
);
3859 htb_qdisc_set(struct netdev
*netdev
, const struct smap
*details
)
3861 struct htb_class hc
;
3864 htb_parse_qdisc_details__(netdev
, details
, &hc
);
3865 error
= htb_setup_class__(netdev
, tc_make_handle(1, 0xfffe),
3866 tc_make_handle(1, 0), &hc
);
3868 htb_get__(netdev
)->max_rate
= hc
.max_rate
;
3874 htb_class_get(const struct netdev
*netdev OVS_UNUSED
,
3875 const struct tc_queue
*queue
, struct smap
*details
)
3877 const struct htb_class
*hc
= htb_class_cast__(queue
);
3879 smap_add_format(details
, "min-rate", "%llu", 8ULL * hc
->min_rate
);
3880 if (hc
->min_rate
!= hc
->max_rate
) {
3881 smap_add_format(details
, "max-rate", "%llu", 8ULL * hc
->max_rate
);
3883 smap_add_format(details
, "burst", "%llu", 8ULL * hc
->burst
);
3885 smap_add_format(details
, "priority", "%u", hc
->priority
);
3891 htb_class_set(struct netdev
*netdev
, unsigned int queue_id
,
3892 const struct smap
*details
)
3894 struct htb_class hc
;
3897 error
= htb_parse_class_details__(netdev
, details
, &hc
);
3902 error
= htb_setup_class__(netdev
, tc_make_handle(1, queue_id
+ 1),
3903 tc_make_handle(1, 0xfffe), &hc
);
3908 htb_update_queue__(netdev
, queue_id
, &hc
);
3913 htb_class_delete(struct netdev
*netdev
, struct tc_queue
*queue
)
3915 struct htb_class
*hc
= htb_class_cast__(queue
);
3916 struct htb
*htb
= htb_get__(netdev
);
3919 error
= tc_delete_class(netdev
, tc_make_handle(1, queue
->queue_id
+ 1));
3921 hmap_remove(&htb
->tc
.queues
, &hc
->tc_queue
.hmap_node
);
3928 htb_class_get_stats(const struct netdev
*netdev
, const struct tc_queue
*queue
,
3929 struct netdev_queue_stats
*stats
)
3931 return htb_query_class__(netdev
, tc_make_handle(1, queue
->queue_id
+ 1),
3932 tc_make_handle(1, 0xfffe), NULL
, stats
);
3936 htb_class_dump_stats(const struct netdev
*netdev OVS_UNUSED
,
3937 const struct ofpbuf
*nlmsg
,
3938 netdev_dump_queue_stats_cb
*cb
, void *aux
)
3940 struct netdev_queue_stats stats
;
3941 unsigned int handle
, major
, minor
;
3944 error
= tc_parse_class(nlmsg
, &handle
, NULL
, &stats
);
3949 major
= tc_get_major(handle
);
3950 minor
= tc_get_minor(handle
);
3951 if (major
== 1 && minor
> 0 && minor
<= HTB_N_QUEUES
) {
3952 (*cb
)(minor
- 1, &stats
, aux
);
3957 static const struct tc_ops tc_ops_htb
= {
3958 "htb", /* linux_name */
3959 "linux-htb", /* ovs_name */
3960 HTB_N_QUEUES
, /* n_queues */
3969 htb_class_get_stats
,
3970 htb_class_dump_stats
3973 /* "linux-hfsc" traffic control class. */
3975 #define HFSC_N_QUEUES 0xf000
3983 struct tc_queue tc_queue
;
3988 static struct hfsc
*
3989 hfsc_get__(const struct netdev
*netdev_
)
3991 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
3992 return CONTAINER_OF(netdev
->tc
, struct hfsc
, tc
);
3995 static struct hfsc_class
*
3996 hfsc_class_cast__(const struct tc_queue
*queue
)
3998 return CONTAINER_OF(queue
, struct hfsc_class
, tc_queue
);
4002 hfsc_install__(struct netdev
*netdev_
, uint32_t max_rate
)
4004 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
4007 hfsc
= xmalloc(sizeof *hfsc
);
4008 tc_init(&hfsc
->tc
, &tc_ops_hfsc
);
4009 hfsc
->max_rate
= max_rate
;
4010 netdev
->tc
= &hfsc
->tc
;
4014 hfsc_update_queue__(struct netdev
*netdev
, unsigned int queue_id
,
4015 const struct hfsc_class
*hc
)
4019 struct hfsc_class
*hcp
;
4020 struct tc_queue
*queue
;
4022 hfsc
= hfsc_get__(netdev
);
4023 hash
= hash_int(queue_id
, 0);
4025 queue
= tc_find_queue__(netdev
, queue_id
, hash
);
4027 hcp
= hfsc_class_cast__(queue
);
4029 hcp
= xmalloc(sizeof *hcp
);
4030 queue
= &hcp
->tc_queue
;
4031 queue
->queue_id
= queue_id
;
4032 queue
->created
= time_msec();
4033 hmap_insert(&hfsc
->tc
.queues
, &queue
->hmap_node
, hash
);
4036 hcp
->min_rate
= hc
->min_rate
;
4037 hcp
->max_rate
= hc
->max_rate
;
4041 hfsc_parse_tca_options__(struct nlattr
*nl_options
, struct hfsc_class
*class)
4043 const struct tc_service_curve
*rsc
, *fsc
, *usc
;
4044 static const struct nl_policy tca_hfsc_policy
[] = {
4046 .type
= NL_A_UNSPEC
,
4048 .min_len
= sizeof(struct tc_service_curve
),
4051 .type
= NL_A_UNSPEC
,
4053 .min_len
= sizeof(struct tc_service_curve
),
4056 .type
= NL_A_UNSPEC
,
4058 .min_len
= sizeof(struct tc_service_curve
),
4061 struct nlattr
*attrs
[ARRAY_SIZE(tca_hfsc_policy
)];
4063 if (!nl_parse_nested(nl_options
, tca_hfsc_policy
,
4064 attrs
, ARRAY_SIZE(tca_hfsc_policy
))) {
4065 VLOG_WARN_RL(&rl
, "failed to parse HFSC class options");
4069 rsc
= nl_attr_get(attrs
[TCA_HFSC_RSC
]);
4070 fsc
= nl_attr_get(attrs
[TCA_HFSC_FSC
]);
4071 usc
= nl_attr_get(attrs
[TCA_HFSC_USC
]);
4073 if (rsc
->m1
!= 0 || rsc
->d
!= 0 ||
4074 fsc
->m1
!= 0 || fsc
->d
!= 0 ||
4075 usc
->m1
!= 0 || usc
->d
!= 0) {
4076 VLOG_WARN_RL(&rl
, "failed to parse HFSC class options. "
4077 "Non-linear service curves are not supported.");
4081 if (rsc
->m2
!= fsc
->m2
) {
4082 VLOG_WARN_RL(&rl
, "failed to parse HFSC class options. "
4083 "Real-time service curves are not supported ");
4087 if (rsc
->m2
> usc
->m2
) {
4088 VLOG_WARN_RL(&rl
, "failed to parse HFSC class options. "
4089 "Min-rate service curve is greater than "
4090 "the max-rate service curve.");
4094 class->min_rate
= fsc
->m2
;
4095 class->max_rate
= usc
->m2
;
4100 hfsc_parse_tcmsg__(struct ofpbuf
*tcmsg
, unsigned int *queue_id
,
4101 struct hfsc_class
*options
,
4102 struct netdev_queue_stats
*stats
)
4105 unsigned int handle
;
4106 struct nlattr
*nl_options
;
4108 error
= tc_parse_class(tcmsg
, &handle
, &nl_options
, stats
);
4114 unsigned int major
, minor
;
4116 major
= tc_get_major(handle
);
4117 minor
= tc_get_minor(handle
);
4118 if (major
== 1 && minor
> 0 && minor
<= HFSC_N_QUEUES
) {
4119 *queue_id
= minor
- 1;
4126 error
= hfsc_parse_tca_options__(nl_options
, options
);
4133 hfsc_query_class__(const struct netdev
*netdev
, unsigned int handle
,
4134 unsigned int parent
, struct hfsc_class
*options
,
4135 struct netdev_queue_stats
*stats
)
4138 struct ofpbuf
*reply
;
4140 error
= tc_query_class(netdev
, handle
, parent
, &reply
);
4145 error
= hfsc_parse_tcmsg__(reply
, NULL
, options
, stats
);
4146 ofpbuf_delete(reply
);
4151 hfsc_parse_qdisc_details__(struct netdev
*netdev_
, const struct smap
*details
,
4152 struct hfsc_class
*class)
4154 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
4156 uint32_t max_rate
= smap_get_ullong(details
, "max-rate", 0) / 8;
4158 enum netdev_features current
;
4160 netdev_linux_read_features(netdev
);
4161 current
= !netdev
->get_features_error
? netdev
->current
: 0;
4162 max_rate
= netdev_features_to_bps(current
, 100 * 1000 * 1000) / 8;
4165 class->min_rate
= max_rate
;
4166 class->max_rate
= max_rate
;
4170 hfsc_parse_class_details__(struct netdev
*netdev
,
4171 const struct smap
*details
,
4172 struct hfsc_class
* class)
4174 const struct hfsc
*hfsc
;
4175 uint32_t min_rate
, max_rate
;
4177 hfsc
= hfsc_get__(netdev
);
4179 min_rate
= smap_get_ullong(details
, "min-rate", 0) / 8;
4180 min_rate
= MAX(min_rate
, 1);
4181 min_rate
= MIN(min_rate
, hfsc
->max_rate
);
4183 max_rate
= smap_get_ullong(details
, "max-rate", hfsc
->max_rate
* 8) / 8;
4184 max_rate
= MAX(max_rate
, min_rate
);
4185 max_rate
= MIN(max_rate
, hfsc
->max_rate
);
4187 class->min_rate
= min_rate
;
4188 class->max_rate
= max_rate
;
4193 /* Create an HFSC qdisc.
4195 * Equivalent to "tc qdisc add dev <dev> root handle 1: hfsc default 1". */
4197 hfsc_setup_qdisc__(struct netdev
* netdev
)
4199 struct tcmsg
*tcmsg
;
4200 struct ofpbuf request
;
4201 struct tc_hfsc_qopt opt
;
4203 tc_del_qdisc(netdev
);
4205 tcmsg
= tc_make_request(netdev
, RTM_NEWQDISC
,
4206 NLM_F_EXCL
| NLM_F_CREATE
, &request
);
4212 tcmsg
->tcm_handle
= tc_make_handle(1, 0);
4213 tcmsg
->tcm_parent
= TC_H_ROOT
;
4215 memset(&opt
, 0, sizeof opt
);
4218 nl_msg_put_string(&request
, TCA_KIND
, "hfsc");
4219 nl_msg_put_unspec(&request
, TCA_OPTIONS
, &opt
, sizeof opt
);
4221 return tc_transact(&request
, NULL
);
4224 /* Create an HFSC class.
4226 * Equivalent to "tc class add <dev> parent <parent> classid <handle> hfsc
4227 * sc rate <min_rate> ul rate <max_rate>" */
4229 hfsc_setup_class__(struct netdev
*netdev
, unsigned int handle
,
4230 unsigned int parent
, struct hfsc_class
*class)
4234 struct tcmsg
*tcmsg
;
4235 struct ofpbuf request
;
4236 struct tc_service_curve min
, max
;
4238 tcmsg
= tc_make_request(netdev
, RTM_NEWTCLASS
, NLM_F_CREATE
, &request
);
4244 tcmsg
->tcm_handle
= handle
;
4245 tcmsg
->tcm_parent
= parent
;
4249 min
.m2
= class->min_rate
;
4253 max
.m2
= class->max_rate
;
4255 nl_msg_put_string(&request
, TCA_KIND
, "hfsc");
4256 opt_offset
= nl_msg_start_nested(&request
, TCA_OPTIONS
);
4257 nl_msg_put_unspec(&request
, TCA_HFSC_RSC
, &min
, sizeof min
);
4258 nl_msg_put_unspec(&request
, TCA_HFSC_FSC
, &min
, sizeof min
);
4259 nl_msg_put_unspec(&request
, TCA_HFSC_USC
, &max
, sizeof max
);
4260 nl_msg_end_nested(&request
, opt_offset
);
4262 error
= tc_transact(&request
, NULL
);
4264 VLOG_WARN_RL(&rl
, "failed to replace %s class %u:%u, parent %u:%u, "
4265 "min-rate %ubps, max-rate %ubps (%s)",
4266 netdev_get_name(netdev
),
4267 tc_get_major(handle
), tc_get_minor(handle
),
4268 tc_get_major(parent
), tc_get_minor(parent
),
4269 class->min_rate
, class->max_rate
, ovs_strerror(error
));
4276 hfsc_tc_install(struct netdev
*netdev
, const struct smap
*details
)
4279 struct hfsc_class
class;
4281 error
= hfsc_setup_qdisc__(netdev
);
4287 hfsc_parse_qdisc_details__(netdev
, details
, &class);
4288 error
= hfsc_setup_class__(netdev
, tc_make_handle(1, 0xfffe),
4289 tc_make_handle(1, 0), &class);
4295 hfsc_install__(netdev
, class.max_rate
);
4300 hfsc_tc_load(struct netdev
*netdev
, struct ofpbuf
*nlmsg OVS_UNUSED
)
4303 struct queue_dump_state state
;
4304 struct hfsc_class hc
;
4307 hfsc_query_class__(netdev
, tc_make_handle(1, 0xfffe), 0, &hc
, NULL
);
4308 hfsc_install__(netdev
, hc
.max_rate
);
4310 if (!start_queue_dump(netdev
, &state
)) {
4314 while (nl_dump_next(&state
.dump
, &msg
, &state
.buf
)) {
4315 unsigned int queue_id
;
4317 if (!hfsc_parse_tcmsg__(&msg
, &queue_id
, &hc
, NULL
)) {
4318 hfsc_update_queue__(netdev
, queue_id
, &hc
);
4322 finish_queue_dump(&state
);
4327 hfsc_tc_destroy(struct tc
*tc
)
4330 struct hfsc_class
*hc
, *next
;
4332 hfsc
= CONTAINER_OF(tc
, struct hfsc
, tc
);
4334 HMAP_FOR_EACH_SAFE (hc
, next
, tc_queue
.hmap_node
, &hfsc
->tc
.queues
) {
4335 hmap_remove(&hfsc
->tc
.queues
, &hc
->tc_queue
.hmap_node
);
4344 hfsc_qdisc_get(const struct netdev
*netdev
, struct smap
*details
)
4346 const struct hfsc
*hfsc
;
4347 hfsc
= hfsc_get__(netdev
);
4348 smap_add_format(details
, "max-rate", "%llu", 8ULL * hfsc
->max_rate
);
4353 hfsc_qdisc_set(struct netdev
*netdev
, const struct smap
*details
)
4356 struct hfsc_class
class;
4358 hfsc_parse_qdisc_details__(netdev
, details
, &class);
4359 error
= hfsc_setup_class__(netdev
, tc_make_handle(1, 0xfffe),
4360 tc_make_handle(1, 0), &class);
4363 hfsc_get__(netdev
)->max_rate
= class.max_rate
;
4370 hfsc_class_get(const struct netdev
*netdev OVS_UNUSED
,
4371 const struct tc_queue
*queue
, struct smap
*details
)
4373 const struct hfsc_class
*hc
;
4375 hc
= hfsc_class_cast__(queue
);
4376 smap_add_format(details
, "min-rate", "%llu", 8ULL * hc
->min_rate
);
4377 if (hc
->min_rate
!= hc
->max_rate
) {
4378 smap_add_format(details
, "max-rate", "%llu", 8ULL * hc
->max_rate
);
4384 hfsc_class_set(struct netdev
*netdev
, unsigned int queue_id
,
4385 const struct smap
*details
)
4388 struct hfsc_class
class;
4390 error
= hfsc_parse_class_details__(netdev
, details
, &class);
4395 error
= hfsc_setup_class__(netdev
, tc_make_handle(1, queue_id
+ 1),
4396 tc_make_handle(1, 0xfffe), &class);
4401 hfsc_update_queue__(netdev
, queue_id
, &class);
4406 hfsc_class_delete(struct netdev
*netdev
, struct tc_queue
*queue
)
4410 struct hfsc_class
*hc
;
4412 hc
= hfsc_class_cast__(queue
);
4413 hfsc
= hfsc_get__(netdev
);
4415 error
= tc_delete_class(netdev
, tc_make_handle(1, queue
->queue_id
+ 1));
4417 hmap_remove(&hfsc
->tc
.queues
, &hc
->tc_queue
.hmap_node
);
4424 hfsc_class_get_stats(const struct netdev
*netdev
, const struct tc_queue
*queue
,
4425 struct netdev_queue_stats
*stats
)
4427 return hfsc_query_class__(netdev
, tc_make_handle(1, queue
->queue_id
+ 1),
4428 tc_make_handle(1, 0xfffe), NULL
, stats
);
4432 hfsc_class_dump_stats(const struct netdev
*netdev OVS_UNUSED
,
4433 const struct ofpbuf
*nlmsg
,
4434 netdev_dump_queue_stats_cb
*cb
, void *aux
)
4436 struct netdev_queue_stats stats
;
4437 unsigned int handle
, major
, minor
;
4440 error
= tc_parse_class(nlmsg
, &handle
, NULL
, &stats
);
4445 major
= tc_get_major(handle
);
4446 minor
= tc_get_minor(handle
);
4447 if (major
== 1 && minor
> 0 && minor
<= HFSC_N_QUEUES
) {
4448 (*cb
)(minor
- 1, &stats
, aux
);
4453 static const struct tc_ops tc_ops_hfsc
= {
4454 "hfsc", /* linux_name */
4455 "linux-hfsc", /* ovs_name */
4456 HFSC_N_QUEUES
, /* n_queues */
4457 hfsc_tc_install
, /* tc_install */
4458 hfsc_tc_load
, /* tc_load */
4459 hfsc_tc_destroy
, /* tc_destroy */
4460 hfsc_qdisc_get
, /* qdisc_get */
4461 hfsc_qdisc_set
, /* qdisc_set */
4462 hfsc_class_get
, /* class_get */
4463 hfsc_class_set
, /* class_set */
4464 hfsc_class_delete
, /* class_delete */
4465 hfsc_class_get_stats
, /* class_get_stats */
4466 hfsc_class_dump_stats
/* class_dump_stats */
4469 /* "linux-noop" traffic control class. */
4472 noop_install__(struct netdev
*netdev_
)
4474 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
4475 static const struct tc tc
= TC_INITIALIZER(&tc
, &tc_ops_default
);
4477 netdev
->tc
= CONST_CAST(struct tc
*, &tc
);
4481 noop_tc_install(struct netdev
*netdev
,
4482 const struct smap
*details OVS_UNUSED
)
4484 noop_install__(netdev
);
4489 noop_tc_load(struct netdev
*netdev
, struct ofpbuf
*nlmsg OVS_UNUSED
)
4491 noop_install__(netdev
);
4495 static const struct tc_ops tc_ops_noop
= {
4496 NULL
, /* linux_name */
4497 "linux-noop", /* ovs_name */
4501 NULL
, /* tc_destroy */
4502 NULL
, /* qdisc_get */
4503 NULL
, /* qdisc_set */
4504 NULL
, /* class_get */
4505 NULL
, /* class_set */
4506 NULL
, /* class_delete */
4507 NULL
, /* class_get_stats */
4508 NULL
/* class_dump_stats */
4511 /* "linux-default" traffic control class.
4513 * This class represents the default, unnamed Linux qdisc. It corresponds to
4514 * the "" (empty string) QoS type in the OVS database. */
4517 default_install__(struct netdev
*netdev_
)
4519 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
4520 static const struct tc tc
= TC_INITIALIZER(&tc
, &tc_ops_default
);
4522 /* Nothing but a tc class implementation is allowed to write to a tc. This
4523 * class never does that, so we can legitimately use a const tc object. */
4524 netdev
->tc
= CONST_CAST(struct tc
*, &tc
);
4528 default_tc_install(struct netdev
*netdev
,
4529 const struct smap
*details OVS_UNUSED
)
4531 default_install__(netdev
);
4536 default_tc_load(struct netdev
*netdev
, struct ofpbuf
*nlmsg OVS_UNUSED
)
4538 default_install__(netdev
);
4542 static const struct tc_ops tc_ops_default
= {
4543 NULL
, /* linux_name */
4548 NULL
, /* tc_destroy */
4549 NULL
, /* qdisc_get */
4550 NULL
, /* qdisc_set */
4551 NULL
, /* class_get */
4552 NULL
, /* class_set */
4553 NULL
, /* class_delete */
4554 NULL
, /* class_get_stats */
4555 NULL
/* class_dump_stats */
4558 /* "linux-other" traffic control class.
4563 other_tc_load(struct netdev
*netdev_
, struct ofpbuf
*nlmsg OVS_UNUSED
)
4565 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
4566 static const struct tc tc
= TC_INITIALIZER(&tc
, &tc_ops_other
);
4568 /* Nothing but a tc class implementation is allowed to write to a tc. This
4569 * class never does that, so we can legitimately use a const tc object. */
4570 netdev
->tc
= CONST_CAST(struct tc
*, &tc
);
4574 static const struct tc_ops tc_ops_other
= {
4575 NULL
, /* linux_name */
4576 "linux-other", /* ovs_name */
4578 NULL
, /* tc_install */
4580 NULL
, /* tc_destroy */
4581 NULL
, /* qdisc_get */
4582 NULL
, /* qdisc_set */
4583 NULL
, /* class_get */
4584 NULL
, /* class_set */
4585 NULL
, /* class_delete */
4586 NULL
, /* class_get_stats */
4587 NULL
/* class_dump_stats */
4590 /* Traffic control. */
4592 /* Number of kernel "tc" ticks per second. */
4593 static double ticks_per_s
;
4595 /* Number of kernel "jiffies" per second. This is used for the purpose of
4596 * computing buffer sizes. Generally kernel qdiscs need to be able to buffer
4597 * one jiffy's worth of data.
4599 * There are two possibilities here:
4601 * - 'buffer_hz' is the kernel's real timer tick rate, a small number in the
4602 * approximate range of 100 to 1024. That means that we really need to
4603 * make sure that the qdisc can buffer that much data.
4605 * - 'buffer_hz' is an absurdly large number. That means that the kernel
4606 * has finely granular timers and there's no need to fudge additional room
4607 * for buffers. (There's no extra effort needed to implement that: the
4608 * large 'buffer_hz' is used as a divisor, so practically any number will
4609 * come out as 0 in the division. Small integer results in the case of
4610 * really high dividends won't have any real effect anyhow.)
4612 static unsigned int buffer_hz
;
4614 /* Returns tc handle 'major':'minor'. */
4616 tc_make_handle(unsigned int major
, unsigned int minor
)
4618 return TC_H_MAKE(major
<< 16, minor
);
4621 /* Returns the major number from 'handle'. */
4623 tc_get_major(unsigned int handle
)
4625 return TC_H_MAJ(handle
) >> 16;
4628 /* Returns the minor number from 'handle'. */
4630 tc_get_minor(unsigned int handle
)
4632 return TC_H_MIN(handle
);
4635 static struct tcmsg
*
4636 tc_make_request(const struct netdev
*netdev
, int type
, unsigned int flags
,
4637 struct ofpbuf
*request
)
4639 struct tcmsg
*tcmsg
;
4643 error
= get_ifindex(netdev
, &ifindex
);
4648 ofpbuf_init(request
, 512);
4649 nl_msg_put_nlmsghdr(request
, sizeof *tcmsg
, type
, NLM_F_REQUEST
| flags
);
4650 tcmsg
= ofpbuf_put_zeros(request
, sizeof *tcmsg
);
4651 tcmsg
->tcm_family
= AF_UNSPEC
;
4652 tcmsg
->tcm_ifindex
= ifindex
;
4653 /* Caller should fill in tcmsg->tcm_handle. */
4654 /* Caller should fill in tcmsg->tcm_parent. */
4660 tc_transact(struct ofpbuf
*request
, struct ofpbuf
**replyp
)
4662 int error
= nl_transact(NETLINK_ROUTE
, request
, replyp
);
4663 ofpbuf_uninit(request
);
4667 /* Adds or deletes a root ingress qdisc on 'netdev'. We use this for
4668 * policing configuration.
4670 * This function is equivalent to running the following when 'add' is true:
4671 * /sbin/tc qdisc add dev <devname> handle ffff: ingress
4673 * This function is equivalent to running the following when 'add' is false:
4674 * /sbin/tc qdisc del dev <devname> handle ffff: ingress
4676 * The configuration and stats may be seen with the following command:
4677 * /sbin/tc -s qdisc show dev <devname>
4679 * Returns 0 if successful, otherwise a positive errno value.
4682 tc_add_del_ingress_qdisc(struct netdev
*netdev
, bool add
)
4684 struct ofpbuf request
;
4685 struct tcmsg
*tcmsg
;
4687 int type
= add
? RTM_NEWQDISC
: RTM_DELQDISC
;
4688 int flags
= add
? NLM_F_EXCL
| NLM_F_CREATE
: 0;
4690 tcmsg
= tc_make_request(netdev
, type
, flags
, &request
);
4694 tcmsg
->tcm_handle
= tc_make_handle(0xffff, 0);
4695 tcmsg
->tcm_parent
= TC_H_INGRESS
;
4696 nl_msg_put_string(&request
, TCA_KIND
, "ingress");
4697 nl_msg_put_unspec(&request
, TCA_OPTIONS
, NULL
, 0);
4699 error
= tc_transact(&request
, NULL
);
4701 /* If we're deleting the qdisc, don't worry about some of the
4702 * error conditions. */
4703 if (!add
&& (error
== ENOENT
|| error
== EINVAL
)) {
4712 /* Adds a policer to 'netdev' with a rate of 'kbits_rate' and a burst size
4715 * This function is equivalent to running:
4716 * /sbin/tc filter add dev <devname> parent ffff: protocol all prio 49
4717 * basic police rate <kbits_rate>kbit burst <kbits_burst>k
4720 * The configuration and stats may be seen with the following command:
4721 * /sbin/tc -s filter show dev <devname> parent ffff:
4723 * Returns 0 if successful, otherwise a positive errno value.
4726 tc_add_policer(struct netdev
*netdev
,
4727 uint32_t kbits_rate
, uint32_t kbits_burst
)
4729 struct tc_police tc_police
;
4730 struct ofpbuf request
;
4731 struct tcmsg
*tcmsg
;
4732 size_t basic_offset
;
4733 size_t police_offset
;
4737 memset(&tc_police
, 0, sizeof tc_police
);
4738 tc_police
.action
= TC_POLICE_SHOT
;
4739 tc_police
.mtu
= mtu
;
4740 tc_fill_rate(&tc_police
.rate
, ((uint64_t) kbits_rate
* 1000)/8, mtu
);
4742 /* The following appears wrong in one way: In networking a kilobit is
4743 * usually 1000 bits but this uses 1024 bits.
4745 * However if you "fix" those problems then "tc filter show ..." shows
4746 * "125000b", meaning 125,000 bits, when OVS configures it for 1000 kbit ==
4747 * 1,000,000 bits, whereas this actually ends up doing the right thing from
4748 * tc's point of view. Whatever. */
4749 tc_police
.burst
= tc_bytes_to_ticks(
4750 tc_police
.rate
.rate
, MIN(UINT32_MAX
/ 1024, kbits_burst
) * 1024 / 8);
4752 tcmsg
= tc_make_request(netdev
, RTM_NEWTFILTER
,
4753 NLM_F_EXCL
| NLM_F_CREATE
, &request
);
4757 tcmsg
->tcm_parent
= tc_make_handle(0xffff, 0);
4758 tcmsg
->tcm_info
= tc_make_handle(49,
4759 (OVS_FORCE
uint16_t) htons(ETH_P_ALL
));
4761 nl_msg_put_string(&request
, TCA_KIND
, "basic");
4762 basic_offset
= nl_msg_start_nested(&request
, TCA_OPTIONS
);
4763 police_offset
= nl_msg_start_nested(&request
, TCA_BASIC_POLICE
);
4764 nl_msg_put_unspec(&request
, TCA_POLICE_TBF
, &tc_police
, sizeof tc_police
);
4765 tc_put_rtab(&request
, TCA_POLICE_RATE
, &tc_police
.rate
);
4766 nl_msg_end_nested(&request
, police_offset
);
4767 nl_msg_end_nested(&request
, basic_offset
);
4769 error
= tc_transact(&request
, NULL
);
4780 /* The values in psched are not individually very meaningful, but they are
4781 * important. The tables below show some values seen in the wild.
4785 * - "c" has always been a constant 1000000 since at least Linux 2.4.14.
4786 * (Before that, there are hints that it was 1000000000.)
4788 * - "d" can be unrealistically large, see the comment on 'buffer_hz'
4792 * -----------------------------------
4793 * [1] 000c8000 000f4240 000f4240 00000064
4794 * [2] 000003e8 00000400 000f4240 3b9aca00
4795 * [3] 000003e8 00000400 000f4240 3b9aca00
4796 * [4] 000003e8 00000400 000f4240 00000064
4797 * [5] 000003e8 00000040 000f4240 3b9aca00
4798 * [6] 000003e8 00000040 000f4240 000000f9
4800 * a b c d ticks_per_s buffer_hz
4801 * ------- --------- ---------- ------------- ----------- -------------
4802 * [1] 819,200 1,000,000 1,000,000 100 819,200 100
4803 * [2] 1,000 1,024 1,000,000 1,000,000,000 976,562 1,000,000,000
4804 * [3] 1,000 1,024 1,000,000 1,000,000,000 976,562 1,000,000,000
4805 * [4] 1,000 1,024 1,000,000 100 976,562 100
4806 * [5] 1,000 64 1,000,000 1,000,000,000 15,625,000 1,000,000,000
4807 * [6] 1,000 64 1,000,000 249 15,625,000 249
4809 * [1] 2.6.18-128.1.6.el5.xs5.5.0.505.1024xen from XenServer 5.5.0-24648p
4810 * [2] 2.6.26-1-686-bigmem from Debian lenny
4811 * [3] 2.6.26-2-sparc64 from Debian lenny
4812 * [4] 2.6.27.42-0.1.1.xs5.6.810.44.111163xen from XenServer 5.6.810-31078p
4813 * [5] 2.6.32.21.22 (approx.) from Ubuntu 10.04 on VMware Fusion
4814 * [6] 2.6.34 from kernel.org on KVM
4816 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
4817 static const char fn
[] = "/proc/net/psched";
4818 unsigned int a
, b
, c
, d
;
4821 if (!ovsthread_once_start(&once
)) {
4828 stream
= fopen(fn
, "r");
4830 VLOG_WARN("%s: open failed: %s", fn
, ovs_strerror(errno
));
4834 if (fscanf(stream
, "%x %x %x %x", &a
, &b
, &c
, &d
) != 4) {
4835 VLOG_WARN("%s: read failed", fn
);
4839 VLOG_DBG("%s: psched parameters are: %u %u %u %u", fn
, a
, b
, c
, d
);
4843 VLOG_WARN("%s: invalid scheduler parameters", fn
);
4847 ticks_per_s
= (double) a
* c
/ b
;
4851 VLOG_WARN("%s: unexpected psched parameters: %u %u %u %u",
4854 VLOG_DBG("%s: ticks_per_s=%f buffer_hz=%u", fn
, ticks_per_s
, buffer_hz
);
4857 ovsthread_once_done(&once
);
4860 /* Returns the number of bytes that can be transmitted in 'ticks' ticks at a
4861 * rate of 'rate' bytes per second. */
4863 tc_ticks_to_bytes(unsigned int rate
, unsigned int ticks
)
4866 return (rate
* ticks
) / ticks_per_s
;
4869 /* Returns the number of ticks that it would take to transmit 'size' bytes at a
4870 * rate of 'rate' bytes per second. */
4872 tc_bytes_to_ticks(unsigned int rate
, unsigned int size
)
4875 return rate
? ((unsigned long long int) ticks_per_s
* size
) / rate
: 0;
4878 /* Returns the number of bytes that need to be reserved for qdisc buffering at
4879 * a transmission rate of 'rate' bytes per second. */
4881 tc_buffer_per_jiffy(unsigned int rate
)
4884 return rate
/ buffer_hz
;
4887 /* Given Netlink 'msg' that describes a qdisc, extracts the name of the qdisc,
4888 * e.g. "htb", into '*kind' (if it is nonnull). If 'options' is nonnull,
4889 * extracts 'msg''s TCA_OPTIONS attributes into '*options' if it is present or
4890 * stores NULL into it if it is absent.
4892 * '*kind' and '*options' point into 'msg', so they are owned by whoever owns
4895 * Returns 0 if successful, otherwise a positive errno value. */
4897 tc_parse_qdisc(const struct ofpbuf
*msg
, const char **kind
,
4898 struct nlattr
**options
)
4900 static const struct nl_policy tca_policy
[] = {
4901 [TCA_KIND
] = { .type
= NL_A_STRING
, .optional
= false },
4902 [TCA_OPTIONS
] = { .type
= NL_A_NESTED
, .optional
= true },
4904 struct nlattr
*ta
[ARRAY_SIZE(tca_policy
)];
4906 if (!nl_policy_parse(msg
, NLMSG_HDRLEN
+ sizeof(struct tcmsg
),
4907 tca_policy
, ta
, ARRAY_SIZE(ta
))) {
4908 VLOG_WARN_RL(&rl
, "failed to parse qdisc message");
4913 *kind
= nl_attr_get_string(ta
[TCA_KIND
]);
4917 *options
= ta
[TCA_OPTIONS
];
4932 /* Given Netlink 'msg' that describes a class, extracts the queue ID (e.g. the
4933 * minor number of its class ID) into '*queue_id', its TCA_OPTIONS attribute
4934 * into '*options', and its queue statistics into '*stats'. Any of the output
4935 * arguments may be null.
4937 * Returns 0 if successful, otherwise a positive errno value. */
4939 tc_parse_class(const struct ofpbuf
*msg
, unsigned int *handlep
,
4940 struct nlattr
**options
, struct netdev_queue_stats
*stats
)
4942 static const struct nl_policy tca_policy
[] = {
4943 [TCA_OPTIONS
] = { .type
= NL_A_NESTED
, .optional
= false },
4944 [TCA_STATS2
] = { .type
= NL_A_NESTED
, .optional
= false },
4946 struct nlattr
*ta
[ARRAY_SIZE(tca_policy
)];
4948 if (!nl_policy_parse(msg
, NLMSG_HDRLEN
+ sizeof(struct tcmsg
),
4949 tca_policy
, ta
, ARRAY_SIZE(ta
))) {
4950 VLOG_WARN_RL(&rl
, "failed to parse class message");
4955 struct tcmsg
*tc
= ofpbuf_at_assert(msg
, NLMSG_HDRLEN
, sizeof *tc
);
4956 *handlep
= tc
->tcm_handle
;
4960 *options
= ta
[TCA_OPTIONS
];
4964 const struct gnet_stats_queue
*gsq
;
4965 struct gnet_stats_basic gsb
;
4967 static const struct nl_policy stats_policy
[] = {
4968 [TCA_STATS_BASIC
] = { .type
= NL_A_UNSPEC
, .optional
= false,
4969 .min_len
= sizeof gsb
},
4970 [TCA_STATS_QUEUE
] = { .type
= NL_A_UNSPEC
, .optional
= false,
4971 .min_len
= sizeof *gsq
},
4973 struct nlattr
*sa
[ARRAY_SIZE(stats_policy
)];
4975 if (!nl_parse_nested(ta
[TCA_STATS2
], stats_policy
,
4976 sa
, ARRAY_SIZE(sa
))) {
4977 VLOG_WARN_RL(&rl
, "failed to parse class stats");
4981 /* Alignment issues screw up the length of struct gnet_stats_basic on
4982 * some arch/bitsize combinations. Newer versions of Linux have a
4983 * struct gnet_stats_basic_packed, but we can't depend on that. The
4984 * easiest thing to do is just to make a copy. */
4985 memset(&gsb
, 0, sizeof gsb
);
4986 memcpy(&gsb
, nl_attr_get(sa
[TCA_STATS_BASIC
]),
4987 MIN(nl_attr_get_size(sa
[TCA_STATS_BASIC
]), sizeof gsb
));
4988 stats
->tx_bytes
= gsb
.bytes
;
4989 stats
->tx_packets
= gsb
.packets
;
4991 gsq
= nl_attr_get(sa
[TCA_STATS_QUEUE
]);
4992 stats
->tx_errors
= gsq
->drops
;
5002 memset(stats
, 0, sizeof *stats
);
5007 /* Queries the kernel for class with identifier 'handle' and parent 'parent'
5010 tc_query_class(const struct netdev
*netdev
,
5011 unsigned int handle
, unsigned int parent
,
5012 struct ofpbuf
**replyp
)
5014 struct ofpbuf request
;
5015 struct tcmsg
*tcmsg
;
5018 tcmsg
= tc_make_request(netdev
, RTM_GETTCLASS
, NLM_F_ECHO
, &request
);
5022 tcmsg
->tcm_handle
= handle
;
5023 tcmsg
->tcm_parent
= parent
;
5025 error
= tc_transact(&request
, replyp
);
5027 VLOG_WARN_RL(&rl
, "query %s class %u:%u (parent %u:%u) failed (%s)",
5028 netdev_get_name(netdev
),
5029 tc_get_major(handle
), tc_get_minor(handle
),
5030 tc_get_major(parent
), tc_get_minor(parent
),
5031 ovs_strerror(error
));
5036 /* Equivalent to "tc class del dev <name> handle <handle>". */
5038 tc_delete_class(const struct netdev
*netdev
, unsigned int handle
)
5040 struct ofpbuf request
;
5041 struct tcmsg
*tcmsg
;
5044 tcmsg
= tc_make_request(netdev
, RTM_DELTCLASS
, 0, &request
);
5048 tcmsg
->tcm_handle
= handle
;
5049 tcmsg
->tcm_parent
= 0;
5051 error
= tc_transact(&request
, NULL
);
5053 VLOG_WARN_RL(&rl
, "delete %s class %u:%u failed (%s)",
5054 netdev_get_name(netdev
),
5055 tc_get_major(handle
), tc_get_minor(handle
),
5056 ovs_strerror(error
));
5061 /* Equivalent to "tc qdisc del dev <name> root". */
5063 tc_del_qdisc(struct netdev
*netdev_
)
5065 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
5066 struct ofpbuf request
;
5067 struct tcmsg
*tcmsg
;
5070 tcmsg
= tc_make_request(netdev_
, RTM_DELQDISC
, 0, &request
);
5074 tcmsg
->tcm_handle
= tc_make_handle(1, 0);
5075 tcmsg
->tcm_parent
= TC_H_ROOT
;
5077 error
= tc_transact(&request
, NULL
);
5078 if (error
== EINVAL
) {
5079 /* EINVAL probably means that the default qdisc was in use, in which
5080 * case we've accomplished our purpose. */
5083 if (!error
&& netdev
->tc
) {
5084 if (netdev
->tc
->ops
->tc_destroy
) {
5085 netdev
->tc
->ops
->tc_destroy(netdev
->tc
);
5093 getqdisc_is_safe(void)
5095 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
5096 static bool safe
= false;
5098 if (ovsthread_once_start(&once
)) {
5099 struct utsname utsname
;
5102 if (uname(&utsname
) == -1) {
5103 VLOG_WARN("uname failed (%s)", ovs_strerror(errno
));
5104 } else if (!ovs_scan(utsname
.release
, "%d.%d", &major
, &minor
)) {
5105 VLOG_WARN("uname reported bad OS release (%s)", utsname
.release
);
5106 } else if (major
< 2 || (major
== 2 && minor
< 35)) {
5107 VLOG_INFO("disabling unsafe RTM_GETQDISC in Linux kernel %s",
5112 ovsthread_once_done(&once
);
5117 /* If 'netdev''s qdisc type and parameters are not yet known, queries the
5118 * kernel to determine what they are. Returns 0 if successful, otherwise a
5119 * positive errno value. */
5121 tc_query_qdisc(const struct netdev
*netdev_
)
5123 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
5124 struct ofpbuf request
, *qdisc
;
5125 const struct tc_ops
*ops
;
5126 struct tcmsg
*tcmsg
;
5134 /* This RTM_GETQDISC is crafted to avoid OOPSing kernels that do not have
5135 * commit 53b0f08 "net_sched: Fix qdisc_notify()", which is anything before
5136 * 2.6.35 without that fix backported to it.
5138 * To avoid the OOPS, we must not make a request that would attempt to dump
5139 * a "built-in" qdisc, that is, the default pfifo_fast qdisc or one of a
5140 * few others. There are a few ways that I can see to do this, but most of
5141 * them seem to be racy (and if you lose the race the kernel OOPSes). The
5142 * technique chosen here is to assume that any non-default qdisc that we
5143 * create will have a class with handle 1:0. The built-in qdiscs only have
5144 * a class with handle 0:0.
5146 * On Linux 2.6.35+ we use the straightforward method because it allows us
5147 * to handle non-builtin qdiscs without handle 1:0 (e.g. codel). However,
5148 * in such a case we get no response at all from the kernel (!) if a
5149 * builtin qdisc is in use (which is later caught by "!error &&
5150 * !qdisc->size"). */
5151 tcmsg
= tc_make_request(netdev_
, RTM_GETQDISC
, NLM_F_ECHO
, &request
);
5155 tcmsg
->tcm_handle
= tc_make_handle(getqdisc_is_safe() ? 0 : 1, 0);
5156 tcmsg
->tcm_parent
= getqdisc_is_safe() ? TC_H_ROOT
: 0;
5158 /* Figure out what tc class to instantiate. */
5159 error
= tc_transact(&request
, &qdisc
);
5160 if (!error
&& qdisc
->size
) {
5163 error
= tc_parse_qdisc(qdisc
, &kind
, NULL
);
5165 ops
= &tc_ops_other
;
5167 ops
= tc_lookup_linux_name(kind
);
5169 static struct vlog_rate_limit rl2
= VLOG_RATE_LIMIT_INIT(1, 1);
5170 VLOG_DBG_RL(&rl2
, "unknown qdisc \"%s\"", kind
);
5172 ops
= &tc_ops_other
;
5175 } else if ((!error
&& !qdisc
->size
) || error
== ENOENT
) {
5176 /* Either it's a built-in qdisc, or (on Linux pre-2.6.35) it's a qdisc
5177 * set up by some other entity that doesn't have a handle 1:0. We will
5178 * assume that it's the system default qdisc. */
5179 ops
= &tc_ops_default
;
5182 /* Who knows? Maybe the device got deleted. */
5183 VLOG_WARN_RL(&rl
, "query %s qdisc failed (%s)",
5184 netdev_get_name(netdev_
), ovs_strerror(error
));
5185 ops
= &tc_ops_other
;
5188 /* Instantiate it. */
5189 load_error
= ops
->tc_load(CONST_CAST(struct netdev
*, netdev_
), qdisc
);
5190 ovs_assert((load_error
== 0) == (netdev
->tc
!= NULL
));
5191 ofpbuf_delete(qdisc
);
5193 return error
? error
: load_error
;
5196 /* Linux traffic control uses tables with 256 entries ("rtab" tables) to
5197 approximate the time to transmit packets of various lengths. For an MTU of
5198 256 or less, each entry is exact; for an MTU of 257 through 512, each entry
5199 represents two possible packet lengths; for a MTU of 513 through 1024, four
5200 possible lengths; and so on.
5202 Returns, for the specified 'mtu', the number of bits that packet lengths
5203 need to be shifted right to fit within such a 256-entry table. */
5205 tc_calc_cell_log(unsigned int mtu
)
5210 mtu
= ETH_PAYLOAD_MAX
;
5212 mtu
+= ETH_HEADER_LEN
+ VLAN_HEADER_LEN
;
5214 for (cell_log
= 0; mtu
>= 256; cell_log
++) {
5221 /* Initializes 'rate' properly for a rate of 'Bps' bytes per second with an MTU
5224 tc_fill_rate(struct tc_ratespec
*rate
, uint64_t Bps
, int mtu
)
5226 memset(rate
, 0, sizeof *rate
);
5227 rate
->cell_log
= tc_calc_cell_log(mtu
);
5228 /* rate->overhead = 0; */ /* New in 2.6.24, not yet in some */
5229 /* rate->cell_align = 0; */ /* distro headers. */
5230 rate
->mpu
= ETH_TOTAL_MIN
;
5234 /* Appends to 'msg' an "rtab" table for the specified 'rate' as a Netlink
5235 * attribute of the specified "type".
5237 * See tc_calc_cell_log() above for a description of "rtab"s. */
5239 tc_put_rtab(struct ofpbuf
*msg
, uint16_t type
, const struct tc_ratespec
*rate
)
5244 rtab
= nl_msg_put_unspec_uninit(msg
, type
, TC_RTAB_SIZE
);
5245 for (i
= 0; i
< TC_RTAB_SIZE
/ sizeof *rtab
; i
++) {
5246 unsigned packet_size
= (i
+ 1) << rate
->cell_log
;
5247 if (packet_size
< rate
->mpu
) {
5248 packet_size
= rate
->mpu
;
5250 rtab
[i
] = tc_bytes_to_ticks(rate
->rate
, packet_size
);
5254 /* Calculates the proper value of 'buffer' or 'cbuffer' in HTB options given a
5255 * rate of 'Bps' bytes per second, the specified 'mtu', and a user-requested
5256 * burst size of 'burst_bytes'. (If no value was requested, a 'burst_bytes' of
5259 tc_calc_buffer(unsigned int Bps
, int mtu
, uint64_t burst_bytes
)
5261 unsigned int min_burst
= tc_buffer_per_jiffy(Bps
) + mtu
;
5262 return tc_bytes_to_ticks(Bps
, MAX(burst_bytes
, min_burst
));
5265 /* Linux-only functions declared in netdev-linux.h */
5267 /* Modifies the 'flag' bit in ethtool's flags field for 'netdev'. If
5268 * 'enable' is true, the bit is set. Otherwise, it is cleared. */
5270 netdev_linux_ethtool_set_flag(struct netdev
*netdev
, uint32_t flag
,
5271 const char *flag_name
, bool enable
)
5273 const char *netdev_name
= netdev_get_name(netdev
);
5274 struct ethtool_value evalue
;
5278 COVERAGE_INC(netdev_get_ethtool
);
5279 memset(&evalue
, 0, sizeof evalue
);
5280 error
= netdev_linux_do_ethtool(netdev_name
,
5281 (struct ethtool_cmd
*)&evalue
,
5282 ETHTOOL_GFLAGS
, "ETHTOOL_GFLAGS");
5287 COVERAGE_INC(netdev_set_ethtool
);
5288 new_flags
= (evalue
.data
& ~flag
) | (enable
? flag
: 0);
5289 if (new_flags
== evalue
.data
) {
5292 evalue
.data
= new_flags
;
5293 error
= netdev_linux_do_ethtool(netdev_name
,
5294 (struct ethtool_cmd
*)&evalue
,
5295 ETHTOOL_SFLAGS
, "ETHTOOL_SFLAGS");
5300 COVERAGE_INC(netdev_get_ethtool
);
5301 memset(&evalue
, 0, sizeof evalue
);
5302 error
= netdev_linux_do_ethtool(netdev_name
,
5303 (struct ethtool_cmd
*)&evalue
,
5304 ETHTOOL_GFLAGS
, "ETHTOOL_GFLAGS");
5309 if (new_flags
!= evalue
.data
) {
5310 VLOG_WARN_RL(&rl
, "attempt to %s ethtool %s flag on network "
5311 "device %s failed", enable
? "enable" : "disable",
5312 flag_name
, netdev_name
);
5319 /* Utility functions. */
5321 /* Copies 'src' into 'dst', performing format conversion in the process. */
5323 netdev_stats_from_rtnl_link_stats(struct netdev_stats
*dst
,
5324 const struct rtnl_link_stats
*src
)
5326 dst
->rx_packets
= src
->rx_packets
;
5327 dst
->tx_packets
= src
->tx_packets
;
5328 dst
->rx_bytes
= src
->rx_bytes
;
5329 dst
->tx_bytes
= src
->tx_bytes
;
5330 dst
->rx_errors
= src
->rx_errors
;
5331 dst
->tx_errors
= src
->tx_errors
;
5332 dst
->rx_dropped
= src
->rx_dropped
;
5333 dst
->tx_dropped
= src
->tx_dropped
;
5334 dst
->multicast
= src
->multicast
;
5335 dst
->collisions
= src
->collisions
;
5336 dst
->rx_length_errors
= src
->rx_length_errors
;
5337 dst
->rx_over_errors
= src
->rx_over_errors
;
5338 dst
->rx_crc_errors
= src
->rx_crc_errors
;
5339 dst
->rx_frame_errors
= src
->rx_frame_errors
;
5340 dst
->rx_fifo_errors
= src
->rx_fifo_errors
;
5341 dst
->rx_missed_errors
= src
->rx_missed_errors
;
5342 dst
->tx_aborted_errors
= src
->tx_aborted_errors
;
5343 dst
->tx_carrier_errors
= src
->tx_carrier_errors
;
5344 dst
->tx_fifo_errors
= src
->tx_fifo_errors
;
5345 dst
->tx_heartbeat_errors
= src
->tx_heartbeat_errors
;
5346 dst
->tx_window_errors
= src
->tx_window_errors
;
5349 /* Copies 'src' into 'dst', performing format conversion in the process. */
5351 netdev_stats_from_rtnl_link_stats64(struct netdev_stats
*dst
,
5352 const struct rtnl_link_stats64
*src
)
5354 dst
->rx_packets
= src
->rx_packets
;
5355 dst
->tx_packets
= src
->tx_packets
;
5356 dst
->rx_bytes
= src
->rx_bytes
;
5357 dst
->tx_bytes
= src
->tx_bytes
;
5358 dst
->rx_errors
= src
->rx_errors
;
5359 dst
->tx_errors
= src
->tx_errors
;
5360 dst
->rx_dropped
= src
->rx_dropped
;
5361 dst
->tx_dropped
= src
->tx_dropped
;
5362 dst
->multicast
= src
->multicast
;
5363 dst
->collisions
= src
->collisions
;
5364 dst
->rx_length_errors
= src
->rx_length_errors
;
5365 dst
->rx_over_errors
= src
->rx_over_errors
;
5366 dst
->rx_crc_errors
= src
->rx_crc_errors
;
5367 dst
->rx_frame_errors
= src
->rx_frame_errors
;
5368 dst
->rx_fifo_errors
= src
->rx_fifo_errors
;
5369 dst
->rx_missed_errors
= src
->rx_missed_errors
;
5370 dst
->tx_aborted_errors
= src
->tx_aborted_errors
;
5371 dst
->tx_carrier_errors
= src
->tx_carrier_errors
;
5372 dst
->tx_fifo_errors
= src
->tx_fifo_errors
;
5373 dst
->tx_heartbeat_errors
= src
->tx_heartbeat_errors
;
5374 dst
->tx_window_errors
= src
->tx_window_errors
;
5378 get_stats_via_netlink(const struct netdev
*netdev_
, struct netdev_stats
*stats
)
5380 struct ofpbuf request
;
5381 struct ofpbuf
*reply
;
5384 /* Filtering all counters by default */
5385 memset(stats
, 0xFF, sizeof(struct netdev_stats
));
5387 ofpbuf_init(&request
, 0);
5388 nl_msg_put_nlmsghdr(&request
,
5389 sizeof(struct ifinfomsg
) + NL_ATTR_SIZE(IFNAMSIZ
),
5390 RTM_GETLINK
, NLM_F_REQUEST
);
5391 ofpbuf_put_zeros(&request
, sizeof(struct ifinfomsg
));
5392 nl_msg_put_string(&request
, IFLA_IFNAME
, netdev_get_name(netdev_
));
5393 error
= nl_transact(NETLINK_ROUTE
, &request
, &reply
);
5394 ofpbuf_uninit(&request
);
5399 if (ofpbuf_try_pull(reply
, NLMSG_HDRLEN
+ sizeof(struct ifinfomsg
))) {
5400 const struct nlattr
*a
= nl_attr_find(reply
, 0, IFLA_STATS64
);
5401 if (a
&& nl_attr_get_size(a
) >= sizeof(struct rtnl_link_stats64
)) {
5402 netdev_stats_from_rtnl_link_stats64(stats
, nl_attr_get(a
));
5405 const struct nlattr
*a
= nl_attr_find(reply
, 0, IFLA_STATS
);
5406 if (a
&& nl_attr_get_size(a
) >= sizeof(struct rtnl_link_stats
)) {
5407 netdev_stats_from_rtnl_link_stats(stats
, nl_attr_get(a
));
5410 VLOG_WARN_RL(&rl
, "RTM_GETLINK reply lacks stats");
5415 VLOG_WARN_RL(&rl
, "short RTM_GETLINK reply");
5420 ofpbuf_delete(reply
);
5425 get_flags(const struct netdev
*dev
, unsigned int *flags
)
5431 error
= af_inet_ifreq_ioctl(dev
->name
, &ifr
, SIOCGIFFLAGS
, "SIOCGIFFLAGS");
5433 *flags
= ifr
.ifr_flags
;
5439 set_flags(const char *name
, unsigned int flags
)
5443 ifr
.ifr_flags
= flags
;
5444 return af_inet_ifreq_ioctl(name
, &ifr
, SIOCSIFFLAGS
, "SIOCSIFFLAGS");
5448 do_get_ifindex(const char *netdev_name
)
5453 ovs_strzcpy(ifr
.ifr_name
, netdev_name
, sizeof ifr
.ifr_name
);
5454 COVERAGE_INC(netdev_get_ifindex
);
5456 error
= af_inet_ioctl(SIOCGIFINDEX
, &ifr
);
5458 VLOG_WARN_RL(&rl
, "ioctl(SIOCGIFINDEX) on %s device failed: %s",
5459 netdev_name
, ovs_strerror(error
));
5462 return ifr
.ifr_ifindex
;
5466 get_ifindex(const struct netdev
*netdev_
, int *ifindexp
)
5468 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
5470 if (!(netdev
->cache_valid
& VALID_IFINDEX
)) {
5471 int ifindex
= do_get_ifindex(netdev_get_name(netdev_
));
5474 netdev
->get_ifindex_error
= -ifindex
;
5475 netdev
->ifindex
= 0;
5477 netdev
->get_ifindex_error
= 0;
5478 netdev
->ifindex
= ifindex
;
5480 netdev
->cache_valid
|= VALID_IFINDEX
;
5483 *ifindexp
= netdev
->ifindex
;
5484 return netdev
->get_ifindex_error
;
5488 get_etheraddr(const char *netdev_name
, struct eth_addr
*ea
)
5494 memset(&ifr
, 0, sizeof ifr
);
5495 ovs_strzcpy(ifr
.ifr_name
, netdev_name
, sizeof ifr
.ifr_name
);
5496 COVERAGE_INC(netdev_get_hwaddr
);
5497 error
= af_inet_ioctl(SIOCGIFHWADDR
, &ifr
);
5499 /* ENODEV probably means that a vif disappeared asynchronously and
5500 * hasn't been removed from the database yet, so reduce the log level
5501 * to INFO for that case. */
5502 VLOG(error
== ENODEV
? VLL_INFO
: VLL_ERR
,
5503 "ioctl(SIOCGIFHWADDR) on %s device failed: %s",
5504 netdev_name
, ovs_strerror(error
));
5507 hwaddr_family
= ifr
.ifr_hwaddr
.sa_family
;
5508 if (hwaddr_family
!= AF_UNSPEC
&& hwaddr_family
!= ARPHRD_ETHER
) {
5509 VLOG_INFO("%s device has unknown hardware address family %d",
5510 netdev_name
, hwaddr_family
);
5513 memcpy(ea
, ifr
.ifr_hwaddr
.sa_data
, ETH_ADDR_LEN
);
5518 set_etheraddr(const char *netdev_name
, const struct eth_addr mac
)
5523 memset(&ifr
, 0, sizeof ifr
);
5524 ovs_strzcpy(ifr
.ifr_name
, netdev_name
, sizeof ifr
.ifr_name
);
5525 ifr
.ifr_hwaddr
.sa_family
= ARPHRD_ETHER
;
5526 memcpy(ifr
.ifr_hwaddr
.sa_data
, &mac
, ETH_ADDR_LEN
);
5527 COVERAGE_INC(netdev_set_hwaddr
);
5528 error
= af_inet_ioctl(SIOCSIFHWADDR
, &ifr
);
5530 VLOG_ERR("ioctl(SIOCSIFHWADDR) on %s device failed: %s",
5531 netdev_name
, ovs_strerror(error
));
5537 netdev_linux_do_ethtool(const char *name
, struct ethtool_cmd
*ecmd
,
5538 int cmd
, const char *cmd_name
)
5543 memset(&ifr
, 0, sizeof ifr
);
5544 ovs_strzcpy(ifr
.ifr_name
, name
, sizeof ifr
.ifr_name
);
5545 ifr
.ifr_data
= (caddr_t
) ecmd
;
5548 error
= af_inet_ioctl(SIOCETHTOOL
, &ifr
);
5550 if (error
!= EOPNOTSUPP
) {
5551 VLOG_WARN_RL(&rl
, "ethtool command %s on network device %s "
5552 "failed: %s", cmd_name
, name
, ovs_strerror(error
));
5554 /* The device doesn't support this operation. That's pretty
5555 * common, so there's no point in logging anything. */
5561 /* Returns an AF_PACKET raw socket or a negative errno value. */
5563 af_packet_sock(void)
5565 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
5568 if (ovsthread_once_start(&once
)) {
5569 sock
= socket(AF_PACKET
, SOCK_RAW
, 0);
5571 int error
= set_nonblocking(sock
);
5578 VLOG_ERR("failed to create packet socket: %s",
5579 ovs_strerror(errno
));
5581 ovsthread_once_done(&once
);