使用netlink之前,先参考一下资料:http://www.ibm.com/developerworks/cn/linux/l-connector/

netlink通信机制介绍:资料来源 linux-4.8.13/Documentation/connector/connector.txt 2016-12-10 21:31:04

/*****************************************/
Kernel Connector.
/*****************************************/

Kernel connector - new netlink based userspace <-> kernel space easy
to use communication module.

The Connector driver makes it easy to connect various agents using a
netlink based network.  One must register a callback and an identifier.
When the driver receives a special netlink message with the appropriate
identifier, the appropriate callback will be called.

From the userspace point of view it's quite straightforward:

    socket();
    bind();
    send();
    recv();

用户空间使用netlink相对简单一些。

But if kernelspace wants to use the full power of such connections, the
driver writer must create special sockets, must know about struct sk_buff
handling, etc...  The Connector driver allows any kernelspace agents to use
netlink based networking for inter-process communication in a significantly
easier way:

int cn_add_callback(struct cb_id *id, char *name, void (*callback) (struct cn_msg *, struct netlink_skb_parms *));
void cn_netlink_send_multi(struct cn_msg *msg, u16 len, u32 portid, u32 __group, int gfp_mask);
void cn_netlink_send(struct cn_msg *msg, u32 portid, u32 __group, int gfp_mask);

struct cb_id
{
    __u32            idx;
    __u32            val;
};

idx and val are unique identifiers which must be registered in the
connector.h header for in-kernel usage.  void (*callback) (void *) is a
callback function which will be called when a message with above idx.val
is received by the connector core.  The argument for that function must
be dereferenced to struct cn_msg *.

struct cn_msg
{
    struct cb_id        id;

    __u32            seq;
    __u32            ack;

    __u32            len;        /* Length of the following data */
    __u8            data[0];
};

/*****************************************/
Connector interfaces.
/*****************************************/
注:

cn应该是connector的缩写

cb:call back

int cn_add_callback(struct cb_id *id, char *name, void (*callback) (struct cn_msg *, struct netlink_skb_parms *));

 Registers new callback with connector core.

 struct cb_id *id        - unique connector's user identifier.
                  It must be registered in connector.h for legal in-kernel users.
 char *name            - connector's callback symbolic name.
 void (*callback) (struct cn..)    - connector's callback.
                  cn_msg and the sender's credentials

void cn_del_callback(struct cb_id *id);

 Unregisters new callback with connector core.

 struct cb_id *id        - unique connector's user identifier.

int cn_netlink_send_multi(struct cn_msg *msg, u16 len, u32 portid, u32 __groups, int gfp_mask);
int cn_netlink_send(struct cn_msg *msg, u32 portid, u32 __groups, int gfp_mask);

 Sends message to the specified groups.  It can be safely called from
 softirq context, but may silently fail under strong memory pressure.
 If there are no listeners for given group -ESRCH can be returned.

 struct cn_msg *        - message header(with attached data).
 u16 len            - for *_multi multiple cn_msg messages can be sent
 u32 port            - destination port.
                   If non-zero the message will be sent to the
                  given port, which should be set to the
                  original sender.
 u32 __group            - destination group.
                  If port and __group is zero, then appropriate group will
                  be searched through all registered connector users,
                  and message will be delivered to the group which was
                  created for user with the same ID as in msg.
                  If __group is not zero, then message will be delivered
                  to the specified group.
 int gfp_mask            - GFP mask.

 Note: When registering new callback user, connector core assigns
 netlink group to the user which is equal to its id.idx.

/*****************************************/
Protocol description.
/*****************************************/

The current framework offers a transport layer with fixed headers.  The
recommended protocol which uses such a header is as following:

msg->seq and msg->ack are used to determine message genealogy.  When
someone sends a message, they use a locally unique sequence and random
acknowledge number.  The sequence number may be copied into
nlmsghdr->nlmsg_seq too.

The sequence number is incremented with each message sent.

If you expect a reply to the message, then the sequence number in the
received message MUST be the same as in the original message, and the
acknowledge number MUST be the same + 1.

