server.c 15 KB

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