FreeBSD kernel IPv4 code
tcp_hostcache.c File Reference
#include <sys/cdefs.h>
#include "opt_inet6.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/hash.h>
#include <sys/jail.h>
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/malloc.h>
#include <sys/proc.h>
#include <sys/sbuf.h>
#include <sys/smr.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/sysctl.h>
#include <net/vnet.h>
#include <netinet/in.h>
#include <netinet/in_pcb.h>
#include <netinet/tcp.h>
#include <netinet/tcp_var.h>
#include <vm/uma.h>
Include dependency graph for tcp_hostcache.c:

Go to the source code of this file.

Data Structures

struct  hc_head
 
struct  hc_metrics
 
struct  tcp_hostcache
 

Macros

#define TCP_HOSTCACHE_HASHSIZE   512
 
#define TCP_HOSTCACHE_BUCKETLIMIT   30
 
#define TCP_HOSTCACHE_EXPIRE   60*60 /* one hour */
 
#define TCP_HOSTCACHE_PRUNE   5*60 /* every 5 minutes */
 
#define V_tcp_hostcache   VNET(tcp_hostcache)
 
#define V_tcp_hc_callout   VNET(tcp_hc_callout)
 
#define V_tcp_use_hostcache   VNET(tcp_use_hostcache)
 
#define HOSTCACHE_HASH(inc)
 
#define THC_LOCK(h)   mtx_lock(&(h)->hch_mtx)
 
#define THC_UNLOCK(h)   mtx_unlock(&(h)->hch_mtx)
 
#define msec(u)   (((u) + 500) / 1000)
 

Functions

 __FBSDID ("$FreeBSD$")
 
 VNET_DEFINE_STATIC (struct tcp_hostcache, tcp_hostcache)
 
 VNET_DEFINE_STATIC (struct callout, tcp_hc_callout)
 
static struct hc_metricstcp_hc_lookup (struct in_conninfo *)
 
static int sysctl_tcp_hc_list (SYSCTL_HANDLER_ARGS)
 
static int sysctl_tcp_hc_histo (SYSCTL_HANDLER_ARGS)
 
static int sysctl_tcp_hc_purgenow (SYSCTL_HANDLER_ARGS)
 
static void tcp_hc_purge_internal (int)
 
static void tcp_hc_purge (void *)
 
static SYSCTL_NODE (_net_inet_tcp, OID_AUTO, hostcache, CTLFLAG_RW|CTLFLAG_MPSAFE, 0, "TCP Host cache")
 
 VNET_DEFINE (int, tcp_use_hostcache)
 
 SYSCTL_INT (_net_inet_tcp_hostcache, OID_AUTO, enable, CTLFLAG_VNET|CTLFLAG_RW, &VNET_NAME(tcp_use_hostcache), 0, "Enable the TCP hostcache")
 
 SYSCTL_UINT (_net_inet_tcp_hostcache, OID_AUTO, cachelimit, CTLFLAG_VNET|CTLFLAG_RDTUN, &VNET_NAME(tcp_hostcache.cache_limit), 0, "Overall entry limit for hostcache")
 
 SYSCTL_UINT (_net_inet_tcp_hostcache, OID_AUTO, hashsize, CTLFLAG_VNET|CTLFLAG_RDTUN, &VNET_NAME(tcp_hostcache.hashsize), 0, "Size of TCP hostcache hashtable")
 
 SYSCTL_UINT (_net_inet_tcp_hostcache, OID_AUTO, bucketlimit, CTLFLAG_VNET|CTLFLAG_RDTUN, &VNET_NAME(tcp_hostcache.bucket_limit), 0, "Per-bucket hash limit for hostcache")
 
 SYSCTL_UINT (_net_inet_tcp_hostcache, OID_AUTO, count, CTLFLAG_VNET|CTLFLAG_RD, &VNET_NAME(tcp_hostcache.cache_count), 0, "Current number of entries in hostcache")
 
 SYSCTL_INT (_net_inet_tcp_hostcache, OID_AUTO, expire, CTLFLAG_VNET|CTLFLAG_RW, &VNET_NAME(tcp_hostcache.expire), 0, "Expire time of TCP hostcache entries")
 
 SYSCTL_INT (_net_inet_tcp_hostcache, OID_AUTO, prune, CTLFLAG_VNET|CTLFLAG_RW, &VNET_NAME(tcp_hostcache.prune), 0, "Time between purge runs")
 
 SYSCTL_INT (_net_inet_tcp_hostcache, OID_AUTO, purge, CTLFLAG_VNET|CTLFLAG_RW, &VNET_NAME(tcp_hostcache.purgeall), 0, "Expire all entries on next purge run")
 
 SYSCTL_PROC (_net_inet_tcp_hostcache, OID_AUTO, list, CTLTYPE_STRING|CTLFLAG_RD|CTLFLAG_SKIP|CTLFLAG_MPSAFE, 0, 0, sysctl_tcp_hc_list, "A", "List of all hostcache entries")
 
 SYSCTL_PROC (_net_inet_tcp_hostcache, OID_AUTO, histo, CTLTYPE_STRING|CTLFLAG_RD|CTLFLAG_SKIP|CTLFLAG_MPSAFE, 0, 0, sysctl_tcp_hc_histo, "A", "Print a histogram of hostcache hashbucket utilization")
 
 SYSCTL_PROC (_net_inet_tcp_hostcache, OID_AUTO, purgenow, CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_MPSAFE, NULL, 0, sysctl_tcp_hc_purgenow, "I", "Immediately purge all entries")
 
