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>
36 #include "dp-packet.h"
37 #include "openvswitch/dynamic-string.h"
38 #include "fatal-signal.h"
40 #include "openvswitch/list.h"
41 #include "netdev-dpdk.h"
42 #include "netdev-provider.h"
43 #include "netdev-vport.h"
44 #include "odp-netlink.h"
45 #include "openflow/openflow.h"
47 #include "poll-loop.h"
53 #include "openvswitch/vlog.h"
57 VLOG_DEFINE_THIS_MODULE(netdev);
59 COVERAGE_DEFINE(netdev_received);
60 COVERAGE_DEFINE(netdev_sent);
61 COVERAGE_DEFINE(netdev_add_router);
62 COVERAGE_DEFINE(netdev_get_stats);
64 struct netdev_saved_flags {
65 struct netdev *netdev;
66 struct ovs_list node; /* In struct netdev's saved_flags_list. */
67 enum netdev_flags saved_flags;
68 enum netdev_flags saved_values;
71 /* Protects 'netdev_shash' and the mutable members of struct netdev. */
72 static struct ovs_mutex netdev_mutex = OVS_MUTEX_INITIALIZER;
74 /* All created network devices. */
75 static struct shash netdev_shash OVS_GUARDED_BY(netdev_mutex)
76 = SHASH_INITIALIZER(&netdev_shash);
78 /* Protects 'netdev_classes' against insertions or deletions.
80 * This is a recursive mutex to allow recursive acquisition when calling into
81 * providers. For example, netdev_run() calls into provider 'run' functions,
82 * which might reasonably want to call one of the netdev functions that takes
83 * netdev_class_mutex. */
84 static struct ovs_mutex netdev_class_mutex OVS_ACQ_BEFORE(netdev_mutex);
86 /* Contains 'struct netdev_registered_class'es. */
87 static struct hmap netdev_classes OVS_GUARDED_BY(netdev_class_mutex)
88 = HMAP_INITIALIZER(&netdev_classes);
90 struct netdev_registered_class {
91 /* In 'netdev_classes', by class->type. */
92 struct hmap_node hmap_node OVS_GUARDED_BY(netdev_class_mutex);
93 const struct netdev_class *class OVS_GUARDED_BY(netdev_class_mutex);
94 /* Number of 'struct netdev's of this class. */
95 int ref_cnt OVS_GUARDED_BY(netdev_class_mutex);
98 /* This is set pretty low because we probably won't learn anything from the
99 * additional log messages. */
100 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
102 static void restore_all_flags(void *aux OVS_UNUSED);
103 void update_device_args(struct netdev *, const struct shash *args);
106 netdev_n_txq(const struct netdev *netdev)
108 return netdev->n_txq;
112 netdev_n_rxq(const struct netdev *netdev)
114 return netdev->n_rxq;
118 netdev_requested_n_rxq(const struct netdev *netdev)
120 return netdev->requested_n_rxq;
124 netdev_is_pmd(const struct netdev *netdev)
126 return netdev->netdev_class->is_pmd;
130 netdev_class_mutex_initialize(void)
131 OVS_EXCLUDED(netdev_class_mutex, netdev_mutex)
133 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
135 if (ovsthread_once_start(&once)) {
136 ovs_mutex_init_recursive(&netdev_class_mutex);
137 ovsthread_once_done(&once);
142 netdev_initialize(void)
143 OVS_EXCLUDED(netdev_class_mutex, netdev_mutex)
145 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
147 if (ovsthread_once_start(&once)) {
148 netdev_class_mutex_initialize();
150 fatal_signal_add_hook(restore_all_flags, NULL, NULL, true);
151 netdev_vport_patch_register();
154 netdev_register_provider(&netdev_linux_class);
155 netdev_register_provider(&netdev_internal_class);
156 netdev_register_provider(&netdev_tap_class);
157 netdev_vport_tunnel_register();
159 #if defined(__FreeBSD__) || defined(__NetBSD__)
160 netdev_register_provider(&netdev_tap_class);
161 netdev_register_provider(&netdev_bsd_class);
164 netdev_register_provider(&netdev_windows_class);
165 netdev_register_provider(&netdev_internal_class);
166 netdev_vport_tunnel_register();
168 ovsthread_once_done(&once);
172 /* Performs periodic work needed by all the various kinds of netdevs.
174 * If your program opens any netdevs, it must call this function within its
178 OVS_EXCLUDED(netdev_class_mutex, netdev_mutex)
180 struct netdev_registered_class *rc;
183 ovs_mutex_lock(&netdev_class_mutex);
184 HMAP_FOR_EACH (rc, hmap_node, &netdev_classes) {
185 if (rc->class->run) {
189 ovs_mutex_unlock(&netdev_class_mutex);
192 /* Arranges for poll_block() to wake up when netdev_run() needs to be called.
194 * If your program opens any netdevs, it must call this function within its
198 OVS_EXCLUDED(netdev_class_mutex, netdev_mutex)
200 struct netdev_registered_class *rc;
202 ovs_mutex_lock(&netdev_class_mutex);
203 HMAP_FOR_EACH (rc, hmap_node, &netdev_classes) {
204 if (rc->class->wait) {
208 ovs_mutex_unlock(&netdev_class_mutex);
211 static struct netdev_registered_class *
212 netdev_lookup_class(const char *type)
213 OVS_REQ_RDLOCK(netdev_class_mutex)
215 struct netdev_registered_class *rc;
217 HMAP_FOR_EACH_WITH_HASH (rc, hmap_node, hash_string(type, 0),
219 if (!strcmp(type, rc->class->type)) {
226 /* Initializes and registers a new netdev provider. After successful
227 * registration, new netdevs of that type can be opened using netdev_open(). */
229 netdev_register_provider(const struct netdev_class *new_class)
230 OVS_EXCLUDED(netdev_class_mutex, netdev_mutex)
234 netdev_class_mutex_initialize();
235 ovs_mutex_lock(&netdev_class_mutex);
236 if (netdev_lookup_class(new_class->type)) {
237 VLOG_WARN("attempted to register duplicate netdev provider: %s",
241 error = new_class->init ? new_class->init() : 0;
243 struct netdev_registered_class *rc;
245 rc = xmalloc(sizeof *rc);
246 hmap_insert(&netdev_classes, &rc->hmap_node,
247 hash_string(new_class->type, 0));
248 rc->class = new_class;
251 VLOG_ERR("failed to initialize %s network device class: %s",
252 new_class->type, ovs_strerror(error));
255 ovs_mutex_unlock(&netdev_class_mutex);
260 /* Unregisters a netdev provider. 'type' must have been previously
261 * registered and not currently be in use by any netdevs. After unregistration
262 * new netdevs of that type cannot be opened using netdev_open(). */
264 netdev_unregister_provider(const char *type)
265 OVS_EXCLUDED(netdev_class_mutex, netdev_mutex)
267 struct netdev_registered_class *rc;
272 ovs_mutex_lock(&netdev_class_mutex);
273 rc = netdev_lookup_class(type);
275 VLOG_WARN("attempted to unregister a netdev provider that is not "
276 "registered: %s", type);
277 error = EAFNOSUPPORT;
280 hmap_remove(&netdev_classes, &rc->hmap_node);
284 VLOG_WARN("attempted to unregister in use netdev provider: %s",
289 ovs_mutex_unlock(&netdev_class_mutex);
294 /* Clears 'types' and enumerates the types of all currently registered netdev
295 * providers into it. The caller must first initialize the sset. */
297 netdev_enumerate_types(struct sset *types)
298 OVS_EXCLUDED(netdev_mutex)
300 struct netdev_registered_class *rc;
305 ovs_mutex_lock(&netdev_class_mutex);
306 HMAP_FOR_EACH (rc, hmap_node, &netdev_classes) {
307 sset_add(types, rc->class->type);
309 ovs_mutex_unlock(&netdev_class_mutex);
312 /* Check that the network device name is not the same as any of the registered
313 * vport providers' dpif_port name (dpif_port is NULL if the vport provider
314 * does not define it) or the datapath internal port name (e.g. ovs-system).
316 * Returns true if there is a name conflict, false otherwise. */
318 netdev_is_reserved_name(const char *name)
319 OVS_EXCLUDED(netdev_mutex)
321 struct netdev_registered_class *rc;
325 ovs_mutex_lock(&netdev_class_mutex);
326 HMAP_FOR_EACH (rc, hmap_node, &netdev_classes) {
327 const char *dpif_port = netdev_vport_class_get_dpif_port(rc->class);
328 if (dpif_port && !strncmp(name, dpif_port, strlen(dpif_port))) {
329 ovs_mutex_unlock(&netdev_class_mutex);
333 ovs_mutex_unlock(&netdev_class_mutex);
335 if (!strncmp(name, "ovs-", 4)) {
340 dp_enumerate_types(&types);
341 SSET_FOR_EACH (type, &types) {
342 if (!strcmp(name+4, type)) {
343 sset_destroy(&types);
347 sset_destroy(&types);
353 /* Opens the network device named 'name' (e.g. "eth0") of the specified 'type'
354 * (e.g. "system") and returns zero if successful, otherwise a positive errno
355 * value. On success, sets '*netdevp' to the new network device, otherwise to
358 * Some network devices may need to be configured (with netdev_set_config())
359 * before they can be used. */
361 netdev_open(const char *name, const char *type, struct netdev **netdevp)
362 OVS_EXCLUDED(netdev_mutex)
364 struct netdev *netdev;
369 ovs_mutex_lock(&netdev_class_mutex);
370 ovs_mutex_lock(&netdev_mutex);
371 netdev = shash_find_data(&netdev_shash, name);
373 struct netdev_registered_class *rc;
375 rc = netdev_lookup_class(type && type[0] ? type : "system");
377 netdev = rc->class->alloc();
379 memset(netdev, 0, sizeof *netdev);
380 netdev->netdev_class = rc->class;
381 netdev->name = xstrdup(name);
382 netdev->change_seq = 1;
383 netdev->node = shash_add(&netdev_shash, name, netdev);
385 /* By default enable one tx and rx queue per netdev. */
386 netdev->n_txq = netdev->netdev_class->send ? 1 : 0;
387 netdev->n_rxq = netdev->netdev_class->rxq_alloc ? 1 : 0;
388 netdev->requested_n_rxq = netdev->n_rxq;
390 ovs_list_init(&netdev->saved_flags_list);
392 error = rc->class->construct(netdev);
395 netdev_change_seq_changed(netdev);
398 ovs_assert(ovs_list_is_empty(&netdev->saved_flags_list));
399 shash_delete(&netdev_shash, netdev->node);
400 rc->class->dealloc(netdev);
406 VLOG_WARN("could not create netdev %s of unknown type %s",
408 error = EAFNOSUPPORT;
420 ovs_mutex_unlock(&netdev_mutex);
421 ovs_mutex_unlock(&netdev_class_mutex);
426 /* Returns a reference to 'netdev_' for the caller to own. Returns null if
427 * 'netdev_' is null. */
429 netdev_ref(const struct netdev *netdev_)
430 OVS_EXCLUDED(netdev_mutex)
432 struct netdev *netdev = CONST_CAST(struct netdev *, netdev_);
435 ovs_mutex_lock(&netdev_mutex);
436 ovs_assert(netdev->ref_cnt > 0);
438 ovs_mutex_unlock(&netdev_mutex);
443 /* Reconfigures the device 'netdev' with 'args'. 'args' may be empty
444 * or NULL if none are needed. */
446 netdev_set_config(struct netdev *netdev, const struct smap *args, char **errp)
447 OVS_EXCLUDED(netdev_mutex)
449 if (netdev->netdev_class->set_config) {
450 const struct smap no_args = SMAP_INITIALIZER(&no_args);
453 error = netdev->netdev_class->set_config(netdev,
454 args ? args : &no_args);
456 VLOG_WARN_BUF(errp, "%s: could not set configuration (%s)",
457 netdev_get_name(netdev), ovs_strerror(error));
460 } else if (args && !smap_is_empty(args)) {
461 VLOG_WARN_BUF(errp, "%s: arguments provided to device that is not configurable",
462 netdev_get_name(netdev));
467 /* Returns the current configuration for 'netdev' in 'args'. The caller must
468 * have already initialized 'args' with smap_init(). Returns 0 on success, in
469 * which case 'args' will be filled with 'netdev''s configuration. On failure
470 * returns a positive errno value, in which case 'args' will be empty.
472 * The caller owns 'args' and its contents and must eventually free them with
475 netdev_get_config(const struct netdev *netdev, struct smap *args)
476 OVS_EXCLUDED(netdev_mutex)
481 if (netdev->netdev_class->get_config) {
482 error = netdev->netdev_class->get_config(netdev, args);
493 const struct netdev_tunnel_config *
494 netdev_get_tunnel_config(const struct netdev *netdev)
495 OVS_EXCLUDED(netdev_mutex)
497 if (netdev->netdev_class->get_tunnel_config) {
498 return netdev->netdev_class->get_tunnel_config(netdev);
504 /* Returns the id of the numa node the 'netdev' is on. If the function
505 * is not implemented, returns NETDEV_NUMA_UNSPEC. */
507 netdev_get_numa_id(const struct netdev *netdev)
509 if (netdev->netdev_class->get_numa_id) {
510 return netdev->netdev_class->get_numa_id(netdev);
512 return NETDEV_NUMA_UNSPEC;
517 netdev_unref(struct netdev *dev)
518 OVS_RELEASES(netdev_mutex)
520 ovs_assert(dev->ref_cnt);
521 if (!--dev->ref_cnt) {
522 const struct netdev_class *class = dev->netdev_class;
523 struct netdev_registered_class *rc;
525 dev->netdev_class->destruct(dev);
528 shash_delete(&netdev_shash, dev->node);
531 dev->netdev_class->dealloc(dev);
532 ovs_mutex_unlock(&netdev_mutex);
534 ovs_mutex_lock(&netdev_class_mutex);
535 rc = netdev_lookup_class(class->type);
536 ovs_assert(rc->ref_cnt > 0);
538 ovs_mutex_unlock(&netdev_class_mutex);
540 ovs_mutex_unlock(&netdev_mutex);
544 /* Closes and destroys 'netdev'. */
546 netdev_close(struct netdev *netdev)
547 OVS_EXCLUDED(netdev_mutex)
550 ovs_mutex_lock(&netdev_mutex);
551 netdev_unref(netdev);
555 /* Removes 'netdev' from the global shash and unrefs 'netdev'.
557 * This allows handler and revalidator threads to still retain references
558 * to this netdev while the main thread changes interface configuration.
560 * This function should only be called by the main thread when closing
561 * netdevs during user configuration changes. Otherwise, netdev_close should be
562 * used to close netdevs. */
564 netdev_remove(struct netdev *netdev)
567 ovs_mutex_lock(&netdev_mutex);
569 shash_delete(&netdev_shash, netdev->node);
571 netdev_change_seq_changed(netdev);
573 netdev_unref(netdev);
577 /* Parses 'netdev_name_', which is of the form [type@]name into its component
578 * pieces. 'name' and 'type' must be freed by the caller. */
580 netdev_parse_name(const char *netdev_name_, char **name, char **type)
582 char *netdev_name = xstrdup(netdev_name_);
585 separator = strchr(netdev_name, '@');
589 *name = xstrdup(separator + 1);
592 *type = xstrdup("system");
596 /* Attempts to open a netdev_rxq handle for obtaining packets received on
597 * 'netdev'. On success, returns 0 and stores a nonnull 'netdev_rxq *' into
598 * '*rxp'. On failure, returns a positive errno value and stores NULL into
601 * Some kinds of network devices might not support receiving packets. This
602 * function returns EOPNOTSUPP in that case.*/
604 netdev_rxq_open(struct netdev *netdev, struct netdev_rxq **rxp, int id)
605 OVS_EXCLUDED(netdev_mutex)
609 if (netdev->netdev_class->rxq_alloc && id < netdev->n_rxq) {
610 struct netdev_rxq *rx = netdev->netdev_class->rxq_alloc();
614 error = netdev->netdev_class->rxq_construct(rx);
620 netdev->netdev_class->rxq_dealloc(rx);
634 netdev_rxq_close(struct netdev_rxq *rx)
635 OVS_EXCLUDED(netdev_mutex)
638 struct netdev *netdev = rx->netdev;
639 netdev->netdev_class->rxq_destruct(rx);
640 netdev->netdev_class->rxq_dealloc(rx);
641 netdev_close(netdev);
645 /* Attempts to receive a batch of packets from 'rx'. 'pkts' should point to
646 * the beginning of an array of MAX_RX_BATCH pointers to dp_packet. If
647 * successful, this function stores pointers to up to MAX_RX_BATCH dp_packets
648 * into the array, transferring ownership of the packets to the caller, stores
649 * the number of received packets into '*cnt', and returns 0.
651 * The implementation does not necessarily initialize any non-data members of
652 * 'pkts'. That is, the caller must initialize layer pointers and metadata
653 * itself, if desired, e.g. with pkt_metadata_init() and miniflow_extract().
655 * Returns EAGAIN immediately if no packet is ready to be received or another
656 * positive errno value if an error was encountered. */
658 netdev_rxq_recv(struct netdev_rxq *rx, struct dp_packet **pkts, int *cnt)
662 retval = rx->netdev->netdev_class->rxq_recv(rx, pkts, cnt);
664 COVERAGE_INC(netdev_received);
671 /* Arranges for poll_block() to wake up when a packet is ready to be received
674 netdev_rxq_wait(struct netdev_rxq *rx)
676 rx->netdev->netdev_class->rxq_wait(rx);
679 /* Discards any packets ready to be received on 'rx'. */
681 netdev_rxq_drain(struct netdev_rxq *rx)
683 return (rx->netdev->netdev_class->rxq_drain
684 ? rx->netdev->netdev_class->rxq_drain(rx)
688 /* Configures the number of tx queues and rx queues of 'netdev'.
689 * Return 0 if successful, otherwise a positive errno value.
691 * 'n_rxq' specifies the maximum number of receive queues to create.
692 * The netdev provider might choose to create less (e.g. if the hardware
693 * supports only a smaller number). The caller can check how many have been
694 * actually created by calling 'netdev_n_rxq()'
696 * 'n_txq' specifies the exact number of transmission queues to create.
697 * If this function returns successfully, the caller can make 'n_txq'
698 * concurrent calls to netdev_send() (each one with a different 'qid' in the
699 * range [0..'n_txq'-1]).
701 * On error, the tx queue and rx queue configuration is indeterminant.
702 * Caller should make decision on whether to restore the previous or
703 * the default configuration. Also, caller must make sure there is no
704 * other thread accessing the queues at the same time. */
706 netdev_set_multiq(struct netdev *netdev, unsigned int n_txq,
711 error = (netdev->netdev_class->set_multiq
712 ? netdev->netdev_class->set_multiq(netdev,
717 if (error && error != EOPNOTSUPP) {
718 VLOG_DBG_RL(&rl, "failed to set tx/rx queue for network device %s:"
719 "%s", netdev_get_name(netdev), ovs_strerror(error));
725 /* Sends 'buffers' on 'netdev'. Returns 0 if successful (for every packet),
726 * otherwise a positive errno value. Returns EAGAIN without blocking if
727 * at least one the packets cannot be queued immediately. Returns EMSGSIZE
728 * if a partial packet was transmitted or if a packet is too big or too small
729 * to transmit on the device.
731 * If the function returns a non-zero value, some of the packets might have
734 * If 'may_steal' is false, the caller retains ownership of all the packets.
735 * If 'may_steal' is true, the caller transfers ownership of all the packets
736 * to the network device, regardless of success.
738 * The network device is expected to maintain one or more packet
739 * transmission queues, so that the caller does not ordinarily have to
740 * do additional queuing of packets. 'qid' specifies the queue to use
741 * and can be ignored if the implementation does not support multiple
744 * Some network devices may not implement support for this function. In such
745 * cases this function will always return EOPNOTSUPP. */
747 netdev_send(struct netdev *netdev, int qid, struct dp_packet **buffers,
748 int cnt, bool may_steal)
750 if (!netdev->netdev_class->send) {
752 for (int i = 0; i < cnt; i++) {
753 dp_packet_delete(buffers[i]);
759 int error = netdev->netdev_class->send(netdev, qid, buffers, cnt,
762 COVERAGE_INC(netdev_sent);
768 netdev_pop_header(struct netdev *netdev, struct dp_packet **buffers, int cnt)
772 if (!netdev->netdev_class->pop_header) {
776 for (i = 0; i < cnt; i++) {
779 err = netdev->netdev_class->pop_header(buffers[i]);
781 dp_packet_clear(buffers[i]);
789 netdev_build_header(const struct netdev *netdev, struct ovs_action_push_tnl *data,
790 const struct flow *tnl_flow)
792 if (netdev->netdev_class->build_header) {
793 return netdev->netdev_class->build_header(netdev, data, tnl_flow);
799 netdev_push_header(const struct netdev *netdev,
800 struct dp_packet **buffers, int cnt,
801 const struct ovs_action_push_tnl *data)
805 if (!netdev->netdev_class->push_header) {
809 for (i = 0; i < cnt; i++) {
810 netdev->netdev_class->push_header(buffers[i], data);
811 pkt_metadata_init(&buffers[i]->md, u32_to_odp(data->out_port));
817 /* Registers with the poll loop to wake up from the next call to poll_block()
818 * when the packet transmission queue has sufficient room to transmit a packet
819 * with netdev_send().
821 * The network device is expected to maintain one or more packet
822 * transmission queues, so that the caller does not ordinarily have to
823 * do additional queuing of packets. 'qid' specifies the queue to use
824 * and can be ignored if the implementation does not support multiple
827 netdev_send_wait(struct netdev *netdev, int qid)
829 if (netdev->netdev_class->send_wait) {
830 netdev->netdev_class->send_wait(netdev, qid);
834 /* Attempts to set 'netdev''s MAC address to 'mac'. Returns 0 if successful,
835 * otherwise a positive errno value. */
837 netdev_set_etheraddr(struct netdev *netdev, const struct eth_addr mac)
839 return netdev->netdev_class->set_etheraddr(netdev, mac);
842 /* Retrieves 'netdev''s MAC address. If successful, returns 0 and copies the
843 * the MAC address into 'mac'. On failure, returns a positive errno value and
844 * clears 'mac' to all-zeros. */
846 netdev_get_etheraddr(const struct netdev *netdev, struct eth_addr *mac)
848 return netdev->netdev_class->get_etheraddr(netdev, mac);
851 /* Returns the name of the network device that 'netdev' represents,
852 * e.g. "eth0". The caller must not modify or free the returned string. */
854 netdev_get_name(const struct netdev *netdev)
859 /* Retrieves the MTU of 'netdev'. The MTU is the maximum size of transmitted
860 * (and received) packets, in bytes, not including the hardware header; thus,
861 * this is typically 1500 bytes for Ethernet devices.
863 * If successful, returns 0 and stores the MTU size in '*mtup'. Returns
864 * EOPNOTSUPP if 'netdev' does not have an MTU (as e.g. some tunnels do not).
865 * On other failure, returns a positive errno value. On failure, sets '*mtup'
868 netdev_get_mtu(const struct netdev *netdev, int *mtup)
870 const struct netdev_class *class = netdev->netdev_class;
873 error = class->get_mtu ? class->get_mtu(netdev, mtup) : EOPNOTSUPP;
876 if (error != EOPNOTSUPP) {
877 VLOG_DBG_RL(&rl, "failed to retrieve MTU for network device %s: "
878 "%s", netdev_get_name(netdev), ovs_strerror(error));
884 /* Sets the MTU of 'netdev'. The MTU is the maximum size of transmitted
885 * (and received) packets, in bytes.
887 * If successful, returns 0. Returns EOPNOTSUPP if 'netdev' does not have an
888 * MTU (as e.g. some tunnels do not). On other failure, returns a positive
891 netdev_set_mtu(const struct netdev *netdev, int mtu)
893 const struct netdev_class *class = netdev->netdev_class;
896 error = class->set_mtu ? class->set_mtu(netdev, mtu) : EOPNOTSUPP;
897 if (error && error != EOPNOTSUPP) {
898 VLOG_DBG_RL(&rl, "failed to set MTU for network device %s: %s",
899 netdev_get_name(netdev), ovs_strerror(error));
905 /* Returns the ifindex of 'netdev', if successful, as a positive number. On
906 * failure, returns a negative errno value.
908 * The desired semantics of the ifindex value are a combination of those
909 * specified by POSIX for if_nametoindex() and by SNMP for ifIndex. An ifindex
910 * value should be unique within a host and remain stable at least until
911 * reboot. SNMP says an ifindex "ranges between 1 and the value of ifNumber"
912 * but many systems do not follow this rule anyhow.
914 * Some network devices may not implement support for this function. In such
915 * cases this function will always return -EOPNOTSUPP.
918 netdev_get_ifindex(const struct netdev *netdev)
920 int (*get_ifindex)(const struct netdev *);
922 get_ifindex = netdev->netdev_class->get_ifindex;
924 return get_ifindex ? get_ifindex(netdev) : -EOPNOTSUPP;
927 /* Stores the features supported by 'netdev' into each of '*current',
928 * '*advertised', '*supported', and '*peer' that are non-null. Each value is a
929 * bitmap of "enum ofp_port_features" bits, in host byte order. Returns 0 if
930 * successful, otherwise a positive errno value. On failure, all of the
931 * passed-in values are set to 0.
933 * Some network devices may not implement support for this function. In such
934 * cases this function will always return EOPNOTSUPP. */
936 netdev_get_features(const struct netdev *netdev,
937 enum netdev_features *current,
938 enum netdev_features *advertised,
939 enum netdev_features *supported,
940 enum netdev_features *peer)
942 int (*get_features)(const struct netdev *netdev,
943 enum netdev_features *current,
944 enum netdev_features *advertised,
945 enum netdev_features *supported,
946 enum netdev_features *peer);
947 enum netdev_features dummy[4];
954 advertised = &dummy[1];
957 supported = &dummy[2];
963 get_features = netdev->netdev_class->get_features;
965 ? get_features(netdev, current, advertised, supported,
969 *current = *advertised = *supported = *peer = 0;
974 /* Returns the maximum speed of a network connection that has the NETDEV_F_*
975 * bits in 'features', in bits per second. If no bits that indicate a speed
976 * are set in 'features', returns 'default_bps'. */
978 netdev_features_to_bps(enum netdev_features features,
979 uint64_t default_bps)
982 F_1000000MB = NETDEV_F_1TB_FD,
983 F_100000MB = NETDEV_F_100GB_FD,
984 F_40000MB = NETDEV_F_40GB_FD,
985 F_10000MB = NETDEV_F_10GB_FD,
986 F_1000MB = NETDEV_F_1GB_HD | NETDEV_F_1GB_FD,
987 F_100MB = NETDEV_F_100MB_HD | NETDEV_F_100MB_FD,
988 F_10MB = NETDEV_F_10MB_HD | NETDEV_F_10MB_FD
991 return ( features & F_1000000MB ? UINT64_C(1000000000000)
992 : features & F_100000MB ? UINT64_C(100000000000)
993 : features & F_40000MB ? UINT64_C(40000000000)
994 : features & F_10000MB ? UINT64_C(10000000000)
995 : features & F_1000MB ? UINT64_C(1000000000)
996 : features & F_100MB ? UINT64_C(100000000)
997 : features & F_10MB ? UINT64_C(10000000)
1001 /* Returns true if any of the NETDEV_F_* bits that indicate a full-duplex link
1002 * are set in 'features', otherwise false. */
1004 netdev_features_is_full_duplex(enum netdev_features features)
1006 return (features & (NETDEV_F_10MB_FD | NETDEV_F_100MB_FD | NETDEV_F_1GB_FD
1007 | NETDEV_F_10GB_FD | NETDEV_F_40GB_FD
1008 | NETDEV_F_100GB_FD | NETDEV_F_1TB_FD)) != 0;
1011 /* Set the features advertised by 'netdev' to 'advertise'. Returns 0 if
1012 * successful, otherwise a positive errno value. */
1014 netdev_set_advertisements(struct netdev *netdev,
1015 enum netdev_features advertise)
1017 return (netdev->netdev_class->set_advertisements
1018 ? netdev->netdev_class->set_advertisements(
1023 /* Assigns 'addr' as 'netdev''s IPv4 address and 'mask' as its netmask. If
1024 * 'addr' is INADDR_ANY, 'netdev''s IPv4 address is cleared. Returns a
1025 * positive errno value. */
1027 netdev_set_in4(struct netdev *netdev, struct in_addr addr, struct in_addr mask)
1029 return (netdev->netdev_class->set_in4
1030 ? netdev->netdev_class->set_in4(netdev, addr, mask)
1034 /* Obtains ad IPv4 address from device name and save the address in
1035 * in4. Returns 0 if successful, otherwise a positive errno value.
1038 netdev_get_in4_by_name(const char *device_name, struct in_addr *in4)
1040 struct in6_addr *mask, *addr6;
1044 err = netdev_open(device_name, NULL, &dev);
1049 err = netdev_get_addr_list(dev, &addr6, &mask, &n_in6);
1054 for (i = 0; i < n_in6; i++) {
1055 if (IN6_IS_ADDR_V4MAPPED(&addr6[i])) {
1056 in4->s_addr = in6_addr_get_mapped_ipv4(&addr6[i]);
1069 /* Adds 'router' as a default IP gateway for the TCP/IP stack that corresponds
1072 netdev_add_router(struct netdev *netdev, struct in_addr router)
1074 COVERAGE_INC(netdev_add_router);
1075 return (netdev->netdev_class->add_router
1076 ? netdev->netdev_class->add_router(netdev, router)
1080 /* Looks up the next hop for 'host' for the TCP/IP stack that corresponds to
1081 * 'netdev'. If a route cannot not be determined, sets '*next_hop' to 0,
1082 * '*netdev_name' to null, and returns a positive errno value. Otherwise, if a
1083 * next hop is found, stores the next hop gateway's address (0 if 'host' is on
1084 * a directly connected network) in '*next_hop' and a copy of the name of the
1085 * device to reach 'host' in '*netdev_name', and returns 0. The caller is
1086 * responsible for freeing '*netdev_name' (by calling free()). */
1088 netdev_get_next_hop(const struct netdev *netdev,
1089 const struct in_addr *host, struct in_addr *next_hop,
1092 int error = (netdev->netdev_class->get_next_hop
1093 ? netdev->netdev_class->get_next_hop(
1094 host, next_hop, netdev_name)
1097 next_hop->s_addr = 0;
1098 *netdev_name = NULL;
1103 /* Populates 'smap' with status information.
1105 * Populates 'smap' with 'netdev' specific status information. This
1106 * information may be used to populate the status column of the Interface table
1107 * as defined in ovs-vswitchd.conf.db(5). */
1109 netdev_get_status(const struct netdev *netdev, struct smap *smap)
1111 return (netdev->netdev_class->get_status
1112 ? netdev->netdev_class->get_status(netdev, smap)
1116 /* Returns all assigned IP address to 'netdev' and returns 0.
1117 * API allocates array of address and masks and set it to
1118 * '*addr' and '*mask'.
1119 * Otherwise, returns a positive errno value and sets '*addr', '*mask
1120 * and '*n_addr' to NULL.
1122 * The following error values have well-defined meanings:
1124 * - EADDRNOTAVAIL: 'netdev' has no assigned IPv6 address.
1126 * - EOPNOTSUPP: No IPv6 network stack attached to 'netdev'.
1128 * 'addr' may be null, in which case the address itself is not reported. */
1130 netdev_get_addr_list(const struct netdev *netdev, struct in6_addr **addr,
1131 struct in6_addr **mask, int *n_addr)
1135 error = (netdev->netdev_class->get_addr_list
1136 ? netdev->netdev_class->get_addr_list(netdev, addr, mask, n_addr): EOPNOTSUPP);
1137 if (error && addr) {
1146 /* On 'netdev', turns off the flags in 'off' and then turns on the flags in
1147 * 'on'. Returns 0 if successful, otherwise a positive errno value. */
1149 do_update_flags(struct netdev *netdev, enum netdev_flags off,
1150 enum netdev_flags on, enum netdev_flags *old_flagsp,
1151 struct netdev_saved_flags **sfp)
1152 OVS_EXCLUDED(netdev_mutex)
1154 struct netdev_saved_flags *sf = NULL;
1155 enum netdev_flags old_flags;
1158 error = netdev->netdev_class->update_flags(netdev, off & ~on, on,
1161 VLOG_WARN_RL(&rl, "failed to %s flags for network device %s: %s",
1162 off || on ? "set" : "get", netdev_get_name(netdev),
1163 ovs_strerror(error));
1165 } else if ((off || on) && sfp) {
1166 enum netdev_flags new_flags = (old_flags & ~off) | on;
1167 enum netdev_flags changed_flags = old_flags ^ new_flags;
1168 if (changed_flags) {
1169 ovs_mutex_lock(&netdev_mutex);
1170 *sfp = sf = xmalloc(sizeof *sf);
1171 sf->netdev = netdev;
1172 ovs_list_push_front(&netdev->saved_flags_list, &sf->node);
1173 sf->saved_flags = changed_flags;
1174 sf->saved_values = changed_flags & new_flags;
1177 ovs_mutex_unlock(&netdev_mutex);
1182 *old_flagsp = old_flags;
1191 /* Obtains the current flags for 'netdev' and stores them into '*flagsp'.
1192 * Returns 0 if successful, otherwise a positive errno value. On failure,
1193 * stores 0 into '*flagsp'. */
1195 netdev_get_flags(const struct netdev *netdev_, enum netdev_flags *flagsp)
1197 struct netdev *netdev = CONST_CAST(struct netdev *, netdev_);
1198 return do_update_flags(netdev, 0, 0, flagsp, NULL);
1201 /* Sets the flags for 'netdev' to 'flags'.
1202 * Returns 0 if successful, otherwise a positive errno value. */
1204 netdev_set_flags(struct netdev *netdev, enum netdev_flags flags,
1205 struct netdev_saved_flags **sfp)
1207 return do_update_flags(netdev, -1, flags, NULL, sfp);
1210 /* Turns on the specified 'flags' on 'netdev':
1212 * - On success, returns 0. If 'sfp' is nonnull, sets '*sfp' to a newly
1213 * allocated 'struct netdev_saved_flags *' that may be passed to
1214 * netdev_restore_flags() to restore the original values of 'flags' on
1215 * 'netdev' (this will happen automatically at program termination if
1216 * netdev_restore_flags() is never called) , or to NULL if no flags were
1219 * - On failure, returns a positive errno value. If 'sfp' is nonnull, sets
1220 * '*sfp' to NULL. */
1222 netdev_turn_flags_on(struct netdev *netdev, enum netdev_flags flags,
1223 struct netdev_saved_flags **sfp)
1225 return do_update_flags(netdev, 0, flags, NULL, sfp);
1228 /* Turns off the specified 'flags' on 'netdev'. See netdev_turn_flags_on() for
1229 * details of the interface. */
1231 netdev_turn_flags_off(struct netdev *netdev, enum netdev_flags flags,
1232 struct netdev_saved_flags **sfp)
1234 return do_update_flags(netdev, flags, 0, NULL, sfp);
1237 /* Restores the flags that were saved in 'sf', and destroys 'sf'.
1238 * Does nothing if 'sf' is NULL. */
1240 netdev_restore_flags(struct netdev_saved_flags *sf)
1241 OVS_EXCLUDED(netdev_mutex)
1244 struct netdev *netdev = sf->netdev;
1245 enum netdev_flags old_flags;
1247 netdev->netdev_class->update_flags(netdev,
1248 sf->saved_flags & sf->saved_values,
1249 sf->saved_flags & ~sf->saved_values,
1252 ovs_mutex_lock(&netdev_mutex);
1253 ovs_list_remove(&sf->node);
1255 netdev_unref(netdev);
1259 /* Looks up the ARP table entry for 'ip' on 'netdev'. If one exists and can be
1260 * successfully retrieved, it stores the corresponding MAC address in 'mac' and
1261 * returns 0. Otherwise, it returns a positive errno value; in particular,
1262 * ENXIO indicates that there is no ARP table entry for 'ip' on 'netdev'. */
1264 netdev_arp_lookup(const struct netdev *netdev,
1265 ovs_be32 ip, struct eth_addr *mac)
1267 int error = (netdev->netdev_class->arp_lookup
1268 ? netdev->netdev_class->arp_lookup(netdev, ip, mac)
1271 *mac = eth_addr_zero;
1276 /* Returns true if carrier is active (link light is on) on 'netdev'. */
1278 netdev_get_carrier(const struct netdev *netdev)
1281 enum netdev_flags flags;
1284 netdev_get_flags(netdev, &flags);
1285 if (!(flags & NETDEV_UP)) {
1289 if (!netdev->netdev_class->get_carrier) {
1293 error = netdev->netdev_class->get_carrier(netdev, &carrier);
1295 VLOG_DBG("%s: failed to get network device carrier status, assuming "
1296 "down: %s", netdev_get_name(netdev), ovs_strerror(error));
1303 /* Returns the number of times 'netdev''s carrier has changed. */
1305 netdev_get_carrier_resets(const struct netdev *netdev)
1307 return (netdev->netdev_class->get_carrier_resets
1308 ? netdev->netdev_class->get_carrier_resets(netdev)
1312 /* Attempts to force netdev_get_carrier() to poll 'netdev''s MII registers for
1313 * link status instead of checking 'netdev''s carrier. 'netdev''s MII
1314 * registers will be polled once ever 'interval' milliseconds. If 'netdev'
1315 * does not support MII, another method may be used as a fallback. If
1316 * 'interval' is less than or equal to zero, reverts netdev_get_carrier() to
1317 * its normal behavior.
1319 * Returns 0 if successful, otherwise a positive errno value. */
1321 netdev_set_miimon_interval(struct netdev *netdev, long long int interval)
1323 return (netdev->netdev_class->set_miimon_interval
1324 ? netdev->netdev_class->set_miimon_interval(netdev, interval)
1328 /* Retrieves current device stats for 'netdev'. */
1330 netdev_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
1334 /* Statistics are initialized before passing it to particular device
1335 * implementation so all values are filtered out by default. */
1336 memset(stats, 0xFF, sizeof *stats);
1338 COVERAGE_INC(netdev_get_stats);
1339 error = (netdev->netdev_class->get_stats
1340 ? netdev->netdev_class->get_stats(netdev, stats)
1343 /* In case of error all statistics are filtered out */
1344 memset(stats, 0xff, sizeof *stats);
1349 /* Attempts to set input rate limiting (policing) policy, such that up to
1350 * 'kbits_rate' kbps of traffic is accepted, with a maximum accumulative burst
1351 * size of 'kbits' kb. */
1353 netdev_set_policing(struct netdev *netdev, uint32_t kbits_rate,
1354 uint32_t kbits_burst)
1356 return (netdev->netdev_class->set_policing
1357 ? netdev->netdev_class->set_policing(netdev,
1358 kbits_rate, kbits_burst)
1362 /* Adds to 'types' all of the forms of QoS supported by 'netdev', or leaves it
1363 * empty if 'netdev' does not support QoS. Any names added to 'types' should
1364 * be documented as valid for the "type" column in the "QoS" table in
1365 * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
1367 * Every network device supports disabling QoS with a type of "", but this type
1368 * will not be added to 'types'.
1370 * The caller must initialize 'types' (e.g. with sset_init()) before calling
1371 * this function. The caller is responsible for destroying 'types' (e.g. with
1372 * sset_destroy()) when it is no longer needed.
1374 * Returns 0 if successful, otherwise a positive errno value. */
1376 netdev_get_qos_types(const struct netdev *netdev, struct sset *types)
1378 const struct netdev_class *class = netdev->netdev_class;
1379 return (class->get_qos_types
1380 ? class->get_qos_types(netdev, types)
1384 /* Queries 'netdev' for its capabilities regarding the specified 'type' of QoS,
1385 * which should be "" or one of the types returned by netdev_get_qos_types()
1386 * for 'netdev'. Returns 0 if successful, otherwise a positive errno value.
1387 * On success, initializes 'caps' with the QoS capabilities; on failure, clears
1388 * 'caps' to all zeros. */
1390 netdev_get_qos_capabilities(const struct netdev *netdev, const char *type,
1391 struct netdev_qos_capabilities *caps)
1393 const struct netdev_class *class = netdev->netdev_class;
1396 int retval = (class->get_qos_capabilities
1397 ? class->get_qos_capabilities(netdev, type, caps)
1400 memset(caps, 0, sizeof *caps);
1404 /* Every netdev supports turning off QoS. */
1405 memset(caps, 0, sizeof *caps);
1410 /* Obtains the number of queues supported by 'netdev' for the specified 'type'
1411 * of QoS. Returns 0 if successful, otherwise a positive errno value. Stores
1412 * the number of queues (zero on failure) in '*n_queuesp'.
1414 * This is just a simple wrapper around netdev_get_qos_capabilities(). */
1416 netdev_get_n_queues(const struct netdev *netdev,
1417 const char *type, unsigned int *n_queuesp)
1419 struct netdev_qos_capabilities caps;
1422 retval = netdev_get_qos_capabilities(netdev, type, &caps);
1423 *n_queuesp = caps.n_queues;
1427 /* Queries 'netdev' about its currently configured form of QoS. If successful,
1428 * stores the name of the current form of QoS into '*typep', stores any details
1429 * of configuration as string key-value pairs in 'details', and returns 0. On
1430 * failure, sets '*typep' to NULL and returns a positive errno value.
1432 * A '*typep' of "" indicates that QoS is currently disabled on 'netdev'.
1434 * The caller must initialize 'details' as an empty smap (e.g. with
1435 * smap_init()) before calling this function. The caller must free 'details'
1436 * when it is no longer needed (e.g. with smap_destroy()).
1438 * The caller must not modify or free '*typep'.
1440 * '*typep' will be one of the types returned by netdev_get_qos_types() for
1441 * 'netdev'. The contents of 'details' should be documented as valid for
1442 * '*typep' in the "other_config" column in the "QoS" table in
1443 * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)). */
1445 netdev_get_qos(const struct netdev *netdev,
1446 const char **typep, struct smap *details)
1448 const struct netdev_class *class = netdev->netdev_class;
1451 if (class->get_qos) {
1452 retval = class->get_qos(netdev, typep, details);
1455 smap_clear(details);
1459 /* 'netdev' doesn't support QoS, so report that QoS is disabled. */
1465 /* Attempts to reconfigure QoS on 'netdev', changing the form of QoS to 'type'
1466 * with details of configuration from 'details'. Returns 0 if successful,
1467 * otherwise a positive errno value. On error, the previous QoS configuration
1470 * When this function changes the type of QoS (not just 'details'), this also
1471 * resets all queue configuration for 'netdev' to their defaults (which depend
1472 * on the specific type of QoS). Otherwise, the queue configuration for
1473 * 'netdev' is unchanged.
1475 * 'type' should be "" (to disable QoS) or one of the types returned by
1476 * netdev_get_qos_types() for 'netdev'. The contents of 'details' should be
1477 * documented as valid for the given 'type' in the "other_config" column in the
1478 * "QoS" table in vswitchd/vswitch.xml (which is built as
1479 * ovs-vswitchd.conf.db(8)).
1481 * NULL may be specified for 'details' if there are no configuration
1484 netdev_set_qos(struct netdev *netdev,
1485 const char *type, const struct smap *details)
1487 const struct netdev_class *class = netdev->netdev_class;
1493 if (class->set_qos) {
1495 static const struct smap empty = SMAP_INITIALIZER(&empty);
1498 return class->set_qos(netdev, type, details);
1500 return *type ? EOPNOTSUPP : 0;
1504 /* Queries 'netdev' for information about the queue numbered 'queue_id'. If
1505 * successful, adds that information as string key-value pairs to 'details'.
1506 * Returns 0 if successful, otherwise a positive errno value.
1508 * 'queue_id' must be less than the number of queues supported by 'netdev' for
1509 * the current form of QoS (e.g. as returned by netdev_get_n_queues(netdev)).
1511 * The returned contents of 'details' should be documented as valid for the
1512 * given 'type' in the "other_config" column in the "Queue" table in
1513 * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
1515 * The caller must initialize 'details' (e.g. with smap_init()) before calling
1516 * this function. The caller must free 'details' when it is no longer needed
1517 * (e.g. with smap_destroy()). */
1519 netdev_get_queue(const struct netdev *netdev,
1520 unsigned int queue_id, struct smap *details)
1522 const struct netdev_class *class = netdev->netdev_class;
1525 retval = (class->get_queue
1526 ? class->get_queue(netdev, queue_id, details)
1529 smap_clear(details);
1534 /* Configures the queue numbered 'queue_id' on 'netdev' with the key-value
1535 * string pairs in 'details'. The contents of 'details' should be documented
1536 * as valid for the given 'type' in the "other_config" column in the "Queue"
1537 * table in vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
1538 * Returns 0 if successful, otherwise a positive errno value. On failure, the
1539 * given queue's configuration should be unmodified.
1541 * 'queue_id' must be less than the number of queues supported by 'netdev' for
1542 * the current form of QoS (e.g. as returned by netdev_get_n_queues(netdev)).
1544 * This function does not modify 'details', and the caller retains ownership of
1547 netdev_set_queue(struct netdev *netdev,
1548 unsigned int queue_id, const struct smap *details)
1550 const struct netdev_class *class = netdev->netdev_class;
1551 return (class->set_queue
1552 ? class->set_queue(netdev, queue_id, details)
1556 /* Attempts to delete the queue numbered 'queue_id' from 'netdev'. Some kinds
1557 * of QoS may have a fixed set of queues, in which case attempts to delete them
1558 * will fail with EOPNOTSUPP.
1560 * Returns 0 if successful, otherwise a positive errno value. On failure, the
1561 * given queue will be unmodified.
1563 * 'queue_id' must be less than the number of queues supported by 'netdev' for
1564 * the current form of QoS (e.g. as returned by
1565 * netdev_get_n_queues(netdev)). */
1567 netdev_delete_queue(struct netdev *netdev, unsigned int queue_id)
1569 const struct netdev_class *class = netdev->netdev_class;
1570 return (class->delete_queue
1571 ? class->delete_queue(netdev, queue_id)
1575 /* Obtains statistics about 'queue_id' on 'netdev'. On success, returns 0 and
1576 * fills 'stats' with the queue's statistics; individual members of 'stats' may
1577 * be set to all-1-bits if the statistic is unavailable. On failure, returns a
1578 * positive errno value and fills 'stats' with values indicating unsupported
1581 netdev_get_queue_stats(const struct netdev *netdev, unsigned int queue_id,
1582 struct netdev_queue_stats *stats)
1584 const struct netdev_class *class = netdev->netdev_class;
1587 retval = (class->get_queue_stats
1588 ? class->get_queue_stats(netdev, queue_id, stats)
1591 stats->tx_bytes = UINT64_MAX;
1592 stats->tx_packets = UINT64_MAX;
1593 stats->tx_errors = UINT64_MAX;
1594 stats->created = LLONG_MIN;
1599 /* Initializes 'dump' to begin dumping the queues in a netdev.
1601 * This function provides no status indication. An error status for the entire
1602 * dump operation is provided when it is completed by calling
1603 * netdev_queue_dump_done().
1606 netdev_queue_dump_start(struct netdev_queue_dump *dump,
1607 const struct netdev *netdev)
1609 dump->netdev = netdev_ref(netdev);
1610 if (netdev->netdev_class->queue_dump_start) {
1611 dump->error = netdev->netdev_class->queue_dump_start(netdev,
1614 dump->error = EOPNOTSUPP;
1618 /* Attempts to retrieve another queue from 'dump', which must have been
1619 * initialized with netdev_queue_dump_start(). On success, stores a new queue
1620 * ID into '*queue_id', fills 'details' with configuration details for the
1621 * queue, and returns true. On failure, returns false.
1623 * Queues are not necessarily dumped in increasing order of queue ID (or any
1624 * other predictable order).
1626 * Failure might indicate an actual error or merely that the last queue has
1627 * been dumped. An error status for the entire dump operation is provided when
1628 * it is completed by calling netdev_queue_dump_done().
1630 * The returned contents of 'details' should be documented as valid for the
1631 * given 'type' in the "other_config" column in the "Queue" table in
1632 * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
1634 * The caller must initialize 'details' (e.g. with smap_init()) before calling
1635 * this function. This function will clear and replace its contents. The
1636 * caller must free 'details' when it is no longer needed (e.g. with
1637 * smap_destroy()). */
1639 netdev_queue_dump_next(struct netdev_queue_dump *dump,
1640 unsigned int *queue_id, struct smap *details)
1642 const struct netdev *netdev = dump->netdev;
1648 dump->error = netdev->netdev_class->queue_dump_next(netdev, dump->state,
1652 netdev->netdev_class->queue_dump_done(netdev, dump->state);
1658 /* Completes queue table dump operation 'dump', which must have been
1659 * initialized with netdev_queue_dump_start(). Returns 0 if the dump operation
1660 * was error-free, otherwise a positive errno value describing the problem. */
1662 netdev_queue_dump_done(struct netdev_queue_dump *dump)
1664 const struct netdev *netdev = dump->netdev;
1665 if (!dump->error && netdev->netdev_class->queue_dump_done) {
1666 dump->error = netdev->netdev_class->queue_dump_done(netdev,
1669 netdev_close(dump->netdev);
1670 return dump->error == EOF ? 0 : dump->error;
1673 /* Iterates over all of 'netdev''s queues, calling 'cb' with the queue's ID,
1674 * its statistics, and the 'aux' specified by the caller. The order of
1675 * iteration is unspecified, but (when successful) each queue is visited
1678 * Calling this function may be more efficient than calling
1679 * netdev_get_queue_stats() for every queue.
1681 * 'cb' must not modify or free the statistics passed in.
1683 * Returns 0 if successful, otherwise a positive errno value. On error, some
1684 * configured queues may not have been included in the iteration. */
1686 netdev_dump_queue_stats(const struct netdev *netdev,
1687 netdev_dump_queue_stats_cb *cb, void *aux)
1689 const struct netdev_class *class = netdev->netdev_class;
1690 return (class->dump_queue_stats
1691 ? class->dump_queue_stats(netdev, cb, aux)
1696 /* Returns the class type of 'netdev'.
1698 * The caller must not free the returned value. */
1700 netdev_get_type(const struct netdev *netdev)
1702 return netdev->netdev_class->type;
1705 /* Returns the class associated with 'netdev'. */
1706 const struct netdev_class *
1707 netdev_get_class(const struct netdev *netdev)
1709 return netdev->netdev_class;
1712 /* Returns the netdev with 'name' or NULL if there is none.
1714 * The caller must free the returned netdev with netdev_close(). */
1716 netdev_from_name(const char *name)
1717 OVS_EXCLUDED(netdev_mutex)
1719 struct netdev *netdev;
1721 ovs_mutex_lock(&netdev_mutex);
1722 netdev = shash_find_data(&netdev_shash, name);
1726 ovs_mutex_unlock(&netdev_mutex);
1731 /* Fills 'device_list' with devices that match 'netdev_class'.
1733 * The caller is responsible for initializing and destroying 'device_list' and
1734 * must close each device on the list. */
1736 netdev_get_devices(const struct netdev_class *netdev_class,
1737 struct shash *device_list)
1738 OVS_EXCLUDED(netdev_mutex)
1740 struct shash_node *node;
1742 ovs_mutex_lock(&netdev_mutex);
1743 SHASH_FOR_EACH (node, &netdev_shash) {
1744 struct netdev *dev = node->data;
1746 if (dev->netdev_class == netdev_class) {
1748 shash_add(device_list, node->name, node->data);
1751 ovs_mutex_unlock(&netdev_mutex);
1754 /* Extracts pointers to all 'netdev-vports' into an array 'vports'
1755 * and returns it. Stores the size of the array into '*size'.
1757 * The caller is responsible for freeing 'vports' and must close
1758 * each 'netdev-vport' in the list. */
1760 netdev_get_vports(size_t *size)
1761 OVS_EXCLUDED(netdev_mutex)
1763 struct netdev **vports;
1764 struct shash_node *node;
1771 /* Explicitly allocates big enough chunk of memory. */
1772 vports = xmalloc(shash_count(&netdev_shash) * sizeof *vports);
1773 ovs_mutex_lock(&netdev_mutex);
1774 SHASH_FOR_EACH (node, &netdev_shash) {
1775 struct netdev *dev = node->data;
1777 if (netdev_vport_is_vport_class(dev->netdev_class)) {
1783 ovs_mutex_unlock(&netdev_mutex);
1790 netdev_get_type_from_name(const char *name)
1792 struct netdev *dev = netdev_from_name(name);
1793 const char *type = dev ? netdev_get_type(dev) : NULL;
1799 netdev_rxq_get_netdev(const struct netdev_rxq *rx)
1801 ovs_assert(rx->netdev->ref_cnt > 0);
1806 netdev_rxq_get_name(const struct netdev_rxq *rx)
1808 return netdev_get_name(netdev_rxq_get_netdev(rx));
1812 netdev_rxq_get_queue_id(const struct netdev_rxq *rx)
1814 return rx->queue_id;
1818 restore_all_flags(void *aux OVS_UNUSED)
1820 struct shash_node *node;
1822 SHASH_FOR_EACH (node, &netdev_shash) {
1823 struct netdev *netdev = node->data;
1824 const struct netdev_saved_flags *sf;
1825 enum netdev_flags saved_values;
1826 enum netdev_flags saved_flags;
1828 saved_values = saved_flags = 0;
1829 LIST_FOR_EACH (sf, node, &netdev->saved_flags_list) {
1830 saved_flags |= sf->saved_flags;
1831 saved_values &= ~sf->saved_flags;
1832 saved_values |= sf->saved_flags & sf->saved_values;
1835 enum netdev_flags old_flags;
1837 netdev->netdev_class->update_flags(netdev,
1838 saved_flags & saved_values,
1839 saved_flags & ~saved_values,
1846 netdev_get_change_seq(const struct netdev *netdev)
1848 return netdev->change_seq;
1852 /* This implementation is shared by Linux and BSD. */
1854 static struct ifaddrs *if_addr_list;
1855 static struct ovs_mutex if_addr_list_lock = OVS_MUTEX_INITIALIZER;
1858 netdev_get_addrs_list_flush(void)
1860 ovs_mutex_lock(&if_addr_list_lock);
1862 freeifaddrs(if_addr_list);
1863 if_addr_list = NULL;
1865 ovs_mutex_unlock(&if_addr_list_lock);
1869 netdev_get_addrs(const char dev[], struct in6_addr **paddr,
1870 struct in6_addr **pmask, int *n_in)
1872 struct in6_addr *addr_array, *mask_array;
1873 const struct ifaddrs *ifa;
1876 ovs_mutex_lock(&if_addr_list_lock);
1877 if (!if_addr_list) {
1880 err = getifaddrs(&if_addr_list);
1882 ovs_mutex_unlock(&if_addr_list_lock);
1887 for (ifa = if_addr_list; ifa; ifa = ifa->ifa_next) {
1888 if (ifa->ifa_addr != NULL) {
1891 family = ifa->ifa_addr->sa_family;
1892 if (family == AF_INET || family == AF_INET6) {
1893 if (!strncmp(ifa->ifa_name, dev, IFNAMSIZ)) {
1901 ovs_mutex_unlock(&if_addr_list_lock);
1902 return EADDRNOTAVAIL;
1904 addr_array = xzalloc(sizeof *addr_array * cnt);
1905 mask_array = xzalloc(sizeof *mask_array * cnt);
1906 for (ifa = if_addr_list; ifa; ifa = ifa->ifa_next) {
1909 if (strncmp(ifa->ifa_name, dev, IFNAMSIZ) || ifa->ifa_addr == NULL) {
1913 family = ifa->ifa_addr->sa_family;
1914 if (family == AF_INET) {
1915 const struct sockaddr_in *sin;
1917 sin = ALIGNED_CAST(const struct sockaddr_in *, ifa->ifa_addr);
1918 in6_addr_set_mapped_ipv4(&addr_array[i], sin->sin_addr.s_addr);
1919 sin = (struct sockaddr_in *) &ifa->ifa_netmask;
1920 in6_addr_set_mapped_ipv4(&mask_array[i], sin->sin_addr.s_addr);
1922 } else if (family == AF_INET6) {
1923 const struct sockaddr_in6 *sin6;
1925 sin6 = ALIGNED_CAST(const struct sockaddr_in6 *, ifa->ifa_addr);
1926 memcpy(&addr_array[i], &sin6->sin6_addr, sizeof *addr_array);
1927 sin6 = (struct sockaddr_in6 *) &ifa->ifa_netmask;
1928 memcpy(&mask_array[i], &sin6->sin6_addr, sizeof *mask_array);
1932 ovs_mutex_unlock(&if_addr_list_lock);
1935 *paddr = addr_array;
1936 *pmask = mask_array;