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