2 * Copyright (c) 2015 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 /* TCP-cache to store and retrieve TCP-related information */
31 #include <net/flowhash.h>
32 #include <net/route.h>
33 #include <netinet/in_pcb.h>
34 #include <netinet/tcp_cache.h>
35 #include <netinet/tcp_seq.h>
36 #include <netinet/tcp_var.h>
37 #include <kern/locks.h>
38 #include <sys/queue.h>
39 #include <dev/random/randomdev.h>
41 struct tcp_heuristic_key
{
43 uint8_t thk_net_signature
[IFNET_SIGNATURELEN
];
46 struct in6_addr addr6
;
49 sa_family_t thk_family
;
52 struct tcp_heuristic
{
53 SLIST_ENTRY(tcp_heuristic
) list
;
55 u_int32_t th_last_access
;
57 struct tcp_heuristic_key th_key
;
59 char th_val_start
[0]; /* Marker for memsetting to 0 */
61 u_int8_t th_tfo_cookie_loss
; /* The number of times a SYN+cookie has been lost */
62 u_int8_t th_ecn_loss
; /* The number of times a SYN+ecn has been lost */
63 u_int8_t th_ecn_aggressive
; /* The number of times we did an aggressive fallback */
64 u_int32_t th_tfo_fallback_trials
; /* Number of times we did not try out TFO due to SYN-loss */
65 u_int32_t th_tfo_cookie_backoff
; /* Time until when we should not try out TFO */
66 u_int32_t th_ecn_backoff
; /* Time until when we should not try out ECN */
68 u_int8_t th_tfo_in_backoff
:1, /* Are we avoiding TFO due to the backoff timer? */
69 th_tfo_aggressive_fallback
:1, /* Aggressive fallback due to nasty middlebox */
70 th_tfo_snd_middlebox_supp
:1, /* We are sure that the network supports TFO in upstream direction */
71 th_tfo_rcv_middlebox_supp
:1; /* We are sure that the network supports TFO in downstream direction*/
73 char th_val_end
[0]; /* Marker for memsetting to 0 */
76 struct tcp_heuristics_head
{
77 SLIST_HEAD(tcp_heur_bucket
, tcp_heuristic
) tcp_heuristics
;
79 /* Per-hashbucket lock to avoid lock-contention */
83 struct tcp_cache_key
{
84 sa_family_t tck_family
;
86 struct tcp_heuristic_key tck_src
;
89 struct in6_addr addr6
;
94 SLIST_ENTRY(tcp_cache
) list
;
96 u_int32_t tc_last_access
;
98 struct tcp_cache_key tc_key
;
100 u_int8_t tc_tfo_cookie
[TFO_COOKIE_LEN_MAX
];
101 u_int8_t tc_tfo_cookie_len
;
104 struct tcp_cache_head
{
105 SLIST_HEAD(tcp_cache_bucket
, tcp_cache
) tcp_caches
;
107 /* Per-hashbucket lock to avoid lock-contention */
111 static u_int32_t tcp_cache_hash_seed
;
113 size_t tcp_cache_size
;
116 * The maximum depth of the hash-bucket. This way we limit the tcp_cache to
117 * TCP_CACHE_BUCKET_SIZE * tcp_cache_size and have "natural" garbage collection
119 #define TCP_CACHE_BUCKET_SIZE 5
121 static struct tcp_cache_head
*tcp_cache
;
123 decl_lck_mtx_data(, tcp_cache_mtx
);
125 static lck_attr_t
*tcp_cache_mtx_attr
;
126 static lck_grp_t
*tcp_cache_mtx_grp
;
127 static lck_grp_attr_t
*tcp_cache_mtx_grp_attr
;
129 static struct tcp_heuristics_head
*tcp_heuristics
;
131 decl_lck_mtx_data(, tcp_heuristics_mtx
);
133 static lck_attr_t
*tcp_heuristic_mtx_attr
;
134 static lck_grp_t
*tcp_heuristic_mtx_grp
;
135 static lck_grp_attr_t
*tcp_heuristic_mtx_grp_attr
;
137 int tcp_ecn_timeout
= 60;
138 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, ecn_timeout
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
139 &tcp_ecn_timeout
, 0, "Initial minutes to wait before re-trying ECN");
142 * Round up to next higher power-of 2. See "Bit Twiddling Hacks".
144 * Might be worth moving this to a library so that others
145 * (e.g., scale_to_powerof2()) can use this as well instead of a while-loop.
147 static u_int32_t
tcp_cache_roundup2(u_int32_t a
)
160 static void tcp_cache_hash_src(struct inpcb
*inp
, struct tcp_heuristic_key
*key
)
162 struct ifnet
*ifn
= inp
->inp_last_outifp
;
163 uint8_t len
= sizeof(key
->thk_net_signature
);
166 if (inp
->inp_vflag
& INP_IPV6
) {
169 key
->thk_family
= AF_INET6
;
170 ret
= ifnet_get_netsignature(ifn
, AF_INET6
, &len
, &flags
,
171 key
->thk_net_signature
);
174 * ifnet_get_netsignature only returns EINVAL if ifn is NULL
175 * (we made sure that in the other cases it does not). So,
176 * in this case we should take the connection's address.
178 if (ret
== ENOENT
|| ret
== EINVAL
)
179 memcpy(&key
->thk_ip
.addr6
, &inp
->in6p_laddr
, sizeof(struct in6_addr
));
183 key
->thk_family
= AF_INET
;
184 ret
= ifnet_get_netsignature(ifn
, AF_INET
, &len
, &flags
,
185 key
->thk_net_signature
);
188 * ifnet_get_netsignature only returns EINVAL if ifn is NULL
189 * (we made sure that in the other cases it does not). So,
190 * in this case we should take the connection's address.
192 if (ret
== ENOENT
|| ret
== EINVAL
)
193 memcpy(&key
->thk_ip
.addr
, &inp
->inp_laddr
, sizeof(struct in_addr
));
197 static u_int16_t
tcp_cache_hash(struct inpcb
*inp
, struct tcp_cache_key
*key
)
201 bzero(key
, sizeof(struct tcp_cache_key
));
203 tcp_cache_hash_src(inp
, &key
->tck_src
);
205 if (inp
->inp_vflag
& INP_IPV6
) {
206 key
->tck_family
= AF_INET6
;
207 memcpy(&key
->tck_dst
.addr6
, &inp
->in6p_faddr
,
208 sizeof(struct in6_addr
));
210 key
->tck_family
= AF_INET
;
211 memcpy(&key
->tck_dst
.addr
, &inp
->inp_faddr
,
212 sizeof(struct in_addr
));
215 hash
= net_flowhash(key
, sizeof(struct tcp_cache_key
),
216 tcp_cache_hash_seed
);
218 return (hash
& (tcp_cache_size
- 1));
221 static void tcp_cache_unlock(struct tcp_cache_head
*head
)
223 lck_mtx_unlock(&head
->tch_mtx
);
227 * Make sure that everything that happens after tcp_getcache_with_lock()
228 * is short enough to justify that you hold the per-bucket lock!!!
230 * Otherwise, better build another lookup-function that does not hold the
231 * lock and you copy out the bits and bytes.
233 * That's why we provide the head as a "return"-pointer so that the caller
234 * can give it back to use for tcp_cache_unlock().
236 static struct tcp_cache
*tcp_getcache_with_lock(struct tcpcb
*tp
, int create
,
237 struct tcp_cache_head
**headarg
)
239 struct inpcb
*inp
= tp
->t_inpcb
;
240 struct tcp_cache
*tpcache
= NULL
;
241 struct tcp_cache_head
*head
;
242 struct tcp_cache_key key
;
246 hash
= tcp_cache_hash(inp
, &key
);
247 head
= &tcp_cache
[hash
];
249 lck_mtx_lock(&head
->tch_mtx
);
251 /*** First step: Look for the tcp_cache in our bucket ***/
252 SLIST_FOREACH(tpcache
, &head
->tcp_caches
, list
) {
253 if (memcmp(&tpcache
->tc_key
, &key
, sizeof(key
)) == 0)
259 /*** Second step: If it's not there, create/recycle it ***/
260 if ((tpcache
== NULL
) && create
) {
261 if (i
>= TCP_CACHE_BUCKET_SIZE
) {
262 struct tcp_cache
*oldest_cache
= NULL
;
263 u_int32_t max_age
= 0;
265 /* Look for the oldest tcp_cache in the bucket */
266 SLIST_FOREACH(tpcache
, &head
->tcp_caches
, list
) {
267 u_int32_t age
= tcp_now
- tpcache
->tc_last_access
;
270 oldest_cache
= tpcache
;
273 VERIFY(oldest_cache
!= NULL
);
275 tpcache
= oldest_cache
;
277 /* We recycle, thus let's indicate that there is no cookie */
278 tpcache
->tc_tfo_cookie_len
= 0;
280 /* Create a new cache and add it to the list */
281 tpcache
= _MALLOC(sizeof(struct tcp_cache
), M_TEMP
,
286 SLIST_INSERT_HEAD(&head
->tcp_caches
, tpcache
, list
);
289 memcpy(&tpcache
->tc_key
, &key
, sizeof(key
));
295 /* Update timestamp for garbage collection purposes */
296 tpcache
->tc_last_access
= tcp_now
;
302 tcp_cache_unlock(head
);
306 void tcp_cache_set_cookie(struct tcpcb
*tp
, u_char
*cookie
, u_int8_t len
)
308 struct tcp_cache_head
*head
;
309 struct tcp_cache
*tpcache
;
311 /* Call lookup/create function */
312 tpcache
= tcp_getcache_with_lock(tp
, 1, &head
);
316 tpcache
->tc_tfo_cookie_len
= len
;
317 memcpy(tpcache
->tc_tfo_cookie
, cookie
, len
);
319 tcp_cache_unlock(head
);
323 * Get the cookie related to 'tp', and copy it into 'cookie', provided that len
324 * is big enough (len designates the available memory.
325 * Upon return, 'len' is set to the cookie's length.
327 * Returns 0 if we should request a cookie.
328 * Returns 1 if the cookie has been found and written.
330 int tcp_cache_get_cookie(struct tcpcb
*tp
, u_char
*cookie
, u_int8_t
*len
)
332 struct tcp_cache_head
*head
;
333 struct tcp_cache
*tpcache
;
335 /* Call lookup/create function */
336 tpcache
= tcp_getcache_with_lock(tp
, 1, &head
);
340 if (tpcache
->tc_tfo_cookie_len
== 0) {
341 tcp_cache_unlock(head
);
346 * Not enough space - this should never happen as it has been checked
347 * in tcp_tfo_check. So, fail here!
349 VERIFY(tpcache
->tc_tfo_cookie_len
<= *len
);
351 memcpy(cookie
, tpcache
->tc_tfo_cookie
, tpcache
->tc_tfo_cookie_len
);
352 *len
= tpcache
->tc_tfo_cookie_len
;
354 tcp_cache_unlock(head
);
359 unsigned int tcp_cache_get_cookie_len(struct tcpcb
*tp
)
361 struct tcp_cache_head
*head
;
362 struct tcp_cache
*tpcache
;
363 unsigned int cookie_len
;
365 /* Call lookup/create function */
366 tpcache
= tcp_getcache_with_lock(tp
, 1, &head
);
370 cookie_len
= tpcache
->tc_tfo_cookie_len
;
372 tcp_cache_unlock(head
);
377 static u_int16_t
tcp_heuristics_hash(struct inpcb
*inp
,
378 struct tcp_heuristic_key
*key
)
382 bzero(key
, sizeof(struct tcp_heuristic_key
));
384 tcp_cache_hash_src(inp
, key
);
386 hash
= net_flowhash(key
, sizeof(struct tcp_heuristic_key
),
387 tcp_cache_hash_seed
);
389 return (hash
& (tcp_cache_size
- 1));
392 static void tcp_heuristic_unlock(struct tcp_heuristics_head
*head
)
394 lck_mtx_unlock(&head
->thh_mtx
);
398 * Make sure that everything that happens after tcp_getheuristic_with_lock()
399 * is short enough to justify that you hold the per-bucket lock!!!
401 * Otherwise, better build another lookup-function that does not hold the
402 * lock and you copy out the bits and bytes.
404 * That's why we provide the head as a "return"-pointer so that the caller
405 * can give it back to use for tcp_heur_unlock().
408 * ToDo - way too much code-duplication. We should create an interface to handle
409 * bucketized hashtables with recycling of the oldest element.
411 static struct tcp_heuristic
*tcp_getheuristic_with_lock(struct tcpcb
*tp
,
412 int create
, struct tcp_heuristics_head
**headarg
)
414 struct inpcb
*inp
= tp
->t_inpcb
;
415 struct tcp_heuristic
*tpheur
= NULL
;
416 struct tcp_heuristics_head
*head
;
417 struct tcp_heuristic_key key
;
421 hash
= tcp_heuristics_hash(inp
, &key
);
422 head
= &tcp_heuristics
[hash
];
424 lck_mtx_lock(&head
->thh_mtx
);
426 /*** First step: Look for the tcp_heur in our bucket ***/
427 SLIST_FOREACH(tpheur
, &head
->tcp_heuristics
, list
) {
428 if (memcmp(&tpheur
->th_key
, &key
, sizeof(key
)) == 0)
434 /*** Second step: If it's not there, create/recycle it ***/
435 if ((tpheur
== NULL
) && create
) {
436 if (i
>= TCP_CACHE_BUCKET_SIZE
) {
437 struct tcp_heuristic
*oldest_heur
= NULL
;
438 u_int32_t max_age
= 0;
440 /* Look for the oldest tcp_heur in the bucket */
441 SLIST_FOREACH(tpheur
, &head
->tcp_heuristics
, list
) {
442 u_int32_t age
= tcp_now
- tpheur
->th_last_access
;
445 oldest_heur
= tpheur
;
448 VERIFY(oldest_heur
!= NULL
);
450 tpheur
= oldest_heur
;
452 /* We recycle - set everything to 0 */
453 bzero(tpheur
->th_val_start
,
454 tpheur
->th_val_end
- tpheur
->th_val_start
);
456 /* Create a new heuristic and add it to the list */
457 tpheur
= _MALLOC(sizeof(struct tcp_heuristic
), M_TEMP
,
462 SLIST_INSERT_HEAD(&head
->tcp_heuristics
, tpheur
, list
);
466 * Set to tcp_now, to make sure it won't be > than tcp_now in the
469 tpheur
->th_ecn_backoff
= tcp_now
;
470 tpheur
->th_tfo_cookie_backoff
= tcp_now
;
472 memcpy(&tpheur
->th_key
, &key
, sizeof(key
));
478 /* Update timestamp for garbage collection purposes */
479 tpheur
->th_last_access
= tcp_now
;
485 tcp_heuristic_unlock(head
);
489 void tcp_heuristic_tfo_success(struct tcpcb
*tp
)
491 struct tcp_heuristics_head
*head
;
493 struct tcp_heuristic
*tpheur
= tcp_getheuristic_with_lock(tp
, 1, &head
);
497 tpheur
->th_tfo_cookie_loss
= 0;
499 tcp_heuristic_unlock(head
);
502 void tcp_heuristic_tfo_rcv_good(struct tcpcb
*tp
)
504 struct tcp_heuristics_head
*head
;
506 struct tcp_heuristic
*tpheur
= tcp_getheuristic_with_lock(tp
, 1, &head
);
510 tpheur
->th_tfo_rcv_middlebox_supp
= 1;
512 tcp_heuristic_unlock(head
);
514 tp
->t_tfo_flags
|= TFO_F_NO_RCVPROBING
;
517 void tcp_heuristic_tfo_snd_good(struct tcpcb
*tp
)
519 struct tcp_heuristics_head
*head
;
521 struct tcp_heuristic
*tpheur
= tcp_getheuristic_with_lock(tp
, 1, &head
);
525 tpheur
->th_tfo_snd_middlebox_supp
= 1;
527 tcp_heuristic_unlock(head
);
529 tp
->t_tfo_flags
|= TFO_F_NO_SNDPROBING
;
532 void tcp_heuristic_inc_loss(struct tcpcb
*tp
, int tfo
, int ecn
)
534 struct tcp_heuristics_head
*head
;
535 struct tcp_heuristic
*tpheur
;
537 tpheur
= tcp_getheuristic_with_lock(tp
, 1, &head
);
541 /* Limit to 9 to prevent integer-overflow during exponential backoff */
542 if (tfo
&& tpheur
->th_tfo_cookie_loss
< 9)
543 tpheur
->th_tfo_cookie_loss
++;
545 if (ecn
&& tpheur
->th_ecn_loss
< 9) {
546 tpheur
->th_ecn_loss
++;
547 if (tpheur
->th_ecn_loss
>= ECN_MAX_SYN_LOSS
) {
548 tcpstat
.tcps_ecn_fallback_synloss
++;
549 INP_INC_IFNET_STAT(tp
->t_inpcb
, ecn_fallback_synloss
);
550 tpheur
->th_ecn_backoff
= tcp_now
+
551 ((tcp_ecn_timeout
* 60 * TCP_RETRANSHZ
)
552 << (tpheur
->th_ecn_loss
- ECN_MAX_SYN_LOSS
));
556 tcp_heuristic_unlock(head
);
559 void tcp_heuristic_tfo_middlebox(struct tcpcb
*tp
)
561 struct tcp_heuristics_head
*head
;
562 struct tcp_heuristic
*tpheur
;
564 tpheur
= tcp_getheuristic_with_lock(tp
, 1, &head
);
568 tpheur
->th_tfo_aggressive_fallback
= 1;
570 tcp_heuristic_unlock(head
);
573 void tcp_heuristic_ecn_aggressive(struct tcpcb
*tp
)
575 struct tcp_heuristics_head
*head
;
576 struct tcp_heuristic
*tpheur
;
578 tpheur
= tcp_getheuristic_with_lock(tp
, 1, &head
);
582 /* Must be done before, otherwise we will start off with expo-backoff */
583 tpheur
->th_ecn_backoff
= tcp_now
+
584 ((tcp_ecn_timeout
* 60 * TCP_RETRANSHZ
) << (tpheur
->th_ecn_aggressive
));
587 * Ugly way to prevent integer overflow... limit to 9 to prevent in
588 * overflow during exp. backoff.
590 if (tpheur
->th_ecn_aggressive
< 9)
591 tpheur
->th_ecn_aggressive
++;
593 tcp_heuristic_unlock(head
);
596 void tcp_heuristic_reset_loss(struct tcpcb
*tp
, int tfo
, int ecn
)
598 struct tcp_heuristics_head
*head
;
599 struct tcp_heuristic
*tpheur
;
602 * Don't attempt to create it! Keep the heuristics clean if the
603 * server does not support TFO. This reduces the lookup-cost on
606 tpheur
= tcp_getheuristic_with_lock(tp
, 0, &head
);
611 tpheur
->th_tfo_cookie_loss
= 0;
614 tpheur
->th_ecn_loss
= 0;
616 tcp_heuristic_unlock(head
);
619 boolean_t
tcp_heuristic_do_tfo(struct tcpcb
*tp
)
621 struct tcp_heuristics_head
*head
;
622 struct tcp_heuristic
*tpheur
;
624 /* Get the tcp-heuristic. */
625 tpheur
= tcp_getheuristic_with_lock(tp
, 0, &head
);
629 if (tpheur
->th_tfo_aggressive_fallback
) {
630 /* Aggressive fallback - don't do TFO anymore... :'( */
631 tcp_heuristic_unlock(head
);
635 if (tpheur
->th_tfo_cookie_loss
>= TFO_MAX_COOKIE_LOSS
&&
636 (tpheur
->th_tfo_fallback_trials
< tcp_tfo_fallback_min
||
637 TSTMP_GT(tpheur
->th_tfo_cookie_backoff
, tcp_now
))) {
639 * So, when we are in SYN-loss mode we try to stop using TFO
640 * for the next 'tcp_tfo_fallback_min' connections. That way,
641 * we are sure that never more than 1 out of tcp_tfo_fallback_min
642 * connections will suffer from our nice little middelbox.
644 * After that we first wait for 2 minutes. If we fail again,
645 * we wait for yet another 60 minutes.
647 tpheur
->th_tfo_fallback_trials
++;
648 if (tpheur
->th_tfo_fallback_trials
>= tcp_tfo_fallback_min
&&
649 !tpheur
->th_tfo_in_backoff
) {
650 if (tpheur
->th_tfo_cookie_loss
== TFO_MAX_COOKIE_LOSS
)
651 /* Backoff for 2 minutes */
652 tpheur
->th_tfo_cookie_backoff
= tcp_now
+ (60 * 2 * TCP_RETRANSHZ
);
654 /* Backoff for 60 minutes */
655 tpheur
->th_tfo_cookie_backoff
= tcp_now
+ (60 * 60 * TCP_RETRANSHZ
);
657 tpheur
->th_tfo_in_backoff
= 1;
660 tcp_heuristic_unlock(head
);
665 * We give it a new shot, set trials back to 0. This allows to
666 * start counting again from zero in case we get yet another SYN-loss
668 tpheur
->th_tfo_fallback_trials
= 0;
669 tpheur
->th_tfo_in_backoff
= 0;
671 if (tpheur
->th_tfo_rcv_middlebox_supp
)
672 tp
->t_tfo_flags
|= TFO_F_NO_RCVPROBING
;
673 if (tpheur
->th_tfo_snd_middlebox_supp
)
674 tp
->t_tfo_flags
|= TFO_F_NO_SNDPROBING
;
676 tcp_heuristic_unlock(head
);
681 boolean_t
tcp_heuristic_do_ecn(struct tcpcb
*tp
)
683 struct tcp_heuristics_head
*head
;
684 struct tcp_heuristic
*tpheur
;
685 boolean_t ret
= true;
687 /* Get the tcp-heuristic. */
688 tpheur
= tcp_getheuristic_with_lock(tp
, 0, &head
);
692 if (TSTMP_GT(tpheur
->th_ecn_backoff
, tcp_now
))
695 tcp_heuristic_unlock(head
);
700 static void sysctl_cleartfocache(void)
704 for (i
= 0; i
< tcp_cache_size
; i
++) {
705 struct tcp_cache_head
*head
= &tcp_cache
[i
];
706 struct tcp_cache
*tpcache
, *tmp
;
707 struct tcp_heuristics_head
*hhead
= &tcp_heuristics
[i
];
708 struct tcp_heuristic
*tpheur
, *htmp
;
710 lck_mtx_lock(&head
->tch_mtx
);
711 SLIST_FOREACH_SAFE(tpcache
, &head
->tcp_caches
, list
, tmp
) {
712 SLIST_REMOVE(&head
->tcp_caches
, tpcache
, tcp_cache
, list
);
713 _FREE(tpcache
, M_TEMP
);
715 lck_mtx_unlock(&head
->tch_mtx
);
717 lck_mtx_lock(&hhead
->thh_mtx
);
718 SLIST_FOREACH_SAFE(tpheur
, &hhead
->tcp_heuristics
, list
, htmp
) {
719 SLIST_REMOVE(&hhead
->tcp_heuristics
, tpheur
, tcp_heuristic
, list
);
720 _FREE(tpheur
, M_TEMP
);
722 lck_mtx_unlock(&hhead
->thh_mtx
);
726 /* This sysctl is useful for testing purposes only */
727 static int tcpcleartfo
= 0;
729 static int sysctl_cleartfo SYSCTL_HANDLER_ARGS
731 #pragma unused(arg1, arg2)
732 int error
= 0, val
, oldval
= tcpcleartfo
;
735 error
= sysctl_handle_int(oidp
, &val
, 0, req
);
736 if (error
|| !req
->newptr
)
740 * The actual value does not matter. If the value is set, it triggers
741 * the clearing of the TFO cache. If a future implementation does not
742 * use the route entry to hold the TFO cache, replace the route sysctl.
746 sysctl_cleartfocache();
753 SYSCTL_PROC(_net_inet_tcp
, OID_AUTO
, clear_tfocache
, CTLTYPE_INT
| CTLFLAG_RW
|
754 CTLFLAG_LOCKED
, &tcpcleartfo
, 0, &sysctl_cleartfo
, "I",
755 "Toggle to clear the TFO destination based heuristic cache");
757 void tcp_cache_init(void)
759 uint64_t sane_size_meg
= sane_size
/ 1024 / 1024;
763 * On machines with <100MB of memory this will result in a (full) cache-size
764 * of 32 entries, thus 32 * 5 * 64bytes = 10KB. (about 0.01 %)
765 * On machines with > 4GB of memory, we have a cache-size of 1024 entries,
768 * Side-note: we convert to u_int32_t. If sane_size is more than
769 * 16000 TB, we loose precision. But, who cares? :)
771 tcp_cache_size
= tcp_cache_roundup2((u_int32_t
)(sane_size_meg
>> 2));
772 if (tcp_cache_size
< 32)
774 else if (tcp_cache_size
> 1024)
775 tcp_cache_size
= 1024;
777 tcp_cache
= _MALLOC(sizeof(struct tcp_cache_head
) * tcp_cache_size
,
779 if (tcp_cache
== NULL
)
780 panic("Allocating tcp_cache failed at boot-time!");
782 tcp_cache_mtx_grp_attr
= lck_grp_attr_alloc_init();
783 tcp_cache_mtx_grp
= lck_grp_alloc_init("tcpcache", tcp_cache_mtx_grp_attr
);
784 tcp_cache_mtx_attr
= lck_attr_alloc_init();
786 tcp_heuristics
= _MALLOC(sizeof(struct tcp_heuristics_head
) * tcp_cache_size
,
788 if (tcp_heuristics
== NULL
)
789 panic("Allocating tcp_heuristic failed at boot-time!");
791 tcp_heuristic_mtx_grp_attr
= lck_grp_attr_alloc_init();
792 tcp_heuristic_mtx_grp
= lck_grp_alloc_init("tcpheuristic", tcp_heuristic_mtx_grp_attr
);
793 tcp_heuristic_mtx_attr
= lck_attr_alloc_init();
795 for (i
= 0; i
< tcp_cache_size
; i
++) {
796 lck_mtx_init(&tcp_cache
[i
].tch_mtx
, tcp_cache_mtx_grp
,
798 SLIST_INIT(&tcp_cache
[i
].tcp_caches
);
800 lck_mtx_init(&tcp_heuristics
[i
].thh_mtx
, tcp_heuristic_mtx_grp
,
801 tcp_heuristic_mtx_attr
);
802 SLIST_INIT(&tcp_heuristics
[i
].tcp_heuristics
);
805 tcp_cache_hash_seed
= RandomULong();