[ovs-dev] [patch v2 3/5] conntrack: Create nat_conn_keys_insert().

Darrell Ball dlu998 at gmail.com
Thu Sep 21 07:12:07 UTC 2017


Create a separate function from existing code, so the
code can be reused in a subsequent patch; no change
in functionality.

Signed-off-by: Darrell Ball <dlu998 at gmail.com>
---
 lib/conntrack.c | 42 +++++++++++++++++++++++++++++-------------
 1 file changed, 29 insertions(+), 13 deletions(-)

diff --git a/lib/conntrack.c b/lib/conntrack.c
index c94bc27..2eca38d 100644
--- a/lib/conntrack.c
+++ b/lib/conntrack.c
@@ -96,6 +96,11 @@ nat_conn_keys_lookup(struct hmap *nat_conn_keys,
                      const struct conn_key *key,
                      uint32_t basis);
 
+static bool
+nat_conn_keys_insert(struct hmap *nat_conn_keys,
+                     const struct conn *nat_conn,
+                     uint32_t hash_basis);
+
 static void
 nat_conn_keys_remove(struct hmap *nat_conn_keys,
                      const struct conn_key *key,
@@ -2065,19 +2070,10 @@ nat_select_range_tuple(struct conntrack *ct, const struct conn *conn,
             nat_conn->rev_key.src.port = htons(port);
         }
 
-        struct nat_conn_key_node *nat_conn_key_node =
-            nat_conn_keys_lookup(&ct->nat_conn_keys, &nat_conn->rev_key,
-                                 ct->hash_basis);
-
-        if (!nat_conn_key_node) {
-            struct nat_conn_key_node *nat_conn_key =
-                xzalloc(sizeof *nat_conn_key);
-            nat_conn_key->key = nat_conn->rev_key;
-            nat_conn_key->value = nat_conn->key;
-            uint32_t nat_conn_key_hash = conn_key_hash(&nat_conn_key->key,
-                                                       ct->hash_basis);
-            hmap_insert(&ct->nat_conn_keys, &nat_conn_key->node,
-                        nat_conn_key_hash);
+        bool new_insert = nat_conn_keys_insert(&ct->nat_conn_keys, nat_conn,
+                                               ct->hash_basis);
+
+        if (new_insert) {
             return true;
         } else if (!all_ports_tried) {
             if (min_port == max_port) {
@@ -2137,6 +2133,26 @@ nat_conn_keys_lookup(struct hmap *nat_conn_keys,
     return NULL;
 }
 
+/* This function must be called with the ct->resources lock taken. */
+static bool
+nat_conn_keys_insert(struct hmap *nat_conn_keys, const struct conn *nat_conn,
+                     uint32_t hash_basis)
+{
+    struct nat_conn_key_node *nat_conn_key_node =
+        nat_conn_keys_lookup(nat_conn_keys, &nat_conn->rev_key, hash_basis);
+
+    if (!nat_conn_key_node) {
+        struct nat_conn_key_node *nat_conn_key = xzalloc(sizeof *nat_conn_key);
+        nat_conn_key->key = nat_conn->rev_key;
+        nat_conn_key->value = nat_conn->key;
+        uint32_t nat_conn_key_hash = conn_key_hash(&nat_conn_key->key,
+                                                   hash_basis);
+        hmap_insert(nat_conn_keys, &nat_conn_key->node, nat_conn_key_hash);
+        return true;
+    }
+    return false;
+}
+
 /* This function must be called with the ct->resources write lock taken. */
 static void
 nat_conn_keys_remove(struct hmap *nat_conn_keys,
-- 
1.9.1



More information about the dev mailing list