af_rds.c 15 KB

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