您的位置:首页 > 运维架构 > Linux

linux路由内核实现分析(一)----邻居子节点(1)

2013-07-10 12:13 519 查看
------------------------------------------------------------------------------------------

以下是我根据 linux-2.6.23.9版本内核源代码所做阅读笔记,属个人兴趣而为,希望找到有共同兴趣

的朋友一起讨论和研究,有谬误之处,笔者水平有限,欢迎大家拍砖:)

------------------------------------------------------------------------------------------

有三种路由结构:

1,neigh_table{}结构和neighbour{}结构
 存储和本机物理上相邻的主机地址信息表,通常称为邻居子节点,指的是和本机相邻只有
 一跳的机器,其中neigh_table{}作为数据结构链表来表示neighbour{}表示相邻的机器节点

2,路由规则的存储,判断了一个到达一个网络地址必须经过怎样的路由,使用fib_table来表示

3,提供了路由地址的缓存机制,使用rtable链表来表示.

neigh_table结构

struct neigh_table
{
struct neigh_table *next;
int family;
int entry_size;
int key_len;
__u32 (*hash)(const void *pkey, const struct net_device *);
int (*constructor)(struct neighbour *);
int (*pconstructor)(struct pneigh_entry *);
void (*pdestructor)(struct pneigh_entry *);
void (*proxy_redo)(struct sk_buff *skb);
char *id;
struct neigh_parms parms;

int gc_interval;
int gc_thresh1;
int gc_thresh2;
int gc_thresh3;
unsigned long last_flush;
struct timer_list gc_timer;
struct timer_list proxy_timer;
struct sk_buff_head proxy_queue;
atomic_t entries;
rwlock_t lock;
unsigned long last_rand;
struct kmem_cache *kmem_cachep;
struct neigh_statistics *stats;
struct neighbour **hash_buckets;
unsigned int hash_mask;
__u32 hash_rnd;
unsigned int hash_chain_gc;
struct pneigh_entry **phash_buckets;
#ifdef CONFIG_PROC_FS
struct proc_dir_entry *pde;
#endif
};

struct proc_dir_entry *pde
这个成员是linux 2.6中添加了对proc文件系统的支持,但是我没有找到proc文件系统中
有直接相关于邻居节点的信息,估计是和其他结构一起向用户态提供了路由信息,有可能
是输出到/proc/net/route里面.

struct neigh_table *next; //下一个邻居表,实际上就是ARP报文到达的下一台机器

int family;//地址族,对于以太网而言就是 AF_INET

int entry_size; //入口长度,也就是一个邻居结构的大小,初始化为sizeof(neighbour)+4(4为一个IP地址的长度)

//哈希关键值长度 即IP地址的长度,为4
int key_len;

__u32 (*hash)(const void *pkey, const struct net_device *);构造出存放和检索这个neigh_table的neighbour的哈希函数  

//允许邻居的上限,根据网络的类型,大小会有所变化,例如C类地址,邻居限制就应该小于255
int gc_thresh3

//哈希数组,存入其中的邻居,在一个neigh_table里面,最多可以有32个neighbour结构的链表.
struct neighbour **hash_buckets;
int entries //整个neigh_table中邻居的数量
unsigned int hash_mask; //哈希数组大小的掩码

neighbour结构

struct neighbour
{
struct neighbour *next;
struct neigh_table *tbl;
struct neigh_parms *parms;
struct net_device *dev;
unsigned long used;
unsigned long confirmed;
unsigned long updated;
__u8 flags;
__u8 nud_state;
__u8 type;
__u8 dead;
atomic_t probes;
rwlock_t lock;
unsigned char ha[(MAX_ADDR_LEN+sizeof(unsigned long)-1)&~(sizeof(unsigned long)-1)];
struct hh_cache *hh;
atomic_t refcnt;
int (*output)(struct sk_buff *skb);
struct sk_buff_head arp_queue;
struct timer_list timer;
struct neigh_ops *ops;
u8 primary_key[0];
};

struct neigh_table *tbl;//所在的邻居表,指向上层的neigh_table结构
struct net_device *dev;//邻居所对应的网络设备接口指针

int (*output)(struct sk_buff *skb);//找到合适的邻居节点之后,系统将调用这个函数指针,
使用结构中的dev设备,将数据包发送出去,如果协议
               族是AF_INET,将调用dev_queue_xmit函数发送数据

u8 primary_key[0];//哈希关键字

//这段代码完成函数指针的转换(net/ipv4/arp.c)
static struct neigh_ops arp_hh_ops = {
.family = AF_INET,
.solicit = arp_solicit,
.error_report = arp_error_report,
.output = neigh_resolve_output,
.connected_output = neigh_resolve_output,
.hh_output = dev_queue_xmit,
.queue_xmit = dev_queue_xmit,
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: