recv.c 15 KB

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