Linux网络虚拟化 Bridge

本文假设已经具备基本的路由交换基础。个人学习记录,欢迎指正。

bridge是linux实现的虚拟化的交换机设备,其工作原理同现实交换机基本一致。

基本操作

# 安装配置工具
yum -y install bridge-utils

# 添加交换机
brctl addbr br1

# 查看交换机
brctl show

# 将veth插入交换机
brctl addif br1 veth1

# 移除交换机上的veth端口
brctl delif br1 veth1

# 删除交换机
brctl delbr br1

利用bridge使不同网络名称空间互联互通(同网段)

namespace 和 veth 相关请阅读另一篇文章。

拓扑如下,三台终端连接至Bridge,实现互联互通。
Linux网络虚拟化 Bridge_第1张图片

# 使用 brctl 命令进行配置
# 创建 bridge 并启用
brctl addbr BR1
ip link set dev BR1 up

# 创建网络名称空间
ip netns add ns1
ip netns add ns2
ip netns add ns3

# 创建veth
ip link add veth1 type veth peer name veth11
ip link add veth2 type veth peer name veth22
ip link add veth3 type veth peer name veth33

# 将veth pair的一端插入ns,启用并配置ip
ip link set dev veth11 netns ns1
ip netns exec ns1 ip link set dev veth11 up
ip netns exec ns1 ip addr add 10.0.1.1/24 dev veth11
ip link set dev veth22 netns ns2
ip netns exec ns2 ip link set dev veth22 up
ip netns exec ns2 ip addr add 10.0.1.2/24 dev veth22
ip link set dev veth33 netns ns3
ip netns exec ns3 ip link set dev veth33 up
ip netns exec ns3 ip addr add 10.0.1.3/24 dev veth33

# 将veth pair的一端插入bridge
ip link set veth1 up
brctl addif BR1 veth1
ip link set veth2 up
brctl addif BR1 veth2
ip link set veth3 up
brctl addif BR1 veth3

# 测试
[root@localhost ~]# ip netns exec ns1 ping 10.0.1.3 -c 2
PING 10.0.1.3 (10.0.1.3) 56(84) bytes of data.
64 bytes from 10.0.1.3: icmp_seq=1 ttl=64 time=0.090 ms
64 bytes from 10.0.1.3: icmp_seq=2 ttl=64 time=0.068 ms

# veth3抓包
[root@localhost ~]# tcpdump -i veth3 -nn
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on veth3, link-type EN10MB (Ethernet), capture size 262144 bytes
11:05:30.660457 ARP, Request who-has 10.0.1.3 tell 10.0.1.1, length 28
11:05:30.660473 ARP, Reply 10.0.1.3 is-at 1a:16:88:9f:f1:c8, length 28
11:05:30.660479 IP 10.0.1.1 > 10.0.1.3: ICMP echo request, id 1325, seq 1, length 64
11:05:30.660504 IP 10.0.1.3 > 10.0.1.1: ICMP echo reply, id 1325, seq 1, length 64
11:05:31.700612 IP 10.0.1.1 > 10.0.1.3: ICMP echo request, id 1325, seq 2, length 64
11:05:31.700632 IP 10.0.1.3 > 10.0.1.1: ICMP echo reply, id 1325, seq 2, length 64

模拟bridge vlan trunk

记得清理之前的环境配置,以免影响该模拟环境

拓扑如下,两台终端连接至不同的交换机,实现互联互通。 交换机间的链路为Trunk,并且允许vlan10通过。

Linux网络虚拟化 Bridge_第2张图片

# 使用 ip 命令进行配置
# 创建交换机启用,并打开vlan filter功能
ip link add BR1 type bridge
ip link set BR1 up
ip link set dev BR1 type bridge vlan_filtering 1
ip link add BR2 type bridge
ip link set BR2 up
ip link set dev BR2 type bridge vlan_filtering 1

# 创建名称空间模拟终端
ip netns add ns1
ip netns add ns2

# 创建veth pair
ip link add veth1 type veth peer name veth11
ip link add veth2 type veth peer name veth22
ip link add veth12 type veth peer name veth21

# 按图示,将veth移入ns,并配置IP和启用
ip link set dev veth11 netns ns1
ip netns exec ns1 ip link set veth11 up
ip netns exec ns1 ip addr add 10.0.1.1/24 dev veth11
#
ip link set dev veth22 netns ns2
ip netns exec ns2 ip link set veth22 up 
ip netns exec ns2 ip addr add 10.0.1.2/24 dev veth22


# 按图示,将veth插入bridge并打上vlan tag
ip link set veth1 master BR1
ip link set veth1 up
bridge vlan add dev veth1 vid 10 pvid untagged
#
ip link set veth2 master BR2
ip link set veth2 up
bridge vlan add dev veth2 vid 10 pvid untagged
#
ip link set veth12 master BR1
ip link set veth12 up
bridge vlan add vid 2-4094 dev veth12  # trunk
#
ip link set veth21 master BR2
ip link set veth21 up
bridge vlan add vid 2-4094 dev veth21  # trunk

# 查看配置,端口和vlan tag情况
bridge -c vlan show


# 使用 ns1 到 ns2 进行 ping 测
[root@localhost ~]# ip netns exec ns1 ping 10.0.1.2 -c 2
PING 10.0.1.2 (10.0.1.2) 56(84) bytes of data.
64 bytes from 10.0.1.2: icmp_seq=1 ttl=64 time=0.119 ms
64 bytes from 10.0.1.2: icmp_seq=2 ttl=64 time=0.047 ms

# 在veth12抓包分析,报文带tag则成功
[root@localhost ~]# tcpdump -i veth12 -ne
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on veth12, link-type EN10MB (Ethernet), capture size 262144 bytes
10:59:23.576557 b2:82:aa:ee:05:05 > Broadcast, ethertype 802.1Q (0x8100), length 46: vlan 10, p 0, ethertype ARP, Request who-has 10.0.1.2 tell 10.0.1.1, length 28
10:59:23.576587 62:0d:66:a9:41:b2 > b2:82:aa:ee:05:05, ethertype 802.1Q (0x8100), length 46: vlan 10, p 0, ethertype ARP, Reply 10.0.1.2 is-at 62:0d:66:a9:41:b2, length 28
10:59:23.576593 b2:82:aa:ee:05:05 > 62:0d:66:a9:41:b2, ethertype 802.1Q (0x8100), length 102: vlan 10, p 0, ethertype IPv4, 10.0.1.1 > 10.0.1.2: ICMP echo request, id 1211, seq 1, length 64
10:59:23.576619 62:0d:66:a9:41:b2 > b2:82:aa:ee:05:05, ethertype 802.1Q (0x8100), length 102: vlan 10, p 0, ethertype IPv4, 10.0.1.2 > 10.0.1.1: ICMP echo reply, id 1211, seq 1, length 64
10:59:24.597118 b2:82:aa:ee:05:05 > 62:0d:66:a9:41:b2, ethertype 802.1Q (0x8100), length 102: vlan 10, p 0, ethertype IPv4, 10.0.1.1 > 10.0.1.2: ICMP echo request, id 1211, seq 2, length 64
10:59:24.597137 62:0d:66:a9:41:b2 > b2:82:aa:ee:05:05, ethertype 802.1Q (0x8100), length 102: vlan 10, p 0, ethertype IPv4, 10.0.1.2 > 10.0.1.1: ICMP echo reply, id 1211, seq 2, length 64

你可能感兴趣的:(Linux网络虚拟化,linux,网络,运维)