Skip to content

Commit

Permalink
ofp-actions: Add action bitmap abstraction.
Browse files Browse the repository at this point in the history
Until now, sets of actions have been abstracted separately outside
ofp-actions, as enum ofputil_action_bitmap.  Drawing sets of actions into
ofp-actions, as done in this commit, makes for a better overall
abstraction of actions, with better consistency.

A big part of this commit is shifting from using ofp12_table_stats as if
it were an abstraction for OpenFlow table stats, toward using a new
struct ofputil_table_stats, which is what we generally do with other
OpenFlow structures and fits better with the rest of the code.

Signed-off-by: Ben Pfaff <[email protected]>
Acked-by: Jarno Rajahalme <[email protected]>
  • Loading branch information
blp committed Aug 11, 2014
1 parent 6778253 commit 08d1e23
Show file tree
Hide file tree
Showing 11 changed files with 512 additions and 435 deletions.
12 changes: 10 additions & 2 deletions include/openflow/openflow-1.2.h
Original file line number Diff line number Diff line change
Expand Up @@ -306,12 +306,20 @@ struct ofp12_table_stats {
};
OFP_ASSERT(sizeof(struct ofp12_table_stats) == 128);

/* Number of types of groups supported by ofp12_group_features_stats. */
#define OFPGT12_N_TYPES 4

/* Body of reply to OFPST12_GROUP_FEATURES request. Group features. */
struct ofp12_group_features_stats {
ovs_be32 types; /* Bitmap of OFPGT11_* values supported. */
ovs_be32 capabilities; /* Bitmap of OFPGFC12_* capability supported. */
ovs_be32 max_groups[4]; /* Maximum number of groups for each type. */
ovs_be32 actions[4]; /* Bitmaps of OFPAT_* that are supported. */

/* Each element in the following arrays corresponds to the group type with
* the same number, e.g. max_groups[0] is the maximum number of OFPGT11_ALL
* groups, actions[2] is the actions supported by OFPGT11_INDIRECT
* groups. */
ovs_be32 max_groups[OFPGT12_N_TYPES]; /* Max number of groups. */
ovs_be32 actions[OFPGT12_N_TYPES]; /* Bitmaps of supported OFPAT_*. */
};
OFP_ASSERT(sizeof(struct ofp12_group_features_stats) == 40);

Expand Down
160 changes: 160 additions & 0 deletions lib/ofp-actions.c
Original file line number Diff line number Diff line change
Expand Up @@ -3091,6 +3091,155 @@ ofpacts_put_openflow_instructions(const struct ofpact ofpacts[],
}
}

/* Sets of supported actions. */

/* Two-way translation between OVS's internal "OFPACT_*" representation of
* actions and the "OFPAT_*" representation used in some OpenFlow version.
* (OFPAT_* numbering varies from one OpenFlow version to another, so a given
* instance is specific to one OpenFlow version.) */
struct ofpact_map {
enum ofpact_type ofpact; /* Internal name for action type. */
int ofpat; /* OFPAT_* number from OpenFlow spec. */
};

