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