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()
91 * Using SND.UNA we will fail to open window, SND.NXT is out of window. :-(
97 const struct tcp_sock *tp = tcp_sk(sk); in tcp_acceptable_seq() local
99 if (!before(tcp_wnd_end(tp), tp->snd_nxt) || in tcp_acceptable_seq()
100 (tp->rx_opt.wscale_ok && in tcp_acceptable_seq()
101 ((tp->snd_nxt - tcp_wnd_end(tp)) < (1 << tp->rx_opt.rcv_wscale)))) in tcp_acceptable_seq()
102 return tp->snd_nxt; in tcp_acceptable_seq()
104 return tcp_wnd_end(tp); in tcp_acceptable_seq()
108 * RFC1122, RFC1063, draft-ietf-tcpimpl-pmtud-01 state that:
111 * 2. Ideally, it is maximal possible segment size i.e. 65535-40.
123 struct tcp_sock *tp = tcp_sk(sk); in tcp_advertise_mss() local
125 int mss = tp->advmss; in tcp_advertise_mss()
132 tp->advmss = mss; in tcp_advertise_mss()
144 struct tcp_sock *tp = tcp_sk(sk); in tcp_cwnd_restart() local
145 u32 restart_cwnd = tcp_init_cwnd(tp, __sk_dst_get(sk)); in tcp_cwnd_restart()
146 u32 cwnd = tcp_snd_cwnd(tp); in tcp_cwnd_restart()
150 tp->snd_ssthresh = tcp_current_ssthresh(sk); in tcp_cwnd_restart()
153 while ((delta -= inet_csk(sk)->icsk_rto) > 0 && cwnd > restart_cwnd) in tcp_cwnd_restart()
155 tcp_snd_cwnd_set(tp, max(cwnd, restart_cwnd)); in tcp_cwnd_restart()
156 tp->snd_cwnd_stamp = tcp_jiffies32; in tcp_cwnd_restart()
157 tp->snd_cwnd_used = 0; in tcp_cwnd_restart()
161 static void tcp_event_data_sent(struct tcp_sock *tp, in tcp_event_data_sent() argument
167 if (tcp_packets_in_flight(tp) == 0) in tcp_event_data_sent()
170 tp->lsndtime = now; in tcp_event_data_sent()
175 if ((u32)(now - icsk->icsk_ack.lrcvtime) < icsk->icsk_ack.ato) in tcp_event_data_sent()
183 struct tcp_sock *tp = tcp_sk(sk); in tcp_event_ack_sent() local
185 if (unlikely(tp->compressed_ack)) { in tcp_event_ack_sent()
187 tp->compressed_ack); in tcp_event_ack_sent()
188 tp->compressed_ack = 0; in tcp_event_ack_sent()
189 if (hrtimer_try_to_cancel(&tp->compressed_ack_timer) == 1) in tcp_event_ack_sent()
193 if (unlikely(rcv_nxt != tp->rcv_nxt)) in tcp_event_ack_sent()
201 * will be offered. Store the results in the tp structure.
225 * we will truncate our initial window offering to 32K-1 in tcp_select_initial_window()
230 if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_workaround_signed_windows)) in tcp_select_initial_window()
241 space = max_t(u32, space, READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_rmem[2])); in tcp_select_initial_window()
244 *rcv_wscale = clamp_t(int, ilog2(space) - 15, in tcp_select_initial_window()
254 * value can be stuffed directly into th->window for an outgoing
259 struct tcp_sock *tp = tcp_sk(sk); in tcp_select_window() local
260 u32 old_win = tp->rcv_wnd; in tcp_select_window()
261 u32 cur_win = tcp_receive_window(tp); in tcp_select_window()
269 * window in time. --DaveM in tcp_select_window()
276 new_win = ALIGN(cur_win, 1 << tp->rx_opt.rcv_wscale); in tcp_select_window()
278 tp->rcv_wnd = new_win; in tcp_select_window()
279 tp->rcv_wup = tp->rcv_nxt; in tcp_select_window()
284 if (!tp->rx_opt.rcv_wscale && in tcp_select_window()
285 READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_workaround_signed_windows)) in tcp_select_window()
288 new_win = min(new_win, (65535U << tp->rx_opt.rcv_wscale)); in tcp_select_window()
291 new_win >>= tp->rx_opt.rcv_wscale; in tcp_select_window()
295 tp->pred_flags = 0; in tcp_select_window()
306 /* Packet ECN state for a SYN-ACK */
309 const struct tcp_sock *tp = tcp_sk(sk); in tcp_ecn_send_synack() local
311 TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_CWR; in tcp_ecn_send_synack()
312 if (!(tp->ecn_flags & TCP_ECN_OK)) in tcp_ecn_send_synack()
313 TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_ECE; in tcp_ecn_send_synack()
322 struct tcp_sock *tp = tcp_sk(sk); in tcp_ecn_send_syn() local
324 bool use_ecn = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_ecn) == 1 || in tcp_ecn_send_syn()
334 tp->ecn_flags = 0; in tcp_ecn_send_syn()
337 TCP_SKB_CB(skb)->tcp_flags |= TCPHDR_ECE | TCPHDR_CWR; in tcp_ecn_send_syn()
338 tp->ecn_flags = TCP_ECN_OK; in tcp_ecn_send_syn()
346 if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_ecn_fallback)) in tcp_ecn_clear_syn()
347 /* tp->ecn_flags are cleared at a later point in time when in tcp_ecn_clear_syn()
350 TCP_SKB_CB(skb)->tcp_flags &= ~(TCPHDR_ECE | TCPHDR_CWR); in tcp_ecn_clear_syn()
356 if (inet_rsk(req)->ecn_ok) in tcp_ecn_make_synack()
357 th->ece = 1; in tcp_ecn_make_synack()
366 struct tcp_sock *tp = tcp_sk(sk); in tcp_ecn_send() local
368 if (tp->ecn_flags & TCP_ECN_OK) { in tcp_ecn_send()
369 /* Not-retransmitted data segment: set ECT and inject CWR. */ in tcp_ecn_send()
370 if (skb->len != tcp_header_len && in tcp_ecn_send()
371 !before(TCP_SKB_CB(skb)->seq, tp->snd_nxt)) { in tcp_ecn_send()
373 if (tp->ecn_flags & TCP_ECN_QUEUE_CWR) { in tcp_ecn_send()
374 tp->ecn_flags &= ~TCP_ECN_QUEUE_CWR; in tcp_ecn_send()
375 th->cwr = 1; in tcp_ecn_send()
376 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN; in tcp_ecn_send()
382 if (tp->ecn_flags & TCP_ECN_DEMAND_CWR) in tcp_ecn_send()
383 th->ece = 1; in tcp_ecn_send()
387 /* Constructs common control bits of non-data skb. If SYN/FIN is present,
392 skb->ip_summed = CHECKSUM_PARTIAL; in tcp_init_nondata_skb()
394 TCP_SKB_CB(skb)->tcp_flags = flags; in tcp_init_nondata_skb()
398 TCP_SKB_CB(skb)->seq = seq; in tcp_init_nondata_skb()
401 TCP_SKB_CB(skb)->end_seq = seq; in tcp_init_nondata_skb()
404 static inline bool tcp_urg_mode(const struct tcp_sock *tp) in tcp_urg_mode() argument
406 return tp->snd_una != tp->snd_up; in tcp_urg_mode()
446 struct tcp_sock *tp, in mptcp_options_write() argument
450 if (unlikely(OPTION_MPTCP & opts->options)) in mptcp_options_write()
451 mptcp_write_options(th, ptr, tp, &opts->mptcp); in mptcp_options_write()
497 * Thus, "req" is passed here and the cgroup-bpf-progs in bpf_skops_hdr_opt_len()
502 * consistent between fastopen and non-fastopen on in bpf_skops_hdr_opt_len()
525 opts->bpf_opt_len = *remaining - sock_ops.remaining_opt_len; in bpf_skops_hdr_opt_len()
527 opts->bpf_opt_len = (opts->bpf_opt_len + 3) & ~3; in bpf_skops_hdr_opt_len()
529 *remaining -= opts->bpf_opt_len; in bpf_skops_hdr_opt_len()
538 u8 first_opt_off, nr_written, max_opt_len = opts->bpf_opt_len; in bpf_skops_write_hdr_opt()
561 first_opt_off = tcp_hdrlen(skb) - max_opt_len; in bpf_skops_write_hdr_opt()
569 nr_written = max_opt_len - sock_ops.remaining_opt_len; in bpf_skops_write_hdr_opt()
572 memset(skb->data + first_opt_off + nr_written, TCPOPT_NOP, in bpf_skops_write_hdr_opt()
573 max_opt_len - nr_written); in bpf_skops_write_hdr_opt()
596 * Beware: Something in the Internet is very sensitive to the ordering of
598 * Luckily we can at least blame others for their non-compliance but from
599 * inter-operability perspective it seems that we're somewhat stuck with
607 static void tcp_options_write(struct tcphdr *th, 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()
704 mptcp_options_write(th, ptr, tp, opts); 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(READ_ONCE(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(READ_ONCE(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(READ_ONCE(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 tcp_snd_cwnd(tp) > 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_setup(&tsq->tasklet, tcp_tasklet_func); in tcp_tasklet_init()
1139 struct sock *sk = skb->sk; in tcp_wfree()
1140 struct tcp_sock *tp = tcp_sk(sk); in tcp_wfree() local
1146 WARN_ON(refcount_sub_and_test(skb->truesize - 1, &sk->sk_wmem_alloc)); in tcp_wfree()
1151 * - less callbacks to tcp_write_xmit(), reducing stress (batches) in tcp_wfree()
1152 * - chance for incoming ACK (processed by another cpu maybe) in tcp_wfree()
1153 * to migrate this flow (skb->ooo_okay will be eventually set) in tcp_wfree()
1155 if (refcount_read(&sk->sk_wmem_alloc) >= SKB_TRUESIZE(1) && this_cpu_ksoftirqd() == current) in tcp_wfree()
1158 for (oval = READ_ONCE(sk->sk_tsq_flags);; oval = nval) { in tcp_wfree()
1166 nval = cmpxchg(&sk->sk_tsq_flags, oval, nval); in tcp_wfree()
1173 empty = list_empty(&tsq->head); in tcp_wfree()
1174 list_add(&tp->tsq_node, &tsq->head); in tcp_wfree()
1176 tasklet_schedule(&tsq->tasklet); in tcp_wfree()
1189 struct tcp_sock *tp = container_of(timer, struct tcp_sock, pacing_timer); in tcp_pace_kick() local
1190 struct sock *sk = (struct sock *)tp; in tcp_pace_kick()
1201 struct tcp_sock *tp = tcp_sk(sk); in tcp_update_skb_after_send() local
1203 if (sk->sk_pacing_status != SK_PACING_NONE) { in tcp_update_skb_after_send()
1204 unsigned long rate = sk->sk_pacing_rate; in tcp_update_skb_after_send()
1207 * Note that tp->data_segs_out overflows after 2^32 packets, in tcp_update_skb_after_send()
1210 if (rate != ~0UL && rate && tp->data_segs_out >= 10) { in tcp_update_skb_after_send()
1211 u64 len_ns = div64_ul((u64)skb->len * NSEC_PER_SEC, rate); in tcp_update_skb_after_send()
1212 u64 credit = tp->tcp_wstamp_ns - prior_wstamp; in tcp_update_skb_after_send()
1215 len_ns -= min_t(u64, len_ns / 2, credit); in tcp_update_skb_after_send()
1216 tp->tcp_wstamp_ns += len_ns; in tcp_update_skb_after_send()
1219 list_move_tail(&skb->tcp_tsorted_anchor, &tp->tsorted_sent_queue); in tcp_update_skb_after_send()
1242 struct tcp_sock *tp; in __tcp_transmit_skb() local
1253 tp = tcp_sk(sk); in __tcp_transmit_skb()
1254 prior_wstamp = tp->tcp_wstamp_ns; in __tcp_transmit_skb()
1255 tp->tcp_wstamp_ns = max(tp->tcp_wstamp_ns, tp->tcp_clock_cache); in __tcp_transmit_skb()
1256 skb_set_delivery_time(skb, tp->tcp_wstamp_ns, true); in __tcp_transmit_skb()
1268 return -ENOBUFS; in __tcp_transmit_skb()
1269 /* retransmit skbs might have a non zero value in skb->dev in __tcp_transmit_skb()
1270 * because skb->dev is aliased with skb->rbnode.rb_left in __tcp_transmit_skb()
1272 skb->dev = NULL; in __tcp_transmit_skb()
1279 if (unlikely(tcb->tcp_flags & TCPHDR_SYN)) { in __tcp_transmit_skb()
1288 * and in this case it is better to delay the delivery of 1-MSS in __tcp_transmit_skb()
1293 tcb->tcp_flags |= TCPHDR_PSH; in __tcp_transmit_skb()
1301 * TODO: Ideally, in-flight pure ACK packets should not matter here. in __tcp_transmit_skb()
1302 * One way to get this would be to set skb->truesize = 2 on them. in __tcp_transmit_skb()
1304 skb->ooo_okay = sk_wmem_alloc_get(sk) < SKB_TRUESIZE(1); in __tcp_transmit_skb()
1311 skb->pfmemalloc = 0; in __tcp_transmit_skb()
1317 skb->sk = sk; in __tcp_transmit_skb()
1318 skb->destructor = skb_is_tcp_pure_ack(skb) ? __sock_wfree : tcp_wfree; in __tcp_transmit_skb()
1319 refcount_add(skb->truesize, &sk->sk_wmem_alloc); in __tcp_transmit_skb()
1321 skb_set_dst_pending_confirm(skb, sk->sk_dst_pending_confirm); in __tcp_transmit_skb()
1324 th = (struct tcphdr *)skb->data; in __tcp_transmit_skb()
1325 th->source = inet->inet_sport; in __tcp_transmit_skb()
1326 th->dest = inet->inet_dport; in __tcp_transmit_skb()
1327 th->seq = htonl(tcb->seq); in __tcp_transmit_skb()
1328 th->ack_seq = htonl(rcv_nxt); in __tcp_transmit_skb()
1330 tcb->tcp_flags); in __tcp_transmit_skb()
1332 th->check = 0; in __tcp_transmit_skb()
1333 th->urg_ptr = 0; in __tcp_transmit_skb()
1336 if (unlikely(tcp_urg_mode(tp) && before(tcb->seq, tp->snd_up))) { in __tcp_transmit_skb()
1337 if (before(tp->snd_up, tcb->seq + 0x10000)) { in __tcp_transmit_skb()
1338 th->urg_ptr = htons(tp->snd_up - tcb->seq); in __tcp_transmit_skb()
1339 th->urg = 1; in __tcp_transmit_skb()
1340 } else if (after(tcb->seq + 0xFFFF, tp->snd_nxt)) { in __tcp_transmit_skb()
1341 th->urg_ptr = htons(0xFFFF); in __tcp_transmit_skb()
1342 th->urg = 1; in __tcp_transmit_skb()
1346 skb_shinfo(skb)->gso_type = sk->sk_gso_type; in __tcp_transmit_skb()
1347 if (likely(!(tcb->tcp_flags & TCPHDR_SYN))) { in __tcp_transmit_skb()
1348 th->window = htons(tcp_select_window(sk)); in __tcp_transmit_skb()
1354 th->window = htons(min(tp->rcv_wnd, 65535U)); in __tcp_transmit_skb()
1357 tcp_options_write(th, tp, &opts); in __tcp_transmit_skb()
1363 tp->af_specific->calc_md5_hash(opts.hash_location, in __tcp_transmit_skb()
1371 INDIRECT_CALL_INET(icsk->icsk_af_ops->send_check, in __tcp_transmit_skb()
1375 if (likely(tcb->tcp_flags & TCPHDR_ACK)) in __tcp_transmit_skb()
1378 if (skb->len != tcp_header_size) { in __tcp_transmit_skb()
1379 tcp_event_data_sent(tp, sk); in __tcp_transmit_skb()
1380 tp->data_segs_out += tcp_skb_pcount(skb); in __tcp_transmit_skb()
1381 tp->bytes_sent += skb->len - tcp_header_size; in __tcp_transmit_skb()
1384 if (after(tcb->end_seq, tp->snd_nxt) || tcb->seq == tcb->end_seq) in __tcp_transmit_skb()
1388 tp->segs_out += tcp_skb_pcount(skb); in __tcp_transmit_skb()
1390 /* OK, its time to fill skb_shinfo(skb)->gso_{segs|size} */ in __tcp_transmit_skb()
1391 skb_shinfo(skb)->gso_segs = tcp_skb_pcount(skb); in __tcp_transmit_skb()
1392 skb_shinfo(skb)->gso_size = tcp_skb_mss(skb); in __tcp_transmit_skb()
1394 /* Leave earliest departure time in skb->tstamp (skb->skb_mstamp_ns) */ in __tcp_transmit_skb()
1397 memset(skb->cb, 0, max(sizeof(struct inet_skb_parm), in __tcp_transmit_skb()
1400 tcp_add_tx_delay(skb, tp); in __tcp_transmit_skb()
1402 err = INDIRECT_CALL_INET(icsk->icsk_af_ops->queue_xmit, in __tcp_transmit_skb()
1404 sk, skb, &inet->cork.fl); in __tcp_transmit_skb()
1421 tcp_sk(sk)->rcv_nxt); in tcp_transmit_skb()
1431 struct tcp_sock *tp = tcp_sk(sk); in tcp_queue_skb() local
1434 WRITE_ONCE(tp->write_seq, TCP_SKB_CB(skb)->end_seq); in tcp_queue_skb()
1437 sk_wmem_queued_add(sk, skb->truesize); in tcp_queue_skb()
1438 sk_mem_charge(sk, skb->truesize); in tcp_queue_skb()
1444 if (skb->len <= mss_now) { in tcp_set_skb_tso_segs()
1446 * non-TSO case. in tcp_set_skb_tso_segs()
1449 TCP_SKB_CB(skb)->tcp_gso_size = 0; in tcp_set_skb_tso_segs()
1451 tcp_skb_pcount_set(skb, DIV_ROUND_UP(skb->len, mss_now)); in tcp_set_skb_tso_segs()
1452 TCP_SKB_CB(skb)->tcp_gso_size = mss_now; in tcp_set_skb_tso_segs()
1461 struct tcp_sock *tp = tcp_sk(sk); in tcp_adjust_pcount() local
1463 tp->packets_out -= decr; in tcp_adjust_pcount()
1465 if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED) in tcp_adjust_pcount()
1466 tp->sacked_out -= decr; in tcp_adjust_pcount()
1467 if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_RETRANS) in tcp_adjust_pcount()
1468 tp->retrans_out -= decr; in tcp_adjust_pcount()
1469 if (TCP_SKB_CB(skb)->sacked & TCPCB_LOST) in tcp_adjust_pcount()
1470 tp->lost_out -= decr; in tcp_adjust_pcount()
1473 if (tcp_is_reno(tp) && decr > 0) in tcp_adjust_pcount()
1474 tp->sacked_out -= min_t(u32, tp->sacked_out, decr); in tcp_adjust_pcount()
1476 if (tp->lost_skb_hint && in tcp_adjust_pcount()
1477 before(TCP_SKB_CB(skb)->seq, TCP_SKB_CB(tp->lost_skb_hint)->seq) && in tcp_adjust_pcount()
1478 (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED)) in tcp_adjust_pcount()
1479 tp->lost_cnt_hint -= decr; in tcp_adjust_pcount()
1481 tcp_verify_left_out(tp); in tcp_adjust_pcount()
1486 return TCP_SKB_CB(skb)->txstamp_ack || in tcp_has_tx_tstamp()
1487 (skb_shinfo(skb)->tx_flags & SKBTX_ANY_TSTAMP); in tcp_has_tx_tstamp()
1495 !before(shinfo->tskey, TCP_SKB_CB(skb2)->seq)) { in tcp_fragment_tstamp()
1497 u8 tsflags = shinfo->tx_flags & SKBTX_ANY_TSTAMP; in tcp_fragment_tstamp()
1499 shinfo->tx_flags &= ~tsflags; in tcp_fragment_tstamp()
1500 shinfo2->tx_flags |= tsflags; in tcp_fragment_tstamp()
1501 swap(shinfo->tskey, shinfo2->tskey); in tcp_fragment_tstamp()
1502 TCP_SKB_CB(skb2)->txstamp_ack = TCP_SKB_CB(skb)->txstamp_ack; in tcp_fragment_tstamp()
1503 TCP_SKB_CB(skb)->txstamp_ack = 0; in tcp_fragment_tstamp()
1509 TCP_SKB_CB(skb2)->eor = TCP_SKB_CB(skb)->eor; in tcp_skb_fragment_eor()
1510 TCP_SKB_CB(skb)->eor = 0; in tcp_skb_fragment_eor()
1520 __skb_queue_after(&sk->sk_write_queue, skb, buff); in tcp_insert_write_queue_after()
1522 tcp_rbtree_insert(&sk->tcp_rtx_queue, buff); in tcp_insert_write_queue_after()
1534 struct tcp_sock *tp = tcp_sk(sk); in tcp_fragment() local
1541 if (WARN_ON(len > skb->len)) in tcp_fragment()
1542 return -EINVAL; in tcp_fragment()
1544 nsize = skb_headlen(skb) - len; in tcp_fragment()
1553 limit = sk->sk_sndbuf + 2 * SKB_TRUESIZE(GSO_LEGACY_MAX_SIZE); in tcp_fragment()
1554 if (unlikely((sk->sk_wmem_queued >> 1) > limit && in tcp_fragment()
1559 return -ENOMEM; in tcp_fragment()
1563 return -ENOMEM; in tcp_fragment()
1568 return -ENOMEM; /* We'll just try again later. */ in tcp_fragment()
1572 sk_wmem_queued_add(sk, buff->truesize); in tcp_fragment()
1573 sk_mem_charge(sk, buff->truesize); in tcp_fragment()
1574 nlen = skb->len - len - nsize; in tcp_fragment()
1575 buff->truesize += nlen; in tcp_fragment()
1576 skb->truesize -= nlen; in tcp_fragment()
1579 TCP_SKB_CB(buff)->seq = TCP_SKB_CB(skb)->seq + len; in tcp_fragment()
1580 TCP_SKB_CB(buff)->end_seq = TCP_SKB_CB(skb)->end_seq; in tcp_fragment()
1581 TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(buff)->seq; in tcp_fragment()
1584 flags = TCP_SKB_CB(skb)->tcp_flags; in tcp_fragment()
1585 TCP_SKB_CB(skb)->tcp_flags = flags & ~(TCPHDR_FIN | TCPHDR_PSH); in tcp_fragment()
1586 TCP_SKB_CB(buff)->tcp_flags = flags; in tcp_fragment()
1587 TCP_SKB_CB(buff)->sacked = TCP_SKB_CB(skb)->sacked; in tcp_fragment()
1592 skb_set_delivery_time(buff, skb->tstamp, true); in tcp_fragment()
1602 TCP_SKB_CB(buff)->tx = TCP_SKB_CB(skb)->tx; in tcp_fragment()
1605 * adjust the various packet counters. in tcp_fragment()
1607 if (!before(tp->snd_nxt, TCP_SKB_CB(buff)->end_seq)) { in tcp_fragment()
1608 int diff = old_factor - tcp_skb_pcount(skb) - in tcp_fragment()
1619 list_add(&buff->tcp_tsorted_anchor, &skb->tcp_tsorted_anchor); in tcp_fragment()
1635 len -= eat; in __pskb_trim_head()
1642 for (i = 0; i < shinfo->nr_frags; i++) { in __pskb_trim_head()
1643 int size = skb_frag_size(&shinfo->frags[i]); in __pskb_trim_head()
1647 eat -= size; in __pskb_trim_head()
1649 shinfo->frags[k] = shinfo->frags[i]; in __pskb_trim_head()
1651 skb_frag_off_add(&shinfo->frags[k], eat); in __pskb_trim_head()
1652 skb_frag_size_sub(&shinfo->frags[k], eat); in __pskb_trim_head()
1658 shinfo->nr_frags = k; in __pskb_trim_head()
1660 skb->data_len -= len; in __pskb_trim_head()
1661 skb->len = skb->data_len; in __pskb_trim_head()
1671 return -ENOMEM; in tcp_trim_head()
1675 TCP_SKB_CB(skb)->seq += len; in tcp_trim_head()
1678 skb->truesize -= delta_truesize; in tcp_trim_head()
1679 sk_wmem_queued_add(sk, -delta_truesize); in tcp_trim_head()
1684 /* Any change of skb->len requires recalculation of tso factor. */ in tcp_trim_head()
1694 const struct tcp_sock *tp = tcp_sk(sk); in __tcp_mtu_to_mss() local
1699 It is MMS_S - sizeof(tcphdr) of rfc1122 in __tcp_mtu_to_mss()
1701 mss_now = pmtu - icsk->icsk_af_ops->net_header_len - sizeof(struct tcphdr); in __tcp_mtu_to_mss()
1704 if (icsk->icsk_af_ops->net_frag_header_len) { in __tcp_mtu_to_mss()
1708 mss_now -= icsk->icsk_af_ops->net_frag_header_len; in __tcp_mtu_to_mss()
1712 if (mss_now > tp->rx_opt.mss_clamp) in __tcp_mtu_to_mss()
1713 mss_now = tp->rx_opt.mss_clamp; in __tcp_mtu_to_mss()
1716 mss_now -= icsk->icsk_ext_hdr_len; in __tcp_mtu_to_mss()
1720 READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_min_snd_mss)); in __tcp_mtu_to_mss()
1728 return __tcp_mtu_to_mss(sk, pmtu) - in tcp_mtu_to_mss()
1729 (tcp_sk(sk)->tcp_header_len - sizeof(struct tcphdr)); in tcp_mtu_to_mss()
1736 const struct tcp_sock *tp = tcp_sk(sk); in tcp_mss_to_mtu() local
1741 tp->tcp_header_len + in tcp_mss_to_mtu()
1742 icsk->icsk_ext_hdr_len + in tcp_mss_to_mtu()
1743 icsk->icsk_af_ops->net_header_len; in tcp_mss_to_mtu()
1746 if (icsk->icsk_af_ops->net_frag_header_len) { in tcp_mss_to_mtu()
1750 mtu += icsk->icsk_af_ops->net_frag_header_len; in tcp_mss_to_mtu()
1759 struct tcp_sock *tp = tcp_sk(sk); in tcp_mtup_init() local
1763 icsk->icsk_mtup.enabled = READ_ONCE(net->ipv4.sysctl_tcp_mtu_probing) > 1; in tcp_mtup_init()
1764 icsk->icsk_mtup.search_high = tp->rx_opt.mss_clamp + sizeof(struct tcphdr) + in tcp_mtup_init()
1765 icsk->icsk_af_ops->net_header_len; in tcp_mtup_init()
1766 icsk->icsk_mtup.search_low = tcp_mss_to_mtu(sk, READ_ONCE(net->ipv4.sysctl_tcp_base_mss)); in tcp_mtup_init()
1767 icsk->icsk_mtup.probe_size = 0; in tcp_mtup_init()
1768 if (icsk->icsk_mtup.enabled) in tcp_mtup_init()
1769 icsk->icsk_mtup.probe_timestamp = tcp_jiffies32; in tcp_mtup_init()
1775 tp->rx_opt.user_mss is mss set by user by TCP_MAXSEG. It does NOT counts
1778 tp->rx_opt.mss_clamp is mss negotiated at connection setup.
1782 inet_csk(sk)->icsk_pmtu_cookie is last pmtu, seen by this function.
1784 tp->mss_cache is current effective sending mss, including
1787 tp->rx_opt.mss_clamp.
1792 NOTE2. inet_csk(sk)->icsk_pmtu_cookie and tp->mss_cache
1793 are READ ONLY outside this function. --ANK (980731)
1797 struct tcp_sock *tp = tcp_sk(sk); in tcp_sync_mss() local
1801 if (icsk->icsk_mtup.search_high > pmtu) in tcp_sync_mss()
1802 icsk->icsk_mtup.search_high = pmtu; in tcp_sync_mss()
1805 mss_now = tcp_bound_to_half_wnd(tp, mss_now); in tcp_sync_mss()
1808 icsk->icsk_pmtu_cookie = pmtu; in tcp_sync_mss()
1809 if (icsk->icsk_mtup.enabled) in tcp_sync_mss()
1810 mss_now = min(mss_now, tcp_mtu_to_mss(sk, icsk->icsk_mtup.search_low)); in tcp_sync_mss()
1811 tp->mss_cache = mss_now; in tcp_sync_mss()
1822 const struct tcp_sock *tp = tcp_sk(sk); in tcp_current_mss() local
1829 mss_now = tp->mss_cache; in tcp_current_mss()
1833 if (mtu != inet_csk(sk)->icsk_pmtu_cookie) in tcp_current_mss()
1839 /* The mss_cache is sized based on tp->tcp_header_len, which assumes in tcp_current_mss()
1842 * we have to adjust mss_now correspondingly */ in tcp_current_mss()
1843 if (header_len != tp->tcp_header_len) { in tcp_current_mss()
1844 int delta = (int) header_len - tp->tcp_header_len; in tcp_current_mss()
1845 mss_now -= delta; in tcp_current_mss()
1851 /* RFC2861, slow part. Adjust cwnd, after it was not full during one rto.
1857 struct tcp_sock *tp = tcp_sk(sk); in tcp_cwnd_application_limited() local
1859 if (inet_csk(sk)->icsk_ca_state == TCP_CA_Open && in tcp_cwnd_application_limited()
1860 sk->sk_socket && !test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) { in tcp_cwnd_application_limited()
1862 u32 init_win = tcp_init_cwnd(tp, __sk_dst_get(sk)); in tcp_cwnd_application_limited()
1863 u32 win_used = max(tp->snd_cwnd_used, init_win); in tcp_cwnd_application_limited()
1864 if (win_used < tcp_snd_cwnd(tp)) { in tcp_cwnd_application_limited()
1865 tp->snd_ssthresh = tcp_current_ssthresh(sk); in tcp_cwnd_application_limited()
1866 tcp_snd_cwnd_set(tp, (tcp_snd_cwnd(tp) + win_used) >> 1); in tcp_cwnd_application_limited()
1868 tp->snd_cwnd_used = 0; in tcp_cwnd_application_limited()
1870 tp->snd_cwnd_stamp = tcp_jiffies32; in tcp_cwnd_application_limited()
1875 const struct tcp_congestion_ops *ca_ops = inet_csk(sk)->icsk_ca_ops; in tcp_cwnd_validate()
1876 struct tcp_sock *tp = tcp_sk(sk); in tcp_cwnd_validate() local
1879 * is fully utilized. If cwnd-limited then remember that fact for the in tcp_cwnd_validate()
1880 * current window. If not cwnd-limited then track the maximum number of in tcp_cwnd_validate()
1881 * outstanding packets in the current window. (If cwnd-limited then we in tcp_cwnd_validate()
1882 * chose to not update tp->max_packets_out to avoid an extra else in tcp_cwnd_validate()
1885 if (!before(tp->snd_una, tp->cwnd_usage_seq) || in tcp_cwnd_validate()
1887 (!tp->is_cwnd_limited && in tcp_cwnd_validate()
1888 tp->packets_out > tp->max_packets_out)) { in tcp_cwnd_validate()
1889 tp->is_cwnd_limited = is_cwnd_limited; in tcp_cwnd_validate()
1890 tp->max_packets_out = tp->packets_out; in tcp_cwnd_validate()
1891 tp->cwnd_usage_seq = tp->snd_nxt; in tcp_cwnd_validate()
1896 tp->snd_cwnd_used = 0; in tcp_cwnd_validate()
1897 tp->snd_cwnd_stamp = tcp_jiffies32; in tcp_cwnd_validate()
1900 if (tp->packets_out > tp->snd_cwnd_used) in tcp_cwnd_validate()
1901 tp->snd_cwnd_used = tp->packets_out; in tcp_cwnd_validate()
1903 if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_slow_start_after_idle) && in tcp_cwnd_validate()
1904 (s32)(tcp_jiffies32 - tp->snd_cwnd_stamp) >= inet_csk(sk)->icsk_rto && in tcp_cwnd_validate()
1905 !ca_ops->cong_control) in tcp_cwnd_validate()
1915 if (tcp_write_queue_empty(sk) && sk->sk_socket && in tcp_cwnd_validate()
1916 test_bit(SOCK_NOSPACE, &sk->sk_socket->flags) && in tcp_cwnd_validate()
1917 (1 << sk->sk_state) & (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) in tcp_cwnd_validate()
1923 static bool tcp_minshall_check(const struct tcp_sock *tp) in tcp_minshall_check() argument
1925 return after(tp->snd_sml, tp->snd_una) && in tcp_minshall_check()
1926 !after(tp->snd_sml, tp->snd_nxt); in tcp_minshall_check()
1930 * Note that a TSO packet might end with a sub-mss segment
1932 * if ((skb->len % mss) != 0)
1933 * tp->snd_sml = TCP_SKB_CB(skb)->end_seq;
1935 * skb_pcount = skb->len / mss_now
1937 static void tcp_minshall_update(struct tcp_sock *tp, unsigned int mss_now, in tcp_minshall_update() argument
1940 if (skb->len < tcp_skb_pcount(skb) * mss_now) in tcp_minshall_update()
1941 tp->snd_sml = TCP_SKB_CB(skb)->end_seq; in tcp_minshall_update()
1951 static bool tcp_nagle_check(bool partial, const struct tcp_sock *tp, in tcp_nagle_check() argument
1956 (!nonagle && tp->packets_out && tcp_minshall_check(tp))); in tcp_nagle_check()
1963 * - For close peers, we rather send bigger packets to reduce
1965 * - For long distance/rtt flows, we would like to get ACK clocking
1969 * in bigger TSO bursts. We we cut the RTT-based allowance in half
1970 * for every 2^9 usec (aka 512 us) of RTT, so that the RTT-based allowance
1979 bytes = sk->sk_pacing_rate >> READ_ONCE(sk->sk_pacing_shift); in tcp_tso_autosize()
1981 r = tcp_min_rtt(tcp_sk(sk)) >> READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_tso_rtt_log); in tcp_tso_autosize()
1982 if (r < BITS_PER_TYPE(sk->sk_gso_max_size)) in tcp_tso_autosize()
1983 bytes += sk->sk_gso_max_size >> r; in tcp_tso_autosize()
1985 bytes = min_t(unsigned long, bytes, sk->sk_gso_max_size); in tcp_tso_autosize()
1995 const struct tcp_congestion_ops *ca_ops = inet_csk(sk)->icsk_ca_ops; in tcp_tso_segs()
1998 min_tso = ca_ops->min_tso_segs ? in tcp_tso_segs()
1999 ca_ops->min_tso_segs(sk) : in tcp_tso_segs()
2000 READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_min_tso_segs); in tcp_tso_segs()
2003 return min_t(u32, tso_segs, sk->sk_gso_max_segs); in tcp_tso_segs()
2013 const struct tcp_sock *tp = tcp_sk(sk); in tcp_mss_split_point() local
2016 window = tcp_wnd_end(tp) - TCP_SKB_CB(skb)->seq; in tcp_mss_split_point()
2022 needed = min(skb->len, window); in tcp_mss_split_point()
2032 if (tcp_nagle_check(partial != 0, tp, nonagle)) in tcp_mss_split_point()
2033 return needed - partial; in tcp_mss_split_point()
2041 static inline unsigned int tcp_cwnd_test(const struct tcp_sock *tp, in tcp_cwnd_test() argument
2047 if ((TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN) && in tcp_cwnd_test()
2051 in_flight = tcp_packets_in_flight(tp); in tcp_cwnd_test()
2052 cwnd = tcp_snd_cwnd(tp); in tcp_cwnd_test()
2060 return min(halfcwnd, cwnd - in_flight); in tcp_cwnd_test()
2082 static inline bool tcp_nagle_test(const struct tcp_sock *tp, const struct sk_buff *skb, in tcp_nagle_test() argument
2095 if (tcp_urg_mode(tp) || (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)) in tcp_nagle_test()
2098 if (!tcp_nagle_check(skb->len < cur_mss, tp, nonagle)) in tcp_nagle_test()
2105 static bool tcp_snd_wnd_test(const struct tcp_sock *tp, in tcp_snd_wnd_test() argument
2109 u32 end_seq = TCP_SKB_CB(skb)->end_seq; in tcp_snd_wnd_test()
2111 if (skb->len > cur_mss) in tcp_snd_wnd_test()
2112 end_seq = TCP_SKB_CB(skb)->seq + cur_mss; in tcp_snd_wnd_test()
2114 return !after(end_seq, tcp_wnd_end(tp)); in tcp_snd_wnd_test()
2121 * know that all the data is in scatter-gather pages, and that the
2127 int nlen = skb->len - len; in tso_fragment()
2132 if (skb->len != skb->data_len) in tso_fragment()
2138 return -ENOMEM; in tso_fragment()
2142 sk_wmem_queued_add(sk, buff->truesize); in tso_fragment()
2143 sk_mem_charge(sk, buff->truesize); in tso_fragment()
2144 buff->truesize += nlen; in tso_fragment()
2145 skb->truesize -= nlen; in tso_fragment()
2148 TCP_SKB_CB(buff)->seq = TCP_SKB_CB(skb)->seq + len; in tso_fragment()
2149 TCP_SKB_CB(buff)->end_seq = TCP_SKB_CB(skb)->end_seq; in tso_fragment()
2150 TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(buff)->seq; in tso_fragment()
2153 flags = TCP_SKB_CB(skb)->tcp_flags; in tso_fragment()
2154 TCP_SKB_CB(skb)->tcp_flags = flags & ~(TCPHDR_FIN | TCPHDR_PSH); in tso_fragment()
2155 TCP_SKB_CB(buff)->tcp_flags = flags; in tso_fragment()
2185 struct tcp_sock *tp = tcp_sk(sk); in tcp_tso_should_defer() local
2190 if (icsk->icsk_ca_state >= TCP_CA_Recovery) in tcp_tso_should_defer()
2195 * Note that tp->tcp_wstamp_ns can be in the future if we have in tcp_tso_should_defer()
2198 delta = tp->tcp_clock_cache - tp->tcp_wstamp_ns - NSEC_PER_MSEC; in tcp_tso_should_defer()
2202 in_flight = tcp_packets_in_flight(tp); in tcp_tso_should_defer()
2205 BUG_ON(tcp_snd_cwnd(tp) <= in_flight); in tcp_tso_should_defer()
2207 send_win = tcp_wnd_end(tp) - TCP_SKB_CB(skb)->seq; in tcp_tso_should_defer()
2210 cong_win = (tcp_snd_cwnd(tp) - in_flight) * tp->mss_cache; in tcp_tso_should_defer()
2214 /* If a full-sized TSO skb can be sent, do it. */ in tcp_tso_should_defer()
2215 if (limit >= max_segs * tp->mss_cache) in tcp_tso_should_defer()
2219 if ((skb != tcp_write_queue_tail(sk)) && (limit >= skb->len)) in tcp_tso_should_defer()
2222 win_divisor = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_tso_win_divisor); in tcp_tso_should_defer()
2224 u32 chunk = min(tp->snd_wnd, tcp_snd_cwnd(tp) * tp->mss_cache); in tcp_tso_should_defer()
2238 if (limit > tcp_max_tso_deferred_mss(tp) * tp->mss_cache) in tcp_tso_should_defer()
2246 delta = tp->tcp_clock_cache - head->tstamp; in tcp_tso_should_defer()
2248 if ((s64)(delta - (u64)NSEC_PER_USEC * (tp->srtt_us >> 4)) < 0) in tcp_tso_should_defer()
2253 * 1) We are cwnd-limited in tcp_tso_should_defer()
2254 * 2) We are rwnd-limited in tcp_tso_should_defer()
2258 if (cong_win <= skb->len) { in tcp_tso_should_defer()
2263 if (send_win <= skb->len) { in tcp_tso_should_defer()
2270 if ((TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN) || in tcp_tso_should_defer()
2271 TCP_SKB_CB(skb)->eor) in tcp_tso_should_defer()
2283 struct tcp_sock *tp = tcp_sk(sk); in tcp_mtu_check_reprobe() local
2288 interval = READ_ONCE(net->ipv4.sysctl_tcp_probe_interval); in tcp_mtu_check_reprobe()
2289 delta = tcp_jiffies32 - icsk->icsk_mtup.probe_timestamp; in tcp_mtu_check_reprobe()
2294 icsk->icsk_mtup.probe_size = 0; in tcp_mtu_check_reprobe()
2295 icsk->icsk_mtup.search_high = tp->rx_opt.mss_clamp + in tcp_mtu_check_reprobe()
2297 icsk->icsk_af_ops->net_header_len; in tcp_mtu_check_reprobe()
2298 icsk->icsk_mtup.search_low = tcp_mss_to_mtu(sk, mss); in tcp_mtu_check_reprobe()
2301 icsk->icsk_mtup.probe_timestamp = tcp_jiffies32; in tcp_mtu_check_reprobe()
2311 if (len <= skb->len) in tcp_can_coalesce_send_queue_head()
2314 if (unlikely(TCP_SKB_CB(skb)->eor) || in tcp_can_coalesce_send_queue_head()
2319 len -= skb->len; in tcp_can_coalesce_send_queue_head()
2332 * -1 otherwise
2337 struct tcp_sock *tp = tcp_sk(sk); in tcp_mtu_probe() local
2351 if (likely(!icsk->icsk_mtup.enabled || in tcp_mtu_probe()
2352 icsk->icsk_mtup.probe_size || in tcp_mtu_probe()
2353 inet_csk(sk)->icsk_ca_state != TCP_CA_Open || in tcp_mtu_probe()
2354 tcp_snd_cwnd(tp) < 11 || in tcp_mtu_probe()
2355 tp->rx_opt.num_sacks || tp->rx_opt.dsack)) in tcp_mtu_probe()
2356 return -1; in tcp_mtu_probe()
2359 * and current mss_clamp. if (search_high - search_low) in tcp_mtu_probe()
2363 probe_size = tcp_mtu_to_mss(sk, (icsk->icsk_mtup.search_high + in tcp_mtu_probe()
2364 icsk->icsk_mtup.search_low) >> 1); in tcp_mtu_probe()
2365 size_needed = probe_size + (tp->reordering + 1) * tp->mss_cache; in tcp_mtu_probe()
2366 interval = icsk->icsk_mtup.search_high - icsk->icsk_mtup.search_low; in tcp_mtu_probe()
2371 if (probe_size > tcp_mtu_to_mss(sk, icsk->icsk_mtup.search_high) || in tcp_mtu_probe()
2372 interval < READ_ONCE(net->ipv4.sysctl_tcp_probe_threshold)) { in tcp_mtu_probe()
2377 return -1; in tcp_mtu_probe()
2381 if (tp->write_seq - tp->snd_nxt < size_needed) in tcp_mtu_probe()
2382 return -1; in tcp_mtu_probe()
2384 if (tp->snd_wnd < size_needed) in tcp_mtu_probe()
2385 return -1; in tcp_mtu_probe()
2386 if (after(tp->snd_nxt + size_needed, tcp_wnd_end(tp))) in tcp_mtu_probe()
2390 if (tcp_packets_in_flight(tp) + 2 > tcp_snd_cwnd(tp)) { in tcp_mtu_probe()
2391 if (!tcp_packets_in_flight(tp)) in tcp_mtu_probe()
2392 return -1; in tcp_mtu_probe()
2398 return -1; in tcp_mtu_probe()
2403 return -1; in tcp_mtu_probe()
2404 sk_wmem_queued_add(sk, nskb->truesize); in tcp_mtu_probe()
2405 sk_mem_charge(sk, nskb->truesize); in tcp_mtu_probe()
2411 TCP_SKB_CB(nskb)->seq = TCP_SKB_CB(skb)->seq; in tcp_mtu_probe()
2412 TCP_SKB_CB(nskb)->end_seq = TCP_SKB_CB(skb)->seq + probe_size; in tcp_mtu_probe()
2413 TCP_SKB_CB(nskb)->tcp_flags = TCPHDR_ACK; in tcp_mtu_probe()
2420 copy = min_t(int, skb->len, probe_size - len); in tcp_mtu_probe()
2423 if (skb->len <= copy) { in tcp_mtu_probe()
2426 TCP_SKB_CB(nskb)->tcp_flags |= TCP_SKB_CB(skb)->tcp_flags; in tcp_mtu_probe()
2430 TCP_SKB_CB(nskb)->eor = TCP_SKB_CB(skb)->eor; in tcp_mtu_probe()
2435 TCP_SKB_CB(nskb)->tcp_flags |= TCP_SKB_CB(skb)->tcp_flags & in tcp_mtu_probe()
2437 if (!skb_shinfo(skb)->nr_frags) { in tcp_mtu_probe()
2443 TCP_SKB_CB(skb)->seq += copy; in tcp_mtu_probe()
2451 tcp_init_tso_segs(nskb, nskb->len); in tcp_mtu_probe()
2454 * be resegmented into mss-sized pieces by tcp_write_xmit(). in tcp_mtu_probe()
2459 tcp_snd_cwnd_set(tp, tcp_snd_cwnd(tp) - 1); in tcp_mtu_probe()
2462 icsk->icsk_mtup.probe_size = tcp_mss_to_mtu(sk, nskb->len); in tcp_mtu_probe()
2463 tp->mtu_probe.probe_seq_start = TCP_SKB_CB(nskb)->seq; in tcp_mtu_probe()
2464 tp->mtu_probe.probe_seq_end = TCP_SKB_CB(nskb)->end_seq; in tcp_mtu_probe()
2469 return -1; in tcp_mtu_probe()
2474 struct tcp_sock *tp = tcp_sk(sk); in tcp_pacing_check() local
2479 if (tp->tcp_wstamp_ns <= tp->tcp_clock_cache) in tcp_pacing_check()
2482 if (!hrtimer_is_queued(&tp->pacing_timer)) { in tcp_pacing_check()
2483 hrtimer_start(&tp->pacing_timer, in tcp_pacing_check()
2484 ns_to_ktime(tp->tcp_wstamp_ns), in tcp_pacing_check()
2495 * - better RTT estimation and ACK scheduling
2496 * - faster recovery
2497 * - high rates
2508 2 * skb->truesize, in tcp_small_queue_check()
2509 sk->sk_pacing_rate >> READ_ONCE(sk->sk_pacing_shift)); in tcp_small_queue_check()
2510 if (sk->sk_pacing_status == SK_PACING_NONE) in tcp_small_queue_check()
2512 READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_limit_output_bytes)); in tcp_small_queue_check()
2516 tcp_sk(sk)->tcp_tx_delay) { in tcp_small_queue_check()
2517 u64 extra_bytes = (u64)sk->sk_pacing_rate * tcp_sk(sk)->tcp_tx_delay; in tcp_small_queue_check()
2520 * approximate our needs assuming an ~100% skb->truesize overhead. in tcp_small_queue_check()
2524 extra_bytes >>= (20 - 1); in tcp_small_queue_check()
2527 if (refcount_read(&sk->sk_wmem_alloc) > limit) { in tcp_small_queue_check()
2536 set_bit(TSQ_THROTTLED, &sk->sk_tsq_flags); in tcp_small_queue_check()
2542 if (refcount_read(&sk->sk_wmem_alloc) > limit) in tcp_small_queue_check()
2548 static void tcp_chrono_set(struct tcp_sock *tp, const enum tcp_chrono new) in tcp_chrono_set() argument
2551 enum tcp_chrono old = tp->chrono_type; in tcp_chrono_set()
2554 tp->chrono_stat[old - 1] += now - tp->chrono_start; in tcp_chrono_set()
2555 tp->chrono_start = now; in tcp_chrono_set()
2556 tp->chrono_type = new; in tcp_chrono_set()
2561 struct tcp_sock *tp = tcp_sk(sk); in tcp_chrono_start() local
2568 if (type > tp->chrono_type) in tcp_chrono_start()
2569 tcp_chrono_set(tp, type); in tcp_chrono_start()
2574 struct tcp_sock *tp = tcp_sk(sk); in tcp_chrono_stop() local
2585 tcp_chrono_set(tp, TCP_CHRONO_UNSPEC); in tcp_chrono_stop()
2586 else if (type == tp->chrono_type) in tcp_chrono_stop()
2587 tcp_chrono_set(tp, TCP_CHRONO_BUSY); in tcp_chrono_stop()
2595 * snd_up-64k-mss .. snd_up cannot be large. However, taking into
2607 struct tcp_sock *tp = tcp_sk(sk); in tcp_write_xmit() local
2617 tcp_mstamp_refresh(tp); in tcp_write_xmit()
2632 if (unlikely(tp->repair) && tp->repair_queue == TCP_SEND_QUEUE) { in tcp_write_xmit()
2634 tp->tcp_wstamp_ns = tp->tcp_clock_cache; in tcp_write_xmit()
2635 skb_set_delivery_time(skb, tp->tcp_wstamp_ns, true); in tcp_write_xmit()
2636 list_move_tail(&skb->tcp_tsorted_anchor, &tp->tsorted_sent_queue); in tcp_write_xmit()
2647 cwnd_quota = tcp_cwnd_test(tp, skb); in tcp_write_xmit()
2656 if (unlikely(!tcp_snd_wnd_test(tp, skb, mss_now))) { in tcp_write_xmit()
2662 if (unlikely(!tcp_nagle_test(tp, skb, mss_now, in tcp_write_xmit()
2674 if (tso_segs > 1 && !tcp_urg_mode(tp)) in tcp_write_xmit()
2681 if (skb->len > limit && in tcp_write_xmit()
2690 * We do not want to send a pure-ack packet and have in tcp_write_xmit()
2693 if (TCP_SKB_CB(skb)->end_seq == TCP_SKB_CB(skb)->seq) in tcp_write_xmit()
2705 tcp_minshall_update(tp, mss_now, skb); in tcp_write_xmit()
2717 is_cwnd_limited |= (tcp_packets_in_flight(tp) >= tcp_snd_cwnd(tp)); in tcp_write_xmit()
2723 tp->prr_out += sent_pkts; in tcp_write_xmit()
2730 return !tp->packets_out && !tcp_write_queue_empty(sk); in tcp_write_xmit()
2736 struct tcp_sock *tp = tcp_sk(sk); in tcp_schedule_loss_probe() local
2743 if (rcu_access_pointer(tp->fastopen_rsk)) in tcp_schedule_loss_probe()
2746 early_retrans = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_early_retrans); in tcp_schedule_loss_probe()
2751 !tp->packets_out || !tcp_is_sack(tp) || in tcp_schedule_loss_probe()
2752 (icsk->icsk_ca_state != TCP_CA_Open && in tcp_schedule_loss_probe()
2753 icsk->icsk_ca_state != TCP_CA_CWR)) in tcp_schedule_loss_probe()
2760 if (tp->srtt_us) { in tcp_schedule_loss_probe()
2761 timeout = usecs_to_jiffies(tp->srtt_us >> 2); in tcp_schedule_loss_probe()
2762 if (tp->packets_out == 1) in tcp_schedule_loss_probe()
2772 jiffies_to_usecs(inet_csk(sk)->icsk_rto) : in tcp_schedule_loss_probe()
2789 set_bit(TSQ_THROTTLED, &sk->sk_tsq_flags); in skb_still_in_host_queue()
2805 struct tcp_sock *tp = tcp_sk(sk); in tcp_send_loss_probe() local
2811 if (tp->tlp_high_seq) in tcp_send_loss_probe()
2814 tp->tlp_retrans = 0; in tcp_send_loss_probe()
2816 if (skb && tcp_snd_wnd_test(tp, skb, mss)) { in tcp_send_loss_probe()
2817 pcount = tp->packets_out; in tcp_send_loss_probe()
2819 if (tp->packets_out > pcount) in tcp_send_loss_probe()
2823 skb = skb_rb_last(&sk->tcp_rtx_queue); in tcp_send_loss_probe()
2825 WARN_ONCE(tp->packets_out, in tcp_send_loss_probe()
2827 tp->packets_out, sk->sk_state, tcp_snd_cwnd(tp), mss); in tcp_send_loss_probe()
2828 inet_csk(sk)->icsk_pending = 0; in tcp_send_loss_probe()
2839 if ((pcount > 1) && (skb->len > (pcount - 1) * mss)) { in tcp_send_loss_probe()
2841 (pcount - 1) * mss, mss, in tcp_send_loss_probe()
2853 tp->tlp_retrans = 1; in tcp_send_loss_probe()
2857 tp->tlp_high_seq = tp->snd_nxt; in tcp_send_loss_probe()
2861 inet_csk(sk)->icsk_pending = 0; in tcp_send_loss_probe()
2877 if (unlikely(sk->sk_state == TCP_CLOSE)) in __tcp_push_pending_frames()
2892 BUG_ON(!skb || skb->len < mss_now); in tcp_push_one()
2894 tcp_write_xmit(sk, mss_now, TCP_NAGLE_PUSH, 1, sk->sk_allocation); in tcp_push_one()
2906 * RCV.BUFF - RCV.USER - RCV.WINDOW >= min(1/2 RCV.BUFF, MSS)"
2912 * since header prediction assumes th->window stays fixed.
2914 * Strictly speaking, keeping th->window fixed violates the receiver
2946 * Note, we don't "adjust" for TIMESTAMP or SACK option bytes.
2952 struct tcp_sock *tp = tcp_sk(sk); in __tcp_select_window() local
2957 * fluctuations. --SAW 1998/11/1 in __tcp_select_window()
2959 int mss = icsk->icsk_ack.rcv_mss; in __tcp_select_window()
2967 full_space = min_t(int, tp->window_clamp, allowed_space); in __tcp_select_window()
2975 icsk->icsk_ack.quick = 0; in __tcp_select_window()
2983 free_space = round_down(free_space, 1 << tp->rx_opt.rcv_wscale); in __tcp_select_window()
2986 * of the maximum allowed, try to move to zero-window, else in __tcp_select_window()
2996 if (free_space > tp->rcv_ssthresh) in __tcp_select_window()
2997 free_space = tp->rcv_ssthresh; in __tcp_select_window()
3002 if (tp->rx_opt.rcv_wscale) { in __tcp_select_window()
3009 window = ALIGN(window, (1 << tp->rx_opt.rcv_wscale)); in __tcp_select_window()
3011 window = tp->rcv_wnd; in __tcp_select_window()
3020 if (window <= free_space - mss || window > free_space) in __tcp_select_window()
3038 shinfo->tx_flags |= next_shinfo->tx_flags & SKBTX_ANY_TSTAMP; in tcp_skb_collapse_tstamp()
3039 shinfo->tskey = next_shinfo->tskey; in tcp_skb_collapse_tstamp()
3040 TCP_SKB_CB(skb)->txstamp_ack |= in tcp_skb_collapse_tstamp()
3041 TCP_SKB_CB(next_skb)->txstamp_ack; in tcp_skb_collapse_tstamp()
3048 struct tcp_sock *tp = tcp_sk(sk); in tcp_collapse_retrans() local
3052 next_skb_size = next_skb->len; in tcp_collapse_retrans()
3062 TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(next_skb)->end_seq; in tcp_collapse_retrans()
3065 TCP_SKB_CB(skb)->tcp_flags |= TCP_SKB_CB(next_skb)->tcp_flags; in tcp_collapse_retrans()
3070 TCP_SKB_CB(skb)->sacked |= TCP_SKB_CB(next_skb)->sacked & TCPCB_EVER_RETRANS; in tcp_collapse_retrans()
3071 TCP_SKB_CB(skb)->eor = TCP_SKB_CB(next_skb)->eor; in tcp_collapse_retrans()
3074 tcp_clear_retrans_hints_partial(tp); in tcp_collapse_retrans()
3075 if (next_skb == tp->retransmit_skb_hint) in tcp_collapse_retrans()
3076 tp->retransmit_skb_hint = skb; in tcp_collapse_retrans()
3094 if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED) in tcp_can_collapse()
3106 struct tcp_sock *tp = tcp_sk(sk); in tcp_retrans_try_collapse() local
3110 if (!READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_retrans_collapse)) in tcp_retrans_try_collapse()
3112 if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN) in tcp_retrans_try_collapse()
3122 space -= skb->len; in tcp_retrans_try_collapse()
3132 if (after(TCP_SKB_CB(skb)->end_seq, tcp_wnd_end(tp))) in tcp_retrans_try_collapse()
3141 * state updates are done by the caller. Returns non-zero if an
3147 struct tcp_sock *tp = tcp_sk(sk); in __tcp_retransmit_skb() local
3153 if (icsk->icsk_mtup.probe_size) in __tcp_retransmit_skb()
3154 icsk->icsk_mtup.probe_size = 0; in __tcp_retransmit_skb()
3157 return -EBUSY; in __tcp_retransmit_skb()
3159 if (before(TCP_SKB_CB(skb)->seq, tp->snd_una)) { in __tcp_retransmit_skb()
3160 if (unlikely(before(TCP_SKB_CB(skb)->end_seq, tp->snd_una))) { in __tcp_retransmit_skb()
3162 return -EINVAL; in __tcp_retransmit_skb()
3164 if (tcp_trim_head(sk, skb, tp->snd_una - TCP_SKB_CB(skb)->seq)) in __tcp_retransmit_skb()
3165 return -ENOMEM; in __tcp_retransmit_skb()
3168 if (inet_csk(sk)->icsk_af_ops->rebuild_header(sk)) in __tcp_retransmit_skb()
3169 return -EHOSTUNREACH; /* Routing failure or similar. */ in __tcp_retransmit_skb()
3172 avail_wnd = tcp_wnd_end(tp) - TCP_SKB_CB(skb)->seq; in __tcp_retransmit_skb()
3180 if (TCP_SKB_CB(skb)->seq != tp->snd_una) in __tcp_retransmit_skb()
3181 return -EAGAIN; in __tcp_retransmit_skb()
3191 if (skb->len > len) { in __tcp_retransmit_skb()
3194 return -ENOMEM; /* We'll try again later. */ in __tcp_retransmit_skb()
3197 return -ENOMEM; in __tcp_retransmit_skb()
3201 diff -= tcp_skb_pcount(skb); in __tcp_retransmit_skb()
3205 if (skb->len < avail_wnd) in __tcp_retransmit_skb()
3210 if ((TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN_ECN) == TCPHDR_SYN_ECN) in __tcp_retransmit_skb()
3216 if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN) in __tcp_retransmit_skb()
3218 tp->total_retrans += segs; in __tcp_retransmit_skb()
3219 tp->bytes_retrans += skb->len; in __tcp_retransmit_skb()
3221 /* make sure skb->data is aligned on arches that require it in __tcp_retransmit_skb()
3222 * and check if ack-trimming & collapsing extended the headroom in __tcp_retransmit_skb()
3225 if (unlikely((NET_IP_ALIGN && ((unsigned long)skb->data & 3)) || in __tcp_retransmit_skb()
3232 nskb->dev = NULL; in __tcp_retransmit_skb()
3235 err = -ENOBUFS; in __tcp_retransmit_skb()
3240 tcp_update_skb_after_send(sk, skb, tp->tcp_wstamp_ns); in __tcp_retransmit_skb()
3250 TCP_SKB_CB(skb)->sacked |= TCPCB_EVER_RETRANS; in __tcp_retransmit_skb()
3252 if (BPF_SOCK_OPS_TEST_FLAG(tp, BPF_SOCK_OPS_RETRANS_CB_FLAG)) in __tcp_retransmit_skb()
3254 TCP_SKB_CB(skb)->seq, segs, err); in __tcp_retransmit_skb()
3258 } else if (err != -EBUSY) { in __tcp_retransmit_skb()
3266 struct tcp_sock *tp = tcp_sk(sk); in tcp_retransmit_skb() local
3271 if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_RETRANS) { in tcp_retransmit_skb()
3275 TCP_SKB_CB(skb)->sacked |= TCPCB_RETRANS; in tcp_retransmit_skb()
3276 tp->retrans_out += tcp_skb_pcount(skb); in tcp_retransmit_skb()
3280 if (!tp->retrans_stamp) in tcp_retransmit_skb()
3281 tp->retrans_stamp = tcp_skb_timestamp(skb); in tcp_retransmit_skb()
3283 if (tp->undo_retrans < 0) in tcp_retransmit_skb()
3284 tp->undo_retrans = 0; in tcp_retransmit_skb()
3285 tp->undo_retrans += tcp_skb_pcount(skb); in tcp_retransmit_skb()
3298 struct tcp_sock *tp = tcp_sk(sk); in tcp_xmit_retransmit_queue() local
3303 if (!tp->packets_out) in tcp_xmit_retransmit_queue()
3307 skb = tp->retransmit_skb_hint ?: rtx_head; in tcp_xmit_retransmit_queue()
3318 tp->retransmit_skb_hint = skb; in tcp_xmit_retransmit_queue()
3320 segs = tcp_snd_cwnd(tp) - tcp_packets_in_flight(tp); in tcp_xmit_retransmit_queue()
3323 sacked = TCP_SKB_CB(skb)->sacked; in tcp_xmit_retransmit_queue()
3329 if (tp->retrans_out >= tp->lost_out) { in tcp_xmit_retransmit_queue()
3337 if (icsk->icsk_ca_state != TCP_CA_Loss) in tcp_xmit_retransmit_queue()
3355 tp->prr_out += tcp_skb_pcount(skb); in tcp_xmit_retransmit_queue()
3358 icsk->icsk_pending != ICSK_TIME_REO_TIMEOUT) in tcp_xmit_retransmit_queue()
3364 inet_csk(sk)->icsk_rto, in tcp_xmit_retransmit_queue()
3379 delta = size - sk->sk_forward_alloc; in sk_forced_mem_schedule()
3383 sk->sk_forward_alloc += amt << PAGE_SHIFT; in sk_forced_mem_schedule()
3386 if (mem_cgroup_sockets_enabled && sk->sk_memcg) in sk_forced_mem_schedule()
3387 mem_cgroup_charge_skmem(sk->sk_memcg, amt, in sk_forced_mem_schedule()
3397 struct tcp_sock *tp = tcp_sk(sk); in tcp_send_fin() local
3406 tskb = skb_rb_last(&sk->tcp_rtx_queue); in tcp_send_fin()
3409 TCP_SKB_CB(tskb)->tcp_flags |= TCPHDR_FIN; in tcp_send_fin()
3410 TCP_SKB_CB(tskb)->end_seq++; in tcp_send_fin()
3411 tp->write_seq++; in tcp_send_fin()
3415 * We need to set tp->snd_nxt to the value it would have in tcp_send_fin()
3417 * does not change tp->snd_nxt. in tcp_send_fin()
3419 WRITE_ONCE(tp->snd_nxt, tp->snd_nxt + 1); in tcp_send_fin()
3423 skb = alloc_skb_fclone(MAX_TCP_HEADER, sk->sk_allocation); in tcp_send_fin()
3427 INIT_LIST_HEAD(&skb->tcp_tsorted_anchor); in tcp_send_fin()
3429 sk_forced_mem_schedule(sk, skb->truesize); in tcp_send_fin()
3431 tcp_init_nondata_skb(skb, tp->write_seq, in tcp_send_fin()
3441 * by RFC 2525, section 2.17. -DaveM
3471 /* Send a crossed SYN-ACK during socket establishment.
3482 if (!skb || !(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN)) { in tcp_send_synack()
3484 return -EFAULT; in tcp_send_synack()
3486 if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_ACK)) { in tcp_send_synack()
3494 return -ENOMEM; in tcp_send_synack()
3495 INIT_LIST_HEAD(&nskb->tcp_tsorted_anchor); in tcp_send_synack()
3499 tcp_rbtree_insert(&sk->tcp_rtx_queue, nskb); in tcp_send_synack()
3500 sk_wmem_queued_add(sk, nskb->truesize); in tcp_send_synack()
3501 sk_mem_charge(sk, nskb->truesize); in tcp_send_synack()
3505 TCP_SKB_CB(skb)->tcp_flags |= TCPHDR_ACK; in tcp_send_synack()
3512 * tcp_make_synack - Allocate one skb and build a SYNACK packet.
3528 const struct tcp_sock *tp = tcp_sk(sk); in tcp_make_synack() local
3557 * sk->sk_wmem_alloc in an atomic, we can promote to rw. in tcp_make_synack()
3564 mss = tcp_mss_clamp(tp, dst_metric_advmss(dst)); in tcp_make_synack()
3569 if (unlikely(synack_type == TCP_SYNACK_COOKIE && ireq->tstamp_ok)) in tcp_make_synack()
3576 if (!tcp_rsk(req)->snt_synack) /* Timestamp first SYNACK */ in tcp_make_synack()
3577 tcp_rsk(req)->snt_synack = tcp_skb_timestamp_us(skb); in tcp_make_synack()
3582 md5 = tcp_rsk(req)->af_specific->req_md5_lookup(sk, req_to_sk(req)); in tcp_make_synack()
3584 skb_set_hash(skb, tcp_rsk(req)->txhash, PKT_HASH_TYPE_L4); in tcp_make_synack()
3586 TCP_SKB_CB(skb)->tcp_flags = TCPHDR_SYN | TCPHDR_ACK; in tcp_make_synack()
3594 th = (struct tcphdr *)skb->data; in tcp_make_synack()
3596 th->syn = 1; in tcp_make_synack()
3597 th->ack = 1; in tcp_make_synack()
3599 th->source = htons(ireq->ir_num); in tcp_make_synack()
3600 th->dest = ireq->ir_rmt_port; in tcp_make_synack()
3601 skb->mark = ireq->ir_mark; in tcp_make_synack()
3602 skb->ip_summed = CHECKSUM_PARTIAL; in tcp_make_synack()
3603 th->seq = htonl(tcp_rsk(req)->snt_isn); in tcp_make_synack()
3605 th->ack_seq = htonl(tcp_rsk(req)->rcv_nxt); in tcp_make_synack()
3608 th->window = htons(min(req->rsk_rcv_wnd, 65535U)); in tcp_make_synack()
3610 th->doff = (tcp_header_size >> 2); in tcp_make_synack()
3614 /* Okay, we have all we need - do the md5 hash if needed */ in tcp_make_synack()
3616 tcp_rsk(req)->af_specific->calc_md5_hash(opts.hash_location, in tcp_make_synack()
3625 tcp_add_tx_delay(skb, tp); in tcp_make_synack()
3642 if (likely(ca && bpf_try_module_get(ca, ca->owner))) { in tcp_ca_dst_init()
3643 bpf_module_put(icsk->icsk_ca_ops, icsk->icsk_ca_ops->owner); in tcp_ca_dst_init()
3644 icsk->icsk_ca_dst_locked = tcp_ca_dst_locked(dst); in tcp_ca_dst_init()
3645 icsk->icsk_ca_ops = ca; in tcp_ca_dst_init()
3654 struct tcp_sock *tp = tcp_sk(sk); in tcp_connect_init() local
3661 tp->tcp_header_len = sizeof(struct tcphdr); in tcp_connect_init()
3662 if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_timestamps)) in tcp_connect_init()
3663 tp->tcp_header_len += TCPOLEN_TSTAMP_ALIGNED; in tcp_connect_init()
3666 if (tp->af_specific->md5_lookup(sk, sk)) in tcp_connect_init()
3667 tp->tcp_header_len += TCPOLEN_MD5SIG_ALIGNED; in tcp_connect_init()
3671 if (tp->rx_opt.user_mss) in tcp_connect_init()
3672 tp->rx_opt.mss_clamp = tp->rx_opt.user_mss; in tcp_connect_init()
3673 tp->max_window = 0; in tcp_connect_init()
3679 if (!tp->window_clamp) in tcp_connect_init()
3680 tp->window_clamp = dst_metric(dst, RTAX_WINDOW); in tcp_connect_init()
3681 tp->advmss = tcp_mss_clamp(tp, dst_metric_advmss(dst)); in tcp_connect_init()
3686 if (sk->sk_userlocks & SOCK_RCVBUF_LOCK && in tcp_connect_init()
3687 (tp->window_clamp > tcp_full_space(sk) || tp->window_clamp == 0)) in tcp_connect_init()
3688 tp->window_clamp = tcp_full_space(sk); in tcp_connect_init()
3695 tp->advmss - (tp->rx_opt.ts_recent_stamp ? tp->tcp_header_len - sizeof(struct tcphdr) : 0), in tcp_connect_init()
3696 &tp->rcv_wnd, in tcp_connect_init()
3697 &tp->window_clamp, in tcp_connect_init()
3698 READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_window_scaling), in tcp_connect_init()
3702 tp->rx_opt.rcv_wscale = rcv_wscale; in tcp_connect_init()
3703 tp->rcv_ssthresh = tp->rcv_wnd; in tcp_connect_init()
3705 sk->sk_err = 0; in tcp_connect_init()
3707 tp->snd_wnd = 0; in tcp_connect_init()
3708 tcp_init_wl(tp, 0); in tcp_connect_init()
3710 tp->snd_una = tp->write_seq; in tcp_connect_init()
3711 tp->snd_sml = tp->write_seq; in tcp_connect_init()
3712 tp->snd_up = tp->write_seq; in tcp_connect_init()
3713 WRITE_ONCE(tp->snd_nxt, tp->write_seq); in tcp_connect_init()
3715 if (likely(!tp->repair)) in tcp_connect_init()
3716 tp->rcv_nxt = 0; in tcp_connect_init()
3718 tp->rcv_tstamp = tcp_jiffies32; in tcp_connect_init()
3719 tp->rcv_wup = tp->rcv_nxt; in tcp_connect_init()
3720 WRITE_ONCE(tp->copied_seq, tp->rcv_nxt); in tcp_connect_init()
3722 inet_csk(sk)->icsk_rto = tcp_timeout_init(sk); in tcp_connect_init()
3723 inet_csk(sk)->icsk_retransmits = 0; in tcp_connect_init()
3724 tcp_clear_retrans(tp); in tcp_connect_init()
3729 struct tcp_sock *tp = tcp_sk(sk); in tcp_connect_queue_skb() local
3732 tcb->end_seq += skb->len; in tcp_connect_queue_skb()
3734 sk_wmem_queued_add(sk, skb->truesize); in tcp_connect_queue_skb()
3735 sk_mem_charge(sk, skb->truesize); in tcp_connect_queue_skb()
3736 WRITE_ONCE(tp->write_seq, tcb->end_seq); in tcp_connect_queue_skb()
3737 tp->packets_out += tcp_skb_pcount(skb); in tcp_connect_queue_skb()
3741 * queue a data-only packet after the regular SYN, such that regular SYNs
3742 * are retransmitted on timeouts. Also if the remote SYN-ACK acknowledges
3750 struct tcp_sock *tp = tcp_sk(sk); in tcp_send_syn_data() local
3751 struct tcp_fastopen_request *fo = tp->fastopen_req; in tcp_send_syn_data()
3755 tp->rx_opt.mss_clamp = tp->advmss; /* If MSS is not cached */ in tcp_send_syn_data()
3756 if (!tcp_fastopen_cookie_check(sk, &tp->rx_opt.mss_clamp, &fo->cookie)) in tcp_send_syn_data()
3759 /* MSS for SYN-data is based on cached MSS and bounded by PMTU and in tcp_send_syn_data()
3760 * user-MSS. Reserve maximum option space for middleboxes that add in tcp_send_syn_data()
3763 tp->rx_opt.mss_clamp = tcp_mss_clamp(tp, tp->rx_opt.mss_clamp); in tcp_send_syn_data()
3765 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie); in tcp_send_syn_data()
3767 space = __tcp_mtu_to_mss(sk, icsk->icsk_pmtu_cookie) - in tcp_send_syn_data()
3770 space = min_t(size_t, space, fo->size); in tcp_send_syn_data()
3772 /* limit to order-0 allocations */ in tcp_send_syn_data()
3775 syn_data = tcp_stream_alloc_skb(sk, space, sk->sk_allocation, false); in tcp_send_syn_data()
3778 memcpy(syn_data->cb, syn->cb, sizeof(syn->cb)); in tcp_send_syn_data()
3781 &fo->data->msg_iter); in tcp_send_syn_data()
3791 skb_zcopy_set(syn_data, fo->uarg, NULL); in tcp_send_syn_data()
3794 if (space == fo->size) in tcp_send_syn_data()
3795 fo->data = NULL; in tcp_send_syn_data()
3796 fo->copied = space; in tcp_send_syn_data()
3799 if (syn_data->len) in tcp_send_syn_data()
3802 err = tcp_transmit_skb(sk, syn_data, 1, sk->sk_allocation); in tcp_send_syn_data()
3804 skb_set_delivery_time(syn, syn_data->skb_mstamp_ns, true); in tcp_send_syn_data()
3811 TCP_SKB_CB(syn_data)->seq++; in tcp_send_syn_data()
3812 TCP_SKB_CB(syn_data)->tcp_flags = TCPHDR_ACK | TCPHDR_PSH; in tcp_send_syn_data()
3814 tp->syn_data = (fo->copied > 0); in tcp_send_syn_data()
3815 tcp_rbtree_insert(&sk->tcp_rtx_queue, syn_data); in tcp_send_syn_data()
3821 __skb_queue_tail(&sk->sk_write_queue, syn_data); in tcp_send_syn_data()
3822 tp->packets_out -= tcp_skb_pcount(syn_data); in tcp_send_syn_data()
3826 if (fo->cookie.len > 0) in tcp_send_syn_data()
3827 fo->cookie.len = 0; in tcp_send_syn_data()
3828 err = tcp_transmit_skb(sk, syn, 1, sk->sk_allocation); in tcp_send_syn_data()
3830 tp->syn_fastopen = 0; in tcp_send_syn_data()
3832 fo->cookie.len = -1; /* Exclude Fast Open option for SYN retries */ in tcp_send_syn_data()
3839 struct tcp_sock *tp = tcp_sk(sk); in tcp_connect() local
3845 if (inet_csk(sk)->icsk_af_ops->rebuild_header(sk)) in tcp_connect()
3846 return -EHOSTUNREACH; /* Routing failure or similar. */ in tcp_connect()
3850 if (unlikely(tp->repair)) { in tcp_connect()
3855 buff = tcp_stream_alloc_skb(sk, 0, sk->sk_allocation, true); in tcp_connect()
3857 return -ENOBUFS; in tcp_connect()
3859 tcp_init_nondata_skb(buff, tp->write_seq++, TCPHDR_SYN); in tcp_connect()
3860 tcp_mstamp_refresh(tp); in tcp_connect()
3861 tp->retrans_stamp = tcp_time_stamp(tp); in tcp_connect()
3864 tcp_rbtree_insert(&sk->tcp_rtx_queue, buff); in tcp_connect()
3867 err = tp->fastopen_req ? tcp_send_syn_data(sk, buff) : in tcp_connect()
3868 tcp_transmit_skb(sk, buff, 1, sk->sk_allocation); in tcp_connect()
3869 if (err == -ECONNREFUSED) in tcp_connect()
3872 /* We change tp->snd_nxt after the tcp_transmit_skb() call in tcp_connect()
3875 WRITE_ONCE(tp->snd_nxt, tp->write_seq); in tcp_connect()
3876 tp->pushed_seq = tp->write_seq; in tcp_connect()
3879 WRITE_ONCE(tp->snd_nxt, TCP_SKB_CB(buff)->seq); in tcp_connect()
3880 tp->pushed_seq = TCP_SKB_CB(buff)->seq; in tcp_connect()
3886 inet_csk(sk)->icsk_rto, TCP_RTO_MAX); in tcp_connect()
3898 int ato = icsk->icsk_ack.ato; in tcp_send_delayed_ack()
3902 const struct tcp_sock *tp = tcp_sk(sk); in tcp_send_delayed_ack() local
3906 (icsk->icsk_ack.pending & ICSK_ACK_PUSHED)) in tcp_send_delayed_ack()
3912 * Do not use inet_csk(sk)->icsk_rto here, use results of rtt measurements in tcp_send_delayed_ack()
3915 if (tp->srtt_us) { in tcp_send_delayed_ack()
3916 int rtt = max_t(int, usecs_to_jiffies(tp->srtt_us >> 3), in tcp_send_delayed_ack()
3926 ato = min_t(u32, ato, inet_csk(sk)->icsk_delack_max); in tcp_send_delayed_ack()
3932 if (icsk->icsk_ack.pending & ICSK_ACK_TIMER) { in tcp_send_delayed_ack()
3934 if (time_before_eq(icsk->icsk_ack.timeout, jiffies + (ato >> 2))) { in tcp_send_delayed_ack()
3939 if (!time_before(timeout, icsk->icsk_ack.timeout)) in tcp_send_delayed_ack()
3940 timeout = icsk->icsk_ack.timeout; in tcp_send_delayed_ack()
3942 icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER; in tcp_send_delayed_ack()
3943 icsk->icsk_ack.timeout = timeout; in tcp_send_delayed_ack()
3944 sk_reset_timer(sk, &icsk->icsk_delack_timer, timeout); in tcp_send_delayed_ack()
3953 if (sk->sk_state == TCP_CLOSE) in __tcp_send_ack()
3966 delay = TCP_DELACK_MAX << icsk->icsk_ack.retry; in __tcp_send_ack()
3968 icsk->icsk_ack.retry++; in __tcp_send_ack()
3970 icsk->icsk_ack.ato = TCP_ATO_MIN; in __tcp_send_ack()
3992 __tcp_send_ack(sk, tcp_sk(sk)->rcv_nxt); in tcp_send_ack()
4002 * Current solution: to send TWO zero-length segments in urgent mode:
4004 * out-of-date with SND.UNA-1 to probe window.
4008 struct tcp_sock *tp = tcp_sk(sk); in tcp_xmit_probe_skb() local
4015 return -1; in tcp_xmit_probe_skb()
4023 tcp_init_nondata_skb(skb, tp->snd_una - !urgent, TCPHDR_ACK); in tcp_xmit_probe_skb()
4031 if (sk->sk_state == TCP_ESTABLISHED) { in tcp_send_window_probe()
4032 tcp_sk(sk)->snd_wl1 = tcp_sk(sk)->rcv_nxt - 1; in tcp_send_window_probe()
4041 struct tcp_sock *tp = tcp_sk(sk); in tcp_write_wakeup() local
4044 if (sk->sk_state == TCP_CLOSE) in tcp_write_wakeup()
4045 return -1; in tcp_write_wakeup()
4048 if (skb && before(TCP_SKB_CB(skb)->seq, tcp_wnd_end(tp))) { in tcp_write_wakeup()
4051 unsigned int seg_size = tcp_wnd_end(tp) - TCP_SKB_CB(skb)->seq; in tcp_write_wakeup()
4053 if (before(tp->pushed_seq, TCP_SKB_CB(skb)->end_seq)) in tcp_write_wakeup()
4054 tp->pushed_seq = TCP_SKB_CB(skb)->end_seq; in tcp_write_wakeup()
4060 if (seg_size < TCP_SKB_CB(skb)->end_seq - TCP_SKB_CB(skb)->seq || in tcp_write_wakeup()
4061 skb->len > mss) { in tcp_write_wakeup()
4063 TCP_SKB_CB(skb)->tcp_flags |= TCPHDR_PSH; in tcp_write_wakeup()
4066 return -1; in tcp_write_wakeup()
4070 TCP_SKB_CB(skb)->tcp_flags |= TCPHDR_PSH; in tcp_write_wakeup()
4076 if (between(tp->snd_up, tp->snd_una + 1, tp->snd_una + 0xFFFF)) in tcp_write_wakeup()
4088 struct tcp_sock *tp = tcp_sk(sk); in tcp_send_probe0() local
4095 if (tp->packets_out || tcp_write_queue_empty(sk)) { in tcp_send_probe0()
4097 icsk->icsk_probes_out = 0; in tcp_send_probe0()
4098 icsk->icsk_backoff = 0; in tcp_send_probe0()
4099 icsk->icsk_probes_tstamp = 0; in tcp_send_probe0()
4103 icsk->icsk_probes_out++; in tcp_send_probe0()
4105 if (icsk->icsk_backoff < READ_ONCE(net->ipv4.sysctl_tcp_retries2)) in tcp_send_probe0()
4106 icsk->icsk_backoff++; in tcp_send_probe0()
4121 const struct tcp_request_sock_ops *af_ops = tcp_rsk(req)->af_specific; in tcp_rtx_synack()
4126 if (READ_ONCE(sk->sk_txrehash) == SOCK_TXREHASH_ENABLED) in tcp_rtx_synack()
4127 tcp_rsk(req)->txhash = net_tx_rndhash(); in tcp_rtx_synack()
4128 res = af_ops->send_synack(sk, NULL, &fl, req, NULL, TCP_SYNACK_NORMAL, in tcp_rtx_synack()
4134 tcp_sk(sk)->total_retrans++; in tcp_rtx_synack()