From e23ae585275dfa76de50b698754b2066cc944e01 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Wed, 17 Aug 2011 11:01:17 -0700 Subject: [PATCH] ofp-util: Further abstract definitions of action properties. This commit primarily moves the OFPAT_ACTION and NXAST_ACTION invocations into a new file ofp-util.def. This allows multiple places in the source to use them. This commit also adds a new function ofputil_action_code_from_name(). The following commit will add the first user. --- lib/automake.mk | 1 + lib/ofp-util.c | 76 ++++++++++++++++++++++++------------------------ lib/ofp-util.def | 37 +++++++++++++++++++++++ lib/ofp-util.h | 76 ++++++++++++++++++++++++++++++------------------ 4 files changed, 123 insertions(+), 67 deletions(-) create mode 100644 lib/ofp-util.def diff --git a/lib/automake.mk b/lib/automake.mk index de2b635f1..01eeb8553 100644 --- a/lib/automake.mk +++ b/lib/automake.mk @@ -95,6 +95,7 @@ lib_libopenvswitch_a_SOURCES = \ lib/ofp-print.c \ lib/ofp-print.h \ lib/ofp-util.c \ + lib/ofp-util.def \ lib/ofp-util.h \ lib/ofpbuf.c \ lib/ofpbuf.h \ diff --git a/lib/ofp-util.c b/lib/ofp-util.c index bcb1b9ef6..00d9df496 100644 --- a/lib/ofp-util.c +++ b/lib/ofp-util.c @@ -2187,26 +2187,16 @@ ofputil_decode_ofpat_action(const union ofp_action *a) enum ofp_action_type type = ntohs(a->type); switch (type) { -#define OFPAT_ACTION(ENUM, TYPE) \ +#define OFPAT_ACTION(ENUM, STRUCT, NAME) \ case ENUM: { \ static const struct ofputil_action action = { \ - OFPUTIL_##ENUM, sizeof(TYPE), sizeof(TYPE) \ + OFPUTIL_##ENUM, \ + sizeof(struct STRUCT), \ + sizeof(struct STRUCT) \ }; \ return &action; \ } - OFPAT_ACTION(OFPAT_OUTPUT, struct ofp_action_output); - OFPAT_ACTION(OFPAT_SET_VLAN_VID, struct ofp_action_vlan_vid); - OFPAT_ACTION(OFPAT_SET_VLAN_PCP, struct ofp_action_vlan_pcp); - OFPAT_ACTION(OFPAT_STRIP_VLAN, struct ofp_action_header); - OFPAT_ACTION(OFPAT_SET_DL_SRC, struct ofp_action_dl_addr); - OFPAT_ACTION(OFPAT_SET_DL_DST, struct ofp_action_dl_addr); - OFPAT_ACTION(OFPAT_SET_NW_SRC, struct ofp_action_nw_addr); - OFPAT_ACTION(OFPAT_SET_NW_DST, struct ofp_action_nw_addr); - OFPAT_ACTION(OFPAT_SET_NW_TOS, struct ofp_action_nw_tos); - OFPAT_ACTION(OFPAT_SET_TP_SRC, struct ofp_action_tp_port); - OFPAT_ACTION(OFPAT_SET_TP_DST, struct ofp_action_tp_port); - OFPAT_ACTION(OFPAT_ENQUEUE, struct ofp_action_enqueue); -#undef OFPAT_ACTION +#include "ofp-util.def" case OFPAT_VENDOR: default: @@ -2221,30 +2211,16 @@ ofputil_decode_nxast_action(const union ofp_action *a) enum nx_action_subtype subtype = ntohs(nah->subtype); switch (subtype) { -#define NXAST_ACTION(ENUM, TYPE, EXTENSIBLE) \ - case ENUM: { \ - static const struct ofputil_action action = { \ - OFPUTIL_##ENUM, \ - sizeof(TYPE), \ - EXTENSIBLE ? UINT_MAX : sizeof(TYPE) \ - }; \ - return &action; \ +#define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \ + case ENUM: { \ + static const struct ofputil_action action = { \ + OFPUTIL_##ENUM, \ + sizeof(struct STRUCT), \ + EXTENSIBLE ? UINT_MAX : sizeof(struct STRUCT) \ + }; \ + return &action; \ } - NXAST_ACTION(NXAST_RESUBMIT, struct nx_action_resubmit, false); - NXAST_ACTION(NXAST_SET_TUNNEL, struct nx_action_set_tunnel, false); - NXAST_ACTION(NXAST_SET_QUEUE, struct nx_action_set_queue, false); - NXAST_ACTION(NXAST_POP_QUEUE, struct nx_action_pop_queue, false); - NXAST_ACTION(NXAST_REG_MOVE, struct nx_action_reg_move, false); - NXAST_ACTION(NXAST_REG_LOAD, struct nx_action_reg_load, false); - NXAST_ACTION(NXAST_NOTE, struct nx_action_note, true); - NXAST_ACTION(NXAST_SET_TUNNEL64, struct nx_action_set_tunnel64, false); - NXAST_ACTION(NXAST_MULTIPATH, struct nx_action_multipath, false); - NXAST_ACTION(NXAST_AUTOPATH, struct nx_action_autopath, false); - NXAST_ACTION(NXAST_BUNDLE, struct nx_action_bundle, true); - NXAST_ACTION(NXAST_BUNDLE_LOAD, struct nx_action_bundle, true); - NXAST_ACTION(NXAST_RESUBMIT_TABLE, struct nx_action_resubmit, false); - NXAST_ACTION(NXAST_OUTPUT_REG, struct nx_action_output_reg, false); -#undef NXAST_ACTION +#include "ofp-util.def" case NXAST_SNAT__OBSOLETE: case NXAST_DROP_SPOOFED_ARP__OBSOLETE: @@ -2308,6 +2284,30 @@ ofputil_decode_action_unsafe(const union ofp_action *a) return action->code; } +/* Returns the 'enum ofputil_action_code' corresponding to 'name' (e.g. if + * 'name' is "output" then the return value is OFPUTIL_OFPAT_OUTPUT), or -1 if + * 'name' is not the name of any action. + * + * ofp-util.def lists the mapping from names to action. */ +int +ofputil_action_code_from_name(const char *name) +{ + static const char *names[OFPUTIL_N_ACTIONS] = { +#define OFPAT_ACTION(ENUM, STRUCT, NAME) NAME, +#define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) NAME, +#include "ofp-util.def" + }; + + const char **p; + + for (p = names; p < &names[ARRAY_SIZE(names)]; p++) { + if (*p && !strcasecmp(name, *p)) { + return p - names; + } + } + return -1; +} + /* Returns true if 'action' outputs to 'port', false otherwise. */ bool action_outputs_to_port(const union ofp_action *action, ovs_be16 port) diff --git a/lib/ofp-util.def b/lib/ofp-util.def new file mode 100644 index 000000000..c5d883d67 --- /dev/null +++ b/lib/ofp-util.def @@ -0,0 +1,37 @@ +/* -*- c -*- */ + +#ifndef OFPAT_ACTION +#define OFPAT_ACTION(ENUM, STRUCT, NAME) +#endif +OFPAT_ACTION(OFPAT_OUTPUT, ofp_action_output, "output") +OFPAT_ACTION(OFPAT_SET_VLAN_VID, ofp_action_vlan_vid, "mod_vlan_vid") +OFPAT_ACTION(OFPAT_SET_VLAN_PCP, ofp_action_vlan_pcp, "mod_vlan_pcp") +OFPAT_ACTION(OFPAT_STRIP_VLAN, ofp_action_header, "strip_vlan") +OFPAT_ACTION(OFPAT_SET_DL_SRC, ofp_action_dl_addr, "mod_dl_src") +OFPAT_ACTION(OFPAT_SET_DL_DST, ofp_action_dl_addr, "mod_dl_dst") +OFPAT_ACTION(OFPAT_SET_NW_SRC, ofp_action_nw_addr, "mod_nw_src") +OFPAT_ACTION(OFPAT_SET_NW_DST, ofp_action_nw_addr, "mod_nw_dst") +OFPAT_ACTION(OFPAT_SET_NW_TOS, ofp_action_nw_tos, "mod_nw_tos") +OFPAT_ACTION(OFPAT_SET_TP_SRC, ofp_action_tp_port, "mod_tp_src") +OFPAT_ACTION(OFPAT_SET_TP_DST, ofp_action_tp_port, "mod_tp_dst") +OFPAT_ACTION(OFPAT_ENQUEUE, ofp_action_enqueue, "enqueue") +#undef OFPAT_ACTION + +#ifndef NXAST_ACTION +#define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) +#endif +NXAST_ACTION(NXAST_RESUBMIT, nx_action_resubmit, 0, "resubmit") +NXAST_ACTION(NXAST_SET_TUNNEL, nx_action_set_tunnel, 0, "set_tunnel") +NXAST_ACTION(NXAST_SET_QUEUE, nx_action_set_queue, 0, "set_queue") +NXAST_ACTION(NXAST_POP_QUEUE, nx_action_pop_queue, 0, "pop_queue") +NXAST_ACTION(NXAST_REG_MOVE, nx_action_reg_move, 0, "move") +NXAST_ACTION(NXAST_REG_LOAD, nx_action_reg_load, 0, "load") +NXAST_ACTION(NXAST_NOTE, nx_action_note, 1, "note") +NXAST_ACTION(NXAST_SET_TUNNEL64, nx_action_set_tunnel64, 0, "set_tunnel64") +NXAST_ACTION(NXAST_MULTIPATH, nx_action_multipath, 0, "multipath") +NXAST_ACTION(NXAST_AUTOPATH, nx_action_autopath, 0, "autopath") +NXAST_ACTION(NXAST_BUNDLE, nx_action_bundle, 1, "bundle") +NXAST_ACTION(NXAST_BUNDLE_LOAD, nx_action_bundle, 1, "bundle_load") +NXAST_ACTION(NXAST_RESUBMIT_TABLE, nx_action_resubmit, 0, NULL) +NXAST_ACTION(NXAST_OUTPUT_REG, nx_action_output_reg, 0, NULL) +#undef NXAST_ACTION diff --git a/lib/ofp-util.h b/lib/ofp-util.h index e3640dbe8..e70d963d5 100644 --- a/lib/ofp-util.h +++ b/lib/ofp-util.h @@ -280,42 +280,60 @@ struct ofpbuf *make_echo_reply(const struct ofp_header *rq); /* Actions. */ +/* The type of an action. + * + * For each implemented OFPAT_* and NXAST_* action type, there is a + * corresponding constant prefixed with OFPUTIL_, e.g.: + * + * OFPUTIL_OFPAT_OUTPUT + * OFPUTIL_OFPAT_SET_VLAN_VID + * OFPUTIL_OFPAT_SET_VLAN_PCP + * OFPUTIL_OFPAT_STRIP_VLAN + * OFPUTIL_OFPAT_SET_DL_SRC + * OFPUTIL_OFPAT_SET_DL_DST + * OFPUTIL_OFPAT_SET_NW_SRC + * OFPUTIL_OFPAT_SET_NW_DST + * OFPUTIL_OFPAT_SET_NW_TOS + * OFPUTIL_OFPAT_SET_TP_SRC + * OFPUTIL_OFPAT_SET_TP_DST + * OFPUTIL_OFPAT_ENQUEUE + * OFPUTIL_NXAST_RESUBMIT + * OFPUTIL_NXAST_SET_TUNNEL + * OFPUTIL_NXAST_SET_QUEUE + * OFPUTIL_NXAST_POP_QUEUE + * OFPUTIL_NXAST_REG_MOVE + * OFPUTIL_NXAST_REG_LOAD + * OFPUTIL_NXAST_NOTE + * OFPUTIL_NXAST_SET_TUNNEL64 + * OFPUTIL_NXAST_MULTIPATH + * OFPUTIL_NXAST_AUTOPATH + * OFPUTIL_NXAST_BUNDLE + * OFPUTIL_NXAST_BUNDLE_LOAD + * OFPUTIL_NXAST_RESUBMIT_TABLE + * OFPUTIL_NXAST_OUTPUT_REG + * + * (The above list helps developers who want to "grep" for these definitions.) + */ enum ofputil_action_code { - /* OFPAT_* actions. */ - OFPUTIL_OFPAT_OUTPUT, - OFPUTIL_OFPAT_SET_VLAN_VID, - OFPUTIL_OFPAT_SET_VLAN_PCP, - OFPUTIL_OFPAT_STRIP_VLAN, - OFPUTIL_OFPAT_SET_DL_SRC, - OFPUTIL_OFPAT_SET_DL_DST, - OFPUTIL_OFPAT_SET_NW_SRC, - OFPUTIL_OFPAT_SET_NW_DST, - OFPUTIL_OFPAT_SET_NW_TOS, - OFPUTIL_OFPAT_SET_TP_SRC, - OFPUTIL_OFPAT_SET_TP_DST, - OFPUTIL_OFPAT_ENQUEUE, - - /* NXAST_* actions. */ - OFPUTIL_NXAST_RESUBMIT, - OFPUTIL_NXAST_SET_TUNNEL, - OFPUTIL_NXAST_SET_QUEUE, - OFPUTIL_NXAST_POP_QUEUE, - OFPUTIL_NXAST_REG_MOVE, - OFPUTIL_NXAST_REG_LOAD, - OFPUTIL_NXAST_NOTE, - OFPUTIL_NXAST_SET_TUNNEL64, - OFPUTIL_NXAST_MULTIPATH, - OFPUTIL_NXAST_AUTOPATH, - OFPUTIL_NXAST_BUNDLE, - OFPUTIL_NXAST_BUNDLE_LOAD, - OFPUTIL_NXAST_RESUBMIT_TABLE, - OFPUTIL_NXAST_OUTPUT_REG +#define OFPAT_ACTION(ENUM, STRUCT, NAME) OFPUTIL_##ENUM, +#define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) OFPUTIL_##ENUM, +#include "ofp-util.def" +}; + +/* The number of values of "enum ofputil_action_code". */ +enum { +#define OFPAT_ACTION(ENUM, STRUCT, NAME) + 1 +#define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) + 1 + OFPUTIL_N_ACTIONS = 0 +#include "ofp-util.def" }; int ofputil_decode_action(const union ofp_action *); enum ofputil_action_code ofputil_decode_action_unsafe( const union ofp_action *); +int ofputil_action_code_from_name(const char *); + #define OFP_ACTION_ALIGN 8 /* Alignment of ofp_actions. */ static inline union ofp_action * -- 2.43.0