Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
at v4.1-rc1 637 lines 16 kB view raw
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" 38#include "socket.h" 39#include <net/sock.h> 40#include <linux/module.h> 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 46#define CF_SERVER 2 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 61 * @outqueue_lock: control access to the outqueue 62 * @outqueue: list of connection objects for its server 63 * @swork: send work item 64 */ 65struct 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 */ 80struct outqueue_entry { 81 struct list_head list; 82 struct kvec iov; 83 struct sockaddr_tipc dest; 84}; 85 86static void tipc_recv_work(struct work_struct *work); 87static void tipc_send_work(struct work_struct *work); 88static void tipc_clean_outqueues(struct tipc_conn *con); 89 90static void tipc_conn_kref_release(struct kref *kref) 91{ 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; 95 struct sock *sk; 96 97 if (sock) { 98 sk = sock->sk; 99 if (test_bit(CF_SERVER, &con->flags)) { 100 __module_get(sock->ops->owner); 101 __module_get(sk->sk_prot_creator->owner); 102 } 103 saddr->scope = -TIPC_NODE_SCOPE; 104 kernel_bind(sock, (struct sockaddr *)saddr, sizeof(*saddr)); 105 sk_release_kernel(sk); 106 con->sock = NULL; 107 } 108 109 tipc_clean_outqueues(con); 110 kfree(con); 111} 112 113static void conn_put(struct tipc_conn *con) 114{ 115 kref_put(&con->kref, tipc_conn_kref_release); 116} 117 118static void conn_get(struct tipc_conn *con) 119{ 120 kref_get(&con->kref); 121} 122 123static struct tipc_conn *tipc_conn_lookup(struct tipc_server *s, int conid) 124{ 125 struct tipc_conn *con; 126 127 spin_lock_bh(&s->idr_lock); 128 con = idr_find(&s->conn_idr, conid); 129 if (con) 130 conn_get(con); 131 spin_unlock_bh(&s->idr_lock); 132 return con; 133} 134 135static void sock_data_ready(struct sock *sk) 136{ 137 struct tipc_conn *con; 138 139 read_lock(&sk->sk_callback_lock); 140 con = sock2con(sk); 141 if (con && test_bit(CF_CONNECTED, &con->flags)) { 142 conn_get(con); 143 if (!queue_work(con->server->rcv_wq, &con->rwork)) 144 conn_put(con); 145 } 146 read_unlock(&sk->sk_callback_lock); 147} 148 149static void sock_write_space(struct sock *sk) 150{ 151 struct tipc_conn *con; 152 153 read_lock(&sk->sk_callback_lock); 154 con = sock2con(sk); 155 if (con && test_bit(CF_CONNECTED, &con->flags)) { 156 conn_get(con); 157 if (!queue_work(con->server->send_wq, &con->swork)) 158 conn_put(con); 159 } 160 read_unlock(&sk->sk_callback_lock); 161} 162 163static void tipc_register_callbacks(struct socket *sock, struct tipc_conn *con) 164{ 165 struct sock *sk = sock->sk; 166 167 write_lock_bh(&sk->sk_callback_lock); 168 169 sk->sk_data_ready = sock_data_ready; 170 sk->sk_write_space = sock_write_space; 171 sk->sk_user_data = con; 172 173 con->sock = sock; 174 175 write_unlock_bh(&sk->sk_callback_lock); 176} 177 178static void tipc_unregister_callbacks(struct tipc_conn *con) 179{ 180 struct sock *sk = con->sock->sk; 181 182 write_lock_bh(&sk->sk_callback_lock); 183 sk->sk_user_data = NULL; 184 write_unlock_bh(&sk->sk_callback_lock); 185} 186 187static void tipc_close_conn(struct tipc_conn *con) 188{ 189 struct tipc_server *s = con->server; 190 191 if (test_and_clear_bit(CF_CONNECTED, &con->flags)) { 192 if (con->conid) 193 s->tipc_conn_shutdown(con->conid, con->usr_data); 194 195 spin_lock_bh(&s->idr_lock); 196 idr_remove(&s->conn_idr, con->conid); 197 s->idr_in_use--; 198 spin_unlock_bh(&s->idr_lock); 199 200 tipc_unregister_callbacks(con); 201 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. 207 */ 208 kernel_sock_shutdown(con->sock, SHUT_RDWR); 209 210 conn_put(con); 211 } 212} 213 214static struct tipc_conn *tipc_alloc_conn(struct tipc_server *s) 215{ 216 struct tipc_conn *con; 217 int ret; 218 219 con = kzalloc(sizeof(struct tipc_conn), GFP_ATOMIC); 220 if (!con) 221 return ERR_PTR(-ENOMEM); 222 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); 228 229 spin_lock_bh(&s->idr_lock); 230 ret = idr_alloc(&s->conn_idr, con, 0, 0, GFP_ATOMIC); 231 if (ret < 0) { 232 kfree(con); 233 spin_unlock_bh(&s->idr_lock); 234 return ERR_PTR(-ENOMEM); 235 } 236 con->conid = ret; 237 s->idr_in_use++; 238 spin_unlock_bh(&s->idr_lock); 239 240 set_bit(CF_CONNECTED, &con->flags); 241 con->server = s; 242 243 return con; 244} 245 246static int tipc_receive_from_sock(struct tipc_conn *con) 247{ 248 struct msghdr msg = {}; 249 struct tipc_server *s = con->server; 250 struct sockaddr_tipc addr; 251 struct kvec iov; 252 void *buf; 253 int ret; 254 255 buf = kmem_cache_alloc(s->rcvbuf_cache, GFP_ATOMIC); 256 if (!buf) { 257 ret = -ENOMEM; 258 goto out_close; 259 } 260 261 iov.iov_base = buf; 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, 265 MSG_DONTWAIT); 266 if (ret <= 0) { 267 kmem_cache_free(s->rcvbuf_cache, buf); 268 goto out_close; 269 } 270 271 s->tipc_conn_recvmsg(sock_net(con->sock->sk), con->conid, &addr, 272 con->usr_data, buf, ret); 273 274 kmem_cache_free(s->rcvbuf_cache, buf); 275 276 return 0; 277 278out_close: 279 if (ret != -EWOULDBLOCK) 280 tipc_close_conn(con); 281 else if (ret == 0) 282 /* Don't return success if we really got EOF */ 283 ret = -EAGAIN; 284 285 return ret; 286} 287 288static int tipc_accept_from_sock(struct tipc_conn *con) 289{ 290 struct tipc_server *s = con->server; 291 struct socket *sock = con->sock; 292 struct socket *newsock; 293 struct tipc_conn *newcon; 294 int ret; 295 296 ret = kernel_accept(sock, &newsock, O_NONBLOCK); 297 if (ret < 0) 298 return ret; 299 300 newcon = tipc_alloc_conn(con->server); 301 if (IS_ERR(newcon)) { 302 ret = PTR_ERR(newcon); 303 sock_release(newsock); 304 return ret; 305 } 306 307 newcon->rx_action = tipc_receive_from_sock; 308 tipc_register_callbacks(newsock, newcon); 309 310 /* Notify that new connection is incoming */ 311 newcon->usr_data = s->tipc_conn_new(newcon->conid); 312 313 /* Wake up receive process in case of 'SYN+' message */ 314 newsock->sk->sk_data_ready(newsock->sk); 315 return ret; 316} 317 318static struct socket *tipc_create_listen_sock(struct tipc_conn *con) 319{ 320 struct tipc_server *s = con->server; 321 struct socket *sock = NULL; 322 int ret; 323 324 ret = sock_create_kern(AF_TIPC, SOCK_SEQPACKET, 0, &sock); 325 if (ret < 0) 326 return NULL; 327 328 sk_change_net(sock->sk, s->net); 329 330 ret = kernel_setsockopt(sock, SOL_TIPC, TIPC_IMPORTANCE, 331 (char *)&s->imp, sizeof(s->imp)); 332 if (ret < 0) 333 goto create_err; 334 ret = kernel_bind(sock, (struct sockaddr *)s->saddr, sizeof(*s->saddr)); 335 if (ret < 0) 336 goto create_err; 337 338 switch (s->type) { 339 case SOCK_STREAM: 340 case SOCK_SEQPACKET: 341 con->rx_action = tipc_accept_from_sock; 342 343 ret = kernel_listen(sock, 0); 344 if (ret < 0) 345 goto create_err; 346 break; 347 case SOCK_DGRAM: 348 case SOCK_RDM: 349 con->rx_action = tipc_receive_from_sock; 350 break; 351 default: 352 pr_err("Unknown socket type %d\n", s->type); 353 goto create_err; 354 } 355 356 /* As server's listening socket owner and creator is the same module, 357 * we have to decrease TIPC module reference count to guarantee that 358 * it remains zero after the server socket is created, otherwise, 359 * executing "rmmod" command is unable to make TIPC module deleted 360 * after TIPC module is inserted successfully. 361 * 362 * However, the reference count is ever increased twice in 363 * sock_create_kern(): one is to increase the reference count of owner 364 * of TIPC socket's proto_ops struct; another is to increment the 365 * reference count of owner of TIPC proto struct. Therefore, we must 366 * decrement the module reference count twice to ensure that it keeps 367 * zero after server's listening socket is created. Of course, we 368 * must bump the module reference count twice as well before the socket 369 * is closed. 370 */ 371 module_put(sock->ops->owner); 372 module_put(sock->sk->sk_prot_creator->owner); 373 set_bit(CF_SERVER, &con->flags); 374 375 return sock; 376 377create_err: 378 kernel_sock_shutdown(sock, SHUT_RDWR); 379 sk_release_kernel(sock->sk); 380 return NULL; 381} 382 383static int tipc_open_listening_sock(struct tipc_server *s) 384{ 385 struct socket *sock; 386 struct tipc_conn *con; 387 388 con = tipc_alloc_conn(s); 389 if (IS_ERR(con)) 390 return PTR_ERR(con); 391 392 sock = tipc_create_listen_sock(con); 393 if (!sock) { 394 idr_remove(&s->conn_idr, con->conid); 395 s->idr_in_use--; 396 kfree(con); 397 return -EINVAL; 398 } 399 400 tipc_register_callbacks(sock, con); 401 return 0; 402} 403 404static struct outqueue_entry *tipc_alloc_entry(void *data, int len) 405{ 406 struct outqueue_entry *entry; 407 void *buf; 408 409 entry = kmalloc(sizeof(struct outqueue_entry), GFP_ATOMIC); 410 if (!entry) 411 return NULL; 412 413 buf = kmalloc(len, GFP_ATOMIC); 414 if (!buf) { 415 kfree(entry); 416 return NULL; 417 } 418 419 memcpy(buf, data, len); 420 entry->iov.iov_base = buf; 421 entry->iov.iov_len = len; 422 423 return entry; 424} 425 426static void tipc_free_entry(struct outqueue_entry *e) 427{ 428 kfree(e->iov.iov_base); 429 kfree(e); 430} 431 432static void tipc_clean_outqueues(struct tipc_conn *con) 433{ 434 struct outqueue_entry *e, *safe; 435 436 spin_lock_bh(&con->outqueue_lock); 437 list_for_each_entry_safe(e, safe, &con->outqueue, list) { 438 list_del(&e->list); 439 tipc_free_entry(e); 440 } 441 spin_unlock_bh(&con->outqueue_lock); 442} 443 444int tipc_conn_sendmsg(struct tipc_server *s, int conid, 445 struct sockaddr_tipc *addr, void *data, size_t len) 446{ 447 struct outqueue_entry *e; 448 struct tipc_conn *con; 449 450 con = tipc_conn_lookup(s, conid); 451 if (!con) 452 return -EINVAL; 453 454 e = tipc_alloc_entry(data, len); 455 if (!e) { 456 conn_put(con); 457 return -ENOMEM; 458 } 459 460 if (addr) 461 memcpy(&e->dest, addr, sizeof(struct sockaddr_tipc)); 462 463 spin_lock_bh(&con->outqueue_lock); 464 list_add_tail(&e->list, &con->outqueue); 465 spin_unlock_bh(&con->outqueue_lock); 466 467 if (test_bit(CF_CONNECTED, &con->flags)) { 468 if (!queue_work(s->send_wq, &con->swork)) 469 conn_put(con); 470 } else { 471 conn_put(con); 472 } 473 return 0; 474} 475 476void tipc_conn_terminate(struct tipc_server *s, int conid) 477{ 478 struct tipc_conn *con; 479 480 con = tipc_conn_lookup(s, conid); 481 if (con) { 482 tipc_close_conn(con); 483 conn_put(con); 484 } 485} 486 487static void tipc_send_to_sock(struct tipc_conn *con) 488{ 489 int count = 0; 490 struct tipc_server *s = con->server; 491 struct outqueue_entry *e; 492 struct msghdr msg; 493 int ret; 494 495 spin_lock_bh(&con->outqueue_lock); 496 while (1) { 497 e = list_entry(con->outqueue.next, struct outqueue_entry, 498 list); 499 if ((struct list_head *) e == &con->outqueue) 500 break; 501 spin_unlock_bh(&con->outqueue_lock); 502 503 memset(&msg, 0, sizeof(msg)); 504 msg.msg_flags = MSG_DONTWAIT; 505 506 if (s->type == SOCK_DGRAM || s->type == SOCK_RDM) { 507 msg.msg_name = &e->dest; 508 msg.msg_namelen = sizeof(struct sockaddr_tipc); 509 } 510 ret = kernel_sendmsg(con->sock, &msg, &e->iov, 1, 511 e->iov.iov_len); 512 if (ret == -EWOULDBLOCK || ret == 0) { 513 cond_resched(); 514 goto out; 515 } else if (ret < 0) { 516 goto send_err; 517 } 518 519 /* Don't starve users filling buffers */ 520 if (++count >= MAX_SEND_MSG_COUNT) { 521 cond_resched(); 522 count = 0; 523 } 524 525 spin_lock_bh(&con->outqueue_lock); 526 list_del(&e->list); 527 tipc_free_entry(e); 528 } 529 spin_unlock_bh(&con->outqueue_lock); 530out: 531 return; 532 533send_err: 534 tipc_close_conn(con); 535} 536 537static void tipc_recv_work(struct work_struct *work) 538{ 539 struct tipc_conn *con = container_of(work, struct tipc_conn, rwork); 540 int count = 0; 541 542 while (test_bit(CF_CONNECTED, &con->flags)) { 543 if (con->rx_action(con)) 544 break; 545 546 /* Don't flood Rx machine */ 547 if (++count >= MAX_RECV_MSG_COUNT) { 548 cond_resched(); 549 count = 0; 550 } 551 } 552 conn_put(con); 553} 554 555static void tipc_send_work(struct work_struct *work) 556{ 557 struct tipc_conn *con = container_of(work, struct tipc_conn, swork); 558 559 if (test_bit(CF_CONNECTED, &con->flags)) 560 tipc_send_to_sock(con); 561 562 conn_put(con); 563} 564 565static void tipc_work_stop(struct tipc_server *s) 566{ 567 destroy_workqueue(s->rcv_wq); 568 destroy_workqueue(s->send_wq); 569} 570 571static int tipc_work_start(struct tipc_server *s) 572{ 573 s->rcv_wq = alloc_workqueue("tipc_rcv", WQ_UNBOUND, 1); 574 if (!s->rcv_wq) { 575 pr_err("can't start tipc receive workqueue\n"); 576 return -ENOMEM; 577 } 578 579 s->send_wq = alloc_workqueue("tipc_send", WQ_UNBOUND, 1); 580 if (!s->send_wq) { 581 pr_err("can't start tipc send workqueue\n"); 582 destroy_workqueue(s->rcv_wq); 583 return -ENOMEM; 584 } 585 586 return 0; 587} 588 589int tipc_server_start(struct tipc_server *s) 590{ 591 int ret; 592 593 spin_lock_init(&s->idr_lock); 594 idr_init(&s->conn_idr); 595 s->idr_in_use = 0; 596 597 s->rcvbuf_cache = kmem_cache_create(s->name, s->max_rcvbuf_size, 598 0, SLAB_HWCACHE_ALIGN, NULL); 599 if (!s->rcvbuf_cache) 600 return -ENOMEM; 601 602 ret = tipc_work_start(s); 603 if (ret < 0) { 604 kmem_cache_destroy(s->rcvbuf_cache); 605 return ret; 606 } 607 ret = tipc_open_listening_sock(s); 608 if (ret < 0) { 609 tipc_work_stop(s); 610 kmem_cache_destroy(s->rcvbuf_cache); 611 return ret; 612 } 613 return ret; 614} 615 616void tipc_server_stop(struct tipc_server *s) 617{ 618 struct tipc_conn *con; 619 int total = 0; 620 int id; 621 622 spin_lock_bh(&s->idr_lock); 623 for (id = 0; total < s->idr_in_use; id++) { 624 con = idr_find(&s->conn_idr, id); 625 if (con) { 626 total++; 627 spin_unlock_bh(&s->idr_lock); 628 tipc_close_conn(con); 629 spin_lock_bh(&s->idr_lock); 630 } 631 } 632 spin_unlock_bh(&s->idr_lock); 633 634 tipc_work_stop(s); 635 kmem_cache_destroy(s->rcvbuf_cache); 636 idr_destroy(&s->conn_idr); 637}