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