如何让tap设备使用socket API?
我正在使用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,
};
问题:
- 如何创建一个文件描述符以与此套接字连接?这使我可以在用户空间中使用
- > sendmsg()
和- > recvmsg()
。我的操作系统是Centos7.9,带有内核版本3.10.0-1160.el7.x86_64
。 - 还是我可以一次从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:
- 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 version3.10.0-1160.el7.x86_64
. - 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论