]> git.proxmox.com Git - mirror_ovs.git/blame - lib/netdev.c
netdev: Minor formatting improvements.
[mirror_ovs.git] / lib / netdev.c
CommitLineData
064af421 1/*
275707c3 2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 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
27#include "coverage.h"
94a53842 28#include "dpif.h"
064af421
BP
29#include "dynamic-string.h"
30#include "fatal-signal.h"
149f577a 31#include "hash.h"
064af421 32#include "list.h"
8b61709d 33#include "netdev-provider.h"
2b9d6589 34#include "netdev-vport.h"
064af421 35#include "ofpbuf.h"
622ee2cf 36#include "openflow/openflow.h"
064af421
BP
37#include "packets.h"
38#include "poll-loop.h"
e9e28be3 39#include "shash.h"
79f1cbe9 40#include "smap.h"
b3c01ed3 41#include "sset.h"
064af421 42#include "svec.h"
064af421
BP
43#include "vlog.h"
44
d98e6007 45VLOG_DEFINE_THIS_MODULE(netdev);
5136ce49 46
d76f09ea
BP
47COVERAGE_DEFINE(netdev_received);
48COVERAGE_DEFINE(netdev_sent);
49COVERAGE_DEFINE(netdev_add_router);
50COVERAGE_DEFINE(netdev_get_stats);
51
4b609110 52struct netdev_saved_flags {
b5d57fc8
BP
53 struct netdev *netdev;
54 struct list node; /* In struct netdev's saved_flags_list. */
4b609110
BP
55 enum netdev_flags saved_flags;
56 enum netdev_flags saved_values;
57};
58
77909859 59static struct shash netdev_classes = SHASH_INITIALIZER(&netdev_classes);
064af421 60
6c88d577 61/* All created network devices. */
b5d57fc8 62static struct shash netdev_shash = SHASH_INITIALIZER(&netdev_shash);
064af421 63
064af421
BP
64/* This is set pretty low because we probably won't learn anything from the
65 * additional log messages. */
66static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
67
4b609110 68static void restore_all_flags(void *aux OVS_UNUSED);
b5d57fc8 69void update_device_args(struct netdev *, const struct shash *args);
8b61709d 70
77909859 71static void
8b61709d 72netdev_initialize(void)
064af421 73{
2b9d6589 74 static bool inited;
e3830e90 75
2b9d6589
BP
76 if (!inited) {
77 inited = true;
8b61709d 78
4b609110 79 fatal_signal_add_hook(restore_all_flags, NULL, NULL, true);
c060c4cf 80 netdev_vport_patch_register();
064af421 81
361906b1 82#ifdef LINUX_DATAPATH
2b9d6589 83 netdev_register_provider(&netdev_linux_class);
c3827f61 84 netdev_register_provider(&netdev_internal_class);
2b9d6589 85 netdev_register_provider(&netdev_tap_class);
c060c4cf 86 netdev_vport_tunnel_register();
f6eb6b20 87#endif
666afb55 88#if defined(__FreeBSD__) || defined(__NetBSD__)
f6eb6b20
GL
89 netdev_register_provider(&netdev_tap_class);
90 netdev_register_provider(&netdev_bsd_class);
2b9d6589 91#endif
064af421 92 }
064af421
BP
93}
94
8b61709d
BP
95/* Performs periodic work needed by all the various kinds of netdevs.
96 *
97 * If your program opens any netdevs, it must call this function within its
98 * main poll loop. */
99void
100netdev_run(void)
064af421 101{
77909859
JG
102 struct shash_node *node;
103 SHASH_FOR_EACH(node, &netdev_classes) {
7dab847a
JG
104 const struct netdev_class *netdev_class = node->data;
105 if (netdev_class->run) {
106 netdev_class->run();
064af421 107 }
064af421
BP
108 }
109}
110
8b61709d
BP
111/* Arranges for poll_block() to wake up when netdev_run() needs to be called.
112 *
113 * If your program opens any netdevs, it must call this function within its
114 * main poll loop. */
115void
116netdev_wait(void)
064af421 117{
77909859
JG
118 struct shash_node *node;
119 SHASH_FOR_EACH(node, &netdev_classes) {
7dab847a
JG
120 const struct netdev_class *netdev_class = node->data;
121 if (netdev_class->wait) {
122 netdev_class->wait();
8b61709d 123 }
064af421 124 }
064af421
BP
125}
126
77909859
JG
127/* Initializes and registers a new netdev provider. After successful
128 * registration, new netdevs of that type can be opened using netdev_open(). */
129int
130netdev_register_provider(const struct netdev_class *new_class)
131{
77909859
JG
132 if (shash_find(&netdev_classes, new_class->type)) {
133 VLOG_WARN("attempted to register duplicate netdev provider: %s",
134 new_class->type);
135 return EEXIST;
136 }
137
138 if (new_class->init) {
139 int error = new_class->init();
140 if (error) {
141 VLOG_ERR("failed to initialize %s network device class: %s",
10a89ef0 142 new_class->type, ovs_strerror(error));
77909859
JG
143 return error;
144 }
145 }
146
2b9d6589 147 shash_add(&netdev_classes, new_class->type, new_class);
77909859
JG
148
149 return 0;
150}
151
152/* Unregisters a netdev provider. 'type' must have been previously
153 * registered and not currently be in use by any netdevs. After unregistration
154 * new netdevs of that type cannot be opened using netdev_open(). */
155int
156netdev_unregister_provider(const char *type)
157{
b5d57fc8 158 struct shash_node *del_node, *netdev_node;
77909859
JG
159
160 del_node = shash_find(&netdev_classes, type);
161 if (!del_node) {
162 VLOG_WARN("attempted to unregister a netdev provider that is not "
163 "registered: %s", type);
164 return EAFNOSUPPORT;
165 }
166
b5d57fc8
BP
167 SHASH_FOR_EACH (netdev_node, &netdev_shash) {
168 struct netdev *netdev = netdev_node->data;
169 if (!strcmp(netdev->netdev_class->type, type)) {
77909859
JG
170 VLOG_WARN("attempted to unregister in use netdev provider: %s",
171 type);
172 return EBUSY;
173 }
174 }
175
176 shash_delete(&netdev_classes, del_node);
77909859
JG
177
178 return 0;
179}
180
c3827f61
BP
181const struct netdev_class *
182netdev_lookup_provider(const char *type)
183{
184 netdev_initialize();
185 return shash_find_data(&netdev_classes, type && type[0] ? type : "system");
186}
187
77909859 188/* Clears 'types' and enumerates the types of all currently registered netdev
19993ef3 189 * providers into it. The caller must first initialize the sset. */
77909859 190void
19993ef3 191netdev_enumerate_types(struct sset *types)
77909859
JG
192{
193 struct shash_node *node;
194
195 netdev_initialize();
19993ef3 196 sset_clear(types);
77909859
JG
197
198 SHASH_FOR_EACH(node, &netdev_classes) {
199 const struct netdev_class *netdev_class = node->data;
19993ef3 200 sset_add(types, netdev_class->type);
77909859
JG
201 }
202}
203
94a53842
AW
204/* Check that the network device name is not the same as any of the registered
205 * vport providers' dpif_port name (dpif_port is NULL if the vport provider
206 * does not define it) or the datapath internal port name (e.g. ovs-system).
207 *
208 * Returns true if there is a name conflict, false otherwise. */
209bool
210netdev_is_reserved_name(const char *name)
211{
212 struct shash_node *node;
213
214 netdev_initialize();
215 SHASH_FOR_EACH (node, &netdev_classes) {
216 const char *dpif_port;
217 dpif_port = netdev_vport_class_get_dpif_port(node->data);
218 if (dpif_port && !strcmp(dpif_port, name)) {
219 return true;
220 }
221 }
222
223 if (!strncmp(name, "ovs-", 4)) {
224 struct sset types;
225 const char *type;
226
227 sset_init(&types);
228 dp_enumerate_types(&types);
229 SSET_FOR_EACH (type, &types) {
230 if (!strcmp(name+4, type)) {
231 sset_destroy(&types);
232 return true;
233 }
234 }
235 sset_destroy(&types);
236 }
237
238 return false;
239}
240
18812dff
BP
241/* Opens the network device named 'name' (e.g. "eth0") of the specified 'type'
242 * (e.g. "system") and returns zero if successful, otherwise a positive errno
243 * value. On success, sets '*netdevp' to the new network device, otherwise to
244 * null.
064af421 245 *
de5cdb90
BP
246 * Some network devices may need to be configured (with netdev_set_config())
247 * before they can be used. */
064af421 248int
18812dff 249netdev_open(const char *name, const char *type, struct netdev **netdevp)
064af421 250{
b5d57fc8 251 struct netdev *netdev;
064af421 252 int error;
064af421 253
149f577a 254 *netdevp = NULL;
559843ed 255 netdev_initialize();
6c88d577 256
b5d57fc8
BP
257 netdev = shash_find_data(&netdev_shash, name);
258 if (!netdev) {
c3827f61
BP
259 const struct netdev_class *class;
260
18812dff 261 class = netdev_lookup_provider(type);
c3827f61
BP
262 if (!class) {
263 VLOG_WARN("could not create netdev %s of unknown type %s",
18812dff 264 name, type);
c3827f61
BP
265 return EAFNOSUPPORT;
266 }
b5d57fc8 267 error = class->create(class, name, &netdev);
149f577a
JG
268 if (error) {
269 return error;
270 }
b5d57fc8 271 ovs_assert(netdev->netdev_class == class);
149f577a 272
6c88d577 273 }
b5d57fc8 274 netdev->ref_cnt++;
064af421 275
b5d57fc8
BP
276 *netdevp = netdev;
277 return 0;
064af421
BP
278}
279
e20ae811
EJ
280/* Returns a reference to 'netdev_' for the caller to own. Returns null if
281 * 'netdev_' is null. */
0bb0393a
BP
282struct netdev *
283netdev_ref(const struct netdev *netdev_)
284{
285 struct netdev *netdev = CONST_CAST(struct netdev *, netdev_);
286
e20ae811
EJ
287 if (netdev) {
288 ovs_assert(netdev->ref_cnt > 0);
289 netdev->ref_cnt++;
290 }
0bb0393a
BP
291 return netdev;
292}
293
149f577a
JG
294/* Reconfigures the device 'netdev' with 'args'. 'args' may be empty
295 * or NULL if none are needed. */
296int
79f1cbe9 297netdev_set_config(struct netdev *netdev, const struct smap *args)
149f577a 298{
b5d57fc8 299 if (netdev->netdev_class->set_config) {
79f1cbe9 300 struct smap no_args = SMAP_INITIALIZER(&no_args);
b5d57fc8
BP
301 return netdev->netdev_class->set_config(netdev,
302 args ? args : &no_args);
79f1cbe9 303 } else if (args && !smap_is_empty(args)) {
de5cdb90
BP
304 VLOG_WARN("%s: arguments provided to device that is not configurable",
305 netdev_get_name(netdev));
149f577a
JG
306 }
307
308 return 0;
309}
310
de5cdb90 311/* Returns the current configuration for 'netdev' in 'args'. The caller must
79f1cbe9 312 * have already initialized 'args' with smap_init(). Returns 0 on success, in
de5cdb90
BP
313 * which case 'args' will be filled with 'netdev''s configuration. On failure
314 * returns a positive errno value, in which case 'args' will be empty.
6d9e6eb4 315 *
de5cdb90 316 * The caller owns 'args' and its contents and must eventually free them with
79f1cbe9 317 * smap_destroy(). */
de5cdb90 318int
79f1cbe9 319netdev_get_config(const struct netdev *netdev, struct smap *args)
6d9e6eb4 320{
de5cdb90
BP
321 int error;
322
79f1cbe9 323 smap_clear(args);
b5d57fc8
BP
324 if (netdev->netdev_class->get_config) {
325 error = netdev->netdev_class->get_config(netdev, args);
de5cdb90 326 if (error) {
79f1cbe9 327 smap_clear(args);
de5cdb90
BP
328 }
329 } else {
330 error = 0;
331 }
332
333 return error;
6d9e6eb4
BP
334}
335
f431bf7d
EJ
336const struct netdev_tunnel_config *
337netdev_get_tunnel_config(const struct netdev *netdev)
338{
b5d57fc8
BP
339 if (netdev->netdev_class->get_tunnel_config) {
340 return netdev->netdev_class->get_tunnel_config(netdev);
f431bf7d
EJ
341 } else {
342 return NULL;
343 }
344}
345
4b609110 346static void
b5d57fc8 347netdev_unref(struct netdev *dev)
4b609110
BP
348{
349 ovs_assert(dev->ref_cnt);
350 if (!--dev->ref_cnt) {
b5d57fc8 351 netdev_uninit(dev, true);
4b609110
BP
352 }
353}
354
064af421
BP
355/* Closes and destroys 'netdev'. */
356void
357netdev_close(struct netdev *netdev)
358{
359 if (netdev) {
b5d57fc8 360 netdev_unref(netdev);
064af421
BP
361 }
362}
363
a75531e5
BP
364/* Parses 'netdev_name_', which is of the form [type@]name into its component
365 * pieces. 'name' and 'type' must be freed by the caller. */
366void
367netdev_parse_name(const char *netdev_name_, char **name, char **type)
368{
369 char *netdev_name = xstrdup(netdev_name_);
370 char *separator;
371
372 separator = strchr(netdev_name, '@');
373 if (separator) {
374 *separator = '\0';
375 *type = netdev_name;
376 *name = xstrdup(separator + 1);
377 } else {
378 *name = netdev_name;
379 *type = xstrdup("system");
380 }
381}
382
7b6b0ef4 383int
796223f5 384netdev_rx_open(struct netdev *netdev, struct netdev_rx **rxp)
7b6b0ef4 385{
796223f5 386 int error;
7b6b0ef4 387
b5d57fc8
BP
388 error = (netdev->netdev_class->rx_open
389 ? netdev->netdev_class->rx_open(netdev, rxp)
796223f5
BP
390 : EOPNOTSUPP);
391 if (!error) {
b5d57fc8
BP
392 ovs_assert((*rxp)->netdev == netdev);
393 netdev->ref_cnt++;
796223f5
BP
394 } else {
395 *rxp = NULL;
396 }
397 return error;
398}
399
400void
401netdev_rx_close(struct netdev_rx *rx)
402{
403 if (rx) {
b5d57fc8 404 struct netdev *dev = rx->netdev;
796223f5
BP
405
406 rx->rx_class->destroy(rx);
b5d57fc8 407 netdev_unref(dev);
796223f5 408 }
7b6b0ef4
BP
409}
410
064af421 411int
796223f5 412netdev_rx_recv(struct netdev_rx *rx, struct ofpbuf *buffer)
064af421 413{
8b61709d 414 int retval;
064af421 415
cb22974d
BP
416 ovs_assert(buffer->size == 0);
417 ovs_assert(ofpbuf_tailroom(buffer) >= ETH_TOTAL_MIN);
8b61709d 418
796223f5 419 retval = rx->rx_class->recv(rx, buffer->data, ofpbuf_tailroom(buffer));
8b61709d 420 if (retval >= 0) {
064af421 421 COVERAGE_INC(netdev_received);
8b61709d
BP
422 buffer->size += retval;
423 if (buffer->size < ETH_TOTAL_MIN) {
424 ofpbuf_put_zeros(buffer, ETH_TOTAL_MIN - buffer->size);
425 }
064af421 426 return 0;
8b61709d
BP
427 } else {
428 return -retval;
064af421
BP
429 }
430}
431
064af421 432void
796223f5 433netdev_rx_wait(struct netdev_rx *rx)
064af421 434{
796223f5 435 rx->rx_class->wait(rx);
064af421
BP
436}
437
064af421 438int
796223f5 439netdev_rx_drain(struct netdev_rx *rx)
064af421 440{
796223f5 441 return rx->rx_class->drain ? rx->rx_class->drain(rx) : 0;
064af421
BP
442}
443
444/* Sends 'buffer' on 'netdev'. Returns 0 if successful, otherwise a positive
445 * errno value. Returns EAGAIN without blocking if the packet cannot be queued
446 * immediately. Returns EMSGSIZE if a partial packet was transmitted or if
447 * the packet is too big or too small to transmit on the device.
448 *
449 * The caller retains ownership of 'buffer' in all cases.
450 *
451 * The kernel maintains a packet transmission queue, so the caller is not
1ac98180
BP
452 * expected to do additional queuing of packets.
453 *
454 * Some network devices may not implement support for this function. In such
455 * cases this function will always return EOPNOTSUPP. */
064af421
BP
456int
457netdev_send(struct netdev *netdev, const struct ofpbuf *buffer)
458{
1ac98180
BP
459 int error;
460
b5d57fc8
BP
461 error = (netdev->netdev_class->send
462 ? netdev->netdev_class->send(netdev, buffer->data, buffer->size)
463 : EOPNOTSUPP);
8b61709d 464 if (!error) {
064af421 465 COVERAGE_INC(netdev_sent);
064af421 466 }
8b61709d 467 return error;
064af421
BP
468}
469
470/* Registers with the poll loop to wake up from the next call to poll_block()
471 * when the packet transmission queue has sufficient room to transmit a packet
472 * with netdev_send().
473 *
474 * The kernel maintains a packet transmission queue, so the client is not
475 * expected to do additional queuing of packets. Thus, this function is
476 * unlikely to ever be used. It is included for completeness. */
477void
478netdev_send_wait(struct netdev *netdev)
479{
b5d57fc8
BP
480 if (netdev->netdev_class->send_wait) {
481 netdev->netdev_class->send_wait(netdev);
1ac98180 482 }
064af421
BP
483}
484
485/* Attempts to set 'netdev''s MAC address to 'mac'. Returns 0 if successful,
486 * otherwise a positive errno value. */
487int
488netdev_set_etheraddr(struct netdev *netdev, const uint8_t mac[ETH_ADDR_LEN])
489{
b5d57fc8 490 return netdev->netdev_class->set_etheraddr(netdev, mac);
064af421
BP
491}
492
80992a35
BP
493/* Retrieves 'netdev''s MAC address. If successful, returns 0 and copies the
494 * the MAC address into 'mac'. On failure, returns a positive errno value and
495 * clears 'mac' to all-zeros. */
496int
497netdev_get_etheraddr(const struct netdev *netdev, uint8_t mac[ETH_ADDR_LEN])
064af421 498{
b5d57fc8 499 return netdev->netdev_class->get_etheraddr(netdev, mac);
064af421
BP
500}
501
502/* Returns the name of the network device that 'netdev' represents,
503 * e.g. "eth0". The caller must not modify or free the returned string. */
504const char *
505netdev_get_name(const struct netdev *netdev)
506{
b5d57fc8 507 return netdev->name;
064af421
BP
508}
509
3d222126
BP
510/* Retrieves the MTU of 'netdev'. The MTU is the maximum size of transmitted
511 * (and received) packets, in bytes, not including the hardware header; thus,
512 * this is typically 1500 bytes for Ethernet devices.
513 *
9b020780
PS
514 * If successful, returns 0 and stores the MTU size in '*mtup'. Returns
515 * EOPNOTSUPP if 'netdev' does not have an MTU (as e.g. some tunnels do not).
14622f22
BP
516 * On other failure, returns a positive errno value. On failure, sets '*mtup'
517 * to 0. */
064af421 518int
3d222126 519netdev_get_mtu(const struct netdev *netdev, int *mtup)
064af421 520{
b5d57fc8 521 const struct netdev_class *class = netdev->netdev_class;
14622f22
BP
522 int error;
523
524 error = class->get_mtu ? class->get_mtu(netdev, mtup) : EOPNOTSUPP;
525 if (error) {
526 *mtup = 0;
527 if (error != EOPNOTSUPP) {
90a6637d 528 VLOG_DBG_RL(&rl, "failed to retrieve MTU for network device %s: "
10a89ef0 529 "%s", netdev_get_name(netdev), ovs_strerror(error));
14622f22 530 }
8b61709d
BP
531 }
532 return error;
064af421
BP
533}
534
9b020780
PS
535/* Sets the MTU of 'netdev'. The MTU is the maximum size of transmitted
536 * (and received) packets, in bytes.
537 *
538 * If successful, returns 0. Returns EOPNOTSUPP if 'netdev' does not have an
539 * MTU (as e.g. some tunnels do not). On other failure, returns a positive
540 * errno value. */
541int
542netdev_set_mtu(const struct netdev *netdev, int mtu)
543{
b5d57fc8 544 const struct netdev_class *class = netdev->netdev_class;
14622f22 545 int error;
9b020780 546
14622f22 547 error = class->set_mtu ? class->set_mtu(netdev, mtu) : EOPNOTSUPP;
9b020780 548 if (error && error != EOPNOTSUPP) {
90a6637d 549 VLOG_DBG_RL(&rl, "failed to set MTU for network device %s: %s",
10a89ef0 550 netdev_get_name(netdev), ovs_strerror(error));
9b020780
PS
551 }
552
553 return error;
554}
555
9ab3d9a3
BP
556/* Returns the ifindex of 'netdev', if successful, as a positive number. On
557 * failure, returns a negative errno value.
558 *
559 * The desired semantics of the ifindex value are a combination of those
560 * specified by POSIX for if_nametoindex() and by SNMP for ifIndex. An ifindex
561 * value should be unique within a host and remain stable at least until
562 * reboot. SNMP says an ifindex "ranges between 1 and the value of ifNumber"
563 * but many systems do not follow this rule anyhow.
4c0f1780
JG
564 *
565 * Some network devices may not implement support for this function. In such
566 * cases this function will always return -EOPNOTSUPP.
9ab3d9a3
BP
567 */
568int
569netdev_get_ifindex(const struct netdev *netdev)
570{
4c0f1780
JG
571 int (*get_ifindex)(const struct netdev *);
572
b5d57fc8 573 get_ifindex = netdev->netdev_class->get_ifindex;
4c0f1780
JG
574
575 return get_ifindex ? get_ifindex(netdev) : -EOPNOTSUPP;
9ab3d9a3
BP
576}
577
064af421
BP
578/* Stores the features supported by 'netdev' into each of '*current',
579 * '*advertised', '*supported', and '*peer' that are non-null. Each value is a
580 * bitmap of "enum ofp_port_features" bits, in host byte order. Returns 0 if
581 * successful, otherwise a positive errno value. On failure, all of the
4c0f1780
JG
582 * passed-in values are set to 0.
583 *
584 * Some network devices may not implement support for this function. In such
c1fdab01 585 * cases this function will always return EOPNOTSUPP. */
064af421 586int
6f2f5cce 587netdev_get_features(const struct netdev *netdev,
6c038611
BP
588 enum netdev_features *current,
589 enum netdev_features *advertised,
590 enum netdev_features *supported,
591 enum netdev_features *peer)
064af421 592{
6f2f5cce 593 int (*get_features)(const struct netdev *netdev,
6c038611
BP
594 enum netdev_features *current,
595 enum netdev_features *advertised,
596 enum netdev_features *supported,
597 enum netdev_features *peer);
598 enum netdev_features dummy[4];
7671589a
BP
599 int error;
600
601 if (!current) {
602 current = &dummy[0];
603 }
604 if (!advertised) {
605 advertised = &dummy[1];
606 }
607 if (!supported) {
608 supported = &dummy[2];
609 }
610 if (!peer) {
611 peer = &dummy[3];
612 }
613
b5d57fc8 614 get_features = netdev->netdev_class->get_features;
4c0f1780 615 error = get_features
a00ca915
EJ
616 ? get_features(netdev, current, advertised, supported,
617 peer)
4c0f1780 618 : EOPNOTSUPP;
7671589a
BP
619 if (error) {
620 *current = *advertised = *supported = *peer = 0;
621 }
622 return error;
064af421
BP
623}
624
6c038611
BP
625/* Returns the maximum speed of a network connection that has the NETDEV_F_*
626 * bits in 'features', in bits per second. If no bits that indicate a speed
d02a5f8e 627 * are set in 'features', returns 'default_bps'. */
622ee2cf 628uint64_t
d02a5f8e
BP
629netdev_features_to_bps(enum netdev_features features,
630 uint64_t default_bps)
622ee2cf
BP
631{
632 enum {
6c038611
BP
633 F_1000000MB = NETDEV_F_1TB_FD,
634 F_100000MB = NETDEV_F_100GB_FD,
635 F_40000MB = NETDEV_F_40GB_FD,
636 F_10000MB = NETDEV_F_10GB_FD,
637 F_1000MB = NETDEV_F_1GB_HD | NETDEV_F_1GB_FD,
638 F_100MB = NETDEV_F_100MB_HD | NETDEV_F_100MB_FD,
639 F_10MB = NETDEV_F_10MB_HD | NETDEV_F_10MB_FD
622ee2cf
BP
640 };
641
6c038611
BP
642 return ( features & F_1000000MB ? UINT64_C(1000000000000)
643 : features & F_100000MB ? UINT64_C(100000000000)
644 : features & F_40000MB ? UINT64_C(40000000000)
645 : features & F_10000MB ? UINT64_C(10000000000)
646 : features & F_1000MB ? UINT64_C(1000000000)
647 : features & F_100MB ? UINT64_C(100000000)
648 : features & F_10MB ? UINT64_C(10000000)
d02a5f8e 649 : default_bps);
622ee2cf
BP
650}
651
6c038611
BP
652/* Returns true if any of the NETDEV_F_* bits that indicate a full-duplex link
653 * are set in 'features', otherwise false. */
622ee2cf 654bool
6c038611 655netdev_features_is_full_duplex(enum netdev_features features)
622ee2cf 656{
6c038611
BP
657 return (features & (NETDEV_F_10MB_FD | NETDEV_F_100MB_FD | NETDEV_F_1GB_FD
658 | NETDEV_F_10GB_FD | NETDEV_F_40GB_FD
659 | NETDEV_F_100GB_FD | NETDEV_F_1TB_FD)) != 0;
622ee2cf
BP
660}
661
8b61709d
BP
662/* Set the features advertised by 'netdev' to 'advertise'. Returns 0 if
663 * successful, otherwise a positive errno value. */
064af421 664int
6c038611
BP
665netdev_set_advertisements(struct netdev *netdev,
666 enum netdev_features advertise)
064af421 667{
b5d57fc8
BP
668 return (netdev->netdev_class->set_advertisements
669 ? netdev->netdev_class->set_advertisements(
a4af0040 670 netdev, advertise)
8b61709d 671 : EOPNOTSUPP);
064af421
BP
672}
673
f1acd62b
BP
674/* If 'netdev' has an assigned IPv4 address, sets '*address' to that address
675 * and '*netmask' to its netmask and returns 0. Otherwise, returns a positive
676 * errno value and sets '*address' to 0 (INADDR_ANY).
8b61709d
BP
677 *
678 * The following error values have well-defined meanings:
679 *
680 * - EADDRNOTAVAIL: 'netdev' has no assigned IPv4 address.
681 *
682 * - EOPNOTSUPP: No IPv4 network stack attached to 'netdev'.
683 *
b53055f4 684 * 'address' or 'netmask' or both may be null, in which case the address or
c1fdab01 685 * netmask is not reported. */
6b9bd979 686int
f1acd62b
BP
687netdev_get_in4(const struct netdev *netdev,
688 struct in_addr *address_, struct in_addr *netmask_)
064af421 689{
f1acd62b
BP
690 struct in_addr address;
691 struct in_addr netmask;
064af421
BP
692 int error;
693
b5d57fc8
BP
694 error = (netdev->netdev_class->get_in4
695 ? netdev->netdev_class->get_in4(netdev,
a4af0040 696 &address, &netmask)
8b61709d 697 : EOPNOTSUPP);
f1acd62b
BP
698 if (address_) {
699 address_->s_addr = error ? 0 : address.s_addr;
700 }
701 if (netmask_) {
702 netmask_->s_addr = error ? 0 : netmask.s_addr;
064af421
BP
703 }
704 return error;
705}
706
707/* Assigns 'addr' as 'netdev''s IPv4 address and 'mask' as its netmask. If
708 * 'addr' is INADDR_ANY, 'netdev''s IPv4 address is cleared. Returns a
709 * positive errno value. */
710int
711netdev_set_in4(struct netdev *netdev, struct in_addr addr, struct in_addr mask)
712{
b5d57fc8
BP
713 return (netdev->netdev_class->set_in4
714 ? netdev->netdev_class->set_in4(netdev, addr, mask)
8b61709d 715 : EOPNOTSUPP);
064af421
BP
716}
717
733adf2a
LG
718/* Obtains ad IPv4 address from device name and save the address in
719 * in4. Returns 0 if successful, otherwise a positive errno value.
720 */
721int
722netdev_get_in4_by_name(const char *device_name, struct in_addr *in4)
723{
724 struct netdev *netdev;
725 int error;
726
727 error = netdev_open(device_name, "system", &netdev);
728 if (error) {
729 in4->s_addr = htonl(0);
730 return error;
731 }
732
733 error = netdev_get_in4(netdev, in4, NULL);
734 netdev_close(netdev);
735 return error;
736}
737
0efaf4b5
BP
738/* Adds 'router' as a default IP gateway for the TCP/IP stack that corresponds
739 * to 'netdev'. */
064af421 740int
8b61709d 741netdev_add_router(struct netdev *netdev, struct in_addr router)
064af421 742{
064af421 743 COVERAGE_INC(netdev_add_router);
b5d57fc8
BP
744 return (netdev->netdev_class->add_router
745 ? netdev->netdev_class->add_router(netdev, router)
8b61709d 746 : EOPNOTSUPP);
064af421
BP
747}
748
f1acd62b
BP
749/* Looks up the next hop for 'host' for the TCP/IP stack that corresponds to
750 * 'netdev'. If a route cannot not be determined, sets '*next_hop' to 0,
751 * '*netdev_name' to null, and returns a positive errno value. Otherwise, if a
752 * next hop is found, stores the next hop gateway's address (0 if 'host' is on
753 * a directly connected network) in '*next_hop' and a copy of the name of the
754 * device to reach 'host' in '*netdev_name', and returns 0. The caller is
755 * responsible for freeing '*netdev_name' (by calling free()). */
756int
757netdev_get_next_hop(const struct netdev *netdev,
758 const struct in_addr *host, struct in_addr *next_hop,
759 char **netdev_name)
760{
b5d57fc8
BP
761 int error = (netdev->netdev_class->get_next_hop
762 ? netdev->netdev_class->get_next_hop(
a4af0040 763 host, next_hop, netdev_name)
f1acd62b 764 : EOPNOTSUPP);
064af421 765 if (error) {
f1acd62b
BP
766 next_hop->s_addr = 0;
767 *netdev_name = NULL;
064af421
BP
768 }
769 return error;
770}
771
79f1cbe9 772/* Populates 'smap' with status information.
ea763e0e 773 *
79f1cbe9
EJ
774 * Populates 'smap' with 'netdev' specific status information. This
775 * information may be used to populate the status column of the Interface table
776 * as defined in ovs-vswitchd.conf.db(5). */
ea763e0e 777int
275707c3 778netdev_get_status(const struct netdev *netdev, struct smap *smap)
ea83a2fc 779{
b5d57fc8
BP
780 return (netdev->netdev_class->get_status
781 ? netdev->netdev_class->get_status(netdev, smap)
ea763e0e 782 : EOPNOTSUPP);
ea83a2fc
EJ
783}
784
8b61709d
BP
785/* If 'netdev' has an assigned IPv6 address, sets '*in6' to that address and
786 * returns 0. Otherwise, returns a positive errno value and sets '*in6' to
787 * all-zero-bits (in6addr_any).
788 *
789 * The following error values have well-defined meanings:
790 *
791 * - EADDRNOTAVAIL: 'netdev' has no assigned IPv6 address.
792 *
793 * - EOPNOTSUPP: No IPv6 network stack attached to 'netdev'.
794 *
795 * 'in6' may be null, in which case the address itself is not reported. */
064af421 796int
8b61709d 797netdev_get_in6(const struct netdev *netdev, struct in6_addr *in6)
064af421 798{
8b61709d
BP
799 struct in6_addr dummy;
800 int error;
b1bf7d43 801
b5d57fc8
BP
802 error = (netdev->netdev_class->get_in6
803 ? netdev->netdev_class->get_in6(netdev,
a4af0040 804 in6 ? in6 : &dummy)
8b61709d
BP
805 : EOPNOTSUPP);
806 if (error && in6) {
807 memset(in6, 0, sizeof *in6);
b1bf7d43 808 }
8b61709d 809 return error;
064af421
BP
810}
811
812/* On 'netdev', turns off the flags in 'off' and then turns on the flags in
4b609110 813 * 'on'. Returns 0 if successful, otherwise a positive errno value. */
064af421
BP
814static int
815do_update_flags(struct netdev *netdev, enum netdev_flags off,
8b61709d 816 enum netdev_flags on, enum netdev_flags *old_flagsp,
4b609110 817 struct netdev_saved_flags **sfp)
064af421 818{
4b609110 819 struct netdev_saved_flags *sf = NULL;
8b61709d 820 enum netdev_flags old_flags;
064af421
BP
821 int error;
822
b5d57fc8
BP
823 error = netdev->netdev_class->update_flags(netdev, off & ~on, on,
824 &old_flags);
064af421 825 if (error) {
8b61709d
BP
826 VLOG_WARN_RL(&rl, "failed to %s flags for network device %s: %s",
827 off || on ? "set" : "get", netdev_get_name(netdev),
10a89ef0 828 ovs_strerror(error));
8b61709d 829 old_flags = 0;
4b609110 830 } else if ((off || on) && sfp) {
8b61709d
BP
831 enum netdev_flags new_flags = (old_flags & ~off) | on;
832 enum netdev_flags changed_flags = old_flags ^ new_flags;
833 if (changed_flags) {
4b609110 834 *sfp = sf = xmalloc(sizeof *sf);
b5d57fc8
BP
835 sf->netdev = netdev;
836 list_push_front(&netdev->saved_flags_list, &sf->node);
4b609110
BP
837 sf->saved_flags = changed_flags;
838 sf->saved_values = changed_flags & new_flags;
839
b5d57fc8 840 netdev->ref_cnt++;
8b61709d 841 }
064af421 842 }
4b609110 843
8b61709d
BP
844 if (old_flagsp) {
845 *old_flagsp = old_flags;
064af421 846 }
4b609110
BP
847 if (sfp) {
848 *sfp = sf;
849 }
850
064af421
BP
851 return error;
852}
853
8b61709d
BP
854/* Obtains the current flags for 'netdev' and stores them into '*flagsp'.
855 * Returns 0 if successful, otherwise a positive errno value. On failure,
856 * stores 0 into '*flagsp'. */
857int
858netdev_get_flags(const struct netdev *netdev_, enum netdev_flags *flagsp)
859{
ebc56baa 860 struct netdev *netdev = CONST_CAST(struct netdev *, netdev_);
4b609110 861 return do_update_flags(netdev, 0, 0, flagsp, NULL);
8b61709d
BP
862}
863
064af421 864/* Sets the flags for 'netdev' to 'flags'.
064af421
BP
865 * Returns 0 if successful, otherwise a positive errno value. */
866int
867netdev_set_flags(struct netdev *netdev, enum netdev_flags flags,
4b609110 868 struct netdev_saved_flags **sfp)
064af421 869{
4b609110 870 return do_update_flags(netdev, -1, flags, NULL, sfp);
064af421
BP
871}
872
4b609110
BP
873/* Turns on the specified 'flags' on 'netdev':
874 *
875 * - On success, returns 0. If 'sfp' is nonnull, sets '*sfp' to a newly
876 * allocated 'struct netdev_saved_flags *' that may be passed to
877 * netdev_restore_flags() to restore the original values of 'flags' on
878 * 'netdev' (this will happen automatically at program termination if
879 * netdev_restore_flags() is never called) , or to NULL if no flags were
880 * actually changed.
881 *
882 * - On failure, returns a positive errno value. If 'sfp' is nonnull, sets
883 * '*sfp' to NULL. */
064af421
BP
884int
885netdev_turn_flags_on(struct netdev *netdev, enum netdev_flags flags,
4b609110 886 struct netdev_saved_flags **sfp)
064af421 887{
4b609110 888 return do_update_flags(netdev, 0, flags, NULL, sfp);
064af421
BP
889}
890
4b609110
BP
891/* Turns off the specified 'flags' on 'netdev'. See netdev_turn_flags_on() for
892 * details of the interface. */
064af421
BP
893int
894netdev_turn_flags_off(struct netdev *netdev, enum netdev_flags flags,
4b609110
BP
895 struct netdev_saved_flags **sfp)
896{
897 return do_update_flags(netdev, flags, 0, NULL, sfp);
898}
899
900/* Restores the flags that were saved in 'sf', and destroys 'sf'.
901 * Does nothing if 'sf' is NULL. */
902void
903netdev_restore_flags(struct netdev_saved_flags *sf)
064af421 904{
4b609110 905 if (sf) {
b5d57fc8 906 struct netdev *netdev = sf->netdev;
4b609110
BP
907 enum netdev_flags old_flags;
908
b5d57fc8
BP
909 netdev->netdev_class->update_flags(netdev,
910 sf->saved_flags & sf->saved_values,
911 sf->saved_flags & ~sf->saved_values,
912 &old_flags);
4b609110
BP
913 list_remove(&sf->node);
914 free(sf);
915
b5d57fc8 916 netdev_unref(netdev);
4b609110 917 }
064af421
BP
918}
919
920/* Looks up the ARP table entry for 'ip' on 'netdev'. If one exists and can be
921 * successfully retrieved, it stores the corresponding MAC address in 'mac' and
922 * returns 0. Otherwise, it returns a positive errno value; in particular,
8b61709d 923 * ENXIO indicates that there is no ARP table entry for 'ip' on 'netdev'. */
064af421 924int
8b61709d 925netdev_arp_lookup(const struct netdev *netdev,
dbba996b 926 ovs_be32 ip, uint8_t mac[ETH_ADDR_LEN])
064af421 927{
b5d57fc8 928 int error = (netdev->netdev_class->arp_lookup
15aee116 929 ? netdev->netdev_class->arp_lookup(netdev, ip, mac)
8b61709d 930 : EOPNOTSUPP);
064af421 931 if (error) {
8b61709d 932 memset(mac, 0, ETH_ADDR_LEN);
064af421 933 }
8b61709d 934 return error;
064af421
BP
935}
936
85da620e
JG
937/* Returns true if carrier is active (link light is on) on 'netdev'. */
938bool
939netdev_get_carrier(const struct netdev *netdev)
064af421 940{
85da620e
JG
941 int error;
942 enum netdev_flags flags;
943 bool carrier;
944
945 netdev_get_flags(netdev, &flags);
946 if (!(flags & NETDEV_UP)) {
947 return false;
948 }
949
b5d57fc8 950 if (!netdev->netdev_class->get_carrier) {
85da620e
JG
951 return true;
952 }
953
15aee116 954 error = netdev->netdev_class->get_carrier(netdev, &carrier);
8b61709d 955 if (error) {
85da620e 956 VLOG_DBG("%s: failed to get network device carrier status, assuming "
10a89ef0 957 "down: %s", netdev_get_name(netdev), ovs_strerror(error));
85da620e 958 carrier = false;
064af421 959 }
85da620e
JG
960
961 return carrier;
064af421
BP
962}
963
65c3058c
EJ
964/* Returns the number of times 'netdev''s carrier has changed. */
965long long int
966netdev_get_carrier_resets(const struct netdev *netdev)
967{
b5d57fc8
BP
968 return (netdev->netdev_class->get_carrier_resets
969 ? netdev->netdev_class->get_carrier_resets(netdev)
65c3058c
EJ
970 : 0);
971}
972
1670c579
EJ
973/* Attempts to force netdev_get_carrier() to poll 'netdev''s MII registers for
974 * link status instead of checking 'netdev''s carrier. 'netdev''s MII
975 * registers will be polled once ever 'interval' milliseconds. If 'netdev'
976 * does not support MII, another method may be used as a fallback. If
977 * 'interval' is less than or equal to zero, reverts netdev_get_carrier() to
978 * its normal behavior.
979 *
980 * Returns 0 if successful, otherwise a positive errno value. */
981int
982netdev_set_miimon_interval(struct netdev *netdev, long long int interval)
63331829 983{
b5d57fc8
BP
984 return (netdev->netdev_class->set_miimon_interval
985 ? netdev->netdev_class->set_miimon_interval(netdev, interval)
1670c579 986 : EOPNOTSUPP);
63331829
EJ
987}
988
887fd0ba 989/* Retrieves current device stats for 'netdev'. */
064af421
BP
990int
991netdev_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
992{
993 int error;
994
995 COVERAGE_INC(netdev_get_stats);
b5d57fc8
BP
996 error = (netdev->netdev_class->get_stats
997 ? netdev->netdev_class->get_stats(netdev, stats)
8b61709d 998 : EOPNOTSUPP);
064af421
BP
999 if (error) {
1000 memset(stats, 0xff, sizeof *stats);
1001 }
1002 return error;
1003}
1004
8722022c
BP
1005/* Attempts to change the stats for 'netdev' to those provided in 'stats'.
1006 * Returns 0 if successful, otherwise a positive errno value.
1007 *
1008 * This will probably fail for most network devices. Some devices might only
1009 * allow setting their stats to 0. */
1010int
1011netdev_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
1012{
b5d57fc8
BP
1013 return (netdev->netdev_class->set_stats
1014 ? netdev->netdev_class->set_stats(netdev, stats)
8722022c
BP
1015 : EOPNOTSUPP);
1016}
1017
8b61709d
BP
1018/* Attempts to set input rate limiting (policing) policy, such that up to
1019 * 'kbits_rate' kbps of traffic is accepted, with a maximum accumulative burst
1020 * size of 'kbits' kb. */
064af421 1021int
b1bf7d43
BP
1022netdev_set_policing(struct netdev *netdev, uint32_t kbits_rate,
1023 uint32_t kbits_burst)
064af421 1024{
b5d57fc8
BP
1025 return (netdev->netdev_class->set_policing
1026 ? netdev->netdev_class->set_policing(netdev,
a4af0040 1027 kbits_rate, kbits_burst)
8b61709d 1028 : EOPNOTSUPP);
064af421
BP
1029}
1030
c1c9c9c4
BP
1031/* Adds to 'types' all of the forms of QoS supported by 'netdev', or leaves it
1032 * empty if 'netdev' does not support QoS. Any names added to 'types' should
1033 * be documented as valid for the "type" column in the "QoS" table in
1034 * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
1035 *
1036 * Every network device supports disabling QoS with a type of "", but this type
1037 * will not be added to 'types'.
1038 *
19993ef3 1039 * The caller must initialize 'types' (e.g. with sset_init()) before calling
c1c9c9c4 1040 * this function. The caller is responsible for destroying 'types' (e.g. with
19993ef3 1041 * sset_destroy()) when it is no longer needed.
c1c9c9c4
BP
1042 *
1043 * Returns 0 if successful, otherwise a positive errno value. */
1044int
19993ef3 1045netdev_get_qos_types(const struct netdev *netdev, struct sset *types)
c1c9c9c4 1046{
b5d57fc8 1047 const struct netdev_class *class = netdev->netdev_class;
c1c9c9c4
BP
1048 return (class->get_qos_types
1049 ? class->get_qos_types(netdev, types)
1050 : 0);
1051}
1052
1053/* Queries 'netdev' for its capabilities regarding the specified 'type' of QoS,
1054 * which should be "" or one of the types returned by netdev_get_qos_types()
1055 * for 'netdev'. Returns 0 if successful, otherwise a positive errno value.
1056 * On success, initializes 'caps' with the QoS capabilities; on failure, clears
1057 * 'caps' to all zeros. */
1058int
1059netdev_get_qos_capabilities(const struct netdev *netdev, const char *type,
1060 struct netdev_qos_capabilities *caps)
1061{
b5d57fc8 1062 const struct netdev_class *class = netdev->netdev_class;
c1c9c9c4
BP
1063
1064 if (*type) {
1065 int retval = (class->get_qos_capabilities
1066 ? class->get_qos_capabilities(netdev, type, caps)
1067 : EOPNOTSUPP);
1068 if (retval) {
1069 memset(caps, 0, sizeof *caps);
1070 }
1071 return retval;
1072 } else {
1073 /* Every netdev supports turning off QoS. */
1074 memset(caps, 0, sizeof *caps);
1075 return 0;
1076 }
1077}
1078
1079/* Obtains the number of queues supported by 'netdev' for the specified 'type'
1080 * of QoS. Returns 0 if successful, otherwise a positive errno value. Stores
1081 * the number of queues (zero on failure) in '*n_queuesp'.
1082 *
1083 * This is just a simple wrapper around netdev_get_qos_capabilities(). */
1084int
1085netdev_get_n_queues(const struct netdev *netdev,
1086 const char *type, unsigned int *n_queuesp)
1087{
1088 struct netdev_qos_capabilities caps;
1089 int retval;
1090
1091 retval = netdev_get_qos_capabilities(netdev, type, &caps);
1092 *n_queuesp = caps.n_queues;
1093 return retval;
1094}
1095
1096/* Queries 'netdev' about its currently configured form of QoS. If successful,
1097 * stores the name of the current form of QoS into '*typep', stores any details
1098 * of configuration as string key-value pairs in 'details', and returns 0. On
1099 * failure, sets '*typep' to NULL and returns a positive errno value.
1100 *
1101 * A '*typep' of "" indicates that QoS is currently disabled on 'netdev'.
1102 *
79f1cbe9
EJ
1103 * The caller must initialize 'details' as an empty smap (e.g. with
1104 * smap_init()) before calling this function. The caller must free 'details'
1105 * when it is no longer needed (e.g. with smap_destroy()).
c1c9c9c4
BP
1106 *
1107 * The caller must not modify or free '*typep'.
1108 *
1109 * '*typep' will be one of the types returned by netdev_get_qos_types() for
1110 * 'netdev'. The contents of 'details' should be documented as valid for
1111 * '*typep' in the "other_config" column in the "QoS" table in
1112 * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)). */
1113int
1114netdev_get_qos(const struct netdev *netdev,
79f1cbe9 1115 const char **typep, struct smap *details)
c1c9c9c4 1116{
b5d57fc8 1117 const struct netdev_class *class = netdev->netdev_class;
c1c9c9c4
BP
1118 int retval;
1119
1120 if (class->get_qos) {
1121 retval = class->get_qos(netdev, typep, details);
1122 if (retval) {
1123 *typep = NULL;
79f1cbe9 1124 smap_clear(details);
c1c9c9c4
BP
1125 }
1126 return retval;
1127 } else {
1128 /* 'netdev' doesn't support QoS, so report that QoS is disabled. */
1129 *typep = "";
1130 return 0;
1131 }
1132}
1133
1134/* Attempts to reconfigure QoS on 'netdev', changing the form of QoS to 'type'
1135 * with details of configuration from 'details'. Returns 0 if successful,
1136 * otherwise a positive errno value. On error, the previous QoS configuration
1137 * is retained.
1138 *
1139 * When this function changes the type of QoS (not just 'details'), this also
1140 * resets all queue configuration for 'netdev' to their defaults (which depend
1141 * on the specific type of QoS). Otherwise, the queue configuration for
1142 * 'netdev' is unchanged.
1143 *
1144 * 'type' should be "" (to disable QoS) or one of the types returned by
1145 * netdev_get_qos_types() for 'netdev'. The contents of 'details' should be
1146 * documented as valid for the given 'type' in the "other_config" column in the
1147 * "QoS" table in vswitchd/vswitch.xml (which is built as
1148 * ovs-vswitchd.conf.db(8)).
1149 *
1150 * NULL may be specified for 'details' if there are no configuration
1151 * details. */
1152int
1153netdev_set_qos(struct netdev *netdev,
79f1cbe9 1154 const char *type, const struct smap *details)
c1c9c9c4 1155{
b5d57fc8 1156 const struct netdev_class *class = netdev->netdev_class;
c1c9c9c4
BP
1157
1158 if (!type) {
1159 type = "";
1160 }
1161
1162 if (class->set_qos) {
1163 if (!details) {
edfbe9f7 1164 static const struct smap empty = SMAP_INITIALIZER(&empty);
c1c9c9c4
BP
1165 details = &empty;
1166 }
1167 return class->set_qos(netdev, type, details);
1168 } else {
1169 return *type ? EOPNOTSUPP : 0;
1170 }
1171}
1172
1173/* Queries 'netdev' for information about the queue numbered 'queue_id'. If
1174 * successful, adds that information as string key-value pairs to 'details'.
1175 * Returns 0 if successful, otherwise a positive errno value.
1176 *
1177 * 'queue_id' must be less than the number of queues supported by 'netdev' for
1178 * the current form of QoS (e.g. as returned by netdev_get_n_queues(netdev)).
1179 *
1180 * The returned contents of 'details' should be documented as valid for the
1181 * given 'type' in the "other_config" column in the "Queue" table in
1182 * vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
1183 *
79f1cbe9
EJ
1184 * The caller must initialize 'details' (e.g. with smap_init()) before calling
1185 * this function. The caller must free 'details' when it is no longer needed
1186 * (e.g. with smap_destroy()). */
c1c9c9c4
BP
1187int
1188netdev_get_queue(const struct netdev *netdev,
79f1cbe9 1189 unsigned int queue_id, struct smap *details)
c1c9c9c4 1190{
b5d57fc8 1191 const struct netdev_class *class = netdev->netdev_class;
c1c9c9c4
BP
1192 int retval;
1193
1194 retval = (class->get_queue
1195 ? class->get_queue(netdev, queue_id, details)
1196 : EOPNOTSUPP);
1197 if (retval) {
79f1cbe9 1198 smap_clear(details);
c1c9c9c4
BP
1199 }
1200 return retval;
1201}
1202
1203/* Configures the queue numbered 'queue_id' on 'netdev' with the key-value
1204 * string pairs in 'details'. The contents of 'details' should be documented
1205 * as valid for the given 'type' in the "other_config" column in the "Queue"
1206 * table in vswitchd/vswitch.xml (which is built as ovs-vswitchd.conf.db(8)).
1207 * Returns 0 if successful, otherwise a positive errno value. On failure, the
1208 * given queue's configuration should be unmodified.
1209 *
1210 * 'queue_id' must be less than the number of queues supported by 'netdev' for
1211 * the current form of QoS (e.g. as returned by netdev_get_n_queues(netdev)).
1212 *
1213 * This function does not modify 'details', and the caller retains ownership of
c1fdab01 1214 * it. */
c1c9c9c4
BP
1215int
1216netdev_set_queue(struct netdev *netdev,
79f1cbe9 1217 unsigned int queue_id, const struct smap *details)
c1c9c9c4 1218{
b5d57fc8 1219 const struct netdev_class *class = netdev->netdev_class;
c1c9c9c4
BP
1220 return (class->set_queue
1221 ? class->set_queue(netdev, queue_id, details)
1222 : EOPNOTSUPP);
1223}
1224
1225/* Attempts to delete the queue numbered 'queue_id' from 'netdev'. Some kinds
1226 * of QoS may have a fixed set of queues, in which case attempts to delete them
1227 * will fail with EOPNOTSUPP.
1228 *
1229 * Returns 0 if successful, otherwise a positive errno value. On failure, the
1230 * given queue will be unmodified.
1231 *
1232 * 'queue_id' must be less than the number of queues supported by 'netdev' for
1233 * the current form of QoS (e.g. as returned by
1234 * netdev_get_n_queues(netdev)). */
1235int
1236netdev_delete_queue(struct netdev *netdev, unsigned int queue_id)
1237{
b5d57fc8 1238 const struct netdev_class *class = netdev->netdev_class;
c1c9c9c4
BP
1239 return (class->delete_queue
1240 ? class->delete_queue(netdev, queue_id)
1241 : EOPNOTSUPP);
1242}
1243
1244/* Obtains statistics about 'queue_id' on 'netdev'. On success, returns 0 and
1245 * fills 'stats' with the queue's statistics; individual members of 'stats' may
1246 * be set to all-1-bits if the statistic is unavailable. On failure, returns a
6dc34a0d
BP
1247 * positive errno value and fills 'stats' with values indicating unsupported
1248 * statistics. */
c1c9c9c4
BP
1249int
1250netdev_get_queue_stats(const struct netdev *netdev, unsigned int queue_id,
1251 struct netdev_queue_stats *stats)
1252{
b5d57fc8 1253 const struct netdev_class *class = netdev->netdev_class;
c1c9c9c4
BP
1254 int retval;
1255
1256 retval = (class->get_queue_stats
1257 ? class->get_queue_stats(netdev, queue_id, stats)
1258 : EOPNOTSUPP);
1259 if (retval) {
6dc34a0d
BP
1260 stats->tx_bytes = UINT64_MAX;
1261 stats->tx_packets = UINT64_MAX;
1262 stats->tx_errors = UINT64_MAX;
1263 stats->created = LLONG_MIN;
c1c9c9c4
BP
1264 }
1265 return retval;
1266}
1267
1268/* Iterates over all of 'netdev''s queues, calling 'cb' with the queue's ID,
1269 * its configuration, and the 'aux' specified by the caller. The order of
1270 * iteration is unspecified, but (when successful) each queue is visited
1271 * exactly once.
1272 *
1273 * Calling this function may be more efficient than calling netdev_get_queue()
1274 * for every queue.
1275 *
f486e840
BP
1276 * 'cb' must not modify or free the 'details' argument passed in. It may
1277 * delete or modify the queue passed in as its 'queue_id' argument. It may
1278 * modify but must not delete any other queue within 'netdev'. 'cb' should not
1279 * add new queues because this may cause some queues to be visited twice or not
1280 * at all.
c1c9c9c4
BP
1281 *
1282 * Returns 0 if successful, otherwise a positive errno value. On error, some
1283 * configured queues may not have been included in the iteration. */
1284int
1285netdev_dump_queues(const struct netdev *netdev,
1286 netdev_dump_queues_cb *cb, void *aux)
1287{
b5d57fc8 1288 const struct netdev_class *class = netdev->netdev_class;
c1c9c9c4
BP
1289 return (class->dump_queues
1290 ? class->dump_queues(netdev, cb, aux)
1291 : EOPNOTSUPP);
1292}
1293
1294/* Iterates over all of 'netdev''s queues, calling 'cb' with the queue's ID,
1295 * its statistics, and the 'aux' specified by the caller. The order of
1296 * iteration is unspecified, but (when successful) each queue is visited
1297 * exactly once.
1298 *
1299 * Calling this function may be more efficient than calling
1300 * netdev_get_queue_stats() for every queue.
1301 *
1302 * 'cb' must not modify or free the statistics passed in.
1303 *
1304 * Returns 0 if successful, otherwise a positive errno value. On error, some
1305 * configured queues may not have been included in the iteration. */
1306int
1307netdev_dump_queue_stats(const struct netdev *netdev,
1308 netdev_dump_queue_stats_cb *cb, void *aux)
1309{
b5d57fc8 1310 const struct netdev_class *class = netdev->netdev_class;
c1c9c9c4
BP
1311 return (class->dump_queue_stats
1312 ? class->dump_queue_stats(netdev, cb, aux)
1313 : EOPNOTSUPP);
1314}
1315
ac4d3bcb
EJ
1316/* Returns a sequence number which indicates changes in one of 'netdev''s
1317 * properties. The returned sequence will be nonzero so that callers have a
1318 * value which they may use as a reset when tracking 'netdev'.
1319 *
1320 * The returned sequence number will change whenever 'netdev''s flags,
1321 * features, ethernet address, or carrier changes. It may change for other
1322 * reasons as well, or no reason at all. */
1323unsigned int
1324netdev_change_seq(const struct netdev *netdev)
1325{
b5d57fc8 1326 return netdev->netdev_class->change_seq(netdev);
ac4d3bcb 1327}
8b61709d 1328\f
b5d57fc8 1329/* Initializes 'netdev' as a netdev device named 'name' of the specified
6d9e6eb4
BP
1330 * 'netdev_class'. This function is ordinarily called from a netdev provider's
1331 * 'create' function.
1332 *
b5d57fc8
BP
1333 * This function adds 'netdev' to a netdev-owned shash, so it is very important
1334 * that 'netdev' only be freed after calling netdev_uninit(). */
149f577a 1335void
b5d57fc8
BP
1336netdev_init(struct netdev *netdev, const char *name,
1337 const struct netdev_class *netdev_class)
149f577a 1338{
b5d57fc8 1339 ovs_assert(!shash_find(&netdev_shash, name));
149f577a 1340
b5d57fc8
BP
1341 memset(netdev, 0, sizeof *netdev);
1342 netdev->netdev_class = netdev_class;
1343 netdev->name = xstrdup(name);
1344 netdev->node = shash_add(&netdev_shash, name, netdev);
1345 list_init(&netdev->saved_flags_list);
149f577a
JG
1346}
1347
1348/* Undoes the results of initialization.
1349 *
1350 * Normally this function does not need to be called as netdev_close has
1351 * the same effect when the refcount drops to zero.
1352 * However, it may be called by providers due to an error on creation
1353 * that occurs after initialization. It this case netdev_close() would
1354 * never be called. */
6c88d577 1355void
b5d57fc8 1356netdev_uninit(struct netdev *netdev, bool destroy)
6c88d577 1357{
b5d57fc8 1358 char *name = netdev->name;
149f577a 1359
b5d57fc8
BP
1360 ovs_assert(!netdev->ref_cnt);
1361 ovs_assert(list_is_empty(&netdev->saved_flags_list));
149f577a 1362
b5d57fc8 1363 shash_delete(&netdev_shash, netdev->node);
6c88d577 1364
149f577a 1365 if (destroy) {
b5d57fc8 1366 netdev->netdev_class->destroy(netdev);
149f577a
JG
1367 }
1368 free(name);
6c88d577
JP
1369}
1370
b5d57fc8 1371/* Returns the class type of 'netdev'.
a740f0de
JG
1372 *
1373 * The caller must not free the returned value. */
149f577a 1374const char *
b5d57fc8 1375netdev_get_type(const struct netdev *netdev)
a740f0de 1376{
b5d57fc8 1377 return netdev->netdev_class->type;
a740f0de
JG
1378}
1379
b5d57fc8 1380/* Returns the class associated with 'netdev'. */
15b3596a 1381const struct netdev_class *
b5d57fc8 1382netdev_get_class(const struct netdev *netdev)
a740f0de 1383{
b5d57fc8 1384 return netdev->netdev_class;
a740f0de
JG
1385}
1386
b5d57fc8 1387/* Returns the netdev with 'name' or NULL if there is none.
46415c90
JG
1388 *
1389 * The caller must not free the returned value. */
b5d57fc8
BP
1390struct netdev *
1391netdev_from_name(const char *name)
46415c90 1392{
b5d57fc8 1393 return shash_find_data(&netdev_shash, name);
46415c90
JG
1394}
1395
7dab847a 1396/* Fills 'device_list' with devices that match 'netdev_class'.
46415c90
JG
1397 *
1398 * The caller is responsible for initializing and destroying 'device_list'
b5d57fc8 1399 * but the contained netdevs must not be freed. */
46415c90 1400void
b5d57fc8
BP
1401netdev_get_devices(const struct netdev_class *netdev_class,
1402 struct shash *device_list)
46415c90
JG
1403{
1404 struct shash_node *node;
b5d57fc8
BP
1405 SHASH_FOR_EACH (node, &netdev_shash) {
1406 struct netdev *dev = node->data;
46415c90 1407
7dab847a 1408 if (dev->netdev_class == netdev_class) {
46415c90
JG
1409 shash_add(device_list, node->name, node->data);
1410 }
1411 }
1412}
1413
0a740f48
EJ
1414const char *
1415netdev_get_type_from_name(const char *name)
1416{
b5d57fc8
BP
1417 const struct netdev *dev = netdev_from_name(name);
1418 return dev ? netdev_get_type(dev) : NULL;
6c88d577 1419}
e9e28be3 1420\f
796223f5 1421void
b5d57fc8 1422netdev_rx_init(struct netdev_rx *rx, struct netdev *netdev,
796223f5
BP
1423 const struct netdev_rx_class *class)
1424{
b5d57fc8 1425 ovs_assert(netdev->ref_cnt > 0);
796223f5 1426 rx->rx_class = class;
b5d57fc8 1427 rx->netdev = netdev;
796223f5
BP
1428}
1429
1430void
1431netdev_rx_uninit(struct netdev_rx *rx OVS_UNUSED)
1432{
1433 /* Nothing to do. */
1434}
1435
b5d57fc8
BP
1436struct netdev *
1437netdev_rx_get_netdev(const struct netdev_rx *rx)
796223f5 1438{
b5d57fc8
BP
1439 ovs_assert(rx->netdev->ref_cnt > 0);
1440 return rx->netdev;
796223f5
BP
1441}
1442
1443const char *
1444netdev_rx_get_name(const struct netdev_rx *rx)
1445{
b5d57fc8 1446 return netdev_get_name(netdev_rx_get_netdev(rx));
796223f5
BP
1447}
1448
064af421 1449static void
4b609110 1450restore_all_flags(void *aux OVS_UNUSED)
064af421 1451{
4b609110
BP
1452 struct shash_node *node;
1453
b5d57fc8
BP
1454 SHASH_FOR_EACH (node, &netdev_shash) {
1455 struct netdev *netdev = node->data;
4b609110
BP
1456 const struct netdev_saved_flags *sf;
1457 enum netdev_flags saved_values;
1458 enum netdev_flags saved_flags;
1459
1460 saved_values = saved_flags = 0;
b5d57fc8 1461 LIST_FOR_EACH (sf, node, &netdev->saved_flags_list) {
4b609110
BP
1462 saved_flags |= sf->saved_flags;
1463 saved_values &= ~sf->saved_flags;
1464 saved_values |= sf->saved_flags & sf->saved_values;
1465 }
1466 if (saved_flags) {
1467 enum netdev_flags old_flags;
1468
b5d57fc8
BP
1469 netdev->netdev_class->update_flags(netdev,
1470 saved_flags & saved_values,
1471 saved_flags & ~saved_values,
1472 &old_flags);
4b609110 1473 }
064af421
BP
1474 }
1475}