vswitchd: Use wdp functions instead of xfif ones for enumerating bridges.
[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 "netdev.h"
31 #include "netlink.h"
32 #include "ofp-print.h"
33 #include "ofpbuf.h"
34 #include "packets.h"
35 #include "poll-loop.h"
36 #include "shash.h"
37 #include "svec.h"
38 #include "timeval.h"
39 #include "util.h"
40 #include "valgrind.h"
41 #include "vlog.h"
42 #include "wdp-xflow.h"
43
44 VLOG_DEFINE_THIS_MODULE(wdp)
45 \f
46 /* wdp_rule */
47
48 /* Initializes a new 'struct wdp_rule', copying in the 'n_actions' elements of
49  * 'actions'.
50  *
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. */
53 void
54 wdp_rule_init(struct wdp_rule *rule, const union ofp_action *actions,
55               size_t n_actions)
56 {
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;
64 }
65
66 /* Frees the data in 'rule'. */
67 void
68 wdp_rule_uninit(struct wdp_rule *rule)
69 {
70     free(rule->actions);
71 }
72 \f
73 /* wdp */
74
75 static const struct wdp_class *base_wdp_classes[] = {
76     /* XXX none yet */
77 };
78
79 struct registered_wdp_class {
80     const struct wdp_class *wdp_class;
81     int refcount;
82 };
83
84 static struct shash wdp_classes = SHASH_INITIALIZER(&wdp_classes);
85
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);
90
91 /* Not really much point in logging many wdp errors. */
92 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
93
94 static void log_operation(const struct wdp *, const char *operation,
95                           int error);
96
97 static void
98 wdp_initialize(void)
99 {
100     static int status = -1;
101
102     if (status < 0) {
103         int i;
104
105         status = 0;
106         for (i = 0; i < ARRAY_SIZE(base_wdp_classes); i++) {
107             wdp_register_provider(base_wdp_classes[i]);
108         }
109         wdp_xflow_register();
110     }
111 }
112
113 /* Performs periodic work needed by all the various kinds of wdps.
114  *
115  * If your program opens any wdps, it must call both this function and
116  * netdev_run() within its main poll loop. */
117 void
118 wdp_run(void)
119 {
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();
125         }
126     }
127 }
128
129 /* Arranges for poll_block() to wake up when wdp_run() needs to be called.
130  *
131  * If your program opens any wdps, it must call both this function and
132  * netdev_wait() within its main poll loop. */
133 void
134 wdp_wait(void)
135 {
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();
141         }
142     }
143 }
144
145 /* Registers a new datapath provider.  After successful registration, new
146  * datapaths of that type can be opened using wdp_open(). */
147 int
148 wdp_register_provider(const struct wdp_class *new_class)
149 {
150     struct registered_wdp_class *registered_class;
151
152     if (shash_find(&wdp_classes, new_class->type)) {
153         VLOG_WARN("attempted to register duplicate datapath provider: %s",
154                   new_class->type);
155         return EEXIST;
156     }
157
158     registered_class = xmalloc(sizeof *registered_class);
159     registered_class->wdp_class = new_class;
160     registered_class->refcount = 0;
161
162     shash_add(&wdp_classes, new_class->type, registered_class);
163
164     return 0;
165 }
166
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(). */
170 int
171 wdp_unregister_provider(const char *type)
172 {
173     struct shash_node *node;
174     struct registered_wdp_class *registered_class;
175
176     node = shash_find(&wdp_classes, type);
177     if (!node) {
178         VLOG_WARN("attempted to unregister a datapath provider that is not "
179                   "registered: %s", type);
180         return EAFNOSUPPORT;
181     }
182
183     registered_class = node->data;
184     if (registered_class->refcount) {
185         VLOG_WARN("attempted to unregister in use datapath provider: %s",
186                   type);
187         return EBUSY;
188     }
189
190     shash_delete(&wdp_classes, node);
191     free(registered_class);
192
193     return 0;
194 }
195
196 /* Clears 'types' and enumerates the types of all currently registered wdp
197  * providers into it.  The caller must first initialize the svec. */
198 void
199 wdp_enumerate_types(struct svec *types)
200 {
201     struct shash_node *node;
202
203     wdp_initialize();
204     svec_clear(types);
205
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);
209     }
210 }
211
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.
215  *
216  * Some kinds of datapaths might not be practically enumerable.  This is not
217  * considered an error. */
218 int
219 wdp_enumerate_names(const char *type, struct svec *names)
220 {
221     const struct registered_wdp_class *registered_class;
222     const struct wdp_class *wdp_class;
223     int error;
224
225     wdp_initialize();
226     svec_clear(names);
227
228     registered_class = shash_find_data(&wdp_classes, type);
229     if (!registered_class) {
230         VLOG_WARN("could not enumerate unknown type: %s", type);
231         return EAFNOSUPPORT;
232     }
233
234     wdp_class = registered_class->wdp_class;
235     error = (wdp_class->enumerate
236              ? wdp_class->enumerate(wdp_class, names)
237              : 0);
238
239     if (error) {
240         VLOG_WARN("failed to enumerate %s datapaths: %s", wdp_class->type,
241                   strerror(error));
242     }
243
244     return error;
245 }
246
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. */
249 void
250 wdp_parse_name(const char *datapath_name_, char **name, char **type)
251 {
252     char *datapath_name = xstrdup(datapath_name_);
253     char *separator;
254
255     separator = strchr(datapath_name, '@');
256     if (separator) {
257         *separator = '\0';
258         *type = datapath_name;
259         *name = xstrdup(separator + 1);
260     } else {
261         *name = datapath_name;
262         *type = NULL;
263     }
264 }
265
266 static int
267 do_open(const char *name, const char *type, bool create, struct wdp **wdpp)
268 {
269     struct wdp *wdp = NULL;
270     int error;
271     struct registered_wdp_class *registered_class;
272
273     wdp_initialize();
274
275     if (!type || *type == '\0') {
276         type = "system";
277     }
278
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,
282                   type);
283         error = EAFNOSUPPORT;
284         goto exit;
285     }
286
287     error = registered_class->wdp_class->open(registered_class->wdp_class,
288                                               name, create, &wdp);
289     if (!error) {
290         registered_class->refcount++;
291     }
292
293 exit:
294     *wdpp = error ? NULL : wdp;
295     return error;
296 }
297
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. */
303 int
304 wdp_open(const char *name, const char *type, struct wdp **wdpp)
305 {
306     return do_open(name, type, false, wdpp);
307 }
308
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. */
314 int
315 wdp_create(const char *name, const char *type, struct wdp **wdpp)
316 {
317     return do_open(name, type, true, wdpp);
318 }
319
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. */
325 int
326 wdp_create_and_open(const char *name, const char *type, struct wdp **wdpp)
327 {
328     int error;
329
330     error = wdp_create(name, type, wdpp);
331     if (error == EEXIST || error == EBUSY) {
332         error = wdp_open(name, type, wdpp);
333         if (error) {
334             VLOG_WARN("datapath %s already exists but cannot be opened: %s",
335                       name, strerror(error));
336         }
337     } else if (error) {
338         VLOG_WARN("failed to create datapath %s: %s", name, strerror(error));
339     }
340     return error;
341 }
342
343 /* Closes and frees the connection to 'wdp'.  Does not destroy the wdp
344  * itself; call wdp_delete() first, instead, if that is desirable. */
345 void
346 wdp_close(struct wdp *wdp)
347 {
348     if (wdp) {
349         struct registered_wdp_class *registered_class;
350
351         registered_class = shash_find_data(&wdp_classes, 
352                                            wdp->wdp_class->type);
353         assert(registered_class);
354         assert(registered_class->refcount);
355
356         registered_class->refcount--;
357         wdp_uninit(wdp, true);
358     }
359 }
360
361 /* Returns the name of datapath 'wdp' prefixed with the type
362  * (for use in log messages). */
363 const char *
364 wdp_name(const struct wdp *wdp)
365 {
366     return wdp->full_name;
367 }
368
369 /* Returns the name of datapath 'wdp' without the type
370  * (for use in device names). */
371 const char *
372 wdp_base_name(const struct wdp *wdp)
373 {
374     return wdp->base_name;
375 }
376
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.
383  *
384  * The caller must already have initialized 'all_names'.  Any existing names in
385  * 'all_names' will not be disturbed. */
386 int
387 wdp_get_all_names(const struct wdp *wdp, struct svec *all_names)
388 {
389     if (wdp->wdp_class->get_all_names) {
390         int error = wdp->wdp_class->get_all_names(wdp, all_names);
391         if (error) {
392             VLOG_WARN_RL(&error_rl,
393                          "failed to retrieve names for datpath %s: %s",
394                          wdp_name(wdp), strerror(error));
395         }
396         return error;
397     } else {
398         svec_add(all_names, wdp_base_name(wdp));
399         return 0;
400     }
401 }
402
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(). */
406 int
407 wdp_delete(struct wdp *wdp)
408 {
409     int error;
410
411     COVERAGE_INC(wdp_destroy);
412
413     error = wdp->wdp_class->destroy(wdp);
414     log_operation(wdp, "delete", error);
415     return error;
416 }
417
418 /* Obtains the set of features supported by 'wdp'.
419  *
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.
426  *
427  * On error, returns an OpenFlow error code (as constructed by ofp_mkerr()) and
428  * sets '*featuresp' to NULL. */
429 int
430 wdp_get_features(const struct wdp *wdp, struct ofpbuf **featuresp)
431 {
432     int error = wdp->wdp_class->get_features(wdp, featuresp);
433     if (error) {
434         *featuresp = NULL;
435     }
436     return error;
437 }
438
439 /* Retrieves statistics for 'wdp' into 'stats'.  Returns 0 if successful,
440  * otherwise a positive errno value.  On error, clears 'stats' to
441  * all-bits-zero. */
442 int
443 wdp_get_wdp_stats(const struct wdp *wdp, struct wdp_stats *stats)
444 {
445     int error = wdp->wdp_class->get_stats(wdp, stats);
446     if (error) {
447         memset(stats, 0, sizeof *stats);
448     }
449     log_operation(wdp, "get_stats", error);
450     return error;
451 }
452
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(). */
456 int
457 wdp_get_table_stats(const struct wdp *wdp, struct ofpbuf *stats)
458 {
459     int error = wdp->wdp_class->get_table_stats(wdp, stats);
460     if (!error) {
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);
464     }
465     log_operation(wdp, "get_table_stats", error);
466     return error;
467 }
468
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. */
474 int
475 wdp_get_drop_frags(const struct wdp *wdp, bool *drop_frags)
476 {
477     int error = wdp->wdp_class->get_drop_frags(wdp, drop_frags);
478     if (error) {
479         *drop_frags = false;
480     }
481     log_operation(wdp, "get_drop_frags", error);
482     return error;
483 }
484
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. */
489 int
490 wdp_set_drop_frags(struct wdp *wdp, bool drop_frags)
491 {
492     int error;
493     error = (wdp->wdp_class->set_drop_frags
494              ? wdp->wdp_class->set_drop_frags(wdp, drop_frags)
495              : EOPNOTSUPP);
496     log_operation(wdp, "set_drop_frags", error);
497     return error;
498 }
499
500 /* Clears the contents of 'port'. */
501 void
502 wdp_port_clear(struct wdp_port *port)
503 {
504     memset(port, 0, sizeof *port);
505 }
506
507 /* Makes a deep copy of 'old' in 'port'.  The caller may free 'port''s data
508  * with wdp_port_free(). */
509 void
510 wdp_port_copy(struct wdp_port *port, const struct wdp_port *old)
511 {
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;
516 }
517
518 /* Frees the data that 'port' points to (but not 'port' itself). */
519 void
520 wdp_port_free(struct wdp_port *port)
521 {
522     if (port) {
523         netdev_close(port->netdev);
524         free(port->devname);
525     }
526 }
527
528 /* Frees the data that each of the 'n' ports in 'ports' points to, and then
529  * frees 'ports' itself. */
530 void
531 wdp_port_array_free(struct wdp_port *ports, size_t n)
532 {
533     size_t i;
534
535     for (i = 0; i < n; i++) {
536         wdp_port_free(&ports[i]);
537     }
538     free(ports);
539 }
540
541 /* Attempts to add 'devname' as a port on 'wdp':
542  *
543  *   - If 'internal' is true, attempts to create a new internal port (a virtual
544  *     port implemented in software) by that name.
545  *
546  *   - If 'internal' is false, 'devname' must name an existing network device.
547  *
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).
551  *
552  * Some wildcarded datapaths might have fixed sets of ports.  For these
553  * datapaths this function will always fail.
554  *
555  * Possible error return values include:
556  *
557  *   - ENODEV: No device named 'devname' exists (if 'internal' is false).
558  *
559  *   - EEXIST: A device named 'devname' already exists (if 'internal' is true).
560  *
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)
564  *
565  *   - EFBIG: The datapath already has as many ports as it can support.
566  *
567  *   - EOPNOTSUPP: 'wdp' has a fixed set of ports.
568  */
569 int
570 wdp_port_add(struct wdp *wdp, const char *devname,
571              bool internal, uint16_t *port_nop)
572 {
573     uint16_t port_no;
574     int error;
575
576     COVERAGE_INC(wdp_port_add);
577
578     error = (wdp->wdp_class->port_add
579              ? wdp->wdp_class->port_add(wdp, devname, internal, &port_no)
580              : EOPNOTSUPP);
581     if (!error) {
582         VLOG_DBG_RL(&wdpmsg_rl, "%s: added %s as port %"PRIu16,
583                     wdp_name(wdp), devname, port_no);
584     } else {
585         VLOG_WARN_RL(&error_rl, "%s: failed to add %s as port: %s",
586                      wdp_name(wdp), devname, strerror(error));
587         port_no = OFPP_NONE;
588     }
589     if (port_nop) {
590         *port_nop = port_no;
591     }
592     return error;
593 }
594
595 /* Attempts to remove 'wdp''s port numbered 'port_no'.  Returns 0 if
596  * successful, otherwise a positive errno value.
597  *
598  * Some wildcarded datapaths might have fixed sets of ports.  For these
599  * datapaths this function will always fail.
600  *
601  * Possible error return values include:
602  *
603  *   - EINVAL: 'port_no' is outside the valid range, or this particular port is
604  *     not removable (e.g. it is the local port).
605  *
606  *   - ENOENT: 'wdp' currently has no port numbered 'port_no'.
607  *
608  *   - EOPNOTSUPP: 'wdp' has a fixed set of ports.
609  */
610 int
611 wdp_port_del(struct wdp *wdp, uint16_t port_no)
612 {
613     int error;
614
615     COVERAGE_INC(wdp_port_del);
616
617     error = (wdp->wdp_class->port_del
618              ? wdp->wdp_class->port_del(wdp, port_no)
619              : EOPNOTSUPP);
620     log_operation(wdp, "port_del", error);
621     return error;
622 }
623
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()).
627  *
628  * The caller takes ownership of everything in '*portp' and will eventually
629  * free it with, e.g., wdp_port_free().
630  *
631  * Possible error return values include:
632  *
633  *   - EINVAL: 'port_no' is outside the valid range.
634  *
635  *   - ENOENT: 'wdp' currently has no port numbered 'port_no'.
636  */
637 int
638 wdp_port_query_by_number(const struct wdp *wdp, uint16_t port_no,
639                          struct wdp_port *port)
640 {
641     int error;
642
643     error = wdp->wdp_class->port_query_by_number(wdp, port_no, port);
644     if (!error) {
645         VLOG_DBG_RL(&wdpmsg_rl, "%s: port %"PRIu16" is device %s",
646                     wdp_name(wdp), port_no, port->devname);
647     } else {
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));
651     }
652     return error;
653 }
654
655 /* Same as wdp_port_query_by_number() except that it look for a port named
656  * 'devname' in 'wdp'.
657  *
658  * Possible error return values include:
659  *
660  *   - ENODEV: No device named 'devname' exists.
661  *
662  *   - ENOENT: 'devname' exists but it is not attached as a port on 'wdp'.
663  */
664 int
665 wdp_port_query_by_name(const struct wdp *wdp, const char *devname,
666                        struct wdp_port *port)
667 {
668     int error = wdp->wdp_class->port_query_by_name(wdp, devname, port);
669     if (!error) {
670         VLOG_DBG_RL(&wdpmsg_rl, "%s: device %s is on port %"PRIu16,
671                     wdp_name(wdp), devname, port->opp.port_no);
672     } else {
673         wdp_port_clear(port);
674
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));
680     }
681     return error;
682 }
683
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'.
687  *
688  * Error return values are the same as for wdp_port_query_by_name().
689  *
690  * The caller is responsible for freeing '*namep' (with free()). */
691 int
692 wdp_port_get_name(struct wdp *wdp, uint16_t port_no, char **namep)
693 {
694     struct wdp_port port;
695     int error;
696
697     error = wdp_port_query_by_number(wdp, port_no, &port);
698     *namep = port.devname;
699     port.devname = NULL;
700     wdp_port_free(&port);
701
702     return error;
703 }
704
705 /* Obtains a list of all the ports in 'wdp', in no particular order.
706  *
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
710  * '*n_portsp' to 0.
711  *
712  * The caller is responsible for freeing '*portsp' and the individual wdp_port
713  * structures, e.g. with wdp_port_array_free().  */
714 int
715 wdp_port_list(const struct wdp *wdp,
716               struct wdp_port **portsp, size_t *n_portsp)
717 {
718     int error;
719
720     error = wdp->wdp_class->port_list(wdp, portsp, n_portsp);
721     if (error) {
722         *portsp = NULL;
723         *n_portsp = 0;
724     }
725     log_operation(wdp, "port_list", error);
726     return error;
727 }
728
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
732  * ofp_mkerr().  */
733 int
734 wdp_port_set_config(struct wdp *wdp, uint16_t port_no, uint32_t config)
735 {
736     return wdp->wdp_class->port_set_config(wdp, port_no, config);
737 }
738
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:
742  *
743  *   - For a port that has been added, OFPPR_ADD as 'reason' and the new port's
744  *     "struct ofp_phy_port" as 'opp'.
745  *
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'.
748  *
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'.
751  *
752  * 'opp' is in *host* byte order.
753  *
754  * Normally returns 0.  May also return a positive errno value to indicate
755  * that something has gone wrong.
756  */
757 int
758 wdp_port_poll(struct wdp *wdp, wdp_port_poll_cb_func *cb, void *aux)
759 {
760     return wdp->wdp_class->port_poll(wdp, cb, aux);
761 }
762
763 /* Arranges for the poll loop to wake up when 'port_poll' will call its
764  * callback. */
765 int
766 wdp_port_poll_wait(const struct wdp *wdp)
767 {
768     return wdp->wdp_class->port_poll_wait(wdp);
769 }
770
771 /* Deletes all flows from 'wdp'.  Returns 0 if successful, otherwise a
772  * positive errno value.  */
773 int
774 wdp_flow_flush(struct wdp *wdp)
775 {
776     int error;
777
778     COVERAGE_INC(wdp_flow_flush);
779
780     error = wdp->wdp_class->flow_flush(wdp);
781     log_operation(wdp, "flow_flush", error);
782     return error;
783 }
784
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.
788  *
789  * A flow in table 'table_id' is a candidate for matching if 'include & (1u <<
790  * table_id)' is nonzero. */
791 struct wdp_rule *
792 wdp_flow_get(struct wdp *wdp, const flow_t *flow, unsigned int include)
793 {
794     return wdp->wdp_class->flow_get(wdp, flow, include);
795 }
796
797 struct wdp_rule *
798 wdp_flow_match(struct wdp *wdp, const flow_t *flow)
799 {
800     return wdp->wdp_class->flow_match(wdp, flow);
801 }
802
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
808  * visited.
809  *
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.
813  *
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.
818  *
819  * Ignores 'target->priority'.
820  *
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'. */
825 int
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)
829 {
830     return wdp->wdp_class->flow_for_each_match(wdp, target, include,
831                                                callback, aux);
832 }
833
834 int
835 wdp_flow_get_stats(const struct wdp *wdp, const struct wdp_rule *rule,
836                    struct wdp_flow_stats *stats)
837 {
838     int error = wdp->wdp_class->flow_get_stats(wdp, rule, stats);
839     if (error) {
840         memset(stats, 0, sizeof *stats);
841     }
842     return error;
843 }
844
845 bool
846 wdp_flow_overlaps(const struct wdp *wdp, const flow_t *flow)
847 {
848     return wdp->wdp_class->flow_overlaps(wdp, flow);
849 }
850
851 int
852 wdp_flow_put(struct wdp *wdp, struct wdp_flow_put *put,
853              struct wdp_flow_stats *old_stats, struct wdp_rule **rulep)
854 {
855     int error = wdp->wdp_class->flow_put(wdp, put, old_stats, rulep);
856     if (error) {
857         if (old_stats) {
858             memset(old_stats, 0, sizeof *old_stats);
859         }
860         if (rulep) {
861             *rulep = NULL;
862         }
863     }
864     return error;
865 }
866
867 int
868 wdp_flow_delete(struct wdp *wdp, struct wdp_rule *rule,
869                 struct wdp_flow_stats *final_stats)
870 {
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);
874     }
875     return error;
876 }
877
878 int
879 wdp_flow_inject(struct wdp *wdp, struct wdp_rule *rule,
880                 uint16_t in_port, const struct ofpbuf *packet)
881 {
882     return wdp->wdp_class->flow_inject(wdp, rule, in_port, packet);
883 }
884
885 int
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)
889 {
890     int error;
891
892     COVERAGE_INC(wdp_execute);
893     if (n_actions > 0) {
894         error = wdp->wdp_class->execute(wdp, in_port, actions,
895                                         n_actions, buf);
896     } else {
897         error = 0;
898     }
899     return error;
900 }
901
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. */
906 int
907 wdp_recv_get_mask(const struct wdp *wdp, int *listen_mask)
908 {
909     int error = wdp->wdp_class->recv_get_mask(wdp, listen_mask);
910     if (error) {
911         *listen_mask = 0;
912     }
913     log_operation(wdp, "recv_get_mask", error);
914     return error;
915 }
916
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. */
921 int
922 wdp_recv_set_mask(struct wdp *wdp, int listen_mask)
923 {
924     int error = wdp->wdp_class->recv_set_mask(wdp, listen_mask);
925     log_operation(wdp, "recv_set_mask", error);
926     return error;
927 }
928
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.
932  *
933  * Returns 0 if successful, otherwise a positive errno value.  EOPNOTSUPP
934  * indicates that 'wdp' does not support sFlow sampling. */
935 int
936 wdp_get_sflow_probability(const struct wdp *wdp, uint32_t *probability)
937 {
938     int error = (wdp->wdp_class->get_sflow_probability
939                  ? wdp->wdp_class->get_sflow_probability(wdp, probability)
940                  : EOPNOTSUPP);
941     if (error) {
942         *probability = 0;
943     }
944     log_operation(wdp, "get_sflow_probability", error);
945     return error;
946 }
947
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.
951  *
952  * Returns 0 if successful, otherwise a positive errno value.  EOPNOTSUPP
953  * indicates that 'wdp' does not support sFlow sampling. */
954 int
955 wdp_set_sflow_probability(struct wdp *wdp, uint32_t probability)
956 {
957     int error = (wdp->wdp_class->set_sflow_probability
958                  ? wdp->wdp_class->set_sflow_probability(wdp, probability)
959                  : EOPNOTSUPP);
960     log_operation(wdp, "set_sflow_probability", error);
961     return error;
962 }
963
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).
968  *
969  * Returns 0 if successful, otherwise a positive errno value.  Returns EAGAIN
970  * if no message is immediately available. */
971 int
972 wdp_recv(struct wdp *wdp, struct wdp_packet *packet)
973 {
974     int error = wdp->wdp_class->recv(wdp, packet);
975     if (!error) {
976         /* XXX vlog_dbg received packet */
977     } else {
978         memset(packet, 0, sizeof *packet);
979         packet->channel = -1;
980     }
981     return error;
982 }
983
984 /* Discards all messages that would otherwise be received by wdp_recv() on
985  * 'wdp'.  Returns 0 if successful, otherwise a positive errno value. */
986 int
987 wdp_recv_purge(struct wdp *wdp)
988 {
989     COVERAGE_INC(wdp_purge);
990     return wdp->wdp_class->recv_purge(wdp);
991 }
992
993 /* Arranges for the poll loop to wake up when 'wdp' has a message queued to be
994  * received with wdp_recv(). */
995 void
996 wdp_recv_wait(struct wdp *wdp)
997 {
998     wdp->wdp_class->recv_wait(wdp);
999 }
1000
1001 /* Obtains the NetFlow engine type and engine ID for 'wdp' into '*engine_type'
1002  * and '*engine_id', respectively. */
1003 void
1004 wdp_get_netflow_ids(const struct wdp *wdp,
1005                     uint8_t *engine_type, uint8_t *engine_id)
1006 {
1007     *engine_type = wdp->netflow_engine_type;
1008     *engine_id = wdp->netflow_engine_id;
1009 }
1010 \f
1011 /* ovs-vswitchd interface.
1012  *
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
1015  * ovs-vswitchd. */
1016
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.
1020  *
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
1023  * redone.
1024  *
1025  * Returns 0 if successful, otherwise a positive errno value. */
1026 int
1027 wdp_set_ofhooks(struct wdp *wdp, const struct ofhooks *ofhooks, void *aux)
1028 {
1029     int error;
1030     error = (wdp->wdp_class->set_ofhooks
1031              ? wdp->wdp_class->set_ofhooks(wdp, ofhooks, aux)
1032              : EOPNOTSUPP);
1033     log_operation(wdp, "set_ofhooks", error);
1034     return error;
1035 }
1036
1037 /* Tell 'wdp' to revalidate all the flows that match 'tag'.
1038  *
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. */
1042 void
1043 wdp_revalidate(struct wdp *wdp, tag_type tag)
1044 {
1045     if (wdp->wdp_class->revalidate) {
1046         wdp->wdp_class->revalidate(wdp, tag);
1047     }
1048 }
1049
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.)
1053  *
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. */
1057 void
1058 wdp_revalidate_all(struct wdp *wdp)
1059 {
1060     if (wdp->wdp_class->revalidate_all) {
1061         wdp->wdp_class->revalidate_all(wdp);
1062     }
1063 }
1064 \f
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. */
1067 struct wdp_packet *
1068 wdp_packet_clone(const struct wdp_packet *old, size_t trim)
1069 {
1070     struct wdp_packet *new = xmemdup(old, sizeof *old);
1071     if (old->payload) {
1072         new->payload = ofpbuf_clone_data(old->payload->data,
1073                                          MIN(trim, old->payload->size));
1074     }
1075     return new;
1076 }
1077
1078 void
1079 wdp_packet_destroy(struct wdp_packet *packet)
1080 {
1081     if (packet) {
1082         ofpbuf_delete(packet->payload);
1083         free(packet);
1084     }
1085 }
1086
1087 void
1088 wdp_init(struct wdp *wdp, const struct wdp_class *wdp_class,
1089          const char *name,
1090          uint8_t netflow_engine_type, uint8_t netflow_engine_id)
1091 {
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;
1097 }
1098
1099 /* Undoes the results of initialization.
1100  *
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. */
1105 void
1106 wdp_uninit(struct wdp *wdp, bool close)
1107 {
1108     char *base_name = wdp->base_name;
1109     char *full_name = wdp->full_name;
1110
1111     if (close) {
1112         wdp->wdp_class->close(wdp);
1113     }
1114
1115     free(base_name);
1116     free(full_name);
1117 }
1118 \f
1119 static void
1120 log_operation(const struct wdp *wdp, const char *operation, int error)
1121 {
1122     if (!error) {
1123         VLOG_DBG_RL(&wdpmsg_rl, "%s: %s success", wdp_name(wdp), operation);
1124     } else {
1125         VLOG_WARN_RL(&error_rl, "%s: %s failed (%s)",
1126                      wdp_name(wdp), operation, strerror(error));
1127     }
1128 }