Branch data Line data Source code
1 : : /*
2 : : * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc.
3 : : *
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:
7 : : *
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.
15 : : */
16 : :
17 : : #include <config.h>
18 : : #include "dpif-provider.h"
19 : :
20 : : #include <ctype.h>
21 : : #include <errno.h>
22 : : #include <inttypes.h>
23 : : #include <stdlib.h>
24 : : #include <string.h>
25 : :
26 : : #include "coverage.h"
27 : : #include "dpctl.h"
28 : : #include "dp-packet.h"
29 : : #include "dpif-netdev.h"
30 : : #include "openvswitch/dynamic-string.h"
31 : : #include "flow.h"
32 : : #include "netdev.h"
33 : : #include "netlink.h"
34 : : #include "odp-execute.h"
35 : : #include "odp-util.h"
36 : : #include "openvswitch/ofp-print.h"
37 : : #include "openvswitch/ofp-util.h"
38 : : #include "openvswitch/ofpbuf.h"
39 : : #include "packets.h"
40 : : #include "poll-loop.h"
41 : : #include "route-table.h"
42 : : #include "seq.h"
43 : : #include "openvswitch/shash.h"
44 : : #include "sset.h"
45 : : #include "timeval.h"
46 : : #include "tnl-neigh-cache.h"
47 : : #include "tnl-ports.h"
48 : : #include "util.h"
49 : : #include "uuid.h"
50 : : #include "valgrind.h"
51 : : #include "openvswitch/ofp-errors.h"
52 : : #include "openvswitch/vlog.h"
53 : :
54 : 20190 : VLOG_DEFINE_THIS_MODULE(dpif);
55 : :
56 : 93222 : COVERAGE_DEFINE(dpif_destroy);
57 : 105654 : COVERAGE_DEFINE(dpif_port_add);
58 : 94830 : COVERAGE_DEFINE(dpif_port_del);
59 : 101928 : COVERAGE_DEFINE(dpif_flow_flush);
60 : 125994 : COVERAGE_DEFINE(dpif_flow_get);
61 : 139944 : COVERAGE_DEFINE(dpif_flow_put);
62 : 165750 : COVERAGE_DEFINE(dpif_flow_del);
63 : 123384 : COVERAGE_DEFINE(dpif_execute);
64 : 92826 : COVERAGE_DEFINE(dpif_purge);
65 : 94338 : COVERAGE_DEFINE(dpif_execute_with_help);
66 : :
67 : : static const struct dpif_class *base_dpif_classes[] = {
68 : : #if defined(__linux__) || defined(_WIN32)
69 : : &dpif_netlink_class,
70 : : #endif
71 : : &dpif_netdev_class,
72 : : };
73 : :
74 : : struct registered_dpif_class {
75 : : const struct dpif_class *dpif_class;
76 : : int refcount;
77 : : };
78 : : static struct shash dpif_classes = SHASH_INITIALIZER(&dpif_classes);
79 : : static struct sset dpif_blacklist = SSET_INITIALIZER(&dpif_blacklist);
80 : :
81 : : /* Protects 'dpif_classes', including the refcount, and 'dpif_blacklist'. */
82 : : static struct ovs_mutex dpif_mutex = OVS_MUTEX_INITIALIZER;
83 : :
84 : : /* Rate limit for individual messages going to or from the datapath, output at
85 : : * DBG level. This is very high because, if these are enabled, it is because
86 : : * we really need to see them. */
87 : : static struct vlog_rate_limit dpmsg_rl = VLOG_RATE_LIMIT_INIT(600, 600);
88 : :
89 : : /* Not really much point in logging many dpif errors. */
90 : : static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(60, 5);
91 : :
92 : : static void log_flow_message(const struct dpif *dpif, int error,
93 : : const char *operation,
94 : : const struct nlattr *key, size_t key_len,
95 : : const struct nlattr *mask, size_t mask_len,
96 : : const ovs_u128 *ufid,
97 : : const struct dpif_flow_stats *stats,
98 : : const struct nlattr *actions, size_t actions_len);
99 : : static void log_operation(const struct dpif *, const char *operation,
100 : : int error);
101 : : static bool should_log_flow_message(int error);
102 : : static void log_flow_put_message(struct dpif *, const struct dpif_flow_put *,
103 : : int error);
104 : : static void log_flow_del_message(struct dpif *, const struct dpif_flow_del *,
105 : : int error);
106 : : static void log_execute_message(struct dpif *, const struct dpif_execute *,
107 : : bool subexecute, int error);
108 : : static void log_flow_get_message(const struct dpif *,
109 : : const struct dpif_flow_get *, int error);
110 : :
111 : : /* Incremented whenever tnl route, arp, etc changes. */
112 : : struct seq *tnl_conf_seq;
113 : :
114 : : static void
115 : 780170 : dp_initialize(void)
116 : : {
117 : : static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
118 : :
119 [ + + ]: 780170 : if (ovsthread_once_start(&once)) {
120 : : int i;
121 : :
122 : 684 : tnl_conf_seq = seq_create();
123 : 684 : dpctl_unixctl_register();
124 : 684 : tnl_port_map_init();
125 : 684 : tnl_neigh_cache_init();
126 : 684 : route_table_init();
127 : :
128 [ + + ]: 2052 : for (i = 0; i < ARRAY_SIZE(base_dpif_classes); i++) {
129 : 1368 : dp_register_provider(base_dpif_classes[i]);
130 : : }
131 : :
132 : 684 : ovsthread_once_done(&once);
133 : : }
134 : 780170 : }
135 : :
136 : : static int
137 : 1994 : dp_register_provider__(const struct dpif_class *new_class)
138 : : {
139 : : struct registered_dpif_class *registered_class;
140 : : int error;
141 : :
142 [ + + ]: 1994 : if (sset_contains(&dpif_blacklist, new_class->type)) {
143 [ - + ]: 474 : VLOG_DBG("attempted to register blacklisted provider: %s",
144 : : new_class->type);
145 : 474 : return EINVAL;
146 : : }
147 : :
148 [ - + ]: 1520 : if (shash_find(&dpif_classes, new_class->type)) {
149 [ # # ]: 0 : VLOG_WARN("attempted to register duplicate datapath provider: %s",
150 : : new_class->type);
151 : 0 : return EEXIST;
152 : : }
153 : :
154 [ + + ]: 1520 : error = new_class->init ? new_class->init() : 0;
155 [ - + ]: 1520 : if (error) {
156 [ # # ]: 0 : VLOG_WARN("failed to initialize %s datapath class: %s",
157 : : new_class->type, ovs_strerror(error));
158 : 0 : return error;
159 : : }
160 : :
161 : 1520 : registered_class = xmalloc(sizeof *registered_class);
162 : 1520 : registered_class->dpif_class = new_class;
163 : 1520 : registered_class->refcount = 0;
164 : :
165 : 1520 : shash_add(&dpif_classes, new_class->type, registered_class);
166 : :
167 : 1520 : return 0;
168 : : }
169 : :
170 : : /* Registers a new datapath provider. After successful registration, new
171 : : * datapaths of that type can be opened using dpif_open(). */
172 : : int
173 : 1994 : dp_register_provider(const struct dpif_class *new_class)
174 : : {
175 : : int error;
176 : :
177 : 1994 : ovs_mutex_lock(&dpif_mutex);
178 : 1994 : error = dp_register_provider__(new_class);
179 : 1994 : ovs_mutex_unlock(&dpif_mutex);
180 : :
181 : 1994 : return error;
182 : : }
183 : :
184 : : /* Unregisters a datapath provider. 'type' must have been previously
185 : : * registered and not currently be in use by any dpifs. After unregistration
186 : : * new datapaths of that type cannot be opened using dpif_open(). */
187 : : static int
188 : 83 : dp_unregister_provider__(const char *type)
189 : : {
190 : : struct shash_node *node;
191 : : struct registered_dpif_class *registered_class;
192 : :
193 : 83 : node = shash_find(&dpif_classes, type);
194 [ - + ]: 83 : if (!node) {
195 : 0 : return EAFNOSUPPORT;
196 : : }
197 : :
198 : 83 : registered_class = node->data;
199 [ - + ]: 83 : if (registered_class->refcount) {
200 [ # # ]: 0 : VLOG_WARN("attempted to unregister in use datapath provider: %s", type);
201 : 0 : return EBUSY;
202 : : }
203 : :
204 : 83 : shash_delete(&dpif_classes, node);
205 : 83 : free(registered_class);
206 : :
207 : 83 : return 0;
208 : : }
209 : :
210 : : /* Unregisters a datapath provider. 'type' must have been previously
211 : : * registered and not currently be in use by any dpifs. After unregistration
212 : : * new datapaths of that type cannot be opened using dpif_open(). */
213 : : int
214 : 83 : dp_unregister_provider(const char *type)
215 : : {
216 : : int error;
217 : :
218 : 83 : dp_initialize();
219 : :
220 : 83 : ovs_mutex_lock(&dpif_mutex);
221 : 83 : error = dp_unregister_provider__(type);
222 : 83 : ovs_mutex_unlock(&dpif_mutex);
223 : :
224 : 83 : return error;
225 : : }
226 : :
227 : : /* Blacklists a provider. Causes future calls of dp_register_provider() with
228 : : * a dpif_class which implements 'type' to fail. */
229 : : void
230 : 497 : dp_blacklist_provider(const char *type)
231 : : {
232 : 497 : ovs_mutex_lock(&dpif_mutex);
233 : 497 : sset_add(&dpif_blacklist, type);
234 : 497 : ovs_mutex_unlock(&dpif_mutex);
235 : 497 : }
236 : :
237 : : /* Adds the types of all currently registered datapath providers to 'types'.
238 : : * The caller must first initialize the sset. */
239 : : void
240 : 778488 : dp_enumerate_types(struct sset *types)
241 : : {
242 : : struct shash_node *node;
243 : :
244 : 778488 : dp_initialize();
245 : :
246 : 778488 : ovs_mutex_lock(&dpif_mutex);
247 [ + + ][ - + ]: 2606265 : SHASH_FOR_EACH(node, &dpif_classes) {
248 : 1827777 : const struct registered_dpif_class *registered_class = node->data;
249 : 1827777 : sset_add(types, registered_class->dpif_class->type);
250 : : }
251 : 778488 : ovs_mutex_unlock(&dpif_mutex);
252 : 778488 : }
253 : :
254 : : static void
255 : 1599 : dp_class_unref(struct registered_dpif_class *rc)
256 : : {
257 : 1599 : ovs_mutex_lock(&dpif_mutex);
258 [ - + ]: 1599 : ovs_assert(rc->refcount);
259 : 1599 : rc->refcount--;
260 : 1599 : ovs_mutex_unlock(&dpif_mutex);
261 : 1599 : }
262 : :
263 : : static struct registered_dpif_class *
264 : 1599 : dp_class_lookup(const char *type)
265 : : {
266 : : struct registered_dpif_class *rc;
267 : :
268 : 1599 : ovs_mutex_lock(&dpif_mutex);
269 : 1599 : rc = shash_find_data(&dpif_classes, type);
270 [ + - ]: 1599 : if (rc) {
271 : 1599 : rc->refcount++;
272 : : }
273 : 1599 : ovs_mutex_unlock(&dpif_mutex);
274 : :
275 : 1599 : return rc;
276 : : }
277 : :
278 : : /* Clears 'names' and enumerates the names of all known created datapaths with
279 : : * the given 'type'. The caller must first initialize the sset. Returns 0 if
280 : : * successful, otherwise a positive errno value.
281 : : *
282 : : * Some kinds of datapaths might not be practically enumerable. This is not
283 : : * considered an error. */
284 : : int
285 : 798 : dp_enumerate_names(const char *type, struct sset *names)
286 : : {
287 : : struct registered_dpif_class *registered_class;
288 : : const struct dpif_class *dpif_class;
289 : : int error;
290 : :
291 : 798 : dp_initialize();
292 : 798 : sset_clear(names);
293 : :
294 : 798 : registered_class = dp_class_lookup(type);
295 [ - + ]: 798 : if (!registered_class) {
296 [ # # ]: 0 : VLOG_WARN("could not enumerate unknown type: %s", type);
297 : 0 : return EAFNOSUPPORT;
298 : : }
299 : :
300 : 798 : dpif_class = registered_class->dpif_class;
301 : 1596 : error = (dpif_class->enumerate
302 : 798 : ? dpif_class->enumerate(names, dpif_class)
303 [ + - ]: 798 : : 0);
304 [ - + ]: 798 : if (error) {
305 [ # # ]: 0 : VLOG_WARN("failed to enumerate %s datapaths: %s", dpif_class->type,
306 : : ovs_strerror(error));
307 : : }
308 : 798 : dp_class_unref(registered_class);
309 : :
310 : 798 : return error;
311 : : }
312 : :
313 : : /* Parses 'datapath_name_', which is of the form [type@]name into its
314 : : * component pieces. 'name' and 'type' must be freed by the caller.
315 : : *
316 : : * The returned 'type' is normalized, as if by dpif_normalize_type(). */
317 : : void
318 : 3561 : dp_parse_name(const char *datapath_name_, char **name, char **type)
319 : : {
320 : 3561 : char *datapath_name = xstrdup(datapath_name_);
321 : : char *separator;
322 : :
323 : 3561 : separator = strchr(datapath_name, '@');
324 [ + + ]: 3561 : if (separator) {
325 : 112 : *separator = '\0';
326 : 112 : *type = datapath_name;
327 : 112 : *name = xstrdup(dpif_normalize_type(separator + 1));
328 : : } else {
329 : 3449 : *name = datapath_name;
330 : 3449 : *type = xstrdup(dpif_normalize_type(NULL));
331 : : }
332 : 3561 : }
333 : :
334 : : static int
335 : 801 : do_open(const char *name, const char *type, bool create, struct dpif **dpifp)
336 : : {
337 : 801 : struct dpif *dpif = NULL;
338 : : int error;
339 : : struct registered_dpif_class *registered_class;
340 : :
341 : 801 : dp_initialize();
342 : :
343 : 801 : type = dpif_normalize_type(type);
344 : 801 : registered_class = dp_class_lookup(type);
345 [ - + ]: 801 : if (!registered_class) {
346 [ # # ]: 0 : VLOG_WARN("could not create datapath %s of unknown type %s", name,
347 : : type);
348 : 0 : error = EAFNOSUPPORT;
349 : 0 : goto exit;
350 : : }
351 : :
352 : 801 : error = registered_class->dpif_class->open(registered_class->dpif_class,
353 : : name, create, &dpif);
354 [ + + ]: 801 : if (!error) {
355 [ - + ]: 798 : ovs_assert(dpif->dpif_class == registered_class->dpif_class);
356 : : } else {
357 : 3 : dp_class_unref(registered_class);
358 : : }
359 : :
360 : : exit:
361 [ + + ]: 801 : *dpifp = error ? NULL : dpif;
362 : 801 : return error;
363 : : }
364 : :
365 : : /* Tries to open an existing datapath named 'name' and type 'type'. Will fail
366 : : * if no datapath with 'name' and 'type' exists. 'type' may be either NULL or
367 : : * the empty string to specify the default system type. Returns 0 if
368 : : * successful, otherwise a positive errno value. On success stores a pointer
369 : : * to the datapath in '*dpifp', otherwise a null pointer. */
370 : : int
371 : 184 : dpif_open(const char *name, const char *type, struct dpif **dpifp)
372 : : {
373 : 184 : return do_open(name, type, false, dpifp);
374 : : }
375 : :
376 : : /* Tries to create and open a new datapath with the given 'name' and 'type'.
377 : : * 'type' may be either NULL or the empty string to specify the default system
378 : : * type. Will fail if a datapath with 'name' and 'type' already exists.
379 : : * Returns 0 if successful, otherwise a positive errno value. On success
380 : : * stores a pointer to the datapath in '*dpifp', otherwise a null pointer. */
381 : : int
382 : 617 : dpif_create(const char *name, const char *type, struct dpif **dpifp)
383 : : {
384 : 617 : return do_open(name, type, true, dpifp);
385 : : }
386 : :
387 : : /* Tries to open a datapath with the given 'name' and 'type', creating it if it
388 : : * does not exist. 'type' may be either NULL or the empty string to specify
389 : : * the default system type. Returns 0 if successful, otherwise a positive
390 : : * errno value. On success stores a pointer to the datapath in '*dpifp',
391 : : * otherwise a null pointer. */
392 : : int
393 : 614 : dpif_create_and_open(const char *name, const char *type, struct dpif **dpifp)
394 : : {
395 : : int error;
396 : :
397 : 614 : error = dpif_create(name, type, dpifp);
398 [ + - ][ - + ]: 614 : if (error == EEXIST || error == EBUSY) {
399 : 0 : error = dpif_open(name, type, dpifp);
400 [ # # ]: 0 : if (error) {
401 [ # # ]: 0 : VLOG_WARN("datapath %s already exists but cannot be opened: %s",
402 : : name, ovs_strerror(error));
403 : : }
404 [ - + ]: 614 : } else if (error) {
405 [ # # ]: 0 : VLOG_WARN("failed to create datapath %s: %s",
406 : : name, ovs_strerror(error));
407 : : }
408 : 614 : return error;
409 : : }
410 : :
411 : : /* Closes and frees the connection to 'dpif'. Does not destroy the datapath
412 : : * itself; call dpif_delete() first, instead, if that is desirable. */
413 : : void
414 : 798 : dpif_close(struct dpif *dpif)
415 : : {
416 [ + - ]: 798 : if (dpif) {
417 : : struct registered_dpif_class *rc;
418 : :
419 : 798 : rc = shash_find_data(&dpif_classes, dpif->dpif_class->type);
420 : 798 : dpif_uninit(dpif, true);
421 : 798 : dp_class_unref(rc);
422 : : }
423 : 798 : }
424 : :
425 : : /* Performs periodic work needed by 'dpif'. */
426 : : bool
427 : 105699 : dpif_run(struct dpif *dpif)
428 : : {
429 [ + - ]: 105699 : if (dpif->dpif_class->run) {
430 : 105699 : return dpif->dpif_class->run(dpif);
431 : : }
432 : 0 : return false;
433 : : }
434 : :
435 : : /* Arranges for poll_block() to wake up when dp_run() needs to be called for
436 : : * 'dpif'. */
437 : : void
438 : 102689 : dpif_wait(struct dpif *dpif)
439 : : {
440 [ + + ]: 102689 : if (dpif->dpif_class->wait) {
441 : 97752 : dpif->dpif_class->wait(dpif);
442 : : }
443 : 102689 : }
444 : :
445 : : /* Returns the name of datapath 'dpif' prefixed with the type
446 : : * (for use in log messages). */
447 : : const char *
448 : 8548 : dpif_name(const struct dpif *dpif)
449 : : {
450 : 8548 : return dpif->full_name;
451 : : }
452 : :
453 : : /* Returns the name of datapath 'dpif' without the type
454 : : * (for use in device names). */
455 : : const char *
456 : 7194 : dpif_base_name(const struct dpif *dpif)
457 : : {
458 : 7194 : return dpif->base_name;
459 : : }
460 : :
461 : : /* Returns the type of datapath 'dpif'. */
462 : : const char *
463 : 0 : dpif_type(const struct dpif *dpif)
464 : : {
465 : 0 : return dpif->dpif_class->type;
466 : : }
467 : :
468 : : /* Returns the fully spelled out name for the given datapath 'type'.
469 : : *
470 : : * Normalized type string can be compared with strcmp(). Unnormalized type
471 : : * string might be the same even if they have different spellings. */
472 : : const char *
473 : 69285 : dpif_normalize_type(const char *type)
474 : : {
475 [ + + ][ + - ]: 69285 : return type && type[0] ? type : "system";
476 : : }
477 : :
478 : : /* Destroys the datapath that 'dpif' is connected to, first removing all of its
479 : : * ports. After calling this function, it does not make sense to pass 'dpif'
480 : : * to any functions other than dpif_name() or dpif_close(). */
481 : : int
482 : 66 : dpif_delete(struct dpif *dpif)
483 : : {
484 : : int error;
485 : :
486 : 66 : COVERAGE_INC(dpif_destroy);
487 : :
488 : 66 : error = dpif->dpif_class->destroy(dpif);
489 : 66 : log_operation(dpif, "delete", error);
490 : 66 : return error;
491 : : }
492 : :
493 : : /* Retrieves statistics for 'dpif' into 'stats'. Returns 0 if successful,
494 : : * otherwise a positive errno value. */
495 : : int
496 : 12334 : dpif_get_dp_stats(const struct dpif *dpif, struct dpif_dp_stats *stats)
497 : : {
498 : 12334 : int error = dpif->dpif_class->get_stats(dpif, stats);
499 [ - + ]: 12334 : if (error) {
500 : 0 : memset(stats, 0, sizeof *stats);
501 : : }
502 : 12334 : log_operation(dpif, "get_stats", error);
503 : 12334 : return error;
504 : : }
505 : :
506 : : const char *
507 : 64923 : dpif_port_open_type(const char *datapath_type, const char *port_type)
508 : : {
509 : : struct registered_dpif_class *rc;
510 : :
511 : 64923 : datapath_type = dpif_normalize_type(datapath_type);
512 : :
513 : 64923 : ovs_mutex_lock(&dpif_mutex);
514 : 64923 : rc = shash_find_data(&dpif_classes, datapath_type);
515 [ + - ][ + + ]: 64923 : if (rc && rc->dpif_class->port_open_type) {
516 : 57909 : port_type = rc->dpif_class->port_open_type(rc->dpif_class, port_type);
517 : : }
518 : 64923 : ovs_mutex_unlock(&dpif_mutex);
519 : :
520 : 64923 : return port_type;
521 : : }
522 : :
523 : : /* Attempts to add 'netdev' as a port on 'dpif'. If 'port_nop' is
524 : : * non-null and its value is not ODPP_NONE, then attempts to use the
525 : : * value as the port number.
526 : : *
527 : : * If successful, returns 0 and sets '*port_nop' to the new port's port
528 : : * number (if 'port_nop' is non-null). On failure, returns a positive
529 : : * errno value and sets '*port_nop' to ODPP_NONE (if 'port_nop' is
530 : : * non-null). */
531 : : int
532 : 2138 : dpif_port_add(struct dpif *dpif, struct netdev *netdev, odp_port_t *port_nop)
533 : : {
534 : 2138 : const char *netdev_name = netdev_get_name(netdev);
535 : 2138 : odp_port_t port_no = ODPP_NONE;
536 : : int error;
537 : :
538 : 2138 : COVERAGE_INC(dpif_port_add);
539 : :
540 [ + - ]: 2138 : if (port_nop) {
541 : 2138 : port_no = *port_nop;
542 : : }
543 : :
544 : 2138 : error = dpif->dpif_class->port_add(dpif, netdev, &port_no);
545 [ + + ]: 2138 : if (!error) {
546 [ + + ]: 2137 : VLOG_DBG_RL(&dpmsg_rl, "%s: added %s as port %"PRIu32,
547 : : dpif_name(dpif), netdev_name, port_no);
548 : : } else {
549 [ + - ]: 1 : VLOG_WARN_RL(&error_rl, "%s: failed to add %s as port: %s",
550 : : dpif_name(dpif), netdev_name, ovs_strerror(error));
551 : 1 : port_no = ODPP_NONE;
552 : : }
553 [ + - ]: 2138 : if (port_nop) {
554 : 2138 : *port_nop = port_no;
555 : : }
556 : 2138 : return error;
557 : : }
558 : :
559 : : /* Attempts to remove 'dpif''s port number 'port_no'. Returns 0 if successful,
560 : : * otherwise a positive errno value. */
561 : : int
562 : 334 : dpif_port_del(struct dpif *dpif, odp_port_t port_no)
563 : : {
564 : : int error;
565 : :
566 : 334 : COVERAGE_INC(dpif_port_del);
567 : :
568 : 334 : error = dpif->dpif_class->port_del(dpif, port_no);
569 [ + + ]: 334 : if (!error) {
570 [ + + ]: 333 : VLOG_DBG_RL(&dpmsg_rl, "%s: port_del(%"PRIu32")",
571 : : dpif_name(dpif), port_no);
572 : : } else {
573 : 1 : log_operation(dpif, "port_del", error);
574 : : }
575 : 334 : return error;
576 : : }
577 : :
578 : : /* Makes a deep copy of 'src' into 'dst'. */
579 : : void
580 : 0 : dpif_port_clone(struct dpif_port *dst, const struct dpif_port *src)
581 : : {
582 : 0 : dst->name = xstrdup(src->name);
583 : 0 : dst->type = xstrdup(src->type);
584 : 0 : dst->port_no = src->port_no;
585 : 0 : }
586 : :
587 : : /* Frees memory allocated to members of 'dpif_port'.
588 : : *
589 : : * Do not call this function on a dpif_port obtained from
590 : : * dpif_port_dump_next(): that function retains ownership of the data in the
591 : : * dpif_port. */
592 : : void
593 : 7422 : dpif_port_destroy(struct dpif_port *dpif_port)
594 : : {
595 : 7422 : free(dpif_port->name);
596 : 7422 : free(dpif_port->type);
597 : 7422 : }
598 : :
599 : : /* Checks if port named 'devname' exists in 'dpif'. If so, returns
600 : : * true; otherwise, returns false. */
601 : : bool
602 : 2785 : dpif_port_exists(const struct dpif *dpif, const char *devname)
603 : : {
604 : 2785 : int error = dpif->dpif_class->port_query_by_name(dpif, devname, NULL);
605 [ + + ][ + + ]: 2785 : if (error != 0 && error != ENOENT && error != ENODEV) {
[ + + ]
606 [ + - ]: 12 : VLOG_WARN_RL(&error_rl, "%s: failed to query port %s: %s",
607 : : dpif_name(dpif), devname, ovs_strerror(error));
608 : : }
609 : :
610 : 2785 : return !error;
611 : : }
612 : :
613 : : /* Refreshes configuration of 'dpif's port. */
614 : : int
615 : 9306 : dpif_port_set_config(struct dpif *dpif, odp_port_t port_no,
616 : : const struct smap *cfg)
617 : : {
618 : 9306 : int error = 0;
619 : :
620 [ + + ]: 9306 : if (dpif->dpif_class->port_set_config) {
621 : 6669 : error = dpif->dpif_class->port_set_config(dpif, port_no, cfg);
622 [ - + ]: 6669 : if (error) {
623 : 0 : log_operation(dpif, "port_set_config", error);
624 : : }
625 : : }
626 : :
627 : 9306 : return error;
628 : : }
629 : :
630 : : /* Looks up port number 'port_no' in 'dpif'. On success, returns 0 and
631 : : * initializes '*port' appropriately; on failure, returns a positive errno
632 : : * value.
633 : : *
634 : : * The caller owns the data in 'port' and must free it with
635 : : * dpif_port_destroy() when it is no longer needed. */
636 : : int
637 : 5 : dpif_port_query_by_number(const struct dpif *dpif, odp_port_t port_no,
638 : : struct dpif_port *port)
639 : : {
640 : 5 : int error = dpif->dpif_class->port_query_by_number(dpif, port_no, port);
641 [ + - ]: 5 : if (!error) {
642 [ - + ]: 5 : VLOG_DBG_RL(&dpmsg_rl, "%s: port %"PRIu32" is device %s",
643 : : dpif_name(dpif), port_no, port->name);
644 : : } else {
645 : 0 : memset(port, 0, sizeof *port);
646 [ # # ]: 0 : VLOG_WARN_RL(&error_rl, "%s: failed to query port %"PRIu32": %s",
647 : : dpif_name(dpif), port_no, ovs_strerror(error));
648 : : }
649 : 5 : return error;
650 : : }
651 : :
652 : : /* Looks up port named 'devname' in 'dpif'. On success, returns 0 and
653 : : * initializes '*port' appropriately; on failure, returns a positive errno
654 : : * value.
655 : : *
656 : : * The caller owns the data in 'port' and must free it with
657 : : * dpif_port_destroy() when it is no longer needed. */
658 : : int
659 : 24338 : dpif_port_query_by_name(const struct dpif *dpif, const char *devname,
660 : : struct dpif_port *port)
661 : : {
662 : 24338 : int error = dpif->dpif_class->port_query_by_name(dpif, devname, port);
663 [ + + ]: 24338 : if (!error) {
664 [ + + ]: 22859 : VLOG_DBG_RL(&dpmsg_rl, "%s: device %s is on port %"PRIu32,
665 : : dpif_name(dpif), devname, port->port_no);
666 : : } else {
667 : 1479 : memset(port, 0, sizeof *port);
668 : :
669 : : /* For ENOENT or ENODEV we use DBG level because the caller is probably
670 : : * interested in whether 'dpif' actually has a port 'devname', so that
671 : : * it's not an issue worth logging if it doesn't. Other errors are
672 : : * uncommon and more likely to indicate a real problem. */
673 [ + + ][ + - ]: 1479 : VLOG_RL(&error_rl,
[ + + ]
674 : : error == ENOENT || error == ENODEV ? VLL_DBG : VLL_WARN,
675 : : "%s: failed to query port %s: %s",
676 : : dpif_name(dpif), devname, ovs_strerror(error));
677 : : }
678 : 24338 : return error;
679 : : }
680 : :
681 : : /* Returns the Netlink PID value to supply in OVS_ACTION_ATTR_USERSPACE
682 : : * actions as the OVS_USERSPACE_ATTR_PID attribute's value, for use in
683 : : * flows whose packets arrived on port 'port_no'. In the case where the
684 : : * provider allocates multiple Netlink PIDs to a single port, it may use
685 : : * 'hash' to spread load among them. The caller need not use a particular
686 : : * hash function; a 5-tuple hash is suitable.
687 : : *
688 : : * (The datapath implementation might use some different hash function for
689 : : * distributing packets received via flow misses among PIDs. This means
690 : : * that packets received via flow misses might be reordered relative to
691 : : * packets received via userspace actions. This is not ordinarily a
692 : : * problem.)
693 : : *
694 : : * A 'port_no' of ODPP_NONE is a special case: it returns a reserved PID, not
695 : : * allocated to any port, that the client may use for special purposes.
696 : : *
697 : : * The return value is only meaningful when DPIF_UC_ACTION has been enabled in
698 : : * the 'dpif''s listen mask. It is allowed to change when DPIF_UC_ACTION is
699 : : * disabled and then re-enabled, so a client that does that must be prepared to
700 : : * update all of the flows that it installed that contain
701 : : * OVS_ACTION_ATTR_USERSPACE actions. */
702 : : uint32_t
703 : 28310 : dpif_port_get_pid(const struct dpif *dpif, odp_port_t port_no, uint32_t hash)
704 : : {
705 : 28310 : return (dpif->dpif_class->port_get_pid
706 : 242 : ? (dpif->dpif_class->port_get_pid)(dpif, port_no, hash)
707 [ + + ]: 28310 : : 0);
708 : : }
709 : :
710 : : /* Looks up port number 'port_no' in 'dpif'. On success, returns 0 and copies
711 : : * the port's name into the 'name_size' bytes in 'name', ensuring that the
712 : : * result is null-terminated. On failure, returns a positive errno value and
713 : : * makes 'name' the empty string. */
714 : : int
715 : 0 : dpif_port_get_name(struct dpif *dpif, odp_port_t port_no,
716 : : char *name, size_t name_size)
717 : : {
718 : : struct dpif_port port;
719 : : int error;
720 : :
721 [ # # ]: 0 : ovs_assert(name_size > 0);
722 : :
723 : 0 : error = dpif_port_query_by_number(dpif, port_no, &port);
724 [ # # ]: 0 : if (!error) {
725 : 0 : ovs_strlcpy(name, port.name, name_size);
726 : 0 : dpif_port_destroy(&port);
727 : : } else {
728 : 0 : *name = '\0';
729 : : }
730 : 0 : return error;
731 : : }
732 : :
733 : : /* Initializes 'dump' to begin dumping the ports in a dpif.
734 : : *
735 : : * This function provides no status indication. An error status for the entire
736 : : * dump operation is provided when it is completed by calling
737 : : * dpif_port_dump_done().
738 : : */
739 : : void
740 : 2363 : dpif_port_dump_start(struct dpif_port_dump *dump, const struct dpif *dpif)
741 : : {
742 : 2363 : dump->dpif = dpif;
743 : 2363 : dump->error = dpif->dpif_class->port_dump_start(dpif, &dump->state);
744 : 2363 : log_operation(dpif, "port_dump_start", dump->error);
745 : 2363 : }
746 : :
747 : : /* Attempts to retrieve another port from 'dump', which must have been
748 : : * initialized with dpif_port_dump_start(). On success, stores a new dpif_port
749 : : * into 'port' and returns true. On failure, returns false.
750 : : *
751 : : * Failure might indicate an actual error or merely that the last port has been
752 : : * dumped. An error status for the entire dump operation is provided when it
753 : : * is completed by calling dpif_port_dump_done().
754 : : *
755 : : * The dpif owns the data stored in 'port'. It will remain valid until at
756 : : * least the next time 'dump' is passed to dpif_port_dump_next() or
757 : : * dpif_port_dump_done(). */
758 : : bool
759 : 10571 : dpif_port_dump_next(struct dpif_port_dump *dump, struct dpif_port *port)
760 : : {
761 : 10571 : const struct dpif *dpif = dump->dpif;
762 : :
763 [ - + ]: 10571 : if (dump->error) {
764 : 0 : return false;
765 : : }
766 : :
767 : 10571 : dump->error = dpif->dpif_class->port_dump_next(dpif, dump->state, port);
768 [ + + ]: 10571 : if (dump->error == EOF) {
769 [ + + ]: 2363 : VLOG_DBG_RL(&dpmsg_rl, "%s: dumped all ports", dpif_name(dpif));
770 : : } else {
771 : 8208 : log_operation(dpif, "port_dump_next", dump->error);
772 : : }
773 : :
774 [ + + ]: 10571 : if (dump->error) {
775 : 2363 : dpif->dpif_class->port_dump_done(dpif, dump->state);
776 : 2363 : return false;
777 : : }
778 : 8208 : return true;
779 : : }
780 : :
781 : : /* Completes port table dump operation 'dump', which must have been initialized
782 : : * with dpif_port_dump_start(). Returns 0 if the dump operation was
783 : : * error-free, otherwise a positive errno value describing the problem. */
784 : : int
785 : 2363 : dpif_port_dump_done(struct dpif_port_dump *dump)
786 : : {
787 : 2363 : const struct dpif *dpif = dump->dpif;
788 [ - + ]: 2363 : if (!dump->error) {
789 : 0 : dump->error = dpif->dpif_class->port_dump_done(dpif, dump->state);
790 : 0 : log_operation(dpif, "port_dump_done", dump->error);
791 : : }
792 [ - + ]: 2363 : return dump->error == EOF ? 0 : dump->error;
793 : : }
794 : :
795 : : /* Polls for changes in the set of ports in 'dpif'. If the set of ports in
796 : : * 'dpif' has changed, this function does one of the following:
797 : : *
798 : : * - Stores the name of the device that was added to or deleted from 'dpif' in
799 : : * '*devnamep' and returns 0. The caller is responsible for freeing
800 : : * '*devnamep' (with free()) when it no longer needs it.
801 : : *
802 : : * - Returns ENOBUFS and sets '*devnamep' to NULL.
803 : : *
804 : : * This function may also return 'false positives', where it returns 0 and
805 : : * '*devnamep' names a device that was not actually added or deleted or it
806 : : * returns ENOBUFS without any change.
807 : : *
808 : : * Returns EAGAIN if the set of ports in 'dpif' has not changed. May also
809 : : * return other positive errno values to indicate that something has gone
810 : : * wrong. */
811 : : int
812 : 107252 : dpif_port_poll(const struct dpif *dpif, char **devnamep)
813 : : {
814 : 107252 : int error = dpif->dpif_class->port_poll(dpif, devnamep);
815 [ + + ]: 107252 : if (error) {
816 : 107084 : *devnamep = NULL;
817 : : }
818 : 107252 : return error;
819 : : }
820 : :
821 : : /* Arranges for the poll loop to wake up when port_poll(dpif) will return a
822 : : * value other than EAGAIN. */
823 : : void
824 : 153645 : dpif_port_poll_wait(const struct dpif *dpif)
825 : : {
826 : 153645 : dpif->dpif_class->port_poll_wait(dpif);
827 : 153645 : }
828 : :
829 : : /* Extracts the flow stats for a packet. The 'flow' and 'packet'
830 : : * arguments must have been initialized through a call to flow_extract().
831 : : * 'used' is stored into stats->used. */
832 : : void
833 : 8061 : dpif_flow_stats_extract(const struct flow *flow, const struct dp_packet *packet,
834 : : long long int used, struct dpif_flow_stats *stats)
835 : : {
836 : 8061 : stats->tcp_flags = ntohs(flow->tcp_flags);
837 : 8061 : stats->n_bytes = dp_packet_size(packet);
838 : 8061 : stats->n_packets = 1;
839 : 8061 : stats->used = used;
840 : 8061 : }
841 : :
842 : : /* Appends a human-readable representation of 'stats' to 's'. */
843 : : void
844 : 1746 : dpif_flow_stats_format(const struct dpif_flow_stats *stats, struct ds *s)
845 : : {
846 : 1746 : ds_put_format(s, "packets:%"PRIu64", bytes:%"PRIu64", used:",
847 : : stats->n_packets, stats->n_bytes);
848 [ + + ]: 1746 : if (stats->used) {
849 : 157 : ds_put_format(s, "%.3fs", (time_msec() - stats->used) / 1000.0);
850 : : } else {
851 : 1589 : ds_put_format(s, "never");
852 : : }
853 [ + + ]: 1746 : if (stats->tcp_flags) {
854 : 54 : ds_put_cstr(s, ", flags:");
855 : 54 : packet_format_tcp_flags(s, stats->tcp_flags);
856 : : }
857 : 1746 : }
858 : :
859 : : /* Places the hash of the 'key_len' bytes starting at 'key' into '*hash'. */
860 : : void
861 : 21835 : dpif_flow_hash(const struct dpif *dpif OVS_UNUSED,
862 : : const void *key, size_t key_len, ovs_u128 *hash)
863 : : {
864 : : static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
865 : : static uint32_t secret;
866 : :
867 [ + + ]: 21835 : if (ovsthread_once_start(&once)) {
868 : 614 : secret = random_uint32();
869 : 614 : ovsthread_once_done(&once);
870 : : }
871 : 21835 : hash_bytes128(key, key_len, secret, hash);
872 : 21835 : uuid_set_bits_v4((struct uuid *)hash);
873 : 21835 : }
874 : :
875 : : /* Deletes all flows from 'dpif'. Returns 0 if successful, otherwise a
876 : : * positive errno value. */
877 : : int
878 : 1517 : dpif_flow_flush(struct dpif *dpif)
879 : : {
880 : : int error;
881 : :
882 : 1517 : COVERAGE_INC(dpif_flow_flush);
883 : :
884 : 1517 : error = dpif->dpif_class->flow_flush(dpif);
885 : 1517 : log_operation(dpif, "flow_flush", error);
886 : 1517 : return error;
887 : : }
888 : :
889 : : /* Attempts to install 'key' into the datapath, fetches it, then deletes it.
890 : : * Returns true if the datapath supported installing 'flow', false otherwise.
891 : : */
892 : : bool
893 : 6077 : dpif_probe_feature(struct dpif *dpif, const char *name,
894 : : const struct ofpbuf *key, const ovs_u128 *ufid)
895 : : {
896 : : struct dpif_flow flow;
897 : : struct ofpbuf reply;
898 : : uint64_t stub[DPIF_FLOW_BUFSIZE / 8];
899 : 6077 : bool enable_feature = false;
900 : : int error;
901 : :
902 : : /* Use DPIF_FP_MODIFY to cover the case where ovs-vswitchd is killed (and
903 : : * restarted) at just the right time such that feature probes from the
904 : : * previous run are still present in the datapath. */
905 : 6077 : error = dpif_flow_put(dpif, DPIF_FP_CREATE | DPIF_FP_MODIFY | DPIF_FP_PROBE,
906 : 12154 : key->data, key->size, NULL, 0, NULL, 0,
907 : : ufid, PMD_ID_NULL, NULL);
908 [ + + ]: 6077 : if (error) {
909 [ - + ]: 614 : if (error != EINVAL) {
910 [ # # ]: 0 : VLOG_WARN("%s: %s flow probe failed (%s)",
911 : : dpif_name(dpif), name, ovs_strerror(error));
912 : : }
913 : 614 : return false;
914 : : }
915 : :
916 : 5463 : ofpbuf_use_stack(&reply, &stub, sizeof stub);
917 : 5463 : error = dpif_flow_get(dpif, key->data, key->size, ufid,
918 : : PMD_ID_NULL, &reply, &flow);
919 [ + - ]: 5463 : if (!error
920 [ + + ][ + - ]: 5463 : && (!ufid || (flow.ufid_present
921 [ + - ]: 614 : && ovs_u128_equals(*ufid, flow.ufid)))) {
922 : 5463 : enable_feature = true;
923 : : }
924 : :
925 : 5463 : error = dpif_flow_del(dpif, key->data, key->size, ufid,
926 : : PMD_ID_NULL, NULL);
927 [ - + ]: 5463 : if (error) {
928 [ # # ]: 0 : VLOG_WARN("%s: failed to delete %s feature probe flow",
929 : : dpif_name(dpif), name);
930 : : }
931 : :
932 : 6077 : return enable_feature;
933 : : }
934 : :
935 : : /* A dpif_operate() wrapper for performing a single DPIF_OP_FLOW_GET. */
936 : : int
937 : 5528 : dpif_flow_get(struct dpif *dpif,
938 : : const struct nlattr *key, size_t key_len, const ovs_u128 *ufid,
939 : : const unsigned pmd_id, struct ofpbuf *buf, struct dpif_flow *flow)
940 : : {
941 : : struct dpif_op *opp;
942 : : struct dpif_op op;
943 : :
944 : 5528 : op.type = DPIF_OP_FLOW_GET;
945 : 5528 : op.u.flow_get.key = key;
946 : 5528 : op.u.flow_get.key_len = key_len;
947 : 5528 : op.u.flow_get.ufid = ufid;
948 : 5528 : op.u.flow_get.pmd_id = pmd_id;
949 : 5528 : op.u.flow_get.buffer = buf;
950 : :
951 : 5528 : memset(flow, 0, sizeof *flow);
952 : 5528 : op.u.flow_get.flow = flow;
953 : 5528 : op.u.flow_get.flow->key = key;
954 : 5528 : op.u.flow_get.flow->key_len = key_len;
955 : :
956 : 5528 : opp = &op;
957 : 5528 : dpif_operate(dpif, &opp, 1);
958 : :
959 : 5528 : return op.error;
960 : : }
961 : :
962 : : /* A dpif_operate() wrapper for performing a single DPIF_OP_FLOW_PUT. */
963 : : int
964 : 6140 : dpif_flow_put(struct dpif *dpif, enum dpif_flow_put_flags flags,
965 : : const struct nlattr *key, size_t key_len,
966 : : const struct nlattr *mask, size_t mask_len,
967 : : const struct nlattr *actions, size_t actions_len,
968 : : const ovs_u128 *ufid, const unsigned pmd_id,
969 : : struct dpif_flow_stats *stats)
970 : : {
971 : : struct dpif_op *opp;
972 : : struct dpif_op op;
973 : :
974 : 6140 : op.type = DPIF_OP_FLOW_PUT;
975 : 6140 : op.u.flow_put.flags = flags;
976 : 6140 : op.u.flow_put.key = key;
977 : 6140 : op.u.flow_put.key_len = key_len;
978 : 6140 : op.u.flow_put.mask = mask;
979 : 6140 : op.u.flow_put.mask_len = mask_len;
980 : 6140 : op.u.flow_put.actions = actions;
981 : 6140 : op.u.flow_put.actions_len = actions_len;
982 : 6140 : op.u.flow_put.ufid = ufid;
983 : 6140 : op.u.flow_put.pmd_id = pmd_id;
984 : 6140 : op.u.flow_put.stats = stats;
985 : :
986 : 6140 : opp = &op;
987 : 6140 : dpif_operate(dpif, &opp, 1);
988 : :
989 : 6140 : return op.error;
990 : : }
991 : :
992 : : /* A dpif_operate() wrapper for performing a single DPIF_OP_FLOW_DEL. */
993 : : int
994 : 5463 : dpif_flow_del(struct dpif *dpif,
995 : : const struct nlattr *key, size_t key_len, const ovs_u128 *ufid,
996 : : const unsigned pmd_id, struct dpif_flow_stats *stats)
997 : : {
998 : : struct dpif_op *opp;
999 : : struct dpif_op op;
1000 : :
1001 : 5463 : op.type = DPIF_OP_FLOW_DEL;
1002 : 5463 : op.u.flow_del.key = key;
1003 : 5463 : op.u.flow_del.key_len = key_len;
1004 : 5463 : op.u.flow_del.ufid = ufid;
1005 : 5463 : op.u.flow_del.pmd_id = pmd_id;
1006 : 5463 : op.u.flow_del.stats = stats;
1007 : 5463 : op.u.flow_del.terse = false;
1008 : :
1009 : 5463 : opp = &op;
1010 : 5463 : dpif_operate(dpif, &opp, 1);
1011 : :
1012 : 5463 : return op.error;
1013 : : }
1014 : :
1015 : : /* Creates and returns a new 'struct dpif_flow_dump' for iterating through the
1016 : : * flows in 'dpif'. If 'terse' is true, then only UFID and statistics will
1017 : : * be returned in the dump. Otherwise, all fields will be returned.
1018 : : *
1019 : : * This function always successfully returns a dpif_flow_dump. Error
1020 : : * reporting is deferred to dpif_flow_dump_destroy(). */
1021 : : struct dpif_flow_dump *
1022 : 28196 : dpif_flow_dump_create(const struct dpif *dpif, bool terse)
1023 : : {
1024 : 28196 : return dpif->dpif_class->flow_dump_create(dpif, terse);
1025 : : }
1026 : :
1027 : : /* Destroys 'dump', which must have been created with dpif_flow_dump_create().
1028 : : * All dpif_flow_dump_thread structures previously created for 'dump' must
1029 : : * previously have been destroyed.
1030 : : *
1031 : : * Returns 0 if the dump operation was error-free, otherwise a positive errno
1032 : : * value describing the problem. */
1033 : : int
1034 : 28196 : dpif_flow_dump_destroy(struct dpif_flow_dump *dump)
1035 : : {
1036 : 28196 : const struct dpif *dpif = dump->dpif;
1037 : 28196 : int error = dpif->dpif_class->flow_dump_destroy(dump);
1038 : 28196 : log_operation(dpif, "flow_dump_destroy", error);
1039 [ + - ]: 28196 : return error == EOF ? 0 : error;
1040 : : }
1041 : :
1042 : : /* Returns new thread-local state for use with dpif_flow_dump_next(). */
1043 : : struct dpif_flow_dump_thread *
1044 : 28199 : dpif_flow_dump_thread_create(struct dpif_flow_dump *dump)
1045 : : {
1046 : 28199 : return dump->dpif->dpif_class->flow_dump_thread_create(dump);
1047 : : }
1048 : :
1049 : : /* Releases 'thread'. */
1050 : : void
1051 : 28199 : dpif_flow_dump_thread_destroy(struct dpif_flow_dump_thread *thread)
1052 : : {
1053 : 28199 : thread->dpif->dpif_class->flow_dump_thread_destroy(thread);
1054 : 28199 : }
1055 : :
1056 : : /* Attempts to retrieve up to 'max_flows' more flows from 'thread'. Returns 0
1057 : : * if and only if no flows remained to be retrieved, otherwise a positive
1058 : : * number reflecting the number of elements in 'flows[]' that were updated.
1059 : : * The number of flows returned might be less than 'max_flows' because
1060 : : * fewer than 'max_flows' remained, because this particular datapath does not
1061 : : * benefit from batching, or because an error occurred partway through
1062 : : * retrieval. Thus, the caller should continue calling until a 0 return value,
1063 : : * even if intermediate return values are less than 'max_flows'.
1064 : : *
1065 : : * No error status is immediately provided. An error status for the entire
1066 : : * dump operation is provided when it is completed by calling
1067 : : * dpif_flow_dump_destroy().
1068 : : *
1069 : : * All of the data stored into 'flows' is owned by the datapath, not by the
1070 : : * caller, and the caller must not modify or free it. The datapath guarantees
1071 : : * that it remains accessible and unchanged until the first of:
1072 : : * - The next call to dpif_flow_dump_next() for 'thread', or
1073 : : * - The next rcu quiescent period. */
1074 : : int
1075 : 38662 : dpif_flow_dump_next(struct dpif_flow_dump_thread *thread,
1076 : : struct dpif_flow *flows, int max_flows)
1077 : : {
1078 : 38662 : struct dpif *dpif = thread->dpif;
1079 : : int n;
1080 : :
1081 [ - + ]: 38662 : ovs_assert(max_flows > 0);
1082 : 38662 : n = dpif->dpif_class->flow_dump_next(thread, flows, max_flows);
1083 [ + + ]: 38662 : if (n > 0) {
1084 : : struct dpif_flow *f;
1085 : :
1086 [ + + ][ + + ]: 11076 : for (f = flows; f < &flows[n] && should_log_flow_message(0); f++) {
1087 : 613 : log_flow_message(dpif, 0, "flow_dump",
1088 : : f->key, f->key_len, f->mask, f->mask_len,
1089 : 613 : &f->ufid, &f->stats, f->actions, f->actions_len);
1090 : : }
1091 : : } else {
1092 [ + + ]: 28199 : VLOG_DBG_RL(&dpmsg_rl, "%s: dumped all flows", dpif_name(dpif));
1093 : : }
1094 : 38662 : return n;
1095 : : }
1096 : :
1097 : : struct dpif_execute_helper_aux {
1098 : : struct dpif *dpif;
1099 : : const struct flow *flow;
1100 : : int error;
1101 : : };
1102 : :
1103 : : /* This is called for actions that need the context of the datapath to be
1104 : : * meaningful. */
1105 : : static void
1106 : 1327 : dpif_execute_helper_cb(void *aux_, struct dp_packet_batch *packets_,
1107 : : const struct nlattr *action, bool may_steal OVS_UNUSED)
1108 : : {
1109 : 1327 : struct dpif_execute_helper_aux *aux = aux_;
1110 : 1327 : int type = nl_attr_type(action);
1111 : 1327 : struct dp_packet *packet = packets_->packets[0];
1112 : 1327 : struct dp_packet *trunc_packet = NULL, *orig_packet;
1113 : :
1114 [ - + ]: 1327 : ovs_assert(packets_->count == 1);
1115 : :
1116 [ + - - ]: 1327 : switch ((enum ovs_action_attr)type) {
1117 : : case OVS_ACTION_ATTR_CT:
1118 : : case OVS_ACTION_ATTR_OUTPUT:
1119 : : case OVS_ACTION_ATTR_TUNNEL_PUSH:
1120 : : case OVS_ACTION_ATTR_TUNNEL_POP:
1121 : : case OVS_ACTION_ATTR_USERSPACE:
1122 : : case OVS_ACTION_ATTR_RECIRC: {
1123 : : struct dpif_execute execute;
1124 : : struct ofpbuf execute_actions;
1125 : : uint64_t stub[256 / 8];
1126 : 1327 : struct pkt_metadata *md = &packet->md;
1127 : 1327 : bool dst_set, clone = false;
1128 : 1327 : uint32_t cutlen = dp_packet_get_cutlen(packet);
1129 : :
1130 : 1327 : dst_set = flow_tnl_dst_is_set(&md->tunnel);
1131 [ + + ]: 1327 : if (dst_set) {
1132 : : /* The Linux kernel datapath throws away the tunnel information
1133 : : * that we supply as metadata. We have to use a "set" action to
1134 : : * supply it. */
1135 : 9 : ofpbuf_use_stub(&execute_actions, stub, sizeof stub);
1136 : 9 : odp_put_tunnel_action(&md->tunnel, &execute_actions);
1137 : 9 : ofpbuf_put(&execute_actions, action, NLA_ALIGN(action->nla_len));
1138 : :
1139 : 9 : execute.actions = execute_actions.data;
1140 : 9 : execute.actions_len = execute_actions.size;
1141 : : } else {
1142 : 1318 : execute.actions = action;
1143 : 1318 : execute.actions_len = NLA_ALIGN(action->nla_len);
1144 : : }
1145 : :
1146 : 1327 : orig_packet = packet;
1147 : :
1148 [ + + ][ - + ]: 1327 : if (cutlen > 0 && (type == OVS_ACTION_ATTR_OUTPUT ||
[ # # ]
1149 [ # # ]: 0 : type == OVS_ACTION_ATTR_TUNNEL_PUSH ||
1150 [ # # ]: 0 : type == OVS_ACTION_ATTR_TUNNEL_POP ||
1151 : : type == OVS_ACTION_ATTR_USERSPACE)) {
1152 [ + - ]: 7 : if (!may_steal) {
1153 : 7 : trunc_packet = dp_packet_clone(packet);
1154 : 7 : packet = trunc_packet;
1155 : 7 : clone = true;
1156 : : }
1157 : :
1158 : 7 : dp_packet_set_size(packet, dp_packet_size(packet) - cutlen);
1159 : 7 : dp_packet_reset_cutlen(orig_packet);
1160 : : }
1161 : :
1162 : 1327 : execute.packet = packet;
1163 : 1327 : execute.flow = aux->flow;
1164 : 1327 : execute.needs_help = false;
1165 : 1327 : execute.probe = false;
1166 : 1327 : execute.mtu = 0;
1167 : 1327 : aux->error = dpif_execute(aux->dpif, &execute);
1168 : 1327 : log_execute_message(aux->dpif, &execute, true, aux->error);
1169 : :
1170 [ + + ]: 1327 : if (dst_set) {
1171 : 9 : ofpbuf_uninit(&execute_actions);
1172 : : }
1173 : :
1174 [ + + ]: 1327 : if (clone) {
1175 : 7 : dp_packet_delete(trunc_packet);
1176 : : }
1177 : 1327 : break;
1178 : : }
1179 : :
1180 : : case OVS_ACTION_ATTR_HASH:
1181 : : case OVS_ACTION_ATTR_PUSH_VLAN:
1182 : : case OVS_ACTION_ATTR_POP_VLAN:
1183 : : case OVS_ACTION_ATTR_PUSH_MPLS:
1184 : : case OVS_ACTION_ATTR_POP_MPLS:
1185 : : case OVS_ACTION_ATTR_SET:
1186 : : case OVS_ACTION_ATTR_SET_MASKED:
1187 : : case OVS_ACTION_ATTR_SAMPLE:
1188 : : case OVS_ACTION_ATTR_TRUNC:
1189 : : case OVS_ACTION_ATTR_UNSPEC:
1190 : : case __OVS_ACTION_ATTR_MAX:
1191 : 0 : OVS_NOT_REACHED();
1192 : : }
1193 : 1327 : }
1194 : :
1195 : : /* Executes 'execute' by performing most of the actions in userspace and
1196 : : * passing the fully constructed packets to 'dpif' for output and userspace
1197 : : * actions.
1198 : : *
1199 : : * This helps with actions that a given 'dpif' doesn't implement directly. */
1200 : : static int
1201 : 252 : dpif_execute_with_help(struct dpif *dpif, struct dpif_execute *execute)
1202 : : {
1203 : 252 : struct dpif_execute_helper_aux aux = {dpif, execute->flow, 0};
1204 : : struct dp_packet_batch pb;
1205 : :
1206 : 252 : COVERAGE_INC(dpif_execute_with_help);
1207 : :
1208 : 252 : packet_batch_init_packet(&pb, execute->packet);
1209 : 252 : odp_execute_actions(&aux, &pb, false, execute->actions,
1210 : : execute->actions_len, dpif_execute_helper_cb);
1211 : 252 : return aux.error;
1212 : : }
1213 : :
1214 : : /* Returns true if the datapath needs help executing 'execute'. */
1215 : : static bool
1216 : 5129 : dpif_execute_needs_help(const struct dpif_execute *execute)
1217 : : {
1218 [ + + ][ - + ]: 5129 : return execute->needs_help || nl_attr_oversized(execute->actions_len);
1219 : : }
1220 : :
1221 : : /* A dpif_operate() wrapper for performing a single DPIF_OP_EXECUTE. */
1222 : : int
1223 : 11429 : dpif_execute(struct dpif *dpif, struct dpif_execute *execute)
1224 : : {
1225 [ + + ]: 11429 : if (execute->actions_len) {
1226 : : struct dpif_op *opp;
1227 : : struct dpif_op op;
1228 : :
1229 : 4096 : op.type = DPIF_OP_EXECUTE;
1230 : 4096 : op.u.execute = *execute;
1231 : :
1232 : 4096 : opp = &op;
1233 : 4096 : dpif_operate(dpif, &opp, 1);
1234 : :
1235 : 4096 : return op.error;
1236 : : } else {
1237 : 7333 : return 0;
1238 : : }
1239 : : }
1240 : :
1241 : : /* Executes each of the 'n_ops' operations in 'ops' on 'dpif', in the order in
1242 : : * which they are specified. Places each operation's results in the "output"
1243 : : * members documented in comments, and 0 in the 'error' member on success or a
1244 : : * positive errno on failure. */
1245 : : void
1246 : 26872 : dpif_operate(struct dpif *dpif, struct dpif_op **ops, size_t n_ops)
1247 : : {
1248 [ + + ]: 53773 : while (n_ops > 0) {
1249 : : size_t chunk;
1250 : :
1251 : : /* Count 'chunk', the number of ops that can be executed without
1252 : : * needing any help. Ops that need help should be rare, so we
1253 : : * expect this to ordinarily be 'n_ops', that is, all the ops. */
1254 [ + + ]: 57277 : for (chunk = 0; chunk < n_ops; chunk++) {
1255 : 30662 : struct dpif_op *op = ops[chunk];
1256 : :
1257 [ + + ]: 30662 : if (op->type == DPIF_OP_EXECUTE
1258 [ + + ]: 5127 : && dpif_execute_needs_help(&op->u.execute)) {
1259 : 286 : break;
1260 : : }
1261 : : }
1262 : :
1263 [ + + ]: 26901 : if (chunk) {
1264 : : /* Execute a chunk full of ops that the dpif provider can
1265 : : * handle itself, without help. */
1266 : : size_t i;
1267 : :
1268 : 26649 : dpif->dpif_class->operate(dpif, ops, chunk);
1269 : :
1270 [ + + ]: 57025 : for (i = 0; i < chunk; i++) {
1271 : 30376 : struct dpif_op *op = ops[i];
1272 : 30376 : int error = op->error;
1273 : :
1274 [ + + + + : 30376 : switch (op->type) {
- ]
1275 : : case DPIF_OP_FLOW_PUT: {
1276 : 7853 : struct dpif_flow_put *put = &op->u.flow_put;
1277 : :
1278 : 7853 : COVERAGE_INC(dpif_flow_put);
1279 : 7853 : log_flow_put_message(dpif, put, error);
1280 [ + + ][ - + ]: 7853 : if (error && put->stats) {
1281 : 0 : memset(put->stats, 0, sizeof *put->stats);
1282 : : }
1283 : 7853 : break;
1284 : : }
1285 : :
1286 : : case DPIF_OP_FLOW_GET: {
1287 : 5528 : struct dpif_flow_get *get = &op->u.flow_get;
1288 : :
1289 : 5528 : COVERAGE_INC(dpif_flow_get);
1290 [ - + ]: 5528 : if (error) {
1291 : 0 : memset(get->flow, 0, sizeof *get->flow);
1292 : : }
1293 : 5528 : log_flow_get_message(dpif, get, error);
1294 : :
1295 : 5528 : break;
1296 : : }
1297 : :
1298 : : case DPIF_OP_FLOW_DEL: {
1299 : 12154 : struct dpif_flow_del *del = &op->u.flow_del;
1300 : :
1301 : 12154 : COVERAGE_INC(dpif_flow_del);
1302 : 12154 : log_flow_del_message(dpif, del, error);
1303 [ - + ][ # # ]: 12154 : if (error && del->stats) {
1304 : 0 : memset(del->stats, 0, sizeof *del->stats);
1305 : : }
1306 : 12154 : break;
1307 : : }
1308 : :
1309 : : case DPIF_OP_EXECUTE:
1310 : 4841 : COVERAGE_INC(dpif_execute);
1311 : 4841 : log_execute_message(dpif, &op->u.execute, false, error);
1312 : 4841 : break;
1313 : : }
1314 : : }
1315 : :
1316 : 26649 : ops += chunk;
1317 : 26649 : n_ops -= chunk;
1318 : : } else {
1319 : : /* Help the dpif provider to execute one op. */
1320 : 252 : struct dpif_op *op = ops[0];
1321 : :
1322 : 252 : COVERAGE_INC(dpif_execute);
1323 : 252 : op->error = dpif_execute_with_help(dpif, &op->u.execute);
1324 : 252 : ops++;
1325 : 252 : n_ops--;
1326 : : }
1327 : : }
1328 : 26872 : }
1329 : :
1330 : : /* Returns a string that represents 'type', for use in log messages. */
1331 : : const char *
1332 : 204 : dpif_upcall_type_to_string(enum dpif_upcall_type type)
1333 : : {
1334 [ + + - ]: 204 : switch (type) {
1335 : 197 : case DPIF_UC_MISS: return "miss";
1336 : 7 : case DPIF_UC_ACTION: return "action";
1337 : 0 : case DPIF_N_UC_TYPES: default: return "<unknown>";
1338 : : }
1339 : : }
1340 : :
1341 : : /* Enables or disables receiving packets with dpif_recv() on 'dpif'. Returns 0
1342 : : * if successful, otherwise a positive errno value.
1343 : : *
1344 : : * Turning packet receive off and then back on may change the Netlink PID
1345 : : * assignments returned by dpif_port_get_pid(). If the client does this, it
1346 : : * must update all of the flows that have OVS_ACTION_ATTR_USERSPACE actions
1347 : : * using the new PID assignment. */
1348 : : int
1349 : 615 : dpif_recv_set(struct dpif *dpif, bool enable)
1350 : : {
1351 : 615 : int error = 0;
1352 : :
1353 [ + + ]: 615 : if (dpif->dpif_class->recv_set) {
1354 : 63 : error = dpif->dpif_class->recv_set(dpif, enable);
1355 : 63 : log_operation(dpif, "recv_set", error);
1356 : : }
1357 : 615 : return error;
1358 : : }
1359 : :
1360 : : /* Refreshes the poll loops and Netlink sockets associated to each port,
1361 : : * when the number of upcall handlers (upcall receiving thread) is changed
1362 : : * to 'n_handlers' and receiving packets for 'dpif' is enabled by
1363 : : * recv_set().
1364 : : *
1365 : : * Since multiple upcall handlers can read upcalls simultaneously from
1366 : : * 'dpif', each port can have multiple Netlink sockets, one per upcall
1367 : : * handler. So, handlers_set() is responsible for the following tasks:
1368 : : *
1369 : : * When receiving upcall is enabled, extends or creates the
1370 : : * configuration to support:
1371 : : *
1372 : : * - 'n_handlers' Netlink sockets for each port.
1373 : : *
1374 : : * - 'n_handlers' poll loops, one for each upcall handler.
1375 : : *
1376 : : * - registering the Netlink sockets for the same upcall handler to
1377 : : * the corresponding poll loop.
1378 : : *
1379 : : * Returns 0 if successful, otherwise a positive errno value. */
1380 : : int
1381 : 615 : dpif_handlers_set(struct dpif *dpif, uint32_t n_handlers)
1382 : : {
1383 : 615 : int error = 0;
1384 : :
1385 [ + + ]: 615 : if (dpif->dpif_class->handlers_set) {
1386 : 63 : error = dpif->dpif_class->handlers_set(dpif, n_handlers);
1387 : 63 : log_operation(dpif, "handlers_set", error);
1388 : : }
1389 : 615 : return error;
1390 : : }
1391 : :
1392 : : void
1393 : 1228 : dpif_register_dp_purge_cb(struct dpif *dpif, dp_purge_callback *cb, void *aux)
1394 : : {
1395 [ + + ]: 1228 : if (dpif->dpif_class->register_dp_purge_cb) {
1396 : 1102 : dpif->dpif_class->register_dp_purge_cb(dpif, cb, aux);
1397 : : }
1398 : 1228 : }
1399 : :
1400 : : void
1401 : 1228 : dpif_register_upcall_cb(struct dpif *dpif, upcall_callback *cb, void *aux)
1402 : : {
1403 [ + + ]: 1228 : if (dpif->dpif_class->register_upcall_cb) {
1404 : 1102 : dpif->dpif_class->register_upcall_cb(dpif, cb, aux);
1405 : : }
1406 : 1228 : }
1407 : :
1408 : : void
1409 : 2266 : dpif_enable_upcall(struct dpif *dpif)
1410 : : {
1411 [ + + ]: 2266 : if (dpif->dpif_class->enable_upcall) {
1412 : 2055 : dpif->dpif_class->enable_upcall(dpif);
1413 : : }
1414 : 2266 : }
1415 : :
1416 : : void
1417 : 2266 : dpif_disable_upcall(struct dpif *dpif)
1418 : : {
1419 [ + + ]: 2266 : if (dpif->dpif_class->disable_upcall) {
1420 : 2055 : dpif->dpif_class->disable_upcall(dpif);
1421 : : }
1422 : 2266 : }
1423 : :
1424 : : void
1425 : 1374 : dpif_print_packet(struct dpif *dpif, struct dpif_upcall *upcall)
1426 : : {
1427 [ - + ]: 1374 : if (!VLOG_DROP_DBG(&dpmsg_rl)) {
1428 : : struct ds flow;
1429 : : char *packet;
1430 : :
1431 : 0 : packet = ofp_packet_to_string(dp_packet_data(&upcall->packet),
1432 : 0 : dp_packet_size(&upcall->packet));
1433 : :
1434 : 0 : ds_init(&flow);
1435 : 0 : odp_flow_key_format(upcall->key, upcall->key_len, &flow);
1436 : :
1437 [ # # ]: 0 : VLOG_DBG("%s: %s upcall:\n%s\n%s",
1438 : : dpif_name(dpif), dpif_upcall_type_to_string(upcall->type),
1439 : : ds_cstr(&flow), packet);
1440 : :
1441 : 0 : ds_destroy(&flow);
1442 : 0 : free(packet);
1443 : : }
1444 : 1374 : }
1445 : :
1446 : : /* If 'dpif' creates its own I/O polling threads, refreshes poll threads
1447 : : * configuration. */
1448 : : int
1449 : 105699 : dpif_poll_threads_set(struct dpif *dpif, const char *cmask)
1450 : : {
1451 : 105699 : int error = 0;
1452 : :
1453 [ + + ]: 105699 : if (dpif->dpif_class->poll_threads_set) {
1454 : 99708 : error = dpif->dpif_class->poll_threads_set(dpif, cmask);
1455 [ - + ]: 99708 : if (error) {
1456 : 0 : log_operation(dpif, "poll_threads_set", error);
1457 : : }
1458 : : }
1459 : :
1460 : 105699 : return error;
1461 : : }
1462 : :
1463 : : /* Polls for an upcall from 'dpif' for an upcall handler. Since there
1464 : : * there can be multiple poll loops, 'handler_id' is needed as index to
1465 : : * identify the corresponding poll loop. If successful, stores the upcall
1466 : : * into '*upcall', using 'buf' for storage. Should only be called if
1467 : : * 'recv_set' has been used to enable receiving packets from 'dpif'.
1468 : : *
1469 : : * 'upcall->key' and 'upcall->userdata' point into data in the caller-provided
1470 : : * 'buf', so their memory cannot be freed separately from 'buf'.
1471 : : *
1472 : : * The caller owns the data of 'upcall->packet' and may modify it. If
1473 : : * packet's headroom is exhausted as it is manipulated, 'upcall->packet'
1474 : : * will be reallocated. This requires the data of 'upcall->packet' to be
1475 : : * released with ofpbuf_uninit() before 'upcall' is destroyed. However,
1476 : : * when an error is returned, the 'upcall->packet' may be uninitialized
1477 : : * and should not be released.
1478 : : *
1479 : : * Returns 0 if successful, otherwise a positive errno value. Returns EAGAIN
1480 : : * if no upcall is immediately available. */
1481 : : int
1482 : 9947 : dpif_recv(struct dpif *dpif, uint32_t handler_id, struct dpif_upcall *upcall,
1483 : : struct ofpbuf *buf)
1484 : : {
1485 : 9947 : int error = EAGAIN;
1486 : :
1487 [ + + ]: 9947 : if (dpif->dpif_class->recv) {
1488 : 3449 : error = dpif->dpif_class->recv(dpif, handler_id, upcall, buf);
1489 [ + + ]: 3449 : if (!error) {
1490 : 1374 : dpif_print_packet(dpif, upcall);
1491 [ - + ]: 2075 : } else if (error != EAGAIN) {
1492 : 0 : log_operation(dpif, "recv", error);
1493 : : }
1494 : : }
1495 : 9947 : return error;
1496 : : }
1497 : :
1498 : : /* Discards all messages that would otherwise be received by dpif_recv() on
1499 : : * 'dpif'. */
1500 : : void
1501 : 0 : dpif_recv_purge(struct dpif *dpif)
1502 : : {
1503 : 0 : COVERAGE_INC(dpif_purge);
1504 [ # # ]: 0 : if (dpif->dpif_class->recv_purge) {
1505 : 0 : dpif->dpif_class->recv_purge(dpif);
1506 : : }
1507 : 0 : }
1508 : :
1509 : : /* Arranges for the poll loop for an upcall handler to wake up when 'dpif'
1510 : : * 'dpif' has a message queued to be received with the recv member
1511 : : * function. Since there can be multiple poll loops, 'handler_id' is
1512 : : * needed as index to identify the corresponding poll loop. */
1513 : : void
1514 : 7383 : dpif_recv_wait(struct dpif *dpif, uint32_t handler_id)
1515 : : {
1516 [ + + ]: 7383 : if (dpif->dpif_class->recv_wait) {
1517 : 885 : dpif->dpif_class->recv_wait(dpif, handler_id);
1518 : : }
1519 : 7383 : }
1520 : :
1521 : : /*
1522 : : * Return the datapath version. Caller is responsible for freeing
1523 : : * the string.
1524 : : */
1525 : : char *
1526 : 614 : dpif_get_dp_version(const struct dpif *dpif)
1527 : : {
1528 : 614 : char *version = NULL;
1529 : :
1530 [ + - ]: 614 : if (dpif->dpif_class->get_datapath_version) {
1531 : 614 : version = dpif->dpif_class->get_datapath_version();
1532 : : }
1533 : :
1534 : 614 : return version;
1535 : : }
1536 : :
1537 : : /* Obtains the NetFlow engine type and engine ID for 'dpif' into '*engine_type'
1538 : : * and '*engine_id', respectively. */
1539 : : void
1540 : 6 : dpif_get_netflow_ids(const struct dpif *dpif,
1541 : : uint8_t *engine_type, uint8_t *engine_id)
1542 : : {
1543 : 6 : *engine_type = dpif->netflow_engine_type;
1544 : 6 : *engine_id = dpif->netflow_engine_id;
1545 : 6 : }
1546 : :
1547 : : /* Translates OpenFlow queue ID 'queue_id' (in host byte order) into a priority
1548 : : * value used for setting packet priority.
1549 : : * On success, returns 0 and stores the priority into '*priority'.
1550 : : * On failure, returns a positive errno value and stores 0 into '*priority'. */
1551 : : int
1552 : 14 : dpif_queue_to_priority(const struct dpif *dpif, uint32_t queue_id,
1553 : : uint32_t *priority)
1554 : : {
1555 : 28 : int error = (dpif->dpif_class->queue_to_priority
1556 : 14 : ? dpif->dpif_class->queue_to_priority(dpif, queue_id,
1557 : : priority)
1558 [ + - ]: 14 : : EOPNOTSUPP);
1559 [ - + ]: 14 : if (error) {
1560 : 0 : *priority = 0;
1561 : : }
1562 : 14 : log_operation(dpif, "queue_to_priority", error);
1563 : 14 : return error;
1564 : : }
1565 : :
1566 : : void
1567 : 798 : dpif_init(struct dpif *dpif, const struct dpif_class *dpif_class,
1568 : : const char *name,
1569 : : uint8_t netflow_engine_type, uint8_t netflow_engine_id)
1570 : : {
1571 : 798 : dpif->dpif_class = dpif_class;
1572 : 798 : dpif->base_name = xstrdup(name);
1573 : 798 : dpif->full_name = xasprintf("%s@%s", dpif_class->type, name);
1574 : 798 : dpif->netflow_engine_type = netflow_engine_type;
1575 : 798 : dpif->netflow_engine_id = netflow_engine_id;
1576 : 798 : }
1577 : :
1578 : : /* Undoes the results of initialization.
1579 : : *
1580 : : * Normally this function only needs to be called from dpif_close().
1581 : : * However, it may be called by providers due to an error on opening
1582 : : * that occurs after initialization. It this case dpif_close() would
1583 : : * never be called. */
1584 : : void
1585 : 798 : dpif_uninit(struct dpif *dpif, bool close)
1586 : : {
1587 : 798 : char *base_name = dpif->base_name;
1588 : 798 : char *full_name = dpif->full_name;
1589 : :
1590 [ + - ]: 798 : if (close) {
1591 : 798 : dpif->dpif_class->close(dpif);
1592 : : }
1593 : :
1594 : 798 : free(base_name);
1595 : 798 : free(full_name);
1596 : 798 : }
1597 : :
1598 : : static void
1599 : 52825 : log_operation(const struct dpif *dpif, const char *operation, int error)
1600 : : {
1601 [ + + ]: 52825 : if (!error) {
1602 [ + + ]: 52824 : VLOG_DBG_RL(&dpmsg_rl, "%s: %s success", dpif_name(dpif), operation);
1603 [ - + ]: 1 : } else if (ofperr_is_valid(error)) {
1604 [ # # ]: 0 : VLOG_WARN_RL(&error_rl, "%s: %s failed (%s)",
1605 : : dpif_name(dpif), operation, ofperr_get_name(error));
1606 : : } else {
1607 [ + - ]: 1 : VLOG_WARN_RL(&error_rl, "%s: %s failed (%s)",
1608 : : dpif_name(dpif), operation, ovs_strerror(error));
1609 : : }
1610 : 52825 : }
1611 : :
1612 : : static enum vlog_level
1613 : 37080 : flow_message_log_level(int error)
1614 : : {
1615 : : /* If flows arrive in a batch, userspace may push down multiple
1616 : : * unique flow definitions that overlap when wildcards are applied.
1617 : : * Kernels that support flow wildcarding will reject these flows as
1618 : : * duplicates (EEXIST), so lower the log level to debug for these
1619 : : * types of messages. */
1620 [ + + ][ + + ]: 37080 : return (error && error != EEXIST) ? VLL_WARN : VLL_DBG;
1621 : : }
1622 : :
1623 : : static bool
1624 : 36321 : should_log_flow_message(int error)
1625 : : {
1626 [ + + ]: 36321 : return !vlog_should_drop(&this_module, flow_message_log_level(error),
1627 : 36321 : error ? &error_rl : &dpmsg_rl);
1628 : : }
1629 : :
1630 : : static void
1631 : 759 : log_flow_message(const struct dpif *dpif, int error, const char *operation,
1632 : : const struct nlattr *key, size_t key_len,
1633 : : const struct nlattr *mask, size_t mask_len,
1634 : : const ovs_u128 *ufid, const struct dpif_flow_stats *stats,
1635 : : const struct nlattr *actions, size_t actions_len)
1636 : : {
1637 : 759 : struct ds ds = DS_EMPTY_INITIALIZER;
1638 : 759 : ds_put_format(&ds, "%s: ", dpif_name(dpif));
1639 [ + + ]: 759 : if (error) {
1640 : 5 : ds_put_cstr(&ds, "failed to ");
1641 : : }
1642 : 759 : ds_put_format(&ds, "%s ", operation);
1643 [ + + ]: 759 : if (error) {
1644 : 5 : ds_put_format(&ds, "(%s) ", ovs_strerror(error));
1645 : : }
1646 [ + - ]: 759 : if (ufid) {
1647 : 759 : odp_format_ufid(ufid, &ds);
1648 : 759 : ds_put_cstr(&ds, " ");
1649 : : }
1650 : 759 : odp_flow_format(key, key_len, mask, mask_len, NULL, &ds, true);
1651 [ + + ]: 759 : if (stats) {
1652 : 747 : ds_put_cstr(&ds, ", ");
1653 : 747 : dpif_flow_stats_format(stats, &ds);
1654 : : }
1655 [ + + ][ - + ]: 759 : if (actions || actions_len) {
1656 : 84 : ds_put_cstr(&ds, ", actions:");
1657 : 84 : format_odp_actions(&ds, actions, actions_len);
1658 : : }
1659 : 759 : vlog(&this_module, flow_message_log_level(error), "%s", ds_cstr(&ds));
1660 : 759 : ds_destroy(&ds);
1661 : 759 : }
1662 : :
1663 : : static void
1664 : 7853 : log_flow_put_message(struct dpif *dpif, const struct dpif_flow_put *put,
1665 : : int error)
1666 : : {
1667 [ + + ][ + + ]: 7853 : if (should_log_flow_message(error) && !(put->flags & DPIF_FP_PROBE)) {
1668 : : struct ds s;
1669 : :
1670 : 12 : ds_init(&s);
1671 : 12 : ds_put_cstr(&s, "put");
1672 [ - + ]: 12 : if (put->flags & DPIF_FP_CREATE) {
1673 : 0 : ds_put_cstr(&s, "[create]");
1674 : : }
1675 [ + - ]: 12 : if (put->flags & DPIF_FP_MODIFY) {
1676 : 12 : ds_put_cstr(&s, "[modify]");
1677 : : }
1678 [ - + ]: 12 : if (put->flags & DPIF_FP_ZERO_STATS) {
1679 : 0 : ds_put_cstr(&s, "[zero]");
1680 : : }
1681 : 12 : log_flow_message(dpif, error, ds_cstr(&s),
1682 : : put->key, put->key_len, put->mask, put->mask_len,
1683 : 12 : put->ufid, put->stats, put->actions,
1684 : : put->actions_len);
1685 : 12 : ds_destroy(&s);
1686 : : }
1687 : 7853 : }
1688 : :
1689 : : static void
1690 : 12154 : log_flow_del_message(struct dpif *dpif, const struct dpif_flow_del *del,
1691 : : int error)
1692 : : {
1693 [ + + ]: 12154 : if (should_log_flow_message(error)) {
1694 [ + - ]: 134 : log_flow_message(dpif, error, "flow_del", del->key, del->key_len,
1695 : : NULL, 0, del->ufid, !error ? del->stats : NULL,
1696 : : NULL, 0);
1697 : : }
1698 : 12154 : }
1699 : :
1700 : : /* Logs that 'execute' was executed on 'dpif' and completed with errno 'error'
1701 : : * (0 for success). 'subexecute' should be true if the execution is a result
1702 : : * of breaking down a larger execution that needed help, false otherwise.
1703 : : *
1704 : : *
1705 : : * XXX In theory, the log message could be deceptive because this function is
1706 : : * called after the dpif_provider's '->execute' function, which is allowed to
1707 : : * modify execute->packet and execute->md. In practice, though:
1708 : : *
1709 : : * - dpif-netlink doesn't modify execute->packet or execute->md.
1710 : : *
1711 : : * - dpif-netdev does modify them but it is less likely to have problems
1712 : : * because it is built into ovs-vswitchd and cannot have version skew,
1713 : : * etc.
1714 : : *
1715 : : * It would still be better to avoid the potential problem. I don't know of a
1716 : : * good way to do that, though, that isn't expensive. */
1717 : : static void
1718 : 6168 : log_execute_message(struct dpif *dpif, const struct dpif_execute *execute,
1719 : : bool subexecute, int error)
1720 : : {
1721 [ + + ]: 12336 : if (!(error ? VLOG_DROP_WARN(&error_rl) : VLOG_DROP_DBG(&dpmsg_rl))
1722 [ - + ][ + - ]: 12338 : && !execute->probe) {
1723 : 2 : struct ds ds = DS_EMPTY_INITIALIZER;
1724 : : char *packet;
1725 : :
1726 : 2 : packet = ofp_packet_to_string(dp_packet_data(execute->packet),
1727 : 2 : dp_packet_size(execute->packet));
1728 [ - + ]: 4 : ds_put_format(&ds, "%s: %sexecute ",
1729 : : dpif_name(dpif),
1730 : : (subexecute ? "sub-"
1731 : 2 : : dpif_execute_needs_help(execute) ? "super-"
1732 [ - + ]: 2 : : ""));
1733 : 2 : format_odp_actions(&ds, execute->actions, execute->actions_len);
1734 [ - + ]: 2 : if (error) {
1735 : 0 : ds_put_format(&ds, " failed (%s)", ovs_strerror(error));
1736 : : }
1737 : 2 : ds_put_format(&ds, " on packet %s", packet);
1738 : 2 : ds_put_format(&ds, " mtu %d", execute->mtu);
1739 [ - + ]: 2 : vlog(&this_module, error ? VLL_WARN : VLL_DBG, "%s", ds_cstr(&ds));
1740 : 2 : ds_destroy(&ds);
1741 : 2 : free(packet);
1742 : : }
1743 : 6168 : }
1744 : :
1745 : : static void
1746 : 5528 : log_flow_get_message(const struct dpif *dpif, const struct dpif_flow_get *get,
1747 : : int error)
1748 : : {
1749 [ - + ]: 5528 : if (should_log_flow_message(error)) {
1750 : 0 : log_flow_message(dpif, error, "flow_get",
1751 : : get->key, get->key_len,
1752 : 0 : get->flow->mask, get->flow->mask_len,
1753 : 0 : get->ufid, &get->flow->stats,
1754 : 0 : get->flow->actions, get->flow->actions_len);
1755 : : }
1756 : 5528 : }
1757 : :
1758 : : bool
1759 : 614 : dpif_supports_tnl_push_pop(const struct dpif *dpif)
1760 : : {
1761 : 614 : return dpif_is_netdev(dpif);
1762 : : }
|