2 * Copyright (c) 2008, 2009, 2010 Nicira Networks.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "dpif-provider.h"
28 #include "dynamic-string.h"
32 #include "ofp-print.h"
35 #include "poll-loop.h"
42 #define THIS_MODULE VLM_dpif
44 static const struct dpif_class *base_dpif_classes[] = {
49 struct registered_dpif_class {
50 struct dpif_class dpif_class;
53 static struct shash dpif_classes = SHASH_INITIALIZER(&dpif_classes);
55 /* Rate limit for individual messages going to or from the datapath, output at
56 * DBG level. This is very high because, if these are enabled, it is because
57 * we really need to see them. */
58 static struct vlog_rate_limit dpmsg_rl = VLOG_RATE_LIMIT_INIT(600, 600);
60 /* Not really much point in logging many dpif errors. */
61 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(60, 5);
63 static void log_operation(const struct dpif *, const char *operation,
65 static void log_flow_operation(const struct dpif *, const char *operation,
66 int error, struct odp_flow *flow);
67 static void log_flow_put(struct dpif *, int error,
68 const struct odp_flow_put *);
69 static bool should_log_flow_message(int error);
70 static void check_rw_odp_flow(struct odp_flow *);
75 static int status = -1;
81 for (i = 0; i < ARRAY_SIZE(base_dpif_classes); i++) {
82 dp_register_provider(base_dpif_classes[i]);
87 /* Performs periodic work needed by all the various kinds of dpifs.
89 * If your program opens any dpifs, it must call both this function and
90 * netdev_run() within its main poll loop. */
94 struct shash_node *node;
95 SHASH_FOR_EACH(node, &dpif_classes) {
96 const struct registered_dpif_class *registered_class = node->data;
97 if (registered_class->dpif_class.run) {
98 registered_class->dpif_class.run();
103 /* Arranges for poll_block() to wake up when dp_run() needs to be called.
105 * If your program opens any dpifs, it must call both this function and
106 * netdev_wait() within its main poll loop. */
110 struct shash_node *node;
111 SHASH_FOR_EACH(node, &dpif_classes) {
112 const struct registered_dpif_class *registered_class = node->data;
113 if (registered_class->dpif_class.wait) {
114 registered_class->dpif_class.wait();
119 /* Registers a new datapath provider. After successful registration, new
120 * datapaths of that type can be opened using dpif_open(). */
122 dp_register_provider(const struct dpif_class *new_class)
124 struct registered_dpif_class *registered_class;
126 if (shash_find(&dpif_classes, new_class->type)) {
127 VLOG_WARN("attempted to register duplicate datapath provider: %s",
132 registered_class = xmalloc(sizeof *registered_class);
133 memcpy(®istered_class->dpif_class, new_class,
134 sizeof registered_class->dpif_class);
135 registered_class->refcount = 0;
137 shash_add(&dpif_classes, new_class->type, registered_class);
142 /* Unregisters a datapath provider. 'type' must have been previously
143 * registered and not currently be in use by any dpifs. After unregistration
144 * new datapaths of that type cannot be opened using dpif_open(). */
146 dp_unregister_provider(const char *type)
148 struct shash_node *node;
149 struct registered_dpif_class *registered_class;
151 node = shash_find(&dpif_classes, type);
153 VLOG_WARN("attempted to unregister a datapath provider that is not "
154 "registered: %s", type);
158 registered_class = node->data;
159 if (registered_class->refcount) {
160 VLOG_WARN("attempted to unregister in use datapath provider: %s", type);
164 shash_delete(&dpif_classes, node);
165 free(registered_class);
170 /* Clears 'types' and enumerates the types of all currently registered datapath
171 * providers into it. The caller must first initialize the svec. */
173 dp_enumerate_types(struct svec *types)
175 struct shash_node *node;
180 SHASH_FOR_EACH(node, &dpif_classes) {
181 const struct registered_dpif_class *registered_class = node->data;
182 svec_add(types, registered_class->dpif_class.type);
186 /* Clears 'names' and enumerates the names of all known created datapaths with
187 * the given 'type'. The caller must first initialize the svec. Returns 0 if
188 * successful, otherwise a positive errno value.
190 * Some kinds of datapaths might not be practically enumerable. This is not
191 * considered an error. */
193 dp_enumerate_names(const char *type, struct svec *names)
195 const struct registered_dpif_class *registered_class;
196 const struct dpif_class *dpif_class;
202 registered_class = shash_find_data(&dpif_classes, type);
203 if (!registered_class) {
204 VLOG_WARN("could not enumerate unknown type: %s", type);
208 dpif_class = ®istered_class->dpif_class;
209 error = dpif_class->enumerate ? dpif_class->enumerate(names) : 0;
212 VLOG_WARN("failed to enumerate %s datapaths: %s", dpif_class->type,
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. */
222 dp_parse_name(const char *datapath_name_, char **name, char **type)
224 char *datapath_name = xstrdup(datapath_name_);
227 separator = strchr(datapath_name, '@');
230 *type = datapath_name;
231 *name = xstrdup(separator + 1);
233 *name = datapath_name;
239 do_open(const char *name, const char *type, bool create, struct dpif **dpifp)
241 struct dpif *dpif = NULL;
243 struct registered_dpif_class *registered_class;
247 if (!type || *type == '\0') {
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,
255 error = EAFNOSUPPORT;
259 error = registered_class->dpif_class.open(name, type, create, &dpif);
261 registered_class->refcount++;
265 *dpifp = error ? NULL : dpif;
269 /* Tries to open an existing datapath named 'name' and type 'type'. Will fail
270 * if no datapath with 'name' and 'type' exists. 'type' may be either NULL or
271 * the empty string to specify the default system type. Returns 0 if
272 * successful, otherwise a positive errno value. On success stores a pointer
273 * to the datapath in '*dpifp', otherwise a null pointer. */
275 dpif_open(const char *name, const char *type, struct dpif **dpifp)
277 return do_open(name, type, false, dpifp);
280 /* Tries to create and open a new datapath with the given 'name' and 'type'.
281 * 'type' may be either NULL or the empty string to specify the default system
282 * type. Will fail if a datapath with 'name' and 'type' already exists.
283 * Returns 0 if successful, otherwise a positive errno value. On success
284 * stores a pointer to the datapath in '*dpifp', otherwise a null pointer. */
286 dpif_create(const char *name, const char *type, struct dpif **dpifp)
288 return do_open(name, type, true, dpifp);
291 /* Tries to open a datapath with the given 'name' and 'type', creating it if it
292 * does not exist. 'type' may be either NULL or the empty string to specify
293 * the default system type. Returns 0 if successful, otherwise a positive
294 * errno value. On success stores a pointer to the datapath in '*dpifp',
295 * otherwise a null pointer. */
297 dpif_create_and_open(const char *name, const char *type, struct dpif **dpifp)
301 error = dpif_create(name, type, dpifp);
302 if (error == EEXIST || error == EBUSY) {
303 error = dpif_open(name, type, dpifp);
305 VLOG_WARN("datapath %s already exists but cannot be opened: %s",
306 name, strerror(error));
309 VLOG_WARN("failed to create datapath %s: %s", name, strerror(error));
314 /* Closes and frees the connection to 'dpif'. Does not destroy the datapath
315 * itself; call dpif_delete() first, instead, if that is desirable. */
317 dpif_close(struct dpif *dpif)
320 struct registered_dpif_class *registered_class;
322 registered_class = shash_find_data(&dpif_classes,
323 dpif->dpif_class->type);
324 assert(registered_class);
325 assert(registered_class->refcount);
327 registered_class->refcount--;
328 dpif_uninit(dpif, true);
332 /* Returns the name of datapath 'dpif' prefixed with the type
333 * (for use in log messages). */
335 dpif_name(const struct dpif *dpif)
337 return dpif->full_name;
340 /* Returns the name of datapath 'dpif' without the type
341 * (for use in device names). */
343 dpif_base_name(const struct dpif *dpif)
345 return dpif->base_name;
348 /* Enumerates all names that may be used to open 'dpif' into 'all_names'. The
349 * Linux datapath, for example, supports opening a datapath both by number,
350 * e.g. "dp0", and by the name of the datapath's local port. For some
351 * datapaths, this might be an infinite set (e.g. in a file name, slashes may
352 * be duplicated any number of times), in which case only the names most likely
353 * to be used will be enumerated.
355 * The caller must already have initialized 'all_names'. Any existing names in
356 * 'all_names' will not be disturbed. */
358 dpif_get_all_names(const struct dpif *dpif, struct svec *all_names)
360 if (dpif->dpif_class->get_all_names) {
361 int error = dpif->dpif_class->get_all_names(dpif, all_names);
363 VLOG_WARN_RL(&error_rl,
364 "failed to retrieve names for datpath %s: %s",
365 dpif_name(dpif), strerror(error));
369 svec_add(all_names, dpif_base_name(dpif));
374 /* Destroys the datapath that 'dpif' is connected to, first removing all of its
375 * ports. After calling this function, it does not make sense to pass 'dpif'
376 * to any functions other than dpif_name() or dpif_close(). */
378 dpif_delete(struct dpif *dpif)
382 COVERAGE_INC(dpif_destroy);
384 error = dpif->dpif_class->destroy(dpif);
385 log_operation(dpif, "delete", error);
389 /* Retrieves statistics for 'dpif' into 'stats'. Returns 0 if successful,
390 * otherwise a positive errno value. */
392 dpif_get_dp_stats(const struct dpif *dpif, struct odp_stats *stats)
394 int error = dpif->dpif_class->get_stats(dpif, stats);
396 memset(stats, 0, sizeof *stats);
398 log_operation(dpif, "get_stats", error);
402 /* Retrieves the current IP fragment handling policy for 'dpif' into
403 * '*drop_frags': true indicates that fragments are dropped, false indicates
404 * that fragments are treated in the same way as other IP packets (except that
405 * the L4 header cannot be read). Returns 0 if successful, otherwise a
406 * positive errno value. */
408 dpif_get_drop_frags(const struct dpif *dpif, bool *drop_frags)
410 int error = dpif->dpif_class->get_drop_frags(dpif, drop_frags);
414 log_operation(dpif, "get_drop_frags", error);
418 /* Changes 'dpif''s treatment of IP fragments to 'drop_frags', whose meaning is
419 * the same as for the get_drop_frags member function. Returns 0 if
420 * successful, otherwise a positive errno value. */
422 dpif_set_drop_frags(struct dpif *dpif, bool drop_frags)
424 int error = dpif->dpif_class->set_drop_frags(dpif, drop_frags);
425 log_operation(dpif, "set_drop_frags", error);
429 /* Attempts to add 'devname' as a port on 'dpif', given the combination of
430 * ODP_PORT_* flags in 'flags'. If successful, returns 0 and sets '*port_nop'
431 * to the new port's port number (if 'port_nop' is non-null). On failure,
432 * returns a positive errno value and sets '*port_nop' to UINT16_MAX (if
433 * 'port_nop' is non-null). */
435 dpif_port_add(struct dpif *dpif, const char *devname, uint16_t flags,
441 COVERAGE_INC(dpif_port_add);
443 error = dpif->dpif_class->port_add(dpif, devname, flags, &port_no);
445 VLOG_DBG_RL(&dpmsg_rl, "%s: added %s as port %"PRIu16,
446 dpif_name(dpif), devname, port_no);
448 VLOG_WARN_RL(&error_rl, "%s: failed to add %s as port: %s",
449 dpif_name(dpif), devname, strerror(error));
450 port_no = UINT16_MAX;
458 /* Attempts to remove 'dpif''s port number 'port_no'. Returns 0 if successful,
459 * otherwise a positive errno value. */
461 dpif_port_del(struct dpif *dpif, uint16_t port_no)
465 COVERAGE_INC(dpif_port_del);
467 error = dpif->dpif_class->port_del(dpif, port_no);
468 log_operation(dpif, "port_del", error);
472 /* Looks up port number 'port_no' in 'dpif'. On success, returns 0 and
473 * initializes '*port' appropriately; on failure, returns a positive errno
476 dpif_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
477 struct odp_port *port)
479 int error = dpif->dpif_class->port_query_by_number(dpif, port_no, port);
481 VLOG_DBG_RL(&dpmsg_rl, "%s: port %"PRIu16" is device %s",
482 dpif_name(dpif), port_no, port->devname);
484 memset(port, 0, sizeof *port);
485 VLOG_WARN_RL(&error_rl, "%s: failed to query port %"PRIu16": %s",
486 dpif_name(dpif), port_no, strerror(error));
491 /* Looks up port named 'devname' in 'dpif'. On success, returns 0 and
492 * initializes '*port' appropriately; on failure, returns a positive errno
495 dpif_port_query_by_name(const struct dpif *dpif, const char *devname,
496 struct odp_port *port)
498 int error = dpif->dpif_class->port_query_by_name(dpif, devname, port);
500 VLOG_DBG_RL(&dpmsg_rl, "%s: device %s is on port %"PRIu16,
501 dpif_name(dpif), devname, port->port);
503 memset(port, 0, sizeof *port);
505 /* Log level is DBG here because all the current callers are interested
506 * in whether 'dpif' actually has a port 'devname', so that it's not an
507 * issue worth logging if it doesn't. */
508 VLOG_DBG_RL(&error_rl, "%s: failed to query port %s: %s",
509 dpif_name(dpif), devname, strerror(error));
514 /* Looks up port number 'port_no' in 'dpif'. On success, returns 0 and copies
515 * the port's name into the 'name_size' bytes in 'name', ensuring that the
516 * result is null-terminated. On failure, returns a positive errno value and
517 * makes 'name' the empty string. */
519 dpif_port_get_name(struct dpif *dpif, uint16_t port_no,
520 char *name, size_t name_size)
522 struct odp_port port;
525 assert(name_size > 0);
527 error = dpif_port_query_by_number(dpif, port_no, &port);
529 ovs_strlcpy(name, port.devname, name_size);
536 /* Obtains a list of all the ports in 'dpif'.
538 * If successful, returns 0 and sets '*portsp' to point to an array of
539 * appropriately initialized port structures and '*n_portsp' to the number of
540 * ports in the array. The caller is responsible for freeing '*portp' by
543 * On failure, returns a positive errno value and sets '*portsp' to NULL and
544 * '*n_portsp' to 0. */
546 dpif_port_list(const struct dpif *dpif,
547 struct odp_port **portsp, size_t *n_portsp)
549 struct odp_port *ports;
554 struct odp_stats stats;
557 error = dpif_get_dp_stats(dpif, &stats);
562 ports = xcalloc(stats.n_ports, sizeof *ports);
563 retval = dpif->dpif_class->port_list(dpif, ports, stats.n_ports);
569 } else if (retval <= stats.n_ports) {
575 /* Soft error: port count increased behind our back. Try again. */
588 log_operation(dpif, "port_list", error);
592 /* Polls for changes in the set of ports in 'dpif'. If the set of ports in
593 * 'dpif' has changed, this function does one of the following:
595 * - Stores the name of the device that was added to or deleted from 'dpif' in
596 * '*devnamep' and returns 0. The caller is responsible for freeing
597 * '*devnamep' (with free()) when it no longer needs it.
599 * - Returns ENOBUFS and sets '*devnamep' to NULL.
601 * This function may also return 'false positives', where it returns 0 and
602 * '*devnamep' names a device that was not actually added or deleted or it
603 * returns ENOBUFS without any change.
605 * Returns EAGAIN if the set of ports in 'dpif' has not changed. May also
606 * return other positive errno values to indicate that something has gone
609 dpif_port_poll(const struct dpif *dpif, char **devnamep)
611 int error = dpif->dpif_class->port_poll(dpif, devnamep);
618 /* Arranges for the poll loop to wake up when port_poll(dpif) will return a
619 * value other than EAGAIN. */
621 dpif_port_poll_wait(const struct dpif *dpif)
623 dpif->dpif_class->port_poll_wait(dpif);
626 /* Retrieves a list of the port numbers in port group 'group' in 'dpif'.
628 * On success, returns 0 and points '*ports' to a newly allocated array of
629 * integers, each of which is a 'dpif' port number for a port in
630 * 'group'. Stores the number of elements in the array in '*n_ports'. The
631 * caller is responsible for freeing '*ports' by calling free().
633 * On failure, returns a positive errno value and sets '*ports' to NULL and
634 * '*n_ports' to 0. */
636 dpif_port_group_get(const struct dpif *dpif, uint16_t group,
637 uint16_t **ports, size_t *n_ports)
644 int retval = dpif->dpif_class->port_group_get(dpif, group,
653 } else if (retval <= *n_ports) {
659 /* Soft error: there were more ports than we expected in the
660 * group. Try again. */
662 *ports = xcalloc(retval, sizeof **ports);
666 log_operation(dpif, "port_group_get", error);
670 /* Updates port group 'group' in 'dpif', making it contain the 'n_ports' ports
671 * whose 'dpif' port numbers are given in 'n_ports'. Returns 0 if
672 * successful, otherwise a positive errno value.
674 * Behavior is undefined if the values in ports[] are not unique. */
676 dpif_port_group_set(struct dpif *dpif, uint16_t group,
677 const uint16_t ports[], size_t n_ports)
681 COVERAGE_INC(dpif_port_group_set);
683 error = dpif->dpif_class->port_group_set(dpif, group, ports, n_ports);
684 log_operation(dpif, "port_group_set", error);
688 /* Deletes all flows from 'dpif'. Returns 0 if successful, otherwise a
689 * positive errno value. */
691 dpif_flow_flush(struct dpif *dpif)
695 COVERAGE_INC(dpif_flow_flush);
697 error = dpif->dpif_class->flow_flush(dpif);
698 log_operation(dpif, "flow_flush", error);
702 /* Queries 'dpif' for a flow entry matching 'flow->key'.
704 * If a flow matching 'flow->key' exists in 'dpif', stores statistics for the
705 * flow into 'flow->stats'. If 'flow->n_actions' is zero, then 'flow->actions'
706 * is ignored. If 'flow->n_actions' is nonzero, then 'flow->actions' should
707 * point to an array of the specified number of actions. At most that many of
708 * the flow's actions will be copied into that array. 'flow->n_actions' will
709 * be updated to the number of actions actually present in the flow, which may
710 * be greater than the number stored if the flow has more actions than space
711 * available in the array.
713 * If no flow matching 'flow->key' exists in 'dpif', returns ENOENT. On other
714 * failure, returns a positive errno value. */
716 dpif_flow_get(const struct dpif *dpif, struct odp_flow *flow)
720 COVERAGE_INC(dpif_flow_get);
722 check_rw_odp_flow(flow);
723 error = dpif->dpif_class->flow_get(dpif, flow, 1);
725 error = flow->stats.error;
728 /* Make the results predictable on error. */
729 memset(&flow->stats, 0, sizeof flow->stats);
732 if (should_log_flow_message(error)) {
733 log_flow_operation(dpif, "flow_get", error, flow);
738 /* For each flow 'flow' in the 'n' flows in 'flows':
740 * - If a flow matching 'flow->key' exists in 'dpif':
742 * Stores 0 into 'flow->stats.error' and stores statistics for the flow
743 * into 'flow->stats'.
745 * If 'flow->n_actions' is zero, then 'flow->actions' is ignored. If
746 * 'flow->n_actions' is nonzero, then 'flow->actions' should point to an
747 * array of the specified number of actions. At most that many of the
748 * flow's actions will be copied into that array. 'flow->n_actions' will
749 * be updated to the number of actions actually present in the flow, which
750 * may be greater than the number stored if the flow has more actions than
751 * space available in the array.
753 * - Flow-specific errors are indicated by a positive errno value in
754 * 'flow->stats.error'. In particular, ENOENT indicates that no flow
755 * matching 'flow->key' exists in 'dpif'. When an error value is stored, the
756 * contents of 'flow->key' are preserved but other members of 'flow' should
757 * be treated as indeterminate.
759 * Returns 0 if all 'n' flows in 'flows' were updated (whether they were
760 * individually successful or not is indicated by 'flow->stats.error',
761 * however). Returns a positive errno value if an error that prevented this
762 * update occurred, in which the caller must not depend on any elements in
763 * 'flows' being updated or not updated.
766 dpif_flow_get_multiple(const struct dpif *dpif,
767 struct odp_flow flows[], size_t n)
772 COVERAGE_ADD(dpif_flow_get, n);
774 for (i = 0; i < n; i++) {
775 check_rw_odp_flow(&flows[i]);
778 error = dpif->dpif_class->flow_get(dpif, flows, n);
779 log_operation(dpif, "flow_get_multiple", error);
783 /* Adds or modifies a flow in 'dpif' as specified in 'put':
785 * - If the flow specified in 'put->flow' does not exist in 'dpif', then
786 * behavior depends on whether ODPPF_CREATE is specified in 'put->flags': if
787 * it is, the flow will be added, otherwise the operation will fail with
790 * - Otherwise, the flow specified in 'put->flow' does exist in 'dpif'.
791 * Behavior in this case depends on whether ODPPF_MODIFY is specified in
792 * 'put->flags': if it is, the flow's actions will be updated, otherwise the
793 * operation will fail with EEXIST. If the flow's actions are updated, then
794 * its statistics will be zeroed if ODPPF_ZERO_STATS is set in 'put->flags',
795 * left as-is otherwise.
797 * Returns 0 if successful, otherwise a positive errno value.
800 dpif_flow_put(struct dpif *dpif, struct odp_flow_put *put)
804 COVERAGE_INC(dpif_flow_put);
806 error = dpif->dpif_class->flow_put(dpif, put);
807 if (should_log_flow_message(error)) {
808 log_flow_put(dpif, error, put);
813 /* Deletes a flow matching 'flow->key' from 'dpif' or returns ENOENT if 'dpif'
814 * does not contain such a flow.
816 * If successful, updates 'flow->stats', 'flow->n_actions', and 'flow->actions'
817 * as described for dpif_flow_get(). */
819 dpif_flow_del(struct dpif *dpif, struct odp_flow *flow)
823 COVERAGE_INC(dpif_flow_del);
825 check_rw_odp_flow(flow);
826 memset(&flow->stats, 0, sizeof flow->stats);
828 error = dpif->dpif_class->flow_del(dpif, flow);
829 if (should_log_flow_message(error)) {
830 log_flow_operation(dpif, "delete flow", error, flow);
835 /* Stores up to 'n' flows in 'dpif' into 'flows', including their statistics
836 * but not including any information about their actions. If successful,
837 * returns 0 and sets '*n_out' to the number of flows actually present in
838 * 'dpif', which might be greater than the number stored (if 'dpif' has more
839 * than 'n' flows). On failure, returns a negative errno value and sets
842 dpif_flow_list(const struct dpif *dpif, struct odp_flow flows[], size_t n,
848 COVERAGE_INC(dpif_flow_query_list);
849 if (RUNNING_ON_VALGRIND) {
850 memset(flows, 0, n * sizeof *flows);
852 for (i = 0; i < n; i++) {
853 flows[i].actions = NULL;
854 flows[i].n_actions = 0;
857 retval = dpif->dpif_class->flow_list(dpif, flows, n);
860 VLOG_WARN_RL(&error_rl, "%s: flow list failed (%s)",
861 dpif_name(dpif), strerror(-retval));
864 COVERAGE_ADD(dpif_flow_query_list_n, retval);
865 *n_out = MIN(n, retval);
866 VLOG_DBG_RL(&dpmsg_rl, "%s: listed %zu flows (of %d)",
867 dpif_name(dpif), *n_out, retval);
872 /* Retrieves all of the flows in 'dpif'.
874 * If successful, returns 0 and stores in '*flowsp' a pointer to a newly
875 * allocated array of flows, including their statistics but not including any
876 * information about their actions, and sets '*np' to the number of flows in
877 * '*flowsp'. The caller is responsible for freeing '*flowsp' by calling
880 * On failure, returns a positive errno value and sets '*flowsp' to NULL and
883 dpif_flow_list_all(const struct dpif *dpif,
884 struct odp_flow **flowsp, size_t *np)
886 struct odp_stats stats;
887 struct odp_flow *flows;
894 error = dpif_get_dp_stats(dpif, &stats);
899 flows = xmalloc(sizeof *flows * stats.n_flows);
900 error = dpif_flow_list(dpif, flows, stats.n_flows, &n_flows);
906 if (stats.n_flows != n_flows) {
907 VLOG_WARN_RL(&error_rl, "%s: datapath stats reported %"PRIu32" "
908 "flows but flow listing reported %zu",
909 dpif_name(dpif), stats.n_flows, n_flows);
916 /* Causes 'dpif' to perform the 'n_actions' actions in 'actions' on the
917 * Ethernet frame specified in 'packet'.
919 * Pretends that the frame was originally received on the port numbered
920 * 'in_port'. This affects only ODPAT_OUTPUT_GROUP actions, which will not
921 * send a packet out their input port. Specify the number of an unused port
922 * (e.g. UINT16_MAX is currently always unused) to avoid this behavior.
924 * Returns 0 if successful, otherwise a positive errno value. */
926 dpif_execute(struct dpif *dpif, uint16_t in_port,
927 const union odp_action actions[], size_t n_actions,
928 const struct ofpbuf *buf)
932 COVERAGE_INC(dpif_execute);
934 error = dpif->dpif_class->execute(dpif, in_port, actions,
940 if (!(error ? VLOG_DROP_WARN(&error_rl) : VLOG_DROP_DBG(&dpmsg_rl))) {
941 struct ds ds = DS_EMPTY_INITIALIZER;
942 char *packet = ofp_packet_to_string(buf->data, buf->size, buf->size);
943 ds_put_format(&ds, "%s: execute ", dpif_name(dpif));
944 format_odp_actions(&ds, actions, n_actions);
946 ds_put_format(&ds, " failed (%s)", strerror(error));
948 ds_put_format(&ds, " on packet %s", packet);
949 vlog(THIS_MODULE, error ? VLL_WARN : VLL_DBG, "%s", ds_cstr(&ds));
956 /* Retrieves 'dpif''s "listen mask" into '*listen_mask'. Each ODPL_* bit set
957 * in '*listen_mask' indicates that dpif_recv() will receive messages of that
958 * type. Returns 0 if successful, otherwise a positive errno value. */
960 dpif_recv_get_mask(const struct dpif *dpif, int *listen_mask)
962 int error = dpif->dpif_class->recv_get_mask(dpif, listen_mask);
966 log_operation(dpif, "recv_get_mask", error);
970 /* Sets 'dpif''s "listen mask" to 'listen_mask'. Each ODPL_* bit set in
971 * '*listen_mask' requests that dpif_recv() receive messages of that type.
972 * Returns 0 if successful, otherwise a positive errno value. */
974 dpif_recv_set_mask(struct dpif *dpif, int listen_mask)
976 int error = dpif->dpif_class->recv_set_mask(dpif, listen_mask);
977 log_operation(dpif, "recv_set_mask", error);
981 /* Retrieve the sFlow sampling probability. '*probability' is expressed as the
982 * number of packets out of UINT_MAX to sample, e.g. probability/UINT_MAX is
983 * the probability of sampling a given packet.
985 * Returns 0 if successful, otherwise a positive errno value. EOPNOTSUPP
986 * indicates that 'dpif' does not support sFlow sampling. */
988 dpif_get_sflow_probability(const struct dpif *dpif, uint32_t *probability)
990 int error = (dpif->dpif_class->get_sflow_probability
991 ? dpif->dpif_class->get_sflow_probability(dpif, probability)
996 log_operation(dpif, "get_sflow_probability", error);
1000 /* Set the sFlow sampling probability. 'probability' is expressed as the
1001 * number of packets out of UINT_MAX to sample, e.g. probability/UINT_MAX is
1002 * the probability of sampling a given packet.
1004 * Returns 0 if successful, otherwise a positive errno value. EOPNOTSUPP
1005 * indicates that 'dpif' does not support sFlow sampling. */
1007 dpif_set_sflow_probability(struct dpif *dpif, uint32_t probability)
1009 int error = (dpif->dpif_class->set_sflow_probability
1010 ? dpif->dpif_class->set_sflow_probability(dpif, probability)
1012 log_operation(dpif, "set_sflow_probability", error);
1016 /* Attempts to receive a message from 'dpif'. If successful, stores the
1017 * message into '*packetp'. The message, if one is received, will begin with
1018 * 'struct odp_msg' as a header, and will have at least DPIF_RECV_MSG_PADDING
1019 * bytes of headroom. Only messages of the types selected with
1020 * dpif_set_listen_mask() will ordinarily be received (but if a message type is
1021 * enabled and then later disabled, some stragglers might pop up).
1023 * Returns 0 if successful, otherwise a positive errno value. Returns EAGAIN
1024 * if no message is immediately available. */
1026 dpif_recv(struct dpif *dpif, struct ofpbuf **packetp)
1028 int error = dpif->dpif_class->recv(dpif, packetp);
1030 struct ofpbuf *buf = *packetp;
1032 assert(ofpbuf_headroom(buf) >= DPIF_RECV_MSG_PADDING);
1033 if (VLOG_IS_DBG_ENABLED()) {
1034 struct odp_msg *msg = buf->data;
1035 void *payload = msg + 1;
1036 size_t payload_len = buf->size - sizeof *msg;
1037 char *s = ofp_packet_to_string(payload, payload_len, payload_len);
1038 VLOG_DBG_RL(&dpmsg_rl, "%s: received %s message of length "
1039 "%zu on port %"PRIu16": %s", dpif_name(dpif),
1040 (msg->type == _ODPL_MISS_NR ? "miss"
1041 : msg->type == _ODPL_ACTION_NR ? "action"
1042 : msg->type == _ODPL_SFLOW_NR ? "sFlow"
1044 payload_len, msg->port, s);
1053 /* Discards all messages that would otherwise be received by dpif_recv() on
1054 * 'dpif'. Returns 0 if successful, otherwise a positive errno value. */
1056 dpif_recv_purge(struct dpif *dpif)
1058 struct odp_stats stats;
1062 COVERAGE_INC(dpif_purge);
1064 error = dpif_get_dp_stats(dpif, &stats);
1069 for (i = 0; i < stats.max_miss_queue + stats.max_action_queue + stats.max_sflow_queue; i++) {
1071 error = dpif_recv(dpif, &buf);
1073 return error == EAGAIN ? 0 : error;
1080 /* Arranges for the poll loop to wake up when 'dpif' has a message queued to be
1081 * received with dpif_recv(). */
1083 dpif_recv_wait(struct dpif *dpif)
1085 dpif->dpif_class->recv_wait(dpif);
1088 /* Obtains the NetFlow engine type and engine ID for 'dpif' into '*engine_type'
1089 * and '*engine_id', respectively. */
1091 dpif_get_netflow_ids(const struct dpif *dpif,
1092 uint8_t *engine_type, uint8_t *engine_id)
1094 *engine_type = dpif->netflow_engine_type;
1095 *engine_id = dpif->netflow_engine_id;
1099 dpif_init(struct dpif *dpif, const struct dpif_class *dpif_class,
1101 uint8_t netflow_engine_type, uint8_t netflow_engine_id)
1103 dpif->dpif_class = dpif_class;
1104 dpif->base_name = xstrdup(name);
1105 dpif->full_name = xasprintf("%s@%s", dpif_class->type, name);
1106 dpif->netflow_engine_type = netflow_engine_type;
1107 dpif->netflow_engine_id = netflow_engine_id;
1110 /* Undoes the results of initialization.
1112 * Normally this function only needs to be called from dpif_close().
1113 * However, it may be called by providers due to an error on opening
1114 * that occurs after initialization. It this case dpif_close() would
1115 * never be called. */
1117 dpif_uninit(struct dpif *dpif, bool close)
1119 char *base_name = dpif->base_name;
1120 char *full_name = dpif->full_name;
1123 dpif->dpif_class->close(dpif);
1131 log_operation(const struct dpif *dpif, const char *operation, int error)
1134 VLOG_DBG_RL(&dpmsg_rl, "%s: %s success", dpif_name(dpif), operation);
1136 VLOG_WARN_RL(&error_rl, "%s: %s failed (%s)",
1137 dpif_name(dpif), operation, strerror(error));
1141 static enum vlog_level
1142 flow_message_log_level(int error)
1144 return error ? VLL_WARN : VLL_DBG;
1148 should_log_flow_message(int error)
1150 return !vlog_should_drop(THIS_MODULE, flow_message_log_level(error),
1151 error ? &error_rl : &dpmsg_rl);
1155 log_flow_message(const struct dpif *dpif, int error, const char *operation,
1156 const flow_t *flow, const struct odp_flow_stats *stats,
1157 const union odp_action *actions, size_t n_actions)
1159 struct ds ds = DS_EMPTY_INITIALIZER;
1160 ds_put_format(&ds, "%s: ", dpif_name(dpif));
1162 ds_put_cstr(&ds, "failed to ");
1164 ds_put_format(&ds, "%s ", operation);
1166 ds_put_format(&ds, "(%s) ", strerror(error));
1168 flow_format(&ds, flow);
1170 ds_put_cstr(&ds, ", ");
1171 format_odp_flow_stats(&ds, stats);
1173 if (actions || n_actions) {
1174 ds_put_cstr(&ds, ", actions:");
1175 format_odp_actions(&ds, actions, n_actions);
1177 vlog(THIS_MODULE, flow_message_log_level(error), "%s", ds_cstr(&ds));
1182 log_flow_operation(const struct dpif *dpif, const char *operation, int error,
1183 struct odp_flow *flow)
1186 flow->n_actions = 0;
1188 log_flow_message(dpif, error, operation, &flow->key,
1189 !error ? &flow->stats : NULL,
1190 flow->actions, flow->n_actions);
1194 log_flow_put(struct dpif *dpif, int error, const struct odp_flow_put *put)
1196 enum { ODPPF_ALL = ODPPF_CREATE | ODPPF_MODIFY | ODPPF_ZERO_STATS };
1200 ds_put_cstr(&s, "put");
1201 if (put->flags & ODPPF_CREATE) {
1202 ds_put_cstr(&s, "[create]");
1204 if (put->flags & ODPPF_MODIFY) {
1205 ds_put_cstr(&s, "[modify]");
1207 if (put->flags & ODPPF_ZERO_STATS) {
1208 ds_put_cstr(&s, "[zero]");
1210 if (put->flags & ~ODPPF_ALL) {
1211 ds_put_format(&s, "[%x]", put->flags & ~ODPPF_ALL);
1213 log_flow_message(dpif, error, ds_cstr(&s), &put->flow.key,
1214 !error ? &put->flow.stats : NULL,
1215 put->flow.actions, put->flow.n_actions);
1219 /* There is a tendency to construct odp_flow objects on the stack and to
1220 * forget to properly initialize their "actions" and "n_actions" members.
1221 * When this happens, we get memory corruption because the kernel
1222 * writes through the random pointer that is in the "actions" member.
1224 * This function attempts to combat the problem by:
1226 * - Forcing a segfault if "actions" points to an invalid region (instead
1227 * of just getting back EFAULT, which can be easily missed in the log).
1229 * - Storing a distinctive value that is likely to cause an
1230 * easy-to-identify error later if it is dereferenced, etc.
1232 * - Triggering a warning on uninitialized memory from Valgrind if
1233 * "actions" or "n_actions" was not initialized.
1236 check_rw_odp_flow(struct odp_flow *flow)
1238 if (flow->n_actions) {
1239 memset(&flow->actions[0], 0xcc, sizeof flow->actions[0]);