Exemplo n.º 1
0
Arquivo: core.py Projeto: wakira/ookm
 def query_link(self, switch1=None, port1=None, switch2=None, host=None):
     if switch1:
         found = None
         if switch2:
             def ss_match(port):
                 return port.src.dpid == switch1.dpid and port.dst.dpid == switch2.dpid
             found = list(filter(ss_match, get_all_link(self.app)))
         elif port1:
             def sp_match(port):
                 return port.src.dpid == switch1.dpid and port.src.port_no == port1
             found = list(filter(sp_match, get_all_link(self.app)))
         elif host:
             if host.mac:
                 r = host_mgr.query_host(mac=host.mac)
             elif host.ipv4:
                 r = host_mgr.query_host(ipv4=host.ipv4)
             elif host.ipv6:
                 r = host_mgr.query_host(ipv6=host.ipv6)
             else:
                 return None, None, None, None
             if r:
                 found_dpid, found_port = r
             else:
                 return None, None, None, None
             return found_dpid, found_port, None, None
         if found:
             return switch1.dpid, found[0].src.port_no, found[0].dst.dpid, found[0].dst.port_no
     return None, None, None, None
Exemplo n.º 2
0
    def get_topology_delay(self):

        hub.sleep(2)

        switch_list = get_all_switch(self)
        switches = [switch.dp.id for switch in switch_list]
        self.graph.add_nodes_from(switches)

        # get edges
        link_list = get_all_link(self)
        for link in link_list:
            try:
                lldp_delay1 = self.lldp_delay[(link.src.dpid, link.dst.dpid)]
                lldp_delay2 = self.lldp_delay[(link.dst.dpid, link.src.dpid)]
                echo_delay1 = self.echo_delay[link.src.dpid]
                echo_delay2 = self.echo_delay[link.dst.dpid]
                delay = (lldp_delay1 + lldp_delay2 - echo_delay1 -
                         echo_delay2) / 2
                w = max(delay, 0)
            except:
                w = float('inf')
            self.logger.info('lldp_delay: %s %s echo_delay: %s %s',
                             lldp_delay1, lldp_delay2, echo_delay1,
                             echo_delay2)
            self.logger.info('delay between %s and %s = %s ms', link.src.dpid,
                             link.dst.dpid, delay * 1000)
            self.graph.add_edge(link.src.dpid,
                                link.dst.dpid,
                                weight=w,
                                port=link.src.port_no)
            self.graph.add_edge(link.dst.dpid,
                                link.src.dpid,
                                weight=w,
                                port=link.dst.port_no)
Exemplo n.º 3
0
    def obtain_topology(self, ev):
        self.switches = get_all_switch(self.app)
        self.links = get_all_link(self.app)
        self.hosts = get_all_host(self.app)

        # handle switches
        for switch in self.switches:
            #print "[INFO]", switch
            dpid = switch.dp.id
            self.switch_dict[dpid] = switch.dp
            self.switch_dp2ports_table.setdefault(dpid, set())
            for port in switch.ports:
                self.switch_dp2ports_table[dpid].add(port)

        # handle links
        for link in self.links:
            #print "[INFO]", link
            src_port = link.src
            dst_port = link.dst
            self.link_dp2port_table[(src_port.dpid,
                                     dst_port.dpid)] = (src_port, dst_port)

        # handle hosts
        for host in self.hosts:
            #print "[INFO]", host
            host_ip = host.ipv4
            host_mac = host.mac
            host_port = host.port
            if len(host_ip) == 0:
                pass
            else:
                self.host_ip2mac_table[host_ip[0]] = host_mac
                self.host_ip2port_table[host_ip[0]] = host_port

        self.process_topo_graph_and_paths()
