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!!!")
28 "{0} <switch_count> <host_per_switch> <base_mac: Eg:00:4b:00:00:00:00 > \
29 <base_ip: Eg:75.75.0.0>".format(
30 sys.argv[0].split("/")[-1]
34 "Dpid of switches is derived from base mac and \
35 host ip address is derived from base ip"
39 switch_count = int(sys.argv[1])
40 host_per_switch = int(sys.argv[2])
41 base_mac = sys.argv[3]
42 base_host_ip = sys.argv[4]
44 base_host_mac = base_mac.split(":")
45 base_host_mac[0] = "10"
46 base_host_mac = (":").join(base_host_mac)
47 dpid_mac = base_mac.split(":")
48 dpid_mac = ("").join(dpid_mac)
51 def new_mac(mac, offset):
53 Description: This function increments an existing mac address by offset
54 and returns the new mac
55 :param mac: Mac address with each hex separated by a colon
56 (Eg: 00:01:02:03:04:05
57 :param offset: Increment the mac to this offset value
58 :return: new mac in same format as input mac.
60 mac = netaddr.EUI(mac).value
62 mac = str(netaddr.EUI(mac)).replace("-", ":")
66 def new_ip(ip, offset):
68 Description: This function increments an existing ip address by offset
69 and returns the new ip
70 :param ip: Ipv4 address string
71 :param offset: increment value of IP
72 :rtype : new Ipv4 address string after incrementing by offset
74 ip = netaddr.IPAddress(ip)
75 return ip.__add__(offset)
78 def new_dpid(mac, offset):
80 Description: This function is for returns a new dpid by
81 incrementing a mac address by offset value.
82 :param mac: mac address separated by colon
83 :param offset: increment value
86 mac = netaddr.EUI(mac).value
88 mac = str(netaddr.EUI(mac)).replace("-", ":")
89 dpid_mac = mac.split(":")
90 dpid_mac = ("").join(dpid_mac)
91 DPID = "0000" + dpid_mac
95 if __name__ == "__main__":
96 DPID = new_dpid(base_mac, 1)
97 HMAC = new_mac(base_host_mac, 1)
98 HIP = new_ip(base_host_ip, 1)
100 configfile = open("switch.py", "w")
102 '"""@author: sandeep gangadharan\n \
103 This topology has {0:d} switches {1:d} hosts \
104 \nThis topology is made out of {2:s} script \
105 \nThis is a fully mesh topology. Not available in mininet by default.\
106 \nHence generating this python file dynamically""" \
107 \nfrom mininet.topo import Topo\nclass DemoTopo(Topo): \
109 switch_count, switch_count * host_per_switch, sys.argv[0]
113 "This topology has %d switches %d hosts"
114 % (switch_count, switch_count * host_per_switch)
116 configfile.write(" def __init__(self):\n ")
117 configfile.write(" # Initialize topology\n")
118 configfile.write(" Topo.__init__(self)\n")
119 configfile.write(" # Add Switches\n")
121 for i in range(1, switch_count + 1):
123 " s{0:d} = self.addSwitch('s{1:d}',dpid='{2:s}')\
128 DPID = new_dpid(base_mac, i + 1)
131 configfile.write(" # Add Hosts\n")
132 for i in range(1, switch_count + 1):
133 for j in range(1, host_per_switch + 1):
135 " self.addLink(s{0:d}, \
136 self.addHost('s{1:d}h{2:d}',\
137 ip='{3:s}',mac='{4:s}',prefixLen='{5:d}'))\n".format(
138 i, i, j, HIP, HMAC, prefix
141 HMAC = new_mac(HMAC, 1)
145 configfile.write(" # Add Links\n")
147 for i in range(1, switch_count + 1):
150 for j in range(1, i + 1):
153 " self.addLink(s{0:d}, s{1:d})\
158 configfile.write("topos = { 'demotopo': ( lambda: DemoTopo() ) }")