2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 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>
28 #include "dynamic-string.h"
29 #include "fatal-signal.h"
32 #include "netdev-provider.h"
33 #include "netdev-vport.h"
35 #include "openflow/openflow.h"
37 #include "poll-loop.h"
44 VLOG_DEFINE_THIS_MODULE(netdev);
46 COVERAGE_DEFINE(netdev_received);
47 COVERAGE_DEFINE(netdev_sent);
48 COVERAGE_DEFINE(netdev_add_router);
49 COVERAGE_DEFINE(netdev_get_stats);
51 struct netdev_saved_flags {
52 struct netdev_dev *dev;
53 struct list node; /* In struct netdev_dev's saved_flags_list. */
54 enum netdev_flags saved_flags;
55 enum netdev_flags saved_values;
58 static struct shash netdev_classes = SHASH_INITIALIZER(&netdev_classes);
60 /* All created network devices. */
61 static struct shash netdev_dev_shash = SHASH_INITIALIZER(&netdev_dev_shash);
63 /* All open network devices. */
64 static struct list netdev_list = LIST_INITIALIZER(&netdev_list);
66 /* This is set pretty low because we probably won't learn anything from the
67 * additional log messages. */
68 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
70 static void restore_all_flags(void *aux OVS_UNUSED);
71 void update_device_args(struct netdev_dev *, const struct shash *args);
74 netdev_initialize(void)
81 fatal_signal_add_hook(restore_all_flags, NULL, NULL, true);
82 netdev_vport_patch_register();
85 netdev_register_provider(&netdev_linux_class);
86 netdev_register_provider(&netdev_internal_class);
87 netdev_register_provider(&netdev_tap_class);
88 netdev_vport_tunnel_register();
91 netdev_register_provider(&netdev_tap_class);
92 netdev_register_provider(&netdev_bsd_class);
94 netdev_register_provider(&netdev_tunnel_class);
95 netdev_register_provider(&netdev_pltap_class);
99 /* Performs periodic work needed by all the various kinds of netdevs.
101 * If your program opens any netdevs, it must call this function within its
106 struct shash_node *node;
107 SHASH_FOR_EACH(node, &netdev_classes) {
108 const struct netdev_class *netdev_class = node->data;
109 if (netdev_class->run) {
115 /* Arranges for poll_block() to wake up when netdev_run() needs to be called.
117 * If your program opens any netdevs, it must call this function within its
122 struct shash_node *node;
123 SHASH_FOR_EACH(node, &netdev_classes) {
124 const struct netdev_class *netdev_class = node->data;
125 if (netdev_class->wait) {
126 netdev_class->wait();
131 /* Initializes and registers a new netdev provider. After successful
132 * registration, new netdevs of that type can be opened using netdev_open(). */
134 netdev_register_provider(const struct netdev_class *new_class)
136 if (shash_find(&netdev_classes, new_class->type)) {
137 VLOG_WARN("attempted to register duplicate netdev provider: %s",
142 if (new_class->init) {
143 int error = new_class->init();
145 VLOG_ERR("failed to initialize %s network device class: %s",
146 new_class->type, strerror(error));
151 shash_add(&netdev_classes, new_class->type, new_class);
156 /* Unregisters a netdev provider. 'type' must have been previously
157 * registered and not currently be in use by any netdevs. After unregistration
158 * new netdevs of that type cannot be opened using netdev_open(). */
160 netdev_unregister_provider(const char *type)
162 struct shash_node *del_node, *netdev_dev_node;
164 del_node = shash_find(&netdev_classes, type);
166 VLOG_WARN("attempted to unregister a netdev provider that is not "
167 "registered: %s", type);
171 SHASH_FOR_EACH(netdev_dev_node, &netdev_dev_shash) {
172 struct netdev_dev *netdev_dev = netdev_dev_node->data;
173 if (!strcmp(netdev_dev->netdev_class->type, type)) {
174 VLOG_WARN("attempted to unregister in use netdev provider: %s",
180 shash_delete(&netdev_classes, del_node);
185 const struct netdev_class *
186 netdev_lookup_provider(const char *type)
189 return shash_find_data(&netdev_classes, type && type[0] ? type : "system");
192 /* Clears 'types' and enumerates the types of all currently registered netdev
193 * providers into it. The caller must first initialize the sset. */
195 netdev_enumerate_types(struct sset *types)
197 struct shash_node *node;
202 SHASH_FOR_EACH(node, &netdev_classes) {
203 const struct netdev_class *netdev_class = node->data;
204 sset_add(types, netdev_class->type);
208 /* Opens the network device named 'name' (e.g. "eth0") of the specified 'type'
209 * (e.g. "system") and returns zero if successful, otherwise a positive errno
210 * value. On success, sets '*netdevp' to the new network device, otherwise to
213 * Some network devices may need to be configured (with netdev_set_config())
214 * before they can be used. */
216 netdev_open(const char *name, const char *type, struct netdev **netdevp)
218 struct netdev_dev *netdev_dev;
224 netdev_dev = shash_find_data(&netdev_dev_shash, name);
227 const struct netdev_class *class;
229 class = netdev_lookup_provider(type);
231 VLOG_WARN("could not create netdev %s of unknown type %s",
235 error = class->create(class, name, &netdev_dev);
239 ovs_assert(netdev_dev->netdev_class == class);
243 error = netdev_dev->netdev_class->open(netdev_dev, netdevp);
246 netdev_dev->ref_cnt++;
248 if (!netdev_dev->ref_cnt) {
249 netdev_dev_uninit(netdev_dev, true);
256 /* Reconfigures the device 'netdev' with 'args'. 'args' may be empty
257 * or NULL if none are needed. */
259 netdev_set_config(struct netdev *netdev, const struct smap *args)
261 struct netdev_dev *netdev_dev = netdev_get_dev(netdev);
263 if (netdev_dev->netdev_class->set_config) {
264 struct smap no_args = SMAP_INITIALIZER(&no_args);
265 return netdev_dev->netdev_class->set_config(netdev_dev,
266 args ? args : &no_args);
267 } else if (args && !smap_is_empty(args)) {
268 VLOG_WARN("%s: arguments provided to device that is not configurable",
269 netdev_get_name(netdev));
275 /* Returns the current configuration for 'netdev' in 'args'. The caller must
276 * have already initialized 'args' with smap_init(). Returns 0 on success, in
277 * which case 'args' will be filled with 'netdev''s configuration. On failure
278 * returns a positive errno value, in which case 'args' will be empty.
280 * The caller owns 'args' and its contents and must eventually free them with
283 netdev_get_config(const struct netdev *netdev, struct smap *args)
285 struct netdev_dev *netdev_dev = netdev_get_dev(netdev);
289 if (netdev_dev->netdev_class->get_config) {
290 error = netdev_dev->netdev_class->get_config(netdev_dev, args);
301 const struct netdev_tunnel_config *
302 netdev_get_tunnel_config(const struct netdev *netdev)
304 struct netdev_dev *netdev_dev = netdev_get_dev(netdev);
306 if (netdev_dev->netdev_class->get_tunnel_config) {
307 return netdev_dev->netdev_class->get_tunnel_config(netdev_dev);
314 netdev_dev_unref(struct netdev_dev *dev)
316 ovs_assert(dev->ref_cnt);
317 if (!--dev->ref_cnt) {
318 netdev_dev_uninit(dev, true);
322 /* Closes and destroys 'netdev'. */
324 netdev_close(struct netdev *netdev)
327 struct netdev_dev *dev = netdev_get_dev(netdev);
329 netdev_uninit(netdev, true);
330 netdev_dev_unref(dev);
334 /* Parses 'netdev_name_', which is of the form [type@]name into its component
335 * pieces. 'name' and 'type' must be freed by the caller. */
337 netdev_parse_name(const char *netdev_name_, char **name, char **type)
339 char *netdev_name = xstrdup(netdev_name_);
342 separator = strchr(netdev_name, '@');
346 *name = xstrdup(separator + 1);
349 *type = xstrdup("system");
354 netdev_rx_open(struct netdev *netdev, struct netdev_rx **rxp)
356 struct netdev_dev *dev = netdev_get_dev(netdev);
359 error = (dev->netdev_class->rx_open
360 ? dev->netdev_class->rx_open(netdev, rxp)
363 ovs_assert((*rxp)->netdev_dev == dev);
372 netdev_rx_close(struct netdev_rx *rx)
375 struct netdev_dev *dev = rx->netdev_dev;
377 rx->rx_class->destroy(rx);
378 netdev_dev_unref(dev);
383 netdev_rx_recv(struct netdev_rx *rx, struct ofpbuf *buffer)
387 ovs_assert(buffer->size == 0);
388 ovs_assert(ofpbuf_tailroom(buffer) >= ETH_TOTAL_MIN);
390 retval = rx->rx_class->recv(rx, buffer->data, ofpbuf_tailroom(buffer));
392 COVERAGE_INC(netdev_received);
393 buffer->size += retval;
394 if (buffer->size < ETH_TOTAL_MIN) {
395 ofpbuf_put_zeros(buffer, ETH_TOTAL_MIN - buffer->size);
404 netdev_rx_wait(struct netdev_rx *rx)
406 rx->rx_class->wait(rx);
410 netdev_rx_drain(struct netdev_rx *rx)
412 return rx->rx_class->drain ? rx->rx_class->drain(rx) : 0;
415 /* Sends 'buffer' on 'netdev'. Returns 0 if successful, otherwise a positive
416 * errno value. Returns EAGAIN without blocking if the packet cannot be queued
417 * immediately. Returns EMSGSIZE if a partial packet was transmitted or if
418 * the packet is too big or too small to transmit on the device.
420 * The caller retains ownership of 'buffer' in all cases.
422 * The kernel maintains a packet transmission queue, so the caller is not
423 * expected to do additional queuing of packets.
425 * Some network devices may not implement support for this function. In such
426 * cases this function will always return EOPNOTSUPP. */
428 netdev_send(struct netdev *netdev, const struct ofpbuf *buffer)
430 int (*send)(struct netdev *, const void *, size_t);
433 send = netdev_get_dev(netdev)->netdev_class->send;
434 error = send ? (send)(netdev, buffer->data, buffer->size) : EOPNOTSUPP;
436 COVERAGE_INC(netdev_sent);
441 /* Registers with the poll loop to wake up from the next call to poll_block()
442 * when the packet transmission queue has sufficient room to transmit a packet
443 * with netdev_send().
445 * The kernel maintains a packet transmission queue, so the client is not
446 * expected to do additional queuing of packets. Thus, this function is
447 * unlikely to ever be used. It is included for completeness. */
449 netdev_send_wait(struct netdev *netdev)
451 void (*send_wait)(struct netdev *);
453 send_wait = netdev_get_dev(netdev)->netdev_class->send_wait;
459 /* Attempts to set 'netdev''s MAC address to 'mac'. Returns 0 if successful,
460 * otherwise a positive errno value. */
462 netdev_set_etheraddr(struct netdev *netdev, const uint8_t mac[ETH_ADDR_LEN])
464 return netdev_get_dev(netdev)->netdev_class->set_etheraddr(netdev, mac);
467 /* Retrieves 'netdev''s MAC address. If successful, returns 0 and copies the
468 * the MAC address into 'mac'. On failure, returns a positive errno value and
469 * clears 'mac' to all-zeros. */
471 netdev_get_etheraddr(const struct netdev *netdev, uint8_t mac[ETH_ADDR_LEN])
473 return netdev_get_dev(netdev)->netdev_class->get_etheraddr(netdev, mac);
476 /* Returns the name of the network device that 'netdev' represents,
477 * e.g. "eth0". The caller must not modify or free the returned string. */
479 netdev_get_name(const struct netdev *netdev)
481 return netdev_get_dev(netdev)->name;
484 /* Retrieves the MTU of 'netdev'. The MTU is the maximum size of transmitted
485 * (and received) packets, in bytes, not including the hardware header; thus,
486 * this is typically 1500 bytes for Ethernet devices.
488 * If successful, returns 0 and stores the MTU size in '*mtup'. Returns
489 * EOPNOTSUPP if 'netdev' does not have an MTU (as e.g. some tunnels do not).
490 * On other failure, returns a positive errno value. On failure, sets '*mtup'
493 netdev_get_mtu(const struct netdev *netdev, int *mtup)
495 const struct netdev_class *class = netdev_get_dev(netdev)->netdev_class;
498 error = class->get_mtu ? class->get_mtu(netdev, mtup) : EOPNOTSUPP;
501 if (error != EOPNOTSUPP) {
502 VLOG_DBG_RL(&rl, "failed to retrieve MTU for network device %s: "
503 "%s", netdev_get_name(netdev), strerror(error));
509 /* Sets the MTU of 'netdev'. The MTU is the maximum size of transmitted
510 * (and received) packets, in bytes.
512 * If successful, returns 0. Returns EOPNOTSUPP if 'netdev' does not have an
513 * MTU (as e.g. some tunnels do not). On other failure, returns a positive
516 netdev_set_mtu(const struct netdev *netdev, int mtu)
518 const struct netdev_class *class = netdev_get_dev(netdev)->netdev_class;
521 error = class->set_mtu ? class->set_mtu(netdev, mtu) : EOPNOTSUPP;
522 if (error && error != EOPNOTSUPP) {
523 VLOG_DBG_RL(&rl, "failed to set MTU for network device %s: %s",
524 netdev_get_name(netdev), strerror(error));
530 /* Returns the ifindex of 'netdev', if successful, as a positive number. On
531 * failure, returns a negative errno value.
533 * The desired semantics of the ifindex value are a combination of those
534 * specified by POSIX for if_nametoindex() and by SNMP for ifIndex. An ifindex
535 * value should be unique within a host and remain stable at least until
536 * reboot. SNMP says an ifindex "ranges between 1 and the value of ifNumber"
537 * but many systems do not follow this rule anyhow.
539 * Some network devices may not implement support for this function. In such
540 * cases this function will always return -EOPNOTSUPP.
543 netdev_get_ifindex(const struct netdev *netdev)
545 int (*get_ifindex)(const struct netdev *);
547 get_ifindex = netdev_get_dev(netdev)->netdev_class->get_ifindex;
549 return get_ifindex ? get_ifindex(netdev) : -EOPNOTSUPP;
552 /* Stores the features supported by 'netdev' into each of '*current',
553 * '*advertised', '*supported', and '*peer' that are non-null. Each value is a
554 * bitmap of "enum ofp_port_features" bits, in host byte order. Returns 0 if
555 * successful, otherwise a positive errno value. On failure, all of the
556 * passed-in values are set to 0.
558 * Some network devices may not implement support for this function. In such
559 * cases this function will always return EOPNOTSUPP. */
561 netdev_get_features(const struct netdev *netdev,
562 enum netdev_features *current,
563 enum netdev_features *advertised,
564 enum netdev_features *supported,
565 enum netdev_features *peer)
567 int (*get_features)(const struct netdev *netdev,
568 enum netdev_features *current,
569 enum netdev_features *advertised,
570 enum netdev_features *supported,
571 enum netdev_features *peer);
572 enum netdev_features dummy[4];
579 advertised = &dummy[1];
582 supported = &dummy[2];
588 get_features = netdev_get_dev(netdev)->netdev_class->get_features;
590 ? get_features(netdev, current, advertised, supported,
594 *current = *advertised = *supported = *peer = 0;
599 /* Returns the maximum speed of a network connection that has the NETDEV_F_*
600 * bits in 'features', in bits per second. If no bits that indicate a speed
601 * are set in 'features', returns 'default_bps'. */
603 netdev_features_to_bps(enum netdev_features features,
604 uint64_t default_bps)
607 F_1000000MB = NETDEV_F_1TB_FD,
608 F_100000MB = NETDEV_F_100GB_FD,
609 F_40000MB = NETDEV_F_40GB_FD,
610 F_10000MB = NETDEV_F_10GB_FD,
611 F_1000MB = NETDEV_F_1GB_HD | NETDEV_F_1GB_FD,
612 F_100MB = NETDEV_F_100MB_HD | NETDEV_F_100MB_FD,
613 F_10MB = NETDEV_F_10MB_HD | NETDEV_F_10MB_FD
616 return ( features & F_1000000MB ? UINT64_C(1000000000000)
617 : features & F_100000MB ? UINT64_C(100000000000)
618 : features & F_40000MB ? UINT64_C(40000000000)
619 : features & F_10000MB ? UINT64_C(10000000000)
620 : features & F_1000MB ? UINT64_C(1000000000)
621 : features & F_100MB ? UINT64_C(100000000)
622 : features & F_10MB ? UINT64_C(10000000)
626 /* Returns true if any of the NETDEV_F_* bits that indicate a full-duplex link
627 * are set in 'features', otherwise false. */
629 netdev_features_is_full_duplex(enum netdev_features features)
631 return (features & (NETDEV_F_10MB_FD | NETDEV_F_100MB_FD | NETDEV_F_1GB_FD
632 | NETDEV_F_10GB_FD | NETDEV_F_40GB_FD
633 | NETDEV_F_100GB_FD | NETDEV_F_1TB_FD)) != 0;
636 /* Set the features advertised by 'netdev' to 'advertise'. Returns 0 if
637 * successful, otherwise a positive errno value. */
639 netdev_set_advertisements(struct netdev *netdev,
640 enum netdev_features advertise)
642 return (netdev_get_dev(netdev)->netdev_class->set_advertisements
643 ? netdev_get_dev(netdev)->netdev_class->set_advertisements(
648 /* If 'netdev' has an assigned IPv4 address, sets '*address' to that address
649 * and '*netmask' to its netmask and returns 0. Otherwise, returns a positive
650 * errno value and sets '*address' to 0 (INADDR_ANY).
652 * The following error values have well-defined meanings:
654 * - EADDRNOTAVAIL: 'netdev' has no assigned IPv4 address.
656 * - EOPNOTSUPP: No IPv4 network stack attached to 'netdev'.
658 * 'address' or 'netmask' or both may be null, in which case the address or
659 * netmask is not reported. */
661 netdev_get_in4(const struct netdev *netdev,
662 struct in_addr *address_, struct in_addr *netmask_)
664 struct in_addr address;
665 struct in_addr netmask;
668 error = (netdev_get_dev(netdev)->netdev_class->get_in4
669 ? netdev_get_dev(netdev)->netdev_class->get_in4(netdev,
673 address_->s_addr = error ? 0 : address.s_addr;
676 netmask_->s_addr = error ? 0 : netmask.s_addr;
681 /* Assigns 'addr' as 'netdev''s IPv4 address and 'mask' as its netmask. If
682 * 'addr' is INADDR_ANY, 'netdev''s IPv4 address is cleared. Returns a
683 * positive errno value. */
685 netdev_set_in4(struct netdev *netdev, struct in_addr addr, struct in_addr mask)
687 return (netdev_get_dev(netdev)->netdev_class->set_in4
688 ? netdev_get_dev(netdev)->netdev_class->set_in4(netdev, addr, mask)
692 /* Obtains ad IPv4 address from device name and save the address in
693 * in4. Returns 0 if successful, otherwise a positive errno value.
696 netdev_get_in4_by_name(const char *device_name, struct in_addr *in4)
698 struct netdev *netdev;
701 error = netdev_open(device_name, "system", &netdev);
703 in4->s_addr = htonl(0);
707 error = netdev_get_in4(netdev, in4, NULL);
708 netdev_close(netdev);
712 /* Adds 'router' as a default IP gateway for the TCP/IP stack that corresponds
715 netdev_add_router(struct netdev *netdev, struct in_addr router)
717 COVERAGE_INC(netdev_add_router);
718 return (netdev_get_dev(netdev)->netdev_class->add_router
719 ? netdev_get_dev(netdev)->netdev_class->add_router(netdev, router)
723 /* Looks up the next hop for 'host' for the TCP/IP stack that corresponds to
724 * 'netdev'. If a route cannot not be determined, sets '*next_hop' to 0,
725 * '*netdev_name' to null, and returns a positive errno value. Otherwise, if a
726 * next hop is found, stores the next hop gateway's address (0 if 'host' is on
727 * a directly connected network) in '*next_hop' and a copy of the name of the
728 * device to reach 'host' in '*netdev_name', and returns 0. The caller is
729 * responsible for freeing '*netdev_name' (by calling free()). */
731 netdev_get_next_hop(const struct netdev *netdev,
732 const struct in_addr *host, struct in_addr *next_hop,
735 int error = (netdev_get_dev(netdev)->netdev_class->get_next_hop
736 ? netdev_get_dev(netdev)->netdev_class->get_next_hop(
737 host, next_hop, netdev_name)
740 next_hop->s_addr = 0;
746 /* Populates 'smap' with status information.
748 * Populates 'smap' with 'netdev' specific status information. This
749 * information may be used to populate the status column of the Interface table
750 * as defined in ovs-vswitchd.conf.db(5). */
752 netdev_get_status(const struct netdev *netdev, struct smap *smap)
754 struct netdev_dev *dev = netdev_get_dev(netdev);
756 return (dev->netdev_class->get_status
757 ? dev->netdev_class->get_status(netdev, smap)
761 /* If 'netdev' has an assigned IPv6 address, sets '*in6' to that address and
762 * returns 0. Otherwise, returns a positive errno value and sets '*in6' to
763 * all-zero-bits (in6addr_any).
765 * The following error values have well-defined meanings:
767 * - EADDRNOTAVAIL: 'netdev' has no assigned IPv6 address.
769 * - EOPNOTSUPP: No IPv6 network stack attached to 'netdev'.
771 * 'in6' may be null, in which case the address itself is not reported. */
773 netdev_get_in6(const struct netdev *netdev, struct in6_addr *in6)
775 struct in6_addr dummy;
778 error = (netdev_get_dev(netdev)->netdev_class->get_in6
779 ? netdev_get_dev(netdev)->netdev_class->get_in6(netdev,
783 memset(in6, 0, sizeof *in6);
788 /* On 'netdev', turns off the flags in 'off' and then turns on the flags in
789 * 'on'. Returns 0 if successful, otherwise a positive errno value. */
791 do_update_flags(struct netdev *netdev, enum netdev_flags off,
792 enum netdev_flags on, enum netdev_flags *old_flagsp,
793 struct netdev_saved_flags **sfp)
795 struct netdev_dev *dev = netdev_get_dev(netdev);
796 struct netdev_saved_flags *sf = NULL;
797 enum netdev_flags old_flags;
800 error = dev->netdev_class->update_flags(dev, off & ~on, on, &old_flags);
802 VLOG_WARN_RL(&rl, "failed to %s flags for network device %s: %s",
803 off || on ? "set" : "get", netdev_get_name(netdev),
806 } else if ((off || on) && sfp) {
807 enum netdev_flags new_flags = (old_flags & ~off) | on;
808 enum netdev_flags changed_flags = old_flags ^ new_flags;
810 *sfp = sf = xmalloc(sizeof *sf);
812 list_push_front(&dev->saved_flags_list, &sf->node);
813 sf->saved_flags = changed_flags;
814 sf->saved_values = changed_flags & new_flags;
821 *old_flagsp = old_flags;
830 /* Obtains the current flags for 'netdev' and stores them into '*flagsp'.
831 * Returns 0 if successful, otherwise a positive errno value. On failure,
832 * stores 0 into '*flagsp'. */
834 netdev_get_flags(const struct netdev *netdev_, enum netdev_flags *flagsp)
836 struct netdev *netdev = CONST_CAST(struct netdev *, netdev_);
837 return do_update_flags(netdev, 0, 0, flagsp, NULL);
840 /* Sets the flags for 'netdev' to 'flags'.
841 * Returns 0 if successful, otherwise a positive errno value. */
843 netdev_set_flags(struct netdev *netdev, enum netdev_flags flags,
844 struct netdev_saved_flags **sfp)
846 return do_update_flags(netdev, -1, flags, NULL, sfp);
849 /* Turns on the specified 'flags' on 'netdev':
851 * - On success, returns 0. If 'sfp' is nonnull, sets '*sfp' to a newly
852 * allocated 'struct netdev_saved_flags *' that may be passed to
853 * netdev_restore_flags() to restore the original values of 'flags' on
854 * 'netdev' (this will happen automatically at program termination if
855 * netdev_restore_flags() is never called) , or to NULL if no flags were
858 * - On failure, returns a positive errno value. If 'sfp' is nonnull, sets
861 netdev_turn_flags_on(struct netdev *netdev, enum netdev_flags flags,
862 struct netdev_saved_flags **sfp)
864 return do_update_flags(netdev, 0, flags, NULL, sfp);
867 /* Turns off the specified 'flags' on 'netdev'. See netdev_turn_flags_on() for
868 * details of the interface. */
870 netdev_turn_flags_off(struct netdev *netdev, enum netdev_flags flags,
871 struct netdev_saved_flags **sfp)
873 return do_update_flags(netdev, flags, 0, NULL, sfp);
876 /* Restores the flags that were saved in 'sf', and destroys 'sf'.
877 * Does nothing if 'sf' is NULL. */
879 netdev_restore_flags(struct netdev_saved_flags *sf)
882 struct netdev_dev *dev = sf->dev;
883 enum netdev_flags old_flags;
885 dev->netdev_class->update_flags(dev,
886 sf->saved_flags & sf->saved_values,
887 sf->saved_flags & ~sf->saved_values,
889 list_remove(&sf->node);
892 netdev_dev_unref(dev);
896 /* Looks up the ARP table entry for 'ip' on 'netdev'. If one exists and can be
897 * successfully retrieved, it stores the corresponding MAC address in 'mac' and
898 * returns 0. Otherwise, it returns a positive errno value; in particular,
899 * ENXIO indicates that there is no ARP table entry for 'ip' on 'netdev'. */
901 netdev_arp_lookup(const struct netdev *netdev,
902 ovs_be32 ip, uint8_t mac[ETH_ADDR_LEN])
904 int error = (netdev_get_dev(netdev)->netdev_class->arp_lookup
905 ? netdev_get_dev(netdev)->netdev_class->arp_lookup(netdev,
909 memset(mac, 0, ETH_ADDR_LEN);
914 /* Returns true if carrier is active (link light is on) on 'netdev'. */
916 netdev_get_carrier(const struct netdev *netdev)
919 enum netdev_flags flags;
922 netdev_get_flags(netdev, &flags);
923 if (!(flags & NETDEV_UP)) {
927 if (!netdev_get_dev(netdev)->netdev_class->get_carrier) {
931 error = netdev_get_dev(netdev)->netdev_class->get_carrier(netdev,
934 VLOG_DBG("%s: failed to get network device carrier status, assuming "
935 "down: %s", netdev_get_name(netdev), strerror(error));
942 /* Returns the number of times 'netdev''s carrier has changed. */
944 netdev_get_carrier_resets(const struct netdev *netdev)
946 return (netdev_get_dev(netdev)->netdev_class->get_carrier_resets
947 ? netdev_get_dev(netdev)->netdev_class->get_carrier_resets(netdev)
951 /* Attempts to force netdev_get_carrier() to poll 'netdev''s MII registers for
952 * link status instead of checking 'netdev''s carrier. 'netdev''s MII
953 * registers will be polled once ever 'interval' milliseconds. If 'netdev'
954 * does not support MII, another method may be used as a fallback. If
955 * 'interval' is less than or equal to zero, reverts netdev_get_carrier() to
956 * its normal behavior.
958 * Returns 0 if successful, otherwise a positive errno value. */
960 netdev_set_miimon_interval(struct netdev *netdev, long long int interval)
962 struct netdev_dev *netdev_dev = netdev_get_dev(netdev);
963 return (netdev_dev->netdev_class->set_miimon_interval
964 ? netdev_dev->netdev_class->set_miimon_interval(netdev, interval)
968 /* Retrieves current device stats for 'netdev'. */
970 netdev_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
974 COVERAGE_INC(netdev_get_stats);
975 error = (netdev_get_dev(netdev)->netdev_class->get_stats
976 ? netdev_get_dev(netdev)->netdev_class->get_stats(netdev, stats)
979 memset(stats, 0xff, sizeof *stats);
984 /* Attempts to change the stats for 'netdev' to those provided in 'stats'.
985 * Returns 0 if successful, otherwise a positive errno value.
987 * This will probably fail for most network devices. Some devices might only
988 * allow setting their stats to 0. */
990 netdev_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
992 return (netdev_get_dev(netdev)->netdev_class->set_stats
993 ? netdev_get_dev(netdev)->netdev_class->set_stats(netdev, stats)
997 /* Attempts to set input rate limiting (policing) policy, such that up to
998 * 'kbits_rate' kbps of traffic is accepted, with a maximum accumulative burst
999 * size of 'kbits' kb. */
1001 netdev_set_policing(struct netdev *netdev, uint32_t kbits_rate,
1002 uint32_t kbits_burst)
1004 return (netdev_get_dev(netdev)->netdev_class->set_policing
1005 ? netdev_get_dev(netdev)->netdev_class->set_policing(netdev,
1006 kbits_rate, kbits_burst)
1010 /* Adds to 'types' all of the forms of QoS supported by 'netdev', or leaves it
1011 * empty if 'netdev' does not support QoS. Any names added to 'types' should
1012 * be documented as valid for the "type" column in the "QoS" table in
1013 * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
1015 * Every network device supports disabling QoS with a type of "", but this type
1016 * will not be added to 'types'.
1018 * The caller must initialize 'types' (e.g. with sset_init()) before calling
1019 * this function. The caller is responsible for destroying 'types' (e.g. with
1020 * sset_destroy()) when it is no longer needed.
1022 * Returns 0 if successful, otherwise a positive errno value. */
1024 netdev_get_qos_types(const struct netdev *netdev, struct sset *types)
1026 const struct netdev_class *class = netdev_get_dev(netdev)->netdev_class;
1027 return (class->get_qos_types
1028 ? class->get_qos_types(netdev, types)
1032 /* Queries 'netdev' for its capabilities regarding the specified 'type' of QoS,
1033 * which should be "" or one of the types returned by netdev_get_qos_types()
1034 * for 'netdev'. Returns 0 if successful, otherwise a positive errno value.
1035 * On success, initializes 'caps' with the QoS capabilities; on failure, clears
1036 * 'caps' to all zeros. */
1038 netdev_get_qos_capabilities(const struct netdev *netdev, const char *type,
1039 struct netdev_qos_capabilities *caps)
1041 const struct netdev_class *class = netdev_get_dev(netdev)->netdev_class;
1044 int retval = (class->get_qos_capabilities
1045 ? class->get_qos_capabilities(netdev, type, caps)
1048 memset(caps, 0, sizeof *caps);
1052 /* Every netdev supports turning off QoS. */
1053 memset(caps, 0, sizeof *caps);
1058 /* Obtains the number of queues supported by 'netdev' for the specified 'type'
1059 * of QoS. Returns 0 if successful, otherwise a positive errno value. Stores
1060 * the number of queues (zero on failure) in '*n_queuesp'.
1062 * This is just a simple wrapper around netdev_get_qos_capabilities(). */
1064 netdev_get_n_queues(const struct netdev *netdev,
1065 const char *type, unsigned int *n_queuesp)
1067 struct netdev_qos_capabilities caps;
1070 retval = netdev_get_qos_capabilities(netdev, type, &caps);
1071 *n_queuesp = caps.n_queues;
1075 /* Queries 'netdev' about its currently configured form of QoS. If successful,
1076 * stores the name of the current form of QoS into '*typep', stores any details
1077 * of configuration as string key-value pairs in 'details', and returns 0. On
1078 * failure, sets '*typep' to NULL and returns a positive errno value.
1080 * A '*typep' of "" indicates that QoS is currently disabled on 'netdev'.
1082 * The caller must initialize 'details' as an empty smap (e.g. with
1083 * smap_init()) before calling this function. The caller must free 'details'
1084 * when it is no longer needed (e.g. with smap_destroy()).
1086 * The caller must not modify or free '*typep'.
1088 * '*typep' will be one of the types returned by netdev_get_qos_types() for
1089 * 'netdev'. The contents of 'details' should be documented as valid for
1090 * '*typep' in the "other_config" column in the "QoS" table in
1091 * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)). */
1093 netdev_get_qos(const struct netdev *netdev,
1094 const char **typep, struct smap *details)
1096 const struct netdev_class *class = netdev_get_dev(netdev)->netdev_class;
1099 if (class->get_qos) {
1100 retval = class->get_qos(netdev, typep, details);
1103 smap_clear(details);
1107 /* 'netdev' doesn't support QoS, so report that QoS is disabled. */
1113 /* Attempts to reconfigure QoS on 'netdev', changing the form of QoS to 'type'
1114 * with details of configuration from 'details'. Returns 0 if successful,
1115 * otherwise a positive errno value. On error, the previous QoS configuration
1118 * When this function changes the type of QoS (not just 'details'), this also
1119 * resets all queue configuration for 'netdev' to their defaults (which depend
1120 * on the specific type of QoS). Otherwise, the queue configuration for
1121 * 'netdev' is unchanged.
1123 * 'type' should be "" (to disable QoS) or one of the types returned by
1124 * netdev_get_qos_types() for 'netdev'. The contents of 'details' should be
1125 * documented as valid for the given 'type' in the "other_config" column in the
1126 * "QoS" table in vswitchd/vswitch.xml (which is built as
1127 * ovs-vswitchd.conf.db(8)).
1129 * NULL may be specified for 'details' if there are no configuration
1132 netdev_set_qos(struct netdev *netdev,
1133 const char *type, const struct smap *details)
1135 const struct netdev_class *class = netdev_get_dev(netdev)->netdev_class;
1141 if (class->set_qos) {
1143 static const struct smap empty = SMAP_INITIALIZER(&empty);
1146 return class->set_qos(netdev, type, details);
1148 return *type ? EOPNOTSUPP : 0;
1152 /* Queries 'netdev' for information about the queue numbered 'queue_id'. If
1153 * successful, adds that information as string key-value pairs to 'details'.
1154 * Returns 0 if successful, otherwise a positive errno value.
1156 * 'queue_id' must be less than the number of queues supported by 'netdev' for
1157 * the current form of QoS (e.g. as returned by netdev_get_n_queues(netdev)).
1159 * The returned contents of 'details' should be documented as valid for the
1160 * given 'type' in the "other_config" column in the "Queue" table in
1161 * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
1163 * The caller must initialize 'details' (e.g. with smap_init()) before calling
1164 * this function. The caller must free 'details' when it is no longer needed
1165 * (e.g. with smap_destroy()). */
1167 netdev_get_queue(const struct netdev *netdev,
1168 unsigned int queue_id, struct smap *details)
1170 const struct netdev_class *class = netdev_get_dev(netdev)->netdev_class;
1173 retval = (class->get_queue
1174 ? class->get_queue(netdev, queue_id, details)
1177 smap_clear(details);
1182 /* Configures the queue numbered 'queue_id' on 'netdev' with the key-value
1183 * string pairs in 'details'. The contents of 'details' should be documented
1184 * as valid for the given 'type' in the "other_config" column in the "Queue"
1185 * table in vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
1186 * Returns 0 if successful, otherwise a positive errno value. On failure, the
1187 * given queue's configuration should be unmodified.
1189 * 'queue_id' must be less than the number of queues supported by 'netdev' for
1190 * the current form of QoS (e.g. as returned by netdev_get_n_queues(netdev)).
1192 * This function does not modify 'details', and the caller retains ownership of
1195 netdev_set_queue(struct netdev *netdev,
1196 unsigned int queue_id, const struct smap *details)
1198 const struct netdev_class *class = netdev_get_dev(netdev)->netdev_class;
1199 return (class->set_queue
1200 ? class->set_queue(netdev, queue_id, details)
1204 /* Attempts to delete the queue numbered 'queue_id' from 'netdev'. Some kinds
1205 * of QoS may have a fixed set of queues, in which case attempts to delete them
1206 * will fail with EOPNOTSUPP.
1208 * Returns 0 if successful, otherwise a positive errno value. On failure, the
1209 * given queue will be unmodified.
1211 * 'queue_id' must be less than the number of queues supported by 'netdev' for
1212 * the current form of QoS (e.g. as returned by
1213 * netdev_get_n_queues(netdev)). */
1215 netdev_delete_queue(struct netdev *netdev, unsigned int queue_id)
1217 const struct netdev_class *class = netdev_get_dev(netdev)->netdev_class;
1218 return (class->delete_queue
1219 ? class->delete_queue(netdev, queue_id)
1223 /* Obtains statistics about 'queue_id' on 'netdev'. On success, returns 0 and
1224 * fills 'stats' with the queue's statistics; individual members of 'stats' may
1225 * be set to all-1-bits if the statistic is unavailable. On failure, returns a
1226 * positive errno value and fills 'stats' with all-1-bits. */
1228 netdev_get_queue_stats(const struct netdev *netdev, unsigned int queue_id,
1229 struct netdev_queue_stats *stats)
1231 const struct netdev_class *class = netdev_get_dev(netdev)->netdev_class;
1234 retval = (class->get_queue_stats
1235 ? class->get_queue_stats(netdev, queue_id, stats)
1238 memset(stats, 0xff, sizeof *stats);
1243 /* Iterates over all of 'netdev''s queues, calling 'cb' with the queue's ID,
1244 * its configuration, and the 'aux' specified by the caller. The order of
1245 * iteration is unspecified, but (when successful) each queue is visited
1248 * Calling this function may be more efficient than calling netdev_get_queue()
1251 * 'cb' must not modify or free the 'details' argument passed in. It may
1252 * delete or modify the queue passed in as its 'queue_id' argument. It may
1253 * modify but must not delete any other queue within 'netdev'. 'cb' should not
1254 * add new queues because this may cause some queues to be visited twice or not
1257 * Returns 0 if successful, otherwise a positive errno value. On error, some
1258 * configured queues may not have been included in the iteration. */
1260 netdev_dump_queues(const struct netdev *netdev,
1261 netdev_dump_queues_cb *cb, void *aux)
1263 const struct netdev_class *class = netdev_get_dev(netdev)->netdev_class;
1264 return (class->dump_queues
1265 ? class->dump_queues(netdev, cb, aux)
1269 /* Iterates over all of 'netdev''s queues, calling 'cb' with the queue's ID,
1270 * its statistics, and the 'aux' specified by the caller. The order of
1271 * iteration is unspecified, but (when successful) each queue is visited
1274 * Calling this function may be more efficient than calling
1275 * netdev_get_queue_stats() for every queue.
1277 * 'cb' must not modify or free the statistics passed in.
1279 * Returns 0 if successful, otherwise a positive errno value. On error, some
1280 * configured queues may not have been included in the iteration. */
1282 netdev_dump_queue_stats(const struct netdev *netdev,
1283 netdev_dump_queue_stats_cb *cb, void *aux)
1285 const struct netdev_class *class = netdev_get_dev(netdev)->netdev_class;
1286 return (class->dump_queue_stats
1287 ? class->dump_queue_stats(netdev, cb, aux)
1291 /* Returns a sequence number which indicates changes in one of 'netdev''s
1292 * properties. The returned sequence will be nonzero so that callers have a
1293 * value which they may use as a reset when tracking 'netdev'.
1295 * The returned sequence number will change whenever 'netdev''s flags,
1296 * features, ethernet address, or carrier changes. It may change for other
1297 * reasons as well, or no reason at all. */
1299 netdev_change_seq(const struct netdev *netdev)
1301 return netdev_get_dev(netdev)->netdev_class->change_seq(netdev);
1304 /* Initializes 'netdev_dev' as a netdev device named 'name' of the specified
1305 * 'netdev_class'. This function is ordinarily called from a netdev provider's
1306 * 'create' function.
1308 * This function adds 'netdev_dev' to a netdev-owned shash, so it is
1309 * very important that 'netdev_dev' only be freed after calling
1310 * the refcount drops to zero. */
1312 netdev_dev_init(struct netdev_dev *netdev_dev, const char *name,
1313 const struct netdev_class *netdev_class)
1315 ovs_assert(!shash_find(&netdev_dev_shash, name));
1317 memset(netdev_dev, 0, sizeof *netdev_dev);
1318 netdev_dev->netdev_class = netdev_class;
1319 netdev_dev->name = xstrdup(name);
1320 netdev_dev->node = shash_add(&netdev_dev_shash, name, netdev_dev);
1321 list_init(&netdev_dev->saved_flags_list);
1324 /* Undoes the results of initialization.
1326 * Normally this function does not need to be called as netdev_close has
1327 * the same effect when the refcount drops to zero.
1328 * However, it may be called by providers due to an error on creation
1329 * that occurs after initialization. It this case netdev_close() would
1330 * never be called. */
1332 netdev_dev_uninit(struct netdev_dev *netdev_dev, bool destroy)
1334 char *name = netdev_dev->name;
1336 ovs_assert(!netdev_dev->ref_cnt);
1337 ovs_assert(list_is_empty(&netdev_dev->saved_flags_list));
1339 shash_delete(&netdev_dev_shash, netdev_dev->node);
1342 netdev_dev->netdev_class->destroy(netdev_dev);
1347 /* Returns the class type of 'netdev_dev'.
1349 * The caller must not free the returned value. */
1351 netdev_dev_get_type(const struct netdev_dev *netdev_dev)
1353 return netdev_dev->netdev_class->type;
1356 /* Returns the class associated with 'netdev_dev'. */
1357 const struct netdev_class *
1358 netdev_dev_get_class(const struct netdev_dev *netdev_dev)
1360 return netdev_dev->netdev_class;
1363 /* Returns the name of 'netdev_dev'.
1365 * The caller must not free the returned value. */
1367 netdev_dev_get_name(const struct netdev_dev *netdev_dev)
1369 return netdev_dev->name;
1372 /* Returns the netdev_dev with 'name' or NULL if there is none.
1374 * The caller must not free the returned value. */
1376 netdev_dev_from_name(const char *name)
1378 return shash_find_data(&netdev_dev_shash, name);
1381 /* Fills 'device_list' with devices that match 'netdev_class'.
1383 * The caller is responsible for initializing and destroying 'device_list'
1384 * but the contained netdev_devs must not be freed. */
1386 netdev_dev_get_devices(const struct netdev_class *netdev_class,
1387 struct shash *device_list)
1389 struct shash_node *node;
1390 SHASH_FOR_EACH (node, &netdev_dev_shash) {
1391 struct netdev_dev *dev = node->data;
1393 if (dev->netdev_class == netdev_class) {
1394 shash_add(device_list, node->name, node->data);
1399 /* Initializes 'netdev' as a instance of the netdev_dev.
1401 * This function adds 'netdev' to a netdev-owned linked list, so it is very
1402 * important that 'netdev' only be freed after calling netdev_close(). */
1404 netdev_init(struct netdev *netdev, struct netdev_dev *netdev_dev)
1406 memset(netdev, 0, sizeof *netdev);
1407 netdev->netdev_dev = netdev_dev;
1408 list_push_back(&netdev_list, &netdev->node);
1411 /* Undoes the results of initialization.
1413 * Normally this function only needs to be called from netdev_close().
1414 * However, it may be called by providers due to an error on opening
1415 * that occurs after initialization. It this case netdev_close() would
1416 * never be called. */
1418 netdev_uninit(struct netdev *netdev, bool close)
1420 list_remove(&netdev->node);
1422 netdev_get_dev(netdev)->netdev_class->close(netdev);
1426 /* Returns the class type of 'netdev'.
1428 * The caller must not free the returned value. */
1430 netdev_get_type(const struct netdev *netdev)
1432 return netdev_get_dev(netdev)->netdev_class->type;
1436 netdev_get_type_from_name(const char *name)
1438 const struct netdev_dev *dev = netdev_dev_from_name(name);
1439 return dev ? netdev_dev_get_type(dev) : NULL;
1443 netdev_get_dev(const struct netdev *netdev)
1445 return netdev->netdev_dev;
1449 netdev_rx_init(struct netdev_rx *rx, struct netdev_dev *dev,
1450 const struct netdev_rx_class *class)
1452 ovs_assert(dev->ref_cnt > 0);
1453 rx->rx_class = class;
1454 rx->netdev_dev = dev;
1458 netdev_rx_uninit(struct netdev_rx *rx OVS_UNUSED)
1460 /* Nothing to do. */
1464 netdev_rx_get_dev(const struct netdev_rx *rx)
1466 ovs_assert(rx->netdev_dev->ref_cnt > 0);
1467 return rx->netdev_dev;
1471 netdev_rx_get_name(const struct netdev_rx *rx)
1473 return netdev_dev_get_name(netdev_rx_get_dev(rx));
1477 restore_all_flags(void *aux OVS_UNUSED)
1479 struct shash_node *node;
1481 SHASH_FOR_EACH (node, &netdev_dev_shash) {
1482 struct netdev_dev *dev = node->data;
1483 const struct netdev_saved_flags *sf;
1484 enum netdev_flags saved_values;
1485 enum netdev_flags saved_flags;
1487 saved_values = saved_flags = 0;
1488 LIST_FOR_EACH (sf, node, &dev->saved_flags_list) {
1489 saved_flags |= sf->saved_flags;
1490 saved_values &= ~sf->saved_flags;
1491 saved_values |= sf->saved_flags & sf->saved_values;
1494 enum netdev_flags old_flags;
1496 dev->netdev_class->update_flags(dev,
1497 saved_flags & saved_values,
1498 saved_flags & ~saved_values,