LVS 内核实现分析(3)

6370阅读 0评论2017-02-17 frankzfz
分类:云计算

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_FORWARDNF_INET_POST_ROUTINGLVS同时支持IPv4/IPv6两种协议。


点击(此处)折叠或打开

  1. static struct nf_hook_ops ip_vs_ops[] __read_mostly = {
  2.     /* After packet filtering, forward packet through VS/DR, VS/TUN,
  3.      * or VS/NAT(change destination), so that filtering rules can be
  4.      * applied to IPVS. */
  5.     {
  6.         .hook        = ip_vs_in,    
  7.         .owner        = THIS_MODULE,
  8.         .pf        = PF_INET,
  9.         .hooknum = NF_INET_LOCAL_IN,
  10.         .priority = 100,
  11.     },
  12.     /* After packet filtering, change source only for VS/NAT */
  13.     {
  14.         .hook        = ip_vs_out,
  15.         .owner        = THIS_MODULE,
  16.         .pf        = PF_INET,
  17.         .hooknum = NF_INET_FORWARD,
  18.         .priority = 100,
  19.     },
  20.     /* After packet filtering (but before ip_vs_out_icmp), catch icmp
  21.      * destined for 0.0.0.0/0, which is for incoming IPVS connections */
  22.     {
  23.         .hook        = ip_vs_forward_icmp,
  24.         .owner        = THIS_MODULE,
  25.         .pf        = PF_INET,
  26.         .hooknum = NF_INET_FORWARD,
  27.         .priority = 99,
  28.     },
  29.     /* Before the netfilter connection tracking, exit from POST_ROUTING */
  30.     {
  31.         .hook        = ip_vs_post_routing,
  32.         .owner        = THIS_MODULE,
  33.         .pf        = PF_INET,
  34.         .hooknum = NF_INET_POST_ROUTING,
  35.         .priority = NF_IP_PRI_NAT_SRC-1,
  36.     },
  37. #ifdef CONFIG_IP_VS_IPV6
  38.     /* After packet filtering, forward packet through VS/DR, VS/TUN,
  39.      * or VS/NAT(change destination), so that filtering rules can be
  40.      * applied to IPVS. */
  41.     {
  42.         .hook        = ip_vs_in,
  43.         .owner        = THIS_MODULE,
  44.         .pf        = PF_INET6,
  45.         .hooknum = NF_INET_LOCAL_IN,
  46.         .priority = 100,
  47.     },
  48.     /* After packet filtering, change source only for VS/NAT */
  49.     {
  50.         .hook        = ip_vs_out,
  51.         .owner        = THIS_MODULE,
  52.         .pf        = PF_INET6,
  53.         .hooknum = NF_INET_FORWARD,
  54.         .priority = 100,
  55.     },
  56.     /* After packet filtering (but before ip_vs_out_icmp), catch icmp
  57.      * destined for 0.0.0.0/0, which is for incoming IPVS connections */
  58.     {
  59.         .hook        = ip_vs_forward_icmp_v6,
  60.         .owner        = THIS_MODULE,
  61.         .pf        = PF_INET6,
  62.         .hooknum = NF_INET_FORWARD,
  63.         .priority = 99,
  64.     },
  65.     /* Before the netfilter connection tracking, exit from POST_ROUTING */
  66.     {
  67.         .hook        = ip_vs_post_routing,
  68.         .owner        = THIS_MODULE,
  69.         .pf        = PF_INET6,
  70.         .hooknum = NF_INET_POST_ROUTING,
  71.         .priority = NF_IP6_PRI_NAT_SRC-1,
  72.     },
  73. #endif
  74. };

4.2  ip_vs_in函数

当远端的客户端发送数据到达服务器时,也就是LVS Server时,该数据包的目的IP地址就是到达本地的,所以会先进入NF_LOCAL_IN的HOOK点,进行本地处理。

