topsrv.c 18.6 KB
Newer Older
1 2 3 4
/*
 * net/tipc/server.c: TIPC server infrastructure
 *
 * Copyright (c) 2012-2013, Wind River Systems
5
 * Copyright (c) 2017-2018, Ericsson AB
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the names of the copyright holders nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * Alternatively, this software may be distributed under the terms of the
 * GNU General Public License ("GPL") version 2 as published by the Free
 * Software Foundation.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

37
#include "subscr.h"
38
#include "topsrv.h"
39
#include "core.h"
Y
Ying Xue 已提交
40
#include "socket.h"
41 42
#include "addr.h"
#include "msg.h"
43
#include <net/sock.h>
Y
Ying Xue 已提交
44
#include <linux/module.h>
45 46 47 48 49

/* Number of messages to send before rescheduling */
#define MAX_SEND_MSG_COUNT	25
#define MAX_RECV_MSG_COUNT	25
#define CF_CONNECTED		1
Y
Ying Xue 已提交
50
#define CF_SERVER		2
51

52 53 54
#define TIPC_SERVER_NAME_LEN	32

/**
55
 * struct tipc_topsrv - TIPC server structure
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
 * @conn_idr: identifier set of connection
 * @idr_lock: protect the connection identifier set
 * @idr_in_use: amount of allocated identifier entry
 * @net: network namspace instance
 * @rcvbuf_cache: memory cache of server receive buffer
 * @rcv_wq: receive workqueue
 * @send_wq: send workqueue
 * @max_rcvbuf_size: maximum permitted receive message length
 * @tipc_conn_new: callback will be called when new connection is incoming
 * @tipc_conn_release: callback will be called before releasing the connection
 * @tipc_conn_recvmsg: callback will be called when message arrives
 * @name: server name
 * @imp: message importance
 * @type: socket type
 */
71
struct tipc_topsrv {
72 73 74 75
	struct idr conn_idr;
	spinlock_t idr_lock; /* for idr list */
	int idr_in_use;
	struct net *net;
76
	struct work_struct awork;
77 78 79
	struct workqueue_struct *rcv_wq;
	struct workqueue_struct *send_wq;
	int max_rcvbuf_size;
80
	struct socket *listener;
81 82
	char name[TIPC_SERVER_NAME_LEN];
};
83 84 85 86 87 88 89 90

/**
 * struct tipc_conn - TIPC connection structure
 * @kref: reference counter to connection object
 * @conid: connection identifier
 * @sock: socket handler associated with connection
 * @flags: indicates connection state
 * @server: pointer to connected server
91 92 93
 * @sub_list: lsit to all pertaing subscriptions
 * @sub_lock: lock protecting the subscription list
 * @outqueue_lock: control access to the outqueue
94 95 96
 * @rwork: receive work item
 * @rx_action: what to do when connection socket is active
 * @outqueue: pointer to first outbound message in queue
S
stephen hemminger 已提交
97
 * @outqueue_lock: control access to the outqueue
98 99 100 101 102 103 104
 * @swork: send work item
 */
struct tipc_conn {
	struct kref kref;
	int conid;
	struct socket *sock;
	unsigned long flags;
105
	struct tipc_topsrv *server;
106 107
	struct list_head sub_list;
	spinlock_t sub_lock; /* for subscription list */
108 109
	struct work_struct rwork;
	struct list_head outqueue;
110
	spinlock_t outqueue_lock; /* for outqueue */
111 112 113 114 115
	struct work_struct swork;
};

/* An entry waiting to be sent */
struct outqueue_entry {
116 117
	bool inactive;
	struct tipc_event evt;
118 119 120
	struct list_head list;
};

121 122 123 124
static void tipc_conn_recv_work(struct work_struct *work);
static void tipc_conn_send_work(struct work_struct *work);
static void tipc_topsrv_kern_evt(struct net *net, struct tipc_event *evt);
static void tipc_conn_delete_sub(struct tipc_conn *con, struct tipc_subscr *s);
125

126 127 128 129 130
static bool connected(struct tipc_conn *con)
{
	return con && test_bit(CF_CONNECTED, &con->flags);
}

