linux中list.h中函數應用執行個體

來源:互聯網
上載者:User

這些程式碼片段展示如何使用linux核心模組,list,以及hash

===================tccounter.c=====================

#include <linux/init.h>

#include <linux/module.h>

#include <linux/moduleparam.h>

#include <linux/list.h>

#include <linux/jhash.h>

#include <linux/ctype.h>

#include <linux/proc_fs.h>

#include <asm/uaccess.h>

MODULE_LICENSE("GPL");

static unsigned int ip = 123456789;

module_param(ip, uint, 0);

static unsigned int port = 1;

module_param(port, uint, 0);

#define TCCOUNT_SIZE 255

struct tccount

{

    struct list_head list;

    unsigned int ip;

    unsigned short port;

    unsigned short counter;

};

struct tccount tclist[TCCOUNT_SIZE];

unsigned int gethash(unsigned int ip, unsigned int port)

{

     return jhash_2words(ip, port, 4)%TCCOUNT_SIZE;

}

int tccount_init(void)

{

    int i;

    for(i = 0; i < TCCOUNT_SIZE; i++ )

    {

       INIT_LIST_HEAD(&(tclist[i].list));

       tclist[i].ip = 0;

       tclist[i].port = 0;

       tclist[i].counter = 0;

    }

    return 0;

}

int addNewStream(unsigned int sip, unsigned int sport)

{

    unsigned int hash;

    struct tccount *stream;

    hash = gethash(sip, sport);

    list_for_each_entry(stream, &(tclist[hash].list), list)

    {

        if(stream && stream->ip == sip && stream->port == sport)

        {

            stream->counter++;

            return 0;

        }

    }

    stream = kmalloc(sizeof(struct tccount), GFP_KERNEL);

    stream->ip = sip;

    stream->port = sport;

    stream->counter = 1;

    list_add_rcu(&(stream->list), &(tclist[hash].list));

    return 0;

}

int delStream(unsigned int sip, unsigned int sport)

{

    unsigned int hash;

    struct tccount *stream;

    hash = gethash(sip, sport);

    list_for_each_entry(stream, &(tclist[hash].list), list)

    {

        if(stream->ip == sip && stream->port == sport)

        {

            stream->counter--;

            if(stream->counter == 0 && stream != &tclist[hash])

            {

                list_del_rcu(&(stream->list));

                kfree(&stream);

            }

        }

    }

    return 0;

}

unsigned short getStreamCount(unsigned int sip, unsigned int sport)

{

    unsigned int hash;

    struct tccount *stream;

    hash = gethash(sip, sport);

    list_for_each_entry(stream, &(tclist[hash].list), list)

    {

        if(stream->ip == sip && stream->port == sport)

            return stream->counter;

    }

    return 0;

}

/*debug proc*/

static int proc_show_tccount(struct file *f, const char *buf, unsigned long cnt, void *data)

{

    char input[32];

    int r;

    unsigned int ip;

    unsigned int port;

    unsigned short count;

    if (copy_from_user(input, buf, cnt) != 0)

        return -EFAULT;

    r = cnt;

    sscanf(input, "%u %u", &ip, &port) ;

    count = getStreamCount(ip, port);

    printk(KERN_ALERT "(%d, %d) counter:%u/n",ip, port, count);

    return cnt;

}

static int proc_set_tccount(struct file *f, const char *buf, unsigned long cnt, void *data)

{

    char input[32];

    int r;

    unsigned int ip;

    unsigned int port;

    unsigned int count;

    if (copy_from_user(input, buf, cnt) != 0)

        return -EFAULT;

    r = cnt;

    sscanf(input, "%u %u", &ip, &port) ;

    count = addNewStream(ip, port);

    printk(KERN_ALERT "(%d, %d) has been added/n", ip, port);

    return cnt;

}

static int proc_del_tccount(struct file *f, const char *buf, unsigned long cnt, void *data)

{

    char input[32];

    int r;

    unsigned int ip;

    unsigned int port;

    unsigned int count;

    if (copy_from_user(input, buf, cnt) != 0)

        return -EFAULT;

    r = cnt;

    sscanf(input, "%u %u", &ip, &port) ;

    count = delStream(ip, port);

    printk(KERN_ALERT "a stream of (%d, %d) has been deleted/n", ip, port);

    return cnt;

}

static int proc_add(void)

{

    char tmp[32];

    struct proc_dir_entry *proc_tcctl, *pentry;

    sprintf(tmp, "tccount");

    proc_tcctl = proc_mkdir(tmp, NULL);

    sprintf(tmp, "show");

    pentry = create_proc_entry(tmp, 0644, proc_tcctl);

    pentry->data        = NULL;

    pentry->read_proc   = NULL;

    pentry->write_proc  = proc_show_tccount;

    pentry->owner       = THIS_MODULE;

    sprintf(tmp, "del");

    pentry = create_proc_entry(tmp, 0644, proc_tcctl);

    pentry->data        = NULL;

    pentry->read_proc   = NULL;

    pentry->write_proc  = proc_del_tccount;

    pentry->owner       = THIS_MODULE;

    sprintf(tmp, "add");

    pentry = create_proc_entry(tmp, 0644, proc_tcctl);

    pentry->data        = NULL;

    pentry->read_proc   = NULL;

    pentry->write_proc  = proc_set_tccount;

    pentry->owner       = THIS_MODULE;

    return 0;

}

static int hello_init(void)

{

    tccount_init();

    proc_add();

    return 0;

}

static void hello_exit(void)

{

    printk("Goodbye!/n");

}

module_init(hello_init);

module_exit(hello_exit);

====================Makefile========================

 

obj-m += tccounter.o

all:


make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:


make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

====================debug方法======================echo 8 > /proc/sys/kernel/printkecho 1234234123 123 > /proc/tccount/showecho 1234234123 123 > /proc/tccount/addecho 1234234123 123 > /proc/tccount/del
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.