Commit | Line | Data |
---|---|---|
c5fa7b3c YX |
1 | /* |
2 | * net/tipc/server.c: TIPC server infrastructure | |
3 | * | |
4 | * Copyright (c) 2012-2013, Wind River Systems | |
5 | * All rights reserved. | |
6 | * | |
7 | * Redistribution and use in source and binary forms, with or without | |
8 | * modification, are permitted provided that the following conditions are met: | |
9 | * | |
10 | * 1. Redistributions of source code must retain the above copyright | |
11 | * notice, this list of conditions and the following disclaimer. | |
12 | * 2. Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in the | |
14 | * documentation and/or other materials provided with the distribution. | |
15 | * 3. Neither the names of the copyright holders nor the names of its | |
16 | * contributors may be used to endorse or promote products derived from | |
17 | * this software without specific prior written permission. | |
18 | * | |
19 | * Alternatively, this software may be distributed under the terms of the | |
20 | * GNU General Public License ("GPL") version 2 as published by the Free | |
21 | * Software Foundation. | |
22 | * | |
23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | |
27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
33 | * POSSIBILITY OF SUCH DAMAGE. | |
34 | */ | |
35 | ||
36 | #include "server.h" | |
37 | #include "core.h" | |
859fc7c0 | 38 | #include "socket.h" |
c5fa7b3c | 39 | #include <net/sock.h> |
76100a8a | 40 | #include <linux/module.h> |
c5fa7b3c YX |
41 | |
42 | /* Number of messages to send before rescheduling */ | |
43 | #define MAX_SEND_MSG_COUNT 25 | |
44 | #define MAX_RECV_MSG_COUNT 25 | |
45 | #define CF_CONNECTED 1 | |
76100a8a | 46 | #define CF_SERVER 2 |
c5fa7b3c YX |
47 | |
48 | #define sock2con(x) ((struct tipc_conn *)(x)->sk_user_data) | |
49 | ||
50 | /** | |
51 | * struct tipc_conn - TIPC connection structure | |
52 | * @kref: reference counter to connection object | |
53 | * @conid: connection identifier | |
54 | * @sock: socket handler associated with connection | |
55 | * @flags: indicates connection state | |
56 | * @server: pointer to connected server | |
57 | * @rwork: receive work item | |
58 | * @usr_data: user-specified field | |
59 | * @rx_action: what to do when connection socket is active | |
60 | * @outqueue: pointer to first outbound message in queue | |
963a1855 | 61 | * @outqueue_lock: control access to the outqueue |
c5fa7b3c YX |
62 | * @outqueue: list of connection objects for its server |
63 | * @swork: send work item | |
64 | */ | |
65 | struct tipc_conn { | |
66 | struct kref kref; | |
67 | int conid; | |
68 | struct socket *sock; | |
69 | unsigned long flags; | |
70 | struct tipc_server *server; | |
71 | struct work_struct rwork; | |
72 | int (*rx_action) (struct tipc_conn *con); | |
73 | void *usr_data; | |
74 | struct list_head outqueue; | |
75 | spinlock_t outqueue_lock; | |
76 | struct work_struct swork; | |
77 | }; | |
78 | ||
79 | /* An entry waiting to be sent */ | |
80 | struct outqueue_entry { | |
81 | struct list_head list; | |
82 | struct kvec iov; | |
83 | struct sockaddr_tipc dest; | |
84 | }; | |
85 | ||
86 | static void tipc_recv_work(struct work_struct *work); | |
87 | static void tipc_send_work(struct work_struct *work); | |
88 | static void tipc_clean_outqueues(struct tipc_conn *con); | |
333f7962 | 89 | static void tipc_sock_release(struct tipc_conn *con); |
c5fa7b3c YX |
90 | |
91 | static void tipc_conn_kref_release(struct kref *kref) | |
92 | { | |
93 | struct tipc_conn *con = container_of(kref, struct tipc_conn, kref); | |
2b9bb7f3 | 94 | struct sockaddr_tipc *saddr = con->server->saddr; |
76100a8a YX |
95 | struct socket *sock = con->sock; |
96 | struct sock *sk; | |
97 | ||
98 | if (sock) { | |
99 | sk = sock->sk; | |
100 | if (test_bit(CF_SERVER, &con->flags)) { | |
101 | __module_get(sock->ops->owner); | |
102 | __module_get(sk->sk_prot_creator->owner); | |
103 | } | |
2b9bb7f3 YX |
104 | saddr->scope = -TIPC_NODE_SCOPE; |
105 | kernel_bind(sock, (struct sockaddr *)saddr, sizeof(*saddr)); | |
333f7962 | 106 | tipc_sock_release(con); |
def81f69 | 107 | sock_release(sock); |
c5fa7b3c YX |
108 | con->sock = NULL; |
109 | } | |
110 | ||
111 | tipc_clean_outqueues(con); | |
c5fa7b3c YX |
112 | kfree(con); |
113 | } | |
114 | ||
115 | static void conn_put(struct tipc_conn *con) | |
116 | { | |
117 | kref_put(&con->kref, tipc_conn_kref_release); | |
118 | } | |
119 | ||
120 | static void conn_get(struct tipc_conn *con) | |
121 | { | |
122 | kref_get(&con->kref); | |
123 | } | |
124 | ||
125 | static struct tipc_conn *tipc_conn_lookup(struct tipc_server *s, int conid) | |
126 | { | |
127 | struct tipc_conn *con; | |
128 | ||
129 | spin_lock_bh(&s->idr_lock); | |
130 | con = idr_find(&s->conn_idr, conid); | |
131 | if (con) | |
132 | conn_get(con); | |
133 | spin_unlock_bh(&s->idr_lock); | |
134 | return con; | |
135 | } | |
136 | ||
676d2369 | 137 | static void sock_data_ready(struct sock *sk) |
c5fa7b3c YX |
138 | { |
139 | struct tipc_conn *con; | |
140 | ||
141 | read_lock(&sk->sk_callback_lock); | |
142 | con = sock2con(sk); | |
143 | if (con && test_bit(CF_CONNECTED, &con->flags)) { | |
144 | conn_get(con); | |
145 | if (!queue_work(con->server->rcv_wq, &con->rwork)) | |
146 | conn_put(con); | |
147 | } | |
148 | read_unlock(&sk->sk_callback_lock); | |
149 | } | |
150 | ||
151 | static void sock_write_space(struct sock *sk) | |
152 | { | |
153 | struct tipc_conn *con; | |
154 | ||
155 | read_lock(&sk->sk_callback_lock); | |
156 | con = sock2con(sk); | |
157 | if (con && test_bit(CF_CONNECTED, &con->flags)) { | |
158 | conn_get(con); | |
159 | if (!queue_work(con->server->send_wq, &con->swork)) | |
160 | conn_put(con); | |
161 | } | |
162 | read_unlock(&sk->sk_callback_lock); | |
163 | } | |
164 | ||
165 | static void tipc_register_callbacks(struct socket *sock, struct tipc_conn *con) | |
166 | { | |
167 | struct sock *sk = sock->sk; | |
168 | ||
169 | write_lock_bh(&sk->sk_callback_lock); | |
170 | ||
171 | sk->sk_data_ready = sock_data_ready; | |
172 | sk->sk_write_space = sock_write_space; | |
173 | sk->sk_user_data = con; | |
174 | ||
175 | con->sock = sock; | |
176 | ||
177 | write_unlock_bh(&sk->sk_callback_lock); | |
178 | } | |
179 | ||
180 | static void tipc_unregister_callbacks(struct tipc_conn *con) | |
181 | { | |
182 | struct sock *sk = con->sock->sk; | |
183 | ||
184 | write_lock_bh(&sk->sk_callback_lock); | |
185 | sk->sk_user_data = NULL; | |
186 | write_unlock_bh(&sk->sk_callback_lock); | |
187 | } | |
188 | ||
333f7962 PB |
189 | static void tipc_sock_release(struct tipc_conn *con) |
190 | { | |
191 | struct tipc_server *s = con->server; | |
192 | ||
193 | if (con->conid) | |
194 | s->tipc_conn_release(con->conid, con->usr_data); | |
195 | ||
196 | tipc_unregister_callbacks(con); | |
197 | } | |
198 | ||
c5fa7b3c YX |
199 | static void tipc_close_conn(struct tipc_conn *con) |
200 | { | |
201 | struct tipc_server *s = con->server; | |
202 | ||
203 | if (test_and_clear_bit(CF_CONNECTED, &con->flags)) { | |
6d4ebeb4 | 204 | |
c5fa7b3c YX |
205 | spin_lock_bh(&s->idr_lock); |
206 | idr_remove(&s->conn_idr, con->conid); | |
207 | s->idr_in_use--; | |
208 | spin_unlock_bh(&s->idr_lock); | |
209 | ||
c5fa7b3c YX |
210 | /* We shouldn't flush pending works as we may be in the |
211 | * thread. In fact the races with pending rx/tx work structs | |
212 | * are harmless for us here as we have already deleted this | |
333f7962 | 213 | * connection from server connection list. |
c5fa7b3c YX |
214 | */ |
215 | kernel_sock_shutdown(con->sock, SHUT_RDWR); | |
216 | ||
217 | conn_put(con); | |
218 | } | |
219 | } | |
220 | ||
221 | static struct tipc_conn *tipc_alloc_conn(struct tipc_server *s) | |
222 | { | |
223 | struct tipc_conn *con; | |
224 | int ret; | |
225 | ||
226 | con = kzalloc(sizeof(struct tipc_conn), GFP_ATOMIC); | |
227 | if (!con) | |
228 | return ERR_PTR(-ENOMEM); | |
229 | ||
230 | kref_init(&con->kref); | |
231 | INIT_LIST_HEAD(&con->outqueue); | |
232 | spin_lock_init(&con->outqueue_lock); | |
233 | INIT_WORK(&con->swork, tipc_send_work); | |
234 | INIT_WORK(&con->rwork, tipc_recv_work); | |
235 | ||
236 | spin_lock_bh(&s->idr_lock); | |
237 | ret = idr_alloc(&s->conn_idr, con, 0, 0, GFP_ATOMIC); | |
238 | if (ret < 0) { | |
239 | kfree(con); | |
240 | spin_unlock_bh(&s->idr_lock); | |
241 | return ERR_PTR(-ENOMEM); | |
242 | } | |
243 | con->conid = ret; | |
244 | s->idr_in_use++; | |
245 | spin_unlock_bh(&s->idr_lock); | |
246 | ||
247 | set_bit(CF_CONNECTED, &con->flags); | |
248 | con->server = s; | |
249 | ||
250 | return con; | |
251 | } | |
252 | ||
253 | static int tipc_receive_from_sock(struct tipc_conn *con) | |
254 | { | |
255 | struct msghdr msg = {}; | |
256 | struct tipc_server *s = con->server; | |
257 | struct sockaddr_tipc addr; | |
258 | struct kvec iov; | |
259 | void *buf; | |
260 | int ret; | |
261 | ||
262 | buf = kmem_cache_alloc(s->rcvbuf_cache, GFP_ATOMIC); | |
263 | if (!buf) { | |
264 | ret = -ENOMEM; | |
265 | goto out_close; | |
266 | } | |
267 | ||
268 | iov.iov_base = buf; | |
269 | iov.iov_len = s->max_rcvbuf_size; | |
270 | msg.msg_name = &addr; | |
271 | ret = kernel_recvmsg(con->sock, &msg, &iov, 1, iov.iov_len, | |
272 | MSG_DONTWAIT); | |
273 | if (ret <= 0) { | |
274 | kmem_cache_free(s->rcvbuf_cache, buf); | |
275 | goto out_close; | |
276 | } | |
277 | ||
4ac1c8d0 YX |
278 | s->tipc_conn_recvmsg(sock_net(con->sock->sk), con->conid, &addr, |
279 | con->usr_data, buf, ret); | |
c5fa7b3c YX |
280 | |
281 | kmem_cache_free(s->rcvbuf_cache, buf); | |
282 | ||
283 | return 0; | |
284 | ||
285 | out_close: | |
286 | if (ret != -EWOULDBLOCK) | |
287 | tipc_close_conn(con); | |
288 | else if (ret == 0) | |
289 | /* Don't return success if we really got EOF */ | |
290 | ret = -EAGAIN; | |
291 | ||
292 | return ret; | |
293 | } | |
294 | ||
295 | static int tipc_accept_from_sock(struct tipc_conn *con) | |
296 | { | |
297 | struct tipc_server *s = con->server; | |
298 | struct socket *sock = con->sock; | |
299 | struct socket *newsock; | |
300 | struct tipc_conn *newcon; | |
301 | int ret; | |
302 | ||
76100a8a | 303 | ret = kernel_accept(sock, &newsock, O_NONBLOCK); |
c5fa7b3c YX |
304 | if (ret < 0) |
305 | return ret; | |
306 | ||
307 | newcon = tipc_alloc_conn(con->server); | |
308 | if (IS_ERR(newcon)) { | |
309 | ret = PTR_ERR(newcon); | |
310 | sock_release(newsock); | |
311 | return ret; | |
312 | } | |
313 | ||
314 | newcon->rx_action = tipc_receive_from_sock; | |
315 | tipc_register_callbacks(newsock, newcon); | |
316 | ||
317 | /* Notify that new connection is incoming */ | |
318 | newcon->usr_data = s->tipc_conn_new(newcon->conid); | |
90bdfcb7 YX |
319 | if (!newcon->usr_data) { |
320 | sock_release(newsock); | |
321 | return -ENOMEM; | |
322 | } | |
c5fa7b3c YX |
323 | |
324 | /* Wake up receive process in case of 'SYN+' message */ | |
676d2369 | 325 | newsock->sk->sk_data_ready(newsock->sk); |
c5fa7b3c YX |
326 | return ret; |
327 | } | |
328 | ||
329 | static struct socket *tipc_create_listen_sock(struct tipc_conn *con) | |
330 | { | |
331 | struct tipc_server *s = con->server; | |
332 | struct socket *sock = NULL; | |
333 | int ret; | |
334 | ||
fa787ae0 | 335 | ret = sock_create_kern(s->net, AF_TIPC, SOCK_SEQPACKET, 0, &sock); |
c5fa7b3c YX |
336 | if (ret < 0) |
337 | return NULL; | |
338 | ret = kernel_setsockopt(sock, SOL_TIPC, TIPC_IMPORTANCE, | |
339 | (char *)&s->imp, sizeof(s->imp)); | |
340 | if (ret < 0) | |
341 | goto create_err; | |
342 | ret = kernel_bind(sock, (struct sockaddr *)s->saddr, sizeof(*s->saddr)); | |
343 | if (ret < 0) | |
344 | goto create_err; | |
345 | ||
346 | switch (s->type) { | |
347 | case SOCK_STREAM: | |
348 | case SOCK_SEQPACKET: | |
349 | con->rx_action = tipc_accept_from_sock; | |
350 | ||
351 | ret = kernel_listen(sock, 0); | |
352 | if (ret < 0) | |
353 | goto create_err; | |
354 | break; | |
355 | case SOCK_DGRAM: | |
356 | case SOCK_RDM: | |
357 | con->rx_action = tipc_receive_from_sock; | |
358 | break; | |
359 | default: | |
360 | pr_err("Unknown socket type %d\n", s->type); | |
361 | goto create_err; | |
362 | } | |
76100a8a YX |
363 | |
364 | /* As server's listening socket owner and creator is the same module, | |
365 | * we have to decrease TIPC module reference count to guarantee that | |
366 | * it remains zero after the server socket is created, otherwise, | |
367 | * executing "rmmod" command is unable to make TIPC module deleted | |
368 | * after TIPC module is inserted successfully. | |
369 | * | |
370 | * However, the reference count is ever increased twice in | |
371 | * sock_create_kern(): one is to increase the reference count of owner | |
372 | * of TIPC socket's proto_ops struct; another is to increment the | |
373 | * reference count of owner of TIPC proto struct. Therefore, we must | |
374 | * decrement the module reference count twice to ensure that it keeps | |
375 | * zero after server's listening socket is created. Of course, we | |
376 | * must bump the module reference count twice as well before the socket | |
377 | * is closed. | |
378 | */ | |
379 | module_put(sock->ops->owner); | |
380 | module_put(sock->sk->sk_prot_creator->owner); | |
381 | set_bit(CF_SERVER, &con->flags); | |
382 | ||
c5fa7b3c YX |
383 | return sock; |
384 | ||
385 | create_err: | |
76100a8a | 386 | kernel_sock_shutdown(sock, SHUT_RDWR); |
def81f69 | 387 | sock_release(sock); |
c5fa7b3c YX |
388 | return NULL; |
389 | } | |
390 | ||
391 | static int tipc_open_listening_sock(struct tipc_server *s) | |
392 | { | |
393 | struct socket *sock; | |
394 | struct tipc_conn *con; | |
395 | ||
396 | con = tipc_alloc_conn(s); | |
397 | if (IS_ERR(con)) | |
398 | return PTR_ERR(con); | |
399 | ||
400 | sock = tipc_create_listen_sock(con); | |
c756891a YX |
401 | if (!sock) { |
402 | idr_remove(&s->conn_idr, con->conid); | |
403 | s->idr_in_use--; | |
404 | kfree(con); | |
c5fa7b3c | 405 | return -EINVAL; |
c756891a | 406 | } |
c5fa7b3c YX |
407 | |
408 | tipc_register_callbacks(sock, con); | |
409 | return 0; | |
410 | } | |
411 | ||
412 | static struct outqueue_entry *tipc_alloc_entry(void *data, int len) | |
413 | { | |
414 | struct outqueue_entry *entry; | |
415 | void *buf; | |
416 | ||
417 | entry = kmalloc(sizeof(struct outqueue_entry), GFP_ATOMIC); | |
418 | if (!entry) | |
419 | return NULL; | |
420 | ||
421 | buf = kmalloc(len, GFP_ATOMIC); | |
422 | if (!buf) { | |
423 | kfree(entry); | |
424 | return NULL; | |
425 | } | |
426 | ||
427 | memcpy(buf, data, len); | |
428 | entry->iov.iov_base = buf; | |
429 | entry->iov.iov_len = len; | |
430 | ||
431 | return entry; | |
432 | } | |
433 | ||
434 | static void tipc_free_entry(struct outqueue_entry *e) | |
435 | { | |
436 | kfree(e->iov.iov_base); | |
437 | kfree(e); | |
438 | } | |
439 | ||
440 | static void tipc_clean_outqueues(struct tipc_conn *con) | |
441 | { | |
442 | struct outqueue_entry *e, *safe; | |
443 | ||
444 | spin_lock_bh(&con->outqueue_lock); | |
445 | list_for_each_entry_safe(e, safe, &con->outqueue, list) { | |
446 | list_del(&e->list); | |
447 | tipc_free_entry(e); | |
448 | } | |
449 | spin_unlock_bh(&con->outqueue_lock); | |
450 | } | |
451 | ||
452 | int tipc_conn_sendmsg(struct tipc_server *s, int conid, | |
453 | struct sockaddr_tipc *addr, void *data, size_t len) | |
454 | { | |
455 | struct outqueue_entry *e; | |
456 | struct tipc_conn *con; | |
457 | ||
458 | con = tipc_conn_lookup(s, conid); | |
459 | if (!con) | |
460 | return -EINVAL; | |
461 | ||
462 | e = tipc_alloc_entry(data, len); | |
463 | if (!e) { | |
464 | conn_put(con); | |
465 | return -ENOMEM; | |
466 | } | |
467 | ||
468 | if (addr) | |
469 | memcpy(&e->dest, addr, sizeof(struct sockaddr_tipc)); | |
470 | ||
471 | spin_lock_bh(&con->outqueue_lock); | |
472 | list_add_tail(&e->list, &con->outqueue); | |
473 | spin_unlock_bh(&con->outqueue_lock); | |
474 | ||
4652edb7 | 475 | if (test_bit(CF_CONNECTED, &con->flags)) { |
c5fa7b3c YX |
476 | if (!queue_work(s->send_wq, &con->swork)) |
477 | conn_put(con); | |
4652edb7 YX |
478 | } else { |
479 | conn_put(con); | |
480 | } | |
c5fa7b3c YX |
481 | return 0; |
482 | } | |
483 | ||
484 | void tipc_conn_terminate(struct tipc_server *s, int conid) | |
485 | { | |
486 | struct tipc_conn *con; | |
487 | ||
488 | con = tipc_conn_lookup(s, conid); | |
489 | if (con) { | |
490 | tipc_close_conn(con); | |
491 | conn_put(con); | |
492 | } | |
493 | } | |
494 | ||
495 | static void tipc_send_to_sock(struct tipc_conn *con) | |
496 | { | |
497 | int count = 0; | |
498 | struct tipc_server *s = con->server; | |
499 | struct outqueue_entry *e; | |
500 | struct msghdr msg; | |
501 | int ret; | |
502 | ||
503 | spin_lock_bh(&con->outqueue_lock); | |
504 | while (1) { | |
505 | e = list_entry(con->outqueue.next, struct outqueue_entry, | |
506 | list); | |
507 | if ((struct list_head *) e == &con->outqueue) | |
508 | break; | |
509 | spin_unlock_bh(&con->outqueue_lock); | |
510 | ||
511 | memset(&msg, 0, sizeof(msg)); | |
512 | msg.msg_flags = MSG_DONTWAIT; | |
513 | ||
514 | if (s->type == SOCK_DGRAM || s->type == SOCK_RDM) { | |
515 | msg.msg_name = &e->dest; | |
516 | msg.msg_namelen = sizeof(struct sockaddr_tipc); | |
517 | } | |
518 | ret = kernel_sendmsg(con->sock, &msg, &e->iov, 1, | |
519 | e->iov.iov_len); | |
520 | if (ret == -EWOULDBLOCK || ret == 0) { | |
521 | cond_resched(); | |
522 | goto out; | |
523 | } else if (ret < 0) { | |
524 | goto send_err; | |
525 | } | |
526 | ||
527 | /* Don't starve users filling buffers */ | |
528 | if (++count >= MAX_SEND_MSG_COUNT) { | |
529 | cond_resched(); | |
530 | count = 0; | |
531 | } | |
532 | ||
533 | spin_lock_bh(&con->outqueue_lock); | |
534 | list_del(&e->list); | |
535 | tipc_free_entry(e); | |
536 | } | |
537 | spin_unlock_bh(&con->outqueue_lock); | |
538 | out: | |
539 | return; | |
540 | ||
541 | send_err: | |
542 | tipc_close_conn(con); | |
543 | } | |
544 | ||
545 | static void tipc_recv_work(struct work_struct *work) | |
546 | { | |
547 | struct tipc_conn *con = container_of(work, struct tipc_conn, rwork); | |
548 | int count = 0; | |
549 | ||
550 | while (test_bit(CF_CONNECTED, &con->flags)) { | |
551 | if (con->rx_action(con)) | |
552 | break; | |
553 | ||
554 | /* Don't flood Rx machine */ | |
555 | if (++count >= MAX_RECV_MSG_COUNT) { | |
556 | cond_resched(); | |
557 | count = 0; | |
558 | } | |
559 | } | |
560 | conn_put(con); | |
561 | } | |
562 | ||
563 | static void tipc_send_work(struct work_struct *work) | |
564 | { | |
565 | struct tipc_conn *con = container_of(work, struct tipc_conn, swork); | |
566 | ||
567 | if (test_bit(CF_CONNECTED, &con->flags)) | |
568 | tipc_send_to_sock(con); | |
569 | ||
570 | conn_put(con); | |
571 | } | |
572 | ||
573 | static void tipc_work_stop(struct tipc_server *s) | |
574 | { | |
575 | destroy_workqueue(s->rcv_wq); | |
576 | destroy_workqueue(s->send_wq); | |
577 | } | |
578 | ||
579 | static int tipc_work_start(struct tipc_server *s) | |
580 | { | |
06c8581f | 581 | s->rcv_wq = alloc_ordered_workqueue("tipc_rcv", 0); |
c5fa7b3c YX |
582 | if (!s->rcv_wq) { |
583 | pr_err("can't start tipc receive workqueue\n"); | |
584 | return -ENOMEM; | |
585 | } | |
586 | ||
06c8581f | 587 | s->send_wq = alloc_ordered_workqueue("tipc_send", 0); |
c5fa7b3c YX |
588 | if (!s->send_wq) { |
589 | pr_err("can't start tipc send workqueue\n"); | |
590 | destroy_workqueue(s->rcv_wq); | |
591 | return -ENOMEM; | |
592 | } | |
593 | ||
594 | return 0; | |
595 | } | |
596 | ||
597 | int tipc_server_start(struct tipc_server *s) | |
598 | { | |
599 | int ret; | |
600 | ||
601 | spin_lock_init(&s->idr_lock); | |
602 | idr_init(&s->conn_idr); | |
603 | s->idr_in_use = 0; | |
604 | ||
605 | s->rcvbuf_cache = kmem_cache_create(s->name, s->max_rcvbuf_size, | |
606 | 0, SLAB_HWCACHE_ALIGN, NULL); | |
607 | if (!s->rcvbuf_cache) | |
608 | return -ENOMEM; | |
609 | ||
610 | ret = tipc_work_start(s); | |
611 | if (ret < 0) { | |
612 | kmem_cache_destroy(s->rcvbuf_cache); | |
613 | return ret; | |
614 | } | |
c756891a YX |
615 | ret = tipc_open_listening_sock(s); |
616 | if (ret < 0) { | |
617 | tipc_work_stop(s); | |
618 | kmem_cache_destroy(s->rcvbuf_cache); | |
619 | return ret; | |
620 | } | |
c756891a | 621 | return ret; |
c5fa7b3c YX |
622 | } |
623 | ||
624 | void tipc_server_stop(struct tipc_server *s) | |
625 | { | |
626 | struct tipc_conn *con; | |
627 | int total = 0; | |
628 | int id; | |
629 | ||
c5fa7b3c YX |
630 | spin_lock_bh(&s->idr_lock); |
631 | for (id = 0; total < s->idr_in_use; id++) { | |
632 | con = idr_find(&s->conn_idr, id); | |
633 | if (con) { | |
634 | total++; | |
635 | spin_unlock_bh(&s->idr_lock); | |
636 | tipc_close_conn(con); | |
637 | spin_lock_bh(&s->idr_lock); | |
638 | } | |
639 | } | |
640 | spin_unlock_bh(&s->idr_lock); | |
641 | ||
642 | tipc_work_stop(s); | |
643 | kmem_cache_destroy(s->rcvbuf_cache); | |
644 | idr_destroy(&s->conn_idr); | |
645 | } |