2
0
mirror of https://github.com/openvswitch/ovs synced 2025-08-31 14:25:26 +00:00

ofp-util: Simplify iteration through OpenFlow actions.

The existing actions_first() and actions_next() iterator functions are not
much like the other iteration constructs found throughout the Open vSwitch
tree.  Also, they only work with actions that have already been validated,
so there are cases where they cannot be used.

This commit adds new macros for iterating through OpenFlow actions, one
for actions that have been validated and one for actions that have not, and
adapts the existing users.  The following commit will further refine action
parsing and add more users.
This commit is contained in:
Ben Pfaff
2011-06-30 10:04:09 -07:00
parent 8f93e93c80
commit b4b8c7812b
7 changed files with 66 additions and 95 deletions

View File

@@ -277,13 +277,36 @@ struct ofpbuf *make_echo_reply(const struct ofp_header *rq);
#define OFP_ACTION_ALIGN 8 /* Alignment of ofp_actions. */
struct actions_iterator {
const union ofp_action *pos, *end;
};
const union ofp_action *actions_first(struct actions_iterator *,
const union ofp_action *,
size_t n_actions);
const union ofp_action *actions_next(struct actions_iterator *);
static inline union ofp_action *
ofputil_action_next(const union ofp_action *a)
{
return (void *) ((uint8_t *) a + ntohs(a->header.len));
}
static inline bool
ofputil_action_is_valid(const union ofp_action *a, size_t n_actions)
{
uint16_t len = ntohs(a->header.len);
return (!(len % OFP_ACTION_ALIGN)
&& len >= sizeof *a
&& len / sizeof *a <= n_actions);
}
/* This macro is careful to check for actions with bad lengths. */
#define OFPUTIL_ACTION_FOR_EACH(ITER, LEFT, ACTIONS, N_ACTIONS) \
for ((ITER) = (ACTIONS), (LEFT) = (N_ACTIONS); \
(LEFT) > 0 && ofputil_action_is_valid(ITER, LEFT); \
((LEFT) -= ntohs((ITER)->header.len) / sizeof(union ofp_action), \
(ITER) = ofputil_action_next(ITER)))
/* This macro does not check for actions with bad lengths. It should only be
* used with actions from trusted sources or with actions that have already
* been validated (e.g. with OFPUTIL_ACTION_FOR_EACH). */
#define OFPUTIL_ACTION_FOR_EACH_UNSAFE(ITER, LEFT, ACTIONS, N_ACTIONS) \
for ((ITER) = (ACTIONS), (LEFT) = (N_ACTIONS); \
(LEFT) > 0; \
((LEFT) -= ntohs((ITER)->header.len) / sizeof(union ofp_action), \
(ITER) = ofputil_action_next(ITER)))
int validate_actions(const union ofp_action *, size_t n_actions,
const struct flow *, int max_ports);