recv.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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/kernel.h>
  34. #include <net/sock.h>
  35. #include <linux/in.h>
  36. #include "rds.h"
  37. #include "rdma.h"
  38. void rds_inc_init(struct rds_incoming *inc, struct rds_connection *conn,
  39. __be32 saddr)
  40. {
  41. atomic_set(&inc->i_refcount, 1);
  42. INIT_LIST_HEAD(&inc->i_item);
  43. inc->i_conn = conn;
  44. inc->i_saddr = saddr;
  45. inc->i_rdma_cookie = 0;
  46. }
  47. void rds_inc_addref(struct rds_incoming *inc)
  48. {
  49. rdsdebug("addref inc %p ref %d\n", inc, atomic_read(&inc->i_refcount));
  50. atomic_inc(&inc->i_refcount);
  51. }
  52. void rds_inc_put(struct rds_incoming *inc)
  53. {
  54. rdsdebug("put inc %p ref %d\n", inc, atomic_read(&inc->i_refcount));
  55. if (atomic_dec_and_test(&inc->i_refcount)) {
  56. BUG_ON(!list_empty(&inc->i_item));
  57. inc->i_conn->c_trans->inc_free(inc);
  58. }
  59. }
  60. static void rds_recv_rcvbuf_delta(struct rds_sock *rs, struct sock *sk,
  61. struct rds_cong_map *map,
  62. int delta, __be16 port)
  63. {
  64. int now_congested;
  65. if (delta == 0)
  66. return;
  67. rs->rs_rcv_bytes += delta;
  68. now_congested = rs->rs_rcv_bytes > rds_sk_rcvbuf(rs);
  69. rdsdebug("rs %p (%pI4:%u) recv bytes %d buf %d "
  70. "now_cong %d delta %d\n",
  71. rs, &rs->rs_bound_addr,
  72. ntohs(rs->rs_bound_port), rs->rs_rcv_bytes,
  73. rds_sk_rcvbuf(rs), now_congested, delta);
  74. /* wasn't -> am congested */
  75. if (!rs->rs_congested && now_congested) {
  76. rs->rs_congested = 1;
  77. rds_cong_set_bit(map, port);
  78. rds_cong_queue_updates(map);
  79. }
  80. /* was -> aren't congested */
  81. /* Require more free space before reporting uncongested to prevent
  82. bouncing cong/uncong state too often */
  83. else if (rs->rs_congested && (rs->rs_rcv_bytes < (rds_sk_rcvbuf(rs)/2))) {
  84. rs->rs_congested = 0;
  85. rds_cong_clear_bit(map, port);
  86. rds_cong_queue_updates(map);
  87. }
  88. /* do nothing if no change in cong state */
  89. }
  90. /*
  91. * Process all extension headers that come with this message.
  92. */
  93. static void rds_recv_incoming_exthdrs(struct rds_incoming *inc, struct rds_sock *rs)
  94. {
  95. struct rds_header *hdr = &inc->i_hdr;
  96. unsigned int pos = 0, type, len;
  97. union {
  98. struct rds_ext_header_version version;
  99. struct rds_ext_header_rdma rdma;
  100. struct rds_ext_header_rdma_dest rdma_dest;
  101. } buffer;
  102. while (1) {
  103. len = sizeof(buffer);
  104. type = rds_message_next_extension(hdr, &pos, &buffer, &len);
  105. if (type == RDS_EXTHDR_NONE)
  106. break;
  107. /* Process extension header here */
  108. switch (type) {
  109. case RDS_EXTHDR_RDMA:
  110. rds_rdma_unuse(rs, be32_to_cpu(buffer.rdma.h_rdma_rkey), 0);
  111. break;
  112. case RDS_EXTHDR_RDMA_DEST:
  113. /* We ignore the size for now. We could stash it
  114. * somewhere and use it for error checking. */
  115. inc->i_rdma_cookie = rds_rdma_make_cookie(
  116. be32_to_cpu(buffer.rdma_dest.h_rdma_rkey),
  117. be32_to_cpu(buffer.rdma_dest.h_rdma_offset));
  118. break;
  119. }
  120. }
  121. }
  122. /*
  123. * The transport must make sure that this is serialized against other
  124. * rx and conn reset on this specific conn.
  125. *
  126. * We currently assert that only one fragmented message will be sent
  127. * down a connection at a time. This lets us reassemble in the conn
  128. * instead of per-flow which means that we don't have to go digging through
  129. * flows to tear down partial reassembly progress on conn failure and
  130. * we save flow lookup and locking for each frag arrival. It does mean
  131. * that small messages will wait behind large ones. Fragmenting at all
  132. * is only to reduce the memory consumption of pre-posted buffers.
  133. *
  134. * The caller passes in saddr and daddr instead of us getting it from the
  135. * conn. This lets loopback, who only has one conn for both directions,
  136. * tell us which roles the addrs in the conn are playing for this message.
  137. */
  138. void rds_recv_incoming(struct rds_connection *conn, __be32 saddr, __be32 daddr,
  139. struct rds_incoming *inc, gfp_t gfp, enum km_type km)
  140. {
  141. struct rds_sock *rs = NULL;
  142. struct sock *sk;
  143. unsigned long flags;
  144. inc->i_conn = conn;
  145. inc->i_rx_jiffies = jiffies;
  146. rdsdebug("conn %p next %llu inc %p seq %llu len %u sport %u dport %u "
  147. "flags 0x%x rx_jiffies %lu\n", conn,
  148. (unsigned long long)conn->c_next_rx_seq,
  149. inc,
  150. (unsigned long long)be64_to_cpu(inc->i_hdr.h_sequence),
  151. be32_to_cpu(inc->i_hdr.h_len),
  152. be16_to_cpu(inc->i_hdr.h_sport),
  153. be16_to_cpu(inc->i_hdr.h_dport),
  154. inc->i_hdr.h_flags,
  155. inc->i_rx_jiffies);
  156. /*
  157. * Sequence numbers should only increase. Messages get their
  158. * sequence number as they're queued in a sending conn. They
  159. * can be dropped, though, if the sending socket is closed before
  160. * they hit the wire. So sequence numbers can skip forward
  161. * under normal operation. They can also drop back in the conn
  162. * failover case as previously sent messages are resent down the
  163. * new instance of a conn. We drop those, otherwise we have
  164. * to assume that the next valid seq does not come after a
  165. * hole in the fragment stream.
  166. *
  167. * The headers don't give us a way to realize if fragments of
  168. * a message have been dropped. We assume that frags that arrive
  169. * to a flow are part of the current message on the flow that is
  170. * being reassembled. This means that senders can't drop messages
  171. * from the sending conn until all their frags are sent.
  172. *
  173. * XXX we could spend more on the wire to get more robust failure
  174. * detection, arguably worth it to avoid data corruption.
  175. */
  176. if (be64_to_cpu(inc->i_hdr.h_sequence) < conn->c_next_rx_seq
  177. && (inc->i_hdr.h_flags & RDS_FLAG_RETRANSMITTED)) {
  178. rds_stats_inc(s_recv_drop_old_seq);
  179. goto out;
  180. }
  181. conn->c_next_rx_seq = be64_to_cpu(inc->i_hdr.h_sequence) + 1;
  182. if (rds_sysctl_ping_enable && inc->i_hdr.h_dport == 0) {
  183. rds_stats_inc(s_recv_ping);
  184. rds_send_pong(conn, inc->i_hdr.h_sport);
  185. goto out;
  186. }
  187. rs = rds_find_bound(daddr, inc->i_hdr.h_dport);
  188. if (rs == NULL) {
  189. rds_stats_inc(s_recv_drop_no_sock);
  190. goto out;
  191. }
  192. /* Process extension headers */
  193. rds_recv_incoming_exthdrs(inc, rs);
  194. /* We can be racing with rds_release() which marks the socket dead. */
  195. sk = rds_rs_to_sk(rs);
  196. /* serialize with rds_release -> sock_orphan */
  197. write_lock_irqsave(&rs->rs_recv_lock, flags);
  198. if (!sock_flag(sk, SOCK_DEAD)) {
  199. rdsdebug("adding inc %p to rs %p's recv queue\n", inc, rs);
  200. rds_stats_inc(s_recv_queued);
  201. rds_recv_rcvbuf_delta(rs, sk, inc->i_conn->c_lcong,
  202. be32_to_cpu(inc->i_hdr.h_len),
  203. inc->i_hdr.h_dport);
  204. rds_inc_addref(inc);
  205. list_add_tail(&inc->i_item, &rs->rs_recv_queue);
  206. __rds_wake_sk_sleep(sk);
  207. } else {
  208. rds_stats_inc(s_recv_drop_dead_sock);
  209. }
  210. write_unlock_irqrestore(&rs->rs_recv_lock, flags);
  211. out:
  212. if (rs)
  213. rds_sock_put(rs);
  214. }
  215. /*
  216. * be very careful here. This is being called as the condition in
  217. * wait_event_*() needs to cope with being called many times.
  218. */
  219. static int rds_next_incoming(struct rds_sock *rs, struct rds_incoming **inc)
  220. {
  221. unsigned long flags;
  222. if (*inc == NULL) {
  223. read_lock_irqsave(&rs->rs_recv_lock, flags);
  224. if (!list_empty(&rs->rs_recv_queue)) {
  225. *inc = list_entry(rs->rs_recv_queue.next,
  226. struct rds_incoming,
  227. i_item);
  228. rds_inc_addref(*inc);
  229. }
  230. read_unlock_irqrestore(&rs->rs_recv_lock, flags);
  231. }
  232. return *inc != NULL;
  233. }
  234. static int rds_still_queued(struct rds_sock *rs, struct rds_incoming *inc,
  235. int drop)
  236. {
  237. struct sock *sk = rds_rs_to_sk(rs);
  238. int ret = 0;
  239. unsigned long flags;
  240. write_lock_irqsave(&rs->rs_recv_lock, flags);
  241. if (!list_empty(&inc->i_item)) {
  242. ret = 1;
  243. if (drop) {
  244. /* XXX make sure this i_conn is reliable */
  245. rds_recv_rcvbuf_delta(rs, sk, inc->i_conn->c_lcong,
  246. -be32_to_cpu(inc->i_hdr.h_len),
  247. inc->i_hdr.h_dport);
  248. list_del_init(&inc->i_item);
  249. rds_inc_put(inc);
  250. }
  251. }
  252. write_unlock_irqrestore(&rs->rs_recv_lock, flags);
  253. rdsdebug("inc %p rs %p still %d dropped %d\n", inc, rs, ret, drop);
  254. return ret;
  255. }
  256. /*
  257. * Pull errors off the error queue.
  258. * If msghdr is NULL, we will just purge the error queue.
  259. */
  260. int rds_notify_queue_get(struct rds_sock *rs, struct msghdr *msghdr)
  261. {
  262. struct rds_notifier *notifier;
  263. struct rds_rdma_notify cmsg;
  264. unsigned int count = 0, max_messages = ~0U;
  265. unsigned long flags;
  266. LIST_HEAD(copy);
  267. int err = 0;
  268. /* put_cmsg copies to user space and thus may sleep. We can't do this
  269. * with rs_lock held, so first grab as many notifications as we can stuff
  270. * in the user provided cmsg buffer. We don't try to copy more, to avoid
  271. * losing notifications - except when the buffer is so small that it wouldn't
  272. * even hold a single notification. Then we give him as much of this single
  273. * msg as we can squeeze in, and set MSG_CTRUNC.
  274. */
  275. if (msghdr) {
  276. max_messages = msghdr->msg_controllen / CMSG_SPACE(sizeof(cmsg));
  277. if (!max_messages)
  278. max_messages = 1;
  279. }
  280. spin_lock_irqsave(&rs->rs_lock, flags);
  281. while (!list_empty(&rs->rs_notify_queue) && count < max_messages) {
  282. notifier = list_entry(rs->rs_notify_queue.next,
  283. struct rds_notifier, n_list);
  284. list_move(&notifier->n_list, &copy);
  285. count++;
  286. }
  287. spin_unlock_irqrestore(&rs->rs_lock, flags);
  288. if (!count)
  289. return 0;
  290. while (!list_empty(&copy)) {
  291. notifier = list_entry(copy.next, struct rds_notifier, n_list);
  292. if (msghdr) {
  293. cmsg.user_token = notifier->n_user_token;
  294. cmsg.status = notifier->n_status;
  295. err = put_cmsg(msghdr, SOL_RDS, RDS_CMSG_RDMA_STATUS,
  296. sizeof(cmsg), &cmsg);
  297. if (err)
  298. break;
  299. }
  300. list_del_init(&notifier->n_list);
  301. kfree(notifier);
  302. }
  303. /* If we bailed out because of an error in put_cmsg,
  304. * we may be left with one or more notifications that we
  305. * didn't process. Return them to the head of the list. */
  306. if (!list_empty(&copy)) {
  307. spin_lock_irqsave(&rs->rs_lock, flags);
  308. list_splice(&copy, &rs->rs_notify_queue);
  309. spin_unlock_irqrestore(&rs->rs_lock, flags);
  310. }
  311. return err;
  312. }
  313. /*
  314. * Queue a congestion notification
  315. */
  316. static int rds_notify_cong(struct rds_sock *rs, struct msghdr *msghdr)
  317. {
  318. uint64_t notify = rs->rs_cong_notify;
  319. unsigned long flags;
  320. int err;
  321. err = put_cmsg(msghdr, SOL_RDS, RDS_CMSG_CONG_UPDATE,
  322. sizeof(notify), &notify);
  323. if (err)
  324. return err;
  325. spin_lock_irqsave(&rs->rs_lock, flags);
  326. rs->rs_cong_notify &= ~notify;
  327. spin_unlock_irqrestore(&rs->rs_lock, flags);
  328. return 0;
  329. }
  330. /*
  331. * Receive any control messages.
  332. */
  333. static int rds_cmsg_recv(struct rds_incoming *inc, struct msghdr *msg)
  334. {
  335. int ret = 0;
  336. if (inc->i_rdma_cookie) {
  337. ret = put_cmsg(msg, SOL_RDS, RDS_CMSG_RDMA_DEST,
  338. sizeof(inc->i_rdma_cookie), &inc->i_rdma_cookie);
  339. if (ret)
  340. return ret;
  341. }
  342. return 0;
  343. }
  344. int rds_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
  345. size_t size, int msg_flags)
  346. {
  347. struct sock *sk = sock->sk;
  348. struct rds_sock *rs = rds_sk_to_rs(sk);
  349. long timeo;
  350. int ret = 0, nonblock = msg_flags & MSG_DONTWAIT;
  351. struct sockaddr_in *sin;
  352. struct rds_incoming *inc = NULL;
  353. /* udp_recvmsg()->sock_recvtimeo() gets away without locking too.. */
  354. timeo = sock_rcvtimeo(sk, nonblock);
  355. rdsdebug("size %zu flags 0x%x timeo %ld\n", size, msg_flags, timeo);
  356. if (msg_flags & MSG_OOB)
  357. goto out;
  358. /* If there are pending notifications, do those - and nothing else */
  359. if (!list_empty(&rs->rs_notify_queue)) {
  360. ret = rds_notify_queue_get(rs, msg);
  361. goto out;
  362. }
  363. if (rs->rs_cong_notify) {
  364. ret = rds_notify_cong(rs, msg);
  365. goto out;
  366. }
  367. while (1) {
  368. if (!rds_next_incoming(rs, &inc)) {
  369. if (nonblock) {
  370. ret = -EAGAIN;
  371. break;
  372. }
  373. timeo = wait_event_interruptible_timeout(*sk->sk_sleep,
  374. rds_next_incoming(rs, &inc),
  375. timeo);
  376. rdsdebug("recvmsg woke inc %p timeo %ld\n", inc,
  377. timeo);
  378. if (timeo > 0 || timeo == MAX_SCHEDULE_TIMEOUT)
  379. continue;
  380. ret = timeo;
  381. if (ret == 0)
  382. ret = -ETIMEDOUT;
  383. break;
  384. }
  385. rdsdebug("copying inc %p from %pI4:%u to user\n", inc,
  386. &inc->i_conn->c_faddr,
  387. ntohs(inc->i_hdr.h_sport));
  388. ret = inc->i_conn->c_trans->inc_copy_to_user(inc, msg->msg_iov,
  389. size);
  390. if (ret < 0)
  391. break;
  392. /*
  393. * if the message we just copied isn't at the head of the
  394. * recv queue then someone else raced us to return it, try
  395. * to get the next message.
  396. */
  397. if (!rds_still_queued(rs, inc, !(msg_flags & MSG_PEEK))) {
  398. rds_inc_put(inc);
  399. inc = NULL;
  400. rds_stats_inc(s_recv_deliver_raced);
  401. continue;
  402. }
  403. if (ret < be32_to_cpu(inc->i_hdr.h_len)) {
  404. if (msg_flags & MSG_TRUNC)
  405. ret = be32_to_cpu(inc->i_hdr.h_len);
  406. msg->msg_flags |= MSG_TRUNC;
  407. }
  408. if (rds_cmsg_recv(inc, msg)) {
  409. ret = -EFAULT;
  410. goto out;
  411. }
  412. rds_stats_inc(s_recv_delivered);
  413. sin = (struct sockaddr_in *)msg->msg_name;
  414. if (sin) {
  415. sin->sin_family = AF_INET;
  416. sin->sin_port = inc->i_hdr.h_sport;
  417. sin->sin_addr.s_addr = inc->i_saddr;
  418. memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
  419. }
  420. break;
  421. }
  422. if (inc)
  423. rds_inc_put(inc);
  424. out:
  425. return ret;
  426. }
  427. /*
  428. * The socket is being shut down and we're asked to drop messages that were
  429. * queued for recvmsg. The caller has unbound the socket so the receive path
  430. * won't queue any more incoming fragments or messages on the socket.
  431. */
  432. void rds_clear_recv_queue(struct rds_sock *rs)
  433. {
  434. struct sock *sk = rds_rs_to_sk(rs);
  435. struct rds_incoming *inc, *tmp;
  436. unsigned long flags;
  437. write_lock_irqsave(&rs->rs_recv_lock, flags);
  438. list_for_each_entry_safe(inc, tmp, &rs->rs_recv_queue, i_item) {
  439. rds_recv_rcvbuf_delta(rs, sk, inc->i_conn->c_lcong,
  440. -be32_to_cpu(inc->i_hdr.h_len),
  441. inc->i_hdr.h_dport);
  442. list_del_init(&inc->i_item);
  443. rds_inc_put(inc);
  444. }
  445. write_unlock_irqrestore(&rs->rs_recv_lock, flags);
  446. }
  447. /*
  448. * inc->i_saddr isn't used here because it is only set in the receive
  449. * path.
  450. */
  451. void rds_inc_info_copy(struct rds_incoming *inc,
  452. struct rds_info_iterator *iter,
  453. __be32 saddr, __be32 daddr, int flip)
  454. {
  455. struct rds_info_message minfo;
  456. minfo.seq = be64_to_cpu(inc->i_hdr.h_sequence);
  457. minfo.len = be32_to_cpu(inc->i_hdr.h_len);
  458. if (flip) {
  459. minfo.laddr = daddr;
  460. minfo.faddr = saddr;
  461. minfo.lport = inc->i_hdr.h_dport;
  462. minfo.fport = inc->i_hdr.h_sport;
  463. } else {
  464. minfo.laddr = saddr;
  465. minfo.faddr = daddr;
  466. minfo.lport = inc->i_hdr.h_sport;
  467. minfo.fport = inc->i_hdr.h_dport;
  468. }
  469. rds_info_copy(iter, &minfo, sizeof(minfo));
  470. }