]> git.proxmox.com Git - ovs.git/blame - lib/dpif.c
dpif: Factor 'type' and 'error' out of individual dpif_op members.
[ovs.git] / lib / dpif.c
CommitLineData
064af421 1/*
4c738a8d 2 * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
064af421 3 *
a14bc59f
BP
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
064af421 7 *
a14bc59f
BP
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
064af421
BP
15 */
16
17#include <config.h>
96fba48f 18#include "dpif-provider.h"
064af421
BP
19
20#include <assert.h>
21#include <ctype.h>
22#include <errno.h>
064af421 23#include <inttypes.h>
064af421
BP
24#include <stdlib.h>
25#include <string.h>
064af421
BP
26
27#include "coverage.h"
28#include "dynamic-string.h"
29#include "flow.h"
c3827f61 30#include "netdev.h"
064af421
BP
31#include "netlink.h"
32#include "odp-util.h"
90bf1e07 33#include "ofp-errors.h"
064af421 34#include "ofp-print.h"
71ce9235 35#include "ofp-util.h"
064af421
BP
36#include "ofpbuf.h"
37#include "packets.h"
38#include "poll-loop.h"
999401aa 39#include "shash.h"
d0c23a1a 40#include "sset.h"
c97fb132 41#include "timeval.h"
064af421
BP
42#include "util.h"
43#include "valgrind.h"
064af421 44#include "vlog.h"
5136ce49 45
d98e6007 46VLOG_DEFINE_THIS_MODULE(dpif);
064af421 47
d76f09ea
BP
48COVERAGE_DEFINE(dpif_destroy);
49COVERAGE_DEFINE(dpif_port_add);
50COVERAGE_DEFINE(dpif_port_del);
51COVERAGE_DEFINE(dpif_flow_flush);
52COVERAGE_DEFINE(dpif_flow_get);
53COVERAGE_DEFINE(dpif_flow_put);
54COVERAGE_DEFINE(dpif_flow_del);
55COVERAGE_DEFINE(dpif_flow_query_list);
56COVERAGE_DEFINE(dpif_flow_query_list_n);
57COVERAGE_DEFINE(dpif_execute);
58COVERAGE_DEFINE(dpif_purge);
59
999401aa 60static const struct dpif_class *base_dpif_classes[] = {
c83cdd30 61#ifdef HAVE_NETLINK
96fba48f 62 &dpif_linux_class,
c83cdd30 63#endif
72865317 64 &dpif_netdev_class,
c228a364 65};
999401aa
JG
66
67struct registered_dpif_class {
d2d8fbeb 68 const struct dpif_class *dpif_class;
999401aa
JG
69 int refcount;
70};
71static struct shash dpif_classes = SHASH_INITIALIZER(&dpif_classes);
579a77e0 72static struct sset dpif_blacklist = SSET_INITIALIZER(&dpif_blacklist);
c228a364 73
064af421
BP
74/* Rate limit for individual messages going to or from the datapath, output at
75 * DBG level. This is very high because, if these are enabled, it is because
76 * we really need to see them. */
77static struct vlog_rate_limit dpmsg_rl = VLOG_RATE_LIMIT_INIT(600, 600);
78
79/* Not really much point in logging many dpif errors. */
e2781405 80static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(60, 5);
064af421 81
feebdea2
BP
82static void log_flow_message(const struct dpif *dpif, int error,
83 const char *operation,
84 const struct nlattr *key, size_t key_len,
c97fb132 85 const struct dpif_flow_stats *stats,
feebdea2 86 const struct nlattr *actions, size_t actions_len);
96fba48f
BP
87static void log_operation(const struct dpif *, const char *operation,
88 int error);
96fba48f 89static bool should_log_flow_message(int error);
064af421 90
999401aa
JG
91static void
92dp_initialize(void)
93{
94 static int status = -1;
95
96 if (status < 0) {
97 int i;
98
99 status = 0;
100 for (i = 0; i < ARRAY_SIZE(base_dpif_classes); i++) {
101 dp_register_provider(base_dpif_classes[i]);
102 }
103 }
104}
105
999401aa
JG
106/* Registers a new datapath provider. After successful registration, new
107 * datapaths of that type can be opened using dpif_open(). */
108int
109dp_register_provider(const struct dpif_class *new_class)
110{
111 struct registered_dpif_class *registered_class;
112
579a77e0
EJ
113 if (sset_contains(&dpif_blacklist, new_class->type)) {
114 VLOG_DBG("attempted to register blacklisted provider: %s",
115 new_class->type);
116 return EINVAL;
117 }
118
999401aa
JG
119 if (shash_find(&dpif_classes, new_class->type)) {
120 VLOG_WARN("attempted to register duplicate datapath provider: %s",
121 new_class->type);
122 return EEXIST;
123 }
1a6f1e2a 124
999401aa 125 registered_class = xmalloc(sizeof *registered_class);
d2d8fbeb 126 registered_class->dpif_class = new_class;
999401aa
JG
127 registered_class->refcount = 0;
128
129 shash_add(&dpif_classes, new_class->type, registered_class);
130
131 return 0;
132}
133
134/* Unregisters a datapath provider. 'type' must have been previously
135 * registered and not currently be in use by any dpifs. After unregistration
136 * new datapaths of that type cannot be opened using dpif_open(). */
137int
138dp_unregister_provider(const char *type)
139{
140 struct shash_node *node;
141 struct registered_dpif_class *registered_class;
142
143 node = shash_find(&dpif_classes, type);
144 if (!node) {
145 VLOG_WARN("attempted to unregister a datapath provider that is not "
146 "registered: %s", type);
147 return EAFNOSUPPORT;
148 }
149
150 registered_class = node->data;
151 if (registered_class->refcount) {
152 VLOG_WARN("attempted to unregister in use datapath provider: %s", type);
153 return EBUSY;
154 }
155
156 shash_delete(&dpif_classes, node);
157 free(registered_class);
158
159 return 0;
160}
161
579a77e0
EJ
162/* Blacklists a provider. Causes future calls of dp_register_provider() with
163 * a dpif_class which implements 'type' to fail. */
164void
165dp_blacklist_provider(const char *type)
166{
167 sset_add(&dpif_blacklist, type);
168}
169
999401aa 170/* Clears 'types' and enumerates the types of all currently registered datapath
d0c23a1a 171 * providers into it. The caller must first initialize the sset. */
1a6f1e2a 172void
d0c23a1a 173dp_enumerate_types(struct sset *types)
1a6f1e2a 174{
999401aa 175 struct shash_node *node;
1a6f1e2a 176
999401aa 177 dp_initialize();
d0c23a1a 178 sset_clear(types);
1a6f1e2a 179
999401aa
JG
180 SHASH_FOR_EACH(node, &dpif_classes) {
181 const struct registered_dpif_class *registered_class = node->data;
d0c23a1a 182 sset_add(types, registered_class->dpif_class->type);
1a6f1e2a
JG
183 }
184}
185
186/* Clears 'names' and enumerates the names of all known created datapaths with
d0c23a1a 187 * the given 'type'. The caller must first initialize the sset. Returns 0 if
1a6f1e2a 188 * successful, otherwise a positive errno value.
d3d22744
BP
189 *
190 * Some kinds of datapaths might not be practically enumerable. This is not
191 * considered an error. */
192int
d0c23a1a 193dp_enumerate_names(const char *type, struct sset *names)
d3d22744 194{
999401aa
JG
195 const struct registered_dpif_class *registered_class;
196 const struct dpif_class *dpif_class;
197 int error;
d3d22744 198
999401aa 199 dp_initialize();
d0c23a1a 200 sset_clear(names);
1a6f1e2a 201
999401aa
JG
202 registered_class = shash_find_data(&dpif_classes, type);
203 if (!registered_class) {
204 VLOG_WARN("could not enumerate unknown type: %s", type);
205 return EAFNOSUPPORT;
206 }
1a6f1e2a 207
d2d8fbeb 208 dpif_class = registered_class->dpif_class;
999401aa 209 error = dpif_class->enumerate ? dpif_class->enumerate(names) : 0;
1a6f1e2a 210
999401aa
JG
211 if (error) {
212 VLOG_WARN("failed to enumerate %s datapaths: %s", dpif_class->type,
213 strerror(error));
d3d22744 214 }
1a6f1e2a 215
999401aa 216 return error;
1a6f1e2a
JG
217}
218
54ed8a5d
BP
219/* Parses 'datapath_name_', which is of the form [type@]name into its
220 * component pieces. 'name' and 'type' must be freed by the caller.
221 *
222 * The returned 'type' is normalized, as if by dpif_normalize_type(). */
1a6f1e2a
JG
223void
224dp_parse_name(const char *datapath_name_, char **name, char **type)
225{
226 char *datapath_name = xstrdup(datapath_name_);
227 char *separator;
228
229 separator = strchr(datapath_name, '@');
230 if (separator) {
231 *separator = '\0';
232 *type = datapath_name;
54ed8a5d 233 *name = xstrdup(dpif_normalize_type(separator + 1));
1a6f1e2a
JG
234 } else {
235 *name = datapath_name;
54ed8a5d 236 *type = xstrdup(dpif_normalize_type(NULL));
1a6f1e2a 237 }
d3d22744
BP
238}
239
96fba48f 240static int
1a6f1e2a 241do_open(const char *name, const char *type, bool create, struct dpif **dpifp)
064af421 242{
96fba48f 243 struct dpif *dpif = NULL;
064af421 244 int error;
999401aa
JG
245 struct registered_dpif_class *registered_class;
246
247 dp_initialize();
064af421 248
3a225db7 249 type = dpif_normalize_type(type);
064af421 250
999401aa
JG
251 registered_class = shash_find_data(&dpif_classes, type);
252 if (!registered_class) {
253 VLOG_WARN("could not create datapath %s of unknown type %s", name,
254 type);
255 error = EAFNOSUPPORT;
256 goto exit;
257 }
258
4a387741
BP
259 error = registered_class->dpif_class->open(registered_class->dpif_class,
260 name, create, &dpif);
999401aa 261 if (!error) {
4a387741 262 assert(dpif->dpif_class == registered_class->dpif_class);
999401aa 263 registered_class->refcount++;
064af421 264 }
064af421 265
96fba48f
BP
266exit:
267 *dpifp = error ? NULL : dpif;
268 return error;
064af421
BP
269}
270
1a6f1e2a
JG
271/* Tries to open an existing datapath named 'name' and type 'type'. Will fail
272 * if no datapath with 'name' and 'type' exists. 'type' may be either NULL or
273 * the empty string to specify the default system type. Returns 0 if
274 * successful, otherwise a positive errno value. On success stores a pointer
275 * to the datapath in '*dpifp', otherwise a null pointer. */
96fba48f 276int
1a6f1e2a 277dpif_open(const char *name, const char *type, struct dpif **dpifp)
064af421 278{
1a6f1e2a 279 return do_open(name, type, false, dpifp);
064af421
BP
280}
281
1a6f1e2a
JG
282/* Tries to create and open a new datapath with the given 'name' and 'type'.
283 * 'type' may be either NULL or the empty string to specify the default system
284 * type. Will fail if a datapath with 'name' and 'type' already exists.
285 * Returns 0 if successful, otherwise a positive errno value. On success
286 * stores a pointer to the datapath in '*dpifp', otherwise a null pointer. */
064af421 287int
1a6f1e2a 288dpif_create(const char *name, const char *type, struct dpif **dpifp)
064af421 289{
1a6f1e2a 290 return do_open(name, type, true, dpifp);
96fba48f 291}
064af421 292
1a6f1e2a
JG
293/* Tries to open a datapath with the given 'name' and 'type', creating it if it
294 * does not exist. 'type' may be either NULL or the empty string to specify
295 * the default system type. Returns 0 if successful, otherwise a positive
296 * errno value. On success stores a pointer to the datapath in '*dpifp',
297 * otherwise a null pointer. */
efacbce6 298int
1a6f1e2a 299dpif_create_and_open(const char *name, const char *type, struct dpif **dpifp)
efacbce6
BP
300{
301 int error;
302
1a6f1e2a 303 error = dpif_create(name, type, dpifp);
efacbce6 304 if (error == EEXIST || error == EBUSY) {
1a6f1e2a 305 error = dpif_open(name, type, dpifp);
efacbce6
BP
306 if (error) {
307 VLOG_WARN("datapath %s already exists but cannot be opened: %s",
308 name, strerror(error));
309 }
310 } else if (error) {
311 VLOG_WARN("failed to create datapath %s: %s", name, strerror(error));
312 }
313 return error;
314}
315
96fba48f
BP
316/* Closes and frees the connection to 'dpif'. Does not destroy the datapath
317 * itself; call dpif_delete() first, instead, if that is desirable. */
318void
319dpif_close(struct dpif *dpif)
320{
321 if (dpif) {
999401aa
JG
322 struct registered_dpif_class *registered_class;
323
d295e8e9 324 registered_class = shash_find_data(&dpif_classes,
a4af0040 325 dpif->dpif_class->type);
999401aa
JG
326 assert(registered_class);
327 assert(registered_class->refcount);
328
329 registered_class->refcount--;
330 dpif_uninit(dpif, true);
064af421
BP
331 }
332}
333
640e1b20
BP
334/* Performs periodic work needed by 'dpif'. */
335void
336dpif_run(struct dpif *dpif)
337{
338 if (dpif->dpif_class->run) {
339 dpif->dpif_class->run(dpif);
340 }
341}
342
343/* Arranges for poll_block() to wake up when dp_run() needs to be called for
344 * 'dpif'. */
345void
346dpif_wait(struct dpif *dpif)
347{
348 if (dpif->dpif_class->wait) {
349 dpif->dpif_class->wait(dpif);
350 }
351}
352
1a6f1e2a
JG
353/* Returns the name of datapath 'dpif' prefixed with the type
354 * (for use in log messages). */
b29ba128
BP
355const char *
356dpif_name(const struct dpif *dpif)
357{
1a6f1e2a
JG
358 return dpif->full_name;
359}
360
361/* Returns the name of datapath 'dpif' without the type
362 * (for use in device names). */
363const char *
364dpif_base_name(const struct dpif *dpif)
365{
366 return dpif->base_name;
b29ba128
BP
367}
368
3a225db7
BP
369/* Returns the fully spelled out name for the given datapath 'type'.
370 *
371 * Normalized type string can be compared with strcmp(). Unnormalized type
372 * string might be the same even if they have different spellings. */
373const char *
374dpif_normalize_type(const char *type)
375{
376 return type && type[0] ? type : "system";
377}
378
96fba48f
BP
379/* Destroys the datapath that 'dpif' is connected to, first removing all of its
380 * ports. After calling this function, it does not make sense to pass 'dpif'
381 * to any functions other than dpif_name() or dpif_close(). */
064af421
BP
382int
383dpif_delete(struct dpif *dpif)
384{
96fba48f
BP
385 int error;
386
064af421 387 COVERAGE_INC(dpif_destroy);
96fba48f 388
1acb6baa 389 error = dpif->dpif_class->destroy(dpif);
96fba48f
BP
390 log_operation(dpif, "delete", error);
391 return error;
064af421
BP
392}
393
96fba48f
BP
394/* Retrieves statistics for 'dpif' into 'stats'. Returns 0 if successful,
395 * otherwise a positive errno value. */
064af421 396int
a8d9304d 397dpif_get_dp_stats(const struct dpif *dpif, struct dpif_dp_stats *stats)
064af421 398{
1acb6baa 399 int error = dpif->dpif_class->get_stats(dpif, stats);
96fba48f
BP
400 if (error) {
401 memset(stats, 0, sizeof *stats);
402 }
403 log_operation(dpif, "get_stats", error);
404 return error;
064af421
BP
405}
406
c3827f61
BP
407/* Attempts to add 'netdev' as a port on 'dpif'. If successful, returns 0 and
408 * sets '*port_nop' to the new port's port number (if 'port_nop' is non-null).
409 * On failure, returns a positive errno value and sets '*port_nop' to
410 * UINT16_MAX (if 'port_nop' is non-null). */
064af421 411int
c3827f61 412dpif_port_add(struct dpif *dpif, struct netdev *netdev, uint16_t *port_nop)
064af421 413{
c3827f61 414 const char *netdev_name = netdev_get_name(netdev);
9ee3ae3e
BP
415 uint16_t port_no;
416 int error;
064af421
BP
417
418 COVERAGE_INC(dpif_port_add);
9ee3ae3e 419
c3827f61 420 error = dpif->dpif_class->port_add(dpif, netdev, &port_no);
9ee3ae3e 421 if (!error) {
b29ba128 422 VLOG_DBG_RL(&dpmsg_rl, "%s: added %s as port %"PRIu16,
c3827f61 423 dpif_name(dpif), netdev_name, port_no);
064af421 424 } else {
9ee3ae3e 425 VLOG_WARN_RL(&error_rl, "%s: failed to add %s as port: %s",
c3827f61 426 dpif_name(dpif), netdev_name, strerror(error));
96fba48f 427 port_no = UINT16_MAX;
9ee3ae3e
BP
428 }
429 if (port_nop) {
430 *port_nop = port_no;
064af421 431 }
9ee3ae3e 432 return error;
064af421
BP
433}
434
96fba48f
BP
435/* Attempts to remove 'dpif''s port number 'port_no'. Returns 0 if successful,
436 * otherwise a positive errno value. */
064af421
BP
437int
438dpif_port_del(struct dpif *dpif, uint16_t port_no)
439{
96fba48f
BP
440 int error;
441
064af421 442 COVERAGE_INC(dpif_port_del);
96fba48f 443
1acb6baa 444 error = dpif->dpif_class->port_del(dpif, port_no);
a1811296
BP
445 if (!error) {
446 VLOG_DBG_RL(&dpmsg_rl, "%s: port_del(%"PRIu16")",
447 dpif_name(dpif), port_no);
448 } else {
449 log_operation(dpif, "port_del", error);
450 }
96fba48f 451 return error;
064af421
BP
452}
453
4c738a8d
BP
454/* Makes a deep copy of 'src' into 'dst'. */
455void
456dpif_port_clone(struct dpif_port *dst, const struct dpif_port *src)
457{
458 dst->name = xstrdup(src->name);
459 dst->type = xstrdup(src->type);
460 dst->port_no = src->port_no;
461}
462
463/* Frees memory allocated to members of 'dpif_port'.
464 *
465 * Do not call this function on a dpif_port obtained from
466 * dpif_port_dump_next(): that function retains ownership of the data in the
467 * dpif_port. */
468void
469dpif_port_destroy(struct dpif_port *dpif_port)
470{
471 free(dpif_port->name);
472 free(dpif_port->type);
473}
474
96fba48f
BP
475/* Looks up port number 'port_no' in 'dpif'. On success, returns 0 and
476 * initializes '*port' appropriately; on failure, returns a positive errno
4c738a8d
BP
477 * value.
478 *
479 * The caller owns the data in 'port' and must free it with
480 * dpif_port_destroy() when it is no longer needed. */
064af421
BP
481int
482dpif_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
4c738a8d 483 struct dpif_port *port)
064af421 484{
1acb6baa 485 int error = dpif->dpif_class->port_query_by_number(dpif, port_no, port);
96fba48f 486 if (!error) {
b29ba128 487 VLOG_DBG_RL(&dpmsg_rl, "%s: port %"PRIu16" is device %s",
4c738a8d 488 dpif_name(dpif), port_no, port->name);
064af421 489 } else {
96fba48f 490 memset(port, 0, sizeof *port);
b29ba128 491 VLOG_WARN_RL(&error_rl, "%s: failed to query port %"PRIu16": %s",
96fba48f 492 dpif_name(dpif), port_no, strerror(error));
064af421 493 }
96fba48f 494 return error;
064af421
BP
495}
496
96fba48f
BP
497/* Looks up port named 'devname' in 'dpif'. On success, returns 0 and
498 * initializes '*port' appropriately; on failure, returns a positive errno
4c738a8d
BP
499 * value.
500 *
501 * The caller owns the data in 'port' and must free it with
502 * dpif_port_destroy() when it is no longer needed. */
064af421
BP
503int
504dpif_port_query_by_name(const struct dpif *dpif, const char *devname,
4c738a8d 505 struct dpif_port *port)
064af421 506{
1acb6baa 507 int error = dpif->dpif_class->port_query_by_name(dpif, devname, port);
96fba48f 508 if (!error) {
b29ba128 509 VLOG_DBG_RL(&dpmsg_rl, "%s: device %s is on port %"PRIu16,
4c738a8d 510 dpif_name(dpif), devname, port->port_no);
064af421 511 } else {
96fba48f
BP
512 memset(port, 0, sizeof *port);
513
d647f0a7
BP
514 /* For ENOENT or ENODEV we use DBG level because the caller is probably
515 * interested in whether 'dpif' actually has a port 'devname', so that
516 * it's not an issue worth logging if it doesn't. Other errors are
517 * uncommon and more likely to indicate a real problem. */
518 VLOG_RL(&error_rl,
519 error == ENOENT || error == ENODEV ? VLL_DBG : VLL_WARN,
520 "%s: failed to query port %s: %s",
521 dpif_name(dpif), devname, strerror(error));
064af421 522 }
96fba48f 523 return error;
064af421
BP
524}
525
996c1b3d
BP
526/* Returns one greater than the maximum port number accepted in flow
527 * actions. */
528int
529dpif_get_max_ports(const struct dpif *dpif)
530{
531 return dpif->dpif_class->get_max_ports(dpif);
532}
533
98403001
BP
534/* Returns the Netlink PID value to supply in OVS_ACTION_ATTR_USERSPACE actions
535 * as the OVS_USERSPACE_ATTR_PID attribute's value, for use in flows whose
536 * packets arrived on port 'port_no'.
537 *
538 * The return value is only meaningful when DPIF_UC_ACTION has been enabled in
539 * the 'dpif''s listen mask. It is allowed to change when DPIF_UC_ACTION is
540 * disabled and then re-enabled, so a client that does that must be prepared to
541 * update all of the flows that it installed that contain
542 * OVS_ACTION_ATTR_USERSPACE actions. */
543uint32_t
544dpif_port_get_pid(const struct dpif *dpif, uint16_t port_no)
545{
546 return (dpif->dpif_class->port_get_pid
547 ? (dpif->dpif_class->port_get_pid)(dpif, port_no)
548 : 0);
549}
550
96fba48f
BP
551/* Looks up port number 'port_no' in 'dpif'. On success, returns 0 and copies
552 * the port's name into the 'name_size' bytes in 'name', ensuring that the
553 * result is null-terminated. On failure, returns a positive errno value and
554 * makes 'name' the empty string. */
335562c0
BP
555int
556dpif_port_get_name(struct dpif *dpif, uint16_t port_no,
557 char *name, size_t name_size)
558{
4c738a8d 559 struct dpif_port port;
335562c0
BP
560 int error;
561
562 assert(name_size > 0);
563
564 error = dpif_port_query_by_number(dpif, port_no, &port);
565 if (!error) {
4c738a8d
BP
566 ovs_strlcpy(name, port.name, name_size);
567 dpif_port_destroy(&port);
335562c0
BP
568 } else {
569 *name = '\0';
570 }
571 return error;
572}
573
b0ec0f27 574/* Initializes 'dump' to begin dumping the ports in a dpif.
96fba48f 575 *
b0ec0f27
BP
576 * This function provides no status indication. An error status for the entire
577 * dump operation is provided when it is completed by calling
578 * dpif_port_dump_done().
579 */
580void
581dpif_port_dump_start(struct dpif_port_dump *dump, const struct dpif *dpif)
582{
583 dump->dpif = dpif;
584 dump->error = dpif->dpif_class->port_dump_start(dpif, &dump->state);
585 log_operation(dpif, "port_dump_start", dump->error);
586}
587
588/* Attempts to retrieve another port from 'dump', which must have been
4c738a8d 589 * initialized with dpif_port_dump_start(). On success, stores a new dpif_port
b0ec0f27 590 * into 'port' and returns true. On failure, returns false.
96fba48f 591 *
b0ec0f27
BP
592 * Failure might indicate an actual error or merely that the last port has been
593 * dumped. An error status for the entire dump operation is provided when it
4c738a8d
BP
594 * is completed by calling dpif_port_dump_done().
595 *
596 * The dpif owns the data stored in 'port'. It will remain valid until at
597 * least the next time 'dump' is passed to dpif_port_dump_next() or
598 * dpif_port_dump_done(). */
b0ec0f27 599bool
4c738a8d 600dpif_port_dump_next(struct dpif_port_dump *dump, struct dpif_port *port)
064af421 601{
b0ec0f27 602 const struct dpif *dpif = dump->dpif;
064af421 603
b0ec0f27
BP
604 if (dump->error) {
605 return false;
606 }
f4ba4c4f 607
b0ec0f27
BP
608 dump->error = dpif->dpif_class->port_dump_next(dpif, dump->state, port);
609 if (dump->error == EOF) {
610 VLOG_DBG_RL(&dpmsg_rl, "%s: dumped all ports", dpif_name(dpif));
611 } else {
612 log_operation(dpif, "port_dump_next", dump->error);
613 }
064af421 614
b0ec0f27
BP
615 if (dump->error) {
616 dpif->dpif_class->port_dump_done(dpif, dump->state);
617 return false;
f4ba4c4f 618 }
b0ec0f27
BP
619 return true;
620}
064af421 621
b0ec0f27
BP
622/* Completes port table dump operation 'dump', which must have been initialized
623 * with dpif_port_dump_start(). Returns 0 if the dump operation was
624 * error-free, otherwise a positive errno value describing the problem. */
625int
626dpif_port_dump_done(struct dpif_port_dump *dump)
627{
628 const struct dpif *dpif = dump->dpif;
629 if (!dump->error) {
630 dump->error = dpif->dpif_class->port_dump_done(dpif, dump->state);
631 log_operation(dpif, "port_dump_done", dump->error);
f4ba4c4f 632 }
b0ec0f27 633 return dump->error == EOF ? 0 : dump->error;
064af421
BP
634}
635
e9e28be3
BP
636/* Polls for changes in the set of ports in 'dpif'. If the set of ports in
637 * 'dpif' has changed, this function does one of the following:
638 *
639 * - Stores the name of the device that was added to or deleted from 'dpif' in
640 * '*devnamep' and returns 0. The caller is responsible for freeing
641 * '*devnamep' (with free()) when it no longer needs it.
642 *
643 * - Returns ENOBUFS and sets '*devnamep' to NULL.
644 *
645 * This function may also return 'false positives', where it returns 0 and
646 * '*devnamep' names a device that was not actually added or deleted or it
647 * returns ENOBUFS without any change.
648 *
649 * Returns EAGAIN if the set of ports in 'dpif' has not changed. May also
650 * return other positive errno values to indicate that something has gone
651 * wrong. */
652int
653dpif_port_poll(const struct dpif *dpif, char **devnamep)
654{
1acb6baa 655 int error = dpif->dpif_class->port_poll(dpif, devnamep);
e9e28be3
BP
656 if (error) {
657 *devnamep = NULL;
658 }
659 return error;
660}
661
662/* Arranges for the poll loop to wake up when port_poll(dpif) will return a
663 * value other than EAGAIN. */
664void
665dpif_port_poll_wait(const struct dpif *dpif)
666{
1acb6baa 667 dpif->dpif_class->port_poll_wait(dpif);
e9e28be3
BP
668}
669
572b7068
BP
670/* Extracts the flow stats for a packet. The 'flow' and 'packet'
671 * arguments must have been initialized through a call to flow_extract().
672 */
673void
674dpif_flow_stats_extract(const struct flow *flow, struct ofpbuf *packet,
675 struct dpif_flow_stats *stats)
676{
677 memset(stats, 0, sizeof(*stats));
678
679 if ((flow->dl_type == htons(ETH_TYPE_IP)) && packet->l4) {
680 if ((flow->nw_proto == IPPROTO_TCP) && packet->l7) {
681 struct tcp_header *tcp = packet->l4;
682 stats->tcp_flags = TCP_FLAGS(tcp->tcp_ctl);
683 }
684 }
685
686 stats->n_bytes = packet->size;
687 stats->n_packets = 1;
688}
689
c97fb132
BP
690/* Appends a human-readable representation of 'stats' to 's'. */
691void
692dpif_flow_stats_format(const struct dpif_flow_stats *stats, struct ds *s)
693{
694 ds_put_format(s, "packets:%"PRIu64", bytes:%"PRIu64", used:",
695 stats->n_packets, stats->n_bytes);
696 if (stats->used) {
697 ds_put_format(s, "%.3fs", (time_msec() - stats->used) / 1000.0);
698 } else {
699 ds_put_format(s, "never");
700 }
701 /* XXX tcp_flags? */
702}
703
96fba48f
BP
704/* Deletes all flows from 'dpif'. Returns 0 if successful, otherwise a
705 * positive errno value. */
706int
707dpif_flow_flush(struct dpif *dpif)
064af421 708{
96fba48f
BP
709 int error;
710
711 COVERAGE_INC(dpif_flow_flush);
712
1acb6baa 713 error = dpif->dpif_class->flow_flush(dpif);
96fba48f
BP
714 log_operation(dpif, "flow_flush", error);
715 return error;
064af421
BP
716}
717
feebdea2 718/* Queries 'dpif' for a flow entry. The flow is specified by the Netlink
df2c07f4 719 * attributes with types OVS_KEY_ATTR_* in the 'key_len' bytes starting at
feebdea2 720 * 'key'.
96fba48f 721 *
feebdea2
BP
722 * Returns 0 if successful. If no flow matches, returns ENOENT. On other
723 * failure, returns a positive errno value.
96fba48f 724 *
feebdea2
BP
725 * If 'actionsp' is nonnull, then on success '*actionsp' will be set to an
726 * ofpbuf owned by the caller that contains the Netlink attributes for the
727 * flow's actions. The caller must free the ofpbuf (with ofpbuf_delete()) when
728 * it is no longer needed.
729 *
730 * If 'stats' is nonnull, then on success it will be updated with the flow's
731 * statistics. */
96fba48f 732int
693c4a01 733dpif_flow_get(const struct dpif *dpif,
feebdea2 734 const struct nlattr *key, size_t key_len,
c97fb132 735 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
064af421 736{
96fba48f
BP
737 int error;
738
739 COVERAGE_INC(dpif_flow_get);
740
693c4a01 741 error = dpif->dpif_class->flow_get(dpif, key, key_len, actionsp, stats);
b843fa1b 742 if (error) {
feebdea2
BP
743 if (actionsp) {
744 *actionsp = NULL;
745 }
746 if (stats) {
747 memset(stats, 0, sizeof *stats);
748 }
b843fa1b 749 }
96fba48f 750 if (should_log_flow_message(error)) {
feebdea2
BP
751 const struct nlattr *actions;
752 size_t actions_len;
753
754 if (!error && actionsp) {
755 actions = (*actionsp)->data;
756 actions_len = (*actionsp)->size;
757 } else {
758 actions = NULL;
759 actions_len = 0;
760 }
761 log_flow_message(dpif, error, "flow_get", key, key_len, stats,
762 actions, actions_len);
064af421 763 }
96fba48f 764 return error;
064af421
BP
765}
766
feebdea2 767/* Adds or modifies a flow in 'dpif'. The flow is specified by the Netlink
df2c07f4 768 * attributes with types OVS_KEY_ATTR_* in the 'key_len' bytes starting at
feebdea2 769 * 'key'. The associated actions are specified by the Netlink attributes with
df2c07f4 770 * types OVS_ACTION_ATTR_* in the 'actions_len' bytes starting at 'actions'.
96fba48f 771 *
feebdea2 772 * - If the flow's key does not exist in 'dpif', then the flow will be added if
ba25b8f4 773 * 'flags' includes DPIF_FP_CREATE. Otherwise the operation will fail with
96fba48f
BP
774 * ENOENT.
775 *
feebdea2 776 * If the operation succeeds, then 'stats', if nonnull, will be zeroed.
96fba48f 777 *
feebdea2 778 * - If the flow's key does exist in 'dpif', then the flow's actions will be
ba25b8f4 779 * updated if 'flags' includes DPIF_FP_MODIFY. Otherwise the operation will
feebdea2 780 * fail with EEXIST. If the flow's actions are updated, then its statistics
ba25b8f4 781 * will be zeroed if 'flags' includes DPIF_FP_ZERO_STATS, and left as-is
feebdea2
BP
782 * otherwise.
783 *
784 * If the operation succeeds, then 'stats', if nonnull, will be set to the
785 * flow's statistics before the update.
96fba48f 786 */
064af421 787int
ba25b8f4 788dpif_flow_put(struct dpif *dpif, enum dpif_flow_put_flags flags,
feebdea2
BP
789 const struct nlattr *key, size_t key_len,
790 const struct nlattr *actions, size_t actions_len,
c97fb132 791 struct dpif_flow_stats *stats)
064af421 792{
96fba48f
BP
793 int error;
794
064af421 795 COVERAGE_INC(dpif_flow_put);
ba25b8f4 796 assert(!(flags & ~(DPIF_FP_CREATE | DPIF_FP_MODIFY | DPIF_FP_ZERO_STATS)));
96fba48f 797
feebdea2
BP
798 error = dpif->dpif_class->flow_put(dpif, flags, key, key_len,
799 actions, actions_len, stats);
800 if (error && stats) {
801 memset(stats, 0, sizeof *stats);
802 }
064af421 803 if (should_log_flow_message(error)) {
feebdea2
BP
804 struct ds s;
805
806 ds_init(&s);
807 ds_put_cstr(&s, "put");
ba25b8f4 808 if (flags & DPIF_FP_CREATE) {
feebdea2
BP
809 ds_put_cstr(&s, "[create]");
810 }
ba25b8f4 811 if (flags & DPIF_FP_MODIFY) {
feebdea2
BP
812 ds_put_cstr(&s, "[modify]");
813 }
ba25b8f4 814 if (flags & DPIF_FP_ZERO_STATS) {
feebdea2
BP
815 ds_put_cstr(&s, "[zero]");
816 }
feebdea2
BP
817 log_flow_message(dpif, error, ds_cstr(&s), key, key_len, stats,
818 actions, actions_len);
819 ds_destroy(&s);
064af421
BP
820 }
821 return error;
822}
823
feebdea2
BP
824/* Deletes a flow from 'dpif' and returns 0, or returns ENOENT if 'dpif' does
825 * not contain such a flow. The flow is specified by the Netlink attributes
df2c07f4 826 * with types OVS_KEY_ATTR_* in the 'key_len' bytes starting at 'key'.
96fba48f 827 *
feebdea2
BP
828 * If the operation succeeds, then 'stats', if nonnull, will be set to the
829 * flow's statistics before its deletion. */
064af421 830int
feebdea2
BP
831dpif_flow_del(struct dpif *dpif,
832 const struct nlattr *key, size_t key_len,
c97fb132 833 struct dpif_flow_stats *stats)
064af421 834{
f1aa2072
BP
835 int error;
836
96fba48f 837 COVERAGE_INC(dpif_flow_del);
f1aa2072 838
feebdea2
BP
839 error = dpif->dpif_class->flow_del(dpif, key, key_len, stats);
840 if (error && stats) {
841 memset(stats, 0, sizeof *stats);
842 }
96fba48f 843 if (should_log_flow_message(error)) {
feebdea2
BP
844 log_flow_message(dpif, error, "flow_del", key, key_len,
845 !error ? stats : NULL, NULL, 0);
064af421 846 }
96fba48f 847 return error;
064af421
BP
848}
849
704a1e09
BP
850/* Initializes 'dump' to begin dumping the flows in a dpif.
851 *
852 * This function provides no status indication. An error status for the entire
853 * dump operation is provided when it is completed by calling
854 * dpif_flow_dump_done().
855 */
856void
857dpif_flow_dump_start(struct dpif_flow_dump *dump, const struct dpif *dpif)
064af421 858{
704a1e09
BP
859 dump->dpif = dpif;
860 dump->error = dpif->dpif_class->flow_dump_start(dpif, &dump->state);
861 log_operation(dpif, "flow_dump_start", dump->error);
064af421
BP
862}
863
704a1e09 864/* Attempts to retrieve another flow from 'dump', which must have been
feebdea2
BP
865 * initialized with dpif_flow_dump_start(). On success, updates the output
866 * parameters as described below and returns true. Otherwise, returns false.
867 * Failure might indicate an actual error or merely the end of the flow table.
868 * An error status for the entire dump operation is provided when it is
869 * completed by calling dpif_flow_dump_done().
870 *
871 * On success, if 'key' and 'key_len' are nonnull then '*key' and '*key_len'
df2c07f4 872 * will be set to Netlink attributes with types OVS_KEY_ATTR_* representing the
feebdea2 873 * dumped flow's key. If 'actions' and 'actions_len' are nonnull then they are
df2c07f4 874 * set to Netlink attributes with types OVS_ACTION_ATTR_* representing the
7aec165d
BP
875 * dumped flow's actions. If 'stats' is nonnull then it will be set to the
876 * dumped flow's statistics.
96fba48f 877 *
feebdea2
BP
878 * All of the returned data is owned by 'dpif', not by the caller, and the
879 * caller must not modify or free it. 'dpif' guarantees that it remains
880 * accessible and unchanging until at least the next call to 'flow_dump_next'
881 * or 'flow_dump_done' for 'dump'. */
704a1e09 882bool
feebdea2
BP
883dpif_flow_dump_next(struct dpif_flow_dump *dump,
884 const struct nlattr **key, size_t *key_len,
885 const struct nlattr **actions, size_t *actions_len,
c97fb132 886 const struct dpif_flow_stats **stats)
704a1e09
BP
887{
888 const struct dpif *dpif = dump->dpif;
feebdea2 889 int error = dump->error;
064af421 890
feebdea2
BP
891 if (!error) {
892 error = dpif->dpif_class->flow_dump_next(dpif, dump->state,
893 key, key_len,
894 actions, actions_len,
895 stats);
896 if (error) {
897 dpif->dpif_class->flow_dump_done(dpif, dump->state);
898 }
064af421 899 }
feebdea2
BP
900 if (error) {
901 if (key) {
902 *key = NULL;
903 *key_len = 0;
36956a7d 904 }
feebdea2
BP
905 if (actions) {
906 *actions = NULL;
907 *actions_len = 0;
908 }
909 if (stats) {
910 *stats = NULL;
704a1e09 911 }
064af421 912 }
feebdea2
BP
913 if (!dump->error) {
914 if (error == EOF) {
915 VLOG_DBG_RL(&dpmsg_rl, "%s: dumped all flows", dpif_name(dpif));
916 } else if (should_log_flow_message(error)) {
917 log_flow_message(dpif, error, "flow_dump",
918 key ? *key : NULL, key ? *key_len : 0,
919 stats ? *stats : NULL, actions ? *actions : NULL,
920 actions ? *actions_len : 0);
921 }
064af421 922 }
feebdea2
BP
923 dump->error = error;
924 return !error;
704a1e09
BP
925}
926
927/* Completes flow table dump operation 'dump', which must have been initialized
928 * with dpif_flow_dump_start(). Returns 0 if the dump operation was
929 * error-free, otherwise a positive errno value describing the problem. */
930int
931dpif_flow_dump_done(struct dpif_flow_dump *dump)
932{
933 const struct dpif *dpif = dump->dpif;
934 if (!dump->error) {
935 dump->error = dpif->dpif_class->flow_dump_done(dpif, dump->state);
936 log_operation(dpif, "flow_dump_done", dump->error);
937 }
938 return dump->error == EOF ? 0 : dump->error;
064af421
BP
939}
940
cdee00fd 941/* Causes 'dpif' to perform the 'actions_len' bytes of actions in 'actions' on
80e5eed9
BP
942 * the Ethernet frame specified in 'packet' taken from the flow specified in
943 * the 'key_len' bytes of 'key'. ('key' is mostly redundant with 'packet', but
944 * it contains some metadata that cannot be recovered from 'packet', such as
945 * tun_id and in_port.)
96fba48f 946 *
96fba48f 947 * Returns 0 if successful, otherwise a positive errno value. */
064af421 948int
f1588b1f 949dpif_execute(struct dpif *dpif,
80e5eed9 950 const struct nlattr *key, size_t key_len,
cdee00fd 951 const struct nlattr *actions, size_t actions_len,
064af421
BP
952 const struct ofpbuf *buf)
953{
954 int error;
955
956 COVERAGE_INC(dpif_execute);
cdee00fd 957 if (actions_len > 0) {
80e5eed9
BP
958 error = dpif->dpif_class->execute(dpif, key, key_len,
959 actions, actions_len, buf);
064af421
BP
960 } else {
961 error = 0;
962 }
963
964 if (!(error ? VLOG_DROP_WARN(&error_rl) : VLOG_DROP_DBG(&dpmsg_rl))) {
965 struct ds ds = DS_EMPTY_INITIALIZER;
c499c75d 966 char *packet = ofp_packet_to_string(buf->data, buf->size);
b29ba128 967 ds_put_format(&ds, "%s: execute ", dpif_name(dpif));
cdee00fd 968 format_odp_actions(&ds, actions, actions_len);
064af421
BP
969 if (error) {
970 ds_put_format(&ds, " failed (%s)", strerror(error));
971 }
972 ds_put_format(&ds, " on packet %s", packet);
973 vlog(THIS_MODULE, error ? VLL_WARN : VLL_DBG, "%s", ds_cstr(&ds));
974 ds_destroy(&ds);
975 free(packet);
976 }
977 return error;
978}
979
6bc60024
BP
980/* Executes each of the 'n_ops' operations in 'ops' on 'dpif', in the order in
981 * which they are specified, placing each operation's results in the "output"
982 * members documented in comments.
983 *
984 * This function exists because some datapaths can perform batched operations
985 * faster than individual operations. */
986void
c2b565b5 987dpif_operate(struct dpif *dpif, struct dpif_op **ops, size_t n_ops)
6bc60024
BP
988{
989 size_t i;
990
991 if (dpif->dpif_class->operate) {
992 dpif->dpif_class->operate(dpif, ops, n_ops);
993 return;
994 }
995
996 for (i = 0; i < n_ops; i++) {
c2b565b5 997 struct dpif_op *op = ops[i];
6bc60024
BP
998 struct dpif_flow_put *put;
999 struct dpif_execute *execute;
1000
1001 switch (op->type) {
1002 case DPIF_OP_FLOW_PUT:
c2b565b5
BP
1003 put = &op->u.flow_put;
1004 op->error = dpif_flow_put(dpif, put->flags,
1005 put->key, put->key_len,
1006 put->actions, put->actions_len,
1007 put->stats);
6bc60024
BP
1008 break;
1009
1010 case DPIF_OP_EXECUTE:
c2b565b5
BP
1011 execute = &op->u.execute;
1012 op->error = dpif_execute(dpif, execute->key, execute->key_len,
1013 execute->actions,
1014 execute->actions_len,
1015 execute->packet);
6bc60024
BP
1016 break;
1017
1018 default:
1019 NOT_REACHED();
1020 }
1021 }
1022}
1023
1024
01545c1a
BP
1025/* Returns a string that represents 'type', for use in log messages. */
1026const char *
1027dpif_upcall_type_to_string(enum dpif_upcall_type type)
1028{
1029 switch (type) {
1030 case DPIF_UC_MISS: return "miss";
1031 case DPIF_UC_ACTION: return "action";
01545c1a
BP
1032 case DPIF_N_UC_TYPES: default: return "<unknown>";
1033 }
1034}
1035
a12b3ead
BP
1036/* Enables or disables receiving packets with dpif_recv() on 'dpif'. Returns 0
1037 * if successful, otherwise a positive errno value.
98403001 1038 *
a12b3ead 1039 * Turning packet receive off and then back on may change the Netlink PID
98403001
BP
1040 * assignments returned by dpif_port_get_pid(). If the client does this, it
1041 * must update all of the flows that have OVS_ACTION_ATTR_USERSPACE actions
1042 * using the new PID assignment. */
8f24562a 1043int
a12b3ead 1044dpif_recv_set(struct dpif *dpif, bool enable)
8f24562a 1045{
a12b3ead
BP
1046 int error = dpif->dpif_class->recv_set(dpif, enable);
1047 log_operation(dpif, "recv_set", error);
96fba48f 1048 return error;
8f24562a
BP
1049}
1050
856081f6 1051/* Polls for an upcall from 'dpif'. If successful, stores the upcall into
a12b3ead
BP
1052 * '*upcall'. Should only be called if dpif_recv_set() has been used to enable
1053 * receiving packets on 'dpif'.
96fba48f 1054 *
856081f6
BP
1055 * The caller takes ownership of the data that 'upcall' points to.
1056 * 'upcall->key' and 'upcall->actions' (if nonnull) point into data owned by
1057 * 'upcall->packet', so their memory cannot be freed separately. (This is
1058 * hardly a great way to do things but it works out OK for the dpif providers
1059 * and clients that exist so far.)
1060 *
96fba48f 1061 * Returns 0 if successful, otherwise a positive errno value. Returns EAGAIN
856081f6 1062 * if no upcall is immediately available. */
064af421 1063int
856081f6 1064dpif_recv(struct dpif *dpif, struct dpif_upcall *upcall)
064af421 1065{
856081f6
BP
1066 int error = dpif->dpif_class->recv(dpif, upcall);
1067 if (!error && !VLOG_DROP_DBG(&dpmsg_rl)) {
01545c1a
BP
1068 struct ds flow;
1069 char *packet;
1070
1071 packet = ofp_packet_to_string(upcall->packet->data,
01545c1a
BP
1072 upcall->packet->size);
1073
1074 ds_init(&flow);
1075 odp_flow_key_format(upcall->key, upcall->key_len, &flow);
1076
1077 VLOG_DBG("%s: %s upcall:\n%s\n%s",
1078 dpif_name(dpif), dpif_upcall_type_to_string(upcall->type),
1079 ds_cstr(&flow), packet);
1080
1081 ds_destroy(&flow);
1082 free(packet);
5fcc0d00
BP
1083 } else if (error && error != EAGAIN) {
1084 log_operation(dpif, "recv", error);
064af421 1085 }
064af421
BP
1086 return error;
1087}
1088
96fba48f 1089/* Discards all messages that would otherwise be received by dpif_recv() on
1ba530f4
BP
1090 * 'dpif'. */
1091void
96fba48f
BP
1092dpif_recv_purge(struct dpif *dpif)
1093{
96fba48f 1094 COVERAGE_INC(dpif_purge);
1ba530f4
BP
1095 if (dpif->dpif_class->recv_purge) {
1096 dpif->dpif_class->recv_purge(dpif);
96fba48f 1097 }
96fba48f
BP
1098}
1099
1100/* Arranges for the poll loop to wake up when 'dpif' has a message queued to be
1101 * received with dpif_recv(). */
064af421
BP
1102void
1103dpif_recv_wait(struct dpif *dpif)
1104{
1acb6baa 1105 dpif->dpif_class->recv_wait(dpif);
064af421 1106}
53a4218d 1107
96fba48f
BP
1108/* Obtains the NetFlow engine type and engine ID for 'dpif' into '*engine_type'
1109 * and '*engine_id', respectively. */
53a4218d
BP
1110void
1111dpif_get_netflow_ids(const struct dpif *dpif,
1112 uint8_t *engine_type, uint8_t *engine_id)
1113{
96fba48f
BP
1114 *engine_type = dpif->netflow_engine_type;
1115 *engine_id = dpif->netflow_engine_id;
1116}
aae51f53
BP
1117
1118/* Translates OpenFlow queue ID 'queue_id' (in host byte order) into a priority
abff858b
PS
1119 * value used for setting packet priority.
1120 * On success, returns 0 and stores the priority into '*priority'.
1121 * On failure, returns a positive errno value and stores 0 into '*priority'. */
aae51f53
BP
1122int
1123dpif_queue_to_priority(const struct dpif *dpif, uint32_t queue_id,
1124 uint32_t *priority)
1125{
1126 int error = (dpif->dpif_class->queue_to_priority
1127 ? dpif->dpif_class->queue_to_priority(dpif, queue_id,
1128 priority)
1129 : EOPNOTSUPP);
1130 if (error) {
1131 *priority = 0;
1132 }
1133 log_operation(dpif, "queue_to_priority", error);
1134 return error;
1135}
96fba48f
BP
1136\f
1137void
1acb6baa
BP
1138dpif_init(struct dpif *dpif, const struct dpif_class *dpif_class,
1139 const char *name,
96fba48f
BP
1140 uint8_t netflow_engine_type, uint8_t netflow_engine_id)
1141{
1acb6baa 1142 dpif->dpif_class = dpif_class;
1a6f1e2a 1143 dpif->base_name = xstrdup(name);
a4af0040 1144 dpif->full_name = xasprintf("%s@%s", dpif_class->type, name);
96fba48f
BP
1145 dpif->netflow_engine_type = netflow_engine_type;
1146 dpif->netflow_engine_id = netflow_engine_id;
1147}
999401aa
JG
1148
1149/* Undoes the results of initialization.
1150 *
1151 * Normally this function only needs to be called from dpif_close().
1152 * However, it may be called by providers due to an error on opening
1153 * that occurs after initialization. It this case dpif_close() would
1154 * never be called. */
1155void
1156dpif_uninit(struct dpif *dpif, bool close)
1157{
1158 char *base_name = dpif->base_name;
1159 char *full_name = dpif->full_name;
1160
1161 if (close) {
a4af0040 1162 dpif->dpif_class->close(dpif);
999401aa
JG
1163 }
1164
1165 free(base_name);
1166 free(full_name);
1167}
96fba48f
BP
1168\f
1169static void
1170log_operation(const struct dpif *dpif, const char *operation, int error)
1171{
1172 if (!error) {
1173 VLOG_DBG_RL(&dpmsg_rl, "%s: %s success", dpif_name(dpif), operation);
90bf1e07 1174 } else if (ofperr_is_valid(error)) {
96fba48f 1175 VLOG_WARN_RL(&error_rl, "%s: %s failed (%s)",
90bf1e07 1176 dpif_name(dpif), operation, ofperr_get_name(error));
71ce9235 1177 } else {
90bf1e07
BP
1178 VLOG_WARN_RL(&error_rl, "%s: %s failed (%s)",
1179 dpif_name(dpif), operation, strerror(error));
96fba48f
BP
1180 }
1181}
1182
1183static enum vlog_level
1184flow_message_log_level(int error)
1185{
1186 return error ? VLL_WARN : VLL_DBG;
1187}
1188
1189static bool
1190should_log_flow_message(int error)
1191{
1192 return !vlog_should_drop(THIS_MODULE, flow_message_log_level(error),
1193 error ? &error_rl : &dpmsg_rl);
1194}
1195
1196static void
1197log_flow_message(const struct dpif *dpif, int error, const char *operation,
36956a7d 1198 const struct nlattr *key, size_t key_len,
c97fb132 1199 const struct dpif_flow_stats *stats,
cf22f8cb 1200 const struct nlattr *actions, size_t actions_len)
96fba48f
BP
1201{
1202 struct ds ds = DS_EMPTY_INITIALIZER;
1203 ds_put_format(&ds, "%s: ", dpif_name(dpif));
1204 if (error) {
1205 ds_put_cstr(&ds, "failed to ");
1206 }
1207 ds_put_format(&ds, "%s ", operation);
1208 if (error) {
1209 ds_put_format(&ds, "(%s) ", strerror(error));
1210 }
36956a7d 1211 odp_flow_key_format(key, key_len, &ds);
96fba48f
BP
1212 if (stats) {
1213 ds_put_cstr(&ds, ", ");
c97fb132 1214 dpif_flow_stats_format(stats, &ds);
96fba48f 1215 }
cdee00fd 1216 if (actions || actions_len) {
96fba48f 1217 ds_put_cstr(&ds, ", actions:");
cdee00fd 1218 format_odp_actions(&ds, actions, actions_len);
96fba48f
BP
1219 }
1220 vlog(THIS_MODULE, flow_message_log_level(error), "%s", ds_cstr(&ds));
1221 ds_destroy(&ds);
1222}