static MALLOC_DEFINE (M_HOSTCACHE, "hostcache", "TCP hostcache")
 
void tcp_hc_init (void)
 
static bool tcp_hc_cmp (struct hc_metrics *hc_entry, struct in_conninfo *inc)
 
void tcp_hc_get (struct in_conninfo *inc, struct hc_metrics_lite *hc_metrics_lite)
 
uint32_t tcp_hc_getmtu (struct in_conninfo *inc)
 
void tcp_hc_updatemtu (struct in_conninfo *inc, uint32_t mtu)
 
void tcp_hc_update (struct in_conninfo *inc, struct hc_metrics_lite *hcml)
 

Macro Definition Documentation

◆ HOSTCACHE_HASH

#define HOSTCACHE_HASH (   inc)
Value:
((inc)->inc_flags & INC_ISIPV6) ? \
(jenkins_hash32((inc)->inc6_faddr.s6_addr32, 4, \
V_tcp_hostcache.hashsalt) & V_tcp_hostcache.hashmask) \
: \
(jenkins_hash32(&(inc)->inc_faddr.s_addr, 1, \
V_tcp_hostcache.hashsalt) & V_tcp_hostcache.hashmask)
#define INC_ISIPV6
Definition: in_pcb.h:124
#define inc_faddr
Definition: in_pcb.h:129
#define inc6_faddr
Definition: in_pcb.h:131
#define V_tcp_hostcache

Definition at line 215 of file tcp_hostcache.c.

◆ msec

#define msec (   u)    (((u) + 500) / 1000)

◆ TCP_HOSTCACHE_BUCKETLIMIT

#define TCP_HOSTCACHE_BUCKETLIMIT   30

Definition at line 142 of file tcp_hostcache.c.

◆ TCP_HOSTCACHE_EXPIRE

#define TCP_HOSTCACHE_EXPIRE   60*60 /* one hour */

Definition at line 143 of file tcp_hostcache.c.

◆ TCP_HOSTCACHE_HASHSIZE

#define TCP_HOSTCACHE_HASHSIZE   512

Definition at line 141 of file tcp_hostcache.c.

◆ TCP_HOSTCACHE_PRUNE

#define TCP_HOSTCACHE_PRUNE   5*60 /* every 5 minutes */

Definition at line 144 of file tcp_hostcache.c.

◆ THC_LOCK

#define THC_LOCK (   h)    mtx_lock(&(h)->hch_mtx)

Definition at line 223 of file tcp_hostcache.c.

◆ THC_UNLOCK

#define THC_UNLOCK (   h)    mtx_unlock(&(h)->hch_mtx)

Definition at line 224 of file tcp_hostcache.c.

◆ V_tcp_hc_callout

#define V_tcp_hc_callout   VNET(tcp_hc_callout)

Definition at line 150 of file tcp_hostcache.c.

◆ V_tcp_hostcache

#define V_tcp_hostcache   VNET(tcp_hostcache)

Definition at line 147 of file tcp_hostcache.c.

◆ V_tcp_use_hostcache

#define V_tcp_use_hostcache   VNET(tcp_use_hostcache)

Definition at line 164 of file tcp_hostcache.c.

