内核模块链表奇怪的行为

时间:2017-09-09 13:16:32

标签: c kernel

我在内核模块中创建一个链表;添加第一条记录没有问题,在init模块中我也使用create map function初始化地图;

typedef struct IP4SYN{    
    unsigned int KaynakIP;
    u_int16_t KaynakPort;
    u_int16_t HedefPort;
} IP4SYN;

typedef struct IP4Map{
    unsigned int HedefIP;
    IP4SYN * a;
    size_t s;
    size_t n;
} IP4Map;

typedef struct Sessions{
    IP4Map * a;
    size_t s;
    size_t n;
} Sessions;
static size_t blocksize = (1024*102);

Sessions * Session_Map;

static unsigned int get_tsval(void){    
    return (unsigned int)(ktime_to_ns(ktime_get())>>10);
}
Sessions * CreateMap(size_t initial){
    Sessions * pMap = (Sessions *)vmalloc(sizeof(Sessions));
    if (initial > 0 && blocksize != initial) {
        blocksize = initial;
    }
    pMap->a = (IP4Map *)vmalloc(sizeof(IP4Map)*blocksize);
    memset(pMap->a, 0, sizeof(IP4Map)*blocksize);
    pMap->s = blocksize;  
    pMap->n = 0;  

    pMap->a->a=(IP4SYN *)vmalloc(sizeof(IP4SYN)*blocksize);
    memset(pMap->a->a, 0, sizeof(IP4SYN)*blocksize);
    pMap->a->s = blocksize;  
    pMap->a->n = 0;  
    return pMap;   
}
size_t HedefIPKayitNo(unsigned int HedefIP){
    size_t beg = 0; 
    size_t end = Session_Map->n;
    for (beg; beg < end; beg) {
        IP4Map map = Session_Map->a[beg];
        if(map.HedefIP==HedefIP){
            return beg;
            break;
        }
    }
    return beg;
}   
size_t YeniHedefIP(unsigned int HedefIP,size_t index){
    Session_Map->a[index].HedefIP = HedefIP;
    Session_Map->n++;
}

我想检查列表中是否存在ip,如果未列出,我想将其添加到列表中。但奇怪的是它在第二次尝试时导致缺乏内核。第一个加入是完美的。应该是什么问题?

size_t IP_Index = HedefIPKayitNo(hedef_ip);
        if(IP_Index == Session_Map->n ){
                YeniHedefIP(hedef_ip,(IP_Index));

}

0 个答案:

没有答案