MiniNet host talking to Internet

I’ve seen the nat.py example that comes with MiniNet.  But I was wondering if there was another way to have a MiniNet host communicate with the internet.  Since I am running MiniNet inside a VirtualBox VM, I thought I would just connect one of the external interfaces on the VM to the OVS switch running in MiniNet.  I also didn’t want to configure static IP address on all the hosts, so I decided to use DHCP.  Now MiniNet hosts only support being created with static IP addresses, but there is a shortcut to have them do DHCP.  So the basic steps to get MiniNet hosts using DHCP to get an address and connect to the internet are as follows.

  • In the VirtualBox settings for your VM, add an additional interface to the VM and set it to NAT.
  • Start up your VM.   In my case, the new interface that was added was called eth2 in the VM.  Don’t do any special configuration in the VM for the interface, just leave it as is.
  • Create a simple one host one switch network and attach the VM interface (eth2 in my case) to the switch.
  • Start your MiniNet network.
  • Open an xterm to your MiniNet host and issue the following. Note, ny host is called ‘h1’.
    • ifconfig h1-eth0 inet 0.0.0.0
    • dhclient h1-eth0

After that is done, you should be able to communicate with the internet.  I tested it by pinging http://www.google.com.

Note that, instead of adding a new interface to your VirtualBox VM as NAT setting, you could have added it as ‘Bridge Adapter’ and then the host would be communicating on your LAN getting an IP address from your LAN’s DHCP server.  But in some cases, that is not desired scenerio.

So I made a small MiniNet script, see below, that automates the above steps and you can use as an example on how to add it to your scripts.

#!/usr/bin/python

from mininet.net import Mininet
from mininet.node import Controller
from mininet.cli import CLI
from mininet.link import Intf
from mininet.log import setLogLevel, info

def myNetwork():

    net = Mininet( topo=None,
                   build=False)

    info( '*** Adding controller\n' )
    net.addController(name='c0')

    info( '*** Add switches\n')
    s1 = net.addSwitch('s1')
    Intf( 'eth2', node=s1 )

    info( '*** Add hosts\n')
    h1 = net.addHost('h1', ip='0.0.0.0')

    info( '*** Add links\n')
    net.addLink(h1, s1)

    info( '*** Starting network\n')
    net.start()
    h1.cmdPrint('dhclient '+h1.defaultIntf().name)
    CLI(net)
    net.stop()

if __name__ == '__main__':
    setLogLevel( 'info' )
    myNetwork()

And then you can see it in action below.

mininet@mininet-vm:~$ sudo ./hostInternet.py
*** Adding controller
*** Add switches
*** Add hosts
*** Add links
*** Starting network
*** Configuring hosts
h1
*** Starting controller
*** Starting 1 switches
s1
*** h1 : ('dhclient h1-eth0',)
*** Starting CLI:
mininet> h1 ping -c 1 www.google.com
PING www.google.com (74.125.226.112) 56(84) bytes of data.
64 bytes from yyz08s13-in-f16.1e100.net (74.125.226.112): icmp_req=1 ttl=55 time=11.6 ms

--- www.google.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 11.692/11.692/11.692/0.000 ms

Hope you found this useful.

This post ‘MiniNet host talking to Internet’ first appeared on https://gregorygee.wordpress.com/.

 

Advertisement