static const struct ofpact_map *
get_ofpact_map(enum ofp_version version)
{
/* OpenFlow 1.0 actions. */
static const struct ofpact_map of10[] = {
{ OFPACT_OUTPUT, 0 },
{ OFPACT_SET_VLAN_VID, 1 },
{ OFPACT_SET_VLAN_PCP, 2 },
{ OFPACT_STRIP_VLAN, 3 },
{ OFPACT_SET_ETH_SRC, 4 },
{ OFPACT_SET_ETH_DST, 5 },
{ OFPACT_SET_IPV4_SRC, 6 },
{ OFPACT_SET_IPV4_DST, 7 },
{ OFPACT_SET_IP_DSCP, 8 },
{ OFPACT_SET_L4_SRC_PORT, 9 },
{ OFPACT_SET_L4_DST_PORT, 10 },
{ OFPACT_ENQUEUE, 11 },
{ 0, -1 },
};

/* OpenFlow 1.1 actions. */
static const struct ofpact_map of11[] = {
{ OFPACT_OUTPUT, 0 },
{ OFPACT_SET_VLAN_VID, 1 },
{ OFPACT_SET_VLAN_PCP, 2 },
{ OFPACT_SET_ETH_SRC, 3 },
{ OFPACT_SET_ETH_DST, 4 },
{ OFPACT_SET_IPV4_SRC, 5 },
{ OFPACT_SET_IPV4_DST, 6 },
{ OFPACT_SET_IP_DSCP, 7 },
{ OFPACT_SET_IP_ECN, 8 },
{ OFPACT_SET_L4_SRC_PORT, 9 },
{ OFPACT_SET_L4_DST_PORT, 10 },
/* OFPAT_COPY_TTL_OUT (11) not supported. */
/* OFPAT_COPY_TTL_IN (12) not supported. */
{ OFPACT_SET_MPLS_LABEL, 13 },
{ OFPACT_SET_MPLS_TC, 14 },
{ OFPACT_SET_MPLS_TTL, 15 },
{ OFPACT_DEC_MPLS_TTL, 16 },
{ OFPACT_PUSH_VLAN, 17 },
{ OFPACT_STRIP_VLAN, 18 },
{ OFPACT_PUSH_MPLS, 19 },
{ OFPACT_POP_MPLS, 20 },
{ OFPACT_SET_QUEUE, 21 },
{ OFPACT_GROUP, 22 },
{ OFPACT_SET_IP_TTL, 23 },
{ OFPACT_DEC_TTL, 24 },
{ 0, -1 },
};

/* OpenFlow 1.2, 1.3, and 1.4 actions. */
static const struct ofpact_map of12[] = {
{ OFPACT_OUTPUT, 0 },
/* OFPAT_COPY_TTL_OUT (11) not supported. */
/* OFPAT_COPY_TTL_IN (12) not supported. */
{ OFPACT_SET_MPLS_TTL, 15 },
{ OFPACT_DEC_MPLS_TTL, 16 },
{ OFPACT_PUSH_VLAN, 17 },
{ OFPACT_STRIP_VLAN, 18 },
{ OFPACT_PUSH_MPLS, 19 },
{ OFPACT_POP_MPLS, 20 },
{ OFPACT_SET_QUEUE, 21 },
{ OFPACT_GROUP, 22 },
{ OFPACT_SET_IP_TTL, 23 },
{ OFPACT_DEC_TTL, 24 },
{ OFPACT_SET_FIELD, 25 },
/* OF1.3+ OFPAT_PUSH_PBB (26) not supported. */
/* OF1.3+ OFPAT_POP_PBB (27) not supported. */
{ 0, -1 },
};

switch (version) {
case OFP10_VERSION:
return of10;

case OFP11_VERSION:
return of11;

case OFP12_VERSION:
case OFP13_VERSION:
case OFP14_VERSION:
case OFP15_VERSION:
default:
return of12;
}
}

/* Converts 'ofpacts_bitmap', a bitmap whose bits correspond to OFPACT_*
* values, into a bitmap of actions suitable for OpenFlow 'version', and
* returns the result. */
ovs_be32
ofpact_bitmap_to_openflow(uint64_t ofpacts_bitmap, enum ofp_version version)
{
uint32_t openflow_bitmap = 0;
const struct ofpact_map *x;

for (x = get_ofpact_map(version); x->ofpat >= 0; x++) {
if (ofpacts_bitmap & (UINT64_C(1) << x->ofpact)) {
openflow_bitmap |= 1u << x->ofpat;
}
}
return htonl(openflow_bitmap);
}

/* Converts 'ofpat_bitmap', a bitmap of actions from an OpenFlow message with
* the given 'version' into a bitmap whose bits correspond to OFPACT_* values,
* and returns the result. */
uint64_t
ofpact_bitmap_from_openflow(ovs_be32 ofpat_bitmap, enum ofp_version version)
{
uint64_t ofpact_bitmap = 0;
const struct ofpact_map *x;

for (x = get_ofpact_map(version); x->ofpat >= 0; x++) {
if (ofpat_bitmap & htonl(1u << x->ofpat)) {
ofpact_bitmap |= UINT64_C(1) << x->ofpact;
}
}
return ofpact_bitmap;
}

/* Appends to 's' a string representation of the set of OFPACT_* represented
* by 'ofpacts_bitmap'. */
void
ofpact_bitmap_format(uint64_t ofpacts_bitmap, struct ds *s)
{
if (!ofpacts_bitmap) {
ds_put_cstr(s, "<none>");
} else {
while (ofpacts_bitmap) {
ds_put_format(s, "%s ",
ofpact_name(rightmost_1bit_idx(ofpacts_bitmap)));
ofpacts_bitmap = zero_rightmost_1bit(ofpacts_bitmap);
}
ds_chomp(s, ' ');
}
}

