Green Networks: Introduction To Mininet

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Green Networks

Introduction to Mininet
Dino Lpez
This lab session aims at introducing the Mininet platform, that will be used in this module in
order to deploy Software Defined Network (SDN applications). Mininet is a solution that
provides the needed tools and libraries in order to deploy virtual networks in a single (or
several) physical machine, by mean of a few commands (or a few lines of code). A virtual
Mininet network is composed by Virtual Machines (VMs) or real machines (able to execute
native software), by virtual or real switches or any other forwarding device.
For its aim, Mininet employs very advanced technologies, like Linux Containers and SDN
switches.

Setting up the Mininet platform


1. First of all, uncompress the mininet-x86_64.vmdk.gz file with the gunzip command. The
length of the compressed file is around 1.6GB, but once such a file is uncompressed, it will
take around 4.2GB in your file system.
2. Create a new VirtualBox VM, that you can label mininet for instance. Select Linux in
the OS type and Ubuntu (64 bits) for the Linux version. Regarding the RAM, you can choose
1024MB if your physical machine has more than 2GB. Finally, regarding the HD, choose the
one that you have uncompressed in step number 1.
3. The created mininet VM, will not have a Windows Manager. However, graphical applications
have been installed inside (e.g. wireshark) that you will probably use in other labs from
this course itself. So how are you going to proceed in order to correctly use such graphical
softwares? The solution that I prefer is configure the network of the VM in the Host-only
Adapter mode and then access it by ssh. In order to do that, in the main window of
VirtualBox click on File Preferences Network, then in the Host-only Networks
tab, verify that a module has been already created (e.g. vboxnet0). If the module does not
exist, then add a new module. Usually, when a very first module is created, it will be
automatically configured with the right values. However, it is always a goof idea to verify
that the IPv4 addresses and DHCP server parameters are well configured. Once the Hostonly adapter has been created, configure the network interface of the VM: right click on
your VM, then click on settings network. Select Host-only Adapter mode and select
also the host-only module (that you maybe just created). The instructions given in this
document are not clear enough? Then, you want to have a look at this website http://
christophermaier.name/blog/2010/09/01/host-only-networking-with-virtualbox or this one
http://technology.amis.nl/2014/01/27/a-short-guide-to-networking-in-virtual-box-withoracle-linux-inside/

4. Once the network of the VM is configured, turn on your VM, wait for the OS to load, and
then log in. The user/password for your mininet guest will be mininet/mininet. Now, open
a terminal in the host PC and connect to mininet by SSH
If everything is working well and you're already connected by SSH to you mininet guest, then
please proceed with the following exercises.

Your first contact with Mininet


