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"
41 #include "wdp-xflow.h"
44 #define THIS_MODULE VLM_wdp
48 /* Initializes a new 'struct wdp_rule', copying in the 'n_actions' elements of
51 * The caller is responsible for initializing 'rule->cr'. */
53 wdp_rule_init(struct wdp_rule *rule, const union ofp_action *actions,
56 rule->actions = xmemdup(actions, n_actions * sizeof *actions);
57 rule->n_actions = n_actions;
58 rule->created = time_msec();
59 rule->idle_timeout = 0;
60 rule->hard_timeout = 0;
61 rule->client_data = NULL;
64 /* Frees the data in 'rule'. */
66 wdp_rule_uninit(struct wdp_rule *rule)
73 static const struct wdp_class *base_wdp_classes[] = {
77 struct registered_wdp_class {
78 const struct wdp_class *wdp_class;
82 static struct shash wdp_classes = SHASH_INITIALIZER(&wdp_classes);
84 /* Rate limit for individual messages going to or from the datapath, output at
85 * DBG level. This is very high because, if these are enabled, it is because
86 * we really need to see them. */
87 static struct vlog_rate_limit wdpmsg_rl = VLOG_RATE_LIMIT_INIT(600, 600);
89 /* Not really much point in logging many wdp errors. */
90 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
92 static void log_operation(const struct wdp *, const char *operation,
98 static int status = -1;
104 for (i = 0; i < ARRAY_SIZE(base_wdp_classes); i++) {
105 wdp_register_provider(base_wdp_classes[i]);
107 wdp_xflow_register();
111 /* Performs periodic work needed by all the various kinds of wdps.
113 * If your program opens any wdps, it must call both this function and
114 * netdev_run() within its main poll loop. */
118 struct shash_node *node;
119 SHASH_FOR_EACH (node, &wdp_classes) {
120 const struct registered_wdp_class *registered_class = node->data;
121 if (registered_class->wdp_class->run) {
122 registered_class->wdp_class->run();
127 /* Arranges for poll_block() to wake up when wdp_run() needs to be called.
129 * If your program opens any wdps, it must call both this function and
130 * netdev_wait() within its main poll loop. */
134 struct shash_node *node;
135 SHASH_FOR_EACH(node, &wdp_classes) {
136 const struct registered_wdp_class *registered_class = node->data;
137 if (registered_class->wdp_class->wait) {
138 registered_class->wdp_class->wait();
143 /* Registers a new datapath provider. After successful registration, new
144 * datapaths of that type can be opened using wdp_open(). */
146 wdp_register_provider(const struct wdp_class *new_class)
148 struct registered_wdp_class *registered_class;
150 if (shash_find(&wdp_classes, new_class->type)) {
151 VLOG_WARN("attempted to register duplicate datapath provider: %s",
156 registered_class = xmalloc(sizeof *registered_class);
157 registered_class->wdp_class = new_class;
158 registered_class->refcount = 0;
160 shash_add(&wdp_classes, new_class->type, registered_class);
165 /* Unregisters a datapath provider. 'type' must have been previously
166 * registered and not currently be in use by any wdps. After unregistration
167 * new datapaths of that type cannot be opened using wdp_open(). */
169 wdp_unregister_provider(const char *type)
171 struct shash_node *node;
172 struct registered_wdp_class *registered_class;
174 node = shash_find(&wdp_classes, type);
176 VLOG_WARN("attempted to unregister a datapath provider that is not "
177 "registered: %s", type);
181 registered_class = node->data;
182 if (registered_class->refcount) {
183 VLOG_WARN("attempted to unregister in use datapath provider: %s",
188 shash_delete(&wdp_classes, node);
189 free(registered_class);
194 /* Clears 'types' and enumerates the types of all currently registered wdp
195 * providers into it. The caller must first initialize the svec. */
197 wdp_enumerate_types(struct svec *types)
199 struct shash_node *node;
204 SHASH_FOR_EACH (node, &wdp_classes) {
205 const struct registered_wdp_class *registered_class = node->data;
206 svec_add(types, registered_class->wdp_class->type);
210 /* Clears 'names' and enumerates the names of all known created datapaths
211 * with the given 'type'. The caller must first initialize the svec. Returns 0
212 * if successful, otherwise a positive errno value.
214 * Some kinds of datapaths might not be practically enumerable. This is not
215 * considered an error. */
217 wdp_enumerate_names(const char *type, struct svec *names)
219 const struct registered_wdp_class *registered_class;
220 const struct wdp_class *wdp_class;
226 registered_class = shash_find_data(&wdp_classes, type);
227 if (!registered_class) {
228 VLOG_WARN("could not enumerate unknown type: %s", type);
232 wdp_class = registered_class->wdp_class;
233 error = (wdp_class->enumerate
234 ? wdp_class->enumerate(wdp_class, names)
238 VLOG_WARN("failed to enumerate %s datapaths: %s", wdp_class->type,
245 /* Parses 'datapath_name', which is of the form type@name, into its
246 * component pieces. 'name' and 'type' must be freed by the caller. */
248 wdp_parse_name(const char *datapath_name_, char **name, char **type)
250 char *datapath_name = xstrdup(datapath_name_);
253 separator = strchr(datapath_name, '@');
256 *type = datapath_name;
257 *name = xstrdup(separator + 1);
259 *name = datapath_name;
265 do_open(const char *name, const char *type, bool create, struct wdp **wdpp)
267 struct wdp *wdp = NULL;
269 struct registered_wdp_class *registered_class;
273 if (!type || *type == '\0') {
277 registered_class = shash_find_data(&wdp_classes, type);
278 if (!registered_class) {
279 VLOG_WARN("could not create datapath %s of unknown type %s", name,
281 error = EAFNOSUPPORT;
285 error = registered_class->wdp_class->open(registered_class->wdp_class,
288 registered_class->refcount++;
292 *wdpp = error ? NULL : wdp;
296 /* Tries to open an existing datapath named 'name' and type 'type'. Will fail
297 * if no datapath with 'name' and 'type' exists. 'type' may be either NULL or
298 * the empty string to specify the default system type. Returns 0 if
299 * successful, otherwise a positive errno value. On success stores a pointer
300 * to the datapath in '*wdpp', otherwise a null pointer. */
302 wdp_open(const char *name, const char *type, struct wdp **wdpp)
304 return do_open(name, type, false, wdpp);
307 /* Tries to create and open a new datapath with the given 'name' and 'type'.
308 * 'type' may be either NULL or the empty string to specify the default system
309 * type. Will fail if a datapath with 'name' and 'type' already exists.
310 * Returns 0 if successful, otherwise a positive errno value. On success
311 * stores a pointer to the datapath in '*wdpp', otherwise a null pointer. */
313 wdp_create(const char *name, const char *type, struct wdp **wdpp)
315 return do_open(name, type, true, wdpp);
318 /* Tries to open a datapath with the given 'name' and 'type', creating it if it
319 * does not exist. 'type' may be either NULL or the empty string to specify
320 * the default system type. Returns 0 if successful, otherwise a positive
321 * errno value. On success stores a pointer to the datapath in '*wdpp',
322 * otherwise a null pointer. */
324 wdp_create_and_open(const char *name, const char *type, struct wdp **wdpp)
328 error = wdp_create(name, type, wdpp);
329 if (error == EEXIST || error == EBUSY) {
330 error = wdp_open(name, type, wdpp);
332 VLOG_WARN("datapath %s already exists but cannot be opened: %s",
333 name, strerror(error));
336 VLOG_WARN("failed to create datapath %s: %s", name, strerror(error));
341 /* Closes and frees the connection to 'wdp'. Does not destroy the wdp
342 * itself; call wdp_delete() first, instead, if that is desirable. */
344 wdp_close(struct wdp *wdp)
347 struct registered_wdp_class *registered_class;
349 registered_class = shash_find_data(&wdp_classes,
350 wdp->wdp_class->type);
351 assert(registered_class);
352 assert(registered_class->refcount);
354 registered_class->refcount--;
355 wdp_uninit(wdp, true);
359 /* Returns the name of datapath 'wdp' prefixed with the type
360 * (for use in log messages). */
362 wdp_name(const struct wdp *wdp)
364 return wdp->full_name;
367 /* Returns the name of datapath 'wdp' without the type
368 * (for use in device names). */
370 wdp_base_name(const struct wdp *wdp)
372 return wdp->base_name;
375 /* Enumerates all names that may be used to open 'wdp' into 'all_names'. The
376 * Linux datapath, for example, supports opening a datapath both by number,
377 * e.g. "wdp0", and by the name of the datapath's local port. For some
378 * datapaths, this might be an infinite set (e.g. in a file name, slashes may
379 * be duplicated any number of times), in which case only the names most likely
380 * to be used will be enumerated.
382 * The caller must already have initialized 'all_names'. Any existing names in
383 * 'all_names' will not be disturbed. */
385 wdp_get_all_names(const struct wdp *wdp, struct svec *all_names)
387 if (wdp->wdp_class->get_all_names) {
388 int error = wdp->wdp_class->get_all_names(wdp, all_names);
390 VLOG_WARN_RL(&error_rl,
391 "failed to retrieve names for datpath %s: %s",
392 wdp_name(wdp), strerror(error));
396 svec_add(all_names, wdp_base_name(wdp));
401 /* Destroys the datapath that 'wdp' is connected to, first removing all of
402 * its ports. After calling this function, it does not make sense to pass
403 * 'wdp' to any functions other than wdp_name() or wdp_close(). */
405 wdp_delete(struct wdp *wdp)
409 COVERAGE_INC(wdp_destroy);
411 error = wdp->wdp_class->destroy(wdp);
412 log_operation(wdp, "delete", error);
416 /* Obtains the set of features supported by 'wdp'.
418 * If successful, returns 0 and stores in '*featuresp' a newly allocated
419 * "struct ofp_switch_features" that describes the features and ports supported
420 * by 'wdp'. The caller is responsible for initializing the header,
421 * datapath_id, and n_buffers members of the returned "struct
422 * ofp_switch_features". The caller must free the returned buffer (with
423 * ofpbuf_delete()) when it is no longer needed.
425 * On error, returns an OpenFlow error code (as constructed by ofp_mkerr()) and
426 * sets '*featuresp' to NULL. */
428 wdp_get_features(const struct wdp *wdp, struct ofpbuf **featuresp)
430 int error = wdp->wdp_class->get_features(wdp, featuresp);
437 /* Retrieves statistics for 'wdp' into 'stats'. Returns 0 if successful,
438 * otherwise a positive errno value. On error, clears 'stats' to
441 wdp_get_wdp_stats(const struct wdp *wdp, struct wdp_stats *stats)
443 int error = wdp->wdp_class->get_stats(wdp, stats);
445 memset(stats, 0, sizeof *stats);
447 log_operation(wdp, "get_stats", error);
451 /* Retrieves the current IP fragment handling policy for 'wdp' into
452 * '*drop_frags': true indicates that fragments are dropped, false indicates
453 * that fragments are treated in the same way as other IP packets (except that
454 * the L4 header cannot be read). Returns 0 if successful, otherwise a
455 * positive errno value. */
457 wdp_get_drop_frags(const struct wdp *wdp, bool *drop_frags)
459 int error = wdp->wdp_class->get_drop_frags(wdp, drop_frags);
463 log_operation(wdp, "get_drop_frags", error);
467 /* Changes 'wdp''s treatment of IP fragments to 'drop_frags', whose meaning is
468 * the same as for the get_drop_frags member function. Returns 0 if
469 * successful, otherwise a positive errno value. EOPNOTSUPP indicates that
470 * 'wdp''s fragment dropping policy is not configurable. */
472 wdp_set_drop_frags(struct wdp *wdp, bool drop_frags)
475 error = (wdp->wdp_class->set_drop_frags
476 ? wdp->wdp_class->set_drop_frags(wdp, drop_frags)
478 log_operation(wdp, "set_drop_frags", error);
482 /* Clears the contents of 'port'. */
484 wdp_port_clear(struct wdp_port *port)
486 memset(port, 0, sizeof *port);
489 /* Makes a deep copy of 'old' in 'port'. The caller may free 'port''s data
490 * with wdp_port_free(). */
492 wdp_port_copy(struct wdp_port *port, const struct wdp_port *old)
494 port->netdev = old->netdev ? netdev_reopen(old->netdev) : NULL;
495 port->opp = old->opp;
496 port->devname = old->devname ? xstrdup(old->devname) : NULL;
497 port->internal = old->internal;
500 /* Frees the data that 'port' points to (but not 'port' itself). */
502 wdp_port_free(struct wdp_port *port)
505 netdev_close(port->netdev);
510 /* Frees the data that each of the 'n' ports in 'ports' points to, and then
511 * frees 'ports' itself. */
513 wdp_port_array_free(struct wdp_port *ports, size_t n)
517 for (i = 0; i < n; i++) {
518 wdp_port_free(&ports[i]);
523 /* Attempts to add 'devname' as a port on 'wdp':
525 * - If 'internal' is true, attempts to create a new internal port (a virtual
526 * port implemented in software) by that name.
528 * - If 'internal' is false, 'devname' must name an existing network device.
530 * If successful, returns 0 and sets '*port_nop' to the new port's OpenFlow
531 * port number (if 'port_nop' is non-null). On failure, returns a positive
532 * errno value and sets '*port_nop' to OFPP_NONE (if 'port_nop' is non-null).
534 * Some wildcarded datapaths might have fixed sets of ports. For these
535 * datapaths this function will always fail.
537 * Possible error return values include:
539 * - ENODEV: No device named 'devname' exists (if 'internal' is false).
541 * - EEXIST: A device named 'devname' already exists (if 'internal' is true).
543 * - EINVAL: Device 'devname' is not supported as part of a datapath (e.g. it
544 * is not an Ethernet device), or 'devname' is too long for a network
545 * device name (if 'internal' is true)
547 * - EFBIG: The datapath already has as many ports as it can support.
549 * - EOPNOTSUPP: 'wdp' has a fixed set of ports.
552 wdp_port_add(struct wdp *wdp, const char *devname,
553 bool internal, uint16_t *port_nop)
558 COVERAGE_INC(wdp_port_add);
560 error = (wdp->wdp_class->port_add
561 ? wdp->wdp_class->port_add(wdp, devname, internal, &port_no)
564 VLOG_DBG_RL(&wdpmsg_rl, "%s: added %s as port %"PRIu16,
565 wdp_name(wdp), devname, port_no);
567 VLOG_WARN_RL(&error_rl, "%s: failed to add %s as port: %s",
568 wdp_name(wdp), devname, strerror(error));
577 /* Attempts to remove 'wdp''s port numbered 'port_no'. Returns 0 if
578 * successful, otherwise a positive errno value.
580 * Some wildcarded datapaths might have fixed sets of ports. For these
581 * datapaths this function will always fail.
583 * Possible error return values include:
585 * - EINVAL: 'port_no' is outside the valid range, or this particular port is
586 * not removable (e.g. it is the local port).
588 * - ENOENT: 'wdp' currently has no port numbered 'port_no'.
590 * - EOPNOTSUPP: 'wdp' has a fixed set of ports.
593 wdp_port_del(struct wdp *wdp, uint16_t port_no)
597 COVERAGE_INC(wdp_port_del);
599 error = (wdp->wdp_class->port_del
600 ? wdp->wdp_class->port_del(wdp, port_no)
602 log_operation(wdp, "port_del", error);
606 /* Looks up port number 'port_no' in 'wdp'. On success, returns 0 and
607 * initializes 'port' with port details. On failure, returns a positive errno
608 * value and clears the contents of 'port' (with wdp_port_clear()).
610 * The caller must not modify or free the returned wdp_port. Calling
611 * wdp_run() or wdp_port_poll() may free the returned wdp_port.
613 * Possible error return values include:
615 * - EINVAL: 'port_no' is outside the valid range.
617 * - ENOENT: 'wdp' currently has no port numbered 'port_no'.
620 wdp_port_query_by_number(const struct wdp *wdp, uint16_t port_no,
621 struct wdp_port *port)
625 error = wdp->wdp_class->port_query_by_number(wdp, port_no, port);
627 VLOG_DBG_RL(&wdpmsg_rl, "%s: port %"PRIu16" is device %s",
628 wdp_name(wdp), port_no, port->devname);
630 wdp_port_clear(port);
631 VLOG_WARN_RL(&error_rl, "%s: failed to query port %"PRIu16": %s",
632 wdp_name(wdp), port_no, strerror(error));
637 /* Same as wdp_port_query_by_number() except that it look for a port named
638 * 'devname' in 'wdp'.
640 * Possible error return values include:
642 * - ENODEV: No device named 'devname' exists.
644 * - ENOENT: 'devname' exists but it is not attached as a port on 'wdp'.
647 wdp_port_query_by_name(const struct wdp *wdp, const char *devname,
648 struct wdp_port *port)
650 int error = wdp->wdp_class->port_query_by_name(wdp, devname, port);
652 VLOG_DBG_RL(&wdpmsg_rl, "%s: device %s is on port %"PRIu16,
653 wdp_name(wdp), devname, port->opp.port_no);
655 wdp_port_clear(port);
657 /* Log level is DBG here because all the current callers are interested
658 * in whether 'wdp' actually has a port 'devname', so that it's not
659 * an issue worth logging if it doesn't. */
660 VLOG_DBG_RL(&error_rl, "%s: failed to query port %s: %s",
661 wdp_name(wdp), devname, strerror(error));
666 /* Looks up port number 'port_no' in 'wdp'. On success, returns 0 and stores
667 * a copy of the port's name in '*namep'. On failure, returns a positive errno
668 * value and stores NULL in '*namep'.
670 * Error return values are the same as for wdp_port_query_by_name().
672 * The caller is responsible for freeing '*namep' (with free()). */
674 wdp_port_get_name(struct wdp *wdp, uint16_t port_no, char **namep)
676 struct wdp_port port;
679 error = wdp_port_query_by_number(wdp, port_no, &port);
680 *namep = port.devname;
682 wdp_port_free(&port);
687 /* Obtains a list of all the ports in 'wdp', in no particular order.
689 * If successful, returns 0 and sets '*portsp' to point to an array of struct
690 * wdp_port and '*n_portsp' to the number of pointers in the array. On
691 * failure, returns a positive errno value and sets '*portsp' to NULL and
694 * The caller is responsible for freeing '*portsp' and the individual wdp_port
695 * structures, e.g. with wdp_port_array_free(). */
697 wdp_port_list(const struct wdp *wdp,
698 struct wdp_port **portsp, size_t *n_portsp)
702 error = wdp->wdp_class->port_list(wdp, portsp, n_portsp);
707 log_operation(wdp, "port_list", error);
712 wdp_port_set_config(struct wdp *wdp, uint16_t port_no, uint32_t config)
714 return wdp->wdp_class->port_set_config(wdp, port_no, config);
717 /* Polls for changes in the set of ports in 'wdp'. If the set of ports in
718 * 'wdp' has changed, this function does one of the following:
720 * - Stores the name of the device that was added to or deleted from 'wdp' in
721 * '*devnamep' and returns 0. The caller is responsible for freeing
722 * '*devnamep' (with free()) when it no longer needs it.
724 * - Returns ENOBUFS and sets '*devnamep' to NULL.
726 * This function may also return 'false positives', where it returns 0 and
727 * '*devnamep' names a device that was not actually added or deleted or it
728 * returns ENOBUFS without any change.
730 * Returns EAGAIN if the set of ports in 'wdp' has not changed. May also
731 * return other positive errno values to indicate that something has gone
734 wdp_port_poll(const struct wdp *wdp, char **devnamep)
736 int error = (wdp->wdp_class->port_poll
737 ? wdp->wdp_class->port_poll(wdp, devnamep)
745 /* Arranges for the poll loop to wake up when port_poll(wdp) will return a
746 * value other than EAGAIN. */
748 wdp_port_poll_wait(const struct wdp *wdp)
750 if (wdp->wdp_class->port_poll_wait) {
751 wdp->wdp_class->port_poll_wait(wdp);
755 /* Deletes all flows from 'wdp'. Returns 0 if successful, otherwise a
756 * positive errno value. */
758 wdp_flow_flush(struct wdp *wdp)
762 COVERAGE_INC(wdp_flow_flush);
764 error = wdp->wdp_class->flow_flush(wdp);
765 log_operation(wdp, "flow_flush", error);
770 wdp_flow_get(struct wdp *wdp, const flow_t *flow)
772 return wdp->wdp_class->flow_get(wdp, flow);
776 wdp_flow_match(struct wdp *wdp, const flow_t *flow)
778 return wdp->wdp_class->flow_match(wdp, flow);
782 wdp_flow_for_each_match(const struct wdp *wdp, const flow_t *target,
783 int include, wdp_flow_cb_func *callback, void *aux)
785 wdp->wdp_class->flow_for_each_match(wdp, target, include,
790 wdp_flow_get_stats(const struct wdp *wdp, const struct wdp_rule *rule,
791 struct wdp_flow_stats *stats)
793 int error = wdp->wdp_class->flow_get_stats(wdp, rule, stats);
795 memset(stats, 0, sizeof *stats);
801 wdp_flow_overlaps(const struct wdp *wdp, const flow_t *flow)
803 return wdp->wdp_class->flow_overlaps(wdp, flow);
807 wdp_flow_put(struct wdp *wdp, struct wdp_flow_put *put,
808 struct wdp_flow_stats *old_stats, struct wdp_rule **rulep)
810 int error = wdp->wdp_class->flow_put(wdp, put, old_stats, rulep);
813 memset(old_stats, 0, sizeof *old_stats);
823 wdp_flow_delete(struct wdp *wdp, struct wdp_rule *rule,
824 struct wdp_flow_stats *final_stats)
826 int error = wdp->wdp_class->flow_delete(wdp, rule, final_stats);
827 if (error && final_stats) {
828 memset(final_stats, 0, sizeof *final_stats);
834 wdp_flow_inject(struct wdp *wdp, struct wdp_rule *rule,
835 uint16_t in_port, const struct ofpbuf *packet)
837 return wdp->wdp_class->flow_inject(wdp, rule, in_port, packet);
841 wdp_execute(struct wdp *wdp, uint16_t in_port,
842 const union ofp_action actions[], size_t n_actions,
843 const struct ofpbuf *buf)
847 COVERAGE_INC(wdp_execute);
849 error = wdp->wdp_class->execute(wdp, in_port, actions,
857 /* Retrieves 'wdp''s "listen mask" into '*listen_mask'. Each bit set in
858 * '*listen_mask' indicates that wdp_recv() will receive messages of the
859 * corresponding WDP_CHAN_* type. Returns 0 if successful, otherwise a
860 * positive errno value. */
862 wdp_recv_get_mask(const struct wdp *wdp, int *listen_mask)
864 int error = wdp->wdp_class->recv_get_mask(wdp, listen_mask);
868 log_operation(wdp, "recv_get_mask", error);
872 /* Sets 'wdp''s "listen mask" to 'listen_mask'. Each bit set in
873 * '*listen_mask' requests that wdp_recv() receive messages of the
874 * corresponding WDP_CHAN_* type. Returns 0 if successful, otherwise a
875 * positive errno value. */
877 wdp_recv_set_mask(struct wdp *wdp, int listen_mask)
879 int error = wdp->wdp_class->recv_set_mask(wdp, listen_mask);
880 log_operation(wdp, "recv_set_mask", error);
884 /* Retrieve the sFlow sampling probability. '*probability' is expressed as the
885 * number of packets out of UINT_MAX to sample, e.g. probability/UINT_MAX is
886 * the probability of sampling a given packet.
888 * Returns 0 if successful, otherwise a positive errno value. EOPNOTSUPP
889 * indicates that 'wdp' does not support sFlow sampling. */
891 wdp_get_sflow_probability(const struct wdp *wdp, uint32_t *probability)
893 int error = (wdp->wdp_class->get_sflow_probability
894 ? wdp->wdp_class->get_sflow_probability(wdp, probability)
899 log_operation(wdp, "get_sflow_probability", error);
903 /* Set the sFlow sampling probability. 'probability' is expressed as the
904 * number of packets out of UINT_MAX to sample, e.g. probability/UINT_MAX is
905 * the probability of sampling a given packet.
907 * Returns 0 if successful, otherwise a positive errno value. EOPNOTSUPP
908 * indicates that 'wdp' does not support sFlow sampling. */
910 wdp_set_sflow_probability(struct wdp *wdp, uint32_t probability)
912 int error = (wdp->wdp_class->set_sflow_probability
913 ? wdp->wdp_class->set_sflow_probability(wdp, probability)
915 log_operation(wdp, "set_sflow_probability", error);
919 /* Attempts to receive a message from 'wdp'. If successful, stores the
920 * message into '*packetp'. Only messages of the types selected with
921 * wdp_set_listen_mask() will ordinarily be received (but if a message type
922 * is enabled and then later disabled, some stragglers might pop up).
924 * Returns 0 if successful, otherwise a positive errno value. Returns EAGAIN
925 * if no message is immediately available. */
927 wdp_recv(struct wdp *wdp, struct wdp_packet *packet)
929 int error = wdp->wdp_class->recv(wdp, packet);
931 /* XXX vlog_dbg received packet */
933 memset(packet, 0, sizeof *packet);
934 packet->channel = -1;
939 /* Discards all messages that would otherwise be received by wdp_recv() on
940 * 'wdp'. Returns 0 if successful, otherwise a positive errno value. */
942 wdp_recv_purge(struct wdp *wdp)
944 struct wdp_stats stats;
948 COVERAGE_INC(wdp_purge);
950 error = wdp_get_wdp_stats(wdp, &stats);
955 for (i = 0; i < stats.max_miss_queue + stats.max_action_queue + stats.max_sflow_queue; i++) {
956 struct wdp_packet packet;
958 error = wdp_recv(wdp, &packet);
960 return error == EAGAIN ? 0 : error;
962 ofpbuf_delete(packet.payload);
967 /* Arranges for the poll loop to wake up when 'wdp' has a message queued to be
968 * received with wdp_recv(). */
970 wdp_recv_wait(struct wdp *wdp)
972 wdp->wdp_class->recv_wait(wdp);
975 /* Obtains the NetFlow engine type and engine ID for 'wdp' into '*engine_type'
976 * and '*engine_id', respectively. */
978 wdp_get_netflow_ids(const struct wdp *wdp,
979 uint8_t *engine_type, uint8_t *engine_id)
981 *engine_type = wdp->netflow_engine_type;
982 *engine_id = wdp->netflow_engine_id;
985 /* Returns a copy of 'old'. The packet's payload, if any, is copied as well,
986 * but if it is longer than 'trim' bytes it is truncated to that length. */
988 wdp_packet_clone(const struct wdp_packet *old, size_t trim)
990 struct wdp_packet *new = xmemdup(old, sizeof *old);
992 new->payload = ofpbuf_clone_data(old->payload->data,
993 MIN(trim, old->payload->size));
999 wdp_packet_destroy(struct wdp_packet *packet)
1002 ofpbuf_delete(packet->payload);
1008 wdp_init(struct wdp *wdp, const struct wdp_class *wdp_class,
1010 uint8_t netflow_engine_type, uint8_t netflow_engine_id)
1012 wdp->wdp_class = wdp_class;
1013 wdp->base_name = xstrdup(name);
1014 wdp->full_name = xasprintf("%s@%s", wdp_class->type, name);
1015 wdp->netflow_engine_type = netflow_engine_type;
1016 wdp->netflow_engine_id = netflow_engine_id;
1019 /* Undoes the results of initialization.
1021 * Normally this function only needs to be called from wdp_close().
1022 * However, it may be called by providers due to an error on opening
1023 * that occurs after initialization. It this case wdp_close() would
1024 * never be called. */
1026 wdp_uninit(struct wdp *wdp, bool close)
1028 char *base_name = wdp->base_name;
1029 char *full_name = wdp->full_name;
1032 wdp->wdp_class->close(wdp);
1040 log_operation(const struct wdp *wdp, const char *operation, int error)
1043 VLOG_DBG_RL(&wdpmsg_rl, "%s: %s success", wdp_name(wdp), operation);
1045 VLOG_WARN_RL(&error_rl, "%s: %s failed (%s)",
1046 wdp_name(wdp), operation, strerror(error));