aboutsummaryrefslogtreecommitdiffstats
path: root/net/tipc/socket.c
diff options
context:
space:
mode:
authorYing Xue <ying.xue@windriver.com>2015-01-09 02:27:08 -0500
committerDavid S. Miller <davem@davemloft.net>2015-01-12 16:24:33 -0500
commite05b31f4bf8994d49322e9afb004ad479a129db0 (patch)
tree7d413a68ee6fdc5ca81b2eec93323433e61753a8 /net/tipc/socket.c
parent1da465683a93142488a54a9038155f23d6349441 (diff)
tipc: make tipc socket support net namespace
Now tipc socket table is statically allocated as a global variable. Through it, we can look up one socket instance with port ID, insert a new socket instance to the table, and delete a socket from the table. But when tipc supports net namespace, each namespace must own its specific socket table. So the global variable of socket table must be redefined in tipc_net structure. As a concequence, a new socket table will be allocated when a new namespace is created, and a socket table will be deallocated when namespace is destroyed. Signed-off-by: Ying Xue <ying.xue@windriver.com> Tested-by: Tero Aho <Tero.Aho@coriant.com> Reviewed-by: Jon Maloy <jon.maloy@ericsson.com> Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'net/tipc/socket.c')
-rw-r--r--net/tipc/socket.c48
1 files changed, 29 insertions, 19 deletions
diff --git a/net/tipc/socket.c b/net/tipc/socket.c
index 68831453bc0e..accb02cb3527 100644
--- a/net/tipc/socket.c
+++ b/net/tipc/socket.c
@@ -115,7 +115,7 @@ static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
115 struct tipc_name_seq const *seq); 115 struct tipc_name_seq const *seq);
116static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope, 116static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
117 struct tipc_name_seq const *seq); 117 struct tipc_name_seq const *seq);
118static struct tipc_sock *tipc_sk_lookup(u32 portid); 118static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid);
119static int tipc_sk_insert(struct tipc_sock *tsk); 119static int tipc_sk_insert(struct tipc_sock *tsk);
120static void tipc_sk_remove(struct tipc_sock *tsk); 120static void tipc_sk_remove(struct tipc_sock *tsk);
121 121
@@ -179,9 +179,6 @@ static const struct nla_policy tipc_nl_sock_policy[TIPC_NLA_SOCK_MAX + 1] = {
179 * - port reference 179 * - port reference
180 */ 180 */
181 181
182/* Protects tipc socket hash table mutations */
183static struct rhashtable tipc_sk_rht;
184
185static u32 tsk_peer_node(struct tipc_sock *tsk) 182static u32 tsk_peer_node(struct tipc_sock *tsk)
186{ 183{
187 return msg_destnode(&tsk->phdr); 184 return msg_destnode(&tsk->phdr);
@@ -1766,7 +1763,7 @@ int tipc_sk_rcv(struct net *net, struct sk_buff *skb)
1766 u32 dnode; 1763 u32 dnode;
1767 1764
1768 /* Validate destination and message */ 1765 /* Validate destination and message */
1769 tsk = tipc_sk_lookup(dport); 1766 tsk = tipc_sk_lookup(net, dport);
1770 if (unlikely(!tsk)) { 1767 if (unlikely(!tsk)) {
1771 rc = tipc_msg_eval(skb, &dnode); 1768 rc = tipc_msg_eval(skb, &dnode);
1772 goto exit; 1769 goto exit;
@@ -2245,8 +2242,9 @@ static int tipc_sk_show(struct tipc_sock *tsk, char *buf,
2245 return ret; 2242 return ret;
2246} 2243}
2247 2244
2248struct sk_buff *tipc_sk_socks_show(void) 2245struct sk_buff *tipc_sk_socks_show(struct net *net)
2249{ 2246{
2247 struct tipc_net *tn = net_generic(net, tipc_net_id);
2250 const struct bucket_table *tbl; 2248 const struct bucket_table *tbl;
2251 struct rhash_head *pos; 2249 struct rhash_head *pos;
2252 struct sk_buff *buf; 2250 struct sk_buff *buf;
@@ -2265,7 +2263,7 @@ struct sk_buff *tipc_sk_socks_show(void)
2265 pb_len = ULTRA_STRING_MAX_LEN; 2263 pb_len = ULTRA_STRING_MAX_LEN;
2266 2264
2267 rcu_read_lock(); 2265 rcu_read_lock();
2268 tbl = rht_dereference_rcu((&tipc_sk_rht)->tbl, &tipc_sk_rht); 2266 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
2269 for (i = 0; i < tbl->size; i++) { 2267 for (i = 0; i < tbl->size; i++) {
2270 rht_for_each_entry_rcu(tsk, pos, tbl, i, node) { 2268 rht_for_each_entry_rcu(tsk, pos, tbl, i, node) {
2271 spin_lock_bh(&tsk->sk.sk_lock.slock); 2269 spin_lock_bh(&tsk->sk.sk_lock.slock);
@@ -2286,8 +2284,9 @@ struct sk_buff *tipc_sk_socks_show(void)
2286/* tipc_sk_reinit: set non-zero address in all existing sockets 2284/* tipc_sk_reinit: set non-zero address in all existing sockets
2287 * when we go from standalone to network mode. 2285 * when we go from standalone to network mode.
2288 */ 2286 */
2289void tipc_sk_reinit(void) 2287void tipc_sk_reinit(struct net *net)
2290{ 2288{
2289 struct tipc_net *tn = net_generic(net, tipc_net_id);
2291 const struct bucket_table *tbl; 2290 const struct bucket_table *tbl;
2292 struct rhash_head *pos; 2291 struct rhash_head *pos;
2293 struct tipc_sock *tsk; 2292 struct tipc_sock *tsk;
@@ -2295,7 +2294,7 @@ void tipc_sk_reinit(void)
2295 int i; 2294 int i;
2296 2295
2297 rcu_read_lock(); 2296 rcu_read_lock();
2298 tbl = rht_dereference_rcu((&tipc_sk_rht)->tbl, &tipc_sk_rht); 2297 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
2299 for (i = 0; i < tbl->size; i++) { 2298 for (i = 0; i < tbl->size; i++) {
2300 rht_for_each_entry_rcu(tsk, pos, tbl, i, node) { 2299 rht_for_each_entry_rcu(tsk, pos, tbl, i, node) {
2301 spin_lock_bh(&tsk->sk.sk_lock.slock); 2300 spin_lock_bh(&tsk->sk.sk_lock.slock);
@@ -2308,12 +2307,13 @@ void tipc_sk_reinit(void)
2308 rcu_read_unlock(); 2307 rcu_read_unlock();
2309} 2308}
2310 2309
2311static struct tipc_sock *tipc_sk_lookup(u32 portid) 2310static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid)
2312{ 2311{
2312 struct tipc_net *tn = net_generic(net, tipc_net_id);
2313 struct tipc_sock *tsk; 2313 struct tipc_sock *tsk;
2314 2314
2315 rcu_read_lock(); 2315 rcu_read_lock();
2316 tsk = rhashtable_lookup(&tipc_sk_rht, &portid); 2316 tsk = rhashtable_lookup(&tn->sk_rht, &portid);
2317 if (tsk) 2317 if (tsk)
2318 sock_hold(&tsk->sk); 2318 sock_hold(&tsk->sk);
2319 rcu_read_unlock(); 2319 rcu_read_unlock();
@@ -2323,6 +2323,9 @@ static struct tipc_sock *tipc_sk_lookup(u32 portid)
2323 2323
2324static int tipc_sk_insert(struct tipc_sock *tsk) 2324static int tipc_sk_insert(struct tipc_sock *tsk)
2325{ 2325{
2326 struct sock *sk = &tsk->sk;
2327 struct net *net = sock_net(sk);
2328 struct tipc_net *tn = net_generic(net, tipc_net_id);
2326 u32 remaining = (TIPC_MAX_PORT - TIPC_MIN_PORT) + 1; 2329 u32 remaining = (TIPC_MAX_PORT - TIPC_MIN_PORT) + 1;
2327 u32 portid = prandom_u32() % remaining + TIPC_MIN_PORT; 2330 u32 portid = prandom_u32() % remaining + TIPC_MIN_PORT;
2328 2331
@@ -2332,7 +2335,7 @@ static int tipc_sk_insert(struct tipc_sock *tsk)
2332 portid = TIPC_MIN_PORT; 2335 portid = TIPC_MIN_PORT;
2333 tsk->portid = portid; 2336 tsk->portid = portid;
2334 sock_hold(&tsk->sk); 2337 sock_hold(&tsk->sk);
2335 if (rhashtable_lookup_insert(&tipc_sk_rht, &tsk->node)) 2338 if (rhashtable_lookup_insert(&tn->sk_rht, &tsk->node))
2336 return 0; 2339 return 0;
2337 sock_put(&tsk->sk); 2340 sock_put(&tsk->sk);
2338 } 2341 }
@@ -2343,15 +2346,17 @@ static int tipc_sk_insert(struct tipc_sock *tsk)
2343static void tipc_sk_remove(struct tipc_sock *tsk) 2346static void tipc_sk_remove(struct tipc_sock *tsk)
2344{ 2347{
2345 struct sock *sk = &tsk->sk; 2348 struct sock *sk = &tsk->sk;
2349 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
2346 2350
2347 if (rhashtable_remove(&tipc_sk_rht, &tsk->node)) { 2351 if (rhashtable_remove(&tn->sk_rht, &tsk->node)) {
2348 WARN_ON(atomic_read(&sk->sk_refcnt) == 1); 2352 WARN_ON(atomic_read(&sk->sk_refcnt) == 1);
2349 __sock_put(sk); 2353 __sock_put(sk);
2350 } 2354 }
2351} 2355}
2352 2356
2353int tipc_sk_rht_init(void) 2357int tipc_sk_rht_init(struct net *net)
2354{ 2358{
2359 struct tipc_net *tn = net_generic(net, tipc_net_id);
2355 struct rhashtable_params rht_params = { 2360 struct rhashtable_params rht_params = {
2356 .nelem_hint = 192, 2361 .nelem_hint = 192,
2357 .head_offset = offsetof(struct tipc_sock, node), 2362 .head_offset = offsetof(struct tipc_sock, node),
@@ -2364,15 +2369,17 @@ int tipc_sk_rht_init(void)
2364 .shrink_decision = rht_shrink_below_30, 2369 .shrink_decision = rht_shrink_below_30,
2365 }; 2370 };
2366 2371
2367 return rhashtable_init(&tipc_sk_rht, &rht_params); 2372 return rhashtable_init(&tn->sk_rht, &rht_params);
2368} 2373}
2369 2374
2370void tipc_sk_rht_destroy(void) 2375void tipc_sk_rht_destroy(struct net *net)
2371{ 2376{
2377 struct tipc_net *tn = net_generic(net, tipc_net_id);
2378
2372 /* Wait for socket readers to complete */ 2379 /* Wait for socket readers to complete */
2373 synchronize_net(); 2380 synchronize_net();
2374 2381
2375 rhashtable_destroy(&tipc_sk_rht); 2382 rhashtable_destroy(&tn->sk_rht);
2376} 2383}
2377 2384
2378/** 2385/**
@@ -2730,10 +2737,12 @@ int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb)
2730 struct rhash_head *pos; 2737 struct rhash_head *pos;
2731 u32 prev_portid = cb->args[0]; 2738 u32 prev_portid = cb->args[0];
2732 u32 portid = prev_portid; 2739 u32 portid = prev_portid;
2740 struct net *net = sock_net(skb->sk);
2741 struct tipc_net *tn = net_generic(net, tipc_net_id);
2733 int i; 2742 int i;
2734 2743
2735 rcu_read_lock(); 2744 rcu_read_lock();
2736 tbl = rht_dereference_rcu((&tipc_sk_rht)->tbl, &tipc_sk_rht); 2745 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
2737 for (i = 0; i < tbl->size; i++) { 2746 for (i = 0; i < tbl->size; i++) {
2738 rht_for_each_entry_rcu(tsk, pos, tbl, i, node) { 2747 rht_for_each_entry_rcu(tsk, pos, tbl, i, node) {
2739 spin_lock_bh(&tsk->sk.sk_lock.slock); 2748 spin_lock_bh(&tsk->sk.sk_lock.slock);
@@ -2839,6 +2848,7 @@ int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb)
2839 u32 tsk_portid = cb->args[0]; 2848 u32 tsk_portid = cb->args[0];
2840 u32 last_publ = cb->args[1]; 2849 u32 last_publ = cb->args[1];
2841 u32 done = cb->args[2]; 2850 u32 done = cb->args[2];
2851 struct net *net = sock_net(skb->sk);
2842 struct tipc_sock *tsk; 2852 struct tipc_sock *tsk;
2843 2853
2844 if (!tsk_portid) { 2854 if (!tsk_portid) {
@@ -2864,7 +2874,7 @@ int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb)
2864 if (done) 2874 if (done)
2865 return 0; 2875 return 0;
2866 2876
2867 tsk = tipc_sk_lookup(tsk_portid); 2877 tsk = tipc_sk_lookup(net, tsk_portid);
2868 if (!tsk) 2878 if (!tsk)
2869 return -EINVAL; 2879 return -EINVAL;
2870 2880