Deleted Added
full compact
tcp_hostcache.c (185419) tcp_hostcache.c (185571)
1/*-
2 * Copyright (c) 2002 Andre Oppermann, Internet Business Solutions AG
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote
14 * products derived from this software without specific prior written
15 * permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30/*
31 * The tcp_hostcache moves the tcp-specific cached metrics from the routing
32 * table to a dedicated structure indexed by the remote IP address. It keeps
33 * information on the measured TCP parameters of past TCP sessions to allow
34 * better initial start values to be used with later connections to/from the
35 * same source. Depending on the network parameters (delay, bandwidth, max
36 * MTU, congestion window) between local and remote sites, this can lead to
37 * significant speed-ups for new TCP connections after the first one.
38 *
39 * Due to the tcp_hostcache, all TCP-specific metrics information in the
40 * routing table have been removed. The inpcb no longer keeps a pointer to
41 * the routing entry, and protocol-initiated route cloning has been removed
42 * as well. With these changes, the routing table has gone back to being
43 * more lightwight and only carries information related to packet forwarding.
44 *
45 * tcp_hostcache is designed for multiple concurrent access in SMP
46 * environments and high contention. All bucket rows have their own lock and
47 * thus multiple lookups and modifies can be done at the same time as long as
48 * they are in different bucket rows. If a request for insertion of a new
49 * record can't be satisfied, it simply returns an empty structure. Nobody
50 * and nothing outside of tcp_hostcache.c will ever point directly to any
51 * entry in the tcp_hostcache. All communication is done in an
52 * object-oriented way and only functions of tcp_hostcache will manipulate
53 * hostcache entries. Otherwise, we are unable to achieve good behaviour in
54 * concurrent access situations. Since tcp_hostcache is only caching
55 * information, there are no fatal consequences if we either can't satisfy
56 * any particular request or have to drop/overwrite an existing entry because
57 * of bucket limit memory constrains.
58 */
59
60/*
61 * Many thanks to jlemon for basic structure of tcp_syncache which is being
62 * followed here.
63 */
64
65#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 2002 Andre Oppermann, Internet Business Solutions AG
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote
14 * products derived from this software without specific prior written
15 * permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30/*
31 * The tcp_hostcache moves the tcp-specific cached metrics from the routing
32 * table to a dedicated structure indexed by the remote IP address. It keeps
33 * information on the measured TCP parameters of past TCP sessions to allow
34 * better initial start values to be used with later connections to/from the
35 * same source. Depending on the network parameters (delay, bandwidth, max
36 * MTU, congestion window) between local and remote sites, this can lead to
37 * significant speed-ups for new TCP connections after the first one.
38 *
39 * Due to the tcp_hostcache, all TCP-specific metrics information in the
40 * routing table have been removed. The inpcb no longer keeps a pointer to
41 * the routing entry, and protocol-initiated route cloning has been removed
42 * as well. With these changes, the routing table has gone back to being
43 * more lightwight and only carries information related to packet forwarding.
44 *
45 * tcp_hostcache is designed for multiple concurrent access in SMP
46 * environments and high contention. All bucket rows have their own lock and
47 * thus multiple lookups and modifies can be done at the same time as long as
48 * they are in different bucket rows. If a request for insertion of a new
49 * record can't be satisfied, it simply returns an empty structure. Nobody
50 * and nothing outside of tcp_hostcache.c will ever point directly to any
51 * entry in the tcp_hostcache. All communication is done in an
52 * object-oriented way and only functions of tcp_hostcache will manipulate
53 * hostcache entries. Otherwise, we are unable to achieve good behaviour in
54 * concurrent access situations. Since tcp_hostcache is only caching
55 * information, there are no fatal consequences if we either can't satisfy
56 * any particular request or have to drop/overwrite an existing entry because
57 * of bucket limit memory constrains.
58 */
59
60/*
61 * Many thanks to jlemon for basic structure of tcp_syncache which is being
62 * followed here.
63 */
64
65#include <sys/cdefs.h>
66__FBSDID("$FreeBSD: head/sys/netinet/tcp_hostcache.c 185419 2008-11-28 23:30:51Z zec $");
66__FBSDID("$FreeBSD: head/sys/netinet/tcp_hostcache.c 185571 2008-12-02 21:37:28Z bz $");
67
68#include "opt_inet6.h"
69
70#include <sys/param.h>
71#include <sys/systm.h>
72#include <sys/kernel.h>
73#include <sys/lock.h>
74#include <sys/mutex.h>
75#include <sys/malloc.h>
76#include <sys/socket.h>
77#include <sys/socketvar.h>
78#include <sys/sysctl.h>
79#include <sys/vimage.h>
80
81#include <net/if.h>
82
83#include <netinet/in.h>
84#include <netinet/in_systm.h>
85#include <netinet/ip.h>
86#include <netinet/in_var.h>
87#include <netinet/in_pcb.h>
88#include <netinet/ip_var.h>
89#ifdef INET6
90#include <netinet/ip6.h>
91#include <netinet6/ip6_var.h>
92#endif
93#include <netinet/tcp.h>
94#include <netinet/tcp_var.h>
67
68#include "opt_inet6.h"
69
70#include <sys/param.h>
71#include <sys/systm.h>
72#include <sys/kernel.h>
73#include <sys/lock.h>
74#include <sys/mutex.h>
75#include <sys/malloc.h>
76#include <sys/socket.h>
77#include <sys/socketvar.h>
78#include <sys/sysctl.h>
79#include <sys/vimage.h>
80
81#include <net/if.h>
82
83#include <netinet/in.h>
84#include <netinet/in_systm.h>
85#include <netinet/ip.h>
86#include <netinet/in_var.h>
87#include <netinet/in_pcb.h>
88#include <netinet/ip_var.h>
89#ifdef INET6
90#include <netinet/ip6.h>
91#include <netinet6/ip6_var.h>
92#endif
93#include <netinet/tcp.h>
94#include <netinet/tcp_var.h>
95#include <netinet/tcp_hostcache.h>
96#include <netinet/vinet.h>
95#ifdef INET6
96#include <netinet6/tcp6_var.h>
97#endif
98
99#include <vm/uma.h>
100
101/* Arbitrary values */
102#define TCP_HOSTCACHE_HASHSIZE 512
103#define TCP_HOSTCACHE_BUCKETLIMIT 30
104#define TCP_HOSTCACHE_EXPIRE 60*60 /* one hour */
105#define TCP_HOSTCACHE_PRUNE 5*60 /* every 5 minutes */
106
107#ifdef VIMAGE_GLOBALS
108static struct tcp_hostcache tcp_hostcache;
109static struct callout tcp_hc_callout;
110#endif
111
112static struct hc_metrics *tcp_hc_lookup(struct in_conninfo *);
113static struct hc_metrics *tcp_hc_insert(struct in_conninfo *);
114static int sysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS);
115static void tcp_hc_purge(void *);
116
117SYSCTL_NODE(_net_inet_tcp, OID_AUTO, hostcache, CTLFLAG_RW, 0,
118 "TCP Host cache");
119
120SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_hostcache, OID_AUTO, cachelimit,
121 CTLFLAG_RDTUN, tcp_hostcache.cache_limit, 0,
122 "Overall entry limit for hostcache");
123
124SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_hostcache, OID_AUTO, hashsize,
125 CTLFLAG_RDTUN, tcp_hostcache.hashsize, 0,
126 "Size of TCP hostcache hashtable");
127
128SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_hostcache, OID_AUTO, bucketlimit,
129 CTLFLAG_RDTUN, tcp_hostcache.bucket_limit, 0,
130 "Per-bucket hash limit for hostcache");
131
132SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_hostcache, OID_AUTO, count,
133 CTLFLAG_RD, tcp_hostcache.cache_count, 0,
134 "Current number of entries in hostcache");
135
136SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_hostcache, OID_AUTO, expire,
137 CTLFLAG_RW, tcp_hostcache.expire, 0,
138 "Expire time of TCP hostcache entries");
139
140SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_hostcache, OID_AUTO, prune,
141 CTLFLAG_RW, tcp_hostcache.prune, 0, "Time between purge runs");
142
143SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_hostcache, OID_AUTO, purge,
144 CTLFLAG_RW, tcp_hostcache.purgeall, 0,
145 "Expire all entires on next purge run");
146
147SYSCTL_PROC(_net_inet_tcp_hostcache, OID_AUTO, list,
148 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_SKIP, 0, 0,
149 sysctl_tcp_hc_list, "A", "List of all hostcache entries");
150
151
152static MALLOC_DEFINE(M_HOSTCACHE, "hostcache", "TCP hostcache");
153
154#define HOSTCACHE_HASH(ip) \
155 (((ip)->s_addr ^ ((ip)->s_addr >> 7) ^ ((ip)->s_addr >> 17)) & \
156 V_tcp_hostcache.hashmask)
157
158/* XXX: What is the recommended hash to get good entropy for IPv6 addresses? */
159#define HOSTCACHE_HASH6(ip6) \
160 (((ip6)->s6_addr32[0] ^ \
161 (ip6)->s6_addr32[1] ^ \
162 (ip6)->s6_addr32[2] ^ \
163 (ip6)->s6_addr32[3]) & \
164 V_tcp_hostcache.hashmask)
165
166#define THC_LOCK(lp) mtx_lock(lp)
167#define THC_UNLOCK(lp) mtx_unlock(lp)
168
169void
170tcp_hc_init(void)
171{
172 INIT_VNET_INET(curvnet);
173 int i;
174
175 /*
176 * Initialize hostcache structures.
177 */
178 V_tcp_hostcache.cache_count = 0;
179 V_tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE;
180 V_tcp_hostcache.bucket_limit = TCP_HOSTCACHE_BUCKETLIMIT;
181 V_tcp_hostcache.cache_limit =
182 V_tcp_hostcache.hashsize * V_tcp_hostcache.bucket_limit;
183 V_tcp_hostcache.expire = TCP_HOSTCACHE_EXPIRE;
184 V_tcp_hostcache.prune = TCP_HOSTCACHE_PRUNE;
185
186 TUNABLE_INT_FETCH("net.inet.tcp.hostcache.hashsize",
187 &V_tcp_hostcache.hashsize);
188 TUNABLE_INT_FETCH("net.inet.tcp.hostcache.cachelimit",
189 &V_tcp_hostcache.cache_limit);
190 TUNABLE_INT_FETCH("net.inet.tcp.hostcache.bucketlimit",
191 &V_tcp_hostcache.bucket_limit);
192 if (!powerof2(V_tcp_hostcache.hashsize)) {
193 printf("WARNING: hostcache hash size is not a power of 2.\n");
194 V_tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE; /* default */
195 }
196 V_tcp_hostcache.hashmask = V_tcp_hostcache.hashsize - 1;
197
198 /*
199 * Allocate the hash table.
200 */
201 V_tcp_hostcache.hashbase = (struct hc_head *)
202 malloc(V_tcp_hostcache.hashsize * sizeof(struct hc_head),
203 M_HOSTCACHE, M_WAITOK | M_ZERO);
204
205 /*
206 * Initialize the hash buckets.
207 */
208 for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
209 TAILQ_INIT(&V_tcp_hostcache.hashbase[i].hch_bucket);
210 V_tcp_hostcache.hashbase[i].hch_length = 0;
211 mtx_init(&V_tcp_hostcache.hashbase[i].hch_mtx, "tcp_hc_entry",
212 NULL, MTX_DEF);
213 }
214
215 /*
216 * Allocate the hostcache entries.
217 */
218 V_tcp_hostcache.zone =
219 uma_zcreate("hostcache", sizeof(struct hc_metrics),
220 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
221 uma_zone_set_max(V_tcp_hostcache.zone, V_tcp_hostcache.cache_limit);
222
223 /*
224 * Set up periodic cache cleanup.
225 */
226 callout_init(&V_tcp_hc_callout, CALLOUT_MPSAFE);
227 callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz,
228 tcp_hc_purge, 0);
229}
230
231/*
232 * Internal function: look up an entry in the hostcache or return NULL.
233 *
234 * If an entry has been returned, the caller becomes responsible for
235 * unlocking the bucket row after he is done reading/modifying the entry.
236 */
237static struct hc_metrics *
238tcp_hc_lookup(struct in_conninfo *inc)
239{
240 INIT_VNET_INET(curvnet);
241 int hash;
242 struct hc_head *hc_head;
243 struct hc_metrics *hc_entry;
244
245 KASSERT(inc != NULL, ("tcp_hc_lookup with NULL in_conninfo pointer"));
246
247 /*
248 * Hash the foreign ip address.
249 */
250 if (inc->inc_isipv6)
251 hash = HOSTCACHE_HASH6(&inc->inc6_faddr);
252 else
253 hash = HOSTCACHE_HASH(&inc->inc_faddr);
254
255 hc_head = &V_tcp_hostcache.hashbase[hash];
256
257 /*
258 * Acquire lock for this bucket row; we release the lock if we don't
259 * find an entry, otherwise the caller has to unlock after he is
260 * done.
261 */
262 THC_LOCK(&hc_head->hch_mtx);
263
264 /*
265 * Iterate through entries in bucket row looking for a match.
266 */
267 TAILQ_FOREACH(hc_entry, &hc_head->hch_bucket, rmx_q) {
268 if (inc->inc_isipv6) {
269 if (memcmp(&inc->inc6_faddr, &hc_entry->ip6,
270 sizeof(inc->inc6_faddr)) == 0)
271 return hc_entry;
272 } else {
273 if (memcmp(&inc->inc_faddr, &hc_entry->ip4,
274 sizeof(inc->inc_faddr)) == 0)
275 return hc_entry;
276 }
277 }
278
279 /*
280 * We were unsuccessful and didn't find anything.
281 */
282 THC_UNLOCK(&hc_head->hch_mtx);
283 return NULL;
284}
285
286/*
287 * Internal function: insert an entry into the hostcache or return NULL if
288 * unable to allocate a new one.
289 *
290 * If an entry has been returned, the caller becomes responsible for
291 * unlocking the bucket row after he is done reading/modifying the entry.
292 */
293static struct hc_metrics *
294tcp_hc_insert(struct in_conninfo *inc)
295{
296 INIT_VNET_INET(curvnet);
297 int hash;
298 struct hc_head *hc_head;
299 struct hc_metrics *hc_entry;
300
301 KASSERT(inc != NULL, ("tcp_hc_insert with NULL in_conninfo pointer"));
302
303 /*
304 * Hash the foreign ip address.
305 */
306 if (inc->inc_isipv6)
307 hash = HOSTCACHE_HASH6(&inc->inc6_faddr);
308 else
309 hash = HOSTCACHE_HASH(&inc->inc_faddr);
310
311 hc_head = &V_tcp_hostcache.hashbase[hash];
312
313 /*
314 * Acquire lock for this bucket row; we release the lock if we don't
315 * find an entry, otherwise the caller has to unlock after he is
316 * done.
317 */
318 THC_LOCK(&hc_head->hch_mtx);
319
320 /*
321 * If the bucket limit is reached, reuse the least-used element.
322 */
323 if (hc_head->hch_length >= V_tcp_hostcache.bucket_limit ||
324 V_tcp_hostcache.cache_count >= V_tcp_hostcache.cache_limit) {
325 hc_entry = TAILQ_LAST(&hc_head->hch_bucket, hc_qhead);
326 /*
327 * At first we were dropping the last element, just to
328 * reacquire it in the next two lines again, which isn't very
329 * efficient. Instead just reuse the least used element.
330 * We may drop something that is still "in-use" but we can be
331 * "lossy".
332 * Just give up if this bucket row is empty and we don't have
333 * anything to replace.
334 */
335 if (hc_entry == NULL) {
336 THC_UNLOCK(&hc_head->hch_mtx);
337 return NULL;
338 }
339 TAILQ_REMOVE(&hc_head->hch_bucket, hc_entry, rmx_q);
340 V_tcp_hostcache.hashbase[hash].hch_length--;
341 V_tcp_hostcache.cache_count--;
342 V_tcpstat.tcps_hc_bucketoverflow++;
343#if 0
344 uma_zfree(V_tcp_hostcache.zone, hc_entry);
345#endif
346 } else {
347 /*
348 * Allocate a new entry, or balk if not possible.
349 */
350 hc_entry = uma_zalloc(V_tcp_hostcache.zone, M_NOWAIT);
351 if (hc_entry == NULL) {
352 THC_UNLOCK(&hc_head->hch_mtx);
353 return NULL;
354 }
355 }
356
357 /*
358 * Initialize basic information of hostcache entry.
359 */
360 bzero(hc_entry, sizeof(*hc_entry));
361 if (inc->inc_isipv6)
362 bcopy(&inc->inc6_faddr, &hc_entry->ip6, sizeof(hc_entry->ip6));
363 else
364 hc_entry->ip4 = inc->inc_faddr;
365 hc_entry->rmx_head = hc_head;
366 hc_entry->rmx_expire = V_tcp_hostcache.expire;
367
368 /*
369 * Put it upfront.
370 */
371 TAILQ_INSERT_HEAD(&hc_head->hch_bucket, hc_entry, rmx_q);
372 V_tcp_hostcache.hashbase[hash].hch_length++;
373 V_tcp_hostcache.cache_count++;
374 V_tcpstat.tcps_hc_added++;
375
376 return hc_entry;
377}
378
379/*
380 * External function: look up an entry in the hostcache and fill out the
381 * supplied TCP metrics structure. Fills in NULL when no entry was found or
382 * a value is not set.
383 */
384void
385tcp_hc_get(struct in_conninfo *inc, struct hc_metrics_lite *hc_metrics_lite)
386{
387 INIT_VNET_INET(curvnet);
388 struct hc_metrics *hc_entry;
389
390 /*
391 * Find the right bucket.
392 */
393 hc_entry = tcp_hc_lookup(inc);
394
395 /*
396 * If we don't have an existing object.
397 */
398 if (hc_entry == NULL) {
399 bzero(hc_metrics_lite, sizeof(*hc_metrics_lite));
400 return;
401 }
402 hc_entry->rmx_hits++;
403 hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
404
405 hc_metrics_lite->rmx_mtu = hc_entry->rmx_mtu;
406 hc_metrics_lite->rmx_ssthresh = hc_entry->rmx_ssthresh;
407 hc_metrics_lite->rmx_rtt = hc_entry->rmx_rtt;
408 hc_metrics_lite->rmx_rttvar = hc_entry->rmx_rttvar;
409 hc_metrics_lite->rmx_bandwidth = hc_entry->rmx_bandwidth;
410 hc_metrics_lite->rmx_cwnd = hc_entry->rmx_cwnd;
411 hc_metrics_lite->rmx_sendpipe = hc_entry->rmx_sendpipe;
412 hc_metrics_lite->rmx_recvpipe = hc_entry->rmx_recvpipe;
413
414 /*
415 * Unlock bucket row.
416 */
417 THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
418}
419
420/*
421 * External function: look up an entry in the hostcache and return the
422 * discovered path MTU. Returns NULL if no entry is found or value is not
423 * set.
424 */
425u_long
426tcp_hc_getmtu(struct in_conninfo *inc)
427{
428 INIT_VNET_INET(curvnet);
429 struct hc_metrics *hc_entry;
430 u_long mtu;
431
432 hc_entry = tcp_hc_lookup(inc);
433 if (hc_entry == NULL) {
434 return 0;
435 }
436 hc_entry->rmx_hits++;
437 hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
438
439 mtu = hc_entry->rmx_mtu;
440 THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
441 return mtu;
442}
443
444/*
445 * External function: update the MTU value of an entry in the hostcache.
446 * Creates a new entry if none was found.
447 */
448void
449tcp_hc_updatemtu(struct in_conninfo *inc, u_long mtu)
450{
451 INIT_VNET_INET(curvnet);
452 struct hc_metrics *hc_entry;
453
454 /*
455 * Find the right bucket.
456 */
457 hc_entry = tcp_hc_lookup(inc);
458
459 /*
460 * If we don't have an existing object, try to insert a new one.
461 */
462 if (hc_entry == NULL) {
463 hc_entry = tcp_hc_insert(inc);
464 if (hc_entry == NULL)
465 return;
466 }
467 hc_entry->rmx_updates++;
468 hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
469
470 hc_entry->rmx_mtu = mtu;
471
472 /*
473 * Put it upfront so we find it faster next time.
474 */
475 TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
476 TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
477
478 /*
479 * Unlock bucket row.
480 */
481 THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
482}
483
484/*
485 * External function: update the TCP metrics of an entry in the hostcache.
486 * Creates a new entry if none was found.
487 */
488void
489tcp_hc_update(struct in_conninfo *inc, struct hc_metrics_lite *hcml)
490{
491 INIT_VNET_INET(curvnet);
492 struct hc_metrics *hc_entry;
493
494 hc_entry = tcp_hc_lookup(inc);
495 if (hc_entry == NULL) {
496 hc_entry = tcp_hc_insert(inc);
497 if (hc_entry == NULL)
498 return;
499 }
500 hc_entry->rmx_updates++;
501 hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
502
503 if (hcml->rmx_rtt != 0) {
504 if (hc_entry->rmx_rtt == 0)
505 hc_entry->rmx_rtt = hcml->rmx_rtt;
506 else
507 hc_entry->rmx_rtt =
508 (hc_entry->rmx_rtt + hcml->rmx_rtt) / 2;
509 V_tcpstat.tcps_cachedrtt++;
510 }
511 if (hcml->rmx_rttvar != 0) {
512 if (hc_entry->rmx_rttvar == 0)
513 hc_entry->rmx_rttvar = hcml->rmx_rttvar;
514 else
515 hc_entry->rmx_rttvar =
516 (hc_entry->rmx_rttvar + hcml->rmx_rttvar) / 2;
517 V_tcpstat.tcps_cachedrttvar++;
518 }
519 if (hcml->rmx_ssthresh != 0) {
520 if (hc_entry->rmx_ssthresh == 0)
521 hc_entry->rmx_ssthresh = hcml->rmx_ssthresh;
522 else
523 hc_entry->rmx_ssthresh =
524 (hc_entry->rmx_ssthresh + hcml->rmx_ssthresh) / 2;
525 V_tcpstat.tcps_cachedssthresh++;
526 }
527 if (hcml->rmx_bandwidth != 0) {
528 if (hc_entry->rmx_bandwidth == 0)
529 hc_entry->rmx_bandwidth = hcml->rmx_bandwidth;
530 else
531 hc_entry->rmx_bandwidth =
532 (hc_entry->rmx_bandwidth + hcml->rmx_bandwidth) / 2;
533 /* V_tcpstat.tcps_cachedbandwidth++; */
534 }
535 if (hcml->rmx_cwnd != 0) {
536 if (hc_entry->rmx_cwnd == 0)
537 hc_entry->rmx_cwnd = hcml->rmx_cwnd;
538 else
539 hc_entry->rmx_cwnd =
540 (hc_entry->rmx_cwnd + hcml->rmx_cwnd) / 2;
541 /* V_tcpstat.tcps_cachedcwnd++; */
542 }
543 if (hcml->rmx_sendpipe != 0) {
544 if (hc_entry->rmx_sendpipe == 0)
545 hc_entry->rmx_sendpipe = hcml->rmx_sendpipe;
546 else
547 hc_entry->rmx_sendpipe =
548 (hc_entry->rmx_sendpipe + hcml->rmx_sendpipe) /2;
549 /* V_tcpstat.tcps_cachedsendpipe++; */
550 }
551 if (hcml->rmx_recvpipe != 0) {
552 if (hc_entry->rmx_recvpipe == 0)
553 hc_entry->rmx_recvpipe = hcml->rmx_recvpipe;
554 else
555 hc_entry->rmx_recvpipe =
556 (hc_entry->rmx_recvpipe + hcml->rmx_recvpipe) /2;
557 /* V_tcpstat.tcps_cachedrecvpipe++; */
558 }
559
560 TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
561 TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
562 THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
563}
564
565/*
566 * Sysctl function: prints the list and values of all hostcache entries in
567 * unsorted order.
568 */
569static int
570sysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS)
571{
572 INIT_VNET_INET(curvnet);
573 int bufsize;
574 int linesize = 128;
575 char *p, *buf;
576 int len, i, error;
577 struct hc_metrics *hc_entry;
578#ifdef INET6
579 char ip6buf[INET6_ADDRSTRLEN];
580#endif
581
582 bufsize = linesize * (V_tcp_hostcache.cache_count + 1);
583
584 p = buf = (char *)malloc(bufsize, M_TEMP, M_WAITOK|M_ZERO);
585
586 len = snprintf(p, linesize,
587 "\nIP address MTU SSTRESH RTT RTTVAR BANDWIDTH "
588 " CWND SENDPIPE RECVPIPE HITS UPD EXP\n");
589 p += len;
590
591#define msec(u) (((u) + 500) / 1000)
592 for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
593 THC_LOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
594 TAILQ_FOREACH(hc_entry, &V_tcp_hostcache.hashbase[i].hch_bucket,
595 rmx_q) {
596 len = snprintf(p, linesize,
597 "%-15s %5lu %8lu %6lums %6lums %9lu %8lu %8lu %8lu "
598 "%4lu %4lu %4i\n",
599 hc_entry->ip4.s_addr ? inet_ntoa(hc_entry->ip4) :
600#ifdef INET6
601 ip6_sprintf(ip6buf, &hc_entry->ip6),
602#else
603 "IPv6?",
604#endif
605 hc_entry->rmx_mtu,
606 hc_entry->rmx_ssthresh,
607 msec(hc_entry->rmx_rtt *
608 (RTM_RTTUNIT / (hz * TCP_RTT_SCALE))),
609 msec(hc_entry->rmx_rttvar *
610 (RTM_RTTUNIT / (hz * TCP_RTT_SCALE))),
611 hc_entry->rmx_bandwidth * 8,
612 hc_entry->rmx_cwnd,
613 hc_entry->rmx_sendpipe,
614 hc_entry->rmx_recvpipe,
615 hc_entry->rmx_hits,
616 hc_entry->rmx_updates,
617 hc_entry->rmx_expire);
618 p += len;
619 }
620 THC_UNLOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
621 }
622#undef msec
623 error = SYSCTL_OUT(req, buf, p - buf);
624 free(buf, M_TEMP);
625 return(error);
626}
627
628/*
629 * Expire and purge (old|all) entries in the tcp_hostcache. Runs
630 * periodically from the callout.
631 */
632static void
633tcp_hc_purge(void *arg)
634{
635 INIT_VNET_INET(curvnet);
636 struct hc_metrics *hc_entry, *hc_next;
637 int all = (intptr_t)arg;
638 int i;
639
640 if (V_tcp_hostcache.purgeall) {
641 all = 1;
642 V_tcp_hostcache.purgeall = 0;
643 }
644
645 for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
646 THC_LOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
647 TAILQ_FOREACH_SAFE(hc_entry,
648 &V_tcp_hostcache.hashbase[i].hch_bucket, rmx_q, hc_next) {
649 if (all || hc_entry->rmx_expire <= 0) {
650 TAILQ_REMOVE(&V_tcp_hostcache.hashbase[i].hch_bucket,
651 hc_entry, rmx_q);
652 uma_zfree(V_tcp_hostcache.zone, hc_entry);
653 V_tcp_hostcache.hashbase[i].hch_length--;
654 V_tcp_hostcache.cache_count--;
655 } else
656 hc_entry->rmx_expire -= V_tcp_hostcache.prune;
657 }
658 THC_UNLOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
659 }
660
661 callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz,
662 tcp_hc_purge, arg);
663}
97#ifdef INET6
98#include <netinet6/tcp6_var.h>
99#endif
100
101#include <vm/uma.h>
102
103/* Arbitrary values */
104#define TCP_HOSTCACHE_HASHSIZE 512
105#define TCP_HOSTCACHE_BUCKETLIMIT 30
106#define TCP_HOSTCACHE_EXPIRE 60*60 /* one hour */
107#define TCP_HOSTCACHE_PRUNE 5*60 /* every 5 minutes */
108
109#ifdef VIMAGE_GLOBALS
110static struct tcp_hostcache tcp_hostcache;
111static struct callout tcp_hc_callout;
112#endif
113
114static struct hc_metrics *tcp_hc_lookup(struct in_conninfo *);
115static struct hc_metrics *tcp_hc_insert(struct in_conninfo *);
116static int sysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS);
117static void tcp_hc_purge(void *);
118
119SYSCTL_NODE(_net_inet_tcp, OID_AUTO, hostcache, CTLFLAG_RW, 0,
120 "TCP Host cache");
121
122SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_hostcache, OID_AUTO, cachelimit,
123 CTLFLAG_RDTUN, tcp_hostcache.cache_limit, 0,
124 "Overall entry limit for hostcache");
125
126SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_hostcache, OID_AUTO, hashsize,
127 CTLFLAG_RDTUN, tcp_hostcache.hashsize, 0,
128 "Size of TCP hostcache hashtable");
129
130SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_hostcache, OID_AUTO, bucketlimit,
131 CTLFLAG_RDTUN, tcp_hostcache.bucket_limit, 0,
132 "Per-bucket hash limit for hostcache");
133
134SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_hostcache, OID_AUTO, count,
135 CTLFLAG_RD, tcp_hostcache.cache_count, 0,
136 "Current number of entries in hostcache");
137
138SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_hostcache, OID_AUTO, expire,
139 CTLFLAG_RW, tcp_hostcache.expire, 0,
140 "Expire time of TCP hostcache entries");
141
142SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_hostcache, OID_AUTO, prune,
143 CTLFLAG_RW, tcp_hostcache.prune, 0, "Time between purge runs");
144
145SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_hostcache, OID_AUTO, purge,
146 CTLFLAG_RW, tcp_hostcache.purgeall, 0,
147 "Expire all entires on next purge run");
148
149SYSCTL_PROC(_net_inet_tcp_hostcache, OID_AUTO, list,
150 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_SKIP, 0, 0,
151 sysctl_tcp_hc_list, "A", "List of all hostcache entries");
152
153
154static MALLOC_DEFINE(M_HOSTCACHE, "hostcache", "TCP hostcache");
155
156#define HOSTCACHE_HASH(ip) \
157 (((ip)->s_addr ^ ((ip)->s_addr >> 7) ^ ((ip)->s_addr >> 17)) & \
158 V_tcp_hostcache.hashmask)
159
160/* XXX: What is the recommended hash to get good entropy for IPv6 addresses? */
161#define HOSTCACHE_HASH6(ip6) \
162 (((ip6)->s6_addr32[0] ^ \
163 (ip6)->s6_addr32[1] ^ \
164 (ip6)->s6_addr32[2] ^ \
165 (ip6)->s6_addr32[3]) & \
166 V_tcp_hostcache.hashmask)
167
168#define THC_LOCK(lp) mtx_lock(lp)
169#define THC_UNLOCK(lp) mtx_unlock(lp)
170
171void
172tcp_hc_init(void)
173{
174 INIT_VNET_INET(curvnet);
175 int i;
176
177 /*
178 * Initialize hostcache structures.
179 */
180 V_tcp_hostcache.cache_count = 0;
181 V_tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE;
182 V_tcp_hostcache.bucket_limit = TCP_HOSTCACHE_BUCKETLIMIT;
183 V_tcp_hostcache.cache_limit =
184 V_tcp_hostcache.hashsize * V_tcp_hostcache.bucket_limit;
185 V_tcp_hostcache.expire = TCP_HOSTCACHE_EXPIRE;
186 V_tcp_hostcache.prune = TCP_HOSTCACHE_PRUNE;
187
188 TUNABLE_INT_FETCH("net.inet.tcp.hostcache.hashsize",
189 &V_tcp_hostcache.hashsize);
190 TUNABLE_INT_FETCH("net.inet.tcp.hostcache.cachelimit",
191 &V_tcp_hostcache.cache_limit);
192 TUNABLE_INT_FETCH("net.inet.tcp.hostcache.bucketlimit",
193 &V_tcp_hostcache.bucket_limit);
194 if (!powerof2(V_tcp_hostcache.hashsize)) {
195 printf("WARNING: hostcache hash size is not a power of 2.\n");
196 V_tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE; /* default */
197 }
198 V_tcp_hostcache.hashmask = V_tcp_hostcache.hashsize - 1;
199
200 /*
201 * Allocate the hash table.
202 */
203 V_tcp_hostcache.hashbase = (struct hc_head *)
204 malloc(V_tcp_hostcache.hashsize * sizeof(struct hc_head),
205 M_HOSTCACHE, M_WAITOK | M_ZERO);
206
207 /*
208 * Initialize the hash buckets.
209 */
210 for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
211 TAILQ_INIT(&V_tcp_hostcache.hashbase[i].hch_bucket);
212 V_tcp_hostcache.hashbase[i].hch_length = 0;
213 mtx_init(&V_tcp_hostcache.hashbase[i].hch_mtx, "tcp_hc_entry",
214 NULL, MTX_DEF);
215 }
216
217 /*
218 * Allocate the hostcache entries.
219 */
220 V_tcp_hostcache.zone =
221 uma_zcreate("hostcache", sizeof(struct hc_metrics),
222 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
223 uma_zone_set_max(V_tcp_hostcache.zone, V_tcp_hostcache.cache_limit);
224
225 /*
226 * Set up periodic cache cleanup.
227 */
228 callout_init(&V_tcp_hc_callout, CALLOUT_MPSAFE);
229 callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz,
230 tcp_hc_purge, 0);
231}
232
233/*
234 * Internal function: look up an entry in the hostcache or return NULL.
235 *
236 * If an entry has been returned, the caller becomes responsible for
237 * unlocking the bucket row after he is done reading/modifying the entry.
238 */
239static struct hc_metrics *
240tcp_hc_lookup(struct in_conninfo *inc)
241{
242 INIT_VNET_INET(curvnet);
243 int hash;
244 struct hc_head *hc_head;
245 struct hc_metrics *hc_entry;
246
247 KASSERT(inc != NULL, ("tcp_hc_lookup with NULL in_conninfo pointer"));
248
249 /*
250 * Hash the foreign ip address.
251 */
252 if (inc->inc_isipv6)
253 hash = HOSTCACHE_HASH6(&inc->inc6_faddr);
254 else
255 hash = HOSTCACHE_HASH(&inc->inc_faddr);
256
257 hc_head = &V_tcp_hostcache.hashbase[hash];
258
259 /*
260 * Acquire lock for this bucket row; we release the lock if we don't
261 * find an entry, otherwise the caller has to unlock after he is
262 * done.
263 */
264 THC_LOCK(&hc_head->hch_mtx);
265
266 /*
267 * Iterate through entries in bucket row looking for a match.
268 */
269 TAILQ_FOREACH(hc_entry, &hc_head->hch_bucket, rmx_q) {
270 if (inc->inc_isipv6) {
271 if (memcmp(&inc->inc6_faddr, &hc_entry->ip6,
272 sizeof(inc->inc6_faddr)) == 0)
273 return hc_entry;
274 } else {
275 if (memcmp(&inc->inc_faddr, &hc_entry->ip4,
276 sizeof(inc->inc_faddr)) == 0)
277 return hc_entry;
278 }
279 }
280
281 /*
282 * We were unsuccessful and didn't find anything.
283 */
284 THC_UNLOCK(&hc_head->hch_mtx);
285 return NULL;
286}
287
288/*
289 * Internal function: insert an entry into the hostcache or return NULL if
290 * unable to allocate a new one.
291 *
292 * If an entry has been returned, the caller becomes responsible for
293 * unlocking the bucket row after he is done reading/modifying the entry.
294 */
295static struct hc_metrics *
296tcp_hc_insert(struct in_conninfo *inc)
297{
298 INIT_VNET_INET(curvnet);
299 int hash;
300 struct hc_head *hc_head;
301 struct hc_metrics *hc_entry;
302
303 KASSERT(inc != NULL, ("tcp_hc_insert with NULL in_conninfo pointer"));
304
305 /*
306 * Hash the foreign ip address.
307 */
308 if (inc->inc_isipv6)
309 hash = HOSTCACHE_HASH6(&inc->inc6_faddr);
310 else
311 hash = HOSTCACHE_HASH(&inc->inc_faddr);
312
313 hc_head = &V_tcp_hostcache.hashbase[hash];
314
315 /*
316 * Acquire lock for this bucket row; we release the lock if we don't
317 * find an entry, otherwise the caller has to unlock after he is
318 * done.
319 */
320 THC_LOCK(&hc_head->hch_mtx);
321
322 /*
323 * If the bucket limit is reached, reuse the least-used element.
324 */
325 if (hc_head->hch_length >= V_tcp_hostcache.bucket_limit ||
326 V_tcp_hostcache.cache_count >= V_tcp_hostcache.cache_limit) {
327 hc_entry = TAILQ_LAST(&hc_head->hch_bucket, hc_qhead);
328 /*
329 * At first we were dropping the last element, just to
330 * reacquire it in the next two lines again, which isn't very
331 * efficient. Instead just reuse the least used element.
332 * We may drop something that is still "in-use" but we can be
333 * "lossy".
334 * Just give up if this bucket row is empty and we don't have
335 * anything to replace.
336 */
337 if (hc_entry == NULL) {
338 THC_UNLOCK(&hc_head->hch_mtx);
339 return NULL;
340 }
341 TAILQ_REMOVE(&hc_head->hch_bucket, hc_entry, rmx_q);
342 V_tcp_hostcache.hashbase[hash].hch_length--;
343 V_tcp_hostcache.cache_count--;
344 V_tcpstat.tcps_hc_bucketoverflow++;
345#if 0
346 uma_zfree(V_tcp_hostcache.zone, hc_entry);
347#endif
348 } else {
349 /*
350 * Allocate a new entry, or balk if not possible.
351 */
352 hc_entry = uma_zalloc(V_tcp_hostcache.zone, M_NOWAIT);
353 if (hc_entry == NULL) {
354 THC_UNLOCK(&hc_head->hch_mtx);
355 return NULL;
356 }
357 }
358
359 /*
360 * Initialize basic information of hostcache entry.
361 */
362 bzero(hc_entry, sizeof(*hc_entry));
363 if (inc->inc_isipv6)
364 bcopy(&inc->inc6_faddr, &hc_entry->ip6, sizeof(hc_entry->ip6));
365 else
366 hc_entry->ip4 = inc->inc_faddr;
367 hc_entry->rmx_head = hc_head;
368 hc_entry->rmx_expire = V_tcp_hostcache.expire;
369
370 /*
371 * Put it upfront.
372 */
373 TAILQ_INSERT_HEAD(&hc_head->hch_bucket, hc_entry, rmx_q);
374 V_tcp_hostcache.hashbase[hash].hch_length++;
375 V_tcp_hostcache.cache_count++;
376 V_tcpstat.tcps_hc_added++;
377
378 return hc_entry;
379}
380
381/*
382 * External function: look up an entry in the hostcache and fill out the
383 * supplied TCP metrics structure. Fills in NULL when no entry was found or
384 * a value is not set.
385 */
386void
387tcp_hc_get(struct in_conninfo *inc, struct hc_metrics_lite *hc_metrics_lite)
388{
389 INIT_VNET_INET(curvnet);
390 struct hc_metrics *hc_entry;
391
392 /*
393 * Find the right bucket.
394 */
395 hc_entry = tcp_hc_lookup(inc);
396
397 /*
398 * If we don't have an existing object.
399 */
400 if (hc_entry == NULL) {
401 bzero(hc_metrics_lite, sizeof(*hc_metrics_lite));
402 return;
403 }
404 hc_entry->rmx_hits++;
405 hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
406
407 hc_metrics_lite->rmx_mtu = hc_entry->rmx_mtu;
408 hc_metrics_lite->rmx_ssthresh = hc_entry->rmx_ssthresh;
409 hc_metrics_lite->rmx_rtt = hc_entry->rmx_rtt;
410 hc_metrics_lite->rmx_rttvar = hc_entry->rmx_rttvar;
411 hc_metrics_lite->rmx_bandwidth = hc_entry->rmx_bandwidth;
412 hc_metrics_lite->rmx_cwnd = hc_entry->rmx_cwnd;
413 hc_metrics_lite->rmx_sendpipe = hc_entry->rmx_sendpipe;
414 hc_metrics_lite->rmx_recvpipe = hc_entry->rmx_recvpipe;
415
416 /*
417 * Unlock bucket row.
418 */
419 THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
420}
421
422/*
423 * External function: look up an entry in the hostcache and return the
424 * discovered path MTU. Returns NULL if no entry is found or value is not
425 * set.
426 */
427u_long
428tcp_hc_getmtu(struct in_conninfo *inc)
429{
430 INIT_VNET_INET(curvnet);
431 struct hc_metrics *hc_entry;
432 u_long mtu;
433
434 hc_entry = tcp_hc_lookup(inc);
435 if (hc_entry == NULL) {
436 return 0;
437 }
438 hc_entry->rmx_hits++;
439 hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
440
441 mtu = hc_entry->rmx_mtu;
442 THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
443 return mtu;
444}
445
446/*
447 * External function: update the MTU value of an entry in the hostcache.
448 * Creates a new entry if none was found.
449 */
450void
451tcp_hc_updatemtu(struct in_conninfo *inc, u_long mtu)
452{
453 INIT_VNET_INET(curvnet);
454 struct hc_metrics *hc_entry;
455
456 /*
457 * Find the right bucket.
458 */
459 hc_entry = tcp_hc_lookup(inc);
460
461 /*
462 * If we don't have an existing object, try to insert a new one.
463 */
464 if (hc_entry == NULL) {
465 hc_entry = tcp_hc_insert(inc);
466 if (hc_entry == NULL)
467 return;
468 }
469 hc_entry->rmx_updates++;
470 hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
471
472 hc_entry->rmx_mtu = mtu;
473
474 /*
475 * Put it upfront so we find it faster next time.
476 */
477 TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
478 TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
479
480 /*
481 * Unlock bucket row.
482 */
483 THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
484}
485
486/*
487 * External function: update the TCP metrics of an entry in the hostcache.
488 * Creates a new entry if none was found.
489 */
490void
491tcp_hc_update(struct in_conninfo *inc, struct hc_metrics_lite *hcml)
492{
493 INIT_VNET_INET(curvnet);
494 struct hc_metrics *hc_entry;
495
496 hc_entry = tcp_hc_lookup(inc);
497 if (hc_entry == NULL) {
498 hc_entry = tcp_hc_insert(inc);
499 if (hc_entry == NULL)
500 return;
501 }
502 hc_entry->rmx_updates++;
503 hc_entry->rmx_expire = V_tcp_hostcache.expire; /* start over again */
504
505 if (hcml->rmx_rtt != 0) {
506 if (hc_entry->rmx_rtt == 0)
507 hc_entry->rmx_rtt = hcml->rmx_rtt;
508 else
509 hc_entry->rmx_rtt =
510 (hc_entry->rmx_rtt + hcml->rmx_rtt) / 2;
511 V_tcpstat.tcps_cachedrtt++;
512 }
513 if (hcml->rmx_rttvar != 0) {
514 if (hc_entry->rmx_rttvar == 0)
515 hc_entry->rmx_rttvar = hcml->rmx_rttvar;
516 else
517 hc_entry->rmx_rttvar =
518 (hc_entry->rmx_rttvar + hcml->rmx_rttvar) / 2;
519 V_tcpstat.tcps_cachedrttvar++;
520 }
521 if (hcml->rmx_ssthresh != 0) {
522 if (hc_entry->rmx_ssthresh == 0)
523 hc_entry->rmx_ssthresh = hcml->rmx_ssthresh;
524 else
525 hc_entry->rmx_ssthresh =
526 (hc_entry->rmx_ssthresh + hcml->rmx_ssthresh) / 2;
527 V_tcpstat.tcps_cachedssthresh++;
528 }
529 if (hcml->rmx_bandwidth != 0) {
530 if (hc_entry->rmx_bandwidth == 0)
531 hc_entry->rmx_bandwidth = hcml->rmx_bandwidth;
532 else
533 hc_entry->rmx_bandwidth =
534 (hc_entry->rmx_bandwidth + hcml->rmx_bandwidth) / 2;
535 /* V_tcpstat.tcps_cachedbandwidth++; */
536 }
537 if (hcml->rmx_cwnd != 0) {
538 if (hc_entry->rmx_cwnd == 0)
539 hc_entry->rmx_cwnd = hcml->rmx_cwnd;
540 else
541 hc_entry->rmx_cwnd =
542 (hc_entry->rmx_cwnd + hcml->rmx_cwnd) / 2;
543 /* V_tcpstat.tcps_cachedcwnd++; */
544 }
545 if (hcml->rmx_sendpipe != 0) {
546 if (hc_entry->rmx_sendpipe == 0)
547 hc_entry->rmx_sendpipe = hcml->rmx_sendpipe;
548 else
549 hc_entry->rmx_sendpipe =
550 (hc_entry->rmx_sendpipe + hcml->rmx_sendpipe) /2;
551 /* V_tcpstat.tcps_cachedsendpipe++; */
552 }
553 if (hcml->rmx_recvpipe != 0) {
554 if (hc_entry->rmx_recvpipe == 0)
555 hc_entry->rmx_recvpipe = hcml->rmx_recvpipe;
556 else
557 hc_entry->rmx_recvpipe =
558 (hc_entry->rmx_recvpipe + hcml->rmx_recvpipe) /2;
559 /* V_tcpstat.tcps_cachedrecvpipe++; */
560 }
561
562 TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
563 TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
564 THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
565}
566
567/*
568 * Sysctl function: prints the list and values of all hostcache entries in
569 * unsorted order.
570 */
571static int
572sysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS)
573{
574 INIT_VNET_INET(curvnet);
575 int bufsize;
576 int linesize = 128;
577 char *p, *buf;
578 int len, i, error;
579 struct hc_metrics *hc_entry;
580#ifdef INET6
581 char ip6buf[INET6_ADDRSTRLEN];
582#endif
583
584 bufsize = linesize * (V_tcp_hostcache.cache_count + 1);
585
586 p = buf = (char *)malloc(bufsize, M_TEMP, M_WAITOK|M_ZERO);
587
588 len = snprintf(p, linesize,
589 "\nIP address MTU SSTRESH RTT RTTVAR BANDWIDTH "
590 " CWND SENDPIPE RECVPIPE HITS UPD EXP\n");
591 p += len;
592
593#define msec(u) (((u) + 500) / 1000)
594 for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
595 THC_LOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
596 TAILQ_FOREACH(hc_entry, &V_tcp_hostcache.hashbase[i].hch_bucket,
597 rmx_q) {
598 len = snprintf(p, linesize,
599 "%-15s %5lu %8lu %6lums %6lums %9lu %8lu %8lu %8lu "
600 "%4lu %4lu %4i\n",
601 hc_entry->ip4.s_addr ? inet_ntoa(hc_entry->ip4) :
602#ifdef INET6
603 ip6_sprintf(ip6buf, &hc_entry->ip6),
604#else
605 "IPv6?",
606#endif
607 hc_entry->rmx_mtu,
608 hc_entry->rmx_ssthresh,
609 msec(hc_entry->rmx_rtt *
610 (RTM_RTTUNIT / (hz * TCP_RTT_SCALE))),
611 msec(hc_entry->rmx_rttvar *
612 (RTM_RTTUNIT / (hz * TCP_RTT_SCALE))),
613 hc_entry->rmx_bandwidth * 8,
614 hc_entry->rmx_cwnd,
615 hc_entry->rmx_sendpipe,
616 hc_entry->rmx_recvpipe,
617 hc_entry->rmx_hits,
618 hc_entry->rmx_updates,
619 hc_entry->rmx_expire);
620 p += len;
621 }
622 THC_UNLOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
623 }
624#undef msec
625 error = SYSCTL_OUT(req, buf, p - buf);
626 free(buf, M_TEMP);
627 return(error);
628}
629
630/*
631 * Expire and purge (old|all) entries in the tcp_hostcache. Runs
632 * periodically from the callout.
633 */
634static void
635tcp_hc_purge(void *arg)
636{
637 INIT_VNET_INET(curvnet);
638 struct hc_metrics *hc_entry, *hc_next;
639 int all = (intptr_t)arg;
640 int i;
641
642 if (V_tcp_hostcache.purgeall) {
643 all = 1;
644 V_tcp_hostcache.purgeall = 0;
645 }
646
647 for (i = 0; i < V_tcp_hostcache.hashsize; i++) {
648 THC_LOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
649 TAILQ_FOREACH_SAFE(hc_entry,
650 &V_tcp_hostcache.hashbase[i].hch_bucket, rmx_q, hc_next) {
651 if (all || hc_entry->rmx_expire <= 0) {
652 TAILQ_REMOVE(&V_tcp_hostcache.hashbase[i].hch_bucket,
653 hc_entry, rmx_q);
654 uma_zfree(V_tcp_hostcache.zone, hc_entry);
655 V_tcp_hostcache.hashbase[i].hch_length--;
656 V_tcp_hostcache.cache_count--;
657 } else
658 hc_entry->rmx_expire -= V_tcp_hostcache.prune;
659 }
660 THC_UNLOCK(&V_tcp_hostcache.hashbase[i].hch_mtx);
661 }
662
663 callout_reset(&V_tcp_hc_callout, V_tcp_hostcache.prune * hz,
664 tcp_hc_purge, arg);
665}