Function Documentation

◆ __FBSDID()

__FBSDID ( "$FreeBSD$"  )

◆ MALLOC_DEFINE()

static MALLOC_DEFINE ( M_HOSTCACHE  ,
"hostcache"  ,
"TCP hostcache"   
)
static

◆ SYSCTL_INT() [1/4]

SYSCTL_INT ( _net_inet_tcp_hostcache  ,
OID_AUTO  ,
enable  ,
CTLFLAG_VNET|  CTLFLAG_RW,
VNET_NAMEtcp_use_hostcache,
,
"Enable the TCP hostcache"   
)

◆ SYSCTL_INT() [2/4]

SYSCTL_INT ( _net_inet_tcp_hostcache  ,
OID_AUTO  ,
expire  ,
CTLFLAG_VNET|  CTLFLAG_RW,
VNET_NAMEtcp_hostcache.expire,
,
"Expire time of TCP hostcache entries"   
)

◆ SYSCTL_INT() [3/4]

SYSCTL_INT ( _net_inet_tcp_hostcache  ,
OID_AUTO  ,
prune  ,
CTLFLAG_VNET|  CTLFLAG_RW,
VNET_NAMEtcp_hostcache.prune,
,
"Time between purge runs"   
)

◆ SYSCTL_INT() [4/4]

SYSCTL_INT ( _net_inet_tcp_hostcache  ,
OID_AUTO  ,
purge  ,
CTLFLAG_VNET|  CTLFLAG_RW,
VNET_NAMEtcp_hostcache.purgeall,
,
"Expire all entries on next purge run"   
)

◆ SYSCTL_NODE()

static SYSCTL_NODE ( _net_inet_tcp  ,
OID_AUTO  ,
hostcache  ,
CTLFLAG_RW|  CTLFLAG_MPSAFE,
,
"TCP Host cache"   
)
static

◆ SYSCTL_PROC() [1/3]

SYSCTL_PROC ( _net_inet_tcp_hostcache  ,
OID_AUTO  ,
histo  ,
CTLTYPE_STRING|CTLFLAG_RD|CTLFLAG_SKIP|  CTLFLAG_MPSAFE,
,
,
sysctl_tcp_hc_histo  ,
"A"  ,
"Print a histogram of hostcache hashbucket utilization"   
)

◆ SYSCTL_PROC() [2/3]

SYSCTL_PROC ( _net_inet_tcp_hostcache  ,
OID_AUTO  ,
list  ,
CTLTYPE_STRING|CTLFLAG_RD|CTLFLAG_SKIP|  CTLFLAG_MPSAFE,
,
,
sysctl_tcp_hc_list  ,
"A"  ,
"List of all hostcache entries"   
)

◆ SYSCTL_PROC() [3/3]

SYSCTL_PROC ( _net_inet_tcp_hostcache  ,
OID_AUTO  ,
purgenow  ,
CTLTYPE_INT|CTLFLAG_RW|  CTLFLAG_MPSAFE,
NULL  ,
,
sysctl_tcp_hc_purgenow  ,
"I"  ,
"Immediately purge all entries"   
)

◆ sysctl_tcp_hc_histo()

static int sysctl_tcp_hc_histo ( SYSCTL_HANDLER_ARGS  )
static

Definition at line 716 of file tcp_hostcache.c.

References V_tcp_hostcache.

◆ sysctl_tcp_hc_list()

static int sysctl_tcp_hc_list ( SYSCTL_HANDLER_ARGS  )
static

Definition at line 630 of file tcp_hostcache.c.

References inet_ntoa_r(), msec, TCP_RTT_SCALE, TCP_RTTVAR_SCALE, THC_LOCK, THC_UNLOCK, and V_tcp_hostcache.

Here is the call graph for this function:

◆ sysctl_tcp_hc_purgenow()

static int sysctl_tcp_hc_purgenow ( SYSCTL_HANDLER_ARGS  )
static

Definition at line 830 of file tcp_hostcache.c.

References tcp_hc_purge(), tcp_hc_purge_internal(), V_tcp_hc_callout, and V_tcp_hostcache.

Here is the call graph for this function:

◆ SYSCTL_UINT() [1/4]

SYSCTL_UINT ( _net_inet_tcp_hostcache  ,
OID_AUTO  ,
bucketlimit  ,
CTLFLAG_VNET|  CTLFLAG_RDTUN,
VNET_NAMEtcp_hostcache.bucket_limit,
,
"Per-bucket hash limit for hostcache"   
)

