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