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