summaryrefslogtreecommitdiffstats
path: root/net/tipc/server.c
diff options
context:
space:
mode:
Diffstat (limited to 'net/tipc/server.c')
-rw-r--r--net/tipc/server.c124
1 files changed, 99 insertions, 25 deletions
diff --git a/net/tipc/server.c b/net/tipc/server.c
index a5c112ed3bbe..0abbdd698662 100644
--- a/net/tipc/server.c
+++ b/net/tipc/server.c
@@ -49,7 +49,37 @@
49#define CF_CONNECTED 1 49#define CF_CONNECTED 1
50#define CF_SERVER 2 50#define CF_SERVER 2
51 51
52#define sock2con(x) ((struct tipc_conn *)(x)->sk_user_data) 52#define TIPC_SERVER_NAME_LEN 32
53
54/**
55 * struct tipc_server - TIPC server structure
56 * @conn_idr: identifier set of connection
57 * @idr_lock: protect the connection identifier set
58 * @idr_in_use: amount of allocated identifier entry
59 * @net: network namspace instance
60 * @rcvbuf_cache: memory cache of server receive buffer
61 * @rcv_wq: receive workqueue
62 * @send_wq: send workqueue
63 * @max_rcvbuf_size: maximum permitted receive message length
64 * @tipc_conn_new: callback will be called when new connection is incoming
65 * @tipc_conn_release: callback will be called before releasing the connection
66 * @tipc_conn_recvmsg: callback will be called when message arrives
67 * @saddr: TIPC server address
68 * @name: server name
69 * @imp: message importance
70 * @type: socket type
71 */
72struct tipc_server {
73 struct idr conn_idr;
74 spinlock_t idr_lock; /* for idr list */
75 int idr_in_use;
76 struct net *net;
77 struct workqueue_struct *rcv_wq;
78 struct workqueue_struct *send_wq;
79 int max_rcvbuf_size;
80 struct sockaddr_tipc *saddr;
81 char name[TIPC_SERVER_NAME_LEN];
82};
53 83
54/** 84/**
55 * struct tipc_conn - TIPC connection structure 85 * struct tipc_conn - TIPC connection structure
@@ -93,6 +123,11 @@ static void tipc_recv_work(struct work_struct *work);
93static void tipc_send_work(struct work_struct *work); 123static void tipc_send_work(struct work_struct *work);
94static void tipc_clean_outqueues(struct tipc_conn *con); 124static void tipc_clean_outqueues(struct tipc_conn *con);
95 125
126static struct tipc_conn *sock2con(struct sock *sk)
127{
128 return sk->sk_user_data;
129}
130
96static bool connected(struct tipc_conn *con) 131static bool connected(struct tipc_conn *con)
97{ 132{
98 return con && test_bit(CF_CONNECTED, &con->flags); 133 return con && test_bit(CF_CONNECTED, &con->flags);
@@ -198,14 +233,17 @@ static void tipc_register_callbacks(struct socket *sock, struct tipc_conn *con)
198static void tipc_con_delete_sub(struct tipc_conn *con, struct tipc_subscr *s) 233static void tipc_con_delete_sub(struct tipc_conn *con, struct tipc_subscr *s)
199{ 234{
200 struct list_head *sub_list = &con->sub_list; 235 struct list_head *sub_list = &con->sub_list;
236 struct tipc_net *tn = tipc_net(con->server->net);
201 struct tipc_subscription *sub, *tmp; 237 struct tipc_subscription *sub, *tmp;
202 238
203 spin_lock_bh(&con->sub_lock); 239 spin_lock_bh(&con->sub_lock);
204 list_for_each_entry_safe(sub, tmp, sub_list, sub_list) { 240 list_for_each_entry_safe(sub, tmp, sub_list, sub_list) {
205 if (!s || !memcmp(s, &sub->evt.s, sizeof(*s))) 241 if (!s || !memcmp(s, &sub->evt.s, sizeof(*s))) {
206 tipc_sub_unsubscribe(sub); 242 tipc_sub_unsubscribe(sub);
207 else if (s) 243 atomic_dec(&tn->subscription_count);
244 } else if (s) {
208 break; 245 break;
246 }
209 } 247 }
210 spin_unlock_bh(&con->sub_lock); 248 spin_unlock_bh(&con->sub_lock);
211} 249}
@@ -220,8 +258,7 @@ static void tipc_close_conn(struct tipc_conn *con)
220 258
221 if (disconnect) { 259 if (disconnect) {
222 sk->sk_user_data = NULL; 260 sk->sk_user_data = NULL;
223 if (con->conid) 261 tipc_con_delete_sub(con, NULL);
224 tipc_con_delete_sub(con, NULL);
225 } 262 }
226 write_unlock_bh(&sk->sk_callback_lock); 263 write_unlock_bh(&sk->sk_callback_lock);
227 264
@@ -272,15 +309,21 @@ static int tipc_con_rcv_sub(struct tipc_server *srv,
272 struct tipc_conn *con, 309 struct tipc_conn *con,
273 struct tipc_subscr *s) 310 struct tipc_subscr *s)
274{ 311{
312 struct tipc_net *tn = tipc_net(srv->net);
275 struct tipc_subscription *sub; 313 struct tipc_subscription *sub;
276 314
277 if (tipc_sub_read(s, filter) & TIPC_SUB_CANCEL) { 315 if (tipc_sub_read(s, filter) & TIPC_SUB_CANCEL) {
278 tipc_con_delete_sub(con, s); 316 tipc_con_delete_sub(con, s);
279 return 0; 317 return 0;
280 } 318 }
281 sub = tipc_sub_subscribe(srv, s, con->conid); 319 if (atomic_read(&tn->subscription_count) >= TIPC_MAX_SUBSCR) {
320 pr_warn("Subscription rejected, max (%u)\n", TIPC_MAX_SUBSCR);
321 return -1;
322 }
323 sub = tipc_sub_subscribe(srv->net, s, con->conid);
282 if (!sub) 324 if (!sub)
283 return -1; 325 return -1;
326 atomic_inc(&tn->subscription_count);
284 spin_lock_bh(&con->sub_lock); 327 spin_lock_bh(&con->sub_lock);
285 list_add(&sub->sub_list, &con->sub_list); 328 list_add(&sub->sub_list, &con->sub_list);
286 spin_unlock_bh(&con->sub_lock); 329 spin_unlock_bh(&con->sub_lock);
@@ -426,13 +469,14 @@ static void tipc_clean_outqueues(struct tipc_conn *con)
426/* tipc_conn_queue_evt - interrupt level call from a subscription instance 469/* tipc_conn_queue_evt - interrupt level call from a subscription instance
427 * The queued job is launched in tipc_send_to_sock() 470 * The queued job is launched in tipc_send_to_sock()
428 */ 471 */
429void tipc_conn_queue_evt(struct tipc_server *s, int conid, 472void tipc_conn_queue_evt(struct net *net, int conid,
430 u32 event, struct tipc_event *evt) 473 u32 event, struct tipc_event *evt)
431{ 474{
475 struct tipc_server *srv = tipc_topsrv(net);
432 struct outqueue_entry *e; 476 struct outqueue_entry *e;
433 struct tipc_conn *con; 477 struct tipc_conn *con;
434 478
435 con = tipc_conn_lookup(s, conid); 479 con = tipc_conn_lookup(srv, conid);
436 if (!con) 480 if (!con)
437 return; 481 return;
438 482
@@ -448,7 +492,7 @@ void tipc_conn_queue_evt(struct tipc_server *s, int conid,
448 list_add_tail(&e->list, &con->outqueue); 492 list_add_tail(&e->list, &con->outqueue);
449 spin_unlock_bh(&con->outqueue_lock); 493 spin_unlock_bh(&con->outqueue_lock);
450 494
451 if (queue_work(s->send_wq, &con->swork)) 495 if (queue_work(srv->send_wq, &con->swork))
452 return; 496 return;
453err: 497err:
454 conn_put(con); 498 conn_put(con);
@@ -620,41 +664,71 @@ static int tipc_work_start(struct tipc_server *s)
620 return 0; 664 return 0;
621} 665}
622 666
623int tipc_server_start(struct tipc_server *s) 667int tipc_topsrv_start(struct net *net)
624{ 668{
669 struct tipc_net *tn = tipc_net(net);
670 const char name[] = "topology_server";
671 struct sockaddr_tipc *saddr;
672 struct tipc_server *srv;
625 int ret; 673 int ret;
626 674
627 spin_lock_init(&s->idr_lock); 675 saddr = kzalloc(sizeof(*saddr), GFP_ATOMIC);
628 idr_init(&s->conn_idr); 676 if (!saddr)
629 s->idr_in_use = 0; 677 return -ENOMEM;
678 saddr->family = AF_TIPC;
679 saddr->addrtype = TIPC_ADDR_NAMESEQ;
680 saddr->addr.nameseq.type = TIPC_TOP_SRV;
681 saddr->addr.nameseq.lower = TIPC_TOP_SRV;
682 saddr->addr.nameseq.upper = TIPC_TOP_SRV;
683 saddr->scope = TIPC_NODE_SCOPE;
684
685 srv = kzalloc(sizeof(*srv), GFP_ATOMIC);
686 if (!srv) {
687 kfree(saddr);
688 return -ENOMEM;
689 }
690 srv->net = net;
691 srv->saddr = saddr;
692 srv->max_rcvbuf_size = sizeof(struct tipc_subscr);
693
694 strncpy(srv->name, name, strlen(name) + 1);
695 tn->topsrv = srv;
696 atomic_set(&tn->subscription_count, 0);
697
698 spin_lock_init(&srv->idr_lock);
699 idr_init(&srv->conn_idr);
700 srv->idr_in_use = 0;
630 701
631 ret = tipc_work_start(s); 702 ret = tipc_work_start(srv);
632 if (ret < 0) 703 if (ret < 0)
633 return ret; 704 return ret;
634 705
635 ret = tipc_open_listening_sock(s); 706 ret = tipc_open_listening_sock(srv);
636 if (ret < 0) 707 if (ret < 0)
637 tipc_work_stop(s); 708 tipc_work_stop(srv);
638 709
639 return ret; 710 return ret;
640} 711}
641 712
642void tipc_server_stop(struct tipc_server *s) 713void tipc_topsrv_stop(struct net *net)
643{ 714{
715 struct tipc_server *srv = tipc_topsrv(net);
644 struct tipc_conn *con; 716 struct tipc_conn *con;
645 int id; 717 int id;
646 718
647 spin_lock_bh(&s->idr_lock); 719 spin_lock_bh(&srv->idr_lock);
648 for (id = 0; s->idr_in_use; id++) { 720 for (id = 0; srv->idr_in_use; id++) {
649 con = idr_find(&s->conn_idr, id); 721 con = idr_find(&srv->conn_idr, id);
650 if (con) { 722 if (con) {
651 spin_unlock_bh(&s->idr_lock); 723 spin_unlock_bh(&srv->idr_lock);
652 tipc_close_conn(con); 724 tipc_close_conn(con);
653 spin_lock_bh(&s->idr_lock); 725 spin_lock_bh(&srv->idr_lock);
654 } 726 }
655 } 727 }
656 spin_unlock_bh(&s->idr_lock); 728 spin_unlock_bh(&srv->idr_lock);
657 729
658 tipc_work_stop(s); 730 tipc_work_stop(srv);
659 idr_destroy(&s->conn_idr); 731 idr_destroy(&srv->conn_idr);
732 kfree(srv->saddr);
733 kfree(srv);
660} 734}