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