跳至主要内容

【转】Linux ARP缓存表

arp_tbl是一个类型为struct neigh_table的全局变量,它是一个ARP的缓存表,也称为邻居表。协议栈通过ARP协议获取到的网络上邻居主机的IP地址与MAC地址的对应关 系都会保存在这个表中,以备下次与邻居通讯时使用,同时,ARP模块自身也会提供一套相应的机制来更新和维护这个邻居表。下面逐个分析arp_tbl中的 重要成员数据与函数。
    entry_size,key_len,kmem_cachep。
    entry_size是一个入口的大小,也就是arp_tbl中一个邻居的大小,邻居用struct neighbour结构体表示,该结构体的最后一个成员是u8 primary_key[0],用于存放IP地址,作为这个邻居的哈希主键。所以entry_size的大小就是sizeof(struct neighbour) + 4,因为是用IP地址作主键,所以key_len就是4。kmem_cachep是一个后备高速缓存,创建一个邻居需要的内存从这个后备高速缓存中去取。
    hash_buckets,hash_mask,entries,hash。
    hash_buckets是一个哈希数组,里面存放了arp_tbl当前维护的所有的邻居,hash_mask是哈希数组大小的掩码,其初始值为1,所以 hash_buckets的初始大小为2(0到hash_mask的空间范围)。entries是整个arp_tbl中邻居的数量,当entries大于 hash_mask+1的时候,hash_buckets增长为原来的两部。成员hash是一个哈希函数指针,用于计算哈希值。
    phash_buckets,PNEIGH_HASHMASK。
    这是用于代理ARP的邻居哈希表,PNEIGH_HASHMASK固定为0xF,所以phash_buckets固定有16项,其它与hash_buckets相同。
    id。
    id作为这个邻居表的一个名称,是一个字符串信息,内核协议栈的arp_tbl的id是arp_cache。
    gc_interval,gc_thresh1,gc_thresh2,gc_thresh3。
    gc_thresh3是arp_tbl中允许拥有的邻居数量的上限,一旦超过这个上限,并且表中没有可以清理掉的垃圾邻居,那么就无法创建新的邻居,这个 值缺省被置为1024。gc_thresh2是第二个阀值,如果表中的邻居数量超过这个阀值,并且在需要创建新的邻居时,发现已经超过5秒时间表没有被刷 新过,则必须立即刷新arp_tbl表,进行强制垃圾回收,这个值缺省被置为512。gc_thresh1的用途暂时还没有发现,它缺省被置为128。 gc_interval应该是常规的垃圾回收间隔时间,被缺省置为30秒,但目前在源代码中似乎没有看到它的应用。强制垃圾收集的工作即是把引用计数为 1,且状态中没有NUD_PERMANENT的邻居全部从arp_tbl表中删除。
    gc_timer。
    这是一个常规垃圾回收的定时器,其定时处理函数是neigh_periodic_timer。该定时器超时后,处理函数处理hash_buckets表中 的一项,下次超时后,再处理下一项,这里的垃圾回收比强制垃圾回收条件要宽松得多,如果邻居的状态为NUD_PERMANENT或 NUD_IN_TIMER(该邻居正在解析中),则不能回收。当邻居的引用计数为1时,并且邻居状态为NUD_FAILED(解析失败)或者该邻居距最近 一次被使用时间已超过参数表中gc_staletime的值(缺省为60秒),则可以作为垃圾回收。回收完毕后,要设置下一次进行回收的时间 (gc_timer的超时时间),下次回收时间为参数表中base_reachable_time的值(缺省设为30秒)的一半,再除以 hash_buckets哈希表中的项数。也就是,基本上15秒左右会把整个arp_tbl缓存表进行一次垃圾回收。
    proxy_timer,proxy_queue,proxy_redo。
    proxy_timer是一个关于代理ARP的定时器,proxy_queue是一个待处理的代理ARP数据包的队列,每次定时器超时,处理函数 neigh_proxy_process依次检查队列中每一个代理ARP数据包(struct sk_buff),对于超时,且满足相关条件的,调用proxy_redo进行处理。有关代理ARP,将专门分析讲述,这里暂时略过。
    constructor。
    这是一个邻居的初始化函数指针,每次创建出一个邻居后,需要马上调用这个函数对新创建的邻居进行一些初始化操作。邻居创建完,已经被赋于一个IP地址(邻 居结构体的primary_key成员),该函数首先根据这个IP地址来确定其地址类型,然后为邻居选择相应的操作函数集(初始化邻居结构体的一些成员, 在讲到邻居结构体内容时再进行分析)。
    pconstructor,pdestructor。
    这是代理ARP的邻居的构建和析构函数指针,在IPv4模块中,未提供这两个函数,所以它们的指针值为空。
    parms。
    这是一个结构体struct neigh_parms的链表,系统中每个网络设备接口对应链表中一个节点,表示该设备接口上的邻居的一些传输参数。同时,链表中还有一个缺省的项。
    last_rand,hash_rand
    这两个成员其实没有联系,hash_rand是用于邻居哈希表hash_buckets的一个随机数,last_rand用于记录一个时间,即上次为 parms链表中每个节点生成reachable_time的时间,reachable_time是需要被定时刷新的。
    stats。
    记录arp_tbl被操作次数的一些统计数据。 

    结构体struct neigh_table是一个哈希表,用于描述物理上互相连接的机器的信息。ARP缓存myarp_tbl就是这样的一个结构。在分析ARP相关的初始化之前,我们先来看一下这个结构体:
    truct 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;
        /* HACK. gc_* shoul follow parms without a gap! */
        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;
        kmem_cache_t        *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;

