4. LVS内核源码分析------NAT模式下TCP报文
前面已经讲过了LVS支持三种模式,其中NAT模式的应用比较广泛,该种模式下的配置仅仅涉及到LVS服务器,Real Server上不需要进行额外的配置。
4.1 LVS的HOOK点函数Linux2.6.32
在内核中IPVS的源码在net/netfilter/ipvs目录下,LVS是以netfilter框架为基础,先看一下LVS在哪些HOOK点挂载了自己的处理函数。IPVS的处理函数在挂载在下面三个HOOK点,NF_INET_LOCAL_IN, NF_INET_FORWARD,NF_INET_POST_ROUTING,LVS同时支持IPv4/IPv6两种协议。
点击(此处)折叠或打开
-
static struct nf_hook_ops ip_vs_ops[] __read_mostly = {
-
/* After packet filtering, forward packet through VS/DR, VS/TUN,
-
* or VS/NAT(change destination), so that filtering rules can be
-
* applied to IPVS. */
-
{
-
.hook = ip_vs_in,
-
.owner = THIS_MODULE,
-
.pf = PF_INET,
-
.hooknum = NF_INET_LOCAL_IN,
-
.priority = 100,
-
},
-
/* After packet filtering, change source only for VS/NAT */
-
{
-
.hook = ip_vs_out,
-
.owner = THIS_MODULE,
-
.pf = PF_INET,
-
.hooknum = NF_INET_FORWARD,
-
.priority = 100,
-
},
-
/* After packet filtering (but before ip_vs_out_icmp), catch icmp
-
* destined for 0.0.0.0/0, which is for incoming IPVS connections */
-
{
-
.hook = ip_vs_forward_icmp,
-
.owner = THIS_MODULE,
-
.pf = PF_INET,
-
.hooknum = NF_INET_FORWARD,
-
.priority = 99,
-
},
-
/* Before the netfilter connection tracking, exit from POST_ROUTING */
-
{
-
.hook = ip_vs_post_routing,
-
.owner = THIS_MODULE,
-
.pf = PF_INET,
-
.hooknum = NF_INET_POST_ROUTING,
-
.priority = NF_IP_PRI_NAT_SRC-1,
-
},
-
#ifdef CONFIG_IP_VS_IPV6
-
/* After packet filtering, forward packet through VS/DR, VS/TUN,
-
* or VS/NAT(change destination), so that filtering rules can be
-
* applied to IPVS. */
-
{
-
.hook = ip_vs_in,
-
.owner = THIS_MODULE,
-
.pf = PF_INET6,
-
.hooknum = NF_INET_LOCAL_IN,
-
.priority = 100,
-
},
-
/* After packet filtering, change source only for VS/NAT */
-
{
-
.hook = ip_vs_out,
-
.owner = THIS_MODULE,
-
.pf = PF_INET6,
-
.hooknum = NF_INET_FORWARD,
-
.priority = 100,
-
},
-
/* After packet filtering (but before ip_vs_out_icmp), catch icmp
-
* destined for 0.0.0.0/0, which is for incoming IPVS connections */
-
{
-
.hook = ip_vs_forward_icmp_v6,
-
.owner = THIS_MODULE,
-
.pf = PF_INET6,
-
.hooknum = NF_INET_FORWARD,
-
.priority = 99,
-
},
-
/* Before the netfilter connection tracking, exit from POST_ROUTING */
-
{
-
.hook = ip_vs_post_routing,
-
.owner = THIS_MODULE,
-
.pf = PF_INET6,
-
.hooknum = NF_INET_POST_ROUTING,
-
.priority = NF_IP6_PRI_NAT_SRC-1,
-
},
-
#endif
- };
4.2 ip_vs_in函数
当远端的客户端发送数据到达服务器时,也就是LVS Server时,该数据包的目的IP地址就是到达本地的,所以会先进入NF_LOCAL_IN的HOOK点,进行本地处理。点击(此处)折叠或打开
-
static unsigned int
-
ip_vs_in(unsigned int hooknum, struct sk_buff *skb,
-
const struct net_device *in, const struct net_device *out,
-
int (*okfn)(struct sk_buff *))
-
{
-
struct ip_vs_iphdr iph;
-
struct ip_vs_protocol *pp;
-
struct ip_vs_conn *cp;
-
int ret, restart, af, pkts;
-
//判断是IPV4还是IPV6协议
-
af = (skb->protocol == htons(ETH_P_IP)) ? AF_INET : AF_INET6;
-
/*获取到IP头*/
-
ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
-
-
/*
-
* Big tappo: only PACKET_HOST, including loopback for local client
-
* Don't handle local packets on IPv6 for now
-
判断是否是达到本机的*/
-
if (unlikely(skb->pkt_type != PACKET_HOST)) {
-
IP_VS_DBG_BUF(12, "packet type=%d proto=%d daddr=%s ignored\n",
-
skb->pkt_type,
-
iph.protocol,
-
IP_VS_DBG_ADDR(af, &iph.daddr));
-
return NF_ACCEPT;
-
}
-
/*对IPV6的判断忽略*/
-
#ifdef CONFIG_IP_VS_IPV6
-
if (af == AF_INET6) {
-
if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
-
int related, verdict = ip_vs_in_icmp_v6(skb, &related, hooknum);
-
-
if (related)
-
return verdict;
-
ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
-
}
-
} else
-
#endif
-
/*判断是否是ICMP报文,*/
-
if (unlikely(iph.protocol == IPPROTO_ICMP)) {
-
int related, verdict = ip_vs_in_icmp(skb, &related, hooknum);
-
-
if (related)
-
return verdict;
-
ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
-
}
-
-
/* Protocol supported? 判断LVS支持的协议,目前支持TCP、UDP、sctp、ah、esp */
-
pp = ip_vs_proto_get(iph.protocol);
-
if (unlikely(!pp))
-
return NF_ACCEPT;
-
-
/*
-
* Check if the packet belongs to an existing connection entry,检查该数据包是否已经属于已经存在的连接实例,对于第一个包,这里返回NULL,这里对应的回调函数为ip_vs_conn_in_get_proto,该函数主要是根据源IP,目的IP,源端口和目的端口,协议号来查找。 */
-
cp = pp->conn_in_get(af, skb, pp, &iph, iph.len, 0);
-
/*对于新到来的数据包包,查找不到cp为空,进入下面的if处理*/
-
if (unlikely(!cp)) {
-
int v;
-
-
/* For local client packets, it could be a response 对于TCP这里调用的函数为ip_vs_conn_out_get_proto 新的内核版本已经没有了response的处理,这里不作分析*/
-
cp = pp->conn_out_get(af, skb, pp, &iph, iph.len, 0);
-
if (cp)
-
return handle_response(af, skb, pp, cp, iph.len); /*这里查找是否是出去的报文,if (res_dir == IP_VS_CIDX_F_IN2OUT) {
-
return handle_response(af, skb, pp, cp, iph.len);
-
} 做SNAT*/
-
/*没有查找到connection ,创建新的connection ,这里的函数为tcp_conn_schedule */
-
if (!pp->conn_schedule(af, skb, pp, &v, &cp))
-
return v;
-
}
-
-
if (unlikely(!cp)) {
-
/* sorry, all this trouble for a no-hit :) 到这里说明创建也没有成功,直接返回ACCEPT*/
-
IP_VS_DBG_PKT(12, pp, skb, 0,
-
"packet continues traversal as normal");
-
return NF_ACCEPT;
-
}
-
-
IP_VS_DBG_PKT(11, pp, skb, 0, "Incoming packet");
-
-
/* Check the server status 检查server的状态,如果cp->dest不为空,但是服务器不可用,则直接丢弃该数据包*/
-
if (cp->dest && !(cp->dest->flags & IP_VS_DEST_F_AVAILABLE)) {
-
/* the destination server is not available */
-
-
if (sysctl_ip_vs_expire_nodest_conn) {
-
/* try to expire the connection immediately */
-
ip_vs_conn_expire_now(cp);
-
}
-
/* don't restart its timer, and silently
-
drop the packet. 引用计数减一 */
-
__ip_vs_conn_put(cp);
-
return NF_DROP;
-
}
-
/*更新统计和状态信息*/
-
ip_vs_in_stats(cp, skb);
-
/*设置现在的状态为IP_VS_DIR_INPUT,调用的函数为,最终调用的函数为tcp_state_transition */
-
restart = ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pp);
-
if (cp->packet_xmit)
-
ret = cp->packet_xmit(skb, cp, pp); /*调用发包函数,对于NAT模式来说这里的发包函数为ip_vs_nat_xmit */
-
/* do not touch skb anymore */
-
else {
-
IP_VS_DBG_RL("warning: packet_xmit is null");
-
ret = NF_ACCEPT;
-
}
-
-
/* Increase its packet counter and check if it is needed
-
* to be synchronized
-
*
-
* Sync connection if it is about to close to
-
* encorage the standby servers to update the connections timeout
-
增加发送的包的数量,并进行同步*/
-
pkts = atomic_add_return(1, &cp->in_pkts);
-
if (af == AF_INET && (ip_vs_sync_state & IP_VS_STATE_MASTER) &&
-
cp->protocol == IPPROTO_SCTP) {
-
if ((cp->state == IP_VS_SCTP_S_ESTABLISHED &&
-
(atomic_read(&cp->in_pkts) %
-
sysctl_ip_vs_sync_threshold[1]
-
== sysctl_ip_vs_sync_threshold[0])) ||
-
(cp->old_state != cp->state &&
-
((cp->state == IP_VS_SCTP_S_CLOSED) ||
-
(cp->state == IP_VS_SCTP_S_SHUT_ACK_CLI) ||
-
(cp->state == IP_VS_SCTP_S_SHUT_ACK_SER)))) {
-
ip_vs_sync_conn(cp);//这里进行同步,同步主要是同步备份LVS server,这里是针对SCTP协议
-
goto out;
-
}
-
}
-
-
if (af == AF_INET &&
-
(ip_vs_sync_state & IP_VS_STATE_MASTER) &&
-
(((cp->protocol != IPPROTO_TCP ||
-
cp->state == IP_VS_TCP_S_ESTABLISHED) &&
-
(pkts % sysctl_ip_vs_sync_threshold[1]
-
== sysctl_ip_vs_sync_threshold[0])) ||
-
((cp->protocol == IPPROTO_TCP) && (cp->old_state != cp->state) &&
-
((cp->state == IP_VS_TCP_S_FIN_WAIT) ||
-
(cp->state == IP_VS_TCP_S_CLOSE_WAIT) ||
-
(cp->state == IP_VS_TCP_S_TIME_WAIT)))))
-
ip_vs_sync_conn(cp); //这里进行同步,同步主要是同步备份LVS server,这里是针对TCP协议
-
-
out:
-
cp->old_state = cp->state;
-
-
ip_vs_conn_put(cp);
-
return ret;
- }
4.2.1 tcp_conn_schedule
在该函数中调用调度策略函数,按照调度策略找到真正的real Server。
点击(此处)折叠或打开
-
static int
-
tcp_conn_schedule(int af, struct sk_buff *skb, struct ip_vs_protocol *pp,
-
int *verdict, struct ip_vs_conn **cpp)
-
{
-
struct ip_vs_service *svc;
-
struct tcphdr _tcph, *th;
-
struct ip_vs_iphdr iph;
-
-
ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
-
-
th = skb_header_pointer(skb, iph.len, sizeof(_tcph), &_tcph);
-
if (th == NULL) {
-
*verdict = NF_DROP;
-
return 0;
-
}
-
/*根据协议号和目的地址、目的端口号查找到ip_vs_service 实例*/
-
if (th->syn &&
-
(svc = ip_vs_service_get(af, skb->mark, iph.protocol, &iph.daddr,
-
th->dest))) {
-
if (ip_vs_todrop()) {
-
/*
-
* It seems that we are very loaded.
- &nbs