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 "wdp-provider.h"
28 #include "dynamic-string.h"
32 #include "ofp-print.h"
35 #include "poll-loop.h"
42 #include "wdp-xflow.h"
44 VLOG_DEFINE_THIS_MODULE(wdp)
48 /* Initializes a new 'struct wdp_rule', copying in the 'n_actions' elements of
51 * The caller is responsible for initializing 'rule->cr'. The caller must also
52 * fill in 'rule->ofp_table_id', if the wdp has more than one table. */
54 wdp_rule_init(struct wdp_rule *rule, const union ofp_action *actions,
57 rule->actions = xmemdup(actions, n_actions * sizeof *actions);
58 rule->n_actions = n_actions;
59 rule->created = time_msec();
60 rule->idle_timeout = 0;
61 rule->hard_timeout = 0;
62 rule->ofp_table_id = 0;
63 rule->client_data = NULL;
66 /* Frees the data in 'rule'. */
68 wdp_rule_uninit(struct wdp_rule *rule)
75 static const struct wdp_class *base_wdp_classes[] = {
79 struct registered_wdp_class {
80 const struct wdp_class *wdp_class;
84 static struct shash wdp_classes = SHASH_INITIALIZER(&wdp_classes);
86 /* Rate limit for individual messages going to or from the datapath, output at
87 * DBG level. This is very high because, if these are enabled, it is because
88 * we really need to see them. */
89 static struct vlog_rate_limit wdpmsg_rl = VLOG_RATE_LIMIT_INIT(600, 600);
91 /* Not really much point in logging many wdp errors. */
92 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
94 static void log_operation(const struct wdp *, const char *operation,
100 static int status = -1;
106 for (i = 0; i < ARRAY_SIZE(base_wdp_classes); i++) {
107 wdp_register_provider(base_wdp_classes[i]);
109 wdp_xflow_register();
113 /* Performs periodic work needed by all the various kinds of wdps.
115 * If your program opens any wdps, it must call both this function and
116 * netdev_run() within its main poll loop. */
120 struct shash_node *node;
121 SHASH_FOR_EACH (node, &wdp_classes) {
122 const struct registered_wdp_class *registered_class = node->data;
123 if (registered_class->wdp_class->run) {
124 registered_class->wdp_class->run();
129 /* Arranges for poll_block() to wake up when wdp_run() needs to be called.
131 * If your program opens any wdps, it must call both this function and
132 * netdev_wait() within its main poll loop. */
136 struct shash_node *node;
137 SHASH_FOR_EACH(node, &wdp_classes) {
138 const struct registered_wdp_class *registered_class = node->data;
139 if (registered_class->wdp_class->wait) {
140 registered_class->wdp_class->wait();
145 /* Registers a new datapath provider. After successful registration, new
146 * datapaths of that type can be opened using wdp_open(). */
148 wdp_register_provider(const struct wdp_class *new_class)
150 struct registered_wdp_class *registered_class;
152 if (shash_find(&wdp_classes, new_class->type)) {
153 VLOG_WARN("attempted to register duplicate datapath provider: %s",
158 registered_class = xmalloc(sizeof *registered_class);
159 registered_class->wdp_class = new_class;
160 registered_class->refcount = 0;
162 shash_add(&wdp_classes, new_class->type, registered_class);
167 /* Unregisters a datapath provider. 'type' must have been previously
168 * registered and not currently be in use by any wdps. After unregistration
169 * new datapaths of that type cannot be opened using wdp_open(). */
171 wdp_unregister_provider(const char *type)
173 struct shash_node *node;
174 struct registered_wdp_class *registered_class;
176 node = shash_find(&wdp_classes, type);
178 VLOG_WARN("attempted to unregister a datapath provider that is not "
179 "registered: %s", type);
183 registered_class = node->data;
184 if (registered_class->refcount) {
185 VLOG_WARN("attempted to unregister in use datapath provider: %s",
190 shash_delete(&wdp_classes, node);
191 free(registered_class);
196 /* Clears 'types' and enumerates the types of all currently registered wdp
197 * providers into it. The caller must first initialize the svec. */
199 wdp_enumerate_types(struct svec *types)
201 struct shash_node *node;
206 SHASH_FOR_EACH (node, &wdp_classes) {
207 const struct registered_wdp_class *registered_class = node->data;
208 svec_add(types, registered_class->wdp_class->type);
212 /* Clears 'names' and enumerates the names of all known created datapaths
213 * with the given 'type'. The caller must first initialize the svec. Returns 0
214 * if successful, otherwise a positive errno value.
216 * Some kinds of datapaths might not be practically enumerable. This is not
217 * considered an error. */
219 wdp_enumerate_names(const char *type, struct svec *names)
221 const struct registered_wdp_class *registered_class;
222 const struct wdp_class *wdp_class;
228 registered_class = shash_find_data(&wdp_classes, type);
229 if (!registered_class) {
230 VLOG_WARN("could not enumerate unknown type: %s", type);
234 wdp_class = registered_class->wdp_class;
235 error = (wdp_class->enumerate
236 ? wdp_class->enumerate(wdp_class, names)
240 VLOG_WARN("failed to enumerate %s datapaths: %s", wdp_class->type,
247 /* Parses 'datapath_name', which is of the form type@name, into its
248 * component pieces. 'name' and 'type' must be freed by the caller. */
250 wdp_parse_name(const char *datapath_name_, char **name, char **type)
252 char *datapath_name = xstrdup(datapath_name_);
255 separator = strchr(datapath_name, '@');
258 *type = datapath_name;
259 *name = xstrdup(separator + 1);
261 *name = datapath_name;
267 do_open(const char *name, const char *type, bool create, struct wdp **wdpp)
269 struct wdp *wdp = NULL;
271 struct registered_wdp_class *registered_class;
275 if (!type || *type == '\0') {
279 registered_class = shash_find_data(&wdp_classes, type);
280 if (!registered_class) {
281 VLOG_WARN("could not create datapath %s of unknown type %s", name,
283 error = EAFNOSUPPORT;
287 error = registered_class->wdp_class->open(registered_class->wdp_class,
290 registered_class->refcount++;
294 *wdpp = error ? NULL : wdp;
298 /* Tries to open an existing datapath named 'name' and type 'type'. Will fail
299 * if no datapath with 'name' and 'type' exists. 'type' may be either NULL or
300 * the empty string to specify the default system type. Returns 0 if
301 * successful, otherwise a positive errno value. On success stores a pointer
302 * to the datapath in '*wdpp', otherwise a null pointer. */
304 wdp_open(const char *name, const char *type, struct wdp **wdpp)
306 return do_open(name, type, false, wdpp);
309 /* Tries to create and open a new datapath with the given 'name' and 'type'.
310 * 'type' may be either NULL or the empty string to specify the default system
311 * type. Will fail if a datapath with 'name' and 'type' already exists.
312 * Returns 0 if successful, otherwise a positive errno value. On success
313 * stores a pointer to the datapath in '*wdpp', otherwise a null pointer. */
315 wdp_create(const char *name, const char *type, struct wdp **wdpp)
317 return do_open(name, type, true, wdpp);
320 /* Tries to open a datapath with the given 'name' and 'type', creating it if it
321 * does not exist. 'type' may be either NULL or the empty string to specify
322 * the default system type. Returns 0 if successful, otherwise a positive
323 * errno value. On success stores a pointer to the datapath in '*wdpp',
324 * otherwise a null pointer. */
326 wdp_create_and_open(const char *name, const char *type, struct wdp **wdpp)
330 error = wdp_create(name, type, wdpp);
331 if (error == EEXIST || error == EBUSY) {
332 error = wdp_open(name, type, wdpp);
334 VLOG_WARN("datapath %s already exists but cannot be opened: %s",
335 name, strerror(error));
338 VLOG_WARN("failed to create datapath %s: %s", name, strerror(error));
343 /* Closes and frees the connection to 'wdp'. Does not destroy the wdp
344 * itself; call wdp_delete() first, instead, if that is desirable. */
346 wdp_close(struct wdp *wdp)
349 struct registered_wdp_class *registered_class;
351 registered_class = shash_find_data(&wdp_classes,
352 wdp->wdp_class->type);
353 assert(registered_class);
354 assert(registered_class->refcount);
356 registered_class->refcount--;
357 wdp_uninit(wdp, true);
361 /* Returns the name of datapath 'wdp' prefixed with the type
362 * (for use in log messages). */
364 wdp_name(const struct wdp *wdp)
366 return wdp->full_name;
369 /* Returns the name of datapath 'wdp' without the type
370 * (for use in device names). */
372 wdp_base_name(const struct wdp *wdp)
374 return wdp->base_name;
377 /* Enumerates all names that may be used to open 'wdp' into 'all_names'. The
378 * Linux datapath, for example, supports opening a datapath both by number,
379 * e.g. "wdp0", and by the name of the datapath's local port. For some
380 * datapaths, this might be an infinite set (e.g. in a file name, slashes may
381 * be duplicated any number of times), in which case only the names most likely
382 * to be used will be enumerated.
384 * The caller must already have initialized 'all_names'. Any existing names in
385 * 'all_names' will not be disturbed. */
387 wdp_get_all_names(const struct wdp *wdp, struct svec *all_names)
389 if (wdp->wdp_class->get_all_names) {
390 int error = wdp->wdp_class->get_all_names(wdp, all_names);
392 VLOG_WARN_RL(&error_rl,
393 "failed to retrieve names for datpath %s: %s",
394 wdp_name(wdp), strerror(error));
398 svec_add(all_names, wdp_base_name(wdp));
403 /* Destroys the datapath that 'wdp' is connected to, first removing all of
404 * its ports. After calling this function, it does not make sense to pass
405 * 'wdp' to any functions other than wdp_name() or wdp_close(). */
407 wdp_delete(struct wdp *wdp)
411 COVERAGE_INC(wdp_destroy);
413 error = wdp->wdp_class->destroy(wdp);
414 log_operation(wdp, "delete", error);
418 /* Obtains the set of features supported by 'wdp'.
420 * If successful, returns 0 and stores in '*featuresp' a newly allocated
421 * "struct ofp_switch_features" that describes the features and ports supported
422 * by 'wdp'. The caller is responsible for initializing the header,
423 * datapath_id, and n_buffers members of the returned "struct
424 * ofp_switch_features". The caller must free the returned buffer (with
425 * ofpbuf_delete()) when it is no longer needed.
427 * On error, returns an OpenFlow error code (as constructed by ofp_mkerr()) and
428 * sets '*featuresp' to NULL. */
430 wdp_get_features(const struct wdp *wdp, struct ofpbuf **featuresp)
432 int error = wdp->wdp_class->get_features(wdp, featuresp);
439 /* Retrieves statistics for 'wdp' into 'stats'. Returns 0 if successful,
440 * otherwise a positive errno value. On error, clears 'stats' to
443 wdp_get_wdp_stats(const struct wdp *wdp, struct wdp_stats *stats)
445 int error = wdp->wdp_class->get_stats(wdp, stats);
447 memset(stats, 0, sizeof *stats);
449 log_operation(wdp, "get_stats", error);
453 /* Appends to 'stats' one or more 'struct ofp_table_stats' structures that
454 * represent the tables maintained by 'wdp'. Returns 0 if successful,
455 * otherwise an OpenFlow error code constructed with ofp_mkerr(). */
457 wdp_get_table_stats(const struct wdp *wdp, struct ofpbuf *stats)
459 int error = wdp->wdp_class->get_table_stats(wdp, stats);
461 assert(stats->size > sizeof(struct ofp_stats_reply));
462 assert(((stats->size - sizeof(struct ofp_stats_reply))
463 % sizeof(struct ofp_table_stats)) == 0);
465 log_operation(wdp, "get_table_stats", error);
469 /* Retrieves the current IP fragment handling policy for 'wdp' into
470 * '*drop_frags': true indicates that fragments are dropped, false indicates
471 * that fragments are treated in the same way as other IP packets (except that
472 * the L4 header cannot be read). Returns 0 if successful, otherwise a
473 * positive errno value. */
475 wdp_get_drop_frags(const struct wdp *wdp, bool *drop_frags)
477 int error = wdp->wdp_class->get_drop_frags(wdp, drop_frags);
481 log_operation(wdp, "get_drop_frags", error);
485 /* Changes 'wdp''s treatment of IP fragments to 'drop_frags', whose meaning is
486 * the same as for the get_drop_frags member function. Returns 0 if
487 * successful, otherwise a positive errno value. EOPNOTSUPP indicates that
488 * 'wdp''s fragment dropping policy is not configurable. */
490 wdp_set_drop_frags(struct wdp *wdp, bool drop_frags)
493 error = (wdp->wdp_class->set_drop_frags
494 ? wdp->wdp_class->set_drop_frags(wdp, drop_frags)
496 log_operation(wdp, "set_drop_frags", error);
500 /* Clears the contents of 'port'. */
502 wdp_port_clear(struct wdp_port *port)
504 memset(port, 0, sizeof *port);
507 /* Makes a deep copy of 'old' in 'port'. The caller may free 'port''s data
508 * with wdp_port_free(). */
510 wdp_port_copy(struct wdp_port *port, const struct wdp_port *old)
512 port->netdev = old->netdev ? netdev_reopen(old->netdev) : NULL;
513 port->opp = old->opp;
514 port->devname = old->devname ? xstrdup(old->devname) : NULL;
515 port->internal = old->internal;
518 /* Frees the data that 'port' points to (but not 'port' itself). */
520 wdp_port_free(struct wdp_port *port)
523 netdev_close(port->netdev);
528 /* Frees the data that each of the 'n' ports in 'ports' points to, and then
529 * frees 'ports' itself. */
531 wdp_port_array_free(struct wdp_port *ports, size_t n)
535 for (i = 0; i < n; i++) {
536 wdp_port_free(&ports[i]);
541 /* Attempts to add 'devname' as a port on 'wdp':
543 * - If 'internal' is true, attempts to create a new internal port (a virtual
544 * port implemented in software) by that name.
546 * - If 'internal' is false, 'devname' must name an existing network device.
548 * If successful, returns 0 and sets '*port_nop' to the new port's OpenFlow
549 * port number (if 'port_nop' is non-null). On failure, returns a positive
550 * errno value and sets '*port_nop' to OFPP_NONE (if 'port_nop' is non-null).
552 * Some wildcarded datapaths might have fixed sets of ports. For these
553 * datapaths this function will always fail.
555 * Possible error return values include:
557 * - ENODEV: No device named 'devname' exists (if 'internal' is false).
559 * - EEXIST: A device named 'devname' already exists (if 'internal' is true).
561 * - EINVAL: Device 'devname' is not supported as part of a datapath (e.g. it
562 * is not an Ethernet device), or 'devname' is too long for a network
563 * device name (if 'internal' is true)
565 * - EFBIG: The datapath already has as many ports as it can support.
567 * - EOPNOTSUPP: 'wdp' has a fixed set of ports.
570 wdp_port_add(struct wdp *wdp, const char *devname,
571 bool internal, uint16_t *port_nop)
576 COVERAGE_INC(wdp_port_add);
578 error = (wdp->wdp_class->port_add
579 ? wdp->wdp_class->port_add(wdp, devname, internal, &port_no)
582 VLOG_DBG_RL(&wdpmsg_rl, "%s: added %s as port %"PRIu16,
583 wdp_name(wdp), devname, port_no);
585 VLOG_WARN_RL(&error_rl, "%s: failed to add %s as port: %s",
586 wdp_name(wdp), devname, strerror(error));
595 /* Attempts to remove 'wdp''s port numbered 'port_no'. Returns 0 if
596 * successful, otherwise a positive errno value.
598 * Some wildcarded datapaths might have fixed sets of ports. For these
599 * datapaths this function will always fail.
601 * Possible error return values include:
603 * - EINVAL: 'port_no' is outside the valid range, or this particular port is
604 * not removable (e.g. it is the local port).
606 * - ENOENT: 'wdp' currently has no port numbered 'port_no'.
608 * - EOPNOTSUPP: 'wdp' has a fixed set of ports.
611 wdp_port_del(struct wdp *wdp, uint16_t port_no)
615 COVERAGE_INC(wdp_port_del);
617 error = (wdp->wdp_class->port_del
618 ? wdp->wdp_class->port_del(wdp, port_no)
620 log_operation(wdp, "port_del", error);
624 /* Looks up port number 'port_no' in 'wdp'. On success, returns 0 and
625 * initializes 'port' with port details. On failure, returns a positive errno
626 * value and clears the contents of 'port' (with wdp_port_clear()).
628 * The caller takes ownership of everything in '*portp' and will eventually
629 * free it with, e.g., wdp_port_free().
631 * Possible error return values include:
633 * - EINVAL: 'port_no' is outside the valid range.
635 * - ENOENT: 'wdp' currently has no port numbered 'port_no'.
638 wdp_port_query_by_number(const struct wdp *wdp, uint16_t port_no,
639 struct wdp_port *port)
643 error = wdp->wdp_class->port_query_by_number(wdp, port_no, port);
645 VLOG_DBG_RL(&wdpmsg_rl, "%s: port %"PRIu16" is device %s",
646 wdp_name(wdp), port_no, port->devname);
648 wdp_port_clear(port);
649 VLOG_WARN_RL(&error_rl, "%s: failed to query port %"PRIu16": %s",
650 wdp_name(wdp), port_no, strerror(error));
655 /* Same as wdp_port_query_by_number() except that it look for a port named
656 * 'devname' in 'wdp'.
658 * Possible error return values include:
660 * - ENODEV: No device named 'devname' exists.
662 * - ENOENT: 'devname' exists but it is not attached as a port on 'wdp'.
665 wdp_port_query_by_name(const struct wdp *wdp, const char *devname,
666 struct wdp_port *port)
668 int error = wdp->wdp_class->port_query_by_name(wdp, devname, port);
670 VLOG_DBG_RL(&wdpmsg_rl, "%s: device %s is on port %"PRIu16,
671 wdp_name(wdp), devname, port->opp.port_no);
673 wdp_port_clear(port);
675 /* Log level is DBG here because all the current callers are interested
676 * in whether 'wdp' actually has a port 'devname', so that it's not
677 * an issue worth logging if it doesn't. */
678 VLOG_DBG_RL(&error_rl, "%s: failed to query port %s: %s",
679 wdp_name(wdp), devname, strerror(error));
684 /* Looks up port number 'port_no' in 'wdp'. On success, returns 0 and stores
685 * a copy of the port's name in '*namep'. On failure, returns a positive errno
686 * value and stores NULL in '*namep'.
688 * Error return values are the same as for wdp_port_query_by_name().
690 * The caller is responsible for freeing '*namep' (with free()). */
692 wdp_port_get_name(struct wdp *wdp, uint16_t port_no, char **namep)
694 struct wdp_port port;
697 error = wdp_port_query_by_number(wdp, port_no, &port);
698 *namep = port.devname;
700 wdp_port_free(&port);
705 /* Obtains a list of all the ports in 'wdp', in no particular order.
707 * If successful, returns 0 and sets '*portsp' to point to an array of struct
708 * wdp_port and '*n_portsp' to the number of pointers in the array. On
709 * failure, returns a positive errno value and sets '*portsp' to NULL and
712 * The caller is responsible for freeing '*portsp' and the individual wdp_port
713 * structures, e.g. with wdp_port_array_free(). */
715 wdp_port_list(const struct wdp *wdp,
716 struct wdp_port **portsp, size_t *n_portsp)
720 error = wdp->wdp_class->port_list(wdp, portsp, n_portsp);
725 log_operation(wdp, "port_list", error);
729 /* Updates the configuration for the port number 'port_no' within 'wdp' to
730 * 'config', which is a set of OpenFlow OFPPC_* constants in host byte order.
731 * Returns 0 if successful, otherwise an OpenFlow error code constructed with
734 wdp_port_set_config(struct wdp *wdp, uint16_t port_no, uint32_t config)
736 return wdp->wdp_class->port_set_config(wdp, port_no, config);
739 /* Polls for changes in the set of ports in 'wdp' since the last call to this
740 * function or, if this is the first call, since 'wdp' was opened. For each
741 * change, calls 'cb' passing 'aux' and:
743 * - For a port that has been added, OFPPR_ADD as 'reason' and the new port's
744 * "struct ofp_phy_port" as 'opp'.
746 * - For a port that has been removed, OFPPR_DELETE as 'reason' and the
747 * deleted port's former "struct ofp_phy_port" as 'opp'.
749 * - For a port whose configuration has changed, OFPPR_MODIFY as 'reason' and
750 * the modified port's new "struct ofp_phy_port" as 'opp'.
752 * 'opp' is in *host* byte order.
754 * Normally returns 0. May also return a positive errno value to indicate
755 * that something has gone wrong.
758 wdp_port_poll(struct wdp *wdp, wdp_port_poll_cb_func *cb, void *aux)
760 return wdp->wdp_class->port_poll(wdp, cb, aux);
763 /* Arranges for the poll loop to wake up when 'port_poll' will call its
766 wdp_port_poll_wait(const struct wdp *wdp)
768 return wdp->wdp_class->port_poll_wait(wdp);
771 /* Deletes all flows from 'wdp'. Returns 0 if successful, otherwise a
772 * positive errno value. */
774 wdp_flow_flush(struct wdp *wdp)
778 COVERAGE_INC(wdp_flow_flush);
780 error = wdp->wdp_class->flow_flush(wdp);
781 log_operation(wdp, "flow_flush", error);
785 /* If 'wdp' contains exactly one flow exactly equal to 'flow' in one of the
786 * tables in the bit-mask in 'include', returns that flow. Otherwise (if there
787 * is no match or more than one match), returns null.
789 * A flow in table 'table_id' is a candidate for matching if 'include & (1u <<
790 * table_id)' is nonzero. */
792 wdp_flow_get(struct wdp *wdp, const flow_t *flow, unsigned int include)
794 return wdp->wdp_class->flow_get(wdp, flow, include);
798 wdp_flow_match(struct wdp *wdp, const flow_t *flow)
800 return wdp->wdp_class->flow_match(wdp, flow);
803 /* Iterates through all of the flows in 'wdp''s flow table, passing each flow
804 * that matches the specified search criteria to 'callback' along with 'aux'.
805 * If 'callback' returns nonzero, then this stops the iteration and
806 * wdp_flow_for_each_match() passes the return value along. Otherwise
807 * wdp_flow_for_each_match() returns zero after all matching flows have been
810 * Flows are filtered out in two ways. First, based on the bit-mask in
811 * 'include': wdp_rule 'wr' is included only if 'include & (1u <<
812 * wr->ofp_table_id)' is nonzero.
814 * Flows are also filtered out based on 'target': on a field-by-field basis, a
815 * flow is included if 'target' wildcards that field or if the flow and
816 * 'target' both have the same exact value for the field. A flow is excluded
817 * if any field does not match based on these criteria.
819 * Ignores 'target->priority'.
821 * 'callback' is allowed to delete the rule that is passed as its argument. It
822 * may modify any flow in 'wdp', e.g. changing their actions. 'callback' must
823 * not delete flows from 'wdp' other than its argument flow, nor may it insert
824 * new flows into 'wdp'. */
826 wdp_flow_for_each_match(const struct wdp *wdp, const flow_t *target,
827 unsigned int include,
828 wdp_flow_cb_func *callback, void *aux)
830 return wdp->wdp_class->flow_for_each_match(wdp, target, include,
835 wdp_flow_get_stats(const struct wdp *wdp, const struct wdp_rule *rule,
836 struct wdp_flow_stats *stats)
838 int error = wdp->wdp_class->flow_get_stats(wdp, rule, stats);
840 memset(stats, 0, sizeof *stats);
846 wdp_flow_overlaps(const struct wdp *wdp, const flow_t *flow)
848 return wdp->wdp_class->flow_overlaps(wdp, flow);
852 wdp_flow_put(struct wdp *wdp, struct wdp_flow_put *put,
853 struct wdp_flow_stats *old_stats, struct wdp_rule **rulep)
855 int error = wdp->wdp_class->flow_put(wdp, put, old_stats, rulep);
858 memset(old_stats, 0, sizeof *old_stats);
868 wdp_flow_delete(struct wdp *wdp, struct wdp_rule *rule,
869 struct wdp_flow_stats *final_stats)
871 int error = wdp->wdp_class->flow_delete(wdp, rule, final_stats);
872 if (error && final_stats) {
873 memset(final_stats, 0, sizeof *final_stats);
879 wdp_flow_inject(struct wdp *wdp, struct wdp_rule *rule,
880 uint16_t in_port, const struct ofpbuf *packet)
882 return wdp->wdp_class->flow_inject(wdp, rule, in_port, packet);
886 wdp_execute(struct wdp *wdp, uint16_t in_port,
887 const union ofp_action actions[], size_t n_actions,
888 const struct ofpbuf *buf)
892 COVERAGE_INC(wdp_execute);
894 error = wdp->wdp_class->execute(wdp, in_port, actions,
902 /* Retrieves 'wdp''s "listen mask" into '*listen_mask'. Each bit set in
903 * '*listen_mask' indicates that wdp_recv() will receive messages of the
904 * corresponding WDP_CHAN_* type. Returns 0 if successful, otherwise a
905 * positive errno value. */
907 wdp_recv_get_mask(const struct wdp *wdp, int *listen_mask)
909 int error = wdp->wdp_class->recv_get_mask(wdp, listen_mask);
913 log_operation(wdp, "recv_get_mask", error);
917 /* Sets 'wdp''s "listen mask" to 'listen_mask'. Each bit set in
918 * '*listen_mask' requests that wdp_recv() receive messages of the
919 * corresponding WDP_CHAN_* type. Returns 0 if successful, otherwise a
920 * positive errno value. */
922 wdp_recv_set_mask(struct wdp *wdp, int listen_mask)
924 int error = wdp->wdp_class->recv_set_mask(wdp, listen_mask);
925 log_operation(wdp, "recv_set_mask", error);
929 /* Retrieve the sFlow sampling probability. '*probability' is expressed as the
930 * number of packets out of UINT_MAX to sample, e.g. probability/UINT_MAX is
931 * the probability of sampling a given packet.
933 * Returns 0 if successful, otherwise a positive errno value. EOPNOTSUPP
934 * indicates that 'wdp' does not support sFlow sampling. */
936 wdp_get_sflow_probability(const struct wdp *wdp, uint32_t *probability)
938 int error = (wdp->wdp_class->get_sflow_probability
939 ? wdp->wdp_class->get_sflow_probability(wdp, probability)
944 log_operation(wdp, "get_sflow_probability", error);
948 /* Set the sFlow sampling probability. 'probability' is expressed as the
949 * number of packets out of UINT_MAX to sample, e.g. probability/UINT_MAX is
950 * the probability of sampling a given packet.
952 * Returns 0 if successful, otherwise a positive errno value. EOPNOTSUPP
953 * indicates that 'wdp' does not support sFlow sampling. */
955 wdp_set_sflow_probability(struct wdp *wdp, uint32_t probability)
957 int error = (wdp->wdp_class->set_sflow_probability
958 ? wdp->wdp_class->set_sflow_probability(wdp, probability)
960 log_operation(wdp, "set_sflow_probability", error);
964 /* Attempts to receive a message from 'wdp'. If successful, stores the
965 * message into '*packetp'. Only messages of the types selected with
966 * wdp_set_listen_mask() will ordinarily be received (but if a message type
967 * is enabled and then later disabled, some stragglers might pop up).
969 * Returns 0 if successful, otherwise a positive errno value. Returns EAGAIN
970 * if no message is immediately available. */
972 wdp_recv(struct wdp *wdp, struct wdp_packet *packet)
974 int error = wdp->wdp_class->recv(wdp, packet);
976 /* XXX vlog_dbg received packet */
978 memset(packet, 0, sizeof *packet);
979 packet->channel = -1;
984 /* Discards all messages that would otherwise be received by wdp_recv() on
985 * 'wdp'. Returns 0 if successful, otherwise a positive errno value. */
987 wdp_recv_purge(struct wdp *wdp)
989 COVERAGE_INC(wdp_purge);
990 return wdp->wdp_class->recv_purge(wdp);
993 /* Arranges for the poll loop to wake up when 'wdp' has a message queued to be
994 * received with wdp_recv(). */
996 wdp_recv_wait(struct wdp *wdp)
998 wdp->wdp_class->recv_wait(wdp);
1001 /* Obtains the NetFlow engine type and engine ID for 'wdp' into '*engine_type'
1002 * and '*engine_id', respectively. */
1004 wdp_get_netflow_ids(const struct wdp *wdp,
1005 uint8_t *engine_type, uint8_t *engine_id)
1007 *engine_type = wdp->netflow_engine_type;
1008 *engine_id = wdp->netflow_engine_id;
1011 /* ovs-vswitchd interface.
1013 * This needs to be redesigned, because it only makes sense for wdp-xflow. The
1014 * ofhooks are currently the key to implementing the OFPP_NORMAL feature of
1017 /* Sets the ofhooks for 'wdp' to 'ofhooks' with the accompanying 'aux' value.
1018 * Only the xflow implementation of wdp is expected to implement this function;
1019 * other implementations should just set it to NULL.
1021 * The ofhooks are currently the key to implementing the OFPP_NORMAL feature of
1022 * ovs-vswitchd. This design is not adequate for the long term; it needs to be
1025 * Returns 0 if successful, otherwise a positive errno value. */
1027 wdp_set_ofhooks(struct wdp *wdp, const struct ofhooks *ofhooks, void *aux)
1030 error = (wdp->wdp_class->set_ofhooks
1031 ? wdp->wdp_class->set_ofhooks(wdp, ofhooks, aux)
1033 log_operation(wdp, "set_ofhooks", error);
1037 /* Tell 'wdp' to revalidate all the flows that match 'tag'.
1039 * This needs to be redesigned, because it only makes sense for wdp-xflow.
1040 * Other implementations cannot practically use this interface and should just
1041 * set this to NULL. */
1043 wdp_revalidate(struct wdp *wdp, tag_type tag)
1045 if (wdp->wdp_class->revalidate) {
1046 wdp->wdp_class->revalidate(wdp, tag);
1050 /* Tell 'wdp' to revalidate every flow. (This is not the same as calling
1051 * 'revalidate' with all-1-bits for 'tag' because it also revalidates flows
1052 * that do not have any tag at all.)
1054 * This needs to be redesigned, because it only makes sense for wdp-xflow.
1055 * Other implementations cannot practically use this interface and should just
1056 * set this to NULL. */
1058 wdp_revalidate_all(struct wdp *wdp)
1060 if (wdp->wdp_class->revalidate_all) {
1061 wdp->wdp_class->revalidate_all(wdp);
1065 /* Returns a copy of 'old'. The packet's payload, if any, is copied as well,
1066 * but if it is longer than 'trim' bytes it is truncated to that length. */
1068 wdp_packet_clone(const struct wdp_packet *old, size_t trim)
1070 struct wdp_packet *new = xmemdup(old, sizeof *old);
1072 new->payload = ofpbuf_clone_data(old->payload->data,
1073 MIN(trim, old->payload->size));
1079 wdp_packet_destroy(struct wdp_packet *packet)
1082 ofpbuf_delete(packet->payload);
1088 wdp_init(struct wdp *wdp, const struct wdp_class *wdp_class,
1090 uint8_t netflow_engine_type, uint8_t netflow_engine_id)
1092 wdp->wdp_class = wdp_class;
1093 wdp->base_name = xstrdup(name);
1094 wdp->full_name = xasprintf("%s@%s", wdp_class->type, name);
1095 wdp->netflow_engine_type = netflow_engine_type;
1096 wdp->netflow_engine_id = netflow_engine_id;
1099 /* Undoes the results of initialization.
1101 * Normally this function only needs to be called from wdp_close().
1102 * However, it may be called by providers due to an error on opening
1103 * that occurs after initialization. It this case wdp_close() would
1104 * never be called. */
1106 wdp_uninit(struct wdp *wdp, bool close)
1108 char *base_name = wdp->base_name;
1109 char *full_name = wdp->full_name;
1112 wdp->wdp_class->close(wdp);
1120 log_operation(const struct wdp *wdp, const char *operation, int error)
1123 VLOG_DBG_RL(&wdpmsg_rl, "%s: %s success", wdp_name(wdp), operation);
1125 VLOG_WARN_RL(&error_rl, "%s: %s failed (%s)",
1126 wdp_name(wdp), operation, strerror(error));