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