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);
97 /* Performs periodic work needed by all the various kinds of netdevs.
99 * If your program opens any netdevs, it must call this function within its
104 struct shash_node *node;
105 SHASH_FOR_EACH(node, &netdev_classes) {
106 const struct netdev_class *netdev_class = node->data;
107 if (netdev_class->run) {
113 /* Arranges for poll_block() to wake up when netdev_run() needs to be called.
115 * If your program opens any netdevs, it must call this function within its
120 struct shash_node *node;
121 SHASH_FOR_EACH(node, &netdev_classes) {
122 const struct netdev_class *netdev_class = node->data;
123 if (netdev_class->wait) {
124 netdev_class->wait();
129 /* Initializes and registers a new netdev provider. After successful
130 * registration, new netdevs of that type can be opened using netdev_open(). */
132 netdev_register_provider(const struct netdev_class *new_class)
134 if (shash_find(&netdev_classes, new_class->type)) {
135 VLOG_WARN("attempted to register duplicate netdev provider: %s",
140 if (new_class->init) {
141 int error = new_class->init();
143 VLOG_ERR("failed to initialize %s network device class: %s",
144 new_class->type, strerror(error));
149 shash_add(&netdev_classes, new_class->type, new_class);
154 /* Unregisters a netdev provider. 'type' must have been previously
155 * registered and not currently be in use by any netdevs. After unregistration
156 * new netdevs of that type cannot be opened using netdev_open(). */
158 netdev_unregister_provider(const char *type)
160 struct shash_node *del_node, *netdev_dev_node;
162 del_node = shash_find(&netdev_classes, type);
164 VLOG_WARN("attempted to unregister a netdev provider that is not "
165 "registered: %s", type);
169 SHASH_FOR_EACH(netdev_dev_node, &netdev_dev_shash) {
170 struct netdev_dev *netdev_dev = netdev_dev_node->data;
171 if (!strcmp(netdev_dev->netdev_class->type, type)) {
172 VLOG_WARN("attempted to unregister in use netdev provider: %s",
178 shash_delete(&netdev_classes, del_node);
183 const struct netdev_class *
184 netdev_lookup_provider(const char *type)
187 return shash_find_data(&netdev_classes, type && type[0] ? type : "system");
190 /* Clears 'types' and enumerates the types of all currently registered netdev
191 * providers into it. The caller must first initialize the sset. */
193 netdev_enumerate_types(struct sset *types)
195 struct shash_node *node;
200 SHASH_FOR_EACH(node, &netdev_classes) {
201 const struct netdev_class *netdev_class = node->data;
202 sset_add(types, netdev_class->type);
206 /* Opens the network device named 'name' (e.g. "eth0") of the specified 'type'
207 * (e.g. "system") and returns zero if successful, otherwise a positive errno
208 * value. On success, sets '*netdevp' to the new network device, otherwise to
211 * Some network devices may need to be configured (with netdev_set_config())
212 * before they can be used. */
214 netdev_open(const char *name, const char *type, struct netdev **netdevp)
216 struct netdev_dev *netdev_dev;
222 netdev_dev = shash_find_data(&netdev_dev_shash, name);
225 const struct netdev_class *class;
227 class = netdev_lookup_provider(type);
229 VLOG_WARN("could not create netdev %s of unknown type %s",
233 error = class->create(class, name, &netdev_dev);
237 ovs_assert(netdev_dev->netdev_class == class);
241 error = netdev_dev->netdev_class->open(netdev_dev, netdevp);
244 netdev_dev->ref_cnt++;
246 if (!netdev_dev->ref_cnt) {
247 netdev_dev_uninit(netdev_dev, true);
254 /* Reconfigures the device 'netdev' with 'args'. 'args' may be empty
255 * or NULL if none are needed. */
257 netdev_set_config(struct netdev *netdev, const struct smap *args)
259 struct netdev_dev *netdev_dev = netdev_get_dev(netdev);
261 if (netdev_dev->netdev_class->set_config) {
262 struct smap no_args = SMAP_INITIALIZER(&no_args);
263 return netdev_dev->netdev_class->set_config(netdev_dev,
264 args ? args : &no_args);
265 } else if (args && !smap_is_empty(args)) {
266 VLOG_WARN("%s: arguments provided to device that is not configurable",
267 netdev_get_name(netdev));
273 /* Returns the current configuration for 'netdev' in 'args'. The caller must
274 * have already initialized 'args' with smap_init(). Returns 0 on success, in
275 * which case 'args' will be filled with 'netdev''s configuration. On failure
276 * returns a positive errno value, in which case 'args' will be empty.
278 * The caller owns 'args' and its contents and must eventually free them with
281 netdev_get_config(const struct netdev *netdev, struct smap *args)
283 struct netdev_dev *netdev_dev = netdev_get_dev(netdev);
287 if (netdev_dev->netdev_class->get_config) {
288 error = netdev_dev->netdev_class->get_config(netdev_dev, args);
299 const struct netdev_tunnel_config *
300 netdev_get_tunnel_config(const struct netdev *netdev)
302 struct netdev_dev *netdev_dev = netdev_get_dev(netdev);
304 if (netdev_dev->netdev_class->get_tunnel_config) {
305 return netdev_dev->netdev_class->get_tunnel_config(netdev_dev);
312 netdev_dev_unref(struct netdev_dev *dev)
314 ovs_assert(dev->ref_cnt);
315 if (!--dev->ref_cnt) {
316 netdev_dev_uninit(dev, true);
320 /* Closes and destroys 'netdev'. */
322 netdev_close(struct netdev *netdev)
325 struct netdev_dev *dev = netdev_get_dev(netdev);
327 netdev_uninit(netdev, true);
328 netdev_dev_unref(dev);
332 /* Parses 'netdev_name_', which is of the form [type@]name into its component
333 * pieces. 'name' and 'type' must be freed by the caller. */
335 netdev_parse_name(const char *netdev_name_, char **name, char **type)
337 char *netdev_name = xstrdup(netdev_name_);
340 separator = strchr(netdev_name, '@');
344 *name = xstrdup(separator + 1);
347 *type = xstrdup("system");
352 netdev_rx_open(struct netdev *netdev, struct netdev_rx **rxp)
354 struct netdev_dev *dev = netdev_get_dev(netdev);
357 error = (dev->netdev_class->rx_open
358 ? dev->netdev_class->rx_open(netdev, rxp)
361 ovs_assert((*rxp)->netdev_dev == dev);
370 netdev_rx_close(struct netdev_rx *rx)
373 struct netdev_dev *dev = rx->netdev_dev;
375 rx->rx_class->destroy(rx);
376 netdev_dev_unref(dev);
381 netdev_rx_recv(struct netdev_rx *rx, struct ofpbuf *buffer)
385 ovs_assert(buffer->size == 0);
386 ovs_assert(ofpbuf_tailroom(buffer) >= ETH_TOTAL_MIN);
388 retval = rx->rx_class->recv(rx, buffer->data, ofpbuf_tailroom(buffer));
390 COVERAGE_INC(netdev_received);
391 buffer->size += retval;
392 if (buffer->size < ETH_TOTAL_MIN) {
393 ofpbuf_put_zeros(buffer, ETH_TOTAL_MIN - buffer->size);
402 netdev_rx_wait(struct netdev_rx *rx)
404 rx->rx_class->wait(rx);
408 netdev_rx_drain(struct netdev_rx *rx)
410 return rx->rx_class->drain ? rx->rx_class->drain(rx) : 0;
413 /* Sends 'buffer' on 'netdev'. Returns 0 if successful, otherwise a positive
414 * errno value. Returns EAGAIN without blocking if the packet cannot be queued
415 * immediately. Returns EMSGSIZE if a partial packet was transmitted or if
416 * the packet is too big or too small to transmit on the device.
418 * The caller retains ownership of 'buffer' in all cases.
420 * The kernel maintains a packet transmission queue, so the caller is not
421 * expected to do additional queuing of packets.
423 * Some network devices may not implement support for this function. In such
424 * cases this function will always return EOPNOTSUPP. */
426 netdev_send(struct netdev *netdev, const struct ofpbuf *buffer)
428 int (*send)(struct netdev *, const void *, size_t);
431 send = netdev_get_dev(netdev)->netdev_class->send;
432 error = send ? (send)(netdev, buffer->data, buffer->size) : EOPNOTSUPP;
434 COVERAGE_INC(netdev_sent);
439 /* Registers with the poll loop to wake up from the next call to poll_block()
440 * when the packet transmission queue has sufficient room to transmit a packet
441 * with netdev_send().
443 * The kernel maintains a packet transmission queue, so the client is not
444 * expected to do additional queuing of packets. Thus, this function is
445 * unlikely to ever be used. It is included for completeness. */
447 netdev_send_wait(struct netdev *netdev)
449 void (*send_wait)(struct netdev *);
451 send_wait = netdev_get_dev(netdev)->netdev_class->send_wait;
457 /* Attempts to set 'netdev''s MAC address to 'mac'. Returns 0 if successful,
458 * otherwise a positive errno value. */
460 netdev_set_etheraddr(struct netdev *netdev, const uint8_t mac[ETH_ADDR_LEN])
462 return netdev_get_dev(netdev)->netdev_class->set_etheraddr(netdev, mac);
465 /* Retrieves 'netdev''s MAC address. If successful, returns 0 and copies the
466 * the MAC address into 'mac'. On failure, returns a positive errno value and
467 * clears 'mac' to all-zeros. */
469 netdev_get_etheraddr(const struct netdev *netdev, uint8_t mac[ETH_ADDR_LEN])
471 return netdev_get_dev(netdev)->netdev_class->get_etheraddr(netdev, mac);
474 /* Returns the name of the network device that 'netdev' represents,
475 * e.g. "eth0". The caller must not modify or free the returned string. */
477 netdev_get_name(const struct netdev *netdev)
479 return netdev_get_dev(netdev)->name;
482 /* Retrieves the MTU of 'netdev'. The MTU is the maximum size of transmitted
483 * (and received) packets, in bytes, not including the hardware header; thus,
484 * this is typically 1500 bytes for Ethernet devices.
486 * If successful, returns 0 and stores the MTU size in '*mtup'. Returns
487 * EOPNOTSUPP if 'netdev' does not have an MTU (as e.g. some tunnels do not).
488 * On other failure, returns a positive errno value. On failure, sets '*mtup'
491 netdev_get_mtu(const struct netdev *netdev, int *mtup)
493 const struct netdev_class *class = netdev_get_dev(netdev)->netdev_class;
496 error = class->get_mtu ? class->get_mtu(netdev, mtup) : EOPNOTSUPP;
499 if (error != EOPNOTSUPP) {
500 VLOG_DBG_RL(&rl, "failed to retrieve MTU for network device %s: "
501 "%s", netdev_get_name(netdev), strerror(error));
507 /* Sets the MTU of 'netdev'. The MTU is the maximum size of transmitted
508 * (and received) packets, in bytes.
510 * If successful, returns 0. Returns EOPNOTSUPP if 'netdev' does not have an
511 * MTU (as e.g. some tunnels do not). On other failure, returns a positive
514 netdev_set_mtu(const struct netdev *netdev, int mtu)
516 const struct netdev_class *class = netdev_get_dev(netdev)->netdev_class;
519 error = class->set_mtu ? class->set_mtu(netdev, mtu) : EOPNOTSUPP;
520 if (error && error != EOPNOTSUPP) {
521 VLOG_DBG_RL(&rl, "failed to set MTU for network device %s: %s",
522 netdev_get_name(netdev), strerror(error));
528 /* Returns the ifindex of 'netdev', if successful, as a positive number. On
529 * failure, returns a negative errno value.
531 * The desired semantics of the ifindex value are a combination of those
532 * specified by POSIX for if_nametoindex() and by SNMP for ifIndex. An ifindex
533 * value should be unique within a host and remain stable at least until
534 * reboot. SNMP says an ifindex "ranges between 1 and the value of ifNumber"
535 * but many systems do not follow this rule anyhow.
537 * Some network devices may not implement support for this function. In such
538 * cases this function will always return -EOPNOTSUPP.
541 netdev_get_ifindex(const struct netdev *netdev)
543 int (*get_ifindex)(const struct netdev *);
545 get_ifindex = netdev_get_dev(netdev)->netdev_class->get_ifindex;
547 return get_ifindex ? get_ifindex(netdev) : -EOPNOTSUPP;
550 /* Stores the features supported by 'netdev' into each of '*current',
551 * '*advertised', '*supported', and '*peer' that are non-null. Each value is a
552 * bitmap of "enum ofp_port_features" bits, in host byte order. Returns 0 if
553 * successful, otherwise a positive errno value. On failure, all of the
554 * passed-in values are set to 0.
556 * Some network devices may not implement support for this function. In such
557 * cases this function will always return EOPNOTSUPP. */
559 netdev_get_features(const struct netdev *netdev,
560 enum netdev_features *current,
561 enum netdev_features *advertised,
562 enum netdev_features *supported,
563 enum netdev_features *peer)
565 int (*get_features)(const struct netdev *netdev,
566 enum netdev_features *current,
567 enum netdev_features *advertised,
568 enum netdev_features *supported,
569 enum netdev_features *peer);
570 enum netdev_features dummy[4];
577 advertised = &dummy[1];
580 supported = &dummy[2];
586 get_features = netdev_get_dev(netdev)->netdev_class->get_features;
588 ? get_features(netdev, current, advertised, supported,
592 *current = *advertised = *supported = *peer = 0;
597 /* Returns the maximum speed of a network connection that has the NETDEV_F_*
598 * bits in 'features', in bits per second. If no bits that indicate a speed
599 * are set in 'features', returns 'default_bps'. */
601 netdev_features_to_bps(enum netdev_features features,
602 uint64_t default_bps)
605 F_1000000MB = NETDEV_F_1TB_FD,
606 F_100000MB = NETDEV_F_100GB_FD,
607 F_40000MB = NETDEV_F_40GB_FD,
608 F_10000MB = NETDEV_F_10GB_FD,
609 F_1000MB = NETDEV_F_1GB_HD | NETDEV_F_1GB_FD,
610 F_100MB = NETDEV_F_100MB_HD | NETDEV_F_100MB_FD,
611 F_10MB = NETDEV_F_10MB_HD | NETDEV_F_10MB_FD
614 return ( features & F_1000000MB ? UINT64_C(1000000000000)
615 : features & F_100000MB ? UINT64_C(100000000000)
616 : features & F_40000MB ? UINT64_C(40000000000)
617 : features & F_10000MB ? UINT64_C(10000000000)
618 : features & F_1000MB ? UINT64_C(1000000000)
619 : features & F_100MB ? UINT64_C(100000000)
620 : features & F_10MB ? UINT64_C(10000000)
624 /* Returns true if any of the NETDEV_F_* bits that indicate a full-duplex link
625 * are set in 'features', otherwise false. */
627 netdev_features_is_full_duplex(enum netdev_features features)
629 return (features & (NETDEV_F_10MB_FD | NETDEV_F_100MB_FD | NETDEV_F_1GB_FD
630 | NETDEV_F_10GB_FD | NETDEV_F_40GB_FD
631 | NETDEV_F_100GB_FD | NETDEV_F_1TB_FD)) != 0;
634 /* Set the features advertised by 'netdev' to 'advertise'. Returns 0 if
635 * successful, otherwise a positive errno value. */
637 netdev_set_advertisements(struct netdev *netdev,
638 enum netdev_features advertise)
640 return (netdev_get_dev(netdev)->netdev_class->set_advertisements
641 ? netdev_get_dev(netdev)->netdev_class->set_advertisements(
646 /* If 'netdev' has an assigned IPv4 address, sets '*address' to that address
647 * and '*netmask' to its netmask and returns 0. Otherwise, returns a positive
648 * errno value and sets '*address' to 0 (INADDR_ANY).
650 * The following error values have well-defined meanings:
652 * - EADDRNOTAVAIL: 'netdev' has no assigned IPv4 address.
654 * - EOPNOTSUPP: No IPv4 network stack attached to 'netdev'.
656 * 'address' or 'netmask' or both may be null, in which case the address or
657 * netmask is not reported. */
659 netdev_get_in4(const struct netdev *netdev,
660 struct in_addr *address_, struct in_addr *netmask_)
662 struct in_addr address;
663 struct in_addr netmask;
666 error = (netdev_get_dev(netdev)->netdev_class->get_in4
667 ? netdev_get_dev(netdev)->netdev_class->get_in4(netdev,
671 address_->s_addr = error ? 0 : address.s_addr;
674 netmask_->s_addr = error ? 0 : netmask.s_addr;
679 /* Assigns 'addr' as 'netdev''s IPv4 address and 'mask' as its netmask. If
680 * 'addr' is INADDR_ANY, 'netdev''s IPv4 address is cleared. Returns a
681 * positive errno value. */
683 netdev_set_in4(struct netdev *netdev, struct in_addr addr, struct in_addr mask)
685 return (netdev_get_dev(netdev)->netdev_class->set_in4
686 ? netdev_get_dev(netdev)->netdev_class->set_in4(netdev, addr, mask)
690 /* Obtains ad IPv4 address from device name and save the address in
691 * in4. Returns 0 if successful, otherwise a positive errno value.
694 netdev_get_in4_by_name(const char *device_name, struct in_addr *in4)
696 struct netdev *netdev;
699 error = netdev_open(device_name, "system", &netdev);
701 in4->s_addr = htonl(0);
705 error = netdev_get_in4(netdev, in4, NULL);
706 netdev_close(netdev);
710 /* Adds 'router' as a default IP gateway for the TCP/IP stack that corresponds
713 netdev_add_router(struct netdev *netdev, struct in_addr router)
715 COVERAGE_INC(netdev_add_router);
716 return (netdev_get_dev(netdev)->netdev_class->add_router
717 ? netdev_get_dev(netdev)->netdev_class->add_router(netdev, router)
721 /* Looks up the next hop for 'host' for the TCP/IP stack that corresponds to
722 * 'netdev'. If a route cannot not be determined, sets '*next_hop' to 0,
723 * '*netdev_name' to null, and returns a positive errno value. Otherwise, if a
724 * next hop is found, stores the next hop gateway's address (0 if 'host' is on
725 * a directly connected network) in '*next_hop' and a copy of the name of the
726 * device to reach 'host' in '*netdev_name', and returns 0. The caller is
727 * responsible for freeing '*netdev_name' (by calling free()). */
729 netdev_get_next_hop(const struct netdev *netdev,
730 const struct in_addr *host, struct in_addr *next_hop,
733 int error = (netdev_get_dev(netdev)->netdev_class->get_next_hop
734 ? netdev_get_dev(netdev)->netdev_class->get_next_hop(
735 host, next_hop, netdev_name)
738 next_hop->s_addr = 0;
744 /* Populates 'smap' with status information.
746 * Populates 'smap' with 'netdev' specific status information. This
747 * information may be used to populate the status column of the Interface table
748 * as defined in ovs-vswitchd.conf.db(5). */
750 netdev_get_status(const struct netdev *netdev, struct smap *smap)
752 struct netdev_dev *dev = netdev_get_dev(netdev);
754 return (dev->netdev_class->get_status
755 ? dev->netdev_class->get_status(netdev, smap)
759 /* If 'netdev' has an assigned IPv6 address, sets '*in6' to that address and
760 * returns 0. Otherwise, returns a positive errno value and sets '*in6' to
761 * all-zero-bits (in6addr_any).
763 * The following error values have well-defined meanings:
765 * - EADDRNOTAVAIL: 'netdev' has no assigned IPv6 address.
767 * - EOPNOTSUPP: No IPv6 network stack attached to 'netdev'.
769 * 'in6' may be null, in which case the address itself is not reported. */
771 netdev_get_in6(const struct netdev *netdev, struct in6_addr *in6)
773 struct in6_addr dummy;
776 error = (netdev_get_dev(netdev)->netdev_class->get_in6
777 ? netdev_get_dev(netdev)->netdev_class->get_in6(netdev,
781 memset(in6, 0, sizeof *in6);
786 /* On 'netdev', turns off the flags in 'off' and then turns on the flags in
787 * 'on'. Returns 0 if successful, otherwise a positive errno value. */
789 do_update_flags(struct netdev *netdev, enum netdev_flags off,
790 enum netdev_flags on, enum netdev_flags *old_flagsp,
791 struct netdev_saved_flags **sfp)
793 struct netdev_dev *dev = netdev_get_dev(netdev);
794 struct netdev_saved_flags *sf = NULL;
795 enum netdev_flags old_flags;
798 error = dev->netdev_class->update_flags(dev, off & ~on, on, &old_flags);
800 VLOG_WARN_RL(&rl, "failed to %s flags for network device %s: %s",
801 off || on ? "set" : "get", netdev_get_name(netdev),
804 } else if ((off || on) && sfp) {
805 enum netdev_flags new_flags = (old_flags & ~off) | on;
806 enum netdev_flags changed_flags = old_flags ^ new_flags;
808 *sfp = sf = xmalloc(sizeof *sf);
810 list_push_front(&dev->saved_flags_list, &sf->node);
811 sf->saved_flags = changed_flags;
812 sf->saved_values = changed_flags & new_flags;
819 *old_flagsp = old_flags;
828 /* Obtains the current flags for 'netdev' and stores them into '*flagsp'.
829 * Returns 0 if successful, otherwise a positive errno value. On failure,
830 * stores 0 into '*flagsp'. */
832 netdev_get_flags(const struct netdev *netdev_, enum netdev_flags *flagsp)
834 struct netdev *netdev = CONST_CAST(struct netdev *, netdev_);
835 return do_update_flags(netdev, 0, 0, flagsp, NULL);
838 /* Sets the flags for 'netdev' to 'flags'.
839 * Returns 0 if successful, otherwise a positive errno value. */
841 netdev_set_flags(struct netdev *netdev, enum netdev_flags flags,
842 struct netdev_saved_flags **sfp)
844 return do_update_flags(netdev, -1, flags, NULL, sfp);
847 /* Turns on the specified 'flags' on 'netdev':
849 * - On success, returns 0. If 'sfp' is nonnull, sets '*sfp' to a newly
850 * allocated 'struct netdev_saved_flags *' that may be passed to
851 * netdev_restore_flags() to restore the original values of 'flags' on
852 * 'netdev' (this will happen automatically at program termination if
853 * netdev_restore_flags() is never called) , or to NULL if no flags were
856 * - On failure, returns a positive errno value. If 'sfp' is nonnull, sets
859 netdev_turn_flags_on(struct netdev *netdev, enum netdev_flags flags,
860 struct netdev_saved_flags **sfp)
862 return do_update_flags(netdev, 0, flags, NULL, sfp);
865 /* Turns off the specified 'flags' on 'netdev'. See netdev_turn_flags_on() for
866 * details of the interface. */
868 netdev_turn_flags_off(struct netdev *netdev, enum netdev_flags flags,
869 struct netdev_saved_flags **sfp)
871 return do_update_flags(netdev, flags, 0, NULL, sfp);
874 /* Restores the flags that were saved in 'sf', and destroys 'sf'.
875 * Does nothing if 'sf' is NULL. */
877 netdev_restore_flags(struct netdev_saved_flags *sf)
880 struct netdev_dev *dev = sf->dev;
881 enum netdev_flags old_flags;
883 dev->netdev_class->update_flags(dev,
884 sf->saved_flags & sf->saved_values,
885 sf->saved_flags & ~sf->saved_values,
887 list_remove(&sf->node);
890 netdev_dev_unref(dev);
894 /* Looks up the ARP table entry for 'ip' on 'netdev'. If one exists and can be
895 * successfully retrieved, it stores the corresponding MAC address in 'mac' and
896 * returns 0. Otherwise, it returns a positive errno value; in particular,
897 * ENXIO indicates that there is no ARP table entry for 'ip' on 'netdev'. */
899 netdev_arp_lookup(const struct netdev *netdev,
900 ovs_be32 ip, uint8_t mac[ETH_ADDR_LEN])
902 int error = (netdev_get_dev(netdev)->netdev_class->arp_lookup
903 ? netdev_get_dev(netdev)->netdev_class->arp_lookup(netdev,
907 memset(mac, 0, ETH_ADDR_LEN);
912 /* Returns true if carrier is active (link light is on) on 'netdev'. */
914 netdev_get_carrier(const struct netdev *netdev)
917 enum netdev_flags flags;
920 netdev_get_flags(netdev, &flags);
921 if (!(flags & NETDEV_UP)) {
925 if (!netdev_get_dev(netdev)->netdev_class->get_carrier) {
929 error = netdev_get_dev(netdev)->netdev_class->get_carrier(netdev,
932 VLOG_DBG("%s: failed to get network device carrier status, assuming "
933 "down: %s", netdev_get_name(netdev), strerror(error));
940 /* Returns the number of times 'netdev''s carrier has changed. */
942 netdev_get_carrier_resets(const struct netdev *netdev)
944 return (netdev_get_dev(netdev)->netdev_class->get_carrier_resets
945 ? netdev_get_dev(netdev)->netdev_class->get_carrier_resets(netdev)
949 /* Attempts to force netdev_get_carrier() to poll 'netdev''s MII registers for
950 * link status instead of checking 'netdev''s carrier. 'netdev''s MII
951 * registers will be polled once ever 'interval' milliseconds. If 'netdev'
952 * does not support MII, another method may be used as a fallback. If
953 * 'interval' is less than or equal to zero, reverts netdev_get_carrier() to
954 * its normal behavior.
956 * Returns 0 if successful, otherwise a positive errno value. */
958 netdev_set_miimon_interval(struct netdev *netdev, long long int interval)
960 struct netdev_dev *netdev_dev = netdev_get_dev(netdev);
961 return (netdev_dev->netdev_class->set_miimon_interval
962 ? netdev_dev->netdev_class->set_miimon_interval(netdev, interval)
966 /* Retrieves current device stats for 'netdev'. */
968 netdev_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
972 COVERAGE_INC(netdev_get_stats);
973 error = (netdev_get_dev(netdev)->netdev_class->get_stats
974 ? netdev_get_dev(netdev)->netdev_class->get_stats(netdev, stats)
977 memset(stats, 0xff, sizeof *stats);
982 /* Attempts to change the stats for 'netdev' to those provided in 'stats'.
983 * Returns 0 if successful, otherwise a positive errno value.
985 * This will probably fail for most network devices. Some devices might only
986 * allow setting their stats to 0. */
988 netdev_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
990 return (netdev_get_dev(netdev)->netdev_class->set_stats
991 ? netdev_get_dev(netdev)->netdev_class->set_stats(netdev, stats)
995 /* Attempts to set input rate limiting (policing) policy, such that up to
996 * 'kbits_rate' kbps of traffic is accepted, with a maximum accumulative burst
997 * size of 'kbits' kb. */
999 netdev_set_policing(struct netdev *netdev, uint32_t kbits_rate,
1000 uint32_t kbits_burst)
1002 return (netdev_get_dev(netdev)->netdev_class->set_policing
1003 ? netdev_get_dev(netdev)->netdev_class->set_policing(netdev,
1004 kbits_rate, kbits_burst)
1008 /* Adds to 'types' all of the forms of QoS supported by 'netdev', or leaves it
1009 * empty if 'netdev' does not support QoS. Any names added to 'types' should
1010 * be documented as valid for the "type" column in the "QoS" table in
1011 * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
1013 * Every network device supports disabling QoS with a type of "", but this type
1014 * will not be added to 'types'.
1016 * The caller must initialize 'types' (e.g. with sset_init()) before calling
1017 * this function. The caller is responsible for destroying 'types' (e.g. with
1018 * sset_destroy()) when it is no longer needed.
1020 * Returns 0 if successful, otherwise a positive errno value. */
1022 netdev_get_qos_types(const struct netdev *netdev, struct sset *types)
1024 const struct netdev_class *class = netdev_get_dev(netdev)->netdev_class;
1025 return (class->get_qos_types
1026 ? class->get_qos_types(netdev, types)
1030 /* Queries 'netdev' for its capabilities regarding the specified 'type' of QoS,
1031 * which should be "" or one of the types returned by netdev_get_qos_types()
1032 * for 'netdev'. Returns 0 if successful, otherwise a positive errno value.
1033 * On success, initializes 'caps' with the QoS capabilities; on failure, clears
1034 * 'caps' to all zeros. */
1036 netdev_get_qos_capabilities(const struct netdev *netdev, const char *type,
1037 struct netdev_qos_capabilities *caps)
1039 const struct netdev_class *class = netdev_get_dev(netdev)->netdev_class;
1042 int retval = (class->get_qos_capabilities
1043 ? class->get_qos_capabilities(netdev, type, caps)
1046 memset(caps, 0, sizeof *caps);
1050 /* Every netdev supports turning off QoS. */
1051 memset(caps, 0, sizeof *caps);
1056 /* Obtains the number of queues supported by 'netdev' for the specified 'type'
1057 * of QoS. Returns 0 if successful, otherwise a positive errno value. Stores
1058 * the number of queues (zero on failure) in '*n_queuesp'.
1060 * This is just a simple wrapper around netdev_get_qos_capabilities(). */
1062 netdev_get_n_queues(const struct netdev *netdev,
1063 const char *type, unsigned int *n_queuesp)
1065 struct netdev_qos_capabilities caps;
1068 retval = netdev_get_qos_capabilities(netdev, type, &caps);
1069 *n_queuesp = caps.n_queues;
1073 /* Queries 'netdev' about its currently configured form of QoS. If successful,
1074 * stores the name of the current form of QoS into '*typep', stores any details
1075 * of configuration as string key-value pairs in 'details', and returns 0. On
1076 * failure, sets '*typep' to NULL and returns a positive errno value.
1078 * A '*typep' of "" indicates that QoS is currently disabled on 'netdev'.
1080 * The caller must initialize 'details' as an empty smap (e.g. with
1081 * smap_init()) before calling this function. The caller must free 'details'
1082 * when it is no longer needed (e.g. with smap_destroy()).
1084 * The caller must not modify or free '*typep'.
1086 * '*typep' will be one of the types returned by netdev_get_qos_types() for
1087 * 'netdev'. The contents of 'details' should be documented as valid for
1088 * '*typep' in the "other_config" column in the "QoS" table in
1089 * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)). */
1091 netdev_get_qos(const struct netdev *netdev,
1092 const char **typep, struct smap *details)
1094 const struct netdev_class *class = netdev_get_dev(netdev)->netdev_class;
1097 if (class->get_qos) {
1098 retval = class->get_qos(netdev, typep, details);
1101 smap_clear(details);
1105 /* 'netdev' doesn't support QoS, so report that QoS is disabled. */
1111 /* Attempts to reconfigure QoS on 'netdev', changing the form of QoS to 'type'
1112 * with details of configuration from 'details'. Returns 0 if successful,
1113 * otherwise a positive errno value. On error, the previous QoS configuration
1116 * When this function changes the type of QoS (not just 'details'), this also
1117 * resets all queue configuration for 'netdev' to their defaults (which depend
1118 * on the specific type of QoS). Otherwise, the queue configuration for
1119 * 'netdev' is unchanged.
1121 * 'type' should be "" (to disable QoS) or one of the types returned by
1122 * netdev_get_qos_types() for 'netdev'. The contents of 'details' should be
1123 * documented as valid for the given 'type' in the "other_config" column in the
1124 * "QoS" table in vswitchd/vswitch.xml (which is built as
1125 * ovs-vswitchd.conf.db(8)).
1127 * NULL may be specified for 'details' if there are no configuration
1130 netdev_set_qos(struct netdev *netdev,
1131 const char *type, const struct smap *details)
1133 const struct netdev_class *class = netdev_get_dev(netdev)->netdev_class;
1139 if (class->set_qos) {
1141 static const struct smap empty = SMAP_INITIALIZER(&empty);
1144 return class->set_qos(netdev, type, details);
1146 return *type ? EOPNOTSUPP : 0;
1150 /* Queries 'netdev' for information about the queue numbered 'queue_id'. If
1151 * successful, adds that information as string key-value pairs to 'details'.
1152 * Returns 0 if successful, otherwise a positive errno value.
1154 * 'queue_id' must be less than the number of queues supported by 'netdev' for
1155 * the current form of QoS (e.g. as returned by netdev_get_n_queues(netdev)).
1157 * The returned contents of 'details' should be documented as valid for the
1158 * given 'type' in the "other_config" column in the "Queue" table in
1159 * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
1161 * The caller must initialize 'details' (e.g. with smap_init()) before calling
1162 * this function. The caller must free 'details' when it is no longer needed
1163 * (e.g. with smap_destroy()). */
1165 netdev_get_queue(const struct netdev *netdev,
1166 unsigned int queue_id, struct smap *details)
1168 const struct netdev_class *class = netdev_get_dev(netdev)->netdev_class;
1171 retval = (class->get_queue
1172 ? class->get_queue(netdev, queue_id, details)
1175 smap_clear(details);
1180 /* Configures the queue numbered 'queue_id' on 'netdev' with the key-value
1181 * string pairs in 'details'. The contents of 'details' should be documented
1182 * as valid for the given 'type' in the "other_config" column in the "Queue"
1183 * table in vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
1184 * Returns 0 if successful, otherwise a positive errno value. On failure, the
1185 * given queue's configuration should be unmodified.
1187 * 'queue_id' must be less than the number of queues supported by 'netdev' for
1188 * the current form of QoS (e.g. as returned by netdev_get_n_queues(netdev)).
1190 * This function does not modify 'details', and the caller retains ownership of
1193 netdev_set_queue(struct netdev *netdev,
1194 unsigned int queue_id, const struct smap *details)
1196 const struct netdev_class *class = netdev_get_dev(netdev)->netdev_class;
1197 return (class->set_queue
1198 ? class->set_queue(netdev, queue_id, details)
1202 /* Attempts to delete the queue numbered 'queue_id' from 'netdev'. Some kinds
1203 * of QoS may have a fixed set of queues, in which case attempts to delete them
1204 * will fail with EOPNOTSUPP.
1206 * Returns 0 if successful, otherwise a positive errno value. On failure, the
1207 * given queue will be unmodified.
1209 * 'queue_id' must be less than the number of queues supported by 'netdev' for
1210 * the current form of QoS (e.g. as returned by
1211 * netdev_get_n_queues(netdev)). */
1213 netdev_delete_queue(struct netdev *netdev, unsigned int queue_id)
1215 const struct netdev_class *class = netdev_get_dev(netdev)->netdev_class;
1216 return (class->delete_queue
1217 ? class->delete_queue(netdev, queue_id)
1221 /* Obtains statistics about 'queue_id' on 'netdev'. On success, returns 0 and
1222 * fills 'stats' with the queue's statistics; individual members of 'stats' may
1223 * be set to all-1-bits if the statistic is unavailable. On failure, returns a
1224 * positive errno value and fills 'stats' with all-1-bits. */
1226 netdev_get_queue_stats(const struct netdev *netdev, unsigned int queue_id,
1227 struct netdev_queue_stats *stats)
1229 const struct netdev_class *class = netdev_get_dev(netdev)->netdev_class;
1232 retval = (class->get_queue_stats
1233 ? class->get_queue_stats(netdev, queue_id, stats)
1236 memset(stats, 0xff, sizeof *stats);
1241 /* Iterates over all of 'netdev''s queues, calling 'cb' with the queue's ID,
1242 * its configuration, and the 'aux' specified by the caller. The order of
1243 * iteration is unspecified, but (when successful) each queue is visited
1246 * Calling this function may be more efficient than calling netdev_get_queue()
1249 * 'cb' must not modify or free the 'details' argument passed in. It may
1250 * delete or modify the queue passed in as its 'queue_id' argument. It may
1251 * modify but must not delete any other queue within 'netdev'. 'cb' should not
1252 * add new queues because this may cause some queues to be visited twice or not
1255 * Returns 0 if successful, otherwise a positive errno value. On error, some
1256 * configured queues may not have been included in the iteration. */
1258 netdev_dump_queues(const struct netdev *netdev,
1259 netdev_dump_queues_cb *cb, void *aux)
1261 const struct netdev_class *class = netdev_get_dev(netdev)->netdev_class;
1262 return (class->dump_queues
1263 ? class->dump_queues(netdev, cb, aux)
1267 /* Iterates over all of 'netdev''s queues, calling 'cb' with the queue's ID,
1268 * its statistics, and the 'aux' specified by the caller. The order of
1269 * iteration is unspecified, but (when successful) each queue is visited
1272 * Calling this function may be more efficient than calling
1273 * netdev_get_queue_stats() for every queue.
1275 * 'cb' must not modify or free the statistics passed in.
1277 * Returns 0 if successful, otherwise a positive errno value. On error, some
1278 * configured queues may not have been included in the iteration. */
1280 netdev_dump_queue_stats(const struct netdev *netdev,
1281 netdev_dump_queue_stats_cb *cb, void *aux)
1283 const struct netdev_class *class = netdev_get_dev(netdev)->netdev_class;
1284 return (class->dump_queue_stats
1285 ? class->dump_queue_stats(netdev, cb, aux)
1289 /* Returns a sequence number which indicates changes in one of 'netdev''s
1290 * properties. The returned sequence will be nonzero so that callers have a
1291 * value which they may use as a reset when tracking 'netdev'.
1293 * The returned sequence number will change whenever 'netdev''s flags,
1294 * features, ethernet address, or carrier changes. It may change for other
1295 * reasons as well, or no reason at all. */
1297 netdev_change_seq(const struct netdev *netdev)
1299 return netdev_get_dev(netdev)->netdev_class->change_seq(netdev);
1302 /* Initializes 'netdev_dev' as a netdev device named 'name' of the specified
1303 * 'netdev_class'. This function is ordinarily called from a netdev provider's
1304 * 'create' function.
1306 * This function adds 'netdev_dev' to a netdev-owned shash, so it is
1307 * very important that 'netdev_dev' only be freed after calling
1308 * the refcount drops to zero. */
1310 netdev_dev_init(struct netdev_dev *netdev_dev, const char *name,
1311 const struct netdev_class *netdev_class)
1313 ovs_assert(!shash_find(&netdev_dev_shash, name));
1315 memset(netdev_dev, 0, sizeof *netdev_dev);
1316 netdev_dev->netdev_class = netdev_class;
1317 netdev_dev->name = xstrdup(name);
1318 netdev_dev->node = shash_add(&netdev_dev_shash, name, netdev_dev);
1319 list_init(&netdev_dev->saved_flags_list);
1322 /* Undoes the results of initialization.
1324 * Normally this function does not need to be called as netdev_close has
1325 * the same effect when the refcount drops to zero.
1326 * However, it may be called by providers due to an error on creation
1327 * that occurs after initialization. It this case netdev_close() would
1328 * never be called. */
1330 netdev_dev_uninit(struct netdev_dev *netdev_dev, bool destroy)
1332 char *name = netdev_dev->name;
1334 ovs_assert(!netdev_dev->ref_cnt);
1335 ovs_assert(list_is_empty(&netdev_dev->saved_flags_list));
1337 shash_delete(&netdev_dev_shash, netdev_dev->node);
1340 netdev_dev->netdev_class->destroy(netdev_dev);
1345 /* Returns the class type of 'netdev_dev'.
1347 * The caller must not free the returned value. */
1349 netdev_dev_get_type(const struct netdev_dev *netdev_dev)
1351 return netdev_dev->netdev_class->type;
1354 /* Returns the class associated with 'netdev_dev'. */
1355 const struct netdev_class *
1356 netdev_dev_get_class(const struct netdev_dev *netdev_dev)
1358 return netdev_dev->netdev_class;
1361 /* Returns the name of 'netdev_dev'.
1363 * The caller must not free the returned value. */
1365 netdev_dev_get_name(const struct netdev_dev *netdev_dev)
1367 return netdev_dev->name;
1370 /* Returns the netdev_dev with 'name' or NULL if there is none.
1372 * The caller must not free the returned value. */
1374 netdev_dev_from_name(const char *name)
1376 return shash_find_data(&netdev_dev_shash, name);
1379 /* Fills 'device_list' with devices that match 'netdev_class'.
1381 * The caller is responsible for initializing and destroying 'device_list'
1382 * but the contained netdev_devs must not be freed. */
1384 netdev_dev_get_devices(const struct netdev_class *netdev_class,
1385 struct shash *device_list)
1387 struct shash_node *node;
1388 SHASH_FOR_EACH (node, &netdev_dev_shash) {
1389 struct netdev_dev *dev = node->data;
1391 if (dev->netdev_class == netdev_class) {
1392 shash_add(device_list, node->name, node->data);
1397 /* Initializes 'netdev' as a instance of the netdev_dev.
1399 * This function adds 'netdev' to a netdev-owned linked list, so it is very
1400 * important that 'netdev' only be freed after calling netdev_close(). */
1402 netdev_init(struct netdev *netdev, struct netdev_dev *netdev_dev)
1404 memset(netdev, 0, sizeof *netdev);
1405 netdev->netdev_dev = netdev_dev;
1406 list_push_back(&netdev_list, &netdev->node);
1409 /* Undoes the results of initialization.
1411 * Normally this function only needs to be called from netdev_close().
1412 * However, it may be called by providers due to an error on opening
1413 * that occurs after initialization. It this case netdev_close() would
1414 * never be called. */
1416 netdev_uninit(struct netdev *netdev, bool close)
1418 list_remove(&netdev->node);
1420 netdev_get_dev(netdev)->netdev_class->close(netdev);
1424 /* Returns the class type of 'netdev'.
1426 * The caller must not free the returned value. */
1428 netdev_get_type(const struct netdev *netdev)
1430 return netdev_get_dev(netdev)->netdev_class->type;
1434 netdev_get_type_from_name(const char *name)
1436 const struct netdev_dev *dev = netdev_dev_from_name(name);
1437 return dev ? netdev_dev_get_type(dev) : NULL;
1441 netdev_get_dev(const struct netdev *netdev)
1443 return netdev->netdev_dev;
1447 netdev_rx_init(struct netdev_rx *rx, struct netdev_dev *dev,
1448 const struct netdev_rx_class *class)
1450 ovs_assert(dev->ref_cnt > 0);
1451 rx->rx_class = class;
1452 rx->netdev_dev = dev;
1456 netdev_rx_uninit(struct netdev_rx *rx OVS_UNUSED)
1458 /* Nothing to do. */
1462 netdev_rx_get_dev(const struct netdev_rx *rx)
1464 ovs_assert(rx->netdev_dev->ref_cnt > 0);
1465 return rx->netdev_dev;
1469 netdev_rx_get_name(const struct netdev_rx *rx)
1471 return netdev_dev_get_name(netdev_rx_get_dev(rx));
1475 restore_all_flags(void *aux OVS_UNUSED)
1477 struct shash_node *node;
1479 SHASH_FOR_EACH (node, &netdev_dev_shash) {
1480 struct netdev_dev *dev = node->data;
1481 const struct netdev_saved_flags *sf;
1482 enum netdev_flags saved_values;
1483 enum netdev_flags saved_flags;
1485 saved_values = saved_flags = 0;
1486 LIST_FOR_EACH (sf, node, &dev->saved_flags_list) {
1487 saved_flags |= sf->saved_flags;
1488 saved_values &= ~sf->saved_flags;
1489 saved_values |= sf->saved_flags & sf->saved_values;
1492 enum netdev_flags old_flags;
1494 dev->netdev_class->update_flags(dev,
1495 saved_flags & saved_values,
1496 saved_flags & ~saved_values,