/* Returns true if 'action' outputs to 'port', false otherwise. */
static bool
ofpact_outputs_to_port(const struct ofpact *ofpact, ofp_port_t port)
Expand Down Expand Up @@ -3648,3 +3797,14 @@ ofpact_pad(struct ofpbuf *ofpacts)
ofpbuf_put_zeros(ofpacts, pad);
}
}

const char *
ofpact_name(enum ofpact_type type)
{
switch (type) {
#define OFPACT(ENUM, STRUCT, MEMBER, NAME) case OFPACT_##ENUM: return NAME;
OFPACTS
#undef OFPACT
}
return "<unknown>";
}
148 changes: 78 additions & 70 deletions lib/ofp-actions.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* This macro is used directly only internally by this header, but the list is
* still of interest to developers.
*
* Each DEFINE_OFPACT invocation has the following parameters:
* Each OFPACT invocation has the following parameters:
*
* 1. <ENUM>, used below in the enum definition of OFPACT_<ENUM>, and
* elsewhere.
Expand All @@ -48,81 +48,83 @@
*
* - If "struct <STRUCT>" is variable-length, it must be the name of the
* flexible array member.
*
* 4. <NAME>, a quoted string that gives the name of the action, for use in
* parsing actions from text.
*/
#define OFPACTS \
/* Output. */ \
DEFINE_OFPACT(OUTPUT, ofpact_output, ofpact) \
DEFINE_OFPACT(GROUP, ofpact_group, ofpact) \
DEFINE_OFPACT(CONTROLLER, ofpact_controller, ofpact) \
DEFINE_OFPACT(ENQUEUE, ofpact_enqueue, ofpact) \
DEFINE_OFPACT(OUTPUT_REG, ofpact_output_reg, ofpact) \
DEFINE_OFPACT(BUNDLE, ofpact_bundle, slaves) \
\
/* Header changes. */ \
DEFINE_OFPACT(SET_FIELD, ofpact_set_field, ofpact) \
DEFINE_OFPACT(SET_VLAN_VID, ofpact_vlan_vid, ofpact) \
DEFINE_OFPACT(SET_VLAN_PCP, ofpact_vlan_pcp, ofpact) \
DEFINE_OFPACT(STRIP_VLAN, ofpact_null, ofpact) \
DEFINE_OFPACT(PUSH_VLAN, ofpact_null, ofpact) \
DEFINE_OFPACT(SET_ETH_SRC, ofpact_mac, ofpact) \
DEFINE_OFPACT(SET_ETH_DST, ofpact_mac, ofpact) \
DEFINE_OFPACT(SET_IPV4_SRC, ofpact_ipv4, ofpact) \
DEFINE_OFPACT(SET_IPV4_DST, ofpact_ipv4, ofpact) \
DEFINE_OFPACT(SET_IP_DSCP, ofpact_dscp, ofpact) \
DEFINE_OFPACT(SET_IP_ECN, ofpact_ecn, ofpact) \
DEFINE_OFPACT(SET_IP_TTL, ofpact_ip_ttl, ofpact) \
DEFINE_OFPACT(SET_L4_SRC_PORT, ofpact_l4_port, ofpact) \
DEFINE_OFPACT(SET_L4_DST_PORT, ofpact_l4_port, ofpact) \
DEFINE_OFPACT(REG_MOVE, ofpact_reg_move, ofpact) \
DEFINE_OFPACT(REG_LOAD, ofpact_reg_load, ofpact) \
DEFINE_OFPACT(STACK_PUSH, ofpact_stack, ofpact) \
DEFINE_OFPACT(STACK_POP, ofpact_stack, ofpact) \
DEFINE_OFPACT(DEC_TTL, ofpact_cnt_ids, cnt_ids) \
DEFINE_OFPACT(SET_MPLS_LABEL, ofpact_mpls_label, ofpact) \
DEFINE_OFPACT(SET_MPLS_TC, ofpact_mpls_tc, ofpact) \
DEFINE_OFPACT(SET_MPLS_TTL, ofpact_mpls_ttl, ofpact) \
DEFINE_OFPACT(DEC_MPLS_TTL, ofpact_null, ofpact) \
DEFINE_OFPACT(PUSH_MPLS, ofpact_push_mpls, ofpact) \
DEFINE_OFPACT(POP_MPLS, ofpact_pop_mpls, ofpact) \
\
/* Metadata. */ \
DEFINE_OFPACT(SET_TUNNEL, ofpact_tunnel, ofpact) \
DEFINE_OFPACT(SET_QUEUE, ofpact_queue, ofpact) \
DEFINE_OFPACT(POP_QUEUE, ofpact_null, ofpact) \
DEFINE_OFPACT(FIN_TIMEOUT, ofpact_fin_timeout, ofpact) \
\
/* Flow table interaction. */ \
DEFINE_OFPACT(RESUBMIT, ofpact_resubmit, ofpact) \
DEFINE_OFPACT(LEARN, ofpact_learn, specs) \
\
/* Arithmetic. */ \
DEFINE_OFPACT(MULTIPATH, ofpact_multipath, ofpact) \
\
/* Other. */ \
DEFINE_OFPACT(NOTE, ofpact_note, data) \
DEFINE_OFPACT(EXIT, ofpact_null, ofpact) \
DEFINE_OFPACT(SAMPLE, ofpact_sample, ofpact) \
\
/* Instructions */ \
DEFINE_OFPACT(METER, ofpact_meter, ofpact) \
DEFINE_OFPACT(CLEAR_ACTIONS, ofpact_null, ofpact) \
DEFINE_OFPACT(WRITE_ACTIONS, ofpact_nest, ofpact) \
DEFINE_OFPACT(WRITE_METADATA, ofpact_metadata, ofpact) \
DEFINE_OFPACT(GOTO_TABLE, ofpact_goto_table, ofpact)
#define OFPACTS \
/* Output. */ \
OFPACT(OUTPUT, ofpact_output, ofpact, "output") \
OFPACT(GROUP, ofpact_group, ofpact, "group") \
OFPACT(CONTROLLER, ofpact_controller, ofpact, "controller") \
OFPACT(ENQUEUE, ofpact_enqueue, ofpact, "enqueue") \
OFPACT(OUTPUT_REG, ofpact_output_reg, ofpact, "output_reg") \
OFPACT(BUNDLE, ofpact_bundle, slaves, "bundle") \
\
/* Header changes. */ \
OFPACT(SET_FIELD, ofpact_set_field, ofpact, "set_field") \
OFPACT(SET_VLAN_VID, ofpact_vlan_vid, ofpact, "set_vlan_vid") \
OFPACT(SET_VLAN_PCP, ofpact_vlan_pcp, ofpact, "set_vlan_pcp") \
OFPACT(STRIP_VLAN, ofpact_null, ofpact, "strip_vlan") \
OFPACT(PUSH_VLAN, ofpact_null, ofpact, "push_vlan") \
OFPACT(SET_ETH_SRC, ofpact_mac, ofpact, "mod_dl_src") \
OFPACT(SET_ETH_DST, ofpact_mac, ofpact, "mod_dl_dst") \
OFPACT(SET_IPV4_SRC, ofpact_ipv4, ofpact, "mod_nw_src") \
OFPACT(SET_IPV4_DST, ofpact_ipv4, ofpact, "mod_nw_dst") \
OFPACT(SET_IP_DSCP, ofpact_dscp, ofpact, "mod_nw_tos") \
OFPACT(SET_IP_ECN, ofpact_ecn, ofpact, "mod_nw_ecn") \
OFPACT(SET_IP_TTL, ofpact_ip_ttl, ofpact, "mod_nw_ttl") \
OFPACT(SET_L4_SRC_PORT, ofpact_l4_port, ofpact, "mod_tp_src") \
OFPACT(SET_L4_DST_PORT, ofpact_l4_port, ofpact, "mod_tp_dst") \
OFPACT(REG_MOVE, ofpact_reg_move, ofpact, "move") \
OFPACT(REG_LOAD, ofpact_reg_load, ofpact, "load") \
OFPACT(STACK_PUSH, ofpact_stack, ofpact, "push") \
OFPACT(STACK_POP, ofpact_stack, ofpact, "pop") \
OFPACT(DEC_TTL, ofpact_cnt_ids, cnt_ids, "dec_ttl") \
OFPACT(SET_MPLS_LABEL, ofpact_mpls_label, ofpact, "set_mpls_label") \
OFPACT(SET_MPLS_TC, ofpact_mpls_tc, ofpact, "set_mpls_tc") \
OFPACT(SET_MPLS_TTL, ofpact_mpls_ttl, ofpact, "set_mpls_ttl") \
OFPACT(DEC_MPLS_TTL, ofpact_null, ofpact, "dec_mpls_ttl") \
OFPACT(PUSH_MPLS, ofpact_push_mpls, ofpact, "push_mpls") \
OFPACT(POP_MPLS, ofpact_pop_mpls, ofpact, "pop_mpls") \
\
/* Metadata. */ \
OFPACT(SET_TUNNEL, ofpact_tunnel, ofpact, "set_tunnel") \
OFPACT(SET_QUEUE, ofpact_queue, ofpact, "set_queue") \
OFPACT(POP_QUEUE, ofpact_null, ofpact, "pop_queue") \
OFPACT(FIN_TIMEOUT, ofpact_fin_timeout, ofpact, "fin_timeout") \
\
/* Flow table interaction. */ \
OFPACT(RESUBMIT, ofpact_resubmit, ofpact, "resubmit") \
OFPACT(LEARN, ofpact_learn, specs, "learn") \
\
/* Arithmetic. */ \
OFPACT(MULTIPATH, ofpact_multipath, ofpact, "multipath") \
\
/* Other. */ \
OFPACT(NOTE, ofpact_note, data, "note") \
OFPACT(EXIT, ofpact_null, ofpact, "exit") \
OFPACT(SAMPLE, ofpact_sample, ofpact, "sample") \
\
/* Instructions. */ \
OFPACT(METER, ofpact_meter, ofpact, "meter") \
OFPACT(CLEAR_ACTIONS, ofpact_null, ofpact, "clear_actions") \
OFPACT(WRITE_ACTIONS, ofpact_nest, ofpact, "write_actions") \
OFPACT(WRITE_METADATA, ofpact_metadata, ofpact, "write_metadata") \
OFPACT(GOTO_TABLE, ofpact_goto_table, ofpact, "goto_table")

