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