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