2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2016, 2017 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 #include <sys/types.h>
23 #include <netinet/in.h>
31 #include <sys/ioctl.h>
37 #include "dp-packet.h"
38 #include "openvswitch/dynamic-string.h"
39 #include "fatal-signal.h"
41 #include "openvswitch/list.h"
42 #include "netdev-offload-provider.h"
43 #include "netdev-provider.h"
44 #include "netdev-vport.h"
45 #include "odp-netlink.h"
46 #include "openflow/openflow.h"
48 #include "openvswitch/ofp-print.h"
49 #include "openvswitch/poll-loop.h"
51 #include "openvswitch/shash.h"
53 #include "socket-util.h"
56 #include "openvswitch/vlog.h"
63 VLOG_DEFINE_THIS_MODULE(netdev
);
65 COVERAGE_DEFINE(netdev_received
);
66 COVERAGE_DEFINE(netdev_sent
);
67 COVERAGE_DEFINE(netdev_add_router
);
68 COVERAGE_DEFINE(netdev_get_stats
);
69 COVERAGE_DEFINE(netdev_send_prepare_drops
);
70 COVERAGE_DEFINE(netdev_push_header_drops
);
72 struct netdev_saved_flags
{
73 struct netdev
*netdev
;
74 struct ovs_list node
; /* In struct netdev's saved_flags_list. */
75 enum netdev_flags saved_flags
;
76 enum netdev_flags saved_values
;
79 /* Protects 'netdev_shash' and the mutable members of struct netdev. */
80 static struct ovs_mutex netdev_mutex
= OVS_MUTEX_INITIALIZER
;
82 /* All created network devices. */
83 static struct shash netdev_shash
OVS_GUARDED_BY(netdev_mutex
)
84 = SHASH_INITIALIZER(&netdev_shash
);
86 /* Mutual exclusion of */
87 static struct ovs_mutex netdev_class_mutex
OVS_ACQ_BEFORE(netdev_mutex
)
88 = OVS_MUTEX_INITIALIZER
;
90 /* Contains 'struct netdev_registered_class'es. */
91 static struct cmap netdev_classes
= CMAP_INITIALIZER
;
93 struct netdev_registered_class
{
94 struct cmap_node cmap_node
; /* In 'netdev_classes', by class->type. */
95 const struct netdev_class
*class;
97 /* Number of references: one for the class itself and one for every
98 * instance of the class. */
99 struct ovs_refcount refcnt
;
102 /* This is set pretty low because we probably won't learn anything from the
103 * additional log messages. */
104 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 20);
106 static void restore_all_flags(void *aux OVS_UNUSED
);
107 void update_device_args(struct netdev
*, const struct shash
*args
);
109 void signal_remove_xdp(struct netdev
*netdev
);
113 netdev_n_txq(const struct netdev
*netdev
)
115 return netdev
->n_txq
;
119 netdev_n_rxq(const struct netdev
*netdev
)
121 return netdev
->n_rxq
;
125 netdev_is_pmd(const struct netdev
*netdev
)
127 return netdev
->netdev_class
->is_pmd
;
131 netdev_has_tunnel_push_pop(const struct netdev
*netdev
)
133 return netdev
->netdev_class
->push_header
134 && netdev
->netdev_class
->pop_header
;
138 netdev_initialize(void)
139 OVS_EXCLUDED(netdev_mutex
)
141 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
143 if (ovsthread_once_start(&once
)) {
144 fatal_signal_add_hook(restore_all_flags
, NULL
, NULL
, true);
146 netdev_vport_patch_register();
149 netdev_register_provider(&netdev_linux_class
);
150 netdev_register_provider(&netdev_internal_class
);
151 netdev_register_provider(&netdev_tap_class
);
152 netdev_vport_tunnel_register();
154 netdev_register_flow_api_provider(&netdev_offload_tc
);
156 netdev_register_provider(&netdev_afxdp_class
);
157 netdev_register_provider(&netdev_afxdp_nonpmd_class
);
160 #if defined(__FreeBSD__) || defined(__NetBSD__)
161 netdev_register_provider(&netdev_tap_class
);
162 netdev_register_provider(&netdev_bsd_class
);
165 netdev_register_provider(&netdev_windows_class
);
166 netdev_register_provider(&netdev_internal_class
);
167 netdev_vport_tunnel_register();
169 ovsthread_once_done(&once
);
173 /* Performs periodic work needed by all the various kinds of netdevs.
175 * If your program opens any netdevs, it must call this function within its
179 OVS_EXCLUDED(netdev_mutex
)
183 struct netdev_registered_class
*rc
;
184 CMAP_FOR_EACH (rc
, cmap_node
, &netdev_classes
) {
185 if (rc
->class->run
) {
186 rc
->class->run(rc
->class);
191 /* Arranges for poll_block() to wake up when netdev_run() needs to be called.
193 * If your program opens any netdevs, it must call this function within its
197 OVS_EXCLUDED(netdev_mutex
)
201 struct netdev_registered_class
*rc
;
202 CMAP_FOR_EACH (rc
, cmap_node
, &netdev_classes
) {
203 if (rc
->class->wait
) {
204 rc
->class->wait(rc
->class);
209 static struct netdev_registered_class
*
210 netdev_lookup_class(const char *type
)
212 struct netdev_registered_class
*rc
;
213 CMAP_FOR_EACH_WITH_HASH (rc
, cmap_node
, hash_string(type
, 0),
215 if (!strcmp(type
, rc
->class->type
)) {
222 /* Initializes and registers a new netdev provider. After successful
223 * registration, new netdevs of that type can be opened using netdev_open(). */
225 netdev_register_provider(const struct netdev_class
*new_class
)
226 OVS_EXCLUDED(netdev_class_mutex
, netdev_mutex
)
230 ovs_mutex_lock(&netdev_class_mutex
);
231 if (netdev_lookup_class(new_class
->type
)) {
232 VLOG_WARN("attempted to register duplicate netdev provider: %s",
236 error
= new_class
->init
? new_class
->init() : 0;
238 struct netdev_registered_class
*rc
;
240 rc
= xmalloc(sizeof *rc
);
241 cmap_insert(&netdev_classes
, &rc
->cmap_node
,
242 hash_string(new_class
->type
, 0));
243 rc
->class = new_class
;
244 ovs_refcount_init(&rc
->refcnt
);
246 VLOG_ERR("failed to initialize %s network device class: %s",
247 new_class
->type
, ovs_strerror(error
));
250 ovs_mutex_unlock(&netdev_class_mutex
);
255 /* Unregisters a netdev provider. 'type' must have been previously registered
256 * and not currently be in use by any netdevs. After unregistration new
257 * netdevs of that type cannot be opened using netdev_open(). (However, the
258 * provider may still be accessible from other threads until the next RCU grace
259 * period, so the caller must not free or re-register the same netdev_class
260 * until that has passed.) */
262 netdev_unregister_provider(const char *type
)
263 OVS_EXCLUDED(netdev_class_mutex
, netdev_mutex
)
265 struct netdev_registered_class
*rc
;
270 ovs_mutex_lock(&netdev_class_mutex
);
271 rc
= netdev_lookup_class(type
);
273 VLOG_WARN("attempted to unregister a netdev provider that is not "
274 "registered: %s", type
);
275 error
= EAFNOSUPPORT
;
276 } else if (ovs_refcount_unref(&rc
->refcnt
) != 1) {
277 ovs_refcount_ref(&rc
->refcnt
);
278 VLOG_WARN("attempted to unregister in use netdev provider: %s",
282 cmap_remove(&netdev_classes
, &rc
->cmap_node
,
283 hash_string(rc
->class->type
, 0));
284 ovsrcu_postpone(free
, rc
);
287 ovs_mutex_unlock(&netdev_class_mutex
);
292 /* Clears 'types' and enumerates the types of all currently registered netdev
293 * providers into it. The caller must first initialize the sset. */
295 netdev_enumerate_types(struct sset
*types
)
296 OVS_EXCLUDED(netdev_mutex
)
301 struct netdev_registered_class
*rc
;
302 CMAP_FOR_EACH (rc
, cmap_node
, &netdev_classes
) {
303 sset_add(types
, rc
->class->type
);
308 netdev_vport_type_from_name(const char *name
)
310 struct netdev_registered_class
*rc
;
312 CMAP_FOR_EACH (rc
, cmap_node
, &netdev_classes
) {
313 const char *dpif_port
= netdev_vport_class_get_dpif_port(rc
->class);
314 if (dpif_port
&& !strncmp(name
, dpif_port
, strlen(dpif_port
))) {
315 type
= rc
->class->type
;
322 /* Check that the network device name is not the same as any of the registered
323 * vport providers' dpif_port name (dpif_port is NULL if the vport provider
324 * does not define it) or the datapath internal port name (e.g. ovs-system).
326 * Returns true if there is a name conflict, false otherwise. */
328 netdev_is_reserved_name(const char *name
)
329 OVS_EXCLUDED(netdev_mutex
)
333 struct netdev_registered_class
*rc
;
334 CMAP_FOR_EACH (rc
, cmap_node
, &netdev_classes
) {
335 const char *dpif_port
= netdev_vport_class_get_dpif_port(rc
->class);
336 if (dpif_port
&& !strncmp(name
, dpif_port
, strlen(dpif_port
))) {
341 if (!strncmp(name
, "ovs-", 4)) {
346 dp_enumerate_types(&types
);
347 SSET_FOR_EACH (type
, &types
) {
348 if (!strcmp(name
+4, type
)) {
349 sset_destroy(&types
);
353 sset_destroy(&types
);
359 /* Opens the network device named 'name' (e.g. "eth0") of the specified 'type'
360 * (e.g. "system") and returns zero if successful, otherwise a positive errno
361 * value. On success, sets '*netdevp' to the new network device, otherwise to
364 * Some network devices may need to be configured (with netdev_set_config())
365 * before they can be used.
367 * Before opening rxqs or sending packets, '*netdevp' may need to be
368 * reconfigured (with netdev_is_reconf_required() and netdev_reconfigure()).
371 netdev_open(const char *name
, const char *type
, struct netdev
**netdevp
)
372 OVS_EXCLUDED(netdev_mutex
)
374 struct netdev
*netdev
;
378 /* Reject empty names. This saves the providers having to do this. At
379 * least one screwed this up: the netdev-linux "tap" implementation
380 * passed the name directly to the Linux TUNSETIFF call, which treats
381 * an empty string as a request to generate a unique name. */
387 ovs_mutex_lock(&netdev_mutex
);
388 netdev
= shash_find_data(&netdev_shash
, name
);
391 type
&& type
[0] && strcmp(type
, netdev
->netdev_class
->type
)) {
393 if (netdev
->auto_classified
) {
394 /* If this device was first created without a classification type,
395 * for example due to routing or tunneling code, and they keep a
396 * reference, a "classified" call to open will fail. In this case
397 * we remove the classless device, and re-add it below. We remove
398 * the netdev from the shash, and change the sequence, so owners of
399 * the old classless device can release/cleanup. */
401 shash_delete(&netdev_shash
, netdev
->node
);
403 netdev_change_seq_changed(netdev
);
413 struct netdev_registered_class
*rc
;
415 rc
= netdev_lookup_class(type
&& type
[0] ? type
: "system");
416 if (rc
&& ovs_refcount_try_ref_rcu(&rc
->refcnt
)) {
417 netdev
= rc
->class->alloc();
419 memset(netdev
, 0, sizeof *netdev
);
420 netdev
->netdev_class
= rc
->class;
421 netdev
->auto_classified
= type
&& type
[0] ? false : true;
422 netdev
->name
= xstrdup(name
);
423 netdev
->change_seq
= 1;
424 netdev
->reconfigure_seq
= seq_create();
425 netdev
->last_reconfigure_seq
=
426 seq_read(netdev
->reconfigure_seq
);
427 ovsrcu_set(&netdev
->flow_api
, NULL
);
428 netdev
->hw_info
.oor
= false;
429 netdev
->node
= shash_add(&netdev_shash
, name
, netdev
);
431 /* By default enable one tx and rx queue per netdev. */
432 netdev
->n_txq
= netdev
->netdev_class
->send
? 1 : 0;
433 netdev
->n_rxq
= netdev
->netdev_class
->rxq_alloc
? 1 : 0;
435 ovs_list_init(&netdev
->saved_flags_list
);
437 error
= rc
->class->construct(netdev
);
439 netdev_change_seq_changed(netdev
);
441 ovs_refcount_unref(&rc
->refcnt
);
442 seq_destroy(netdev
->reconfigure_seq
);
444 ovs_assert(ovs_list_is_empty(&netdev
->saved_flags_list
));
445 shash_delete(&netdev_shash
, netdev
->node
);
446 rc
->class->dealloc(netdev
);
452 VLOG_WARN("could not create netdev %s of unknown type %s",
454 error
= EAFNOSUPPORT
;
464 ovs_mutex_unlock(&netdev_mutex
);
469 /* Returns a reference to 'netdev_' for the caller to own. Returns null if
470 * 'netdev_' is null. */
472 netdev_ref(const struct netdev
*netdev_
)
473 OVS_EXCLUDED(netdev_mutex
)
475 struct netdev
*netdev
= CONST_CAST(struct netdev
*, netdev_
);
478 ovs_mutex_lock(&netdev_mutex
);
479 ovs_assert(netdev
->ref_cnt
> 0);
481 ovs_mutex_unlock(&netdev_mutex
);
486 /* Reconfigures the device 'netdev' with 'args'. 'args' may be empty
487 * or NULL if none are needed. */
489 netdev_set_config(struct netdev
*netdev
, const struct smap
*args
, char **errp
)
490 OVS_EXCLUDED(netdev_mutex
)
492 if (netdev
->netdev_class
->set_config
) {
493 const struct smap no_args
= SMAP_INITIALIZER(&no_args
);
494 char *verbose_error
= NULL
;
497 error
= netdev
->netdev_class
->set_config(netdev
,
498 args
? args
: &no_args
,
501 VLOG_WARN_BUF(verbose_error
? NULL
: errp
,
502 "%s: could not set configuration (%s)",
503 netdev_get_name(netdev
), ovs_strerror(error
));
506 *errp
= verbose_error
;
513 } else if (args
&& !smap_is_empty(args
)) {
514 VLOG_WARN_BUF(errp
, "%s: arguments provided to device that is not configurable",
515 netdev_get_name(netdev
));
520 /* Returns the current configuration for 'netdev' in 'args'. The caller must
521 * have already initialized 'args' with smap_init(). Returns 0 on success, in
522 * which case 'args' will be filled with 'netdev''s configuration. On failure
523 * returns a positive errno value, in which case 'args' will be empty.
525 * The caller owns 'args' and its contents and must eventually free them with
528 netdev_get_config(const struct netdev
*netdev
, struct smap
*args
)
529 OVS_EXCLUDED(netdev_mutex
)
534 if (netdev
->netdev_class
->get_config
) {
535 error
= netdev
->netdev_class
->get_config(netdev
, args
);
546 const struct netdev_tunnel_config
*
547 netdev_get_tunnel_config(const struct netdev
*netdev
)
548 OVS_EXCLUDED(netdev_mutex
)
550 if (netdev
->netdev_class
->get_tunnel_config
) {
551 return netdev
->netdev_class
->get_tunnel_config(netdev
);
557 /* Returns the id of the numa node the 'netdev' is on. If the function
558 * is not implemented, returns NETDEV_NUMA_UNSPEC. */
560 netdev_get_numa_id(const struct netdev
*netdev
)
562 if (netdev
->netdev_class
->get_numa_id
) {
563 return netdev
->netdev_class
->get_numa_id(netdev
);
565 return NETDEV_NUMA_UNSPEC
;
570 netdev_unref(struct netdev
*dev
)
571 OVS_RELEASES(netdev_mutex
)
573 ovs_assert(dev
->ref_cnt
);
574 if (!--dev
->ref_cnt
) {
575 const struct netdev_class
*class = dev
->netdev_class
;
576 struct netdev_registered_class
*rc
;
578 netdev_uninit_flow_api(dev
);
580 dev
->netdev_class
->destruct(dev
);
583 shash_delete(&netdev_shash
, dev
->node
);
586 seq_destroy(dev
->reconfigure_seq
);
587 dev
->netdev_class
->dealloc(dev
);
588 ovs_mutex_unlock(&netdev_mutex
);
590 rc
= netdev_lookup_class(class->type
);
591 ovs_refcount_unref(&rc
->refcnt
);
593 ovs_mutex_unlock(&netdev_mutex
);
597 /* Closes and destroys 'netdev'. */
599 netdev_close(struct netdev
*netdev
)
600 OVS_EXCLUDED(netdev_mutex
)
603 ovs_mutex_lock(&netdev_mutex
);
604 netdev_unref(netdev
);
608 /* Removes 'netdev' from the global shash and unrefs 'netdev'.
610 * This allows handler and revalidator threads to still retain references
611 * to this netdev while the main thread changes interface configuration.
613 * This function should only be called by the main thread when closing
614 * netdevs during user configuration changes. Otherwise, netdev_close should be
615 * used to close netdevs. */
617 netdev_remove(struct netdev
*netdev
)
620 ovs_mutex_lock(&netdev_mutex
);
622 shash_delete(&netdev_shash
, netdev
->node
);
624 netdev_change_seq_changed(netdev
);
626 netdev_unref(netdev
);
630 /* Parses 'netdev_name_', which is of the form [type@]name into its component
631 * pieces. 'name' and 'type' must be freed by the caller. */
633 netdev_parse_name(const char *netdev_name_
, char **name
, char **type
)
635 char *netdev_name
= xstrdup(netdev_name_
);
638 separator
= strchr(netdev_name
, '@');
642 *name
= xstrdup(separator
+ 1);
645 *type
= xstrdup("system");
649 /* Attempts to open a netdev_rxq handle for obtaining packets received on
650 * 'netdev'. On success, returns 0 and stores a nonnull 'netdev_rxq *' into
651 * '*rxp'. On failure, returns a positive errno value and stores NULL into
654 * Some kinds of network devices might not support receiving packets. This
655 * function returns EOPNOTSUPP in that case.*/
657 netdev_rxq_open(struct netdev
*netdev
, struct netdev_rxq
**rxp
, int id
)
658 OVS_EXCLUDED(netdev_mutex
)
662 if (netdev
->netdev_class
->rxq_alloc
&& id
< netdev
->n_rxq
) {
663 struct netdev_rxq
*rx
= netdev
->netdev_class
->rxq_alloc();
667 error
= netdev
->netdev_class
->rxq_construct(rx
);
673 netdev
->netdev_class
->rxq_dealloc(rx
);
687 netdev_rxq_close(struct netdev_rxq
*rx
)
688 OVS_EXCLUDED(netdev_mutex
)
691 struct netdev
*netdev
= rx
->netdev
;
692 netdev
->netdev_class
->rxq_destruct(rx
);
693 netdev
->netdev_class
->rxq_dealloc(rx
);
694 netdev_close(netdev
);
698 bool netdev_rxq_enabled(struct netdev_rxq
*rx
)
702 if (rx
->netdev
->netdev_class
->rxq_enabled
) {
703 enabled
= rx
->netdev
->netdev_class
->rxq_enabled(rx
);
708 /* Attempts to receive a batch of packets from 'rx'. 'batch' should point to
709 * the beginning of an array of NETDEV_MAX_BURST pointers to dp_packet. If
710 * successful, this function stores pointers to up to NETDEV_MAX_BURST
711 * dp_packets into the array, transferring ownership of the packets to the
712 * caller, stores the number of received packets in 'batch->count', and returns
715 * The implementation does not necessarily initialize any non-data members of
716 * 'batch'. That is, the caller must initialize layer pointers and metadata
717 * itself, if desired, e.g. with pkt_metadata_init() and miniflow_extract().
719 * Returns EAGAIN immediately if no packet is ready to be received or another
720 * positive errno value if an error was encountered. */
722 netdev_rxq_recv(struct netdev_rxq
*rx
, struct dp_packet_batch
*batch
,
727 retval
= rx
->netdev
->netdev_class
->rxq_recv(rx
, batch
, qfill
);
729 COVERAGE_INC(netdev_received
);
736 /* Arranges for poll_block() to wake up when a packet is ready to be received
739 netdev_rxq_wait(struct netdev_rxq
*rx
)
741 rx
->netdev
->netdev_class
->rxq_wait(rx
);
744 /* Discards any packets ready to be received on 'rx'. */
746 netdev_rxq_drain(struct netdev_rxq
*rx
)
748 return (rx
->netdev
->netdev_class
->rxq_drain
749 ? rx
->netdev
->netdev_class
->rxq_drain(rx
)
753 /* Configures the number of tx queues of 'netdev'. Returns 0 if successful,
754 * otherwise a positive errno value.
756 * 'n_txq' specifies the exact number of transmission queues to create.
758 * The change might not effective immediately. The caller must check if a
759 * reconfiguration is required with netdev_is_reconf_required() and eventually
760 * call netdev_reconfigure() before using the new queues.
762 * On error, the tx queue configuration is unchanged */
764 netdev_set_tx_multiq(struct netdev
*netdev
, unsigned int n_txq
)
768 error
= (netdev
->netdev_class
->set_tx_multiq
769 ? netdev
->netdev_class
->set_tx_multiq(netdev
, MAX(n_txq
, 1))
772 if (error
&& error
!= EOPNOTSUPP
) {
773 VLOG_DBG_RL(&rl
, "failed to set tx queue for network device %s:"
774 "%s", netdev_get_name(netdev
), ovs_strerror(error
));
781 netdev_get_pt_mode(const struct netdev
*netdev
)
783 return (netdev
->netdev_class
->get_pt_mode
784 ? netdev
->netdev_class
->get_pt_mode(netdev
)
785 : NETDEV_PT_LEGACY_L2
);
788 /* Check if a 'packet' is compatible with 'netdev_flags'.
789 * If a packet is incompatible, return 'false' with the 'errormsg'
790 * pointing to a reason. */
792 netdev_send_prepare_packet(const uint64_t netdev_flags
,
793 struct dp_packet
*packet
, char **errormsg
)
797 if (dp_packet_hwol_is_tso(packet
)
798 && !(netdev_flags
& NETDEV_TX_OFFLOAD_TCP_TSO
)) {
799 /* Fall back to GSO in software. */
800 VLOG_ERR_BUF(errormsg
, "No TSO support");
804 l4_mask
= dp_packet_hwol_l4_mask(packet
);
806 if (dp_packet_hwol_l4_is_tcp(packet
)) {
807 if (!(netdev_flags
& NETDEV_TX_OFFLOAD_TCP_CKSUM
)) {
808 /* Fall back to TCP csum in software. */
809 VLOG_ERR_BUF(errormsg
, "No TCP checksum support");
812 } else if (dp_packet_hwol_l4_is_udp(packet
)) {
813 if (!(netdev_flags
& NETDEV_TX_OFFLOAD_UDP_CKSUM
)) {
814 /* Fall back to UDP csum in software. */
815 VLOG_ERR_BUF(errormsg
, "No UDP checksum support");
818 } else if (dp_packet_hwol_l4_is_sctp(packet
)) {
819 if (!(netdev_flags
& NETDEV_TX_OFFLOAD_SCTP_CKSUM
)) {
820 /* Fall back to SCTP csum in software. */
821 VLOG_ERR_BUF(errormsg
, "No SCTP checksum support");
825 VLOG_ERR_BUF(errormsg
, "No L4 checksum support: mask: %"PRIu64
,
834 /* Check if each packet in 'batch' is compatible with 'netdev' features,
835 * otherwise either fall back to software implementation or drop it. */
837 netdev_send_prepare_batch(const struct netdev
*netdev
,
838 struct dp_packet_batch
*batch
)
840 struct dp_packet
*packet
;
841 size_t i
, size
= dp_packet_batch_size(batch
);
843 DP_PACKET_BATCH_REFILL_FOR_EACH (i
, size
, packet
, batch
) {
844 char *errormsg
= NULL
;
846 if (netdev_send_prepare_packet(netdev
->ol_flags
, packet
, &errormsg
)) {
847 dp_packet_batch_refill(batch
, packet
, i
);
849 dp_packet_delete(packet
);
850 COVERAGE_INC(netdev_send_prepare_drops
);
851 VLOG_WARN_RL(&rl
, "%s: Packet dropped: %s",
852 netdev_get_name(netdev
), errormsg
);
858 /* Sends 'batch' on 'netdev'. Returns 0 if successful (for every packet),
859 * otherwise a positive errno value. Returns EAGAIN without blocking if
860 * at least one the packets cannot be queued immediately. Returns EMSGSIZE
861 * if a partial packet was transmitted or if a packet is too big or too small
862 * to transmit on the device.
864 * The caller must make sure that 'netdev' supports sending by making sure that
865 * 'netdev_n_txq(netdev)' returns >= 1.
867 * If the function returns a non-zero value, some of the packets might have
870 * The caller transfers ownership of all the packets to the network device,
871 * regardless of success.
873 * If 'concurrent_txq' is true, the caller may perform concurrent calls
874 * to netdev_send() with the same 'qid'. The netdev provider is responsible
875 * for making sure that these concurrent calls do not create a race condition
876 * by using locking or other synchronization if required.
878 * The network device is expected to maintain one or more packet
879 * transmission queues, so that the caller does not ordinarily have to
880 * do additional queuing of packets. 'qid' specifies the queue to use
881 * and can be ignored if the implementation does not support multiple
884 netdev_send(struct netdev
*netdev
, int qid
, struct dp_packet_batch
*batch
,
889 netdev_send_prepare_batch(netdev
, batch
);
890 if (OVS_UNLIKELY(dp_packet_batch_is_empty(batch
))) {
894 error
= netdev
->netdev_class
->send(netdev
, qid
, batch
, concurrent_txq
);
896 COVERAGE_INC(netdev_sent
);
901 /* Pop tunnel header, build tunnel metadata and resize 'batch->packets'
902 * for further processing.
904 * The caller must make sure that 'netdev' support this operation by checking
905 * that netdev_has_tunnel_push_pop() returns true. */
907 netdev_pop_header(struct netdev
*netdev
, struct dp_packet_batch
*batch
)
909 struct dp_packet
*packet
;
910 size_t i
, size
= dp_packet_batch_size(batch
);
912 DP_PACKET_BATCH_REFILL_FOR_EACH (i
, size
, packet
, batch
) {
913 packet
= netdev
->netdev_class
->pop_header(packet
);
915 /* Reset the offload flags if present, to avoid wrong
916 * interpretation in the further packet processing when
918 dp_packet_reset_offload(packet
);
919 pkt_metadata_init_conn(&packet
->md
);
920 dp_packet_batch_refill(batch
, packet
, i
);
926 netdev_init_tnl_build_header_params(struct netdev_tnl_build_header_params
*params
,
927 const struct flow
*tnl_flow
,
928 const struct in6_addr
*src
,
929 struct eth_addr dmac
,
930 struct eth_addr smac
)
932 params
->flow
= tnl_flow
;
936 params
->is_ipv6
= !IN6_IS_ADDR_V4MAPPED(src
);
939 int netdev_build_header(const struct netdev
*netdev
,
940 struct ovs_action_push_tnl
*data
,
941 const struct netdev_tnl_build_header_params
*params
)
943 if (netdev
->netdev_class
->build_header
) {
944 return netdev
->netdev_class
->build_header(netdev
, data
, params
);
949 /* Push tunnel header (reading from tunnel metadata) and resize
950 * 'batch->packets' for further processing.
952 * The caller must make sure that 'netdev' support this operation by checking
953 * that netdev_has_tunnel_push_pop() returns true. */
955 netdev_push_header(const struct netdev
*netdev
,
956 struct dp_packet_batch
*batch
,
957 const struct ovs_action_push_tnl
*data
)
959 struct dp_packet
*packet
;
960 size_t i
, size
= dp_packet_batch_size(batch
);
962 DP_PACKET_BATCH_REFILL_FOR_EACH (i
, size
, packet
, batch
) {
963 if (OVS_UNLIKELY(dp_packet_hwol_is_tso(packet
)
964 || dp_packet_hwol_l4_mask(packet
))) {
965 COVERAGE_INC(netdev_push_header_drops
);
966 dp_packet_delete(packet
);
967 VLOG_WARN_RL(&rl
, "%s: Tunneling packets with HW offload flags is "
968 "not supported: packet dropped",
969 netdev_get_name(netdev
));
971 netdev
->netdev_class
->push_header(netdev
, packet
, data
);
972 pkt_metadata_init(&packet
->md
, data
->out_port
);
973 dp_packet_batch_refill(batch
, packet
, i
);
980 /* Registers with the poll loop to wake up from the next call to poll_block()
981 * when the packet transmission queue has sufficient room to transmit a packet
982 * with netdev_send().
984 * The network device is expected to maintain one or more packet
985 * transmission queues, so that the caller does not ordinarily have to
986 * do additional queuing of packets. 'qid' specifies the queue to use
987 * and can be ignored if the implementation does not support multiple
990 netdev_send_wait(struct netdev
*netdev
, int qid
)
992 if (netdev
->netdev_class
->send_wait
) {
993 netdev
->netdev_class
->send_wait(netdev
, qid
);
997 /* Attempts to set 'netdev''s MAC address to 'mac'. Returns 0 if successful,
998 * otherwise a positive errno value. */
1000 netdev_set_etheraddr(struct netdev
*netdev
, const struct eth_addr mac
)
1002 return netdev
->netdev_class
->set_etheraddr(netdev
, mac
);
1005 /* Retrieves 'netdev''s MAC address. If successful, returns 0 and copies the
1006 * the MAC address into 'mac'. On failure, returns a positive errno value and
1007 * clears 'mac' to all-zeros. */
1009 netdev_get_etheraddr(const struct netdev
*netdev
, struct eth_addr
*mac
)
1013 error
= netdev
->netdev_class
->get_etheraddr(netdev
, mac
);
1015 memset(mac
, 0, sizeof *mac
);
1020 /* Returns the name of the network device that 'netdev' represents,
1021 * e.g. "eth0". The caller must not modify or free the returned string. */
1023 netdev_get_name(const struct netdev
*netdev
)
1025 return netdev
->name
;
1028 /* Retrieves the MTU of 'netdev'. The MTU is the maximum size of transmitted
1029 * (and received) packets, in bytes, not including the hardware header; thus,
1030 * this is typically 1500 bytes for Ethernet devices.
1032 * If successful, returns 0 and stores the MTU size in '*mtup'. Returns
1033 * EOPNOTSUPP if 'netdev' does not have an MTU (as e.g. some tunnels do not).
1034 * On other failure, returns a positive errno value. On failure, sets '*mtup'
1037 netdev_get_mtu(const struct netdev
*netdev
, int *mtup
)
1039 const struct netdev_class
*class = netdev
->netdev_class
;
1042 error
= class->get_mtu
? class->get_mtu(netdev
, mtup
) : EOPNOTSUPP
;
1045 if (error
!= EOPNOTSUPP
) {
1046 VLOG_DBG_RL(&rl
, "failed to retrieve MTU for network device %s: "
1047 "%s", netdev_get_name(netdev
), ovs_strerror(error
));
1053 /* Sets the MTU of 'netdev'. The MTU is the maximum size of transmitted
1054 * (and received) packets, in bytes.
1056 * If successful, returns 0. Returns EOPNOTSUPP if 'netdev' does not have an
1057 * MTU (as e.g. some tunnels do not). On other failure, returns a positive
1060 netdev_set_mtu(struct netdev
*netdev
, int mtu
)
1062 const struct netdev_class
*class = netdev
->netdev_class
;
1065 error
= class->set_mtu
? class->set_mtu(netdev
, mtu
) : EOPNOTSUPP
;
1066 if (error
&& error
!= EOPNOTSUPP
) {
1067 VLOG_WARN_RL(&rl
, "failed to set MTU for network device %s: %s",
1068 netdev_get_name(netdev
), ovs_strerror(error
));
1074 /* If 'user_config' is true, the user wants to control 'netdev''s MTU and we
1075 * should not override it. If 'user_config' is false, we may adjust
1076 * 'netdev''s MTU (e.g., if 'netdev' is internal). */
1078 netdev_mtu_user_config(struct netdev
*netdev
, bool user_config
)
1080 if (netdev
->mtu_user_config
!= user_config
) {
1081 netdev_change_seq_changed(netdev
);
1082 netdev
->mtu_user_config
= user_config
;
1086 /* Returns 'true' if the user explicitly specified an MTU value for 'netdev'.
1087 * Otherwise, returns 'false', in which case we are allowed to adjust the
1090 netdev_mtu_is_user_config(struct netdev
*netdev
)
1092 return netdev
->mtu_user_config
;
1095 /* Returns the ifindex of 'netdev', if successful, as a positive number. On
1096 * failure, returns a negative errno value.
1098 * The desired semantics of the ifindex value are a combination of those
1099 * specified by POSIX for if_nametoindex() and by SNMP for ifIndex. An ifindex
1100 * value should be unique within a host and remain stable at least until
1101 * reboot. SNMP says an ifindex "ranges between 1 and the value of ifNumber"
1102 * but many systems do not follow this rule anyhow.
1104 * Some network devices may not implement support for this function. In such
1105 * cases this function will always return -EOPNOTSUPP.
1108 netdev_get_ifindex(const struct netdev
*netdev
)
1110 int (*get_ifindex
)(const struct netdev
*);
1112 get_ifindex
= netdev
->netdev_class
->get_ifindex
;
1114 return get_ifindex
? get_ifindex(netdev
) : -EOPNOTSUPP
;
1117 /* Stores the features supported by 'netdev' into each of '*current',
1118 * '*advertised', '*supported', and '*peer' that are non-null. Each value is a
1119 * bitmap of "enum ofp_port_features" bits, in host byte order. Returns 0 if
1120 * successful, otherwise a positive errno value. On failure, all of the
1121 * passed-in values are set to 0.
1123 * Some network devices may not implement support for this function. In such
1124 * cases this function will always return EOPNOTSUPP. */
1126 netdev_get_features(const struct netdev
*netdev
,
1127 enum netdev_features
*current
,
1128 enum netdev_features
*advertised
,
1129 enum netdev_features
*supported
,
1130 enum netdev_features
*peer
)
1132 int (*get_features
)(const struct netdev
*netdev
,
1133 enum netdev_features
*current
,
1134 enum netdev_features
*advertised
,
1135 enum netdev_features
*supported
,
1136 enum netdev_features
*peer
);
1137 enum netdev_features dummy
[4];
1141 current
= &dummy
[0];
1144 advertised
= &dummy
[1];
1147 supported
= &dummy
[2];
1153 get_features
= netdev
->netdev_class
->get_features
;
1154 error
= get_features
1155 ? get_features(netdev
, current
, advertised
, supported
,
1159 *current
= *advertised
= *supported
= *peer
= 0;
1164 /* Returns the maximum speed of a network connection that has the NETDEV_F_*
1165 * bits in 'features', in bits per second. If no bits that indicate a speed
1166 * are set in 'features', returns 'default_bps'. */
1168 netdev_features_to_bps(enum netdev_features features
,
1169 uint64_t default_bps
)
1172 F_1000000MB
= NETDEV_F_1TB_FD
,
1173 F_100000MB
= NETDEV_F_100GB_FD
,
1174 F_40000MB
= NETDEV_F_40GB_FD
,
1175 F_10000MB
= NETDEV_F_10GB_FD
,
1176 F_1000MB
= NETDEV_F_1GB_HD
| NETDEV_F_1GB_FD
,
1177 F_100MB
= NETDEV_F_100MB_HD
| NETDEV_F_100MB_FD
,
1178 F_10MB
= NETDEV_F_10MB_HD
| NETDEV_F_10MB_FD
1181 return ( features
& F_1000000MB
? UINT64_C(1000000000000)
1182 : features
& F_100000MB
? UINT64_C(100000000000)
1183 : features
& F_40000MB
? UINT64_C(40000000000)
1184 : features
& F_10000MB
? UINT64_C(10000000000)
1185 : features
& F_1000MB
? UINT64_C(1000000000)
1186 : features
& F_100MB
? UINT64_C(100000000)
1187 : features
& F_10MB
? UINT64_C(10000000)
1191 /* Returns true if any of the NETDEV_F_* bits that indicate a full-duplex link
1192 * are set in 'features', otherwise false. */
1194 netdev_features_is_full_duplex(enum netdev_features features
)
1196 return (features
& (NETDEV_F_10MB_FD
| NETDEV_F_100MB_FD
| NETDEV_F_1GB_FD
1197 | NETDEV_F_10GB_FD
| NETDEV_F_40GB_FD
1198 | NETDEV_F_100GB_FD
| NETDEV_F_1TB_FD
)) != 0;
1201 /* Set the features advertised by 'netdev' to 'advertise'. Returns 0 if
1202 * successful, otherwise a positive errno value. */
1204 netdev_set_advertisements(struct netdev
*netdev
,
1205 enum netdev_features advertise
)
1207 return (netdev
->netdev_class
->set_advertisements
1208 ? netdev
->netdev_class
->set_advertisements(
1214 netdev_feature_to_name(uint32_t bit
)
1216 enum netdev_features f
= bit
;
1219 case NETDEV_F_10MB_HD
: return "10MB-HD";
1220 case NETDEV_F_10MB_FD
: return "10MB-FD";
1221 case NETDEV_F_100MB_HD
: return "100MB-HD";
1222 case NETDEV_F_100MB_FD
: return "100MB-FD";
1223 case NETDEV_F_1GB_HD
: return "1GB-HD";
1224 case NETDEV_F_1GB_FD
: return "1GB-FD";
1225 case NETDEV_F_10GB_FD
: return "10GB-FD";
1226 case NETDEV_F_40GB_FD
: return "40GB-FD";
1227 case NETDEV_F_100GB_FD
: return "100GB-FD";
1228 case NETDEV_F_1TB_FD
: return "1TB-FD";
1229 case NETDEV_F_OTHER
: return "OTHER";
1230 case NETDEV_F_COPPER
: return "COPPER";
1231 case NETDEV_F_FIBER
: return "FIBER";
1232 case NETDEV_F_AUTONEG
: return "AUTO_NEG";
1233 case NETDEV_F_PAUSE
: return "AUTO_PAUSE";
1234 case NETDEV_F_PAUSE_ASYM
: return "AUTO_PAUSE_ASYM";
1241 netdev_features_format(struct ds
*s
, enum netdev_features features
)
1243 ofp_print_bit_names(s
, features
, netdev_feature_to_name
, ' ');
1244 ds_put_char(s
, '\n');
1247 /* Assigns 'addr' as 'netdev''s IPv4 address and 'mask' as its netmask. If
1248 * 'addr' is INADDR_ANY, 'netdev''s IPv4 address is cleared. Returns a
1249 * positive errno value. */
1251 netdev_set_in4(struct netdev
*netdev
, struct in_addr addr
, struct in_addr mask
)
1253 return (netdev
->netdev_class
->set_in4
1254 ? netdev
->netdev_class
->set_in4(netdev
, addr
, mask
)
1259 netdev_get_addresses_by_name(const char *device_name
,
1260 struct in6_addr
**addrsp
, int *n_addrsp
)
1262 struct netdev
*netdev
;
1263 int error
= netdev_open(device_name
, NULL
, &netdev
);
1270 struct in6_addr
*masks
;
1271 error
= netdev_get_addr_list(netdev
, addrsp
, &masks
, n_addrsp
);
1272 netdev_close(netdev
);
1277 /* Obtains an IPv4 address from 'device_name' and save the address in '*in4'.
1278 * Returns 0 if successful, otherwise a positive errno value. */
1280 netdev_get_in4_by_name(const char *device_name
, struct in_addr
*in4
)
1282 struct in6_addr
*addrs
;
1284 int error
= netdev_get_addresses_by_name(device_name
, &addrs
, &n
);
1289 for (int i
= 0; i
< n
; i
++) {
1290 if (IN6_IS_ADDR_V4MAPPED(&addrs
[i
])) {
1291 in4
->s_addr
= in6_addr_get_mapped_ipv4(&addrs
[i
]);
1302 /* Obtains an IPv4 or IPv6 address from 'device_name' and save the address in
1303 * '*in6', representing IPv4 addresses as v6-mapped. Returns 0 if successful,
1304 * otherwise a positive errno value. */
1306 netdev_get_ip_by_name(const char *device_name
, struct in6_addr
*in6
)
1308 struct in6_addr
*addrs
;
1310 int error
= netdev_get_addresses_by_name(device_name
, &addrs
, &n
);
1315 for (int i
= 0; i
< n
; i
++) {
1316 if (!in6_is_lla(&addrs
[i
])) {
1328 /* Adds 'router' as a default IP gateway for the TCP/IP stack that corresponds
1331 netdev_add_router(struct netdev
*netdev
, struct in_addr router
)
1333 COVERAGE_INC(netdev_add_router
);
1334 return (netdev
->netdev_class
->add_router
1335 ? netdev
->netdev_class
->add_router(netdev
, router
)
1339 /* Looks up the next hop for 'host' for the TCP/IP stack that corresponds to
1340 * 'netdev'. If a route cannot not be determined, sets '*next_hop' to 0,
1341 * '*netdev_name' to null, and returns a positive errno value. Otherwise, if a
1342 * next hop is found, stores the next hop gateway's address (0 if 'host' is on
1343 * a directly connected network) in '*next_hop' and a copy of the name of the
1344 * device to reach 'host' in '*netdev_name', and returns 0. The caller is
1345 * responsible for freeing '*netdev_name' (by calling free()). */
1347 netdev_get_next_hop(const struct netdev
*netdev
,
1348 const struct in_addr
*host
, struct in_addr
*next_hop
,
1351 int error
= (netdev
->netdev_class
->get_next_hop
1352 ? netdev
->netdev_class
->get_next_hop(
1353 host
, next_hop
, netdev_name
)
1356 next_hop
->s_addr
= 0;
1357 *netdev_name
= NULL
;
1362 /* Populates 'smap' with status information.
1364 * Populates 'smap' with 'netdev' specific status information. This
1365 * information may be used to populate the status column of the Interface table
1366 * as defined in ovs-vswitchd.conf.db(5). */
1368 netdev_get_status(const struct netdev
*netdev
, struct smap
*smap
)
1370 return (netdev
->netdev_class
->get_status
1371 ? netdev
->netdev_class
->get_status(netdev
, smap
)
1375 /* Returns all assigned IP address to 'netdev' and returns 0.
1376 * API allocates array of address and masks and set it to
1377 * '*addr' and '*mask'.
1378 * Otherwise, returns a positive errno value and sets '*addr', '*mask
1379 * and '*n_addr' to NULL.
1381 * The following error values have well-defined meanings:
1383 * - EADDRNOTAVAIL: 'netdev' has no assigned IPv6 address.
1385 * - EOPNOTSUPP: No IPv6 network stack attached to 'netdev'.
1387 * 'addr' may be null, in which case the address itself is not reported. */
1389 netdev_get_addr_list(const struct netdev
*netdev
, struct in6_addr
**addr
,
1390 struct in6_addr
**mask
, int *n_addr
)
1394 error
= (netdev
->netdev_class
->get_addr_list
1395 ? netdev
->netdev_class
->get_addr_list(netdev
, addr
, mask
, n_addr
): EOPNOTSUPP
);
1396 if (error
&& addr
) {
1405 /* On 'netdev', turns off the flags in 'off' and then turns on the flags in
1406 * 'on'. Returns 0 if successful, otherwise a positive errno value. */
1408 do_update_flags(struct netdev
*netdev
, enum netdev_flags off
,
1409 enum netdev_flags on
, enum netdev_flags
*old_flagsp
,
1410 struct netdev_saved_flags
**sfp
)
1411 OVS_EXCLUDED(netdev_mutex
)
1413 struct netdev_saved_flags
*sf
= NULL
;
1414 enum netdev_flags old_flags
;
1417 error
= netdev
->netdev_class
->update_flags(netdev
, off
& ~on
, on
,
1420 VLOG_WARN_RL(&rl
, "failed to %s flags for network device %s: %s",
1421 off
|| on
? "set" : "get", netdev_get_name(netdev
),
1422 ovs_strerror(error
));
1424 } else if ((off
|| on
) && sfp
) {
1425 enum netdev_flags new_flags
= (old_flags
& ~off
) | on
;
1426 enum netdev_flags changed_flags
= old_flags
^ new_flags
;
1427 if (changed_flags
) {
1428 ovs_mutex_lock(&netdev_mutex
);
1429 *sfp
= sf
= xmalloc(sizeof *sf
);
1430 sf
->netdev
= netdev
;
1431 ovs_list_push_front(&netdev
->saved_flags_list
, &sf
->node
);
1432 sf
->saved_flags
= changed_flags
;
1433 sf
->saved_values
= changed_flags
& new_flags
;
1436 ovs_mutex_unlock(&netdev_mutex
);
1441 *old_flagsp
= old_flags
;
1450 /* Obtains the current flags for 'netdev' and stores them into '*flagsp'.
1451 * Returns 0 if successful, otherwise a positive errno value. On failure,
1452 * stores 0 into '*flagsp'. */
1454 netdev_get_flags(const struct netdev
*netdev_
, enum netdev_flags
*flagsp
)
1456 struct netdev
*netdev
= CONST_CAST(struct netdev
*, netdev_
);
1457 return do_update_flags(netdev
, 0, 0, flagsp
, NULL
);
1460 /* Sets the flags for 'netdev' to 'flags'.
1461 * Returns 0 if successful, otherwise a positive errno value. */
1463 netdev_set_flags(struct netdev
*netdev
, enum netdev_flags flags
,
1464 struct netdev_saved_flags
**sfp
)
1466 return do_update_flags(netdev
, -1, flags
, NULL
, sfp
);
1469 /* Turns on the specified 'flags' on 'netdev':
1471 * - On success, returns 0. If 'sfp' is nonnull, sets '*sfp' to a newly
1472 * allocated 'struct netdev_saved_flags *' that may be passed to
1473 * netdev_restore_flags() to restore the original values of 'flags' on
1474 * 'netdev' (this will happen automatically at program termination if
1475 * netdev_restore_flags() is never called) , or to NULL if no flags were
1478 * - On failure, returns a positive errno value. If 'sfp' is nonnull, sets
1479 * '*sfp' to NULL. */
1481 netdev_turn_flags_on(struct netdev
*netdev
, enum netdev_flags flags
,
1482 struct netdev_saved_flags
**sfp
)
1484 return do_update_flags(netdev
, 0, flags
, NULL
, sfp
);
1487 /* Turns off the specified 'flags' on 'netdev'. See netdev_turn_flags_on() for
1488 * details of the interface. */
1490 netdev_turn_flags_off(struct netdev
*netdev
, enum netdev_flags flags
,
1491 struct netdev_saved_flags
**sfp
)
1493 return do_update_flags(netdev
, flags
, 0, NULL
, sfp
);
1496 /* Restores the flags that were saved in 'sf', and destroys 'sf'.
1497 * Does nothing if 'sf' is NULL. */
1499 netdev_restore_flags(struct netdev_saved_flags
*sf
)
1500 OVS_EXCLUDED(netdev_mutex
)
1503 struct netdev
*netdev
= sf
->netdev
;
1504 enum netdev_flags old_flags
;
1506 netdev
->netdev_class
->update_flags(netdev
,
1507 sf
->saved_flags
& sf
->saved_values
,
1508 sf
->saved_flags
& ~sf
->saved_values
,
1511 ovs_mutex_lock(&netdev_mutex
);
1512 ovs_list_remove(&sf
->node
);
1514 netdev_unref(netdev
);
1518 /* Looks up the ARP table entry for 'ip' on 'netdev'. If one exists and can be
1519 * successfully retrieved, it stores the corresponding MAC address in 'mac' and
1520 * returns 0. Otherwise, it returns a positive errno value; in particular,
1521 * ENXIO indicates that there is no ARP table entry for 'ip' on 'netdev'. */
1523 netdev_arp_lookup(const struct netdev
*netdev
,
1524 ovs_be32 ip
, struct eth_addr
*mac
)
1526 int error
= (netdev
->netdev_class
->arp_lookup
1527 ? netdev
->netdev_class
->arp_lookup(netdev
, ip
, mac
)
1530 *mac
= eth_addr_zero
;
1535 /* Returns true if carrier is active (link light is on) on 'netdev'. */
1537 netdev_get_carrier(const struct netdev
*netdev
)
1540 enum netdev_flags flags
;
1543 netdev_get_flags(netdev
, &flags
);
1544 if (!(flags
& NETDEV_UP
)) {
1548 if (!netdev
->netdev_class
->get_carrier
) {
1552 error
= netdev
->netdev_class
->get_carrier(netdev
, &carrier
);
1554 VLOG_DBG("%s: failed to get network device carrier status, assuming "
1555 "down: %s", netdev_get_name(netdev
), ovs_strerror(error
));
1562 /* Returns the number of times 'netdev''s carrier has changed. */
1564 netdev_get_carrier_resets(const struct netdev
*netdev
)
1566 return (netdev
->netdev_class
->get_carrier_resets
1567 ? netdev
->netdev_class
->get_carrier_resets(netdev
)
1571 /* Attempts to force netdev_get_carrier() to poll 'netdev''s MII registers for
1572 * link status instead of checking 'netdev''s carrier. 'netdev''s MII
1573 * registers will be polled once ever 'interval' milliseconds. If 'netdev'
1574 * does not support MII, another method may be used as a fallback. If
1575 * 'interval' is less than or equal to zero, reverts netdev_get_carrier() to
1576 * its normal behavior.
1578 * Returns 0 if successful, otherwise a positive errno value. */
1580 netdev_set_miimon_interval(struct netdev
*netdev
, long long int interval
)
1582 return (netdev
->netdev_class
->set_miimon_interval
1583 ? netdev
->netdev_class
->set_miimon_interval(netdev
, interval
)
1587 /* Retrieves current device stats for 'netdev'. */
1589 netdev_get_stats(const struct netdev
*netdev
, struct netdev_stats
*stats
)
1593 /* Statistics are initialized before passing it to particular device
1594 * implementation so all values are filtered out by default. */
1595 memset(stats
, 0xFF, sizeof *stats
);
1597 COVERAGE_INC(netdev_get_stats
);
1598 error
= (netdev
->netdev_class
->get_stats
1599 ? netdev
->netdev_class
->get_stats(netdev
, stats
)
1602 /* In case of error all statistics are filtered out */
1603 memset(stats
, 0xff, sizeof *stats
);
1608 /* Retrieves current device custom stats for 'netdev'. */
1610 netdev_get_custom_stats(const struct netdev
*netdev
,
1611 struct netdev_custom_stats
*custom_stats
)
1614 memset(custom_stats
, 0, sizeof *custom_stats
);
1615 error
= (netdev
->netdev_class
->get_custom_stats
1616 ? netdev
->netdev_class
->get_custom_stats(netdev
, custom_stats
)
1623 /* Attempts to set input rate limiting (policing) policy, such that up to
1624 * 'kbits_rate' kbps of traffic is accepted, with a maximum accumulative burst
1625 * size of 'kbits' kb. */
1627 netdev_set_policing(struct netdev
*netdev
, uint32_t kbits_rate
,
1628 uint32_t kbits_burst
)
1630 return (netdev
->netdev_class
->set_policing
1631 ? netdev
->netdev_class
->set_policing(netdev
,
1632 kbits_rate
, kbits_burst
)
1636 /* Adds to 'types' all of the forms of QoS supported by 'netdev', or leaves it
1637 * empty if 'netdev' does not support QoS. Any names added to 'types' should
1638 * be documented as valid for the "type" column in the "QoS" table in
1639 * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
1641 * Every network device supports disabling QoS with a type of "", but this type
1642 * will not be added to 'types'.
1644 * The caller must initialize 'types' (e.g. with sset_init()) before calling
1645 * this function. The caller is responsible for destroying 'types' (e.g. with
1646 * sset_destroy()) when it is no longer needed.
1648 * Returns 0 if successful, otherwise a positive errno value. */
1650 netdev_get_qos_types(const struct netdev
*netdev
, struct sset
*types
)
1652 const struct netdev_class
*class = netdev
->netdev_class
;
1653 return (class->get_qos_types
1654 ? class->get_qos_types(netdev
, types
)
1658 /* Queries 'netdev' for its capabilities regarding the specified 'type' of QoS,
1659 * which should be "" or one of the types returned by netdev_get_qos_types()
1660 * for 'netdev'. Returns 0 if successful, otherwise a positive errno value.
1661 * On success, initializes 'caps' with the QoS capabilities; on failure, clears
1662 * 'caps' to all zeros. */
1664 netdev_get_qos_capabilities(const struct netdev
*netdev
, const char *type
,
1665 struct netdev_qos_capabilities
*caps
)
1667 const struct netdev_class
*class = netdev
->netdev_class
;
1670 int retval
= (class->get_qos_capabilities
1671 ? class->get_qos_capabilities(netdev
, type
, caps
)
1674 memset(caps
, 0, sizeof *caps
);
1678 /* Every netdev supports turning off QoS. */
1679 memset(caps
, 0, sizeof *caps
);
1684 /* Obtains the number of queues supported by 'netdev' for the specified 'type'
1685 * of QoS. Returns 0 if successful, otherwise a positive errno value. Stores
1686 * the number of queues (zero on failure) in '*n_queuesp'.
1688 * This is just a simple wrapper around netdev_get_qos_capabilities(). */
1690 netdev_get_n_queues(const struct netdev
*netdev
,
1691 const char *type
, unsigned int *n_queuesp
)
1693 struct netdev_qos_capabilities caps
;
1696 retval
= netdev_get_qos_capabilities(netdev
, type
, &caps
);
1697 *n_queuesp
= caps
.n_queues
;
1701 /* Queries 'netdev' about its currently configured form of QoS. If successful,
1702 * stores the name of the current form of QoS into '*typep', stores any details
1703 * of configuration as string key-value pairs in 'details', and returns 0. On
1704 * failure, sets '*typep' to NULL and returns a positive errno value.
1706 * A '*typep' of "" indicates that QoS is currently disabled on 'netdev'.
1708 * The caller must initialize 'details' as an empty smap (e.g. with
1709 * smap_init()) before calling this function. The caller must free 'details'
1710 * when it is no longer needed (e.g. with smap_destroy()).
1712 * The caller must not modify or free '*typep'.
1714 * '*typep' will be one of the types returned by netdev_get_qos_types() for
1715 * 'netdev'. The contents of 'details' should be documented as valid for
1716 * '*typep' in the "other_config" column in the "QoS" table in
1717 * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)). */
1719 netdev_get_qos(const struct netdev
*netdev
,
1720 const char **typep
, struct smap
*details
)
1722 const struct netdev_class
*class = netdev
->netdev_class
;
1725 if (class->get_qos
) {
1726 retval
= class->get_qos(netdev
, typep
, details
);
1729 smap_clear(details
);
1733 /* 'netdev' doesn't support QoS, so report that QoS is disabled. */
1739 /* Attempts to reconfigure QoS on 'netdev', changing the form of QoS to 'type'
1740 * with details of configuration from 'details'. Returns 0 if successful,
1741 * otherwise a positive errno value. On error, the previous QoS configuration
1744 * When this function changes the type of QoS (not just 'details'), this also
1745 * resets all queue configuration for 'netdev' to their defaults (which depend
1746 * on the specific type of QoS). Otherwise, the queue configuration for
1747 * 'netdev' is unchanged.
1749 * 'type' should be "" (to disable QoS) or one of the types returned by
1750 * netdev_get_qos_types() for 'netdev'. The contents of 'details' should be
1751 * documented as valid for the given 'type' in the "other_config" column in the
1752 * "QoS" table in vswitchd/vswitch.xml (which is built as
1753 * ovs-vswitchd.conf.db(8)).
1755 * NULL may be specified for 'details' if there are no configuration
1758 netdev_set_qos(struct netdev
*netdev
,
1759 const char *type
, const struct smap
*details
)
1761 const struct netdev_class
*class = netdev
->netdev_class
;
1767 if (class->set_qos
) {
1769 static const struct smap empty
= SMAP_INITIALIZER(&empty
);
1772 return class->set_qos(netdev
, type
, details
);
1774 return *type
? EOPNOTSUPP
: 0;
1778 /* Queries 'netdev' for information about the queue numbered 'queue_id'. If
1779 * successful, adds that information as string key-value pairs to 'details'.
1780 * Returns 0 if successful, otherwise a positive errno value.
1782 * 'queue_id' must be less than the number of queues supported by 'netdev' for
1783 * the current form of QoS (e.g. as returned by netdev_get_n_queues(netdev)).
1785 * The returned contents of 'details' should be documented as valid for the
1786 * given 'type' in the "other_config" column in the "Queue" table in
1787 * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
1789 * The caller must initialize 'details' (e.g. with smap_init()) before calling
1790 * this function. The caller must free 'details' when it is no longer needed
1791 * (e.g. with smap_destroy()). */
1793 netdev_get_queue(const struct netdev
*netdev
,
1794 unsigned int queue_id
, struct smap
*details
)
1796 const struct netdev_class
*class = netdev
->netdev_class
;
1799 retval
= (class->get_queue
1800 ? class->get_queue(netdev
, queue_id
, details
)
1803 smap_clear(details
);
1808 /* Configures the queue numbered 'queue_id' on 'netdev' with the key-value
1809 * string pairs in 'details'. The contents of 'details' should be documented
1810 * as valid for the given 'type' in the "other_config" column in the "Queue"
1811 * table in vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
1812 * Returns 0 if successful, otherwise a positive errno value. On failure, the
1813 * given queue's configuration should be unmodified.
1815 * 'queue_id' must be less than the number of queues supported by 'netdev' for
1816 * the current form of QoS (e.g. as returned by netdev_get_n_queues(netdev)).
1818 * This function does not modify 'details', and the caller retains ownership of
1821 netdev_set_queue(struct netdev
*netdev
,
1822 unsigned int queue_id
, const struct smap
*details
)
1824 const struct netdev_class
*class = netdev
->netdev_class
;
1825 return (class->set_queue
1826 ? class->set_queue(netdev
, queue_id
, details
)
1830 /* Attempts to delete the queue numbered 'queue_id' from 'netdev'. Some kinds
1831 * of QoS may have a fixed set of queues, in which case attempts to delete them
1832 * will fail with EOPNOTSUPP.
1834 * Returns 0 if successful, otherwise a positive errno value. On failure, the
1835 * given queue will be unmodified.
1837 * 'queue_id' must be less than the number of queues supported by 'netdev' for
1838 * the current form of QoS (e.g. as returned by
1839 * netdev_get_n_queues(netdev)). */
1841 netdev_delete_queue(struct netdev
*netdev
, unsigned int queue_id
)
1843 const struct netdev_class
*class = netdev
->netdev_class
;
1844 return (class->delete_queue
1845 ? class->delete_queue(netdev
, queue_id
)
1849 /* Obtains statistics about 'queue_id' on 'netdev'. On success, returns 0 and
1850 * fills 'stats' with the queue's statistics; individual members of 'stats' may
1851 * be set to all-1-bits if the statistic is unavailable. On failure, returns a
1852 * positive errno value and fills 'stats' with values indicating unsupported
1855 netdev_get_queue_stats(const struct netdev
*netdev
, unsigned int queue_id
,
1856 struct netdev_queue_stats
*stats
)
1858 const struct netdev_class
*class = netdev
->netdev_class
;
1861 retval
= (class->get_queue_stats
1862 ? class->get_queue_stats(netdev
, queue_id
, stats
)
1865 stats
->tx_bytes
= UINT64_MAX
;
1866 stats
->tx_packets
= UINT64_MAX
;
1867 stats
->tx_errors
= UINT64_MAX
;
1868 stats
->created
= LLONG_MIN
;
1873 /* Initializes 'dump' to begin dumping the queues in a netdev.
1875 * This function provides no status indication. An error status for the entire
1876 * dump operation is provided when it is completed by calling
1877 * netdev_queue_dump_done().
1880 netdev_queue_dump_start(struct netdev_queue_dump
*dump
,
1881 const struct netdev
*netdev
)
1883 dump
->netdev
= netdev_ref(netdev
);
1884 if (netdev
->netdev_class
->queue_dump_start
) {
1885 dump
->error
= netdev
->netdev_class
->queue_dump_start(netdev
,
1888 dump
->error
= EOPNOTSUPP
;
1892 /* Attempts to retrieve another queue from 'dump', which must have been
1893 * initialized with netdev_queue_dump_start(). On success, stores a new queue
1894 * ID into '*queue_id', fills 'details' with configuration details for the
1895 * queue, and returns true. On failure, returns false.
1897 * Queues are not necessarily dumped in increasing order of queue ID (or any
1898 * other predictable order).
1900 * Failure might indicate an actual error or merely that the last queue has
1901 * been dumped. An error status for the entire dump operation is provided when
1902 * it is completed by calling netdev_queue_dump_done().
1904 * The returned contents of 'details' should be documented as valid for the
1905 * given 'type' in the "other_config" column in the "Queue" table in
1906 * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
1908 * The caller must initialize 'details' (e.g. with smap_init()) before calling
1909 * this function. This function will clear and replace its contents. The
1910 * caller must free 'details' when it is no longer needed (e.g. with
1911 * smap_destroy()). */
1913 netdev_queue_dump_next(struct netdev_queue_dump
*dump
,
1914 unsigned int *queue_id
, struct smap
*details
)
1916 smap_clear(details
);
1918 const struct netdev
*netdev
= dump
->netdev
;
1923 dump
->error
= netdev
->netdev_class
->queue_dump_next(netdev
, dump
->state
,
1927 netdev
->netdev_class
->queue_dump_done(netdev
, dump
->state
);
1933 /* Completes queue table dump operation 'dump', which must have been
1934 * initialized with netdev_queue_dump_start(). Returns 0 if the dump operation
1935 * was error-free, otherwise a positive errno value describing the problem. */
1937 netdev_queue_dump_done(struct netdev_queue_dump
*dump
)
1939 const struct netdev
*netdev
= dump
->netdev
;
1940 if (!dump
->error
&& netdev
->netdev_class
->queue_dump_done
) {
1941 dump
->error
= netdev
->netdev_class
->queue_dump_done(netdev
,
1944 netdev_close(dump
->netdev
);
1945 return dump
->error
== EOF
? 0 : dump
->error
;
1948 /* Iterates over all of 'netdev''s queues, calling 'cb' with the queue's ID,
1949 * its statistics, and the 'aux' specified by the caller. The order of
1950 * iteration is unspecified, but (when successful) each queue is visited
1953 * Calling this function may be more efficient than calling
1954 * netdev_get_queue_stats() for every queue.
1956 * 'cb' must not modify or free the statistics passed in.
1958 * Returns 0 if successful, otherwise a positive errno value. On error, some
1959 * configured queues may not have been included in the iteration. */
1961 netdev_dump_queue_stats(const struct netdev
*netdev
,
1962 netdev_dump_queue_stats_cb
*cb
, void *aux
)
1964 const struct netdev_class
*class = netdev
->netdev_class
;
1965 return (class->dump_queue_stats
1966 ? class->dump_queue_stats(netdev
, cb
, aux
)
1971 /* Returns the class type of 'netdev'.
1973 * The caller must not free the returned value. */
1975 netdev_get_type(const struct netdev
*netdev
)
1977 return netdev
->netdev_class
->type
;
1980 /* Returns the class associated with 'netdev'. */
1981 const struct netdev_class
*
1982 netdev_get_class(const struct netdev
*netdev
)
1984 return netdev
->netdev_class
;
1987 /* Set the type of 'dpif' this 'netdev' belongs to. */
1989 netdev_set_dpif_type(struct netdev
*netdev
, const char *type
)
1991 netdev
->dpif_type
= type
;
1994 /* Returns the type of 'dpif' this 'netdev' belongs to.
1996 * The caller must not free the returned value. */
1998 netdev_get_dpif_type(const struct netdev
*netdev
)
2000 return netdev
->dpif_type
;
2003 /* Returns the netdev with 'name' or NULL if there is none.
2005 * The caller must free the returned netdev with netdev_close(). */
2007 netdev_from_name(const char *name
)
2008 OVS_EXCLUDED(netdev_mutex
)
2010 struct netdev
*netdev
;
2012 ovs_mutex_lock(&netdev_mutex
);
2013 netdev
= shash_find_data(&netdev_shash
, name
);
2017 ovs_mutex_unlock(&netdev_mutex
);
2022 /* Fills 'device_list' with devices that match 'netdev_class'.
2024 * The caller is responsible for initializing and destroying 'device_list' and
2025 * must close each device on the list. */
2027 netdev_get_devices(const struct netdev_class
*netdev_class
,
2028 struct shash
*device_list
)
2029 OVS_EXCLUDED(netdev_mutex
)
2031 struct shash_node
*node
;
2033 ovs_mutex_lock(&netdev_mutex
);
2034 SHASH_FOR_EACH (node
, &netdev_shash
) {
2035 struct netdev
*dev
= node
->data
;
2037 if (dev
->netdev_class
== netdev_class
) {
2039 shash_add(device_list
, node
->name
, node
->data
);
2042 ovs_mutex_unlock(&netdev_mutex
);
2045 /* Extracts pointers to all 'netdev-vports' into an array 'vports'
2046 * and returns it. Stores the size of the array into '*size'.
2048 * The caller is responsible for freeing 'vports' and must close
2049 * each 'netdev-vport' in the list. */
2051 netdev_get_vports(size_t *size
)
2052 OVS_EXCLUDED(netdev_mutex
)
2054 struct netdev
**vports
;
2055 struct shash_node
*node
;
2062 /* Explicitly allocates big enough chunk of memory. */
2063 ovs_mutex_lock(&netdev_mutex
);
2064 vports
= xmalloc(shash_count(&netdev_shash
) * sizeof *vports
);
2065 SHASH_FOR_EACH (node
, &netdev_shash
) {
2066 struct netdev
*dev
= node
->data
;
2068 if (netdev_vport_is_vport_class(dev
->netdev_class
)) {
2074 ovs_mutex_unlock(&netdev_mutex
);
2081 netdev_get_type_from_name(const char *name
)
2085 type
= netdev_vport_type_from_name(name
);
2087 dev
= netdev_from_name(name
);
2088 type
= dev
? netdev_get_type(dev
) : NULL
;
2095 netdev_rxq_get_netdev(const struct netdev_rxq
*rx
)
2097 ovs_assert(rx
->netdev
->ref_cnt
> 0);
2102 netdev_rxq_get_name(const struct netdev_rxq
*rx
)
2104 return netdev_get_name(netdev_rxq_get_netdev(rx
));
2108 netdev_rxq_get_queue_id(const struct netdev_rxq
*rx
)
2110 return rx
->queue_id
;
2114 restore_all_flags(void *aux OVS_UNUSED
)
2116 struct shash_node
*node
;
2118 SHASH_FOR_EACH (node
, &netdev_shash
) {
2119 struct netdev
*netdev
= node
->data
;
2120 const struct netdev_saved_flags
*sf
;
2121 enum netdev_flags saved_values
;
2122 enum netdev_flags saved_flags
;
2124 saved_values
= saved_flags
= 0;
2125 LIST_FOR_EACH (sf
, node
, &netdev
->saved_flags_list
) {
2126 saved_flags
|= sf
->saved_flags
;
2127 saved_values
&= ~sf
->saved_flags
;
2128 saved_values
|= sf
->saved_flags
& sf
->saved_values
;
2131 enum netdev_flags old_flags
;
2133 netdev
->netdev_class
->update_flags(netdev
,
2134 saved_flags
& saved_values
,
2135 saved_flags
& ~saved_values
,
2139 if (netdev
->netdev_class
== &netdev_afxdp_class
) {
2140 signal_remove_xdp(netdev
);
2147 netdev_get_change_seq(const struct netdev
*netdev
)
2149 uint64_t change_seq
;
2151 atomic_read_explicit(&CONST_CAST(struct netdev
*, netdev
)->change_seq
,
2152 &change_seq
, memory_order_acquire
);
2158 /* This implementation is shared by Linux and BSD. */
2160 static struct ifaddrs
*if_addr_list
;
2161 static struct ovs_mutex if_addr_list_lock
= OVS_MUTEX_INITIALIZER
;
2164 netdev_get_addrs_list_flush(void)
2166 ovs_mutex_lock(&if_addr_list_lock
);
2168 freeifaddrs(if_addr_list
);
2169 if_addr_list
= NULL
;
2171 ovs_mutex_unlock(&if_addr_list_lock
);
2175 netdev_get_addrs(const char dev
[], struct in6_addr
**paddr
,
2176 struct in6_addr
**pmask
, int *n_in
)
2178 struct in6_addr
*addr_array
, *mask_array
;
2179 const struct ifaddrs
*ifa
;
2183 ovs_mutex_lock(&if_addr_list_lock
);
2184 if (!if_addr_list
) {
2188 err
= getifaddrs(&if_addr_list
);
2190 ovs_mutex_unlock(&if_addr_list_lock
);
2196 for (ifa
= if_addr_list
; ifa
; ifa
= ifa
->ifa_next
) {
2197 if (!ifa
->ifa_name
) {
2199 /* Older versions of glibc have a bug on race condition with
2200 * address addition which may cause one of the returned
2201 * ifa_name values to be NULL. In such case, we know that we've
2202 * got an inconsistent dump. Retry but beware of an endless
2203 * loop. From glibc 2.28 and beyond, this workaround is not
2204 * needed and should be eventually removed. */
2205 freeifaddrs(if_addr_list
);
2208 VLOG_WARN("Proceeding with an inconsistent dump of "
2209 "interfaces from the kernel. Some may be missing");
2212 if (ifa
->ifa_addr
&& ifa
->ifa_name
&& ifa
->ifa_netmask
) {
2215 family
= ifa
->ifa_addr
->sa_family
;
2216 if (family
== AF_INET
|| family
== AF_INET6
) {
2217 if (!strncmp(ifa
->ifa_name
, dev
, IFNAMSIZ
)) {
2225 ovs_mutex_unlock(&if_addr_list_lock
);
2226 return EADDRNOTAVAIL
;
2228 addr_array
= xzalloc(sizeof *addr_array
* cnt
);
2229 mask_array
= xzalloc(sizeof *mask_array
* cnt
);
2230 for (ifa
= if_addr_list
; ifa
; ifa
= ifa
->ifa_next
) {
2234 && !strncmp(ifa
->ifa_name
, dev
, IFNAMSIZ
)
2235 && sa_is_ip(ifa
->ifa_addr
)) {
2236 addr_array
[i
] = sa_get_address(ifa
->ifa_addr
);
2237 mask_array
[i
] = sa_get_address(ifa
->ifa_netmask
);
2241 ovs_mutex_unlock(&if_addr_list_lock
);
2244 *paddr
= addr_array
;
2245 *pmask
= mask_array
;
2255 netdev_wait_reconf_required(struct netdev
*netdev
)
2257 seq_wait(netdev
->reconfigure_seq
, netdev
->last_reconfigure_seq
);
2261 netdev_is_reconf_required(struct netdev
*netdev
)
2263 return seq_read(netdev
->reconfigure_seq
) != netdev
->last_reconfigure_seq
;
2266 /* Give a chance to 'netdev' to reconfigure some of its parameters.
2268 * If a module uses netdev_send() and netdev_rxq_recv(), it must call this
2269 * function when netdev_is_reconf_required() returns true.
2271 * Return 0 if successful, otherwise a positive errno value. If the
2272 * reconfiguration fails the netdev will not be able to send or receive
2275 * When this function is called, no call to netdev_rxq_recv() or netdev_send()
2276 * must be issued. */
2278 netdev_reconfigure(struct netdev
*netdev
)
2280 const struct netdev_class
*class = netdev
->netdev_class
;
2282 netdev
->last_reconfigure_seq
= seq_read(netdev
->reconfigure_seq
);
2284 return (class->reconfigure
2285 ? class->reconfigure(netdev
)
2290 netdev_free_custom_stats_counters(struct netdev_custom_stats
*custom_stats
)
2293 if (custom_stats
->counters
) {
2294 free(custom_stats
->counters
);
2295 custom_stats
->counters
= NULL
;
2296 custom_stats
->size
= 0;