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