def load_edges(self, fname): with open(fname) as f: for line in f.readlines(): tokens = line.strip().split() src = tokens[0] dst = tokens[1] if src not in self.switches: self.hosts[src] = Host(ip=src, name=src) if dst not in self.switches: self.hosts[dst] = Host(ip=dst, name=dst) if src in self.switches: if dst not in self.edges[src]: self.edges[src].append(dst)
def _make_topo(self): self.graph = self._generate_graph() switches = [node for node in self.graph.nodes() if node[0] == 's'] hosts = [node for node in self.graph.nodes() if node[0] == 'h'] startip = ip2int("10.1.0.0") for name in switches: ip = startip + len(self.switches) + 1 self.switches[name] = Switch(name=name, ip=int2ip(ip)) count = 0 for name in hosts: podnum, idx = self.pods_rev[name] ip = "10.0.{0}.{1}".format(podnum, idx) mac = int2mac(count) count += 1 self.hosts[name] = Host(name=name, ip=ip, mac=mac) for e0, e1 in self.graph.edges(): if e0 not in self.edges: self.edges[e0] = [] if e1 not in self.edges: self.edges[e1] = [] self.edges[e0].append(e1) self.edges[e1].append(e0)
def read_links(self, path): topofile = os.path.join(path, "as1755.topo") egressfile = os.path.join(path, "as1755.egress") #self.graph = networkx.DiGraph() self.graph = networkx.Graph() with open(topofile) as f: for line in f.readlines(): tokens = line.split() if len(tokens) != 2: raise Exception("Invalid line in topo: {0}".format(line)) # skip duplicates and self loops if not self.graph.has_edge(tokens[0], tokens[1]) and \ tokens[0] != tokens[1]: self.graph.add_edge(tokens[0], tokens[1]) for node in self.graph.nodes(): if node.startswith("10."): if node not in self.switches: self.switches[node] = Switch(name=node, ip=node) else: if node not in self.hosts: self.hosts[node] = Host(name=node, ip=node) for e0, e1 in self.graph.edges(): if e0 not in self.edges: self.edges[e0] = [] if e1 not in self.edges: self.edges[e1] = [] self.edges[e0].append(e1) self.edges[e1].append(e0) with open(egressfile) as f: self._egresses = [e.strip() for e in f.readlines()]
from link import Link, LinkLayer import threading from time import sleep import sys from copy import deepcopy ##configuration parameters router_queue_size = 0 #0 means unlimited simulation_time = 10 #give the network sufficient time to execute transfers if __name__ == '__main__': object_L = [ ] #keeps track of objects, so we can kill their threads at the end #create network hosts host_1 = Host('H1') object_L.append(host_1) host_2 = Host('H2') object_L.append(host_2) #create routers and routing tables for connected clients (subnets) encap_tbl_D = { } # table used to encapsulate network packets into MPLS frames frwd_tbl_D = {} # table used to forward MPLS frames decap_tbl_D = { } # table used to decapsulate network packets from MPLS frames router_a = Router(name='RA', intf_capacity_L=[500, 500], encap_tbl_D=encap_tbl_D, frwd_tbl_D=frwd_tbl_D, decap_tbl_D=decap_tbl_D,
def __init__(self, definition, ntf, ttf, port_ids, router, no_ft=False): super(TfTopology, self).__init__() self.definition = definition if no_ft: for rtr1, intf1, rtr2, intf2 in self.definition: if rtr1 not in self.edges: self.edges[rtr1] = [] if rtr2 not in self.edges: self.edges[rtr2] = [] self.edges[rtr1].append(rtr2) self.edges[rtr2].append(rtr1) self.switches[rtr1] = Switch(name=rtr1) self.switches[rtr2] = Switch(name=rtr2) return self.ntf = ntf() self.ttf = ttf() self.port_map, self.port_reverse_map = port_ids() self.router = router(1) self.edge_ports = set() self.intfs = {} edge_port_ips = {} start_ip = ip2int("10.0.0.1") # keys (ports) should be ints self.port_reverse_map = dict( (int(k), v) for (k, v) in self.port_reverse_map.iteritems()) for rtr1, intf1, rtr2, intf2 in self.definition: if rtr1 not in self.intfs: self.intfs[rtr1] = {} if rtr2 not in self.intfs: self.intfs[rtr2] = {} if rtr1 not in self.edges: self.edges[rtr1] = [] if rtr2 not in self.edges: self.edges[rtr2] = [] self.intfs[rtr1][intf1] = rtr2 self.intfs[rtr2][intf2] = rtr1 self.edges[rtr1].append(rtr2) self.edges[rtr2].append(rtr1) self.switches[rtr1] = Switch(name=rtr1) self.switches[rtr2] = Switch(name=rtr2) host_limit = {} for rtr in self.port_map.keys(): for port in self.port_map[rtr].values(): host_limit[rtr] = int(port) + \ self.router.PORT_TYPE_MULTIPLIER * \ self.router.OUTPUT_PORT_TYPE_CONST break # self.edge_ports.add(int(port) + # self.router.PORT_TYPE_MULTIPLIER * # self.router.OUTPUT_PORT_TYPE_CONST) #for port in sorted(self.edge_ports): for port in sorted(host_limit.values()): self.edge_ports.add(port) ip = int2ip(start_ip + len(edge_port_ips)) edge_port_ips[port] = ip self.hosts[ip] = Host(ip=ip, name=ip) for tf in self.ntf.tf_list: ofg = OpenFlow_Rule_Generator(tf, self.router.HS_FORMAT()) rules = ofg.generate_of_rules() swname = tf.prefix_id for rule in rules: #dest = int2ip(rule['ip_dst_match']) #wc = wc2ip(rule['ip_dst_wc']) outports = rule['out_ports'] location = tf.prefix_id nexthops = [] for port in outports: if port in self.edge_ports: nexthops.append(edge_port_ips[port]) pass else: p = port - self.router.PORT_TYPE_MULTIPLIER if p in self.port_reverse_map.keys(): portname = self.port_reverse_map[p].split("-")[1] if portname in self.intfs[location].keys(): nexthop = self.intfs[location][portname] nexthops.append(nexthop) else: # TODO pass r = trie.Rule() r.ruleType = trie.Rule.FORWARDING r.location = location r.priority = 1 # TODO: handle multipath if len(nexthops) > 0: r.nextHop = nexthops[0] nw_dst_match = rule['ip_dst_match'] nw_dst_wc = ip2int(wc2ip(rule['ip_dst_wc'])) nw_src_match = rule['ip_src_match'] nw_src_wc = ip2int(wc2ip(rule['ip_src_wc'])) r.fieldValue[HeaderField.Index["NW_DST"]] = nw_dst_match r.fieldMask[HeaderField.Index["NW_DST"]] = nw_dst_wc r.fieldValue[HeaderField.Index["NW_SRC"]] = nw_src_match r.fieldMask[HeaderField.Index["NW_SRC"]] = nw_src_wc # filter inports - only add edge inports inports = [ (p + router.PORT_TYPE_MULTIPLIER) for p in rule['in_ports'] if (p + router.PORT_TYPE_MULTIPLIER) in self.edge_ports ] if len(inports) > 0: r.fieldValue[HeaderField.Index["IN_PORT"]] = sorted( inports)[0] r.fieldMask[HeaderField.Index["IN_PORT"]] = 0xFFFF # TODO: handle vlan rewrite? # r.fieldValue[HeaderField.Index["DL_VLAN"]] = rule['vlan_match'] # flow = FlowEntry(dest=dest, # wildcard=wc, # location=location, # nexthops=nexthops) # add host to edges for n in nexthops: if n in self.hosts and n not in self.edges[location]: self.edges[location].append(n) if len(nexthops) > 1: print "Can't handle multiple next hops", nexthops if len(nexthops) > 0: # self.switches[swname].ft.append(flow) self.switches[swname].ft.append(r)