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