v1-v2:
        - Use hash table.
        - Remove dpif-linux port number allocator.
--8<--------------------------cut here-------------------------->8-

Use hash table to store ports of datapath. Allow full ranges of
32-bit number for port no, But limits count to 64K.

Signed-off-by: Pravin B Shelar <[email protected]>

Bug #2462
---
 datapath/actions.c                         |    2 +-
 datapath/datapath.c                        |  128 +++++++++++++--------
 datapath/datapath.h                        |   30 ++++-
 datapath/dp_sysfs_dp.c                     |    4 +-
 datapath/dp_sysfs_if.c                     |    2 +-
 datapath/flow.c                            |    8 +-
 datapath/linux/Modules.mk                  |    1 +
 datapath/linux/compat/include/linux/hash.h |   19 +++
 datapath/vport.c                           |    1 +
 datapath/vport.h                           |    2 +
 lib/dpif-linux.c                           |  175 +++++++++-------------------
 11 files changed, 189 insertions(+), 183 deletions(-)
 create mode 100644 datapath/linux/compat/include/linux/hash.h

diff --git a/datapath/actions.c b/datapath/actions.c
index 824791d..4b07603 100644
--- a/datapath/actions.c
+++ b/datapath/actions.c
@@ -248,7 +248,7 @@ static int do_output(struct datapath *dp, struct sk_buff 
*skb, int out_port)
        if (unlikely(!skb))
                return -ENOMEM;
 
-       vport = rcu_dereference(dp->ports[out_port]);
+       vport = ovs_vport_rcu(dp, out_port);
        if (unlikely(!vport)) {
                kfree_skb(skb);
                return -ENODEV;
diff --git a/datapath/datapath.c b/datapath/datapath.c
index 220c7dd..3248fb4 100644
--- a/datapath/datapath.c
+++ b/datapath/datapath.c
@@ -29,6 +29,7 @@
 #include <linux/time.h>
 #include <linux/etherdevice.h>
 #include <linux/genetlink.h>
+#include <linux/hash.h>
 #include <linux/kernel.h>
 #include <linux/kthread.h>
 #include <linux/mutex.h>
@@ -120,7 +121,7 @@ static struct datapath *get_dp(struct net *net, int 
dp_ifindex)
 /* Must be called with rcu_read_lock or RTNL lock. */
 const char *ovs_dp_name(const struct datapath *dp)
 {
-       struct vport *vport = rcu_dereference_rtnl(dp->ports[OVSP_LOCAL]);
+       struct vport *vport = ovs_vport_rtnl_check(dp, OVSP_LOCAL);
        return vport->ops->get_name(vport);
 }
 
@@ -131,7 +132,7 @@ static int get_dpifindex(struct datapath *dp)
 
        rcu_read_lock();
 
-       local = rcu_dereference(dp->ports[OVSP_LOCAL]);
+       local = ovs_vport_rcu(dp, OVSP_LOCAL);
        if (local)
                ifindex = local->ops->get_ifindex(local);
        else
@@ -244,9 +245,30 @@ static void destroy_dp_rcu(struct rcu_head *rcu)
        ovs_flow_tbl_destroy((__force struct flow_table *)dp->table);
        free_percpu(dp->stats_percpu);
        release_net(ovs_dp_get_net(dp));
+       kfree(dp->ports);
        kobject_put(&dp->ifobj);
 }
 
+static struct hlist_head *hash_bucket(const struct datapath *dp, int id)
+{
+       unsigned int hash = hash_32(id, DP_VPORT_HASH_BITS);
+       return &dp->ports[hash];
+}
+
+struct vport *ovs_vport_rcu(const struct datapath *dp, int id)
+{
+       struct vport *vport;
+       struct hlist_node *n;
+       struct hlist_head *head;
+
+       head = hash_bucket(dp, id);
+       hlist_for_each_entry_rcu(vport, n, head, dp_hash_node) {
+               if (vport->port_no == id)
+                       return vport;
+       }
+       return NULL;
+}
+
 /* Called with RTNL lock and genl_lock. */
 static struct vport *new_vport(const struct vport_parms *parms)
 {
@@ -256,12 +278,11 @@ static struct vport *new_vport(const struct vport_parms 
*parms)
        if (!IS_ERR(vport)) {
                struct datapath *dp = parms->dp;
 
-               rcu_assign_pointer(dp->ports[parms->port_no], vport);
-               list_add(&vport->node, &dp->port_list);
+               hlist_add_head_rcu(&vport->dp_hash_node, hash_bucket(dp, 
vport->port_no));
 
                dp_ifinfo_notify(RTM_NEWLINK, vport);
+               dp->port_count++;
        }
-
        return vport;
 }
 
