]> git.proxmox.com Git - mirror_ovs.git/blame - lib/netdev.c
ofp-util: Fix indirect group delete message with no buckets.
[mirror_ovs.git] / lib / netdev.c
CommitLineData
064af421 1/*
7dbd520e 2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2016 Nicira, Inc.
064af421 3 *
a14bc59f
BP
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:
064af421 7 *
a14bc59f
BP
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.
064af421
BP
15 */
16
17#include <config.h>
18#include "netdev.h"
19
064af421 20#include <errno.h>
064af421 21#include <inttypes.h>
064af421
BP
22#include <netinet/in.h>
23#include <stdlib.h>
24#include <string.h>
25#include <unistd.h>
26
a8704b50
PS
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
064af421 34#include "coverage.h"
94a53842 35#include "dpif.h"
cf62fa4c 36#include "dp-packet.h"
3e8a2ad1 37#include "openvswitch/dynamic-string.h"
064af421 38#include "fatal-signal.h"
149f577a 39#include "hash.h"
b19bab5b 40#include "openvswitch/list.h"
8a9562d2 41#include "netdev-dpdk.h"
8b61709d 42#include "netdev-provider.h"
2b9d6589 43#include "netdev-vport.h"
d625fbd1 44#include "odp-netlink.h"
622ee2cf 45#include "openflow/openflow.h"
064af421
BP
46#include "packets.h"
47#include "poll-loop.h"
a36de779 48#include "seq.h"
e9e28be3 49#include "shash.h"
79f1cbe9 50#include "smap.h"
b3c01ed3 51#include "sset.h"
064af421 52#include "svec.h"
e6211adc 53#include "openvswitch/vlog.h"
c876a4bb 54#include "flow.h"
a8704b50 55#include "util.h"
064af421 56
d98e6007 57VLOG_DEFINE_THIS_MODULE(netdev);
5136ce49 58
d76f09ea
BP
59COVERAGE_DEFINE(netdev_received);
60COVERAGE_DEFINE(netdev_sent);
61COVERAGE_DEFINE(netdev_add_router);
62COVERAGE_DEFINE(netdev_get_stats);
63
4b609110 64struct netdev_saved_flags {
b5d57fc8 65 struct netdev *netdev;
ca6ba700 66 struct ovs_list node; /* In struct netdev's saved_flags_list. */
4b609110
BP
67 enum netdev_flags saved_flags;
68 enum netdev_flags saved_values;
69};
70
86383816
BP
71/* Protects 'netdev_shash' and the mutable members of struct netdev. */
72static struct ovs_mutex netdev_mutex = OVS_MUTEX_INITIALIZER;
064af421 73
6c88d577 74/* All created network devices. */
86383816
BP
75static struct shash netdev_shash OVS_GUARDED_BY(netdev_mutex)
76 = SHASH_INITIALIZER(&netdev_shash);
77
78/* Protects 'netdev_classes' against insertions or deletions.
79 *
da695497
BP
80 * This is a recursive mutex to allow recursive acquisition when calling into
81 * providers. For example, netdev_run() calls into provider 'run' functions,
82 * which might reasonably want to call one of the netdev functions that takes
83 * netdev_class_mutex. */
84static struct ovs_mutex netdev_class_mutex OVS_ACQ_BEFORE(netdev_mutex);
86383816
BP
85
86/* Contains 'struct netdev_registered_class'es. */
da695497 87static struct hmap netdev_classes OVS_GUARDED_BY(netdev_class_mutex)
86383816
BP
88 = HMAP_INITIALIZER(&netdev_classes);
89
90struct netdev_registered_class {
fd0318be
JR
91 /* In 'netdev_classes', by class->type. */
92 struct hmap_node hmap_node OVS_GUARDED_BY(netdev_class_mutex);
93 const struct netdev_class *class OVS_GUARDED_BY(netdev_class_mutex);
94 /* Number of 'struct netdev's of this class. */
95 int ref_cnt OVS_GUARDED_BY(netdev_class_mutex);
86383816 96};
064af421 97
064af421
BP
98/* This is set pretty low because we probably won't learn anything from the
99 * additional log messages. */
100static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
101
4b609110 102static void restore_all_flags(void *aux OVS_UNUSED);
b5d57fc8 103void update_device_args(struct netdev *, const struct shash *args);
8b61709d 104
f00fa8cb
AW
105int
106netdev_n_txq(const struct netdev *netdev)
107{
108 return netdev->n_txq;
109}
110
55c955bd
PS
111int
112netdev_n_rxq(const struct netdev *netdev)
113{
114 return netdev->n_rxq;
115}
116
a14b8947
IM
117int
118netdev_requested_n_rxq(const struct netdev *netdev)
119{
120 return netdev->requested_n_rxq;
121}
122
e4cfed38
PS
123bool
124netdev_is_pmd(const struct netdev *netdev)
125{
118c77b1 126 return netdev->netdev_class->is_pmd;
e4cfed38
PS
127}
128
77909859 129static void
a4fdb0f3 130netdev_class_mutex_initialize(void)
da695497 131 OVS_EXCLUDED(netdev_class_mutex, netdev_mutex)
064af421 132{
86383816 133 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
8b61709d 134
86383816 135 if (ovsthread_once_start(&once)) {
da695497 136 ovs_mutex_init_recursive(&netdev_class_mutex);
a4fdb0f3
GS
137 ovsthread_once_done(&once);
138 }
139}
140
141static void
142netdev_initialize(void)
143 OVS_EXCLUDED(netdev_class_mutex, netdev_mutex)
144{
145 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
146
147 if (ovsthread_once_start(&once)) {
148 netdev_class_mutex_initialize();
da695497 149
4b609110 150 fatal_signal_add_hook(restore_all_flags, NULL, NULL, true);
c060c4cf 151 netdev_vport_patch_register();
064af421 152
2f51a7eb 153#ifdef __linux__
2b9d6589 154 netdev_register_provider(&netdev_linux_class);
c3827f61 155 netdev_register_provider(&netdev_internal_class);
2b9d6589 156 netdev_register_provider(&netdev_tap_class);
c060c4cf 157 netdev_vport_tunnel_register();
f6eb6b20 158#endif
666afb55 159#if defined(__FreeBSD__) || defined(__NetBSD__)
f6eb6b20
GL
160 netdev_register_provider(&netdev_tap_class);
161 netdev_register_provider(&netdev_bsd_class);
078eedf4
NR
162#endif
163#ifdef _WIN32
164 netdev_register_provider(&netdev_windows_class);
165 netdev_register_provider(&netdev_internal_class);
166 netdev_vport_tunnel_register();
2b9d6589 167#endif
8a9562d2 168 netdev_dpdk_register();
86383816
BP
169
170 ovsthread_once_done(&once);
064af421 171 }
064af421
BP
172}
173
8b61709d
BP
174/* Performs periodic work needed by all the various kinds of netdevs.
175 *
176 * If your program opens any netdevs, it must call this function within its
177 * main poll loop. */
178void
179netdev_run(void)
da695497 180 OVS_EXCLUDED(netdev_class_mutex, netdev_mutex)
064af421 181{
86383816
BP
182 struct netdev_registered_class *rc;
183
aaea735b 184 netdev_initialize();
da695497 185 ovs_mutex_lock(&netdev_class_mutex);
86383816 186 HMAP_FOR_EACH (rc, hmap_node, &netdev_classes) {
3fd6ef29
EJ
187 if (rc->class->run) {
188 rc->class->run();
189 }
064af421 190 }
da695497 191 ovs_mutex_unlock(&netdev_class_mutex);
064af421
BP
192}
193
8b61709d
BP
194/* Arranges for poll_block() to wake up when netdev_run() needs to be called.
195 *
196 * If your program opens any netdevs, it must call this function within its
197 * main poll loop. */
198void
199netdev_wait(void)
da695497 200 OVS_EXCLUDED(netdev_class_mutex, netdev_mutex)
064af421 201{
86383816
BP
202 struct netdev_registered_class *rc;
203
da695497 204 ovs_mutex_lock(&netdev_class_mutex);
86383816 205 HMAP_FOR_EACH (rc, hmap_node, &netdev_classes) {
3fd6ef29
EJ
206 if (rc->class->wait) {
207 rc->class->wait();
208 }
86383816 209 }
da695497 210 ovs_mutex_unlock(&netdev_class_mutex);
86383816
BP
211}
212
213static struct netdev_registered_class *
214netdev_lookup_class(const char *type)
da695497 215 OVS_REQ_RDLOCK(netdev_class_mutex)
86383816
BP
216{
217 struct netdev_registered_class *rc;
218
219 HMAP_FOR_EACH_WITH_HASH (rc, hmap_node, hash_string(type, 0),
220 &netdev_classes) {
221 if (!strcmp(type, rc->class->type)) {
222 return rc;
8b61709d 223 }
064af421 224 }
86383816 225 return NULL;
064af421
BP
226}
227
77909859
JG
228/* Initializes and registers a new netdev provider. After successful
229 * registration, new netdevs of that type can be opened using netdev_open(). */
230int
231netdev_register_provider(const struct netdev_class *new_class)
da695497 232 OVS_EXCLUDED(netdev_class_mutex, netdev_mutex)
77909859 233{
86383816
BP
234 int error;
235
a4fdb0f3 236 netdev_class_mutex_initialize();
da695497 237 ovs_mutex_lock(&netdev_class_mutex);
86383816 238 if (netdev_lookup_class(new_class->type)) {
77909859
JG
239 VLOG_WARN("attempted to register duplicate netdev provider: %s",
240 new_class->type);
86383816
BP
241 error = EEXIST;
242 } else {
243 error = new_class->init ? new_class->init() : 0;
244 if (!error) {
245 struct netdev_registered_class *rc;
246
247 rc = xmalloc(sizeof *rc);
248 hmap_insert(&netdev_classes, &rc->hmap_node,
249 hash_string(new_class->type, 0));
250 rc->class = new_class;
fd0318be 251 rc->ref_cnt = 0;
86383816 252 } else {
77909859 253 VLOG_ERR("failed to initialize %s network device class: %s",
10a89ef0 254 new_class->type, ovs_strerror(error));
77909859
JG
255 }
256 }
da695497 257 ovs_mutex_unlock(&netdev_class_mutex);
77909859 258
86383816 259 return error;
77909859
JG
260}
261
262/* Unregisters a netdev provider. 'type' must have been previously
263 * registered and not currently be in use by any netdevs. After unregistration
264 * new netdevs of that type cannot be opened using netdev_open(). */
265int
266netdev_unregister_provider(const char *type)
da695497 267 OVS_EXCLUDED(netdev_class_mutex, netdev_mutex)
77909859 268{
86383816
BP
269 struct netdev_registered_class *rc;
270 int error;
77909859 271
7a82d305
BP
272 netdev_initialize();
273
da695497 274 ovs_mutex_lock(&netdev_class_mutex);
86383816
BP
275 rc = netdev_lookup_class(type);
276 if (!rc) {
77909859
JG
277 VLOG_WARN("attempted to unregister a netdev provider that is not "
278 "registered: %s", type);
86383816
BP
279 error = EAFNOSUPPORT;
280 } else {
fd0318be 281 if (!rc->ref_cnt) {
86383816
BP
282 hmap_remove(&netdev_classes, &rc->hmap_node);
283 free(rc);
284 error = 0;
285 } else {
77909859
JG
286 VLOG_WARN("attempted to unregister in use netdev provider: %s",
287 type);
86383816 288 error = EBUSY;
77909859
JG
289 }
290 }
da695497 291 ovs_mutex_unlock(&netdev_class_mutex);
77909859 292
86383816 293 return error;
c3827f61
BP
294}
295
77909859 296/* Clears 'types' and enumerates the types of all currently registered netdev
19993ef3 297 * providers into it. The caller must first initialize the sset. */
77909859 298void
19993ef3 299netdev_enumerate_types(struct sset *types)
86383816 300 OVS_EXCLUDED(netdev_mutex)
77909859 301{
86383816 302 struct netdev_registered_class *rc;
77909859
JG
303
304 netdev_initialize();
19993ef3 305 sset_clear(types);
77909859 306
da695497 307 ovs_mutex_lock(&netdev_class_mutex);
86383816
BP
308 HMAP_FOR_EACH (rc, hmap_node, &netdev_classes) {
309 sset_add(types, rc->class->type);
77909859 310 }
da695497 311 ovs_mutex_unlock(&netdev_class_mutex);
77909859
JG
312}
313
94a53842
AW
314/* Check that the network device name is not the same as any of the registered
315 * vport providers' dpif_port name (dpif_port is NULL if the vport provider
316 * does not define it) or the datapath internal port name (e.g. ovs-system).
317 *
318 * Returns true if there is a name conflict, false otherwise. */
319bool
320netdev_is_reserved_name(const char *name)
86383816 321 OVS_EXCLUDED(netdev_mutex)
94a53842 322{
86383816 323 struct netdev_registered_class *rc;
94a53842
AW
324
325 netdev_initialize();
86383816 326
da695497 327 ovs_mutex_lock(&netdev_class_mutex);
86383816
BP
328 HMAP_FOR_EACH (rc, hmap_node, &netdev_classes) {
329 const char *dpif_port = netdev_vport_class_get_dpif_port(rc->class);
a5d4fadd 330 if (dpif_port && !strncmp(name, dpif_port, strlen(dpif_port))) {
da695497 331 ovs_mutex_unlock(&netdev_class_mutex);
94a53842
AW
332 return true;
333 }
334 }
da695497 335 ovs_mutex_unlock(&netdev_class_mutex);
94a53842
AW
336
337 if (!strncmp(name, "ovs-", 4)) {
338 struct sset types;
339 const char *type;
340
341 sset_init(&types);
342 dp_enumerate_types(&types);
343 SSET_FOR_EACH (type, &types) {
344 if (!strcmp(name+4, type)) {
345 sset_destroy(&types);
346 return true;
347 }
348 }
349 sset_destroy(&types);
350 }
351
352 return false;
353}
354
18812dff
BP
355/* Opens the network device named 'name' (e.g. "eth0") of the specified 'type'
356 * (e.g. "system") and returns zero if successful, otherwise a positive errno
357 * value. On success, sets '*netdevp' to the new network device, otherwise to
358 * null.
064af421 359 *
de5cdb90
BP
360 * Some network devices may need to be configured (with netdev_set_config())
361 * before they can be used. */
064af421 362int
18812dff 363netdev_open(const char *name, const char *type, struct netdev **netdevp)
86383816 364 OVS_EXCLUDED(netdev_mutex)
064af421 365{
b5d57fc8 366 struct netdev *netdev;
064af421 367 int error;
064af421 368
559843ed 369 netdev_initialize();
6c88d577 370
da695497 371 ovs_mutex_lock(&netdev_class_mutex);
86383816 372 ovs_mutex_lock(&netdev_mutex);
b5d57fc8
BP
373 netdev = shash_find_data(&netdev_shash, name);
374 if (!netdev) {
86383816 375 struct netdev_registered_class *rc;
c3827f61 376
86383816
BP
377 rc = netdev_lookup_class(type && type[0] ? type : "system");
378 if (rc) {
379 netdev = rc->class->alloc();
9dc63482
BP
380 if (netdev) {
381 memset(netdev, 0, sizeof *netdev);
86383816 382 netdev->netdev_class = rc->class;
9dc63482 383 netdev->name = xstrdup(name);
3e912ffc 384 netdev->change_seq = 1;
9dc63482 385 netdev->node = shash_add(&netdev_shash, name, netdev);
55c955bd 386
f00fa8cb
AW
387 /* By default enable one tx and rx queue per netdev. */
388 netdev->n_txq = netdev->netdev_class->send ? 1 : 0;
389 netdev->n_rxq = netdev->netdev_class->rxq_alloc ? 1 : 0;
a14b8947 390 netdev->requested_n_rxq = netdev->n_rxq;
f00fa8cb 391
417e7e66 392 ovs_list_init(&netdev->saved_flags_list);
9dc63482 393
86383816
BP
394 error = rc->class->construct(netdev);
395 if (!error) {
fd0318be 396 rc->ref_cnt++;
3e912ffc 397 netdev_change_seq_changed(netdev);
86383816 398 } else {
d72e22c8 399 free(netdev->name);
417e7e66 400 ovs_assert(ovs_list_is_empty(&netdev->saved_flags_list));
d72e22c8 401 shash_delete(&netdev_shash, netdev->node);
86383816 402 rc->class->dealloc(netdev);
9dc63482
BP
403 }
404 } else {
405 error = ENOMEM;
406 }
407 } else {
c3827f61 408 VLOG_WARN("could not create netdev %s of unknown type %s",
18812dff 409 name, type);
9dc63482 410 error = EAFNOSUPPORT;
149f577a 411 }
9dc63482
BP
412 } else {
413 error = 0;
6c88d577 414 }
064af421 415
9dc63482
BP
416 if (!error) {
417 netdev->ref_cnt++;
418 *netdevp = netdev;
419 } else {
420 *netdevp = NULL;
421 }
ca94dda6
JS
422 ovs_mutex_unlock(&netdev_mutex);
423 ovs_mutex_unlock(&netdev_class_mutex);
424
9dc63482 425 return error;
064af421
BP
426}
427
e20ae811
EJ
428/* Returns a reference to 'netdev_' for the caller to own. Returns null if
429 * 'netdev_' is null. */
0bb0393a
BP
430struct netdev *
431netdev_ref(const struct netdev *netdev_)
86383816 432 OVS_EXCLUDED(netdev_mutex)
0bb0393a
BP
433{
434 struct netdev *netdev = CONST_CAST(struct netdev *, netdev_);
435
e20ae811 436 if (netdev) {
86383816 437 ovs_mutex_lock(&netdev_mutex);
e20ae811
EJ
438 ovs_assert(netdev->ref_cnt > 0);
439 netdev->ref_cnt++;
86383816 440 ovs_mutex_unlock(&netdev_mutex);
e20ae811 441 }
0bb0393a
BP
442 return netdev;
443}
444
149f577a
JG
445/* Reconfigures the device 'netdev' with 'args'. 'args' may be empty
446 * or NULL if none are needed. */
447int
bbe6109d 448netdev_set_config(struct netdev *netdev, const struct smap *args, char **errp)
86383816 449 OVS_EXCLUDED(netdev_mutex)
149f577a 450{
b5d57fc8 451 if (netdev->netdev_class->set_config) {
86383816 452 const struct smap no_args = SMAP_INITIALIZER(&no_args);
4f6b9934
BP
453 int error;
454
455 error = netdev->netdev_class->set_config(netdev,
456 args ? args : &no_args);
457 if (error) {
bbe6109d
TG
458 VLOG_WARN_BUF(errp, "%s: could not set configuration (%s)",
459 netdev_get_name(netdev), ovs_strerror(error));
4f6b9934
BP
460 }
461 return error;
79f1cbe9 462 } else if (args && !smap_is_empty(args)) {
bbe6109d
TG
463 VLOG_WARN_BUF(errp, "%s: arguments provided to device that is not configurable",
464 netdev_get_name(netdev));
149f577a 465 }
149f577a
JG
466 return 0;
467}
468
de5cdb90 469/* Returns the current configuration for 'netdev' in 'args'. The caller must
79f1cbe9 470 * have already initialized 'args' with smap_init(). Returns 0 on success, in
de5cdb90
BP
471 * which case 'args' will be filled with 'netdev''s configuration. On failure
472 * returns a positive errno value, in which case 'args' will be empty.
6d9e6eb4 473 *
de5cdb90 474 * The caller owns 'args' and its contents and must eventually free them with
79f1cbe9 475 * smap_destroy(). */
de5cdb90 476int
79f1cbe9 477netdev_get_config(const struct netdev *netdev, struct smap *args)
86383816 478 OVS_EXCLUDED(netdev_mutex)
6d9e6eb4 479{
de5cdb90
BP
480 int error;
481
79f1cbe9 482 smap_clear(args);
b5d57fc8
BP
483 if (netdev->netdev_class->get_config) {
484 error = netdev->netdev_class->get_config(netdev, args);
de5cdb90 485 if (error) {
79f1cbe9 486 smap_clear(args);
de5cdb90
BP
487 }
488 } else {
489 error = 0;
490 }
491
492 return error;
6d9e6eb4
BP
493}
494
f431bf7d
EJ
495const struct netdev_tunnel_config *
496netdev_get_tunnel_config(const struct netdev *netdev)
86383816 497 OVS_EXCLUDED(netdev_mutex)
f431bf7d 498{
b5d57fc8
BP
499 if (netdev->netdev_class->get_tunnel_config) {
500 return netdev->netdev_class->get_tunnel_config(netdev);
f431bf7d
EJ
501 } else {
502 return NULL;
503 }
504}
505
7dec44fe
AW
506/* Returns the id of the numa node the 'netdev' is on. If the function
507 * is not implemented, returns NETDEV_NUMA_UNSPEC. */
508int
509netdev_get_numa_id(const struct netdev *netdev)
510{
511 if (netdev->netdev_class->get_numa_id) {
512 return netdev->netdev_class->get_numa_id(netdev);
513 } else {
514 return NETDEV_NUMA_UNSPEC;
515 }
516}
517
4b609110 518static void
b5d57fc8 519netdev_unref(struct netdev *dev)
86383816 520 OVS_RELEASES(netdev_mutex)
4b609110
BP
521{
522 ovs_assert(dev->ref_cnt);
523 if (!--dev->ref_cnt) {
86383816
BP
524 const struct netdev_class *class = dev->netdev_class;
525 struct netdev_registered_class *rc;
86383816 526
9dc63482
BP
527 dev->netdev_class->destruct(dev);
528
fe83f81d
RW
529 if (dev->node) {
530 shash_delete(&netdev_shash, dev->node);
531 }
9dc63482
BP
532 free(dev->name);
533 dev->netdev_class->dealloc(dev);
86383816
BP
534 ovs_mutex_unlock(&netdev_mutex);
535
da695497 536 ovs_mutex_lock(&netdev_class_mutex);
86383816 537 rc = netdev_lookup_class(class->type);
fd0318be
JR
538 ovs_assert(rc->ref_cnt > 0);
539 rc->ref_cnt--;
da695497 540 ovs_mutex_unlock(&netdev_class_mutex);
86383816
BP
541 } else {
542 ovs_mutex_unlock(&netdev_mutex);
4b609110
BP
543 }
544}
545
064af421
BP
546/* Closes and destroys 'netdev'. */
547void
548netdev_close(struct netdev *netdev)
86383816 549 OVS_EXCLUDED(netdev_mutex)
064af421
BP
550{
551 if (netdev) {
86383816 552 ovs_mutex_lock(&netdev_mutex);
b5d57fc8 553 netdev_unref(netdev);
064af421
BP
554 }
555}
556
fe83f81d
RW
557/* Removes 'netdev' from the global shash and unrefs 'netdev'.
558 *
559 * This allows handler and revalidator threads to still retain references
560 * to this netdev while the main thread changes interface configuration.
561 *
562 * This function should only be called by the main thread when closing
563 * netdevs during user configuration changes. Otherwise, netdev_close should be
564 * used to close netdevs. */
565void
566netdev_remove(struct netdev *netdev)
567{
568 if (netdev) {
569 ovs_mutex_lock(&netdev_mutex);
570 if (netdev->node) {
571 shash_delete(&netdev_shash, netdev->node);
572 netdev->node = NULL;
573 netdev_change_seq_changed(netdev);
574 }
575 netdev_unref(netdev);
576 }
577}
578
a75531e5
BP
579/* Parses 'netdev_name_', which is of the form [type@]name into its component
580 * pieces. 'name' and 'type' must be freed by the caller. */
581void
582netdev_parse_name(const char *netdev_name_, char **name, char **type)
583{
584 char *netdev_name = xstrdup(netdev_name_);
585 char *separator;
586
587 separator = strchr(netdev_name, '@');
588 if (separator) {
589 *separator = '\0';
590 *type = netdev_name;
591 *name = xstrdup(separator + 1);
592 } else {
593 *name = netdev_name;
594 *type = xstrdup("system");
595 }
596}
597
f7791740
PS
598/* Attempts to open a netdev_rxq handle for obtaining packets received on
599 * 'netdev'. On success, returns 0 and stores a nonnull 'netdev_rxq *' into
b73c8518
SH
600 * '*rxp'. On failure, returns a positive errno value and stores NULL into
601 * '*rxp'.
602 *
603 * Some kinds of network devices might not support receiving packets. This
604 * function returns EOPNOTSUPP in that case.*/
7b6b0ef4 605int
55c955bd 606netdev_rxq_open(struct netdev *netdev, struct netdev_rxq **rxp, int id)
86383816 607 OVS_EXCLUDED(netdev_mutex)
7b6b0ef4 608{
796223f5 609 int error;
7b6b0ef4 610
55c955bd 611 if (netdev->netdev_class->rxq_alloc && id < netdev->n_rxq) {
f7791740 612 struct netdev_rxq *rx = netdev->netdev_class->rxq_alloc();
9dc63482
BP
613 if (rx) {
614 rx->netdev = netdev;
55c955bd 615 rx->queue_id = id;
f7791740 616 error = netdev->netdev_class->rxq_construct(rx);
9dc63482 617 if (!error) {
a17ceb1b 618 netdev_ref(netdev);
9dc63482
BP
619 *rxp = rx;
620 return 0;
621 }
f7791740 622 netdev->netdev_class->rxq_dealloc(rx);
9dc63482
BP
623 } else {
624 error = ENOMEM;
625 }
796223f5 626 } else {
9dc63482 627 error = EOPNOTSUPP;
796223f5 628 }
9dc63482
BP
629
630 *rxp = NULL;
796223f5
BP
631 return error;
632}
633
b73c8518 634/* Closes 'rx'. */
796223f5 635void
f7791740 636netdev_rxq_close(struct netdev_rxq *rx)
86383816 637 OVS_EXCLUDED(netdev_mutex)
796223f5
BP
638{
639 if (rx) {
9dc63482 640 struct netdev *netdev = rx->netdev;
f7791740
PS
641 netdev->netdev_class->rxq_destruct(rx);
642 netdev->netdev_class->rxq_dealloc(rx);
9dc63482 643 netdev_close(netdev);
796223f5 644 }
7b6b0ef4
BP
645}
646
e175dc83
BP
647/* Attempts to receive a batch of packets from 'rx'. 'pkts' should point to
648 * the beginning of an array of MAX_RX_BATCH pointers to dp_packet. If
649 * successful, this function stores pointers to up to MAX_RX_BATCH dp_packets
650 * into the array, transferring ownership of the packets to the caller, stores
651 * the number of received packets into '*cnt', and returns 0.
b73c8518 652 *
e175dc83
BP
653 * The implementation does not necessarily initialize any non-data members of
654 * 'pkts'. That is, the caller must initialize layer pointers and metadata
655 * itself, if desired, e.g. with pkt_metadata_init() and miniflow_extract().
b73c8518 656 *
e175dc83
BP
657 * Returns EAGAIN immediately if no packet is ready to be received or another
658 * positive errno value if an error was encountered. */
064af421 659int
e175dc83 660netdev_rxq_recv(struct netdev_rxq *rx, struct dp_packet **pkts, int *cnt)
064af421 661{
8b61709d 662 int retval;
064af421 663
e175dc83 664 retval = rx->netdev->netdev_class->rxq_recv(rx, pkts, cnt);
bfd3367b 665 if (!retval) {
064af421 666 COVERAGE_INC(netdev_received);
e175dc83
BP
667 } else {
668 *cnt = 0;
064af421 669 }
df1e5a3b 670 return retval;
064af421
BP
671}
672
b73c8518
SH
673/* Arranges for poll_block() to wake up when a packet is ready to be received
674 * on 'rx'. */
064af421 675void
f7791740 676netdev_rxq_wait(struct netdev_rxq *rx)
064af421 677{
f7791740 678 rx->netdev->netdev_class->rxq_wait(rx);
064af421
BP
679}
680
b73c8518 681/* Discards any packets ready to be received on 'rx'. */
064af421 682int
f7791740 683netdev_rxq_drain(struct netdev_rxq *rx)
064af421 684{
f7791740
PS
685 return (rx->netdev->netdev_class->rxq_drain
686 ? rx->netdev->netdev_class->rxq_drain(rx)
9dc63482 687 : 0);
064af421
BP
688}
689
5496878c
AW
690/* Configures the number of tx queues and rx queues of 'netdev'.
691 * Return 0 if successful, otherwise a positive errno value.
692 *
a0cb2d66
DDP
693 * 'n_rxq' specifies the maximum number of receive queues to create.
694 * The netdev provider might choose to create less (e.g. if the hardware
695 * supports only a smaller number). The caller can check how many have been
696 * actually created by calling 'netdev_n_rxq()'
697 *
698 * 'n_txq' specifies the exact number of transmission queues to create.
699 * If this function returns successfully, the caller can make 'n_txq'
700 * concurrent calls to netdev_send() (each one with a different 'qid' in the
701 * range [0..'n_txq'-1]).
702 *
5496878c
AW
703 * On error, the tx queue and rx queue configuration is indeterminant.
704 * Caller should make decision on whether to restore the previous or
705 * the default configuration. Also, caller must make sure there is no
706 * other thread accessing the queues at the same time. */
707int
708netdev_set_multiq(struct netdev *netdev, unsigned int n_txq,
709 unsigned int n_rxq)
710{
711 int error;
712
713 error = (netdev->netdev_class->set_multiq
714 ? netdev->netdev_class->set_multiq(netdev,
715 MAX(n_txq, 1),
716 MAX(n_rxq, 1))
717 : EOPNOTSUPP);
718
f6d0d4b3 719 if (error && error != EOPNOTSUPP) {
5496878c
AW
720 VLOG_DBG_RL(&rl, "failed to set tx/rx queue for network device %s:"
721 "%s", netdev_get_name(netdev), ovs_strerror(error));
722 }
723
724 return error;
725}
726
f4fd623c
DDP
727/* Sends 'buffers' on 'netdev'. Returns 0 if successful (for every packet),
728 * otherwise a positive errno value. Returns EAGAIN without blocking if
729 * at least one the packets cannot be queued immediately. Returns EMSGSIZE
730 * if a partial packet was transmitted or if a packet is too big or too small
731 * to transmit on the device.
732 *
733 * If the function returns a non-zero value, some of the packets might have
734 * been sent anyway.
064af421 735 *
7dbd520e
BP
736 * If 'may_steal' is false, the caller retains ownership of all the packets.
737 * If 'may_steal' is true, the caller transfers ownership of all the packets
738 * to the network device, regardless of success.
064af421 739 *
f00fa8cb
AW
740 * The network device is expected to maintain one or more packet
741 * transmission queues, so that the caller does not ordinarily have to
742 * do additional queuing of packets. 'qid' specifies the queue to use
743 * and can be ignored if the implementation does not support multiple
744 * queues.
1ac98180
BP
745 *
746 * Some network devices may not implement support for this function. In such
747 * cases this function will always return EOPNOTSUPP. */
064af421 748int
e14deea0 749netdev_send(struct netdev *netdev, int qid, struct dp_packet **buffers,
f00fa8cb 750 int cnt, bool may_steal)
064af421 751{
7dbd520e
BP
752 if (!netdev->netdev_class->send) {
753 if (may_steal) {
754 for (int i = 0; i < cnt; i++) {
755 dp_packet_delete(buffers[i]);
756 }
757 }
758 return EOPNOTSUPP;
759 }
1ac98180 760
7dbd520e
BP
761 int error = netdev->netdev_class->send(netdev, qid, buffers, cnt,
762 may_steal);
8b61709d 763 if (!error) {
064af421 764 COVERAGE_INC(netdev_sent);
064af421 765 }
8b61709d 766 return error;
064af421
BP
767}
768
a36de779 769int
e14deea0 770netdev_pop_header(struct netdev *netdev, struct dp_packet **buffers, int cnt)
a36de779 771{
d625fbd1
JG
772 int i;
773
774 if (!netdev->netdev_class->pop_header) {
775 return EOPNOTSUPP;
776 }
777
778 for (i = 0; i < cnt; i++) {
779 int err;
780
781 err = netdev->netdev_class->pop_header(buffers[i]);
782 if (err) {
e62d903b 783 dp_packet_clear(buffers[i]);
d625fbd1
JG
784 }
785 }
786
787 return 0;
a36de779
PS
788}
789
790int
c876a4bb
RL
791netdev_build_header(const struct netdev *netdev, struct ovs_action_push_tnl *data,
792 const struct flow *tnl_flow)
a36de779
PS
793{
794 if (netdev->netdev_class->build_header) {
c876a4bb 795 return netdev->netdev_class->build_header(netdev, data, tnl_flow);
a36de779
PS
796 }
797 return EOPNOTSUPP;
798}
799
800int
801netdev_push_header(const struct netdev *netdev,
e14deea0 802 struct dp_packet **buffers, int cnt,
a36de779
PS
803 const struct ovs_action_push_tnl *data)
804{
d625fbd1
JG
805 int i;
806
807 if (!netdev->netdev_class->push_header) {
a36de779
PS
808 return -EINVAL;
809 }
d625fbd1
JG
810
811 for (i = 0; i < cnt; i++) {
812 netdev->netdev_class->push_header(buffers[i], data);
35303d71 813 pkt_metadata_init(&buffers[i]->md, u32_to_odp(data->out_port));
d625fbd1
JG
814 }
815
816 return 0;
a36de779
PS
817}
818
064af421
BP
819/* Registers with the poll loop to wake up from the next call to poll_block()
820 * when the packet transmission queue has sufficient room to transmit a packet
821 * with netdev_send().
822 *
f00fa8cb
AW
823 * The network device is expected to maintain one or more packet
824 * transmission queues, so that the caller does not ordinarily have to
825 * do additional queuing of packets. 'qid' specifies the queue to use
826 * and can be ignored if the implementation does not support multiple
827 * queues. */
064af421 828void
f00fa8cb 829netdev_send_wait(struct netdev *netdev, int qid)
064af421 830{
b5d57fc8 831 if (netdev->netdev_class->send_wait) {
f00fa8cb 832 netdev->netdev_class->send_wait(netdev, qid);
1ac98180 833 }
064af421
BP
834}
835
836/* Attempts to set 'netdev''s MAC address to 'mac'. Returns 0 if successful,
837 * otherwise a positive errno value. */
838int
74ff3298 839netdev_set_etheraddr(struct netdev *netdev, const struct eth_addr mac)
064af421 840{
b5d57fc8 841 return netdev->netdev_class->set_etheraddr(netdev, mac);
064af421
BP
842}
843
80992a35
BP
844/* Retrieves 'netdev''s MAC address. If successful, returns 0 and copies the
845 * the MAC address into 'mac'. On failure, returns a positive errno value and
846 * clears 'mac' to all-zeros. */
847int
74ff3298 848netdev_get_etheraddr(const struct netdev *netdev, struct eth_addr *mac)
064af421 849{
b5d57fc8 850 return netdev->netdev_class->get_etheraddr(netdev, mac);
064af421
BP
851}
852
853/* Returns the name of the network device that 'netdev' represents,
854 * e.g. "eth0". The caller must not modify or free the returned string. */
855const char *
856netdev_get_name(const struct netdev *netdev)
857{
b5d57fc8 858 return netdev->name;
064af421
BP
859}
860
3d222126
BP
861/* Retrieves the MTU of 'netdev'. The MTU is the maximum size of transmitted
862 * (and received) packets, in bytes, not including the hardware header; thus,
863 * this is typically 1500 bytes for Ethernet devices.
864 *
9b020780
PS
865 * If successful, returns 0 and stores the MTU size in '*mtup'. Returns
866 * EOPNOTSUPP if 'netdev' does not have an MTU (as e.g. some tunnels do not).
14622f22
BP
867 * On other failure, returns a positive errno value. On failure, sets '*mtup'
868 * to 0. */
064af421 869int
3d222126 870netdev_get_mtu(const struct netdev *netdev, int *mtup)
064af421 871{
b5d57fc8 872 const struct netdev_class *class = netdev->netdev_class;
14622f22
BP
873 int error;
874
875 error = class->get_mtu ? class->get_mtu(netdev, mtup) : EOPNOTSUPP;
876 if (error) {
877 *mtup = 0;
878 if (error != EOPNOTSUPP) {
90a6637d 879 VLOG_DBG_RL(&rl, "failed to retrieve MTU for network device %s: "
10a89ef0 880 "%s", netdev_get_name(netdev), ovs_strerror(error));
14622f22 881 }
8b61709d
BP
882 }
883 return error;
064af421
BP
884}
885
9b020780
PS
886/* Sets the MTU of 'netdev'. The MTU is the maximum size of transmitted
887 * (and received) packets, in bytes.
888 *
889 * If successful, returns 0. Returns EOPNOTSUPP if 'netdev' does not have an
890 * MTU (as e.g. some tunnels do not). On other failure, returns a positive
891 * errno value. */
892int
893netdev_set_mtu(const struct netdev *netdev, int mtu)
894{
b5d57fc8 895 const struct netdev_class *class = netdev->netdev_class;
14622f22 896 int error;
9b020780 897
14622f22 898 error = class->set_mtu ? class->set_mtu(netdev, mtu) : EOPNOTSUPP;
9b020780 899 if (error && error != EOPNOTSUPP) {
90a6637d 900 VLOG_DBG_RL(&rl, "failed to set MTU for network device %s: %s",
10a89ef0 901 netdev_get_name(netdev), ovs_strerror(error));
9b020780
PS
902 }
903
904 return error;
905}
906
9ab3d9a3
BP
907/* Returns the ifindex of 'netdev', if successful, as a positive number. On
908 * failure, returns a negative errno value.
909 *
910 * The desired semantics of the ifindex value are a combination of those
911 * specified by POSIX for if_nametoindex() and by SNMP for ifIndex. An ifindex
912 * value should be unique within a host and remain stable at least until
913 * reboot. SNMP says an ifindex "ranges between 1 and the value of ifNumber"
914 * but many systems do not follow this rule anyhow.
4c0f1780
JG
915 *
916 * Some network devices may not implement support for this function. In such
917 * cases this function will always return -EOPNOTSUPP.
9ab3d9a3
BP
918 */
919int
920netdev_get_ifindex(const struct netdev *netdev)
921{
4c0f1780
JG
922 int (*get_ifindex)(const struct netdev *);
923
b5d57fc8 924 get_ifindex = netdev->netdev_class->get_ifindex;
4c0f1780
JG
925
926 return get_ifindex ? get_ifindex(netdev) : -EOPNOTSUPP;
9ab3d9a3
BP
927}
928
064af421
BP
929/* Stores the features supported by 'netdev' into each of '*current',
930 * '*advertised', '*supported', and '*peer' that are non-null. Each value is a
931 * bitmap of "enum ofp_port_features" bits, in host byte order. Returns 0 if
932 * successful, otherwise a positive errno value. On failure, all of the
4c0f1780
JG
933 * passed-in values are set to 0.
934 *
935 * Some network devices may not implement support for this function. In such
c1fdab01 936 * cases this function will always return EOPNOTSUPP. */
064af421 937int
6f2f5cce 938netdev_get_features(const struct netdev *netdev,
6c038611
BP
939 enum netdev_features *current,
940 enum netdev_features *advertised,
941 enum netdev_features *supported,
942 enum netdev_features *peer)
064af421 943{
6f2f5cce 944 int (*get_features)(const struct netdev *netdev,
6c038611
BP
945 enum netdev_features *current,
946 enum netdev_features *advertised,
947 enum netdev_features *supported,
948 enum netdev_features *peer);
949 enum netdev_features dummy[4];
7671589a
BP
950 int error;
951
952 if (!current) {
953 current = &dummy[0];
954 }
955 if (!advertised) {
956 advertised = &dummy[1];
957 }
958 if (!supported) {
959 supported = &dummy[2];
960 }
961 if (!peer) {
962 peer = &dummy[3];
963 }
964
b5d57fc8 965 get_features = netdev->netdev_class->get_features;
4c0f1780 966 error = get_features
a00ca915
EJ
967 ? get_features(netdev, current, advertised, supported,
968 peer)
4c0f1780 969 : EOPNOTSUPP;
7671589a
BP
970 if (error) {
971 *current = *advertised = *supported = *peer = 0;
972 }
973 return error;
064af421
BP
974}
975
6c038611
BP
976/* Returns the maximum speed of a network connection that has the NETDEV_F_*
977 * bits in 'features', in bits per second. If no bits that indicate a speed
d02a5f8e 978 * are set in 'features', returns 'default_bps'. */
622ee2cf 979uint64_t
d02a5f8e
BP
980netdev_features_to_bps(enum netdev_features features,
981 uint64_t default_bps)
622ee2cf
BP
982{
983 enum {
6c038611
BP
984 F_1000000MB = NETDEV_F_1TB_FD,
985 F_100000MB = NETDEV_F_100GB_FD,
986 F_40000MB = NETDEV_F_40GB_FD,
987 F_10000MB = NETDEV_F_10GB_FD,
988 F_1000MB = NETDEV_F_1GB_HD | NETDEV_F_1GB_FD,
989 F_100MB = NETDEV_F_100MB_HD | NETDEV_F_100MB_FD,
990 F_10MB = NETDEV_F_10MB_HD | NETDEV_F_10MB_FD
622ee2cf
BP
991 };
992
6c038611
BP
993 return ( features & F_1000000MB ? UINT64_C(1000000000000)
994 : features & F_100000MB ? UINT64_C(100000000000)
995 : features & F_40000MB ? UINT64_C(40000000000)
996 : features & F_10000MB ? UINT64_C(10000000000)
997 : features & F_1000MB ? UINT64_C(1000000000)
998 : features & F_100MB ? UINT64_C(100000000)
999 : features & F_10MB ? UINT64_C(10000000)
d02a5f8e 1000 : default_bps);
622ee2cf
BP
1001}
1002
6c038611
BP
1003/* Returns true if any of the NETDEV_F_* bits that indicate a full-duplex link
1004 * are set in 'features', otherwise false. */
622ee2cf 1005bool
6c038611 1006netdev_features_is_full_duplex(enum netdev_features features)
622ee2cf 1007{
6c038611
BP
1008 return (features & (NETDEV_F_10MB_FD | NETDEV_F_100MB_FD | NETDEV_F_1GB_FD
1009 | NETDEV_F_10GB_FD | NETDEV_F_40GB_FD
1010 | NETDEV_F_100GB_FD | NETDEV_F_1TB_FD)) != 0;
622ee2cf
BP
1011}
1012
8b61709d
BP
1013/* Set the features advertised by 'netdev' to 'advertise'. Returns 0 if
1014 * successful, otherwise a positive errno value. */
064af421 1015int
6c038611
BP
1016netdev_set_advertisements(struct netdev *netdev,
1017 enum netdev_features advertise)
064af421 1018{
b5d57fc8
BP
1019 return (netdev->netdev_class->set_advertisements
1020 ? netdev->netdev_class->set_advertisements(
a4af0040 1021 netdev, advertise)
8b61709d 1022 : EOPNOTSUPP);
064af421
BP
1023}
1024
064af421
BP
1025/* Assigns 'addr' as 'netdev''s IPv4 address and 'mask' as its netmask. If
1026 * 'addr' is INADDR_ANY, 'netdev''s IPv4 address is cleared. Returns a
1027 * positive errno value. */
1028int
1029netdev_set_in4(struct netdev *netdev, struct in_addr addr, struct in_addr mask)
1030{
b5d57fc8
BP
1031 return (netdev->netdev_class->set_in4
1032 ? netdev->netdev_class->set_in4(netdev, addr, mask)
8b61709d 1033 : EOPNOTSUPP);
064af421
BP
1034}
1035
733adf2a
LG
1036/* Obtains ad IPv4 address from device name and save the address in
1037 * in4. Returns 0 if successful, otherwise a positive errno value.
1038 */
1039int
1040netdev_get_in4_by_name(const char *device_name, struct in_addr *in4)
1041{
6b6e1329
PS
1042 struct in6_addr *mask, *addr6;
1043 int err, n_in6, i;
1044 struct netdev *dev;
733adf2a 1045
6b6e1329
PS
1046 err = netdev_open(device_name, NULL, &dev);
1047 if (err) {
1048 return err;
733adf2a
LG
1049 }
1050
6b6e1329
PS
1051 err = netdev_get_addr_list(dev, &addr6, &mask, &n_in6);
1052 if (err) {
1053 goto out;
1054 }
1055
1056 for (i = 0; i < n_in6; i++) {
1057 if (IN6_IS_ADDR_V4MAPPED(&addr6[i])) {
1058 in4->s_addr = in6_addr_get_mapped_ipv4(&addr6[i]);
1059 goto out;
1060 }
1061 }
1062 err = -ENOENT;
1063out:
1064 free(addr6);
1065 free(mask);
1066 netdev_close(dev);
1067 return err;
1068
733adf2a
LG
1069}
1070
0efaf4b5
BP
1071/* Adds 'router' as a default IP gateway for the TCP/IP stack that corresponds
1072 * to 'netdev'. */
064af421 1073int
8b61709d 1074netdev_add_router(struct netdev *netdev, struct in_addr router)
064af421 1075{
064af421 1076 COVERAGE_INC(netdev_add_router);
b5d57fc8
BP
1077 return (netdev->netdev_class->add_router
1078 ? netdev->netdev_class->add_router(netdev, router)
8b61709d 1079 : EOPNOTSUPP);
064af421
BP
1080}
1081
f1acd62b
BP
1082/* Looks up the next hop for 'host' for the TCP/IP stack that corresponds to
1083 * 'netdev'. If a route cannot not be determined, sets '*next_hop' to 0,
1084 * '*netdev_name' to null, and returns a positive errno value. Otherwise, if a
1085 * next hop is found, stores the next hop gateway's address (0 if 'host' is on
1086 * a directly connected network) in '*next_hop' and a copy of the name of the
1087 * device to reach 'host' in '*netdev_name', and returns 0. The caller is
1088 * responsible for freeing '*netdev_name' (by calling free()). */
1089int
1090netdev_get_next_hop(const struct netdev *netdev,
1091 const struct in_addr *host, struct in_addr *next_hop,
1092 char **netdev_name)
1093{
b5d57fc8
BP
1094 int error = (netdev->netdev_class->get_next_hop
1095 ? netdev->netdev_class->get_next_hop(
a4af0040 1096 host, next_hop, netdev_name)
f1acd62b 1097 : EOPNOTSUPP);
064af421 1098 if (error) {
f1acd62b
BP
1099 next_hop->s_addr = 0;
1100 *netdev_name = NULL;
064af421
BP
1101 }
1102 return error;
1103}
1104
79f1cbe9 1105/* Populates 'smap' with status information.
ea763e0e 1106 *
79f1cbe9
EJ
1107 * Populates 'smap' with 'netdev' specific status information. This
1108 * information may be used to populate the status column of the Interface table
1109 * as defined in ovs-vswitchd.conf.db(5). */
ea763e0e 1110int
275707c3 1111netdev_get_status(const struct netdev *netdev, struct smap *smap)
ea83a2fc 1112{
b5d57fc8
BP
1113 return (netdev->netdev_class->get_status
1114 ? netdev->netdev_class->get_status(netdev, smap)
ea763e0e 1115 : EOPNOTSUPP);
ea83a2fc
EJ
1116}
1117
a8704b50
PS
1118/* Returns all assigned IP address to 'netdev' and returns 0.
1119 * API allocates array of address and masks and set it to
1120 * '*addr' and '*mask'.
1121 * Otherwise, returns a positive errno value and sets '*addr', '*mask
1122 * and '*n_addr' to NULL.
8b61709d
BP
1123 *
1124 * The following error values have well-defined meanings:
1125 *
1126 * - EADDRNOTAVAIL: 'netdev' has no assigned IPv6 address.
1127 *
1128 * - EOPNOTSUPP: No IPv6 network stack attached to 'netdev'.
1129 *
a8704b50 1130 * 'addr' may be null, in which case the address itself is not reported. */
064af421 1131int
a8704b50
PS
1132netdev_get_addr_list(const struct netdev *netdev, struct in6_addr **addr,
1133 struct in6_addr **mask, int *n_addr)
064af421 1134{
8b61709d 1135 int error;
b1bf7d43 1136
a8704b50
PS
1137 error = (netdev->netdev_class->get_addr_list
1138 ? netdev->netdev_class->get_addr_list(netdev, addr, mask, n_addr): EOPNOTSUPP);
1139 if (error && addr) {
1140 *addr = NULL;
1141 *mask = NULL;
1142 *n_addr = 0;
b1bf7d43 1143 }
a8704b50 1144
8b61709d 1145 return error;
064af421
BP
1146}
1147
1148/* On 'netdev', turns off the flags in 'off' and then turns on the flags in
4b609110 1149 * 'on'. Returns 0 if successful, otherwise a positive errno value. */
064af421
BP
1150static int
1151do_update_flags(struct netdev *netdev, enum netdev_flags off,
8b61709d 1152 enum netdev_flags on, enum netdev_flags *old_flagsp,
4b609110 1153 struct netdev_saved_flags **sfp)
86383816 1154 OVS_EXCLUDED(netdev_mutex)
064af421 1155{
4b609110 1156 struct netdev_saved_flags *sf = NULL;
8b61709d 1157 enum netdev_flags old_flags;
064af421
BP
1158 int error;
1159
b5d57fc8
BP
1160 error = netdev->netdev_class->update_flags(netdev, off & ~on, on,
1161 &old_flags);
064af421 1162 if (error) {
8b61709d
BP
1163 VLOG_WARN_RL(&rl, "failed to %s flags for network device %s: %s",
1164 off || on ? "set" : "get", netdev_get_name(netdev),
10a89ef0 1165 ovs_strerror(error));
8b61709d 1166 old_flags = 0;
4b609110 1167 } else if ((off || on) && sfp) {
8b61709d
BP
1168 enum netdev_flags new_flags = (old_flags & ~off) | on;
1169 enum netdev_flags changed_flags = old_flags ^ new_flags;
1170 if (changed_flags) {
86383816 1171 ovs_mutex_lock(&netdev_mutex);
4b609110 1172 *sfp = sf = xmalloc(sizeof *sf);
b5d57fc8 1173 sf->netdev = netdev;
417e7e66 1174 ovs_list_push_front(&netdev->saved_flags_list, &sf->node);
4b609110
BP
1175 sf->saved_flags = changed_flags;
1176 sf->saved_values = changed_flags & new_flags;
1177
b5d57fc8 1178 netdev->ref_cnt++;
86383816 1179 ovs_mutex_unlock(&netdev_mutex);
8b61709d 1180 }
064af421 1181 }
4b609110 1182
8b61709d
BP
1183 if (old_flagsp) {
1184 *old_flagsp = old_flags;
064af421 1185 }
4b609110
BP
1186 if (sfp) {
1187 *sfp = sf;
1188 }
1189
064af421
BP
1190 return error;
1191}
1192
8b61709d
BP
1193/* Obtains the current flags for 'netdev' and stores them into '*flagsp'.
1194 * Returns 0 if successful, otherwise a positive errno value. On failure,
1195 * stores 0 into '*flagsp'. */
1196int
1197netdev_get_flags(const struct netdev *netdev_, enum netdev_flags *flagsp)
1198{
ebc56baa 1199 struct netdev *netdev = CONST_CAST(struct netdev *, netdev_);
4b609110 1200 return do_update_flags(netdev, 0, 0, flagsp, NULL);
8b61709d
BP
1201}
1202
064af421 1203/* Sets the flags for 'netdev' to 'flags'.
064af421
BP
1204 * Returns 0 if successful, otherwise a positive errno value. */
1205int
1206netdev_set_flags(struct netdev *netdev, enum netdev_flags flags,
4b609110 1207 struct netdev_saved_flags **sfp)
064af421 1208{
4b609110 1209 return do_update_flags(netdev, -1, flags, NULL, sfp);
064af421
BP
1210}
1211
4b609110
BP
1212/* Turns on the specified 'flags' on 'netdev':
1213 *
1214 * - On success, returns 0. If 'sfp' is nonnull, sets '*sfp' to a newly
1215 * allocated 'struct netdev_saved_flags *' that may be passed to
1216 * netdev_restore_flags() to restore the original values of 'flags' on
1217 * 'netdev' (this will happen automatically at program termination if
1218 * netdev_restore_flags() is never called) , or to NULL if no flags were
1219 * actually changed.
1220 *
1221 * - On failure, returns a positive errno value. If 'sfp' is nonnull, sets
1222 * '*sfp' to NULL. */
064af421
BP
1223int
1224netdev_turn_flags_on(struct netdev *netdev, enum netdev_flags flags,
4b609110 1225 struct netdev_saved_flags **sfp)
064af421 1226{
4b609110 1227 return do_update_flags(netdev, 0, flags, NULL, sfp);
064af421
BP
1228}
1229
4b609110
BP
1230/* Turns off the specified 'flags' on 'netdev'. See netdev_turn_flags_on() for
1231 * details of the interface. */
064af421
BP
1232int
1233netdev_turn_flags_off(struct netdev *netdev, enum netdev_flags flags,
4b609110
BP
1234 struct netdev_saved_flags **sfp)
1235{
1236 return do_update_flags(netdev, flags, 0, NULL, sfp);
1237}
1238
1239/* Restores the flags that were saved in 'sf', and destroys 'sf'.
1240 * Does nothing if 'sf' is NULL. */
1241void
1242netdev_restore_flags(struct netdev_saved_flags *sf)
86383816 1243 OVS_EXCLUDED(netdev_mutex)
064af421 1244{
4b609110 1245 if (sf) {
b5d57fc8 1246 struct netdev *netdev = sf->netdev;
4b609110
BP
1247 enum netdev_flags old_flags;
1248
b5d57fc8
BP
1249 netdev->netdev_class->update_flags(netdev,
1250 sf->saved_flags & sf->saved_values,
1251 sf->saved_flags & ~sf->saved_values,
1252 &old_flags);
86383816
BP
1253
1254 ovs_mutex_lock(&netdev_mutex);
417e7e66 1255 ovs_list_remove(&sf->node);
4b609110 1256 free(sf);
b5d57fc8 1257 netdev_unref(netdev);
4b609110 1258 }
064af421
BP
1259}
1260
1261/* Looks up the ARP table entry for 'ip' on 'netdev'. If one exists and can be
1262 * successfully retrieved, it stores the corresponding MAC address in 'mac' and
1263 * returns 0. Otherwise, it returns a positive errno value; in particular,
8b61709d 1264 * ENXIO indicates that there is no ARP table entry for 'ip' on 'netdev'. */
064af421 1265int
8b61709d 1266netdev_arp_lookup(const struct netdev *netdev,
74ff3298 1267 ovs_be32 ip, struct eth_addr *mac)
064af421 1268{
b5d57fc8 1269 int error = (netdev->netdev_class->arp_lookup
15aee116 1270 ? netdev->netdev_class->arp_lookup(netdev, ip, mac)
8b61709d 1271 : EOPNOTSUPP);
064af421 1272 if (error) {
74ff3298 1273 *mac = eth_addr_zero;
064af421 1274 }
8b61709d 1275 return error;
064af421
BP
1276}
1277
85da620e
JG
1278/* Returns true if carrier is active (link light is on) on 'netdev'. */
1279bool
1280netdev_get_carrier(const struct netdev *netdev)
064af421 1281{
85da620e
JG
1282 int error;
1283 enum netdev_flags flags;
1284 bool carrier;
1285
1286 netdev_get_flags(netdev, &flags);
1287 if (!(flags & NETDEV_UP)) {
1288 return false;
1289 }
1290
b5d57fc8 1291 if (!netdev->netdev_class->get_carrier) {
85da620e
JG
1292 return true;
1293 }
1294
15aee116 1295 error = netdev->netdev_class->get_carrier(netdev, &carrier);
8b61709d 1296 if (error) {
85da620e 1297 VLOG_DBG("%s: failed to get network device carrier status, assuming "
10a89ef0 1298 "down: %s", netdev_get_name(netdev), ovs_strerror(error));
85da620e 1299 carrier = false;
064af421 1300 }
85da620e
JG
1301
1302 return carrier;
064af421
BP
1303}
1304
65c3058c
EJ
1305/* Returns the number of times 'netdev''s carrier has changed. */
1306long long int
1307netdev_get_carrier_resets(const struct netdev *netdev)
1308{
b5d57fc8
BP
1309 return (netdev->netdev_class->get_carrier_resets
1310 ? netdev->netdev_class->get_carrier_resets(netdev)
65c3058c
EJ
1311 : 0);
1312}
1313
1670c579
EJ
1314/* Attempts to force netdev_get_carrier() to poll 'netdev''s MII registers for
1315 * link status instead of checking 'netdev''s carrier. 'netdev''s MII
1316 * registers will be polled once ever 'interval' milliseconds. If 'netdev'
1317 * does not support MII, another method may be used as a fallback. If
1318 * 'interval' is less than or equal to zero, reverts netdev_get_carrier() to
1319 * its normal behavior.
1320 *
1321 * Returns 0 if successful, otherwise a positive errno value. */
1322int
1323netdev_set_miimon_interval(struct netdev *netdev, long long int interval)
63331829 1324{
b5d57fc8
BP
1325 return (netdev->netdev_class->set_miimon_interval
1326 ? netdev->netdev_class->set_miimon_interval(netdev, interval)
1670c579 1327 : EOPNOTSUPP);
63331829
EJ
1328}
1329
887fd0ba 1330/* Retrieves current device stats for 'netdev'. */
064af421
BP
1331int
1332netdev_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
1333{
1334 int error;
1335
1336 COVERAGE_INC(netdev_get_stats);
b5d57fc8
BP
1337 error = (netdev->netdev_class->get_stats
1338 ? netdev->netdev_class->get_stats(netdev, stats)
8b61709d 1339 : EOPNOTSUPP);
064af421
BP
1340 if (error) {
1341 memset(stats, 0xff, sizeof *stats);
1342 }
1343 return error;
1344}
1345
8b61709d
BP
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. */
064af421 1349int
b1bf7d43
BP
1350netdev_set_policing(struct netdev *netdev, uint32_t kbits_rate,
1351 uint32_t kbits_burst)
064af421 1352{
b5d57fc8
BP
1353 return (netdev->netdev_class->set_policing
1354 ? netdev->netdev_class->set_policing(netdev,
a4af0040 1355 kbits_rate, kbits_burst)
8b61709d 1356 : EOPNOTSUPP);
064af421
BP
1357}
1358
c1c9c9c4
BP
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 *
19993ef3 1367 * The caller must initialize 'types' (e.g. with sset_init()) before calling
c1c9c9c4 1368 * this function. The caller is responsible for destroying 'types' (e.g. with
19993ef3 1369 * sset_destroy()) when it is no longer needed.
c1c9c9c4
BP
1370 *
1371 * Returns 0 if successful, otherwise a positive errno value. */
1372int
19993ef3 1373netdev_get_qos_types(const struct netdev *netdev, struct sset *types)
c1c9c9c4 1374{
b5d57fc8 1375 const struct netdev_class *class = netdev->netdev_class;
c1c9c9c4
BP
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. */
1386int
1387netdev_get_qos_capabilities(const struct netdev *netdev, const char *type,
1388 struct netdev_qos_capabilities *caps)
1389{
b5d57fc8 1390 const struct netdev_class *class = netdev->netdev_class;
c1c9c9c4
BP
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(). */
1412int
1413netdev_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 *
79f1cbe9
EJ
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()).
c1c9c9c4
BP
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)). */
1441int
1442netdev_get_qos(const struct netdev *netdev,
79f1cbe9 1443 const char **typep, struct smap *details)
c1c9c9c4 1444{
b5d57fc8 1445 const struct netdev_class *class = netdev->netdev_class;
c1c9c9c4
BP
1446 int retval;
1447
1448 if (class->get_qos) {
1449 retval = class->get_qos(netdev, typep, details);
1450 if (retval) {
1451 *typep = NULL;
79f1cbe9 1452 smap_clear(details);
c1c9c9c4
BP
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. */
1480int
1481netdev_set_qos(struct netdev *netdev,
79f1cbe9 1482 const char *type, const struct smap *details)
c1c9c9c4 1483{
b5d57fc8 1484 const struct netdev_class *class = netdev->netdev_class;
c1c9c9c4
BP
1485
1486 if (!type) {
1487 type = "";
1488 }
1489
1490 if (class->set_qos) {
1491 if (!details) {
edfbe9f7 1492 static const struct smap empty = SMAP_INITIALIZER(&empty);
c1c9c9c4
BP
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 *
79f1cbe9
EJ
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()). */
c1c9c9c4
BP
1515int
1516netdev_get_queue(const struct netdev *netdev,
79f1cbe9 1517 unsigned int queue_id, struct smap *details)
c1c9c9c4 1518{
b5d57fc8 1519 const struct netdev_class *class = netdev->netdev_class;
c1c9c9c4
BP
1520 int retval;
1521
1522 retval = (class->get_queue
1523 ? class->get_queue(netdev, queue_id, details)
1524 : EOPNOTSUPP);
1525 if (retval) {
79f1cbe9 1526 smap_clear(details);
c1c9c9c4
BP
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
c1fdab01 1542 * it. */
c1c9c9c4
BP
1543int
1544netdev_set_queue(struct netdev *netdev,
79f1cbe9 1545 unsigned int queue_id, const struct smap *details)
c1c9c9c4 1546{
b5d57fc8 1547 const struct netdev_class *class = netdev->netdev_class;
c1c9c9c4
BP
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)). */
1563int
1564netdev_delete_queue(struct netdev *netdev, unsigned int queue_id)
1565{
b5d57fc8 1566 const struct netdev_class *class = netdev->netdev_class;
c1c9c9c4
BP
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
6dc34a0d
BP
1575 * positive errno value and fills 'stats' with values indicating unsupported
1576 * statistics. */
c1c9c9c4
BP
1577int
1578netdev_get_queue_stats(const struct netdev *netdev, unsigned int queue_id,
1579 struct netdev_queue_stats *stats)
1580{
b5d57fc8 1581 const struct netdev_class *class = netdev->netdev_class;
c1c9c9c4
BP
1582 int retval;
1583
1584 retval = (class->get_queue_stats
1585 ? class->get_queue_stats(netdev, queue_id, stats)
1586 : EOPNOTSUPP);
1587 if (retval) {
6dc34a0d
BP
1588 stats->tx_bytes = UINT64_MAX;
1589 stats->tx_packets = UINT64_MAX;
1590 stats->tx_errors = UINT64_MAX;
1591 stats->created = LLONG_MIN;
c1c9c9c4
BP
1592 }
1593 return retval;
1594}
1595
89454bf4
BP
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 */
1602void
1603netdev_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.
c1c9c9c4 1619 *
89454bf4
BP
1620 * Queues are not necessarily dumped in increasing order of queue ID (or any
1621 * other predictable order).
c1c9c9c4 1622 *
89454bf4
BP
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().
c1c9c9c4 1626 *
89454bf4
BP
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()). */
1635bool
1636netdev_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. */
c1c9c9c4 1658int
89454bf4 1659netdev_queue_dump_done(struct netdev_queue_dump *dump)
c1c9c9c4 1660{
89454bf4
BP
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;
c1c9c9c4
BP
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. */
1682int
1683netdev_dump_queue_stats(const struct netdev *netdev,
1684 netdev_dump_queue_stats_cb *cb, void *aux)
1685{
b5d57fc8 1686 const struct netdev_class *class = netdev->netdev_class;
c1c9c9c4
BP
1687 return (class->dump_queue_stats
1688 ? class->dump_queue_stats(netdev, cb, aux)
1689 : EOPNOTSUPP);
1690}
1691
8b61709d 1692\f
b5d57fc8 1693/* Returns the class type of 'netdev'.
a740f0de
JG
1694 *
1695 * The caller must not free the returned value. */
149f577a 1696const char *
b5d57fc8 1697netdev_get_type(const struct netdev *netdev)
a740f0de 1698{
b5d57fc8 1699 return netdev->netdev_class->type;
a740f0de
JG
1700}
1701
b5d57fc8 1702/* Returns the class associated with 'netdev'. */
15b3596a 1703const struct netdev_class *
b5d57fc8 1704netdev_get_class(const struct netdev *netdev)
a740f0de 1705{
b5d57fc8 1706 return netdev->netdev_class;
a740f0de
JG
1707}
1708
b5d57fc8 1709/* Returns the netdev with 'name' or NULL if there is none.
46415c90 1710 *
991e5fae 1711 * The caller must free the returned netdev with netdev_close(). */
b5d57fc8
BP
1712struct netdev *
1713netdev_from_name(const char *name)
86383816 1714 OVS_EXCLUDED(netdev_mutex)
46415c90 1715{
991e5fae
BP
1716 struct netdev *netdev;
1717
86383816 1718 ovs_mutex_lock(&netdev_mutex);
991e5fae
BP
1719 netdev = shash_find_data(&netdev_shash, name);
1720 if (netdev) {
86383816 1721 netdev->ref_cnt++;
991e5fae 1722 }
86383816 1723 ovs_mutex_unlock(&netdev_mutex);
991e5fae
BP
1724
1725 return netdev;
46415c90
JG
1726}
1727
7dab847a 1728/* Fills 'device_list' with devices that match 'netdev_class'.
46415c90 1729 *
2f980d74
BP
1730 * The caller is responsible for initializing and destroying 'device_list' and
1731 * must close each device on the list. */
46415c90 1732void
b5d57fc8
BP
1733netdev_get_devices(const struct netdev_class *netdev_class,
1734 struct shash *device_list)
86383816 1735 OVS_EXCLUDED(netdev_mutex)
46415c90
JG
1736{
1737 struct shash_node *node;
86383816
BP
1738
1739 ovs_mutex_lock(&netdev_mutex);
b5d57fc8
BP
1740 SHASH_FOR_EACH (node, &netdev_shash) {
1741 struct netdev *dev = node->data;
46415c90 1742
7dab847a 1743 if (dev->netdev_class == netdev_class) {
2f980d74 1744 dev->ref_cnt++;
46415c90
JG
1745 shash_add(device_list, node->name, node->data);
1746 }
1747 }
86383816 1748 ovs_mutex_unlock(&netdev_mutex);
46415c90
JG
1749}
1750
41ca1e0a
AW
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. */
1756struct netdev **
1757netdev_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
0a740f48
EJ
1786const char *
1787netdev_get_type_from_name(const char *name)
1788{
991e5fae
BP
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;
6c88d577 1793}
e9e28be3 1794\f
b5d57fc8 1795struct netdev *
f7791740 1796netdev_rxq_get_netdev(const struct netdev_rxq *rx)
796223f5 1797{
b5d57fc8
BP
1798 ovs_assert(rx->netdev->ref_cnt > 0);
1799 return rx->netdev;
796223f5
BP
1800}
1801
1802const char *
f7791740 1803netdev_rxq_get_name(const struct netdev_rxq *rx)
796223f5 1804{
f7791740 1805 return netdev_get_name(netdev_rxq_get_netdev(rx));
796223f5
BP
1806}
1807
ce179f11
IM
1808int
1809netdev_rxq_get_queue_id(const struct netdev_rxq *rx)
1810{
1811 return rx->queue_id;
1812}
1813
064af421 1814static void
4b609110 1815restore_all_flags(void *aux OVS_UNUSED)
064af421 1816{
4b609110
BP
1817 struct shash_node *node;
1818
b5d57fc8
BP
1819 SHASH_FOR_EACH (node, &netdev_shash) {
1820 struct netdev *netdev = node->data;
4b609110
BP
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;
b5d57fc8 1826 LIST_FOR_EACH (sf, node, &netdev->saved_flags_list) {
4b609110
BP
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
b5d57fc8
BP
1834 netdev->netdev_class->update_flags(netdev,
1835 saved_flags & saved_values,
1836 saved_flags & ~saved_values,
1837 &old_flags);
4b609110 1838 }
064af421
BP
1839 }
1840}
3e912ffc
AW
1841
1842uint64_t
1843netdev_get_change_seq(const struct netdev *netdev)
1844{
1845 return netdev->change_seq;
1846}
a8704b50
PS
1847
1848#ifndef _WIN32
1849/* This implementation is shared by Linux and BSD. */
1850
1851static struct ifaddrs *if_addr_list;
1852static struct ovs_mutex if_addr_list_lock = OVS_MUTEX_INITIALIZER;
1853
1854void
1855netdev_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
1865int
1866netdev_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 int family;
1886
1887 family = ifa->ifa_addr->sa_family;
1888 if (family == AF_INET || family == AF_INET6) {
1889 if (!strncmp(ifa->ifa_name, dev, IFNAMSIZ)) {
1890 cnt++;
1891 }
1892 }
1893 }
1894
1895 if (!cnt) {
1896 ovs_mutex_unlock(&if_addr_list_lock);
1897 return EADDRNOTAVAIL;
1898 }
1899 addr_array = xzalloc(sizeof *addr_array * cnt);
1900 mask_array = xzalloc(sizeof *mask_array * cnt);
1901 for (ifa = if_addr_list; ifa; ifa = ifa->ifa_next) {
1902 int family;
1903
1904 if (strncmp(ifa->ifa_name, dev, IFNAMSIZ)) {
1905 continue;
1906 }
1907
1908 family = ifa->ifa_addr->sa_family;
1909 if (family == AF_INET) {
1910 const struct sockaddr_in *sin;
1911
1912 sin = ALIGNED_CAST(const struct sockaddr_in *, ifa->ifa_addr);
1913 in6_addr_set_mapped_ipv4(&addr_array[i], sin->sin_addr.s_addr);
1914 sin = (struct sockaddr_in *) &ifa->ifa_netmask;
1915 in6_addr_set_mapped_ipv4(&mask_array[i], sin->sin_addr.s_addr);
1916 i++;
1917 } else if (family == AF_INET6) {
1918 const struct sockaddr_in6 *sin6;
1919
1920 sin6 = ALIGNED_CAST(const struct sockaddr_in6 *, ifa->ifa_addr);
1921 memcpy(&addr_array[i], &sin6->sin6_addr, sizeof *addr_array);
1922 sin6 = (struct sockaddr_in6 *) &ifa->ifa_netmask;
1923 memcpy(&mask_array[i], &sin6->sin6_addr, sizeof *mask_array);
1924 i++;
1925 }
1926 }
1927 ovs_mutex_unlock(&if_addr_list_lock);
1928 if (paddr) {
1929 *n_in = cnt;
1930 *paddr = addr_array;
1931 *pmask = mask_array;
1932 } else {
1933 free(addr_array);
1934 free(mask_array);
1935 }
1936 return 0;
1937}
1938#endif