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