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