Example #1
0
    def find_host(self, mac_addr):
        hosts = ryu_api.get_all_host(self)
        for host in hosts:
            if host.mac == mac_addr:
                return host

        return None
Example #2
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()
Example #3
0
    def get_nexthop_host(self, ip):
        hosts = topo_api.get_all_host(self)

        for host in hosts:
            if ip in host.ipv4:
                return host

        return None
Example #4
0
    def get_host(self, ip):
        hosts = topo_api.get_all_host(self)

        for host in hosts:
            if ip in host.ipv4:
                return host

        return None
 def switch_leave_handler(self, ev):
     hostList = api.get_all_host(self)
     self.hosts = [host.mac for host in hostList]
     self.hostLinks = [(host.mac, host.port.dpid, {
         'port': host.port.port_no,
         'bytes': 0
     }) for host in hostList]
     self.get_topology_data(ev)
Example #6
0
    def _packet_in_handler(self, ev):

        msg = ev.msg
        datapath = msg.datapath
        ofproto = datapath.ofproto
        parser = datapath.ofproto_parser
        in_port = msg.match['in_port']

        pkt_in = packet.Packet(msg.data)
        eth_in = pkt_in.get_protocol(ethernet.ethernet)
        arp_in = pkt_in.get_protocol(arp.arp)

        #        if eth.ethertype != ether_types.ETH_TYPE_ARP:
        #            return

        # gestiamo solo i pacchetti ARP REQUEST
        if arp_in is None:
            return

        assert arp_in.opcode == arp.ARP_REQUEST

        destination_host_mac = None

        host_list = get_all_host(self)
        for host in host_list:
            if arp_in.dst_ip in host.ipv4:
                destination_host_mac = host.mac
                break

        # host non trovato
        if destination_host_mac is None:
            return

        pkt_out = packet.Packet()
        eth_out = ethernet.ethernet(dst=eth_in.src,
                                    src=destination_host_mac,
                                    ethertype=ether_types.ETH_TYPE_ARP)
        arp_out = arp.arp(opcode=arp.ARP_REPLY,
                          src_mac=destination_host_mac,
                          src_ip=arp_in.dst_ip,
                          dst_mac=arp_in.src_mac,
                          dst_ip=arp_in.src_ip)
        pkt_out.add_protocol(eth_out)
        pkt_out.add_protocol(arp_out)
        pkt_out.serialize()

        actions = [parser.OFPActionOutput(in_port)]

        out = parser.OFPPacketOut(datapath=datapath,
                                  buffer_id=ofproto.OFP_NO_BUFFER,
                                  in_port=ofproto.OFPP_CONTROLLER,
                                  actions=actions,
                                  data=pkt_out.data)
        datapath.send_msg(out)
Example #7
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}
Example #8
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)
Example #9
0
File: DPI.py Project: dsdn/tssdn
    def create_ip_mac_dict(self, ev):

        # Wait for one second for IP address to get updated
        time.sleep(1)
        hosts = api.get_all_host(self)
        if len(self.ip_to_mac) == hosts:
            return
        else:
            for f in hosts:
                if f.ipv4[0]: self.ip_to_mac[f.ipv4[0]] = f.mac

            print self.ip_to_mac
Example #10
0
File: DPI.py Project: dsdn/tssdn
    def create_ip_mac_dict(self, ev): 
        
        # Wait for one second for IP address to get updated
        time.sleep(1)
        hosts = api.get_all_host(self)
        if len(self.ip_to_mac) == hosts:
            return
        else:
            for f in hosts:
                if f.ipv4[0]: self.ip_to_mac[f.ipv4[0]]=f.mac

            print self.ip_to_mac
Example #11
0
    def packet_in_handler(self, ev):
        msg = ev.msg
        dp = msg.datapath
        dpid = dp.id
        ofproto = dp.ofproto

        pkt = packet.Packet(msg.data)
        tcp_header = pkt.get_protocol(tcp.tcp)

        if tcp_header is None or (tcp_header.src_port is not 179 and
           tcp_header.dst_port is not 179):
            # ignore non-BGP packet
            return

        ipv4_header = pkt.get_protocol(ipv4.ipv4)
        src_ip = ipv4_header.src
        dst_ip = ipv4_header.dst
        self.logger.info("BGP from %s to %s", src_ip, dst_ip)

        # create a path from src to dst
        hosts = topo_api.get_all_host(self)
        src_host = None
        dst_host = None

        for host in hosts:
            if src_ip in host.ipv4:
                src_host = host

            elif dst_ip in host.ipv4:
                dst_host = host

        if src_host is None or dst_host is None:
            # can't find src or dst, drop it
            return

        src_port = src_host.port
        dst_port = dst_host.port
        dst_mac = dst_host.mac
        to_dst_match = dp.ofproto_parser.OFPMatch(eth_dst=dst_mac,
                                                  ipv4_dst=dst_ip,
                                                  eth_type=2048)

        port_no = self.fwd.setup_shortest_path(src_port.dpid,
                                               dst_port.dpid,
                                               dst_port.port_no,
                                               to_dst_match)

        if port_no is None:
            # Can't find path to destination, ignore it.
            return

        self.packet_out(dp, msg, port_no)
