server.c 15 KB

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