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 * $FreeBSD: src/sys/netinet/tcp_hostcache.c,v 1.7.2.2 2005/02/13 18:18:33 rwatson Exp $
30 */
31
32 /*
33 * The tcp_hostcache moves the tcp specific cached metrics from the routing
34 * table into a dedicated structure indexed by the remote IP address. It
35 * keeps information on the measured tcp parameters of past tcp sessions
36 * to have better initial start values for following connections from the
37 * same source. Depending on the network parameters (delay, bandwidth, max
38 * MTU, congestion window) between local and remote site this can lead to
39 * significant speedups for new tcp connections after the first one.
40 *
41 * Due to this new tcp_hostcache all tcp specific metrics information in
42 * the routing table has been removed. The INPCB no longer keeps a pointer
43 * to the routing entry and protocol initiated route cloning has been
44 * removed as well. With these changes the routing table has gone back
45 * to being more lightwight and only carries information related to packet
46 * forwarding.
47 *
48 * Tcp_hostcache is designed for multiple concurrent access in SMP
49 * environments and high contention. All bucket rows have their own
50 * lock and thus multiple lookups and modifies can be done at the same
51 * time as long as they are in different bucket rows. If a request for
52 * insertion of a new record can't be satisfied it simply returns an
53 * empty structure. Nobody and nothing shall ever point directly to
54 * any entry in tcp_hostcache. All communication is done in an object
55 * oriented way and only funtions of tcp_hostcache will manipulate hostcache
56 * entries. Otherwise we are unable to achieve good behaviour in concurrent
57 * access situations. Since tcp_hostcache is only caching information there
58 * are no fatal consequences if we either can't satisfy any particular request
59 * or have to drop/overwrite an existing entry because of bucket limit
60 * memory constrains.
61 */
62
63 /*
64 * Many thanks to jlemon for basic structure of tcp_syncache which is being
65 * followed here.
66 */
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
80 #include <net/if.h>
81
82 #include <netinet/in.h>
83 #include <netinet/in_systm.h>
84 #include <netinet/ip.h>
85 #include <netinet/in_var.h>
86 #include <netinet/in_pcb.h>
87 #include <netinet/ip_var.h>
88 #ifdef INET6
89 #include <netinet/ip6.h>
90 #include <netinet6/ip6_var.h>
91 #endif
92 #include <netinet/tcp.h>
93 #include <netinet/tcp_var.h>
94 #ifdef INET6
95 #include <netinet6/tcp6_var.h>
96 #endif
97
98 #include <vm/uma.h>
99
100
101 TAILQ_HEAD(hc_qhead, hc_metrics);
102
103 struct hc_head {
104 struct hc_qhead hch_bucket;
105 u_int hch_length;
106 struct mtx hch_mtx;
107 };
108
109 struct hc_metrics {
110 /* housekeeping */
111 TAILQ_ENTRY(hc_metrics) rmx_q;
112 struct hc_head *rmx_head; /* head of bucket tail queue */
113 struct in_addr ip4; /* IP address */
114 struct in6_addr ip6; /* IP6 address */
115 /* endpoint specific values for tcp */
116 u_long rmx_mtu; /* MTU for this path */
117 u_long rmx_ssthresh; /* outbound gateway buffer limit */
118 u_long rmx_rtt; /* estimated round trip time */
119 u_long rmx_rttvar; /* estimated rtt variance */
120 u_long rmx_bandwidth; /* estimated bandwidth */
121 u_long rmx_cwnd; /* congestion window */
122 u_long rmx_sendpipe; /* outbound delay-bandwidth product */
123 u_long rmx_recvpipe; /* inbound delay-bandwidth product */
124 struct rmxp_tao rmx_tao; /* TAO cache for T/TCP */
125 /* tcp hostcache internal data */
126 int rmx_expire; /* lifetime for object */
127 u_long rmx_hits; /* number of hits */
128 u_long rmx_updates; /* number of updates */
129 };
130
131 /* Arbitrary values */
132 #define TCP_HOSTCACHE_HASHSIZE 512
133 #define TCP_HOSTCACHE_BUCKETLIMIT 30
134 #define TCP_HOSTCACHE_EXPIRE 60*60 /* one hour */
135 #define TCP_HOSTCACHE_PRUNE 5*60 /* every 5 minutes */
136
137 struct tcp_hostcache {
138 struct hc_head *hashbase;
139 uma_zone_t zone;
140 u_int hashsize;
141 u_int hashmask;
142 u_int bucket_limit;
143 u_int cache_count;
144 u_int cache_limit;
145 int expire;
146 int purgeall;
147 };
148 static struct tcp_hostcache tcp_hostcache;
149
150 static struct callout tcp_hc_callout;
151
152 static struct hc_metrics *tcp_hc_lookup(struct in_conninfo *);
153 static struct hc_metrics *tcp_hc_insert(struct in_conninfo *);
154 static int sysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS);
155 static void tcp_hc_purge(void *);
156
157 SYSCTL_NODE(_net_inet_tcp, OID_AUTO, hostcache, CTLFLAG_RW, 0, "TCP Host cache");
158
159 SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, cachelimit, CTLFLAG_RDTUN,
160 &tcp_hostcache.cache_limit, 0, "Overall entry limit for hostcache");
161
162 SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, hashsize, CTLFLAG_RDTUN,
163 &tcp_hostcache.hashsize, 0, "Size of TCP hostcache hashtable");
164
165 SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, bucketlimit, CTLFLAG_RDTUN,
166 &tcp_hostcache.bucket_limit, 0, "Per-bucket hash limit for hostcache");
167
168 SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, count, CTLFLAG_RD,
169 &tcp_hostcache.cache_count, 0, "Current number of entries in hostcache");
170
171 SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, expire, CTLFLAG_RW,
172 &tcp_hostcache.expire, 0, "Expire time of TCP hostcache entries");
173
174 SYSCTL_INT(_net_inet_tcp_hostcache, OID_AUTO, purge, CTLFLAG_RW,
175 &tcp_hostcache.purgeall, 0, "Expire all entires on next purge run");
176
177 SYSCTL_PROC(_net_inet_tcp_hostcache, OID_AUTO, list,
178 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_SKIP, 0, 0,
179 sysctl_tcp_hc_list, "A", "List of all hostcache entries");
180
181
182 static MALLOC_DEFINE(M_HOSTCACHE, "hostcache", "TCP hostcache");
183
184 #define HOSTCACHE_HASH(ip) \
185 (((ip)->s_addr ^ ((ip)->s_addr >> 7) ^ ((ip)->s_addr >> 17)) & \
186 tcp_hostcache.hashmask)
187
188 /* XXX: What is the recommended hash to get good entropy for IPv6 addresses? */
189 #define HOSTCACHE_HASH6(ip6) \
190 (((ip6)->s6_addr32[0] ^ \
191 (ip6)->s6_addr32[1] ^ \
192 (ip6)->s6_addr32[2] ^ \
193 (ip6)->s6_addr32[3]) & \
194 tcp_hostcache.hashmask)
195
196 #define THC_LOCK(lp) mtx_lock(lp)
197 #define THC_UNLOCK(lp) mtx_unlock(lp)
198
199 void
200 tcp_hc_init(void)
201 {
202 int i;
203
204 /*
205 * Initialize hostcache structures
206 */
207 tcp_hostcache.cache_count = 0;
208 tcp_hostcache.hashsize = TCP_HOSTCACHE_HASHSIZE;
209 tcp_hostcache.bucket_limit = TCP_HOSTCACHE_BUCKETLIMIT;
210 tcp_hostcache.cache_limit =
211 tcp_hostcache.hashsize * tcp_hostcache.bucket_limit;
212 tcp_hostcache.expire = TCP_HOSTCACHE_EXPIRE;
213
214 TUNABLE_INT_FETCH("net.inet.tcp.hostcache.hashsize",
215 &tcp_hostcache.hashsize);
216 TUNABLE_INT_FETCH("net.inet.tcp.hostcache.cachelimit",
217 &tcp_hostcache.cache_limit);
218 TUNABLE_INT_FETCH("net.inet.tcp.hostcache.bucketlimit",
219 &tcp_hostcache.bucket_limit);
220 if (!powerof2(tcp_hostcache.hashsize)) {
221 printf("WARNING: hostcache hash size is not a power of 2.\n");
222 tcp_hostcache.hashsize = 512; /* safe default */
223 }
224 tcp_hostcache.hashmask = tcp_hostcache.hashsize - 1;
225
226 /*
227 * Allocate the hash table
228 */
229 tcp_hostcache.hashbase = (struct hc_head *)
230 malloc(tcp_hostcache.hashsize * sizeof(struct hc_head),
231 M_HOSTCACHE, M_WAITOK | M_ZERO);
232
233 /*
234 * Initialize the hash buckets
235 */
236 for (i = 0; i < tcp_hostcache.hashsize; i++) {
237 TAILQ_INIT(&tcp_hostcache.hashbase[i].hch_bucket);
238 tcp_hostcache.hashbase[i].hch_length = 0;
239 mtx_init(&tcp_hostcache.hashbase[i].hch_mtx, "tcp_hc_entry",
240 NULL, MTX_DEF);
241 }
242
243 /*
244 * Allocate the hostcache entries.
245 */
246 tcp_hostcache.zone = uma_zcreate("hostcache", sizeof(struct hc_metrics),
247 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
248 uma_zone_set_max(tcp_hostcache.zone, tcp_hostcache.cache_limit);
249
250 /*
251 * Set up periodic cache cleanup.
252 */
253 callout_init(&tcp_hc_callout, CALLOUT_MPSAFE);
254 callout_reset(&tcp_hc_callout, TCP_HOSTCACHE_PRUNE * hz, tcp_hc_purge, 0);
255 }
256
257 /*
258 * Internal function: lookup an entry in the hostcache or return NULL.
259 *
260 * If an entry has been returned, the caller becomes responsible for
261 * unlocking the bucket row after he is done reading/modifying the entry.
262 */
263 static struct hc_metrics *
264 tcp_hc_lookup(struct in_conninfo *inc)
265 {
266 int hash;
267 struct hc_head *hc_head;
268 struct hc_metrics *hc_entry;
269
270 KASSERT(inc != NULL, ("tcp_hc_lookup with NULL in_conninfo pointer"));
271
272 /*
273 * Hash the foreign ip address.
274 */
275 if (inc->inc_isipv6)
276 hash = HOSTCACHE_HASH6(&inc->inc6_faddr);
277 else
278 hash = HOSTCACHE_HASH(&inc->inc_faddr);
279
280 hc_head = &tcp_hostcache.hashbase[hash];
281
282 /*
283 * aquire lock for this bucket row
284 * we release the lock if we don't find an entry,
285 * otherwise the caller has to unlock after he is done
286 */
287 THC_LOCK(&hc_head->hch_mtx);
288
289 /*
290 * circle through entries in bucket row looking for a match
291 */
292 TAILQ_FOREACH(hc_entry, &hc_head->hch_bucket, rmx_q) {
293 if (inc->inc_isipv6) {
294 if (memcmp(&inc->inc6_faddr, &hc_entry->ip6,
295 sizeof(inc->inc6_faddr)) == 0)
296 return hc_entry;
297 } else {
298 if (memcmp(&inc->inc_faddr, &hc_entry->ip4,
299 sizeof(inc->inc_faddr)) == 0)
300 return hc_entry;
301 }
302 }
303
304 /*
305 * We were unsuccessful and didn't find anything
306 */
307 THC_UNLOCK(&hc_head->hch_mtx);
308 return NULL;
309 }
310
311 /*
312 * Internal function: insert an entry into the hostcache or return NULL
313 * if unable to allocate a new one.
314 *
315 * If an entry has been returned, the caller becomes responsible for
316 * unlocking the bucket row after he is done reading/modifying the entry.
317 */
318 static struct hc_metrics *
319 tcp_hc_insert(struct in_conninfo *inc)
320 {
321 int hash;
322 struct hc_head *hc_head;
323 struct hc_metrics *hc_entry;
324
325 KASSERT(inc != NULL, ("tcp_hc_insert with NULL in_conninfo pointer"));
326
327 /*
328 * Hash the foreign ip address
329 */
330 if (inc->inc_isipv6)
331 hash = HOSTCACHE_HASH6(&inc->inc6_faddr);
332 else
333 hash = HOSTCACHE_HASH(&inc->inc_faddr);
334
335 hc_head = &tcp_hostcache.hashbase[hash];
336
337 /*
338 * aquire lock for this bucket row
339 * we release the lock if we don't find an entry,
340 * otherwise the caller has to unlock after he is done
341 */
342 THC_LOCK(&hc_head->hch_mtx);
343
344 /*
345 * If the bucket limit is reached reuse the least used element
346 */
347 if (hc_head->hch_length >= tcp_hostcache.bucket_limit ||
348 tcp_hostcache.cache_count >= tcp_hostcache.cache_limit) {
349 hc_entry = TAILQ_LAST(&hc_head->hch_bucket, hc_qhead);
350 /*
351 * At first we were dropping the last element, just to
352 * reaquire it in the next two lines again which ain't
353 * very efficient. Instead just reuse the least used element.
354 * Maybe we drop something that is still "in-use" but we can
355 * be "lossy".
356 */
357 TAILQ_REMOVE(&hc_head->hch_bucket, hc_entry, rmx_q);
358 tcp_hostcache.hashbase[hash].hch_length--;
359 tcp_hostcache.cache_count--;
360 tcpstat.tcps_hc_bucketoverflow++;
361 #if 0
362 uma_zfree(tcp_hostcache.zone, hc_entry);
363 #endif
364 } else {
365 /*
366 * Allocate a new entry, or balk if not possible
367 */
368 hc_entry = uma_zalloc(tcp_hostcache.zone, M_NOWAIT);
369 if (hc_entry == NULL) {
370 THC_UNLOCK(&hc_head->hch_mtx);
371 return NULL;
372 }
373 }
374
375 /*
376 * Initialize basic information of hostcache entry
377 */
378 bzero(hc_entry, sizeof(*hc_entry));
379 if (inc->inc_isipv6)
380 bcopy(&inc->inc6_faddr, &hc_entry->ip6, sizeof(hc_entry->ip6));
381 else
382 hc_entry->ip4 = inc->inc_faddr;
383 hc_entry->rmx_head = hc_head;
384 hc_entry->rmx_expire = tcp_hostcache.expire;
385
386 /*
387 * Put it upfront
388 */
389 TAILQ_INSERT_HEAD(&hc_head->hch_bucket, hc_entry, rmx_q);
390 tcp_hostcache.hashbase[hash].hch_length++;
391 tcp_hostcache.cache_count++;
392 tcpstat.tcps_hc_added++;
393
394 return hc_entry;
395 }
396
397 /*
398 * External function: lookup an entry in the hostcache and fill out the
399 * supplied tcp metrics structure. Fills in null when no entry was found
400 * or a value is not set.
401 */
402 void
403 tcp_hc_get(struct in_conninfo *inc, struct hc_metrics_lite *hc_metrics_lite)
404 {
405 struct hc_metrics *hc_entry;
406
407 /*
408 * Find the right bucket
409 */
410 hc_entry = tcp_hc_lookup(inc);
411
412 /*
413 * If we don't have an existing object
414 */
415 if (hc_entry == NULL) {
416 bzero(hc_metrics_lite, sizeof(*hc_metrics_lite));
417 return;
418 }
419 hc_entry->rmx_hits++;
420 hc_entry->rmx_expire = tcp_hostcache.expire; /* start over again */
421
422 hc_metrics_lite->rmx_mtu = hc_entry->rmx_mtu;
423 hc_metrics_lite->rmx_ssthresh = hc_entry->rmx_ssthresh;
424 hc_metrics_lite->rmx_rtt = hc_entry->rmx_rtt;
425 hc_metrics_lite->rmx_rttvar = hc_entry->rmx_rttvar;
426 hc_metrics_lite->rmx_bandwidth = hc_entry->rmx_bandwidth;
427 hc_metrics_lite->rmx_cwnd = hc_entry->rmx_cwnd;
428 hc_metrics_lite->rmx_sendpipe = hc_entry->rmx_sendpipe;
429 hc_metrics_lite->rmx_recvpipe = hc_entry->rmx_recvpipe;
430
431 /*
432 * unlock bucket row
433 */
434 THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
435 }
436
437 /*
438 * External function: lookup an entry in the hostcache and return the
439 * discovered path mtu. Returns null if no entry is found or value is not
440 * set.
441 */
442 u_long
443 tcp_hc_getmtu(struct in_conninfo *inc)
444 {
445 struct hc_metrics *hc_entry;
446 u_long mtu;
447
448 hc_entry = tcp_hc_lookup(inc);
449 if (hc_entry == NULL) {
450 return 0;
451 }
452 hc_entry->rmx_hits++;
453 hc_entry->rmx_expire = tcp_hostcache.expire; /* start over again */
454
455 mtu = hc_entry->rmx_mtu;
456 THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
457 return mtu;
458 }
459
460 /*
461 * External function: lookup an entry in the hostcache and fill out the
462 * supplied t/tcp tao structure. Fills in null when no entry was found
463 * or a value is not set.
464 */
465 void
466 tcp_hc_gettao(struct in_conninfo *inc, struct rmxp_tao *tao)
467 {
468 struct hc_metrics *hc_entry;
469
470 hc_entry = tcp_hc_lookup(inc);
471 if (hc_entry == NULL) {
472 bzero(tao, sizeof(*tao));
473 return;
474 }
475 hc_entry->rmx_hits++;
476 hc_entry->rmx_expire = tcp_hostcache.expire; /* start over again */
477
478 bcopy(&hc_entry->rmx_tao, tao, sizeof(*tao));
479 THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
480 }
481
482 /*
483 * External function: update the mtu value of an entry in the hostcache.
484 * Creates a new entry if none was found.
485 */
486 void
487 tcp_hc_updatemtu(struct in_conninfo *inc, u_long mtu)
488 {
489 struct hc_metrics *hc_entry;
490
491 /*
492 * Find the right bucket
493 */
494 hc_entry = tcp_hc_lookup(inc);
495
496 /*
497 * If we don't have an existing object try to insert a new one
498 */
499 if (hc_entry == NULL) {
500 hc_entry = tcp_hc_insert(inc);
501 if (hc_entry == NULL)
502 return;
503 }
504 hc_entry->rmx_updates++;
505 hc_entry->rmx_expire = tcp_hostcache.expire; /* start over again */
506
507 hc_entry->rmx_mtu = mtu;
508
509 /*
510 * put it upfront so we find it faster next time
511 */
512 TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
513 TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
514
515 /*
516 * unlock bucket row
517 */
518 THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
519 }
520
521 /*
522 * External function: update the tcp metrics of an entry in the hostcache.
523 * Creates a new entry if none was found.
524 */
525 void
526 tcp_hc_update(struct in_conninfo *inc, struct hc_metrics_lite *hcml)
527 {
528 struct hc_metrics *hc_entry;
529
530 hc_entry = tcp_hc_lookup(inc);
531 if (hc_entry == NULL) {
532 hc_entry = tcp_hc_insert(inc);
533 if (hc_entry == NULL)
534 return;
535 }
536 hc_entry->rmx_updates++;
537 hc_entry->rmx_expire = tcp_hostcache.expire; /* start over again */
538
539 if (hcml->rmx_rtt != 0) {
540 if (hc_entry->rmx_rtt == 0)
541 hc_entry->rmx_rtt = hcml->rmx_rtt;
542 else
543 hc_entry->rmx_rtt =
544 (hc_entry->rmx_rtt + hcml->rmx_rtt) / 2;
545 tcpstat.tcps_cachedrtt++;
546 }
547 if (hcml->rmx_rttvar != 0) {
548 if (hc_entry->rmx_rttvar == 0)
549 hc_entry->rmx_rttvar = hcml->rmx_rttvar;
550 else
551 hc_entry->rmx_rttvar =
552 (hc_entry->rmx_rttvar + hcml->rmx_rttvar) / 2;
553 tcpstat.tcps_cachedrttvar++;
554 }
555 if (hcml->rmx_ssthresh != 0) {
556 if (hc_entry->rmx_ssthresh == 0)
557 hc_entry->rmx_ssthresh = hcml->rmx_ssthresh;
558 else
559 hc_entry->rmx_ssthresh =
560 (hc_entry->rmx_ssthresh + hcml->rmx_ssthresh) / 2;
561 tcpstat.tcps_cachedssthresh++;
562 }
563 if (hcml->rmx_bandwidth != 0) {
564 if (hc_entry->rmx_bandwidth == 0)
565 hc_entry->rmx_bandwidth = hcml->rmx_bandwidth;
566 else
567 hc_entry->rmx_bandwidth =
568 (hc_entry->rmx_bandwidth + hcml->rmx_bandwidth) / 2;
569 /* tcpstat.tcps_cachedbandwidth++; */
570 }
571 if (hcml->rmx_cwnd != 0) {
572 if (hc_entry->rmx_cwnd == 0)
573 hc_entry->rmx_cwnd = hcml->rmx_cwnd;
574 else
575 hc_entry->rmx_cwnd =
576 (hc_entry->rmx_cwnd + hcml->rmx_cwnd) / 2;
577 /* tcpstat.tcps_cachedcwnd++; */
578 }
579 if (hcml->rmx_sendpipe != 0) {
580 if (hc_entry->rmx_sendpipe == 0)
581 hc_entry->rmx_sendpipe = hcml->rmx_sendpipe;
582 else
583 hc_entry->rmx_sendpipe =
584 (hc_entry->rmx_sendpipe + hcml->rmx_sendpipe) /2;
585 /* tcpstat.tcps_cachedsendpipe++; */
586 }
587 if (hcml->rmx_recvpipe != 0) {
588 if (hc_entry->rmx_recvpipe == 0)
589 hc_entry->rmx_recvpipe = hcml->rmx_recvpipe;
590 else
591 hc_entry->rmx_recvpipe =
592 (hc_entry->rmx_recvpipe + hcml->rmx_recvpipe) /2;
593 /* tcpstat.tcps_cachedrecvpipe++; */
594 }
595
596 TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
597 TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
598 THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
599 }
600
601 /*
602 * External function: update the t/tcp tao of an entry in the hostcache.
603 * Creates a new entry if none was found.
604 */
605 void
606 tcp_hc_updatetao(struct in_conninfo *inc, int field, tcp_cc ccount, u_short mss)
607 {
608 struct hc_metrics *hc_entry;
609
610 hc_entry = tcp_hc_lookup(inc);
611 if (hc_entry == NULL) {
612 hc_entry = tcp_hc_insert(inc);
613 if (hc_entry == NULL)
614 return;
615 }
616 hc_entry->rmx_updates++;
617 hc_entry->rmx_expire = tcp_hostcache.expire; /* start over again */
618
619 switch(field) {
620 case TCP_HC_TAO_CC:
621 hc_entry->rmx_tao.tao_cc = ccount;
622 break;
623
624 case TCP_HC_TAO_CCSENT:
625 hc_entry->rmx_tao.tao_ccsent = ccount;
626 break;
627
628 case TCP_HC_TAO_MSSOPT:
629 hc_entry->rmx_tao.tao_mssopt = mss;
630 break;
631 }
632
633 TAILQ_REMOVE(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
634 TAILQ_INSERT_HEAD(&hc_entry->rmx_head->hch_bucket, hc_entry, rmx_q);
635 THC_UNLOCK(&hc_entry->rmx_head->hch_mtx);
636 }
637
638 /*
639 * Sysctl function: prints the list and values of all hostcache entries in
640 * unsorted order.
641 */
642 static int
643 sysctl_tcp_hc_list(SYSCTL_HANDLER_ARGS)
644 {
645 int bufsize;
646 int linesize = 128;
647 char *p, *buf;
648 int len, i, error;
649 struct hc_metrics *hc_entry;
650
651 bufsize = linesize * (tcp_hostcache.cache_count + 1);
652
653 p = buf = (char *)malloc(bufsize, M_TEMP, M_WAITOK|M_ZERO);
654
655 len = snprintf(p, linesize,
656 "\nIP address MTU SSTRESH RTT RTTVAR BANDWIDTH "
657 " CWND SENDPIPE RECVPIPE HITS UPD EXP\n");
658 p += len;
659
660 #define msec(u) (((u) + 500) / 1000)
661 for (i = 0; i < tcp_hostcache.hashsize; i++) {
662 THC_LOCK(&tcp_hostcache.hashbase[i].hch_mtx);
663 TAILQ_FOREACH(hc_entry, &tcp_hostcache.hashbase[i].hch_bucket,
664 rmx_q) {
665 len = snprintf(p, linesize,
666 "%-15s %5lu %8lu %6lums %6lums %9lu %8lu %8lu %8lu "
667 "%4lu %4lu %4i\n",
668 hc_entry->ip4.s_addr ? inet_ntoa(hc_entry->ip4) :
669 #ifdef INET6
670 ip6_sprintf(&hc_entry->ip6),
671 #else
672 "IPv6?",
673 #endif
674 hc_entry->rmx_mtu,
675 hc_entry->rmx_ssthresh,
676 msec(hc_entry->rmx_rtt *
677 (RTM_RTTUNIT / (hz * TCP_RTT_SCALE))),
678 msec(hc_entry->rmx_rttvar *
679 (RTM_RTTUNIT / (hz * TCP_RTT_SCALE))),
680 hc_entry->rmx_bandwidth * 8,
681 hc_entry->rmx_cwnd,
682 hc_entry->rmx_sendpipe,
683 hc_entry->rmx_recvpipe,
684 hc_entry->rmx_hits,
685 hc_entry->rmx_updates,
686 hc_entry->rmx_expire);
687 p += len;
688 }
689 THC_UNLOCK(&tcp_hostcache.hashbase[i].hch_mtx);
690 }
691 #undef msec
692 error = SYSCTL_OUT(req, buf, p - buf);
693 free(buf, M_TEMP);
694 return(error);
695 }
696
697 /*
698 * Expire and purge (old|all) entries in the tcp_hostcache. Runs periodically
699 * from the callout.
700 */
701 static void
702 tcp_hc_purge(void *arg)
703 {
704 struct hc_metrics *hc_entry, *hc_next;
705 int all = (intptr_t)arg;
706 int i;
707
708 if (tcp_hostcache.purgeall) {
709 all = 1;
710 tcp_hostcache.purgeall = 0;
711 }
712
713 for (i = 0; i < tcp_hostcache.hashsize; i++) {
714 THC_LOCK(&tcp_hostcache.hashbase[i].hch_mtx);
715 TAILQ_FOREACH_SAFE(hc_entry, &tcp_hostcache.hashbase[i].hch_bucket,
716 rmx_q, hc_next) {
717 if (all || hc_entry->rmx_expire <= 0) {
718 TAILQ_REMOVE(&tcp_hostcache.hashbase[i].hch_bucket,
719 hc_entry, rmx_q);
720 uma_zfree(tcp_hostcache.zone, hc_entry);
721 tcp_hostcache.hashbase[i].hch_length--;
722 tcp_hostcache.cache_count--;
723 } else
724 hc_entry->rmx_expire -= TCP_HOSTCACHE_PRUNE;
725 }
726 THC_UNLOCK(&tcp_hostcache.hashbase[i].hch_mtx);
727 }
728 callout_reset(&tcp_hc_callout, TCP_HOSTCACHE_PRUNE * hz, tcp_hc_purge, 0);
729 }
Cache object: e395d6deba0e92c79556cd0ab5d1803a
|