recv.c 15 KB

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