server.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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. idr_remove(&s->conn_idr, con->conid);
  301. s->idr_in_use--;
  302. kfree(con);
  303. return -EINVAL;
  304. }
  305. tipc_register_callbacks(sock, con);
  306. return 0;
  307. }
  308. static struct outqueue_entry *tipc_alloc_entry(void *data, int len)
  309. {
  310. struct outqueue_entry *entry;
  311. void *buf;
  312. entry = kmalloc(sizeof(struct outqueue_entry), GFP_ATOMIC);
  313. if (!entry)
  314. return NULL;
  315. buf = kmalloc(len, GFP_ATOMIC);
  316. if (!buf) {
  317. kfree(entry);
  318. return NULL;
  319. }
  320. memcpy(buf, data, len);
  321. entry->iov.iov_base = buf;
  322. entry->iov.iov_len = len;
  323. return entry;
  324. }
  325. static void tipc_free_entry(struct outqueue_entry *e)
  326. {
  327. kfree(e->iov.iov_base);
  328. kfree(e);
  329. }
  330. static void tipc_clean_outqueues(struct tipc_conn *con)
  331. {
  332. struct outqueue_entry *e, *safe;
  333. spin_lock_bh(&con->outqueue_lock);
  334. list_for_each_entry_safe(e, safe, &con->outqueue, list) {
  335. list_del(&e->list);
  336. tipc_free_entry(e);
  337. }
  338. spin_unlock_bh(&con->outqueue_lock);
  339. }
  340. int tipc_conn_sendmsg(struct tipc_server *s, int conid,
  341. struct sockaddr_tipc *addr, void *data, size_t len)
  342. {
  343. struct outqueue_entry *e;
  344. struct tipc_conn *con;
  345. con = tipc_conn_lookup(s, conid);
  346. if (!con)
  347. return -EINVAL;
  348. e = tipc_alloc_entry(data, len);
  349. if (!e) {
  350. conn_put(con);
  351. return -ENOMEM;
  352. }
  353. if (addr)
  354. memcpy(&e->dest, addr, sizeof(struct sockaddr_tipc));
  355. spin_lock_bh(&con->outqueue_lock);
  356. list_add_tail(&e->list, &con->outqueue);
  357. spin_unlock_bh(&con->outqueue_lock);
  358. if (test_bit(CF_CONNECTED, &con->flags))
  359. if (!queue_work(s->send_wq, &con->swork))
  360. conn_put(con);
  361. return 0;
  362. }
  363. void tipc_conn_terminate(struct tipc_server *s, int conid)
  364. {
  365. struct tipc_conn *con;
  366. con = tipc_conn_lookup(s, conid);
  367. if (con) {
  368. tipc_close_conn(con);
  369. conn_put(con);
  370. }
  371. }
  372. static void tipc_send_to_sock(struct tipc_conn *con)
  373. {
  374. int count = 0;
  375. struct tipc_server *s = con->server;
  376. struct outqueue_entry *e;
  377. struct msghdr msg;
  378. int ret;
  379. spin_lock_bh(&con->outqueue_lock);
  380. while (1) {
  381. e = list_entry(con->outqueue.next, struct outqueue_entry,
  382. list);
  383. if ((struct list_head *) e == &con->outqueue)
  384. break;
  385. spin_unlock_bh(&con->outqueue_lock);
  386. memset(&msg, 0, sizeof(msg));
  387. msg.msg_flags = MSG_DONTWAIT;
  388. if (s->type == SOCK_DGRAM || s->type == SOCK_RDM) {
  389. msg.msg_name = &e->dest;
  390. msg.msg_namelen = sizeof(struct sockaddr_tipc);
  391. }
  392. ret = kernel_sendmsg(con->sock, &msg, &e->iov, 1,
  393. e->iov.iov_len);
  394. if (ret == -EWOULDBLOCK || ret == 0) {
  395. cond_resched();
  396. goto out;
  397. } else if (ret < 0) {
  398. goto send_err;
  399. }
  400. /* Don't starve users filling buffers */
  401. if (++count >= MAX_SEND_MSG_COUNT) {
  402. cond_resched();
  403. count = 0;
  404. }
  405. spin_lock_bh(&con->outqueue_lock);
  406. list_del(&e->list);
  407. tipc_free_entry(e);
  408. }
  409. spin_unlock_bh(&con->outqueue_lock);
  410. out:
  411. return;
  412. send_err:
  413. tipc_close_conn(con);
  414. }
  415. static void tipc_recv_work(struct work_struct *work)
  416. {
  417. struct tipc_conn *con = container_of(work, struct tipc_conn, rwork);
  418. int count = 0;
  419. while (test_bit(CF_CONNECTED, &con->flags)) {
  420. if (con->rx_action(con))
  421. break;
  422. /* Don't flood Rx machine */
  423. if (++count >= MAX_RECV_MSG_COUNT) {
  424. cond_resched();
  425. count = 0;
  426. }
  427. }
  428. conn_put(con);
  429. }
  430. static void tipc_send_work(struct work_struct *work)
  431. {
  432. struct tipc_conn *con = container_of(work, struct tipc_conn, swork);
  433. if (test_bit(CF_CONNECTED, &con->flags))
  434. tipc_send_to_sock(con);
  435. conn_put(con);
  436. }
  437. static void tipc_work_stop(struct tipc_server *s)
  438. {
  439. destroy_workqueue(s->rcv_wq);
  440. destroy_workqueue(s->send_wq);
  441. }
  442. static int tipc_work_start(struct tipc_server *s)
  443. {
  444. s->rcv_wq = alloc_workqueue("tipc_rcv", WQ_UNBOUND, 1);
  445. if (!s->rcv_wq) {
  446. pr_err("can't start tipc receive workqueue\n");
  447. return -ENOMEM;
  448. }
  449. s->send_wq = alloc_workqueue("tipc_send", WQ_UNBOUND, 1);
  450. if (!s->send_wq) {
  451. pr_err("can't start tipc send workqueue\n");
  452. destroy_workqueue(s->rcv_wq);
  453. return -ENOMEM;
  454. }
  455. return 0;
  456. }
  457. int tipc_server_start(struct tipc_server *s)
  458. {
  459. int ret;
  460. spin_lock_init(&s->idr_lock);
  461. idr_init(&s->conn_idr);
  462. s->idr_in_use = 0;
  463. s->rcvbuf_cache = kmem_cache_create(s->name, s->max_rcvbuf_size,
  464. 0, SLAB_HWCACHE_ALIGN, NULL);
  465. if (!s->rcvbuf_cache)
  466. return -ENOMEM;
  467. ret = tipc_work_start(s);
  468. if (ret < 0) {
  469. kmem_cache_destroy(s->rcvbuf_cache);
  470. return ret;
  471. }
  472. ret = tipc_open_listening_sock(s);
  473. if (ret < 0) {
  474. tipc_work_stop(s);
  475. kmem_cache_destroy(s->rcvbuf_cache);
  476. return ret;
  477. }
  478. s->enabled = 1;
  479. return ret;
  480. }
  481. void tipc_server_stop(struct tipc_server *s)
  482. {
  483. struct tipc_conn *con;
  484. int total = 0;
  485. int id;
  486. if (!s->enabled)
  487. return;
  488. s->enabled = 0;
  489. spin_lock_bh(&s->idr_lock);
  490. for (id = 0; total < s->idr_in_use; id++) {
  491. con = idr_find(&s->conn_idr, id);
  492. if (con) {
  493. total++;
  494. spin_unlock_bh(&s->idr_lock);
  495. tipc_close_conn(con);
  496. spin_lock_bh(&s->idr_lock);
  497. }
  498. }
  499. spin_unlock_bh(&s->idr_lock);
  500. tipc_work_stop(s);
  501. kmem_cache_destroy(s->rcvbuf_cache);
  502. idr_destroy(&s->conn_idr);
  503. }