/* enum ofpact_type, with a member OFPACT_<ENUM> for each action. */
enum OVS_PACKED_ENUM ofpact_type {
#define DEFINE_OFPACT(ENUM, STRUCT, MEMBER) OFPACT_##ENUM,
#define OFPACT(ENUM, STRUCT, MEMBER, NAME) OFPACT_##ENUM,
OFPACTS
#undef DEFINE_OFPACT
#undef OFPACT
};

/* N_OFPACTS, the number of values of "enum ofpact_type". */
/* Define N_OFPACTS to the number of types of ofpacts. */
enum {
N_OFPACTS =
#define DEFINE_OFPACT(ENUM, STRUCT, MEMBER) + 1
OFPACTS
#undef DEFINE_OFPACT
#define OFPACT(ENUM, STRUCT, MEMBER, NAME) + 1
N_OFPACTS = OFPACTS
#undef OFPACT
};

/* Header for an action.
Expand Down Expand Up @@ -611,6 +613,11 @@ void ofpacts_put_openflow_instructions(const struct ofpact[],
struct ofpbuf *openflow,
enum ofp_version ofp_version);

/* Sets of supported actions. */
ovs_be32 ofpact_bitmap_to_openflow(uint64_t ofpacts_bitmap, enum ofp_version);
uint64_t ofpact_bitmap_from_openflow(ovs_be32 ofpat_bitmap, enum ofp_version);
void ofpact_bitmap_format(uint64_t ofpacts_bitmap, struct ds *);

