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"
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_requested_n_rxq(const struct netdev
*netdev
)
117 return netdev
->requested_n_rxq
;
121 netdev_is_pmd(const struct netdev
*netdev
)
123 return netdev
->netdev_class
->is_pmd
;
127 netdev_initialize(void)
128 OVS_EXCLUDED(netdev_mutex
)
130 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
132 if (ovsthread_once_start(&once
)) {
133 fatal_signal_add_hook(restore_all_flags
, NULL
, NULL
, true);
135 netdev_vport_patch_register();
138 netdev_register_provider(&netdev_linux_class
);
139 netdev_register_provider(&netdev_internal_class
);
140 netdev_register_provider(&netdev_tap_class
);
141 netdev_vport_tunnel_register();
143 #if defined(__FreeBSD__) || defined(__NetBSD__)
144 netdev_register_provider(&netdev_tap_class
);
145 netdev_register_provider(&netdev_bsd_class
);
148 netdev_register_provider(&netdev_windows_class
);
149 netdev_register_provider(&netdev_internal_class
);
150 netdev_vport_tunnel_register();
152 ovsthread_once_done(&once
);
156 /* Performs periodic work needed by all the various kinds of netdevs.
158 * If your program opens any netdevs, it must call this function within its
162 OVS_EXCLUDED(netdev_mutex
)
166 struct netdev_registered_class
*rc
;
167 CMAP_FOR_EACH (rc
, cmap_node
, &netdev_classes
) {
168 if (rc
->class->run
) {
174 /* Arranges for poll_block() to wake up when netdev_run() needs to be called.
176 * If your program opens any netdevs, it must call this function within its
180 OVS_EXCLUDED(netdev_mutex
)
184 struct netdev_registered_class
*rc
;
185 CMAP_FOR_EACH (rc
, cmap_node
, &netdev_classes
) {
186 if (rc
->class->wait
) {
192 static struct netdev_registered_class
*
193 netdev_lookup_class(const char *type
)
195 struct netdev_registered_class
*rc
;
196 CMAP_FOR_EACH_WITH_HASH (rc
, cmap_node
, hash_string(type
, 0),
198 if (!strcmp(type
, rc
->class->type
)) {
205 /* Initializes and registers a new netdev provider. After successful
206 * registration, new netdevs of that type can be opened using netdev_open(). */
208 netdev_register_provider(const struct netdev_class
*new_class
)
209 OVS_EXCLUDED(netdev_class_mutex
, netdev_mutex
)
213 ovs_mutex_lock(&netdev_class_mutex
);
214 if (netdev_lookup_class(new_class
->type
)) {
215 VLOG_WARN("attempted to register duplicate netdev provider: %s",
219 error
= new_class
->init
? new_class
->init() : 0;
221 struct netdev_registered_class
*rc
;
223 rc
= xmalloc(sizeof *rc
);
224 cmap_insert(&netdev_classes
, &rc
->cmap_node
,
225 hash_string(new_class
->type
, 0));
226 rc
->class = new_class
;
227 ovs_refcount_init(&rc
->refcnt
);
229 VLOG_ERR("failed to initialize %s network device class: %s",
230 new_class
->type
, ovs_strerror(error
));
233 ovs_mutex_unlock(&netdev_class_mutex
);
238 /* Unregisters a netdev provider. 'type' must have been previously registered
239 * and not currently be in use by any netdevs. After unregistration new
240 * netdevs of that type cannot be opened using netdev_open(). (However, the
241 * provider may still be accessible from other threads until the next RCU grace
242 * period, so the caller must not free or re-register the same netdev_class
243 * until that has passed.) */
245 netdev_unregister_provider(const char *type
)
246 OVS_EXCLUDED(netdev_class_mutex
, netdev_mutex
)
248 struct netdev_registered_class
*rc
;
253 ovs_mutex_lock(&netdev_class_mutex
);
254 rc
= netdev_lookup_class(type
);
256 VLOG_WARN("attempted to unregister a netdev provider that is not "
257 "registered: %s", type
);
258 error
= EAFNOSUPPORT
;
259 } else if (ovs_refcount_unref(&rc
->refcnt
) != 1) {
260 ovs_refcount_ref(&rc
->refcnt
);
261 VLOG_WARN("attempted to unregister in use netdev provider: %s",
265 cmap_remove(&netdev_classes
, &rc
->cmap_node
,
266 hash_string(rc
->class->type
, 0));
267 ovsrcu_postpone(free
, rc
);
270 ovs_mutex_unlock(&netdev_class_mutex
);
275 /* Clears 'types' and enumerates the types of all currently registered netdev
276 * providers into it. The caller must first initialize the sset. */
278 netdev_enumerate_types(struct sset
*types
)
279 OVS_EXCLUDED(netdev_mutex
)
284 struct netdev_registered_class
*rc
;
285 CMAP_FOR_EACH (rc
, cmap_node
, &netdev_classes
) {
286 sset_add(types
, rc
->class->type
);
290 /* Check that the network device name is not the same as any of the registered
291 * vport providers' dpif_port name (dpif_port is NULL if the vport provider
292 * does not define it) or the datapath internal port name (e.g. ovs-system).
294 * Returns true if there is a name conflict, false otherwise. */
296 netdev_is_reserved_name(const char *name
)
297 OVS_EXCLUDED(netdev_mutex
)
301 struct netdev_registered_class
*rc
;
302 CMAP_FOR_EACH (rc
, cmap_node
, &netdev_classes
) {
303 const char *dpif_port
= netdev_vport_class_get_dpif_port(rc
->class);
304 if (dpif_port
&& !strncmp(name
, dpif_port
, strlen(dpif_port
))) {
309 if (!strncmp(name
, "ovs-", 4)) {
314 dp_enumerate_types(&types
);
315 SSET_FOR_EACH (type
, &types
) {
316 if (!strcmp(name
+4, type
)) {
317 sset_destroy(&types
);
321 sset_destroy(&types
);
327 /* Opens the network device named 'name' (e.g. "eth0") of the specified 'type'
328 * (e.g. "system") and returns zero if successful, otherwise a positive errno
329 * value. On success, sets '*netdevp' to the new network device, otherwise to
332 * Some network devices may need to be configured (with netdev_set_config())
333 * before they can be used. */
335 netdev_open(const char *name
, const char *type
, struct netdev
**netdevp
)
336 OVS_EXCLUDED(netdev_mutex
)
338 struct netdev
*netdev
;
343 ovs_mutex_lock(&netdev_mutex
);
344 netdev
= shash_find_data(&netdev_shash
, name
);
346 struct netdev_registered_class
*rc
;
348 rc
= netdev_lookup_class(type
&& type
[0] ? type
: "system");
349 if (rc
&& ovs_refcount_try_ref_rcu(&rc
->refcnt
)) {
350 netdev
= rc
->class->alloc();
352 memset(netdev
, 0, sizeof *netdev
);
353 netdev
->netdev_class
= rc
->class;
354 netdev
->name
= xstrdup(name
);
355 netdev
->change_seq
= 1;
356 netdev
->node
= shash_add(&netdev_shash
, name
, netdev
);
358 /* By default enable one tx and rx queue per netdev. */
359 netdev
->n_txq
= netdev
->netdev_class
->send
? 1 : 0;
360 netdev
->n_rxq
= netdev
->netdev_class
->rxq_alloc
? 1 : 0;
361 netdev
->requested_n_rxq
= netdev
->n_rxq
;
363 ovs_list_init(&netdev
->saved_flags_list
);
365 error
= rc
->class->construct(netdev
);
367 netdev_change_seq_changed(netdev
);
369 ovs_refcount_unref(&rc
->refcnt
);
371 ovs_assert(ovs_list_is_empty(&netdev
->saved_flags_list
));
372 shash_delete(&netdev_shash
, netdev
->node
);
373 rc
->class->dealloc(netdev
);
379 VLOG_WARN("could not create netdev %s of unknown type %s",
381 error
= EAFNOSUPPORT
;
393 ovs_mutex_unlock(&netdev_mutex
);
398 /* Returns a reference to 'netdev_' for the caller to own. Returns null if
399 * 'netdev_' is null. */
401 netdev_ref(const struct netdev
*netdev_
)
402 OVS_EXCLUDED(netdev_mutex
)
404 struct netdev
*netdev
= CONST_CAST(struct netdev
*, netdev_
);
407 ovs_mutex_lock(&netdev_mutex
);
408 ovs_assert(netdev
->ref_cnt
> 0);
410 ovs_mutex_unlock(&netdev_mutex
);
415 /* Reconfigures the device 'netdev' with 'args'. 'args' may be empty
416 * or NULL if none are needed. */
418 netdev_set_config(struct netdev
*netdev
, const struct smap
*args
, char **errp
)
419 OVS_EXCLUDED(netdev_mutex
)
421 if (netdev
->netdev_class
->set_config
) {
422 const struct smap no_args
= SMAP_INITIALIZER(&no_args
);
425 error
= netdev
->netdev_class
->set_config(netdev
,
426 args
? args
: &no_args
);
428 VLOG_WARN_BUF(errp
, "%s: could not set configuration (%s)",
429 netdev_get_name(netdev
), ovs_strerror(error
));
432 } else if (args
&& !smap_is_empty(args
)) {
433 VLOG_WARN_BUF(errp
, "%s: arguments provided to device that is not configurable",
434 netdev_get_name(netdev
));
439 /* Returns the current configuration for 'netdev' in 'args'. The caller must
440 * have already initialized 'args' with smap_init(). Returns 0 on success, in
441 * which case 'args' will be filled with 'netdev''s configuration. On failure
442 * returns a positive errno value, in which case 'args' will be empty.
444 * The caller owns 'args' and its contents and must eventually free them with
447 netdev_get_config(const struct netdev
*netdev
, struct smap
*args
)
448 OVS_EXCLUDED(netdev_mutex
)
453 if (netdev
->netdev_class
->get_config
) {
454 error
= netdev
->netdev_class
->get_config(netdev
, args
);
465 const struct netdev_tunnel_config
*
466 netdev_get_tunnel_config(const struct netdev
*netdev
)
467 OVS_EXCLUDED(netdev_mutex
)
469 if (netdev
->netdev_class
->get_tunnel_config
) {
470 return netdev
->netdev_class
->get_tunnel_config(netdev
);
476 /* Returns the id of the numa node the 'netdev' is on. If the function
477 * is not implemented, returns NETDEV_NUMA_UNSPEC. */
479 netdev_get_numa_id(const struct netdev
*netdev
)
481 if (netdev
->netdev_class
->get_numa_id
) {
482 return netdev
->netdev_class
->get_numa_id(netdev
);
484 return NETDEV_NUMA_UNSPEC
;
489 netdev_unref(struct netdev
*dev
)
490 OVS_RELEASES(netdev_mutex
)
492 ovs_assert(dev
->ref_cnt
);
493 if (!--dev
->ref_cnt
) {
494 const struct netdev_class
*class = dev
->netdev_class
;
495 struct netdev_registered_class
*rc
;
497 dev
->netdev_class
->destruct(dev
);
500 shash_delete(&netdev_shash
, dev
->node
);
503 dev
->netdev_class
->dealloc(dev
);
504 ovs_mutex_unlock(&netdev_mutex
);
506 rc
= netdev_lookup_class(class->type
);
507 ovs_refcount_unref(&rc
->refcnt
);
509 ovs_mutex_unlock(&netdev_mutex
);
513 /* Closes and destroys 'netdev'. */
515 netdev_close(struct netdev
*netdev
)
516 OVS_EXCLUDED(netdev_mutex
)
519 ovs_mutex_lock(&netdev_mutex
);
520 netdev_unref(netdev
);
524 /* Removes 'netdev' from the global shash and unrefs 'netdev'.
526 * This allows handler and revalidator threads to still retain references
527 * to this netdev while the main thread changes interface configuration.
529 * This function should only be called by the main thread when closing
530 * netdevs during user configuration changes. Otherwise, netdev_close should be
531 * used to close netdevs. */
533 netdev_remove(struct netdev
*netdev
)
536 ovs_mutex_lock(&netdev_mutex
);
538 shash_delete(&netdev_shash
, netdev
->node
);
540 netdev_change_seq_changed(netdev
);
542 netdev_unref(netdev
);
546 /* Parses 'netdev_name_', which is of the form [type@]name into its component
547 * pieces. 'name' and 'type' must be freed by the caller. */
549 netdev_parse_name(const char *netdev_name_
, char **name
, char **type
)
551 char *netdev_name
= xstrdup(netdev_name_
);
554 separator
= strchr(netdev_name
, '@');
558 *name
= xstrdup(separator
+ 1);
561 *type
= xstrdup("system");
565 /* Attempts to open a netdev_rxq handle for obtaining packets received on
566 * 'netdev'. On success, returns 0 and stores a nonnull 'netdev_rxq *' into
567 * '*rxp'. On failure, returns a positive errno value and stores NULL into
570 * Some kinds of network devices might not support receiving packets. This
571 * function returns EOPNOTSUPP in that case.*/
573 netdev_rxq_open(struct netdev
*netdev
, struct netdev_rxq
**rxp
, int id
)
574 OVS_EXCLUDED(netdev_mutex
)
578 if (netdev
->netdev_class
->rxq_alloc
&& id
< netdev
->n_rxq
) {
579 struct netdev_rxq
*rx
= netdev
->netdev_class
->rxq_alloc();
583 error
= netdev
->netdev_class
->rxq_construct(rx
);
589 netdev
->netdev_class
->rxq_dealloc(rx
);
603 netdev_rxq_close(struct netdev_rxq
*rx
)
604 OVS_EXCLUDED(netdev_mutex
)
607 struct netdev
*netdev
= rx
->netdev
;
608 netdev
->netdev_class
->rxq_destruct(rx
);
609 netdev
->netdev_class
->rxq_dealloc(rx
);
610 netdev_close(netdev
);
614 /* Attempts to receive a batch of packets from 'rx'. 'pkts' should point to
615 * the beginning of an array of MAX_RX_BATCH pointers to dp_packet. If
616 * successful, this function stores pointers to up to MAX_RX_BATCH dp_packets
617 * into the array, transferring ownership of the packets to the caller, stores
618 * the number of received packets into '*cnt', and returns 0.
620 * The implementation does not necessarily initialize any non-data members of
621 * 'pkts'. That is, the caller must initialize layer pointers and metadata
622 * itself, if desired, e.g. with pkt_metadata_init() and miniflow_extract().
624 * Returns EAGAIN immediately if no packet is ready to be received or another
625 * positive errno value if an error was encountered. */
627 netdev_rxq_recv(struct netdev_rxq
*rx
, struct dp_packet_batch
*batch
)
631 retval
= rx
->netdev
->netdev_class
->rxq_recv(rx
, batch
->packets
, &batch
->count
);
633 COVERAGE_INC(netdev_received
);
640 /* Arranges for poll_block() to wake up when a packet is ready to be received
643 netdev_rxq_wait(struct netdev_rxq
*rx
)
645 rx
->netdev
->netdev_class
->rxq_wait(rx
);
648 /* Discards any packets ready to be received on 'rx'. */
650 netdev_rxq_drain(struct netdev_rxq
*rx
)
652 return (rx
->netdev
->netdev_class
->rxq_drain
653 ? rx
->netdev
->netdev_class
->rxq_drain(rx
)
657 /* Configures the number of tx queues and rx queues of 'netdev'.
658 * Return 0 if successful, otherwise a positive errno value.
660 * 'n_rxq' specifies the maximum number of receive queues to create.
661 * The netdev provider might choose to create less (e.g. if the hardware
662 * supports only a smaller number). The caller can check how many have been
663 * actually created by calling 'netdev_n_rxq()'
665 * 'n_txq' specifies the exact number of transmission queues to create.
666 * If this function returns successfully, the caller can make 'n_txq'
667 * concurrent calls to netdev_send() (each one with a different 'qid' in the
668 * range [0..'n_txq'-1]).
670 * On error, the tx queue and rx queue configuration is indeterminant.
671 * Caller should make decision on whether to restore the previous or
672 * the default configuration. Also, caller must make sure there is no
673 * other thread accessing the queues at the same time. */
675 netdev_set_multiq(struct netdev
*netdev
, unsigned int n_txq
,
680 error
= (netdev
->netdev_class
->set_multiq
681 ? netdev
->netdev_class
->set_multiq(netdev
,
686 if (error
&& error
!= EOPNOTSUPP
) {
687 VLOG_DBG_RL(&rl
, "failed to set tx/rx queue for network device %s:"
688 "%s", netdev_get_name(netdev
), ovs_strerror(error
));
694 /* Sends 'buffers' on 'netdev'. Returns 0 if successful (for every packet),
695 * otherwise a positive errno value. Returns EAGAIN without blocking if
696 * at least one the packets cannot be queued immediately. Returns EMSGSIZE
697 * if a partial packet was transmitted or if a packet is too big or too small
698 * to transmit on the device.
700 * If the function returns a non-zero value, some of the packets might have
703 * If 'may_steal' is false, the caller retains ownership of all the packets.
704 * If 'may_steal' is true, the caller transfers ownership of all the packets
705 * to the network device, regardless of success.
707 * The network device is expected to maintain one or more packet
708 * transmission queues, so that the caller does not ordinarily have to
709 * do additional queuing of packets. 'qid' specifies the queue to use
710 * and can be ignored if the implementation does not support multiple
713 * Some network devices may not implement support for this function. In such
714 * cases this function will always return EOPNOTSUPP. */
716 netdev_send(struct netdev
*netdev
, int qid
, struct dp_packet_batch
*batch
,
719 if (!netdev
->netdev_class
->send
) {
720 dp_packet_delete_batch(batch
, may_steal
);
724 int error
= netdev
->netdev_class
->send(netdev
, qid
,
725 batch
->packets
, batch
->count
,
728 COVERAGE_INC(netdev_sent
);
734 netdev_pop_header(struct netdev
*netdev
, struct dp_packet_batch
*batch
)
737 struct dp_packet
**buffers
= batch
->packets
;
739 if (!netdev
->netdev_class
->pop_header
) {
743 for (i
= 0; i
< batch
->count
; i
++) {
744 buffers
[i
] = netdev
->netdev_class
->pop_header(buffers
[i
]);
746 buffers
[n_cnt
++] = buffers
[i
];
749 batch
->count
= n_cnt
;
754 netdev_build_header(const struct netdev
*netdev
, struct ovs_action_push_tnl
*data
,
755 const struct flow
*tnl_flow
)
757 if (netdev
->netdev_class
->build_header
) {
758 return netdev
->netdev_class
->build_header(netdev
, data
, tnl_flow
);
764 netdev_push_header(const struct netdev
*netdev
,
765 struct dp_packet_batch
*batch
,
766 const struct ovs_action_push_tnl
*data
)
770 if (!netdev
->netdev_class
->push_header
) {
774 for (i
= 0; i
< batch
->count
; i
++) {
775 netdev
->netdev_class
->push_header(batch
->packets
[i
], data
);
776 pkt_metadata_init(&batch
->packets
[i
]->md
, u32_to_odp(data
->out_port
));
782 /* Registers with the poll loop to wake up from the next call to poll_block()
783 * when the packet transmission queue has sufficient room to transmit a packet
784 * with netdev_send().
786 * The network device is expected to maintain one or more packet
787 * transmission queues, so that the caller does not ordinarily have to
788 * do additional queuing of packets. 'qid' specifies the queue to use
789 * and can be ignored if the implementation does not support multiple
792 netdev_send_wait(struct netdev
*netdev
, int qid
)
794 if (netdev
->netdev_class
->send_wait
) {
795 netdev
->netdev_class
->send_wait(netdev
, qid
);
799 /* Attempts to set 'netdev''s MAC address to 'mac'. Returns 0 if successful,
800 * otherwise a positive errno value. */
802 netdev_set_etheraddr(struct netdev
*netdev
, const struct eth_addr mac
)
804 return netdev
->netdev_class
->set_etheraddr(netdev
, mac
);
807 /* Retrieves 'netdev''s MAC address. If successful, returns 0 and copies the
808 * the MAC address into 'mac'. On failure, returns a positive errno value and
809 * clears 'mac' to all-zeros. */
811 netdev_get_etheraddr(const struct netdev
*netdev
, struct eth_addr
*mac
)
813 return netdev
->netdev_class
->get_etheraddr(netdev
, mac
);
816 /* Returns the name of the network device that 'netdev' represents,
817 * e.g. "eth0". The caller must not modify or free the returned string. */
819 netdev_get_name(const struct netdev
*netdev
)
824 /* Retrieves the MTU of 'netdev'. The MTU is the maximum size of transmitted
825 * (and received) packets, in bytes, not including the hardware header; thus,
826 * this is typically 1500 bytes for Ethernet devices.
828 * If successful, returns 0 and stores the MTU size in '*mtup'. Returns
829 * EOPNOTSUPP if 'netdev' does not have an MTU (as e.g. some tunnels do not).
830 * On other failure, returns a positive errno value. On failure, sets '*mtup'
833 netdev_get_mtu(const struct netdev
*netdev
, int *mtup
)
835 const struct netdev_class
*class = netdev
->netdev_class
;
838 error
= class->get_mtu
? class->get_mtu(netdev
, mtup
) : EOPNOTSUPP
;
841 if (error
!= EOPNOTSUPP
) {
842 VLOG_DBG_RL(&rl
, "failed to retrieve MTU for network device %s: "
843 "%s", netdev_get_name(netdev
), ovs_strerror(error
));
849 /* Sets the MTU of 'netdev'. The MTU is the maximum size of transmitted
850 * (and received) packets, in bytes.
852 * If successful, returns 0. Returns EOPNOTSUPP if 'netdev' does not have an
853 * MTU (as e.g. some tunnels do not). On other failure, returns a positive
856 netdev_set_mtu(const struct netdev
*netdev
, int mtu
)
858 const struct netdev_class
*class = netdev
->netdev_class
;
861 error
= class->set_mtu
? class->set_mtu(netdev
, mtu
) : EOPNOTSUPP
;
862 if (error
&& error
!= EOPNOTSUPP
) {
863 VLOG_DBG_RL(&rl
, "failed to set MTU for network device %s: %s",
864 netdev_get_name(netdev
), ovs_strerror(error
));
870 /* Returns the ifindex of 'netdev', if successful, as a positive number. On
871 * failure, returns a negative errno value.
873 * The desired semantics of the ifindex value are a combination of those
874 * specified by POSIX for if_nametoindex() and by SNMP for ifIndex. An ifindex
875 * value should be unique within a host and remain stable at least until
876 * reboot. SNMP says an ifindex "ranges between 1 and the value of ifNumber"
877 * but many systems do not follow this rule anyhow.
879 * Some network devices may not implement support for this function. In such
880 * cases this function will always return -EOPNOTSUPP.
883 netdev_get_ifindex(const struct netdev
*netdev
)
885 int (*get_ifindex
)(const struct netdev
*);
887 get_ifindex
= netdev
->netdev_class
->get_ifindex
;
889 return get_ifindex
? get_ifindex(netdev
) : -EOPNOTSUPP
;
892 /* Stores the features supported by 'netdev' into each of '*current',
893 * '*advertised', '*supported', and '*peer' that are non-null. Each value is a
894 * bitmap of "enum ofp_port_features" bits, in host byte order. Returns 0 if
895 * successful, otherwise a positive errno value. On failure, all of the
896 * passed-in values are set to 0.
898 * Some network devices may not implement support for this function. In such
899 * cases this function will always return EOPNOTSUPP. */
901 netdev_get_features(const struct netdev
*netdev
,
902 enum netdev_features
*current
,
903 enum netdev_features
*advertised
,
904 enum netdev_features
*supported
,
905 enum netdev_features
*peer
)
907 int (*get_features
)(const struct netdev
*netdev
,
908 enum netdev_features
*current
,
909 enum netdev_features
*advertised
,
910 enum netdev_features
*supported
,
911 enum netdev_features
*peer
);
912 enum netdev_features dummy
[4];
919 advertised
= &dummy
[1];
922 supported
= &dummy
[2];
928 get_features
= netdev
->netdev_class
->get_features
;
930 ? get_features(netdev
, current
, advertised
, supported
,
934 *current
= *advertised
= *supported
= *peer
= 0;
939 /* Returns the maximum speed of a network connection that has the NETDEV_F_*
940 * bits in 'features', in bits per second. If no bits that indicate a speed
941 * are set in 'features', returns 'default_bps'. */
943 netdev_features_to_bps(enum netdev_features features
,
944 uint64_t default_bps
)
947 F_1000000MB
= NETDEV_F_1TB_FD
,
948 F_100000MB
= NETDEV_F_100GB_FD
,
949 F_40000MB
= NETDEV_F_40GB_FD
,
950 F_10000MB
= NETDEV_F_10GB_FD
,
951 F_1000MB
= NETDEV_F_1GB_HD
| NETDEV_F_1GB_FD
,
952 F_100MB
= NETDEV_F_100MB_HD
| NETDEV_F_100MB_FD
,
953 F_10MB
= NETDEV_F_10MB_HD
| NETDEV_F_10MB_FD
956 return ( features
& F_1000000MB
? UINT64_C(1000000000000)
957 : features
& F_100000MB
? UINT64_C(100000000000)
958 : features
& F_40000MB
? UINT64_C(40000000000)
959 : features
& F_10000MB
? UINT64_C(10000000000)
960 : features
& F_1000MB
? UINT64_C(1000000000)
961 : features
& F_100MB
? UINT64_C(100000000)
962 : features
& F_10MB
? UINT64_C(10000000)
966 /* Returns true if any of the NETDEV_F_* bits that indicate a full-duplex link
967 * are set in 'features', otherwise false. */
969 netdev_features_is_full_duplex(enum netdev_features features
)
971 return (features
& (NETDEV_F_10MB_FD
| NETDEV_F_100MB_FD
| NETDEV_F_1GB_FD
972 | NETDEV_F_10GB_FD
| NETDEV_F_40GB_FD
973 | NETDEV_F_100GB_FD
| NETDEV_F_1TB_FD
)) != 0;
976 /* Set the features advertised by 'netdev' to 'advertise'. Returns 0 if
977 * successful, otherwise a positive errno value. */
979 netdev_set_advertisements(struct netdev
*netdev
,
980 enum netdev_features advertise
)
982 return (netdev
->netdev_class
->set_advertisements
983 ? netdev
->netdev_class
->set_advertisements(
988 /* Assigns 'addr' as 'netdev''s IPv4 address and 'mask' as its netmask. If
989 * 'addr' is INADDR_ANY, 'netdev''s IPv4 address is cleared. Returns a
990 * positive errno value. */
992 netdev_set_in4(struct netdev
*netdev
, struct in_addr addr
, struct in_addr mask
)
994 return (netdev
->netdev_class
->set_in4
995 ? netdev
->netdev_class
->set_in4(netdev
, addr
, mask
)
999 /* Obtains ad IPv4 address from device name and save the address in
1000 * in4. Returns 0 if successful, otherwise a positive errno value.
1003 netdev_get_in4_by_name(const char *device_name
, struct in_addr
*in4
)
1005 struct in6_addr
*mask
, *addr6
;
1009 err
= netdev_open(device_name
, NULL
, &dev
);
1014 err
= netdev_get_addr_list(dev
, &addr6
, &mask
, &n_in6
);
1019 for (i
= 0; i
< n_in6
; i
++) {
1020 if (IN6_IS_ADDR_V4MAPPED(&addr6
[i
])) {
1021 in4
->s_addr
= in6_addr_get_mapped_ipv4(&addr6
[i
]);
1034 /* Adds 'router' as a default IP gateway for the TCP/IP stack that corresponds
1037 netdev_add_router(struct netdev
*netdev
, struct in_addr router
)
1039 COVERAGE_INC(netdev_add_router
);
1040 return (netdev
->netdev_class
->add_router
1041 ? netdev
->netdev_class
->add_router(netdev
, router
)
1045 /* Looks up the next hop for 'host' for the TCP/IP stack that corresponds to
1046 * 'netdev'. If a route cannot not be determined, sets '*next_hop' to 0,
1047 * '*netdev_name' to null, and returns a positive errno value. Otherwise, if a
1048 * next hop is found, stores the next hop gateway's address (0 if 'host' is on
1049 * a directly connected network) in '*next_hop' and a copy of the name of the
1050 * device to reach 'host' in '*netdev_name', and returns 0. The caller is
1051 * responsible for freeing '*netdev_name' (by calling free()). */
1053 netdev_get_next_hop(const struct netdev
*netdev
,
1054 const struct in_addr
*host
, struct in_addr
*next_hop
,
1057 int error
= (netdev
->netdev_class
->get_next_hop
1058 ? netdev
->netdev_class
->get_next_hop(
1059 host
, next_hop
, netdev_name
)
1062 next_hop
->s_addr
= 0;
1063 *netdev_name
= NULL
;
1068 /* Populates 'smap' with status information.
1070 * Populates 'smap' with 'netdev' specific status information. This
1071 * information may be used to populate the status column of the Interface table
1072 * as defined in ovs-vswitchd.conf.db(5). */
1074 netdev_get_status(const struct netdev
*netdev
, struct smap
*smap
)
1076 return (netdev
->netdev_class
->get_status
1077 ? netdev
->netdev_class
->get_status(netdev
, smap
)
1081 /* Returns all assigned IP address to 'netdev' and returns 0.
1082 * API allocates array of address and masks and set it to
1083 * '*addr' and '*mask'.
1084 * Otherwise, returns a positive errno value and sets '*addr', '*mask
1085 * and '*n_addr' to NULL.
1087 * The following error values have well-defined meanings:
1089 * - EADDRNOTAVAIL: 'netdev' has no assigned IPv6 address.
1091 * - EOPNOTSUPP: No IPv6 network stack attached to 'netdev'.
1093 * 'addr' may be null, in which case the address itself is not reported. */
1095 netdev_get_addr_list(const struct netdev
*netdev
, struct in6_addr
**addr
,
1096 struct in6_addr
**mask
, int *n_addr
)
1100 error
= (netdev
->netdev_class
->get_addr_list
1101 ? netdev
->netdev_class
->get_addr_list(netdev
, addr
, mask
, n_addr
): EOPNOTSUPP
);
1102 if (error
&& addr
) {
1111 /* On 'netdev', turns off the flags in 'off' and then turns on the flags in
1112 * 'on'. Returns 0 if successful, otherwise a positive errno value. */
1114 do_update_flags(struct netdev
*netdev
, enum netdev_flags off
,
1115 enum netdev_flags on
, enum netdev_flags
*old_flagsp
,
1116 struct netdev_saved_flags
**sfp
)
1117 OVS_EXCLUDED(netdev_mutex
)
1119 struct netdev_saved_flags
*sf
= NULL
;
1120 enum netdev_flags old_flags
;
1123 error
= netdev
->netdev_class
->update_flags(netdev
, off
& ~on
, on
,
1126 VLOG_WARN_RL(&rl
, "failed to %s flags for network device %s: %s",
1127 off
|| on
? "set" : "get", netdev_get_name(netdev
),
1128 ovs_strerror(error
));
1130 } else if ((off
|| on
) && sfp
) {
1131 enum netdev_flags new_flags
= (old_flags
& ~off
) | on
;
1132 enum netdev_flags changed_flags
= old_flags
^ new_flags
;
1133 if (changed_flags
) {
1134 ovs_mutex_lock(&netdev_mutex
);
1135 *sfp
= sf
= xmalloc(sizeof *sf
);
1136 sf
->netdev
= netdev
;
1137 ovs_list_push_front(&netdev
->saved_flags_list
, &sf
->node
);
1138 sf
->saved_flags
= changed_flags
;
1139 sf
->saved_values
= changed_flags
& new_flags
;
1142 ovs_mutex_unlock(&netdev_mutex
);
1147 *old_flagsp
= old_flags
;
1156 /* Obtains the current flags for 'netdev' and stores them into '*flagsp'.
1157 * Returns 0 if successful, otherwise a positive errno value. On failure,
1158 * stores 0 into '*flagsp'. */
1160 netdev_get_flags(const struct netdev
*netdev_
, enum netdev_flags
*flagsp
)
1162 struct netdev
*netdev
= CONST_CAST(struct netdev
*, netdev_
);
1163 return do_update_flags(netdev
, 0, 0, flagsp
, NULL
);
1166 /* Sets the flags for 'netdev' to 'flags'.
1167 * Returns 0 if successful, otherwise a positive errno value. */
1169 netdev_set_flags(struct netdev
*netdev
, enum netdev_flags flags
,
1170 struct netdev_saved_flags
**sfp
)
1172 return do_update_flags(netdev
, -1, flags
, NULL
, sfp
);
1175 /* Turns on the specified 'flags' on 'netdev':
1177 * - On success, returns 0. If 'sfp' is nonnull, sets '*sfp' to a newly
1178 * allocated 'struct netdev_saved_flags *' that may be passed to
1179 * netdev_restore_flags() to restore the original values of 'flags' on
1180 * 'netdev' (this will happen automatically at program termination if
1181 * netdev_restore_flags() is never called) , or to NULL if no flags were
1184 * - On failure, returns a positive errno value. If 'sfp' is nonnull, sets
1185 * '*sfp' to NULL. */
1187 netdev_turn_flags_on(struct netdev
*netdev
, enum netdev_flags flags
,
1188 struct netdev_saved_flags
**sfp
)
1190 return do_update_flags(netdev
, 0, flags
, NULL
, sfp
);
1193 /* Turns off the specified 'flags' on 'netdev'. See netdev_turn_flags_on() for
1194 * details of the interface. */
1196 netdev_turn_flags_off(struct netdev
*netdev
, enum netdev_flags flags
,
1197 struct netdev_saved_flags
**sfp
)
1199 return do_update_flags(netdev
, flags
, 0, NULL
, sfp
);
1202 /* Restores the flags that were saved in 'sf', and destroys 'sf'.
1203 * Does nothing if 'sf' is NULL. */
1205 netdev_restore_flags(struct netdev_saved_flags
*sf
)
1206 OVS_EXCLUDED(netdev_mutex
)
1209 struct netdev
*netdev
= sf
->netdev
;
1210 enum netdev_flags old_flags
;
1212 netdev
->netdev_class
->update_flags(netdev
,
1213 sf
->saved_flags
& sf
->saved_values
,
1214 sf
->saved_flags
& ~sf
->saved_values
,
1217 ovs_mutex_lock(&netdev_mutex
);
1218 ovs_list_remove(&sf
->node
);
1220 netdev_unref(netdev
);
1224 /* Looks up the ARP table entry for 'ip' on 'netdev'. If one exists and can be
1225 * successfully retrieved, it stores the corresponding MAC address in 'mac' and
1226 * returns 0. Otherwise, it returns a positive errno value; in particular,
1227 * ENXIO indicates that there is no ARP table entry for 'ip' on 'netdev'. */
1229 netdev_arp_lookup(const struct netdev
*netdev
,
1230 ovs_be32 ip
, struct eth_addr
*mac
)
1232 int error
= (netdev
->netdev_class
->arp_lookup
1233 ? netdev
->netdev_class
->arp_lookup(netdev
, ip
, mac
)
1236 *mac
= eth_addr_zero
;
1241 /* Returns true if carrier is active (link light is on) on 'netdev'. */
1243 netdev_get_carrier(const struct netdev
*netdev
)
1246 enum netdev_flags flags
;
1249 netdev_get_flags(netdev
, &flags
);
1250 if (!(flags
& NETDEV_UP
)) {
1254 if (!netdev
->netdev_class
->get_carrier
) {
1258 error
= netdev
->netdev_class
->get_carrier(netdev
, &carrier
);
1260 VLOG_DBG("%s: failed to get network device carrier status, assuming "
1261 "down: %s", netdev_get_name(netdev
), ovs_strerror(error
));
1268 /* Returns the number of times 'netdev''s carrier has changed. */
1270 netdev_get_carrier_resets(const struct netdev
*netdev
)
1272 return (netdev
->netdev_class
->get_carrier_resets
1273 ? netdev
->netdev_class
->get_carrier_resets(netdev
)
1277 /* Attempts to force netdev_get_carrier() to poll 'netdev''s MII registers for
1278 * link status instead of checking 'netdev''s carrier. 'netdev''s MII
1279 * registers will be polled once ever 'interval' milliseconds. If 'netdev'
1280 * does not support MII, another method may be used as a fallback. If
1281 * 'interval' is less than or equal to zero, reverts netdev_get_carrier() to
1282 * its normal behavior.
1284 * Returns 0 if successful, otherwise a positive errno value. */
1286 netdev_set_miimon_interval(struct netdev
*netdev
, long long int interval
)
1288 return (netdev
->netdev_class
->set_miimon_interval
1289 ? netdev
->netdev_class
->set_miimon_interval(netdev
, interval
)
1293 /* Retrieves current device stats for 'netdev'. */
1295 netdev_get_stats(const struct netdev
*netdev
, struct netdev_stats
*stats
)
1299 /* Statistics are initialized before passing it to particular device
1300 * implementation so all values are filtered out by default. */
1301 memset(stats
, 0xFF, sizeof *stats
);
1303 COVERAGE_INC(netdev_get_stats
);
1304 error
= (netdev
->netdev_class
->get_stats
1305 ? netdev
->netdev_class
->get_stats(netdev
, stats
)
1308 /* In case of error all statistics are filtered out */
1309 memset(stats
, 0xff, sizeof *stats
);
1314 /* Attempts to set input rate limiting (policing) policy, such that up to
1315 * 'kbits_rate' kbps of traffic is accepted, with a maximum accumulative burst
1316 * size of 'kbits' kb. */
1318 netdev_set_policing(struct netdev
*netdev
, uint32_t kbits_rate
,
1319 uint32_t kbits_burst
)
1321 return (netdev
->netdev_class
->set_policing
1322 ? netdev
->netdev_class
->set_policing(netdev
,
1323 kbits_rate
, kbits_burst
)
1327 /* Adds to 'types' all of the forms of QoS supported by 'netdev', or leaves it
1328 * empty if 'netdev' does not support QoS. Any names added to 'types' should
1329 * be documented as valid for the "type" column in the "QoS" table in
1330 * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
1332 * Every network device supports disabling QoS with a type of "", but this type
1333 * will not be added to 'types'.
1335 * The caller must initialize 'types' (e.g. with sset_init()) before calling
1336 * this function. The caller is responsible for destroying 'types' (e.g. with
1337 * sset_destroy()) when it is no longer needed.
1339 * Returns 0 if successful, otherwise a positive errno value. */
1341 netdev_get_qos_types(const struct netdev
*netdev
, struct sset
*types
)
1343 const struct netdev_class
*class = netdev
->netdev_class
;
1344 return (class->get_qos_types
1345 ? class->get_qos_types(netdev
, types
)
1349 /* Queries 'netdev' for its capabilities regarding the specified 'type' of QoS,
1350 * which should be "" or one of the types returned by netdev_get_qos_types()
1351 * for 'netdev'. Returns 0 if successful, otherwise a positive errno value.
1352 * On success, initializes 'caps' with the QoS capabilities; on failure, clears
1353 * 'caps' to all zeros. */
1355 netdev_get_qos_capabilities(const struct netdev
*netdev
, const char *type
,
1356 struct netdev_qos_capabilities
*caps
)
1358 const struct netdev_class
*class = netdev
->netdev_class
;
1361 int retval
= (class->get_qos_capabilities
1362 ? class->get_qos_capabilities(netdev
, type
, caps
)
1365 memset(caps
, 0, sizeof *caps
);
1369 /* Every netdev supports turning off QoS. */
1370 memset(caps
, 0, sizeof *caps
);
1375 /* Obtains the number of queues supported by 'netdev' for the specified 'type'
1376 * of QoS. Returns 0 if successful, otherwise a positive errno value. Stores
1377 * the number of queues (zero on failure) in '*n_queuesp'.
1379 * This is just a simple wrapper around netdev_get_qos_capabilities(). */
1381 netdev_get_n_queues(const struct netdev
*netdev
,
1382 const char *type
, unsigned int *n_queuesp
)
1384 struct netdev_qos_capabilities caps
;
1387 retval
= netdev_get_qos_capabilities(netdev
, type
, &caps
);
1388 *n_queuesp
= caps
.n_queues
;
1392 /* Queries 'netdev' about its currently configured form of QoS. If successful,
1393 * stores the name of the current form of QoS into '*typep', stores any details
1394 * of configuration as string key-value pairs in 'details', and returns 0. On
1395 * failure, sets '*typep' to NULL and returns a positive errno value.
1397 * A '*typep' of "" indicates that QoS is currently disabled on 'netdev'.
1399 * The caller must initialize 'details' as an empty smap (e.g. with
1400 * smap_init()) before calling this function. The caller must free 'details'
1401 * when it is no longer needed (e.g. with smap_destroy()).
1403 * The caller must not modify or free '*typep'.
1405 * '*typep' will be one of the types returned by netdev_get_qos_types() for
1406 * 'netdev'. The contents of 'details' should be documented as valid for
1407 * '*typep' in the "other_config" column in the "QoS" table in
1408 * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)). */
1410 netdev_get_qos(const struct netdev
*netdev
,
1411 const char **typep
, struct smap
*details
)
1413 const struct netdev_class
*class = netdev
->netdev_class
;
1416 if (class->get_qos
) {
1417 retval
= class->get_qos(netdev
, typep
, details
);
1420 smap_clear(details
);
1424 /* 'netdev' doesn't support QoS, so report that QoS is disabled. */
1430 /* Attempts to reconfigure QoS on 'netdev', changing the form of QoS to 'type'
1431 * with details of configuration from 'details'. Returns 0 if successful,
1432 * otherwise a positive errno value. On error, the previous QoS configuration
1435 * When this function changes the type of QoS (not just 'details'), this also
1436 * resets all queue configuration for 'netdev' to their defaults (which depend
1437 * on the specific type of QoS). Otherwise, the queue configuration for
1438 * 'netdev' is unchanged.
1440 * 'type' should be "" (to disable QoS) or one of the types returned by
1441 * netdev_get_qos_types() for 'netdev'. The contents of 'details' should be
1442 * documented as valid for the given 'type' in the "other_config" column in the
1443 * "QoS" table in vswitchd/vswitch.xml (which is built as
1444 * ovs-vswitchd.conf.db(8)).
1446 * NULL may be specified for 'details' if there are no configuration
1449 netdev_set_qos(struct netdev
*netdev
,
1450 const char *type
, const struct smap
*details
)
1452 const struct netdev_class
*class = netdev
->netdev_class
;
1458 if (class->set_qos
) {
1460 static const struct smap empty
= SMAP_INITIALIZER(&empty
);
1463 return class->set_qos(netdev
, type
, details
);
1465 return *type
? EOPNOTSUPP
: 0;
1469 /* Queries 'netdev' for information about the queue numbered 'queue_id'. If
1470 * successful, adds that information as string key-value pairs to 'details'.
1471 * Returns 0 if successful, otherwise a positive errno value.
1473 * 'queue_id' must be less than the number of queues supported by 'netdev' for
1474 * the current form of QoS (e.g. as returned by netdev_get_n_queues(netdev)).
1476 * The returned contents of 'details' should be documented as valid for the
1477 * given 'type' in the "other_config" column in the "Queue" table in
1478 * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
1480 * The caller must initialize 'details' (e.g. with smap_init()) before calling
1481 * this function. The caller must free 'details' when it is no longer needed
1482 * (e.g. with smap_destroy()). */
1484 netdev_get_queue(const struct netdev
*netdev
,
1485 unsigned int queue_id
, struct smap
*details
)
1487 const struct netdev_class
*class = netdev
->netdev_class
;
1490 retval
= (class->get_queue
1491 ? class->get_queue(netdev
, queue_id
, details
)
1494 smap_clear(details
);
1499 /* Configures the queue numbered 'queue_id' on 'netdev' with the key-value
1500 * string pairs in 'details'. The contents of 'details' should be documented
1501 * as valid for the given 'type' in the "other_config" column in the "Queue"
1502 * table in vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
1503 * Returns 0 if successful, otherwise a positive errno value. On failure, the
1504 * given queue's configuration should be unmodified.
1506 * 'queue_id' must be less than the number of queues supported by 'netdev' for
1507 * the current form of QoS (e.g. as returned by netdev_get_n_queues(netdev)).
1509 * This function does not modify 'details', and the caller retains ownership of
1512 netdev_set_queue(struct netdev
*netdev
,
1513 unsigned int queue_id
, const struct smap
*details
)
1515 const struct netdev_class
*class = netdev
->netdev_class
;
1516 return (class->set_queue
1517 ? class->set_queue(netdev
, queue_id
, details
)
1521 /* Attempts to delete the queue numbered 'queue_id' from 'netdev'. Some kinds
1522 * of QoS may have a fixed set of queues, in which case attempts to delete them
1523 * will fail with EOPNOTSUPP.
1525 * Returns 0 if successful, otherwise a positive errno value. On failure, the
1526 * given queue will be unmodified.
1528 * 'queue_id' must be less than the number of queues supported by 'netdev' for
1529 * the current form of QoS (e.g. as returned by
1530 * netdev_get_n_queues(netdev)). */
1532 netdev_delete_queue(struct netdev
*netdev
, unsigned int queue_id
)
1534 const struct netdev_class
*class = netdev
->netdev_class
;
1535 return (class->delete_queue
1536 ? class->delete_queue(netdev
, queue_id
)
1540 /* Obtains statistics about 'queue_id' on 'netdev'. On success, returns 0 and
1541 * fills 'stats' with the queue's statistics; individual members of 'stats' may
1542 * be set to all-1-bits if the statistic is unavailable. On failure, returns a
1543 * positive errno value and fills 'stats' with values indicating unsupported
1546 netdev_get_queue_stats(const struct netdev
*netdev
, unsigned int queue_id
,
1547 struct netdev_queue_stats
*stats
)
1549 const struct netdev_class
*class = netdev
->netdev_class
;
1552 retval
= (class->get_queue_stats
1553 ? class->get_queue_stats(netdev
, queue_id
, stats
)
1556 stats
->tx_bytes
= UINT64_MAX
;
1557 stats
->tx_packets
= UINT64_MAX
;
1558 stats
->tx_errors
= UINT64_MAX
;
1559 stats
->created
= LLONG_MIN
;
1564 /* Initializes 'dump' to begin dumping the queues in a netdev.
1566 * This function provides no status indication. An error status for the entire
1567 * dump operation is provided when it is completed by calling
1568 * netdev_queue_dump_done().
1571 netdev_queue_dump_start(struct netdev_queue_dump
*dump
,
1572 const struct netdev
*netdev
)
1574 dump
->netdev
= netdev_ref(netdev
);
1575 if (netdev
->netdev_class
->queue_dump_start
) {
1576 dump
->error
= netdev
->netdev_class
->queue_dump_start(netdev
,
1579 dump
->error
= EOPNOTSUPP
;
1583 /* Attempts to retrieve another queue from 'dump', which must have been
1584 * initialized with netdev_queue_dump_start(). On success, stores a new queue
1585 * ID into '*queue_id', fills 'details' with configuration details for the
1586 * queue, and returns true. On failure, returns false.
1588 * Queues are not necessarily dumped in increasing order of queue ID (or any
1589 * other predictable order).
1591 * Failure might indicate an actual error or merely that the last queue has
1592 * been dumped. An error status for the entire dump operation is provided when
1593 * it is completed by calling netdev_queue_dump_done().
1595 * The returned contents of 'details' should be documented as valid for the
1596 * given 'type' in the "other_config" column in the "Queue" table in
1597 * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
1599 * The caller must initialize 'details' (e.g. with smap_init()) before calling
1600 * this function. This function will clear and replace its contents. The
1601 * caller must free 'details' when it is no longer needed (e.g. with
1602 * smap_destroy()). */
1604 netdev_queue_dump_next(struct netdev_queue_dump
*dump
,
1605 unsigned int *queue_id
, struct smap
*details
)
1607 const struct netdev
*netdev
= dump
->netdev
;
1613 dump
->error
= netdev
->netdev_class
->queue_dump_next(netdev
, dump
->state
,
1617 netdev
->netdev_class
->queue_dump_done(netdev
, dump
->state
);
1623 /* Completes queue table dump operation 'dump', which must have been
1624 * initialized with netdev_queue_dump_start(). Returns 0 if the dump operation
1625 * was error-free, otherwise a positive errno value describing the problem. */
1627 netdev_queue_dump_done(struct netdev_queue_dump
*dump
)
1629 const struct netdev
*netdev
= dump
->netdev
;
1630 if (!dump
->error
&& netdev
->netdev_class
->queue_dump_done
) {
1631 dump
->error
= netdev
->netdev_class
->queue_dump_done(netdev
,
1634 netdev_close(dump
->netdev
);
1635 return dump
->error
== EOF
? 0 : dump
->error
;
1638 /* Iterates over all of 'netdev''s queues, calling 'cb' with the queue's ID,
1639 * its statistics, and the 'aux' specified by the caller. The order of
1640 * iteration is unspecified, but (when successful) each queue is visited
1643 * Calling this function may be more efficient than calling
1644 * netdev_get_queue_stats() for every queue.
1646 * 'cb' must not modify or free the statistics passed in.
1648 * Returns 0 if successful, otherwise a positive errno value. On error, some
1649 * configured queues may not have been included in the iteration. */
1651 netdev_dump_queue_stats(const struct netdev
*netdev
,
1652 netdev_dump_queue_stats_cb
*cb
, void *aux
)
1654 const struct netdev_class
*class = netdev
->netdev_class
;
1655 return (class->dump_queue_stats
1656 ? class->dump_queue_stats(netdev
, cb
, aux
)
1661 /* Returns the class type of 'netdev'.
1663 * The caller must not free the returned value. */
1665 netdev_get_type(const struct netdev
*netdev
)
1667 return netdev
->netdev_class
->type
;
1670 /* Returns the class associated with 'netdev'. */
1671 const struct netdev_class
*
1672 netdev_get_class(const struct netdev
*netdev
)
1674 return netdev
->netdev_class
;
1677 /* Returns the netdev with 'name' or NULL if there is none.
1679 * The caller must free the returned netdev with netdev_close(). */
1681 netdev_from_name(const char *name
)
1682 OVS_EXCLUDED(netdev_mutex
)
1684 struct netdev
*netdev
;
1686 ovs_mutex_lock(&netdev_mutex
);
1687 netdev
= shash_find_data(&netdev_shash
, name
);
1691 ovs_mutex_unlock(&netdev_mutex
);
1696 /* Fills 'device_list' with devices that match 'netdev_class'.
1698 * The caller is responsible for initializing and destroying 'device_list' and
1699 * must close each device on the list. */
1701 netdev_get_devices(const struct netdev_class
*netdev_class
,
1702 struct shash
*device_list
)
1703 OVS_EXCLUDED(netdev_mutex
)
1705 struct shash_node
*node
;
1707 ovs_mutex_lock(&netdev_mutex
);
1708 SHASH_FOR_EACH (node
, &netdev_shash
) {
1709 struct netdev
*dev
= node
->data
;
1711 if (dev
->netdev_class
== netdev_class
) {
1713 shash_add(device_list
, node
->name
, node
->data
);
1716 ovs_mutex_unlock(&netdev_mutex
);
1719 /* Extracts pointers to all 'netdev-vports' into an array 'vports'
1720 * and returns it. Stores the size of the array into '*size'.
1722 * The caller is responsible for freeing 'vports' and must close
1723 * each 'netdev-vport' in the list. */
1725 netdev_get_vports(size_t *size
)
1726 OVS_EXCLUDED(netdev_mutex
)
1728 struct netdev
**vports
;
1729 struct shash_node
*node
;
1736 /* Explicitly allocates big enough chunk of memory. */
1737 vports
= xmalloc(shash_count(&netdev_shash
) * sizeof *vports
);
1738 ovs_mutex_lock(&netdev_mutex
);
1739 SHASH_FOR_EACH (node
, &netdev_shash
) {
1740 struct netdev
*dev
= node
->data
;
1742 if (netdev_vport_is_vport_class(dev
->netdev_class
)) {
1748 ovs_mutex_unlock(&netdev_mutex
);
1755 netdev_get_type_from_name(const char *name
)
1757 struct netdev
*dev
= netdev_from_name(name
);
1758 const char *type
= dev
? netdev_get_type(dev
) : NULL
;
1764 netdev_rxq_get_netdev(const struct netdev_rxq
*rx
)
1766 ovs_assert(rx
->netdev
->ref_cnt
> 0);
1771 netdev_rxq_get_name(const struct netdev_rxq
*rx
)
1773 return netdev_get_name(netdev_rxq_get_netdev(rx
));
1777 netdev_rxq_get_queue_id(const struct netdev_rxq
*rx
)
1779 return rx
->queue_id
;
1783 restore_all_flags(void *aux OVS_UNUSED
)
1785 struct shash_node
*node
;
1787 SHASH_FOR_EACH (node
, &netdev_shash
) {
1788 struct netdev
*netdev
= node
->data
;
1789 const struct netdev_saved_flags
*sf
;
1790 enum netdev_flags saved_values
;
1791 enum netdev_flags saved_flags
;
1793 saved_values
= saved_flags
= 0;
1794 LIST_FOR_EACH (sf
, node
, &netdev
->saved_flags_list
) {
1795 saved_flags
|= sf
->saved_flags
;
1796 saved_values
&= ~sf
->saved_flags
;
1797 saved_values
|= sf
->saved_flags
& sf
->saved_values
;
1800 enum netdev_flags old_flags
;
1802 netdev
->netdev_class
->update_flags(netdev
,
1803 saved_flags
& saved_values
,
1804 saved_flags
& ~saved_values
,
1811 netdev_get_change_seq(const struct netdev
*netdev
)
1813 return netdev
->change_seq
;
1817 /* This implementation is shared by Linux and BSD. */
1819 static struct ifaddrs
*if_addr_list
;
1820 static struct ovs_mutex if_addr_list_lock
= OVS_MUTEX_INITIALIZER
;
1823 netdev_get_addrs_list_flush(void)
1825 ovs_mutex_lock(&if_addr_list_lock
);
1827 freeifaddrs(if_addr_list
);
1828 if_addr_list
= NULL
;
1830 ovs_mutex_unlock(&if_addr_list_lock
);
1834 netdev_get_addrs(const char dev
[], struct in6_addr
**paddr
,
1835 struct in6_addr
**pmask
, int *n_in
)
1837 struct in6_addr
*addr_array
, *mask_array
;
1838 const struct ifaddrs
*ifa
;
1841 ovs_mutex_lock(&if_addr_list_lock
);
1842 if (!if_addr_list
) {
1845 err
= getifaddrs(&if_addr_list
);
1847 ovs_mutex_unlock(&if_addr_list_lock
);
1852 for (ifa
= if_addr_list
; ifa
; ifa
= ifa
->ifa_next
) {
1853 if (ifa
->ifa_addr
!= NULL
) {
1856 family
= ifa
->ifa_addr
->sa_family
;
1857 if (family
== AF_INET
|| family
== AF_INET6
) {
1858 if (!strncmp(ifa
->ifa_name
, dev
, IFNAMSIZ
)) {
1866 ovs_mutex_unlock(&if_addr_list_lock
);
1867 return EADDRNOTAVAIL
;
1869 addr_array
= xzalloc(sizeof *addr_array
* cnt
);
1870 mask_array
= xzalloc(sizeof *mask_array
* cnt
);
1871 for (ifa
= if_addr_list
; ifa
; ifa
= ifa
->ifa_next
) {
1874 if (strncmp(ifa
->ifa_name
, dev
, IFNAMSIZ
) || ifa
->ifa_addr
== NULL
) {
1878 family
= ifa
->ifa_addr
->sa_family
;
1879 if (family
== AF_INET
) {
1880 const struct sockaddr_in
*sin
;
1882 sin
= ALIGNED_CAST(const struct sockaddr_in
*, ifa
->ifa_addr
);
1883 in6_addr_set_mapped_ipv4(&addr_array
[i
], sin
->sin_addr
.s_addr
);
1884 sin
= (struct sockaddr_in
*) &ifa
->ifa_netmask
;
1885 in6_addr_set_mapped_ipv4(&mask_array
[i
], sin
->sin_addr
.s_addr
);
1887 } else if (family
== AF_INET6
) {
1888 const struct sockaddr_in6
*sin6
;
1890 sin6
= ALIGNED_CAST(const struct sockaddr_in6
*, ifa
->ifa_addr
);
1891 memcpy(&addr_array
[i
], &sin6
->sin6_addr
, sizeof *addr_array
);
1892 sin6
= (struct sockaddr_in6
*) &ifa
->ifa_netmask
;
1893 memcpy(&mask_array
[i
], &sin6
->sin6_addr
, sizeof *mask_array
);
1897 ovs_mutex_unlock(&if_addr_list_lock
);
1900 *paddr
= addr_array
;
1901 *pmask
= mask_array
;