recv.c 15 KB

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