点击(此处)折叠或打开

  1. static unsigned int
  2. ip_vs_in(unsigned int hooknum, struct sk_buff *skb,
  3.      const struct net_device *in, const struct net_device *out,
  4.      int (*okfn)(struct sk_buff *))
  5. {
  6.     struct ip_vs_iphdr iph;
  7.     struct ip_vs_protocol *pp;
  8.     struct ip_vs_conn *cp;
  9.     int ret, restart, af, pkts;
  10.    //判断是IPV4还是IPV6协议
  11.     af = (skb->protocol == htons(ETH_P_IP)) ? AF_INET : AF_INET6;
  12.    /*获取到IP头*/
  13.     ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);

  14.     /*
  15.      *    Big tappo: only PACKET_HOST, including loopback for local client
  16.      *    Don't handle local packets on IPv6 for now
  17.      判断是否是达到本机的*/
  18.     if (unlikely(skb->pkt_type != PACKET_HOST)) {
  19.         IP_VS_DBG_BUF(12, "packet type=%d proto=%d daddr=%s ignored\n",
  20.              skb->pkt_type,
  21.              iph.protocol,
  22.              IP_VS_DBG_ADDR(af, &iph.daddr));
  23.         return NF_ACCEPT;
  24.     }
  25. /*对IPV6的判断忽略*/
  26. #ifdef CONFIG_IP_VS_IPV6
  27.     if (af == AF_INET6) {
  28.         if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
  29.             int related, verdict = ip_vs_in_icmp_v6(skb, &related, hooknum);

  30.             if (related)
  31.                 return verdict;
  32.             ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
  33.         }
  34.     } else
  35. #endif
  36. /*判断是否是ICMP报文,*/
  37.         if (unlikely(iph.protocol == IPPROTO_ICMP)) {
  38.             int related, verdict = ip_vs_in_icmp(skb, &related, hooknum);

  39.             if (related)
  40.                 return verdict;
  41.             ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
  42.         }

  43.     /* Protocol supported? 判断LVS支持的协议,目前支持TCP、UDP、sctp、ah、esp    */
  44.     pp = ip_vs_proto_get(iph.protocol);
  45.     if (unlikely(!pp))
  46.         return NF_ACCEPT;

  47.     /*
  48.      * Check if the packet belongs to an existing connection entry,检查该数据包是否已经属于已经存在的连接实例,对于第一个包,这里返回NULL,这里对应的回调函数为ip_vs_conn_in_get_proto,该函数主要是根据源IP,目的IP,源端口和目的端口,协议号来查找。 */
  49.     cp = pp->conn_in_get(af, skb, pp, &iph, iph.len, 0);
  50. /*对于新到来的数据包包,查找不到cp为空,进入下面的if处理*/
  51.     if (unlikely(!cp)) {
  52.         int v;

  53.         /* For local client packets, it could be a response 对于TCP这里调用的函数为ip_vs_conn_out_get_proto 新的内核版本已经没有了response的处理,这里不作分析*/
  54.         cp = pp->conn_out_get(af, skb, pp, &iph, iph.len, 0);
  55.         if (cp)
  56.             return handle_response(af, skb, pp, cp, iph.len); /*这里查找是否是出去的报文,if (res_dir == IP_VS_CIDX_F_IN2OUT) {
  57.             return handle_response(af, skb, pp, cp, iph.len);
  58.         } 做SNAT*/
  59.     /*没有查找到connection ,创建新的connection ,这里的函数为tcp_conn_schedule */
  60.         if (!pp->conn_schedule(af, skb, pp, &v, &cp))
  61.             return v;
  62.     }

  63.     if (unlikely(!cp)) {
  64.         /* sorry, all this trouble for a no-hit :) 到这里说明创建也没有成功,直接返回ACCEPT*/
  65.         IP_VS_DBG_PKT(12, pp, skb, 0,
  66.              "packet continues traversal as normal");
  67.         return NF_ACCEPT;
  68.     }

  69.     IP_VS_DBG_PKT(11, pp, skb, 0, "Incoming packet");

  70.     /* Check the server status 检查server的状态,如果cp->dest不为空,但是服务器不可用,则直接丢弃该数据包*/
  71.     if (cp->dest && !(cp->dest->flags & IP_VS_DEST_F_AVAILABLE)) {
  72.         /* the destination server is not available */

  73.         if (sysctl_ip_vs_expire_nodest_conn) {
  74.             /* try to expire the connection immediately */
  75.             ip_vs_conn_expire_now(cp);
  76.         }
  77.         /* don't restart its timer, and silently
  78.          drop the packet. 引用计数减一 */
  79.         __ip_vs_conn_put(cp);
  80.         return NF_DROP;
  81.     }
  82.    /*更新统计和状态信息*/
  83.     ip_vs_in_stats(cp, skb);
  84.   /*设置现在的状态为IP_VS_DIR_INPUT,调用的函数为,最终调用的函数为tcp_state_transition */
  85.     restart = ip_vs_set_state(cp, IP_VS_DIR_INPUT, skb, pp);
  86.     if (cp->packet_xmit)
  87.         ret = cp->packet_xmit(skb, cp, pp); /*调用发包函数,对于NAT模式来说这里的发包函数为ip_vs_nat_xmit */
  88.         /* do not touch skb anymore */
  89.     else {
  90.         IP_VS_DBG_RL("warning: packet_xmit is null");
  91.         ret = NF_ACCEPT;
  92.     }

  93.     /* Increase its packet counter and check if it is needed
  94.      * to be synchronized
  95.      *
  96.      * Sync connection if it is about to close to
  97.      * encorage the standby servers to update the connections timeout
  98.      增加发送的包的数量,并进行同步*/
  99.     pkts = atomic_add_return(1, &cp->in_pkts);
  100.     if (af == AF_INET && (ip_vs_sync_state & IP_VS_STATE_MASTER) &&
  101.      cp->protocol == IPPROTO_SCTP) {
  102.         if ((cp->state == IP_VS_SCTP_S_ESTABLISHED &&
  103.             (atomic_read(&cp->in_pkts) %
  104.              sysctl_ip_vs_sync_threshold[1]
  105.              == sysctl_ip_vs_sync_threshold[0])) ||
  106.                 (cp->old_state != cp->state &&
  107.                  ((cp->state == IP_VS_SCTP_S_CLOSED) ||
  108.                  (cp->state == IP_VS_SCTP_S_SHUT_ACK_CLI) ||
  109.                  (cp->state == IP_VS_SCTP_S_SHUT_ACK_SER)))) {
  110.             ip_vs_sync_conn(cp);//这里进行同步,同步主要是同步备份LVS server,这里是针对SCTP协议
  111.             goto out;
  112.         }
  113.     }

  114.     if (af == AF_INET &&
  115.      (ip_vs_sync_state & IP_VS_STATE_MASTER) &&
  116.      (((cp->protocol != IPPROTO_TCP ||
  117.      cp->state == IP_VS_TCP_S_ESTABLISHED) &&
  118.      (pkts % sysctl_ip_vs_sync_threshold[1]
  119.      == sysctl_ip_vs_sync_threshold[0])) ||
  120.      ((cp->protocol == IPPROTO_TCP) && (cp->old_state != cp->state) &&
  121.      ((cp->state == IP_VS_TCP_S_FIN_WAIT) ||
  122.      (cp->state == IP_VS_TCP_S_CLOSE_WAIT) ||
  123.      (cp->state == IP_VS_TCP_S_TIME_WAIT)))))
  124.         ip_vs_sync_conn(cp); //这里进行同步,同步主要是同步备份LVS server,这里是针对TCP协议

  125. out:
  126.     cp->old_state = cp->state;

  127.     ip_vs_conn_put(cp);
  128.     return ret;
  129. }