131 132 133
static void tipc_conn_kref_release(struct kref *kref)
{
	struct tipc_conn *con = container_of(kref, struct tipc_conn, kref);
134
	struct tipc_topsrv *s = con->server;
135
	struct outqueue_entry *e, *safe;
Y
Ying Xue 已提交
136

137 138 139 140
	spin_lock_bh(&s->idr_lock);
	idr_remove(&s->conn_idr, con->conid);
	s->idr_in_use--;
	spin_unlock_bh(&s->idr_lock);
141 142 143 144 145 146 147 148 149
	if (con->sock)
		sock_release(con->sock);

	spin_lock_bh(&con->outqueue_lock);
	list_for_each_entry_safe(e, safe, &con->outqueue, list) {
		list_del(&e->list);
		kfree(e);
	}
	spin_unlock_bh(&con->outqueue_lock);
150 151 152 153 154 155 156 157 158 159 160 161 162
	kfree(con);
}

static void conn_put(struct tipc_conn *con)
{
	kref_put(&con->kref, tipc_conn_kref_release);
}

static void conn_get(struct tipc_conn *con)
{
	kref_get(&con->kref);
}

163
static void tipc_conn_close(struct tipc_conn *con)
164
{
165 166
	struct sock *sk = con->sock->sk;
	bool disconnect = false;
167

168 169
	write_lock_bh(&sk->sk_callback_lock);
	disconnect = test_and_clear_bit(CF_CONNECTED, &con->flags);
170

171 172
	if (disconnect) {
		sk->sk_user_data = NULL;
173
		tipc_conn_delete_sub(con, NULL);
174
	}
175 176 177 178 179 180 181 182
	write_unlock_bh(&sk->sk_callback_lock);

	/* Handle concurrent calls from sending and receiving threads */
	if (!disconnect)
		return;

	/* Don't flush pending works, -just let them expire */
	kernel_sock_shutdown(con->sock, SHUT_RDWR);
183

184
	conn_put(con);
185 186
}

187
static struct tipc_conn *tipc_conn_alloc(struct tipc_topsrv *s)
188 189 190 191
{
	struct tipc_conn *con;
	int ret;

192
	con = kzalloc(sizeof(*con), GFP_ATOMIC);
193 194 195 196 197
	if (!con)
		return ERR_PTR(-ENOMEM);

	kref_init(&con->kref);
	INIT_LIST_HEAD(&con->outqueue);
198
	INIT_LIST_HEAD(&con->sub_list);
199
	spin_lock_init(&con->outqueue_lock);
200
	spin_lock_init(&con->sub_lock);
201 202
	INIT_WORK(&con->swork, tipc_conn_send_work);
	INIT_WORK(&con->rwork, tipc_conn_recv_work);
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220

	spin_lock_bh(&s->idr_lock);
	ret = idr_alloc(&s->conn_idr, con, 0, 0, GFP_ATOMIC);
	if (ret < 0) {
		kfree(con);
		spin_unlock_bh(&s->idr_lock);
		return ERR_PTR(-ENOMEM);
	}
	con->conid = ret;
	s->idr_in_use++;
	spin_unlock_bh(&s->idr_lock);

	set_bit(CF_CONNECTED, &con->flags);
	con->server = s;

	return con;
}

221
static struct tipc_conn *tipc_conn_lookup(struct tipc_topsrv *s, int conid)
222
{
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
	struct tipc_conn *con;

	spin_lock_bh(&s->idr_lock);
	con = idr_find(&s->conn_idr, conid);
	if (!connected(con) || !kref_get_unless_zero(&con->kref))
		con = NULL;
	spin_unlock_bh(&s->idr_lock);
	return con;
}

/* tipc_conn_delete_sub - delete a specific or all subscriptions
 * for a given subscriber
 */
static void tipc_conn_delete_sub(struct tipc_conn *con, struct tipc_subscr *s)
{
	struct tipc_net *tn = tipc_net(con->server->net);
	struct list_head *sub_list = &con->sub_list;
	struct tipc_subscription *sub, *tmp;
241 242

	spin_lock_bh(&con->sub_lock);
243 244 245 246 247 248 249 250
	list_for_each_entry_safe(sub, tmp, sub_list, sub_list) {
		if (!s || !memcmp(s, &sub->evt.s, sizeof(*s))) {
			tipc_sub_unsubscribe(sub);
			atomic_dec(&tn->subscription_count);
		} else if (s) {
			break;
		}
	}
251 252 253
	spin_unlock_bh(&con->sub_lock);
}

