コード例 #1
0
ファイル: trans_agent.py プロジェクト: muzixing/Ccontroller
    def switch_handler(self, address, fd, events):
        if events & io_loop.READ:
            data = self.sock_sw.recv(1024)
            if data == '':
                print "switch disconnected"
                io_loop.remove_handler(self.fd_sw)
                print "closing connection to controller"
                self.sock_con.close()
                io_loop.remove_handler(self.fd_con)
            else:
                rmsg = of.ofp_header(data[0:8])

                if rmsg.type == 6:
                    print "OFPT_FEATURES_REPLY"                                                  #Actually,we just need to change here.
                    header = of.ofp_header(data[0:8]) 
                    print "ofp_features_reply.xid ", header.xid
                    msg = of.ofp_features_reply(data[8:32])     #all sw type should make the convertion. Because our protocol need to use in all nets.
                    msg_port = data[32:]
                    msg = header/msg/msg_port                     
                    self.dpid=msg.datapath_id       #record the dpid
                    data = convert.of2ofc(msg, self.buffer, self.dpid)   
                    
                elif rmsg.type == 10:
                    pkt_in_msg = of.ofp_packet_in(data[8:18])
                    pkt_parsed = of.Ether(data[18:])
                    self.counter+=1
                    #[port + id+ dpid] --> [buffer_id + pkt_in_msg]
                    if isinstance(pkt_parsed.payload, of.IP) or isinstance(pkt_parsed.payload.payload, of.IP):
                        self.buffer[(pkt_in_msg.in_port, self.counter, self.dpid)] = [pkt_in_msg.buffer_id, rmsg/pkt_in_msg/pkt_parsed] # bind buffer id with in port 
                    rmsg.xid = self.counter                 # use the counter to check the buffer
                    data = rmsg/pkt_in_msg/pkt_parsed

                elif rmsg.type ==11:
                    match = ofc.ofp_match(data[12:48])                  #data[8:12]is wildcards
                    for flow in  self.flow_cache:
                        if match == ofc.ofp_match(str(flow[1])[12:48]):
                            self.flow_cache.remove(flow)                #delete the flow
                elif rmsg.type == 17:
                    print "stats_reply" ,len(data)
                    body = data[8:]
                    reply_header = of.ofp_stats_reply(body[:4])
                    if reply_header.type == 1 and len(data)>91:
                        reply_body_match = ofc.ofp_match(body[12:48])
                        reply_body_data2 = ofc.ofp_flow_stats_data(body[48:92])
                        if reply_body_data2.byte_count == 0 and reply_body_data2.packet_count == 0:  #it is a junck flow,delete it!
                            for flow in  self.flow_cache: 
                                if reply_body_match == ofc.ofp_match(str(flow[1])[12:48]):
                                    self.flow_cache.remove(flow)    
                io_loop.update_handler(self.fd_con, io_loop.WRITE)
                self.queue_con.put(str(data))
    
        if events & io_loop.WRITE:
            try:
                next_msg = self.queue_sw.get_nowait()
            except Queue.Empty:
                #print "%s queue empty" % str(address)
                io_loop.update_handler(self.fd_sw, io_loop.READ)
            else:
                #print 'sending "%s" to %s' % (of.ofp_type[of.ofp_header(next_msg).type], self.sock_sw.getpeername())
                self.sock_sw.send(next_msg)
コード例 #2
0
ファイル: trans_agent.py プロジェクト: 09zwcbupt/Controller
 def switch_handler(self, address, fd, events):
     if events & io_loop.READ:
         data = self.sock_sw.recv(1024)
         if data == '':
             print "switch disconnected"
             io_loop.remove_handler(self.fd_sw)
             print "closing connection to controller"
             self.sock_con.close()
             io_loop.remove_handler(self.fd_con)
         else:
             rmsg = of.ofp_header(data)
             rmsg.show()
             # Here, we can manipulate OpenFlow packets from SWITCH.
             io_loop.update_handler(self.fd_con, io_loop.WRITE)
             self.queue_con.put(str(data))
 
     if events & io_loop.WRITE:
         try:
             next_msg = self.queue_sw.get_nowait()
         except Queue.Empty:
             #print "%s queue empty" % str(address)
             io_loop.update_handler(self.fd_sw, io_loop.READ)
         else:
             print 'sending "%s" to %s' % (of.ofp_type[of.ofp_header(next_msg).type], self.sock_sw.getpeername())
             self.sock_sw.send(next_msg)
コード例 #3
0
ファイル: ofp_handler.py プロジェクト: MatheMatrix/Miracle
def hello_handler(data,*arg):
	rmsg = of.ofp_header(data)
	if rmsg.version == 1:
		#print ">>>OFPT_HELLO"
		return of.ofp_header(data)
	else:
		#print ">>>HELLO_ERROR"
		return None
コード例 #4
0
ファイル: ofp_handler.py プロジェクト: opensource-cyy/Miracle
def hello_handler(data, *arg):
    rmsg = of.ofp_header(data)
    if rmsg.version == 1:
        #print ">>>OFPT_HELLO"
        return of.ofp_header(data)
    else:
        #print ">>>HELLO_ERROR"
        return None
コード例 #5
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]
コード例 #6
0
ファイル: ofp_handler.py プロジェクト: MatheMatrix/Miracle
def packet_in_handler(data,fd):
	rmsg =of.ofp_header(data[0:8])
	body = data[8:]
	pkt_in_msg = of.ofp_packet_in(body)
	pkt_parsed = of.Ether(pkt_in_msg.load)
	pkt = rmsg/pkt_in_msg/pkt_parsed
	dpid = sock_dpid[fd]     #if there is not the key of sock_dpid[fd] ,it will be an error.

	return l2_learning.switch(pkt,dpid)
コード例 #7
0
ファイル: ofp_handler.py プロジェクト: opensource-cyy/Miracle
def packet_in_handler(data, fd):
    rmsg = of.ofp_header(data[0:8])
    body = data[8:]
    pkt_in_msg = of.ofp_packet_in(body)
    pkt_parsed = of.Ether(pkt_in_msg.load)
    pkt = rmsg / pkt_in_msg / pkt_parsed
    dpid = sock_dpid[
        fd]  #if there is not the key of sock_dpid[fd] ,it will be an error.

    return l2_learning.switch(pkt, dpid)
コード例 #8
0
ファイル: ofp_handler.py プロジェクト: MatheMatrix/Miracle
def send_stats_request_handler(Type, flow=None, port =None):
	if flow == None:
		flow = of.ofp_header()/of.ofp_flow_wildcards()/of.ofp_match()/of.ofp_flow_mod()
	flow =str(flow)
	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
			}
	#print ">>>OFPT_STATS_REQUEST"
	return msg[Type]
