]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
1a6f1e2a | 2 | * Copyright (c) 2008, 2009, 2010 Nicira Networks. |
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 BP |
19 | |
20 | #include <assert.h> | |
21 | #include <ctype.h> | |
22 | #include <errno.h> | |
064af421 | 23 | #include <inttypes.h> |
064af421 BP |
24 | #include <stdlib.h> |
25 | #include <string.h> | |
064af421 BP |
26 | |
27 | #include "coverage.h" | |
28 | #include "dynamic-string.h" | |
29 | #include "flow.h" | |
30 | #include "netlink.h" | |
31 | #include "odp-util.h" | |
32 | #include "ofp-print.h" | |
71ce9235 | 33 | #include "ofp-util.h" |
064af421 BP |
34 | #include "ofpbuf.h" |
35 | #include "packets.h" | |
36 | #include "poll-loop.h" | |
999401aa | 37 | #include "shash.h" |
d3d22744 | 38 | #include "svec.h" |
064af421 BP |
39 | #include "util.h" |
40 | #include "valgrind.h" | |
064af421 | 41 | #include "vlog.h" |
5136ce49 | 42 | |
d98e6007 | 43 | VLOG_DEFINE_THIS_MODULE(dpif); |
064af421 | 44 | |
d76f09ea BP |
45 | COVERAGE_DEFINE(dpif_destroy); |
46 | COVERAGE_DEFINE(dpif_port_add); | |
47 | COVERAGE_DEFINE(dpif_port_del); | |
48 | COVERAGE_DEFINE(dpif_flow_flush); | |
49 | COVERAGE_DEFINE(dpif_flow_get); | |
50 | COVERAGE_DEFINE(dpif_flow_put); | |
51 | COVERAGE_DEFINE(dpif_flow_del); | |
52 | COVERAGE_DEFINE(dpif_flow_query_list); | |
53 | COVERAGE_DEFINE(dpif_flow_query_list_n); | |
54 | COVERAGE_DEFINE(dpif_execute); | |
55 | COVERAGE_DEFINE(dpif_purge); | |
56 | ||
999401aa | 57 | static const struct dpif_class *base_dpif_classes[] = { |
c83cdd30 | 58 | #ifdef HAVE_NETLINK |
96fba48f | 59 | &dpif_linux_class, |
c83cdd30 | 60 | #endif |
72865317 | 61 | &dpif_netdev_class, |
c228a364 | 62 | }; |
999401aa JG |
63 | |
64 | struct registered_dpif_class { | |
d2d8fbeb | 65 | const struct dpif_class *dpif_class; |
999401aa JG |
66 | int refcount; |
67 | }; | |
68 | static struct shash dpif_classes = SHASH_INITIALIZER(&dpif_classes); | |
c228a364 | 69 | |
064af421 BP |
70 | /* Rate limit for individual messages going to or from the datapath, output at |
71 | * DBG level. This is very high because, if these are enabled, it is because | |
72 | * we really need to see them. */ | |
73 | static struct vlog_rate_limit dpmsg_rl = VLOG_RATE_LIMIT_INIT(600, 600); | |
74 | ||
75 | /* Not really much point in logging many dpif errors. */ | |
e2781405 | 76 | static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(60, 5); |
064af421 | 77 | |
96fba48f BP |
78 | static void log_operation(const struct dpif *, const char *operation, |
79 | int error); | |
80 | static void log_flow_operation(const struct dpif *, const char *operation, | |
81 | int error, struct odp_flow *flow); | |
82 | static void log_flow_put(struct dpif *, int error, | |
83 | const struct odp_flow_put *); | |
84 | static bool should_log_flow_message(int error); | |
064af421 BP |
85 | static void check_rw_odp_flow(struct odp_flow *); |
86 | ||
999401aa JG |
87 | static void |
88 | dp_initialize(void) | |
89 | { | |
90 | static int status = -1; | |
91 | ||
92 | if (status < 0) { | |
93 | int i; | |
94 | ||
95 | status = 0; | |
96 | for (i = 0; i < ARRAY_SIZE(base_dpif_classes); i++) { | |
97 | dp_register_provider(base_dpif_classes[i]); | |
98 | } | |
99 | } | |
100 | } | |
101 | ||
5792c5c6 BP |
102 | /* Performs periodic work needed by all the various kinds of dpifs. |
103 | * | |
8b61709d BP |
104 | * If your program opens any dpifs, it must call both this function and |
105 | * netdev_run() within its main poll loop. */ | |
5792c5c6 BP |
106 | void |
107 | dp_run(void) | |
108 | { | |
999401aa JG |
109 | struct shash_node *node; |
110 | SHASH_FOR_EACH(node, &dpif_classes) { | |
111 | const struct registered_dpif_class *registered_class = node->data; | |
d2d8fbeb BP |
112 | if (registered_class->dpif_class->run) { |
113 | registered_class->dpif_class->run(); | |
5792c5c6 BP |
114 | } |
115 | } | |
116 | } | |
117 | ||
118 | /* Arranges for poll_block() to wake up when dp_run() needs to be called. | |
119 | * | |
8b61709d BP |
120 | * If your program opens any dpifs, it must call both this function and |
121 | * netdev_wait() within its main poll loop. */ | |
5792c5c6 BP |
122 | void |
123 | dp_wait(void) | |
124 | { | |
999401aa JG |
125 | struct shash_node *node; |
126 | SHASH_FOR_EACH(node, &dpif_classes) { | |
127 | const struct registered_dpif_class *registered_class = node->data; | |
d2d8fbeb BP |
128 | if (registered_class->dpif_class->wait) { |
129 | registered_class->dpif_class->wait(); | |
5792c5c6 BP |
130 | } |
131 | } | |
132 | } | |
133 | ||
999401aa JG |
134 | /* Registers a new datapath provider. After successful registration, new |
135 | * datapaths of that type can be opened using dpif_open(). */ | |
136 | int | |
137 | dp_register_provider(const struct dpif_class *new_class) | |
138 | { | |
139 | struct registered_dpif_class *registered_class; | |
140 | ||
141 | if (shash_find(&dpif_classes, new_class->type)) { | |
142 | VLOG_WARN("attempted to register duplicate datapath provider: %s", | |
143 | new_class->type); | |
144 | return EEXIST; | |
145 | } | |
1a6f1e2a | 146 | |
999401aa | 147 | registered_class = xmalloc(sizeof *registered_class); |
d2d8fbeb | 148 | registered_class->dpif_class = new_class; |
999401aa JG |
149 | registered_class->refcount = 0; |
150 | ||
151 | shash_add(&dpif_classes, new_class->type, registered_class); | |
152 | ||
153 | return 0; | |
154 | } | |
155 | ||
156 | /* Unregisters a datapath provider. 'type' must have been previously | |
157 | * registered and not currently be in use by any dpifs. After unregistration | |
158 | * new datapaths of that type cannot be opened using dpif_open(). */ | |
159 | int | |
160 | dp_unregister_provider(const char *type) | |
161 | { | |
162 | struct shash_node *node; | |
163 | struct registered_dpif_class *registered_class; | |
164 | ||
165 | node = shash_find(&dpif_classes, type); | |
166 | if (!node) { | |
167 | VLOG_WARN("attempted to unregister a datapath provider that is not " | |
168 | "registered: %s", type); | |
169 | return EAFNOSUPPORT; | |
170 | } | |
171 | ||
172 | registered_class = node->data; | |
173 | if (registered_class->refcount) { | |
174 | VLOG_WARN("attempted to unregister in use datapath provider: %s", type); | |
175 | return EBUSY; | |
176 | } | |
177 | ||
178 | shash_delete(&dpif_classes, node); | |
179 | free(registered_class); | |
180 | ||
181 | return 0; | |
182 | } | |
183 | ||
184 | /* Clears 'types' and enumerates the types of all currently registered datapath | |
185 | * providers into it. The caller must first initialize the svec. */ | |
1a6f1e2a JG |
186 | void |
187 | dp_enumerate_types(struct svec *types) | |
188 | { | |
999401aa | 189 | struct shash_node *node; |
1a6f1e2a | 190 | |
999401aa | 191 | dp_initialize(); |
1a6f1e2a JG |
192 | svec_clear(types); |
193 | ||
999401aa JG |
194 | SHASH_FOR_EACH(node, &dpif_classes) { |
195 | const struct registered_dpif_class *registered_class = node->data; | |
d2d8fbeb | 196 | svec_add(types, registered_class->dpif_class->type); |
1a6f1e2a JG |
197 | } |
198 | } | |
199 | ||
200 | /* Clears 'names' and enumerates the names of all known created datapaths with | |
201 | * the given 'type'. The caller must first initialize the svec. Returns 0 if | |
202 | * successful, otherwise a positive errno value. | |
d3d22744 BP |
203 | * |
204 | * Some kinds of datapaths might not be practically enumerable. This is not | |
205 | * considered an error. */ | |
206 | int | |
1a6f1e2a | 207 | dp_enumerate_names(const char *type, struct svec *names) |
d3d22744 | 208 | { |
999401aa JG |
209 | const struct registered_dpif_class *registered_class; |
210 | const struct dpif_class *dpif_class; | |
211 | int error; | |
d3d22744 | 212 | |
999401aa | 213 | dp_initialize(); |
1a6f1e2a JG |
214 | svec_clear(names); |
215 | ||
999401aa JG |
216 | registered_class = shash_find_data(&dpif_classes, type); |
217 | if (!registered_class) { | |
218 | VLOG_WARN("could not enumerate unknown type: %s", type); | |
219 | return EAFNOSUPPORT; | |
220 | } | |
1a6f1e2a | 221 | |
d2d8fbeb | 222 | dpif_class = registered_class->dpif_class; |
999401aa | 223 | error = dpif_class->enumerate ? dpif_class->enumerate(names) : 0; |
1a6f1e2a | 224 | |
999401aa JG |
225 | if (error) { |
226 | VLOG_WARN("failed to enumerate %s datapaths: %s", dpif_class->type, | |
227 | strerror(error)); | |
d3d22744 | 228 | } |
1a6f1e2a | 229 | |
999401aa | 230 | return error; |
1a6f1e2a JG |
231 | } |
232 | ||
233 | /* Parses 'datapath name', which is of the form type@name into its | |
234 | * component pieces. 'name' and 'type' must be freed by the caller. */ | |
235 | void | |
236 | dp_parse_name(const char *datapath_name_, char **name, char **type) | |
237 | { | |
238 | char *datapath_name = xstrdup(datapath_name_); | |
239 | char *separator; | |
240 | ||
241 | separator = strchr(datapath_name, '@'); | |
242 | if (separator) { | |
243 | *separator = '\0'; | |
244 | *type = datapath_name; | |
245 | *name = xstrdup(separator + 1); | |
246 | } else { | |
247 | *name = datapath_name; | |
248 | *type = NULL; | |
249 | } | |
d3d22744 BP |
250 | } |
251 | ||
96fba48f | 252 | static int |
1a6f1e2a | 253 | do_open(const char *name, const char *type, bool create, struct dpif **dpifp) |
064af421 | 254 | { |
96fba48f | 255 | struct dpif *dpif = NULL; |
064af421 | 256 | int error; |
999401aa JG |
257 | struct registered_dpif_class *registered_class; |
258 | ||
259 | dp_initialize(); | |
064af421 | 260 | |
1a6f1e2a JG |
261 | if (!type || *type == '\0') { |
262 | type = "system"; | |
064af421 BP |
263 | } |
264 | ||
999401aa JG |
265 | registered_class = shash_find_data(&dpif_classes, type); |
266 | if (!registered_class) { | |
267 | VLOG_WARN("could not create datapath %s of unknown type %s", name, | |
268 | type); | |
269 | error = EAFNOSUPPORT; | |
270 | goto exit; | |
271 | } | |
272 | ||
4a387741 BP |
273 | error = registered_class->dpif_class->open(registered_class->dpif_class, |
274 | name, create, &dpif); | |
999401aa | 275 | if (!error) { |
4a387741 | 276 | assert(dpif->dpif_class == registered_class->dpif_class); |
999401aa | 277 | registered_class->refcount++; |
064af421 | 278 | } |
064af421 | 279 | |
96fba48f BP |
280 | exit: |
281 | *dpifp = error ? NULL : dpif; | |
282 | return error; | |
064af421 BP |
283 | } |
284 | ||
1a6f1e2a JG |
285 | /* Tries to open an existing datapath named 'name' and type 'type'. Will fail |
286 | * if no datapath with 'name' and 'type' exists. 'type' may be either NULL or | |
287 | * the empty string to specify the default system type. Returns 0 if | |
288 | * successful, otherwise a positive errno value. On success stores a pointer | |
289 | * to the datapath in '*dpifp', otherwise a null pointer. */ | |
96fba48f | 290 | int |
1a6f1e2a | 291 | dpif_open(const char *name, const char *type, struct dpif **dpifp) |
064af421 | 292 | { |
1a6f1e2a | 293 | return do_open(name, type, false, dpifp); |
064af421 BP |
294 | } |
295 | ||
1a6f1e2a JG |
296 | /* Tries to create and open a new datapath with the given 'name' and 'type'. |
297 | * 'type' may be either NULL or the empty string to specify the default system | |
298 | * type. Will fail if a datapath with 'name' and 'type' already exists. | |
299 | * Returns 0 if successful, otherwise a positive errno value. On success | |
300 | * stores a pointer to the datapath in '*dpifp', otherwise a null pointer. */ | |
064af421 | 301 | int |
1a6f1e2a | 302 | dpif_create(const char *name, const char *type, struct dpif **dpifp) |
064af421 | 303 | { |
1a6f1e2a | 304 | return do_open(name, type, true, dpifp); |
96fba48f | 305 | } |
064af421 | 306 | |
1a6f1e2a JG |
307 | /* Tries to open a datapath with the given 'name' and 'type', creating it if it |
308 | * does not exist. 'type' may be either NULL or the empty string to specify | |
309 | * the default system type. Returns 0 if successful, otherwise a positive | |
310 | * errno value. On success stores a pointer to the datapath in '*dpifp', | |
311 | * otherwise a null pointer. */ | |
efacbce6 | 312 | int |
1a6f1e2a | 313 | dpif_create_and_open(const char *name, const char *type, struct dpif **dpifp) |
efacbce6 BP |
314 | { |
315 | int error; | |
316 | ||
1a6f1e2a | 317 | error = dpif_create(name, type, dpifp); |
efacbce6 | 318 | if (error == EEXIST || error == EBUSY) { |
1a6f1e2a | 319 | error = dpif_open(name, type, dpifp); |
efacbce6 BP |
320 | if (error) { |
321 | VLOG_WARN("datapath %s already exists but cannot be opened: %s", | |
322 | name, strerror(error)); | |
323 | } | |
324 | } else if (error) { | |
325 | VLOG_WARN("failed to create datapath %s: %s", name, strerror(error)); | |
326 | } | |
327 | return error; | |
328 | } | |
329 | ||
96fba48f BP |
330 | /* Closes and frees the connection to 'dpif'. Does not destroy the datapath |
331 | * itself; call dpif_delete() first, instead, if that is desirable. */ | |
332 | void | |
333 | dpif_close(struct dpif *dpif) | |
334 | { | |
335 | if (dpif) { | |
999401aa JG |
336 | struct registered_dpif_class *registered_class; |
337 | ||
d295e8e9 | 338 | registered_class = shash_find_data(&dpif_classes, |
a4af0040 | 339 | dpif->dpif_class->type); |
999401aa JG |
340 | assert(registered_class); |
341 | assert(registered_class->refcount); | |
342 | ||
343 | registered_class->refcount--; | |
344 | dpif_uninit(dpif, true); | |
064af421 BP |
345 | } |
346 | } | |
347 | ||
1a6f1e2a JG |
348 | /* Returns the name of datapath 'dpif' prefixed with the type |
349 | * (for use in log messages). */ | |
b29ba128 BP |
350 | const char * |
351 | dpif_name(const struct dpif *dpif) | |
352 | { | |
1a6f1e2a JG |
353 | return dpif->full_name; |
354 | } | |
355 | ||
356 | /* Returns the name of datapath 'dpif' without the type | |
357 | * (for use in device names). */ | |
358 | const char * | |
359 | dpif_base_name(const struct dpif *dpif) | |
360 | { | |
361 | return dpif->base_name; | |
b29ba128 BP |
362 | } |
363 | ||
d3d22744 BP |
364 | /* Enumerates all names that may be used to open 'dpif' into 'all_names'. The |
365 | * Linux datapath, for example, supports opening a datapath both by number, | |
366 | * e.g. "dp0", and by the name of the datapath's local port. For some | |
367 | * datapaths, this might be an infinite set (e.g. in a file name, slashes may | |
368 | * be duplicated any number of times), in which case only the names most likely | |
369 | * to be used will be enumerated. | |
370 | * | |
371 | * The caller must already have initialized 'all_names'. Any existing names in | |
372 | * 'all_names' will not be disturbed. */ | |
373 | int | |
374 | dpif_get_all_names(const struct dpif *dpif, struct svec *all_names) | |
375 | { | |
1acb6baa BP |
376 | if (dpif->dpif_class->get_all_names) { |
377 | int error = dpif->dpif_class->get_all_names(dpif, all_names); | |
d3d22744 BP |
378 | if (error) { |
379 | VLOG_WARN_RL(&error_rl, | |
380 | "failed to retrieve names for datpath %s: %s", | |
381 | dpif_name(dpif), strerror(error)); | |
382 | } | |
383 | return error; | |
384 | } else { | |
1a6f1e2a | 385 | svec_add(all_names, dpif_base_name(dpif)); |
d3d22744 BP |
386 | return 0; |
387 | } | |
388 | } | |
389 | ||
d76f09ea | 390 | |
96fba48f BP |
391 | /* Destroys the datapath that 'dpif' is connected to, first removing all of its |
392 | * ports. After calling this function, it does not make sense to pass 'dpif' | |
393 | * to any functions other than dpif_name() or dpif_close(). */ | |
064af421 BP |
394 | int |
395 | dpif_delete(struct dpif *dpif) | |
396 | { | |
96fba48f BP |
397 | int error; |
398 | ||
064af421 | 399 | COVERAGE_INC(dpif_destroy); |
96fba48f | 400 | |
1acb6baa | 401 | error = dpif->dpif_class->destroy(dpif); |
96fba48f BP |
402 | log_operation(dpif, "delete", error); |
403 | return error; | |
064af421 BP |
404 | } |
405 | ||
96fba48f BP |
406 | /* Retrieves statistics for 'dpif' into 'stats'. Returns 0 if successful, |
407 | * otherwise a positive errno value. */ | |
064af421 BP |
408 | int |
409 | dpif_get_dp_stats(const struct dpif *dpif, struct odp_stats *stats) | |
410 | { | |
1acb6baa | 411 | int error = dpif->dpif_class->get_stats(dpif, stats); |
96fba48f BP |
412 | if (error) { |
413 | memset(stats, 0, sizeof *stats); | |
414 | } | |
415 | log_operation(dpif, "get_stats", error); | |
416 | return error; | |
064af421 BP |
417 | } |
418 | ||
96fba48f BP |
419 | /* Retrieves the current IP fragment handling policy for 'dpif' into |
420 | * '*drop_frags': true indicates that fragments are dropped, false indicates | |
421 | * that fragments are treated in the same way as other IP packets (except that | |
422 | * the L4 header cannot be read). Returns 0 if successful, otherwise a | |
423 | * positive errno value. */ | |
064af421 BP |
424 | int |
425 | dpif_get_drop_frags(const struct dpif *dpif, bool *drop_frags) | |
426 | { | |
1acb6baa | 427 | int error = dpif->dpif_class->get_drop_frags(dpif, drop_frags); |
96fba48f BP |
428 | if (error) { |
429 | *drop_frags = false; | |
430 | } | |
431 | log_operation(dpif, "get_drop_frags", error); | |
064af421 BP |
432 | return error; |
433 | } | |
434 | ||
96fba48f BP |
435 | /* Changes 'dpif''s treatment of IP fragments to 'drop_frags', whose meaning is |
436 | * the same as for the get_drop_frags member function. Returns 0 if | |
437 | * successful, otherwise a positive errno value. */ | |
064af421 BP |
438 | int |
439 | dpif_set_drop_frags(struct dpif *dpif, bool drop_frags) | |
440 | { | |
1acb6baa | 441 | int error = dpif->dpif_class->set_drop_frags(dpif, drop_frags); |
96fba48f BP |
442 | log_operation(dpif, "set_drop_frags", error); |
443 | return error; | |
064af421 BP |
444 | } |
445 | ||
96fba48f BP |
446 | /* Attempts to add 'devname' as a port on 'dpif', given the combination of |
447 | * ODP_PORT_* flags in 'flags'. If successful, returns 0 and sets '*port_nop' | |
448 | * to the new port's port number (if 'port_nop' is non-null). On failure, | |
449 | * returns a positive errno value and sets '*port_nop' to UINT16_MAX (if | |
450 | * 'port_nop' is non-null). */ | |
064af421 | 451 | int |
9ee3ae3e BP |
452 | dpif_port_add(struct dpif *dpif, const char *devname, uint16_t flags, |
453 | uint16_t *port_nop) | |
064af421 | 454 | { |
9ee3ae3e BP |
455 | uint16_t port_no; |
456 | int error; | |
064af421 BP |
457 | |
458 | COVERAGE_INC(dpif_port_add); | |
9ee3ae3e | 459 | |
1acb6baa | 460 | error = dpif->dpif_class->port_add(dpif, devname, flags, &port_no); |
9ee3ae3e | 461 | if (!error) { |
b29ba128 BP |
462 | VLOG_DBG_RL(&dpmsg_rl, "%s: added %s as port %"PRIu16, |
463 | dpif_name(dpif), devname, port_no); | |
064af421 | 464 | } else { |
9ee3ae3e | 465 | VLOG_WARN_RL(&error_rl, "%s: failed to add %s as port: %s", |
96fba48f BP |
466 | dpif_name(dpif), devname, strerror(error)); |
467 | port_no = UINT16_MAX; | |
9ee3ae3e BP |
468 | } |
469 | if (port_nop) { | |
470 | *port_nop = port_no; | |
064af421 | 471 | } |
9ee3ae3e | 472 | return error; |
064af421 BP |
473 | } |
474 | ||
96fba48f BP |
475 | /* Attempts to remove 'dpif''s port number 'port_no'. Returns 0 if successful, |
476 | * otherwise a positive errno value. */ | |
064af421 BP |
477 | int |
478 | dpif_port_del(struct dpif *dpif, uint16_t port_no) | |
479 | { | |
96fba48f BP |
480 | int error; |
481 | ||
064af421 | 482 | COVERAGE_INC(dpif_port_del); |
96fba48f | 483 | |
1acb6baa | 484 | error = dpif->dpif_class->port_del(dpif, port_no); |
96fba48f BP |
485 | log_operation(dpif, "port_del", error); |
486 | return error; | |
064af421 BP |
487 | } |
488 | ||
96fba48f BP |
489 | /* Looks up port number 'port_no' in 'dpif'. On success, returns 0 and |
490 | * initializes '*port' appropriately; on failure, returns a positive errno | |
491 | * value. */ | |
064af421 BP |
492 | int |
493 | dpif_port_query_by_number(const struct dpif *dpif, uint16_t port_no, | |
494 | struct odp_port *port) | |
495 | { | |
1acb6baa | 496 | int error = dpif->dpif_class->port_query_by_number(dpif, port_no, port); |
96fba48f | 497 | if (!error) { |
b29ba128 BP |
498 | VLOG_DBG_RL(&dpmsg_rl, "%s: port %"PRIu16" is device %s", |
499 | dpif_name(dpif), port_no, port->devname); | |
064af421 | 500 | } else { |
96fba48f | 501 | memset(port, 0, sizeof *port); |
b29ba128 | 502 | VLOG_WARN_RL(&error_rl, "%s: failed to query port %"PRIu16": %s", |
96fba48f | 503 | dpif_name(dpif), port_no, strerror(error)); |
064af421 | 504 | } |
96fba48f | 505 | return error; |
064af421 BP |
506 | } |
507 | ||
96fba48f BP |
508 | /* Looks up port named 'devname' in 'dpif'. On success, returns 0 and |
509 | * initializes '*port' appropriately; on failure, returns a positive errno | |
510 | * value. */ | |
064af421 BP |
511 | int |
512 | dpif_port_query_by_name(const struct dpif *dpif, const char *devname, | |
513 | struct odp_port *port) | |
514 | { | |
1acb6baa | 515 | int error = dpif->dpif_class->port_query_by_name(dpif, devname, port); |
96fba48f | 516 | if (!error) { |
b29ba128 BP |
517 | VLOG_DBG_RL(&dpmsg_rl, "%s: device %s is on port %"PRIu16, |
518 | dpif_name(dpif), devname, port->port); | |
064af421 | 519 | } else { |
96fba48f BP |
520 | memset(port, 0, sizeof *port); |
521 | ||
5c6d2a3f BP |
522 | /* Log level is DBG here because all the current callers are interested |
523 | * in whether 'dpif' actually has a port 'devname', so that it's not an | |
524 | * issue worth logging if it doesn't. */ | |
525 | VLOG_DBG_RL(&error_rl, "%s: failed to query port %s: %s", | |
96fba48f | 526 | dpif_name(dpif), devname, strerror(error)); |
064af421 | 527 | } |
96fba48f | 528 | return error; |
064af421 BP |
529 | } |
530 | ||
96fba48f BP |
531 | /* Looks up port number 'port_no' in 'dpif'. On success, returns 0 and copies |
532 | * the port's name into the 'name_size' bytes in 'name', ensuring that the | |
533 | * result is null-terminated. On failure, returns a positive errno value and | |
534 | * makes 'name' the empty string. */ | |
335562c0 BP |
535 | int |
536 | dpif_port_get_name(struct dpif *dpif, uint16_t port_no, | |
537 | char *name, size_t name_size) | |
538 | { | |
539 | struct odp_port port; | |
540 | int error; | |
541 | ||
542 | assert(name_size > 0); | |
543 | ||
544 | error = dpif_port_query_by_number(dpif, port_no, &port); | |
545 | if (!error) { | |
546 | ovs_strlcpy(name, port.devname, name_size); | |
547 | } else { | |
548 | *name = '\0'; | |
549 | } | |
550 | return error; | |
551 | } | |
552 | ||
96fba48f BP |
553 | /* Obtains a list of all the ports in 'dpif'. |
554 | * | |
555 | * If successful, returns 0 and sets '*portsp' to point to an array of | |
556 | * appropriately initialized port structures and '*n_portsp' to the number of | |
557 | * ports in the array. The caller is responsible for freeing '*portp' by | |
558 | * calling free(). | |
559 | * | |
560 | * On failure, returns a positive errno value and sets '*portsp' to NULL and | |
561 | * '*n_portsp' to 0. */ | |
064af421 BP |
562 | int |
563 | dpif_port_list(const struct dpif *dpif, | |
f4ba4c4f | 564 | struct odp_port **portsp, size_t *n_portsp) |
064af421 | 565 | { |
f4ba4c4f | 566 | struct odp_port *ports; |
b9b0ce61 | 567 | size_t n_ports = 0; |
064af421 BP |
568 | int error; |
569 | ||
f4ba4c4f BP |
570 | for (;;) { |
571 | struct odp_stats stats; | |
96fba48f | 572 | int retval; |
f4ba4c4f | 573 | |
064af421 BP |
574 | error = dpif_get_dp_stats(dpif, &stats); |
575 | if (error) { | |
f4ba4c4f | 576 | goto exit; |
064af421 BP |
577 | } |
578 | ||
f4ba4c4f | 579 | ports = xcalloc(stats.n_ports, sizeof *ports); |
1acb6baa | 580 | retval = dpif->dpif_class->port_list(dpif, ports, stats.n_ports); |
96fba48f | 581 | if (retval < 0) { |
f4ba4c4f | 582 | /* Hard error. */ |
96fba48f | 583 | error = -retval; |
f4ba4c4f BP |
584 | free(ports); |
585 | goto exit; | |
96fba48f | 586 | } else if (retval <= stats.n_ports) { |
f4ba4c4f BP |
587 | /* Success. */ |
588 | error = 0; | |
96fba48f | 589 | n_ports = retval; |
f4ba4c4f BP |
590 | goto exit; |
591 | } else { | |
592 | /* Soft error: port count increased behind our back. Try again. */ | |
593 | free(ports); | |
064af421 | 594 | } |
f4ba4c4f | 595 | } |
064af421 | 596 | |
f4ba4c4f BP |
597 | exit: |
598 | if (error) { | |
599 | *portsp = NULL; | |
600 | *n_portsp = 0; | |
601 | } else { | |
602 | *portsp = ports; | |
603 | *n_portsp = n_ports; | |
604 | } | |
96fba48f | 605 | log_operation(dpif, "port_list", error); |
064af421 BP |
606 | return error; |
607 | } | |
608 | ||
e9e28be3 BP |
609 | /* Polls for changes in the set of ports in 'dpif'. If the set of ports in |
610 | * 'dpif' has changed, this function does one of the following: | |
611 | * | |
612 | * - Stores the name of the device that was added to or deleted from 'dpif' in | |
613 | * '*devnamep' and returns 0. The caller is responsible for freeing | |
614 | * '*devnamep' (with free()) when it no longer needs it. | |
615 | * | |
616 | * - Returns ENOBUFS and sets '*devnamep' to NULL. | |
617 | * | |
618 | * This function may also return 'false positives', where it returns 0 and | |
619 | * '*devnamep' names a device that was not actually added or deleted or it | |
620 | * returns ENOBUFS without any change. | |
621 | * | |
622 | * Returns EAGAIN if the set of ports in 'dpif' has not changed. May also | |
623 | * return other positive errno values to indicate that something has gone | |
624 | * wrong. */ | |
625 | int | |
626 | dpif_port_poll(const struct dpif *dpif, char **devnamep) | |
627 | { | |
1acb6baa | 628 | int error = dpif->dpif_class->port_poll(dpif, devnamep); |
e9e28be3 BP |
629 | if (error) { |
630 | *devnamep = NULL; | |
631 | } | |
632 | return error; | |
633 | } | |
634 | ||
635 | /* Arranges for the poll loop to wake up when port_poll(dpif) will return a | |
636 | * value other than EAGAIN. */ | |
637 | void | |
638 | dpif_port_poll_wait(const struct dpif *dpif) | |
639 | { | |
1acb6baa | 640 | dpif->dpif_class->port_poll_wait(dpif); |
e9e28be3 BP |
641 | } |
642 | ||
96fba48f BP |
643 | /* Deletes all flows from 'dpif'. Returns 0 if successful, otherwise a |
644 | * positive errno value. */ | |
645 | int | |
646 | dpif_flow_flush(struct dpif *dpif) | |
064af421 | 647 | { |
96fba48f BP |
648 | int error; |
649 | ||
650 | COVERAGE_INC(dpif_flow_flush); | |
651 | ||
1acb6baa | 652 | error = dpif->dpif_class->flow_flush(dpif); |
96fba48f BP |
653 | log_operation(dpif, "flow_flush", error); |
654 | return error; | |
064af421 BP |
655 | } |
656 | ||
96fba48f BP |
657 | /* Queries 'dpif' for a flow entry matching 'flow->key'. |
658 | * | |
659 | * If a flow matching 'flow->key' exists in 'dpif', stores statistics for the | |
660 | * flow into 'flow->stats'. If 'flow->n_actions' is zero, then 'flow->actions' | |
661 | * is ignored. If 'flow->n_actions' is nonzero, then 'flow->actions' should | |
662 | * point to an array of the specified number of actions. At most that many of | |
663 | * the flow's actions will be copied into that array. 'flow->n_actions' will | |
664 | * be updated to the number of actions actually present in the flow, which may | |
665 | * be greater than the number stored if the flow has more actions than space | |
666 | * available in the array. | |
667 | * | |
668 | * If no flow matching 'flow->key' exists in 'dpif', returns ENOENT. On other | |
669 | * failure, returns a positive errno value. */ | |
670 | int | |
671 | dpif_flow_get(const struct dpif *dpif, struct odp_flow *flow) | |
064af421 | 672 | { |
96fba48f BP |
673 | int error; |
674 | ||
675 | COVERAGE_INC(dpif_flow_get); | |
676 | ||
677 | check_rw_odp_flow(flow); | |
1acb6baa | 678 | error = dpif->dpif_class->flow_get(dpif, flow, 1); |
96fba48f BP |
679 | if (!error) { |
680 | error = flow->stats.error; | |
064af421 | 681 | } |
b843fa1b BP |
682 | if (error) { |
683 | /* Make the results predictable on error. */ | |
684 | memset(&flow->stats, 0, sizeof flow->stats); | |
685 | flow->n_actions = 0; | |
686 | } | |
96fba48f BP |
687 | if (should_log_flow_message(error)) { |
688 | log_flow_operation(dpif, "flow_get", error, flow); | |
064af421 | 689 | } |
96fba48f | 690 | return error; |
064af421 BP |
691 | } |
692 | ||
96fba48f BP |
693 | /* For each flow 'flow' in the 'n' flows in 'flows': |
694 | * | |
695 | * - If a flow matching 'flow->key' exists in 'dpif': | |
696 | * | |
697 | * Stores 0 into 'flow->stats.error' and stores statistics for the flow | |
698 | * into 'flow->stats'. | |
699 | * | |
700 | * If 'flow->n_actions' is zero, then 'flow->actions' is ignored. If | |
701 | * 'flow->n_actions' is nonzero, then 'flow->actions' should point to an | |
702 | * array of the specified number of actions. At most that many of the | |
703 | * flow's actions will be copied into that array. 'flow->n_actions' will | |
704 | * be updated to the number of actions actually present in the flow, which | |
705 | * may be greater than the number stored if the flow has more actions than | |
706 | * space available in the array. | |
707 | * | |
708 | * - Flow-specific errors are indicated by a positive errno value in | |
709 | * 'flow->stats.error'. In particular, ENOENT indicates that no flow | |
710 | * matching 'flow->key' exists in 'dpif'. When an error value is stored, the | |
711 | * contents of 'flow->key' are preserved but other members of 'flow' should | |
712 | * be treated as indeterminate. | |
713 | * | |
714 | * Returns 0 if all 'n' flows in 'flows' were updated (whether they were | |
715 | * individually successful or not is indicated by 'flow->stats.error', | |
716 | * however). Returns a positive errno value if an error that prevented this | |
717 | * update occurred, in which the caller must not depend on any elements in | |
718 | * 'flows' being updated or not updated. | |
719 | */ | |
720 | int | |
721 | dpif_flow_get_multiple(const struct dpif *dpif, | |
722 | struct odp_flow flows[], size_t n) | |
064af421 | 723 | { |
96fba48f BP |
724 | int error; |
725 | size_t i; | |
726 | ||
727 | COVERAGE_ADD(dpif_flow_get, n); | |
728 | ||
729 | for (i = 0; i < n; i++) { | |
730 | check_rw_odp_flow(&flows[i]); | |
064af421 | 731 | } |
96fba48f | 732 | |
1acb6baa | 733 | error = dpif->dpif_class->flow_get(dpif, flows, n); |
96fba48f | 734 | log_operation(dpif, "flow_get_multiple", error); |
064af421 BP |
735 | return error; |
736 | } | |
737 | ||
96fba48f BP |
738 | /* Adds or modifies a flow in 'dpif' as specified in 'put': |
739 | * | |
740 | * - If the flow specified in 'put->flow' does not exist in 'dpif', then | |
741 | * behavior depends on whether ODPPF_CREATE is specified in 'put->flags': if | |
742 | * it is, the flow will be added, otherwise the operation will fail with | |
743 | * ENOENT. | |
744 | * | |
745 | * - Otherwise, the flow specified in 'put->flow' does exist in 'dpif'. | |
746 | * Behavior in this case depends on whether ODPPF_MODIFY is specified in | |
747 | * 'put->flags': if it is, the flow's actions will be updated, otherwise the | |
748 | * operation will fail with EEXIST. If the flow's actions are updated, then | |
749 | * its statistics will be zeroed if ODPPF_ZERO_STATS is set in 'put->flags', | |
750 | * left as-is otherwise. | |
751 | * | |
752 | * Returns 0 if successful, otherwise a positive errno value. | |
753 | */ | |
064af421 BP |
754 | int |
755 | dpif_flow_put(struct dpif *dpif, struct odp_flow_put *put) | |
756 | { | |
96fba48f BP |
757 | int error; |
758 | ||
064af421 | 759 | COVERAGE_INC(dpif_flow_put); |
96fba48f | 760 | |
1acb6baa | 761 | error = dpif->dpif_class->flow_put(dpif, put); |
064af421 | 762 | if (should_log_flow_message(error)) { |
96fba48f | 763 | log_flow_put(dpif, error, put); |
064af421 BP |
764 | } |
765 | return error; | |
766 | } | |
767 | ||
96fba48f BP |
768 | /* Deletes a flow matching 'flow->key' from 'dpif' or returns ENOENT if 'dpif' |
769 | * does not contain such a flow. | |
770 | * | |
771 | * If successful, updates 'flow->stats', 'flow->n_actions', and 'flow->actions' | |
772 | * as described for dpif_flow_get(). */ | |
064af421 BP |
773 | int |
774 | dpif_flow_del(struct dpif *dpif, struct odp_flow *flow) | |
775 | { | |
f1aa2072 BP |
776 | int error; |
777 | ||
96fba48f | 778 | COVERAGE_INC(dpif_flow_del); |
f1aa2072 | 779 | |
064af421 | 780 | check_rw_odp_flow(flow); |
96fba48f | 781 | memset(&flow->stats, 0, sizeof flow->stats); |
064af421 | 782 | |
1acb6baa | 783 | error = dpif->dpif_class->flow_del(dpif, flow); |
96fba48f BP |
784 | if (should_log_flow_message(error)) { |
785 | log_flow_operation(dpif, "delete flow", error, flow); | |
064af421 | 786 | } |
96fba48f | 787 | return error; |
064af421 BP |
788 | } |
789 | ||
96fba48f BP |
790 | /* Stores up to 'n' flows in 'dpif' into 'flows', including their statistics |
791 | * but not including any information about their actions. If successful, | |
792 | * returns 0 and sets '*n_out' to the number of flows actually present in | |
793 | * 'dpif', which might be greater than the number stored (if 'dpif' has more | |
794 | * than 'n' flows). On failure, returns a negative errno value and sets | |
795 | * '*n_out' to 0. */ | |
064af421 BP |
796 | int |
797 | dpif_flow_list(const struct dpif *dpif, struct odp_flow flows[], size_t n, | |
798 | size_t *n_out) | |
799 | { | |
064af421 | 800 | uint32_t i; |
96fba48f | 801 | int retval; |
064af421 BP |
802 | |
803 | COVERAGE_INC(dpif_flow_query_list); | |
064af421 BP |
804 | if (RUNNING_ON_VALGRIND) { |
805 | memset(flows, 0, n * sizeof *flows); | |
806 | } else { | |
807 | for (i = 0; i < n; i++) { | |
808 | flows[i].actions = NULL; | |
809 | flows[i].n_actions = 0; | |
810 | } | |
811 | } | |
1acb6baa | 812 | retval = dpif->dpif_class->flow_list(dpif, flows, n); |
96fba48f | 813 | if (retval < 0) { |
064af421 | 814 | *n_out = 0; |
b29ba128 | 815 | VLOG_WARN_RL(&error_rl, "%s: flow list failed (%s)", |
96fba48f BP |
816 | dpif_name(dpif), strerror(-retval)); |
817 | return -retval; | |
064af421 | 818 | } else { |
96fba48f BP |
819 | COVERAGE_ADD(dpif_flow_query_list_n, retval); |
820 | *n_out = MIN(n, retval); | |
821 | VLOG_DBG_RL(&dpmsg_rl, "%s: listed %zu flows (of %d)", | |
822 | dpif_name(dpif), *n_out, retval); | |
823 | return 0; | |
064af421 | 824 | } |
064af421 BP |
825 | } |
826 | ||
96fba48f BP |
827 | /* Retrieves all of the flows in 'dpif'. |
828 | * | |
829 | * If successful, returns 0 and stores in '*flowsp' a pointer to a newly | |
830 | * allocated array of flows, including their statistics but not including any | |
831 | * information about their actions, and sets '*np' to the number of flows in | |
832 | * '*flowsp'. The caller is responsible for freeing '*flowsp' by calling | |
833 | * free(). | |
834 | * | |
835 | * On failure, returns a positive errno value and sets '*flowsp' to NULL and | |
836 | * '*np' to 0. */ | |
064af421 BP |
837 | int |
838 | dpif_flow_list_all(const struct dpif *dpif, | |
839 | struct odp_flow **flowsp, size_t *np) | |
840 | { | |
841 | struct odp_stats stats; | |
842 | struct odp_flow *flows; | |
843 | size_t n_flows; | |
844 | int error; | |
845 | ||
846 | *flowsp = NULL; | |
847 | *np = 0; | |
848 | ||
849 | error = dpif_get_dp_stats(dpif, &stats); | |
850 | if (error) { | |
851 | return error; | |
852 | } | |
853 | ||
854 | flows = xmalloc(sizeof *flows * stats.n_flows); | |
855 | error = dpif_flow_list(dpif, flows, stats.n_flows, &n_flows); | |
856 | if (error) { | |
857 | free(flows); | |
858 | return error; | |
859 | } | |
860 | ||
861 | if (stats.n_flows != n_flows) { | |
b29ba128 | 862 | VLOG_WARN_RL(&error_rl, "%s: datapath stats reported %"PRIu32" " |
064af421 | 863 | "flows but flow listing reported %zu", |
b29ba128 | 864 | dpif_name(dpif), stats.n_flows, n_flows); |
064af421 BP |
865 | } |
866 | *flowsp = flows; | |
867 | *np = n_flows; | |
868 | return 0; | |
869 | } | |
870 | ||
96fba48f BP |
871 | /* Causes 'dpif' to perform the 'n_actions' actions in 'actions' on the |
872 | * Ethernet frame specified in 'packet'. | |
873 | * | |
96fba48f | 874 | * Returns 0 if successful, otherwise a positive errno value. */ |
064af421 | 875 | int |
f1588b1f | 876 | dpif_execute(struct dpif *dpif, |
064af421 BP |
877 | const union odp_action actions[], size_t n_actions, |
878 | const struct ofpbuf *buf) | |
879 | { | |
880 | int error; | |
881 | ||
882 | COVERAGE_INC(dpif_execute); | |
883 | if (n_actions > 0) { | |
f1588b1f | 884 | error = dpif->dpif_class->execute(dpif, actions, n_actions, buf); |
064af421 BP |
885 | } else { |
886 | error = 0; | |
887 | } | |
888 | ||
889 | if (!(error ? VLOG_DROP_WARN(&error_rl) : VLOG_DROP_DBG(&dpmsg_rl))) { | |
890 | struct ds ds = DS_EMPTY_INITIALIZER; | |
891 | char *packet = ofp_packet_to_string(buf->data, buf->size, buf->size); | |
b29ba128 | 892 | ds_put_format(&ds, "%s: execute ", dpif_name(dpif)); |
064af421 BP |
893 | format_odp_actions(&ds, actions, n_actions); |
894 | if (error) { | |
895 | ds_put_format(&ds, " failed (%s)", strerror(error)); | |
896 | } | |
897 | ds_put_format(&ds, " on packet %s", packet); | |
898 | vlog(THIS_MODULE, error ? VLL_WARN : VLL_DBG, "%s", ds_cstr(&ds)); | |
899 | ds_destroy(&ds); | |
900 | free(packet); | |
901 | } | |
902 | return error; | |
903 | } | |
904 | ||
96fba48f BP |
905 | /* Retrieves 'dpif''s "listen mask" into '*listen_mask'. Each ODPL_* bit set |
906 | * in '*listen_mask' indicates that dpif_recv() will receive messages of that | |
907 | * type. Returns 0 if successful, otherwise a positive errno value. */ | |
8f24562a BP |
908 | int |
909 | dpif_recv_get_mask(const struct dpif *dpif, int *listen_mask) | |
910 | { | |
1acb6baa | 911 | int error = dpif->dpif_class->recv_get_mask(dpif, listen_mask); |
8f24562a BP |
912 | if (error) { |
913 | *listen_mask = 0; | |
914 | } | |
96fba48f | 915 | log_operation(dpif, "recv_get_mask", error); |
8f24562a BP |
916 | return error; |
917 | } | |
918 | ||
96fba48f BP |
919 | /* Sets 'dpif''s "listen mask" to 'listen_mask'. Each ODPL_* bit set in |
920 | * '*listen_mask' requests that dpif_recv() receive messages of that type. | |
921 | * Returns 0 if successful, otherwise a positive errno value. */ | |
8f24562a BP |
922 | int |
923 | dpif_recv_set_mask(struct dpif *dpif, int listen_mask) | |
924 | { | |
1acb6baa | 925 | int error = dpif->dpif_class->recv_set_mask(dpif, listen_mask); |
96fba48f BP |
926 | log_operation(dpif, "recv_set_mask", error); |
927 | return error; | |
8f24562a BP |
928 | } |
929 | ||
b4a7a3f3 BP |
930 | /* Retrieve the sFlow sampling probability. '*probability' is expressed as the |
931 | * number of packets out of UINT_MAX to sample, e.g. probability/UINT_MAX is | |
932 | * the probability of sampling a given packet. | |
72b06300 BP |
933 | * |
934 | * Returns 0 if successful, otherwise a positive errno value. EOPNOTSUPP | |
935 | * indicates that 'dpif' does not support sFlow sampling. */ | |
936 | int | |
937 | dpif_get_sflow_probability(const struct dpif *dpif, uint32_t *probability) | |
938 | { | |
49c36903 BP |
939 | int error = (dpif->dpif_class->get_sflow_probability |
940 | ? dpif->dpif_class->get_sflow_probability(dpif, probability) | |
72b06300 BP |
941 | : EOPNOTSUPP); |
942 | if (error) { | |
943 | *probability = 0; | |
944 | } | |
945 | log_operation(dpif, "get_sflow_probability", error); | |
946 | return error; | |
947 | } | |
948 | ||
b4a7a3f3 BP |
949 | /* Set the sFlow sampling probability. 'probability' is expressed as the |
950 | * number of packets out of UINT_MAX to sample, e.g. probability/UINT_MAX is | |
951 | * the probability of sampling a given packet. | |
72b06300 BP |
952 | * |
953 | * Returns 0 if successful, otherwise a positive errno value. EOPNOTSUPP | |
954 | * indicates that 'dpif' does not support sFlow sampling. */ | |
955 | int | |
956 | dpif_set_sflow_probability(struct dpif *dpif, uint32_t probability) | |
957 | { | |
49c36903 BP |
958 | int error = (dpif->dpif_class->set_sflow_probability |
959 | ? dpif->dpif_class->set_sflow_probability(dpif, probability) | |
72b06300 BP |
960 | : EOPNOTSUPP); |
961 | log_operation(dpif, "set_sflow_probability", error); | |
962 | return error; | |
963 | } | |
964 | ||
96fba48f BP |
965 | /* Attempts to receive a message from 'dpif'. If successful, stores the |
966 | * message into '*packetp'. The message, if one is received, will begin with | |
43253595 BP |
967 | * 'struct odp_msg' as a header, and will have at least DPIF_RECV_MSG_PADDING |
968 | * bytes of headroom. Only messages of the types selected with | |
96fba48f BP |
969 | * dpif_set_listen_mask() will ordinarily be received (but if a message type is |
970 | * enabled and then later disabled, some stragglers might pop up). | |
971 | * | |
972 | * Returns 0 if successful, otherwise a positive errno value. Returns EAGAIN | |
973 | * if no message is immediately available. */ | |
064af421 | 974 | int |
96fba48f | 975 | dpif_recv(struct dpif *dpif, struct ofpbuf **packetp) |
064af421 | 976 | { |
1acb6baa | 977 | int error = dpif->dpif_class->recv(dpif, packetp); |
96fba48f | 978 | if (!error) { |
43253595 BP |
979 | struct ofpbuf *buf = *packetp; |
980 | ||
981 | assert(ofpbuf_headroom(buf) >= DPIF_RECV_MSG_PADDING); | |
96fba48f | 982 | if (VLOG_IS_DBG_ENABLED()) { |
96fba48f BP |
983 | struct odp_msg *msg = buf->data; |
984 | void *payload = msg + 1; | |
985 | size_t payload_len = buf->size - sizeof *msg; | |
986 | char *s = ofp_packet_to_string(payload, payload_len, payload_len); | |
987 | VLOG_DBG_RL(&dpmsg_rl, "%s: received %s message of length " | |
988 | "%zu on port %"PRIu16": %s", dpif_name(dpif), | |
989 | (msg->type == _ODPL_MISS_NR ? "miss" | |
990 | : msg->type == _ODPL_ACTION_NR ? "action" | |
72b06300 | 991 | : msg->type == _ODPL_SFLOW_NR ? "sFlow" |
96fba48f BP |
992 | : "<unknown>"), |
993 | payload_len, msg->port, s); | |
994 | free(s); | |
064af421 | 995 | } |
064af421 | 996 | } else { |
96fba48f | 997 | *packetp = NULL; |
064af421 | 998 | } |
064af421 BP |
999 | return error; |
1000 | } | |
1001 | ||
96fba48f BP |
1002 | /* Discards all messages that would otherwise be received by dpif_recv() on |
1003 | * 'dpif'. Returns 0 if successful, otherwise a positive errno value. */ | |
1004 | int | |
1005 | dpif_recv_purge(struct dpif *dpif) | |
1006 | { | |
1007 | struct odp_stats stats; | |
1008 | unsigned int i; | |
1009 | int error; | |
1010 | ||
1011 | COVERAGE_INC(dpif_purge); | |
1012 | ||
1013 | error = dpif_get_dp_stats(dpif, &stats); | |
1014 | if (error) { | |
1015 | return error; | |
1016 | } | |
1017 | ||
72b06300 | 1018 | for (i = 0; i < stats.max_miss_queue + stats.max_action_queue + stats.max_sflow_queue; i++) { |
96fba48f BP |
1019 | struct ofpbuf *buf; |
1020 | error = dpif_recv(dpif, &buf); | |
1021 | if (error) { | |
1022 | return error == EAGAIN ? 0 : error; | |
1023 | } | |
1024 | ofpbuf_delete(buf); | |
1025 | } | |
1026 | return 0; | |
1027 | } | |
1028 | ||
1029 | /* Arranges for the poll loop to wake up when 'dpif' has a message queued to be | |
1030 | * received with dpif_recv(). */ | |
064af421 BP |
1031 | void |
1032 | dpif_recv_wait(struct dpif *dpif) | |
1033 | { | |
1acb6baa | 1034 | dpif->dpif_class->recv_wait(dpif); |
064af421 | 1035 | } |
53a4218d | 1036 | |
96fba48f BP |
1037 | /* Obtains the NetFlow engine type and engine ID for 'dpif' into '*engine_type' |
1038 | * and '*engine_id', respectively. */ | |
53a4218d BP |
1039 | void |
1040 | dpif_get_netflow_ids(const struct dpif *dpif, | |
1041 | uint8_t *engine_type, uint8_t *engine_id) | |
1042 | { | |
96fba48f BP |
1043 | *engine_type = dpif->netflow_engine_type; |
1044 | *engine_id = dpif->netflow_engine_id; | |
1045 | } | |
aae51f53 BP |
1046 | |
1047 | /* Translates OpenFlow queue ID 'queue_id' (in host byte order) into a priority | |
1048 | * value for use in the ODPAT_SET_PRIORITY action. On success, returns 0 and | |
1049 | * stores the priority into '*priority'. On failure, returns a positive errno | |
1050 | * value and stores 0 into '*priority'. */ | |
1051 | int | |
1052 | dpif_queue_to_priority(const struct dpif *dpif, uint32_t queue_id, | |
1053 | uint32_t *priority) | |
1054 | { | |
1055 | int error = (dpif->dpif_class->queue_to_priority | |
1056 | ? dpif->dpif_class->queue_to_priority(dpif, queue_id, | |
1057 | priority) | |
1058 | : EOPNOTSUPP); | |
1059 | if (error) { | |
1060 | *priority = 0; | |
1061 | } | |
1062 | log_operation(dpif, "queue_to_priority", error); | |
1063 | return error; | |
1064 | } | |
96fba48f BP |
1065 | \f |
1066 | void | |
1acb6baa BP |
1067 | dpif_init(struct dpif *dpif, const struct dpif_class *dpif_class, |
1068 | const char *name, | |
96fba48f BP |
1069 | uint8_t netflow_engine_type, uint8_t netflow_engine_id) |
1070 | { | |
1acb6baa | 1071 | dpif->dpif_class = dpif_class; |
1a6f1e2a | 1072 | dpif->base_name = xstrdup(name); |
a4af0040 | 1073 | dpif->full_name = xasprintf("%s@%s", dpif_class->type, name); |
96fba48f BP |
1074 | dpif->netflow_engine_type = netflow_engine_type; |
1075 | dpif->netflow_engine_id = netflow_engine_id; | |
1076 | } | |
999401aa JG |
1077 | |
1078 | /* Undoes the results of initialization. | |
1079 | * | |
1080 | * Normally this function only needs to be called from dpif_close(). | |
1081 | * However, it may be called by providers due to an error on opening | |
1082 | * that occurs after initialization. It this case dpif_close() would | |
1083 | * never be called. */ | |
1084 | void | |
1085 | dpif_uninit(struct dpif *dpif, bool close) | |
1086 | { | |
1087 | char *base_name = dpif->base_name; | |
1088 | char *full_name = dpif->full_name; | |
1089 | ||
1090 | if (close) { | |
a4af0040 | 1091 | dpif->dpif_class->close(dpif); |
999401aa JG |
1092 | } |
1093 | ||
1094 | free(base_name); | |
1095 | free(full_name); | |
1096 | } | |
96fba48f BP |
1097 | \f |
1098 | static void | |
1099 | log_operation(const struct dpif *dpif, const char *operation, int error) | |
1100 | { | |
1101 | if (!error) { | |
1102 | VLOG_DBG_RL(&dpmsg_rl, "%s: %s success", dpif_name(dpif), operation); | |
71ce9235 | 1103 | } else if (is_errno(error)) { |
96fba48f BP |
1104 | VLOG_WARN_RL(&error_rl, "%s: %s failed (%s)", |
1105 | dpif_name(dpif), operation, strerror(error)); | |
71ce9235 BP |
1106 | } else { |
1107 | VLOG_WARN_RL(&error_rl, "%s: %s failed (%d/%d)", | |
1108 | dpif_name(dpif), operation, | |
1109 | get_ofp_err_type(error), get_ofp_err_code(error)); | |
96fba48f BP |
1110 | } |
1111 | } | |
1112 | ||
1113 | static enum vlog_level | |
1114 | flow_message_log_level(int error) | |
1115 | { | |
1116 | return error ? VLL_WARN : VLL_DBG; | |
1117 | } | |
1118 | ||
1119 | static bool | |
1120 | should_log_flow_message(int error) | |
1121 | { | |
1122 | return !vlog_should_drop(THIS_MODULE, flow_message_log_level(error), | |
1123 | error ? &error_rl : &dpmsg_rl); | |
1124 | } | |
1125 | ||
1126 | static void | |
1127 | log_flow_message(const struct dpif *dpif, int error, const char *operation, | |
14608a15 BP |
1128 | const struct odp_flow_key *flow, |
1129 | const struct odp_flow_stats *stats, | |
96fba48f BP |
1130 | const union odp_action *actions, size_t n_actions) |
1131 | { | |
1132 | struct ds ds = DS_EMPTY_INITIALIZER; | |
1133 | ds_put_format(&ds, "%s: ", dpif_name(dpif)); | |
1134 | if (error) { | |
1135 | ds_put_cstr(&ds, "failed to "); | |
1136 | } | |
1137 | ds_put_format(&ds, "%s ", operation); | |
1138 | if (error) { | |
1139 | ds_put_format(&ds, "(%s) ", strerror(error)); | |
1140 | } | |
14608a15 | 1141 | format_odp_flow_key(&ds, flow); |
96fba48f BP |
1142 | if (stats) { |
1143 | ds_put_cstr(&ds, ", "); | |
1144 | format_odp_flow_stats(&ds, stats); | |
1145 | } | |
1146 | if (actions || n_actions) { | |
1147 | ds_put_cstr(&ds, ", actions:"); | |
1148 | format_odp_actions(&ds, actions, n_actions); | |
1149 | } | |
1150 | vlog(THIS_MODULE, flow_message_log_level(error), "%s", ds_cstr(&ds)); | |
1151 | ds_destroy(&ds); | |
1152 | } | |
1153 | ||
1154 | static void | |
1155 | log_flow_operation(const struct dpif *dpif, const char *operation, int error, | |
1156 | struct odp_flow *flow) | |
1157 | { | |
1158 | if (error) { | |
1159 | flow->n_actions = 0; | |
1160 | } | |
1161 | log_flow_message(dpif, error, operation, &flow->key, | |
1162 | !error ? &flow->stats : NULL, | |
1163 | flow->actions, flow->n_actions); | |
1164 | } | |
1165 | ||
1166 | static void | |
1167 | log_flow_put(struct dpif *dpif, int error, const struct odp_flow_put *put) | |
1168 | { | |
1169 | enum { ODPPF_ALL = ODPPF_CREATE | ODPPF_MODIFY | ODPPF_ZERO_STATS }; | |
1170 | struct ds s; | |
1171 | ||
1172 | ds_init(&s); | |
1173 | ds_put_cstr(&s, "put"); | |
1174 | if (put->flags & ODPPF_CREATE) { | |
1175 | ds_put_cstr(&s, "[create]"); | |
1176 | } | |
1177 | if (put->flags & ODPPF_MODIFY) { | |
1178 | ds_put_cstr(&s, "[modify]"); | |
1179 | } | |
1180 | if (put->flags & ODPPF_ZERO_STATS) { | |
1181 | ds_put_cstr(&s, "[zero]"); | |
1182 | } | |
1183 | if (put->flags & ~ODPPF_ALL) { | |
1184 | ds_put_format(&s, "[%x]", put->flags & ~ODPPF_ALL); | |
1185 | } | |
1186 | log_flow_message(dpif, error, ds_cstr(&s), &put->flow.key, | |
1187 | !error ? &put->flow.stats : NULL, | |
1188 | put->flow.actions, put->flow.n_actions); | |
1189 | ds_destroy(&s); | |
1190 | } | |
1191 | ||
1192 | /* There is a tendency to construct odp_flow objects on the stack and to | |
1193 | * forget to properly initialize their "actions" and "n_actions" members. | |
1194 | * When this happens, we get memory corruption because the kernel | |
1195 | * writes through the random pointer that is in the "actions" member. | |
1196 | * | |
1197 | * This function attempts to combat the problem by: | |
1198 | * | |
1199 | * - Forcing a segfault if "actions" points to an invalid region (instead | |
1200 | * of just getting back EFAULT, which can be easily missed in the log). | |
1201 | * | |
1202 | * - Storing a distinctive value that is likely to cause an | |
1203 | * easy-to-identify error later if it is dereferenced, etc. | |
1204 | * | |
1205 | * - Triggering a warning on uninitialized memory from Valgrind if | |
1206 | * "actions" or "n_actions" was not initialized. | |
1207 | */ | |
1208 | static void | |
1209 | check_rw_odp_flow(struct odp_flow *flow) | |
1210 | { | |
1211 | if (flow->n_actions) { | |
1212 | memset(&flow->actions[0], 0xcc, sizeof flow->actions[0]); | |
1213 | } | |
53a4218d | 1214 | } |