254
static void tipc_conn_send_to_sock(struct tipc_conn *con)
255
{
256 257 258 259 260
	struct list_head *queue = &con->outqueue;
	struct tipc_topsrv *srv = con->server;
	struct outqueue_entry *e;
	struct tipc_event *evt;
	struct msghdr msg;
261
	struct kvec iov;
262
	int count = 0;
263 264
	int ret;

265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
	spin_lock_bh(&con->outqueue_lock);

	while (!list_empty(queue)) {
		e = list_first_entry(queue, struct outqueue_entry, list);
		evt = &e->evt;
		spin_unlock_bh(&con->outqueue_lock);

		if (e->inactive)
			tipc_conn_delete_sub(con, &evt->s);

		memset(&msg, 0, sizeof(msg));
		msg.msg_flags = MSG_DONTWAIT;
		iov.iov_base = evt;
		iov.iov_len = sizeof(*evt);
		msg.msg_name = NULL;

		if (con->sock) {
			ret = kernel_sendmsg(con->sock, &msg, &iov,
					     1, sizeof(*evt));
			if (ret == -EWOULDBLOCK || ret == 0) {
				cond_resched();
				return;
			} else if (ret < 0) {
				return tipc_conn_close(con);
			}
		} else {
			tipc_topsrv_kern_evt(srv->net, evt);
		}

		/* Don't starve users filling buffers */
		if (++count >= MAX_SEND_MSG_COUNT) {
			cond_resched();
			count = 0;
		}
		spin_lock_bh(&con->outqueue_lock);
		list_del(&e->list);
		kfree(e);
302
	}
303 304
	spin_unlock_bh(&con->outqueue_lock);
}
305

306 307 308 309 310 311 312 313
static void tipc_conn_send_work(struct work_struct *work)
{
	struct tipc_conn *con = container_of(work, struct tipc_conn, swork);

	if (connected(con))
		tipc_conn_send_to_sock(con);

	conn_put(con);
314 315
}

316 317
/* tipc_topsrv_queue_evt() - interrupt level call from a subscription instance
 * The queued work is launched into tipc_conn_send_work()->tipc_conn_send_to_sock()
318
 */
319 320
void tipc_topsrv_queue_evt(struct net *net, int conid,
			   u32 event, struct tipc_event *evt)
321
{
322
	struct tipc_topsrv *srv = tipc_topsrv(net);
323 324 325
	struct outqueue_entry *e;
	struct tipc_conn *con;

326
	con = tipc_conn_lookup(srv, conid);
327
	if (!con)
328
		return;
329

330 331
	if (!connected(con))
		goto err;
332

333 334 335 336 337
	e = kmalloc(sizeof(*e), GFP_ATOMIC);
	if (!e)
		goto err;
	e->inactive = (event == TIPC_SUBSCR_TIMEOUT);
	memcpy(&e->evt, evt, sizeof(*evt));
338 339 340 341
	spin_lock_bh(&con->outqueue_lock);
	list_add_tail(&e->list, &con->outqueue);
	spin_unlock_bh(&con->outqueue_lock);

342
	if (queue_work(srv->send_wq, &con->swork))
343 344 345
		return;
err:
	conn_put(con);
346 347
}

348 349 350 351 352
/* tipc_conn_write_space - interrupt callback after a sendmsg EAGAIN
 * Indicates that there now is more space in the send buffer
 * The queued work is launched into tipc_send_work()->tipc_conn_send_to_sock()
 */
static void tipc_conn_write_space(struct sock *sk)
353 354 355
{
	struct tipc_conn *con;

356 357 358 359 360 361 362 363
	read_lock_bh(&sk->sk_callback_lock);
	con = sk->sk_user_data;
	if (connected(con)) {
		conn_get(con);
		if (!queue_work(con->server->send_wq, &con->swork))
			conn_put(con);
	}
	read_unlock_bh(&sk->sk_callback_lock);
364 365
}

366 367 368
static int tipc_conn_rcv_sub(struct tipc_topsrv *srv,
			     struct tipc_conn *con,
			     struct tipc_subscr *s)