评论

此博客中的热门博文

【转】smb协议栈使用示例

/*  * * uncdownload.c  * *  * * Utility for downloading files from SMB shares using libsmbclient  * *  * * Copyright(C) 2006 Sophos Plc, Oxford, England.  * *  * * This program is free software; you can redistribute it and/or modify it under the terms of the  * * GNU General Public License Version 2 as published by the Free Software Foundation.  * *  * * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without  * * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  * * See the GNU General Public License for more details.  * *  * * You should have received a copy of the GNU General Public License along with this program; if not,  * * write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA  * *  * */ # include < sys / types . h > # include < sys / time . h > # include ...

【转】Ether Types

Ether Types (last updated 2008-09-09) NOTE: Please see [RFC5342] for current information and registration procedures. This registry will be revised soon and will be replaced with up-to-date information. Many of the networks of all classes are Ethernets (10Mb) or Experimental Ethernets (3Mb). These systems use a message "type" field in much the same way the ARPANET uses the "link" field. If you need an Ether Type, contact: IEEE Registration Authority IEEE Standards Department 445 Hoes Lane Piscataway, NJ 08854 Phone +1 732 562 3813 Fax: +1 732 562 1571 Email: <ieee-registration-authority& ieee.org > http://standards.ieee.org/regauth/index.html The following list of EtherTypes is contributed unverified information from various sources. Another list of EtherTypes is maintained by Michael A. Patton and is accessible at: <URL: http://www.cavebear.com/CaveBear/Ethernet/ > <URL: ftp://ftp.cavebear.com/pub/Ethernet-codes > Assign...

【转】udp编程实例

UDP通讯实例 2008-04-29 15:30:05 / 个人分类: linux C编程 UDP协议的几点好处: 1.UDP不要求保持一个连接; 2.UDP没有因接收方认可收到数据包(或者当数据包没有正确抵达而自动重传)而带来的开销; 3.设计UDP的目的是用于短应用和控制信息; 4.在一个数据包接一个数据包的基础上,UDP要求的 网络 带宽比TCP更小。 UDP的几个缺点: 1.程序员必须创建代码检测传输错误并进行重传(如果应用程序要求这样做); 2.程序员必须把大数据包分片。 code: <1> /* * sender.c--UDP protocol example */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> int port = 6789; int main() {     int socket_descrīptor;     int iter = 0;     char buf[80];     struct sockaddr_in address;     /* Initialize socket address structure for Interner Protocols */     bzero(&address, sizeof(address)); // empty data structure     address.sin_family = AF_INET;     address.sin_addr.s_addr = inet_addr("127.0.0.1");     address...