This change is meant to cleanup the tunnel lookup code. We are now using
kernel hash table so we can simplify `struct port_lookup_key`.

Signed-off-by: Pravin B Shelar <pshe...@nicira.com>
---
 datapath/tunnel.c    |  106 ++++++++++++++++++-------------------------------
 datapath/tunnel.h    |   15 +++++--
 datapath/vport-gre.c |    2 +-
 3 files changed, 51 insertions(+), 72 deletions(-)

diff --git a/datapath/tunnel.c b/datapath/tunnel.c
index 67fbd2b..173580f 100644
--- a/datapath/tunnel.c
+++ b/datapath/tunnel.c
@@ -160,61 +160,26 @@ static void assign_cache_rcu(struct vport *vport, struct 
tnl_cache *new_cache)
 static unsigned int *find_port_pool(const struct tnl_mutable_config *mutable)
 {
        if (mutable->flags & TNL_F_IN_KEY_MATCH) {
-               if (mutable->saddr)
+               if (mutable->key.saddr)
                        return &local_remote_ports;
                else
                        return &remote_ports;
        } else {
-               if (mutable->saddr)
+               if (mutable->key.saddr)
                        return &key_local_remote_ports;
                else
                        return &key_remote_ports;
        }
 }
 
-struct port_lookup_key {
-       const struct tnl_mutable_config *mutable;
-       __be64 key;
-       u32 tunnel_type;
-       __be32 saddr;
-       __be32 daddr;
-};
-
-/*
- * Modifies 'target' to store the rcu_dereferenced pointer that was used to do
- * the comparision.
- */
-static int port_cmp(const struct tnl_vport *tnl_vport,
-                    struct port_lookup_key *lookup)
-{
-       lookup->mutable = rcu_dereference_rtnl(tnl_vport->mutable);
-
-       return (lookup->mutable->tunnel_type == lookup->tunnel_type &&
-               lookup->mutable->daddr == lookup->daddr &&
-               lookup->mutable->in_key == lookup->key &&
-               lookup->mutable->saddr == lookup->saddr);
-}
-
-static u32 port_hash(struct port_lookup_key *k)
+static u32 port_hash(const struct port_lookup_key *k)
 {
        u32 x = jhash_3words((__force u32)k->saddr, (__force u32)k->daddr,
                             k->tunnel_type, 0);
-       return jhash_2words((__force u64)k->key >> 32, (__force u32)k->key, x);
+       return jhash_2words((__force u64)k->in_key >> 32,
+                           (__force u32)k->in_key, x);
 }
 