369
{
370 371
	struct tipc_net *tn = tipc_net(srv->net);
	struct tipc_subscription *sub;
372

373
	if (tipc_sub_read(s, filter) & TIPC_SUB_CANCEL) {
374
		s->filter &= __constant_ntohl(~TIPC_SUB_CANCEL);
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
		tipc_conn_delete_sub(con, s);
		return 0;
	}
	if (atomic_read(&tn->subscription_count) >= TIPC_MAX_SUBSCR) {
		pr_warn("Subscription rejected, max (%u)\n", TIPC_MAX_SUBSCR);
		return -1;
	}
	sub = tipc_sub_subscribe(srv->net, s, con->conid);
	if (!sub)
		return -1;
	atomic_inc(&tn->subscription_count);
	spin_lock_bh(&con->sub_lock);
	list_add(&sub->sub_list, &con->sub_list);
	spin_unlock_bh(&con->sub_lock);
	return 0;
390 391
}

392
static int tipc_conn_rcv_from_sock(struct tipc_conn *con)
393
{
394 395 396 397
	struct tipc_topsrv *srv = con->server;
	struct sock *sk = con->sock->sk;
	struct msghdr msg = {};
	struct tipc_subscr s;
398
	struct kvec iov;
399 400
	int ret;

401 402 403 404 405 406 407
	iov.iov_base = &s;
	iov.iov_len = sizeof(s);
	msg.msg_name = NULL;
	iov_iter_kvec(&msg.msg_iter, READ | ITER_KVEC, &iov, 1, iov.iov_len);
	ret = sock_recvmsg(con->sock, &msg, MSG_DONTWAIT);
	if (ret == -EWOULDBLOCK)
		return -EWOULDBLOCK;
408
	if (ret == sizeof(s)) {
409 410 411
		read_lock_bh(&sk->sk_callback_lock);
		ret = tipc_conn_rcv_sub(srv, con, &s);
		read_unlock_bh(&sk->sk_callback_lock);
412
	}
413 414 415 416
	if (ret < 0)
		tipc_conn_close(con);

	return ret;
417 418
}

419
static void tipc_conn_recv_work(struct work_struct *work)
420 421 422 423
{
	struct tipc_conn *con = container_of(work, struct tipc_conn, rwork);
	int count = 0;

424
	while (connected(con)) {
425
		if (tipc_conn_rcv_from_sock(con))
426 427 428 429 430 431 432 433 434 435 436
			break;

		/* Don't flood Rx machine */
		if (++count >= MAX_RECV_MSG_COUNT) {
			cond_resched();
			count = 0;
		}
	}
	conn_put(con);
}

437 438 439 440
/* tipc_conn_data_ready - interrupt callback indicating the socket has data
 * The queued work is launched into tipc_recv_work()->tipc_conn_rcv_from_sock()
 */
static void tipc_conn_data_ready(struct sock *sk)
441
{
442
	struct tipc_conn *con;
443

444 445 446 447 448 449 450 451
	read_lock_bh(&sk->sk_callback_lock);
	con = sk->sk_user_data;
	if (connected(con)) {
		conn_get(con);
		if (!queue_work(con->server->rcv_wq, &con->rwork))
			conn_put(con);
	}
	read_unlock_bh(&sk->sk_callback_lock);
452 453
}

454
static void tipc_topsrv_accept(struct work_struct *work)
455
{
456
	struct tipc_topsrv *srv = container_of(work, struct tipc_topsrv, awork);
457 458 459 460 461 462 463 464 465 466
	struct socket *lsock = srv->listener;
	struct socket *newsock;
	struct tipc_conn *con;
	struct sock *newsk;
	int ret;

	while (1) {
		ret = kernel_accept(lsock, &newsock, O_NONBLOCK);
		if (ret < 0)
			return;
467
		con = tipc_conn_alloc(srv);
468 469 470 471 472 473 474 475
		if (IS_ERR(con)) {
			ret = PTR_ERR(con);
			sock_release(newsock);
			return;
		}
		/* Register callbacks */
		newsk = newsock->sk;
		write_lock_bh(&newsk->sk_callback_lock);
476 477
		newsk->sk_data_ready = tipc_conn_data_ready;
		newsk->sk_write_space = tipc_conn_write_space;
478 479 480 481 482 483 484 485 486
		newsk->sk_user_data = con;
		con->sock = newsock;
		write_unlock_bh(&newsk->sk_callback_lock);

		/* Wake up receive process in case of 'SYN+' message */
		newsk->sk_data_ready(newsk);
	}
}

