ofp-actions/instruction: helper functions for intructions
[sliver-openvswitch.git] / lib / ofp-actions.c
index 5681fba..6503f61 100644 (file)
@@ -434,38 +434,54 @@ action_is_valid(const union ofp_action *a, size_t n_actions)
          ((LEFT) -= ntohs((ITER)->header.len) / sizeof(union ofp_action), \
           (ITER) = action_next(ITER)))
 
+static void
+log_bad_action(const union ofp_action *actions, size_t n_actions, size_t ofs,
+               enum ofperr error)
+{
+    if (!VLOG_DROP_WARN(&rl)) {
+        struct ds s;
+
+        ds_init(&s);
+        ds_put_hex_dump(&s, actions, n_actions * sizeof *actions, 0, false);
+        VLOG_WARN("bad action at offset %#zx (%s):\n%s",
+                  ofs * sizeof *actions, ofperr_get_name(error), ds_cstr(&s));
+        ds_destroy(&s);
+    }
+}
+
 static enum ofperr
-ofpacts_from_openflow10(const union ofp_action *in, size_t n_in,
-                        struct ofpbuf *out)
+ofpacts_from_openflow(const union ofp_action *in, size_t n_in,
+                      struct ofpbuf *out,
+                      enum ofperr (*ofpact_from_openflow)(
+                          const union ofp_action *a, struct ofpbuf *out))
 {
     const union ofp_action *a;
     size_t left;
 
     ACTION_FOR_EACH (a, left, in, n_in) {
-        enum ofperr error = ofpact_from_openflow10(a, out);
+        enum ofperr error = ofpact_from_openflow(a, out);
         if (error) {
-            VLOG_WARN_RL(&rl, "bad action at offset %td (%s)",
-                         (a - in) * sizeof *a, ofperr_get_name(error));
+            log_bad_action(in, n_in, a - in, error);
             return error;
         }
     }
     if (left) {
-        if (!VLOG_DROP_WARN(&rl)) {
-            struct ds s;
-
-            ds_init(&s);
-            ds_put_hex_dump(&s, in, n_in * sizeof *a, 0, false);
-            VLOG_WARN("bad action format at offset %#zx:\n%s",
-                      (n_in - left) * sizeof *a, ds_cstr(&s));
-            ds_destroy(&s);
-        }
-        return OFPERR_OFPBAC_BAD_LEN;
+        enum ofperr error = OFPERR_OFPBAC_BAD_LEN;
+        log_bad_action(in, n_in, n_in - left, error);
+        return error;
     }
 
     ofpact_pad(out);
     return 0;
 }
 
+static enum ofperr
+ofpacts_from_openflow10(const union ofp_action *in, size_t n_in,
+                        struct ofpbuf *out)
+{
+    return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow10);
+}
+
 static enum ofperr
 ofpacts_pull_actions(struct ofpbuf *openflow, unsigned int actions_len,
                      struct ofpbuf *ofpacts,
@@ -652,62 +668,19 @@ static enum ofperr
 ofpacts_from_openflow11(const union ofp_action *in, size_t n_in,
                         struct ofpbuf *out)
 {
-    const union ofp_action *a;
-    size_t left;
-
-    ACTION_FOR_EACH (a, left, in, n_in) {
-        enum ofperr error = ofpact_from_openflow11(a, out);
-        if (error) {
-            VLOG_WARN_RL(&rl, "bad action at offset %td (%s)",
-                         (a - in) * sizeof *a, ofperr_get_name(error));
-            return error;
-        }
-    }
-    if (left) {
-        VLOG_WARN_RL(&rl, "bad action format at offset %zu",
-                     (n_in - left) * sizeof *a);
-        return OFPERR_OFPBAC_BAD_LEN;
-    }
-
-    return 0;
+    return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow11);
 }
 \f
 /* OpenFlow 1.1 instructions. */
 
-#define OVS_INSTRUCTIONS                                    \
-    DEFINE_INST(OFPIT11_GOTO_TABLE,                         \
-                ofp11_instruction_goto_table,     false,    \
-                "goto_table")                               \
-                                                            \
-    DEFINE_INST(OFPIT11_WRITE_METADATA,                     \
-                ofp11_instruction_write_metadata, false,    \
-                "write_metadata")                           \
-                                                            \
-    DEFINE_INST(OFPIT11_WRITE_ACTIONS,                      \
-                ofp11_instruction_actions,        true,     \
-                "write_actions")                            \
-                                                            \
-    DEFINE_INST(OFPIT11_APPLY_ACTIONS,                      \
-                ofp11_instruction_actions,        true,     \
-                "apply_actions")                            \
-                                                            \
-    DEFINE_INST(OFPIT11_CLEAR_ACTIONS,                      \
-                ofp11_instruction,                false,    \
-                "clear_actions")
-
-enum ovs_instruction_type {
-#define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) OVSINST_##ENUM,
-    OVS_INSTRUCTIONS
-#undef DEFINE_INST
-};
-
-enum {
-#define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) + 1
-    N_OVS_INSTRUCTIONS = OVS_INSTRUCTIONS
-#undef DEFINE_INST
-};
-
 #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)                   \
     {                                                           \
@@ -726,6 +699,41 @@ enum {
 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)
 {
@@ -839,14 +847,8 @@ ofpacts_pull_openflow11_actions(struct ofpbuf *openflow,
                                 unsigned int actions_len,
                                 struct ofpbuf *ofpacts)
 {
-    enum ofperr error;
-
-    error = ofpacts_pull_actions(openflow, actions_len, ofpacts,
-                                 ofpacts_from_openflow11);
-    if (!error) {
-        ofpact_pad(ofpacts);
-    }
-    return error;
+    return ofpacts_pull_actions(openflow, actions_len, ofpacts,
+                                ofpacts_from_openflow11);
 }
 
 enum ofperr
@@ -897,8 +899,6 @@ ofpacts_pull_openflow11_instructions(struct ofpbuf *openflow,
         }
     }
 
-    ofpact_pad(ofpacts);
-
     if (insts[OVSINST_OFPIT11_GOTO_TABLE] ||
         insts[OVSINST_OFPIT11_WRITE_METADATA] ||
         insts[OVSINST_OFPIT11_WRITE_ACTIONS] ||
@@ -1393,15 +1393,18 @@ ofpact_to_openflow11(const struct ofpact *a, struct ofpbuf *out)
 /* Converts the ofpacts in 'ofpacts' (terminated by OFPACT_END) into OpenFlow
  * 1.1 actions in 'openflow', appending the actions to any existing data in
  * 'openflow'. */
-void
+size_t
 ofpacts_put_openflow11_actions(const struct ofpact ofpacts[],
                                size_t ofpacts_len, struct ofpbuf *openflow)
 {
     const struct ofpact *a;
+    size_t start_size = openflow->size;
 
     OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
         ofpact_to_openflow11(a, openflow);
     }
+
+    return openflow->size - start_size;
 }
 
 void