ofproto: Start work to enable datapaths with built-in wildcard support.
[sliver-openvswitch.git] / ofproto / wdp.c
1 /*
2  * Copyright (c) 2008, 2009, 2010 Nicira Networks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18 #include "wdp-provider.h"
19
20 #include <assert.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <inttypes.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "coverage.h"
28 #include "dynamic-string.h"
29 #include "flow.h"
30 #include "netlink.h"
31 #include "ofp-print.h"
32 #include "ofpbuf.h"
33 #include "packets.h"
34 #include "poll-loop.h"
35 #include "shash.h"
36 #include "svec.h"
37 #include "timeval.h"
38 #include "util.h"
39 #include "valgrind.h"
40 #include "wdp-xflow.h"
41
42 #include "vlog.h"
43 #define THIS_MODULE VLM_wdp
44 \f
45 /* wdp_rule */
46
47 /* The caller is responsible for initialization 'rule->cr'. */
48 void
49 wdp_rule_init(struct wdp_rule *rule, const union ofp_action *actions,
50               size_t n_actions)
51 {
52     rule->actions = xmemdup(actions, n_actions * sizeof *actions);
53     rule->n_actions = n_actions;
54     rule->created = time_msec();
55     rule->idle_timeout = 0;
56     rule->hard_timeout = 0;
57     rule->client_data = NULL;
58 }
59
60 void
61 wdp_rule_uninit(struct wdp_rule *rule)
62 {
63     free(rule->actions);
64 }
65 \f
66 /* wdp */
67
68 static const struct wdp_class *base_wdp_classes[] = {
69     /* XXX none yet */
70 };
71
72 struct registered_wdp_class {
73     const struct wdp_class *wdp_class;
74     int refcount;
75 };
76
77 static struct shash wdp_classes = SHASH_INITIALIZER(&wdp_classes);
78
79 /* Rate limit for individual messages going to or from the datapath, output at
80  * DBG level.  This is very high because, if these are enabled, it is because
81  * we really need to see them. */
82 static struct vlog_rate_limit wdpmsg_rl = VLOG_RATE_LIMIT_INIT(600, 600);
83
84 /* Not really much point in logging many wdp errors. */
85 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
86
87 static void log_operation(const struct wdp *, const char *operation,
88                           int error);
89
90 static void
91 wdp_initialize(void)
92 {
93     static int status = -1;
94
95     if (status < 0) {
96         int i;
97
98         status = 0;
99         for (i = 0; i < ARRAY_SIZE(base_wdp_classes); i++) {
100             wdp_register_provider(base_wdp_classes[i]);
101         }
102         wdp_xflow_register();
103     }
104 }
105
106 /* Performs periodic work needed by all the various kinds of wdps.
107  *
108  * If your program opens any wdps, it must call both this function and
109  * netdev_run() within its main poll loop. */
110 void
111 wdp_run(void)
112 {
113     struct shash_node *node;
114     SHASH_FOR_EACH(node, &wdp_classes) {
115         const struct registered_wdp_class *registered_class = node->data;
116         if (registered_class->wdp_class->run) {
117             registered_class->wdp_class->run();
118         }
119     }
120 }
121
122 /* Arranges for poll_block() to wake up when wdp_run() needs to be called.
123  *
124  * If your program opens any wdps, it must call both this function and
125  * netdev_wait() within its main poll loop. */
126 void
127 wdp_wait(void)
128 {
129     struct shash_node *node;
130     SHASH_FOR_EACH(node, &wdp_classes) {
131         const struct registered_wdp_class *registered_class = node->data;
132         if (registered_class->wdp_class->wait) {
133             registered_class->wdp_class->wait();
134         }
135     }
136 }
137
138 /* Registers a new datapath provider.  After successful registration, new
139  * datapaths of that type can be opened using wdp_open(). */
140 int
141 wdp_register_provider(const struct wdp_class *new_class)
142 {
143     struct registered_wdp_class *registered_class;
144
145     if (shash_find(&wdp_classes, new_class->type)) {
146         VLOG_WARN("attempted to register duplicate datapath provider: %s",
147                   new_class->type);
148         return EEXIST;
149     }
150
151     registered_class = xmalloc(sizeof *registered_class);
152     registered_class->wdp_class = new_class;
153     registered_class->refcount = 0;
154
155     shash_add(&wdp_classes, new_class->type, registered_class);
156
157     return 0;
158 }
159
160 /* Unregisters a datapath provider.  'type' must have been previously
161  * registered and not currently be in use by any wdps.  After unregistration
162  * new datapaths of that type cannot be opened using wdp_open(). */
163 int
164 wdp_unregister_provider(const char *type)
165 {
166     struct shash_node *node;
167     struct registered_wdp_class *registered_class;
168
169     node = shash_find(&wdp_classes, type);
170     if (!node) {
171         VLOG_WARN("attempted to unregister a datapath provider that is not "
172                   "registered: %s", type);
173         return EAFNOSUPPORT;
174     }
175
176     registered_class = node->data;
177     if (registered_class->refcount) {
178         VLOG_WARN("attempted to unregister in use datapath provider: %s", type);
179         return EBUSY;
180     }
181
182     shash_delete(&wdp_classes, node);
183     free(registered_class);
184
185     return 0;
186 }
187
188 /* Clears 'types' and enumerates the types of all currently registered datapath
189  * providers into it.  The caller must first initialize the svec. */
190 void
191 wdp_enumerate_types(struct svec *types)
192 {
193     struct shash_node *node;
194
195     wdp_initialize();
196     svec_clear(types);
197
198     SHASH_FOR_EACH(node, &wdp_classes) {
199         const struct registered_wdp_class *registered_class = node->data;
200         svec_add(types, registered_class->wdp_class->type);
201     }
202 }
203
204 /* Clears 'names' and enumerates the names of all known created datapaths with
205  * the given 'type'.  The caller must first initialize the svec. Returns 0 if
206  * successful, otherwise a positive errno value.
207  *
208  * Some kinds of datapaths might not be practically enumerable.  This is not
209  * considered an error. */
210 int
211 wdp_enumerate_names(const char *type, struct svec *names)
212 {
213     const struct registered_wdp_class *registered_class;
214     const struct wdp_class *wdp_class;
215     int error;
216
217     wdp_initialize();
218     svec_clear(names);
219
220     registered_class = shash_find_data(&wdp_classes, type);
221     if (!registered_class) {
222         VLOG_WARN("could not enumerate unknown type: %s", type);
223         return EAFNOSUPPORT;
224     }
225
226     wdp_class = registered_class->wdp_class;
227     error = (wdp_class->enumerate
228              ? wdp_class->enumerate(wdp_class, names)
229              : 0);
230
231     if (error) {
232         VLOG_WARN("failed to enumerate %s datapaths: %s", wdp_class->type,
233                   strerror(error));
234     }
235
236     return error;
237 }
238
239 /* Parses 'datapath name', which is of the form type@name into its
240  * component pieces.  'name' and 'type' must be freed by the caller. */
241 void
242 wdp_parse_name(const char *datapath_name_, char **name, char **type)
243 {
244     char *datapath_name = xstrdup(datapath_name_);
245     char *separator;
246
247     separator = strchr(datapath_name, '@');
248     if (separator) {
249         *separator = '\0';
250         *type = datapath_name;
251         *name = xstrdup(separator + 1);
252     } else {
253         *name = datapath_name;
254         *type = NULL;
255     }
256 }
257
258 static int
259 do_open(const char *name, const char *type, bool create, struct wdp **wdpp)
260 {
261     struct wdp *wdp = NULL;
262     int error;
263     struct registered_wdp_class *registered_class;
264
265     wdp_initialize();
266
267     if (!type || *type == '\0') {
268         type = "system";
269     }
270
271     registered_class = shash_find_data(&wdp_classes, type);
272     if (!registered_class) {
273         VLOG_WARN("could not create datapath %s of unknown type %s", name,
274                   type);
275         error = EAFNOSUPPORT;
276         goto exit;
277     }
278
279     error = registered_class->wdp_class->open(registered_class->wdp_class,
280                                               name, create, &wdp);
281     if (!error) {
282         registered_class->refcount++;
283     }
284
285 exit:
286     *wdpp = error ? NULL : wdp;
287     return error;
288 }
289
290 /* Tries to open an existing datapath named 'name' and type 'type'.  Will fail
291  * if no datapath with 'name' and 'type' exists.  'type' may be either NULL or
292  * the empty string to specify the default system type.  Returns 0 if
293  * successful, otherwise a positive errno value.  On success stores a pointer
294  * to the datapath in '*wdpp', otherwise a null pointer. */
295 int
296 wdp_open(const char *name, const char *type, struct wdp **wdpp)
297 {
298     return do_open(name, type, false, wdpp);
299 }
300
301 /* Tries to create and open a new datapath with the given 'name' and 'type'.
302  * 'type' may be either NULL or the empty string to specify the default system
303  * type.  Will fail if a datapath with 'name' and 'type' already exists.
304  * Returns 0 if successful, otherwise a positive errno value.  On success
305  * stores a pointer to the datapath in '*wdpp', otherwise a null pointer. */
306 int
307 wdp_create(const char *name, const char *type, struct wdp **wdpp)
308 {
309     return do_open(name, type, true, wdpp);
310 }
311
312 /* Tries to open a datapath with the given 'name' and 'type', creating it if it
313  * does not exist.  'type' may be either NULL or the empty string to specify
314  * the default system type.  Returns 0 if successful, otherwise a positive
315  * errno value. On success stores a pointer to the datapath in '*wdpp',
316  * otherwise a null pointer. */
317 int
318 wdp_create_and_open(const char *name, const char *type, struct wdp **wdpp)
319 {
320     int error;
321
322     error = wdp_create(name, type, wdpp);
323     if (error == EEXIST || error == EBUSY) {
324         error = wdp_open(name, type, wdpp);
325         if (error) {
326             VLOG_WARN("datapath %s already exists but cannot be opened: %s",
327                       name, strerror(error));
328         }
329     } else if (error) {
330         VLOG_WARN("failed to create datapath %s: %s", name, strerror(error));
331     }
332     return error;
333 }
334
335 /* Closes and frees the connection to 'wdp'.  Does not destroy the wdp
336  * itself; call wdp_delete() first, instead, if that is desirable. */
337 void
338 wdp_close(struct wdp *wdp)
339 {
340     if (wdp) {
341         struct registered_wdp_class *registered_class;
342
343         registered_class = shash_find_data(&wdp_classes, 
344                                            wdp->wdp_class->type);
345         assert(registered_class);
346         assert(registered_class->refcount);
347
348         registered_class->refcount--;
349         wdp_uninit(wdp, true);
350     }
351 }
352
353 /* Returns the name of datapath 'wdp' prefixed with the type
354  * (for use in log messages). */
355 const char *
356 wdp_name(const struct wdp *wdp)
357 {
358     return wdp->full_name;
359 }
360
361 /* Returns the name of datapath 'wdp' without the type
362  * (for use in device names). */
363 const char *
364 wdp_base_name(const struct wdp *wdp)
365 {
366     return wdp->base_name;
367 }
368
369 /* Enumerates all names that may be used to open 'wdp' into 'all_names'.  The
370  * Linux datapath, for example, supports opening a datapath both by number,
371  * e.g. "wdp0", and by the name of the datapath's local port.  For some
372  * datapaths, this might be an infinite set (e.g. in a file name, slashes may
373  * be duplicated any number of times), in which case only the names most likely
374  * to be used will be enumerated.
375  *
376  * The caller must already have initialized 'all_names'.  Any existing names in
377  * 'all_names' will not be disturbed. */
378 int
379 wdp_get_all_names(const struct wdp *wdp, struct svec *all_names)
380 {
381     if (wdp->wdp_class->get_all_names) {
382         int error = wdp->wdp_class->get_all_names(wdp, all_names);
383         if (error) {
384             VLOG_WARN_RL(&error_rl,
385                          "failed to retrieve names for datpath %s: %s",
386                          wdp_name(wdp), strerror(error));
387         }
388         return error;
389     } else {
390         svec_add(all_names, wdp_base_name(wdp));
391         return 0;
392     }
393 }
394
395 /* Destroys the datapath that 'wdp' is connected to, first removing all of
396  * its ports.  After calling this function, it does not make sense to pass
397  * 'wdp' to any functions other than wdp_name() or wdp_close(). */
398 int
399 wdp_delete(struct wdp *wdp)
400 {
401     int error;
402
403     COVERAGE_INC(wdp_destroy);
404
405     error = wdp->wdp_class->destroy(wdp);
406     log_operation(wdp, "delete", error);
407     return error;
408 }
409
410 int
411 wdp_get_features(const struct wdp *wdp, struct ofpbuf **featuresp)
412 {
413     int error = wdp->wdp_class->get_features(wdp, featuresp);
414     if (error) {
415         *featuresp = NULL;
416     }
417     return error;
418 }
419
420 /* Retrieves statistics for 'wdp' into 'stats'.  Returns 0 if successful,
421  * otherwise a positive errno value. */
422 int
423 wdp_get_wdp_stats(const struct wdp *wdp, struct wdp_stats *stats)
424 {
425     int error = wdp->wdp_class->get_stats(wdp, stats);
426     if (error) {
427         memset(stats, 0, sizeof *stats);
428     }
429     log_operation(wdp, "get_stats", error);
430     return error;
431 }
432
433 /* Retrieves the current IP fragment handling policy for 'wdp' into
434  * '*drop_frags': true indicates that fragments are dropped, false indicates
435  * that fragments are treated in the same way as other IP packets (except that
436  * the L4 header cannot be read).  Returns 0 if successful, otherwise a
437  * positive errno value. */
438 int
439 wdp_get_drop_frags(const struct wdp *wdp, bool *drop_frags)
440 {
441     int error = wdp->wdp_class->get_drop_frags(wdp, drop_frags);
442     if (error) {
443         *drop_frags = false;
444     }
445     log_operation(wdp, "get_drop_frags", error);
446     return error;
447 }
448
449 /* Changes 'wdp''s treatment of IP fragments to 'drop_frags', whose meaning is
450  * the same as for the get_drop_frags member function.  Returns 0 if
451  * successful, otherwise a positive errno value. */
452 int
453 wdp_set_drop_frags(struct wdp *wdp, bool drop_frags)
454 {
455     int error = wdp->wdp_class->set_drop_frags(wdp, drop_frags);
456     log_operation(wdp, "set_drop_frags", error);
457     return error;
458 }
459
460 /* Attempts to add 'devname' as a port on 'wdp'.  If 'internal' is true,
461  * creates the port as an internal port.  If successful, returns 0 and sets
462  * '*port_nop' to the new port's port number (if 'port_nop' is non-null).  On
463  * failure, returns a positive errno value and sets '*port_nop' to UINT16_MAX
464  * (if 'port_nop' is non-null). */
465 int
466 wdp_port_add(struct wdp *wdp, const char *devname,
467              bool internal, uint16_t *port_nop)
468 {
469     uint16_t port_no;
470     int error;
471
472     COVERAGE_INC(wdp_port_add);
473
474     error = wdp->wdp_class->port_add(wdp, devname, internal, &port_no);
475     if (!error) {
476         VLOG_DBG_RL(&wdpmsg_rl, "%s: added %s as port %"PRIu16,
477                     wdp_name(wdp), devname, port_no);
478     } else {
479         VLOG_WARN_RL(&error_rl, "%s: failed to add %s as port: %s",
480                      wdp_name(wdp), devname, strerror(error));
481         port_no = UINT16_MAX;
482     }
483     if (port_nop) {
484         *port_nop = port_no;
485     }
486     return error;
487 }
488
489 /* Attempts to remove 'wdp''s port number 'port_no'.  Returns 0 if successful,
490  * otherwise a positive errno value. */
491 int
492 wdp_port_del(struct wdp *wdp, uint16_t port_no)
493 {
494     int error;
495
496     COVERAGE_INC(wdp_port_del);
497
498     error = wdp->wdp_class->port_del(wdp, port_no);
499     log_operation(wdp, "port_del", error);
500     return error;
501 }
502
503 /* Looks up port number 'port_no' in 'wdp'.  On success, returns 0 and points
504  * '*portp' to a wdp_port representing the specified port.  On failure, returns
505  * a positive errno value and sets '*portp' to NULL.
506  *
507  * The caller must not modify or free the returned wdp_port.  Calling
508  * wdp_run() or wdp_port_poll() may free the returned wdp_port. */
509 int
510 wdp_port_query_by_number(const struct wdp *wdp, uint16_t port_no,
511                          struct wdp_port **portp)
512 {
513     int error;
514
515     error = wdp->wdp_class->port_query_by_number(wdp, port_no, portp);
516     if (!error) {
517         VLOG_DBG_RL(&wdpmsg_rl, "%s: port %"PRIu16" is device %s",
518                     wdp_name(wdp), port_no, (*portp)->devname);
519     } else {
520         *portp = NULL;
521         VLOG_WARN_RL(&error_rl, "%s: failed to query port %"PRIu16": %s",
522                      wdp_name(wdp), port_no, strerror(error));
523     }
524     return error;
525 }
526
527 /* Same as wdp_port_query_by_number() except that it look for a port named
528  * 'devname' in 'wdp'. */
529 int
530 wdp_port_query_by_name(const struct wdp *wdp, const char *devname,
531                        struct wdp_port **portp)
532 {
533     int error = wdp->wdp_class->port_query_by_name(wdp, devname, portp);
534     if (!error) {
535         VLOG_DBG_RL(&wdpmsg_rl, "%s: device %s is on port %"PRIu16,
536                     wdp_name(wdp), devname, (*portp)->opp.port_no);
537     } else {
538         *portp = NULL;
539
540         /* Log level is DBG here because all the current callers are interested
541          * in whether 'wdp' actually has a port 'devname', so that it's not
542          * an issue worth logging if it doesn't. */
543         VLOG_DBG_RL(&error_rl, "%s: failed to query port %s: %s",
544                     wdp_name(wdp), devname, strerror(error));
545     }
546     return error;
547 }
548
549 /* Looks up port number 'port_no' in 'wdp'.  On success, returns 0 and stores
550  * a copy of the port's name in '*namep'.  On failure, returns a positive errno
551  * value and stores NULL in '*namep'.
552  *
553  * The caller is responsible for freeing '*namep' (with free()). */
554 int
555 wdp_port_get_name(struct wdp *wdp, uint16_t port_no, char **namep)
556 {
557     struct wdp_port *port;
558     int error;
559
560     error = wdp_port_query_by_number(wdp, port_no, &port);
561     *namep = !error ? xstrdup(port->devname) : NULL;
562     return error;
563 }
564
565 /* Obtains a list of all the ports in 'wdp'.
566  *
567  * If successful, returns 0 and sets '*portsp' to point to an array of
568  * pointers to port structures and '*n_portsp' to the number of pointers in the
569  * array.  On failure, returns a positive errno value and sets '*portsp' to
570  * NULL and '*n_portsp' to 0.
571  *
572  * The caller is responsible for freeing '*portsp' by calling free().  The
573  * caller must not free the individual wdp_port structures.  Calling
574  * wdp_run() or wdp_port_poll() may free the returned wdp_ports. */
575 int
576 wdp_port_list(const struct wdp *wdp,
577               struct wdp_port ***portsp, size_t *n_portsp)
578 {
579     int error;
580
581     error = wdp->wdp_class->port_list(wdp, portsp, n_portsp);
582     if (error) {
583         *portsp = NULL;
584         *n_portsp = 0;
585     }
586     log_operation(wdp, "port_list", error);
587     return error;
588 }
589
590 int
591 wdp_port_set_config(struct wdp *wdp, uint16_t port_no, uint32_t config)
592 {
593     return wdp->wdp_class->port_set_config(wdp, port_no, config);
594 }
595
596 /* Polls for changes in the set of ports in 'wdp'.  If the set of ports in
597  * 'wdp' has changed, this function does one of the following:
598  *
599  * - Stores the name of the device that was added to or deleted from 'wdp' in
600  *   '*devnamep' and returns 0.  The caller is responsible for freeing
601  *   '*devnamep' (with free()) when it no longer needs it.
602  *
603  * - Returns ENOBUFS and sets '*devnamep' to NULL.
604  *
605  * This function may also return 'false positives', where it returns 0 and
606  * '*devnamep' names a device that was not actually added or deleted or it
607  * returns ENOBUFS without any change.
608  *
609  * Returns EAGAIN if the set of ports in 'wdp' has not changed.  May also
610  * return other positive errno values to indicate that something has gone
611  * wrong. */
612 int
613 wdp_port_poll(const struct wdp *wdp, char **devnamep)
614 {
615     int error = wdp->wdp_class->port_poll(wdp, devnamep);
616     if (error) {
617         *devnamep = NULL;
618     }
619     return error;
620 }
621
622 /* Arranges for the poll loop to wake up when port_poll(wdp) will return a
623  * value other than EAGAIN. */
624 void
625 wdp_port_poll_wait(const struct wdp *wdp)
626 {
627     wdp->wdp_class->port_poll_wait(wdp);
628 }
629
630 /* Deletes all flows from 'wdp'.  Returns 0 if successful, otherwise a
631  * positive errno value.  */
632 int
633 wdp_flow_flush(struct wdp *wdp)
634 {
635     int error;
636
637     COVERAGE_INC(wdp_flow_flush);
638
639     error = wdp->wdp_class->flow_flush(wdp);
640     log_operation(wdp, "flow_flush", error);
641     return error;
642 }
643
644 struct wdp_rule *
645 wdp_flow_get(struct wdp *wdp, const flow_t *flow)
646 {
647     return wdp->wdp_class->flow_get(wdp, flow);
648 }
649
650 struct wdp_rule *
651 wdp_flow_match(struct wdp *wdp, const flow_t *flow)
652 {
653     return wdp->wdp_class->flow_match(wdp, flow);
654 }
655
656 void
657 wdp_flow_for_each_match(const struct wdp *wdp, const flow_t *target,
658                         int include, wdp_flow_cb_func *callback, void *aux)
659 {
660     wdp->wdp_class->flow_for_each_match(wdp, target, include,
661                                         callback, aux); 
662 }
663
664 int
665 wdp_flow_get_stats(const struct wdp *wdp, const struct wdp_rule *rule,
666                    struct wdp_flow_stats *stats)
667 {
668     int error = wdp->wdp_class->flow_get_stats(wdp, rule, stats);
669     if (error) {
670         memset(stats, 0, sizeof *stats);
671     }
672     return error;
673 }
674
675 bool
676 wdp_flow_overlaps(const struct wdp *wdp, const flow_t *flow)
677 {
678     return wdp->wdp_class->flow_overlaps(wdp, flow);
679 }
680
681 int
682 wdp_flow_put(struct wdp *wdp, struct wdp_flow_put *put,
683              struct wdp_flow_stats *old_stats, struct wdp_rule **rulep)
684 {
685     int error = wdp->wdp_class->flow_put(wdp, put, old_stats, rulep);
686     if (error) {
687         if (old_stats) {
688             memset(old_stats, 0, sizeof *old_stats);
689         }
690         if (rulep) {
691             *rulep = NULL;
692         }
693     }
694     return error;
695 }
696
697 int
698 wdp_flow_delete(struct wdp *wdp, struct wdp_rule *rule,
699                 struct wdp_flow_stats *final_stats)
700 {
701     int error = wdp->wdp_class->flow_delete(wdp, rule, final_stats);
702     if (error && final_stats) {
703         memset(final_stats, 0, sizeof *final_stats);
704     }
705     return error;
706 }
707
708 int
709 wdp_flow_inject(struct wdp *wdp, struct wdp_rule *rule,
710                 uint16_t in_port, const struct ofpbuf *packet)
711 {
712     return wdp->wdp_class->flow_inject(wdp, rule, in_port, packet);
713 }
714
715 int
716 wdp_execute(struct wdp *wdp, uint16_t in_port,
717             const union ofp_action actions[], size_t n_actions,
718             const struct ofpbuf *buf)
719 {
720     int error;
721
722     COVERAGE_INC(wdp_execute);
723     if (n_actions > 0) {
724         error = wdp->wdp_class->execute(wdp, in_port, actions,
725                                         n_actions, buf);
726     } else {
727         error = 0;
728     }
729     return error;
730 }
731
732 /* Retrieves 'wdp''s "listen mask" into '*listen_mask'.  Each bit set in
733  * '*listen_mask' indicates that wdp_recv() will receive messages of the
734  * corresponding WDP_CHAN_* type.  Returns 0 if successful, otherwise a
735  * positive errno value. */
736 int
737 wdp_recv_get_mask(const struct wdp *wdp, int *listen_mask)
738 {
739     int error = wdp->wdp_class->recv_get_mask(wdp, listen_mask);
740     if (error) {
741         *listen_mask = 0;
742     }
743     log_operation(wdp, "recv_get_mask", error);
744     return error;
745 }
746
747 /* Sets 'wdp''s "listen mask" to 'listen_mask'.  Each bit set in
748  * '*listen_mask' requests that wdp_recv() receive messages of the
749  * corresponding WDP_CHAN_* type.  Returns 0 if successful, otherwise a
750  * positive errno value. */
751 int
752 wdp_recv_set_mask(struct wdp *wdp, int listen_mask)
753 {
754     int error = wdp->wdp_class->recv_set_mask(wdp, listen_mask);
755     log_operation(wdp, "recv_set_mask", error);
756     return error;
757 }
758
759 /* Retrieve the sFlow sampling probability.  '*probability' is expressed as the
760  * number of packets out of UINT_MAX to sample, e.g. probability/UINT_MAX is
761  * the probability of sampling a given packet.
762  *
763  * Returns 0 if successful, otherwise a positive errno value.  EOPNOTSUPP
764  * indicates that 'wdp' does not support sFlow sampling. */
765 int
766 wdp_get_sflow_probability(const struct wdp *wdp, uint32_t *probability)
767 {
768     int error = (wdp->wdp_class->get_sflow_probability
769                  ? wdp->wdp_class->get_sflow_probability(wdp, probability)
770                  : EOPNOTSUPP);
771     if (error) {
772         *probability = 0;
773     }
774     log_operation(wdp, "get_sflow_probability", error);
775     return error;
776 }
777
778 /* Set the sFlow sampling probability.  'probability' is expressed as the
779  * number of packets out of UINT_MAX to sample, e.g. probability/UINT_MAX is
780  * the probability of sampling a given packet.
781  *
782  * Returns 0 if successful, otherwise a positive errno value.  EOPNOTSUPP
783  * indicates that 'wdp' does not support sFlow sampling. */
784 int
785 wdp_set_sflow_probability(struct wdp *wdp, uint32_t probability)
786 {
787     int error = (wdp->wdp_class->set_sflow_probability
788                  ? wdp->wdp_class->set_sflow_probability(wdp, probability)
789                  : EOPNOTSUPP);
790     log_operation(wdp, "set_sflow_probability", error);
791     return error;
792 }
793
794 /* Attempts to receive a message from 'wdp'.  If successful, stores the
795  * message into '*packetp'.  Only messages of the types selected with
796  * wdp_set_listen_mask() will ordinarily be received (but if a message type
797  * is enabled and then later disabled, some stragglers might pop up).
798  *
799  * Returns 0 if successful, otherwise a positive errno value.  Returns EAGAIN
800  * if no message is immediately available. */
801 int
802 wdp_recv(struct wdp *wdp, struct wdp_packet *packet)
803 {
804     int error = wdp->wdp_class->recv(wdp, packet);
805     if (!error) {
806         /* XXX vlog_dbg received packet */
807     } else {
808         memset(packet, 0, sizeof *packet);
809         packet->channel = -1;
810     }
811     return error;
812 }
813
814 /* Discards all messages that would otherwise be received by wdp_recv() on
815  * 'wdp'.  Returns 0 if successful, otherwise a positive errno value. */
816 int
817 wdp_recv_purge(struct wdp *wdp)
818 {
819     struct wdp_stats stats;
820     unsigned int i;
821     int error;
822
823     COVERAGE_INC(wdp_purge);
824
825     error = wdp_get_wdp_stats(wdp, &stats);
826     if (error) {
827         return error;
828     }
829
830     for (i = 0; i < stats.max_miss_queue + stats.max_action_queue + stats.max_sflow_queue; i++) {
831         struct wdp_packet packet;
832
833         error = wdp_recv(wdp, &packet);
834         if (error) {
835             return error == EAGAIN ? 0 : error;
836         }
837         ofpbuf_delete(packet.payload);
838     }
839     return 0;
840 }
841
842 /* Arranges for the poll loop to wake up when 'wdp' has a message queued to be
843  * received with wdp_recv(). */
844 void
845 wdp_recv_wait(struct wdp *wdp)
846 {
847     wdp->wdp_class->recv_wait(wdp);
848 }
849
850 /* Obtains the NetFlow engine type and engine ID for 'wdp' into '*engine_type'
851  * and '*engine_id', respectively. */
852 void
853 wdp_get_netflow_ids(const struct wdp *wdp,
854                     uint8_t *engine_type, uint8_t *engine_id)
855 {
856     *engine_type = wdp->netflow_engine_type;
857     *engine_id = wdp->netflow_engine_id;
858 }
859 \f
860 void
861 wdp_packet_destroy(struct wdp_packet *packet)
862 {
863     if (packet) {
864         ofpbuf_delete(packet->payload);
865         free(packet);
866     }
867 }
868
869 void
870 wdp_init(struct wdp *wdp, const struct wdp_class *wdp_class,
871          const char *name,
872          uint8_t netflow_engine_type, uint8_t netflow_engine_id)
873 {
874     wdp->wdp_class = wdp_class;
875     wdp->base_name = xstrdup(name);
876     wdp->full_name = xasprintf("%s@%s", wdp_class->type, name);
877     wdp->netflow_engine_type = netflow_engine_type;
878     wdp->netflow_engine_id = netflow_engine_id;
879 }
880
881 /* Undoes the results of initialization.
882  *
883  * Normally this function only needs to be called from wdp_close().
884  * However, it may be called by providers due to an error on opening
885  * that occurs after initialization.  It this case wdp_close() would
886  * never be called. */
887 void
888 wdp_uninit(struct wdp *wdp, bool close)
889 {
890     char *base_name = wdp->base_name;
891     char *full_name = wdp->full_name;
892
893     if (close) {
894         wdp->wdp_class->close(wdp);
895     }
896
897     free(base_name);
898     free(full_name);
899 }
900 \f
901 static void
902 log_operation(const struct wdp *wdp, const char *operation, int error)
903 {
904     if (!error) {
905         VLOG_DBG_RL(&wdpmsg_rl, "%s: %s success", wdp_name(wdp), operation);
906     } else {
907         VLOG_WARN_RL(&error_rl, "%s: %s failed (%s)",
908                      wdp_name(wdp), operation, strerror(error));
909     }
910 }