network namespace与veth pair
来源:互联网 发布:淘宝运费险换货赔吗 编辑:程序博客网 时间:2024/05/12 01:54
原文:https://yq.aliyun.com/articles/64855
network namespace与veth pair
作者
digoal
日期
2016-11-10
标签
namespace , veth pair , OpenvSwitch , OVS , bridge , VLAN , VM , 虚拟化 , 虚拟网桥
背景
在Linux虚拟化技术中,网络层面,通常除了网桥或虚拟交换机技术外。
还有一个重要的就是namespace和veth pair。
net namespace主要是隔离网络设备本身,例如在Linux 中有多个容器,每个容器对应各自的namespace,我们可以把不同的网络设备指派给不同的容器。
veth pair你可以理解为使用网线连接好的两个接口,把两个端口放到两个namespace中,那么这两个namespace就能打通。
如果要把namespace和本地网络打通,也可以创建veth设备,把两端分别放入本地和namespace。
下面是例子
http://www.cnblogs.com/hustcat/p/3928261.html
network namespace
创建network namespace
# ip netns add blue# ip netns listblue
添加网口到namespace
先创建veth
# ip link add veth0 type veth peer name veth1
在当前namespace可以看到veth0和veth1
# ip link list1: 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:002: 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:ff3: 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:ff4: 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一节。
主要参考
http://blog.scottlowe.org/2013/09/04/introducing-linux-network-namespaces/
veth pair
veth pair是用于不同network namespace间进行通信的方式,veth pair将一个network namespace数据发往另一个network namespace的veth。如下:
# add the namespacesip netns add ns1ip netns add ns2# create the veth pairip link add tap1 type veth peer name tap2# move the interfaces to the namespacesip link set tap1 netns ns1ip link set tap2 netns ns2# bring up the linksip netns exec ns1 ip link set dev tap1 upip netns exec ns2 ip link set dev tap2 up
如果多个network namespace需要进行通信,则需要借助bridge:
# add the namespacesip netns add ns1ip netns add ns2# create the switchBRIDGE=br-testbrctl addbr $BRIDGEbrctl stp $BRIDGE offip link set dev $BRIDGE up##### PORT 1# create a port pairip link add tap1 type veth peer name br-tap1# attach one side to linuxbridgebrctl addif br-test br-tap1# attach the other side to namespaceip link set tap1 netns ns1# set the ports to upip netns exec ns1 ip link set dev tap1 upip link set dev br-tap1 up##### PORT 2# create a port pairip link add tap2 type veth peer name br-tap2# attach one side to linuxbridgebrctl addif br-test br-tap2# attach the other side to namespaceip link set tap2 netns ns2# set the ports to upip netns exec ns2 ip link set dev tap2 upip 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; //转发给peerif (dev_forward_skb(rcv, skb) != NET_RX_SUCCESS)goto rx_drop;
NETIF_F_NETNS_LOCAL
NETIF_F_NETNS_LOCAL是网络设备的一个特性,设置该特性的网络设备,不允许在不同network namespace间移动。这类设备也叫做本地设备(local devices)。
Loopback,VXLAN,PPP,bridge都是这类设备。可以通过ethtool -k,或者ethtool –show- features查看该值:
# ethtool -k br0netns-local: on [fixed]
如果对这类设备network namespace,会报下面的错误:
# ip link set br0 netns ns1RTNETLINK answers: Invalid argument
参考《Resource management:Linux kernel Namespaces and cgroups》
主要参考
http://www.opencloudblog.com/?p=66
- network namespace与veth pair
- network namespace 与veth pair
- network namespace与veth pair
- veth pair
- veth pair
- neutron(1)veth pair
- Linux-虚拟网络设备-veth pair
- Network Namespace
- linux kernel network namespace
- Linux NetWork Namespace Technique
- Linux Network Namespace
- Linux Network Namespace
- network namespace tips
- linux network namespace
- linux network namespace 例子
- Linux Network Namespace
- Neutron OVS Bridge 连接方式 (veth pair / ovs peer) 的选型和性能测试
- (3) Linux Switching – Interconnecting Namespaces – brctl – ovs-vsctl – veth pair
- Java开发工具myeclipse和eclipse内存溢出解决方法之一
- JAVA下载单个文件并且返回客户端,不写入本地
- (三)Java设计模式之工厂方法模式
- Java Ecplise 导出Jar包
- VS Code 中Python 开发环境配置(Settings.json、launch.json、Tasks.json)
- network namespace与veth pair
- 事件总线EventBus3.0解读
- Axure RP 8 下载与最新激活码(可用注册码)
- 理解node.js(Understanding node.js)
- IOC、AOP 详解(spring)
- bzoj 1015
- Mybatis的Dao向mapper传多个参数(三种解决方案)
- (010) java后台开发之String字符串操作
- 用一个数组表示一个数,然后加一,返回这个数组