4.2.1  tcp_conn_schedule

在该函数中调用调度策略函数,按照调度策略找到真正的real Server

点击(此处)折叠或打开

  1. static int
  2. tcp_conn_schedule(int af, struct sk_buff *skb, struct ip_vs_protocol *pp,
  3.          int *verdict, struct ip_vs_conn **cpp)
  4. {
  5.     struct ip_vs_service *svc;
  6.     struct tcphdr _tcph, *th;
  7.     struct ip_vs_iphdr iph;

  8.     ip_vs_fill_iphdr(af, skb_network_header(skb), &iph);
  9.    
  10.     th = skb_header_pointer(skb, iph.len, sizeof(_tcph), &_tcph);
  11.     if (th == NULL) {
  12.         *verdict = NF_DROP;
  13.         return 0;
  14.     }
  15.  /*根据协议号和目的地址、目的端口号查找到ip_vs_service 实例*/
  16.     if (th->syn &&
  17.      (svc = ip_vs_service_get(af, skb->mark, iph.protocol, &iph.daddr,
  18.                  th->dest))) {
  19.         if (ip_vs_todrop()) {
  20.             /*
  21.              * It seems that we are very loaded.
  22.         &nbs
上一篇:LVS 内核实现分析(2)
下一篇:TCP_BBR 算法笔记