Example #12
0
 def _monitor(self):
     while True:
         # print("size of dp "+str(len(self.datapaths)))
         # for dp in self.datapaths.values():
         #     #self._request_stats(dp)
         #     self.send_flow_stats_request(dp,0,0)
         self.print_topo()
         self.logger.info(self.hosts.keys())
         if len(self.all_switches.dps) > 0:
             hosts = get_all_host(self.topology_api_app)
             for host in hosts:
                 self.add_host(host.to_dict())
         hub.sleep(10)
Example #13
0
    def bgp_packet_in_handler(self, ev):
        msg = ev.msg
        dp = msg.datapath
        dpid = dp.id
        ofproto = dp.ofproto

        pkt = packet.Packet(msg.data)
        tcp_header = pkt.get_protocol(tcp.tcp)

        #只处理BGP数据包,tcp端口为179
        if tcp_header is None or (tcp_header.src_port is not 179
                                  and tcp_header.dst_port is not 179):
            return

        ipv4_header = pkt.get_protocol(ipv4.ipv4)
        src_ip = ipv4_header.src
        dst_ip = ipv4_header.dst
        self.logger.info("BGP from %s to %s", src_ip, dst_ip)

        # 获取源、目的主机信息
        hosts = topo_api.get_all_host(self)
        src_host = None
        dst_host = None

        for host in hosts:
            if src_ip in host.ipv4:
                src_host = host

            elif dst_ip in host.ipv4:
                dst_host = host
        if src_host is None or dst_host is None:
            return

        # 建立BGPSpeaker间的数据路径
        src_port = src_host.port
        dst_port = dst_host.port
        dst_mac = dst_host.mac
        to_dst_match = dp.ofproto_parser.OFPMatch(eth_dst=dst_mac,
                                                  ipv4_dst=dst_ip,
                                                  eth_type=2048)
        pre_actions = [dp.ofproto_parser.OFPActionSetField(eth_dst=dst_mac)]
        port_no = self.fwd_util.setup_shortest_path(src_port.dpid,
                                                    dst_port.dpid,
                                                    dst_port.port_no,
                                                    to_dst_match, pre_actions)
        # 将首个数据包直接递交给目的主机,防止首包丢失
        if port_no is None:
            return
        self.fwd_util.packet_out(dp, msg, port_no)
Example #14
0
    def get_topology_data(self, ev=None):
        try:
            switch_list = get_switch(self, None)
            host_list = get_all_host(self)
            #print([h.mac for h in host_list])
            self.num_hosts = len(host_list)

            self.switches = [switch.dp for switch in switch_list]
            self.N = len(self.switches)
            self.num_nodes = self.N + self.num_hosts

            switches=[switch.dp.id for switch in switch_list]
            
            

            self.switches.insert(0, None)
            self.estimated_traffic = np.zeros((self.N + 1, self.N + 1))
            self.real_traffic = np.zeros((self.N + 1, self.N + 1))
            self.estimated_packet_count = np.zeros((self.N + 1, self.N + 1), dtype=np.int)
            self.real_packet_count = np.zeros((self.N + 1, self.N + 1), dtype=np.int)
            self.last_measured = np.zeros((self.N + 1, self.N + 1), dtype=np.int)
            n = int(self.ratio * self.N)
            n = 1 if n == 0 else n
            self.n = n

            link_list = get_link(self, None)
            self.link_list = link_list
            links=[(link.src.dpid,link.dst.dpid,{'port':link.src.port_no}) for link in link_list]

            self.net.add_nodes_from(switches)
            self.net.add_edges_from(links)

            self.switch_ports = {}
            self.switch_links = np.zeros((self.N + 1, self.N + 1), dtype=np.int)
            for link in link_list:
                if link.src.dpid in self.switch_ports:
                    self.switch_ports[link.src.dpid][link.src.port_no] = link.dst.dpid
                    self.switch_links[link.src.dpid, link.dst.dpid] = 1
                else:
                    self.switch_ports[link.src.dpid] = {link.src.port_no: link.dst.dpid}
                    self.switch_links[link.src.dpid, link.dst.dpid] = 1
                if link.dst.dpid in self.switch_ports:
                    self.switch_ports[link.dst.dpid][link.dst.port_no] = link.src.dpid
                    self.switch_links[link.dst.dpid, link.src.dpid] = 1
                else:
                    self.switch_ports[link.dst.dpid] = {link.dst.port_no: link.src.dpid}
                    self.switch_links[link.dst.dpid, link.src.dpid] = 1
        except:
            print(traceback.print_exc())
