Introduce a macro instead for
With this macro, the code is a bit reduced.
test: compile-tested and unit tests passed.

Signed-off-by: Isaku Yamahata <yamah...@valinux.co.jp>
---
 lib/meta-flow.c |  266 +++----------------------------------------------------
 lib/meta-flow.h |   31 +++++++
 2 files changed, 45 insertions(+), 252 deletions(-)

diff --git a/lib/meta-flow.c b/lib/meta-flow.c
index c208690..5f16fa7 100644
--- a/lib/meta-flow.c
+++ b/lib/meta-flow.c
@@ -576,33 +576,9 @@ mf_is_all_wild(const struct mf_field *mf, const struct 
flow_wildcards *wc)
         return !wc->metadata_mask;
 
 #if FLOW_N_REGS > 0
-    case MFF_REG0:
-#endif
-#if FLOW_N_REGS > 1
-    case MFF_REG1:
-#endif
-#if FLOW_N_REGS > 2
-    case MFF_REG2:
-#endif
-#if FLOW_N_REGS > 3
-    case MFF_REG3:
-#endif
-#if FLOW_N_REGS > 4
-    case MFF_REG4:
-#endif
-#if FLOW_N_REGS > 5
-    case MFF_REG5:
-#endif
-#if FLOW_N_REGS > 6
-    case MFF_REG6:
-#endif
-#if FLOW_N_REGS > 7
-    case MFF_REG7:
-#endif
-#if FLOW_N_REGS > 8
-#error
-#endif
+    CASE_MFF_REGS
         return !wc->reg_masks[mf->id - MFF_REG0];
+#endif
 
     case MFF_ETH_SRC:
         return eth_addr_is_zero(wc->dl_src_mask);
