Phonet: put sockets in a hash table

Signed-off-by: Rémi Denis-Courmont <remi.denis-courmont@nokia.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
This commit is contained in:
Rémi Denis-Courmont 2009-11-09 02:17:01 +00:00 committed by David S. Miller
parent 4a9b5e5053
commit 6b0d07ba15
3 changed files with 56 additions and 25 deletions

View file

@ -46,6 +46,7 @@ static inline struct pn_sock *pn_sk(struct sock *sk)
extern const struct proto_ops phonet_dgram_ops; extern const struct proto_ops phonet_dgram_ops;
void pn_sock_init(void);
struct sock *pn_find_sock_by_sa(struct net *net, const struct sockaddr_pn *sa); struct sock *pn_find_sock_by_sa(struct net *net, const struct sockaddr_pn *sa);
void pn_deliver_sock_broadcast(struct net *net, struct sk_buff *skb); void pn_deliver_sock_broadcast(struct net *net, struct sk_buff *skb);
void phonet_get_local_port_range(int *min, int *max); void phonet_get_local_port_range(int *min, int *max);

View file

@ -481,6 +481,7 @@ static int __init phonet_init(void)
if (err) if (err)
return err; return err;
pn_sock_init();
err = sock_register(&phonet_proto_family); err = sock_register(&phonet_proto_family);
if (err) { if (err) {
printk(KERN_ALERT printk(KERN_ALERT

View file

@ -45,13 +45,28 @@ static int pn_socket_release(struct socket *sock)
return 0; return 0;
} }
#define PN_HASHSIZE 16
#define PN_HASHMASK (PN_HASHSIZE-1)
static struct { static struct {
struct hlist_head hlist; struct hlist_head hlist[PN_HASHSIZE];
spinlock_t lock; spinlock_t lock;
} pnsocks = { } pnsocks;
.hlist = HLIST_HEAD_INIT,
.lock = __SPIN_LOCK_UNLOCKED(pnsocks.lock), void __init pn_sock_init(void)
}; {
unsigned i;
for (i = 0; i < PN_HASHSIZE; i++)
INIT_HLIST_HEAD(pnsocks.hlist + i);
spin_lock_init(&pnsocks.lock);
}
static struct hlist_head *pn_hash_list(u16 obj)
{
return pnsocks.hlist + (obj & PN_HASHMASK);
}
/* /*
* Find address based on socket address, match only certain fields. * Find address based on socket address, match only certain fields.
@ -64,10 +79,11 @@ struct sock *pn_find_sock_by_sa(struct net *net, const struct sockaddr_pn *spn)
struct sock *rval = NULL; struct sock *rval = NULL;
u16 obj = pn_sockaddr_get_object(spn); u16 obj = pn_sockaddr_get_object(spn);
u8 res = spn->spn_resource; u8 res = spn->spn_resource;
struct hlist_head *hlist = pn_hash_list(obj);
spin_lock_bh(&pnsocks.lock); spin_lock_bh(&pnsocks.lock);
sk_for_each(sknode, node, &pnsocks.hlist) { sk_for_each(sknode, node, hlist) {
struct pn_sock *pn = pn_sk(sknode); struct pn_sock *pn = pn_sk(sknode);
BUG_ON(!pn->sobject); /* unbound socket */ BUG_ON(!pn->sobject); /* unbound socket */
@ -99,11 +115,15 @@ struct sock *pn_find_sock_by_sa(struct net *net, const struct sockaddr_pn *spn)
/* Deliver a broadcast packet (only in bottom-half) */ /* Deliver a broadcast packet (only in bottom-half) */
void pn_deliver_sock_broadcast(struct net *net, struct sk_buff *skb) void pn_deliver_sock_broadcast(struct net *net, struct sk_buff *skb)
{ {
struct hlist_head *hlist = pnsocks.hlist;
unsigned h;
spin_lock(&pnsocks.lock);
for (h = 0; h < PN_HASHSIZE; h++) {
struct hlist_node *node; struct hlist_node *node;
struct sock *sknode; struct sock *sknode;
spin_lock(&pnsocks.lock); sk_for_each(sknode, node, hlist) {
sk_for_each(sknode, node, &pnsocks.hlist) {
struct sk_buff *clone; struct sk_buff *clone;
if (!net_eq(sock_net(sknode), net)) if (!net_eq(sock_net(sknode), net))
@ -117,13 +137,17 @@ void pn_deliver_sock_broadcast(struct net *net, struct sk_buff *skb)
sk_receive_skb(sknode, clone, 0); sk_receive_skb(sknode, clone, 0);
} }
} }
hlist++;
}
spin_unlock(&pnsocks.lock); spin_unlock(&pnsocks.lock);
} }
void pn_sock_hash(struct sock *sk) void pn_sock_hash(struct sock *sk)
{ {
struct hlist_head *hlist = pn_hash_list(pn_sk(sk)->sobject);
spin_lock_bh(&pnsocks.lock); spin_lock_bh(&pnsocks.lock);
sk_add_node(sk, &pnsocks.hlist); sk_add_node(sk, hlist);
spin_unlock_bh(&pnsocks.lock); spin_unlock_bh(&pnsocks.lock);
} }
EXPORT_SYMBOL(pn_sock_hash); EXPORT_SYMBOL(pn_sock_hash);
@ -439,16 +463,21 @@ EXPORT_SYMBOL(pn_sock_get_port);
static struct sock *pn_sock_get_idx(struct seq_file *seq, loff_t pos) static struct sock *pn_sock_get_idx(struct seq_file *seq, loff_t pos)
{ {
struct net *net = seq_file_net(seq); struct net *net = seq_file_net(seq);
struct hlist_head *hlist = pnsocks.hlist;
struct hlist_node *node; struct hlist_node *node;
struct sock *sknode; struct sock *sknode;
unsigned h;
sk_for_each(sknode, node, &pnsocks.hlist) { for (h = 0; h < PN_HASHSIZE; h++) {
sk_for_each(sknode, node, hlist) {
if (!net_eq(net, sock_net(sknode))) if (!net_eq(net, sock_net(sknode)))
continue; continue;
if (!pos) if (!pos)
return sknode; return sknode;
pos--; pos--;
} }
hlist++;
}
return NULL; return NULL;
} }