neutron(1)veth pair

来源:互联网 发布:sqlserver count 编辑:程序博客网 时间:2024/04/29 14:52

主要参考

[0]Introducing Linux Network Namespaces

 

veth pair

veth pair是用于不同network namespace间进行通信的方式,veth pair将一个network namespace数据发往另一个network namespace的veth。如下:

NewImage

      



Veth pair 是一对虚拟网卡,从一张veth网卡发出的数据包可以直接到达它的peer veth,两者之间存在着虚拟链路。

        Veth 网卡和常规的以太网区别仅在于xmit接口:将数据发送到其peer,触发peer的Rx 过程。
        Veth 的原理示意图如下:
        \
        
        Veth 的实现在linux/drivers/net/veth.c 下,总体来看比较简单:
        关键的数据结构:


  1. struct veth_priv {
  2.     struct net_device __rcu    *peer;
  3.     atomic64_t        dropped;
  4. };
        在初始化时veth_newlink 函数中有:
  1. /*
  2.  * tie the deviced together
  3.  */

  4.  priv = netdev_priv(dev);
  5.  rcu_assign_pointer(priv->peer, peer);

  6.  priv = netdev_priv(peer);
  7.  rcu_assign_pointer(priv->peer, dev);

        在发送函数veth_xmit中有:
   
  1.     rcv = rcu_dereference(priv->peer);
  2.     ...
  3.     if (likely(dev_forward_skb(rcv, skb)== NET_RX_SUCCESS)){
  4.         struct pcpu_vstats *stats = this_cpu_ptr(dev->vstats);

  5.         u64_stats_update_begin(&stats->syncp);
  6.         stats->bytes+= length;
  7.         stats->packets++;
  8.         u64_stats_update_end(&stats->syncp);
  9.     } else{
        
        应用方式:通过net namespace 做实验如下:
        
  1. #!/bin/sh

  2. echo "create net namespace net0 and net1"
  3. ip netns add net0
  4. ip netns add net1

  5. echo "list net namespace"
  6. ip netns list

  7. echo "add veth pair v1 and vp1"
  8. ip link add veth_0 type veth peer name veth_0_peer
  9. ip link

  10. echo "set veth_0 in net0"
  11. ip link set veth_0 netns net0
  12. echo "set veth_0_peer in net1"
  13. ip link set veth_0_peer netns net1

  14. ip netns exec net0 ip addr add local 10.0.78.3/24 dev veth_0
  15. ip netns exec net0 ifconfig veth_0 up
  16. ip netns exec net1 ip addr add local 10.0.78.4/24 dev veth_0_peer
  17. ip netns exec net1 ifconfig veth_0_peer up

  18. echo "show ip netns net0"
  19. ip netns exec net0 ip addr
  20. echo "show ip netns net1"
  21. ip netns exec net1 ip addr

  22. ip netns exec net1 ping 10.0.78.3
0 0