tile: nohz: warn if nohz_full uses hypervisor shared cores
[linux-2.6/btrfs-unstable.git] / net / tipc / server.c
blobeadd4ed459051ddc776a82f4f2fcc9fe34ab89cd
1 /*
2 * net/tipc/server.c: TIPC server infrastructure
4 * Copyright (c) 2012-2013, Wind River Systems
5 * All rights reserved.
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.
36 #include "server.h"
37 #include "core.h"
38 #include "socket.h"
39 #include <net/sock.h>
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
46 #define sock2con(x) ((struct tipc_conn *)(x)->sk_user_data)
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
59 * @outqueue_lock: control access to the outqueue
60 * @outqueue: list of connection objects for its server
61 * @swork: send work item
63 struct 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;
77 /* An entry waiting to be sent */
78 struct outqueue_entry {
79 struct list_head list;
80 struct kvec iov;
81 struct sockaddr_tipc dest;
84 static void tipc_recv_work(struct work_struct *work);
85 static void tipc_send_work(struct work_struct *work);
86 static void tipc_clean_outqueues(struct tipc_conn *con);
88 static void tipc_conn_kref_release(struct kref *kref)
90 struct tipc_conn *con = container_of(kref, struct tipc_conn, kref);
92 if (con->sock) {
93 tipc_sock_release_local(con->sock);
94 con->sock = NULL;
97 tipc_clean_outqueues(con);
98 kfree(con);
101 static void conn_put(struct tipc_conn *con)
103 kref_put(&con->kref, tipc_conn_kref_release);
106 static void conn_get(struct tipc_conn *con)
108 kref_get(&con->kref);
111 static struct tipc_conn *tipc_conn_lookup(struct tipc_server *s, int conid)
113 struct tipc_conn *con;
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;
123 static void sock_data_ready(struct sock *sk)
125 struct tipc_conn *con;
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);
134 read_unlock(&sk->sk_callback_lock);
137 static void sock_write_space(struct sock *sk)
139 struct tipc_conn *con;
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);
148 read_unlock(&sk->sk_callback_lock);
151 static void tipc_register_callbacks(struct socket *sock, struct tipc_conn *con)
153 struct sock *sk = sock->sk;
155 write_lock_bh(&sk->sk_callback_lock);
157 sk->sk_data_ready = sock_data_ready;
158 sk->sk_write_space = sock_write_space;
159 sk->sk_user_data = con;
161 con->sock = sock;
163 write_unlock_bh(&sk->sk_callback_lock);
166 static void tipc_unregister_callbacks(struct tipc_conn *con)
168 struct sock *sk = con->sock->sk;
170 write_lock_bh(&sk->sk_callback_lock);
171 sk->sk_user_data = NULL;
172 write_unlock_bh(&sk->sk_callback_lock);
175 static void tipc_close_conn(struct tipc_conn *con)
177 struct tipc_server *s = con->server;
179 if (test_and_clear_bit(CF_CONNECTED, &con->flags)) {
180 if (con->conid)
181 s->tipc_conn_shutdown(con->conid, con->usr_data);
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);
188 tipc_unregister_callbacks(con);
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.
196 kernel_sock_shutdown(con->sock, SHUT_RDWR);
198 conn_put(con);
202 static struct tipc_conn *tipc_alloc_conn(struct tipc_server *s)
204 struct tipc_conn *con;
205 int ret;
207 con = kzalloc(sizeof(struct tipc_conn), GFP_ATOMIC);
208 if (!con)
209 return ERR_PTR(-ENOMEM);
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);
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);
224 con->conid = ret;
225 s->idr_in_use++;
226 spin_unlock_bh(&s->idr_lock);
228 set_bit(CF_CONNECTED, &con->flags);
229 con->server = s;
231 return con;
234 static int tipc_receive_from_sock(struct tipc_conn *con)
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;
243 buf = kmem_cache_alloc(s->rcvbuf_cache, GFP_ATOMIC);
244 if (!buf) {
245 ret = -ENOMEM;
246 goto out_close;
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;
259 s->tipc_conn_recvmsg(sock_net(con->sock->sk), con->conid, &addr,
260 con->usr_data, buf, ret);
262 kmem_cache_free(s->rcvbuf_cache, buf);
264 return 0;
266 out_close:
267 if (ret != -EWOULDBLOCK)
268 tipc_close_conn(con);
269 else if (ret == 0)
270 /* Don't return success if we really got EOF */
271 ret = -EAGAIN;
273 return ret;
276 static int tipc_accept_from_sock(struct tipc_conn *con)
278 struct tipc_server *s = con->server;
279 struct socket *sock = con->sock;
280 struct socket *newsock;
281 struct tipc_conn *newcon;
282 int ret;
284 ret = tipc_sock_accept_local(sock, &newsock, O_NONBLOCK);
285 if (ret < 0)
286 return ret;
288 newcon = tipc_alloc_conn(con->server);
289 if (IS_ERR(newcon)) {
290 ret = PTR_ERR(newcon);
291 sock_release(newsock);
292 return ret;
295 newcon->rx_action = tipc_receive_from_sock;
296 tipc_register_callbacks(newsock, newcon);
298 /* Notify that new connection is incoming */
299 newcon->usr_data = s->tipc_conn_new(newcon->conid);
301 /* Wake up receive process in case of 'SYN+' message */
302 newsock->sk->sk_data_ready(newsock->sk);
303 return ret;
306 static struct socket *tipc_create_listen_sock(struct tipc_conn *con)
308 struct tipc_server *s = con->server;
309 struct socket *sock = NULL;
310 int ret;
312 ret = tipc_sock_create_local(s->net, s->type, &sock);
313 if (ret < 0)
314 return NULL;
315 ret = kernel_setsockopt(sock, SOL_TIPC, TIPC_IMPORTANCE,
316 (char *)&s->imp, sizeof(s->imp));
317 if (ret < 0)
318 goto create_err;
319 ret = kernel_bind(sock, (struct sockaddr *)s->saddr, sizeof(*s->saddr));
320 if (ret < 0)
321 goto create_err;
323 switch (s->type) {
324 case SOCK_STREAM:
325 case SOCK_SEQPACKET:
326 con->rx_action = tipc_accept_from_sock;
328 ret = kernel_listen(sock, 0);
329 if (ret < 0)
330 goto create_err;
331 break;
332 case SOCK_DGRAM:
333 case SOCK_RDM:
334 con->rx_action = tipc_receive_from_sock;
335 break;
336 default:
337 pr_err("Unknown socket type %d\n", s->type);
338 goto create_err;
340 return sock;
342 create_err:
343 sock_release(sock);
344 con->sock = NULL;
345 return NULL;
348 static int tipc_open_listening_sock(struct tipc_server *s)
350 struct socket *sock;
351 struct tipc_conn *con;
353 con = tipc_alloc_conn(s);
354 if (IS_ERR(con))
355 return PTR_ERR(con);
357 sock = tipc_create_listen_sock(con);
358 if (!sock) {
359 idr_remove(&s->conn_idr, con->conid);
360 s->idr_in_use--;
361 kfree(con);
362 return -EINVAL;
365 tipc_register_callbacks(sock, con);
366 return 0;
369 static struct outqueue_entry *tipc_alloc_entry(void *data, int len)
371 struct outqueue_entry *entry;
372 void *buf;
374 entry = kmalloc(sizeof(struct outqueue_entry), GFP_ATOMIC);
375 if (!entry)
376 return NULL;
378 buf = kmalloc(len, GFP_ATOMIC);
379 if (!buf) {
380 kfree(entry);
381 return NULL;
384 memcpy(buf, data, len);
385 entry->iov.iov_base = buf;
386 entry->iov.iov_len = len;
388 return entry;
391 static void tipc_free_entry(struct outqueue_entry *e)
393 kfree(e->iov.iov_base);
394 kfree(e);
397 static void tipc_clean_outqueues(struct tipc_conn *con)
399 struct outqueue_entry *e, *safe;
401 spin_lock_bh(&con->outqueue_lock);
402 list_for_each_entry_safe(e, safe, &con->outqueue, list) {
403 list_del(&e->list);
404 tipc_free_entry(e);
406 spin_unlock_bh(&con->outqueue_lock);
409 int tipc_conn_sendmsg(struct tipc_server *s, int conid,
410 struct sockaddr_tipc *addr, void *data, size_t len)
412 struct outqueue_entry *e;
413 struct tipc_conn *con;
415 con = tipc_conn_lookup(s, conid);
416 if (!con)
417 return -EINVAL;
419 e = tipc_alloc_entry(data, len);
420 if (!e) {
421 conn_put(con);
422 return -ENOMEM;
425 if (addr)
426 memcpy(&e->dest, addr, sizeof(struct sockaddr_tipc));
428 spin_lock_bh(&con->outqueue_lock);
429 list_add_tail(&e->list, &con->outqueue);
430 spin_unlock_bh(&con->outqueue_lock);
432 if (test_bit(CF_CONNECTED, &con->flags)) {
433 if (!queue_work(s->send_wq, &con->swork))
434 conn_put(con);
435 } else {
436 conn_put(con);
438 return 0;
441 void tipc_conn_terminate(struct tipc_server *s, int conid)
443 struct tipc_conn *con;
445 con = tipc_conn_lookup(s, conid);
446 if (con) {
447 tipc_close_conn(con);
448 conn_put(con);
452 static void tipc_send_to_sock(struct tipc_conn *con)
454 int count = 0;
455 struct tipc_server *s = con->server;
456 struct outqueue_entry *e;
457 struct msghdr msg;
458 int ret;
460 spin_lock_bh(&con->outqueue_lock);
461 while (1) {
462 e = list_entry(con->outqueue.next, struct outqueue_entry,
463 list);
464 if ((struct list_head *) e == &con->outqueue)
465 break;
466 spin_unlock_bh(&con->outqueue_lock);
468 memset(&msg, 0, sizeof(msg));
469 msg.msg_flags = MSG_DONTWAIT;
471 if (s->type == SOCK_DGRAM || s->type == SOCK_RDM) {
472 msg.msg_name = &e->dest;
473 msg.msg_namelen = sizeof(struct sockaddr_tipc);
475 ret = kernel_sendmsg(con->sock, &msg, &e->iov, 1,
476 e->iov.iov_len);
477 if (ret == -EWOULDBLOCK || ret == 0) {
478 cond_resched();
479 goto out;
480 } else if (ret < 0) {
481 goto send_err;
484 /* Don't starve users filling buffers */
485 if (++count >= MAX_SEND_MSG_COUNT) {
486 cond_resched();
487 count = 0;
490 spin_lock_bh(&con->outqueue_lock);
491 list_del(&e->list);
492 tipc_free_entry(e);
494 spin_unlock_bh(&con->outqueue_lock);
495 out:
496 return;
498 send_err:
499 tipc_close_conn(con);
502 static void tipc_recv_work(struct work_struct *work)
504 struct tipc_conn *con = container_of(work, struct tipc_conn, rwork);
505 int count = 0;
507 while (test_bit(CF_CONNECTED, &con->flags)) {
508 if (con->rx_action(con))
509 break;
511 /* Don't flood Rx machine */
512 if (++count >= MAX_RECV_MSG_COUNT) {
513 cond_resched();
514 count = 0;
517 conn_put(con);
520 static void tipc_send_work(struct work_struct *work)
522 struct tipc_conn *con = container_of(work, struct tipc_conn, swork);
524 if (test_bit(CF_CONNECTED, &con->flags))
525 tipc_send_to_sock(con);
527 conn_put(con);
530 static void tipc_work_stop(struct tipc_server *s)
532 destroy_workqueue(s->rcv_wq);
533 destroy_workqueue(s->send_wq);
536 static int tipc_work_start(struct tipc_server *s)
538 s->rcv_wq = alloc_workqueue("tipc_rcv", WQ_UNBOUND, 1);
539 if (!s->rcv_wq) {
540 pr_err("can't start tipc receive workqueue\n");
541 return -ENOMEM;
544 s->send_wq = alloc_workqueue("tipc_send", WQ_UNBOUND, 1);
545 if (!s->send_wq) {
546 pr_err("can't start tipc send workqueue\n");
547 destroy_workqueue(s->rcv_wq);
548 return -ENOMEM;
551 return 0;
554 int tipc_server_start(struct tipc_server *s)
556 int ret;
558 spin_lock_init(&s->idr_lock);
559 idr_init(&s->conn_idr);
560 s->idr_in_use = 0;
562 s->rcvbuf_cache = kmem_cache_create(s->name, s->max_rcvbuf_size,
563 0, SLAB_HWCACHE_ALIGN, NULL);
564 if (!s->rcvbuf_cache)
565 return -ENOMEM;
567 ret = tipc_work_start(s);
568 if (ret < 0) {
569 kmem_cache_destroy(s->rcvbuf_cache);
570 return ret;
572 ret = tipc_open_listening_sock(s);
573 if (ret < 0) {
574 tipc_work_stop(s);
575 kmem_cache_destroy(s->rcvbuf_cache);
576 return ret;
578 return ret;
581 void tipc_server_stop(struct tipc_server *s)
583 struct tipc_conn *con;
584 int total = 0;
585 int id;
587 spin_lock_bh(&s->idr_lock);
588 for (id = 0; total < s->idr_in_use; id++) {
589 con = idr_find(&s->conn_idr, id);
590 if (con) {
591 total++;
592 spin_unlock_bh(&s->idr_lock);
593 tipc_close_conn(con);
594 spin_lock_bh(&s->idr_lock);
597 spin_unlock_bh(&s->idr_lock);
599 tipc_work_stop(s);
600 kmem_cache_destroy(s->rcvbuf_cache);
601 idr_destroy(&s->conn_idr);