mininet-sample/topos/multi_interface.py

180 lines
6.8 KiB
Python
Raw Normal View History

2020-06-24 08:36:26 +00:00
from core.topo import Topo, TopoConfig, TopoParameter
2020-06-24 10:28:44 +00:00
class MultiInterfaceTopo(Topo):
NAME = "MultiIf"
2020-06-29 07:51:55 +00:00
def __init__(self, topo_builder, parameterFile):
super(MultiInterfaceTopo, self).__init__(topo_builder, parameterFile)
2020-06-24 10:28:44 +00:00
print("Hello from topo multi if")
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.router = self.add_host(Topo.ROUTER_NAME)
2020-06-24 10:28:44 +00:00
self.switchClient = []
self.switchServer = []
2020-06-29 07:51:55 +00:00
for l in self.topo_parameter.link_characteristics:
self.switchClient.append(self.add_switch1ForLink(l))
self.add_link(self.client,self.switchClient[-1])
self.switchServer.append(self.add_switch2ForLink(l))
2020-06-29 10:46:07 +00:00
self.add_bottleneck_link(self.switchClient[-1], self.switchServer[-1], link_characteristics=l)
2020-06-29 07:51:55 +00:00
self.add_link(self.switchServer[-1],self.router)
self.add_link(self.router, self.server)
def add_switch1ForLink(self, link):
return self.add_switch(MultiInterfaceTopo.SWITCH_NAME_PREFIX +
2020-06-24 10:28:44 +00:00
str(2 * link.id))
2020-06-29 07:51:55 +00:00
def add_switch2ForLink(self, link):
return self.add_switch(MultiInterfaceTopo.SWITCH_NAME_PREFIX +
2020-06-24 10:28:44 +00:00
str(2 * link.id + 1))
def __str__(self):
s = "Simple multiple interface topolgy \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 r-----s\n"
s = s + "|--sw----sw--|\n"
else:
s = s + "c--sw----sw--r-----s\n"
else:
s = s + "|--sw----sw--|\n"
i = i + 1
return s
class MultiInterfaceConfig(TopoConfig):
NAME = "MultiIf"
2020-06-23 11:20:07 +00:00
def __init__(self, topo, param):
2020-06-24 10:28:44 +00:00
super(MultiInterfaceConfig, 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
for l in self.topo.switchClient:
2020-06-29 07:51:55 +00:00
cmd = self.add_table_route_command(self.getClientIP(i), i)
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.add_link_scope_route_command(
2020-06-23 11:20:07 +00:00
self.getClientSubnet(i),
2020-06-26 09:17:00 +00:00
self.get_client_interface(i), i)
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.add_table_default_route_command(self.getRouterIPSwitch(i),
2020-06-23 11:20:07 +00:00
i)
self.topo.command_to(self.client, cmd)
2020-06-23 11:20:07 +00:00
i = i + 1
2020-06-29 07:51:55 +00:00
cmd = self.add_global_default_route_command(self.getRouterIPSwitch(0),
2020-06-26 09:17:00 +00:00
self.get_client_interface(0))
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.add_simple_default_route_command(self.getRouterIPServer())
self.topo.command_to(self.server, cmd)
2020-06-23 11:20:07 +00:00
2020-06-29 07:51:55 +00:00
def configure_interfaces(self):
2020-06-23 11:20:07 +00:00
print("Configure interfaces for multi inf")
2020-06-29 07:51:55 +00:00
self.client = self.topo.get_host(Topo.CLIENT_NAME)
self.server = self.topo.get_host(Topo.SERVER_NAME)
self.router = self.topo.get_host(Topo.ROUTER_NAME)
2020-06-23 11:20:07 +00:00
i = 0
netmask = "255.255.255.0"
2020-06-29 07:51:55 +00:00
links = self.topo.get_link_characteristics()
2020-06-23 11:20:07 +00:00
for l in self.topo.switchClient:
2020-06-29 07:51:55 +00:00
cmd = self.interface_up_command(
2020-06-26 09:17:00 +00:00
self.get_client_interface(i),
2020-06-23 11:20:07 +00:00
self.getClientIP(i), netmask)
self.topo.command_to(self.client, cmd)
2020-06-26 09:17:00 +00:00
clientIntfMac = self.client.intf(self.get_client_interface(i)).MAC()
self.topo.command_to(self.router, "arp -s " + self.getClientIP(i) + " " + clientIntfMac)
2020-06-23 11:20:07 +00:00
if(links[i].backup):
2020-06-26 09:17:00 +00:00
cmd = self.interface_backup_command(
self.get_client_interface(i))
self.topo.command_to(self.client, cmd)
2020-06-23 11:20:07 +00:00
i = i + 1
i = 0
for l in self.topo.switchServer:
2020-06-29 07:51:55 +00:00
cmd = self.interface_up_command(
2020-06-26 09:17:00 +00:00
self.get_router_interface_to_switch(i),
2020-06-23 11:20:07 +00:00
self.getRouterIPSwitch(i), netmask)
self.topo.command_to(self.router, cmd)
2020-06-26 09:17:00 +00:00
routerIntfMac = self.router.intf(self.get_router_interface_to_switch(i)).MAC()
self.topo.command_to(self.client, "arp -s " + self.getRouterIPSwitch(i) + " " + routerIntfMac)
2020-06-23 11:20:07 +00:00
print(str(links[i]))
i = i + 1
2020-06-29 07:51:55 +00:00
cmd = self.interface_up_command(self.getRouterInterfaceServer(),
2020-06-23 11:20:07 +00:00
self.getRouterIPServer(), netmask)
self.topo.command_to(self.router, cmd)
2020-06-23 11:20:07 +00:00
routerIntfMac = self.router.intf(self.getRouterInterfaceServer()).MAC()
self.topo.command_to(self.server, "arp -s " + self.getRouterIPServer() + " " + routerIntfMac)
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-29 07:51:55 +00:00
serverIntfMac = self.server.intf(self.get_server_interface()).MAC()
self.topo.command_to(self.router, "arp -s " + self.getServerIP() + " " + serverIntfMac)
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 getRouterIPSwitch(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
routerIP = lSubnet + str(interfaceID) + ".2"
return routerIP
def getRouterIPServer(self):
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.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 len(self.topo.switchClient)
def getRouterInterfaceServer(self):
2020-06-26 09:17:00 +00:00
return self.get_router_interface_to_switch(len(self.topo.switchServer))
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-26 09:17:00 +00:00
def get_router_interface_to_switch(self, interfaceID):
2020-06-29 07:51:55 +00:00
return Topo.ROUTER_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 getSwitchClientName(self, id):
2020-06-29 07:51:55 +00:00
return Topo.SWITCH_NAME_PREFIX + str(2 * id)
2020-06-23 11:20:07 +00:00
def getSwitchServerName(self, id):
2020-06-29 07:51:55 +00:00
return Topo.SWITCH_NAME_PREFIX + str(2 * id + 1)
2020-06-23 11:20:07 +00:00
def getMidLeftName(self, id):
return self.getSwitchClientName(id)
def getMidRightName(self, id):
return self.getSwitchServerName(id)
def getMidL2RIncomingInterface(self, id):
return self.getMidLeftName(id) + "-eth1"
def getMidR2LIncomingInterface(self, id):
return self.getMidRightName(id) + "-eth2"