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