Mininet Operations

本文介绍如何使用Mininet配置主机作为路由器,并演示DHCP服务器启动、添加NAT功能及建立GRE隧道等实验过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


http://csie.nqu.edu.tw/smallko/sdn/mininet-operations.htm


[Descriptions]

 In this lab, I will show how to configure a host as a router. How tostart a dhcp server at a router is also presented. Then I will use iptables tomake a router to own NAT ability. Finally, how to build a GRE tunnels betweentwo local networks is given.

 

[First Lab: configure a host as a router]

h1--h2--h3  (h2 will be configured as a router)

#!/usr/bin/env python

from mininet.cli import CLI

from mininet.net import Mininet

from mininet.link import Link,TCLink,Intf

 

if '__main__' == __name__:

  net = Mininet(link=TCLink)

  h1 = net.addHost('h1')

  h2 = net.addHost('h2')

  h3 = net.addHost('h3')

  Link(h1, h2)

  Link(h2, h3, intfName1='h2-eth1')

  net.build()

  h2.cmd('ifconfig h2-eth0 192.168.10.1 netmask 255.255.255.0')

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

  h2.cmd("echo 1 > /proc/sys/net/ipv4/ip_forward")

  h1.cmd("ifconfig h1-eth0 0")

  h3.cmd("ifconfig h3-eth0 0")

  h1.cmd("ip address add 192.168.10.2/24 dev h1-eth0")

  h1.cmd("ip route add default via 192.168.10.1 dev h1-eth0")

  h3.cmd("ip address add 192.168.20.2/24 dev h3-eth0")

  h3.cmd("ip route add default via 192.168.20.1 dev h3-eth0")

  CLI(net)

  net.stop()

 

 

[Second Lab: start a dhcp server]

h1--h2--h3  (h2 will be configured as a router.Also, a dhcp server is running at h2.)

Before theexperiment, use "sudo apt-get install isc-dhcp-server" command toinstall dhcp server in Ubuntu.

 

#!/usr/bin/env python

from mininet.cli import CLI

from mininet.net import Mininet

from mininet.link import Link,TCLink,Intf

 

if '__main__' == __name__:

  net = Mininet(link=TCLink)

  h1 = net.addHost('h1')

  h2 = net.addHost('h2')

  h3 = net.addHost('h3')

  Link(h1, h2, intfName1='h1-eth0', intfName2='h2-eth0')

  Link(h2, h3, intfName1='h2-eth1', intfName2='h3-eth0')

  net.build()

  h2.cmd('ifconfig h2-eth0 192.168.10.1 netmask 255.255.255.0')

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

  h2.cmd("echo 1 > /proc/sys/net/ipv4/ip_forward")

  h2.cmd("service isc-dhcp-server restart &")

  h1.cmd("ifconfig h1-eth0 0")

  h3.cmd("ifconfig h3-eth0 0")

  h1.cmd("dhclient h1-eth0")

  h3.cmd("dhclient h3-eth0")

  CLI(net)

  net.stop()

 

Before running themininet script, we have to configure the dhcp server. Edit the dhcpd.conf under/etc/dhcp

1

 

Running themininet script.

 

running thewireshark at h3 to monitor the traffic between h1 and h3.

 

 

 

From the followingfigure, we can see that the packets are transmitted between h1 (192.168.10.6)and h3 (192.168.20.6)  ---- Note:Different Domains.

 

[Third Lab: Add NAT function at h2]

h1-h2-h3 (h2 will be configured as arouter. Also, use iptables to let h2 have the NAT function)

#!/usr/bin/env python

from mininet.cli import CLI

from mininet.net import Mininet

from mininet.link import Link,TCLink,Intf

 