If we receive a message and its sequence number is not equal to one we
are expecting, then it is a new message.  If we receive a message and
its sequence number is the same as one we are expecting, but its
acknowledge is not equal to the sequence number in the original
message + 1, then it is a new message.

Obviously, the protocol header contains the above id.

The connector allows event notification in the following form: kernel
driver or userspace process can ask connector to notify it when
selected ids will be turned on or off (registered or unregistered its
callback).  It is done by sending a special command to the connector
driver (it also registers itself with id={-1, -1}).

内核自身提供了示例程序cn_test.c
As example of this usage can be found in the cn_test.c module which
uses the connector to request notification and to send messages.

/*****************************************/
Reliability.
/*****************************************/

Netlink itself is not a reliable protocol.  That means that messages can
be lost due to memory pressure or process' receiving queue overflowed,
so caller is warned that it must be prepared.  That is why the struct
cn_msg [main connector's message header] contains u32 seq and u32 ack
fields.

/*****************************************/
Userspace usage.
/*****************************************/

2.6.14 has a new netlink socket implementation, which by default does not
allow people to send data to netlink groups other than 1.
So, if you wish to use a netlink socket (for example using connector)
with a different group number, the userspace application must subscribe to
that group first.  It can be achieved by the following pseudocode:

s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);

l_local.nl_family = AF_NETLINK;
l_local.nl_groups = 12345;
l_local.nl_pid = 0;

if (bind(s, (struct sockaddr *)&l_local, sizeof(struct sockaddr_nl)) == -1) {
    perror("bind");
    close(s);
    return -1;
}

{
    int on = l_local.nl_groups;
    setsockopt(s, 270, 1, &on, sizeof(on));
}

Where 270 above is SOL_NETLINK, and 1 is a NETLINK_ADD_MEMBERSHIP socket
option.  To drop a multicast subscription, one should call the above socket
option with the NETLINK_DROP_MEMBERSHIP parameter which is defined as 0.

2.6.14 netlink code only allows to select a group which is less or equal to
the maximum group number, which is used at netlink_kernel_create() time.
In case of connector it is CN_NETLINK_USERS + 0xf, so if you want to use
group number 12345, you must increment CN_NETLINK_USERS to that number.
Additional 0xf numbers are allocated to be used by non-in-kernel users.

Due to this limitation, group 0xffffffff does not work now, so one can
not use add/remove connector's group notifications, but as far as I know,
only cn_test.c test module used it.

Some work in netlink area is still being done, so things can be changed in
2.6.15 timeframe, if it will happen, documentation will be updated for that
kernel.

/*****************************************/
Code samples
/*****************************************/

Sample code for a connector test module and user space can be found
in samples/connector/. To build this code, enable CONFIG_CONNECTOR
and CONFIG_SAMPLES.

