2 * Copyright (c) 2008, 2009 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"
32 #include "netdev-provider.h"
35 #include "poll-loop.h"
39 #define THIS_MODULE VLM_netdev
42 static const struct netdev_class *netdev_classes[] = {
46 static int n_netdev_classes = ARRAY_SIZE(netdev_classes);
48 /* All open network devices. */
49 static struct list netdev_list = LIST_INITIALIZER(&netdev_list);
51 /* This is set pretty low because we probably won't learn anything from the
52 * additional log messages. */
53 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
55 static void restore_all_flags(void *aux);
56 static int restore_flags(struct netdev *netdev);
58 /* Attempts to initialize the netdev module. Returns 0 if successful,
59 * otherwise a positive errno value.
61 * Calling this function is optional. If not called explicitly, it will
62 * automatically be called upon the first attempt to open a network device. */
64 netdev_initialize(void)
66 static int status = -1;
70 fatal_signal_add_hook(restore_all_flags, NULL, true);
73 for (i = j = 0; i < n_netdev_classes; i++) {
74 const struct netdev_class *class = netdev_classes[i];
76 int retval = class->init();
78 netdev_classes[j++] = class;
80 VLOG_ERR("failed to initialize %s network device "
81 "class: %s", class->name, strerror(retval));
87 netdev_classes[j++] = class;
95 /* Performs periodic work needed by all the various kinds of netdevs.
97 * If your program opens any netdevs, it must call this function within its
103 for (i = 0; i < n_netdev_classes; i++) {
104 const struct netdev_class *class = netdev_classes[i];
111 /* Arranges for poll_block() to wake up when netdev_run() needs to be called.
113 * If your program opens any netdevs, it must call this function within its
119 for (i = 0; i < n_netdev_classes; i++) {
120 const struct netdev_class *class = netdev_classes[i];
127 /* Opens the network device named 'name' (e.g. "eth0") and returns zero if
128 * successful, otherwise a positive errno value. On success, sets '*netdevp'
129 * to the new network device, otherwise to null.
131 * 'ethertype' may be a 16-bit Ethernet protocol value in host byte order to
132 * capture frames of that type received on the device. It may also be one of
133 * the 'enum netdev_pseudo_ethertype' values to receive frames in one of those
136 netdev_open(const char *name_, int ethertype, struct netdev **netdevp)
138 char *name = xstrdup(name_);
139 char *prefix, *suffix, *colon;
140 struct netdev *netdev = NULL;
145 colon = strchr(name, ':');
155 for (i = 0; i < n_netdev_classes; i++) {
156 const struct netdev_class *class = netdev_classes[i];
157 if (!strcmp(prefix, class->prefix)) {
158 error = class->open(name_, suffix, ethertype, &netdev);
162 error = EAFNOSUPPORT;
165 *netdevp = error ? NULL : netdev;
170 /* Closes and destroys 'netdev'. */
172 netdev_close(struct netdev *netdev)
178 /* Restore flags that we changed, if any. */
179 fatal_signal_block();
180 error = restore_flags(netdev);
181 list_remove(&netdev->node);
182 fatal_signal_unblock();
184 VLOG_WARN("failed to restore network device flags on %s: %s",
185 netdev->name, strerror(error));
190 netdev->class->close(netdev);
195 /* Returns true if a network device named 'name' exists and may be opened,
196 * otherwise false. */
198 netdev_exists(const char *name)
200 struct netdev *netdev;
203 error = netdev_open(name, NETDEV_ETH_TYPE_NONE, &netdev);
205 netdev_close(netdev);
208 if (error != ENODEV) {
209 VLOG_WARN("failed to open network device %s: %s",
210 name, strerror(error));
216 /* Initializes 'svec' with a list of the names of all known network devices. */
218 netdev_enumerate(struct svec *svec)
228 for (i = 0; i < n_netdev_classes; i++) {
229 const struct netdev_class *class = netdev_classes[i];
230 if (class->enumerate) {
231 int retval = class->enumerate(svec);
233 VLOG_WARN("failed to enumerate %s network devices: %s",
234 class->name, strerror(retval));
244 /* Attempts to receive a packet from 'netdev' into 'buffer', which the caller
245 * must have initialized with sufficient room for the packet. The space
246 * required to receive any packet is ETH_HEADER_LEN bytes, plus VLAN_HEADER_LEN
247 * bytes, plus the device's MTU (which may be retrieved via netdev_get_mtu()).
248 * (Some devices do not allow for a VLAN header, in which case VLAN_HEADER_LEN
249 * need not be included.)
251 * If a packet is successfully retrieved, returns 0. In this case 'buffer' is
252 * guaranteed to contain at least ETH_TOTAL_MIN bytes. Otherwise, returns a
253 * positive errno value. Returns EAGAIN immediately if no packet is ready to
257 netdev_recv(struct netdev *netdev, struct ofpbuf *buffer)
261 assert(buffer->size == 0);
262 assert(ofpbuf_tailroom(buffer) >= ETH_TOTAL_MIN);
264 retval = netdev->class->recv(netdev,
265 buffer->data, ofpbuf_tailroom(buffer));
267 COVERAGE_INC(netdev_received);
268 buffer->size += retval;
269 if (buffer->size < ETH_TOTAL_MIN) {
270 ofpbuf_put_zeros(buffer, ETH_TOTAL_MIN - buffer->size);
278 /* Registers with the poll loop to wake up from the next call to poll_block()
279 * when a packet is ready to be received with netdev_recv() on 'netdev'. */
281 netdev_recv_wait(struct netdev *netdev)
283 netdev->class->recv_wait(netdev);
286 /* Discards all packets waiting to be received from 'netdev'. */
288 netdev_drain(struct netdev *netdev)
290 return netdev->class->drain(netdev);
293 /* Sends 'buffer' on 'netdev'. Returns 0 if successful, otherwise a positive
294 * errno value. Returns EAGAIN without blocking if the packet cannot be queued
295 * immediately. Returns EMSGSIZE if a partial packet was transmitted or if
296 * the packet is too big or too small to transmit on the device.
298 * The caller retains ownership of 'buffer' in all cases.
300 * The kernel maintains a packet transmission queue, so the caller is not
301 * expected to do additional queuing of packets. */
303 netdev_send(struct netdev *netdev, const struct ofpbuf *buffer)
305 int error = netdev->class->send(netdev, buffer->data, buffer->size);
307 COVERAGE_INC(netdev_sent);
312 /* Registers with the poll loop to wake up from the next call to poll_block()
313 * when the packet transmission queue has sufficient room to transmit a packet
314 * with netdev_send().
316 * The kernel maintains a packet transmission queue, so the client is not
317 * expected to do additional queuing of packets. Thus, this function is
318 * unlikely to ever be used. It is included for completeness. */
320 netdev_send_wait(struct netdev *netdev)
322 return netdev->class->send_wait(netdev);
325 /* Attempts to set 'netdev''s MAC address to 'mac'. Returns 0 if successful,
326 * otherwise a positive errno value. */
328 netdev_set_etheraddr(struct netdev *netdev, const uint8_t mac[ETH_ADDR_LEN])
330 return netdev->class->set_etheraddr(netdev, mac);
333 /* Retrieves 'netdev''s MAC address. If successful, returns 0 and copies the
334 * the MAC address into 'mac'. On failure, returns a positive errno value and
335 * clears 'mac' to all-zeros. */
337 netdev_get_etheraddr(const struct netdev *netdev, uint8_t mac[ETH_ADDR_LEN])
339 return netdev->class->get_etheraddr(netdev, mac);
342 /* Returns the name of the network device that 'netdev' represents,
343 * e.g. "eth0". The caller must not modify or free the returned string. */
345 netdev_get_name(const struct netdev *netdev)
350 /* Retrieves the MTU of 'netdev'. The MTU is the maximum size of transmitted
351 * (and received) packets, in bytes, not including the hardware header; thus,
352 * this is typically 1500 bytes for Ethernet devices.
354 * If successful, returns 0 and stores the MTU size in '*mtup'. On failure,
355 * returns a positive errno value and stores ETH_PAYLOAD_MAX (1500) in
358 netdev_get_mtu(const struct netdev *netdev, int *mtup)
360 int error = netdev->class->get_mtu(netdev, mtup);
362 VLOG_WARN_RL(&rl, "failed to retrieve MTU for network device %s: %s",
363 netdev_get_name(netdev), strerror(error));
364 *mtup = ETH_PAYLOAD_MAX;
369 /* Stores the features supported by 'netdev' into each of '*current',
370 * '*advertised', '*supported', and '*peer' that are non-null. Each value is a
371 * bitmap of "enum ofp_port_features" bits, in host byte order. Returns 0 if
372 * successful, otherwise a positive errno value. On failure, all of the
373 * passed-in values are set to 0. */
375 netdev_get_features(struct netdev *netdev,
376 uint32_t *current, uint32_t *advertised,
377 uint32_t *supported, uint32_t *peer)
380 return netdev->class->get_features(netdev,
381 current ? current : &dummy[0],
382 advertised ? advertised : &dummy[1],
383 supported ? supported : &dummy[2],
384 peer ? peer : &dummy[3]);
387 /* Set the features advertised by 'netdev' to 'advertise'. Returns 0 if
388 * successful, otherwise a positive errno value. */
390 netdev_set_advertisements(struct netdev *netdev, uint32_t advertise)
392 return (netdev->class->set_advertisements
393 ? netdev->class->set_advertisements(netdev, advertise)
397 /* If 'netdev' has an assigned IPv4 address, sets '*address' to that address
398 * and '*netmask' to its netmask and returns 0. Otherwise, returns a positive
399 * errno value and sets '*address' to 0 (INADDR_ANY).
401 * The following error values have well-defined meanings:
403 * - EADDRNOTAVAIL: 'netdev' has no assigned IPv4 address.
405 * - EOPNOTSUPP: No IPv4 network stack attached to 'netdev'.
407 * 'address' or 'netmask' or both may be null, in which case the address or netmask
408 * is not reported. */
410 netdev_get_in4(const struct netdev *netdev,
411 struct in_addr *address_, struct in_addr *netmask_)
413 struct in_addr address;
414 struct in_addr netmask;
417 error = (netdev->class->get_in4
418 ? netdev->class->get_in4(netdev, &address, &netmask)
421 address_->s_addr = error ? 0 : address.s_addr;
424 netmask_->s_addr = error ? 0 : netmask.s_addr;
429 /* Assigns 'addr' as 'netdev''s IPv4 address and 'mask' as its netmask. If
430 * 'addr' is INADDR_ANY, 'netdev''s IPv4 address is cleared. Returns a
431 * positive errno value. */
433 netdev_set_in4(struct netdev *netdev, struct in_addr addr, struct in_addr mask)
435 return (netdev->class->set_in4
436 ? netdev->class->set_in4(netdev, addr, mask)
440 /* Adds 'router' as a default IP gateway for the TCP/IP stack that corresponds
443 netdev_add_router(struct netdev *netdev, struct in_addr router)
445 COVERAGE_INC(netdev_add_router);
446 return (netdev->class->add_router
447 ? netdev->class->add_router(netdev, router)
451 /* Looks up the next hop for 'host' for the TCP/IP stack that corresponds to
452 * 'netdev'. If a route cannot not be determined, sets '*next_hop' to 0,
453 * '*netdev_name' to null, and returns a positive errno value. Otherwise, if a
454 * next hop is found, stores the next hop gateway's address (0 if 'host' is on
455 * a directly connected network) in '*next_hop' and a copy of the name of the
456 * device to reach 'host' in '*netdev_name', and returns 0. The caller is
457 * responsible for freeing '*netdev_name' (by calling free()). */
459 netdev_get_next_hop(const struct netdev *netdev,
460 const struct in_addr *host, struct in_addr *next_hop,
463 int error = (netdev->class->get_next_hop
464 ? netdev->class->get_next_hop(host, next_hop, netdev_name)
467 next_hop->s_addr = 0;
473 /* If 'netdev' has an assigned IPv6 address, sets '*in6' to that address and
474 * returns 0. Otherwise, returns a positive errno value and sets '*in6' to
475 * all-zero-bits (in6addr_any).
477 * The following error values have well-defined meanings:
479 * - EADDRNOTAVAIL: 'netdev' has no assigned IPv6 address.
481 * - EOPNOTSUPP: No IPv6 network stack attached to 'netdev'.
483 * 'in6' may be null, in which case the address itself is not reported. */
485 netdev_get_in6(const struct netdev *netdev, struct in6_addr *in6)
487 struct in6_addr dummy;
490 error = (netdev->class->get_in6
491 ? netdev->class->get_in6(netdev, in6 ? in6 : &dummy)
494 memset(in6, 0, sizeof *in6);
499 /* On 'netdev', turns off the flags in 'off' and then turns on the flags in
500 * 'on'. If 'permanent' is true, the changes will persist; otherwise, they
501 * will be reverted when 'netdev' is closed or the program exits. Returns 0 if
502 * successful, otherwise a positive errno value. */
504 do_update_flags(struct netdev *netdev, enum netdev_flags off,
505 enum netdev_flags on, enum netdev_flags *old_flagsp,
508 enum netdev_flags old_flags;
511 error = netdev->class->update_flags(netdev, off & ~on, on, &old_flags);
513 VLOG_WARN_RL(&rl, "failed to %s flags for network device %s: %s",
514 off || on ? "set" : "get", netdev_get_name(netdev),
517 } else if ((off || on) && !permanent) {
518 enum netdev_flags new_flags = (old_flags & ~off) | on;
519 enum netdev_flags changed_flags = old_flags ^ new_flags;
521 if (!netdev->changed_flags) {
522 netdev->save_flags = old_flags;
524 netdev->changed_flags |= changed_flags;
528 *old_flagsp = old_flags;
533 /* Obtains the current flags for 'netdev' and stores them into '*flagsp'.
534 * Returns 0 if successful, otherwise a positive errno value. On failure,
535 * stores 0 into '*flagsp'. */
537 netdev_get_flags(const struct netdev *netdev_, enum netdev_flags *flagsp)
539 struct netdev *netdev = (struct netdev *) netdev_;
540 return do_update_flags(netdev, 0, 0, flagsp, false);
543 /* Sets the flags for 'netdev' to 'flags'.
544 * If 'permanent' is true, the changes will persist; otherwise, they
545 * will be reverted when 'netdev' is closed or the program exits.
546 * Returns 0 if successful, otherwise a positive errno value. */
548 netdev_set_flags(struct netdev *netdev, enum netdev_flags flags,
551 return do_update_flags(netdev, -1, flags, NULL, permanent);
554 /* Turns on the specified 'flags' on 'netdev'.
555 * If 'permanent' is true, the changes will persist; otherwise, they
556 * will be reverted when 'netdev' is closed or the program exits.
557 * Returns 0 if successful, otherwise a positive errno value. */
559 netdev_turn_flags_on(struct netdev *netdev, enum netdev_flags flags,
562 return do_update_flags(netdev, 0, flags, NULL, permanent);
565 /* Turns off the specified 'flags' on 'netdev'.
566 * If 'permanent' is true, the changes will persist; otherwise, they
567 * will be reverted when 'netdev' is closed or the program exits.
568 * Returns 0 if successful, otherwise a positive errno value. */
570 netdev_turn_flags_off(struct netdev *netdev, enum netdev_flags flags,
573 return do_update_flags(netdev, flags, 0, NULL, permanent);
576 /* Looks up the ARP table entry for 'ip' on 'netdev'. If one exists and can be
577 * successfully retrieved, it stores the corresponding MAC address in 'mac' and
578 * returns 0. Otherwise, it returns a positive errno value; in particular,
579 * ENXIO indicates that there is no ARP table entry for 'ip' on 'netdev'. */
581 netdev_arp_lookup(const struct netdev *netdev,
582 uint32_t ip, uint8_t mac[ETH_ADDR_LEN])
584 int error = (netdev->class->arp_lookup
585 ? netdev->class->arp_lookup(netdev, ip, mac)
588 memset(mac, 0, ETH_ADDR_LEN);
593 /* Sets 'carrier' to true if carrier is active (link light is on) on
596 netdev_get_carrier(const struct netdev *netdev, bool *carrier)
598 int error = (netdev->class->get_carrier
599 ? netdev->class->get_carrier(netdev, carrier)
607 /* Retrieves current device stats for 'netdev'. */
609 netdev_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
613 COVERAGE_INC(netdev_get_stats);
614 error = (netdev->class->get_stats
615 ? netdev->class->get_stats(netdev, stats)
618 memset(stats, 0xff, sizeof *stats);
623 /* Attempts to set input rate limiting (policing) policy, such that up to
624 * 'kbits_rate' kbps of traffic is accepted, with a maximum accumulative burst
625 * size of 'kbits' kb. */
627 netdev_set_policing(struct netdev *netdev, uint32_t kbits_rate,
628 uint32_t kbits_burst)
630 return (netdev->class->set_policing
631 ? netdev->class->set_policing(netdev, kbits_rate, kbits_burst)
635 /* If 'netdev' is a VLAN network device (e.g. one created with vconfig(8)),
636 * sets '*vlan_vid' to the VLAN VID associated with that device and returns 0.
637 * Otherwise returns a errno value (specifically ENOENT if 'netdev_name' is the
638 * name of a network device that is not a VLAN device) and sets '*vlan_vid' to
641 netdev_get_vlan_vid(const struct netdev *netdev, int *vlan_vid)
643 int error = (netdev->class->get_vlan_vid
644 ? netdev->class->get_vlan_vid(netdev, vlan_vid)
652 /* Returns a network device that has 'in4' as its IP address, if one exists,
653 * otherwise a null pointer. */
655 netdev_find_dev_by_in4(const struct in_addr *in4)
657 struct netdev *netdev;
658 struct svec dev_list;
661 netdev_enumerate(&dev_list);
662 for (i = 0; i < dev_list.n; i++) {
663 const char *name = dev_list.names[i];
664 struct in_addr dev_in4;
666 if (!netdev_open(name, NETDEV_ETH_TYPE_NONE, &netdev)
667 && !netdev_get_in4(netdev, &dev_in4, NULL)
668 && dev_in4.s_addr == in4->s_addr) {
671 netdev_close(netdev);
676 svec_destroy(&dev_list);
680 /* Initializes 'netdev' as a netdev named 'name' of the specified 'class'.
682 * This function adds 'netdev' to a netdev-owned linked list, so it is very
683 * important that 'netdev' only be freed after calling netdev_close(). */
685 netdev_init(struct netdev *netdev, const char *name,
686 const struct netdev_class *class)
688 netdev->class = class;
689 netdev->name = xstrdup(name);
690 netdev->save_flags = 0;
691 netdev->changed_flags = 0;
692 list_push_back(&netdev_list, &netdev->node);
695 /* Initializes 'notifier' as a netdev notifier for 'netdev', for which
696 * notification will consist of calling 'cb', with auxiliary data 'aux'. */
698 netdev_notifier_init(struct netdev_notifier *notifier, struct netdev *netdev,
699 void (*cb)(struct netdev_notifier *), void *aux)
701 notifier->netdev = netdev;
706 /* Tracks changes in the status of a set of network devices. */
707 struct netdev_monitor {
708 struct shash polled_netdevs;
709 struct shash changed_netdevs;
712 /* Creates and returns a new structure for monitor changes in the status of
713 * network devices. */
714 struct netdev_monitor *
715 netdev_monitor_create(void)
717 struct netdev_monitor *monitor = xmalloc(sizeof *monitor);
718 shash_init(&monitor->polled_netdevs);
719 shash_init(&monitor->changed_netdevs);
723 /* Destroys 'monitor'. */
725 netdev_monitor_destroy(struct netdev_monitor *monitor)
728 struct shash_node *node;
730 SHASH_FOR_EACH (node, &monitor->polled_netdevs) {
731 struct netdev_notifier *notifier = node->data;
732 notifier->netdev->class->poll_remove(notifier);
735 shash_destroy(&monitor->polled_netdevs);
736 shash_destroy(&monitor->changed_netdevs);
742 netdev_monitor_cb(struct netdev_notifier *notifier)
744 struct netdev_monitor *monitor = notifier->aux;
745 const char *name = netdev_get_name(notifier->netdev);
746 if (!shash_find(&monitor->changed_netdevs, name)) {
747 shash_add(&monitor->changed_netdevs, name, NULL);
751 /* Attempts to add 'netdev' as a netdev monitored by 'monitor'. Returns 0 if
752 * successful, otherwise a positive errno value.
754 * Adding a given 'netdev' to a monitor multiple times is equivalent to adding
757 netdev_monitor_add(struct netdev_monitor *monitor, struct netdev *netdev)
759 const char *netdev_name = netdev_get_name(netdev);
761 if (!shash_find(&monitor->polled_netdevs, netdev_name)
762 && netdev->class->poll_add)
764 struct netdev_notifier *notifier;
765 error = netdev->class->poll_add(netdev, netdev_monitor_cb, monitor,
768 assert(notifier->netdev == netdev);
769 shash_add(&monitor->polled_netdevs, netdev_name, notifier);
775 /* Removes 'netdev' from the set of netdevs monitored by 'monitor'. (This has
776 * no effect if 'netdev' is not in the set of devices monitored by
779 netdev_monitor_remove(struct netdev_monitor *monitor, struct netdev *netdev)
781 const char *netdev_name = netdev_get_name(netdev);
782 struct shash_node *node;
784 node = shash_find(&monitor->polled_netdevs, netdev_name);
786 /* Cancel future notifications. */
787 struct netdev_notifier *notifier = node->data;
788 netdev->class->poll_remove(notifier);
789 shash_delete(&monitor->polled_netdevs, node);
791 /* Drop any pending notification. */
792 node = shash_find(&monitor->changed_netdevs, netdev_name);
794 shash_delete(&monitor->changed_netdevs, node);
799 /* Checks for changes to netdevs in the set monitored by 'monitor'. If any of
800 * the attributes (Ethernet address, carrier status, speed or peer-advertised
801 * speed, flags, etc.) of a network device monitored by 'monitor' has changed,
802 * sets '*devnamep' to the name of a device that has changed and returns 0.
803 * The caller is responsible for freeing '*devnamep' (with free()).
805 * If no devices have changed, sets '*devnamep' to NULL and returns EAGAIN.
808 netdev_monitor_poll(struct netdev_monitor *monitor, char **devnamep)
810 struct shash_node *node = shash_first(&monitor->changed_netdevs);
815 *devnamep = xstrdup(node->name);
816 shash_delete(&monitor->changed_netdevs, node);
821 /* Registers with the poll loop to wake up from the next call to poll_block()
822 * when netdev_monitor_poll(monitor) would indicate that a device has
825 netdev_monitor_poll_wait(const struct netdev_monitor *monitor)
827 if (!shash_is_empty(&monitor->changed_netdevs)) {
828 poll_immediate_wake();
830 /* XXX Nothing needed here for netdev_linux, but maybe other netdev
831 * classes need help. */
835 /* Restore the network device flags on 'netdev' to those that were active
836 * before we changed them. Returns 0 if successful, otherwise a positive
839 * To avoid reentry, the caller must ensure that fatal signals are blocked. */
841 restore_flags(struct netdev *netdev)
843 if (netdev->changed_flags) {
844 enum netdev_flags restore = netdev->save_flags & netdev->changed_flags;
845 enum netdev_flags old_flags;
846 return netdev->class->update_flags(netdev,
847 netdev->changed_flags & ~restore,
848 restore, &old_flags);
853 /* Retores all the flags on all network devices that we modified. Called from
854 * a signal handler, so it does not attempt to report error conditions. */
856 restore_all_flags(void *aux UNUSED)
858 struct netdev *netdev;
859 LIST_FOR_EACH (netdev, struct netdev, node, &netdev_list) {
860 restore_flags(netdev);