server.c 14 KB

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