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