ofp-actions/instruction: helper functions for intructions
authorIsaku Yamahata <yamahata@valinux.co.jp>
Wed, 1 Aug 2012 15:24:10 +0000 (00:24 +0900)
committerBen Pfaff <blp@nicira.com>
Tue, 14 Aug 2012 16:58:18 +0000 (09:58 -0700)
This patch introduces helper functions
- to cast
- to convert from/to text

Signed-off-by: Isaku Yamahata <yamahata@valinux.co.jp>
Signed-off-by: Ben Pfaff <blp@nicira.com>
lib/ofp-actions.c
lib/ofp-actions.h

index b84ca63d19e9f08688e504e0c6e1ec62a98d5508..6503f61ca56561c7ce61583635417d707a31d7ab 100644 (file)
@@ -674,6 +674,13 @@ ofpacts_from_openflow11(const union ofp_action *in, size_t n_in,
 /* OpenFlow 1.1 instructions. */
 
 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME)             \
+    static inline const struct STRUCT *                         \
+    instruction_get_##ENUM(const struct ofp11_instruction *inst)\
+    {                                                           \
+        assert(inst->type == htons(ENUM));                      \
+        return (struct STRUCT *)inst;                           \
+    }                                                           \
+                                                                \
     static inline void                                          \
     instruction_init_##ENUM(struct STRUCT *s)                   \
     {                                                           \
@@ -692,6 +699,41 @@ ofpacts_from_openflow11(const union ofp_action *in, size_t n_in,
 OVS_INSTRUCTIONS
 #undef DEFINE_INST
 
+struct instruction_type_info {
+    enum ovs_instruction_type type;
+    const char *name;
+};
+
+static const struct instruction_type_info inst_info[] = {
+#define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME)    {OVSINST_##ENUM, NAME},
+OVS_INSTRUCTIONS
+#undef DEFINE_INST
+};
+
+const char *
+ofpact_instruction_name_from_type(enum ovs_instruction_type type)
+{
+    const struct instruction_type_info *p;
+    for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
+        if (p->type == type) {
+            return p->name;
+        }
+    }
+    return NULL;
+}
+
+int
+ofpact_instruction_type_from_name(const char *name)
+{
+    const struct instruction_type_info *p;
+    for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
+        if (!strcasecmp(name, p->name)) {
+            return p->type;
+        }
+    }
+    return -1;
+}
+
 static inline struct ofp11_instruction *
 instruction_next(const struct ofp11_instruction *inst)
 {
index 604b56c264e87a40cf5dadbaec90738c912aac02..2e021819d4464d71bfe9c7085d9960d21d50e0d3 100644 (file)
@@ -530,4 +530,7 @@ enum {
 #undef DEFINE_INST
 };
 
+const char *ofpact_instruction_name_from_type(enum ovs_instruction_type type);
+int ofpact_instruction_type_from_name(const char *name);
+
 #endif /* ofp-actions.h */