I was about to add another complete list of all the connection states but
this eliminates the need.

Signed-off-by: Ben Pfaff <b...@ovn.org>
---
 lib/flow.c               | 21 ++++--------------
 lib/odp-util.c           | 56 +++++++++---------------------------------------
 lib/packets.h            | 45 +++++++++++++++++++-------------------
 ovn/lib/logical-fields.c | 37 ++++++++++++--------------------
 4 files changed, 51 insertions(+), 108 deletions(-)

diff --git a/lib/flow.c b/lib/flow.c
index 2b1ec4fed7ef..5f9c3d0e3a88 100644
--- a/lib/flow.c
+++ b/lib/flow.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
+ * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2017 Nicira, 
Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -1003,22 +1003,9 @@ flow_get_metadata(const struct flow *flow, struct match 
*flow_metadata)
 const char *ct_state_to_string(uint32_t state)
 {
     switch (state) {
-    case CS_REPLY_DIR:
-        return "rpl";
-    case CS_TRACKED:
-        return "trk";
-    case CS_NEW:
-        return "new";
-    case CS_ESTABLISHED:
-        return "est";
-    case CS_RELATED:
-        return "rel";
-    case CS_INVALID:
-        return "inv";
-    case CS_SRC_NAT:
-        return "snat";
-    case CS_DST_NAT:
-        return "dnat";
+#define CS_STATE(ENUM, INDEX, NAME) case CS_##ENUM: return NAME;
+        CS_STATES
+#undef CS_STATE
     default:
         return NULL;
     }
diff --git a/lib/odp-util.c b/lib/odp-util.c
index 874777858906..ebb572dc1acc 100644
--- a/lib/odp-util.c
+++ b/lib/odp-util.c
@@ -3533,30 +3533,12 @@ ovs_to_odp_ct_state(uint8_t state)
 {
     uint32_t odp = 0;
 
-    if (state & CS_NEW) {
-        odp |= OVS_CS_F_NEW;
-    }
-    if (state & CS_ESTABLISHED) {
-        odp |= OVS_CS_F_ESTABLISHED;
-    }
-    if (state & CS_RELATED) {
-        odp |= OVS_CS_F_RELATED;
-    }
-    if (state & CS_INVALID) {
-        odp |= OVS_CS_F_INVALID;
-    }
-    if (state & CS_REPLY_DIR) {
-        odp |= OVS_CS_F_REPLY_DIR;
-    }
-    if (state & CS_TRACKED) {
-        odp |= OVS_CS_F_TRACKED;
-    }
-    if (state & CS_SRC_NAT) {
-        odp |= OVS_CS_F_SRC_NAT;
-    }
-    if (state & CS_DST_NAT) {
-        odp |= OVS_CS_F_DST_NAT;
+#define CS_STATE(ENUM, INDEX, NAME)             \
+    if (state & CS_##ENUM) {                    \
+        odp |= OVS_CS_F_##ENUM;                 \
     }
+    CS_STATES
+#undef CS_STATE
 
     return odp;
 }
@@ -3566,30 +3548,12 @@ odp_to_ovs_ct_state(uint32_t flags)
 {
     uint32_t state = 0;
 
-    if (flags & OVS_CS_F_NEW) {
-        state |= CS_NEW;
-    }
-    if (flags & OVS_CS_F_ESTABLISHED) {
-        state |= CS_ESTABLISHED;
-    }
-    if (flags & OVS_CS_F_RELATED) {
-        state |= CS_RELATED;
-    }
-    if (flags & OVS_CS_F_INVALID) {
-        state |= CS_INVALID;
-    }
-    if (flags & OVS_CS_F_REPLY_DIR) {
-        state |= CS_REPLY_DIR;
-    }
-    if (flags & OVS_CS_F_TRACKED) {
-        state |= CS_TRACKED;
-    }
-    if (flags & OVS_CS_F_SRC_NAT) {
-        state |= CS_SRC_NAT;
-    }
-    if (flags & OVS_CS_F_DST_NAT) {
-        state |= CS_DST_NAT;
+#define CS_STATE(ENUM, INDEX, NAME) \
+    if (flags & OVS_CS_F_##ENUM) {  \
+        state |= CS_##ENUM;         \
     }
+    CS_STATES
+#undef CS_STATE
 
     return state;
 }
diff --git a/lib/packets.h b/lib/packets.h
index d8f32fc5f3f7..6776be36dcc1 100644
--- a/lib/packets.h
+++ b/lib/packets.h
@@ -794,33 +794,34 @@ struct tcp_header {
 };
 BUILD_ASSERT_DECL(TCP_HEADER_LEN == sizeof(struct tcp_header));
 
-/* Connection states */
-enum {
-    CS_NEW_BIT =         0,
-    CS_ESTABLISHED_BIT = 1,
-    CS_RELATED_BIT =     2,
-    CS_REPLY_DIR_BIT =   3,
-    CS_INVALID_BIT =     4,
-    CS_TRACKED_BIT =     5,
-    CS_SRC_NAT_BIT =     6,
-    CS_DST_NAT_BIT =     7,
-};
+/* Connection states.
+ *
+ * Names like CS_RELATED are bit values, e.g. 1 << 2.
+ * Names like CS_RELATED_BIT are bit indexes, e.g. 2. */
+#define CS_STATES                               \
+    CS_STATE(NEW,         0, "new")             \
+    CS_STATE(ESTABLISHED, 1, "est")             \
+    CS_STATE(RELATED,     2, "rel")             \
+    CS_STATE(REPLY_DIR,   3, "rpl")             \
+    CS_STATE(INVALID,     4, "inv")             \
+    CS_STATE(TRACKED,     5, "trk")             \
+    CS_STATE(SRC_NAT,     6, "snat")            \
+    CS_STATE(DST_NAT,     7, "dnat")
 
 enum {
-    CS_NEW =         (1 << CS_NEW_BIT),
-    CS_ESTABLISHED = (1 << CS_ESTABLISHED_BIT),
-    CS_RELATED =     (1 << CS_RELATED_BIT),
-    CS_REPLY_DIR =   (1 << CS_REPLY_DIR_BIT),
-    CS_INVALID =     (1 << CS_INVALID_BIT),
-    CS_TRACKED =     (1 << CS_TRACKED_BIT),
-    CS_SRC_NAT =     (1 << CS_SRC_NAT_BIT),
-    CS_DST_NAT =     (1 << CS_DST_NAT_BIT),
+#define CS_STATE(ENUM, INDEX, NAME) \
+    CS_##ENUM = 1 << INDEX, \
+    CS_##ENUM##_BIT = INDEX,
+    CS_STATES
+#undef CS_STATE
 };
 
 /* Undefined connection state bits. */
-#define CS_SUPPORTED_MASK    (CS_NEW | CS_ESTABLISHED | CS_RELATED \
-                              | CS_INVALID | CS_REPLY_DIR | CS_TRACKED \
-                              | CS_SRC_NAT | CS_DST_NAT)
+enum {
+#define CS_STATE(ENUM, INDEX, NAME) +CS_##ENUM
+    CS_SUPPORTED_MASK = CS_STATES
+#undef CS_STATE
+};
 #define CS_UNSUPPORTED_MASK  (~(uint32_t)CS_SUPPORTED_MASK)
 
 #define ARP_HRD_ETHERNET 1
diff --git a/ovn/lib/logical-fields.c b/ovn/lib/logical-fields.c
index fa134d63e2dc..26e336f5a01c 100644
--- a/ovn/lib/logical-fields.c
+++ b/ovn/lib/logical-fields.c
@@ -1,4 +1,4 @@
-/* Copyright (c) 2016 Nicira, Inc.
+/* Copyright (c) 2016, 2017 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -36,6 +36,15 @@ add_subregister(const char *name,
     free(expansion);
 }
 
+static void
+add_ct_bit(const char *name, int index, struct shash *symtab)
+{
+    char *expansion = xasprintf("ct_state[%d]", index);
+    const char *prereqs = index == CS_TRACKED_BIT ? NULL : "ct.trk";
+    expr_symtab_add_subfield(symtab, name, prereqs, expansion);
+    free(expansion);
+}
+
 void
 ovn_init_symtab(struct shash *symtab)
 {
@@ -105,28 +114,10 @@ ovn_init_symtab(struct shash *symtab)
 
     expr_symtab_add_field(symtab, "ct_state", MFF_CT_STATE, NULL, false);
 
-    struct ct_bit {
-        const char *name;
-        int bit;
-    };
-    static const struct ct_bit bits[] = {
-        {"trk", CS_TRACKED_BIT},
-        {"new", CS_NEW_BIT},
-        {"est", CS_ESTABLISHED_BIT},
-        {"rel", CS_RELATED_BIT},
-        {"rpl", CS_REPLY_DIR_BIT},
-        {"inv", CS_INVALID_BIT},
-        {"dnat", CS_DST_NAT_BIT},
-        {"snat", CS_SRC_NAT_BIT},
-    };
-    for (const struct ct_bit *b = bits; b < &bits[ARRAY_SIZE(bits)]; b++) {
-        char *name = xasprintf("ct.%s", b->name);
-        char *expansion = xasprintf("ct_state[%d]", b->bit);
-        const char *prereqs = b->bit == CS_TRACKED_BIT ? NULL : "ct.trk";
-        expr_symtab_add_subfield(symtab, name, prereqs, expansion);
-        free(expansion);
-        free(name);
-    }
+#define CS_STATE(ENUM, INDEX, NAME) \
+    add_ct_bit("ct."NAME, CS_##ENUM##_BIT, symtab);
+    CS_STATES
+#undef CS_STATE
 
     /* Data fields. */
     expr_symtab_add_field(symtab, "eth.src", MFF_ETH_SRC, NULL, false);
-- 
2.10.2

_______________________________________________
dev mailing list
d...@openvswitch.org
https://mail.openvswitch.org/mailman/listinfo/ovs-dev

Reply via email to