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 "xfif-provider.h"
28 #include "dynamic-string.h"
31 #include "xflow-util.h"
32 #include "ofp-print.h"
36 #include "poll-loop.h"
43 VLOG_DEFINE_THIS_MODULE(xfif)
45 static const struct xfif_class *base_xfif_classes[] = {
52 struct registered_xfif_class {
53 struct xfif_class xfif_class;
56 static struct shash xfif_classes = SHASH_INITIALIZER(&xfif_classes);
58 /* Rate limit for individual messages going to or from the datapath, output at
59 * DBG level. This is very high because, if these are enabled, it is because
60 * we really need to see them. */
61 static struct vlog_rate_limit dpmsg_rl = VLOG_RATE_LIMIT_INIT(600, 600);
63 /* Not really much point in logging many xfif errors. */
64 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(60, 5);
66 static void log_operation(const struct xfif *, const char *operation,
68 static void log_flow_operation(const struct xfif *, const char *operation,
69 int error, struct xflow_flow *flow);
70 static void log_flow_put(struct xfif *, int error,
71 const struct xflow_flow_put *);
72 static bool should_log_flow_message(int error);
73 static void check_rw_xflow_flow(struct xflow_flow *);
78 static int status = -1;
84 for (i = 0; i < ARRAY_SIZE(base_xfif_classes); i++) {
85 xf_register_provider(base_xfif_classes[i]);
90 /* Performs periodic work needed by all the various kinds of xfifs.
92 * If your program opens any xfifs, it must call both this function and
93 * netdev_run() within its main poll loop. */
97 struct shash_node *node;
98 SHASH_FOR_EACH(node, &xfif_classes) {
99 const struct registered_xfif_class *registered_class = node->data;
100 if (registered_class->xfif_class.run) {
101 registered_class->xfif_class.run();
106 /* Arranges for poll_block() to wake up when xf_run() needs to be called.
108 * If your program opens any xfifs, it must call both this function and
109 * netdev_wait() within its main poll loop. */
113 struct shash_node *node;
114 SHASH_FOR_EACH(node, &xfif_classes) {
115 const struct registered_xfif_class *registered_class = node->data;
116 if (registered_class->xfif_class.wait) {
117 registered_class->xfif_class.wait();
122 /* Registers a new datapath provider. After successful registration, new
123 * datapaths of that type can be opened using xfif_open(). */
125 xf_register_provider(const struct xfif_class *new_class)
127 struct registered_xfif_class *registered_class;
129 if (shash_find(&xfif_classes, new_class->type)) {
130 VLOG_WARN("attempted to register duplicate datapath provider: %s",
135 registered_class = xmalloc(sizeof *registered_class);
136 memcpy(®istered_class->xfif_class, new_class,
137 sizeof registered_class->xfif_class);
138 registered_class->refcount = 0;
140 shash_add(&xfif_classes, new_class->type, registered_class);
145 /* Unregisters a datapath provider. 'type' must have been previously
146 * registered and not currently be in use by any xfifs. After unregistration
147 * new datapaths of that type cannot be opened using xfif_open(). */
149 xf_unregister_provider(const char *type)
151 struct shash_node *node;
152 struct registered_xfif_class *registered_class;
154 node = shash_find(&xfif_classes, type);
156 VLOG_WARN("attempted to unregister a datapath provider that is not "
157 "registered: %s", type);
161 registered_class = node->data;
162 if (registered_class->refcount) {
163 VLOG_WARN("attempted to unregister in use datapath provider: %s", type);
167 shash_delete(&xfif_classes, node);
168 free(registered_class);
173 /* Clears 'types' and enumerates the types of all currently registered datapath
174 * providers into it. The caller must first initialize the svec. */
176 xf_enumerate_types(struct svec *types)
178 struct shash_node *node;
183 SHASH_FOR_EACH(node, &xfif_classes) {
184 const struct registered_xfif_class *registered_class = node->data;
185 svec_add(types, registered_class->xfif_class.type);
189 /* Clears 'names' and enumerates the names of all known created datapaths with
190 * the given 'type'. The caller must first initialize the svec. Returns 0 if
191 * successful, otherwise a positive errno value.
193 * Some kinds of datapaths might not be practically enumerable. This is not
194 * considered an error. */
196 xf_enumerate_names(const char *type, struct svec *names)
198 const struct registered_xfif_class *registered_class;
199 const struct xfif_class *xfif_class;
205 registered_class = shash_find_data(&xfif_classes, type);
206 if (!registered_class) {
207 VLOG_WARN("could not enumerate unknown type: %s", type);
211 xfif_class = ®istered_class->xfif_class;
212 error = xfif_class->enumerate ? xfif_class->enumerate(names) : 0;
215 VLOG_WARN("failed to enumerate %s datapaths: %s", xfif_class->type,
222 /* Parses 'datapath name', which is of the form type@name into its
223 * component pieces. 'name' and 'type' must be freed by the caller. */
225 xf_parse_name(const char *datapath_name_, char **name, char **type)
227 char *datapath_name = xstrdup(datapath_name_);
230 separator = strchr(datapath_name, '@');
233 *type = datapath_name;
234 *name = xstrdup(separator + 1);
236 *name = datapath_name;
242 do_open(const char *name, const char *type, bool create, struct xfif **xfifp)
244 struct xfif *xfif = NULL;
246 struct registered_xfif_class *registered_class;
250 if (!type || *type == '\0') {
254 registered_class = shash_find_data(&xfif_classes, type);
255 if (!registered_class) {
256 VLOG_WARN("could not create datapath %s of unknown type %s", name,
258 error = EAFNOSUPPORT;
262 error = registered_class->xfif_class.open(name, type, create, &xfif);
264 registered_class->refcount++;
268 *xfifp = error ? NULL : xfif;
272 /* Tries to open an existing datapath named 'name' and type 'type'. Will fail
273 * if no datapath with 'name' and 'type' exists. 'type' may be either NULL or
274 * the empty string to specify the default system type. Returns 0 if
275 * successful, otherwise a positive errno value. On success stores a pointer
276 * to the datapath in '*xfifp', otherwise a null pointer. */
278 xfif_open(const char *name, const char *type, struct xfif **xfifp)
280 return do_open(name, type, false, xfifp);
283 /* Tries to create and open a new datapath with the given 'name' and 'type'.
284 * 'type' may be either NULL or the empty string to specify the default system
285 * type. Will fail if a datapath with 'name' and 'type' already exists.
286 * Returns 0 if successful, otherwise a positive errno value. On success
287 * stores a pointer to the datapath in '*xfifp', otherwise a null pointer. */
289 xfif_create(const char *name, const char *type, struct xfif **xfifp)
291 return do_open(name, type, true, xfifp);
294 /* Tries to open a datapath with the given 'name' and 'type', creating it if it
295 * does not exist. 'type' may be either NULL or the empty string to specify
296 * the default system type. Returns 0 if successful, otherwise a positive
297 * errno value. On success stores a pointer to the datapath in '*xfifp',
298 * otherwise a null pointer. */
300 xfif_create_and_open(const char *name, const char *type, struct xfif **xfifp)
304 error = xfif_create(name, type, xfifp);
305 if (error == EEXIST || error == EBUSY) {
306 error = xfif_open(name, type, xfifp);
308 VLOG_WARN("datapath %s already exists but cannot be opened: %s",
309 name, strerror(error));
312 VLOG_WARN("failed to create datapath %s: %s", name, strerror(error));
317 /* Closes and frees the connection to 'xfif'. Does not destroy the datapath
318 * itself; call xfif_delete() first, instead, if that is desirable. */
320 xfif_close(struct xfif *xfif)
323 struct registered_xfif_class *registered_class;
325 registered_class = shash_find_data(&xfif_classes,
326 xfif->xfif_class->type);
327 assert(registered_class);
328 assert(registered_class->refcount);
330 registered_class->refcount--;
331 xfif_uninit(xfif, true);
335 /* Returns the name of datapath 'xfif' prefixed with the type
336 * (for use in log messages). */
338 xfif_name(const struct xfif *xfif)
340 return xfif->full_name;
343 /* Returns the name of datapath 'xfif' without the type
344 * (for use in device names). */
346 xfif_base_name(const struct xfif *xfif)
348 return xfif->base_name;
351 /* Enumerates all names that may be used to open 'xfif' into 'all_names'. The
352 * Linux datapath, for example, supports opening a datapath both by number,
353 * e.g. "dp0", and by the name of the datapath's local port. For some
354 * datapaths, this might be an infinite set (e.g. in a file name, slashes may
355 * be duplicated any number of times), in which case only the names most likely
356 * to be used will be enumerated.
358 * The caller must already have initialized 'all_names'. Any existing names in
359 * 'all_names' will not be disturbed. */
361 xfif_get_all_names(const struct xfif *xfif, struct svec *all_names)
363 if (xfif->xfif_class->get_all_names) {
364 int error = xfif->xfif_class->get_all_names(xfif, all_names);
366 VLOG_WARN_RL(&error_rl,
367 "failed to retrieve names for datpath %s: %s",
368 xfif_name(xfif), strerror(error));
372 svec_add(all_names, xfif_base_name(xfif));
377 /* Destroys the datapath that 'xfif' is connected to, first removing all of its
378 * ports. After calling this function, it does not make sense to pass 'xfif'
379 * to any functions other than xfif_name() or xfif_close(). */
381 xfif_delete(struct xfif *xfif)
385 COVERAGE_INC(xfif_destroy);
387 error = xfif->xfif_class->destroy(xfif);
388 log_operation(xfif, "delete", error);
392 /* Retrieves statistics for 'xfif' into 'stats'. Returns 0 if successful,
393 * otherwise a positive errno value. */
395 xfif_get_xf_stats(const struct xfif *xfif, struct xflow_stats *stats)
397 int error = xfif->xfif_class->get_stats(xfif, stats);
399 memset(stats, 0, sizeof *stats);
401 log_operation(xfif, "get_stats", error);
405 /* Retrieves the current IP fragment handling policy for 'xfif' into
406 * '*drop_frags': true indicates that fragments are dropped, false indicates
407 * that fragments are treated in the same way as other IP packets (except that
408 * the L4 header cannot be read). Returns 0 if successful, otherwise a
409 * positive errno value. */
411 xfif_get_drop_frags(const struct xfif *xfif, bool *drop_frags)
413 int error = xfif->xfif_class->get_drop_frags(xfif, drop_frags);
417 log_operation(xfif, "get_drop_frags", error);
421 /* Changes 'xfif''s treatment of IP fragments to 'drop_frags', whose meaning is
422 * the same as for the get_drop_frags member function. Returns 0 if
423 * successful, otherwise a positive errno value. */
425 xfif_set_drop_frags(struct xfif *xfif, bool drop_frags)
427 int error = xfif->xfif_class->set_drop_frags(xfif, drop_frags);
428 log_operation(xfif, "set_drop_frags", error);
432 /* Attempts to add 'devname' as a port on 'xfif', given the combination of
433 * XFLOW_PORT_* flags in 'flags'. If successful, returns 0 and sets '*port_nop'
434 * to the new port's port number (if 'port_nop' is non-null). On failure,
435 * returns a positive errno value and sets '*port_nop' to UINT16_MAX (if
436 * 'port_nop' is non-null). */
438 xfif_port_add(struct xfif *xfif, const char *devname, uint16_t flags,
444 COVERAGE_INC(xfif_port_add);
446 error = xfif->xfif_class->port_add(xfif, devname, flags, &port_no);
448 VLOG_DBG_RL(&dpmsg_rl, "%s: added %s as port %"PRIu16,
449 xfif_name(xfif), devname, port_no);
451 VLOG_WARN_RL(&error_rl, "%s: failed to add %s as port: %s",
452 xfif_name(xfif), devname, strerror(error));
453 port_no = UINT16_MAX;
461 /* Attempts to remove 'xfif''s port number 'port_no'. Returns 0 if successful,
462 * otherwise a positive errno value. */
464 xfif_port_del(struct xfif *xfif, uint16_t port_no)
468 COVERAGE_INC(xfif_port_del);
470 error = xfif->xfif_class->port_del(xfif, port_no);
471 log_operation(xfif, "port_del", error);
475 /* Looks up port number 'port_no' in 'xfif'. On success, returns 0 and
476 * initializes '*port' appropriately; on failure, returns a positive errno
479 xfif_port_query_by_number(const struct xfif *xfif, uint16_t port_no,
480 struct xflow_port *port)
482 int error = xfif->xfif_class->port_query_by_number(xfif, port_no, port);
484 VLOG_DBG_RL(&dpmsg_rl, "%s: port %"PRIu16" is device %s",
485 xfif_name(xfif), port_no, port->devname);
487 memset(port, 0, sizeof *port);
488 VLOG_WARN_RL(&error_rl, "%s: failed to query port %"PRIu16": %s",
489 xfif_name(xfif), port_no, strerror(error));
494 /* Looks up port named 'devname' in 'xfif'. On success, returns 0 and
495 * initializes '*port' appropriately; on failure, returns a positive errno
498 xfif_port_query_by_name(const struct xfif *xfif, const char *devname,
499 struct xflow_port *port)
501 int error = xfif->xfif_class->port_query_by_name(xfif, devname, port);
503 VLOG_DBG_RL(&dpmsg_rl, "%s: device %s is on port %"PRIu16,
504 xfif_name(xfif), devname, port->port);
506 memset(port, 0, sizeof *port);
508 /* Log level is DBG here because all the current callers are interested
509 * in whether 'xfif' actually has a port 'devname', so that it's not an
510 * issue worth logging if it doesn't. */
511 VLOG_DBG_RL(&error_rl, "%s: failed to query port %s: %s",
512 xfif_name(xfif), devname, strerror(error));
517 /* Looks up port number 'port_no' in 'xfif'. On success, returns 0 and copies
518 * the port's name into the 'name_size' bytes in 'name', ensuring that the
519 * result is null-terminated. On failure, returns a positive errno value and
520 * makes 'name' the empty string. */
522 xfif_port_get_name(struct xfif *xfif, uint16_t port_no,
523 char *name, size_t name_size)
525 struct xflow_port port;
528 assert(name_size > 0);
530 error = xfif_port_query_by_number(xfif, port_no, &port);
532 ovs_strlcpy(name, port.devname, name_size);
539 /* Obtains a list of all the ports in 'xfif'.
541 * If successful, returns 0 and sets '*portsp' to point to an array of
542 * appropriately initialized port structures and '*n_portsp' to the number of
543 * ports in the array. The caller is responsible for freeing '*portp' by
546 * On failure, returns a positive errno value and sets '*portsp' to NULL and
547 * '*n_portsp' to 0. */
549 xfif_port_list(const struct xfif *xfif,
550 struct xflow_port **portsp, size_t *n_portsp)
552 struct xflow_port *ports;
557 struct xflow_stats stats;
560 error = xfif_get_xf_stats(xfif, &stats);
565 ports = xcalloc(stats.n_ports, sizeof *ports);
566 retval = xfif->xfif_class->port_list(xfif, ports, stats.n_ports);
572 } else if (retval <= stats.n_ports) {
578 /* Soft error: port count increased behind our back. Try again. */
591 log_operation(xfif, "port_list", error);
595 /* Polls for changes in the set of ports in 'xfif'. If the set of ports in
596 * 'xfif' has changed, this function does one of the following:
598 * - Stores the name of the device that was added to or deleted from 'xfif' in
599 * '*devnamep' and returns 0. The caller is responsible for freeing
600 * '*devnamep' (with free()) when it no longer needs it.
602 * - Returns ENOBUFS and sets '*devnamep' to NULL.
604 * This function may also return 'false positives', where it returns 0 and
605 * '*devnamep' names a device that was not actually added or deleted or it
606 * returns ENOBUFS without any change.
608 * Returns EAGAIN if the set of ports in 'xfif' has not changed. May also
609 * return other positive errno values to indicate that something has gone
612 xfif_port_poll(const struct xfif *xfif, char **devnamep)
614 int error = xfif->xfif_class->port_poll(xfif, devnamep);
621 /* Arranges for the poll loop to wake up when port_poll(xfif) will return a
622 * value other than EAGAIN. */
624 xfif_port_poll_wait(const struct xfif *xfif)
626 xfif->xfif_class->port_poll_wait(xfif);
629 /* Retrieves a list of the port numbers in port group 'group' in 'xfif'.
631 * On success, returns 0 and points '*ports' to a newly allocated array of
632 * integers, each of which is a 'xfif' port number for a port in
633 * 'group'. Stores the number of elements in the array in '*n_ports'. The
634 * caller is responsible for freeing '*ports' by calling free().
636 * On failure, returns a positive errno value and sets '*ports' to NULL and
637 * '*n_ports' to 0. */
639 xfif_port_group_get(const struct xfif *xfif, uint16_t group,
640 uint16_t **ports, size_t *n_ports)
647 int retval = xfif->xfif_class->port_group_get(xfif, group,
656 } else if (retval <= *n_ports) {
662 /* Soft error: there were more ports than we expected in the
663 * group. Try again. */
665 *ports = xcalloc(retval, sizeof **ports);
669 log_operation(xfif, "port_group_get", error);
673 /* Updates port group 'group' in 'xfif', making it contain the 'n_ports' ports
674 * whose 'xfif' port numbers are given in 'n_ports'. Returns 0 if
675 * successful, otherwise a positive errno value.
677 * Behavior is undefined if the values in ports[] are not unique. */
679 xfif_port_group_set(struct xfif *xfif, uint16_t group,
680 const uint16_t ports[], size_t n_ports)
684 COVERAGE_INC(xfif_port_group_set);
686 error = xfif->xfif_class->port_group_set(xfif, group, ports, n_ports);
687 log_operation(xfif, "port_group_set", error);
691 /* Deletes all flows from 'xfif'. Returns 0 if successful, otherwise a
692 * positive errno value. */
694 xfif_flow_flush(struct xfif *xfif)
698 COVERAGE_INC(xfif_flow_flush);
700 error = xfif->xfif_class->flow_flush(xfif);
701 log_operation(xfif, "flow_flush", error);
705 /* Queries 'xfif' for a flow entry matching 'flow->key'.
707 * If a flow matching 'flow->key' exists in 'xfif', stores statistics for the
708 * flow into 'flow->stats'. If 'flow->n_actions' is zero, then 'flow->actions'
709 * is ignored. If 'flow->n_actions' is nonzero, then 'flow->actions' should
710 * point to an array of the specified number of actions. At most that many of
711 * the flow's actions will be copied into that array. 'flow->n_actions' will
712 * be updated to the number of actions actually present in the flow, which may
713 * be greater than the number stored if the flow has more actions than space
714 * available in the array.
716 * If no flow matching 'flow->key' exists in 'xfif', returns ENOENT. On other
717 * failure, returns a positive errno value. */
719 xfif_flow_get(const struct xfif *xfif, struct xflow_flow *flow)
723 COVERAGE_INC(xfif_flow_get);
725 check_rw_xflow_flow(flow);
726 error = xfif->xfif_class->flow_get(xfif, flow, 1);
728 error = flow->stats.error;
731 /* Make the results predictable on error. */
732 memset(&flow->stats, 0, sizeof flow->stats);
735 if (should_log_flow_message(error)) {
736 log_flow_operation(xfif, "flow_get", error, flow);
741 /* For each flow 'flow' in the 'n' flows in 'flows':
743 * - If a flow matching 'flow->key' exists in 'xfif':
745 * Stores 0 into 'flow->stats.error' and stores statistics for the flow
746 * into 'flow->stats'.
748 * If 'flow->n_actions' is zero, then 'flow->actions' is ignored. If
749 * 'flow->n_actions' is nonzero, then 'flow->actions' should point to an
750 * array of the specified number of actions. At most that many of the
751 * flow's actions will be copied into that array. 'flow->n_actions' will
752 * be updated to the number of actions actually present in the flow, which
753 * may be greater than the number stored if the flow has more actions than
754 * space available in the array.
756 * - Flow-specific errors are indicated by a positive errno value in
757 * 'flow->stats.error'. In particular, ENOENT indicates that no flow
758 * matching 'flow->key' exists in 'xfif'. When an error value is stored, the
759 * contents of 'flow->key' are preserved but other members of 'flow' should
760 * be treated as indeterminate.
762 * Returns 0 if all 'n' flows in 'flows' were updated (whether they were
763 * individually successful or not is indicated by 'flow->stats.error',
764 * however). Returns a positive errno value if an error that prevented this
765 * update occurred, in which the caller must not depend on any elements in
766 * 'flows' being updated or not updated.
769 xfif_flow_get_multiple(const struct xfif *xfif,
770 struct xflow_flow flows[], size_t n)
775 COVERAGE_ADD(xfif_flow_get, n);
777 for (i = 0; i < n; i++) {
778 check_rw_xflow_flow(&flows[i]);
781 error = xfif->xfif_class->flow_get(xfif, flows, n);
782 log_operation(xfif, "flow_get_multiple", error);
786 /* Adds or modifies a flow in 'xfif' as specified in 'put':
788 * - If the flow specified in 'put->flow' does not exist in 'xfif', then
789 * behavior depends on whether XFLOWPF_CREATE is specified in 'put->flags': if
790 * it is, the flow will be added, otherwise the operation will fail with
793 * - Otherwise, the flow specified in 'put->flow' does exist in 'xfif'.
794 * Behavior in this case depends on whether XFLOWPF_MODIFY is specified in
795 * 'put->flags': if it is, the flow's actions will be updated, otherwise the
796 * operation will fail with EEXIST. If the flow's actions are updated, then
797 * its statistics will be zeroed if XFLOWPF_ZERO_STATS is set in 'put->flags',
798 * left as-is otherwise.
800 * Returns 0 if successful, otherwise a positive errno value.
803 xfif_flow_put(struct xfif *xfif, struct xflow_flow_put *put)
807 COVERAGE_INC(xfif_flow_put);
809 error = xfif->xfif_class->flow_put(xfif, put);
810 if (should_log_flow_message(error)) {
811 log_flow_put(xfif, error, put);
816 /* Deletes a flow matching 'flow->key' from 'xfif' or returns ENOENT if 'xfif'
817 * does not contain such a flow.
819 * If successful, updates 'flow->stats', 'flow->n_actions', and 'flow->actions'
820 * as described for xfif_flow_get(). */
822 xfif_flow_del(struct xfif *xfif, struct xflow_flow *flow)
826 COVERAGE_INC(xfif_flow_del);
828 check_rw_xflow_flow(flow);
829 memset(&flow->stats, 0, sizeof flow->stats);
831 error = xfif->xfif_class->flow_del(xfif, flow);
832 if (should_log_flow_message(error)) {
833 log_flow_operation(xfif, "delete flow", error, flow);
838 /* Stores up to 'n' flows in 'xfif' into 'flows', including their statistics
839 * but not including any information about their actions. If successful,
840 * returns 0 and sets '*n_out' to the number of flows actually present in
841 * 'xfif', which might be greater than the number stored (if 'xfif' has more
842 * than 'n' flows). On failure, returns a negative errno value and sets
845 xfif_flow_list(const struct xfif *xfif, struct xflow_flow flows[], size_t n,
851 COVERAGE_INC(xfif_flow_query_list);
852 if (RUNNING_ON_VALGRIND) {
853 memset(flows, 0, n * sizeof *flows);
855 for (i = 0; i < n; i++) {
856 flows[i].actions = NULL;
857 flows[i].n_actions = 0;
860 retval = xfif->xfif_class->flow_list(xfif, flows, n);
863 VLOG_WARN_RL(&error_rl, "%s: flow list failed (%s)",
864 xfif_name(xfif), strerror(-retval));
867 COVERAGE_ADD(xfif_flow_query_list_n, retval);
868 *n_out = MIN(n, retval);
869 VLOG_DBG_RL(&dpmsg_rl, "%s: listed %zu flows (of %d)",
870 xfif_name(xfif), *n_out, retval);
875 /* Retrieves all of the flows in 'xfif'.
877 * If successful, returns 0 and stores in '*flowsp' a pointer to a newly
878 * allocated array of flows, including their statistics but not including any
879 * information about their actions, and sets '*np' to the number of flows in
880 * '*flowsp'. The caller is responsible for freeing '*flowsp' by calling
883 * On failure, returns a positive errno value and sets '*flowsp' to NULL and
886 xfif_flow_list_all(const struct xfif *xfif,
887 struct xflow_flow **flowsp, size_t *np)
889 struct xflow_stats stats;
890 struct xflow_flow *flows;
897 error = xfif_get_xf_stats(xfif, &stats);
902 flows = xmalloc(sizeof *flows * stats.n_flows);
903 error = xfif_flow_list(xfif, flows, stats.n_flows, &n_flows);
909 if (stats.n_flows != n_flows) {
910 VLOG_WARN_RL(&error_rl, "%s: datapath stats reported %"PRIu32" "
911 "flows but flow listing reported %zu",
912 xfif_name(xfif), stats.n_flows, n_flows);
919 /* Causes 'xfif' to perform the 'n_actions' actions in 'actions' on the
920 * Ethernet frame specified in 'packet'.
922 * Pretends that the frame was originally received on the port numbered
923 * 'in_port'. This affects only XFLOWAT_OUTPUT_GROUP actions, which will not
924 * send a packet out their input port. Specify the number of an unused port
925 * (e.g. UINT16_MAX is currently always unused) to avoid this behavior.
927 * Returns 0 if successful, otherwise a positive errno value. */
929 xfif_execute(struct xfif *xfif, uint16_t in_port,
930 const union xflow_action actions[], size_t n_actions,
931 const struct ofpbuf *buf)
935 COVERAGE_INC(xfif_execute);
937 error = xfif->xfif_class->execute(xfif, in_port, actions,
943 if (!(error ? VLOG_DROP_WARN(&error_rl) : VLOG_DROP_DBG(&dpmsg_rl))) {
944 struct ds ds = DS_EMPTY_INITIALIZER;
945 char *packet = ofp_packet_to_string(buf->data, buf->size, buf->size);
946 ds_put_format(&ds, "%s: execute ", xfif_name(xfif));
947 format_xflow_actions(&ds, actions, n_actions);
949 ds_put_format(&ds, " failed (%s)", strerror(error));
951 ds_put_format(&ds, " on packet %s", packet);
952 vlog(THIS_MODULE, error ? VLL_WARN : VLL_DBG, "%s", ds_cstr(&ds));
959 /* Retrieves 'xfif''s "listen mask" into '*listen_mask'. Each XFLOWL_* bit set
960 * in '*listen_mask' indicates that xfif_recv() will receive messages of that
961 * type. Returns 0 if successful, otherwise a positive errno value. */
963 xfif_recv_get_mask(const struct xfif *xfif, int *listen_mask)
965 int error = xfif->xfif_class->recv_get_mask(xfif, listen_mask);
969 log_operation(xfif, "recv_get_mask", error);
973 /* Sets 'xfif''s "listen mask" to 'listen_mask'. Each XFLOWL_* bit set in
974 * '*listen_mask' requests that xfif_recv() receive messages of that type.
975 * Returns 0 if successful, otherwise a positive errno value. */
977 xfif_recv_set_mask(struct xfif *xfif, int listen_mask)
979 int error = xfif->xfif_class->recv_set_mask(xfif, listen_mask);
980 log_operation(xfif, "recv_set_mask", error);
984 /* Retrieve the sFlow sampling probability. '*probability' is expressed as the
985 * number of packets out of UINT_MAX to sample, e.g. probability/UINT_MAX is
986 * the probability of sampling a given packet.
988 * Returns 0 if successful, otherwise a positive errno value. EOPNOTSUPP
989 * indicates that 'xfif' does not support sFlow sampling. */
991 xfif_get_sflow_probability(const struct xfif *xfif, uint32_t *probability)
993 int error = (xfif->xfif_class->get_sflow_probability
994 ? xfif->xfif_class->get_sflow_probability(xfif, probability)
999 log_operation(xfif, "get_sflow_probability", error);
1003 /* Set the sFlow sampling probability. 'probability' is expressed as the
1004 * number of packets out of UINT_MAX to sample, e.g. probability/UINT_MAX is
1005 * the probability of sampling a given packet.
1007 * Returns 0 if successful, otherwise a positive errno value. EOPNOTSUPP
1008 * indicates that 'xfif' does not support sFlow sampling. */
1010 xfif_set_sflow_probability(struct xfif *xfif, uint32_t probability)
1012 int error = (xfif->xfif_class->set_sflow_probability
1013 ? xfif->xfif_class->set_sflow_probability(xfif, probability)
1015 log_operation(xfif, "set_sflow_probability", error);
1019 /* Attempts to receive a message from 'xfif'. If successful, stores the
1020 * message into '*packetp'. The message, if one is received, will begin with
1021 * 'struct xflow_msg' as a header, and will have at least XFIF_RECV_MSG_PADDING
1022 * bytes of headroom. Only messages of the types selected with
1023 * xfif_set_listen_mask() will ordinarily be received (but if a message type is
1024 * enabled and then later disabled, some stragglers might pop up).
1026 * Returns 0 if successful, otherwise a positive errno value. Returns EAGAIN
1027 * if no message is immediately available. */
1029 xfif_recv(struct xfif *xfif, struct ofpbuf **packetp)
1031 int error = xfif->xfif_class->recv(xfif, packetp);
1033 struct ofpbuf *buf = *packetp;
1035 assert(ofpbuf_headroom(buf) >= XFIF_RECV_MSG_PADDING);
1036 if (VLOG_IS_DBG_ENABLED()) {
1037 struct xflow_msg *msg = buf->data;
1038 void *payload = msg + 1;
1039 size_t payload_len = buf->size - sizeof *msg;
1040 char *s = ofp_packet_to_string(payload, payload_len, payload_len);
1041 VLOG_DBG_RL(&dpmsg_rl, "%s: received %s message of length "
1042 "%zu on port %"PRIu16": %s", xfif_name(xfif),
1043 (msg->type == _XFLOWL_MISS_NR ? "miss"
1044 : msg->type == _XFLOWL_ACTION_NR ? "action"
1045 : msg->type == _XFLOWL_SFLOW_NR ? "sFlow"
1047 payload_len, msg->port, s);
1056 /* Discards all messages that would otherwise be received by xfif_recv() on
1057 * 'xfif'. Returns 0 if successful, otherwise a positive errno value. */
1059 xfif_recv_purge(struct xfif *xfif)
1061 struct xflow_stats stats;
1065 COVERAGE_INC(xfif_purge);
1067 error = xfif_get_xf_stats(xfif, &stats);
1072 for (i = 0; i < stats.max_miss_queue + stats.max_action_queue + stats.max_sflow_queue; i++) {
1074 error = xfif_recv(xfif, &buf);
1076 return error == EAGAIN ? 0 : error;
1083 /* Arranges for the poll loop to wake up when 'xfif' has a message queued to be
1084 * received with xfif_recv(). */
1086 xfif_recv_wait(struct xfif *xfif)
1088 xfif->xfif_class->recv_wait(xfif);
1091 /* Obtains the NetFlow engine type and engine ID for 'xfif' into '*engine_type'
1092 * and '*engine_id', respectively. */
1094 xfif_get_netflow_ids(const struct xfif *xfif,
1095 uint8_t *engine_type, uint8_t *engine_id)
1097 *engine_type = xfif->netflow_engine_type;
1098 *engine_id = xfif->netflow_engine_id;
1101 /* Translates OpenFlow queue ID 'queue_id' (in host byte order) into a priority
1102 * value for use in the ODPAT_SET_PRIORITY action. On success, returns 0 and
1103 * stores the priority into '*priority'. On failure, returns a positive errno
1104 * value and stores 0 into '*priority'. */
1106 xfif_queue_to_priority(const struct xfif *xfif, uint32_t queue_id,
1109 int error = (xfif->xfif_class->queue_to_priority
1110 ? xfif->xfif_class->queue_to_priority(xfif, queue_id,
1116 log_operation(xfif, "queue_to_priority", error);
1121 xfif_init(struct xfif *xfif, const struct xfif_class *xfif_class,
1123 uint8_t netflow_engine_type, uint8_t netflow_engine_id)
1125 xfif->xfif_class = xfif_class;
1126 xfif->base_name = xstrdup(name);
1127 xfif->full_name = xasprintf("%s@%s", xfif_class->type, name);
1128 xfif->netflow_engine_type = netflow_engine_type;
1129 xfif->netflow_engine_id = netflow_engine_id;
1132 /* Undoes the results of initialization.
1134 * Normally this function only needs to be called from xfif_close().
1135 * However, it may be called by providers due to an error on opening
1136 * that occurs after initialization. In this case xfif_close() would
1137 * never be called. */
1139 xfif_uninit(struct xfif *xfif, bool close)
1141 char *base_name = xfif->base_name;
1142 char *full_name = xfif->full_name;
1145 xfif->xfif_class->close(xfif);
1153 log_operation(const struct xfif *xfif, const char *operation, int error)
1156 VLOG_DBG_RL(&dpmsg_rl, "%s: %s success", xfif_name(xfif), operation);
1157 } else if (is_errno(error)) {
1158 VLOG_WARN_RL(&error_rl, "%s: %s failed (%s)",
1159 xfif_name(xfif), operation, strerror(error));
1161 VLOG_WARN_RL(&error_rl, "%s: %s failed (%d/%d)",
1162 xfif_name(xfif), operation,
1163 get_ofp_err_type(error), get_ofp_err_code(error));
1167 static enum vlog_level
1168 flow_message_log_level(int error)
1170 return error ? VLL_WARN : VLL_DBG;
1174 should_log_flow_message(int error)
1176 return !vlog_should_drop(THIS_MODULE, flow_message_log_level(error),
1177 error ? &error_rl : &dpmsg_rl);
1181 log_flow_message(const struct xfif *xfif, int error, const char *operation,
1182 const struct xflow_key *flow,
1183 const struct xflow_flow_stats *stats,
1184 const union xflow_action *actions, size_t n_actions)
1186 struct ds ds = DS_EMPTY_INITIALIZER;
1187 ds_put_format(&ds, "%s: ", xfif_name(xfif));
1189 ds_put_cstr(&ds, "failed to ");
1191 ds_put_format(&ds, "%s ", operation);
1193 ds_put_format(&ds, "(%s) ", strerror(error));
1195 format_xflow_key(&ds, flow);
1197 ds_put_cstr(&ds, ", ");
1198 format_xflow_flow_stats(&ds, stats);
1200 if (actions || n_actions) {
1201 ds_put_cstr(&ds, ", actions:");
1202 format_xflow_actions(&ds, actions, n_actions);
1204 vlog(THIS_MODULE, flow_message_log_level(error), "%s", ds_cstr(&ds));
1209 log_flow_operation(const struct xfif *xfif, const char *operation, int error,
1210 struct xflow_flow *flow)
1213 flow->n_actions = 0;
1215 log_flow_message(xfif, error, operation, &flow->key,
1216 !error ? &flow->stats : NULL,
1217 flow->actions, flow->n_actions);
1221 log_flow_put(struct xfif *xfif, int error, const struct xflow_flow_put *put)
1223 enum { XFLOWPF_ALL = XFLOWPF_CREATE | XFLOWPF_MODIFY | XFLOWPF_ZERO_STATS };
1227 ds_put_cstr(&s, "put");
1228 if (put->flags & XFLOWPF_CREATE) {
1229 ds_put_cstr(&s, "[create]");
1231 if (put->flags & XFLOWPF_MODIFY) {
1232 ds_put_cstr(&s, "[modify]");
1234 if (put->flags & XFLOWPF_ZERO_STATS) {
1235 ds_put_cstr(&s, "[zero]");
1237 if (put->flags & ~XFLOWPF_ALL) {
1238 ds_put_format(&s, "[%x]", put->flags & ~XFLOWPF_ALL);
1240 log_flow_message(xfif, error, ds_cstr(&s), &put->flow.key,
1241 !error ? &put->flow.stats : NULL,
1242 put->flow.actions, put->flow.n_actions);
1246 /* There is a tendency to construct xflow_flow objects on the stack and to
1247 * forget to properly initialize their "actions" and "n_actions" members.
1248 * When this happens, we get memory corruption because the kernel
1249 * writes through the random pointer that is in the "actions" member.
1251 * This function attempts to combat the problem by:
1253 * - Forcing a segfault if "actions" points to an invalid region (instead
1254 * of just getting back EFAULT, which can be easily missed in the log).
1256 * - Storing a distinctive value that is likely to cause an
1257 * easy-to-identify error later if it is dereferenced, etc.
1259 * - Triggering a warning on uninitialized memory from Valgrind if
1260 * "actions" or "n_actions" was not initialized.
1263 check_rw_xflow_flow(struct xflow_flow *flow)
1265 if (flow->n_actions) {
1266 memset(&flow->actions[0], 0xcc, sizeof flow->actions[0]);