xref: /xnu-10002.81.5/bsd/netinet/mptcp_timer.c (revision 5e3eaea39dcf651e66cb99ba7d70e32cc4a99587)
1*5e3eaea3SApple OSS Distributions /*
2*5e3eaea3SApple OSS Distributions  * Copyright (c) 2012-2017 Apple Inc. All rights reserved.
3*5e3eaea3SApple OSS Distributions  *
4*5e3eaea3SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*5e3eaea3SApple OSS Distributions  *
6*5e3eaea3SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*5e3eaea3SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*5e3eaea3SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*5e3eaea3SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*5e3eaea3SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*5e3eaea3SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*5e3eaea3SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*5e3eaea3SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*5e3eaea3SApple OSS Distributions  *
15*5e3eaea3SApple OSS Distributions  * Please obtain a copy of the License at
16*5e3eaea3SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*5e3eaea3SApple OSS Distributions  *
18*5e3eaea3SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*5e3eaea3SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*5e3eaea3SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*5e3eaea3SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*5e3eaea3SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*5e3eaea3SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*5e3eaea3SApple OSS Distributions  * limitations under the License.
25*5e3eaea3SApple OSS Distributions  *
26*5e3eaea3SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*5e3eaea3SApple OSS Distributions  */
28*5e3eaea3SApple OSS Distributions 
29*5e3eaea3SApple OSS Distributions #include <sys/param.h>
30*5e3eaea3SApple OSS Distributions #include <sys/systm.h>
31*5e3eaea3SApple OSS Distributions #include <sys/kernel.h>
32*5e3eaea3SApple OSS Distributions #include <sys/mcache.h>
33*5e3eaea3SApple OSS Distributions #include <sys/socket.h>
34*5e3eaea3SApple OSS Distributions #include <sys/socketvar.h>
35*5e3eaea3SApple OSS Distributions #include <sys/syslog.h>
36*5e3eaea3SApple OSS Distributions #include <sys/protosw.h>
37*5e3eaea3SApple OSS Distributions #include <sys/sysctl.h>
38*5e3eaea3SApple OSS Distributions 
39*5e3eaea3SApple OSS Distributions #include <mach/sdt.h>
40*5e3eaea3SApple OSS Distributions 
41*5e3eaea3SApple OSS Distributions #include <netinet/mp_pcb.h>
42*5e3eaea3SApple OSS Distributions #include <netinet/mptcp_var.h>
43*5e3eaea3SApple OSS Distributions #include <netinet/mptcp_timer.h>
44*5e3eaea3SApple OSS Distributions #include <netinet/mptcp_seq.h>
45*5e3eaea3SApple OSS Distributions 
46*5e3eaea3SApple OSS Distributions #include <kern/locks.h>
47*5e3eaea3SApple OSS Distributions 
48*5e3eaea3SApple OSS Distributions /*
49*5e3eaea3SApple OSS Distributions  * MPTCP Retransmission Timer comes into play only when subflow level
50*5e3eaea3SApple OSS Distributions  * data is acked, but Data ACK is not received. Time is in seconds.
51*5e3eaea3SApple OSS Distributions  */
52*5e3eaea3SApple OSS Distributions static u_int32_t mptcp_rto = 3;
53*5e3eaea3SApple OSS Distributions SYSCTL_INT(_net_inet_mptcp, OID_AUTO, rto, CTLFLAG_RW | CTLFLAG_LOCKED,
54*5e3eaea3SApple OSS Distributions     &mptcp_rto, 0, "MPTCP Retransmission Timeout");
55*5e3eaea3SApple OSS Distributions 
56*5e3eaea3SApple OSS Distributions static int mptcp_nrtos = 3;
57*5e3eaea3SApple OSS Distributions SYSCTL_INT(_net_inet_mptcp, OID_AUTO, nrto, CTLFLAG_RW | CTLFLAG_LOCKED,
58*5e3eaea3SApple OSS Distributions     &mptcp_rto, 0, "MPTCP Retransmissions");
59*5e3eaea3SApple OSS Distributions 
60*5e3eaea3SApple OSS Distributions /*
61*5e3eaea3SApple OSS Distributions  * MPTCP connections timewait interval in seconds.
62*5e3eaea3SApple OSS Distributions  */
63*5e3eaea3SApple OSS Distributions static u_int32_t mptcp_tw = 60;
64*5e3eaea3SApple OSS Distributions SYSCTL_INT(_net_inet_mptcp, OID_AUTO, tw, CTLFLAG_RW | CTLFLAG_LOCKED,
65*5e3eaea3SApple OSS Distributions     &mptcp_tw, 0, "MPTCP Timewait Period");
66*5e3eaea3SApple OSS Distributions 
67*5e3eaea3SApple OSS Distributions #define TIMEVAL_TO_HZ(_tv_)     ((_tv_).tv_sec * hz + (_tv_).tv_usec / hz)
68*5e3eaea3SApple OSS Distributions 
69*5e3eaea3SApple OSS Distributions static int mptcp_cancel_urgency_timer(struct mptses *mpte);
70*5e3eaea3SApple OSS Distributions 
71*5e3eaea3SApple OSS Distributions static int
mptcp_timer_demux(struct mptses * mpte,uint64_t now_msecs)72*5e3eaea3SApple OSS Distributions mptcp_timer_demux(struct mptses *mpte, uint64_t now_msecs)
73*5e3eaea3SApple OSS Distributions {
74*5e3eaea3SApple OSS Distributions 	struct mptcb *mp_tp = NULL;
75*5e3eaea3SApple OSS Distributions 	mp_tp = mpte->mpte_mptcb;
76*5e3eaea3SApple OSS Distributions 	int resched_timer = 0;
77*5e3eaea3SApple OSS Distributions 
78*5e3eaea3SApple OSS Distributions 	DTRACE_MPTCP2(timer, struct mptses *, mpte, struct mptcb *, mp_tp);
79*5e3eaea3SApple OSS Distributions 
80*5e3eaea3SApple OSS Distributions 	switch (mp_tp->mpt_timer_vals) {
81*5e3eaea3SApple OSS Distributions 	case MPTT_REXMT:
82*5e3eaea3SApple OSS Distributions 		if (mp_tp->mpt_rxtstart == 0) {
83*5e3eaea3SApple OSS Distributions 			break;
84*5e3eaea3SApple OSS Distributions 		}
85*5e3eaea3SApple OSS Distributions 		if ((now_msecs - mp_tp->mpt_rxtstart) > (mptcp_rto * hz)) {
86*5e3eaea3SApple OSS Distributions 			if (MPTCP_SEQ_GT(mp_tp->mpt_snduna, mp_tp->mpt_rtseq)) {
87*5e3eaea3SApple OSS Distributions 				mp_tp->mpt_timer_vals = 0;
88*5e3eaea3SApple OSS Distributions 				mp_tp->mpt_rtseq = 0;
89*5e3eaea3SApple OSS Distributions 				break;
90*5e3eaea3SApple OSS Distributions 			}
91*5e3eaea3SApple OSS Distributions 			mp_tp->mpt_rxtshift++;
92*5e3eaea3SApple OSS Distributions 			if (mp_tp->mpt_rxtshift > mptcp_nrtos) {
93*5e3eaea3SApple OSS Distributions 				mp_tp->mpt_softerror = ETIMEDOUT;
94*5e3eaea3SApple OSS Distributions 				DTRACE_MPTCP1(error, struct mptcb *, mp_tp);
95*5e3eaea3SApple OSS Distributions 			} else {
96*5e3eaea3SApple OSS Distributions 				mp_tp->mpt_sndnxt = mp_tp->mpt_rtseq;
97*5e3eaea3SApple OSS Distributions 				os_log_info(mptcp_log_handle,
98*5e3eaea3SApple OSS Distributions 				    "%s: REXMT %d sndnxt %u\n",
99*5e3eaea3SApple OSS Distributions 				    __func__, mp_tp->mpt_rxtshift,
100*5e3eaea3SApple OSS Distributions 				    (uint32_t)mp_tp->mpt_sndnxt);
101*5e3eaea3SApple OSS Distributions 				mptcp_output(mpte);
102*5e3eaea3SApple OSS Distributions 			}
103*5e3eaea3SApple OSS Distributions 		} else {
104*5e3eaea3SApple OSS Distributions 			resched_timer = 1;
105*5e3eaea3SApple OSS Distributions 		}
106*5e3eaea3SApple OSS Distributions 		break;
107*5e3eaea3SApple OSS Distributions 	case MPTT_TW:
108*5e3eaea3SApple OSS Distributions 		/* Allows for break before make XXX */
109*5e3eaea3SApple OSS Distributions 		if (mp_tp->mpt_timewait == 0) {
110*5e3eaea3SApple OSS Distributions 			VERIFY(0);
111*5e3eaea3SApple OSS Distributions 		}
112*5e3eaea3SApple OSS Distributions 		if ((now_msecs - mp_tp->mpt_timewait) >
113*5e3eaea3SApple OSS Distributions 		    (mptcp_tw * hz)) {
114*5e3eaea3SApple OSS Distributions 			mp_tp->mpt_softerror = ETIMEDOUT;
115*5e3eaea3SApple OSS Distributions 			DTRACE_MPTCP1(error, struct mptcb *, mp_tp);
116*5e3eaea3SApple OSS Distributions 		} else {
117*5e3eaea3SApple OSS Distributions 			resched_timer = 1;
118*5e3eaea3SApple OSS Distributions 		}
119*5e3eaea3SApple OSS Distributions 		break;
120*5e3eaea3SApple OSS Distributions 	case MPTT_FASTCLOSE:
121*5e3eaea3SApple OSS Distributions 		/* TODO XXX */
122*5e3eaea3SApple OSS Distributions 		break;
123*5e3eaea3SApple OSS Distributions 	default:
124*5e3eaea3SApple OSS Distributions 		break;
125*5e3eaea3SApple OSS Distributions 	}
126*5e3eaea3SApple OSS Distributions 
127*5e3eaea3SApple OSS Distributions 	return resched_timer;
128*5e3eaea3SApple OSS Distributions }
129*5e3eaea3SApple OSS Distributions 
130*5e3eaea3SApple OSS Distributions uint32_t
mptcp_timer(struct mppcbinfo * mppi)131*5e3eaea3SApple OSS Distributions mptcp_timer(struct mppcbinfo *mppi)
132*5e3eaea3SApple OSS Distributions {
133*5e3eaea3SApple OSS Distributions 	struct mppcb *mpp, *tmpp;
134*5e3eaea3SApple OSS Distributions 	struct timeval now;
135*5e3eaea3SApple OSS Distributions 	uint32_t resched_timer = 0;
136*5e3eaea3SApple OSS Distributions 	uint64_t now_msecs;
137*5e3eaea3SApple OSS Distributions 
138*5e3eaea3SApple OSS Distributions 	LCK_MTX_ASSERT(&mppi->mppi_lock, LCK_MTX_ASSERT_OWNED);
139*5e3eaea3SApple OSS Distributions 
140*5e3eaea3SApple OSS Distributions 	microuptime(&now);
141*5e3eaea3SApple OSS Distributions 	now_msecs = TIMEVAL_TO_HZ(now);
142*5e3eaea3SApple OSS Distributions 	TAILQ_FOREACH_SAFE(mpp, &mppi->mppi_pcbs, mpp_entry, tmpp) {
143*5e3eaea3SApple OSS Distributions 		struct socket *mp_so;
144*5e3eaea3SApple OSS Distributions 		struct mptses *mpte;
145*5e3eaea3SApple OSS Distributions 
146*5e3eaea3SApple OSS Distributions 		mp_so = mpp->mpp_socket;
147*5e3eaea3SApple OSS Distributions 		mpte = mptompte(mpp);
148*5e3eaea3SApple OSS Distributions 		socket_lock(mp_so, 1);
149*5e3eaea3SApple OSS Distributions 
150*5e3eaea3SApple OSS Distributions 		VERIFY(mpp->mpp_flags & MPP_ATTACHED);
151*5e3eaea3SApple OSS Distributions 
152*5e3eaea3SApple OSS Distributions 		if (mptcp_timer_demux(mpte, now_msecs)) {
153*5e3eaea3SApple OSS Distributions 			resched_timer = 1;
154*5e3eaea3SApple OSS Distributions 		}
155*5e3eaea3SApple OSS Distributions 		socket_unlock(mp_so, 1);
156*5e3eaea3SApple OSS Distributions 	}
157*5e3eaea3SApple OSS Distributions 
158*5e3eaea3SApple OSS Distributions 	return resched_timer;
159*5e3eaea3SApple OSS Distributions }
160*5e3eaea3SApple OSS Distributions 
161*5e3eaea3SApple OSS Distributions void
mptcp_start_timer(struct mptses * mpte,int timer_type)162*5e3eaea3SApple OSS Distributions mptcp_start_timer(struct mptses *mpte, int timer_type)
163*5e3eaea3SApple OSS Distributions {
164*5e3eaea3SApple OSS Distributions 	struct timeval now;
165*5e3eaea3SApple OSS Distributions 	struct mptcb *mp_tp = mpte->mpte_mptcb;
166*5e3eaea3SApple OSS Distributions 
167*5e3eaea3SApple OSS Distributions 	microuptime(&now);
168*5e3eaea3SApple OSS Distributions 
169*5e3eaea3SApple OSS Distributions 	DTRACE_MPTCP2(start__timer, struct mptcb *, mp_tp, int, timer_type);
170*5e3eaea3SApple OSS Distributions 
171*5e3eaea3SApple OSS Distributions 	socket_lock_assert_owned(mptetoso(mpte));
172*5e3eaea3SApple OSS Distributions 
173*5e3eaea3SApple OSS Distributions 	switch (timer_type) {
174*5e3eaea3SApple OSS Distributions 	case MPTT_REXMT:
175*5e3eaea3SApple OSS Distributions 		mp_tp->mpt_timer_vals |= MPTT_REXMT;
176*5e3eaea3SApple OSS Distributions 		mp_tp->mpt_rxtstart = TIMEVAL_TO_HZ(now);
177*5e3eaea3SApple OSS Distributions 		mp_tp->mpt_rxtshift = 0;
178*5e3eaea3SApple OSS Distributions 		mp_tp->mpt_rtseq = mp_tp->mpt_sndnxt;
179*5e3eaea3SApple OSS Distributions 		break;
180*5e3eaea3SApple OSS Distributions 	case MPTT_TW:
181*5e3eaea3SApple OSS Distributions 		/* XXX: Not implemented yet */
182*5e3eaea3SApple OSS Distributions 		mp_tp->mpt_timer_vals |= MPTT_TW;
183*5e3eaea3SApple OSS Distributions 		mp_tp->mpt_timewait = TIMEVAL_TO_HZ(now);
184*5e3eaea3SApple OSS Distributions 		break;
185*5e3eaea3SApple OSS Distributions 	case MPTT_FASTCLOSE:
186*5e3eaea3SApple OSS Distributions 		/* NO-OP */
187*5e3eaea3SApple OSS Distributions 		break;
188*5e3eaea3SApple OSS Distributions 	default:
189*5e3eaea3SApple OSS Distributions 		VERIFY(0);
190*5e3eaea3SApple OSS Distributions 		/* NOTREACHED */
191*5e3eaea3SApple OSS Distributions 	}
192*5e3eaea3SApple OSS Distributions 	mptcp_timer_sched();
193*5e3eaea3SApple OSS Distributions }
194*5e3eaea3SApple OSS Distributions 
195*5e3eaea3SApple OSS Distributions void
mptcp_cancel_timer(struct mptcb * mp_tp,int timer_type)196*5e3eaea3SApple OSS Distributions mptcp_cancel_timer(struct mptcb *mp_tp, int timer_type)
197*5e3eaea3SApple OSS Distributions {
198*5e3eaea3SApple OSS Distributions 	socket_lock_assert_owned(mptetoso(mp_tp->mpt_mpte));
199*5e3eaea3SApple OSS Distributions 
200*5e3eaea3SApple OSS Distributions 	switch (timer_type) {
201*5e3eaea3SApple OSS Distributions 	case MPTT_REXMT:
202*5e3eaea3SApple OSS Distributions 		mp_tp->mpt_rxtstart = 0;
203*5e3eaea3SApple OSS Distributions 		mp_tp->mpt_rxtshift = 0;
204*5e3eaea3SApple OSS Distributions 		mp_tp->mpt_timer_vals = 0;
205*5e3eaea3SApple OSS Distributions 		break;
206*5e3eaea3SApple OSS Distributions 	case MPTT_TW:
207*5e3eaea3SApple OSS Distributions 		/* NO-OP */
208*5e3eaea3SApple OSS Distributions 		break;
209*5e3eaea3SApple OSS Distributions 	case MPTT_FASTCLOSE:
210*5e3eaea3SApple OSS Distributions 		/* NO-OP */
211*5e3eaea3SApple OSS Distributions 		break;
212*5e3eaea3SApple OSS Distributions 	default:
213*5e3eaea3SApple OSS Distributions 		break;
214*5e3eaea3SApple OSS Distributions 	}
215*5e3eaea3SApple OSS Distributions }
216*5e3eaea3SApple OSS Distributions 
217*5e3eaea3SApple OSS Distributions void
mptcp_cancel_all_timers(struct mptcb * mp_tp)218*5e3eaea3SApple OSS Distributions mptcp_cancel_all_timers(struct mptcb *mp_tp)
219*5e3eaea3SApple OSS Distributions {
220*5e3eaea3SApple OSS Distributions 	struct mptses *mpte = mp_tp->mpt_mpte;
221*5e3eaea3SApple OSS Distributions 
222*5e3eaea3SApple OSS Distributions 	mptcp_cancel_urgency_timer(mpte);
223*5e3eaea3SApple OSS Distributions 
224*5e3eaea3SApple OSS Distributions 	mptcp_cancel_timer(mp_tp, MPTT_REXMT);
225*5e3eaea3SApple OSS Distributions 	mptcp_cancel_timer(mp_tp, MPTT_TW);
226*5e3eaea3SApple OSS Distributions 	mptcp_cancel_timer(mp_tp, MPTT_FASTCLOSE);
227*5e3eaea3SApple OSS Distributions }
228*5e3eaea3SApple OSS Distributions 
229*5e3eaea3SApple OSS Distributions static void
mptcp_urgency_timer(void * param0,__unused void * param1)230*5e3eaea3SApple OSS Distributions mptcp_urgency_timer(void *param0, __unused void *param1)
231*5e3eaea3SApple OSS Distributions {
232*5e3eaea3SApple OSS Distributions 	struct mptses *mpte = (struct mptses *)param0;
233*5e3eaea3SApple OSS Distributions 	struct socket *mp_so = mptetoso(mpte);
234*5e3eaea3SApple OSS Distributions 	uint64_t time_now;
235*5e3eaea3SApple OSS Distributions 
236*5e3eaea3SApple OSS Distributions 	socket_lock(mp_so, 1);
237*5e3eaea3SApple OSS Distributions 
238*5e3eaea3SApple OSS Distributions 	time_now = mach_continuous_time();
239*5e3eaea3SApple OSS Distributions 	VERIFY(mp_so->so_usecount >= 0);
240*5e3eaea3SApple OSS Distributions 
241*5e3eaea3SApple OSS Distributions 	os_log(mptcp_log_handle, "%s - %lx: timer at %llu now %llu usecount %u\n",
242*5e3eaea3SApple OSS Distributions 	    __func__, (unsigned long)VM_KERNEL_ADDRPERM(mpte), mpte->mpte_time_target, time_now, mp_so->so_usecount);
243*5e3eaea3SApple OSS Distributions 
244*5e3eaea3SApple OSS Distributions 	mptcp_check_subflows_and_add(mpte);
245*5e3eaea3SApple OSS Distributions 
246*5e3eaea3SApple OSS Distributions 	mp_so->so_usecount--;
247*5e3eaea3SApple OSS Distributions 
248*5e3eaea3SApple OSS Distributions 	socket_unlock(mp_so, 1);
249*5e3eaea3SApple OSS Distributions }
250*5e3eaea3SApple OSS Distributions 
251*5e3eaea3SApple OSS Distributions static void
mptcp_urgency_stop(void * param0,__unused void * param1)252*5e3eaea3SApple OSS Distributions mptcp_urgency_stop(void *param0, __unused void *param1)
253*5e3eaea3SApple OSS Distributions {
254*5e3eaea3SApple OSS Distributions 	struct mptses *mpte = (struct mptses *)param0;
255*5e3eaea3SApple OSS Distributions 	struct socket *mp_so = mptetoso(mpte);
256*5e3eaea3SApple OSS Distributions 
257*5e3eaea3SApple OSS Distributions 	socket_lock(mp_so, 1);
258*5e3eaea3SApple OSS Distributions 
259*5e3eaea3SApple OSS Distributions 	VERIFY(mp_so->so_usecount >= 1);
260*5e3eaea3SApple OSS Distributions 
261*5e3eaea3SApple OSS Distributions 	os_log(mptcp_log_handle, "%s - %lx: usecount %u\n",
262*5e3eaea3SApple OSS Distributions 	    __func__, (unsigned long)VM_KERNEL_ADDRPERM(mpte), mp_so->so_usecount);
263*5e3eaea3SApple OSS Distributions 
264*5e3eaea3SApple OSS Distributions 	mptcp_check_subflows_and_remove(mpte);
265*5e3eaea3SApple OSS Distributions 
266*5e3eaea3SApple OSS Distributions 	mp_so->so_usecount--;
267*5e3eaea3SApple OSS Distributions 
268*5e3eaea3SApple OSS Distributions 	socket_unlock(mp_so, 1);
269*5e3eaea3SApple OSS Distributions }
270*5e3eaea3SApple OSS Distributions 
271*5e3eaea3SApple OSS Distributions void
mptcp_init_urgency_timer(struct mptses * mpte)272*5e3eaea3SApple OSS Distributions mptcp_init_urgency_timer(struct mptses *mpte)
273*5e3eaea3SApple OSS Distributions {
274*5e3eaea3SApple OSS Distributions 	/* thread_call_allocate never fails */
275*5e3eaea3SApple OSS Distributions 	mpte->mpte_time_thread = thread_call_allocate(mptcp_urgency_timer, mpte);
276*5e3eaea3SApple OSS Distributions 	mpte->mpte_stop_urgency = thread_call_allocate(mptcp_urgency_stop, mpte);
277*5e3eaea3SApple OSS Distributions }
278*5e3eaea3SApple OSS Distributions 
279*5e3eaea3SApple OSS Distributions void
mptcp_set_urgency_timer(struct mptses * mpte)280*5e3eaea3SApple OSS Distributions mptcp_set_urgency_timer(struct mptses *mpte)
281*5e3eaea3SApple OSS Distributions {
282*5e3eaea3SApple OSS Distributions 	struct socket *mp_so = mptetoso(mpte);
283*5e3eaea3SApple OSS Distributions 	uint64_t time_now = 0;
284*5e3eaea3SApple OSS Distributions 	boolean_t ret = FALSE;
285*5e3eaea3SApple OSS Distributions 
286*5e3eaea3SApple OSS Distributions 	socket_lock_assert_owned(mp_so);
287*5e3eaea3SApple OSS Distributions 
288*5e3eaea3SApple OSS Distributions 	VERIFY(mp_so->so_usecount >= 1);
289*5e3eaea3SApple OSS Distributions 
290*5e3eaea3SApple OSS Distributions 	if (mpte->mpte_time_target == 0) {
291*5e3eaea3SApple OSS Distributions 		/* Close subflows right now */
292*5e3eaea3SApple OSS Distributions 
293*5e3eaea3SApple OSS Distributions 		ret = thread_call_enter(mpte->mpte_stop_urgency);
294*5e3eaea3SApple OSS Distributions 
295*5e3eaea3SApple OSS Distributions 		if (!ret) {
296*5e3eaea3SApple OSS Distributions 			mp_so->so_usecount++;
297*5e3eaea3SApple OSS Distributions 		}
298*5e3eaea3SApple OSS Distributions 
299*5e3eaea3SApple OSS Distributions 		goto exit_log;
300*5e3eaea3SApple OSS Distributions 	}
301*5e3eaea3SApple OSS Distributions 
302*5e3eaea3SApple OSS Distributions 	time_now = mach_continuous_time();
303*5e3eaea3SApple OSS Distributions 
304*5e3eaea3SApple OSS Distributions 	if ((int64_t)(mpte->mpte_time_target - time_now) > 0) {
305*5e3eaea3SApple OSS Distributions 		ret = thread_call_enter(mpte->mpte_stop_urgency);
306*5e3eaea3SApple OSS Distributions 
307*5e3eaea3SApple OSS Distributions 		if (!ret) {
308*5e3eaea3SApple OSS Distributions 			mp_so->so_usecount++;
309*5e3eaea3SApple OSS Distributions 		}
310*5e3eaea3SApple OSS Distributions 
311*5e3eaea3SApple OSS Distributions 		ret = thread_call_enter_delayed_with_leeway(mpte->mpte_time_thread, NULL,
312*5e3eaea3SApple OSS Distributions 		    mpte->mpte_time_target, 0, THREAD_CALL_CONTINUOUS);
313*5e3eaea3SApple OSS Distributions 
314*5e3eaea3SApple OSS Distributions 		if (!ret) {
315*5e3eaea3SApple OSS Distributions 			mp_so->so_usecount++;
316*5e3eaea3SApple OSS Distributions 		}
317*5e3eaea3SApple OSS Distributions 	} else if ((int64_t)(mpte->mpte_time_target - time_now) <= 0) {
318*5e3eaea3SApple OSS Distributions 		/* Already passed the deadline, trigger subflows now */
319*5e3eaea3SApple OSS Distributions 		ret = thread_call_enter(mpte->mpte_time_thread);
320*5e3eaea3SApple OSS Distributions 
321*5e3eaea3SApple OSS Distributions 		if (!ret) {
322*5e3eaea3SApple OSS Distributions 			mp_so->so_usecount++;
323*5e3eaea3SApple OSS Distributions 		}
324*5e3eaea3SApple OSS Distributions 	}
325*5e3eaea3SApple OSS Distributions 
326*5e3eaea3SApple OSS Distributions exit_log:
327*5e3eaea3SApple OSS Distributions 	os_log(mptcp_log_handle, "%s - %lx: timer at %llu now %llu usecount %u ret %u\n",
328*5e3eaea3SApple OSS Distributions 	    __func__, (unsigned long)VM_KERNEL_ADDRPERM(mpte), mpte->mpte_time_target, time_now,
329*5e3eaea3SApple OSS Distributions 	    mp_so->so_usecount, ret);
330*5e3eaea3SApple OSS Distributions }
331*5e3eaea3SApple OSS Distributions 
332*5e3eaea3SApple OSS Distributions static int
mptcp_cancel_urgency_timer(struct mptses * mpte)333*5e3eaea3SApple OSS Distributions mptcp_cancel_urgency_timer(struct mptses *mpte)
334*5e3eaea3SApple OSS Distributions {
335*5e3eaea3SApple OSS Distributions 	struct socket *mp_so = mptetoso(mpte);
336*5e3eaea3SApple OSS Distributions 	boolean_t ret;
337*5e3eaea3SApple OSS Distributions 
338*5e3eaea3SApple OSS Distributions 	ret = thread_call_cancel(mpte->mpte_time_thread);
339*5e3eaea3SApple OSS Distributions 
340*5e3eaea3SApple OSS Distributions 	os_log(mptcp_log_handle, "%s - %lx: Canceled timer thread usecount %u ret %u\n",
341*5e3eaea3SApple OSS Distributions 	    __func__, (unsigned long)VM_KERNEL_ADDRPERM(mpte), mp_so->so_usecount, ret);
342*5e3eaea3SApple OSS Distributions 
343*5e3eaea3SApple OSS Distributions 	mptcp_check_subflows_and_remove(mpte);
344*5e3eaea3SApple OSS Distributions 
345*5e3eaea3SApple OSS Distributions 	if (ret) {
346*5e3eaea3SApple OSS Distributions 		VERIFY(mp_so->so_usecount >= 1);
347*5e3eaea3SApple OSS Distributions 		mp_so->so_usecount--;
348*5e3eaea3SApple OSS Distributions 	}
349*5e3eaea3SApple OSS Distributions 
350*5e3eaea3SApple OSS Distributions 	ret = thread_call_cancel(mpte->mpte_stop_urgency);
351*5e3eaea3SApple OSS Distributions 	if (ret) {
352*5e3eaea3SApple OSS Distributions 		VERIFY(mp_so->so_usecount >= 1);
353*5e3eaea3SApple OSS Distributions 		mp_so->so_usecount--;
354*5e3eaea3SApple OSS Distributions 	}
355*5e3eaea3SApple OSS Distributions 
356*5e3eaea3SApple OSS Distributions 	return 0;
357*5e3eaea3SApple OSS Distributions }
358