如何让tap设备使用socket API?

发布于 2025-01-18 00:19:41 字数 1163 浏览 0 评论 0原文

我正在使用TAP设备。

问题是您只能- > read()- > write()每个系统调用一个数据包。

阅读源文件 tun.c 之后,我发现tun设备和函数tun_get_socket()中有struct socket可以返回。

struct socket *tun_get_socket(struct file *file)
{
    struct tun_file *tfile;
    if (file->f_op != &tun_fops)
        return ERR_PTR(-EINVAL);
    tfile = file->private_data;
    if (!tfile)
        return ERR_PTR(-EBADFD);
    return &tfile->socket;
}
EXPORT_SYMBOL_GPL(tun_get_socket);

socket.ops使用tun_socket_ops 具有- > sendmsg()- > recvmsg() >。

static const struct proto_ops tun_socket_ops = {
    .sendmsg = tun_sendmsg,
    .recvmsg = tun_recvmsg,
    .release = tun_release,
}; 

问题:

  1. 如何创建一个文件描述符以与此套接字连接?这使我可以在用户空间中使用- > sendmsg()- > recvmsg()。我的操作系统是Centos7.9,带有内核版本3.10.0-1160.el7.x86_64
  2. 还是我可以一次从TAP设备读取或编写多个数据包的其他方法?

编写新内核模块是可以接受的。

I'm using tap device.

The problem is that you just can ->read() or ->write() one packet every system call.

After read the source file tun.c, I found there is struct socket in tun device and function tun_get_socket() can return it.

struct socket *tun_get_socket(struct file *file)
{
    struct tun_file *tfile;
    if (file->f_op != &tun_fops)
        return ERR_PTR(-EINVAL);
    tfile = file->private_data;
    if (!tfile)
        return ERR_PTR(-EBADFD);
    return &tfile->socket;
}
EXPORT_SYMBOL_GPL(tun_get_socket);

The socket.ops is set with tun_socket_ops which have ->sendmsg() and ->recvmsg().

static const struct proto_ops tun_socket_ops = {
    .sendmsg = tun_sendmsg,
    .recvmsg = tun_recvmsg,
    .release = tun_release,
}; 

The questions:

  1. How to create one file descriptor to connect with this socket? That makes me to use ->sendmsg() and ->recvmsg() in userspace. My OS is CentOS7.9 with kernel version 3.10.0-1160.el7.x86_64.
  2. Or is there other ways I can read or write multiple packets at one time from tap device?

Writing new kernel module is acceptable.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文