2020-06-24 08:36:26 +00:00
|
|
|
from core.topo import Topo, TopoConfig, TopoParameter
|
2015-01-12 11:26:54 +00:00
|
|
|
from struct import *
|
|
|
|
|
2020-06-24 10:28:44 +00:00
|
|
|
class ECMPSingleInterfaceTopo(Topo):
|
|
|
|
NAME = "ECMPLike"
|
|
|
|
|
|
|
|
def __init__(self, topoBuilder, parameterFile):
|
|
|
|
super(ECMPSingleInterfaceTopo, self).__init__(topoBuilder, parameterFile)
|
|
|
|
|
|
|
|
print("Hello ECMP topo")
|
|
|
|
|
|
|
|
self.client = self.addHost(Topo.clientName)
|
|
|
|
self.server = self.addHost(Topo.serverName)
|
|
|
|
self.lswitch = self.addSwitch(Topo.switchNamePrefix + "0")
|
|
|
|
self.rswitch = self.addSwitch(Topo.switchNamePrefix + "1")
|
|
|
|
|
|
|
|
self.addLink( self.client, self.lswitch)
|
|
|
|
self.addLink( self.server, self.rswitch)
|
|
|
|
|
|
|
|
self.routers = []
|
|
|
|
for l in self.topoParam.linkCharacteristics:
|
|
|
|
self.routers.append(self.addOneRouterPerLink(l))
|
|
|
|
print("added : " + self.routers[-1])
|
|
|
|
self.addLink(self.lswitch, self.routers[-1])
|
|
|
|
self.addLink(self.rswitch, self.routers[-1], **l.asDict())
|
|
|
|
|
|
|
|
def addOneRouterPerLink(self, link):
|
|
|
|
return self.addHost(Topo.routerNamePrefix +
|
|
|
|
str(link.id))
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
s = "Single if ECMP like env\n"
|
|
|
|
i = 0
|
|
|
|
n = len(self.topoParam.linkCharacteristics)
|
|
|
|
for p in self.topoParam.linkCharacteristics:
|
|
|
|
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
|
|
|
|
|
|
|
def configureRoute(self):
|
|
|
|
i = 0
|
|
|
|
mask = len(self.topo.routers) - 1
|
|
|
|
for l in self.topo.routers:
|
|
|
|
cmd = self.getIptableRuleICMP(mask, i)
|
2020-06-25 08:53:56 +00:00
|
|
|
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)
|
2020-06-25 08:53:56 +00:00
|
|
|
self.topo.command_to(self.client, cmd)
|
2020-06-23 11:20:07 +00:00
|
|
|
cmd = self.getIptableRuleTCPPortServer(mask, i)
|
2020-06-25 08:53:56 +00:00
|
|
|
self.topo.command_to(self.server, cmd)
|
2020-06-23 11:20:07 +00:00
|
|
|
|
|
|
|
cmd = self.getIpRuleCmd(i)
|
2020-06-25 08:53:56 +00:00
|
|
|
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)
|
2020-06-25 08:53:56 +00:00
|
|
|
self.topo.command_to(self.client, cmd)
|
2020-06-23 11:20:07 +00:00
|
|
|
cmd = self.getDefaultRouteCmd(self.getRouterIPServer(i),
|
|
|
|
i)
|
2020-06-25 08:53:56 +00:00
|
|
|
self.topo.command_to(self.server, cmd)
|
2020-06-23 11:20:07 +00:00
|
|
|
|
|
|
|
i = i + 1
|
|
|
|
|
|
|
|
###
|
|
|
|
cmd = self.addRouteDefaultSimple(self.getRouterIPServer(0))
|
2020-06-25 08:53:56 +00:00
|
|
|
self.topo.command_to(self.server, cmd)
|
2020-06-23 11:20:07 +00:00
|
|
|
|
|
|
|
cmd = self.addRouteDefaultSimple(self.getRouterIPClient(0))
|
2020-06-25 08:53:56 +00:00
|
|
|
self.topo.command_to(self.client, cmd)
|
2020-06-23 11:20:07 +00:00
|
|
|
|
2020-06-25 08:53:56 +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
|
|
|
|
|
|
|
|
def configureInterfaces(self):
|
2020-06-25 08:53:56 +00:00
|
|
|
self.client = self.topo.get_host(Topo.clientName)
|
|
|
|
self.server = self.topo.get_host(Topo.serverName)
|
2020-06-23 11:20:07 +00:00
|
|
|
self.routers = []
|
|
|
|
i = 0
|
|
|
|
netmask = "255.255.255.0"
|
|
|
|
for l in self.topo.routers:
|
2020-06-25 08:53:56 +00:00
|
|
|
self.routers.append(self.topo.get_host(
|
2020-06-24 08:36:26 +00:00
|
|
|
Topo.routerNamePrefix + str(i)))
|
2020-06-23 11:20:07 +00:00
|
|
|
cmd = self.interfaceUpCommand(
|
|
|
|
self.getRouterInterfaceLSwitch(i),
|
|
|
|
self.getRouterIPClient(i), netmask)
|
2020-06-25 08:53:56 +00:00
|
|
|
self.topo.command_to(self.routers[-1] , cmd)
|
2020-06-23 11:20:07 +00:00
|
|
|
|
|
|
|
cmd = self.interfaceUpCommand(
|
|
|
|
self.getRouterInterfaceRSwitch(i),
|
|
|
|
self.getRouterIPServer(i), netmask)
|
2020-06-25 08:53:56 +00:00
|
|
|
self.topo.command_to(self.routers[-1] , cmd)
|
2020-06-23 11:20:07 +00:00
|
|
|
|
|
|
|
i = i + 1
|
|
|
|
|
|
|
|
cmd = self.interfaceUpCommand(self.getClientInterface(0),
|
|
|
|
self.getClientIP(0), netmask)
|
2020-06-25 08:53:56 +00:00
|
|
|
self.topo.command_to(self.client, cmd)
|
2020-06-23 11:20:07 +00:00
|
|
|
|
|
|
|
cmd = self.interfaceUpCommand(self.getServerInterface(),
|
|
|
|
self.getServerIP(), netmask)
|
2020-06-25 08:53:56 +00:00
|
|
|
self.topo.command_to(self.server, cmd)
|
2020-06-23 11:20:07 +00:00
|
|
|
|
|
|
|
def getClientIP(self, interfaceID):
|
2020-06-25 08:53:56 +00:00
|
|
|
lSubnet = self.param.get(TopoParameter.LSUBNET)
|
2020-06-23 11:20:07 +00:00
|
|
|
clientIP = lSubnet + str(interfaceID) + ".1"
|
|
|
|
return clientIP
|
|
|
|
|
|
|
|
def getClientSubnet(self, interfaceID):
|
2020-06-25 08:53:56 +00:00
|
|
|
lSubnet = self.param.get(TopoParameter.LSUBNET)
|
2020-06-23 11:20:07 +00:00
|
|
|
clientSubnet = lSubnet + str(interfaceID) + ".0/24"
|
|
|
|
return clientSubnet
|
|
|
|
|
|
|
|
def getRouterIPClient(self, id):
|
2020-06-25 08:53:56 +00:00
|
|
|
lSubnet = self.param.get(TopoParameter.LSUBNET)
|
2020-06-23 11:20:07 +00:00
|
|
|
routerIP = lSubnet + "0." + str(id + 2)
|
|
|
|
return routerIP
|
|
|
|
|
|
|
|
def getRouterIPServer(self, id):
|
2020-06-25 08:53:56 +00:00
|
|
|
rSubnet = self.param.get(TopoParameter.RSUBNET)
|
2020-06-23 11:20:07 +00:00
|
|
|
routerIP = rSubnet + "0." + str(id + 2)
|
|
|
|
return routerIP
|
|
|
|
|
|
|
|
def getServerIP(self):
|
2020-06-25 08:53:56 +00:00
|
|
|
rSubnet = self.param.get(TopoParameter.RSUBNET)
|
2020-06-23 11:20:07 +00:00
|
|
|
serverIP = rSubnet + "0.1"
|
|
|
|
return serverIP
|
|
|
|
|
|
|
|
def getClientInterfaceCount(self):
|
|
|
|
return 1
|
|
|
|
|
|
|
|
def getRouterInterfaceLSwitch(self, id):
|
2020-06-24 08:36:26 +00:00
|
|
|
return Topo.routerNamePrefix + str(id) + "-eth0"
|
2020-06-23 11:20:07 +00:00
|
|
|
|
|
|
|
def getRouterInterfaceRSwitch(self, id):
|
2020-06-24 08:36:26 +00:00
|
|
|
return Topo.routerNamePrefix + str(id) + "-eth1"
|
2020-06-23 11:20:07 +00:00
|
|
|
|
|
|
|
def getClientInterface(self, interfaceID):
|
2020-06-24 08:36:26 +00:00
|
|
|
return Topo.clientName + "-eth" + str(interfaceID)
|
2020-06-23 11:20:07 +00:00
|
|
|
|
|
|
|
def getServerInterface(self):
|
2020-06-24 08:36:26 +00:00
|
|
|
return Topo.serverName + "-eth0"
|
2020-06-23 11:20:07 +00:00
|
|
|
|
|
|
|
def getMidLeftName(self, id):
|
2020-06-24 08:36:26 +00:00
|
|
|
return Topo.routerNamePrefix + str(id)
|
2020-06-23 11:20:07 +00:00
|
|
|
|
|
|
|
def getMidRightName(self, id):
|
2020-06-24 08:36:26 +00:00
|
|
|
return Topo.switchNamePrefix + "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)
|