首頁  >  文章  >  運維  >  linux vlan配置詳解

linux vlan配置詳解

小云云
小云云原創
2017-12-14 11:01:486874瀏覽

本文主要為大家介紹了關於linux虛擬網路設備之vlan配置的相關資料,文中透過範例程式碼介紹的非常詳細,對大家的學習或工作具有一定的參考學習價值,希望能幫助大家。

簡介

VLAN是網路堆疊的附加功能,且位於下兩層。首先來學習Linux中網路棧下兩層的實現,再去看如何把VLAN這個功能附加上去。下兩層涉及特定的硬體設備,日趨完善的Linux核心已經做到了很好的程式碼隔離,對網路設備驅動也是如此,如下圖所示:

這裡要注意的是,Linux下的網路設備net_dev並不一定都對應實際的硬體設備,只要註冊一個struct net_device{}結構體(netdevice.h)到核心中,那麼這個網路設備就存在了。這個結構體很龐大,其中包含設備的協定位址(對於IP即IP位址),這樣它就能被網路層識別,並參與路由系統,最有名的當數loopback設備。不同的設備(包括硬體和非硬體)的ops操作方法各不相同,由驅動自己實現。一些通用性的、與設備無關的操作流程(如設備鎖定等)則被Linux提煉出來,我們稱為驅動框架。

linux虛擬網路設備之vlan配置

我們透過一個網橋兩個裝置對,來連接兩個網路名字空間,每個名字空間中建立兩個vlan

#借助vconfig來設定vlan:

#创建网桥
brctl addbr br-test-vlan 
#创建veth对儿
ip link add veth01 type veth peer name veth10
ip link add veth02 type veth peer name veth20 
#将veth对儿的一段添加到网桥
brctl addif br-test-vlan veth01
brctl addif br-test-vlan veth02 
#启动设备
ip link set dev br-test-vlan up
ip link set dev veth01 up
ip link set dev veth02 up
ip link set dev veth10 up
ip link set dev veth20 up 
#创建网络名字空间
ip netns add test-vlan-vm01
ip netns add test-vlan-vm02 
#将设备对儿的另一端添加到另个名字空间(其实在一个名字空间也能玩,只是两个名字空间更加形象)
ip link set veth10 netns test-vlan-vm01
ip link set veth20 netns test-vlan-vm02 
#分别进入两个名字空间创建vlan和配置ip
#配置名字空间test-vlan-vm01
ip netns exec test-vlan-vm01 bash
#配置vlan 3001 和 vlan 3002
vconfig add veth10 3001
vconfig add veth10 3002
#启动两个vlan的设备
ip link set veth10.3001 up
ip link set veth10.3002 up 
#分别在两个vlan上配置ip (这里简单起见,使用了同一个网段了IP,缺点是,需要了解一点儿路由的知识)
ip a add 172.16.30.1/24 dev veth10.3001
ip a add 172.16.30.2/24 dev veth10.3002 
#添加路由
route add 172.16.30.21 dev veth10.3001
route add 172.16.30.22 dev veth10.3002 
#配置名字空间test-vlan-vm02
ip netns exec test-vlan-vm02 bash
#配置vlan 3001 和 vlan 3002
vconfig add veth20 3001
vconfig add veth20 3002
#启动两个vlan的设备
ip link set veth20.3001 up
ip link set veth20.3002 up
#分别在两个vlan上配置ip (这里简单起见,使用了同一个网段了IP,缺点是,需要了解一点儿路由的知识)
ip a add 172.16.30.21/24 dev veth20.3001
ip a add 172.16.30.22/24 dev veth20.3002 
#添加路由
route add 172.16.30.1 dev veth20.3001
route add 172.16.30.2 dev veth20.3002

查看vlan設定:

# cat /proc/net/vlan/config 
VLAN Dev name | VLAN ID
Name-Type: VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD
veth10.3001 | 3001 | veth10
veth10.3002 | 3002 | veth10

現在,我們可以分別在兩個名字空間來ping另外一個名字空間的兩個IP,雖然兩個IP都能ping通,但使用的來源IP是不同的,走的vlan也是不同的,我們可以在veth01/veth10/veth02/veth20/br-test-vlan 任一個上抓包,會看到vlan訊息:

# tcpdump -i veth10 -nn -e
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on veth10, link-type EN10MB (Ethernet), capture size 262144 bytes
15:38:18.381010 82:f7:0e:2d:3f:62 > 9e:58:72:fa:11:15, ethertype 802.1Q (0x8100), length 102: vlan <span style="color: #ff0000;">3001</span>, p 0, ethertype IPv4, <strong><span style="color: #ff0000;">172.16.30.1 > 172.16.30.21</span></strong>: ICMP echo request, id 19466, seq 1, length 64
15:38:18.381183 9e:58:72:fa:11:15 > 82:f7:0e:2d:3f:62, ethertype 802.1Q (0x8100), length 102: vlan <span style="color: #ff0000;"><strong>3001</strong></span>, p 0, ethertype IPv4, 172.16.30.21 > 172.16.30.1: ICMP echo reply, id 19466, seq 1, length 64
15:38:19.396796 82:f7:0e:2d:3f:62 > 9e:58:72:fa:11:15, ethertype 802.1Q (0x8100), length 102: vlan 3001, p 0, ethertype IPv4, 172.16.30.1 > 172.16.30.21: ICMP echo request, id 19466, seq 2, length 64
15:38:19.396859 9e:58:72:fa:11:15 > 82:f7:0e:2d:3f:62, ethertype 802.1Q (0x8100), length 102: vlan 3001, p 0, ethertype IPv4, 172.16.30.21 > 172.16.30.1: ICMP echo reply, id 19466, seq 2, length 64
15:38:23.162052 82:f7:0e:2d:3f:62 > 9e:58:72:fa:11:15, ethertype 802.1Q (0x8100), length 102: vlan 3002, p 0, ethertype IPv4, 172.16.30.2 > <strong><span style="color: #ff0000;">172.16.30.22</span></strong>: ICMP echo request, id 19473, seq 1, length 64
15:38:23.162107 9e:58:72:fa:11:15 > 82:f7:0e:2d:3f:62, ethertype 802.1Q (0x8100), length 102: vlan 3002, p 0, ethertype IPv4, <strong><span style="color: #ff0000;">172.16.30.22 > 172.16.30.2</span></strong>: ICMP echo reply, id 19473, seq 1, length 64

如果試圖從veth10.3001 去ping 172.16.30.22 是不能通的,因為是不同的vlan呀:

# ping -I veth10.3001 172.16.30.22
PING 172.16.30.22 (172.16.30.22) from 172.16.30.1 veth10.3001: 56(84) bytes of data.
^C
--- 172.16.30.22 ping statistics ---
9 packets transmitted, 0 received, 100% packet loss, time 8231ms

不適用vconfig的解法:

ip link add link veth10 name veth10.3001 type vlan id 3001

另: vlan 一般以  裝置名稱.vlanid 來命名,不過並非強制,如下命名為vlan3003也是沒問題的

# ip link add link veth10 name vlan3003 type vlan id 3003

注意:一個主裝置上相同vlan好的子設備最多只能有一個

# ip link add link veth10 name vlan3001 type vlan id 3001
 RTNETLINK answers: File exists

所以,正常來講,一般是這樣的:

相關推薦:

動手實作Linux VLAN

VLAN學習總結

交換器VLAN設定

#VLAN學習總結#############交換器VLAN設定######

以上是linux vlan配置詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn