簡(jiǎn)介
VLAN是網(wǎng)絡(luò)棧的一個(gè)附加功能,且位于下兩層。首先來學(xué)習(xí)Linux中網(wǎng)絡(luò)棧下兩層的實(shí)現(xiàn),再去看如何把VLAN這個(gè)功能附加上去。下兩層涉及到具體的硬件設(shè)備,日趨完善的Linux內(nèi)核已經(jīng)做到了很好的代碼隔離,對(duì)網(wǎng)絡(luò)設(shè)備驅(qū)動(dòng)也是如此,如下圖所示:
這里要注意的是,Linux下的網(wǎng)絡(luò)設(shè)備net_dev并不一定都對(duì)應(yīng)實(shí)際的硬件設(shè)備,只要注冊(cè)一個(gè)struct net_device{}結(jié)構(gòu)體(netdevice.h)到內(nèi)核中,那么這個(gè)網(wǎng)絡(luò)設(shè)備就存在了。該結(jié)構(gòu)體很龐大,其中包含設(shè)備的協(xié)議地址(對(duì)于IP即IP地址),這樣它就能被網(wǎng)絡(luò)層識(shí)別,并參與路由系統(tǒng),最有名的當(dāng)數(shù)loopback設(shè)備。不同的設(shè)備(包括硬件和非硬件)的ops操作方法各不相同,由驅(qū)動(dòng)自己實(shí)現(xiàn)。一些通用性的、與設(shè)備無關(guān)的操作流程(如設(shè)備鎖定等)則被Linux提煉出來,我們稱為驅(qū)動(dòng)框架。
linux虛擬網(wǎng)絡(luò)設(shè)備之vlan配置
我們通過一個(gè)網(wǎng)橋兩個(gè)設(shè)備對(duì),來連接兩個(gè)網(wǎng)絡(luò)名字空間,每個(gè)名字空間中創(chuàng)建兩個(gè)vlan
借助vconfig來配置vlan:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
#創(chuàng)建網(wǎng)橋 brctl addbr br- test -vlan #創(chuàng)建veth對(duì)兒 ip link add veth01 type veth peer name veth10 ip link add veth02 type veth peer name veth20 #將veth對(duì)兒的一段添加到網(wǎng)橋 brctl addif br- test -vlan veth01 brctl addif br- test -vlan veth02 #啟動(dòng)設(shè)備 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 #創(chuàng)建網(wǎng)絡(luò)名字空間 ip netns add test -vlan-vm01 ip netns add test -vlan-vm02 #將設(shè)備對(duì)兒的另一端添加到另個(gè)名字空間(其實(shí)在一個(gè)名字空間也能玩,只是兩個(gè)名字空間更加形象) ip link set veth10 netns test -vlan-vm01 ip link set veth20 netns test -vlan-vm02 #分別進(jìn)入兩個(gè)名字空間創(chuàng)建vlan和配置ip #配置名字空間test-vlan-vm01 ip netns exec test -vlan-vm01 bash #配置vlan 3001 和 vlan 3002 vconfig add veth10 3001 vconfig add veth10 3002 #啟動(dòng)兩個(gè)vlan的設(shè)備 ip link set veth10.3001 up ip link set veth10.3002 up #分別在兩個(gè)vlan上配置ip (這里簡(jiǎn)單起見,使用了同一個(gè)網(wǎng)段了IP,缺點(diǎn)是,需要了解一點(diǎn)兒路由的知識(shí)) 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 #啟動(dòng)兩個(gè)vlan的設(shè)備 ip link set veth20.3001 up ip link set veth20.3002 up #分別在兩個(gè)vlan上配置ip (這里簡(jiǎn)單起見,使用了同一個(gè)網(wǎng)段了IP,缺點(diǎn)是,需要了解一點(diǎn)兒路由的知識(shí)) 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配置:
1
2
3
4
5
|
# 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 |
現(xiàn)在,我們可以分別在兩個(gè)名字空間來ping另外一個(gè)名字空間的兩個(gè)IP,雖然兩個(gè)IP都能ping通,但是使用的源IP是不同的,走的vlan也是不同的,我們可以在veth01/veth10/veth02/veth20/br-test-vlan 任意一個(gè)上抓包,會(huì)看到vlan信息:
1
2
3
4
5
6
7
8
9
|
# 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 是不能通的,因?yàn)槭遣煌膙lan呀:
1
2
3
4
5
|
# 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的解法:
1
|
ip link add link veth10 name veth10.3001 type vlan id 3001 |
另: vlan 一般以 設(shè)備名.vlanid 來命名,不過并非強(qiáng)制,如下命名為 vlan3003也是沒問題的
1
|
# ip link add link veth10 name vlan3003 type vlan id 3003 |
注意:一個(gè)主設(shè)備上相同vlan好的子設(shè)備最多只能有一個(gè)
1
2
|
# ip link add link veth10 name vlan3001 type vlan id 3001 RTNETLINK answers: File exists |
所以,正常來講,一般是這樣的:
參考: http://network.51cto.com/art/201504/473419.htm
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)服務(wù)器之家的支持。
原文鏈接:https://phpor.net/blog/post/7109