af_rds.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. /*
  2. * Copyright (c) 2006 Oracle. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. *
  32. */
  33. #include <linux/module.h>
  34. #include <linux/errno.h>
  35. #include <linux/kernel.h>
  36. #include <linux/gfp.h>
  37. #include <linux/in.h>
  38. #include <linux/poll.h>
  39. #include <net/sock.h>
  40. #include "rds.h"
  41. char *rds_str_array(char **array, size_t elements, size_t index)
  42. {
  43. if ((index < elements) && array[index])
  44. return array[index];
  45. else
  46. return "unknown";
  47. }
  48. EXPORT_SYMBOL(rds_str_array);
  49. /* this is just used for stats gathering :/ */
  50. static DEFINE_SPINLOCK(rds_sock_lock);
  51. static unsigned long rds_sock_count;
  52. static LIST_HEAD(rds_sock_list);
  53. DECLARE_WAIT_QUEUE_HEAD(rds_poll_waitq);
  54. /*
  55. * This is called as the final descriptor referencing this socket is closed.
  56. * We have to unbind the socket so that another socket can be bound to the
  57. * address it was using.
  58. *
  59. * We have to be careful about racing with the incoming path. sock_orphan()
  60. * sets SOCK_DEAD and we use that as an indicator to the rx path that new
  61. * messages shouldn't be queued.
  62. */
  63. static int rds_release(struct socket *sock)
  64. {
  65. struct sock *sk = sock->sk;
  66. struct rds_sock *rs;
  67. if (!sk)
  68. goto out;
  69. rs = rds_sk_to_rs(sk);
  70. sock_orphan(sk);
  71. /* Note - rds_clear_recv_queue grabs rs_recv_lock, so
  72. * that ensures the recv path has completed messing
  73. * with the socket. */
  74. rds_clear_recv_queue(rs);
  75. rds_cong_remove_socket(rs);
  76. /*
  77. * the binding lookup hash uses rcu, we need to
  78. * make sure we sychronize_rcu before we free our
  79. * entry
  80. */
  81. rds_remove_bound(rs);
  82. synchronize_rcu();
  83. rds_send_drop_to(rs, NULL);
  84. rds_rdma_drop_keys(rs);
  85. rds_notify_queue_get(rs, NULL);
  86. spin_lock_bh(&rds_sock_lock);
  87. list_del_init(&rs->rs_item);
  88. rds_sock_count--;
  89. spin_unlock_bh(&rds_sock_lock);
  90. rds_trans_put(rs->rs_transport);
  91. sock->sk = NULL;
  92. sock_put(sk);
  93. out:
  94. return 0;
  95. }
  96. /*
  97. * Careful not to race with rds_release -> sock_orphan which clears sk_sleep.
  98. * _bh() isn't OK here, we're called from interrupt handlers. It's probably OK
  99. * to wake the waitqueue after sk_sleep is clear as we hold a sock ref, but
  100. * this seems more conservative.
  101. * NB - normally, one would use sk_callback_lock for this, but we can
  102. * get here from interrupts, whereas the network code grabs sk_callback_lock
  103. * with _lock_bh only - so relying on sk_callback_lock introduces livelocks.
  104. */
  105. void rds_wake_sk_sleep(struct rds_sock *rs)
  106. {
  107. unsigned long flags;
  108. read_lock_irqsave(&rs->rs_recv_lock, flags);
  109. __rds_wake_sk_sleep(rds_rs_to_sk(rs));
  110. read_unlock_irqrestore(&rs->rs_recv_lock, flags);
  111. }
  112. static int rds_getname(struct socket *sock, struct sockaddr *uaddr,
  113. int *uaddr_len, int peer)
  114. {
  115. struct sockaddr_in *sin = (struct sockaddr_in *)uaddr;
  116. struct rds_sock *rs = rds_sk_to_rs(sock->sk);
  117. memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
  118. /* racey, don't care */
  119. if (peer) {
  120. if (!rs->rs_conn_addr)
  121. return -ENOTCONN;
  122. sin->sin_port = rs->rs_conn_port;
  123. sin->sin_addr.s_addr = rs->rs_conn_addr;
  124. } else {
  125. sin->sin_port = rs->rs_bound_port;
  126. sin->sin_addr.s_addr = rs->rs_bound_addr;
  127. }
  128. sin->sin_family = AF_INET;
  129. *uaddr_len = sizeof(*sin);
  130. return 0;
  131. }
  132. /*
  133. * RDS' poll is without a doubt the least intuitive part of the interface,
  134. * as POLLIN and POLLOUT do not behave entirely as you would expect from
  135. * a network protocol.
  136. *
  137. * POLLIN is asserted if
  138. * - there is data on the receive queue.
  139. * - to signal that a previously congested destination may have become
  140. * uncongested
  141. * - A notification has been queued to the socket (this can be a congestion
  142. * update, or a RDMA completion).
  143. *
  144. * POLLOUT is asserted if there is room on the send queue. This does not mean
  145. * however, that the next sendmsg() call will succeed. If the application tries
  146. * to send to a congested destination, the system call may still fail (and
  147. * return ENOBUFS).
  148. */
  149. static unsigned int rds_poll(struct file *file, struct socket *sock,
  150. poll_table *wait)
  151. {
  152. struct sock *sk = sock->sk;
  153. struct rds_sock *rs = rds_sk_to_rs(sk);
  154. unsigned int mask = 0;
  155. unsigned long flags;
  156. poll_wait(file, sk_sleep(sk), wait);
  157. if (rs->rs_seen_congestion)
  158. poll_wait(file, &rds_poll_waitq, wait);
  159. read_lock_irqsave(&rs->rs_recv_lock, flags);
  160. if (!rs->rs_cong_monitor) {
  161. /* When a congestion map was updated, we signal POLLIN for
  162. * "historical" reasons. Applications can also poll for
  163. * WRBAND instead. */
  164. if (rds_cong_updated_since(&rs->rs_cong_track))
  165. mask |= (POLLIN | POLLRDNORM | POLLWRBAND);
  166. } else {
  167. spin_lock(&rs->rs_lock);
  168. if (rs->rs_cong_notify)
  169. mask |= (POLLIN | POLLRDNORM);
  170. spin_unlock(&rs->rs_lock);
  171. }
  172. if (!list_empty(&rs->rs_recv_queue) ||
  173. !list_empty(&rs->rs_notify_queue))
  174. mask |= (POLLIN | POLLRDNORM);
  175. if (rs->rs_snd_bytes < rds_sk_sndbuf(rs))
  176. mask |= (POLLOUT | POLLWRNORM);
  177. read_unlock_irqrestore(&rs->rs_recv_lock, flags);
  178. /* clear state any time we wake a seen-congested socket */
  179. if (mask)
  180. rs->rs_seen_congestion = 0;
  181. return mask;
  182. }
  183. static int rds_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  184. {
  185. return -ENOIOCTLCMD;
  186. }
  187. static int rds_cancel_sent_to(struct rds_sock *rs, char __user *optval,
  188. int len)
  189. {
  190. struct sockaddr_in sin;
  191. int ret = 0;
  192. /* racing with another thread binding seems ok here */
  193. if (rs->rs_bound_addr == 0) {
  194. ret = -ENOTCONN; /* XXX not a great errno */
  195. goto out;
  196. }
  197. if (len < sizeof(struct sockaddr_in)) {
  198. ret = -EINVAL;
  199. goto out;
  200. }
  201. if (copy_from_user(&sin, optval, sizeof(sin))) {
  202. ret = -EFAULT;
  203. goto out;
  204. }
  205. rds_send_drop_to(rs, &sin);
  206. out:
  207. return ret;
  208. }
  209. static int rds_set_bool_option(unsigned char *optvar, char __user *optval,
  210. int optlen)
  211. {
  212. int value;
  213. if (optlen < sizeof(int))
  214. return -EINVAL;
  215. if (get_user(value, (int __user *) optval))
  216. return -EFAULT;
  217. *optvar = !!value;
  218. return 0;
  219. }
  220. static int rds_cong_monitor(struct rds_sock *rs, char __user *optval,
  221. int optlen)
  222. {
  223. int ret;
  224. ret = rds_set_bool_option(&rs->rs_cong_monitor, optval, optlen);
  225. if (ret == 0) {
  226. if (rs->rs_cong_monitor) {
  227. rds_cong_add_socket(rs);
  228. } else {
  229. rds_cong_remove_socket(rs);
  230. rs->rs_cong_mask = 0;
  231. rs->rs_cong_notify = 0;
  232. }
  233. }
  234. return ret;
  235. }
  236. static int rds_setsockopt(struct socket *sock, int level, int optname,
  237. char __user *optval, unsigned int optlen)
  238. {
  239. struct rds_sock *rs = rds_sk_to_rs(sock->sk);
  240. int ret;
  241. if (level != SOL_RDS) {
  242. ret = -ENOPROTOOPT;
  243. goto out;
  244. }
  245. switch (optname) {
  246. case RDS_CANCEL_SENT_TO:
  247. ret = rds_cancel_sent_to(rs, optval, optlen);
  248. break;
  249. case RDS_GET_MR:
  250. ret = rds_get_mr(rs, optval, optlen);
  251. break;
  252. case RDS_GET_MR_FOR_DEST:
  253. ret = rds_get_mr_for_dest(rs, optval, optlen);
  254. break;
  255. case RDS_FREE_MR:
  256. ret = rds_free_mr(rs, optval, optlen);
  257. break;
  258. case RDS_RECVERR:
  259. ret = rds_set_bool_option(&rs->rs_recverr, optval, optlen);
  260. break;
  261. case RDS_CONG_MONITOR:
  262. ret = rds_cong_monitor(rs, optval, optlen);
  263. break;
  264. default:
  265. ret = -ENOPROTOOPT;
  266. }
  267. out:
  268. return ret;
  269. }
  270. static int rds_getsockopt(struct socket *sock, int level, int optname,
  271. char __user *optval, int __user *optlen)
  272. {
  273. struct rds_sock *rs = rds_sk_to_rs(sock->sk);
  274. int ret = -ENOPROTOOPT, len;
  275. if (level != SOL_RDS)
  276. goto out;
  277. if (get_user(len, optlen)) {
  278. ret = -EFAULT;
  279. goto out;
  280. }
  281. switch (optname) {
  282. case RDS_INFO_FIRST ... RDS_INFO_LAST:
  283. ret = rds_info_getsockopt(sock, optname, optval,
  284. optlen);
  285. break;
  286. case RDS_RECVERR:
  287. if (len < sizeof(int))
  288. ret = -EINVAL;
  289. else
  290. if (put_user(rs->rs_recverr, (int __user *) optval) ||
  291. put_user(sizeof(int), optlen))
  292. ret = -EFAULT;
  293. else
  294. ret = 0;
  295. break;
  296. default:
  297. break;
  298. }
  299. out:
  300. return ret;
  301. }
  302. static int rds_connect(struct socket *sock, struct sockaddr *uaddr,
  303. int addr_len, int flags)
  304. {
  305. struct sock *sk = sock->sk;
  306. struct sockaddr_in *sin = (struct sockaddr_in *)uaddr;
  307. struct rds_sock *rs = rds_sk_to_rs(sk);
  308. int ret = 0;
  309. lock_sock(sk);
  310. if (addr_len != sizeof(struct sockaddr_in)) {
  311. ret = -EINVAL;
  312. goto out;
  313. }
  314. if (sin->sin_family != AF_INET) {
  315. ret = -EAFNOSUPPORT;
  316. goto out;
  317. }
  318. if (sin->sin_addr.s_addr == htonl(INADDR_ANY)) {
  319. ret = -EDESTADDRREQ;
  320. goto out;
  321. }
  322. rs->rs_conn_addr = sin->sin_addr.s_addr;
  323. rs->rs_conn_port = sin->sin_port;
  324. out:
  325. release_sock(sk);
  326. return ret;
  327. }
  328. static struct proto rds_proto = {
  329. .name = "RDS",
  330. .owner = THIS_MODULE,
  331. .obj_size = sizeof(struct rds_sock),
  332. };
  333. static const struct proto_ops rds_proto_ops = {
  334. .family = AF_RDS,
  335. .owner = THIS_MODULE,
  336. .release = rds_release,
  337. .bind = rds_bind,
  338. .connect = rds_connect,
  339. .socketpair = sock_no_socketpair,
  340. .accept = sock_no_accept,
  341. .getname = rds_getname,
  342. .poll = rds_poll,
  343. .ioctl = rds_ioctl,
  344. .listen = sock_no_listen,
  345. .shutdown = sock_no_shutdown,
  346. .setsockopt = rds_setsockopt,
  347. .getsockopt = rds_getsockopt,
  348. .sendmsg = rds_sendmsg,
  349. .recvmsg = rds_recvmsg,
  350. .mmap = sock_no_mmap,
  351. .sendpage = sock_no_sendpage,
  352. };
  353. static int __rds_create(struct socket *sock, struct sock *sk, int protocol)
  354. {
  355. struct rds_sock *rs;
  356. sock_init_data(sock, sk);
  357. sock->ops = &rds_proto_ops;
  358. sk->sk_protocol = protocol;
  359. rs = rds_sk_to_rs(sk);
  360. spin_lock_init(&rs->rs_lock);
  361. rwlock_init(&rs->rs_recv_lock);
  362. INIT_LIST_HEAD(&rs->rs_send_queue);
  363. INIT_LIST_HEAD(&rs->rs_recv_queue);
  364. INIT_LIST_HEAD(&rs->rs_notify_queue);
  365. INIT_LIST_HEAD(&rs->rs_cong_list);
  366. spin_lock_init(&rs->rs_rdma_lock);
  367. rs->rs_rdma_keys = RB_ROOT;
  368. spin_lock_bh(&rds_sock_lock);
  369. list_add_tail(&rs->rs_item, &rds_sock_list);
  370. rds_sock_count++;
  371. spin_unlock_bh(&rds_sock_lock);
  372. return 0;
  373. }
  374. static int rds_create(struct net *net, struct socket *sock, int protocol,
  375. int kern)
  376. {
  377. struct sock *sk;
  378. if (sock->type != SOCK_SEQPACKET || protocol)
  379. return -ESOCKTNOSUPPORT;
  380. sk = sk_alloc(net, AF_RDS, GFP_ATOMIC, &rds_proto);
  381. if (!sk)
  382. return -ENOMEM;
  383. return __rds_create(sock, sk, protocol);
  384. }
  385. void rds_sock_addref(struct rds_sock *rs)
  386. {
  387. sock_hold(rds_rs_to_sk(rs));
  388. }
  389. void rds_sock_put(struct rds_sock *rs)
  390. {
  391. sock_put(rds_rs_to_sk(rs));
  392. }
  393. static const struct net_proto_family rds_family_ops = {
  394. .family = AF_RDS,
  395. .create = rds_create,
  396. .owner = THIS_MODULE,
  397. };
  398. static void rds_sock_inc_info(struct socket *sock, unsigned int len,
  399. struct rds_info_iterator *iter,
  400. struct rds_info_lengths *lens)
  401. {
  402. struct rds_sock *rs;
  403. struct rds_incoming *inc;
  404. unsigned int total = 0;
  405. len /= sizeof(struct rds_info_message);
  406. spin_lock_bh(&rds_sock_lock);
  407. list_for_each_entry(rs, &rds_sock_list, rs_item) {
  408. read_lock(&rs->rs_recv_lock);
  409. /* XXX too lazy to maintain counts.. */
  410. list_for_each_entry(inc, &rs->rs_recv_queue, i_item) {
  411. total++;
  412. if (total <= len)
  413. rds_inc_info_copy(inc, iter, inc->i_saddr,
  414. rs->rs_bound_addr, 1);
  415. }
  416. read_unlock(&rs->rs_recv_lock);
  417. }
  418. spin_unlock_bh(&rds_sock_lock);
  419. lens->nr = total;
  420. lens->each = sizeof(struct rds_info_message);
  421. }
  422. static void rds_sock_info(struct socket *sock, unsigned int len,
  423. struct rds_info_iterator *iter,
  424. struct rds_info_lengths *lens)
  425. {
  426. struct rds_info_socket sinfo;
  427. struct rds_sock *rs;
  428. len /= sizeof(struct rds_info_socket);
  429. spin_lock_bh(&rds_sock_lock);
  430. if (len < rds_sock_count)
  431. goto out;
  432. list_for_each_entry(rs, &rds_sock_list, rs_item) {
  433. sinfo.sndbuf = rds_sk_sndbuf(rs);
  434. sinfo.rcvbuf = rds_sk_rcvbuf(rs);
  435. sinfo.bound_addr = rs->rs_bound_addr;
  436. sinfo.connected_addr = rs->rs_conn_addr;
  437. sinfo.bound_port = rs->rs_bound_port;
  438. sinfo.connected_port = rs->rs_conn_port;
  439. sinfo.inum = sock_i_ino(rds_rs_to_sk(rs));
  440. rds_info_copy(iter, &sinfo, sizeof(sinfo));
  441. }
  442. out:
  443. lens->nr = rds_sock_count;
  444. lens->each = sizeof(struct rds_info_socket);
  445. spin_unlock_bh(&rds_sock_lock);
  446. }
  447. static void rds_exit(void)
  448. {
  449. sock_unregister(rds_family_ops.family);
  450. proto_unregister(&rds_proto);
  451. rds_conn_exit();
  452. rds_cong_exit();
  453. rds_sysctl_exit();
  454. rds_threads_exit();
  455. rds_stats_exit();
  456. rds_page_exit();
  457. rds_info_deregister_func(RDS_INFO_SOCKETS, rds_sock_info);
  458. rds_info_deregister_func(RDS_INFO_RECV_MESSAGES, rds_sock_inc_info);
  459. }
  460. module_exit(rds_exit);
  461. static int rds_init(void)
  462. {
  463. int ret;
  464. ret = rds_conn_init();
  465. if (ret)
  466. goto out;
  467. ret = rds_threads_init();
  468. if (ret)
  469. goto out_conn;
  470. ret = rds_sysctl_init();
  471. if (ret)
  472. goto out_threads;
  473. ret = rds_stats_init();
  474. if (ret)
  475. goto out_sysctl;
  476. ret = proto_register(&rds_proto, 1);
  477. if (ret)
  478. goto out_stats;
  479. ret = sock_register(&rds_family_ops);
  480. if (ret)
  481. goto out_proto;
  482. rds_info_register_func(RDS_INFO_SOCKETS, rds_sock_info);
  483. rds_info_register_func(RDS_INFO_RECV_MESSAGES, rds_sock_inc_info);
  484. goto out;
  485. out_proto:
  486. proto_unregister(&rds_proto);
  487. out_stats:
  488. rds_stats_exit();
  489. out_sysctl:
  490. rds_sysctl_exit();
  491. out_threads:
  492. rds_threads_exit();
  493. out_conn:
  494. rds_conn_exit();
  495. rds_cong_exit();
  496. rds_page_exit();
  497. out:
  498. return ret;
  499. }
  500. module_init(rds_init);
  501. #define DRV_VERSION "4.0"
  502. #define DRV_RELDATE "Feb 12, 2009"
  503. MODULE_AUTHOR("Oracle Corporation <rds-devel@oss.oracle.com>");
  504. MODULE_DESCRIPTION("RDS: Reliable Datagram Sockets"
  505. " v" DRV_VERSION " (" DRV_RELDATE ")");
  506. MODULE_VERSION(DRV_VERSION);
  507. MODULE_LICENSE("Dual BSD/GPL");
  508. MODULE_ALIAS_NETPROTO(PF_RDS);