| 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 |
| 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 |
| 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(tv: &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(mp: mpp); |
| 148 | socket_lock(so: mp_so, refcount: 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(so: mp_so, refcount: 1); |
| 156 | } |
| 157 | |
| 158 | return resched_timer; |
| 159 | } |
| 160 | |
| 161 | void |
| 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(tv: &now); |
| 168 | |
| 169 | DTRACE_MPTCP2(start__timer, struct mptcb *, mp_tp, int, timer_type); |
| 170 | |
| 171 | socket_lock_assert_owned(so: mptetoso(mpte)); |
| 172 | |
| 173 | switch (timer_type) { |
| 174 | case MPTT_REXMT: |
| 175 | mp_tp->mpt_timer_vals |= MPTT_REXMT; |
| 176 | mp_tp->mpt_rxtstart = TIMEVAL_TO_HZ(now); |
| 177 | mp_tp->mpt_rxtshift = 0; |
| 178 | mp_tp->mpt_rtseq = mp_tp->mpt_sndnxt; |
| 179 | break; |
| 180 | case MPTT_TW: |
| 181 | /* XXX: Not implemented yet */ |
| 182 | mp_tp->mpt_timer_vals |= MPTT_TW; |
| 183 | mp_tp->mpt_timewait = TIMEVAL_TO_HZ(now); |
| 184 | break; |
| 185 | case MPTT_FASTCLOSE: |
| 186 | /* NO-OP */ |
| 187 | break; |
| 188 | default: |
| 189 | VERIFY(0); |
| 190 | /* NOTREACHED */ |
| 191 | } |
| 192 | mptcp_timer_sched(); |
| 193 | } |
| 194 | |
| 195 | void |
| 196 | mptcp_cancel_timer(struct mptcb *mp_tp, int timer_type) |
| 197 | { |
| 198 | socket_lock_assert_owned(so: mptetoso(mpte: mp_tp->mpt_mpte)); |
| 199 | |
| 200 | switch (timer_type) { |
| 201 | case MPTT_REXMT: |
| 202 | mp_tp->mpt_rxtstart = 0; |
| 203 | mp_tp->mpt_rxtshift = 0; |
| 204 | mp_tp->mpt_timer_vals = 0; |
| 205 | break; |
| 206 | case MPTT_TW: |
| 207 | /* NO-OP */ |
| 208 | break; |
| 209 | case MPTT_FASTCLOSE: |
| 210 | /* NO-OP */ |
| 211 | break; |
| 212 | default: |
| 213 | break; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | void |
| 218 | mptcp_cancel_all_timers(struct mptcb *mp_tp) |
| 219 | { |
| 220 | struct mptses *mpte = mp_tp->mpt_mpte; |
| 221 | |
| 222 | mptcp_cancel_urgency_timer(mpte); |
| 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 | } |
| 228 | |
| 229 | static void |
| 230 | mptcp_urgency_timer(void *param0, __unused void *param1) |
| 231 | { |
| 232 | struct mptses *mpte = (struct mptses *)param0; |
| 233 | struct socket *mp_so = mptetoso(mpte); |
| 234 | uint64_t time_now; |
| 235 | |
| 236 | socket_lock(so: mp_so, refcount: 1); |
| 237 | |
| 238 | time_now = mach_continuous_time(); |
| 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 | socket_unlock(so: mp_so, refcount: 1); |
| 249 | } |
| 250 | |
| 251 | static void |
| 252 | mptcp_urgency_stop(void *param0, __unused void *param1) |
| 253 | { |
| 254 | struct mptses *mpte = (struct mptses *)param0; |
| 255 | struct socket *mp_so = mptetoso(mpte); |
| 256 | |
| 257 | socket_lock(so: mp_so, refcount: 1); |
| 258 | |
| 259 | VERIFY(mp_so->so_usecount >= 1); |
| 260 | |
| 261 | os_log(mptcp_log_handle, "%s - %lx: usecount %u\n" , |
| 262 | __func__, (unsigned long)VM_KERNEL_ADDRPERM(mpte), mp_so->so_usecount); |
| 263 | |
| 264 | mptcp_check_subflows_and_remove(mpte); |
| 265 | |
| 266 | mp_so->so_usecount--; |
| 267 | |
| 268 | socket_unlock(so: mp_so, refcount: 1); |
| 269 | } |
| 270 | |
| 271 | void |
| 272 | mptcp_init_urgency_timer(struct mptses *mpte) |
| 273 | { |
| 274 | /* thread_call_allocate never fails */ |
| 275 | mpte->mpte_time_thread = thread_call_allocate(func: mptcp_urgency_timer, param0: mpte); |
| 276 | mpte->mpte_stop_urgency = thread_call_allocate(func: mptcp_urgency_stop, param0: mpte); |
| 277 | } |
| 278 | |
| 279 | void |
| 280 | mptcp_set_urgency_timer(struct mptses *mpte) |
| 281 | { |
| 282 | struct socket *mp_so = mptetoso(mpte); |
| 283 | uint64_t time_now = 0; |
| 284 | boolean_t ret = FALSE; |
| 285 | |
| 286 | socket_lock_assert_owned(so: mp_so); |
| 287 | |
| 288 | VERIFY(mp_so->so_usecount >= 1); |
| 289 | |
| 290 | if (mpte->mpte_time_target == 0) { |
| 291 | /* Close subflows right now */ |
| 292 | |
| 293 | ret = thread_call_enter(call: mpte->mpte_stop_urgency); |
| 294 | |
| 295 | if (!ret) { |
| 296 | mp_so->so_usecount++; |
| 297 | } |
| 298 | |
| 299 | goto exit_log; |
| 300 | } |
| 301 | |
| 302 | time_now = mach_continuous_time(); |
| 303 | |
| 304 | if ((int64_t)(mpte->mpte_time_target - time_now) > 0) { |
| 305 | ret = thread_call_enter(call: mpte->mpte_stop_urgency); |
| 306 | |
| 307 | if (!ret) { |
| 308 | mp_so->so_usecount++; |
| 309 | } |
| 310 | |
| 311 | ret = thread_call_enter_delayed_with_leeway(call: mpte->mpte_time_thread, NULL, |
| 312 | deadline: mpte->mpte_time_target, leeway: 0, THREAD_CALL_CONTINUOUS); |
| 313 | |
| 314 | if (!ret) { |
| 315 | mp_so->so_usecount++; |
| 316 | } |
| 317 | } else if ((int64_t)(mpte->mpte_time_target - time_now) <= 0) { |
| 318 | /* Already passed the deadline, trigger subflows now */ |
| 319 | ret = thread_call_enter(call: mpte->mpte_time_thread); |
| 320 | |
| 321 | if (!ret) { |
| 322 | mp_so->so_usecount++; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | exit_log: |
| 327 | os_log(mptcp_log_handle, "%s - %lx: timer at %llu now %llu usecount %u ret %u\n" , |
| 328 | __func__, (unsigned long)VM_KERNEL_ADDRPERM(mpte), mpte->mpte_time_target, time_now, |
| 329 | mp_so->so_usecount, ret); |
| 330 | } |
| 331 | |
| 332 | static int |
| 333 | mptcp_cancel_urgency_timer(struct mptses *mpte) |
| 334 | { |
| 335 | struct socket *mp_so = mptetoso(mpte); |
| 336 | boolean_t ret; |
| 337 | |
| 338 | ret = thread_call_cancel(call: mpte->mpte_time_thread); |
| 339 | |
| 340 | os_log(mptcp_log_handle, "%s - %lx: Canceled timer thread usecount %u ret %u\n" , |
| 341 | __func__, (unsigned long)VM_KERNEL_ADDRPERM(mpte), mp_so->so_usecount, ret); |
| 342 | |
| 343 | mptcp_check_subflows_and_remove(mpte); |
| 344 | |
| 345 | if (ret) { |
| 346 | VERIFY(mp_so->so_usecount >= 1); |
| 347 | mp_so->so_usecount--; |
| 348 | } |
| 349 | |
| 350 | ret = thread_call_cancel(call: mpte->mpte_stop_urgency); |
| 351 | if (ret) { |
| 352 | VERIFY(mp_so->so_usecount >= 1); |
| 353 | mp_so->so_usecount--; |
| 354 | } |
| 355 | |
| 356 | return 0; |
| 357 | } |
| 358 | |