2 * Copyright (c) 2000-2015 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>
88 #include <netinet6/in6_pcb.h>
90 #include <netinet/ip_var.h>
91 #include <netinet/tcp.h>
92 #include <netinet/tcp_cache.h>
93 #include <netinet/tcp_fsm.h>
94 #include <netinet/tcp_seq.h>
95 #include <netinet/tcp_timer.h>
96 #include <netinet/tcp_var.h>
97 #include <netinet/tcp_cc.h>
99 #include <netinet6/tcp6_var.h>
101 #include <netinet/tcpip.h>
103 #include <netinet/tcp_debug.h>
105 #include <sys/kdebug.h>
106 #include <mach/sdt.h>
107 #include <netinet/mptcp_var.h>
109 #define TIMERENTRY_TO_TP(te) ((struct tcpcb *)((uintptr_t)te - offsetof(struct tcpcb, tentry.le.le_next)))
111 #define VERIFY_NEXT_LINK(elm,field) do { \
112 if (LIST_NEXT((elm),field) != NULL && \
113 LIST_NEXT((elm),field)->field.le_prev != \
114 &((elm)->field.le_next)) \
115 panic("Bad link elm %p next->prev != elm", (elm)); \
118 #define VERIFY_PREV_LINK(elm,field) do { \
119 if (*(elm)->field.le_prev != (elm)) \
120 panic("Bad link elm %p prev->next != elm", (elm)); \
123 #define TCP_SET_TIMER_MODE(mode, i) do { \
124 if (IS_TIMER_HZ_10MS(i)) \
125 (mode) |= TCP_TIMERLIST_10MS_MODE; \
126 else if (IS_TIMER_HZ_100MS(i)) \
127 (mode) |= TCP_TIMERLIST_100MS_MODE; \
129 (mode) |= TCP_TIMERLIST_500MS_MODE; \
132 /* Max number of times a stretch ack can be delayed on a connection */
133 #define TCP_STRETCHACK_DELAY_THRESHOLD 5
136 * If the host processor has been sleeping for too long, this is the threshold
137 * used to avoid sending stale retransmissions.
139 #define TCP_SLEEP_TOO_LONG (10 * 60 * 1000) /* 10 minutes in ms */
142 struct tcptimerlist tcp_timer_list
;
144 /* List of pcbs in timewait state, protected by tcbinfo's ipi_lock */
145 struct tcptailq tcp_tw_tailq
;
148 sysctl_msec_to_ticks SYSCTL_HANDLER_ARGS
150 #pragma unused(arg1, arg2)
153 tt
= *(int *)oidp
->oid_arg1
;
154 s
= tt
* 1000 / TCP_RETRANSHZ
;;
156 error
= sysctl_handle_int(oidp
, &s
, 0, req
);
157 if (error
|| !req
->newptr
)
160 tt
= s
* TCP_RETRANSHZ
/ 1000;
164 *(int *)oidp
->oid_arg1
= tt
;
169 SYSCTL_PROC(_net_inet_tcp
, TCPCTL_KEEPINIT
, keepinit
,
170 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
171 &tcp_keepinit
, 0, sysctl_msec_to_ticks
, "I", "");
174 SYSCTL_PROC(_net_inet_tcp
, TCPCTL_KEEPIDLE
, keepidle
,
175 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
176 &tcp_keepidle
, 0, sysctl_msec_to_ticks
, "I", "");
179 SYSCTL_PROC(_net_inet_tcp
, TCPCTL_KEEPINTVL
, keepintvl
,
180 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
181 &tcp_keepintvl
, 0, sysctl_msec_to_ticks
, "I", "");
184 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, keepcnt
,
185 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
186 &tcp_keepcnt
, 0, "number of times to repeat keepalive");
189 SYSCTL_PROC(_net_inet_tcp
, OID_AUTO
, msl
,
190 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
191 &tcp_msl
, 0, sysctl_msec_to_ticks
, "I", "Maximum segment lifetime");
194 * Avoid DoS via TCP Robustness in Persist Condition
195 * (see http://www.ietf.org/id/draft-ananth-tcpm-persist-02.txt)
196 * by allowing a system wide maximum persistence timeout value when in
197 * Zero Window Probe mode.
199 * Expressed in milliseconds to be consistent without timeout related
200 * values, the TCP socket option is in seconds.
202 u_int32_t tcp_max_persist_timeout
= 0;
203 SYSCTL_PROC(_net_inet_tcp
, OID_AUTO
, max_persist_timeout
,
204 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
205 &tcp_max_persist_timeout
, 0, sysctl_msec_to_ticks
, "I",
206 "Maximum persistence timeout for ZWP");
208 static int always_keepalive
= 0;
209 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, always_keepalive
,
210 CTLFLAG_RW
| CTLFLAG_LOCKED
,
211 &always_keepalive
, 0, "Assume SO_KEEPALIVE on all TCP connections");
214 * This parameter determines how long the timer list will stay in fast or
215 * quick mode even though all connections are idle. In this state, the
216 * timer will run more frequently anticipating new data.
218 int timer_fastmode_idlemax
= TCP_FASTMODE_IDLERUN_MAX
;
219 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, timer_fastmode_idlemax
,
220 CTLFLAG_RW
| CTLFLAG_LOCKED
,
221 &timer_fastmode_idlemax
, 0, "Maximum idle generations in fast mode");
224 * See tcp_syn_backoff[] for interval values between SYN retransmits;
225 * the value set below defines the number of retransmits, before we
226 * disable the timestamp and window scaling options during subsequent
227 * SYN retransmits. Setting it to 0 disables the dropping off of those
230 static int tcp_broken_peer_syn_rxmit_thres
= 10;
231 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, broken_peer_syn_rexmit_thres
,
232 CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_broken_peer_syn_rxmit_thres
, 0,
233 "Number of retransmitted SYNs before disabling RFC 1323 "
234 "options on local connections");
236 static int tcp_timer_advanced
= 0;
237 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, tcp_timer_advanced
,
238 CTLFLAG_RD
| CTLFLAG_LOCKED
, &tcp_timer_advanced
, 0,
239 "Number of times one of the timers was advanced");
241 static int tcp_resched_timerlist
= 0;
242 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, tcp_resched_timerlist
,
243 CTLFLAG_RD
| CTLFLAG_LOCKED
, &tcp_resched_timerlist
, 0,
244 "Number of times timer list was rescheduled as part of processing a packet");
246 int tcp_pmtud_black_hole_detect
= 1 ;
247 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, pmtud_blackhole_detection
,
248 CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_pmtud_black_hole_detect
, 0,
249 "Path MTU Discovery Black Hole Detection");
251 int tcp_pmtud_black_hole_mss
= 1200 ;
252 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, pmtud_blackhole_mss
,
253 CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_pmtud_black_hole_mss
, 0,
254 "Path MTU Discovery Black Hole Detection lowered MSS");
256 #define TCP_REPORT_STATS_INTERVAL 43200 /* 12 hours, in seconds */
257 int tcp_report_stats_interval
= TCP_REPORT_STATS_INTERVAL
;
258 #if (DEVELOPMENT || DEBUG)
259 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, report_stats_interval
,
260 CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_report_stats_interval
, 0,
261 "Report stats interval");
262 #endif /* (DEVELOPMENT || DEBUG) */
264 /* performed garbage collection of "used" sockets */
265 static boolean_t tcp_gc_done
= FALSE
;
267 /* max idle probes */
268 int tcp_maxpersistidle
;
271 * TCP delack timer is set to 100 ms. Since the processing of timer list
272 * in fast mode will happen no faster than 100 ms, the delayed ack timer
273 * will fire some where between 100 and 200 ms.
275 int tcp_delack
= TCP_RETRANSHZ
/ 10;
279 * MP_JOIN retransmission of 3rd ACK will be every 500 msecs without backoff
281 int tcp_jack_rxmt
= TCP_RETRANSHZ
/ 2;
284 static boolean_t tcp_itimer_done
= FALSE
;
286 static void tcp_remove_timer(struct tcpcb
*tp
);
287 static void tcp_sched_timerlist(uint32_t offset
);
288 static u_int32_t
tcp_run_conn_timer(struct tcpcb
*tp
, u_int16_t
*mode
,
289 u_int16_t probe_if_index
);
290 static void tcp_sched_timers(struct tcpcb
*tp
);
291 static inline void tcp_set_lotimer_index(struct tcpcb
*);
292 __private_extern__
void tcp_remove_from_time_wait(struct inpcb
*inp
);
293 __private_extern__
void tcp_report_stats(void);
296 * Macro to compare two timers. If there is a reset of the sign bit,
297 * it is safe to assume that the timer has wrapped around. By doing
298 * signed comparision, we take care of wrap around such that the value
299 * with the sign bit reset is actually ahead of the other.
302 timer_diff(uint32_t t1
, uint32_t toff1
, uint32_t t2
, uint32_t toff2
) {
303 return (int32_t)((t1
+ toff1
) - (t2
+ toff2
));
306 static u_int64_t tcp_last_report_time
;
309 * Structure to store previously reported stats so that we can send
310 * incremental changes in each report interval.
312 struct tcp_last_report_stats
{
313 u_int32_t tcps_connattempt
;
314 u_int32_t tcps_accepts
;
315 u_int32_t tcps_ecn_client_setup
;
316 u_int32_t tcps_ecn_server_setup
;
317 u_int32_t tcps_ecn_client_success
;
318 u_int32_t tcps_ecn_server_success
;
319 u_int32_t tcps_ecn_not_supported
;
320 u_int32_t tcps_ecn_lost_syn
;
321 u_int32_t tcps_ecn_lost_synack
;
322 u_int32_t tcps_ecn_recv_ce
;
323 u_int32_t tcps_ecn_recv_ece
;
324 u_int32_t tcps_ecn_sent_ece
;
325 u_int32_t tcps_ecn_conn_recv_ce
;
326 u_int32_t tcps_ecn_conn_recv_ece
;
327 u_int32_t tcps_ecn_conn_plnoce
;
328 u_int32_t tcps_ecn_conn_pl_ce
;
329 u_int32_t tcps_ecn_conn_nopl_ce
;
330 u_int32_t tcps_ecn_fallback_synloss
;
331 u_int32_t tcps_ecn_fallback_reorder
;
332 u_int32_t tcps_ecn_fallback_ce
;
334 /* TFO-related statistics */
335 u_int32_t tcps_tfo_syn_data_rcv
;
336 u_int32_t tcps_tfo_cookie_req_rcv
;
337 u_int32_t tcps_tfo_cookie_sent
;
338 u_int32_t tcps_tfo_cookie_invalid
;
339 u_int32_t tcps_tfo_cookie_req
;
340 u_int32_t tcps_tfo_cookie_rcv
;
341 u_int32_t tcps_tfo_syn_data_sent
;
342 u_int32_t tcps_tfo_syn_data_acked
;
343 u_int32_t tcps_tfo_syn_loss
;
344 u_int32_t tcps_tfo_blackhole
;
348 /* Returns true if the timer is on the timer list */
349 #define TIMER_IS_ON_LIST(tp) ((tp)->t_flags & TF_TIMER_ONLIST)
351 /* Run the TCP timerlist atleast once every hour */
352 #define TCP_TIMERLIST_MAX_OFFSET (60 * 60 * TCP_RETRANSHZ)
355 static void add_to_time_wait_locked(struct tcpcb
*tp
, uint32_t delay
);
356 static boolean_t
tcp_garbage_collect(struct inpcb
*, int);
359 * Add to tcp timewait list, delay is given in milliseconds.
362 add_to_time_wait_locked(struct tcpcb
*tp
, uint32_t delay
)
364 struct inpcbinfo
*pcbinfo
= &tcbinfo
;
365 struct inpcb
*inp
= tp
->t_inpcb
;
368 /* pcb list should be locked when we get here */
369 lck_rw_assert(pcbinfo
->ipi_lock
, LCK_RW_ASSERT_EXCLUSIVE
);
371 /* We may get here multiple times, so check */
372 if (!(inp
->inp_flags2
& INP2_TIMEWAIT
)) {
373 pcbinfo
->ipi_twcount
++;
374 inp
->inp_flags2
|= INP2_TIMEWAIT
;
376 /* Remove from global inp list */
377 LIST_REMOVE(inp
, inp_list
);
379 TAILQ_REMOVE(&tcp_tw_tailq
, tp
, t_twentry
);
382 /* Compute the time at which this socket can be closed */
383 timer
= tcp_now
+ delay
;
385 /* We will use the TCPT_2MSL timer for tracking this delay */
387 if (TIMER_IS_ON_LIST(tp
))
388 tcp_remove_timer(tp
);
389 tp
->t_timer
[TCPT_2MSL
] = timer
;
391 TAILQ_INSERT_TAIL(&tcp_tw_tailq
, tp
, t_twentry
);
395 add_to_time_wait(struct tcpcb
*tp
, uint32_t delay
)
397 struct inpcbinfo
*pcbinfo
= &tcbinfo
;
398 if (tp
->t_inpcb
->inp_socket
->so_options
& SO_NOWAKEFROMSLEEP
)
399 socket_post_kev_msg_closed(tp
->t_inpcb
->inp_socket
);
401 /* 19182803: Notify nstat that connection is closing before waiting. */
402 nstat_pcb_detach(tp
->t_inpcb
);
404 if (!lck_rw_try_lock_exclusive(pcbinfo
->ipi_lock
)) {
405 tcp_unlock(tp
->t_inpcb
->inp_socket
, 0, 0);
406 lck_rw_lock_exclusive(pcbinfo
->ipi_lock
);
407 tcp_lock(tp
->t_inpcb
->inp_socket
, 0, 0);
409 add_to_time_wait_locked(tp
, delay
);
410 lck_rw_done(pcbinfo
->ipi_lock
);
412 inpcb_gc_sched(pcbinfo
, INPCB_TIMER_LAZY
);
415 /* If this is on time wait queue, remove it. */
417 tcp_remove_from_time_wait(struct inpcb
*inp
)
419 struct tcpcb
*tp
= intotcpcb(inp
);
420 if (inp
->inp_flags2
& INP2_TIMEWAIT
)
421 TAILQ_REMOVE(&tcp_tw_tailq
, tp
, t_twentry
);
425 tcp_garbage_collect(struct inpcb
*inp
, int istimewait
)
427 boolean_t active
= FALSE
;
431 so
= inp
->inp_socket
;
435 * Skip if still in use or busy; it would have been more efficient
436 * if we were to test so_usecount against 0, but this isn't possible
437 * due to the current implementation of tcp_dropdropablreq() where
438 * overflow sockets that are eligible for garbage collection have
439 * their usecounts set to 1.
441 if (!lck_mtx_try_lock_spin(&inp
->inpcb_mtx
))
444 /* Check again under the lock */
445 if (so
->so_usecount
> 1) {
446 if (inp
->inp_wantcnt
== WNT_STOPUSING
)
448 lck_mtx_unlock(&inp
->inpcb_mtx
);
453 TSTMP_GEQ(tcp_now
, tp
->t_timer
[TCPT_2MSL
]) &&
454 tp
->t_state
!= TCPS_CLOSED
) {
455 /* Become a regular mutex */
456 lck_mtx_convert_spin(&inp
->inpcb_mtx
);
461 * Overflowed socket dropped from the listening queue? Do this
462 * only if we are called to clean up the time wait slots, since
463 * tcp_dropdropablreq() considers a socket to have been fully
464 * dropped after add_to_time_wait() is finished.
465 * Also handle the case of connections getting closed by the peer
466 * while in the queue as seen with rdar://6422317
469 if (so
->so_usecount
== 1 &&
470 ((istimewait
&& (so
->so_flags
& SOF_OVERFLOW
)) ||
471 ((tp
!= NULL
) && (tp
->t_state
== TCPS_CLOSED
) &&
472 (so
->so_head
!= NULL
) &&
473 ((so
->so_state
& (SS_INCOMP
|SS_CANTSENDMORE
|SS_CANTRCVMORE
)) ==
474 (SS_INCOMP
|SS_CANTSENDMORE
|SS_CANTRCVMORE
))))) {
476 if (inp
->inp_state
!= INPCB_STATE_DEAD
) {
477 /* Become a regular mutex */
478 lck_mtx_convert_spin(&inp
->inpcb_mtx
);
480 if (SOCK_CHECK_DOM(so
, PF_INET6
))
487 if (inp
->inp_wantcnt
== WNT_STOPUSING
)
489 lck_mtx_unlock(&inp
->inpcb_mtx
);
491 } else if (inp
->inp_wantcnt
!= WNT_STOPUSING
) {
492 lck_mtx_unlock(&inp
->inpcb_mtx
);
497 * We get here because the PCB is no longer searchable
498 * (WNT_STOPUSING); detach (if needed) and dispose if it is dead
499 * (usecount is 0). This covers all cases, including overflow
500 * sockets and those that are considered as "embryonic",
501 * i.e. created by sonewconn() in TCP input path, and have
502 * not yet been committed. For the former, we reduce the usecount
503 * to 0 as done by the code above. For the latter, the usecount
504 * would have reduced to 0 as part calling soabort() when the
505 * socket is dropped at the end of tcp_input().
507 if (so
->so_usecount
== 0) {
508 DTRACE_TCP4(state__change
, void, NULL
, struct inpcb
*, inp
,
509 struct tcpcb
*, tp
, int32_t, TCPS_CLOSED
);
510 /* Become a regular mutex */
511 lck_mtx_convert_spin(&inp
->inpcb_mtx
);
514 * If this tp still happens to be on the timer list,
517 if (TIMER_IS_ON_LIST(tp
)) {
518 tcp_remove_timer(tp
);
521 if (inp
->inp_state
!= INPCB_STATE_DEAD
) {
523 if (SOCK_CHECK_DOM(so
, PF_INET6
))
533 lck_mtx_unlock(&inp
->inpcb_mtx
);
538 * TCP garbage collector callback (inpcb_timer_func_t).
540 * Returns the number of pcbs that will need to be gc-ed soon,
541 * returnining > 0 will keep timer active.
544 tcp_gc(struct inpcbinfo
*ipi
)
546 struct inpcb
*inp
, *nxt
;
547 struct tcpcb
*tw_tp
, *tw_ntp
;
552 static int tws_checked
= 0;
555 KERNEL_DEBUG(DBG_FNC_TCP_SLOW
| DBG_FUNC_START
, 0, 0, 0, 0, 0);
558 * Update tcp_now here as it may get used while
559 * processing the slow timer.
561 calculate_tcp_clock();
564 * Garbage collect socket/tcpcb: We need to acquire the list lock
565 * exclusively to do this
568 if (lck_rw_try_lock_exclusive(ipi
->ipi_lock
) == FALSE
) {
569 /* don't sweat it this time; cleanup was done last time */
570 if (tcp_gc_done
== TRUE
) {
572 KERNEL_DEBUG(DBG_FNC_TCP_SLOW
| DBG_FUNC_END
,
573 tws_checked
, cur_tw_slot
, 0, 0, 0);
574 /* Lock upgrade failed, give up this round */
575 atomic_add_32(&ipi
->ipi_gc_req
.intimer_fast
, 1);
578 /* Upgrade failed, lost lock now take it again exclusive */
579 lck_rw_lock_exclusive(ipi
->ipi_lock
);
583 LIST_FOREACH_SAFE(inp
, &tcb
, inp_list
, nxt
) {
584 if (tcp_garbage_collect(inp
, 0))
585 atomic_add_32(&ipi
->ipi_gc_req
.intimer_fast
, 1);
588 /* Now cleanup the time wait ones */
589 TAILQ_FOREACH_SAFE(tw_tp
, &tcp_tw_tailq
, t_twentry
, tw_ntp
) {
591 * We check the timestamp here without holding the
592 * socket lock for better performance. If there are
593 * any pcbs in time-wait, the timer will get rescheduled.
594 * Hence some error in this check can be tolerated.
596 * Sometimes a socket on time-wait queue can be closed if
597 * 2MSL timer expired but the application still has a
600 if (tw_tp
->t_state
== TCPS_CLOSED
||
601 TSTMP_GEQ(tcp_now
, tw_tp
->t_timer
[TCPT_2MSL
])) {
602 if (tcp_garbage_collect(tw_tp
->t_inpcb
, 1))
603 atomic_add_32(&ipi
->ipi_gc_req
.intimer_lazy
, 1);
607 /* take into account pcbs that are still in time_wait_slots */
608 atomic_add_32(&ipi
->ipi_gc_req
.intimer_lazy
, ipi
->ipi_twcount
);
610 lck_rw_done(ipi
->ipi_lock
);
612 /* Clean up the socache while we are here */
613 if (so_cache_timer())
614 atomic_add_32(&ipi
->ipi_gc_req
.intimer_lazy
, 1);
616 KERNEL_DEBUG(DBG_FNC_TCP_SLOW
| DBG_FUNC_END
, tws_checked
,
617 cur_tw_slot
, 0, 0, 0);
623 * Cancel all timers for TCP tp.
631 tcp_remove_timer(tp
);
632 for (i
= 0; i
< TCPT_NTIMERS
; i
++)
634 tp
->tentry
.timer_start
= tcp_now
;
635 tp
->tentry
.index
= TCPT_NONE
;
638 int tcp_syn_backoff
[TCP_MAXRXTSHIFT
+ 1] =
639 { 1, 1, 1, 1, 1, 2, 4, 8, 16, 32, 64, 64, 64 };
641 int tcp_backoff
[TCP_MAXRXTSHIFT
+ 1] =
642 { 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64 };
644 static int tcp_totbackoff
= 511; /* sum of tcp_backoff[] */
646 void tcp_rexmt_save_state(struct tcpcb
*tp
)
649 if (TSTMP_SUPPORTED(tp
)) {
651 * Since timestamps are supported on the connection,
652 * we can do recovery as described in rfc 4015.
654 fsize
= tp
->snd_max
- tp
->snd_una
;
655 tp
->snd_ssthresh_prev
= max(fsize
, tp
->snd_ssthresh
);
656 tp
->snd_recover_prev
= tp
->snd_recover
;
659 * Timestamp option is not supported on this connection.
660 * Record ssthresh and cwnd so they can
661 * be recovered if this turns out to be a "bad" retransmit.
662 * A retransmit is considered "bad" if an ACK for this
663 * segment is received within RTT/2 interval; the assumption
664 * here is that the ACK was already in flight. See
665 * "On Estimating End-to-End Network Path Properties" by
666 * Allman and Paxson for more details.
668 tp
->snd_cwnd_prev
= tp
->snd_cwnd
;
669 tp
->snd_ssthresh_prev
= tp
->snd_ssthresh
;
670 tp
->snd_recover_prev
= tp
->snd_recover
;
671 if (IN_FASTRECOVERY(tp
))
672 tp
->t_flags
|= TF_WASFRECOVERY
;
674 tp
->t_flags
&= ~TF_WASFRECOVERY
;
676 tp
->t_srtt_prev
= (tp
->t_srtt
>> TCP_RTT_SHIFT
) + 2;
677 tp
->t_rttvar_prev
= (tp
->t_rttvar
>> TCP_RTTVAR_SHIFT
);
678 tp
->t_flagsext
&= ~(TF_RECOMPUTE_RTT
);
682 * Revert to the older segment size if there is an indication that PMTU
683 * blackhole detection was not needed.
685 void tcp_pmtud_revert_segment_size(struct tcpcb
*tp
)
689 VERIFY(tp
->t_pmtud_saved_maxopd
> 0);
690 tp
->t_flags
|= TF_PMTUD
;
691 tp
->t_flags
&= ~TF_BLACKHOLE
;
692 optlen
= tp
->t_maxopd
- tp
->t_maxseg
;
693 tp
->t_maxopd
= tp
->t_pmtud_saved_maxopd
;
694 tp
->t_maxseg
= tp
->t_maxopd
- optlen
;
696 * Reset the slow-start flight size as it
697 * may depend on the new MSS
699 if (CC_ALGO(tp
)->cwnd_init
!= NULL
)
700 CC_ALGO(tp
)->cwnd_init(tp
);
701 tp
->t_pmtud_start_ts
= 0;
702 tcpstat
.tcps_pmtudbh_reverted
++;
706 * TCP timer processing.
709 tcp_timers(tp
, timer
)
710 register struct tcpcb
*tp
;
713 int32_t rexmt
, optlen
= 0, idle_time
= 0;
715 struct tcptemp
*t_template
;
721 int isipv6
= (tp
->t_inpcb
->inp_vflag
& INP_IPV4
) == 0;
723 u_int64_t accsleep_ms
;
724 u_int32_t last_sleep_ms
= 0;
726 so
= tp
->t_inpcb
->inp_socket
;
727 idle_time
= tcp_now
- tp
->t_rcvtime
;
732 * 2 MSL timeout in shutdown went off. If we're closed but
733 * still waiting for peer to close and connection has been idle
734 * too long, or if 2MSL time is up from TIME_WAIT or FIN_WAIT_2,
735 * delete connection control block.
736 * Otherwise, (this case shouldn't happen) check again in a bit
737 * we keep the socket in the main list in that case.
740 tcp_free_sackholes(tp
);
741 if (tp
->t_state
!= TCPS_TIME_WAIT
&&
742 tp
->t_state
!= TCPS_FIN_WAIT_2
&&
743 ((idle_time
> 0) && (idle_time
< TCP_CONN_MAXIDLE(tp
)))) {
744 tp
->t_timer
[TCPT_2MSL
] = OFFSET_FROM_START(tp
,
745 (u_int32_t
)TCP_CONN_KEEPINTVL(tp
));
753 * Retransmission timer went off. Message has not
754 * been acked within retransmit interval. Back off
755 * to a longer retransmit interval and retransmit one segment.
758 accsleep_ms
= mach_absolutetime_asleep
/ 1000000UL;
759 if (accsleep_ms
> tp
->t_accsleep_ms
)
760 last_sleep_ms
= accsleep_ms
- tp
->t_accsleep_ms
;
762 * Drop a connection in the retransmit timer
763 * 1. If we have retransmitted more than TCP_MAXRXTSHIFT
765 * 2. If the time spent in this retransmission episode is
766 * more than the time limit set with TCP_RXT_CONNDROPTIME
768 * 3. If TCP_RXT_FINDROP socket option was set and
769 * we have already retransmitted the FIN 3 times without
772 if (++tp
->t_rxtshift
> TCP_MAXRXTSHIFT
||
773 (tp
->t_rxt_conndroptime
> 0 && tp
->t_rxtstart
> 0 &&
774 (tcp_now
- tp
->t_rxtstart
) >= tp
->t_rxt_conndroptime
) ||
775 ((tp
->t_flagsext
& TF_RXTFINDROP
) != 0 &&
776 (tp
->t_flags
& TF_SENTFIN
) != 0 && tp
->t_rxtshift
>= 4) ||
777 (tp
->t_rxtshift
> 4 && last_sleep_ms
>= TCP_SLEEP_TOO_LONG
)) {
778 if ((tp
->t_flagsext
& TF_RXTFINDROP
) != 0) {
779 tcpstat
.tcps_rxtfindrop
++;
780 } else if (last_sleep_ms
>= TCP_SLEEP_TOO_LONG
) {
781 tcpstat
.tcps_drop_after_sleep
++;
783 tcpstat
.tcps_timeoutdrop
++;
785 if (tp
->t_rxtshift
>= TCP_MAXRXTSHIFT
) {
786 if (TCP_ECN_ENABLED(tp
)) {
787 INP_INC_IFNET_STAT(tp
->t_inpcb
,
790 INP_INC_IFNET_STAT(tp
->t_inpcb
,
794 tp
->t_rxtshift
= TCP_MAXRXTSHIFT
;
795 postevent(so
, 0, EV_TIMEOUT
);
797 (SO_FILT_HINT_LOCKED
|SO_FILT_HINT_TIMEOUT
));
798 tp
= tcp_drop(tp
, tp
->t_softerror
?
799 tp
->t_softerror
: ETIMEDOUT
);
804 tcpstat
.tcps_rexmttimeo
++;
805 tp
->t_accsleep_ms
= accsleep_ms
;
807 if (tp
->t_rxtshift
== 1 &&
808 tp
->t_state
== TCPS_ESTABLISHED
) {
809 /* Set the time at which retransmission started. */
810 tp
->t_rxtstart
= tcp_now
;
813 * if this is the first retransmit timeout, save
814 * the state so that we can recover if the timeout
817 tcp_rexmt_save_state(tp
);
820 if ((tp
->t_rxtshift
>= mptcp_fail_thresh
) &&
821 (tp
->t_state
== TCPS_ESTABLISHED
) &&
822 (tp
->t_mpflags
& TMPF_MPTCP_TRUE
)) {
823 mptcp_act_on_txfail(so
);
828 if (tp
->t_adaptive_wtimo
> 0 &&
829 tp
->t_rxtshift
> tp
->t_adaptive_wtimo
&&
830 TCPS_HAVEESTABLISHED(tp
->t_state
)) {
831 /* Send an event to the application */
833 (SO_FILT_HINT_LOCKED
|
834 SO_FILT_HINT_ADAPTIVE_WTIMO
));
838 * If this is a retransmit timeout after PTO, the PTO
841 if (tp
->t_flagsext
& TF_SENT_TLPROBE
) {
842 tp
->t_flagsext
&= ~(TF_SENT_TLPROBE
);
843 tcpstat
.tcps_rto_after_pto
++;
846 if (tp
->t_flagsext
& TF_DELAY_RECOVERY
) {
848 * Retransmit timer fired before entering recovery
849 * on a connection with packet re-ordering. This
850 * suggests that the reordering metrics computed
853 tp
->t_reorderwin
= 0;
854 tp
->t_timer
[TCPT_DELAYFR
] = 0;
855 tp
->t_flagsext
&= ~(TF_DELAY_RECOVERY
);
858 if (tp
->t_state
== TCPS_SYN_RECEIVED
)
861 if ((tp
->t_tfo_stats
& TFO_S_SYN_DATA_SENT
) &&
862 !(tp
->t_tfo_flags
& TFO_F_NO_SNDPROBING
) &&
863 ((tp
->t_state
!= TCPS_SYN_SENT
&& tp
->t_rxtshift
> 1) ||
864 tp
->t_rxtshift
> 2)) {
866 * For regular retransmissions, a first one is being
867 * done for tail-loss probe.
868 * Thus, if rxtshift > 1, this means we have sent the segment
869 * a total of 3 times.
871 * If we are in SYN-SENT state, then there is no tail-loss
872 * probe thus we have to let rxtshift go up to 3.
874 tcp_heuristic_tfo_middlebox(tp
);
876 so
->so_error
= ENODATA
;
881 if (tp
->t_state
== TCPS_SYN_SENT
) {
882 rexmt
= TCP_REXMTVAL(tp
) * tcp_syn_backoff
[tp
->t_rxtshift
];
883 tp
->t_stat
.synrxtshift
= tp
->t_rxtshift
;
885 /* When retransmitting, disable TFO */
886 if (tfo_enabled(tp
)) {
887 tp
->t_flagsext
&= ~TF_FASTOPEN
;
888 tp
->t_tfo_flags
|= TFO_F_SYN_LOSS
;
890 tp
->t_tfo_stats
|= TFO_S_SYN_LOSS
;
891 tcpstat
.tcps_tfo_syn_loss
++;
894 rexmt
= TCP_REXMTVAL(tp
) * tcp_backoff
[tp
->t_rxtshift
];
897 TCPT_RANGESET(tp
->t_rxtcur
, rexmt
, tp
->t_rttmin
, TCPTV_REXMTMAX
,
898 TCP_ADD_REXMTSLOP(tp
));
899 tp
->t_timer
[TCPT_REXMT
] = OFFSET_FROM_START(tp
, tp
->t_rxtcur
);
901 if (INP_WAIT_FOR_IF_FEEDBACK(tp
->t_inpcb
))
904 tcp_free_sackholes(tp
);
906 * Check for potential Path MTU Discovery Black Hole
908 if (tcp_pmtud_black_hole_detect
&&
909 !(tp
->t_flagsext
& TF_NOBLACKHOLE_DETECTION
) &&
910 (tp
->t_state
== TCPS_ESTABLISHED
)) {
911 if ((tp
->t_flags
& TF_PMTUD
) &&
912 ((tp
->t_flags
& TF_MAXSEGSNT
)
913 || tp
->t_pmtud_lastseg_size
> tcp_pmtud_black_hole_mss
) &&
914 tp
->t_rxtshift
== 2) {
916 * Enter Path MTU Black-hole Detection mechanism:
917 * - Disable Path MTU Discovery (IP "DF" bit).
918 * - Reduce MTU to lower value than what we
919 * negotiated with the peer.
921 /* Disable Path MTU Discovery for now */
922 tp
->t_flags
&= ~TF_PMTUD
;
923 /* Record that we may have found a black hole */
924 tp
->t_flags
|= TF_BLACKHOLE
;
925 optlen
= tp
->t_maxopd
- tp
->t_maxseg
;
926 /* Keep track of previous MSS */
927 tp
->t_pmtud_saved_maxopd
= tp
->t_maxopd
;
928 tp
->t_pmtud_start_ts
= tcp_now
;
929 if (tp
->t_pmtud_start_ts
== 0)
930 tp
->t_pmtud_start_ts
++;
931 /* Reduce the MSS to intermediary value */
932 if (tp
->t_maxopd
> tcp_pmtud_black_hole_mss
) {
933 tp
->t_maxopd
= tcp_pmtud_black_hole_mss
;
935 tp
->t_maxopd
= /* use the default MSS */
937 isipv6
? tcp_v6mssdflt
:
941 tp
->t_maxseg
= tp
->t_maxopd
- optlen
;
944 * Reset the slow-start flight size
945 * as it may depend on the new MSS
947 if (CC_ALGO(tp
)->cwnd_init
!= NULL
)
948 CC_ALGO(tp
)->cwnd_init(tp
);
951 * If further retransmissions are still
952 * unsuccessful with a lowered MTU, maybe this
953 * isn't a Black Hole and we restore the previous
954 * MSS and blackhole detection flags.
958 if ((tp
->t_flags
& TF_BLACKHOLE
) &&
959 (tp
->t_rxtshift
> 4)) {
960 tcp_pmtud_revert_segment_size(tp
);
967 * Disable rfc1323 and rfc1644 if we haven't got any
968 * response to our SYN (after we reach the threshold)
969 * to work-around some broken terminal servers (most of
970 * which have hopefully been retired) that have bad VJ
971 * header compression code which trashes TCP segments
972 * containing unknown-to-them TCP options.
973 * Do this only on non-local connections.
975 if (tp
->t_state
== TCPS_SYN_SENT
&&
976 tp
->t_rxtshift
== tcp_broken_peer_syn_rxmit_thres
)
977 tp
->t_flags
&= ~(TF_REQ_SCALE
|TF_REQ_TSTMP
|TF_REQ_CC
);
980 * If losing, let the lower level know and try for
981 * a better route. Also, if we backed off this far,
982 * our srtt estimate is probably bogus. Clobber it
983 * so we'll take the next rtt measurement as our srtt;
984 * move the current srtt into rttvar to keep the current
985 * retransmit times until then.
987 if (tp
->t_rxtshift
> TCP_MAXRXTSHIFT
/ 4) {
990 in6_losing(tp
->t_inpcb
);
993 in_losing(tp
->t_inpcb
);
994 tp
->t_rttvar
+= (tp
->t_srtt
>> TCP_RTT_SHIFT
);
997 tp
->snd_nxt
= tp
->snd_una
;
999 * Note: We overload snd_recover to function also as the
1000 * snd_last variable described in RFC 2582
1002 tp
->snd_recover
= tp
->snd_max
;
1004 * Force a segment to be sent.
1006 tp
->t_flags
|= TF_ACKNOW
;
1008 /* If timing a segment in this window, stop the timer */
1011 if (!IN_FASTRECOVERY(tp
) && tp
->t_rxtshift
== 1)
1012 tcpstat
.tcps_tailloss_rto
++;
1016 * RFC 5681 says: when a TCP sender detects segment loss
1017 * using retransmit timer and the given segment has already
1018 * been retransmitted by way of the retransmission timer at
1019 * least once, the value of ssthresh is held constant
1021 if (tp
->t_rxtshift
== 1 &&
1022 CC_ALGO(tp
)->after_timeout
!= NULL
) {
1023 CC_ALGO(tp
)->after_timeout(tp
);
1025 * CWR notifications are to be sent on new data
1026 * right after Fast Retransmits and ECE
1027 * notification receipts.
1029 if (TCP_ECN_ENABLED(tp
))
1030 tp
->ecn_flags
|= TE_SENDCWR
;
1033 EXIT_FASTRECOVERY(tp
);
1035 /* Exit cwnd non validated phase */
1036 tp
->t_flagsext
&= ~TF_CWND_NONVALIDATED
;
1040 tcp_ccdbg_trace(tp
, NULL
, TCP_CC_REXMT_TIMEOUT
);
1042 (void) tcp_output(tp
);
1046 * Persistance timer into zero window.
1047 * Force a byte to be output, if possible.
1050 tcpstat
.tcps_persisttimeo
++;
1052 * Hack: if the peer is dead/unreachable, we do not
1053 * time out if the window is closed. After a full
1054 * backoff, drop the connection if the idle time
1055 * (no responses to probes) reaches the maximum
1056 * backoff that we would use if retransmitting.
1058 * Drop the connection if we reached the maximum allowed time for
1059 * Zero Window Probes without a non-zero update from the peer.
1060 * See rdar://5805356
1062 if ((tp
->t_rxtshift
== TCP_MAXRXTSHIFT
&&
1063 (idle_time
>= tcp_maxpersistidle
||
1064 idle_time
>= TCP_REXMTVAL(tp
) * tcp_totbackoff
)) ||
1065 ((tp
->t_persist_stop
!= 0) &&
1066 TSTMP_LEQ(tp
->t_persist_stop
, tcp_now
))) {
1067 tcpstat
.tcps_persistdrop
++;
1068 postevent(so
, 0, EV_TIMEOUT
);
1070 (SO_FILT_HINT_LOCKED
|SO_FILT_HINT_TIMEOUT
));
1071 tp
= tcp_drop(tp
, ETIMEDOUT
);
1075 tp
->t_flagsext
|= TF_FORCE
;
1076 (void) tcp_output(tp
);
1077 tp
->t_flagsext
&= ~TF_FORCE
;
1081 * Keep-alive timer went off; send something
1082 * or drop connection if idle for too long.
1085 tcpstat
.tcps_keeptimeo
++;
1088 * Regular TCP connections do not send keepalives after closing
1089 * MPTCP must not also, after sending Data FINs.
1091 struct mptcb
*mp_tp
= tp
->t_mptcb
;
1092 if ((tp
->t_mpflags
& TMPF_MPTCP_TRUE
) &&
1093 (tp
->t_state
> TCPS_ESTABLISHED
)) {
1095 } else if (mp_tp
!= NULL
) {
1096 if ((mptcp_ok_to_keepalive(mp_tp
) == 0))
1100 if (tp
->t_state
< TCPS_ESTABLISHED
)
1102 if ((always_keepalive
||
1103 (tp
->t_inpcb
->inp_socket
->so_options
& SO_KEEPALIVE
) ||
1104 (tp
->t_flagsext
& TF_DETECT_READSTALL
) ||
1105 (tp
->t_tfo_probe_state
== TFO_PROBE_PROBING
)) &&
1106 (tp
->t_state
<= TCPS_CLOSING
|| tp
->t_state
== TCPS_FIN_WAIT_2
)) {
1107 if (idle_time
>= TCP_CONN_KEEPIDLE(tp
) + TCP_CONN_MAXIDLE(tp
))
1110 * Send a packet designed to force a response
1111 * if the peer is up and reachable:
1112 * either an ACK if the connection is still alive,
1113 * or an RST if the peer has closed the connection
1114 * due to timeout or reboot.
1115 * Using sequence number tp->snd_una-1
1116 * causes the transmitted zero-length segment
1117 * to lie outside the receive window;
1118 * by the protocol spec, this requires the
1119 * correspondent TCP to respond.
1121 tcpstat
.tcps_keepprobe
++;
1122 t_template
= tcp_maketemplate(tp
);
1124 struct inpcb
*inp
= tp
->t_inpcb
;
1125 struct tcp_respond_args tra
;
1127 bzero(&tra
, sizeof(tra
));
1128 tra
.nocell
= INP_NO_CELLULAR(inp
);
1129 tra
.noexpensive
= INP_NO_EXPENSIVE(inp
);
1130 tra
.awdl_unrestricted
= INP_AWDL_UNRESTRICTED(inp
);
1131 if (tp
->t_inpcb
->inp_flags
& INP_BOUND_IF
)
1132 tra
.ifscope
= tp
->t_inpcb
->inp_boundifp
->if_index
;
1134 tra
.ifscope
= IFSCOPE_NONE
;
1135 tcp_respond(tp
, t_template
->tt_ipgen
,
1136 &t_template
->tt_t
, (struct mbuf
*)NULL
,
1137 tp
->rcv_nxt
, tp
->snd_una
- 1, 0, &tra
);
1138 (void) m_free(dtom(t_template
));
1139 if (tp
->t_flagsext
& TF_DETECT_READSTALL
)
1140 tp
->t_rtimo_probes
++;
1142 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
,
1143 TCP_CONN_KEEPINTVL(tp
));
1145 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
,
1146 TCP_CONN_KEEPIDLE(tp
));
1148 if (tp
->t_flagsext
& TF_DETECT_READSTALL
) {
1149 struct ifnet
*outifp
= tp
->t_inpcb
->inp_last_outifp
;
1150 bool reenable_probe
= false;
1152 * The keep alive packets sent to detect a read
1153 * stall did not get a response from the
1154 * peer. Generate more keep-alives to confirm this.
1155 * If the number of probes sent reaches the limit,
1156 * generate an event.
1158 if (tp
->t_adaptive_rtimo
> 0) {
1159 if (tp
->t_rtimo_probes
> tp
->t_adaptive_rtimo
) {
1160 /* Generate an event */
1162 (SO_FILT_HINT_LOCKED
|
1163 SO_FILT_HINT_ADAPTIVE_RTIMO
));
1164 tcp_keepalive_reset(tp
);
1166 reenable_probe
= true;
1168 } else if (outifp
!= NULL
&&
1169 (outifp
->if_eflags
& IFEF_PROBE_CONNECTIVITY
) &&
1170 tp
->t_rtimo_probes
<= TCP_CONNECTIVITY_PROBES_MAX
) {
1171 reenable_probe
= true;
1173 tp
->t_flagsext
&= ~TF_DETECT_READSTALL
;
1175 if (reenable_probe
) {
1176 int ind
= min(tp
->t_rtimo_probes
,
1178 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(
1179 tp
, tcp_backoff
[ind
] * TCP_REXMTVAL(tp
));
1182 if (tp
->t_tfo_probe_state
== TFO_PROBE_PROBING
) {
1186 ind
= min(tp
->t_tfo_probes
, TCP_MAXRXTSHIFT
);
1189 * We take the minimum among the time set by true
1190 * keepalive (see above) and the backoff'd RTO. That
1191 * way we backoff in case of packet-loss but will never
1192 * timeout slower than regular keepalive due to the
1195 tp
->t_timer
[TCPT_KEEP
] = min(OFFSET_FROM_START(
1196 tp
, tcp_backoff
[ind
] * TCP_REXMTVAL(tp
)),
1197 tp
->t_timer
[TCPT_KEEP
]);
1198 } else if (tp
->t_tfo_probe_state
== TFO_PROBE_WAIT_DATA
) {
1199 /* Still no data! Let's assume a TFO-error and err out... */
1200 tcp_heuristic_tfo_middlebox(tp
);
1202 so
->so_error
= ENODATA
;
1204 tcpstat
.tcps_tfo_blackhole
++;
1208 if (tcp_delack_enabled
&& (tp
->t_flags
& TF_DELACK
)) {
1209 tp
->t_flags
&= ~TF_DELACK
;
1210 tp
->t_timer
[TCPT_DELACK
] = 0;
1211 tp
->t_flags
|= TF_ACKNOW
;
1214 * If delayed ack timer fired while stretching
1215 * acks, count the number of times the streaming
1216 * detection was not correct. If this exceeds a
1217 * threshold, disable strech ack on this
1220 * Also, go back to acking every other packet.
1222 if ((tp
->t_flags
& TF_STRETCHACK
)) {
1223 if (tp
->t_unacksegs
> 1 &&
1224 tp
->t_unacksegs
< maxseg_unacked
)
1225 tp
->t_stretchack_delayed
++;
1227 if (tp
->t_stretchack_delayed
>
1228 TCP_STRETCHACK_DELAY_THRESHOLD
) {
1229 tp
->t_flagsext
|= TF_DISABLE_STRETCHACK
;
1231 * Note the time at which stretch
1232 * ack was disabled automatically
1234 tp
->rcv_nostrack_ts
= tcp_now
;
1235 tcpstat
.tcps_nostretchack
++;
1236 tp
->t_stretchack_delayed
= 0;
1238 tcp_reset_stretch_ack(tp
);
1242 * If we are measuring inter packet arrival jitter
1243 * for throttling a connection, this delayed ack
1244 * might be the reason for accumulating some
1245 * jitter. So let's restart the measurement.
1247 CLEAR_IAJ_STATE(tp
);
1249 tcpstat
.tcps_delack
++;
1250 (void) tcp_output(tp
);
1255 case TCPT_JACK_RXMT
:
1256 if ((tp
->t_state
== TCPS_ESTABLISHED
) &&
1257 (tp
->t_mpflags
& TMPF_PREESTABLISHED
) &&
1258 (tp
->t_mpflags
& TMPF_JOINED_FLOW
)) {
1259 if (++tp
->t_mprxtshift
> TCP_MAXRXTSHIFT
) {
1260 tcpstat
.tcps_timeoutdrop
++;
1261 postevent(so
, 0, EV_TIMEOUT
);
1263 (SO_FILT_HINT_LOCKED
|
1264 SO_FILT_HINT_TIMEOUT
));
1265 tp
= tcp_drop(tp
, tp
->t_softerror
?
1266 tp
->t_softerror
: ETIMEDOUT
);
1269 tcpstat
.tcps_join_rxmts
++;
1270 tp
->t_flags
|= TF_ACKNOW
;
1273 * No backoff is implemented for simplicity for this
1276 (void) tcp_output(tp
);
1284 tp
->t_flagsext
&= ~(TF_SENT_TLPROBE
);
1287 * Check if the connection is in the right state to
1290 if (tp
->t_state
!= TCPS_ESTABLISHED
||
1291 (tp
->t_rxtshift
> 0 && !(tp
->t_flagsext
& TF_PROBING
))
1292 || tp
->snd_max
== tp
->snd_una
||
1293 !SACK_ENABLED(tp
) || !TAILQ_EMPTY(&tp
->snd_holes
) ||
1294 IN_FASTRECOVERY(tp
))
1298 * If there is no new data to send or if the
1299 * connection is limited by receive window then
1300 * retransmit the last segment, otherwise send
1303 snd_len
= min(so
->so_snd
.sb_cc
, tp
->snd_wnd
)
1304 - (tp
->snd_max
- tp
->snd_una
);
1306 tp
->snd_nxt
= tp
->snd_max
;
1308 snd_len
= min((tp
->snd_max
- tp
->snd_una
),
1310 tp
->snd_nxt
= tp
->snd_max
- snd_len
;
1314 if (tp
->t_flagsext
& TF_PROBING
)
1315 tcpstat
.tcps_probe_if
++;
1317 /* If timing a segment in this window, stop the timer */
1319 /* Note that tail loss probe is being sent */
1320 tp
->t_flagsext
|= TF_SENT_TLPROBE
;
1321 tp
->t_tlpstart
= tcp_now
;
1323 tp
->snd_cwnd
+= tp
->t_maxseg
;
1324 (void )tcp_output(tp
);
1325 tp
->snd_cwnd
-= tp
->t_maxseg
;
1327 tp
->t_tlphighrxt
= tp
->snd_nxt
;
1331 tp
->t_flagsext
&= ~TF_DELAY_RECOVERY
;
1334 * Don't do anything if one of the following is true:
1335 * - the connection is already in recovery
1336 * - sequence until snd_recover has been acknowledged.
1337 * - retransmit timeout has fired
1339 if (IN_FASTRECOVERY(tp
) ||
1340 SEQ_GEQ(tp
->snd_una
, tp
->snd_recover
) ||
1344 VERIFY(SACK_ENABLED(tp
));
1345 tcp_rexmt_save_state(tp
);
1346 if (CC_ALGO(tp
)->pre_fr
!= NULL
) {
1347 CC_ALGO(tp
)->pre_fr(tp
);
1348 if (TCP_ECN_ENABLED(tp
))
1349 tp
->ecn_flags
|= TE_SENDCWR
;
1351 ENTER_FASTRECOVERY(tp
);
1353 tp
->t_timer
[TCPT_REXMT
] = 0;
1354 tcpstat
.tcps_sack_recovery_episode
++;
1355 tp
->t_sack_recovery_episode
++;
1356 tp
->sack_newdata
= tp
->snd_nxt
;
1357 tp
->snd_cwnd
= tp
->t_maxseg
;
1358 tcp_ccdbg_trace(tp
, NULL
, TCP_CC_ENTER_FASTRECOVERY
);
1359 (void) tcp_output(tp
);
1362 tcpstat
.tcps_keepdrops
++;
1363 postevent(so
, 0, EV_TIMEOUT
);
1365 (SO_FILT_HINT_LOCKED
|SO_FILT_HINT_TIMEOUT
));
1366 tp
= tcp_drop(tp
, ETIMEDOUT
);
1370 if (tp
->t_inpcb
->inp_socket
->so_options
& SO_DEBUG
)
1371 tcp_trace(TA_USER
, ostate
, tp
, (void *)0, (struct tcphdr
*)0,
1377 /* Remove a timer entry from timer list */
1379 tcp_remove_timer(struct tcpcb
*tp
)
1381 struct tcptimerlist
*listp
= &tcp_timer_list
;
1383 lck_mtx_assert(&tp
->t_inpcb
->inpcb_mtx
, LCK_MTX_ASSERT_OWNED
);
1384 if (!(TIMER_IS_ON_LIST(tp
))) {
1387 lck_mtx_lock(listp
->mtx
);
1389 /* Check if pcb is on timer list again after acquiring the lock */
1390 if (!(TIMER_IS_ON_LIST(tp
))) {
1391 lck_mtx_unlock(listp
->mtx
);
1395 if (listp
->next_te
!= NULL
&& listp
->next_te
== &tp
->tentry
)
1396 listp
->next_te
= LIST_NEXT(&tp
->tentry
, le
);
1398 LIST_REMOVE(&tp
->tentry
, le
);
1399 tp
->t_flags
&= ~(TF_TIMER_ONLIST
);
1403 tp
->tentry
.le
.le_next
= NULL
;
1404 tp
->tentry
.le
.le_prev
= NULL
;
1405 lck_mtx_unlock(listp
->mtx
);
1409 * Function to check if the timerlist needs to be rescheduled to run
1410 * the timer entry correctly. Basically, this is to check if we can avoid
1411 * taking the list lock.
1415 need_to_resched_timerlist(u_int32_t runtime
, u_int16_t mode
)
1417 struct tcptimerlist
*listp
= &tcp_timer_list
;
1421 * If the list is being processed then the state of the list is
1422 * in flux. In this case always acquire the lock and set the state
1428 if (!listp
->scheduled
)
1431 diff
= timer_diff(listp
->runtime
, 0, runtime
, 0);
1433 /* The list is going to run before this timer */
1436 if (mode
& TCP_TIMERLIST_10MS_MODE
) {
1437 if (diff
<= TCP_TIMER_10MS_QUANTUM
)
1439 } else if (mode
& TCP_TIMERLIST_100MS_MODE
) {
1440 if (diff
<= TCP_TIMER_100MS_QUANTUM
)
1443 if (diff
<= TCP_TIMER_500MS_QUANTUM
)
1451 tcp_sched_timerlist(uint32_t offset
)
1453 uint64_t deadline
= 0;
1454 struct tcptimerlist
*listp
= &tcp_timer_list
;
1456 lck_mtx_assert(listp
->mtx
, LCK_MTX_ASSERT_OWNED
);
1458 offset
= min(offset
, TCP_TIMERLIST_MAX_OFFSET
);
1459 listp
->runtime
= tcp_now
+ offset
;
1460 if (listp
->runtime
== 0) {
1465 clock_interval_to_deadline(offset
, USEC_PER_SEC
, &deadline
);
1467 thread_call_enter_delayed(listp
->call
, deadline
);
1468 listp
->scheduled
= TRUE
;
1472 * Function to run the timers for a connection.
1474 * Returns the offset of next timer to be run for this connection which
1475 * can be used to reschedule the timerlist.
1477 * te_mode is an out parameter that indicates the modes of active
1478 * timers for this connection.
1481 tcp_run_conn_timer(struct tcpcb
*tp
, u_int16_t
*te_mode
,
1482 u_int16_t probe_if_index
)
1485 u_int16_t i
= 0, index
= TCPT_NONE
, lo_index
= TCPT_NONE
;
1486 u_int32_t timer_val
, offset
= 0, lo_timer
= 0;
1488 boolean_t needtorun
[TCPT_NTIMERS
];
1492 bzero(needtorun
, sizeof(needtorun
));
1495 tcp_lock(tp
->t_inpcb
->inp_socket
, 1, 0);
1497 so
= tp
->t_inpcb
->inp_socket
;
1498 /* Release the want count on inp */
1499 if (in_pcb_checkstate(tp
->t_inpcb
, WNT_RELEASE
, 1)
1501 if (TIMER_IS_ON_LIST(tp
)) {
1502 tcp_remove_timer(tp
);
1505 /* Looks like the TCP connection got closed while we
1506 * were waiting for the lock.. Done
1512 * If this connection is over an interface that needs to
1513 * be probed, send probe packets to reinitiate communication.
1515 if (probe_if_index
> 0 && tp
->t_inpcb
->inp_last_outifp
!= NULL
&&
1516 tp
->t_inpcb
->inp_last_outifp
->if_index
== probe_if_index
) {
1517 tp
->t_flagsext
|= TF_PROBING
;
1518 tcp_timers(tp
, TCPT_PTO
);
1519 tp
->t_timer
[TCPT_PTO
] = 0;
1520 tp
->t_flagsext
&= TF_PROBING
;
1524 * Since the timer thread needs to wait for tcp lock, it may race
1525 * with another thread that can cancel or reschedule the timer
1526 * that is about to run. Check if we need to run anything.
1528 if ((index
= tp
->tentry
.index
) == TCPT_NONE
)
1531 timer_val
= tp
->t_timer
[index
];
1533 diff
= timer_diff(tp
->tentry
.runtime
, 0, tcp_now
, 0);
1535 if (tp
->tentry
.index
!= TCPT_NONE
) {
1537 *(te_mode
) = tp
->tentry
.mode
;
1542 tp
->t_timer
[index
] = 0;
1543 if (timer_val
> 0) {
1544 tp
= tcp_timers(tp
, index
);
1550 * Check if there are any other timers that need to be run.
1551 * While doing it, adjust the timer values wrt tcp_now.
1553 tp
->tentry
.mode
= 0;
1554 for (i
= 0; i
< TCPT_NTIMERS
; ++i
) {
1555 if (tp
->t_timer
[i
] != 0) {
1556 diff
= timer_diff(tp
->tentry
.timer_start
,
1557 tp
->t_timer
[i
], tcp_now
, 0);
1559 needtorun
[i
] = TRUE
;
1562 tp
->t_timer
[i
] = diff
;
1563 needtorun
[i
] = FALSE
;
1564 if (lo_timer
== 0 || diff
< lo_timer
) {
1568 TCP_SET_TIMER_MODE(tp
->tentry
.mode
, i
);
1573 tp
->tentry
.timer_start
= tcp_now
;
1574 tp
->tentry
.index
= lo_index
;
1575 VERIFY(tp
->tentry
.index
== TCPT_NONE
|| tp
->tentry
.mode
> 0);
1577 if (tp
->tentry
.index
!= TCPT_NONE
) {
1578 tp
->tentry
.runtime
= tp
->tentry
.timer_start
+
1579 tp
->t_timer
[tp
->tentry
.index
];
1580 if (tp
->tentry
.runtime
== 0)
1581 tp
->tentry
.runtime
++;
1585 /* run any other timers outstanding at this time. */
1586 for (i
= 0; i
< TCPT_NTIMERS
; ++i
) {
1589 tp
= tcp_timers(tp
, i
);
1597 tcp_set_lotimer_index(tp
);
1600 if (tp
->tentry
.index
< TCPT_NONE
) {
1601 offset
= tp
->t_timer
[tp
->tentry
.index
];
1602 *(te_mode
) = tp
->tentry
.mode
;
1606 if (tp
!= NULL
&& tp
->tentry
.index
== TCPT_NONE
) {
1607 tcp_remove_timer(tp
);
1611 tcp_unlock(so
, 1, 0);
1616 tcp_run_timerlist(void * arg1
, void * arg2
) {
1617 #pragma unused(arg1, arg2)
1618 struct tcptimerentry
*te
, *next_te
;
1619 struct tcptimerlist
*listp
= &tcp_timer_list
;
1621 uint32_t next_timer
= 0; /* offset of the next timer on the list */
1622 u_int16_t te_mode
= 0; /* modes of all active timers in a tcpcb */
1623 u_int16_t list_mode
= 0; /* cumulative of modes of all tcpcbs */
1624 uint32_t active_count
= 0;
1626 calculate_tcp_clock();
1628 lck_mtx_lock(listp
->mtx
);
1630 listp
->running
= TRUE
;
1632 LIST_FOREACH_SAFE(te
, &listp
->lhead
, le
, next_te
) {
1633 uint32_t offset
= 0;
1634 uint32_t runtime
= te
->runtime
;
1635 if (te
->index
< TCPT_NONE
&& TSTMP_GT(runtime
, tcp_now
)) {
1636 offset
= timer_diff(runtime
, 0, tcp_now
, 0);
1637 if (next_timer
== 0 || offset
< next_timer
) {
1638 next_timer
= offset
;
1640 list_mode
|= te
->mode
;
1644 tp
= TIMERENTRY_TO_TP(te
);
1647 * Acquire an inp wantcnt on the inpcb so that the socket
1648 * won't get detached even if tcp_close is called
1650 if (in_pcb_checkstate(tp
->t_inpcb
, WNT_ACQUIRE
, 0)
1653 * Some how this pcb went into dead state while
1654 * on the timer list, just take it off the list.
1655 * Since the timer list entry pointers are
1656 * protected by the timer list lock, we can
1657 * do it here without the socket lock.
1659 if (TIMER_IS_ON_LIST(tp
)) {
1660 tp
->t_flags
&= ~(TF_TIMER_ONLIST
);
1661 LIST_REMOVE(&tp
->tentry
, le
);
1664 tp
->tentry
.le
.le_next
= NULL
;
1665 tp
->tentry
.le
.le_prev
= NULL
;
1672 * Store the next timerentry pointer before releasing the
1673 * list lock. If that entry has to be removed when we
1674 * release the lock, this pointer will be updated to the
1675 * element after that.
1677 listp
->next_te
= next_te
;
1679 VERIFY_NEXT_LINK(&tp
->tentry
, le
);
1680 VERIFY_PREV_LINK(&tp
->tentry
, le
);
1682 lck_mtx_unlock(listp
->mtx
);
1684 offset
= tcp_run_conn_timer(tp
, &te_mode
,
1685 listp
->probe_if_index
);
1687 lck_mtx_lock(listp
->mtx
);
1689 next_te
= listp
->next_te
;
1690 listp
->next_te
= NULL
;
1692 if (offset
> 0 && te_mode
!= 0) {
1693 list_mode
|= te_mode
;
1695 if (next_timer
== 0 || offset
< next_timer
)
1696 next_timer
= offset
;
1700 if (!LIST_EMPTY(&listp
->lhead
)) {
1701 u_int16_t next_mode
= 0;
1702 if ((list_mode
& TCP_TIMERLIST_10MS_MODE
) ||
1703 (listp
->pref_mode
& TCP_TIMERLIST_10MS_MODE
))
1704 next_mode
= TCP_TIMERLIST_10MS_MODE
;
1705 else if ((list_mode
& TCP_TIMERLIST_100MS_MODE
) ||
1706 (listp
->pref_mode
& TCP_TIMERLIST_100MS_MODE
))
1707 next_mode
= TCP_TIMERLIST_100MS_MODE
;
1709 next_mode
= TCP_TIMERLIST_500MS_MODE
;
1711 if (next_mode
!= TCP_TIMERLIST_500MS_MODE
) {
1712 listp
->idleruns
= 0;
1715 * the next required mode is slow mode, but if
1716 * the last one was a faster mode and we did not
1717 * have enough idle runs, repeat the last mode.
1719 * We try to keep the timer list in fast mode for
1720 * some idle time in expectation of new data.
1722 if (listp
->mode
!= next_mode
&&
1723 listp
->idleruns
< timer_fastmode_idlemax
) {
1725 next_mode
= listp
->mode
;
1726 next_timer
= TCP_TIMER_100MS_QUANTUM
;
1728 listp
->idleruns
= 0;
1731 listp
->mode
= next_mode
;
1732 if (listp
->pref_offset
!= 0)
1733 next_timer
= min(listp
->pref_offset
, next_timer
);
1735 if (listp
->mode
== TCP_TIMERLIST_500MS_MODE
)
1736 next_timer
= max(next_timer
,
1737 TCP_TIMER_500MS_QUANTUM
);
1739 tcp_sched_timerlist(next_timer
);
1742 * No need to reschedule this timer, but always run
1743 * periodically at a much higher granularity.
1745 tcp_sched_timerlist(TCP_TIMERLIST_MAX_OFFSET
);
1748 listp
->running
= FALSE
;
1749 listp
->pref_mode
= 0;
1750 listp
->pref_offset
= 0;
1751 listp
->probe_if_index
= 0;
1753 lck_mtx_unlock(listp
->mtx
);
1757 * Function to check if the timerlist needs to be rescheduled to run this
1758 * connection's timers correctly.
1761 tcp_sched_timers(struct tcpcb
*tp
)
1763 struct tcptimerentry
*te
= &tp
->tentry
;
1764 u_int16_t index
= te
->index
;
1765 u_int16_t mode
= te
->mode
;
1766 struct tcptimerlist
*listp
= &tcp_timer_list
;
1768 boolean_t list_locked
= FALSE
;
1770 if (tp
->t_inpcb
->inp_state
== INPCB_STATE_DEAD
) {
1771 /* Just return without adding the dead pcb to the list */
1772 if (TIMER_IS_ON_LIST(tp
)) {
1773 tcp_remove_timer(tp
);
1778 if (index
== TCPT_NONE
) {
1779 /* Nothing to run */
1780 tcp_remove_timer(tp
);
1785 * compute the offset at which the next timer for this connection
1788 offset
= timer_diff(te
->runtime
, 0, tcp_now
, 0);
1791 tcp_timer_advanced
++;
1794 if (!TIMER_IS_ON_LIST(tp
)) {
1796 lck_mtx_lock(listp
->mtx
);
1800 LIST_INSERT_HEAD(&listp
->lhead
, te
, le
);
1801 tp
->t_flags
|= TF_TIMER_ONLIST
;
1804 if (listp
->entries
> listp
->maxentries
)
1805 listp
->maxentries
= listp
->entries
;
1807 /* if the list is not scheduled, just schedule it */
1808 if (!listp
->scheduled
)
1814 * Timer entry is currently on the list, check if the list needs
1815 * to be rescheduled.
1817 if (need_to_resched_timerlist(te
->runtime
, mode
)) {
1818 tcp_resched_timerlist
++;
1821 lck_mtx_lock(listp
->mtx
);
1825 VERIFY_NEXT_LINK(te
, le
);
1826 VERIFY_PREV_LINK(te
, le
);
1828 if (listp
->running
) {
1829 listp
->pref_mode
|= mode
;
1830 if (listp
->pref_offset
== 0 ||
1831 offset
< listp
->pref_offset
) {
1832 listp
->pref_offset
= offset
;
1836 * The list could have got rescheduled while
1837 * this thread was waiting for the lock
1839 if (listp
->scheduled
) {
1841 diff
= timer_diff(listp
->runtime
, 0,
1856 * Since a connection with timers is getting scheduled, the timer
1857 * list moves from idle to active state and that is why idlegen is
1860 if (mode
& TCP_TIMERLIST_10MS_MODE
) {
1861 listp
->mode
= TCP_TIMERLIST_10MS_MODE
;
1862 listp
->idleruns
= 0;
1863 offset
= min(offset
, TCP_TIMER_10MS_QUANTUM
);
1864 } else if (mode
& TCP_TIMERLIST_100MS_MODE
) {
1865 if (listp
->mode
> TCP_TIMERLIST_100MS_MODE
)
1866 listp
->mode
= TCP_TIMERLIST_100MS_MODE
;
1867 listp
->idleruns
= 0;
1868 offset
= min(offset
, TCP_TIMER_100MS_QUANTUM
);
1870 tcp_sched_timerlist(offset
);
1874 lck_mtx_unlock(listp
->mtx
);
1880 tcp_set_lotimer_index(struct tcpcb
*tp
)
1882 uint16_t i
, lo_index
= TCPT_NONE
, mode
= 0;
1883 uint32_t lo_timer
= 0;
1884 for (i
= 0; i
< TCPT_NTIMERS
; ++i
) {
1885 if (tp
->t_timer
[i
] != 0) {
1886 TCP_SET_TIMER_MODE(mode
, i
);
1887 if (lo_timer
== 0 || tp
->t_timer
[i
] < lo_timer
) {
1888 lo_timer
= tp
->t_timer
[i
];
1893 tp
->tentry
.index
= lo_index
;
1894 tp
->tentry
.mode
= mode
;
1895 VERIFY(tp
->tentry
.index
== TCPT_NONE
|| tp
->tentry
.mode
> 0);
1897 if (tp
->tentry
.index
!= TCPT_NONE
) {
1898 tp
->tentry
.runtime
= tp
->tentry
.timer_start
1899 + tp
->t_timer
[tp
->tentry
.index
];
1900 if (tp
->tentry
.runtime
== 0)
1901 tp
->tentry
.runtime
++;
1906 tcp_check_timer_state(struct tcpcb
*tp
)
1908 lck_mtx_assert(&tp
->t_inpcb
->inpcb_mtx
, LCK_MTX_ASSERT_OWNED
);
1910 if (tp
->t_inpcb
->inp_flags2
& INP2_TIMEWAIT
)
1913 tcp_set_lotimer_index(tp
);
1915 tcp_sched_timers(tp
);
1920 tcp_cumulative_stat(u_int32_t cur
, u_int32_t
*prev
, u_int32_t
*dest
)
1922 /* handle wrap around */
1923 int32_t diff
= (int32_t) (cur
- *prev
);
1932 __private_extern__
void
1933 tcp_report_stats(void)
1935 struct nstat_sysinfo_data data
;
1936 struct sockaddr_in dst
;
1937 struct sockaddr_in6 dst6
;
1938 struct rtentry
*rt
= NULL
;
1939 static struct tcp_last_report_stats prev
;
1940 u_int64_t var
, uptime
;
1942 #define stat data.u.tcp_stats
1943 if (((uptime
= net_uptime()) - tcp_last_report_time
) <
1944 tcp_report_stats_interval
)
1947 tcp_last_report_time
= uptime
;
1949 bzero(&data
, sizeof(data
));
1950 data
.flags
= NSTAT_SYSINFO_TCP_STATS
;
1952 bzero(&dst
, sizeof(dst
));
1953 dst
.sin_len
= sizeof(dst
);
1954 dst
.sin_family
= AF_INET
;
1957 lck_mtx_lock(rnh_lock
);
1958 rt
= rt_lookup(TRUE
, (struct sockaddr
*)&dst
, NULL
,
1959 rt_tables
[AF_INET
], IFSCOPE_NONE
);
1960 lck_mtx_unlock(rnh_lock
);
1963 if (rt_primary_default(rt
, rt_key(rt
)) &&
1964 rt
->rt_stats
!= NULL
) {
1965 stat
.ipv4_avgrtt
= rt
->rt_stats
->nstat_avg_rtt
;
1973 bzero(&dst6
, sizeof(dst6
));
1974 dst6
.sin6_len
= sizeof(dst6
);
1975 dst6
.sin6_family
= AF_INET6
;
1977 lck_mtx_lock(rnh_lock
);
1978 rt
= rt_lookup(TRUE
,(struct sockaddr
*)&dst6
, NULL
,
1979 rt_tables
[AF_INET6
], IFSCOPE_NONE
);
1980 lck_mtx_unlock(rnh_lock
);
1983 if (rt_primary_default(rt
, rt_key(rt
)) &&
1984 rt
->rt_stats
!= NULL
) {
1985 stat
.ipv6_avgrtt
= rt
->rt_stats
->nstat_avg_rtt
;
1992 /* send packet loss rate, shift by 10 for precision */
1993 if (tcpstat
.tcps_sndpack
> 0 && tcpstat
.tcps_sndrexmitpack
> 0) {
1994 var
= tcpstat
.tcps_sndrexmitpack
<< 10;
1995 stat
.send_plr
= (var
* 100) / tcpstat
.tcps_sndpack
;
1998 /* recv packet loss rate, shift by 10 for precision */
1999 if (tcpstat
.tcps_rcvpack
> 0 && tcpstat
.tcps_recovered_pkts
> 0) {
2000 var
= tcpstat
.tcps_recovered_pkts
<< 10;
2001 stat
.recv_plr
= (var
* 100) / tcpstat
.tcps_rcvpack
;
2004 /* RTO after tail loss, shift by 10 for precision */
2005 if (tcpstat
.tcps_sndrexmitpack
> 0
2006 && tcpstat
.tcps_tailloss_rto
> 0) {
2007 var
= tcpstat
.tcps_tailloss_rto
<< 10;
2008 stat
.send_tlrto_rate
=
2009 (var
* 100) / tcpstat
.tcps_sndrexmitpack
;
2012 /* packet reordering */
2013 if (tcpstat
.tcps_sndpack
> 0 && tcpstat
.tcps_reordered_pkts
> 0) {
2014 var
= tcpstat
.tcps_reordered_pkts
<< 10;
2015 stat
.send_reorder_rate
=
2016 (var
* 100) / tcpstat
.tcps_sndpack
;
2019 if (tcp_ecn_outbound
== 1)
2020 stat
.ecn_client_enabled
= 1;
2021 if (tcp_ecn_inbound
== 1)
2022 stat
.ecn_server_enabled
= 1;
2023 tcp_cumulative_stat(tcpstat
.tcps_connattempt
,
2024 &prev
.tcps_connattempt
, &stat
.connection_attempts
);
2025 tcp_cumulative_stat(tcpstat
.tcps_accepts
,
2026 &prev
.tcps_accepts
, &stat
.connection_accepts
);
2027 tcp_cumulative_stat(tcpstat
.tcps_ecn_client_setup
,
2028 &prev
.tcps_ecn_client_setup
, &stat
.ecn_client_setup
);
2029 tcp_cumulative_stat(tcpstat
.tcps_ecn_server_setup
,
2030 &prev
.tcps_ecn_server_setup
, &stat
.ecn_server_setup
);
2031 tcp_cumulative_stat(tcpstat
.tcps_ecn_client_success
,
2032 &prev
.tcps_ecn_client_success
, &stat
.ecn_client_success
);
2033 tcp_cumulative_stat(tcpstat
.tcps_ecn_server_success
,
2034 &prev
.tcps_ecn_server_success
, &stat
.ecn_server_success
);
2035 tcp_cumulative_stat(tcpstat
.tcps_ecn_not_supported
,
2036 &prev
.tcps_ecn_not_supported
, &stat
.ecn_not_supported
);
2037 tcp_cumulative_stat(tcpstat
.tcps_ecn_lost_syn
,
2038 &prev
.tcps_ecn_lost_syn
, &stat
.ecn_lost_syn
);
2039 tcp_cumulative_stat(tcpstat
.tcps_ecn_lost_synack
,
2040 &prev
.tcps_ecn_lost_synack
, &stat
.ecn_lost_synack
);
2041 tcp_cumulative_stat(tcpstat
.tcps_ecn_recv_ce
,
2042 &prev
.tcps_ecn_recv_ce
, &stat
.ecn_recv_ce
);
2043 tcp_cumulative_stat(tcpstat
.tcps_ecn_recv_ece
,
2044 &prev
.tcps_ecn_recv_ece
, &stat
.ecn_recv_ece
);
2045 tcp_cumulative_stat(tcpstat
.tcps_ecn_recv_ece
,
2046 &prev
.tcps_ecn_recv_ece
, &stat
.ecn_recv_ece
);
2047 tcp_cumulative_stat(tcpstat
.tcps_ecn_sent_ece
,
2048 &prev
.tcps_ecn_sent_ece
, &stat
.ecn_sent_ece
);
2049 tcp_cumulative_stat(tcpstat
.tcps_ecn_sent_ece
,
2050 &prev
.tcps_ecn_sent_ece
, &stat
.ecn_sent_ece
);
2051 tcp_cumulative_stat(tcpstat
.tcps_ecn_conn_recv_ce
,
2052 &prev
.tcps_ecn_conn_recv_ce
, &stat
.ecn_conn_recv_ce
);
2053 tcp_cumulative_stat(tcpstat
.tcps_ecn_conn_recv_ece
,
2054 &prev
.tcps_ecn_conn_recv_ece
, &stat
.ecn_conn_recv_ece
);
2055 tcp_cumulative_stat(tcpstat
.tcps_ecn_conn_plnoce
,
2056 &prev
.tcps_ecn_conn_plnoce
, &stat
.ecn_conn_plnoce
);
2057 tcp_cumulative_stat(tcpstat
.tcps_ecn_conn_pl_ce
,
2058 &prev
.tcps_ecn_conn_pl_ce
, &stat
.ecn_conn_pl_ce
);
2059 tcp_cumulative_stat(tcpstat
.tcps_ecn_conn_nopl_ce
,
2060 &prev
.tcps_ecn_conn_nopl_ce
, &stat
.ecn_conn_nopl_ce
);
2061 tcp_cumulative_stat(tcpstat
.tcps_ecn_fallback_synloss
,
2062 &prev
.tcps_ecn_fallback_synloss
, &stat
.ecn_fallback_synloss
);
2063 tcp_cumulative_stat(tcpstat
.tcps_ecn_fallback_reorder
,
2064 &prev
.tcps_ecn_fallback_reorder
, &stat
.ecn_fallback_reorder
);
2065 tcp_cumulative_stat(tcpstat
.tcps_ecn_fallback_ce
,
2066 &prev
.tcps_ecn_fallback_ce
, &stat
.ecn_fallback_ce
);
2067 tcp_cumulative_stat(tcpstat
.tcps_tfo_syn_data_rcv
,
2068 &prev
.tcps_tfo_syn_data_rcv
, &stat
.tfo_syn_data_rcv
);
2069 tcp_cumulative_stat(tcpstat
.tcps_tfo_cookie_req_rcv
,
2070 &prev
.tcps_tfo_cookie_req_rcv
, &stat
.tfo_cookie_req_rcv
);
2071 tcp_cumulative_stat(tcpstat
.tcps_tfo_cookie_sent
,
2072 &prev
.tcps_tfo_cookie_sent
, &stat
.tfo_cookie_sent
);
2073 tcp_cumulative_stat(tcpstat
.tcps_tfo_cookie_invalid
,
2074 &prev
.tcps_tfo_cookie_invalid
, &stat
.tfo_cookie_invalid
);
2075 tcp_cumulative_stat(tcpstat
.tcps_tfo_cookie_req
,
2076 &prev
.tcps_tfo_cookie_req
, &stat
.tfo_cookie_req
);
2077 tcp_cumulative_stat(tcpstat
.tcps_tfo_cookie_rcv
,
2078 &prev
.tcps_tfo_cookie_rcv
, &stat
.tfo_cookie_rcv
);
2079 tcp_cumulative_stat(tcpstat
.tcps_tfo_syn_data_sent
,
2080 &prev
.tcps_tfo_syn_data_sent
, &stat
.tfo_syn_data_sent
);
2081 tcp_cumulative_stat(tcpstat
.tcps_tfo_syn_data_acked
,
2082 &prev
.tcps_tfo_syn_data_acked
, &stat
.tfo_syn_data_acked
);
2083 tcp_cumulative_stat(tcpstat
.tcps_tfo_syn_loss
,
2084 &prev
.tcps_tfo_syn_loss
, &stat
.tfo_syn_loss
);
2085 tcp_cumulative_stat(tcpstat
.tcps_tfo_blackhole
,
2086 &prev
.tcps_tfo_blackhole
, &stat
.tfo_blackhole
);
2088 nstat_sysinfo_send_data(&data
);
2094 tcp_interface_send_probe(u_int16_t probe_if_index
)
2097 struct tcptimerlist
*listp
= &tcp_timer_list
;
2099 /* Make sure TCP clock is up to date */
2100 calculate_tcp_clock();
2102 lck_mtx_lock(listp
->mtx
);
2103 if (listp
->probe_if_index
> 0) {
2104 tcpstat
.tcps_probe_if_conflict
++;
2108 listp
->probe_if_index
= probe_if_index
;
2113 * Reschedule the timerlist to run within the next 10ms, which is
2114 * the fastest that we can do.
2116 offset
= TCP_TIMER_10MS_QUANTUM
;
2117 if (listp
->scheduled
) {
2119 diff
= timer_diff(listp
->runtime
, 0, tcp_now
, offset
);
2121 /* The timer will fire sooner than what's needed */
2125 listp
->mode
= TCP_TIMERLIST_10MS_MODE
;
2126 listp
->idleruns
= 0;
2128 tcp_sched_timerlist(offset
);
2131 lck_mtx_unlock(listp
->mtx
);
2136 * Enable read probes on this connection, if:
2137 * - it is in established state
2138 * - doesn't have any data outstanding
2139 * - the outgoing ifp matches
2140 * - we have not already sent any read probes
2143 tcp_enable_read_probe(struct tcpcb
*tp
, struct ifnet
*ifp
)
2145 if (tp
->t_state
== TCPS_ESTABLISHED
&&
2146 tp
->snd_max
== tp
->snd_una
&&
2147 tp
->t_inpcb
->inp_last_outifp
== ifp
&&
2148 !(tp
->t_flagsext
& TF_DETECT_READSTALL
) &&
2149 tp
->t_rtimo_probes
== 0) {
2150 tp
->t_flagsext
|= TF_DETECT_READSTALL
;
2151 tp
->t_rtimo_probes
= 0;
2152 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
,
2153 TCP_TIMER_10MS_QUANTUM
);
2154 if (tp
->tentry
.index
== TCPT_NONE
) {
2155 tp
->tentry
.index
= TCPT_KEEP
;
2156 tp
->tentry
.runtime
= tcp_now
+
2157 TCP_TIMER_10MS_QUANTUM
;
2161 /* Reset runtime to be in next 10ms */
2162 diff
= timer_diff(tp
->tentry
.runtime
, 0,
2163 tcp_now
, TCP_TIMER_10MS_QUANTUM
);
2165 tp
->tentry
.index
= TCPT_KEEP
;
2166 tp
->tentry
.runtime
= tcp_now
+
2167 TCP_TIMER_10MS_QUANTUM
;
2168 if (tp
->tentry
.runtime
== 0)
2169 tp
->tentry
.runtime
++;
2176 * Disable read probe and reset the keep alive timer
2179 tcp_disable_read_probe(struct tcpcb
*tp
)
2181 if (tp
->t_adaptive_rtimo
== 0 &&
2182 ((tp
->t_flagsext
& TF_DETECT_READSTALL
) ||
2183 tp
->t_rtimo_probes
> 0)) {
2184 tcp_keepalive_reset(tp
);
2189 * Reschedule the tcp timerlist in the next 10ms to re-enable read/write
2190 * probes on connections going over a particular interface.
2193 tcp_probe_connectivity(struct ifnet
*ifp
, u_int32_t enable
)
2196 struct tcptimerlist
*listp
= &tcp_timer_list
;
2197 struct inpcbinfo
*pcbinfo
= &tcbinfo
;
2198 struct inpcb
*inp
, *nxt
;
2204 calculate_tcp_clock();
2207 * Enable keep alive timer on all connections that are
2208 * active/established on this interface.
2210 lck_rw_lock_shared(pcbinfo
->ipi_lock
);
2212 LIST_FOREACH_SAFE(inp
, pcbinfo
->ipi_listhead
, inp_list
, nxt
) {
2213 struct tcpcb
*tp
= NULL
;
2214 if (in_pcb_checkstate(inp
, WNT_ACQUIRE
, 0) ==
2218 /* Acquire lock to look at the state of the connection */
2219 tcp_lock(inp
->inp_socket
, 1, 0);
2221 /* Release the want count */
2222 if (inp
->inp_ppcb
== NULL
||
2223 (in_pcb_checkstate(inp
, WNT_RELEASE
, 1) == WNT_STOPUSING
)) {
2224 tcp_unlock(inp
->inp_socket
, 1, 0);
2227 tp
= intotcpcb(inp
);
2229 tcp_enable_read_probe(tp
, ifp
);
2231 tcp_disable_read_probe(tp
);
2233 tcp_unlock(inp
->inp_socket
, 1, 0);
2235 lck_rw_done(pcbinfo
->ipi_lock
);
2237 lck_mtx_lock(listp
->mtx
);
2238 if (listp
->running
) {
2239 listp
->pref_mode
|= TCP_TIMERLIST_10MS_MODE
;
2243 /* Reschedule within the next 10ms */
2244 offset
= TCP_TIMER_10MS_QUANTUM
;
2245 if (listp
->scheduled
) {
2247 diff
= timer_diff(listp
->runtime
, 0, tcp_now
, offset
);
2249 /* The timer will fire sooner than what's needed */
2253 listp
->mode
= TCP_TIMERLIST_10MS_MODE
;
2254 listp
->idleruns
= 0;
2256 tcp_sched_timerlist(offset
);
2258 lck_mtx_unlock(listp
->mtx
);
2263 tcp_itimer(struct inpcbinfo
*ipi
)
2265 struct inpcb
*inp
, *nxt
;
2267 if (lck_rw_try_lock_exclusive(ipi
->ipi_lock
) == FALSE
) {
2268 if (tcp_itimer_done
== TRUE
) {
2269 tcp_itimer_done
= FALSE
;
2270 atomic_add_32(&ipi
->ipi_timer_req
.intimer_fast
, 1);
2273 /* Upgrade failed, lost lock now take it again exclusive */
2274 lck_rw_lock_exclusive(ipi
->ipi_lock
);
2276 tcp_itimer_done
= TRUE
;
2278 LIST_FOREACH_SAFE(inp
, &tcb
, inp_list
, nxt
) {
2281 if (in_pcb_checkstate(inp
, WNT_ACQUIRE
, 0) == WNT_STOPUSING
)
2283 so
= inp
->inp_socket
;
2285 if (in_pcb_checkstate(inp
, WNT_RELEASE
, 1) == WNT_STOPUSING
) {
2286 tcp_unlock(so
, 1, 0);
2289 so_check_extended_bk_idle_time(so
);
2290 tcp_unlock(so
, 1, 0);
2293 lck_rw_done(ipi
->ipi_lock
);