Lines Matching +full:tp +full:- +full:sensitive +full:- +full:adjust
1 // SPDX-License-Identifier: GPL-2.0-only
12 * Corey Minyard <wf-rch!minyard@relay.EU.net>
13 * Florian La Roche, <flla@stud.uni-sb.de>
33 * Cacophonix Gaul : draft-minshall-nagle-01
53 void tcp_mstamp_refresh(struct tcp_sock *tp) in tcp_mstamp_refresh() argument
57 tp->tcp_clock_cache = val; in tcp_mstamp_refresh()
58 tp->tcp_mstamp = div_u64(val, NSEC_PER_USEC); in tcp_mstamp_refresh()
68 struct tcp_sock *tp = tcp_sk(sk); in tcp_event_new_data_sent() local
69 unsigned int prior_packets = tp->packets_out; in tcp_event_new_data_sent()
71 WRITE_ONCE(tp->snd_nxt, TCP_SKB_CB(skb)->end_seq); in tcp_event_new_data_sent()
73 __skb_unlink(skb, &sk->sk_write_queue); in tcp_event_new_data_sent()
74 tcp_rbtree_insert(&sk->tcp_rtx_queue, skb); in tcp_event_new_data_sent()
76 if (tp->highest_sack == NULL) in tcp_event_new_data_sent()
77 tp->highest_sack = skb; in tcp_event_new_data_sent()
79 tp->packets_out += tcp_skb_pcount(skb); in tcp_event_new_data_sent()
80 if (!prior_packets || icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) in tcp_event_new_data_sent()
90 * Using SND.UNA we will fail to open window, SND.NXT is out of window. :-(
96 const struct tcp_sock *tp = tcp_sk(sk); in tcp_acceptable_seq() local
98 if (!before(tcp_wnd_end(tp), tp->snd_nxt) || in tcp_acceptable_seq()
99 (tp->rx_opt.wscale_ok && in tcp_acceptable_seq()
100 ((tp->snd_nxt - tcp_wnd_end(tp)) < (1 << tp->rx_opt.rcv_wscale)))) in tcp_acceptable_seq()
101 return tp->snd_nxt; in tcp_acceptable_seq()
103 return tcp_wnd_end(tp); in tcp_acceptable_seq()
107 * RFC1122, RFC1063, draft-ietf-tcpimpl-pmtud-01 state that:
110 * 2. Ideally, it is maximal possible segment size i.e. 65535-40.
122 struct tcp_sock *tp = tcp_sk(sk); in tcp_advertise_mss() local
124 int mss = tp->advmss; in tcp_advertise_mss()
131 tp->advmss = mss; in tcp_advertise_mss()
143 struct tcp_sock *tp = tcp_sk(sk); in tcp_cwnd_restart() local
144 u32 restart_cwnd = tcp_init_cwnd(tp, __sk_dst_get(sk)); in tcp_cwnd_restart()
145 u32 cwnd = tp->snd_cwnd; in tcp_cwnd_restart()
149 tp->snd_ssthresh = tcp_current_ssthresh(sk); in tcp_cwnd_restart()
152 while ((delta -= inet_csk(sk)->icsk_rto) > 0 && cwnd > restart_cwnd) in tcp_cwnd_restart()
154 tp->snd_cwnd = max(cwnd, restart_cwnd); in tcp_cwnd_restart()
155 tp->snd_cwnd_stamp = tcp_jiffies32; in tcp_cwnd_restart()
156 tp->snd_cwnd_used = 0; in tcp_cwnd_restart()
160 static void tcp_event_data_sent(struct tcp_sock *tp, in tcp_event_data_sent() argument
166 if (tcp_packets_in_flight(tp) == 0) in tcp_event_data_sent()
174 if (before(tp->lsndtime, icsk->icsk_ack.lrcvtime) && in tcp_event_data_sent()
175 (u32)(now - icsk->icsk_ack.lrcvtime) < icsk->icsk_ack.ato) in tcp_event_data_sent()
178 tp->lsndtime = now; in tcp_event_data_sent()
185 struct tcp_sock *tp = tcp_sk(sk); in tcp_event_ack_sent() local
187 if (unlikely(tp->compressed_ack)) { in tcp_event_ack_sent()
189 tp->compressed_ack); in tcp_event_ack_sent()
190 tp->compressed_ack = 0; in tcp_event_ack_sent()
191 if (hrtimer_try_to_cancel(&tp->compressed_ack_timer) == 1) in tcp_event_ack_sent()
195 if (unlikely(rcv_nxt != tp->rcv_nxt)) in tcp_event_ack_sent()
203 * will be offered. Store the results in the tp structure.
227 * we will truncate our initial window offering to 32K-1 in tcp_select_initial_window()
232 if (sock_net(sk)->ipv4.sysctl_tcp_workaround_signed_windows) in tcp_select_initial_window()
243 space = max_t(u32, space, sock_net(sk)->ipv4.sysctl_tcp_rmem[2]); in tcp_select_initial_window()
246 *rcv_wscale = clamp_t(int, ilog2(space) - 15, in tcp_select_initial_window()
256 * value can be stuffed directly into th->window for an outgoing
261 struct tcp_sock *tp = tcp_sk(sk); in tcp_select_window() local
262 u32 old_win = tp->rcv_wnd; in tcp_select_window()
263 u32 cur_win = tcp_receive_window(tp); in tcp_select_window()
271 * window in time. --DaveM in tcp_select_window()
278 new_win = ALIGN(cur_win, 1 << tp->rx_opt.rcv_wscale); in tcp_select_window()
280 tp->rcv_wnd = new_win; in tcp_select_window()
281 tp->rcv_wup = tp->rcv_nxt; in tcp_select_window()
286 if (!tp->rx_opt.rcv_wscale && in tcp_select_window()
287 sock_net(sk)->ipv4.sysctl_tcp_workaround_signed_windows) in tcp_select_window()
290 new_win = min(new_win, (65535U << tp->rx_opt.rcv_wscale)); in tcp_select_window()
293 new_win >>= tp->rx_opt.rcv_wscale; in tcp_select_window()
297 tp->pred_flags = 0; in tcp_select_window()
308 /* Packet ECN state for a SYN-ACK */
311 const struct tcp_sock *tp = tcp_sk(sk); in tcp_ecn_send_synack() local
313 TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_CWR; in tcp_ecn_send_synack()
314 if (!(tp->ecn_flags & TCP_ECN_OK)) in tcp_ecn_send_synack()
315 TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_ECE; in tcp_ecn_send_synack()
324 struct tcp_sock *tp = tcp_sk(sk); in tcp_ecn_send_syn() local
326 bool use_ecn = sock_net(sk)->ipv4.sysctl_tcp_ecn == 1 || in tcp_ecn_send_syn()
336 tp->ecn_flags = 0; in tcp_ecn_send_syn()
339 TCP_SKB_CB(skb)->tcp_flags |= TCPHDR_ECE | TCPHDR_CWR; in tcp_ecn_send_syn()
340 tp->ecn_flags = TCP_ECN_OK; in tcp_ecn_send_syn()
348 if (sock_net(sk)->ipv4.sysctl_tcp_ecn_fallback) in tcp_ecn_clear_syn()
349 /* tp->ecn_flags are cleared at a later point in time when in tcp_ecn_clear_syn()
352 TCP_SKB_CB(skb)->tcp_flags &= ~(TCPHDR_ECE | TCPHDR_CWR); in tcp_ecn_clear_syn()
358 if (inet_rsk(req)->ecn_ok) in tcp_ecn_make_synack()
359 th->ece = 1; in tcp_ecn_make_synack()
368 struct tcp_sock *tp = tcp_sk(sk); in tcp_ecn_send() local
370 if (tp->ecn_flags & TCP_ECN_OK) { in tcp_ecn_send()
371 /* Not-retransmitted data segment: set ECT and inject CWR. */ in tcp_ecn_send()
372 if (skb->len != tcp_header_len && in tcp_ecn_send()
373 !before(TCP_SKB_CB(skb)->seq, tp->snd_nxt)) { in tcp_ecn_send()
375 if (tp->ecn_flags & TCP_ECN_QUEUE_CWR) { in tcp_ecn_send()
376 tp->ecn_flags &= ~TCP_ECN_QUEUE_CWR; in tcp_ecn_send()
377 th->cwr = 1; in tcp_ecn_send()
378 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN; in tcp_ecn_send()
384 if (tp->ecn_flags & TCP_ECN_DEMAND_CWR) in tcp_ecn_send()
385 th->ece = 1; in tcp_ecn_send()
389 /* Constructs common control bits of non-data skb. If SYN/FIN is present,
394 skb->ip_summed = CHECKSUM_PARTIAL; in tcp_init_nondata_skb()
396 TCP_SKB_CB(skb)->tcp_flags = flags; in tcp_init_nondata_skb()
397 TCP_SKB_CB(skb)->sacked = 0; in tcp_init_nondata_skb()
401 TCP_SKB_CB(skb)->seq = seq; in tcp_init_nondata_skb()
404 TCP_SKB_CB(skb)->end_seq = seq; in tcp_init_nondata_skb()
407 static inline bool tcp_urg_mode(const struct tcp_sock *tp) in tcp_urg_mode() argument
409 return tp->snd_una != tp->snd_up; in tcp_urg_mode()
451 if (unlikely(OPTION_MPTCP & opts->options)) in mptcp_options_write()
452 mptcp_write_options(ptr, &opts->mptcp); in mptcp_options_write()
498 * Thus, "req" is passed here and the cgroup-bpf-progs in bpf_skops_hdr_opt_len()
503 * consistent between fastopen and non-fastopen on in bpf_skops_hdr_opt_len()
526 opts->bpf_opt_len = *remaining - sock_ops.remaining_opt_len; in bpf_skops_hdr_opt_len()
528 opts->bpf_opt_len = (opts->bpf_opt_len + 3) & ~3; in bpf_skops_hdr_opt_len()
530 *remaining -= opts->bpf_opt_len; in bpf_skops_hdr_opt_len()
539 u8 first_opt_off, nr_written, max_opt_len = opts->bpf_opt_len; in bpf_skops_write_hdr_opt()
562 first_opt_off = tcp_hdrlen(skb) - max_opt_len; in bpf_skops_write_hdr_opt()
570 nr_written = max_opt_len - sock_ops.remaining_opt_len; in bpf_skops_write_hdr_opt()
573 memset(skb->data + first_opt_off + nr_written, TCPOPT_NOP, in bpf_skops_write_hdr_opt()
574 max_opt_len - nr_written); in bpf_skops_write_hdr_opt()
597 * Beware: Something in the Internet is very sensitive to the ordering of
599 * Luckily we can at least blame others for their non-compliance but from
600 * inter-operability perspective it seems that we're somewhat stuck with
608 static void tcp_options_write(__be32 *ptr, struct tcp_sock *tp, in tcp_options_write() argument
611 u16 options = opts->options; /* mungable copy */ in tcp_options_write()
617 opts->hash_location = (__u8 *)ptr; in tcp_options_write()
621 if (unlikely(opts->mss)) { in tcp_options_write()
624 opts->mss); in tcp_options_write()
640 *ptr++ = htonl(opts->tsval); in tcp_options_write()
641 *ptr++ = htonl(opts->tsecr); in tcp_options_write()
655 opts->ws); in tcp_options_write()
658 if (unlikely(opts->num_sack_blocks)) { in tcp_options_write()
659 struct tcp_sack_block *sp = tp->rx_opt.dsack ? in tcp_options_write()
660 tp->duplicate_sack : tp->selective_acks; in tcp_options_write()
666 (TCPOLEN_SACK_BASE + (opts->num_sack_blocks * in tcp_options_write()
669 for (this_sack = 0; this_sack < opts->num_sack_blocks; in tcp_options_write()
675 tp->rx_opt.dsack = 0; in tcp_options_write()
679 struct tcp_fastopen_cookie *foc = opts->fastopen_cookie; in tcp_options_write()
683 if (foc->exp) { in tcp_options_write()
684 len = TCPOLEN_EXP_FASTOPEN_BASE + foc->len; in tcp_options_write()
689 len = TCPOLEN_FASTOPEN_BASE + foc->len; in tcp_options_write()
694 memcpy(p, foc->val, foc->len); in tcp_options_write()
696 p[foc->len] = TCPOPT_NOP; in tcp_options_write()
697 p[foc->len + 1] = TCPOPT_NOP; in tcp_options_write()
707 static void smc_set_option(const struct tcp_sock *tp, in smc_set_option() argument
713 if (tp->syn_smc) { in smc_set_option()
715 opts->options |= OPTION_SMC; in smc_set_option()
716 *remaining -= TCPOLEN_EXP_SMC_BASE_ALIGNED; in smc_set_option()
723 static void smc_set_option_cond(const struct tcp_sock *tp, in smc_set_option_cond() argument
730 if (tp->syn_smc && ireq->smc_ok) { in smc_set_option_cond()
732 opts->options |= OPTION_SMC; in smc_set_option_cond()
733 *remaining -= TCPOLEN_EXP_SMC_BASE_ALIGNED; in smc_set_option_cond()
747 if (mptcp_synack_options(req, &size, &opts->mptcp)) { in mptcp_set_option_cond()
749 opts->options |= OPTION_MPTCP; in mptcp_set_option_cond()
750 *remaining -= size; in mptcp_set_option_cond()
763 struct tcp_sock *tp = tcp_sk(sk); in tcp_syn_options() local
765 struct tcp_fastopen_request *fastopen = tp->fastopen_req; in tcp_syn_options()
770 rcu_access_pointer(tp->md5sig_info)) { in tcp_syn_options()
771 *md5 = tp->af_specific->md5_lookup(sk, sk); in tcp_syn_options()
773 opts->options |= OPTION_MD5; in tcp_syn_options()
774 remaining -= TCPOLEN_MD5SIG_ALIGNED; in tcp_syn_options()
781 * advertised. But we subtract them from tp->mss_cache so that in tcp_syn_options()
788 opts->mss = tcp_advertise_mss(sk); in tcp_syn_options()
789 remaining -= TCPOLEN_MSS_ALIGNED; in tcp_syn_options()
791 if (likely(sock_net(sk)->ipv4.sysctl_tcp_timestamps && !*md5)) { in tcp_syn_options()
792 opts->options |= OPTION_TS; in tcp_syn_options()
793 opts->tsval = tcp_skb_timestamp(skb) + tp->tsoffset; in tcp_syn_options()
794 opts->tsecr = tp->rx_opt.ts_recent; in tcp_syn_options()
795 remaining -= TCPOLEN_TSTAMP_ALIGNED; in tcp_syn_options()
797 if (likely(sock_net(sk)->ipv4.sysctl_tcp_window_scaling)) { in tcp_syn_options()
798 opts->ws = tp->rx_opt.rcv_wscale; in tcp_syn_options()
799 opts->options |= OPTION_WSCALE; in tcp_syn_options()
800 remaining -= TCPOLEN_WSCALE_ALIGNED; in tcp_syn_options()
802 if (likely(sock_net(sk)->ipv4.sysctl_tcp_sack)) { in tcp_syn_options()
803 opts->options |= OPTION_SACK_ADVERTISE; in tcp_syn_options()
804 if (unlikely(!(OPTION_TS & opts->options))) in tcp_syn_options()
805 remaining -= TCPOLEN_SACKPERM_ALIGNED; in tcp_syn_options()
808 if (fastopen && fastopen->cookie.len >= 0) { in tcp_syn_options()
809 u32 need = fastopen->cookie.len; in tcp_syn_options()
811 need += fastopen->cookie.exp ? TCPOLEN_EXP_FASTOPEN_BASE : in tcp_syn_options()
815 opts->options |= OPTION_FAST_OPEN_COOKIE; in tcp_syn_options()
816 opts->fastopen_cookie = &fastopen->cookie; in tcp_syn_options()
817 remaining -= need; in tcp_syn_options()
818 tp->syn_fastopen = 1; in tcp_syn_options()
819 tp->syn_fastopen_exp = fastopen->cookie.exp ? 1 : 0; in tcp_syn_options()
823 smc_set_option(tp, opts, &remaining); in tcp_syn_options()
828 if (mptcp_syn_options(sk, skb, &size, &opts->mptcp)) { in tcp_syn_options()
829 opts->options |= OPTION_MPTCP; in tcp_syn_options()
830 remaining -= size; in tcp_syn_options()
836 return MAX_TCP_OPTION_SPACE - remaining; in tcp_syn_options()
839 /* Set up TCP options for SYN-ACKs. */
854 opts->options |= OPTION_MD5; in tcp_synack_options()
855 remaining -= TCPOLEN_MD5SIG_ALIGNED; in tcp_synack_options()
863 ireq->tstamp_ok &= !ireq->sack_ok; in tcp_synack_options()
868 opts->mss = mss; in tcp_synack_options()
869 remaining -= TCPOLEN_MSS_ALIGNED; in tcp_synack_options()
871 if (likely(ireq->wscale_ok)) { in tcp_synack_options()
872 opts->ws = ireq->rcv_wscale; in tcp_synack_options()
873 opts->options |= OPTION_WSCALE; in tcp_synack_options()
874 remaining -= TCPOLEN_WSCALE_ALIGNED; in tcp_synack_options()
876 if (likely(ireq->tstamp_ok)) { in tcp_synack_options()
877 opts->options |= OPTION_TS; in tcp_synack_options()
878 opts->tsval = tcp_skb_timestamp(skb) + tcp_rsk(req)->ts_off; in tcp_synack_options()
879 opts->tsecr = req->ts_recent; in tcp_synack_options()
880 remaining -= TCPOLEN_TSTAMP_ALIGNED; in tcp_synack_options()
882 if (likely(ireq->sack_ok)) { in tcp_synack_options()
883 opts->options |= OPTION_SACK_ADVERTISE; in tcp_synack_options()
884 if (unlikely(!ireq->tstamp_ok)) in tcp_synack_options()
885 remaining -= TCPOLEN_SACKPERM_ALIGNED; in tcp_synack_options()
887 if (foc != NULL && foc->len >= 0) { in tcp_synack_options()
888 u32 need = foc->len; in tcp_synack_options()
890 need += foc->exp ? TCPOLEN_EXP_FASTOPEN_BASE : in tcp_synack_options()
894 opts->options |= OPTION_FAST_OPEN_COOKIE; in tcp_synack_options()
895 opts->fastopen_cookie = foc; in tcp_synack_options()
896 remaining -= need; in tcp_synack_options()
907 return MAX_TCP_OPTION_SPACE - remaining; in tcp_synack_options()
917 struct tcp_sock *tp = tcp_sk(sk); in tcp_established_options() local
921 opts->options = 0; in tcp_established_options()
926 rcu_access_pointer(tp->md5sig_info)) { in tcp_established_options()
927 *md5 = tp->af_specific->md5_lookup(sk, sk); in tcp_established_options()
929 opts->options |= OPTION_MD5; in tcp_established_options()
935 if (likely(tp->rx_opt.tstamp_ok)) { in tcp_established_options()
936 opts->options |= OPTION_TS; in tcp_established_options()
937 opts->tsval = skb ? tcp_skb_timestamp(skb) + tp->tsoffset : 0; in tcp_established_options()
938 opts->tsecr = tp->rx_opt.ts_recent; in tcp_established_options()
949 unsigned int remaining = MAX_TCP_OPTION_SPACE - size; in tcp_established_options()
953 &opts->mptcp)) { in tcp_established_options()
954 opts->options |= OPTION_MPTCP; in tcp_established_options()
959 eff_sacks = tp->rx_opt.num_sacks + tp->rx_opt.dsack; in tcp_established_options()
961 const unsigned int remaining = MAX_TCP_OPTION_SPACE - size; in tcp_established_options()
966 opts->num_sack_blocks = in tcp_established_options()
968 (remaining - TCPOLEN_SACK_BASE_ALIGNED) / in tcp_established_options()
972 opts->num_sack_blocks * TCPOLEN_SACK_PERBLOCK; in tcp_established_options()
975 if (unlikely(BPF_SOCK_OPS_TEST_FLAG(tp, in tcp_established_options()
977 unsigned int remaining = MAX_TCP_OPTION_SPACE - size; in tcp_established_options()
981 size = MAX_TCP_OPTION_SPACE - remaining; in tcp_established_options()
996 * The invariant being skb->truesize subtracted from sk->sk_wmem_alloc
1010 if ((1 << sk->sk_state) & in tcp_tsq_write()
1013 struct tcp_sock *tp = tcp_sk(sk); in tcp_tsq_write() local
1015 if (tp->lost_out > tp->retrans_out && in tcp_tsq_write()
1016 tp->snd_cwnd > tcp_packets_in_flight(tp)) { in tcp_tsq_write()
1017 tcp_mstamp_refresh(tp); in tcp_tsq_write()
1021 tcp_write_xmit(sk, tcp_current_mss(sk), tp->nonagle, in tcp_tsq_write()
1031 else if (!test_and_set_bit(TCP_TSQ_DEFERRED, &sk->sk_tsq_flags)) in tcp_tsq_handler()
1038 * transferring tsq->head because tcp_wfree() might
1047 struct tcp_sock *tp; in tcp_tasklet_func() local
1051 list_splice_init(&tsq->head, &list); in tcp_tasklet_func()
1055 tp = list_entry(q, struct tcp_sock, tsq_node); in tcp_tasklet_func()
1056 list_del(&tp->tsq_node); in tcp_tasklet_func()
1058 sk = (struct sock *)tp; in tcp_tasklet_func()
1060 clear_bit(TSQ_QUEUED, &sk->sk_tsq_flags); in tcp_tasklet_func()
1072 * tcp_release_cb - tcp release_sock() callback
1084 flags = sk->sk_tsq_flags; in tcp_release_cb()
1088 } while (cmpxchg(&sk->sk_tsq_flags, flags, nflags) != flags); in tcp_release_cb()
1098 * 3) socket owned by us (sk->sk_lock.owned == 1) in tcp_release_cb()
1114 inet_csk(sk)->icsk_af_ops->mtu_reduced(sk); in tcp_release_cb()
1127 INIT_LIST_HEAD(&tsq->head); in tcp_tasklet_init()
1128 tasklet_init(&tsq->tasklet, in tcp_tasklet_init()
1141 struct sock *sk = skb->sk; in tcp_wfree()
1142 struct tcp_sock *tp = tcp_sk(sk); in tcp_wfree() local
1148 WARN_ON(refcount_sub_and_test(skb->truesize - 1, &sk->sk_wmem_alloc)); in tcp_wfree()
1153 * - less callbacks to tcp_write_xmit(), reducing stress (batches) in tcp_wfree()
1154 * - chance for incoming ACK (processed by another cpu maybe) in tcp_wfree()
1155 * to migrate this flow (skb->ooo_okay will be eventually set) in tcp_wfree()
1157 if (refcount_read(&sk->sk_wmem_alloc) >= SKB_TRUESIZE(1) && this_cpu_ksoftirqd() == current) in tcp_wfree()
1160 for (oval = READ_ONCE(sk->sk_tsq_flags);; oval = nval) { in tcp_wfree()
1168 nval = cmpxchg(&sk->sk_tsq_flags, oval, nval); in tcp_wfree()
1175 empty = list_empty(&tsq->head); in tcp_wfree()
1176 list_add(&tp->tsq_node, &tsq->head); in tcp_wfree()
1178 tasklet_schedule(&tsq->tasklet); in tcp_wfree()
1191 struct tcp_sock *tp = container_of(timer, struct tcp_sock, pacing_timer); in tcp_pace_kick() local
1192 struct sock *sk = (struct sock *)tp; in tcp_pace_kick()
1203 struct tcp_sock *tp = tcp_sk(sk); in tcp_update_skb_after_send() local
1205 if (sk->sk_pacing_status != SK_PACING_NONE) { in tcp_update_skb_after_send()
1206 unsigned long rate = sk->sk_pacing_rate; in tcp_update_skb_after_send()
1209 * Note that tp->data_segs_out overflows after 2^32 packets, in tcp_update_skb_after_send()
1212 if (rate != ~0UL && rate && tp->data_segs_out >= 10) { in tcp_update_skb_after_send()
1213 u64 len_ns = div64_ul((u64)skb->len * NSEC_PER_SEC, rate); in tcp_update_skb_after_send()
1214 u64 credit = tp->tcp_wstamp_ns - prior_wstamp; in tcp_update_skb_after_send()
1217 len_ns -= min_t(u64, len_ns / 2, credit); in tcp_update_skb_after_send()
1218 tp->tcp_wstamp_ns += len_ns; in tcp_update_skb_after_send()
1221 list_move_tail(&skb->tcp_tsorted_anchor, &tp->tsorted_sent_queue); in tcp_update_skb_after_send()
1244 struct tcp_sock *tp; in __tcp_transmit_skb() local
1255 tp = tcp_sk(sk); in __tcp_transmit_skb()
1256 prior_wstamp = tp->tcp_wstamp_ns; in __tcp_transmit_skb()
1257 tp->tcp_wstamp_ns = max(tp->tcp_wstamp_ns, tp->tcp_clock_cache); in __tcp_transmit_skb()
1258 skb->skb_mstamp_ns = tp->tcp_wstamp_ns; in __tcp_transmit_skb()
1260 TCP_SKB_CB(skb)->tx.in_flight = TCP_SKB_CB(skb)->end_seq in __tcp_transmit_skb()
1261 - tp->snd_una; in __tcp_transmit_skb()
1272 return -ENOBUFS; in __tcp_transmit_skb()
1273 /* retransmit skbs might have a non zero value in skb->dev in __tcp_transmit_skb()
1274 * because skb->dev is aliased with skb->rbnode.rb_left in __tcp_transmit_skb()
1276 skb->dev = NULL; in __tcp_transmit_skb()
1283 if (unlikely(tcb->tcp_flags & TCPHDR_SYN)) { in __tcp_transmit_skb()
1292 * and in this case it is better to delay the delivery of 1-MSS in __tcp_transmit_skb()
1297 tcb->tcp_flags |= TCPHDR_PSH; in __tcp_transmit_skb()
1305 * TODO: Ideally, in-flight pure ACK packets should not matter here. in __tcp_transmit_skb()
1306 * One way to get this would be to set skb->truesize = 2 on them. in __tcp_transmit_skb()
1308 skb->ooo_okay = sk_wmem_alloc_get(sk) < SKB_TRUESIZE(1); in __tcp_transmit_skb()
1315 skb->pfmemalloc = 0; in __tcp_transmit_skb()
1321 skb->sk = sk; in __tcp_transmit_skb()
1322 skb->destructor = skb_is_tcp_pure_ack(skb) ? __sock_wfree : tcp_wfree; in __tcp_transmit_skb()
1324 refcount_add(skb->truesize, &sk->sk_wmem_alloc); in __tcp_transmit_skb()
1326 skb_set_dst_pending_confirm(skb, sk->sk_dst_pending_confirm); in __tcp_transmit_skb()
1329 th = (struct tcphdr *)skb->data; in __tcp_transmit_skb()
1330 th->source = inet->inet_sport; in __tcp_transmit_skb()
1331 th->dest = inet->inet_dport; in __tcp_transmit_skb()
1332 th->seq = htonl(tcb->seq); in __tcp_transmit_skb()
1333 th->ack_seq = htonl(rcv_nxt); in __tcp_transmit_skb()
1335 tcb->tcp_flags); in __tcp_transmit_skb()
1337 th->check = 0; in __tcp_transmit_skb()
1338 th->urg_ptr = 0; in __tcp_transmit_skb()
1341 if (unlikely(tcp_urg_mode(tp) && before(tcb->seq, tp->snd_up))) { in __tcp_transmit_skb()
1342 if (before(tp->snd_up, tcb->seq + 0x10000)) { in __tcp_transmit_skb()
1343 th->urg_ptr = htons(tp->snd_up - tcb->seq); in __tcp_transmit_skb()
1344 th->urg = 1; in __tcp_transmit_skb()
1345 } else if (after(tcb->seq + 0xFFFF, tp->snd_nxt)) { in __tcp_transmit_skb()
1346 th->urg_ptr = htons(0xFFFF); in __tcp_transmit_skb()
1347 th->urg = 1; in __tcp_transmit_skb()
1351 tcp_options_write((__be32 *)(th + 1), tp, &opts); in __tcp_transmit_skb()
1352 skb_shinfo(skb)->gso_type = sk->sk_gso_type; in __tcp_transmit_skb()
1353 if (likely(!(tcb->tcp_flags & TCPHDR_SYN))) { in __tcp_transmit_skb()
1354 th->window = htons(tcp_select_window(sk)); in __tcp_transmit_skb()
1360 th->window = htons(min(tp->rcv_wnd, 65535U)); in __tcp_transmit_skb()
1366 tp->af_specific->calc_md5_hash(opts.hash_location, in __tcp_transmit_skb()
1374 INDIRECT_CALL_INET(icsk->icsk_af_ops->send_check, in __tcp_transmit_skb()
1378 if (likely(tcb->tcp_flags & TCPHDR_ACK)) in __tcp_transmit_skb()
1381 if (skb->len != tcp_header_size) { in __tcp_transmit_skb()
1382 tcp_event_data_sent(tp, sk); in __tcp_transmit_skb()
1383 tp->data_segs_out += tcp_skb_pcount(skb); in __tcp_transmit_skb()
1384 tp->bytes_sent += skb->len - tcp_header_size; in __tcp_transmit_skb()
1387 if (after(tcb->end_seq, tp->snd_nxt) || tcb->seq == tcb->end_seq) in __tcp_transmit_skb()
1391 tp->segs_out += tcp_skb_pcount(skb); in __tcp_transmit_skb()
1392 /* OK, its time to fill skb_shinfo(skb)->gso_{segs|size} */ in __tcp_transmit_skb()
1393 skb_shinfo(skb)->gso_segs = tcp_skb_pcount(skb); in __tcp_transmit_skb()
1394 skb_shinfo(skb)->gso_size = tcp_skb_mss(skb); in __tcp_transmit_skb()
1396 /* Leave earliest departure time in skb->tstamp (skb->skb_mstamp_ns) */ in __tcp_transmit_skb()
1399 memset(skb->cb, 0, max(sizeof(struct inet_skb_parm), in __tcp_transmit_skb()
1402 tcp_add_tx_delay(skb, tp); in __tcp_transmit_skb()
1404 err = INDIRECT_CALL_INET(icsk->icsk_af_ops->queue_xmit, in __tcp_transmit_skb()
1406 sk, skb, &inet->cork.fl); in __tcp_transmit_skb()
1423 tcp_sk(sk)->rcv_nxt); in tcp_transmit_skb()
1433 struct tcp_sock *tp = tcp_sk(sk); in tcp_queue_skb() local
1436 WRITE_ONCE(tp->write_seq, TCP_SKB_CB(skb)->end_seq); in tcp_queue_skb()
1439 sk_wmem_queued_add(sk, skb->truesize); in tcp_queue_skb()
1440 sk_mem_charge(sk, skb->truesize); in tcp_queue_skb()
1446 if (skb->len <= mss_now) { in tcp_set_skb_tso_segs()
1448 * non-TSO case. in tcp_set_skb_tso_segs()
1451 TCP_SKB_CB(skb)->tcp_gso_size = 0; in tcp_set_skb_tso_segs()
1453 tcp_skb_pcount_set(skb, DIV_ROUND_UP(skb->len, mss_now)); in tcp_set_skb_tso_segs()
1454 TCP_SKB_CB(skb)->tcp_gso_size = mss_now; in tcp_set_skb_tso_segs()
1463 struct tcp_sock *tp = tcp_sk(sk); in tcp_adjust_pcount() local
1465 tp->packets_out -= decr; in tcp_adjust_pcount()
1467 if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED) in tcp_adjust_pcount()
1468 tp->sacked_out -= decr; in tcp_adjust_pcount()
1469 if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_RETRANS) in tcp_adjust_pcount()
1470 tp->retrans_out -= decr; in tcp_adjust_pcount()
1471 if (TCP_SKB_CB(skb)->sacked & TCPCB_LOST) in tcp_adjust_pcount()
1472 tp->lost_out -= decr; in tcp_adjust_pcount()
1475 if (tcp_is_reno(tp) && decr > 0) in tcp_adjust_pcount()
1476 tp->sacked_out -= min_t(u32, tp->sacked_out, decr); in tcp_adjust_pcount()
1478 if (tp->lost_skb_hint && in tcp_adjust_pcount()
1479 before(TCP_SKB_CB(skb)->seq, TCP_SKB_CB(tp->lost_skb_hint)->seq) && in tcp_adjust_pcount()
1480 (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED)) in tcp_adjust_pcount()
1481 tp->lost_cnt_hint -= decr; in tcp_adjust_pcount()
1483 tcp_verify_left_out(tp); in tcp_adjust_pcount()
1488 return TCP_SKB_CB(skb)->txstamp_ack || in tcp_has_tx_tstamp()
1489 (skb_shinfo(skb)->tx_flags & SKBTX_ANY_TSTAMP); in tcp_has_tx_tstamp()
1497 !before(shinfo->tskey, TCP_SKB_CB(skb2)->seq)) { in tcp_fragment_tstamp()
1499 u8 tsflags = shinfo->tx_flags & SKBTX_ANY_TSTAMP; in tcp_fragment_tstamp()
1501 shinfo->tx_flags &= ~tsflags; in tcp_fragment_tstamp()
1502 shinfo2->tx_flags |= tsflags; in tcp_fragment_tstamp()
1503 swap(shinfo->tskey, shinfo2->tskey); in tcp_fragment_tstamp()
1504 TCP_SKB_CB(skb2)->txstamp_ack = TCP_SKB_CB(skb)->txstamp_ack; in tcp_fragment_tstamp()
1505 TCP_SKB_CB(skb)->txstamp_ack = 0; in tcp_fragment_tstamp()
1511 TCP_SKB_CB(skb2)->eor = TCP_SKB_CB(skb)->eor; in tcp_skb_fragment_eor()
1512 TCP_SKB_CB(skb)->eor = 0; in tcp_skb_fragment_eor()
1522 __skb_queue_after(&sk->sk_write_queue, skb, buff); in tcp_insert_write_queue_after()
1524 tcp_rbtree_insert(&sk->tcp_rtx_queue, buff); in tcp_insert_write_queue_after()
1536 struct tcp_sock *tp = tcp_sk(sk); in tcp_fragment() local
1543 if (WARN_ON(len > skb->len)) in tcp_fragment()
1544 return -EINVAL; in tcp_fragment()
1546 nsize = skb_headlen(skb) - len; in tcp_fragment()
1555 limit = sk->sk_sndbuf + 2 * SKB_TRUESIZE(GSO_MAX_SIZE); in tcp_fragment()
1556 if (unlikely((sk->sk_wmem_queued >> 1) > limit && in tcp_fragment()
1561 return -ENOMEM; in tcp_fragment()
1565 return -ENOMEM; in tcp_fragment()
1570 return -ENOMEM; /* We'll just try again later. */ in tcp_fragment()
1573 sk_wmem_queued_add(sk, buff->truesize); in tcp_fragment()
1574 sk_mem_charge(sk, buff->truesize); in tcp_fragment()
1575 nlen = skb->len - len - nsize; in tcp_fragment()
1576 buff->truesize += nlen; in tcp_fragment()
1577 skb->truesize -= nlen; in tcp_fragment()
1580 TCP_SKB_CB(buff)->seq = TCP_SKB_CB(skb)->seq + len; in tcp_fragment()
1581 TCP_SKB_CB(buff)->end_seq = TCP_SKB_CB(skb)->end_seq; in tcp_fragment()
1582 TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(buff)->seq; in tcp_fragment()
1585 flags = TCP_SKB_CB(skb)->tcp_flags; in tcp_fragment()
1586 TCP_SKB_CB(skb)->tcp_flags = flags & ~(TCPHDR_FIN | TCPHDR_PSH); in tcp_fragment()
1587 TCP_SKB_CB(buff)->tcp_flags = flags; in tcp_fragment()
1588 TCP_SKB_CB(buff)->sacked = TCP_SKB_CB(skb)->sacked; in tcp_fragment()
1593 buff->ip_summed = CHECKSUM_PARTIAL; in tcp_fragment()
1595 buff->tstamp = skb->tstamp; in tcp_fragment()
1605 TCP_SKB_CB(buff)->tx = TCP_SKB_CB(skb)->tx; in tcp_fragment()
1608 * adjust the various packet counters. in tcp_fragment()
1610 if (!before(tp->snd_nxt, TCP_SKB_CB(buff)->end_seq)) { in tcp_fragment()
1611 int diff = old_factor - tcp_skb_pcount(skb) - in tcp_fragment()
1622 list_add(&buff->tcp_tsorted_anchor, &skb->tcp_tsorted_anchor); in tcp_fragment()
1638 len -= eat; in __pskb_trim_head()
1645 for (i = 0; i < shinfo->nr_frags; i++) { in __pskb_trim_head()
1646 int size = skb_frag_size(&shinfo->frags[i]); in __pskb_trim_head()
1650 eat -= size; in __pskb_trim_head()
1652 shinfo->frags[k] = shinfo->frags[i]; in __pskb_trim_head()
1654 skb_frag_off_add(&shinfo->frags[k], eat); in __pskb_trim_head()
1655 skb_frag_size_sub(&shinfo->frags[k], eat); in __pskb_trim_head()
1661 shinfo->nr_frags = k; in __pskb_trim_head()
1663 skb->data_len -= len; in __pskb_trim_head()
1664 skb->len = skb->data_len; in __pskb_trim_head()
1674 return -ENOMEM; in tcp_trim_head()
1678 TCP_SKB_CB(skb)->seq += len; in tcp_trim_head()
1679 skb->ip_summed = CHECKSUM_PARTIAL; in tcp_trim_head()
1682 skb->truesize -= delta_truesize; in tcp_trim_head()
1683 sk_wmem_queued_add(sk, -delta_truesize); in tcp_trim_head()
1687 /* Any change of skb->len requires recalculation of tso factor. */ in tcp_trim_head()
1697 const struct tcp_sock *tp = tcp_sk(sk); in __tcp_mtu_to_mss() local
1702 It is MMS_S - sizeof(tcphdr) of rfc1122 in __tcp_mtu_to_mss()
1704 mss_now = pmtu - icsk->icsk_af_ops->net_header_len - sizeof(struct tcphdr); in __tcp_mtu_to_mss()
1707 if (icsk->icsk_af_ops->net_frag_header_len) { in __tcp_mtu_to_mss()
1711 mss_now -= icsk->icsk_af_ops->net_frag_header_len; in __tcp_mtu_to_mss()
1715 if (mss_now > tp->rx_opt.mss_clamp) in __tcp_mtu_to_mss()
1716 mss_now = tp->rx_opt.mss_clamp; in __tcp_mtu_to_mss()
1719 mss_now -= icsk->icsk_ext_hdr_len; in __tcp_mtu_to_mss()
1722 mss_now = max(mss_now, sock_net(sk)->ipv4.sysctl_tcp_min_snd_mss); in __tcp_mtu_to_mss()
1730 return __tcp_mtu_to_mss(sk, pmtu) - in tcp_mtu_to_mss()
1731 (tcp_sk(sk)->tcp_header_len - sizeof(struct tcphdr)); in tcp_mtu_to_mss()
1737 const struct tcp_sock *tp = tcp_sk(sk); in tcp_mss_to_mtu() local
1742 tp->tcp_header_len + in tcp_mss_to_mtu()
1743 icsk->icsk_ext_hdr_len + in tcp_mss_to_mtu()
1744 icsk->icsk_af_ops->net_header_len; in tcp_mss_to_mtu()
1747 if (icsk->icsk_af_ops->net_frag_header_len) { in tcp_mss_to_mtu()
1751 mtu += icsk->icsk_af_ops->net_frag_header_len; in tcp_mss_to_mtu()
1760 struct tcp_sock *tp = tcp_sk(sk); in tcp_mtup_init() local
1764 icsk->icsk_mtup.enabled = net->ipv4.sysctl_tcp_mtu_probing > 1; in tcp_mtup_init()
1765 icsk->icsk_mtup.search_high = tp->rx_opt.mss_clamp + sizeof(struct tcphdr) + in tcp_mtup_init()
1766 icsk->icsk_af_ops->net_header_len; in tcp_mtup_init()
1767 icsk->icsk_mtup.search_low = tcp_mss_to_mtu(sk, net->ipv4.sysctl_tcp_base_mss); in tcp_mtup_init()
1768 icsk->icsk_mtup.probe_size = 0; in tcp_mtup_init()
1769 if (icsk->icsk_mtup.enabled) in tcp_mtup_init()
1770 icsk->icsk_mtup.probe_timestamp = tcp_jiffies32; in tcp_mtup_init()
1776 tp->rx_opt.user_mss is mss set by user by TCP_MAXSEG. It does NOT counts
1779 tp->rx_opt.mss_clamp is mss negotiated at connection setup.
1783 inet_csk(sk)->icsk_pmtu_cookie is last pmtu, seen by this function.
1785 tp->mss_cache is current effective sending mss, including
1788 tp->rx_opt.mss_clamp.
1793 NOTE2. inet_csk(sk)->icsk_pmtu_cookie and tp->mss_cache
1794 are READ ONLY outside this function. --ANK (980731)
1798 struct tcp_sock *tp = tcp_sk(sk); in tcp_sync_mss() local
1802 if (icsk->icsk_mtup.search_high > pmtu) in tcp_sync_mss()
1803 icsk->icsk_mtup.search_high = pmtu; in tcp_sync_mss()
1806 mss_now = tcp_bound_to_half_wnd(tp, mss_now); in tcp_sync_mss()
1809 icsk->icsk_pmtu_cookie = pmtu; in tcp_sync_mss()
1810 if (icsk->icsk_mtup.enabled) in tcp_sync_mss()
1811 mss_now = min(mss_now, tcp_mtu_to_mss(sk, icsk->icsk_mtup.search_low)); in tcp_sync_mss()
1812 tp->mss_cache = mss_now; in tcp_sync_mss()
1823 const struct tcp_sock *tp = tcp_sk(sk); in tcp_current_mss() local
1830 mss_now = tp->mss_cache; in tcp_current_mss()
1834 if (mtu != inet_csk(sk)->icsk_pmtu_cookie) in tcp_current_mss()
1840 /* The mss_cache is sized based on tp->tcp_header_len, which assumes in tcp_current_mss()
1843 * we have to adjust mss_now correspondingly */ in tcp_current_mss()
1844 if (header_len != tp->tcp_header_len) { in tcp_current_mss()
1845 int delta = (int) header_len - tp->tcp_header_len; in tcp_current_mss()
1846 mss_now -= delta; in tcp_current_mss()
1852 /* RFC2861, slow part. Adjust cwnd, after it was not full during one rto.
1858 struct tcp_sock *tp = tcp_sk(sk); in tcp_cwnd_application_limited() local
1860 if (inet_csk(sk)->icsk_ca_state == TCP_CA_Open && in tcp_cwnd_application_limited()
1861 sk->sk_socket && !test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) { in tcp_cwnd_application_limited()
1863 u32 init_win = tcp_init_cwnd(tp, __sk_dst_get(sk)); in tcp_cwnd_application_limited()
1864 u32 win_used = max(tp->snd_cwnd_used, init_win); in tcp_cwnd_application_limited()
1865 if (win_used < tp->snd_cwnd) { in tcp_cwnd_application_limited()
1866 tp->snd_ssthresh = tcp_current_ssthresh(sk); in tcp_cwnd_application_limited()
1867 tp->snd_cwnd = (tp->snd_cwnd + win_used) >> 1; in tcp_cwnd_application_limited()
1869 tp->snd_cwnd_used = 0; in tcp_cwnd_application_limited()
1871 tp->snd_cwnd_stamp = tcp_jiffies32; in tcp_cwnd_application_limited()
1876 const struct tcp_congestion_ops *ca_ops = inet_csk(sk)->icsk_ca_ops; in tcp_cwnd_validate()
1877 struct tcp_sock *tp = tcp_sk(sk); in tcp_cwnd_validate() local
1880 * window, and remember whether we were cwnd-limited then. in tcp_cwnd_validate()
1882 if (!before(tp->snd_una, tp->max_packets_seq) || in tcp_cwnd_validate()
1883 tp->packets_out > tp->max_packets_out || in tcp_cwnd_validate()
1885 tp->max_packets_out = tp->packets_out; in tcp_cwnd_validate()
1886 tp->max_packets_seq = tp->snd_nxt; in tcp_cwnd_validate()
1887 tp->is_cwnd_limited = is_cwnd_limited; in tcp_cwnd_validate()
1892 tp->snd_cwnd_used = 0; in tcp_cwnd_validate()
1893 tp->snd_cwnd_stamp = tcp_jiffies32; in tcp_cwnd_validate()
1896 if (tp->packets_out > tp->snd_cwnd_used) in tcp_cwnd_validate()
1897 tp->snd_cwnd_used = tp->packets_out; in tcp_cwnd_validate()
1899 if (sock_net(sk)->ipv4.sysctl_tcp_slow_start_after_idle && in tcp_cwnd_validate()
1900 (s32)(tcp_jiffies32 - tp->snd_cwnd_stamp) >= inet_csk(sk)->icsk_rto && in tcp_cwnd_validate()
1901 !ca_ops->cong_control) in tcp_cwnd_validate()
1911 if (tcp_write_queue_empty(sk) && sk->sk_socket && in tcp_cwnd_validate()
1912 test_bit(SOCK_NOSPACE, &sk->sk_socket->flags) && in tcp_cwnd_validate()
1913 (1 << sk->sk_state) & (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) in tcp_cwnd_validate()
1919 static bool tcp_minshall_check(const struct tcp_sock *tp) in tcp_minshall_check() argument
1921 return after(tp->snd_sml, tp->snd_una) && in tcp_minshall_check()
1922 !after(tp->snd_sml, tp->snd_nxt); in tcp_minshall_check()
1926 * Note that a TSO packet might end with a sub-mss segment
1928 * if ((skb->len % mss) != 0)
1929 * tp->snd_sml = TCP_SKB_CB(skb)->end_seq;
1931 * skb_pcount = skb->len / mss_now
1933 static void tcp_minshall_update(struct tcp_sock *tp, unsigned int mss_now, in tcp_minshall_update() argument
1936 if (skb->len < tcp_skb_pcount(skb) * mss_now) in tcp_minshall_update()
1937 tp->snd_sml = TCP_SKB_CB(skb)->end_seq; in tcp_minshall_update()
1947 static bool tcp_nagle_check(bool partial, const struct tcp_sock *tp, in tcp_nagle_check() argument
1952 (!nonagle && tp->packets_out && tcp_minshall_check(tp))); in tcp_nagle_check()
1964 sk->sk_pacing_rate >> READ_ONCE(sk->sk_pacing_shift), in tcp_tso_autosize()
1965 sk->sk_gso_max_size - 1 - MAX_TCP_HEADER); in tcp_tso_autosize()
1982 const struct tcp_congestion_ops *ca_ops = inet_csk(sk)->icsk_ca_ops; in tcp_tso_segs()
1985 min_tso = ca_ops->min_tso_segs ? in tcp_tso_segs()
1986 ca_ops->min_tso_segs(sk) : in tcp_tso_segs()
1987 sock_net(sk)->ipv4.sysctl_tcp_min_tso_segs; in tcp_tso_segs()
1990 return min_t(u32, tso_segs, sk->sk_gso_max_segs); in tcp_tso_segs()
2000 const struct tcp_sock *tp = tcp_sk(sk); in tcp_mss_split_point() local
2003 window = tcp_wnd_end(tp) - TCP_SKB_CB(skb)->seq; in tcp_mss_split_point()
2009 needed = min(skb->len, window); in tcp_mss_split_point()
2019 if (tcp_nagle_check(partial != 0, tp, nonagle)) in tcp_mss_split_point()
2020 return needed - partial; in tcp_mss_split_point()
2028 static inline unsigned int tcp_cwnd_test(const struct tcp_sock *tp, in tcp_cwnd_test() argument
2034 if ((TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN) && in tcp_cwnd_test()
2038 in_flight = tcp_packets_in_flight(tp); in tcp_cwnd_test()
2039 cwnd = tp->snd_cwnd; in tcp_cwnd_test()
2047 return min(halfcwnd, cwnd - in_flight); in tcp_cwnd_test()
2069 static inline bool tcp_nagle_test(const struct tcp_sock *tp, const struct sk_buff *skb, in tcp_nagle_test() argument
2082 if (tcp_urg_mode(tp) || (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)) in tcp_nagle_test()
2085 if (!tcp_nagle_check(skb->len < cur_mss, tp, nonagle)) in tcp_nagle_test()
2092 static bool tcp_snd_wnd_test(const struct tcp_sock *tp, in tcp_snd_wnd_test() argument
2096 u32 end_seq = TCP_SKB_CB(skb)->end_seq; in tcp_snd_wnd_test()
2098 if (skb->len > cur_mss) in tcp_snd_wnd_test()
2099 end_seq = TCP_SKB_CB(skb)->seq + cur_mss; in tcp_snd_wnd_test()
2101 return !after(end_seq, tcp_wnd_end(tp)); in tcp_snd_wnd_test()
2108 * know that all the data is in scatter-gather pages, and that the
2114 int nlen = skb->len - len; in tso_fragment()
2119 if (skb->len != skb->data_len) in tso_fragment()
2125 return -ENOMEM; in tso_fragment()
2128 sk_wmem_queued_add(sk, buff->truesize); in tso_fragment()
2129 sk_mem_charge(sk, buff->truesize); in tso_fragment()
2130 buff->truesize += nlen; in tso_fragment()
2131 skb->truesize -= nlen; in tso_fragment()
2134 TCP_SKB_CB(buff)->seq = TCP_SKB_CB(skb)->seq + len; in tso_fragment()
2135 TCP_SKB_CB(buff)->end_seq = TCP_SKB_CB(skb)->end_seq; in tso_fragment()
2136 TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(buff)->seq; in tso_fragment()
2139 flags = TCP_SKB_CB(skb)->tcp_flags; in tso_fragment()
2140 TCP_SKB_CB(skb)->tcp_flags = flags & ~(TCPHDR_FIN | TCPHDR_PSH); in tso_fragment()
2141 TCP_SKB_CB(buff)->tcp_flags = flags; in tso_fragment()
2144 TCP_SKB_CB(buff)->sacked = 0; in tso_fragment()
2148 buff->ip_summed = CHECKSUM_PARTIAL; in tso_fragment()
2175 struct tcp_sock *tp = tcp_sk(sk); in tcp_tso_should_defer() local
2180 if (icsk->icsk_ca_state >= TCP_CA_Recovery) in tcp_tso_should_defer()
2185 * Note that tp->tcp_wstamp_ns can be in the future if we have in tcp_tso_should_defer()
2188 delta = tp->tcp_clock_cache - tp->tcp_wstamp_ns - NSEC_PER_MSEC; in tcp_tso_should_defer()
2192 in_flight = tcp_packets_in_flight(tp); in tcp_tso_should_defer()
2195 BUG_ON(tp->snd_cwnd <= in_flight); in tcp_tso_should_defer()
2197 send_win = tcp_wnd_end(tp) - TCP_SKB_CB(skb)->seq; in tcp_tso_should_defer()
2200 cong_win = (tp->snd_cwnd - in_flight) * tp->mss_cache; in tcp_tso_should_defer()
2204 /* If a full-sized TSO skb can be sent, do it. */ in tcp_tso_should_defer()
2205 if (limit >= max_segs * tp->mss_cache) in tcp_tso_should_defer()
2209 if ((skb != tcp_write_queue_tail(sk)) && (limit >= skb->len)) in tcp_tso_should_defer()
2212 win_divisor = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_tso_win_divisor); in tcp_tso_should_defer()
2214 u32 chunk = min(tp->snd_wnd, tp->snd_cwnd * tp->mss_cache); in tcp_tso_should_defer()
2228 if (limit > tcp_max_tso_deferred_mss(tp) * tp->mss_cache) in tcp_tso_should_defer()
2236 delta = tp->tcp_clock_cache - head->tstamp; in tcp_tso_should_defer()
2238 if ((s64)(delta - (u64)NSEC_PER_USEC * (tp->srtt_us >> 4)) < 0) in tcp_tso_should_defer()
2243 * 1) We are cwnd-limited in tcp_tso_should_defer()
2244 * 2) We are rwnd-limited in tcp_tso_should_defer()
2248 if (cong_win <= skb->len) { in tcp_tso_should_defer()
2253 if (send_win <= skb->len) { in tcp_tso_should_defer()
2260 if ((TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN) || in tcp_tso_should_defer()
2261 TCP_SKB_CB(skb)->eor) in tcp_tso_should_defer()
2273 struct tcp_sock *tp = tcp_sk(sk); in tcp_mtu_check_reprobe() local
2278 interval = net->ipv4.sysctl_tcp_probe_interval; in tcp_mtu_check_reprobe()
2279 delta = tcp_jiffies32 - icsk->icsk_mtup.probe_timestamp; in tcp_mtu_check_reprobe()
2284 icsk->icsk_mtup.probe_size = 0; in tcp_mtu_check_reprobe()
2285 icsk->icsk_mtup.search_high = tp->rx_opt.mss_clamp + in tcp_mtu_check_reprobe()
2287 icsk->icsk_af_ops->net_header_len; in tcp_mtu_check_reprobe()
2288 icsk->icsk_mtup.search_low = tcp_mss_to_mtu(sk, mss); in tcp_mtu_check_reprobe()
2291 icsk->icsk_mtup.probe_timestamp = tcp_jiffies32; in tcp_mtu_check_reprobe()
2301 if (len <= skb->len) in tcp_can_coalesce_send_queue_head()
2304 if (unlikely(TCP_SKB_CB(skb)->eor) || tcp_has_tx_tstamp(skb)) in tcp_can_coalesce_send_queue_head()
2307 len -= skb->len; in tcp_can_coalesce_send_queue_head()
2320 * -1 otherwise
2325 struct tcp_sock *tp = tcp_sk(sk); in tcp_mtu_probe() local
2339 if (likely(!icsk->icsk_mtup.enabled || in tcp_mtu_probe()
2340 icsk->icsk_mtup.probe_size || in tcp_mtu_probe()
2341 inet_csk(sk)->icsk_ca_state != TCP_CA_Open || in tcp_mtu_probe()
2342 tp->snd_cwnd < 11 || in tcp_mtu_probe()
2343 tp->rx_opt.num_sacks || tp->rx_opt.dsack)) in tcp_mtu_probe()
2344 return -1; in tcp_mtu_probe()
2347 * and current mss_clamp. if (search_high - search_low) in tcp_mtu_probe()
2351 probe_size = tcp_mtu_to_mss(sk, (icsk->icsk_mtup.search_high + in tcp_mtu_probe()
2352 icsk->icsk_mtup.search_low) >> 1); in tcp_mtu_probe()
2353 size_needed = probe_size + (tp->reordering + 1) * tp->mss_cache; in tcp_mtu_probe()
2354 interval = icsk->icsk_mtup.search_high - icsk->icsk_mtup.search_low; in tcp_mtu_probe()
2359 if (probe_size > tcp_mtu_to_mss(sk, icsk->icsk_mtup.search_high) || in tcp_mtu_probe()
2360 interval < net->ipv4.sysctl_tcp_probe_threshold) { in tcp_mtu_probe()
2365 return -1; in tcp_mtu_probe()
2369 if (tp->write_seq - tp->snd_nxt < size_needed) in tcp_mtu_probe()
2370 return -1; in tcp_mtu_probe()
2372 if (tp->snd_wnd < size_needed) in tcp_mtu_probe()
2373 return -1; in tcp_mtu_probe()
2374 if (after(tp->snd_nxt + size_needed, tcp_wnd_end(tp))) in tcp_mtu_probe()
2378 if (tcp_packets_in_flight(tp) + 2 > tp->snd_cwnd) { in tcp_mtu_probe()
2379 if (!tcp_packets_in_flight(tp)) in tcp_mtu_probe()
2380 return -1; in tcp_mtu_probe()
2386 return -1; in tcp_mtu_probe()
2391 return -1; in tcp_mtu_probe()
2392 sk_wmem_queued_add(sk, nskb->truesize); in tcp_mtu_probe()
2393 sk_mem_charge(sk, nskb->truesize); in tcp_mtu_probe()
2398 TCP_SKB_CB(nskb)->seq = TCP_SKB_CB(skb)->seq; in tcp_mtu_probe()
2399 TCP_SKB_CB(nskb)->end_seq = TCP_SKB_CB(skb)->seq + probe_size; in tcp_mtu_probe()
2400 TCP_SKB_CB(nskb)->tcp_flags = TCPHDR_ACK; in tcp_mtu_probe()
2401 TCP_SKB_CB(nskb)->sacked = 0; in tcp_mtu_probe()
2402 nskb->csum = 0; in tcp_mtu_probe()
2403 nskb->ip_summed = CHECKSUM_PARTIAL; in tcp_mtu_probe()
2410 copy = min_t(int, skb->len, probe_size - len); in tcp_mtu_probe()
2413 if (skb->len <= copy) { in tcp_mtu_probe()
2416 TCP_SKB_CB(nskb)->tcp_flags |= TCP_SKB_CB(skb)->tcp_flags; in tcp_mtu_probe()
2420 TCP_SKB_CB(nskb)->eor = TCP_SKB_CB(skb)->eor; in tcp_mtu_probe()
2425 TCP_SKB_CB(nskb)->tcp_flags |= TCP_SKB_CB(skb)->tcp_flags & in tcp_mtu_probe()
2427 if (!skb_shinfo(skb)->nr_frags) { in tcp_mtu_probe()
2433 TCP_SKB_CB(skb)->seq += copy; in tcp_mtu_probe()
2441 tcp_init_tso_segs(nskb, nskb->len); in tcp_mtu_probe()
2444 * be resegmented into mss-sized pieces by tcp_write_xmit(). in tcp_mtu_probe()
2449 tp->snd_cwnd--; in tcp_mtu_probe()
2452 icsk->icsk_mtup.probe_size = tcp_mss_to_mtu(sk, nskb->len); in tcp_mtu_probe()
2453 tp->mtu_probe.probe_seq_start = TCP_SKB_CB(nskb)->seq; in tcp_mtu_probe()
2454 tp->mtu_probe.probe_seq_end = TCP_SKB_CB(nskb)->end_seq; in tcp_mtu_probe()
2459 return -1; in tcp_mtu_probe()
2464 struct tcp_sock *tp = tcp_sk(sk); in tcp_pacing_check() local
2469 if (tp->tcp_wstamp_ns <= tp->tcp_clock_cache) in tcp_pacing_check()
2472 if (!hrtimer_is_queued(&tp->pacing_timer)) { in tcp_pacing_check()
2473 hrtimer_start(&tp->pacing_timer, in tcp_pacing_check()
2474 ns_to_ktime(tp->tcp_wstamp_ns), in tcp_pacing_check()
2485 * - better RTT estimation and ACK scheduling
2486 * - faster recovery
2487 * - high rates
2498 2 * skb->truesize, in tcp_small_queue_check()
2499 sk->sk_pacing_rate >> READ_ONCE(sk->sk_pacing_shift)); in tcp_small_queue_check()
2500 if (sk->sk_pacing_status == SK_PACING_NONE) in tcp_small_queue_check()
2502 sock_net(sk)->ipv4.sysctl_tcp_limit_output_bytes); in tcp_small_queue_check()
2506 tcp_sk(sk)->tcp_tx_delay) { in tcp_small_queue_check()
2507 u64 extra_bytes = (u64)sk->sk_pacing_rate * tcp_sk(sk)->tcp_tx_delay; in tcp_small_queue_check()
2510 * approximate our needs assuming an ~100% skb->truesize overhead. in tcp_small_queue_check()
2514 extra_bytes >>= (20 - 1); in tcp_small_queue_check()
2517 if (refcount_read(&sk->sk_wmem_alloc) > limit) { in tcp_small_queue_check()
2526 set_bit(TSQ_THROTTLED, &sk->sk_tsq_flags); in tcp_small_queue_check()
2532 if (refcount_read(&sk->sk_wmem_alloc) > limit) in tcp_small_queue_check()
2538 static void tcp_chrono_set(struct tcp_sock *tp, const enum tcp_chrono new) in tcp_chrono_set() argument
2541 enum tcp_chrono old = tp->chrono_type; in tcp_chrono_set()
2544 tp->chrono_stat[old - 1] += now - tp->chrono_start; in tcp_chrono_set()
2545 tp->chrono_start = now; in tcp_chrono_set()
2546 tp->chrono_type = new; in tcp_chrono_set()
2551 struct tcp_sock *tp = tcp_sk(sk); in tcp_chrono_start() local
2558 if (type > tp->chrono_type) in tcp_chrono_start()
2559 tcp_chrono_set(tp, type); in tcp_chrono_start()
2564 struct tcp_sock *tp = tcp_sk(sk); in tcp_chrono_stop() local
2575 tcp_chrono_set(tp, TCP_CHRONO_UNSPEC); in tcp_chrono_stop()
2576 else if (type == tp->chrono_type) in tcp_chrono_stop()
2577 tcp_chrono_set(tp, TCP_CHRONO_BUSY); in tcp_chrono_stop()
2585 * snd_up-64k-mss .. snd_up cannot be large. However, taking into
2597 struct tcp_sock *tp = tcp_sk(sk); in tcp_write_xmit() local
2607 tcp_mstamp_refresh(tp); in tcp_write_xmit()
2622 if (unlikely(tp->repair) && tp->repair_queue == TCP_SEND_QUEUE) { in tcp_write_xmit()
2624 skb->skb_mstamp_ns = tp->tcp_wstamp_ns = tp->tcp_clock_cache; in tcp_write_xmit()
2625 list_move_tail(&skb->tcp_tsorted_anchor, &tp->tsorted_sent_queue); in tcp_write_xmit()
2636 cwnd_quota = tcp_cwnd_test(tp, skb); in tcp_write_xmit()
2645 if (unlikely(!tcp_snd_wnd_test(tp, skb, mss_now))) { in tcp_write_xmit()
2651 if (unlikely(!tcp_nagle_test(tp, skb, mss_now, in tcp_write_xmit()
2663 if (tso_segs > 1 && !tcp_urg_mode(tp)) in tcp_write_xmit()
2670 if (skb->len > limit && in tcp_write_xmit()
2679 * We do not want to send a pure-ack packet and have in tcp_write_xmit()
2682 if (TCP_SKB_CB(skb)->end_seq == TCP_SKB_CB(skb)->seq) in tcp_write_xmit()
2694 tcp_minshall_update(tp, mss_now, skb); in tcp_write_xmit()
2706 is_cwnd_limited |= (tcp_packets_in_flight(tp) >= tp->snd_cwnd); in tcp_write_xmit()
2712 tp->prr_out += sent_pkts; in tcp_write_xmit()
2719 return !tp->packets_out && !tcp_write_queue_empty(sk); in tcp_write_xmit()
2725 struct tcp_sock *tp = tcp_sk(sk); in tcp_schedule_loss_probe() local
2732 if (rcu_access_pointer(tp->fastopen_rsk)) in tcp_schedule_loss_probe()
2735 early_retrans = sock_net(sk)->ipv4.sysctl_tcp_early_retrans; in tcp_schedule_loss_probe()
2740 !tp->packets_out || !tcp_is_sack(tp) || in tcp_schedule_loss_probe()
2741 (icsk->icsk_ca_state != TCP_CA_Open && in tcp_schedule_loss_probe()
2742 icsk->icsk_ca_state != TCP_CA_CWR)) in tcp_schedule_loss_probe()
2749 if (tp->srtt_us) { in tcp_schedule_loss_probe()
2750 timeout = usecs_to_jiffies(tp->srtt_us >> 2); in tcp_schedule_loss_probe()
2751 if (tp->packets_out == 1) in tcp_schedule_loss_probe()
2761 jiffies_to_usecs(inet_csk(sk)->icsk_rto) : in tcp_schedule_loss_probe()
2790 struct tcp_sock *tp = tcp_sk(sk); in tcp_send_loss_probe() local
2796 if (tp->tlp_high_seq) in tcp_send_loss_probe()
2799 tp->tlp_retrans = 0; in tcp_send_loss_probe()
2801 if (skb && tcp_snd_wnd_test(tp, skb, mss)) { in tcp_send_loss_probe()
2802 pcount = tp->packets_out; in tcp_send_loss_probe()
2804 if (tp->packets_out > pcount) in tcp_send_loss_probe()
2808 skb = skb_rb_last(&sk->tcp_rtx_queue); in tcp_send_loss_probe()
2810 WARN_ONCE(tp->packets_out, in tcp_send_loss_probe()
2812 tp->packets_out, sk->sk_state, tp->snd_cwnd, mss); in tcp_send_loss_probe()
2813 inet_csk(sk)->icsk_pending = 0; in tcp_send_loss_probe()
2824 if ((pcount > 1) && (skb->len > (pcount - 1) * mss)) { in tcp_send_loss_probe()
2826 (pcount - 1) * mss, mss, in tcp_send_loss_probe()
2838 tp->tlp_retrans = 1; in tcp_send_loss_probe()
2842 tp->tlp_high_seq = tp->snd_nxt; in tcp_send_loss_probe()
2846 inet_csk(sk)->icsk_pending = 0; in tcp_send_loss_probe()
2862 if (unlikely(sk->sk_state == TCP_CLOSE)) in __tcp_push_pending_frames()
2877 BUG_ON(!skb || skb->len < mss_now); in tcp_push_one()
2879 tcp_write_xmit(sk, mss_now, TCP_NAGLE_PUSH, 1, sk->sk_allocation); in tcp_push_one()
2891 * RCV.BUFF - RCV.USER - RCV.WINDOW >= min(1/2 RCV.BUFF, MSS)"
2897 * since header prediction assumes th->window stays fixed.
2899 * Strictly speaking, keeping th->window fixed violates the receiver
2931 * Note, we don't "adjust" for TIMESTAMP or SACK option bytes.
2937 struct tcp_sock *tp = tcp_sk(sk); in __tcp_select_window() local
2942 * fluctuations. --SAW 1998/11/1 in __tcp_select_window()
2944 int mss = icsk->icsk_ack.rcv_mss; in __tcp_select_window()
2952 full_space = min_t(int, tp->window_clamp, allowed_space); in __tcp_select_window()
2960 icsk->icsk_ack.quick = 0; in __tcp_select_window()
2963 tp->rcv_ssthresh = min(tp->rcv_ssthresh, in __tcp_select_window()
2964 4U * tp->advmss); in __tcp_select_window()
2969 free_space = round_down(free_space, 1 << tp->rx_opt.rcv_wscale); in __tcp_select_window()
2972 * of the maximum allowed, try to move to zero-window, else in __tcp_select_window()
2982 if (free_space > tp->rcv_ssthresh) in __tcp_select_window()
2983 free_space = tp->rcv_ssthresh; in __tcp_select_window()
2988 if (tp->rx_opt.rcv_wscale) { in __tcp_select_window()
2995 window = ALIGN(window, (1 << tp->rx_opt.rcv_wscale)); in __tcp_select_window()
2997 window = tp->rcv_wnd; in __tcp_select_window()
3006 if (window <= free_space - mss || window > free_space) in __tcp_select_window()
3024 shinfo->tx_flags |= next_shinfo->tx_flags & SKBTX_ANY_TSTAMP; in tcp_skb_collapse_tstamp()
3025 shinfo->tskey = next_shinfo->tskey; in tcp_skb_collapse_tstamp()
3026 TCP_SKB_CB(skb)->txstamp_ack |= in tcp_skb_collapse_tstamp()
3027 TCP_SKB_CB(next_skb)->txstamp_ack; in tcp_skb_collapse_tstamp()
3034 struct tcp_sock *tp = tcp_sk(sk); in tcp_collapse_retrans() local
3038 next_skb_size = next_skb->len; in tcp_collapse_retrans()
3052 TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(next_skb)->end_seq; in tcp_collapse_retrans()
3055 TCP_SKB_CB(skb)->tcp_flags |= TCP_SKB_CB(next_skb)->tcp_flags; in tcp_collapse_retrans()
3060 TCP_SKB_CB(skb)->sacked |= TCP_SKB_CB(next_skb)->sacked & TCPCB_EVER_RETRANS; in tcp_collapse_retrans()
3061 TCP_SKB_CB(skb)->eor = TCP_SKB_CB(next_skb)->eor; in tcp_collapse_retrans()
3064 tcp_clear_retrans_hints_partial(tp); in tcp_collapse_retrans()
3065 if (next_skb == tp->retransmit_skb_hint) in tcp_collapse_retrans()
3066 tp->retransmit_skb_hint = skb; in tcp_collapse_retrans()
3084 if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED) in tcp_can_collapse()
3096 struct tcp_sock *tp = tcp_sk(sk); in tcp_retrans_try_collapse() local
3100 if (!sock_net(sk)->ipv4.sysctl_tcp_retrans_collapse) in tcp_retrans_try_collapse()
3102 if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN) in tcp_retrans_try_collapse()
3112 space -= skb->len; in tcp_retrans_try_collapse()
3122 if (after(TCP_SKB_CB(skb)->end_seq, tcp_wnd_end(tp))) in tcp_retrans_try_collapse()
3131 * state updates are done by the caller. Returns non-zero if an
3137 struct tcp_sock *tp = tcp_sk(sk); in __tcp_retransmit_skb() local
3143 if (icsk->icsk_mtup.probe_size) in __tcp_retransmit_skb()
3144 icsk->icsk_mtup.probe_size = 0; in __tcp_retransmit_skb()
3149 if (refcount_read(&sk->sk_wmem_alloc) > in __tcp_retransmit_skb()
3150 min_t(u32, sk->sk_wmem_queued + (sk->sk_wmem_queued >> 2), in __tcp_retransmit_skb()
3151 sk->sk_sndbuf)) in __tcp_retransmit_skb()
3152 return -EAGAIN; in __tcp_retransmit_skb()
3155 return -EBUSY; in __tcp_retransmit_skb()
3157 if (before(TCP_SKB_CB(skb)->seq, tp->snd_una)) { in __tcp_retransmit_skb()
3158 if (unlikely(before(TCP_SKB_CB(skb)->end_seq, tp->snd_una))) { in __tcp_retransmit_skb()
3160 return -EINVAL; in __tcp_retransmit_skb()
3162 if (tcp_trim_head(sk, skb, tp->snd_una - TCP_SKB_CB(skb)->seq)) in __tcp_retransmit_skb()
3163 return -ENOMEM; in __tcp_retransmit_skb()
3166 if (inet_csk(sk)->icsk_af_ops->rebuild_header(sk)) in __tcp_retransmit_skb()
3167 return -EHOSTUNREACH; /* Routing failure or similar. */ in __tcp_retransmit_skb()
3176 if (!before(TCP_SKB_CB(skb)->seq, tcp_wnd_end(tp)) && in __tcp_retransmit_skb()
3177 TCP_SKB_CB(skb)->seq != tp->snd_una) in __tcp_retransmit_skb()
3178 return -EAGAIN; in __tcp_retransmit_skb()
3181 if (skb->len > len) { in __tcp_retransmit_skb()
3184 return -ENOMEM; /* We'll try again later. */ in __tcp_retransmit_skb()
3187 return -ENOMEM; in __tcp_retransmit_skb()
3191 diff -= tcp_skb_pcount(skb); in __tcp_retransmit_skb()
3194 if (skb->len < cur_mss) in __tcp_retransmit_skb()
3199 if ((TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN_ECN) == TCPHDR_SYN_ECN) in __tcp_retransmit_skb()
3205 if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN) in __tcp_retransmit_skb()
3207 tp->total_retrans += segs; in __tcp_retransmit_skb()
3208 tp->bytes_retrans += skb->len; in __tcp_retransmit_skb()
3210 /* make sure skb->data is aligned on arches that require it in __tcp_retransmit_skb()
3211 * and check if ack-trimming & collapsing extended the headroom in __tcp_retransmit_skb()
3214 if (unlikely((NET_IP_ALIGN && ((unsigned long)skb->data & 3)) || in __tcp_retransmit_skb()
3221 nskb->dev = NULL; in __tcp_retransmit_skb()
3224 err = -ENOBUFS; in __tcp_retransmit_skb()
3229 tcp_update_skb_after_send(sk, skb, tp->tcp_wstamp_ns); in __tcp_retransmit_skb()
3239 TCP_SKB_CB(skb)->sacked |= TCPCB_EVER_RETRANS; in __tcp_retransmit_skb()
3241 if (BPF_SOCK_OPS_TEST_FLAG(tp, BPF_SOCK_OPS_RETRANS_CB_FLAG)) in __tcp_retransmit_skb()
3243 TCP_SKB_CB(skb)->seq, segs, err); in __tcp_retransmit_skb()
3247 } else if (err != -EBUSY) { in __tcp_retransmit_skb()
3255 struct tcp_sock *tp = tcp_sk(sk); in tcp_retransmit_skb() local
3260 if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_RETRANS) { in tcp_retransmit_skb()
3264 TCP_SKB_CB(skb)->sacked |= TCPCB_RETRANS; in tcp_retransmit_skb()
3265 tp->retrans_out += tcp_skb_pcount(skb); in tcp_retransmit_skb()
3269 if (!tp->retrans_stamp) in tcp_retransmit_skb()
3270 tp->retrans_stamp = tcp_skb_timestamp(skb); in tcp_retransmit_skb()
3272 if (tp->undo_retrans < 0) in tcp_retransmit_skb()
3273 tp->undo_retrans = 0; in tcp_retransmit_skb()
3274 tp->undo_retrans += tcp_skb_pcount(skb); in tcp_retransmit_skb()
3287 struct tcp_sock *tp = tcp_sk(sk); in tcp_xmit_retransmit_queue() local
3292 if (!tp->packets_out) in tcp_xmit_retransmit_queue()
3296 skb = tp->retransmit_skb_hint ?: rtx_head; in tcp_xmit_retransmit_queue()
3307 tp->retransmit_skb_hint = skb; in tcp_xmit_retransmit_queue()
3309 segs = tp->snd_cwnd - tcp_packets_in_flight(tp); in tcp_xmit_retransmit_queue()
3312 sacked = TCP_SKB_CB(skb)->sacked; in tcp_xmit_retransmit_queue()
3318 if (tp->retrans_out >= tp->lost_out) { in tcp_xmit_retransmit_queue()
3326 if (icsk->icsk_ca_state != TCP_CA_Loss) in tcp_xmit_retransmit_queue()
3344 tp->prr_out += tcp_skb_pcount(skb); in tcp_xmit_retransmit_queue()
3347 icsk->icsk_pending != ICSK_TIME_REO_TIMEOUT) in tcp_xmit_retransmit_queue()
3353 inet_csk(sk)->icsk_rto, in tcp_xmit_retransmit_queue()
3368 if (size <= sk->sk_forward_alloc) in sk_forced_mem_schedule()
3371 sk->sk_forward_alloc += amt * SK_MEM_QUANTUM; in sk_forced_mem_schedule()
3374 if (mem_cgroup_sockets_enabled && sk->sk_memcg) in sk_forced_mem_schedule()
3375 mem_cgroup_charge_skmem(sk->sk_memcg, amt); in sk_forced_mem_schedule()
3384 struct tcp_sock *tp = tcp_sk(sk); in tcp_send_fin() local
3393 tskb = skb_rb_last(&sk->tcp_rtx_queue); in tcp_send_fin()
3396 TCP_SKB_CB(tskb)->tcp_flags |= TCPHDR_FIN; in tcp_send_fin()
3397 TCP_SKB_CB(tskb)->end_seq++; in tcp_send_fin()
3398 tp->write_seq++; in tcp_send_fin()
3402 * We need to set tp->snd_nxt to the value it would have in tcp_send_fin()
3404 * does not change tp->snd_nxt. in tcp_send_fin()
3406 WRITE_ONCE(tp->snd_nxt, tp->snd_nxt + 1); in tcp_send_fin()
3410 skb = alloc_skb_fclone(MAX_TCP_HEADER, sk->sk_allocation); in tcp_send_fin()
3414 INIT_LIST_HEAD(&skb->tcp_tsorted_anchor); in tcp_send_fin()
3416 sk_forced_mem_schedule(sk, skb->truesize); in tcp_send_fin()
3418 tcp_init_nondata_skb(skb, tp->write_seq, in tcp_send_fin()
3428 * by RFC 2525, section 2.17. -DaveM
3458 /* Send a crossed SYN-ACK during socket establishment.
3469 if (!skb || !(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN)) { in tcp_send_synack()
3471 return -EFAULT; in tcp_send_synack()
3473 if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_ACK)) { in tcp_send_synack()
3481 return -ENOMEM; in tcp_send_synack()
3482 INIT_LIST_HEAD(&nskb->tcp_tsorted_anchor); in tcp_send_synack()
3486 tcp_rbtree_insert(&sk->tcp_rtx_queue, nskb); in tcp_send_synack()
3487 sk_wmem_queued_add(sk, nskb->truesize); in tcp_send_synack()
3488 sk_mem_charge(sk, nskb->truesize); in tcp_send_synack()
3492 TCP_SKB_CB(skb)->tcp_flags |= TCPHDR_ACK; in tcp_send_synack()
3499 * tcp_make_synack - Allocate one skb and build a SYNACK packet.
3515 const struct tcp_sock *tp = tcp_sk(sk); in tcp_make_synack() local
3544 * sk->sk_wmem_alloc in an atomic, we can promote to rw. in tcp_make_synack()
3551 mss = tcp_mss_clamp(tp, dst_metric_advmss(dst)); in tcp_make_synack()
3556 if (unlikely(synack_type == TCP_SYNACK_COOKIE && ireq->tstamp_ok)) in tcp_make_synack()
3557 skb->skb_mstamp_ns = cookie_init_timestamp(req, now); in tcp_make_synack()
3561 skb->skb_mstamp_ns = now; in tcp_make_synack()
3562 if (!tcp_rsk(req)->snt_synack) /* Timestamp first SYNACK */ in tcp_make_synack()
3563 tcp_rsk(req)->snt_synack = tcp_skb_timestamp_us(skb); in tcp_make_synack()
3568 md5 = tcp_rsk(req)->af_specific->req_md5_lookup(sk, req_to_sk(req)); in tcp_make_synack()
3570 skb_set_hash(skb, tcp_rsk(req)->txhash, PKT_HASH_TYPE_L4); in tcp_make_synack()
3572 TCP_SKB_CB(skb)->tcp_flags = TCPHDR_SYN | TCPHDR_ACK; in tcp_make_synack()
3580 th = (struct tcphdr *)skb->data; in tcp_make_synack()
3582 th->syn = 1; in tcp_make_synack()
3583 th->ack = 1; in tcp_make_synack()
3585 th->source = htons(ireq->ir_num); in tcp_make_synack()
3586 th->dest = ireq->ir_rmt_port; in tcp_make_synack()
3587 skb->mark = ireq->ir_mark; in tcp_make_synack()
3588 skb->ip_summed = CHECKSUM_PARTIAL; in tcp_make_synack()
3589 th->seq = htonl(tcp_rsk(req)->snt_isn); in tcp_make_synack()
3591 th->ack_seq = htonl(tcp_rsk(req)->rcv_nxt); in tcp_make_synack()
3594 th->window = htons(min(req->rsk_rcv_wnd, 65535U)); in tcp_make_synack()
3596 th->doff = (tcp_header_size >> 2); in tcp_make_synack()
3600 /* Okay, we have all we need - do the md5 hash if needed */ in tcp_make_synack()
3602 tcp_rsk(req)->af_specific->calc_md5_hash(opts.hash_location, in tcp_make_synack()
3610 skb->skb_mstamp_ns = now; in tcp_make_synack()
3611 tcp_add_tx_delay(skb, tp); in tcp_make_synack()
3628 if (likely(ca && bpf_try_module_get(ca, ca->owner))) { in tcp_ca_dst_init()
3629 bpf_module_put(icsk->icsk_ca_ops, icsk->icsk_ca_ops->owner); in tcp_ca_dst_init()
3630 icsk->icsk_ca_dst_locked = tcp_ca_dst_locked(dst); in tcp_ca_dst_init()
3631 icsk->icsk_ca_ops = ca; in tcp_ca_dst_init()
3640 struct tcp_sock *tp = tcp_sk(sk); in tcp_connect_init() local
3647 tp->tcp_header_len = sizeof(struct tcphdr); in tcp_connect_init()
3648 if (sock_net(sk)->ipv4.sysctl_tcp_timestamps) in tcp_connect_init()
3649 tp->tcp_header_len += TCPOLEN_TSTAMP_ALIGNED; in tcp_connect_init()
3652 if (tp->af_specific->md5_lookup(sk, sk)) in tcp_connect_init()
3653 tp->tcp_header_len += TCPOLEN_MD5SIG_ALIGNED; in tcp_connect_init()
3657 if (tp->rx_opt.user_mss) in tcp_connect_init()
3658 tp->rx_opt.mss_clamp = tp->rx_opt.user_mss; in tcp_connect_init()
3659 tp->max_window = 0; in tcp_connect_init()
3665 if (!tp->window_clamp) in tcp_connect_init()
3666 tp->window_clamp = dst_metric(dst, RTAX_WINDOW); in tcp_connect_init()
3667 tp->advmss = tcp_mss_clamp(tp, dst_metric_advmss(dst)); in tcp_connect_init()
3672 if (sk->sk_userlocks & SOCK_RCVBUF_LOCK && in tcp_connect_init()
3673 (tp->window_clamp > tcp_full_space(sk) || tp->window_clamp == 0)) in tcp_connect_init()
3674 tp->window_clamp = tcp_full_space(sk); in tcp_connect_init()
3681 tp->advmss - (tp->rx_opt.ts_recent_stamp ? tp->tcp_header_len - sizeof(struct tcphdr) : 0), in tcp_connect_init()
3682 &tp->rcv_wnd, in tcp_connect_init()
3683 &tp->window_clamp, in tcp_connect_init()
3684 sock_net(sk)->ipv4.sysctl_tcp_window_scaling, in tcp_connect_init()
3688 tp->rx_opt.rcv_wscale = rcv_wscale; in tcp_connect_init()
3689 tp->rcv_ssthresh = tp->rcv_wnd; in tcp_connect_init()
3691 sk->sk_err = 0; in tcp_connect_init()
3693 tp->snd_wnd = 0; in tcp_connect_init()
3694 tcp_init_wl(tp, 0); in tcp_connect_init()
3696 tp->snd_una = tp->write_seq; in tcp_connect_init()
3697 tp->snd_sml = tp->write_seq; in tcp_connect_init()
3698 tp->snd_up = tp->write_seq; in tcp_connect_init()
3699 WRITE_ONCE(tp->snd_nxt, tp->write_seq); in tcp_connect_init()
3701 if (likely(!tp->repair)) in tcp_connect_init()
3702 tp->rcv_nxt = 0; in tcp_connect_init()
3704 tp->rcv_tstamp = tcp_jiffies32; in tcp_connect_init()
3705 tp->rcv_wup = tp->rcv_nxt; in tcp_connect_init()
3706 WRITE_ONCE(tp->copied_seq, tp->rcv_nxt); in tcp_connect_init()
3708 inet_csk(sk)->icsk_rto = tcp_timeout_init(sk); in tcp_connect_init()
3709 inet_csk(sk)->icsk_retransmits = 0; in tcp_connect_init()
3710 tcp_clear_retrans(tp); in tcp_connect_init()
3715 struct tcp_sock *tp = tcp_sk(sk); in tcp_connect_queue_skb() local
3718 tcb->end_seq += skb->len; in tcp_connect_queue_skb()
3720 sk_wmem_queued_add(sk, skb->truesize); in tcp_connect_queue_skb()
3721 sk_mem_charge(sk, skb->truesize); in tcp_connect_queue_skb()
3722 WRITE_ONCE(tp->write_seq, tcb->end_seq); in tcp_connect_queue_skb()
3723 tp->packets_out += tcp_skb_pcount(skb); in tcp_connect_queue_skb()
3727 * queue a data-only packet after the regular SYN, such that regular SYNs
3728 * are retransmitted on timeouts. Also if the remote SYN-ACK acknowledges
3735 struct tcp_sock *tp = tcp_sk(sk); in tcp_send_syn_data() local
3736 struct tcp_fastopen_request *fo = tp->fastopen_req; in tcp_send_syn_data()
3740 tp->rx_opt.mss_clamp = tp->advmss; /* If MSS is not cached */ in tcp_send_syn_data()
3741 if (!tcp_fastopen_cookie_check(sk, &tp->rx_opt.mss_clamp, &fo->cookie)) in tcp_send_syn_data()
3744 /* MSS for SYN-data is based on cached MSS and bounded by PMTU and in tcp_send_syn_data()
3745 * user-MSS. Reserve maximum option space for middleboxes that add in tcp_send_syn_data()
3748 tp->rx_opt.mss_clamp = tcp_mss_clamp(tp, tp->rx_opt.mss_clamp); in tcp_send_syn_data()
3750 space = __tcp_mtu_to_mss(sk, inet_csk(sk)->icsk_pmtu_cookie) - in tcp_send_syn_data()
3753 space = min_t(size_t, space, fo->size); in tcp_send_syn_data()
3755 /* limit to order-0 allocations */ in tcp_send_syn_data()
3758 syn_data = sk_stream_alloc_skb(sk, space, sk->sk_allocation, false); in tcp_send_syn_data()
3761 syn_data->ip_summed = CHECKSUM_PARTIAL; in tcp_send_syn_data()
3762 memcpy(syn_data->cb, syn->cb, sizeof(syn->cb)); in tcp_send_syn_data()
3765 &fo->data->msg_iter); in tcp_send_syn_data()
3775 skb_zcopy_set(syn_data, fo->uarg, NULL); in tcp_send_syn_data()
3778 if (space == fo->size) in tcp_send_syn_data()
3779 fo->data = NULL; in tcp_send_syn_data()
3780 fo->copied = space; in tcp_send_syn_data()
3783 if (syn_data->len) in tcp_send_syn_data()
3786 err = tcp_transmit_skb(sk, syn_data, 1, sk->sk_allocation); in tcp_send_syn_data()
3788 syn->skb_mstamp_ns = syn_data->skb_mstamp_ns; in tcp_send_syn_data()
3795 TCP_SKB_CB(syn_data)->seq++; in tcp_send_syn_data()
3796 TCP_SKB_CB(syn_data)->tcp_flags = TCPHDR_ACK | TCPHDR_PSH; in tcp_send_syn_data()
3798 tp->syn_data = (fo->copied > 0); in tcp_send_syn_data()
3799 tcp_rbtree_insert(&sk->tcp_rtx_queue, syn_data); in tcp_send_syn_data()
3805 __skb_queue_tail(&sk->sk_write_queue, syn_data); in tcp_send_syn_data()
3806 tp->packets_out -= tcp_skb_pcount(syn_data); in tcp_send_syn_data()
3810 if (fo->cookie.len > 0) in tcp_send_syn_data()
3811 fo->cookie.len = 0; in tcp_send_syn_data()
3812 err = tcp_transmit_skb(sk, syn, 1, sk->sk_allocation); in tcp_send_syn_data()
3814 tp->syn_fastopen = 0; in tcp_send_syn_data()
3816 fo->cookie.len = -1; /* Exclude Fast Open option for SYN retries */ in tcp_send_syn_data()
3823 struct tcp_sock *tp = tcp_sk(sk); in tcp_connect() local
3829 if (inet_csk(sk)->icsk_af_ops->rebuild_header(sk)) in tcp_connect()
3830 return -EHOSTUNREACH; /* Routing failure or similar. */ in tcp_connect()
3834 if (unlikely(tp->repair)) { in tcp_connect()
3839 buff = sk_stream_alloc_skb(sk, 0, sk->sk_allocation, true); in tcp_connect()
3841 return -ENOBUFS; in tcp_connect()
3843 tcp_init_nondata_skb(buff, tp->write_seq++, TCPHDR_SYN); in tcp_connect()
3844 tcp_mstamp_refresh(tp); in tcp_connect()
3845 tp->retrans_stamp = tcp_time_stamp(tp); in tcp_connect()
3848 tcp_rbtree_insert(&sk->tcp_rtx_queue, buff); in tcp_connect()
3851 err = tp->fastopen_req ? tcp_send_syn_data(sk, buff) : in tcp_connect()
3852 tcp_transmit_skb(sk, buff, 1, sk->sk_allocation); in tcp_connect()
3853 if (err == -ECONNREFUSED) in tcp_connect()
3856 /* We change tp->snd_nxt after the tcp_transmit_skb() call in tcp_connect()
3859 WRITE_ONCE(tp->snd_nxt, tp->write_seq); in tcp_connect()
3860 tp->pushed_seq = tp->write_seq; in tcp_connect()
3863 WRITE_ONCE(tp->snd_nxt, TCP_SKB_CB(buff)->seq); in tcp_connect()
3864 tp->pushed_seq = TCP_SKB_CB(buff)->seq; in tcp_connect()
3870 inet_csk(sk)->icsk_rto, TCP_RTO_MAX); in tcp_connect()
3882 int ato = icsk->icsk_ack.ato; in tcp_send_delayed_ack()
3886 const struct tcp_sock *tp = tcp_sk(sk); in tcp_send_delayed_ack() local
3890 (icsk->icsk_ack.pending & ICSK_ACK_PUSHED)) in tcp_send_delayed_ack()
3896 * Do not use inet_csk(sk)->icsk_rto here, use results of rtt measurements in tcp_send_delayed_ack()
3899 if (tp->srtt_us) { in tcp_send_delayed_ack()
3900 int rtt = max_t(int, usecs_to_jiffies(tp->srtt_us >> 3), in tcp_send_delayed_ack()
3910 ato = min_t(u32, ato, inet_csk(sk)->icsk_delack_max); in tcp_send_delayed_ack()
3916 if (icsk->icsk_ack.pending & ICSK_ACK_TIMER) { in tcp_send_delayed_ack()
3918 if (time_before_eq(icsk->icsk_ack.timeout, jiffies + (ato >> 2))) { in tcp_send_delayed_ack()
3923 if (!time_before(timeout, icsk->icsk_ack.timeout)) in tcp_send_delayed_ack()
3924 timeout = icsk->icsk_ack.timeout; in tcp_send_delayed_ack()
3926 icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER; in tcp_send_delayed_ack()
3927 icsk->icsk_ack.timeout = timeout; in tcp_send_delayed_ack()
3928 sk_reset_timer(sk, &icsk->icsk_delack_timer, timeout); in tcp_send_delayed_ack()
3937 if (sk->sk_state == TCP_CLOSE) in __tcp_send_ack()
3950 delay = TCP_DELACK_MAX << icsk->icsk_ack.retry; in __tcp_send_ack()
3952 icsk->icsk_ack.retry++; in __tcp_send_ack()
3954 icsk->icsk_ack.ato = TCP_ATO_MIN; in __tcp_send_ack()
3976 __tcp_send_ack(sk, tcp_sk(sk)->rcv_nxt); in tcp_send_ack()
3986 * Current solution: to send TWO zero-length segments in urgent mode:
3988 * out-of-date with SND.UNA-1 to probe window.
3992 struct tcp_sock *tp = tcp_sk(sk); in tcp_xmit_probe_skb() local
3999 return -1; in tcp_xmit_probe_skb()
4007 tcp_init_nondata_skb(skb, tp->snd_una - !urgent, TCPHDR_ACK); in tcp_xmit_probe_skb()
4015 if (sk->sk_state == TCP_ESTABLISHED) { in tcp_send_window_probe()
4016 tcp_sk(sk)->snd_wl1 = tcp_sk(sk)->rcv_nxt - 1; in tcp_send_window_probe()
4025 struct tcp_sock *tp = tcp_sk(sk); in tcp_write_wakeup() local
4028 if (sk->sk_state == TCP_CLOSE) in tcp_write_wakeup()
4029 return -1; in tcp_write_wakeup()
4032 if (skb && before(TCP_SKB_CB(skb)->seq, tcp_wnd_end(tp))) { in tcp_write_wakeup()
4035 unsigned int seg_size = tcp_wnd_end(tp) - TCP_SKB_CB(skb)->seq; in tcp_write_wakeup()
4037 if (before(tp->pushed_seq, TCP_SKB_CB(skb)->end_seq)) in tcp_write_wakeup()
4038 tp->pushed_seq = TCP_SKB_CB(skb)->end_seq; in tcp_write_wakeup()
4044 if (seg_size < TCP_SKB_CB(skb)->end_seq - TCP_SKB_CB(skb)->seq || in tcp_write_wakeup()
4045 skb->len > mss) { in tcp_write_wakeup()
4047 TCP_SKB_CB(skb)->tcp_flags |= TCPHDR_PSH; in tcp_write_wakeup()
4050 return -1; in tcp_write_wakeup()
4054 TCP_SKB_CB(skb)->tcp_flags |= TCPHDR_PSH; in tcp_write_wakeup()
4060 if (between(tp->snd_up, tp->snd_una + 1, tp->snd_una + 0xFFFF)) in tcp_write_wakeup()
4072 struct tcp_sock *tp = tcp_sk(sk); in tcp_send_probe0() local
4079 if (tp->packets_out || tcp_write_queue_empty(sk)) { in tcp_send_probe0()
4081 icsk->icsk_probes_out = 0; in tcp_send_probe0()
4082 icsk->icsk_backoff = 0; in tcp_send_probe0()
4086 icsk->icsk_probes_out++; in tcp_send_probe0()
4088 if (icsk->icsk_backoff < net->ipv4.sysctl_tcp_retries2) in tcp_send_probe0()
4089 icsk->icsk_backoff++; in tcp_send_probe0()
4102 const struct tcp_request_sock_ops *af_ops = tcp_rsk(req)->af_specific; in tcp_rtx_synack()
4106 tcp_rsk(req)->txhash = net_tx_rndhash(); in tcp_rtx_synack()
4107 res = af_ops->send_synack(sk, NULL, &fl, req, NULL, TCP_SYNACK_NORMAL, in tcp_rtx_synack()
4113 tcp_sk(sk)->total_retrans++; in tcp_rtx_synack()