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));
93 /* Performs periodic work needed by all the various kinds of netdevs.
95 * If your program opens any netdevs, it must call this function within its
101 for (i = 0; i < n_netdev_classes; i++) {
102 const struct netdev_class *class = netdev_classes[i];
109 /* Arranges for poll_block() to wake up when netdev_run() needs to be called.
111 * If your program opens any netdevs, it must call this function within its
117 for (i = 0; i < n_netdev_classes; i++) {
118 const struct netdev_class *class = netdev_classes[i];
125 /* Opens the network device named 'name' (e.g. "eth0") and returns zero if
126 * successful, otherwise a positive errno value. On success, sets '*netdevp'
127 * to the new network device, otherwise to null.
129 * 'ethertype' may be a 16-bit Ethernet protocol value in host byte order to
130 * capture frames of that type received on the device. It may also be one of
131 * the 'enum netdev_pseudo_ethertype' values to receive frames in one of those
134 netdev_open(const char *name_, int ethertype, struct netdev **netdevp)
136 char *name = xstrdup(name_);
137 char *prefix, *suffix, *colon;
138 struct netdev *netdev = NULL;
143 colon = strchr(name, ':');
153 for (i = 0; i < n_netdev_classes; i++) {
154 const struct netdev_class *class = netdev_classes[i];
155 if (!strcmp(prefix, class->prefix)) {
156 error = class->open(name_, suffix, ethertype, &netdev);
160 error = EAFNOSUPPORT;
163 *netdevp = error ? NULL : netdev;
167 /* Closes and destroys 'netdev'. */
169 netdev_close(struct netdev *netdev)
175 /* Restore flags that we changed, if any. */
176 fatal_signal_block();
177 error = restore_flags(netdev);
178 list_remove(&netdev->node);
179 fatal_signal_unblock();
181 VLOG_WARN("failed to restore network device flags on %s: %s",
182 netdev->name, strerror(error));
187 netdev->class->close(netdev);
192 /* Returns true if a network device named 'name' exists and may be opened,
193 * otherwise false. */
195 netdev_exists(const char *name)
197 struct netdev *netdev;
200 error = netdev_open(name, NETDEV_ETH_TYPE_NONE, &netdev);
202 netdev_close(netdev);
205 if (error != ENODEV) {
206 VLOG_WARN("failed to open network device %s: %s",
207 name, strerror(error));
213 /* Initializes 'svec' with a list of the names of all known network devices. */
215 netdev_enumerate(struct svec *svec)
225 for (i = 0; i < n_netdev_classes; i++) {
226 const struct netdev_class *class = netdev_classes[i];
227 if (class->enumerate) {
228 int retval = class->enumerate(svec);
230 VLOG_WARN("failed to enumerate %s network devices: %s",
231 class->name, strerror(retval));
241 /* Attempts to receive a packet from 'netdev' into 'buffer', which the caller
242 * must have initialized with sufficient room for the packet. The space
243 * required to receive any packet is ETH_HEADER_LEN bytes, plus VLAN_HEADER_LEN
244 * bytes, plus the device's MTU (which may be retrieved via netdev_get_mtu()).
245 * (Some devices do not allow for a VLAN header, in which case VLAN_HEADER_LEN
246 * need not be included.)
248 * If a packet is successfully retrieved, returns 0. In this case 'buffer' is
249 * guaranteed to contain at least ETH_TOTAL_MIN bytes. Otherwise, returns a
250 * positive errno value. Returns EAGAIN immediately if no packet is ready to
254 netdev_recv(struct netdev *netdev, struct ofpbuf *buffer)
258 assert(buffer->size == 0);
259 assert(ofpbuf_tailroom(buffer) >= ETH_TOTAL_MIN);
261 retval = netdev->class->recv(netdev,
262 buffer->data, ofpbuf_tailroom(buffer));
264 COVERAGE_INC(netdev_received);
265 buffer->size += retval;
266 if (buffer->size < ETH_TOTAL_MIN) {
267 ofpbuf_put_zeros(buffer, ETH_TOTAL_MIN - buffer->size);
275 /* Registers with the poll loop to wake up from the next call to poll_block()
276 * when a packet is ready to be received with netdev_recv() on 'netdev'. */
278 netdev_recv_wait(struct netdev *netdev)
280 netdev->class->recv_wait(netdev);
283 /* Discards all packets waiting to be received from 'netdev'. */
285 netdev_drain(struct netdev *netdev)
287 return netdev->class->drain(netdev);
290 /* Sends 'buffer' on 'netdev'. Returns 0 if successful, otherwise a positive
291 * errno value. Returns EAGAIN without blocking if the packet cannot be queued
292 * immediately. Returns EMSGSIZE if a partial packet was transmitted or if
293 * the packet is too big or too small to transmit on the device.
295 * The caller retains ownership of 'buffer' in all cases.
297 * The kernel maintains a packet transmission queue, so the caller is not
298 * expected to do additional queuing of packets. */
300 netdev_send(struct netdev *netdev, const struct ofpbuf *buffer)
302 int error = netdev->class->send(netdev, buffer->data, buffer->size);
304 COVERAGE_INC(netdev_sent);
309 /* Registers with the poll loop to wake up from the next call to poll_block()
310 * when the packet transmission queue has sufficient room to transmit a packet
311 * with netdev_send().
313 * The kernel maintains a packet transmission queue, so the client is not
314 * expected to do additional queuing of packets. Thus, this function is
315 * unlikely to ever be used. It is included for completeness. */
317 netdev_send_wait(struct netdev *netdev)
319 return netdev->class->send_wait(netdev);
322 /* Attempts to set 'netdev''s MAC address to 'mac'. Returns 0 if successful,
323 * otherwise a positive errno value. */
325 netdev_set_etheraddr(struct netdev *netdev, const uint8_t mac[ETH_ADDR_LEN])
327 return netdev->class->set_etheraddr(netdev, mac);
330 /* Retrieves 'netdev''s MAC address. If successful, returns 0 and copies the
331 * the MAC address into 'mac'. On failure, returns a positive errno value and
332 * clears 'mac' to all-zeros. */
334 netdev_get_etheraddr(const struct netdev *netdev, uint8_t mac[ETH_ADDR_LEN])
336 return netdev->class->get_etheraddr(netdev, mac);
339 /* Returns the name of the network device that 'netdev' represents,
340 * e.g. "eth0". The caller must not modify or free the returned string. */
342 netdev_get_name(const struct netdev *netdev)
347 /* Retrieves the MTU of 'netdev'. The MTU is the maximum size of transmitted
348 * (and received) packets, in bytes, not including the hardware header; thus,
349 * this is typically 1500 bytes for Ethernet devices.
351 * If successful, returns 0 and stores the MTU size in '*mtup'. On failure,
352 * returns a positive errno value and stores ETH_PAYLOAD_MAX (1500) in
355 netdev_get_mtu(const struct netdev *netdev, int *mtup)
357 int error = netdev->class->get_mtu(netdev, mtup);
359 VLOG_WARN_RL(&rl, "failed to retrieve MTU for network device %s: %s",
360 netdev_get_name(netdev), strerror(error));
361 *mtup = ETH_PAYLOAD_MAX;
366 /* Stores the features supported by 'netdev' into each of '*current',
367 * '*advertised', '*supported', and '*peer' that are non-null. Each value is a
368 * bitmap of "enum ofp_port_features" bits, in host byte order. Returns 0 if
369 * successful, otherwise a positive errno value. On failure, all of the
370 * passed-in values are set to 0. */
372 netdev_get_features(struct netdev *netdev,
373 uint32_t *current, uint32_t *advertised,
374 uint32_t *supported, uint32_t *peer)
377 return netdev->class->get_features(netdev,
378 current ? current : &dummy[0],
379 advertised ? advertised : &dummy[1],
380 supported ? supported : &dummy[2],
381 peer ? peer : &dummy[3]);
384 /* Set the features advertised by 'netdev' to 'advertise'. Returns 0 if
385 * successful, otherwise a positive errno value. */
387 netdev_set_advertisements(struct netdev *netdev, uint32_t advertise)
389 return (netdev->class->set_advertisements
390 ? netdev->class->set_advertisements(netdev, advertise)
394 /* If 'netdev' has an assigned IPv4 address, sets '*in4' to that address and
395 * returns 0. Otherwise, returns a positive errno value and sets '*in4' to 0
398 * The following error values have well-defined meanings:
400 * - EADDRNOTAVAIL: 'netdev' has no assigned IPv4 address.
402 * - EOPNOTSUPP: No IPv4 network stack attached to 'netdev'.
404 * 'in4' may be null, in which case the address itself is not reported. */
406 netdev_get_in4(const struct netdev *netdev, struct in_addr *in4)
408 struct in_addr dummy;
411 error = (netdev->class->get_in4
412 ? netdev->class->get_in4(netdev, in4 ? in4 : &dummy)
420 /* Assigns 'addr' as 'netdev''s IPv4 address and 'mask' as its netmask. If
421 * 'addr' is INADDR_ANY, 'netdev''s IPv4 address is cleared. Returns a
422 * positive errno value. */
424 netdev_set_in4(struct netdev *netdev, struct in_addr addr, struct in_addr mask)
426 return (netdev->class->set_in4
427 ? netdev->class->set_in4(netdev, addr, mask)
431 /* Adds 'router' as a default IP gateway for the TCP/IP stack that corresponds
434 netdev_add_router(struct netdev *netdev, struct in_addr router)
436 COVERAGE_INC(netdev_add_router);
437 return (netdev->class->add_router
438 ? netdev->class->add_router(netdev, router)
442 /* If 'netdev' has an assigned IPv6 address, sets '*in6' to that address and
443 * returns 0. Otherwise, returns a positive errno value and sets '*in6' to
444 * all-zero-bits (in6addr_any).
446 * The following error values have well-defined meanings:
448 * - EADDRNOTAVAIL: 'netdev' has no assigned IPv6 address.
450 * - EOPNOTSUPP: No IPv6 network stack attached to 'netdev'.
452 * 'in6' may be null, in which case the address itself is not reported. */
454 netdev_get_in6(const struct netdev *netdev, struct in6_addr *in6)
456 struct in6_addr dummy;
459 error = (netdev->class->get_in6
460 ? netdev->class->get_in6(netdev, in6 ? in6 : &dummy)
463 memset(in6, 0, sizeof *in6);
468 /* On 'netdev', turns off the flags in 'off' and then turns on the flags in
469 * 'on'. If 'permanent' is true, the changes will persist; otherwise, they
470 * will be reverted when 'netdev' is closed or the program exits. Returns 0 if
471 * successful, otherwise a positive errno value. */
473 do_update_flags(struct netdev *netdev, enum netdev_flags off,
474 enum netdev_flags on, enum netdev_flags *old_flagsp,
477 enum netdev_flags old_flags;
480 error = netdev->class->update_flags(netdev, off & ~on, on, &old_flags);
482 VLOG_WARN_RL(&rl, "failed to %s flags for network device %s: %s",
483 off || on ? "set" : "get", netdev_get_name(netdev),
486 } else if ((off || on) && !permanent) {
487 enum netdev_flags new_flags = (old_flags & ~off) | on;
488 enum netdev_flags changed_flags = old_flags ^ new_flags;
490 if (!netdev->changed_flags) {
491 netdev->save_flags = old_flags;
493 netdev->changed_flags |= changed_flags;
497 *old_flagsp = old_flags;
502 /* Obtains the current flags for 'netdev' and stores them into '*flagsp'.
503 * Returns 0 if successful, otherwise a positive errno value. On failure,
504 * stores 0 into '*flagsp'. */
506 netdev_get_flags(const struct netdev *netdev_, enum netdev_flags *flagsp)
508 struct netdev *netdev = (struct netdev *) netdev_;
509 return do_update_flags(netdev, 0, 0, flagsp, false);
512 /* Sets the flags for 'netdev' to 'flags'.
513 * If 'permanent' is true, the changes will persist; otherwise, they
514 * will be reverted when 'netdev' is closed or the program exits.
515 * Returns 0 if successful, otherwise a positive errno value. */
517 netdev_set_flags(struct netdev *netdev, enum netdev_flags flags,
520 return do_update_flags(netdev, -1, flags, NULL, permanent);
523 /* Turns on the specified 'flags' on 'netdev'.
524 * If 'permanent' is true, the changes will persist; otherwise, they
525 * will be reverted when 'netdev' is closed or the program exits.
526 * Returns 0 if successful, otherwise a positive errno value. */
528 netdev_turn_flags_on(struct netdev *netdev, enum netdev_flags flags,
531 return do_update_flags(netdev, 0, flags, NULL, permanent);
534 /* Turns off the specified 'flags' on 'netdev'.
535 * If 'permanent' is true, the changes will persist; otherwise, they
536 * will be reverted when 'netdev' is closed or the program exits.
537 * Returns 0 if successful, otherwise a positive errno value. */
539 netdev_turn_flags_off(struct netdev *netdev, enum netdev_flags flags,
542 return do_update_flags(netdev, flags, 0, NULL, permanent);
545 /* Looks up the ARP table entry for 'ip' on 'netdev'. If one exists and can be
546 * successfully retrieved, it stores the corresponding MAC address in 'mac' and
547 * returns 0. Otherwise, it returns a positive errno value; in particular,
548 * ENXIO indicates that there is no ARP table entry for 'ip' on 'netdev'. */
550 netdev_arp_lookup(const struct netdev *netdev,
551 uint32_t ip, uint8_t mac[ETH_ADDR_LEN])
553 int error = (netdev->class->arp_lookup
554 ? netdev->class->arp_lookup(netdev, ip, mac)
557 memset(mac, 0, ETH_ADDR_LEN);
562 /* Sets 'carrier' to true if carrier is active (link light is on) on
565 netdev_get_carrier(const struct netdev *netdev, bool *carrier)
567 int error = (netdev->class->get_carrier
568 ? netdev->class->get_carrier(netdev, carrier)
576 /* Retrieves current device stats for 'netdev'. */
578 netdev_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
582 COVERAGE_INC(netdev_get_stats);
583 error = (netdev->class->get_stats
584 ? netdev->class->get_stats(netdev, stats)
587 memset(stats, 0xff, sizeof *stats);
592 /* Attempts to set input rate limiting (policing) policy, such that up to
593 * 'kbits_rate' kbps of traffic is accepted, with a maximum accumulative burst
594 * size of 'kbits' kb. */
596 netdev_set_policing(struct netdev *netdev, uint32_t kbits_rate,
597 uint32_t kbits_burst)
599 return (netdev->class->set_policing
600 ? netdev->class->set_policing(netdev, kbits_rate, kbits_burst)
604 /* If 'netdev' is a VLAN network device (e.g. one created with vconfig(8)),
605 * sets '*vlan_vid' to the VLAN VID associated with that device and returns 0.
606 * Otherwise returns a errno value (specifically ENOENT if 'netdev_name' is the
607 * name of a network device that is not a VLAN device) and sets '*vlan_vid' to
610 netdev_get_vlan_vid(const struct netdev *netdev, int *vlan_vid)
612 int error = (netdev->class->get_vlan_vid
613 ? netdev->class->get_vlan_vid(netdev, vlan_vid)
621 /* Returns a network device that has 'in4' as its IP address, if one exists,
622 * otherwise a null pointer. */
624 netdev_find_dev_by_in4(const struct in_addr *in4)
626 struct netdev *netdev;
627 struct svec dev_list;
630 netdev_enumerate(&dev_list);
631 for (i = 0; i < dev_list.n; i++) {
632 const char *name = dev_list.names[i];
633 struct in_addr dev_in4;
635 if (!netdev_open(name, NETDEV_ETH_TYPE_NONE, &netdev)
636 && !netdev_get_in4(netdev, &dev_in4)
637 && dev_in4.s_addr == in4->s_addr) {
640 netdev_close(netdev);
645 svec_destroy(&dev_list);
649 /* Initializes 'netdev' as a netdev named 'name' of the specified 'class'.
651 * This function adds 'netdev' to a netdev-owned linked list, so it is very
652 * important that 'netdev' only be freed after calling netdev_close(). */
654 netdev_init(struct netdev *netdev, const char *name,
655 const struct netdev_class *class)
657 netdev->class = class;
658 netdev->name = xstrdup(name);
659 netdev->save_flags = 0;
660 netdev->changed_flags = 0;
661 list_push_back(&netdev_list, &netdev->node);
664 /* Initializes 'notifier' as a netdev notifier for 'netdev', for which
665 * notification will consist of calling 'cb', with auxiliary data 'aux'. */
667 netdev_notifier_init(struct netdev_notifier *notifier, struct netdev *netdev,
668 void (*cb)(struct netdev_notifier *), void *aux)
670 notifier->netdev = netdev;
675 /* Tracks changes in the status of a set of network devices. */
676 struct netdev_monitor {
677 struct shash polled_netdevs;
678 struct shash changed_netdevs;
681 /* Creates and returns a new structure for monitor changes in the status of
682 * network devices. */
683 struct netdev_monitor *
684 netdev_monitor_create(void)
686 struct netdev_monitor *monitor = xmalloc(sizeof *monitor);
687 shash_init(&monitor->polled_netdevs);
688 shash_init(&monitor->changed_netdevs);
692 /* Destroys 'monitor'. */
694 netdev_monitor_destroy(struct netdev_monitor *monitor)
697 struct shash_node *node;
699 SHASH_FOR_EACH (node, &monitor->polled_netdevs) {
700 struct netdev_notifier *notifier = node->data;
701 notifier->netdev->class->poll_remove(notifier);
704 shash_destroy(&monitor->polled_netdevs);
705 shash_destroy(&monitor->changed_netdevs);
711 netdev_monitor_cb(struct netdev_notifier *notifier)
713 struct netdev_monitor *monitor = notifier->aux;
714 const char *name = netdev_get_name(notifier->netdev);
715 if (!shash_find(&monitor->changed_netdevs, name)) {
716 shash_add(&monitor->changed_netdevs, name, NULL);
720 /* Attempts to add 'netdev' as a netdev monitored by 'monitor'. Returns 0 if
721 * successful, otherwise a positive errno value.
723 * Adding a given 'netdev' to a monitor multiple times is equivalent to adding
726 netdev_monitor_add(struct netdev_monitor *monitor, struct netdev *netdev)
728 const char *netdev_name = netdev_get_name(netdev);
730 if (!shash_find(&monitor->polled_netdevs, netdev_name)
731 && netdev->class->poll_add)
733 struct netdev_notifier *notifier;
734 error = netdev->class->poll_add(netdev, netdev_monitor_cb, monitor,
737 assert(notifier->netdev == netdev);
738 shash_add(&monitor->polled_netdevs, netdev_name, notifier);
744 /* Removes 'netdev' from the set of netdevs monitored by 'monitor'. (This has
745 * no effect if 'netdev' is not in the set of devices monitored by
748 netdev_monitor_remove(struct netdev_monitor *monitor, struct netdev *netdev)
750 const char *netdev_name = netdev_get_name(netdev);
751 struct shash_node *node;
753 node = shash_find(&monitor->polled_netdevs, netdev_name);
755 /* Cancel future notifications. */
756 struct netdev_notifier *notifier = node->data;
757 netdev->class->poll_remove(notifier);
758 shash_delete(&monitor->polled_netdevs, node);
760 /* Drop any pending notification. */
761 node = shash_find(&monitor->changed_netdevs, netdev_name);
763 shash_delete(&monitor->changed_netdevs, node);
768 /* Checks for changes to netdevs in the set monitored by 'monitor'. If any of
769 * the attributes (Ethernet address, carrier status, speed or peer-advertised
770 * speed, flags, etc.) of a network device monitored by 'monitor' has changed,
771 * sets '*devnamep' to the name of a device that has changed and returns 0.
772 * The caller is responsible for freeing '*devnamep' (with free()).
774 * If no devices have changed, sets '*devnamep' to NULL and returns EAGAIN.
777 netdev_monitor_poll(struct netdev_monitor *monitor, char **devnamep)
779 struct shash_node *node = shash_first(&monitor->changed_netdevs);
784 *devnamep = xstrdup(node->name);
785 shash_delete(&monitor->changed_netdevs, node);
790 /* Registers with the poll loop to wake up from the next call to poll_block()
791 * when netdev_monitor_poll(monitor) would indicate that a device has
794 netdev_monitor_poll_wait(const struct netdev_monitor *monitor)
796 if (!shash_is_empty(&monitor->changed_netdevs)) {
797 poll_immediate_wake();
799 /* XXX Nothing needed here for netdev_linux, but maybe other netdev
800 * classes need help. */
804 /* Restore the network device flags on 'netdev' to those that were active
805 * before we changed them. Returns 0 if successful, otherwise a positive
808 * To avoid reentry, the caller must ensure that fatal signals are blocked. */
810 restore_flags(struct netdev *netdev)
812 if (netdev->changed_flags) {
813 enum netdev_flags restore = netdev->save_flags & netdev->changed_flags;
814 enum netdev_flags old_flags;
815 return netdev->class->update_flags(netdev,
816 netdev->changed_flags & ~restore,
817 restore, &old_flags);
822 /* Retores all the flags on all network devices that we modified. Called from
823 * a signal handler, so it does not attempt to report error conditions. */
825 restore_all_flags(void *aux UNUSED)
827 struct netdev *netdev;
828 LIST_FOR_EACH (netdev, struct netdev, node, &netdev_list) {
829 restore_flags(netdev);