1、netlink 连接器 通信机制

使用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.

时间: 2024-10-09 22:23:24

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

linux netlink通信机制

一.什么是Netlink通信机制  Netlink套接字是用以实现用户进程与内核进程通信的一种特殊的进程间通信(IPC) ,也是网络应用程序与内核通信的最常用的接口. Netlink 是一种特殊的 socket,它是 Linux 所特有的,类似于 BSD 中的AF_ROUTE 但又远比它的功能强大,目前在Linux 内核中    使用netlink 进行应用与内核通信的应用很多; 包括:路由 daemon(NETLINK_ROUTE),用户态 socket 协议(NETLINK_USERSOCK

NSNotificationCenter消息通信机制介绍(KVO)

NSNotificationCenter消息通信机制介绍(KVO) 作用:NSNotificationCenter是专门供程序中不同类间的消息通信而设置的. 注册通知:即要在什么地方接受消息                [[NSNotificationCenter defaultCenter]  addObserver:self selector:@selector(mytest:) name:@" mytest" object:nil];        参数介绍:         

React Native通信机制详解

本文转载至 http://blog.cnbang.net/tech/2698/?from=groupmessage&isappinstalled=1 React Native是facebook刚开源的框架,可以用javascript直接开发原生APP,先不说这个框架后续是否能得到大众认可,单从源码来说,这个框架源码里有非常多的设计思想和实现方式值得学习,本篇先来看看它最基础的JavaScript-ObjectC通信机制(以下简称JS/OC). 概览 React Native用iOS自带的Java

php socket通信机制实例说明与代码

php socket通信机制实例说明与代码----什么是socket 所谓socket一般也称作"套接字",用于描述ip地址和端口,是一个通讯链的句柄.使用程序一般经过"套接字"向network发出请求也许应对network请求.说白了就是一种通讯机制.它类似于银行,电信啊这一些部分的电话客服业务单元.您打电话的时候,那边会调设置一个人回答您的问题,客服业务单元就相当于socket的服务器端了,您这边呢就相当于用户端了,在和您通话完结前,假设有人在想找和您通话的那个

线程同步机制之互斥锁通信机制

#include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <string.h> void *thread_function(void *arg); pthread_mutex_t work_mutex; #define WORK_SIZE 1024 char work_area[WORK_SIZE]; int time_to_exit=0; int main(int argc,

Qt的内部进程通信机制 [转]

Qt 作为一种跨平台的基于 C++ 的 GUI系统,能够提供给用户构造图形用户界面的强大功能.自从 1996 年 Qt 被 Trolltech公司发布以来,该系统成为世界上很多成功的图形用户应用所使用的主要系统.更为重要的是,Linux 操作系统的桌面环境系统 KDE 也是基于 Qt构造的.目前,Qt 已经提供了对包括 MS/Windows.Unix/X11 和嵌入式平台的支持,得到了越来越广泛的应用. 在 Qt 系统中,不仅有着构造完善的系统结构,而且为了满足用户对编写图形用户界面应用的种种需

Android 进程通信机制之 AIDL

什么是 AIDL AIDL 全称 Android Interface Definition Language,即 安卓接口描述语言.听起来很深奥,其实它的本质就是生成进程间通信接口的辅助工具.它的存在形式是一种 .aidl 文件,开发者需要做的就是在该文件中定义进程间通信的接口,编译的时候 IDE 就会根据我们的 .aidl 接口文件生成可供项目使用的 .java 文件,这和我们说的"语法糖"有些类似. AIDL 的语法就是 java 的语法,就是导包上有点细微差别.java 中如果两

消息通信机制NSNotificationCenter -备

消息通信机制NSNotificationCenter的学习.最近写程序需要用到这类,研究了下,现把成果和 NSNotificationCenter是专门供程序中不同类间的消息通信而设置的,使用起来极为方便, 长话短说. 设置通知,就是说要在什么地方(哪个类)接受通知,一般在初始化中做. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(test:) name:@"test" object:

线程间通信机制posix匿名信号量

信号量分为两种 一种是简单的信号量,另一种是用于进程间通信的信号量集. 简单的信号量 属于POSIX标准的信号量; 从信号量的命名来看,信号量又可分为命名信号量和匿名(未命名)信号量; 从信号量的值来看,信号量可分为二进制信号量和计数信号量; 1.匿名信号量和命名信号量: 匿名信号量是在内存中分配内存.进行初始化并由系统API进行管理的,它可以在多个线程之间进行资源同步,也可以在多个进程之间进行资源同步,这主要是看在初始化的时候给pshared传递的参数值,为0,则在线程之间同步,非0,则在进程