datapath: Make adding and attaching a vport a single step.
[sliver-openvswitch.git] / datapath / tunnel.c
index cf74212..b6bd6f6 100644 (file)
@@ -68,7 +68,7 @@
 #define CACHE_DATA_ALIGN 16
 
 /* Protected by RCU. */
-static struct tbl *port_table;
+static struct tbl *port_table __read_mostly;
 
 static void cache_cleaner(struct work_struct *work);
 DECLARE_DELAYED_WORK(cache_cleaner_wq, cache_cleaner);
@@ -78,10 +78,10 @@ DECLARE_DELAYED_WORK(cache_cleaner_wq, cache_cleaner);
  * synchronization because we could have just as easily read the value before
  * the port change happened.
  */
-static unsigned int key_local_remote_ports;
-static unsigned int key_remote_ports;
-static unsigned int local_remote_ports;
-static unsigned int remote_ports;
+static unsigned int key_local_remote_ports __read_mostly;
+static unsigned int key_remote_ports __read_mostly;
+static unsigned int local_remote_ports __read_mostly;
+static unsigned int remote_ports __read_mostly;
 
 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
 #define rt_dst(rt) (rt->dst)
@@ -164,15 +164,11 @@ static unsigned int *find_port_pool(const struct tnl_mutable_config *mutable)
        }
 }
 
-enum lookup_key {
-       LOOKUP_TUNNEL_TYPE      = 0,
-       LOOKUP_SADDR            = 1,
-       LOOKUP_DADDR            = 2,
-       LOOKUP_KEY              = 3,
-};
-
 struct port_lookup_key {
-       u32 vals[4];                    /* Contains enum lookup_key keys. */
+       u32 tunnel_type;
+       __be32 saddr;
+       __be32 daddr;
+       __be32 key;
        const struct tnl_mutable_config *mutable;
 };
 
@@ -187,25 +183,25 @@ static int port_cmp(const struct tbl_node *node, void *target)
 
        lookup->mutable = rcu_dereference(tnl_vport->mutable);
 
-       return (lookup->mutable->tunnel_type == lookup->vals[LOOKUP_TUNNEL_TYPE]) &&
-              lookup->mutable->port_config.daddr == lookup->vals[LOOKUP_DADDR] &&
-              lookup->mutable->port_config.in_key == lookup->vals[LOOKUP_KEY] &&
-              lookup->mutable->port_config.saddr == lookup->vals[LOOKUP_SADDR];
+       return (lookup->mutable->tunnel_type == lookup->tunnel_type &&
+               lookup->mutable->port_config.daddr == lookup->daddr &&
+               lookup->mutable->port_config.in_key == lookup->key &&
+               lookup->mutable->port_config.saddr == lookup->saddr);
 }
 
-static u32 port_hash(struct port_lookup_key *lookup)
+static u32 port_hash(struct port_lookup_key *k)
 {
-       return jhash2(lookup->vals, ARRAY_SIZE(lookup->vals), 0);
+       return jhash_3words(k->key, k->saddr, k->daddr, k->tunnel_type);
 }
 
 static u32 mutable_hash(const struct tnl_mutable_config *mutable)
 {
        struct port_lookup_key lookup;
 
-       lookup.vals[LOOKUP_SADDR] = mutable->port_config.saddr;
-       lookup.vals[LOOKUP_DADDR] = mutable->port_config.daddr;
-       lookup.vals[LOOKUP_KEY] = mutable->port_config.in_key;
-       lookup.vals[LOOKUP_TUNNEL_TYPE] = mutable->tunnel_type;
+       lookup.saddr = mutable->port_config.saddr;
+       lookup.daddr = mutable->port_config.daddr;
+       lookup.key = mutable->port_config.in_key;
+       lookup.tunnel_type = mutable->tunnel_type;
 
        return port_hash(&lookup);
 }