/* Working with ofpacts. */
bool ofpacts_output_to_port(const struct ofpact[], size_t ofpacts_len,
ofp_port_t port);
Expand All @@ -624,6 +631,7 @@ uint32_t ofpacts_get_meter(const struct ofpact[], size_t ofpacts_len);
*
* (For parsing ofpacts, see ofp-parse.h.) */
void ofpacts_format(const struct ofpact[], size_t ofpacts_len, struct ds *);
const char *ofpact_name(enum ofpact_type);

/* Internal use by the helpers below. */
void ofpact_init(struct ofpact *, enum ofpact_type, size_t len);
Expand Down Expand Up @@ -667,7 +675,7 @@ void *ofpact_put(struct ofpbuf *, enum ofpact_type, size_t len);
* An integer constant, the value of OFPACT_<ENUM>_RAW_SIZE rounded up to a
* multiple of OFPACT_ALIGNTO.
*/
#define DEFINE_OFPACT(ENUM, STRUCT, MEMBER) \
#define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
BUILD_ASSERT_DECL(offsetof(struct STRUCT, ofpact) == 0); \
\
enum { OFPACT_##ENUM##_RAW_SIZE \
Expand Down Expand Up @@ -699,7 +707,7 @@ void *ofpact_put(struct ofpbuf *, enum ofpact_type, size_t len);
OFPACT_##ENUM##_RAW_SIZE); \
}
OFPACTS
#undef DEFINE_OFPACT
#undef OFPACT

/* Functions to use after adding ofpacts to a buffer. */
void ofpact_update_len(struct ofpbuf *, struct ofpact *);
Expand Down
Loading

0 comments on commit 08d1e23

Please sign in to comment.