]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
3e170ce0 | 2 | * Copyright (c) 2000-2015 Apple Inc. All rights reserved. |
5d5c5d0d | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 5 | * |
2d21ac55 A |
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. | |
8f6c56a5 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
17 | * | |
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 | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
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. | |
8f6c56a5 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | /* | |
29 | * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995 | |
30 | * The Regents of the University of California. All rights reserved. | |
31 | * | |
32 | * Redistribution and use in source and binary forms, with or without | |
33 | * modification, are permitted provided that the following conditions | |
34 | * are met: | |
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. | |
47 | * | |
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 | |
58 | * SUCH DAMAGE. | |
59 | * | |
60 | * @(#)tcp_timer.c 8.2 (Berkeley) 5/24/95 | |
9bccf70c | 61 | * $FreeBSD: src/sys/netinet/tcp_timer.c,v 1.34.2.11 2001/08/22 00:59:12 silby Exp $ |
1c79356b A |
62 | */ |
63 | ||
1c79356b A |
64 | |
65 | #include <sys/param.h> | |
66 | #include <sys/systm.h> | |
67 | #include <sys/kernel.h> | |
9bccf70c | 68 | #include <sys/mbuf.h> |
1c79356b A |
69 | #include <sys/sysctl.h> |
70 | #include <sys/socket.h> | |
71 | #include <sys/socketvar.h> | |
72 | #include <sys/protosw.h> | |
b0d623f7 | 73 | #include <sys/domain.h> |
6d2010ae A |
74 | #include <sys/mcache.h> |
75 | #include <sys/queue.h> | |
91447636 | 76 | #include <kern/locks.h> |
1c79356b | 77 | #include <kern/cpu_number.h> /* before tcp_seq.h, for tcp_random18() */ |
39236c6e | 78 | #include <mach/boolean.h> |
1c79356b A |
79 | |
80 | #include <net/route.h> | |
316670eb | 81 | #include <net/if_var.h> |
fe8ab488 | 82 | #include <net/ntstat.h> |
1c79356b A |
83 | |
84 | #include <netinet/in.h> | |
85 | #include <netinet/in_systm.h> | |
1c79356b | 86 | #include <netinet/in_pcb.h> |
9bccf70c A |
87 | #if INET6 |
88 | #include <netinet6/in6_pcb.h> | |
89 | #endif | |
1c79356b A |
90 | #include <netinet/ip_var.h> |
91 | #include <netinet/tcp.h> | |
3e170ce0 | 92 | #include <netinet/tcp_cache.h> |
1c79356b A |
93 | #include <netinet/tcp_fsm.h> |
94 | #include <netinet/tcp_seq.h> | |
95 | #include <netinet/tcp_timer.h> | |
96 | #include <netinet/tcp_var.h> | |
6d2010ae | 97 | #include <netinet/tcp_cc.h> |
b0d623f7 A |
98 | #if INET6 |
99 | #include <netinet6/tcp6_var.h> | |
100 | #endif | |
1c79356b A |
101 | #include <netinet/tcpip.h> |
102 | #if TCPDEBUG | |
103 | #include <netinet/tcp_debug.h> | |
104 | #endif | |
105 | #include <sys/kdebug.h> | |
6d2010ae | 106 | #include <mach/sdt.h> |
39236c6e | 107 | #include <netinet/mptcp_var.h> |
1c79356b | 108 | |
6d2010ae A |
109 | #define TIMERENTRY_TO_TP(te) ((struct tcpcb *)((uintptr_t)te - offsetof(struct tcpcb, tentry.le.le_next))) |
110 | ||
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)); \ | |
116 | } while(0) | |
117 | ||
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)); \ | |
121 | } while(0) | |
122 | ||
fe8ab488 A |
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; \ | |
128 | else \ | |
129 | (mode) |= TCP_TIMERLIST_500MS_MODE; \ | |
130 | } while(0) | |
131 | ||
132 | /* Max number of times a stretch ack can be delayed on a connection */ | |
133 | #define TCP_STRETCHACK_DELAY_THRESHOLD 5 | |
134 | ||
3e170ce0 A |
135 | /* |
136 | * If the host processor has been sleeping for too long, this is the threshold | |
137 | * used to avoid sending stale retransmissions. | |
138 | */ | |
139 | #define TCP_SLEEP_TOO_LONG (10 * 60 * 1000) /* 10 minutes in ms */ | |
140 | ||
39236c6e A |
141 | /* tcp timer list */ |
142 | struct tcptimerlist tcp_timer_list; | |
143 | ||
144 | /* List of pcbs in timewait state, protected by tcbinfo's ipi_lock */ | |
145 | struct tcptailq tcp_tw_tailq; | |
146 | ||
9bccf70c A |
147 | static int |
148 | sysctl_msec_to_ticks SYSCTL_HANDLER_ARGS | |
149 | { | |
2d21ac55 | 150 | #pragma unused(arg1, arg2) |
9bccf70c | 151 | int error, s, tt; |
1c79356b | 152 | |
9bccf70c | 153 | tt = *(int *)oidp->oid_arg1; |
2d21ac55 | 154 | s = tt * 1000 / TCP_RETRANSHZ;; |
1c79356b | 155 | |
9bccf70c A |
156 | error = sysctl_handle_int(oidp, &s, 0, req); |
157 | if (error || !req->newptr) | |
158 | return (error); | |
159 | ||
2d21ac55 | 160 | tt = s * TCP_RETRANSHZ / 1000; |
9bccf70c A |
161 | if (tt < 1) |
162 | return (EINVAL); | |
163 | ||
164 | *(int *)oidp->oid_arg1 = tt; | |
165 | return (0); | |
166 | } | |
1c79356b | 167 | |
9bccf70c | 168 | int tcp_keepinit; |
fe8ab488 A |
169 | SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINIT, keepinit, |
170 | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, | |
9bccf70c A |
171 | &tcp_keepinit, 0, sysctl_msec_to_ticks, "I", ""); |
172 | ||
173 | int tcp_keepidle; | |
fe8ab488 A |
174 | SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPIDLE, keepidle, |
175 | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, | |
9bccf70c A |
176 | &tcp_keepidle, 0, sysctl_msec_to_ticks, "I", ""); |
177 | ||
178 | int tcp_keepintvl; | |
fe8ab488 A |
179 | SYSCTL_PROC(_net_inet_tcp, TCPCTL_KEEPINTVL, keepintvl, |
180 | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, | |
9bccf70c A |
181 | &tcp_keepintvl, 0, sysctl_msec_to_ticks, "I", ""); |
182 | ||
39236c6e | 183 | int tcp_keepcnt; |
fe8ab488 A |
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"); | |
39236c6e | 187 | |
9bccf70c | 188 | int tcp_msl; |
fe8ab488 A |
189 | SYSCTL_PROC(_net_inet_tcp, OID_AUTO, msl, |
190 | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, | |
9bccf70c | 191 | &tcp_msl, 0, sysctl_msec_to_ticks, "I", "Maximum segment lifetime"); |
1c79356b | 192 | |
6d2010ae | 193 | /* |
fe8ab488 A |
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. | |
198 | * | |
199 | * Expressed in milliseconds to be consistent without timeout related | |
200 | * values, the TCP socket option is in seconds. | |
6d2010ae A |
201 | */ |
202 | u_int32_t tcp_max_persist_timeout = 0; | |
fe8ab488 A |
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"); | |
6d2010ae | 207 | |
1c79356b | 208 | static int always_keepalive = 0; |
fe8ab488 A |
209 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, always_keepalive, |
210 | CTLFLAG_RW | CTLFLAG_LOCKED, | |
9bccf70c | 211 | &always_keepalive , 0, "Assume SO_KEEPALIVE on all TCP connections"); |
1c79356b | 212 | |
fe8ab488 A |
213 | /* |
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. | |
6d2010ae | 217 | */ |
fe8ab488 A |
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"); | |
6d2010ae | 222 | |
b0d623f7 A |
223 | /* |
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 | |
228 | * two options. | |
229 | */ | |
3e170ce0 A |
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, | |
fe8ab488 A |
233 | "Number of retransmitted SYNs before disabling RFC 1323 " |
234 | "options on local connections"); | |
39236c6e | 235 | |
6d2010ae | 236 | static int tcp_timer_advanced = 0; |
fe8ab488 A |
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"); | |
6d2010ae A |
240 | |
241 | static int tcp_resched_timerlist = 0; | |
fe8ab488 A |
242 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, tcp_resched_timerlist, |
243 | CTLFLAG_RD | CTLFLAG_LOCKED, &tcp_resched_timerlist, 0, | |
6d2010ae A |
244 | "Number of times timer list was rescheduled as part of processing a packet"); |
245 | ||
b0d623f7 | 246 | int tcp_pmtud_black_hole_detect = 1 ; |
fe8ab488 A |
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"); | |
b0d623f7 A |
250 | |
251 | int tcp_pmtud_black_hole_mss = 1200 ; | |
fe8ab488 A |
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"); | |
b0d623f7 | 255 | |
3e170ce0 A |
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) */ | |
263 | ||
39236c6e A |
264 | /* performed garbage collection of "used" sockets */ |
265 | static boolean_t tcp_gc_done = FALSE; | |
266 | ||
fe8ab488 | 267 | /* max idle probes */ |
9bccf70c | 268 | int tcp_maxpersistidle; |
1c79356b | 269 | |
fe8ab488 A |
270 | /* |
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. | |
6d2010ae A |
274 | */ |
275 | int tcp_delack = TCP_RETRANSHZ / 10; | |
276 | ||
39236c6e A |
277 | #if MPTCP |
278 | /* | |
279 | * MP_JOIN retransmission of 3rd ACK will be every 500 msecs without backoff | |
280 | */ | |
281 | int tcp_jack_rxmt = TCP_RETRANSHZ / 2; | |
282 | #endif /* MPTCP */ | |
1c79356b | 283 | |
3e170ce0 A |
284 | static boolean_t tcp_itimer_done = FALSE; |
285 | ||
6d2010ae A |
286 | static void tcp_remove_timer(struct tcpcb *tp); |
287 | static void tcp_sched_timerlist(uint32_t offset); | |
3e170ce0 A |
288 | static u_int32_t tcp_run_conn_timer(struct tcpcb *tp, u_int16_t *mode, |
289 | u_int16_t probe_if_index); | |
6d2010ae A |
290 | static void tcp_sched_timers(struct tcpcb *tp); |
291 | static inline void tcp_set_lotimer_index(struct tcpcb *); | |
fe8ab488 A |
292 | __private_extern__ void tcp_remove_from_time_wait(struct inpcb *inp); |
293 | __private_extern__ void tcp_report_stats(void); | |
6d2010ae | 294 | |
fe8ab488 | 295 | /* |
3e170ce0 A |
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 | |
fe8ab488 | 299 | * with the sign bit reset is actually ahead of the other. |
6d2010ae | 300 | */ |
fe8ab488 | 301 | inline int32_t |
3e170ce0 | 302 | timer_diff(uint32_t t1, uint32_t toff1, uint32_t t2, uint32_t toff2) { |
6d2010ae A |
303 | return (int32_t)((t1 + toff1) - (t2 + toff2)); |
304 | }; | |
305 | ||
fe8ab488 | 306 | static u_int64_t tcp_last_report_time; |
3e170ce0 A |
307 | |
308 | /* | |
309 | * Structure to store previously reported stats so that we can send | |
310 | * incremental changes in each report interval. | |
311 | */ | |
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; | |
4bd07ac2 A |
330 | u_int32_t tcps_ecn_fallback_synloss; |
331 | u_int32_t tcps_ecn_fallback_reorder; | |
332 | u_int32_t tcps_ecn_fallback_ce; | |
3e170ce0 A |
333 | |
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; | |
345 | }; | |
346 | ||
fe8ab488 | 347 | |
6d2010ae A |
348 | /* Returns true if the timer is on the timer list */ |
349 | #define TIMER_IS_ON_LIST(tp) ((tp)->t_flags & TF_TIMER_ONLIST) | |
350 | ||
8a3053a0 | 351 | /* Run the TCP timerlist atleast once every hour */ |
fe8ab488 | 352 | #define TCP_TIMERLIST_MAX_OFFSET (60 * 60 * TCP_RETRANSHZ) |
6d2010ae | 353 | |
2d21ac55 | 354 | |
fe8ab488 | 355 | static void add_to_time_wait_locked(struct tcpcb *tp, uint32_t delay); |
39236c6e | 356 | static boolean_t tcp_garbage_collect(struct inpcb *, int); |
1c79356b | 357 | |
39236c6e A |
358 | /* |
359 | * Add to tcp timewait list, delay is given in milliseconds. | |
360 | */ | |
361 | static void | |
362 | add_to_time_wait_locked(struct tcpcb *tp, uint32_t delay) | |
1c79356b | 363 | { |
39236c6e A |
364 | struct inpcbinfo *pcbinfo = &tcbinfo; |
365 | struct inpcb *inp = tp->t_inpcb; | |
6d2010ae | 366 | uint32_t timer; |
1c79356b | 367 | |
39236c6e A |
368 | /* pcb list should be locked when we get here */ |
369 | lck_rw_assert(pcbinfo->ipi_lock, LCK_RW_ASSERT_EXCLUSIVE); | |
1c79356b | 370 | |
39236c6e A |
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; | |
375 | ||
376 | /* Remove from global inp list */ | |
377 | LIST_REMOVE(inp, inp_list); | |
378 | } else { | |
379 | TAILQ_REMOVE(&tcp_tw_tailq, tp, t_twentry); | |
380 | } | |
2d21ac55 | 381 | |
39236c6e A |
382 | /* Compute the time at which this socket can be closed */ |
383 | timer = tcp_now + delay; | |
384 | ||
385 | /* We will use the TCPT_2MSL timer for tracking this delay */ | |
2d21ac55 | 386 | |
39236c6e A |
387 | if (TIMER_IS_ON_LIST(tp)) |
388 | tcp_remove_timer(tp); | |
389 | tp->t_timer[TCPT_2MSL] = timer; | |
1c79356b | 390 | |
39236c6e | 391 | TAILQ_INSERT_TAIL(&tcp_tw_tailq, tp, t_twentry); |
1c79356b A |
392 | } |
393 | ||
39236c6e A |
394 | void |
395 | add_to_time_wait(struct tcpcb *tp, uint32_t delay) | |
91447636 | 396 | { |
39236c6e | 397 | struct inpcbinfo *pcbinfo = &tcbinfo; |
fe8ab488 A |
398 | if (tp->t_inpcb->inp_socket->so_options & SO_NOWAKEFROMSLEEP) |
399 | socket_post_kev_msg_closed(tp->t_inpcb->inp_socket); | |
39236c6e | 400 | |
3e170ce0 A |
401 | /* 19182803: Notify nstat that connection is closing before waiting. */ |
402 | nstat_pcb_detach(tp->t_inpcb); | |
403 | ||
39236c6e | 404 | if (!lck_rw_try_lock_exclusive(pcbinfo->ipi_lock)) { |
91447636 | 405 | tcp_unlock(tp->t_inpcb->inp_socket, 0, 0); |
39236c6e | 406 | lck_rw_lock_exclusive(pcbinfo->ipi_lock); |
91447636 A |
407 | tcp_lock(tp->t_inpcb->inp_socket, 0, 0); |
408 | } | |
6d2010ae | 409 | add_to_time_wait_locked(tp, delay); |
39236c6e A |
410 | lck_rw_done(pcbinfo->ipi_lock); |
411 | ||
412 | inpcb_gc_sched(pcbinfo, INPCB_TIMER_LAZY); | |
91447636 | 413 | } |
1c79356b | 414 | |
39236c6e A |
415 | /* If this is on time wait queue, remove it. */ |
416 | void | |
417 | tcp_remove_from_time_wait(struct inpcb *inp) | |
418 | { | |
419 | struct tcpcb *tp = intotcpcb(inp); | |
420 | if (inp->inp_flags2 & INP2_TIMEWAIT) | |
421 | TAILQ_REMOVE(&tcp_tw_tailq, tp, t_twentry); | |
422 | } | |
423 | ||
424 | static boolean_t | |
b0d623f7 | 425 | tcp_garbage_collect(struct inpcb *inp, int istimewait) |
2d21ac55 | 426 | { |
39236c6e | 427 | boolean_t active = FALSE; |
2d21ac55 A |
428 | struct socket *so; |
429 | struct tcpcb *tp; | |
430 | ||
b0d623f7 A |
431 | so = inp->inp_socket; |
432 | tp = intotcpcb(inp); | |
2d21ac55 | 433 | |
b0d623f7 A |
434 | /* |
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. | |
440 | */ | |
39236c6e A |
441 | if (!lck_mtx_try_lock_spin(&inp->inpcb_mtx)) |
442 | return (TRUE); | |
2d21ac55 | 443 | |
b0d623f7 A |
444 | /* Check again under the lock */ |
445 | if (so->so_usecount > 1) { | |
39236c6e A |
446 | if (inp->inp_wantcnt == WNT_STOPUSING) |
447 | active = TRUE; | |
6d2010ae | 448 | lck_mtx_unlock(&inp->inpcb_mtx); |
39236c6e A |
449 | return (active); |
450 | } | |
451 | ||
452 | if (istimewait && | |
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); | |
457 | tcp_close(tp); | |
b0d623f7 | 458 | } |
2d21ac55 | 459 | |
b0d623f7 A |
460 | /* |
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. | |
39236c6e A |
465 | * Also handle the case of connections getting closed by the peer |
466 | * while in the queue as seen with rdar://6422317 | |
467 | * | |
b0d623f7 | 468 | */ |
39236c6e | 469 | if (so->so_usecount == 1 && |
b0d623f7 | 470 | ((istimewait && (so->so_flags & SOF_OVERFLOW)) || |
39236c6e A |
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))))) { | |
b0d623f7 A |
475 | |
476 | if (inp->inp_state != INPCB_STATE_DEAD) { | |
477 | /* Become a regular mutex */ | |
6d2010ae | 478 | lck_mtx_convert_spin(&inp->inpcb_mtx); |
b0d623f7 | 479 | #if INET6 |
39236c6e | 480 | if (SOCK_CHECK_DOM(so, PF_INET6)) |
b0d623f7 A |
481 | in6_pcbdetach(inp); |
482 | else | |
483 | #endif /* INET6 */ | |
39236c6e | 484 | in_pcbdetach(inp); |
2d21ac55 | 485 | } |
b0d623f7 | 486 | so->so_usecount--; |
39236c6e A |
487 | if (inp->inp_wantcnt == WNT_STOPUSING) |
488 | active = TRUE; | |
6d2010ae | 489 | lck_mtx_unlock(&inp->inpcb_mtx); |
39236c6e | 490 | return (active); |
b0d623f7 | 491 | } else if (inp->inp_wantcnt != WNT_STOPUSING) { |
6d2010ae | 492 | lck_mtx_unlock(&inp->inpcb_mtx); |
39236c6e | 493 | return (FALSE); |
b0d623f7 | 494 | } |
2d21ac55 | 495 | |
b0d623f7 | 496 | /* |
39236c6e A |
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 | |
b0d623f7 A |
505 | * socket is dropped at the end of tcp_input(). |
506 | */ | |
507 | if (so->so_usecount == 0) { | |
6d2010ae A |
508 | DTRACE_TCP4(state__change, void, NULL, struct inpcb *, inp, |
509 | struct tcpcb *, tp, int32_t, TCPS_CLOSED); | |
b0d623f7 | 510 | /* Become a regular mutex */ |
6d2010ae | 511 | lck_mtx_convert_spin(&inp->inpcb_mtx); |
39236c6e A |
512 | |
513 | /* | |
514 | * If this tp still happens to be on the timer list, | |
ebb1b9f4 A |
515 | * take it out |
516 | */ | |
517 | if (TIMER_IS_ON_LIST(tp)) { | |
518 | tcp_remove_timer(tp); | |
519 | } | |
520 | ||
b0d623f7 A |
521 | if (inp->inp_state != INPCB_STATE_DEAD) { |
522 | #if INET6 | |
39236c6e | 523 | if (SOCK_CHECK_DOM(so, PF_INET6)) |
b0d623f7 A |
524 | in6_pcbdetach(inp); |
525 | else | |
526 | #endif /* INET6 */ | |
39236c6e | 527 | in_pcbdetach(inp); |
2d21ac55 | 528 | } |
b0d623f7 | 529 | in_pcbdispose(inp); |
39236c6e | 530 | return (FALSE); |
b0d623f7 | 531 | } |
39236c6e A |
532 | |
533 | lck_mtx_unlock(&inp->inpcb_mtx); | |
534 | return (TRUE); | |
2d21ac55 A |
535 | } |
536 | ||
39236c6e A |
537 | /* |
538 | * TCP garbage collector callback (inpcb_timer_func_t). | |
539 | * | |
540 | * Returns the number of pcbs that will need to be gc-ed soon, | |
541 | * returnining > 0 will keep timer active. | |
542 | */ | |
1c79356b | 543 | void |
39236c6e | 544 | tcp_gc(struct inpcbinfo *ipi) |
1c79356b | 545 | { |
4a3eedf9 | 546 | struct inpcb *inp, *nxt; |
39236c6e | 547 | struct tcpcb *tw_tp, *tw_ntp; |
1c79356b A |
548 | #if TCPDEBUG |
549 | int ostate; | |
550 | #endif | |
b0d623f7 | 551 | #if KDEBUG |
2d21ac55 | 552 | static int tws_checked = 0; |
b0d623f7 | 553 | #endif |
2d21ac55 | 554 | |
39236c6e | 555 | KERNEL_DEBUG(DBG_FNC_TCP_SLOW | DBG_FUNC_START, 0, 0, 0, 0, 0); |
1c79356b | 556 | |
39236c6e A |
557 | /* |
558 | * Update tcp_now here as it may get used while | |
559 | * processing the slow timer. | |
560 | */ | |
6d2010ae | 561 | calculate_tcp_clock(); |
8ad349bb | 562 | |
39236c6e A |
563 | /* |
564 | * Garbage collect socket/tcpcb: We need to acquire the list lock | |
6d2010ae | 565 | * exclusively to do this |
2d21ac55 A |
566 | */ |
567 | ||
39236c6e A |
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) { | |
2d21ac55 | 571 | tcp_gc_done = FALSE; |
39236c6e A |
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); | |
576 | return; | |
2d21ac55 | 577 | } |
39236c6e A |
578 | /* Upgrade failed, lost lock now take it again exclusive */ |
579 | lck_rw_lock_exclusive(ipi->ipi_lock); | |
2d21ac55 A |
580 | } |
581 | tcp_gc_done = TRUE; | |
1c79356b | 582 | |
39236c6e A |
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); | |
586 | } | |
2d21ac55 | 587 | |
39236c6e A |
588 | /* Now cleanup the time wait ones */ |
589 | TAILQ_FOREACH_SAFE(tw_tp, &tcp_tw_tailq, t_twentry, tw_ntp) { | |
590 | /* | |
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. | |
15129b1c A |
595 | * |
596 | * Sometimes a socket on time-wait queue can be closed if | |
597 | * 2MSL timer expired but the application still has a | |
598 | * usecount on it. | |
39236c6e | 599 | */ |
15129b1c A |
600 | if (tw_tp->t_state == TCPS_CLOSED || |
601 | TSTMP_GEQ(tcp_now, tw_tp->t_timer[TCPT_2MSL])) { | |
39236c6e A |
602 | if (tcp_garbage_collect(tw_tp->t_inpcb, 1)) |
603 | atomic_add_32(&ipi->ipi_gc_req.intimer_lazy, 1); | |
2d21ac55 | 604 | } |
91447636 A |
605 | } |
606 | ||
39236c6e A |
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); | |
91447636 | 609 | |
39236c6e | 610 | lck_rw_done(ipi->ipi_lock); |
1c79356b | 611 | |
39236c6e A |
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); | |
91447636 | 615 | |
39236c6e A |
616 | KERNEL_DEBUG(DBG_FNC_TCP_SLOW | DBG_FUNC_END, tws_checked, |
617 | cur_tw_slot, 0, 0, 0); | |
618 | ||
619 | return; | |
1c79356b A |
620 | } |
621 | ||
622 | /* | |
623 | * Cancel all timers for TCP tp. | |
624 | */ | |
625 | void | |
626 | tcp_canceltimers(tp) | |
627 | struct tcpcb *tp; | |
628 | { | |
629 | register int i; | |
630 | ||
6d2010ae | 631 | tcp_remove_timer(tp); |
1c79356b A |
632 | for (i = 0; i < TCPT_NTIMERS; i++) |
633 | tp->t_timer[i] = 0; | |
6d2010ae A |
634 | tp->tentry.timer_start = tcp_now; |
635 | tp->tentry.index = TCPT_NONE; | |
1c79356b A |
636 | } |
637 | ||
9bccf70c A |
638 | int tcp_syn_backoff[TCP_MAXRXTSHIFT + 1] = |
639 | { 1, 1, 1, 1, 1, 2, 4, 8, 16, 32, 64, 64, 64 }; | |
640 | ||
1c79356b A |
641 | int tcp_backoff[TCP_MAXRXTSHIFT + 1] = |
642 | { 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64 }; | |
643 | ||
644 | static int tcp_totbackoff = 511; /* sum of tcp_backoff[] */ | |
645 | ||
3e170ce0 | 646 | void tcp_rexmt_save_state(struct tcpcb *tp) |
39236c6e A |
647 | { |
648 | u_int32_t fsize; | |
649 | if (TSTMP_SUPPORTED(tp)) { | |
650 | /* | |
651 | * Since timestamps are supported on the connection, | |
652 | * we can do recovery as described in rfc 4015. | |
653 | */ | |
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; | |
657 | } else { | |
658 | /* | |
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. | |
667 | */ | |
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; | |
673 | else | |
674 | tp->t_flags &= ~TF_WASFRECOVERY; | |
675 | } | |
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); | |
679 | } | |
680 | ||
fe8ab488 A |
681 | /* |
682 | * Revert to the older segment size if there is an indication that PMTU | |
683 | * blackhole detection was not needed. | |
684 | */ | |
685 | void tcp_pmtud_revert_segment_size(struct tcpcb *tp) | |
686 | { | |
687 | int32_t optlen; | |
688 | ||
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; | |
695 | /* | |
696 | * Reset the slow-start flight size as it | |
697 | * may depend on the new MSS | |
698 | */ | |
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++; | |
703 | } | |
704 | ||
1c79356b A |
705 | /* |
706 | * TCP timer processing. | |
707 | */ | |
708 | struct tcpcb * | |
709 | tcp_timers(tp, timer) | |
710 | register struct tcpcb *tp; | |
711 | int timer; | |
712 | { | |
fe8ab488 | 713 | int32_t rexmt, optlen = 0, idle_time = 0; |
316670eb | 714 | struct socket *so; |
9bccf70c | 715 | struct tcptemp *t_template; |
55e303ae A |
716 | #if TCPDEBUG |
717 | int ostate; | |
718 | #endif | |
719 | ||
1c79356b A |
720 | #if INET6 |
721 | int isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV4) == 0; | |
722 | #endif /* INET6 */ | |
3e170ce0 A |
723 | u_int64_t accsleep_ms; |
724 | u_int32_t last_sleep_ms = 0; | |
1c79356b | 725 | |
316670eb | 726 | so = tp->t_inpcb->inp_socket; |
6d2010ae | 727 | idle_time = tcp_now - tp->t_rcvtime; |
9bccf70c | 728 | |
1c79356b A |
729 | switch (timer) { |
730 | ||
731 | /* | |
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 | |
2d21ac55 A |
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. | |
1c79356b A |
738 | */ |
739 | case TCPT_2MSL: | |
8ad349bb | 740 | tcp_free_sackholes(tp); |
1c79356b | 741 | if (tp->t_state != TCPS_TIME_WAIT && |
2d21ac55 | 742 | tp->t_state != TCPS_FIN_WAIT_2 && |
39236c6e A |
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)); | |
746 | } else { | |
1c79356b | 747 | tp = tcp_close(tp); |
91447636 A |
748 | return(tp); |
749 | } | |
1c79356b A |
750 | break; |
751 | ||
752 | /* | |
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. | |
756 | */ | |
757 | case TCPT_REXMT: | |
3e170ce0 A |
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; | |
fe8ab488 A |
761 | /* |
762 | * Drop a connection in the retransmit timer | |
763 | * 1. If we have retransmitted more than TCP_MAXRXTSHIFT | |
764 | * times | |
765 | * 2. If the time spent in this retransmission episode is | |
766 | * more than the time limit set with TCP_RXT_CONNDROPTIME | |
767 | * socket option | |
768 | * 3. If TCP_RXT_FINDROP socket option was set and | |
769 | * we have already retransmitted the FIN 3 times without | |
770 | * receiving an ack | |
6d2010ae A |
771 | */ |
772 | if (++tp->t_rxtshift > TCP_MAXRXTSHIFT || | |
3e170ce0 A |
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)) { | |
6d2010ae A |
778 | if ((tp->t_flagsext & TF_RXTFINDROP) != 0) { |
779 | tcpstat.tcps_rxtfindrop++; | |
3e170ce0 A |
780 | } else if (last_sleep_ms >= TCP_SLEEP_TOO_LONG) { |
781 | tcpstat.tcps_drop_after_sleep++; | |
6d2010ae A |
782 | } else { |
783 | tcpstat.tcps_timeoutdrop++; | |
784 | } | |
4bd07ac2 A |
785 | if (tp->t_rxtshift >= TCP_MAXRXTSHIFT) { |
786 | if (TCP_ECN_ENABLED(tp)) { | |
787 | INP_INC_IFNET_STAT(tp->t_inpcb, | |
788 | ecn_on.rxmit_drop); | |
789 | } else { | |
790 | INP_INC_IFNET_STAT(tp->t_inpcb, | |
791 | ecn_off.rxmit_drop); | |
792 | } | |
793 | } | |
1c79356b | 794 | tp->t_rxtshift = TCP_MAXRXTSHIFT; |
316670eb A |
795 | postevent(so, 0, EV_TIMEOUT); |
796 | soevent(so, | |
797 | (SO_FILT_HINT_LOCKED|SO_FILT_HINT_TIMEOUT)); | |
1c79356b A |
798 | tp = tcp_drop(tp, tp->t_softerror ? |
799 | tp->t_softerror : ETIMEDOUT); | |
316670eb | 800 | |
1c79356b A |
801 | break; |
802 | } | |
9bccf70c | 803 | |
39236c6e | 804 | tcpstat.tcps_rexmttimeo++; |
3e170ce0 | 805 | tp->t_accsleep_ms = accsleep_ms; |
6d2010ae | 806 | |
39236c6e A |
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; | |
811 | ||
812 | /* | |
813 | * if this is the first retransmit timeout, save | |
814 | * the state so that we can recover if the timeout | |
815 | * is spurious. | |
816 | */ | |
817 | tcp_rexmt_save_state(tp); | |
818 | } | |
819 | #if MPTCP | |
fe8ab488 | 820 | if ((tp->t_rxtshift >= mptcp_fail_thresh) && |
39236c6e A |
821 | (tp->t_state == TCPS_ESTABLISHED) && |
822 | (tp->t_mpflags & TMPF_MPTCP_TRUE)) { | |
823 | mptcp_act_on_txfail(so); | |
824 | ||
825 | } | |
826 | #endif /* MPTCP */ | |
827 | ||
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 */ | |
832 | soevent(so, | |
833 | (SO_FILT_HINT_LOCKED| | |
834 | SO_FILT_HINT_ADAPTIVE_WTIMO)); | |
9bccf70c | 835 | } |
316670eb | 836 | |
fe8ab488 A |
837 | /* |
838 | * If this is a retransmit timeout after PTO, the PTO | |
839 | * was not effective | |
840 | */ | |
841 | if (tp->t_flagsext & TF_SENT_TLPROBE) { | |
842 | tp->t_flagsext &= ~(TF_SENT_TLPROBE); | |
843 | tcpstat.tcps_rto_after_pto++; | |
844 | } | |
845 | ||
846 | if (tp->t_flagsext & TF_DELAY_RECOVERY) { | |
847 | /* | |
848 | * Retransmit timer fired before entering recovery | |
849 | * on a connection with packet re-ordering. This | |
850 | * suggests that the reordering metrics computed | |
851 | * are not accurate. | |
852 | */ | |
853 | tp->t_reorderwin = 0; | |
854 | tp->t_timer[TCPT_DELAYFR] = 0; | |
855 | tp->t_flagsext &= ~(TF_DELAY_RECOVERY); | |
856 | } | |
857 | ||
3e170ce0 A |
858 | if (tp->t_state == TCPS_SYN_RECEIVED) |
859 | tcp_disable_tfo(tp); | |
860 | ||
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)) { | |
865 | /* | |
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. | |
870 | * | |
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. | |
873 | */ | |
874 | tcp_heuristic_tfo_middlebox(tp); | |
875 | ||
876 | so->so_error = ENODATA; | |
877 | sorwakeup(so); | |
878 | sowwakeup(so); | |
879 | } | |
880 | ||
39236c6e | 881 | if (tp->t_state == TCPS_SYN_SENT) { |
9bccf70c | 882 | rexmt = TCP_REXMTVAL(tp) * tcp_syn_backoff[tp->t_rxtshift]; |
39236c6e | 883 | tp->t_stat.synrxtshift = tp->t_rxtshift; |
3e170ce0 A |
884 | |
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; | |
889 | ||
890 | tp->t_tfo_stats |= TFO_S_SYN_LOSS; | |
891 | tcpstat.tcps_tfo_syn_loss++; | |
892 | } | |
fe8ab488 | 893 | } else { |
9bccf70c | 894 | rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift]; |
fe8ab488 A |
895 | } |
896 | ||
490019cf | 897 | TCPT_RANGESET(tp->t_rxtcur, rexmt, tp->t_rttmin, TCPTV_REXMTMAX, |
6d2010ae A |
898 | TCP_ADD_REXMTSLOP(tp)); |
899 | tp->t_timer[TCPT_REXMT] = OFFSET_FROM_START(tp, tp->t_rxtcur); | |
9bccf70c | 900 | |
316670eb A |
901 | if (INP_WAIT_FOR_IF_FEEDBACK(tp->t_inpcb)) |
902 | goto fc_output; | |
903 | ||
904 | tcp_free_sackholes(tp); | |
9bccf70c | 905 | /* |
fe8ab488 | 906 | * Check for potential Path MTU Discovery Black Hole |
b0d623f7 | 907 | */ |
fe8ab488 A |
908 | if (tcp_pmtud_black_hole_detect && |
909 | !(tp->t_flagsext & TF_NOBLACKHOLE_DETECTION) && | |
910 | (tp->t_state == TCPS_ESTABLISHED)) { | |
3e170ce0 A |
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) { | |
b0d623f7 A |
915 | /* |
916 | * Enter Path MTU Black-hole Detection mechanism: | |
917 | * - Disable Path MTU Discovery (IP "DF" bit). | |
fe8ab488 A |
918 | * - Reduce MTU to lower value than what we |
919 | * negotiated with the peer. | |
b0d623f7 | 920 | */ |
39236c6e A |
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; | |
b0d623f7 | 925 | optlen = tp->t_maxopd - tp->t_maxseg; |
39236c6e A |
926 | /* Keep track of previous MSS */ |
927 | tp->t_pmtud_saved_maxopd = tp->t_maxopd; | |
fe8ab488 A |
928 | tp->t_pmtud_start_ts = tcp_now; |
929 | if (tp->t_pmtud_start_ts == 0) | |
930 | tp->t_pmtud_start_ts++; | |
39236c6e A |
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; | |
934 | } else { | |
b0d623f7 A |
935 | tp->t_maxopd = /* use the default MSS */ |
936 | #if INET6 | |
937 | isipv6 ? tcp_v6mssdflt : | |
938 | #endif /* INET6 */ | |
939 | tcp_mssdflt; | |
940 | } | |
941 | tp->t_maxseg = tp->t_maxopd - optlen; | |
6d2010ae A |
942 | |
943 | /* | |
39236c6e A |
944 | * Reset the slow-start flight size |
945 | * as it may depend on the new MSS | |
6d2010ae A |
946 | */ |
947 | if (CC_ALGO(tp)->cwnd_init != NULL) | |
948 | CC_ALGO(tp)->cwnd_init(tp); | |
b0d623f7 A |
949 | } |
950 | /* | |
fe8ab488 A |
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. | |
b0d623f7 A |
955 | */ |
956 | else { | |
957 | ||
fe8ab488 A |
958 | if ((tp->t_flags & TF_BLACKHOLE) && |
959 | (tp->t_rxtshift > 4)) { | |
960 | tcp_pmtud_revert_segment_size(tp); | |
b0d623f7 A |
961 | } |
962 | } | |
963 | } | |
964 | ||
965 | ||
966 | /* | |
fe8ab488 A |
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. | |
39236c6e | 973 | * Do this only on non-local connections. |
9bccf70c | 974 | */ |
39236c6e | 975 | if (tp->t_state == TCPS_SYN_SENT && |
3e170ce0 | 976 | tp->t_rxtshift == tcp_broken_peer_syn_rxmit_thres) |
b0d623f7 | 977 | tp->t_flags &= ~(TF_REQ_SCALE|TF_REQ_TSTMP|TF_REQ_CC); |
316670eb | 978 | |
1c79356b A |
979 | /* |
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. | |
986 | */ | |
987 | if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) { | |
988 | #if INET6 | |
989 | if (isipv6) | |
990 | in6_losing(tp->t_inpcb); | |
991 | else | |
992 | #endif /* INET6 */ | |
993 | in_losing(tp->t_inpcb); | |
994 | tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT); | |
995 | tp->t_srtt = 0; | |
996 | } | |
997 | tp->snd_nxt = tp->snd_una; | |
9bccf70c A |
998 | /* |
999 | * Note: We overload snd_recover to function also as the | |
1000 | * snd_last variable described in RFC 2582 | |
1001 | */ | |
1002 | tp->snd_recover = tp->snd_max; | |
1c79356b A |
1003 | /* |
1004 | * Force a segment to be sent. | |
1005 | */ | |
1006 | tp->t_flags |= TF_ACKNOW; | |
fe8ab488 A |
1007 | |
1008 | /* If timing a segment in this window, stop the timer */ | |
9bccf70c | 1009 | tp->t_rtttime = 0; |
6d2010ae | 1010 | |
fe8ab488 A |
1011 | if (!IN_FASTRECOVERY(tp) && tp->t_rxtshift == 1) |
1012 | tcpstat.tcps_tailloss_rto++; | |
1013 | ||
39236c6e | 1014 | |
fe8ab488 A |
1015 | /* |
1016 | * RFC 5681 says: when a TCP sender detects segment loss | |
39236c6e A |
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 | |
1020 | */ | |
1021 | if (tp->t_rxtshift == 1 && | |
3e170ce0 | 1022 | CC_ALGO(tp)->after_timeout != NULL) { |
6d2010ae | 1023 | CC_ALGO(tp)->after_timeout(tp); |
3e170ce0 A |
1024 | /* |
1025 | * CWR notifications are to be sent on new data | |
1026 | * right after Fast Retransmits and ECE | |
1027 | * notification receipts. | |
1028 | */ | |
1029 | if (TCP_ECN_ENABLED(tp)) | |
1030 | tp->ecn_flags |= TE_SENDCWR; | |
1031 | } | |
6d2010ae | 1032 | |
fe8ab488 | 1033 | EXIT_FASTRECOVERY(tp); |
6d2010ae | 1034 | |
3e170ce0 A |
1035 | /* Exit cwnd non validated phase */ |
1036 | tp->t_flagsext &= ~TF_CWND_NONVALIDATED; | |
1037 | ||
1038 | ||
316670eb | 1039 | fc_output: |
fe8ab488 | 1040 | tcp_ccdbg_trace(tp, NULL, TCP_CC_REXMT_TIMEOUT); |
6d2010ae | 1041 | |
1c79356b A |
1042 | (void) tcp_output(tp); |
1043 | break; | |
1044 | ||
1045 | /* | |
1046 | * Persistance timer into zero window. | |
1047 | * Force a byte to be output, if possible. | |
1048 | */ | |
1049 | case TCPT_PERSIST: | |
1050 | tcpstat.tcps_persisttimeo++; | |
1051 | /* | |
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. | |
6d2010ae A |
1057 | * |
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 | |
1c79356b | 1061 | */ |
6d2010ae A |
1062 | if ((tp->t_rxtshift == TCP_MAXRXTSHIFT && |
1063 | (idle_time >= tcp_maxpersistidle || | |
1064 | idle_time >= TCP_REXMTVAL(tp) * tcp_totbackoff)) || | |
316670eb A |
1065 | ((tp->t_persist_stop != 0) && |
1066 | TSTMP_LEQ(tp->t_persist_stop, tcp_now))) { | |
1c79356b | 1067 | tcpstat.tcps_persistdrop++; |
316670eb A |
1068 | postevent(so, 0, EV_TIMEOUT); |
1069 | soevent(so, | |
1070 | (SO_FILT_HINT_LOCKED|SO_FILT_HINT_TIMEOUT)); | |
1c79356b | 1071 | tp = tcp_drop(tp, ETIMEDOUT); |
1c79356b A |
1072 | break; |
1073 | } | |
1074 | tcp_setpersist(tp); | |
fe8ab488 | 1075 | tp->t_flagsext |= TF_FORCE; |
1c79356b | 1076 | (void) tcp_output(tp); |
fe8ab488 | 1077 | tp->t_flagsext &= ~TF_FORCE; |
1c79356b A |
1078 | break; |
1079 | ||
1080 | /* | |
1081 | * Keep-alive timer went off; send something | |
1082 | * or drop connection if idle for too long. | |
1083 | */ | |
1084 | case TCPT_KEEP: | |
1085 | tcpstat.tcps_keeptimeo++; | |
39236c6e A |
1086 | #if MPTCP |
1087 | /* | |
1088 | * Regular TCP connections do not send keepalives after closing | |
1089 | * MPTCP must not also, after sending Data FINs. | |
1090 | */ | |
1091 | struct mptcb *mp_tp = tp->t_mptcb; | |
fe8ab488 A |
1092 | if ((tp->t_mpflags & TMPF_MPTCP_TRUE) && |
1093 | (tp->t_state > TCPS_ESTABLISHED)) { | |
39236c6e A |
1094 | goto dropit; |
1095 | } else if (mp_tp != NULL) { | |
1096 | if ((mptcp_ok_to_keepalive(mp_tp) == 0)) | |
1097 | goto dropit; | |
1098 | } | |
1099 | #endif /* MPTCP */ | |
1c79356b A |
1100 | if (tp->t_state < TCPS_ESTABLISHED) |
1101 | goto dropit; | |
1102 | if ((always_keepalive || | |
39236c6e | 1103 | (tp->t_inpcb->inp_socket->so_options & SO_KEEPALIVE) || |
3e170ce0 A |
1104 | (tp->t_flagsext & TF_DETECT_READSTALL) || |
1105 | (tp->t_tfo_probe_state == TFO_PROBE_PROBING)) && | |
2d21ac55 | 1106 | (tp->t_state <= TCPS_CLOSING || tp->t_state == TCPS_FIN_WAIT_2)) { |
39236c6e | 1107 | if (idle_time >= TCP_CONN_KEEPIDLE(tp) + TCP_CONN_MAXIDLE(tp)) |
1c79356b A |
1108 | goto dropit; |
1109 | /* | |
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. | |
1120 | */ | |
1121 | tcpstat.tcps_keepprobe++; | |
9bccf70c A |
1122 | t_template = tcp_maketemplate(tp); |
1123 | if (t_template) { | |
fe8ab488 A |
1124 | struct inpcb *inp = tp->t_inpcb; |
1125 | struct tcp_respond_args tra; | |
c910b4d9 | 1126 | |
fe8ab488 A |
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); | |
c910b4d9 | 1131 | if (tp->t_inpcb->inp_flags & INP_BOUND_IF) |
fe8ab488 | 1132 | tra.ifscope = tp->t_inpcb->inp_boundifp->if_index; |
c910b4d9 | 1133 | else |
fe8ab488 | 1134 | tra.ifscope = IFSCOPE_NONE; |
9bccf70c A |
1135 | tcp_respond(tp, t_template->tt_ipgen, |
1136 | &t_template->tt_t, (struct mbuf *)NULL, | |
fe8ab488 | 1137 | tp->rcv_nxt, tp->snd_una - 1, 0, &tra); |
9bccf70c | 1138 | (void) m_free(dtom(t_template)); |
39236c6e A |
1139 | if (tp->t_flagsext & TF_DETECT_READSTALL) |
1140 | tp->t_rtimo_probes++; | |
1141 | } | |
1142 | tp->t_timer[TCPT_KEEP] = OFFSET_FROM_START(tp, | |
3e170ce0 | 1143 | TCP_CONN_KEEPINTVL(tp)); |
39236c6e A |
1144 | } else { |
1145 | tp->t_timer[TCPT_KEEP] = OFFSET_FROM_START(tp, | |
3e170ce0 | 1146 | TCP_CONN_KEEPIDLE(tp)); |
39236c6e A |
1147 | } |
1148 | if (tp->t_flagsext & TF_DETECT_READSTALL) { | |
3e170ce0 A |
1149 | struct ifnet *outifp = tp->t_inpcb->inp_last_outifp; |
1150 | bool reenable_probe = false; | |
39236c6e A |
1151 | /* |
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. | |
1157 | */ | |
3e170ce0 A |
1158 | if (tp->t_adaptive_rtimo > 0) { |
1159 | if (tp->t_rtimo_probes > tp->t_adaptive_rtimo) { | |
1160 | /* Generate an event */ | |
1161 | soevent(so, | |
1162 | (SO_FILT_HINT_LOCKED | | |
1163 | SO_FILT_HINT_ADAPTIVE_RTIMO)); | |
1164 | tcp_keepalive_reset(tp); | |
1165 | } else { | |
1166 | reenable_probe = true; | |
1167 | } | |
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; | |
39236c6e | 1172 | } else { |
3e170ce0 A |
1173 | tp->t_flagsext &= ~TF_DETECT_READSTALL; |
1174 | } | |
1175 | if (reenable_probe) { | |
1176 | int ind = min(tp->t_rtimo_probes, | |
1177 | TCP_MAXRXTSHIFT); | |
39236c6e | 1178 | tp->t_timer[TCPT_KEEP] = OFFSET_FROM_START( |
3e170ce0 | 1179 | tp, tcp_backoff[ind] * TCP_REXMTVAL(tp)); |
9bccf70c | 1180 | } |
39236c6e | 1181 | } |
3e170ce0 A |
1182 | if (tp->t_tfo_probe_state == TFO_PROBE_PROBING) { |
1183 | int ind; | |
1184 | ||
1185 | tp->t_tfo_probes++; | |
1186 | ind = min(tp->t_tfo_probes, TCP_MAXRXTSHIFT); | |
1187 | ||
1188 | /* | |
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 | |
1193 | * backing off. | |
1194 | */ | |
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); | |
1201 | ||
1202 | so->so_error = ENODATA; | |
1203 | sorwakeup(so); | |
1204 | tcpstat.tcps_tfo_blackhole++; | |
1205 | } | |
6d2010ae A |
1206 | break; |
1207 | case TCPT_DELACK: | |
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; | |
1212 | ||
fe8ab488 A |
1213 | /* |
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 | |
1218 | * connection | |
1219 | * | |
1220 | * Also, go back to acking every other packet. | |
6d2010ae | 1221 | */ |
fe8ab488 A |
1222 | if ((tp->t_flags & TF_STRETCHACK)) { |
1223 | if (tp->t_unacksegs > 1 && | |
1224 | tp->t_unacksegs < maxseg_unacked) | |
1225 | tp->t_stretchack_delayed++; | |
1226 | ||
1227 | if (tp->t_stretchack_delayed > | |
1228 | TCP_STRETCHACK_DELAY_THRESHOLD) { | |
1229 | tp->t_flagsext |= TF_DISABLE_STRETCHACK; | |
1230 | /* | |
1231 | * Note the time at which stretch | |
1232 | * ack was disabled automatically | |
1233 | */ | |
1234 | tp->rcv_nostrack_ts = tcp_now; | |
1235 | tcpstat.tcps_nostretchack++; | |
1236 | tp->t_stretchack_delayed = 0; | |
1237 | } | |
6d2010ae | 1238 | tcp_reset_stretch_ack(tp); |
fe8ab488 | 1239 | } |
6d2010ae | 1240 | |
fe8ab488 A |
1241 | /* |
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. | |
316670eb A |
1246 | */ |
1247 | CLEAR_IAJ_STATE(tp); | |
1248 | ||
6d2010ae A |
1249 | tcpstat.tcps_delack++; |
1250 | (void) tcp_output(tp); | |
1251 | } | |
1c79356b | 1252 | break; |
9bccf70c | 1253 | |
39236c6e A |
1254 | #if MPTCP |
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); | |
1262 | soevent(so, | |
1263 | (SO_FILT_HINT_LOCKED| | |
1264 | SO_FILT_HINT_TIMEOUT)); | |
1265 | tp = tcp_drop(tp, tp->t_softerror ? | |
1266 | tp->t_softerror : ETIMEDOUT); | |
1267 | break; | |
1268 | } | |
1269 | tcpstat.tcps_join_rxmts++; | |
1270 | tp->t_flags |= TF_ACKNOW; | |
1271 | ||
1272 | /* | |
1273 | * No backoff is implemented for simplicity for this | |
1274 | * corner case. | |
1275 | */ | |
1276 | (void) tcp_output(tp); | |
1277 | } | |
1278 | break; | |
1279 | #endif /* MPTCP */ | |
1280 | ||
fe8ab488 A |
1281 | case TCPT_PTO: |
1282 | { | |
fe8ab488 | 1283 | int32_t snd_len; |
fe8ab488 A |
1284 | tp->t_flagsext &= ~(TF_SENT_TLPROBE); |
1285 | ||
1286 | /* | |
1287 | * Check if the connection is in the right state to | |
1288 | * send a probe | |
1289 | */ | |
1290 | if (tp->t_state != TCPS_ESTABLISHED || | |
3e170ce0 A |
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)) | |
fe8ab488 A |
1295 | break; |
1296 | ||
3e170ce0 A |
1297 | /* |
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 | |
1301 | * new data. | |
1302 | */ | |
1303 | snd_len = min(so->so_snd.sb_cc, tp->snd_wnd) | |
1304 | - (tp->snd_max - tp->snd_una); | |
1305 | if (snd_len > 0) { | |
1306 | tp->snd_nxt = tp->snd_max; | |
1307 | } else { | |
1308 | snd_len = min((tp->snd_max - tp->snd_una), | |
1309 | tp->t_maxseg); | |
1310 | tp->snd_nxt = tp->snd_max - snd_len; | |
1311 | } | |
1312 | ||
fe8ab488 | 1313 | tcpstat.tcps_pto++; |
3e170ce0 A |
1314 | if (tp->t_flagsext & TF_PROBING) |
1315 | tcpstat.tcps_probe_if++; | |
fe8ab488 A |
1316 | |
1317 | /* If timing a segment in this window, stop the timer */ | |
1318 | tp->t_rtttime = 0; | |
fe8ab488 A |
1319 | /* Note that tail loss probe is being sent */ |
1320 | tp->t_flagsext |= TF_SENT_TLPROBE; | |
1321 | tp->t_tlpstart = tcp_now; | |
1322 | ||
1323 | tp->snd_cwnd += tp->t_maxseg; | |
1324 | (void )tcp_output(tp); | |
1325 | tp->snd_cwnd -= tp->t_maxseg; | |
1326 | ||
1327 | tp->t_tlphighrxt = tp->snd_nxt; | |
fe8ab488 A |
1328 | break; |
1329 | } | |
1330 | case TCPT_DELAYFR: | |
1331 | tp->t_flagsext &= ~TF_DELAY_RECOVERY; | |
1332 | ||
1333 | /* | |
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 | |
1338 | */ | |
1339 | if (IN_FASTRECOVERY(tp) || | |
1340 | SEQ_GEQ(tp->snd_una, tp->snd_recover) || | |
1341 | tp->t_rxtshift > 0) | |
1342 | break; | |
1343 | ||
1344 | VERIFY(SACK_ENABLED(tp)); | |
3e170ce0 A |
1345 | tcp_rexmt_save_state(tp); |
1346 | if (CC_ALGO(tp)->pre_fr != NULL) { | |
fe8ab488 | 1347 | CC_ALGO(tp)->pre_fr(tp); |
3e170ce0 A |
1348 | if (TCP_ECN_ENABLED(tp)) |
1349 | tp->ecn_flags |= TE_SENDCWR; | |
1350 | } | |
fe8ab488 | 1351 | ENTER_FASTRECOVERY(tp); |
fe8ab488 A |
1352 | |
1353 | tp->t_timer[TCPT_REXMT] = 0; | |
1354 | tcpstat.tcps_sack_recovery_episode++; | |
4bd07ac2 | 1355 | tp->t_sack_recovery_episode++; |
fe8ab488 A |
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); | |
1360 | break; | |
1c79356b A |
1361 | dropit: |
1362 | tcpstat.tcps_keepdrops++; | |
316670eb A |
1363 | postevent(so, 0, EV_TIMEOUT); |
1364 | soevent(so, | |
1365 | (SO_FILT_HINT_LOCKED|SO_FILT_HINT_TIMEOUT)); | |
1c79356b | 1366 | tp = tcp_drop(tp, ETIMEDOUT); |
1c79356b A |
1367 | break; |
1368 | } | |
fe8ab488 A |
1369 | #if TCPDEBUG |
1370 | if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG) | |
1371 | tcp_trace(TA_USER, ostate, tp, (void *)0, (struct tcphdr *)0, | |
1372 | PRU_SLOWTIMO); | |
1373 | #endif | |
1c79356b A |
1374 | return (tp); |
1375 | } | |
6d2010ae A |
1376 | |
1377 | /* Remove a timer entry from timer list */ | |
1378 | void | |
1379 | tcp_remove_timer(struct tcpcb *tp) | |
1380 | { | |
1381 | struct tcptimerlist *listp = &tcp_timer_list; | |
1382 | ||
1383 | lck_mtx_assert(&tp->t_inpcb->inpcb_mtx, LCK_MTX_ASSERT_OWNED); | |
1384 | if (!(TIMER_IS_ON_LIST(tp))) { | |
1385 | return; | |
1386 | } | |
1387 | lck_mtx_lock(listp->mtx); | |
1388 | ||
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); | |
1392 | return; | |
1393 | } | |
1394 | ||
1395 | if (listp->next_te != NULL && listp->next_te == &tp->tentry) | |
1396 | listp->next_te = LIST_NEXT(&tp->tentry, le); | |
1397 | ||
1398 | LIST_REMOVE(&tp->tentry, le); | |
1399 | tp->t_flags &= ~(TF_TIMER_ONLIST); | |
1400 | ||
1401 | listp->entries--; | |
6d2010ae A |
1402 | |
1403 | tp->tentry.le.le_next = NULL; | |
1404 | tp->tentry.le.le_prev = NULL; | |
ebb1b9f4 | 1405 | lck_mtx_unlock(listp->mtx); |
6d2010ae A |
1406 | } |
1407 | ||
fe8ab488 A |
1408 | /* |
1409 | * Function to check if the timerlist needs to be rescheduled to run | |
6d2010ae A |
1410 | * the timer entry correctly. Basically, this is to check if we can avoid |
1411 | * taking the list lock. | |
1412 | */ | |
1413 | ||
1414 | static boolean_t | |
fe8ab488 A |
1415 | need_to_resched_timerlist(u_int32_t runtime, u_int16_t mode) |
1416 | { | |
6d2010ae A |
1417 | struct tcptimerlist *listp = &tcp_timer_list; |
1418 | int32_t diff; | |
6d2010ae | 1419 | |
fe8ab488 A |
1420 | /* |
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 | |
1423 | * correctly. | |
6d2010ae | 1424 | */ |
8a3053a0 | 1425 | if (listp->running) |
fe8ab488 | 1426 | return (TRUE); |
8a3053a0 A |
1427 | |
1428 | if (!listp->scheduled) | |
1429 | return (TRUE); | |
6d2010ae A |
1430 | |
1431 | diff = timer_diff(listp->runtime, 0, runtime, 0); | |
1432 | if (diff <= 0) { | |
1433 | /* The list is going to run before this timer */ | |
fe8ab488 | 1434 | return (FALSE); |
6d2010ae | 1435 | } else { |
fe8ab488 A |
1436 | if (mode & TCP_TIMERLIST_10MS_MODE) { |
1437 | if (diff <= TCP_TIMER_10MS_QUANTUM) | |
1438 | return (FALSE); | |
1439 | } else if (mode & TCP_TIMERLIST_100MS_MODE) { | |
1440 | if (diff <= TCP_TIMER_100MS_QUANTUM) | |
1441 | return (FALSE); | |
6d2010ae | 1442 | } else { |
fe8ab488 A |
1443 | if (diff <= TCP_TIMER_500MS_QUANTUM) |
1444 | return (FALSE); | |
6d2010ae A |
1445 | } |
1446 | } | |
fe8ab488 | 1447 | return (TRUE); |
6d2010ae A |
1448 | } |
1449 | ||
1450 | void | |
1451 | tcp_sched_timerlist(uint32_t offset) | |
1452 | { | |
6d2010ae A |
1453 | uint64_t deadline = 0; |
1454 | struct tcptimerlist *listp = &tcp_timer_list; | |
1455 | ||
1456 | lck_mtx_assert(listp->mtx, LCK_MTX_ASSERT_OWNED); | |
1457 | ||
8a3053a0 | 1458 | offset = min(offset, TCP_TIMERLIST_MAX_OFFSET); |
6d2010ae | 1459 | listp->runtime = tcp_now + offset; |
fe8ab488 | 1460 | if (listp->runtime == 0) { |
8a3053a0 | 1461 | listp->runtime++; |
fe8ab488 A |
1462 | offset++; |
1463 | } | |
6d2010ae | 1464 | |
fe8ab488 | 1465 | clock_interval_to_deadline(offset, USEC_PER_SEC, &deadline); |
6d2010ae A |
1466 | |
1467 | thread_call_enter_delayed(listp->call, deadline); | |
8a3053a0 | 1468 | listp->scheduled = TRUE; |
6d2010ae A |
1469 | } |
1470 | ||
fe8ab488 A |
1471 | /* |
1472 | * Function to run the timers for a connection. | |
6d2010ae A |
1473 | * |
1474 | * Returns the offset of next timer to be run for this connection which | |
1475 | * can be used to reschedule the timerlist. | |
fe8ab488 A |
1476 | * |
1477 | * te_mode is an out parameter that indicates the modes of active | |
1478 | * timers for this connection. | |
6d2010ae | 1479 | */ |
fe8ab488 | 1480 | u_int32_t |
3e170ce0 A |
1481 | tcp_run_conn_timer(struct tcpcb *tp, u_int16_t *te_mode, |
1482 | u_int16_t probe_if_index) | |
1483 | { | |
fe8ab488 A |
1484 | struct socket *so; |
1485 | u_int16_t i = 0, index = TCPT_NONE, lo_index = TCPT_NONE; | |
1486 | u_int32_t timer_val, offset = 0, lo_timer = 0; | |
6d2010ae A |
1487 | int32_t diff; |
1488 | boolean_t needtorun[TCPT_NTIMERS]; | |
1489 | int count = 0; | |
1490 | ||
fe8ab488 A |
1491 | VERIFY(tp != NULL); |
1492 | bzero(needtorun, sizeof(needtorun)); | |
1493 | *te_mode = 0; | |
6d2010ae | 1494 | |
fe8ab488 | 1495 | tcp_lock(tp->t_inpcb->inp_socket, 1, 0); |
6d2010ae | 1496 | |
fe8ab488 | 1497 | so = tp->t_inpcb->inp_socket; |
6d2010ae | 1498 | /* Release the want count on inp */ |
fe8ab488 A |
1499 | if (in_pcb_checkstate(tp->t_inpcb, WNT_RELEASE, 1) |
1500 | == WNT_STOPUSING) { | |
6d2010ae A |
1501 | if (TIMER_IS_ON_LIST(tp)) { |
1502 | tcp_remove_timer(tp); | |
1503 | } | |
1504 | ||
1505 | /* Looks like the TCP connection got closed while we | |
1506 | * were waiting for the lock.. Done | |
1507 | */ | |
1508 | goto done; | |
1509 | } | |
1510 | ||
3e170ce0 A |
1511 | /* |
1512 | * If this connection is over an interface that needs to | |
1513 | * be probed, send probe packets to reinitiate communication. | |
1514 | */ | |
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; | |
1521 | } | |
1522 | ||
fe8ab488 A |
1523 | /* |
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. | |
1527 | */ | |
8a3053a0 | 1528 | if ((index = tp->tentry.index) == TCPT_NONE) |
6d2010ae | 1529 | goto done; |
fe8ab488 | 1530 | |
8a3053a0 | 1531 | timer_val = tp->t_timer[index]; |
6d2010ae A |
1532 | |
1533 | diff = timer_diff(tp->tentry.runtime, 0, tcp_now, 0); | |
1534 | if (diff > 0) { | |
1535 | if (tp->tentry.index != TCPT_NONE) { | |
1536 | offset = diff; | |
fe8ab488 | 1537 | *(te_mode) = tp->tentry.mode; |
6d2010ae A |
1538 | } |
1539 | goto done; | |
1540 | } | |
1541 | ||
1542 | tp->t_timer[index] = 0; | |
1543 | if (timer_val > 0) { | |
1544 | tp = tcp_timers(tp, index); | |
fe8ab488 | 1545 | if (tp == NULL) |
6d2010ae A |
1546 | goto done; |
1547 | } | |
1548 | ||
fe8ab488 A |
1549 | /* |
1550 | * Check if there are any other timers that need to be run. | |
1551 | * While doing it, adjust the timer values wrt tcp_now. | |
6d2010ae | 1552 | */ |
fe8ab488 | 1553 | tp->tentry.mode = 0; |
6d2010ae A |
1554 | for (i = 0; i < TCPT_NTIMERS; ++i) { |
1555 | if (tp->t_timer[i] != 0) { | |
fe8ab488 A |
1556 | diff = timer_diff(tp->tentry.timer_start, |
1557 | tp->t_timer[i], tcp_now, 0); | |
6d2010ae | 1558 | if (diff <= 0) { |
6d2010ae A |
1559 | needtorun[i] = TRUE; |
1560 | count++; | |
1561 | } else { | |
1562 | tp->t_timer[i] = diff; | |
1563 | needtorun[i] = FALSE; | |
1564 | if (lo_timer == 0 || diff < lo_timer) { | |
1565 | lo_timer = diff; | |
1566 | lo_index = i; | |
1567 | } | |
fe8ab488 | 1568 | TCP_SET_TIMER_MODE(tp->tentry.mode, i); |
6d2010ae A |
1569 | } |
1570 | } | |
1571 | } | |
1572 | ||
1573 | tp->tentry.timer_start = tcp_now; | |
1574 | tp->tentry.index = lo_index; | |
fe8ab488 A |
1575 | VERIFY(tp->tentry.index == TCPT_NONE || tp->tentry.mode > 0); |
1576 | ||
1577 | if (tp->tentry.index != TCPT_NONE) { | |
1578 | tp->tentry.runtime = tp->tentry.timer_start + | |
1579 | tp->t_timer[tp->tentry.index]; | |
8a3053a0 A |
1580 | if (tp->tentry.runtime == 0) |
1581 | tp->tentry.runtime++; | |
6d2010ae A |
1582 | } |
1583 | ||
1584 | if (count > 0) { | |
fe8ab488 | 1585 | /* run any other timers outstanding at this time. */ |
6d2010ae A |
1586 | for (i = 0; i < TCPT_NTIMERS; ++i) { |
1587 | if (needtorun[i]) { | |
1588 | tp->t_timer[i] = 0; | |
1589 | tp = tcp_timers(tp, i); | |
8a3053a0 A |
1590 | if (tp == NULL) { |
1591 | offset = 0; | |
fe8ab488 | 1592 | *(te_mode) = 0; |
6d2010ae | 1593 | goto done; |
8a3053a0 | 1594 | } |
6d2010ae A |
1595 | } |
1596 | } | |
1597 | tcp_set_lotimer_index(tp); | |
1598 | } | |
1599 | ||
1600 | if (tp->tentry.index < TCPT_NONE) { | |
1601 | offset = tp->t_timer[tp->tentry.index]; | |
fe8ab488 | 1602 | *(te_mode) = tp->tentry.mode; |
6d2010ae A |
1603 | } |
1604 | ||
1605 | done: | |
1606 | if (tp != NULL && tp->tentry.index == TCPT_NONE) { | |
1607 | tcp_remove_timer(tp); | |
8a3053a0 | 1608 | offset = 0; |
6d2010ae | 1609 | } |
fe8ab488 A |
1610 | |
1611 | tcp_unlock(so, 1, 0); | |
1612 | return(offset); | |
6d2010ae A |
1613 | } |
1614 | ||
1615 | void | |
1616 | tcp_run_timerlist(void * arg1, void * arg2) { | |
6d2010ae | 1617 | #pragma unused(arg1, arg2) |
6d2010ae A |
1618 | struct tcptimerentry *te, *next_te; |
1619 | struct tcptimerlist *listp = &tcp_timer_list; | |
1620 | struct tcpcb *tp; | |
fe8ab488 A |
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 */ | |
6d2010ae | 1624 | uint32_t active_count = 0; |
6d2010ae A |
1625 | |
1626 | calculate_tcp_clock(); | |
1627 | ||
1628 | lck_mtx_lock(listp->mtx); | |
1629 | ||
1630 | listp->running = TRUE; | |
1631 | ||
1632 | LIST_FOREACH_SAFE(te, &listp->lhead, le, next_te) { | |
1633 | uint32_t offset = 0; | |
1634 | uint32_t runtime = te->runtime; | |
8a3053a0 | 1635 | if (te->index < TCPT_NONE && TSTMP_GT(runtime, tcp_now)) { |
6d2010ae A |
1636 | offset = timer_diff(runtime, 0, tcp_now, 0); |
1637 | if (next_timer == 0 || offset < next_timer) { | |
1638 | next_timer = offset; | |
1639 | } | |
fe8ab488 | 1640 | list_mode |= te->mode; |
6d2010ae A |
1641 | continue; |
1642 | } | |
6d2010ae A |
1643 | |
1644 | tp = TIMERENTRY_TO_TP(te); | |
1645 | ||
fe8ab488 A |
1646 | /* |
1647 | * Acquire an inp wantcnt on the inpcb so that the socket | |
1648 | * won't get detached even if tcp_close is called | |
6d2010ae | 1649 | */ |
fe8ab488 A |
1650 | if (in_pcb_checkstate(tp->t_inpcb, WNT_ACQUIRE, 0) |
1651 | == WNT_STOPUSING) { | |
1652 | /* | |
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. | |
6d2010ae A |
1658 | */ |
1659 | if (TIMER_IS_ON_LIST(tp)) { | |
1660 | tp->t_flags &= ~(TF_TIMER_ONLIST); | |
1661 | LIST_REMOVE(&tp->tentry, le); | |
1662 | listp->entries--; | |
1663 | ||
1664 | tp->tentry.le.le_next = NULL; | |
1665 | tp->tentry.le.le_prev = NULL; | |
1666 | } | |
1667 | continue; | |
1668 | } | |
fe8ab488 | 1669 | active_count++; |
6d2010ae | 1670 | |
fe8ab488 A |
1671 | /* |
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. | |
6d2010ae A |
1676 | */ |
1677 | listp->next_te = next_te; | |
1678 | ||
1679 | VERIFY_NEXT_LINK(&tp->tentry, le); | |
1680 | VERIFY_PREV_LINK(&tp->tentry, le); | |
1681 | ||
1682 | lck_mtx_unlock(listp->mtx); | |
1683 | ||
3e170ce0 A |
1684 | offset = tcp_run_conn_timer(tp, &te_mode, |
1685 | listp->probe_if_index); | |
6d2010ae A |
1686 | |
1687 | lck_mtx_lock(listp->mtx); | |
1688 | ||
1689 | next_te = listp->next_te; | |
1690 | listp->next_te = NULL; | |
1691 | ||
fe8ab488 A |
1692 | if (offset > 0 && te_mode != 0) { |
1693 | list_mode |= te_mode; | |
6d2010ae | 1694 | |
fe8ab488 A |
1695 | if (next_timer == 0 || offset < next_timer) |
1696 | next_timer = offset; | |
6d2010ae A |
1697 | } |
1698 | } | |
1699 | ||
1700 | if (!LIST_EMPTY(&listp->lhead)) { | |
fe8ab488 A |
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; | |
1708 | else | |
1709 | next_mode = TCP_TIMERLIST_500MS_MODE; | |
6d2010ae | 1710 | |
fe8ab488 A |
1711 | if (next_mode != TCP_TIMERLIST_500MS_MODE) { |
1712 | listp->idleruns = 0; | |
6d2010ae | 1713 | } else { |
fe8ab488 A |
1714 | /* |
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. | |
1718 | * | |
1719 | * We try to keep the timer list in fast mode for | |
1720 | * some idle time in expectation of new data. | |
1721 | */ | |
1722 | if (listp->mode != next_mode && | |
1723 | listp->idleruns < timer_fastmode_idlemax) { | |
1724 | listp->idleruns++; | |
1725 | next_mode = listp->mode; | |
1726 | next_timer = TCP_TIMER_100MS_QUANTUM; | |
1727 | } else { | |
1728 | listp->idleruns = 0; | |
1729 | } | |
6d2010ae | 1730 | } |
fe8ab488 A |
1731 | listp->mode = next_mode; |
1732 | if (listp->pref_offset != 0) | |
1733 | next_timer = min(listp->pref_offset, next_timer); | |
6d2010ae | 1734 | |
fe8ab488 A |
1735 | if (listp->mode == TCP_TIMERLIST_500MS_MODE) |
1736 | next_timer = max(next_timer, | |
1737 | TCP_TIMER_500MS_QUANTUM); | |
6d2010ae A |
1738 | |
1739 | tcp_sched_timerlist(next_timer); | |
1740 | } else { | |
8a3053a0 A |
1741 | /* |
1742 | * No need to reschedule this timer, but always run | |
1743 | * periodically at a much higher granularity. | |
1744 | */ | |
1745 | tcp_sched_timerlist(TCP_TIMERLIST_MAX_OFFSET); | |
6d2010ae A |
1746 | } |
1747 | ||
1748 | listp->running = FALSE; | |
1749 | listp->pref_mode = 0; | |
1750 | listp->pref_offset = 0; | |
3e170ce0 | 1751 | listp->probe_if_index = 0; |
6d2010ae A |
1752 | |
1753 | lck_mtx_unlock(listp->mtx); | |
1754 | } | |
1755 | ||
fe8ab488 | 1756 | /* |
3e170ce0 | 1757 | * Function to check if the timerlist needs to be rescheduled to run this |
fe8ab488 A |
1758 | * connection's timers correctly. |
1759 | */ | |
6d2010ae A |
1760 | void |
1761 | tcp_sched_timers(struct tcpcb *tp) | |
1762 | { | |
1763 | struct tcptimerentry *te = &tp->tentry; | |
fe8ab488 A |
1764 | u_int16_t index = te->index; |
1765 | u_int16_t mode = te->mode; | |
6d2010ae | 1766 | struct tcptimerlist *listp = &tcp_timer_list; |
8a3053a0 | 1767 | int32_t offset = 0; |
fe8ab488 | 1768 | boolean_t list_locked = FALSE; |
6d2010ae A |
1769 | |
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); | |
1774 | } | |
1775 | return; | |
1776 | } | |
1777 | ||
1778 | if (index == TCPT_NONE) { | |
fe8ab488 | 1779 | /* Nothing to run */ |
6d2010ae A |
1780 | tcp_remove_timer(tp); |
1781 | return; | |
1782 | } | |
1783 | ||
fe8ab488 A |
1784 | /* |
1785 | * compute the offset at which the next timer for this connection | |
1786 | * has to run. | |
1787 | */ | |
8a3053a0 A |
1788 | offset = timer_diff(te->runtime, 0, tcp_now, 0); |
1789 | if (offset <= 0) { | |
6d2010ae A |
1790 | offset = 1; |
1791 | tcp_timer_advanced++; | |
1792 | } | |
6d2010ae A |
1793 | |
1794 | if (!TIMER_IS_ON_LIST(tp)) { | |
1795 | if (!list_locked) { | |
1796 | lck_mtx_lock(listp->mtx); | |
fe8ab488 | 1797 | list_locked = TRUE; |
6d2010ae A |
1798 | } |
1799 | ||
1800 | LIST_INSERT_HEAD(&listp->lhead, te, le); | |
1801 | tp->t_flags |= TF_TIMER_ONLIST; | |
1802 | ||
fe8ab488 A |
1803 | listp->entries++; |
1804 | if (listp->entries > listp->maxentries) | |
1805 | listp->maxentries = listp->entries; | |
6d2010ae A |
1806 | |
1807 | /* if the list is not scheduled, just schedule it */ | |
8a3053a0 | 1808 | if (!listp->scheduled) |
6d2010ae | 1809 | goto schedule; |
6d2010ae A |
1810 | } |
1811 | ||
1812 | ||
fe8ab488 A |
1813 | /* |
1814 | * Timer entry is currently on the list, check if the list needs | |
1815 | * to be rescheduled. | |
1816 | */ | |
1817 | if (need_to_resched_timerlist(te->runtime, mode)) { | |
6d2010ae A |
1818 | tcp_resched_timerlist++; |
1819 | ||
1820 | if (!list_locked) { | |
1821 | lck_mtx_lock(listp->mtx); | |
fe8ab488 | 1822 | list_locked = TRUE; |
6d2010ae A |
1823 | } |
1824 | ||
1825 | VERIFY_NEXT_LINK(te, le); | |
1826 | VERIFY_PREV_LINK(te, le); | |
1827 | ||
1828 | if (listp->running) { | |
fe8ab488 A |
1829 | listp->pref_mode |= mode; |
1830 | if (listp->pref_offset == 0 || | |
8a3053a0 | 1831 | offset < listp->pref_offset) { |
6d2010ae A |
1832 | listp->pref_offset = offset; |
1833 | } | |
1834 | } else { | |
8a3053a0 | 1835 | /* |
fe8ab488 A |
1836 | * The list could have got rescheduled while |
1837 | * this thread was waiting for the lock | |
8a3053a0 A |
1838 | */ |
1839 | if (listp->scheduled) { | |
1840 | int32_t diff; | |
1841 | diff = timer_diff(listp->runtime, 0, | |
1842 | tcp_now, offset); | |
1843 | if (diff <= 0) | |
1844 | goto done; | |
1845 | else | |
1846 | goto schedule; | |
6d2010ae A |
1847 | } else { |
1848 | goto schedule; | |
1849 | } | |
1850 | } | |
1851 | } | |
1852 | goto done; | |
1853 | ||
1854 | schedule: | |
fe8ab488 A |
1855 | /* |
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 | |
1858 | * reset | |
1859 | */ | |
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); | |
6d2010ae A |
1869 | } |
1870 | tcp_sched_timerlist(offset); | |
1871 | ||
1872 | done: | |
1873 | if (list_locked) | |
1874 | lck_mtx_unlock(listp->mtx); | |
1875 | ||
1876 | return; | |
1877 | } | |
1878 | ||
fe8ab488 | 1879 | static inline void |
3e170ce0 A |
1880 | tcp_set_lotimer_index(struct tcpcb *tp) |
1881 | { | |
fe8ab488 | 1882 | uint16_t i, lo_index = TCPT_NONE, mode = 0; |
6d2010ae A |
1883 | uint32_t lo_timer = 0; |
1884 | for (i = 0; i < TCPT_NTIMERS; ++i) { | |
fe8ab488 A |
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]; | |
1889 | lo_index = i; | |
1890 | } | |
6d2010ae A |
1891 | } |
1892 | } | |
1893 | tp->tentry.index = lo_index; | |
fe8ab488 A |
1894 | tp->tentry.mode = mode; |
1895 | VERIFY(tp->tentry.index == TCPT_NONE || tp->tentry.mode > 0); | |
1896 | ||
1897 | if (tp->tentry.index != TCPT_NONE) { | |
1898 | tp->tentry.runtime = tp->tentry.timer_start | |
1899 | + tp->t_timer[tp->tentry.index]; | |
8a3053a0 A |
1900 | if (tp->tentry.runtime == 0) |
1901 | tp->tentry.runtime++; | |
6d2010ae A |
1902 | } |
1903 | } | |
1904 | ||
1905 | void | |
3e170ce0 A |
1906 | tcp_check_timer_state(struct tcpcb *tp) |
1907 | { | |
6d2010ae A |
1908 | lck_mtx_assert(&tp->t_inpcb->inpcb_mtx, LCK_MTX_ASSERT_OWNED); |
1909 | ||
8a3053a0 A |
1910 | if (tp->t_inpcb->inp_flags2 & INP2_TIMEWAIT) |
1911 | return; | |
1912 | ||
6d2010ae A |
1913 | tcp_set_lotimer_index(tp); |
1914 | ||
1915 | tcp_sched_timers(tp); | |
1916 | return; | |
1917 | } | |
fe8ab488 | 1918 | |
3e170ce0 A |
1919 | static inline void |
1920 | tcp_cumulative_stat(u_int32_t cur, u_int32_t *prev, u_int32_t *dest) | |
1921 | { | |
1922 | /* handle wrap around */ | |
1923 | int32_t diff = (int32_t) (cur - *prev); | |
1924 | if (diff > 0) | |
1925 | *dest = diff; | |
1926 | else | |
1927 | *dest = 0; | |
1928 | *prev = cur; | |
1929 | return; | |
1930 | } | |
1931 | ||
fe8ab488 A |
1932 | __private_extern__ void |
1933 | tcp_report_stats(void) | |
1934 | { | |
1935 | struct nstat_sysinfo_data data; | |
1936 | struct sockaddr_in dst; | |
1937 | struct sockaddr_in6 dst6; | |
1938 | struct rtentry *rt = NULL; | |
3e170ce0 | 1939 | static struct tcp_last_report_stats prev; |
fe8ab488 A |
1940 | u_int64_t var, uptime; |
1941 | ||
1942 | #define stat data.u.tcp_stats | |
1943 | if (((uptime = net_uptime()) - tcp_last_report_time) < | |
3e170ce0 | 1944 | tcp_report_stats_interval) |
fe8ab488 A |
1945 | return; |
1946 | ||
1947 | tcp_last_report_time = uptime; | |
1948 | ||
1949 | bzero(&data, sizeof(data)); | |
1950 | data.flags = NSTAT_SYSINFO_TCP_STATS; | |
1951 | ||
1952 | bzero(&dst, sizeof(dst)); | |
1953 | dst.sin_len = sizeof(dst); | |
1954 | dst.sin_family = AF_INET; | |
1955 | ||
1956 | /* ipv4 avg rtt */ | |
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); | |
1961 | if (rt != NULL) { | |
1962 | RT_LOCK(rt); | |
1963 | if (rt_primary_default(rt, rt_key(rt)) && | |
1964 | rt->rt_stats != NULL) { | |
1965 | stat.ipv4_avgrtt = rt->rt_stats->nstat_avg_rtt; | |
1966 | } | |
1967 | RT_UNLOCK(rt); | |
1968 | rtfree(rt); | |
1969 | rt = NULL; | |
1970 | } | |
1971 | ||
1972 | /* ipv6 avg rtt */ | |
1973 | bzero(&dst6, sizeof(dst6)); | |
1974 | dst6.sin6_len = sizeof(dst6); | |
1975 | dst6.sin6_family = AF_INET6; | |
1976 | ||
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); | |
1981 | if (rt != NULL) { | |
1982 | RT_LOCK(rt); | |
1983 | if (rt_primary_default(rt, rt_key(rt)) && | |
1984 | rt->rt_stats != NULL) { | |
1985 | stat.ipv6_avgrtt = rt->rt_stats->nstat_avg_rtt; | |
1986 | } | |
1987 | RT_UNLOCK(rt); | |
1988 | rtfree(rt); | |
1989 | rt = NULL; | |
1990 | } | |
1991 | ||
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; | |
1996 | } | |
1997 | ||
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; | |
2002 | } | |
2003 | ||
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; | |
2010 | } | |
2011 | ||
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; | |
2017 | } | |
2018 | ||
3e170ce0 A |
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); | |
4bd07ac2 A |
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); | |
3e170ce0 A |
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); | |
2087 | ||
fe8ab488 A |
2088 | nstat_sysinfo_send_data(&data); |
2089 | ||
2090 | #undef stat | |
2091 | } | |
3e170ce0 A |
2092 | |
2093 | void | |
2094 | tcp_interface_send_probe(u_int16_t probe_if_index) | |
2095 | { | |
2096 | int32_t offset = 0; | |
2097 | struct tcptimerlist *listp = &tcp_timer_list; | |
2098 | ||
2099 | /* Make sure TCP clock is up to date */ | |
2100 | calculate_tcp_clock(); | |
2101 | ||
2102 | lck_mtx_lock(listp->mtx); | |
2103 | if (listp->probe_if_index > 0) { | |
2104 | tcpstat.tcps_probe_if_conflict++; | |
2105 | goto done; | |
2106 | } | |
2107 | ||
2108 | listp->probe_if_index = probe_if_index; | |
2109 | if (listp->running) | |
2110 | goto done; | |
2111 | ||
2112 | /* | |
2113 | * Reschedule the timerlist to run within the next 10ms, which is | |
2114 | * the fastest that we can do. | |
2115 | */ | |
2116 | offset = TCP_TIMER_10MS_QUANTUM; | |
2117 | if (listp->scheduled) { | |
2118 | int32_t diff; | |
2119 | diff = timer_diff(listp->runtime, 0, tcp_now, offset); | |
2120 | if (diff <= 0) { | |
2121 | /* The timer will fire sooner than what's needed */ | |
2122 | goto done; | |
2123 | } | |
2124 | } | |
2125 | listp->mode = TCP_TIMERLIST_10MS_MODE; | |
2126 | listp->idleruns = 0; | |
2127 | ||
2128 | tcp_sched_timerlist(offset); | |
2129 | ||
2130 | done: | |
2131 | lck_mtx_unlock(listp->mtx); | |
2132 | return; | |
2133 | } | |
2134 | ||
2135 | /* | |
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 | |
2141 | */ | |
2142 | static void | |
2143 | tcp_enable_read_probe(struct tcpcb *tp, struct ifnet *ifp) | |
2144 | { | |
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; | |
2158 | } else { | |
2159 | int32_t diff = 0; | |
2160 | ||
2161 | /* Reset runtime to be in next 10ms */ | |
2162 | diff = timer_diff(tp->tentry.runtime, 0, | |
2163 | tcp_now, TCP_TIMER_10MS_QUANTUM); | |
2164 | if (diff > 0) { | |
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++; | |
2170 | } | |
2171 | } | |
2172 | } | |
2173 | } | |
2174 | ||
2175 | /* | |
2176 | * Disable read probe and reset the keep alive timer | |
2177 | */ | |
2178 | static void | |
2179 | tcp_disable_read_probe(struct tcpcb *tp) | |
2180 | { | |
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); | |
2185 | } | |
2186 | } | |
2187 | ||
2188 | /* | |
2189 | * Reschedule the tcp timerlist in the next 10ms to re-enable read/write | |
2190 | * probes on connections going over a particular interface. | |
2191 | */ | |
2192 | void | |
2193 | tcp_probe_connectivity(struct ifnet *ifp, u_int32_t enable) | |
2194 | { | |
2195 | int32_t offset; | |
2196 | struct tcptimerlist *listp = &tcp_timer_list; | |
2197 | struct inpcbinfo *pcbinfo = &tcbinfo; | |
2198 | struct inpcb *inp, *nxt; | |
2199 | ||
2200 | if (ifp == NULL) | |
2201 | return; | |
2202 | ||
2203 | /* update clock */ | |
2204 | calculate_tcp_clock(); | |
2205 | ||
2206 | /* | |
2207 | * Enable keep alive timer on all connections that are | |
2208 | * active/established on this interface. | |
2209 | */ | |
2210 | lck_rw_lock_shared(pcbinfo->ipi_lock); | |
2211 | ||
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) == | |
2215 | WNT_STOPUSING) | |
2216 | continue; | |
2217 | ||
2218 | /* Acquire lock to look at the state of the connection */ | |
2219 | tcp_lock(inp->inp_socket, 1, 0); | |
2220 | ||
2221 | /* Release the want count */ | |
490019cf A |
2222 | if (inp->inp_ppcb == NULL || |
2223 | (in_pcb_checkstate(inp, WNT_RELEASE, 1) == WNT_STOPUSING)) { | |
3e170ce0 A |
2224 | tcp_unlock(inp->inp_socket, 1, 0); |
2225 | continue; | |
2226 | } | |
3e170ce0 A |
2227 | tp = intotcpcb(inp); |
2228 | if (enable) | |
2229 | tcp_enable_read_probe(tp, ifp); | |
2230 | else | |
2231 | tcp_disable_read_probe(tp); | |
2232 | ||
2233 | tcp_unlock(inp->inp_socket, 1, 0); | |
2234 | } | |
2235 | lck_rw_done(pcbinfo->ipi_lock); | |
2236 | ||
2237 | lck_mtx_lock(listp->mtx); | |
2238 | if (listp->running) { | |
2239 | listp->pref_mode |= TCP_TIMERLIST_10MS_MODE; | |
2240 | goto done; | |
2241 | } | |
2242 | ||
2243 | /* Reschedule within the next 10ms */ | |
2244 | offset = TCP_TIMER_10MS_QUANTUM; | |
2245 | if (listp->scheduled) { | |
2246 | int32_t diff; | |
2247 | diff = timer_diff(listp->runtime, 0, tcp_now, offset); | |
2248 | if (diff <= 0) { | |
2249 | /* The timer will fire sooner than what's needed */ | |
2250 | goto done; | |
2251 | } | |
2252 | } | |
2253 | listp->mode = TCP_TIMERLIST_10MS_MODE; | |
2254 | listp->idleruns = 0; | |
2255 | ||
2256 | tcp_sched_timerlist(offset); | |
2257 | done: | |
2258 | lck_mtx_unlock(listp->mtx); | |
2259 | return; | |
2260 | } | |
2261 | ||
2262 | void | |
2263 | tcp_itimer(struct inpcbinfo *ipi) | |
2264 | { | |
2265 | struct inpcb *inp, *nxt; | |
2266 | ||
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); | |
2271 | return; | |
2272 | } | |
2273 | /* Upgrade failed, lost lock now take it again exclusive */ | |
2274 | lck_rw_lock_exclusive(ipi->ipi_lock); | |
2275 | } | |
2276 | tcp_itimer_done = TRUE; | |
2277 | ||
2278 | LIST_FOREACH_SAFE(inp, &tcb, inp_list, nxt) { | |
2279 | struct socket *so; | |
2280 | ||
2281 | if (in_pcb_checkstate(inp, WNT_ACQUIRE, 0) == WNT_STOPUSING) | |
2282 | continue; | |
2283 | so = inp->inp_socket; | |
2284 | tcp_lock(so, 1, 0); | |
2285 | if (in_pcb_checkstate(inp, WNT_RELEASE, 1) == WNT_STOPUSING) { | |
2286 | tcp_unlock(so, 1, 0); | |
2287 | continue; | |
2288 | } | |
2289 | so_check_extended_bk_idle_time(so); | |
2290 | tcp_unlock(so, 1, 0); | |
2291 | } | |
2292 | ||
2293 | lck_rw_done(ipi->ipi_lock); | |
2294 | } | |
2295 |