]> git.proxmox.com Git - ovs.git/blob - lib/dpif.c
Cleanup incorrect unitialized variable warnings.
[ovs.git] / lib / dpif.c
1 /*
2 * Copyright (c) 2008, 2009 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 "dpif-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 "odp-util.h"
32 #include "ofp-print.h"
33 #include "ofpbuf.h"
34 #include "packets.h"
35 #include "poll-loop.h"
36 #include "svec.h"
37 #include "util.h"
38 #include "valgrind.h"
39
40 #include "vlog.h"
41 #define THIS_MODULE VLM_dpif
42
43 static const struct dpif_class *dpif_classes[] = {
44 &dpif_linux_class,
45 &dpif_netdev_class,
46 };
47 enum { N_DPIF_CLASSES = ARRAY_SIZE(dpif_classes) };
48
49 /* Rate limit for individual messages going to or from the datapath, output at
50 * DBG level. This is very high because, if these are enabled, it is because
51 * we really need to see them. */
52 static struct vlog_rate_limit dpmsg_rl = VLOG_RATE_LIMIT_INIT(600, 600);
53
54 /* Not really much point in logging many dpif errors. */
55 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
56
57 static void log_operation(const struct dpif *, const char *operation,
58 int error);
59 static void log_flow_operation(const struct dpif *, const char *operation,
60 int error, struct odp_flow *flow);
61 static void log_flow_put(struct dpif *, int error,
62 const struct odp_flow_put *);
63 static bool should_log_flow_message(int error);
64 static void check_rw_odp_flow(struct odp_flow *);
65
66 /* Performs periodic work needed by all the various kinds of dpifs.
67 *
68 * If your program opens any dpifs, it must call both this function and
69 * netdev_run() within its main poll loop. */
70 void
71 dp_run(void)
72 {
73 int i;
74 for (i = 0; i < N_DPIF_CLASSES; i++) {
75 const struct dpif_class *class = dpif_classes[i];
76 if (class->run) {
77 class->run();
78 }
79 }
80 }
81
82 /* Arranges for poll_block() to wake up when dp_run() needs to be called.
83 *
84 * If your program opens any dpifs, it must call both this function and
85 * netdev_wait() within its main poll loop. */
86 void
87 dp_wait(void)
88 {
89 int i;
90 for (i = 0; i < N_DPIF_CLASSES; i++) {
91 const struct dpif_class *class = dpif_classes[i];
92 if (class->wait) {
93 class->wait();
94 }
95 }
96 }
97
98 /* Clears 'all_dps' and enumerates the names of all known created datapaths,
99 * where possible, into it. The caller must first initialize 'all_dps'.
100 * Returns 0 if successful, otherwise a positive errno value.
101 *
102 * Some kinds of datapaths might not be practically enumerable. This is not
103 * considered an error. */
104 int
105 dp_enumerate(struct svec *all_dps)
106 {
107 int error;
108 int i;
109
110 svec_clear(all_dps);
111 error = 0;
112 for (i = 0; i < N_DPIF_CLASSES; i++) {
113 const struct dpif_class *class = dpif_classes[i];
114 int retval = class->enumerate ? class->enumerate(all_dps) : 0;
115 if (retval) {
116 VLOG_WARN("failed to enumerate %s datapaths: %s",
117 class->name, strerror(retval));
118 if (!error) {
119 error = retval;
120 }
121 }
122 }
123 return error;
124 }
125
126 static int
127 do_open(const char *name_, bool create, struct dpif **dpifp)
128 {
129 char *name = xstrdup(name_);
130 char *prefix, *suffix, *colon;
131 struct dpif *dpif = NULL;
132 int error;
133 int i;
134
135 colon = strchr(name, ':');
136 if (colon) {
137 *colon = '\0';
138 prefix = name;
139 suffix = colon + 1;
140 } else {
141 prefix = "";
142 suffix = name;
143 }
144
145 for (i = 0; i < N_DPIF_CLASSES; i++) {
146 const struct dpif_class *class = dpif_classes[i];
147 if (!strcmp(prefix, class->prefix)) {
148 error = class->open(name_, suffix, create, &dpif);
149 goto exit;
150 }
151 }
152 error = EAFNOSUPPORT;
153
154 exit:
155 *dpifp = error ? NULL : dpif;
156 return error;
157 }
158
159 /* Tries to open an existing datapath named 'name'. Will fail if no datapath
160 * named 'name' exists. Returns 0 if successful, otherwise a positive errno
161 * value. On success stores a pointer to the datapath in '*dpifp', otherwise a
162 * null pointer. */
163 int
164 dpif_open(const char *name, struct dpif **dpifp)
165 {
166 return do_open(name, false, dpifp);
167 }
168
169 /* Tries to create and open a new datapath with the given 'name'. Will fail if
170 * a datapath named 'name' already exists. Returns 0 if successful, otherwise
171 * a positive errno value. On success stores a pointer to the datapath in
172 * '*dpifp', otherwise a null pointer.*/
173 int
174 dpif_create(const char *name, struct dpif **dpifp)
175 {
176 return do_open(name, true, dpifp);
177 }
178
179 /* Closes and frees the connection to 'dpif'. Does not destroy the datapath
180 * itself; call dpif_delete() first, instead, if that is desirable. */
181 void
182 dpif_close(struct dpif *dpif)
183 {
184 if (dpif) {
185 char *name = dpif->name;
186 dpif->class->close(dpif);
187 free(name);
188 }
189 }
190
191 /* Returns the name of datapath 'dpif' (for use in log messages). */
192 const char *
193 dpif_name(const struct dpif *dpif)
194 {
195 return dpif->name;
196 }
197
198 /* Enumerates all names that may be used to open 'dpif' into 'all_names'. The
199 * Linux datapath, for example, supports opening a datapath both by number,
200 * e.g. "dp0", and by the name of the datapath's local port. For some
201 * datapaths, this might be an infinite set (e.g. in a file name, slashes may
202 * be duplicated any number of times), in which case only the names most likely
203 * to be used will be enumerated.
204 *
205 * The caller must already have initialized 'all_names'. Any existing names in
206 * 'all_names' will not be disturbed. */
207 int
208 dpif_get_all_names(const struct dpif *dpif, struct svec *all_names)
209 {
210 if (dpif->class->get_all_names) {
211 int error = dpif->class->get_all_names(dpif, all_names);
212 if (error) {
213 VLOG_WARN_RL(&error_rl,
214 "failed to retrieve names for datpath %s: %s",
215 dpif_name(dpif), strerror(error));
216 }
217 return error;
218 } else {
219 svec_add(all_names, dpif_name(dpif));
220 return 0;
221 }
222 }
223
224 /* Destroys the datapath that 'dpif' is connected to, first removing all of its
225 * ports. After calling this function, it does not make sense to pass 'dpif'
226 * to any functions other than dpif_name() or dpif_close(). */
227 int
228 dpif_delete(struct dpif *dpif)
229 {
230 int error;
231
232 COVERAGE_INC(dpif_destroy);
233
234 error = dpif->class->delete(dpif);
235 log_operation(dpif, "delete", error);
236 return error;
237 }
238
239 /* Retrieves statistics for 'dpif' into 'stats'. Returns 0 if successful,
240 * otherwise a positive errno value. */
241 int
242 dpif_get_dp_stats(const struct dpif *dpif, struct odp_stats *stats)
243 {
244 int error = dpif->class->get_stats(dpif, stats);
245 if (error) {
246 memset(stats, 0, sizeof *stats);
247 }
248 log_operation(dpif, "get_stats", error);
249 return error;
250 }
251
252 /* Retrieves the current IP fragment handling policy for 'dpif' into
253 * '*drop_frags': true indicates that fragments are dropped, false indicates
254 * that fragments are treated in the same way as other IP packets (except that
255 * the L4 header cannot be read). Returns 0 if successful, otherwise a
256 * positive errno value. */
257 int
258 dpif_get_drop_frags(const struct dpif *dpif, bool *drop_frags)
259 {
260 int error = dpif->class->get_drop_frags(dpif, drop_frags);
261 if (error) {
262 *drop_frags = false;
263 }
264 log_operation(dpif, "get_drop_frags", error);
265 return error;
266 }
267
268 /* Changes 'dpif''s treatment of IP fragments to 'drop_frags', whose meaning is
269 * the same as for the get_drop_frags member function. Returns 0 if
270 * successful, otherwise a positive errno value. */
271 int
272 dpif_set_drop_frags(struct dpif *dpif, bool drop_frags)
273 {
274 int error = dpif->class->set_drop_frags(dpif, drop_frags);
275 log_operation(dpif, "set_drop_frags", error);
276 return error;
277 }
278
279 /* Attempts to add 'devname' as a port on 'dpif', given the combination of
280 * ODP_PORT_* flags in 'flags'. If successful, returns 0 and sets '*port_nop'
281 * to the new port's port number (if 'port_nop' is non-null). On failure,
282 * returns a positive errno value and sets '*port_nop' to UINT16_MAX (if
283 * 'port_nop' is non-null). */
284 int
285 dpif_port_add(struct dpif *dpif, const char *devname, uint16_t flags,
286 uint16_t *port_nop)
287 {
288 uint16_t port_no;
289 int error;
290
291 COVERAGE_INC(dpif_port_add);
292
293 error = dpif->class->port_add(dpif, devname, flags, &port_no);
294 if (!error) {
295 VLOG_DBG_RL(&dpmsg_rl, "%s: added %s as port %"PRIu16,
296 dpif_name(dpif), devname, port_no);
297 } else {
298 VLOG_WARN_RL(&error_rl, "%s: failed to add %s as port: %s",
299 dpif_name(dpif), devname, strerror(error));
300 port_no = UINT16_MAX;
301 }
302 if (port_nop) {
303 *port_nop = port_no;
304 }
305 return error;
306 }
307
308 /* Attempts to remove 'dpif''s port number 'port_no'. Returns 0 if successful,
309 * otherwise a positive errno value. */
310 int
311 dpif_port_del(struct dpif *dpif, uint16_t port_no)
312 {
313 int error;
314
315 COVERAGE_INC(dpif_port_del);
316
317 error = dpif->class->port_del(dpif, port_no);
318 log_operation(dpif, "port_del", error);
319 return error;
320 }
321
322 /* Looks up port number 'port_no' in 'dpif'. On success, returns 0 and
323 * initializes '*port' appropriately; on failure, returns a positive errno
324 * value. */
325 int
326 dpif_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
327 struct odp_port *port)
328 {
329 int error = dpif->class->port_query_by_number(dpif, port_no, port);
330 if (!error) {
331 VLOG_DBG_RL(&dpmsg_rl, "%s: port %"PRIu16" is device %s",
332 dpif_name(dpif), port_no, port->devname);
333 } else {
334 memset(port, 0, sizeof *port);
335 VLOG_WARN_RL(&error_rl, "%s: failed to query port %"PRIu16": %s",
336 dpif_name(dpif), port_no, strerror(error));
337 }
338 return error;
339 }
340
341 /* Looks up port named 'devname' in 'dpif'. On success, returns 0 and
342 * initializes '*port' appropriately; on failure, returns a positive errno
343 * value. */
344 int
345 dpif_port_query_by_name(const struct dpif *dpif, const char *devname,
346 struct odp_port *port)
347 {
348 int error = dpif->class->port_query_by_name(dpif, devname, port);
349 if (!error) {
350 VLOG_DBG_RL(&dpmsg_rl, "%s: device %s is on port %"PRIu16,
351 dpif_name(dpif), devname, port->port);
352 } else {
353 memset(port, 0, sizeof *port);
354
355 /* Log level is DBG here because all the current callers are interested
356 * in whether 'dpif' actually has a port 'devname', so that it's not an
357 * issue worth logging if it doesn't. */
358 VLOG_DBG_RL(&error_rl, "%s: failed to query port %s: %s",
359 dpif_name(dpif), devname, strerror(error));
360 }
361 return error;
362 }
363
364 /* Looks up port number 'port_no' in 'dpif'. On success, returns 0 and copies
365 * the port's name into the 'name_size' bytes in 'name', ensuring that the
366 * result is null-terminated. On failure, returns a positive errno value and
367 * makes 'name' the empty string. */
368 int
369 dpif_port_get_name(struct dpif *dpif, uint16_t port_no,
370 char *name, size_t name_size)
371 {
372 struct odp_port port;
373 int error;
374
375 assert(name_size > 0);
376
377 error = dpif_port_query_by_number(dpif, port_no, &port);
378 if (!error) {
379 ovs_strlcpy(name, port.devname, name_size);
380 } else {
381 *name = '\0';
382 }
383 return error;
384 }
385
386 /* Obtains a list of all the ports in 'dpif'.
387 *
388 * If successful, returns 0 and sets '*portsp' to point to an array of
389 * appropriately initialized port structures and '*n_portsp' to the number of
390 * ports in the array. The caller is responsible for freeing '*portp' by
391 * calling free().
392 *
393 * On failure, returns a positive errno value and sets '*portsp' to NULL and
394 * '*n_portsp' to 0. */
395 int
396 dpif_port_list(const struct dpif *dpif,
397 struct odp_port **portsp, size_t *n_portsp)
398 {
399 struct odp_port *ports;
400 size_t n_ports = 0;
401 int error;
402
403 for (;;) {
404 struct odp_stats stats;
405 int retval;
406
407 error = dpif_get_dp_stats(dpif, &stats);
408 if (error) {
409 goto exit;
410 }
411
412 ports = xcalloc(stats.n_ports, sizeof *ports);
413 retval = dpif->class->port_list(dpif, ports, stats.n_ports);
414 if (retval < 0) {
415 /* Hard error. */
416 error = -retval;
417 free(ports);
418 goto exit;
419 } else if (retval <= stats.n_ports) {
420 /* Success. */
421 error = 0;
422 n_ports = retval;
423 goto exit;
424 } else {
425 /* Soft error: port count increased behind our back. Try again. */
426 free(ports);
427 }
428 }
429
430 exit:
431 if (error) {
432 *portsp = NULL;
433 *n_portsp = 0;
434 } else {
435 *portsp = ports;
436 *n_portsp = n_ports;
437 }
438 log_operation(dpif, "port_list", error);
439 return error;
440 }
441
442 /* Polls for changes in the set of ports in 'dpif'. If the set of ports in
443 * 'dpif' has changed, this function does one of the following:
444 *
445 * - Stores the name of the device that was added to or deleted from 'dpif' in
446 * '*devnamep' and returns 0. The caller is responsible for freeing
447 * '*devnamep' (with free()) when it no longer needs it.
448 *
449 * - Returns ENOBUFS and sets '*devnamep' to NULL.
450 *
451 * This function may also return 'false positives', where it returns 0 and
452 * '*devnamep' names a device that was not actually added or deleted or it
453 * returns ENOBUFS without any change.
454 *
455 * Returns EAGAIN if the set of ports in 'dpif' has not changed. May also
456 * return other positive errno values to indicate that something has gone
457 * wrong. */
458 int
459 dpif_port_poll(const struct dpif *dpif, char **devnamep)
460 {
461 int error = dpif->class->port_poll(dpif, devnamep);
462 if (error) {
463 *devnamep = NULL;
464 }
465 return error;
466 }
467
468 /* Arranges for the poll loop to wake up when port_poll(dpif) will return a
469 * value other than EAGAIN. */
470 void
471 dpif_port_poll_wait(const struct dpif *dpif)
472 {
473 dpif->class->port_poll_wait(dpif);
474 }
475
476 /* Retrieves a list of the port numbers in port group 'group' in 'dpif'.
477 *
478 * On success, returns 0 and points '*ports' to a newly allocated array of
479 * integers, each of which is a 'dpif' port number for a port in
480 * 'group'. Stores the number of elements in the array in '*n_ports'. The
481 * caller is responsible for freeing '*ports' by calling free().
482 *
483 * On failure, returns a positive errno value and sets '*ports' to NULL and
484 * '*n_ports' to 0. */
485 int
486 dpif_port_group_get(const struct dpif *dpif, uint16_t group,
487 uint16_t **ports, size_t *n_ports)
488 {
489 int error;
490
491 *ports = NULL;
492 *n_ports = 0;
493 for (;;) {
494 int retval = dpif->class->port_group_get(dpif, group,
495 *ports, *n_ports);
496 if (retval < 0) {
497 /* Hard error. */
498 error = -retval;
499 free(*ports);
500 *ports = NULL;
501 *n_ports = 0;
502 break;
503 } else if (retval <= *n_ports) {
504 /* Success. */
505 error = 0;
506 *n_ports = retval;
507 break;
508 } else {
509 /* Soft error: there were more ports than we expected in the
510 * group. Try again. */
511 free(*ports);
512 *ports = xcalloc(retval, sizeof **ports);
513 *n_ports = retval;
514 }
515 }
516 log_operation(dpif, "port_group_get", error);
517 return error;
518 }
519
520 /* Updates port group 'group' in 'dpif', making it contain the 'n_ports' ports
521 * whose 'dpif' port numbers are given in 'n_ports'. Returns 0 if
522 * successful, otherwise a positive errno value.
523 *
524 * Behavior is undefined if the values in ports[] are not unique. */
525 int
526 dpif_port_group_set(struct dpif *dpif, uint16_t group,
527 const uint16_t ports[], size_t n_ports)
528 {
529 int error;
530
531 COVERAGE_INC(dpif_port_group_set);
532
533 error = dpif->class->port_group_set(dpif, group, ports, n_ports);
534 log_operation(dpif, "port_group_set", error);
535 return error;
536 }
537
538 /* Deletes all flows from 'dpif'. Returns 0 if successful, otherwise a
539 * positive errno value. */
540 int
541 dpif_flow_flush(struct dpif *dpif)
542 {
543 int error;
544
545 COVERAGE_INC(dpif_flow_flush);
546
547 error = dpif->class->flow_flush(dpif);
548 log_operation(dpif, "flow_flush", error);
549 return error;
550 }
551
552 /* Queries 'dpif' for a flow entry matching 'flow->key'.
553 *
554 * If a flow matching 'flow->key' exists in 'dpif', stores statistics for the
555 * flow into 'flow->stats'. If 'flow->n_actions' is zero, then 'flow->actions'
556 * is ignored. If 'flow->n_actions' is nonzero, then 'flow->actions' should
557 * point to an array of the specified number of actions. At most that many of
558 * the flow's actions will be copied into that array. 'flow->n_actions' will
559 * be updated to the number of actions actually present in the flow, which may
560 * be greater than the number stored if the flow has more actions than space
561 * available in the array.
562 *
563 * If no flow matching 'flow->key' exists in 'dpif', returns ENOENT. On other
564 * failure, returns a positive errno value. */
565 int
566 dpif_flow_get(const struct dpif *dpif, struct odp_flow *flow)
567 {
568 int error;
569
570 COVERAGE_INC(dpif_flow_get);
571
572 check_rw_odp_flow(flow);
573 error = dpif->class->flow_get(dpif, flow, 1);
574 if (!error) {
575 error = flow->stats.error;
576 }
577 if (should_log_flow_message(error)) {
578 log_flow_operation(dpif, "flow_get", error, flow);
579 }
580 return error;
581 }
582
583 /* For each flow 'flow' in the 'n' flows in 'flows':
584 *
585 * - If a flow matching 'flow->key' exists in 'dpif':
586 *
587 * Stores 0 into 'flow->stats.error' and stores statistics for the flow
588 * into 'flow->stats'.
589 *
590 * If 'flow->n_actions' is zero, then 'flow->actions' is ignored. If
591 * 'flow->n_actions' is nonzero, then 'flow->actions' should point to an
592 * array of the specified number of actions. At most that many of the
593 * flow's actions will be copied into that array. 'flow->n_actions' will
594 * be updated to the number of actions actually present in the flow, which
595 * may be greater than the number stored if the flow has more actions than
596 * space available in the array.
597 *
598 * - Flow-specific errors are indicated by a positive errno value in
599 * 'flow->stats.error'. In particular, ENOENT indicates that no flow
600 * matching 'flow->key' exists in 'dpif'. When an error value is stored, the
601 * contents of 'flow->key' are preserved but other members of 'flow' should
602 * be treated as indeterminate.
603 *
604 * Returns 0 if all 'n' flows in 'flows' were updated (whether they were
605 * individually successful or not is indicated by 'flow->stats.error',
606 * however). Returns a positive errno value if an error that prevented this
607 * update occurred, in which the caller must not depend on any elements in
608 * 'flows' being updated or not updated.
609 */
610 int
611 dpif_flow_get_multiple(const struct dpif *dpif,
612 struct odp_flow flows[], size_t n)
613 {
614 int error;
615 size_t i;
616
617 COVERAGE_ADD(dpif_flow_get, n);
618
619 for (i = 0; i < n; i++) {
620 check_rw_odp_flow(&flows[i]);
621 }
622
623 error = dpif->class->flow_get(dpif, flows, n);
624 log_operation(dpif, "flow_get_multiple", error);
625 return error;
626 }
627
628 /* Adds or modifies a flow in 'dpif' as specified in 'put':
629 *
630 * - If the flow specified in 'put->flow' does not exist in 'dpif', then
631 * behavior depends on whether ODPPF_CREATE is specified in 'put->flags': if
632 * it is, the flow will be added, otherwise the operation will fail with
633 * ENOENT.
634 *
635 * - Otherwise, the flow specified in 'put->flow' does exist in 'dpif'.
636 * Behavior in this case depends on whether ODPPF_MODIFY is specified in
637 * 'put->flags': if it is, the flow's actions will be updated, otherwise the
638 * operation will fail with EEXIST. If the flow's actions are updated, then
639 * its statistics will be zeroed if ODPPF_ZERO_STATS is set in 'put->flags',
640 * left as-is otherwise.
641 *
642 * Returns 0 if successful, otherwise a positive errno value.
643 */
644 int
645 dpif_flow_put(struct dpif *dpif, struct odp_flow_put *put)
646 {
647 int error;
648
649 COVERAGE_INC(dpif_flow_put);
650
651 error = dpif->class->flow_put(dpif, put);
652 if (should_log_flow_message(error)) {
653 log_flow_put(dpif, error, put);
654 }
655 return error;
656 }
657
658 /* Deletes a flow matching 'flow->key' from 'dpif' or returns ENOENT if 'dpif'
659 * does not contain such a flow.
660 *
661 * If successful, updates 'flow->stats', 'flow->n_actions', and 'flow->actions'
662 * as described for dpif_flow_get(). */
663 int
664 dpif_flow_del(struct dpif *dpif, struct odp_flow *flow)
665 {
666 int error;
667
668 COVERAGE_INC(dpif_flow_del);
669
670 check_rw_odp_flow(flow);
671 memset(&flow->stats, 0, sizeof flow->stats);
672
673 error = dpif->class->flow_del(dpif, flow);
674 if (should_log_flow_message(error)) {
675 log_flow_operation(dpif, "delete flow", error, flow);
676 }
677 return error;
678 }
679
680 /* Stores up to 'n' flows in 'dpif' into 'flows', including their statistics
681 * but not including any information about their actions. If successful,
682 * returns 0 and sets '*n_out' to the number of flows actually present in
683 * 'dpif', which might be greater than the number stored (if 'dpif' has more
684 * than 'n' flows). On failure, returns a negative errno value and sets
685 * '*n_out' to 0. */
686 int
687 dpif_flow_list(const struct dpif *dpif, struct odp_flow flows[], size_t n,
688 size_t *n_out)
689 {
690 uint32_t i;
691 int retval;
692
693 COVERAGE_INC(dpif_flow_query_list);
694 if (RUNNING_ON_VALGRIND) {
695 memset(flows, 0, n * sizeof *flows);
696 } else {
697 for (i = 0; i < n; i++) {
698 flows[i].actions = NULL;
699 flows[i].n_actions = 0;
700 }
701 }
702 retval = dpif->class->flow_list(dpif, flows, n);
703 if (retval < 0) {
704 *n_out = 0;
705 VLOG_WARN_RL(&error_rl, "%s: flow list failed (%s)",
706 dpif_name(dpif), strerror(-retval));
707 return -retval;
708 } else {
709 COVERAGE_ADD(dpif_flow_query_list_n, retval);
710 *n_out = MIN(n, retval);
711 VLOG_DBG_RL(&dpmsg_rl, "%s: listed %zu flows (of %d)",
712 dpif_name(dpif), *n_out, retval);
713 return 0;
714 }
715 }
716
717 /* Retrieves all of the flows in 'dpif'.
718 *
719 * If successful, returns 0 and stores in '*flowsp' a pointer to a newly
720 * allocated array of flows, including their statistics but not including any
721 * information about their actions, and sets '*np' to the number of flows in
722 * '*flowsp'. The caller is responsible for freeing '*flowsp' by calling
723 * free().
724 *
725 * On failure, returns a positive errno value and sets '*flowsp' to NULL and
726 * '*np' to 0. */
727 int
728 dpif_flow_list_all(const struct dpif *dpif,
729 struct odp_flow **flowsp, size_t *np)
730 {
731 struct odp_stats stats;
732 struct odp_flow *flows;
733 size_t n_flows;
734 int error;
735
736 *flowsp = NULL;
737 *np = 0;
738
739 error = dpif_get_dp_stats(dpif, &stats);
740 if (error) {
741 return error;
742 }
743
744 flows = xmalloc(sizeof *flows * stats.n_flows);
745 error = dpif_flow_list(dpif, flows, stats.n_flows, &n_flows);
746 if (error) {
747 free(flows);
748 return error;
749 }
750
751 if (stats.n_flows != n_flows) {
752 VLOG_WARN_RL(&error_rl, "%s: datapath stats reported %"PRIu32" "
753 "flows but flow listing reported %zu",
754 dpif_name(dpif), stats.n_flows, n_flows);
755 }
756 *flowsp = flows;
757 *np = n_flows;
758 return 0;
759 }
760
761 /* Causes 'dpif' to perform the 'n_actions' actions in 'actions' on the
762 * Ethernet frame specified in 'packet'.
763 *
764 * Pretends that the frame was originally received on the port numbered
765 * 'in_port'. This affects only ODPAT_OUTPUT_GROUP actions, which will not
766 * send a packet out their input port. Specify the number of an unused port
767 * (e.g. UINT16_MAX is currently always unused) to avoid this behavior.
768 *
769 * Returns 0 if successful, otherwise a positive errno value. */
770 int
771 dpif_execute(struct dpif *dpif, uint16_t in_port,
772 const union odp_action actions[], size_t n_actions,
773 const struct ofpbuf *buf)
774 {
775 int error;
776
777 COVERAGE_INC(dpif_execute);
778 if (n_actions > 0) {
779 error = dpif->class->execute(dpif, in_port, actions, n_actions, buf);
780 } else {
781 error = 0;
782 }
783
784 if (!(error ? VLOG_DROP_WARN(&error_rl) : VLOG_DROP_DBG(&dpmsg_rl))) {
785 struct ds ds = DS_EMPTY_INITIALIZER;
786 char *packet = ofp_packet_to_string(buf->data, buf->size, buf->size);
787 ds_put_format(&ds, "%s: execute ", dpif_name(dpif));
788 format_odp_actions(&ds, actions, n_actions);
789 if (error) {
790 ds_put_format(&ds, " failed (%s)", strerror(error));
791 }
792 ds_put_format(&ds, " on packet %s", packet);
793 vlog(THIS_MODULE, error ? VLL_WARN : VLL_DBG, "%s", ds_cstr(&ds));
794 ds_destroy(&ds);
795 free(packet);
796 }
797 return error;
798 }
799
800 /* Retrieves 'dpif''s "listen mask" into '*listen_mask'. Each ODPL_* bit set
801 * in '*listen_mask' indicates that dpif_recv() will receive messages of that
802 * type. Returns 0 if successful, otherwise a positive errno value. */
803 int
804 dpif_recv_get_mask(const struct dpif *dpif, int *listen_mask)
805 {
806 int error = dpif->class->recv_get_mask(dpif, listen_mask);
807 if (error) {
808 *listen_mask = 0;
809 }
810 log_operation(dpif, "recv_get_mask", error);
811 return error;
812 }
813
814 /* Sets 'dpif''s "listen mask" to 'listen_mask'. Each ODPL_* bit set in
815 * '*listen_mask' requests that dpif_recv() receive messages of that type.
816 * Returns 0 if successful, otherwise a positive errno value. */
817 int
818 dpif_recv_set_mask(struct dpif *dpif, int listen_mask)
819 {
820 int error = dpif->class->recv_set_mask(dpif, listen_mask);
821 log_operation(dpif, "recv_set_mask", error);
822 return error;
823 }
824
825 /* Attempts to receive a message from 'dpif'. If successful, stores the
826 * message into '*packetp'. The message, if one is received, will begin with
827 * 'struct odp_msg' as a header. Only messages of the types selected with
828 * dpif_set_listen_mask() will ordinarily be received (but if a message type is
829 * enabled and then later disabled, some stragglers might pop up).
830 *
831 * Returns 0 if successful, otherwise a positive errno value. Returns EAGAIN
832 * if no message is immediately available. */
833 int
834 dpif_recv(struct dpif *dpif, struct ofpbuf **packetp)
835 {
836 int error = dpif->class->recv(dpif, packetp);
837 if (!error) {
838 if (VLOG_IS_DBG_ENABLED()) {
839 struct ofpbuf *buf = *packetp;
840 struct odp_msg *msg = buf->data;
841 void *payload = msg + 1;
842 size_t payload_len = buf->size - sizeof *msg;
843 char *s = ofp_packet_to_string(payload, payload_len, payload_len);
844 VLOG_DBG_RL(&dpmsg_rl, "%s: received %s message of length "
845 "%zu on port %"PRIu16": %s", dpif_name(dpif),
846 (msg->type == _ODPL_MISS_NR ? "miss"
847 : msg->type == _ODPL_ACTION_NR ? "action"
848 : "<unknown>"),
849 payload_len, msg->port, s);
850 free(s);
851 }
852 } else {
853 *packetp = NULL;
854 }
855 return error;
856 }
857
858 /* Discards all messages that would otherwise be received by dpif_recv() on
859 * 'dpif'. Returns 0 if successful, otherwise a positive errno value. */
860 int
861 dpif_recv_purge(struct dpif *dpif)
862 {
863 struct odp_stats stats;
864 unsigned int i;
865 int error;
866
867 COVERAGE_INC(dpif_purge);
868
869 error = dpif_get_dp_stats(dpif, &stats);
870 if (error) {
871 return error;
872 }
873
874 for (i = 0; i < stats.max_miss_queue + stats.max_action_queue; i++) {
875 struct ofpbuf *buf;
876 error = dpif_recv(dpif, &buf);
877 if (error) {
878 return error == EAGAIN ? 0 : error;
879 }
880 ofpbuf_delete(buf);
881 }
882 return 0;
883 }
884
885 /* Arranges for the poll loop to wake up when 'dpif' has a message queued to be
886 * received with dpif_recv(). */
887 void
888 dpif_recv_wait(struct dpif *dpif)
889 {
890 dpif->class->recv_wait(dpif);
891 }
892
893 /* Obtains the NetFlow engine type and engine ID for 'dpif' into '*engine_type'
894 * and '*engine_id', respectively. */
895 void
896 dpif_get_netflow_ids(const struct dpif *dpif,
897 uint8_t *engine_type, uint8_t *engine_id)
898 {
899 *engine_type = dpif->netflow_engine_type;
900 *engine_id = dpif->netflow_engine_id;
901 }
902 \f
903 void
904 dpif_init(struct dpif *dpif, const struct dpif_class *class, const char *name,
905 uint8_t netflow_engine_type, uint8_t netflow_engine_id)
906 {
907 dpif->class = class;
908 dpif->name = xstrdup(name);
909 dpif->netflow_engine_type = netflow_engine_type;
910 dpif->netflow_engine_id = netflow_engine_id;
911 }
912 \f
913 static void
914 log_operation(const struct dpif *dpif, const char *operation, int error)
915 {
916 if (!error) {
917 VLOG_DBG_RL(&dpmsg_rl, "%s: %s success", dpif_name(dpif), operation);
918 } else {
919 VLOG_WARN_RL(&error_rl, "%s: %s failed (%s)",
920 dpif_name(dpif), operation, strerror(error));
921 }
922 }
923
924 static enum vlog_level
925 flow_message_log_level(int error)
926 {
927 return error ? VLL_WARN : VLL_DBG;
928 }
929
930 static bool
931 should_log_flow_message(int error)
932 {
933 return !vlog_should_drop(THIS_MODULE, flow_message_log_level(error),
934 error ? &error_rl : &dpmsg_rl);
935 }
936
937 static void
938 log_flow_message(const struct dpif *dpif, int error, const char *operation,
939 const flow_t *flow, const struct odp_flow_stats *stats,
940 const union odp_action *actions, size_t n_actions)
941 {
942 struct ds ds = DS_EMPTY_INITIALIZER;
943 ds_put_format(&ds, "%s: ", dpif_name(dpif));
944 if (error) {
945 ds_put_cstr(&ds, "failed to ");
946 }
947 ds_put_format(&ds, "%s ", operation);
948 if (error) {
949 ds_put_format(&ds, "(%s) ", strerror(error));
950 }
951 flow_format(&ds, flow);
952 if (stats) {
953 ds_put_cstr(&ds, ", ");
954 format_odp_flow_stats(&ds, stats);
955 }
956 if (actions || n_actions) {
957 ds_put_cstr(&ds, ", actions:");
958 format_odp_actions(&ds, actions, n_actions);
959 }
960 vlog(THIS_MODULE, flow_message_log_level(error), "%s", ds_cstr(&ds));
961 ds_destroy(&ds);
962 }
963
964 static void
965 log_flow_operation(const struct dpif *dpif, const char *operation, int error,
966 struct odp_flow *flow)
967 {
968 if (error) {
969 flow->n_actions = 0;
970 }
971 log_flow_message(dpif, error, operation, &flow->key,
972 !error ? &flow->stats : NULL,
973 flow->actions, flow->n_actions);
974 }
975
976 static void
977 log_flow_put(struct dpif *dpif, int error, const struct odp_flow_put *put)
978 {
979 enum { ODPPF_ALL = ODPPF_CREATE | ODPPF_MODIFY | ODPPF_ZERO_STATS };
980 struct ds s;
981
982 ds_init(&s);
983 ds_put_cstr(&s, "put");
984 if (put->flags & ODPPF_CREATE) {
985 ds_put_cstr(&s, "[create]");
986 }
987 if (put->flags & ODPPF_MODIFY) {
988 ds_put_cstr(&s, "[modify]");
989 }
990 if (put->flags & ODPPF_ZERO_STATS) {
991 ds_put_cstr(&s, "[zero]");
992 }
993 if (put->flags & ~ODPPF_ALL) {
994 ds_put_format(&s, "[%x]", put->flags & ~ODPPF_ALL);
995 }
996 log_flow_message(dpif, error, ds_cstr(&s), &put->flow.key,
997 !error ? &put->flow.stats : NULL,
998 put->flow.actions, put->flow.n_actions);
999 ds_destroy(&s);
1000 }
1001
1002 /* There is a tendency to construct odp_flow objects on the stack and to
1003 * forget to properly initialize their "actions" and "n_actions" members.
1004 * When this happens, we get memory corruption because the kernel
1005 * writes through the random pointer that is in the "actions" member.
1006 *
1007 * This function attempts to combat the problem by:
1008 *
1009 * - Forcing a segfault if "actions" points to an invalid region (instead
1010 * of just getting back EFAULT, which can be easily missed in the log).
1011 *
1012 * - Storing a distinctive value that is likely to cause an
1013 * easy-to-identify error later if it is dereferenced, etc.
1014 *
1015 * - Triggering a warning on uninitialized memory from Valgrind if
1016 * "actions" or "n_actions" was not initialized.
1017 */
1018 static void
1019 check_rw_odp_flow(struct odp_flow *flow)
1020 {
1021 if (flow->n_actions) {
1022 memset(&flow->actions[0], 0xcc, sizeof flow->actions[0]);
1023 }
1024 }