コード例 #9
0
ファイル: controller.py プロジェクト: 09zwcbupt/Controller
def client_handler(address, fd, events):
    sock = fd_map[fd]
    #print sock, sock.getpeername(), sock.getsockname()
    if events & io_loop.READ:
        data = sock.recv(1024)
        if data == '':
            """
            According to stackoverflow(http://stackoverflow.com/questions/667640/how-to-tell-if-a-connection-is-dead-in-python)
            When a socket is closed, the server will receive a EOF. In python, however, server will
            receive a empty string(''). So, when a switch disconnected, the server will find out
            at once. But, if you do not react on this incident, there will be always a ``ioloop.read``
            event. And the loop will run forever, thus, the CPU useage will be pretty high.
            """
            print "connection dropped"
            io_loop.remove_handler(fd)
        if len(data)<8:
            print "not a openflow message"
        else:
            #print len(data)
            #if the data length is 8, then only of header
            #else, there are payload after the header
            if len(data)>8:
                rmsg = of.ofp_header(data[0:8])
                body = data[8:]
            else:
                rmsg = of.ofp_header(data)
            #rmsg.show()
            if rmsg.type == 0:
                print "OFPT_HELLO"
                msg = of.ofp_header(type = 5)
                io_loop.update_handler(fd, io_loop.WRITE)
                message_queue_map[sock].put(data)
                message_queue_map[sock].put(str(msg))
            elif rmsg.type == 1:
                print "OFPT_ERROR"
            elif rmsg.type == 5:
                print "OFPT_FEATURES_REQUEST"
            elif rmsg.type == 6:
                print "OFPT_FEATURES_REPLY"
                #print "rmsg.load:",len(body)/48
                msg = of.ofp_features_reply(body[0:24])#length of reply msg
                #msg.show()
                port_info_raw = body[24:]
                port_info = {}
                print "port number:",len(port_info_raw)/48, "total length:", len(port_info_raw)
                for i in range(len(port_info_raw)/48):
                    #print "port", i, ",len:", len(port_info_raw[0+i*48:48+i*48]) 
                    """The port structure has a length of 48 bytes.
                       so when receiving port info, first split the list
                       into port structure length and then analysis
                    """
                    port_info[i] = of.ofp_phy_port(port_info_raw[0+i*48:48+i*48])
                    #print port_info[i].port_name
                    #port_info[i].show()
                    #print port_info[i].OFPPC_PORT_DOWN

            elif rmsg.type == 2:
                print "OFPT_ECHO_REQUEST"
                msg = of.ofp_header(type=3, xid=rmsg.xid)
                
                #test for status request [which is good]
                global exe_id
                global ofp_match_obj
                if exe_id>1:
                    #len = 8+4+44
                    stat_req = of.ofp_header(type=16,length=56)\
                               /of.ofp_stats_request(type=1)\
                               /of.ofp_flow_wildcards()\
                               /ofp_match_obj\
                               /of.ofp_flow_stats_request()
                    #stat_req.show()
                
                message_queue_map[sock].put(str(msg))
                if exe_id>1:
                    message_queue_map[sock].put(str(stat_req))
                io_loop.update_handler(fd, io_loop.WRITE)
                #end of test
                
                #io_loop.update_handler(fd, io_loop.WRITE)
                #message_queue_map[sock].put(str(msg))
            elif rmsg.type == 3:
                print "OFPT_ECHO_REPLY"
            elif rmsg.type == 4:
                print "OFPT_VENDOR"
            elif rmsg.type == 6:
                print "OFPT_FEATURES_REPLY"
            elif rmsg.type == 7:
                print "OFPT_GET_CONFIG_REQUEST"
            elif rmsg.type == 8:
                print "OFPT_GET_CONFIG_REPLY"
            elif rmsg.type == 9:
                print "OFPT_SET_CONFIG"
            elif rmsg.type == 10:
                #print "OFPT_PACKET_IN"
                #rmsg.show()
                pkt_in_msg = of.ofp_packet_in(body)
                #pkt_in_msg.show()
                raw = pkt_in_msg.load
                pkt_parsed = of.Ether(raw)
                #pkt_parsed.payload.show()
                #print "to see if the payload of ether is IP"
                #if isinstance(pkt_parsed.payload, of.IP):
                    #pkt_parsed.show()
                if isinstance(pkt_parsed.payload, of.ARP):
                    #pkt_parsed.show()
                    #pkt_out = of.ofp_header()/of.ofp_pktout_header()/of.ofp_action_output()
                    pkt_out.payload.payload.port = 0xfffb
                    pkt_out.payload.buffer_id = pkt_in_msg.buffer_id
                    pkt_out.payload.in_port = pkt_in_msg.in_port
                    pkt_out.length = 24
                    #pkt_out.show()
                    io_loop.update_handler(fd, io_loop.WRITE)
                    message_queue_map[sock].put(str(pkt_out))
                if isinstance(pkt_parsed.payload, of.IP):
                    if isinstance(pkt_parsed.payload.payload, of.ICMP):
                        #print "from", pkt_parsed.src, "to", pkt_parsed.dst 
                        
                        """
                        When receive a OPF_PACKET_IN message, you can caculate a path, and then
                        use OFP_FLOW_MOD message to install path. Also, you can find out the exact
                        port to send the message, then you can use the following code to send a
                        OFP_PACKET_OUT message and send the packet to destination.
                        """
                        
                        #pkt_parsed.show()
                        #pkt_out = of.ofp_header()/of.ofp_pktout_header()/of.ofp_action_output()
                        pkt_out.payload.payload.port = 0xfffb
                        pkt_out.payload.buffer_id = pkt_in_msg.buffer_id
                        pkt_out.payload.in_port = pkt_in_msg.in_port
                        pkt_out.length = 24
                        
                        """
                        io_loop.update_handler(fd, io_loop.WRITE)
                        message_queue_map[sock].put(str(pkt_out))
                        """
                        
                        #print pkt_parsed.payload.proto
                        #pkt_parsed.show()
                        #print "ICMP protocol"
                        #pkt_out.show()
                        #usually don't have to fill in ``wilecards`` area
                        
                        global cookie
                        global exe_id
                        exe_id += 1
                        cookie += 1
                        flow_mod_msg = of.ofp_header(type=14,
                                                     length=80,
                                                     xid=exe_id)/\
                        of.ofp_flow_wildcards()\
                        /of.ofp_match(in_port=pkt_in_msg.in_port,
                                      dl_src=pkt_parsed.src,
                                      dl_dst=pkt_parsed.dst,
                                      dl_type=pkt_parsed.type,
                                      nw_tos=pkt_parsed.payload.tos,
                                      nw_proto=pkt_parsed.payload.proto,
                                      nw_src=pkt_parsed.payload.src,
                                      nw_dst=pkt_parsed.payload.dst,
                                      tp_src = pkt_parsed.payload.payload.type,
                                      tp_dst = pkt_parsed.payload.payload.code)\
                        /of.ofp_flow_mod(cookie=cookie,
                                         command=0,
                                         idle_timeout=60,
                                         buffer_id=pkt_in_msg.buffer_id,#icmp type 8: request, 0: reply
                                         flags=1)\
                        /of.ofp_action_output(type=0, 
                                              port=0xfffb,
                                              len=8)
                        
                        #flow_mod_msg.show()
                        global ofp_match_obj
                        ofp_match_obj = of.ofp_match(in_port=pkt_in_msg.in_port,
                                      dl_src=pkt_parsed.src,
                                      dl_dst=pkt_parsed.dst,
                                      dl_type=pkt_parsed.type,
                                      nw_tos=pkt_parsed.payload.tos,
                                      nw_proto=pkt_parsed.payload.proto,
                                      nw_src=pkt_parsed.payload.src,
                                      nw_dst=pkt_parsed.payload.dst,
                                      tp_src = pkt_parsed.payload.payload.type,
                                      tp_dst = pkt_parsed.payload.payload.code)
                        
                        exe_id += 1
                        """
                        
                        print "--------------------------------------------------------------------------------------"
                        print "len of flow_mod_msg        :", len(str(flow_mod_msg))
                        print "len of of_header()         :", len(str(of.ofp_header()))
                        print "len of ofp_flow_wildcards():", len(str(of.ofp_flow_wildcards()))
                        print "len of ofp_match()         :", len(str(of.ofp_match()))
                        print "len of ofp_flow_mod()      :", len(str(of.ofp_flow_mod(command=0,idle_timeout=60,buffer_id=pkt_in_msg.buffer_id)))
                        print "len of ofp_action_output() :", len(str(of.ofp_action_output(type=0,port=0xfffb,len=8)))
                        print "--------------------------------------------------------------------------------------"
                        """
                        io_loop.update_handler(fd, io_loop.WRITE)
                        #message_queue_map[sock].put(str(pkt_out))
                        message_queue_map[sock].put(str(flow_mod_msg))
                        message_queue_map[sock].put(str(of.ofp_header(type=18,xid=exe_id))) #send a barrier request msg.

            elif rmsg.type == 11: 
                print "OFPT_FLOW_REMOVED"
            elif rmsg.type == 12:
                print "OFPT_PORT_STATUS"
            elif rmsg.type == 13:
                print "OFPT_PACKET_OUT"
            elif rmsg.type == 14:
                print "OFPT_FLOW_MOD"
            elif rmsg.type == 15:
                print "OFPT_PORT_MOD"
            elif rmsg.type == 16:
                print "OFPT_STATS_REQUEST"
                
            elif rmsg.type == 17:
                print "OFPT_STATS_REPLY"
                # 1. parsing ofp_stats_reply
                reply_header = of.ofp_stats_reply(body[:4])
                
                # 2.parsing ofp_flow_stats msg
                reply_body_data1 = of.ofp_flow_stats(body[4:8])
                # match field in ofp_flow_stats
                reply_body_wildcards = of.ofp_flow_wildcards(body[8:12])
                reply_body_match = of.ofp_match(body[12:48])
                # second part in ofp_flow_stats
                reply_body_data2 = of.ofp_flow_stats_data(body[48:92])
                
                # 3.parsing actions
                # should first judge action type 
                i = 0
                reply_body_action = []
                #print len(body[92:])
                while i<len(body[92:]):
                    if body[95+i:96+i]==0x08:
                        print "0x08"
                    i+=8
                    if body[95+i:96+i] == 0x08:
                        reply_body_action.append(of.ofp_action_output(body[92+i:100+i]))
                        #i+=8
                # 4.show msg
                msg = reply_header/reply_body_data1/reply_body_wildcards/reply_body_match/reply_body_data2
                msg.show()
                print reply_body_action
            
            # no message body
            elif rmsg.type == 18:
                print "OFPT_BARRIER_REQUEST"
            
            #no message body, the xid is the previous barrier request xid
            elif rmsg.type == 19:
                print "OFPT_BARRIER_REPLY: ", rmsg.xid, "Successful"
            elif rmsg.type == 20:
                print "OFPT_QUEUE_GET_CONFIG_REQUEST"
            elif rmsg.type == 21:
                print "OFPT_QUEUE_GET_CONFIG_REPLY"

    if events & io_loop.WRITE:
        try:
            next_msg = message_queue_map[sock].get_nowait()
        except Queue.Empty:
            #print "%s queue empty" % str(address)
            io_loop.update_handler(fd, io_loop.READ)
        else:
            #print 'sending "%s" to %s' % (of.ofp_header(next_msg).type, address)
            sock.send(next_msg)
