RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / net / ipv4 / tcp_output.c
blob8e98d8217bb414f6d990e3de4b17bce8907d3322
1 /*
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.
6 * Implementation of the Transmission Control Protocol(TCP).
8 * Version: $Id: tcp_output.c,v 1.146 2002/02/01 22:01:04 davem Exp $
10 * Authors: Ross Biro
11 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
12 * Mark Evans, <evansmp@uhura.aston.ac.uk>
13 * Corey Minyard <wf-rch!minyard@relay.EU.net>
14 * Florian La Roche, <flla@stud.uni-sb.de>
15 * Charles Hedrick, <hedrick@klinzhai.rutgers.edu>
16 * Linus Torvalds, <torvalds@cs.helsinki.fi>
17 * Alan Cox, <gw4pts@gw4pts.ampr.org>
18 * Matthew Dillon, <dillon@apollo.west.oic.com>
19 * Arnt Gulbrandsen, <agulbra@nvg.unit.no>
20 * Jorge Cwik, <jorge@laser.satlink.net>
24 * Changes: Pedro Roque : Retransmit queue handled by TCP.
25 * : Fragmentation on mtu decrease
26 * : Segment collapse on retransmit
27 * : AF independence
29 * Linus Torvalds : send_delayed_ack
30 * David S. Miller : Charge memory using the right skb
31 * during syn/ack processing.
32 * David S. Miller : Output engine completely rewritten.
33 * Andrea Arcangeli: SYNACK carry ts_recent in tsecr.
34 * Cacophonix Gaul : draft-minshall-nagle-01
35 * J Hadi Salim : ECN support
39 #include <net/tcp.h>
41 #include <linux/compiler.h>
42 #include <linux/module.h>
44 #define IsReno(tp) ((tp)->rx_opt.sack_ok == 0)
45 #define IsFack(tp) ((tp)->rx_opt.sack_ok & 2)
47 /* People can turn this off for buggy TCP's found in printers etc. */
48 int sysctl_tcp_retrans_collapse __read_mostly = 1;
50 /* People can turn this on to work with those rare, broken TCPs that
51 * interpret the window field as a signed quantity.
53 int sysctl_tcp_workaround_signed_windows __read_mostly = 0;
55 /* This limits the percentage of the congestion window which we
56 * will allow a single TSO frame to consume. Building TSO frames
57 * which are too large can cause TCP streams to be bursty.
59 int sysctl_tcp_tso_win_divisor __read_mostly = 3;
61 int sysctl_tcp_mtu_probing __read_mostly = 0;
62 int sysctl_tcp_base_mss __read_mostly = 512;
64 /* By default, RFC2861 behavior. */
65 int sysctl_tcp_slow_start_after_idle __read_mostly = 1;
67 static void update_send_head(struct sock *sk, struct sk_buff *skb)
69 struct tcp_sock *tp = tcp_sk(sk);
71 tcp_advance_send_head(sk, skb);
72 tp->snd_nxt = TCP_SKB_CB(skb)->end_seq;
73 tcp_packets_out_inc(sk, skb);
76 /* SND.NXT, if window was not shrunk.
77 * If window has been shrunk, what should we make? It is not clear at all.
78 * Using SND.UNA we will fail to open window, SND.NXT is out of window. :-(
79 * Anything in between SND.UNA...SND.UNA+SND.WND also can be already
80 * invalid. OK, let's make this for now:
82 static inline __u32 tcp_acceptable_seq(struct sock *sk)
84 struct tcp_sock *tp = tcp_sk(sk);
86 if (!before(tp->snd_una+tp->snd_wnd, tp->snd_nxt))
87 return tp->snd_nxt;
88 else
89 return tp->snd_una+tp->snd_wnd;
92 /* Calculate mss to advertise in SYN segment.
93 * RFC1122, RFC1063, draft-ietf-tcpimpl-pmtud-01 state that:
95 * 1. It is independent of path mtu.
96 * 2. Ideally, it is maximal possible segment size i.e. 65535-40.
97 * 3. For IPv4 it is reasonable to calculate it from maximal MTU of
98 * attached devices, because some buggy hosts are confused by
99 * large MSS.
100 * 4. We do not make 3, we advertise MSS, calculated from first
101 * hop device mtu, but allow to raise it to ip_rt_min_advmss.
102 * This may be overridden via information stored in routing table.
103 * 5. Value 65535 for MSS is valid in IPv6 and means "as large as possible,
104 * probably even Jumbo".
106 static __u16 tcp_advertise_mss(struct sock *sk)
108 struct tcp_sock *tp = tcp_sk(sk);
109 struct dst_entry *dst = __sk_dst_get(sk);
110 int mss = tp->advmss;
112 if (dst && dst_metric(dst, RTAX_ADVMSS) < mss) {
113 mss = dst_metric(dst, RTAX_ADVMSS);
114 tp->advmss = mss;
117 return (__u16)mss;
120 /* RFC2861. Reset CWND after idle period longer RTO to "restart window".
121 * This is the first part of cwnd validation mechanism. */
122 static void tcp_cwnd_restart(struct sock *sk, struct dst_entry *dst)
124 struct tcp_sock *tp = tcp_sk(sk);
125 s32 delta = tcp_time_stamp - tp->lsndtime;
126 u32 restart_cwnd = tcp_init_cwnd(tp, dst);
127 u32 cwnd = tp->snd_cwnd;
129 tcp_ca_event(sk, CA_EVENT_CWND_RESTART);
131 tp->snd_ssthresh = tcp_current_ssthresh(sk);
132 restart_cwnd = min(restart_cwnd, cwnd);
134 while ((delta -= inet_csk(sk)->icsk_rto) > 0 && cwnd > restart_cwnd)
135 cwnd >>= 1;
136 tp->snd_cwnd = max(cwnd, restart_cwnd);
137 tp->snd_cwnd_stamp = tcp_time_stamp;
138 tp->snd_cwnd_used = 0;
141 static void tcp_event_data_sent(struct tcp_sock *tp,
142 struct sk_buff *skb, struct sock *sk)
144 struct inet_connection_sock *icsk = inet_csk(sk);
145 const u32 now = tcp_time_stamp;
147 if (sysctl_tcp_slow_start_after_idle &&
148 (!tp->packets_out && (s32)(now - tp->lsndtime) > icsk->icsk_rto))
149 tcp_cwnd_restart(sk, __sk_dst_get(sk));
151 tp->lsndtime = now;
153 /* If it is a reply for ato after last received
154 * packet, enter pingpong mode.
156 if ((u32)(now - icsk->icsk_ack.lrcvtime) < icsk->icsk_ack.ato)
157 icsk->icsk_ack.pingpong = 1;
160 static inline void tcp_event_ack_sent(struct sock *sk, unsigned int pkts)
162 tcp_dec_quickack_mode(sk, pkts);
163 inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
166 /* Determine a window scaling and initial window to offer.
167 * Based on the assumption that the given amount of space
168 * will be offered. Store the results in the tp structure.
169 * NOTE: for smooth operation initial space offering should
170 * be a multiple of mss if possible. We assume here that mss >= 1.
171 * This MUST be enforced by all callers.
173 void tcp_select_initial_window(int __space, __u32 mss,
174 __u32 *rcv_wnd, __u32 *window_clamp,
175 int wscale_ok, __u8 *rcv_wscale)
177 unsigned int space = (__space < 0 ? 0 : __space);
179 /* If no clamp set the clamp to the max possible scaled window */
180 if (*window_clamp == 0)
181 (*window_clamp) = (65535 << 14);
182 space = min(*window_clamp, space);
184 /* Quantize space offering to a multiple of mss if possible. */
185 if (space > mss)
186 space = (space / mss) * mss;
188 /* NOTE: offering an initial window larger than 32767
189 * will break some buggy TCP stacks. If the admin tells us
190 * it is likely we could be speaking with such a buggy stack
191 * we will truncate our initial window offering to 32K-1
192 * unless the remote has sent us a window scaling option,
193 * which we interpret as a sign the remote TCP is not
194 * misinterpreting the window field as a signed quantity.
196 if (sysctl_tcp_workaround_signed_windows)
197 (*rcv_wnd) = min(space, MAX_TCP_WINDOW);
198 else
199 (*rcv_wnd) = space;
201 (*rcv_wscale) = 0;
202 if (wscale_ok) {
203 /* Set window scaling on max possible window
204 * See RFC1323 for an explanation of the limit to 14
206 space = max_t(u32, sysctl_tcp_rmem[2], sysctl_rmem_max);
207 space = min_t(u32, space, *window_clamp);
208 while (space > 65535 && (*rcv_wscale) < 14) {
209 space >>= 1;
210 (*rcv_wscale)++;
214 /* Set initial window to value enough for senders,
215 * following RFC2414. Senders, not following this RFC,
216 * will be satisfied with 2.
218 if (mss > (1<<*rcv_wscale)) {
219 int init_cwnd = 4;
220 if (mss > 1460*3)
221 init_cwnd = 2;
222 else if (mss > 1460)
223 init_cwnd = 3;
224 if (*rcv_wnd > init_cwnd*mss)
225 *rcv_wnd = init_cwnd*mss;
228 /* Set the clamp no higher than max representable value */
229 (*window_clamp) = min(65535U << (*rcv_wscale), *window_clamp);
232 /* Chose a new window to advertise, update state in tcp_sock for the
233 * socket, and return result with RFC1323 scaling applied. The return
234 * value can be stuffed directly into th->window for an outgoing
235 * frame.
237 static u16 tcp_select_window(struct sock *sk)
239 struct tcp_sock *tp = tcp_sk(sk);
240 u32 cur_win = tcp_receive_window(tp);
241 u32 new_win = __tcp_select_window(sk);
243 /* Never shrink the offered window */
244 if (new_win < cur_win) {
245 /* Danger Will Robinson!
246 * Don't update rcv_wup/rcv_wnd here or else
247 * we will not be able to advertise a zero
248 * window in time. --DaveM
250 * Relax Will Robinson.
252 new_win = ALIGN(cur_win, 1 << tp->rx_opt.rcv_wscale);
254 tp->rcv_wnd = new_win;
255 tp->rcv_wup = tp->rcv_nxt;
257 /* Make sure we do not exceed the maximum possible
258 * scaled window.
260 if (!tp->rx_opt.rcv_wscale && sysctl_tcp_workaround_signed_windows)
261 new_win = min(new_win, MAX_TCP_WINDOW);
262 else
263 new_win = min(new_win, (65535U << tp->rx_opt.rcv_wscale));
265 /* RFC1323 scaling applied */
266 new_win >>= tp->rx_opt.rcv_wscale;
268 /* If we advertise zero window, disable fast path. */
269 if (new_win == 0)
270 tp->pred_flags = 0;
272 return new_win;
275 static void tcp_build_and_update_options(__be32 *ptr, struct tcp_sock *tp,
276 __u32 tstamp, __u8 **md5_hash)
278 if (tp->rx_opt.tstamp_ok) {
279 *ptr++ = htonl((TCPOPT_NOP << 24) |
280 (TCPOPT_NOP << 16) |
281 (TCPOPT_TIMESTAMP << 8) |
282 TCPOLEN_TIMESTAMP);
283 *ptr++ = htonl(tstamp);
284 *ptr++ = htonl(tp->rx_opt.ts_recent);
286 if (tp->rx_opt.eff_sacks) {
287 struct tcp_sack_block *sp = tp->rx_opt.dsack ? tp->duplicate_sack : tp->selective_acks;
288 int this_sack;
290 *ptr++ = htonl((TCPOPT_NOP << 24) |
291 (TCPOPT_NOP << 16) |
292 (TCPOPT_SACK << 8) |
293 (TCPOLEN_SACK_BASE + (tp->rx_opt.eff_sacks *
294 TCPOLEN_SACK_PERBLOCK)));
296 for (this_sack = 0; this_sack < tp->rx_opt.eff_sacks; this_sack++) {
297 *ptr++ = htonl(sp[this_sack].start_seq);
298 *ptr++ = htonl(sp[this_sack].end_seq);
301 if (tp->rx_opt.dsack) {
302 tp->rx_opt.dsack = 0;
303 tp->rx_opt.eff_sacks--;
306 #ifdef CONFIG_TCP_MD5SIG
307 if (md5_hash) {
308 *ptr++ = htonl((TCPOPT_NOP << 24) |
309 (TCPOPT_NOP << 16) |
310 (TCPOPT_MD5SIG << 8) |
311 TCPOLEN_MD5SIG);
312 *md5_hash = (__u8 *)ptr;
314 #endif
317 /* Construct a tcp options header for a SYN or SYN_ACK packet.
318 * If this is every changed make sure to change the definition of
319 * MAX_SYN_SIZE to match the new maximum number of options that you
320 * can generate.
322 * Note - that with the RFC2385 TCP option, we make room for the
323 * 16 byte MD5 hash. This will be filled in later, so the pointer for the
324 * location to be filled is passed back up.
326 static void tcp_syn_build_options(__be32 *ptr, int mss, int ts, int sack,
327 int offer_wscale, int wscale, __u32 tstamp,
328 __u32 ts_recent, __u8 **md5_hash)
330 /* We always get an MSS option.
331 * The option bytes which will be seen in normal data
332 * packets should timestamps be used, must be in the MSS
333 * advertised. But we subtract them from tp->mss_cache so
334 * that calculations in tcp_sendmsg are simpler etc.
335 * So account for this fact here if necessary. If we
336 * don't do this correctly, as a receiver we won't
337 * recognize data packets as being full sized when we
338 * should, and thus we won't abide by the delayed ACK
339 * rules correctly.
340 * SACKs don't matter, we never delay an ACK when we
341 * have any of those going out.
343 *ptr++ = htonl((TCPOPT_MSS << 24) | (TCPOLEN_MSS << 16) | mss);
344 if (ts) {
345 if (sack)
346 *ptr++ = htonl((TCPOPT_SACK_PERM << 24) |
347 (TCPOLEN_SACK_PERM << 16) |
348 (TCPOPT_TIMESTAMP << 8) |
349 TCPOLEN_TIMESTAMP);
350 else
351 *ptr++ = htonl((TCPOPT_NOP << 24) |
352 (TCPOPT_NOP << 16) |
353 (TCPOPT_TIMESTAMP << 8) |
354 TCPOLEN_TIMESTAMP);
355 *ptr++ = htonl(tstamp); /* TSVAL */
356 *ptr++ = htonl(ts_recent); /* TSECR */
357 } else if (sack)
358 *ptr++ = htonl((TCPOPT_NOP << 24) |
359 (TCPOPT_NOP << 16) |
360 (TCPOPT_SACK_PERM << 8) |
361 TCPOLEN_SACK_PERM);
362 if (offer_wscale)
363 *ptr++ = htonl((TCPOPT_NOP << 24) |
364 (TCPOPT_WINDOW << 16) |
365 (TCPOLEN_WINDOW << 8) |
366 (wscale));
367 #ifdef CONFIG_TCP_MD5SIG
369 * If MD5 is enabled, then we set the option, and include the size
370 * (always 18). The actual MD5 hash is added just before the
371 * packet is sent.
373 if (md5_hash) {
374 *ptr++ = htonl((TCPOPT_NOP << 24) |
375 (TCPOPT_NOP << 16) |
376 (TCPOPT_MD5SIG << 8) |
377 TCPOLEN_MD5SIG);
378 *md5_hash = (__u8 *) ptr;
380 #endif
383 /* This routine actually transmits TCP packets queued in by
384 * tcp_do_sendmsg(). This is used by both the initial
385 * transmission and possible later retransmissions.
386 * All SKB's seen here are completely headerless. It is our
387 * job to build the TCP header, and pass the packet down to
388 * IP so it can do the same plus pass the packet off to the
389 * device.
391 * We are working here with either a clone of the original
392 * SKB, or a fresh unique copy made by the retransmit engine.
394 static int tcp_transmit_skb(struct sock *sk, struct sk_buff *skb, int clone_it, gfp_t gfp_mask)
396 const struct inet_connection_sock *icsk = inet_csk(sk);
397 struct inet_sock *inet;
398 struct tcp_sock *tp;
399 struct tcp_skb_cb *tcb;
400 int tcp_header_size;
401 #ifdef CONFIG_TCP_MD5SIG
402 struct tcp_md5sig_key *md5;
403 __u8 *md5_hash_location;
404 #endif
405 struct tcphdr *th;
406 int sysctl_flags;
407 int err;
409 BUG_ON(!skb || !tcp_skb_pcount(skb));
411 /* If congestion control is doing timestamping, we must
412 * take such a timestamp before we potentially clone/copy.
414 if (icsk->icsk_ca_ops->flags & TCP_CONG_RTT_STAMP)
415 __net_timestamp(skb);
417 if (likely(clone_it)) {
418 if (unlikely(skb_cloned(skb)))
419 skb = pskb_copy(skb, gfp_mask);
420 else
421 skb = skb_clone(skb, gfp_mask);
422 if (unlikely(!skb))
423 return -ENOBUFS;
426 inet = inet_sk(sk);
427 tp = tcp_sk(sk);
428 tcb = TCP_SKB_CB(skb);
429 tcp_header_size = tp->tcp_header_len;
431 #define SYSCTL_FLAG_TSTAMPS 0x1
432 #define SYSCTL_FLAG_WSCALE 0x2
433 #define SYSCTL_FLAG_SACK 0x4
435 sysctl_flags = 0;
436 if (unlikely(tcb->flags & TCPCB_FLAG_SYN)) {
437 tcp_header_size = sizeof(struct tcphdr) + TCPOLEN_MSS;
438 if (sysctl_tcp_timestamps) {
439 tcp_header_size += TCPOLEN_TSTAMP_ALIGNED;
440 sysctl_flags |= SYSCTL_FLAG_TSTAMPS;
442 if (sysctl_tcp_window_scaling) {
443 tcp_header_size += TCPOLEN_WSCALE_ALIGNED;
444 sysctl_flags |= SYSCTL_FLAG_WSCALE;
446 if (sysctl_tcp_sack) {
447 sysctl_flags |= SYSCTL_FLAG_SACK;
448 if (!(sysctl_flags & SYSCTL_FLAG_TSTAMPS))
449 tcp_header_size += TCPOLEN_SACKPERM_ALIGNED;
451 } else if (unlikely(tp->rx_opt.eff_sacks)) {
452 /* A SACK is 2 pad bytes, a 2 byte header, plus
453 * 2 32-bit sequence numbers for each SACK block.
455 tcp_header_size += (TCPOLEN_SACK_BASE_ALIGNED +
456 (tp->rx_opt.eff_sacks *
457 TCPOLEN_SACK_PERBLOCK));
460 if (tcp_packets_in_flight(tp) == 0)
461 tcp_ca_event(sk, CA_EVENT_TX_START);
463 #ifdef CONFIG_TCP_MD5SIG
465 * Are we doing MD5 on this segment? If so - make
466 * room for it.
468 md5 = tp->af_specific->md5_lookup(sk, sk);
469 if (md5)
470 tcp_header_size += TCPOLEN_MD5SIG_ALIGNED;
471 #endif
473 skb_push(skb, tcp_header_size);
474 skb_reset_transport_header(skb);
475 skb_set_owner_w(skb, sk);
477 /* Build TCP header and checksum it. */
478 th = tcp_hdr(skb);
479 th->source = inet->sport;
480 th->dest = inet->dport;
481 th->seq = htonl(tcb->seq);
482 th->ack_seq = htonl(tp->rcv_nxt);
483 *(((__be16 *)th) + 6) = htons(((tcp_header_size >> 2) << 12) |
484 tcb->flags);
486 if (unlikely(tcb->flags & TCPCB_FLAG_SYN)) {
487 /* RFC1323: The window in SYN & SYN/ACK segments
488 * is never scaled.
490 th->window = htons(min(tp->rcv_wnd, 65535U));
491 } else {
492 th->window = htons(tcp_select_window(sk));
494 th->check = 0;
495 th->urg_ptr = 0;
497 if (unlikely(tp->urg_mode &&
498 between(tp->snd_up, tcb->seq+1, tcb->seq+0xFFFF))) {
499 th->urg_ptr = htons(tp->snd_up-tcb->seq);
500 th->urg = 1;
503 if (unlikely(tcb->flags & TCPCB_FLAG_SYN)) {
504 tcp_syn_build_options((__be32 *)(th + 1),
505 tcp_advertise_mss(sk),
506 (sysctl_flags & SYSCTL_FLAG_TSTAMPS),
507 (sysctl_flags & SYSCTL_FLAG_SACK),
508 (sysctl_flags & SYSCTL_FLAG_WSCALE),
509 tp->rx_opt.rcv_wscale,
510 tcb->when,
511 tp->rx_opt.ts_recent,
513 #ifdef CONFIG_TCP_MD5SIG
514 md5 ? &md5_hash_location :
515 #endif
516 NULL);
517 } else {
518 tcp_build_and_update_options((__be32 *)(th + 1),
519 tp, tcb->when,
520 #ifdef CONFIG_TCP_MD5SIG
521 md5 ? &md5_hash_location :
522 #endif
523 NULL);
524 TCP_ECN_send(sk, skb, tcp_header_size);
527 #ifdef CONFIG_TCP_MD5SIG
528 /* Calculate the MD5 hash, as we have all we need now */
529 if (md5) {
530 tp->af_specific->calc_md5_hash(md5_hash_location,
531 md5,
532 sk, NULL, NULL,
533 tcp_hdr(skb),
534 sk->sk_protocol,
535 skb->len);
537 #endif
539 icsk->icsk_af_ops->send_check(sk, skb->len, skb);
541 if (likely(tcb->flags & TCPCB_FLAG_ACK))
542 tcp_event_ack_sent(sk, tcp_skb_pcount(skb));
544 if (skb->len != tcp_header_size)
545 tcp_event_data_sent(tp, skb, sk);
547 if (after(tcb->end_seq, tp->snd_nxt) || tcb->seq == tcb->end_seq)
548 TCP_INC_STATS(TCP_MIB_OUTSEGS);
550 err = icsk->icsk_af_ops->queue_xmit(skb, 0);
551 if (likely(err <= 0))
552 return err;
554 tcp_enter_cwr(sk, 1);
556 return net_xmit_eval(err);
558 #undef SYSCTL_FLAG_TSTAMPS
559 #undef SYSCTL_FLAG_WSCALE
560 #undef SYSCTL_FLAG_SACK
564 /* This routine just queue's the buffer
566 * NOTE: probe0 timer is not checked, do not forget tcp_push_pending_frames,
567 * otherwise socket can stall.
569 static void tcp_queue_skb(struct sock *sk, struct sk_buff *skb)
571 struct tcp_sock *tp = tcp_sk(sk);
573 /* Advance write_seq and place onto the write_queue. */
574 tp->write_seq = TCP_SKB_CB(skb)->end_seq;
575 skb_header_release(skb);
576 tcp_add_write_queue_tail(sk, skb);
577 sk_charge_skb(sk, skb);
580 static void tcp_set_skb_tso_segs(struct sock *sk, struct sk_buff *skb, unsigned int mss_now)
582 if (skb->len <= mss_now || !sk_can_gso(sk)) {
583 /* Avoid the costly divide in the normal
584 * non-TSO case.
586 skb_shinfo(skb)->gso_segs = 1;
587 skb_shinfo(skb)->gso_size = 0;
588 skb_shinfo(skb)->gso_type = 0;
589 } else {
590 unsigned int factor;
592 factor = skb->len + (mss_now - 1);
593 factor /= mss_now;
594 skb_shinfo(skb)->gso_segs = factor;
595 skb_shinfo(skb)->gso_size = mss_now;
596 skb_shinfo(skb)->gso_type = sk->sk_gso_type;
600 /* Function to create two new TCP segments. Shrinks the given segment
601 * to the specified size and appends a new segment with the rest of the
602 * packet to the list. This won't be called frequently, I hope.
603 * Remember, these are still headerless SKBs at this point.
605 int tcp_fragment(struct sock *sk, struct sk_buff *skb, u32 len, unsigned int mss_now)
607 struct tcp_sock *tp = tcp_sk(sk);
608 struct sk_buff *buff;
609 int nsize, old_factor;
610 int nlen;
611 u8 flags;
613 BUG_ON(len > skb->len);
615 clear_all_retrans_hints(tp);
616 nsize = skb_headlen(skb) - len;
617 if (nsize < 0)
618 nsize = 0;
620 if (skb_cloned(skb) &&
621 skb_is_nonlinear(skb) &&
622 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
623 return -ENOMEM;
625 /* Get a new skb... force flag on. */
626 buff = sk_stream_alloc_skb(sk, nsize, GFP_ATOMIC);
627 if (buff == NULL)
628 return -ENOMEM; /* We'll just try again later. */
630 sk_charge_skb(sk, buff);
631 nlen = skb->len - len - nsize;
632 buff->truesize += nlen;
633 skb->truesize -= nlen;
635 /* Correct the sequence numbers. */
636 TCP_SKB_CB(buff)->seq = TCP_SKB_CB(skb)->seq + len;
637 TCP_SKB_CB(buff)->end_seq = TCP_SKB_CB(skb)->end_seq;
638 TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(buff)->seq;
640 /* PSH and FIN should only be set in the second packet. */
641 flags = TCP_SKB_CB(skb)->flags;
642 TCP_SKB_CB(skb)->flags = flags & ~(TCPCB_FLAG_FIN|TCPCB_FLAG_PSH);
643 TCP_SKB_CB(buff)->flags = flags;
644 TCP_SKB_CB(buff)->sacked = TCP_SKB_CB(skb)->sacked;
645 TCP_SKB_CB(skb)->sacked &= ~TCPCB_AT_TAIL;
647 if (!skb_shinfo(skb)->nr_frags && skb->ip_summed != CHECKSUM_PARTIAL) {
648 /* Copy and checksum data tail into the new buffer. */
649 buff->csum = csum_partial_copy_nocheck(skb->data + len, skb_put(buff, nsize),
650 nsize, 0);
652 skb_trim(skb, len);
654 skb->csum = csum_block_sub(skb->csum, buff->csum, len);
655 } else {
656 skb->ip_summed = CHECKSUM_PARTIAL;
657 skb_split(skb, buff, len);
660 buff->ip_summed = skb->ip_summed;
662 /* Looks stupid, but our code really uses when of
663 * skbs, which it never sent before. --ANK
665 TCP_SKB_CB(buff)->when = TCP_SKB_CB(skb)->when;
666 buff->tstamp = skb->tstamp;
668 old_factor = tcp_skb_pcount(skb);
670 /* Fix up tso_factor for both original and new SKB. */
671 tcp_set_skb_tso_segs(sk, skb, mss_now);
672 tcp_set_skb_tso_segs(sk, buff, mss_now);
674 /* If this packet has been sent out already, we must
675 * adjust the various packet counters.
677 if (!before(tp->snd_nxt, TCP_SKB_CB(buff)->end_seq)) {
678 int diff = old_factor - tcp_skb_pcount(skb) -
679 tcp_skb_pcount(buff);
681 tp->packets_out -= diff;
683 if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED)
684 tp->sacked_out -= diff;
685 if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_RETRANS)
686 tp->retrans_out -= diff;
688 if (TCP_SKB_CB(skb)->sacked & TCPCB_LOST) {
689 tp->lost_out -= diff;
690 tp->left_out -= diff;
693 if (diff > 0) {
694 /* Adjust Reno SACK estimate. */
695 if (!tp->rx_opt.sack_ok) {
696 tp->sacked_out -= diff;
697 if ((int)tp->sacked_out < 0)
698 tp->sacked_out = 0;
699 tcp_sync_left_out(tp);
702 tp->fackets_out -= diff;
703 if ((int)tp->fackets_out < 0)
704 tp->fackets_out = 0;
708 /* Link BUFF into the send queue. */
709 skb_header_release(buff);
710 tcp_insert_write_queue_after(skb, buff, sk);
712 return 0;
715 /* This is similar to __pskb_pull_head() (it will go to core/skbuff.c
716 * eventually). The difference is that pulled data not copied, but
717 * immediately discarded.
719 static void __pskb_trim_head(struct sk_buff *skb, int len)
721 int i, k, eat;
723 eat = len;
724 k = 0;
725 for (i=0; i<skb_shinfo(skb)->nr_frags; i++) {
726 if (skb_shinfo(skb)->frags[i].size <= eat) {
727 put_page(skb_shinfo(skb)->frags[i].page);
728 eat -= skb_shinfo(skb)->frags[i].size;
729 } else {
730 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
731 if (eat) {
732 skb_shinfo(skb)->frags[k].page_offset += eat;
733 skb_shinfo(skb)->frags[k].size -= eat;
734 eat = 0;
736 k++;
739 skb_shinfo(skb)->nr_frags = k;
741 skb_reset_tail_pointer(skb);
742 skb->data_len -= len;
743 skb->len = skb->data_len;
746 int tcp_trim_head(struct sock *sk, struct sk_buff *skb, u32 len)
748 if (skb_cloned(skb) &&
749 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
750 return -ENOMEM;
752 /* If len == headlen, we avoid __skb_pull to preserve alignment. */
753 if (unlikely(len < skb_headlen(skb)))
754 __skb_pull(skb, len);
755 else
756 __pskb_trim_head(skb, len - skb_headlen(skb));
758 TCP_SKB_CB(skb)->seq += len;
759 skb->ip_summed = CHECKSUM_PARTIAL;
761 skb->truesize -= len;
762 sk->sk_wmem_queued -= len;
763 sk->sk_forward_alloc += len;
764 sock_set_flag(sk, SOCK_QUEUE_SHRUNK);
766 /* Any change of skb->len requires recalculation of tso
767 * factor and mss.
769 if (tcp_skb_pcount(skb) > 1)
770 tcp_set_skb_tso_segs(sk, skb, tcp_current_mss(sk, 1));
772 return 0;
775 /* Not accounting for SACKs here. */
776 int tcp_mtu_to_mss(struct sock *sk, int pmtu)
778 struct tcp_sock *tp = tcp_sk(sk);
779 struct inet_connection_sock *icsk = inet_csk(sk);
780 int mss_now;
782 /* Calculate base mss without TCP options:
783 It is MMS_S - sizeof(tcphdr) of rfc1122
785 mss_now = pmtu - icsk->icsk_af_ops->net_header_len - sizeof(struct tcphdr);
787 /* Clamp it (mss_clamp does not include tcp options) */
788 if (mss_now > tp->rx_opt.mss_clamp)
789 mss_now = tp->rx_opt.mss_clamp;
791 /* Now subtract optional transport overhead */
792 mss_now -= icsk->icsk_ext_hdr_len;
794 /* Then reserve room for full set of TCP options and 8 bytes of data */
795 if (mss_now < 48)
796 mss_now = 48;
798 /* Now subtract TCP options size, not including SACKs */
799 mss_now -= tp->tcp_header_len - sizeof(struct tcphdr);
801 return mss_now;
804 /* Inverse of above */
805 int tcp_mss_to_mtu(struct sock *sk, int mss)
807 struct tcp_sock *tp = tcp_sk(sk);
808 struct inet_connection_sock *icsk = inet_csk(sk);
809 int mtu;
811 mtu = mss +
812 tp->tcp_header_len +
813 icsk->icsk_ext_hdr_len +
814 icsk->icsk_af_ops->net_header_len;
816 return mtu;
819 void tcp_mtup_init(struct sock *sk)
821 struct tcp_sock *tp = tcp_sk(sk);
822 struct inet_connection_sock *icsk = inet_csk(sk);
824 icsk->icsk_mtup.enabled = sysctl_tcp_mtu_probing > 1;
825 icsk->icsk_mtup.search_high = tp->rx_opt.mss_clamp + sizeof(struct tcphdr) +
826 icsk->icsk_af_ops->net_header_len;
827 icsk->icsk_mtup.search_low = tcp_mss_to_mtu(sk, sysctl_tcp_base_mss);
828 icsk->icsk_mtup.probe_size = 0;
831 /* This function synchronize snd mss to current pmtu/exthdr set.
833 tp->rx_opt.user_mss is mss set by user by TCP_MAXSEG. It does NOT counts
834 for TCP options, but includes only bare TCP header.
836 tp->rx_opt.mss_clamp is mss negotiated at connection setup.
837 It is minimum of user_mss and mss received with SYN.
838 It also does not include TCP options.
840 inet_csk(sk)->icsk_pmtu_cookie is last pmtu, seen by this function.
842 tp->mss_cache is current effective sending mss, including
843 all tcp options except for SACKs. It is evaluated,
844 taking into account current pmtu, but never exceeds
845 tp->rx_opt.mss_clamp.
847 NOTE1. rfc1122 clearly states that advertised MSS
848 DOES NOT include either tcp or ip options.
850 NOTE2. inet_csk(sk)->icsk_pmtu_cookie and tp->mss_cache
851 are READ ONLY outside this function. --ANK (980731)
854 unsigned int tcp_sync_mss(struct sock *sk, u32 pmtu)
856 struct tcp_sock *tp = tcp_sk(sk);
857 struct inet_connection_sock *icsk = inet_csk(sk);
858 int mss_now;
860 if (icsk->icsk_mtup.search_high > pmtu)
861 icsk->icsk_mtup.search_high = pmtu;
863 mss_now = tcp_mtu_to_mss(sk, pmtu);
865 /* Bound mss with half of window */
866 if (tp->max_window && mss_now > (tp->max_window>>1))
867 mss_now = max((tp->max_window>>1), 68U - tp->tcp_header_len);
869 /* And store cached results */
870 icsk->icsk_pmtu_cookie = pmtu;
871 if (icsk->icsk_mtup.enabled)
872 mss_now = min(mss_now, tcp_mtu_to_mss(sk, icsk->icsk_mtup.search_low));
873 tp->mss_cache = mss_now;
875 return mss_now;
878 /* Compute the current effective MSS, taking SACKs and IP options,
879 * and even PMTU discovery events into account.
881 * LARGESEND note: !urg_mode is overkill, only frames up to snd_up
882 * cannot be large. However, taking into account rare use of URG, this
883 * is not a big flaw.
885 unsigned int tcp_current_mss(struct sock *sk, int large_allowed)
887 struct tcp_sock *tp = tcp_sk(sk);
888 struct dst_entry *dst = __sk_dst_get(sk);
889 u32 mss_now;
890 u16 xmit_size_goal;
891 int doing_tso = 0;
893 mss_now = tp->mss_cache;
895 if (large_allowed && sk_can_gso(sk) && !tp->urg_mode)
896 doing_tso = 1;
898 if (dst) {
899 u32 mtu = dst_mtu(dst);
900 if (mtu != inet_csk(sk)->icsk_pmtu_cookie)
901 mss_now = tcp_sync_mss(sk, mtu);
904 if (tp->rx_opt.eff_sacks)
905 mss_now -= (TCPOLEN_SACK_BASE_ALIGNED +
906 (tp->rx_opt.eff_sacks * TCPOLEN_SACK_PERBLOCK));
908 #ifdef CONFIG_TCP_MD5SIG
909 if (tp->af_specific->md5_lookup(sk, sk))
910 mss_now -= TCPOLEN_MD5SIG_ALIGNED;
911 #endif
913 xmit_size_goal = mss_now;
915 if (doing_tso) {
916 xmit_size_goal = (65535 -
917 inet_csk(sk)->icsk_af_ops->net_header_len -
918 inet_csk(sk)->icsk_ext_hdr_len -
919 tp->tcp_header_len);
921 if (tp->max_window &&
922 (xmit_size_goal > (tp->max_window >> 1)))
923 xmit_size_goal = max((tp->max_window >> 1),
924 68U - tp->tcp_header_len);
926 xmit_size_goal -= (xmit_size_goal % mss_now);
928 tp->xmit_size_goal = xmit_size_goal;
930 return mss_now;
933 /* Congestion window validation. (RFC2861) */
935 static void tcp_cwnd_validate(struct sock *sk)
937 struct tcp_sock *tp = tcp_sk(sk);
938 __u32 packets_out = tp->packets_out;
940 if (packets_out >= tp->snd_cwnd) {
941 /* Network is feed fully. */
942 tp->snd_cwnd_used = 0;
943 tp->snd_cwnd_stamp = tcp_time_stamp;
944 } else {
945 /* Network starves. */
946 if (tp->packets_out > tp->snd_cwnd_used)
947 tp->snd_cwnd_used = tp->packets_out;
949 if (sysctl_tcp_slow_start_after_idle &&
950 (s32)(tcp_time_stamp - tp->snd_cwnd_stamp) >= inet_csk(sk)->icsk_rto)
951 tcp_cwnd_application_limited(sk);
955 /* Returns the portion of skb which can be sent right away without
956 * introducing MSS oddities to segment boundaries. In rare cases where
957 * mss_now != mss_cache, we will request caller to create a small skb
958 * per input skb which could be mostly avoided here (if desired).
960 * We explicitly want to create a request for splitting write queue tail
961 * to a small skb for Nagle purposes while avoiding unnecessary modulos,
962 * thus all the complexity (cwnd_len is always MSS multiple which we
963 * return whenever allowed by the other factors). Basically we need the
964 * modulo only when the receiver window alone is the limiting factor or
965 * when we would be allowed to send the split-due-to-Nagle skb fully.
967 static unsigned int tcp_mss_split_point(struct sock *sk, struct sk_buff *skb,
968 unsigned int mss_now,
969 unsigned int cwnd)
971 struct tcp_sock *tp = tcp_sk(sk);
972 u32 needed, window, cwnd_len;
974 window = (tp->snd_una + tp->snd_wnd - TCP_SKB_CB(skb)->seq);
975 cwnd_len = mss_now * cwnd;
977 if (likely(cwnd_len <= window && skb != tcp_write_queue_tail(sk)))
978 return cwnd_len;
980 needed = min(skb->len, window);
982 if (skb == tcp_write_queue_tail(sk) && cwnd_len <= needed)
983 return cwnd_len;
985 return needed - needed % mss_now;
988 /* Can at least one segment of SKB be sent right now, according to the
989 * congestion window rules? If so, return how many segments are allowed.
991 static inline unsigned int tcp_cwnd_test(struct tcp_sock *tp, struct sk_buff *skb)
993 u32 in_flight, cwnd;
995 /* Don't be strict about the congestion window for the final FIN. */
996 if ((TCP_SKB_CB(skb)->flags & TCPCB_FLAG_FIN) &&
997 tcp_skb_pcount(skb) == 1)
998 return 1;
1000 in_flight = tcp_packets_in_flight(tp);
1001 cwnd = tp->snd_cwnd;
1002 if (in_flight < cwnd)
1003 return (cwnd - in_flight);
1005 return 0;
1008 /* This must be invoked the first time we consider transmitting
1009 * SKB onto the wire.
1011 static int tcp_init_tso_segs(struct sock *sk, struct sk_buff *skb, unsigned int mss_now)
1013 int tso_segs = tcp_skb_pcount(skb);
1015 if (!tso_segs ||
1016 (tso_segs > 1 &&
1017 tcp_skb_mss(skb) != mss_now)) {
1018 tcp_set_skb_tso_segs(sk, skb, mss_now);
1019 tso_segs = tcp_skb_pcount(skb);
1021 return tso_segs;
1024 static inline int tcp_minshall_check(const struct tcp_sock *tp)
1026 return after(tp->snd_sml,tp->snd_una) &&
1027 !after(tp->snd_sml, tp->snd_nxt);
1030 /* Return 0, if packet can be sent now without violation Nagle's rules:
1031 * 1. It is full sized.
1032 * 2. Or it contains FIN. (already checked by caller)
1033 * 3. Or TCP_NODELAY was set.
1034 * 4. Or TCP_CORK is not set, and all sent packets are ACKed.
1035 * With Minshall's modification: all sent small packets are ACKed.
1038 static inline int tcp_nagle_check(const struct tcp_sock *tp,
1039 const struct sk_buff *skb,
1040 unsigned mss_now, int nonagle)
1042 return (skb->len < mss_now &&
1043 ((nonagle&TCP_NAGLE_CORK) ||
1044 (!nonagle &&
1045 tp->packets_out &&
1046 tcp_minshall_check(tp))));
1049 /* Return non-zero if the Nagle test allows this packet to be
1050 * sent now.
1052 static inline int tcp_nagle_test(struct tcp_sock *tp, struct sk_buff *skb,
1053 unsigned int cur_mss, int nonagle)
1055 /* Nagle rule does not apply to frames, which sit in the middle of the
1056 * write_queue (they have no chances to get new data).
1058 * This is implemented in the callers, where they modify the 'nonagle'
1059 * argument based upon the location of SKB in the send queue.
1061 if (nonagle & TCP_NAGLE_PUSH)
1062 return 1;
1064 /* Don't use the nagle rule for urgent data (or for the final FIN).
1065 * Nagle can be ignored during F-RTO too (see RFC4138).
1067 if (tp->urg_mode || (tp->frto_counter == 2) ||
1068 (TCP_SKB_CB(skb)->flags & TCPCB_FLAG_FIN))
1069 return 1;
1071 if (!tcp_nagle_check(tp, skb, cur_mss, nonagle))
1072 return 1;
1074 return 0;
1077 /* Does at least the first segment of SKB fit into the send window? */
1078 static inline int tcp_snd_wnd_test(struct tcp_sock *tp, struct sk_buff *skb, unsigned int cur_mss)
1080 u32 end_seq = TCP_SKB_CB(skb)->end_seq;
1082 if (skb->len > cur_mss)
1083 end_seq = TCP_SKB_CB(skb)->seq + cur_mss;
1085 return !after(end_seq, tp->snd_una + tp->snd_wnd);
1088 /* This checks if the data bearing packet SKB (usually tcp_send_head(sk))
1089 * should be put on the wire right now. If so, it returns the number of
1090 * packets allowed by the congestion window.
1092 static unsigned int tcp_snd_test(struct sock *sk, struct sk_buff *skb,
1093 unsigned int cur_mss, int nonagle)
1095 struct tcp_sock *tp = tcp_sk(sk);
1096 unsigned int cwnd_quota;
1098 tcp_init_tso_segs(sk, skb, cur_mss);
1100 if (!tcp_nagle_test(tp, skb, cur_mss, nonagle))
1101 return 0;
1103 cwnd_quota = tcp_cwnd_test(tp, skb);
1104 if (cwnd_quota &&
1105 !tcp_snd_wnd_test(tp, skb, cur_mss))
1106 cwnd_quota = 0;
1108 return cwnd_quota;
1111 int tcp_may_send_now(struct sock *sk)
1113 struct tcp_sock *tp = tcp_sk(sk);
1114 struct sk_buff *skb = tcp_send_head(sk);
1116 return (skb &&
1117 tcp_snd_test(sk, skb, tcp_current_mss(sk, 1),
1118 (tcp_skb_is_last(sk, skb) ?
1119 tp->nonagle : TCP_NAGLE_PUSH)));
1122 /* Trim TSO SKB to LEN bytes, put the remaining data into a new packet
1123 * which is put after SKB on the list. It is very much like
1124 * tcp_fragment() except that it may make several kinds of assumptions
1125 * in order to speed up the splitting operation. In particular, we
1126 * know that all the data is in scatter-gather pages, and that the
1127 * packet has never been sent out before (and thus is not cloned).
1129 static int tso_fragment(struct sock *sk, struct sk_buff *skb, unsigned int len, unsigned int mss_now)
1131 struct sk_buff *buff;
1132 int nlen = skb->len - len;
1133 u8 flags;
1135 /* All of a TSO frame must be composed of paged data. */
1136 if (skb->len != skb->data_len)
1137 return tcp_fragment(sk, skb, len, mss_now);
1139 buff = sk_stream_alloc_skb(sk, 0, GFP_ATOMIC);
1140 if (unlikely(buff == NULL))
1141 return -ENOMEM;
1143 sk_charge_skb(sk, buff);
1144 buff->truesize += nlen;
1145 skb->truesize -= nlen;
1147 /* Correct the sequence numbers. */
1148 TCP_SKB_CB(buff)->seq = TCP_SKB_CB(skb)->seq + len;
1149 TCP_SKB_CB(buff)->end_seq = TCP_SKB_CB(skb)->end_seq;
1150 TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(buff)->seq;
1152 /* PSH and FIN should only be set in the second packet. */
1153 flags = TCP_SKB_CB(skb)->flags;
1154 TCP_SKB_CB(skb)->flags = flags & ~(TCPCB_FLAG_FIN|TCPCB_FLAG_PSH);
1155 TCP_SKB_CB(buff)->flags = flags;
1157 /* This packet was never sent out yet, so no SACK bits. */
1158 TCP_SKB_CB(buff)->sacked = 0;
1160 buff->ip_summed = skb->ip_summed = CHECKSUM_PARTIAL;
1161 skb_split(skb, buff, len);
1163 /* Fix up tso_factor for both original and new SKB. */
1164 tcp_set_skb_tso_segs(sk, skb, mss_now);
1165 tcp_set_skb_tso_segs(sk, buff, mss_now);
1167 /* Link BUFF into the send queue. */
1168 skb_header_release(buff);
1169 tcp_insert_write_queue_after(skb, buff, sk);
1171 return 0;
1174 /* Try to defer sending, if possible, in order to minimize the amount
1175 * of TSO splitting we do. View it as a kind of TSO Nagle test.
1177 * This algorithm is from John Heffner.
1179 static int tcp_tso_should_defer(struct sock *sk, struct sk_buff *skb)
1181 struct tcp_sock *tp = tcp_sk(sk);
1182 const struct inet_connection_sock *icsk = inet_csk(sk);
1183 u32 send_win, cong_win, limit, in_flight;
1185 if (TCP_SKB_CB(skb)->flags & TCPCB_FLAG_FIN)
1186 goto send_now;
1188 if (icsk->icsk_ca_state != TCP_CA_Open)
1189 goto send_now;
1191 /* Defer for less than two clock ticks. */
1192 if (tp->tso_deferred &&
1193 ((jiffies << 1) >> 1) - (tp->tso_deferred >> 1) > 1)
1194 goto send_now;
1196 in_flight = tcp_packets_in_flight(tp);
1198 BUG_ON(tcp_skb_pcount(skb) <= 1 ||
1199 (tp->snd_cwnd <= in_flight));
1201 send_win = (tp->snd_una + tp->snd_wnd) - TCP_SKB_CB(skb)->seq;
1203 /* From in_flight test above, we know that cwnd > in_flight. */
1204 cong_win = (tp->snd_cwnd - in_flight) * tp->mss_cache;
1206 limit = min(send_win, cong_win);
1208 /* If a full-sized TSO skb can be sent, do it. */
1209 if (limit >= 65536)
1210 goto send_now;
1212 if (sysctl_tcp_tso_win_divisor) {
1213 u32 chunk = min(tp->snd_wnd, tp->snd_cwnd * tp->mss_cache);
1215 /* If at least some fraction of a window is available,
1216 * just use it.
1218 chunk /= sysctl_tcp_tso_win_divisor;
1219 if (limit >= chunk)
1220 goto send_now;
1221 } else {
1222 /* Different approach, try not to defer past a single
1223 * ACK. Receiver should ACK every other full sized
1224 * frame, so if we have space for more than 3 frames
1225 * then send now.
1227 if (limit > tcp_max_burst(tp) * tp->mss_cache)
1228 goto send_now;
1231 /* Ok, it looks like it is advisable to defer. */
1232 tp->tso_deferred = 1 | (jiffies<<1);
1234 return 1;
1236 send_now:
1237 tp->tso_deferred = 0;
1238 return 0;
1241 /* Create a new MTU probe if we are ready.
1242 * Returns 0 if we should wait to probe (no cwnd available),
1243 * 1 if a probe was sent,
1244 * -1 otherwise */
1245 static int tcp_mtu_probe(struct sock *sk)
1247 struct tcp_sock *tp = tcp_sk(sk);
1248 struct inet_connection_sock *icsk = inet_csk(sk);
1249 struct sk_buff *skb, *nskb, *next;
1250 int len;
1251 int probe_size;
1252 int size_needed;
1253 unsigned int pif;
1254 int copy;
1255 int mss_now;
1257 /* Not currently probing/verifying,
1258 * not in recovery,
1259 * have enough cwnd, and
1260 * not SACKing (the variable headers throw things off) */
1261 if (!icsk->icsk_mtup.enabled ||
1262 icsk->icsk_mtup.probe_size ||
1263 inet_csk(sk)->icsk_ca_state != TCP_CA_Open ||
1264 tp->snd_cwnd < 11 ||
1265 tp->rx_opt.eff_sacks)
1266 return -1;
1268 /* Very simple search strategy: just double the MSS. */
1269 mss_now = tcp_current_mss(sk, 0);
1270 probe_size = 2*tp->mss_cache;
1271 size_needed = probe_size + (tp->reordering + 1) * tp->mss_cache;
1272 if (probe_size > tcp_mtu_to_mss(sk, icsk->icsk_mtup.search_high)) {
1273 /* TODO: set timer for probe_converge_event */
1274 return -1;
1277 /* Have enough data in the send queue to probe? */
1278 if (tp->write_seq - tp->snd_nxt < size_needed)
1279 return -1;
1281 if (tp->snd_wnd < size_needed)
1282 return -1;
1283 if (after(tp->snd_nxt + size_needed, tp->snd_una + tp->snd_wnd))
1284 return 0;
1286 /* Do we need to wait to drain cwnd? */
1287 pif = tcp_packets_in_flight(tp);
1288 if (pif + 2 > tp->snd_cwnd) {
1289 /* With no packets in flight, don't stall. */
1290 if (pif == 0)
1291 return -1;
1292 else
1293 return 0;
1296 /* We're allowed to probe. Build it now. */
1297 if ((nskb = sk_stream_alloc_skb(sk, probe_size, GFP_ATOMIC)) == NULL)
1298 return -1;
1299 sk_charge_skb(sk, nskb);
1301 skb = tcp_send_head(sk);
1303 TCP_SKB_CB(nskb)->seq = TCP_SKB_CB(skb)->seq;
1304 TCP_SKB_CB(nskb)->end_seq = TCP_SKB_CB(skb)->seq + probe_size;
1305 TCP_SKB_CB(nskb)->flags = TCPCB_FLAG_ACK;
1306 TCP_SKB_CB(nskb)->sacked = 0;
1307 nskb->csum = 0;
1308 nskb->ip_summed = skb->ip_summed;
1310 tcp_insert_write_queue_before(nskb, skb, sk);
1312 len = 0;
1313 while (len < probe_size) {
1314 next = tcp_write_queue_next(sk, skb);
1316 copy = min_t(int, skb->len, probe_size - len);
1317 if (nskb->ip_summed)
1318 skb_copy_bits(skb, 0, skb_put(nskb, copy), copy);
1319 else
1320 nskb->csum = skb_copy_and_csum_bits(skb, 0,
1321 skb_put(nskb, copy), copy, nskb->csum);
1323 if (skb->len <= copy) {
1324 /* We've eaten all the data from this skb.
1325 * Throw it away. */
1326 TCP_SKB_CB(nskb)->flags |= TCP_SKB_CB(skb)->flags;
1327 tcp_unlink_write_queue(skb, sk);
1328 sk_stream_free_skb(sk, skb);
1329 } else {
1330 TCP_SKB_CB(nskb)->flags |= TCP_SKB_CB(skb)->flags &
1331 ~(TCPCB_FLAG_FIN|TCPCB_FLAG_PSH);
1332 if (!skb_shinfo(skb)->nr_frags) {
1333 skb_pull(skb, copy);
1334 if (skb->ip_summed != CHECKSUM_PARTIAL)
1335 skb->csum = csum_partial(skb->data, skb->len, 0);
1336 } else {
1337 __pskb_trim_head(skb, copy);
1338 tcp_set_skb_tso_segs(sk, skb, mss_now);
1340 TCP_SKB_CB(skb)->seq += copy;
1343 len += copy;
1344 skb = next;
1346 tcp_init_tso_segs(sk, nskb, nskb->len);
1348 /* We're ready to send. If this fails, the probe will
1349 * be resegmented into mss-sized pieces by tcp_write_xmit(). */
1350 TCP_SKB_CB(nskb)->when = tcp_time_stamp;
1351 if (!tcp_transmit_skb(sk, nskb, 1, GFP_ATOMIC)) {
1352 /* Decrement cwnd here because we are sending
1353 * effectively two packets. */
1354 tp->snd_cwnd--;
1355 update_send_head(sk, nskb);
1357 icsk->icsk_mtup.probe_size = tcp_mss_to_mtu(sk, nskb->len);
1358 tp->mtu_probe.probe_seq_start = TCP_SKB_CB(nskb)->seq;
1359 tp->mtu_probe.probe_seq_end = TCP_SKB_CB(nskb)->end_seq;
1361 return 1;
1364 return -1;
1368 /* This routine writes packets to the network. It advances the
1369 * send_head. This happens as incoming acks open up the remote
1370 * window for us.
1372 * Returns 1, if no segments are in flight and we have queued segments, but
1373 * cannot send anything now because of SWS or another problem.
1375 static int tcp_write_xmit(struct sock *sk, unsigned int mss_now, int nonagle)
1377 struct tcp_sock *tp = tcp_sk(sk);
1378 struct sk_buff *skb;
1379 unsigned int tso_segs, sent_pkts;
1380 int cwnd_quota;
1381 int result;
1383 /* If we are closed, the bytes will have to remain here.
1384 * In time closedown will finish, we empty the write queue and all
1385 * will be happy.
1387 if (unlikely(sk->sk_state == TCP_CLOSE))
1388 return 0;
1390 sent_pkts = 0;
1392 /* Do MTU probing. */
1393 if ((result = tcp_mtu_probe(sk)) == 0) {
1394 return 0;
1395 } else if (result > 0) {
1396 sent_pkts = 1;
1399 while ((skb = tcp_send_head(sk))) {
1400 unsigned int limit;
1402 tso_segs = tcp_init_tso_segs(sk, skb, mss_now);
1403 BUG_ON(!tso_segs);
1405 cwnd_quota = tcp_cwnd_test(tp, skb);
1406 if (!cwnd_quota)
1407 break;
1409 if (unlikely(!tcp_snd_wnd_test(tp, skb, mss_now)))
1410 break;
1412 if (tso_segs == 1) {
1413 if (unlikely(!tcp_nagle_test(tp, skb, mss_now,
1414 (tcp_skb_is_last(sk, skb) ?
1415 nonagle : TCP_NAGLE_PUSH))))
1416 break;
1417 } else {
1418 if (tcp_tso_should_defer(sk, skb))
1419 break;
1422 limit = mss_now;
1423 if (tso_segs > 1)
1424 limit = tcp_mss_split_point(sk, skb, mss_now,
1425 cwnd_quota);
1427 if (skb->len > limit &&
1428 unlikely(tso_fragment(sk, skb, limit, mss_now)))
1429 break;
1431 TCP_SKB_CB(skb)->when = tcp_time_stamp;
1433 if (unlikely(tcp_transmit_skb(sk, skb, 1, GFP_ATOMIC)))
1434 break;
1436 /* Advance the send_head. This one is sent out.
1437 * This call will increment packets_out.
1439 update_send_head(sk, skb);
1441 tcp_minshall_update(tp, mss_now, skb);
1442 sent_pkts++;
1445 if (likely(sent_pkts)) {
1446 tcp_cwnd_validate(sk);
1447 return 0;
1449 return !tp->packets_out && tcp_send_head(sk);
1452 /* Push out any pending frames which were held back due to
1453 * TCP_CORK or attempt at coalescing tiny packets.
1454 * The socket must be locked by the caller.
1456 void __tcp_push_pending_frames(struct sock *sk, unsigned int cur_mss,
1457 int nonagle)
1459 struct sk_buff *skb = tcp_send_head(sk);
1461 if (skb) {
1462 if (tcp_write_xmit(sk, cur_mss, nonagle))
1463 tcp_check_probe_timer(sk);
1467 /* Send _single_ skb sitting at the send head. This function requires
1468 * true push pending frames to setup probe timer etc.
1470 void tcp_push_one(struct sock *sk, unsigned int mss_now)
1472 struct sk_buff *skb = tcp_send_head(sk);
1473 unsigned int tso_segs, cwnd_quota;
1475 BUG_ON(!skb || skb->len < mss_now);
1477 tso_segs = tcp_init_tso_segs(sk, skb, mss_now);
1478 cwnd_quota = tcp_snd_test(sk, skb, mss_now, TCP_NAGLE_PUSH);
1480 if (likely(cwnd_quota)) {
1481 unsigned int limit;
1483 BUG_ON(!tso_segs);
1485 limit = mss_now;
1486 if (tso_segs > 1)
1487 limit = tcp_mss_split_point(sk, skb, mss_now,
1488 cwnd_quota);
1490 if (skb->len > limit &&
1491 unlikely(tso_fragment(sk, skb, limit, mss_now)))
1492 return;
1494 /* Send it out now. */
1495 TCP_SKB_CB(skb)->when = tcp_time_stamp;
1497 if (likely(!tcp_transmit_skb(sk, skb, 1, sk->sk_allocation))) {
1498 update_send_head(sk, skb);
1499 tcp_cwnd_validate(sk);
1500 return;
1505 /* This function returns the amount that we can raise the
1506 * usable window based on the following constraints
1508 * 1. The window can never be shrunk once it is offered (RFC 793)
1509 * 2. We limit memory per socket
1511 * RFC 1122:
1512 * "the suggested [SWS] avoidance algorithm for the receiver is to keep
1513 * RECV.NEXT + RCV.WIN fixed until:
1514 * RCV.BUFF - RCV.USER - RCV.WINDOW >= min(1/2 RCV.BUFF, MSS)"
1516 * i.e. don't raise the right edge of the window until you can raise
1517 * it at least MSS bytes.
1519 * Unfortunately, the recommended algorithm breaks header prediction,
1520 * since header prediction assumes th->window stays fixed.
1522 * Strictly speaking, keeping th->window fixed violates the receiver
1523 * side SWS prevention criteria. The problem is that under this rule
1524 * a stream of single byte packets will cause the right side of the
1525 * window to always advance by a single byte.
1527 * Of course, if the sender implements sender side SWS prevention
1528 * then this will not be a problem.
1530 * BSD seems to make the following compromise:
1532 * If the free space is less than the 1/4 of the maximum
1533 * space available and the free space is less than 1/2 mss,
1534 * then set the window to 0.
1535 * [ Actually, bsd uses MSS and 1/4 of maximal _window_ ]
1536 * Otherwise, just prevent the window from shrinking
1537 * and from being larger than the largest representable value.
1539 * This prevents incremental opening of the window in the regime
1540 * where TCP is limited by the speed of the reader side taking
1541 * data out of the TCP receive queue. It does nothing about
1542 * those cases where the window is constrained on the sender side
1543 * because the pipeline is full.
1545 * BSD also seems to "accidentally" limit itself to windows that are a
1546 * multiple of MSS, at least until the free space gets quite small.
1547 * This would appear to be a side effect of the mbuf implementation.
1548 * Combining these two algorithms results in the observed behavior
1549 * of having a fixed window size at almost all times.
1551 * Below we obtain similar behavior by forcing the offered window to
1552 * a multiple of the mss when it is feasible to do so.
1554 * Note, we don't "adjust" for TIMESTAMP or SACK option bytes.
1555 * Regular options like TIMESTAMP are taken into account.
1557 u32 __tcp_select_window(struct sock *sk)
1559 struct inet_connection_sock *icsk = inet_csk(sk);
1560 struct tcp_sock *tp = tcp_sk(sk);
1561 /* MSS for the peer's data. Previous versions used mss_clamp
1562 * here. I don't know if the value based on our guesses
1563 * of peer's MSS is better for the performance. It's more correct
1564 * but may be worse for the performance because of rcv_mss
1565 * fluctuations. --SAW 1998/11/1
1567 int mss = icsk->icsk_ack.rcv_mss;
1568 int free_space = tcp_space(sk);
1569 int full_space = min_t(int, tp->window_clamp, tcp_full_space(sk));
1570 int window;
1572 if (mss > full_space)
1573 mss = full_space;
1575 if (free_space < (full_space >> 1)) {
1576 icsk->icsk_ack.quick = 0;
1578 if (tcp_memory_pressure)
1579 tp->rcv_ssthresh = min(tp->rcv_ssthresh, 4U*tp->advmss);
1581 if (free_space < mss)
1582 return 0;
1585 if (free_space > tp->rcv_ssthresh)
1586 free_space = tp->rcv_ssthresh;
1588 /* Don't do rounding if we are using window scaling, since the
1589 * scaled window will not line up with the MSS boundary anyway.
1591 window = tp->rcv_wnd;
1592 if (tp->rx_opt.rcv_wscale) {
1593 window = free_space;
1595 /* Advertise enough space so that it won't get scaled away.
1596 * Import case: prevent zero window announcement if
1597 * 1<<rcv_wscale > mss.
1599 if (((window >> tp->rx_opt.rcv_wscale) << tp->rx_opt.rcv_wscale) != window)
1600 window = (((window >> tp->rx_opt.rcv_wscale) + 1)
1601 << tp->rx_opt.rcv_wscale);
1602 } else {
1603 /* Get the largest window that is a nice multiple of mss.
1604 * Window clamp already applied above.
1605 * If our current window offering is within 1 mss of the
1606 * free space we just keep it. This prevents the divide
1607 * and multiply from happening most of the time.
1608 * We also don't do any window rounding when the free space
1609 * is too small.
1611 if (window <= free_space - mss || window > free_space)
1612 window = (free_space/mss)*mss;
1613 else if (mss == full_space &&
1614 free_space > window + (full_space >> 1))
1615 window = free_space;
1618 return window;
1621 /* Attempt to collapse two adjacent SKB's during retransmission. */
1622 static void tcp_retrans_try_collapse(struct sock *sk, struct sk_buff *skb, int mss_now)
1624 struct tcp_sock *tp = tcp_sk(sk);
1625 struct sk_buff *next_skb = tcp_write_queue_next(sk, skb);
1627 /* The first test we must make is that neither of these two
1628 * SKB's are still referenced by someone else.
1630 if (!skb_cloned(skb) && !skb_cloned(next_skb)) {
1631 int skb_size = skb->len, next_skb_size = next_skb->len;
1632 u16 flags = TCP_SKB_CB(skb)->flags;
1634 /* Also punt if next skb has been SACK'd. */
1635 if (TCP_SKB_CB(next_skb)->sacked & TCPCB_SACKED_ACKED)
1636 return;
1638 /* Next skb is out of window. */
1639 if (after(TCP_SKB_CB(next_skb)->end_seq, tp->snd_una+tp->snd_wnd))
1640 return;
1642 /* Punt if not enough space exists in the first SKB for
1643 * the data in the second, or the total combined payload
1644 * would exceed the MSS.
1646 if ((next_skb_size > skb_tailroom(skb)) ||
1647 ((skb_size + next_skb_size) > mss_now))
1648 return;
1650 BUG_ON(tcp_skb_pcount(skb) != 1 ||
1651 tcp_skb_pcount(next_skb) != 1);
1653 /* changing transmit queue under us so clear hints */
1654 clear_all_retrans_hints(tp);
1656 /* Ok. We will be able to collapse the packet. */
1657 tcp_unlink_write_queue(next_skb, sk);
1659 skb_copy_from_linear_data(next_skb,
1660 skb_put(skb, next_skb_size),
1661 next_skb_size);
1663 if (next_skb->ip_summed == CHECKSUM_PARTIAL)
1664 skb->ip_summed = CHECKSUM_PARTIAL;
1666 if (skb->ip_summed != CHECKSUM_PARTIAL)
1667 skb->csum = csum_block_add(skb->csum, next_skb->csum, skb_size);
1669 /* Update sequence range on original skb. */
1670 TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(next_skb)->end_seq;
1672 /* Merge over control information. */
1673 flags |= TCP_SKB_CB(next_skb)->flags; /* This moves PSH/FIN etc. over */
1674 TCP_SKB_CB(skb)->flags = flags;
1676 /* All done, get rid of second SKB and account for it so
1677 * packet counting does not break.
1679 TCP_SKB_CB(skb)->sacked |= TCP_SKB_CB(next_skb)->sacked&(TCPCB_EVER_RETRANS|TCPCB_AT_TAIL);
1680 if (TCP_SKB_CB(next_skb)->sacked&TCPCB_SACKED_RETRANS)
1681 tp->retrans_out -= tcp_skb_pcount(next_skb);
1682 if (TCP_SKB_CB(next_skb)->sacked&TCPCB_LOST) {
1683 tp->lost_out -= tcp_skb_pcount(next_skb);
1684 tp->left_out -= tcp_skb_pcount(next_skb);
1686 /* Reno case is special. Sigh... */
1687 if (!tp->rx_opt.sack_ok && tp->sacked_out) {
1688 tcp_dec_pcount_approx(&tp->sacked_out, next_skb);
1689 tp->left_out -= tcp_skb_pcount(next_skb);
1692 /* Not quite right: it can be > snd.fack, but
1693 * it is better to underestimate fackets.
1695 tcp_dec_pcount_approx(&tp->fackets_out, next_skb);
1696 tcp_packets_out_dec(tp, next_skb);
1697 sk_stream_free_skb(sk, next_skb);
1701 /* Do a simple retransmit without using the backoff mechanisms in
1702 * tcp_timer. This is used for path mtu discovery.
1703 * The socket is already locked here.
1705 void tcp_simple_retransmit(struct sock *sk)
1707 const struct inet_connection_sock *icsk = inet_csk(sk);
1708 struct tcp_sock *tp = tcp_sk(sk);
1709 struct sk_buff *skb;
1710 unsigned int mss = tcp_current_mss(sk, 0);
1711 int lost = 0;
1713 tcp_for_write_queue(skb, sk) {
1714 if (skb == tcp_send_head(sk))
1715 break;
1716 if (skb->len > mss &&
1717 !(TCP_SKB_CB(skb)->sacked&TCPCB_SACKED_ACKED)) {
1718 if (TCP_SKB_CB(skb)->sacked&TCPCB_SACKED_RETRANS) {
1719 TCP_SKB_CB(skb)->sacked &= ~TCPCB_SACKED_RETRANS;
1720 tp->retrans_out -= tcp_skb_pcount(skb);
1722 if (!(TCP_SKB_CB(skb)->sacked&TCPCB_LOST)) {
1723 TCP_SKB_CB(skb)->sacked |= TCPCB_LOST;
1724 tp->lost_out += tcp_skb_pcount(skb);
1725 lost = 1;
1730 clear_all_retrans_hints(tp);
1732 if (!lost)
1733 return;
1735 tcp_sync_left_out(tp);
1737 /* Don't muck with the congestion window here.
1738 * Reason is that we do not increase amount of _data_
1739 * in network, but units changed and effective
1740 * cwnd/ssthresh really reduced now.
1742 if (icsk->icsk_ca_state != TCP_CA_Loss) {
1743 tp->high_seq = tp->snd_nxt;
1744 tp->snd_ssthresh = tcp_current_ssthresh(sk);
1745 tp->prior_ssthresh = 0;
1746 tp->undo_marker = 0;
1747 tcp_set_ca_state(sk, TCP_CA_Loss);
1749 tcp_xmit_retransmit_queue(sk);
1752 /* This retransmits one SKB. Policy decisions and retransmit queue
1753 * state updates are done by the caller. Returns non-zero if an
1754 * error occurred which prevented the send.
1756 int tcp_retransmit_skb(struct sock *sk, struct sk_buff *skb)
1758 struct tcp_sock *tp = tcp_sk(sk);
1759 struct inet_connection_sock *icsk = inet_csk(sk);
1760 unsigned int cur_mss;
1761 int err;
1763 /* Inconslusive MTU probe */
1764 if (icsk->icsk_mtup.probe_size) {
1765 icsk->icsk_mtup.probe_size = 0;
1768 /* Do not sent more than we queued. 1/4 is reserved for possible
1769 * copying overhead: fragmentation, tunneling, mangling etc.
1771 if (atomic_read(&sk->sk_wmem_alloc) >
1772 min(sk->sk_wmem_queued + (sk->sk_wmem_queued >> 2), sk->sk_sndbuf))
1773 return -EAGAIN;
1775 if (before(TCP_SKB_CB(skb)->seq, tp->snd_una)) {
1776 if (before(TCP_SKB_CB(skb)->end_seq, tp->snd_una))
1777 BUG();
1778 if (tcp_trim_head(sk, skb, tp->snd_una - TCP_SKB_CB(skb)->seq))
1779 return -ENOMEM;
1782 if (inet_csk(sk)->icsk_af_ops->rebuild_header(sk))
1783 return -EHOSTUNREACH; /* Routing failure or similar. */
1785 cur_mss = tcp_current_mss(sk, 0);
1787 /* If receiver has shrunk his window, and skb is out of
1788 * new window, do not retransmit it. The exception is the
1789 * case, when window is shrunk to zero. In this case
1790 * our retransmit serves as a zero window probe.
1792 if (!before(TCP_SKB_CB(skb)->seq, tp->snd_una+tp->snd_wnd)
1793 && TCP_SKB_CB(skb)->seq != tp->snd_una)
1794 return -EAGAIN;
1796 if (skb->len > cur_mss) {
1797 if (tcp_fragment(sk, skb, cur_mss, cur_mss))
1798 return -ENOMEM; /* We'll try again later. */
1799 } else {
1800 tcp_init_tso_segs(sk, skb, cur_mss);
1803 /* Collapse two adjacent packets if worthwhile and we can. */
1804 if (!(TCP_SKB_CB(skb)->flags & TCPCB_FLAG_SYN) &&
1805 (skb->len < (cur_mss >> 1)) &&
1806 (tcp_write_queue_next(sk, skb) != tcp_send_head(sk)) &&
1807 (!tcp_skb_is_last(sk, skb)) &&
1808 (skb_shinfo(skb)->nr_frags == 0 && skb_shinfo(tcp_write_queue_next(sk, skb))->nr_frags == 0) &&
1809 (tcp_skb_pcount(skb) == 1 && tcp_skb_pcount(tcp_write_queue_next(sk, skb)) == 1) &&
1810 (sysctl_tcp_retrans_collapse != 0))
1811 tcp_retrans_try_collapse(sk, skb, cur_mss);
1813 /* Some Solaris stacks overoptimize and ignore the FIN on a
1814 * retransmit when old data is attached. So strip it off
1815 * since it is cheap to do so and saves bytes on the network.
1817 if (skb->len > 0 &&
1818 (TCP_SKB_CB(skb)->flags & TCPCB_FLAG_FIN) &&
1819 tp->snd_una == (TCP_SKB_CB(skb)->end_seq - 1)) {
1820 if (!pskb_trim(skb, 0)) {
1821 TCP_SKB_CB(skb)->seq = TCP_SKB_CB(skb)->end_seq - 1;
1822 skb_shinfo(skb)->gso_segs = 1;
1823 skb_shinfo(skb)->gso_size = 0;
1824 skb_shinfo(skb)->gso_type = 0;
1825 skb->ip_summed = CHECKSUM_NONE;
1826 skb->csum = 0;
1830 /* Make a copy, if the first transmission SKB clone we made
1831 * is still in somebody's hands, else make a clone.
1833 TCP_SKB_CB(skb)->when = tcp_time_stamp;
1835 err = tcp_transmit_skb(sk, skb, 1, GFP_ATOMIC);
1837 if (err == 0) {
1838 /* Update global TCP statistics. */
1839 TCP_INC_STATS(TCP_MIB_RETRANSSEGS);
1841 tp->total_retrans++;
1843 #if FASTRETRANS_DEBUG > 0
1844 if (TCP_SKB_CB(skb)->sacked&TCPCB_SACKED_RETRANS) {
1845 if (net_ratelimit())
1846 printk(KERN_DEBUG "retrans_out leaked.\n");
1848 #endif
1849 TCP_SKB_CB(skb)->sacked |= TCPCB_RETRANS;
1850 tp->retrans_out += tcp_skb_pcount(skb);
1852 /* Save stamp of the first retransmit. */
1853 if (!tp->retrans_stamp)
1854 tp->retrans_stamp = TCP_SKB_CB(skb)->when;
1856 tp->undo_retrans++;
1858 /* snd_nxt is stored to detect loss of retransmitted segment,
1859 * see tcp_input.c tcp_sacktag_write_queue().
1861 TCP_SKB_CB(skb)->ack_seq = tp->snd_nxt;
1863 return err;
1866 /* This gets called after a retransmit timeout, and the initially
1867 * retransmitted data is acknowledged. It tries to continue
1868 * resending the rest of the retransmit queue, until either
1869 * we've sent it all or the congestion window limit is reached.
1870 * If doing SACK, the first ACK which comes back for a timeout
1871 * based retransmit packet might feed us FACK information again.
1872 * If so, we use it to avoid unnecessarily retransmissions.
1874 void tcp_xmit_retransmit_queue(struct sock *sk)
1876 const struct inet_connection_sock *icsk = inet_csk(sk);
1877 struct tcp_sock *tp = tcp_sk(sk);
1878 struct sk_buff *skb;
1879 int packet_cnt;
1881 if (tp->retransmit_skb_hint) {
1882 skb = tp->retransmit_skb_hint;
1883 packet_cnt = tp->retransmit_cnt_hint;
1884 }else{
1885 skb = tcp_write_queue_head(sk);
1886 packet_cnt = 0;
1889 /* First pass: retransmit lost packets. */
1890 if (tp->lost_out) {
1891 tcp_for_write_queue_from(skb, sk) {
1892 __u8 sacked = TCP_SKB_CB(skb)->sacked;
1894 if (skb == tcp_send_head(sk))
1895 break;
1896 /* we could do better than to assign each time */
1897 tp->retransmit_skb_hint = skb;
1898 tp->retransmit_cnt_hint = packet_cnt;
1900 /* Assume this retransmit will generate
1901 * only one packet for congestion window
1902 * calculation purposes. This works because
1903 * tcp_retransmit_skb() will chop up the
1904 * packet to be MSS sized and all the
1905 * packet counting works out.
1907 if (tcp_packets_in_flight(tp) >= tp->snd_cwnd)
1908 return;
1910 if (sacked & TCPCB_LOST) {
1911 if (!(sacked&(TCPCB_SACKED_ACKED|TCPCB_SACKED_RETRANS))) {
1912 if (tcp_retransmit_skb(sk, skb)) {
1913 tp->retransmit_skb_hint = NULL;
1914 return;
1916 if (icsk->icsk_ca_state != TCP_CA_Loss)
1917 NET_INC_STATS_BH(LINUX_MIB_TCPFASTRETRANS);
1918 else
1919 NET_INC_STATS_BH(LINUX_MIB_TCPSLOWSTARTRETRANS);
1921 if (skb == tcp_write_queue_head(sk))
1922 inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
1923 inet_csk(sk)->icsk_rto,
1924 TCP_RTO_MAX);
1927 packet_cnt += tcp_skb_pcount(skb);
1928 if (packet_cnt >= tp->lost_out)
1929 break;
1934 /* OK, demanded retransmission is finished. */
1936 /* Forward retransmissions are possible only during Recovery. */
1937 if (icsk->icsk_ca_state != TCP_CA_Recovery)
1938 return;
1940 /* No forward retransmissions in Reno are possible. */
1941 if (!tp->rx_opt.sack_ok)
1942 return;
1944 /* Yeah, we have to make difficult choice between forward transmission
1945 * and retransmission... Both ways have their merits...
1947 * For now we do not retransmit anything, while we have some new
1948 * segments to send.
1951 if (tcp_may_send_now(sk))
1952 return;
1954 if (tp->forward_skb_hint) {
1955 skb = tp->forward_skb_hint;
1956 packet_cnt = tp->forward_cnt_hint;
1957 } else{
1958 skb = tcp_write_queue_head(sk);
1959 packet_cnt = 0;
1962 tcp_for_write_queue_from(skb, sk) {
1963 if (skb == tcp_send_head(sk))
1964 break;
1965 tp->forward_cnt_hint = packet_cnt;
1966 tp->forward_skb_hint = skb;
1968 /* Similar to the retransmit loop above we
1969 * can pretend that the retransmitted SKB
1970 * we send out here will be composed of one
1971 * real MSS sized packet because tcp_retransmit_skb()
1972 * will fragment it if necessary.
1974 if (++packet_cnt > tp->fackets_out)
1975 break;
1977 if (tcp_packets_in_flight(tp) >= tp->snd_cwnd)
1978 break;
1980 if (TCP_SKB_CB(skb)->sacked & TCPCB_TAGBITS)
1981 continue;
1983 /* Ok, retransmit it. */
1984 if (tcp_retransmit_skb(sk, skb)) {
1985 tp->forward_skb_hint = NULL;
1986 break;
1989 if (skb == tcp_write_queue_head(sk))
1990 inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
1991 inet_csk(sk)->icsk_rto,
1992 TCP_RTO_MAX);
1994 NET_INC_STATS_BH(LINUX_MIB_TCPFORWARDRETRANS);
1999 /* Send a fin. The caller locks the socket for us. This cannot be
2000 * allowed to fail queueing a FIN frame under any circumstances.
2002 void tcp_send_fin(struct sock *sk)
2004 struct tcp_sock *tp = tcp_sk(sk);
2005 struct sk_buff *skb = tcp_write_queue_tail(sk);
2006 int mss_now;
2008 /* Optimization, tack on the FIN if we have a queue of
2009 * unsent frames. But be careful about outgoing SACKS
2010 * and IP options.
2012 mss_now = tcp_current_mss(sk, 1);
2014 if (tcp_send_head(sk) != NULL) {
2015 TCP_SKB_CB(skb)->flags |= TCPCB_FLAG_FIN;
2016 TCP_SKB_CB(skb)->end_seq++;
2017 tp->write_seq++;
2018 } else {
2019 /* Socket is locked, keep trying until memory is available. */
2020 for (;;) {
2021 skb = alloc_skb_fclone(MAX_TCP_HEADER, GFP_KERNEL);
2022 if (skb)
2023 break;
2024 yield();
2027 /* Reserve space for headers and prepare control bits. */
2028 skb_reserve(skb, MAX_TCP_HEADER);
2029 skb->csum = 0;
2030 TCP_SKB_CB(skb)->flags = (TCPCB_FLAG_ACK | TCPCB_FLAG_FIN);
2031 TCP_SKB_CB(skb)->sacked = 0;
2032 skb_shinfo(skb)->gso_segs = 1;
2033 skb_shinfo(skb)->gso_size = 0;
2034 skb_shinfo(skb)->gso_type = 0;
2036 /* FIN eats a sequence byte, write_seq advanced by tcp_queue_skb(). */
2037 TCP_SKB_CB(skb)->seq = tp->write_seq;
2038 TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(skb)->seq + 1;
2039 tcp_queue_skb(sk, skb);
2041 __tcp_push_pending_frames(sk, mss_now, TCP_NAGLE_OFF);
2044 /* We get here when a process closes a file descriptor (either due to
2045 * an explicit close() or as a byproduct of exit()'ing) and there
2046 * was unread data in the receive queue. This behavior is recommended
2047 * by RFC 2525, section 2.17. -DaveM
2049 void tcp_send_active_reset(struct sock *sk, gfp_t priority)
2051 struct sk_buff *skb;
2053 /* NOTE: No TCP options attached and we never retransmit this. */
2054 skb = alloc_skb(MAX_TCP_HEADER, priority);
2055 if (!skb) {
2056 NET_INC_STATS(LINUX_MIB_TCPABORTFAILED);
2057 return;
2060 /* Reserve space for headers and prepare control bits. */
2061 skb_reserve(skb, MAX_TCP_HEADER);
2062 skb->csum = 0;
2063 TCP_SKB_CB(skb)->flags = (TCPCB_FLAG_ACK | TCPCB_FLAG_RST);
2064 TCP_SKB_CB(skb)->sacked = 0;
2065 skb_shinfo(skb)->gso_segs = 1;
2066 skb_shinfo(skb)->gso_size = 0;
2067 skb_shinfo(skb)->gso_type = 0;
2069 /* Send it off. */
2070 TCP_SKB_CB(skb)->seq = tcp_acceptable_seq(sk);
2071 TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(skb)->seq;
2072 TCP_SKB_CB(skb)->when = tcp_time_stamp;
2073 if (tcp_transmit_skb(sk, skb, 0, priority))
2074 NET_INC_STATS(LINUX_MIB_TCPABORTFAILED);
2077 /* WARNING: This routine must only be called when we have already sent
2078 * a SYN packet that crossed the incoming SYN that caused this routine
2079 * to get called. If this assumption fails then the initial rcv_wnd
2080 * and rcv_wscale values will not be correct.
2082 int tcp_send_synack(struct sock *sk)
2084 struct sk_buff* skb;
2086 skb = tcp_write_queue_head(sk);
2087 if (skb == NULL || !(TCP_SKB_CB(skb)->flags&TCPCB_FLAG_SYN)) {
2088 printk(KERN_DEBUG "tcp_send_synack: wrong queue state\n");
2089 return -EFAULT;
2091 if (!(TCP_SKB_CB(skb)->flags&TCPCB_FLAG_ACK)) {
2092 if (skb_cloned(skb)) {
2093 struct sk_buff *nskb = skb_copy(skb, GFP_ATOMIC);
2094 if (nskb == NULL)
2095 return -ENOMEM;
2096 tcp_unlink_write_queue(skb, sk);
2097 skb_header_release(nskb);
2098 __tcp_add_write_queue_head(sk, nskb);
2099 sk_stream_free_skb(sk, skb);
2100 sk_charge_skb(sk, nskb);
2101 skb = nskb;
2104 TCP_SKB_CB(skb)->flags |= TCPCB_FLAG_ACK;
2105 TCP_ECN_send_synack(tcp_sk(sk), skb);
2107 TCP_SKB_CB(skb)->when = tcp_time_stamp;
2108 return tcp_transmit_skb(sk, skb, 1, GFP_ATOMIC);
2112 * Prepare a SYN-ACK.
2114 struct sk_buff * tcp_make_synack(struct sock *sk, struct dst_entry *dst,
2115 struct request_sock *req)
2117 struct inet_request_sock *ireq = inet_rsk(req);
2118 struct tcp_sock *tp = tcp_sk(sk);
2119 struct tcphdr *th;
2120 int tcp_header_size;
2121 struct sk_buff *skb;
2122 #ifdef CONFIG_TCP_MD5SIG
2123 struct tcp_md5sig_key *md5;
2124 __u8 *md5_hash_location;
2125 #endif
2126 int mss;
2128 skb = sock_wmalloc(sk, MAX_TCP_HEADER + 15, 1, GFP_ATOMIC);
2129 if (skb == NULL)
2130 return NULL;
2132 /* Reserve space for headers. */
2133 skb_reserve(skb, MAX_TCP_HEADER);
2135 skb->dst = dst_clone(dst);
2137 mss = dst_metric(dst, RTAX_ADVMSS);
2138 if (tp->rx_opt.user_mss && tp->rx_opt.user_mss < mss)
2139 mss = tp->rx_opt.user_mss;
2141 tcp_header_size = (sizeof(struct tcphdr) + TCPOLEN_MSS +
2142 (ireq->tstamp_ok ? TCPOLEN_TSTAMP_ALIGNED : 0) +
2143 (ireq->wscale_ok ? TCPOLEN_WSCALE_ALIGNED : 0) +
2144 /* SACK_PERM is in the place of NOP NOP of TS */
2145 ((ireq->sack_ok && !ireq->tstamp_ok) ? TCPOLEN_SACKPERM_ALIGNED : 0));
2147 #ifdef CONFIG_TCP_MD5SIG
2148 /* Are we doing MD5 on this segment? If so - make room for it */
2149 md5 = tcp_rsk(req)->af_specific->md5_lookup(sk, req);
2150 if (md5)
2151 tcp_header_size += TCPOLEN_MD5SIG_ALIGNED;
2152 #endif
2153 skb_push(skb, tcp_header_size);
2154 skb_reset_transport_header(skb);
2156 th = tcp_hdr(skb);
2157 memset(th, 0, sizeof(struct tcphdr));
2158 th->syn = 1;
2159 th->ack = 1;
2160 TCP_ECN_make_synack(req, th);
2161 th->source = inet_sk(sk)->sport;
2162 th->dest = ireq->rmt_port;
2163 TCP_SKB_CB(skb)->seq = tcp_rsk(req)->snt_isn;
2164 TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(skb)->seq + 1;
2165 TCP_SKB_CB(skb)->sacked = 0;
2166 skb_shinfo(skb)->gso_segs = 1;
2167 skb_shinfo(skb)->gso_size = 0;
2168 skb_shinfo(skb)->gso_type = 0;
2169 th->seq = htonl(TCP_SKB_CB(skb)->seq);
2170 th->ack_seq = htonl(tcp_rsk(req)->rcv_isn + 1);
2171 if (req->rcv_wnd == 0) { /* ignored for retransmitted syns */
2172 __u8 rcv_wscale;
2173 /* Set this up on the first call only */
2174 req->window_clamp = tp->window_clamp ? : dst_metric(dst, RTAX_WINDOW);
2175 /* tcp_full_space because it is guaranteed to be the first packet */
2176 tcp_select_initial_window(tcp_full_space(sk),
2177 mss - (ireq->tstamp_ok ? TCPOLEN_TSTAMP_ALIGNED : 0),
2178 &req->rcv_wnd,
2179 &req->window_clamp,
2180 ireq->wscale_ok,
2181 &rcv_wscale);
2182 ireq->rcv_wscale = rcv_wscale;
2185 /* RFC1323: The window in SYN & SYN/ACK segments is never scaled. */
2186 th->window = htons(min(req->rcv_wnd, 65535U));
2188 TCP_SKB_CB(skb)->when = tcp_time_stamp;
2189 tcp_syn_build_options((__be32 *)(th + 1), mss, ireq->tstamp_ok,
2190 ireq->sack_ok, ireq->wscale_ok, ireq->rcv_wscale,
2191 TCP_SKB_CB(skb)->when,
2192 req->ts_recent,
2194 #ifdef CONFIG_TCP_MD5SIG
2195 md5 ? &md5_hash_location :
2196 #endif
2197 NULL)
2200 skb->csum = 0;
2201 th->doff = (tcp_header_size >> 2);
2202 TCP_INC_STATS(TCP_MIB_OUTSEGS);
2204 #ifdef CONFIG_TCP_MD5SIG
2205 /* Okay, we have all we need - do the md5 hash if needed */
2206 if (md5) {
2207 tp->af_specific->calc_md5_hash(md5_hash_location,
2208 md5,
2209 NULL, dst, req,
2210 tcp_hdr(skb), sk->sk_protocol,
2211 skb->len);
2213 #endif
2215 return skb;
2219 * Do all connect socket setups that can be done AF independent.
2221 static void tcp_connect_init(struct sock *sk)
2223 struct dst_entry *dst = __sk_dst_get(sk);
2224 struct tcp_sock *tp = tcp_sk(sk);
2225 __u8 rcv_wscale;
2227 /* We'll fix this up when we get a response from the other end.
2228 * See tcp_input.c:tcp_rcv_state_process case TCP_SYN_SENT.
2230 tp->tcp_header_len = sizeof(struct tcphdr) +
2231 (sysctl_tcp_timestamps ? TCPOLEN_TSTAMP_ALIGNED : 0);
2233 #ifdef CONFIG_TCP_MD5SIG
2234 if (tp->af_specific->md5_lookup(sk, sk) != NULL)
2235 tp->tcp_header_len += TCPOLEN_MD5SIG_ALIGNED;
2236 #endif
2238 /* If user gave his TCP_MAXSEG, record it to clamp */
2239 if (tp->rx_opt.user_mss)
2240 tp->rx_opt.mss_clamp = tp->rx_opt.user_mss;
2241 tp->max_window = 0;
2242 tcp_mtup_init(sk);
2243 tcp_sync_mss(sk, dst_mtu(dst));
2245 if (!tp->window_clamp)
2246 tp->window_clamp = dst_metric(dst, RTAX_WINDOW);
2247 tp->advmss = dst_metric(dst, RTAX_ADVMSS);
2248 if (tp->rx_opt.user_mss && tp->rx_opt.user_mss < tp->advmss)
2249 tp->advmss = tp->rx_opt.user_mss;
2251 tcp_initialize_rcv_mss(sk);
2253 tcp_select_initial_window(tcp_full_space(sk),
2254 tp->advmss - (tp->rx_opt.ts_recent_stamp ? tp->tcp_header_len - sizeof(struct tcphdr) : 0),
2255 &tp->rcv_wnd,
2256 &tp->window_clamp,
2257 sysctl_tcp_window_scaling,
2258 &rcv_wscale);
2260 tp->rx_opt.rcv_wscale = rcv_wscale;
2261 tp->rcv_ssthresh = tp->rcv_wnd;
2263 sk->sk_err = 0;
2264 sock_reset_flag(sk, SOCK_DONE);
2265 tp->snd_wnd = 0;
2266 tcp_init_wl(tp, tp->write_seq, 0);
2267 tp->snd_una = tp->write_seq;
2268 tp->snd_sml = tp->write_seq;
2269 tp->rcv_nxt = 0;
2270 tp->rcv_wup = 0;
2271 tp->copied_seq = 0;
2273 inet_csk(sk)->icsk_rto = TCP_TIMEOUT_INIT;
2274 inet_csk(sk)->icsk_retransmits = 0;
2275 tcp_clear_retrans(tp);
2279 * Build a SYN and send it off.
2281 int tcp_connect(struct sock *sk)
2283 struct tcp_sock *tp = tcp_sk(sk);
2284 struct sk_buff *buff;
2286 tcp_connect_init(sk);
2288 buff = alloc_skb_fclone(MAX_TCP_HEADER + 15, sk->sk_allocation);
2289 if (unlikely(buff == NULL))
2290 return -ENOBUFS;
2292 /* Reserve space for headers. */
2293 skb_reserve(buff, MAX_TCP_HEADER);
2295 TCP_SKB_CB(buff)->flags = TCPCB_FLAG_SYN;
2296 TCP_ECN_send_syn(sk, buff);
2297 TCP_SKB_CB(buff)->sacked = 0;
2298 skb_shinfo(buff)->gso_segs = 1;
2299 skb_shinfo(buff)->gso_size = 0;
2300 skb_shinfo(buff)->gso_type = 0;
2301 buff->csum = 0;
2302 tp->snd_nxt = tp->write_seq;
2303 TCP_SKB_CB(buff)->seq = tp->write_seq++;
2304 TCP_SKB_CB(buff)->end_seq = tp->write_seq;
2306 /* Send it off. */
2307 TCP_SKB_CB(buff)->when = tcp_time_stamp;
2308 tp->retrans_stamp = TCP_SKB_CB(buff)->when;
2309 skb_header_release(buff);
2310 __tcp_add_write_queue_tail(sk, buff);
2311 sk_charge_skb(sk, buff);
2312 tp->packets_out += tcp_skb_pcount(buff);
2313 tcp_transmit_skb(sk, buff, 1, GFP_KERNEL);
2315 /* We change tp->snd_nxt after the tcp_transmit_skb() call
2316 * in order to make this packet get counted in tcpOutSegs.
2318 tp->snd_nxt = tp->write_seq;
2319 tp->pushed_seq = tp->write_seq;
2320 TCP_INC_STATS(TCP_MIB_ACTIVEOPENS);
2322 /* Timer for repeating the SYN until an answer. */
2323 inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
2324 inet_csk(sk)->icsk_rto, TCP_RTO_MAX);
2325 return 0;
2328 /* Send out a delayed ack, the caller does the policy checking
2329 * to see if we should even be here. See tcp_input.c:tcp_ack_snd_check()
2330 * for details.
2332 void tcp_send_delayed_ack(struct sock *sk)
2334 struct inet_connection_sock *icsk = inet_csk(sk);
2335 int ato = icsk->icsk_ack.ato;
2336 unsigned long timeout;
2338 if (ato > TCP_DELACK_MIN) {
2339 const struct tcp_sock *tp = tcp_sk(sk);
2340 int max_ato = HZ/2;
2342 if (icsk->icsk_ack.pingpong || (icsk->icsk_ack.pending & ICSK_ACK_PUSHED))
2343 max_ato = TCP_DELACK_MAX;
2345 /* Slow path, intersegment interval is "high". */
2347 /* If some rtt estimate is known, use it to bound delayed ack.
2348 * Do not use inet_csk(sk)->icsk_rto here, use results of rtt measurements
2349 * directly.
2351 if (tp->srtt) {
2352 int rtt = max(tp->srtt>>3, TCP_DELACK_MIN);
2354 if (rtt < max_ato)
2355 max_ato = rtt;
2358 ato = min(ato, max_ato);
2361 /* Stay within the limit we were given */
2362 timeout = jiffies + ato;
2364 /* Use new timeout only if there wasn't a older one earlier. */
2365 if (icsk->icsk_ack.pending & ICSK_ACK_TIMER) {
2366 /* If delack timer was blocked or is about to expire,
2367 * send ACK now.
2369 if (icsk->icsk_ack.blocked ||
2370 time_before_eq(icsk->icsk_ack.timeout, jiffies + (ato >> 2))) {
2371 tcp_send_ack(sk);
2372 return;
2375 if (!time_before(timeout, icsk->icsk_ack.timeout))
2376 timeout = icsk->icsk_ack.timeout;
2378 icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER;
2379 icsk->icsk_ack.timeout = timeout;
2380 sk_reset_timer(sk, &icsk->icsk_delack_timer, timeout);
2383 /* This routine sends an ack and also updates the window. */
2384 void tcp_send_ack(struct sock *sk)
2386 /* If we have been reset, we may not send again. */
2387 if (sk->sk_state != TCP_CLOSE) {
2388 struct sk_buff *buff;
2390 /* We are not putting this on the write queue, so
2391 * tcp_transmit_skb() will set the ownership to this
2392 * sock.
2394 buff = alloc_skb(MAX_TCP_HEADER, GFP_ATOMIC);
2395 if (buff == NULL) {
2396 inet_csk_schedule_ack(sk);
2397 inet_csk(sk)->icsk_ack.ato = TCP_ATO_MIN;
2398 inet_csk_reset_xmit_timer(sk, ICSK_TIME_DACK,
2399 TCP_DELACK_MAX, TCP_RTO_MAX);
2400 return;
2403 /* Reserve space for headers and prepare control bits. */
2404 skb_reserve(buff, MAX_TCP_HEADER);
2405 buff->csum = 0;
2406 TCP_SKB_CB(buff)->flags = TCPCB_FLAG_ACK;
2407 TCP_SKB_CB(buff)->sacked = 0;
2408 skb_shinfo(buff)->gso_segs = 1;
2409 skb_shinfo(buff)->gso_size = 0;
2410 skb_shinfo(buff)->gso_type = 0;
2412 /* Send it off, this clears delayed acks for us. */
2413 TCP_SKB_CB(buff)->seq = TCP_SKB_CB(buff)->end_seq = tcp_acceptable_seq(sk);
2414 TCP_SKB_CB(buff)->when = tcp_time_stamp;
2415 tcp_transmit_skb(sk, buff, 0, GFP_ATOMIC);
2419 /* This routine sends a packet with an out of date sequence
2420 * number. It assumes the other end will try to ack it.
2422 * Question: what should we make while urgent mode?
2423 * 4.4BSD forces sending single byte of data. We cannot send
2424 * out of window data, because we have SND.NXT==SND.MAX...
2426 * Current solution: to send TWO zero-length segments in urgent mode:
2427 * one is with SEG.SEQ=SND.UNA to deliver urgent pointer, another is
2428 * out-of-date with SND.UNA-1 to probe window.
2430 static int tcp_xmit_probe_skb(struct sock *sk, int urgent)
2432 struct tcp_sock *tp = tcp_sk(sk);
2433 struct sk_buff *skb;
2435 /* We don't queue it, tcp_transmit_skb() sets ownership. */
2436 skb = alloc_skb(MAX_TCP_HEADER, GFP_ATOMIC);
2437 if (skb == NULL)
2438 return -1;
2440 /* Reserve space for headers and set control bits. */
2441 skb_reserve(skb, MAX_TCP_HEADER);
2442 skb->csum = 0;
2443 TCP_SKB_CB(skb)->flags = TCPCB_FLAG_ACK;
2444 TCP_SKB_CB(skb)->sacked = urgent;
2445 skb_shinfo(skb)->gso_segs = 1;
2446 skb_shinfo(skb)->gso_size = 0;
2447 skb_shinfo(skb)->gso_type = 0;
2449 /* Use a previous sequence. This should cause the other
2450 * end to send an ack. Don't queue or clone SKB, just
2451 * send it.
2453 TCP_SKB_CB(skb)->seq = urgent ? tp->snd_una : tp->snd_una - 1;
2454 TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(skb)->seq;
2455 TCP_SKB_CB(skb)->when = tcp_time_stamp;
2456 return tcp_transmit_skb(sk, skb, 0, GFP_ATOMIC);
2459 int tcp_write_wakeup(struct sock *sk)
2461 if (sk->sk_state != TCP_CLOSE) {
2462 struct tcp_sock *tp = tcp_sk(sk);
2463 struct sk_buff *skb;
2465 if ((skb = tcp_send_head(sk)) != NULL &&
2466 before(TCP_SKB_CB(skb)->seq, tp->snd_una+tp->snd_wnd)) {
2467 int err;
2468 unsigned int mss = tcp_current_mss(sk, 0);
2469 unsigned int seg_size = tp->snd_una+tp->snd_wnd-TCP_SKB_CB(skb)->seq;
2471 if (before(tp->pushed_seq, TCP_SKB_CB(skb)->end_seq))
2472 tp->pushed_seq = TCP_SKB_CB(skb)->end_seq;
2474 /* We are probing the opening of a window
2475 * but the window size is != 0
2476 * must have been a result SWS avoidance ( sender )
2478 if (seg_size < TCP_SKB_CB(skb)->end_seq - TCP_SKB_CB(skb)->seq ||
2479 skb->len > mss) {
2480 seg_size = min(seg_size, mss);
2481 TCP_SKB_CB(skb)->flags |= TCPCB_FLAG_PSH;
2482 if (tcp_fragment(sk, skb, seg_size, mss))
2483 return -1;
2484 } else if (!tcp_skb_pcount(skb))
2485 tcp_set_skb_tso_segs(sk, skb, mss);
2487 TCP_SKB_CB(skb)->flags |= TCPCB_FLAG_PSH;
2488 TCP_SKB_CB(skb)->when = tcp_time_stamp;
2489 err = tcp_transmit_skb(sk, skb, 1, GFP_ATOMIC);
2490 if (!err) {
2491 update_send_head(sk, skb);
2493 return err;
2494 } else {
2495 if (tp->urg_mode &&
2496 between(tp->snd_up, tp->snd_una+1, tp->snd_una+0xFFFF))
2497 tcp_xmit_probe_skb(sk, TCPCB_URG);
2498 return tcp_xmit_probe_skb(sk, 0);
2501 return -1;
2504 /* A window probe timeout has occurred. If window is not closed send
2505 * a partial packet else a zero probe.
2507 void tcp_send_probe0(struct sock *sk)
2509 struct inet_connection_sock *icsk = inet_csk(sk);
2510 struct tcp_sock *tp = tcp_sk(sk);
2511 int err;
2513 err = tcp_write_wakeup(sk);
2515 if (tp->packets_out || !tcp_send_head(sk)) {
2516 /* Cancel probe timer, if it is not required. */
2517 icsk->icsk_probes_out = 0;
2518 icsk->icsk_backoff = 0;
2519 return;
2522 if (err <= 0) {
2523 if (icsk->icsk_backoff < sysctl_tcp_retries2)
2524 icsk->icsk_backoff++;
2525 icsk->icsk_probes_out++;
2526 inet_csk_reset_xmit_timer(sk, ICSK_TIME_PROBE0,
2527 min(icsk->icsk_rto << icsk->icsk_backoff, TCP_RTO_MAX),
2528 TCP_RTO_MAX);
2529 } else {
2530 /* If packet was not sent due to local congestion,
2531 * do not backoff and do not remember icsk_probes_out.
2532 * Let local senders to fight for local resources.
2534 * Use accumulated backoff yet.
2536 if (!icsk->icsk_probes_out)
2537 icsk->icsk_probes_out = 1;
2538 inet_csk_reset_xmit_timer(sk, ICSK_TIME_PROBE0,
2539 min(icsk->icsk_rto << icsk->icsk_backoff,
2540 TCP_RESOURCE_PROBE_INTERVAL),
2541 TCP_RTO_MAX);
2545 EXPORT_SYMBOL(tcp_connect);
2546 EXPORT_SYMBOL(tcp_make_synack);
2547 EXPORT_SYMBOL(tcp_simple_retransmit);
2548 EXPORT_SYMBOL(tcp_sync_mss);
2549 EXPORT_SYMBOL(sysctl_tcp_tso_win_divisor);
2550 EXPORT_SYMBOL(tcp_mtup_init);