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