1、netlink 连接器 通信机制的更多相关文章

  1. linux netlink通信机制

    一.什么是Netlink通信机制  Netlink套接字是用以实现用户进程与内核进程通信的一种特殊的进程间通信(IPC) ,也是网络应用程序与内核通信的最常用的接口. Netlink 是一种特殊的 s ...

  2. Netlink通信机制【转】

    本文转载自:http://www.cnblogs.com/wenqiang/p/6306727.html 一.什么是Netlink通信机制  Netlink套接字是用以实现用户进程与内核进程通信的一种 ...

  3. linux netlink通信机制简介

    一.什么是Netlink通信机制  Netlink套接字是用以实现用户进程与内核进程通信的一种特殊的进程间通信(IPC) ,也是网络应用程序与内核通信的最常用的接口. Netlink 是一种特殊的 s ...

  4. netlink---Linux下基于socket的内核和上层通信机制 (转)

    需要在linux网卡 驱动中加入一个自己的驱动,实现在内核态完成一些报文处理(这个过程可以实现一种零COPY的网络报文截获),对于复杂报文COPY下必要的数据交给用户 态来完成(因为过于复杂的报文消耗 ...

  5. .Net中Remoting通信机制简单实例

    .Net中Remoting通信机制 前言: 本程序例子实现一个简单的Remoting通信案例 本程序采用语言:c# 编译工具:vs2013工程文件 编译环境:.net 4.0 程序模块: Test测试 ...

  6. .Net中Remoting通信机制

    Remoting通信机制 Remoting介绍 主要元素 通道类型 激活方式 对象定义 Remoting介绍 什么是Remoting,简而言之,我们可以将其看作是一种分布式处理方式. 从微软的产品角度 ...

  7. 【单页应用之通信机制】view之间应该如何通信

    前言 在单页应用中,view与view之间的通信机制一直是一个重点,因为单页应用的所有操作以及状态管理全部发生在一个页面上 没有很好的组织的话很容易就乱了,就算表面上看起来没有问题,事实上会有各种隐忧 ...

  8. Android多线程通信机制

    掌握Android的多线程通信机制,我们首先应该掌握Android中进程与线程是什么. 1. 进程 在Android中,一个应用程序就是一个独立的进程(应用运行在一个独立的环境中,可以避免其他应用程序 ...

  9. Storm进程通信机制

    storm的worker进程之间消息传递机制图: 每个worker都有一个独立的监听进程,监听配置文件中配置过的端口列表supervisor.slots.ports,topology.receiver ...

随机推荐

  1. openfl关于windows平台编译报错解决办法

    报错信息:  无法打开程序数据库“e:\newproj\mainclient\bin\windows\cpp\obj\obj\msvc-debug-ncxp\vc.pdb”:如果要将多个 CL.EXE ...

  2. 多校5 1001 HDU5781 ATM Mechine 记忆化搜索+概率

    // 多校5 1001 HDU5781 ATM Mechine // http://acm.hdu.edu.cn/search.php?field=problem&key=2016+Multi ...

  3. MLE MAP EM

    1.最大似然估计 (MLE):  什么是最大似然估计?     问题:给定一组观察数据还有一个参数待定的模型,如何来估计这个未知参数呢? 观察数据(x1,y1)......(xn,yn)   待定模型 ...

  4. [AngularJS学习笔记] 基础学习01

    2016-06-06开始学习AngularJS AngularJS是会extend HTML的 ng-directives 先学习了四个 ng-app:定义AngularJS Application的 ...

  5. RabbitMQ (四) 路由选择 (Routing) -摘自网络

    本篇博客我们准备给日志系统添加新的特性,让日志接收者能够订阅部分消息.例如,我们可以仅仅将致命的错误写入日志文件,然而仍然在控制面板上打印出所有的其他类型的日志消息. 1.绑定(Bindings) 在 ...

  6. Linux 命令之last命令详解

    last:命令解释show listing of last logged in users 指令所在路径:/usr/bin/last 命令输出字段介绍: 第一列:用户名 第二列:终端位置.pts/0 ...

  7. [iOS基础控件 - 6.9.1] 聊天界面Demo 代码

    框架:   所有代码文件:   Model: // // Message.h // QQChatDemo // // Created by hellovoidworld on 14/12/8. // ...

  8. VBA在Excel中的应用(一):改变符合条件单元格的背景颜色

    在使用excel处理数据的时候,为了能更清晰的标示出满足特定条件的单元格,对单元格添加背景色是不错的选择.手工处理的方式简单快捷,但是当遇到大批量数据,就会特别的费时费力,而且不讨好(容易出错).通过 ...

  9. ubuntu为IDE(Eclipse WebStorm)添加桌面快捷方式

    在ubuntu15.10环境配置webstorm和eclipse的时候会下载官网上编译好的包, bin目录下面会有一个.sh文件(linux版本), 那么一般情况下,执行 ./sh就会启动IDE, 但 ...

  10. BaiduMap开发,获取公交站点信息。

    可能有些人会出现无法导入overlayutil的错误,这是因为BaiduMap里面的包把这部分删除掉了,并且官方没有给出说明,这个地方以前也是让我折腾了很久. 不知道现在有没有说明这个问题,如果需要这 ...