gpt4 book ai didi

c - 如何填写用于 msghdr 的 iov_iter 结构?

转载 作者:太空宇宙 更新时间:2023-11-04 03:20:00 34 4
gpt4 key购买 nike

我正在尝试使用 sock_sendmsg() 从驱动程序发送 UDP 数据包。

关于如何使用 sock_sendmsg() 和 msghdr 填充 UDP 数据包并传输它有几个指南,但它们都是针对旧内核版本的。在 3.13 和 4.8 之间的某个时间,struct msghdr 更改为:

struct msghdr {
void * msg_name; /* Socket name */
int msg_namelen; /* Length of name */
struct iovec * msg_iov; /* Data blocks */
__kernel_size_t msg_iovlen; /* Number of blocks */
void * msg_control; /* Per protocol magic (eg BSD file descriptor passing) */
__kernel_size_t msg_controllen; /* Length of cmsg list */
unsigned int msg_flags;
};

收件人:

struct msghdr {
void *msg_name; /* ptr to socket address structure */
int msg_namelen; /* size of socket address structure */
struct iov_iter msg_iter; /* data */
void *msg_control; /* ancillary data */
__kernel_size_t msg_controllen; /* ancillary data buffer length */
unsigned int msg_flags; /* flags on received message */
struct kiocb *msg_iocb; /* ptr to iocb for async requests */
};

不是将 msg_iov 设置为指向带有数据的 iovec:

struct msghdr msg = {};
struct iovec iov = {};

iov.iov_base = data;
iov.iov_len = dataLen;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;

现在有一个 iov_iter 结构:

struct iov_iter {
int type;
size_t iov_offset;
size_t count;
union {
const struct iovec *iov;
const struct kvec *kvec;
const struct bio_vec *bvec;
struct pipe_inode_info *pipe;
};
union {
unsigned long nr_segs;
struct {
int idx;
int start_idx;
};
};
};

如何用数据填充 msghdr 中的 iov_iter 结构,以便将其传递给 sock_sendmsg?

最佳答案

在尝试并阅读 Ctx ( lwn.net/Articles/625077 ) 发布的指南后,我明白了。

unsigned long nr_segments = 1;
size_t count = 1;
struct msghdr msg = {};
struct iovec iov = {};

iov.iov_base = data;
iov.iov_len = dataLen;

iov_iter_init(&msg.msg_iter, READ, &iov, nr_segments, count);

关于c - 如何填写用于 msghdr 的 iov_iter 结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47315064/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com