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
476 * Some network devices may not implement support for this function. In such
477 * cases this function will always return EOPNOTSUPP.
480 netdev_recv(struct netdev *netdev, struct ofpbuf *buffer)
482 int (*recv)(struct netdev *, void *, size_t);
485 assert(buffer->size == 0);
486 assert(ofpbuf_tailroom(buffer) >= ETH_TOTAL_MIN);
488 recv = netdev_get_dev(netdev)->netdev_class->recv;
490 ? (recv)(netdev, buffer->data, ofpbuf_tailroom(buffer))
493 COVERAGE_INC(netdev_received);
494 buffer->size += retval;
495 if (buffer->size < ETH_TOTAL_MIN) {
496 ofpbuf_put_zeros(buffer, ETH_TOTAL_MIN - buffer->size);
504 /* Registers with the poll loop to wake up from the next call to poll_block()
505 * when a packet is ready to be received with netdev_recv() on 'netdev'. */
507 netdev_recv_wait(struct netdev *netdev)
509 void (*recv_wait)(struct netdev *);
511 recv_wait = netdev_get_dev(netdev)->netdev_class->recv_wait;
517 /* Discards all packets waiting to be received from 'netdev'. */
519 netdev_drain(struct netdev *netdev)
521 int (*drain)(struct netdev *);
523 drain = netdev_get_dev(netdev)->netdev_class->drain;
524 return drain ? drain(netdev) : 0;
527 /* Sends 'buffer' on 'netdev'. Returns 0 if successful, otherwise a positive
528 * errno value. Returns EAGAIN without blocking if the packet cannot be queued
529 * immediately. Returns EMSGSIZE if a partial packet was transmitted or if
530 * the packet is too big or too small to transmit on the device.
532 * The caller retains ownership of 'buffer' in all cases.
534 * The kernel maintains a packet transmission queue, so the caller is not
535 * expected to do additional queuing of packets.
537 * Some network devices may not implement support for this function. In such
538 * cases this function will always return EOPNOTSUPP. */
540 netdev_send(struct netdev *netdev, const struct ofpbuf *buffer)
542 int (*send)(struct netdev *, const void *, size_t);
545 send = netdev_get_dev(netdev)->netdev_class->send;
546 error = send ? (send)(netdev, buffer->data, buffer->size) : EOPNOTSUPP;
548 COVERAGE_INC(netdev_sent);
553 /* Registers with the poll loop to wake up from the next call to poll_block()
554 * when the packet transmission queue has sufficient room to transmit a packet
555 * with netdev_send().
557 * The kernel maintains a packet transmission queue, so the client is not
558 * expected to do additional queuing of packets. Thus, this function is
559 * unlikely to ever be used. It is included for completeness. */
561 netdev_send_wait(struct netdev *netdev)
563 void (*send_wait)(struct netdev *);
565 send_wait = netdev_get_dev(netdev)->netdev_class->send_wait;
571 /* Attempts to set 'netdev''s MAC address to 'mac'. Returns 0 if successful,
572 * otherwise a positive errno value. */
574 netdev_set_etheraddr(struct netdev *netdev, const uint8_t mac[ETH_ADDR_LEN])
576 return netdev_get_dev(netdev)->netdev_class->set_etheraddr(netdev, mac);
579 /* Retrieves 'netdev''s MAC address. If successful, returns 0 and copies the
580 * the MAC address into 'mac'. On failure, returns a positive errno value and
581 * clears 'mac' to all-zeros. */
583 netdev_get_etheraddr(const struct netdev *netdev, uint8_t mac[ETH_ADDR_LEN])
585 return netdev_get_dev(netdev)->netdev_class->get_etheraddr(netdev, mac);
588 /* Returns the name of the network device that 'netdev' represents,
589 * e.g. "eth0". The caller must not modify or free the returned string. */
591 netdev_get_name(const struct netdev *netdev)
593 return netdev_get_dev(netdev)->name;
596 /* Retrieves the MTU of 'netdev'. The MTU is the maximum size of transmitted
597 * (and received) packets, in bytes, not including the hardware header; thus,
598 * this is typically 1500 bytes for Ethernet devices.
600 * If successful, returns 0 and stores the MTU size in '*mtup'. On failure,
601 * returns a positive errno value and stores ETH_PAYLOAD_MAX (1500) in
604 netdev_get_mtu(const struct netdev *netdev, int *mtup)
606 int error = netdev_get_dev(netdev)->netdev_class->get_mtu(netdev, mtup);
608 VLOG_WARN_RL(&rl, "failed to retrieve MTU for network device %s: %s",
609 netdev_get_name(netdev), strerror(error));
610 *mtup = ETH_PAYLOAD_MAX;
615 /* Returns the ifindex of 'netdev', if successful, as a positive number. On
616 * failure, returns a negative errno value.
618 * The desired semantics of the ifindex value are a combination of those
619 * specified by POSIX for if_nametoindex() and by SNMP for ifIndex. An ifindex
620 * value should be unique within a host and remain stable at least until
621 * reboot. SNMP says an ifindex "ranges between 1 and the value of ifNumber"
622 * but many systems do not follow this rule anyhow.
625 netdev_get_ifindex(const struct netdev *netdev)
627 return netdev_get_dev(netdev)->netdev_class->get_ifindex(netdev);
630 /* Stores the features supported by 'netdev' into each of '*current',
631 * '*advertised', '*supported', and '*peer' that are non-null. Each value is a
632 * bitmap of "enum ofp_port_features" bits, in host byte order. Returns 0 if
633 * successful, otherwise a positive errno value. On failure, all of the
634 * passed-in values are set to 0. */
636 netdev_get_features(struct netdev *netdev,
637 uint32_t *current, uint32_t *advertised,
638 uint32_t *supported, uint32_t *peer)
647 advertised = &dummy[1];
650 supported = &dummy[2];
656 error = netdev_get_dev(netdev)->netdev_class->get_features(netdev, current,
657 advertised, supported, peer);
659 *current = *advertised = *supported = *peer = 0;
664 /* Returns the maximum speed of a network connection that has the "enum
665 * ofp_port_features" bits in 'features', in bits per second. If no bits that
666 * indicate a speed are set in 'features', assumes 100Mbps. */
668 netdev_features_to_bps(uint32_t features)
671 F_10000MB = OFPPF_10GB_FD,
672 F_1000MB = OFPPF_1GB_HD | OFPPF_1GB_FD,
673 F_100MB = OFPPF_100MB_HD | OFPPF_100MB_FD,
674 F_10MB = OFPPF_10MB_HD | OFPPF_10MB_FD
677 return ( features & F_10000MB ? UINT64_C(10000000000)
678 : features & F_1000MB ? UINT64_C(1000000000)
679 : features & F_100MB ? UINT64_C(100000000)
680 : features & F_10MB ? UINT64_C(10000000)
681 : UINT64_C(100000000));
684 /* Returns true if any of the "enum ofp_port_features" bits that indicate a
685 * full-duplex link are set in 'features', otherwise false. */
687 netdev_features_is_full_duplex(uint32_t features)
689 return (features & (OFPPF_10MB_FD | OFPPF_100MB_FD | OFPPF_1GB_FD
690 | OFPPF_10GB_FD)) != 0;
693 /* Set the features advertised by 'netdev' to 'advertise'. Returns 0 if
694 * successful, otherwise a positive errno value. */
696 netdev_set_advertisements(struct netdev *netdev, uint32_t advertise)
698 return (netdev_get_dev(netdev)->netdev_class->set_advertisements
699 ? netdev_get_dev(netdev)->netdev_class->set_advertisements(
704 /* If 'netdev' has an assigned IPv4 address, sets '*address' to that address
705 * and '*netmask' to its netmask and returns 0. Otherwise, returns a positive
706 * errno value and sets '*address' to 0 (INADDR_ANY).
708 * The following error values have well-defined meanings:
710 * - EADDRNOTAVAIL: 'netdev' has no assigned IPv4 address.
712 * - EOPNOTSUPP: No IPv4 network stack attached to 'netdev'.
714 * 'address' or 'netmask' or both may be null, in which case the address or netmask
715 * is not reported. */
717 netdev_get_in4(const struct netdev *netdev,
718 struct in_addr *address_, struct in_addr *netmask_)
720 struct in_addr address;
721 struct in_addr netmask;
724 error = (netdev_get_dev(netdev)->netdev_class->get_in4
725 ? netdev_get_dev(netdev)->netdev_class->get_in4(netdev,
729 address_->s_addr = error ? 0 : address.s_addr;
732 netmask_->s_addr = error ? 0 : netmask.s_addr;
737 /* Assigns 'addr' as 'netdev''s IPv4 address and 'mask' as its netmask. If
738 * 'addr' is INADDR_ANY, 'netdev''s IPv4 address is cleared. Returns a
739 * positive errno value. */
741 netdev_set_in4(struct netdev *netdev, struct in_addr addr, struct in_addr mask)
743 return (netdev_get_dev(netdev)->netdev_class->set_in4
744 ? netdev_get_dev(netdev)->netdev_class->set_in4(netdev, addr, mask)
748 /* Adds 'router' as a default IP gateway for the TCP/IP stack that corresponds
751 netdev_add_router(struct netdev *netdev, struct in_addr router)
753 COVERAGE_INC(netdev_add_router);
754 return (netdev_get_dev(netdev)->netdev_class->add_router
755 ? netdev_get_dev(netdev)->netdev_class->add_router(netdev, router)
759 /* Looks up the next hop for 'host' for the TCP/IP stack that corresponds to
760 * 'netdev'. If a route cannot not be determined, sets '*next_hop' to 0,
761 * '*netdev_name' to null, and returns a positive errno value. Otherwise, if a
762 * next hop is found, stores the next hop gateway's address (0 if 'host' is on
763 * a directly connected network) in '*next_hop' and a copy of the name of the
764 * device to reach 'host' in '*netdev_name', and returns 0. The caller is
765 * responsible for freeing '*netdev_name' (by calling free()). */
767 netdev_get_next_hop(const struct netdev *netdev,
768 const struct in_addr *host, struct in_addr *next_hop,
771 int error = (netdev_get_dev(netdev)->netdev_class->get_next_hop
772 ? netdev_get_dev(netdev)->netdev_class->get_next_hop(
773 host, next_hop, netdev_name)
776 next_hop->s_addr = 0;
782 /* If 'netdev' has an assigned IPv6 address, sets '*in6' to that address and
783 * returns 0. Otherwise, returns a positive errno value and sets '*in6' to
784 * all-zero-bits (in6addr_any).
786 * The following error values have well-defined meanings:
788 * - EADDRNOTAVAIL: 'netdev' has no assigned IPv6 address.
790 * - EOPNOTSUPP: No IPv6 network stack attached to 'netdev'.
792 * 'in6' may be null, in which case the address itself is not reported. */
794 netdev_get_in6(const struct netdev *netdev, struct in6_addr *in6)
796 struct in6_addr dummy;
799 error = (netdev_get_dev(netdev)->netdev_class->get_in6
800 ? netdev_get_dev(netdev)->netdev_class->get_in6(netdev,
804 memset(in6, 0, sizeof *in6);
809 /* On 'netdev', turns off the flags in 'off' and then turns on the flags in
810 * 'on'. If 'permanent' is true, the changes will persist; otherwise, they
811 * will be reverted when 'netdev' is closed or the program exits. Returns 0 if
812 * successful, otherwise a positive errno value. */
814 do_update_flags(struct netdev *netdev, enum netdev_flags off,
815 enum netdev_flags on, enum netdev_flags *old_flagsp,
818 enum netdev_flags old_flags;
821 error = netdev_get_dev(netdev)->netdev_class->update_flags(netdev,
822 off & ~on, on, &old_flags);
824 VLOG_WARN_RL(&rl, "failed to %s flags for network device %s: %s",
825 off || on ? "set" : "get", netdev_get_name(netdev),
828 } else if ((off || on) && !permanent) {
829 enum netdev_flags new_flags = (old_flags & ~off) | on;
830 enum netdev_flags changed_flags = old_flags ^ new_flags;
832 if (!netdev->changed_flags) {
833 netdev->save_flags = old_flags;
835 netdev->changed_flags |= changed_flags;
839 *old_flagsp = old_flags;
844 /* Obtains the current flags for 'netdev' and stores them into '*flagsp'.
845 * Returns 0 if successful, otherwise a positive errno value. On failure,
846 * stores 0 into '*flagsp'. */
848 netdev_get_flags(const struct netdev *netdev_, enum netdev_flags *flagsp)
850 struct netdev *netdev = (struct netdev *) netdev_;
851 return do_update_flags(netdev, 0, 0, flagsp, false);
854 /* Sets the flags for 'netdev' to 'flags'.
855 * If 'permanent' is true, the changes will persist; otherwise, they
856 * will be reverted when 'netdev' is closed or the program exits.
857 * Returns 0 if successful, otherwise a positive errno value. */
859 netdev_set_flags(struct netdev *netdev, enum netdev_flags flags,
862 return do_update_flags(netdev, -1, flags, NULL, permanent);
865 /* Turns on the specified 'flags' on 'netdev'.
866 * If 'permanent' is true, the changes will persist; otherwise, they
867 * will be reverted when 'netdev' is closed or the program exits.
868 * Returns 0 if successful, otherwise a positive errno value. */
870 netdev_turn_flags_on(struct netdev *netdev, enum netdev_flags flags,
873 return do_update_flags(netdev, 0, flags, NULL, permanent);
876 /* Turns off the specified 'flags' on 'netdev'.
877 * If 'permanent' is true, the changes will persist; otherwise, they
878 * will be reverted when 'netdev' is closed or the program exits.
879 * Returns 0 if successful, otherwise a positive errno value. */
881 netdev_turn_flags_off(struct netdev *netdev, enum netdev_flags flags,
884 return do_update_flags(netdev, flags, 0, NULL, permanent);
887 /* Looks up the ARP table entry for 'ip' on 'netdev'. If one exists and can be
888 * successfully retrieved, it stores the corresponding MAC address in 'mac' and
889 * returns 0. Otherwise, it returns a positive errno value; in particular,
890 * ENXIO indicates that there is no ARP table entry for 'ip' on 'netdev'. */
892 netdev_arp_lookup(const struct netdev *netdev,
893 uint32_t ip, uint8_t mac[ETH_ADDR_LEN])
895 int error = (netdev_get_dev(netdev)->netdev_class->arp_lookup
896 ? netdev_get_dev(netdev)->netdev_class->arp_lookup(netdev,
900 memset(mac, 0, ETH_ADDR_LEN);
905 /* Sets 'carrier' to true if carrier is active (link light is on) on
908 netdev_get_carrier(const struct netdev *netdev, bool *carrier)
910 int error = (netdev_get_dev(netdev)->netdev_class->get_carrier
911 ? netdev_get_dev(netdev)->netdev_class->get_carrier(netdev,
920 /* Retrieves current device stats for 'netdev'. */
922 netdev_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
926 COVERAGE_INC(netdev_get_stats);
927 error = (netdev_get_dev(netdev)->netdev_class->get_stats
928 ? netdev_get_dev(netdev)->netdev_class->get_stats(netdev, stats)
931 memset(stats, 0xff, sizeof *stats);
936 /* Attempts to set input rate limiting (policing) policy, such that up to
937 * 'kbits_rate' kbps of traffic is accepted, with a maximum accumulative burst
938 * size of 'kbits' kb. */
940 netdev_set_policing(struct netdev *netdev, uint32_t kbits_rate,
941 uint32_t kbits_burst)
943 return (netdev_get_dev(netdev)->netdev_class->set_policing
944 ? netdev_get_dev(netdev)->netdev_class->set_policing(netdev,
945 kbits_rate, kbits_burst)
949 /* If 'netdev' is a VLAN network device (e.g. one created with vconfig(8)),
950 * sets '*vlan_vid' to the VLAN VID associated with that device and returns 0.
951 * Otherwise returns a errno value (specifically ENOENT if 'netdev_name' is the
952 * name of a network device that is not a VLAN device) and sets '*vlan_vid' to
955 netdev_get_vlan_vid(const struct netdev *netdev, int *vlan_vid)
957 int error = (netdev_get_dev(netdev)->netdev_class->get_vlan_vid
958 ? netdev_get_dev(netdev)->netdev_class->get_vlan_vid(netdev,
967 /* Returns a network device that has 'in4' as its IP address, if one exists,
968 * otherwise a null pointer. */
970 netdev_find_dev_by_in4(const struct in_addr *in4)
972 struct netdev *netdev;
973 struct svec dev_list = SVEC_EMPTY_INITIALIZER;
976 netdev_enumerate(&dev_list);
977 for (i = 0; i < dev_list.n; i++) {
978 const char *name = dev_list.names[i];
979 struct in_addr dev_in4;
981 if (!netdev_open_default(name, &netdev)
982 && !netdev_get_in4(netdev, &dev_in4, NULL)
983 && dev_in4.s_addr == in4->s_addr) {
986 netdev_close(netdev);
991 svec_destroy(&dev_list);
995 /* Initializes 'netdev_dev' as a netdev device named 'name' of the
996 * specified 'netdev_class'.
998 * This function adds 'netdev_dev' to a netdev-owned shash, so it is
999 * very important that 'netdev_dev' only be freed after calling
1000 * the refcount drops to zero. */
1002 netdev_dev_init(struct netdev_dev *netdev_dev, const char *name,
1003 const struct netdev_class *netdev_class)
1005 assert(!shash_find(&netdev_dev_shash, name));
1007 memset(netdev_dev, 0, sizeof *netdev_dev);
1008 netdev_dev->netdev_class = netdev_class;
1009 netdev_dev->name = xstrdup(name);
1010 netdev_dev->node = shash_add(&netdev_dev_shash, name, netdev_dev);
1013 /* Undoes the results of initialization.
1015 * Normally this function does not need to be called as netdev_close has
1016 * the same effect when the refcount drops to zero.
1017 * However, it may be called by providers due to an error on creation
1018 * that occurs after initialization. It this case netdev_close() would
1019 * never be called. */
1021 netdev_dev_uninit(struct netdev_dev *netdev_dev, bool destroy)
1023 char *name = netdev_dev->name;
1025 assert(!netdev_dev->ref_cnt);
1027 shash_delete(&netdev_dev_shash, netdev_dev->node);
1028 update_device_args(netdev_dev, NULL);
1031 netdev_dev->netdev_class->destroy(netdev_dev);
1036 /* Returns the class type of 'netdev_dev'.
1038 * The caller must not free the returned value. */
1040 netdev_dev_get_type(const struct netdev_dev *netdev_dev)
1042 return netdev_dev->netdev_class->type;
1045 /* Returns the name of 'netdev_dev'.
1047 * The caller must not free the returned value. */
1049 netdev_dev_get_name(const struct netdev_dev *netdev_dev)
1051 return netdev_dev->name;
1054 /* Returns the netdev_dev with 'name' or NULL if there is none.
1056 * The caller must not free the returned value. */
1058 netdev_dev_from_name(const char *name)
1060 return shash_find_data(&netdev_dev_shash, name);
1063 /* Fills 'device_list' with devices that match 'netdev_class'.
1065 * The caller is responsible for initializing and destroying 'device_list'
1066 * but the contained netdev_devs must not be freed. */
1068 netdev_dev_get_devices(const struct netdev_class *netdev_class,
1069 struct shash *device_list)
1071 struct shash_node *node;
1072 SHASH_FOR_EACH (node, &netdev_dev_shash) {
1073 struct netdev_dev *dev = node->data;
1075 if (dev->netdev_class == netdev_class) {
1076 shash_add(device_list, node->name, node->data);
1081 /* Initializes 'netdev' as a instance of the netdev_dev.
1083 * This function adds 'netdev' to a netdev-owned linked list, so it is very
1084 * important that 'netdev' only be freed after calling netdev_close(). */
1086 netdev_init(struct netdev *netdev, struct netdev_dev *netdev_dev)
1088 memset(netdev, 0, sizeof *netdev);
1089 netdev->netdev_dev = netdev_dev;
1090 list_push_back(&netdev_list, &netdev->node);
1093 /* Undoes the results of initialization.
1095 * Normally this function only needs to be called from netdev_close().
1096 * However, it may be called by providers due to an error on opening
1097 * that occurs after initialization. It this case netdev_close() would
1098 * never be called. */
1100 netdev_uninit(struct netdev *netdev, bool close)
1102 /* Restore flags that we changed, if any. */
1103 int error = restore_flags(netdev);
1104 list_remove(&netdev->node);
1106 VLOG_WARN("failed to restore network device flags on %s: %s",
1107 netdev_get_name(netdev), strerror(error));
1111 netdev_get_dev(netdev)->netdev_class->close(netdev);
1116 /* Returns the class type of 'netdev'.
1118 * The caller must not free the returned value. */
1120 netdev_get_type(const struct netdev *netdev)
1122 return netdev_get_dev(netdev)->netdev_class->type;
1126 netdev_get_dev(const struct netdev *netdev)
1128 return netdev->netdev_dev;
1131 /* Initializes 'notifier' as a netdev notifier for 'netdev', for which
1132 * notification will consist of calling 'cb', with auxiliary data 'aux'. */
1134 netdev_notifier_init(struct netdev_notifier *notifier, struct netdev *netdev,
1135 void (*cb)(struct netdev_notifier *), void *aux)
1137 notifier->netdev = netdev;
1139 notifier->aux = aux;
1142 /* Tracks changes in the status of a set of network devices. */
1143 struct netdev_monitor {
1144 struct shash polled_netdevs;
1145 struct shash changed_netdevs;
1148 /* Creates and returns a new structure for monitor changes in the status of
1149 * network devices. */
1150 struct netdev_monitor *
1151 netdev_monitor_create(void)
1153 struct netdev_monitor *monitor = xmalloc(sizeof *monitor);
1154 shash_init(&monitor->polled_netdevs);
1155 shash_init(&monitor->changed_netdevs);
1159 /* Destroys 'monitor'. */
1161 netdev_monitor_destroy(struct netdev_monitor *monitor)
1164 struct shash_node *node;
1166 SHASH_FOR_EACH (node, &monitor->polled_netdevs) {
1167 struct netdev_notifier *notifier = node->data;
1168 netdev_get_dev(notifier->netdev)->netdev_class->poll_remove(
1172 shash_destroy(&monitor->polled_netdevs);
1173 shash_destroy(&monitor->changed_netdevs);
1179 netdev_monitor_cb(struct netdev_notifier *notifier)
1181 struct netdev_monitor *monitor = notifier->aux;
1182 const char *name = netdev_get_name(notifier->netdev);
1183 if (!shash_find(&monitor->changed_netdevs, name)) {
1184 shash_add(&monitor->changed_netdevs, name, NULL);
1188 /* Attempts to add 'netdev' as a netdev monitored by 'monitor'. Returns 0 if
1189 * successful, otherwise a positive errno value.
1191 * Adding a given 'netdev' to a monitor multiple times is equivalent to adding
1194 netdev_monitor_add(struct netdev_monitor *monitor, struct netdev *netdev)
1196 const char *netdev_name = netdev_get_name(netdev);
1198 if (!shash_find(&monitor->polled_netdevs, netdev_name)
1199 && netdev_get_dev(netdev)->netdev_class->poll_add)
1201 struct netdev_notifier *notifier;
1202 error = netdev_get_dev(netdev)->netdev_class->poll_add(netdev,
1203 netdev_monitor_cb, monitor, ¬ifier);
1205 assert(notifier->netdev == netdev);
1206 shash_add(&monitor->polled_netdevs, netdev_name, notifier);
1212 /* Removes 'netdev' from the set of netdevs monitored by 'monitor'. (This has
1213 * no effect if 'netdev' is not in the set of devices monitored by
1216 netdev_monitor_remove(struct netdev_monitor *monitor, struct netdev *netdev)
1218 const char *netdev_name = netdev_get_name(netdev);
1219 struct shash_node *node;
1221 node = shash_find(&monitor->polled_netdevs, netdev_name);
1223 /* Cancel future notifications. */
1224 struct netdev_notifier *notifier = node->data;
1225 netdev_get_dev(netdev)->netdev_class->poll_remove(notifier);
1226 shash_delete(&monitor->polled_netdevs, node);
1228 /* Drop any pending notification. */
1229 node = shash_find(&monitor->changed_netdevs, netdev_name);
1231 shash_delete(&monitor->changed_netdevs, node);
1236 /* Checks for changes to netdevs in the set monitored by 'monitor'. If any of
1237 * the attributes (Ethernet address, carrier status, speed or peer-advertised
1238 * speed, flags, etc.) of a network device monitored by 'monitor' has changed,
1239 * sets '*devnamep' to the name of a device that has changed and returns 0.
1240 * The caller is responsible for freeing '*devnamep' (with free()).
1242 * If no devices have changed, sets '*devnamep' to NULL and returns EAGAIN.
1245 netdev_monitor_poll(struct netdev_monitor *monitor, char **devnamep)
1247 struct shash_node *node = shash_first(&monitor->changed_netdevs);
1252 *devnamep = xstrdup(node->name);
1253 shash_delete(&monitor->changed_netdevs, node);
1258 /* Registers with the poll loop to wake up from the next call to poll_block()
1259 * when netdev_monitor_poll(monitor) would indicate that a device has
1262 netdev_monitor_poll_wait(const struct netdev_monitor *monitor)
1264 if (!shash_is_empty(&monitor->changed_netdevs)) {
1265 poll_immediate_wake();
1267 /* XXX Nothing needed here for netdev_linux, but maybe other netdev
1268 * classes need help. */
1272 /* Restore the network device flags on 'netdev' to those that were active
1273 * before we changed them. Returns 0 if successful, otherwise a positive
1276 * To avoid reentry, the caller must ensure that fatal signals are blocked. */
1278 restore_flags(struct netdev *netdev)
1280 if (netdev->changed_flags) {
1281 enum netdev_flags restore = netdev->save_flags & netdev->changed_flags;
1282 enum netdev_flags old_flags;
1283 return netdev_get_dev(netdev)->netdev_class->update_flags(netdev,
1284 netdev->changed_flags & ~restore,
1285 restore, &old_flags);
1290 /* Close all netdevs on shutdown so they can do any needed cleanup such as
1291 * destroying devices, restoring flags, etc. */
1293 close_all_netdevs(void *aux OVS_UNUSED)
1295 struct netdev *netdev, *next;
1296 LIST_FOR_EACH_SAFE(netdev, next, struct netdev, node, &netdev_list) {
1297 netdev_close(netdev);