Linux上の基礎ネットワーク設備のまとめ


1 Linux上の基礎ネットワーク設備の詳細と動作原理
http://www.ibm.com/developerworks/cn/linux/1310_xiawcnetworkdevice/
2 ip操作
VLANs management
ip link add link eth0 name eth0.2 type vlan id 2
ip link add link eth0 name myvlan type vlan id 2 loose_binding on
ip link delete myvlan type vlan
ip link add foo type vlan help
This replacces the obsoliete vconfig(8)program and adds new feature.
The loose_binding flags stops the VLAN interface from trocing the line protocol status of the undering device.
Creation of TUN/TAP interfaces
ip tuntap add dev mytap mode tap user md
This replacces the obsoluete tunctl(8)program.
クリエーションof dummy interfaces
ip link add mydummy type dummy
The only way to create more dummy interfaces after the dummy module has been loaded used to be loading it again with a different name,and they weree all named dummyN.Since module-nit to ols does nopportime
Ethernet in GRE tunnels
ip link add mygretun type gretap remote 192.0.2.1
ip link add foo type gretap help
A practical way to remotely bridge tworks.The IP MTU is reduced of the expected 20(IP)+4(GRE)+14(Ethernet II)bytes.
3仮想ネットワークデバイス–VTH pair
Veth pairは仮想ネットワークカードのペアであり、1枚のvethネットワークカードから送信されるパケットは直接にそのpeer vethに到達し、両者の間に仮想リンクが存在する.Vethネットカードと従来のイーサネットの違いは、xmitインターフェースだけにあります.データをそのpeerに送り、peerのRxプロセスをトリガします.Vethの原理概略図は、Linux上的基础网络设备总结_第1张图片実験である.
#!/bin/sh
echo "create net namespace net0 and net1"
ip netns add net0
ip netns add net1
echo "list net namespace"
ip netns list
echo "add veth pair v1 and vp1"
ip link add veth_0 type veth peer name veth_0_peer
ip link
echo "set veth_0 in net0"
ip link set veth_0 netns net0
echo "set veth_0_peer in net1"
ip link set veth_0_peer netns net1
ip netns exec net0 ip addr add local 10.0.78.3/24 dev veth_0
ip netns exec net0 ifconfig veth_0 up
ip netns exec net1 ip addr add local 10.0.78.4/24 dev veth_0_peer
ip netns exec net1 ifconfig veth_0_peer up
echo "show ip netns net0"
ip netns exec net0 ip addr
echo "show ip netns net1"
ip netns exec net1 ip addr
ip netns exec net1 ping 10.0.78.3
veth pairは、別のnetwork namespace間で通信するための方法であり、veth pairは、別のnetwork namespaceデータを別のnetwork namespaceのvethに送る.Linux上的基础网络设备总结_第2张图片操作
# 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:Linux上的基础网络设备总结_第3张图片を介して動作する必要がある.
# 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;
4 tun/tap
参考:1http://backreference.org/2010/03/26/tuntap-interface-tutorial/ 2http://blog.csdn.net/zhaihaifei/article/details/23168621
5 bridge
参考:http://blog.csdn.net/zhaihaifei/article/details/38581247
6 vlan
参考:http://blog.csdn.net/zhaihaifei/article/details/38562047
7 network namespace
network namespaceを作成します.
# ip netns add blue
# ip netns list
ネットワークを追加してnamespaceにvethを作成します.
# ip link add veth0 type veth peer name veth1
現在のnamespaceではveth 0とveth 1が見られます.
# ip link list
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0:  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:  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:  mtu 1500 qdisc noop state DOWN qlen 1000
link/ether 42:e7:50:d4:bb:c5 brd ff:ff:ff:ff:ff:ff
namespace「blue」にveth 1を追加します.
# ip link set veth1 netns blue
この時、namepapceはveth 0しか見えません.次のコマンドで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/