Exemplo n.º 4
0
    def _packet_out_to(self, msg, dst_dpid, dst_out_port):
        dp = msg.datapath
        src_dpid = dp.id

        # same dp
        if src_dpid == dst_dpid:
            self._packet_out(msg, dst_out_port)

        else:
            g = nx.Graph()
            links = api.get_all_link(self)

            for link in links:
                src = link.src.dpid
                dst = link.dst.dpid
                g.add_edge(src, dst)
            src = src_dpid
            dst = dst_dpid
            path = None

            if nx.has_path(g, src, dst):
                path = nx.shortest_path(g, src, dst)

            if path == None:
                return
            out_port = self._get_out_port(path[0], path[1])
            self._packet_out(msg, out_port)
Exemplo n.º 5
0
    def _packet_out_to(self, msg, dst_dpid, dst_out_port):
        dp = msg.datapath
        src_dpid = dp.id

        # same dp
        if src_dpid == dst_dpid:
            self._packet_out(msg, dst_out_port)

        else:
            g = nx.Graph()
            links = api.get_all_link(self)

            for link in links:
                src = link.src.dpid
                dst = link.dst.dpid
                g.add_edge(src, dst)
            src = src_dpid
            dst = dst_dpid
            path = None

            if nx.has_path(g, src, dst):
                path = nx.shortest_path(g, src, dst)

            if path == None:
                return
            out_port = self._get_out_port(path[0], path[1])
            self._packet_out(msg, out_port)
Exemplo n.º 6
0
    def process_switch_enter(self, ev):
        print "--switch enter--"

        link_list = get_all_link(self.topology_api_app)

        links = list()
        for link in link_list:
            links.append((link.src.dpid, link.dst.dpid, {
                'port': link.src.port_no,
                link.src.dpid: link.src.port_no,
                link.dst.dpid: link.dst.port_no
            }))
            links.append((link.dst.dpid, link.src.dpid, {
                'port': link.dst.port_no,
                link.src.dpid: link.src.port_no,
                link.dst.dpid: link.dst.port_no
            }))
            print "links %s %s: " % (link.src.dpid,
                                     link.dst.dpid), link.src.port_no
            self.network.add_node(link.src.dpid, connected=dict())
            self.network.add_node(link.dst.dpid, connected=dict())
            # print self.network.nodes
            self.network.node[link.src.dpid]['connected'][
                link.src.port_no] = link.dst.dpid
            self.network.node[link.dst.dpid]['connected'][
                link.dst.port_no] = link.src.dpid

        print "links: ", links
        self.network.add_edges_from(links)
Exemplo n.º 7
0
    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))
Exemplo n.º 8
0
    def new_link_handler(self, ev):
        pr,start = self.enableProf()

        links = api.get_all_link(self)
        switches = api.get_all_switch(self)

        self.next_array = self.FloydWarshall(switches, links)
        self.disableProf(pr,start,"FW")
Exemplo n.º 9
0
    def promptFW(self):
        pr, start = self.enableProf()

        links = api.get_all_link(self)
        switches = api.get_all_switch(self)

        self.next_array = self.FloydWarshall(switches, links)
        self.disableProf(pr, start, "FW")
Exemplo n.º 10
0
 def get_switch_port_to_switch_mapping(self):
     for link in get_all_link(self):
         if link.src.port_no != 4294967294:
             # print 'link.src.dpid = ' + str(link.src.dpid) + ',link.src.port = ' + str(link.src.port_no) + 'link.dst.dpid = ' + str(link.dst.dpid)
             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
Exemplo n.º 11
0
    def is_edge_port(self, dpid, port_no):
        links = ryu_api.get_all_link(self)

        for link in links:
            if link.src.dpid == dpid and link.src.port_no == port_no:
                return False

        return True
Exemplo n.º 12
0
    def is_edge_port(self, dpid, port_no):
        links = ryu_api.get_all_link(self)

        for link in links:
            if link.src.dpid == dpid and link.src.port_no == port_no:
                return False

        return True
Exemplo n.º 13
0
    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")
Exemplo n.º 14
0
Arquivo: core.py Projeto: wakira/ookm
    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")
Exemplo n.º 15
0
 def get_link_between_switches(self):
     AllLink = api.get_all_link(self)
     for Link in AllLink:
         src_port = Link.src
         dst_port = Link.dst
         src_sw_dpid = src_port.dpid
         dst_sw_dpid = dst_port.dpid
         self.LinkBetweenSwitches[(src_sw_dpid,
                                   dst_sw_dpid)] = (src_port.port_no,
                                                    dst_port.port_no)
