]> git.saurik.com Git - apple/xnu.git/blame - bsd/netinet/mptcp_timer.c
xnu-4903.270.47.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
A
68
69static int
70mptcp_timer_demux(struct mptses *mpte, uint32_t now_msecs)
71{
72 struct mptcb *mp_tp = NULL;
73 mp_tp = mpte->mpte_mptcb;
74 int resched_timer = 0;
75
76 DTRACE_MPTCP2(timer, struct mptses *, mpte, struct mptcb *, mp_tp);
39236c6e 77
5ba3f43e 78 mpte_lock_assert_held(mpte);
39236c6e
A
79 switch (mp_tp->mpt_timer_vals) {
80 case MPTT_REXMT:
0a7de745 81 if (mp_tp->mpt_rxtstart == 0) {
39236c6e 82 break;
0a7de745 83 }
39236c6e 84 if ((now_msecs - mp_tp->mpt_rxtstart) >
0a7de745 85 (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;
135 u_int32_t now_msecs;
136 uint32_t resched_timer = 0;
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;
147 VERIFY(mp_so != NULL);
148 mpte = mptompte(mpp);
149 VERIFY(mpte != NULL);
5ba3f43e 150 mpte_lock(mpte);
39236c6e 151 VERIFY(mpp->mpp_flags & MPP_ATTACHED);
39037602 152
0a7de745 153 if (mptcp_timer_demux(mpte, now_msecs)) {
39236c6e 154 resched_timer = 1;
0a7de745 155 }
5ba3f43e 156 mpte_unlock(mpte);
39236c6e
A
157 }
158
0a7de745 159 return resched_timer;
39236c6e
A
160}
161
162void
3e170ce0 163mptcp_start_timer(struct mptses *mpte, int timer_type)
39236c6e
A
164{
165 struct timeval now;
3e170ce0 166 struct mptcb *mp_tp = mpte->mpte_mptcb;
39236c6e
A
167
168 microuptime(&now);
169
39236c6e 170 DTRACE_MPTCP2(start__timer, struct mptcb *, mp_tp, int, timer_type);
3e170ce0
A
171 mptcplog((LOG_DEBUG, "MPTCP Socket: %s: %d\n", __func__, timer_type),
172 MPTCP_SOCKET_DBG, MPTCP_LOGLVL_VERBOSE);
39236c6e 173
5ba3f43e
A
174 mpte_lock_assert_held(mpte);
175
39236c6e
A
176 switch (timer_type) {
177 case MPTT_REXMT:
178 mp_tp->mpt_timer_vals |= MPTT_REXMT;
179 mp_tp->mpt_rxtstart = TIMEVAL_TO_HZ(now);
180 mp_tp->mpt_rxtshift = 0;
181 mp_tp->mpt_rtseq = mp_tp->mpt_sndnxt;
182 break;
183 case MPTT_TW:
3e170ce0 184 /* XXX: Not implemented yet */
39236c6e
A
185 mp_tp->mpt_timer_vals |= MPTT_TW;
186 mp_tp->mpt_timewait = TIMEVAL_TO_HZ(now);
187 break;
188 case MPTT_FASTCLOSE:
189 /* NO-OP */
190 break;
191 default:
192 VERIFY(0);
193 /* NOTREACHED */
194 }
195 mptcp_timer_sched();
196}
197
198void
199mptcp_cancel_timer(struct mptcb *mp_tp, int timer_type)
200{
5ba3f43e 201 mpte_lock_assert_held(mp_tp->mpt_mpte);
39236c6e 202 DTRACE_MPTCP2(cancel__timer, struct mptcb *, mp_tp, int, timer_type);
39236c6e
A
203
204 switch (timer_type) {
205 case MPTT_REXMT:
206 mp_tp->mpt_rxtstart = 0;
207 mp_tp->mpt_rxtshift = 0;
208 mp_tp->mpt_timer_vals = 0;
209 break;
210 case MPTT_TW:
211 /* NO-OP */
212 break;
213 case MPTT_FASTCLOSE:
214 /* NO-OP */
215 break;
216 default:
217 break;
218 }
219}
220
221void
222mptcp_cancel_all_timers(struct mptcb *mp_tp)
223{
224 mptcp_cancel_timer(mp_tp, MPTT_REXMT);
225 mptcp_cancel_timer(mp_tp, MPTT_TW);
226 mptcp_cancel_timer(mp_tp, MPTT_FASTCLOSE);
227}