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.c

static 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;

 

NETIF_F_NETNS_LOCAL

NETIF_F_NETNS_LOCAL是网络设备的一个特性,设置该特性的网络设备,不允许在不同network namespace间移动。这类设备也叫做本地设备(local devices)。

Loopback,VXLAN,PPP,bridge都是这类设备。可以通过ethtool -k,或者ethtool –show- features查看该值:

# ethtool -k br0

netns-local: on [fixed]

 

如果对这类设备network namespace,会报下面的错误:

# ip link set br0 netns ns1

RTNETLINK answers: Invalid argument

 

参考《Resource management:Linux kernel Namespaces and cgroups》

主要参考

[0]Linux Switching – Interconnecting Namespaces

 

作者:YY哥 
出处:http://www.cnblogs.com/hustcat/ 
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

 

Docker实践(4)—network namespace与veth pair的更多相关文章

  1. linux 网络虚拟化: network namespace 简介

    linux 网络虚拟化: network namespace 简介 network namespace 是实现网络虚拟化的重要功能,它能创建多个隔离的网络空间,它们有独自的网络栈信息.不管是虚拟机还是 ...

  2. docker 实践十:docker 网络管理

    本篇是关于 docker 网络管理的内容,同时也包含了 docker 网络的高级应用. 注:环境为 CentOS7,docker 19.03. docker 网络基础 docker 网络模型 在 do ...

  3. 【转】理解Docker容器网络之Linux Network Namespace

    原文:理解Docker容器网络之Linux Network Namespace 由于2016年年中调换工作的原因,对容器网络的研究中断过一段时间.随着当前项目对Kubernetes应用的深入,我感觉之 ...

  4. 一文搞懂 Linux network namespace

    本文首发于我的公众号 Linux云计算网络(id: cloud_dev),专注于干货分享,号内有 10T 书籍和视频资源,后台回复「1024」即可领取,欢迎大家关注,二维码文末可以扫. 本文通过 IP ...

  5. docker 深入理解之namespace

    namespace 名称空间 docker容器主要通过资源隔离来实现的,应该具有的6种资源隔 namespace 的六项隔离 namespace 系统调用参数 隔离的内容 UTS CLONE_NEWU ...

  6. Linux ns 6. Network Namespace 详解

    文章目录 1. 简介 1.1 Docker Network 桥接模式配置 2. 代码解析 2.1 copy_net_ns() 2.2 pernet_list 2.2.1 loopback_net_op ...

  7. 【转】linux network namespace 学习

    原文地址:https://segmentfault.com/a/1190000004059167 介绍 在专业的网络世界中,经常使用到Virtual Routing and Forwarding(VR ...

  8. 容器虚拟网卡与网桥docker0虚拟网卡的veth pair的配对

    一)基本知识: Docker 安装时会自动在 host 上创建三个网络:none,host,和bridge:详细说明可参考其它文档.我们可用 docker network ls 命令查看: 基于DRI ...

  9. network namespace连接的4种方法及性能

    veth pair # add the namespaces ip netns add ns1 ip netns add ns2 # create the veth pair ip link add ...

随机推荐

  1. Razor标记语言介绍

    什么是Razor?   Razor的中文意思是"剃刀",它不是编程语言,只是一种服务器段的标记语言,与PHP和ASP类似   Razor允许你向网页中嵌入基于服务器的代码(Visu ...

  2. hdu 5682 zxa and leaf

    zxa and leaf  Accepts: 25  Submissions: 249  Time Limit: 5000/2500 MS (Java/Others)  Memory Limit: 6 ...

  3. Python 之 Bunch Pattern

    When prototyping (or even finalizing) data structures such as trees, it can be useful to have a flex ...

  4. JAVA对象转化JSON出现死循环问题

    主要是解决JSON因Hibernate映射生成的集合的转化出现的死循环问题. 这个方法很重要 public String ajaxJsonByObjectDirecdt(Object obj, Str ...

  5. Java的历史

    1991 绿色计划 (Green Project) 1991 年 1 月 一个名为"Green Project"的项目启动.该项旨在为家用电器提供支持,使这些电器智能化并且能够彼此 ...

  6. JqueryEasyUI浅谈---视频教程公布

    http://pan.baidu.com/s/1pJqGXez 前两天我在博客园发了一个关于JqueryEasyUI浅谈本地化应用的博客,我简单的介绍了JqueryEasyUI的应用,今天我录制了了一 ...

  7. ASP.NET Core 源码阅读笔记(2) ---Microsoft.Extensions.DependencyInjection生命周期管理

    在上一篇文章中我们主要分析了ASP.NET Core默认依赖注入容器的存储和解析,这一篇文章主要补充一下上一篇文章忽略的一些细节:有关服务回收的问题,即服务的生命周期问题.有关源码可以去GitHub上 ...

  8. Arcgis for Javascript 出现“init.js->TypeError: f is not a function”

    环境 采用离线JS包,版本为v3.8 问题描述 在为map添加了 app.map.on("pan-start", this.showHandBeignPan()); 在拖动地图的时 ...

  9. ASP.NET中的KRE是什么?

    KRE的英文全称是K Runtime Environment,其中K是ASP.NET vNext(后来叫ASP.NET 5)的项目代号. KRE是ASP.NET 5运行时环境,它是ASP.NET 5的 ...

  10. WPF Dispatcher 一次小重构

    几个月之前因为项目需要,需要实现一个类似于WPF Dispatcher类的类,来实现一些线程的调度.之前因为一直做Asp.Net,根本没有钻到这个层次去,做的过程中,诸多不顺,重构了四五次,终于实现, ...