2 * Copyright (c) 2008, 2009, 2010 Nicira Networks.
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.
23 #include <netinet/in.h>
29 #include "dynamic-string.h"
30 #include "fatal-signal.h"
33 #include "netdev-provider.h"
36 #include "poll-loop.h"
40 #define THIS_MODULE VLM_netdev
43 static const struct netdev_class *netdev_classes[] = {
48 static int n_netdev_classes = ARRAY_SIZE(netdev_classes);
50 /* All created network devices. */
51 static struct shash netdev_dev_shash = SHASH_INITIALIZER(&netdev_dev_shash);
53 /* All open network devices. */
54 static struct list netdev_list = LIST_INITIALIZER(&netdev_list);
56 /* This is set pretty low because we probably won't learn anything from the
57 * additional log messages. */
58 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
60 static void close_all_netdevs(void *aux UNUSED);
61 static int restore_flags(struct netdev *netdev);
62 void update_device_args(struct netdev_dev *, const struct shash *args);
64 /* Attempts to initialize the netdev module. Returns 0 if successful,
65 * otherwise a positive errno value.
67 * Calling this function is optional. If not called explicitly, it will
68 * automatically be called upon the first attempt to open or create a
71 netdev_initialize(void)
73 static int status = -1;
78 fatal_signal_add_hook(close_all_netdevs, NULL, NULL, true);
81 for (i = j = 0; i < n_netdev_classes; i++) {
82 const struct netdev_class *class = netdev_classes[i];
84 int retval = class->init();
86 netdev_classes[j++] = class;
88 VLOG_ERR("failed to initialize %s network device "
89 "class: %s", class->type, strerror(retval));
95 netdev_classes[j++] = class;
103 /* Performs periodic work needed by all the various kinds of netdevs.
105 * If your program opens any netdevs, it must call this function within its
111 for (i = 0; i < n_netdev_classes; i++) {
112 const struct netdev_class *class = netdev_classes[i];
119 /* Arranges for poll_block() to wake up when netdev_run() needs to be called.
121 * If your program opens any netdevs, it must call this function within its
127 for (i = 0; i < n_netdev_classes; i++) {
128 const struct netdev_class *class = netdev_classes[i];
135 /* Compares 'args' to those used to those used by 'dev'. Returns true
136 * if the arguments are the same, false otherwise. Does not update the
137 * values stored in 'dev'. */
139 compare_device_args(const struct netdev_dev *dev, const struct shash *args)
141 const struct shash_node **new_args;
145 if (shash_count(args) != dev->n_args) {
149 new_args = shash_sort(args);
150 for (i = 0; i < dev->n_args; i++) {
151 if (strcmp(dev->args[i].key, new_args[i]->name) ||
152 strcmp(dev->args[i].value, new_args[i]->data)) {
164 compare_args(const void *a_, const void *b_)
166 const struct arg *a = a_;
167 const struct arg *b = b_;
168 return strcmp(a->key, b->key);
172 update_device_args(struct netdev_dev *dev, const struct shash *args)
174 struct shash_node *node;
178 for (i = 0; i < dev->n_args; i++) {
179 free(dev->args[i].key);
180 free(dev->args[i].value);
187 if (!args || shash_is_empty(args)) {
191 dev->n_args = shash_count(args);
192 dev->args = xmalloc(dev->n_args * sizeof *dev->args);
195 SHASH_FOR_EACH(node, args) {
196 dev->args[i].key = xstrdup(node->name);
197 dev->args[i].value = xstrdup(node->data);
201 qsort(dev->args, dev->n_args, sizeof *dev->args, compare_args);
205 create_device(struct netdev_options *options, struct netdev_dev **netdev_devp)
209 if (!options->may_create) {
210 VLOG_WARN("attempted to create a device that may not be created: %s",
215 if (!options->type || strlen(options->type) == 0) {
216 /* Default to system. */
217 options->type = "system";
220 for (i = 0; i < n_netdev_classes; i++) {
221 const struct netdev_class *class = netdev_classes[i];
223 if (!strcmp(options->type, class->type)) {
224 return class->create(options->name, options->type, options->args,
229 VLOG_WARN("could not create netdev %s of unknown type %s", options->name,
234 /* Opens the network device named 'name' (e.g. "eth0") and returns zero if
235 * successful, otherwise a positive errno value. On success, sets '*netdevp'
236 * to the new network device, otherwise to null.
238 * If this is the first time the device has been opened, then create is called
239 * before opening. The device is created using the given type and arguments.
241 * 'ethertype' may be a 16-bit Ethernet protocol value in host byte order to
242 * capture frames of that type received on the device. It may also be one of
243 * the 'enum netdev_pseudo_ethertype' values to receive frames in one of those
246 * If the 'may_create' flag is set then this is allowed to be the first time
247 * the device is opened (i.e. the refcount will be 1 after this call). It
248 * may be set to false if the device should have already been created.
250 * If the 'may_open' flag is set then the call will succeed even if another
251 * caller has already opened it. It may be to false if the device should not
252 * currently be open. */
255 netdev_open(struct netdev_options *options, struct netdev **netdevp)
257 struct shash empty_args = SHASH_INITIALIZER(&empty_args);
258 struct netdev_dev *netdev_dev;
264 if (!options->args) {
265 options->args = &empty_args;
268 netdev_dev = shash_find_data(&netdev_dev_shash, options->name);
271 error = create_device(options, &netdev_dev);
275 update_device_args(netdev_dev, options->args);
277 } else if (options->may_open) {
278 if (!shash_is_empty(options->args) &&
279 !compare_device_args(netdev_dev, options->args)) {
281 VLOG_WARN("%s: attempted to open already created netdev with "
282 "different arguments", options->name);
286 VLOG_WARN("%s: attempted to create a netdev device with bound name",
291 error = netdev_dev->class->open(netdev_dev, options->ethertype, netdevp);
294 netdev_dev->ref_cnt++;
296 if (!netdev_dev->ref_cnt) {
297 netdev_dev_uninit(netdev_dev, true);
305 netdev_open_default(const char *name, struct netdev **netdevp)
307 struct netdev_options options;
309 memset(&options, 0, sizeof options);
312 options.ethertype = NETDEV_ETH_TYPE_NONE;
313 options.may_create = true;
314 options.may_open = true;
316 return netdev_open(&options, netdevp);
319 /* Reconfigures the device 'netdev' with 'args'. 'args' may be empty
320 * or NULL if none are needed. */
322 netdev_reconfigure(struct netdev *netdev, const struct shash *args)
324 struct shash empty_args = SHASH_INITIALIZER(&empty_args);
325 struct netdev_dev *netdev_dev = netdev_get_dev(netdev);
331 if (netdev_dev->class->reconfigure) {
332 if (!compare_device_args(netdev_dev, args)) {
333 update_device_args(netdev_dev, args);
334 return netdev_dev->class->reconfigure(netdev_dev, args);
336 } else if (!shash_is_empty(args)) {
337 VLOG_WARN("%s: arguments provided to device that does not have a "
338 "reconfigure function", netdev_get_name(netdev));
344 /* Closes and destroys 'netdev'. */
346 netdev_close(struct netdev *netdev)
349 struct netdev_dev *netdev_dev = netdev_get_dev(netdev);
351 assert(netdev_dev->ref_cnt);
352 netdev_dev->ref_cnt--;
353 netdev_uninit(netdev, true);
355 /* If the reference count for the netdev device is zero, destroy it. */
356 if (!netdev_dev->ref_cnt) {
357 netdev_dev_uninit(netdev_dev, true);
362 /* Returns true if a network device named 'name' exists and may be opened,
363 * otherwise false. */
365 netdev_exists(const char *name)
367 struct netdev *netdev;
370 error = netdev_open_default(name, &netdev);
372 netdev_close(netdev);
375 if (error != ENODEV) {
376 VLOG_WARN("failed to open network device %s: %s",
377 name, strerror(error));
383 /* Initializes 'svec' with a list of the names of all known network devices. */
385 netdev_enumerate(struct svec *svec)
395 for (i = 0; i < n_netdev_classes; i++) {
396 const struct netdev_class *class = netdev_classes[i];
397 if (class->enumerate) {
398 int retval = class->enumerate(svec);
400 VLOG_WARN("failed to enumerate %s network devices: %s",
401 class->type, strerror(retval));
411 /* Attempts to receive a packet from 'netdev' into 'buffer', which the caller
412 * must have initialized with sufficient room for the packet. The space
413 * required to receive any packet is ETH_HEADER_LEN bytes, plus VLAN_HEADER_LEN
414 * bytes, plus the device's MTU (which may be retrieved via netdev_get_mtu()).
415 * (Some devices do not allow for a VLAN header, in which case VLAN_HEADER_LEN
416 * need not be included.)
418 * If a packet is successfully retrieved, returns 0. In this case 'buffer' is
419 * guaranteed to contain at least ETH_TOTAL_MIN bytes. Otherwise, returns a
420 * positive errno value. Returns EAGAIN immediately if no packet is ready to
424 netdev_recv(struct netdev *netdev, struct ofpbuf *buffer)
428 assert(buffer->size == 0);
429 assert(ofpbuf_tailroom(buffer) >= ETH_TOTAL_MIN);
431 retval = netdev_get_dev(netdev)->class->recv(netdev, buffer->data,
432 ofpbuf_tailroom(buffer));
434 COVERAGE_INC(netdev_received);
435 buffer->size += retval;
436 if (buffer->size < ETH_TOTAL_MIN) {
437 ofpbuf_put_zeros(buffer, ETH_TOTAL_MIN - buffer->size);
445 /* Registers with the poll loop to wake up from the next call to poll_block()
446 * when a packet is ready to be received with netdev_recv() on 'netdev'. */
448 netdev_recv_wait(struct netdev *netdev)
450 netdev_get_dev(netdev)->class->recv_wait(netdev);
453 /* Discards all packets waiting to be received from 'netdev'. */
455 netdev_drain(struct netdev *netdev)
457 return netdev_get_dev(netdev)->class->drain(netdev);
460 /* Sends 'buffer' on 'netdev'. Returns 0 if successful, otherwise a positive
461 * errno value. Returns EAGAIN without blocking if the packet cannot be queued
462 * immediately. Returns EMSGSIZE if a partial packet was transmitted or if
463 * the packet is too big or too small to transmit on the device.
465 * The caller retains ownership of 'buffer' in all cases.
467 * The kernel maintains a packet transmission queue, so the caller is not
468 * expected to do additional queuing of packets. */
470 netdev_send(struct netdev *netdev, const struct ofpbuf *buffer)
472 int error = netdev_get_dev(netdev)->class->send(netdev, buffer->data,
475 COVERAGE_INC(netdev_sent);
480 /* Registers with the poll loop to wake up from the next call to poll_block()
481 * when the packet transmission queue has sufficient room to transmit a packet
482 * with netdev_send().
484 * The kernel maintains a packet transmission queue, so the client is not
485 * expected to do additional queuing of packets. Thus, this function is
486 * unlikely to ever be used. It is included for completeness. */
488 netdev_send_wait(struct netdev *netdev)
490 return netdev_get_dev(netdev)->class->send_wait(netdev);
493 /* Attempts to set 'netdev''s MAC address to 'mac'. Returns 0 if successful,
494 * otherwise a positive errno value. */
496 netdev_set_etheraddr(struct netdev *netdev, const uint8_t mac[ETH_ADDR_LEN])
498 return netdev_get_dev(netdev)->class->set_etheraddr(netdev, mac);
501 /* Retrieves 'netdev''s MAC address. If successful, returns 0 and copies the
502 * the MAC address into 'mac'. On failure, returns a positive errno value and
503 * clears 'mac' to all-zeros. */
505 netdev_get_etheraddr(const struct netdev *netdev, uint8_t mac[ETH_ADDR_LEN])
507 return netdev_get_dev(netdev)->class->get_etheraddr(netdev, mac);
510 /* Returns the name of the network device that 'netdev' represents,
511 * e.g. "eth0". The caller must not modify or free the returned string. */
513 netdev_get_name(const struct netdev *netdev)
515 return netdev_get_dev(netdev)->name;
518 /* Retrieves the MTU of 'netdev'. The MTU is the maximum size of transmitted
519 * (and received) packets, in bytes, not including the hardware header; thus,
520 * this is typically 1500 bytes for Ethernet devices.
522 * If successful, returns 0 and stores the MTU size in '*mtup'. On failure,
523 * returns a positive errno value and stores ETH_PAYLOAD_MAX (1500) in
526 netdev_get_mtu(const struct netdev *netdev, int *mtup)
528 int error = netdev_get_dev(netdev)->class->get_mtu(netdev, mtup);
530 VLOG_WARN_RL(&rl, "failed to retrieve MTU for network device %s: %s",
531 netdev_get_name(netdev), strerror(error));
532 *mtup = ETH_PAYLOAD_MAX;
537 /* Returns the ifindex of 'netdev', if successful, as a positive number. On
538 * failure, returns a negative errno value.
540 * The desired semantics of the ifindex value are a combination of those
541 * specified by POSIX for if_nametoindex() and by SNMP for ifIndex. An ifindex
542 * value should be unique within a host and remain stable at least until
543 * reboot. SNMP says an ifindex "ranges between 1 and the value of ifNumber"
544 * but many systems do not follow this rule anyhow.
547 netdev_get_ifindex(const struct netdev *netdev)
549 return netdev_get_dev(netdev)->class->get_ifindex(netdev);
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 netdev_get_features(struct netdev *netdev,
559 uint32_t *current, uint32_t *advertised,
560 uint32_t *supported, uint32_t *peer)
569 advertised = &dummy[1];
572 supported = &dummy[2];
578 error = netdev_get_dev(netdev)->class->get_features(netdev, current,
579 advertised, supported,
582 *current = *advertised = *supported = *peer = 0;
587 /* Set the features advertised by 'netdev' to 'advertise'. Returns 0 if
588 * successful, otherwise a positive errno value. */
590 netdev_set_advertisements(struct netdev *netdev, uint32_t advertise)
592 return (netdev_get_dev(netdev)->class->set_advertisements
593 ? netdev_get_dev(netdev)->class->set_advertisements(netdev,
598 /* If 'netdev' has an assigned IPv4 address, sets '*address' to that address
599 * and '*netmask' to its netmask and returns 0. Otherwise, returns a positive
600 * errno value and sets '*address' to 0 (INADDR_ANY).
602 * The following error values have well-defined meanings:
604 * - EADDRNOTAVAIL: 'netdev' has no assigned IPv4 address.
606 * - EOPNOTSUPP: No IPv4 network stack attached to 'netdev'.
608 * 'address' or 'netmask' or both may be null, in which case the address or netmask
609 * is not reported. */
611 netdev_get_in4(const struct netdev *netdev,
612 struct in_addr *address_, struct in_addr *netmask_)
614 struct in_addr address;
615 struct in_addr netmask;
618 error = (netdev_get_dev(netdev)->class->get_in4
619 ? netdev_get_dev(netdev)->class->get_in4(netdev, &address,
623 address_->s_addr = error ? 0 : address.s_addr;
626 netmask_->s_addr = error ? 0 : netmask.s_addr;
631 /* Assigns 'addr' as 'netdev''s IPv4 address and 'mask' as its netmask. If
632 * 'addr' is INADDR_ANY, 'netdev''s IPv4 address is cleared. Returns a
633 * positive errno value. */
635 netdev_set_in4(struct netdev *netdev, struct in_addr addr, struct in_addr mask)
637 return (netdev_get_dev(netdev)->class->set_in4
638 ? netdev_get_dev(netdev)->class->set_in4(netdev, addr, mask)
642 /* Adds 'router' as a default IP gateway for the TCP/IP stack that corresponds
645 netdev_add_router(struct netdev *netdev, struct in_addr router)
647 COVERAGE_INC(netdev_add_router);
648 return (netdev_get_dev(netdev)->class->add_router
649 ? netdev_get_dev(netdev)->class->add_router(netdev, router)
653 /* Looks up the next hop for 'host' for the TCP/IP stack that corresponds to
654 * 'netdev'. If a route cannot not be determined, sets '*next_hop' to 0,
655 * '*netdev_name' to null, and returns a positive errno value. Otherwise, if a
656 * next hop is found, stores the next hop gateway's address (0 if 'host' is on
657 * a directly connected network) in '*next_hop' and a copy of the name of the
658 * device to reach 'host' in '*netdev_name', and returns 0. The caller is
659 * responsible for freeing '*netdev_name' (by calling free()). */
661 netdev_get_next_hop(const struct netdev *netdev,
662 const struct in_addr *host, struct in_addr *next_hop,
665 int error = (netdev_get_dev(netdev)->class->get_next_hop
666 ? netdev_get_dev(netdev)->class->get_next_hop(host, next_hop,
670 next_hop->s_addr = 0;
676 /* If 'netdev' has an assigned IPv6 address, sets '*in6' to that address and
677 * returns 0. Otherwise, returns a positive errno value and sets '*in6' to
678 * all-zero-bits (in6addr_any).
680 * The following error values have well-defined meanings:
682 * - EADDRNOTAVAIL: 'netdev' has no assigned IPv6 address.
684 * - EOPNOTSUPP: No IPv6 network stack attached to 'netdev'.
686 * 'in6' may be null, in which case the address itself is not reported. */
688 netdev_get_in6(const struct netdev *netdev, struct in6_addr *in6)
690 struct in6_addr dummy;
693 error = (netdev_get_dev(netdev)->class->get_in6
694 ? netdev_get_dev(netdev)->class->get_in6(netdev, in6 ? in6
698 memset(in6, 0, sizeof *in6);
703 /* On 'netdev', turns off the flags in 'off' and then turns on the flags in
704 * 'on'. If 'permanent' is true, the changes will persist; otherwise, they
705 * will be reverted when 'netdev' is closed or the program exits. Returns 0 if
706 * successful, otherwise a positive errno value. */
708 do_update_flags(struct netdev *netdev, enum netdev_flags off,
709 enum netdev_flags on, enum netdev_flags *old_flagsp,
712 enum netdev_flags old_flags;
715 error = netdev_get_dev(netdev)->class->update_flags(netdev, off & ~on, on,
718 VLOG_WARN_RL(&rl, "failed to %s flags for network device %s: %s",
719 off || on ? "set" : "get", netdev_get_name(netdev),
722 } else if ((off || on) && !permanent) {
723 enum netdev_flags new_flags = (old_flags & ~off) | on;
724 enum netdev_flags changed_flags = old_flags ^ new_flags;
726 if (!netdev->changed_flags) {
727 netdev->save_flags = old_flags;
729 netdev->changed_flags |= changed_flags;
733 *old_flagsp = old_flags;
738 /* Obtains the current flags for 'netdev' and stores them into '*flagsp'.
739 * Returns 0 if successful, otherwise a positive errno value. On failure,
740 * stores 0 into '*flagsp'. */
742 netdev_get_flags(const struct netdev *netdev_, enum netdev_flags *flagsp)
744 struct netdev *netdev = (struct netdev *) netdev_;
745 return do_update_flags(netdev, 0, 0, flagsp, false);
748 /* Sets the flags for 'netdev' to 'flags'.
749 * If 'permanent' is true, the changes will persist; otherwise, they
750 * will be reverted when 'netdev' is closed or the program exits.
751 * Returns 0 if successful, otherwise a positive errno value. */
753 netdev_set_flags(struct netdev *netdev, enum netdev_flags flags,
756 return do_update_flags(netdev, -1, flags, NULL, permanent);
759 /* Turns on the specified 'flags' on 'netdev'.
760 * If 'permanent' is true, the changes will persist; otherwise, they
761 * will be reverted when 'netdev' is closed or the program exits.
762 * Returns 0 if successful, otherwise a positive errno value. */
764 netdev_turn_flags_on(struct netdev *netdev, enum netdev_flags flags,
767 return do_update_flags(netdev, 0, flags, NULL, permanent);
770 /* Turns off the specified 'flags' on 'netdev'.
771 * If 'permanent' is true, the changes will persist; otherwise, they
772 * will be reverted when 'netdev' is closed or the program exits.
773 * Returns 0 if successful, otherwise a positive errno value. */
775 netdev_turn_flags_off(struct netdev *netdev, enum netdev_flags flags,
778 return do_update_flags(netdev, flags, 0, NULL, permanent);
781 /* Looks up the ARP table entry for 'ip' on 'netdev'. If one exists and can be
782 * successfully retrieved, it stores the corresponding MAC address in 'mac' and
783 * returns 0. Otherwise, it returns a positive errno value; in particular,
784 * ENXIO indicates that there is no ARP table entry for 'ip' on 'netdev'. */
786 netdev_arp_lookup(const struct netdev *netdev,
787 uint32_t ip, uint8_t mac[ETH_ADDR_LEN])
789 int error = (netdev_get_dev(netdev)->class->arp_lookup
790 ? netdev_get_dev(netdev)->class->arp_lookup(netdev, ip, mac)
793 memset(mac, 0, ETH_ADDR_LEN);
798 /* Sets 'carrier' to true if carrier is active (link light is on) on
801 netdev_get_carrier(const struct netdev *netdev, bool *carrier)
803 int error = (netdev_get_dev(netdev)->class->get_carrier
804 ? netdev_get_dev(netdev)->class->get_carrier(netdev, carrier)
812 /* Retrieves current device stats for 'netdev'. */
814 netdev_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
818 COVERAGE_INC(netdev_get_stats);
819 error = (netdev_get_dev(netdev)->class->get_stats
820 ? netdev_get_dev(netdev)->class->get_stats(netdev, stats)
823 memset(stats, 0xff, sizeof *stats);
828 /* Attempts to set input rate limiting (policing) policy, such that up to
829 * 'kbits_rate' kbps of traffic is accepted, with a maximum accumulative burst
830 * size of 'kbits' kb. */
832 netdev_set_policing(struct netdev *netdev, uint32_t kbits_rate,
833 uint32_t kbits_burst)
835 return (netdev_get_dev(netdev)->class->set_policing
836 ? netdev_get_dev(netdev)->class->set_policing(netdev, kbits_rate,
841 /* If 'netdev' is a VLAN network device (e.g. one created with vconfig(8)),
842 * sets '*vlan_vid' to the VLAN VID associated with that device and returns 0.
843 * Otherwise returns a errno value (specifically ENOENT if 'netdev_name' is the
844 * name of a network device that is not a VLAN device) and sets '*vlan_vid' to
847 netdev_get_vlan_vid(const struct netdev *netdev, int *vlan_vid)
849 int error = (netdev_get_dev(netdev)->class->get_vlan_vid
850 ? netdev_get_dev(netdev)->class->get_vlan_vid(netdev, vlan_vid)
858 /* Returns a network device that has 'in4' as its IP address, if one exists,
859 * otherwise a null pointer. */
861 netdev_find_dev_by_in4(const struct in_addr *in4)
863 struct netdev *netdev;
864 struct svec dev_list;
867 netdev_enumerate(&dev_list);
868 for (i = 0; i < dev_list.n; i++) {
869 const char *name = dev_list.names[i];
870 struct in_addr dev_in4;
872 if (!netdev_open_default(name, &netdev)
873 && !netdev_get_in4(netdev, &dev_in4, NULL)
874 && dev_in4.s_addr == in4->s_addr) {
877 netdev_close(netdev);
882 svec_destroy(&dev_list);
886 /* Initializes 'netdev_dev' as a netdev device named 'name' of the
889 * This function adds 'netdev_dev' to a netdev-owned shash, so it is
890 * very important that 'netdev_dev' only be freed after calling
891 * the refcount drops to zero. */
893 netdev_dev_init(struct netdev_dev *netdev_dev, const char *name,
894 const struct netdev_class *class)
896 assert(!shash_find(&netdev_dev_shash, name));
898 memset(netdev_dev, 0, sizeof *netdev_dev);
899 netdev_dev->class = class;
900 netdev_dev->name = xstrdup(name);
901 netdev_dev->node = shash_add(&netdev_dev_shash, name, netdev_dev);
904 /* Undoes the results of initialization.
906 * Normally this function does not need to be called as netdev_close has
907 * the same effect when the refcount drops to zero.
908 * However, it may be called by providers due to an error on creation
909 * that occurs after initialization. It this case netdev_close() would
910 * never be called. */
912 netdev_dev_uninit(struct netdev_dev *netdev_dev, bool destroy)
914 char *name = netdev_dev->name;
916 assert(!netdev_dev->ref_cnt);
918 shash_delete(&netdev_dev_shash, netdev_dev->node);
919 update_device_args(netdev_dev, NULL);
922 netdev_dev->class->destroy(netdev_dev);
927 /* Returns the class type of 'netdev_dev'.
929 * The caller must not free the returned value. */
931 netdev_dev_get_type(const struct netdev_dev *netdev_dev)
933 return netdev_dev->class->type;
936 /* Returns the name of 'netdev_dev'.
938 * The caller must not free the returned value. */
940 netdev_dev_get_name(const struct netdev_dev *netdev_dev)
942 return netdev_dev->name;
945 /* Returns the netdev_dev with 'name' or NULL if there is none.
947 * The caller must not free the returned value. */
949 netdev_dev_from_name(const char *name)
951 return shash_find_data(&netdev_dev_shash, name);
954 /* Fills 'device_list' with devices that match 'class'.
956 * The caller is responsible for initializing and destroying 'device_list'
957 * but the contained netdev_devs must not be freed. */
959 netdev_dev_get_devices(const struct netdev_class *class,
960 struct shash *device_list)
962 struct shash_node *node;
963 SHASH_FOR_EACH (node, &netdev_dev_shash) {
964 struct netdev_dev *dev = node->data;
966 if (dev->class == class) {
967 shash_add(device_list, node->name, node->data);
972 /* Initializes 'netdev' as a instance of the netdev_dev.
974 * This function adds 'netdev' to a netdev-owned linked list, so it is very
975 * important that 'netdev' only be freed after calling netdev_close(). */
977 netdev_init(struct netdev *netdev, struct netdev_dev *netdev_dev)
979 memset(netdev, 0, sizeof *netdev);
980 netdev->netdev_dev = netdev_dev;
981 list_push_back(&netdev_list, &netdev->node);
984 /* Undoes the results of initialization.
986 * Normally this function only needs to be called from netdev_close().
987 * However, it may be called by providers due to an error on opening
988 * that occurs after initialization. It this case netdev_close() would
989 * never be called. */
991 netdev_uninit(struct netdev *netdev, bool close)
993 /* Restore flags that we changed, if any. */
994 int error = restore_flags(netdev);
995 list_remove(&netdev->node);
997 VLOG_WARN("failed to restore network device flags on %s: %s",
998 netdev_get_name(netdev), strerror(error));
1002 netdev_get_dev(netdev)->class->close(netdev);
1007 /* Returns the class type of 'netdev'.
1009 * The caller must not free the returned value. */
1011 netdev_get_type(const struct netdev *netdev)
1013 return netdev_get_dev(netdev)->class->type;
1017 netdev_get_dev(const struct netdev *netdev)
1019 return netdev->netdev_dev;
1022 /* Initializes 'notifier' as a netdev notifier for 'netdev', for which
1023 * notification will consist of calling 'cb', with auxiliary data 'aux'. */
1025 netdev_notifier_init(struct netdev_notifier *notifier, struct netdev *netdev,
1026 void (*cb)(struct netdev_notifier *), void *aux)
1028 notifier->netdev = netdev;
1030 notifier->aux = aux;
1033 /* Tracks changes in the status of a set of network devices. */
1034 struct netdev_monitor {
1035 struct shash polled_netdevs;
1036 struct shash changed_netdevs;
1039 /* Creates and returns a new structure for monitor changes in the status of
1040 * network devices. */
1041 struct netdev_monitor *
1042 netdev_monitor_create(void)
1044 struct netdev_monitor *monitor = xmalloc(sizeof *monitor);
1045 shash_init(&monitor->polled_netdevs);
1046 shash_init(&monitor->changed_netdevs);
1050 /* Destroys 'monitor'. */
1052 netdev_monitor_destroy(struct netdev_monitor *monitor)
1055 struct shash_node *node;
1057 SHASH_FOR_EACH (node, &monitor->polled_netdevs) {
1058 struct netdev_notifier *notifier = node->data;
1059 netdev_get_dev(notifier->netdev)->class->poll_remove(notifier);
1062 shash_destroy(&monitor->polled_netdevs);
1063 shash_destroy(&monitor->changed_netdevs);
1069 netdev_monitor_cb(struct netdev_notifier *notifier)
1071 struct netdev_monitor *monitor = notifier->aux;
1072 const char *name = netdev_get_name(notifier->netdev);
1073 if (!shash_find(&monitor->changed_netdevs, name)) {
1074 shash_add(&monitor->changed_netdevs, name, NULL);
1078 /* Attempts to add 'netdev' as a netdev monitored by 'monitor'. Returns 0 if
1079 * successful, otherwise a positive errno value.
1081 * Adding a given 'netdev' to a monitor multiple times is equivalent to adding
1084 netdev_monitor_add(struct netdev_monitor *monitor, struct netdev *netdev)
1086 const char *netdev_name = netdev_get_name(netdev);
1088 if (!shash_find(&monitor->polled_netdevs, netdev_name)
1089 && netdev_get_dev(netdev)->class->poll_add)
1091 struct netdev_notifier *notifier;
1092 error = netdev_get_dev(netdev)->class->poll_add(netdev,
1094 monitor, ¬ifier);
1096 assert(notifier->netdev == netdev);
1097 shash_add(&monitor->polled_netdevs, netdev_name, notifier);
1103 /* Removes 'netdev' from the set of netdevs monitored by 'monitor'. (This has
1104 * no effect if 'netdev' is not in the set of devices monitored by
1107 netdev_monitor_remove(struct netdev_monitor *monitor, struct netdev *netdev)
1109 const char *netdev_name = netdev_get_name(netdev);
1110 struct shash_node *node;
1112 node = shash_find(&monitor->polled_netdevs, netdev_name);
1114 /* Cancel future notifications. */
1115 struct netdev_notifier *notifier = node->data;
1116 netdev_get_dev(netdev)->class->poll_remove(notifier);
1117 shash_delete(&monitor->polled_netdevs, node);
1119 /* Drop any pending notification. */
1120 node = shash_find(&monitor->changed_netdevs, netdev_name);
1122 shash_delete(&monitor->changed_netdevs, node);
1127 /* Checks for changes to netdevs in the set monitored by 'monitor'. If any of
1128 * the attributes (Ethernet address, carrier status, speed or peer-advertised
1129 * speed, flags, etc.) of a network device monitored by 'monitor' has changed,
1130 * sets '*devnamep' to the name of a device that has changed and returns 0.
1131 * The caller is responsible for freeing '*devnamep' (with free()).
1133 * If no devices have changed, sets '*devnamep' to NULL and returns EAGAIN.
1136 netdev_monitor_poll(struct netdev_monitor *monitor, char **devnamep)
1138 struct shash_node *node = shash_first(&monitor->changed_netdevs);
1143 *devnamep = xstrdup(node->name);
1144 shash_delete(&monitor->changed_netdevs, node);
1149 /* Registers with the poll loop to wake up from the next call to poll_block()
1150 * when netdev_monitor_poll(monitor) would indicate that a device has
1153 netdev_monitor_poll_wait(const struct netdev_monitor *monitor)
1155 if (!shash_is_empty(&monitor->changed_netdevs)) {
1156 poll_immediate_wake();
1158 /* XXX Nothing needed here for netdev_linux, but maybe other netdev
1159 * classes need help. */
1163 /* Restore the network device flags on 'netdev' to those that were active
1164 * before we changed them. Returns 0 if successful, otherwise a positive
1167 * To avoid reentry, the caller must ensure that fatal signals are blocked. */
1169 restore_flags(struct netdev *netdev)
1171 if (netdev->changed_flags) {
1172 enum netdev_flags restore = netdev->save_flags & netdev->changed_flags;
1173 enum netdev_flags old_flags;
1174 return netdev_get_dev(netdev)->class->update_flags(netdev,
1175 netdev->changed_flags & ~restore,
1176 restore, &old_flags);
1181 /* Close all netdevs on shutdown so they can do any needed cleanup such as
1182 * destroying devices, restoring flags, etc. */
1184 close_all_netdevs(void *aux UNUSED)
1186 struct netdev *netdev, *next;
1187 LIST_FOR_EACH_SAFE(netdev, next, struct netdev, node, &netdev_list) {
1188 netdev_close(netdev);