]> git.proxmox.com Git - ovs.git/blob - lib/dpif.c
dpif: Simplify the "listen mask" concept.
[ovs.git] / lib / dpif.c
1 /*
2 * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <config.h>
18 #include "dpif-provider.h"
19
20 #include <assert.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <inttypes.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "coverage.h"
28 #include "dynamic-string.h"
29 #include "flow.h"
30 #include "netdev.h"
31 #include "netlink.h"
32 #include "odp-util.h"
33 #include "ofp-errors.h"
34 #include "ofp-print.h"
35 #include "ofp-util.h"
36 #include "ofpbuf.h"
37 #include "packets.h"
38 #include "poll-loop.h"
39 #include "shash.h"
40 #include "sset.h"
41 #include "timeval.h"
42 #include "util.h"
43 #include "valgrind.h"
44 #include "vlog.h"
45
46 VLOG_DEFINE_THIS_MODULE(dpif);
47
48 COVERAGE_DEFINE(dpif_destroy);
49 COVERAGE_DEFINE(dpif_port_add);
50 COVERAGE_DEFINE(dpif_port_del);
51 COVERAGE_DEFINE(dpif_flow_flush);
52 COVERAGE_DEFINE(dpif_flow_get);
53 COVERAGE_DEFINE(dpif_flow_put);
54 COVERAGE_DEFINE(dpif_flow_del);
55 COVERAGE_DEFINE(dpif_flow_query_list);
56 COVERAGE_DEFINE(dpif_flow_query_list_n);
57 COVERAGE_DEFINE(dpif_execute);
58 COVERAGE_DEFINE(dpif_purge);
59
60 static const struct dpif_class *base_dpif_classes[] = {
61 #ifdef HAVE_NETLINK
62 &dpif_linux_class,
63 #endif
64 &dpif_netdev_class,
65 };
66
67 struct registered_dpif_class {
68 const struct dpif_class *dpif_class;
69 int refcount;
70 };
71 static struct shash dpif_classes = SHASH_INITIALIZER(&dpif_classes);
72 static struct sset dpif_blacklist = SSET_INITIALIZER(&dpif_blacklist);
73
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. */
77 static struct vlog_rate_limit dpmsg_rl = VLOG_RATE_LIMIT_INIT(600, 600);
78
79 /* Not really much point in logging many dpif errors. */
80 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(60, 5);
81
82 static void log_flow_message(const struct dpif *dpif, int error,
83 const char *operation,
84 const struct nlattr *key, size_t key_len,
85 const struct dpif_flow_stats *stats,
86 const struct nlattr *actions, size_t actions_len);
87 static void log_operation(const struct dpif *, const char *operation,
88 int error);
89 static bool should_log_flow_message(int error);
90
91 static void
92 dp_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
106 /* Registers a new datapath provider. After successful registration, new
107 * datapaths of that type can be opened using dpif_open(). */
108 int
109 dp_register_provider(const struct dpif_class *new_class)
110 {
111 struct registered_dpif_class *registered_class;
112
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
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 }
124
125 registered_class = xmalloc(sizeof *registered_class);
126 registered_class->dpif_class = new_class;
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(). */
137 int
138 dp_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
162 /* Blacklists a provider. Causes future calls of dp_register_provider() with
163 * a dpif_class which implements 'type' to fail. */
164 void
165 dp_blacklist_provider(const char *type)
166 {
167 sset_add(&dpif_blacklist, type);
168 }
169
170 /* Clears 'types' and enumerates the types of all currently registered datapath
171 * providers into it. The caller must first initialize the sset. */
172 void
173 dp_enumerate_types(struct sset *types)
174 {
175 struct shash_node *node;
176
177 dp_initialize();
178 sset_clear(types);
179
180 SHASH_FOR_EACH(node, &dpif_classes) {
181 const struct registered_dpif_class *registered_class = node->data;
182 sset_add(types, registered_class->dpif_class->type);
183 }
184 }
185
186 /* Clears 'names' and enumerates the names of all known created datapaths with
187 * the given 'type'. The caller must first initialize the sset. Returns 0 if
188 * successful, otherwise a positive errno value.
189 *
190 * Some kinds of datapaths might not be practically enumerable. This is not
191 * considered an error. */
192 int
193 dp_enumerate_names(const char *type, struct sset *names)
194 {
195 const struct registered_dpif_class *registered_class;
196 const struct dpif_class *dpif_class;
197 int error;
198
199 dp_initialize();
200 sset_clear(names);
201
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 }
207
208 dpif_class = registered_class->dpif_class;
209 error = dpif_class->enumerate ? dpif_class->enumerate(names) : 0;
210
211 if (error) {
212 VLOG_WARN("failed to enumerate %s datapaths: %s", dpif_class->type,
213 strerror(error));
214 }
215
216 return error;
217 }
218
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(). */
223 void
224 dp_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;
233 *name = xstrdup(dpif_normalize_type(separator + 1));
234 } else {
235 *name = datapath_name;
236 *type = xstrdup(dpif_normalize_type(NULL));
237 }
238 }
239
240 static int
241 do_open(const char *name, const char *type, bool create, struct dpif **dpifp)
242 {
243 struct dpif *dpif = NULL;
244 int error;
245 struct registered_dpif_class *registered_class;
246
247 dp_initialize();
248
249 type = dpif_normalize_type(type);
250
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
259 error = registered_class->dpif_class->open(registered_class->dpif_class,
260 name, create, &dpif);
261 if (!error) {
262 assert(dpif->dpif_class == registered_class->dpif_class);
263 registered_class->refcount++;
264 }
265
266 exit:
267 *dpifp = error ? NULL : dpif;
268 return error;
269 }
270
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. */
276 int
277 dpif_open(const char *name, const char *type, struct dpif **dpifp)
278 {
279 return do_open(name, type, false, dpifp);
280 }
281
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. */
287 int
288 dpif_create(const char *name, const char *type, struct dpif **dpifp)
289 {
290 return do_open(name, type, true, dpifp);
291 }
292
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. */
298 int
299 dpif_create_and_open(const char *name, const char *type, struct dpif **dpifp)
300 {
301 int error;
302
303 error = dpif_create(name, type, dpifp);
304 if (error == EEXIST || error == EBUSY) {
305 error = dpif_open(name, type, dpifp);
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
316 /* Closes and frees the connection to 'dpif'. Does not destroy the datapath
317 * itself; call dpif_delete() first, instead, if that is desirable. */
318 void
319 dpif_close(struct dpif *dpif)
320 {
321 if (dpif) {
322 struct registered_dpif_class *registered_class;
323
324 registered_class = shash_find_data(&dpif_classes,
325 dpif->dpif_class->type);
326 assert(registered_class);
327 assert(registered_class->refcount);
328
329 registered_class->refcount--;
330 dpif_uninit(dpif, true);
331 }
332 }
333
334 /* Performs periodic work needed by 'dpif'. */
335 void
336 dpif_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'. */
345 void
346 dpif_wait(struct dpif *dpif)
347 {
348 if (dpif->dpif_class->wait) {
349 dpif->dpif_class->wait(dpif);
350 }
351 }
352
353 /* Returns the name of datapath 'dpif' prefixed with the type
354 * (for use in log messages). */
355 const char *
356 dpif_name(const struct dpif *dpif)
357 {
358 return dpif->full_name;
359 }
360
361 /* Returns the name of datapath 'dpif' without the type
362 * (for use in device names). */
363 const char *
364 dpif_base_name(const struct dpif *dpif)
365 {
366 return dpif->base_name;
367 }
368
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. */
373 const char *
374 dpif_normalize_type(const char *type)
375 {
376 return type && type[0] ? type : "system";
377 }
378
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(). */
382 int
383 dpif_delete(struct dpif *dpif)
384 {
385 int error;
386
387 COVERAGE_INC(dpif_destroy);
388
389 error = dpif->dpif_class->destroy(dpif);
390 log_operation(dpif, "delete", error);
391 return error;
392 }
393
394 /* Retrieves statistics for 'dpif' into 'stats'. Returns 0 if successful,
395 * otherwise a positive errno value. */
396 int
397 dpif_get_dp_stats(const struct dpif *dpif, struct dpif_dp_stats *stats)
398 {
399 int error = dpif->dpif_class->get_stats(dpif, stats);
400 if (error) {
401 memset(stats, 0, sizeof *stats);
402 }
403 log_operation(dpif, "get_stats", error);
404 return error;
405 }
406
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). */
411 int
412 dpif_port_add(struct dpif *dpif, struct netdev *netdev, uint16_t *port_nop)
413 {
414 const char *netdev_name = netdev_get_name(netdev);
415 uint16_t port_no;
416 int error;
417
418 COVERAGE_INC(dpif_port_add);
419
420 error = dpif->dpif_class->port_add(dpif, netdev, &port_no);
421 if (!error) {
422 VLOG_DBG_RL(&dpmsg_rl, "%s: added %s as port %"PRIu16,
423 dpif_name(dpif), netdev_name, port_no);
424 } else {
425 VLOG_WARN_RL(&error_rl, "%s: failed to add %s as port: %s",
426 dpif_name(dpif), netdev_name, strerror(error));
427 port_no = UINT16_MAX;
428 }
429 if (port_nop) {
430 *port_nop = port_no;
431 }
432 return error;
433 }
434
435 /* Attempts to remove 'dpif''s port number 'port_no'. Returns 0 if successful,
436 * otherwise a positive errno value. */
437 int
438 dpif_port_del(struct dpif *dpif, uint16_t port_no)
439 {
440 int error;
441
442 COVERAGE_INC(dpif_port_del);
443
444 error = dpif->dpif_class->port_del(dpif, port_no);
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 }
451 return error;
452 }
453
454 /* Makes a deep copy of 'src' into 'dst'. */
455 void
456 dpif_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. */
468 void
469 dpif_port_destroy(struct dpif_port *dpif_port)
470 {
471 free(dpif_port->name);
472 free(dpif_port->type);
473 }
474
475 /* Looks up port number 'port_no' in 'dpif'. On success, returns 0 and
476 * initializes '*port' appropriately; on failure, returns a positive errno
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. */
481 int
482 dpif_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
483 struct dpif_port *port)
484 {
485 int error = dpif->dpif_class->port_query_by_number(dpif, port_no, port);
486 if (!error) {
487 VLOG_DBG_RL(&dpmsg_rl, "%s: port %"PRIu16" is device %s",
488 dpif_name(dpif), port_no, port->name);
489 } else {
490 memset(port, 0, sizeof *port);
491 VLOG_WARN_RL(&error_rl, "%s: failed to query port %"PRIu16": %s",
492 dpif_name(dpif), port_no, strerror(error));
493 }
494 return error;
495 }
496
497 /* Looks up port named 'devname' in 'dpif'. On success, returns 0 and
498 * initializes '*port' appropriately; on failure, returns a positive errno
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. */
503 int
504 dpif_port_query_by_name(const struct dpif *dpif, const char *devname,
505 struct dpif_port *port)
506 {
507 int error = dpif->dpif_class->port_query_by_name(dpif, devname, port);
508 if (!error) {
509 VLOG_DBG_RL(&dpmsg_rl, "%s: device %s is on port %"PRIu16,
510 dpif_name(dpif), devname, port->port_no);
511 } else {
512 memset(port, 0, sizeof *port);
513
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));
522 }
523 return error;
524 }
525
526 /* Returns one greater than the maximum port number accepted in flow
527 * actions. */
528 int
529 dpif_get_max_ports(const struct dpif *dpif)
530 {
531 return dpif->dpif_class->get_max_ports(dpif);
532 }
533
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. */
543 uint32_t
544 dpif_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
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. */
555 int
556 dpif_port_get_name(struct dpif *dpif, uint16_t port_no,
557 char *name, size_t name_size)
558 {
559 struct dpif_port port;
560 int error;
561
562 assert(name_size > 0);
563
564 error = dpif_port_query_by_number(dpif, port_no, &port);
565 if (!error) {
566 ovs_strlcpy(name, port.name, name_size);
567 dpif_port_destroy(&port);
568 } else {
569 *name = '\0';
570 }
571 return error;
572 }
573
574 /* Initializes 'dump' to begin dumping the ports in a dpif.
575 *
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 */
580 void
581 dpif_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
589 * initialized with dpif_port_dump_start(). On success, stores a new dpif_port
590 * into 'port' and returns true. On failure, returns false.
591 *
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
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(). */
599 bool
600 dpif_port_dump_next(struct dpif_port_dump *dump, struct dpif_port *port)
601 {
602 const struct dpif *dpif = dump->dpif;
603
604 if (dump->error) {
605 return false;
606 }
607
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 }
614
615 if (dump->error) {
616 dpif->dpif_class->port_dump_done(dpif, dump->state);
617 return false;
618 }
619 return true;
620 }
621
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. */
625 int
626 dpif_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);
632 }
633 return dump->error == EOF ? 0 : dump->error;
634 }
635
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. */
652 int
653 dpif_port_poll(const struct dpif *dpif, char **devnamep)
654 {
655 int error = dpif->dpif_class->port_poll(dpif, devnamep);
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. */
664 void
665 dpif_port_poll_wait(const struct dpif *dpif)
666 {
667 dpif->dpif_class->port_poll_wait(dpif);
668 }
669
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 */
673 void
674 dpif_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
690 /* Appends a human-readable representation of 'stats' to 's'. */
691 void
692 dpif_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
704 /* Deletes all flows from 'dpif'. Returns 0 if successful, otherwise a
705 * positive errno value. */
706 int
707 dpif_flow_flush(struct dpif *dpif)
708 {
709 int error;
710
711 COVERAGE_INC(dpif_flow_flush);
712
713 error = dpif->dpif_class->flow_flush(dpif);
714 log_operation(dpif, "flow_flush", error);
715 return error;
716 }
717
718 /* Queries 'dpif' for a flow entry. The flow is specified by the Netlink
719 * attributes with types OVS_KEY_ATTR_* in the 'key_len' bytes starting at
720 * 'key'.
721 *
722 * Returns 0 if successful. If no flow matches, returns ENOENT. On other
723 * failure, returns a positive errno value.
724 *
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. */
732 int
733 dpif_flow_get(const struct dpif *dpif,
734 const struct nlattr *key, size_t key_len,
735 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
736 {
737 int error;
738
739 COVERAGE_INC(dpif_flow_get);
740
741 error = dpif->dpif_class->flow_get(dpif, key, key_len, actionsp, stats);
742 if (error) {
743 if (actionsp) {
744 *actionsp = NULL;
745 }
746 if (stats) {
747 memset(stats, 0, sizeof *stats);
748 }
749 }
750 if (should_log_flow_message(error)) {
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);
763 }
764 return error;
765 }
766
767 /* Adds or modifies a flow in 'dpif'. The flow is specified by the Netlink
768 * attributes with types OVS_KEY_ATTR_* in the 'key_len' bytes starting at
769 * 'key'. The associated actions are specified by the Netlink attributes with
770 * types OVS_ACTION_ATTR_* in the 'actions_len' bytes starting at 'actions'.
771 *
772 * - If the flow's key does not exist in 'dpif', then the flow will be added if
773 * 'flags' includes DPIF_FP_CREATE. Otherwise the operation will fail with
774 * ENOENT.
775 *
776 * If the operation succeeds, then 'stats', if nonnull, will be zeroed.
777 *
778 * - If the flow's key does exist in 'dpif', then the flow's actions will be
779 * updated if 'flags' includes DPIF_FP_MODIFY. Otherwise the operation will
780 * fail with EEXIST. If the flow's actions are updated, then its statistics
781 * will be zeroed if 'flags' includes DPIF_FP_ZERO_STATS, and left as-is
782 * otherwise.
783 *
784 * If the operation succeeds, then 'stats', if nonnull, will be set to the
785 * flow's statistics before the update.
786 */
787 int
788 dpif_flow_put(struct dpif *dpif, enum dpif_flow_put_flags flags,
789 const struct nlattr *key, size_t key_len,
790 const struct nlattr *actions, size_t actions_len,
791 struct dpif_flow_stats *stats)
792 {
793 int error;
794
795 COVERAGE_INC(dpif_flow_put);
796 assert(!(flags & ~(DPIF_FP_CREATE | DPIF_FP_MODIFY | DPIF_FP_ZERO_STATS)));
797
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 }
803 if (should_log_flow_message(error)) {
804 struct ds s;
805
806 ds_init(&s);
807 ds_put_cstr(&s, "put");
808 if (flags & DPIF_FP_CREATE) {
809 ds_put_cstr(&s, "[create]");
810 }
811 if (flags & DPIF_FP_MODIFY) {
812 ds_put_cstr(&s, "[modify]");
813 }
814 if (flags & DPIF_FP_ZERO_STATS) {
815 ds_put_cstr(&s, "[zero]");
816 }
817 log_flow_message(dpif, error, ds_cstr(&s), key, key_len, stats,
818 actions, actions_len);
819 ds_destroy(&s);
820 }
821 return error;
822 }
823
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
826 * with types OVS_KEY_ATTR_* in the 'key_len' bytes starting at 'key'.
827 *
828 * If the operation succeeds, then 'stats', if nonnull, will be set to the
829 * flow's statistics before its deletion. */
830 int
831 dpif_flow_del(struct dpif *dpif,
832 const struct nlattr *key, size_t key_len,
833 struct dpif_flow_stats *stats)
834 {
835 int error;
836
837 COVERAGE_INC(dpif_flow_del);
838
839 error = dpif->dpif_class->flow_del(dpif, key, key_len, stats);
840 if (error && stats) {
841 memset(stats, 0, sizeof *stats);
842 }
843 if (should_log_flow_message(error)) {
844 log_flow_message(dpif, error, "flow_del", key, key_len,
845 !error ? stats : NULL, NULL, 0);
846 }
847 return error;
848 }
849
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 */
856 void
857 dpif_flow_dump_start(struct dpif_flow_dump *dump, const struct dpif *dpif)
858 {
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);
862 }
863
864 /* Attempts to retrieve another flow from 'dump', which must have been
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'
872 * will be set to Netlink attributes with types OVS_KEY_ATTR_* representing the
873 * dumped flow's key. If 'actions' and 'actions_len' are nonnull then they are
874 * set to Netlink attributes with types OVS_ACTION_ATTR_* representing the
875 * dumped flow's actions. If 'stats' is nonnull then it will be set to the
876 * dumped flow's statistics.
877 *
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'. */
882 bool
883 dpif_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,
886 const struct dpif_flow_stats **stats)
887 {
888 const struct dpif *dpif = dump->dpif;
889 int error = dump->error;
890
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 }
899 }
900 if (error) {
901 if (key) {
902 *key = NULL;
903 *key_len = 0;
904 }
905 if (actions) {
906 *actions = NULL;
907 *actions_len = 0;
908 }
909 if (stats) {
910 *stats = NULL;
911 }
912 }
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 }
922 }
923 dump->error = error;
924 return !error;
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. */
930 int
931 dpif_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;
939 }
940
941 /* Causes 'dpif' to perform the 'actions_len' bytes of actions in 'actions' on
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.)
946 *
947 * Returns 0 if successful, otherwise a positive errno value. */
948 int
949 dpif_execute(struct dpif *dpif,
950 const struct nlattr *key, size_t key_len,
951 const struct nlattr *actions, size_t actions_len,
952 const struct ofpbuf *buf)
953 {
954 int error;
955
956 COVERAGE_INC(dpif_execute);
957 if (actions_len > 0) {
958 error = dpif->dpif_class->execute(dpif, key, key_len,
959 actions, actions_len, buf);
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;
966 char *packet = ofp_packet_to_string(buf->data, buf->size);
967 ds_put_format(&ds, "%s: execute ", dpif_name(dpif));
968 format_odp_actions(&ds, actions, actions_len);
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
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. */
986 void
987 dpif_operate(struct dpif *dpif, union dpif_op **ops, size_t n_ops)
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++) {
997 union dpif_op *op = ops[i];
998 struct dpif_flow_put *put;
999 struct dpif_execute *execute;
1000
1001 switch (op->type) {
1002 case DPIF_OP_FLOW_PUT:
1003 put = &op->flow_put;
1004 put->error = dpif_flow_put(dpif, put->flags,
1005 put->key, put->key_len,
1006 put->actions, put->actions_len,
1007 put->stats);
1008 break;
1009
1010 case DPIF_OP_EXECUTE:
1011 execute = &op->execute;
1012 execute->error = dpif_execute(dpif, execute->key, execute->key_len,
1013 execute->actions,
1014 execute->actions_len,
1015 execute->packet);
1016 break;
1017
1018 default:
1019 NOT_REACHED();
1020 }
1021 }
1022 }
1023
1024
1025 /* Returns a string that represents 'type', for use in log messages. */
1026 const char *
1027 dpif_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";
1032 case DPIF_N_UC_TYPES: default: return "<unknown>";
1033 }
1034 }
1035
1036 /* Enables or disables receiving packets with dpif_recv() on 'dpif'. Returns 0
1037 * if successful, otherwise a positive errno value.
1038 *
1039 * Turning packet receive off and then back on may change the Netlink PID
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. */
1043 int
1044 dpif_recv_set(struct dpif *dpif, bool enable)
1045 {
1046 int error = dpif->dpif_class->recv_set(dpif, enable);
1047 log_operation(dpif, "recv_set", error);
1048 return error;
1049 }
1050
1051 /* Polls for an upcall from 'dpif'. If successful, stores the upcall into
1052 * '*upcall'. Should only be called if dpif_recv_set() has been used to enable
1053 * receiving packets on 'dpif'.
1054 *
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 *
1061 * Returns 0 if successful, otherwise a positive errno value. Returns EAGAIN
1062 * if no upcall is immediately available. */
1063 int
1064 dpif_recv(struct dpif *dpif, struct dpif_upcall *upcall)
1065 {
1066 int error = dpif->dpif_class->recv(dpif, upcall);
1067 if (!error && !VLOG_DROP_DBG(&dpmsg_rl)) {
1068 struct ds flow;
1069 char *packet;
1070
1071 packet = ofp_packet_to_string(upcall->packet->data,
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);
1083 } else if (error && error != EAGAIN) {
1084 log_operation(dpif, "recv", error);
1085 }
1086 return error;
1087 }
1088
1089 /* Discards all messages that would otherwise be received by dpif_recv() on
1090 * 'dpif'. */
1091 void
1092 dpif_recv_purge(struct dpif *dpif)
1093 {
1094 COVERAGE_INC(dpif_purge);
1095 if (dpif->dpif_class->recv_purge) {
1096 dpif->dpif_class->recv_purge(dpif);
1097 }
1098 }
1099
1100 /* Arranges for the poll loop to wake up when 'dpif' has a message queued to be
1101 * received with dpif_recv(). */
1102 void
1103 dpif_recv_wait(struct dpif *dpif)
1104 {
1105 dpif->dpif_class->recv_wait(dpif);
1106 }
1107
1108 /* Obtains the NetFlow engine type and engine ID for 'dpif' into '*engine_type'
1109 * and '*engine_id', respectively. */
1110 void
1111 dpif_get_netflow_ids(const struct dpif *dpif,
1112 uint8_t *engine_type, uint8_t *engine_id)
1113 {
1114 *engine_type = dpif->netflow_engine_type;
1115 *engine_id = dpif->netflow_engine_id;
1116 }
1117
1118 /* Translates OpenFlow queue ID 'queue_id' (in host byte order) into a priority
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'. */
1122 int
1123 dpif_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 }
1136 \f
1137 void
1138 dpif_init(struct dpif *dpif, const struct dpif_class *dpif_class,
1139 const char *name,
1140 uint8_t netflow_engine_type, uint8_t netflow_engine_id)
1141 {
1142 dpif->dpif_class = dpif_class;
1143 dpif->base_name = xstrdup(name);
1144 dpif->full_name = xasprintf("%s@%s", dpif_class->type, name);
1145 dpif->netflow_engine_type = netflow_engine_type;
1146 dpif->netflow_engine_id = netflow_engine_id;
1147 }
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. */
1155 void
1156 dpif_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) {
1162 dpif->dpif_class->close(dpif);
1163 }
1164
1165 free(base_name);
1166 free(full_name);
1167 }
1168 \f
1169 static void
1170 log_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);
1174 } else if (ofperr_is_valid(error)) {
1175 VLOG_WARN_RL(&error_rl, "%s: %s failed (%s)",
1176 dpif_name(dpif), operation, ofperr_get_name(error));
1177 } else {
1178 VLOG_WARN_RL(&error_rl, "%s: %s failed (%s)",
1179 dpif_name(dpif), operation, strerror(error));
1180 }
1181 }
1182
1183 static enum vlog_level
1184 flow_message_log_level(int error)
1185 {
1186 return error ? VLL_WARN : VLL_DBG;
1187 }
1188
1189 static bool
1190 should_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
1196 static void
1197 log_flow_message(const struct dpif *dpif, int error, const char *operation,
1198 const struct nlattr *key, size_t key_len,
1199 const struct dpif_flow_stats *stats,
1200 const struct nlattr *actions, size_t actions_len)
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 }
1211 odp_flow_key_format(key, key_len, &ds);
1212 if (stats) {
1213 ds_put_cstr(&ds, ", ");
1214 dpif_flow_stats_format(stats, &ds);
1215 }
1216 if (actions || actions_len) {
1217 ds_put_cstr(&ds, ", actions:");
1218 format_odp_actions(&ds, actions, actions_len);
1219 }
1220 vlog(THIS_MODULE, flow_message_log_level(error), "%s", ds_cstr(&ds));
1221 ds_destroy(&ds);
1222 }