コード例 #10
0
ファイル: convert.py プロジェクト: opensource-cyy/Miracle
def of2ofc(msg, buffer, dpid):
    print "of->ofc converting"
    if isinstance(msg, of.ofp_header):#it is a of packet.
        if isinstance(msg.payload, of.ofp_packet_in):
            # Save the buffer_id from pkt_in message. As 1. of.pkt_out message needs buffer_id
            # 2. the in_port is only one kind of pkt, this method seems okay in linear or ring topo
            
            #only need the ofp_header()/ofp_packet_in() part of the msg
            print "packet in from port", msg.payload.in_port
            buffer[msg.payload.in_port] = msg.payload.buffer_id
            
        if isinstance(msg.payload, of.ofp_flow_mod):
            #basic structure: of.ofp_header()/of.ofp_flow_wildcards()/of.ofp_match()/of.ofp_flow_mod()/other_ofp_actions()
            #select info from match (VLAN) and actions (just copy)
            pass
        if isinstance(msg.payload, of.ofp_features_reply):
            print"it is a ofp_features_reply packet"
            #basic structure:0fc.ofp_header()/ofc.ofp_cfeatures_reply()/ofc.ofp_phy_cport()/sup_wave_port_bandwidth()[n] 
            pkt_parsed = msg.payload                        #feature_reply
            port_info = msg.payload.payload
            
            port_raw=str(port_info)
            port_num = len(port_raw)/48  

            phy_port = {}
            phy_cport = {}
            MyPort = {}

            sw = setting.creat_sw(pkt_parsed.datapath_id)  

            cfeatures_reply = ofc.ofp_cfeatures_reply(datapath_id = pkt_parsed.datapath_id,
                                                  n_buffers = pkt_parsed.n_buffers,
                                                  n_tables = pkt_parsed.n_tables,
                                                  n_cports = port_num,
                                                  #features

                                                  OFPC_OTN_SWITCH = sw.type_otn,      #1<<31  if it is a otn switch
                                                  OFPC_WAVE_SWITCH = sw.type_wave,    #1<<30
                                                  OFPC_IP_SWITCH = sw.type_ip,      #1<<29

                                                  OFPC_ARP_MATCH_IP = pkt_parsed.OFPC_ARP_MATCH_IP,
                                                  OFPC_QUEUE_STATS = pkt_parsed.OFPC_QUEUE_STATS,   #1<<6 Queue statistics
                                                  OFPC_IP_STREAM = pkt_parsed.OFPC_IP_STREAM,       #1<<5 Can reassemble IP fragments
                                                  OFPC_RESERVED = pkt_parsed.OFPC_RESERVED,         #1<<4 Reserved, must be zero
                                                  OFPC_STP = pkt_parsed.OFPC_STP,                   #1<<3 802.1d spanning tree
                                                  OFPC_PORT_STATS =pkt_parsed.OFPC_PORT_STATS,      #1<<2 Port statistics
                                                  OFPC_TABLE_STATS = pkt_parsed.OFPC_TABLE_STATS,   #1<<1 Table statistics
                                                  OFPC_FLOW_STATS = pkt_parsed.OFPC_FLOW_STATS,     #1<<0 Flow statistics
                                                  actions = pkt_parsed.actions)
            for i in xrange(port_num):  
                phy_port[i] = of.ofp_phy_port(port_raw[i*48:i*48+48]) 

                MyPort[i] = setting.creat_port(pkt_parsed.datapath_id, phy_port[i].port_no)

                phy_cport[i] =  ofc.ofp_phy_cport(port_no = phy_port[i].port_no, 
                                                  hw_addr = phy_port[i].hw_addr,
                                                  port_name = phy_port[i].port_name,
                                                  #config 
                                                  not_defined = phy_port[i].not_defined,
                                                  OFPPC_NO_PACKET_IN = phy_port[i].OFPPC_NO_PACKET_IN,
                                                  OFPPC_NO_FWD = phy_port[i].OFPPC_NO_FWD,
                                                  OFPPC_NO_FLOOD = phy_port[i].OFPPC_NO_FLOOD,
                                                  OFPPC_NO_RECV_STP =phy_port[i].OFPPC_NO_RECV_STP,
                                                  OFPPC_NO_RECV = phy_port[i].OFPPC_NO_RECV,
                                                  OFPPC_NO_STP = phy_port[i].OFPPC_NO_STP,
                                                  OFPPC_PORT_DOWN =phy_port[i].OFPPC_PORT_DOWN,
                                                  #state 
                                                  
                                                  OFPPS_LINK_DOWN = 0,
                                                  
                                                  curr = 0,
                                                  advertised = 0,
                                                  supported = 0,
                                                  peer = 0,
                                                  #expend for circuit switch ports.
                                                  OFPST_FIBER = MyPort[i].OFPST_FIBER,   # 1<<15 can switch circuits based on SM/MM fiber
                                                  OFPST_WAVE = MyPort[i].OFPST_WAVE,     # 1<<14 can switch circuits based on ITU-T lambdas
                                                  OFPST_T_OTN = MyPort[i].OFPST_T_OTN,   # 1<<13 can switch circuits based on OTN standard
                                                  OFPST_T_SDH = MyPort[i].OFPST_T_SDH,  # 1<<12 can switch circuits based on SDH standard
                                                  OFPST_T_SONET = MyPort[i].OFPST_T_SONET,  # 1<<11 can switch circuits based on SONET standard
                                                  #NOT_DEFINED = 0,  # Not used
                                                  OFPST_ETH = MyPort[i].OFPST_ETH,  # 1<<4 can switch packets based on ETH headers
                                                  OFPST_VLAN = MyPort[i].OFPST_VLAN,  # 1<<3 can switch packets based on VLAN tags
                                                  OFPST_MPLS = MyPort[i].OFPST_MPLS,  # 1<<2 can switch packets based on MPLS labels
                                                  OFPST_IP = MyPort[i].OFPST_IP,  # 1<<1 can switch packets based on IP headers 
                                                  OFPST_L4 = MyPort[i].OFPST_L4,  # 1<<0 can switch packets based on TCP/UDP headers

                                                  SUPP_SW_GRAN = MyPort[i].SUPP_SW_GRAN,         #use for defined something ,waiting a second.
                                                  sup_sdh_port_bandwidth = MyPort[i].sup_sdh_port_bandwidth,
                                                  sup_otn_port_bandwidth = MyPort[i].sup_otn_port_bandwidth,
                                                  peer_port_no = MyPort[i].peer_port_no,
                                                  peer_datapath_id = MyPort[i].peer_datapath_id)\
                                /ofc.sup_wave_port_bandwidth(center_freq_lmda = MyPort[i].center_freq_lmda,
                                                             num_lmda = MyPort[i].num_lmda,
                                                             freq_space_lmda = MyPort[i].freq_space_lmda
                                                             )
                cfeatures_reply =cfeatures_reply/phy_cport[i]    
                
            cfeatures_reply = ofc.ofp_header(type = 24, length =port_num*74+32,)/cfeatures_reply
            return cfeatures_reply 
