mininet-sample/topos/ecmp_single_interface.py

212 lines
7.3 KiB
Python
Raw Normal View History

2020-06-24 08:36:26 +00:00
from core.topo import Topo, TopoConfig, TopoParameter
from struct import *
2020-06-24 10:28:44 +00:00
class ECMPSingleInterfaceTopo(Topo):
NAME = "ECMPLike"
2020-06-29 07:51:55 +00:00
def __init__(self, topo_builder, parameterFile):
super(ECMPSingleInterfaceTopo, self).__init__(topo_builder, parameterFile)
2020-06-24 10:28:44 +00:00
print("Hello ECMP topo")
2020-06-29 07:51:55 +00:00
self.client = self.add_host(Topo.CLIENT_NAME)
self.server = self.add_host(Topo.SERVER_NAME)
self.lswitch = self.add_switch(Topo.SWITCH_NAME_PREFIX + "0")
self.rswitch = self.add_switch(Topo.SWITCH_NAME_PREFIX + "1")
2020-06-24 10:28:44 +00:00
2020-06-29 07:51:55 +00:00
self.add_link( self.client, self.lswitch)
self.add_link( self.server, self.rswitch)
2020-06-24 10:28:44 +00:00
self.routers = []
2020-06-29 07:51:55 +00:00
for l in self.topo_parameter.link_characteristics:
2020-06-24 10:28:44 +00:00
self.routers.append(self.addOneRouterPerLink(l))
print("added : " + self.routers[-1])
2020-06-29 07:51:55 +00:00
self.add_link(self.lswitch, self.routers[-1])
self.add_link(self.rswitch, self.routers[-1], **l.as_dict())
2020-06-24 10:28:44 +00:00
def addOneRouterPerLink(self, link):
2020-06-29 07:51:55 +00:00
return self.add_host(Topo.ROUTER_NAME_PREFIX +
2020-06-24 10:28:44 +00:00
str(link.id))
def __str__(self):
s = "Single if ECMP like env\n"
i = 0
2020-06-29 07:51:55 +00:00
n = len(self.topo_parameter.link_characteristics)
for p in self.topo_parameter.link_characteristics:
2020-06-24 10:28:44 +00:00
if i == n // 2:
if n % 2 == 0:
s = s + "c---sw sw-----s\n"
s = s + " |-----R-----|\n"
else:
s = s + "c---sw----R-----sw-----s\n"
else:
s = s + " |-----R-----|\n"
i = i + 1
return s
class ECMPSingleInterfaceConfig(TopoConfig):
NAME = "ECMPLike"
2020-06-23 11:20:07 +00:00
def __init__(self, topo, param):
2020-06-24 10:28:44 +00:00
super(ECMPSingleInterfaceConfig, self).__init__(topo, param)
2020-06-23 11:20:07 +00:00
2020-06-29 07:51:55 +00:00
def configure_routing(self):
2020-06-23 11:20:07 +00:00
i = 0
mask = len(self.topo.routers) - 1
for l in self.topo.routers:
cmd = self.getIptableRuleICMP(mask, i)
self.topo.command_to(self.client, cmd)
self.topo.command_to(self.server, cmd)
2020-06-23 11:20:07 +00:00
cmd = self.getIptableRuleTCPPortClient(mask, i)
self.topo.command_to(self.client, cmd)
2020-06-23 11:20:07 +00:00
cmd = self.getIptableRuleTCPPortServer(mask, i)
self.topo.command_to(self.server, cmd)
2020-06-23 11:20:07 +00:00
cmd = self.getIpRuleCmd(i)
self.topo.command_to(self.client, cmd)
self.topo.command_to(self.server, cmd)
2020-06-23 11:20:07 +00:00
cmd = self.getDefaultRouteCmd(self.getRouterIPClient(i),
i)
self.topo.command_to(self.client, cmd)
2020-06-23 11:20:07 +00:00
cmd = self.getDefaultRouteCmd(self.getRouterIPServer(i),
i)
self.topo.command_to(self.server, cmd)
2020-06-23 11:20:07 +00:00
i = i + 1
###
2020-06-29 07:51:55 +00:00
cmd = self.add_simple_default_route_command(self.getRouterIPServer(0))
self.topo.command_to(self.server, cmd)
2020-06-23 11:20:07 +00:00
2020-06-29 07:51:55 +00:00
cmd = self.add_simple_default_route_command(self.getRouterIPClient(0))
self.topo.command_to(self.client, cmd)
2020-06-23 11:20:07 +00:00
self.topo.command_to(self.client, "ip route flush cache")
self.topo.command_to(self.server, "ip route flush cache")
2020-06-23 11:20:07 +00:00
def getIptableRuleICMP(self, mask, id):
s = 'iptables -t mangle -A OUTPUT -m u32 --u32 ' + \
'"6&0xFF=0x1 && ' + \
'24&0x' + \
pack('>I',(mask)).encode('hex') + \
'=0x' + pack('>I',id).encode('hex') + \
'" -j MARK --set-mark ' + str(id + 1)
print (s)
return s
def getIptableRuleTCPPortClient(self, mask, id):
s = 'iptables -t mangle -A OUTPUT -m u32 --u32 ' + \
'"6&0xFF=0x6 && ' + \
'18&0x' + \
pack('>I',(mask)).encode('hex') + \
'=0x' + pack('>I',id).encode('hex') + \
'" -j MARK --set-mark ' + str(id + 1)
print (s)
return s
def getIptableRuleTCPPortServer(self, mask, id):
s = 'iptables -t mangle -A OUTPUT -m u32 --u32 ' + \
'"6&0xFF=0x6 && ' + \
'20&0x' + \
pack('>I',(mask)).encode('hex') + \
'=0x' + pack('>I',id).encode('hex') + \
'" -j MARK --set-mark ' + str(id + 1)
print (s)
return s
def getIpRuleCmd(self, id):
s = 'ip rule add fwmark ' + str(id + 1) + ' table ' + \
str(id + 1)
print(s)
return s
def getDefaultRouteCmd(self, via, id):
s = 'ip route add default via ' + via + ' table ' + str(id + 1)
print(s)
return s
2020-06-29 07:51:55 +00:00
def configure_interfaces(self):
self.client = self.topo.get_host(Topo.CLIENT_NAME)
self.server = self.topo.get_host(Topo.SERVER_NAME)
2020-06-23 11:20:07 +00:00
self.routers = []
i = 0
netmask = "255.255.255.0"
for l in self.topo.routers:
self.routers.append(self.topo.get_host(
2020-06-29 07:51:55 +00:00
Topo.ROUTER_NAME_PREFIX + str(i)))
cmd = self.interface_up_command(
2020-06-23 11:20:07 +00:00
self.getRouterInterfaceLSwitch(i),
self.getRouterIPClient(i), netmask)
self.topo.command_to(self.routers[-1] , cmd)
2020-06-23 11:20:07 +00:00
2020-06-29 07:51:55 +00:00
cmd = self.interface_up_command(
2020-06-23 11:20:07 +00:00
self.getRouterInterfaceRSwitch(i),
self.getRouterIPServer(i), netmask)
self.topo.command_to(self.routers[-1] , cmd)
2020-06-23 11:20:07 +00:00
i = i + 1
2020-06-29 07:51:55 +00:00
cmd = self.interface_up_command(self.get_client_interface(0),
2020-06-23 11:20:07 +00:00
self.getClientIP(0), netmask)
self.topo.command_to(self.client, cmd)
2020-06-23 11:20:07 +00:00
2020-06-29 07:51:55 +00:00
cmd = self.interface_up_command(self.get_server_interface(),
2020-06-23 11:20:07 +00:00
self.getServerIP(), netmask)
self.topo.command_to(self.server, cmd)
2020-06-23 11:20:07 +00:00
def getClientIP(self, interfaceID):
2020-06-29 07:51:55 +00:00
lSubnet = self.param.get(TopoParameter.LEFT_SUBNET)
2020-06-23 11:20:07 +00:00
clientIP = lSubnet + str(interfaceID) + ".1"
return clientIP
def getClientSubnet(self, interfaceID):
2020-06-29 07:51:55 +00:00
lSubnet = self.param.get(TopoParameter.LEFT_SUBNET)
2020-06-23 11:20:07 +00:00
clientSubnet = lSubnet + str(interfaceID) + ".0/24"
return clientSubnet
def getRouterIPClient(self, id):
2020-06-29 07:51:55 +00:00
lSubnet = self.param.get(TopoParameter.LEFT_SUBNET)
2020-06-23 11:20:07 +00:00
routerIP = lSubnet + "0." + str(id + 2)
return routerIP
def getRouterIPServer(self, id):
2020-06-29 07:51:55 +00:00
rSubnet = self.param.get(TopoParameter.RIGHT_SUBNET)
2020-06-23 11:20:07 +00:00
routerIP = rSubnet + "0." + str(id + 2)
return routerIP
def getServerIP(self):
2020-06-29 07:51:55 +00:00
rSubnet = self.param.get(TopoParameter.RIGHT_SUBNET)
2020-06-23 11:20:07 +00:00
serverIP = rSubnet + "0.1"
return serverIP
2020-06-26 09:17:00 +00:00
def client_interface_count(self):
2020-06-23 11:20:07 +00:00
return 1
def getRouterInterfaceLSwitch(self, id):
2020-06-29 07:51:55 +00:00
return Topo.ROUTER_NAME_PREFIX + str(id) + "-eth0"
2020-06-23 11:20:07 +00:00
def getRouterInterfaceRSwitch(self, id):
2020-06-29 07:51:55 +00:00
return Topo.ROUTER_NAME_PREFIX + str(id) + "-eth1"
2020-06-23 11:20:07 +00:00
2020-06-26 09:17:00 +00:00
def get_client_interface(self, interfaceID):
2020-06-29 07:51:55 +00:00
return Topo.CLIENT_NAME + "-eth" + str(interfaceID)
2020-06-23 11:20:07 +00:00
2020-06-29 07:51:55 +00:00
def get_server_interface(self):
return Topo.SERVER_NAME + "-eth0"
2020-06-23 11:20:07 +00:00
def getMidLeftName(self, id):
2020-06-29 07:51:55 +00:00
return Topo.ROUTER_NAME_PREFIX + str(id)
2020-06-23 11:20:07 +00:00
def getMidRightName(self, id):
2020-06-29 07:51:55 +00:00
return Topo.SWITCH_NAME_PREFIX + "1"
2020-06-23 11:20:07 +00:00
def getMidL2RInterface(self, id):
return self.getMidLeftName(id) + "-eth1"
def getMidR2LInterface(self, id):
return self.getMidRightName(id) + "-eth" + str(id+2)