recv.c 15 KB

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