コード例 #11
0
ファイル: trans_agent.py プロジェクト: opensource-cyy/Miracle
    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)
コード例 #12
0
ファイル: ofp_handler.py プロジェクト: opensource-cyy/Miracle
def barrier_handler(data, *arg):
    #print ">>>OFPT_BARRIER_REQUEST"
    rmsg = of.ofp_header(data[0:8])
    body = data[8:]
    msg = of.ofp_header(type=18, xid=rmsg.xid)
    return msg
コード例 #13
0
ファイル: convert.py プロジェクト: opensource-cyy/Miracle
ofc2of_dict_odu = { 0: lambda x:x+2000,
                    1: lambda x:x+2100,
                    2: lambda x:x+2200,
                    3: lambda x:x+2300}

ofc2of_dict_wave = lambda x:x+3000


##################################The code below is just for test,and you have no need to read it.#################################


if __name__ == "__main__":
    # this convert (can) only match in-coming port and vlan
    
    # 1. packet_in message
    pkt_in_msg = ofc.ofp_header(type=6,length=104)/ofc.ofp_cfeatures_reply(datapath_id=1)/ofc.ofp_phy_cport()/ofc.sup_wave_port_bandwidth()
    #pkt_in_msg.show()
    ofc_pkt = of2ofc(pkt_in_msg, pkt_in_msg.datapath_id, 10) # get buffer_id
    print ofc_pkt
    """
    ofc_pkt = ofc.ofp_header()\
          /ofc.ofp_cflow_mod()\
          /ofc.ofp_connect_wildcards()\
          /ofc.ofp_connect(nport_in=1, supp_sw_otn_gran_in=1, in_port=1)\
          /of.ofp_action_header(type=3)\
          /of.ofp_action_output(type=0, port=0xfffb, len=8)
    #ofc_pkt.show()
    """
    print buffer_id

    # 2. parse ofc message
コード例 #14
0
ファイル: ofp_handler.py プロジェクト: MatheMatrix/Miracle
def echo_request_handler(data,*arg):
	#print ">>>OFPT_ECHO_REQUEST"
	rmsg = of.ofp_header(data)
	msg = of.ofp_header(type = 3,xid=rmsg.xid)
	#msg.show()
	return msg
コード例 #15
0
ファイル: ofp_handler.py プロジェクト: MatheMatrix/Miracle
def stats_reply_handler(data,*arg):
	#print ">>>OFPT_STATS_REPLY:%d",len(data)
	rmsg = of.ofp_header(data[0:8])
	body = data[8:]
	# 1. parsing ofp_stats_reply
	reply_header = of.ofp_stats_reply(body[:4])
	#reply_header.show()
	# 2.parsing ofp_flow_stats msg
	if reply_header.type == 0:
		reply_desc = of.ofp_desc_stats(body[4:])
		reply.show()

	elif reply_header.type == 1 and len(data)>92:
		reply_body_data1 = of.ofp_flow_stats(body[4:8])
		reply_body_data1.show()
		# match field in ofp_flow_stats
		reply_body_wildcards = of.ofp_flow_wildcards(body[8:12])
		reply_body_match = of.ofp_match(body[12:48])
		# second part in ofp_flow_stats
		reply_body_data2 = of.ofp_flow_stats_data(body[48:92])
		# 3.parsing actions
		reply_body_action = []
		if len(body[92:])>8:                         #it is very important!
			num = len(body[92:])/8
			for x in xrange(num):
				reply_body_action.append(of.ofp_action_output(body[92+x*8:100+x*8]))
				msg = reply_header/reply_body_data1/reply_body_wildcards/reply_body_match/reply_body_data2
				msg.show()

	elif reply_header.type == 2:
		reply_aggregate = of.ofp_aggregate_stats_reply(body[4:])
		reply_aggregate.show()

	elif reply_header.type == 3:
		#table_stats
		length = rmsg.length - 12
		num = length/64
		for i in xrange(num):
			table_body = body[4+i*64:i*64+68]
			reply_table_stats = of.ofp_table_stats(table_body[:36])
			table_wildcards = of.ofp_flow_wildcards(table_body[36:40])
			reply_table_stats_data = of.ofp_table_stats_data(table_body[40:64])
			msg_tmp = reply_header/reply_table_stats/table_wildcards/reply_table_stats_data
			msg = rmsg/msg_tmp
			msg.show() 
	elif reply_header.type == 4:
		#port stats reply
		length = rmsg.length - 12
		num = length/104
		for i in xrange(num):
			offset = 4+i*104
			reply_port_stats = of.ofp_port_stats_reply(body[offset:(offset+104)])
			msg_tmp = reply_header/reply_port_stats
			msg = rmsg/msg_tmp
			msg.show()
	elif reply_header.type == 5:
		#queue reply
		length = rmsg.length - 12
		num = length/32
		if num:                     #if the queue is empty ,you need to check it !
			for i in xrange(num):
				offset = 4+i*32
				queue_reply = of.ofp_queue_stats(body[offset:offset+32])
				msg_tmp = reply_header/queue_reply
				msg = rmsg/msg_tmp
				msg.show()
	elif reply_header.type == 0xffff:
		#vendor reply
		msg = rmsg/reply_header/of.ofp_vendor(body[4:])
	return None
