网络链路内核空间用户空间通信 [英] kernel-space user-space communication with netlink

查看:192
本文介绍了网络链路内核空间用户空间通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是在任何时候都在内核内存数组或列表,因此它总是从内核空间访问。要,我使用netlink的插座为推荐这里做到这一点。我按照<一href=\"http://stackoverflow.com/questions/3299386/how-to-use-netlink-socket-to-communicate-with-a-kernel-module\">this例如的,它展示了如何发送一个字符串。我不知道是如何在套接字送结构数组或列表结构的。

My objective is to have an array or list in kernel memory at all times, so that it is always accessible from kernel-space. To do this, I am using netlink sockets as recommended here. I follow this example, which shows how to send a string. I am not sure as to how to send an array of struct or list of structs in sockets.

typedef struct {
    int fiveDollarBills;
    int denDollarBills;
} Bills;

是否可以使用网络链路发送列表或数组?

Is it possible to send a list or array using netlink?

推荐答案

NETLINK本身并不关心数据是否是一个字符串,一个整数,一个结构,等它只是需要一个void *,长度和副本的的数据不看它。你可以施展你的结构为void *,并使用的sizeof来确定长度和发送数据通过netlink。

NetLink itself doesn't care whether the data is a string, an integer, a struct, etc. It just takes a void* and a length and copy's the data without looking at it. You can cast your struct to a void* and use sizeof to determine the length and send that data over netlink.

在另一端,你只需要获得无效*和长度,验证长度是它应该是,投虚空*回指针,你的结构。验证这两个重要的事情是:

On the other end, you just need to get the void* and length, verify the length is what it is supposed to be, and cast the void* back to a pointer to your struct. The two important things to verify are:


  1. 无论是用户空间和内核code对结构的内存布局一致。这意味着双方对同一.H编制,并确保编译选项是这样的内存布局和走线都是一样的。

  1. Both the UserSpace and KernelSpace code agree on the memory layout of the structure. This means compiling both against the same .h and making sure that the compile options are such that memory layout and alignment are the same.

该结构将被转移只是一个原始内存拷贝,世界上没有情报固定起来指针,所以你的结构不能包含任何指针等。

The Struct will be transferred as just a raw memory copy, theres no intelligence for fixing up pointers, so your struct can not contain any pointers, etc.

另一种选择是,而不是发送的原始数据翻过,通过在一公知的格式将其转换为,从字符串自己序列的数据。这将允许你在额外的CPU和内存开销

The other option is rather than sending the raw data accross, to "serialize" the data yourself by converting it to, from a string in a known format. This would allow you to handle more complex data structures at the expense of extra CPU and memory overhead

这篇关于网络链路内核空间用户空间通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