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