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"
32 #include "netdev-provider.h"
34 #include "openflow/openflow.h"
36 #include "poll-loop.h"
40 #define THIS_MODULE VLM_netdev
43 static const struct netdev_class *netdev_classes[] = {
47 static int n_netdev_classes = ARRAY_SIZE(netdev_classes);
49 /* All created network devices. */
50 static struct shash netdev_obj_shash = SHASH_INITIALIZER(&netdev_obj_shash);
52 /* All open network devices. */
53 static struct list netdev_list = LIST_INITIALIZER(&netdev_list);
55 /* This is set pretty low because we probably won't learn anything from the
56 * additional log messages. */
57 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
59 static void restore_all_flags(void *aux);
60 static int restore_flags(struct netdev *netdev);
62 /* Attempts to initialize the netdev module. Returns 0 if successful,
63 * otherwise a positive errno value.
65 * Calling this function is optional. If not called explicitly, it will
66 * automatically be called upon the first attempt to open or create a
69 netdev_initialize(void)
71 static int status = -1;
75 fatal_signal_add_hook(restore_all_flags, NULL, true);
78 for (i = j = 0; i < n_netdev_classes; i++) {
79 const struct netdev_class *class = netdev_classes[i];
81 int retval = class->init();
83 netdev_classes[j++] = class;
85 VLOG_ERR("failed to initialize %s network device "
86 "class: %s", class->type, strerror(retval));
92 netdev_classes[j++] = class;
100 /* Performs periodic work needed by all the various kinds of netdevs.
102 * If your program opens any netdevs, it must call this function within its
108 for (i = 0; i < n_netdev_classes; i++) {
109 const struct netdev_class *class = netdev_classes[i];
116 /* Arranges for poll_block() to wake up when netdev_run() needs to be called.
118 * If your program opens any netdevs, it must call this function within its
124 for (i = 0; i < n_netdev_classes; i++) {
125 const struct netdev_class *class = netdev_classes[i];
132 /* Attempts to create a network device object of 'type' with 'name'. 'type'
133 * corresponds to the 'type' field used in the netdev_class * structure.
134 * Arguments for creation are provided in 'args', which may be empty or NULL
135 * if none are needed. */
137 netdev_create(const char *name, const char *type, const struct shash *args)
139 struct shash empty_args = SHASH_INITIALIZER(&empty_args);
148 if (shash_find(&netdev_obj_shash, name)) {
149 VLOG_WARN("attempted to create a netdev object with bound name: %s",
154 for (i = 0; i < n_netdev_classes; i++) {
155 const struct netdev_class *class = netdev_classes[i];
156 if (!strcmp(type, class->type)) {
157 return class->create(name, type, args, true);
161 VLOG_WARN("could not create netdev object of unknown type: %s", type);
166 /* Destroys netdev object 'name'. Netdev objects maintain a reference count
167 * which is incremented on netdev_open() and decremented on netdev_close().
168 * If 'name' has a non-zero reference count, it will not destroy the object
169 * and return EBUSY. */
171 netdev_destroy(const char *name)
173 struct shash_node *node;
174 struct netdev_obj *netdev_obj;
176 node = shash_find(&netdev_obj_shash, name);
181 netdev_obj = node->data;
182 if (netdev_obj->ref_cnt != 0) {
183 VLOG_WARN("attempt to destroy open netdev object (%d): %s",
184 netdev_obj->ref_cnt, name);
188 shash_delete(&netdev_obj_shash, node);
189 netdev_obj->netdev_class->destroy(netdev_obj);
194 /* Reconfigures the device object 'name' with 'args'. 'args' may be empty
195 * or NULL if none are needed. */
197 netdev_reconfigure(const char *name, const struct shash *args)
199 struct shash empty_args = SHASH_INITIALIZER(&empty_args);
200 struct netdev_obj *netdev_obj;
206 netdev_obj = shash_find_data(&netdev_obj_shash, name);
211 if (netdev_obj->netdev_class->reconfigure) {
212 return netdev_obj->netdev_class->reconfigure(netdev_obj, args);
218 /* Opens the network device named 'name' (e.g. "eth0") and returns zero if
219 * successful, otherwise a positive errno value. On success, sets '*netdevp'
220 * to the new network device, otherwise to null.
222 * 'ethertype' may be a 16-bit Ethernet protocol value in host byte order to
223 * capture frames of that type received on the device. It may also be one of
224 * the 'enum netdev_pseudo_ethertype' values to receive frames in one of those
227 netdev_open(const char *name, int ethertype, struct netdev **netdevp)
229 struct netdev_obj *netdev_obj;
230 struct netdev *netdev = NULL;
236 netdev_obj = shash_find_data(&netdev_obj_shash, name);
238 netdev_obj->ref_cnt++;
239 error = netdev_obj->netdev_class->open(name, ethertype, &netdev);
241 /* Default to "system". */
242 error = EAFNOSUPPORT;
243 for (i = 0; i < n_netdev_classes; i++) {
244 const struct netdev_class *class = netdev_classes[i];
245 if (!strcmp(class->type, "system")) {
246 struct shash empty_args = SHASH_INITIALIZER(&empty_args);
248 /* Dynamically create the netdev object, but indicate
249 * that it should be destroyed when the the last user
250 * closes its handle. */
251 error = class->create(name, "system", &empty_args, false);
253 netdev_obj = shash_find_data(&netdev_obj_shash, name);
254 netdev_obj->ref_cnt++;
255 error = class->open(name, ethertype, &netdev);
262 *netdevp = error ? NULL : netdev;
266 /* Closes and destroys 'netdev'. */
268 netdev_close(struct netdev *netdev)
271 struct netdev_obj *netdev_obj;
272 char *name = netdev->name;
275 netdev_obj = shash_find_data(&netdev_obj_shash, name);
277 if (netdev_obj->ref_cnt > 0) {
278 netdev_obj->ref_cnt--;
280 VLOG_WARN("netdev %s closed too many times", name);
283 /* If the reference count for the netdev object is zero, and it
284 * was dynamically created by netdev_open(), destroy it. */
285 if (!netdev_obj->ref_cnt && !netdev_obj->created) {
286 netdev_destroy(name);
289 /* Restore flags that we changed, if any. */
290 fatal_signal_block();
291 error = restore_flags(netdev);
292 list_remove(&netdev->node);
293 fatal_signal_unblock();
295 VLOG_WARN("failed to restore network device flags on %s: %s",
296 name, strerror(error));
300 netdev->netdev_class->close(netdev);
305 /* Returns true if a network device named 'name' exists and may be opened,
306 * otherwise false. */
308 netdev_exists(const char *name)
310 struct netdev *netdev;
313 error = netdev_open(name, NETDEV_ETH_TYPE_NONE, &netdev);
315 netdev_close(netdev);
318 if (error != ENODEV) {
319 VLOG_WARN("failed to open network device %s: %s",
320 name, strerror(error));
326 /* Initializes 'svec' with a list of the names of all known network devices. */
328 netdev_enumerate(struct svec *svec)
338 for (i = 0; i < n_netdev_classes; i++) {
339 const struct netdev_class *class = netdev_classes[i];
340 if (class->enumerate) {
341 int retval = class->enumerate(svec);
343 VLOG_WARN("failed to enumerate %s network devices: %s",
344 class->type, strerror(retval));
354 /* Attempts to receive a packet from 'netdev' into 'buffer', which the caller
355 * must have initialized with sufficient room for the packet. The space
356 * required to receive any packet is ETH_HEADER_LEN bytes, plus VLAN_HEADER_LEN
357 * bytes, plus the device's MTU (which may be retrieved via netdev_get_mtu()).
358 * (Some devices do not allow for a VLAN header, in which case VLAN_HEADER_LEN
359 * need not be included.)
361 * If a packet is successfully retrieved, returns 0. In this case 'buffer' is
362 * guaranteed to contain at least ETH_TOTAL_MIN bytes. Otherwise, returns a
363 * positive errno value. Returns EAGAIN immediately if no packet is ready to
367 netdev_recv(struct netdev *netdev, struct ofpbuf *buffer)
371 assert(buffer->size == 0);
372 assert(ofpbuf_tailroom(buffer) >= ETH_TOTAL_MIN);
374 retval = netdev->netdev_class->recv(netdev,
375 buffer->data, ofpbuf_tailroom(buffer));
377 COVERAGE_INC(netdev_received);
378 buffer->size += retval;
379 if (buffer->size < ETH_TOTAL_MIN) {
380 ofpbuf_put_zeros(buffer, ETH_TOTAL_MIN - buffer->size);
388 /* Registers with the poll loop to wake up from the next call to poll_block()
389 * when a packet is ready to be received with netdev_recv() on 'netdev'. */
391 netdev_recv_wait(struct netdev *netdev)
393 netdev->netdev_class->recv_wait(netdev);
396 /* Discards all packets waiting to be received from 'netdev'. */
398 netdev_drain(struct netdev *netdev)
400 return netdev->netdev_class->drain(netdev);
403 /* Sends 'buffer' on 'netdev'. Returns 0 if successful, otherwise a positive
404 * errno value. Returns EAGAIN without blocking if the packet cannot be queued
405 * immediately. Returns EMSGSIZE if a partial packet was transmitted or if
406 * the packet is too big or too small to transmit on the device.
408 * The caller retains ownership of 'buffer' in all cases.
410 * The kernel maintains a packet transmission queue, so the caller is not
411 * expected to do additional queuing of packets. */
413 netdev_send(struct netdev *netdev, const struct ofpbuf *buffer)
415 int error = netdev->netdev_class->send(netdev, buffer->data, buffer->size);
417 COVERAGE_INC(netdev_sent);
422 /* Registers with the poll loop to wake up from the next call to poll_block()
423 * when the packet transmission queue has sufficient room to transmit a packet
424 * with netdev_send().
426 * The kernel maintains a packet transmission queue, so the client is not
427 * expected to do additional queuing of packets. Thus, this function is
428 * unlikely to ever be used. It is included for completeness. */
430 netdev_send_wait(struct netdev *netdev)
432 return netdev->netdev_class->send_wait(netdev);
435 /* Attempts to set 'netdev''s MAC address to 'mac'. Returns 0 if successful,
436 * otherwise a positive errno value. */
438 netdev_set_etheraddr(struct netdev *netdev, const uint8_t mac[ETH_ADDR_LEN])
440 return netdev->netdev_class->set_etheraddr(netdev, mac);
443 /* Retrieves 'netdev''s MAC address. If successful, returns 0 and copies the
444 * the MAC address into 'mac'. On failure, returns a positive errno value and
445 * clears 'mac' to all-zeros. */
447 netdev_get_etheraddr(const struct netdev *netdev, uint8_t mac[ETH_ADDR_LEN])
449 return netdev->netdev_class->get_etheraddr(netdev, mac);
452 /* Returns the name of the network device that 'netdev' represents,
453 * e.g. "eth0". The caller must not modify or free the returned string. */
455 netdev_get_name(const struct netdev *netdev)
460 /* Retrieves the MTU of 'netdev'. The MTU is the maximum size of transmitted
461 * (and received) packets, in bytes, not including the hardware header; thus,
462 * this is typically 1500 bytes for Ethernet devices.
464 * If successful, returns 0 and stores the MTU size in '*mtup'. On failure,
465 * returns a positive errno value and stores ETH_PAYLOAD_MAX (1500) in
468 netdev_get_mtu(const struct netdev *netdev, int *mtup)
470 int error = netdev->netdev_class->get_mtu(netdev, mtup);
472 VLOG_WARN_RL(&rl, "failed to retrieve MTU for network device %s: %s",
473 netdev_get_name(netdev), strerror(error));
474 *mtup = ETH_PAYLOAD_MAX;
479 /* Returns the ifindex of 'netdev', if successful, as a positive number. On
480 * failure, returns a negative errno value.
482 * The desired semantics of the ifindex value are a combination of those
483 * specified by POSIX for if_nametoindex() and by SNMP for ifIndex. An ifindex
484 * value should be unique within a host and remain stable at least until
485 * reboot. SNMP says an ifindex "ranges between 1 and the value of ifNumber"
486 * but many systems do not follow this rule anyhow.
489 netdev_get_ifindex(const struct netdev *netdev)
491 return netdev->netdev_class->get_ifindex(netdev);
494 /* Stores the features supported by 'netdev' into each of '*current',
495 * '*advertised', '*supported', and '*peer' that are non-null. Each value is a
496 * bitmap of "enum ofp_port_features" bits, in host byte order. Returns 0 if
497 * successful, otherwise a positive errno value. On failure, all of the
498 * passed-in values are set to 0. */
500 netdev_get_features(struct netdev *netdev,
501 uint32_t *current, uint32_t *advertised,
502 uint32_t *supported, uint32_t *peer)
511 advertised = &dummy[1];
514 supported = &dummy[2];
520 error = netdev->netdev_class->get_features(netdev, current, advertised,
523 *current = *advertised = *supported = *peer = 0;
528 /* Returns the maximum speed of a network connection that has the "enum
529 * ofp_port_features" bits in 'features', in bits per second. If no bits that
530 * indicate a speed are set in 'features', assumes 100Mbps. */
532 netdev_features_to_bps(uint32_t features)
535 F_10000MB = OFPPF_10GB_FD,
536 F_1000MB = OFPPF_1GB_HD | OFPPF_1GB_FD,
537 F_100MB = OFPPF_100MB_HD | OFPPF_100MB_FD,
538 F_10MB = OFPPF_10MB_HD | OFPPF_10MB_FD
541 return ( features & F_10000MB ? UINT64_C(10000000000)
542 : features & F_1000MB ? UINT64_C(1000000000)
543 : features & F_100MB ? UINT64_C(100000000)
544 : features & F_10MB ? UINT64_C(10000000)
545 : UINT64_C(100000000));
548 /* Returns true if any of the "enum ofp_port_features" bits that indicate a
549 * full-duplex link are set in 'features', otherwise false. */
551 netdev_features_is_full_duplex(uint32_t features)
553 return (features & (OFPPF_10MB_FD | OFPPF_100MB_FD | OFPPF_1GB_FD
554 | OFPPF_10GB_FD)) != 0;
557 /* Set the features advertised by 'netdev' to 'advertise'. Returns 0 if
558 * successful, otherwise a positive errno value. */
560 netdev_set_advertisements(struct netdev *netdev, uint32_t advertise)
562 return (netdev->netdev_class->set_advertisements
563 ? netdev->netdev_class->set_advertisements(netdev, advertise)
567 /* If 'netdev' has an assigned IPv4 address, sets '*address' to that address
568 * and '*netmask' to its netmask and returns 0. Otherwise, returns a positive
569 * errno value and sets '*address' to 0 (INADDR_ANY).
571 * The following error values have well-defined meanings:
573 * - EADDRNOTAVAIL: 'netdev' has no assigned IPv4 address.
575 * - EOPNOTSUPP: No IPv4 network stack attached to 'netdev'.
577 * 'address' or 'netmask' or both may be null, in which case the address or netmask
578 * is not reported. */
580 netdev_get_in4(const struct netdev *netdev,
581 struct in_addr *address_, struct in_addr *netmask_)
583 struct in_addr address;
584 struct in_addr netmask;
587 error = (netdev->netdev_class->get_in4
588 ? netdev->netdev_class->get_in4(netdev, &address, &netmask)
591 address_->s_addr = error ? 0 : address.s_addr;
594 netmask_->s_addr = error ? 0 : netmask.s_addr;
599 /* Assigns 'addr' as 'netdev''s IPv4 address and 'mask' as its netmask. If
600 * 'addr' is INADDR_ANY, 'netdev''s IPv4 address is cleared. Returns a
601 * positive errno value. */
603 netdev_set_in4(struct netdev *netdev, struct in_addr addr, struct in_addr mask)
605 return (netdev->netdev_class->set_in4
606 ? netdev->netdev_class->set_in4(netdev, addr, mask)
610 /* Adds 'router' as a default IP gateway for the TCP/IP stack that corresponds
613 netdev_add_router(struct netdev *netdev, struct in_addr router)
615 COVERAGE_INC(netdev_add_router);
616 return (netdev->netdev_class->add_router
617 ? netdev->netdev_class->add_router(netdev, router)
621 /* Looks up the next hop for 'host' for the TCP/IP stack that corresponds to
622 * 'netdev'. If a route cannot not be determined, sets '*next_hop' to 0,
623 * '*netdev_name' to null, and returns a positive errno value. Otherwise, if a
624 * next hop is found, stores the next hop gateway's address (0 if 'host' is on
625 * a directly connected network) in '*next_hop' and a copy of the name of the
626 * device to reach 'host' in '*netdev_name', and returns 0. The caller is
627 * responsible for freeing '*netdev_name' (by calling free()). */
629 netdev_get_next_hop(const struct netdev *netdev,
630 const struct in_addr *host, struct in_addr *next_hop,
633 int error = (netdev->netdev_class->get_next_hop
634 ? netdev->netdev_class->get_next_hop(host, next_hop,
638 next_hop->s_addr = 0;
644 /* If 'netdev' has an assigned IPv6 address, sets '*in6' to that address and
645 * returns 0. Otherwise, returns a positive errno value and sets '*in6' to
646 * all-zero-bits (in6addr_any).
648 * The following error values have well-defined meanings:
650 * - EADDRNOTAVAIL: 'netdev' has no assigned IPv6 address.
652 * - EOPNOTSUPP: No IPv6 network stack attached to 'netdev'.
654 * 'in6' may be null, in which case the address itself is not reported. */
656 netdev_get_in6(const struct netdev *netdev, struct in6_addr *in6)
658 struct in6_addr dummy;
661 error = (netdev->netdev_class->get_in6
662 ? netdev->netdev_class->get_in6(netdev, in6 ? in6 : &dummy)
665 memset(in6, 0, sizeof *in6);
670 /* On 'netdev', turns off the flags in 'off' and then turns on the flags in
671 * 'on'. If 'permanent' is true, the changes will persist; otherwise, they
672 * will be reverted when 'netdev' is closed or the program exits. Returns 0 if
673 * successful, otherwise a positive errno value. */
675 do_update_flags(struct netdev *netdev, enum netdev_flags off,
676 enum netdev_flags on, enum netdev_flags *old_flagsp,
679 enum netdev_flags old_flags;
682 error = netdev->netdev_class->update_flags(netdev, off & ~on,
685 VLOG_WARN_RL(&rl, "failed to %s flags for network device %s: %s",
686 off || on ? "set" : "get", netdev_get_name(netdev),
689 } else if ((off || on) && !permanent) {
690 enum netdev_flags new_flags = (old_flags & ~off) | on;
691 enum netdev_flags changed_flags = old_flags ^ new_flags;
693 if (!netdev->changed_flags) {
694 netdev->save_flags = old_flags;
696 netdev->changed_flags |= changed_flags;
700 *old_flagsp = old_flags;
705 /* Obtains the current flags for 'netdev' and stores them into '*flagsp'.
706 * Returns 0 if successful, otherwise a positive errno value. On failure,
707 * stores 0 into '*flagsp'. */
709 netdev_get_flags(const struct netdev *netdev_, enum netdev_flags *flagsp)
711 struct netdev *netdev = (struct netdev *) netdev_;
712 return do_update_flags(netdev, 0, 0, flagsp, false);
715 /* Sets the flags for 'netdev' to 'flags'.
716 * If 'permanent' is true, the changes will persist; otherwise, they
717 * will be reverted when 'netdev' is closed or the program exits.
718 * Returns 0 if successful, otherwise a positive errno value. */
720 netdev_set_flags(struct netdev *netdev, enum netdev_flags flags,
723 return do_update_flags(netdev, -1, flags, NULL, permanent);
726 /* Turns on the specified 'flags' on 'netdev'.
727 * If 'permanent' is true, the changes will persist; otherwise, they
728 * will be reverted when 'netdev' is closed or the program exits.
729 * Returns 0 if successful, otherwise a positive errno value. */
731 netdev_turn_flags_on(struct netdev *netdev, enum netdev_flags flags,
734 return do_update_flags(netdev, 0, flags, NULL, permanent);
737 /* Turns off the specified 'flags' on 'netdev'.
738 * If 'permanent' is true, the changes will persist; otherwise, they
739 * will be reverted when 'netdev' is closed or the program exits.
740 * Returns 0 if successful, otherwise a positive errno value. */
742 netdev_turn_flags_off(struct netdev *netdev, enum netdev_flags flags,
745 return do_update_flags(netdev, flags, 0, NULL, permanent);
748 /* Looks up the ARP table entry for 'ip' on 'netdev'. If one exists and can be
749 * successfully retrieved, it stores the corresponding MAC address in 'mac' and
750 * returns 0. Otherwise, it returns a positive errno value; in particular,
751 * ENXIO indicates that there is no ARP table entry for 'ip' on 'netdev'. */
753 netdev_arp_lookup(const struct netdev *netdev,
754 uint32_t ip, uint8_t mac[ETH_ADDR_LEN])
756 int error = (netdev->netdev_class->arp_lookup
757 ? netdev->netdev_class->arp_lookup(netdev, ip, mac)
760 memset(mac, 0, ETH_ADDR_LEN);
765 /* Sets 'carrier' to true if carrier is active (link light is on) on
768 netdev_get_carrier(const struct netdev *netdev, bool *carrier)
770 int error = (netdev->netdev_class->get_carrier
771 ? netdev->netdev_class->get_carrier(netdev, carrier)
779 /* Retrieves current device stats for 'netdev'. */
781 netdev_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
785 COVERAGE_INC(netdev_get_stats);
786 error = (netdev->netdev_class->get_stats
787 ? netdev->netdev_class->get_stats(netdev, stats)
790 memset(stats, 0xff, sizeof *stats);
795 /* Attempts to set input rate limiting (policing) policy, such that up to
796 * 'kbits_rate' kbps of traffic is accepted, with a maximum accumulative burst
797 * size of 'kbits' kb. */
799 netdev_set_policing(struct netdev *netdev, uint32_t kbits_rate,
800 uint32_t kbits_burst)
802 return (netdev->netdev_class->set_policing
803 ? netdev->netdev_class->set_policing(netdev,
804 kbits_rate, kbits_burst)
808 /* If 'netdev' is a VLAN network device (e.g. one created with vconfig(8)),
809 * sets '*vlan_vid' to the VLAN VID associated with that device and returns 0.
810 * Otherwise returns a errno value (specifically ENOENT if 'netdev_name' is the
811 * name of a network device that is not a VLAN device) and sets '*vlan_vid' to
814 netdev_get_vlan_vid(const struct netdev *netdev, int *vlan_vid)
816 int error = (netdev->netdev_class->get_vlan_vid
817 ? netdev->netdev_class->get_vlan_vid(netdev, vlan_vid)
825 /* Returns a network device that has 'in4' as its IP address, if one exists,
826 * otherwise a null pointer. */
828 netdev_find_dev_by_in4(const struct in_addr *in4)
830 struct netdev *netdev;
831 struct svec dev_list;
834 netdev_enumerate(&dev_list);
835 for (i = 0; i < dev_list.n; i++) {
836 const char *name = dev_list.names[i];
837 struct in_addr dev_in4;
839 if (!netdev_open(name, NETDEV_ETH_TYPE_NONE, &netdev)
840 && !netdev_get_in4(netdev, &dev_in4, NULL)
841 && dev_in4.s_addr == in4->s_addr) {
844 netdev_close(netdev);
849 svec_destroy(&dev_list);
853 /* Initializes 'netdev_obj' as a netdev object named 'name' of the
854 * specified 'netdev_class'.
856 * This function adds 'netdev_obj' to a netdev-owned shash, so it is
857 * very important that 'netdev_obj' only be freed after calling
858 * netdev_destroy(). */
860 netdev_obj_init(struct netdev_obj *netdev_obj, const char *name,
861 const struct netdev_class *netdev_class, bool created)
863 assert(!shash_find(&netdev_obj_shash, name));
865 netdev_obj->netdev_class = netdev_class;
866 netdev_obj->ref_cnt = 0;
867 netdev_obj->created = created;
868 shash_add(&netdev_obj_shash, name, netdev_obj);
871 /* Initializes 'netdev' as a netdev named 'name' of the specified
874 * This function adds 'netdev' to a netdev-owned linked list, so it is very
875 * important that 'netdev' only be freed after calling netdev_close(). */
877 netdev_init(struct netdev *netdev, const char *name,
878 const struct netdev_class *netdev_class)
880 netdev->netdev_class = netdev_class;
881 netdev->name = xstrdup(name);
882 netdev->save_flags = 0;
883 netdev->changed_flags = 0;
884 list_push_back(&netdev_list, &netdev->node);
887 /* Returns the class type of 'netdev'.
889 * The caller must not free the returned value. */
890 const char *netdev_get_type(const struct netdev *netdev)
892 return netdev->netdev_class->type;
895 /* Initializes 'notifier' as a netdev notifier for 'netdev', for which
896 * notification will consist of calling 'cb', with auxiliary data 'aux'. */
898 netdev_notifier_init(struct netdev_notifier *notifier, struct netdev *netdev,
899 void (*cb)(struct netdev_notifier *), void *aux)
901 notifier->netdev = netdev;
906 /* Tracks changes in the status of a set of network devices. */
907 struct netdev_monitor {
908 struct shash polled_netdevs;
909 struct shash changed_netdevs;
912 /* Creates and returns a new structure for monitor changes in the status of
913 * network devices. */
914 struct netdev_monitor *
915 netdev_monitor_create(void)
917 struct netdev_monitor *monitor = xmalloc(sizeof *monitor);
918 shash_init(&monitor->polled_netdevs);
919 shash_init(&monitor->changed_netdevs);
923 /* Destroys 'monitor'. */
925 netdev_monitor_destroy(struct netdev_monitor *monitor)
928 struct shash_node *node;
930 SHASH_FOR_EACH (node, &monitor->polled_netdevs) {
931 struct netdev_notifier *notifier = node->data;
932 notifier->netdev->netdev_class->poll_remove(notifier);
935 shash_destroy(&monitor->polled_netdevs);
936 shash_destroy(&monitor->changed_netdevs);
942 netdev_monitor_cb(struct netdev_notifier *notifier)
944 struct netdev_monitor *monitor = notifier->aux;
945 const char *name = netdev_get_name(notifier->netdev);
946 if (!shash_find(&monitor->changed_netdevs, name)) {
947 shash_add(&monitor->changed_netdevs, name, NULL);
951 /* Attempts to add 'netdev' as a netdev monitored by 'monitor'. Returns 0 if
952 * successful, otherwise a positive errno value.
954 * Adding a given 'netdev' to a monitor multiple times is equivalent to adding
957 netdev_monitor_add(struct netdev_monitor *monitor, struct netdev *netdev)
959 const char *netdev_name = netdev_get_name(netdev);
961 if (!shash_find(&monitor->polled_netdevs, netdev_name)
962 && netdev->netdev_class->poll_add)
964 struct netdev_notifier *notifier;
965 error = netdev->netdev_class->poll_add(netdev, netdev_monitor_cb,
968 assert(notifier->netdev == netdev);
969 shash_add(&monitor->polled_netdevs, netdev_name, notifier);
975 /* Removes 'netdev' from the set of netdevs monitored by 'monitor'. (This has
976 * no effect if 'netdev' is not in the set of devices monitored by
979 netdev_monitor_remove(struct netdev_monitor *monitor, struct netdev *netdev)
981 const char *netdev_name = netdev_get_name(netdev);
982 struct shash_node *node;
984 node = shash_find(&monitor->polled_netdevs, netdev_name);
986 /* Cancel future notifications. */
987 struct netdev_notifier *notifier = node->data;
988 netdev->netdev_class->poll_remove(notifier);
989 shash_delete(&monitor->polled_netdevs, node);
991 /* Drop any pending notification. */
992 node = shash_find(&monitor->changed_netdevs, netdev_name);
994 shash_delete(&monitor->changed_netdevs, node);
999 /* Checks for changes to netdevs in the set monitored by 'monitor'. If any of
1000 * the attributes (Ethernet address, carrier status, speed or peer-advertised
1001 * speed, flags, etc.) of a network device monitored by 'monitor' has changed,
1002 * sets '*devnamep' to the name of a device that has changed and returns 0.
1003 * The caller is responsible for freeing '*devnamep' (with free()).
1005 * If no devices have changed, sets '*devnamep' to NULL and returns EAGAIN.
1008 netdev_monitor_poll(struct netdev_monitor *monitor, char **devnamep)
1010 struct shash_node *node = shash_first(&monitor->changed_netdevs);
1015 *devnamep = xstrdup(node->name);
1016 shash_delete(&monitor->changed_netdevs, node);
1021 /* Registers with the poll loop to wake up from the next call to poll_block()
1022 * when netdev_monitor_poll(monitor) would indicate that a device has
1025 netdev_monitor_poll_wait(const struct netdev_monitor *monitor)
1027 if (!shash_is_empty(&monitor->changed_netdevs)) {
1028 poll_immediate_wake();
1030 /* XXX Nothing needed here for netdev_linux, but maybe other netdev
1031 * classes need help. */
1035 /* Restore the network device flags on 'netdev' to those that were active
1036 * before we changed them. Returns 0 if successful, otherwise a positive
1039 * To avoid reentry, the caller must ensure that fatal signals are blocked. */
1041 restore_flags(struct netdev *netdev)
1043 if (netdev->changed_flags) {
1044 enum netdev_flags restore = netdev->save_flags & netdev->changed_flags;
1045 enum netdev_flags old_flags;
1046 return netdev->netdev_class->update_flags(netdev,
1047 netdev->changed_flags
1049 restore, &old_flags);
1054 /* Retores all the flags on all network devices that we modified. Called from
1055 * a signal handler, so it does not attempt to report error conditions. */
1057 restore_all_flags(void *aux OVS_UNUSED)
1059 struct netdev *netdev;
1060 LIST_FOR_EACH (netdev, struct netdev, node, &netdev_list) {
1061 restore_flags(netdev);