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