Andy Grover | 7004108 | 2009-08-21 12:28:31 +0000 | [diff] [blame] | 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 | |
Sowmini Varadhan | 0cb4396 | 2016-06-13 09:44:26 -0700 | [diff] [blame] | 37 | #include "rds_single_path.h" |
Andy Grover | 7004108 | 2009-08-21 12:28:31 +0000 | [diff] [blame] | 38 | #include "rds.h" |
| 39 | #include "tcp.h" |
| 40 | |
| 41 | static void rds_tcp_cork(struct socket *sock, int val) |
| 42 | { |
Al Viro | e73a67f | 2017-03-18 21:20:27 -0400 | [diff] [blame^] | 43 | kernel_setsockopt(sock, SOL_TCP, TCP_CORK, (void *)&val, sizeof(val)); |
Andy Grover | 7004108 | 2009-08-21 12:28:31 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Sowmini Varadhan | 226f7a7 | 2016-06-30 16:11:10 -0700 | [diff] [blame] | 46 | void rds_tcp_xmit_path_prepare(struct rds_conn_path *cp) |
Andy Grover | 7004108 | 2009-08-21 12:28:31 +0000 | [diff] [blame] | 47 | { |
Sowmini Varadhan | 226f7a7 | 2016-06-30 16:11:10 -0700 | [diff] [blame] | 48 | struct rds_tcp_connection *tc = cp->cp_transport_data; |
Andy Grover | 7004108 | 2009-08-21 12:28:31 +0000 | [diff] [blame] | 49 | |
| 50 | rds_tcp_cork(tc->t_sock, 1); |
| 51 | } |
| 52 | |
Sowmini Varadhan | 226f7a7 | 2016-06-30 16:11:10 -0700 | [diff] [blame] | 53 | void rds_tcp_xmit_path_complete(struct rds_conn_path *cp) |
Andy Grover | 7004108 | 2009-08-21 12:28:31 +0000 | [diff] [blame] | 54 | { |
Sowmini Varadhan | 226f7a7 | 2016-06-30 16:11:10 -0700 | [diff] [blame] | 55 | struct rds_tcp_connection *tc = cp->cp_transport_data; |
Andy Grover | 7004108 | 2009-08-21 12:28:31 +0000 | [diff] [blame] | 56 | |
| 57 | rds_tcp_cork(tc->t_sock, 0); |
| 58 | } |
| 59 | |
| 60 | /* the core send_sem serializes this with other xmit and shutdown */ |
stephen hemminger | ff51bf8 | 2010-10-19 08:08:33 +0000 | [diff] [blame] | 61 | static int rds_tcp_sendmsg(struct socket *sock, void *data, unsigned int len) |
Andy Grover | 7004108 | 2009-08-21 12:28:31 +0000 | [diff] [blame] | 62 | { |
| 63 | struct kvec vec = { |
Joshua Houghton | 5c3da57 | 2016-06-18 15:46:31 +0000 | [diff] [blame] | 64 | .iov_base = data, |
| 65 | .iov_len = len, |
Andy Grover | 7004108 | 2009-08-21 12:28:31 +0000 | [diff] [blame] | 66 | }; |
Joshua Houghton | 5c3da57 | 2016-06-18 15:46:31 +0000 | [diff] [blame] | 67 | struct msghdr msg = { |
| 68 | .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL, |
| 69 | }; |
Andy Grover | 7004108 | 2009-08-21 12:28:31 +0000 | [diff] [blame] | 70 | |
| 71 | return kernel_sendmsg(sock, &msg, &vec, 1, vec.iov_len); |
| 72 | } |
| 73 | |
| 74 | /* the core send_sem serializes this with other xmit and shutdown */ |
Andy Grover | 7004108 | 2009-08-21 12:28:31 +0000 | [diff] [blame] | 75 | int rds_tcp_xmit(struct rds_connection *conn, struct rds_message *rm, |
Joshua Houghton | 5c3da57 | 2016-06-18 15:46:31 +0000 | [diff] [blame] | 76 | unsigned int hdr_off, unsigned int sg, unsigned int off) |
Andy Grover | 7004108 | 2009-08-21 12:28:31 +0000 | [diff] [blame] | 77 | { |
Sowmini Varadhan | 5916e2c | 2016-07-14 03:51:03 -0700 | [diff] [blame] | 78 | struct rds_conn_path *cp = rm->m_inc.i_conn_path; |
| 79 | struct rds_tcp_connection *tc = cp->cp_transport_data; |
Andy Grover | 7004108 | 2009-08-21 12:28:31 +0000 | [diff] [blame] | 80 | int done = 0; |
| 81 | int ret = 0; |
Sowmini Varadhan | 76b29ef | 2015-09-30 16:54:09 -0400 | [diff] [blame] | 82 | int more; |
Andy Grover | 7004108 | 2009-08-21 12:28:31 +0000 | [diff] [blame] | 83 | |
| 84 | if (hdr_off == 0) { |
| 85 | /* |
| 86 | * m_ack_seq is set to the sequence number of the last byte of |
| 87 | * header and data. see rds_tcp_is_acked(). |
| 88 | */ |
| 89 | tc->t_last_sent_nxt = rds_tcp_snd_nxt(tc); |
| 90 | rm->m_ack_seq = tc->t_last_sent_nxt + |
| 91 | sizeof(struct rds_header) + |
| 92 | be32_to_cpu(rm->m_inc.i_hdr.h_len) - 1; |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 93 | smp_mb__before_atomic(); |
Andy Grover | 7004108 | 2009-08-21 12:28:31 +0000 | [diff] [blame] | 94 | set_bit(RDS_MSG_HAS_ACK_SEQ, &rm->m_flags); |
| 95 | tc->t_last_expected_una = rm->m_ack_seq + 1; |
| 96 | |
Sowmini Varadhan | 315ca6d | 2016-11-16 13:29:48 -0800 | [diff] [blame] | 97 | if (test_bit(RDS_MSG_RETRANSMITTED, &rm->m_flags)) |
| 98 | rm->m_inc.i_hdr.h_flags |= RDS_FLAG_RETRANSMITTED; |
| 99 | |
Andy Grover | 7004108 | 2009-08-21 12:28:31 +0000 | [diff] [blame] | 100 | rdsdebug("rm %p tcp nxt %u ack_seq %llu\n", |
| 101 | rm, rds_tcp_snd_nxt(tc), |
| 102 | (unsigned long long)rm->m_ack_seq); |
| 103 | } |
| 104 | |
| 105 | if (hdr_off < sizeof(struct rds_header)) { |
| 106 | /* see rds_tcp_write_space() */ |
| 107 | set_bit(SOCK_NOSPACE, &tc->t_sock->sk->sk_socket->flags); |
| 108 | |
| 109 | ret = rds_tcp_sendmsg(tc->t_sock, |
| 110 | (void *)&rm->m_inc.i_hdr + hdr_off, |
| 111 | sizeof(rm->m_inc.i_hdr) - hdr_off); |
| 112 | if (ret < 0) |
| 113 | goto out; |
| 114 | done += ret; |
| 115 | if (hdr_off + done != sizeof(struct rds_header)) |
| 116 | goto out; |
| 117 | } |
| 118 | |
Sowmini Varadhan | 76b29ef | 2015-09-30 16:54:09 -0400 | [diff] [blame] | 119 | more = rm->data.op_nents > 1 ? (MSG_MORE | MSG_SENDPAGE_NOTLAST) : 0; |
Andy Grover | 6c7cc6e | 2010-01-27 18:04:18 -0800 | [diff] [blame] | 120 | while (sg < rm->data.op_nents) { |
Sowmini Varadhan | 76b29ef | 2015-09-30 16:54:09 -0400 | [diff] [blame] | 121 | int flags = MSG_DONTWAIT | MSG_NOSIGNAL | more; |
| 122 | |
Andy Grover | 7004108 | 2009-08-21 12:28:31 +0000 | [diff] [blame] | 123 | ret = tc->t_sock->ops->sendpage(tc->t_sock, |
Andy Grover | 6c7cc6e | 2010-01-27 18:04:18 -0800 | [diff] [blame] | 124 | sg_page(&rm->data.op_sg[sg]), |
| 125 | rm->data.op_sg[sg].offset + off, |
| 126 | rm->data.op_sg[sg].length - off, |
Sowmini Varadhan | 76b29ef | 2015-09-30 16:54:09 -0400 | [diff] [blame] | 127 | flags); |
Andy Grover | 6c7cc6e | 2010-01-27 18:04:18 -0800 | [diff] [blame] | 128 | rdsdebug("tcp sendpage %p:%u:%u ret %d\n", (void *)sg_page(&rm->data.op_sg[sg]), |
| 129 | rm->data.op_sg[sg].offset + off, rm->data.op_sg[sg].length - off, |
Andy Grover | 7004108 | 2009-08-21 12:28:31 +0000 | [diff] [blame] | 130 | ret); |
| 131 | if (ret <= 0) |
| 132 | break; |
| 133 | |
| 134 | off += ret; |
| 135 | done += ret; |
Andy Grover | 6c7cc6e | 2010-01-27 18:04:18 -0800 | [diff] [blame] | 136 | if (off == rm->data.op_sg[sg].length) { |
Andy Grover | 7004108 | 2009-08-21 12:28:31 +0000 | [diff] [blame] | 137 | off = 0; |
| 138 | sg++; |
| 139 | } |
Sowmini Varadhan | 76b29ef | 2015-09-30 16:54:09 -0400 | [diff] [blame] | 140 | if (sg == rm->data.op_nents - 1) |
| 141 | more = 0; |
Andy Grover | 7004108 | 2009-08-21 12:28:31 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | out: |
| 145 | if (ret <= 0) { |
| 146 | /* write_space will hit after EAGAIN, all else fatal */ |
| 147 | if (ret == -EAGAIN) { |
| 148 | rds_tcp_stats_inc(s_tcp_sndbuf_full); |
| 149 | ret = 0; |
| 150 | } else { |
Sowmini Varadhan | 5916e2c | 2016-07-14 03:51:03 -0700 | [diff] [blame] | 151 | /* No need to disconnect/reconnect if path_drop |
| 152 | * has already been triggered, because, e.g., of |
| 153 | * an incoming RST. |
| 154 | */ |
| 155 | if (rds_conn_path_up(cp)) { |
| 156 | pr_warn("RDS/tcp: send to %pI4 on cp [%d]" |
| 157 | "returned %d, " |
| 158 | "disconnecting and reconnecting\n", |
| 159 | &conn->c_faddr, cp->cp_index, ret); |
| 160 | rds_conn_path_drop(cp); |
| 161 | } |
Andy Grover | 7004108 | 2009-08-21 12:28:31 +0000 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | if (done == 0) |
| 165 | done = ret; |
| 166 | return done; |
| 167 | } |
| 168 | |
| 169 | /* |
| 170 | * rm->m_ack_seq is set to the tcp sequence number that corresponds to the |
| 171 | * last byte of the message, including the header. This means that the |
| 172 | * entire message has been received if rm->m_ack_seq is "before" the next |
| 173 | * unacked byte of the TCP sequence space. We have to do very careful |
| 174 | * wrapping 32bit comparisons here. |
| 175 | */ |
| 176 | static int rds_tcp_is_acked(struct rds_message *rm, uint64_t ack) |
| 177 | { |
| 178 | if (!test_bit(RDS_MSG_HAS_ACK_SEQ, &rm->m_flags)) |
| 179 | return 0; |
| 180 | return (__s32)((u32)rm->m_ack_seq - (u32)ack) < 0; |
| 181 | } |
| 182 | |
| 183 | void rds_tcp_write_space(struct sock *sk) |
| 184 | { |
| 185 | void (*write_space)(struct sock *sk); |
Sowmini Varadhan | ea3b1ea | 2016-06-30 16:11:14 -0700 | [diff] [blame] | 186 | struct rds_conn_path *cp; |
Andy Grover | 7004108 | 2009-08-21 12:28:31 +0000 | [diff] [blame] | 187 | struct rds_tcp_connection *tc; |
| 188 | |
Eric Dumazet | 3803662 | 2016-05-17 17:44:08 -0700 | [diff] [blame] | 189 | read_lock_bh(&sk->sk_callback_lock); |
Sowmini Varadhan | ea3b1ea | 2016-06-30 16:11:14 -0700 | [diff] [blame] | 190 | cp = sk->sk_user_data; |
| 191 | if (!cp) { |
Andy Grover | 7004108 | 2009-08-21 12:28:31 +0000 | [diff] [blame] | 192 | write_space = sk->sk_write_space; |
| 193 | goto out; |
| 194 | } |
| 195 | |
Sowmini Varadhan | ea3b1ea | 2016-06-30 16:11:14 -0700 | [diff] [blame] | 196 | tc = cp->cp_transport_data; |
Andy Grover | 7004108 | 2009-08-21 12:28:31 +0000 | [diff] [blame] | 197 | rdsdebug("write_space for tc %p\n", tc); |
| 198 | write_space = tc->t_orig_write_space; |
| 199 | rds_tcp_stats_inc(s_tcp_write_space_calls); |
| 200 | |
| 201 | rdsdebug("tcp una %u\n", rds_tcp_snd_una(tc)); |
| 202 | tc->t_last_seen_una = rds_tcp_snd_una(tc); |
Sowmini Varadhan | ea3b1ea | 2016-06-30 16:11:14 -0700 | [diff] [blame] | 203 | rds_send_path_drop_acked(cp, rds_tcp_snd_una(tc), rds_tcp_is_acked); |
Andy Grover | 7004108 | 2009-08-21 12:28:31 +0000 | [diff] [blame] | 204 | |
Joshua Houghton | 5c3da57 | 2016-06-18 15:46:31 +0000 | [diff] [blame] | 205 | if ((atomic_read(&sk->sk_wmem_alloc) << 1) <= sk->sk_sndbuf) |
Sowmini Varadhan | ea3b1ea | 2016-06-30 16:11:14 -0700 | [diff] [blame] | 206 | queue_delayed_work(rds_wq, &cp->cp_send_w, 0); |
Andy Grover | 8e82376 | 2010-03-11 13:49:58 +0000 | [diff] [blame] | 207 | |
Andy Grover | 7004108 | 2009-08-21 12:28:31 +0000 | [diff] [blame] | 208 | out: |
Eric Dumazet | 3803662 | 2016-05-17 17:44:08 -0700 | [diff] [blame] | 209 | read_unlock_bh(&sk->sk_callback_lock); |
Andy Grover | 7004108 | 2009-08-21 12:28:31 +0000 | [diff] [blame] | 210 | |
| 211 | /* |
| 212 | * write_space is only called when data leaves tcp's send queue if |
| 213 | * SOCK_NOSPACE is set. We set SOCK_NOSPACE every time we put |
| 214 | * data in tcp's send queue because we use write_space to parse the |
| 215 | * sequence numbers and notice that rds messages have been fully |
| 216 | * received. |
| 217 | * |
| 218 | * tcp's write_space clears SOCK_NOSPACE if the send queue has more |
| 219 | * than a certain amount of space. So we need to set it again *after* |
| 220 | * we call tcp's write_space or else we might only get called on the |
| 221 | * first of a series of incoming tcp acks. |
| 222 | */ |
| 223 | write_space(sk); |
| 224 | |
| 225 | if (sk->sk_socket) |
| 226 | set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); |
| 227 | } |