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

关于如何使用sock_sendmsg()和msghdr填充UDP数据包并进行传输的方法,有几本指南,但是它们都适用于较早的内核版本。在3.13和4.8之间的某个时间,结构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/

10-13 07:10