内核空间 用户空间与 netlink 的通信

发布于 2024-10-07 21:49:02 字数 363 浏览 2 评论 0原文

我的目标是始终在内核内存中拥有一个数组或列表,以便始终可以从内核空间访问它。为此,我使用此处推荐的 netlink 套接字。我按照这个示例,显示如何发送字符串。我不确定如何在套接字中发送结构数组或结构列表。

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

是否可以使用 netlink 发送列表或数组?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挖个坑埋了你 2024-10-14 21:49:02

NetLink 本身并不关心数据是字符串、整数、结构体等。它只需要一个 void* 和一个长度,然后复制数据而不查看它。您可以将结构转换为 void* 并使用 sizeof 来确定长度并通过 netlink 发送该数据。

另一方面,您只需要获取 void* 和长度,验证长度是否正确,然后将 void* 转换回指向结构的指针。需要验证的两个重要事项是:

  1. 用户空间和内核空间代码都同意该结构的内存布局。这意味着针对相同的 .h 进行编译,并确保编译选项使内存布局和对齐相同。

  2. 结构将仅作为原始内存副本进行传输,没有修复指针的智能,因此您的结构不能包含任何指针等。

另一种选择是不发送原始数据,而是通过将数据从已知格式的字符串转换为自己“序列化”数据。这将允许您处理更复杂的数据结构,但代价是额外的 CPU 和内存开销

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

  2. 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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文