没有合适的资源?快使用搜索试试~ 我知道了~
IPVS源代码阅读笔记_入门教程(负载均衡)PDF
5星 · 超过95%的资源 需积分: 23 65 下载量 77 浏览量
2009-04-05
00:02:28
上传
评论
收藏 853KB PDF 举报
温馨提示
试读
155页
本人整理的IPVS源代码学习笔记,很好的IPVS入门教程。PDF版,建立了完整的标签导航,方便阅读。
资源推荐
资源详情
资源评论
本文档的Copyleft归yfydz所有,使用GPL发布,可以自由拷贝,转载,转载时请保持文档的完
整性,严禁用于任何商业用途。
msn:
yfydz_no1@hotmail.com
来源:
http://yfydz.cublog.cn
PDF 文档制作:SahuSoft。sahumail@sina.com
1. 前言
ipvs 是章文嵩先生主持的一个开源项目,早在 2.2 内核时就已经以内核补丁形式出现,
RedHat6.1 和 6.0 的一个重要区别就是增加了 IPVS。从 2.4.24 后 IPVS 已经成为 Linux 官方
标准内核的一部分,2.2 时 IPVS 是完全独立的部分,2.4 以后借用了 netfilter 的一些处理机
制,但主体还是比较独立,但功能和 netfilter 有重复的地方。
IPVS 官方网站为: http://www.linuxvirtualserver.org
以下内核代码版本 2.6.17.11, ipvs 版本为 1.2.1。
2. IPVS 的外部表现
根据 LVS 官方网站的介绍,LVS 支持三种负载均衡模式:NAT,tunnel 和 direct routing(DR)。
NAT 是通用模式,所有交互数据必须通过均衡器;后两种则是一种半连接处理方式,请求
数据通过均衡器,而服务器的回应则是直接路由返回的,而这两种方法的区别是 tunnel 模式
下由于进行了 IP 封装所以可路由,而 DR 方式是修改 MAC 地址来实现,所以必须同一网
段。
3. 几个重要结构
3.1 协议
这个结构用来描述 IPVS 支持的 IP 协议。IPVS 的 IP 层协议支持 TCP, UDP, AH 和 ESP 这 4
种 IP 层协议
struct ip_vs_protocol {
// 链表中的下一项
struct ip_vs_protocol *next;
// 协议名称, "TCP", "UDP"...
char *name;
// 协议值: 6, 17, ...
__u16 protocol;
// 不进行分配
int dont_defrag;
// 协议应用计数器,也据是该协议的中多连接协议的数量
atomic_t appcnt; /* counter of proto app incs */
// 协议各状态的超时数组
int *timeout_table; /* protocol timeout table */
// 协议初始化
void (*init)(struct ip_vs_protocol *pp);
// 协议释放
void (*exit)(struct ip_vs_protocol *pp);
// 协议调度
int (*conn_schedule)(struct sk_buff *skb,
struct ip_vs_protocol *pp,
int *verdict, struct ip_vs_conn **cpp);
// 查找 in 方向的 IPVS 连接
struct ip_vs_conn *
(*conn_in_get)(const struct sk_buff *skb,
struct ip_vs_protocol *pp,
const struct iphdr *iph,
unsigned int proto_off,
int inverse);
// 查找 out 方向的 IPVS 连接
struct ip_vs_conn *
(*conn_out_get)(const struct sk_buff *skb,
struct ip_vs_protocol *pp,
const struct iphdr *iph,
unsigned int proto_off,
int inverse);
// 源 NAT 操作
int (*snat_handler)(struct sk_buff **pskb,
struct ip_vs_protocol *pp, struct ip_vs_conn *cp);
// 目的 NAT 操作
int (*dnat_handler)(struct sk_buff **pskb,
struct ip_vs_protocol *pp, struct ip_vs_conn *cp);
// 协议校验和计算
int (*csum_check)(struct sk_buff *skb, struct ip_vs_protocol *pp);
// 当前协议状态名称: 如"LISTEN", "ESTABLISH"...
const char *(*state_name)(int state);
// 协议状态迁移
int (*state_transition)(struct ip_vs_conn *cp, int direction,
const struct sk_buff *skb,
struct ip_vs_protocol *pp);
// 登记应用
int (*register_app)(struct ip_vs_app *inc);
// 去除应用登记
void (*unregister_app)(struct ip_vs_app *inc);
int (*app_conn_bind)(struct ip_vs_conn *cp);
// 数据包打印
void (*debug_packet)(struct ip_vs_protocol *pp,
const struct sk_buff *skb,
int offset,
const char *msg);
// 调整超时
void (*timeout_change)(struct ip_vs_protocol *pp, int flags);
// 设置各种状态下的协议超时
int (*set_state_timeout)(struct ip_vs_protocol *pp, char *sname, int to);
};
3.2 IPVS 连接
这个结构用来描述 IPVS 的连接。IPVS 的连接和 netfilter 定义的连接类似
/*
* IP_VS structure allocated for each dynamically scheduled connection
*/
struct ip_vs_conn {
// HASH 链表
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 */
// 主连接, 如 FTP
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 */
// IPVS 应用
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 */
};
3.3 IPVS 服务
这个结构用来描述 IPVS 对外的虚拟服务器信息。
/*
* The information about the virtual service offered to the net
* and the forwarding entries
*/
struct ip_vs_service {
// 按普通协议,地址,端口进行 HASH 的链表
struct list_head s_list; /* for normal service table */
// 按 nfmark 进行 HASH 的链表(感觉没必要)
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 */
// 就是 skb 中的 nfmark
__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 */
};
3.4 IPVS 目的服务器
这个结构用来描述具体的真实服务器的信息
/*
剩余154页未读,继续阅读
sahusoft
- 粉丝: 604
- 资源: 123
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 功率拓扑快速参考指南-ti,TI官方出品
- 开关电源拓朴图表,各种电路拓扑表格
- 登录和注册 前端:vue3+iview plus +axios 后台:spring boot +mybatis
- 软件测试入门简介:从基础到实践的全面介绍
- 2024CDA Level Ⅰ 认证考试大纲
- YOLO 数据集:淋巴病灶检测(1类别,包含训练集、验证集)
- 基于AT91RM9200处理器+XC3S250E(FPGA)+AD5322采集板Cadence ORCAD(硬件原理图+PCB)
- java+vue2实现zebra打印机,js实现
- J-link烧录软件,用于hex文件烧录
- VB打开时间同步页面/Internet时间设置.vbs
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页