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