]> git.saurik.com Git - apple/xnu.git/blame - bsd/netinet/mptcp_timer.c
xnu-7195.81.3.tar.gz
[apple/xnu.git] / bsd / netinet / mptcp_timer.c
CommitLineData
39236c6e 1/*
5ba3f43e 2 * Copyright (c) 2012-2017 Apple Inc. All rights reserved.
39236c6e
A
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 */
52static u_int32_t mptcp_rto = 3;
53SYSCTL_INT(_net_inet_mptcp, OID_AUTO, rto, CTLFLAG_RW | CTLFLAG_LOCKED,
0a7de745 54 &mptcp_rto, 0, "MPTCP Retransmission Timeout");
39236c6e
A
55
56static int mptcp_nrtos = 3;
57SYSCTL_INT(_net_inet_mptcp, OID_AUTO, nrto, CTLFLAG_RW | CTLFLAG_LOCKED,
0a7de745 58 &mptcp_rto, 0, "MPTCP Retransmissions");
39236c6e
A
59
60/*
61 * MPTCP connections timewait interval in seconds.
62 */
63static u_int32_t mptcp_tw = 60;
64SYSCTL_INT(_net_inet_mptcp, OID_AUTO, tw, CTLFLAG_RW | CTLFLAG_LOCKED,
0a7de745 65 &mptcp_tw, 0, "MPTCP Timewait Period");
39236c6e 66
0a7de745 67#define TIMEVAL_TO_HZ(_tv_) ((_tv_).tv_sec * hz + (_tv_).tv_usec / hz)
39236c6e 68
cb323159
A
69static int mptcp_cancel_urgency_timer(struct mptses *mpte);
70
39236c6e 71static int
f427ee49 72mptcp_timer_demux(struct mptses *mpte, uint64_t now_msecs)
39236c6e
A
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);
39236c6e 79
39236c6e
A
80 switch (mp_tp->mpt_timer_vals) {
81 case MPTT_REXMT:
0a7de745 82 if (mp_tp->mpt_rxtstart == 0) {
39236c6e 83 break;
0a7de745 84 }
f427ee49 85 if ((now_msecs - mp_tp->mpt_rxtstart) > (mptcp_rto * hz)) {
a39ff7e2 86 if (MPTCP_SEQ_GT(mp_tp->mpt_snduna, mp_tp->mpt_rtseq)) {
39236c6e
A
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;
a39ff7e2 97 os_log_info(mptcp_log_handle,
0a7de745
A
98 "%s: REXMT %d sndnxt %u\n",
99 __func__, mp_tp->mpt_rxtshift,
100 (uint32_t)mp_tp->mpt_sndnxt);
39236c6e 101 mptcp_output(mpte);
39236c6e
A
102 }
103 } else {
104 resched_timer = 1;
105 }
106 break;
107 case MPTT_TW:
108 /* Allows for break before make XXX */
0a7de745 109 if (mp_tp->mpt_timewait == 0) {
39236c6e 110 VERIFY(0);
0a7de745 111 }
39236c6e
A
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 }
39236c6e 126
0a7de745 127 return resched_timer;
39236c6e
A
128}
129
130uint32_t
131mptcp_timer(struct mppcbinfo *mppi)
132{
133 struct mppcb *mpp, *tmpp;
134 struct timeval now;
39236c6e 135 uint32_t resched_timer = 0;
f427ee49 136 uint64_t now_msecs;
39236c6e 137
5ba3f43e 138 LCK_MTX_ASSERT(&mppi->mppi_lock, LCK_MTX_ASSERT_OWNED);
39236c6e
A
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;
39236c6e 147 mpte = mptompte(mpp);
cb323159
A
148 socket_lock(mp_so, 1);
149
39236c6e 150 VERIFY(mpp->mpp_flags & MPP_ATTACHED);
39037602 151
0a7de745 152 if (mptcp_timer_demux(mpte, now_msecs)) {
39236c6e 153 resched_timer = 1;
0a7de745 154 }
cb323159 155 socket_unlock(mp_so, 1);
39236c6e
A
156 }
157
0a7de745 158 return resched_timer;
39236c6e
A
159}
160
161void
3e170ce0 162mptcp_start_timer(struct mptses *mpte, int timer_type)
39236c6e
A
163{
164 struct timeval now;
3e170ce0 165 struct mptcb *mp_tp = mpte->mpte_mptcb;
39236c6e
A
166
167 microuptime(&now);
168
39236c6e 169 DTRACE_MPTCP2(start__timer, struct mptcb *, mp_tp, int, timer_type);
3e170ce0
A
170 mptcplog((LOG_DEBUG, "MPTCP Socket: %s: %d\n", __func__, timer_type),
171 MPTCP_SOCKET_DBG, MPTCP_LOGLVL_VERBOSE);
39236c6e 172
cb323159 173 socket_lock_assert_owned(mptetoso(mpte));
5ba3f43e 174
39236c6e
A
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:
3e170ce0 183 /* XXX: Not implemented yet */
39236c6e
A
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
197void
198mptcp_cancel_timer(struct mptcb *mp_tp, int timer_type)
199{
cb323159 200 socket_lock_assert_owned(mptetoso(mp_tp->mpt_mpte));
39236c6e
A
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
219void
220mptcp_cancel_all_timers(struct mptcb *mp_tp)
221{
cb323159
A
222 struct mptses *mpte = mp_tp->mpt_mpte;
223
224 if (mpte->mpte_time_target) {
225 mptcp_cancel_urgency_timer(mpte);
226 }
227
39236c6e
A
228 mptcp_cancel_timer(mp_tp, MPTT_REXMT);
229 mptcp_cancel_timer(mp_tp, MPTT_TW);
230 mptcp_cancel_timer(mp_tp, MPTT_FASTCLOSE);
231}
cb323159
A
232
233static void
234mptcp_urgency_timer_locked(struct mptses *mpte)
235{
236 uint64_t time_now = mach_continuous_time();
237 struct socket *mp_so = mptetoso(mpte);
238
239 VERIFY(mp_so->so_usecount >= 0);
240
241 os_log(mptcp_log_handle, "%s - %lx: timer at %llu now %llu usecount %u\n",
242 __func__, (unsigned long)VM_KERNEL_ADDRPERM(mpte), mpte->mpte_time_target, time_now, mp_so->so_usecount);
243
244 mptcp_check_subflows_and_add(mpte);
245
246 mp_so->so_usecount--;
247}
248
249static void
250mptcp_urgency_timer(void *param0, __unused void *param1)
251{
252 struct mptses *mpte = (struct mptses *)param0;
253 struct socket *mp_so = mptetoso(mpte);
254
255 socket_lock(mp_so, 1);
256
257 mptcp_urgency_timer_locked(mpte);
258
259 socket_unlock(mp_so, 1);
260}
261
262void
263mptcp_init_urgency_timer(struct mptses *mpte)
264{
265 /* thread_call_allocate never fails */
266 mpte->mpte_time_thread = thread_call_allocate(mptcp_urgency_timer, mpte);
267}
268
269void
270mptcp_set_urgency_timer(struct mptses *mpte)
271{
272 struct socket *mp_so = mptetoso(mpte);
273 uint64_t time_now = 0;
274 boolean_t ret = FALSE;
275
276 socket_lock_assert_owned(mp_so);
277
278 VERIFY(mp_so->so_usecount >= 0);
279 if (mp_so->so_usecount == 0) {
280 goto exit_log;
281 }
282
283 if (mpte->mpte_time_target == 0) {
284 mptcp_cancel_urgency_timer(mpte);
285
286 goto exit_log;
287 }
288
289 time_now = mach_continuous_time();
290
291 if ((int64_t)(mpte->mpte_time_target - time_now) > 0) {
292 mptcp_check_subflows_and_remove(mpte);
293
294 ret = thread_call_enter_delayed_with_leeway(mpte->mpte_time_thread, NULL,
295 mpte->mpte_time_target, 0, THREAD_CALL_CONTINUOUS);
296
297 if (!ret) {
298 mp_so->so_usecount++;
299 }
300 } else if ((int64_t)(mpte->mpte_time_target - time_now) <= 0) {
301 mp_so->so_usecount++;
302
303 /* Already passed the deadline, trigger subflows now */
304 mptcp_urgency_timer_locked(mpte);
305 }
306
307exit_log:
308 os_log(mptcp_log_handle, "%s - %lx: timer at %llu now %llu usecount %u ret %u\n",
309 __func__, (unsigned long)VM_KERNEL_ADDRPERM(mpte), mpte->mpte_time_target, time_now,
310 mp_so->so_usecount, ret);
311}
312
313static int
314mptcp_cancel_urgency_timer(struct mptses *mpte)
315{
316 struct socket *mp_so = mptetoso(mpte);
317 boolean_t ret;
318
319 ret = thread_call_cancel(mpte->mpte_time_thread);
320
321 os_log(mptcp_log_handle, "%s - %lx: Canceled timer thread usecount %u ret %u\n",
322 __func__, (unsigned long)VM_KERNEL_ADDRPERM(mpte), mp_so->so_usecount, ret);
323
324 mptcp_check_subflows_and_remove(mpte);
325
326 if (ret) {
327 mp_so->so_usecount--;
328 }
329
330 return 0;
331}