Пример #1
0
def send(Type, flow_1, port=None):
    flow = str(flow_1)
    ofp_flow_wildcards = of.ofp_flow_wildcards(flow[8:12])
    ofp_match = of.ofp_match(flow[12:48])
    ofp_flow_mod = of.ofp_flow_mod(flow[48:72])
    if len(flow) >= 88:
        action_header = of.ofp_action_header(flow[72:80])
        action_output = of.ofp_action_output(flow[80:88])
    #we need to send the stats request packets periodically
    msg = {
        0:
        of.ofp_header(type=16, length=12) /
        of.ofp_stats_request(type=0),  #Type of  OFPST_DESC (0) 
        1:
        of.ofp_header(type=16, length=56) / of.ofp_stats_request(type=1) /
        ofp_flow_wildcards / ofp_match /
        of.ofp_flow_stats_request(out_port=ofp_flow_mod.out_port),  #flow stats
        2:
        of.ofp_header(type=16, length=56) / of.ofp_stats_request(type=2) /
        ofp_flow_wildcards / ofp_match /
        of.ofp_aggregate_stats_request(),  # aggregate stats request
        3:
        of.ofp_header(type=16, length=12) /
        of.ofp_stats_request(type=3),  #Type of  OFPST_TABLE (0) 
        4:
        of.ofp_header(type=16, length=20) / of.ofp_stats_request(type=4) /
        of.ofp_port_stats_request(port_no=port),  # port stats request    
        5:
        of.ofp_header(type=16, length=20) / of.ofp_stats_request(type=5) /
        of.ofp_queue_stats_request(),  #queue request
        6:
        of.ofp_header(type=16, length=12) /
        of.ofp_stats_request(type=0xffff)  #vendor request
    }
    return msg[Type]
Пример #2
0
    def controller_handler(self, address, fd, events):
        if events & io_loop.READ:
            data = self.sock_con.recv(1024)
            if data == '':
                print "controller disconnected"
                io_loop.remove_handler(self.fd_con)
                print "closing connection to switch"
                self.sock_sw.close()
                io_loop.remove_handler(self.fd_sw)
            else:
                rmsg = of.ofp_header(data[0:8])
                # Here, we can manipulate OpenFlow packets from CONTROLLER.
                if rmsg.type == 0xff:  #cflow_mod
                    header = ofc.ofp_header(data[0:8])

                    cflow_mod = ofc.ofp_cflow_mod(data[8:16])
                    cflow_connect_wildcards = ofc.ofp_connect_wildcards(
                        data[16:18])
                    cflow_connect = ofc.ofp_connect(data[18:92])
                    ofp_action_output = ofc.ofp_action_output(data[92:])

                    msg = header / cflow_mod / cflow_connect_wildcards / cflow_connect / ofp_action_output
                    msg.show()
                    data = convert.ofc2of(msg, self.buffer, self.dpid)

                    self.flow_cache.append([time.time(), data])

                elif rmsg.type == 14:
                    print "send flow_mod"
                    #header = of.ofp_header(data[0:8])
                    #wildcards=of.ofp_flow_wildcards(data[8:12])
                    #match=of.ofp_match(data[12:48])
                    #flow_mod =of.ofp_flow_mod(data[48:72])
                    #action_header = of.ofp_action_header(data[72:80])
                    #action_output =of.ofp_action_output(data[80:88])
                    #data1 =header/wildcards/match/flow_mod/action_header/action_output
                    #self.flow_cache.append([time.time(),data1])
                #full message for flow status request: ofp_stats_rqeuest()/ofp_flow_wildcards()/ofp_match()/ofp_flow_stats_request()
                elif rmsg.type == 16 and 0:  #do nothing and send it .
                    header = ofc.ofp_header(data[0:8])
                    ofp_stats_request = ofc.ofp_stats_request(data[8:12])
                    if ofp_stats_request.type == 1:
                        ofp_flow_wildcards = ofc.ofp_flow_wildcards(
                            data[12:16])
                        data_match = ofc.ofp_match(data[16:52])
                        ofp_flow_stats_request = ofc.ofp_flow_stats_request(
                            data[52:56])
                        for f in self.flow_cache:
                            flow = str(f[1])
                            ofp_flow_wildcards = ofc.ofp_flow_wildcards(
                                flow[8:12])
                            ofp_flow_match = ofc.ofp_match(flow[12:48])
                            ofp_flow_stats_request.out_put = of.ofp_action_output(
                                flow[80:88]).port
                            data = ofc.ofp_header(
                                type=16, length=56
                            ) / ofp_stats_request / ofp_flow_wildcards / ofp_flow_match / ofp_flow_stats_request

                            #we try to delete the flow by this code.
                            #data = of.ofp_header(type=14,length=88)/ofp_flow_wildcards/ofp_flow_match/of.ofp_flow_mod(command=3,flags=1)
                            #print 'delete matching flow'

                            io_loop.update_handler(self.fd_sw, io_loop.WRITE)
                            self.queue_sw.put(
                                str(data)
                            )  #put it into the queue of packet which need to send to Switch.
                    elif ofp_stats_request.type == 0:
                        print "send the ofp_stats_request(type = 0)"
                    elif ofp_stats_request.type == 2:
                        print "aggregate request"
                        ofp_flow_wildcards = ofc.ofp_flow_wildcards(
                            data[12:16])
                        data_match = ofc.ofp_match(data[16:52])
                        ofp_aggregate_stats_request = ofc.ofp_aggregate_stats_request(
                            data[52:56])
                        flow = str(self.flow_cache)
                        wildcards = ofc.ofp_flow_wildcards(flow[8:12])
                        match = ofc.ofp_match(flow[12:48])

                        data = header / ofp_stats_request / wildcards / match / ofp_aggregate_stats_request
                    elif ofp_stats_request.type == 3:
                        print "table request"
                    elif ofp_stats_request.type == 4:
                        print "port request"
                    elif ofp_stats_request.type == 5:
                        print "queue request"
                    elif ofp_stats_request.type == 0xffff:
                        print "vendor request"
                #There are no need to change other packets,just send them!
                io_loop.update_handler(self.fd_sw, io_loop.WRITE)
                self.queue_sw.put(str(data))

        if events & io_loop.WRITE:
            try:
                next_msg = self.queue_con.get_nowait()
            except Queue.Empty:
                io_loop.update_handler(self.fd_con, io_loop.READ)
            else:
                self.sock_con.send(next_msg)
