Merge citrix branch into master.
[sliver-openvswitch.git] / lib / netdev.c
1 /*
2  * Copyright (c) 2008, 2009 Nicira Networks.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <config.h>
18 #include "netdev.h"
19
20 #include <assert.h>
21 #include <errno.h>
22 #include <inttypes.h>
23 #include <netinet/in.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27
28 #include "coverage.h"
29 #include "dynamic-string.h"
30 #include "fatal-signal.h"
31 #include "list.h"
32 #include "netdev-provider.h"
33 #include "ofpbuf.h"
34 #include "packets.h"
35 #include "poll-loop.h"
36 #include "shash.h"
37 #include "svec.h"
38
39 #define THIS_MODULE VLM_netdev
40 #include "vlog.h"
41
42 static const struct netdev_class *netdev_classes[] = {
43     &netdev_linux_class,
44     &netdev_tap_class,
45 };
46 static int n_netdev_classes = ARRAY_SIZE(netdev_classes);
47
48 /* All open network devices. */
49 static struct list netdev_list = LIST_INITIALIZER(&netdev_list);
50
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);
54
55 static void restore_all_flags(void *aux);
56 static int restore_flags(struct netdev *netdev);
57
58 /* Attempts to initialize the netdev module.  Returns 0 if successful,
59  * otherwise a positive errno value.
60  *
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. */
63 int
64 netdev_initialize(void)
65 {
66     static int status = -1;
67     if (status < 0) {
68         int i, j;
69
70         fatal_signal_add_hook(restore_all_flags, NULL, true);
71
72         status = 0;
73         for (i = j = 0; i < n_netdev_classes; i++) {
74             const struct netdev_class *class = netdev_classes[i];
75             if (class->init) {
76                 int retval = class->init();
77                 if (!retval) {
78                     netdev_classes[j++] = class;
79                 } else {
80                     VLOG_ERR("failed to initialize %s network device "
81                              "class: %s", class->name, strerror(retval));
82                     if (!status) {
83                         status = retval;
84                     }
85                 }
86             }
87         }
88         n_netdev_classes = j;
89     }
90     return status;
91 }
92
93 /* Performs periodic work needed by all the various kinds of netdevs.
94  *
95  * If your program opens any netdevs, it must call this function within its
96  * main poll loop. */
97 void
98 netdev_run(void)
99 {
100     int i;
101     for (i = 0; i < n_netdev_classes; i++) {
102         const struct netdev_class *class = netdev_classes[i];
103         if (class->run) {
104             class->run();
105         }
106     }
107 }
108
109 /* Arranges for poll_block() to wake up when netdev_run() needs to be called.
110  *
111  * If your program opens any netdevs, it must call this function within its
112  * main poll loop. */
113 void
114 netdev_wait(void)
115 {
116     int i;
117     for (i = 0; i < n_netdev_classes; i++) {
118         const struct netdev_class *class = netdev_classes[i];
119         if (class->wait) {
120             class->wait();
121         }
122     }
123 }
124
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.
128  *
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
132  * categories. */
133 int
134 netdev_open(const char *name_, int ethertype, struct netdev **netdevp)
135 {
136     char *name = xstrdup(name_);
137     char *prefix, *suffix, *colon;
138     struct netdev *netdev = NULL;
139     int error;
140     int i;
141
142     netdev_initialize();
143     colon = strchr(name, ':');
144     if (colon) {
145         *colon = '\0';
146         prefix = name;
147         suffix = colon + 1;
148     } else {
149         prefix = "";
150         suffix = name;
151     }
152
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);
157             goto exit;
158         }
159     }
160     error = EAFNOSUPPORT;
161
162 exit:
163     *netdevp = error ? NULL : netdev;
164     return error;
165 }
166
167 /* Closes and destroys 'netdev'. */
168 void
169 netdev_close(struct netdev *netdev)
170 {
171     if (netdev) {
172         char *name;
173         int error;
174
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();
180         if (error) {
181             VLOG_WARN("failed to restore network device flags on %s: %s",
182                       netdev->name, strerror(error));
183         }
184
185         /* Free. */
186         name = netdev->name;
187         netdev->class->close(netdev);
188         free(name);
189     }
190 }
191
192 /* Returns true if a network device named 'name' exists and may be opened,
193  * otherwise false. */
194 bool
195 netdev_exists(const char *name)
196 {
197     struct netdev *netdev;
198     int error;
199
200     error = netdev_open(name, NETDEV_ETH_TYPE_NONE, &netdev);
201     if (!error) {
202         netdev_close(netdev);
203         return true;
204     } else {
205         if (error != ENODEV) {
206             VLOG_WARN("failed to open network device %s: %s",
207                       name, strerror(error));
208         }
209         return false;
210     }
211 }
212
213 /* Initializes 'svec' with a list of the names of all known network devices. */
214 int
215 netdev_enumerate(struct svec *svec)
216 {
217     int error;
218     int i;
219
220     svec_init(svec);
221
222     netdev_initialize();
223
224     error = 0;
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);
229             if (retval) {
230                 VLOG_WARN("failed to enumerate %s network devices: %s",
231                           class->name, strerror(retval));
232                 if (!error) {
233                     error = retval;
234                 }
235             }
236         }
237     }
238     return error;
239 }
240
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.)
247  *
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
251  * be returned.
252  */
253 int
254 netdev_recv(struct netdev *netdev, struct ofpbuf *buffer)
255 {
256     int retval;
257
258     assert(buffer->size == 0);
259     assert(ofpbuf_tailroom(buffer) >= ETH_TOTAL_MIN);
260
261     retval = netdev->class->recv(netdev,
262                                  buffer->data, ofpbuf_tailroom(buffer));
263     if (retval >= 0) {
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);
268         }
269         return 0;
270     } else {
271         return -retval;
272     }
273 }
274
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'. */
277 void
278 netdev_recv_wait(struct netdev *netdev)
279 {
280     netdev->class->recv_wait(netdev);
281 }
282
283 /* Discards all packets waiting to be received from 'netdev'. */
284 int
285 netdev_drain(struct netdev *netdev)
286 {
287     return netdev->class->drain(netdev);
288 }
289
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.
294  *
295  * The caller retains ownership of 'buffer' in all cases.
296  *
297  * The kernel maintains a packet transmission queue, so the caller is not
298  * expected to do additional queuing of packets. */
299 int
300 netdev_send(struct netdev *netdev, const struct ofpbuf *buffer)
301 {
302     int error = netdev->class->send(netdev, buffer->data, buffer->size);
303     if (!error) {
304         COVERAGE_INC(netdev_sent);
305     }
306     return error;
307 }
308
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().
312  *
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. */
316 void
317 netdev_send_wait(struct netdev *netdev)
318 {
319     return netdev->class->send_wait(netdev);
320 }
321
322 /* Attempts to set 'netdev''s MAC address to 'mac'.  Returns 0 if successful,
323  * otherwise a positive errno value. */
324 int
325 netdev_set_etheraddr(struct netdev *netdev, const uint8_t mac[ETH_ADDR_LEN])
326 {
327     return netdev->class->set_etheraddr(netdev, mac);
328 }
329
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. */
333 int
334 netdev_get_etheraddr(const struct netdev *netdev, uint8_t mac[ETH_ADDR_LEN])
335 {
336     return netdev->class->get_etheraddr(netdev, mac);
337 }
338
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. */
341 const char *
342 netdev_get_name(const struct netdev *netdev)
343 {
344     return netdev->name;
345 }
346
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.
350  *
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
353  * '*mtup'. */
354 int
355 netdev_get_mtu(const struct netdev *netdev, int *mtup)
356 {
357     int error = netdev->class->get_mtu(netdev, mtup);
358     if (error) {
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;
362     }
363     return error;
364 }
365
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. */
371 int
372 netdev_get_features(struct netdev *netdev,
373                     uint32_t *current, uint32_t *advertised,
374                     uint32_t *supported, uint32_t *peer)
375 {
376     uint32_t dummy[4];
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]);
382 }
383
384 /* Set the features advertised by 'netdev' to 'advertise'.  Returns 0 if
385  * successful, otherwise a positive errno value. */
386 int
387 netdev_set_advertisements(struct netdev *netdev, uint32_t advertise)
388 {
389     return (netdev->class->set_advertisements
390             ? netdev->class->set_advertisements(netdev, advertise)
391             : EOPNOTSUPP);
392 }
393
394 /* If 'netdev' has an assigned IPv4 address, sets '*address' to that address
395  * and '*netmask' to its netmask and returns 0.  Otherwise, returns a positive
396  * errno value and sets '*address' to 0 (INADDR_ANY).
397  *
398  * The following error values have well-defined meanings:
399  *
400  *   - EADDRNOTAVAIL: 'netdev' has no assigned IPv4 address.
401  *
402  *   - EOPNOTSUPP: No IPv4 network stack attached to 'netdev'.
403  *
404  * 'address' or 'netmask' or both may be null, in which case the address or netmask
405  * is not reported. */
406 int
407 netdev_get_in4(const struct netdev *netdev,
408                struct in_addr *address_, struct in_addr *netmask_)
409 {
410     struct in_addr address;
411     struct in_addr netmask;
412     int error;
413
414     error = (netdev->class->get_in4
415              ? netdev->class->get_in4(netdev, &address, &netmask)
416              : EOPNOTSUPP);
417     if (address_) {
418         address_->s_addr = error ? 0 : address.s_addr;
419     }
420     if (netmask_) {
421         netmask_->s_addr = error ? 0 : netmask.s_addr;
422     }
423     return error;
424 }
425
426 /* Assigns 'addr' as 'netdev''s IPv4 address and 'mask' as its netmask.  If
427  * 'addr' is INADDR_ANY, 'netdev''s IPv4 address is cleared.  Returns a
428  * positive errno value. */
429 int
430 netdev_set_in4(struct netdev *netdev, struct in_addr addr, struct in_addr mask)
431 {
432     return (netdev->class->set_in4
433             ? netdev->class->set_in4(netdev, addr, mask)
434             : EOPNOTSUPP);
435 }
436
437 /* Adds 'router' as a default IP gateway for the TCP/IP stack that corresponds
438  * to 'netdev'. */
439 int
440 netdev_add_router(struct netdev *netdev, struct in_addr router)
441 {
442     COVERAGE_INC(netdev_add_router);
443     return (netdev->class->add_router
444             ? netdev->class->add_router(netdev, router)
445             : EOPNOTSUPP);
446 }
447
448 /* Looks up the next hop for 'host' for the TCP/IP stack that corresponds to
449  * 'netdev'.  If a route cannot not be determined, sets '*next_hop' to 0,
450  * '*netdev_name' to null, and returns a positive errno value.  Otherwise, if a
451  * next hop is found, stores the next hop gateway's address (0 if 'host' is on
452  * a directly connected network) in '*next_hop' and a copy of the name of the
453  * device to reach 'host' in '*netdev_name', and returns 0.  The caller is
454  * responsible for freeing '*netdev_name' (by calling free()). */
455 int
456 netdev_get_next_hop(const struct netdev *netdev,
457                     const struct in_addr *host, struct in_addr *next_hop,
458                     char **netdev_name)
459 {
460     int error = (netdev->class->get_next_hop
461                  ? netdev->class->get_next_hop(host, next_hop, netdev_name)
462                  : EOPNOTSUPP);
463     if (error) {
464         next_hop->s_addr = 0;
465         *netdev_name = NULL;
466     }
467     return error;
468 }
469
470 /* If 'netdev' has an assigned IPv6 address, sets '*in6' to that address and
471  * returns 0.  Otherwise, returns a positive errno value and sets '*in6' to
472  * all-zero-bits (in6addr_any).
473  *
474  * The following error values have well-defined meanings:
475  *
476  *   - EADDRNOTAVAIL: 'netdev' has no assigned IPv6 address.
477  *
478  *   - EOPNOTSUPP: No IPv6 network stack attached to 'netdev'.
479  *
480  * 'in6' may be null, in which case the address itself is not reported. */
481 int
482 netdev_get_in6(const struct netdev *netdev, struct in6_addr *in6)
483 {
484     struct in6_addr dummy;
485     int error;
486
487     error = (netdev->class->get_in6
488              ? netdev->class->get_in6(netdev, in6 ? in6 : &dummy)
489              : EOPNOTSUPP);
490     if (error && in6) {
491         memset(in6, 0, sizeof *in6);
492     }
493     return error;
494 }
495
496 /* On 'netdev', turns off the flags in 'off' and then turns on the flags in
497  * 'on'.  If 'permanent' is true, the changes will persist; otherwise, they
498  * will be reverted when 'netdev' is closed or the program exits.  Returns 0 if
499  * successful, otherwise a positive errno value. */
500 static int
501 do_update_flags(struct netdev *netdev, enum netdev_flags off,
502                 enum netdev_flags on, enum netdev_flags *old_flagsp,
503                 bool permanent)
504 {
505     enum netdev_flags old_flags;
506     int error;
507
508     error = netdev->class->update_flags(netdev, off & ~on, on, &old_flags);
509     if (error) {
510         VLOG_WARN_RL(&rl, "failed to %s flags for network device %s: %s",
511                      off || on ? "set" : "get", netdev_get_name(netdev),
512                      strerror(error));
513         old_flags = 0;
514     } else if ((off || on) && !permanent) {
515         enum netdev_flags new_flags = (old_flags & ~off) | on;
516         enum netdev_flags changed_flags = old_flags ^ new_flags;
517         if (changed_flags) {
518             if (!netdev->changed_flags) {
519                 netdev->save_flags = old_flags;
520             }
521             netdev->changed_flags |= changed_flags;
522         }
523     }
524     if (old_flagsp) {
525         *old_flagsp = old_flags;
526     }
527     return error;
528 }
529
530 /* Obtains the current flags for 'netdev' and stores them into '*flagsp'.
531  * Returns 0 if successful, otherwise a positive errno value.  On failure,
532  * stores 0 into '*flagsp'. */
533 int
534 netdev_get_flags(const struct netdev *netdev_, enum netdev_flags *flagsp)
535 {
536     struct netdev *netdev = (struct netdev *) netdev_;
537     return do_update_flags(netdev, 0, 0, flagsp, false);
538 }
539
540 /* Sets the flags for 'netdev' to 'flags'.
541  * If 'permanent' is true, the changes will persist; otherwise, they
542  * will be reverted when 'netdev' is closed or the program exits.
543  * Returns 0 if successful, otherwise a positive errno value. */
544 int
545 netdev_set_flags(struct netdev *netdev, enum netdev_flags flags,
546                  bool permanent)
547 {
548     return do_update_flags(netdev, -1, flags, NULL, permanent);
549 }
550
551 /* Turns on the specified 'flags' on 'netdev'.
552  * If 'permanent' is true, the changes will persist; otherwise, they
553  * will be reverted when 'netdev' is closed or the program exits.
554  * Returns 0 if successful, otherwise a positive errno value. */
555 int
556 netdev_turn_flags_on(struct netdev *netdev, enum netdev_flags flags,
557                      bool permanent)
558 {
559     return do_update_flags(netdev, 0, flags, NULL, permanent);
560 }
561
562 /* Turns off the specified 'flags' on 'netdev'.
563  * If 'permanent' is true, the changes will persist; otherwise, they
564  * will be reverted when 'netdev' is closed or the program exits.
565  * Returns 0 if successful, otherwise a positive errno value. */
566 int
567 netdev_turn_flags_off(struct netdev *netdev, enum netdev_flags flags,
568                       bool permanent)
569 {
570     return do_update_flags(netdev, flags, 0, NULL, permanent);
571 }
572
573 /* Looks up the ARP table entry for 'ip' on 'netdev'.  If one exists and can be
574  * successfully retrieved, it stores the corresponding MAC address in 'mac' and
575  * returns 0.  Otherwise, it returns a positive errno value; in particular,
576  * ENXIO indicates that there is no ARP table entry for 'ip' on 'netdev'. */
577 int
578 netdev_arp_lookup(const struct netdev *netdev,
579                   uint32_t ip, uint8_t mac[ETH_ADDR_LEN])
580 {
581     int error = (netdev->class->arp_lookup
582                  ? netdev->class->arp_lookup(netdev, ip, mac)
583                  : EOPNOTSUPP);
584     if (error) {
585         memset(mac, 0, ETH_ADDR_LEN);
586     }
587     return error;
588 }
589
590 /* Sets 'carrier' to true if carrier is active (link light is on) on
591  * 'netdev'. */
592 int
593 netdev_get_carrier(const struct netdev *netdev, bool *carrier)
594 {
595     int error = (netdev->class->get_carrier
596                  ? netdev->class->get_carrier(netdev, carrier)
597                  : EOPNOTSUPP);
598     if (error) {
599         *carrier = false;
600     }
601     return error;
602 }
603
604 /* Retrieves current device stats for 'netdev'. */
605 int
606 netdev_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
607 {
608     int error;
609
610     COVERAGE_INC(netdev_get_stats);
611     error = (netdev->class->get_stats
612              ? netdev->class->get_stats(netdev, stats)
613              : EOPNOTSUPP);
614     if (error) {
615         memset(stats, 0xff, sizeof *stats);
616     }
617     return error;
618 }
619
620 /* Attempts to set input rate limiting (policing) policy, such that up to
621  * 'kbits_rate' kbps of traffic is accepted, with a maximum accumulative burst
622  * size of 'kbits' kb. */
623 int
624 netdev_set_policing(struct netdev *netdev, uint32_t kbits_rate,
625                     uint32_t kbits_burst)
626 {
627     return (netdev->class->set_policing
628             ? netdev->class->set_policing(netdev, kbits_rate, kbits_burst)
629             : EOPNOTSUPP);
630 }
631
632 /* If 'netdev' is a VLAN network device (e.g. one created with vconfig(8)),
633  * sets '*vlan_vid' to the VLAN VID associated with that device and returns 0.
634  * Otherwise returns a errno value (specifically ENOENT if 'netdev_name' is the
635  * name of a network device that is not a VLAN device) and sets '*vlan_vid' to
636  * -1. */
637 int
638 netdev_get_vlan_vid(const struct netdev *netdev, int *vlan_vid)
639 {
640     int error = (netdev->class->get_vlan_vid
641                  ? netdev->class->get_vlan_vid(netdev, vlan_vid)
642                  : ENOENT);
643     if (error) {
644         *vlan_vid = 0;
645     }
646     return error;
647 }
648
649 /* Returns a network device that has 'in4' as its IP address, if one exists,
650  * otherwise a null pointer. */
651 struct netdev *
652 netdev_find_dev_by_in4(const struct in_addr *in4)
653 {
654     struct netdev *netdev;
655     struct svec dev_list;
656     size_t i;
657
658     netdev_enumerate(&dev_list);
659     for (i = 0; i < dev_list.n; i++) {
660         const char *name = dev_list.names[i];
661         struct in_addr dev_in4;
662
663         if (!netdev_open(name, NETDEV_ETH_TYPE_NONE, &netdev)
664             && !netdev_get_in4(netdev, &dev_in4, NULL)
665             && dev_in4.s_addr == in4->s_addr) {
666             goto exit;
667         }
668         netdev_close(netdev);
669     }
670     netdev = NULL;
671
672 exit:
673     svec_destroy(&dev_list);
674     return netdev;
675 }
676 \f
677 /* Initializes 'netdev' as a netdev named 'name' of the specified 'class'.
678  *
679  * This function adds 'netdev' to a netdev-owned linked list, so it is very
680  * important that 'netdev' only be freed after calling netdev_close(). */
681 void
682 netdev_init(struct netdev *netdev, const char *name,
683             const struct netdev_class *class)
684 {
685     netdev->class = class;
686     netdev->name = xstrdup(name);
687     netdev->save_flags = 0;
688     netdev->changed_flags = 0;
689     list_push_back(&netdev_list, &netdev->node);
690 }
691
692 /* Initializes 'notifier' as a netdev notifier for 'netdev', for which
693  * notification will consist of calling 'cb', with auxiliary data 'aux'. */
694 void
695 netdev_notifier_init(struct netdev_notifier *notifier, struct netdev *netdev,
696                      void (*cb)(struct netdev_notifier *), void *aux)
697 {
698     notifier->netdev = netdev;
699     notifier->cb = cb;
700     notifier->aux = aux;
701 }
702 \f
703 /* Tracks changes in the status of a set of network devices. */
704 struct netdev_monitor {
705     struct shash polled_netdevs;
706     struct shash changed_netdevs;
707 };
708
709 /* Creates and returns a new structure for monitor changes in the status of
710  * network devices. */
711 struct netdev_monitor *
712 netdev_monitor_create(void)
713 {
714     struct netdev_monitor *monitor = xmalloc(sizeof *monitor);
715     shash_init(&monitor->polled_netdevs);
716     shash_init(&monitor->changed_netdevs);
717     return monitor;
718 }
719
720 /* Destroys 'monitor'. */
721 void
722 netdev_monitor_destroy(struct netdev_monitor *monitor)
723 {
724     if (monitor) {
725         struct shash_node *node;
726
727         SHASH_FOR_EACH (node, &monitor->polled_netdevs) {
728             struct netdev_notifier *notifier = node->data;
729             notifier->netdev->class->poll_remove(notifier);
730         }
731
732         shash_destroy(&monitor->polled_netdevs);
733         shash_destroy(&monitor->changed_netdevs);
734         free(monitor);
735     }
736 }
737
738 static void
739 netdev_monitor_cb(struct netdev_notifier *notifier)
740 {
741     struct netdev_monitor *monitor = notifier->aux;
742     const char *name = netdev_get_name(notifier->netdev);
743     if (!shash_find(&monitor->changed_netdevs, name)) {
744         shash_add(&monitor->changed_netdevs, name, NULL);
745     }
746 }
747
748 /* Attempts to add 'netdev' as a netdev monitored by 'monitor'.  Returns 0 if
749  * successful, otherwise a positive errno value.
750  *
751  * Adding a given 'netdev' to a monitor multiple times is equivalent to adding
752  * it once. */
753 int
754 netdev_monitor_add(struct netdev_monitor *monitor, struct netdev *netdev)
755 {
756     const char *netdev_name = netdev_get_name(netdev);
757     int error = 0;
758     if (!shash_find(&monitor->polled_netdevs, netdev_name)
759         && netdev->class->poll_add)
760     {
761         struct netdev_notifier *notifier;
762         error = netdev->class->poll_add(netdev, netdev_monitor_cb, monitor,
763                                         &notifier);
764         if (!error) {
765             assert(notifier->netdev == netdev);
766             shash_add(&monitor->polled_netdevs, netdev_name, notifier);
767         }
768     }
769     return error;
770 }
771
772 /* Removes 'netdev' from the set of netdevs monitored by 'monitor'.  (This has
773  * no effect if 'netdev' is not in the set of devices monitored by
774  * 'monitor'.) */
775 void
776 netdev_monitor_remove(struct netdev_monitor *monitor, struct netdev *netdev)
777 {
778     const char *netdev_name = netdev_get_name(netdev);
779     struct shash_node *node;
780
781     node = shash_find(&monitor->polled_netdevs, netdev_name);
782     if (node) {
783         /* Cancel future notifications. */
784         struct netdev_notifier *notifier = node->data;
785         netdev->class->poll_remove(notifier);
786         shash_delete(&monitor->polled_netdevs, node);
787
788         /* Drop any pending notification. */
789         node = shash_find(&monitor->changed_netdevs, netdev_name);
790         if (node) {
791             shash_delete(&monitor->changed_netdevs, node);
792         }
793     }
794 }
795
796 /* Checks for changes to netdevs in the set monitored by 'monitor'.  If any of
797  * the attributes (Ethernet address, carrier status, speed or peer-advertised
798  * speed, flags, etc.) of a network device monitored by 'monitor' has changed,
799  * sets '*devnamep' to the name of a device that has changed and returns 0.
800  * The caller is responsible for freeing '*devnamep' (with free()).
801  *
802  * If no devices have changed, sets '*devnamep' to NULL and returns EAGAIN.
803  */
804 int
805 netdev_monitor_poll(struct netdev_monitor *monitor, char **devnamep)
806 {
807     struct shash_node *node = shash_first(&monitor->changed_netdevs);
808     if (!node) {
809         *devnamep = NULL;
810         return EAGAIN;
811     } else {
812         *devnamep = xstrdup(node->name);
813         shash_delete(&monitor->changed_netdevs, node);
814         return 0;
815     }
816 }
817
818 /* Registers with the poll loop to wake up from the next call to poll_block()
819  * when netdev_monitor_poll(monitor) would indicate that a device has
820  * changed. */
821 void
822 netdev_monitor_poll_wait(const struct netdev_monitor *monitor)
823 {
824     if (!shash_is_empty(&monitor->changed_netdevs)) {
825         poll_immediate_wake();
826     } else {
827         /* XXX Nothing needed here for netdev_linux, but maybe other netdev
828          * classes need help. */
829     }
830 }
831 \f
832 /* Restore the network device flags on 'netdev' to those that were active
833  * before we changed them.  Returns 0 if successful, otherwise a positive
834  * errno value.
835  *
836  * To avoid reentry, the caller must ensure that fatal signals are blocked. */
837 static int
838 restore_flags(struct netdev *netdev)
839 {
840     if (netdev->changed_flags) {
841         enum netdev_flags restore = netdev->save_flags & netdev->changed_flags;
842         enum netdev_flags old_flags;
843         return netdev->class->update_flags(netdev,
844                                            netdev->changed_flags & ~restore,
845                                            restore, &old_flags);
846     }
847     return 0;
848 }
849
850 /* Retores all the flags on all network devices that we modified.  Called from
851  * a signal handler, so it does not attempt to report error conditions. */
852 static void
853 restore_all_flags(void *aux UNUSED)
854 {
855     struct netdev *netdev;
856     LIST_FOR_EACH (netdev, struct netdev, node, &netdev_list) {
857         restore_flags(netdev);
858     }
859 }