Example #15
0
    def get_topology_data(self, ev):
        switch_list = get_switch(self.topology_api_app, None)
        host=get_all_host(self.topology_api_app)
        switches = [switch.dp.id for switch in switch_list]
        self.net.add_nodes_from(switches)

        links_list = get_link(self.topology_api_app, None)
        # print links_list
        links = [(link.src.dpid, link.dst.dpid, {'port': link.src.port_no, 'weight': 1}) for link in links_list]
        # add_edges src to dst
        self.net.add_edges_from(links)
        links = [(link.dst.dpid, link.src.dpid, {'port': link.dst.port_no, 'weight': 1}) for link in links_list]
        # add_edges dst to src
        self.net.add_edges_from(links)
        print "**********List of links"
        print self.net.edges()
Example #16
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}
Example #17
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)
    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
Example #19
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", 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)
Example #20
0
    def packet_in_handler(self, ev):
        msg = ev.msg
        datapath = msg.datapath
        ofproto = datapath.ofproto
        parser = datapath.ofproto_parser
        in_port = msg.match['in_port']
        dpid = datapath.id
        pkt = packet.Packet(msg.data)
        eth = pkt.get_protocol(ethernet.ethernet)
        arp_in = pkt.get_protocol(arp.arp)

        # installo le tabelle dopo aver acquisito tutti gli host
        global hostOk
        global routing_matrix
        sw = [x for x in routing_matrix if x.id == dpid]
        sw_obj = sw[0]
        if hostOk and not sw_obj.installed:  # and non ho ancora installato la tabella su dpid

            # prendendo i riferimenti dall'oggetto switch che matcha l'id compilo flow e group tables
            # poi set flag di avvenuta installazione (cosi la prossima volta evito di reinstallare)
            self.group_mod(datapath, sw_obj.port_cw, sw_obj.port_ccw)
            self.flow_mod(datapath, sw_obj)

            # set flag di installazione avvenuta
            idx = 0
            for s in routing_matrix:
                if s.id == sw_obj.id:
                    routing_matrix[idx].installed = 1
                    break
                idx += 1

            print("\n---------------------")
            print("Tables installed on switch", dpid)
            print("---------------------\n")

        # gestione centralizzata arp
        if arp_in is not None:
            assert arp_in.opcode == arp.ARP_REQUEST
            print("\n---------------------")
            print("ARP packet from DP id-> ", dpid, "SRC mac->", eth.src)
            print("---------------------\n")
            destination_host_mac = None
            host_list = get_all_host(self)
            for host in host_list:
                if arp_in.dst_ip in host.ipv4:
                    destination_host_mac = host.mac
                    break

            # aggiungo host a routing matrix
            global i
            # if i < len(routing_matrix):
            if i < 3:
                for host in host_list:
                    for sw in routing_matrix:
                        if host.port.dpid == sw.id and sw.host_mac is "":
                            sw.host_mac = host.mac
                            sw.host_port = int(host.port.name[-1])
                            i += 1

            # elif not hostOk and i == len(routing_matrix):
            elif not hostOk and i == 3:
                hostOk = True
                print("\n---------------------")
                print("Discover all hosts")
                printMat(routing_matrix)
                print("---------------------\n")

            # host non trovato
            if destination_host_mac is None:
                return
            pkt_out = packet.Packet()
            eth_out = ethernet.ethernet(dst=eth.src,
                                        src=destination_host_mac,
                                        ethertype=ether_types.ETH_TYPE_ARP)
            arp_out = arp.arp(opcode=arp.ARP_REPLY,
                              src_mac=destination_host_mac,
                              src_ip=arp_in.dst_ip,
                              dst_mac=arp_in.src_mac,
                              dst_ip=arp_in.src_ip)
            pkt_out.add_protocol(eth_out)
            pkt_out.add_protocol(arp_out)
            pkt_out.serialize()
            actions = [parser.OFPActionOutput(in_port)]
            out = parser.OFPPacketOut(datapath=datapath,
                                      buffer_id=ofproto.OFP_NO_BUFFER,
                                      in_port=ofproto.OFPP_CONTROLLER,
                                      actions=actions,
                                      data=pkt_out.data)
            datapath.send_msg(out)

        else:
            return
