Mininet consists of a few network topologies that are predefined. You may define your own topology. In this recipe, we are using tree topology, which is probably the most common and the most basic network topology. We ping one emulated host from another emulated host.
Listing 9.2 emulates a simple network with a tree topology:
#!/usr/bin/env python # Python Network Programming Cookbook, Second Edition -- Chapter - 9 # This program is optimized for Python 2.7.12. # It may run on any other version with/without modifications. import argparse from mininet.net import Mininet from mininet.topolib import TreeTopo # Emulate a network with depth of depth_ and fanout of fanout_ def emulate(depth_, fanout_): # Create a network with tree topology tree_ = TreeTopo(depth=depth_,fanout=fanout_) # Initiating the Mininet instance net = Mininet(topo=tree_) # Start Execution of the Emulated System. net.start() # Name two of the instances as h1 and h2. h1, h2 = net.hosts[0], net.hosts[depth_] # Ping from an instance to another, and print the output. print (h1.cmd('ping -c1 %s' % h2.IP())) # Stop the Mininet Emulation. net.stop() if __name__ == '__main__': parser = argparse.ArgumentParser(description='Mininet
Simple Emulation') parser.add_argument('--depth', action="store",
dest="depth", type=int, required=True) parser.add_argument('--fanout', action="store",
dest="fanout", type=int, required=True) given_args = parser.parse_args() emulate(given_args.depth, given_args.fanout)
Tree topology is defined by its depth and fanout. A network configured to be a complete tree can have (fanout) to the power of depth number of leaves. As the hosts make the leaves, this leads us to (fanout) to the power of depth number of hosts in the network. The following figure illustrates a network of tree topology with Fanout = 2 and Depth = 3:
The following output is printed when the program is executed with the depth of 2 and fanout of 3 as the arguments:
$ sudo python 19_2_mininet_emulation.py --depth=2 --fanout=3 PING 10.0.0.3 (10.0.0.3) 56(84) bytes of data. 64 bytes from 10.0.0.3: icmp_seq=1 ttl=64 time=2.86 ms --- 10.0.0.3 ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 2.864/2.864/2.864/0.000 ms