• Mininet Operations



    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


  • 相关阅读:
    PAT A1108 Finding Average [字符串处理]
    PAT A1013 Battle Over Cities [图的遍历]
    关于斐波那契数列的算法
    关于浏览器的事件队列
    类型与进制转换
    【待整理】python 关键字
    闭包和函数的自运行
    cookie-cart购物车
    有意思的效果——左右摇摆
    工厂模式
  • 原文地址:https://www.cnblogs.com/ztguang/p/12644957.html
Copyright © 2020-2023  润新知