server.c 14 KB

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