@@ -272,11 +293,12 @@ void ovs_dp_detach_port(struct vport *p)
 
        if (p->port_no != OVSP_LOCAL)
                ovs_dp_sysfs_del_if(p);
+
+       p->dp->port_count--;
        dp_ifinfo_notify(RTM_DELLINK, p);
 
        /* First drop references to device. */
-       list_del(&p->node);
-       rcu_assign_pointer(p->dp->ports[p->port_no], NULL);
+       hlist_del_rcu(&p->dp_hash_node);
 
        /* Then destroy it. */
        ovs_vport_del(p);
@@ -660,11 +682,8 @@ static int validate_actions(const struct nlattr *attr,
                        break;
 
                case OVS_ACTION_ATTR_OUTPUT:
-                       if (nla_get_u32(a) >= DP_MAX_PORTS)
-                               return -EINVAL;
                        break;
 
-
                case OVS_ACTION_ATTR_POP_VLAN:
                        break;
 
@@ -1354,7 +1373,7 @@ static int ovs_dp_cmd_new(struct sk_buff *skb, struct 
genl_info *info)
        struct datapath *dp;
        struct vport *vport;
        struct ovs_net *ovs_net;
-       int err;
+       int err, i;
 
        err = -EINVAL;
        if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
@@ -1371,8 +1390,6 @@ static int ovs_dp_cmd_new(struct sk_buff *skb, struct 
genl_info *info)
        if (dp == NULL)
                goto err_unlock_rtnl;
 
-       INIT_LIST_HEAD(&dp->port_list);
-
        /* Initialize kobject for bridge.  This will be added as
         * /sys/class/net/<devname>/brif later, if sysfs is enabled. */
        dp->ifobj.kset = NULL;
@@ -1391,6 +1408,16 @@ static int ovs_dp_cmd_new(struct sk_buff *skb, struct 
genl_info *info)
        }
        ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
 
+       dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
+                           GFP_KERNEL);
+       if (!dp->ports) {
+               err = -ENOMEM;
+               goto err_destroy_percpu;
+       }
+
+       for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
+               INIT_HLIST_HEAD(&dp->ports[i]);
+
        /* Set up our datapath device. */
        parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
        parms.type = OVS_VPORT_TYPE_INTERNAL;
@@ -1405,7 +1432,7 @@ static int ovs_dp_cmd_new(struct sk_buff *skb, struct 
genl_info *info)
                if (err == -EBUSY)
                        err = -EEXIST;
 
-               goto err_destroy_percpu;
+               goto err_destroy_ports_array;
        }
 
        reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
@@ -1426,7 +1453,9 @@ static int ovs_dp_cmd_new(struct sk_buff *skb, struct 
genl_info *info)
        return 0;
 
 err_destroy_local_port:
-       ovs_dp_detach_port(rtnl_dereference(dp->ports[OVSP_LOCAL]));
+       ovs_dp_detach_port(ovs_vport_rtnl_protected(dp, OVSP_LOCAL));
+err_destroy_ports_array:
+       kfree(dp->ports);
 err_destroy_percpu:
        free_percpu(dp->stats_percpu);
 err_destroy_table:
@@ -1442,16 +1471,22 @@ err:
 /* Called with genl_mutex. */
 static void __dp_destroy(struct datapath *dp)
 {
-       struct vport *vport, *next_vport;
+       int i;
 
        rtnl_lock();
-       list_for_each_entry_safe(vport, next_vport, &dp->port_list, node)
-               if (vport->port_no != OVSP_LOCAL)
-                       ovs_dp_detach_port(vport);
+
+       for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
+               struct vport *vport;
+               struct hlist_node *n;
+
+               hlist_for_each_entry_rcu(vport, n, &dp->ports[i], dp_hash_node)
+                       if (vport->port_no != OVSP_LOCAL)
+                               ovs_dp_detach_port(vport);
+       }
 
        ovs_dp_sysfs_del_dp(dp);
        list_del(&dp->list_node);
