]> git.proxmox.com Git - mirror_ovs.git/blame - lib/netdev.c
datapath-windows: Correct endianness for deleting zone.
[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 47#include "packets.h"
fd016ae3 48#include "openvswitch/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
14f137ba 787 * queues. */
064af421 788int
1895cc8d 789netdev_send(struct netdev *netdev, int qid, struct dp_packet_batch *batch,
324c8374 790 bool may_steal, bool concurrent_txq)
064af421 791{
324c8374
IM
792 int error = netdev->netdev_class->send(netdev, qid, batch, may_steal,
793 concurrent_txq);
8b61709d 794 if (!error) {
064af421 795 COVERAGE_INC(netdev_sent);
aaca4fe0
WT
796 if (!may_steal) {
797 dp_packet_batch_reset_cutlen(batch);
798 }
064af421 799 }
8b61709d 800 return error;
064af421
BP
801}
802
57eebbb4
DDP
803/* Pop tunnel header, build tunnel metadata and resize 'batch->packets'
804 * for further processing.
805 *
806 * The caller must make sure that 'netdev' support this operation by checking
807 * that netdev_has_tunnel_push_pop() returns true. */
9235b479 808void
1895cc8d 809netdev_pop_header(struct netdev *netdev, struct dp_packet_batch *batch)
a36de779 810{
72c84bc2
AZ
811 struct dp_packet *packet;
812 size_t i, size = dp_packet_batch_size(batch);
d625fbd1 813
72c84bc2
AZ
814 DP_PACKET_BATCH_REFILL_FOR_EACH (i, size, packet, batch) {
815 packet = netdev->netdev_class->pop_header(packet);
816 if (packet) {
1a2bb118
SC
817 /* Reset the checksum offload flags if present, to avoid wrong
818 * interpretation in the further packet processing when
819 * recirculated.*/
72c84bc2
AZ
820 reset_dp_packet_checksum_ol_flags(packet);
821 dp_packet_batch_refill(batch, packet, i);
d625fbd1
JG
822 }
823 }
a36de779
PS
824}
825
4975aa3e
PS
826void
827netdev_init_tnl_build_header_params(struct netdev_tnl_build_header_params *params,
828 const struct flow *tnl_flow,
829 const struct in6_addr *src,
830 struct eth_addr dmac,
831 struct eth_addr smac)
832{
833 params->flow = tnl_flow;
834 params->dmac = dmac;
835 params->smac = smac;
836 params->s_ip = src;
837 params->is_ipv6 = !IN6_IS_ADDR_V4MAPPED(src);
838}
839
840int netdev_build_header(const struct netdev *netdev,
841 struct ovs_action_push_tnl *data,
842 const struct netdev_tnl_build_header_params *params)
a36de779
PS
843{
844 if (netdev->netdev_class->build_header) {
4975aa3e 845 return netdev->netdev_class->build_header(netdev, data, params);
a36de779
PS
846 }
847 return EOPNOTSUPP;
848}
849
57eebbb4
DDP
850/* Push tunnel header (reading from tunnel metadata) and resize
851 * 'batch->packets' for further processing.
852 *
853 * The caller must make sure that 'netdev' support this operation by checking
854 * that netdev_has_tunnel_push_pop() returns true. */
a36de779
PS
855int
856netdev_push_header(const struct netdev *netdev,
1895cc8d 857 struct dp_packet_batch *batch,
a36de779
PS
858 const struct ovs_action_push_tnl *data)
859{
72c84bc2
AZ
860 struct dp_packet *packet;
861 DP_PACKET_BATCH_FOR_EACH (packet, batch) {
862 netdev->netdev_class->push_header(packet, data);
81765c00 863 pkt_metadata_init(&packet->md, data->out_port);
d625fbd1
JG
864 }
865
866 return 0;
a36de779
PS
867}
868
064af421
BP
869/* Registers with the poll loop to wake up from the next call to poll_block()
870 * when the packet transmission queue has sufficient room to transmit a packet
871 * with netdev_send().
872 *
f00fa8cb
AW
873 * The network device is expected to maintain one or more packet
874 * transmission queues, so that the caller does not ordinarily have to
875 * do additional queuing of packets. 'qid' specifies the queue to use
876 * and can be ignored if the implementation does not support multiple
877 * queues. */
064af421 878void
f00fa8cb 879netdev_send_wait(struct netdev *netdev, int qid)
064af421 880{
b5d57fc8 881 if (netdev->netdev_class->send_wait) {
f00fa8cb 882 netdev->netdev_class->send_wait(netdev, qid);
1ac98180 883 }
064af421
BP
884}
885
886/* Attempts to set 'netdev''s MAC address to 'mac'. Returns 0 if successful,
887 * otherwise a positive errno value. */
888int
74ff3298 889netdev_set_etheraddr(struct netdev *netdev, const struct eth_addr mac)
064af421 890{
b5d57fc8 891 return netdev->netdev_class->set_etheraddr(netdev, mac);
064af421
BP
892}
893
80992a35
BP
894/* Retrieves 'netdev''s MAC address. If successful, returns 0 and copies the
895 * the MAC address into 'mac'. On failure, returns a positive errno value and
896 * clears 'mac' to all-zeros. */
897int
74ff3298 898netdev_get_etheraddr(const struct netdev *netdev, struct eth_addr *mac)
064af421 899{
59b1e023
YS
900 int error;
901
902 error = netdev->netdev_class->get_etheraddr(netdev, mac);
903 if (error) {
904 memset(mac, 0, sizeof *mac);
905 }
906 return error;
064af421
BP
907}
908
909/* Returns the name of the network device that 'netdev' represents,
910 * e.g. "eth0". The caller must not modify or free the returned string. */
911const char *
912netdev_get_name(const struct netdev *netdev)
913{
b5d57fc8 914 return netdev->name;
064af421
BP
915}
916
3d222126
BP
917/* Retrieves the MTU of 'netdev'. The MTU is the maximum size of transmitted
918 * (and received) packets, in bytes, not including the hardware header; thus,
919 * this is typically 1500 bytes for Ethernet devices.
920 *
9b020780
PS
921 * If successful, returns 0 and stores the MTU size in '*mtup'. Returns
922 * EOPNOTSUPP if 'netdev' does not have an MTU (as e.g. some tunnels do not).
14622f22
BP
923 * On other failure, returns a positive errno value. On failure, sets '*mtup'
924 * to 0. */
064af421 925int
3d222126 926netdev_get_mtu(const struct netdev *netdev, int *mtup)
064af421 927{
b5d57fc8 928 const struct netdev_class *class = netdev->netdev_class;
14622f22
BP
929 int error;
930
931 error = class->get_mtu ? class->get_mtu(netdev, mtup) : EOPNOTSUPP;
932 if (error) {
933 *mtup = 0;
934 if (error != EOPNOTSUPP) {
90a6637d 935 VLOG_DBG_RL(&rl, "failed to retrieve MTU for network device %s: "
10a89ef0 936 "%s", netdev_get_name(netdev), ovs_strerror(error));
14622f22 937 }
8b61709d
BP
938 }
939 return error;
064af421
BP
940}
941
9b020780
PS
942/* Sets the MTU of 'netdev'. The MTU is the maximum size of transmitted
943 * (and received) packets, in bytes.
944 *
945 * If successful, returns 0. Returns EOPNOTSUPP if 'netdev' does not have an
946 * MTU (as e.g. some tunnels do not). On other failure, returns a positive
947 * errno value. */
948int
4124cb12 949netdev_set_mtu(struct netdev *netdev, int mtu)
9b020780 950{
b5d57fc8 951 const struct netdev_class *class = netdev->netdev_class;
14622f22 952 int error;
9b020780 953
14622f22 954 error = class->set_mtu ? class->set_mtu(netdev, mtu) : EOPNOTSUPP;
9b020780 955 if (error && error != EOPNOTSUPP) {
90a6637d 956 VLOG_DBG_RL(&rl, "failed to set MTU for network device %s: %s",
10a89ef0 957 netdev_get_name(netdev), ovs_strerror(error));
9b020780
PS
958 }
959
960 return error;
961}
962
3a414a0a
DDP
963/* If 'user_config' is true, the user wants to control 'netdev''s MTU and we
964 * should not override it. If 'user_config' is false, we may adjust
965 * 'netdev''s MTU (e.g., if 'netdev' is internal). */
966void
967netdev_mtu_user_config(struct netdev *netdev, bool user_config)
968{
969 if (netdev->mtu_user_config != user_config) {
970 netdev_change_seq_changed(netdev);
971 netdev->mtu_user_config = user_config;
972 }
973}
974
975/* Returns 'true' if the user explicitly specified an MTU value for 'netdev'.
976 * Otherwise, returns 'false', in which case we are allowed to adjust the
977 * device MTU. */
978bool
979netdev_mtu_is_user_config(struct netdev *netdev)
980{
981 return netdev->mtu_user_config;
982}
983
9ab3d9a3
BP
984/* Returns the ifindex of 'netdev', if successful, as a positive number. On
985 * failure, returns a negative errno value.
986 *
987 * The desired semantics of the ifindex value are a combination of those
988 * specified by POSIX for if_nametoindex() and by SNMP for ifIndex. An ifindex
989 * value should be unique within a host and remain stable at least until
990 * reboot. SNMP says an ifindex "ranges between 1 and the value of ifNumber"
991 * but many systems do not follow this rule anyhow.
4c0f1780
JG
992 *
993 * Some network devices may not implement support for this function. In such
994 * cases this function will always return -EOPNOTSUPP.
9ab3d9a3
BP
995 */
996int
997netdev_get_ifindex(const struct netdev *netdev)
998{
4c0f1780
JG
999 int (*get_ifindex)(const struct netdev *);
1000
b5d57fc8 1001 get_ifindex = netdev->netdev_class->get_ifindex;
4c0f1780
JG
1002
1003 return get_ifindex ? get_ifindex(netdev) : -EOPNOTSUPP;
9ab3d9a3
BP
1004}
1005
064af421
BP
1006/* Stores the features supported by 'netdev' into each of '*current',
1007 * '*advertised', '*supported', and '*peer' that are non-null. Each value is a
1008 * bitmap of "enum ofp_port_features" bits, in host byte order. Returns 0 if
1009 * successful, otherwise a positive errno value. On failure, all of the
4c0f1780
JG
1010 * passed-in values are set to 0.
1011 *
1012 * Some network devices may not implement support for this function. In such
c1fdab01 1013 * cases this function will always return EOPNOTSUPP. */
064af421 1014int
6f2f5cce 1015netdev_get_features(const struct netdev *netdev,
6c038611
BP
1016 enum netdev_features *current,
1017 enum netdev_features *advertised,
1018 enum netdev_features *supported,
1019 enum netdev_features *peer)
064af421 1020{
6f2f5cce 1021 int (*get_features)(const struct netdev *netdev,
6c038611
BP
1022 enum netdev_features *current,
1023 enum netdev_features *advertised,
1024 enum netdev_features *supported,
1025 enum netdev_features *peer);
1026 enum netdev_features dummy[4];
7671589a
BP
1027 int error;
1028
1029 if (!current) {
1030 current = &dummy[0];
1031 }
1032 if (!advertised) {
1033 advertised = &dummy[1];
1034 }
1035 if (!supported) {
1036 supported = &dummy[2];
1037 }
1038 if (!peer) {
1039 peer = &dummy[3];
1040 }
1041
b5d57fc8 1042 get_features = netdev->netdev_class->get_features;
4c0f1780 1043 error = get_features
a00ca915
EJ
1044 ? get_features(netdev, current, advertised, supported,
1045 peer)
4c0f1780 1046 : EOPNOTSUPP;
7671589a
BP
1047 if (error) {
1048 *current = *advertised = *supported = *peer = 0;
1049 }
1050 return error;
064af421
BP
1051}
1052
6c038611
BP
1053/* Returns the maximum speed of a network connection that has the NETDEV_F_*
1054 * bits in 'features', in bits per second. If no bits that indicate a speed
d02a5f8e 1055 * are set in 'features', returns 'default_bps'. */
622ee2cf 1056uint64_t
d02a5f8e
BP
1057netdev_features_to_bps(enum netdev_features features,
1058 uint64_t default_bps)
622ee2cf
BP
1059{
1060 enum {
6c038611
BP
1061 F_1000000MB = NETDEV_F_1TB_FD,
1062 F_100000MB = NETDEV_F_100GB_FD,
1063 F_40000MB = NETDEV_F_40GB_FD,
1064 F_10000MB = NETDEV_F_10GB_FD,
1065 F_1000MB = NETDEV_F_1GB_HD | NETDEV_F_1GB_FD,
1066 F_100MB = NETDEV_F_100MB_HD | NETDEV_F_100MB_FD,
1067 F_10MB = NETDEV_F_10MB_HD | NETDEV_F_10MB_FD
622ee2cf
BP
1068 };
1069
6c038611
BP
1070 return ( features & F_1000000MB ? UINT64_C(1000000000000)
1071 : features & F_100000MB ? UINT64_C(100000000000)
1072 : features & F_40000MB ? UINT64_C(40000000000)
1073 : features & F_10000MB ? UINT64_C(10000000000)
1074 : features & F_1000MB ? UINT64_C(1000000000)
1075 : features & F_100MB ? UINT64_C(100000000)
1076 : features & F_10MB ? UINT64_C(10000000)
d02a5f8e 1077 : default_bps);
622ee2cf
BP
1078}
1079
6c038611
BP
1080/* Returns true if any of the NETDEV_F_* bits that indicate a full-duplex link
1081 * are set in 'features', otherwise false. */
622ee2cf 1082bool
6c038611 1083netdev_features_is_full_duplex(enum netdev_features features)
622ee2cf 1084{
6c038611
BP
1085 return (features & (NETDEV_F_10MB_FD | NETDEV_F_100MB_FD | NETDEV_F_1GB_FD
1086 | NETDEV_F_10GB_FD | NETDEV_F_40GB_FD
1087 | NETDEV_F_100GB_FD | NETDEV_F_1TB_FD)) != 0;
622ee2cf
BP
1088}
1089
8b61709d
BP
1090/* Set the features advertised by 'netdev' to 'advertise'. Returns 0 if
1091 * successful, otherwise a positive errno value. */
064af421 1092int
6c038611
BP
1093netdev_set_advertisements(struct netdev *netdev,
1094 enum netdev_features advertise)
064af421 1095{
b5d57fc8
BP
1096 return (netdev->netdev_class->set_advertisements
1097 ? netdev->netdev_class->set_advertisements(
a4af0040 1098 netdev, advertise)
8b61709d 1099 : EOPNOTSUPP);
064af421
BP
1100}
1101
064af421
BP
1102/* Assigns 'addr' as 'netdev''s IPv4 address and 'mask' as its netmask. If
1103 * 'addr' is INADDR_ANY, 'netdev''s IPv4 address is cleared. Returns a
1104 * positive errno value. */
1105int
1106netdev_set_in4(struct netdev *netdev, struct in_addr addr, struct in_addr mask)
1107{
b5d57fc8
BP
1108 return (netdev->netdev_class->set_in4
1109 ? netdev->netdev_class->set_in4(netdev, addr, mask)
8b61709d 1110 : EOPNOTSUPP);
064af421
BP
1111}
1112
733adf2a
LG
1113/* Obtains ad IPv4 address from device name and save the address in
1114 * in4. Returns 0 if successful, otherwise a positive errno value.
1115 */
1116int
1117netdev_get_in4_by_name(const char *device_name, struct in_addr *in4)
1118{
6b6e1329
PS
1119 struct in6_addr *mask, *addr6;
1120 int err, n_in6, i;
1121 struct netdev *dev;
733adf2a 1122
6b6e1329
PS
1123 err = netdev_open(device_name, NULL, &dev);
1124 if (err) {
1125 return err;
733adf2a
LG
1126 }
1127
6b6e1329
PS
1128 err = netdev_get_addr_list(dev, &addr6, &mask, &n_in6);
1129 if (err) {
1130 goto out;
1131 }
1132
1133 for (i = 0; i < n_in6; i++) {
1134 if (IN6_IS_ADDR_V4MAPPED(&addr6[i])) {
1135 in4->s_addr = in6_addr_get_mapped_ipv4(&addr6[i]);
1136 goto out;
1137 }
1138 }
1139 err = -ENOENT;
1140out:
1141 free(addr6);
1142 free(mask);
1143 netdev_close(dev);
1144 return err;
1145
733adf2a
LG
1146}
1147
0efaf4b5
BP
1148/* Adds 'router' as a default IP gateway for the TCP/IP stack that corresponds
1149 * to 'netdev'. */
064af421 1150int
8b61709d 1151netdev_add_router(struct netdev *netdev, struct in_addr router)
064af421 1152{
064af421 1153 COVERAGE_INC(netdev_add_router);
b5d57fc8
BP
1154 return (netdev->netdev_class->add_router
1155 ? netdev->netdev_class->add_router(netdev, router)
8b61709d 1156 : EOPNOTSUPP);
064af421
BP
1157}
1158
f1acd62b
BP
1159/* Looks up the next hop for 'host' for the TCP/IP stack that corresponds to
1160 * 'netdev'. If a route cannot not be determined, sets '*next_hop' to 0,
1161 * '*netdev_name' to null, and returns a positive errno value. Otherwise, if a
1162 * next hop is found, stores the next hop gateway's address (0 if 'host' is on
1163 * a directly connected network) in '*next_hop' and a copy of the name of the
1164 * device to reach 'host' in '*netdev_name', and returns 0. The caller is
1165 * responsible for freeing '*netdev_name' (by calling free()). */
1166int
1167netdev_get_next_hop(const struct netdev *netdev,
1168 const struct in_addr *host, struct in_addr *next_hop,
1169 char **netdev_name)
1170{
b5d57fc8
BP
1171 int error = (netdev->netdev_class->get_next_hop
1172 ? netdev->netdev_class->get_next_hop(
a4af0040 1173 host, next_hop, netdev_name)
f1acd62b 1174 : EOPNOTSUPP);
064af421 1175 if (error) {
f1acd62b
BP
1176 next_hop->s_addr = 0;
1177 *netdev_name = NULL;
064af421
BP
1178 }
1179 return error;
1180}
1181
79f1cbe9 1182/* Populates 'smap' with status information.
ea763e0e 1183 *
79f1cbe9
EJ
1184 * Populates 'smap' with 'netdev' specific status information. This
1185 * information may be used to populate the status column of the Interface table
1186 * as defined in ovs-vswitchd.conf.db(5). */
ea763e0e 1187int
275707c3 1188netdev_get_status(const struct netdev *netdev, struct smap *smap)
ea83a2fc 1189{
b5d57fc8
BP
1190 return (netdev->netdev_class->get_status
1191 ? netdev->netdev_class->get_status(netdev, smap)
ea763e0e 1192 : EOPNOTSUPP);
ea83a2fc
EJ
1193}
1194
a8704b50
PS
1195/* Returns all assigned IP address to 'netdev' and returns 0.
1196 * API allocates array of address and masks and set it to
1197 * '*addr' and '*mask'.
1198 * Otherwise, returns a positive errno value and sets '*addr', '*mask
1199 * and '*n_addr' to NULL.
8b61709d
BP
1200 *
1201 * The following error values have well-defined meanings:
1202 *
1203 * - EADDRNOTAVAIL: 'netdev' has no assigned IPv6 address.
1204 *
1205 * - EOPNOTSUPP: No IPv6 network stack attached to 'netdev'.
1206 *
a8704b50 1207 * 'addr' may be null, in which case the address itself is not reported. */
064af421 1208int
a8704b50
PS
1209netdev_get_addr_list(const struct netdev *netdev, struct in6_addr **addr,
1210 struct in6_addr **mask, int *n_addr)
064af421 1211{
8b61709d 1212 int error;
b1bf7d43 1213
a8704b50
PS
1214 error = (netdev->netdev_class->get_addr_list
1215 ? netdev->netdev_class->get_addr_list(netdev, addr, mask, n_addr): EOPNOTSUPP);
1216 if (error && addr) {
1217 *addr = NULL;
1218 *mask = NULL;
1219 *n_addr = 0;
b1bf7d43 1220 }
a8704b50 1221
8b61709d 1222 return error;
064af421
BP
1223}
1224
1225/* On 'netdev', turns off the flags in 'off' and then turns on the flags in
4b609110 1226 * 'on'. Returns 0 if successful, otherwise a positive errno value. */
064af421
BP
1227static int
1228do_update_flags(struct netdev *netdev, enum netdev_flags off,
8b61709d 1229 enum netdev_flags on, enum netdev_flags *old_flagsp,
4b609110 1230 struct netdev_saved_flags **sfp)
86383816 1231 OVS_EXCLUDED(netdev_mutex)
064af421 1232{
4b609110 1233 struct netdev_saved_flags *sf = NULL;
8b61709d 1234 enum netdev_flags old_flags;
064af421
BP
1235 int error;
1236
b5d57fc8
BP
1237 error = netdev->netdev_class->update_flags(netdev, off & ~on, on,
1238 &old_flags);
064af421 1239 if (error) {
8b61709d
BP
1240 VLOG_WARN_RL(&rl, "failed to %s flags for network device %s: %s",
1241 off || on ? "set" : "get", netdev_get_name(netdev),
10a89ef0 1242 ovs_strerror(error));
8b61709d 1243 old_flags = 0;
4b609110 1244 } else if ((off || on) && sfp) {
8b61709d
BP
1245 enum netdev_flags new_flags = (old_flags & ~off) | on;
1246 enum netdev_flags changed_flags = old_flags ^ new_flags;
1247 if (changed_flags) {
86383816 1248 ovs_mutex_lock(&netdev_mutex);
4b609110 1249 *sfp = sf = xmalloc(sizeof *sf);
b5d57fc8 1250 sf->netdev = netdev;
417e7e66 1251 ovs_list_push_front(&netdev->saved_flags_list, &sf->node);
4b609110
BP
1252 sf->saved_flags = changed_flags;
1253 sf->saved_values = changed_flags & new_flags;
1254
b5d57fc8 1255 netdev->ref_cnt++;
86383816 1256 ovs_mutex_unlock(&netdev_mutex);
8b61709d 1257 }
064af421 1258 }
4b609110 1259
8b61709d
BP
1260 if (old_flagsp) {
1261 *old_flagsp = old_flags;
064af421 1262 }
4b609110
BP
1263 if (sfp) {
1264 *sfp = sf;
1265 }
1266
064af421
BP
1267 return error;
1268}
1269
8b61709d
BP
1270/* Obtains the current flags for 'netdev' and stores them into '*flagsp'.
1271 * Returns 0 if successful, otherwise a positive errno value. On failure,
1272 * stores 0 into '*flagsp'. */
1273int
1274netdev_get_flags(const struct netdev *netdev_, enum netdev_flags *flagsp)
1275{
ebc56baa 1276 struct netdev *netdev = CONST_CAST(struct netdev *, netdev_);
4b609110 1277 return do_update_flags(netdev, 0, 0, flagsp, NULL);
8b61709d
BP
1278}
1279
064af421 1280/* Sets the flags for 'netdev' to 'flags'.
064af421
BP
1281 * Returns 0 if successful, otherwise a positive errno value. */
1282int
1283netdev_set_flags(struct netdev *netdev, enum netdev_flags flags,
4b609110 1284 struct netdev_saved_flags **sfp)
064af421 1285{
4b609110 1286 return do_update_flags(netdev, -1, flags, NULL, sfp);
064af421
BP
1287}
1288
4b609110
BP
1289/* Turns on the specified 'flags' on 'netdev':
1290 *
1291 * - On success, returns 0. If 'sfp' is nonnull, sets '*sfp' to a newly
1292 * allocated 'struct netdev_saved_flags *' that may be passed to
1293 * netdev_restore_flags() to restore the original values of 'flags' on
1294 * 'netdev' (this will happen automatically at program termination if
1295 * netdev_restore_flags() is never called) , or to NULL if no flags were
1296 * actually changed.
1297 *
1298 * - On failure, returns a positive errno value. If 'sfp' is nonnull, sets
1299 * '*sfp' to NULL. */
064af421
BP
1300int
1301netdev_turn_flags_on(struct netdev *netdev, enum netdev_flags flags,
4b609110 1302 struct netdev_saved_flags **sfp)
064af421 1303{
4b609110 1304 return do_update_flags(netdev, 0, flags, NULL, sfp);
064af421
BP
1305}
1306
4b609110
BP
1307/* Turns off the specified 'flags' on 'netdev'. See netdev_turn_flags_on() for
1308 * details of the interface. */
064af421
BP
1309int
1310netdev_turn_flags_off(struct netdev *netdev, enum netdev_flags flags,
4b609110
BP
1311 struct netdev_saved_flags **sfp)
1312{
1313 return do_update_flags(netdev, flags, 0, NULL, sfp);
1314}
1315
1316/* Restores the flags that were saved in 'sf', and destroys 'sf'.
1317 * Does nothing if 'sf' is NULL. */
1318void
1319netdev_restore_flags(struct netdev_saved_flags *sf)
86383816 1320 OVS_EXCLUDED(netdev_mutex)
064af421 1321{
4b609110 1322 if (sf) {
b5d57fc8 1323 struct netdev *netdev = sf->netdev;
4b609110
BP
1324 enum netdev_flags old_flags;
1325
b5d57fc8
BP
1326 netdev->netdev_class->update_flags(netdev,
1327 sf->saved_flags & sf->saved_values,
1328 sf->saved_flags & ~sf->saved_values,
1329 &old_flags);
86383816
BP
1330
1331 ovs_mutex_lock(&netdev_mutex);
417e7e66 1332 ovs_list_remove(&sf->node);
4b609110 1333 free(sf);
b5d57fc8 1334 netdev_unref(netdev);
4b609110 1335 }
064af421
BP
1336}
1337
1338/* Looks up the ARP table entry for 'ip' on 'netdev'. If one exists and can be
1339 * successfully retrieved, it stores the corresponding MAC address in 'mac' and
1340 * returns 0. Otherwise, it returns a positive errno value; in particular,
8b61709d 1341 * ENXIO indicates that there is no ARP table entry for 'ip' on 'netdev'. */
064af421 1342int
8b61709d 1343netdev_arp_lookup(const struct netdev *netdev,
74ff3298 1344 ovs_be32 ip, struct eth_addr *mac)
064af421 1345{
b5d57fc8 1346 int error = (netdev->netdev_class->arp_lookup
15aee116 1347 ? netdev->netdev_class->arp_lookup(netdev, ip, mac)
8b61709d 1348 : EOPNOTSUPP);
064af421 1349 if (error) {
74ff3298 1350 *mac = eth_addr_zero;
064af421 1351 }
8b61709d 1352 return error;
064af421
BP
1353}
1354
85da620e
JG
1355/* Returns true if carrier is active (link light is on) on 'netdev'. */
1356bool
1357netdev_get_carrier(const struct netdev *netdev)
064af421 1358{
85da620e
JG
1359 int error;
1360 enum netdev_flags flags;
1361 bool carrier;
1362
1363 netdev_get_flags(netdev, &flags);
1364 if (!(flags & NETDEV_UP)) {
1365 return false;
1366 }
1367
b5d57fc8 1368 if (!netdev->netdev_class->get_carrier) {
85da620e
JG
1369 return true;
1370 }
1371
15aee116 1372 error = netdev->netdev_class->get_carrier(netdev, &carrier);
8b61709d 1373 if (error) {
85da620e 1374 VLOG_DBG("%s: failed to get network device carrier status, assuming "
10a89ef0 1375 "down: %s", netdev_get_name(netdev), ovs_strerror(error));
85da620e 1376 carrier = false;
064af421 1377 }
85da620e
JG
1378
1379 return carrier;
064af421
BP
1380}
1381
65c3058c
EJ
1382/* Returns the number of times 'netdev''s carrier has changed. */
1383long long int
1384netdev_get_carrier_resets(const struct netdev *netdev)
1385{
b5d57fc8
BP
1386 return (netdev->netdev_class->get_carrier_resets
1387 ? netdev->netdev_class->get_carrier_resets(netdev)
65c3058c
EJ
1388 : 0);
1389}
1390
1670c579
EJ
1391/* Attempts to force netdev_get_carrier() to poll 'netdev''s MII registers for
1392 * link status instead of checking 'netdev''s carrier. 'netdev''s MII
1393 * registers will be polled once ever 'interval' milliseconds. If 'netdev'
1394 * does not support MII, another method may be used as a fallback. If
1395 * 'interval' is less than or equal to zero, reverts netdev_get_carrier() to
1396 * its normal behavior.
1397 *
1398 * Returns 0 if successful, otherwise a positive errno value. */
1399int
1400netdev_set_miimon_interval(struct netdev *netdev, long long int interval)
63331829 1401{
b5d57fc8
BP
1402 return (netdev->netdev_class->set_miimon_interval
1403 ? netdev->netdev_class->set_miimon_interval(netdev, interval)
1670c579 1404 : EOPNOTSUPP);
63331829
EJ
1405}
1406
887fd0ba 1407/* Retrieves current device stats for 'netdev'. */
064af421
BP
1408int
1409netdev_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
1410{
1411 int error;
1412
d6e3feb5 1413 /* Statistics are initialized before passing it to particular device
1414 * implementation so all values are filtered out by default. */
1415 memset(stats, 0xFF, sizeof *stats);
1416
064af421 1417 COVERAGE_INC(netdev_get_stats);
b5d57fc8
BP
1418 error = (netdev->netdev_class->get_stats
1419 ? netdev->netdev_class->get_stats(netdev, stats)
8b61709d 1420 : EOPNOTSUPP);
064af421 1421 if (error) {
d6e3feb5 1422 /* In case of error all statistics are filtered out */
064af421
BP
1423 memset(stats, 0xff, sizeof *stats);
1424 }
1425 return error;
1426}
1427
8b61709d
BP
1428/* Attempts to set input rate limiting (policing) policy, such that up to
1429 * 'kbits_rate' kbps of traffic is accepted, with a maximum accumulative burst
1430 * size of 'kbits' kb. */
064af421 1431int
b1bf7d43
BP
1432netdev_set_policing(struct netdev *netdev, uint32_t kbits_rate,
1433 uint32_t kbits_burst)
064af421 1434{
b5d57fc8
BP
1435 return (netdev->netdev_class->set_policing
1436 ? netdev->netdev_class->set_policing(netdev,
a4af0040 1437 kbits_rate, kbits_burst)
8b61709d 1438 : EOPNOTSUPP);
064af421
BP
1439}
1440
c1c9c9c4
BP
1441/* Adds to 'types' all of the forms of QoS supported by 'netdev', or leaves it
1442 * empty if 'netdev' does not support QoS. Any names added to 'types' should
1443 * be documented as valid for the "type" column in the "QoS" table in
1444 * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
1445 *
1446 * Every network device supports disabling QoS with a type of "", but this type
1447 * will not be added to 'types'.
1448 *
19993ef3 1449 * The caller must initialize 'types' (e.g. with sset_init()) before calling
c1c9c9c4 1450 * this function. The caller is responsible for destroying 'types' (e.g. with
19993ef3 1451 * sset_destroy()) when it is no longer needed.
c1c9c9c4
BP
1452 *
1453 * Returns 0 if successful, otherwise a positive errno value. */
1454int
19993ef3 1455netdev_get_qos_types(const struct netdev *netdev, struct sset *types)
c1c9c9c4 1456{
b5d57fc8 1457 const struct netdev_class *class = netdev->netdev_class;
c1c9c9c4
BP
1458 return (class->get_qos_types
1459 ? class->get_qos_types(netdev, types)
1460 : 0);
1461}
1462
1463/* Queries 'netdev' for its capabilities regarding the specified 'type' of QoS,
1464 * which should be "" or one of the types returned by netdev_get_qos_types()
1465 * for 'netdev'. Returns 0 if successful, otherwise a positive errno value.
1466 * On success, initializes 'caps' with the QoS capabilities; on failure, clears
1467 * 'caps' to all zeros. */
1468int
1469netdev_get_qos_capabilities(const struct netdev *netdev, const char *type,
1470 struct netdev_qos_capabilities *caps)
1471{
b5d57fc8 1472 const struct netdev_class *class = netdev->netdev_class;
c1c9c9c4
BP
1473
1474 if (*type) {
1475 int retval = (class->get_qos_capabilities
1476 ? class->get_qos_capabilities(netdev, type, caps)
1477 : EOPNOTSUPP);
1478 if (retval) {
1479 memset(caps, 0, sizeof *caps);
1480 }
1481 return retval;
1482 } else {
1483 /* Every netdev supports turning off QoS. */
1484 memset(caps, 0, sizeof *caps);
1485 return 0;
1486 }
1487}
1488
1489/* Obtains the number of queues supported by 'netdev' for the specified 'type'
1490 * of QoS. Returns 0 if successful, otherwise a positive errno value. Stores
1491 * the number of queues (zero on failure) in '*n_queuesp'.
1492 *
1493 * This is just a simple wrapper around netdev_get_qos_capabilities(). */
1494int
1495netdev_get_n_queues(const struct netdev *netdev,
1496 const char *type, unsigned int *n_queuesp)
1497{
1498 struct netdev_qos_capabilities caps;
1499 int retval;
1500
1501 retval = netdev_get_qos_capabilities(netdev, type, &caps);
1502 *n_queuesp = caps.n_queues;
1503 return retval;
1504}
1505
1506/* Queries 'netdev' about its currently configured form of QoS. If successful,
1507 * stores the name of the current form of QoS into '*typep', stores any details
1508 * of configuration as string key-value pairs in 'details', and returns 0. On
1509 * failure, sets '*typep' to NULL and returns a positive errno value.
1510 *
1511 * A '*typep' of "" indicates that QoS is currently disabled on 'netdev'.
1512 *
79f1cbe9
EJ
1513 * The caller must initialize 'details' as an empty smap (e.g. with
1514 * smap_init()) before calling this function. The caller must free 'details'
1515 * when it is no longer needed (e.g. with smap_destroy()).
c1c9c9c4
BP
1516 *
1517 * The caller must not modify or free '*typep'.
1518 *
1519 * '*typep' will be one of the types returned by netdev_get_qos_types() for
1520 * 'netdev'. The contents of 'details' should be documented as valid for
1521 * '*typep' in the "other_config" column in the "QoS" table in
1522 * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)). */
1523int
1524netdev_get_qos(const struct netdev *netdev,
79f1cbe9 1525 const char **typep, struct smap *details)
c1c9c9c4 1526{
b5d57fc8 1527 const struct netdev_class *class = netdev->netdev_class;
c1c9c9c4
BP
1528 int retval;
1529
1530 if (class->get_qos) {
1531 retval = class->get_qos(netdev, typep, details);
1532 if (retval) {
1533 *typep = NULL;
79f1cbe9 1534 smap_clear(details);
c1c9c9c4
BP
1535 }
1536 return retval;
1537 } else {
1538 /* 'netdev' doesn't support QoS, so report that QoS is disabled. */
1539 *typep = "";
1540 return 0;
1541 }
1542}
1543
1544/* Attempts to reconfigure QoS on 'netdev', changing the form of QoS to 'type'
1545 * with details of configuration from 'details'. Returns 0 if successful,
1546 * otherwise a positive errno value. On error, the previous QoS configuration
1547 * is retained.
1548 *
1549 * When this function changes the type of QoS (not just 'details'), this also
1550 * resets all queue configuration for 'netdev' to their defaults (which depend
1551 * on the specific type of QoS). Otherwise, the queue configuration for
1552 * 'netdev' is unchanged.
1553 *
1554 * 'type' should be "" (to disable QoS) or one of the types returned by
1555 * netdev_get_qos_types() for 'netdev'. The contents of 'details' should be
1556 * documented as valid for the given 'type' in the "other_config" column in the
1557 * "QoS" table in vswitchd/vswitch.xml (which is built as
1558 * ovs-vswitchd.conf.db(8)).
1559 *
1560 * NULL may be specified for 'details' if there are no configuration
1561 * details. */
1562int
1563netdev_set_qos(struct netdev *netdev,
79f1cbe9 1564 const char *type, const struct smap *details)
c1c9c9c4 1565{
b5d57fc8 1566 const struct netdev_class *class = netdev->netdev_class;
c1c9c9c4
BP
1567
1568 if (!type) {
1569 type = "";
1570 }
1571
1572 if (class->set_qos) {
1573 if (!details) {
edfbe9f7 1574 static const struct smap empty = SMAP_INITIALIZER(&empty);
c1c9c9c4
BP
1575 details = &empty;
1576 }
1577 return class->set_qos(netdev, type, details);
1578 } else {
1579 return *type ? EOPNOTSUPP : 0;
1580 }
1581}
1582
1583/* Queries 'netdev' for information about the queue numbered 'queue_id'. If
1584 * successful, adds that information as string key-value pairs to 'details'.
1585 * Returns 0 if successful, otherwise a positive errno value.
1586 *
1587 * 'queue_id' must be less than the number of queues supported by 'netdev' for
1588 * the current form of QoS (e.g. as returned by netdev_get_n_queues(netdev)).
1589 *
1590 * The returned contents of 'details' should be documented as valid for the
1591 * given 'type' in the "other_config" column in the "Queue" table in
1592 * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
1593 *
79f1cbe9
EJ
1594 * The caller must initialize 'details' (e.g. with smap_init()) before calling
1595 * this function. The caller must free 'details' when it is no longer needed
1596 * (e.g. with smap_destroy()). */
c1c9c9c4
BP
1597int
1598netdev_get_queue(const struct netdev *netdev,
79f1cbe9 1599 unsigned int queue_id, struct smap *details)
c1c9c9c4 1600{
b5d57fc8 1601 const struct netdev_class *class = netdev->netdev_class;
c1c9c9c4
BP
1602 int retval;
1603
1604 retval = (class->get_queue
1605 ? class->get_queue(netdev, queue_id, details)
1606 : EOPNOTSUPP);
1607 if (retval) {
79f1cbe9 1608 smap_clear(details);
c1c9c9c4
BP
1609 }
1610 return retval;
1611}
1612
1613/* Configures the queue numbered 'queue_id' on 'netdev' with the key-value
1614 * string pairs in 'details'. The contents of 'details' should be documented
1615 * as valid for the given 'type' in the "other_config" column in the "Queue"
1616 * table in vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
1617 * Returns 0 if successful, otherwise a positive errno value. On failure, the
1618 * given queue's configuration should be unmodified.
1619 *
1620 * 'queue_id' must be less than the number of queues supported by 'netdev' for
1621 * the current form of QoS (e.g. as returned by netdev_get_n_queues(netdev)).
1622 *
1623 * This function does not modify 'details', and the caller retains ownership of
c1fdab01 1624 * it. */
c1c9c9c4
BP
1625int
1626netdev_set_queue(struct netdev *netdev,
79f1cbe9 1627 unsigned int queue_id, const struct smap *details)
c1c9c9c4 1628{
b5d57fc8 1629 const struct netdev_class *class = netdev->netdev_class;
c1c9c9c4
BP
1630 return (class->set_queue
1631 ? class->set_queue(netdev, queue_id, details)
1632 : EOPNOTSUPP);
1633}
1634
1635/* Attempts to delete the queue numbered 'queue_id' from 'netdev'. Some kinds
1636 * of QoS may have a fixed set of queues, in which case attempts to delete them
1637 * will fail with EOPNOTSUPP.
1638 *
1639 * Returns 0 if successful, otherwise a positive errno value. On failure, the
1640 * given queue will be unmodified.
1641 *
1642 * 'queue_id' must be less than the number of queues supported by 'netdev' for
1643 * the current form of QoS (e.g. as returned by
1644 * netdev_get_n_queues(netdev)). */
1645int
1646netdev_delete_queue(struct netdev *netdev, unsigned int queue_id)
1647{
b5d57fc8 1648 const struct netdev_class *class = netdev->netdev_class;
c1c9c9c4
BP
1649 return (class->delete_queue
1650 ? class->delete_queue(netdev, queue_id)
1651 : EOPNOTSUPP);
1652}
1653
1654/* Obtains statistics about 'queue_id' on 'netdev'. On success, returns 0 and
1655 * fills 'stats' with the queue's statistics; individual members of 'stats' may
1656 * be set to all-1-bits if the statistic is unavailable. On failure, returns a
6dc34a0d
BP
1657 * positive errno value and fills 'stats' with values indicating unsupported
1658 * statistics. */
c1c9c9c4
BP
1659int
1660netdev_get_queue_stats(const struct netdev *netdev, unsigned int queue_id,
1661 struct netdev_queue_stats *stats)
1662{
b5d57fc8 1663 const struct netdev_class *class = netdev->netdev_class;
c1c9c9c4
BP
1664 int retval;
1665
1666 retval = (class->get_queue_stats
1667 ? class->get_queue_stats(netdev, queue_id, stats)
1668 : EOPNOTSUPP);
1669 if (retval) {
6dc34a0d
BP
1670 stats->tx_bytes = UINT64_MAX;
1671 stats->tx_packets = UINT64_MAX;
1672 stats->tx_errors = UINT64_MAX;
1673 stats->created = LLONG_MIN;
c1c9c9c4
BP
1674 }
1675 return retval;
1676}
1677
89454bf4
BP
1678/* Initializes 'dump' to begin dumping the queues in a netdev.
1679 *
1680 * This function provides no status indication. An error status for the entire
1681 * dump operation is provided when it is completed by calling
1682 * netdev_queue_dump_done().
1683 */
1684void
1685netdev_queue_dump_start(struct netdev_queue_dump *dump,
1686 const struct netdev *netdev)
1687{
1688 dump->netdev = netdev_ref(netdev);
1689 if (netdev->netdev_class->queue_dump_start) {
1690 dump->error = netdev->netdev_class->queue_dump_start(netdev,
1691 &dump->state);
1692 } else {
1693 dump->error = EOPNOTSUPP;
1694 }
1695}
1696
1697/* Attempts to retrieve another queue from 'dump', which must have been
1698 * initialized with netdev_queue_dump_start(). On success, stores a new queue
1699 * ID into '*queue_id', fills 'details' with configuration details for the
1700 * queue, and returns true. On failure, returns false.
c1c9c9c4 1701 *
89454bf4
BP
1702 * Queues are not necessarily dumped in increasing order of queue ID (or any
1703 * other predictable order).
c1c9c9c4 1704 *
89454bf4
BP
1705 * Failure might indicate an actual error or merely that the last queue has
1706 * been dumped. An error status for the entire dump operation is provided when
1707 * it is completed by calling netdev_queue_dump_done().
c1c9c9c4 1708 *
89454bf4
BP
1709 * The returned contents of 'details' should be documented as valid for the
1710 * given 'type' in the "other_config" column in the "Queue" table in
1711 * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
1712 *
1713 * The caller must initialize 'details' (e.g. with smap_init()) before calling
1714 * this function. This function will clear and replace its contents. The
1715 * caller must free 'details' when it is no longer needed (e.g. with
1716 * smap_destroy()). */
1717bool
1718netdev_queue_dump_next(struct netdev_queue_dump *dump,
1719 unsigned int *queue_id, struct smap *details)
1720{
1721 const struct netdev *netdev = dump->netdev;
1722
1723 if (dump->error) {
1724 return false;
1725 }
1726
1727 dump->error = netdev->netdev_class->queue_dump_next(netdev, dump->state,
1728 queue_id, details);
1729
1730 if (dump->error) {
1731 netdev->netdev_class->queue_dump_done(netdev, dump->state);
1732 return false;
1733 }
1734 return true;
1735}
1736
1737/* Completes queue table dump operation 'dump', which must have been
1738 * initialized with netdev_queue_dump_start(). Returns 0 if the dump operation
1739 * was error-free, otherwise a positive errno value describing the problem. */
c1c9c9c4 1740int
89454bf4 1741netdev_queue_dump_done(struct netdev_queue_dump *dump)
c1c9c9c4 1742{
89454bf4
BP
1743 const struct netdev *netdev = dump->netdev;
1744 if (!dump->error && netdev->netdev_class->queue_dump_done) {
1745 dump->error = netdev->netdev_class->queue_dump_done(netdev,
1746 dump->state);
1747 }
1748 netdev_close(dump->netdev);
1749 return dump->error == EOF ? 0 : dump->error;
c1c9c9c4
BP
1750}
1751
1752/* Iterates over all of 'netdev''s queues, calling 'cb' with the queue's ID,
1753 * its statistics, and the 'aux' specified by the caller. The order of
1754 * iteration is unspecified, but (when successful) each queue is visited
1755 * exactly once.
1756 *
1757 * Calling this function may be more efficient than calling
1758 * netdev_get_queue_stats() for every queue.
1759 *
1760 * 'cb' must not modify or free the statistics passed in.
1761 *
1762 * Returns 0 if successful, otherwise a positive errno value. On error, some
1763 * configured queues may not have been included in the iteration. */
1764int
1765netdev_dump_queue_stats(const struct netdev *netdev,
1766 netdev_dump_queue_stats_cb *cb, void *aux)
1767{
b5d57fc8 1768 const struct netdev_class *class = netdev->netdev_class;
c1c9c9c4
BP
1769 return (class->dump_queue_stats
1770 ? class->dump_queue_stats(netdev, cb, aux)
1771 : EOPNOTSUPP);
1772}
1773
8b61709d 1774\f
b5d57fc8 1775/* Returns the class type of 'netdev'.
a740f0de
JG
1776 *
1777 * The caller must not free the returned value. */
149f577a 1778const char *
b5d57fc8 1779netdev_get_type(const struct netdev *netdev)
a740f0de 1780{
b5d57fc8 1781 return netdev->netdev_class->type;
a740f0de
JG
1782}
1783
b5d57fc8 1784/* Returns the class associated with 'netdev'. */
15b3596a 1785const struct netdev_class *
b5d57fc8 1786netdev_get_class(const struct netdev *netdev)
a740f0de 1787{
b5d57fc8 1788 return netdev->netdev_class;
a740f0de
JG
1789}
1790
b5d57fc8 1791/* Returns the netdev with 'name' or NULL if there is none.
46415c90 1792 *
991e5fae 1793 * The caller must free the returned netdev with netdev_close(). */
b5d57fc8
BP
1794struct netdev *
1795netdev_from_name(const char *name)
86383816 1796 OVS_EXCLUDED(netdev_mutex)
46415c90 1797{
991e5fae
BP
1798 struct netdev *netdev;
1799
86383816 1800 ovs_mutex_lock(&netdev_mutex);
991e5fae
BP
1801 netdev = shash_find_data(&netdev_shash, name);
1802 if (netdev) {
86383816 1803 netdev->ref_cnt++;
991e5fae 1804 }
86383816 1805 ovs_mutex_unlock(&netdev_mutex);
991e5fae
BP
1806
1807 return netdev;
46415c90
JG
1808}
1809
7dab847a 1810/* Fills 'device_list' with devices that match 'netdev_class'.
46415c90 1811 *
2f980d74
BP
1812 * The caller is responsible for initializing and destroying 'device_list' and
1813 * must close each device on the list. */
46415c90 1814void
b5d57fc8
BP
1815netdev_get_devices(const struct netdev_class *netdev_class,
1816 struct shash *device_list)
86383816 1817 OVS_EXCLUDED(netdev_mutex)
46415c90
JG
1818{
1819 struct shash_node *node;
86383816
BP
1820
1821 ovs_mutex_lock(&netdev_mutex);
b5d57fc8
BP
1822 SHASH_FOR_EACH (node, &netdev_shash) {
1823 struct netdev *dev = node->data;
46415c90 1824
7dab847a 1825 if (dev->netdev_class == netdev_class) {
2f980d74 1826 dev->ref_cnt++;
46415c90
JG
1827 shash_add(device_list, node->name, node->data);
1828 }
1829 }
86383816 1830 ovs_mutex_unlock(&netdev_mutex);
46415c90
JG
1831}
1832
41ca1e0a
AW
1833/* Extracts pointers to all 'netdev-vports' into an array 'vports'
1834 * and returns it. Stores the size of the array into '*size'.
1835 *
1836 * The caller is responsible for freeing 'vports' and must close
1837 * each 'netdev-vport' in the list. */
1838struct netdev **
1839netdev_get_vports(size_t *size)
1840 OVS_EXCLUDED(netdev_mutex)
1841{
1842 struct netdev **vports;
1843 struct shash_node *node;
1844 size_t n = 0;
1845
1846 if (!size) {
1847 return NULL;
1848 }
1849
1850 /* Explicitly allocates big enough chunk of memory. */
41ca1e0a 1851 ovs_mutex_lock(&netdev_mutex);
048318e0 1852 vports = xmalloc(shash_count(&netdev_shash) * sizeof *vports);
41ca1e0a
AW
1853 SHASH_FOR_EACH (node, &netdev_shash) {
1854 struct netdev *dev = node->data;
1855
1856 if (netdev_vport_is_vport_class(dev->netdev_class)) {
1857 dev->ref_cnt++;
1858 vports[n] = dev;
1859 n++;
1860 }
1861 }
1862 ovs_mutex_unlock(&netdev_mutex);
1863 *size = n;
1864
1865 return vports;
1866}
1867
0a740f48
EJ
1868const char *
1869netdev_get_type_from_name(const char *name)
1870{
33d80cf9
TLSC
1871 struct netdev *dev;
1872 const char *type;
1873 type = netdev_vport_type_from_name(name);
1874 if (type == NULL) {
1875 dev = netdev_from_name(name);
1876 type = dev ? netdev_get_type(dev) : NULL;
1877 netdev_close(dev);
1878 }
991e5fae 1879 return type;
6c88d577 1880}
e9e28be3 1881\f
b5d57fc8 1882struct netdev *
f7791740 1883netdev_rxq_get_netdev(const struct netdev_rxq *rx)
796223f5 1884{
b5d57fc8
BP
1885 ovs_assert(rx->netdev->ref_cnt > 0);
1886 return rx->netdev;
796223f5
BP
1887}
1888
1889const char *
f7791740 1890netdev_rxq_get_name(const struct netdev_rxq *rx)
796223f5 1891{
f7791740 1892 return netdev_get_name(netdev_rxq_get_netdev(rx));
796223f5
BP
1893}
1894
ce179f11
IM
1895int
1896netdev_rxq_get_queue_id(const struct netdev_rxq *rx)
1897{
1898 return rx->queue_id;
1899}
1900
064af421 1901static void
4b609110 1902restore_all_flags(void *aux OVS_UNUSED)
064af421 1903{
4b609110
BP
1904 struct shash_node *node;
1905
b5d57fc8
BP
1906 SHASH_FOR_EACH (node, &netdev_shash) {
1907 struct netdev *netdev = node->data;
4b609110
BP
1908 const struct netdev_saved_flags *sf;
1909 enum netdev_flags saved_values;
1910 enum netdev_flags saved_flags;
1911
1912 saved_values = saved_flags = 0;
b5d57fc8 1913 LIST_FOR_EACH (sf, node, &netdev->saved_flags_list) {
4b609110
BP
1914 saved_flags |= sf->saved_flags;
1915 saved_values &= ~sf->saved_flags;
1916 saved_values |= sf->saved_flags & sf->saved_values;
1917 }
1918 if (saved_flags) {
1919 enum netdev_flags old_flags;
1920
b5d57fc8
BP
1921 netdev->netdev_class->update_flags(netdev,
1922 saved_flags & saved_values,
1923 saved_flags & ~saved_values,
1924 &old_flags);
4b609110 1925 }
064af421
BP
1926 }
1927}
3e912ffc
AW
1928
1929uint64_t
1930netdev_get_change_seq(const struct netdev *netdev)
1931{
1932 return netdev->change_seq;
1933}
a8704b50
PS
1934
1935#ifndef _WIN32
1936/* This implementation is shared by Linux and BSD. */
1937
1938static struct ifaddrs *if_addr_list;
1939static struct ovs_mutex if_addr_list_lock = OVS_MUTEX_INITIALIZER;
1940
1941void
1942netdev_get_addrs_list_flush(void)
1943{
1944 ovs_mutex_lock(&if_addr_list_lock);
1945 if (if_addr_list) {
1946 freeifaddrs(if_addr_list);
1947 if_addr_list = NULL;
1948 }
1949 ovs_mutex_unlock(&if_addr_list_lock);
1950}
1951
1952int
1953netdev_get_addrs(const char dev[], struct in6_addr **paddr,
1954 struct in6_addr **pmask, int *n_in)
1955{
1956 struct in6_addr *addr_array, *mask_array;
1957 const struct ifaddrs *ifa;
1958 int cnt = 0, i = 0;
1959
1960 ovs_mutex_lock(&if_addr_list_lock);
1961 if (!if_addr_list) {
1962 int err;
1963
1964 err = getifaddrs(&if_addr_list);
1965 if (err) {
1966 ovs_mutex_unlock(&if_addr_list_lock);
1967 return -err;
1968 }
1969 }
1970
1971 for (ifa = if_addr_list; ifa; ifa = ifa->ifa_next) {
0ac01021 1972 if (ifa->ifa_addr && ifa->ifa_name && ifa->ifa_netmask) {
3e6dc8b7 1973 int family;
a8704b50 1974
3e6dc8b7
TLSC
1975 family = ifa->ifa_addr->sa_family;
1976 if (family == AF_INET || family == AF_INET6) {
1977 if (!strncmp(ifa->ifa_name, dev, IFNAMSIZ)) {
1978 cnt++;
1979 }
a8704b50
PS
1980 }
1981 }
1982 }
1983
1984 if (!cnt) {
1985 ovs_mutex_unlock(&if_addr_list_lock);
1986 return EADDRNOTAVAIL;
1987 }
1988 addr_array = xzalloc(sizeof *addr_array * cnt);
1989 mask_array = xzalloc(sizeof *mask_array * cnt);
1990 for (ifa = if_addr_list; ifa; ifa = ifa->ifa_next) {
1991 int family;
1992
f70ad934
HL
1993 if (!ifa->ifa_name || !ifa->ifa_addr || !ifa->ifa_netmask
1994 || strncmp(ifa->ifa_name, dev, IFNAMSIZ)) {
a8704b50
PS
1995 continue;
1996 }
1997
1998 family = ifa->ifa_addr->sa_family;
1999 if (family == AF_INET) {
2000 const struct sockaddr_in *sin;
2001
2002 sin = ALIGNED_CAST(const struct sockaddr_in *, ifa->ifa_addr);
2003 in6_addr_set_mapped_ipv4(&addr_array[i], sin->sin_addr.s_addr);
32b3c533 2004 sin = ALIGNED_CAST(const struct sockaddr_in *, ifa->ifa_netmask);
a8704b50
PS
2005 in6_addr_set_mapped_ipv4(&mask_array[i], sin->sin_addr.s_addr);
2006 i++;
2007 } else if (family == AF_INET6) {
2008 const struct sockaddr_in6 *sin6;
2009
2010 sin6 = ALIGNED_CAST(const struct sockaddr_in6 *, ifa->ifa_addr);
2011 memcpy(&addr_array[i], &sin6->sin6_addr, sizeof *addr_array);
32b3c533 2012 sin6 = ALIGNED_CAST(const struct sockaddr_in6 *, ifa->ifa_netmask);
a8704b50
PS
2013 memcpy(&mask_array[i], &sin6->sin6_addr, sizeof *mask_array);
2014 i++;
2015 }
2016 }
2017 ovs_mutex_unlock(&if_addr_list_lock);
2018 if (paddr) {
2019 *n_in = cnt;
2020 *paddr = addr_array;
2021 *pmask = mask_array;
2022 } else {
2023 free(addr_array);
2024 free(mask_array);
2025 }
2026 return 0;
2027}
2028#endif
790fb3b7
DDP
2029
2030void
2031netdev_wait_reconf_required(struct netdev *netdev)
2032{
2033 seq_wait(netdev->reconfigure_seq, netdev->last_reconfigure_seq);
2034}
2035
2036bool
2037netdev_is_reconf_required(struct netdev *netdev)
2038{
2039 return seq_read(netdev->reconfigure_seq) != netdev->last_reconfigure_seq;
2040}
2041
2042/* Give a chance to 'netdev' to reconfigure some of its parameters.
2043 *
2044 * If a module uses netdev_send() and netdev_rxq_recv(), it must call this
2045 * function when netdev_is_reconf_required() returns true.
2046 *
2047 * Return 0 if successful, otherwise a positive errno value. If the
2048 * reconfiguration fails the netdev will not be able to send or receive
2049 * packets.
2050 *
2051 * When this function is called, no call to netdev_rxq_recv() or netdev_send()
2052 * must be issued. */
2053int
2054netdev_reconfigure(struct netdev *netdev)
2055{
2056 const struct netdev_class *class = netdev->netdev_class;
2057
2058 netdev->last_reconfigure_seq = seq_read(netdev->reconfigure_seq);
2059
2060 return (class->reconfigure
2061 ? class->reconfigure(netdev)
2062 : EOPNOTSUPP);
2063}
18ebd48c
PB
2064
2065int
2066netdev_flow_flush(struct netdev *netdev)
2067{
2068 const struct netdev_class *class = netdev->netdev_class;
2069
2070 return (class->flow_flush
2071 ? class->flow_flush(netdev)
2072 : EOPNOTSUPP);
2073}
2074
2075int
2076netdev_flow_dump_create(struct netdev *netdev, struct netdev_flow_dump **dump)
2077{
2078 const struct netdev_class *class = netdev->netdev_class;
2079
2080 return (class->flow_dump_create
2081 ? class->flow_dump_create(netdev, dump)
2082 : EOPNOTSUPP);
2083}
2084
2085int
2086netdev_flow_dump_destroy(struct netdev_flow_dump *dump)
2087{
2088 const struct netdev_class *class = dump->netdev->netdev_class;
2089
2090 return (class->flow_dump_destroy
2091 ? class->flow_dump_destroy(dump)
2092 : EOPNOTSUPP);
2093}
2094
2095bool
2096netdev_flow_dump_next(struct netdev_flow_dump *dump, struct match *match,
2097 struct nlattr **actions, struct dpif_flow_stats *stats,
2098 ovs_u128 *ufid, struct ofpbuf *rbuffer,
2099 struct ofpbuf *wbuffer)
2100{
2101 const struct netdev_class *class = dump->netdev->netdev_class;
2102
2103 return (class->flow_dump_next
2104 ? class->flow_dump_next(dump, match, actions, stats, ufid,
2105 rbuffer, wbuffer)
2106 : false);
2107}
2108
2109int
2110netdev_flow_put(struct netdev *netdev, struct match *match,
2111 struct nlattr *actions, size_t act_len,
2112 const ovs_u128 *ufid, struct offload_info *info,
2113 struct dpif_flow_stats *stats)
2114{
2115 const struct netdev_class *class = netdev->netdev_class;
2116
2117 return (class->flow_put
2118 ? class->flow_put(netdev, match, actions, act_len, ufid,
2119 info, stats)
2120 : EOPNOTSUPP);
2121}
2122
2123int
2124netdev_flow_get(struct netdev *netdev, struct match *match,
2125 struct nlattr **actions, const ovs_u128 *ufid,
2126 struct dpif_flow_stats *stats, struct ofpbuf *buf)
2127{
2128 const struct netdev_class *class = netdev->netdev_class;
2129
2130 return (class->flow_get
2131 ? class->flow_get(netdev, match, actions, ufid, stats, buf)
2132 : EOPNOTSUPP);
2133}
2134
2135int
2136netdev_flow_del(struct netdev *netdev, const ovs_u128 *ufid,
2137 struct dpif_flow_stats *stats)
2138{
2139 const struct netdev_class *class = netdev->netdev_class;
2140
2141 return (class->flow_del
2142 ? class->flow_del(netdev, ufid, stats)
2143 : EOPNOTSUPP);
2144}
2145
2146int
2147netdev_init_flow_api(struct netdev *netdev)
2148{
2149 const struct netdev_class *class = netdev->netdev_class;
2150
53611f7b
PB
2151 if (!netdev_is_flow_api_enabled()) {
2152 return EOPNOTSUPP;
2153 }
2154
18ebd48c
PB
2155 return (class->init_flow_api
2156 ? class->init_flow_api(netdev)
2157 : EOPNOTSUPP);
2158}
53611f7b
PB
2159
2160bool
2161netdev_is_flow_api_enabled(void)
2162{
2163 return netdev_flow_api_enabled;
2164}
2165
32b77c31
PB
2166/* Protects below port hashmaps. */
2167static struct ovs_mutex netdev_hmap_mutex = OVS_MUTEX_INITIALIZER;
2168
2169static struct hmap port_to_netdev OVS_GUARDED_BY(netdev_hmap_mutex)
2170 = HMAP_INITIALIZER(&port_to_netdev);
2171static struct hmap ifindex_to_port OVS_GUARDED_BY(netdev_hmap_mutex)
2172 = HMAP_INITIALIZER(&ifindex_to_port);
2173
2174struct port_to_netdev_data {
fa54741e
BP
2175 struct hmap_node portno_node; /* By (dpif_class, dpif_port.port_no). */
2176 struct hmap_node ifindex_node; /* By (dpif_class, ifindex). */
32b77c31
PB
2177 struct netdev *netdev;
2178 struct dpif_port dpif_port;
dfaf79dd 2179 const struct dpif_class *dpif_class;
32b77c31 2180 int ifindex;
32b77c31
PB
2181};
2182
0d8efdc9
BP
2183static uint32_t
2184netdev_ports_hash(odp_port_t port, const struct dpif_class *dpif_class)
2185{
2186 return hash_int(odp_to_u32(port), hash_pointer(dpif_class, 0));
2187}
dfaf79dd 2188
32b77c31 2189static struct port_to_netdev_data *
dfaf79dd 2190netdev_ports_lookup(odp_port_t port_no, const struct dpif_class *dpif_class)
32b77c31
PB
2191 OVS_REQUIRES(netdev_hmap_mutex)
2192{
32b77c31
PB
2193 struct port_to_netdev_data *data;
2194
fa54741e 2195 HMAP_FOR_EACH_WITH_HASH (data, portno_node,
0d8efdc9
BP
2196 netdev_ports_hash(port_no, dpif_class),
2197 &port_to_netdev) {
2198 if (data->dpif_class == dpif_class
2199 && data->dpif_port.port_no == port_no) {
2200 return data;
2201 }
32b77c31
PB
2202 }
2203 return NULL;
2204}
2205
2206int
dfaf79dd 2207netdev_ports_insert(struct netdev *netdev, const struct dpif_class *dpif_class,
32b77c31
PB
2208 struct dpif_port *dpif_port)
2209{
32b77c31 2210 struct port_to_netdev_data *data;
32b77c31
PB
2211 int ifindex = netdev_get_ifindex(netdev);
2212
2213 if (ifindex < 0) {
2214 return ENODEV;
2215 }
2216
32b77c31 2217 ovs_mutex_lock(&netdev_hmap_mutex);
dfaf79dd 2218 if (netdev_ports_lookup(dpif_port->port_no, dpif_class)) {
32b77c31
PB
2219 ovs_mutex_unlock(&netdev_hmap_mutex);
2220 return EEXIST;
2221 }
2222
d2a5f170 2223 data = xzalloc(sizeof *data);
32b77c31 2224 data->netdev = netdev_ref(netdev);
dfaf79dd 2225 data->dpif_class = dpif_class;
32b77c31 2226 dpif_port_clone(&data->dpif_port, dpif_port);
fa54741e 2227 data->ifindex = ifindex;
32b77c31 2228
fa54741e 2229 hmap_insert(&port_to_netdev, &data->portno_node,
0d8efdc9 2230 netdev_ports_hash(dpif_port->port_no, dpif_class));
fa54741e 2231 hmap_insert(&ifindex_to_port, &data->ifindex_node, ifindex);
32b77c31
PB
2232 ovs_mutex_unlock(&netdev_hmap_mutex);
2233
2234 netdev_init_flow_api(netdev);
2235
2236 return 0;
2237}
2238
2239struct netdev *
dfaf79dd 2240netdev_ports_get(odp_port_t port_no, const struct dpif_class *dpif_class)
32b77c31
PB
2241{
2242 struct port_to_netdev_data *data;
2243 struct netdev *ret = NULL;
2244
2245 ovs_mutex_lock(&netdev_hmap_mutex);
dfaf79dd 2246 data = netdev_ports_lookup(port_no, dpif_class);
32b77c31
PB
2247 if (data) {
2248 ret = netdev_ref(data->netdev);
2249 }
2250 ovs_mutex_unlock(&netdev_hmap_mutex);
2251
2252 return ret;
2253}
2254
2255int
dfaf79dd 2256netdev_ports_remove(odp_port_t port_no, const struct dpif_class *dpif_class)
32b77c31
PB
2257{
2258 struct port_to_netdev_data *data;
2259 int ret = ENOENT;
2260
2261 ovs_mutex_lock(&netdev_hmap_mutex);
2262
dfaf79dd 2263 data = netdev_ports_lookup(port_no, dpif_class);
32b77c31
PB
2264 if (data) {
2265 dpif_port_destroy(&data->dpif_port);
2266 netdev_close(data->netdev); /* unref and possibly close */
fa54741e
BP
2267 hmap_remove(&port_to_netdev, &data->portno_node);
2268 hmap_remove(&ifindex_to_port, &data->ifindex_node);
32b77c31
PB
2269 free(data);
2270 ret = 0;
2271 }
2272
2273 ovs_mutex_unlock(&netdev_hmap_mutex);
2274
2275 return ret;
2276}
2277
2278odp_port_t
2279netdev_ifindex_to_odp_port(int ifindex)
2280{
fa54741e 2281 struct port_to_netdev_data *data;
32b77c31
PB
2282 odp_port_t ret = 0;
2283
2284 ovs_mutex_lock(&netdev_hmap_mutex);
fa54741e 2285 HMAP_FOR_EACH_WITH_HASH (data, ifindex_node, ifindex, &ifindex_to_port) {
86395553 2286 if (data->ifindex == ifindex) {
fa54741e 2287 ret = data->dpif_port.port_no;
86395553
BP
2288 break;
2289 }
32b77c31
PB
2290 }
2291 ovs_mutex_unlock(&netdev_hmap_mutex);
2292
2293 return ret;
2294}
2295
f7dde6df 2296void
dfaf79dd 2297netdev_ports_flow_flush(const struct dpif_class *dpif_class)
f7dde6df
PB
2298{
2299 struct port_to_netdev_data *data;
2300
2301 ovs_mutex_lock(&netdev_hmap_mutex);
fa54741e 2302 HMAP_FOR_EACH (data, portno_node, &port_to_netdev) {
dfaf79dd 2303 if (data->dpif_class == dpif_class) {
f7dde6df
PB
2304 netdev_flow_flush(data->netdev);
2305 }
2306 }
2307 ovs_mutex_unlock(&netdev_hmap_mutex);
2308}
2309
f2280b41 2310struct netdev_flow_dump **
dfaf79dd 2311netdev_ports_flow_dump_create(const struct dpif_class *dpif_class, int *ports)
f2280b41
PB
2312{
2313 struct port_to_netdev_data *data;
2314 struct netdev_flow_dump **dumps;
2315 int count = 0;
2316 int i = 0;
2317
2318 ovs_mutex_lock(&netdev_hmap_mutex);
fa54741e 2319 HMAP_FOR_EACH (data, portno_node, &port_to_netdev) {
dfaf79dd 2320 if (data->dpif_class == dpif_class) {
f2280b41
PB
2321 count++;
2322 }
2323 }
2324
2325 dumps = count ? xzalloc(sizeof *dumps * count) : NULL;
2326
fa54741e 2327 HMAP_FOR_EACH (data, portno_node, &port_to_netdev) {
dfaf79dd 2328 if (data->dpif_class == dpif_class) {
f2280b41
PB
2329 if (netdev_flow_dump_create(data->netdev, &dumps[i])) {
2330 continue;
2331 }
2332
2333 dumps[i]->port = data->dpif_port.port_no;
2334 i++;
2335 }
2336 }
2337 ovs_mutex_unlock(&netdev_hmap_mutex);
2338
2339 *ports = i;
2340 return dumps;
2341}
2342
0335a89c 2343int
dfaf79dd
RD
2344netdev_ports_flow_del(const struct dpif_class *dpif_class,
2345 const ovs_u128 *ufid,
0335a89c
PB
2346 struct dpif_flow_stats *stats)
2347{
2348 struct port_to_netdev_data *data;
2349
2350 ovs_mutex_lock(&netdev_hmap_mutex);
fa54741e 2351 HMAP_FOR_EACH (data, portno_node, &port_to_netdev) {
dfaf79dd
RD
2352 if (data->dpif_class == dpif_class
2353 && !netdev_flow_del(data->netdev, ufid, stats)) {
0335a89c
PB
2354 ovs_mutex_unlock(&netdev_hmap_mutex);
2355 return 0;
2356 }
2357 }
2358 ovs_mutex_unlock(&netdev_hmap_mutex);
2359
2360 return ENOENT;
2361}
2362
6c343984 2363int
dfaf79dd
RD
2364netdev_ports_flow_get(const struct dpif_class *dpif_class, struct match *match,
2365 struct nlattr **actions, const ovs_u128 *ufid,
2366 struct dpif_flow_stats *stats, struct ofpbuf *buf)
6c343984
PB
2367{
2368 struct port_to_netdev_data *data;
2369
2370 ovs_mutex_lock(&netdev_hmap_mutex);
fa54741e 2371 HMAP_FOR_EACH (data, portno_node, &port_to_netdev) {
dfaf79dd
RD
2372 if (data->dpif_class == dpif_class
2373 && !netdev_flow_get(data->netdev, match, actions,
2374 ufid, stats, buf)) {
6c343984
PB
2375 ovs_mutex_unlock(&netdev_hmap_mutex);
2376 return 0;
2377 }
2378 }
2379 ovs_mutex_unlock(&netdev_hmap_mutex);
2380 return ENOENT;
2381}
2382
53611f7b 2383#ifdef __linux__
1067cf93
PB
2384static void
2385netdev_ports_flow_init(void)
2386{
2387 struct port_to_netdev_data *data;
2388
2389 ovs_mutex_lock(&netdev_hmap_mutex);
fa54741e 2390 HMAP_FOR_EACH (data, portno_node, &port_to_netdev) {
1067cf93
PB
2391 netdev_init_flow_api(data->netdev);
2392 }
2393 ovs_mutex_unlock(&netdev_hmap_mutex);
2394}
2395
53611f7b
PB
2396void
2397netdev_set_flow_api_enabled(const struct smap *ovs_other_config)
2398{
2399 if (smap_get_bool(ovs_other_config, "hw-offload", false)) {
2400 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
2401
2402 if (ovsthread_once_start(&once)) {
2403 netdev_flow_api_enabled = true;
2404
2405 VLOG_INFO("netdev: Flow API Enabled");
2406
691d20cb
PB
2407 tc_set_policy(smap_get_def(ovs_other_config, "tc-policy",
2408 TC_POLICY_DEFAULT));
2409
1067cf93
PB
2410 netdev_ports_flow_init();
2411
53611f7b
PB
2412 ovsthread_once_done(&once);
2413 }
2414 }
2415}
2416#else
2417void
2418netdev_set_flow_api_enabled(const struct smap *ovs_other_config OVS_UNUSED)
2419{
2420}
2421#endif