Docker0

Posted by qijun on January 5, 2022

Linux tun:tap

tun/tap 虽然工作原理一致,但是工作的层次不一样。

tun是三层网络设备,收发的是IP层数据包,无法处理以太网数据帧,例如OpenVPN的路由模式就是使用了tun网络设备,OpenVPN Server重新规划了一个网段,所有的客户端都会获取到该网段下的一个IP,并且会添加对应的路由规则,而客户端与目标机器产生的数据报文都要经过OpenVPN网关才能转发。

tap是二层网络设备,收发以太网数据帧,拥有MAC层的功能,可以和物理网卡通过网桥相连,组成一个二层网络。例如OpenVPN的桥接模式可以从外部打一条隧道到本地网络。进来的机器就像本地的机器一样参与通讯,丝毫看不出这些机器是在远程。如果你有使用过虚拟机的经验,桥接模式也是一种十分常见的网络方案,虚拟机会分配到和宿主机器同网段的IP,其他同网段的机器也可以通过网络访问到这台虚拟机。

# 创建 tap 
ip tuntap add dev tap0 mode tap 
# 创建 tun
ip tuntap add dev tun0 mode tun 

# 删除 tap
ip tuntap del dev tap0 mode tap
# 删除 tun
ip tuntap del dev tun0 mode tun 

tun/tap 设备创建成功后可以当作普通的网卡一样使用,因此我们也可以通过ip link命令来操作它。

# 例如使用ip link命令也可以删除tun/tap设备
ip link del tap0
ip link del tun0

Linux netns

Network Namespace (以下简称netns)是Linux内核提供的一项实现网络隔离的功能,它能隔离多个不同的网络空间,并且各自拥有独立的网络协议栈,这其中便包括了网络接口(网卡),路由表,iptables规则等。

Usage: ip netns list
       ip netns add NAME
       ip netns set NAME NETNSID
       ip [-all] netns delete [NAME]
       ip netns identify [PID]
       ip netns pids NAME
       ip [-all] netns exec [NAME] cmd ...
       ip netns monitor
       ip netns list-id

Linux veth pair

veth pair是成对出现的一种虚拟网络设备接口,一端连着网络协议栈,一端彼此相连。

ip link add <veth name> type veth peer name <peer name>

Linux Bridge

Linux Bridge(网桥)是用纯软件实现的虚拟交换机,有着和物理交换机相同的功能,例如二层交换,MAC地址学习等。因此我们可以把tun/tap,veth pair等设备绑定到网桥上,就像是把设备连接到物理交换机上一样。此外它和veth pair、tun/tap一样,也是一种虚拟网络设备,具有虚拟设备的所有特性,例如配置IP,MAC地址等。

安装

# centos
yum install -y bridge-utils
# ubuntu
apt-get install -y bridge-utils
never heard of command [help]
Usage: brctl [commands]
commands:
 addbr      <bridge>  add bridge
 delbr      <bridge>  delete bridge
 addif      <bridge> <device> add interface to bridge
 delif      <bridge> <device> delete interface from bridge
 hairpin    <bridge> <port> {on|off} turn hairpin on/off
 setageing  <bridge> <time>  set ageing time
 setbridgeprio <bridge> <prio>  set bridge priority
 setfd      <bridge> <time>  set bridge forward delay
 sethello   <bridge> <time>  set hello time
 setmaxage  <bridge> <time>  set max message age
 setpathcost <bridge> <port> <cost> set path cost
 setportprio <bridge> <port> <prio> set port priority
 show       [ <bridge> ]  show a list of bridges
 showmacs   <bridge>  show a list of mac addrs
 showstp    <bridge>  show bridge stp info
 stp        <bridge> {on|off} turn stp on/off

实验

# 添加网桥
brctl addbr br0
# 启动网桥
ip link set br0 up

# 新增三个netns
ip netns add ns0
ip netns add ns1
ip netns add ns2

# 新增两对veth
ip link add veth0-ns type veth peer name veth0-br
ip link add veth1-ns type veth peer name veth1-br
ip link add veth2-ns type veth peer name veth2-br

# 将veth的一端移动到netns中
ip link set veth0-ns netns ns0
ip link set veth1-ns netns ns1
ip link set veth2-ns netns ns2

# 将netns中的本地环回和veth启动并配置IP
ip netns exec ns0 ip link set lo up
ip netns exec ns0 ip link set veth0-ns up
ip netns exec ns0 ip addr add 10.0.0.1/24 dev veth0-ns

ip netns exec ns1 ip link set lo up
ip netns exec ns1 ip link set veth1-ns up
ip netns exec ns1 ip addr add 10.0.0.2/24 dev veth1-ns

ip netns exec ns2 ip link set lo up
ip netns exec ns2 ip link set veth2-ns up
ip netns exec ns2 ip addr add 10.0.0.3/24 dev veth2-ns

# 将veth的另一端启动并挂载到网桥上
ip link set veth0-br up
ip link set veth1-br up
ip link set veth2-br up
brctl addif br0 veth0-br
brctl addif br0 veth1-br
brctl addif br0 veth2-br

使用ip netns exec ns0 ping 10.0.0.2在命名空间ns0中测试与ns1的10.0.0.2的网络连通性

参考