您的位置:首页 > 其它

Generic Netlink详解

2014-08-08 13:44 253 查看
http://www.linuxidc.com/Linux/2012-07/65482p2.htm

http://www.tuicool.com/articles/jE7nim

http://blog.csdn.net/u011638528/article/details/10221557

netlink socket是一种用于用户态进程和内核态进程之间的通信机制。它通过为内核模块提供一组特殊的API,并为用户程序提供了一组标准的socket接口的方式,实现了全双工的通讯连接。

/* attribute type */

enum {

DOC_EXMPL_A_UNSPEC,

DOC_EXMPL_A_MSG,

__DOC_EXMPL_A_MAX,

};

#define DOC_EXMPL_A_MAX (__DOC_EXMPL_A_MAX - 1)

/* family definition */

static struct genl_family doc_exmpl_gnl_family = {

.id = GENL_ID_GENERATE,

.hdrsize = 0,

.name = "DOC_EXMPL",

.version = 1,

.maxattr = DOC_EXMPL_A_MAX,

};

Generic Netlink服务端(内核)初始化

初始化Generic Netlink的过程分为以下四步:定义family,定义operation,注册family,注册operation。下面通过一个简单例子来说明如何完成Generic Netlink的初始化。我们首先创建一个genl_family结构体的实例。我们在这里定义一个名为"DOC_EXMPL"的family

genl_ops 结构体

struct genl_ops
{
u8                      cmd;
unsigned int            flags;
struct nla_policy       *policy;
int                     (*doit)(struct sk_buff *skb,
struct genl_info *info);
int                     (*dumpit)(struct sk_buff *skb,
struct netlink_callback *cb);
struct list_head        ops_list;
};


struct nla_policy { u16 type; u16 len; };

http://yhpan0613.blog.163.com/blog/static/17828305020116523824742/

The first step is to define the family
itself, which we do by creating an

instance of the genl_family structure. In our simple example we are going to

create a new Generic Netlink family named "DOC_EXMPL".

/* attributes */

enum {

DOC_EXMPL_A_UNSPEC,

DOC_EXMPL_A_MSG,

__DOC_EXMPL_A_MAX,

};

#define DOC_EXMPL_A_MAX (__DOC_EXMPL_A_MAX - 1)

/* attribute policy */

static struct nla_policy doc_exmpl_genl_policy[DOC_EXMPL_A_MAX + 1] = {

[DOC_EXMPL_A_MSG] = { .type = NLA_NUL_STRING },

};

/* family definition */

static struct genl_family doc_exmpl_gnl_family = {

.id = GENL_ID_GENERATE,

.hdrsize = 0,

.name = "DOC_EXMPL",

.version = 1,

.maxattr = DOC_EXMPL_A_MAX,

};


这里,我们把attribute policy设为NLA_NUL_STRING,表示attr中数据的属性为无NULL结尾的字符串。控制器在收到数据时会自动完成这一类型检查。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: