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