tcp_send.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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/in.h>
  35. #include <net/tcp.h>
  36. #include "rds.h"
  37. #include "tcp.h"
  38. static void rds_tcp_cork(struct socket *sock, int val)
  39. {
  40. mm_segment_t oldfs;
  41. oldfs = get_fs();
  42. set_fs(KERNEL_DS);
  43. sock->ops->setsockopt(sock, SOL_TCP, TCP_CORK, (char __user *)&val,
  44. sizeof(val));
  45. set_fs(oldfs);
  46. }
  47. void rds_tcp_xmit_prepare(struct rds_connection *conn)
  48. {
  49. struct rds_tcp_connection *tc = conn->c_transport_data;
  50. rds_tcp_cork(tc->t_sock, 1);
  51. }
  52. void rds_tcp_xmit_complete(struct rds_connection *conn)
  53. {
  54. struct rds_tcp_connection *tc = conn->c_transport_data;
  55. rds_tcp_cork(tc->t_sock, 0);
  56. }
  57. /* the core send_sem serializes this with other xmit and shutdown */
  58. int rds_tcp_sendmsg(struct socket *sock, void *data, unsigned int len)
  59. {
  60. struct kvec vec = {
  61. .iov_base = data,
  62. .iov_len = len,
  63. };
  64. struct msghdr msg = {
  65. .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL,
  66. };
  67. return kernel_sendmsg(sock, &msg, &vec, 1, vec.iov_len);
  68. }
  69. /* the core send_sem serializes this with other xmit and shutdown */
  70. int rds_tcp_xmit_cong_map(struct rds_connection *conn,
  71. struct rds_cong_map *map, unsigned long offset)
  72. {
  73. static struct rds_header rds_tcp_map_header = {
  74. .h_flags = RDS_FLAG_CONG_BITMAP,
  75. };
  76. struct rds_tcp_connection *tc = conn->c_transport_data;
  77. unsigned long i;
  78. int ret;
  79. int copied = 0;
  80. /* Some problem claims cpu_to_be32(constant) isn't a constant. */
  81. rds_tcp_map_header.h_len = cpu_to_be32(RDS_CONG_MAP_BYTES);
  82. if (offset < sizeof(struct rds_header)) {
  83. ret = rds_tcp_sendmsg(tc->t_sock,
  84. (void *)&rds_tcp_map_header + offset,
  85. sizeof(struct rds_header) - offset);
  86. if (ret <= 0)
  87. return ret;
  88. offset += ret;
  89. copied = ret;
  90. if (offset < sizeof(struct rds_header))
  91. return ret;
  92. }
  93. offset -= sizeof(struct rds_header);
  94. i = offset / PAGE_SIZE;
  95. offset = offset % PAGE_SIZE;
  96. BUG_ON(i >= RDS_CONG_MAP_PAGES);
  97. do {
  98. ret = tc->t_sock->ops->sendpage(tc->t_sock,
  99. virt_to_page(map->m_page_addrs[i]),
  100. offset, PAGE_SIZE - offset,
  101. MSG_DONTWAIT);
  102. if (ret <= 0)
  103. break;
  104. copied += ret;
  105. offset += ret;
  106. if (offset == PAGE_SIZE) {
  107. offset = 0;
  108. i++;
  109. }
  110. } while (i < RDS_CONG_MAP_PAGES);
  111. return copied ? copied : ret;
  112. }
  113. /* the core send_sem serializes this with other xmit and shutdown */
  114. int rds_tcp_xmit(struct rds_connection *conn, struct rds_message *rm,
  115. unsigned int hdr_off, unsigned int sg, unsigned int off)
  116. {
  117. struct rds_tcp_connection *tc = conn->c_transport_data;
  118. int done = 0;
  119. int ret = 0;
  120. if (hdr_off == 0) {
  121. /*
  122. * m_ack_seq is set to the sequence number of the last byte of
  123. * header and data. see rds_tcp_is_acked().
  124. */
  125. tc->t_last_sent_nxt = rds_tcp_snd_nxt(tc);
  126. rm->m_ack_seq = tc->t_last_sent_nxt +
  127. sizeof(struct rds_header) +
  128. be32_to_cpu(rm->m_inc.i_hdr.h_len) - 1;
  129. smp_mb__before_clear_bit();
  130. set_bit(RDS_MSG_HAS_ACK_SEQ, &rm->m_flags);
  131. tc->t_last_expected_una = rm->m_ack_seq + 1;
  132. rdsdebug("rm %p tcp nxt %u ack_seq %llu\n",
  133. rm, rds_tcp_snd_nxt(tc),
  134. (unsigned long long)rm->m_ack_seq);
  135. }
  136. if (hdr_off < sizeof(struct rds_header)) {
  137. /* see rds_tcp_write_space() */
  138. set_bit(SOCK_NOSPACE, &tc->t_sock->sk->sk_socket->flags);
  139. ret = rds_tcp_sendmsg(tc->t_sock,
  140. (void *)&rm->m_inc.i_hdr + hdr_off,
  141. sizeof(rm->m_inc.i_hdr) - hdr_off);
  142. if (ret < 0)
  143. goto out;
  144. done += ret;
  145. if (hdr_off + done != sizeof(struct rds_header))
  146. goto out;
  147. }
  148. while (sg < rm->m_nents) {
  149. ret = tc->t_sock->ops->sendpage(tc->t_sock,
  150. sg_page(&rm->m_sg[sg]),
  151. rm->m_sg[sg].offset + off,
  152. rm->m_sg[sg].length - off,
  153. MSG_DONTWAIT|MSG_NOSIGNAL);
  154. rdsdebug("tcp sendpage %p:%u:%u ret %d\n", (void *)sg_page(&rm->m_sg[sg]),
  155. rm->m_sg[sg].offset + off, rm->m_sg[sg].length - off,
  156. ret);
  157. if (ret <= 0)
  158. break;
  159. off += ret;
  160. done += ret;
  161. if (off == rm->m_sg[sg].length) {
  162. off = 0;
  163. sg++;
  164. }
  165. }
  166. out:
  167. if (ret <= 0) {
  168. /* write_space will hit after EAGAIN, all else fatal */
  169. if (ret == -EAGAIN) {
  170. rds_tcp_stats_inc(s_tcp_sndbuf_full);
  171. ret = 0;
  172. } else {
  173. printk(KERN_WARNING "RDS/tcp: send to %u.%u.%u.%u "
  174. "returned %d, disconnecting and reconnecting\n",
  175. NIPQUAD(conn->c_faddr), ret);
  176. rds_conn_drop(conn);
  177. }
  178. }
  179. if (done == 0)
  180. done = ret;
  181. return done;
  182. }
  183. /*
  184. * rm->m_ack_seq is set to the tcp sequence number that corresponds to the
  185. * last byte of the message, including the header. This means that the
  186. * entire message has been received if rm->m_ack_seq is "before" the next
  187. * unacked byte of the TCP sequence space. We have to do very careful
  188. * wrapping 32bit comparisons here.
  189. */
  190. static int rds_tcp_is_acked(struct rds_message *rm, uint64_t ack)
  191. {
  192. if (!test_bit(RDS_MSG_HAS_ACK_SEQ, &rm->m_flags))
  193. return 0;
  194. return (__s32)((u32)rm->m_ack_seq - (u32)ack) < 0;
  195. }
  196. void rds_tcp_write_space(struct sock *sk)
  197. {
  198. void (*write_space)(struct sock *sk);
  199. struct rds_connection *conn;
  200. struct rds_tcp_connection *tc;
  201. read_lock(&sk->sk_callback_lock);
  202. conn = sk->sk_user_data;
  203. if (conn == NULL) {
  204. write_space = sk->sk_write_space;
  205. goto out;
  206. }
  207. tc = conn->c_transport_data;
  208. rdsdebug("write_space for tc %p\n", tc);
  209. write_space = tc->t_orig_write_space;
  210. rds_tcp_stats_inc(s_tcp_write_space_calls);
  211. rdsdebug("tcp una %u\n", rds_tcp_snd_una(tc));
  212. tc->t_last_seen_una = rds_tcp_snd_una(tc);
  213. rds_send_drop_acked(conn, rds_tcp_snd_una(tc), rds_tcp_is_acked);
  214. queue_delayed_work(rds_wq, &conn->c_send_w, 0);
  215. out:
  216. read_unlock(&sk->sk_callback_lock);
  217. /*
  218. * write_space is only called when data leaves tcp's send queue if
  219. * SOCK_NOSPACE is set. We set SOCK_NOSPACE every time we put
  220. * data in tcp's send queue because we use write_space to parse the
  221. * sequence numbers and notice that rds messages have been fully
  222. * received.
  223. *
  224. * tcp's write_space clears SOCK_NOSPACE if the send queue has more
  225. * than a certain amount of space. So we need to set it again *after*
  226. * we call tcp's write_space or else we might only get called on the
  227. * first of a series of incoming tcp acks.
  228. */
  229. write_space(sk);
  230. if (sk->sk_socket)
  231. set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
  232. }