Exemplo n.º 16
0
    def _is_switch_port_to_port(self, dpid, port):
        links = api.get_all_link(self)

        for link in links:

            if link.src.dpid == dpid and \
                link.src.port_no == port:
                return True

        return False
Exemplo n.º 17
0
    def get_nx_graph(self):
        links = ryu_api.get_all_link(self)
        g = nx.DiGraph()

        for link in links:
            src = link.src
            dst = link.dst
            g.add_edge(src.dpid, dst.dpid, src_port=src.port_no, dst_port=dst.port_no)

        return g
Exemplo n.º 18
0
    def _is_switch_port_to_port(self, dpid, port):
        links = api.get_all_link(self)

        for link in links:

            if link.src.dpid == dpid and \
               link.src.port_no == port:
                return True

        return False
Exemplo n.º 19
0
 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)
Exemplo n.º 20
0
 def get_graph(self):
     link_list = get_all_link(self)
     for link in link_list:
         src_dpid = link.src.dpid
         dst_dpid = link.dst.dpid
         src_port = link.src.port_no
         dst_port = link.dst.port_no
         self.graph.add_edge(src_dpid, dst_dpid,
                             src_port=src_port,
                             dst_port=dst_port)
     return self.graph
Exemplo n.º 21
0
    def get_topology_data(self):
        """Get Topology Data
        """
        switch_list = get_all_switch(self)
        switches = [switch.to_dict() for switch in switch_list]
        links_list = get_all_link(self)
        links = [link.to_dict() for link in links_list]
        host_list = get_all_host(self)
        hosts = [h.to_dict() for h in host_list]

        return {"switches": switches, "links": links, "hosts": hosts}
 def get_topology_data(self, ev):
     switchList = api.get_all_switch(self)
     linkList = api.get_all_link(self)
     self.switches = [switch.dp.id for switch in switchList]
     self.srcLinks = [(link.src.dpid, link.dst.dpid, {
         'port': link.src.port_no
     }) for link in linkList]
     self.dstLinks = [(link.dst.dpid, link.src.dpid, {
         'port': link.dst.port_no
     }) for link in linkList]
     self.constructGraph()
Exemplo n.º 23
0
    def find_next_hop_to_destination(self, source_id, destination_id):
        link_list = get_all_link(self)
        net = nx.DiGraph()
        for link in link_list:
            net.add_edge(link.src.dpid, link.dst.dpid, port=link.src.port_no)

        path = nx.shortest_path(net, source_id, destination_id)

        first_link = net[path[0]][path[1]]

        return first_link['port']
Exemplo n.º 24
0
    def _get_out_port(self, src, dst):
        '''
        get output port from src switch to dst switch
        return
        '''
        links = api.get_all_link(self)

        for link in links:

            if link.src.dpid == src and \
               link.dst.dpid == dst:
                return link.src.port_no
Exemplo n.º 25
0
    def _get_topology(self):
        while True:
            self.logger.info('\n')

            hosts = get_all_host(self)
            switch_list = get_all_switch(self)
            links_list = get_all_link(self)
            switches =[switch.dp.id for switch in switch_list]
            self.net.add_nodes_from(switches)
            links=[(link.src.dpid,link.dst.dpid,{'port':link.src.port_no}) for link in links_list]
            self.net.add_edges_from(links)
            hub.sleep(2)
Exemplo n.º 26
0
    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)
Exemplo n.º 27
0
 def get_links(self):
     result = {}
     links = topology_api.get_all_link(self)
     if links:
         for s in links:
             dst = s.dst
             if not result.has_key(str(dst.dpid)):
                 result.setdefault(str(dst.dpid), dst.port_no)
             src = s.src
             if not result.has_key(str(src.dpid)):
                 result.setdefault(str(src.dpid), src.port_no)
     return result
