]>
git.proxmox.com Git - ovs.git/blob - lib/dpif.c
2 * Copyright (c) 2008, 2009 Nicira Networks.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "dpif-provider.h"
28 #include "dynamic-string.h"
32 #include "ofp-print.h"
35 #include "poll-loop.h"
41 #define THIS_MODULE VLM_dpif
43 static const struct dpif_class
*dpif_classes
[] = {
47 enum { N_DPIF_CLASSES
= ARRAY_SIZE(dpif_classes
) };
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);
54 /* Not really much point in logging many dpif errors. */
55 static struct vlog_rate_limit error_rl
= VLOG_RATE_LIMIT_INIT(9999, 5);
57 static void log_operation(const struct dpif
*, const char *operation
,
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
*);
66 /* Performs periodic work needed by all the various kinds of dpifs.
68 * If your program opens any dpifs, it must call both this function and
69 * netdev_run() within its main poll loop. */
74 for (i
= 0; i
< N_DPIF_CLASSES
; i
++) {
75 const struct dpif_class
*class = dpif_classes
[i
];
82 /* Arranges for poll_block() to wake up when dp_run() needs to be called.
84 * If your program opens any dpifs, it must call both this function and
85 * netdev_wait() within its main poll loop. */
90 for (i
= 0; i
< N_DPIF_CLASSES
; i
++) {
91 const struct dpif_class
*class = dpif_classes
[i
];
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.
102 * Some kinds of datapaths might not be practically enumerable. This is not
103 * considered an error. */
105 dp_enumerate(struct svec
*all_dps
)
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;
116 VLOG_WARN("failed to enumerate %s datapaths: %s",
117 class->name
, strerror(retval
));
127 do_open(const char *name_
, bool create
, struct dpif
**dpifp
)
129 char *name
= xstrdup(name_
);
130 char *prefix
, *suffix
, *colon
;
131 struct dpif
*dpif
= NULL
;
135 colon
= strchr(name
, ':');
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
);
152 error
= EAFNOSUPPORT
;
155 *dpifp
= error
? NULL
: dpif
;
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
164 dpif_open(const char *name
, struct dpif
**dpifp
)
166 return do_open(name
, false, dpifp
);
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.*/
174 dpif_create(const char *name
, struct dpif
**dpifp
)
176 return do_open(name
, true, dpifp
);
179 /* Closes and frees the connection to 'dpif'. Does not destroy the datapath
180 * itself; call dpif_delete() first, instead, if that is desirable. */
182 dpif_close(struct dpif
*dpif
)
185 char *name
= dpif
->name
;
186 dpif
->class->close(dpif
);
191 /* Returns the name of datapath 'dpif' (for use in log messages). */
193 dpif_name(const struct dpif
*dpif
)
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.
205 * The caller must already have initialized 'all_names'. Any existing names in
206 * 'all_names' will not be disturbed. */
208 dpif_get_all_names(const struct dpif
*dpif
, struct svec
*all_names
)
210 if (dpif
->class->get_all_names
) {
211 int error
= dpif
->class->get_all_names(dpif
, all_names
);
213 VLOG_WARN_RL(&error_rl
,
214 "failed to retrieve names for datpath %s: %s",
215 dpif_name(dpif
), strerror(error
));
219 svec_add(all_names
, dpif_name(dpif
));
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(). */
228 dpif_delete(struct dpif
*dpif
)
232 COVERAGE_INC(dpif_destroy
);
234 error
= dpif
->class->delete(dpif
);
235 log_operation(dpif
, "delete", error
);
239 /* Retrieves statistics for 'dpif' into 'stats'. Returns 0 if successful,
240 * otherwise a positive errno value. */
242 dpif_get_dp_stats(const struct dpif
*dpif
, struct odp_stats
*stats
)
244 int error
= dpif
->class->get_stats(dpif
, stats
);
246 memset(stats
, 0, sizeof *stats
);
248 log_operation(dpif
, "get_stats", error
);
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. */
258 dpif_get_drop_frags(const struct dpif
*dpif
, bool *drop_frags
)
260 int error
= dpif
->class->get_drop_frags(dpif
, drop_frags
);
264 log_operation(dpif
, "get_drop_frags", error
);
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. */
272 dpif_set_drop_frags(struct dpif
*dpif
, bool drop_frags
)
274 int error
= dpif
->class->set_drop_frags(dpif
, drop_frags
);
275 log_operation(dpif
, "set_drop_frags", error
);
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). */
285 dpif_port_add(struct dpif
*dpif
, const char *devname
, uint16_t flags
,
291 COVERAGE_INC(dpif_port_add
);
293 error
= dpif
->class->port_add(dpif
, devname
, flags
, &port_no
);
295 VLOG_DBG_RL(&dpmsg_rl
, "%s: added %s as port %"PRIu16
,
296 dpif_name(dpif
), devname
, port_no
);
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
;
308 /* Attempts to remove 'dpif''s port number 'port_no'. Returns 0 if successful,
309 * otherwise a positive errno value. */
311 dpif_port_del(struct dpif
*dpif
, uint16_t port_no
)
315 COVERAGE_INC(dpif_port_del
);
317 error
= dpif
->class->port_del(dpif
, port_no
);
318 log_operation(dpif
, "port_del", error
);
322 /* Looks up port number 'port_no' in 'dpif'. On success, returns 0 and
323 * initializes '*port' appropriately; on failure, returns a positive errno
326 dpif_port_query_by_number(const struct dpif
*dpif
, uint16_t port_no
,
327 struct odp_port
*port
)
329 int error
= dpif
->class->port_query_by_number(dpif
, port_no
, port
);
331 VLOG_DBG_RL(&dpmsg_rl
, "%s: port %"PRIu16
" is device %s",
332 dpif_name(dpif
), port_no
, port
->devname
);
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
));
341 /* Looks up port named 'devname' in 'dpif'. On success, returns 0 and
342 * initializes '*port' appropriately; on failure, returns a positive errno
345 dpif_port_query_by_name(const struct dpif
*dpif
, const char *devname
,
346 struct odp_port
*port
)
348 int error
= dpif
->class->port_query_by_name(dpif
, devname
, port
);
350 VLOG_DBG_RL(&dpmsg_rl
, "%s: device %s is on port %"PRIu16
,
351 dpif_name(dpif
), devname
, port
->port
);
353 memset(port
, 0, sizeof *port
);
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
));
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. */
369 dpif_port_get_name(struct dpif
*dpif
, uint16_t port_no
,
370 char *name
, size_t name_size
)
372 struct odp_port port
;
375 assert(name_size
> 0);
377 error
= dpif_port_query_by_number(dpif
, port_no
, &port
);
379 ovs_strlcpy(name
, port
.devname
, name_size
);
386 /* Obtains a list of all the ports in 'dpif'.
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
393 * On failure, returns a positive errno value and sets '*portsp' to NULL and
394 * '*n_portsp' to 0. */
396 dpif_port_list(const struct dpif
*dpif
,
397 struct odp_port
**portsp
, size_t *n_portsp
)
399 struct odp_port
*ports
;
404 struct odp_stats stats
;
407 error
= dpif_get_dp_stats(dpif
, &stats
);
412 ports
= xcalloc(stats
.n_ports
, sizeof *ports
);
413 retval
= dpif
->class->port_list(dpif
, ports
, stats
.n_ports
);
419 } else if (retval
<= stats
.n_ports
) {
425 /* Soft error: port count increased behind our back. Try again. */
438 log_operation(dpif
, "port_list", error
);
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:
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.
449 * - Returns ENOBUFS and sets '*devnamep' to NULL.
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.
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
459 dpif_port_poll(const struct dpif
*dpif
, char **devnamep
)
461 int error
= dpif
->class->port_poll(dpif
, devnamep
);
468 /* Arranges for the poll loop to wake up when port_poll(dpif) will return a
469 * value other than EAGAIN. */
471 dpif_port_poll_wait(const struct dpif
*dpif
)
473 dpif
->class->port_poll_wait(dpif
);
476 /* Retrieves a list of the port numbers in port group 'group' in 'dpif'.
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().
483 * On failure, returns a positive errno value and sets '*ports' to NULL and
484 * '*n_ports' to 0. */
486 dpif_port_group_get(const struct dpif
*dpif
, uint16_t group
,
487 uint16_t **ports
, size_t *n_ports
)
494 int retval
= dpif
->class->port_group_get(dpif
, group
,
503 } else if (retval
<= *n_ports
) {
509 /* Soft error: there were more ports than we expected in the
510 * group. Try again. */
512 *ports
= xcalloc(retval
, sizeof **ports
);
516 log_operation(dpif
, "port_group_get", error
);
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.
524 * Behavior is undefined if the values in ports[] are not unique. */
526 dpif_port_group_set(struct dpif
*dpif
, uint16_t group
,
527 const uint16_t ports
[], size_t n_ports
)
531 COVERAGE_INC(dpif_port_group_set
);
533 error
= dpif
->class->port_group_set(dpif
, group
, ports
, n_ports
);
534 log_operation(dpif
, "port_group_set", error
);
538 /* Deletes all flows from 'dpif'. Returns 0 if successful, otherwise a
539 * positive errno value. */
541 dpif_flow_flush(struct dpif
*dpif
)
545 COVERAGE_INC(dpif_flow_flush
);
547 error
= dpif
->class->flow_flush(dpif
);
548 log_operation(dpif
, "flow_flush", error
);
552 /* Queries 'dpif' for a flow entry matching 'flow->key'.
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.
563 * If no flow matching 'flow->key' exists in 'dpif', returns ENOENT. On other
564 * failure, returns a positive errno value. */
566 dpif_flow_get(const struct dpif
*dpif
, struct odp_flow
*flow
)
570 COVERAGE_INC(dpif_flow_get
);
572 check_rw_odp_flow(flow
);
573 error
= dpif
->class->flow_get(dpif
, flow
, 1);
575 error
= flow
->stats
.error
;
577 if (should_log_flow_message(error
)) {
578 log_flow_operation(dpif
, "flow_get", error
, flow
);
583 /* For each flow 'flow' in the 'n' flows in 'flows':
585 * - If a flow matching 'flow->key' exists in 'dpif':
587 * Stores 0 into 'flow->stats.error' and stores statistics for the flow
588 * into 'flow->stats'.
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.
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.
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.
611 dpif_flow_get_multiple(const struct dpif
*dpif
,
612 struct odp_flow flows
[], size_t n
)
617 COVERAGE_ADD(dpif_flow_get
, n
);
619 for (i
= 0; i
< n
; i
++) {
620 check_rw_odp_flow(&flows
[i
]);
623 error
= dpif
->class->flow_get(dpif
, flows
, n
);
624 log_operation(dpif
, "flow_get_multiple", error
);
628 /* Adds or modifies a flow in 'dpif' as specified in 'put':
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
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.
642 * Returns 0 if successful, otherwise a positive errno value.
645 dpif_flow_put(struct dpif
*dpif
, struct odp_flow_put
*put
)
649 COVERAGE_INC(dpif_flow_put
);
651 error
= dpif
->class->flow_put(dpif
, put
);
652 if (should_log_flow_message(error
)) {
653 log_flow_put(dpif
, error
, put
);
658 /* Deletes a flow matching 'flow->key' from 'dpif' or returns ENOENT if 'dpif'
659 * does not contain such a flow.
661 * If successful, updates 'flow->stats', 'flow->n_actions', and 'flow->actions'
662 * as described for dpif_flow_get(). */
664 dpif_flow_del(struct dpif
*dpif
, struct odp_flow
*flow
)
668 COVERAGE_INC(dpif_flow_del
);
670 check_rw_odp_flow(flow
);
671 memset(&flow
->stats
, 0, sizeof flow
->stats
);
673 error
= dpif
->class->flow_del(dpif
, flow
);
674 if (should_log_flow_message(error
)) {
675 log_flow_operation(dpif
, "delete flow", error
, flow
);
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
687 dpif_flow_list(const struct dpif
*dpif
, struct odp_flow flows
[], size_t n
,
693 COVERAGE_INC(dpif_flow_query_list
);
694 if (RUNNING_ON_VALGRIND
) {
695 memset(flows
, 0, n
* sizeof *flows
);
697 for (i
= 0; i
< n
; i
++) {
698 flows
[i
].actions
= NULL
;
699 flows
[i
].n_actions
= 0;
702 retval
= dpif
->class->flow_list(dpif
, flows
, n
);
705 VLOG_WARN_RL(&error_rl
, "%s: flow list failed (%s)",
706 dpif_name(dpif
), strerror(-retval
));
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
);
717 /* Retrieves all of the flows in 'dpif'.
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
725 * On failure, returns a positive errno value and sets '*flowsp' to NULL and
728 dpif_flow_list_all(const struct dpif
*dpif
,
729 struct odp_flow
**flowsp
, size_t *np
)
731 struct odp_stats stats
;
732 struct odp_flow
*flows
;
739 error
= dpif_get_dp_stats(dpif
, &stats
);
744 flows
= xmalloc(sizeof *flows
* stats
.n_flows
);
745 error
= dpif_flow_list(dpif
, flows
, stats
.n_flows
, &n_flows
);
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
);
761 /* Causes 'dpif' to perform the 'n_actions' actions in 'actions' on the
762 * Ethernet frame specified in 'packet'.
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.
769 * Returns 0 if successful, otherwise a positive errno value. */
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
)
777 COVERAGE_INC(dpif_execute
);
779 error
= dpif
->class->execute(dpif
, in_port
, actions
, n_actions
, buf
);
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
);
790 ds_put_format(&ds
, " failed (%s)", strerror(error
));
792 ds_put_format(&ds
, " on packet %s", packet
);
793 vlog(THIS_MODULE
, error
? VLL_WARN
: VLL_DBG
, "%s", ds_cstr(&ds
));
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. */
804 dpif_recv_get_mask(const struct dpif
*dpif
, int *listen_mask
)
806 int error
= dpif
->class->recv_get_mask(dpif
, listen_mask
);
810 log_operation(dpif
, "recv_get_mask", error
);
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. */
818 dpif_recv_set_mask(struct dpif
*dpif
, int listen_mask
)
820 int error
= dpif
->class->recv_set_mask(dpif
, listen_mask
);
821 log_operation(dpif
, "recv_set_mask", error
);
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).
831 * Returns 0 if successful, otherwise a positive errno value. Returns EAGAIN
832 * if no message is immediately available. */
834 dpif_recv(struct dpif
*dpif
, struct ofpbuf
**packetp
)
836 int error
= dpif
->class->recv(dpif
, packetp
);
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"
849 payload_len
, msg
->port
, s
);
858 /* Discards all messages that would otherwise be received by dpif_recv() on
859 * 'dpif'. Returns 0 if successful, otherwise a positive errno value. */
861 dpif_recv_purge(struct dpif
*dpif
)
863 struct odp_stats stats
;
867 COVERAGE_INC(dpif_purge
);
869 error
= dpif_get_dp_stats(dpif
, &stats
);
874 for (i
= 0; i
< stats
.max_miss_queue
+ stats
.max_action_queue
; i
++) {
876 error
= dpif_recv(dpif
, &buf
);
878 return error
== EAGAIN
? 0 : error
;
885 /* Arranges for the poll loop to wake up when 'dpif' has a message queued to be
886 * received with dpif_recv(). */
888 dpif_recv_wait(struct dpif
*dpif
)
890 dpif
->class->recv_wait(dpif
);
893 /* Obtains the NetFlow engine type and engine ID for 'dpif' into '*engine_type'
894 * and '*engine_id', respectively. */
896 dpif_get_netflow_ids(const struct dpif
*dpif
,
897 uint8_t *engine_type
, uint8_t *engine_id
)
899 *engine_type
= dpif
->netflow_engine_type
;
900 *engine_id
= dpif
->netflow_engine_id
;
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
)
908 dpif
->name
= xstrdup(name
);
909 dpif
->netflow_engine_type
= netflow_engine_type
;
910 dpif
->netflow_engine_id
= netflow_engine_id
;
914 log_operation(const struct dpif
*dpif
, const char *operation
, int error
)
917 VLOG_DBG_RL(&dpmsg_rl
, "%s: %s success", dpif_name(dpif
), operation
);
919 VLOG_WARN_RL(&error_rl
, "%s: %s failed (%s)",
920 dpif_name(dpif
), operation
, strerror(error
));
924 static enum vlog_level
925 flow_message_log_level(int error
)
927 return error
? VLL_WARN
: VLL_DBG
;
931 should_log_flow_message(int error
)
933 return !vlog_should_drop(THIS_MODULE
, flow_message_log_level(error
),
934 error
? &error_rl
: &dpmsg_rl
);
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
)
942 struct ds ds
= DS_EMPTY_INITIALIZER
;
943 ds_put_format(&ds
, "%s: ", dpif_name(dpif
));
945 ds_put_cstr(&ds
, "failed to ");
947 ds_put_format(&ds
, "%s ", operation
);
949 ds_put_format(&ds
, "(%s) ", strerror(error
));
951 flow_format(&ds
, flow
);
953 ds_put_cstr(&ds
, ", ");
954 format_odp_flow_stats(&ds
, stats
);
956 if (actions
|| n_actions
) {
957 ds_put_cstr(&ds
, ", actions:");
958 format_odp_actions(&ds
, actions
, n_actions
);
960 vlog(THIS_MODULE
, flow_message_log_level(error
), "%s", ds_cstr(&ds
));
965 log_flow_operation(const struct dpif
*dpif
, const char *operation
, int error
,
966 struct odp_flow
*flow
)
971 log_flow_message(dpif
, error
, operation
, &flow
->key
,
972 !error
? &flow
->stats
: NULL
,
973 flow
->actions
, flow
->n_actions
);
977 log_flow_put(struct dpif
*dpif
, int error
, const struct odp_flow_put
*put
)
979 enum { ODPPF_ALL
= ODPPF_CREATE
| ODPPF_MODIFY
| ODPPF_ZERO_STATS
};
983 ds_put_cstr(&s
, "put");
984 if (put
->flags
& ODPPF_CREATE
) {
985 ds_put_cstr(&s
, "[create]");
987 if (put
->flags
& ODPPF_MODIFY
) {
988 ds_put_cstr(&s
, "[modify]");
990 if (put
->flags
& ODPPF_ZERO_STATS
) {
991 ds_put_cstr(&s
, "[zero]");
993 if (put
->flags
& ~ODPPF_ALL
) {
994 ds_put_format(&s
, "[%x]", put
->flags
& ~ODPPF_ALL
);
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
);
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.
1007 * This function attempts to combat the problem by:
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).
1012 * - Storing a distinctive value that is likely to cause an
1013 * easy-to-identify error later if it is dereferenced, etc.
1015 * - Triggering a warning on uninitialized memory from Valgrind if
1016 * "actions" or "n_actions" was not initialized.
1019 check_rw_odp_flow(struct odp_flow
*flow
)
1021 if (flow
->n_actions
) {
1022 memset(&flow
->actions
[0], 0xcc, sizeof flow
->actions
[0]);