tipc: make tipc socket support net namespace
[linux-2.6-block.git] / net / tipc / server.c
CommitLineData
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
YX
39#include <net/sock.h>
40
41/* Number of messages to send before rescheduling */
42#define MAX_SEND_MSG_COUNT 25
43#define MAX_RECV_MSG_COUNT 25
44#define CF_CONNECTED 1
45
46#define sock2con(x) ((struct tipc_conn *)(x)->sk_user_data)
47
48/**
49 * struct tipc_conn - TIPC connection structure
50 * @kref: reference counter to connection object
51 * @conid: connection identifier
52 * @sock: socket handler associated with connection
53 * @flags: indicates connection state
54 * @server: pointer to connected server
55 * @rwork: receive work item
56 * @usr_data: user-specified field
57 * @rx_action: what to do when connection socket is active
58 * @outqueue: pointer to first outbound message in queue
963a1855 59 * @outqueue_lock: control access to the outqueue
c5fa7b3c
YX
60 * @outqueue: list of connection objects for its server
61 * @swork: send work item
62 */
63struct tipc_conn {
64 struct kref kref;
65 int conid;
66 struct socket *sock;
67 unsigned long flags;
68 struct tipc_server *server;
69 struct work_struct rwork;
70 int (*rx_action) (struct tipc_conn *con);
71 void *usr_data;
72 struct list_head outqueue;
73 spinlock_t outqueue_lock;
74 struct work_struct swork;
75};
76
77/* An entry waiting to be sent */
78struct outqueue_entry {
79 struct list_head list;
80 struct kvec iov;
81 struct sockaddr_tipc dest;
82};
83
84static void tipc_recv_work(struct work_struct *work);
85static void tipc_send_work(struct work_struct *work);
86static void tipc_clean_outqueues(struct tipc_conn *con);
87
88static void tipc_conn_kref_release(struct kref *kref)
89{
90 struct tipc_conn *con = container_of(kref, struct tipc_conn, kref);
c5fa7b3c
YX
91
92 if (con->sock) {
93 tipc_sock_release_local(con->sock);
94 con->sock = NULL;
95 }
96
97 tipc_clean_outqueues(con);
c5fa7b3c
YX
98 kfree(con);
99}
100
101static void conn_put(struct tipc_conn *con)
102{
103 kref_put(&con->kref, tipc_conn_kref_release);
104}
105
106static void conn_get(struct tipc_conn *con)
107{
108 kref_get(&con->kref);
109}
110
111static struct tipc_conn *tipc_conn_lookup(struct tipc_server *s, int conid)
112{
113 struct tipc_conn *con;
114
115 spin_lock_bh(&s->idr_lock);
116 con = idr_find(&s->conn_idr, conid);
117 if (con)
118 conn_get(con);
119 spin_unlock_bh(&s->idr_lock);
120 return con;
121}
122
676d2369 123static void sock_data_ready(struct sock *sk)
c5fa7b3c
YX
124{
125 struct tipc_conn *con;
126
127 read_lock(&sk->sk_callback_lock);
128 con = sock2con(sk);
129 if (con && test_bit(CF_CONNECTED, &con->flags)) {
130 conn_get(con);
131 if (!queue_work(con->server->rcv_wq, &con->rwork))
132 conn_put(con);
133 }
134 read_unlock(&sk->sk_callback_lock);
135}
136
137static void sock_write_space(struct sock *sk)
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->send_wq, &con->swork))
146 conn_put(con);
147 }
148 read_unlock(&sk->sk_callback_lock);
149}
150
151static void tipc_register_callbacks(struct socket *sock, struct tipc_conn *con)
152{
153 struct sock *sk = sock->sk;
154
155 write_lock_bh(&sk->sk_callback_lock);
156
157 sk->sk_data_ready = sock_data_ready;
158 sk->sk_write_space = sock_write_space;
159 sk->sk_user_data = con;
160
161 con->sock = sock;
162
163 write_unlock_bh(&sk->sk_callback_lock);
164}
165
166static void tipc_unregister_callbacks(struct tipc_conn *con)
167{
168 struct sock *sk = con->sock->sk;
169
170 write_lock_bh(&sk->sk_callback_lock);
171 sk->sk_user_data = NULL;
172 write_unlock_bh(&sk->sk_callback_lock);
173}
174
175static void tipc_close_conn(struct tipc_conn *con)
176{
177 struct tipc_server *s = con->server;
178
179 if (test_and_clear_bit(CF_CONNECTED, &con->flags)) {
6d4ebeb4
YX
180 if (con->conid)
181 s->tipc_conn_shutdown(con->conid, con->usr_data);
182
c5fa7b3c
YX
183 spin_lock_bh(&s->idr_lock);
184 idr_remove(&s->conn_idr, con->conid);
185 s->idr_in_use--;
186 spin_unlock_bh(&s->idr_lock);
187
188 tipc_unregister_callbacks(con);
189
190 /* We shouldn't flush pending works as we may be in the
191 * thread. In fact the races with pending rx/tx work structs
192 * are harmless for us here as we have already deleted this
193 * connection from server connection list and set
194 * sk->sk_user_data to 0 before releasing connection object.
195 */
196 kernel_sock_shutdown(con->sock, SHUT_RDWR);
197
198 conn_put(con);
199 }
200}
201
202static struct tipc_conn *tipc_alloc_conn(struct tipc_server *s)
203{
204 struct tipc_conn *con;
205 int ret;
206
207 con = kzalloc(sizeof(struct tipc_conn), GFP_ATOMIC);
208 if (!con)
209 return ERR_PTR(-ENOMEM);
210
211 kref_init(&con->kref);
212 INIT_LIST_HEAD(&con->outqueue);
213 spin_lock_init(&con->outqueue_lock);
214 INIT_WORK(&con->swork, tipc_send_work);
215 INIT_WORK(&con->rwork, tipc_recv_work);
216
217 spin_lock_bh(&s->idr_lock);
218 ret = idr_alloc(&s->conn_idr, con, 0, 0, GFP_ATOMIC);
219 if (ret < 0) {
220 kfree(con);
221 spin_unlock_bh(&s->idr_lock);
222 return ERR_PTR(-ENOMEM);
223 }
224 con->conid = ret;
225 s->idr_in_use++;
226 spin_unlock_bh(&s->idr_lock);
227
228 set_bit(CF_CONNECTED, &con->flags);
229 con->server = s;
230
231 return con;
232}
233
234static int tipc_receive_from_sock(struct tipc_conn *con)
235{
236 struct msghdr msg = {};
237 struct tipc_server *s = con->server;
238 struct sockaddr_tipc addr;
239 struct kvec iov;
240 void *buf;
241 int ret;
242
243 buf = kmem_cache_alloc(s->rcvbuf_cache, GFP_ATOMIC);
244 if (!buf) {
245 ret = -ENOMEM;
246 goto out_close;
247 }
248
249 iov.iov_base = buf;
250 iov.iov_len = s->max_rcvbuf_size;
251 msg.msg_name = &addr;
252 ret = kernel_recvmsg(con->sock, &msg, &iov, 1, iov.iov_len,
253 MSG_DONTWAIT);
254 if (ret <= 0) {
255 kmem_cache_free(s->rcvbuf_cache, buf);
256 goto out_close;
257 }
258
259 s->tipc_conn_recvmsg(con->conid, &addr, con->usr_data, buf, ret);
260
261 kmem_cache_free(s->rcvbuf_cache, buf);
262
263 return 0;
264
265out_close:
266 if (ret != -EWOULDBLOCK)
267 tipc_close_conn(con);
268 else if (ret == 0)
269 /* Don't return success if we really got EOF */
270 ret = -EAGAIN;
271
272 return ret;
273}
274
275static int tipc_accept_from_sock(struct tipc_conn *con)
276{
277 struct tipc_server *s = con->server;
278 struct socket *sock = con->sock;
279 struct socket *newsock;
280 struct tipc_conn *newcon;
281 int ret;
282
283 ret = tipc_sock_accept_local(sock, &newsock, O_NONBLOCK);
284 if (ret < 0)
285 return ret;
286
287 newcon = tipc_alloc_conn(con->server);
288 if (IS_ERR(newcon)) {
289 ret = PTR_ERR(newcon);
290 sock_release(newsock);
291 return ret;
292 }
293
294 newcon->rx_action = tipc_receive_from_sock;
295 tipc_register_callbacks(newsock, newcon);
296
297 /* Notify that new connection is incoming */
298 newcon->usr_data = s->tipc_conn_new(newcon->conid);
299
300 /* Wake up receive process in case of 'SYN+' message */
676d2369 301 newsock->sk->sk_data_ready(newsock->sk);
c5fa7b3c
YX
302 return ret;
303}
304
305static struct socket *tipc_create_listen_sock(struct tipc_conn *con)
306{
307 struct tipc_server *s = con->server;
308 struct socket *sock = NULL;
309 int ret;
310
311 ret = tipc_sock_create_local(s->type, &sock);
312 if (ret < 0)
313 return NULL;
314 ret = kernel_setsockopt(sock, SOL_TIPC, TIPC_IMPORTANCE,
315 (char *)&s->imp, sizeof(s->imp));
316 if (ret < 0)
317 goto create_err;
318 ret = kernel_bind(sock, (struct sockaddr *)s->saddr, sizeof(*s->saddr));
319 if (ret < 0)
320 goto create_err;
321
322 switch (s->type) {
323 case SOCK_STREAM:
324 case SOCK_SEQPACKET:
325 con->rx_action = tipc_accept_from_sock;
326
327 ret = kernel_listen(sock, 0);
328 if (ret < 0)
329 goto create_err;
330 break;
331 case SOCK_DGRAM:
332 case SOCK_RDM:
333 con->rx_action = tipc_receive_from_sock;
334 break;
335 default:
336 pr_err("Unknown socket type %d\n", s->type);
337 goto create_err;
338 }
339 return sock;
340
341create_err:
342 sock_release(sock);
343 con->sock = NULL;
344 return NULL;
345}
346
347static int tipc_open_listening_sock(struct tipc_server *s)
348{
349 struct socket *sock;
350 struct tipc_conn *con;
351
352 con = tipc_alloc_conn(s);
353 if (IS_ERR(con))
354 return PTR_ERR(con);
355
356 sock = tipc_create_listen_sock(con);
c756891a
YX
357 if (!sock) {
358 idr_remove(&s->conn_idr, con->conid);
359 s->idr_in_use--;
360 kfree(con);
c5fa7b3c 361 return -EINVAL;
c756891a 362 }
c5fa7b3c
YX
363
364 tipc_register_callbacks(sock, con);
365 return 0;
366}
367
368static struct outqueue_entry *tipc_alloc_entry(void *data, int len)
369{
370 struct outqueue_entry *entry;
371 void *buf;
372
373 entry = kmalloc(sizeof(struct outqueue_entry), GFP_ATOMIC);
374 if (!entry)
375 return NULL;
376
377 buf = kmalloc(len, GFP_ATOMIC);
378 if (!buf) {
379 kfree(entry);
380 return NULL;
381 }
382
383 memcpy(buf, data, len);
384 entry->iov.iov_base = buf;
385 entry->iov.iov_len = len;
386
387 return entry;
388}
389
390static void tipc_free_entry(struct outqueue_entry *e)
391{
392 kfree(e->iov.iov_base);
393 kfree(e);
394}
395
396static void tipc_clean_outqueues(struct tipc_conn *con)
397{
398 struct outqueue_entry *e, *safe;
399
400 spin_lock_bh(&con->outqueue_lock);
401 list_for_each_entry_safe(e, safe, &con->outqueue, list) {
402 list_del(&e->list);
403 tipc_free_entry(e);
404 }
405 spin_unlock_bh(&con->outqueue_lock);
406}
407
408int tipc_conn_sendmsg(struct tipc_server *s, int conid,
409 struct sockaddr_tipc *addr, void *data, size_t len)
410{
411 struct outqueue_entry *e;
412 struct tipc_conn *con;
413
414 con = tipc_conn_lookup(s, conid);
415 if (!con)
416 return -EINVAL;
417
418 e = tipc_alloc_entry(data, len);
419 if (!e) {
420 conn_put(con);
421 return -ENOMEM;
422 }
423
424 if (addr)
425 memcpy(&e->dest, addr, sizeof(struct sockaddr_tipc));
426
427 spin_lock_bh(&con->outqueue_lock);
428 list_add_tail(&e->list, &con->outqueue);
429 spin_unlock_bh(&con->outqueue_lock);
430
4652edb7 431 if (test_bit(CF_CONNECTED, &con->flags)) {
c5fa7b3c
YX
432 if (!queue_work(s->send_wq, &con->swork))
433 conn_put(con);
4652edb7
YX
434 } else {
435 conn_put(con);
436 }
c5fa7b3c
YX
437 return 0;
438}
439
440void tipc_conn_terminate(struct tipc_server *s, int conid)
441{
442 struct tipc_conn *con;
443
444 con = tipc_conn_lookup(s, conid);
445 if (con) {
446 tipc_close_conn(con);
447 conn_put(con);
448 }
449}
450
451static void tipc_send_to_sock(struct tipc_conn *con)
452{
453 int count = 0;
454 struct tipc_server *s = con->server;
455 struct outqueue_entry *e;
456 struct msghdr msg;
457 int ret;
458
459 spin_lock_bh(&con->outqueue_lock);
460 while (1) {
461 e = list_entry(con->outqueue.next, struct outqueue_entry,
462 list);
463 if ((struct list_head *) e == &con->outqueue)
464 break;
465 spin_unlock_bh(&con->outqueue_lock);
466
467 memset(&msg, 0, sizeof(msg));
468 msg.msg_flags = MSG_DONTWAIT;
469
470 if (s->type == SOCK_DGRAM || s->type == SOCK_RDM) {
471 msg.msg_name = &e->dest;
472 msg.msg_namelen = sizeof(struct sockaddr_tipc);
473 }
474 ret = kernel_sendmsg(con->sock, &msg, &e->iov, 1,
475 e->iov.iov_len);
476 if (ret == -EWOULDBLOCK || ret == 0) {
477 cond_resched();
478 goto out;
479 } else if (ret < 0) {
480 goto send_err;
481 }
482
483 /* Don't starve users filling buffers */
484 if (++count >= MAX_SEND_MSG_COUNT) {
485 cond_resched();
486 count = 0;
487 }
488
489 spin_lock_bh(&con->outqueue_lock);
490 list_del(&e->list);
491 tipc_free_entry(e);
492 }
493 spin_unlock_bh(&con->outqueue_lock);
494out:
495 return;
496
497send_err:
498 tipc_close_conn(con);
499}
500
501static void tipc_recv_work(struct work_struct *work)
502{
503 struct tipc_conn *con = container_of(work, struct tipc_conn, rwork);
504 int count = 0;
505
506 while (test_bit(CF_CONNECTED, &con->flags)) {
507 if (con->rx_action(con))
508 break;
509
510 /* Don't flood Rx machine */
511 if (++count >= MAX_RECV_MSG_COUNT) {
512 cond_resched();
513 count = 0;
514 }
515 }
516 conn_put(con);
517}
518
519static void tipc_send_work(struct work_struct *work)
520{
521 struct tipc_conn *con = container_of(work, struct tipc_conn, swork);
522
523 if (test_bit(CF_CONNECTED, &con->flags))
524 tipc_send_to_sock(con);
525
526 conn_put(con);
527}
528
529static void tipc_work_stop(struct tipc_server *s)
530{
531 destroy_workqueue(s->rcv_wq);
532 destroy_workqueue(s->send_wq);
533}
534
535static int tipc_work_start(struct tipc_server *s)
536{
537 s->rcv_wq = alloc_workqueue("tipc_rcv", WQ_UNBOUND, 1);
538 if (!s->rcv_wq) {
539 pr_err("can't start tipc receive workqueue\n");
540 return -ENOMEM;
541 }
542
543 s->send_wq = alloc_workqueue("tipc_send", WQ_UNBOUND, 1);
544 if (!s->send_wq) {
545 pr_err("can't start tipc send workqueue\n");
546 destroy_workqueue(s->rcv_wq);
547 return -ENOMEM;
548 }
549
550 return 0;
551}
552
553int tipc_server_start(struct tipc_server *s)
554{
555 int ret;
556
557 spin_lock_init(&s->idr_lock);
558 idr_init(&s->conn_idr);
559 s->idr_in_use = 0;
560
561 s->rcvbuf_cache = kmem_cache_create(s->name, s->max_rcvbuf_size,
562 0, SLAB_HWCACHE_ALIGN, NULL);
563 if (!s->rcvbuf_cache)
564 return -ENOMEM;
565
566 ret = tipc_work_start(s);
567 if (ret < 0) {
568 kmem_cache_destroy(s->rcvbuf_cache);
569 return ret;
570 }
c756891a
YX
571 ret = tipc_open_listening_sock(s);
572 if (ret < 0) {
573 tipc_work_stop(s);
574 kmem_cache_destroy(s->rcvbuf_cache);
575 return ret;
576 }
c756891a 577 return ret;
c5fa7b3c
YX
578}
579
580void tipc_server_stop(struct tipc_server *s)
581{
582 struct tipc_conn *con;
583 int total = 0;
584 int id;
585
c5fa7b3c
YX
586 spin_lock_bh(&s->idr_lock);
587 for (id = 0; total < s->idr_in_use; id++) {
588 con = idr_find(&s->conn_idr, id);
589 if (con) {
590 total++;
591 spin_unlock_bh(&s->idr_lock);
592 tipc_close_conn(con);
593 spin_lock_bh(&s->idr_lock);
594 }
595 }
596 spin_unlock_bh(&s->idr_lock);
597
598 tipc_work_stop(s);
599 kmem_cache_destroy(s->rcvbuf_cache);
600 idr_destroy(&s->conn_idr);
601}