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