33 Responses to MiniNet host talking to Internet

  1. Rajiv Ranjan says:

    I have tried following
    1. Added new interface (NAT) in VMware.
    2. Trying to create network with simple topology:
    (added following line in the same script you have mentioned.)

    h1 = net.addHost(‘h1′, ip=’0.0.0.0’)
    and removed:
    h1.cmdPrint(‘dhclient ‘+h1.defaultIntf().name)

    But. script throws error like:

    mininet> h1 ping -c 1 74.125.239.56
    connect: Network is unreachable

    Any clue?

    • Rajiv Ranjan says:

      Instead of getting the IP from DHCP, I have assigned following:
      h1 = net.addHost(‘h1′, ip=’10.0.0.1′)

      I am able to ping external IP from switch s1 but not from host h1.

      • gregorygee says:

        Well, there are a few steps we can take to start to debug this.

        – After you added the new interface to your VM, what is it called in the VM? If you are using the MiniNet VM, then it won’t show by default. You can run something like ‘cat /proc/net/dev’ to see a list of all the interfaces.

        – With MiniNet not running, can you run ‘dhclient’ on the interface to see if the VM is even able to get an IP address?

        If you can get you VM to get an address, then at least you will know what IP address range it will use.

        Greg

    • gregorygee says:

      Why did you remove the ‘dhclient’ line? That command is the DHCP client that gets the address.

  2. Jayraj says:

    i used the same script as given by you.the script does not throw any error but when i do
    h1 ping http://www.google.com
    it throws an error-

    ping: unknown host http://www.google.com

  3. Pedro says:

    Hello. I did what you wrote above and it worked, but I used Host-Only Adapters. I have a doubt and maybe you’ĺl know how to solve it. I’m planning to connect an Openflow Physical Switch (HP) together with the switches generated by Mininet (OVS). Since all hosts will have an address of this network and mask: 192.168.56.0/24, I’m planning to do something like this: HOST 1 —- HP SWITCH —- Physical Host —– OpenVSwitch —- HOST 2. What do you think is the best way to connect the HP Switch to the OpenVSwitch and makes possible the communication between H1 and H2?

    • gregorygee says:

      This example gets you most of the way. In the sample, a host is connected to OVS and then OVS is connected to eth2 in the VM. To do what you want, you will then need to bridge the VM eth2 with an available physical port on your physical host, then connect that port on the physical port to your HP switch. Now you have the Host–OVS–HP part done. The part that isn’t done is having the other host. Did you want that second host to be a real host or another one of Mininet hosts. If you want the second host to be another Mininet host, you will need to dedicate another physical port on your physical host to the Mininet host and then plug that physical port into another port on your HP.

      • Pedro says:

        Thanks. It will be necessary to use the Bridge adapter in this case, because, for now, I’m using the Host-Only Adapter to connect the physical host to the Mininet. So I just need to connect the HP switch to the physical port of the host containing Mininet and then connect the HP switch to H1(also a physical host). I was reading through your blog some related to SDN, I’m connecting my OVS switches to a external controller (Floodlight) and now I also want to connect the HP switch to the same controller (using the same Floodlight instance for both switches), using the same topology that I said before. Unfortunately, I can’t test this right now, but do you think I will have some problems to make this work?
        Obs:My controller will be located in the physical machine that contains Mininet.

      • gregorygee says:

        There should be no problems with having OVS and the HP switch use the same controller.

      • Pedro says:

        Hi again, sorry to disturb you. I was doing some search about this on the internet, but didn’t find some that could solve my problem. Now, I’m running Mininet on my physical host (no VirtualBox), I tried to create a virtual interface (e.g: eth0:1) and connect it to my Mininet OVS, but I couldn’t ping my lan’s devices from my virtual network nodes. I also have only one physical interface on my physical host, this interface is connected to my Lan. Is there are way to make the communication between my Lan and my virtual network generated by Mininet?

        Thanks again.

      • gregorygee says:

        If you only have one physical interface, you might want to create some TAP interfaces that your Mininet networks can use and then bridge the TAP interfaces with your physical interface. I found this link here as a good quick explanation of how to create a TAP interface and bridge it to eth0. The virtual interface you were using might work, but I have no way to test and debug that at the moment.

      • haojun says:

        Good Post. Here is my situation:i build the mininet on a physical host(10.136.78.52) which has only one interface(eth0), and want to ping another physical host(10.136.78.220). So i create a virtual bridge and tap on 52:
        ————————-
        brctl addbr br0 # create a virtual bridge
        brctl addif br0 eth0 # add eth0 to br0
        tunctl -t tap0 # create a tap device
        brctl addif br0 tap0 # add tap to br
        ifconfig br0 up
        ifconfig eth0 up
        ifconfig tap0 up
        dhclient br0
        ————————–
        In python, I use Intf(‘tap0’, node=s1) to bind tap0 and s1 (the topology is h1-s1-s2-h2)
        But then i stucked at h1 dhclient h1-eth0, the host can not get ip from dhcp.
        And then i set static ip for h1 and h2, with the same range and mask of br0(10.136.78.52), but i still cannot ping the outside host. And br0 is also unreachable.
        do you have any idea what’s wrong here? thanks

  4. rishi ranjan says:

    script is running fine and it is starting the switch and controller as well but it is not starting the CLI. so for that what could be issue or do i need to wait for some period of time….???
    help me to understand i am beginner

    • gregorygee says:

      If you are not getting the CLI, then the script is probably still stuck on the previous line.

      h1.cmdPrint(‘dhclient ‘+h1.defaultIntf().name)

      If it is really stuck here, there are many parts to look at. This post has a long list of comments about debugging the environment to get things to work.

  5. Lavanya says:

    Hello, thank you for the post. i followed the steps but as in one of the comments above i am able to ping from switch and not from host. and i checked my interface it doesn’t have IP. I am not using mininet VM, instead i am using installed mininet on Ubuntu. is it supposed to work the same way as it is on VM. In hwintf.py of mininet examples there is one method to check if the interface is having IP if it has then that script fails. Do we need the Ip for additional interface or not?

    • gregorygee says:

      There should not be any IP on the interface that you want to use. As for pinging, the switch doesn’t have an IP address. You should be trying to ping from a host in Mininet, through the switch, to the interface and out. Also, make sure the IP addresses used inside Mininet for hosts are in the same IP subnet as the other devices connected to the external interface.

  6. mostafa ammar says:

    hello,

    thanks alot for your effort,I tried that and from mininet I can issue “s1 ping 192.168.1.254(my internet router ip” and working fine , this means that the switch is connected successfully to physical network ,I have 2 questions

    1) how the switch pings however no ip address is specified for it
    2)how can I make the host h1 ping physical network

    Thanks again for you

    • gregorygee says:

      1) You aren’t pinging from the switch. The switches are not running inside a network namespace like the Mininet hosts. You were pinging from outside of Mininet, not inside.

      2) Did the Mininet host get an IP address? You should run ‘h1 ifconfig’ to see if you host got an IP address. If not, there are MANY replies in this post to help debug the problem. There could be many reasons. I don’t know if you are trying this from within a VM or natively installed on your PC. Have a look through the other replies, I’m sure you answer will be in there.

  7. Sumi says:

    Again repeated query but doesn’t solve.
    plz suggest something!

    I used the same script as given by you.the script does not throw any error but when i do
    mininet> h1 ping -c 1 http://www.google.com

    it throws an error-
    ping: unknown host http://www.google.com

    • gregorygee says:

      From your Mininet VM, are you able to do this same ping. The first test you should try to see of the DHCP worked would be to ping your internet router from Mininet. If that works, then your script was fine. Next step would be to see if you have DNS resolution issues. This all depends on what you VM’s DNS resolution is set to. Check to make sure that is working. But as I just mentioned, it would be best to ping using an IP address first to rule out other issues.

  8. Ceron says:

    Thank you. It works.

    If you have any problem, try to use mininet 2.2 VM [1] running on Fusion/VMware.
    I had problems in VirtualBox. The exactly same script running in VirtualBox (using two interface correctly configured) wasn’t working.

    VM configuration:

    eth0 (NAT) – management
    eth1 (NAT) – minnet -> Internet

    [1] https://github.com/mininet/mininet/wiki/Mininet-VM-Images

  9. zafar says:

    Hi gregorygee!,
    i followed your blog and i am working something like this. when i execute your given python script in my mininet console it says “run as root” when i run it as root user it works fine. but after the script when i try to open xterm host1(as you mentioned) it doesn’t open it. and i got to learn that if you are a root user you can’t open xterm. so i was wondering that is there a way to get through this?

  10. Amir Jamil says:

    Thanks for this post. Am I right in thinking that it is possible for a host from the Internet to reach the host in the Mininet network using this setup?

  11. mike says:

    Sorry i’m new to Mininet.
    What do you mean by “create a simple one host one switch network and attach the VM interface (eth2 in my case) to the switch.”

    • Gregory Gee says:

      I recommend reading the tutorials for Mininet first so you get an understanding of what Mininet really does. To basically answer your question, you will create a new mininet setup that has 1 switch and 1 host in it. Then, you will assign one of the interfaces on your PC to be directly connected to the switch.

  12. Lee Min Zhao says:

    Hi

    Apparently, the code is stuck when dhclient is being executed after I added a remote controller. Is there anyway to fix this?

  13. Hi, thanks for your post. I use the script mentioned before and it works well in my machine. However, when I change the mode of eth1 from NAT to bridge, it cannot work. It seems that it cannot DHCP h1. How can I configure it?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: