Linux虚拟网络基础——tap
Posted 牛顿的小脑
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux虚拟网络基础——tap相关的知识,希望对你有一定的参考价值。
原文:https://blog.csdn.net/chengqiuming/article/details/80071073
-------------------------------------------------------------------
一 介绍
Linux中谈到tap,经常会和tun并列谈论。两者都是操作系统内核中的虚拟网络设备。tap位于二层,tun位于三层。需要说明的是,这里所说的设备是Linux的概念,并不是我们平时生活中所说的设备。比如,生活中,我们常常把一台物理路由器称为一台设备。
而Linux所说的设备,其背后指的是一个类似于数据结构、内核模块或设备驱动着样的含义。像tap/tun这样的设备,它的数据结构如下:
struct tun_struct {
char name[8]; //设备名
unsigned long flags; //区分tun和tap设备
struct fasync_struct *fasync; //文件异步通知结构
wait_queue_head_t read_wait; //等待队列
struct net_device dev; //linux 抽象网络设备结构
struct sk_buff_head txq; //网络缓冲区队列
struct net_device_stats stats; //网卡状态信息结构
};
我们看到,甚至连数据结构,tap和tun的定义都是一个,两者仅仅是通过一个Flag来区分。不过从背后所承载的功能而言,两者还是有比较大的区别:tap位于网络OSI模型的二层(数据链路层),tun位于网络的三层。
本篇只介绍tap。
tap从功能上讲,位于数据链路层,数据链路层的主要协议有:
1 点对点协议(Point-to-Point Protocol)
2 以太网(Ethernet)
3 高级数据链路协议(High-Level Data Link Protocol)
4 帧中继(Frame Relay)
5 异步传输模式(Asynchronous Transfer Mode)
但是tap只是与其中一种协议以太网(Ethernet)协议对应。所以,tap有时也称为“虚拟以太设备”。
二 实战
#要想使用Linux命令行操作一个tap,首先得有tun模块(Linux使用tun模块实现了tun/tap),检查方法如下:
[root@centos ~]# modinfo tun
filename: /lib/modules/3.10.0-327.el7.x86_64/kernel/drivers/net/tun.ko
alias: devname:net/tun
alias: char-major-10-200
license: GPL
author: (C) 1999-2004 Max Krasnyansky <maxk@qualcomm.com>
description: Universal TUN/TAP device driver
rhelversion: 7.2
srcversion: B59BCB1255A36FBC7557FC3
depends:
intree: Y
vermagic: 3.10.0-327.el7.x86_64 SMP mod_unload modversions
signer: CentOS Linux kernel signing key
sig_key: 79:AD:88:6A:11:3C:A0:22:35:26:33:6C:0F:82:5B:8A:94:29:6A:B3
sig_hashalgo: sha256
#当Linux版本具有tun模块时,还得看看其已经加载,检查方法如下:
[root@centos ~]# lsmod|grep tun
tun 27141 1
#如果已经加载,则会出现上述的“tun ***”那一行。如果没有加载,则使用如下命令进行加载:
[root@centos ~]# modprobe tun
#当我们确认Linux加载了tun模块以后,我们需要确认Linux是否有操作tun/tap的命令行工具tunctl。在Linux命令行中输入以下命令
[root@centos ~]# tunctl help
#输入这个命令后,如果Linux有输入,则说明OK,否则下面网站进行安装
#https://blog.csdn.net/lopng/article/details/72821438
#具备了tun和tunctl以后,我们就可以创建一个tap设备,命令行如下:
[root@centos yum.repos.d]# tunctl -t tap_test
Set ‘tap_test‘ persistent and owned by uid 0
#我们通过如下命令查看刚刚创建的tep(tap_test)
[root@centos yum.repos.d]# ip link list
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
link/ether 08:00:27:12:f4:ac brd ff:ff:ff:ff:ff:ff
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT
link/ether 00:00:00:00:00:00 brd ff:ff:ff:ff:ff:ff
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN mode DEFAULT qlen 500
link/ether 52:54:00:1b:2a:d5 brd ff:ff:ff:ff:ff:ff
5: tap_test: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 500
link/ether 36:c3:5e:1a:6d:f7 brd ff:ff:ff:ff:ff:ff
#我们也可以通过如下命令查看
[root@centos yum.repos.d]# ifconfig -a
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.0.101 netmask 255.255.255.0 broadcast 192.168.0.255
inet6 fe80::a00:27ff:fe12:f4ac prefixlen 64 scopeid 0x20<link>
ether 08:00:27:12:f4:ac txqueuelen 1000 (Ethernet)
RX packets 6364 bytes 8271654 (7.8 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1770 bytes 172967 (168.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 0 (Local Loopback)
RX packets 8 bytes 1104 (1.0 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 8 bytes 1104 (1.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
tap_test: flags=4098<BROADCAST,MULTICAST> mtu 1500
ether 36:c3:5e:1a:6d:f7 txqueuelen 500 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 00:00:00:00:00:00 txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0-nic: flags=4098<BROADCAST,MULTICAST> mtu 1500
ether 52:54:00:1b:2a:d5 txqueuelen 500 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
#通过上面的命令行的输出,我们看到,这个tap_test还没有绑定IP地址。执行如下命令,给其绑定IP地址:
[root@centos yum.repos.d]# ip addr add local 192.168.100.1/24 dev tap_test
#使用ifconfig -a命令再查看一下
[root@centos yum.repos.d]# ifconfig -a
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.0.101 netmask 255.255.255.0 broadcast 192.168.0.255
inet6 fe80::a00:27ff:fe12:f4ac prefixlen 64 scopeid 0x20<link>
ether 08:00:27:12:f4:ac txqueuelen 1000 (Ethernet)
RX packets 6500 bytes 8282430 (7.8 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1845 bytes 183755 (179.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 0 (Local Loopback)
RX packets 8 bytes 1104 (1.0 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 8 bytes 1104 (1.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
tap_test: flags=4098<BROADCAST,MULTICAST> mtu 1500
inet 192.168.100.1 netmask 255.255.255.0 broadcast 0.0.0.0
ether 36:c3:5e:1a:6d:f7 txqueuelen 500 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 00:00:00:00:00:00 txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0-nic: flags=4098<BROADCAST,MULTICAST> mtu 1500
ether 52:54:00:1b:2a:d5 txqueuelen 500 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
#到此,一个tap设备就创建完了
————————————————
版权声明:本文为CSDN博主「cakincqm」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/chengqiuming/article/details/80071073
以上是关于Linux虚拟网络基础——tap的主要内容,如果未能解决你的问题,请参考以下文章
[Linux用户空间编程-4]:Linux虚拟网络设备TUN/TAP的工作原理与代码示例
[Linux用户空间编程-4]:Linux虚拟网络设备TUN/TAP的工作原理与代码示例