如何解析Linuxvethpair

本篇文章給大家分享的是有關(guān)如何解析Linux veth pair,小編覺得挺實用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

為西烏珠穆沁等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計制作服務(wù),及西烏珠穆沁網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為成都網(wǎng)站制作、網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)、西烏珠穆沁網(wǎng)站設(shè)計,以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達到每一位用戶的要求,就會得到認可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!

Linux veth pair 詳解

veth pair是成對出現(xiàn)的一種虛擬網(wǎng)絡(luò)設(shè)備接口,一端連著網(wǎng)絡(luò)協(xié)議棧,一端彼此相連。如下圖所示:

如何解析Linux veth pair

由于它的這個特性,常常被用于構(gòu)建虛擬網(wǎng)絡(luò)拓撲。例如連接兩個不同的網(wǎng)絡(luò)命名空間(netns),連接docker容器,連接網(wǎng)橋(Bridge)等,其中一個很常見的案例就是OpenStack Neutron底層用它來構(gòu)建非常復(fù)雜的網(wǎng)絡(luò)拓撲。

如何使用?

創(chuàng)建一對veth

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

實驗

我們改造上一節(jié)完成的netns實驗,使用veth pair將兩個的隔離netns連接起來。如下圖所示:

如何解析Linux veth pair

我們首先創(chuàng)建一對veth設(shè)備,將veth設(shè)備分別移動到兩個netns中并啟動。

# 創(chuàng)建一對veth
ip link add veth0 type veth peer name veth2
# 將veth移動到netns中
ip link set veth0 netns ns0
ip link set veth2 netns ns1
# 啟動
ip netns exec ns0 ip link set veth0 up
ip netns exec ns1 ip link set veth2 up

接下來我們測試一下。

使用ip netns exec ns0 ping 10.0.0.2在命名空間ns0中測試與tap1的網(wǎng)絡(luò)連通性。

PING 10.0.0.2 (10.0.0.2) 56(84) bytes of data.
From 10.0.0.1 icmp_seq=1 Destination Host Unreachable
From 10.0.0.1 icmp_seq=2 Destination Host Unreachable
From 10.0.0.1 icmp_seq=3 Destination Host Unreachable
^C
--- 10.0.0.2 ping statistics ---
5 packets transmitted, 0 received, +3 errors, 100% packet loss, time 77ms
pipe 4

使用ip netns exec ns1 ping 10.0.0.1在命名空間ns1中測試與tap0的網(wǎng)絡(luò)連通性。

PING 10.0.0.1 (10.0.0.1) 56(84) bytes of data.
From 10.0.0.2 icmp_seq=1 Destination Host Unreachable
From 10.0.0.2 icmp_seq=2 Destination Host Unreachable
From 10.0.0.2 icmp_seq=3 Destination Host Unreachable
^C
--- 10.0.0.1 ping statistics ---
4 packets transmitted, 0 received, +3 errors, 100% packet loss, time 108ms
pipe 4

什么情況?為什么網(wǎng)絡(luò)還是不通呢?答案就是路由配置有問題。

使用ip netns exec ns0 route -n查看ns0的路由表。

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 tap0

使用ip netns exec ns1 route -n查看ns1的路由表。

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 tap1

原來訪問10.0.0.0/24的流量都從tap設(shè)備發(fā)出去了,又因為tap設(shè)備沒有和其他設(shè)備相連,發(fā)出去的數(shù)據(jù)報文不會被處理,因此還是訪問不到目標(biāo)IP,我們來修改一下路由,讓訪問10.0.0.0/24的流量從veth設(shè)備發(fā)出。

#修改路由出口為veth
ip netns exec ns0 ip route change 10.0.0.0/24 via 0.0.0.0 dev veth0
ip netns exec ns1 ip route change 10.0.0.0/24 via 0.0.0.0 dev veth2

我們再來看一下路由

使用ip netns exec ns0 route -n查看ns0的路由表。

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 veth0

使用ip netns exec ns1 route -n查看ns1的路由表。

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 veth2

最后我們再來測試一下。

使用ip netns exec ns0 ping 10.0.0.2在命名空間ns0中測試與tap1的網(wǎng)絡(luò)連通性。

PING 10.0.0.2 (10.0.0.2) 56(84) bytes of data.
64 bytes from 10.0.0.2: icmp_seq=1 ttl=64 time=0.031 ms
64 bytes from 10.0.0.2: icmp_seq=2 ttl=64 time=0.035 ms
64 bytes from 10.0.0.2: icmp_seq=3 ttl=64 time=0.037 ms
64 bytes from 10.0.0.2: icmp_seq=4 ttl=64 time=0.043 ms
^C
--- 10.0.0.2 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 103ms
rtt min/avg/max/mdev = 0.031/0.036/0.043/0.007 ms

使用ip netns exec ns1 ping 10.0.0.1在命名空間ns1中測試與tap0的網(wǎng)絡(luò)連通性。

PING 10.0.0.1 (10.0.0.1) 56(84) bytes of data.
64 bytes from 10.0.0.1: icmp_seq=1 ttl=64 time=0.027 ms
64 bytes from 10.0.0.1: icmp_seq=2 ttl=64 time=0.047 ms
64 bytes from 10.0.0.1: icmp_seq=3 ttl=64 time=0.051 ms
64 bytes from 10.0.0.1: icmp_seq=4 ttl=64 time=0.042 ms
^C
--- 10.0.0.1 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 66ms
rtt min/avg/max/mdev = 0.027/0.041/0.051/0.012 ms

可以看到我們使用veth pair將兩個隔離的netns成功的連接到了一起。

但是這樣的網(wǎng)絡(luò)拓撲存在一個弊端,隨著網(wǎng)絡(luò)設(shè)備的增多,網(wǎng)絡(luò)連線的復(fù)雜度將成倍增長。 如果連接三個netns時,網(wǎng)絡(luò)連線就成了下圖的樣子

如何解析Linux veth pair

而如果連接四個netns時,網(wǎng)絡(luò)連線就成了下圖的樣子

如何解析Linux veth pair

如果有五臺設(shè)備。。。

有沒有什么技術(shù)可以解決這個問題呢?答案是有的,Linux Bridge(網(wǎng)橋)。下一節(jié)我們將使用網(wǎng)橋來將多個隔離的netns連接起來,這樣網(wǎng)絡(luò)連線就非常清爽了。

以上就是如何解析Linux veth pair,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

當(dāng)前題目:如何解析Linuxvethpair
當(dāng)前鏈接:http://bm7419.com/article34/jceose.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計、網(wǎng)站設(shè)計、外貿(mào)建站虛擬主機、建站公司、關(guān)鍵詞優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)