]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
2f51a7eb | 2 | * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014 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> | |
96fba48f | 18 | #include "dpif-provider.h" |
064af421 | 19 | |
064af421 BP |
20 | #include <ctype.h> |
21 | #include <errno.h> | |
064af421 | 22 | #include <inttypes.h> |
064af421 BP |
23 | #include <stdlib.h> |
24 | #include <string.h> | |
064af421 BP |
25 | |
26 | #include "coverage.h" | |
27 | #include "dynamic-string.h" | |
28 | #include "flow.h" | |
c3827f61 | 29 | #include "netdev.h" |
064af421 | 30 | #include "netlink.h" |
7fd91025 | 31 | #include "odp-execute.h" |
064af421 | 32 | #include "odp-util.h" |
90bf1e07 | 33 | #include "ofp-errors.h" |
064af421 | 34 | #include "ofp-print.h" |
71ce9235 | 35 | #include "ofp-util.h" |
064af421 BP |
36 | #include "ofpbuf.h" |
37 | #include "packets.h" | |
38 | #include "poll-loop.h" | |
999401aa | 39 | #include "shash.h" |
d0c23a1a | 40 | #include "sset.h" |
c97fb132 | 41 | #include "timeval.h" |
064af421 BP |
42 | #include "util.h" |
43 | #include "valgrind.h" | |
064af421 | 44 | #include "vlog.h" |
5136ce49 | 45 | |
d98e6007 | 46 | VLOG_DEFINE_THIS_MODULE(dpif); |
064af421 | 47 | |
d76f09ea BP |
48 | COVERAGE_DEFINE(dpif_destroy); |
49 | COVERAGE_DEFINE(dpif_port_add); | |
50 | COVERAGE_DEFINE(dpif_port_del); | |
51 | COVERAGE_DEFINE(dpif_flow_flush); | |
52 | COVERAGE_DEFINE(dpif_flow_get); | |
53 | COVERAGE_DEFINE(dpif_flow_put); | |
54 | COVERAGE_DEFINE(dpif_flow_del); | |
d76f09ea BP |
55 | COVERAGE_DEFINE(dpif_execute); |
56 | COVERAGE_DEFINE(dpif_purge); | |
7fd91025 | 57 | COVERAGE_DEFINE(dpif_execute_with_help); |
d76f09ea | 58 | |
999401aa | 59 | static const struct dpif_class *base_dpif_classes[] = { |
2f51a7eb | 60 | #ifdef __linux__ |
96fba48f | 61 | &dpif_linux_class, |
c83cdd30 | 62 | #endif |
72865317 | 63 | &dpif_netdev_class, |
c228a364 | 64 | }; |
999401aa JG |
65 | |
66 | struct registered_dpif_class { | |
d2d8fbeb | 67 | const struct dpif_class *dpif_class; |
999401aa JG |
68 | int refcount; |
69 | }; | |
70 | static struct shash dpif_classes = SHASH_INITIALIZER(&dpif_classes); | |
579a77e0 | 71 | static struct sset dpif_blacklist = SSET_INITIALIZER(&dpif_blacklist); |
c228a364 | 72 | |
5703b15f | 73 | /* Protects 'dpif_classes', including the refcount, and 'dpif_blacklist'. */ |
97be1538 | 74 | static struct ovs_mutex dpif_mutex = OVS_MUTEX_INITIALIZER; |
5703b15f | 75 | |
064af421 BP |
76 | /* Rate limit for individual messages going to or from the datapath, output at |
77 | * DBG level. This is very high because, if these are enabled, it is because | |
78 | * we really need to see them. */ | |
79 | static struct vlog_rate_limit dpmsg_rl = VLOG_RATE_LIMIT_INIT(600, 600); | |
80 | ||
81 | /* Not really much point in logging many dpif errors. */ | |
e2781405 | 82 | static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(60, 5); |
064af421 | 83 | |
feebdea2 BP |
84 | static void log_flow_message(const struct dpif *dpif, int error, |
85 | const char *operation, | |
86 | const struct nlattr *key, size_t key_len, | |
61fb711d | 87 | const struct nlattr *mask, size_t mask_len, |
c97fb132 | 88 | const struct dpif_flow_stats *stats, |
feebdea2 | 89 | const struct nlattr *actions, size_t actions_len); |
96fba48f BP |
90 | static void log_operation(const struct dpif *, const char *operation, |
91 | int error); | |
96fba48f | 92 | static bool should_log_flow_message(int error); |
89625d1e BP |
93 | static void log_flow_put_message(struct dpif *, const struct dpif_flow_put *, |
94 | int error); | |
b99d3cee BP |
95 | static void log_flow_del_message(struct dpif *, const struct dpif_flow_del *, |
96 | int error); | |
89625d1e BP |
97 | static void log_execute_message(struct dpif *, const struct dpif_execute *, |
98 | int error); | |
064af421 | 99 | |
999401aa JG |
100 | static void |
101 | dp_initialize(void) | |
102 | { | |
eb8ed438 | 103 | static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER; |
999401aa | 104 | |
eb8ed438 | 105 | if (ovsthread_once_start(&once)) { |
999401aa JG |
106 | int i; |
107 | ||
999401aa JG |
108 | for (i = 0; i < ARRAY_SIZE(base_dpif_classes); i++) { |
109 | dp_register_provider(base_dpif_classes[i]); | |
110 | } | |
eb8ed438 | 111 | ovsthread_once_done(&once); |
999401aa JG |
112 | } |
113 | } | |
114 | ||
5703b15f BP |
115 | static int |
116 | dp_register_provider__(const struct dpif_class *new_class) | |
999401aa JG |
117 | { |
118 | struct registered_dpif_class *registered_class; | |
119 | ||
579a77e0 EJ |
120 | if (sset_contains(&dpif_blacklist, new_class->type)) { |
121 | VLOG_DBG("attempted to register blacklisted provider: %s", | |
122 | new_class->type); | |
123 | return EINVAL; | |
124 | } | |
125 | ||
999401aa JG |
126 | if (shash_find(&dpif_classes, new_class->type)) { |
127 | VLOG_WARN("attempted to register duplicate datapath provider: %s", | |
128 | new_class->type); | |
129 | return EEXIST; | |
130 | } | |
1a6f1e2a | 131 | |
999401aa | 132 | registered_class = xmalloc(sizeof *registered_class); |
d2d8fbeb | 133 | registered_class->dpif_class = new_class; |
999401aa JG |
134 | registered_class->refcount = 0; |
135 | ||
136 | shash_add(&dpif_classes, new_class->type, registered_class); | |
137 | ||
138 | return 0; | |
139 | } | |
140 | ||
5703b15f BP |
141 | /* Registers a new datapath provider. After successful registration, new |
142 | * datapaths of that type can be opened using dpif_open(). */ | |
143 | int | |
144 | dp_register_provider(const struct dpif_class *new_class) | |
145 | { | |
146 | int error; | |
147 | ||
97be1538 | 148 | ovs_mutex_lock(&dpif_mutex); |
5703b15f | 149 | error = dp_register_provider__(new_class); |
97be1538 | 150 | ovs_mutex_unlock(&dpif_mutex); |
5703b15f BP |
151 | |
152 | return error; | |
153 | } | |
154 | ||
999401aa JG |
155 | /* Unregisters a datapath provider. 'type' must have been previously |
156 | * registered and not currently be in use by any dpifs. After unregistration | |
157 | * new datapaths of that type cannot be opened using dpif_open(). */ | |
5703b15f BP |
158 | static int |
159 | dp_unregister_provider__(const char *type) | |
999401aa JG |
160 | { |
161 | struct shash_node *node; | |
162 | struct registered_dpif_class *registered_class; | |
163 | ||
164 | node = shash_find(&dpif_classes, type); | |
165 | if (!node) { | |
166 | VLOG_WARN("attempted to unregister a datapath provider that is not " | |
167 | "registered: %s", type); | |
168 | return EAFNOSUPPORT; | |
169 | } | |
170 | ||
171 | registered_class = node->data; | |
172 | if (registered_class->refcount) { | |
173 | VLOG_WARN("attempted to unregister in use datapath provider: %s", type); | |
174 | return EBUSY; | |
175 | } | |
176 | ||
177 | shash_delete(&dpif_classes, node); | |
178 | free(registered_class); | |
179 | ||
180 | return 0; | |
181 | } | |
182 | ||
5703b15f BP |
183 | /* Unregisters a datapath provider. 'type' must have been previously |
184 | * registered and not currently be in use by any dpifs. After unregistration | |
185 | * new datapaths of that type cannot be opened using dpif_open(). */ | |
186 | int | |
187 | dp_unregister_provider(const char *type) | |
188 | { | |
189 | int error; | |
190 | ||
191 | dp_initialize(); | |
192 | ||
97be1538 | 193 | ovs_mutex_lock(&dpif_mutex); |
5703b15f | 194 | error = dp_unregister_provider__(type); |
97be1538 | 195 | ovs_mutex_unlock(&dpif_mutex); |
5703b15f BP |
196 | |
197 | return error; | |
198 | } | |
199 | ||
579a77e0 EJ |
200 | /* Blacklists a provider. Causes future calls of dp_register_provider() with |
201 | * a dpif_class which implements 'type' to fail. */ | |
202 | void | |
203 | dp_blacklist_provider(const char *type) | |
204 | { | |
97be1538 | 205 | ovs_mutex_lock(&dpif_mutex); |
579a77e0 | 206 | sset_add(&dpif_blacklist, type); |
97be1538 | 207 | ovs_mutex_unlock(&dpif_mutex); |
579a77e0 EJ |
208 | } |
209 | ||
999401aa | 210 | /* Clears 'types' and enumerates the types of all currently registered datapath |
d0c23a1a | 211 | * providers into it. The caller must first initialize the sset. */ |
1a6f1e2a | 212 | void |
d0c23a1a | 213 | dp_enumerate_types(struct sset *types) |
1a6f1e2a | 214 | { |
999401aa | 215 | struct shash_node *node; |
1a6f1e2a | 216 | |
999401aa | 217 | dp_initialize(); |
d0c23a1a | 218 | sset_clear(types); |
1a6f1e2a | 219 | |
97be1538 | 220 | ovs_mutex_lock(&dpif_mutex); |
999401aa JG |
221 | SHASH_FOR_EACH(node, &dpif_classes) { |
222 | const struct registered_dpif_class *registered_class = node->data; | |
d0c23a1a | 223 | sset_add(types, registered_class->dpif_class->type); |
1a6f1e2a | 224 | } |
97be1538 | 225 | ovs_mutex_unlock(&dpif_mutex); |
5703b15f BP |
226 | } |
227 | ||
228 | static void | |
229 | dp_class_unref(struct registered_dpif_class *rc) | |
230 | { | |
97be1538 | 231 | ovs_mutex_lock(&dpif_mutex); |
5703b15f BP |
232 | ovs_assert(rc->refcount); |
233 | rc->refcount--; | |
97be1538 | 234 | ovs_mutex_unlock(&dpif_mutex); |
5703b15f BP |
235 | } |
236 | ||
237 | static struct registered_dpif_class * | |
238 | dp_class_lookup(const char *type) | |
239 | { | |
240 | struct registered_dpif_class *rc; | |
241 | ||
97be1538 | 242 | ovs_mutex_lock(&dpif_mutex); |
5703b15f BP |
243 | rc = shash_find_data(&dpif_classes, type); |
244 | if (rc) { | |
245 | rc->refcount++; | |
246 | } | |
97be1538 | 247 | ovs_mutex_unlock(&dpif_mutex); |
5703b15f BP |
248 | |
249 | return rc; | |
1a6f1e2a JG |
250 | } |
251 | ||
252 | /* Clears 'names' and enumerates the names of all known created datapaths with | |
d0c23a1a | 253 | * the given 'type'. The caller must first initialize the sset. Returns 0 if |
1a6f1e2a | 254 | * successful, otherwise a positive errno value. |
d3d22744 BP |
255 | * |
256 | * Some kinds of datapaths might not be practically enumerable. This is not | |
257 | * considered an error. */ | |
258 | int | |
d0c23a1a | 259 | dp_enumerate_names(const char *type, struct sset *names) |
d3d22744 | 260 | { |
5703b15f | 261 | struct registered_dpif_class *registered_class; |
999401aa JG |
262 | const struct dpif_class *dpif_class; |
263 | int error; | |
d3d22744 | 264 | |
999401aa | 265 | dp_initialize(); |
d0c23a1a | 266 | sset_clear(names); |
1a6f1e2a | 267 | |
5703b15f | 268 | registered_class = dp_class_lookup(type); |
999401aa JG |
269 | if (!registered_class) { |
270 | VLOG_WARN("could not enumerate unknown type: %s", type); | |
271 | return EAFNOSUPPORT; | |
272 | } | |
1a6f1e2a | 273 | |
d2d8fbeb | 274 | dpif_class = registered_class->dpif_class; |
2240af25 DDP |
275 | error = (dpif_class->enumerate |
276 | ? dpif_class->enumerate(names, dpif_class) | |
277 | : 0); | |
999401aa JG |
278 | if (error) { |
279 | VLOG_WARN("failed to enumerate %s datapaths: %s", dpif_class->type, | |
10a89ef0 | 280 | ovs_strerror(error)); |
d3d22744 | 281 | } |
5703b15f | 282 | dp_class_unref(registered_class); |
1a6f1e2a | 283 | |
999401aa | 284 | return error; |
1a6f1e2a JG |
285 | } |
286 | ||
54ed8a5d BP |
287 | /* Parses 'datapath_name_', which is of the form [type@]name into its |
288 | * component pieces. 'name' and 'type' must be freed by the caller. | |
289 | * | |
290 | * The returned 'type' is normalized, as if by dpif_normalize_type(). */ | |
1a6f1e2a JG |
291 | void |
292 | dp_parse_name(const char *datapath_name_, char **name, char **type) | |
293 | { | |
294 | char *datapath_name = xstrdup(datapath_name_); | |
295 | char *separator; | |
296 | ||
297 | separator = strchr(datapath_name, '@'); | |
298 | if (separator) { | |
299 | *separator = '\0'; | |
300 | *type = datapath_name; | |
54ed8a5d | 301 | *name = xstrdup(dpif_normalize_type(separator + 1)); |
1a6f1e2a JG |
302 | } else { |
303 | *name = datapath_name; | |
54ed8a5d | 304 | *type = xstrdup(dpif_normalize_type(NULL)); |
1a6f1e2a | 305 | } |
d3d22744 BP |
306 | } |
307 | ||
96fba48f | 308 | static int |
1a6f1e2a | 309 | do_open(const char *name, const char *type, bool create, struct dpif **dpifp) |
064af421 | 310 | { |
96fba48f | 311 | struct dpif *dpif = NULL; |
064af421 | 312 | int error; |
999401aa JG |
313 | struct registered_dpif_class *registered_class; |
314 | ||
315 | dp_initialize(); | |
064af421 | 316 | |
3a225db7 | 317 | type = dpif_normalize_type(type); |
5703b15f | 318 | registered_class = dp_class_lookup(type); |
999401aa JG |
319 | if (!registered_class) { |
320 | VLOG_WARN("could not create datapath %s of unknown type %s", name, | |
321 | type); | |
322 | error = EAFNOSUPPORT; | |
323 | goto exit; | |
324 | } | |
325 | ||
4a387741 BP |
326 | error = registered_class->dpif_class->open(registered_class->dpif_class, |
327 | name, create, &dpif); | |
999401aa | 328 | if (!error) { |
cb22974d | 329 | ovs_assert(dpif->dpif_class == registered_class->dpif_class); |
5703b15f BP |
330 | } else { |
331 | dp_class_unref(registered_class); | |
064af421 | 332 | } |
064af421 | 333 | |
96fba48f BP |
334 | exit: |
335 | *dpifp = error ? NULL : dpif; | |
336 | return error; | |
064af421 BP |
337 | } |
338 | ||
1a6f1e2a JG |
339 | /* Tries to open an existing datapath named 'name' and type 'type'. Will fail |
340 | * if no datapath with 'name' and 'type' exists. 'type' may be either NULL or | |
341 | * the empty string to specify the default system type. Returns 0 if | |
342 | * successful, otherwise a positive errno value. On success stores a pointer | |
343 | * to the datapath in '*dpifp', otherwise a null pointer. */ | |
96fba48f | 344 | int |
1a6f1e2a | 345 | dpif_open(const char *name, const char *type, struct dpif **dpifp) |
064af421 | 346 | { |
1a6f1e2a | 347 | return do_open(name, type, false, dpifp); |
064af421 BP |
348 | } |
349 | ||
1a6f1e2a JG |
350 | /* Tries to create and open a new datapath with the given 'name' and 'type'. |
351 | * 'type' may be either NULL or the empty string to specify the default system | |
352 | * type. Will fail if a datapath with 'name' and 'type' already exists. | |
353 | * Returns 0 if successful, otherwise a positive errno value. On success | |
354 | * stores a pointer to the datapath in '*dpifp', otherwise a null pointer. */ | |
064af421 | 355 | int |
1a6f1e2a | 356 | dpif_create(const char *name, const char *type, struct dpif **dpifp) |
064af421 | 357 | { |
1a6f1e2a | 358 | return do_open(name, type, true, dpifp); |
96fba48f | 359 | } |
064af421 | 360 | |
1a6f1e2a JG |
361 | /* Tries to open a datapath with the given 'name' and 'type', creating it if it |
362 | * does not exist. 'type' may be either NULL or the empty string to specify | |
363 | * the default system type. Returns 0 if successful, otherwise a positive | |
364 | * errno value. On success stores a pointer to the datapath in '*dpifp', | |
365 | * otherwise a null pointer. */ | |
efacbce6 | 366 | int |
1a6f1e2a | 367 | dpif_create_and_open(const char *name, const char *type, struct dpif **dpifp) |
efacbce6 BP |
368 | { |
369 | int error; | |
370 | ||
1a6f1e2a | 371 | error = dpif_create(name, type, dpifp); |
efacbce6 | 372 | if (error == EEXIST || error == EBUSY) { |
1a6f1e2a | 373 | error = dpif_open(name, type, dpifp); |
efacbce6 BP |
374 | if (error) { |
375 | VLOG_WARN("datapath %s already exists but cannot be opened: %s", | |
10a89ef0 | 376 | name, ovs_strerror(error)); |
efacbce6 BP |
377 | } |
378 | } else if (error) { | |
10a89ef0 BP |
379 | VLOG_WARN("failed to create datapath %s: %s", |
380 | name, ovs_strerror(error)); | |
efacbce6 BP |
381 | } |
382 | return error; | |
383 | } | |
384 | ||
96fba48f BP |
385 | /* Closes and frees the connection to 'dpif'. Does not destroy the datapath |
386 | * itself; call dpif_delete() first, instead, if that is desirable. */ | |
387 | void | |
388 | dpif_close(struct dpif *dpif) | |
389 | { | |
390 | if (dpif) { | |
5703b15f | 391 | struct registered_dpif_class *rc; |
999401aa | 392 | |
5703b15f | 393 | rc = shash_find_data(&dpif_classes, dpif->dpif_class->type); |
999401aa | 394 | dpif_uninit(dpif, true); |
5703b15f | 395 | dp_class_unref(rc); |
064af421 BP |
396 | } |
397 | } | |
398 | ||
640e1b20 BP |
399 | /* Performs periodic work needed by 'dpif'. */ |
400 | void | |
401 | dpif_run(struct dpif *dpif) | |
402 | { | |
403 | if (dpif->dpif_class->run) { | |
404 | dpif->dpif_class->run(dpif); | |
405 | } | |
406 | } | |
407 | ||
408 | /* Arranges for poll_block() to wake up when dp_run() needs to be called for | |
409 | * 'dpif'. */ | |
410 | void | |
411 | dpif_wait(struct dpif *dpif) | |
412 | { | |
413 | if (dpif->dpif_class->wait) { | |
414 | dpif->dpif_class->wait(dpif); | |
415 | } | |
416 | } | |
417 | ||
1a6f1e2a JG |
418 | /* Returns the name of datapath 'dpif' prefixed with the type |
419 | * (for use in log messages). */ | |
b29ba128 BP |
420 | const char * |
421 | dpif_name(const struct dpif *dpif) | |
422 | { | |
1a6f1e2a JG |
423 | return dpif->full_name; |
424 | } | |
425 | ||
426 | /* Returns the name of datapath 'dpif' without the type | |
427 | * (for use in device names). */ | |
428 | const char * | |
429 | dpif_base_name(const struct dpif *dpif) | |
430 | { | |
431 | return dpif->base_name; | |
b29ba128 BP |
432 | } |
433 | ||
c7a26215 JP |
434 | /* Returns the type of datapath 'dpif'. */ |
435 | const char * | |
436 | dpif_type(const struct dpif *dpif) | |
437 | { | |
438 | return dpif->dpif_class->type; | |
439 | } | |
440 | ||
3a225db7 BP |
441 | /* Returns the fully spelled out name for the given datapath 'type'. |
442 | * | |
443 | * Normalized type string can be compared with strcmp(). Unnormalized type | |
444 | * string might be the same even if they have different spellings. */ | |
445 | const char * | |
446 | dpif_normalize_type(const char *type) | |
447 | { | |
448 | return type && type[0] ? type : "system"; | |
449 | } | |
450 | ||
96fba48f BP |
451 | /* Destroys the datapath that 'dpif' is connected to, first removing all of its |
452 | * ports. After calling this function, it does not make sense to pass 'dpif' | |
453 | * to any functions other than dpif_name() or dpif_close(). */ | |
064af421 BP |
454 | int |
455 | dpif_delete(struct dpif *dpif) | |
456 | { | |
96fba48f BP |
457 | int error; |
458 | ||
064af421 | 459 | COVERAGE_INC(dpif_destroy); |
96fba48f | 460 | |
1acb6baa | 461 | error = dpif->dpif_class->destroy(dpif); |
96fba48f BP |
462 | log_operation(dpif, "delete", error); |
463 | return error; | |
064af421 BP |
464 | } |
465 | ||
96fba48f BP |
466 | /* Retrieves statistics for 'dpif' into 'stats'. Returns 0 if successful, |
467 | * otherwise a positive errno value. */ | |
064af421 | 468 | int |
a8d9304d | 469 | dpif_get_dp_stats(const struct dpif *dpif, struct dpif_dp_stats *stats) |
064af421 | 470 | { |
1acb6baa | 471 | int error = dpif->dpif_class->get_stats(dpif, stats); |
96fba48f BP |
472 | if (error) { |
473 | memset(stats, 0, sizeof *stats); | |
474 | } | |
475 | log_operation(dpif, "get_stats", error); | |
476 | return error; | |
064af421 BP |
477 | } |
478 | ||
0aeaabc8 JP |
479 | const char * |
480 | dpif_port_open_type(const char *datapath_type, const char *port_type) | |
481 | { | |
5703b15f | 482 | struct registered_dpif_class *rc; |
0aeaabc8 JP |
483 | |
484 | datapath_type = dpif_normalize_type(datapath_type); | |
485 | ||
97be1538 | 486 | ovs_mutex_lock(&dpif_mutex); |
5703b15f BP |
487 | rc = shash_find_data(&dpif_classes, datapath_type); |
488 | if (rc && rc->dpif_class->port_open_type) { | |
489 | port_type = rc->dpif_class->port_open_type(rc->dpif_class, port_type); | |
0aeaabc8 | 490 | } |
97be1538 | 491 | ovs_mutex_unlock(&dpif_mutex); |
0aeaabc8 | 492 | |
5703b15f | 493 | return port_type; |
0aeaabc8 JP |
494 | } |
495 | ||
232dfa4a | 496 | /* Attempts to add 'netdev' as a port on 'dpif'. If 'port_nop' is |
4e022ec0 | 497 | * non-null and its value is not ODPP_NONE, then attempts to use the |
232dfa4a JP |
498 | * value as the port number. |
499 | * | |
500 | * If successful, returns 0 and sets '*port_nop' to the new port's port | |
501 | * number (if 'port_nop' is non-null). On failure, returns a positive | |
4e022ec0 | 502 | * errno value and sets '*port_nop' to ODPP_NONE (if 'port_nop' is |
232dfa4a | 503 | * non-null). */ |
064af421 | 504 | int |
4e022ec0 | 505 | dpif_port_add(struct dpif *dpif, struct netdev *netdev, odp_port_t *port_nop) |
064af421 | 506 | { |
c3827f61 | 507 | const char *netdev_name = netdev_get_name(netdev); |
4e022ec0 | 508 | odp_port_t port_no = ODPP_NONE; |
9ee3ae3e | 509 | int error; |
064af421 BP |
510 | |
511 | COVERAGE_INC(dpif_port_add); | |
9ee3ae3e | 512 | |
232dfa4a JP |
513 | if (port_nop) { |
514 | port_no = *port_nop; | |
515 | } | |
516 | ||
c3827f61 | 517 | error = dpif->dpif_class->port_add(dpif, netdev, &port_no); |
9ee3ae3e | 518 | if (!error) { |
9b56fe13 | 519 | VLOG_DBG_RL(&dpmsg_rl, "%s: added %s as port %"PRIu32, |
c3827f61 | 520 | dpif_name(dpif), netdev_name, port_no); |
064af421 | 521 | } else { |
9ee3ae3e | 522 | VLOG_WARN_RL(&error_rl, "%s: failed to add %s as port: %s", |
10a89ef0 | 523 | dpif_name(dpif), netdev_name, ovs_strerror(error)); |
4e022ec0 | 524 | port_no = ODPP_NONE; |
9ee3ae3e BP |
525 | } |
526 | if (port_nop) { | |
527 | *port_nop = port_no; | |
064af421 | 528 | } |
9ee3ae3e | 529 | return error; |
064af421 BP |
530 | } |
531 | ||
96fba48f BP |
532 | /* Attempts to remove 'dpif''s port number 'port_no'. Returns 0 if successful, |
533 | * otherwise a positive errno value. */ | |
064af421 | 534 | int |
4e022ec0 | 535 | dpif_port_del(struct dpif *dpif, odp_port_t port_no) |
064af421 | 536 | { |
96fba48f BP |
537 | int error; |
538 | ||
064af421 | 539 | COVERAGE_INC(dpif_port_del); |
96fba48f | 540 | |
1acb6baa | 541 | error = dpif->dpif_class->port_del(dpif, port_no); |
a1811296 | 542 | if (!error) { |
9b56fe13 | 543 | VLOG_DBG_RL(&dpmsg_rl, "%s: port_del(%"PRIu32")", |
a1811296 BP |
544 | dpif_name(dpif), port_no); |
545 | } else { | |
546 | log_operation(dpif, "port_del", error); | |
547 | } | |
96fba48f | 548 | return error; |
064af421 BP |
549 | } |
550 | ||
4c738a8d BP |
551 | /* Makes a deep copy of 'src' into 'dst'. */ |
552 | void | |
553 | dpif_port_clone(struct dpif_port *dst, const struct dpif_port *src) | |
554 | { | |
555 | dst->name = xstrdup(src->name); | |
556 | dst->type = xstrdup(src->type); | |
557 | dst->port_no = src->port_no; | |
558 | } | |
559 | ||
560 | /* Frees memory allocated to members of 'dpif_port'. | |
561 | * | |
562 | * Do not call this function on a dpif_port obtained from | |
563 | * dpif_port_dump_next(): that function retains ownership of the data in the | |
564 | * dpif_port. */ | |
565 | void | |
566 | dpif_port_destroy(struct dpif_port *dpif_port) | |
567 | { | |
568 | free(dpif_port->name); | |
569 | free(dpif_port->type); | |
570 | } | |
571 | ||
4afba28d JP |
572 | /* Checks if port named 'devname' exists in 'dpif'. If so, returns |
573 | * true; otherwise, returns false. */ | |
574 | bool | |
575 | dpif_port_exists(const struct dpif *dpif, const char *devname) | |
576 | { | |
577 | int error = dpif->dpif_class->port_query_by_name(dpif, devname, NULL); | |
bee6b8bc | 578 | if (error != 0 && error != ENOENT && error != ENODEV) { |
4afba28d | 579 | VLOG_WARN_RL(&error_rl, "%s: failed to query port %s: %s", |
10a89ef0 | 580 | dpif_name(dpif), devname, ovs_strerror(error)); |
4afba28d JP |
581 | } |
582 | ||
583 | return !error; | |
584 | } | |
585 | ||
96fba48f BP |
586 | /* Looks up port number 'port_no' in 'dpif'. On success, returns 0 and |
587 | * initializes '*port' appropriately; on failure, returns a positive errno | |
4c738a8d BP |
588 | * value. |
589 | * | |
590 | * The caller owns the data in 'port' and must free it with | |
591 | * dpif_port_destroy() when it is no longer needed. */ | |
064af421 | 592 | int |
4e022ec0 | 593 | dpif_port_query_by_number(const struct dpif *dpif, odp_port_t port_no, |
4c738a8d | 594 | struct dpif_port *port) |
064af421 | 595 | { |
1acb6baa | 596 | int error = dpif->dpif_class->port_query_by_number(dpif, port_no, port); |
96fba48f | 597 | if (!error) { |
9b56fe13 | 598 | VLOG_DBG_RL(&dpmsg_rl, "%s: port %"PRIu32" is device %s", |
4c738a8d | 599 | dpif_name(dpif), port_no, port->name); |
064af421 | 600 | } else { |
96fba48f | 601 | memset(port, 0, sizeof *port); |
9b56fe13 | 602 | VLOG_WARN_RL(&error_rl, "%s: failed to query port %"PRIu32": %s", |
10a89ef0 | 603 | dpif_name(dpif), port_no, ovs_strerror(error)); |
064af421 | 604 | } |
96fba48f | 605 | return error; |
064af421 BP |
606 | } |
607 | ||
96fba48f BP |
608 | /* Looks up port named 'devname' in 'dpif'. On success, returns 0 and |
609 | * initializes '*port' appropriately; on failure, returns a positive errno | |
4c738a8d BP |
610 | * value. |
611 | * | |
612 | * The caller owns the data in 'port' and must free it with | |
613 | * dpif_port_destroy() when it is no longer needed. */ | |
064af421 BP |
614 | int |
615 | dpif_port_query_by_name(const struct dpif *dpif, const char *devname, | |
4c738a8d | 616 | struct dpif_port *port) |
064af421 | 617 | { |
1acb6baa | 618 | int error = dpif->dpif_class->port_query_by_name(dpif, devname, port); |
96fba48f | 619 | if (!error) { |
9b56fe13 | 620 | VLOG_DBG_RL(&dpmsg_rl, "%s: device %s is on port %"PRIu32, |
4c738a8d | 621 | dpif_name(dpif), devname, port->port_no); |
064af421 | 622 | } else { |
96fba48f BP |
623 | memset(port, 0, sizeof *port); |
624 | ||
d647f0a7 BP |
625 | /* For ENOENT or ENODEV we use DBG level because the caller is probably |
626 | * interested in whether 'dpif' actually has a port 'devname', so that | |
627 | * it's not an issue worth logging if it doesn't. Other errors are | |
628 | * uncommon and more likely to indicate a real problem. */ | |
629 | VLOG_RL(&error_rl, | |
630 | error == ENOENT || error == ENODEV ? VLL_DBG : VLL_WARN, | |
631 | "%s: failed to query port %s: %s", | |
10a89ef0 | 632 | dpif_name(dpif), devname, ovs_strerror(error)); |
064af421 | 633 | } |
96fba48f | 634 | return error; |
064af421 BP |
635 | } |
636 | ||
1954e6bb AW |
637 | /* Returns the Netlink PID value to supply in OVS_ACTION_ATTR_USERSPACE |
638 | * actions as the OVS_USERSPACE_ATTR_PID attribute's value, for use in | |
639 | * flows whose packets arrived on port 'port_no'. In the case where the | |
640 | * provider allocates multiple Netlink PIDs to a single port, it may use | |
641 | * 'hash' to spread load among them. The caller need not use a particular | |
642 | * hash function; a 5-tuple hash is suitable. | |
643 | * | |
644 | * (The datapath implementation might use some different hash function for | |
645 | * distributing packets received via flow misses among PIDs. This means | |
646 | * that packets received via flow misses might be reordered relative to | |
647 | * packets received via userspace actions. This is not ordinarily a | |
648 | * problem.) | |
98403001 | 649 | * |
4e022ec0 | 650 | * A 'port_no' of ODPP_NONE is a special case: it returns a reserved PID, not |
625b0720 BP |
651 | * allocated to any port, that the client may use for special purposes. |
652 | * | |
98403001 BP |
653 | * The return value is only meaningful when DPIF_UC_ACTION has been enabled in |
654 | * the 'dpif''s listen mask. It is allowed to change when DPIF_UC_ACTION is | |
655 | * disabled and then re-enabled, so a client that does that must be prepared to | |
656 | * update all of the flows that it installed that contain | |
657 | * OVS_ACTION_ATTR_USERSPACE actions. */ | |
658 | uint32_t | |
1954e6bb | 659 | dpif_port_get_pid(const struct dpif *dpif, odp_port_t port_no, uint32_t hash) |
98403001 BP |
660 | { |
661 | return (dpif->dpif_class->port_get_pid | |
1954e6bb | 662 | ? (dpif->dpif_class->port_get_pid)(dpif, port_no, hash) |
98403001 BP |
663 | : 0); |
664 | } | |
665 | ||
96fba48f BP |
666 | /* Looks up port number 'port_no' in 'dpif'. On success, returns 0 and copies |
667 | * the port's name into the 'name_size' bytes in 'name', ensuring that the | |
668 | * result is null-terminated. On failure, returns a positive errno value and | |
669 | * makes 'name' the empty string. */ | |
335562c0 | 670 | int |
4e022ec0 | 671 | dpif_port_get_name(struct dpif *dpif, odp_port_t port_no, |
335562c0 BP |
672 | char *name, size_t name_size) |
673 | { | |
4c738a8d | 674 | struct dpif_port port; |
335562c0 BP |
675 | int error; |
676 | ||
cb22974d | 677 | ovs_assert(name_size > 0); |
335562c0 BP |
678 | |
679 | error = dpif_port_query_by_number(dpif, port_no, &port); | |
680 | if (!error) { | |
4c738a8d BP |
681 | ovs_strlcpy(name, port.name, name_size); |
682 | dpif_port_destroy(&port); | |
335562c0 BP |
683 | } else { |
684 | *name = '\0'; | |
685 | } | |
686 | return error; | |
687 | } | |
688 | ||
b0ec0f27 | 689 | /* Initializes 'dump' to begin dumping the ports in a dpif. |
96fba48f | 690 | * |
b0ec0f27 BP |
691 | * This function provides no status indication. An error status for the entire |
692 | * dump operation is provided when it is completed by calling | |
693 | * dpif_port_dump_done(). | |
694 | */ | |
695 | void | |
696 | dpif_port_dump_start(struct dpif_port_dump *dump, const struct dpif *dpif) | |
697 | { | |
698 | dump->dpif = dpif; | |
699 | dump->error = dpif->dpif_class->port_dump_start(dpif, &dump->state); | |
700 | log_operation(dpif, "port_dump_start", dump->error); | |
701 | } | |
702 | ||
703 | /* Attempts to retrieve another port from 'dump', which must have been | |
4c738a8d | 704 | * initialized with dpif_port_dump_start(). On success, stores a new dpif_port |
b0ec0f27 | 705 | * into 'port' and returns true. On failure, returns false. |
96fba48f | 706 | * |
b0ec0f27 BP |
707 | * Failure might indicate an actual error or merely that the last port has been |
708 | * dumped. An error status for the entire dump operation is provided when it | |
4c738a8d BP |
709 | * is completed by calling dpif_port_dump_done(). |
710 | * | |
711 | * The dpif owns the data stored in 'port'. It will remain valid until at | |
712 | * least the next time 'dump' is passed to dpif_port_dump_next() or | |
713 | * dpif_port_dump_done(). */ | |
b0ec0f27 | 714 | bool |
4c738a8d | 715 | dpif_port_dump_next(struct dpif_port_dump *dump, struct dpif_port *port) |
064af421 | 716 | { |
b0ec0f27 | 717 | const struct dpif *dpif = dump->dpif; |
064af421 | 718 | |
b0ec0f27 BP |
719 | if (dump->error) { |
720 | return false; | |
721 | } | |
f4ba4c4f | 722 | |
b0ec0f27 BP |
723 | dump->error = dpif->dpif_class->port_dump_next(dpif, dump->state, port); |
724 | if (dump->error == EOF) { | |
725 | VLOG_DBG_RL(&dpmsg_rl, "%s: dumped all ports", dpif_name(dpif)); | |
726 | } else { | |
727 | log_operation(dpif, "port_dump_next", dump->error); | |
728 | } | |
064af421 | 729 | |
b0ec0f27 BP |
730 | if (dump->error) { |
731 | dpif->dpif_class->port_dump_done(dpif, dump->state); | |
732 | return false; | |
f4ba4c4f | 733 | } |
b0ec0f27 BP |
734 | return true; |
735 | } | |
064af421 | 736 | |
b0ec0f27 BP |
737 | /* Completes port table dump operation 'dump', which must have been initialized |
738 | * with dpif_port_dump_start(). Returns 0 if the dump operation was | |
739 | * error-free, otherwise a positive errno value describing the problem. */ | |
740 | int | |
741 | dpif_port_dump_done(struct dpif_port_dump *dump) | |
742 | { | |
743 | const struct dpif *dpif = dump->dpif; | |
744 | if (!dump->error) { | |
745 | dump->error = dpif->dpif_class->port_dump_done(dpif, dump->state); | |
746 | log_operation(dpif, "port_dump_done", dump->error); | |
f4ba4c4f | 747 | } |
b0ec0f27 | 748 | return dump->error == EOF ? 0 : dump->error; |
064af421 BP |
749 | } |
750 | ||
e9e28be3 BP |
751 | /* Polls for changes in the set of ports in 'dpif'. If the set of ports in |
752 | * 'dpif' has changed, this function does one of the following: | |
753 | * | |
754 | * - Stores the name of the device that was added to or deleted from 'dpif' in | |
755 | * '*devnamep' and returns 0. The caller is responsible for freeing | |
756 | * '*devnamep' (with free()) when it no longer needs it. | |
757 | * | |
758 | * - Returns ENOBUFS and sets '*devnamep' to NULL. | |
759 | * | |
760 | * This function may also return 'false positives', where it returns 0 and | |
761 | * '*devnamep' names a device that was not actually added or deleted or it | |
762 | * returns ENOBUFS without any change. | |
763 | * | |
764 | * Returns EAGAIN if the set of ports in 'dpif' has not changed. May also | |
765 | * return other positive errno values to indicate that something has gone | |
766 | * wrong. */ | |
767 | int | |
768 | dpif_port_poll(const struct dpif *dpif, char **devnamep) | |
769 | { | |
1acb6baa | 770 | int error = dpif->dpif_class->port_poll(dpif, devnamep); |
e9e28be3 BP |
771 | if (error) { |
772 | *devnamep = NULL; | |
773 | } | |
774 | return error; | |
775 | } | |
776 | ||
777 | /* Arranges for the poll loop to wake up when port_poll(dpif) will return a | |
778 | * value other than EAGAIN. */ | |
779 | void | |
780 | dpif_port_poll_wait(const struct dpif *dpif) | |
781 | { | |
1acb6baa | 782 | dpif->dpif_class->port_poll_wait(dpif); |
e9e28be3 BP |
783 | } |
784 | ||
572b7068 | 785 | /* Extracts the flow stats for a packet. The 'flow' and 'packet' |
a7752d4a BP |
786 | * arguments must have been initialized through a call to flow_extract(). |
787 | * 'used' is stored into stats->used. */ | |
572b7068 | 788 | void |
a39edbd4 | 789 | dpif_flow_stats_extract(const struct flow *flow, const struct ofpbuf *packet, |
a7752d4a | 790 | long long int used, struct dpif_flow_stats *stats) |
572b7068 | 791 | { |
e0eecb1c | 792 | stats->tcp_flags = ntohs(flow->tcp_flags); |
1f317cb5 | 793 | stats->n_bytes = ofpbuf_size(packet); |
572b7068 | 794 | stats->n_packets = 1; |
a7752d4a | 795 | stats->used = used; |
572b7068 BP |
796 | } |
797 | ||
c97fb132 BP |
798 | /* Appends a human-readable representation of 'stats' to 's'. */ |
799 | void | |
800 | dpif_flow_stats_format(const struct dpif_flow_stats *stats, struct ds *s) | |
801 | { | |
802 | ds_put_format(s, "packets:%"PRIu64", bytes:%"PRIu64", used:", | |
803 | stats->n_packets, stats->n_bytes); | |
804 | if (stats->used) { | |
805 | ds_put_format(s, "%.3fs", (time_msec() - stats->used) / 1000.0); | |
806 | } else { | |
807 | ds_put_format(s, "never"); | |
808 | } | |
7393104d BP |
809 | if (stats->tcp_flags) { |
810 | ds_put_cstr(s, ", flags:"); | |
811 | packet_format_tcp_flags(s, stats->tcp_flags); | |
812 | } | |
c97fb132 BP |
813 | } |
814 | ||
96fba48f BP |
815 | /* Deletes all flows from 'dpif'. Returns 0 if successful, otherwise a |
816 | * positive errno value. */ | |
817 | int | |
818 | dpif_flow_flush(struct dpif *dpif) | |
064af421 | 819 | { |
96fba48f BP |
820 | int error; |
821 | ||
822 | COVERAGE_INC(dpif_flow_flush); | |
823 | ||
1acb6baa | 824 | error = dpif->dpif_class->flow_flush(dpif); |
96fba48f BP |
825 | log_operation(dpif, "flow_flush", error); |
826 | return error; | |
064af421 BP |
827 | } |
828 | ||
feebdea2 | 829 | /* Queries 'dpif' for a flow entry. The flow is specified by the Netlink |
df2c07f4 | 830 | * attributes with types OVS_KEY_ATTR_* in the 'key_len' bytes starting at |
feebdea2 | 831 | * 'key'. |
96fba48f | 832 | * |
feebdea2 BP |
833 | * Returns 0 if successful. If no flow matches, returns ENOENT. On other |
834 | * failure, returns a positive errno value. | |
96fba48f | 835 | * |
feebdea2 BP |
836 | * If 'actionsp' is nonnull, then on success '*actionsp' will be set to an |
837 | * ofpbuf owned by the caller that contains the Netlink attributes for the | |
838 | * flow's actions. The caller must free the ofpbuf (with ofpbuf_delete()) when | |
839 | * it is no longer needed. | |
840 | * | |
841 | * If 'stats' is nonnull, then on success it will be updated with the flow's | |
842 | * statistics. */ | |
96fba48f | 843 | int |
693c4a01 | 844 | dpif_flow_get(const struct dpif *dpif, |
feebdea2 | 845 | const struct nlattr *key, size_t key_len, |
c97fb132 | 846 | struct ofpbuf **actionsp, struct dpif_flow_stats *stats) |
064af421 | 847 | { |
96fba48f BP |
848 | int error; |
849 | ||
850 | COVERAGE_INC(dpif_flow_get); | |
851 | ||
693c4a01 | 852 | error = dpif->dpif_class->flow_get(dpif, key, key_len, actionsp, stats); |
b843fa1b | 853 | if (error) { |
feebdea2 BP |
854 | if (actionsp) { |
855 | *actionsp = NULL; | |
856 | } | |
857 | if (stats) { | |
858 | memset(stats, 0, sizeof *stats); | |
859 | } | |
b843fa1b | 860 | } |
96fba48f | 861 | if (should_log_flow_message(error)) { |
feebdea2 BP |
862 | const struct nlattr *actions; |
863 | size_t actions_len; | |
864 | ||
865 | if (!error && actionsp) { | |
1f317cb5 PS |
866 | actions = ofpbuf_data(*actionsp); |
867 | actions_len = ofpbuf_size(*actionsp); | |
feebdea2 BP |
868 | } else { |
869 | actions = NULL; | |
870 | actions_len = 0; | |
871 | } | |
61fb711d JP |
872 | log_flow_message(dpif, error, "flow_get", key, key_len, |
873 | NULL, 0, stats, actions, actions_len); | |
064af421 | 874 | } |
96fba48f | 875 | return error; |
064af421 BP |
876 | } |
877 | ||
89625d1e BP |
878 | static int |
879 | dpif_flow_put__(struct dpif *dpif, const struct dpif_flow_put *put) | |
880 | { | |
881 | int error; | |
882 | ||
883 | COVERAGE_INC(dpif_flow_put); | |
cb22974d BP |
884 | ovs_assert(!(put->flags & ~(DPIF_FP_CREATE | DPIF_FP_MODIFY |
885 | | DPIF_FP_ZERO_STATS))); | |
89625d1e BP |
886 | |
887 | error = dpif->dpif_class->flow_put(dpif, put); | |
888 | if (error && put->stats) { | |
889 | memset(put->stats, 0, sizeof *put->stats); | |
890 | } | |
891 | log_flow_put_message(dpif, put, error); | |
892 | return error; | |
893 | } | |
894 | ||
feebdea2 | 895 | /* Adds or modifies a flow in 'dpif'. The flow is specified by the Netlink |
e6cc0bab | 896 | * attribute OVS_FLOW_ATTR_KEY with types OVS_KEY_ATTR_* in the 'key_len' bytes |
ee75c546 BP |
897 | * starting at 'key', and OVS_FLOW_ATTR_MASK with types of OVS_KEY_ATTR_* in |
898 | * the 'mask_len' bytes starting at 'mask'. The associated actions are | |
899 | * specified by the Netlink attributes with types OVS_ACTION_ATTR_* in the | |
900 | * 'actions_len' bytes starting at 'actions'. | |
96fba48f | 901 | * |
feebdea2 | 902 | * - If the flow's key does not exist in 'dpif', then the flow will be added if |
ba25b8f4 | 903 | * 'flags' includes DPIF_FP_CREATE. Otherwise the operation will fail with |
96fba48f BP |
904 | * ENOENT. |
905 | * | |
ee75c546 BP |
906 | * The datapath may reject attempts to insert overlapping flows with EINVAL |
907 | * or EEXIST, but clients should not rely on this: avoiding overlapping flows | |
908 | * is primarily the client's responsibility. | |
909 | * | |
feebdea2 | 910 | * If the operation succeeds, then 'stats', if nonnull, will be zeroed. |
96fba48f | 911 | * |
feebdea2 | 912 | * - If the flow's key does exist in 'dpif', then the flow's actions will be |
ba25b8f4 | 913 | * updated if 'flags' includes DPIF_FP_MODIFY. Otherwise the operation will |
feebdea2 | 914 | * fail with EEXIST. If the flow's actions are updated, then its statistics |
ba25b8f4 | 915 | * will be zeroed if 'flags' includes DPIF_FP_ZERO_STATS, and left as-is |
feebdea2 BP |
916 | * otherwise. |
917 | * | |
918 | * If the operation succeeds, then 'stats', if nonnull, will be set to the | |
919 | * flow's statistics before the update. | |
96fba48f | 920 | */ |
064af421 | 921 | int |
ba25b8f4 | 922 | dpif_flow_put(struct dpif *dpif, enum dpif_flow_put_flags flags, |
feebdea2 | 923 | const struct nlattr *key, size_t key_len, |
e6cc0bab | 924 | const struct nlattr *mask, size_t mask_len, |
feebdea2 | 925 | const struct nlattr *actions, size_t actions_len, |
c97fb132 | 926 | struct dpif_flow_stats *stats) |
064af421 | 927 | { |
89625d1e BP |
928 | struct dpif_flow_put put; |
929 | ||
930 | put.flags = flags; | |
931 | put.key = key; | |
932 | put.key_len = key_len; | |
e6cc0bab AZ |
933 | put.mask = mask; |
934 | put.mask_len = mask_len; | |
89625d1e BP |
935 | put.actions = actions; |
936 | put.actions_len = actions_len; | |
937 | put.stats = stats; | |
938 | return dpif_flow_put__(dpif, &put); | |
064af421 BP |
939 | } |
940 | ||
b99d3cee BP |
941 | static int |
942 | dpif_flow_del__(struct dpif *dpif, struct dpif_flow_del *del) | |
943 | { | |
944 | int error; | |
945 | ||
946 | COVERAGE_INC(dpif_flow_del); | |
947 | ||
948 | error = dpif->dpif_class->flow_del(dpif, del); | |
949 | if (error && del->stats) { | |
950 | memset(del->stats, 0, sizeof *del->stats); | |
951 | } | |
952 | log_flow_del_message(dpif, del, error); | |
953 | return error; | |
954 | } | |
955 | ||
feebdea2 BP |
956 | /* Deletes a flow from 'dpif' and returns 0, or returns ENOENT if 'dpif' does |
957 | * not contain such a flow. The flow is specified by the Netlink attributes | |
df2c07f4 | 958 | * with types OVS_KEY_ATTR_* in the 'key_len' bytes starting at 'key'. |
96fba48f | 959 | * |
feebdea2 BP |
960 | * If the operation succeeds, then 'stats', if nonnull, will be set to the |
961 | * flow's statistics before its deletion. */ | |
064af421 | 962 | int |
feebdea2 BP |
963 | dpif_flow_del(struct dpif *dpif, |
964 | const struct nlattr *key, size_t key_len, | |
c97fb132 | 965 | struct dpif_flow_stats *stats) |
064af421 | 966 | { |
b99d3cee | 967 | struct dpif_flow_del del; |
f1aa2072 | 968 | |
b99d3cee BP |
969 | del.key = key; |
970 | del.key_len = key_len; | |
971 | del.stats = stats; | |
972 | return dpif_flow_del__(dpif, &del); | |
064af421 BP |
973 | } |
974 | ||
ac64794a BP |
975 | /* Creates and returns a new 'struct dpif_flow_dump' for iterating through the |
976 | * flows in 'dpif'. | |
977 | * | |
978 | * This function always successfully returns a dpif_flow_dump. Error | |
979 | * reporting is deferred to dpif_flow_dump_destroy(). */ | |
980 | struct dpif_flow_dump * | |
981 | dpif_flow_dump_create(const struct dpif *dpif) | |
e723fd32 | 982 | { |
ac64794a | 983 | return dpif->dpif_class->flow_dump_create(dpif); |
e723fd32 JS |
984 | } |
985 | ||
ac64794a BP |
986 | /* Destroys 'dump', which must have been created with dpif_flow_dump_create(). |
987 | * All dpif_flow_dump_thread structures previously created for 'dump' must | |
988 | * previously have been destroyed. | |
989 | * | |
990 | * Returns 0 if the dump operation was error-free, otherwise a positive errno | |
991 | * value describing the problem. */ | |
992 | int | |
993 | dpif_flow_dump_destroy(struct dpif_flow_dump *dump) | |
e723fd32 | 994 | { |
ac64794a BP |
995 | const struct dpif *dpif = dump->dpif; |
996 | int error = dpif->dpif_class->flow_dump_destroy(dump); | |
997 | log_operation(dpif, "flow_dump_destroy", error); | |
998 | return error == EOF ? 0 : error; | |
e723fd32 JS |
999 | } |
1000 | ||
ac64794a BP |
1001 | /* Returns new thread-local state for use with dpif_flow_dump_next(). */ |
1002 | struct dpif_flow_dump_thread * | |
1003 | dpif_flow_dump_thread_create(struct dpif_flow_dump *dump) | |
064af421 | 1004 | { |
ac64794a | 1005 | return dump->dpif->dpif_class->flow_dump_thread_create(dump); |
064af421 BP |
1006 | } |
1007 | ||
ac64794a BP |
1008 | /* Releases 'thread'. */ |
1009 | void | |
1010 | dpif_flow_dump_thread_destroy(struct dpif_flow_dump_thread *thread) | |
704a1e09 | 1011 | { |
ac64794a | 1012 | thread->dpif->dpif_class->flow_dump_thread_destroy(thread); |
704a1e09 BP |
1013 | } |
1014 | ||
ac64794a BP |
1015 | /* Attempts to retrieve up to 'max_flows' more flows from 'thread'. Returns 0 |
1016 | * if and only if no flows remained to be retrieved, otherwise a positive | |
1017 | * number reflecting the number of elements in 'flows[]' that were updated. | |
1018 | * The number of flows returned might be less than 'max_flows' because | |
1019 | * fewer than 'max_flows' remained, because this particular datapath does not | |
1020 | * benefit from batching, or because an error occurred partway through | |
1021 | * retrieval. Thus, the caller should continue calling until a 0 return value, | |
1022 | * even if intermediate return values are less than 'max_flows'. | |
bdeadfdd | 1023 | * |
ac64794a BP |
1024 | * No error status is immediately provided. An error status for the entire |
1025 | * dump operation is provided when it is completed by calling | |
1026 | * dpif_flow_dump_destroy(). | |
bdeadfdd | 1027 | * |
ac64794a BP |
1028 | * All of the data stored into 'flows' is owned by the datapath, not by the |
1029 | * caller, and the caller must not modify or free it. The datapath guarantees | |
1030 | * that it remains accessible and unchanged until at least the next call to | |
1031 | * dpif_flow_dump_next() for 'thread'. */ | |
704a1e09 | 1032 | int |
ac64794a BP |
1033 | dpif_flow_dump_next(struct dpif_flow_dump_thread *thread, |
1034 | struct dpif_flow *flows, int max_flows) | |
704a1e09 | 1035 | { |
ac64794a BP |
1036 | struct dpif *dpif = thread->dpif; |
1037 | int n; | |
1038 | ||
1039 | ovs_assert(max_flows > 0); | |
1040 | n = dpif->dpif_class->flow_dump_next(thread, flows, max_flows); | |
1041 | if (n > 0) { | |
1042 | struct dpif_flow *f; | |
1043 | ||
1044 | for (f = flows; f < &flows[n] && should_log_flow_message(0); f++) { | |
1045 | log_flow_message(dpif, 0, "flow_dump", | |
1046 | f->key, f->key_len, f->mask, f->mask_len, | |
1047 | &f->stats, f->actions, f->actions_len); | |
1048 | } | |
1049 | } else { | |
1050 | VLOG_DBG_RL(&dpmsg_rl, "%s: dumped all flows", dpif_name(dpif)); | |
1051 | } | |
1052 | return n; | |
064af421 BP |
1053 | } |
1054 | ||
7fd91025 BP |
1055 | struct dpif_execute_helper_aux { |
1056 | struct dpif *dpif; | |
1057 | int error; | |
1058 | }; | |
1059 | ||
09f9da0b JR |
1060 | /* This is called for actions that need the context of the datapath to be |
1061 | * meaningful. */ | |
7fd91025 | 1062 | static void |
758c456d | 1063 | dpif_execute_helper_cb(void *aux_, struct ofpbuf *packet, |
572f732a | 1064 | struct pkt_metadata *md, |
09f9da0b | 1065 | const struct nlattr *action, bool may_steal OVS_UNUSED) |
7fd91025 | 1066 | { |
758c456d JR |
1067 | struct dpif_execute_helper_aux *aux = aux_; |
1068 | struct dpif_execute execute; | |
09f9da0b | 1069 | int type = nl_attr_type(action); |
758c456d | 1070 | |
09f9da0b JR |
1071 | switch ((enum ovs_action_attr)type) { |
1072 | case OVS_ACTION_ATTR_OUTPUT: | |
1073 | case OVS_ACTION_ATTR_USERSPACE: | |
c6bf49f3 | 1074 | case OVS_ACTION_ATTR_RECIRC: |
758c456d JR |
1075 | execute.actions = action; |
1076 | execute.actions_len = NLA_ALIGN(action->nla_len); | |
1077 | execute.packet = packet; | |
1078 | execute.md = *md; | |
1079 | execute.needs_help = false; | |
1080 | aux->error = aux->dpif->dpif_class->execute(aux->dpif, &execute); | |
09f9da0b | 1081 | break; |
758c456d | 1082 | |
c6bf49f3 | 1083 | case OVS_ACTION_ATTR_HASH: |
09f9da0b JR |
1084 | case OVS_ACTION_ATTR_PUSH_VLAN: |
1085 | case OVS_ACTION_ATTR_POP_VLAN: | |
1086 | case OVS_ACTION_ATTR_PUSH_MPLS: | |
1087 | case OVS_ACTION_ATTR_POP_MPLS: | |
1088 | case OVS_ACTION_ATTR_SET: | |
1089 | case OVS_ACTION_ATTR_SAMPLE: | |
1090 | case OVS_ACTION_ATTR_UNSPEC: | |
1091 | case __OVS_ACTION_ATTR_MAX: | |
1092 | OVS_NOT_REACHED(); | |
1093 | } | |
7fd91025 BP |
1094 | } |
1095 | ||
1096 | /* Executes 'execute' by performing most of the actions in userspace and | |
1097 | * passing the fully constructed packets to 'dpif' for output and userspace | |
1098 | * actions. | |
1099 | * | |
1100 | * This helps with actions that a given 'dpif' doesn't implement directly. */ | |
1101 | static int | |
758c456d | 1102 | dpif_execute_with_help(struct dpif *dpif, struct dpif_execute *execute) |
7fd91025 | 1103 | { |
758c456d | 1104 | struct dpif_execute_helper_aux aux = {dpif, 0}; |
7fd91025 BP |
1105 | |
1106 | COVERAGE_INC(dpif_execute_with_help); | |
1107 | ||
df1e5a3b | 1108 | odp_execute_actions(&aux, execute->packet, false, &execute->md, |
7fd91025 | 1109 | execute->actions, execute->actions_len, |
09f9da0b | 1110 | dpif_execute_helper_cb); |
7fd91025 BP |
1111 | return aux.error; |
1112 | } | |
1113 | ||
758c456d JR |
1114 | /* Causes 'dpif' to perform the 'execute->actions_len' bytes of actions in |
1115 | * 'execute->actions' on the Ethernet frame in 'execute->packet' and on packet | |
1116 | * metadata in 'execute->md'. The implementation is allowed to modify both the | |
1117 | * '*execute->packet' and 'execute->md'. | |
1118 | * | |
1119 | * Some dpif providers do not implement every action. The Linux kernel | |
1120 | * datapath, in particular, does not implement ARP field modification. If | |
1121 | * 'needs_help' is true, the dpif layer executes in userspace all of the | |
1122 | * actions that it can, and for OVS_ACTION_ATTR_OUTPUT and | |
1123 | * OVS_ACTION_ATTR_USERSPACE actions it passes the packet through to the dpif | |
1124 | * implementation. | |
1125 | * | |
1126 | * This works even if 'execute->actions_len' is too long for a Netlink | |
1127 | * attribute. | |
1128 | * | |
1129 | * Returns 0 if successful, otherwise a positive errno value. */ | |
1130 | int | |
1131 | dpif_execute(struct dpif *dpif, struct dpif_execute *execute) | |
89625d1e BP |
1132 | { |
1133 | int error; | |
1134 | ||
1135 | COVERAGE_INC(dpif_execute); | |
1136 | if (execute->actions_len > 0) { | |
758c456d | 1137 | error = (execute->needs_help || nl_attr_oversized(execute->actions_len) |
7fd91025 BP |
1138 | ? dpif_execute_with_help(dpif, execute) |
1139 | : dpif->dpif_class->execute(dpif, execute)); | |
89625d1e BP |
1140 | } else { |
1141 | error = 0; | |
1142 | } | |
1143 | ||
1144 | log_execute_message(dpif, execute, error); | |
1145 | ||
1146 | return error; | |
1147 | } | |
1148 | ||
6bc60024 BP |
1149 | /* Executes each of the 'n_ops' operations in 'ops' on 'dpif', in the order in |
1150 | * which they are specified, placing each operation's results in the "output" | |
1151 | * members documented in comments. | |
1152 | * | |
1153 | * This function exists because some datapaths can perform batched operations | |
1154 | * faster than individual operations. */ | |
1155 | void | |
c2b565b5 | 1156 | dpif_operate(struct dpif *dpif, struct dpif_op **ops, size_t n_ops) |
6bc60024 | 1157 | { |
6bc60024 | 1158 | if (dpif->dpif_class->operate) { |
7fd91025 BP |
1159 | while (n_ops > 0) { |
1160 | size_t chunk; | |
1161 | ||
1162 | /* Count 'chunk', the number of ops that can be executed without | |
1163 | * needing any help. Ops that need help should be rare, so we | |
1164 | * expect this to ordinarily be 'n_ops', that is, all the ops. */ | |
1165 | for (chunk = 0; chunk < n_ops; chunk++) { | |
1166 | struct dpif_op *op = ops[chunk]; | |
1167 | ||
1168 | if (op->type == DPIF_OP_EXECUTE && op->u.execute.needs_help) { | |
1169 | break; | |
1170 | } | |
1171 | } | |
1172 | ||
1173 | if (chunk) { | |
1174 | /* Execute a chunk full of ops that the dpif provider can | |
1175 | * handle itself, without help. */ | |
1176 | size_t i; | |
1177 | ||
1178 | dpif->dpif_class->operate(dpif, ops, chunk); | |
1179 | ||
1180 | for (i = 0; i < chunk; i++) { | |
1181 | struct dpif_op *op = ops[i]; | |
1182 | ||
1183 | switch (op->type) { | |
1184 | case DPIF_OP_FLOW_PUT: | |
1185 | log_flow_put_message(dpif, &op->u.flow_put, op->error); | |
1186 | break; | |
1187 | ||
1188 | case DPIF_OP_FLOW_DEL: | |
1189 | log_flow_del_message(dpif, &op->u.flow_del, op->error); | |
1190 | break; | |
1191 | ||
1192 | case DPIF_OP_EXECUTE: | |
1193 | log_execute_message(dpif, &op->u.execute, op->error); | |
1194 | break; | |
1195 | } | |
1196 | } | |
1197 | ||
1198 | ops += chunk; | |
1199 | n_ops -= chunk; | |
1200 | } else { | |
1201 | /* Help the dpif provider to execute one op. */ | |
1202 | struct dpif_op *op = ops[0]; | |
1203 | ||
758c456d | 1204 | op->error = dpif_execute(dpif, &op->u.execute); |
7fd91025 BP |
1205 | ops++; |
1206 | n_ops--; | |
1207 | } | |
1208 | } | |
1209 | } else { | |
1210 | size_t i; | |
f23d2845 BP |
1211 | |
1212 | for (i = 0; i < n_ops; i++) { | |
1213 | struct dpif_op *op = ops[i]; | |
1214 | ||
1215 | switch (op->type) { | |
1216 | case DPIF_OP_FLOW_PUT: | |
7fd91025 | 1217 | op->error = dpif_flow_put__(dpif, &op->u.flow_put); |
f23d2845 BP |
1218 | break; |
1219 | ||
b99d3cee | 1220 | case DPIF_OP_FLOW_DEL: |
7fd91025 | 1221 | op->error = dpif_flow_del__(dpif, &op->u.flow_del); |
b99d3cee BP |
1222 | break; |
1223 | ||
f23d2845 | 1224 | case DPIF_OP_EXECUTE: |
758c456d | 1225 | op->error = dpif_execute(dpif, &op->u.execute); |
f23d2845 | 1226 | break; |
b99d3cee | 1227 | |
7fd91025 | 1228 | default: |
428b2edd | 1229 | OVS_NOT_REACHED(); |
7fd91025 | 1230 | } |
6bc60024 BP |
1231 | } |
1232 | } | |
1233 | } | |
1234 | ||
01545c1a BP |
1235 | /* Returns a string that represents 'type', for use in log messages. */ |
1236 | const char * | |
1237 | dpif_upcall_type_to_string(enum dpif_upcall_type type) | |
1238 | { | |
1239 | switch (type) { | |
1240 | case DPIF_UC_MISS: return "miss"; | |
1241 | case DPIF_UC_ACTION: return "action"; | |
01545c1a BP |
1242 | case DPIF_N_UC_TYPES: default: return "<unknown>"; |
1243 | } | |
1244 | } | |
1245 | ||
a12b3ead BP |
1246 | /* Enables or disables receiving packets with dpif_recv() on 'dpif'. Returns 0 |
1247 | * if successful, otherwise a positive errno value. | |
98403001 | 1248 | * |
a12b3ead | 1249 | * Turning packet receive off and then back on may change the Netlink PID |
98403001 BP |
1250 | * assignments returned by dpif_port_get_pid(). If the client does this, it |
1251 | * must update all of the flows that have OVS_ACTION_ATTR_USERSPACE actions | |
1252 | * using the new PID assignment. */ | |
8f24562a | 1253 | int |
a12b3ead | 1254 | dpif_recv_set(struct dpif *dpif, bool enable) |
8f24562a | 1255 | { |
a12b3ead BP |
1256 | int error = dpif->dpif_class->recv_set(dpif, enable); |
1257 | log_operation(dpif, "recv_set", error); | |
96fba48f | 1258 | return error; |
8f24562a BP |
1259 | } |
1260 | ||
1954e6bb AW |
1261 | /* Refreshes the poll loops and Netlink sockets associated to each port, |
1262 | * when the number of upcall handlers (upcall receiving thread) is changed | |
1263 | * to 'n_handlers' and receiving packets for 'dpif' is enabled by | |
1264 | * recv_set(). | |
1265 | * | |
1266 | * Since multiple upcall handlers can read upcalls simultaneously from | |
1267 | * 'dpif', each port can have multiple Netlink sockets, one per upcall | |
1268 | * handler. So, handlers_set() is responsible for the following tasks: | |
1269 | * | |
1270 | * When receiving upcall is enabled, extends or creates the | |
1271 | * configuration to support: | |
1272 | * | |
1273 | * - 'n_handlers' Netlink sockets for each port. | |
1274 | * | |
1275 | * - 'n_handlers' poll loops, one for each upcall handler. | |
1276 | * | |
1277 | * - registering the Netlink sockets for the same upcall handler to | |
1278 | * the corresponding poll loop. | |
1279 | * | |
1280 | * Returns 0 if successful, otherwise a positive errno value. */ | |
1281 | int | |
1282 | dpif_handlers_set(struct dpif *dpif, uint32_t n_handlers) | |
1283 | { | |
1284 | int error = dpif->dpif_class->handlers_set(dpif, n_handlers); | |
1285 | log_operation(dpif, "handlers_set", error); | |
1286 | return error; | |
1287 | } | |
1288 | ||
1289 | /* Polls for an upcall from 'dpif' for an upcall handler. Since there | |
1290 | * there can be multiple poll loops, 'handler_id' is needed as index to | |
1291 | * identify the corresponding poll loop. If successful, stores the upcall | |
1292 | * into '*upcall', using 'buf' for storage. Should only be called if | |
1293 | * 'recv_set' has been used to enable receiving packets from 'dpif'. | |
96fba48f | 1294 | * |
da546e07 JR |
1295 | * 'upcall->key' and 'upcall->userdata' point into data in the caller-provided |
1296 | * 'buf', so their memory cannot be freed separately from 'buf'. | |
856081f6 | 1297 | * |
837a88dc JR |
1298 | * The caller owns the data of 'upcall->packet' and may modify it. If |
1299 | * packet's headroom is exhausted as it is manipulated, 'upcall->packet' | |
1300 | * will be reallocated. This requires the data of 'upcall->packet' to be | |
1301 | * released with ofpbuf_uninit() before 'upcall' is destroyed. However, | |
1302 | * when an error is returned, the 'upcall->packet' may be uninitialized | |
1303 | * and should not be released. | |
1304 | * | |
96fba48f | 1305 | * Returns 0 if successful, otherwise a positive errno value. Returns EAGAIN |
856081f6 | 1306 | * if no upcall is immediately available. */ |
064af421 | 1307 | int |
1954e6bb AW |
1308 | dpif_recv(struct dpif *dpif, uint32_t handler_id, struct dpif_upcall *upcall, |
1309 | struct ofpbuf *buf) | |
064af421 | 1310 | { |
1954e6bb | 1311 | int error = dpif->dpif_class->recv(dpif, handler_id, upcall, buf); |
856081f6 | 1312 | if (!error && !VLOG_DROP_DBG(&dpmsg_rl)) { |
01545c1a BP |
1313 | struct ds flow; |
1314 | char *packet; | |
1315 | ||
1f317cb5 PS |
1316 | packet = ofp_packet_to_string(ofpbuf_data(&upcall->packet), |
1317 | ofpbuf_size(&upcall->packet)); | |
01545c1a BP |
1318 | |
1319 | ds_init(&flow); | |
1320 | odp_flow_key_format(upcall->key, upcall->key_len, &flow); | |
1321 | ||
1322 | VLOG_DBG("%s: %s upcall:\n%s\n%s", | |
1323 | dpif_name(dpif), dpif_upcall_type_to_string(upcall->type), | |
1324 | ds_cstr(&flow), packet); | |
1325 | ||
1326 | ds_destroy(&flow); | |
1327 | free(packet); | |
5fcc0d00 BP |
1328 | } else if (error && error != EAGAIN) { |
1329 | log_operation(dpif, "recv", error); | |
064af421 | 1330 | } |
064af421 BP |
1331 | return error; |
1332 | } | |
1333 | ||
96fba48f | 1334 | /* Discards all messages that would otherwise be received by dpif_recv() on |
1ba530f4 BP |
1335 | * 'dpif'. */ |
1336 | void | |
96fba48f BP |
1337 | dpif_recv_purge(struct dpif *dpif) |
1338 | { | |
96fba48f | 1339 | COVERAGE_INC(dpif_purge); |
1ba530f4 BP |
1340 | if (dpif->dpif_class->recv_purge) { |
1341 | dpif->dpif_class->recv_purge(dpif); | |
96fba48f | 1342 | } |
96fba48f BP |
1343 | } |
1344 | ||
1954e6bb AW |
1345 | /* Arranges for the poll loop for an upcall handler to wake up when 'dpif' |
1346 | * 'dpif' has a message queued to be received with the recv member | |
1347 | * function. Since there can be multiple poll loops, 'handler_id' is | |
1348 | * needed as index to identify the corresponding poll loop. */ | |
064af421 | 1349 | void |
1954e6bb | 1350 | dpif_recv_wait(struct dpif *dpif, uint32_t handler_id) |
064af421 | 1351 | { |
1954e6bb | 1352 | dpif->dpif_class->recv_wait(dpif, handler_id); |
064af421 | 1353 | } |
53a4218d | 1354 | |
96fba48f BP |
1355 | /* Obtains the NetFlow engine type and engine ID for 'dpif' into '*engine_type' |
1356 | * and '*engine_id', respectively. */ | |
53a4218d BP |
1357 | void |
1358 | dpif_get_netflow_ids(const struct dpif *dpif, | |
1359 | uint8_t *engine_type, uint8_t *engine_id) | |
1360 | { | |
96fba48f BP |
1361 | *engine_type = dpif->netflow_engine_type; |
1362 | *engine_id = dpif->netflow_engine_id; | |
1363 | } | |
aae51f53 BP |
1364 | |
1365 | /* Translates OpenFlow queue ID 'queue_id' (in host byte order) into a priority | |
abff858b PS |
1366 | * value used for setting packet priority. |
1367 | * On success, returns 0 and stores the priority into '*priority'. | |
1368 | * On failure, returns a positive errno value and stores 0 into '*priority'. */ | |
aae51f53 BP |
1369 | int |
1370 | dpif_queue_to_priority(const struct dpif *dpif, uint32_t queue_id, | |
1371 | uint32_t *priority) | |
1372 | { | |
1373 | int error = (dpif->dpif_class->queue_to_priority | |
1374 | ? dpif->dpif_class->queue_to_priority(dpif, queue_id, | |
1375 | priority) | |
1376 | : EOPNOTSUPP); | |
1377 | if (error) { | |
1378 | *priority = 0; | |
1379 | } | |
1380 | log_operation(dpif, "queue_to_priority", error); | |
1381 | return error; | |
1382 | } | |
96fba48f BP |
1383 | \f |
1384 | void | |
1acb6baa BP |
1385 | dpif_init(struct dpif *dpif, const struct dpif_class *dpif_class, |
1386 | const char *name, | |
96fba48f BP |
1387 | uint8_t netflow_engine_type, uint8_t netflow_engine_id) |
1388 | { | |
1acb6baa | 1389 | dpif->dpif_class = dpif_class; |
1a6f1e2a | 1390 | dpif->base_name = xstrdup(name); |
a4af0040 | 1391 | dpif->full_name = xasprintf("%s@%s", dpif_class->type, name); |
96fba48f BP |
1392 | dpif->netflow_engine_type = netflow_engine_type; |
1393 | dpif->netflow_engine_id = netflow_engine_id; | |
1394 | } | |
999401aa JG |
1395 | |
1396 | /* Undoes the results of initialization. | |
1397 | * | |
1398 | * Normally this function only needs to be called from dpif_close(). | |
1399 | * However, it may be called by providers due to an error on opening | |
1400 | * that occurs after initialization. It this case dpif_close() would | |
1401 | * never be called. */ | |
1402 | void | |
1403 | dpif_uninit(struct dpif *dpif, bool close) | |
1404 | { | |
1405 | char *base_name = dpif->base_name; | |
1406 | char *full_name = dpif->full_name; | |
1407 | ||
1408 | if (close) { | |
a4af0040 | 1409 | dpif->dpif_class->close(dpif); |
999401aa JG |
1410 | } |
1411 | ||
1412 | free(base_name); | |
1413 | free(full_name); | |
1414 | } | |
96fba48f BP |
1415 | \f |
1416 | static void | |
1417 | log_operation(const struct dpif *dpif, const char *operation, int error) | |
1418 | { | |
1419 | if (!error) { | |
1420 | VLOG_DBG_RL(&dpmsg_rl, "%s: %s success", dpif_name(dpif), operation); | |
90bf1e07 | 1421 | } else if (ofperr_is_valid(error)) { |
96fba48f | 1422 | VLOG_WARN_RL(&error_rl, "%s: %s failed (%s)", |
90bf1e07 | 1423 | dpif_name(dpif), operation, ofperr_get_name(error)); |
71ce9235 | 1424 | } else { |
90bf1e07 | 1425 | VLOG_WARN_RL(&error_rl, "%s: %s failed (%s)", |
10a89ef0 | 1426 | dpif_name(dpif), operation, ovs_strerror(error)); |
96fba48f BP |
1427 | } |
1428 | } | |
1429 | ||
1430 | static enum vlog_level | |
1431 | flow_message_log_level(int error) | |
1432 | { | |
9b1a48c2 JP |
1433 | /* If flows arrive in a batch, userspace may push down multiple |
1434 | * unique flow definitions that overlap when wildcards are applied. | |
1435 | * Kernels that support flow wildcarding will reject these flows as | |
1436 | * duplicates (EEXIST), so lower the log level to debug for these | |
1437 | * types of messages. */ | |
1438 | return (error && error != EEXIST) ? VLL_WARN : VLL_DBG; | |
96fba48f BP |
1439 | } |
1440 | ||
1441 | static bool | |
1442 | should_log_flow_message(int error) | |
1443 | { | |
1444 | return !vlog_should_drop(THIS_MODULE, flow_message_log_level(error), | |
1445 | error ? &error_rl : &dpmsg_rl); | |
1446 | } | |
1447 | ||
1448 | static void | |
1449 | log_flow_message(const struct dpif *dpif, int error, const char *operation, | |
36956a7d | 1450 | const struct nlattr *key, size_t key_len, |
61fb711d | 1451 | const struct nlattr *mask, size_t mask_len, |
c97fb132 | 1452 | const struct dpif_flow_stats *stats, |
cf22f8cb | 1453 | const struct nlattr *actions, size_t actions_len) |
96fba48f BP |
1454 | { |
1455 | struct ds ds = DS_EMPTY_INITIALIZER; | |
1456 | ds_put_format(&ds, "%s: ", dpif_name(dpif)); | |
1457 | if (error) { | |
1458 | ds_put_cstr(&ds, "failed to "); | |
1459 | } | |
1460 | ds_put_format(&ds, "%s ", operation); | |
1461 | if (error) { | |
10a89ef0 | 1462 | ds_put_format(&ds, "(%s) ", ovs_strerror(error)); |
96fba48f | 1463 | } |
0a37839c | 1464 | odp_flow_format(key, key_len, mask, mask_len, NULL, &ds, true); |
96fba48f BP |
1465 | if (stats) { |
1466 | ds_put_cstr(&ds, ", "); | |
c97fb132 | 1467 | dpif_flow_stats_format(stats, &ds); |
96fba48f | 1468 | } |
cdee00fd | 1469 | if (actions || actions_len) { |
96fba48f | 1470 | ds_put_cstr(&ds, ", actions:"); |
cdee00fd | 1471 | format_odp_actions(&ds, actions, actions_len); |
96fba48f BP |
1472 | } |
1473 | vlog(THIS_MODULE, flow_message_log_level(error), "%s", ds_cstr(&ds)); | |
1474 | ds_destroy(&ds); | |
1475 | } | |
89625d1e BP |
1476 | |
1477 | static void | |
1478 | log_flow_put_message(struct dpif *dpif, const struct dpif_flow_put *put, | |
1479 | int error) | |
1480 | { | |
1481 | if (should_log_flow_message(error)) { | |
1482 | struct ds s; | |
1483 | ||
1484 | ds_init(&s); | |
1485 | ds_put_cstr(&s, "put"); | |
1486 | if (put->flags & DPIF_FP_CREATE) { | |
1487 | ds_put_cstr(&s, "[create]"); | |
1488 | } | |
1489 | if (put->flags & DPIF_FP_MODIFY) { | |
1490 | ds_put_cstr(&s, "[modify]"); | |
1491 | } | |
1492 | if (put->flags & DPIF_FP_ZERO_STATS) { | |
1493 | ds_put_cstr(&s, "[zero]"); | |
1494 | } | |
1495 | log_flow_message(dpif, error, ds_cstr(&s), | |
61fb711d JP |
1496 | put->key, put->key_len, put->mask, put->mask_len, |
1497 | put->stats, put->actions, put->actions_len); | |
89625d1e BP |
1498 | ds_destroy(&s); |
1499 | } | |
1500 | } | |
1501 | ||
b99d3cee BP |
1502 | static void |
1503 | log_flow_del_message(struct dpif *dpif, const struct dpif_flow_del *del, | |
1504 | int error) | |
1505 | { | |
1506 | if (should_log_flow_message(error)) { | |
1507 | log_flow_message(dpif, error, "flow_del", del->key, del->key_len, | |
61fb711d | 1508 | NULL, 0, !error ? del->stats : NULL, NULL, 0); |
b99d3cee BP |
1509 | } |
1510 | } | |
1511 | ||
89625d1e BP |
1512 | static void |
1513 | log_execute_message(struct dpif *dpif, const struct dpif_execute *execute, | |
1514 | int error) | |
1515 | { | |
1516 | if (!(error ? VLOG_DROP_WARN(&error_rl) : VLOG_DROP_DBG(&dpmsg_rl))) { | |
1517 | struct ds ds = DS_EMPTY_INITIALIZER; | |
1518 | char *packet; | |
1519 | ||
1f317cb5 PS |
1520 | packet = ofp_packet_to_string(ofpbuf_data(execute->packet), |
1521 | ofpbuf_size(execute->packet)); | |
89625d1e BP |
1522 | ds_put_format(&ds, "%s: execute ", dpif_name(dpif)); |
1523 | format_odp_actions(&ds, execute->actions, execute->actions_len); | |
1524 | if (error) { | |
10a89ef0 | 1525 | ds_put_format(&ds, " failed (%s)", ovs_strerror(error)); |
89625d1e BP |
1526 | } |
1527 | ds_put_format(&ds, " on packet %s", packet); | |
1528 | vlog(THIS_MODULE, error ? VLL_WARN : VLL_DBG, "%s", ds_cstr(&ds)); | |
1529 | ds_destroy(&ds); | |
1530 | free(packet); | |
1531 | } | |
1532 | } |