tcp_hostcache.c 21 KB

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