latest
unknown
python
3 years ago
4.2 kB
6
Indexable
# Online Python compiler (interpreter) to run Python online.
spine_layer_switches = []
leaf_layer_switches = []
hosts = []
spine_layer_ip_subnets = []
leaf_layer_ip_subnets = []
host_ip_subnets = []
# Mapping of spine layer ip against switch name
# Note that 'x' denotes 'mapping of'
spine_layer_ip_subnet_x_switch = {}
leaf_layer_ip_subnet_x_switch = {}
ip_subnet_x_host = {}
host_x_ip = {}
def generate_topology(sl_factor=4, ll_factor=2, hl_factor=3):
"""Multipling factors for spine layer (sl), leaf layer (ll), and host layer (hl)"""
first_ip_subnet = 10
for i in range(0, sl_factor):
layer_1_subnet = str(first_ip_subnet + i)
spine_layer_switches.append("s1_{}".format(i))
spine_layer_ip_subnets.append(layer_1_subnet)
spine_layer_ip_subnet_x_switch[layer_1_subnet] = spine_layer_switches[-1]
# For every spine layer switch, I'm adding 'll_factor' leaf layer switches
for j in range(0, ll_factor):
layer_2_subnet = layer_1_subnet + ".{}".format(j)
leaf_layer_switches.append("s2_{}".format(ll_factor*i + j))
leaf_layer_ip_subnets.append(layer_2_subnet)
leaf_layer_ip_subnet_x_switch[layer_2_subnet] = leaf_layer_switches[-1]
# For every leaf layer switch, I'm adding 'hl_factor' hosts
for k in range(0, hl_factor):
host_layer_subnet = layer_2_subnet + ".{}".format(k)
hosts.append("h{}".format(
hl_factor*ll_factor*i + hl_factor*j + k))
host_ip_subnets.append(host_layer_subnet)
ip_subnet_x_host[host_layer_subnet] = hosts[-1]
host_x_ip[hosts[-1]] = host_layer_subnet + ".0/24"
print(spine_layer_switches)
print(leaf_layer_switches)
print(hosts)
print("\n----------------------------------------\n")
print(spine_layer_ip_subnet_x_switch)
print(leaf_layer_ip_subnet_x_switch)
print(host_ip_subnets)
# print("\n----------------------------------------\n")
# print(host_x_ip)
# print("\n----------------------------------------\n")
# for i in ip_subnet_x_host.items():
# print(i)
# print("\n----------------------------------------\n")
# for i in host_x_ip.items():
# print(i)
# Rules for spine (layer 1) switches
def get_output_port_for_spine_switches(dst_ip):
dst_16_bit_subnet = ".".join(dst_ip.split(".")[0:2])
leaf_switch = leaf_layer_ip_subnet_x_switch[dst_16_bit_subnet]
port_of_leaf_switch = leaf_layer_switches.index(leaf_switch) + 1
print(port_of_leaf_switch)
return port_of_leaf_switch
# Rules for leaf (layer 2) switches, where packets are travelling upwards
def get_output_port_for_leaf_switches_towards_spine(src_ip, dst_ip):
dst_8_bit_subnet = int(".".join(dst_ip.split(".")[0:1]))
src_8_bit_subnet = int(".".join(src_ip.split(".")[0:1]))
# We consider the larger of the src and dst subnets to find the spine switch to forward the packet to
subnet = max(dst_8_bit_subnet, src_8_bit_subnet)
spine_switch = spine_layer_ip_subnet_x_switch[str(subnet)]
port_of_spine_switch = spine_layer_switches.index(spine_switch) + 1
print(port_of_spine_switch)
return port_of_spine_switch
# These will be given higher priority than the above function (where packets are moving upwards towards spine)
def get_output_port_for_leaf_switches_towards_hosts(ll_switch_subnet, dst_ip):
# Based on which leaf layer switch it is, we will make the decision.
# If the destination is in the same subnet of ll_switch, then packet doesnt have to even touch the spine layer, we just send downwards directly.
if ".".join(dst_ip.split(".")[0:2]) == ll_switch_subnet:
# Based on 3rd octet set, we decide which port downwards
print(int(dst_ip.split(".")[2]) + 1)
return int(dst_ip.split(".")[2]) + 1
else:
print("Packet shall travel upwards only!")
return None
generate_topology()
get_output_port_for_spine_switches("13.0.0.0/24")
get_output_port_for_leaf_switches_towards_spine("12.0.0.0/24", '13.1.1.0/24')
get_output_port_for_leaf_switches_towards_hosts("12.0", "12.0.2.0/24")
Editor is loading...