コード例 #16
0
 def run(self):
     while not self._quit:
         #print "waiting", sock.getsockname()
         #sock.send("123")
         self.event.wait() #waiting for a packet
         if self._quit:
             print "receive quit signal"
             self.event.clear()
             return
         #need something to ?
         #print self.sock, self.sock.getsockname(), self.sock.getpeername()
         try:
             data = message_queue_map[self.fd].get_nowait()
         except Queue.Empty:
             continue
             #print "%s queue empty" % str(address)
             #io_loop.update_handler(fd, io_loop.READ)
             #print 'sending "%s" to %s' % (of.ofp_header(next_msg).type, address)
         #data = self.receive()
         #print len(data), data
         if len(data)>=8:
             body = data[8:]
             msg = of.ofp_header(data)
             #msg.show()
             if msg.type == 0:
                 print "OFPT_HELLO"
                 msg = of.ofp_header(type = 5)
                 self.send(data)
                 self.send(str(msg))
             elif msg.type == 6:
                 print "OFPT_FEATURES_REPLY"
                 #feature = of.ofp_features_reply(body[0:24])
                 #feature.show()
             elif msg.type == 2:
                 print "OFPT_ECHO_REQUEST"
                 msg = of.ofp_header(type=3, xid=msg.xid)
                 self.send(str(msg))
             elif msg.type == 10:
                 #print "OFPT_PACKET_IN"
                 #rmsg.show()
                 pkt_in_msg = of.ofp_packet_in(body)
                 #pkt_in_msg.show()
                 raw = pkt_in_msg.load
                 pkt_parsed = of.Ether(raw)
                 #pkt_parsed.payload.show()
                 #print "to see if the payload of ether is IP"
                 #if isinstance(pkt_parsed.payload, of.IP):
                     #pkt_parsed.show()
                 if isinstance(pkt_parsed.payload, of.ARP):
                     #pkt_parsed.show()
                     #pkt_out = of.ofp_header()/of.ofp_pktout_header()/of.ofp_action_output()
                     pkt_out.payload.payload.port = 0xfffb
                     pkt_out.payload.buffer_id = pkt_in_msg.buffer_id
                     pkt_out.payload.in_port = pkt_in_msg.in_port
                     pkt_out.length = 24
                     #pkt_out.show()
                     self.send(str(pkt_out))
                 if isinstance(pkt_parsed.payload, of.IP):
                     if isinstance(pkt_parsed.payload.payload, of.ICMP):
                         #print "from", pkt_parsed.src, "to", pkt_parsed.dst 
                         pkt_out.payload.payload.port = 0xfffb
                         pkt_out.payload.buffer_id = pkt_in_msg.buffer_id
                         pkt_out.payload.in_port = pkt_in_msg.in_port
                         pkt_out.length = 24
                         self.send(str(pkt_out))
             #io_loop.stop()
             self.event.clear()
         #except 
         #data = ""
         elif data == "":
             print "close connection"
             self.stop()
             return
         else:
             self.send(data)
             self.event.clear()
     print "quit", self._quit
