-
Notifications
You must be signed in to change notification settings - Fork 535
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
26 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,29 @@ | ||
# 3.5.2 虚拟网络设备 TUN 和 TAP | ||
|
||
TUN 和 TAP 是 Linux 内核 2.4.x 版本之后引入的虚拟网卡设备,主要用于用户空间(user space)和内核空间(kernel space)双向传输数据。这两种设备的区别与含义为: | ||
- TUN 设备是虚拟网络接口设备,工作在网络层(Layer 3),主要处理 IP 数据包。它模拟了一个网络层接口,可以让用户空间程序处理 IP 数据包; | ||
- TAP 设备也是虚拟网络接口,但它工作在数据链路层(Layer 2),主要处理以太网帧。与 TUN 设备不同,TAP 设备传递的是完整的以太网帧,包括数据链路层头部,可以让用户空间程序处理以太网帧。 | ||
TUN 和 TAP 是 Linux 内核自 2.4.x 版本引入的虚拟网卡设备,专为用户空间(user space)与内核空间(kernel space)之间的数据传输而设计。两者的主要区别如下: | ||
- **TUN 设备**:工作在网络层(Layer 3),用于处理 IP 数据包。它模拟一个网络层接口,使用户空间程序能够直接收发 IP 数据包; | ||
- **TAP 设备**:工作在数据链路层(Layer 2),用于处理以太网帧。与 TUN 设备不同,TAP 设备传输完整的以太网帧(包括数据链路层头部),使用户空间程序可以处理原始以太网帧。 | ||
|
||
Linux 系统中,内核空间和用户空间之间数据传输有多种方式,字符设备文件是其中一种。 | ||
|
||
Linux 系统中,内核空间和用户空间之间数据传输有多种方式,字符设备文件是其中一种。TUN/TAP 对应的字符设备文件为 /dev/net/tun。当用户空间的程序 open() 字符设备文件时,返回一个 fd 句柄,同时字符设备驱动创建并注册相应的虚拟网卡网络接口,并以 tunX 或 tapX 命名。当用户空间的程序向 fd 执行 read()/write() 时,就可以和内核网络互通了。 | ||
TUN/TAP 设备对应的字符设备文件为 /dev/net/tun。当用户空间的程序打开(open)字符设备文件时,同时,TUN/TAP 的字符设备驱动会创建并注册相应的虚拟网卡,默认命名为 tunX 或 tapX。随后,用户空间程序读写(read/write)该文件描述符,就可以和内核网络栈进行数据交互了。 | ||
|
||
如图 3-13 所示,下面以 TUN 设备构建 VPN 隧道为例,说明其工作原理。 | ||
|
||
TUN 和 TAP 两者工作原理基本相同,只是工作的网络层面不一样。如图 3-13 所示,笔者以 TUN 设备构建 VPN 隧道为例,说明其工作原理: | ||
1. 首先,一个普通的用户程序发起一个网络请求; | ||
2. 接着,数据包进入内核协议栈被路由至 tun0 设备。路由规则如下: | ||
2. 接着,数据包进入内核协议栈,并路由至 tun0 设备。路由规则如下: | ||
```bash | ||
$ ip route show | ||
default via 172.12.0.1 dev tun0 // 默认流量经过 tun0 设备 | ||
192.168.0.0/24 dev eth0 proto kernel scope link src 192.168.0.3 | ||
``` | ||
3. tun0 设备字符文件 /dev/net/tun 由 VPN 程序打开。所以,第一步用户程序发送的数据包被 VPN 程序接管。 | ||
4. VPN 程序对数据包进行封装操作,“封装”是指将一个数据包包装在另一个数据包中,就像将一个盒子放在另一个盒子中一样。封装后的数据再次被发送到内核,最后通过 eth0 接口(也就是图中的物理网卡)发出。 | ||
3. tun0 设备的字符设备文件 /dev/net/tun 由 VPN 程序打开。所以,用户程序发送的数据包不会直接进入网络,而是被 VPN 程序读取并处理。 | ||
4. VPN 程序对数据包进行封装操作,封装(Encapsulation)是指在原始数据包外部包裹新的数据头部,就像将一个盒子放在另一个盒子中一样。 | ||
5. 最后,处理后的数据包再次写入内核网络栈,并通过 eth0(即物理网卡)发送到目标网络。 | ||
|
||
:::center | ||
<br/> | ||
图 3-13 VPN 中数据流动示意图 | ||
::: | ||
|
||
将一个数据包封装到另一个数据包是构建网络隧道,实现虚拟网络的典型方式。本书第七章介绍的容器网络插件 Flannel,早期曾使用 TUN 设备实现了容器间虚拟网络通信,但使用 TUN 设备传输数据需要经过两次协议栈,且有多次的封包/解包操作,导致产生了很大的性能损耗。这是后来 Flannel 弃用 TUN 设备的主要原因。 | ||
封装数据包以构建网络隧道,是实现虚拟网络的常见方式。例如,在本书第七章介绍的容器网络插件 Flannel 早期版本中,曾使用 TUN 设备来实现容器间的虚拟网络通信。但是,TUN 设备的数据传输需经过两次协议栈,并涉及多次封包与解包操作,导致了很大的性能损耗。这也是 Flannel 后来弃用 TUN 设备的主要原因。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters