af_rds.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  1. /*
  2. * Copyright (c) 2006, 2018 Oracle and/or its affiliates. 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/ipv6.h>
  39. #include <linux/poll.h>
  40. #include <net/sock.h>
  41. #include "rds.h"
  42. /* this is just used for stats gathering :/ */
  43. static DEFINE_SPINLOCK(rds_sock_lock);
  44. static unsigned long rds_sock_count;
  45. static LIST_HEAD(rds_sock_list);
  46. DECLARE_WAIT_QUEUE_HEAD(rds_poll_waitq);
  47. /*
  48. * This is called as the final descriptor referencing this socket is closed.
  49. * We have to unbind the socket so that another socket can be bound to the
  50. * address it was using.
  51. *
  52. * We have to be careful about racing with the incoming path. sock_orphan()
  53. * sets SOCK_DEAD and we use that as an indicator to the rx path that new
  54. * messages shouldn't be queued.
  55. */
  56. static int rds_release(struct socket *sock)
  57. {
  58. struct sock *sk = sock->sk;
  59. struct rds_sock *rs;
  60. if (!sk)
  61. goto out;
  62. rs = rds_sk_to_rs(sk);
  63. sock_orphan(sk);
  64. /* Note - rds_clear_recv_queue grabs rs_recv_lock, so
  65. * that ensures the recv path has completed messing
  66. * with the socket. */
  67. rds_clear_recv_queue(rs);
  68. rds_cong_remove_socket(rs);
  69. rds_remove_bound(rs);
  70. rds_send_drop_to(rs, NULL);
  71. rds_rdma_drop_keys(rs);
  72. rds_notify_queue_get(rs, NULL);
  73. rds_notify_msg_zcopy_purge(&rs->rs_zcookie_queue);
  74. spin_lock_bh(&rds_sock_lock);
  75. list_del_init(&rs->rs_item);
  76. rds_sock_count--;
  77. spin_unlock_bh(&rds_sock_lock);
  78. rds_trans_put(rs->rs_transport);
  79. sock->sk = NULL;
  80. sock_put(sk);
  81. out:
  82. return 0;
  83. }
  84. /*
  85. * Careful not to race with rds_release -> sock_orphan which clears sk_sleep.
  86. * _bh() isn't OK here, we're called from interrupt handlers. It's probably OK
  87. * to wake the waitqueue after sk_sleep is clear as we hold a sock ref, but
  88. * this seems more conservative.
  89. * NB - normally, one would use sk_callback_lock for this, but we can
  90. * get here from interrupts, whereas the network code grabs sk_callback_lock
  91. * with _lock_bh only - so relying on sk_callback_lock introduces livelocks.
  92. */
  93. void rds_wake_sk_sleep(struct rds_sock *rs)
  94. {
  95. unsigned long flags;
  96. read_lock_irqsave(&rs->rs_recv_lock, flags);
  97. __rds_wake_sk_sleep(rds_rs_to_sk(rs));
  98. read_unlock_irqrestore(&rs->rs_recv_lock, flags);
  99. }
  100. static int rds_getname(struct socket *sock, struct sockaddr *uaddr,
  101. int peer)
  102. {
  103. struct rds_sock *rs = rds_sk_to_rs(sock->sk);
  104. struct sockaddr_in6 *sin6;
  105. struct sockaddr_in *sin;
  106. int uaddr_len;
  107. /* racey, don't care */
  108. if (peer) {
  109. if (ipv6_addr_any(&rs->rs_conn_addr))
  110. return -ENOTCONN;
  111. if (ipv6_addr_v4mapped(&rs->rs_conn_addr)) {
  112. sin = (struct sockaddr_in *)uaddr;
  113. memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
  114. sin->sin_family = AF_INET;
  115. sin->sin_port = rs->rs_conn_port;
  116. sin->sin_addr.s_addr = rs->rs_conn_addr_v4;
  117. uaddr_len = sizeof(*sin);
  118. } else {
  119. sin6 = (struct sockaddr_in6 *)uaddr;
  120. sin6->sin6_family = AF_INET6;
  121. sin6->sin6_port = rs->rs_conn_port;
  122. sin6->sin6_addr = rs->rs_conn_addr;
  123. sin6->sin6_flowinfo = 0;
  124. /* scope_id is the same as in the bound address. */
  125. sin6->sin6_scope_id = rs->rs_bound_scope_id;
  126. uaddr_len = sizeof(*sin6);
  127. }
  128. } else {
  129. /* If socket is not yet bound and the socket is connected,
  130. * set the return address family to be the same as the
  131. * connected address, but with 0 address value. If it is not
  132. * connected, set the family to be AF_UNSPEC (value 0) and
  133. * the address size to be that of an IPv4 address.
  134. */
  135. if (ipv6_addr_any(&rs->rs_bound_addr)) {
  136. if (ipv6_addr_any(&rs->rs_conn_addr)) {
  137. sin = (struct sockaddr_in *)uaddr;
  138. memset(sin, 0, sizeof(*sin));
  139. sin->sin_family = AF_UNSPEC;
  140. return sizeof(*sin);
  141. }
  142. #if IS_ENABLED(CONFIG_IPV6)
  143. if (!(ipv6_addr_type(&rs->rs_conn_addr) &
  144. IPV6_ADDR_MAPPED)) {
  145. sin6 = (struct sockaddr_in6 *)uaddr;
  146. memset(sin6, 0, sizeof(*sin6));
  147. sin6->sin6_family = AF_INET6;
  148. return sizeof(*sin6);
  149. }
  150. #endif
  151. sin = (struct sockaddr_in *)uaddr;
  152. memset(sin, 0, sizeof(*sin));
  153. sin->sin_family = AF_INET;
  154. return sizeof(*sin);
  155. }
  156. if (ipv6_addr_v4mapped(&rs->rs_bound_addr)) {
  157. sin = (struct sockaddr_in *)uaddr;
  158. memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
  159. sin->sin_family = AF_INET;
  160. sin->sin_port = rs->rs_bound_port;
  161. sin->sin_addr.s_addr = rs->rs_bound_addr_v4;
  162. uaddr_len = sizeof(*sin);
  163. } else {
  164. sin6 = (struct sockaddr_in6 *)uaddr;
  165. sin6->sin6_family = AF_INET6;
  166. sin6->sin6_port = rs->rs_bound_port;
  167. sin6->sin6_addr = rs->rs_bound_addr;
  168. sin6->sin6_flowinfo = 0;
  169. sin6->sin6_scope_id = rs->rs_bound_scope_id;
  170. uaddr_len = sizeof(*sin6);
  171. }
  172. }
  173. return uaddr_len;
  174. }
  175. /*
  176. * RDS' poll is without a doubt the least intuitive part of the interface,
  177. * as EPOLLIN and EPOLLOUT do not behave entirely as you would expect from
  178. * a network protocol.
  179. *
  180. * EPOLLIN is asserted if
  181. * - there is data on the receive queue.
  182. * - to signal that a previously congested destination may have become
  183. * uncongested
  184. * - A notification has been queued to the socket (this can be a congestion
  185. * update, or a RDMA completion, or a MSG_ZEROCOPY completion).
  186. *
  187. * EPOLLOUT is asserted if there is room on the send queue. This does not mean
  188. * however, that the next sendmsg() call will succeed. If the application tries
  189. * to send to a congested destination, the system call may still fail (and
  190. * return ENOBUFS).
  191. */
  192. static __poll_t rds_poll(struct file *file, struct socket *sock,
  193. poll_table *wait)
  194. {
  195. struct sock *sk = sock->sk;
  196. struct rds_sock *rs = rds_sk_to_rs(sk);
  197. __poll_t mask = 0;
  198. unsigned long flags;
  199. poll_wait(file, sk_sleep(sk), wait);
  200. if (rs->rs_seen_congestion)
  201. poll_wait(file, &rds_poll_waitq, wait);
  202. read_lock_irqsave(&rs->rs_recv_lock, flags);
  203. if (!rs->rs_cong_monitor) {
  204. /* When a congestion map was updated, we signal EPOLLIN for
  205. * "historical" reasons. Applications can also poll for
  206. * WRBAND instead. */
  207. if (rds_cong_updated_since(&rs->rs_cong_track))
  208. mask |= (EPOLLIN | EPOLLRDNORM | EPOLLWRBAND);
  209. } else {
  210. spin_lock(&rs->rs_lock);
  211. if (rs->rs_cong_notify)
  212. mask |= (EPOLLIN | EPOLLRDNORM);
  213. spin_unlock(&rs->rs_lock);
  214. }
  215. if (!list_empty(&rs->rs_recv_queue) ||
  216. !list_empty(&rs->rs_notify_queue) ||
  217. !list_empty(&rs->rs_zcookie_queue.zcookie_head))
  218. mask |= (EPOLLIN | EPOLLRDNORM);
  219. if (rs->rs_snd_bytes < rds_sk_sndbuf(rs))
  220. mask |= (EPOLLOUT | EPOLLWRNORM);
  221. if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
  222. mask |= POLLERR;
  223. read_unlock_irqrestore(&rs->rs_recv_lock, flags);
  224. /* clear state any time we wake a seen-congested socket */
  225. if (mask)
  226. rs->rs_seen_congestion = 0;
  227. return mask;
  228. }
  229. static int rds_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  230. {
  231. return -ENOIOCTLCMD;
  232. }
  233. static int rds_cancel_sent_to(struct rds_sock *rs, char __user *optval,
  234. int len)
  235. {
  236. struct sockaddr_in6 sin6;
  237. struct sockaddr_in sin;
  238. int ret = 0;
  239. /* racing with another thread binding seems ok here */
  240. if (ipv6_addr_any(&rs->rs_bound_addr)) {
  241. ret = -ENOTCONN; /* XXX not a great errno */
  242. goto out;
  243. }
  244. if (len < sizeof(struct sockaddr_in)) {
  245. ret = -EINVAL;
  246. goto out;
  247. } else if (len < sizeof(struct sockaddr_in6)) {
  248. /* Assume IPv4 */
  249. if (copy_from_user(&sin, optval, sizeof(struct sockaddr_in))) {
  250. ret = -EFAULT;
  251. goto out;
  252. }
  253. ipv6_addr_set_v4mapped(sin.sin_addr.s_addr, &sin6.sin6_addr);
  254. sin6.sin6_port = sin.sin_port;
  255. } else {
  256. if (copy_from_user(&sin6, optval,
  257. sizeof(struct sockaddr_in6))) {
  258. ret = -EFAULT;
  259. goto out;
  260. }
  261. }
  262. rds_send_drop_to(rs, &sin6);
  263. out:
  264. return ret;
  265. }
  266. static int rds_set_bool_option(unsigned char *optvar, char __user *optval,
  267. int optlen)
  268. {
  269. int value;
  270. if (optlen < sizeof(int))
  271. return -EINVAL;
  272. if (get_user(value, (int __user *) optval))
  273. return -EFAULT;
  274. *optvar = !!value;
  275. return 0;
  276. }
  277. static int rds_cong_monitor(struct rds_sock *rs, char __user *optval,
  278. int optlen)
  279. {
  280. int ret;
  281. ret = rds_set_bool_option(&rs->rs_cong_monitor, optval, optlen);
  282. if (ret == 0) {
  283. if (rs->rs_cong_monitor) {
  284. rds_cong_add_socket(rs);
  285. } else {
  286. rds_cong_remove_socket(rs);
  287. rs->rs_cong_mask = 0;
  288. rs->rs_cong_notify = 0;
  289. }
  290. }
  291. return ret;
  292. }
  293. static int rds_set_transport(struct rds_sock *rs, char __user *optval,
  294. int optlen)
  295. {
  296. int t_type;
  297. if (rs->rs_transport)
  298. return -EOPNOTSUPP; /* previously attached to transport */
  299. if (optlen != sizeof(int))
  300. return -EINVAL;
  301. if (copy_from_user(&t_type, (int __user *)optval, sizeof(t_type)))
  302. return -EFAULT;
  303. if (t_type < 0 || t_type >= RDS_TRANS_COUNT)
  304. return -EINVAL;
  305. rs->rs_transport = rds_trans_get(t_type);
  306. return rs->rs_transport ? 0 : -ENOPROTOOPT;
  307. }
  308. static int rds_enable_recvtstamp(struct sock *sk, char __user *optval,
  309. int optlen)
  310. {
  311. int val, valbool;
  312. if (optlen != sizeof(int))
  313. return -EFAULT;
  314. if (get_user(val, (int __user *)optval))
  315. return -EFAULT;
  316. valbool = val ? 1 : 0;
  317. if (valbool)
  318. sock_set_flag(sk, SOCK_RCVTSTAMP);
  319. else
  320. sock_reset_flag(sk, SOCK_RCVTSTAMP);
  321. return 0;
  322. }
  323. static int rds_recv_track_latency(struct rds_sock *rs, char __user *optval,
  324. int optlen)
  325. {
  326. struct rds_rx_trace_so trace;
  327. int i;
  328. if (optlen != sizeof(struct rds_rx_trace_so))
  329. return -EFAULT;
  330. if (copy_from_user(&trace, optval, sizeof(trace)))
  331. return -EFAULT;
  332. if (trace.rx_traces > RDS_MSG_RX_DGRAM_TRACE_MAX)
  333. return -EFAULT;
  334. rs->rs_rx_traces = trace.rx_traces;
  335. for (i = 0; i < rs->rs_rx_traces; i++) {
  336. if (trace.rx_trace_pos[i] > RDS_MSG_RX_DGRAM_TRACE_MAX) {
  337. rs->rs_rx_traces = 0;
  338. return -EFAULT;
  339. }
  340. rs->rs_rx_trace[i] = trace.rx_trace_pos[i];
  341. }
  342. return 0;
  343. }
  344. static int rds_setsockopt(struct socket *sock, int level, int optname,
  345. char __user *optval, unsigned int optlen)
  346. {
  347. struct rds_sock *rs = rds_sk_to_rs(sock->sk);
  348. int ret;
  349. if (level != SOL_RDS) {
  350. ret = -ENOPROTOOPT;
  351. goto out;
  352. }
  353. switch (optname) {
  354. case RDS_CANCEL_SENT_TO:
  355. ret = rds_cancel_sent_to(rs, optval, optlen);
  356. break;
  357. case RDS_GET_MR:
  358. ret = rds_get_mr(rs, optval, optlen);
  359. break;
  360. case RDS_GET_MR_FOR_DEST:
  361. ret = rds_get_mr_for_dest(rs, optval, optlen);
  362. break;
  363. case RDS_FREE_MR:
  364. ret = rds_free_mr(rs, optval, optlen);
  365. break;
  366. case RDS_RECVERR:
  367. ret = rds_set_bool_option(&rs->rs_recverr, optval, optlen);
  368. break;
  369. case RDS_CONG_MONITOR:
  370. ret = rds_cong_monitor(rs, optval, optlen);
  371. break;
  372. case SO_RDS_TRANSPORT:
  373. lock_sock(sock->sk);
  374. ret = rds_set_transport(rs, optval, optlen);
  375. release_sock(sock->sk);
  376. break;
  377. case SO_TIMESTAMP:
  378. lock_sock(sock->sk);
  379. ret = rds_enable_recvtstamp(sock->sk, optval, optlen);
  380. release_sock(sock->sk);
  381. break;
  382. case SO_RDS_MSG_RXPATH_LATENCY:
  383. ret = rds_recv_track_latency(rs, optval, optlen);
  384. break;
  385. default:
  386. ret = -ENOPROTOOPT;
  387. }
  388. out:
  389. return ret;
  390. }
  391. static int rds_getsockopt(struct socket *sock, int level, int optname,
  392. char __user *optval, int __user *optlen)
  393. {
  394. struct rds_sock *rs = rds_sk_to_rs(sock->sk);
  395. int ret = -ENOPROTOOPT, len;
  396. int trans;
  397. if (level != SOL_RDS)
  398. goto out;
  399. if (get_user(len, optlen)) {
  400. ret = -EFAULT;
  401. goto out;
  402. }
  403. switch (optname) {
  404. case RDS_INFO_FIRST ... RDS_INFO_LAST:
  405. ret = rds_info_getsockopt(sock, optname, optval,
  406. optlen);
  407. break;
  408. case RDS_RECVERR:
  409. if (len < sizeof(int))
  410. ret = -EINVAL;
  411. else
  412. if (put_user(rs->rs_recverr, (int __user *) optval) ||
  413. put_user(sizeof(int), optlen))
  414. ret = -EFAULT;
  415. else
  416. ret = 0;
  417. break;
  418. case SO_RDS_TRANSPORT:
  419. if (len < sizeof(int)) {
  420. ret = -EINVAL;
  421. break;
  422. }
  423. trans = (rs->rs_transport ? rs->rs_transport->t_type :
  424. RDS_TRANS_NONE); /* unbound */
  425. if (put_user(trans, (int __user *)optval) ||
  426. put_user(sizeof(int), optlen))
  427. ret = -EFAULT;
  428. else
  429. ret = 0;
  430. break;
  431. default:
  432. break;
  433. }
  434. out:
  435. return ret;
  436. }
  437. static int rds_connect(struct socket *sock, struct sockaddr *uaddr,
  438. int addr_len, int flags)
  439. {
  440. struct sock *sk = sock->sk;
  441. struct sockaddr_in *sin;
  442. struct rds_sock *rs = rds_sk_to_rs(sk);
  443. int ret = 0;
  444. if (addr_len < offsetofend(struct sockaddr, sa_family))
  445. return -EINVAL;
  446. lock_sock(sk);
  447. switch (uaddr->sa_family) {
  448. case AF_INET:
  449. sin = (struct sockaddr_in *)uaddr;
  450. if (addr_len < sizeof(struct sockaddr_in)) {
  451. ret = -EINVAL;
  452. break;
  453. }
  454. if (sin->sin_addr.s_addr == htonl(INADDR_ANY)) {
  455. ret = -EDESTADDRREQ;
  456. break;
  457. }
  458. if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) ||
  459. sin->sin_addr.s_addr == htonl(INADDR_BROADCAST)) {
  460. ret = -EINVAL;
  461. break;
  462. }
  463. ipv6_addr_set_v4mapped(sin->sin_addr.s_addr, &rs->rs_conn_addr);
  464. rs->rs_conn_port = sin->sin_port;
  465. break;
  466. #if IS_ENABLED(CONFIG_IPV6)
  467. case AF_INET6: {
  468. struct sockaddr_in6 *sin6;
  469. int addr_type;
  470. sin6 = (struct sockaddr_in6 *)uaddr;
  471. if (addr_len < sizeof(struct sockaddr_in6)) {
  472. ret = -EINVAL;
  473. break;
  474. }
  475. addr_type = ipv6_addr_type(&sin6->sin6_addr);
  476. if (!(addr_type & IPV6_ADDR_UNICAST)) {
  477. __be32 addr4;
  478. if (!(addr_type & IPV6_ADDR_MAPPED)) {
  479. ret = -EPROTOTYPE;
  480. break;
  481. }
  482. /* It is a mapped address. Need to do some sanity
  483. * checks.
  484. */
  485. addr4 = sin6->sin6_addr.s6_addr32[3];
  486. if (addr4 == htonl(INADDR_ANY) ||
  487. addr4 == htonl(INADDR_BROADCAST) ||
  488. IN_MULTICAST(ntohl(addr4))) {
  489. ret = -EPROTOTYPE;
  490. break;
  491. }
  492. }
  493. if (addr_type & IPV6_ADDR_LINKLOCAL) {
  494. /* If socket is arleady bound to a link local address,
  495. * the peer address must be on the same link.
  496. */
  497. if (sin6->sin6_scope_id == 0 ||
  498. (!ipv6_addr_any(&rs->rs_bound_addr) &&
  499. rs->rs_bound_scope_id &&
  500. sin6->sin6_scope_id != rs->rs_bound_scope_id)) {
  501. ret = -EINVAL;
  502. break;
  503. }
  504. /* Remember the connected address scope ID. It will
  505. * be checked against the binding local address when
  506. * the socket is bound.
  507. */
  508. rs->rs_bound_scope_id = sin6->sin6_scope_id;
  509. }
  510. rs->rs_conn_addr = sin6->sin6_addr;
  511. rs->rs_conn_port = sin6->sin6_port;
  512. break;
  513. }
  514. #endif
  515. default:
  516. ret = -EAFNOSUPPORT;
  517. break;
  518. }
  519. release_sock(sk);
  520. return ret;
  521. }
  522. static struct proto rds_proto = {
  523. .name = "RDS",
  524. .owner = THIS_MODULE,
  525. .obj_size = sizeof(struct rds_sock),
  526. };
  527. static const struct proto_ops rds_proto_ops = {
  528. .family = AF_RDS,
  529. .owner = THIS_MODULE,
  530. .release = rds_release,
  531. .bind = rds_bind,
  532. .connect = rds_connect,
  533. .socketpair = sock_no_socketpair,
  534. .accept = sock_no_accept,
  535. .getname = rds_getname,
  536. .poll = rds_poll,
  537. .ioctl = rds_ioctl,
  538. .listen = sock_no_listen,
  539. .shutdown = sock_no_shutdown,
  540. .setsockopt = rds_setsockopt,
  541. .getsockopt = rds_getsockopt,
  542. .sendmsg = rds_sendmsg,
  543. .recvmsg = rds_recvmsg,
  544. .mmap = sock_no_mmap,
  545. .sendpage = sock_no_sendpage,
  546. };
  547. static void rds_sock_destruct(struct sock *sk)
  548. {
  549. struct rds_sock *rs = rds_sk_to_rs(sk);
  550. WARN_ON((&rs->rs_item != rs->rs_item.next ||
  551. &rs->rs_item != rs->rs_item.prev));
  552. }
  553. static int __rds_create(struct socket *sock, struct sock *sk, int protocol)
  554. {
  555. struct rds_sock *rs;
  556. sock_init_data(sock, sk);
  557. sock->ops = &rds_proto_ops;
  558. sk->sk_protocol = protocol;
  559. sk->sk_destruct = rds_sock_destruct;
  560. rs = rds_sk_to_rs(sk);
  561. spin_lock_init(&rs->rs_lock);
  562. rwlock_init(&rs->rs_recv_lock);
  563. INIT_LIST_HEAD(&rs->rs_send_queue);
  564. INIT_LIST_HEAD(&rs->rs_recv_queue);
  565. INIT_LIST_HEAD(&rs->rs_notify_queue);
  566. INIT_LIST_HEAD(&rs->rs_cong_list);
  567. rds_message_zcopy_queue_init(&rs->rs_zcookie_queue);
  568. spin_lock_init(&rs->rs_rdma_lock);
  569. rs->rs_rdma_keys = RB_ROOT;
  570. rs->rs_rx_traces = 0;
  571. spin_lock_bh(&rds_sock_lock);
  572. list_add_tail(&rs->rs_item, &rds_sock_list);
  573. rds_sock_count++;
  574. spin_unlock_bh(&rds_sock_lock);
  575. return 0;
  576. }
  577. static int rds_create(struct net *net, struct socket *sock, int protocol,
  578. int kern)
  579. {
  580. struct sock *sk;
  581. if (sock->type != SOCK_SEQPACKET || protocol)
  582. return -ESOCKTNOSUPPORT;
  583. sk = sk_alloc(net, AF_RDS, GFP_ATOMIC, &rds_proto, kern);
  584. if (!sk)
  585. return -ENOMEM;
  586. return __rds_create(sock, sk, protocol);
  587. }
  588. void rds_sock_addref(struct rds_sock *rs)
  589. {
  590. sock_hold(rds_rs_to_sk(rs));
  591. }
  592. void rds_sock_put(struct rds_sock *rs)
  593. {
  594. sock_put(rds_rs_to_sk(rs));
  595. }
  596. static const struct net_proto_family rds_family_ops = {
  597. .family = AF_RDS,
  598. .create = rds_create,
  599. .owner = THIS_MODULE,
  600. };
  601. static void rds_sock_inc_info(struct socket *sock, unsigned int len,
  602. struct rds_info_iterator *iter,
  603. struct rds_info_lengths *lens)
  604. {
  605. struct rds_sock *rs;
  606. struct rds_incoming *inc;
  607. unsigned int total = 0;
  608. len /= sizeof(struct rds_info_message);
  609. spin_lock_bh(&rds_sock_lock);
  610. list_for_each_entry(rs, &rds_sock_list, rs_item) {
  611. read_lock(&rs->rs_recv_lock);
  612. /* XXX too lazy to maintain counts.. */
  613. list_for_each_entry(inc, &rs->rs_recv_queue, i_item) {
  614. total++;
  615. if (total <= len)
  616. rds_inc_info_copy(inc, iter,
  617. inc->i_saddr.s6_addr32[3],
  618. rs->rs_bound_addr_v4,
  619. 1);
  620. }
  621. read_unlock(&rs->rs_recv_lock);
  622. }
  623. spin_unlock_bh(&rds_sock_lock);
  624. lens->nr = total;
  625. lens->each = sizeof(struct rds_info_message);
  626. }
  627. static void rds_sock_info(struct socket *sock, unsigned int len,
  628. struct rds_info_iterator *iter,
  629. struct rds_info_lengths *lens)
  630. {
  631. struct rds_info_socket sinfo;
  632. struct rds_sock *rs;
  633. len /= sizeof(struct rds_info_socket);
  634. spin_lock_bh(&rds_sock_lock);
  635. if (len < rds_sock_count)
  636. goto out;
  637. list_for_each_entry(rs, &rds_sock_list, rs_item) {
  638. sinfo.sndbuf = rds_sk_sndbuf(rs);
  639. sinfo.rcvbuf = rds_sk_rcvbuf(rs);
  640. sinfo.bound_addr = rs->rs_bound_addr_v4;
  641. sinfo.connected_addr = rs->rs_conn_addr_v4;
  642. sinfo.bound_port = rs->rs_bound_port;
  643. sinfo.connected_port = rs->rs_conn_port;
  644. sinfo.inum = sock_i_ino(rds_rs_to_sk(rs));
  645. rds_info_copy(iter, &sinfo, sizeof(sinfo));
  646. }
  647. out:
  648. lens->nr = rds_sock_count;
  649. lens->each = sizeof(struct rds_info_socket);
  650. spin_unlock_bh(&rds_sock_lock);
  651. }
  652. static void rds_exit(void)
  653. {
  654. sock_unregister(rds_family_ops.family);
  655. proto_unregister(&rds_proto);
  656. rds_conn_exit();
  657. rds_cong_exit();
  658. rds_sysctl_exit();
  659. rds_threads_exit();
  660. rds_stats_exit();
  661. rds_page_exit();
  662. rds_bind_lock_destroy();
  663. rds_info_deregister_func(RDS_INFO_SOCKETS, rds_sock_info);
  664. rds_info_deregister_func(RDS_INFO_RECV_MESSAGES, rds_sock_inc_info);
  665. }
  666. module_exit(rds_exit);
  667. u32 rds_gen_num;
  668. static int rds_init(void)
  669. {
  670. int ret;
  671. net_get_random_once(&rds_gen_num, sizeof(rds_gen_num));
  672. ret = rds_bind_lock_init();
  673. if (ret)
  674. goto out;
  675. ret = rds_conn_init();
  676. if (ret)
  677. goto out_bind;
  678. ret = rds_threads_init();
  679. if (ret)
  680. goto out_conn;
  681. ret = rds_sysctl_init();
  682. if (ret)
  683. goto out_threads;
  684. ret = rds_stats_init();
  685. if (ret)
  686. goto out_sysctl;
  687. ret = proto_register(&rds_proto, 1);
  688. if (ret)
  689. goto out_stats;
  690. ret = sock_register(&rds_family_ops);
  691. if (ret)
  692. goto out_proto;
  693. rds_info_register_func(RDS_INFO_SOCKETS, rds_sock_info);
  694. rds_info_register_func(RDS_INFO_RECV_MESSAGES, rds_sock_inc_info);
  695. goto out;
  696. out_proto:
  697. proto_unregister(&rds_proto);
  698. out_stats:
  699. rds_stats_exit();
  700. out_sysctl:
  701. rds_sysctl_exit();
  702. out_threads:
  703. rds_threads_exit();
  704. out_conn:
  705. rds_conn_exit();
  706. rds_cong_exit();
  707. rds_page_exit();
  708. out_bind:
  709. rds_bind_lock_destroy();
  710. out:
  711. return ret;
  712. }
  713. module_init(rds_init);
  714. #define DRV_VERSION "4.0"
  715. #define DRV_RELDATE "Feb 12, 2009"
  716. MODULE_AUTHOR("Oracle Corporation <rds-devel@oss.oracle.com>");
  717. MODULE_DESCRIPTION("RDS: Reliable Datagram Sockets"
  718. " v" DRV_VERSION " (" DRV_RELDATE ")");
  719. MODULE_VERSION(DRV_VERSION);
  720. MODULE_LICENSE("Dual BSD/GPL");
  721. /* MODULE_ALIAS_NETPROTO(PF_RDS); */