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"
35 #include "openflow/openflow.h"
37 #include "poll-loop.h"
41 #define THIS_MODULE VLM_netdev
44 static const struct netdev_class *base_netdev_classes[] = {
50 static struct shash netdev_classes = SHASH_INITIALIZER(&netdev_classes);
52 /* All created network devices. */
53 static struct shash netdev_dev_shash = SHASH_INITIALIZER(&netdev_dev_shash);
55 /* All open network devices. */
56 static struct list netdev_list = LIST_INITIALIZER(&netdev_list);
58 /* This is set pretty low because we probably won't learn anything from the
59 * additional log messages. */
60 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
62 static void close_all_netdevs(void *aux OVS_UNUSED);
63 static int restore_flags(struct netdev *netdev);
64 void update_device_args(struct netdev_dev *, const struct shash *args);
67 netdev_initialize(void)
69 static int status = -1;
74 fatal_signal_add_hook(close_all_netdevs, NULL, NULL, true);
77 for (i = 0; i < ARRAY_SIZE(base_netdev_classes); i++) {
78 netdev_register_provider(base_netdev_classes[i]);
83 /* Performs periodic work needed by all the various kinds of netdevs.
85 * If your program opens any netdevs, it must call this function within its
90 struct shash_node *node;
91 SHASH_FOR_EACH(node, &netdev_classes) {
92 const struct netdev_class *netdev_class = node->data;
93 if (netdev_class->run) {
99 /* Arranges for poll_block() to wake up when netdev_run() needs to be called.
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->wait) {
110 netdev_class->wait();
115 /* Initializes and registers a new netdev provider. After successful
116 * registration, new netdevs of that type can be opened using netdev_open(). */
118 netdev_register_provider(const struct netdev_class *new_class)
120 struct netdev_class *new_provider;
122 if (shash_find(&netdev_classes, new_class->type)) {
123 VLOG_WARN("attempted to register duplicate netdev provider: %s",
128 if (new_class->init) {
129 int error = new_class->init();
131 VLOG_ERR("failed to initialize %s network device class: %s",
132 new_class->type, strerror(error));
137 new_provider = xmalloc(sizeof *new_provider);
138 memcpy(new_provider, new_class, sizeof *new_provider);
140 shash_add(&netdev_classes, new_class->type, new_provider);
145 /* Unregisters a netdev provider. 'type' must have been previously
146 * registered and not currently be in use by any netdevs. After unregistration
147 * new netdevs of that type cannot be opened using netdev_open(). */
149 netdev_unregister_provider(const char *type)
151 struct shash_node *del_node, *netdev_dev_node;
153 del_node = shash_find(&netdev_classes, type);
155 VLOG_WARN("attempted to unregister a netdev provider that is not "
156 "registered: %s", type);
160 SHASH_FOR_EACH(netdev_dev_node, &netdev_dev_shash) {
161 struct netdev_dev *netdev_dev = netdev_dev_node->data;
162 if (!strcmp(netdev_dev->netdev_class->type, type)) {
163 VLOG_WARN("attempted to unregister in use netdev provider: %s",
169 shash_delete(&netdev_classes, del_node);
170 free(del_node->data);
175 /* Clears 'types' and enumerates the types of all currently registered netdev
176 * providers into it. The caller must first initialize the svec. */
178 netdev_enumerate_types(struct svec *types)
180 struct shash_node *node;
185 SHASH_FOR_EACH(node, &netdev_classes) {
186 const struct netdev_class *netdev_class = node->data;
187 svec_add(types, netdev_class->type);
191 /* Compares 'args' to those used to those used by 'dev'. Returns true
192 * if the arguments are the same, false otherwise. Does not update the
193 * values stored in 'dev'. */
195 compare_device_args(const struct netdev_dev *dev, const struct shash *args)
197 const struct shash_node **new_args;
201 if (shash_count(args) != dev->n_args) {
205 new_args = shash_sort(args);
206 for (i = 0; i < dev->n_args; i++) {
207 if (strcmp(dev->args[i].key, new_args[i]->name) ||
208 strcmp(dev->args[i].value, new_args[i]->data)) {
220 compare_args(const void *a_, const void *b_)
222 const struct arg *a = a_;
223 const struct arg *b = b_;
224 return strcmp(a->key, b->key);
228 update_device_args(struct netdev_dev *dev, const struct shash *args)
230 struct shash_node *node;
234 for (i = 0; i < dev->n_args; i++) {
235 free(dev->args[i].key);
236 free(dev->args[i].value);
243 if (!args || shash_is_empty(args)) {
247 dev->n_args = shash_count(args);
248 dev->args = xmalloc(dev->n_args * sizeof *dev->args);
251 SHASH_FOR_EACH(node, args) {
252 dev->args[i].key = xstrdup(node->name);
253 dev->args[i].value = xstrdup(node->data);
257 qsort(dev->args, dev->n_args, sizeof *dev->args, compare_args);
261 create_device(struct netdev_options *options, struct netdev_dev **netdev_devp)
263 struct netdev_class *netdev_class;
265 if (!options->may_create) {
266 VLOG_WARN("attempted to create a device that may not be created: %s",
271 if (!options->type || strlen(options->type) == 0) {
272 /* Default to system. */
273 options->type = "system";
276 netdev_class = shash_find_data(&netdev_classes, options->type);
278 VLOG_WARN("could not create netdev %s of unknown type %s",
279 options->name, options->type);
283 return netdev_class->create(options->name, options->type, options->args,
287 /* Opens the network device named 'name' (e.g. "eth0") and returns zero if
288 * successful, otherwise a positive errno value. On success, sets '*netdevp'
289 * to the new network device, otherwise to null.
291 * If this is the first time the device has been opened, then create is called
292 * before opening. The device is created using the given type and arguments.
294 * 'ethertype' may be a 16-bit Ethernet protocol value in host byte order to
295 * capture frames of that type received on the device. It may also be one of
296 * the 'enum netdev_pseudo_ethertype' values to receive frames in one of those
299 * If the 'may_create' flag is set then this is allowed to be the first time
300 * the device is opened (i.e. the refcount will be 1 after this call). It
301 * may be set to false if the device should have already been created.
303 * If the 'may_open' flag is set then the call will succeed even if another
304 * caller has already opened it. It may be to false if the device should not
305 * currently be open. */
308 netdev_open(struct netdev_options *options, struct netdev **netdevp)
310 struct shash empty_args = SHASH_INITIALIZER(&empty_args);
311 struct netdev_dev *netdev_dev;
317 if (!options->args) {
318 options->args = &empty_args;
321 netdev_dev = shash_find_data(&netdev_dev_shash, options->name);
324 error = create_device(options, &netdev_dev);
328 update_device_args(netdev_dev, options->args);
330 } else if (options->may_open) {
331 if (!shash_is_empty(options->args) &&
332 !compare_device_args(netdev_dev, options->args)) {
334 VLOG_WARN("%s: attempted to open already created netdev with "
335 "different arguments", options->name);
339 VLOG_WARN("%s: attempted to create a netdev device with bound name",
344 error = netdev_dev->netdev_class->open(netdev_dev, options->ethertype,
348 netdev_dev->ref_cnt++;
350 if (!netdev_dev->ref_cnt) {
351 netdev_dev_uninit(netdev_dev, true);
359 netdev_open_default(const char *name, struct netdev **netdevp)
361 struct netdev_options options;
363 memset(&options, 0, sizeof options);
366 options.ethertype = NETDEV_ETH_TYPE_NONE;
367 options.may_create = true;
368 options.may_open = true;
370 return netdev_open(&options, netdevp);
373 /* Reconfigures the device 'netdev' with 'args'. 'args' may be empty
374 * or NULL if none are needed. */
376 netdev_reconfigure(struct netdev *netdev, const struct shash *args)
378 struct shash empty_args = SHASH_INITIALIZER(&empty_args);
379 struct netdev_dev *netdev_dev = netdev_get_dev(netdev);
385 if (netdev_dev->netdev_class->reconfigure) {
386 if (!compare_device_args(netdev_dev, args)) {
387 update_device_args(netdev_dev, args);
388 return netdev_dev->netdev_class->reconfigure(netdev_dev, args);
390 } else if (!shash_is_empty(args)) {
391 VLOG_WARN("%s: arguments provided to device that does not have a "
392 "reconfigure function", netdev_get_name(netdev));
398 /* Closes and destroys 'netdev'. */
400 netdev_close(struct netdev *netdev)
403 struct netdev_dev *netdev_dev = netdev_get_dev(netdev);
405 assert(netdev_dev->ref_cnt);
406 netdev_dev->ref_cnt--;
407 netdev_uninit(netdev, true);
409 /* If the reference count for the netdev device is zero, destroy it. */
410 if (!netdev_dev->ref_cnt) {
411 netdev_dev_uninit(netdev_dev, true);
416 /* Returns true if a network device named 'name' exists and may be opened,
417 * otherwise false. */
419 netdev_exists(const char *name)
421 struct netdev *netdev;
424 error = netdev_open_default(name, &netdev);
426 netdev_close(netdev);
429 if (error != ENODEV) {
430 VLOG_WARN("failed to open network device %s: %s",
431 name, strerror(error));
437 /* Clears 'svec' and enumerates the names of all known network devices. */
439 netdev_enumerate(struct svec *svec)
441 struct shash_node *node;
447 SHASH_FOR_EACH(node, &netdev_classes) {
448 const struct netdev_class *netdev_class = node->data;
449 if (netdev_class->enumerate) {
450 int retval = netdev_class->enumerate(svec);
452 VLOG_WARN("failed to enumerate %s network devices: %s",
453 netdev_class->type, strerror(retval));
464 /* Attempts to receive a packet from 'netdev' into 'buffer', which the caller
465 * must have initialized with sufficient room for the packet. The space
466 * required to receive any packet is ETH_HEADER_LEN bytes, plus VLAN_HEADER_LEN
467 * bytes, plus the device's MTU (which may be retrieved via netdev_get_mtu()).
468 * (Some devices do not allow for a VLAN header, in which case VLAN_HEADER_LEN
469 * need not be included.)
471 * If a packet is successfully retrieved, returns 0. In this case 'buffer' is
472 * guaranteed to contain at least ETH_TOTAL_MIN bytes. Otherwise, returns a
473 * positive errno value. Returns EAGAIN immediately if no packet is ready to
477 netdev_recv(struct netdev *netdev, struct ofpbuf *buffer)
481 assert(buffer->size == 0);
482 assert(ofpbuf_tailroom(buffer) >= ETH_TOTAL_MIN);
484 retval = netdev_get_dev(netdev)->netdev_class->recv(netdev, buffer->data,
485 ofpbuf_tailroom(buffer));
487 COVERAGE_INC(netdev_received);
488 buffer->size += retval;
489 if (buffer->size < ETH_TOTAL_MIN) {
490 ofpbuf_put_zeros(buffer, ETH_TOTAL_MIN - buffer->size);
498 /* Registers with the poll loop to wake up from the next call to poll_block()
499 * when a packet is ready to be received with netdev_recv() on 'netdev'. */
501 netdev_recv_wait(struct netdev *netdev)
503 netdev_get_dev(netdev)->netdev_class->recv_wait(netdev);
506 /* Discards all packets waiting to be received from 'netdev'. */
508 netdev_drain(struct netdev *netdev)
510 return netdev_get_dev(netdev)->netdev_class->drain(netdev);
513 /* Sends 'buffer' on 'netdev'. Returns 0 if successful, otherwise a positive
514 * errno value. Returns EAGAIN without blocking if the packet cannot be queued
515 * immediately. Returns EMSGSIZE if a partial packet was transmitted or if
516 * the packet is too big or too small to transmit on the device.
518 * The caller retains ownership of 'buffer' in all cases.
520 * The kernel maintains a packet transmission queue, so the caller is not
521 * expected to do additional queuing of packets. */
523 netdev_send(struct netdev *netdev, const struct ofpbuf *buffer)
525 int error = netdev_get_dev(netdev)->netdev_class->send(netdev,
526 buffer->data, buffer->size);
528 COVERAGE_INC(netdev_sent);
533 /* Registers with the poll loop to wake up from the next call to poll_block()
534 * when the packet transmission queue has sufficient room to transmit a packet
535 * with netdev_send().
537 * The kernel maintains a packet transmission queue, so the client is not
538 * expected to do additional queuing of packets. Thus, this function is
539 * unlikely to ever be used. It is included for completeness. */
541 netdev_send_wait(struct netdev *netdev)
543 return netdev_get_dev(netdev)->netdev_class->send_wait(netdev);
546 /* Attempts to set 'netdev''s MAC address to 'mac'. Returns 0 if successful,
547 * otherwise a positive errno value. */
549 netdev_set_etheraddr(struct netdev *netdev, const uint8_t mac[ETH_ADDR_LEN])
551 return netdev_get_dev(netdev)->netdev_class->set_etheraddr(netdev, mac);
554 /* Retrieves 'netdev''s MAC address. If successful, returns 0 and copies the
555 * the MAC address into 'mac'. On failure, returns a positive errno value and
556 * clears 'mac' to all-zeros. */
558 netdev_get_etheraddr(const struct netdev *netdev, uint8_t mac[ETH_ADDR_LEN])
560 return netdev_get_dev(netdev)->netdev_class->get_etheraddr(netdev, mac);
563 /* Returns the name of the network device that 'netdev' represents,
564 * e.g. "eth0". The caller must not modify or free the returned string. */
566 netdev_get_name(const struct netdev *netdev)
568 return netdev_get_dev(netdev)->name;
571 /* Retrieves the MTU of 'netdev'. The MTU is the maximum size of transmitted
572 * (and received) packets, in bytes, not including the hardware header; thus,
573 * this is typically 1500 bytes for Ethernet devices.
575 * If successful, returns 0 and stores the MTU size in '*mtup'. On failure,
576 * returns a positive errno value and stores ETH_PAYLOAD_MAX (1500) in
579 netdev_get_mtu(const struct netdev *netdev, int *mtup)
581 int error = netdev_get_dev(netdev)->netdev_class->get_mtu(netdev, mtup);
583 VLOG_WARN_RL(&rl, "failed to retrieve MTU for network device %s: %s",
584 netdev_get_name(netdev), strerror(error));
585 *mtup = ETH_PAYLOAD_MAX;
590 /* Returns the ifindex of 'netdev', if successful, as a positive number. On
591 * failure, returns a negative errno value.
593 * The desired semantics of the ifindex value are a combination of those
594 * specified by POSIX for if_nametoindex() and by SNMP for ifIndex. An ifindex
595 * value should be unique within a host and remain stable at least until
596 * reboot. SNMP says an ifindex "ranges between 1 and the value of ifNumber"
597 * but many systems do not follow this rule anyhow.
600 netdev_get_ifindex(const struct netdev *netdev)
602 return netdev_get_dev(netdev)->netdev_class->get_ifindex(netdev);
605 /* Stores the features supported by 'netdev' into each of '*current',
606 * '*advertised', '*supported', and '*peer' that are non-null. Each value is a
607 * bitmap of "enum ofp_port_features" bits, in host byte order. Returns 0 if
608 * successful, otherwise a positive errno value. On failure, all of the
609 * passed-in values are set to 0. */
611 netdev_get_features(struct netdev *netdev,
612 uint32_t *current, uint32_t *advertised,
613 uint32_t *supported, uint32_t *peer)
622 advertised = &dummy[1];
625 supported = &dummy[2];
631 error = netdev_get_dev(netdev)->netdev_class->get_features(netdev, current,
632 advertised, supported, peer);
634 *current = *advertised = *supported = *peer = 0;
639 /* Returns the maximum speed of a network connection that has the "enum
640 * ofp_port_features" bits in 'features', in bits per second. If no bits that
641 * indicate a speed are set in 'features', assumes 100Mbps. */
643 netdev_features_to_bps(uint32_t features)
646 F_10000MB = OFPPF_10GB_FD,
647 F_1000MB = OFPPF_1GB_HD | OFPPF_1GB_FD,
648 F_100MB = OFPPF_100MB_HD | OFPPF_100MB_FD,
649 F_10MB = OFPPF_10MB_HD | OFPPF_10MB_FD
652 return ( features & F_10000MB ? UINT64_C(10000000000)
653 : features & F_1000MB ? UINT64_C(1000000000)
654 : features & F_100MB ? UINT64_C(100000000)
655 : features & F_10MB ? UINT64_C(10000000)
656 : UINT64_C(100000000));
659 /* Returns true if any of the "enum ofp_port_features" bits that indicate a
660 * full-duplex link are set in 'features', otherwise false. */
662 netdev_features_is_full_duplex(uint32_t features)
664 return (features & (OFPPF_10MB_FD | OFPPF_100MB_FD | OFPPF_1GB_FD
665 | OFPPF_10GB_FD)) != 0;
668 /* Set the features advertised by 'netdev' to 'advertise'. Returns 0 if
669 * successful, otherwise a positive errno value. */
671 netdev_set_advertisements(struct netdev *netdev, uint32_t advertise)
673 return (netdev_get_dev(netdev)->netdev_class->set_advertisements
674 ? netdev_get_dev(netdev)->netdev_class->set_advertisements(
679 /* If 'netdev' has an assigned IPv4 address, sets '*address' to that address
680 * and '*netmask' to its netmask and returns 0. Otherwise, returns a positive
681 * errno value and sets '*address' to 0 (INADDR_ANY).
683 * The following error values have well-defined meanings:
685 * - EADDRNOTAVAIL: 'netdev' has no assigned IPv4 address.
687 * - EOPNOTSUPP: No IPv4 network stack attached to 'netdev'.
689 * 'address' or 'netmask' or both may be null, in which case the address or netmask
690 * is not reported. */
692 netdev_get_in4(const struct netdev *netdev,
693 struct in_addr *address_, struct in_addr *netmask_)
695 struct in_addr address;
696 struct in_addr netmask;
699 error = (netdev_get_dev(netdev)->netdev_class->get_in4
700 ? netdev_get_dev(netdev)->netdev_class->get_in4(netdev,
704 address_->s_addr = error ? 0 : address.s_addr;
707 netmask_->s_addr = error ? 0 : netmask.s_addr;
712 /* Assigns 'addr' as 'netdev''s IPv4 address and 'mask' as its netmask. If
713 * 'addr' is INADDR_ANY, 'netdev''s IPv4 address is cleared. Returns a
714 * positive errno value. */
716 netdev_set_in4(struct netdev *netdev, struct in_addr addr, struct in_addr mask)
718 return (netdev_get_dev(netdev)->netdev_class->set_in4
719 ? netdev_get_dev(netdev)->netdev_class->set_in4(netdev, addr, mask)
723 /* Adds 'router' as a default IP gateway for the TCP/IP stack that corresponds
726 netdev_add_router(struct netdev *netdev, struct in_addr router)
728 COVERAGE_INC(netdev_add_router);
729 return (netdev_get_dev(netdev)->netdev_class->add_router
730 ? netdev_get_dev(netdev)->netdev_class->add_router(netdev, router)
734 /* Looks up the next hop for 'host' for the TCP/IP stack that corresponds to
735 * 'netdev'. If a route cannot not be determined, sets '*next_hop' to 0,
736 * '*netdev_name' to null, and returns a positive errno value. Otherwise, if a
737 * next hop is found, stores the next hop gateway's address (0 if 'host' is on
738 * a directly connected network) in '*next_hop' and a copy of the name of the
739 * device to reach 'host' in '*netdev_name', and returns 0. The caller is
740 * responsible for freeing '*netdev_name' (by calling free()). */
742 netdev_get_next_hop(const struct netdev *netdev,
743 const struct in_addr *host, struct in_addr *next_hop,
746 int error = (netdev_get_dev(netdev)->netdev_class->get_next_hop
747 ? netdev_get_dev(netdev)->netdev_class->get_next_hop(
748 host, next_hop, netdev_name)
751 next_hop->s_addr = 0;
757 /* If 'netdev' has an assigned IPv6 address, sets '*in6' to that address and
758 * returns 0. Otherwise, returns a positive errno value and sets '*in6' to
759 * all-zero-bits (in6addr_any).
761 * The following error values have well-defined meanings:
763 * - EADDRNOTAVAIL: 'netdev' has no assigned IPv6 address.
765 * - EOPNOTSUPP: No IPv6 network stack attached to 'netdev'.
767 * 'in6' may be null, in which case the address itself is not reported. */
769 netdev_get_in6(const struct netdev *netdev, struct in6_addr *in6)
771 struct in6_addr dummy;
774 error = (netdev_get_dev(netdev)->netdev_class->get_in6
775 ? netdev_get_dev(netdev)->netdev_class->get_in6(netdev,
779 memset(in6, 0, sizeof *in6);
784 /* On 'netdev', turns off the flags in 'off' and then turns on the flags in
785 * 'on'. If 'permanent' is true, the changes will persist; otherwise, they
786 * will be reverted when 'netdev' is closed or the program exits. Returns 0 if
787 * 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,
793 enum netdev_flags old_flags;
796 error = netdev_get_dev(netdev)->netdev_class->update_flags(netdev,
797 off & ~on, on, &old_flags);
799 VLOG_WARN_RL(&rl, "failed to %s flags for network device %s: %s",
800 off || on ? "set" : "get", netdev_get_name(netdev),
803 } else if ((off || on) && !permanent) {
804 enum netdev_flags new_flags = (old_flags & ~off) | on;
805 enum netdev_flags changed_flags = old_flags ^ new_flags;
807 if (!netdev->changed_flags) {
808 netdev->save_flags = old_flags;
810 netdev->changed_flags |= changed_flags;
814 *old_flagsp = old_flags;
819 /* Obtains the current flags for 'netdev' and stores them into '*flagsp'.
820 * Returns 0 if successful, otherwise a positive errno value. On failure,
821 * stores 0 into '*flagsp'. */
823 netdev_get_flags(const struct netdev *netdev_, enum netdev_flags *flagsp)
825 struct netdev *netdev = (struct netdev *) netdev_;
826 return do_update_flags(netdev, 0, 0, flagsp, false);
829 /* Sets the flags for 'netdev' to 'flags'.
830 * If 'permanent' is true, the changes will persist; otherwise, they
831 * will be reverted when 'netdev' is closed or the program exits.
832 * Returns 0 if successful, otherwise a positive errno value. */
834 netdev_set_flags(struct netdev *netdev, enum netdev_flags flags,
837 return do_update_flags(netdev, -1, flags, NULL, permanent);
840 /* Turns on the specified 'flags' on 'netdev'.
841 * If 'permanent' is true, the changes will persist; otherwise, they
842 * will be reverted when 'netdev' is closed or the program exits.
843 * Returns 0 if successful, otherwise a positive errno value. */
845 netdev_turn_flags_on(struct netdev *netdev, enum netdev_flags flags,
848 return do_update_flags(netdev, 0, flags, NULL, permanent);
851 /* Turns off the specified 'flags' on 'netdev'.
852 * If 'permanent' is true, the changes will persist; otherwise, they
853 * will be reverted when 'netdev' is closed or the program exits.
854 * Returns 0 if successful, otherwise a positive errno value. */
856 netdev_turn_flags_off(struct netdev *netdev, enum netdev_flags flags,
859 return do_update_flags(netdev, flags, 0, NULL, permanent);
862 /* Looks up the ARP table entry for 'ip' on 'netdev'. If one exists and can be
863 * successfully retrieved, it stores the corresponding MAC address in 'mac' and
864 * returns 0. Otherwise, it returns a positive errno value; in particular,
865 * ENXIO indicates that there is no ARP table entry for 'ip' on 'netdev'. */
867 netdev_arp_lookup(const struct netdev *netdev,
868 uint32_t ip, uint8_t mac[ETH_ADDR_LEN])
870 int error = (netdev_get_dev(netdev)->netdev_class->arp_lookup
871 ? netdev_get_dev(netdev)->netdev_class->arp_lookup(netdev,
875 memset(mac, 0, ETH_ADDR_LEN);
880 /* Sets 'carrier' to true if carrier is active (link light is on) on
883 netdev_get_carrier(const struct netdev *netdev, bool *carrier)
885 int error = (netdev_get_dev(netdev)->netdev_class->get_carrier
886 ? netdev_get_dev(netdev)->netdev_class->get_carrier(netdev,
895 /* Retrieves current device stats for 'netdev'. */
897 netdev_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
901 COVERAGE_INC(netdev_get_stats);
902 error = (netdev_get_dev(netdev)->netdev_class->get_stats
903 ? netdev_get_dev(netdev)->netdev_class->get_stats(netdev, stats)
906 memset(stats, 0xff, sizeof *stats);
911 /* Attempts to set input rate limiting (policing) policy, such that up to
912 * 'kbits_rate' kbps of traffic is accepted, with a maximum accumulative burst
913 * size of 'kbits' kb. */
915 netdev_set_policing(struct netdev *netdev, uint32_t kbits_rate,
916 uint32_t kbits_burst)
918 return (netdev_get_dev(netdev)->netdev_class->set_policing
919 ? netdev_get_dev(netdev)->netdev_class->set_policing(netdev,
920 kbits_rate, kbits_burst)
924 /* If 'netdev' is a VLAN network device (e.g. one created with vconfig(8)),
925 * sets '*vlan_vid' to the VLAN VID associated with that device and returns 0.
926 * Otherwise returns a errno value (specifically ENOENT if 'netdev_name' is the
927 * name of a network device that is not a VLAN device) and sets '*vlan_vid' to
930 netdev_get_vlan_vid(const struct netdev *netdev, int *vlan_vid)
932 int error = (netdev_get_dev(netdev)->netdev_class->get_vlan_vid
933 ? netdev_get_dev(netdev)->netdev_class->get_vlan_vid(netdev,
942 /* Returns a network device that has 'in4' as its IP address, if one exists,
943 * otherwise a null pointer. */
945 netdev_find_dev_by_in4(const struct in_addr *in4)
947 struct netdev *netdev;
948 struct svec dev_list = SVEC_EMPTY_INITIALIZER;
951 netdev_enumerate(&dev_list);
952 for (i = 0; i < dev_list.n; i++) {
953 const char *name = dev_list.names[i];
954 struct in_addr dev_in4;
956 if (!netdev_open_default(name, &netdev)
957 && !netdev_get_in4(netdev, &dev_in4, NULL)
958 && dev_in4.s_addr == in4->s_addr) {
961 netdev_close(netdev);
966 svec_destroy(&dev_list);
970 /* Initializes 'netdev_dev' as a netdev device named 'name' of the
971 * specified 'netdev_class'.
973 * This function adds 'netdev_dev' to a netdev-owned shash, so it is
974 * very important that 'netdev_dev' only be freed after calling
975 * the refcount drops to zero. */
977 netdev_dev_init(struct netdev_dev *netdev_dev, const char *name,
978 const struct netdev_class *netdev_class)
980 assert(!shash_find(&netdev_dev_shash, name));
982 memset(netdev_dev, 0, sizeof *netdev_dev);
983 netdev_dev->netdev_class = netdev_class;
984 netdev_dev->name = xstrdup(name);
985 netdev_dev->node = shash_add(&netdev_dev_shash, name, netdev_dev);
988 /* Undoes the results of initialization.
990 * Normally this function does not need to be called as netdev_close has
991 * the same effect when the refcount drops to zero.
992 * However, it may be called by providers due to an error on creation
993 * that occurs after initialization. It this case netdev_close() would
994 * never be called. */
996 netdev_dev_uninit(struct netdev_dev *netdev_dev, bool destroy)
998 char *name = netdev_dev->name;
1000 assert(!netdev_dev->ref_cnt);
1002 shash_delete(&netdev_dev_shash, netdev_dev->node);
1003 update_device_args(netdev_dev, NULL);
1006 netdev_dev->netdev_class->destroy(netdev_dev);
1011 /* Returns the class type of 'netdev_dev'.
1013 * The caller must not free the returned value. */
1015 netdev_dev_get_type(const struct netdev_dev *netdev_dev)
1017 return netdev_dev->netdev_class->type;
1020 /* Returns the name of 'netdev_dev'.
1022 * The caller must not free the returned value. */
1024 netdev_dev_get_name(const struct netdev_dev *netdev_dev)
1026 return netdev_dev->name;
1029 /* Returns the netdev_dev with 'name' or NULL if there is none.
1031 * The caller must not free the returned value. */
1033 netdev_dev_from_name(const char *name)
1035 return shash_find_data(&netdev_dev_shash, name);
1038 /* Fills 'device_list' with devices that match 'netdev_class'.
1040 * The caller is responsible for initializing and destroying 'device_list'
1041 * but the contained netdev_devs must not be freed. */
1043 netdev_dev_get_devices(const struct netdev_class *netdev_class,
1044 struct shash *device_list)
1046 struct shash_node *node;
1047 SHASH_FOR_EACH (node, &netdev_dev_shash) {
1048 struct netdev_dev *dev = node->data;
1050 if (dev->netdev_class == netdev_class) {
1051 shash_add(device_list, node->name, node->data);
1056 /* Initializes 'netdev' as a instance of the netdev_dev.
1058 * This function adds 'netdev' to a netdev-owned linked list, so it is very
1059 * important that 'netdev' only be freed after calling netdev_close(). */
1061 netdev_init(struct netdev *netdev, struct netdev_dev *netdev_dev)
1063 memset(netdev, 0, sizeof *netdev);
1064 netdev->netdev_dev = netdev_dev;
1065 list_push_back(&netdev_list, &netdev->node);
1068 /* Undoes the results of initialization.
1070 * Normally this function only needs to be called from netdev_close().
1071 * However, it may be called by providers due to an error on opening
1072 * that occurs after initialization. It this case netdev_close() would
1073 * never be called. */
1075 netdev_uninit(struct netdev *netdev, bool close)
1077 /* Restore flags that we changed, if any. */
1078 int error = restore_flags(netdev);
1079 list_remove(&netdev->node);
1081 VLOG_WARN("failed to restore network device flags on %s: %s",
1082 netdev_get_name(netdev), strerror(error));
1086 netdev_get_dev(netdev)->netdev_class->close(netdev);
1091 /* Returns the class type of 'netdev'.
1093 * The caller must not free the returned value. */
1095 netdev_get_type(const struct netdev *netdev)
1097 return netdev_get_dev(netdev)->netdev_class->type;
1101 netdev_get_dev(const struct netdev *netdev)
1103 return netdev->netdev_dev;
1106 /* Initializes 'notifier' as a netdev notifier for 'netdev', for which
1107 * notification will consist of calling 'cb', with auxiliary data 'aux'. */
1109 netdev_notifier_init(struct netdev_notifier *notifier, struct netdev *netdev,
1110 void (*cb)(struct netdev_notifier *), void *aux)
1112 notifier->netdev = netdev;
1114 notifier->aux = aux;
1117 /* Tracks changes in the status of a set of network devices. */
1118 struct netdev_monitor {
1119 struct shash polled_netdevs;
1120 struct shash changed_netdevs;
1123 /* Creates and returns a new structure for monitor changes in the status of
1124 * network devices. */
1125 struct netdev_monitor *
1126 netdev_monitor_create(void)
1128 struct netdev_monitor *monitor = xmalloc(sizeof *monitor);
1129 shash_init(&monitor->polled_netdevs);
1130 shash_init(&monitor->changed_netdevs);
1134 /* Destroys 'monitor'. */
1136 netdev_monitor_destroy(struct netdev_monitor *monitor)
1139 struct shash_node *node;
1141 SHASH_FOR_EACH (node, &monitor->polled_netdevs) {
1142 struct netdev_notifier *notifier = node->data;
1143 netdev_get_dev(notifier->netdev)->netdev_class->poll_remove(
1147 shash_destroy(&monitor->polled_netdevs);
1148 shash_destroy(&monitor->changed_netdevs);
1154 netdev_monitor_cb(struct netdev_notifier *notifier)
1156 struct netdev_monitor *monitor = notifier->aux;
1157 const char *name = netdev_get_name(notifier->netdev);
1158 if (!shash_find(&monitor->changed_netdevs, name)) {
1159 shash_add(&monitor->changed_netdevs, name, NULL);
1163 /* Attempts to add 'netdev' as a netdev monitored by 'monitor'. Returns 0 if
1164 * successful, otherwise a positive errno value.
1166 * Adding a given 'netdev' to a monitor multiple times is equivalent to adding
1169 netdev_monitor_add(struct netdev_monitor *monitor, struct netdev *netdev)
1171 const char *netdev_name = netdev_get_name(netdev);
1173 if (!shash_find(&monitor->polled_netdevs, netdev_name)
1174 && netdev_get_dev(netdev)->netdev_class->poll_add)
1176 struct netdev_notifier *notifier;
1177 error = netdev_get_dev(netdev)->netdev_class->poll_add(netdev,
1178 netdev_monitor_cb, monitor, ¬ifier);
1180 assert(notifier->netdev == netdev);
1181 shash_add(&monitor->polled_netdevs, netdev_name, notifier);
1187 /* Removes 'netdev' from the set of netdevs monitored by 'monitor'. (This has
1188 * no effect if 'netdev' is not in the set of devices monitored by
1191 netdev_monitor_remove(struct netdev_monitor *monitor, struct netdev *netdev)
1193 const char *netdev_name = netdev_get_name(netdev);
1194 struct shash_node *node;
1196 node = shash_find(&monitor->polled_netdevs, netdev_name);
1198 /* Cancel future notifications. */
1199 struct netdev_notifier *notifier = node->data;
1200 netdev_get_dev(netdev)->netdev_class->poll_remove(notifier);
1201 shash_delete(&monitor->polled_netdevs, node);
1203 /* Drop any pending notification. */
1204 node = shash_find(&monitor->changed_netdevs, netdev_name);
1206 shash_delete(&monitor->changed_netdevs, node);
1211 /* Checks for changes to netdevs in the set monitored by 'monitor'. If any of
1212 * the attributes (Ethernet address, carrier status, speed or peer-advertised
1213 * speed, flags, etc.) of a network device monitored by 'monitor' has changed,
1214 * sets '*devnamep' to the name of a device that has changed and returns 0.
1215 * The caller is responsible for freeing '*devnamep' (with free()).
1217 * If no devices have changed, sets '*devnamep' to NULL and returns EAGAIN.
1220 netdev_monitor_poll(struct netdev_monitor *monitor, char **devnamep)
1222 struct shash_node *node = shash_first(&monitor->changed_netdevs);
1227 *devnamep = xstrdup(node->name);
1228 shash_delete(&monitor->changed_netdevs, node);
1233 /* Registers with the poll loop to wake up from the next call to poll_block()
1234 * when netdev_monitor_poll(monitor) would indicate that a device has
1237 netdev_monitor_poll_wait(const struct netdev_monitor *monitor)
1239 if (!shash_is_empty(&monitor->changed_netdevs)) {
1240 poll_immediate_wake();
1242 /* XXX Nothing needed here for netdev_linux, but maybe other netdev
1243 * classes need help. */
1247 /* Restore the network device flags on 'netdev' to those that were active
1248 * before we changed them. Returns 0 if successful, otherwise a positive
1251 * To avoid reentry, the caller must ensure that fatal signals are blocked. */
1253 restore_flags(struct netdev *netdev)
1255 if (netdev->changed_flags) {
1256 enum netdev_flags restore = netdev->save_flags & netdev->changed_flags;
1257 enum netdev_flags old_flags;
1258 return netdev_get_dev(netdev)->netdev_class->update_flags(netdev,
1259 netdev->changed_flags & ~restore,
1260 restore, &old_flags);
1265 /* Close all netdevs on shutdown so they can do any needed cleanup such as
1266 * destroying devices, restoring flags, etc. */
1268 close_all_netdevs(void *aux OVS_UNUSED)
1270 struct netdev *netdev, *next;
1271 LIST_FOR_EACH_SAFE(netdev, next, struct netdev, node, &netdev_list) {
1272 netdev_close(netdev);