@@ -316,12 +312,12 @@ struct vport *tnl_find_port(__be32 saddr, __be32 daddr, __be32 key,
        if (unlikely(!table))
                return NULL;
 
-       lookup.vals[LOOKUP_SADDR] = saddr;
-       lookup.vals[LOOKUP_DADDR] = daddr;
+       lookup.saddr = saddr;
+       lookup.daddr = daddr;
 
        if (tunnel_type & TNL_T_KEY_EXACT) {
-               lookup.vals[LOOKUP_KEY] = key;
-               lookup.vals[LOOKUP_TUNNEL_TYPE] = tunnel_type & ~TNL_T_KEY_MATCH;
+               lookup.key = key;
+               lookup.tunnel_type = tunnel_type & ~TNL_T_KEY_MATCH;
 
                if (key_local_remote_ports) {
                        tbl_node = tbl_lookup(table, &lookup, port_hash(&lookup), port_cmp);
@@ -330,19 +326,19 @@ struct vport *tnl_find_port(__be32 saddr, __be32 daddr, __be32 key,
                }
 
                if (key_remote_ports) {
-                       lookup.vals[LOOKUP_SADDR] = 0;
+                       lookup.saddr = 0;
 
                        tbl_node = tbl_lookup(table, &lookup, port_hash(&lookup), port_cmp);
                        if (tbl_node)
                                goto found;
 
-                       lookup.vals[LOOKUP_SADDR] = saddr;
+                       lookup.saddr = saddr;
                }
        }
 
        if (tunnel_type & TNL_T_KEY_MATCH) {
-               lookup.vals[LOOKUP_KEY] = 0;
-               lookup.vals[LOOKUP_TUNNEL_TYPE] = tunnel_type & ~TNL_T_KEY_EXACT;
+               lookup.key = 0;
+               lookup.tunnel_type = tunnel_type & ~TNL_T_KEY_EXACT;
 
                if (local_remote_ports) {
                        tbl_node = tbl_lookup(table, &lookup, port_hash(&lookup), port_cmp);
@@ -351,7 +347,7 @@ struct vport *tnl_find_port(__be32 saddr, __be32 daddr, __be32 key,
                }
 
                if (remote_ports) {
-                       lookup.vals[LOOKUP_SADDR] = 0;
+                       lookup.saddr = 0;
 
                        tbl_node = tbl_lookup(table, &lookup, port_hash(&lookup), port_cmp);
                        if (tbl_node)
@@ -1099,6 +1095,7 @@ static int send_frags(struct sk_buff *skb,
                int frag_len = skb->len - mutable->tunnel_hlen;
 
                skb->next = NULL;
+               memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
 
                err = ip_local_out(skb);
                if (likely(net_xmit_eval(err) == 0))
@@ -1247,10 +1244,6 @@ int tnl_send(struct vport *vport, struct sk_buff *skb)
                                skb_dst_set(skb, unattached_dst);
                                unattached_dst = NULL;
                        }
-
-
-                       memset(&IPCB(skb)->opt, 0, sizeof(IPCB(skb)->opt));
-                       IPCB(skb)->flags = 0;
                }
                skb_set_transport_header(skb, skb_network_offset(skb) + sizeof(struct iphdr));
 
@@ -1307,15 +1300,14 @@ out:
        return sent_len;
 }
 
-static int set_config(const void __user *uconfig, const struct tnl_ops *tnl_ops,
+static int set_config(const void *config, const struct tnl_ops *tnl_ops,
                      const struct vport *cur_vport,
                      struct tnl_mutable_config *mutable)
 {
        const struct vport *old_vport;
        const struct tnl_mutable_config *old_mutable;
 
-       if (copy_from_user(&mutable->port_config, uconfig, sizeof(struct tnl_port_config)))
-               return -EFAULT;
+       mutable->port_config = *(struct tnl_port_config *)config;
 
        if (mutable->port_config.daddr == 0)
                return -EINVAL;
@@ -1351,7 +1343,7 @@ static int set_config(const void __user *uconfig, const struct tnl_ops *tnl_ops,
        return 0;
 }
 
-struct vport *tnl_create(const char *name, const void __user *config,
+struct vport *tnl_create(const struct vport_parms *parms,
                         const struct vport_ops *vport_ops,
                         const struct tnl_ops *tnl_ops)
 {
@@ -1368,7 +1360,7 @@ struct vport *tnl_create(const char *name, const void __user *config,
 
        tnl_vport = tnl_vport_priv(vport);
 
-       strcpy(tnl_vport->name, name);
+       strcpy(tnl_vport->name, parms->name);
        tnl_vport->tnl_ops = tnl_ops;
 
        tnl_vport->mutable = kzalloc(sizeof(struct tnl_mutable_config), GFP_KERNEL);
@@ -1383,7 +1375,7 @@ struct vport *tnl_create(const char *name, const void __user *config,
        get_random_bytes(&initial_frag_id, sizeof(int));
        atomic_set(&tnl_vport->frag_id, initial_frag_id);
 
-       err = set_config(config, tnl_ops, NULL, tnl_vport->mutable);
+       err = set_config(parms->config, tnl_ops, NULL, tnl_vport->mutable);
        if (err)
                goto error_free_mutable;
 
@@ -1408,7 +1400,7 @@ error:
        return ERR_PTR(err);
 }
 
-int tnl_modify(struct vport *vport, const void __user *config)
+int tnl_modify(struct vport *vport, struct odp_port *port)
 {
        struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
        struct tnl_mutable_config *mutable;
@@ -1420,7 +1412,7 @@ int tnl_modify(struct vport *vport, const void __user *config)
                goto error;
        }
 
-       err = set_config(config, tnl_vport->tnl_ops, vport, mutable);
+       err = set_config(port->config, tnl_vport->tnl_ops, vport, mutable);
        if (err)
                goto error_free;