487 488
/* tipc_toprsv_listener_data_ready - interrupt callback with connection request
 * The queued job is launched into tipc_topsrv_accept()
489
 */
490
static void tipc_topsrv_listener_data_ready(struct sock *sk)
491
{
492
	struct tipc_topsrv *srv;
493 494 495 496 497 498 499 500

	read_lock_bh(&sk->sk_callback_lock);
	srv = sk->sk_user_data;
	if (srv->listener)
		queue_work(srv->rcv_wq, &srv->awork);
	read_unlock_bh(&sk->sk_callback_lock);
}

501
static int tipc_topsrv_create_listener(struct tipc_topsrv *srv)
502 503 504 505 506 507 508 509 510 511 512 513 514 515
{
	int imp = TIPC_CRITICAL_IMPORTANCE;
	struct socket *lsock = NULL;
	struct sockaddr_tipc saddr;
	struct sock *sk;
	int rc;

	rc = sock_create_kern(srv->net, AF_TIPC, SOCK_SEQPACKET, 0, &lsock);
	if (rc < 0)
		return rc;

	srv->listener = lsock;
	sk = lsock->sk;
	write_lock_bh(&sk->sk_callback_lock);
516
	sk->sk_data_ready = tipc_topsrv_listener_data_ready;
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
	sk->sk_user_data = srv;
	write_unlock_bh(&sk->sk_callback_lock);

	rc = kernel_setsockopt(lsock, SOL_TIPC, TIPC_IMPORTANCE,
			       (char *)&imp, sizeof(imp));
	if (rc < 0)
		goto err;

	saddr.family	                = AF_TIPC;
	saddr.addrtype		        = TIPC_ADDR_NAMESEQ;
	saddr.addr.nameseq.type	        = TIPC_TOP_SRV;
	saddr.addr.nameseq.lower	= TIPC_TOP_SRV;
	saddr.addr.nameseq.upper	= TIPC_TOP_SRV;
	saddr.scope			= TIPC_NODE_SCOPE;

	rc = kernel_bind(lsock, (struct sockaddr *)&saddr, sizeof(saddr));
	if (rc < 0)
		goto err;
	rc = kernel_listen(lsock, 0);
	if (rc < 0)
		goto err;

	/* As server's listening socket owner and creator is the same module,
	 * we have to decrease TIPC module reference count to guarantee that
	 * it remains zero after the server socket is created, otherwise,
	 * executing "rmmod" command is unable to make TIPC module deleted
	 * after TIPC module is inserted successfully.
	 *
	 * However, the reference count is ever increased twice in
	 * sock_create_kern(): one is to increase the reference count of owner
	 * of TIPC socket's proto_ops struct; another is to increment the
	 * reference count of owner of TIPC proto struct. Therefore, we must
	 * decrement the module reference count twice to ensure that it keeps
	 * zero after server's listening socket is created. Of course, we
	 * must bump the module reference count twice as well before the socket
	 * is closed.
	 */
	module_put(lsock->ops->owner);
	module_put(sk->sk_prot_creator->owner);

	return 0;
err:
	sock_release(lsock);
	return -EINVAL;
561 562
}

563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
bool tipc_topsrv_kern_subscr(struct net *net, u32 port, u32 type, u32 lower,
			     u32 upper, u32 filter, int *conid)
{
	struct tipc_subscr sub;
	struct tipc_conn *con;
	int rc;

	sub.seq.type = type;
	sub.seq.lower = lower;
	sub.seq.upper = upper;
	sub.timeout = TIPC_WAIT_FOREVER;
	sub.filter = filter;
	*(u32 *)&sub.usr_handle = port;

	con = tipc_conn_alloc(tipc_topsrv(net));
	if (IS_ERR(con))
		return false;

	*conid = con->conid;
	con->sock = NULL;
	rc = tipc_conn_rcv_sub(tipc_topsrv(net), con, &sub);
584 585 586 587
	if (rc >= 0)
		return true;
	conn_put(con);
	return false;
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
}

void tipc_topsrv_kern_unsubscr(struct net *net, int conid)
{
	struct tipc_conn *con;

	con = tipc_conn_lookup(tipc_topsrv(net), conid);
	if (!con)
		return;

	test_and_clear_bit(CF_CONNECTED, &con->flags);
	tipc_conn_delete_sub(con, NULL);
	conn_put(con);
	conn_put(con);
}

