2 * Copyright (c) 2000-2019 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
30 * The Regents of the University of California. All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)tcp_timer.c 8.2 (Berkeley) 5/24/95
61 * $FreeBSD: src/sys/netinet/tcp_timer.c,v 1.34.2.11 2001/08/22 00:59:12 silby Exp $
65 #include <sys/param.h>
66 #include <sys/systm.h>
67 #include <sys/kernel.h>
69 #include <sys/sysctl.h>
70 #include <sys/socket.h>
71 #include <sys/socketvar.h>
72 #include <sys/protosw.h>
73 #include <sys/domain.h>
74 #include <sys/mcache.h>
75 #include <sys/queue.h>
76 #include <kern/locks.h>
77 #include <kern/cpu_number.h> /* before tcp_seq.h, for tcp_random18() */
78 #include <mach/boolean.h>
80 #include <net/route.h>
81 #include <net/if_var.h>
82 #include <net/ntstat.h>
84 #include <netinet/in.h>
85 #include <netinet/in_systm.h>
86 #include <netinet/in_pcb.h>
87 #include <netinet/in_var.h>
89 #include <netinet6/in6_pcb.h>
91 #include <netinet/ip_var.h>
92 #include <netinet/tcp.h>
93 #include <netinet/tcp_cache.h>
94 #include <netinet/tcp_fsm.h>
95 #include <netinet/tcp_seq.h>
96 #include <netinet/tcp_timer.h>
97 #include <netinet/tcp_var.h>
98 #include <netinet/tcp_cc.h>
100 #include <netinet6/tcp6_var.h>
102 #include <netinet/tcpip.h>
104 #include <netinet/tcp_debug.h>
106 #include <netinet/tcp_log.h>
108 #include <sys/kdebug.h>
109 #include <mach/sdt.h>
110 #include <netinet/mptcp_var.h>
112 /* Max number of times a stretch ack can be delayed on a connection */
113 #define TCP_STRETCHACK_DELAY_THRESHOLD 5
116 * If the host processor has been sleeping for too long, this is the threshold
117 * used to avoid sending stale retransmissions.
119 #define TCP_SLEEP_TOO_LONG (10 * 60 * 1000) /* 10 minutes in ms */
122 struct tcptimerlist tcp_timer_list
;
124 /* List of pcbs in timewait state, protected by tcbinfo's ipi_lock */
125 struct tcptailq tcp_tw_tailq
;
128 sysctl_msec_to_ticks SYSCTL_HANDLER_ARGS
134 if (tt
< 0 || tt
>= INT_MAX
/ 1000) {
137 s
= tt
* 1000 / TCP_RETRANSHZ
;
139 error
= sysctl_handle_int(oidp
, &s
, 0, req
);
140 if (error
|| !req
->newptr
) {
144 tt
= s
* TCP_RETRANSHZ
/ 1000;
150 SYSCTL_SKMEM_UPDATE_AT_OFFSET(arg2
, *(int*)arg1
);
155 int tcp_keepinit
= TCPTV_KEEP_INIT
;
156 SYSCTL_PROC(_net_inet_tcp
, TCPCTL_KEEPINIT
, keepinit
,
157 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
158 &tcp_keepinit
, offsetof(skmem_sysctl
, tcp
.keepinit
),
159 sysctl_msec_to_ticks
, "I", "");
161 int tcp_keepidle
= TCPTV_KEEP_IDLE
;
162 SYSCTL_PROC(_net_inet_tcp
, TCPCTL_KEEPIDLE
, keepidle
,
163 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
164 &tcp_keepidle
, offsetof(skmem_sysctl
, tcp
.keepidle
),
165 sysctl_msec_to_ticks
, "I", "");
167 int tcp_keepintvl
= TCPTV_KEEPINTVL
;
168 SYSCTL_PROC(_net_inet_tcp
, TCPCTL_KEEPINTVL
, keepintvl
,
169 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
170 &tcp_keepintvl
, offsetof(skmem_sysctl
, tcp
.keepintvl
),
171 sysctl_msec_to_ticks
, "I", "");
173 SYSCTL_SKMEM_TCP_INT(OID_AUTO
, keepcnt
,
174 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
175 int, tcp_keepcnt
, TCPTV_KEEPCNT
, "number of times to repeat keepalive");
177 int tcp_msl
= TCPTV_MSL
;
178 SYSCTL_PROC(_net_inet_tcp
, OID_AUTO
, msl
,
179 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
180 &tcp_msl
, offsetof(skmem_sysctl
, tcp
.msl
),
181 sysctl_msec_to_ticks
, "I", "Maximum segment lifetime");
182 #else /* SYSCTL_SKMEM */
184 SYSCTL_PROC(_net_inet_tcp
, TCPCTL_KEEPINIT
, keepinit
,
185 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
186 &tcp_keepinit
, 0, sysctl_msec_to_ticks
, "I", "");
189 SYSCTL_PROC(_net_inet_tcp
, TCPCTL_KEEPIDLE
, keepidle
,
190 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
191 &tcp_keepidle
, 0, sysctl_msec_to_ticks
, "I", "");
194 SYSCTL_PROC(_net_inet_tcp
, TCPCTL_KEEPINTVL
, keepintvl
,
195 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
196 &tcp_keepintvl
, 0, sysctl_msec_to_ticks
, "I", "");
199 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, keepcnt
,
200 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
201 &tcp_keepcnt
, 0, "number of times to repeat keepalive");
204 SYSCTL_PROC(_net_inet_tcp
, OID_AUTO
, msl
,
205 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
206 &tcp_msl
, 0, sysctl_msec_to_ticks
, "I", "Maximum segment lifetime");
207 #endif /* SYSCTL_SKMEM */
210 * Avoid DoS via TCP Robustness in Persist Condition
211 * (see http://www.ietf.org/id/draft-ananth-tcpm-persist-02.txt)
212 * by allowing a system wide maximum persistence timeout value when in
213 * Zero Window Probe mode.
215 * Expressed in milliseconds to be consistent without timeout related
216 * values, the TCP socket option is in seconds.
219 u_int32_t tcp_max_persist_timeout
= 0;
220 SYSCTL_PROC(_net_inet_tcp
, OID_AUTO
, max_persist_timeout
,
221 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
222 &tcp_max_persist_timeout
, offsetof(skmem_sysctl
, tcp
.max_persist_timeout
),
223 sysctl_msec_to_ticks
, "I", "Maximum persistence timeout for ZWP");
224 #else /* SYSCTL_SKMEM */
225 u_int32_t tcp_max_persist_timeout
= 0;
226 SYSCTL_PROC(_net_inet_tcp
, OID_AUTO
, max_persist_timeout
,
227 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
228 &tcp_max_persist_timeout
, 0, sysctl_msec_to_ticks
, "I",
229 "Maximum persistence timeout for ZWP");
230 #endif /* SYSCTL_SKMEM */
232 SYSCTL_SKMEM_TCP_INT(OID_AUTO
, always_keepalive
,
233 CTLFLAG_RW
| CTLFLAG_LOCKED
, static int, always_keepalive
, 0,
234 "Assume SO_KEEPALIVE on all TCP connections");
237 * This parameter determines how long the timer list will stay in fast or
238 * quick mode even though all connections are idle. In this state, the
239 * timer will run more frequently anticipating new data.
241 SYSCTL_SKMEM_TCP_INT(OID_AUTO
, timer_fastmode_idlemax
,
242 CTLFLAG_RW
| CTLFLAG_LOCKED
, int, timer_fastmode_idlemax
,
243 TCP_FASTMODE_IDLERUN_MAX
, "Maximum idle generations in fast mode");
246 * See tcp_syn_backoff[] for interval values between SYN retransmits;
247 * the value set below defines the number of retransmits, before we
248 * disable the timestamp and window scaling options during subsequent
249 * SYN retransmits. Setting it to 0 disables the dropping off of those
252 SYSCTL_SKMEM_TCP_INT(OID_AUTO
, broken_peer_syn_rexmit_thres
,
253 CTLFLAG_RW
| CTLFLAG_LOCKED
, static int, tcp_broken_peer_syn_rxmit_thres
,
254 10, "Number of retransmitted SYNs before disabling RFC 1323 "
255 "options on local connections");
257 static int tcp_timer_advanced
= 0;
258 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, tcp_timer_advanced
,
259 CTLFLAG_RD
| CTLFLAG_LOCKED
, &tcp_timer_advanced
, 0,
260 "Number of times one of the timers was advanced");
262 static int tcp_resched_timerlist
= 0;
263 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, tcp_resched_timerlist
,
264 CTLFLAG_RD
| CTLFLAG_LOCKED
, &tcp_resched_timerlist
, 0,
265 "Number of times timer list was rescheduled as part of processing a packet");
267 SYSCTL_SKMEM_TCP_INT(OID_AUTO
, pmtud_blackhole_detection
,
268 CTLFLAG_RW
| CTLFLAG_LOCKED
, int, tcp_pmtud_black_hole_detect
, 1,
269 "Path MTU Discovery Black Hole Detection");
271 SYSCTL_SKMEM_TCP_INT(OID_AUTO
, pmtud_blackhole_mss
,
272 CTLFLAG_RW
| CTLFLAG_LOCKED
, int, tcp_pmtud_black_hole_mss
, 1200,
273 "Path MTU Discovery Black Hole Detection lowered MSS");
275 #if (DEBUG || DEVELOPMENT)
276 int tcp_probe_if_fix_port
= 0;
277 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, probe_if_fix_port
,
278 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
279 &tcp_probe_if_fix_port
, 0, "");
280 #endif /* (DEBUG || DEVELOPMENT) */
282 static u_int32_t tcp_mss_rec_medium
= 1200;
283 static u_int32_t tcp_mss_rec_low
= 512;
285 #define TCP_REPORT_STATS_INTERVAL 43200 /* 12 hours, in seconds */
286 int tcp_report_stats_interval
= TCP_REPORT_STATS_INTERVAL
;
288 /* performed garbage collection of "used" sockets */
289 static boolean_t tcp_gc_done
= FALSE
;
291 /* max idle probes */
292 int tcp_maxpersistidle
= TCPTV_KEEP_IDLE
;
295 * TCP delack timer is set to 100 ms. Since the processing of timer list
296 * in fast mode will happen no faster than 100 ms, the delayed ack timer
297 * will fire some where between 100 and 200 ms.
299 int tcp_delack
= TCP_RETRANSHZ
/ 10;
303 * MP_JOIN retransmission of 3rd ACK will be every 500 msecs without backoff
305 int tcp_jack_rxmt
= TCP_RETRANSHZ
/ 2;
308 static boolean_t tcp_itimer_done
= FALSE
;
310 static void tcp_remove_timer(struct tcpcb
*tp
);
311 static void tcp_sched_timerlist(uint32_t offset
);
312 static u_int32_t
tcp_run_conn_timer(struct tcpcb
*tp
, u_int16_t
*mode
,
313 u_int16_t probe_if_index
);
314 static void tcp_sched_timers(struct tcpcb
*tp
);
315 static inline void tcp_set_lotimer_index(struct tcpcb
*);
316 __private_extern__
void tcp_remove_from_time_wait(struct inpcb
*inp
);
317 static inline void tcp_update_mss_core(struct tcpcb
*tp
, struct ifnet
*ifp
);
318 __private_extern__
void tcp_report_stats(void);
320 static u_int64_t tcp_last_report_time
;
323 * Structure to store previously reported stats so that we can send
324 * incremental changes in each report interval.
326 struct tcp_last_report_stats
{
327 u_int32_t tcps_connattempt
;
328 u_int32_t tcps_accepts
;
329 u_int32_t tcps_ecn_client_setup
;
330 u_int32_t tcps_ecn_server_setup
;
331 u_int32_t tcps_ecn_client_success
;
332 u_int32_t tcps_ecn_server_success
;
333 u_int32_t tcps_ecn_not_supported
;
334 u_int32_t tcps_ecn_lost_syn
;
335 u_int32_t tcps_ecn_lost_synack
;
336 u_int32_t tcps_ecn_recv_ce
;
337 u_int32_t tcps_ecn_recv_ece
;
338 u_int32_t tcps_ecn_sent_ece
;
339 u_int32_t tcps_ecn_conn_recv_ce
;
340 u_int32_t tcps_ecn_conn_recv_ece
;
341 u_int32_t tcps_ecn_conn_plnoce
;
342 u_int32_t tcps_ecn_conn_pl_ce
;
343 u_int32_t tcps_ecn_conn_nopl_ce
;
344 u_int32_t tcps_ecn_fallback_synloss
;
345 u_int32_t tcps_ecn_fallback_reorder
;
346 u_int32_t tcps_ecn_fallback_ce
;
348 /* TFO-related statistics */
349 u_int32_t tcps_tfo_syn_data_rcv
;
350 u_int32_t tcps_tfo_cookie_req_rcv
;
351 u_int32_t tcps_tfo_cookie_sent
;
352 u_int32_t tcps_tfo_cookie_invalid
;
353 u_int32_t tcps_tfo_cookie_req
;
354 u_int32_t tcps_tfo_cookie_rcv
;
355 u_int32_t tcps_tfo_syn_data_sent
;
356 u_int32_t tcps_tfo_syn_data_acked
;
357 u_int32_t tcps_tfo_syn_loss
;
358 u_int32_t tcps_tfo_blackhole
;
359 u_int32_t tcps_tfo_cookie_wrong
;
360 u_int32_t tcps_tfo_no_cookie_rcv
;
361 u_int32_t tcps_tfo_heuristics_disable
;
362 u_int32_t tcps_tfo_sndblackhole
;
364 /* MPTCP-related statistics */
365 u_int32_t tcps_mptcp_handover_attempt
;
366 u_int32_t tcps_mptcp_interactive_attempt
;
367 u_int32_t tcps_mptcp_aggregate_attempt
;
368 u_int32_t tcps_mptcp_fp_handover_attempt
;
369 u_int32_t tcps_mptcp_fp_interactive_attempt
;
370 u_int32_t tcps_mptcp_fp_aggregate_attempt
;
371 u_int32_t tcps_mptcp_heuristic_fallback
;
372 u_int32_t tcps_mptcp_fp_heuristic_fallback
;
373 u_int32_t tcps_mptcp_handover_success_wifi
;
374 u_int32_t tcps_mptcp_handover_success_cell
;
375 u_int32_t tcps_mptcp_interactive_success
;
376 u_int32_t tcps_mptcp_aggregate_success
;
377 u_int32_t tcps_mptcp_fp_handover_success_wifi
;
378 u_int32_t tcps_mptcp_fp_handover_success_cell
;
379 u_int32_t tcps_mptcp_fp_interactive_success
;
380 u_int32_t tcps_mptcp_fp_aggregate_success
;
381 u_int32_t tcps_mptcp_handover_cell_from_wifi
;
382 u_int32_t tcps_mptcp_handover_wifi_from_cell
;
383 u_int32_t tcps_mptcp_interactive_cell_from_wifi
;
384 u_int64_t tcps_mptcp_handover_cell_bytes
;
385 u_int64_t tcps_mptcp_interactive_cell_bytes
;
386 u_int64_t tcps_mptcp_aggregate_cell_bytes
;
387 u_int64_t tcps_mptcp_handover_all_bytes
;
388 u_int64_t tcps_mptcp_interactive_all_bytes
;
389 u_int64_t tcps_mptcp_aggregate_all_bytes
;
390 u_int32_t tcps_mptcp_back_to_wifi
;
391 u_int32_t tcps_mptcp_wifi_proxy
;
392 u_int32_t tcps_mptcp_cell_proxy
;
393 u_int32_t tcps_mptcp_triggered_cell
;
397 /* Returns true if the timer is on the timer list */
398 #define TIMER_IS_ON_LIST(tp) ((tp)->t_flags & TF_TIMER_ONLIST)
400 /* Run the TCP timerlist atleast once every hour */
401 #define TCP_TIMERLIST_MAX_OFFSET (60 * 60 * TCP_RETRANSHZ)
404 static void add_to_time_wait_locked(struct tcpcb
*tp
, uint32_t delay
);
405 static boolean_t
tcp_garbage_collect(struct inpcb
*, int);
407 #define TIMERENTRY_TO_TP(te) ((struct tcpcb *)((uintptr_t)te - offsetof(struct tcpcb, tentry.le.le_next)))
409 #define VERIFY_NEXT_LINK(elm, field) do { \
410 if (LIST_NEXT((elm),field) != NULL && \
411 LIST_NEXT((elm),field)->field.le_prev != \
412 &((elm)->field.le_next)) \
413 panic("Bad link elm %p next->prev != elm", (elm)); \
416 #define VERIFY_PREV_LINK(elm, field) do { \
417 if (*(elm)->field.le_prev != (elm)) \
418 panic("Bad link elm %p prev->next != elm", (elm)); \
421 #define TCP_SET_TIMER_MODE(mode, i) do { \
422 if (IS_TIMER_HZ_10MS(i)) \
423 (mode) |= TCP_TIMERLIST_10MS_MODE; \
424 else if (IS_TIMER_HZ_100MS(i)) \
425 (mode) |= TCP_TIMERLIST_100MS_MODE; \
427 (mode) |= TCP_TIMERLIST_500MS_MODE; \
430 #if (DEVELOPMENT || DEBUG)
431 SYSCTL_UINT(_net_inet_tcp
, OID_AUTO
, mss_rec_medium
,
432 CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_mss_rec_medium
, 0,
433 "Medium MSS based on recommendation in link status report");
434 SYSCTL_UINT(_net_inet_tcp
, OID_AUTO
, mss_rec_low
,
435 CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_mss_rec_low
, 0,
436 "Low MSS based on recommendation in link status report");
438 static int32_t tcp_change_mss_recommended
= 0;
440 sysctl_change_mss_recommended SYSCTL_HANDLER_ARGS
442 #pragma unused(oidp, arg1, arg2)
443 int i
, err
= 0, changed
= 0;
445 struct if_link_status ifsr
;
446 struct if_cellular_status_v1
*new_cell_sr
;
447 err
= sysctl_io_number(req
, tcp_change_mss_recommended
,
448 sizeof(int32_t), &i
, &changed
);
450 ifnet_head_lock_shared();
451 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
) {
452 if (IFNET_IS_CELLULAR(ifp
)) {
453 bzero(&ifsr
, sizeof(ifsr
));
454 new_cell_sr
= &ifsr
.ifsr_u
.ifsr_cell
.if_cell_u
.if_status_v1
;
455 ifsr
.ifsr_version
= IF_CELLULAR_STATUS_REPORT_CURRENT_VERSION
;
456 ifsr
.ifsr_len
= sizeof(*new_cell_sr
);
458 /* Set MSS recommended */
459 new_cell_sr
->valid_bitmask
|= IF_CELL_UL_MSS_RECOMMENDED_VALID
;
460 new_cell_sr
->mss_recommended
= i
;
461 err
= ifnet_link_status_report(ifp
, new_cell_sr
, sizeof(new_cell_sr
));
463 tcp_change_mss_recommended
= i
;
474 SYSCTL_PROC(_net_inet_tcp
, OID_AUTO
, change_mss_recommended
,
475 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_change_mss_recommended
,
476 0, sysctl_change_mss_recommended
, "IU", "Change MSS recommended");
478 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, report_stats_interval
,
479 CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_report_stats_interval
, 0,
480 "Report stats interval");
481 #endif /* (DEVELOPMENT || DEBUG) */
484 * Macro to compare two timers. If there is a reset of the sign bit,
485 * it is safe to assume that the timer has wrapped around. By doing
486 * signed comparision, we take care of wrap around such that the value
487 * with the sign bit reset is actually ahead of the other.
490 timer_diff(uint32_t t1
, uint32_t toff1
, uint32_t t2
, uint32_t toff2
)
492 return (int32_t)((t1
+ toff1
) - (t2
+ toff2
));
496 * Add to tcp timewait list, delay is given in milliseconds.
499 add_to_time_wait_locked(struct tcpcb
*tp
, uint32_t delay
)
501 struct inpcbinfo
*pcbinfo
= &tcbinfo
;
502 struct inpcb
*inp
= tp
->t_inpcb
;
505 /* pcb list should be locked when we get here */
506 LCK_RW_ASSERT(pcbinfo
->ipi_lock
, LCK_RW_ASSERT_EXCLUSIVE
);
508 /* We may get here multiple times, so check */
509 if (!(inp
->inp_flags2
& INP2_TIMEWAIT
)) {
510 pcbinfo
->ipi_twcount
++;
511 inp
->inp_flags2
|= INP2_TIMEWAIT
;
513 /* Remove from global inp list */
514 LIST_REMOVE(inp
, inp_list
);
516 TAILQ_REMOVE(&tcp_tw_tailq
, tp
, t_twentry
);
519 /* Compute the time at which this socket can be closed */
520 timer
= tcp_now
+ delay
;
522 /* We will use the TCPT_2MSL timer for tracking this delay */
524 if (TIMER_IS_ON_LIST(tp
)) {
525 tcp_remove_timer(tp
);
527 tp
->t_timer
[TCPT_2MSL
] = timer
;
529 TAILQ_INSERT_TAIL(&tcp_tw_tailq
, tp
, t_twentry
);
533 add_to_time_wait(struct tcpcb
*tp
, uint32_t delay
)
535 struct inpcbinfo
*pcbinfo
= &tcbinfo
;
536 if (tp
->t_inpcb
->inp_socket
->so_options
& SO_NOWAKEFROMSLEEP
) {
537 socket_post_kev_msg_closed(tp
->t_inpcb
->inp_socket
);
540 /* 19182803: Notify nstat that connection is closing before waiting. */
541 nstat_pcb_detach(tp
->t_inpcb
);
543 if (!lck_rw_try_lock_exclusive(pcbinfo
->ipi_lock
)) {
544 socket_unlock(tp
->t_inpcb
->inp_socket
, 0);
545 lck_rw_lock_exclusive(pcbinfo
->ipi_lock
);
546 socket_lock(tp
->t_inpcb
->inp_socket
, 0);
548 add_to_time_wait_locked(tp
, delay
);
549 lck_rw_done(pcbinfo
->ipi_lock
);
551 inpcb_gc_sched(pcbinfo
, INPCB_TIMER_LAZY
);
554 /* If this is on time wait queue, remove it. */
556 tcp_remove_from_time_wait(struct inpcb
*inp
)
558 struct tcpcb
*tp
= intotcpcb(inp
);
559 if (inp
->inp_flags2
& INP2_TIMEWAIT
) {
560 TAILQ_REMOVE(&tcp_tw_tailq
, tp
, t_twentry
);
565 tcp_garbage_collect(struct inpcb
*inp
, int istimewait
)
567 boolean_t active
= FALSE
;
568 struct socket
*so
, *mp_so
= NULL
;
571 so
= inp
->inp_socket
;
574 if (so
->so_flags
& SOF_MP_SUBFLOW
) {
575 mp_so
= mptetoso(tptomptp(tp
)->mpt_mpte
);
576 if (!socket_try_lock(mp_so
)) {
581 if (mpsotomppcb(mp_so
)->mpp_inside
> 0) {
582 os_log(mptcp_log_handle
, "%s - %lx: Still inside %d usecount %d\n", __func__
,
583 (unsigned long)VM_KERNEL_ADDRPERM(mpsotompte(mp_so
)),
584 mpsotomppcb(mp_so
)->mpp_inside
,
586 socket_unlock(mp_so
, 0);
591 /* We call socket_unlock with refcount further below */
592 mp_so
->so_usecount
++;
593 tptomptp(tp
)->mpt_mpte
->mpte_mppcb
->mpp_inside
++;
597 * Skip if still in use or busy; it would have been more efficient
598 * if we were to test so_usecount against 0, but this isn't possible
599 * due to the current implementation of tcp_dropdropablreq() where
600 * overflow sockets that are eligible for garbage collection have
601 * their usecounts set to 1.
603 if (!lck_mtx_try_lock_spin(&inp
->inpcb_mtx
)) {
608 /* Check again under the lock */
609 if (so
->so_usecount
> 1) {
610 if (inp
->inp_wantcnt
== WNT_STOPUSING
) {
613 lck_mtx_unlock(&inp
->inpcb_mtx
);
617 if (istimewait
&& TSTMP_GEQ(tcp_now
, tp
->t_timer
[TCPT_2MSL
]) &&
618 tp
->t_state
!= TCPS_CLOSED
) {
619 /* Become a regular mutex */
620 lck_mtx_convert_spin(&inp
->inpcb_mtx
);
625 * Overflowed socket dropped from the listening queue? Do this
626 * only if we are called to clean up the time wait slots, since
627 * tcp_dropdropablreq() considers a socket to have been fully
628 * dropped after add_to_time_wait() is finished.
629 * Also handle the case of connections getting closed by the peer
630 * while in the queue as seen with rdar://6422317
633 if (so
->so_usecount
== 1 &&
634 ((istimewait
&& (so
->so_flags
& SOF_OVERFLOW
)) ||
635 ((tp
!= NULL
) && (tp
->t_state
== TCPS_CLOSED
) &&
636 (so
->so_head
!= NULL
) &&
637 ((so
->so_state
& (SS_INCOMP
| SS_CANTSENDMORE
| SS_CANTRCVMORE
)) ==
638 (SS_INCOMP
| SS_CANTSENDMORE
| SS_CANTRCVMORE
))))) {
639 if (inp
->inp_state
!= INPCB_STATE_DEAD
) {
640 /* Become a regular mutex */
641 lck_mtx_convert_spin(&inp
->inpcb_mtx
);
643 if (SOCK_CHECK_DOM(so
, PF_INET6
)) {
649 VERIFY(so
->so_usecount
> 0);
651 if (inp
->inp_wantcnt
== WNT_STOPUSING
) {
654 lck_mtx_unlock(&inp
->inpcb_mtx
);
656 } else if (inp
->inp_wantcnt
!= WNT_STOPUSING
) {
657 lck_mtx_unlock(&inp
->inpcb_mtx
);
663 * We get here because the PCB is no longer searchable
664 * (WNT_STOPUSING); detach (if needed) and dispose if it is dead
665 * (usecount is 0). This covers all cases, including overflow
666 * sockets and those that are considered as "embryonic",
667 * i.e. created by sonewconn() in TCP input path, and have
668 * not yet been committed. For the former, we reduce the usecount
669 * to 0 as done by the code above. For the latter, the usecount
670 * would have reduced to 0 as part calling soabort() when the
671 * socket is dropped at the end of tcp_input().
673 if (so
->so_usecount
== 0) {
674 DTRACE_TCP4(state__change
, void, NULL
, struct inpcb
*, inp
,
675 struct tcpcb
*, tp
, int32_t, TCPS_CLOSED
);
676 /* Become a regular mutex */
677 lck_mtx_convert_spin(&inp
->inpcb_mtx
);
680 * If this tp still happens to be on the timer list,
683 if (TIMER_IS_ON_LIST(tp
)) {
684 tcp_remove_timer(tp
);
687 if (inp
->inp_state
!= INPCB_STATE_DEAD
) {
689 if (SOCK_CHECK_DOM(so
, PF_INET6
)) {
697 mptcp_subflow_del(tptomptp(tp
)->mpt_mpte
, tp
->t_mpsub
);
699 /* so is now unlinked from mp_so - let's drop the lock */
700 socket_unlock(mp_so
, 1);
709 lck_mtx_unlock(&inp
->inpcb_mtx
);
714 socket_unlock(mp_so
, 1);
721 * TCP garbage collector callback (inpcb_timer_func_t).
723 * Returns the number of pcbs that will need to be gc-ed soon,
724 * returnining > 0 will keep timer active.
727 tcp_gc(struct inpcbinfo
*ipi
)
729 struct inpcb
*inp
, *nxt
;
730 struct tcpcb
*tw_tp
, *tw_ntp
;
735 static int tws_checked
= 0;
738 KERNEL_DEBUG(DBG_FNC_TCP_SLOW
| DBG_FUNC_START
, 0, 0, 0, 0, 0);
741 * Update tcp_now here as it may get used while
742 * processing the slow timer.
744 calculate_tcp_clock();
747 * Garbage collect socket/tcpcb: We need to acquire the list lock
748 * exclusively to do this
751 if (lck_rw_try_lock_exclusive(ipi
->ipi_lock
) == FALSE
) {
752 /* don't sweat it this time; cleanup was done last time */
753 if (tcp_gc_done
== TRUE
) {
755 KERNEL_DEBUG(DBG_FNC_TCP_SLOW
| DBG_FUNC_END
,
756 tws_checked
, cur_tw_slot
, 0, 0, 0);
757 /* Lock upgrade failed, give up this round */
758 atomic_add_32(&ipi
->ipi_gc_req
.intimer_fast
, 1);
761 /* Upgrade failed, lost lock now take it again exclusive */
762 lck_rw_lock_exclusive(ipi
->ipi_lock
);
766 LIST_FOREACH_SAFE(inp
, &tcb
, inp_list
, nxt
) {
767 if (tcp_garbage_collect(inp
, 0)) {
768 atomic_add_32(&ipi
->ipi_gc_req
.intimer_fast
, 1);
772 /* Now cleanup the time wait ones */
773 TAILQ_FOREACH_SAFE(tw_tp
, &tcp_tw_tailq
, t_twentry
, tw_ntp
) {
775 * We check the timestamp here without holding the
776 * socket lock for better performance. If there are
777 * any pcbs in time-wait, the timer will get rescheduled.
778 * Hence some error in this check can be tolerated.
780 * Sometimes a socket on time-wait queue can be closed if
781 * 2MSL timer expired but the application still has a
784 if (tw_tp
->t_state
== TCPS_CLOSED
||
785 TSTMP_GEQ(tcp_now
, tw_tp
->t_timer
[TCPT_2MSL
])) {
786 if (tcp_garbage_collect(tw_tp
->t_inpcb
, 1)) {
787 atomic_add_32(&ipi
->ipi_gc_req
.intimer_lazy
, 1);
792 /* take into account pcbs that are still in time_wait_slots */
793 atomic_add_32(&ipi
->ipi_gc_req
.intimer_lazy
, ipi
->ipi_twcount
);
795 lck_rw_done(ipi
->ipi_lock
);
797 /* Clean up the socache while we are here */
798 if (so_cache_timer()) {
799 atomic_add_32(&ipi
->ipi_gc_req
.intimer_lazy
, 1);
802 KERNEL_DEBUG(DBG_FNC_TCP_SLOW
| DBG_FUNC_END
, tws_checked
,
803 cur_tw_slot
, 0, 0, 0);
809 * Cancel all timers for TCP tp.
812 tcp_canceltimers(struct tcpcb
*tp
)
816 tcp_remove_timer(tp
);
817 for (i
= 0; i
< TCPT_NTIMERS
; i
++) {
820 tp
->tentry
.timer_start
= tcp_now
;
821 tp
->tentry
.index
= TCPT_NONE
;
824 int tcp_syn_backoff
[TCP_MAXRXTSHIFT
+ 1] =
825 { 1, 1, 1, 1, 1, 2, 4, 8, 16, 32, 64, 64, 64 };
827 int tcp_backoff
[TCP_MAXRXTSHIFT
+ 1] =
828 { 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64 };
830 static int tcp_totbackoff
= 511; /* sum of tcp_backoff[] */
833 tcp_rexmt_save_state(struct tcpcb
*tp
)
836 if (TSTMP_SUPPORTED(tp
)) {
838 * Since timestamps are supported on the connection,
839 * we can do recovery as described in rfc 4015.
841 fsize
= tp
->snd_max
- tp
->snd_una
;
842 tp
->snd_ssthresh_prev
= max(fsize
, tp
->snd_ssthresh
);
843 tp
->snd_recover_prev
= tp
->snd_recover
;
846 * Timestamp option is not supported on this connection.
847 * Record ssthresh and cwnd so they can
848 * be recovered if this turns out to be a "bad" retransmit.
849 * A retransmit is considered "bad" if an ACK for this
850 * segment is received within RTT/2 interval; the assumption
851 * here is that the ACK was already in flight. See
852 * "On Estimating End-to-End Network Path Properties" by
853 * Allman and Paxson for more details.
855 tp
->snd_cwnd_prev
= tp
->snd_cwnd
;
856 tp
->snd_ssthresh_prev
= tp
->snd_ssthresh
;
857 tp
->snd_recover_prev
= tp
->snd_recover
;
858 if (IN_FASTRECOVERY(tp
)) {
859 tp
->t_flags
|= TF_WASFRECOVERY
;
861 tp
->t_flags
&= ~TF_WASFRECOVERY
;
864 tp
->t_srtt_prev
= (tp
->t_srtt
>> TCP_RTT_SHIFT
) + 2;
865 tp
->t_rttvar_prev
= (tp
->t_rttvar
>> TCP_RTTVAR_SHIFT
);
866 tp
->t_flagsext
&= ~(TF_RECOMPUTE_RTT
);
870 * Revert to the older segment size if there is an indication that PMTU
871 * blackhole detection was not needed.
874 tcp_pmtud_revert_segment_size(struct tcpcb
*tp
)
878 VERIFY(tp
->t_pmtud_saved_maxopd
> 0);
879 tp
->t_flags
|= TF_PMTUD
;
880 tp
->t_flags
&= ~TF_BLACKHOLE
;
881 optlen
= tp
->t_maxopd
- tp
->t_maxseg
;
882 tp
->t_maxopd
= tp
->t_pmtud_saved_maxopd
;
883 tp
->t_maxseg
= tp
->t_maxopd
- optlen
;
886 * Reset the slow-start flight size as it
887 * may depend on the new MSS
889 if (CC_ALGO(tp
)->cwnd_init
!= NULL
) {
890 CC_ALGO(tp
)->cwnd_init(tp
);
892 tp
->t_pmtud_start_ts
= 0;
893 tcpstat
.tcps_pmtudbh_reverted
++;
895 /* change MSS according to recommendation, if there was one */
896 tcp_update_mss_locked(tp
->t_inpcb
->inp_socket
, NULL
);
900 * TCP timer processing.
903 tcp_timers(struct tcpcb
*tp
, int timer
)
905 int32_t rexmt
, optlen
= 0, idle_time
= 0;
907 struct tcptemp
*t_template
;
913 int isipv6
= (tp
->t_inpcb
->inp_vflag
& INP_IPV4
) == 0;
915 u_int64_t accsleep_ms
;
916 u_int32_t last_sleep_ms
= 0;
918 so
= tp
->t_inpcb
->inp_socket
;
919 idle_time
= tcp_now
- tp
->t_rcvtime
;
923 * 2 MSL timeout in shutdown went off. If we're closed but
924 * still waiting for peer to close and connection has been idle
925 * too long, or if 2MSL time is up from TIME_WAIT or FIN_WAIT_2,
926 * delete connection control block.
927 * Otherwise, (this case shouldn't happen) check again in a bit
928 * we keep the socket in the main list in that case.
931 tcp_free_sackholes(tp
);
932 if (tp
->t_state
!= TCPS_TIME_WAIT
&&
933 tp
->t_state
!= TCPS_FIN_WAIT_2
&&
934 ((idle_time
> 0) && (idle_time
< TCP_CONN_MAXIDLE(tp
)))) {
935 tp
->t_timer
[TCPT_2MSL
] = OFFSET_FROM_START(tp
,
936 (u_int32_t
)TCP_CONN_KEEPINTVL(tp
));
944 * Retransmission timer went off. Message has not
945 * been acked within retransmit interval. Back off
946 * to a longer retransmit interval and retransmit one segment.
949 absolutetime_to_nanoseconds(mach_absolutetime_asleep
,
951 accsleep_ms
= accsleep_ms
/ 1000000UL;
952 if (accsleep_ms
> tp
->t_accsleep_ms
) {
953 last_sleep_ms
= accsleep_ms
- tp
->t_accsleep_ms
;
956 * Drop a connection in the retransmit timer
957 * 1. If we have retransmitted more than TCP_MAXRXTSHIFT
959 * 2. If the time spent in this retransmission episode is
960 * more than the time limit set with TCP_RXT_CONNDROPTIME
962 * 3. If TCP_RXT_FINDROP socket option was set and
963 * we have already retransmitted the FIN 3 times without
966 if (++tp
->t_rxtshift
> TCP_MAXRXTSHIFT
||
967 (tp
->t_rxt_conndroptime
> 0 && tp
->t_rxtstart
> 0 &&
968 (tcp_now
- tp
->t_rxtstart
) >= tp
->t_rxt_conndroptime
) ||
969 ((tp
->t_flagsext
& TF_RXTFINDROP
) != 0 &&
970 (tp
->t_flags
& TF_SENTFIN
) != 0 && tp
->t_rxtshift
>= 4) ||
971 (tp
->t_rxtshift
> 4 && last_sleep_ms
>= TCP_SLEEP_TOO_LONG
)) {
972 if (tp
->t_state
== TCPS_ESTABLISHED
&&
973 tp
->t_rxt_minimum_timeout
> 0) {
975 * Avoid dropping a connection if minimum
976 * timeout is set and that time did not
977 * pass. We will retry sending
978 * retransmissions at the maximum interval
980 if (TSTMP_LT(tcp_now
, (tp
->t_rxtstart
+
981 tp
->t_rxt_minimum_timeout
))) {
982 tp
->t_rxtshift
= TCP_MAXRXTSHIFT
- 1;
983 goto retransmit_packet
;
986 if ((tp
->t_flagsext
& TF_RXTFINDROP
) != 0) {
987 tcpstat
.tcps_rxtfindrop
++;
988 } else if (last_sleep_ms
>= TCP_SLEEP_TOO_LONG
) {
989 tcpstat
.tcps_drop_after_sleep
++;
991 tcpstat
.tcps_timeoutdrop
++;
993 if (tp
->t_rxtshift
>= TCP_MAXRXTSHIFT
) {
994 if (TCP_ECN_ENABLED(tp
)) {
995 INP_INC_IFNET_STAT(tp
->t_inpcb
,
998 INP_INC_IFNET_STAT(tp
->t_inpcb
,
1002 tp
->t_rxtshift
= TCP_MAXRXTSHIFT
;
1003 postevent(so
, 0, EV_TIMEOUT
);
1005 (SO_FILT_HINT_LOCKED
| SO_FILT_HINT_TIMEOUT
));
1007 if (TCP_ECN_ENABLED(tp
) &&
1008 tp
->t_state
== TCPS_ESTABLISHED
) {
1009 tcp_heuristic_ecn_droprxmt(tp
);
1012 tp
= tcp_drop(tp
, tp
->t_softerror
?
1013 tp
->t_softerror
: ETIMEDOUT
);
1018 tcpstat
.tcps_rexmttimeo
++;
1019 tp
->t_accsleep_ms
= accsleep_ms
;
1021 if (tp
->t_rxtshift
== 1 &&
1022 tp
->t_state
== TCPS_ESTABLISHED
) {
1023 /* Set the time at which retransmission started. */
1024 tp
->t_rxtstart
= tcp_now
;
1027 * if this is the first retransmit timeout, save
1028 * the state so that we can recover if the timeout
1031 tcp_rexmt_save_state(tp
);
1032 tcp_ccdbg_trace(tp
, NULL
, TCP_CC_FIRST_REXMT
);
1035 if ((tp
->t_rxtshift
>= mptcp_fail_thresh
) &&
1036 (tp
->t_state
== TCPS_ESTABLISHED
) &&
1037 (tp
->t_mpflags
& TMPF_MPTCP_TRUE
)) {
1038 mptcp_act_on_txfail(so
);
1041 if (TCPS_HAVEESTABLISHED(tp
->t_state
) &&
1042 (so
->so_flags
& SOF_MP_SUBFLOW
)) {
1043 struct mptses
*mpte
= tptomptp(tp
)->mpt_mpte
;
1045 if (mpte
->mpte_svctype
== MPTCP_SVCTYPE_HANDOVER
) {
1046 mptcp_check_subflows_and_add(mpte
);
1051 if (tp
->t_adaptive_wtimo
> 0 &&
1052 tp
->t_rxtshift
> tp
->t_adaptive_wtimo
&&
1053 TCPS_HAVEESTABLISHED(tp
->t_state
)) {
1054 /* Send an event to the application */
1056 (SO_FILT_HINT_LOCKED
|
1057 SO_FILT_HINT_ADAPTIVE_WTIMO
));
1061 * If this is a retransmit timeout after PTO, the PTO
1064 if (tp
->t_flagsext
& TF_SENT_TLPROBE
) {
1065 tp
->t_flagsext
&= ~(TF_SENT_TLPROBE
);
1066 tcpstat
.tcps_rto_after_pto
++;
1069 if (tp
->t_flagsext
& TF_DELAY_RECOVERY
) {
1071 * Retransmit timer fired before entering recovery
1072 * on a connection with packet re-ordering. This
1073 * suggests that the reordering metrics computed
1076 tp
->t_reorderwin
= 0;
1077 tp
->t_timer
[TCPT_DELAYFR
] = 0;
1078 tp
->t_flagsext
&= ~(TF_DELAY_RECOVERY
);
1081 if (!(tp
->t_flagsext
& TF_FASTOPEN_FORCE_ENABLE
) &&
1082 tp
->t_state
== TCPS_SYN_RECEIVED
) {
1083 tcp_disable_tfo(tp
);
1086 if (!(tp
->t_flagsext
& TF_FASTOPEN_FORCE_ENABLE
) &&
1087 !(tp
->t_tfo_flags
& TFO_F_HEURISTIC_DONE
) &&
1088 (tp
->t_tfo_stats
& TFO_S_SYN_DATA_SENT
) &&
1089 !(tp
->t_tfo_flags
& TFO_F_NO_SNDPROBING
) &&
1090 ((tp
->t_state
!= TCPS_SYN_SENT
&& tp
->t_rxtshift
> 1) ||
1091 tp
->t_rxtshift
> 4)) {
1093 * For regular retransmissions, a first one is being
1094 * done for tail-loss probe.
1095 * Thus, if rxtshift > 1, this means we have sent the segment
1096 * a total of 3 times.
1098 * If we are in SYN-SENT state, then there is no tail-loss
1099 * probe thus we have to let rxtshift go up to 3.
1101 tcp_heuristic_tfo_middlebox(tp
);
1103 so
->so_error
= ENODATA
;
1105 (SO_FILT_HINT_LOCKED
| SO_FILT_HINT_MP_SUB_ERROR
));
1109 tp
->t_tfo_stats
|= TFO_S_SEND_BLACKHOLE
;
1110 tcpstat
.tcps_tfo_sndblackhole
++;
1113 if (!(tp
->t_flagsext
& TF_FASTOPEN_FORCE_ENABLE
) &&
1114 !(tp
->t_tfo_flags
& TFO_F_HEURISTIC_DONE
) &&
1115 (tp
->t_tfo_stats
& TFO_S_SYN_DATA_ACKED
) &&
1116 tp
->t_rxtshift
> 3) {
1117 if (TSTMP_GT(tp
->t_sndtime
- 10 * TCP_RETRANSHZ
, tp
->t_rcvtime
)) {
1118 tcp_heuristic_tfo_middlebox(tp
);
1120 so
->so_error
= ENODATA
;
1122 (SO_FILT_HINT_LOCKED
| SO_FILT_HINT_MP_SUB_ERROR
));
1128 if (tp
->t_state
== TCPS_SYN_SENT
) {
1129 rexmt
= TCP_REXMTVAL(tp
) * tcp_syn_backoff
[tp
->t_rxtshift
];
1130 tp
->t_stat
.synrxtshift
= tp
->t_rxtshift
;
1131 tp
->t_stat
.rxmitsyns
++;
1133 /* When retransmitting, disable TFO */
1134 if (tfo_enabled(tp
) &&
1135 !(tp
->t_flagsext
& TF_FASTOPEN_FORCE_ENABLE
)) {
1136 tcp_disable_tfo(tp
);
1137 tp
->t_tfo_flags
|= TFO_F_SYN_LOSS
;
1140 rexmt
= TCP_REXMTVAL(tp
) * tcp_backoff
[tp
->t_rxtshift
];
1143 TCPT_RANGESET(tp
->t_rxtcur
, rexmt
, tp
->t_rttmin
, TCPTV_REXMTMAX
,
1144 TCP_ADD_REXMTSLOP(tp
));
1145 tp
->t_timer
[TCPT_REXMT
] = OFFSET_FROM_START(tp
, tp
->t_rxtcur
);
1147 TCP_LOG_RTT_INFO(tp
);
1149 if (INP_WAIT_FOR_IF_FEEDBACK(tp
->t_inpcb
)) {
1153 tcp_free_sackholes(tp
);
1155 * Check for potential Path MTU Discovery Black Hole
1157 if (tcp_pmtud_black_hole_detect
&&
1158 !(tp
->t_flagsext
& TF_NOBLACKHOLE_DETECTION
) &&
1159 (tp
->t_state
== TCPS_ESTABLISHED
)) {
1160 if ((tp
->t_flags
& TF_PMTUD
) &&
1161 ((tp
->t_flags
& TF_MAXSEGSNT
)
1162 || tp
->t_pmtud_lastseg_size
> tcp_pmtud_black_hole_mss
) &&
1163 tp
->t_rxtshift
== 2) {
1165 * Enter Path MTU Black-hole Detection mechanism:
1166 * - Disable Path MTU Discovery (IP "DF" bit).
1167 * - Reduce MTU to lower value than what we
1168 * negotiated with the peer.
1170 /* Disable Path MTU Discovery for now */
1171 tp
->t_flags
&= ~TF_PMTUD
;
1172 /* Record that we may have found a black hole */
1173 tp
->t_flags
|= TF_BLACKHOLE
;
1174 optlen
= tp
->t_maxopd
- tp
->t_maxseg
;
1175 /* Keep track of previous MSS */
1176 tp
->t_pmtud_saved_maxopd
= tp
->t_maxopd
;
1177 tp
->t_pmtud_start_ts
= tcp_now
;
1178 if (tp
->t_pmtud_start_ts
== 0) {
1179 tp
->t_pmtud_start_ts
++;
1181 /* Reduce the MSS to intermediary value */
1182 if (tp
->t_maxopd
> tcp_pmtud_black_hole_mss
) {
1183 tp
->t_maxopd
= tcp_pmtud_black_hole_mss
;
1185 tp
->t_maxopd
= /* use the default MSS */
1187 isipv6
? tcp_v6mssdflt
:
1191 tp
->t_maxseg
= tp
->t_maxopd
- optlen
;
1194 * Reset the slow-start flight size
1195 * as it may depend on the new MSS
1197 if (CC_ALGO(tp
)->cwnd_init
!= NULL
) {
1198 CC_ALGO(tp
)->cwnd_init(tp
);
1200 tp
->snd_cwnd
= tp
->t_maxseg
;
1203 * If further retransmissions are still
1204 * unsuccessful with a lowered MTU, maybe this
1205 * isn't a Black Hole and we restore the previous
1206 * MSS and blackhole detection flags.
1209 if ((tp
->t_flags
& TF_BLACKHOLE
) &&
1210 (tp
->t_rxtshift
> 4)) {
1211 tcp_pmtud_revert_segment_size(tp
);
1212 tp
->snd_cwnd
= tp
->t_maxseg
;
1219 * Disable rfc1323 and rfc1644 if we haven't got any
1220 * response to our SYN (after we reach the threshold)
1221 * to work-around some broken terminal servers (most of
1222 * which have hopefully been retired) that have bad VJ
1223 * header compression code which trashes TCP segments
1224 * containing unknown-to-them TCP options.
1225 * Do this only on non-local connections.
1227 if (tp
->t_state
== TCPS_SYN_SENT
&&
1228 tp
->t_rxtshift
== tcp_broken_peer_syn_rxmit_thres
) {
1229 tp
->t_flags
&= ~(TF_REQ_SCALE
| TF_REQ_TSTMP
| TF_REQ_CC
);
1233 * If losing, let the lower level know and try for
1234 * a better route. Also, if we backed off this far,
1235 * our srtt estimate is probably bogus. Clobber it
1236 * so we'll take the next rtt measurement as our srtt;
1237 * move the current srtt into rttvar to keep the current
1238 * retransmit times until then.
1240 if (tp
->t_rxtshift
> TCP_MAXRXTSHIFT
/ 4) {
1243 in6_losing(tp
->t_inpcb
);
1246 in_losing(tp
->t_inpcb
);
1247 tp
->t_rttvar
+= (tp
->t_srtt
>> TCP_RTT_SHIFT
);
1250 tp
->snd_nxt
= tp
->snd_una
;
1252 * Note: We overload snd_recover to function also as the
1253 * snd_last variable described in RFC 2582
1255 tp
->snd_recover
= tp
->snd_max
;
1257 * Force a segment to be sent.
1259 tp
->t_flags
|= TF_ACKNOW
;
1261 /* If timing a segment in this window, stop the timer */
1264 if (!IN_FASTRECOVERY(tp
) && tp
->t_rxtshift
== 1) {
1265 tcpstat
.tcps_tailloss_rto
++;
1270 * RFC 5681 says: when a TCP sender detects segment loss
1271 * using retransmit timer and the given segment has already
1272 * been retransmitted by way of the retransmission timer at
1273 * least once, the value of ssthresh is held constant
1275 if (tp
->t_rxtshift
== 1 &&
1276 CC_ALGO(tp
)->after_timeout
!= NULL
) {
1277 CC_ALGO(tp
)->after_timeout(tp
);
1279 * CWR notifications are to be sent on new data
1280 * right after Fast Retransmits and ECE
1281 * notification receipts.
1283 if (TCP_ECN_ENABLED(tp
)) {
1284 tp
->ecn_flags
|= TE_SENDCWR
;
1288 EXIT_FASTRECOVERY(tp
);
1290 /* Exit cwnd non validated phase */
1291 tp
->t_flagsext
&= ~TF_CWND_NONVALIDATED
;
1295 tcp_ccdbg_trace(tp
, NULL
, TCP_CC_REXMT_TIMEOUT
);
1297 (void) tcp_output(tp
);
1301 * Persistance timer into zero window.
1302 * Force a byte to be output, if possible.
1305 tcpstat
.tcps_persisttimeo
++;
1307 * Hack: if the peer is dead/unreachable, we do not
1308 * time out if the window is closed. After a full
1309 * backoff, drop the connection if the idle time
1310 * (no responses to probes) reaches the maximum
1311 * backoff that we would use if retransmitting.
1313 * Drop the connection if we reached the maximum allowed time for
1314 * Zero Window Probes without a non-zero update from the peer.
1315 * See rdar://5805356
1317 if ((tp
->t_rxtshift
== TCP_MAXRXTSHIFT
&&
1318 (idle_time
>= tcp_maxpersistidle
||
1319 idle_time
>= TCP_REXMTVAL(tp
) * tcp_totbackoff
)) ||
1320 ((tp
->t_persist_stop
!= 0) &&
1321 TSTMP_LEQ(tp
->t_persist_stop
, tcp_now
))) {
1322 tcpstat
.tcps_persistdrop
++;
1323 postevent(so
, 0, EV_TIMEOUT
);
1325 (SO_FILT_HINT_LOCKED
| SO_FILT_HINT_TIMEOUT
));
1326 tp
= tcp_drop(tp
, ETIMEDOUT
);
1330 tp
->t_flagsext
|= TF_FORCE
;
1331 (void) tcp_output(tp
);
1332 tp
->t_flagsext
&= ~TF_FORCE
;
1336 * Keep-alive timer went off; send something
1337 * or drop connection if idle for too long.
1340 tcpstat
.tcps_keeptimeo
++;
1343 * Regular TCP connections do not send keepalives after closing
1344 * MPTCP must not also, after sending Data FINs.
1346 struct mptcb
*mp_tp
= tptomptp(tp
);
1347 if ((tp
->t_mpflags
& TMPF_MPTCP_TRUE
) &&
1348 (tp
->t_state
> TCPS_ESTABLISHED
)) {
1350 } else if (mp_tp
!= NULL
) {
1351 if ((mptcp_ok_to_keepalive(mp_tp
) == 0)) {
1356 if (tp
->t_state
< TCPS_ESTABLISHED
) {
1359 if ((always_keepalive
||
1360 (tp
->t_inpcb
->inp_socket
->so_options
& SO_KEEPALIVE
) ||
1361 (tp
->t_flagsext
& TF_DETECT_READSTALL
) ||
1362 (tp
->t_tfo_probe_state
== TFO_PROBE_PROBING
)) &&
1363 (tp
->t_state
<= TCPS_CLOSING
|| tp
->t_state
== TCPS_FIN_WAIT_2
)) {
1364 if (idle_time
>= TCP_CONN_KEEPIDLE(tp
) + TCP_CONN_MAXIDLE(tp
)) {
1368 * Send a packet designed to force a response
1369 * if the peer is up and reachable:
1370 * either an ACK if the connection is still alive,
1371 * or an RST if the peer has closed the connection
1372 * due to timeout or reboot.
1373 * Using sequence number tp->snd_una-1
1374 * causes the transmitted zero-length segment
1375 * to lie outside the receive window;
1376 * by the protocol spec, this requires the
1377 * correspondent TCP to respond.
1379 tcpstat
.tcps_keepprobe
++;
1380 t_template
= tcp_maketemplate(tp
);
1382 struct inpcb
*inp
= tp
->t_inpcb
;
1383 struct tcp_respond_args tra
;
1385 bzero(&tra
, sizeof(tra
));
1386 tra
.nocell
= INP_NO_CELLULAR(inp
);
1387 tra
.noexpensive
= INP_NO_EXPENSIVE(inp
);
1388 tra
.noconstrained
= INP_NO_CONSTRAINED(inp
);
1389 tra
.awdl_unrestricted
= INP_AWDL_UNRESTRICTED(inp
);
1390 tra
.intcoproc_allowed
= INP_INTCOPROC_ALLOWED(inp
);
1392 if (tp
->t_inpcb
->inp_flags
& INP_BOUND_IF
) {
1393 tra
.ifscope
= tp
->t_inpcb
->inp_boundifp
->if_index
;
1395 tra
.ifscope
= IFSCOPE_NONE
;
1397 tcp_respond(tp
, t_template
->tt_ipgen
,
1398 &t_template
->tt_t
, (struct mbuf
*)NULL
,
1399 tp
->rcv_nxt
, tp
->snd_una
- 1, 0, &tra
);
1400 (void) m_free(dtom(t_template
));
1401 if (tp
->t_flagsext
& TF_DETECT_READSTALL
) {
1402 tp
->t_rtimo_probes
++;
1406 TCP_LOG_KEEP_ALIVE(tp
, idle_time
);
1408 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
,
1409 TCP_CONN_KEEPINTVL(tp
));
1411 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
,
1412 TCP_CONN_KEEPIDLE(tp
));
1414 if (tp
->t_flagsext
& TF_DETECT_READSTALL
) {
1415 struct ifnet
*outifp
= tp
->t_inpcb
->inp_last_outifp
;
1416 bool reenable_probe
= false;
1418 * The keep alive packets sent to detect a read
1419 * stall did not get a response from the
1420 * peer. Generate more keep-alives to confirm this.
1421 * If the number of probes sent reaches the limit,
1422 * generate an event.
1424 if (tp
->t_adaptive_rtimo
> 0) {
1425 if (tp
->t_rtimo_probes
> tp
->t_adaptive_rtimo
) {
1426 /* Generate an event */
1428 (SO_FILT_HINT_LOCKED
|
1429 SO_FILT_HINT_ADAPTIVE_RTIMO
));
1430 tcp_keepalive_reset(tp
);
1432 reenable_probe
= true;
1434 } else if (outifp
!= NULL
&&
1435 (outifp
->if_eflags
& IFEF_PROBE_CONNECTIVITY
) &&
1436 tp
->t_rtimo_probes
<= TCP_CONNECTIVITY_PROBES_MAX
) {
1437 reenable_probe
= true;
1439 tp
->t_flagsext
&= ~TF_DETECT_READSTALL
;
1441 if (reenable_probe
) {
1442 int ind
= min(tp
->t_rtimo_probes
,
1444 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(
1445 tp
, tcp_backoff
[ind
] * TCP_REXMTVAL(tp
));
1448 if (tp
->t_tfo_probe_state
== TFO_PROBE_PROBING
) {
1452 ind
= min(tp
->t_tfo_probes
, TCP_MAXRXTSHIFT
);
1455 * We take the minimum among the time set by true
1456 * keepalive (see above) and the backoff'd RTO. That
1457 * way we backoff in case of packet-loss but will never
1458 * timeout slower than regular keepalive due to the
1461 tp
->t_timer
[TCPT_KEEP
] = min(OFFSET_FROM_START(
1462 tp
, tcp_backoff
[ind
] * TCP_REXMTVAL(tp
)),
1463 tp
->t_timer
[TCPT_KEEP
]);
1464 } else if (!(tp
->t_flagsext
& TF_FASTOPEN_FORCE_ENABLE
) &&
1465 !(tp
->t_tfo_flags
& TFO_F_HEURISTIC_DONE
) &&
1466 tp
->t_tfo_probe_state
== TFO_PROBE_WAIT_DATA
) {
1467 /* Still no data! Let's assume a TFO-error and err out... */
1468 tcp_heuristic_tfo_middlebox(tp
);
1470 so
->so_error
= ENODATA
;
1472 (SO_FILT_HINT_LOCKED
| SO_FILT_HINT_MP_SUB_ERROR
));
1474 tp
->t_tfo_stats
|= TFO_S_RECV_BLACKHOLE
;
1475 tcpstat
.tcps_tfo_blackhole
++;
1479 if (tcp_delack_enabled
&& (tp
->t_flags
& TF_DELACK
)) {
1480 tp
->t_flags
&= ~TF_DELACK
;
1481 tp
->t_timer
[TCPT_DELACK
] = 0;
1482 tp
->t_flags
|= TF_ACKNOW
;
1485 * If delayed ack timer fired while stretching
1486 * acks, count the number of times the streaming
1487 * detection was not correct. If this exceeds a
1488 * threshold, disable strech ack on this
1491 * Also, go back to acking every other packet.
1493 if ((tp
->t_flags
& TF_STRETCHACK
)) {
1494 if (tp
->t_unacksegs
> 1 &&
1495 tp
->t_unacksegs
< maxseg_unacked
) {
1496 tp
->t_stretchack_delayed
++;
1499 if (tp
->t_stretchack_delayed
>
1500 TCP_STRETCHACK_DELAY_THRESHOLD
) {
1501 tp
->t_flagsext
|= TF_DISABLE_STRETCHACK
;
1503 * Note the time at which stretch
1504 * ack was disabled automatically
1506 tp
->rcv_nostrack_ts
= tcp_now
;
1507 tcpstat
.tcps_nostretchack
++;
1508 tp
->t_stretchack_delayed
= 0;
1509 tp
->rcv_nostrack_pkts
= 0;
1511 tcp_reset_stretch_ack(tp
);
1515 * If we are measuring inter packet arrival jitter
1516 * for throttling a connection, this delayed ack
1517 * might be the reason for accumulating some
1518 * jitter. So let's restart the measurement.
1520 CLEAR_IAJ_STATE(tp
);
1522 tcpstat
.tcps_delack
++;
1523 (void) tcp_output(tp
);
1528 case TCPT_JACK_RXMT
:
1529 if ((tp
->t_state
== TCPS_ESTABLISHED
) &&
1530 (tp
->t_mpflags
& TMPF_PREESTABLISHED
) &&
1531 (tp
->t_mpflags
& TMPF_JOINED_FLOW
)) {
1532 if (++tp
->t_mprxtshift
> TCP_MAXRXTSHIFT
) {
1533 tcpstat
.tcps_timeoutdrop
++;
1534 postevent(so
, 0, EV_TIMEOUT
);
1536 (SO_FILT_HINT_LOCKED
|
1537 SO_FILT_HINT_TIMEOUT
));
1538 tp
= tcp_drop(tp
, tp
->t_softerror
?
1539 tp
->t_softerror
: ETIMEDOUT
);
1542 tcpstat
.tcps_join_rxmts
++;
1543 tp
->t_mpflags
|= TMPF_SND_JACK
;
1544 tp
->t_flags
|= TF_ACKNOW
;
1547 * No backoff is implemented for simplicity for this
1550 (void) tcp_output(tp
);
1559 if (!(tp
->t_flagsext
& TF_IF_PROBING
)) {
1560 tp
->t_flagsext
&= ~(TF_SENT_TLPROBE
);
1563 * Check if the connection is in the right state to
1566 if ((tp
->t_state
!= TCPS_ESTABLISHED
||
1567 tp
->t_rxtshift
> 0 ||
1568 tp
->snd_max
== tp
->snd_una
||
1569 !SACK_ENABLED(tp
) ||
1570 !TAILQ_EMPTY(&tp
->snd_holes
) ||
1571 IN_FASTRECOVERY(tp
)) &&
1572 !(tp
->t_flagsext
& TF_IF_PROBING
)) {
1577 * When the interface state is changed explicitly reset the retransmission
1578 * timer state for both SYN and data packets because we do not want to
1579 * wait unnecessarily or timeout too quickly if the link characteristics
1580 * have changed drastically
1582 if (tp
->t_flagsext
& TF_IF_PROBING
) {
1584 if (tp
->t_state
== TCPS_SYN_SENT
) {
1585 tp
->t_stat
.synrxtshift
= tp
->t_rxtshift
;
1588 * Reset to the the default RTO
1590 tp
->t_srtt
= TCPTV_SRTTBASE
;
1592 ((TCPTV_RTOBASE
- TCPTV_SRTTBASE
) << TCP_RTTVAR_SHIFT
) / 4;
1593 tp
->t_rttmin
= tp
->t_flags
& TF_LOCAL
? tcp_TCPTV_MIN
:
1595 TCPT_RANGESET(tp
->t_rxtcur
, TCP_REXMTVAL(tp
),
1596 tp
->t_rttmin
, TCPTV_REXMTMAX
, TCP_ADD_REXMTSLOP(tp
));
1597 TCP_LOG_RTT_INFO(tp
);
1600 if (tp
->t_state
== TCPS_SYN_SENT
) {
1602 * The PTO for SYN_SENT reinitializes TCP as if it was a fresh
1603 * connection attempt
1605 tp
->snd_nxt
= tp
->snd_una
;
1607 * Note: We overload snd_recover to function also as the
1608 * snd_last variable described in RFC 2582
1610 tp
->snd_recover
= tp
->snd_max
;
1612 * Force a segment to be sent.
1614 tp
->t_flags
|= TF_ACKNOW
;
1616 /* If timing a segment in this window, stop the timer */
1622 * If there is no new data to send or if the
1623 * connection is limited by receive window then
1624 * retransmit the last segment, otherwise send
1627 snd_len
= min(so
->so_snd
.sb_cc
, tp
->snd_wnd
)
1628 - (tp
->snd_max
- tp
->snd_una
);
1630 tp
->snd_nxt
= tp
->snd_max
;
1632 snd_len
= min((tp
->snd_max
- tp
->snd_una
),
1634 tp
->snd_nxt
= tp
->snd_max
- snd_len
;
1639 if (tp
->t_flagsext
& TF_IF_PROBING
) {
1640 tcpstat
.tcps_probe_if
++;
1643 /* If timing a segment in this window, stop the timer */
1645 /* Note that tail loss probe is being sent. Exclude IF probe */
1646 if (!(tp
->t_flagsext
& TF_IF_PROBING
)) {
1647 tp
->t_flagsext
|= TF_SENT_TLPROBE
;
1648 tp
->t_tlpstart
= tcp_now
;
1651 tp
->snd_cwnd
+= tp
->t_maxseg
;
1653 * When tail-loss-probe fires, we reset the RTO timer, because
1654 * a probe just got sent, so we are good to push out the timer.
1656 * Set to 0 to ensure that tcp_output() will reschedule it
1658 tp
->t_timer
[TCPT_REXMT
] = 0;
1659 ret
= tcp_output(tp
);
1661 #if (DEBUG || DEVELOPMENT)
1662 if ((tp
->t_flagsext
& TF_IF_PROBING
) &&
1663 ((IFNET_IS_COMPANION_LINK(tp
->t_inpcb
->inp_last_outifp
)) ||
1664 tp
->t_state
== TCPS_SYN_SENT
)) {
1665 if (ret
== 0 && tcp_probe_if_fix_port
> 0 &&
1666 tcp_probe_if_fix_port
<= IPPORT_HILASTAUTO
) {
1667 tp
->t_timer
[TCPT_REXMT
] = 0;
1668 tcp_set_lotimer_index(tp
);
1671 os_log(OS_LOG_DEFAULT
,
1672 "%s: sent %s probe for %u > %u on interface %s"
1675 tp
->t_state
== TCPS_SYN_SENT
? "SYN" : "data",
1676 ntohs(tp
->t_inpcb
->inp_lport
),
1677 ntohs(tp
->t_inpcb
->inp_fport
),
1678 if_name(tp
->t_inpcb
->inp_last_outifp
),
1679 tp
->t_inpcb
->inp_last_outifp
->if_index
,
1680 ret
== 0 ? "succeeded" :"failed", ret
);
1682 #endif /* DEBUG || DEVELOPMENT */
1685 * When the connection is not idle, make sure the retransmission timer
1686 * is armed because it was set to zero above
1688 if ((tp
->t_timer
[TCPT_REXMT
] == 0 || tp
->t_timer
[TCPT_PERSIST
] == 0) &&
1689 (tp
->t_inpcb
->inp_socket
->so_snd
.sb_cc
!= 0 || tp
->t_state
== TCPS_SYN_SENT
||
1690 tp
->t_state
== TCPS_SYN_RECEIVED
)) {
1691 tp
->t_timer
[TCPT_REXMT
] =
1692 OFFSET_FROM_START(tp
, tp
->t_rxtcur
);
1694 os_log(OS_LOG_DEFAULT
,
1695 "%s: tcp_output() returned %u with retransmission timer disabled "
1696 "for %u > %u in state %d, reset timer to %d",
1698 ntohs(tp
->t_inpcb
->inp_lport
),
1699 ntohs(tp
->t_inpcb
->inp_fport
),
1701 tp
->t_timer
[TCPT_REXMT
]);
1703 tcp_check_timer_state(tp
);
1705 tp
->snd_cwnd
-= tp
->t_maxseg
;
1707 if (!(tp
->t_flagsext
& TF_IF_PROBING
)) {
1708 tp
->t_tlphighrxt
= tp
->snd_nxt
;
1713 tp
->t_flagsext
&= ~TF_DELAY_RECOVERY
;
1716 * Don't do anything if one of the following is true:
1717 * - the connection is already in recovery
1718 * - sequence until snd_recover has been acknowledged.
1719 * - retransmit timeout has fired
1721 if (IN_FASTRECOVERY(tp
) ||
1722 SEQ_GEQ(tp
->snd_una
, tp
->snd_recover
) ||
1723 tp
->t_rxtshift
> 0) {
1727 VERIFY(SACK_ENABLED(tp
));
1728 tcp_rexmt_save_state(tp
);
1729 if (CC_ALGO(tp
)->pre_fr
!= NULL
) {
1730 CC_ALGO(tp
)->pre_fr(tp
);
1731 if (TCP_ECN_ENABLED(tp
)) {
1732 tp
->ecn_flags
|= TE_SENDCWR
;
1735 ENTER_FASTRECOVERY(tp
);
1737 tp
->t_timer
[TCPT_REXMT
] = 0;
1738 tcpstat
.tcps_sack_recovery_episode
++;
1739 tp
->t_sack_recovery_episode
++;
1740 tp
->sack_newdata
= tp
->snd_nxt
;
1741 tp
->snd_cwnd
= tp
->t_maxseg
;
1742 tcp_ccdbg_trace(tp
, NULL
, TCP_CC_ENTER_FASTRECOVERY
);
1743 (void) tcp_output(tp
);
1746 tcpstat
.tcps_keepdrops
++;
1747 postevent(so
, 0, EV_TIMEOUT
);
1749 (SO_FILT_HINT_LOCKED
| SO_FILT_HINT_TIMEOUT
));
1750 tp
= tcp_drop(tp
, ETIMEDOUT
);
1754 if (tp
->t_inpcb
->inp_socket
->so_options
& SO_DEBUG
) {
1755 tcp_trace(TA_USER
, ostate
, tp
, (void *)0, (struct tcphdr
*)0,
1762 /* Remove a timer entry from timer list */
1764 tcp_remove_timer(struct tcpcb
*tp
)
1766 struct tcptimerlist
*listp
= &tcp_timer_list
;
1768 socket_lock_assert_owned(tp
->t_inpcb
->inp_socket
);
1769 if (!(TIMER_IS_ON_LIST(tp
))) {
1772 lck_mtx_lock(listp
->mtx
);
1774 /* Check if pcb is on timer list again after acquiring the lock */
1775 if (!(TIMER_IS_ON_LIST(tp
))) {
1776 lck_mtx_unlock(listp
->mtx
);
1780 if (listp
->next_te
!= NULL
&& listp
->next_te
== &tp
->tentry
) {
1781 listp
->next_te
= LIST_NEXT(&tp
->tentry
, le
);
1784 LIST_REMOVE(&tp
->tentry
, le
);
1785 tp
->t_flags
&= ~(TF_TIMER_ONLIST
);
1789 tp
->tentry
.le
.le_next
= NULL
;
1790 tp
->tentry
.le
.le_prev
= NULL
;
1791 lck_mtx_unlock(listp
->mtx
);
1795 * Function to check if the timerlist needs to be rescheduled to run
1796 * the timer entry correctly. Basically, this is to check if we can avoid
1797 * taking the list lock.
1801 need_to_resched_timerlist(u_int32_t runtime
, u_int16_t mode
)
1803 struct tcptimerlist
*listp
= &tcp_timer_list
;
1807 * If the list is being processed then the state of the list is
1808 * in flux. In this case always acquire the lock and set the state
1811 if (listp
->running
) {
1815 if (!listp
->scheduled
) {
1819 diff
= timer_diff(listp
->runtime
, 0, runtime
, 0);
1821 /* The list is going to run before this timer */
1824 if (mode
& TCP_TIMERLIST_10MS_MODE
) {
1825 if (diff
<= TCP_TIMER_10MS_QUANTUM
) {
1828 } else if (mode
& TCP_TIMERLIST_100MS_MODE
) {
1829 if (diff
<= TCP_TIMER_100MS_QUANTUM
) {
1833 if (diff
<= TCP_TIMER_500MS_QUANTUM
) {
1842 tcp_sched_timerlist(uint32_t offset
)
1844 uint64_t deadline
= 0;
1845 struct tcptimerlist
*listp
= &tcp_timer_list
;
1847 LCK_MTX_ASSERT(listp
->mtx
, LCK_MTX_ASSERT_OWNED
);
1849 offset
= min(offset
, TCP_TIMERLIST_MAX_OFFSET
);
1850 listp
->runtime
= tcp_now
+ offset
;
1851 listp
->schedtime
= tcp_now
;
1852 if (listp
->runtime
== 0) {
1857 clock_interval_to_deadline(offset
, USEC_PER_SEC
, &deadline
);
1859 thread_call_enter_delayed(listp
->call
, deadline
);
1860 listp
->scheduled
= TRUE
;
1864 * Function to run the timers for a connection.
1866 * Returns the offset of next timer to be run for this connection which
1867 * can be used to reschedule the timerlist.
1869 * te_mode is an out parameter that indicates the modes of active
1870 * timers for this connection.
1873 tcp_run_conn_timer(struct tcpcb
*tp
, u_int16_t
*te_mode
,
1874 u_int16_t probe_if_index
)
1877 u_int16_t i
= 0, index
= TCPT_NONE
, lo_index
= TCPT_NONE
;
1878 u_int32_t timer_val
, offset
= 0, lo_timer
= 0;
1880 boolean_t needtorun
[TCPT_NTIMERS
];
1884 bzero(needtorun
, sizeof(needtorun
));
1887 socket_lock(tp
->t_inpcb
->inp_socket
, 1);
1889 so
= tp
->t_inpcb
->inp_socket
;
1890 /* Release the want count on inp */
1891 if (in_pcb_checkstate(tp
->t_inpcb
, WNT_RELEASE
, 1)
1893 if (TIMER_IS_ON_LIST(tp
)) {
1894 tcp_remove_timer(tp
);
1897 /* Looks like the TCP connection got closed while we
1898 * were waiting for the lock.. Done
1904 * If this connection is over an interface that needs to
1905 * be probed, send probe packets to reinitiate communication.
1907 if (TCP_IF_STATE_CHANGED(tp
, probe_if_index
)) {
1908 tp
->t_flagsext
|= TF_IF_PROBING
;
1909 tcp_timers(tp
, TCPT_PTO
);
1910 tp
->t_timer
[TCPT_PTO
] = 0;
1911 tp
->t_flagsext
&= ~TF_IF_PROBING
;
1915 * Since the timer thread needs to wait for tcp lock, it may race
1916 * with another thread that can cancel or reschedule the timer
1917 * that is about to run. Check if we need to run anything.
1919 if ((index
= tp
->tentry
.index
) == TCPT_NONE
) {
1923 timer_val
= tp
->t_timer
[index
];
1925 diff
= timer_diff(tp
->tentry
.runtime
, 0, tcp_now
, 0);
1927 if (tp
->tentry
.index
!= TCPT_NONE
) {
1929 *(te_mode
) = tp
->tentry
.mode
;
1934 tp
->t_timer
[index
] = 0;
1935 if (timer_val
> 0) {
1936 tp
= tcp_timers(tp
, index
);
1943 * Check if there are any other timers that need to be run.
1944 * While doing it, adjust the timer values wrt tcp_now.
1946 tp
->tentry
.mode
= 0;
1947 for (i
= 0; i
< TCPT_NTIMERS
; ++i
) {
1948 if (tp
->t_timer
[i
] != 0) {
1949 diff
= timer_diff(tp
->tentry
.timer_start
,
1950 tp
->t_timer
[i
], tcp_now
, 0);
1952 needtorun
[i
] = TRUE
;
1955 tp
->t_timer
[i
] = diff
;
1956 needtorun
[i
] = FALSE
;
1957 if (lo_timer
== 0 || diff
< lo_timer
) {
1961 TCP_SET_TIMER_MODE(tp
->tentry
.mode
, i
);
1966 tp
->tentry
.timer_start
= tcp_now
;
1967 tp
->tentry
.index
= lo_index
;
1968 VERIFY(tp
->tentry
.index
== TCPT_NONE
|| tp
->tentry
.mode
> 0);
1970 if (tp
->tentry
.index
!= TCPT_NONE
) {
1971 tp
->tentry
.runtime
= tp
->tentry
.timer_start
+
1972 tp
->t_timer
[tp
->tentry
.index
];
1973 if (tp
->tentry
.runtime
== 0) {
1974 tp
->tentry
.runtime
++;
1979 /* run any other timers outstanding at this time. */
1980 for (i
= 0; i
< TCPT_NTIMERS
; ++i
) {
1983 tp
= tcp_timers(tp
, i
);
1991 tcp_set_lotimer_index(tp
);
1994 if (tp
->tentry
.index
< TCPT_NONE
) {
1995 offset
= tp
->t_timer
[tp
->tentry
.index
];
1996 *(te_mode
) = tp
->tentry
.mode
;
2000 if (tp
!= NULL
&& tp
->tentry
.index
== TCPT_NONE
) {
2001 tcp_remove_timer(tp
);
2005 socket_unlock(so
, 1);
2010 tcp_run_timerlist(void * arg1
, void * arg2
)
2012 #pragma unused(arg1, arg2)
2013 struct tcptimerentry
*te
, *next_te
;
2014 struct tcptimerlist
*listp
= &tcp_timer_list
;
2016 uint32_t next_timer
= 0; /* offset of the next timer on the list */
2017 u_int16_t te_mode
= 0; /* modes of all active timers in a tcpcb */
2018 u_int16_t list_mode
= 0; /* cumulative of modes of all tcpcbs */
2019 uint32_t active_count
= 0;
2021 calculate_tcp_clock();
2023 lck_mtx_lock(listp
->mtx
);
2025 int32_t drift
= tcp_now
- listp
->runtime
;
2027 tcpstat
.tcps_timer_drift_le_1_ms
++;
2028 } else if (drift
<= 10) {
2029 tcpstat
.tcps_timer_drift_le_10_ms
++;
2030 } else if (drift
<= 20) {
2031 tcpstat
.tcps_timer_drift_le_20_ms
++;
2032 } else if (drift
<= 50) {
2033 tcpstat
.tcps_timer_drift_le_50_ms
++;
2034 } else if (drift
<= 100) {
2035 tcpstat
.tcps_timer_drift_le_100_ms
++;
2036 } else if (drift
<= 200) {
2037 tcpstat
.tcps_timer_drift_le_200_ms
++;
2038 } else if (drift
<= 500) {
2039 tcpstat
.tcps_timer_drift_le_500_ms
++;
2040 } else if (drift
<= 1000) {
2041 tcpstat
.tcps_timer_drift_le_1000_ms
++;
2043 tcpstat
.tcps_timer_drift_gt_1000_ms
++;
2046 listp
->running
= TRUE
;
2048 LIST_FOREACH_SAFE(te
, &listp
->lhead
, le
, next_te
) {
2049 uint32_t offset
= 0;
2050 uint32_t runtime
= te
->runtime
;
2052 tp
= TIMERENTRY_TO_TP(te
);
2055 * An interface probe may need to happen before the previously scheduled runtime
2057 if (te
->index
< TCPT_NONE
&& TSTMP_GT(runtime
, tcp_now
) &&
2058 !TCP_IF_STATE_CHANGED(tp
, listp
->probe_if_index
)) {
2059 offset
= timer_diff(runtime
, 0, tcp_now
, 0);
2060 if (next_timer
== 0 || offset
< next_timer
) {
2061 next_timer
= offset
;
2063 list_mode
|= te
->mode
;
2068 * Acquire an inp wantcnt on the inpcb so that the socket
2069 * won't get detached even if tcp_close is called
2071 if (in_pcb_checkstate(tp
->t_inpcb
, WNT_ACQUIRE
, 0)
2074 * Some how this pcb went into dead state while
2075 * on the timer list, just take it off the list.
2076 * Since the timer list entry pointers are
2077 * protected by the timer list lock, we can
2078 * do it here without the socket lock.
2080 if (TIMER_IS_ON_LIST(tp
)) {
2081 tp
->t_flags
&= ~(TF_TIMER_ONLIST
);
2082 LIST_REMOVE(&tp
->tentry
, le
);
2085 tp
->tentry
.le
.le_next
= NULL
;
2086 tp
->tentry
.le
.le_prev
= NULL
;
2093 * Store the next timerentry pointer before releasing the
2094 * list lock. If that entry has to be removed when we
2095 * release the lock, this pointer will be updated to the
2096 * element after that.
2098 listp
->next_te
= next_te
;
2100 VERIFY_NEXT_LINK(&tp
->tentry
, le
);
2101 VERIFY_PREV_LINK(&tp
->tentry
, le
);
2103 lck_mtx_unlock(listp
->mtx
);
2105 offset
= tcp_run_conn_timer(tp
, &te_mode
,
2106 listp
->probe_if_index
);
2108 lck_mtx_lock(listp
->mtx
);
2110 next_te
= listp
->next_te
;
2111 listp
->next_te
= NULL
;
2113 if (offset
> 0 && te_mode
!= 0) {
2114 list_mode
|= te_mode
;
2116 if (next_timer
== 0 || offset
< next_timer
) {
2117 next_timer
= offset
;
2122 if (!LIST_EMPTY(&listp
->lhead
)) {
2123 u_int16_t next_mode
= 0;
2124 if ((list_mode
& TCP_TIMERLIST_10MS_MODE
) ||
2125 (listp
->pref_mode
& TCP_TIMERLIST_10MS_MODE
)) {
2126 next_mode
= TCP_TIMERLIST_10MS_MODE
;
2127 } else if ((list_mode
& TCP_TIMERLIST_100MS_MODE
) ||
2128 (listp
->pref_mode
& TCP_TIMERLIST_100MS_MODE
)) {
2129 next_mode
= TCP_TIMERLIST_100MS_MODE
;
2131 next_mode
= TCP_TIMERLIST_500MS_MODE
;
2134 if (next_mode
!= TCP_TIMERLIST_500MS_MODE
) {
2135 listp
->idleruns
= 0;
2138 * the next required mode is slow mode, but if
2139 * the last one was a faster mode and we did not
2140 * have enough idle runs, repeat the last mode.
2142 * We try to keep the timer list in fast mode for
2143 * some idle time in expectation of new data.
2145 if (listp
->mode
!= next_mode
&&
2146 listp
->idleruns
< timer_fastmode_idlemax
) {
2148 next_mode
= listp
->mode
;
2149 next_timer
= TCP_TIMER_100MS_QUANTUM
;
2151 listp
->idleruns
= 0;
2154 listp
->mode
= next_mode
;
2155 if (listp
->pref_offset
!= 0) {
2156 next_timer
= min(listp
->pref_offset
, next_timer
);
2159 if (listp
->mode
== TCP_TIMERLIST_500MS_MODE
) {
2160 next_timer
= max(next_timer
,
2161 TCP_TIMER_500MS_QUANTUM
);
2164 tcp_sched_timerlist(next_timer
);
2167 * No need to reschedule this timer, but always run
2168 * periodically at a much higher granularity.
2170 tcp_sched_timerlist(TCP_TIMERLIST_MAX_OFFSET
);
2173 listp
->running
= FALSE
;
2174 listp
->pref_mode
= 0;
2175 listp
->pref_offset
= 0;
2176 listp
->probe_if_index
= 0;
2178 lck_mtx_unlock(listp
->mtx
);
2182 * Function to check if the timerlist needs to be rescheduled to run this
2183 * connection's timers correctly.
2186 tcp_sched_timers(struct tcpcb
*tp
)
2188 struct tcptimerentry
*te
= &tp
->tentry
;
2189 u_int16_t index
= te
->index
;
2190 u_int16_t mode
= te
->mode
;
2191 struct tcptimerlist
*listp
= &tcp_timer_list
;
2193 boolean_t list_locked
= FALSE
;
2195 if (tp
->t_inpcb
->inp_state
== INPCB_STATE_DEAD
) {
2196 /* Just return without adding the dead pcb to the list */
2197 if (TIMER_IS_ON_LIST(tp
)) {
2198 tcp_remove_timer(tp
);
2203 if (index
== TCPT_NONE
) {
2204 /* Nothing to run */
2205 tcp_remove_timer(tp
);
2210 * compute the offset at which the next timer for this connection
2213 offset
= timer_diff(te
->runtime
, 0, tcp_now
, 0);
2216 tcp_timer_advanced
++;
2219 if (!TIMER_IS_ON_LIST(tp
)) {
2221 lck_mtx_lock(listp
->mtx
);
2225 if (!TIMER_IS_ON_LIST(tp
)) {
2226 LIST_INSERT_HEAD(&listp
->lhead
, te
, le
);
2227 tp
->t_flags
|= TF_TIMER_ONLIST
;
2230 if (listp
->entries
> listp
->maxentries
) {
2231 listp
->maxentries
= listp
->entries
;
2234 /* if the list is not scheduled, just schedule it */
2235 if (!listp
->scheduled
) {
2242 * Timer entry is currently on the list, check if the list needs
2243 * to be rescheduled.
2245 if (need_to_resched_timerlist(te
->runtime
, mode
)) {
2246 tcp_resched_timerlist
++;
2249 lck_mtx_lock(listp
->mtx
);
2253 VERIFY_NEXT_LINK(te
, le
);
2254 VERIFY_PREV_LINK(te
, le
);
2256 if (listp
->running
) {
2257 listp
->pref_mode
|= mode
;
2258 if (listp
->pref_offset
== 0 ||
2259 offset
< listp
->pref_offset
) {
2260 listp
->pref_offset
= offset
;
2264 * The list could have got rescheduled while
2265 * this thread was waiting for the lock
2267 if (listp
->scheduled
) {
2269 diff
= timer_diff(listp
->runtime
, 0,
2285 * Since a connection with timers is getting scheduled, the timer
2286 * list moves from idle to active state and that is why idlegen is
2289 if (mode
& TCP_TIMERLIST_10MS_MODE
) {
2290 listp
->mode
= TCP_TIMERLIST_10MS_MODE
;
2291 listp
->idleruns
= 0;
2292 offset
= min(offset
, TCP_TIMER_10MS_QUANTUM
);
2293 } else if (mode
& TCP_TIMERLIST_100MS_MODE
) {
2294 if (listp
->mode
> TCP_TIMERLIST_100MS_MODE
) {
2295 listp
->mode
= TCP_TIMERLIST_100MS_MODE
;
2297 listp
->idleruns
= 0;
2298 offset
= min(offset
, TCP_TIMER_100MS_QUANTUM
);
2300 tcp_sched_timerlist(offset
);
2304 lck_mtx_unlock(listp
->mtx
);
2311 tcp_set_lotimer_index(struct tcpcb
*tp
)
2313 uint16_t i
, lo_index
= TCPT_NONE
, mode
= 0;
2314 uint32_t lo_timer
= 0;
2315 for (i
= 0; i
< TCPT_NTIMERS
; ++i
) {
2316 if (tp
->t_timer
[i
] != 0) {
2317 TCP_SET_TIMER_MODE(mode
, i
);
2318 if (lo_timer
== 0 || tp
->t_timer
[i
] < lo_timer
) {
2319 lo_timer
= tp
->t_timer
[i
];
2324 tp
->tentry
.index
= lo_index
;
2325 tp
->tentry
.mode
= mode
;
2326 VERIFY(tp
->tentry
.index
== TCPT_NONE
|| tp
->tentry
.mode
> 0);
2328 if (tp
->tentry
.index
!= TCPT_NONE
) {
2329 tp
->tentry
.runtime
= tp
->tentry
.timer_start
2330 + tp
->t_timer
[tp
->tentry
.index
];
2331 if (tp
->tentry
.runtime
== 0) {
2332 tp
->tentry
.runtime
++;
2338 tcp_check_timer_state(struct tcpcb
*tp
)
2340 socket_lock_assert_owned(tp
->t_inpcb
->inp_socket
);
2342 if (tp
->t_inpcb
->inp_flags2
& INP2_TIMEWAIT
) {
2346 tcp_set_lotimer_index(tp
);
2348 tcp_sched_timers(tp
);
2353 tcp_cumulative_stat(u_int32_t cur
, u_int32_t
*prev
, u_int32_t
*dest
)
2355 /* handle wrap around */
2356 int32_t diff
= (int32_t) (cur
- *prev
);
2367 tcp_cumulative_stat64(u_int64_t cur
, u_int64_t
*prev
, u_int64_t
*dest
)
2369 /* handle wrap around */
2370 int64_t diff
= (int64_t) (cur
- *prev
);
2380 __private_extern__
void
2381 tcp_report_stats(void)
2383 struct nstat_sysinfo_data data
;
2384 struct sockaddr_in dst
;
2385 struct sockaddr_in6 dst6
;
2386 struct rtentry
*rt
= NULL
;
2387 static struct tcp_last_report_stats prev
;
2388 u_int64_t var
, uptime
;
2390 #define stat data.u.tcp_stats
2391 if (((uptime
= net_uptime()) - tcp_last_report_time
) <
2392 tcp_report_stats_interval
) {
2396 tcp_last_report_time
= uptime
;
2398 bzero(&data
, sizeof(data
));
2399 data
.flags
= NSTAT_SYSINFO_TCP_STATS
;
2401 bzero(&dst
, sizeof(dst
));
2402 dst
.sin_len
= sizeof(dst
);
2403 dst
.sin_family
= AF_INET
;
2406 lck_mtx_lock(rnh_lock
);
2407 rt
= rt_lookup(TRUE
, (struct sockaddr
*)&dst
, NULL
,
2408 rt_tables
[AF_INET
], IFSCOPE_NONE
);
2409 lck_mtx_unlock(rnh_lock
);
2412 if (rt_primary_default(rt
, rt_key(rt
)) &&
2413 rt
->rt_stats
!= NULL
) {
2414 stat
.ipv4_avgrtt
= rt
->rt_stats
->nstat_avg_rtt
;
2422 bzero(&dst6
, sizeof(dst6
));
2423 dst6
.sin6_len
= sizeof(dst6
);
2424 dst6
.sin6_family
= AF_INET6
;
2426 lck_mtx_lock(rnh_lock
);
2427 rt
= rt_lookup(TRUE
, (struct sockaddr
*)&dst6
, NULL
,
2428 rt_tables
[AF_INET6
], IFSCOPE_NONE
);
2429 lck_mtx_unlock(rnh_lock
);
2432 if (rt_primary_default(rt
, rt_key(rt
)) &&
2433 rt
->rt_stats
!= NULL
) {
2434 stat
.ipv6_avgrtt
= rt
->rt_stats
->nstat_avg_rtt
;
2441 /* send packet loss rate, shift by 10 for precision */
2442 if (tcpstat
.tcps_sndpack
> 0 && tcpstat
.tcps_sndrexmitpack
> 0) {
2443 var
= tcpstat
.tcps_sndrexmitpack
<< 10;
2444 stat
.send_plr
= (var
* 100) / tcpstat
.tcps_sndpack
;
2447 /* recv packet loss rate, shift by 10 for precision */
2448 if (tcpstat
.tcps_rcvpack
> 0 && tcpstat
.tcps_recovered_pkts
> 0) {
2449 var
= tcpstat
.tcps_recovered_pkts
<< 10;
2450 stat
.recv_plr
= (var
* 100) / tcpstat
.tcps_rcvpack
;
2453 /* RTO after tail loss, shift by 10 for precision */
2454 if (tcpstat
.tcps_sndrexmitpack
> 0
2455 && tcpstat
.tcps_tailloss_rto
> 0) {
2456 var
= tcpstat
.tcps_tailloss_rto
<< 10;
2457 stat
.send_tlrto_rate
=
2458 (var
* 100) / tcpstat
.tcps_sndrexmitpack
;
2461 /* packet reordering */
2462 if (tcpstat
.tcps_sndpack
> 0 && tcpstat
.tcps_reordered_pkts
> 0) {
2463 var
= tcpstat
.tcps_reordered_pkts
<< 10;
2464 stat
.send_reorder_rate
=
2465 (var
* 100) / tcpstat
.tcps_sndpack
;
2468 if (tcp_ecn_outbound
== 1) {
2469 stat
.ecn_client_enabled
= 1;
2471 if (tcp_ecn_inbound
== 1) {
2472 stat
.ecn_server_enabled
= 1;
2474 tcp_cumulative_stat(tcpstat
.tcps_connattempt
,
2475 &prev
.tcps_connattempt
, &stat
.connection_attempts
);
2476 tcp_cumulative_stat(tcpstat
.tcps_accepts
,
2477 &prev
.tcps_accepts
, &stat
.connection_accepts
);
2478 tcp_cumulative_stat(tcpstat
.tcps_ecn_client_setup
,
2479 &prev
.tcps_ecn_client_setup
, &stat
.ecn_client_setup
);
2480 tcp_cumulative_stat(tcpstat
.tcps_ecn_server_setup
,
2481 &prev
.tcps_ecn_server_setup
, &stat
.ecn_server_setup
);
2482 tcp_cumulative_stat(tcpstat
.tcps_ecn_client_success
,
2483 &prev
.tcps_ecn_client_success
, &stat
.ecn_client_success
);
2484 tcp_cumulative_stat(tcpstat
.tcps_ecn_server_success
,
2485 &prev
.tcps_ecn_server_success
, &stat
.ecn_server_success
);
2486 tcp_cumulative_stat(tcpstat
.tcps_ecn_not_supported
,
2487 &prev
.tcps_ecn_not_supported
, &stat
.ecn_not_supported
);
2488 tcp_cumulative_stat(tcpstat
.tcps_ecn_lost_syn
,
2489 &prev
.tcps_ecn_lost_syn
, &stat
.ecn_lost_syn
);
2490 tcp_cumulative_stat(tcpstat
.tcps_ecn_lost_synack
,
2491 &prev
.tcps_ecn_lost_synack
, &stat
.ecn_lost_synack
);
2492 tcp_cumulative_stat(tcpstat
.tcps_ecn_recv_ce
,
2493 &prev
.tcps_ecn_recv_ce
, &stat
.ecn_recv_ce
);
2494 tcp_cumulative_stat(tcpstat
.tcps_ecn_recv_ece
,
2495 &prev
.tcps_ecn_recv_ece
, &stat
.ecn_recv_ece
);
2496 tcp_cumulative_stat(tcpstat
.tcps_ecn_recv_ece
,
2497 &prev
.tcps_ecn_recv_ece
, &stat
.ecn_recv_ece
);
2498 tcp_cumulative_stat(tcpstat
.tcps_ecn_sent_ece
,
2499 &prev
.tcps_ecn_sent_ece
, &stat
.ecn_sent_ece
);
2500 tcp_cumulative_stat(tcpstat
.tcps_ecn_sent_ece
,
2501 &prev
.tcps_ecn_sent_ece
, &stat
.ecn_sent_ece
);
2502 tcp_cumulative_stat(tcpstat
.tcps_ecn_conn_recv_ce
,
2503 &prev
.tcps_ecn_conn_recv_ce
, &stat
.ecn_conn_recv_ce
);
2504 tcp_cumulative_stat(tcpstat
.tcps_ecn_conn_recv_ece
,
2505 &prev
.tcps_ecn_conn_recv_ece
, &stat
.ecn_conn_recv_ece
);
2506 tcp_cumulative_stat(tcpstat
.tcps_ecn_conn_plnoce
,
2507 &prev
.tcps_ecn_conn_plnoce
, &stat
.ecn_conn_plnoce
);
2508 tcp_cumulative_stat(tcpstat
.tcps_ecn_conn_pl_ce
,
2509 &prev
.tcps_ecn_conn_pl_ce
, &stat
.ecn_conn_pl_ce
);
2510 tcp_cumulative_stat(tcpstat
.tcps_ecn_conn_nopl_ce
,
2511 &prev
.tcps_ecn_conn_nopl_ce
, &stat
.ecn_conn_nopl_ce
);
2512 tcp_cumulative_stat(tcpstat
.tcps_ecn_fallback_synloss
,
2513 &prev
.tcps_ecn_fallback_synloss
, &stat
.ecn_fallback_synloss
);
2514 tcp_cumulative_stat(tcpstat
.tcps_ecn_fallback_reorder
,
2515 &prev
.tcps_ecn_fallback_reorder
, &stat
.ecn_fallback_reorder
);
2516 tcp_cumulative_stat(tcpstat
.tcps_ecn_fallback_ce
,
2517 &prev
.tcps_ecn_fallback_ce
, &stat
.ecn_fallback_ce
);
2518 tcp_cumulative_stat(tcpstat
.tcps_tfo_syn_data_rcv
,
2519 &prev
.tcps_tfo_syn_data_rcv
, &stat
.tfo_syn_data_rcv
);
2520 tcp_cumulative_stat(tcpstat
.tcps_tfo_cookie_req_rcv
,
2521 &prev
.tcps_tfo_cookie_req_rcv
, &stat
.tfo_cookie_req_rcv
);
2522 tcp_cumulative_stat(tcpstat
.tcps_tfo_cookie_sent
,
2523 &prev
.tcps_tfo_cookie_sent
, &stat
.tfo_cookie_sent
);
2524 tcp_cumulative_stat(tcpstat
.tcps_tfo_cookie_invalid
,
2525 &prev
.tcps_tfo_cookie_invalid
, &stat
.tfo_cookie_invalid
);
2526 tcp_cumulative_stat(tcpstat
.tcps_tfo_cookie_req
,
2527 &prev
.tcps_tfo_cookie_req
, &stat
.tfo_cookie_req
);
2528 tcp_cumulative_stat(tcpstat
.tcps_tfo_cookie_rcv
,
2529 &prev
.tcps_tfo_cookie_rcv
, &stat
.tfo_cookie_rcv
);
2530 tcp_cumulative_stat(tcpstat
.tcps_tfo_syn_data_sent
,
2531 &prev
.tcps_tfo_syn_data_sent
, &stat
.tfo_syn_data_sent
);
2532 tcp_cumulative_stat(tcpstat
.tcps_tfo_syn_data_acked
,
2533 &prev
.tcps_tfo_syn_data_acked
, &stat
.tfo_syn_data_acked
);
2534 tcp_cumulative_stat(tcpstat
.tcps_tfo_syn_loss
,
2535 &prev
.tcps_tfo_syn_loss
, &stat
.tfo_syn_loss
);
2536 tcp_cumulative_stat(tcpstat
.tcps_tfo_blackhole
,
2537 &prev
.tcps_tfo_blackhole
, &stat
.tfo_blackhole
);
2538 tcp_cumulative_stat(tcpstat
.tcps_tfo_cookie_wrong
,
2539 &prev
.tcps_tfo_cookie_wrong
, &stat
.tfo_cookie_wrong
);
2540 tcp_cumulative_stat(tcpstat
.tcps_tfo_no_cookie_rcv
,
2541 &prev
.tcps_tfo_no_cookie_rcv
, &stat
.tfo_no_cookie_rcv
);
2542 tcp_cumulative_stat(tcpstat
.tcps_tfo_heuristics_disable
,
2543 &prev
.tcps_tfo_heuristics_disable
, &stat
.tfo_heuristics_disable
);
2544 tcp_cumulative_stat(tcpstat
.tcps_tfo_sndblackhole
,
2545 &prev
.tcps_tfo_sndblackhole
, &stat
.tfo_sndblackhole
);
2548 tcp_cumulative_stat(tcpstat
.tcps_mptcp_handover_attempt
,
2549 &prev
.tcps_mptcp_handover_attempt
, &stat
.mptcp_handover_attempt
);
2550 tcp_cumulative_stat(tcpstat
.tcps_mptcp_interactive_attempt
,
2551 &prev
.tcps_mptcp_interactive_attempt
, &stat
.mptcp_interactive_attempt
);
2552 tcp_cumulative_stat(tcpstat
.tcps_mptcp_aggregate_attempt
,
2553 &prev
.tcps_mptcp_aggregate_attempt
, &stat
.mptcp_aggregate_attempt
);
2554 tcp_cumulative_stat(tcpstat
.tcps_mptcp_fp_handover_attempt
,
2555 &prev
.tcps_mptcp_fp_handover_attempt
, &stat
.mptcp_fp_handover_attempt
);
2556 tcp_cumulative_stat(tcpstat
.tcps_mptcp_fp_interactive_attempt
,
2557 &prev
.tcps_mptcp_fp_interactive_attempt
, &stat
.mptcp_fp_interactive_attempt
);
2558 tcp_cumulative_stat(tcpstat
.tcps_mptcp_fp_aggregate_attempt
,
2559 &prev
.tcps_mptcp_fp_aggregate_attempt
, &stat
.mptcp_fp_aggregate_attempt
);
2560 tcp_cumulative_stat(tcpstat
.tcps_mptcp_heuristic_fallback
,
2561 &prev
.tcps_mptcp_heuristic_fallback
, &stat
.mptcp_heuristic_fallback
);
2562 tcp_cumulative_stat(tcpstat
.tcps_mptcp_fp_heuristic_fallback
,
2563 &prev
.tcps_mptcp_fp_heuristic_fallback
, &stat
.mptcp_fp_heuristic_fallback
);
2564 tcp_cumulative_stat(tcpstat
.tcps_mptcp_handover_success_wifi
,
2565 &prev
.tcps_mptcp_handover_success_wifi
, &stat
.mptcp_handover_success_wifi
);
2566 tcp_cumulative_stat(tcpstat
.tcps_mptcp_handover_success_cell
,
2567 &prev
.tcps_mptcp_handover_success_cell
, &stat
.mptcp_handover_success_cell
);
2568 tcp_cumulative_stat(tcpstat
.tcps_mptcp_interactive_success
,
2569 &prev
.tcps_mptcp_interactive_success
, &stat
.mptcp_interactive_success
);
2570 tcp_cumulative_stat(tcpstat
.tcps_mptcp_aggregate_success
,
2571 &prev
.tcps_mptcp_aggregate_success
, &stat
.mptcp_aggregate_success
);
2572 tcp_cumulative_stat(tcpstat
.tcps_mptcp_fp_handover_success_wifi
,
2573 &prev
.tcps_mptcp_fp_handover_success_wifi
, &stat
.mptcp_fp_handover_success_wifi
);
2574 tcp_cumulative_stat(tcpstat
.tcps_mptcp_fp_handover_success_cell
,
2575 &prev
.tcps_mptcp_fp_handover_success_cell
, &stat
.mptcp_fp_handover_success_cell
);
2576 tcp_cumulative_stat(tcpstat
.tcps_mptcp_fp_interactive_success
,
2577 &prev
.tcps_mptcp_fp_interactive_success
, &stat
.mptcp_fp_interactive_success
);
2578 tcp_cumulative_stat(tcpstat
.tcps_mptcp_fp_aggregate_success
,
2579 &prev
.tcps_mptcp_fp_aggregate_success
, &stat
.mptcp_fp_aggregate_success
);
2580 tcp_cumulative_stat(tcpstat
.tcps_mptcp_handover_cell_from_wifi
,
2581 &prev
.tcps_mptcp_handover_cell_from_wifi
, &stat
.mptcp_handover_cell_from_wifi
);
2582 tcp_cumulative_stat(tcpstat
.tcps_mptcp_handover_wifi_from_cell
,
2583 &prev
.tcps_mptcp_handover_wifi_from_cell
, &stat
.mptcp_handover_wifi_from_cell
);
2584 tcp_cumulative_stat(tcpstat
.tcps_mptcp_interactive_cell_from_wifi
,
2585 &prev
.tcps_mptcp_interactive_cell_from_wifi
, &stat
.mptcp_interactive_cell_from_wifi
);
2586 tcp_cumulative_stat64(tcpstat
.tcps_mptcp_handover_cell_bytes
,
2587 &prev
.tcps_mptcp_handover_cell_bytes
, &stat
.mptcp_handover_cell_bytes
);
2588 tcp_cumulative_stat64(tcpstat
.tcps_mptcp_interactive_cell_bytes
,
2589 &prev
.tcps_mptcp_interactive_cell_bytes
, &stat
.mptcp_interactive_cell_bytes
);
2590 tcp_cumulative_stat64(tcpstat
.tcps_mptcp_aggregate_cell_bytes
,
2591 &prev
.tcps_mptcp_aggregate_cell_bytes
, &stat
.mptcp_aggregate_cell_bytes
);
2592 tcp_cumulative_stat64(tcpstat
.tcps_mptcp_handover_all_bytes
,
2593 &prev
.tcps_mptcp_handover_all_bytes
, &stat
.mptcp_handover_all_bytes
);
2594 tcp_cumulative_stat64(tcpstat
.tcps_mptcp_interactive_all_bytes
,
2595 &prev
.tcps_mptcp_interactive_all_bytes
, &stat
.mptcp_interactive_all_bytes
);
2596 tcp_cumulative_stat64(tcpstat
.tcps_mptcp_aggregate_all_bytes
,
2597 &prev
.tcps_mptcp_aggregate_all_bytes
, &stat
.mptcp_aggregate_all_bytes
);
2598 tcp_cumulative_stat(tcpstat
.tcps_mptcp_back_to_wifi
,
2599 &prev
.tcps_mptcp_back_to_wifi
, &stat
.mptcp_back_to_wifi
);
2600 tcp_cumulative_stat(tcpstat
.tcps_mptcp_wifi_proxy
,
2601 &prev
.tcps_mptcp_wifi_proxy
, &stat
.mptcp_wifi_proxy
);
2602 tcp_cumulative_stat(tcpstat
.tcps_mptcp_cell_proxy
,
2603 &prev
.tcps_mptcp_cell_proxy
, &stat
.mptcp_cell_proxy
);
2604 tcp_cumulative_stat(tcpstat
.tcps_mptcp_triggered_cell
,
2605 &prev
.tcps_mptcp_triggered_cell
, &stat
.mptcp_triggered_cell
);
2607 nstat_sysinfo_send_data(&data
);
2613 tcp_interface_send_probe(u_int16_t probe_if_index
)
2616 struct tcptimerlist
*listp
= &tcp_timer_list
;
2618 /* Make sure TCP clock is up to date */
2619 calculate_tcp_clock();
2621 lck_mtx_lock(listp
->mtx
);
2622 if (listp
->probe_if_index
> 0 && listp
->probe_if_index
!= probe_if_index
) {
2623 tcpstat
.tcps_probe_if_conflict
++;
2624 os_log(OS_LOG_DEFAULT
,
2625 "%s: probe_if_index %u conflicts with %u, tcps_probe_if_conflict %u\n",
2626 __func__
, probe_if_index
, listp
->probe_if_index
,
2627 tcpstat
.tcps_probe_if_conflict
);
2631 listp
->probe_if_index
= probe_if_index
;
2632 if (listp
->running
) {
2633 os_log(OS_LOG_DEFAULT
, "%s: timer list already running for if_index %u\n",
2634 __func__
, probe_if_index
);
2639 * Reschedule the timerlist to run within the next 10ms, which is
2640 * the fastest that we can do.
2642 offset
= TCP_TIMER_10MS_QUANTUM
;
2643 if (listp
->scheduled
) {
2645 diff
= timer_diff(listp
->runtime
, 0, tcp_now
, offset
);
2647 /* The timer will fire sooner than what's needed */
2648 os_log(OS_LOG_DEFAULT
,
2649 "%s: timer will fire sooner than needed for if_index %u\n",
2650 __func__
, probe_if_index
);
2654 listp
->mode
= TCP_TIMERLIST_10MS_MODE
;
2655 listp
->idleruns
= 0;
2657 tcp_sched_timerlist(offset
);
2660 lck_mtx_unlock(listp
->mtx
);
2665 * Enable read probes on this connection, if:
2666 * - it is in established state
2667 * - doesn't have any data outstanding
2668 * - the outgoing ifp matches
2669 * - we have not already sent any read probes
2672 tcp_enable_read_probe(struct tcpcb
*tp
, struct ifnet
*ifp
)
2674 if (tp
->t_state
== TCPS_ESTABLISHED
&&
2675 tp
->snd_max
== tp
->snd_una
&&
2676 tp
->t_inpcb
->inp_last_outifp
== ifp
&&
2677 !(tp
->t_flagsext
& TF_DETECT_READSTALL
) &&
2678 tp
->t_rtimo_probes
== 0) {
2679 tp
->t_flagsext
|= TF_DETECT_READSTALL
;
2680 tp
->t_rtimo_probes
= 0;
2681 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
,
2682 TCP_TIMER_10MS_QUANTUM
);
2683 if (tp
->tentry
.index
== TCPT_NONE
) {
2684 tp
->tentry
.index
= TCPT_KEEP
;
2685 tp
->tentry
.runtime
= tcp_now
+
2686 TCP_TIMER_10MS_QUANTUM
;
2690 /* Reset runtime to be in next 10ms */
2691 diff
= timer_diff(tp
->tentry
.runtime
, 0,
2692 tcp_now
, TCP_TIMER_10MS_QUANTUM
);
2694 tp
->tentry
.index
= TCPT_KEEP
;
2695 tp
->tentry
.runtime
= tcp_now
+
2696 TCP_TIMER_10MS_QUANTUM
;
2697 if (tp
->tentry
.runtime
== 0) {
2698 tp
->tentry
.runtime
++;
2706 * Disable read probe and reset the keep alive timer
2709 tcp_disable_read_probe(struct tcpcb
*tp
)
2711 if (tp
->t_adaptive_rtimo
== 0 &&
2712 ((tp
->t_flagsext
& TF_DETECT_READSTALL
) ||
2713 tp
->t_rtimo_probes
> 0)) {
2714 tcp_keepalive_reset(tp
);
2717 mptcp_reset_keepalive(tp
);
2723 * Reschedule the tcp timerlist in the next 10ms to re-enable read/write
2724 * probes on connections going over a particular interface.
2727 tcp_probe_connectivity(struct ifnet
*ifp
, u_int32_t enable
)
2730 struct tcptimerlist
*listp
= &tcp_timer_list
;
2731 struct inpcbinfo
*pcbinfo
= &tcbinfo
;
2732 struct inpcb
*inp
, *nxt
;
2739 calculate_tcp_clock();
2742 * Enable keep alive timer on all connections that are
2743 * active/established on this interface.
2745 lck_rw_lock_shared(pcbinfo
->ipi_lock
);
2747 LIST_FOREACH_SAFE(inp
, pcbinfo
->ipi_listhead
, inp_list
, nxt
) {
2748 struct tcpcb
*tp
= NULL
;
2749 if (in_pcb_checkstate(inp
, WNT_ACQUIRE
, 0) ==
2754 /* Acquire lock to look at the state of the connection */
2755 socket_lock(inp
->inp_socket
, 1);
2757 /* Release the want count */
2758 if (inp
->inp_ppcb
== NULL
||
2759 (in_pcb_checkstate(inp
, WNT_RELEASE
, 1) == WNT_STOPUSING
)) {
2760 socket_unlock(inp
->inp_socket
, 1);
2763 tp
= intotcpcb(inp
);
2765 tcp_enable_read_probe(tp
, ifp
);
2767 tcp_disable_read_probe(tp
);
2770 socket_unlock(inp
->inp_socket
, 1);
2772 lck_rw_done(pcbinfo
->ipi_lock
);
2774 lck_mtx_lock(listp
->mtx
);
2775 if (listp
->running
) {
2776 listp
->pref_mode
|= TCP_TIMERLIST_10MS_MODE
;
2780 /* Reschedule within the next 10ms */
2781 offset
= TCP_TIMER_10MS_QUANTUM
;
2782 if (listp
->scheduled
) {
2784 diff
= timer_diff(listp
->runtime
, 0, tcp_now
, offset
);
2786 /* The timer will fire sooner than what's needed */
2790 listp
->mode
= TCP_TIMERLIST_10MS_MODE
;
2791 listp
->idleruns
= 0;
2793 tcp_sched_timerlist(offset
);
2795 lck_mtx_unlock(listp
->mtx
);
2800 tcp_update_mss_core(struct tcpcb
*tp
, struct ifnet
*ifp
)
2802 struct if_cellular_status_v1
*ifsr
;
2804 ifsr
= &ifp
->if_link_status
->ifsr_u
.ifsr_cell
.if_cell_u
.if_status_v1
;
2805 if (ifsr
->valid_bitmask
& IF_CELL_UL_MSS_RECOMMENDED_VALID
) {
2806 optlen
= tp
->t_maxopd
- tp
->t_maxseg
;
2808 if (ifsr
->mss_recommended
==
2809 IF_CELL_UL_MSS_RECOMMENDED_NONE
&&
2810 tp
->t_cached_maxopd
> 0 &&
2811 tp
->t_maxopd
< tp
->t_cached_maxopd
) {
2812 tp
->t_maxopd
= tp
->t_cached_maxopd
;
2813 tcpstat
.tcps_mss_to_default
++;
2814 } else if (ifsr
->mss_recommended
==
2815 IF_CELL_UL_MSS_RECOMMENDED_MEDIUM
&&
2816 tp
->t_maxopd
> tcp_mss_rec_medium
) {
2817 tp
->t_cached_maxopd
= tp
->t_maxopd
;
2818 tp
->t_maxopd
= tcp_mss_rec_medium
;
2819 tcpstat
.tcps_mss_to_medium
++;
2820 } else if (ifsr
->mss_recommended
==
2821 IF_CELL_UL_MSS_RECOMMENDED_LOW
&&
2822 tp
->t_maxopd
> tcp_mss_rec_low
) {
2823 tp
->t_cached_maxopd
= tp
->t_maxopd
;
2824 tp
->t_maxopd
= tcp_mss_rec_low
;
2825 tcpstat
.tcps_mss_to_low
++;
2827 tp
->t_maxseg
= tp
->t_maxopd
- optlen
;
2830 * clear the cached value if it is same as the current
2832 if (tp
->t_maxopd
== tp
->t_cached_maxopd
) {
2833 tp
->t_cached_maxopd
= 0;
2839 tcp_update_mss_locked(struct socket
*so
, struct ifnet
*ifp
)
2841 struct inpcb
*inp
= sotoinpcb(so
);
2842 struct tcpcb
*tp
= intotcpcb(inp
);
2844 if (ifp
== NULL
&& (ifp
= inp
->inp_last_outifp
) == NULL
) {
2848 if (!IFNET_IS_CELLULAR(ifp
)) {
2850 * This optimization is implemented for cellular
2855 if (tp
->t_state
<= TCPS_CLOSE_WAIT
) {
2857 * If the connection is currently doing or has done PMTU
2858 * blackhole detection, do not change the MSS
2860 if (tp
->t_flags
& TF_BLACKHOLE
) {
2863 if (ifp
->if_link_status
== NULL
) {
2866 tcp_update_mss_core(tp
, ifp
);
2871 tcp_itimer(struct inpcbinfo
*ipi
)
2873 struct inpcb
*inp
, *nxt
;
2875 if (lck_rw_try_lock_exclusive(ipi
->ipi_lock
) == FALSE
) {
2876 if (tcp_itimer_done
== TRUE
) {
2877 tcp_itimer_done
= FALSE
;
2878 atomic_add_32(&ipi
->ipi_timer_req
.intimer_fast
, 1);
2881 /* Upgrade failed, lost lock now take it again exclusive */
2882 lck_rw_lock_exclusive(ipi
->ipi_lock
);
2884 tcp_itimer_done
= TRUE
;
2886 LIST_FOREACH_SAFE(inp
, &tcb
, inp_list
, nxt
) {
2890 if (inp
->inp_ppcb
== NULL
||
2891 in_pcb_checkstate(inp
, WNT_ACQUIRE
, 0) == WNT_STOPUSING
) {
2894 so
= inp
->inp_socket
;
2895 ifp
= inp
->inp_last_outifp
;
2897 if (in_pcb_checkstate(inp
, WNT_RELEASE
, 1) == WNT_STOPUSING
) {
2898 socket_unlock(so
, 1);
2901 so_check_extended_bk_idle_time(so
);
2902 if (ipi
->ipi_flags
& INPCBINFO_UPDATE_MSS
) {
2903 tcp_update_mss_locked(so
, NULL
);
2905 socket_unlock(so
, 1);
2908 * Defunct all system-initiated background sockets if the
2909 * socket is using the cellular interface and the interface
2910 * has its LQM set to abort.
2912 if ((ipi
->ipi_flags
& INPCBINFO_HANDLE_LQM_ABORT
) &&
2913 IS_SO_TC_BACKGROUNDSYSTEM(so
->so_traffic_class
) &&
2914 ifp
!= NULL
&& IFNET_IS_CELLULAR(ifp
) &&
2915 (ifp
->if_interface_state
.valid_bitmask
&
2916 IF_INTERFACE_STATE_LQM_STATE_VALID
) &&
2917 ifp
->if_interface_state
.lqm_state
==
2918 IFNET_LQM_THRESH_ABORT
) {
2919 socket_defunct(current_proc(), so
,
2920 SHUTDOWN_SOCKET_LEVEL_DISCONNECT_ALL
);
2924 ipi
->ipi_flags
&= ~(INPCBINFO_UPDATE_MSS
| INPCBINFO_HANDLE_LQM_ABORT
);
2925 lck_rw_done(ipi
->ipi_lock
);