-       ovs_dp_detach_port(rtnl_dereference(dp->ports[OVSP_LOCAL]));
+       ovs_dp_detach_port(ovs_vport_rtnl_protected(dp, OVSP_LOCAL));
 
        /* rtnl_unlock() will wait until all the references to devices that
         * are pending unregistration have been dropped.  We do it here to
@@ -1698,14 +1733,11 @@ static struct vport *lookup_vport(struct net *net,
        } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
                u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
 
-               if (port_no >= DP_MAX_PORTS)
-                       return ERR_PTR(-EFBIG);
-
                dp = get_dp(net, ovs_header->dp_ifindex);
                if (!dp)
                        return ERR_PTR(-ENODEV);
 
-               vport = rcu_dereference_rtnl(dp->ports[port_no]);
+               vport = ovs_vport_rtnl_check(dp, port_no);
                if (!vport)
                        return ERR_PTR(-ENOENT);
                return vport;
@@ -1754,27 +1786,25 @@ static int ovs_vport_cmd_new(struct sk_buff *skb, 
struct genl_info *info)
        if (!dp)
                goto exit_unlock;
 
+       if (dp->port_count >= DP_MAX_PORTS_COUNT) {
+               err = -EFBIG;
+               goto exit_unlock;
+       }
+
        if (a[OVS_VPORT_ATTR_PORT_NO]) {
                port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
 
-               err = -EFBIG;
-               if (port_no >= DP_MAX_PORTS)
-                       goto exit_unlock;
-
-               vport = rtnl_dereference(dp->ports[port_no]);
+               vport = ovs_vport_rtnl_protected(dp, port_no);
                err = -EBUSY;
                if (vport)
                        goto exit_unlock;
        } else {
-               for (port_no = 1; ; port_no++) {
-                       if (port_no >= DP_MAX_PORTS) {
-                               err = -EFBIG;
-                               goto exit_unlock;
-                       }
-                       vport = rtnl_dereference(dp->ports[port_no]);
+               for (port_no = 1; port_no < DP_MAX_PORTS_COUNT; port_no++) {
+                       vport = ovs_vport_rtnl_protected(dp, port_no);
                        if (!vport)
                                break;
                }
+               BUG_ON(vport);
        }
 
        parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
@@ -1936,32 +1966,34 @@ static int ovs_vport_cmd_dump(struct sk_buff *skb, 
struct netlink_callback *cb)
 {
        struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
        struct datapath *dp;
-       u32 port_no;
-       int retval;
+       u32 port_idx = 0, skip = cb->args[0];
+       int i;
 
        dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
        if (!dp)
                return -ENODEV;
 
        rcu_read_lock();
-       for (port_no = cb->args[0]; port_no < DP_MAX_PORTS; port_no++) {
+       for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
                struct vport *vport;
+               struct hlist_node *n;
 
-               vport = rcu_dereference(dp->ports[port_no]);
-               if (!vport)
-                       continue;
+               hlist_for_each_entry_rcu(vport, n, &dp->ports[i], dp_hash_node) 
{
+                       if (port_idx >= skip &&
+                           ovs_vport_cmd_fill_info(vport, skb, 
NETLINK_CB(cb->skb).pid,
+                                                   cb->nlh->nlmsg_seq, 
NLM_F_MULTI,
+                                                   OVS_VPORT_CMD_NEW) < 0)
+                               goto out;
 
-               if (ovs_vport_cmd_fill_info(vport, skb, NETLINK_CB(cb->skb).pid,
-                                           cb->nlh->nlmsg_seq, NLM_F_MULTI,
-                                           OVS_VPORT_CMD_NEW) < 0)
-                       break;
+                       port_idx++;
+               }
        }
+out:
        rcu_read_unlock();
 
-       cb->args[0] = port_no;
-       retval = skb->len;
+       cb->args[0] = port_idx;
 
-       return retval;
+       return skb->len;
 }
 
 static struct genl_ops dp_vport_genl_ops[] = {
diff --git a/datapath/datapath.h b/datapath/datapath.h
index b012a76..d76222f 100644
--- a/datapath/datapath.h
+++ b/datapath/datapath.h
@@ -34,7 +34,10 @@
 #include "vlan.h"
 #include "vport.h"
 
-#define DP_MAX_PORTS 1024
+#define DP_MAX_PORTS_COUNT     (1024 * 64)
+#define DP_VPORT_HASH_BITS     10
+#define DP_VPORT_HASH_BUCKETS  (2 << DP_VPORT_HASH_BITS)
+
 #define SAMPLE_ACTION_DEPTH 3
 
 /**
@@ -63,10 +66,9 @@ struct dp_stats_percpu {
  * @ifobj: Represents /sys/class/net/<devname>/brif.  Protected by RTNL.
  * @n_flows: Number of flows currently in flow table.
  * @table: Current flow table.  Protected by genl_lock and RCU.
- * @ports: Map from port number to &struct vport.  %OVSP_LOCAL port
- * always exists, other ports may be %NULL.  Protected by RTNL and RCU.
- * @port_list: List of all ports in @ports in arbitrary order.  RTNL required
- * to iterate or modify.
+ * @ports: Hash table for ports.  %OVSP_LOCAL port always exists. Protected by
+ * RTNL and RCU.
+ * @port_count: Number of ports on datapath.
  * @stats_percpu: Per-CPU datapath statistics.
  * @net: Reference to net namespace.
  *
@@ -82,8 +84,8 @@ struct datapath {
        struct flow_table __rcu *table;
 
        /* Switch ports. */
