tcp_send.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * Copyright (c) 2006, 2017 Oracle and/or its affiliates. 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/in.h>
  35. #include <net/tcp.h>
  36. #include "rds_single_path.h"
  37. #include "rds.h"
  38. #include "tcp.h"
  39. void rds_tcp_xmit_path_prepare(struct rds_conn_path *cp)
  40. {
  41. struct rds_tcp_connection *tc = cp->cp_transport_data;
  42. tcp_sock_set_cork(tc->t_sock->sk, true);
  43. }
  44. void rds_tcp_xmit_path_complete(struct rds_conn_path *cp)
  45. {
  46. struct rds_tcp_connection *tc = cp->cp_transport_data;
  47. tcp_sock_set_cork(tc->t_sock->sk, false);
  48. }
  49. /* the core send_sem serializes this with other xmit and shutdown */
  50. static int rds_tcp_sendmsg(struct socket *sock, void *data, unsigned int len)
  51. {
  52. struct kvec vec = {
  53. .iov_base = data,
  54. .iov_len = len,
  55. };
  56. struct msghdr msg = {
  57. .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL,
  58. };
  59. return kernel_sendmsg(sock, &msg, &vec, 1, vec.iov_len);
  60. }
  61. /* the core send_sem serializes this with other xmit and shutdown */
  62. int rds_tcp_xmit(struct rds_connection *conn, struct rds_message *rm,
  63. unsigned int hdr_off, unsigned int sg, unsigned int off)
  64. {
  65. struct rds_conn_path *cp = rm->m_inc.i_conn_path;
  66. struct rds_tcp_connection *tc = cp->cp_transport_data;
  67. struct msghdr msg = {};
  68. struct bio_vec bvec;
  69. int done = 0;
  70. int ret = 0;
  71. if (hdr_off == 0) {
  72. /*
  73. * m_ack_seq is set to the sequence number of the last byte of
  74. * header and data. see rds_tcp_is_acked().
  75. */
  76. tc->t_last_sent_nxt = rds_tcp_write_seq(tc);
  77. rm->m_ack_seq = tc->t_last_sent_nxt +
  78. sizeof(struct rds_header) +
  79. be32_to_cpu(rm->m_inc.i_hdr.h_len) - 1;
  80. smp_mb__before_atomic();
  81. set_bit(RDS_MSG_HAS_ACK_SEQ, &rm->m_flags);
  82. tc->t_last_expected_una = rm->m_ack_seq + 1;
  83. if (test_bit(RDS_MSG_RETRANSMITTED, &rm->m_flags))
  84. rm->m_inc.i_hdr.h_flags |= RDS_FLAG_RETRANSMITTED;
  85. rdsdebug("rm %p tcp nxt %u ack_seq %llu\n",
  86. rm, rds_tcp_write_seq(tc),
  87. (unsigned long long)rm->m_ack_seq);
  88. }
  89. if (hdr_off < sizeof(struct rds_header)) {
  90. /* see rds_tcp_write_space() */
  91. set_bit(SOCK_NOSPACE, &tc->t_sock->sk->sk_socket->flags);
  92. ret = rds_tcp_sendmsg(tc->t_sock,
  93. (void *)&rm->m_inc.i_hdr + hdr_off,
  94. sizeof(rm->m_inc.i_hdr) - hdr_off);
  95. if (ret < 0)
  96. goto out;
  97. done += ret;
  98. if (hdr_off + done != sizeof(struct rds_header))
  99. goto out;
  100. }
  101. while (sg < rm->data.op_nents) {
  102. msg.msg_flags = MSG_SPLICE_PAGES | MSG_DONTWAIT | MSG_NOSIGNAL;
  103. if (sg + 1 < rm->data.op_nents)
  104. msg.msg_flags |= MSG_MORE;
  105. bvec_set_page(&bvec, sg_page(&rm->data.op_sg[sg]),
  106. rm->data.op_sg[sg].length - off,
  107. rm->data.op_sg[sg].offset + off);
  108. iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bvec, 1,
  109. rm->data.op_sg[sg].length - off);
  110. ret = sock_sendmsg(tc->t_sock, &msg);
  111. rdsdebug("tcp sendpage %p:%u:%u ret %d\n", (void *)sg_page(&rm->data.op_sg[sg]),
  112. rm->data.op_sg[sg].offset + off, rm->data.op_sg[sg].length - off,
  113. ret);
  114. if (ret <= 0)
  115. break;
  116. off += ret;
  117. done += ret;
  118. if (off == rm->data.op_sg[sg].length) {
  119. off = 0;
  120. sg++;
  121. }
  122. }
  123. out:
  124. if (ret <= 0) {
  125. /* write_space will hit after EAGAIN, all else fatal */
  126. if (ret == -EAGAIN) {
  127. rds_tcp_stats_inc(s_tcp_sndbuf_full);
  128. ret = 0;
  129. } else {
  130. /* No need to disconnect/reconnect if path_drop
  131. * has already been triggered, because, e.g., of
  132. * an incoming RST.
  133. */
  134. if (rds_conn_path_up(cp)) {
  135. pr_warn("RDS/tcp: send to %pI6c on cp [%d]"
  136. "returned %d, "
  137. "disconnecting and reconnecting\n",
  138. &conn->c_faddr, cp->cp_index, ret);
  139. rds_conn_path_drop(cp, false);
  140. }
  141. }
  142. }
  143. if (done == 0)
  144. done = ret;
  145. return done;
  146. }
  147. /*
  148. * rm->m_ack_seq is set to the tcp sequence number that corresponds to the
  149. * last byte of the message, including the header. This means that the
  150. * entire message has been received if rm->m_ack_seq is "before" the next
  151. * unacked byte of the TCP sequence space. We have to do very careful
  152. * wrapping 32bit comparisons here.
  153. */
  154. static int rds_tcp_is_acked(struct rds_message *rm, uint64_t ack)
  155. {
  156. if (!test_bit(RDS_MSG_HAS_ACK_SEQ, &rm->m_flags))
  157. return 0;
  158. return (__s32)((u32)rm->m_ack_seq - (u32)ack) < 0;
  159. }
  160. void rds_tcp_write_space(struct sock *sk)
  161. {
  162. void (*write_space)(struct sock *sk);
  163. struct rds_conn_path *cp;
  164. struct rds_tcp_connection *tc;
  165. read_lock_bh(&sk->sk_callback_lock);
  166. cp = sk->sk_user_data;
  167. if (!cp) {
  168. write_space = sk->sk_write_space;
  169. goto out;
  170. }
  171. tc = cp->cp_transport_data;
  172. rdsdebug("write_space for tc %p\n", tc);
  173. write_space = tc->t_orig_write_space;
  174. rds_tcp_stats_inc(s_tcp_write_space_calls);
  175. rdsdebug("tcp una %u\n", rds_tcp_snd_una(tc));
  176. tc->t_last_seen_una = rds_tcp_snd_una(tc);
  177. rds_send_path_drop_acked(cp, rds_tcp_snd_una(tc), rds_tcp_is_acked);
  178. rcu_read_lock();
  179. if ((refcount_read(&sk->sk_wmem_alloc) << 1) <= sk->sk_sndbuf &&
  180. !rds_destroy_pending(cp->cp_conn))
  181. queue_delayed_work(rds_wq, &cp->cp_send_w, 0);
  182. rcu_read_unlock();
  183. out:
  184. read_unlock_bh(&sk->sk_callback_lock);
  185. /*
  186. * write_space is only called when data leaves tcp's send queue if
  187. * SOCK_NOSPACE is set. We set SOCK_NOSPACE every time we put
  188. * data in tcp's send queue because we use write_space to parse the
  189. * sequence numbers and notice that rds messages have been fully
  190. * received.
  191. *
  192. * tcp's write_space clears SOCK_NOSPACE if the send queue has more
  193. * than a certain amount of space. So we need to set it again *after*
  194. * we call tcp's write_space or else we might only get called on the
  195. * first of a series of incoming tcp acks.
  196. */
  197. write_space(sk);
  198. if (sk->sk_socket)
  199. set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
  200. }