コード例 #17
0
ファイル: libopencflow.py プロジェクト: muzixing/Ccontroller
18   okay    "OFPT_BARRIER_REQUEST",
19   okay    "OFPT_BARRIER_REPLY",
20   okay    "OFPT_QUEUE_GET_CONFIG_REQUEST",
21   okay    "OFPT_QUEUE_GET_CONFIG_REPLY"
24   oaky    "OFPT_CFEATURES_REPLY"
0xfe oaky    "OFPT_CPORT_STATUS" 
0xff okay    "OFPT_CFLOW_MO
"""
    
if __name__ == '__main__':
    import convert
    import libopenflow as of 
    #a = ofp_cfeatures_reply()
    #b = ofp_phy_cport()
    #c = ofp_phy_cport()
    #d=ofp_header(type =24,length = 32+78*2)/ofp_cfeatures_reply(n_cports=2)/ofp_phy_cport()/sup_wave_port_bandwidth(num_lmda=4)/ofp_phy_cport()/sup_wave_port_bandwidth(num_lmda=5)

    #d.show()
    #a.n_cports=2
    #a.OFPC_TABLE_STATS=100
    #a.OFPST_T_OTN = 1
    #a.SUPP_SW_GRAN = 129
    #print a.SUPP_SW_GRAN
    buffer = {}
    dpid = 8

    e = of.ofp_header(type =6)/of.ofp_features_reply(datapath_id=1)/of.ofp_phy_port(port_no=1)/of.ofp_phy_port(port_no=2)\
                              /of.ofp_phy_port(port_no=3)/of.ofp_phy_port(port_no=4)/of.ofp_phy_port(port_no=5)
    f = convert.of2ofc(e, buffer, dpid)
    f.show()
    p = str(f)
コード例 #18
0
ファイル: ofp_handler.py プロジェクト: opensource-cyy/Miracle
def stats_reply_handler(data, *arg):
    #print ">>>OFPT_STATS_REPLY:%d",len(data)
    rmsg = of.ofp_header(data[0:8])
    body = data[8:]
    # 1. parsing ofp_stats_reply
    reply_header = of.ofp_stats_reply(body[:4])
    #reply_header.show()
    # 2.parsing ofp_flow_stats msg
    if reply_header.type == 0:
        reply_desc = of.ofp_desc_stats(body[4:])
        reply.show()

    elif reply_header.type == 1 and len(data) > 92:
        reply_body_data1 = of.ofp_flow_stats(body[4:8])
        reply_body_data1.show()
        # match field in ofp_flow_stats
        reply_body_wildcards = of.ofp_flow_wildcards(body[8:12])
        reply_body_match = of.ofp_match(body[12:48])
        # second part in ofp_flow_stats
        reply_body_data2 = of.ofp_flow_stats_data(body[48:92])
        # 3.parsing actions
        reply_body_action = []
        if len(body[92:]) > 8:  #it is very important!
            num = len(body[92:]) / 8
            for x in xrange(num):
                reply_body_action.append(
                    of.ofp_action_output(body[92 + x * 8:100 + x * 8]))
                msg = reply_header / reply_body_data1 / reply_body_wildcards / reply_body_match / reply_body_data2
                msg.show()

    elif reply_header.type == 2:
        reply_aggregate = of.ofp_aggregate_stats_reply(body[4:])
        reply_aggregate.show()

    elif reply_header.type == 3:
        #table_stats
        length = rmsg.length - 12
        num = length / 64
        for i in xrange(num):
            table_body = body[4 + i * 64:i * 64 + 68]
            reply_table_stats = of.ofp_table_stats(table_body[:36])
            table_wildcards = of.ofp_flow_wildcards(table_body[36:40])
            reply_table_stats_data = of.ofp_table_stats_data(table_body[40:64])
            msg_tmp = reply_header / reply_table_stats / table_wildcards / reply_table_stats_data
            msg = rmsg / msg_tmp
            msg.show()
    elif reply_header.type == 4:
        #port stats reply
        length = rmsg.length - 12
        num = length / 104
        for i in xrange(num):
            offset = 4 + i * 104
            reply_port_stats = of.ofp_port_stats_reply(body[offset:(offset +
                                                                    104)])
            msg_tmp = reply_header / reply_port_stats
            msg = rmsg / msg_tmp
            msg.show()
    elif reply_header.type == 5:
        #queue reply
        length = rmsg.length - 12
        num = length / 32
        if num:  #if the queue is empty ,you need to check it !
            for i in xrange(num):
                offset = 4 + i * 32
                queue_reply = of.ofp_queue_stats(body[offset:offset + 32])
                msg_tmp = reply_header / queue_reply
                msg = rmsg / msg_tmp
                msg.show()
    elif reply_header.type == 0xffff:
        #vendor reply
        msg = rmsg / reply_header / of.ofp_vendor(body[4:])
    return None
コード例 #19
0
ファイル: ofp_handler.py プロジェクト: MatheMatrix/Miracle
def features_request_handler(data,*arg):
	rmsg = of.ofp_header(data)
	#print ">>>OFPT_FEATURES_REQUEST"
	return of.ofp_header(type =5,xid =1)#xid =0
コード例 #20
0
ファイル: ofp_handler.py プロジェクト: MatheMatrix/Miracle
def barrier_handler(data,*arg):
	#print ">>>OFPT_BARRIER_REQUEST"
	rmsg =of.ofp_header(data[0:8])
	body = data[8:]
	msg = of.ofp_header(type = 18,xid = rmsg.xid) 
	return msg
コード例 #21
0
import Queue
import select
import socket
import libopenflow as of
from functools import partial
from tornado.ioloop import IOLoop
import threading

fd_map = {}              
event_map = {}
thread_map = {}
message_queue_map = {}
pkt_out = of.ofp_header()/of.ofp_pktout_header()/of.ofp_action_output()

class switch(threading.Thread):
    def __init__(self, sock, event):
        print "init for connection", sock.fileno()
        self.sock = sock # for sending pkts
        self.fd = sock.fileno()
        self.event = event # for waiting pkt_in
        self._quit = False
        super(switch, self).__init__()
    
    def send(self, pkt):
        select.select([], [self.sock], [])
        self.sock.send(pkt)
    
    def run(self):
        while not self._quit:
            #print "waiting", sock.getsockname()
            #sock.send("123")
コード例 #22
0
ファイル: ofp_handler.py プロジェクト: opensource-cyy/Miracle
def echo_request_handler(data, *arg):
    #print ">>>OFPT_ECHO_REQUEST"
    rmsg = of.ofp_header(data)
    msg = of.ofp_header(type=3, xid=rmsg.xid)
    #msg.show()
    return msg
コード例 #23
0
18   okay    "OFPT_BARRIER_REQUEST",
19   okay    "OFPT_BARRIER_REPLY",
20   okay    "OFPT_QUEUE_GET_CONFIG_REQUEST",
21   okay    "OFPT_QUEUE_GET_CONFIG_REPLY"
24   oaky    "OFPT_CFEATURES_REPLY"
0xfe oaky    "OFPT_CPORT_STATUS" 
0xff okay    "OFPT_CFLOW_MO
"""
    
if __name__ == '__main__':
    import convert
    import libopenflow as of 
    #a = ofp_cfeatures_reply()
    #b = ofp_phy_cport()
    #c = ofp_phy_cport()
    #d=ofp_header(type =24,length = 32+78*2)/ofp_cfeatures_reply(n_cports=2)/ofp_phy_cport()/sup_wave_port_bandwidth(num_lmda=4)/ofp_phy_cport()/sup_wave_port_bandwidth(num_lmda=5)

    #d.show()
    #a.n_cports=2
    #a.OFPC_TABLE_STATS=100
    #a.OFPST_T_OTN = 1
    #a.SUPP_SW_GRAN = 129
    #print a.SUPP_SW_GRAN
    buffer = {}
    dpid = 8

    e = of.ofp_header(type =6)/of.ofp_features_reply(datapath_id=1)/of.ofp_phy_port(port_no=1)/of.ofp_phy_port(port_no=2)\
                              /of.ofp_phy_port(port_no=3)/of.ofp_phy_port(port_no=4)/of.ofp_phy_port(port_no=5)
    f = convert.of2ofc(e, buffer, dpid)
    f.show()
    p = str(f)
コード例 #24
0
ファイル: ofp_handler.py プロジェクト: opensource-cyy/Miracle
def features_request_handler(data, *arg):
    rmsg = of.ofp_header(data)
    #print ">>>OFPT_FEATURES_REQUEST"
    return of.ofp_header(type=5, xid=1)  #xid =0
コード例 #25
0
ファイル: trans_agent.py プロジェクト: muzixing/Ccontroller
    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)
コード例 #26
0
def ofc2of(msg, buffer, dpid):
    print "ofc->ofconverting"
    if isinstance(msg, ofc.ofp_header):
        if isinstance(msg.payload, ofc.ofp_cflow_mod):
            #self.buffer[(pkt_in_msg.in_port, id)] = [pkt_in_msg.buffer_id, rmsg/pkt_in_msg/pkt_parsed]
            #basic structure: ofp_header()/ofp_cflow_mod()/ofp_connect_wildcards()/ofp_connect()/other_ofp_actions()
            #select info from connect (port info) and actions (just copy)
            #WDM: num_wave -> vlan id
            #OTN: supp_sw_otn_gran->different map function ; bitmap->calculate vlan id
            #ODU0 = 0, ODU1 = 1 ...
            
            # [port + id] --> [buffer_id + pkt_in_msg]
            if (msg.payload.payload.payload.in_port, msg.xid, dpid) not in buffer:
                #Do not change the match and wildcards.
                print "not in the buffer"
                flow_mod = of.ofp_flow_wildcards(OFPFW_NW_TOS=1,
                                                  OFPFW_DL_VLAN_PCP=1,
                                                  OFPFW_NW_DST_MASK=0,
                                                  OFPFW_NW_SRC_MASK=0,
                                                  OFPFW_TP_DST=1,
                                                  OFPFW_TP_SRC=1,
                                                  OFPFW_NW_PROTO=1,
                                                  OFPFW_DL_TYPE=1,
                                                  OFPFW_DL_VLAN=1,
                                                  OFPFW_IN_PORT=1,
                                                  OFPFW_DL_DST=1,
                                                  OFPFW_DL_SRC=1)\
                           /of.ofp_match(in_port=msg.payload.payload.payload.in_port)\
                           /of.ofp_flow_mod(cookie=0,
                                            command=0,
                                            idle_timeout=0,
                                            hard_timeout=0,
                                            out_port=msg.payload.payload.payload.payload.port,
                                            buffer_id=0xffff,
                                            flags=1)
                port = msg.payload.payload.payload.payload.port
                if msg.payload.payload.payload.nport_out:
                    odu = sup2odu[msg.payload.payload.payload.supp_sw_otn_gran_out]
                    vid =  ofc2of_dict_odu[odu](msg.payload.payload.payload.nport_out)
                    print "vid", vid
                    flow_mod_msg = of.ofp_header(type=14,length=104,xid=msg.xid)/flow_mod/of.ofp_action_header(type=0)/of.ofp_action_output(type=0, port=port, len=8)/of.ofp_action_header(type =1)/of.ofp_action_vlan_vid(vlan_vid = vid)
                    return flow_mod_msg 
                elif msg.payload.payload.payload.wport_out:
                    vid =  ofc2of_dict_wave(msg.payload.payload.payload.num_wave_out)
                    print "vid", vid
                    flow_mod_msg = of.ofp_header(type=14,length=104,xid=msg.xid)/flow_mod/of.ofp_action_header(type=0)/of.ofp_action_output(type=0, port=port, len=8)/of.ofp_action_header(type =1)/of.ofp_action_vlan_vid(vlan_vid = vid)
                    return flow_mod_msg 
                else:
                    flow_mod_msg = of.ofp_header(type=14,length=88,xid=msg.xid)/flow_mod/of.ofp_action_header(type=0)/of.ofp_action_output(type=0, port=port, len=8)
                    return flow_mod_msg 
