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