在连接结构的初始化函数ip_vs_conn_init中,可见IPVS为连接结构ip_vs_conn分配了专用的slab缓存,全局连接数据结构ip_vs_conn_tab的大小默认为4K(CONFIG_IP_VS_TAB_BITS)。
#ifndef CONFIG_IP_VS_TAB_BITS
#define CONFIG_IP_VS_TAB_BITS 12
#endif
static int ip_vs_conn_tab_bits = CONFIG_IP_VS_TAB_BITS;
int __init ip_vs_conn_init(void)
{
ip_vs_conn_tab_size = 1 << ip_vs_conn_tab_bits;
ip_vs_conn_tab_mask = ip_vs_conn_tab_size - 1;
ip_vs_conn_cachep = kmem_cache_create("ip_vs_conn", sizeof(struct ip_vs_conn), 0, SLAB_HWCACHE_ALIGN, NULL);
for (idx = 0; idx < ip_vs_conn_tab_size; idx++)
INIT_HLIST_HEAD(&ip_vs_conn_tab[idx]);
}
连接创建
如下函数ip_vs_conn_new,其中大部分的代码都是结构体赋值操作。这里面首先包括三个地址的赋值,分别为:客户端地址caddr、目的地址daddr和虚拟地址。
struct ip_vs_conn * ip_vs_conn_new(const struct ip_vs_conn_param *p, int dest_af,
const union nf_inet_addr *daddr, __be16 dport, unsigned int flags, struct ip_vs_dest *dest, __u32 fwmark)
{
struct ip_vs_conn *cp;
struct netns_ipvs *ipvs = p->ipvs;
struct ip_vs_proto_data *pd = ip_vs_proto_data_get(p->ipvs, p->protocol);
cp = kmem_cache_alloc(ip_vs_conn_cachep, GFP_ATOMIC);
INIT_HLIST_NODE(&cp->c_list);
timer_setup(&cp->timer, ip_vs_conn_expire, 0);
cp->ipvs = ipvs;
cp->af = p->af;
cp->daf = dest_af;
cp->protocol = p->protocol;
ip_vs_addr_set(p->af, &cp->caddr, p->caddr);
cp->cport = p->cport;
/* proto should only be IPPROTO_IP if p->vaddr is a fwmark */
ip_vs_addr_set(p->protocol == IPPROTO_IP ? AF_UNSPEC : p->af, &cp->vaddr, p->vaddr);
cp->vport = p->vport;
ip_vs_addr_set(cp->daf, &cp->daddr, daddr);
cp->dport = dport;
cp->flags = flags;
cp->fwmark = fwmark;
如果用户层ipvsadm命令在配置中指定了IP_VS_SVC_F_PERSISTENT属性,即命令:(ipvsadm -A -t 207.175.44.110:80 -s rr --persistent),此属性将同一个客户端的连接调度到同一个目的真实服务器上(第一次选择的真实服务器)。内核中IPVS为此种连接首先创建一个模板连接,以下为赋值PE(Persistent Engine)相关数据。
if (flags & IP_VS_CONN_F_TEMPLATE && p->pe) {
ip_vs_pe_get(p->pe);
cp->pe = p->pe;
cp->pe_data = p->pe_data;
cp->pe_data_len = p->pe_data_len;
} else {
cp->pe = NULL;
cp->pe_data = NULL;
cp->pe_data_len = 0;
}
spin_lock_init(&cp->lock);
/* Set the entry is referenced by the current thread before hashing it in the table, so that other thread run ip_vs_random_dropentry
* but cannot drop this entry.
*/
refcount_set(&cp->refcnt, 1);
cp->control = NULL;
atomic_set(&cp->n_control, 0);
atomic_set(&cp->in_pkts, 0);
cp->packet_xmit = NULL;
cp->app = NULL;
cp->app_data = NULL;
cp->in_seq.delta = 0;
cp->out_seq.delta = 0;
atomic_inc(&ipvs->conn_count);
if (flags & IP_VS_CONN_F_NO_CPORT)
atomic_inc(&ip_vs_conn_no_cport_cnt);
以下为新建的连接绑定目的服务器,稍后查看此ip_vs_bind_dest函数:
/* Bind the connection with a destination server */
cp->dest = NULL;
ip_vs_bind_dest(cp, dest);
函数 ip_vs_bind_xmit负责绑定发送函数。其根据当前连接的转发模式(NAT/Masq,DirectRoute,Tunnel),绑定不同的发送函数。赋值到连接结构的packet_xmit指针。
/* Set its state and timeout */
cp->state = 0;
cp->old_state = 0;
cp->timeout = 3*HZ;
cp->sync_endtime = jiffies & ~3UL;
/* Bind its packet transmitter */
#ifdef CONFIG_IP_VS_IPV6
if (p->af == AF_INET6)
ip_vs_bind_xmit_v6(cp);
else
#endif
ip_vs_bind_xmit(cp);
if (unlikely(pd && atomic_read(&pd->appcnt)))
ip_vs_bind_app(cp, pd->pp);
PROC文件系统的/proc/sys/net/ipv4/vs/conntrack可控制IPVS是否对其连接启用Netfilter系统的conntrack功能,默认情况下是关闭状态。最后函数ip_vs_conn_hash将新建连接添加到全局的ip_vs_conn_tab连接链表中。
/*
* Allow conntrack to be preserved. By default, conntrack is created and destroyed for every packet.
* Sometimes keeping conntrack can be useful for IP_VS_CONN_F_ONE_PACKET too.
*/
if (ip_vs_conntrack_enabled(ipvs))
cp->flags |= IP_VS_CONN_F_NFCT;
/* Hash it in the ip_vs_conn_tab finally */
ip_vs_conn_hash(cp);
return cp;
}
连接绑定目的服务器
函数ip_vs_bind_dest用于将新建的连接绑定一个真实服务器结构。首先对于One-packet scheduling模式,即配置命令为:(ipvsadm -A -u 207.175.44.110:80 -s rr --ops),由于其仅对于UDP协议流量有效,对于非UDP报文,这里清除其标志IP_VS_CONN_F_ONE_PACKET。设置有OPS标志的连接结构不会加入到全局连接链表中。
static inline void ip_vs_bind_dest(struct ip_vs_conn *cp, struct ip_vs_dest *dest)
{
ip_vs_dest_hold(dest);
conn_flags = atomic_read(&dest->conn_flags);
if (cp->protocol != IPPROTO_UDP)
conn_flags &= ~IP_VS_CONN_F_ONE_PACKET;
另外,如果当前连接是由同步线程创建(主同步线程发送而来),对于非模板连接,由于IPVS的主同步线程所在的主机在使用此目的服务器,说明其为活动状态,需要清除本机中的INACTIVE标志。对于同步生成的连接,请空连接的转发设置,这些设置应当从目的服务器结构中继承。
flags = cp->flags;
/* Bind with the destination and its corresponding transmitter */
if (flags & IP_VS_CONN_F_SYNC) {
/* if the connection is not template and is created by sync, preserve the activity flag.
*/
if (!(flags & IP_VS_CONN_F_TEMPLATE))
conn_flags &= ~IP_VS_CONN_F_INACTIVE;
/* connections inherit forwarding method from dest */
flags &= ~(IP_VS_CONN_F_FWD_MASK | IP_VS_CONN_F_NOOUTPUT);
}
flags |= conn_flags;
cp->flags = flags;
cp->dest = dest;
对于模板连接,增加目的服务器结构的persistconns统计计数。否则,对于非模板类型连接,依据当前连接的标志IP_VS_CONN_F_INACTIVE,增加相应的activeconns或者inactconns统计计数。
/* Update the connection counters */
if (!(flags & IP_VS_CONN_F_TEMPLATE)) {
/* It is a normal connection, so modify the counters according to the flags, later the protocol can update them on state change */
if (!(flags & IP_VS_CONN_F_INACTIVE))
atomic_inc(&dest->activeconns);
else
atomic_inc(&dest->inactconns);
} else {
/* It is a persistent connection/template, so increase the persistent connection counter */
atomic_inc(&dest->persistconns);
}
if (dest->u_threshold != 0 && ip_vs_dest_totalconns(dest) >= dest->u_threshold)
dest->flags |= IP_VS_DEST_F_OVERLOAD;
此外,如果用户指定了真实服务器可处理连接的高阈值,当其上的连接数量超过此阈值时,为此真实服务器结构设置超负荷标志IP_VS_DEST_F_OVERLOAD。
连接的绑定传输函数
函数ip_vs_bind_xmit负责根据连接中指定的转发模式,为其成员packet_xmit函数指针赋予不同的处理函数。
static inline void ip_vs_bind_xmit(struct ip_vs_conn *cp)
{
switch (IP_VS_FWD_METHOD(cp)) {
case IP_VS_CONN_F_MASQ:
cp->packet_xmit = ip_vs_nat_xmit;
break;
case IP_VS_CONN_F_TUNNEL:
#ifdef CONFIG_IP_VS_IPV6
if (cp->daf == AF_INET6)
cp->packet_xmit = ip_vs_tunnel_xmit_v6;
else
#endif
cp->packet_xmit = ip_vs_tunnel_xmit;
break;
case IP_VS_CONN_F_DROUTE:
cp->packet_xmit = ip_vs_dr_xmit;
break;
case IP_VS_CONN_F_LOCALNODE:
cp->packet_xmit = ip_vs_null_xmit;
break;
case IP_VS_CONN_F_BYPASS:
cp->packet_xmit = ip_vs_bypass_xmit;
break;
}
目前IPVS支持的5中转发模式及其对应的处理函数:
转发模式 | 处理函数 |
---|---|
IP_VS_CONN_F_MASQ | ip_vs_nat_xmit |
IP_VS_CONN_F_TUNNEL | ip_vs_tunnel_xmit |
IP_VS_CONN_F_DROUTE | ip_vs_dr_xmit |
IP_VS_CONN_F_LOCALNODE | ip_vs_null_xmit |
IP_VS_CONN_F_BYPASS | ip_vs_bypass_xmit |
连接绑定APP应用
函数ip_vs_bind_app负责为当前的连接绑定APP应用,此函数仅针对NAT/Masq转发模式的连接。当前的IPVS仅支持FTP一种应用。
int ip_vs_bind_app(struct ip_vs_conn *cp, struct ip_vs_protocol *pp)
{
return pp->app_conn_bind(cp);
}
对于UDP/TCP/SCTP协议,函数指针app_conn_bind的实现函数分别为:udp_app_conn_bind、tcp_app_conn_bind和sctp_app_conn_bind。
全局连接链表
函数ip_vs_conn_hash负责将新创建的连接添加到全局数组链表ip_vs_conn_tab中。首先前面已经看到,对于设置了IP_VS_CONN_F_ONE_PACKET标志(One-Packet scheduling)的连接不进行添加操作。
static inline int ip_vs_conn_hash(struct ip_vs_conn *cp)
{
if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
return 0;
函数ip_vs_conn_hashkey_conn计算当前连接的hash值,作为全局链表数据的索引值,将当前连接结构添加到按照索引值确定的链表中。
/* Hash by protocol, client address and port */
hash = ip_vs_conn_hashkey_conn(cp);
ct_write_lock_bh(hash);
spin_lock(&cp->lock);
if (!(cp->flags & IP_VS_CONN_F_HASHED)) {
cp->flags |= IP_VS_CONN_F_HASHED;
refcount_inc(&cp->refcnt);
hlist_add_head_rcu(&cp->c_list, &ip_vs_conn_tab[hash]);
ret = 1;
} else {
pr_err("%s(): request for already hashed, called from %pS\n", __func__, __builtin_return_address(0));
ret = 0;
}
连接hash的计算函数ip_vs_conn_hashkey_conn如下,其先构建一个表示连接的参数结构ip_vs_conn_param,以及为其填充PE数据。由函数ip_vs_conn_hashkey_param计算哈希值。
static unsigned int ip_vs_conn_hashkey_conn(const struct ip_vs_conn *cp)
{
struct ip_vs_conn_param p;
ip_vs_conn_fill_param(cp->ipvs, cp->af, cp->protocol, &cp->caddr, cp->cport, NULL, 0, &p);
if (cp->pe) {
p.pe = cp->pe;
p.pe_data = cp->pe_data;
p.pe_data_len = cp->pe_data_len;
}
return ip_vs_conn_hashkey_param(&p, false);
连接参数结构ip_vs_conn_param的赋值函数ip_vs_conn_fill_param,主要赋值的字段有:客户端地址、虚拟地址、协议、协议族等。
static inline void ip_vs_conn_fill_param(struct netns_ipvs *ipvs, int af, int protocol, const union nf_inet_addr *caddr,
__be16 cport, const union nf_inet_addr *vaddr, __be16 vport, struct ip_vs_conn_param *p)
{
p->ipvs = ipvs;
p->af = af;
p->protocol = protocol;
p->caddr = caddr;
p->cport = cport;
p->vaddr = vaddr;
p->vport = vport;
p->pe = NULL;
p->pe_data = NULL;
}
哈希函数ip_vs_conn_hashkey_param根据是否存在PE数据,使用不同的哈希函数。如果PE数据存在,并且PE引擎注册了哈希处理函数hashkey_raw,使用其计算hash值。否则,使用函数ip_vs_conn_hashkey计算hash值。后者使用内核的jhash算法进行hash的计算。
static unsigned int ip_vs_conn_hashkey_param(const struct ip_vs_conn_param *p, bool inverse)
{
const union nf_inet_addr *addr;
__be16 port;
if (p->pe_data && p->pe->hashkey_raw)
return p->pe->hashkey_raw(p, ip_vs_conn_rnd, inverse) & ip_vs_conn_tab_mask;
if (likely(!inverse)) {
addr = p->caddr;
port = p->cport;
} else {
addr = p->vaddr;
port = p->vport;
}
return ip_vs_conn_hashkey(p->ipvs, p->af, p->protocol, addr, port);
}
目前IPVS仅支持SIP一种PE引擎,其主要依据PE数据进行hash值计算。
static u32 ip_vs_sip_hashkey_raw(const struct ip_vs_conn_param *p, u32 initval, bool inverse)
{
return jhash(p->pe_data, p->pe_data_len, initval);
}
连接超时定时器
在创建连接时,IPVS创建了一个超时定时器,其处理函数为ip_vs_conn_expire,时长设置为3秒。此定时器的启用在函数__ip_vs_conn_put_timer中实现。
static void __ip_vs_conn_put_timer(struct ip_vs_conn *cp)
{
unsigned long t = (cp->flags & IP_VS_CONN_F_ONE_PACKET) ? 0 : cp->timeout;
mod_timer(&cp->timer, jiffies+t);
__ip_vs_conn_put(cp);
}
在IPVS系统中,一般情况下调用以上函数__ip_vs_conn_put_timer的封装函数ip_vs_conn_put,此函数在连接结构使用完之后被调用。例如在首次连接新建之后,其引用计数refcnt为1,在使用完成之后,调用ip_vs_conn_put。
void ip_vs_conn_put(struct ip_vs_conn *cp)
{
if ((cp->flags & IP_VS_CONN_F_ONE_PACKET) && (refcount_read(&cp->refcnt) == 1) && !timer_pending(&cp->timer))
/* expire connection immediately */
__ip_vs_conn_put_notimer(cp);
else
__ip_vs_conn_put_timer(cp);
}
对于设置了IP_VS_CONN_F_ONE_PACKET标志的连接,如果其引用计数为1,并且超时定时器未启动。将不启用定时器,直接进行超时处理。
static void __ip_vs_conn_put_notimer(struct ip_vs_conn *cp)
{
__ip_vs_conn_put(cp);
ip_vs_conn_expire(&cp->timer);
}
否则,以上条件不成立,调用__ip_vs_conn_put_timer函数,启动定时器。对于设置了IP_VS_CONN_F_ONE_PACKET标志的连接,超时时长为0,即马上启动超时处理;否则,为默认的3秒钟超时。函数__ip_vs_conn_put负责递减连接的引用计数。
当在超时时间内,又接收到此连接的报文,又将会涉及连接的(get/put)操作,在put操作中(函数ip_vs_conn_put)将更新超时定时器。
当超时时间内未接收到此连接的报文时,执行超时处理函数ip_vs_conn_expire。首先对于模板类型的连接,如果其控制的连接数量不为零,稍后对其进行超时处理。
static void ip_vs_conn_expire(struct timer_list *t)
{
struct ip_vs_conn *cp = from_timer(cp, t, timer);
struct netns_ipvs *ipvs = cp->ipvs;
/* do I control anybody?
*/
if (atomic_read(&cp->n_control))
goto expire_later;
函数ip_vs_conn_unlink首先将超时的连接由全局链表ip_vs_conn_tab中移除,之后开始连接的清理工作。1)如果此连接时根据连接模板生成,移除连接模板数据;2)如果启用了netfilter的连接跟踪,删除连接跟踪信息;3)解绑连接关联的应用app,目前IPVS支持的应用FTP的解绑函数都是空,不执行任何操作;4)解绑定目的真实服务器结构,稍后看此函数。
可见其过程与连接的创建基本上是相对的。最后由函数ip_vs_conn_rcu_free将连接的空间释放,并返还到slab缓存ip_vs_conn_cachep中
/* Unlink conn if not referenced anymore */
if (likely(ip_vs_conn_unlink(cp))) {
/* delete the timer if it is activated by other users */
del_timer(&cp->timer);
/* does anybody control me? */
if (cp->control)
ip_vs_control_del(cp);
if ((cp->flags & IP_VS_CONN_F_NFCT) && !(cp->flags & IP_VS_CONN_F_ONE_PACKET)) {
/* Do not access conntracks during subsys cleanup because nf_conntrack_find_get can not be used after
* conntrack cleanup for the net.
*/
smp_rmb();
if (ipvs->enable)
ip_vs_conn_drop_conntrack(cp);
}
if (unlikely(cp->app != NULL))
ip_vs_unbind_app(cp);
ip_vs_unbind_dest(cp);
if (cp->flags & IP_VS_CONN_F_NO_CPORT)
atomic_dec(&ip_vs_conn_no_cport_cnt);
if (cp->flags & IP_VS_CONN_F_ONE_PACKET)
ip_vs_conn_rcu_free(&cp->rcu_head);
else
call_rcu(&cp->rcu_head, ip_vs_conn_rcu_free);
atomic_dec(&ipvs->conn_count);
return;
}
对于函数开头的连接模板,在这里将其超时时间设置为60秒钟,重新开启定时器,等待其所控制的连接完成释放。如果本机为主同步线程,将此模板连接同步到backup端。
expire_later:
refcount_inc(&cp->refcnt);
cp->timeout = 60*HZ;
if (ipvs->sync_state & IP_VS_STATE_MASTER)
ip_vs_sync_conn(ipvs, cp, sysctl_sync_threshold(ipvs));
__ip_vs_conn_put_timer(cp);
}
解绑定目的服务器
在上节连接超时处理函数中,使用函数ip_vs_unbind_dest解绑连接的目的服务器结构。此过程与绑定目的服务器函数(ip_vs_bind_dest)正相反。此函数中,对于模板连接,递减其persistconns计数;对于非模板连接,根据IP_VS_CONN_F_INACTIVE标志,决定递减inactconns或者activeconns计数。
static inline void ip_vs_unbind_dest(struct ip_vs_conn *cp)
{
struct ip_vs_dest *dest = cp->dest;
/* Update the connection counters */
if (!(cp->flags & IP_VS_CONN_F_TEMPLATE)) {
/* It is a normal connection, so decrease the inactconns or activeconns counter */
if (cp->flags & IP_VS_CONN_F_INACTIVE) {
atomic_dec(&dest->inactconns);
} else {
atomic_dec(&dest->activeconns);
}
} else {
/* It is a persistent connection/template, so decrease the persistent connection counter */
atomic_dec(&dest->persistconns);
}
如果此时目的服务器的连接数量低于其低阈值,清除其过负荷标志。或者,如未设置低阈值,而设置了高阈值,当连接数量低于高阈值的3/4时,也清除过载标志。
if (dest->l_threshold != 0) {
if (ip_vs_dest_totalconns(dest) < dest->l_threshold)
dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
} else if (dest->u_threshold != 0) {
if (ip_vs_dest_totalconns(dest) * 4 < dest->u_threshold * 3)
dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
} else {
if (dest->flags & IP_VS_DEST_F_OVERLOAD)
dest->flags &= ~IP_VS_DEST_F_OVERLOAD;
}
连接查找
在入口函数ip_vs_in中,IPVS使用协议特定的连接查找函数conn_in_get。
static unsigned int ip_vs_in(struct netns_ipvs *ipvs, unsigned int hooknum, struct sk_buff *skb, int af)
{
struct ip_vs_iphdr iph;
struct ip_vs_protocol *pp;
struct ip_vs_proto_data *pd;
/* Protocol supported? */
pd = ip_vs_proto_data_get(ipvs, iph.protocol);
pp = pd->pp;
/* Check if the packet belongs to an existing connection entry
*/
cp = pp->conn_in_get(ipvs, af, skb, &iph);
对于UDP/TCP/SCTP协议,指针conn_in_get函数相同为ip_vs_conn_in_get_proto;而对于AH/ESP协议,其函数为ah_esp_conn_in_get。这两个函数都是通过调用ip_vs_conn_in_get来获取连接结构,不同点在于连接参数结构ip_vs_conn_param的初始化。
struct ip_vs_conn * ip_vs_conn_in_get_proto(struct netns_ipvs *ipvs, int af, const struct sk_buff *skb, const struct ip_vs_iphdr *iph)
{
struct ip_vs_conn_param p;
if (ip_vs_conn_fill_param_proto(ipvs, af, skb, iph, &p))
return NULL;
return ip_vs_conn_in_get(&p);
}
static struct ip_vs_conn *ah_esp_conn_in_get(struct netns_ipvs *ipvs, int af, const struct sk_buff *skb, const struct ip_vs_iphdr *iph)
{
struct ip_vs_conn *cp;
struct ip_vs_conn_param p;
ah_esp_conn_fill_param_proto(ipvs, af, iph, &p);
cp = ip_vs_conn_in_get(&p);
return cp;
}
对比一下的两个连接参数结构赋值函数可见,前者ip_vs_conn_fill_param_proto使用由报文中获取的源和目的端口号;而后者ah_esp_conn_fill_param_proto使用固定的源和目的端口号,即宏定义PORT_ISAKMP,其值为500。
static int ip_vs_conn_fill_param_proto(struct netns_ipvs *ipvs, int af, const struct sk_buff *skb, const struct ip_vs_iphdr *iph, struct ip_vs_conn_param *p)
{
__be16 _ports[2], *pptr;
pptr = frag_safe_skb_hp(skb, iph->len, sizeof(_ports), _ports, iph);
if (likely(!ip_vs_iph_inverse(iph)))
ip_vs_conn_fill_param(ipvs, af, iph->protocol, &iph->saddr, pptr[0], &iph->daddr, pptr[1], p);
else
ip_vs_conn_fill_param(ipvs, af, iph->protocol, &iph->daddr, pptr[1], &iph->saddr, pptr[0], p);
return 0;
}
static void ah_esp_conn_fill_param_proto(struct netns_ipvs *ipvs, int af, const struct ip_vs_iphdr *iph, struct ip_vs_conn_param *p)
{
if (likely(!ip_vs_iph_inverse(iph)))
ip_vs_conn_fill_param(ipvs, af, IPPROTO_UDP, &iph->saddr, htons(PORT_ISAKMP), &iph->daddr, htons(PORT_ISAKMP), p);
else
ip_vs_conn_fill_param(ipvs, af, IPPROTO_UDP, &iph->daddr, htons(PORT_ISAKMP), &iph->saddr, htons(PORT_ISAKMP), p);
}
连接获取函数ip_vs_conn_in_get首先调用__ip_vs_conn_in_get获取链接;如果没有找到,并且IPVS系统中无客户端口的连接数量不为空,清空连接参数结构ip_vs_conn_param中的cport成员,再次进行连接查找。
struct ip_vs_conn *ip_vs_conn_in_get(const struct ip_vs_conn_param *p)
{
struct ip_vs_conn *cp;
cp = __ip_vs_conn_in_get(p);
if (!cp && atomic_read(&ip_vs_conn_no_cport_cnt)) {
struct ip_vs_conn_param cport_zero_p = *p;
cport_zero_p.cport = 0;
cp = __ip_vs_conn_in_get(&cport_zero_p);
}
核心连接查找函数__ip_vs_conn_in_get首先由连接参数计算出对应的hash值,之后遍历对应的全局链表ip_vs_conn_tab[hash]。匹配的连接需要满足以下的条件:1)客户端地址相同;2)虚拟地址相同;3)协议族相同;4)协议相同;5)同一个IPVS实例.
static inline struct ip_vs_conn *__ip_vs_conn_in_get(const struct ip_vs_conn_param *p)
{
unsigned int hash;
struct ip_vs_conn *cp;
hash = ip_vs_conn_hashkey_param(p, false);
hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[hash], c_list) {
if (p->cport == cp->cport && p->vport == cp->vport &&
cp->af == p->af &&
ip_vs_addr_equal(p->af, p->caddr, &cp->caddr) &&
ip_vs_addr_equal(p->af, p->vaddr, &cp->vaddr) &&
((!p->cport) ^ (!(cp->flags & IP_VS_CONN_F_NO_CPORT))) &&
p->protocol == cp->protocol &&
cp->ipvs == p->ipvs) {
if (!__ip_vs_conn_get(cp))
continue;
/* HIT */
rcu_read_unlock();
return cp;
}
}
以下链接查找函数ip_vs_ct_in_get与以上介绍的不同,其主要用于Persistence模式下连接的查找。代码如下所示,其将首先执行PE的匹配函数ct_match,如果其匹配成功,将直接返回匹配的连接。如果PE引擎没有实现ct_match函数指针,将使用与以上连接查找函数__ip_vs_conn_in_get相同的逻辑查找连接。
struct ip_vs_conn *ip_vs_ct_in_get(const struct ip_vs_conn_param *p)
{
unsigned int hash;
struct ip_vs_conn *cp;
hash = ip_vs_conn_hashkey_param(p, false);
rcu_read_lock();
hlist_for_each_entry_rcu(cp, &ip_vs_conn_tab[hash], c_list) {
if (unlikely(p->pe_data && p->pe->ct_match)) {
if (cp->ipvs != p->ipvs)
continue;
if (p->pe == cp->pe && p->pe->ct_match(p, cp)) {
if (__ip_vs_conn_get(cp))
goto out;
}
continue;
}
if (cp->af == p->af &&
ip_vs_addr_equal(p->af, p->caddr, &cp->caddr) &&
/* protocol should only be IPPROTO_IP if p->vaddr is a fwmark */
ip_vs_addr_equal(p->protocol == IPPROTO_IP ? AF_UNSPEC : p->af, p->vaddr, &cp->vaddr) &&
p->vport == cp->vport && p->cport == cp->cport &&
cp->flags & IP_VS_CONN_F_TEMPLATE &&
p->protocol == cp->protocol &&
cp->ipvs == p->ipvs) {
if (__ip_vs_conn_get(cp))
goto out;
}
}
内核版本 4.15