2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 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.
22 #include <netinet/in.h>
30 #include <sys/ioctl.h>
31 #include <sys/types.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-dpdk.h"
43 #include "netdev-provider.h"
44 #include "netdev-vport.h"
45 #include "odp-netlink.h"
46 #include "openflow/openflow.h"
48 #include "poll-loop.h"
50 #include "openvswitch/shash.h"
54 #include "openvswitch/vlog.h"
58 VLOG_DEFINE_THIS_MODULE(netdev
);
60 COVERAGE_DEFINE(netdev_received
);
61 COVERAGE_DEFINE(netdev_sent
);
62 COVERAGE_DEFINE(netdev_add_router
);
63 COVERAGE_DEFINE(netdev_get_stats
);
65 struct netdev_saved_flags
{
66 struct netdev
*netdev
;
67 struct ovs_list node
; /* In struct netdev's saved_flags_list. */
68 enum netdev_flags saved_flags
;
69 enum netdev_flags saved_values
;
72 /* Protects 'netdev_shash' and the mutable members of struct netdev. */
73 static struct ovs_mutex netdev_mutex
= OVS_MUTEX_INITIALIZER
;
75 /* All created network devices. */
76 static struct shash netdev_shash
OVS_GUARDED_BY(netdev_mutex
)
77 = SHASH_INITIALIZER(&netdev_shash
);
79 /* Mutual exclusion of */
80 static struct ovs_mutex netdev_class_mutex
OVS_ACQ_BEFORE(netdev_mutex
)
81 = OVS_MUTEX_INITIALIZER
;
83 /* Contains 'struct netdev_registered_class'es. */
84 static struct cmap netdev_classes
= CMAP_INITIALIZER
;
86 struct netdev_registered_class
{
87 struct cmap_node cmap_node
; /* In 'netdev_classes', by class->type. */
88 const struct netdev_class
*class;
90 /* Number of references: one for the class itself and one for every
91 * instance of the class. */
92 struct ovs_refcount refcnt
;
95 /* This is set pretty low because we probably won't learn anything from the
96 * additional log messages. */
97 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 20);
99 static void restore_all_flags(void *aux OVS_UNUSED
);
100 void update_device_args(struct netdev
*, const struct shash
*args
);
103 netdev_n_txq(const struct netdev
*netdev
)
105 return netdev
->n_txq
;
109 netdev_n_rxq(const struct netdev
*netdev
)
111 return netdev
->n_rxq
;
115 netdev_is_pmd(const struct netdev
*netdev
)
117 return netdev
->netdev_class
->is_pmd
;
121 netdev_initialize(void)
122 OVS_EXCLUDED(netdev_mutex
)
124 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
126 if (ovsthread_once_start(&once
)) {
127 fatal_signal_add_hook(restore_all_flags
, NULL
, NULL
, true);
129 netdev_vport_patch_register();
132 netdev_register_provider(&netdev_linux_class
);
133 netdev_register_provider(&netdev_internal_class
);
134 netdev_register_provider(&netdev_tap_class
);
135 netdev_vport_tunnel_register();
137 #if defined(__FreeBSD__) || defined(__NetBSD__)
138 netdev_register_provider(&netdev_tap_class
);
139 netdev_register_provider(&netdev_bsd_class
);
142 netdev_register_provider(&netdev_windows_class
);
143 netdev_register_provider(&netdev_internal_class
);
144 netdev_vport_tunnel_register();
146 ovsthread_once_done(&once
);
150 /* Performs periodic work needed by all the various kinds of netdevs.
152 * If your program opens any netdevs, it must call this function within its
156 OVS_EXCLUDED(netdev_mutex
)
160 struct netdev_registered_class
*rc
;
161 CMAP_FOR_EACH (rc
, cmap_node
, &netdev_classes
) {
162 if (rc
->class->run
) {
168 /* Arranges for poll_block() to wake up when netdev_run() needs to be called.
170 * If your program opens any netdevs, it must call this function within its
174 OVS_EXCLUDED(netdev_mutex
)
178 struct netdev_registered_class
*rc
;
179 CMAP_FOR_EACH (rc
, cmap_node
, &netdev_classes
) {
180 if (rc
->class->wait
) {
186 static struct netdev_registered_class
*
187 netdev_lookup_class(const char *type
)
189 struct netdev_registered_class
*rc
;
190 CMAP_FOR_EACH_WITH_HASH (rc
, cmap_node
, hash_string(type
, 0),
192 if (!strcmp(type
, rc
->class->type
)) {
199 /* Initializes and registers a new netdev provider. After successful
200 * registration, new netdevs of that type can be opened using netdev_open(). */
202 netdev_register_provider(const struct netdev_class
*new_class
)
203 OVS_EXCLUDED(netdev_class_mutex
, netdev_mutex
)
207 ovs_mutex_lock(&netdev_class_mutex
);
208 if (netdev_lookup_class(new_class
->type
)) {
209 VLOG_WARN("attempted to register duplicate netdev provider: %s",
213 error
= new_class
->init
? new_class
->init() : 0;
215 struct netdev_registered_class
*rc
;
217 rc
= xmalloc(sizeof *rc
);
218 cmap_insert(&netdev_classes
, &rc
->cmap_node
,
219 hash_string(new_class
->type
, 0));
220 rc
->class = new_class
;
221 ovs_refcount_init(&rc
->refcnt
);
223 VLOG_ERR("failed to initialize %s network device class: %s",
224 new_class
->type
, ovs_strerror(error
));
227 ovs_mutex_unlock(&netdev_class_mutex
);
232 /* Unregisters a netdev provider. 'type' must have been previously registered
233 * and not currently be in use by any netdevs. After unregistration new
234 * netdevs of that type cannot be opened using netdev_open(). (However, the
235 * provider may still be accessible from other threads until the next RCU grace
236 * period, so the caller must not free or re-register the same netdev_class
237 * until that has passed.) */
239 netdev_unregister_provider(const char *type
)
240 OVS_EXCLUDED(netdev_class_mutex
, netdev_mutex
)
242 struct netdev_registered_class
*rc
;
247 ovs_mutex_lock(&netdev_class_mutex
);
248 rc
= netdev_lookup_class(type
);
250 VLOG_WARN("attempted to unregister a netdev provider that is not "
251 "registered: %s", type
);
252 error
= EAFNOSUPPORT
;
253 } else if (ovs_refcount_unref(&rc
->refcnt
) != 1) {
254 ovs_refcount_ref(&rc
->refcnt
);
255 VLOG_WARN("attempted to unregister in use netdev provider: %s",
259 cmap_remove(&netdev_classes
, &rc
->cmap_node
,
260 hash_string(rc
->class->type
, 0));
261 ovsrcu_postpone(free
, rc
);
264 ovs_mutex_unlock(&netdev_class_mutex
);
269 /* Clears 'types' and enumerates the types of all currently registered netdev
270 * providers into it. The caller must first initialize the sset. */
272 netdev_enumerate_types(struct sset
*types
)
273 OVS_EXCLUDED(netdev_mutex
)
278 struct netdev_registered_class
*rc
;
279 CMAP_FOR_EACH (rc
, cmap_node
, &netdev_classes
) {
280 sset_add(types
, rc
->class->type
);
284 /* Check that the network device name is not the same as any of the registered
285 * vport providers' dpif_port name (dpif_port is NULL if the vport provider
286 * does not define it) or the datapath internal port name (e.g. ovs-system).
288 * Returns true if there is a name conflict, false otherwise. */
290 netdev_is_reserved_name(const char *name
)
291 OVS_EXCLUDED(netdev_mutex
)
295 struct netdev_registered_class
*rc
;
296 CMAP_FOR_EACH (rc
, cmap_node
, &netdev_classes
) {
297 const char *dpif_port
= netdev_vport_class_get_dpif_port(rc
->class);
298 if (dpif_port
&& !strncmp(name
, dpif_port
, strlen(dpif_port
))) {
303 if (!strncmp(name
, "ovs-", 4)) {
308 dp_enumerate_types(&types
);
309 SSET_FOR_EACH (type
, &types
) {
310 if (!strcmp(name
+4, type
)) {
311 sset_destroy(&types
);
315 sset_destroy(&types
);
321 /* Opens the network device named 'name' (e.g. "eth0") of the specified 'type'
322 * (e.g. "system") and returns zero if successful, otherwise a positive errno
323 * value. On success, sets '*netdevp' to the new network device, otherwise to
326 * Some network devices may need to be configured (with netdev_set_config())
327 * before they can be used. */
329 netdev_open(const char *name
, const char *type
, struct netdev
**netdevp
)
330 OVS_EXCLUDED(netdev_mutex
)
332 struct netdev
*netdev
;
337 ovs_mutex_lock(&netdev_mutex
);
338 netdev
= shash_find_data(&netdev_shash
, name
);
340 struct netdev_registered_class
*rc
;
342 type
= type
&& type
[0] ? type
: "system";
343 rc
= netdev_lookup_class(type
);
344 if (rc
&& ovs_refcount_try_ref_rcu(&rc
->refcnt
)) {
345 netdev
= rc
->class->alloc();
347 memset(netdev
, 0, sizeof *netdev
);
348 netdev
->netdev_class
= rc
->class;
349 netdev
->name
= xstrdup(name
);
350 netdev
->change_seq
= 1;
351 netdev
->reconfigure_seq
= seq_create();
352 netdev
->last_reconfigure_seq
=
353 seq_read(netdev
->reconfigure_seq
);
354 netdev
->node
= shash_add(&netdev_shash
, name
, netdev
);
356 /* By default enable one tx and rx queue per netdev. */
357 netdev
->n_txq
= netdev
->netdev_class
->send
? 1 : 0;
358 netdev
->n_rxq
= netdev
->netdev_class
->rxq_alloc
? 1 : 0;
360 ovs_list_init(&netdev
->saved_flags_list
);
362 error
= rc
->class->construct(netdev
);
364 netdev_change_seq_changed(netdev
);
366 ovs_refcount_unref(&rc
->refcnt
);
368 ovs_assert(ovs_list_is_empty(&netdev
->saved_flags_list
));
369 shash_delete(&netdev_shash
, netdev
->node
);
370 rc
->class->dealloc(netdev
);
376 VLOG_WARN("could not create netdev %s of unknown type %s",
378 error
= EAFNOSUPPORT
;
380 } else if (type
&& strcmp(type
, netdev_get_type(netdev
))) {
381 VLOG_WARN("trying to create netdev %s of different type %s,"
383 name
, type
, netdev_get_type(netdev
));
395 ovs_mutex_unlock(&netdev_mutex
);
400 /* Returns a reference to 'netdev_' for the caller to own. Returns null if
401 * 'netdev_' is null. */
403 netdev_ref(const struct netdev
*netdev_
)
404 OVS_EXCLUDED(netdev_mutex
)
406 struct netdev
*netdev
= CONST_CAST(struct netdev
*, netdev_
);
409 ovs_mutex_lock(&netdev_mutex
);
410 ovs_assert(netdev
->ref_cnt
> 0);
412 ovs_mutex_unlock(&netdev_mutex
);
417 /* Reconfigures the device 'netdev' with 'args'. 'args' may be empty
418 * or NULL if none are needed. */
420 netdev_set_config(struct netdev
*netdev
, const struct smap
*args
, char **errp
)
421 OVS_EXCLUDED(netdev_mutex
)
423 if (netdev
->netdev_class
->set_config
) {
424 const struct smap no_args
= SMAP_INITIALIZER(&no_args
);
427 error
= netdev
->netdev_class
->set_config(netdev
,
428 args
? args
: &no_args
);
430 VLOG_WARN_BUF(errp
, "%s: could not set configuration (%s)",
431 netdev_get_name(netdev
), ovs_strerror(error
));
434 } else if (args
&& !smap_is_empty(args
)) {
435 VLOG_WARN_BUF(errp
, "%s: arguments provided to device that is not configurable",
436 netdev_get_name(netdev
));
441 /* Returns the current configuration for 'netdev' in 'args'. The caller must
442 * have already initialized 'args' with smap_init(). Returns 0 on success, in
443 * which case 'args' will be filled with 'netdev''s configuration. On failure
444 * returns a positive errno value, in which case 'args' will be empty.
446 * The caller owns 'args' and its contents and must eventually free them with
449 netdev_get_config(const struct netdev
*netdev
, struct smap
*args
)
450 OVS_EXCLUDED(netdev_mutex
)
455 if (netdev
->netdev_class
->get_config
) {
456 error
= netdev
->netdev_class
->get_config(netdev
, args
);
467 const struct netdev_tunnel_config
*
468 netdev_get_tunnel_config(const struct netdev
*netdev
)
469 OVS_EXCLUDED(netdev_mutex
)
471 if (netdev
->netdev_class
->get_tunnel_config
) {
472 return netdev
->netdev_class
->get_tunnel_config(netdev
);
478 /* Returns the id of the numa node the 'netdev' is on. If the function
479 * is not implemented, returns NETDEV_NUMA_UNSPEC. */
481 netdev_get_numa_id(const struct netdev
*netdev
)
483 if (netdev
->netdev_class
->get_numa_id
) {
484 return netdev
->netdev_class
->get_numa_id(netdev
);
486 return NETDEV_NUMA_UNSPEC
;
491 netdev_unref(struct netdev
*dev
)
492 OVS_RELEASES(netdev_mutex
)
494 ovs_assert(dev
->ref_cnt
);
495 if (!--dev
->ref_cnt
) {
496 const struct netdev_class
*class = dev
->netdev_class
;
497 struct netdev_registered_class
*rc
;
499 dev
->netdev_class
->destruct(dev
);
502 shash_delete(&netdev_shash
, dev
->node
);
505 seq_destroy(dev
->reconfigure_seq
);
506 dev
->netdev_class
->dealloc(dev
);
507 ovs_mutex_unlock(&netdev_mutex
);
509 rc
= netdev_lookup_class(class->type
);
510 ovs_refcount_unref(&rc
->refcnt
);
512 ovs_mutex_unlock(&netdev_mutex
);
516 /* Closes and destroys 'netdev'. */
518 netdev_close(struct netdev
*netdev
)
519 OVS_EXCLUDED(netdev_mutex
)
522 ovs_mutex_lock(&netdev_mutex
);
523 netdev_unref(netdev
);
527 /* Removes 'netdev' from the global shash and unrefs 'netdev'.
529 * This allows handler and revalidator threads to still retain references
530 * to this netdev while the main thread changes interface configuration.
532 * This function should only be called by the main thread when closing
533 * netdevs during user configuration changes. Otherwise, netdev_close should be
534 * used to close netdevs. */
536 netdev_remove(struct netdev
*netdev
)
539 ovs_mutex_lock(&netdev_mutex
);
541 shash_delete(&netdev_shash
, netdev
->node
);
543 netdev_change_seq_changed(netdev
);
545 netdev_unref(netdev
);
549 /* Parses 'netdev_name_', which is of the form [type@]name into its component
550 * pieces. 'name' and 'type' must be freed by the caller. */
552 netdev_parse_name(const char *netdev_name_
, char **name
, char **type
)
554 char *netdev_name
= xstrdup(netdev_name_
);
557 separator
= strchr(netdev_name
, '@');
561 *name
= xstrdup(separator
+ 1);
564 *type
= xstrdup("system");
568 /* Attempts to open a netdev_rxq handle for obtaining packets received on
569 * 'netdev'. On success, returns 0 and stores a nonnull 'netdev_rxq *' into
570 * '*rxp'. On failure, returns a positive errno value and stores NULL into
573 * Some kinds of network devices might not support receiving packets. This
574 * function returns EOPNOTSUPP in that case.*/
576 netdev_rxq_open(struct netdev
*netdev
, struct netdev_rxq
**rxp
, int id
)
577 OVS_EXCLUDED(netdev_mutex
)
581 if (netdev
->netdev_class
->rxq_alloc
&& id
< netdev
->n_rxq
) {
582 struct netdev_rxq
*rx
= netdev
->netdev_class
->rxq_alloc();
586 error
= netdev
->netdev_class
->rxq_construct(rx
);
592 netdev
->netdev_class
->rxq_dealloc(rx
);
606 netdev_rxq_close(struct netdev_rxq
*rx
)
607 OVS_EXCLUDED(netdev_mutex
)
610 struct netdev
*netdev
= rx
->netdev
;
611 netdev
->netdev_class
->rxq_destruct(rx
);
612 netdev
->netdev_class
->rxq_dealloc(rx
);
613 netdev_close(netdev
);
617 /* Attempts to receive a batch of packets from 'rx'. 'batch' should point to
618 * the beginning of an array of NETDEV_MAX_BURST pointers to dp_packet. If
619 * successful, this function stores pointers to up to NETDEV_MAX_BURST
620 * dp_packets into the array, transferring ownership of the packets to the
621 * caller, stores the number of received packets in 'batch->count', and returns
624 * The implementation does not necessarily initialize any non-data members of
625 * 'batch'. That is, the caller must initialize layer pointers and metadata
626 * itself, if desired, e.g. with pkt_metadata_init() and miniflow_extract().
628 * Returns EAGAIN immediately if no packet is ready to be received or another
629 * positive errno value if an error was encountered. */
631 netdev_rxq_recv(struct netdev_rxq
*rx
, struct dp_packet_batch
*batch
)
635 retval
= rx
->netdev
->netdev_class
->rxq_recv(rx
, batch
);
637 COVERAGE_INC(netdev_received
);
644 /* Arranges for poll_block() to wake up when a packet is ready to be received
647 netdev_rxq_wait(struct netdev_rxq
*rx
)
649 rx
->netdev
->netdev_class
->rxq_wait(rx
);
652 /* Discards any packets ready to be received on 'rx'. */
654 netdev_rxq_drain(struct netdev_rxq
*rx
)
656 return (rx
->netdev
->netdev_class
->rxq_drain
657 ? rx
->netdev
->netdev_class
->rxq_drain(rx
)
661 /* Configures the number of tx queues of 'netdev'. Returns 0 if successful,
662 * otherwise a positive errno value.
664 * 'n_txq' specifies the exact number of transmission queues to create.
666 * The change might not effective immediately. The caller must check if a
667 * reconfiguration is required with netdev_is_reconf_required() and eventually
668 * call netdev_reconfigure() before using the new queues.
670 * On error, the tx queue configuration is unchanged */
672 netdev_set_tx_multiq(struct netdev
*netdev
, unsigned int n_txq
)
676 error
= (netdev
->netdev_class
->set_tx_multiq
677 ? netdev
->netdev_class
->set_tx_multiq(netdev
, MAX(n_txq
, 1))
680 if (error
&& error
!= EOPNOTSUPP
) {
681 VLOG_DBG_RL(&rl
, "failed to set tx queue for network device %s:"
682 "%s", netdev_get_name(netdev
), ovs_strerror(error
));
688 /* Sends 'batch' on 'netdev'. Returns 0 if successful (for every packet),
689 * otherwise a positive errno value. Returns EAGAIN without blocking if
690 * at least one the packets cannot be queued immediately. Returns EMSGSIZE
691 * if a partial packet was transmitted or if a packet is too big or too small
692 * to transmit on the device.
694 * If the function returns a non-zero value, some of the packets might have
697 * If 'may_steal' is false, the caller retains ownership of all the packets.
698 * If 'may_steal' is true, the caller transfers ownership of all the packets
699 * to the network device, regardless of success.
701 * If 'concurrent_txq' is true, the caller may perform concurrent calls
702 * to netdev_send() with the same 'qid'. The netdev provider is responsible
703 * for making sure that these concurrent calls do not create a race condition
704 * by using locking or other synchronization if required.
706 * The network device is expected to maintain one or more packet
707 * transmission queues, so that the caller does not ordinarily have to
708 * do additional queuing of packets. 'qid' specifies the queue to use
709 * and can be ignored if the implementation does not support multiple
712 * Some network devices may not implement support for this function. In such
713 * cases this function will always return EOPNOTSUPP. */
715 netdev_send(struct netdev
*netdev
, int qid
, struct dp_packet_batch
*batch
,
716 bool may_steal
, bool concurrent_txq
)
718 if (!netdev
->netdev_class
->send
) {
719 dp_packet_delete_batch(batch
, may_steal
);
723 int error
= netdev
->netdev_class
->send(netdev
, qid
, batch
, may_steal
,
726 COVERAGE_INC(netdev_sent
);
728 dp_packet_batch_reset_cutlen(batch
);
735 netdev_pop_header(struct netdev
*netdev
, struct dp_packet_batch
*batch
)
738 struct dp_packet
**buffers
= batch
->packets
;
740 if (!netdev
->netdev_class
->pop_header
) {
741 dp_packet_delete_batch(batch
, true);
746 for (i
= 0; i
< batch
->count
; i
++) {
747 buffers
[i
] = netdev
->netdev_class
->pop_header(buffers
[i
]);
749 buffers
[n_cnt
++] = buffers
[i
];
752 batch
->count
= n_cnt
;
756 netdev_init_tnl_build_header_params(struct netdev_tnl_build_header_params
*params
,
757 const struct flow
*tnl_flow
,
758 const struct in6_addr
*src
,
759 struct eth_addr dmac
,
760 struct eth_addr smac
)
762 params
->flow
= tnl_flow
;
766 params
->is_ipv6
= !IN6_IS_ADDR_V4MAPPED(src
);
769 int netdev_build_header(const struct netdev
*netdev
,
770 struct ovs_action_push_tnl
*data
,
771 const struct netdev_tnl_build_header_params
*params
)
773 if (netdev
->netdev_class
->build_header
) {
774 return netdev
->netdev_class
->build_header(netdev
, data
, params
);
780 netdev_push_header(const struct netdev
*netdev
,
781 struct dp_packet_batch
*batch
,
782 const struct ovs_action_push_tnl
*data
)
786 if (!netdev
->netdev_class
->push_header
) {
790 for (i
= 0; i
< batch
->count
; i
++) {
791 netdev
->netdev_class
->push_header(batch
->packets
[i
], data
);
792 pkt_metadata_init(&batch
->packets
[i
]->md
, u32_to_odp(data
->out_port
));
798 /* Registers with the poll loop to wake up from the next call to poll_block()
799 * when the packet transmission queue has sufficient room to transmit a packet
800 * with netdev_send().
802 * The network device is expected to maintain one or more packet
803 * transmission queues, so that the caller does not ordinarily have to
804 * do additional queuing of packets. 'qid' specifies the queue to use
805 * and can be ignored if the implementation does not support multiple
808 netdev_send_wait(struct netdev
*netdev
, int qid
)
810 if (netdev
->netdev_class
->send_wait
) {
811 netdev
->netdev_class
->send_wait(netdev
, qid
);
815 /* Attempts to set 'netdev''s MAC address to 'mac'. Returns 0 if successful,
816 * otherwise a positive errno value. */
818 netdev_set_etheraddr(struct netdev
*netdev
, const struct eth_addr mac
)
820 return netdev
->netdev_class
->set_etheraddr(netdev
, mac
);
823 /* Retrieves 'netdev''s MAC address. If successful, returns 0 and copies the
824 * the MAC address into 'mac'. On failure, returns a positive errno value and
825 * clears 'mac' to all-zeros. */
827 netdev_get_etheraddr(const struct netdev
*netdev
, struct eth_addr
*mac
)
829 return netdev
->netdev_class
->get_etheraddr(netdev
, mac
);
832 /* Returns the name of the network device that 'netdev' represents,
833 * e.g. "eth0". The caller must not modify or free the returned string. */
835 netdev_get_name(const struct netdev
*netdev
)
840 /* Retrieves the MTU of 'netdev'. The MTU is the maximum size of transmitted
841 * (and received) packets, in bytes, not including the hardware header; thus,
842 * this is typically 1500 bytes for Ethernet devices.
844 * If successful, returns 0 and stores the MTU size in '*mtup'. Returns
845 * EOPNOTSUPP if 'netdev' does not have an MTU (as e.g. some tunnels do not).
846 * On other failure, returns a positive errno value. On failure, sets '*mtup'
849 netdev_get_mtu(const struct netdev
*netdev
, int *mtup
)
851 const struct netdev_class
*class = netdev
->netdev_class
;
854 error
= class->get_mtu
? class->get_mtu(netdev
, mtup
) : EOPNOTSUPP
;
857 if (error
!= EOPNOTSUPP
) {
858 VLOG_DBG_RL(&rl
, "failed to retrieve MTU for network device %s: "
859 "%s", netdev_get_name(netdev
), ovs_strerror(error
));
865 /* Sets the MTU of 'netdev'. The MTU is the maximum size of transmitted
866 * (and received) packets, in bytes.
868 * If successful, returns 0. Returns EOPNOTSUPP if 'netdev' does not have an
869 * MTU (as e.g. some tunnels do not). On other failure, returns a positive
872 netdev_set_mtu(const struct netdev
*netdev
, int mtu
)
874 const struct netdev_class
*class = netdev
->netdev_class
;
877 error
= class->set_mtu
? class->set_mtu(netdev
, mtu
) : EOPNOTSUPP
;
878 if (error
&& error
!= EOPNOTSUPP
) {
879 VLOG_DBG_RL(&rl
, "failed to set MTU for network device %s: %s",
880 netdev_get_name(netdev
), ovs_strerror(error
));
886 /* Returns the ifindex of 'netdev', if successful, as a positive number. On
887 * failure, returns a negative errno value.
889 * The desired semantics of the ifindex value are a combination of those
890 * specified by POSIX for if_nametoindex() and by SNMP for ifIndex. An ifindex
891 * value should be unique within a host and remain stable at least until
892 * reboot. SNMP says an ifindex "ranges between 1 and the value of ifNumber"
893 * but many systems do not follow this rule anyhow.
895 * Some network devices may not implement support for this function. In such
896 * cases this function will always return -EOPNOTSUPP.
899 netdev_get_ifindex(const struct netdev
*netdev
)
901 int (*get_ifindex
)(const struct netdev
*);
903 get_ifindex
= netdev
->netdev_class
->get_ifindex
;
905 return get_ifindex
? get_ifindex(netdev
) : -EOPNOTSUPP
;
908 /* Stores the features supported by 'netdev' into each of '*current',
909 * '*advertised', '*supported', and '*peer' that are non-null. Each value is a
910 * bitmap of "enum ofp_port_features" bits, in host byte order. Returns 0 if
911 * successful, otherwise a positive errno value. On failure, all of the
912 * passed-in values are set to 0.
914 * Some network devices may not implement support for this function. In such
915 * cases this function will always return EOPNOTSUPP. */
917 netdev_get_features(const struct netdev
*netdev
,
918 enum netdev_features
*current
,
919 enum netdev_features
*advertised
,
920 enum netdev_features
*supported
,
921 enum netdev_features
*peer
)
923 int (*get_features
)(const struct netdev
*netdev
,
924 enum netdev_features
*current
,
925 enum netdev_features
*advertised
,
926 enum netdev_features
*supported
,
927 enum netdev_features
*peer
);
928 enum netdev_features dummy
[4];
935 advertised
= &dummy
[1];
938 supported
= &dummy
[2];
944 get_features
= netdev
->netdev_class
->get_features
;
946 ? get_features(netdev
, current
, advertised
, supported
,
950 *current
= *advertised
= *supported
= *peer
= 0;
955 /* Returns the maximum speed of a network connection that has the NETDEV_F_*
956 * bits in 'features', in bits per second. If no bits that indicate a speed
957 * are set in 'features', returns 'default_bps'. */
959 netdev_features_to_bps(enum netdev_features features
,
960 uint64_t default_bps
)
963 F_1000000MB
= NETDEV_F_1TB_FD
,
964 F_100000MB
= NETDEV_F_100GB_FD
,
965 F_40000MB
= NETDEV_F_40GB_FD
,
966 F_10000MB
= NETDEV_F_10GB_FD
,
967 F_1000MB
= NETDEV_F_1GB_HD
| NETDEV_F_1GB_FD
,
968 F_100MB
= NETDEV_F_100MB_HD
| NETDEV_F_100MB_FD
,
969 F_10MB
= NETDEV_F_10MB_HD
| NETDEV_F_10MB_FD
972 return ( features
& F_1000000MB
? UINT64_C(1000000000000)
973 : features
& F_100000MB
? UINT64_C(100000000000)
974 : features
& F_40000MB
? UINT64_C(40000000000)
975 : features
& F_10000MB
? UINT64_C(10000000000)
976 : features
& F_1000MB
? UINT64_C(1000000000)
977 : features
& F_100MB
? UINT64_C(100000000)
978 : features
& F_10MB
? UINT64_C(10000000)
982 /* Returns true if any of the NETDEV_F_* bits that indicate a full-duplex link
983 * are set in 'features', otherwise false. */
985 netdev_features_is_full_duplex(enum netdev_features features
)
987 return (features
& (NETDEV_F_10MB_FD
| NETDEV_F_100MB_FD
| NETDEV_F_1GB_FD
988 | NETDEV_F_10GB_FD
| NETDEV_F_40GB_FD
989 | NETDEV_F_100GB_FD
| NETDEV_F_1TB_FD
)) != 0;
992 /* Set the features advertised by 'netdev' to 'advertise'. Returns 0 if
993 * successful, otherwise a positive errno value. */
995 netdev_set_advertisements(struct netdev
*netdev
,
996 enum netdev_features advertise
)
998 return (netdev
->netdev_class
->set_advertisements
999 ? netdev
->netdev_class
->set_advertisements(
1004 /* Assigns 'addr' as 'netdev''s IPv4 address and 'mask' as its netmask. If
1005 * 'addr' is INADDR_ANY, 'netdev''s IPv4 address is cleared. Returns a
1006 * positive errno value. */
1008 netdev_set_in4(struct netdev
*netdev
, struct in_addr addr
, struct in_addr mask
)
1010 return (netdev
->netdev_class
->set_in4
1011 ? netdev
->netdev_class
->set_in4(netdev
, addr
, mask
)
1015 /* Obtains ad IPv4 address from device name and save the address in
1016 * in4. Returns 0 if successful, otherwise a positive errno value.
1019 netdev_get_in4_by_name(const char *device_name
, struct in_addr
*in4
)
1021 struct in6_addr
*mask
, *addr6
;
1025 err
= netdev_open(device_name
, NULL
, &dev
);
1030 err
= netdev_get_addr_list(dev
, &addr6
, &mask
, &n_in6
);
1035 for (i
= 0; i
< n_in6
; i
++) {
1036 if (IN6_IS_ADDR_V4MAPPED(&addr6
[i
])) {
1037 in4
->s_addr
= in6_addr_get_mapped_ipv4(&addr6
[i
]);
1050 /* Adds 'router' as a default IP gateway for the TCP/IP stack that corresponds
1053 netdev_add_router(struct netdev
*netdev
, struct in_addr router
)
1055 COVERAGE_INC(netdev_add_router
);
1056 return (netdev
->netdev_class
->add_router
1057 ? netdev
->netdev_class
->add_router(netdev
, router
)
1061 /* Looks up the next hop for 'host' for the TCP/IP stack that corresponds to
1062 * 'netdev'. If a route cannot not be determined, sets '*next_hop' to 0,
1063 * '*netdev_name' to null, and returns a positive errno value. Otherwise, if a
1064 * next hop is found, stores the next hop gateway's address (0 if 'host' is on
1065 * a directly connected network) in '*next_hop' and a copy of the name of the
1066 * device to reach 'host' in '*netdev_name', and returns 0. The caller is
1067 * responsible for freeing '*netdev_name' (by calling free()). */
1069 netdev_get_next_hop(const struct netdev
*netdev
,
1070 const struct in_addr
*host
, struct in_addr
*next_hop
,
1073 int error
= (netdev
->netdev_class
->get_next_hop
1074 ? netdev
->netdev_class
->get_next_hop(
1075 host
, next_hop
, netdev_name
)
1078 next_hop
->s_addr
= 0;
1079 *netdev_name
= NULL
;
1084 /* Populates 'smap' with status information.
1086 * Populates 'smap' with 'netdev' specific status information. This
1087 * information may be used to populate the status column of the Interface table
1088 * as defined in ovs-vswitchd.conf.db(5). */
1090 netdev_get_status(const struct netdev
*netdev
, struct smap
*smap
)
1092 return (netdev
->netdev_class
->get_status
1093 ? netdev
->netdev_class
->get_status(netdev
, smap
)
1097 /* Returns all assigned IP address to 'netdev' and returns 0.
1098 * API allocates array of address and masks and set it to
1099 * '*addr' and '*mask'.
1100 * Otherwise, returns a positive errno value and sets '*addr', '*mask
1101 * and '*n_addr' to NULL.
1103 * The following error values have well-defined meanings:
1105 * - EADDRNOTAVAIL: 'netdev' has no assigned IPv6 address.
1107 * - EOPNOTSUPP: No IPv6 network stack attached to 'netdev'.
1109 * 'addr' may be null, in which case the address itself is not reported. */
1111 netdev_get_addr_list(const struct netdev
*netdev
, struct in6_addr
**addr
,
1112 struct in6_addr
**mask
, int *n_addr
)
1116 error
= (netdev
->netdev_class
->get_addr_list
1117 ? netdev
->netdev_class
->get_addr_list(netdev
, addr
, mask
, n_addr
): EOPNOTSUPP
);
1118 if (error
&& addr
) {
1127 /* On 'netdev', turns off the flags in 'off' and then turns on the flags in
1128 * 'on'. Returns 0 if successful, otherwise a positive errno value. */
1130 do_update_flags(struct netdev
*netdev
, enum netdev_flags off
,
1131 enum netdev_flags on
, enum netdev_flags
*old_flagsp
,
1132 struct netdev_saved_flags
**sfp
)
1133 OVS_EXCLUDED(netdev_mutex
)
1135 struct netdev_saved_flags
*sf
= NULL
;
1136 enum netdev_flags old_flags
;
1139 error
= netdev
->netdev_class
->update_flags(netdev
, off
& ~on
, on
,
1142 VLOG_WARN_RL(&rl
, "failed to %s flags for network device %s: %s",
1143 off
|| on
? "set" : "get", netdev_get_name(netdev
),
1144 ovs_strerror(error
));
1146 } else if ((off
|| on
) && sfp
) {
1147 enum netdev_flags new_flags
= (old_flags
& ~off
) | on
;
1148 enum netdev_flags changed_flags
= old_flags
^ new_flags
;
1149 if (changed_flags
) {
1150 ovs_mutex_lock(&netdev_mutex
);
1151 *sfp
= sf
= xmalloc(sizeof *sf
);
1152 sf
->netdev
= netdev
;
1153 ovs_list_push_front(&netdev
->saved_flags_list
, &sf
->node
);
1154 sf
->saved_flags
= changed_flags
;
1155 sf
->saved_values
= changed_flags
& new_flags
;
1158 ovs_mutex_unlock(&netdev_mutex
);
1163 *old_flagsp
= old_flags
;
1172 /* Obtains the current flags for 'netdev' and stores them into '*flagsp'.
1173 * Returns 0 if successful, otherwise a positive errno value. On failure,
1174 * stores 0 into '*flagsp'. */
1176 netdev_get_flags(const struct netdev
*netdev_
, enum netdev_flags
*flagsp
)
1178 struct netdev
*netdev
= CONST_CAST(struct netdev
*, netdev_
);
1179 return do_update_flags(netdev
, 0, 0, flagsp
, NULL
);
1182 /* Sets the flags for 'netdev' to 'flags'.
1183 * Returns 0 if successful, otherwise a positive errno value. */
1185 netdev_set_flags(struct netdev
*netdev
, enum netdev_flags flags
,
1186 struct netdev_saved_flags
**sfp
)
1188 return do_update_flags(netdev
, -1, flags
, NULL
, sfp
);
1191 /* Turns on the specified 'flags' on 'netdev':
1193 * - On success, returns 0. If 'sfp' is nonnull, sets '*sfp' to a newly
1194 * allocated 'struct netdev_saved_flags *' that may be passed to
1195 * netdev_restore_flags() to restore the original values of 'flags' on
1196 * 'netdev' (this will happen automatically at program termination if
1197 * netdev_restore_flags() is never called) , or to NULL if no flags were
1200 * - On failure, returns a positive errno value. If 'sfp' is nonnull, sets
1201 * '*sfp' to NULL. */
1203 netdev_turn_flags_on(struct netdev
*netdev
, enum netdev_flags flags
,
1204 struct netdev_saved_flags
**sfp
)
1206 return do_update_flags(netdev
, 0, flags
, NULL
, sfp
);
1209 /* Turns off the specified 'flags' on 'netdev'. See netdev_turn_flags_on() for
1210 * details of the interface. */
1212 netdev_turn_flags_off(struct netdev
*netdev
, enum netdev_flags flags
,
1213 struct netdev_saved_flags
**sfp
)
1215 return do_update_flags(netdev
, flags
, 0, NULL
, sfp
);
1218 /* Restores the flags that were saved in 'sf', and destroys 'sf'.
1219 * Does nothing if 'sf' is NULL. */
1221 netdev_restore_flags(struct netdev_saved_flags
*sf
)
1222 OVS_EXCLUDED(netdev_mutex
)
1225 struct netdev
*netdev
= sf
->netdev
;
1226 enum netdev_flags old_flags
;
1228 netdev
->netdev_class
->update_flags(netdev
,
1229 sf
->saved_flags
& sf
->saved_values
,
1230 sf
->saved_flags
& ~sf
->saved_values
,
1233 ovs_mutex_lock(&netdev_mutex
);
1234 ovs_list_remove(&sf
->node
);
1236 netdev_unref(netdev
);
1240 /* Looks up the ARP table entry for 'ip' on 'netdev'. If one exists and can be
1241 * successfully retrieved, it stores the corresponding MAC address in 'mac' and
1242 * returns 0. Otherwise, it returns a positive errno value; in particular,
1243 * ENXIO indicates that there is no ARP table entry for 'ip' on 'netdev'. */
1245 netdev_arp_lookup(const struct netdev
*netdev
,
1246 ovs_be32 ip
, struct eth_addr
*mac
)
1248 int error
= (netdev
->netdev_class
->arp_lookup
1249 ? netdev
->netdev_class
->arp_lookup(netdev
, ip
, mac
)
1252 *mac
= eth_addr_zero
;
1257 /* Returns true if carrier is active (link light is on) on 'netdev'. */
1259 netdev_get_carrier(const struct netdev
*netdev
)
1262 enum netdev_flags flags
;
1265 netdev_get_flags(netdev
, &flags
);
1266 if (!(flags
& NETDEV_UP
)) {
1270 if (!netdev
->netdev_class
->get_carrier
) {
1274 error
= netdev
->netdev_class
->get_carrier(netdev
, &carrier
);
1276 VLOG_DBG("%s: failed to get network device carrier status, assuming "
1277 "down: %s", netdev_get_name(netdev
), ovs_strerror(error
));
1284 /* Returns the number of times 'netdev''s carrier has changed. */
1286 netdev_get_carrier_resets(const struct netdev
*netdev
)
1288 return (netdev
->netdev_class
->get_carrier_resets
1289 ? netdev
->netdev_class
->get_carrier_resets(netdev
)
1293 /* Attempts to force netdev_get_carrier() to poll 'netdev''s MII registers for
1294 * link status instead of checking 'netdev''s carrier. 'netdev''s MII
1295 * registers will be polled once ever 'interval' milliseconds. If 'netdev'
1296 * does not support MII, another method may be used as a fallback. If
1297 * 'interval' is less than or equal to zero, reverts netdev_get_carrier() to
1298 * its normal behavior.
1300 * Returns 0 if successful, otherwise a positive errno value. */
1302 netdev_set_miimon_interval(struct netdev
*netdev
, long long int interval
)
1304 return (netdev
->netdev_class
->set_miimon_interval
1305 ? netdev
->netdev_class
->set_miimon_interval(netdev
, interval
)
1309 /* Retrieves current device stats for 'netdev'. */
1311 netdev_get_stats(const struct netdev
*netdev
, struct netdev_stats
*stats
)
1315 /* Statistics are initialized before passing it to particular device
1316 * implementation so all values are filtered out by default. */
1317 memset(stats
, 0xFF, sizeof *stats
);
1319 COVERAGE_INC(netdev_get_stats
);
1320 error
= (netdev
->netdev_class
->get_stats
1321 ? netdev
->netdev_class
->get_stats(netdev
, stats
)
1324 /* In case of error all statistics are filtered out */
1325 memset(stats
, 0xff, sizeof *stats
);
1330 /* Attempts to set input rate limiting (policing) policy, such that up to
1331 * 'kbits_rate' kbps of traffic is accepted, with a maximum accumulative burst
1332 * size of 'kbits' kb. */
1334 netdev_set_policing(struct netdev
*netdev
, uint32_t kbits_rate
,
1335 uint32_t kbits_burst
)
1337 return (netdev
->netdev_class
->set_policing
1338 ? netdev
->netdev_class
->set_policing(netdev
,
1339 kbits_rate
, kbits_burst
)
1343 /* Adds to 'types' all of the forms of QoS supported by 'netdev', or leaves it
1344 * empty if 'netdev' does not support QoS. Any names added to 'types' should
1345 * be documented as valid for the "type" column in the "QoS" table in
1346 * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
1348 * Every network device supports disabling QoS with a type of "", but this type
1349 * will not be added to 'types'.
1351 * The caller must initialize 'types' (e.g. with sset_init()) before calling
1352 * this function. The caller is responsible for destroying 'types' (e.g. with
1353 * sset_destroy()) when it is no longer needed.
1355 * Returns 0 if successful, otherwise a positive errno value. */
1357 netdev_get_qos_types(const struct netdev
*netdev
, struct sset
*types
)
1359 const struct netdev_class
*class = netdev
->netdev_class
;
1360 return (class->get_qos_types
1361 ? class->get_qos_types(netdev
, types
)
1365 /* Queries 'netdev' for its capabilities regarding the specified 'type' of QoS,
1366 * which should be "" or one of the types returned by netdev_get_qos_types()
1367 * for 'netdev'. Returns 0 if successful, otherwise a positive errno value.
1368 * On success, initializes 'caps' with the QoS capabilities; on failure, clears
1369 * 'caps' to all zeros. */
1371 netdev_get_qos_capabilities(const struct netdev
*netdev
, const char *type
,
1372 struct netdev_qos_capabilities
*caps
)
1374 const struct netdev_class
*class = netdev
->netdev_class
;
1377 int retval
= (class->get_qos_capabilities
1378 ? class->get_qos_capabilities(netdev
, type
, caps
)
1381 memset(caps
, 0, sizeof *caps
);
1385 /* Every netdev supports turning off QoS. */
1386 memset(caps
, 0, sizeof *caps
);
1391 /* Obtains the number of queues supported by 'netdev' for the specified 'type'
1392 * of QoS. Returns 0 if successful, otherwise a positive errno value. Stores
1393 * the number of queues (zero on failure) in '*n_queuesp'.
1395 * This is just a simple wrapper around netdev_get_qos_capabilities(). */
1397 netdev_get_n_queues(const struct netdev
*netdev
,
1398 const char *type
, unsigned int *n_queuesp
)
1400 struct netdev_qos_capabilities caps
;
1403 retval
= netdev_get_qos_capabilities(netdev
, type
, &caps
);
1404 *n_queuesp
= caps
.n_queues
;
1408 /* Queries 'netdev' about its currently configured form of QoS. If successful,
1409 * stores the name of the current form of QoS into '*typep', stores any details
1410 * of configuration as string key-value pairs in 'details', and returns 0. On
1411 * failure, sets '*typep' to NULL and returns a positive errno value.
1413 * A '*typep' of "" indicates that QoS is currently disabled on 'netdev'.
1415 * The caller must initialize 'details' as an empty smap (e.g. with
1416 * smap_init()) before calling this function. The caller must free 'details'
1417 * when it is no longer needed (e.g. with smap_destroy()).
1419 * The caller must not modify or free '*typep'.
1421 * '*typep' will be one of the types returned by netdev_get_qos_types() for
1422 * 'netdev'. The contents of 'details' should be documented as valid for
1423 * '*typep' in the "other_config" column in the "QoS" table in
1424 * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)). */
1426 netdev_get_qos(const struct netdev
*netdev
,
1427 const char **typep
, struct smap
*details
)
1429 const struct netdev_class
*class = netdev
->netdev_class
;
1432 if (class->get_qos
) {
1433 retval
= class->get_qos(netdev
, typep
, details
);
1436 smap_clear(details
);
1440 /* 'netdev' doesn't support QoS, so report that QoS is disabled. */
1446 /* Attempts to reconfigure QoS on 'netdev', changing the form of QoS to 'type'
1447 * with details of configuration from 'details'. Returns 0 if successful,
1448 * otherwise a positive errno value. On error, the previous QoS configuration
1451 * When this function changes the type of QoS (not just 'details'), this also
1452 * resets all queue configuration for 'netdev' to their defaults (which depend
1453 * on the specific type of QoS). Otherwise, the queue configuration for
1454 * 'netdev' is unchanged.
1456 * 'type' should be "" (to disable QoS) or one of the types returned by
1457 * netdev_get_qos_types() for 'netdev'. The contents of 'details' should be
1458 * documented as valid for the given 'type' in the "other_config" column in the
1459 * "QoS" table in vswitchd/vswitch.xml (which is built as
1460 * ovs-vswitchd.conf.db(8)).
1462 * NULL may be specified for 'details' if there are no configuration
1465 netdev_set_qos(struct netdev
*netdev
,
1466 const char *type
, const struct smap
*details
)
1468 const struct netdev_class
*class = netdev
->netdev_class
;
1474 if (class->set_qos
) {
1476 static const struct smap empty
= SMAP_INITIALIZER(&empty
);
1479 return class->set_qos(netdev
, type
, details
);
1481 return *type
? EOPNOTSUPP
: 0;
1485 /* Queries 'netdev' for information about the queue numbered 'queue_id'. If
1486 * successful, adds that information as string key-value pairs to 'details'.
1487 * Returns 0 if successful, otherwise a positive errno value.
1489 * 'queue_id' must be less than the number of queues supported by 'netdev' for
1490 * the current form of QoS (e.g. as returned by netdev_get_n_queues(netdev)).
1492 * The returned contents of 'details' should be documented as valid for the
1493 * given 'type' in the "other_config" column in the "Queue" table in
1494 * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
1496 * The caller must initialize 'details' (e.g. with smap_init()) before calling
1497 * this function. The caller must free 'details' when it is no longer needed
1498 * (e.g. with smap_destroy()). */
1500 netdev_get_queue(const struct netdev
*netdev
,
1501 unsigned int queue_id
, struct smap
*details
)
1503 const struct netdev_class
*class = netdev
->netdev_class
;
1506 retval
= (class->get_queue
1507 ? class->get_queue(netdev
, queue_id
, details
)
1510 smap_clear(details
);
1515 /* Configures the queue numbered 'queue_id' on 'netdev' with the key-value
1516 * string pairs in 'details'. The contents of 'details' should be documented
1517 * as valid for the given 'type' in the "other_config" column in the "Queue"
1518 * table in vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
1519 * Returns 0 if successful, otherwise a positive errno value. On failure, the
1520 * given queue's configuration should be unmodified.
1522 * 'queue_id' must be less than the number of queues supported by 'netdev' for
1523 * the current form of QoS (e.g. as returned by netdev_get_n_queues(netdev)).
1525 * This function does not modify 'details', and the caller retains ownership of
1528 netdev_set_queue(struct netdev
*netdev
,
1529 unsigned int queue_id
, const struct smap
*details
)
1531 const struct netdev_class
*class = netdev
->netdev_class
;
1532 return (class->set_queue
1533 ? class->set_queue(netdev
, queue_id
, details
)
1537 /* Attempts to delete the queue numbered 'queue_id' from 'netdev'. Some kinds
1538 * of QoS may have a fixed set of queues, in which case attempts to delete them
1539 * will fail with EOPNOTSUPP.
1541 * Returns 0 if successful, otherwise a positive errno value. On failure, the
1542 * given queue will be unmodified.
1544 * 'queue_id' must be less than the number of queues supported by 'netdev' for
1545 * the current form of QoS (e.g. as returned by
1546 * netdev_get_n_queues(netdev)). */
1548 netdev_delete_queue(struct netdev
*netdev
, unsigned int queue_id
)
1550 const struct netdev_class
*class = netdev
->netdev_class
;
1551 return (class->delete_queue
1552 ? class->delete_queue(netdev
, queue_id
)
1556 /* Obtains statistics about 'queue_id' on 'netdev'. On success, returns 0 and
1557 * fills 'stats' with the queue's statistics; individual members of 'stats' may
1558 * be set to all-1-bits if the statistic is unavailable. On failure, returns a
1559 * positive errno value and fills 'stats' with values indicating unsupported
1562 netdev_get_queue_stats(const struct netdev
*netdev
, unsigned int queue_id
,
1563 struct netdev_queue_stats
*stats
)
1565 const struct netdev_class
*class = netdev
->netdev_class
;
1568 retval
= (class->get_queue_stats
1569 ? class->get_queue_stats(netdev
, queue_id
, stats
)
1572 stats
->tx_bytes
= UINT64_MAX
;
1573 stats
->tx_packets
= UINT64_MAX
;
1574 stats
->tx_errors
= UINT64_MAX
;
1575 stats
->created
= LLONG_MIN
;
1580 /* Initializes 'dump' to begin dumping the queues in a netdev.
1582 * This function provides no status indication. An error status for the entire
1583 * dump operation is provided when it is completed by calling
1584 * netdev_queue_dump_done().
1587 netdev_queue_dump_start(struct netdev_queue_dump
*dump
,
1588 const struct netdev
*netdev
)
1590 dump
->netdev
= netdev_ref(netdev
);
1591 if (netdev
->netdev_class
->queue_dump_start
) {
1592 dump
->error
= netdev
->netdev_class
->queue_dump_start(netdev
,
1595 dump
->error
= EOPNOTSUPP
;
1599 /* Attempts to retrieve another queue from 'dump', which must have been
1600 * initialized with netdev_queue_dump_start(). On success, stores a new queue
1601 * ID into '*queue_id', fills 'details' with configuration details for the
1602 * queue, and returns true. On failure, returns false.
1604 * Queues are not necessarily dumped in increasing order of queue ID (or any
1605 * other predictable order).
1607 * Failure might indicate an actual error or merely that the last queue has
1608 * been dumped. An error status for the entire dump operation is provided when
1609 * it is completed by calling netdev_queue_dump_done().
1611 * The returned contents of 'details' should be documented as valid for the
1612 * given 'type' in the "other_config" column in the "Queue" table in
1613 * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
1615 * The caller must initialize 'details' (e.g. with smap_init()) before calling
1616 * this function. This function will clear and replace its contents. The
1617 * caller must free 'details' when it is no longer needed (e.g. with
1618 * smap_destroy()). */
1620 netdev_queue_dump_next(struct netdev_queue_dump
*dump
,
1621 unsigned int *queue_id
, struct smap
*details
)
1623 const struct netdev
*netdev
= dump
->netdev
;
1629 dump
->error
= netdev
->netdev_class
->queue_dump_next(netdev
, dump
->state
,
1633 netdev
->netdev_class
->queue_dump_done(netdev
, dump
->state
);
1639 /* Completes queue table dump operation 'dump', which must have been
1640 * initialized with netdev_queue_dump_start(). Returns 0 if the dump operation
1641 * was error-free, otherwise a positive errno value describing the problem. */
1643 netdev_queue_dump_done(struct netdev_queue_dump
*dump
)
1645 const struct netdev
*netdev
= dump
->netdev
;
1646 if (!dump
->error
&& netdev
->netdev_class
->queue_dump_done
) {
1647 dump
->error
= netdev
->netdev_class
->queue_dump_done(netdev
,
1650 netdev_close(dump
->netdev
);
1651 return dump
->error
== EOF
? 0 : dump
->error
;
1654 /* Iterates over all of 'netdev''s queues, calling 'cb' with the queue's ID,
1655 * its statistics, and the 'aux' specified by the caller. The order of
1656 * iteration is unspecified, but (when successful) each queue is visited
1659 * Calling this function may be more efficient than calling
1660 * netdev_get_queue_stats() for every queue.
1662 * 'cb' must not modify or free the statistics passed in.
1664 * Returns 0 if successful, otherwise a positive errno value. On error, some
1665 * configured queues may not have been included in the iteration. */
1667 netdev_dump_queue_stats(const struct netdev
*netdev
,
1668 netdev_dump_queue_stats_cb
*cb
, void *aux
)
1670 const struct netdev_class
*class = netdev
->netdev_class
;
1671 return (class->dump_queue_stats
1672 ? class->dump_queue_stats(netdev
, cb
, aux
)
1677 /* Returns the class type of 'netdev'.
1679 * The caller must not free the returned value. */
1681 netdev_get_type(const struct netdev
*netdev
)
1683 return netdev
->netdev_class
->type
;
1686 /* Returns the class associated with 'netdev'. */
1687 const struct netdev_class
*
1688 netdev_get_class(const struct netdev
*netdev
)
1690 return netdev
->netdev_class
;
1693 /* Returns the netdev with 'name' or NULL if there is none.
1695 * The caller must free the returned netdev with netdev_close(). */
1697 netdev_from_name(const char *name
)
1698 OVS_EXCLUDED(netdev_mutex
)
1700 struct netdev
*netdev
;
1702 ovs_mutex_lock(&netdev_mutex
);
1703 netdev
= shash_find_data(&netdev_shash
, name
);
1707 ovs_mutex_unlock(&netdev_mutex
);
1712 /* Fills 'device_list' with devices that match 'netdev_class'.
1714 * The caller is responsible for initializing and destroying 'device_list' and
1715 * must close each device on the list. */
1717 netdev_get_devices(const struct netdev_class
*netdev_class
,
1718 struct shash
*device_list
)
1719 OVS_EXCLUDED(netdev_mutex
)
1721 struct shash_node
*node
;
1723 ovs_mutex_lock(&netdev_mutex
);
1724 SHASH_FOR_EACH (node
, &netdev_shash
) {
1725 struct netdev
*dev
= node
->data
;
1727 if (dev
->netdev_class
== netdev_class
) {
1729 shash_add(device_list
, node
->name
, node
->data
);
1732 ovs_mutex_unlock(&netdev_mutex
);
1735 /* Extracts pointers to all 'netdev-vports' into an array 'vports'
1736 * and returns it. Stores the size of the array into '*size'.
1738 * The caller is responsible for freeing 'vports' and must close
1739 * each 'netdev-vport' in the list. */
1741 netdev_get_vports(size_t *size
)
1742 OVS_EXCLUDED(netdev_mutex
)
1744 struct netdev
**vports
;
1745 struct shash_node
*node
;
1752 /* Explicitly allocates big enough chunk of memory. */
1753 vports
= xmalloc(shash_count(&netdev_shash
) * sizeof *vports
);
1754 ovs_mutex_lock(&netdev_mutex
);
1755 SHASH_FOR_EACH (node
, &netdev_shash
) {
1756 struct netdev
*dev
= node
->data
;
1758 if (netdev_vport_is_vport_class(dev
->netdev_class
)) {
1764 ovs_mutex_unlock(&netdev_mutex
);
1771 netdev_get_type_from_name(const char *name
)
1773 struct netdev
*dev
= netdev_from_name(name
);
1774 const char *type
= dev
? netdev_get_type(dev
) : NULL
;
1780 netdev_rxq_get_netdev(const struct netdev_rxq
*rx
)
1782 ovs_assert(rx
->netdev
->ref_cnt
> 0);
1787 netdev_rxq_get_name(const struct netdev_rxq
*rx
)
1789 return netdev_get_name(netdev_rxq_get_netdev(rx
));
1793 netdev_rxq_get_queue_id(const struct netdev_rxq
*rx
)
1795 return rx
->queue_id
;
1799 restore_all_flags(void *aux OVS_UNUSED
)
1801 struct shash_node
*node
;
1803 SHASH_FOR_EACH (node
, &netdev_shash
) {
1804 struct netdev
*netdev
= node
->data
;
1805 const struct netdev_saved_flags
*sf
;
1806 enum netdev_flags saved_values
;
1807 enum netdev_flags saved_flags
;
1809 saved_values
= saved_flags
= 0;
1810 LIST_FOR_EACH (sf
, node
, &netdev
->saved_flags_list
) {
1811 saved_flags
|= sf
->saved_flags
;
1812 saved_values
&= ~sf
->saved_flags
;
1813 saved_values
|= sf
->saved_flags
& sf
->saved_values
;
1816 enum netdev_flags old_flags
;
1818 netdev
->netdev_class
->update_flags(netdev
,
1819 saved_flags
& saved_values
,
1820 saved_flags
& ~saved_values
,
1827 netdev_get_change_seq(const struct netdev
*netdev
)
1829 return netdev
->change_seq
;
1833 /* This implementation is shared by Linux and BSD. */
1835 static struct ifaddrs
*if_addr_list
;
1836 static struct ovs_mutex if_addr_list_lock
= OVS_MUTEX_INITIALIZER
;
1839 netdev_get_addrs_list_flush(void)
1841 ovs_mutex_lock(&if_addr_list_lock
);
1843 freeifaddrs(if_addr_list
);
1844 if_addr_list
= NULL
;
1846 ovs_mutex_unlock(&if_addr_list_lock
);
1850 netdev_get_addrs(const char dev
[], struct in6_addr
**paddr
,
1851 struct in6_addr
**pmask
, int *n_in
)
1853 struct in6_addr
*addr_array
, *mask_array
;
1854 const struct ifaddrs
*ifa
;
1857 ovs_mutex_lock(&if_addr_list_lock
);
1858 if (!if_addr_list
) {
1861 err
= getifaddrs(&if_addr_list
);
1863 ovs_mutex_unlock(&if_addr_list_lock
);
1868 for (ifa
= if_addr_list
; ifa
; ifa
= ifa
->ifa_next
) {
1869 if (ifa
->ifa_addr
!= NULL
) {
1872 family
= ifa
->ifa_addr
->sa_family
;
1873 if (family
== AF_INET
|| family
== AF_INET6
) {
1874 if (!strncmp(ifa
->ifa_name
, dev
, IFNAMSIZ
)) {
1882 ovs_mutex_unlock(&if_addr_list_lock
);
1883 return EADDRNOTAVAIL
;
1885 addr_array
= xzalloc(sizeof *addr_array
* cnt
);
1886 mask_array
= xzalloc(sizeof *mask_array
* cnt
);
1887 for (ifa
= if_addr_list
; ifa
; ifa
= ifa
->ifa_next
) {
1890 if (strncmp(ifa
->ifa_name
, dev
, IFNAMSIZ
) || ifa
->ifa_addr
== NULL
) {
1894 family
= ifa
->ifa_addr
->sa_family
;
1895 if (family
== AF_INET
) {
1896 const struct sockaddr_in
*sin
;
1898 sin
= ALIGNED_CAST(const struct sockaddr_in
*, ifa
->ifa_addr
);
1899 in6_addr_set_mapped_ipv4(&addr_array
[i
], sin
->sin_addr
.s_addr
);
1900 sin
= (struct sockaddr_in
*) &ifa
->ifa_netmask
;
1901 in6_addr_set_mapped_ipv4(&mask_array
[i
], sin
->sin_addr
.s_addr
);
1903 } else if (family
== AF_INET6
) {
1904 const struct sockaddr_in6
*sin6
;
1906 sin6
= ALIGNED_CAST(const struct sockaddr_in6
*, ifa
->ifa_addr
);
1907 memcpy(&addr_array
[i
], &sin6
->sin6_addr
, sizeof *addr_array
);
1908 sin6
= (struct sockaddr_in6
*) &ifa
->ifa_netmask
;
1909 memcpy(&mask_array
[i
], &sin6
->sin6_addr
, sizeof *mask_array
);
1913 ovs_mutex_unlock(&if_addr_list_lock
);
1916 *paddr
= addr_array
;
1917 *pmask
= mask_array
;
1927 netdev_wait_reconf_required(struct netdev
*netdev
)
1929 seq_wait(netdev
->reconfigure_seq
, netdev
->last_reconfigure_seq
);
1933 netdev_is_reconf_required(struct netdev
*netdev
)
1935 return seq_read(netdev
->reconfigure_seq
) != netdev
->last_reconfigure_seq
;
1938 /* Give a chance to 'netdev' to reconfigure some of its parameters.
1940 * If a module uses netdev_send() and netdev_rxq_recv(), it must call this
1941 * function when netdev_is_reconf_required() returns true.
1943 * Return 0 if successful, otherwise a positive errno value. If the
1944 * reconfiguration fails the netdev will not be able to send or receive
1947 * When this function is called, no call to netdev_rxq_recv() or netdev_send()
1948 * must be issued. */
1950 netdev_reconfigure(struct netdev
*netdev
)
1952 const struct netdev_class
*class = netdev
->netdev_class
;
1954 netdev
->last_reconfigure_seq
= seq_read(netdev
->reconfigure_seq
);
1956 return (class->reconfigure
1957 ? class->reconfigure(netdev
)