◆ SYSCTL_UINT() [2/4]

SYSCTL_UINT ( _net_inet_tcp_hostcache  ,
OID_AUTO  ,
cachelimit  ,
CTLFLAG_VNET|  CTLFLAG_RDTUN,
VNET_NAMEtcp_hostcache.cache_limit,
,
"Overall entry limit for hostcache"   
)

◆ SYSCTL_UINT() [3/4]

SYSCTL_UINT ( _net_inet_tcp_hostcache  ,
OID_AUTO  ,
count  ,
CTLFLAG_VNET|  CTLFLAG_RD,
VNET_NAMEtcp_hostcache.cache_count,
,
"Current number of entries in hostcache"   
)

◆ SYSCTL_UINT() [4/4]

SYSCTL_UINT ( _net_inet_tcp_hostcache  ,
OID_AUTO  ,
hashsize  ,
CTLFLAG_VNET|  CTLFLAG_RDTUN,
VNET_NAMEtcp_hostcache.hashsize,
,
"Size of TCP hostcache hashtable"   
)

◆ tcp_hc_cmp()

static bool tcp_hc_cmp ( struct hc_metrics hc_entry,
struct in_conninfo inc 
)
static

Definition at line 318 of file tcp_hostcache.c.

References in_conninfo::inc_flags, and INC_ISIPV6.

Referenced by tcp_hc_lookup(), and tcp_hc_update().

Here is the caller graph for this function:

◆ tcp_hc_get()

void tcp_hc_get ( struct in_conninfo inc,
struct hc_metrics_lite hc_metrics_lite 
)

◆ tcp_hc_getmtu()

uint32_t tcp_hc_getmtu ( struct in_conninfo inc)

Definition at line 416 of file tcp_hostcache.c.

References tcp_hc_lookup(), V_tcp_hostcache, and V_tcp_use_hostcache.

Referenced by sctp_hc_get_mtu(), and tcp_mssopt().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ tcp_hc_init()

void tcp_hc_init ( void  )

Definition at line 227 of file tcp_hostcache.c.

References tcp_hc_purge(), TCP_HOSTCACHE_BUCKETLIMIT, TCP_HOSTCACHE_EXPIRE, TCP_HOSTCACHE_HASHSIZE, TCP_HOSTCACHE_PRUNE, V_tcp_hc_callout, and V_tcp_hostcache.

Referenced by tcp_vnet_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ tcp_hc_lookup()

static struct hc_metrics * tcp_hc_lookup ( struct in_conninfo inc)
static

Definition at line 340 of file tcp_hostcache.c.

References HOSTCACHE_HASH, tcp_hc_cmp(), and V_tcp_hostcache.

Referenced by tcp_hc_get(), and tcp_hc_getmtu().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ tcp_hc_purge()

static void tcp_hc_purge ( void *  arg)
static

Definition at line 807 of file tcp_hostcache.c.

References tcp_hc_purge(), tcp_hc_purge_internal(), V_tcp_hc_callout, and V_tcp_hostcache.

Referenced by sysctl_tcp_hc_purgenow(), tcp_hc_init(), and tcp_hc_purge().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ tcp_hc_purge_internal()

static void tcp_hc_purge_internal ( int  all)
static

Definition at line 757 of file tcp_hostcache.c.

References THC_LOCK, THC_UNLOCK, and V_tcp_hostcache.

Referenced by sysctl_tcp_hc_purgenow(), and tcp_hc_purge().

Here is the caller graph for this function:

◆ tcp_hc_update()

◆ tcp_hc_updatemtu()

void tcp_hc_updatemtu ( struct in_conninfo inc,
uint32_t  mtu 
)

Definition at line 440 of file tcp_hostcache.c.

References hc_metrics_lite::rmx_mtu, and tcp_hc_update().

Referenced by sctp_hc_set_mtu().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ VNET_DEFINE()

VNET_DEFINE ( int  ,
tcp_use_hostcache   
)

◆ VNET_DEFINE_STATIC() [1/2]

VNET_DEFINE_STATIC ( struct callout  ,
tcp_hc_callout   
)

◆ VNET_DEFINE_STATIC() [2/2]

VNET_DEFINE_STATIC ( struct tcp_hostcache  ,
tcp_hostcache   
)