-static u32 mutable_hash(const struct tnl_mutable_config *mutable)
-{
-       struct port_lookup_key lookup;
-
-       lookup.saddr = mutable->saddr;
-       lookup.daddr = mutable->daddr;
-       lookup.key = mutable->in_key;
-       lookup.tunnel_type = mutable->tunnel_type;
-
-       return port_hash(&lookup);
-}
-
-
 static inline struct hlist_head *find_bucket(u32 hash)
 {
        return &port_table[(hash & (PORT_TABLE_SIZE - 1))];
@@ -223,11 +188,14 @@ static inline struct hlist_head *find_bucket(u32 hash)
 static void port_table_add_port(struct vport *vport)
 {
        struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
-       u32 hash = mutable_hash(rtnl_dereference(tnl_vport->mutable));
+       const struct tnl_mutable_config *mutable;
+       u32 hash;
 
        if (port_table_count == 0)
                schedule_cache_cleaner();
 
+       mutable = rtnl_dereference(tnl_vport->mutable);
+       hash = port_hash(&mutable->key);
        hlist_add_head_rcu(&tnl_vport->hash_node, find_bucket(hash));
        port_table_count++;
 
@@ -240,7 +208,7 @@ static void port_table_move_port(struct vport *vport,
        struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
        u32 hash;
 
-       hash = mutable_hash(new_mutable);
+       hash = port_hash(&new_mutable->key);
        hlist_del_init_rcu(&tnl_vport->hash_node);
        hlist_add_head_rcu(&tnl_vport->hash_node, find_bucket(hash));
 
@@ -262,18 +230,23 @@ static void port_table_remove_port(struct vport *vport)
        (*find_port_pool(rtnl_dereference(tnl_vport->mutable)))--;
 }
 
-static struct tnl_vport *port_table_lookup(struct port_lookup_key *lookup)
+static struct tnl_vport *port_table_lookup(struct port_lookup_key *key,
+                           const struct tnl_mutable_config **pmutable)
 {
        struct hlist_node *n;
        struct hlist_head *bucket;
-       u32 hash = port_hash(lookup);
+       u32 hash = port_hash(key);
        struct tnl_vport * tnl_vport;
+       const struct tnl_mutable_config *mutable;
 
        bucket = find_bucket(hash);
 
        hlist_for_each_entry_rcu(tnl_vport, n, bucket, hash_node) {
-               if (port_cmp(tnl_vport, lookup))
+               mutable = rcu_dereference_rtnl(tnl_vport->mutable);
+               if (!memcmp(&mutable->key, key, sizeof(*key))) {
+                       *pmutable = mutable;
                        return tnl_vport;
+               }
        }
 
        return NULL;
@@ -290,18 +263,18 @@ struct vport *tnl_find_port(__be32 saddr, __be32 daddr, 
__be64 key,
        lookup.daddr = daddr;
 
        if (tunnel_type & TNL_T_KEY_EXACT) {
-               lookup.key = key;
+               lookup.in_key = key;
                lookup.tunnel_type = tunnel_type & ~TNL_T_KEY_MATCH;
 
                if (key_local_remote_ports) {
-                       tnl_vport = port_table_lookup(&lookup);
+                       tnl_vport = port_table_lookup(&lookup, mutable);
                        if (tnl_vport)
                                goto found;
                }
 
                if (key_remote_ports) {
                        lookup.saddr = 0;
-                       tnl_vport = port_table_lookup(&lookup);
+                       tnl_vport = port_table_lookup(&lookup, mutable);
                        if (tnl_vport)
                                goto found;
 
@@ -310,18 +283,18 @@ struct vport *tnl_find_port(__be32 saddr, __be32 daddr, 
__be64 key,
        }
 
        if (tunnel_type & TNL_T_KEY_MATCH) {
-               lookup.key = 0;
+               lookup.in_key = 0;
                lookup.tunnel_type = tunnel_type & ~TNL_T_KEY_EXACT;
 
                if (local_remote_ports) {
-                       tnl_vport = port_table_lookup(&lookup);
+                       tnl_vport = port_table_lookup(&lookup, mutable);
                        if (tnl_vport)
                                goto found;
                }
 
                if (remote_ports) {
                        lookup.saddr = 0;
-                       tnl_vport = port_table_lookup(&lookup);
+                       tnl_vport = port_table_lookup(&lookup, mutable);
                        if (tnl_vport)
                                goto found;
                }
@@ -330,7 +303,6 @@ struct vport *tnl_find_port(__be32 saddr, __be32 daddr, 
__be64 key,
        return NULL;
 
 found:
-       *mutable = lookup.mutable;
        return tnl_vport_to_vport(tnl_vport);
 }
 
@@ -973,8 +945,8 @@ static struct rtable *find_route(struct vport *vport,
                if (unlikely(ip_route_output_key(&init_net, &rt, &fl)))
                        return NULL;
 #else
-               struct flowi4 fl = { .daddr = mutable->daddr,
-                                    .saddr = mutable->saddr,
+               struct flowi4 fl = { .daddr = mutable->key.daddr,
+                                    .saddr = mutable->key.saddr,
                                     .flowi4_tos = tos,
                                     .flowi4_proto = 
tnl_vport->tnl_ops->ipproto };
 
@@ -1327,8 +1299,8 @@ static int tnl_set_config(struct nlattr *options, const 
struct tnl_ops *tnl_ops,
        mutable->flags = nla_get_u32(a[OVS_TUNNEL_ATTR_FLAGS]) & TNL_F_PUBLIC;
 
        if (a[OVS_TUNNEL_ATTR_SRC_IPV4])
-               mutable->saddr = nla_get_be32(a[OVS_TUNNEL_ATTR_SRC_IPV4]);
-       mutable->daddr = nla_get_be32(a[OVS_TUNNEL_ATTR_DST_IPV4]);
+               mutable->key.saddr = nla_get_be32(a[OVS_TUNNEL_ATTR_SRC_IPV4]);
+       mutable->key.daddr = nla_get_be32(a[OVS_TUNNEL_ATTR_DST_IPV4]);
 
        if (a[OVS_TUNNEL_ATTR_TOS]) {
                mutable->tos = nla_get_u8(a[OVS_TUNNEL_ATTR_TOS]);
@@ -1339,13 +1311,13 @@ static int tnl_set_config(struct nlattr *options, const 
struct tnl_ops *tnl_ops,
        if (a[OVS_TUNNEL_ATTR_TTL])
                mutable->ttl = nla_get_u8(a[OVS_TUNNEL_ATTR_TTL]);
 
-       mutable->tunnel_type = tnl_ops->tunnel_type;
+       mutable->key.tunnel_type = tnl_ops->tunnel_type;
        if (!a[OVS_TUNNEL_ATTR_IN_KEY]) {
-               mutable->tunnel_type |= TNL_T_KEY_MATCH;
+               mutable->key.tunnel_type |= TNL_T_KEY_MATCH;
                mutable->flags |= TNL_F_IN_KEY_MATCH;
        } else {
-               mutable->tunnel_type |= TNL_T_KEY_EXACT;
-               mutable->in_key = nla_get_be64(a[OVS_TUNNEL_ATTR_IN_KEY]);
+               mutable->key.tunnel_type |= TNL_T_KEY_EXACT;
+               mutable->key.in_key = nla_get_be64(a[OVS_TUNNEL_ATTR_IN_KEY]);
        }
 
        if (!a[OVS_TUNNEL_ATTR_OUT_KEY])
@@ -1359,8 +1331,8 @@ static int tnl_set_config(struct nlattr *options, const 
struct tnl_ops *tnl_ops,
 
        mutable->tunnel_hlen += sizeof(struct iphdr);
 
-       old_vport = tnl_find_port(mutable->saddr, mutable->daddr,
-                                 mutable->in_key, mutable->tunnel_type,
+       old_vport = tnl_find_port(mutable->key.saddr, mutable->key.daddr,
+                                 mutable->key.in_key, mutable->key.tunnel_type,
                                  &old_mutable);
 
        if (old_vport && old_vport != cur_vport)
@@ -1448,7 +1420,7 @@ int tnl_set_options(struct vport *vport, struct nlattr 
*options)
        if (err)
                goto error_free;
 
-       if (mutable_hash(mutable) != mutable_hash(old_mutable))
+       if (port_hash(&mutable->key) != port_hash(&old_mutable->key))
                port_table_move_port(vport, mutable);
 
        return 0;
@@ -1465,14 +1437,14 @@ int tnl_get_options(const struct vport *vport, struct 
sk_buff *skb)
        const struct tnl_mutable_config *mutable = 
rcu_dereference_rtnl(tnl_vport->mutable);
 
        NLA_PUT_U32(skb, OVS_TUNNEL_ATTR_FLAGS, mutable->flags & TNL_F_PUBLIC);
-       NLA_PUT_BE32(skb, OVS_TUNNEL_ATTR_DST_IPV4, mutable->daddr);
+       NLA_PUT_BE32(skb, OVS_TUNNEL_ATTR_DST_IPV4, mutable->key.daddr);
 
        if (!(mutable->flags & TNL_F_IN_KEY_MATCH))
-               NLA_PUT_BE64(skb, OVS_TUNNEL_ATTR_IN_KEY, mutable->in_key);
+               NLA_PUT_BE64(skb, OVS_TUNNEL_ATTR_IN_KEY, mutable->key.in_key);
        if (!(mutable->flags & TNL_F_OUT_KEY_ACTION))
                NLA_PUT_BE64(skb, OVS_TUNNEL_ATTR_OUT_KEY, mutable->out_key);
-       if (mutable->saddr)
-               NLA_PUT_BE32(skb, OVS_TUNNEL_ATTR_SRC_IPV4, mutable->saddr);
+       if (mutable->key.saddr)
+               NLA_PUT_BE32(skb, OVS_TUNNEL_ATTR_SRC_IPV4, mutable->key.saddr);
        if (mutable->tos)
                NLA_PUT_U8(skb, OVS_TUNNEL_ATTR_TOS, mutable->tos);
        if (mutable->ttl)
diff --git a/datapath/tunnel.h b/datapath/tunnel.h
index a051495..1790c16 100644
--- a/datapath/tunnel.h
+++ b/datapath/tunnel.h
@@ -45,6 +45,16 @@
                      TNL_F_HDR_CACHE | TNL_F_IPSEC)
 
 /**
+ * Tunnel port key, used as hash table key.
+ */
+struct port_lookup_key {
+       __be64 in_key;
+       u32 tunnel_type;
+       __be32 saddr;
+       __be32 daddr;
+};
+
+/**
  * struct tnl_mutable_config - modifiable configuration for a tunnel.
  * @rcu: RCU callback head for deferred destruction.
  * @seq: Sequence number for distinguishing configuration versions.
@@ -61,21 +71,18 @@
  * @ttl: IPv4 TTL value to use for tunnel, 0 if no fixed TTL.
  */
 struct tnl_mutable_config {
+       struct port_lookup_key key;
        struct rcu_head rcu;
 
        unsigned seq;
 
-       u32 tunnel_type;
        unsigned tunnel_hlen;
 
        unsigned char eth_addr[ETH_ALEN];
 
        /* Configured via OVS_TUNNEL_ATTR_* attributes. */
-       __be64  in_key;
        __be64  out_key;
        u32     flags;
-       __be32  saddr;
-       __be32  daddr;
        u8      tos;
        u8      ttl;
 };
diff --git a/datapath/vport-gre.c b/datapath/vport-gre.c
index 5beae42..8775d38 100644
--- a/datapath/vport-gre.c
+++ b/datapath/vport-gre.c
@@ -207,7 +207,7 @@ static void gre_err(struct sk_buff *skb, u32 info)
         * out key as if it were the in key and then check to see if the input
         * and output keys are the same.
         */
-       if (mutable->in_key != mutable->out_key)
+       if (mutable->key.in_key != mutable->out_key)
                return;
 
        if (!!(mutable->flags & TNL_F_IN_KEY_MATCH) !=
-- 
1.7.1

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

Reply via email to