Example #21
0
    def create_routing_matrix(self):
        switch_list = get_switch(self, None)
        host_list = get_all_host(self)

        nH = len(host_list)  # The number of hosts
        nS = len(switch_list)  # The number of switches
        N = nH + nS  # The number of nodes
        switches=[switch.dp.id for switch in switch_list]
        print(switches)
        print([host.dp.id for host in host_list])

        links=[str(link.src.dpid) + "->" + str(link.dst.dpid) for link in get_link(self, None)]

        print("host list: " + str(host_list))
        print("host switches: " + str([host.port.dpid for host in host_list]))
        print("nH: " + str(nH))
        print("nS: " + str(nS))
        print("links: " + str(links))


        switches = list(range(nS))  # The list of switch ids
        hosts = list(range(nS, N))  # The list of host ids

        self.net.add_nodes_from([h+1 for h in hosts])
        for i,host in enumerate(host_list):
            print("adding edge between " + str(host.port.dpid) + " and " + str(nS+i+1))
            self.net.add_edge(host.port.dpid,nS+i+1)

        edges = self.net.edges()
        nL = len(edges)
        L = 2*nL  # The number of directed links

        print("L: " + str(L))

        # Populate the dictionaries

        links = {(k-1): list(v.values()) for k,v in self.get_switch_to_link_mapping().items()}
        for i,host in enumerate(host_list):
            links[host.port.dpid-1].append(self.max_link_id)
            self.max_link_id += 1

        lpair2id = {}  # A mapping from edge pairs (src,dst) to link ids
        for i,edge in enumerate(edges):
            lpair2id[edge] = i
            lpair2id[(edge[1], edge[0])] = nL+i
            if edge[0] in links:
                links[edge[0]].append(i)
                links[edge[0]].append(nL+i)
            if edge[1] in links:
                links[edge[1]].append(i)
                links[edge[1]].append(nL+i)
                
        hpair2id = {}  # A mapping from host pairs (src,dst) to the corresponding flow ids
        i = 0
        for src in hosts:
            for dst in hosts:
                if src != dst:
                    hpair2id[(src,dst)] = i
                    i += 1
        H2=i  # The number of flows

        print("Edges----")
        print(edges)
        # Create the routing matrix by using the shortest path algorithm
        A = np.zeros((L,H2))
        for src in hosts:
            for dst in hosts:
                if src != dst:
                    try:
                        path = nx.shortest_path(self.net, src+1, dst+1)
                        for k in range(len(path)-1):
                            A[lpair2id[(path[k],path[k+1])],hpair2id[(src,dst)]] = 1
                    except:
                        continue
        self.A = A
        self.Inference = InferenceModule(A=A, links=links)

        return A
Example #22
0
    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
Example #23
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
        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]

        #alg_pr,alg_start = self.enableProf()
        next_array = self.FloydWarshall(switches, links)
        #self.disableProf(alg_pr,alg_start,"ALGORITHM")

        # Send reverse path first to limit requests to controller
        #rvs_pr,rvs_start = self.enableProf()
        sdnNix = []
        self.BuildNixVector(dstSwitch, srcSwitch, switches, links, next_array,
                            sdnNix)

        # Need to send to last switch to send out host port
        sdnNix.append((srcSwitch, srcNode.port.port_no))
        #self.disableProf(rvs_pr,rvs_start,"RVS_PATH")

        for curNix in reversed(sdnNix):
            self.sendNixRules(dstSwitch, curNix[0], curNix[1], eth.dst,
                              eth.src, msg, False)

        #fwd_pr,fwd_start = self.enableProf()
        sdnNix = []
        self.BuildNixVector(srcSwitch, dstSwitch, switches, links, next_array,
                            sdnNix)

        # Need to send to last switch to send out host port
        sdnNix.append((dstSwitch, dstNode.port.port_no))
        #self.disableProf(fwd_pr,fwd_start,"FWD_PATH")

        for curNix in reversed(sdnNix):
            self.sendNixRules(srcSwitch, curNix[0], curNix[1], eth.src,
                              eth.dst, msg)
        self.disableProf(pr, start, "COMPLETION")
    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()

        # 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(), 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.UCS(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.UCS(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")
Example #25
0
 def _update_hosts(self):
     host_list = get_all_host(self)
     if host_list:
         self.dpids_port_to_host = self._get_dpids_port_to_host(host_list)
         self.hosts = self._get_hosts(host_list)
Example #26
0
 def list_hosts(self):
     hosts = topo_api.get_all_host(self)
     return [host.to_dict() for host in hosts]
Example #27
0
    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()) == 10 and len(Get_all_host()) == 12:
                c=0
                for i in range(0,len(Get_all_switch())) :
                    get_TopoNumberTo().append(['switch',all_switch[i]])
                for i in range(0,len(Get_all_host())) :
                    get_TopoNumberTo().append(['host',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
Example #28
0
    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
Example #29
0
    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
Example #30
0
 def list_hosts(self):
     hosts = topo_api.get_all_host(self)
     return [host.to_dict() for host in hosts]
Example #31
0
 def get_hosts(self):
     return get_all_host(self)
Example #32
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)