In this post, we are going to present an abstract view of the flow of control in the code of Mininet.
Download the source code of Mininet from their GitHub repository. It is also recommended that you play around with the software before starting off to understand the source code.
Mininet has been quickly going through many updates. Mininet version 2.1 was in use on the date on which this post was written. You might see certain variation in the source at the later stages.
cd into the mininet folder. You will see many directories in it but the 2 most important directories are the bin directory and the mininet directory.
Directory bin contains mn, this is the file from where the execution of the program begin.
Lines like:
[...]
from mininet.clean import cleanup
from mininet.cli import CLI
[...]
These files contains the class definition which exist inside the mininet directory. So be prepared to make jumps from one file to another when we walk through the source.
Then we come across codes like these:
[...]
TOPODEF = 'minimal'
TOPOS = { 'minimal': lambda: SingleSwitchTopo( k=2 ),
'linear': LinearTopo,
'reversed': SingleSwitchReversedTopo,
'single': SingleSwitchTopo,
'tree': TreeTopo }
[...]
Here, we are actually creating a dictionary of classes of type Topo. When we enter command to emulate a linear topology or a tree topology, we are actually mapping it to their corresponding classes in the later stage. If you wish to add a new type of object in the code, you should add a member to the dictionary. It has been similarly used for other object creation like SWITCHES, HOSTS, etc.
Then we have some function definitions that's called later. It all starts from the constructor call to MininetRunner() from the bottom of the source of mn.
This calls the init() method inside MininetRunner class. They initialize some class members and then call the parseArgs() function. parseArgs() parses all the arguments you passed in the command line and stores them for further use. Commands not passed in the command-line have a default value set in the program. If you wish to add a new feature in Mininet, you might want to make some changes in here to provide end users with the option to choose from the command-line.
begin() function makes use of the input stored by parseArgs() to start the process of emulating the network.
[...]
topo = buildTopo( TOPOS, self.options.topo )
switch = customConstructor( SWITCHES, self.options.switch )
host = customConstructor( HOSTS, self.options.host )
controller = customConstructor( CONTROLLERS, self.options.controller )
link = customConstructor( LINKS, self.options.link )
[...]
These functions are defined in another file. One good implementation technique of Mininet which we really appreciate is that the developers have mentioned these function names in the import files. It's really convenient to hop to that file to see what that function does.
Coming back to the code, buildTopo takes in the TOPOS dictionary of classes defined on top of mn file and the options.topo stores the type of topo the user had specified in the command-line.
The buildTopo function, maps the topology passed by the user into it's corresponding class and returns the mapped class call back to the topo variable in mn. After this, topo represents the object of
Other objects like switch, host, controller and link also create objects in the similar manner
We then assign all the command-line or default initialized to variables and call the class using keyword parameters.
[...]
inNamespace = self.options.innamespace
Net = MininetWithControlNet if inNamespace else Mininet
[...]
If you don't pass --innamespace in the command-line, inNamespace is set to false by default. Net is hence assigned to Mininet class that's imported from mininet/net.py.
[...]
mn = Net( topo=topo,
switch=switch, host=host, controller=controller,
link=link,
ipBase=ipBase,
inNamespace=inNamespace,
xterms=xterms, autoSetMacs=mac,
autoStaticArp=arp, autoPinCpus=pin,
listenPort=listenPort )
[...]
Hence a constructor call to class Net in mininet/net.py is called and an object of the entire emulated network is returned.
In the next post we will cover about what happens in the constructor call to class Net.