###############################################use the packet_in to send the flow##############################################
            else:
                buffer_id, pkt = buffer[(msg.payload.payload.payload.in_port, msg.xid, dpid)]
                del buffer[(msg.payload.payload.payload.in_port, msg.xid, dpid)]
                pkt_parsed = pkt.payload.payload

#####################################################pkt_parsed is a Ethernet packet############################################
            
                if isinstance(pkt_parsed.payload, of.IP) or isinstance(pkt_parsed.payload.payload, of.IP):
########################################################TCP OR UDP OR SCTP(L4)##################################################
                    if isinstance(pkt_parsed.payload.payload, of.TCP) or isinstance(pkt_parsed.payload.payload, of.UDP) or isinstance(pkt_parsed.payload.payload, of.SCTP) :
                        print "it is TCP or UDP or SCTP packet"
                        if  pkt_parsed.type ==0x8100:
                            print "pkt_parsed.payload.vlan",pkt_parsed.payload.vlan
                            flow_mod =of.ofp_flow_wildcards(OFPFW_NW_TOS=1,
                                                              OFPFW_DL_VLAN_PCP=1,
                                                              OFPFW_NW_DST_MASK=0,
                                                              OFPFW_NW_SRC_MASK=0,
                                                              OFPFW_TP_DST=1,
                                                              OFPFW_TP_SRC=1,
                                                              OFPFW_NW_PROTO=1,
                                                              OFPFW_DL_TYPE=1,
                                                              OFPFW_DL_VLAN=1,
                                                              OFPFW_IN_PORT=1,
                                                              OFPFW_DL_DST=1,
                                                              OFPFW_DL_SRC=1)\
                                       /of.ofp_match(in_port=msg.payload.payload.payload.in_port,
                                                     dl_src=pkt_parsed.src,
                                                     dl_dst=pkt_parsed.dst,
                                                     dl_type=pkt_parsed.type,
                                                     dl_vlan=pkt_parsed.payload.vlan,
                                                     nw_tos=pkt_parsed.payload.tos,
                                                     nw_proto=pkt_parsed.payload.proto,
                                                     nw_src=pkt_parsed.payload.src,
                                                     nw_dst=pkt_parsed.payload.dst,
                                                     tp_src = pkt_parsed.payload.payload.sport,
                                                     tp_dst = pkt_parsed.payload.payload.dport)\
                                       /of.ofp_flow_mod(cookie=0,
                                                        command=0,
                                                        idle_timeout=10,
                                                        hard_timeout=30,
                                                        out_port=msg.payload.payload.payload.payload.port,
                                                        buffer_id=buffer_id,
                                                        flags=1)
                            port = msg.payload.payload.payload.payload.port
                            if msg.payload.payload.payload.nport_out:
                                odu = sup2odu[msg.payload.payload.payload.supp_sw_otn_gran_out]
                                vid =  ofc2of_dict_odu[odu](msg.payload.payload.payload.nport_out)
                                print "vid", vid
                                flow_mod_msg = of.ofp_header(type=14,length=104,xid=pkt.xid)/flow_mod/of.ofp_action_header(type=0)/of.ofp_action_output(type=0, port=port, len=8)/of.ofp_action_header(type =1)/of.ofp_action_vlan_vid(vlan_vid = vid)
                                return flow_mod_msg
                            elif msg.payload.payload.payload.wport_out:
                                vid =  ofc2of_dict_wave(msg.payload.payload.payload.num_wave_out)
                                print "vid", vid
                                flow_mod_msg = of.ofp_header(type=14,length=104,xid=pkt.xid)/flow_mod/of.ofp_action_header(type=0)/of.ofp_action_output(type=0, port=port, len=8)/of.ofp_action_header(type =1)/of.ofp_action_vlan_vid(vlan_vid = vid)
                                return flow_mod_msg
                            else:
                                flow_mod_msg = of.ofp_header(type=14,length=88,xid=pkt.xid)/flow_mod/of.ofp_action_header(type=0)/of.ofp_action_output(type=0, port=port, len=8)
                                return flow_mod_msg
                        else:                                           
                            flow_mod = of.ofp_flow_wildcards(OFPFW_NW_TOS=1,
                                                              OFPFW_DL_VLAN_PCP=1,
                                                              OFPFW_NW_DST_MASK=0,
                                                              OFPFW_NW_SRC_MASK=0,
                                                              OFPFW_TP_DST=1,
                                                              OFPFW_TP_SRC=1,
                                                              OFPFW_NW_PROTO=1,
                                                              OFPFW_DL_TYPE=1,
                                                              OFPFW_DL_VLAN=1,
                                                              OFPFW_IN_PORT=1,
                                                              OFPFW_DL_DST=1,
                                                              OFPFW_DL_SRC=1)\
                                       /of.ofp_match(in_port=msg.payload.payload.payload.in_port,
                                                     dl_src=pkt_parsed.src,
                                                     dl_dst=pkt_parsed.dst,
                                                     dl_type=pkt_parsed.type,
                                                     nw_tos=pkt_parsed.payload.tos,
                                                     nw_proto=pkt_parsed.payload.proto,
                                                     nw_src=pkt_parsed.payload.src,
                                                     nw_dst=pkt_parsed.payload.dst,
                                                     tp_src = pkt_parsed.payload.payload.sport,
                                                     tp_dst = pkt_parsed.payload.payload.dport)\
                                       /of.ofp_flow_mod(cookie=0,
                                                        command=0,
                                                        idle_timeout=10,
                                                        hard_timeout=30,
                                                        out_port=msg.payload.payload.payload.payload.port,
                                                        buffer_id=buffer_id,
                                                        flags=1)
                            port = msg.payload.payload.payload.payload.port
                            if msg.payload.payload.payload.nport_out:
                                odu = sup2odu[msg.payload.payload.payload.supp_sw_otn_gran_out]
                                vid =  ofc2of_dict_odu[odu](msg.payload.payload.payload.nport_out)
                                print "vid", vid
                                flow_mod_msg = of.ofp_header(type=14,length=104,xid=pkt.xid)/flow_mod/of.ofp_action_header(type=0)/of.ofp_action_output(type=0, port=port, len=8)/of.ofp_action_header(type =1)/of.ofp_action_vlan_vid(vlan_vid = vid)
                                return flow_mod_msg                                
                            elif msg.payload.payload.payload.wport_out:
                                vid =  ofc2of_dict_wave(msg.payload.payload.payload.num_wave_out)
                                print "vid", vid
                                flow_mod_msg = of.ofp_header(type=14,length=104,xid=pkt.xid)/flow_mod/of.ofp_action_header(type=0)/of.ofp_action_output(type=0, port=port, len=8)/of.ofp_action_header(type =1)/of.ofp_action_vlan_vid(vlan_vid = vid)
                                return flow_mod_msg
                            else:
                                flow_mod_msg = of.ofp_header(type=14,length=88,xid=pkt.xid)/flow_mod/of.ofp_action_header(type=0)/of.ofp_action_output(type=0, port=port, len=8)
                                return flow_mod_msg
