def port_status_handler(self,ev): """Handler used to handle port status Displays the topology and port information in case of Port modfictation event """ msg=ev.msg reason=msg.reason dp=msg.datapath ofpport = msg.desc port_no = msg.desc.port_no ofproto = dp.ofproto if reason == ofproto.OFPPR_ADD: self.logger.info("port added %s", port_no) pass elif reason == ofproto.OFPPR_DELETE: self.logger.info("port deleted %s", port_no) pass elif reason == ofproto.OFPPR_MODIFY: self.logger.info("port modified %s", port_no) else: self.logger.info("Illeagal port state %s %s", port_no, reason) pass sw_list = api.get_all_switch(self) sw_list_body =json.dumps([ switch.to_dict() for switch in sw_list]) print('sw_list_body {}'.format(sw_list_body)) link_list = api.get_all_link(self) link_list_body = json.dumps([ link.to_dict() for link in link_list ]) print('link_list_body {}'.format(link_list_body))
def run(self): cmd = raw_input('input command') if cmd == 'switchs': switches = api.get_all_switch(self.ryu_app) print switches elif cmd == 'links': links = api.get_all_links(self.ryu_app) print links
def get_dp(self, dpid): switches = ryu_api.get_all_switch(self) for switch in switches: if switch.dp.id == dpid: return switch.dp return None
def update_topology(self, app): self._topo_raw_switches = get_all_switch(app) self._topo_raw_links = get_all_link(app) switch_dpids = [switch.dp.id for switch in self._topo_raw_switches] links = [(link.src.dpid,link.dst.dpid) for link in self._topo_raw_links] self.topology.add_nodes_from(switch_dpids) self.topology.add_edges_from(links) ookm_log.debug("Topology updated")
def get_topology_data(self): # Call get_switch() to get the list of objects Switch. self.topo_shape.topo_raw_switches = copy.copy(get_all_switch(self)) # Call get_link() to get the list of objects Link. self.topo_shape.topo_raw_links = copy.copy(get_all_link(self)) self.topo_shape.print_links("get_topology_data") self.topo_shape.print_switches("get_topology_data")
def get_all_datapaths(self): switches = topo_api.get_all_switch(self) for switch in switches: dp = switch.dp dpid = dp.id self.dps[dpid] = dp return self.dps.values()
def _update_topology(self): switch_list = get_all_switch(self) if len(switch_list) != 0: self.dpids_port_to_mac = self._get_dpids_port_to_mac(switch_list) self.dpids = self._get_dpids(switch_list) #[dpid,dpid,dpid,...] link_dict = get_all_link(self) if len(link_dict) != 0: self.links_dpid_to_port = self._get_links_dpid_to_port(link_dict) self.links = self._get_links(self.links_dpid_to_port) #[(src.dpid,dst.dpid),(src.dpid,dst.dpid),...] if self.dpids and self.links: self.adjacency_matrix = self._get_adjacency_matrix(self.dpids, self.links)
def refresh_datapaths_view(self): while True : hub.sleep(setting.DATAPATH_REFRESH_PERIOD) switches = get_all_switch(self) #LOG.info('switch num is %d'%len(switches)) for sw in switches : dp = sw.dp if dp.id not in self.datapaths : self.datapaths[dp.id] = dp ports = [p.port_no for p in sw.ports] self.ports_in_dp[dp.id] = set(ports)
def install_group_to_all_dp(self, gid): switches = ryu_api.get_all_switch(self) if not gid: return for switch in switches: dp = switch.dp ofproto = dp.ofproto of_parser = dp.ofproto_parser gmod = of_parser.OFPGroupMod(dp, ofproto.OFPGC_ADD, ofproto.OFPGT_FF, gid, []) dp.send_msg(gmod)
def get_all_edge_port(self): edge_ports = set() not_edge_port = set() for link in topo_api.get_all_link(self): not_edge_port.add(link.src) not_edge_port.add(link.dst) for switch in topo_api.get_all_switch(self) for port in switch.ports: if port not in not_edge_port: edge_ports.add(port)
def port_request_loop(self): time.sleep(5) while True: switches = topo_api.get_all_switch(self) dps = [switch.dp for switch in switches] for dp in dps: parser = dp.ofproto_parser ofproto = dp.ofproto msg = parser.OFPPortStatsRequest(dp, 0, ofproto.OFPP_ANY) dp.send_msg(msg) time.sleep(1)
def state_change_handler(self, ev): dp=ev.datapath assert dp is not None LOG.debug(dp) sw_list = api.get_all_switch(self) sw_list_body =json.dumps([ switch.to_dict() for switch in sw_list]) print('sw_list_body {}'.format(sw_list_body)) link_list = api.get_all_link(self) link_list_body =json.dumps([link.to_dict() for link in link_list]) print ('link_list_body {}' .format(link_list_body)) Switches.state_change_handler
def timer_job(self): timer = self.CONF.send_stats.timer while True: time.sleep(timer) if self.servers.__len__() != 0: switches = api.get_all_switch(self) # Create stats request self.datetime = datetime.datetime.now() for switch in switches: dpid = switch.dp.id for table_id in self.CONF.send_stats.table_id: LOG.debug('Création requète table_id n° ' + table_id + ' pour ' + str(dpid)) parser = switch.dp.ofproto_parser request = parser.OFPFlowStatsRequest(switch.dp, 0, int(table_id)) switch.dp.send_msg(request)
def __init__(self, *args, **kwargs): super(LoadBalanceApp, self).__init__(*args, **kwargs) self.load_balancer = kwargs["load_balancer"] switches = api.get_all_switch(self) # change to slave first for switch in switches: dp = switch.dp ofp = dp.ofproto ofp_parser = dp.ofproto_parser role = ofp.OFPCR_ROLE_SLAVE # generate new generation id gen_id = random.randint(0, 10000) msg = ofp_parser.OFPRoleRequest(dp, role, gen_id) dp.send_msg(msg) self.load_balancer.start_serve()
def port_status_handler(self,ev): msg=ev.msg reason=msg.reason dp=msg.datapath ofpport = msg.desc Switches.port_status_handler print('yes') #sw_list = get_switch(self, 1) sw_list = api.get_all_switch(self) sw_list_body =json.dumps([ switch.to_dict() for switch in sw_list]) print('sw_list_body {}'.format(sw_list_body)) link_list = api.get_all_link(self) link_list_body = json.dumps([ link.to_dict() for link in link_list ]) print('link_list_body {}'.format(link_list_body))
def get_nx_graph(self): graph = nx.DiGraph() switches = topo_api.get_all_switch(self) links = topo_api.get_all_link(self) for switch in switches: dpid = switch.dp.id graph.add_node(dpid) for link in links: src_dpid = link.src.dpid dst_dpid = link.dst.dpid src_port = link.src.port_no dst_port = link.dst.port_no graph.add_edge(src_dpid, dst_dpid, src_port=src_port, dst_port=dst_port) return graph
def switch_enter_handler(self, ev): """Automatically create global view""" sw_list = get_all_switch(self.topology_api_app) link_list = get_all_link(self.topology_api_app) # create dpid to datapath mapping # for sw in sw_list: # if sw.dp.id not in self.dpset: # print 'Enter sw %d' % sw.dp.id # self.dpset[sw.dp.id] = sw for link in link_list: if link.src.dpid not in self.links: self.links[link.src.dpid] = {} self.links[link.src.dpid][str(link.dst.dpid)] = link.src.port_no if link.dst.dpid not in self.links: self.links[link.dst.dpid] = {} self.links[link.dst.dpid][str(link.src.dpid)] = link.dst.port_no print self.links
def get_topology_data(self, ev): print "get_topology_data() is called" global myswitches, adjacency, datapath_list switch_list = get_all_switch(self.topology_api_app)#, None) #print colored('get_switch','green') #print (switch_list) myswitches = [switch.dp.id for switch in switch_list] for switch in switch_list: datapath_list[switch.dp.id] = switch.dp # print "datapath_list=", datapath_list print "myswitches=", myswitches links_list = get_all_link(self.topology_api_app)#, None) #print colored('get_link','green') #print (links_list) # print "links_list=", links_list mylinks = [(link.src.dpid, link.dst.dpid, link.src.port_no, link.dst.port_no) for link in links_list] for s1, s2, port1, port2 in mylinks: # print "type(s1)=", type(s1), " type(port1)=", type(port1) adjacency[s1][s2] = port1 adjacency[s2][s1] = port2 print s1, ":", port1, "<--->", s2, ":", port2
def get_all_links(self): ''' link form format: [(a.1, b.1), (b.2, c.1), (b.3, d.1)] x.k means dpid x and port no k a.1 means first port in switch "a" this topology should looks like: a.1 - 1.b.2 - 1.c 3 | 1 d ''' all_links = ryu_api.get_all_link(self) result = [] for link in all_links: src = '{}.{}'.format(link.src.dpid, link.src.port_no) dst = '{}.{}'.format(link.dst.dpid, link.dst.port_no) result.append((src, dst)) # internal switch links all_switches = ryu_api.get_all_switch(self) link_to_add = [] # O(n^3), such dirty!! for switch in all_switches: ports = switch.ports for port in ports: for _port in ports: if port != _port: src = '{}.{}'.format(port.dpid, port.port_no) dst = '{}.{}'.format(_port.dpid, _port.port_no) link_to_add.append((src, dst)) result.extend(link_to_add) return result
def create_spanning_tree(self): """Builds and display topology graph and that run minimum spanning tree algorithm important fields: self.switch_list: Nodes are extracted and stored in dictionary from the LLDP based topology self.links: links are extracted and stored in dictionary from the LLDP based topology self.add_switch: function is called to build topology graph and add nodes self.add_link: function is called to add link to the topology self.mst : minimum spanning tree is created and path is stored in the dictionary""" self.switch_list = api.get_all_switch(self) self.links = api.get_all_link(self) self.sw_list_body =json.dumps([ switch.to_dict() for switch in self.switch_list]) self.add_switch() self.add_link() nx.draw(self.graph) plt.show() self.mst = nx.minimum_spanning_tree(self.graph) print self.mst print(self.mst.nodes()) print(self.mst.edges()) nx.draw(self.mst) plt.show() x = nx.connected_components(self.mst) print x # shortest_path1=nx.shortest_path(self.mst,2) # print shortest_path1 # shortest_path2=nx.shortest_path(self.mst,1,3) # print shortest_path2 # shortest_path3 = nx.shortest_path(self.mst,1,2) # print shortest_path3 return self.mst
def get_topology_data(self, ev): switch_list = get_all_switch(self) self.qos.add_switches(switch_list) links_list = get_all_link(self) self.qos.add_links(links_list)
def list_switches(self): switches = topo_api.get_all_switch(self) return [switch.to_dict() for switch in switches]
def _packet_in_handler(self, ev): # If you hit this you might want to increase # the "miss_send_length" of your switch if ev.msg.msg_len < ev.msg.total_len: self.logger.debug("packet truncated: only %s of %s bytes", ev.msg.msg_len, ev.msg.total_len) msg = ev.msg datapath = msg.datapath ofproto = datapath.ofproto parser = datapath.ofproto_parser in_port = msg.match['in_port'] pkt = packet.Packet(msg.data) eth = pkt.get_protocols(ethernet.ethernet)[0] if eth.ethertype == ether_types.ETH_TYPE_LLDP: # ignore lldp packet return pr,start = self.enableProf() # After the first packet in start allowing FW to monitor link delays #if self.first_packet == 1: # self.threads.append(hub.spawn_after(5, self.repromptFW)) # self.first_packet = 0 # Figure out environment links = api.get_all_link(self) switches = api.get_all_switch(self) hosts = api.get_all_host(self) if eth.ethertype == ether_types.ETH_TYPE_ARP: arp_pkt = pkt.get_protocols(arp.arp)[0] if arp_pkt.opcode == arp.ARP_REQUEST: # Send to ARP proxy. Cannot perform NIx routing until both hosts # are known by the controller self.ArpProxy (msg.data, datapath, in_port, links, switches, hosts) elif arp_pkt.opcode == arp.ARP_REPLY: self.ArpReply (msg.data, datapath, arp_pkt.dst_ip, links, switches, hosts) self.disableProf(pr,start,"ARP") return #self.logger.info("%s: packet in %s %s %s %s", time.time(), datapath.id, eth.src, eth.dst, in_port) # Start nix vector code src_ip = '' dst_ip = '' srcNode = None dstNode = None if eth.ethertype == ether_types.ETH_TYPE_ARP: arp_pkt = pkt.get_protocols(arp.arp)[0] src_ip = arp_pkt.src_ip dst_ip = arp_pkt.dst_ip elif eth.ethertype == ether_types.ETH_TYPE_IP: ipv4_pkt = pkt.get_protocols(ipv4.ipv4)[0] src_ip = ipv4_pkt.src dst_ip = ipv4_pkt.dst for host in hosts: if src_ip == host.ipv4[0]: srcNode = host if dst_ip == host.ipv4[0]: dstNode = host if srcNode is None or dstNode is None: self.ArpProxy(msg.data, datapath, in_port, links, switches, hosts) self.disableProf(pr, start, "UNKDST") return srcSwitch = [switch for switch in switches if switch.dp.id == srcNode.port.dpid][0] dstSwitch = [switch for switch in switches if switch.dp.id == dstNode.port.dpid][0] next_array = self.FloydWarshall(switches, links) # Send reverse path first to limit requests to controller sdnNix = [] self.BuildNixVector (dstSwitch, srcSwitch, switches, links, next_array, sdnNix) sdnNix.append((srcSwitch, srcNode.port.port_no)) self.sendNixPacket(ofproto, parser, dstSwitch, reversed(sdnNix), msg, dst_ip, src_ip, False) sdnNix = [] self.BuildNixVector (srcSwitch, dstSwitch, switches, links, next_array, sdnNix) sdnNix.append((dstSwitch, dstNode.port.port_no)) self.sendNixPacket(ofproto, parser, srcSwitch, reversed(sdnNix), msg, src_ip, dst_ip) self.disableProf(pr,start,"COMPLETION")
def create_topology_vertices(self): self.switch_list = api.get_all_switch(self) self.links = api.get_all_link(self) self.create_switch() self.create_switch_to_switch_links()
def _packet_in_handler(self, ev): # If you hit this you might want to increase # the "miss_send_length" of your switch if ev.msg.msg_len < ev.msg.total_len: self.logger.debug("packet truncated: only %s of %s bytes", ev.msg.msg_len, ev.msg.total_len) msg = ev.msg datapath = msg.datapath ofproto = datapath.ofproto parser = datapath.ofproto_parser in_port = msg.match['in_port'] pkt = packet.Packet(msg.data) eth = pkt.get_protocols(ethernet.ethernet)[0] if eth.ethertype == ether_types.ETH_TYPE_LLDP: # ignore lldp packet return dst = eth.dst src = eth.src dpid = datapath.id # Figure out environment links = api.get_all_link(self) switches = api.get_all_switch(self) hosts = api.get_all_host(self) arp_pkt = None if eth.ethertype == ether_types.ETH_TYPE_ARP: arp_pkt = pkt.get_protocols(arp.arp)[0] if arp_pkt.opcode == arp.ARP_REQUEST: # Send to ARP proxy. Cannot perform NIx routing until both hosts # are known by the controller self.ArpProxy (msg.data, datapath, in_port, links, switches, hosts) return elif arp_pkt.opcode == arp.ARP_REPLY: self.ArpReply (msg.data, datapath, arp_pkt.dst_ip, links, switches, hosts) return self.logger.info("%s: packet in %s %s %s %s %s", time.time(), dpid, src, dst, in_port, eth.ethertype) # Start nix vector code numNodes = len(switches) + len(hosts) src_ip = '' dst_ip = '' srcNode = '' dstNode = '' if eth.ethertype == ether_types.ETH_TYPE_ARP: arp_pkt = pkt.get_protocols(arp.arp)[0] src_ip = arp_pkt.src_ip dst_ip = arp_pkt.dst_ip elif eth.ethertype == ether_types.ETH_TYPE_IP: ipv4_pkt = pkt.get_protocols(ipv4.ipv4)[0] src_ip = ipv4_pkt.src dst_ip = ipv4_pkt.dst for host in hosts: if src_ip == host.ipv4[0]: srcNode = host if dst_ip == host.ipv4[0]: dstNode = host srcSwitch = [switch for switch in switches if switch.dp.id == srcNode.port.dpid][0] dstSwitch = [switch for switch in switches if switch.dp.id == dstNode.port.dpid][0] parentVec = {} foundIt = self.BFS (numNodes, srcSwitch, dstSwitch, links, switches, hosts, parentVec) sdnNix = [] nixVector = [] if foundIt: self.BuildNixVector (parentVec, srcSwitch, dstSwitch, links, switches, hosts, nixVector, sdnNix) sdnNix.insert(0, (dstSwitch, dstNode.port.port_no)) self.sendNixPacket (ofproto, parser, srcSwitch, sdnNix, msg) self.modLastHop (ofproto, parser, dstSwitch, dstNode.port.port_no, msg)
def _topology_thread(self): while True: all_switch = get_all_switch(self) Set_all_switch(all_switch) all_link = get_all_link(self) Set_all_link(all_link) all_host = get_all_host(self) Set_all_host(all_host) self.link_bw = [[0 for row in range(len(Get_all_switch()) + 1)] for col in range(len(Get_all_switch()) + 1)] print 'All Switch = ' + str(len(Get_all_switch())) print 'All Link = ' + str(len(Get_all_link())) print 'All Host = ' + str(len(Get_all_host())) # for a in Get_all_host(): # print a.ipv4 hub.sleep(1) if len(Get_all_switch()) == 6: for i in range(0, len(Get_all_switch())): get_TopoNumberTo().append(['switch', all_switch[i]]) for i in range(0, len(Get_all_switch()) + 1): self.switch_port_to_switch.append( [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) for i in range(0, len(Get_all_host())): get_TopoNumberTo().append(['host', all_host[i]]) # print 'get_TopoNumberTo = ' + str(get_TopoNumberTo()) connectMatrix = [] for i in range(0, len(get_TopoNumberTo())): connectMatrix.append([999999999] * len(get_TopoNumberTo())) for i in range(0, len(connectMatrix)): for j in range(0, len(connectMatrix[i])): if i == j: connectMatrix[i][j] = 0 # Find each link's src & dst # Controller get all switch_port_to_switch for link in Get_all_link(): print 'link = ' + str(link) indexA = 0 indexB = 0 for i in get_TopoNumberTo(): if i[0] == 'switch' and i[1].dp.id == link.src.dpid: indexA = get_TopoNumberTo().index(i) if i[0] == 'switch' and i[1].dp.id == link.dst.dpid: indexB = get_TopoNumberTo().index(i) if link.src.port_no != 4294967294: self.switch_port_to_switch[link.src.dpid][ link.src.port_no] = link.dst.dpid self.switch_port_to_switch[link.dst.dpid][ link.dst.port_no] = link.src.dpid # connectMatrix[src][dst] = 1 represent A to B is connected connectMatrix[indexA][indexB] = 1 connectMatrix[indexB][indexA] = 1 # print 'Switch Port to Switch = \n' + \ # str(np.matrix(self.switch_port_to_switch)) # Controller get (host,switch) connection for host in Get_all_host(): indexA = 0 indexB = 0 for i in get_TopoNumberTo(): if i[0] == 'host' and i[1] == host: # src indexA = get_TopoNumberTo().index(i) # check whether there is a link exists between host and switch if i[0] == 'switch' and i[1].dp.id == host.port.dpid: # dst indexB = get_TopoNumberTo().index(i) connectMatrix[indexA][indexB] = 1 connectMatrix[indexB][indexA] = 1 print 'connectMatrix = ' print np.matrix(connectMatrix) # forwarding matrix to forwarding Table forwardingMatrix, distance = MakeForwardingTable(connectMatrix) print 'forwardingMatrix :\n' + str(np.matrix(forwardingMatrix)) print 'distance : \n' + str(distance) for i in range(0, len(get_TopoNumberTo())): if get_TopoNumberTo()[i][0] == 'switch': switchdp = get_TopoNumberTo()[i][1].dp.id # Create Switch's forwardingTable : {'dpid':{}} get_forwardingTable()[switchdp] = {} for j in range(0, len(forwardingMatrix[i])): if get_TopoNumberTo()[j][0] == 'host': dsthost = get_TopoNumberTo()[j][1].mac Pport = -1 if get_TopoNumberTo()[forwardingMatrix[i] [j]][0] == 'switch': for link in Get_all_link(): if link.src.dpid == switchdp and link.dst.dpid == get_TopoNumberTo( )[forwardingMatrix[i][j]][1].dp.id: Pport = link.src.port_no if get_TopoNumberTo()[forwardingMatrix[i] [j]][0] == 'host': Pport = get_TopoNumberTo( )[j][1].port.port_no if Pport == -1: print 'host not found' return else: get_forwardingTable( )[switchdp][dsthost] = Pport print 'get_forwardingTable' print get_forwardingTable() for i in range(0, len(distance)): if get_TopoNumberTo()[i][0] == 'host': get_distanceTable()[get_TopoNumberTo()[i][1].mac] = {} for j in range(0, len(distance[i])): if get_TopoNumberTo()[j][0] == 'host': get_distanceTable()[get_TopoNumberTo( )[i][1].mac][get_TopoNumberTo()[j] [1].mac] = distance[i][j] print get_distanceTable() # arpMatrix to ArpTable check = [0] * len(get_TopoNumberTo()) check[0] = 1 arpMatrix = [] for i in range(0, len(get_TopoNumberTo())): arpMatrix.append([0] * len(get_TopoNumberTo())) SPTqueue = [] SPTqueue.append(0) while len(SPTqueue) != 0: i = SPTqueue.pop(0) for j in range(0, len(get_TopoNumberTo())): if connectMatrix[i][j] == 1 and check[j] == 0: arpMatrix[i][j] = 1 arpMatrix[j][i] = 1 check[j] = 1 SPTqueue.append(j) print "get_TopoNumberTo() = " for i in get_TopoNumberTo(): print '[' + str(i) + '] = ' + str(get_TopoNumberTo()[0]) print "arpMatrix = " print arpMatrix for i in range(0, len(get_TopoNumberTo())): if get_TopoNumberTo()[i][0] == 'switch': switchdp = get_TopoNumberTo()[i][1].dp.id Get_ArpTable()[switchdp] = [] for j in range(0, len(arpMatrix[i])): if arpMatrix[i][j] == 1: Pport = -1 if get_TopoNumberTo()[j][0] == 'switch': for link in Get_all_link(): if link.src.dpid == switchdp and link.dst.dpid == get_TopoNumberTo( )[j][1].dp.id: Pport = link.src.port_no if get_TopoNumberTo()[j][0] == 'host': Pport = get_TopoNumberTo( )[j][1].port.port_no if Pport == -1: print 'Pport not found' return else: Get_ArpTable()[switchdp].append(Pport) print "ARP Table = " print Get_ArpTable() Set_ready(True) break
def _topology_thread(self): while True: all_switch = get_all_switch(self) Set_all_switch(all_switch) all_link = get_all_link(self) Set_all_link(all_link) all_host = get_all_host(self) Set_all_host(all_host) print 'all_switch = ' print len(Get_all_switch()) print 'all_link = ' print len(Get_all_link()) print 'all_host = ' print len(Get_all_host()) for a in Get_all_host(): print a.ipv4 hub.sleep(1) if len(Get_all_switch()) == 15 and len(Get_all_host()) == 16: c = 0 for i in range(0, len(Get_all_switch())): get_TopoNumberTo().append(['switch', Get_all_switch()[i]]) for i in range(0, len(Get_all_host())): get_TopoNumberTo().append(['host', Get_all_host()[i]]) for x in range(0, len(get_TopoNumberTo())): print(x, ':', str(get_TopoNumberTo()[x])) weight = [] for i in range(0, len(get_TopoNumberTo())): weight.append([999999999] * len(get_TopoNumberTo())) for i in range(0, len(weight)): for j in range(0, len(weight[i])): if i == j: weight[i][j] = 0 for link in Get_all_link(): indexA = 0 indexB = 0 for i in get_TopoNumberTo(): if i[0] == 'switch' and i[1].dp.id == link.src.dpid: indexA = get_TopoNumberTo().index(i) if i[0] == 'switch' and i[1].dp.id == link.dst.dpid: indexB = get_TopoNumberTo().index(i) weight[indexA][indexB] = 1 weight[indexB][indexA] = 1 for host in Get_all_host(): indexA = 0 indexB = 0 for i in get_TopoNumberTo(): if i[0] == 'host' and i[1] == host: indexA = get_TopoNumberTo().index(i) if i[0] == 'switch' and i[1].dp.id == host.port.dpid: indexB = get_TopoNumberTo().index(i) weight[indexA][indexB] = 1 weight[indexB][indexA] = 1 print 'weight = ' print weight #forwarding matrix to forwarding Table forwardingMatrix, distance = MakeForwardingTable(weight) print forwardingMatrix for i in range(0, len(get_TopoNumberTo())): if get_TopoNumberTo()[i][0] == 'switch': switchdp = get_TopoNumberTo()[i][1].dp.id get_forwardingTable()[switchdp] = {} for j in range(0, len(forwardingMatrix[i])): if get_TopoNumberTo()[j][0] == 'host': dsthost = get_TopoNumberTo()[j][1].mac Pport = -1 if get_TopoNumberTo()[forwardingMatrix[i] [j]][0] == 'switch': for link in Get_all_link(): if link.src.dpid == switchdp and link.dst.dpid == get_TopoNumberTo( )[forwardingMatrix[i][j]][1].dp.id: Pport = link.src.port_no if get_TopoNumberTo()[forwardingMatrix[i] [j]][0] == 'host': Pport = get_TopoNumberTo( )[j][1].port.port_no if Pport == -1: print 'host not found' return else: get_forwardingTable( )[switchdp][dsthost] = Pport print 'get_forwardingTable() = ' print get_forwardingTable() for i in range(0, len(distance)): if get_TopoNumberTo()[i][0] == 'host': get_distanceTable()[get_TopoNumberTo()[i][1].mac] = {} for j in range(0, len(distance[i])): if get_TopoNumberTo()[j][0] == 'host': get_distanceTable()[get_TopoNumberTo( )[i][1].mac][get_TopoNumberTo()[j] [1].mac] = distance[i][j] print 'get_distanceTable() = ' print get_distanceTable() Set_ready(True) break
def get_switch(self): ''' :return:switches:dp,port port:dpid,port_no ''' self.switches = api.get_all_switch(self)
def getAllDatapaths(self): """ Returns a list of all switch objects """ switches = list() for i in get_all_switch(self): switches.append(i) return switches
def _topology_thread(self): while True: all_switch = get_all_switch(self) Set_all_switch(all_switch) all_link = get_all_link(self) Set_all_link(all_link) all_host = get_all_host(self) Set_all_host(all_host) print 'all_switch = ' print len(Get_all_switch()) print 'all_link = ' print len(Get_all_link()) print 'all_host = ' print len(Get_all_host()) for a in Get_all_host(): print a.ipv4 hub.sleep(1) if len(Get_all_switch()) == 6 and len(Get_all_host()) == 48: c = 0 for i in range(0, len(Get_all_switch())): get_TopoNumberTo().append(['switch', Get_all_switch()[i]]) for i in range(0, len(Get_all_host())): get_TopoNumberTo().append(['host', Get_all_host()[i]]) for x in get_TopoNumberTo(): print(x, ':', str(x)) weight = [] for i in range(0, len(get_TopoNumberTo())): weight.append([999999999] * len(get_TopoNumberTo())) for i in range(0, len(weight)): for j in range(0, len(weight[i])): if i == j: weight[i][j] = 0 for link in Get_all_link(): indexA = 0 indexB = 0 for i in get_TopoNumberTo(): if i[0] == 'switch' and i[1].dp.id == link.src.dpid: indexA = get_TopoNumberTo().index(i) if i[0] == 'switch' and i[1].dp.id == link.dst.dpid: indexB = get_TopoNumberTo().index(i) weight[indexA][indexB] = 1 weight[indexB][indexA] = 1 for host in Get_all_host(): indexA = 0 indexB = 0 for i in get_TopoNumberTo(): if i[0] == 'host' and i[1] == host: indexA = get_TopoNumberTo().index(i) if i[0] == 'switch' and i[1].dp.id == host.port.dpid: indexB = get_TopoNumberTo().index(i) weight[indexA][indexB] = 1 weight[indexB][indexA] = 1 print weight #forwarding matrix to forwarding Table forwardingMatrix, distance = MakeForwardingTable(weight) print forwardingMatrix for i in range(0, len(get_TopoNumberTo())): if get_TopoNumberTo()[i][0] == 'switch': switchdp = get_TopoNumberTo()[i][1].dp.id get_forwardingTable()[switchdp] = {} for j in range(0, len(forwardingMatrix[i])): if get_TopoNumberTo()[j][0] == 'host': dsthost = get_TopoNumberTo()[j][1].mac Pport = -1 if get_TopoNumberTo()[forwardingMatrix[i] [j]][0] == 'switch': for link in Get_all_link(): if link.src.dpid == switchdp and link.dst.dpid == get_TopoNumberTo( )[forwardingMatrix[i][j]][1].dp.id: Pport = link.src.port_no if get_TopoNumberTo()[forwardingMatrix[i] [j]][0] == 'host': Pport = get_TopoNumberTo( )[j][1].port.port_no if Pport == -1: print 'host not found' return else: get_forwardingTable( )[switchdp][dsthost] = Pport print get_forwardingTable() for i in range(0, len(distance)): if get_TopoNumberTo()[i][0] == 'host': get_distanceTable()[get_TopoNumberTo()[i][1].mac] = {} for j in range(0, len(distance[i])): if get_TopoNumberTo()[j][0] == 'host': get_distanceTable()[get_TopoNumberTo( )[i][1].mac][get_TopoNumberTo()[j] [1].mac] = distance[i][j] print get_distanceTable() #arpMatrix to ArpTable check = [0] * len(get_TopoNumberTo()) check[0] = 1 arpMatrix = [] for i in range(0, len(get_TopoNumberTo())): arpMatrix.append([0] * len(get_TopoNumberTo())) SPTqueue = [] SPTqueue.append(0) while len(SPTqueue) != 0: i = SPTqueue.pop(0) for j in range(0, len(get_TopoNumberTo())): if weight[i][j] == 1 and check[j] == 0: arpMatrix[i][j] = 1 arpMatrix[j][i] = 1 check[j] = 1 SPTqueue.append(j) print "get_TopoNumberTo() = " for i in get_TopoNumberTo(): print 'get_TopoNumberTo()[' + str(i) + '] = ' + str( get_TopoNumberTo()[0]) print "arpMatrix = " print arpMatrix for i in range(0, len(get_TopoNumberTo())): if get_TopoNumberTo()[i][0] == 'switch': switchdp = get_TopoNumberTo()[i][1].dp.id Get_ArpTable()[switchdp] = [] for j in range(0, len(arpMatrix[i])): if arpMatrix[i][j] == 1: Pport = -1 if get_TopoNumberTo()[j][0] == 'switch': for link in Get_all_link(): if link.src.dpid == switchdp and link.dst.dpid == get_TopoNumberTo( )[j][1].dp.id: Pport = link.src.port_no if get_TopoNumberTo()[j][0] == 'host': Pport = get_TopoNumberTo( )[j][1].port.port_no if Pport == -1: print 'Pport not found' return else: Get_ArpTable()[switchdp].append(Pport) print "ARP Table = " print Get_ArpTable() Set_ready(True) break
def get_topology_data(self, ev): # Switches and links in the network switches = get_all_switch(self) #self.switch_manager(switches) links = get_link(self, None)
def get_switches(self): return get_all_switch(self)
def get_all_switches(self): return get_all_switch(self)
def _packet_in_handler(self, ev): # If you hit this you might want to increase # the "miss_send_length" of your switch if ev.msg.msg_len < ev.msg.total_len: self.logger.debug("packet truncated: only %s of %s bytes", ev.msg.msg_len, ev.msg.total_len) msg = ev.msg datapath = msg.datapath in_port = msg.match['in_port'] pkt = packet.Packet(msg.data) eth = pkt.get_protocols(ethernet.ethernet)[0] if eth.ethertype == ether_types.ETH_TYPE_LLDP: # ignore lldp packet return pr, start = self.enableProf() dst = eth.dst src = eth.src dpid = datapath.id # Figure out environment links = api.get_all_link(self) switches = api.get_all_switch(self) hosts = api.get_all_host(self) arp_pkt = None if eth.ethertype == ether_types.ETH_TYPE_ARP: arp_pkt = pkt.get_protocols(arp.arp)[0] if arp_pkt.opcode == arp.ARP_REQUEST: # Send to ARP proxy. Cannot perform NIx routing until both hosts # are known by the controller self.ArpProxy(msg.data, datapath, in_port, links, switches, hosts) elif arp_pkt.opcode == arp.ARP_REPLY: self.ArpReply(msg.data, datapath, arp_pkt.dst_ip, links, switches, hosts) self.disableProf(pr, start, "ARP") return #self.logger.info("%s: packet in %s %s %s %s", time.time(), dpid, src, dst, in_port) # Start nix vector code src_ip = '' dst_ip = '' srcNode = None dstNode = None if eth.ethertype == ether_types.ETH_TYPE_ARP: arp_pkt = pkt.get_protocols(arp.arp)[0] src_ip = arp_pkt.src_ip dst_ip = arp_pkt.dst_ip elif eth.ethertype == ether_types.ETH_TYPE_IP: ipv4_pkt = pkt.get_protocols(ipv4.ipv4)[0] src_ip = ipv4_pkt.src dst_ip = ipv4_pkt.dst for host in hosts: if src_ip == host.ipv4[0]: srcNode = host if dst_ip == host.ipv4[0]: dstNode = host if srcNode is None or dstNode is None: self.ArpProxy(msg.data, datapath, in_port, links, switches, hosts) self.disableProf(pr, start, "UNKDST") return srcSwitch = [ switch for switch in switches if switch.dp.id == srcNode.port.dpid ][0] dstSwitch = [ switch for switch in switches if switch.dp.id == dstNode.port.dpid ][0] # Send reverse path first #alg_pr,alg_start = self.enableProf() parentVec = {} foundIt = self.BFS(dstSwitch, srcSwitch, links, switches, parentVec) #self.disableProf(alg_pr,alg_start,"RVS_ALG") #rvs_pr,rvs_start = self.enableProf() sdnNix = [] nixVector = [] if foundIt: self.BuildNixVector(parentVec, dstSwitch, srcSwitch, links, switches, hosts, nixVector, sdnNix) # Need to send to last switch to send out host port sdnNix.insert(0, (srcSwitch, srcNode.port.port_no)) #self.disableProf(rvs_pr,rvs_start,"RVS_PATH") for curNix in sdnNix: self.sendNixRules(dstSwitch, curNix[0], curNix[1], eth.dst, eth.src, msg, False) #alg_pr,alg_start = self.enableProf() parentVec = {} foundIt = self.BFS(srcSwitch, dstSwitch, links, switches, parentVec) #self.disableProf(alg_pr,alg_start,"FWD_ALG") #fwd_pr,fwd_start = self.enableProf() sdnNix = [] nixVector = [] if foundIt: self.BuildNixVector(parentVec, srcSwitch, dstSwitch, links, switches, hosts, nixVector, sdnNix) # Need to send to last switch to send out host port sdnNix.insert(0, (dstSwitch, dstNode.port.port_no)) #self.disableProf(fwd_pr,fwd_start,"FWD_PATH") for curNix in sdnNix: self.sendNixRules(srcSwitch, curNix[0], curNix[1], eth.src, eth.dst, msg) self.disableProf(pr, start, "COMPLETION")
def _topology_thread(self): while True: self.all_switch = get_all_switch(self) self.all_link = get_all_link(self) self.all_host = get_all_host(self) print 'self.all_switch = ' print len(self.all_switch) print 'self.all_link = ' print len(self.all_link) print 'self.all_host = ' print len(self.all_host) for a in self.all_host: print a.ipv4 hub.sleep(3) if len(self.all_switch) == 6 and len(self.all_host) == 6: c = 0 for i in range(0, len(self.all_switch)): self.TopoNumberTo.append(['switch', self.all_switch[i]]) for i in range(0, len(self.all_host)): self.TopoNumberTo.append(['host', self.all_host[i]]) for x in self.TopoNumberTo: print(x, ':', str(x)) weight = [] for i in range(0, len(self.TopoNumberTo)): weight.append([999999999] * len(self.TopoNumberTo)) for i in range(0, len(weight)): for j in range(0, len(weight[i])): if i == j: weight[i][j] = 0 for link in self.all_link: indexA = 0 indexB = 0 for i in self.TopoNumberTo: if i[0] == 'switch' and i[1].dp.id == link.src.dpid: indexA = self.TopoNumberTo.index(i) if i[0] == 'switch' and i[1].dp.id == link.dst.dpid: indexB = self.TopoNumberTo.index(i) weight[indexA][indexB] = 1 weight[indexB][indexA] = 1 for host in self.all_host: indexA = 0 indexB = 0 for i in self.TopoNumberTo: if i[0] == 'host' and i[1] == host: indexA = self.TopoNumberTo.index(i) if i[0] == 'switch' and i[1].dp.id == host.port.dpid: indexB = self.TopoNumberTo.index(i) weight[indexA][indexB] = 1 weight[indexB][indexA] = 1 print weight #forwarding matrix to forwarding Table forwardingMatrix = MakeForwardingTable(weight) print forwardingMatrix for i in range(0, len(self.TopoNumberTo)): if self.TopoNumberTo[i][0] == 'switch': switchdp = self.TopoNumberTo[i][1].dp.id self.forwardingTable[switchdp] = {} for j in range(0, len(forwardingMatrix[i])): if self.TopoNumberTo[j][0] == 'host': dsthost = self.TopoNumberTo[j][1].mac Pport = -1 if self.TopoNumberTo[forwardingMatrix[i] [j]][0] == 'switch': for link in self.all_link: if link.src.dpid == switchdp and link.dst.dpid == self.TopoNumberTo[ forwardingMatrix[i] [j]][1].dp.id: Pport = link.src.port_no if self.TopoNumberTo[forwardingMatrix[i] [j]][0] == 'host': Pport = self.TopoNumberTo[j][ 1].port.port_no if Pport == -1: print 'host not found' return else: self.forwardingTable[switchdp][ dsthost] = Pport print self.forwardingTable #arpMatrix to ArpTable check = [0] * len(self.TopoNumberTo) check[0] = 1 arpMatrix = [] for i in range(0, len(self.TopoNumberTo)): arpMatrix.append([0] * len(self.TopoNumberTo)) for i in range(0, len(self.TopoNumberTo)): for j in range(0, len(self.TopoNumberTo)): if weight[i][j] == 1 and check[j] == 0: arpMatrix[i][j] = 1 arpMatrix[j][i] = 1 check[j] = 1 print arpMatrix for i in range(0, len(self.TopoNumberTo)): if self.TopoNumberTo[i][0] == 'switch': switchdp = self.TopoNumberTo[i][1].dp.id self.ArpTable[switchdp] = [] for j in range(0, len(arpMatrix[i])): if arpMatrix[i][j] == 1: Pport = -1 if self.TopoNumberTo[j][0] == 'switch': for link in self.all_link: if link.src.dpid == switchdp and link.dst.dpid == self.TopoNumberTo[ j][1].dp.id: Pport = link.src.port_no if self.TopoNumberTo[j][0] == 'host': Pport = self.TopoNumberTo[j][ 1].port.port_no if Pport == -1: print 'Pport not found' return else: self.ArpTable[switchdp].append(Pport) print self.ArpTable break
def getDataPaths(self): """ Returns a dict of all switch objects """ switches = dict() for i in get_all_switch(self): switches[i.dp.id]=i return switches
def _packet_in_handler(self, ev): # If you hit this you might want to increase # the "miss_send_length" of your switch if ev.msg.msg_len < ev.msg.total_len: self.logger.debug("packet truncated: only %s of %s bytes", ev.msg.msg_len, ev.msg.total_len) msg = ev.msg datapath = msg.datapath ofproto = datapath.ofproto parser = datapath.ofproto_parser in_port = msg.match['in_port'] pkt = packet.Packet(msg.data) eth = pkt.get_protocols(ethernet.ethernet)[0] if eth.ethertype == ether_types.ETH_TYPE_LLDP: # ignore lldp packet return dst = eth.dst src = eth.src dpid = datapath.id # Figure out environment links = api.get_all_link(self) switches = api.get_all_switch(self) hosts = api.get_all_host(self) arp_pkt = None if eth.ethertype == ether_types.ETH_TYPE_ARP: arp_pkt = pkt.get_protocols(arp.arp)[0] if arp_pkt.opcode == arp.ARP_REQUEST: # Send to ARP proxy. Cannot perform NIx routing until both hosts # are known by the controller self.ArpProxy(msg.data, datapath, in_port, links, switches, hosts) return elif arp_pkt.opcode == arp.ARP_REPLY: self.ArpReply(msg.data, datapath, arp_pkt.dst_ip, links, switches, hosts) return self.logger.info("%s: packet in %s %s %s %s", time.time(), dpid, src, dst, in_port) # Start nix vector code numNodes = len(switches) + len(hosts) src_ip = '' dst_ip = '' srcNode = '' dstNode = '' if eth.ethertype == ether_types.ETH_TYPE_ARP: arp_pkt = pkt.get_protocols(arp.arp)[0] src_ip = arp_pkt.src_ip dst_ip = arp_pkt.dst_ip elif eth.ethertype == ether_types.ETH_TYPE_IP: ipv4_pkt = pkt.get_protocols(ipv4.ipv4)[0] src_ip = ipv4_pkt.src dst_ip = ipv4_pkt.dst for host in hosts: if src_ip == host.ipv4[0]: srcNode = host if dst_ip == host.ipv4[0]: dstNode = host srcSwitch = [ switch for switch in switches if switch.dp.id == srcNode.port.dpid ][0] dstSwitch = [ switch for switch in switches if switch.dp.id == dstNode.port.dpid ][0] parentVec = {} foundIt = self.BFS(numNodes, srcSwitch, dstSwitch, links, switches, hosts, parentVec) sdnNix = [] nixVector = [] if foundIt: self.BuildNixVector(parentVec, srcSwitch, dstSwitch, links, switches, hosts, nixVector, sdnNix) # Need to send to last switch to send out host port sdnNix.insert(0, (dstSwitch, dstNode.port.port_no)) for curNix in sdnNix: self.sendNixRules(srcSwitch, curNix[0], curNix[1], msg)