Exemplo n.º 28
0
    def _get_out_port(self, src, dst):
        '''
        get output port from src switch to dst switch
        return
        '''
        links = api.get_all_link(self)

        for link in links:

            if link.src.dpid == src and \
                link.dst.dpid == dst:
                return link.src.port_no
Exemplo n.º 29
0
    def get_nx_graph(self):
        links = ryu_api.get_all_link(self)
        g = nx.DiGraph()

        for link in links:
            src = link.src
            dst = link.dst
            g.add_edge(src.dpid,
                       dst.dpid,
                       src_port=src.port_no,
                       dst_port=dst.port_no)

        return g
Exemplo n.º 30
0
    def getTopo(self,ev):
        # 得到所有的交换机对象
        # Switch对象
        # 属性:  self.dp = dp
        #       self.ports = [Port实例],有port_no,dpid
        #self.logger.info("update")
        self.switches = api.get_all_switch(self)

        # AllLink
        AllLink = api.get_all_link(self)
        self.creat_link_between_switches(AllLink)
        self.creat_port_of_switches(self.switches)
        #self.logger.info(self.port_of_switches)
        self.creat_access_ports()
Exemplo n.º 31
0
 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
Exemplo n.º 32
0
 def get_graph(self):
     link_list = topo_api.get_all_link(self)
     for link in link_list:
         src_dpid = link.src.dpid
         dst_dpid = link.dst.dpid
         src_port = link.src.port_no
         dst_port = link.dst.port_no
         if (src_dpid, dst_dpid) not in self.link_delay.keys():
             x = random.randint(1, 501)
             self.link_delay[(src_dpid, dst_dpid)] = x
             self.link_delay[(dst_dpid, src_dpid)] = x
         self.graph.add_edge(src_dpid,
                             dst_dpid,
                             src_port=src_port,
                             dst_port=dst_port,
                             delay=self.link_delay[(src_dpid, dst_dpid)])
     return self.graph
Exemplo n.º 33
0
    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))
Exemplo n.º 34
0
	def _get_topology(self): 
		hub.sleep(10)
		self.logger.info('\n\n\n')
		#hosts = get_all_host(self) 
		switches = get_all_switch(self) 
		links = get_all_link(self)

		self.logger.info('switches:') 
		for switch in switches:
			self.logger.info(switch.to_dict())
			#self.logger.info(switch)
			self.graph.add_node(switch.dp.id)

		self.logger.info('links:')
		for link in links:
			self.logger.info(link.to_dict()) 
			#self.logger.info('src='+str(link.src.dpid)+', dst='+str(link.dst.dpid))
			self.graph.add_edge(link.src.dpid, link.dst.dpid, port=link.src.port_no)
Exemplo n.º 35
0
    def get_topology_data(self):
        """Get Topology Data
        """
        switch_list = get_all_switch(self.app)
        switches = [switch.to_dict() for switch in switch_list]
        links_list = get_all_link(self.app)
        links = [link.to_dict() for link in links_list]
        host_list = get_all_host(self.app)

        # To remove hosts that are not removed by controller
        ports = []
        for switch in switch_list:
            ports += switch.ports
        port_macs = [p.hw_addr for p in ports]
        n_host_list = [h for h in host_list if h.port.hw_addr in port_macs]

        hosts = [h.to_dict() for h in n_host_list]
        return {"switches": switches, "links": links, "hosts": hosts}
Exemplo n.º 36
0
    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
Exemplo n.º 37
0
    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
Exemplo n.º 39
0
    def _get_topology(self):
        while True:
            self.logger.info('\n\n\n')

            hosts = get_all_host(self)
            switches = get_all_switch(self)
            links = get_all_link(self)

            self.logger.info('hosts:')
            for hosts in hosts:
                self.logger.info(hosts.to_dict())

            self.logger.info('switches:')
            for switch in switches:
                self.logger.info(switch.to_dict())

            self.logger.info('links:')
            for link in links:
                self.logger.info(link.to_dict())

            hub.sleep(2)