@@ -689,34 +665,10 @@ mf_get_mask(const struct mf_field *mf, const struct 
flow_wildcards *wc,
         break;
 
 #if FLOW_N_REGS > 0
-    case MFF_REG0:
-#endif
-#if FLOW_N_REGS > 1
-    case MFF_REG1:
-#endif
-#if FLOW_N_REGS > 2
-    case MFF_REG2:
-#endif
-#if FLOW_N_REGS > 3
-    case MFF_REG3:
-#endif
-#if FLOW_N_REGS > 4
-    case MFF_REG4:
-#endif
-#if FLOW_N_REGS > 5
-    case MFF_REG5:
-#endif
-#if FLOW_N_REGS > 6
-    case MFF_REG6:
-#endif
-#if FLOW_N_REGS > 7
-    case MFF_REG7:
-#endif
-#if FLOW_N_REGS > 8
-#error
-#endif
+    CASE_MFF_REGS
         mask->be32 = htonl(wc->reg_masks[mf->id - MFF_REG0]);
         break;
+#endif
 
     case MFF_ETH_DST:
         memcpy(mask->mac, wc->dl_dst_mask, ETH_ADDR_LEN);
@@ -888,33 +840,7 @@ mf_is_value_valid(const struct mf_field *mf, const union 
mf_value *value)
     case MFF_TUN_ID:
     case MFF_METADATA:
     case MFF_IN_PORT:
-#if FLOW_N_REGS > 0
-    case MFF_REG0:
-#endif
-#if FLOW_N_REGS > 1
-    case MFF_REG1:
-#endif
-#if FLOW_N_REGS > 2
-    case MFF_REG2:
-#endif
-#if FLOW_N_REGS > 3
-    case MFF_REG3:
-#endif
-#if FLOW_N_REGS > 4
-    case MFF_REG4:
-#endif
-#if FLOW_N_REGS > 5
-    case MFF_REG5:
-#endif
-#if FLOW_N_REGS > 6
-    case MFF_REG6:
-#endif
-#if FLOW_N_REGS > 7
-    case MFF_REG7:
-#endif
-#if FLOW_N_REGS > 8
-#error
-#endif
+    CASE_MFF_REGS
     case MFF_ETH_SRC:
     case MFF_ETH_DST:
     case MFF_ETH_TYPE:
@@ -986,34 +912,10 @@ mf_get_value(const struct mf_field *mf, const struct flow 
*flow,
         break;
 
 #if FLOW_N_REGS > 0
-    case MFF_REG0:
-#endif
-#if FLOW_N_REGS > 1
-    case MFF_REG1:
-#endif
-#if FLOW_N_REGS > 2
-    case MFF_REG2:
-#endif
-#if FLOW_N_REGS > 3
-    case MFF_REG3:
-#endif
-#if FLOW_N_REGS > 4
-    case MFF_REG4:
-#endif
-#if FLOW_N_REGS > 5
-    case MFF_REG5:
-#endif
-#if FLOW_N_REGS > 6
-    case MFF_REG6:
-#endif
-#if FLOW_N_REGS > 7
-    case MFF_REG7:
-#endif
-#if FLOW_N_REGS > 8
-#error
-#endif
+    CASE_MFF_REGS
         value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
         break;
+#endif
 
     case MFF_ETH_SRC:
         memcpy(value->mac, flow->dl_src, ETH_ADDR_LEN);
@@ -1157,33 +1059,7 @@ mf_set_value(const struct mf_field *mf,
         break;
 
 #if FLOW_N_REGS > 0
-    case MFF_REG0:
-#endif
-#if FLOW_N_REGS > 1
-    case MFF_REG1:
-#endif
-#if FLOW_N_REGS > 2
-    case MFF_REG2:
-#endif
-#if FLOW_N_REGS > 3
-    case MFF_REG3:
-#endif
-#if FLOW_N_REGS > 4
-    case MFF_REG4:
-#endif
-#if FLOW_N_REGS > 5
-    case MFF_REG5:
-#endif
-#if FLOW_N_REGS > 6
-    case MFF_REG6:
-#endif
-#if FLOW_N_REGS > 7
-    case MFF_REG7:
-#endif
-#if FLOW_N_REGS > 8
-#error
-#endif
-#if FLOW_N_REGS > 0
+    CASE_MFF_REGS
         cls_rule_set_reg(rule, mf->id - MFF_REG0, ntohl(value->be32));
         break;
 #endif
@@ -1330,33 +1206,7 @@ mf_set_flow_value(const struct mf_field *mf,
         break;
 
 #if FLOW_N_REGS > 0
-    case MFF_REG0:
-#endif
-#if FLOW_N_REGS > 1
-    case MFF_REG1:
-#endif
-#if FLOW_N_REGS > 2
-    case MFF_REG2:
-#endif
-#if FLOW_N_REGS > 3
-    case MFF_REG3:
-#endif
-#if FLOW_N_REGS > 4
-    case MFF_REG4:
-#endif
-#if FLOW_N_REGS > 5
-    case MFF_REG5:
-#endif
-#if FLOW_N_REGS > 6
-    case MFF_REG6:
-#endif
-#if FLOW_N_REGS > 7
-    case MFF_REG7:
-#endif
-#if FLOW_N_REGS > 8
-#error
-#endif
-#if FLOW_N_REGS > 0
+    CASE_MFF_REGS
         flow->regs[mf->id - MFF_REG0] = ntohl(value->be32);
         break;
 #endif
@@ -1512,48 +1362,10 @@ mf_set_wild(const struct mf_field *mf, struct cls_rule 
*rule)
         break;
 
 #if FLOW_N_REGS > 0
-    case MFF_REG0:
-        cls_rule_set_reg_masked(rule, 0, 0, 0);
-        break;
-#endif
-#if FLOW_N_REGS > 1
-    case MFF_REG1:
-        cls_rule_set_reg_masked(rule, 1, 0, 0);
-        break;
-#endif
-#if FLOW_N_REGS > 2
-    case MFF_REG2:
-        cls_rule_set_reg_masked(rule, 2, 0, 0);
-        break;
-#endif
-#if FLOW_N_REGS > 3
-    case MFF_REG3:
-        cls_rule_set_reg_masked(rule, 3, 0, 0);
-        break;
-#endif
-#if FLOW_N_REGS > 4
-    case MFF_REG4:
-        cls_rule_set_reg_masked(rule, 4, 0, 0);
-        break;
-#endif
-#if FLOW_N_REGS > 5
-    case MFF_REG5:
-        cls_rule_set_reg_masked(rule, 5, 0, 0);
-        break;
-#endif
-#if FLOW_N_REGS > 6
-    case MFF_REG6:
-        cls_rule_set_reg_masked(rule, 6, 0, 0);
+    CASE_MFF_REGS
+        cls_rule_set_reg_masked(rule, mf->id - MFF_REG0, 0, 0);
         break;
 #endif
-#if FLOW_N_REGS > 7
-    case MFF_REG7:
-        cls_rule_set_reg_masked(rule, 7, 0, 0);
-        break;
-#endif
-#if FLOW_N_REGS > 8
-#error
-#endif
 
     case MFF_ETH_SRC:
         memset(rule->flow.dl_src, 0, ETH_ADDR_LEN);
@@ -1729,35 +1541,11 @@ mf_set(const struct mf_field *mf,
         break;
 
 #if FLOW_N_REGS > 0
-    case MFF_REG0:
-#endif
-#if FLOW_N_REGS > 1
-    case MFF_REG1:
-#endif
-#if FLOW_N_REGS > 2
-    case MFF_REG2:
-#endif
-#if FLOW_N_REGS > 3
-    case MFF_REG3:
-#endif
-#if FLOW_N_REGS > 4
-    case MFF_REG4:
-#endif
-#if FLOW_N_REGS > 5
-    case MFF_REG5:
-#endif
-#if FLOW_N_REGS > 6
-    case MFF_REG6:
-#endif
-#if FLOW_N_REGS > 7
-    case MFF_REG7:
-#endif
-#if FLOW_N_REGS > 8
-#error
-#endif
+    CASE_MFF_REGS
         cls_rule_set_reg_masked(rule, mf->id - MFF_REG0,
                                 ntohl(value->be32), ntohl(mask->be32));
         break;
+#endif
 
     case MFF_ETH_DST:
         cls_rule_set_dl_dst_masked(rule, value->mac, mask->mac);
@@ -1888,33 +1676,7 @@ mf_random_value(const struct mf_field *mf, union 
mf_value *value)
     case MFF_TUN_ID:
     case MFF_METADATA:
     case MFF_IN_PORT:
-#if FLOW_N_REGS > 0
-    case MFF_REG0:
-#endif
-#if FLOW_N_REGS > 1
-    case MFF_REG1:
-#endif
-#if FLOW_N_REGS > 2
-    case MFF_REG2:
-#endif
-#if FLOW_N_REGS > 3
-    case MFF_REG3:
-#endif
-#if FLOW_N_REGS > 4
-    case MFF_REG4:
-#endif
-#if FLOW_N_REGS > 5
-    case MFF_REG5:
-#endif
-#if FLOW_N_REGS > 6
-    case MFF_REG6:
-#endif
-#if FLOW_N_REGS > 7
-    case MFF_REG7:
-#endif
-#if FLOW_N_REGS > 8
-#error
-#endif
+    CASE_MFF_REGS
     case MFF_ETH_SRC:
     case MFF_ETH_DST:
     case MFF_ETH_TYPE:
diff --git a/lib/meta-flow.h b/lib/meta-flow.h
index 0967633..e6afd52 100644
--- a/lib/meta-flow.h
+++ b/lib/meta-flow.h
@@ -59,6 +59,37 @@ enum mf_field_id {
 #if FLOW_N_REGS > 7
     MFF_REG7,                   /* be32 */
 #endif
+#if FLOW_N_REGS == 0
+# define CASE_MFF_REGS
+#elif FLOW_N_REGS == 1
+# define CASE_MFF_REGS                                          \
+    case MFF_REG0:
+#elif FLOW_N_REGS == 2
+# define CASE_MFF_REGS                                          \
+    case MFF_REG0: case MFF_REG1:
+#elif FLOW_N_REGS == 3
+# define CASE_MFF_REGS                                          \
+    case MFF_REG0: case MFF_REG1: case MFF_REG2:
+#elif FLOW_N_REGS == 4
+# define CASE_MFF_REGS                                          \
+    case MFF_REG0: case MFF_REG1: case MFF_REG2: case MFF_REG3:
+#elif FLOW_N_REGS == 5
+# define CASE_MFF_REGS                                          \
+    case MFF_REG0: case MFF_REG1: case MFF_REG2: case MFF_REG3: \
+    case MFF_REG4:
+#elif FLOW_N_REGS == 6
+# define CASE_MFF_REGS                                          \
+    case MFF_REG0: case MFF_REG1: case MFF_REG2: case MFF_REG3: \
+    case MFF_REG4: case MFF_REG5:
+#elif FLOW_N_REGS == 7
+# define CASE_MFF_REGS                                          \
+    case MFF_REG0: case MFF_REG1: case MFF_REG2: case MFF_REG3: \
+    case MFF_REG4: case MFF_REG5: case MFF_REG6:
+#elif FLOW_N_REGS == 8
+# define CASE_MFF_REGS                                          \
+    case MFF_REG0: case MFF_REG1: case MFF_REG2: case MFF_REG3: \
+    case MFF_REG4: case MFF_REG5: case MFF_REG6: case MFF_REG7:
+#endif
 #if FLOW_N_REGS > 8
 #error
 #endif
-- 
1.7.1.1

_______________________________________________
dev mailing list
dev@openvswitch.org
http://openvswitch.org/mailman/listinfo/dev

Reply via email to