aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEric Dumazet <eric.dumazet@gmail.com>2009-10-24 09:13:17 -0400
committerDavid S. Miller <davem@davemloft.net>2009-10-24 09:13:17 -0400
commit7c28bd0b8ec4d128bd7660671d1b626b0abc471f (patch)
treedbde8ba53e2059dbd6cb19aca18133b33112ddf5
parent8d5b2c084d2e71587e30a6ef528a8a8051e59dcd (diff)
rtnetlink: speedup rtnl_dump_ifinfo()
When handling large number of netdevice, rtnl_dump_ifinfo() is very slow because it has O(N^2) complexity. Instead of scanning one single list, we can use the 256 sub lists of the dev_index hash table. This considerably speedups "ip link" operations Signed-off-by: Eric Dumazet <eric.dumazet@gmail.com> Signed-off-by: David S. Miller <davem@davemloft.net>
-rw-r--r--include/net/net_namespace.h4
-rw-r--r--net/core/dev.c7
-rw-r--r--net/core/rtnetlink.c37
3 files changed, 30 insertions, 18 deletions
diff --git a/include/net/net_namespace.h b/include/net/net_namespace.h
index 699410142bfa..0addd45038ac 100644
--- a/include/net/net_namespace.h
+++ b/include/net/net_namespace.h
@@ -28,6 +28,10 @@ struct ctl_table_header;
28struct net_generic; 28struct net_generic;
29struct sock; 29struct sock;
30 30
31
32#define NETDEV_HASHBITS 8
33#define NETDEV_HASHENTRIES (1 << NETDEV_HASHBITS)
34
31struct net { 35struct net {
32 atomic_t count; /* To decided when the network 36 atomic_t count; /* To decided when the network
33 * namespace should be freed. 37 * namespace should be freed.
diff --git a/net/core/dev.c b/net/core/dev.c
index fa88dcd476d6..e7bada1d5ed9 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -193,18 +193,15 @@ static struct list_head ptype_all __read_mostly; /* Taps */
193DEFINE_RWLOCK(dev_base_lock); 193DEFINE_RWLOCK(dev_base_lock);
194EXPORT_SYMBOL(dev_base_lock); 194EXPORT_SYMBOL(dev_base_lock);
195 195
196#define NETDEV_HASHBITS 8
197#define NETDEV_HASHENTRIES (1 << NETDEV_HASHBITS)
198
199static inline struct hlist_head *dev_name_hash(struct net *net, const char *name) 196static inline struct hlist_head *dev_name_hash(struct net *net, const char *name)
200{ 197{
201 unsigned hash = full_name_hash(name, strnlen(name, IFNAMSIZ)); 198 unsigned hash = full_name_hash(name, strnlen(name, IFNAMSIZ));
202 return &net->dev_name_head[hash & ((1 << NETDEV_HASHBITS) - 1)]; 199 return &net->dev_name_head[hash & (NETDEV_HASHENTRIES - 1)];
203} 200}
204 201
205static inline struct hlist_head *dev_index_hash(struct net *net, int ifindex) 202static inline struct hlist_head *dev_index_hash(struct net *net, int ifindex)
206{ 203{
207 return &net->dev_index_head[ifindex & ((1 << NETDEV_HASHBITS) - 1)]; 204 return &net->dev_index_head[ifindex & (NETDEV_HASHENTRIES - 1)];
208} 205}
209 206
210/* Device list insertion */ 207/* Device list insertion */
diff --git a/net/core/rtnetlink.c b/net/core/rtnetlink.c
index ba13b0974a7b..52ea418d5302 100644
--- a/net/core/rtnetlink.c
+++ b/net/core/rtnetlink.c
@@ -682,22 +682,33 @@ nla_put_failure:
682static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 682static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
683{ 683{
684 struct net *net = sock_net(skb->sk); 684 struct net *net = sock_net(skb->sk);
685 int idx; 685 int h, s_h;
686 int s_idx = cb->args[0]; 686 int idx = 0, s_idx;
687 struct net_device *dev; 687 struct net_device *dev;
688 688 struct hlist_head *head;
689 idx = 0; 689 struct hlist_node *node;
690 for_each_netdev(net, dev) { 690
691 if (idx < s_idx) 691 s_h = cb->args[0];
692 goto cont; 692 s_idx = cb->args[1];
693 if (rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK, 693
694 NETLINK_CB(cb->skb).pid, 694 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
695 cb->nlh->nlmsg_seq, 0, NLM_F_MULTI) <= 0) 695 idx = 0;
696 break; 696 head = &net->dev_index_head[h];
697 hlist_for_each_entry(dev, node, head, index_hlist) {
698 if (idx < s_idx)
699 goto cont;
700 if (rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
701 NETLINK_CB(cb->skb).pid,
702 cb->nlh->nlmsg_seq, 0,
703 NLM_F_MULTI) <= 0)
704 goto out;
697cont: 705cont:
698 idx++; 706 idx++;
707 }
699 } 708 }
700 cb->args[0] = idx; 709out:
710 cb->args[1] = idx;
711 cb->args[0] = h;
701 712
702 return skb->len; 713 return skb->len;
703} 714}