主机双网卡实验

实验目标

为一台主机配置两张网卡,每张网卡属于不同的子网,使两个子网的主机能够互相ping通。

实验过程

由于没有物理环境,此处使用mininet模拟。

在物理机上运行 ryu-manager 开启控制器

定义主机、交换器、控制器:

h1 = net.addHost( 'h1', mac='00:00:00:00:00:01', ip='10.0.0.1/24' )
h2 = net.addHost( 'h2', mac='00:00:00:00:00:02', ip='10.0.0.2/24' )
h3 = net.addHost( 'h3', mac='00:00:00:00:00:03', ip='20.0.0.1/24' )

s1 = net.addSwitch( 's1', listenPort=6673, mac='00:00:00:00:00:11' )
s2 = net.addSwitch( 's2', listenPort=6674, mac='00:00:00:00:00:12' )

c0 = net.addController( 'c0', controller=RemoteController, ip='127.0.0.1', port=6633 )

定义 \((s_1,h_1),(s_2,h_3)\) 的连接:

net.addLink(s1, h1, 1, 0)
net.addLink(s2, h3, 1, 0)

\(h_2\) 的不同网卡连接到不同的交换机上:

Link(h2, s1, intfName1='h2-eth0')
Link(h2, s2, intfName1='h2-eth1')

h2-eth1 赋 IP

h2.cmd('ifconfig h2-eth1 20.0.0.2 netmask 255.255.255.0')

启动mininet后,给 \(h_2\) 开启内核路由转发参数:

h2.cmd('sysctl net.ipv4.ip_forward=1')

\(h_1,h_3\) 配置网关:

h1.cmd('route add -net 20.0.0.0 netmask 255.255.255.0 gw 10.0.0.2')
h3.cmd('route add -net 10.0.0.0 netmask 255.255.255.0 gw 20.0.0.2')

启动mininet后,给交换机下规则:

sh ovs-ofctl add-flow s1 in_port=1,actions=output:2
sh ovs-ofctl add-flow s1 in_port=2,actions=output:1
sh ovs-ofctl add-flow s2 in_port=1,actions=output:2
sh ovs-ofctl add-flow s2 in_port=2,actions=output:1

测试一下:

mininet> pingall
*** Ping: testing ping reachability
h1 -> h2 X 
h2 -> h1 h3 
h3 -> X X 
*** Results: 50% dropped (3/6 received)

结果比较糟糕。

查看此时 \(h_1,h_3\) 的路由表:

mininet> h1 route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 h1-eth0
mininet> h3 route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
20.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 h3-eth0

发现之前定义的路由表并没有生效,再次手动添加:

mininet> h1 route add -net 20.0.0.0 netmask 255.255.255.0 gw 10.0.0.2
mininet> h3 route add -net 10.0.0.0 netmask 255.255.255.0 gw 20.0.0.2

再次进行测试

mininet> pingall
*** Ping: testing ping reachability
h1 -> h2 h3 
h2 -> h1 h3 
h3 -> h1 h2 
*** Results: 0% dropped (6/6 received)

完美。

附录

main.py

#!/usr/bin/python
import time
from mininet.net import Mininet
from mininet.node import Controller, RemoteController, OVSKernelSwitch,UserSwitch
from mininet.cli import CLI
from mininet.log import setLogLevel
from mininet.link import Link, TCLink

def topology():

    "Create a network."
    net = Mininet( controller=RemoteController, link=TCLink, switch=OVSKernelSwitch )

    print "*** Creating nodes ***"
    h1 = net.addHost( 'h1', mac='00:00:00:00:00:01', ip='10.0.0.1/24' )
    h2 = net.addHost( 'h2', mac='00:00:00:00:00:02', ip='10.0.0.2/24' )
    h3 = net.addHost( 'h3', mac='00:00:00:00:00:03', ip='20.0.0.1/24' )
    s1 = net.addSwitch( 's1', listenPort=6673, mac='00:00:00:00:00:11' )
    s2 = net.addSwitch( 's2', listenPort=6674, mac='00:00:00:00:00:12' )
    c0 = net.addController( 'c0', controller=RemoteController, ip='127.0.0.1', port=6633 )

    print "*** Creating links ***"
    net.addLink(s1, h1, 1, 0)
    net.addLink(s2, h3, 1, 0)

    Link(h2, s1, intfName1='h2-eth0')
    Link(h2, s2, intfName1='h2-eth1')
    h2.cmd('ifconfig h2-eth1 20.0.0.2 netmask 255.255.255.0')


    print "*** Starting network ***"
    net.build()
    c0.start()
    s1.start( [c0] )
    s2.start( [c0] )

    print "*** Running CLI ***"
    CLI( net )

    h2.cmd('sysctl net.ipv4.ip_forward=1')

    h1.cmd('route add -net 20.0.0.0 netmask 255.255.255.0 gw 10.0.0.2')
    h3.cmd('route add -net 10.0.0.0 netmask 255.255.255.0 gw 20.0.0.2')

    print "*** Stopping network ***"
    net.stop()

if __name__ == '__main__':
    setLogLevel( 'info' )
    topology()
posted @ 2023-05-10 16:32  xDaniel  阅读(163)  评论(0编辑  收藏  举报