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