-       struct vport __rcu *ports[DP_MAX_PORTS];
-       struct list_head port_list;
+       struct hlist_head *ports;
+       int port_count;
 
        /* Stats. */
        struct dp_stats_percpu __percpu *stats_percpu;
@@ -159,6 +161,20 @@ static inline void ovs_dp_set_net(struct datapath *dp, 
struct net *net)
        write_pnet(&dp->net, net);
 }
 
+struct vport *ovs_vport_rcu(const struct datapath *dp, int id);
+
+static inline struct vport *ovs_vport_rtnl_check(const struct datapath *dp, 
int id)
+{
+       WARN_ON(!rcu_read_lock_held() && !rtnl_is_locked());
+       return ovs_vport_rcu(dp, id);
+}
+
+static inline struct vport *ovs_vport_rtnl_protected(const struct datapath 
*dp, int id)
+{
+       ASSERT_RTNL();
+       return ovs_vport_rcu(dp, id);
+}
+
 extern struct notifier_block ovs_dp_device_notifier;
 extern struct genl_multicast_group ovs_dp_vport_multicast_group;
 extern int (*ovs_dp_ioctl_hook)(struct net_device *dev, struct ifreq *rq, int 
cmd);
diff --git a/datapath/dp_sysfs_dp.c b/datapath/dp_sysfs_dp.c
index 2582321..2969852 100644
--- a/datapath/dp_sysfs_dp.c
+++ b/datapath/dp_sysfs_dp.c
@@ -362,7 +362,7 @@ static struct attribute_group bridge_group = {
  */
 int ovs_dp_sysfs_add_dp(struct datapath *dp)
 {
-       struct vport *vport = rtnl_dereference(dp->ports[OVSP_LOCAL]);
+       struct vport *vport = ovs_vport_rtnl_protected(dp, OVSP_LOCAL);
        struct kobject *kobj = vport->ops->get_kobj(vport);
        int err;
 
@@ -398,7 +398,7 @@ int ovs_dp_sysfs_add_dp(struct datapath *dp)
 
 int ovs_dp_sysfs_del_dp(struct datapath *dp)
 {
-       struct vport *vport = rtnl_dereference(dp->ports[OVSP_LOCAL]);
+       struct vport *vport = ovs_vport_rtnl_protected(dp, OVSP_LOCAL);
        struct kobject *kobj = vport->ops->get_kobj(vport);
 
 #ifdef CONFIG_NET_NS
diff --git a/datapath/dp_sysfs_if.c b/datapath/dp_sysfs_if.c
index f564e98..10beade 100644
--- a/datapath/dp_sysfs_if.c
+++ b/datapath/dp_sysfs_if.c
@@ -209,7 +209,7 @@ struct sysfs_ops ovs_brport_sysfs_ops = {
 int ovs_dp_sysfs_add_if(struct vport *p)
 {
        struct datapath *dp = p->dp;
-       struct vport *local_port = rtnl_dereference(dp->ports[OVSP_LOCAL]);
+       struct vport *local_port = ovs_vport_rtnl_protected(dp, OVSP_LOCAL);
        struct brport_attribute **a;
        int err;
 
diff --git a/datapath/flow.c b/datapath/flow.c
index 823c6b5..c15f6f0 100644
--- a/datapath/flow.c
+++ b/datapath/flow.c
@@ -203,10 +203,10 @@ struct sw_flow_actions *ovs_flow_actions_alloc(const 
struct nlattr *actions)
        int actions_len = nla_len(actions);
        struct sw_flow_actions *sfa;
 
-       /* At least DP_MAX_PORTS actions are required to be able to flood a
+       /* At least DP_MAX_PORTS_COUNT actions are required to be able to flood 
a
         * packet to every port.  Factor of 2 allows for setting VLAN tags,
         * etc. */
-       if (actions_len > 2 * DP_MAX_PORTS * nla_total_size(4))
+       if (actions_len > 2 * DP_MAX_PORTS_COUNT * nla_total_size(4))
                return ERR_PTR(-EINVAL);
 
        sfa = kmalloc(sizeof(*sfa) + actions_len, GFP_KERNEL);
@@ -1015,8 +1015,6 @@ int ovs_flow_from_nlattrs(struct sw_flow_key *swkey, int 
*key_lenp,
        }
        if (attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
                u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
-               if (in_port >= DP_MAX_PORTS)
-                       return -EINVAL;
                swkey->phy.in_port = in_port;
                attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
        } else {
@@ -1190,8 +1188,6 @@ int ovs_flow_metadata_from_nlattrs(u32 *priority, u16 
*in_port, __be64 *tun_id,
                                break;
 
                        case OVS_KEY_ATTR_IN_PORT:
-                               if (nla_get_u32(nla) >= DP_MAX_PORTS)
-                                       return -EINVAL;
                                *in_port = nla_get_u32(nla);
                                break;
                        }
diff --git a/datapath/linux/Modules.mk b/datapath/linux/Modules.mk
index 7f54bde..4c8926d 100644
--- a/datapath/linux/Modules.mk
+++ b/datapath/linux/Modules.mk
@@ -20,6 +20,7 @@ openvswitch_headers += \
        linux/compat/include/linux/err.h \
        linux/compat/include/linux/flex_array.h \
        linux/compat/include/linux/genetlink.h \
+       linux/compat/include/linux/hash.h \
        linux/compat/include/linux/icmp.h \
        linux/compat/include/linux/icmpv6.h \
        linux/compat/include/linux/if.h \
diff --git a/datapath/linux/compat/include/linux/hash.h 
b/datapath/linux/compat/include/linux/hash.h
new file mode 100644
index 0000000..b909ac6
--- /dev/null
+++ b/datapath/linux/compat/include/linux/hash.h
@@ -0,0 +1,19 @@
+#ifndef __HASH_WRAPPER_H
+#define __HASH_WRAPPER_H 1
+
+#include_next <linux/hash.h>
+
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
+/* 2^31 + 2^29 - 2^25 + 2^22 - 2^19 - 2^16 + 1 */
+#define GOLDEN_RATIO_PRIME_32 0x9e370001UL
+
+static inline u32 hash_32(u32 val, unsigned int bits)
+{
+       /* On some cpus multiply is faster, on others gcc will do shifts */
+       u32 hash = val * GOLDEN_RATIO_PRIME_32;
+
+       /* High bits are more random, so use them. */
+       return hash >> (32 - bits);
+}
+#endif
+#endif /* linux/hash.h wrapper */
diff --git a/datapath/vport.c b/datapath/vport.c
index d81f686..b75a866 100644
--- a/datapath/vport.c
+++ b/datapath/vport.c
@@ -192,6 +192,7 @@ struct vport *ovs_vport_alloc(int priv_size, const struct 
vport_ops *ops,
        vport->port_no = parms->port_no;
        vport->upcall_pid = parms->upcall_pid;
        vport->ops = ops;
+       INIT_HLIST_NODE(&vport->dp_hash_node);
 
        /* Initialize kobject for bridge.  This will be added as
         * /sys/class/net/<devname>/brport later, if sysfs is enabled. */
diff --git a/datapath/vport.h b/datapath/vport.h
index ee9715d..2aafde0 100644
--- a/datapath/vport.h
+++ b/datapath/vport.h
@@ -84,6 +84,7 @@ struct vport_err_stats {
  * @upcall_pid: The Netlink port to use for packets received on this port that
  * miss the flow table.
  * @hash_node: Element in @dev_table hash table in vport.c.
+ * @dp_hash_node: Element in @datapath->ports hash table in datapath.c.
  * @ops: Class structure.
  * @percpu_stats: Points to per-CPU statistics used and maintained by vport
  * @stats_lock: Protects @err_stats and @offset_stats.
@@ -101,6 +102,7 @@ struct vport {
        u32 upcall_pid;
 
        struct hlist_node hash_node;
+       struct hlist_node dp_hash_node;
        const struct vport_ops *ops;
 
        struct vport_percpu_stats __percpu *percpu_stats;
diff --git a/lib/dpif-linux.c b/lib/dpif-linux.c
index c06bb89..30cfe2f 100644
--- a/lib/dpif-linux.c
+++ b/lib/dpif-linux.c
@@ -60,10 +60,6 @@
 
 VLOG_DEFINE_THIS_MODULE(dpif_linux);
 
-enum { LRU_MAX_PORTS = 1024 };
-enum { LRU_MASK = LRU_MAX_PORTS - 1};
-BUILD_ASSERT_DECL(IS_POW2(LRU_MAX_PORTS));
-
 enum { N_UPCALL_SOCKS = 16 };
 BUILD_ASSERT_DECL(IS_POW2(N_UPCALL_SOCKS));
 BUILD_ASSERT_DECL(N_UPCALL_SOCKS <= 32); /* We use a 32-bit word as a mask. */
@@ -146,12 +142,6 @@ struct dpif_linux {
     struct sset changed_ports;  /* Ports that have changed. */
     struct nln_notifier *port_notifier;
     bool change_error;
-
-    /* Queue of unused ports. */
-    unsigned long *lru_bitmap;
-    uint16_t lru_ports[LRU_MAX_PORTS];
-    size_t lru_head;
-    size_t lru_tail;
 };
 
 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
@@ -171,6 +161,7 @@ static void open_dpif(const struct dpif_linux_dp *, struct 
dpif **);
 static bool dpif_linux_nln_parse(struct ofpbuf *, void *);
 static void dpif_linux_port_changed(const void *vport, void *dpif);
 static uint32_t dpif_linux_port_get_pid(const struct dpif *, uint16_t port_no);
+static int set_upcall_pid(const struct dpif *, int);
 
 static void dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *,
                                        struct ofpbuf *);
@@ -184,29 +175,6 @@ dpif_linux_cast(const struct dpif *dpif)
     return CONTAINER_OF(dpif, struct dpif_linux, dpif);
 }
 
-static void
-dpif_linux_push_port(struct dpif_linux *dp, uint16_t port)
-{
-    if (port < LRU_MAX_PORTS && !bitmap_is_set(dp->lru_bitmap, port)) {
-        bitmap_set1(dp->lru_bitmap, port);
-        dp->lru_ports[dp->lru_head++ & LRU_MASK] = port;
-    }
-}
-
-static uint32_t
-dpif_linux_pop_port(struct dpif_linux *dp)
-{
-    uint16_t port;
-
-    if (dp->lru_head == dp->lru_tail) {
-        return UINT32_MAX;
-    }
-
-    port = dp->lru_ports[dp->lru_tail++ & LRU_MASK];
-    bitmap_set0(dp->lru_bitmap, port);
-    return port;
-}
-
 static int
 dpif_linux_enumerate(struct sset *all_dps)
 {
@@ -268,7 +236,6 @@ static void
 open_dpif(const struct dpif_linux_dp *dp, struct dpif **dpifp)
 {
     struct dpif_linux *dpif;
-    int i;
 
     dpif = xzalloc(sizeof *dpif);
     dpif->port_notifier = nln_notifier_create(nln, dpif_linux_port_changed,
@@ -281,12 +248,6 @@ open_dpif(const struct dpif_linux_dp *dp, struct dpif 
**dpifp)
     dpif->dp_ifindex = dp->dp_ifindex;
     sset_init(&dpif->changed_ports);
     *dpifp = &dpif->dpif;
-
-    dpif->lru_bitmap = bitmap_allocate(LRU_MAX_PORTS);
-    bitmap_set1(dpif->lru_bitmap, OVSP_LOCAL);
-    for (i = 1; i < LRU_MAX_PORTS; i++) {
-        dpif_linux_push_port(dpif, i);
-    }
 }
 
 static void
@@ -312,7 +273,6 @@ dpif_linux_close(struct dpif *dpif_)
     nln_notifier_destroy(dpif->port_notifier);
     destroy_upcall_socks(dpif);
     sset_destroy(&dpif->changed_ports);
-    free(dpif->lru_bitmap);
     free(dpif);
 }
 
@@ -363,6 +323,22 @@ dpif_linux_get_stats(const struct dpif *dpif_, struct 
dpif_dp_stats *stats)
 }
 
 static int
+dpif_linux_port_del(struct dpif *dpif_, uint16_t port_no)
+{
+    struct dpif_linux *dpif = dpif_linux_cast(dpif_);
+    struct dpif_linux_vport vport;
+    int error;
+
+    dpif_linux_vport_init(&vport);
+    vport.cmd = OVS_VPORT_CMD_DEL;
+    vport.dp_ifindex = dpif->dp_ifindex;
+    vport.port_no = port_no;
+    error = dpif_linux_vport_transact(&vport, NULL, NULL);
+
+    return error;
+}
+
+static int
 dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
                     uint16_t *port_nop)
 {
@@ -372,6 +348,7 @@ dpif_linux_port_add(struct dpif *dpif_, struct netdev 
*netdev,
     struct dpif_linux_vport request, reply;
     const struct ofpbuf *options;
     struct ofpbuf *buf;
+    uint32_t upcall_pid;
     int error;
 
     dpif_linux_vport_init(&request);
@@ -396,43 +373,20 @@ dpif_linux_port_add(struct dpif *dpif_, struct netdev 
*netdev,
         netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
     }
 
-    /* Loop until we find a port that isn't used. */
-    do {
-        uint32_t upcall_pid;
-
-        request.port_no = dpif_linux_pop_port(dpif);
-        upcall_pid = dpif_linux_port_get_pid(dpif_, request.port_no);
-        request.upcall_pid = &upcall_pid;
-        error = dpif_linux_vport_transact(&request, &reply, &buf);
-
-        if (!error) {
-            *port_nop = reply.port_no;
-            VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
-                     dpif_name(dpif_), request.port_no, upcall_pid);
-        }
-        ofpbuf_delete(buf);
-    } while (request.port_no != UINT32_MAX
-             && (error == EBUSY || error == EFBIG));
-
-    return error;
-}
-
-static int
-dpif_linux_port_del(struct dpif *dpif_, uint16_t port_no)
-{
-    struct dpif_linux *dpif = dpif_linux_cast(dpif_);
-    struct dpif_linux_vport vport;
-    int error;
-
-    dpif_linux_vport_init(&vport);
-    vport.cmd = OVS_VPORT_CMD_DEL;
-    vport.dp_ifindex = dpif->dp_ifindex;
-    vport.port_no = port_no;
-    error = dpif_linux_vport_transact(&vport, NULL, NULL);
+    request.port_no = UINT32_MAX;
+    upcall_pid = dpif_linux_port_get_pid(dpif_, request.port_no);
+    request.upcall_pid = &upcall_pid;
+    error = dpif_linux_vport_transact(&request, &reply, &buf);
 
     if (!error) {
-        dpif_linux_push_port(dpif, port_no);
+        *port_nop = reply.port_no;
+        error = set_upcall_pid(dpif_, reply.port_no);
+        if (error) {
+            dpif_linux_port_del(dpif_, reply.port_no);
+        }
     }
+    ofpbuf_delete(buf);
+
     return error;
 }
 
@@ -478,9 +432,7 @@ dpif_linux_port_query_by_name(const struct dpif *dpif, 
const char *devname,
 static int
 dpif_linux_get_max_ports(const struct dpif *dpif OVS_UNUSED)
 {
-    /* If the datapath increases its range of supported ports, then it should
-     * start reporting that. */
-    return 1024;
+    return UINT32_MAX;
 }
 
 static uint32_t
@@ -510,8 +462,6 @@ dpif_linux_flow_flush(struct dpif *dpif_)
 
 struct dpif_linux_port_state {
     struct nl_dump dump;
-    unsigned long *port_bitmap; /* Ports in the datapath. */
-    bool complete;              /* Dump completed without error. */
 };
 
 static int
@@ -523,8 +473,6 @@ dpif_linux_port_dump_start(const struct dpif *dpif_, void 
**statep)
     struct ofpbuf *buf;
 
     *statep = state = xmalloc(sizeof *state);
-    state->port_bitmap = bitmap_allocate(LRU_MAX_PORTS);
-    state->complete = false;
 
     dpif_linux_vport_init(&request);
     request.cmd = OVS_DP_CMD_GET;
@@ -548,7 +496,6 @@ dpif_linux_port_dump_next(const struct dpif *dpif 
OVS_UNUSED, void *state_,
     int error;
 
     if (!nl_dump_next(&state->dump, &buf)) {
-        state->complete = true;
         return EOF;
     }
 
@@ -557,10 +504,6 @@ dpif_linux_port_dump_next(const struct dpif *dpif 
OVS_UNUSED, void *state_,
         return error;
     }
 
-    if (vport.port_no < LRU_MAX_PORTS) {
-        bitmap_set1(state->port_bitmap, vport.port_no);
-    }
-
     dpif_port->name = (char *) vport.name;
     dpif_port->type = (char *) netdev_vport_get_netdev_type(&vport);
     dpif_port->port_no = vport.port_no;
@@ -568,23 +511,11 @@ dpif_linux_port_dump_next(const struct dpif *dpif 
OVS_UNUSED, void *state_,
 }
 
 static int
-dpif_linux_port_dump_done(const struct dpif *dpif_, void *state_)
+dpif_linux_port_dump_done(const struct dpif *dpif_ OVS_UNUSED, void *state_)
 {
-    struct dpif_linux *dpif = dpif_linux_cast(dpif_);
     struct dpif_linux_port_state *state = state_;
     int error = nl_dump_done(&state->dump);
 
-    if (state->complete) {
-        uint16_t i;
-
-        for (i = 0; i < LRU_MAX_PORTS; i++) {
-            if (!bitmap_is_set(state->port_bitmap, i)) {
-                dpif_linux_push_port(dpif, i);
-            }
-        }
-    }
-
-    free(state->port_bitmap);
     free(state);
     return error;
 }
@@ -929,32 +860,40 @@ dpif_linux_operate(struct dpif *dpif_, struct dpif_op 
**ops, size_t n_ops)
     free(txns);
 }
 
+static int
+set_upcall_pid(const struct dpif *dpif_, int port_no)
+{
+    struct dpif_linux *dpif = dpif_linux_cast(dpif_);
+    uint32_t upcall_pid = dpif_linux_port_get_pid(dpif_, port_no);
+    struct dpif_linux_vport vport_request;
+    int error;
+
+    dpif_linux_vport_init(&vport_request);
+    vport_request.cmd = OVS_VPORT_CMD_SET;
+    vport_request.dp_ifindex = dpif->dp_ifindex;
+    vport_request.port_no = port_no;
+    vport_request.upcall_pid = &upcall_pid;
+    error = dpif_linux_vport_transact(&vport_request, NULL, NULL);
+    if (!error) {
+        VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
+                 dpif_name(&dpif->dpif), vport_request.port_no,
+                 upcall_pid);
+    } else {
+        VLOG_WARN_RL(&error_rl, "%s: failed to set upcall pid on port: %s",
+                     dpif_name(&dpif->dpif), strerror(error));
+    }
+    return error;
+}
+
 static void
 set_upcall_pids(struct dpif *dpif_)
 {
     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
     struct dpif_port_dump port_dump;
     struct dpif_port port;
-    int error;
 
     DPIF_PORT_FOR_EACH (&port, &port_dump, &dpif->dpif) {
-        uint32_t upcall_pid = dpif_linux_port_get_pid(dpif_, port.port_no);
-        struct dpif_linux_vport vport_request;
-
-        dpif_linux_vport_init(&vport_request);
-        vport_request.cmd = OVS_VPORT_CMD_SET;
-        vport_request.dp_ifindex = dpif->dp_ifindex;
-        vport_request.port_no = port.port_no;
-        vport_request.upcall_pid = &upcall_pid;
-        error = dpif_linux_vport_transact(&vport_request, NULL, NULL);
-        if (!error) {
-            VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
-                     dpif_name(&dpif->dpif), vport_request.port_no,
-                     upcall_pid);
-        } else {
-            VLOG_WARN_RL(&error_rl, "%s: failed to set upcall pid on port: %s",
-                         dpif_name(&dpif->dpif), strerror(error));
-        }
+        set_upcall_pid(dpif_, port.port_no);
     }
 }
 
-- 
1.7.1

_______________________________________________
dev mailing list
[email protected]
http://openvswitch.org/mailman/listinfo/dev

Reply via email to