netdev: Get rid of netdev_dev.
[sliver-openvswitch.git] / lib / netdev.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
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 <errno.h>
21 #include <inttypes.h>
22 #include <netinet/in.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26
27 #include "coverage.h"
28 #include "dynamic-string.h"
29 #include "fatal-signal.h"
30 #include "hash.h"
31 #include "list.h"
32 #include "netdev-provider.h"
33 #include "netdev-vport.h"
34 #include "ofpbuf.h"
35 #include "openflow/openflow.h"
36 #include "packets.h"
37 #include "poll-loop.h"
38 #include "shash.h"
39 #include "smap.h"
40 #include "sset.h"
41 #include "svec.h"
42 #include "vlog.h"
43
44 VLOG_DEFINE_THIS_MODULE(netdev);
45
46 COVERAGE_DEFINE(netdev_received);
47 COVERAGE_DEFINE(netdev_sent);
48 COVERAGE_DEFINE(netdev_add_router);
49 COVERAGE_DEFINE(netdev_get_stats);
50
51 struct netdev_saved_flags {
52     struct netdev *netdev;
53     struct list node;           /* In struct netdev's saved_flags_list. */
54     enum netdev_flags saved_flags;
55     enum netdev_flags saved_values;
56 };
57
58 static struct shash netdev_classes = SHASH_INITIALIZER(&netdev_classes);
59
60 /* All created network devices. */
61 static struct shash netdev_shash = SHASH_INITIALIZER(&netdev_shash);
62
63 /* This is set pretty low because we probably won't learn anything from the
64  * additional log messages. */
65 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
66
67 static void restore_all_flags(void *aux OVS_UNUSED);
68 void update_device_args(struct netdev *, const struct shash *args);
69
70 static void
71 netdev_initialize(void)
72 {
73     static bool inited;
74
75     if (!inited) {
76         inited = true;
77
78         fatal_signal_add_hook(restore_all_flags, NULL, NULL, true);
79         netdev_vport_patch_register();
80
81 #ifdef LINUX_DATAPATH
82         netdev_register_provider(&netdev_linux_class);
83         netdev_register_provider(&netdev_internal_class);
84         netdev_register_provider(&netdev_tap_class);
85         netdev_vport_tunnel_register();
86 #endif
87 #ifdef __FreeBSD__
88         netdev_register_provider(&netdev_tap_class);
89         netdev_register_provider(&netdev_bsd_class);
90 #endif
91     }
92 }
93
94 /* Performs periodic work needed by all the various kinds of netdevs.
95  *
96  * If your program opens any netdevs, it must call this function within its
97  * main poll loop. */
98 void
99 netdev_run(void)
100 {
101     struct shash_node *node;
102     SHASH_FOR_EACH(node, &netdev_classes) {
103         const struct netdev_class *netdev_class = node->data;
104         if (netdev_class->run) {
105             netdev_class->run();
106         }
107     }
108 }
109
110 /* Arranges for poll_block() to wake up when netdev_run() needs to be called.
111  *
112  * If your program opens any netdevs, it must call this function within its
113  * main poll loop. */
114 void
115 netdev_wait(void)
116 {
117     struct shash_node *node;
118     SHASH_FOR_EACH(node, &netdev_classes) {
119         const struct netdev_class *netdev_class = node->data;
120         if (netdev_class->wait) {
121             netdev_class->wait();
122         }
123     }
124 }
125
126 /* Initializes and registers a new netdev provider.  After successful
127  * registration, new netdevs of that type can be opened using netdev_open(). */
128 int
129 netdev_register_provider(const struct netdev_class *new_class)
130 {
131     if (shash_find(&netdev_classes, new_class->type)) {
132         VLOG_WARN("attempted to register duplicate netdev provider: %s",
133                    new_class->type);
134         return EEXIST;
135     }
136
137     if (new_class->init) {
138         int error = new_class->init();
139         if (error) {
140             VLOG_ERR("failed to initialize %s network device class: %s",
141                      new_class->type, strerror(error));
142             return error;
143         }
144     }
145
146     shash_add(&netdev_classes, new_class->type, new_class);
147
148     return 0;
149 }
150
151 /* Unregisters a netdev provider.  'type' must have been previously
152  * registered and not currently be in use by any netdevs.  After unregistration
153  * new netdevs of that type cannot be opened using netdev_open(). */
154 int
155 netdev_unregister_provider(const char *type)
156 {
157     struct shash_node *del_node, *netdev_node;
158
159     del_node = shash_find(&netdev_classes, type);
160     if (!del_node) {
161         VLOG_WARN("attempted to unregister a netdev provider that is not "
162                   "registered: %s", type);
163         return EAFNOSUPPORT;
164     }
165
166     SHASH_FOR_EACH (netdev_node, &netdev_shash) {
167         struct netdev *netdev = netdev_node->data;
168         if (!strcmp(netdev->netdev_class->type, type)) {
169             VLOG_WARN("attempted to unregister in use netdev provider: %s",
170                       type);
171             return EBUSY;
172         }
173     }
174
175     shash_delete(&netdev_classes, del_node);
176
177     return 0;
178 }
179
180 const struct netdev_class *
181 netdev_lookup_provider(const char *type)
182 {
183     netdev_initialize();
184     return shash_find_data(&netdev_classes, type && type[0] ? type : "system");
185 }
186
187 /* Clears 'types' and enumerates the types of all currently registered netdev
188  * providers into it.  The caller must first initialize the sset. */
189 void
190 netdev_enumerate_types(struct sset *types)
191 {
192     struct shash_node *node;
193
194     netdev_initialize();
195     sset_clear(types);
196
197     SHASH_FOR_EACH(node, &netdev_classes) {
198         const struct netdev_class *netdev_class = node->data;
199         sset_add(types, netdev_class->type);
200     }
201 }
202
203 /* Opens the network device named 'name' (e.g. "eth0") of the specified 'type'
204  * (e.g. "system") and returns zero if successful, otherwise a positive errno
205  * value.  On success, sets '*netdevp' to the new network device, otherwise to
206  * null.
207  *
208  * Some network devices may need to be configured (with netdev_set_config())
209  * before they can be used. */
210 int
211 netdev_open(const char *name, const char *type, struct netdev **netdevp)
212 {
213     struct netdev *netdev;
214     int error;
215
216     *netdevp = NULL;
217     netdev_initialize();
218
219     netdev = shash_find_data(&netdev_shash, name);
220     if (!netdev) {
221         const struct netdev_class *class;
222
223         class = netdev_lookup_provider(type);
224         if (!class) {
225             VLOG_WARN("could not create netdev %s of unknown type %s",
226                       name, type);
227             return EAFNOSUPPORT;
228         }
229         error = class->create(class, name, &netdev);
230         if (error) {
231             return error;
232         }
233         ovs_assert(netdev->netdev_class == class);
234
235     }
236     netdev->ref_cnt++;
237
238     *netdevp = netdev;
239     return 0;
240 }
241
242 /* Reconfigures the device 'netdev' with 'args'.  'args' may be empty
243  * or NULL if none are needed. */
244 int
245 netdev_set_config(struct netdev *netdev, const struct smap *args)
246 {
247     if (netdev->netdev_class->set_config) {
248         struct smap no_args = SMAP_INITIALIZER(&no_args);
249         return netdev->netdev_class->set_config(netdev,
250                                                 args ? args : &no_args);
251     } else if (args && !smap_is_empty(args)) {
252         VLOG_WARN("%s: arguments provided to device that is not configurable",
253                   netdev_get_name(netdev));
254     }
255
256     return 0;
257 }
258
259 /* Returns the current configuration for 'netdev' in 'args'.  The caller must
260  * have already initialized 'args' with smap_init().  Returns 0 on success, in
261  * which case 'args' will be filled with 'netdev''s configuration.  On failure
262  * returns a positive errno value, in which case 'args' will be empty.
263  *
264  * The caller owns 'args' and its contents and must eventually free them with
265  * smap_destroy(). */
266 int
267 netdev_get_config(const struct netdev *netdev, struct smap *args)
268 {
269     int error;
270
271     smap_clear(args);
272     if (netdev->netdev_class->get_config) {
273         error = netdev->netdev_class->get_config(netdev, args);
274         if (error) {
275             smap_clear(args);
276         }
277     } else {
278         error = 0;
279     }
280
281     return error;
282 }
283
284 const struct netdev_tunnel_config *
285 netdev_get_tunnel_config(const struct netdev *netdev)
286 {
287     if (netdev->netdev_class->get_tunnel_config) {
288         return netdev->netdev_class->get_tunnel_config(netdev);
289     } else {
290         return NULL;
291     }
292 }
293
294 static void
295 netdev_unref(struct netdev *dev)
296 {
297     ovs_assert(dev->ref_cnt);
298     if (!--dev->ref_cnt) {
299         netdev_uninit(dev, true);
300     }
301 }
302
303 /* Closes and destroys 'netdev'. */
304 void
305 netdev_close(struct netdev *netdev)
306 {
307     if (netdev) {
308         netdev_unref(netdev);
309     }
310 }
311
312 /* Parses 'netdev_name_', which is of the form [type@]name into its component
313  * pieces.  'name' and 'type' must be freed by the caller. */
314 void
315 netdev_parse_name(const char *netdev_name_, char **name, char **type)
316 {
317     char *netdev_name = xstrdup(netdev_name_);
318     char *separator;
319
320     separator = strchr(netdev_name, '@');
321     if (separator) {
322         *separator = '\0';
323         *type = netdev_name;
324         *name = xstrdup(separator + 1);
325     } else {
326         *name = netdev_name;
327         *type = xstrdup("system");
328     }
329 }
330
331 int
332 netdev_rx_open(struct netdev *netdev, struct netdev_rx **rxp)
333 {
334     int error;
335
336     error = (netdev->netdev_class->rx_open
337              ? netdev->netdev_class->rx_open(netdev, rxp)
338              : EOPNOTSUPP);
339     if (!error) {
340         ovs_assert((*rxp)->netdev == netdev);
341         netdev->ref_cnt++;
342     } else {
343         *rxp = NULL;
344     }
345     return error;
346 }
347
348 void
349 netdev_rx_close(struct netdev_rx *rx)
350 {
351     if (rx) {
352         struct netdev *dev = rx->netdev;
353
354         rx->rx_class->destroy(rx);
355         netdev_unref(dev);
356     }
357 }
358
359 int
360 netdev_rx_recv(struct netdev_rx *rx, struct ofpbuf *buffer)
361 {
362     int retval;
363
364     ovs_assert(buffer->size == 0);
365     ovs_assert(ofpbuf_tailroom(buffer) >= ETH_TOTAL_MIN);
366
367     retval = rx->rx_class->recv(rx, buffer->data, ofpbuf_tailroom(buffer));
368     if (retval >= 0) {
369         COVERAGE_INC(netdev_received);
370         buffer->size += retval;
371         if (buffer->size < ETH_TOTAL_MIN) {
372             ofpbuf_put_zeros(buffer, ETH_TOTAL_MIN - buffer->size);
373         }
374         return 0;
375     } else {
376         return -retval;
377     }
378 }
379
380 void
381 netdev_rx_wait(struct netdev_rx *rx)
382 {
383     rx->rx_class->wait(rx);
384 }
385
386 int
387 netdev_rx_drain(struct netdev_rx *rx)
388 {
389     return rx->rx_class->drain ? rx->rx_class->drain(rx) : 0;
390 }
391
392 /* Sends 'buffer' on 'netdev'.  Returns 0 if successful, otherwise a positive
393  * errno value.  Returns EAGAIN without blocking if the packet cannot be queued
394  * immediately.  Returns EMSGSIZE if a partial packet was transmitted or if
395  * the packet is too big or too small to transmit on the device.
396  *
397  * The caller retains ownership of 'buffer' in all cases.
398  *
399  * The kernel maintains a packet transmission queue, so the caller is not
400  * expected to do additional queuing of packets.
401  *
402  * Some network devices may not implement support for this function.  In such
403  * cases this function will always return EOPNOTSUPP. */
404 int
405 netdev_send(struct netdev *netdev, const struct ofpbuf *buffer)
406 {
407     int error;
408
409     error = (netdev->netdev_class->send
410              ? netdev->netdev_class->send(netdev, buffer->data, buffer->size)
411              : EOPNOTSUPP);
412     if (!error) {
413         COVERAGE_INC(netdev_sent);
414     }
415     return error;
416 }
417
418 /* Registers with the poll loop to wake up from the next call to poll_block()
419  * when the packet transmission queue has sufficient room to transmit a packet
420  * with netdev_send().
421  *
422  * The kernel maintains a packet transmission queue, so the client is not
423  * expected to do additional queuing of packets.  Thus, this function is
424  * unlikely to ever be used.  It is included for completeness. */
425 void
426 netdev_send_wait(struct netdev *netdev)
427 {
428     if (netdev->netdev_class->send_wait) {
429         netdev->netdev_class->send_wait(netdev);
430     }
431 }
432
433 /* Attempts to set 'netdev''s MAC address to 'mac'.  Returns 0 if successful,
434  * otherwise a positive errno value. */
435 int
436 netdev_set_etheraddr(struct netdev *netdev, const uint8_t mac[ETH_ADDR_LEN])
437 {
438     return netdev->netdev_class->set_etheraddr(netdev, mac);
439 }
440
441 /* Retrieves 'netdev''s MAC address.  If successful, returns 0 and copies the
442  * the MAC address into 'mac'.  On failure, returns a positive errno value and
443  * clears 'mac' to all-zeros. */
444 int
445 netdev_get_etheraddr(const struct netdev *netdev, uint8_t mac[ETH_ADDR_LEN])
446 {
447     return netdev->netdev_class->get_etheraddr(netdev, mac);
448 }
449
450 /* Returns the name of the network device that 'netdev' represents,
451  * e.g. "eth0".  The caller must not modify or free the returned string. */
452 const char *
453 netdev_get_name(const struct netdev *netdev)
454 {
455     return netdev->name;
456 }
457
458 /* Retrieves the MTU of 'netdev'.  The MTU is the maximum size of transmitted
459  * (and received) packets, in bytes, not including the hardware header; thus,
460  * this is typically 1500 bytes for Ethernet devices.
461  *
462  * If successful, returns 0 and stores the MTU size in '*mtup'.  Returns
463  * EOPNOTSUPP if 'netdev' does not have an MTU (as e.g. some tunnels do not).
464  * On other failure, returns a positive errno value.  On failure, sets '*mtup'
465  * to 0. */
466 int
467 netdev_get_mtu(const struct netdev *netdev, int *mtup)
468 {
469     const struct netdev_class *class = netdev->netdev_class;
470     int error;
471
472     error = class->get_mtu ? class->get_mtu(netdev, mtup) : EOPNOTSUPP;
473     if (error) {
474         *mtup = 0;
475         if (error != EOPNOTSUPP) {
476             VLOG_DBG_RL(&rl, "failed to retrieve MTU for network device %s: "
477                          "%s", netdev_get_name(netdev), strerror(error));
478         }
479     }
480     return error;
481 }
482
483 /* Sets the MTU of 'netdev'.  The MTU is the maximum size of transmitted
484  * (and received) packets, in bytes.
485  *
486  * If successful, returns 0.  Returns EOPNOTSUPP if 'netdev' does not have an
487  * MTU (as e.g. some tunnels do not).  On other failure, returns a positive
488  * errno value. */
489 int
490 netdev_set_mtu(const struct netdev *netdev, int mtu)
491 {
492     const struct netdev_class *class = netdev->netdev_class;
493     int error;
494
495     error = class->set_mtu ? class->set_mtu(netdev, mtu) : EOPNOTSUPP;
496     if (error && error != EOPNOTSUPP) {
497         VLOG_DBG_RL(&rl, "failed to set MTU for network device %s: %s",
498                      netdev_get_name(netdev), strerror(error));
499     }
500
501     return error;
502 }
503
504 /* Returns the ifindex of 'netdev', if successful, as a positive number.  On
505  * failure, returns a negative errno value.
506  *
507  * The desired semantics of the ifindex value are a combination of those
508  * specified by POSIX for if_nametoindex() and by SNMP for ifIndex.  An ifindex
509  * value should be unique within a host and remain stable at least until
510  * reboot.  SNMP says an ifindex "ranges between 1 and the value of ifNumber"
511  * but many systems do not follow this rule anyhow.
512  *
513  * Some network devices may not implement support for this function.  In such
514  * cases this function will always return -EOPNOTSUPP.
515  */
516 int
517 netdev_get_ifindex(const struct netdev *netdev)
518 {
519     int (*get_ifindex)(const struct netdev *);
520
521     get_ifindex = netdev->netdev_class->get_ifindex;
522
523     return get_ifindex ? get_ifindex(netdev) : -EOPNOTSUPP;
524 }
525
526 /* Stores the features supported by 'netdev' into each of '*current',
527  * '*advertised', '*supported', and '*peer' that are non-null.  Each value is a
528  * bitmap of "enum ofp_port_features" bits, in host byte order.  Returns 0 if
529  * successful, otherwise a positive errno value.  On failure, all of the
530  * passed-in values are set to 0.
531  *
532  * Some network devices may not implement support for this function.  In such
533  * cases this function will always return EOPNOTSUPP. */
534 int
535 netdev_get_features(const struct netdev *netdev,
536                     enum netdev_features *current,
537                     enum netdev_features *advertised,
538                     enum netdev_features *supported,
539                     enum netdev_features *peer)
540 {
541     int (*get_features)(const struct netdev *netdev,
542                         enum netdev_features *current,
543                         enum netdev_features *advertised,
544                         enum netdev_features *supported,
545                         enum netdev_features *peer);
546     enum netdev_features dummy[4];
547     int error;
548
549     if (!current) {
550         current = &dummy[0];
551     }
552     if (!advertised) {
553         advertised = &dummy[1];
554     }
555     if (!supported) {
556         supported = &dummy[2];
557     }
558     if (!peer) {
559         peer = &dummy[3];
560     }
561
562     get_features = netdev->netdev_class->get_features;
563     error = get_features
564                     ? get_features(netdev, current, advertised, supported,
565                                    peer)
566                     : EOPNOTSUPP;
567     if (error) {
568         *current = *advertised = *supported = *peer = 0;
569     }
570     return error;
571 }
572
573 /* Returns the maximum speed of a network connection that has the NETDEV_F_*
574  * bits in 'features', in bits per second.  If no bits that indicate a speed
575  * are set in 'features', returns 'default_bps'. */
576 uint64_t
577 netdev_features_to_bps(enum netdev_features features,
578                        uint64_t default_bps)
579 {
580     enum {
581         F_1000000MB = NETDEV_F_1TB_FD,
582         F_100000MB = NETDEV_F_100GB_FD,
583         F_40000MB = NETDEV_F_40GB_FD,
584         F_10000MB = NETDEV_F_10GB_FD,
585         F_1000MB = NETDEV_F_1GB_HD | NETDEV_F_1GB_FD,
586         F_100MB = NETDEV_F_100MB_HD | NETDEV_F_100MB_FD,
587         F_10MB = NETDEV_F_10MB_HD | NETDEV_F_10MB_FD
588     };
589
590     return (  features & F_1000000MB ? UINT64_C(1000000000000)
591             : features & F_100000MB  ? UINT64_C(100000000000)
592             : features & F_40000MB   ? UINT64_C(40000000000)
593             : features & F_10000MB   ? UINT64_C(10000000000)
594             : features & F_1000MB    ? UINT64_C(1000000000)
595             : features & F_100MB     ? UINT64_C(100000000)
596             : features & F_10MB      ? UINT64_C(10000000)
597                                      : default_bps);
598 }
599
600 /* Returns true if any of the NETDEV_F_* bits that indicate a full-duplex link
601  * are set in 'features', otherwise false. */
602 bool
603 netdev_features_is_full_duplex(enum netdev_features features)
604 {
605     return (features & (NETDEV_F_10MB_FD | NETDEV_F_100MB_FD | NETDEV_F_1GB_FD
606                         | NETDEV_F_10GB_FD | NETDEV_F_40GB_FD
607                         | NETDEV_F_100GB_FD | NETDEV_F_1TB_FD)) != 0;
608 }
609
610 /* Set the features advertised by 'netdev' to 'advertise'.  Returns 0 if
611  * successful, otherwise a positive errno value. */
612 int
613 netdev_set_advertisements(struct netdev *netdev,
614                           enum netdev_features advertise)
615 {
616     return (netdev->netdev_class->set_advertisements
617             ? netdev->netdev_class->set_advertisements(
618                     netdev, advertise)
619             : EOPNOTSUPP);
620 }
621
622 /* If 'netdev' has an assigned IPv4 address, sets '*address' to that address
623  * and '*netmask' to its netmask and returns 0.  Otherwise, returns a positive
624  * errno value and sets '*address' to 0 (INADDR_ANY).
625  *
626  * The following error values have well-defined meanings:
627  *
628  *   - EADDRNOTAVAIL: 'netdev' has no assigned IPv4 address.
629  *
630  *   - EOPNOTSUPP: No IPv4 network stack attached to 'netdev'.
631  *
632  * 'address' or 'netmask' or both may be null, in which case the address or
633  * netmask is not reported. */
634 int
635 netdev_get_in4(const struct netdev *netdev,
636                struct in_addr *address_, struct in_addr *netmask_)
637 {
638     struct in_addr address;
639     struct in_addr netmask;
640     int error;
641
642     error = (netdev->netdev_class->get_in4
643              ? netdev->netdev_class->get_in4(netdev,
644                     &address, &netmask)
645              : EOPNOTSUPP);
646     if (address_) {
647         address_->s_addr = error ? 0 : address.s_addr;
648     }
649     if (netmask_) {
650         netmask_->s_addr = error ? 0 : netmask.s_addr;
651     }
652     return error;
653 }
654
655 /* Assigns 'addr' as 'netdev''s IPv4 address and 'mask' as its netmask.  If
656  * 'addr' is INADDR_ANY, 'netdev''s IPv4 address is cleared.  Returns a
657  * positive errno value. */
658 int
659 netdev_set_in4(struct netdev *netdev, struct in_addr addr, struct in_addr mask)
660 {
661     return (netdev->netdev_class->set_in4
662             ? netdev->netdev_class->set_in4(netdev, addr, mask)
663             : EOPNOTSUPP);
664 }
665
666 /* Obtains ad IPv4 address from device name and save the address in
667  * in4.  Returns 0 if successful, otherwise a positive errno value.
668  */
669 int
670 netdev_get_in4_by_name(const char *device_name, struct in_addr *in4)
671 {
672     struct netdev *netdev;
673     int error;
674
675     error = netdev_open(device_name, "system", &netdev);
676     if (error) {
677         in4->s_addr = htonl(0);
678         return error;
679     }
680
681     error = netdev_get_in4(netdev, in4, NULL);
682     netdev_close(netdev);
683     return error;
684 }
685
686 /* Adds 'router' as a default IP gateway for the TCP/IP stack that corresponds
687  * to 'netdev'. */
688 int
689 netdev_add_router(struct netdev *netdev, struct in_addr router)
690 {
691     COVERAGE_INC(netdev_add_router);
692     return (netdev->netdev_class->add_router
693             ? netdev->netdev_class->add_router(netdev, router)
694             : EOPNOTSUPP);
695 }
696
697 /* Looks up the next hop for 'host' for the TCP/IP stack that corresponds to
698  * 'netdev'.  If a route cannot not be determined, sets '*next_hop' to 0,
699  * '*netdev_name' to null, and returns a positive errno value.  Otherwise, if a
700  * next hop is found, stores the next hop gateway's address (0 if 'host' is on
701  * a directly connected network) in '*next_hop' and a copy of the name of the
702  * device to reach 'host' in '*netdev_name', and returns 0.  The caller is
703  * responsible for freeing '*netdev_name' (by calling free()). */
704 int
705 netdev_get_next_hop(const struct netdev *netdev,
706                     const struct in_addr *host, struct in_addr *next_hop,
707                     char **netdev_name)
708 {
709     int error = (netdev->netdev_class->get_next_hop
710                  ? netdev->netdev_class->get_next_hop(
711                         host, next_hop, netdev_name)
712                  : EOPNOTSUPP);
713     if (error) {
714         next_hop->s_addr = 0;
715         *netdev_name = NULL;
716     }
717     return error;
718 }
719
720 /* Populates 'smap' with status information.
721  *
722  * Populates 'smap' with 'netdev' specific status information.  This
723  * information may be used to populate the status column of the Interface table
724  * as defined in ovs-vswitchd.conf.db(5). */
725 int
726 netdev_get_status(const struct netdev *netdev, struct smap *smap)
727 {
728     return (netdev->netdev_class->get_status
729             ? netdev->netdev_class->get_status(netdev, smap)
730             : EOPNOTSUPP);
731 }
732
733 /* If 'netdev' has an assigned IPv6 address, sets '*in6' to that address and
734  * returns 0.  Otherwise, returns a positive errno value and sets '*in6' to
735  * all-zero-bits (in6addr_any).
736  *
737  * The following error values have well-defined meanings:
738  *
739  *   - EADDRNOTAVAIL: 'netdev' has no assigned IPv6 address.
740  *
741  *   - EOPNOTSUPP: No IPv6 network stack attached to 'netdev'.
742  *
743  * 'in6' may be null, in which case the address itself is not reported. */
744 int
745 netdev_get_in6(const struct netdev *netdev, struct in6_addr *in6)
746 {
747     struct in6_addr dummy;
748     int error;
749
750     error = (netdev->netdev_class->get_in6
751              ? netdev->netdev_class->get_in6(netdev,
752                     in6 ? in6 : &dummy)
753              : EOPNOTSUPP);
754     if (error && in6) {
755         memset(in6, 0, sizeof *in6);
756     }
757     return error;
758 }
759
760 /* On 'netdev', turns off the flags in 'off' and then turns on the flags in
761  * 'on'.  Returns 0 if successful, otherwise a positive errno value. */
762 static int
763 do_update_flags(struct netdev *netdev, enum netdev_flags off,
764                 enum netdev_flags on, enum netdev_flags *old_flagsp,
765                 struct netdev_saved_flags **sfp)
766 {
767     struct netdev_saved_flags *sf = NULL;
768     enum netdev_flags old_flags;
769     int error;
770
771     error = netdev->netdev_class->update_flags(netdev, off & ~on, on,
772                                                &old_flags);
773     if (error) {
774         VLOG_WARN_RL(&rl, "failed to %s flags for network device %s: %s",
775                      off || on ? "set" : "get", netdev_get_name(netdev),
776                      strerror(error));
777         old_flags = 0;
778     } else if ((off || on) && sfp) {
779         enum netdev_flags new_flags = (old_flags & ~off) | on;
780         enum netdev_flags changed_flags = old_flags ^ new_flags;
781         if (changed_flags) {
782             *sfp = sf = xmalloc(sizeof *sf);
783             sf->netdev = netdev;
784             list_push_front(&netdev->saved_flags_list, &sf->node);
785             sf->saved_flags = changed_flags;
786             sf->saved_values = changed_flags & new_flags;
787
788             netdev->ref_cnt++;
789         }
790     }
791
792     if (old_flagsp) {
793         *old_flagsp = old_flags;
794     }
795     if (sfp) {
796         *sfp = sf;
797     }
798
799     return error;
800 }
801
802 /* Obtains the current flags for 'netdev' and stores them into '*flagsp'.
803  * Returns 0 if successful, otherwise a positive errno value.  On failure,
804  * stores 0 into '*flagsp'. */
805 int
806 netdev_get_flags(const struct netdev *netdev_, enum netdev_flags *flagsp)
807 {
808     struct netdev *netdev = CONST_CAST(struct netdev *, netdev_);
809     return do_update_flags(netdev, 0, 0, flagsp, NULL);
810 }
811
812 /* Sets the flags for 'netdev' to 'flags'.
813  * Returns 0 if successful, otherwise a positive errno value. */
814 int
815 netdev_set_flags(struct netdev *netdev, enum netdev_flags flags,
816                  struct netdev_saved_flags **sfp)
817 {
818     return do_update_flags(netdev, -1, flags, NULL, sfp);
819 }
820
821 /* Turns on the specified 'flags' on 'netdev':
822  *
823  *    - On success, returns 0.  If 'sfp' is nonnull, sets '*sfp' to a newly
824  *      allocated 'struct netdev_saved_flags *' that may be passed to
825  *      netdev_restore_flags() to restore the original values of 'flags' on
826  *      'netdev' (this will happen automatically at program termination if
827  *      netdev_restore_flags() is never called) , or to NULL if no flags were
828  *      actually changed.
829  *
830  *    - On failure, returns a positive errno value.  If 'sfp' is nonnull, sets
831  *      '*sfp' to NULL. */
832 int
833 netdev_turn_flags_on(struct netdev *netdev, enum netdev_flags flags,
834                      struct netdev_saved_flags **sfp)
835 {
836     return do_update_flags(netdev, 0, flags, NULL, sfp);
837 }
838
839 /* Turns off the specified 'flags' on 'netdev'.  See netdev_turn_flags_on() for
840  * details of the interface. */
841 int
842 netdev_turn_flags_off(struct netdev *netdev, enum netdev_flags flags,
843                       struct netdev_saved_flags **sfp)
844 {
845     return do_update_flags(netdev, flags, 0, NULL, sfp);
846 }
847
848 /* Restores the flags that were saved in 'sf', and destroys 'sf'.
849  * Does nothing if 'sf' is NULL. */
850 void
851 netdev_restore_flags(struct netdev_saved_flags *sf)
852 {
853     if (sf) {
854         struct netdev *netdev = sf->netdev;
855         enum netdev_flags old_flags;
856
857         netdev->netdev_class->update_flags(netdev,
858                                            sf->saved_flags & sf->saved_values,
859                                            sf->saved_flags & ~sf->saved_values,
860                                            &old_flags);
861         list_remove(&sf->node);
862         free(sf);
863
864         netdev_unref(netdev);
865     }
866 }
867
868 /* Looks up the ARP table entry for 'ip' on 'netdev'.  If one exists and can be
869  * successfully retrieved, it stores the corresponding MAC address in 'mac' and
870  * returns 0.  Otherwise, it returns a positive errno value; in particular,
871  * ENXIO indicates that there is no ARP table entry for 'ip' on 'netdev'. */
872 int
873 netdev_arp_lookup(const struct netdev *netdev,
874                   ovs_be32 ip, uint8_t mac[ETH_ADDR_LEN])
875 {
876     int error = (netdev->netdev_class->arp_lookup
877                  ? netdev->netdev_class->arp_lookup(netdev,
878                         ip, mac)
879                  : EOPNOTSUPP);
880     if (error) {
881         memset(mac, 0, ETH_ADDR_LEN);
882     }
883     return error;
884 }
885
886 /* Returns true if carrier is active (link light is on) on 'netdev'. */
887 bool
888 netdev_get_carrier(const struct netdev *netdev)
889 {
890     int error;
891     enum netdev_flags flags;
892     bool carrier;
893
894     netdev_get_flags(netdev, &flags);
895     if (!(flags & NETDEV_UP)) {
896         return false;
897     }
898
899     if (!netdev->netdev_class->get_carrier) {
900         return true;
901     }
902
903     error = netdev->netdev_class->get_carrier(netdev,
904                                                               &carrier);
905     if (error) {
906         VLOG_DBG("%s: failed to get network device carrier status, assuming "
907                  "down: %s", netdev_get_name(netdev), strerror(error));
908         carrier = false;
909     }
910
911     return carrier;
912 }
913
914 /* Returns the number of times 'netdev''s carrier has changed. */
915 long long int
916 netdev_get_carrier_resets(const struct netdev *netdev)
917 {
918     return (netdev->netdev_class->get_carrier_resets
919             ? netdev->netdev_class->get_carrier_resets(netdev)
920             : 0);
921 }
922
923 /* Attempts to force netdev_get_carrier() to poll 'netdev''s MII registers for
924  * link status instead of checking 'netdev''s carrier.  'netdev''s MII
925  * registers will be polled once ever 'interval' milliseconds.  If 'netdev'
926  * does not support MII, another method may be used as a fallback.  If
927  * 'interval' is less than or equal to zero, reverts netdev_get_carrier() to
928  * its normal behavior.
929  *
930  * Returns 0 if successful, otherwise a positive errno value. */
931 int
932 netdev_set_miimon_interval(struct netdev *netdev, long long int interval)
933 {
934     return (netdev->netdev_class->set_miimon_interval
935             ? netdev->netdev_class->set_miimon_interval(netdev, interval)
936             : EOPNOTSUPP);
937 }
938
939 /* Retrieves current device stats for 'netdev'. */
940 int
941 netdev_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
942 {
943     int error;
944
945     COVERAGE_INC(netdev_get_stats);
946     error = (netdev->netdev_class->get_stats
947              ? netdev->netdev_class->get_stats(netdev, stats)
948              : EOPNOTSUPP);
949     if (error) {
950         memset(stats, 0xff, sizeof *stats);
951     }
952     return error;
953 }
954
955 /* Attempts to change the stats for 'netdev' to those provided in 'stats'.
956  * Returns 0 if successful, otherwise a positive errno value.
957  *
958  * This will probably fail for most network devices.  Some devices might only
959  * allow setting their stats to 0. */
960 int
961 netdev_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
962 {
963     return (netdev->netdev_class->set_stats
964              ? netdev->netdev_class->set_stats(netdev, stats)
965              : EOPNOTSUPP);
966 }
967
968 /* Attempts to set input rate limiting (policing) policy, such that up to
969  * 'kbits_rate' kbps of traffic is accepted, with a maximum accumulative burst
970  * size of 'kbits' kb. */
971 int
972 netdev_set_policing(struct netdev *netdev, uint32_t kbits_rate,
973                     uint32_t kbits_burst)
974 {
975     return (netdev->netdev_class->set_policing
976             ? netdev->netdev_class->set_policing(netdev,
977                     kbits_rate, kbits_burst)
978             : EOPNOTSUPP);
979 }
980
981 /* Adds to 'types' all of the forms of QoS supported by 'netdev', or leaves it
982  * empty if 'netdev' does not support QoS.  Any names added to 'types' should
983  * be documented as valid for the "type" column in the "QoS" table in
984  * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
985  *
986  * Every network device supports disabling QoS with a type of "", but this type
987  * will not be added to 'types'.
988  *
989  * The caller must initialize 'types' (e.g. with sset_init()) before calling
990  * this function.  The caller is responsible for destroying 'types' (e.g. with
991  * sset_destroy()) when it is no longer needed.
992  *
993  * Returns 0 if successful, otherwise a positive errno value. */
994 int
995 netdev_get_qos_types(const struct netdev *netdev, struct sset *types)
996 {
997     const struct netdev_class *class = netdev->netdev_class;
998     return (class->get_qos_types
999             ? class->get_qos_types(netdev, types)
1000             : 0);
1001 }
1002
1003 /* Queries 'netdev' for its capabilities regarding the specified 'type' of QoS,
1004  * which should be "" or one of the types returned by netdev_get_qos_types()
1005  * for 'netdev'.  Returns 0 if successful, otherwise a positive errno value.
1006  * On success, initializes 'caps' with the QoS capabilities; on failure, clears
1007  * 'caps' to all zeros. */
1008 int
1009 netdev_get_qos_capabilities(const struct netdev *netdev, const char *type,
1010                             struct netdev_qos_capabilities *caps)
1011 {
1012     const struct netdev_class *class = netdev->netdev_class;
1013
1014     if (*type) {
1015         int retval = (class->get_qos_capabilities
1016                       ? class->get_qos_capabilities(netdev, type, caps)
1017                       : EOPNOTSUPP);
1018         if (retval) {
1019             memset(caps, 0, sizeof *caps);
1020         }
1021         return retval;
1022     } else {
1023         /* Every netdev supports turning off QoS. */
1024         memset(caps, 0, sizeof *caps);
1025         return 0;
1026     }
1027 }
1028
1029 /* Obtains the number of queues supported by 'netdev' for the specified 'type'
1030  * of QoS.  Returns 0 if successful, otherwise a positive errno value.  Stores
1031  * the number of queues (zero on failure) in '*n_queuesp'.
1032  *
1033  * This is just a simple wrapper around netdev_get_qos_capabilities(). */
1034 int
1035 netdev_get_n_queues(const struct netdev *netdev,
1036                     const char *type, unsigned int *n_queuesp)
1037 {
1038     struct netdev_qos_capabilities caps;
1039     int retval;
1040
1041     retval = netdev_get_qos_capabilities(netdev, type, &caps);
1042     *n_queuesp = caps.n_queues;
1043     return retval;
1044 }
1045
1046 /* Queries 'netdev' about its currently configured form of QoS.  If successful,
1047  * stores the name of the current form of QoS into '*typep', stores any details
1048  * of configuration as string key-value pairs in 'details', and returns 0.  On
1049  * failure, sets '*typep' to NULL and returns a positive errno value.
1050  *
1051  * A '*typep' of "" indicates that QoS is currently disabled on 'netdev'.
1052  *
1053  * The caller must initialize 'details' as an empty smap (e.g. with
1054  * smap_init()) before calling this function.  The caller must free 'details'
1055  * when it is no longer needed (e.g. with smap_destroy()).
1056  *
1057  * The caller must not modify or free '*typep'.
1058  *
1059  * '*typep' will be one of the types returned by netdev_get_qos_types() for
1060  * 'netdev'.  The contents of 'details' should be documented as valid for
1061  * '*typep' in the "other_config" column in the "QoS" table in
1062  * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)). */
1063 int
1064 netdev_get_qos(const struct netdev *netdev,
1065                const char **typep, struct smap *details)
1066 {
1067     const struct netdev_class *class = netdev->netdev_class;
1068     int retval;
1069
1070     if (class->get_qos) {
1071         retval = class->get_qos(netdev, typep, details);
1072         if (retval) {
1073             *typep = NULL;
1074             smap_clear(details);
1075         }
1076         return retval;
1077     } else {
1078         /* 'netdev' doesn't support QoS, so report that QoS is disabled. */
1079         *typep = "";
1080         return 0;
1081     }
1082 }
1083
1084 /* Attempts to reconfigure QoS on 'netdev', changing the form of QoS to 'type'
1085  * with details of configuration from 'details'.  Returns 0 if successful,
1086  * otherwise a positive errno value.  On error, the previous QoS configuration
1087  * is retained.
1088  *
1089  * When this function changes the type of QoS (not just 'details'), this also
1090  * resets all queue configuration for 'netdev' to their defaults (which depend
1091  * on the specific type of QoS).  Otherwise, the queue configuration for
1092  * 'netdev' is unchanged.
1093  *
1094  * 'type' should be "" (to disable QoS) or one of the types returned by
1095  * netdev_get_qos_types() for 'netdev'.  The contents of 'details' should be
1096  * documented as valid for the given 'type' in the "other_config" column in the
1097  * "QoS" table in vswitchd/vswitch.xml (which is built as
1098  * ovs-vswitchd.conf.db(8)).
1099  *
1100  * NULL may be specified for 'details' if there are no configuration
1101  * details. */
1102 int
1103 netdev_set_qos(struct netdev *netdev,
1104                const char *type, const struct smap *details)
1105 {
1106     const struct netdev_class *class = netdev->netdev_class;
1107
1108     if (!type) {
1109         type = "";
1110     }
1111
1112     if (class->set_qos) {
1113         if (!details) {
1114             static const struct smap empty = SMAP_INITIALIZER(&empty);
1115             details = &empty;
1116         }
1117         return class->set_qos(netdev, type, details);
1118     } else {
1119         return *type ? EOPNOTSUPP : 0;
1120     }
1121 }
1122
1123 /* Queries 'netdev' for information about the queue numbered 'queue_id'.  If
1124  * successful, adds that information as string key-value pairs to 'details'.
1125  * Returns 0 if successful, otherwise a positive errno value.
1126  *
1127  * 'queue_id' must be less than the number of queues supported by 'netdev' for
1128  * the current form of QoS (e.g. as returned by netdev_get_n_queues(netdev)).
1129  *
1130  * The returned contents of 'details' should be documented as valid for the
1131  * given 'type' in the "other_config" column in the "Queue" table in
1132  * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
1133  *
1134  * The caller must initialize 'details' (e.g. with smap_init()) before calling
1135  * this function.  The caller must free 'details' when it is no longer needed
1136  * (e.g. with smap_destroy()). */
1137 int
1138 netdev_get_queue(const struct netdev *netdev,
1139                  unsigned int queue_id, struct smap *details)
1140 {
1141     const struct netdev_class *class = netdev->netdev_class;
1142     int retval;
1143
1144     retval = (class->get_queue
1145               ? class->get_queue(netdev, queue_id, details)
1146               : EOPNOTSUPP);
1147     if (retval) {
1148         smap_clear(details);
1149     }
1150     return retval;
1151 }
1152
1153 /* Configures the queue numbered 'queue_id' on 'netdev' with the key-value
1154  * string pairs in 'details'.  The contents of 'details' should be documented
1155  * as valid for the given 'type' in the "other_config" column in the "Queue"
1156  * table in vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
1157  * Returns 0 if successful, otherwise a positive errno value.  On failure, the
1158  * given queue's configuration should be unmodified.
1159  *
1160  * 'queue_id' must be less than the number of queues supported by 'netdev' for
1161  * the current form of QoS (e.g. as returned by netdev_get_n_queues(netdev)).
1162  *
1163  * This function does not modify 'details', and the caller retains ownership of
1164  * it. */
1165 int
1166 netdev_set_queue(struct netdev *netdev,
1167                  unsigned int queue_id, const struct smap *details)
1168 {
1169     const struct netdev_class *class = netdev->netdev_class;
1170     return (class->set_queue
1171             ? class->set_queue(netdev, queue_id, details)
1172             : EOPNOTSUPP);
1173 }
1174
1175 /* Attempts to delete the queue numbered 'queue_id' from 'netdev'.  Some kinds
1176  * of QoS may have a fixed set of queues, in which case attempts to delete them
1177  * will fail with EOPNOTSUPP.
1178  *
1179  * Returns 0 if successful, otherwise a positive errno value.  On failure, the
1180  * given queue will be unmodified.
1181  *
1182  * 'queue_id' must be less than the number of queues supported by 'netdev' for
1183  * the current form of QoS (e.g. as returned by
1184  * netdev_get_n_queues(netdev)). */
1185 int
1186 netdev_delete_queue(struct netdev *netdev, unsigned int queue_id)
1187 {
1188     const struct netdev_class *class = netdev->netdev_class;
1189     return (class->delete_queue
1190             ? class->delete_queue(netdev, queue_id)
1191             : EOPNOTSUPP);
1192 }
1193
1194 /* Obtains statistics about 'queue_id' on 'netdev'.  On success, returns 0 and
1195  * fills 'stats' with the queue's statistics; individual members of 'stats' may
1196  * be set to all-1-bits if the statistic is unavailable.  On failure, returns a
1197  * positive errno value and fills 'stats' with all-1-bits. */
1198 int
1199 netdev_get_queue_stats(const struct netdev *netdev, unsigned int queue_id,
1200                        struct netdev_queue_stats *stats)
1201 {
1202     const struct netdev_class *class = netdev->netdev_class;
1203     int retval;
1204
1205     retval = (class->get_queue_stats
1206               ? class->get_queue_stats(netdev, queue_id, stats)
1207               : EOPNOTSUPP);
1208     if (retval) {
1209         memset(stats, 0xff, sizeof *stats);
1210     }
1211     return retval;
1212 }
1213
1214 /* Iterates over all of 'netdev''s queues, calling 'cb' with the queue's ID,
1215  * its configuration, and the 'aux' specified by the caller.  The order of
1216  * iteration is unspecified, but (when successful) each queue is visited
1217  * exactly once.
1218  *
1219  * Calling this function may be more efficient than calling netdev_get_queue()
1220  * for every queue.
1221  *
1222  * 'cb' must not modify or free the 'details' argument passed in.  It may
1223  * delete or modify the queue passed in as its 'queue_id' argument.  It may
1224  * modify but must not delete any other queue within 'netdev'.  'cb' should not
1225  * add new queues because this may cause some queues to be visited twice or not
1226  * at all.
1227  *
1228  * Returns 0 if successful, otherwise a positive errno value.  On error, some
1229  * configured queues may not have been included in the iteration. */
1230 int
1231 netdev_dump_queues(const struct netdev *netdev,
1232                    netdev_dump_queues_cb *cb, void *aux)
1233 {
1234     const struct netdev_class *class = netdev->netdev_class;
1235     return (class->dump_queues
1236             ? class->dump_queues(netdev, cb, aux)
1237             : EOPNOTSUPP);
1238 }
1239
1240 /* Iterates over all of 'netdev''s queues, calling 'cb' with the queue's ID,
1241  * its statistics, and the 'aux' specified by the caller.  The order of
1242  * iteration is unspecified, but (when successful) each queue is visited
1243  * exactly once.
1244  *
1245  * Calling this function may be more efficient than calling
1246  * netdev_get_queue_stats() for every queue.
1247  *
1248  * 'cb' must not modify or free the statistics passed in.
1249  *
1250  * Returns 0 if successful, otherwise a positive errno value.  On error, some
1251  * configured queues may not have been included in the iteration. */
1252 int
1253 netdev_dump_queue_stats(const struct netdev *netdev,
1254                         netdev_dump_queue_stats_cb *cb, void *aux)
1255 {
1256     const struct netdev_class *class = netdev->netdev_class;
1257     return (class->dump_queue_stats
1258             ? class->dump_queue_stats(netdev, cb, aux)
1259             : EOPNOTSUPP);
1260 }
1261
1262 /* Returns a sequence number which indicates changes in one of 'netdev''s
1263  * properties.  The returned sequence will be nonzero so that callers have a
1264  * value which they may use as a reset when tracking 'netdev'.
1265  *
1266  * The returned sequence number will change whenever 'netdev''s flags,
1267  * features, ethernet address, or carrier changes.  It may change for other
1268  * reasons as well, or no reason at all. */
1269 unsigned int
1270 netdev_change_seq(const struct netdev *netdev)
1271 {
1272     return netdev->netdev_class->change_seq(netdev);
1273 }
1274 \f
1275 /* Initializes 'netdev' as a netdev device named 'name' of the specified
1276  * 'netdev_class'.  This function is ordinarily called from a netdev provider's
1277  * 'create' function.
1278  *
1279  * This function adds 'netdev' to a netdev-owned shash, so it is very important
1280  * that 'netdev' only be freed after calling netdev_uninit().  */
1281 void
1282 netdev_init(struct netdev *netdev, const char *name,
1283             const struct netdev_class *netdev_class)
1284 {
1285     ovs_assert(!shash_find(&netdev_shash, name));
1286
1287     memset(netdev, 0, sizeof *netdev);
1288     netdev->netdev_class = netdev_class;
1289     netdev->name = xstrdup(name);
1290     netdev->node = shash_add(&netdev_shash, name, netdev);
1291     list_init(&netdev->saved_flags_list);
1292 }
1293
1294 /* Undoes the results of initialization.
1295  *
1296  * Normally this function does not need to be called as netdev_close has
1297  * the same effect when the refcount drops to zero.
1298  * However, it may be called by providers due to an error on creation
1299  * that occurs after initialization.  It this case netdev_close() would
1300  * never be called. */
1301 void
1302 netdev_uninit(struct netdev *netdev, bool destroy)
1303 {
1304     char *name = netdev->name;
1305
1306     ovs_assert(!netdev->ref_cnt);
1307     ovs_assert(list_is_empty(&netdev->saved_flags_list));
1308
1309     shash_delete(&netdev_shash, netdev->node);
1310
1311     if (destroy) {
1312         netdev->netdev_class->destroy(netdev);
1313     }
1314     free(name);
1315 }
1316
1317 /* Returns the class type of 'netdev'.
1318  *
1319  * The caller must not free the returned value. */
1320 const char *
1321 netdev_get_type(const struct netdev *netdev)
1322 {
1323     return netdev->netdev_class->type;
1324 }
1325
1326 /* Returns the class associated with 'netdev'. */
1327 const struct netdev_class *
1328 netdev_get_class(const struct netdev *netdev)
1329 {
1330     return netdev->netdev_class;
1331 }
1332
1333 /* Returns the netdev with 'name' or NULL if there is none.
1334  *
1335  * The caller must not free the returned value. */
1336 struct netdev *
1337 netdev_from_name(const char *name)
1338 {
1339     return shash_find_data(&netdev_shash, name);
1340 }
1341
1342 /* Fills 'device_list' with devices that match 'netdev_class'.
1343  *
1344  * The caller is responsible for initializing and destroying 'device_list'
1345  * but the contained netdevs must not be freed. */
1346 void
1347 netdev_get_devices(const struct netdev_class *netdev_class,
1348                    struct shash *device_list)
1349 {
1350     struct shash_node *node;
1351     SHASH_FOR_EACH (node, &netdev_shash) {
1352         struct netdev *dev = node->data;
1353
1354         if (dev->netdev_class == netdev_class) {
1355             shash_add(device_list, node->name, node->data);
1356         }
1357     }
1358 }
1359
1360 const char *
1361 netdev_get_type_from_name(const char *name)
1362 {
1363     const struct netdev *dev = netdev_from_name(name);
1364     return dev ? netdev_get_type(dev) : NULL;
1365 }
1366 \f
1367 void
1368 netdev_rx_init(struct netdev_rx *rx, struct netdev *netdev,
1369                const struct netdev_rx_class *class)
1370 {
1371     ovs_assert(netdev->ref_cnt > 0);
1372     rx->rx_class = class;
1373     rx->netdev = netdev;
1374 }
1375
1376 void
1377 netdev_rx_uninit(struct netdev_rx *rx OVS_UNUSED)
1378 {
1379     /* Nothing to do. */
1380 }
1381
1382 struct netdev *
1383 netdev_rx_get_netdev(const struct netdev_rx *rx)
1384 {
1385     ovs_assert(rx->netdev->ref_cnt > 0);
1386     return rx->netdev;
1387 }
1388
1389 const char *
1390 netdev_rx_get_name(const struct netdev_rx *rx)
1391 {
1392     return netdev_get_name(netdev_rx_get_netdev(rx));
1393 }
1394
1395 static void
1396 restore_all_flags(void *aux OVS_UNUSED)
1397 {
1398     struct shash_node *node;
1399
1400     SHASH_FOR_EACH (node, &netdev_shash) {
1401         struct netdev *netdev = node->data;
1402         const struct netdev_saved_flags *sf;
1403         enum netdev_flags saved_values;
1404         enum netdev_flags saved_flags;
1405
1406         saved_values = saved_flags = 0;
1407         LIST_FOR_EACH (sf, node, &netdev->saved_flags_list) {
1408             saved_flags |= sf->saved_flags;
1409             saved_values &= ~sf->saved_flags;
1410             saved_values |= sf->saved_flags & sf->saved_values;
1411         }
1412         if (saved_flags) {
1413             enum netdev_flags old_flags;
1414
1415             netdev->netdev_class->update_flags(netdev,
1416                                                saved_flags & saved_values,
1417                                                saved_flags & ~saved_values,
1418                                                &old_flags);
1419         }
1420     }
1421 }