#####################delete the flow cache by hard_timeout###################
        for f in self.flow_cache:
            if fresh(f):
                self.flow_cache.remove(f)
Пример #3
0
    def controller_handler(self, address, fd, events):
        if events & io_loop.READ:
            data = self.sock_con.recv(1024)
            if data == '':
                print "controller disconnected"
                io_loop.remove_handler(self.fd_con)
                print "closing connection to switch"
                self.sock_sw.close()
                io_loop.remove_handler(self.fd_sw)
            else:
                rmsg = of.ofp_header(data[0:8])
                # Here, we can manipulate OpenFlow packets from CONTROLLER.
                if rmsg.type == 0xff:                           #cflow_mod   
                    header = ofc.ofp_header(data[0:8])
                    
                    cflow_mod = ofc.ofp_cflow_mod(data[8:16])
                    cflow_connect_wildcards = ofc.ofp_connect_wildcards(data[16:18])
                    cflow_connect = ofc.ofp_connect(data[18:92])
                    ofp_action_output= ofc.ofp_action_output(data[92:])

                    msg = header/cflow_mod/cflow_connect_wildcards/cflow_connect/ofp_action_output
                    msg.show()
                    data = convert.ofc2of(msg, self.buffer, self.dpid) 
                    
                    self.flow_cache.append([time.time(),data])

                elif rmsg.type == 14:
                    print "send flow_mod"
                    #header = of.ofp_header(data[0:8])
                    #wildcards=of.ofp_flow_wildcards(data[8:12])
                    #match=of.ofp_match(data[12:48])
                    #flow_mod =of.ofp_flow_mod(data[48:72])
                    #action_header = of.ofp_action_header(data[72:80])
                    #action_output =of.ofp_action_output(data[80:88])
                    #data1 =header/wildcards/match/flow_mod/action_header/action_output
                    #self.flow_cache.append([time.time(),data1])
                #full message for flow status request: ofp_stats_rqeuest()/ofp_flow_wildcards()/ofp_match()/ofp_flow_stats_request()
                elif rmsg.type == 16 and 0: #do nothing and send it .
                    header = ofc.ofp_header(data[0:8])
                    ofp_stats_request = ofc.ofp_stats_request(data[8:12])
                    if ofp_stats_request.type == 1:
                        ofp_flow_wildcards = ofc.ofp_flow_wildcards(data[12:16])
                        data_match = ofc.ofp_match(data[16:52])
                        ofp_flow_stats_request = ofc.ofp_flow_stats_request(data[52:56])
                        for f in self.flow_cache:
                            flow = str(f[1])
                            ofp_flow_wildcards = ofc.ofp_flow_wildcards(flow[8:12])
                            ofp_flow_match = ofc.ofp_match(flow[12:48])
                            ofp_flow_stats_request.out_put = of.ofp_action_output(flow[80:88]).port
                            data = ofc.ofp_header(type = 16, length = 56)/ofp_stats_request/ofp_flow_wildcards/ofp_flow_match/ofp_flow_stats_request

                            #we try to delete the flow by this code.
                            #data = of.ofp_header(type=14,length=88)/ofp_flow_wildcards/ofp_flow_match/of.ofp_flow_mod(command=3,flags=1)
                            #print 'delete matching flow'

                            io_loop.update_handler(self.fd_sw, io_loop.WRITE)
                            self.queue_sw.put(str(data))#put it into the queue of packet which need to send to Switch.  
                    elif ofp_stats_request.type == 0:
                        print "send the ofp_stats_request(type = 0)"
                    elif ofp_stats_request.type ==2:
                        print "aggregate request"
                        ofp_flow_wildcards = ofc.ofp_flow_wildcards(data[12:16])
                        data_match = ofc.ofp_match(data[16:52])
                        ofp_aggregate_stats_request = ofc.ofp_aggregate_stats_request(data[52:56])
                        flow =  str(self.flow_cache)
                        wildcards = ofc.ofp_flow_wildcards(flow[8:12])
                        match = ofc.ofp_match(flow[12:48])

                        data = header/ofp_stats_request/wildcards/match/ofp_aggregate_stats_request
                    elif ofp_stats_request.type ==3:
                        print "table request"
                    elif ofp_stats_request.type ==4:
                        print "port request"
                    elif ofp_stats_request.type ==5:
                        print "queue request" 
                    elif ofp_stats_request.type ==0xffff:
                        print "vendor request"
                #There are no need to change other packets,just send them!
                io_loop.update_handler(self.fd_sw, io_loop.WRITE)
                self.queue_sw.put(str(data))
    
        if events & io_loop.WRITE:
            try:
                next_msg = self.queue_con.get_nowait()
            except Queue.Empty:
                io_loop.update_handler(self.fd_con, io_loop.READ)
            else:
                self.sock_con.send(next_msg)
