三种方式使用vlan (by quqi99)

作者:张华  发表于:2016-04-22
版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明

( http://blog.csdn.net/quqi99 )

Use OVS port in QEMU

sudo apt-get install qemu-system qemu-kvm virtinst libvirt-bin openvswitch-datapath-source openvswitch-controller openvswitch-switch virt-top virt-manager Python-libvirt

sudo ovs-vsctl add-br br-mano
sudo ovs-vsctl add-port br-mano eth2

sudo virsh net-destroy default
sudo virsh net-define /tmp/br-mano.xml
<network>
  <name>br-mano</name>
  <forward mode='bridge'/>
  <bridge name='br-mano'/>
  <virtualport type='openvswitch'/>
</network>
#sudo virsh net-undefine default
sudo virsh net-start br-mano  
sudo virsh net-autostart br-mano
 

Linux Bridge VLAN

sudo modprobe 8021q
sudo ip link add link eth1 name eth1.2 type vlan id 2
#sudo vconfig add eth1 2
#sudo ifconfig eth1.2 down
#sudo vconfig rem eth1.2
sudo ip link set eth1.2 up
sudo brctl addbr br2
sudo brctl setfd br2 0
sudo brctl stp br2 on
sudo ip link set br2 up
sudo brctl addif br2 eth1.2
sudo ifconfig br2 192.168.9.122/24
sudo ip tuntap add gw2 mode tap
sudo ip link set gw2 up
sudo brctl addif br2 gw2
hua@node1:~$ ip -d link show eth1.2
22: eth1.2@eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master br2 state UP mode DEFAULT group default qlen 1000
    link/ether 2c:53:4a:02:20:3c brd ff:ff:ff:ff:ff:ff promiscuity 1
    vlan protocol 802.1Q id 2 <REORDER_HDR>

可用如下方法定义VLAN

auto eth0.2
iface eth0.2 inet static
    address 10.12.2.2/24
    netmask 255.255.255.0
    network 10.12.2.0/24
    broadcast 10.12.2.255

如果失败,先确保安装了vlan包 (sudo apt install -y vlan),并且启用了vlan模块(sudo modprobe 8021q), 如果还报下列错的话是因为需要将网卡名改成规范的eth0之类的。

Dec  6 08:54:50 localhost ifup[29785]: Cannot find device "enp0s25.2"
Dec  6 08:54:50 localhost ifup[29785]: Failed to bring up enp0s25.2.

OVS Bridge VLAN

sudo ovs-vsctl add-br br-veth0
sudo ovs-vsctl add-port br-veth0 eth1
sudo ip link add veth0 type veth peer name veth1
sudo ovs-vsctl add-port br-veth0 veth0
sudo ovs-vsctl add-port br-mano veth1
sudo ip link set veth0 up
sudo ip link set veth1 up
#sudo ovs-vsctl add-port br-veth0 veth0 -- set Interface veth0 type=patch options:peer=veth1
#sudo ovs-vsctl add-port br-mano veth1 -- set Interface veth1 type=patch options:peer=veth0
#sudo ovs-vsctl del-port br-mano veth1
#sudo ovs-vsctl del-port br-veth0 veth0

#Create ACCESS VLAN:
sudo ovs-vsctl set port vnet0 tag=2
#sudo ovs-vsctl remove port vnet0 tag 2
#Enable both ACCESS VLAN as well as TRUNK VLAN:
sudo ovs-vsctl set port vnet0 vlan_mode=trunk trunks=2 #access, native-tagged, native-untagged, trunk
#sudo ovs-vsctl set port eth2 vlan_mode=access trunks=[]
 

Verify VLAN

Inside VM: ping 10.0.3.1 -I eth0

sudo  tcpdump -i eth1  -e -n 'arp or icmp' and src host 10.0.3.1


listening on eth1, link-type EN10MB (Ethernet), capture size 65535 bytes
11:38:20.754894 52:54:00:f2:17:37 > ff:ff:ff:ff:ff:ff, ethertype 802.1Q (0x8100), length 46: vlan 2, p 0, ethertype ARP, Request who-has 10.0.3.1 tell 10.0.3.2, length 28

Conclusion

OVS Bridge br-mano上的VM无论是采用在VM里打Tag还是在br-mano vnet0处打Tag后的vlan流量能到达eth1(vlan数据只在物理网卡上能使用tcpdump看到),但无法到达同一机器上linux bridge br2上的eth1.2,反之亦然

sudo tcpdump -leni eth0 vlan 100 -l

sudo tcpdump -i any -e '((arp or icmp) and (host 172.16.0.254 or host 172.16.0.2))' -l |grep -v tap

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

quqi99

你的鼓励就是我创造的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值