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