Exemplo n.º 40
0
 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_topology(self, ev):
        """
            Get topology info.
        """
        # print('----------------')

        # print('get_topology')
        neighbors = {}
        port_no_to_neighbor_id = {}

        switch_list = get_all_switch(self.topology_api_app)
        for sw in switch_list:
            #print (sw.dp.id) #int
            neighbors[sw.dp.id] = set()
            port_no_to_neighbor_id[sw.dp.id] = {}

        links = get_all_link(self.topology_api_app)
        for link in links:
            # print(link)
            # print(link.src.port_no, link.dst.dpid)#int int
            neighbors[link.src.dpid].add(link.dst.dpid)
            port_no_to_neighbor_id[link.src.dpid][link.src.port_no] = link.dst.dpid
        
        #print(neighbors)
        self.neighbors = neighbors
        self.port_no_to_neighbor_id = port_no_to_neighbor_id
        #print(self.neighbors)
        hosts = get_all_host(self.topology_api_app)
        # print(type(hosts))
        for host in hosts:
            # print('host')
            # print(host.mac, type(host.mac))#str
            # print(host.port.dpid, type(host.port.dpid))#int
            if host.ipv4:
                self.host2switch[host.ipv4[0]] = host.port.dpid
            # XXX 上面如果host有ipv4地址,则按第一个ipv4地址录入其连接的交换机,否则按ipv6地址,都没有就不录入(ip存的是点分十进制字符串)
            elif host.ipv6:
                self.host2switch[host.ipv6[0]] = host.port.dpid
            else:
                pass
Exemplo n.º 42
0
    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 _get_topology(self): 
		hub.sleep(10)
		self.logger.info('\n\n\n')
		#hosts = get_all_host(self) 
		switches = get_all_switch(self) 
		links = get_all_link(self)

		self.logger.info('switches:') 
		for switch in switches:
			self.logger.info(switch.to_dict())
			#self.logger.info(switch)
			self.graph.add_node(switch.dp.id)

		self.logger.info('links:')
		for link in links:
			try:
                lldp_delay1 = self.lldp_delay[(link.src.dpid, link.dst.dpid)]
                lldp_delay2 = self.lldp_delay[(link.dst.dpid, link.src.dpid)]
                echo_delay1 = self.echo_delay[link.src.dpid]
                echo_delay2 = self.echo_delay[link.dst.dpid]
                delay = (lldp_delay1 + lldp_delay2 - echo_delay1 - echo_delay2) / 2
                w = max(delay, 0)
Exemplo n.º 44
0
    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
Exemplo n.º 45
0
 def port_modify_handler(self, ev):
     dp = ev.dp
     port_attr = ev.port
     dp_str = dpid_lib.dpid_to_str(dp.id)
     self.logger.info("\t ***switch dpid=%s"
                      "\n \t port_no=%d hw_addr=%s name=%s config=0x%08x "
                      "\n \t state=0x%08x curr=0x%08x advertised=0x%08x "
                      "\n \t supported=0x%08x peer=0x%08x curr_speed=%d max_speed=%d" %
                      (dp_str, port_attr.port_no, port_attr.hw_addr,
                       port_attr.name, port_attr.config,
                       port_attr.state, port_attr.curr, port_attr.advertised,
                       port_attr.supported, port_attr.peer, port_attr.curr_speed,
                       port_attr.max_speed))
     if port_attr.state == 1:
         self.topo_shape.print_links("Link Down")
         out = self.topo_shape.link_with_src_port(port_attr.port_no, dp.id)
         print("out" + str(out))
         if out is not None:
             print(self.topo_shape.find_shortest_path(out.src.dpid))
     elif port_attr.state == 0:
         self.topo_shape.topo_raw_links = get_all_link(self)  ### HERE ###
         print("Link count: " + str(len(self.topo_shape.topo_raw_links)))
         self.topo_shape.print_links("Link Up")
Exemplo n.º 46
0
 def list_links(self):
     links = topo_api.get_all_link(self)
     return [link.to_dict() for link in links]
Exemplo n.º 47
0
 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()
Exemplo n.º 48
0
    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)
Exemplo n.º 49
0
 def get_all_links(self):
     return get_all_link(self)
    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)