#####################delete the flow cache by hard_timeout###################
        for f in self.flow_cache:
            if fresh(f):
                self.flow_cache.remove(f)
Пример #4
0
def send(Type, flow_1, port =None):
    flow =str(flow_1)
    ofp_flow_wildcards=of.ofp_flow_wildcards(flow[8:12])
    ofp_match =of.ofp_match(flow[12:48])
    ofp_flow_mod =of.ofp_flow_mod(flow[48:72])
    if len(flow)>=88:
        action_header = of.ofp_action_header(flow[72:80])
        action_output = of.ofp_action_output(flow[80:88])
    #we need to send the stats request packets periodically
    msg = { 0: of.ofp_header(type = 16, length = 12)/of.ofp_stats_request(type = 0),                            #Type of  OFPST_DESC (0) 
            1: of.ofp_header(type = 16, length = 56)/of.ofp_stats_request(type =1)/ofp_flow_wildcards/ofp_match/of.ofp_flow_stats_request(out_port = ofp_flow_mod.out_port),                  #flow stats
            2: of.ofp_header(type = 16, length =56)/of.ofp_stats_request(type = 2)/ofp_flow_wildcards/ofp_match/of.ofp_aggregate_stats_request(),                                  # aggregate stats request
            3: of.ofp_header(type = 16, length = 12)/of.ofp_stats_request(type = 3),                            #Type of  OFPST_TABLE (0) 
            4: of.ofp_header(type = 16, length =20)/of.ofp_stats_request(type = 4)/of.ofp_port_stats_request(port_no = port),   # port stats request    
            5: of.ofp_header(type = 16, length =20)/of.ofp_stats_request(type =5)/of.ofp_queue_stats_request(), #queue request
            6: of.ofp_header(type = 16, length = 12)/of.ofp_stats_request(type = 0xffff)                        #vendor request
        }
    return msg[Type]