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 sockaddr_tipc *saddr = con->server->saddr;
94 struct socket *sock = con->sock;
99 if (test_bit(CF_SERVER, &con->flags)) {
100 __module_get(sock->ops->owner);
101 __module_get(sk->sk_prot_creator->owner);
103 saddr->scope = -TIPC_NODE_SCOPE;
104 kernel_bind(sock, (struct sockaddr *)saddr, sizeof(*saddr));
109 tipc_clean_outqueues(con);
113 static void conn_put(struct tipc_conn *con)
115 kref_put(&con->kref, tipc_conn_kref_release);
118 static void conn_get(struct tipc_conn *con)
120 kref_get(&con->kref);
123 static struct tipc_conn *tipc_conn_lookup(struct tipc_server *s, int conid)
125 struct tipc_conn *con;
127 spin_lock_bh(&s->idr_lock);
128 con = idr_find(&s->conn_idr, conid);
131 spin_unlock_bh(&s->idr_lock);
135 static void sock_data_ready(struct sock *sk)
137 struct tipc_conn *con;
139 read_lock(&sk->sk_callback_lock);
141 if (con && test_bit(CF_CONNECTED, &con->flags)) {
143 if (!queue_work(con->server->rcv_wq, &con->rwork))
146 read_unlock(&sk->sk_callback_lock);
149 static void sock_write_space(struct sock *sk)
151 struct tipc_conn *con;
153 read_lock(&sk->sk_callback_lock);
155 if (con && test_bit(CF_CONNECTED, &con->flags)) {
157 if (!queue_work(con->server->send_wq, &con->swork))
160 read_unlock(&sk->sk_callback_lock);
163 static void tipc_register_callbacks(struct socket *sock, struct tipc_conn *con)
165 struct sock *sk = sock->sk;
167 write_lock_bh(&sk->sk_callback_lock);
169 sk->sk_data_ready = sock_data_ready;
170 sk->sk_write_space = sock_write_space;
171 sk->sk_user_data = con;
175 write_unlock_bh(&sk->sk_callback_lock);
178 static void tipc_unregister_callbacks(struct tipc_conn *con)
180 struct sock *sk = con->sock->sk;
182 write_lock_bh(&sk->sk_callback_lock);
183 sk->sk_user_data = NULL;
184 write_unlock_bh(&sk->sk_callback_lock);
187 static void tipc_close_conn(struct tipc_conn *con)
189 struct tipc_server *s = con->server;
191 if (test_and_clear_bit(CF_CONNECTED, &con->flags)) {
193 s->tipc_conn_shutdown(con->conid, con->usr_data);
195 spin_lock_bh(&s->idr_lock);
196 idr_remove(&s->conn_idr, con->conid);
198 spin_unlock_bh(&s->idr_lock);
200 tipc_unregister_callbacks(con);
202 /* We shouldn't flush pending works as we may be in the
203 * thread. In fact the races with pending rx/tx work structs
204 * are harmless for us here as we have already deleted this
205 * connection from server connection list and set
206 * sk->sk_user_data to 0 before releasing connection object.
208 kernel_sock_shutdown(con->sock, SHUT_RDWR);
214 static struct tipc_conn *tipc_alloc_conn(struct tipc_server *s)
216 struct tipc_conn *con;
219 con = kzalloc(sizeof(struct tipc_conn), GFP_ATOMIC);
221 return ERR_PTR(-ENOMEM);
223 kref_init(&con->kref);
224 INIT_LIST_HEAD(&con->outqueue);
225 spin_lock_init(&con->outqueue_lock);
226 INIT_WORK(&con->swork, tipc_send_work);
227 INIT_WORK(&con->rwork, tipc_recv_work);
229 spin_lock_bh(&s->idr_lock);
230 ret = idr_alloc(&s->conn_idr, con, 0, 0, GFP_ATOMIC);
233 spin_unlock_bh(&s->idr_lock);
234 return ERR_PTR(-ENOMEM);
238 spin_unlock_bh(&s->idr_lock);
240 set_bit(CF_CONNECTED, &con->flags);
246 static int tipc_receive_from_sock(struct tipc_conn *con)
248 struct msghdr msg = {};
249 struct tipc_server *s = con->server;
250 struct sockaddr_tipc addr;
255 buf = kmem_cache_alloc(s->rcvbuf_cache, GFP_ATOMIC);
262 iov.iov_len = s->max_rcvbuf_size;
263 msg.msg_name = &addr;
264 ret = kernel_recvmsg(con->sock, &msg, &iov, 1, iov.iov_len,
267 kmem_cache_free(s->rcvbuf_cache, buf);
271 s->tipc_conn_recvmsg(sock_net(con->sock->sk), con->conid, &addr,
272 con->usr_data, buf, ret);
274 kmem_cache_free(s->rcvbuf_cache, buf);
279 if (ret != -EWOULDBLOCK)
280 tipc_close_conn(con);
282 /* Don't return success if we really got EOF */
288 static int tipc_accept_from_sock(struct tipc_conn *con)
290 struct tipc_server *s = con->server;
291 struct socket *sock = con->sock;
292 struct socket *newsock;
293 struct tipc_conn *newcon;
296 ret = kernel_accept(sock, &newsock, O_NONBLOCK);
300 newcon = tipc_alloc_conn(con->server);
301 if (IS_ERR(newcon)) {
302 ret = PTR_ERR(newcon);
303 sock_release(newsock);
307 newcon->rx_action = tipc_receive_from_sock;
308 tipc_register_callbacks(newsock, newcon);
310 /* Notify that new connection is incoming */
311 newcon->usr_data = s->tipc_conn_new(newcon->conid);
313 /* Wake up receive process in case of 'SYN+' message */
314 newsock->sk->sk_data_ready(newsock->sk);
318 static struct socket *tipc_create_listen_sock(struct tipc_conn *con)
320 struct tipc_server *s = con->server;
321 struct socket *sock = NULL;
324 ret = __sock_create(s->net, AF_TIPC, SOCK_SEQPACKET, 0, &sock, 1);
327 ret = kernel_setsockopt(sock, SOL_TIPC, TIPC_IMPORTANCE,
328 (char *)&s->imp, sizeof(s->imp));
331 ret = kernel_bind(sock, (struct sockaddr *)s->saddr, sizeof(*s->saddr));
338 con->rx_action = tipc_accept_from_sock;
340 ret = kernel_listen(sock, 0);
346 con->rx_action = tipc_receive_from_sock;
349 pr_err("Unknown socket type %d\n", s->type);
353 /* As server's listening socket owner and creator is the same module,
354 * we have to decrease TIPC module reference count to guarantee that
355 * it remains zero after the server socket is created, otherwise,
356 * executing "rmmod" command is unable to make TIPC module deleted
357 * after TIPC module is inserted successfully.
359 * However, the reference count is ever increased twice in
360 * sock_create_kern(): one is to increase the reference count of owner
361 * of TIPC socket's proto_ops struct; another is to increment the
362 * reference count of owner of TIPC proto struct. Therefore, we must
363 * decrement the module reference count twice to ensure that it keeps
364 * zero after server's listening socket is created. Of course, we
365 * must bump the module reference count twice as well before the socket
368 module_put(sock->ops->owner);
369 module_put(sock->sk->sk_prot_creator->owner);
370 set_bit(CF_SERVER, &con->flags);
375 kernel_sock_shutdown(sock, SHUT_RDWR);
380 static int tipc_open_listening_sock(struct tipc_server *s)
383 struct tipc_conn *con;
385 con = tipc_alloc_conn(s);
389 sock = tipc_create_listen_sock(con);
391 idr_remove(&s->conn_idr, con->conid);
397 tipc_register_callbacks(sock, con);
401 static struct outqueue_entry *tipc_alloc_entry(void *data, int len)
403 struct outqueue_entry *entry;
406 entry = kmalloc(sizeof(struct outqueue_entry), GFP_ATOMIC);
410 buf = kmalloc(len, GFP_ATOMIC);
416 memcpy(buf, data, len);
417 entry->iov.iov_base = buf;
418 entry->iov.iov_len = len;
423 static void tipc_free_entry(struct outqueue_entry *e)
425 kfree(e->iov.iov_base);
429 static void tipc_clean_outqueues(struct tipc_conn *con)
431 struct outqueue_entry *e, *safe;
433 spin_lock_bh(&con->outqueue_lock);
434 list_for_each_entry_safe(e, safe, &con->outqueue, list) {
438 spin_unlock_bh(&con->outqueue_lock);
441 int tipc_conn_sendmsg(struct tipc_server *s, int conid,
442 struct sockaddr_tipc *addr, void *data, size_t len)
444 struct outqueue_entry *e;
445 struct tipc_conn *con;
447 con = tipc_conn_lookup(s, conid);
451 e = tipc_alloc_entry(data, len);
458 memcpy(&e->dest, addr, sizeof(struct sockaddr_tipc));
460 spin_lock_bh(&con->outqueue_lock);
461 list_add_tail(&e->list, &con->outqueue);
462 spin_unlock_bh(&con->outqueue_lock);
464 if (test_bit(CF_CONNECTED, &con->flags)) {
465 if (!queue_work(s->send_wq, &con->swork))
473 void tipc_conn_terminate(struct tipc_server *s, int conid)
475 struct tipc_conn *con;
477 con = tipc_conn_lookup(s, conid);
479 tipc_close_conn(con);
484 static void tipc_send_to_sock(struct tipc_conn *con)
487 struct tipc_server *s = con->server;
488 struct outqueue_entry *e;
492 spin_lock_bh(&con->outqueue_lock);
494 e = list_entry(con->outqueue.next, struct outqueue_entry,
496 if ((struct list_head *) e == &con->outqueue)
498 spin_unlock_bh(&con->outqueue_lock);
500 memset(&msg, 0, sizeof(msg));
501 msg.msg_flags = MSG_DONTWAIT;
503 if (s->type == SOCK_DGRAM || s->type == SOCK_RDM) {
504 msg.msg_name = &e->dest;
505 msg.msg_namelen = sizeof(struct sockaddr_tipc);
507 ret = kernel_sendmsg(con->sock, &msg, &e->iov, 1,
509 if (ret == -EWOULDBLOCK || ret == 0) {
512 } else if (ret < 0) {
516 /* Don't starve users filling buffers */
517 if (++count >= MAX_SEND_MSG_COUNT) {
522 spin_lock_bh(&con->outqueue_lock);
526 spin_unlock_bh(&con->outqueue_lock);
531 tipc_close_conn(con);
534 static void tipc_recv_work(struct work_struct *work)
536 struct tipc_conn *con = container_of(work, struct tipc_conn, rwork);
539 while (test_bit(CF_CONNECTED, &con->flags)) {
540 if (con->rx_action(con))
543 /* Don't flood Rx machine */
544 if (++count >= MAX_RECV_MSG_COUNT) {
552 static void tipc_send_work(struct work_struct *work)
554 struct tipc_conn *con = container_of(work, struct tipc_conn, swork);
556 if (test_bit(CF_CONNECTED, &con->flags))
557 tipc_send_to_sock(con);
562 static void tipc_work_stop(struct tipc_server *s)
564 destroy_workqueue(s->rcv_wq);
565 destroy_workqueue(s->send_wq);
568 static int tipc_work_start(struct tipc_server *s)
570 s->rcv_wq = alloc_workqueue("tipc_rcv", WQ_UNBOUND, 1);
572 pr_err("can't start tipc receive workqueue\n");
576 s->send_wq = alloc_workqueue("tipc_send", WQ_UNBOUND, 1);
578 pr_err("can't start tipc send workqueue\n");
579 destroy_workqueue(s->rcv_wq);
586 int tipc_server_start(struct tipc_server *s)
590 spin_lock_init(&s->idr_lock);
591 idr_init(&s->conn_idr);
594 s->rcvbuf_cache = kmem_cache_create(s->name, s->max_rcvbuf_size,
595 0, SLAB_HWCACHE_ALIGN, NULL);
596 if (!s->rcvbuf_cache)
599 ret = tipc_work_start(s);
601 kmem_cache_destroy(s->rcvbuf_cache);
604 ret = tipc_open_listening_sock(s);
607 kmem_cache_destroy(s->rcvbuf_cache);
613 void tipc_server_stop(struct tipc_server *s)
615 struct tipc_conn *con;
619 spin_lock_bh(&s->idr_lock);
620 for (id = 0; total < s->idr_in_use; id++) {
621 con = idr_find(&s->conn_idr, id);
624 spin_unlock_bh(&s->idr_lock);
625 tipc_close_conn(con);
626 spin_lock_bh(&s->idr_lock);
629 spin_unlock_bh(&s->idr_lock);
632 kmem_cache_destroy(s->rcvbuf_cache);
633 idr_destroy(&s->conn_idr);