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