static void tipc_topsrv_kern_evt(struct net *net, struct tipc_event *evt)
{
	u32 port = *(u32 *)&evt->s.usr_handle;
	u32 self = tipc_own_addr(net);
	struct sk_buff_head evtq;
	struct sk_buff *skb;

	skb = tipc_msg_create(TOP_SRV, 0, INT_H_SIZE, sizeof(*evt),
			      self, self, port, port, 0);
	if (!skb)
		return;
	msg_set_dest_droppable(buf_msg(skb), true);
	memcpy(msg_data(buf_msg(skb)), evt, sizeof(*evt));
	skb_queue_head_init(&evtq);
	__skb_queue_tail(&evtq, skb);
	tipc_sk_rcv(net, &evtq);
}

static int tipc_topsrv_work_start(struct tipc_topsrv *s)
623
{
624
	s->rcv_wq = alloc_ordered_workqueue("tipc_rcv", 0);
625 626 627 628 629
	if (!s->rcv_wq) {
		pr_err("can't start tipc receive workqueue\n");
		return -ENOMEM;
	}

630
	s->send_wq = alloc_ordered_workqueue("tipc_send", 0);
631 632 633 634 635 636 637 638 639
	if (!s->send_wq) {
		pr_err("can't start tipc send workqueue\n");
		destroy_workqueue(s->rcv_wq);
		return -ENOMEM;
	}

	return 0;
}

640
static void tipc_topsrv_work_stop(struct tipc_topsrv *s)
641 642 643 644 645
{
	destroy_workqueue(s->rcv_wq);
	destroy_workqueue(s->send_wq);
}

646
static int tipc_topsrv_start(struct net *net)
647
{
648 649
	struct tipc_net *tn = tipc_net(net);
	const char name[] = "topology_server";
650
	struct tipc_topsrv *srv;
651 652
	int ret;

653
	srv = kzalloc(sizeof(*srv), GFP_ATOMIC);
654
	if (!srv)
655
		return -ENOMEM;
656 657 658

	srv->net = net;
	srv->max_rcvbuf_size = sizeof(struct tipc_subscr);
659
	INIT_WORK(&srv->awork, tipc_topsrv_accept);
660

661
	strscpy(srv->name, name, sizeof(srv->name));
662 663 664 665 666 667
	tn->topsrv = srv;
	atomic_set(&tn->subscription_count, 0);

	spin_lock_init(&srv->idr_lock);
	idr_init(&srv->conn_idr);
	srv->idr_in_use = 0;
668

669
	ret = tipc_topsrv_work_start(srv);
670
	if (ret < 0)
671
		return ret;
672

673
	ret = tipc_topsrv_create_listener(srv);
674
	if (ret < 0)
675
		tipc_topsrv_work_stop(srv);
676

677
	return ret;
678 679
}

680
static void tipc_topsrv_stop(struct net *net)
681
{
682
	struct tipc_topsrv *srv = tipc_topsrv(net);
683
	struct socket *lsock = srv->listener;
684 685 686
	struct tipc_conn *con;
	int id;

687 688 689
	spin_lock_bh(&srv->idr_lock);
	for (id = 0; srv->idr_in_use; id++) {
		con = idr_find(&srv->conn_idr, id);
690
		if (con) {
691
			spin_unlock_bh(&srv->idr_lock);
692
			tipc_conn_close(con);
693
			spin_lock_bh(&srv->idr_lock);
694 695
		}
	}
696 697 698
	__module_get(lsock->ops->owner);
	__module_get(lsock->sk->sk_prot_creator->owner);
	srv->listener = NULL;
699
	spin_unlock_bh(&srv->idr_lock);
700
	sock_release(lsock);
701
	tipc_topsrv_work_stop(srv);
702 703
	idr_destroy(&srv->conn_idr);
	kfree(srv);
704
}
705 706 707 708 709 710 711 712 713 714

int __net_init tipc_topsrv_init_net(struct net *net)
{
	return tipc_topsrv_start(net);
}

void __net_exit tipc_topsrv_exit_net(struct net *net)
{
	tipc_topsrv_stop(net);
}