The following exercises proposed in this document have been taken from the tutorial
available in the Mininet official website http://mininet.org/walkthrough/
Note: keep in mind that in order to execute the Mininet platform, you will need root rights
(i.e. use sudo). So start by executing mininet
$ sudo mn
If everything went OK, at the end you must see the Mininet CLI mininet>, and a virtual
network should be deployed. The default deployed topology is called minimal. All the
command available through the CLI can be listed with the help command.
mininet> help
The nodes created within our virtual networks can be listed with the nodes command
mininet> nodes
In Mininet, clients will be named h1, h2, hX. The switches will be named s1, s2, sX, and
the controllers (don't take care for now about such controllers) are named c0, c1, cX.
How many switches and clients are available in the deployed network?
The informations related to each node can be displayed with the dump command.
Explain what you have obtained with the dump command.
The links of our topology can be listed with the net command
Draw the topology of the deployed virtual network
Since the clients deployed by Mininet are images of the host PC (i.e. the PC where Mininet has
been installed), the mininet clients can access the user-space part and file system of the
host. Therefore, it is possible for a Mininet client to execute inside such clients the
commands and applications available in the host. In order to do that, you must first indicate
the name of the client to execute a command, followed by the command to be executed (as
you'd do in the real PC). For instance, in order to display the configuration of the NIC at the
h1 client, you must issue
mininet> h1 ifconfig a

Add to the topology drawn earlier, the NIC label, IP address and network mask associated to
each client.
Can the h1 client be reached by h2? Verify it thank to the ping command. Give the command
that you must issue through the CLI.
To leave from Mininet, you must excute the exit command. Important: if during the
deployment process of the virtual network some errors occur, or if you leave Mininet due to
an error message, the you must clean Mininet with the following command
$ sudo mn c
If you prefer to interact with a Mininet client like you do with a normal computer through a
terminal, the you can
I. Execute Mininet with -x option, that will open an xterm terminal for each deployed
node. E.g. $ sudo mn x
II. Individually open an xterm terminal emulator from the CLI. For instance
mininet> xterm h1 will open xterm for h1
Important! If you're working with the mininet VM by SSH, you must use X option in order to
activate the X11 forwarding, and hence, be able to correctly deploy the xterm terminal
emulator (for instance, ssh X [email protected]).

Other network topologies


Mininet features different templates able to create different network topologies, by providing
the --topo parameter at the Mininet startup. As written earlier, the default deployed
topology is the minimal one. However, you can use either the single, linear or tree
topology. The following command deploys the single topology:
$ sudo mn --topo single
the above command will deploy the minimal topology actually. However, with single, one
can deploy as many clients as required. For instance, issuing the following command $ sudo
mn topo single,3 will deploy 3 clients.
Execute the following commands and draw the created network topology:
1. $ sudo mn --topo linear
2. $ sudo mn --topo linear,3
3. $ sudo mn --topo tree
4. $sudo mn --topo tree,2
5. $sudo mn --topo tree,2,fanout=3. What are the objectives of the arguments 2 and
fanout=3 ? Explain it.

Custom topologies
What if someone would like to work with a network topology that is not provided by Mininet
templates? In that case, one can easily create the required network topology thanks to the
Python libraries and APIs provided by Mininet. Suppose for instance that we need to create a
client h1 directly connected to a second client named h2, and this last one to a switch s1.
Well, here is the Python code that will solve our problems
from mininet.topo import Topo

class Test_Topo(Topo):
def __init__(self):
"Create custom topology"

# Initialize topology
Topo.__init__(self)

# Add hosts and switches


h1 = self.addNode('h1')
h2 = self.addNode('h2')
s1 = self.addSwitch('s1')

# Add links
self.addLink(h2, s1)
self.addLink(h1, h2)

topos = {
'toptest': (lambda: Test_Topo())
}

Like exercise, write the above code in a toptest.py file and save it in the /home/mininet/
mytopos/ directory (or any one that you prefer). After that, execute the following command
$ sudo mn --custom /home/mininet/mytopos/toptest.py --topo toptest

If the network is successfully created, then you must end in the CLI environment. Open an
xterm for each client (h1 and h2) and explore their NIC configuration. How many interfaces
are attached to h1 and h2? What are the assigned IP addresses? Configure through the CLI the
h1 NIC, as well as the h2 NIC connected to h1 and assign them the IP addresses
192.168.0.1/24 and 192.168.0.2/24 respectively. How can you do to configure the NIC through
the CLI? One option is to use the ifconfig command (please refer to http://
www.tecmint.com/ifconfig-command-examples/ for an example).
Verify the connectivity between h1 and h2. Which command have you used?
Optional exercise: Create a Python script to create the following topology
h1 -------- s1 -------- s2 -------- s3 -------- h2

Almost the end


To be able to create a custom topology is very useful. However, it would be still better if,
instead of providing at each deployment the needed command to obtain a network
configuration, the configuration was done automatically by our Python script. Well, this is the
final exercise to be done in this lab.
Please, read and understand the following code, where we assign and IP address to the h2eth1 interface from h2. Then h1 executes a ping towards h2. As you can guess, to execute a
command from a given client, we use the cmd() method, by providing as argument the
command itself.
from mininet.net import Mininet
from mininet.topo import Topo
from mininet.node import Node
from mininet.cli import CLI
from mininet.link import TCLink
from mininet.log import setLogLevel

class Test_Topo(Topo):

topos = {
'toptest': (lambda: Test_Topo())
}

def topTest():
topo = Test_Topo()
net = Mininet(topo=topo, link=TCLink)
net.start()
h1 = net.get('h1')
h2 = net.get('h2')
h2int1 = h2.intf('h2-eth1')
h2.setIP('11.0.0.3', 8, h2int1)
print h1.cmd('ping -c 3 %s' % h2.IP(h2int1))
CLI(net); # this line allows to land into the CLI
net.stop(); # we need to clean up everything before leaving

if __name__ == '__main__':
setLogLevel('info')
topTest()
Please note that this time, we execute our code by directly calling the Python interpreter
$ sudo python /home/mininet/mytopos/toptest.py
As exercises, add the code shown above in your toptest.py file and execute it. Can h1 reach
h2? If h2 is unreachable, provide through the CLI the needed configurations to provide full
connectivity between h1 and h2. Once you have found the way to enable the connectivity
between h1 and h2, modify your Python code in order to execute all those commands
automatically from your script.

You might also like