#######################################################IP(L3)##########################################################################
                    else:
                        if  pkt_parsed.type ==0x8100:
                            print "it is just a IP packet "
                            print "pkt_parsed.payload.vlan",pkt_parsed.payload.vlan
                            flow_mod = of.ofp_flow_wildcards(OFPFW_NW_TOS=1,
                                                              OFPFW_DL_VLAN_PCP=1,
                                                              OFPFW_NW_DST_MASK=0,
                                                              OFPFW_NW_SRC_MASK=0,
                                                              OFPFW_TP_DST=1,
                                                              OFPFW_TP_SRC=1,
                                                              OFPFW_NW_PROTO=1,
                                                              OFPFW_DL_TYPE=1,
                                                              OFPFW_DL_VLAN=1,
                                                              OFPFW_IN_PORT=1,
                                                              OFPFW_DL_DST=1,
                                                              OFPFW_DL_SRC=1)\
                                       /of.ofp_match(in_port=msg.payload.payload.payload.in_port,
                                                     dl_src=pkt_parsed.src,
                                                     dl_dst=pkt_parsed.dst,
                                                     dl_type=pkt_parsed.type,
                                                     dl_vlan=pkt_parsed.payload.vlan,
                                                     nw_tos=pkt_parsed.payload.tos,
                                                     nw_proto=pkt_parsed.payload.proto,
                                                     nw_src=pkt_parsed.payload.src,
                                                     nw_dst=pkt_parsed.payload.dst,
                                                     tp_src = 0,
                                                     tp_dst = 0)\
                                       /of.ofp_flow_mod(cookie=0,
                                                        command=0,
                                                        idle_timeout=10,
                                                        hard_timeout=30,
                                                        out_port=msg.payload.payload.payload.payload.port,
                                                        buffer_id=buffer_id,
                                                        flags=1)
                            port = msg.payload.payload.payload.payload.port
                            if msg.payload.payload.payload.nport_out:
                                odu = sup2odu[msg.payload.payload.payload.supp_sw_otn_gran_out]
                                vid =  ofc2of_dict_odu[odu](msg.payload.payload.payload.nport_out)
                                print "vid", vid
                                flow_mod_msg = of.ofp_header(type=14,length=104,xid=pkt.xid)/flow_mod/of.ofp_action_header(type=0)/of.ofp_action_output(type=0, port=port, len=8)/of.ofp_action_header(type =1)/of.ofp_action_vlan_vid(vlan_vid = vid)
                                return flow_mod_msg
                            elif msg.payload.payload.payload.wport_out:
                                vid =  ofc2of_dict_wave(msg.payload.payload.payload.num_wave_out)
                                print "vid", vid
                                flow_mod_msg = of.ofp_header(type=14,length=104,xid=pkt.xid)/flow_mod/of.ofp_action_header(type=0)/of.ofp_action_output(type=0, port=port, len=8)/of.ofp_action_header(type =1)/of.ofp_action_vlan_vid(vlan_vid = vid)
                                return flow_mod_msg
                            else:
                                flow_mod_msg = of.ofp_header(type=14,length=88,xid=pkt.xid)/flow_mod/of.ofp_action_header(type=0)/of.ofp_action_output(type=0, port=port, len=8)
                                return flow_mod_msg
                        else:                                           
                            flow_mod = of.ofp_flow_wildcards(OFPFW_NW_TOS=1,
                                                              OFPFW_DL_VLAN_PCP=1,
                                                              OFPFW_NW_DST_MASK=0,
                                                              OFPFW_NW_SRC_MASK=0,
                                                              OFPFW_TP_DST=1,
                                                              OFPFW_TP_SRC=1,
                                                              OFPFW_NW_PROTO=1,
                                                              OFPFW_DL_TYPE=1,
                                                              OFPFW_DL_VLAN=1,
                                                              OFPFW_IN_PORT=0,
                                                              OFPFW_DL_DST=1,
                                                              OFPFW_DL_SRC=1)\
                                       /of.ofp_match(in_port=msg.payload.payload.payload.in_port,
                                                     dl_src=pkt_parsed.src,
                                                     dl_dst=pkt_parsed.dst,
                                                     dl_type=pkt_parsed.type,
                                                     nw_tos=pkt_parsed.payload.tos,
                                                     nw_proto=pkt_parsed.payload.proto,
                                                     nw_src=pkt_parsed.payload.src,
                                                     nw_dst=pkt_parsed.payload.dst,
                                                     tp_src = 0,
                                                     tp_dst = 0)\
                                       /of.ofp_flow_mod(cookie=0,
                                                        command=0,
                                                        idle_timeout=10,
                                                        hard_timeout=30,
                                                        out_port=msg.payload.payload.payload.payload.port,
                                                        buffer_id=buffer_id,
                                                        flags=1)
                            port = msg.payload.payload.payload.payload.port
                            if msg.payload.payload.payload.nport_out:
                                odu = sup2odu[msg.payload.payload.payload.supp_sw_otn_gran_out]
                                vid =  ofc2of_dict_odu[odu](msg.payload.payload.payload.nport_out)
                                print "vid", vid
                                flow_mod_msg = of.ofp_header(type=14,length=104,xid=pkt.xid)/flow_mod/of.ofp_action_header(type=0)/of.ofp_action_output(type=0, port=port, len=8)/of.ofp_action_header(type =1)/of.ofp_action_vlan_vid(vlan_vid = vid)
                                return flow_mod_msg
                            elif msg.payload.payload.payload.wport_out:
                                vid =  ofc2of_dict_wave(msg.payload.payload.payload.num_wave_out)
                                print "vid", vid
                                flow_mod_msg = of.ofp_header(type=14,length=104,xid=pkt.xid)/flow_mod/of.ofp_action_header(type=0)/of.ofp_action_output(type=0, port=port, len=8)/of.ofp_action_header(type =1)/of.ofp_action_vlan_vid(vlan_vid = vid)
                                return flow_mod_msg
                            else:
                                flow_mod_msg = of.ofp_header(type=14,length=88,xid=pkt.xid)/flow_mod/of.ofp_action_header(type=0)/of.ofp_action_output(type=0, port=port, len=8)
                                return flow_mod_msg