2 * net/tipc/server.c: TIPC server infrastructure
4 * Copyright (c) 2012-2013, Wind River Systems
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
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.
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.
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.
40 #include <linux/module.h>
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
48 #define sock2con(x) ((struct tipc_conn *)(x)->sk_user_data)
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
61 * @outqueue_lock: control access to the outqueue
62 * @outqueue: list of connection objects for its server
63 * @swork: send work item
70 struct tipc_server
*server
;
71 struct work_struct rwork
;
72 int (*rx_action
) (struct tipc_conn
*con
);
74 struct list_head outqueue
;
75 spinlock_t outqueue_lock
;
76 struct work_struct swork
;
79 /* An entry waiting to be sent */
80 struct outqueue_entry
{
81 struct list_head list
;
83 struct sockaddr_tipc dest
;
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
);
90 static void tipc_conn_kref_release(struct kref
*kref
)
92 struct tipc_conn
*con
= container_of(kref
, struct tipc_conn
, kref
);
93 struct tipc_server
*s
= con
->server
;
94 struct sockaddr_tipc
*saddr
= s
->saddr
;
95 struct socket
*sock
= con
->sock
;
100 if (test_bit(CF_SERVER
, &con
->flags
)) {
101 __module_get(sock
->ops
->owner
);
102 __module_get(sk
->sk_prot_creator
->owner
);
104 saddr
->scope
= -TIPC_NODE_SCOPE
;
105 kernel_bind(sock
, (struct sockaddr
*)saddr
, sizeof(*saddr
));
109 spin_lock_bh(&s
->idr_lock
);
110 idr_remove(&s
->conn_idr
, con
->conid
);
112 spin_unlock_bh(&s
->idr_lock
);
115 tipc_clean_outqueues(con
);
119 static void conn_put(struct tipc_conn
*con
)
121 kref_put(&con
->kref
, tipc_conn_kref_release
);
124 static void conn_get(struct tipc_conn
*con
)
126 kref_get(&con
->kref
);
129 static struct tipc_conn
*tipc_conn_lookup(struct tipc_server
*s
, int conid
)
131 struct tipc_conn
*con
;
133 spin_lock_bh(&s
->idr_lock
);
134 con
= idr_find(&s
->conn_idr
, conid
);
135 if (con
&& test_bit(CF_CONNECTED
, &con
->flags
))
139 spin_unlock_bh(&s
->idr_lock
);
143 static void sock_data_ready(struct sock
*sk
)
145 struct tipc_conn
*con
;
147 read_lock_bh(&sk
->sk_callback_lock
);
149 if (con
&& test_bit(CF_CONNECTED
, &con
->flags
)) {
151 if (!queue_work(con
->server
->rcv_wq
, &con
->rwork
))
154 read_unlock_bh(&sk
->sk_callback_lock
);
157 static void sock_write_space(struct sock
*sk
)
159 struct tipc_conn
*con
;
161 read_lock_bh(&sk
->sk_callback_lock
);
163 if (con
&& test_bit(CF_CONNECTED
, &con
->flags
)) {
165 if (!queue_work(con
->server
->send_wq
, &con
->swork
))
168 read_unlock_bh(&sk
->sk_callback_lock
);
171 static void tipc_register_callbacks(struct socket
*sock
, struct tipc_conn
*con
)
173 struct sock
*sk
= sock
->sk
;
175 write_lock_bh(&sk
->sk_callback_lock
);
177 sk
->sk_data_ready
= sock_data_ready
;
178 sk
->sk_write_space
= sock_write_space
;
179 sk
->sk_user_data
= con
;
183 write_unlock_bh(&sk
->sk_callback_lock
);
186 static void tipc_unregister_callbacks(struct tipc_conn
*con
)
188 struct sock
*sk
= con
->sock
->sk
;
190 write_lock_bh(&sk
->sk_callback_lock
);
191 sk
->sk_user_data
= NULL
;
192 write_unlock_bh(&sk
->sk_callback_lock
);
195 static void tipc_close_conn(struct tipc_conn
*con
)
197 struct tipc_server
*s
= con
->server
;
199 if (test_and_clear_bit(CF_CONNECTED
, &con
->flags
)) {
200 tipc_unregister_callbacks(con
);
203 s
->tipc_conn_release(con
->conid
, con
->usr_data
);
205 /* We shouldn't flush pending works as we may be in the
206 * thread. In fact the races with pending rx/tx work structs
207 * are harmless for us here as we have already deleted this
208 * connection from server connection list.
210 kernel_sock_shutdown(con
->sock
, SHUT_RDWR
);
216 static struct tipc_conn
*tipc_alloc_conn(struct tipc_server
*s
)
218 struct tipc_conn
*con
;
221 con
= kzalloc(sizeof(struct tipc_conn
), GFP_ATOMIC
);
223 return ERR_PTR(-ENOMEM
);
225 kref_init(&con
->kref
);
226 INIT_LIST_HEAD(&con
->outqueue
);
227 spin_lock_init(&con
->outqueue_lock
);
228 INIT_WORK(&con
->swork
, tipc_send_work
);
229 INIT_WORK(&con
->rwork
, tipc_recv_work
);
231 spin_lock_bh(&s
->idr_lock
);
232 ret
= idr_alloc(&s
->conn_idr
, con
, 0, 0, GFP_ATOMIC
);
235 spin_unlock_bh(&s
->idr_lock
);
236 return ERR_PTR(-ENOMEM
);
240 spin_unlock_bh(&s
->idr_lock
);
242 set_bit(CF_CONNECTED
, &con
->flags
);
248 static int tipc_receive_from_sock(struct tipc_conn
*con
)
250 struct msghdr msg
= {};
251 struct tipc_server
*s
= con
->server
;
252 struct sockaddr_tipc addr
;
257 buf
= kmem_cache_alloc(s
->rcvbuf_cache
, GFP_ATOMIC
);
264 iov
.iov_len
= s
->max_rcvbuf_size
;
265 msg
.msg_name
= &addr
;
266 ret
= kernel_recvmsg(con
->sock
, &msg
, &iov
, 1, iov
.iov_len
,
269 kmem_cache_free(s
->rcvbuf_cache
, buf
);
273 s
->tipc_conn_recvmsg(sock_net(con
->sock
->sk
), con
->conid
, &addr
,
274 con
->usr_data
, buf
, ret
);
276 kmem_cache_free(s
->rcvbuf_cache
, buf
);
281 if (ret
!= -EWOULDBLOCK
)
282 tipc_close_conn(con
);
284 /* Don't return success if we really got EOF */
290 static int tipc_accept_from_sock(struct tipc_conn
*con
)
292 struct tipc_server
*s
= con
->server
;
293 struct socket
*sock
= con
->sock
;
294 struct socket
*newsock
;
295 struct tipc_conn
*newcon
;
298 ret
= kernel_accept(sock
, &newsock
, O_NONBLOCK
);
302 newcon
= tipc_alloc_conn(con
->server
);
303 if (IS_ERR(newcon
)) {
304 ret
= PTR_ERR(newcon
);
305 sock_release(newsock
);
309 newcon
->rx_action
= tipc_receive_from_sock
;
310 tipc_register_callbacks(newsock
, newcon
);
312 /* Notify that new connection is incoming */
313 newcon
->usr_data
= s
->tipc_conn_new(newcon
->conid
);
314 if (!newcon
->usr_data
) {
315 sock_release(newsock
);
320 /* Wake up receive process in case of 'SYN+' message */
321 newsock
->sk
->sk_data_ready(newsock
->sk
);
325 static struct socket
*tipc_create_listen_sock(struct tipc_conn
*con
)
327 struct tipc_server
*s
= con
->server
;
328 struct socket
*sock
= NULL
;
331 ret
= sock_create_kern(s
->net
, AF_TIPC
, SOCK_SEQPACKET
, 0, &sock
);
334 ret
= kernel_setsockopt(sock
, SOL_TIPC
, TIPC_IMPORTANCE
,
335 (char *)&s
->imp
, sizeof(s
->imp
));
338 ret
= kernel_bind(sock
, (struct sockaddr
*)s
->saddr
, sizeof(*s
->saddr
));
345 con
->rx_action
= tipc_accept_from_sock
;
347 ret
= kernel_listen(sock
, 0);
353 con
->rx_action
= tipc_receive_from_sock
;
356 pr_err("Unknown socket type %d\n", s
->type
);
360 /* As server's listening socket owner and creator is the same module,
361 * we have to decrease TIPC module reference count to guarantee that
362 * it remains zero after the server socket is created, otherwise,
363 * executing "rmmod" command is unable to make TIPC module deleted
364 * after TIPC module is inserted successfully.
366 * However, the reference count is ever increased twice in
367 * sock_create_kern(): one is to increase the reference count of owner
368 * of TIPC socket's proto_ops struct; another is to increment the
369 * reference count of owner of TIPC proto struct. Therefore, we must
370 * decrement the module reference count twice to ensure that it keeps
371 * zero after server's listening socket is created. Of course, we
372 * must bump the module reference count twice as well before the socket
375 module_put(sock
->ops
->owner
);
376 module_put(sock
->sk
->sk_prot_creator
->owner
);
377 set_bit(CF_SERVER
, &con
->flags
);
382 kernel_sock_shutdown(sock
, SHUT_RDWR
);
387 static int tipc_open_listening_sock(struct tipc_server
*s
)
390 struct tipc_conn
*con
;
392 con
= tipc_alloc_conn(s
);
396 sock
= tipc_create_listen_sock(con
);
398 idr_remove(&s
->conn_idr
, con
->conid
);
404 tipc_register_callbacks(sock
, con
);
408 static struct outqueue_entry
*tipc_alloc_entry(void *data
, int len
)
410 struct outqueue_entry
*entry
;
413 entry
= kmalloc(sizeof(struct outqueue_entry
), GFP_ATOMIC
);
417 buf
= kmemdup(data
, len
, GFP_ATOMIC
);
423 entry
->iov
.iov_base
= buf
;
424 entry
->iov
.iov_len
= len
;
429 static void tipc_free_entry(struct outqueue_entry
*e
)
431 kfree(e
->iov
.iov_base
);
435 static void tipc_clean_outqueues(struct tipc_conn
*con
)
437 struct outqueue_entry
*e
, *safe
;
439 spin_lock_bh(&con
->outqueue_lock
);
440 list_for_each_entry_safe(e
, safe
, &con
->outqueue
, list
) {
444 spin_unlock_bh(&con
->outqueue_lock
);
447 int tipc_conn_sendmsg(struct tipc_server
*s
, int conid
,
448 struct sockaddr_tipc
*addr
, void *data
, size_t len
)
450 struct outqueue_entry
*e
;
451 struct tipc_conn
*con
;
453 con
= tipc_conn_lookup(s
, conid
);
457 if (!test_bit(CF_CONNECTED
, &con
->flags
)) {
462 e
= tipc_alloc_entry(data
, len
);
469 memcpy(&e
->dest
, addr
, sizeof(struct sockaddr_tipc
));
471 spin_lock_bh(&con
->outqueue_lock
);
472 list_add_tail(&e
->list
, &con
->outqueue
);
473 spin_unlock_bh(&con
->outqueue_lock
);
475 if (!queue_work(s
->send_wq
, &con
->swork
))
480 void tipc_conn_terminate(struct tipc_server
*s
, int conid
)
482 struct tipc_conn
*con
;
484 con
= tipc_conn_lookup(s
, conid
);
486 tipc_close_conn(con
);
491 static void tipc_send_to_sock(struct tipc_conn
*con
)
494 struct tipc_server
*s
= con
->server
;
495 struct outqueue_entry
*e
;
499 spin_lock_bh(&con
->outqueue_lock
);
500 while (test_bit(CF_CONNECTED
, &con
->flags
)) {
501 e
= list_entry(con
->outqueue
.next
, struct outqueue_entry
,
503 if ((struct list_head
*) e
== &con
->outqueue
)
505 spin_unlock_bh(&con
->outqueue_lock
);
507 memset(&msg
, 0, sizeof(msg
));
508 msg
.msg_flags
= MSG_DONTWAIT
;
510 if (s
->type
== SOCK_DGRAM
|| s
->type
== SOCK_RDM
) {
511 msg
.msg_name
= &e
->dest
;
512 msg
.msg_namelen
= sizeof(struct sockaddr_tipc
);
514 ret
= kernel_sendmsg(con
->sock
, &msg
, &e
->iov
, 1,
516 if (ret
== -EWOULDBLOCK
|| ret
== 0) {
519 } else if (ret
< 0) {
523 /* Don't starve users filling buffers */
524 if (++count
>= MAX_SEND_MSG_COUNT
) {
529 spin_lock_bh(&con
->outqueue_lock
);
533 spin_unlock_bh(&con
->outqueue_lock
);
538 tipc_close_conn(con
);
541 static void tipc_recv_work(struct work_struct
*work
)
543 struct tipc_conn
*con
= container_of(work
, struct tipc_conn
, rwork
);
546 while (test_bit(CF_CONNECTED
, &con
->flags
)) {
547 if (con
->rx_action(con
))
550 /* Don't flood Rx machine */
551 if (++count
>= MAX_RECV_MSG_COUNT
) {
559 static void tipc_send_work(struct work_struct
*work
)
561 struct tipc_conn
*con
= container_of(work
, struct tipc_conn
, swork
);
563 if (test_bit(CF_CONNECTED
, &con
->flags
))
564 tipc_send_to_sock(con
);
569 static void tipc_work_stop(struct tipc_server
*s
)
571 destroy_workqueue(s
->rcv_wq
);
572 destroy_workqueue(s
->send_wq
);
575 static int tipc_work_start(struct tipc_server
*s
)
577 s
->rcv_wq
= alloc_ordered_workqueue("tipc_rcv", 0);
579 pr_err("can't start tipc receive workqueue\n");
583 s
->send_wq
= alloc_ordered_workqueue("tipc_send", 0);
585 pr_err("can't start tipc send workqueue\n");
586 destroy_workqueue(s
->rcv_wq
);
593 int tipc_server_start(struct tipc_server
*s
)
597 spin_lock_init(&s
->idr_lock
);
598 idr_init(&s
->conn_idr
);
601 s
->rcvbuf_cache
= kmem_cache_create(s
->name
, s
->max_rcvbuf_size
,
602 0, SLAB_HWCACHE_ALIGN
, NULL
);
603 if (!s
->rcvbuf_cache
)
606 ret
= tipc_work_start(s
);
608 kmem_cache_destroy(s
->rcvbuf_cache
);
611 ret
= tipc_open_listening_sock(s
);
614 kmem_cache_destroy(s
->rcvbuf_cache
);
620 void tipc_server_stop(struct tipc_server
*s
)
622 struct tipc_conn
*con
;
625 spin_lock_bh(&s
->idr_lock
);
626 for (id
= 0; s
->idr_in_use
; id
++) {
627 con
= idr_find(&s
->conn_idr
, id
);
629 spin_unlock_bh(&s
->idr_lock
);
630 tipc_close_conn(con
);
631 spin_lock_bh(&s
->idr_lock
);
634 spin_unlock_bh(&s
->idr_lock
);
637 kmem_cache_destroy(s
->rcvbuf_cache
);
638 idr_destroy(&s
->conn_idr
);