6 __author__ = "Sandeep Gangadharan"
7 __copyright__ = "(c) Copyright [2015] Hewlett-Packard \
8 Development Company, L.P."
9 __license__ = "Eclipse Public License "
10 __email__ = "sandeep.gangadharan@hp.com"
11 __created__ = "19 March 2014"
15 Description : Creates Fully mesh mininet topology.
16 Input : switch_count, host count per switch, base mac address,
18 Output : switch.py (A python topology file)
19 Note : This is a fully mesh network. Not available in
20 mininet by default. Hence generating a python file
21 dynamically which represents the topology.
26 print("Please povide correct inputs. Exiting!!!")
27 print "{0} <switch_count> <host_per_switch> <base_mac: Eg:00:4b:00:00:00:00 > \
28 <base_ip: Eg:75.75.0.0>".format(sys.argv[0].split('/')[-1])
29 print "Dpid of switches is derived from base mac and \
30 host ip address is derived from base ip"
33 switch_count = int(sys.argv[1])
34 host_per_switch = int(sys.argv[2])
35 base_mac = sys.argv[3]
36 base_host_ip = sys.argv[4]
38 base_host_mac = base_mac.split(':')
39 base_host_mac[0] = '10'
40 base_host_mac = (':').join(base_host_mac)
41 dpid_mac = base_mac.split(':')
42 dpid_mac = ('').join(dpid_mac)
45 def new_mac(mac, offset):
47 Description: This function increments an existing mac address by offset
48 and returns the new mac
49 :param mac: Mac address with each hex separated by a colon
50 (Eg: 00:01:02:03:04:05
51 :param offset: Increment the mac to this offset value
52 :return: new mac in same format as input mac.
54 mac = netaddr.EUI(mac).value
56 mac = str(netaddr.EUI(mac)).replace('-', ':')
60 def new_ip(ip, offset):
62 Description: This function increments an existing ip address by offset
63 and returns the new ip
64 :param ip: Ipv4 address string
65 :param offset: increment value of IP
66 :rtype : new Ipv4 address string after incrementing by offset
68 ip = netaddr.IPAddress(ip)
69 return ip.__add__(offset)
72 def new_dpid(mac, offset):
74 Description: This function is for returns a new dpid by
75 incrementing a mac address by offset value.
76 :param mac: mac address separated by colon
77 :param offset: increment value
80 mac = netaddr.EUI(mac).value
82 mac = str(netaddr.EUI(mac)).replace('-', ':')
83 dpid_mac = mac.split(':')
84 dpid_mac = ('').join(dpid_mac)
85 DPID = "0000" + dpid_mac
89 if __name__ == "__main__":
90 DPID = new_dpid(base_mac, 1)
91 HMAC = new_mac(base_host_mac, 1)
92 HIP = new_ip(base_host_ip, 1)
94 configfile = open("switch.py", 'w')
95 configfile.write('\"\"\"@author: sandeep gangadharan\n \
96 This topology has {0:d} switches {1:d} hosts \
97 \nThis topology is made out of {2:s} script \
98 \nThis is a fully mesh topology. Not available in mininet by default.\
99 \nHence generating this python file dynamically\"\"\" \
100 \nfrom mininet.topo import Topo\nclass DemoTopo(Topo): \
101 \n'.format(switch_count, switch_count * host_per_switch, sys.argv[0]))
102 print "This topology has %d switches %d hosts" \
103 % (switch_count, switch_count * host_per_switch)
104 configfile.write(" def __init__(self):\n ")
105 configfile.write(" # Initialize topology\n")
106 configfile.write(" Topo.__init__(self)\n")
107 configfile.write(" # Add Switches\n")
109 for i in range(1, switch_count + 1):
110 configfile.write(" s{0:d} = self.addSwitch(\'s{1:d}\',dpid=\'{2:s}\')\
111 \n".format(i, i, DPID))
112 DPID = new_dpid(base_mac, i + 1)
115 configfile.write(" # Add Hosts\n")
116 for i in range(1, switch_count + 1):
117 for j in range(1, host_per_switch + 1):
118 configfile.write(" self.addLink(s{0:d}, \
119 self.addHost('s{1:d}h{2:d}',\
120 ip='{3:s}',mac='{4:s}',prefixLen='{5:d}'))\n"
121 .format(i, i, j, HIP, HMAC, prefix))
122 HMAC = new_mac(HMAC, 1)
126 configfile.write(" # Add Links\n")
128 for i in range(1, switch_count + 1):
131 for j in range(1, i + 1):
133 configfile.write(" self.addLink(s{0:d}, s{1:d})\
135 configfile.write("topos = { 'demotopo': ( lambda: DemoTopo() ) }")