Docker實踐(4)—network namespace與veth pair

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   for   資料   

network namespace建立network namespace

# ip netns add blue

# ip netns list

blue

 

添加網口到namespace

先建立veth

# ip link add veth0 type veth peer name veth1

在當前namespace可以看到veth0和veth1

# ip link list

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:b2:cf:72 brd ff:ff:ff:ff:ff:ff

3: veth1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000

    link/ether ae:0d:00:e1:11:38 brd ff:ff:ff:ff:ff:ff

4: veth0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000

    link/ether 42:e7:50:d4:bb:c5 brd ff:ff:ff:ff:ff:ff

 

將veth1加到namespace “blue”

# ip link set veth1 netns blue

此時,當前namepapce只能看到veth0。

通過如下命令可以查看blue namespace的網口

# ip netns exec blue ip link list

 配置network namespace的網口

通過ip netns exec可以配置namespace的網口

# ip netns exec blue ifconfig veth1 172.17.42.2/16 up

network namespace的網口與物理網卡的通訊

通過bridge來實現。參見veth pair一節。

 

主要參考

[0]Introducing Linux Network Namespaces

 veth pair

veth pair是用於不同network namespace間進行通訊的方式,veth pair將一個network namespace資料發往另一個network namespace的veth。如下:

 

# add the namespaces

ip netns add ns1

ip netns add ns2

# create the veth pair

ip link add tap1 type veth peer name tap2

# move the interfaces to the namespaces

ip link set tap1 netns ns1

ip link set tap2 netns ns2

# bring up the links

ip netns exec ns1 ip link set dev tap1 up

ip netns exec ns2 ip link set dev tap2 up

 

如果多個network namespace需要進行通訊,則需要藉助bridge:

# add the namespaces

ip netns add ns1

ip netns add ns2

# create the switch

BRIDGE=br-test

brctl addbr $BRIDGE

brctl stp   $BRIDGE off

ip link set dev $BRIDGE up

#

#### PORT 1

# create a port pair

ip link add tap1 type veth peer name br-tap1

# attach one side to linuxbridge

brctl addif br-test br-tap1

# attach the other side to namespace

ip link set tap1 netns ns1

# set the ports to up

ip netns exec ns1 ip link set dev tap1 up

ip link set dev br-tap1 up

#

#### PORT 2

# create a port pair

ip link add tap2 type veth peer name br-tap2

# attach one side to linuxbridge

brctl addif br-test br-tap2

# attach the other side to namespace

ip link set tap2 netns ns2

# set the ports to up

ip netns exec ns2 ip link set dev tap2 up

ip link set dev br-tap2 up

#

 

核心實現

veth的實現與loopback interface類似,比較簡單:

//drivers/net/veth.cstatic netdev_tx_t veth_xmit(struct sk_buff *skb, struct net_device *dev){       struct net_device *rcv = NULL;       struct veth_priv *priv, *rcv_priv;        priv = netdev_priv(dev);       rcv = priv->peer;       rcv_priv = netdev_priv(rcv);        stats = this_cpu_ptr(priv->stats);        length = skb->len;   //轉寄給peer       if (dev_forward_skb(rcv, skb) != NET_RX_SUCCESS)              goto rx_drop;

 

主要參考

[0]Linux Switching – Interconnecting Namespaces

 


YY哥 
出處:http://www.cnblogs.com/hustcat/ 
本文著作權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文串連,否則保留追究法律責任的權利。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.