if '__main__' == __name__:

  net = Mininet(link=TCLink)

  h1 = net.addHost('h1')

  h2 = net.addHost('h2')

  h3 = net.addHost('h3')

  Link(h1, h2, intfName1='h1-eth0', intfName2='h2-eth0')

  Link(h2, h3, intfName1='h2-eth1', intfName2='h3-eth0')

  net.build()

  h2.cmd('ifconfig h2-eth0 192.168.10.1 netmask 255.255.255.0')

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

  h2.cmd("echo 1 > /proc/sys/net/ipv4/ip_forward")

  h2.cmd("iptables -t nat -A POSTROUTING -o h2-eth1 -s 192.168.10.0/24 -j MASQUERADE")

  h2.cmd("service isc-dhcp-server restart &")

  h1.cmd("ifconfig h1-eth0 0")

  h3.cmd("ifconfig h3-eth0 0")

  h1.cmd("dhclient h1-eth0")

  h3.cmd("dhclient h3-eth0")

  CLI(net)

  net.stop()

 

running themininet script

 

Check thefollowing figure, we can see that h1 can ping h3 successfully. But from the wiresharkwindow, we can see that the source address of packets sent by h1 will bemodified (NAT).

 

[Fourth Lab: GREtunnel]

 

h1---h2---h3---h4

 

h2,h3: router   h1, h4:host

h1-h2: LAN 1(10.0.0.0/24)

h3-h4: LAN 2(10.0.1.0/24)

h2-h3: LAN 3(192.168.10.0/24)

we will create aGRE tunnel between h2 and h3 (h2 will have a new ip address:10.0.2.1/30 whileh3 will have a new ip address:10.0.2.2/30)

Note: Without GREtunnel, h1 cannot ping h4.

#!/usr/bin/env python

from mininet.cli import CLI

from mininet.net import Mininet

from mininet.link import Link,TCLink,Intf

 

if '__main__' == __name__:

  net = Mininet(link=TCLink)

  h1 = net.addHost('h1')

  #h2 will be configured as a router

  h2 = net.addHost('h2')

  #h3 will be configured as a router

  h3 = net.addHost('h3')

  h4 = net.addHost('h4')

  Link(h1, h2, intfName1='h1-eth0', intfName2='h2-eth0')

  Link(h2, h3, intfName1='h2-eth1', intfName2='h3-eth0')

  Link(h3, h4, intfName1='h3-eth1', intfName2='h4-eth0')

  net.build()

  h2.cmd('ifconfig h2-eth0 10.0.0.1 netmask 255.255.255.0')

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

  h2.cmd("echo 1 > /proc/sys/net/ipv4/ip_forward")

  h3.cmd('ifconfig h3-eth0 192.168.10.2 netmask 255.255.255.0')

  h3.cmd('ifconfig h3-eth1 10.0.1.1 netmask 255.255.255.0')

  h3.cmd("echo 1 > /proc/sys/net/ipv4/ip_forward")

  h1.cmd("ifconfig h1-eth0 0")

  h4.cmd("ifconfig h4-eth0 0")

  h1.cmd("ip address add 10.0.0.2/24 dev h1-eth0")

  h1.cmd("ip route add default via 10.0.0.1 dev h1-eth0")

  h4.cmd("ip address add 10.0.1.2/24 dev h4-eth0")

  h4.cmd("ip route add default via 10.0.1.1 dev h4-eth0")

  #GRE Tunnel between h2 and h3

  h2.cmd("ip tunnel add tunnel0 mode gre remote 192.168.10.2 local 192.168.10.1 ttl 255")

  h2.cmd("ip link set tunnel0 up mtu 1400")

  h2.cmd("ip addr add 10.0.2.1/30 dev tunnel0")

  h2.cmd("ip route add 10.0.1.0/24 dev tunnel0")

  h3.cmd("ip tunnel add tunnel0 mode gre remote 192.168.10.1 local 192.168.10.2 ttl 255")

  h3.cmd("ip link set tunnel0 up mtu 1400")

  h3.cmd("ip addr add 10.0.2.2/30 dev tunnel0")

  h3.cmd("ip route add 10.0.0.0/24 dev tunnel0")

  CLI(net)

  net.stop()

 

Test 1: mark thered lines above.

 

Test 2: With GREtunnel between h2 and h3

 

 

Now, h1 can pingh4.

 

  

Dr. Chih-Heng Ke

Department of Computer Science and InformationEngineering, National Quemoy University, Kinmen, Taiwan

Email: smallko@gmail.com


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值