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