简体   繁体   中英

kernel-space user-space communication with 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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM