blob: 5f28131eb37e35c4f858566e9cd9c6dbf24ff0d9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
5 *
6 * Implementation of the Transmission Control Protocol(TCP).
7 *
Jesper Juhl02c30a82005-05-05 16:16:16 -07008 * Authors: Ross Biro
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
10 * Mark Evans, <evansmp@uhura.aston.ac.uk>
11 * Corey Minyard <wf-rch!minyard@relay.EU.net>
12 * Florian La Roche, <flla@stud.uni-sb.de>
13 * Charles Hedrick, <hedrick@klinzhai.rutgers.edu>
14 * Linus Torvalds, <torvalds@cs.helsinki.fi>
15 * Alan Cox, <gw4pts@gw4pts.ampr.org>
16 * Matthew Dillon, <dillon@apollo.west.oic.com>
17 * Arnt Gulbrandsen, <agulbra@nvg.unit.no>
18 * Jorge Cwik, <jorge@laser.satlink.net>
19 */
20
21/*
22 * Changes: Pedro Roque : Retransmit queue handled by TCP.
23 * : Fragmentation on mtu decrease
24 * : Segment collapse on retransmit
25 * : AF independence
26 *
27 * Linus Torvalds : send_delayed_ack
28 * David S. Miller : Charge memory using the right skb
29 * during syn/ack processing.
30 * David S. Miller : Output engine completely rewritten.
31 * Andrea Arcangeli: SYNACK carry ts_recent in tsecr.
32 * Cacophonix Gaul : draft-minshall-nagle-01
33 * J Hadi Salim : ECN support
34 *
35 */
36
Joe Perches91df42b2012-05-15 14:11:54 +000037#define pr_fmt(fmt) "TCP: " fmt
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <net/tcp.h>
40
41#include <linux/compiler.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45/* People can turn this off for buggy TCP's found in printers etc. */
Brian Haleyab32ea52006-09-22 14:15:41 -070046int sysctl_tcp_retrans_collapse __read_mostly = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Jianjun Kong09cb1052008-11-03 00:27:11 -080048/* People can turn this on to work with those rare, broken TCPs that
Rick Jones15d99e02006-03-20 22:40:29 -080049 * interpret the window field as a signed quantity.
50 */
Brian Haleyab32ea52006-09-22 14:15:41 -070051int sysctl_tcp_workaround_signed_windows __read_mostly = 0;
Rick Jones15d99e02006-03-20 22:40:29 -080052
Eric Dumazet46d3cea2012-07-11 05:50:31 +000053/* Default TSQ limit of two TSO segments */
54int sysctl_tcp_limit_output_bytes __read_mostly = 131072;
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056/* This limits the percentage of the congestion window which we
57 * will allow a single TSO frame to consume. Building TSO frames
58 * which are too large can cause TCP streams to be bursty.
59 */
Brian Haleyab32ea52006-09-22 14:15:41 -070060int sysctl_tcp_tso_win_divisor __read_mostly = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Brian Haleyab32ea52006-09-22 14:15:41 -070062int sysctl_tcp_mtu_probing __read_mostly = 0;
Shan Wei97b1ce252010-12-01 18:04:50 +000063int sysctl_tcp_base_mss __read_mostly = TCP_BASE_MSS;
John Heffner5d424d52006-03-20 17:53:41 -080064
David S. Miller35089bb2006-06-13 22:33:04 -070065/* By default, RFC2861 behavior. */
Brian Haleyab32ea52006-09-22 14:15:41 -070066int sysctl_tcp_slow_start_after_idle __read_mostly = 1;
David S. Miller35089bb2006-06-13 22:33:04 -070067
Eric Dumazet46d3cea2012-07-11 05:50:31 +000068static bool tcp_write_xmit(struct sock *sk, unsigned int mss_now, int nonagle,
69 int push_one, gfp_t gfp);
William Allen Simpson519855c2009-12-02 18:14:19 +000070
Andi Kleen67edfef2009-07-21 23:00:40 +000071/* Account for new data that has been sent to the network. */
Eric Dumazetcf533ea2011-10-21 05:22:42 -040072static void tcp_event_new_data_sent(struct sock *sk, const struct sk_buff *skb)
Ilpo Järvinen6ff03ac2007-08-24 22:44:06 -070073{
Nandita Dukkipati6ba8a3b2013-03-11 10:00:43 +000074 struct inet_connection_sock *icsk = inet_csk(sk);
Ilpo Järvinen6ff03ac2007-08-24 22:44:06 -070075 struct tcp_sock *tp = tcp_sk(sk);
Ilpo Järvinen66f5fe62007-12-31 04:43:57 -080076 unsigned int prior_packets = tp->packets_out;
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -070077
David S. Millerfe067e82007-03-07 12:12:44 -080078 tcp_advance_send_head(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 tp->snd_nxt = TCP_SKB_CB(skb)->end_seq;
Ilpo Järvinen85124302007-11-26 20:17:38 +080080
Ilpo Järvinen66f5fe62007-12-31 04:43:57 -080081 tp->packets_out += tcp_skb_pcount(skb);
Nandita Dukkipati6ba8a3b2013-03-11 10:00:43 +000082 if (!prior_packets || icsk->icsk_pending == ICSK_TIME_EARLY_RETRANS ||
83 icsk->icsk_pending == ICSK_TIME_LOSS_PROBE)
Yuchung Cheng750ea2b2012-05-02 13:30:04 +000084 tcp_rearm_rto(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
87/* SND.NXT, if window was not shrunk.
88 * If window has been shrunk, what should we make? It is not clear at all.
89 * Using SND.UNA we will fail to open window, SND.NXT is out of window. :-(
90 * Anything in between SND.UNA...SND.UNA+SND.WND also can be already
91 * invalid. OK, let's make this for now:
92 */
Eric Dumazetcf533ea2011-10-21 05:22:42 -040093static inline __u32 tcp_acceptable_seq(const struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Eric Dumazetcf533ea2011-10-21 05:22:42 -040095 const struct tcp_sock *tp = tcp_sk(sk);
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -070096
Ilpo Järvinen90840de2007-12-31 04:48:41 -080097 if (!before(tcp_wnd_end(tp), tp->snd_nxt))
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 return tp->snd_nxt;
99 else
Ilpo Järvinen90840de2007-12-31 04:48:41 -0800100 return tcp_wnd_end(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
102
103/* Calculate mss to advertise in SYN segment.
104 * RFC1122, RFC1063, draft-ietf-tcpimpl-pmtud-01 state that:
105 *
106 * 1. It is independent of path mtu.
107 * 2. Ideally, it is maximal possible segment size i.e. 65535-40.
108 * 3. For IPv4 it is reasonable to calculate it from maximal MTU of
109 * attached devices, because some buggy hosts are confused by
110 * large MSS.
111 * 4. We do not make 3, we advertise MSS, calculated from first
112 * hop device mtu, but allow to raise it to ip_rt_min_advmss.
113 * This may be overridden via information stored in routing table.
114 * 5. Value 65535 for MSS is valid in IPv6 and means "as large as possible,
115 * probably even Jumbo".
116 */
117static __u16 tcp_advertise_mss(struct sock *sk)
118{
119 struct tcp_sock *tp = tcp_sk(sk);
Eric Dumazetcf533ea2011-10-21 05:22:42 -0400120 const struct dst_entry *dst = __sk_dst_get(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 int mss = tp->advmss;
122
David S. Miller0dbaee32010-12-13 12:52:14 -0800123 if (dst) {
124 unsigned int metric = dst_metric_advmss(dst);
125
126 if (metric < mss) {
127 mss = metric;
128 tp->advmss = mss;
129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 }
131
132 return (__u16)mss;
133}
134
135/* RFC2861. Reset CWND after idle period longer RTO to "restart window".
136 * This is the first part of cwnd validation mechanism. */
Eric Dumazetcf533ea2011-10-21 05:22:42 -0400137static void tcp_cwnd_restart(struct sock *sk, const struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700139 struct tcp_sock *tp = tcp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 s32 delta = tcp_time_stamp - tp->lsndtime;
141 u32 restart_cwnd = tcp_init_cwnd(tp, dst);
142 u32 cwnd = tp->snd_cwnd;
143
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300144 tcp_ca_event(sk, CA_EVENT_CWND_RESTART);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300146 tp->snd_ssthresh = tcp_current_ssthresh(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 restart_cwnd = min(restart_cwnd, cwnd);
148
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700149 while ((delta -= inet_csk(sk)->icsk_rto) > 0 && cwnd > restart_cwnd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 cwnd >>= 1;
151 tp->snd_cwnd = max(cwnd, restart_cwnd);
152 tp->snd_cwnd_stamp = tcp_time_stamp;
153 tp->snd_cwnd_used = 0;
154}
155
Andi Kleen67edfef2009-07-21 23:00:40 +0000156/* Congestion state accounting after a packet has been sent. */
Stephen Hemminger40efc6f2006-01-03 16:03:49 -0800157static void tcp_event_data_sent(struct tcp_sock *tp,
Eric Dumazetcf533ea2011-10-21 05:22:42 -0400158 struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700160 struct inet_connection_sock *icsk = inet_csk(sk);
161 const u32 now = tcp_time_stamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
David S. Miller35089bb2006-06-13 22:33:04 -0700163 if (sysctl_tcp_slow_start_after_idle &&
164 (!tp->packets_out && (s32)(now - tp->lsndtime) > icsk->icsk_rto))
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700165 tcp_cwnd_restart(sk, __sk_dst_get(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 tp->lsndtime = now;
168
169 /* If it is a reply for ato after last received
170 * packet, enter pingpong mode.
171 */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700172 if ((u32)(now - icsk->icsk_ack.lrcvtime) < icsk->icsk_ack.ato)
173 icsk->icsk_ack.pingpong = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
175
Andi Kleen67edfef2009-07-21 23:00:40 +0000176/* Account for an ACK we sent. */
Stephen Hemminger40efc6f2006-01-03 16:03:49 -0800177static inline void tcp_event_ack_sent(struct sock *sk, unsigned int pkts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700179 tcp_dec_quickack_mode(sk, pkts);
180 inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182
183/* Determine a window scaling and initial window to offer.
184 * Based on the assumption that the given amount of space
185 * will be offered. Store the results in the tp structure.
186 * NOTE: for smooth operation initial space offering should
187 * be a multiple of mss if possible. We assume here that mss >= 1.
188 * This MUST be enforced by all callers.
189 */
190void tcp_select_initial_window(int __space, __u32 mss,
191 __u32 *rcv_wnd, __u32 *window_clamp,
laurent chavey31d12922009-12-15 11:15:28 +0000192 int wscale_ok, __u8 *rcv_wscale,
193 __u32 init_rcv_wnd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
195 unsigned int space = (__space < 0 ? 0 : __space);
196
197 /* If no clamp set the clamp to the max possible scaled window */
198 if (*window_clamp == 0)
199 (*window_clamp) = (65535 << 14);
200 space = min(*window_clamp, space);
201
202 /* Quantize space offering to a multiple of mss if possible. */
203 if (space > mss)
204 space = (space / mss) * mss;
205
206 /* NOTE: offering an initial window larger than 32767
Rick Jones15d99e02006-03-20 22:40:29 -0800207 * will break some buggy TCP stacks. If the admin tells us
208 * it is likely we could be speaking with such a buggy stack
209 * we will truncate our initial window offering to 32K-1
210 * unless the remote has sent us a window scaling option,
211 * which we interpret as a sign the remote TCP is not
212 * misinterpreting the window field as a signed quantity.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 */
Rick Jones15d99e02006-03-20 22:40:29 -0800214 if (sysctl_tcp_workaround_signed_windows)
215 (*rcv_wnd) = min(space, MAX_TCP_WINDOW);
216 else
217 (*rcv_wnd) = space;
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 (*rcv_wscale) = 0;
220 if (wscale_ok) {
221 /* Set window scaling on max possible window
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900222 * See RFC1323 for an explanation of the limit to 14
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 */
224 space = max_t(u32, sysctl_tcp_rmem[2], sysctl_rmem_max);
Stephen Hemminger316c1592006-08-22 00:06:11 -0700225 space = min_t(u32, space, *window_clamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 while (space > 65535 && (*rcv_wscale) < 14) {
227 space >>= 1;
228 (*rcv_wscale)++;
229 }
230 }
231
Nandita Dukkipati356f0392010-12-20 14:15:56 +0000232 /* Set initial window to a value enough for senders starting with
233 * initial congestion window of TCP_DEFAULT_INIT_RCVWND. Place
234 * a limit on the initial window when mss is larger than 1460.
235 */
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800236 if (mss > (1 << *rcv_wscale)) {
Nandita Dukkipati356f0392010-12-20 14:15:56 +0000237 int init_cwnd = TCP_DEFAULT_INIT_RCVWND;
238 if (mss > 1460)
239 init_cwnd =
240 max_t(u32, (1460 * TCP_DEFAULT_INIT_RCVWND) / mss, 2);
laurent chavey31d12922009-12-15 11:15:28 +0000241 /* when initializing use the value from init_rcv_wnd
242 * rather than the default from above
243 */
Nandita Dukkipatib1afde62010-12-03 13:33:44 +0000244 if (init_rcv_wnd)
245 *rcv_wnd = min(*rcv_wnd, init_rcv_wnd * mss);
246 else
247 *rcv_wnd = min(*rcv_wnd, init_cwnd * mss);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
249
250 /* Set the clamp no higher than max representable value */
251 (*window_clamp) = min(65535U << (*rcv_wscale), *window_clamp);
252}
Eric Dumazet4bc2f182010-07-09 21:22:10 +0000253EXPORT_SYMBOL(tcp_select_initial_window);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255/* Chose a new window to advertise, update state in tcp_sock for the
256 * socket, and return result with RFC1323 scaling applied. The return
257 * value can be stuffed directly into th->window for an outgoing
258 * frame.
259 */
Stephen Hemminger40efc6f2006-01-03 16:03:49 -0800260static u16 tcp_select_window(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
262 struct tcp_sock *tp = tcp_sk(sk);
263 u32 cur_win = tcp_receive_window(tp);
264 u32 new_win = __tcp_select_window(sk);
265
266 /* Never shrink the offered window */
Stephen Hemminger2de979b2007-03-08 20:45:19 -0800267 if (new_win < cur_win) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 /* Danger Will Robinson!
269 * Don't update rcv_wup/rcv_wnd here or else
270 * we will not be able to advertise a zero
271 * window in time. --DaveM
272 *
273 * Relax Will Robinson.
274 */
Patrick McHardy607bfbf2008-03-20 16:11:27 -0700275 new_win = ALIGN(cur_win, 1 << tp->rx_opt.rcv_wscale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 }
277 tp->rcv_wnd = new_win;
278 tp->rcv_wup = tp->rcv_nxt;
279
280 /* Make sure we do not exceed the maximum possible
281 * scaled window.
282 */
Rick Jones15d99e02006-03-20 22:40:29 -0800283 if (!tp->rx_opt.rcv_wscale && sysctl_tcp_workaround_signed_windows)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 new_win = min(new_win, MAX_TCP_WINDOW);
285 else
286 new_win = min(new_win, (65535U << tp->rx_opt.rcv_wscale));
287
288 /* RFC1323 scaling applied */
289 new_win >>= tp->rx_opt.rcv_wscale;
290
291 /* If we advertise zero window, disable fast path. */
292 if (new_win == 0)
293 tp->pred_flags = 0;
294
295 return new_win;
296}
297
Andi Kleen67edfef2009-07-21 23:00:40 +0000298/* Packet ECN state for a SYN-ACK */
Eric Dumazetcf533ea2011-10-21 05:22:42 -0400299static inline void TCP_ECN_send_synack(const struct tcp_sock *tp, struct sk_buff *skb)
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700300{
Eric Dumazet4de075e2011-09-27 13:25:05 -0400301 TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_CWR;
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800302 if (!(tp->ecn_flags & TCP_ECN_OK))
Eric Dumazet4de075e2011-09-27 13:25:05 -0400303 TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_ECE;
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700304}
305
Andi Kleen67edfef2009-07-21 23:00:40 +0000306/* Packet ECN state for a SYN. */
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700307static inline void TCP_ECN_send_syn(struct sock *sk, struct sk_buff *skb)
308{
309 struct tcp_sock *tp = tcp_sk(sk);
310
311 tp->ecn_flags = 0;
Hannes Frederic Sowa5d134f12013-01-05 16:10:48 +0000312 if (sock_net(sk)->ipv4.sysctl_tcp_ecn == 1) {
Eric Dumazet4de075e2011-09-27 13:25:05 -0400313 TCP_SKB_CB(skb)->tcp_flags |= TCPHDR_ECE | TCPHDR_CWR;
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700314 tp->ecn_flags = TCP_ECN_OK;
315 }
316}
317
318static __inline__ void
Eric Dumazetcf533ea2011-10-21 05:22:42 -0400319TCP_ECN_make_synack(const struct request_sock *req, struct tcphdr *th)
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700320{
321 if (inet_rsk(req)->ecn_ok)
322 th->ece = 1;
323}
324
Andi Kleen67edfef2009-07-21 23:00:40 +0000325/* Set up ECN state for a packet on a ESTABLISHED socket that is about to
326 * be sent.
327 */
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700328static inline void TCP_ECN_send(struct sock *sk, struct sk_buff *skb,
329 int tcp_header_len)
330{
331 struct tcp_sock *tp = tcp_sk(sk);
332
333 if (tp->ecn_flags & TCP_ECN_OK) {
334 /* Not-retransmitted data segment: set ECT and inject CWR. */
335 if (skb->len != tcp_header_len &&
336 !before(TCP_SKB_CB(skb)->seq, tp->snd_nxt)) {
337 INET_ECN_xmit(sk);
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800338 if (tp->ecn_flags & TCP_ECN_QUEUE_CWR) {
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700339 tp->ecn_flags &= ~TCP_ECN_QUEUE_CWR;
340 tcp_hdr(skb)->cwr = 1;
341 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
342 }
343 } else {
344 /* ACK or retransmitted segment: clear ECT|CE */
345 INET_ECN_dontxmit(sk);
346 }
347 if (tp->ecn_flags & TCP_ECN_DEMAND_CWR)
348 tcp_hdr(skb)->ece = 1;
349 }
350}
351
Ilpo Järvinene870a8e2008-01-03 20:39:01 -0800352/* Constructs common control bits of non-data skb. If SYN/FIN is present,
353 * auto increment end seqno.
354 */
355static void tcp_init_nondata_skb(struct sk_buff *skb, u32 seq, u8 flags)
356{
David S. Miller2e8e18e2010-04-08 11:32:30 -0700357 skb->ip_summed = CHECKSUM_PARTIAL;
Ilpo Järvinene870a8e2008-01-03 20:39:01 -0800358 skb->csum = 0;
359
Eric Dumazet4de075e2011-09-27 13:25:05 -0400360 TCP_SKB_CB(skb)->tcp_flags = flags;
Ilpo Järvinene870a8e2008-01-03 20:39:01 -0800361 TCP_SKB_CB(skb)->sacked = 0;
362
363 skb_shinfo(skb)->gso_segs = 1;
364 skb_shinfo(skb)->gso_size = 0;
365 skb_shinfo(skb)->gso_type = 0;
366
367 TCP_SKB_CB(skb)->seq = seq;
Changli Gaoa3433f32010-06-12 14:01:43 +0000368 if (flags & (TCPHDR_SYN | TCPHDR_FIN))
Ilpo Järvinene870a8e2008-01-03 20:39:01 -0800369 seq++;
370 TCP_SKB_CB(skb)->end_seq = seq;
371}
372
Eric Dumazeta2a385d2012-05-16 23:15:34 +0000373static inline bool tcp_urg_mode(const struct tcp_sock *tp)
Ilpo Järvinen33f5f572008-10-07 14:43:06 -0700374{
375 return tp->snd_una != tp->snd_up;
376}
377
Adam Langley33ad7982008-07-19 00:04:31 -0700378#define OPTION_SACK_ADVERTISE (1 << 0)
379#define OPTION_TS (1 << 1)
380#define OPTION_MD5 (1 << 2)
Ori Finkelman89e95a62009-10-01 06:41:59 +0000381#define OPTION_WSCALE (1 << 3)
Yuchung Cheng2100c8d2012-07-19 06:43:05 +0000382#define OPTION_FAST_OPEN_COOKIE (1 << 8)
Adam Langley33ad7982008-07-19 00:04:31 -0700383
384struct tcp_out_options {
Yuchung Cheng2100c8d2012-07-19 06:43:05 +0000385 u16 options; /* bit field of OPTION_* */
386 u16 mss; /* 0 to disable */
Adam Langley33ad7982008-07-19 00:04:31 -0700387 u8 ws; /* window scale, 0 to disable */
388 u8 num_sack_blocks; /* number of SACK blocks to include */
William Allen Simpsonbd0388a2009-12-02 18:23:05 +0000389 u8 hash_size; /* bytes in hash_location */
William Allen Simpsonbd0388a2009-12-02 18:23:05 +0000390 __u8 *hash_location; /* temporary pointer, overloaded */
Yuchung Cheng2100c8d2012-07-19 06:43:05 +0000391 __u32 tsval, tsecr; /* need to include OPTION_TS */
392 struct tcp_fastopen_cookie *fastopen_cookie; /* Fast open cookie */
Adam Langley33ad7982008-07-19 00:04:31 -0700393};
394
Andi Kleen67edfef2009-07-21 23:00:40 +0000395/* Write previously computed TCP options to the packet.
396 *
397 * Beware: Something in the Internet is very sensitive to the ordering of
Ilpo Järvinenfd6149d2008-10-23 14:06:35 -0700398 * TCP options, we learned this through the hard way, so be careful here.
399 * Luckily we can at least blame others for their non-compliance but from
400 * inter-operatibility perspective it seems that we're somewhat stuck with
401 * the ordering which we have been using if we want to keep working with
402 * those broken things (not that it currently hurts anybody as there isn't
403 * particular reason why the ordering would need to be changed).
404 *
405 * At least SACK_PERM as the first option is known to lead to a disaster
406 * (but it may well be that other scenarios fail similarly).
407 */
Adam Langley33ad7982008-07-19 00:04:31 -0700408static void tcp_options_write(__be32 *ptr, struct tcp_sock *tp,
William Allen Simpsonbd0388a2009-12-02 18:23:05 +0000409 struct tcp_out_options *opts)
410{
Yuchung Cheng2100c8d2012-07-19 06:43:05 +0000411 u16 options = opts->options; /* mungable copy */
William Allen Simpsonbd0388a2009-12-02 18:23:05 +0000412
William Allen Simpsonbd0388a2009-12-02 18:23:05 +0000413 if (unlikely(OPTION_MD5 & options)) {
Christoph Paasch1a2c6182013-03-17 08:23:34 +0000414 *ptr++ = htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
415 (TCPOPT_MD5SIG << 8) | TCPOLEN_MD5SIG);
William Allen Simpsonbd0388a2009-12-02 18:23:05 +0000416 /* overload cookie hash location */
417 opts->hash_location = (__u8 *)ptr;
Adam Langley33ad7982008-07-19 00:04:31 -0700418 ptr += 4;
Stephen Hemminger40efc6f2006-01-03 16:03:49 -0800419 }
Adam Langley33ad7982008-07-19 00:04:31 -0700420
Ilpo Järvinenfd6149d2008-10-23 14:06:35 -0700421 if (unlikely(opts->mss)) {
422 *ptr++ = htonl((TCPOPT_MSS << 24) |
423 (TCPOLEN_MSS << 16) |
424 opts->mss);
425 }
426
William Allen Simpsonbd0388a2009-12-02 18:23:05 +0000427 if (likely(OPTION_TS & options)) {
428 if (unlikely(OPTION_SACK_ADVERTISE & options)) {
Adam Langley33ad7982008-07-19 00:04:31 -0700429 *ptr++ = htonl((TCPOPT_SACK_PERM << 24) |
430 (TCPOLEN_SACK_PERM << 16) |
431 (TCPOPT_TIMESTAMP << 8) |
432 TCPOLEN_TIMESTAMP);
William Allen Simpsonbd0388a2009-12-02 18:23:05 +0000433 options &= ~OPTION_SACK_ADVERTISE;
Adam Langley33ad7982008-07-19 00:04:31 -0700434 } else {
435 *ptr++ = htonl((TCPOPT_NOP << 24) |
436 (TCPOPT_NOP << 16) |
437 (TCPOPT_TIMESTAMP << 8) |
438 TCPOLEN_TIMESTAMP);
439 }
440 *ptr++ = htonl(opts->tsval);
441 *ptr++ = htonl(opts->tsecr);
442 }
443
William Allen Simpsonbd0388a2009-12-02 18:23:05 +0000444 if (unlikely(OPTION_SACK_ADVERTISE & options)) {
Adam Langley33ad7982008-07-19 00:04:31 -0700445 *ptr++ = htonl((TCPOPT_NOP << 24) |
446 (TCPOPT_NOP << 16) |
447 (TCPOPT_SACK_PERM << 8) |
448 TCPOLEN_SACK_PERM);
449 }
450
William Allen Simpsonbd0388a2009-12-02 18:23:05 +0000451 if (unlikely(OPTION_WSCALE & options)) {
Adam Langley33ad7982008-07-19 00:04:31 -0700452 *ptr++ = htonl((TCPOPT_NOP << 24) |
453 (TCPOPT_WINDOW << 16) |
454 (TCPOLEN_WINDOW << 8) |
455 opts->ws);
456 }
457
458 if (unlikely(opts->num_sack_blocks)) {
459 struct tcp_sack_block *sp = tp->rx_opt.dsack ?
460 tp->duplicate_sack : tp->selective_acks;
Stephen Hemminger40efc6f2006-01-03 16:03:49 -0800461 int this_sack;
462
463 *ptr++ = htonl((TCPOPT_NOP << 24) |
464 (TCPOPT_NOP << 16) |
465 (TCPOPT_SACK << 8) |
Adam Langley33ad7982008-07-19 00:04:31 -0700466 (TCPOLEN_SACK_BASE + (opts->num_sack_blocks *
Stephen Hemminger40efc6f2006-01-03 16:03:49 -0800467 TCPOLEN_SACK_PERBLOCK)));
Stephen Hemminger2de979b2007-03-08 20:45:19 -0800468
Adam Langley33ad7982008-07-19 00:04:31 -0700469 for (this_sack = 0; this_sack < opts->num_sack_blocks;
470 ++this_sack) {
Stephen Hemminger40efc6f2006-01-03 16:03:49 -0800471 *ptr++ = htonl(sp[this_sack].start_seq);
472 *ptr++ = htonl(sp[this_sack].end_seq);
473 }
Stephen Hemminger2de979b2007-03-08 20:45:19 -0800474
Ilpo Järvinen5861f8e2009-03-14 14:23:01 +0000475 tp->rx_opt.dsack = 0;
Stephen Hemminger40efc6f2006-01-03 16:03:49 -0800476 }
Yuchung Cheng2100c8d2012-07-19 06:43:05 +0000477
478 if (unlikely(OPTION_FAST_OPEN_COOKIE & options)) {
479 struct tcp_fastopen_cookie *foc = opts->fastopen_cookie;
480
481 *ptr++ = htonl((TCPOPT_EXP << 24) |
482 ((TCPOLEN_EXP_FASTOPEN_BASE + foc->len) << 16) |
483 TCPOPT_FASTOPEN_MAGIC);
484
485 memcpy(ptr, foc->val, foc->len);
486 if ((foc->len & 3) == 2) {
487 u8 *align = ((u8 *)ptr) + foc->len;
488 align[0] = align[1] = TCPOPT_NOP;
489 }
490 ptr += (foc->len + 3) >> 2;
491 }
Stephen Hemminger40efc6f2006-01-03 16:03:49 -0800492}
493
Andi Kleen67edfef2009-07-21 23:00:40 +0000494/* Compute TCP options for SYN packets. This is not the final
495 * network wire format yet.
496 */
Eric Dumazet95c96172012-04-15 05:58:06 +0000497static unsigned int tcp_syn_options(struct sock *sk, struct sk_buff *skb,
Adam Langley33ad7982008-07-19 00:04:31 -0700498 struct tcp_out_options *opts,
Eric Dumazetcf533ea2011-10-21 05:22:42 -0400499 struct tcp_md5sig_key **md5)
500{
Adam Langley33ad7982008-07-19 00:04:31 -0700501 struct tcp_sock *tp = tcp_sk(sk);
Eric Dumazet95c96172012-04-15 05:58:06 +0000502 unsigned int remaining = MAX_TCP_OPTION_SPACE;
Yuchung Cheng783237e2012-07-19 06:43:07 +0000503 struct tcp_fastopen_request *fastopen = tp->fastopen_req;
Adam Langley33ad7982008-07-19 00:04:31 -0700504
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800505#ifdef CONFIG_TCP_MD5SIG
Adam Langley33ad7982008-07-19 00:04:31 -0700506 *md5 = tp->af_specific->md5_lookup(sk, sk);
507 if (*md5) {
508 opts->options |= OPTION_MD5;
William Allen Simpsonbd0388a2009-12-02 18:23:05 +0000509 remaining -= TCPOLEN_MD5SIG_ALIGNED;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800510 }
Adam Langley33ad7982008-07-19 00:04:31 -0700511#else
512 *md5 = NULL;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800513#endif
Adam Langley33ad7982008-07-19 00:04:31 -0700514
515 /* We always get an MSS option. The option bytes which will be seen in
516 * normal data packets should timestamps be used, must be in the MSS
517 * advertised. But we subtract them from tp->mss_cache so that
518 * calculations in tcp_sendmsg are simpler etc. So account for this
519 * fact here if necessary. If we don't do this correctly, as a
520 * receiver we won't recognize data packets as being full sized when we
521 * should, and thus we won't abide by the delayed ACK rules correctly.
522 * SACKs don't matter, we never delay an ACK when we have any of those
523 * going out. */
524 opts->mss = tcp_advertise_mss(sk);
William Allen Simpsonbd0388a2009-12-02 18:23:05 +0000525 remaining -= TCPOLEN_MSS_ALIGNED;
Adam Langley33ad7982008-07-19 00:04:31 -0700526
David S. Millerbb5b7c12009-12-15 20:56:42 -0800527 if (likely(sysctl_tcp_timestamps && *md5 == NULL)) {
Adam Langley33ad7982008-07-19 00:04:31 -0700528 opts->options |= OPTION_TS;
Andrey Vaginee684b62013-02-11 05:50:19 +0000529 opts->tsval = TCP_SKB_CB(skb)->when + tp->tsoffset;
Adam Langley33ad7982008-07-19 00:04:31 -0700530 opts->tsecr = tp->rx_opt.ts_recent;
William Allen Simpsonbd0388a2009-12-02 18:23:05 +0000531 remaining -= TCPOLEN_TSTAMP_ALIGNED;
Adam Langley33ad7982008-07-19 00:04:31 -0700532 }
David S. Millerbb5b7c12009-12-15 20:56:42 -0800533 if (likely(sysctl_tcp_window_scaling)) {
Adam Langley33ad7982008-07-19 00:04:31 -0700534 opts->ws = tp->rx_opt.rcv_wscale;
Ori Finkelman89e95a62009-10-01 06:41:59 +0000535 opts->options |= OPTION_WSCALE;
William Allen Simpsonbd0388a2009-12-02 18:23:05 +0000536 remaining -= TCPOLEN_WSCALE_ALIGNED;
Adam Langley33ad7982008-07-19 00:04:31 -0700537 }
David S. Millerbb5b7c12009-12-15 20:56:42 -0800538 if (likely(sysctl_tcp_sack)) {
Adam Langley33ad7982008-07-19 00:04:31 -0700539 opts->options |= OPTION_SACK_ADVERTISE;
David S. Millerb32d1312008-07-21 18:45:34 -0700540 if (unlikely(!(OPTION_TS & opts->options)))
William Allen Simpsonbd0388a2009-12-02 18:23:05 +0000541 remaining -= TCPOLEN_SACKPERM_ALIGNED;
Adam Langley33ad7982008-07-19 00:04:31 -0700542 }
543
Yuchung Cheng783237e2012-07-19 06:43:07 +0000544 if (fastopen && fastopen->cookie.len >= 0) {
545 u32 need = TCPOLEN_EXP_FASTOPEN_BASE + fastopen->cookie.len;
546 need = (need + 3) & ~3U; /* Align to 32 bits */
547 if (remaining >= need) {
548 opts->options |= OPTION_FAST_OPEN_COOKIE;
549 opts->fastopen_cookie = &fastopen->cookie;
550 remaining -= need;
551 tp->syn_fastopen = 1;
552 }
553 }
William Allen Simpsonbd0388a2009-12-02 18:23:05 +0000554
William Allen Simpsonbd0388a2009-12-02 18:23:05 +0000555 return MAX_TCP_OPTION_SPACE - remaining;
Adam Langley33ad7982008-07-19 00:04:31 -0700556}
557
Andi Kleen67edfef2009-07-21 23:00:40 +0000558/* Set up TCP options for SYN-ACKs. */
Eric Dumazet95c96172012-04-15 05:58:06 +0000559static unsigned int tcp_synack_options(struct sock *sk,
Adam Langley33ad7982008-07-19 00:04:31 -0700560 struct request_sock *req,
Eric Dumazet95c96172012-04-15 05:58:06 +0000561 unsigned int mss, struct sk_buff *skb,
Adam Langley33ad7982008-07-19 00:04:31 -0700562 struct tcp_out_options *opts,
William Allen Simpson4957faade2009-12-02 18:25:27 +0000563 struct tcp_md5sig_key **md5,
Jerry Chu83368862012-08-31 12:29:12 +0000564 struct tcp_fastopen_cookie *foc)
William Allen Simpson4957faade2009-12-02 18:25:27 +0000565{
Adam Langley33ad7982008-07-19 00:04:31 -0700566 struct inet_request_sock *ireq = inet_rsk(req);
Eric Dumazet95c96172012-04-15 05:58:06 +0000567 unsigned int remaining = MAX_TCP_OPTION_SPACE;
Adam Langley33ad7982008-07-19 00:04:31 -0700568
569#ifdef CONFIG_TCP_MD5SIG
570 *md5 = tcp_rsk(req)->af_specific->md5_lookup(sk, req);
571 if (*md5) {
572 opts->options |= OPTION_MD5;
William Allen Simpson4957faade2009-12-02 18:25:27 +0000573 remaining -= TCPOLEN_MD5SIG_ALIGNED;
574
575 /* We can't fit any SACK blocks in a packet with MD5 + TS
576 * options. There was discussion about disabling SACK
577 * rather than TS in order to fit in better with old,
578 * buggy kernels, but that was deemed to be unnecessary.
579 */
Eric Dumazetde213e5e2010-05-17 22:35:36 -0700580 ireq->tstamp_ok &= !ireq->sack_ok;
Adam Langley33ad7982008-07-19 00:04:31 -0700581 }
582#else
583 *md5 = NULL;
584#endif
585
William Allen Simpson4957faade2009-12-02 18:25:27 +0000586 /* We always send an MSS option. */
Adam Langley33ad7982008-07-19 00:04:31 -0700587 opts->mss = mss;
William Allen Simpson4957faade2009-12-02 18:25:27 +0000588 remaining -= TCPOLEN_MSS_ALIGNED;
Adam Langley33ad7982008-07-19 00:04:31 -0700589
590 if (likely(ireq->wscale_ok)) {
591 opts->ws = ireq->rcv_wscale;
Ori Finkelman89e95a62009-10-01 06:41:59 +0000592 opts->options |= OPTION_WSCALE;
William Allen Simpson4957faade2009-12-02 18:25:27 +0000593 remaining -= TCPOLEN_WSCALE_ALIGNED;
Adam Langley33ad7982008-07-19 00:04:31 -0700594 }
Eric Dumazetde213e5e2010-05-17 22:35:36 -0700595 if (likely(ireq->tstamp_ok)) {
Adam Langley33ad7982008-07-19 00:04:31 -0700596 opts->options |= OPTION_TS;
597 opts->tsval = TCP_SKB_CB(skb)->when;
598 opts->tsecr = req->ts_recent;
William Allen Simpson4957faade2009-12-02 18:25:27 +0000599 remaining -= TCPOLEN_TSTAMP_ALIGNED;
Adam Langley33ad7982008-07-19 00:04:31 -0700600 }
601 if (likely(ireq->sack_ok)) {
602 opts->options |= OPTION_SACK_ADVERTISE;
Eric Dumazetde213e5e2010-05-17 22:35:36 -0700603 if (unlikely(!ireq->tstamp_ok))
William Allen Simpson4957faade2009-12-02 18:25:27 +0000604 remaining -= TCPOLEN_SACKPERM_ALIGNED;
Adam Langley33ad7982008-07-19 00:04:31 -0700605 }
Jerry Chu83368862012-08-31 12:29:12 +0000606 if (foc != NULL) {
607 u32 need = TCPOLEN_EXP_FASTOPEN_BASE + foc->len;
608 need = (need + 3) & ~3U; /* Align to 32 bits */
609 if (remaining >= need) {
610 opts->options |= OPTION_FAST_OPEN_COOKIE;
611 opts->fastopen_cookie = foc;
612 remaining -= need;
613 }
614 }
William Allen Simpson4957faade2009-12-02 18:25:27 +0000615
William Allen Simpson4957faade2009-12-02 18:25:27 +0000616 return MAX_TCP_OPTION_SPACE - remaining;
Adam Langley33ad7982008-07-19 00:04:31 -0700617}
618
Andi Kleen67edfef2009-07-21 23:00:40 +0000619/* Compute TCP options for ESTABLISHED sockets. This is not the
620 * final wire format yet.
621 */
Eric Dumazet95c96172012-04-15 05:58:06 +0000622static unsigned int tcp_established_options(struct sock *sk, struct sk_buff *skb,
Adam Langley33ad7982008-07-19 00:04:31 -0700623 struct tcp_out_options *opts,
Eric Dumazetcf533ea2011-10-21 05:22:42 -0400624 struct tcp_md5sig_key **md5)
625{
Adam Langley33ad7982008-07-19 00:04:31 -0700626 struct tcp_skb_cb *tcb = skb ? TCP_SKB_CB(skb) : NULL;
627 struct tcp_sock *tp = tcp_sk(sk);
Eric Dumazet95c96172012-04-15 05:58:06 +0000628 unsigned int size = 0;
Ilpo Järvinencabeccb2009-02-28 04:44:38 +0000629 unsigned int eff_sacks;
Adam Langley33ad7982008-07-19 00:04:31 -0700630
631#ifdef CONFIG_TCP_MD5SIG
632 *md5 = tp->af_specific->md5_lookup(sk, sk);
633 if (unlikely(*md5)) {
634 opts->options |= OPTION_MD5;
635 size += TCPOLEN_MD5SIG_ALIGNED;
636 }
637#else
638 *md5 = NULL;
639#endif
640
641 if (likely(tp->rx_opt.tstamp_ok)) {
642 opts->options |= OPTION_TS;
Andrey Vaginee684b62013-02-11 05:50:19 +0000643 opts->tsval = tcb ? tcb->when + tp->tsoffset : 0;
Adam Langley33ad7982008-07-19 00:04:31 -0700644 opts->tsecr = tp->rx_opt.ts_recent;
645 size += TCPOLEN_TSTAMP_ALIGNED;
646 }
647
Ilpo Järvinencabeccb2009-02-28 04:44:38 +0000648 eff_sacks = tp->rx_opt.num_sacks + tp->rx_opt.dsack;
649 if (unlikely(eff_sacks)) {
Eric Dumazet95c96172012-04-15 05:58:06 +0000650 const unsigned int remaining = MAX_TCP_OPTION_SPACE - size;
Adam Langley33ad7982008-07-19 00:04:31 -0700651 opts->num_sack_blocks =
Eric Dumazet95c96172012-04-15 05:58:06 +0000652 min_t(unsigned int, eff_sacks,
Adam Langley33ad7982008-07-19 00:04:31 -0700653 (remaining - TCPOLEN_SACK_BASE_ALIGNED) /
654 TCPOLEN_SACK_PERBLOCK);
655 size += TCPOLEN_SACK_BASE_ALIGNED +
656 opts->num_sack_blocks * TCPOLEN_SACK_PERBLOCK;
657 }
658
659 return size;
Stephen Hemminger40efc6f2006-01-03 16:03:49 -0800660}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Eric Dumazet46d3cea2012-07-11 05:50:31 +0000662
663/* TCP SMALL QUEUES (TSQ)
664 *
665 * TSQ goal is to keep small amount of skbs per tcp flow in tx queues (qdisc+dev)
666 * to reduce RTT and bufferbloat.
667 * We do this using a special skb destructor (tcp_wfree).
668 *
669 * Its important tcp_wfree() can be replaced by sock_wfree() in the event skb
670 * needs to be reallocated in a driver.
671 * The invariant being skb->truesize substracted from sk->sk_wmem_alloc
672 *
673 * Since transmit from skb destructor is forbidden, we use a tasklet
674 * to process all sockets that eventually need to send more skbs.
675 * We use one tasklet per cpu, with its own queue of sockets.
676 */
677struct tsq_tasklet {
678 struct tasklet_struct tasklet;
679 struct list_head head; /* queue of tcp sockets */
680};
681static DEFINE_PER_CPU(struct tsq_tasklet, tsq_tasklet);
682
Eric Dumazet6f458df2012-07-20 05:45:50 +0000683static void tcp_tsq_handler(struct sock *sk)
684{
685 if ((1 << sk->sk_state) &
686 (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_CLOSING |
687 TCPF_CLOSE_WAIT | TCPF_LAST_ACK))
688 tcp_write_xmit(sk, tcp_current_mss(sk), 0, 0, GFP_ATOMIC);
689}
Eric Dumazet46d3cea2012-07-11 05:50:31 +0000690/*
691 * One tasklest per cpu tries to send more skbs.
692 * We run in tasklet context but need to disable irqs when
693 * transfering tsq->head because tcp_wfree() might
694 * interrupt us (non NAPI drivers)
695 */
696static void tcp_tasklet_func(unsigned long data)
697{
698 struct tsq_tasklet *tsq = (struct tsq_tasklet *)data;
699 LIST_HEAD(list);
700 unsigned long flags;
701 struct list_head *q, *n;
702 struct tcp_sock *tp;
703 struct sock *sk;
704
705 local_irq_save(flags);
706 list_splice_init(&tsq->head, &list);
707 local_irq_restore(flags);
708
709 list_for_each_safe(q, n, &list) {
710 tp = list_entry(q, struct tcp_sock, tsq_node);
711 list_del(&tp->tsq_node);
712
713 sk = (struct sock *)tp;
714 bh_lock_sock(sk);
715
716 if (!sock_owned_by_user(sk)) {
Eric Dumazet6f458df2012-07-20 05:45:50 +0000717 tcp_tsq_handler(sk);
Eric Dumazet46d3cea2012-07-11 05:50:31 +0000718 } else {
719 /* defer the work to tcp_release_cb() */
Eric Dumazet6f458df2012-07-20 05:45:50 +0000720 set_bit(TCP_TSQ_DEFERRED, &tp->tsq_flags);
Eric Dumazet46d3cea2012-07-11 05:50:31 +0000721 }
722 bh_unlock_sock(sk);
723
724 clear_bit(TSQ_QUEUED, &tp->tsq_flags);
725 sk_free(sk);
726 }
727}
728
Eric Dumazet6f458df2012-07-20 05:45:50 +0000729#define TCP_DEFERRED_ALL ((1UL << TCP_TSQ_DEFERRED) | \
730 (1UL << TCP_WRITE_TIMER_DEFERRED) | \
Eric Dumazet563d34d2012-07-23 09:48:52 +0200731 (1UL << TCP_DELACK_TIMER_DEFERRED) | \
732 (1UL << TCP_MTU_REDUCED_DEFERRED))
Eric Dumazet46d3cea2012-07-11 05:50:31 +0000733/**
734 * tcp_release_cb - tcp release_sock() callback
735 * @sk: socket
736 *
737 * called from release_sock() to perform protocol dependent
738 * actions before socket release.
739 */
740void tcp_release_cb(struct sock *sk)
741{
742 struct tcp_sock *tp = tcp_sk(sk);
Eric Dumazet6f458df2012-07-20 05:45:50 +0000743 unsigned long flags, nflags;
Eric Dumazet46d3cea2012-07-11 05:50:31 +0000744
Eric Dumazet6f458df2012-07-20 05:45:50 +0000745 /* perform an atomic operation only if at least one flag is set */
746 do {
747 flags = tp->tsq_flags;
748 if (!(flags & TCP_DEFERRED_ALL))
749 return;
750 nflags = flags & ~TCP_DEFERRED_ALL;
751 } while (cmpxchg(&tp->tsq_flags, flags, nflags) != flags);
752
753 if (flags & (1UL << TCP_TSQ_DEFERRED))
754 tcp_tsq_handler(sk);
755
Eric Dumazet144d56e2012-08-20 00:22:46 +0000756 if (flags & (1UL << TCP_WRITE_TIMER_DEFERRED)) {
Eric Dumazet6f458df2012-07-20 05:45:50 +0000757 tcp_write_timer_handler(sk);
Eric Dumazet144d56e2012-08-20 00:22:46 +0000758 __sock_put(sk);
759 }
760 if (flags & (1UL << TCP_DELACK_TIMER_DEFERRED)) {
Eric Dumazet6f458df2012-07-20 05:45:50 +0000761 tcp_delack_timer_handler(sk);
Eric Dumazet144d56e2012-08-20 00:22:46 +0000762 __sock_put(sk);
763 }
764 if (flags & (1UL << TCP_MTU_REDUCED_DEFERRED)) {
Eric Dumazet563d34d2012-07-23 09:48:52 +0200765 sk->sk_prot->mtu_reduced(sk);
Eric Dumazet144d56e2012-08-20 00:22:46 +0000766 __sock_put(sk);
767 }
Eric Dumazet46d3cea2012-07-11 05:50:31 +0000768}
769EXPORT_SYMBOL(tcp_release_cb);
770
771void __init tcp_tasklet_init(void)
772{
773 int i;
774
775 for_each_possible_cpu(i) {
776 struct tsq_tasklet *tsq = &per_cpu(tsq_tasklet, i);
777
778 INIT_LIST_HEAD(&tsq->head);
779 tasklet_init(&tsq->tasklet,
780 tcp_tasklet_func,
781 (unsigned long)tsq);
782 }
783}
784
785/*
786 * Write buffer destructor automatically called from kfree_skb.
787 * We cant xmit new skbs from this context, as we might already
788 * hold qdisc lock.
789 */
Eric Dumazetd6a4a102013-04-12 11:31:52 +0000790void tcp_wfree(struct sk_buff *skb)
Eric Dumazet46d3cea2012-07-11 05:50:31 +0000791{
792 struct sock *sk = skb->sk;
793 struct tcp_sock *tp = tcp_sk(sk);
794
795 if (test_and_clear_bit(TSQ_THROTTLED, &tp->tsq_flags) &&
796 !test_and_set_bit(TSQ_QUEUED, &tp->tsq_flags)) {
797 unsigned long flags;
798 struct tsq_tasklet *tsq;
799
800 /* Keep a ref on socket.
801 * This last ref will be released in tcp_tasklet_func()
802 */
803 atomic_sub(skb->truesize - 1, &sk->sk_wmem_alloc);
804
805 /* queue this socket to tasklet queue */
806 local_irq_save(flags);
807 tsq = &__get_cpu_var(tsq_tasklet);
808 list_add(&tp->tsq_node, &tsq->head);
809 tasklet_schedule(&tsq->tasklet);
810 local_irq_restore(flags);
811 } else {
812 sock_wfree(skb);
813 }
814}
815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816/* This routine actually transmits TCP packets queued in by
817 * tcp_do_sendmsg(). This is used by both the initial
818 * transmission and possible later retransmissions.
819 * All SKB's seen here are completely headerless. It is our
820 * job to build the TCP header, and pass the packet down to
821 * IP so it can do the same plus pass the packet off to the
822 * device.
823 *
824 * We are working here with either a clone of the original
825 * SKB, or a fresh unique copy made by the retransmit engine.
826 */
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800827static int tcp_transmit_skb(struct sock *sk, struct sk_buff *skb, int clone_it,
828 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
David S. Millerdfb4b9d2005-12-06 16:24:52 -0800830 const struct inet_connection_sock *icsk = inet_csk(sk);
831 struct inet_sock *inet;
832 struct tcp_sock *tp;
833 struct tcp_skb_cb *tcb;
Adam Langley33ad7982008-07-19 00:04:31 -0700834 struct tcp_out_options opts;
Eric Dumazet95c96172012-04-15 05:58:06 +0000835 unsigned int tcp_options_size, tcp_header_size;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800836 struct tcp_md5sig_key *md5;
David S. Millerdfb4b9d2005-12-06 16:24:52 -0800837 struct tcphdr *th;
David S. Millerdfb4b9d2005-12-06 16:24:52 -0800838 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
David S. Millerdfb4b9d2005-12-06 16:24:52 -0800840 BUG_ON(!skb || !tcp_skb_pcount(skb));
841
842 /* If congestion control is doing timestamping, we must
843 * take such a timestamp before we potentially clone/copy.
844 */
Stephen Hemminger164891a2007-04-23 22:26:16 -0700845 if (icsk->icsk_ca_ops->flags & TCP_CONG_RTT_STAMP)
David S. Millerdfb4b9d2005-12-06 16:24:52 -0800846 __net_timestamp(skb);
847
848 if (likely(clone_it)) {
Eric Dumazet0e280af2013-04-18 06:52:51 +0000849 const struct sk_buff *fclone = skb + 1;
850
851 if (unlikely(skb->fclone == SKB_FCLONE_ORIG &&
852 fclone->fclone == SKB_FCLONE_CLONE))
853 NET_INC_STATS_BH(sock_net(sk),
854 LINUX_MIB_TCPSPURIOUS_RTX_HOSTQUEUES);
855
David S. Millerdfb4b9d2005-12-06 16:24:52 -0800856 if (unlikely(skb_cloned(skb)))
857 skb = pskb_copy(skb, gfp_mask);
858 else
859 skb = skb_clone(skb, gfp_mask);
860 if (unlikely(!skb))
861 return -ENOBUFS;
862 }
863
864 inet = inet_sk(sk);
865 tp = tcp_sk(sk);
866 tcb = TCP_SKB_CB(skb);
Adam Langley33ad7982008-07-19 00:04:31 -0700867 memset(&opts, 0, sizeof(opts));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Eric Dumazet4de075e2011-09-27 13:25:05 -0400869 if (unlikely(tcb->tcp_flags & TCPHDR_SYN))
Adam Langley33ad7982008-07-19 00:04:31 -0700870 tcp_options_size = tcp_syn_options(sk, skb, &opts, &md5);
871 else
872 tcp_options_size = tcp_established_options(sk, skb, &opts,
873 &md5);
874 tcp_header_size = tcp_options_size + sizeof(struct tcphdr);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900875
Tom Herbert3853b582010-11-21 13:17:29 +0000876 if (tcp_packets_in_flight(tp) == 0) {
David S. Millerdfb4b9d2005-12-06 16:24:52 -0800877 tcp_ca_event(sk, CA_EVENT_TX_START);
Tom Herbert3853b582010-11-21 13:17:29 +0000878 skb->ooo_okay = 1;
879 } else
880 skb->ooo_okay = 0;
David S. Millerdfb4b9d2005-12-06 16:24:52 -0800881
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -0700882 skb_push(skb, tcp_header_size);
883 skb_reset_transport_header(skb);
Eric Dumazet46d3cea2012-07-11 05:50:31 +0000884
885 skb_orphan(skb);
886 skb->sk = sk;
887 skb->destructor = (sysctl_tcp_limit_output_bytes > 0) ?
888 tcp_wfree : sock_wfree;
889 atomic_add(skb->truesize, &sk->sk_wmem_alloc);
David S. Millerdfb4b9d2005-12-06 16:24:52 -0800890
891 /* Build TCP header and checksum it. */
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -0700892 th = tcp_hdr(skb);
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000893 th->source = inet->inet_sport;
894 th->dest = inet->inet_dport;
David S. Millerdfb4b9d2005-12-06 16:24:52 -0800895 th->seq = htonl(tcb->seq);
896 th->ack_seq = htonl(tp->rcv_nxt);
Al Virodf7a3b02006-09-27 18:38:52 -0700897 *(((__be16 *)th) + 6) = htons(((tcp_header_size >> 2) << 12) |
Eric Dumazet4de075e2011-09-27 13:25:05 -0400898 tcb->tcp_flags);
David S. Millerdfb4b9d2005-12-06 16:24:52 -0800899
Eric Dumazet4de075e2011-09-27 13:25:05 -0400900 if (unlikely(tcb->tcp_flags & TCPHDR_SYN)) {
David S. Millerdfb4b9d2005-12-06 16:24:52 -0800901 /* RFC1323: The window in SYN & SYN/ACK segments
902 * is never scaled.
903 */
Ilpo Järvinen600ff0c2007-02-13 12:42:11 -0800904 th->window = htons(min(tp->rcv_wnd, 65535U));
David S. Millerdfb4b9d2005-12-06 16:24:52 -0800905 } else {
906 th->window = htons(tcp_select_window(sk));
907 }
908 th->check = 0;
909 th->urg_ptr = 0;
910
Ilpo Järvinen33f5f572008-10-07 14:43:06 -0700911 /* The urg_mode check is necessary during a below snd_una win probe */
Herbert Xu76913672009-02-21 23:52:29 -0800912 if (unlikely(tcp_urg_mode(tp) && before(tcb->seq, tp->snd_up))) {
913 if (before(tp->snd_up, tcb->seq + 0x10000)) {
914 th->urg_ptr = htons(tp->snd_up - tcb->seq);
915 th->urg = 1;
916 } else if (after(tcb->seq + 0xFFFF, tp->snd_nxt)) {
Eric Dumazet0eae88f2010-04-20 19:06:52 -0700917 th->urg_ptr = htons(0xFFFF);
Herbert Xu76913672009-02-21 23:52:29 -0800918 th->urg = 1;
919 }
David S. Millerdfb4b9d2005-12-06 16:24:52 -0800920 }
921
William Allen Simpsonbd0388a2009-12-02 18:23:05 +0000922 tcp_options_write((__be32 *)(th + 1), tp, &opts);
Eric Dumazet4de075e2011-09-27 13:25:05 -0400923 if (likely((tcb->tcp_flags & TCPHDR_SYN) == 0))
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -0700924 TCP_ECN_send(sk, skb, tcp_header_size);
David S. Millerdfb4b9d2005-12-06 16:24:52 -0800925
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800926#ifdef CONFIG_TCP_MD5SIG
927 /* Calculate the MD5 hash, as we have all we need now */
928 if (md5) {
Eric Dumazeta4654192010-05-16 00:36:33 -0700929 sk_nocaps_add(sk, NETIF_F_GSO_MASK);
William Allen Simpsonbd0388a2009-12-02 18:23:05 +0000930 tp->af_specific->calc_md5_hash(opts.hash_location,
Adam Langley49a72df2008-07-19 00:01:42 -0700931 md5, sk, NULL, skb);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800932 }
933#endif
934
Herbert Xubb296242010-04-11 02:15:55 +0000935 icsk->icsk_af_ops->send_check(sk, skb);
David S. Millerdfb4b9d2005-12-06 16:24:52 -0800936
Eric Dumazet4de075e2011-09-27 13:25:05 -0400937 if (likely(tcb->tcp_flags & TCPHDR_ACK))
David S. Millerdfb4b9d2005-12-06 16:24:52 -0800938 tcp_event_ack_sent(sk, tcp_skb_pcount(skb));
939
940 if (skb->len != tcp_header_size)
Eric Dumazetcf533ea2011-10-21 05:22:42 -0400941 tcp_event_data_sent(tp, sk);
David S. Millerdfb4b9d2005-12-06 16:24:52 -0800942
Wei Yongjunbd37a082006-08-07 21:04:15 -0700943 if (after(tcb->end_seq, tp->snd_nxt) || tcb->seq == tcb->end_seq)
Tom Herbertaa2ea052010-04-22 07:00:24 +0000944 TCP_ADD_STATS(sock_net(sk), TCP_MIB_OUTSEGS,
945 tcp_skb_pcount(skb));
David S. Millerdfb4b9d2005-12-06 16:24:52 -0800946
David S. Millerd9d8da82011-05-06 22:23:20 -0700947 err = icsk->icsk_af_ops->queue_xmit(skb, &inet->cork.fl);
Hua Zhong83de47c2006-04-28 15:26:50 -0700948 if (likely(err <= 0))
David S. Millerdfb4b9d2005-12-06 16:24:52 -0800949 return err;
950
Ilpo Järvinen3cfe3ba2007-02-27 10:09:49 -0800951 tcp_enter_cwr(sk, 1);
David S. Millerdfb4b9d2005-12-06 16:24:52 -0800952
Gerrit Renkerb9df3cb2006-11-14 11:21:36 -0200953 return net_xmit_eval(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954}
955
Andi Kleen67edfef2009-07-21 23:00:40 +0000956/* This routine just queues the buffer for sending.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 *
958 * NOTE: probe0 timer is not checked, do not forget tcp_push_pending_frames,
959 * otherwise socket can stall.
960 */
961static void tcp_queue_skb(struct sock *sk, struct sk_buff *skb)
962{
963 struct tcp_sock *tp = tcp_sk(sk);
964
965 /* Advance write_seq and place onto the write_queue. */
966 tp->write_seq = TCP_SKB_CB(skb)->end_seq;
967 skb_header_release(skb);
David S. Millerfe067e82007-03-07 12:12:44 -0800968 tcp_add_write_queue_tail(sk, skb);
Hideo Aoki3ab224b2007-12-31 00:11:19 -0800969 sk->sk_wmem_queued += skb->truesize;
970 sk_mem_charge(sk, skb->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971}
972
Andi Kleen67edfef2009-07-21 23:00:40 +0000973/* Initialize TSO segments for a packet. */
Eric Dumazetcf533ea2011-10-21 05:22:42 -0400974static void tcp_set_skb_tso_segs(const struct sock *sk, struct sk_buff *skb,
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800975 unsigned int mss_now)
David S. Millerf6302d12005-07-05 15:18:03 -0700976{
Herbert Xu8e5b9dd2009-06-28 18:03:30 +0000977 if (skb->len <= mss_now || !sk_can_gso(sk) ||
978 skb->ip_summed == CHECKSUM_NONE) {
David S. Millerf6302d12005-07-05 15:18:03 -0700979 /* Avoid the costly divide in the normal
980 * non-TSO case.
981 */
Herbert Xu79671682006-06-22 02:40:14 -0700982 skb_shinfo(skb)->gso_segs = 1;
983 skb_shinfo(skb)->gso_size = 0;
Pravin B Shelarc9af6db2013-02-11 09:27:41 +0000984 skb_shinfo(skb)->gso_type = 0;
David S. Millerf6302d12005-07-05 15:18:03 -0700985 } else {
Ilpo Järvinen356f89e2007-08-24 23:00:31 -0700986 skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(skb->len, mss_now);
Herbert Xu79671682006-06-22 02:40:14 -0700987 skb_shinfo(skb)->gso_size = mss_now;
Pravin B Shelarc9af6db2013-02-11 09:27:41 +0000988 skb_shinfo(skb)->gso_type = sk->sk_gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 }
990}
991
Ilpo Järvinen91fed7a2007-10-09 01:24:15 -0700992/* When a modification to fackets out becomes necessary, we need to check
Ilpo Järvinen68f83532007-11-15 19:50:37 -0800993 * skb is counted to fackets_out or not.
Ilpo Järvinen91fed7a2007-10-09 01:24:15 -0700994 */
Eric Dumazetcf533ea2011-10-21 05:22:42 -0400995static void tcp_adjust_fackets_out(struct sock *sk, const struct sk_buff *skb,
Ilpo Järvinen91fed7a2007-10-09 01:24:15 -0700996 int decr)
997{
Ilpo Järvinena47e5a92007-11-15 19:41:46 -0800998 struct tcp_sock *tp = tcp_sk(sk);
999
Ilpo Järvinendc869672007-10-01 15:27:19 -07001000 if (!tp->sacked_out || tcp_is_reno(tp))
Ilpo Järvinen91fed7a2007-10-09 01:24:15 -07001001 return;
1002
Ilpo Järvinen6859d492007-12-02 00:48:06 +02001003 if (after(tcp_highest_sack_seq(tp), TCP_SKB_CB(skb)->seq))
Ilpo Järvinen91fed7a2007-10-09 01:24:15 -07001004 tp->fackets_out -= decr;
Ilpo Järvinen91fed7a2007-10-09 01:24:15 -07001005}
1006
Ilpo Järvinen797108d2009-04-01 23:15:17 +00001007/* Pcount in the middle of the write queue got changed, we need to do various
1008 * tweaks to fix counters
1009 */
Eric Dumazetcf533ea2011-10-21 05:22:42 -04001010static void tcp_adjust_pcount(struct sock *sk, const struct sk_buff *skb, int decr)
Ilpo Järvinen797108d2009-04-01 23:15:17 +00001011{
1012 struct tcp_sock *tp = tcp_sk(sk);
1013
1014 tp->packets_out -= decr;
1015
1016 if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED)
1017 tp->sacked_out -= decr;
1018 if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_RETRANS)
1019 tp->retrans_out -= decr;
1020 if (TCP_SKB_CB(skb)->sacked & TCPCB_LOST)
1021 tp->lost_out -= decr;
1022
1023 /* Reno case is special. Sigh... */
1024 if (tcp_is_reno(tp) && decr > 0)
1025 tp->sacked_out -= min_t(u32, tp->sacked_out, decr);
1026
1027 tcp_adjust_fackets_out(sk, skb, decr);
1028
1029 if (tp->lost_skb_hint &&
1030 before(TCP_SKB_CB(skb)->seq, TCP_SKB_CB(tp->lost_skb_hint)->seq) &&
Ilpo Järvinen52cf3cc2009-04-18 05:48:48 +00001031 (tcp_is_fack(tp) || (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED)))
Ilpo Järvinen797108d2009-04-01 23:15:17 +00001032 tp->lost_cnt_hint -= decr;
1033
1034 tcp_verify_left_out(tp);
1035}
1036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037/* Function to create two new TCP segments. Shrinks the given segment
1038 * to the specified size and appends a new segment with the rest of the
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001039 * packet to the list. This won't be called frequently, I hope.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 * Remember, these are still headerless SKBs at this point.
1041 */
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001042int tcp_fragment(struct sock *sk, struct sk_buff *skb, u32 len,
1043 unsigned int mss_now)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
1045 struct tcp_sock *tp = tcp_sk(sk);
1046 struct sk_buff *buff;
David S. Miller6475be12005-09-01 22:47:01 -07001047 int nsize, old_factor;
Herbert Xub60b49e2006-04-19 21:35:00 -07001048 int nlen;
Ilpo Järvinen9ce01462009-02-28 04:44:42 +00001049 u8 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Ilpo Järvinen2fceec12011-04-01 21:47:41 -07001051 if (WARN_ON(len > skb->len))
1052 return -EINVAL;
Stephen Hemminger6a438bb2005-11-10 17:14:59 -08001053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 nsize = skb_headlen(skb) - len;
1055 if (nsize < 0)
1056 nsize = 0;
1057
1058 if (skb_cloned(skb) &&
1059 skb_is_nonlinear(skb) &&
1060 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
1061 return -ENOMEM;
1062
1063 /* Get a new skb... force flag on. */
1064 buff = sk_stream_alloc_skb(sk, nsize, GFP_ATOMIC);
1065 if (buff == NULL)
1066 return -ENOMEM; /* We'll just try again later. */
Herbert Xuef5cb972006-04-18 13:24:14 -07001067
Hideo Aoki3ab224b2007-12-31 00:11:19 -08001068 sk->sk_wmem_queued += buff->truesize;
1069 sk_mem_charge(sk, buff->truesize);
Herbert Xub60b49e2006-04-19 21:35:00 -07001070 nlen = skb->len - len - nsize;
1071 buff->truesize += nlen;
1072 skb->truesize -= nlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
1074 /* Correct the sequence numbers. */
1075 TCP_SKB_CB(buff)->seq = TCP_SKB_CB(skb)->seq + len;
1076 TCP_SKB_CB(buff)->end_seq = TCP_SKB_CB(skb)->end_seq;
1077 TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(buff)->seq;
1078
1079 /* PSH and FIN should only be set in the second packet. */
Eric Dumazet4de075e2011-09-27 13:25:05 -04001080 flags = TCP_SKB_CB(skb)->tcp_flags;
1081 TCP_SKB_CB(skb)->tcp_flags = flags & ~(TCPHDR_FIN | TCPHDR_PSH);
1082 TCP_SKB_CB(buff)->tcp_flags = flags;
Herbert Xue14c3ca2005-09-19 18:18:38 -07001083 TCP_SKB_CB(buff)->sacked = TCP_SKB_CB(skb)->sacked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Patrick McHardy84fa7932006-08-29 16:44:56 -07001085 if (!skb_shinfo(skb)->nr_frags && skb->ip_summed != CHECKSUM_PARTIAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 /* Copy and checksum data tail into the new buffer. */
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001087 buff->csum = csum_partial_copy_nocheck(skb->data + len,
1088 skb_put(buff, nsize),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 nsize, 0);
1090
1091 skb_trim(skb, len);
1092
1093 skb->csum = csum_block_sub(skb->csum, buff->csum, len);
1094 } else {
Patrick McHardy84fa7932006-08-29 16:44:56 -07001095 skb->ip_summed = CHECKSUM_PARTIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 skb_split(skb, buff, len);
1097 }
1098
1099 buff->ip_summed = skb->ip_summed;
1100
1101 /* Looks stupid, but our code really uses when of
1102 * skbs, which it never sent before. --ANK
1103 */
1104 TCP_SKB_CB(buff)->when = TCP_SKB_CB(skb)->when;
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001105 buff->tstamp = skb->tstamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
David S. Miller6475be12005-09-01 22:47:01 -07001107 old_factor = tcp_skb_pcount(skb);
1108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 /* Fix up tso_factor for both original and new SKB. */
David S. Miller846998a2005-08-04 19:52:01 -07001110 tcp_set_skb_tso_segs(sk, skb, mss_now);
1111 tcp_set_skb_tso_segs(sk, buff, mss_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
David S. Miller6475be12005-09-01 22:47:01 -07001113 /* If this packet has been sent out already, we must
1114 * adjust the various packet counters.
1115 */
Herbert Xucf0b4502005-09-08 15:10:52 -07001116 if (!before(tp->snd_nxt, TCP_SKB_CB(buff)->end_seq)) {
David S. Miller6475be12005-09-01 22:47:01 -07001117 int diff = old_factor - tcp_skb_pcount(skb) -
1118 tcp_skb_pcount(buff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Ilpo Järvinen797108d2009-04-01 23:15:17 +00001120 if (diff)
1121 tcp_adjust_pcount(sk, skb, diff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 }
1123
1124 /* Link BUFF into the send queue. */
David S. Millerf44b5272005-07-05 15:18:34 -07001125 skb_header_release(buff);
David S. Millerfe067e82007-03-07 12:12:44 -08001126 tcp_insert_write_queue_after(skb, buff, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128 return 0;
1129}
1130
1131/* This is similar to __pskb_pull_head() (it will go to core/skbuff.c
1132 * eventually). The difference is that pulled data not copied, but
1133 * immediately discarded.
1134 */
Herbert Xu ~{PmVHI~}f2911962006-06-05 15:03:37 -07001135static void __pskb_trim_head(struct sk_buff *skb, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
1137 int i, k, eat;
1138
Eric Dumazet4fa48bf2011-12-04 08:51:08 +00001139 eat = min_t(int, len, skb_headlen(skb));
1140 if (eat) {
1141 __skb_pull(skb, eat);
1142 len -= eat;
1143 if (!len)
1144 return;
1145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 eat = len;
1147 k = 0;
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001148 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001149 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1150
1151 if (size <= eat) {
Ian Campbellaff65da2011-08-22 23:44:59 +00001152 skb_frag_unref(skb, i);
Eric Dumazet9e903e02011-10-18 21:00:24 +00001153 eat -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 } else {
1155 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1156 if (eat) {
1157 skb_shinfo(skb)->frags[k].page_offset += eat;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001158 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 eat = 0;
1160 }
1161 k++;
1162 }
1163 }
1164 skb_shinfo(skb)->nr_frags = k;
1165
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001166 skb_reset_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 skb->data_len -= len;
1168 skb->len = skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169}
1170
Andi Kleen67edfef2009-07-21 23:00:40 +00001171/* Remove acked data from a packet in the transmit queue. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172int tcp_trim_head(struct sock *sk, struct sk_buff *skb, u32 len)
1173{
Pravin B Shelar14bbd6a2013-02-14 09:44:49 +00001174 if (skb_unclone(skb, GFP_ATOMIC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 return -ENOMEM;
1176
Eric Dumazet4fa48bf2011-12-04 08:51:08 +00001177 __pskb_trim_head(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 TCP_SKB_CB(skb)->seq += len;
Patrick McHardy84fa7932006-08-29 16:44:56 -07001180 skb->ip_summed = CHECKSUM_PARTIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182 skb->truesize -= len;
1183 sk->sk_wmem_queued -= len;
Hideo Aoki3ab224b2007-12-31 00:11:19 -08001184 sk_mem_uncharge(sk, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 sock_set_flag(sk, SOCK_QUEUE_SHRUNK);
1186
Neal Cardwell5b35e1e2012-01-28 17:29:46 +00001187 /* Any change of skb->len requires recalculation of tso factor. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 if (tcp_skb_pcount(skb) > 1)
Neal Cardwell5b35e1e2012-01-28 17:29:46 +00001189 tcp_set_skb_tso_segs(sk, skb, tcp_skb_mss(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
1191 return 0;
1192}
1193
Yuchung Cheng1b63edd2013-02-22 08:59:06 +00001194/* Calculate MSS not accounting any TCP options. */
1195static inline int __tcp_mtu_to_mss(struct sock *sk, int pmtu)
John Heffner5d424d52006-03-20 17:53:41 -08001196{
Eric Dumazetcf533ea2011-10-21 05:22:42 -04001197 const struct tcp_sock *tp = tcp_sk(sk);
1198 const struct inet_connection_sock *icsk = inet_csk(sk);
John Heffner5d424d52006-03-20 17:53:41 -08001199 int mss_now;
1200
1201 /* Calculate base mss without TCP options:
1202 It is MMS_S - sizeof(tcphdr) of rfc1122
1203 */
1204 mss_now = pmtu - icsk->icsk_af_ops->net_header_len - sizeof(struct tcphdr);
1205
Eric Dumazet67469602012-04-24 07:37:38 +00001206 /* IPv6 adds a frag_hdr in case RTAX_FEATURE_ALLFRAG is set */
1207 if (icsk->icsk_af_ops->net_frag_header_len) {
1208 const struct dst_entry *dst = __sk_dst_get(sk);
1209
1210 if (dst && dst_allfrag(dst))
1211 mss_now -= icsk->icsk_af_ops->net_frag_header_len;
1212 }
1213
John Heffner5d424d52006-03-20 17:53:41 -08001214 /* Clamp it (mss_clamp does not include tcp options) */
1215 if (mss_now > tp->rx_opt.mss_clamp)
1216 mss_now = tp->rx_opt.mss_clamp;
1217
1218 /* Now subtract optional transport overhead */
1219 mss_now -= icsk->icsk_ext_hdr_len;
1220
1221 /* Then reserve room for full set of TCP options and 8 bytes of data */
1222 if (mss_now < 48)
1223 mss_now = 48;
John Heffner5d424d52006-03-20 17:53:41 -08001224 return mss_now;
1225}
1226
Yuchung Cheng1b63edd2013-02-22 08:59:06 +00001227/* Calculate MSS. Not accounting for SACKs here. */
1228int tcp_mtu_to_mss(struct sock *sk, int pmtu)
1229{
1230 /* Subtract TCP options size, not including SACKs */
1231 return __tcp_mtu_to_mss(sk, pmtu) -
1232 (tcp_sk(sk)->tcp_header_len - sizeof(struct tcphdr));
1233}
1234
John Heffner5d424d52006-03-20 17:53:41 -08001235/* Inverse of above */
Eric Dumazet67469602012-04-24 07:37:38 +00001236int tcp_mss_to_mtu(struct sock *sk, int mss)
John Heffner5d424d52006-03-20 17:53:41 -08001237{
Eric Dumazetcf533ea2011-10-21 05:22:42 -04001238 const struct tcp_sock *tp = tcp_sk(sk);
1239 const struct inet_connection_sock *icsk = inet_csk(sk);
John Heffner5d424d52006-03-20 17:53:41 -08001240 int mtu;
1241
1242 mtu = mss +
1243 tp->tcp_header_len +
1244 icsk->icsk_ext_hdr_len +
1245 icsk->icsk_af_ops->net_header_len;
1246
Eric Dumazet67469602012-04-24 07:37:38 +00001247 /* IPv6 adds a frag_hdr in case RTAX_FEATURE_ALLFRAG is set */
1248 if (icsk->icsk_af_ops->net_frag_header_len) {
1249 const struct dst_entry *dst = __sk_dst_get(sk);
1250
1251 if (dst && dst_allfrag(dst))
1252 mtu += icsk->icsk_af_ops->net_frag_header_len;
1253 }
John Heffner5d424d52006-03-20 17:53:41 -08001254 return mtu;
1255}
1256
Andi Kleen67edfef2009-07-21 23:00:40 +00001257/* MTU probing init per socket */
John Heffner5d424d52006-03-20 17:53:41 -08001258void tcp_mtup_init(struct sock *sk)
1259{
1260 struct tcp_sock *tp = tcp_sk(sk);
1261 struct inet_connection_sock *icsk = inet_csk(sk);
1262
1263 icsk->icsk_mtup.enabled = sysctl_tcp_mtu_probing > 1;
1264 icsk->icsk_mtup.search_high = tp->rx_opt.mss_clamp + sizeof(struct tcphdr) +
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001265 icsk->icsk_af_ops->net_header_len;
John Heffner5d424d52006-03-20 17:53:41 -08001266 icsk->icsk_mtup.search_low = tcp_mss_to_mtu(sk, sysctl_tcp_base_mss);
1267 icsk->icsk_mtup.probe_size = 0;
1268}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001269EXPORT_SYMBOL(tcp_mtup_init);
John Heffner5d424d52006-03-20 17:53:41 -08001270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271/* This function synchronize snd mss to current pmtu/exthdr set.
1272
1273 tp->rx_opt.user_mss is mss set by user by TCP_MAXSEG. It does NOT counts
1274 for TCP options, but includes only bare TCP header.
1275
1276 tp->rx_opt.mss_clamp is mss negotiated at connection setup.
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -08001277 It is minimum of user_mss and mss received with SYN.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 It also does not include TCP options.
1279
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08001280 inet_csk(sk)->icsk_pmtu_cookie is last pmtu, seen by this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
1282 tp->mss_cache is current effective sending mss, including
1283 all tcp options except for SACKs. It is evaluated,
1284 taking into account current pmtu, but never exceeds
1285 tp->rx_opt.mss_clamp.
1286
1287 NOTE1. rfc1122 clearly states that advertised MSS
1288 DOES NOT include either tcp or ip options.
1289
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08001290 NOTE2. inet_csk(sk)->icsk_pmtu_cookie and tp->mss_cache
1291 are READ ONLY outside this function. --ANK (980731)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293unsigned int tcp_sync_mss(struct sock *sk, u32 pmtu)
1294{
1295 struct tcp_sock *tp = tcp_sk(sk);
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08001296 struct inet_connection_sock *icsk = inet_csk(sk);
John Heffner5d424d52006-03-20 17:53:41 -08001297 int mss_now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
John Heffner5d424d52006-03-20 17:53:41 -08001299 if (icsk->icsk_mtup.search_high > pmtu)
1300 icsk->icsk_mtup.search_high = pmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
John Heffner5d424d52006-03-20 17:53:41 -08001302 mss_now = tcp_mtu_to_mss(sk, pmtu);
Ilpo Järvinen409d22b2007-12-31 14:57:40 -08001303 mss_now = tcp_bound_to_half_wnd(tp, mss_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
1305 /* And store cached results */
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08001306 icsk->icsk_pmtu_cookie = pmtu;
John Heffner5d424d52006-03-20 17:53:41 -08001307 if (icsk->icsk_mtup.enabled)
1308 mss_now = min(mss_now, tcp_mtu_to_mss(sk, icsk->icsk_mtup.search_low));
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001309 tp->mss_cache = mss_now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311 return mss_now;
1312}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001313EXPORT_SYMBOL(tcp_sync_mss);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315/* Compute the current effective MSS, taking SACKs and IP options,
1316 * and even PMTU discovery events into account.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 */
Ilpo Järvinen0c54b852009-03-14 14:23:05 +00001318unsigned int tcp_current_mss(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
Eric Dumazetcf533ea2011-10-21 05:22:42 -04001320 const struct tcp_sock *tp = tcp_sk(sk);
1321 const struct dst_entry *dst = __sk_dst_get(sk);
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001322 u32 mss_now;
Eric Dumazet95c96172012-04-15 05:58:06 +00001323 unsigned int header_len;
Adam Langley33ad7982008-07-19 00:04:31 -07001324 struct tcp_out_options opts;
1325 struct tcp_md5sig_key *md5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001327 mss_now = tp->mss_cache;
1328
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 if (dst) {
1330 u32 mtu = dst_mtu(dst);
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08001331 if (mtu != inet_csk(sk)->icsk_pmtu_cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 mss_now = tcp_sync_mss(sk, mtu);
1333 }
1334
Adam Langley33ad7982008-07-19 00:04:31 -07001335 header_len = tcp_established_options(sk, NULL, &opts, &md5) +
1336 sizeof(struct tcphdr);
1337 /* The mss_cache is sized based on tp->tcp_header_len, which assumes
1338 * some common options. If this is an odd packet (because we have SACK
1339 * blocks etc) then our calculated header_len will be different, and
1340 * we have to adjust mss_now correspondingly */
1341 if (header_len != tp->tcp_header_len) {
1342 int delta = (int) header_len - tp->tcp_header_len;
1343 mss_now -= delta;
1344 }
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001345
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 return mss_now;
1347}
1348
David S. Millera762a982005-07-05 15:18:51 -07001349/* Congestion window validation. (RFC2861) */
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07001350static void tcp_cwnd_validate(struct sock *sk)
David S. Millera762a982005-07-05 15:18:51 -07001351{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07001352 struct tcp_sock *tp = tcp_sk(sk);
David S. Millera762a982005-07-05 15:18:51 -07001353
Ilpo Järvinend436d682007-12-31 14:58:00 -08001354 if (tp->packets_out >= tp->snd_cwnd) {
David S. Millera762a982005-07-05 15:18:51 -07001355 /* Network is feed fully. */
1356 tp->snd_cwnd_used = 0;
1357 tp->snd_cwnd_stamp = tcp_time_stamp;
1358 } else {
1359 /* Network starves. */
1360 if (tp->packets_out > tp->snd_cwnd_used)
1361 tp->snd_cwnd_used = tp->packets_out;
1362
David S. Miller15d33c02007-04-09 13:23:14 -07001363 if (sysctl_tcp_slow_start_after_idle &&
1364 (s32)(tcp_time_stamp - tp->snd_cwnd_stamp) >= inet_csk(sk)->icsk_rto)
David S. Millera762a982005-07-05 15:18:51 -07001365 tcp_cwnd_application_limited(sk);
1366 }
1367}
1368
Ilpo Järvinen0e3a4802007-12-24 21:33:45 -08001369/* Returns the portion of skb which can be sent right away without
1370 * introducing MSS oddities to segment boundaries. In rare cases where
1371 * mss_now != mss_cache, we will request caller to create a small skb
1372 * per input skb which could be mostly avoided here (if desired).
Ilpo Järvinen5ea3a742008-03-11 17:55:27 -07001373 *
1374 * We explicitly want to create a request for splitting write queue tail
1375 * to a small skb for Nagle purposes while avoiding unnecessary modulos,
1376 * thus all the complexity (cwnd_len is always MSS multiple which we
1377 * return whenever allowed by the other factors). Basically we need the
1378 * modulo only when the receiver window alone is the limiting factor or
1379 * when we would be allowed to send the split-due-to-Nagle skb fully.
Ilpo Järvinen0e3a4802007-12-24 21:33:45 -08001380 */
Eric Dumazetcf533ea2011-10-21 05:22:42 -04001381static unsigned int tcp_mss_split_point(const struct sock *sk, const struct sk_buff *skb,
Ben Hutchings14853482012-07-30 16:11:42 +00001382 unsigned int mss_now, unsigned int max_segs)
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001383{
Eric Dumazetcf533ea2011-10-21 05:22:42 -04001384 const struct tcp_sock *tp = tcp_sk(sk);
Ben Hutchings14853482012-07-30 16:11:42 +00001385 u32 needed, window, max_len;
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001386
Ilpo Järvinen90840de2007-12-31 04:48:41 -08001387 window = tcp_wnd_end(tp) - TCP_SKB_CB(skb)->seq;
Ben Hutchings14853482012-07-30 16:11:42 +00001388 max_len = mss_now * max_segs;
Ilpo Järvinen0e3a4802007-12-24 21:33:45 -08001389
Ben Hutchings14853482012-07-30 16:11:42 +00001390 if (likely(max_len <= window && skb != tcp_write_queue_tail(sk)))
1391 return max_len;
Ilpo Järvinen0e3a4802007-12-24 21:33:45 -08001392
Ilpo Järvinen5ea3a742008-03-11 17:55:27 -07001393 needed = min(skb->len, window);
1394
Ben Hutchings14853482012-07-30 16:11:42 +00001395 if (max_len <= needed)
1396 return max_len;
Ilpo Järvinen0e3a4802007-12-24 21:33:45 -08001397
Ilpo Järvinen0e3a4802007-12-24 21:33:45 -08001398 return needed - needed % mss_now;
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001399}
1400
1401/* Can at least one segment of SKB be sent right now, according to the
1402 * congestion window rules? If so, return how many segments are allowed.
1403 */
Eric Dumazetcf533ea2011-10-21 05:22:42 -04001404static inline unsigned int tcp_cwnd_test(const struct tcp_sock *tp,
1405 const struct sk_buff *skb)
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001406{
1407 u32 in_flight, cwnd;
1408
1409 /* Don't be strict about the congestion window for the final FIN. */
Eric Dumazet4de075e2011-09-27 13:25:05 -04001410 if ((TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN) &&
1411 tcp_skb_pcount(skb) == 1)
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001412 return 1;
1413
1414 in_flight = tcp_packets_in_flight(tp);
1415 cwnd = tp->snd_cwnd;
1416 if (in_flight < cwnd)
1417 return (cwnd - in_flight);
1418
1419 return 0;
1420}
1421
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001422/* Initialize TSO state of a skb.
Andi Kleen67edfef2009-07-21 23:00:40 +00001423 * This must be invoked the first time we consider transmitting
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001424 * SKB onto the wire.
1425 */
Eric Dumazetcf533ea2011-10-21 05:22:42 -04001426static int tcp_init_tso_segs(const struct sock *sk, struct sk_buff *skb,
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001427 unsigned int mss_now)
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001428{
1429 int tso_segs = tcp_skb_pcount(skb);
1430
Ilpo Järvinenf8269a42008-12-03 21:24:48 -08001431 if (!tso_segs || (tso_segs > 1 && tcp_skb_mss(skb) != mss_now)) {
David S. Miller846998a2005-08-04 19:52:01 -07001432 tcp_set_skb_tso_segs(sk, skb, mss_now);
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001433 tso_segs = tcp_skb_pcount(skb);
1434 }
1435 return tso_segs;
1436}
1437
Andi Kleen67edfef2009-07-21 23:00:40 +00001438/* Minshall's variant of the Nagle send check. */
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001439static inline bool tcp_minshall_check(const struct tcp_sock *tp)
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001440{
Jianjun Kong09cb1052008-11-03 00:27:11 -08001441 return after(tp->snd_sml, tp->snd_una) &&
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001442 !after(tp->snd_sml, tp->snd_nxt);
1443}
1444
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001445/* Return false, if packet can be sent now without violation Nagle's rules:
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001446 * 1. It is full sized.
1447 * 2. Or it contains FIN. (already checked by caller)
Feng King6d67e9b2011-11-05 04:23:23 +00001448 * 3. Or TCP_CORK is not set, and TCP_NODELAY is set.
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001449 * 4. Or TCP_CORK is not set, and all sent packets are ACKed.
1450 * With Minshall's modification: all sent small packets are ACKed.
1451 */
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001452static inline bool tcp_nagle_check(const struct tcp_sock *tp,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001453 const struct sk_buff *skb,
Eric Dumazet95c96172012-04-15 05:58:06 +00001454 unsigned int mss_now, int nonagle)
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001455{
Eric Dumazeta02cec22010-09-22 20:43:57 +00001456 return skb->len < mss_now &&
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001457 ((nonagle & TCP_NAGLE_CORK) ||
Eric Dumazeta02cec22010-09-22 20:43:57 +00001458 (!nonagle && tp->packets_out && tcp_minshall_check(tp)));
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001459}
1460
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001461/* Return true if the Nagle test allows this packet to be
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001462 * sent now.
1463 */
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001464static inline bool tcp_nagle_test(const struct tcp_sock *tp, const struct sk_buff *skb,
1465 unsigned int cur_mss, int nonagle)
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001466{
1467 /* Nagle rule does not apply to frames, which sit in the middle of the
1468 * write_queue (they have no chances to get new data).
1469 *
1470 * This is implemented in the callers, where they modify the 'nonagle'
1471 * argument based upon the location of SKB in the send queue.
1472 */
1473 if (nonagle & TCP_NAGLE_PUSH)
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001474 return true;
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001475
Yuchung Cheng9b441902013-03-20 13:32:58 +00001476 /* Don't use the nagle rule for urgent data (or for the final FIN). */
1477 if (tcp_urg_mode(tp) || (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN))
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001478 return true;
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001479
1480 if (!tcp_nagle_check(tp, skb, cur_mss, nonagle))
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001481 return true;
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001482
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001483 return false;
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001484}
1485
1486/* Does at least the first segment of SKB fit into the send window? */
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001487static bool tcp_snd_wnd_test(const struct tcp_sock *tp,
1488 const struct sk_buff *skb,
1489 unsigned int cur_mss)
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001490{
1491 u32 end_seq = TCP_SKB_CB(skb)->end_seq;
1492
1493 if (skb->len > cur_mss)
1494 end_seq = TCP_SKB_CB(skb)->seq + cur_mss;
1495
Ilpo Järvinen90840de2007-12-31 04:48:41 -08001496 return !after(end_seq, tcp_wnd_end(tp));
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001497}
1498
David S. Millerfe067e82007-03-07 12:12:44 -08001499/* This checks if the data bearing packet SKB (usually tcp_send_head(sk))
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001500 * should be put on the wire right now. If so, it returns the number of
1501 * packets allowed by the congestion window.
1502 */
Eric Dumazetcf533ea2011-10-21 05:22:42 -04001503static unsigned int tcp_snd_test(const struct sock *sk, struct sk_buff *skb,
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001504 unsigned int cur_mss, int nonagle)
1505{
Eric Dumazetcf533ea2011-10-21 05:22:42 -04001506 const struct tcp_sock *tp = tcp_sk(sk);
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001507 unsigned int cwnd_quota;
1508
David S. Miller846998a2005-08-04 19:52:01 -07001509 tcp_init_tso_segs(sk, skb, cur_mss);
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001510
1511 if (!tcp_nagle_test(tp, skb, cur_mss, nonagle))
1512 return 0;
1513
1514 cwnd_quota = tcp_cwnd_test(tp, skb);
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001515 if (cwnd_quota && !tcp_snd_wnd_test(tp, skb, cur_mss))
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001516 cwnd_quota = 0;
1517
1518 return cwnd_quota;
1519}
1520
Andi Kleen67edfef2009-07-21 23:00:40 +00001521/* Test if sending is allowed right now. */
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001522bool tcp_may_send_now(struct sock *sk)
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001523{
Eric Dumazetcf533ea2011-10-21 05:22:42 -04001524 const struct tcp_sock *tp = tcp_sk(sk);
David S. Millerfe067e82007-03-07 12:12:44 -08001525 struct sk_buff *skb = tcp_send_head(sk);
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001526
Eric Dumazeta02cec22010-09-22 20:43:57 +00001527 return skb &&
Ilpo Järvinen0c54b852009-03-14 14:23:05 +00001528 tcp_snd_test(sk, skb, tcp_current_mss(sk),
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001529 (tcp_skb_is_last(sk, skb) ?
Eric Dumazeta02cec22010-09-22 20:43:57 +00001530 tp->nonagle : TCP_NAGLE_PUSH));
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001531}
1532
1533/* Trim TSO SKB to LEN bytes, put the remaining data into a new packet
1534 * which is put after SKB on the list. It is very much like
1535 * tcp_fragment() except that it may make several kinds of assumptions
1536 * in order to speed up the splitting operation. In particular, we
1537 * know that all the data is in scatter-gather pages, and that the
1538 * packet has never been sent out before (and thus is not cloned).
1539 */
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001540static int tso_fragment(struct sock *sk, struct sk_buff *skb, unsigned int len,
Eric Dumazetc4ead4c2010-06-24 01:00:22 +00001541 unsigned int mss_now, gfp_t gfp)
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001542{
1543 struct sk_buff *buff;
1544 int nlen = skb->len - len;
Ilpo Järvinen9ce01462009-02-28 04:44:42 +00001545 u8 flags;
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001546
1547 /* All of a TSO frame must be composed of paged data. */
Herbert Xuc8ac3772005-08-16 20:43:40 -07001548 if (skb->len != skb->data_len)
1549 return tcp_fragment(sk, skb, len, mss_now);
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001550
Eric Dumazetc4ead4c2010-06-24 01:00:22 +00001551 buff = sk_stream_alloc_skb(sk, 0, gfp);
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001552 if (unlikely(buff == NULL))
1553 return -ENOMEM;
1554
Hideo Aoki3ab224b2007-12-31 00:11:19 -08001555 sk->sk_wmem_queued += buff->truesize;
1556 sk_mem_charge(sk, buff->truesize);
Herbert Xub60b49e2006-04-19 21:35:00 -07001557 buff->truesize += nlen;
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001558 skb->truesize -= nlen;
1559
1560 /* Correct the sequence numbers. */
1561 TCP_SKB_CB(buff)->seq = TCP_SKB_CB(skb)->seq + len;
1562 TCP_SKB_CB(buff)->end_seq = TCP_SKB_CB(skb)->end_seq;
1563 TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(buff)->seq;
1564
1565 /* PSH and FIN should only be set in the second packet. */
Eric Dumazet4de075e2011-09-27 13:25:05 -04001566 flags = TCP_SKB_CB(skb)->tcp_flags;
1567 TCP_SKB_CB(skb)->tcp_flags = flags & ~(TCPHDR_FIN | TCPHDR_PSH);
1568 TCP_SKB_CB(buff)->tcp_flags = flags;
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001569
1570 /* This packet was never sent out yet, so no SACK bits. */
1571 TCP_SKB_CB(buff)->sacked = 0;
1572
Patrick McHardy84fa7932006-08-29 16:44:56 -07001573 buff->ip_summed = skb->ip_summed = CHECKSUM_PARTIAL;
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001574 skb_split(skb, buff, len);
1575
1576 /* Fix up tso_factor for both original and new SKB. */
David S. Miller846998a2005-08-04 19:52:01 -07001577 tcp_set_skb_tso_segs(sk, skb, mss_now);
1578 tcp_set_skb_tso_segs(sk, buff, mss_now);
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001579
1580 /* Link BUFF into the send queue. */
1581 skb_header_release(buff);
David S. Millerfe067e82007-03-07 12:12:44 -08001582 tcp_insert_write_queue_after(skb, buff, sk);
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001583
1584 return 0;
1585}
1586
1587/* Try to defer sending, if possible, in order to minimize the amount
1588 * of TSO splitting we do. View it as a kind of TSO Nagle test.
1589 *
1590 * This algorithm is from John Heffner.
1591 */
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001592static bool tcp_tso_should_defer(struct sock *sk, struct sk_buff *skb)
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001593{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07001594 struct tcp_sock *tp = tcp_sk(sk);
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03001595 const struct inet_connection_sock *icsk = inet_csk(sk);
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001596 u32 send_win, cong_win, limit, in_flight;
Eric Dumazetad9f4f52010-12-07 12:03:55 +00001597 int win_divisor;
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001598
Eric Dumazet4de075e2011-09-27 13:25:05 -04001599 if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)
John Heffnerae8064a2006-10-18 20:36:48 -07001600 goto send_now;
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001601
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03001602 if (icsk->icsk_ca_state != TCP_CA_Open)
John Heffnerae8064a2006-10-18 20:36:48 -07001603 goto send_now;
1604
1605 /* Defer for less than two clock ticks. */
Ilpo Järvinenbd515c32007-12-20 20:36:03 -08001606 if (tp->tso_deferred &&
Ilpo Järvinena2acde02008-12-05 22:49:37 -08001607 (((u32)jiffies << 1) >> 1) - (tp->tso_deferred >> 1) > 1)
John Heffnerae8064a2006-10-18 20:36:48 -07001608 goto send_now;
David S. Miller908a75c2005-07-05 15:43:58 -07001609
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001610 in_flight = tcp_packets_in_flight(tp);
1611
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001612 BUG_ON(tcp_skb_pcount(skb) <= 1 || (tp->snd_cwnd <= in_flight));
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001613
Ilpo Järvinen90840de2007-12-31 04:48:41 -08001614 send_win = tcp_wnd_end(tp) - TCP_SKB_CB(skb)->seq;
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001615
1616 /* From in_flight test above, we know that cwnd > in_flight. */
1617 cong_win = (tp->snd_cwnd - in_flight) * tp->mss_cache;
1618
1619 limit = min(send_win, cong_win);
1620
David S. Millerba244fe2006-03-11 18:51:49 -08001621 /* If a full-sized TSO skb can be sent, do it. */
Ben Hutchings14853482012-07-30 16:11:42 +00001622 if (limit >= min_t(unsigned int, sk->sk_gso_max_size,
1623 sk->sk_gso_max_segs * tp->mss_cache))
John Heffnerae8064a2006-10-18 20:36:48 -07001624 goto send_now;
David S. Millerba244fe2006-03-11 18:51:49 -08001625
Ilpo Järvinen62ad2762009-02-28 04:44:29 +00001626 /* Middle in queue won't get any more data, full sendable already? */
1627 if ((skb != tcp_write_queue_tail(sk)) && (limit >= skb->len))
1628 goto send_now;
1629
Eric Dumazetad9f4f52010-12-07 12:03:55 +00001630 win_divisor = ACCESS_ONCE(sysctl_tcp_tso_win_divisor);
1631 if (win_divisor) {
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001632 u32 chunk = min(tp->snd_wnd, tp->snd_cwnd * tp->mss_cache);
1633
1634 /* If at least some fraction of a window is available,
1635 * just use it.
1636 */
Eric Dumazetad9f4f52010-12-07 12:03:55 +00001637 chunk /= win_divisor;
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001638 if (limit >= chunk)
John Heffnerae8064a2006-10-18 20:36:48 -07001639 goto send_now;
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001640 } else {
1641 /* Different approach, try not to defer past a single
1642 * ACK. Receiver should ACK every other full sized
1643 * frame, so if we have space for more than 3 frames
1644 * then send now.
1645 */
Neal Cardwell6b5a5c02011-11-21 17:15:14 +00001646 if (limit > tcp_max_tso_deferred_mss(tp) * tp->mss_cache)
John Heffnerae8064a2006-10-18 20:36:48 -07001647 goto send_now;
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001648 }
1649
Eric Dumazetf4541d62013-03-21 17:36:09 +00001650 /* Ok, it looks like it is advisable to defer.
1651 * Do not rearm the timer if already set to not break TCP ACK clocking.
1652 */
1653 if (!tp->tso_deferred)
1654 tp->tso_deferred = 1 | (jiffies << 1);
John Heffnerae8064a2006-10-18 20:36:48 -07001655
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001656 return true;
John Heffnerae8064a2006-10-18 20:36:48 -07001657
1658send_now:
1659 tp->tso_deferred = 0;
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001660 return false;
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001661}
1662
John Heffner5d424d52006-03-20 17:53:41 -08001663/* Create a new MTU probe if we are ready.
Andi Kleen67edfef2009-07-21 23:00:40 +00001664 * MTU probe is regularly attempting to increase the path MTU by
1665 * deliberately sending larger packets. This discovers routing
1666 * changes resulting in larger path MTUs.
1667 *
John Heffner5d424d52006-03-20 17:53:41 -08001668 * Returns 0 if we should wait to probe (no cwnd available),
1669 * 1 if a probe was sent,
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001670 * -1 otherwise
1671 */
John Heffner5d424d52006-03-20 17:53:41 -08001672static int tcp_mtu_probe(struct sock *sk)
1673{
1674 struct tcp_sock *tp = tcp_sk(sk);
1675 struct inet_connection_sock *icsk = inet_csk(sk);
1676 struct sk_buff *skb, *nskb, *next;
1677 int len;
1678 int probe_size;
Ilpo Järvinen91cc17c2007-11-23 19:08:16 +08001679 int size_needed;
John Heffner5d424d52006-03-20 17:53:41 -08001680 int copy;
1681 int mss_now;
1682
1683 /* Not currently probing/verifying,
1684 * not in recovery,
1685 * have enough cwnd, and
1686 * not SACKing (the variable headers throw things off) */
1687 if (!icsk->icsk_mtup.enabled ||
1688 icsk->icsk_mtup.probe_size ||
1689 inet_csk(sk)->icsk_ca_state != TCP_CA_Open ||
1690 tp->snd_cwnd < 11 ||
Ilpo Järvinencabeccb2009-02-28 04:44:38 +00001691 tp->rx_opt.num_sacks || tp->rx_opt.dsack)
John Heffner5d424d52006-03-20 17:53:41 -08001692 return -1;
1693
1694 /* Very simple search strategy: just double the MSS. */
Ilpo Järvinen0c54b852009-03-14 14:23:05 +00001695 mss_now = tcp_current_mss(sk);
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001696 probe_size = 2 * tp->mss_cache;
Ilpo Järvinen91cc17c2007-11-23 19:08:16 +08001697 size_needed = probe_size + (tp->reordering + 1) * tp->mss_cache;
John Heffner5d424d52006-03-20 17:53:41 -08001698 if (probe_size > tcp_mtu_to_mss(sk, icsk->icsk_mtup.search_high)) {
1699 /* TODO: set timer for probe_converge_event */
1700 return -1;
1701 }
1702
1703 /* Have enough data in the send queue to probe? */
Ilpo Järvinen7f9c33e2007-11-23 19:10:56 +08001704 if (tp->write_seq - tp->snd_nxt < size_needed)
John Heffner5d424d52006-03-20 17:53:41 -08001705 return -1;
1706
Ilpo Järvinen91cc17c2007-11-23 19:08:16 +08001707 if (tp->snd_wnd < size_needed)
1708 return -1;
Ilpo Järvinen90840de2007-12-31 04:48:41 -08001709 if (after(tp->snd_nxt + size_needed, tcp_wnd_end(tp)))
Ilpo Järvinen91cc17c2007-11-23 19:08:16 +08001710 return 0;
John Heffner5d424d52006-03-20 17:53:41 -08001711
Ilpo Järvinend67c58e2007-12-02 00:48:01 +02001712 /* Do we need to wait to drain cwnd? With none in flight, don't stall */
1713 if (tcp_packets_in_flight(tp) + 2 > tp->snd_cwnd) {
1714 if (!tcp_packets_in_flight(tp))
John Heffner5d424d52006-03-20 17:53:41 -08001715 return -1;
1716 else
1717 return 0;
1718 }
1719
1720 /* We're allowed to probe. Build it now. */
1721 if ((nskb = sk_stream_alloc_skb(sk, probe_size, GFP_ATOMIC)) == NULL)
1722 return -1;
Hideo Aoki3ab224b2007-12-31 00:11:19 -08001723 sk->sk_wmem_queued += nskb->truesize;
1724 sk_mem_charge(sk, nskb->truesize);
John Heffner5d424d52006-03-20 17:53:41 -08001725
David S. Millerfe067e82007-03-07 12:12:44 -08001726 skb = tcp_send_head(sk);
John Heffner5d424d52006-03-20 17:53:41 -08001727
1728 TCP_SKB_CB(nskb)->seq = TCP_SKB_CB(skb)->seq;
1729 TCP_SKB_CB(nskb)->end_seq = TCP_SKB_CB(skb)->seq + probe_size;
Eric Dumazet4de075e2011-09-27 13:25:05 -04001730 TCP_SKB_CB(nskb)->tcp_flags = TCPHDR_ACK;
John Heffner5d424d52006-03-20 17:53:41 -08001731 TCP_SKB_CB(nskb)->sacked = 0;
1732 nskb->csum = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07001733 nskb->ip_summed = skb->ip_summed;
John Heffner5d424d52006-03-20 17:53:41 -08001734
Ilpo Järvinen50c48172007-12-02 00:48:00 +02001735 tcp_insert_write_queue_before(nskb, skb, sk);
1736
John Heffner5d424d52006-03-20 17:53:41 -08001737 len = 0;
Ilpo Järvinen234b6862007-12-02 00:48:02 +02001738 tcp_for_write_queue_from_safe(skb, next, sk) {
John Heffner5d424d52006-03-20 17:53:41 -08001739 copy = min_t(int, skb->len, probe_size - len);
1740 if (nskb->ip_summed)
1741 skb_copy_bits(skb, 0, skb_put(nskb, copy), copy);
1742 else
1743 nskb->csum = skb_copy_and_csum_bits(skb, 0,
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001744 skb_put(nskb, copy),
1745 copy, nskb->csum);
John Heffner5d424d52006-03-20 17:53:41 -08001746
1747 if (skb->len <= copy) {
1748 /* We've eaten all the data from this skb.
1749 * Throw it away. */
Eric Dumazet4de075e2011-09-27 13:25:05 -04001750 TCP_SKB_CB(nskb)->tcp_flags |= TCP_SKB_CB(skb)->tcp_flags;
David S. Millerfe067e82007-03-07 12:12:44 -08001751 tcp_unlink_write_queue(skb, sk);
Hideo Aoki3ab224b2007-12-31 00:11:19 -08001752 sk_wmem_free_skb(sk, skb);
John Heffner5d424d52006-03-20 17:53:41 -08001753 } else {
Eric Dumazet4de075e2011-09-27 13:25:05 -04001754 TCP_SKB_CB(nskb)->tcp_flags |= TCP_SKB_CB(skb)->tcp_flags &
Changli Gaoa3433f32010-06-12 14:01:43 +00001755 ~(TCPHDR_FIN|TCPHDR_PSH);
John Heffner5d424d52006-03-20 17:53:41 -08001756 if (!skb_shinfo(skb)->nr_frags) {
1757 skb_pull(skb, copy);
Patrick McHardy84fa7932006-08-29 16:44:56 -07001758 if (skb->ip_summed != CHECKSUM_PARTIAL)
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001759 skb->csum = csum_partial(skb->data,
1760 skb->len, 0);
John Heffner5d424d52006-03-20 17:53:41 -08001761 } else {
1762 __pskb_trim_head(skb, copy);
1763 tcp_set_skb_tso_segs(sk, skb, mss_now);
1764 }
1765 TCP_SKB_CB(skb)->seq += copy;
1766 }
1767
1768 len += copy;
Ilpo Järvinen234b6862007-12-02 00:48:02 +02001769
1770 if (len >= probe_size)
1771 break;
John Heffner5d424d52006-03-20 17:53:41 -08001772 }
1773 tcp_init_tso_segs(sk, nskb, nskb->len);
1774
1775 /* We're ready to send. If this fails, the probe will
1776 * be resegmented into mss-sized pieces by tcp_write_xmit(). */
1777 TCP_SKB_CB(nskb)->when = tcp_time_stamp;
1778 if (!tcp_transmit_skb(sk, nskb, 1, GFP_ATOMIC)) {
1779 /* Decrement cwnd here because we are sending
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001780 * effectively two packets. */
John Heffner5d424d52006-03-20 17:53:41 -08001781 tp->snd_cwnd--;
Ilpo Järvinen66f5fe62007-12-31 04:43:57 -08001782 tcp_event_new_data_sent(sk, nskb);
John Heffner5d424d52006-03-20 17:53:41 -08001783
1784 icsk->icsk_mtup.probe_size = tcp_mss_to_mtu(sk, nskb->len);
John Heffner0e7b1362006-03-20 21:32:58 -08001785 tp->mtu_probe.probe_seq_start = TCP_SKB_CB(nskb)->seq;
1786 tp->mtu_probe.probe_seq_end = TCP_SKB_CB(nskb)->end_seq;
John Heffner5d424d52006-03-20 17:53:41 -08001787
1788 return 1;
1789 }
1790
1791 return -1;
1792}
1793
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794/* This routine writes packets to the network. It advances the
1795 * send_head. This happens as incoming acks open up the remote
1796 * window for us.
1797 *
Ilpo Järvinenf8269a42008-12-03 21:24:48 -08001798 * LARGESEND note: !tcp_urg_mode is overkill, only frames between
1799 * snd_up-64k-mss .. snd_up cannot be large. However, taking into
1800 * account rare use of URG, this is not a big flaw.
1801 *
Nandita Dukkipati6ba8a3b2013-03-11 10:00:43 +00001802 * Send at most one packet when push_one > 0. Temporarily ignore
1803 * cwnd limit to force at most one packet out when push_one == 2.
1804
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001805 * Returns true, if no segments are in flight and we have queued segments,
1806 * but cannot send anything now because of SWS or another problem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 */
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001808static bool tcp_write_xmit(struct sock *sk, unsigned int mss_now, int nonagle,
1809 int push_one, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810{
1811 struct tcp_sock *tp = tcp_sk(sk);
David S. Miller92df7b52005-07-05 15:19:06 -07001812 struct sk_buff *skb;
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001813 unsigned int tso_segs, sent_pkts;
1814 int cwnd_quota;
John Heffner5d424d52006-03-20 17:53:41 -08001815 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
David S. Miller92df7b52005-07-05 15:19:06 -07001817 sent_pkts = 0;
John Heffner5d424d52006-03-20 17:53:41 -08001818
Ilpo Järvinend5dd9172008-12-05 22:48:55 -08001819 if (!push_one) {
1820 /* Do MTU probing. */
1821 result = tcp_mtu_probe(sk);
1822 if (!result) {
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001823 return false;
Ilpo Järvinend5dd9172008-12-05 22:48:55 -08001824 } else if (result > 0) {
1825 sent_pkts = 1;
1826 }
John Heffner5d424d52006-03-20 17:53:41 -08001827 }
1828
David S. Millerfe067e82007-03-07 12:12:44 -08001829 while ((skb = tcp_send_head(sk))) {
Herbert Xuc8ac3772005-08-16 20:43:40 -07001830 unsigned int limit;
1831
Eric Dumazet46d3cea2012-07-11 05:50:31 +00001832
Herbert Xub68e9f82005-08-04 19:52:02 -07001833 tso_segs = tcp_init_tso_segs(sk, skb, mss_now);
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001834 BUG_ON(!tso_segs);
David S. Milleraa934662005-07-05 15:20:09 -07001835
Andrew Vaginec342322012-11-15 04:03:17 +00001836 if (unlikely(tp->repair) && tp->repair_queue == TCP_SEND_QUEUE)
1837 goto repair; /* Skip network transmission */
1838
Herbert Xub68e9f82005-08-04 19:52:02 -07001839 cwnd_quota = tcp_cwnd_test(tp, skb);
Nandita Dukkipati6ba8a3b2013-03-11 10:00:43 +00001840 if (!cwnd_quota) {
1841 if (push_one == 2)
1842 /* Force out a loss probe pkt. */
1843 cwnd_quota = 1;
1844 else
1845 break;
1846 }
Herbert Xub68e9f82005-08-04 19:52:02 -07001847
1848 if (unlikely(!tcp_snd_wnd_test(tp, skb, mss_now)))
1849 break;
1850
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001851 if (tso_segs == 1) {
1852 if (unlikely(!tcp_nagle_test(tp, skb, mss_now,
1853 (tcp_skb_is_last(sk, skb) ?
1854 nonagle : TCP_NAGLE_PUSH))))
1855 break;
1856 } else {
Ilpo Järvinend5dd9172008-12-05 22:48:55 -08001857 if (!push_one && tcp_tso_should_defer(sk, skb))
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001858 break;
1859 }
David S. Milleraa934662005-07-05 15:20:09 -07001860
Eric Dumazet46d3cea2012-07-11 05:50:31 +00001861 /* TSQ : sk_wmem_alloc accounts skb truesize,
1862 * including skb overhead. But thats OK.
1863 */
1864 if (atomic_read(&sk->sk_wmem_alloc) >= sysctl_tcp_limit_output_bytes) {
1865 set_bit(TSQ_THROTTLED, &tp->tsq_flags);
1866 break;
1867 }
Herbert Xuc8ac3772005-08-16 20:43:40 -07001868 limit = mss_now;
Ilpo Järvinenf8269a42008-12-03 21:24:48 -08001869 if (tso_segs > 1 && !tcp_urg_mode(tp))
Ilpo Järvinen0e3a4802007-12-24 21:33:45 -08001870 limit = tcp_mss_split_point(sk, skb, mss_now,
Ben Hutchings14853482012-07-30 16:11:42 +00001871 min_t(unsigned int,
1872 cwnd_quota,
1873 sk->sk_gso_max_segs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
Herbert Xuc8ac3772005-08-16 20:43:40 -07001875 if (skb->len > limit &&
Eric Dumazetc4ead4c2010-06-24 01:00:22 +00001876 unlikely(tso_fragment(sk, skb, limit, mss_now, gfp)))
Herbert Xuc8ac3772005-08-16 20:43:40 -07001877 break;
1878
David S. Miller92df7b52005-07-05 15:19:06 -07001879 TCP_SKB_CB(skb)->when = tcp_time_stamp;
David S. Millerc1b4a7e2005-07-05 15:24:38 -07001880
Ilpo Järvinend5dd9172008-12-05 22:48:55 -08001881 if (unlikely(tcp_transmit_skb(sk, skb, 1, gfp)))
David S. Miller92df7b52005-07-05 15:19:06 -07001882 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
Andrew Vaginec342322012-11-15 04:03:17 +00001884repair:
David S. Miller92df7b52005-07-05 15:19:06 -07001885 /* Advance the send_head. This one is sent out.
1886 * This call will increment packets_out.
1887 */
Ilpo Järvinen66f5fe62007-12-31 04:43:57 -08001888 tcp_event_new_data_sent(sk, skb);
David S. Miller92df7b52005-07-05 15:19:06 -07001889
1890 tcp_minshall_update(tp, mss_now, skb);
Nandita Dukkipatia262f0c2011-08-21 20:21:57 +00001891 sent_pkts += tcp_skb_pcount(skb);
Ilpo Järvinend5dd9172008-12-05 22:48:55 -08001892
1893 if (push_one)
1894 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 }
David S. Miller92df7b52005-07-05 15:19:06 -07001896
David S. Milleraa934662005-07-05 15:20:09 -07001897 if (likely(sent_pkts)) {
Yuchung Cheng684bad12012-09-02 17:38:04 +00001898 if (tcp_in_cwnd_reduction(sk))
1899 tp->prr_out += sent_pkts;
Nandita Dukkipati6ba8a3b2013-03-11 10:00:43 +00001900
1901 /* Send one loss probe per tail loss episode. */
1902 if (push_one != 2)
1903 tcp_schedule_loss_probe(sk);
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07001904 tcp_cwnd_validate(sk);
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001905 return false;
David S. Miller92df7b52005-07-05 15:19:06 -07001906 }
Nandita Dukkipati6ba8a3b2013-03-11 10:00:43 +00001907 return (push_one == 2) || (!tp->packets_out && tcp_send_head(sk));
1908}
1909
1910bool tcp_schedule_loss_probe(struct sock *sk)
1911{
1912 struct inet_connection_sock *icsk = inet_csk(sk);
1913 struct tcp_sock *tp = tcp_sk(sk);
1914 u32 timeout, tlp_time_stamp, rto_time_stamp;
1915 u32 rtt = tp->srtt >> 3;
1916
1917 if (WARN_ON(icsk->icsk_pending == ICSK_TIME_EARLY_RETRANS))
1918 return false;
1919 /* No consecutive loss probes. */
1920 if (WARN_ON(icsk->icsk_pending == ICSK_TIME_LOSS_PROBE)) {
1921 tcp_rearm_rto(sk);
1922 return false;
1923 }
1924 /* Don't do any loss probe on a Fast Open connection before 3WHS
1925 * finishes.
1926 */
1927 if (sk->sk_state == TCP_SYN_RECV)
1928 return false;
1929
1930 /* TLP is only scheduled when next timer event is RTO. */
1931 if (icsk->icsk_pending != ICSK_TIME_RETRANS)
1932 return false;
1933
1934 /* Schedule a loss probe in 2*RTT for SACK capable connections
1935 * in Open state, that are either limited by cwnd or application.
1936 */
1937 if (sysctl_tcp_early_retrans < 3 || !rtt || !tp->packets_out ||
1938 !tcp_is_sack(tp) || inet_csk(sk)->icsk_ca_state != TCP_CA_Open)
1939 return false;
1940
1941 if ((tp->snd_cwnd > tcp_packets_in_flight(tp)) &&
1942 tcp_send_head(sk))
1943 return false;
1944
1945 /* Probe timeout is at least 1.5*rtt + TCP_DELACK_MAX to account
1946 * for delayed ack when there's one outstanding packet.
1947 */
1948 timeout = rtt << 1;
1949 if (tp->packets_out == 1)
1950 timeout = max_t(u32, timeout,
1951 (rtt + (rtt >> 1) + TCP_DELACK_MAX));
1952 timeout = max_t(u32, timeout, msecs_to_jiffies(10));
1953
1954 /* If RTO is shorter, just schedule TLP in its place. */
1955 tlp_time_stamp = tcp_time_stamp + timeout;
1956 rto_time_stamp = (u32)inet_csk(sk)->icsk_timeout;
1957 if ((s32)(tlp_time_stamp - rto_time_stamp) > 0) {
1958 s32 delta = rto_time_stamp - tcp_time_stamp;
1959 if (delta > 0)
1960 timeout = delta;
1961 }
1962
1963 inet_csk_reset_xmit_timer(sk, ICSK_TIME_LOSS_PROBE, timeout,
1964 TCP_RTO_MAX);
1965 return true;
1966}
1967
1968/* When probe timeout (PTO) fires, send a new segment if one exists, else
1969 * retransmit the last segment.
1970 */
1971void tcp_send_loss_probe(struct sock *sk)
1972{
Nandita Dukkipati9b717a82013-03-11 10:00:44 +00001973 struct tcp_sock *tp = tcp_sk(sk);
Nandita Dukkipati6ba8a3b2013-03-11 10:00:43 +00001974 struct sk_buff *skb;
1975 int pcount;
1976 int mss = tcp_current_mss(sk);
1977 int err = -1;
1978
1979 if (tcp_send_head(sk) != NULL) {
1980 err = tcp_write_xmit(sk, mss, TCP_NAGLE_OFF, 2, GFP_ATOMIC);
1981 goto rearm_timer;
1982 }
1983
Nandita Dukkipati9b717a82013-03-11 10:00:44 +00001984 /* At most one outstanding TLP retransmission. */
1985 if (tp->tlp_high_seq)
1986 goto rearm_timer;
1987
Nandita Dukkipati6ba8a3b2013-03-11 10:00:43 +00001988 /* Retransmit last segment. */
1989 skb = tcp_write_queue_tail(sk);
1990 if (WARN_ON(!skb))
1991 goto rearm_timer;
1992
1993 pcount = tcp_skb_pcount(skb);
1994 if (WARN_ON(!pcount))
1995 goto rearm_timer;
1996
1997 if ((pcount > 1) && (skb->len > (pcount - 1) * mss)) {
1998 if (unlikely(tcp_fragment(sk, skb, (pcount - 1) * mss, mss)))
1999 goto rearm_timer;
2000 skb = tcp_write_queue_tail(sk);
2001 }
2002
2003 if (WARN_ON(!skb || !tcp_skb_pcount(skb)))
2004 goto rearm_timer;
2005
2006 /* Probe with zero data doesn't trigger fast recovery. */
2007 if (skb->len > 0)
2008 err = __tcp_retransmit_skb(sk, skb);
2009
Nandita Dukkipati9b717a82013-03-11 10:00:44 +00002010 /* Record snd_nxt for loss detection. */
2011 if (likely(!err))
2012 tp->tlp_high_seq = tp->snd_nxt;
2013
Nandita Dukkipati6ba8a3b2013-03-11 10:00:43 +00002014rearm_timer:
2015 inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
2016 inet_csk(sk)->icsk_rto,
2017 TCP_RTO_MAX);
2018
2019 if (likely(!err))
2020 NET_INC_STATS_BH(sock_net(sk),
2021 LINUX_MIB_TCPLOSSPROBES);
2022 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023}
2024
David S. Millera762a982005-07-05 15:18:51 -07002025/* Push out any pending frames which were held back due to
2026 * TCP_CORK or attempt at coalescing tiny packets.
2027 * The socket must be locked by the caller.
2028 */
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002029void __tcp_push_pending_frames(struct sock *sk, unsigned int cur_mss,
2030 int nonagle)
David S. Millera762a982005-07-05 15:18:51 -07002031{
Ilpo Järvinen726e07a2008-12-05 22:43:56 -08002032 /* If we are closed, the bytes will have to remain here.
2033 * In time closedown will finish, we empty the write queue and
2034 * all will be happy.
2035 */
2036 if (unlikely(sk->sk_state == TCP_CLOSE))
2037 return;
2038
Mel Gorman99a1dec2012-07-31 16:44:14 -07002039 if (tcp_write_xmit(sk, cur_mss, nonagle, 0,
2040 sk_gfp_atomic(sk, GFP_ATOMIC)))
Ilpo Järvinen726e07a2008-12-05 22:43:56 -08002041 tcp_check_probe_timer(sk);
David S. Millera762a982005-07-05 15:18:51 -07002042}
2043
David S. Millerc1b4a7e2005-07-05 15:24:38 -07002044/* Send _single_ skb sitting at the send head. This function requires
2045 * true push pending frames to setup probe timer etc.
2046 */
2047void tcp_push_one(struct sock *sk, unsigned int mss_now)
2048{
David S. Millerfe067e82007-03-07 12:12:44 -08002049 struct sk_buff *skb = tcp_send_head(sk);
David S. Millerc1b4a7e2005-07-05 15:24:38 -07002050
2051 BUG_ON(!skb || skb->len < mss_now);
2052
Ilpo Järvinend5dd9172008-12-05 22:48:55 -08002053 tcp_write_xmit(sk, mss_now, TCP_NAGLE_PUSH, 1, sk->sk_allocation);
David S. Millerc1b4a7e2005-07-05 15:24:38 -07002054}
2055
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056/* This function returns the amount that we can raise the
2057 * usable window based on the following constraints
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002058 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 * 1. The window can never be shrunk once it is offered (RFC 793)
2060 * 2. We limit memory per socket
2061 *
2062 * RFC 1122:
2063 * "the suggested [SWS] avoidance algorithm for the receiver is to keep
2064 * RECV.NEXT + RCV.WIN fixed until:
2065 * RCV.BUFF - RCV.USER - RCV.WINDOW >= min(1/2 RCV.BUFF, MSS)"
2066 *
2067 * i.e. don't raise the right edge of the window until you can raise
2068 * it at least MSS bytes.
2069 *
2070 * Unfortunately, the recommended algorithm breaks header prediction,
2071 * since header prediction assumes th->window stays fixed.
2072 *
2073 * Strictly speaking, keeping th->window fixed violates the receiver
2074 * side SWS prevention criteria. The problem is that under this rule
2075 * a stream of single byte packets will cause the right side of the
2076 * window to always advance by a single byte.
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002077 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 * Of course, if the sender implements sender side SWS prevention
2079 * then this will not be a problem.
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002080 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 * BSD seems to make the following compromise:
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002082 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 * If the free space is less than the 1/4 of the maximum
2084 * space available and the free space is less than 1/2 mss,
2085 * then set the window to 0.
2086 * [ Actually, bsd uses MSS and 1/4 of maximal _window_ ]
2087 * Otherwise, just prevent the window from shrinking
2088 * and from being larger than the largest representable value.
2089 *
2090 * This prevents incremental opening of the window in the regime
2091 * where TCP is limited by the speed of the reader side taking
2092 * data out of the TCP receive queue. It does nothing about
2093 * those cases where the window is constrained on the sender side
2094 * because the pipeline is full.
2095 *
2096 * BSD also seems to "accidentally" limit itself to windows that are a
2097 * multiple of MSS, at least until the free space gets quite small.
2098 * This would appear to be a side effect of the mbuf implementation.
2099 * Combining these two algorithms results in the observed behavior
2100 * of having a fixed window size at almost all times.
2101 *
2102 * Below we obtain similar behavior by forcing the offered window to
2103 * a multiple of the mss when it is feasible to do so.
2104 *
2105 * Note, we don't "adjust" for TIMESTAMP or SACK option bytes.
2106 * Regular options like TIMESTAMP are taken into account.
2107 */
2108u32 __tcp_select_window(struct sock *sk)
2109{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002110 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 struct tcp_sock *tp = tcp_sk(sk);
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -08002112 /* MSS for the peer's data. Previous versions used mss_clamp
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 * here. I don't know if the value based on our guesses
2114 * of peer's MSS is better for the performance. It's more correct
2115 * but may be worse for the performance because of rcv_mss
2116 * fluctuations. --SAW 1998/11/1
2117 */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002118 int mss = icsk->icsk_ack.rcv_mss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 int free_space = tcp_space(sk);
2120 int full_space = min_t(int, tp->window_clamp, tcp_full_space(sk));
2121 int window;
2122
2123 if (mss > full_space)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002124 mss = full_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
Eric Dumazetb92edbe2007-12-20 21:48:32 -08002126 if (free_space < (full_space >> 1)) {
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002127 icsk->icsk_ack.quick = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
Glauber Costa180d8cd2011-12-11 21:47:02 +00002129 if (sk_under_memory_pressure(sk))
Ilpo Järvinen056834d2007-12-31 14:57:14 -08002130 tp->rcv_ssthresh = min(tp->rcv_ssthresh,
2131 4U * tp->advmss);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
2133 if (free_space < mss)
2134 return 0;
2135 }
2136
2137 if (free_space > tp->rcv_ssthresh)
2138 free_space = tp->rcv_ssthresh;
2139
2140 /* Don't do rounding if we are using window scaling, since the
2141 * scaled window will not line up with the MSS boundary anyway.
2142 */
2143 window = tp->rcv_wnd;
2144 if (tp->rx_opt.rcv_wscale) {
2145 window = free_space;
2146
2147 /* Advertise enough space so that it won't get scaled away.
2148 * Import case: prevent zero window announcement if
2149 * 1<<rcv_wscale > mss.
2150 */
2151 if (((window >> tp->rx_opt.rcv_wscale) << tp->rx_opt.rcv_wscale) != window)
2152 window = (((window >> tp->rx_opt.rcv_wscale) + 1)
2153 << tp->rx_opt.rcv_wscale);
2154 } else {
2155 /* Get the largest window that is a nice multiple of mss.
2156 * Window clamp already applied above.
2157 * If our current window offering is within 1 mss of the
2158 * free space we just keep it. This prevents the divide
2159 * and multiply from happening most of the time.
2160 * We also don't do any window rounding when the free space
2161 * is too small.
2162 */
2163 if (window <= free_space - mss || window > free_space)
Ilpo Järvinen056834d2007-12-31 14:57:14 -08002164 window = (free_space / mss) * mss;
John Heffner84565072007-04-02 13:56:32 -07002165 else if (mss == full_space &&
Eric Dumazetb92edbe2007-12-20 21:48:32 -08002166 free_space > window + (full_space >> 1))
John Heffner84565072007-04-02 13:56:32 -07002167 window = free_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 }
2169
2170 return window;
2171}
2172
Ilpo Järvinen4a17fc32008-11-24 21:03:43 -08002173/* Collapses two adjacent SKB's during retransmission. */
2174static void tcp_collapse_retrans(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175{
2176 struct tcp_sock *tp = tcp_sk(sk);
David S. Millerfe067e82007-03-07 12:12:44 -08002177 struct sk_buff *next_skb = tcp_write_queue_next(sk, skb);
Ilpo Järvinen058dc332007-12-31 04:51:11 -08002178 int skb_size, next_skb_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
Ilpo Järvinen058dc332007-12-31 04:51:11 -08002180 skb_size = skb->len;
2181 next_skb_size = next_skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
Ilpo Järvinen058dc332007-12-31 04:51:11 -08002183 BUG_ON(tcp_skb_pcount(skb) != 1 || tcp_skb_pcount(next_skb) != 1);
Ilpo Järvinena6963a62007-09-25 22:44:14 -07002184
Ilpo Järvinen058dc332007-12-31 04:51:11 -08002185 tcp_highest_sack_combine(sk, next_skb, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
Ilpo Järvinen058dc332007-12-31 04:51:11 -08002187 tcp_unlink_write_queue(next_skb, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
Ilpo Järvinen058dc332007-12-31 04:51:11 -08002189 skb_copy_from_linear_data(next_skb, skb_put(skb, next_skb_size),
2190 next_skb_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
Ilpo Järvinen058dc332007-12-31 04:51:11 -08002192 if (next_skb->ip_summed == CHECKSUM_PARTIAL)
2193 skb->ip_summed = CHECKSUM_PARTIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
Ilpo Järvinen058dc332007-12-31 04:51:11 -08002195 if (skb->ip_summed != CHECKSUM_PARTIAL)
2196 skb->csum = csum_block_add(skb->csum, next_skb->csum, skb_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
Ilpo Järvinen058dc332007-12-31 04:51:11 -08002198 /* Update sequence range on original skb. */
2199 TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(next_skb)->end_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
Ilpo Järvinene6c7d082009-02-28 04:44:35 +00002201 /* Merge over control information. This moves PSH/FIN etc. over */
Eric Dumazet4de075e2011-09-27 13:25:05 -04002202 TCP_SKB_CB(skb)->tcp_flags |= TCP_SKB_CB(next_skb)->tcp_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
Ilpo Järvinen058dc332007-12-31 04:51:11 -08002204 /* All done, get rid of second SKB and account for it so
2205 * packet counting does not break.
2206 */
2207 TCP_SKB_CB(skb)->sacked |= TCP_SKB_CB(next_skb)->sacked & TCPCB_EVER_RETRANS;
Ilpo Järvinenb7689202007-09-20 11:37:19 -07002208
Ilpo Järvinen058dc332007-12-31 04:51:11 -08002209 /* changed transmit queue under us so clear hints */
Ilpo Järvinenef9da472008-09-20 21:25:15 -07002210 tcp_clear_retrans_hints_partial(tp);
2211 if (next_skb == tp->retransmit_skb_hint)
2212 tp->retransmit_skb_hint = skb;
Ilpo Järvinen058dc332007-12-31 04:51:11 -08002213
Ilpo Järvinen797108d2009-04-01 23:15:17 +00002214 tcp_adjust_pcount(sk, next_skb, tcp_skb_pcount(next_skb));
2215
Ilpo Järvinen058dc332007-12-31 04:51:11 -08002216 sk_wmem_free_skb(sk, next_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217}
2218
Andi Kleen67edfef2009-07-21 23:00:40 +00002219/* Check if coalescing SKBs is legal. */
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002220static bool tcp_can_collapse(const struct sock *sk, const struct sk_buff *skb)
Ilpo Järvinen4a17fc32008-11-24 21:03:43 -08002221{
2222 if (tcp_skb_pcount(skb) > 1)
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002223 return false;
Ilpo Järvinen4a17fc32008-11-24 21:03:43 -08002224 /* TODO: SACK collapsing could be used to remove this condition */
2225 if (skb_shinfo(skb)->nr_frags != 0)
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002226 return false;
Ilpo Järvinen4a17fc32008-11-24 21:03:43 -08002227 if (skb_cloned(skb))
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002228 return false;
Ilpo Järvinen4a17fc32008-11-24 21:03:43 -08002229 if (skb == tcp_send_head(sk))
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002230 return false;
Ilpo Järvinen4a17fc32008-11-24 21:03:43 -08002231 /* Some heurestics for collapsing over SACK'd could be invented */
2232 if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED)
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002233 return false;
Ilpo Järvinen4a17fc32008-11-24 21:03:43 -08002234
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002235 return true;
Ilpo Järvinen4a17fc32008-11-24 21:03:43 -08002236}
2237
Andi Kleen67edfef2009-07-21 23:00:40 +00002238/* Collapse packets in the retransmit queue to make to create
2239 * less packets on the wire. This is only done on retransmission.
2240 */
Ilpo Järvinen4a17fc32008-11-24 21:03:43 -08002241static void tcp_retrans_try_collapse(struct sock *sk, struct sk_buff *to,
2242 int space)
2243{
2244 struct tcp_sock *tp = tcp_sk(sk);
2245 struct sk_buff *skb = to, *tmp;
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002246 bool first = true;
Ilpo Järvinen4a17fc32008-11-24 21:03:43 -08002247
2248 if (!sysctl_tcp_retrans_collapse)
2249 return;
Eric Dumazet4de075e2011-09-27 13:25:05 -04002250 if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN)
Ilpo Järvinen4a17fc32008-11-24 21:03:43 -08002251 return;
2252
2253 tcp_for_write_queue_from_safe(skb, tmp, sk) {
2254 if (!tcp_can_collapse(sk, skb))
2255 break;
2256
2257 space -= skb->len;
2258
2259 if (first) {
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002260 first = false;
Ilpo Järvinen4a17fc32008-11-24 21:03:43 -08002261 continue;
2262 }
2263
2264 if (space < 0)
2265 break;
2266 /* Punt if not enough space exists in the first SKB for
2267 * the data in the second
2268 */
Eric Dumazeta21d4572012-04-10 20:30:48 +00002269 if (skb->len > skb_availroom(to))
Ilpo Järvinen4a17fc32008-11-24 21:03:43 -08002270 break;
2271
2272 if (after(TCP_SKB_CB(skb)->end_seq, tcp_wnd_end(tp)))
2273 break;
2274
2275 tcp_collapse_retrans(sk, to);
2276 }
2277}
2278
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279/* This retransmits one SKB. Policy decisions and retransmit queue
2280 * state updates are done by the caller. Returns non-zero if an
2281 * error occurred which prevented the send.
2282 */
Yuchung Cheng93b174a2012-12-06 08:45:32 +00002283int __tcp_retransmit_skb(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284{
2285 struct tcp_sock *tp = tcp_sk(sk);
John Heffner5d424d52006-03-20 17:53:41 -08002286 struct inet_connection_sock *icsk = inet_csk(sk);
Sridhar Samudrala7d227cd22008-05-21 16:42:20 -07002287 unsigned int cur_mss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
John Heffner5d424d52006-03-20 17:53:41 -08002289 /* Inconslusive MTU probe */
2290 if (icsk->icsk_mtup.probe_size) {
2291 icsk->icsk_mtup.probe_size = 0;
2292 }
2293
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 /* Do not sent more than we queued. 1/4 is reserved for possible
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -08002295 * copying overhead: fragmentation, tunneling, mangling etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 */
2297 if (atomic_read(&sk->sk_wmem_alloc) >
2298 min(sk->sk_wmem_queued + (sk->sk_wmem_queued >> 2), sk->sk_sndbuf))
2299 return -EAGAIN;
2300
2301 if (before(TCP_SKB_CB(skb)->seq, tp->snd_una)) {
2302 if (before(TCP_SKB_CB(skb)->end_seq, tp->snd_una))
2303 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 if (tcp_trim_head(sk, skb, tp->snd_una - TCP_SKB_CB(skb)->seq))
2305 return -ENOMEM;
2306 }
2307
Sridhar Samudrala7d227cd22008-05-21 16:42:20 -07002308 if (inet_csk(sk)->icsk_af_ops->rebuild_header(sk))
2309 return -EHOSTUNREACH; /* Routing failure or similar. */
2310
Ilpo Järvinen0c54b852009-03-14 14:23:05 +00002311 cur_mss = tcp_current_mss(sk);
Sridhar Samudrala7d227cd22008-05-21 16:42:20 -07002312
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 /* If receiver has shrunk his window, and skb is out of
2314 * new window, do not retransmit it. The exception is the
2315 * case, when window is shrunk to zero. In this case
2316 * our retransmit serves as a zero window probe.
2317 */
Joe Perches9d4fb272009-11-23 10:41:23 -08002318 if (!before(TCP_SKB_CB(skb)->seq, tcp_wnd_end(tp)) &&
2319 TCP_SKB_CB(skb)->seq != tp->snd_una)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 return -EAGAIN;
2321
2322 if (skb->len > cur_mss) {
David S. Miller846998a2005-08-04 19:52:01 -07002323 if (tcp_fragment(sk, skb, cur_mss, cur_mss))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 return -ENOMEM; /* We'll try again later. */
Ilpo Järvinen02276f32009-02-28 04:44:31 +00002325 } else {
Ilpo Järvinen9eb93622009-04-01 23:18:20 +00002326 int oldpcount = tcp_skb_pcount(skb);
2327
2328 if (unlikely(oldpcount > 1)) {
2329 tcp_init_tso_segs(sk, skb, cur_mss);
2330 tcp_adjust_pcount(sk, skb, oldpcount - tcp_skb_pcount(skb));
2331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 }
2333
Ilpo Järvinen4a17fc32008-11-24 21:03:43 -08002334 tcp_retrans_try_collapse(sk, skb, cur_mss);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 /* Some Solaris stacks overoptimize and ignore the FIN on a
2337 * retransmit when old data is attached. So strip it off
2338 * since it is cheap to do so and saves bytes on the network.
2339 */
Stephen Hemminger2de979b2007-03-08 20:45:19 -08002340 if (skb->len > 0 &&
Eric Dumazet4de075e2011-09-27 13:25:05 -04002341 (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN) &&
Stephen Hemminger2de979b2007-03-08 20:45:19 -08002342 tp->snd_una == (TCP_SKB_CB(skb)->end_seq - 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 if (!pskb_trim(skb, 0)) {
Ilpo Järvinene870a8e2008-01-03 20:39:01 -08002344 /* Reuse, even though it does some unnecessary work */
2345 tcp_init_nondata_skb(skb, TCP_SKB_CB(skb)->end_seq - 1,
Eric Dumazet4de075e2011-09-27 13:25:05 -04002346 TCP_SKB_CB(skb)->tcp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 skb->ip_summed = CHECKSUM_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 }
2349 }
2350
2351 /* Make a copy, if the first transmission SKB clone we made
2352 * is still in somebody's hands, else make a clone.
2353 */
2354 TCP_SKB_CB(skb)->when = tcp_time_stamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355
Eric Dumazet117632e2011-12-03 21:39:53 +00002356 /* make sure skb->data is aligned on arches that require it */
2357 if (unlikely(NET_IP_ALIGN && ((unsigned long)skb->data & 3))) {
2358 struct sk_buff *nskb = __pskb_copy(skb, MAX_TCP_HEADER,
2359 GFP_ATOMIC);
Yuchung Cheng93b174a2012-12-06 08:45:32 +00002360 return nskb ? tcp_transmit_skb(sk, nskb, 0, GFP_ATOMIC) :
2361 -ENOBUFS;
Eric Dumazet117632e2011-12-03 21:39:53 +00002362 } else {
Yuchung Cheng93b174a2012-12-06 08:45:32 +00002363 return tcp_transmit_skb(sk, skb, 1, GFP_ATOMIC);
Eric Dumazet117632e2011-12-03 21:39:53 +00002364 }
Yuchung Cheng93b174a2012-12-06 08:45:32 +00002365}
2366
2367int tcp_retransmit_skb(struct sock *sk, struct sk_buff *skb)
2368{
2369 struct tcp_sock *tp = tcp_sk(sk);
2370 int err = __tcp_retransmit_skb(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
2372 if (err == 0) {
2373 /* Update global TCP statistics. */
Pavel Emelyanov81cc8a72008-07-16 20:22:04 -07002374 TCP_INC_STATS(sock_net(sk), TCP_MIB_RETRANSSEGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
2376 tp->total_retrans++;
2377
2378#if FASTRETRANS_DEBUG > 0
Ilpo Järvinen056834d2007-12-31 14:57:14 -08002379 if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_RETRANS) {
Joe Perchese87cc472012-05-13 21:56:26 +00002380 net_dbg_ratelimited("retrans_out leaked\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 }
2382#endif
Ilpo Järvinenb08d6cb2007-10-11 17:36:13 -07002383 if (!tp->retrans_out)
2384 tp->lost_retrans_low = tp->snd_nxt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 TCP_SKB_CB(skb)->sacked |= TCPCB_RETRANS;
2386 tp->retrans_out += tcp_skb_pcount(skb);
2387
2388 /* Save stamp of the first retransmit. */
2389 if (!tp->retrans_stamp)
2390 tp->retrans_stamp = TCP_SKB_CB(skb)->when;
2391
Yuchung Chengc24f6912011-02-07 12:57:04 +00002392 tp->undo_retrans += tcp_skb_pcount(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
2394 /* snd_nxt is stored to detect loss of retransmitted segment,
2395 * see tcp_input.c tcp_sacktag_write_queue().
2396 */
2397 TCP_SKB_CB(skb)->ack_seq = tp->snd_nxt;
2398 }
2399 return err;
2400}
2401
Andi Kleen67edfef2009-07-21 23:00:40 +00002402/* Check if we forward retransmits are possible in the current
2403 * window/congestion state.
2404 */
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002405static bool tcp_can_forward_retransmit(struct sock *sk)
Ilpo Järvinenb5afe7b2008-09-20 21:21:54 -07002406{
2407 const struct inet_connection_sock *icsk = inet_csk(sk);
Eric Dumazetcf533ea2011-10-21 05:22:42 -04002408 const struct tcp_sock *tp = tcp_sk(sk);
Ilpo Järvinenb5afe7b2008-09-20 21:21:54 -07002409
2410 /* Forward retransmissions are possible only during Recovery. */
2411 if (icsk->icsk_ca_state != TCP_CA_Recovery)
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002412 return false;
Ilpo Järvinenb5afe7b2008-09-20 21:21:54 -07002413
2414 /* No forward retransmissions in Reno are possible. */
2415 if (tcp_is_reno(tp))
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002416 return false;
Ilpo Järvinenb5afe7b2008-09-20 21:21:54 -07002417
2418 /* Yeah, we have to make difficult choice between forward transmission
2419 * and retransmission... Both ways have their merits...
2420 *
2421 * For now we do not retransmit anything, while we have some new
2422 * segments to send. In the other cases, follow rule 3 for
2423 * NextSeg() specified in RFC3517.
2424 */
2425
2426 if (tcp_may_send_now(sk))
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002427 return false;
Ilpo Järvinenb5afe7b2008-09-20 21:21:54 -07002428
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002429 return true;
Ilpo Järvinenb5afe7b2008-09-20 21:21:54 -07002430}
2431
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432/* This gets called after a retransmit timeout, and the initially
2433 * retransmitted data is acknowledged. It tries to continue
2434 * resending the rest of the retransmit queue, until either
2435 * we've sent it all or the congestion window limit is reached.
2436 * If doing SACK, the first ACK which comes back for a timeout
2437 * based retransmit packet might feed us FACK information again.
2438 * If so, we use it to avoid unnecessarily retransmissions.
2439 */
2440void tcp_xmit_retransmit_queue(struct sock *sk)
2441{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002442 const struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 struct tcp_sock *tp = tcp_sk(sk);
2444 struct sk_buff *skb;
Ilpo Järvinen0e1c54c2008-09-20 21:24:21 -07002445 struct sk_buff *hole = NULL;
Ilpo Järvinen618d9f22008-09-20 21:26:22 -07002446 u32 last_lost;
Ilpo Järvinen61eb55f2008-09-20 21:22:59 -07002447 int mib_idx;
Ilpo Järvinen0e1c54c2008-09-20 21:24:21 -07002448 int fwd_rexmitting = 0;
Stephen Hemminger6a438bb2005-11-10 17:14:59 -08002449
Ilpo Järvinen45e77d32010-07-19 01:16:18 +00002450 if (!tp->packets_out)
2451 return;
2452
Ilpo Järvinen08ebd172008-09-20 21:23:49 -07002453 if (!tp->lost_out)
2454 tp->retransmit_high = tp->snd_una;
2455
Ilpo Järvinen618d9f22008-09-20 21:26:22 -07002456 if (tp->retransmit_skb_hint) {
Stephen Hemminger6a438bb2005-11-10 17:14:59 -08002457 skb = tp->retransmit_skb_hint;
Ilpo Järvinen618d9f22008-09-20 21:26:22 -07002458 last_lost = TCP_SKB_CB(skb)->end_seq;
2459 if (after(last_lost, tp->retransmit_high))
2460 last_lost = tp->retransmit_high;
2461 } else {
David S. Millerfe067e82007-03-07 12:12:44 -08002462 skb = tcp_write_queue_head(sk);
Ilpo Järvinen618d9f22008-09-20 21:26:22 -07002463 last_lost = tp->snd_una;
2464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465
Ilpo Järvinen08ebd172008-09-20 21:23:49 -07002466 tcp_for_write_queue_from(skb, sk) {
2467 __u8 sacked = TCP_SKB_CB(skb)->sacked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
Ilpo Järvinen08ebd172008-09-20 21:23:49 -07002469 if (skb == tcp_send_head(sk))
2470 break;
2471 /* we could do better than to assign each time */
Ilpo Järvinen0e1c54c2008-09-20 21:24:21 -07002472 if (hole == NULL)
2473 tp->retransmit_skb_hint = skb;
Stephen Hemminger6a438bb2005-11-10 17:14:59 -08002474
Ilpo Järvinen08ebd172008-09-20 21:23:49 -07002475 /* Assume this retransmit will generate
2476 * only one packet for congestion window
2477 * calculation purposes. This works because
2478 * tcp_retransmit_skb() will chop up the
2479 * packet to be MSS sized and all the
2480 * packet counting works out.
2481 */
2482 if (tcp_packets_in_flight(tp) >= tp->snd_cwnd)
2483 return;
Ilpo Järvinen0e1c54c2008-09-20 21:24:21 -07002484
2485 if (fwd_rexmitting) {
2486begin_fwd:
2487 if (!before(TCP_SKB_CB(skb)->seq, tcp_highest_sack_seq(tp)))
2488 break;
2489 mib_idx = LINUX_MIB_TCPFORWARDRETRANS;
2490
2491 } else if (!before(TCP_SKB_CB(skb)->seq, tp->retransmit_high)) {
Ilpo Järvinen618d9f22008-09-20 21:26:22 -07002492 tp->retransmit_high = last_lost;
Ilpo Järvinen0e1c54c2008-09-20 21:24:21 -07002493 if (!tcp_can_forward_retransmit(sk))
2494 break;
2495 /* Backtrack if necessary to non-L'ed skb */
2496 if (hole != NULL) {
2497 skb = hole;
2498 hole = NULL;
2499 }
2500 fwd_rexmitting = 1;
2501 goto begin_fwd;
2502
2503 } else if (!(sacked & TCPCB_LOST)) {
Ilpo Järvinenac11ba72009-02-28 04:44:27 +00002504 if (hole == NULL && !(sacked & (TCPCB_SACKED_RETRANS|TCPCB_SACKED_ACKED)))
Ilpo Järvinen0e1c54c2008-09-20 21:24:21 -07002505 hole = skb;
Ilpo Järvinen08ebd172008-09-20 21:23:49 -07002506 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507
Ilpo Järvinen0e1c54c2008-09-20 21:24:21 -07002508 } else {
Ilpo Järvinen618d9f22008-09-20 21:26:22 -07002509 last_lost = TCP_SKB_CB(skb)->end_seq;
Ilpo Järvinen0e1c54c2008-09-20 21:24:21 -07002510 if (icsk->icsk_ca_state != TCP_CA_Loss)
2511 mib_idx = LINUX_MIB_TCPFASTRETRANS;
2512 else
2513 mib_idx = LINUX_MIB_TCPSLOWSTARTRETRANS;
2514 }
2515
2516 if (sacked & (TCPCB_SACKED_ACKED|TCPCB_SACKED_RETRANS))
Ilpo Järvinen08ebd172008-09-20 21:23:49 -07002517 continue;
Pavel Emelyanov40b215e2008-07-03 01:05:41 -07002518
Eric Dumazet09e9b812012-01-25 04:44:20 +00002519 if (tcp_retransmit_skb(sk, skb)) {
2520 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPRETRANSFAIL);
Ilpo Järvinen08ebd172008-09-20 21:23:49 -07002521 return;
Eric Dumazet09e9b812012-01-25 04:44:20 +00002522 }
Ilpo Järvinen08ebd172008-09-20 21:23:49 -07002523 NET_INC_STATS_BH(sock_net(sk), mib_idx);
2524
Yuchung Cheng684bad12012-09-02 17:38:04 +00002525 if (tcp_in_cwnd_reduction(sk))
Nandita Dukkipatia262f0c2011-08-21 20:21:57 +00002526 tp->prr_out += tcp_skb_pcount(skb);
2527
Ilpo Järvinen08ebd172008-09-20 21:23:49 -07002528 if (skb == tcp_write_queue_head(sk))
2529 inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
2530 inet_csk(sk)->icsk_rto,
2531 TCP_RTO_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533}
2534
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535/* Send a fin. The caller locks the socket for us. This cannot be
2536 * allowed to fail queueing a FIN frame under any circumstances.
2537 */
2538void tcp_send_fin(struct sock *sk)
2539{
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002540 struct tcp_sock *tp = tcp_sk(sk);
David S. Millerfe067e82007-03-07 12:12:44 -08002541 struct sk_buff *skb = tcp_write_queue_tail(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 int mss_now;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002543
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 /* Optimization, tack on the FIN if we have a queue of
2545 * unsent frames. But be careful about outgoing SACKS
2546 * and IP options.
2547 */
Ilpo Järvinen0c54b852009-03-14 14:23:05 +00002548 mss_now = tcp_current_mss(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549
David S. Millerfe067e82007-03-07 12:12:44 -08002550 if (tcp_send_head(sk) != NULL) {
Eric Dumazet4de075e2011-09-27 13:25:05 -04002551 TCP_SKB_CB(skb)->tcp_flags |= TCPHDR_FIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 TCP_SKB_CB(skb)->end_seq++;
2553 tp->write_seq++;
2554 } else {
2555 /* Socket is locked, keep trying until memory is available. */
2556 for (;;) {
Wu Fengguangaa133072009-09-02 23:45:45 -07002557 skb = alloc_skb_fclone(MAX_TCP_HEADER,
2558 sk->sk_allocation);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 if (skb)
2560 break;
2561 yield();
2562 }
2563
2564 /* Reserve space for headers and prepare control bits. */
2565 skb_reserve(skb, MAX_TCP_HEADER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 /* FIN eats a sequence byte, write_seq advanced by tcp_queue_skb(). */
Ilpo Järvinene870a8e2008-01-03 20:39:01 -08002567 tcp_init_nondata_skb(skb, tp->write_seq,
Changli Gaoa3433f32010-06-12 14:01:43 +00002568 TCPHDR_ACK | TCPHDR_FIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 tcp_queue_skb(sk, skb);
2570 }
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002571 __tcp_push_pending_frames(sk, mss_now, TCP_NAGLE_OFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572}
2573
2574/* We get here when a process closes a file descriptor (either due to
2575 * an explicit close() or as a byproduct of exit()'ing) and there
2576 * was unread data in the receive queue. This behavior is recommended
Gerrit Renker65bb7232007-04-28 21:21:46 -07002577 * by RFC 2525, section 2.17. -DaveM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 */
Al Virodd0fc662005-10-07 07:46:04 +01002579void tcp_send_active_reset(struct sock *sk, gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 struct sk_buff *skb;
2582
2583 /* NOTE: No TCP options attached and we never retransmit this. */
2584 skb = alloc_skb(MAX_TCP_HEADER, priority);
2585 if (!skb) {
Pavel Emelyanov4e673442008-07-16 20:30:14 -07002586 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTFAILED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 return;
2588 }
2589
2590 /* Reserve space for headers and prepare control bits. */
2591 skb_reserve(skb, MAX_TCP_HEADER);
Ilpo Järvinene870a8e2008-01-03 20:39:01 -08002592 tcp_init_nondata_skb(skb, tcp_acceptable_seq(sk),
Changli Gaoa3433f32010-06-12 14:01:43 +00002593 TCPHDR_ACK | TCPHDR_RST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 /* Send it off. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 TCP_SKB_CB(skb)->when = tcp_time_stamp;
David S. Millerdfb4b9d2005-12-06 16:24:52 -08002596 if (tcp_transmit_skb(sk, skb, 0, priority))
Pavel Emelyanov4e673442008-07-16 20:30:14 -07002597 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTFAILED);
Sridhar Samudrala26af65c2008-06-04 15:19:35 -07002598
Pavel Emelyanov81cc8a72008-07-16 20:22:04 -07002599 TCP_INC_STATS(sock_net(sk), TCP_MIB_OUTRSTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600}
2601
Andi Kleen67edfef2009-07-21 23:00:40 +00002602/* Send a crossed SYN-ACK during socket establishment.
2603 * WARNING: This routine must only be called when we have already sent
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 * a SYN packet that crossed the incoming SYN that caused this routine
2605 * to get called. If this assumption fails then the initial rcv_wnd
2606 * and rcv_wscale values will not be correct.
2607 */
2608int tcp_send_synack(struct sock *sk)
2609{
Ilpo Järvinen056834d2007-12-31 14:57:14 -08002610 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611
David S. Millerfe067e82007-03-07 12:12:44 -08002612 skb = tcp_write_queue_head(sk);
Eric Dumazet4de075e2011-09-27 13:25:05 -04002613 if (skb == NULL || !(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN)) {
Joe Perches91df42b2012-05-15 14:11:54 +00002614 pr_debug("%s: wrong queue state\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 return -EFAULT;
2616 }
Eric Dumazet4de075e2011-09-27 13:25:05 -04002617 if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_ACK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 if (skb_cloned(skb)) {
2619 struct sk_buff *nskb = skb_copy(skb, GFP_ATOMIC);
2620 if (nskb == NULL)
2621 return -ENOMEM;
David S. Millerfe067e82007-03-07 12:12:44 -08002622 tcp_unlink_write_queue(skb, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 skb_header_release(nskb);
David S. Millerfe067e82007-03-07 12:12:44 -08002624 __tcp_add_write_queue_head(sk, nskb);
Hideo Aoki3ab224b2007-12-31 00:11:19 -08002625 sk_wmem_free_skb(sk, skb);
2626 sk->sk_wmem_queued += nskb->truesize;
2627 sk_mem_charge(sk, nskb->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 skb = nskb;
2629 }
2630
Eric Dumazet4de075e2011-09-27 13:25:05 -04002631 TCP_SKB_CB(skb)->tcp_flags |= TCPHDR_ACK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 TCP_ECN_send_synack(tcp_sk(sk), skb);
2633 }
2634 TCP_SKB_CB(skb)->when = tcp_time_stamp;
David S. Millerdfb4b9d2005-12-06 16:24:52 -08002635 return tcp_transmit_skb(sk, skb, 1, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636}
2637
Eric Dumazet4aea39c2012-06-03 20:33:21 +00002638/**
2639 * tcp_make_synack - Prepare a SYN-ACK.
2640 * sk: listener socket
2641 * dst: dst entry attached to the SYNACK
2642 * req: request_sock pointer
Eric Dumazet4aea39c2012-06-03 20:33:21 +00002643 *
2644 * Allocate one skb and build a SYNACK packet.
2645 * @dst is consumed : Caller should not use it again.
2646 */
Ilpo Järvinen056834d2007-12-31 14:57:14 -08002647struct sk_buff *tcp_make_synack(struct sock *sk, struct dst_entry *dst,
William Allen Simpsone6b4d112009-12-02 18:07:39 +00002648 struct request_sock *req,
Jerry Chu83368862012-08-31 12:29:12 +00002649 struct tcp_fastopen_cookie *foc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650{
William Allen Simpsonbd0388a2009-12-02 18:23:05 +00002651 struct tcp_out_options opts;
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07002652 struct inet_request_sock *ireq = inet_rsk(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 struct tcp_sock *tp = tcp_sk(sk);
2654 struct tcphdr *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 struct sk_buff *skb;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08002656 struct tcp_md5sig_key *md5;
William Allen Simpsonbd0388a2009-12-02 18:23:05 +00002657 int tcp_header_size;
Tom Quetchenbachf5fff5d2008-09-21 00:21:51 -07002658 int mss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659
Christoph Paasch1a2c6182013-03-17 08:23:34 +00002660 skb = alloc_skb(MAX_TCP_HEADER + 15, sk_gfp_atomic(sk, GFP_ATOMIC));
Eric Dumazet4aea39c2012-06-03 20:33:21 +00002661 if (unlikely(!skb)) {
2662 dst_release(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 return NULL;
Eric Dumazet4aea39c2012-06-03 20:33:21 +00002664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 /* Reserve space for headers. */
2666 skb_reserve(skb, MAX_TCP_HEADER);
2667
Eric Dumazet4aea39c2012-06-03 20:33:21 +00002668 skb_dst_set(skb, dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
David S. Miller0dbaee32010-12-13 12:52:14 -08002670 mss = dst_metric_advmss(dst);
Tom Quetchenbachf5fff5d2008-09-21 00:21:51 -07002671 if (tp->rx_opt.user_mss && tp->rx_opt.user_mss < mss)
2672 mss = tp->rx_opt.user_mss;
2673
Adam Langley33ad7982008-07-19 00:04:31 -07002674 if (req->rcv_wnd == 0) { /* ignored for retransmitted syns */
2675 __u8 rcv_wscale;
2676 /* Set this up on the first call only */
2677 req->window_clamp = tp->window_clamp ? : dst_metric(dst, RTAX_WINDOW);
Hagen Paul Pfeifere88c64f2010-08-19 06:33:05 +00002678
2679 /* limit the window selection if the user enforce a smaller rx buffer */
2680 if (sk->sk_userlocks & SOCK_RCVBUF_LOCK &&
2681 (req->window_clamp > tcp_full_space(sk) || req->window_clamp == 0))
2682 req->window_clamp = tcp_full_space(sk);
2683
Adam Langley33ad7982008-07-19 00:04:31 -07002684 /* tcp_full_space because it is guaranteed to be the first packet */
2685 tcp_select_initial_window(tcp_full_space(sk),
Tom Quetchenbachf5fff5d2008-09-21 00:21:51 -07002686 mss - (ireq->tstamp_ok ? TCPOLEN_TSTAMP_ALIGNED : 0),
Adam Langley33ad7982008-07-19 00:04:31 -07002687 &req->rcv_wnd,
2688 &req->window_clamp,
2689 ireq->wscale_ok,
laurent chavey31d12922009-12-15 11:15:28 +00002690 &rcv_wscale,
2691 dst_metric(dst, RTAX_INITRWND));
Adam Langley33ad7982008-07-19 00:04:31 -07002692 ireq->rcv_wscale = rcv_wscale;
2693 }
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08002694
Adam Langley33ad7982008-07-19 00:04:31 -07002695 memset(&opts, 0, sizeof(opts));
Florian Westphal8b5f12d2008-10-26 23:10:12 -07002696#ifdef CONFIG_SYN_COOKIES
2697 if (unlikely(req->cookie_ts))
2698 TCP_SKB_CB(skb)->when = cookie_init_timestamp(req);
2699 else
2700#endif
Adam Langley33ad7982008-07-19 00:04:31 -07002701 TCP_SKB_CB(skb)->when = tcp_time_stamp;
Christoph Paasch1a2c6182013-03-17 08:23:34 +00002702 tcp_header_size = tcp_synack_options(sk, req, mss, skb, &opts, &md5,
2703 foc) + sizeof(*th);
Adam Langley33ad7982008-07-19 00:04:31 -07002704
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07002705 skb_push(skb, tcp_header_size);
2706 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07002708 th = tcp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 memset(th, 0, sizeof(struct tcphdr));
2710 th->syn = 1;
2711 th->ack = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 TCP_ECN_make_synack(req, th);
KOVACS Krisztiana3116ac52008-10-01 07:46:49 -07002713 th->source = ireq->loc_port;
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07002714 th->dest = ireq->rmt_port;
Ilpo Järvinene870a8e2008-01-03 20:39:01 -08002715 /* Setting of flags are superfluous here for callers (and ECE is
2716 * not even correctly set)
2717 */
2718 tcp_init_nondata_skb(skb, tcp_rsk(req)->snt_isn,
Changli Gaoa3433f32010-06-12 14:01:43 +00002719 TCPHDR_SYN | TCPHDR_ACK);
William Allen Simpson4957faade2009-12-02 18:25:27 +00002720
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 th->seq = htonl(TCP_SKB_CB(skb)->seq);
Jerry Chu83368862012-08-31 12:29:12 +00002722 /* XXX data is queued and acked as is. No buffer/window check */
2723 th->ack_seq = htonl(tcp_rsk(req)->rcv_nxt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724
2725 /* RFC1323: The window in SYN & SYN/ACK segments is never scaled. */
Ilpo Järvinen600ff0c2007-02-13 12:42:11 -08002726 th->window = htons(min(req->rcv_wnd, 65535U));
William Allen Simpsonbd0388a2009-12-02 18:23:05 +00002727 tcp_options_write((__be32 *)(th + 1), tp, &opts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 th->doff = (tcp_header_size >> 2);
Tom Herbertaa2ea052010-04-22 07:00:24 +00002729 TCP_ADD_STATS(sock_net(sk), TCP_MIB_OUTSEGS, tcp_skb_pcount(skb));
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08002730
2731#ifdef CONFIG_TCP_MD5SIG
2732 /* Okay, we have all we need - do the md5 hash if needed */
2733 if (md5) {
William Allen Simpsonbd0388a2009-12-02 18:23:05 +00002734 tcp_rsk(req)->af_specific->calc_md5_hash(opts.hash_location,
Adam Langley49a72df2008-07-19 00:01:42 -07002735 md5, NULL, req, skb);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08002736 }
2737#endif
2738
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 return skb;
2740}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00002741EXPORT_SYMBOL(tcp_make_synack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742
Andi Kleen67edfef2009-07-21 23:00:40 +00002743/* Do all connect socket setups that can be done AF independent. */
Pavel Emelyanov370816a2012-04-19 03:40:01 +00002744void tcp_connect_init(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745{
Eric Dumazetcf533ea2011-10-21 05:22:42 -04002746 const struct dst_entry *dst = __sk_dst_get(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 struct tcp_sock *tp = tcp_sk(sk);
2748 __u8 rcv_wscale;
2749
2750 /* We'll fix this up when we get a response from the other end.
2751 * See tcp_input.c:tcp_rcv_state_process case TCP_SYN_SENT.
2752 */
2753 tp->tcp_header_len = sizeof(struct tcphdr) +
David S. Millerbb5b7c12009-12-15 20:56:42 -08002754 (sysctl_tcp_timestamps ? TCPOLEN_TSTAMP_ALIGNED : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08002756#ifdef CONFIG_TCP_MD5SIG
2757 if (tp->af_specific->md5_lookup(sk, sk) != NULL)
2758 tp->tcp_header_len += TCPOLEN_MD5SIG_ALIGNED;
2759#endif
2760
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 /* If user gave his TCP_MAXSEG, record it to clamp */
2762 if (tp->rx_opt.user_mss)
2763 tp->rx_opt.mss_clamp = tp->rx_opt.user_mss;
2764 tp->max_window = 0;
John Heffner5d424d52006-03-20 17:53:41 -08002765 tcp_mtup_init(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 tcp_sync_mss(sk, dst_mtu(dst));
2767
2768 if (!tp->window_clamp)
2769 tp->window_clamp = dst_metric(dst, RTAX_WINDOW);
David S. Miller0dbaee32010-12-13 12:52:14 -08002770 tp->advmss = dst_metric_advmss(dst);
Tom Quetchenbachf5fff5d2008-09-21 00:21:51 -07002771 if (tp->rx_opt.user_mss && tp->rx_opt.user_mss < tp->advmss)
2772 tp->advmss = tp->rx_opt.user_mss;
2773
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 tcp_initialize_rcv_mss(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775
Hagen Paul Pfeifere88c64f2010-08-19 06:33:05 +00002776 /* limit the window selection if the user enforce a smaller rx buffer */
2777 if (sk->sk_userlocks & SOCK_RCVBUF_LOCK &&
2778 (tp->window_clamp > tcp_full_space(sk) || tp->window_clamp == 0))
2779 tp->window_clamp = tcp_full_space(sk);
2780
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 tcp_select_initial_window(tcp_full_space(sk),
2782 tp->advmss - (tp->rx_opt.ts_recent_stamp ? tp->tcp_header_len - sizeof(struct tcphdr) : 0),
2783 &tp->rcv_wnd,
2784 &tp->window_clamp,
David S. Millerbb5b7c12009-12-15 20:56:42 -08002785 sysctl_tcp_window_scaling,
laurent chavey31d12922009-12-15 11:15:28 +00002786 &rcv_wscale,
2787 dst_metric(dst, RTAX_INITRWND));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788
2789 tp->rx_opt.rcv_wscale = rcv_wscale;
2790 tp->rcv_ssthresh = tp->rcv_wnd;
2791
2792 sk->sk_err = 0;
2793 sock_reset_flag(sk, SOCK_DONE);
2794 tp->snd_wnd = 0;
Hantzis Fotisee7537b2009-03-02 22:42:02 -08002795 tcp_init_wl(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 tp->snd_una = tp->write_seq;
2797 tp->snd_sml = tp->write_seq;
Ilpo Järvinen33f5f572008-10-07 14:43:06 -07002798 tp->snd_up = tp->write_seq;
Pavel Emelyanov370816a2012-04-19 03:40:01 +00002799 tp->snd_nxt = tp->write_seq;
Pavel Emelyanovee995282012-04-19 03:40:39 +00002800
2801 if (likely(!tp->repair))
2802 tp->rcv_nxt = 0;
2803 tp->rcv_wup = tp->rcv_nxt;
2804 tp->copied_seq = tp->rcv_nxt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002806 inet_csk(sk)->icsk_rto = TCP_TIMEOUT_INIT;
2807 inet_csk(sk)->icsk_retransmits = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 tcp_clear_retrans(tp);
2809}
2810
Yuchung Cheng783237e2012-07-19 06:43:07 +00002811static void tcp_connect_queue_skb(struct sock *sk, struct sk_buff *skb)
2812{
2813 struct tcp_sock *tp = tcp_sk(sk);
2814 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
2815
2816 tcb->end_seq += skb->len;
2817 skb_header_release(skb);
2818 __tcp_add_write_queue_tail(sk, skb);
2819 sk->sk_wmem_queued += skb->truesize;
2820 sk_mem_charge(sk, skb->truesize);
2821 tp->write_seq = tcb->end_seq;
2822 tp->packets_out += tcp_skb_pcount(skb);
2823}
2824
2825/* Build and send a SYN with data and (cached) Fast Open cookie. However,
2826 * queue a data-only packet after the regular SYN, such that regular SYNs
2827 * are retransmitted on timeouts. Also if the remote SYN-ACK acknowledges
2828 * only the SYN sequence, the data are retransmitted in the first ACK.
2829 * If cookie is not cached or other error occurs, falls back to send a
2830 * regular SYN with Fast Open cookie request option.
2831 */
2832static int tcp_send_syn_data(struct sock *sk, struct sk_buff *syn)
2833{
2834 struct tcp_sock *tp = tcp_sk(sk);
2835 struct tcp_fastopen_request *fo = tp->fastopen_req;
Yuchung Chengaab48742012-07-19 06:43:10 +00002836 int syn_loss = 0, space, i, err = 0, iovlen = fo->data->msg_iovlen;
Yuchung Cheng783237e2012-07-19 06:43:07 +00002837 struct sk_buff *syn_data = NULL, *data;
Yuchung Chengaab48742012-07-19 06:43:10 +00002838 unsigned long last_syn_loss = 0;
Yuchung Cheng783237e2012-07-19 06:43:07 +00002839
Yuchung Cheng67da22d2012-07-19 06:43:11 +00002840 tp->rx_opt.mss_clamp = tp->advmss; /* If MSS is not cached */
Yuchung Chengaab48742012-07-19 06:43:10 +00002841 tcp_fastopen_cache_get(sk, &tp->rx_opt.mss_clamp, &fo->cookie,
2842 &syn_loss, &last_syn_loss);
2843 /* Recurring FO SYN losses: revert to regular handshake temporarily */
2844 if (syn_loss > 1 &&
2845 time_before(jiffies, last_syn_loss + (60*HZ << syn_loss))) {
2846 fo->cookie.len = -1;
2847 goto fallback;
2848 }
2849
Yuchung Cheng67da22d2012-07-19 06:43:11 +00002850 if (sysctl_tcp_fastopen & TFO_CLIENT_NO_COOKIE)
2851 fo->cookie.len = -1;
2852 else if (fo->cookie.len <= 0)
Yuchung Cheng783237e2012-07-19 06:43:07 +00002853 goto fallback;
2854
2855 /* MSS for SYN-data is based on cached MSS and bounded by PMTU and
2856 * user-MSS. Reserve maximum option space for middleboxes that add
2857 * private TCP options. The cost is reduced data space in SYN :(
2858 */
2859 if (tp->rx_opt.user_mss && tp->rx_opt.user_mss < tp->rx_opt.mss_clamp)
2860 tp->rx_opt.mss_clamp = tp->rx_opt.user_mss;
Yuchung Cheng1b63edd2013-02-22 08:59:06 +00002861 space = __tcp_mtu_to_mss(sk, inet_csk(sk)->icsk_pmtu_cookie) -
Yuchung Cheng783237e2012-07-19 06:43:07 +00002862 MAX_TCP_OPTION_SPACE;
2863
2864 syn_data = skb_copy_expand(syn, skb_headroom(syn), space,
2865 sk->sk_allocation);
2866 if (syn_data == NULL)
2867 goto fallback;
2868
2869 for (i = 0; i < iovlen && syn_data->len < space; ++i) {
2870 struct iovec *iov = &fo->data->msg_iov[i];
2871 unsigned char __user *from = iov->iov_base;
2872 int len = iov->iov_len;
2873
2874 if (syn_data->len + len > space)
2875 len = space - syn_data->len;
2876 else if (i + 1 == iovlen)
2877 /* No more data pending in inet_wait_for_connect() */
2878 fo->data = NULL;
2879
2880 if (skb_add_data(syn_data, from, len))
2881 goto fallback;
2882 }
2883
2884 /* Queue a data-only packet after the regular SYN for retransmission */
2885 data = pskb_copy(syn_data, sk->sk_allocation);
2886 if (data == NULL)
2887 goto fallback;
2888 TCP_SKB_CB(data)->seq++;
2889 TCP_SKB_CB(data)->tcp_flags &= ~TCPHDR_SYN;
2890 TCP_SKB_CB(data)->tcp_flags = (TCPHDR_ACK|TCPHDR_PSH);
2891 tcp_connect_queue_skb(sk, data);
2892 fo->copied = data->len;
2893
2894 if (tcp_transmit_skb(sk, syn_data, 0, sk->sk_allocation) == 0) {
Yuchung Cheng67da22d2012-07-19 06:43:11 +00002895 tp->syn_data = (fo->copied > 0);
Yuchung Cheng783237e2012-07-19 06:43:07 +00002896 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPFASTOPENACTIVE);
2897 goto done;
2898 }
2899 syn_data = NULL;
2900
2901fallback:
2902 /* Send a regular SYN with Fast Open cookie request option */
2903 if (fo->cookie.len > 0)
2904 fo->cookie.len = 0;
2905 err = tcp_transmit_skb(sk, syn, 1, sk->sk_allocation);
2906 if (err)
2907 tp->syn_fastopen = 0;
2908 kfree_skb(syn_data);
2909done:
2910 fo->cookie.len = -1; /* Exclude Fast Open option for SYN retries */
2911 return err;
2912}
2913
Andi Kleen67edfef2009-07-21 23:00:40 +00002914/* Build a SYN and send it off. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915int tcp_connect(struct sock *sk)
2916{
2917 struct tcp_sock *tp = tcp_sk(sk);
2918 struct sk_buff *buff;
Eric Parisee586812010-11-16 11:52:49 +00002919 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920
2921 tcp_connect_init(sk);
2922
Andrey Vagin2b916472012-11-22 01:13:58 +00002923 if (unlikely(tp->repair)) {
2924 tcp_finish_connect(sk, NULL);
2925 return 0;
2926 }
2927
David S. Millerd179cd12005-08-17 14:57:30 -07002928 buff = alloc_skb_fclone(MAX_TCP_HEADER + 15, sk->sk_allocation);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 if (unlikely(buff == NULL))
2930 return -ENOBUFS;
2931
2932 /* Reserve space for headers. */
2933 skb_reserve(buff, MAX_TCP_HEADER);
2934
Changli Gaoa3433f32010-06-12 14:01:43 +00002935 tcp_init_nondata_skb(buff, tp->write_seq++, TCPHDR_SYN);
Yuchung Cheng783237e2012-07-19 06:43:07 +00002936 tp->retrans_stamp = TCP_SKB_CB(buff)->when = tcp_time_stamp;
2937 tcp_connect_queue_skb(sk, buff);
Ilpo Järvinene870a8e2008-01-03 20:39:01 -08002938 TCP_ECN_send_syn(sk, buff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939
Yuchung Cheng783237e2012-07-19 06:43:07 +00002940 /* Send off SYN; include data in Fast Open. */
2941 err = tp->fastopen_req ? tcp_send_syn_data(sk, buff) :
2942 tcp_transmit_skb(sk, buff, 1, sk->sk_allocation);
Eric Parisee586812010-11-16 11:52:49 +00002943 if (err == -ECONNREFUSED)
2944 return err;
Wei Yongjunbd37a082006-08-07 21:04:15 -07002945
2946 /* We change tp->snd_nxt after the tcp_transmit_skb() call
2947 * in order to make this packet get counted in tcpOutSegs.
2948 */
2949 tp->snd_nxt = tp->write_seq;
2950 tp->pushed_seq = tp->write_seq;
Pavel Emelyanov81cc8a72008-07-16 20:22:04 -07002951 TCP_INC_STATS(sock_net(sk), TCP_MIB_ACTIVEOPENS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952
2953 /* Timer for repeating the SYN until an answer. */
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -07002954 inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
2955 inet_csk(sk)->icsk_rto, TCP_RTO_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 return 0;
2957}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00002958EXPORT_SYMBOL(tcp_connect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959
2960/* Send out a delayed ack, the caller does the policy checking
2961 * to see if we should even be here. See tcp_input.c:tcp_ack_snd_check()
2962 * for details.
2963 */
2964void tcp_send_delayed_ack(struct sock *sk)
2965{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002966 struct inet_connection_sock *icsk = inet_csk(sk);
2967 int ato = icsk->icsk_ack.ato;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 unsigned long timeout;
2969
2970 if (ato > TCP_DELACK_MIN) {
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002971 const struct tcp_sock *tp = tcp_sk(sk);
Ilpo Järvinen056834d2007-12-31 14:57:14 -08002972 int max_ato = HZ / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973
Ilpo Järvinen056834d2007-12-31 14:57:14 -08002974 if (icsk->icsk_ack.pingpong ||
2975 (icsk->icsk_ack.pending & ICSK_ACK_PUSHED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 max_ato = TCP_DELACK_MAX;
2977
2978 /* Slow path, intersegment interval is "high". */
2979
2980 /* If some rtt estimate is known, use it to bound delayed ack.
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002981 * Do not use inet_csk(sk)->icsk_rto here, use results of rtt measurements
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 * directly.
2983 */
2984 if (tp->srtt) {
Ilpo Järvinen056834d2007-12-31 14:57:14 -08002985 int rtt = max(tp->srtt >> 3, TCP_DELACK_MIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986
2987 if (rtt < max_ato)
2988 max_ato = rtt;
2989 }
2990
2991 ato = min(ato, max_ato);
2992 }
2993
2994 /* Stay within the limit we were given */
2995 timeout = jiffies + ato;
2996
2997 /* Use new timeout only if there wasn't a older one earlier. */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002998 if (icsk->icsk_ack.pending & ICSK_ACK_TIMER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 /* If delack timer was blocked or is about to expire,
3000 * send ACK now.
3001 */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07003002 if (icsk->icsk_ack.blocked ||
3003 time_before_eq(icsk->icsk_ack.timeout, jiffies + (ato >> 2))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 tcp_send_ack(sk);
3005 return;
3006 }
3007
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07003008 if (!time_before(timeout, icsk->icsk_ack.timeout))
3009 timeout = icsk->icsk_ack.timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 }
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07003011 icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER;
3012 icsk->icsk_ack.timeout = timeout;
3013 sk_reset_timer(sk, &icsk->icsk_delack_timer, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014}
3015
3016/* This routine sends an ack and also updates the window. */
3017void tcp_send_ack(struct sock *sk)
3018{
Ilpo Järvinen058dc332007-12-31 04:51:11 -08003019 struct sk_buff *buff;
3020
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 /* If we have been reset, we may not send again. */
Ilpo Järvinen058dc332007-12-31 04:51:11 -08003022 if (sk->sk_state == TCP_CLOSE)
3023 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024
Ilpo Järvinen058dc332007-12-31 04:51:11 -08003025 /* We are not putting this on the write queue, so
3026 * tcp_transmit_skb() will set the ownership to this
3027 * sock.
3028 */
Mel Gorman99a1dec2012-07-31 16:44:14 -07003029 buff = alloc_skb(MAX_TCP_HEADER, sk_gfp_atomic(sk, GFP_ATOMIC));
Ilpo Järvinen058dc332007-12-31 04:51:11 -08003030 if (buff == NULL) {
3031 inet_csk_schedule_ack(sk);
3032 inet_csk(sk)->icsk_ack.ato = TCP_ATO_MIN;
3033 inet_csk_reset_xmit_timer(sk, ICSK_TIME_DACK,
3034 TCP_DELACK_MAX, TCP_RTO_MAX);
3035 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 }
Ilpo Järvinen058dc332007-12-31 04:51:11 -08003037
3038 /* Reserve space for headers and prepare control bits. */
3039 skb_reserve(buff, MAX_TCP_HEADER);
Changli Gaoa3433f32010-06-12 14:01:43 +00003040 tcp_init_nondata_skb(buff, tcp_acceptable_seq(sk), TCPHDR_ACK);
Ilpo Järvinen058dc332007-12-31 04:51:11 -08003041
3042 /* Send it off, this clears delayed acks for us. */
Ilpo Järvinen058dc332007-12-31 04:51:11 -08003043 TCP_SKB_CB(buff)->when = tcp_time_stamp;
Mel Gorman99a1dec2012-07-31 16:44:14 -07003044 tcp_transmit_skb(sk, buff, 0, sk_gfp_atomic(sk, GFP_ATOMIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045}
3046
3047/* This routine sends a packet with an out of date sequence
3048 * number. It assumes the other end will try to ack it.
3049 *
3050 * Question: what should we make while urgent mode?
3051 * 4.4BSD forces sending single byte of data. We cannot send
3052 * out of window data, because we have SND.NXT==SND.MAX...
3053 *
3054 * Current solution: to send TWO zero-length segments in urgent mode:
3055 * one is with SEG.SEQ=SND.UNA to deliver urgent pointer, another is
3056 * out-of-date with SND.UNA-1 to probe window.
3057 */
3058static int tcp_xmit_probe_skb(struct sock *sk, int urgent)
3059{
3060 struct tcp_sock *tp = tcp_sk(sk);
3061 struct sk_buff *skb;
3062
3063 /* We don't queue it, tcp_transmit_skb() sets ownership. */
Mel Gorman99a1dec2012-07-31 16:44:14 -07003064 skb = alloc_skb(MAX_TCP_HEADER, sk_gfp_atomic(sk, GFP_ATOMIC));
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003065 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 return -1;
3067
3068 /* Reserve space for headers and set control bits. */
3069 skb_reserve(skb, MAX_TCP_HEADER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 /* Use a previous sequence. This should cause the other
3071 * end to send an ack. Don't queue or clone SKB, just
3072 * send it.
3073 */
Changli Gaoa3433f32010-06-12 14:01:43 +00003074 tcp_init_nondata_skb(skb, tp->snd_una - !urgent, TCPHDR_ACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 TCP_SKB_CB(skb)->when = tcp_time_stamp;
David S. Millerdfb4b9d2005-12-06 16:24:52 -08003076 return tcp_transmit_skb(sk, skb, 0, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077}
3078
Pavel Emelyanovee995282012-04-19 03:40:39 +00003079void tcp_send_window_probe(struct sock *sk)
3080{
3081 if (sk->sk_state == TCP_ESTABLISHED) {
3082 tcp_sk(sk)->snd_wl1 = tcp_sk(sk)->rcv_nxt - 1;
Pavel Emelyanovc0e88ff2012-04-19 03:41:01 +00003083 tcp_sk(sk)->snd_nxt = tcp_sk(sk)->write_seq;
Pavel Emelyanovee995282012-04-19 03:40:39 +00003084 tcp_xmit_probe_skb(sk, 0);
3085 }
3086}
3087
Andi Kleen67edfef2009-07-21 23:00:40 +00003088/* Initiate keepalive or window probe from timer. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089int tcp_write_wakeup(struct sock *sk)
3090{
Ilpo Järvinen058dc332007-12-31 04:51:11 -08003091 struct tcp_sock *tp = tcp_sk(sk);
3092 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093
Ilpo Järvinen058dc332007-12-31 04:51:11 -08003094 if (sk->sk_state == TCP_CLOSE)
3095 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096
Ilpo Järvinen058dc332007-12-31 04:51:11 -08003097 if ((skb = tcp_send_head(sk)) != NULL &&
3098 before(TCP_SKB_CB(skb)->seq, tcp_wnd_end(tp))) {
3099 int err;
Ilpo Järvinen0c54b852009-03-14 14:23:05 +00003100 unsigned int mss = tcp_current_mss(sk);
Ilpo Järvinen058dc332007-12-31 04:51:11 -08003101 unsigned int seg_size = tcp_wnd_end(tp) - TCP_SKB_CB(skb)->seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102
Ilpo Järvinen058dc332007-12-31 04:51:11 -08003103 if (before(tp->pushed_seq, TCP_SKB_CB(skb)->end_seq))
3104 tp->pushed_seq = TCP_SKB_CB(skb)->end_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105
Ilpo Järvinen058dc332007-12-31 04:51:11 -08003106 /* We are probing the opening of a window
3107 * but the window size is != 0
3108 * must have been a result SWS avoidance ( sender )
3109 */
3110 if (seg_size < TCP_SKB_CB(skb)->end_seq - TCP_SKB_CB(skb)->seq ||
3111 skb->len > mss) {
3112 seg_size = min(seg_size, mss);
Eric Dumazet4de075e2011-09-27 13:25:05 -04003113 TCP_SKB_CB(skb)->tcp_flags |= TCPHDR_PSH;
Ilpo Järvinen058dc332007-12-31 04:51:11 -08003114 if (tcp_fragment(sk, skb, seg_size, mss))
3115 return -1;
3116 } else if (!tcp_skb_pcount(skb))
3117 tcp_set_skb_tso_segs(sk, skb, mss);
3118
Eric Dumazet4de075e2011-09-27 13:25:05 -04003119 TCP_SKB_CB(skb)->tcp_flags |= TCPHDR_PSH;
Ilpo Järvinen058dc332007-12-31 04:51:11 -08003120 TCP_SKB_CB(skb)->when = tcp_time_stamp;
3121 err = tcp_transmit_skb(sk, skb, 1, GFP_ATOMIC);
3122 if (!err)
3123 tcp_event_new_data_sent(sk, skb);
3124 return err;
3125 } else {
Ilpo Järvinen33f5f572008-10-07 14:43:06 -07003126 if (between(tp->snd_up, tp->snd_una + 1, tp->snd_una + 0xFFFF))
Ilpo Järvinen058dc332007-12-31 04:51:11 -08003127 tcp_xmit_probe_skb(sk, 1);
3128 return tcp_xmit_probe_skb(sk, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130}
3131
3132/* A window probe timeout has occurred. If window is not closed send
3133 * a partial packet else a zero probe.
3134 */
3135void tcp_send_probe0(struct sock *sk)
3136{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07003137 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 struct tcp_sock *tp = tcp_sk(sk);
3139 int err;
3140
3141 err = tcp_write_wakeup(sk);
3142
David S. Millerfe067e82007-03-07 12:12:44 -08003143 if (tp->packets_out || !tcp_send_head(sk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 /* Cancel probe timer, if it is not required. */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03003145 icsk->icsk_probes_out = 0;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07003146 icsk->icsk_backoff = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 return;
3148 }
3149
3150 if (err <= 0) {
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07003151 if (icsk->icsk_backoff < sysctl_tcp_retries2)
3152 icsk->icsk_backoff++;
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03003153 icsk->icsk_probes_out++;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003154 inet_csk_reset_xmit_timer(sk, ICSK_TIME_PROBE0,
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -07003155 min(icsk->icsk_rto << icsk->icsk_backoff, TCP_RTO_MAX),
3156 TCP_RTO_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 } else {
3158 /* If packet was not sent due to local congestion,
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03003159 * do not backoff and do not remember icsk_probes_out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 * Let local senders to fight for local resources.
3161 *
3162 * Use accumulated backoff yet.
3163 */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03003164 if (!icsk->icsk_probes_out)
3165 icsk->icsk_probes_out = 1;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003166 inet_csk_reset_xmit_timer(sk, ICSK_TIME_PROBE0,
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07003167 min(icsk->icsk_rto << icsk->icsk_backoff,
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -07003168 TCP_RESOURCE_PROBE_INTERVAL),
3169 TCP_RTO_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 }
3171}