文章目錄
- 1、ip_vs_service結構
- 2、ip_vs_dest結構
- 3、ip_vs_conn結構
IPVS是基於Netfilter架構實現的核心模組。它實現了IP層的負載平衡功能。IPVS完全在核心態實現,效率非常高。IPVS的官方網站:www.linuxvirtualserver.org。 首先來看一下IPVS模組的一些重要資料結構。 1、ip_vs_service結構
ip_vs_service結構代表的是一個IPVS服務物件,它包含了IPVS服務的監聽地址、連接埠、協議和調度演算法等資訊。
/* * The information about the virtual service offered to the net * and the forwarding entries */ struct ip_vs_service { struct list_head s_list; /* for normal service table */ struct list_head f_list; /* for fwmark-based service table */ atomic_t refcnt; /* reference counter */ atomic_t usecnt; /* use counter */
__u16 protocol; /* which protocol (TCP/UDP) */ __u32 addr; /* IP address for virtual service */ __u16 port; /* port number for the service */ __u32 fwmark; /* firewall mark of the service */ unsigned flags; /* service status flags */ unsigned timeout; /* persistent timeout in ticks */ __u32 netmask; /* grouping granularity */
struct list_head destinations; /* real server d-linked list */ __u32 num_dests; /* number of servers */
struct ip_vs_stats stats; /* statistics for the service */ struct ip_vs_app *inc; /* bind conns to this app inc */
/* for scheduling */ struct ip_vs_scheduler *scheduler; /* bound scheduler object */ rwlock_t sched_lock; /* lock sched_data */ void *sched_data; /* scheduler application data */ };
|
s_list是全域hash鏈表ip_vs_svc_table的一個節點。ip_vs_svc_table是這樣定義的:
/* the service table hashed by <protocol, addr, port> */ static struct list_head ip_vs_svc_table[IP_VS_SVC_TAB_SIZE];
|
它是一個數組,每個成員是一個鏈表頭。將ip_vs_service對象的協議類型、地址和連接埠進行hash,hash值作為數組下標,然後將此對象置入數群組成員對應的鏈表中。
refcnt和usecnt分別是ip_vs_service對象的引用計數和使用計數。它們是atomic_t類型的變數,使用
atomic_XXX系列函數對它們進行操作。refcnt在對象建立時為0,當ip_vs_service對象被加入鏈表或從鏈表刪除,或者被
ip_vs_dest對象引用時,refcnt相應地增或減1。usecnt初始化為1,然後隨著__ip_vs_service_get和
ip_vs_service_put的對稱調用,usecnt相應地增減1。
protocol是ip_vs_service對象的協議類型,其值為IPPROTO_XXX,如IPPROTO_TCP等。
addr和port為ip_vs_service對象的監聽地址和連接埠,網路位元組序。
flags為ip_vs_service對象的狀態標誌位,可以取IP_VS_SVC_F_PERSISTENT和
IP_VS_SVC_F_HASHED。前者表示IPVS服務使用了基於IP地址的會話保持,即同一IP地址發起的串連將被負載到同一台真實伺服器上。後
者表示ip_vs_service對象已被加入到ip_vs_svc_table鏈表中。
timeout和netmask只有在IP_VS_SVC_F_PERSISTENT標誌位被設定時才有效。timeout是會話的逾時時間,超過
此時間後,會話將不再有效。同一IP地址發起的兩個串連,如果間隔超過此時間,則未必會被負載到同一台真實伺服器上。netmask可以將會話保持設定成
基於IP網段的,即同一網段發起的串連將被負載到同一台真實伺服器上。
destinations是ip_vs_dest對象鏈表,它代指了IPVS服務對應的真實伺服器列表。num_dests是伺服器個數。
scheduler指向了一個ip_vs_scheduler對象,它代指一種調度演算法。sched_lock和sched_data是ip_vs_scheduler對象的鎖和私人資料。
2、ip_vs_dest結構ip_vs_dest結構代表的是一台真實伺服器,它包含了真實伺服器的地址、連接埠和權重等資訊。
/* * The real server destination forwarding entry * with ip address, port number, and so on. */ struct ip_vs_dest { struct list_head n_list; /* for the dests in the service */ struct list_head d_list; /* for table with all the dests */
__u32 addr; /* IP address of the server */ __u16 port; /* port number of the server */ volatile unsigned flags; /* dest status flags */ atomic_t conn_flags; /* flags to copy to conn */ atomic_t weight; /* server weight */
atomic_t refcnt; /* reference counter */ struct ip_vs_stats stats; /* statistics */
/* connection counters and thresholds */ atomic_t activeconns; /* active connections */ atomic_t inactconns; /* inactive connections */ atomic_t persistconns; /* persistent connections */ __u32 u_threshold; /* upper threshold */ __u32 l_threshold; /* lower threshold */
/* for destination cache */ spinlock_t dst_lock; /* lock of dst_cache */ struct dst_entry *dst_cache; /* destination cache entry */ u32 dst_rtos; /* RT_TOS(tos) for dst */
/* for virtual service */ struct ip_vs_service *svc; /* service it belongs to */ __u16 protocol; /* which protocol (TCP/UDP) */ __u32 vaddr; /* virtual IP address */ __u16 vport; /* virtual port number */ __u32 vfwmark; /* firewall mark of service */ };
|
n_list是ip_vs_service對象中destinations鏈表中的一個節點,表示此
ip_vs_dest對象是IPVS服務的一個伺服器節點。IPVS中有一個全域的ip_vs_dest對象鏈表ip_vs_dest_trash。每當
要銷毀ip_vs_dest對象時,如果其引用計數不為0,則暫時將此ip_vs_dest對象置入鏈表ip_vs_dest_trash,留待以後銷毀
或者重新使用。
d_list是全域hash鏈表ip_vs_rtable的一個節點。
addr和port是ip_vs_dest對象的真實伺服器位址和連接埠,網路位元組序。
flags是ip_vs_dest對象的狀態標誌位,IP_VS_DEST_F_AVAILABLE表示此真實伺服器可用,IP_VS_DEST_F_OVERLOAD表示此真實伺服器超負荷。
conn_flags是ip_vs_dest對象的串連標誌位。這些標誌位本身不是用來標示
ip_vs_dest對象的,而是由ip_vs_dest對象建立ip_vs_conn對象時,賦給後者的。IP_VS_CONN_F_MASQ、
IP_VS_CONN_F_TUNNEL和IP_VS_CONN_F_DROUTE,分別代表NAT、TUN和DR三種模式。後兩種模式也暗含了
IP_VS_CONN_F_NOOUTPUT標誌位。
weight是ip_vs_dest對象的權重,用於調度。
refcnt是ip_vs_dest對象的引用計數,初值為0,當對象被加入鏈表或從鏈表刪除,或者被ip_vs_conn對象引用時,refcnt相應地增或減1。
svc、protocol、vaddr、vport分別是相應的ip_vs_service對象的參數。
3、ip_vs_conn結構
ip_vs_conn結構代表的是一個連線物件,它包含用戶端、IPVS、真實伺服器的地址和連接埠等資訊。
/* * IP_VS structure allocated for each dynamically scheduled connection */ struct ip_vs_conn { struct list_head c_list; /* hashed list heads */
/* Protocol, addresses and port numbers */ __u32 caddr; /* client address */ __u32 vaddr; /* virtual address */ __u32 daddr; /* destination address */ __u16 cport; __u16 vport; __u16 dport; __u16 protocol; /* Which protocol (TCP/UDP) */
/* counter and timer */ atomic_t refcnt; /* reference count */ struct timer_list timer; /* Expiration timer */ volatile unsigned long timeout; /* timeout */
/* Flags and state transition */ spinlock_t lock; /* lock for state transition */ volatile __u16 flags; /* status flags */ volatile __u16 state; /* state info */
/* Control members */ struct ip_vs_conn *control; /* Master control connection */ atomic_t n_control; /* Number of controlled ones */ struct ip_vs_dest *dest; /* real server */ atomic_t in_pkts; /* incoming packet counter */
/* packet transmitter for different forwarding methods. If it mangles the packet, it must return NF_DROP or better NF_STOLEN, otherwise this must be changed to a sk_buff **. */ int (*packet_xmit)(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
/* Note: we can group the following members into a structure, in order to save more space, and the following members are only used in VS/NAT anyway */ struct ip_vs_app *app; /* bound ip_vs_app object */ void *app_data; /* Application private data */ struct ip_vs_seq in_seq; /* incoming seq. struct */ struct ip_vs_seq out_seq; /* outgoing seq. struct */ };
|
c_list是全域hash鏈表ip_vs_conn_tab的一個節點。
caddr、vaddr、daddr、cport、vport、dport分別是用戶端、IPVS、真實伺服器的地址和連接埠。
protocol是ip_vs_conn對象的協議類型。
refcnt是ip_vs_conn對象的使用計數。其初值為1,__ip_vs_conn_in_get/__ip_vs_conn_put成對調用,或者對象置入鏈表中,都會影響到此計數值。
timer是ip_vs_conn對象的生存期,當timer到期時,對象被銷毀。
timeout是ip_vs_conn對象動態逾時時間,每當對象操作完畢,timeout值用來更新timer,以延長對象的生存期。timeout受串連狀態等的影響。
flags是ip_vs_conn對象的標誌位,其中IP_VS_CONN_F_MASQ、IP_VS_CONN_F_TUNNEL、
IP_VS_CONN_F_DROUTE和IP_VS_CONN_F_NOOUTPUT標誌位在介紹ip_vs_dest對象時已經解釋過了。
IP_VS_CONN_F_HASHED標誌位表示此ip_vs_conn對象已經被添加到全域hash鏈表ip_vs_conn_tab中。
IP_VS_CONN_F_INACTIVE標誌位表示當前串連為非使用中,每個ip_vs_conn對象在建立時都會指定此標誌。
IP_VS_CONN_F_NO_CPORT標誌位用於一些特殊協議,如ftp等。IP_VS_CONN_F_TEMPLATE標誌位用於實現基於IP地
址的會話保持。
state是ip_vs_conn對象用來記錄自己的狀態的。
control指向ip_vs_conn對象的父連線物件,它與IP_VS_CONN_F_TEMPLATE的作用一樣,用於實現基於IP地址的會話保持。
dest指向此連線物件對應的ip_vs_dest對象。
packet_xmit為傳輸函數,NAT、TUN和DR模式分別對應不同的傳輸函數。