recv.c 16 KB

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