def add_flow(self,parser,connection,cookie = 0,cookie_mask=0,table_id = 0, priority = 0,idle_time = 0,hard_time = 0,match = None,action = None,buffer_id=None): flowRequest = parser.ofp_flow_mod() flowRequest.table_id = table_id flowRequest.command = parser.OFPFC_ADD flowRequest.priority = priority flowRequest.buffer_id = parser.OFP_NO_BUFFER flowRequest.cookie = cookie flowRequest.cookie_mask = cookie_mask flowRequest.idle_timeout = idle_time flowRequest.hard_timeout = hard_time #flowRequest.out_port #flowRequest.out_group flowRequest.flags = parser.OFPFF_SEND_FLOW_REM flowRequest.match = match instruction = parser.ofp_instruction_actions(type=parser.OFPIT_APPLY_ACTIONS) instruction.actions.append(action) flowRequest.instructions.append(instruction) #log.debug('%r',flowRequest._tobytes()) #log.debug('%r',common.dump(flowRequest)) for m in of_proto.batch([flowRequest],connection,self): yield m for reply in self.openflow_reply: if reply['type'] == common.OFPET_BAD_REQUEST: log.debug(common.dump(self.openflow_reply))
def add_flow(self,parser,connection,proto,cookie = 0,cookie_mask=0,table_id = 0, priority = 0,idle_time = 0,hard_time = 0,match = None,action = None,buffer_id=None): flowRequest = parser.ofp_flow_mod() flowRequest.table_id = table_id flowRequest.command = parser.OFPFC_ADD flowRequest.priority = priority flowRequest.buffer_id = parser.OFP_NO_BUFFER flowRequest.cookie = cookie flowRequest.cookie_mask = cookie_mask flowRequest.idle_timeout = idle_time flowRequest.hard_timeout = hard_time #flowRequest.out_port #flowRequest.out_group flowRequest.flags = parser.OFPFF_SEND_FLOW_REM flowRequest.match = match instruction = parser.ofp_instruction_actions(type=parser.OFPIT_APPLY_ACTIONS) instruction.actions.append(action) flowRequest.instructions.append(instruction) #log.debug('%r',flowRequest._tobytes()) #log.debug('%r',common.dump(flowRequest)) for m in proto.batch([flowRequest],connection,self): yield m for reply in self.openflow_reply: if reply['type'] == common.OFPET_BAD_REQUEST: log.debug(common.dump(self.openflow_reply))
def packet_in_handler(self, event): datapathid = event.datapathid buffer_id = event.message.buffer_id ofpParser = event.connection.openflowdef ofp_proto = event.createby #log.debug('event message = %r',common.dump(event.message)) for oxm in event.message.match.oxm_fields: if oxm.header == ofpParser.OXM_OF_IN_PORT: port_str = ''.join('%d' % n for n in bytearray(oxm.value)) in_port = int(port_str) if datapathid not in self.mac_to_port: return ethernet = common.dump(ethernetPacket.create(event.message.data)) dstMac = ethernet['dstMac'] srcMac = ethernet['srcMac'] log.debug("pakcet in %r,%r, %r", srcMac, dstMac, in_port) self.mac_to_port[datapathid][srcMac] = in_port data = event.message.data if dstMac in self.mac_to_port[datapathid]: # packet out to mac_to_port[datapath][dstMac] # add an flow avoid next packet in output = self.mac_to_port[datapathid][dstMac] for m in self.packetout(ofpParser, event.connection, ofp_proto, in_port, output, buffer_id, data): yield m match = ofpParser.ofp_match_oxm() match.oxm_fields.append( ofpParser.create_oxm(ofpParser.OXM_OF_ETH_DST, EUI(dstMac).packed)) action = ofpParser.ofp_action_output(port=output) for m in self.add_flow(connection=event.connection, parser=ofpParser, proto=ofp_proto, table_id=0, priority=100, match=match, action=action): yield m else: # flood this packet output = ofpParser.OFPP_FLOOD for m in self.packetout(ofpParser, event.connection, ofp_proto, in_port, output, buffer_id, data): yield m
async def main(self): connected = OpenflowConnectionStateEvent.createMatcher() ev = await connected pprint(common.dump(ev.connection.openflow_featuresreply, tostr=True)) connection = ev.connection currdef = connection.openflowdef openflow_reply, _ = await of_proto.batch((currdef.nx_set_flow_format.new(format=currdef.NXFF_NXM),), connection, self) for msg in openflow_reply: pprint(common.dump(msg, tostr=True)) openflow_reply = await of_proto.querymultipart( currdef.ofp_stats_request.new( type = currdef.OFPST_DESC ), connection, self) for msg in openflow_reply: pprint(common.dump(msg, tostr=True)) req = currdef.ofp_flow_stats_request.new( table_id = currdef.OFPTT_ALL, out_port = currdef.OFPP_NONE, match = currdef.ofp_match.new(wildcards=currdef.OFPFW_ALL)) openflow_reply = await of_proto.querymultipart(req, connection, self) for msg in openflow_reply: pprint(common.dump(msg, dumpextra = True, typeinfo = common.DUMPTYPE_FLAT, tostr=True)) req = currdef.nx_flow_stats_request.new(table_id = currdef.OFPTT_ALL, out_port = currdef.OFPP_NONE) openflow_reply = await of_proto.querymultipart(req, connection, self) for msg in openflow_reply: pprint(common.dump(msg, dumpextra = True, typeinfo = common.DUMPTYPE_FLAT, tostr=True)) req = currdef.ofp_msg.new() req.header.type = currdef.OFPT_GET_CONFIG_REQUEST openflow_reply = await of_proto.querywithreply(req, connection, self) pprint(common.dump(openflow_reply, tostr=True)) await mgt_conn.shutdown(False)
def main(self): connected = OpenflowConnectionStateEvent.createMatcher() yield (connected,) pprint(common.dump(self.event.connection.openflow_featuresreply)) connection = self.event.connection currdef = connection.openflowdef for m in of_proto.batch((currdef.nx_set_flow_format.new(format=currdef.NXFF_NXM),), connection, self): yield m for msg in self.openflow_reply: pprint(common.dump(msg)) for m in of_proto.querymultipart(currdef.ofp_stats_request.new( type = currdef.OFPST_DESC), connection, self): yield m for msg in self.openflow_reply: pprint(common.dump(msg)) req = currdef.ofp_flow_stats_request.new( table_id = currdef.OFPTT_ALL, out_port = currdef.OFPP_NONE, match = currdef.ofp_match.new(wildcards=currdef.OFPFW_ALL)) for m in of_proto.querymultipart(req, connection, self): yield m for msg in self.openflow_reply: pprint(common.dump(msg, dumpextra = True, typeinfo = common.DUMPTYPE_FLAT)) req = currdef.nx_flow_stats_request.new(table_id = currdef.OFPTT_ALL, out_port = currdef.OFPP_NONE) for m in of_proto.querymultipart(req, connection, self): yield m for msg in self.openflow_reply: pprint(common.dump(msg, dumpextra = True, typeinfo = common.DUMPTYPE_FLAT)) req = currdef.ofp_msg.new() req.header.type = currdef.OFPT_GET_CONFIG_REQUEST for m in of_proto.querywithreply(req, connection, self): yield m pprint(common.dump(self.openflow_reply)) for m in mgt_conn.shutdown(False): yield m
def packet_in_handler(self,event): datapathid = event.datapathid buffer_id = event.message.buffer_id ofpParser = event.connection.openflowdef ofp_proto = event.createby #log.debug('event message = %r',common.dump(event.message)) for oxm in event.message.match.oxm_fields: if oxm.header == ofpParser.OXM_OF_IN_PORT: port_str = ''.join('%d' % n for n in bytearray(oxm.value)) in_port = int(port_str) if datapathid not in self.mac_to_port: return ethernet = common.dump(ethernetPacket.create(event.message.data)) dstMac = ethernet['dstMac'] srcMac = ethernet['srcMac'] log.debug("pakcet in %r,%r, %r",srcMac,dstMac,in_port) self.mac_to_port[datapathid][srcMac] = in_port data = event.message.data if dstMac in self.mac_to_port[datapathid]: # packet out to mac_to_port[datapath][dstMac] # add an flow avoid next packet in output = self.mac_to_port[datapathid][dstMac] for m in self.packetout(ofpParser,event.connection,ofp_proto,in_port,output,buffer_id,data): yield m match = ofpParser.ofp_match_oxm() match.oxm_fields.append(ofpParser.create_oxm(ofpParser.OXM_OF_ETH_DST,EUI(dstMac).packed)) action = ofpParser.ofp_action_output(port = output) for m in self.add_flow(connection=event.connection,parser=ofpParser,proto=ofp_proto, table_id = 0,priority = 100,match = match,action = action): yield m else: # flood this packet output = ofpParser.OFPP_FLOOD for m in self.packetout(ofpParser,event.connection,ofp_proto,in_port,output,buffer_id,data): yield m
def main(self): connected = OpenflowConnectionStateEvent.createMatcher() yield (connected, ) pprint(common.dump(self.event.connection.openflow_featuresreply)) connection = self.event.connection currdef = connection.openflowdef for m in of_proto.querymultipart( currdef.ofp_multipart_request.new(type=currdef.OFPMP_DESC), connection, self): yield m for msg in self.openflow_reply: pprint(common.dump(msg)) for m in of_proto.querymultipart( currdef.ofp_multipart_request.new( type=currdef.OFPMP_PORT_DESC), connection, self): yield m for msg in self.openflow_reply: pprint(common.dump(msg)) req = currdef.ofp_flow_stats_request.new( table_id=currdef.OFPTT_ALL, out_port=currdef.OFPP_ANY, out_group=currdef.OFPG_ANY, match=currdef.ofp_match_oxm.new()) for m in of_proto.querymultipart(req, connection, self): yield m for msg in self.openflow_reply: pprint( common.dump(msg, dumpextra=True, typeinfo=common.DUMPTYPE_FLAT)) req = currdef.ofp_msg.new() req.header.type = currdef.OFPT_GET_CONFIG_REQUEST for m in of_proto.querywithreply(req, connection, self): yield m pprint(common.dump(self.openflow_reply)) req = currdef.ofp_role_request.new(role=currdef.OFPCR_ROLE_NOCHANGE) for m in of_proto.querywithreply(req, connection, self): yield m pprint(common.dump(self.openflow_reply)) req = currdef.ofp_msg.new() req.header.type = currdef.OFPT_GET_ASYNC_REQUEST for m in of_proto.querywithreply(req, connection, self): yield m pprint(common.dump(self.openflow_reply)) for m in mgt_conn.shutdown(False): yield m
def packetout(self,parser,connection,proto,in_port,output,buffer_id,data): packetoutMessage = parser.ofp_packet_out() packetoutMessage.buffer_id = buffer_id packetoutMessage.in_port = in_port buffer_data = b'' if buffer_id == parser.OFP_NO_BUFFER: buffer_data = data packetoutMessage.data = buffer_data action = parser.ofp_action_output(port = output) packetoutMessage.actions.append(action) log.debug("packet to %r",output) for m in proto.batch([packetoutMessage],connection,self): yield m for reply in self.openflow_reply: #if reply['type'] == common.OFPET_BAD_REQUEST: log.debug(common.dump(self.openflow_reply))
def packetout(self,parser,connection,in_port,output,buffer_id,data): packetoutMessage = parser.ofp_packet_out() packetoutMessage.buffer_id = buffer_id packetoutMessage.in_port = in_port buffer_data = b'' if buffer_id == parser.OFP_NO_BUFFER: buffer_data = data packetoutMessage.data = buffer_data action = parser.ofp_action_output(port = output) packetoutMessage.actions.append(action) log.debug("packet to %r",output) for m in of_proto.batch([packetoutMessage],connection,self): yield m for reply in self.openflow_reply: #if reply['type'] == common.OFPET_BAD_REQUEST: log.debug(common.dump(self.openflow_reply))
def main(self): connected = OpenflowConnectionStateEvent.createMatcher() yield (connected,) pprint(common.dump(self.event.connection.openflow_featuresreply)) connection = self.event.connection currdef = connection.openflowdef for m in of_proto.querymultipart(currdef.ofp_multipart_request.new( type = currdef.OFPMP_DESC), connection, self): yield m for msg in self.openflow_reply: pprint(common.dump(msg)) for m in of_proto.querymultipart(currdef.ofp_multipart_request.new( type = currdef.OFPMP_PORT_DESC), connection, self): yield m for msg in self.openflow_reply: pprint(common.dump(msg)) req = currdef.ofp_flow_stats_request.new( table_id = currdef.OFPTT_ALL, out_port = currdef.OFPP_ANY, out_group = currdef.OFPG_ANY, match = currdef.ofp_match_oxm.new()) for m in of_proto.querymultipart(req, connection, self): yield m for msg in self.openflow_reply: pprint(common.dump(msg, dumpextra = True, typeinfo = common.DUMPTYPE_FLAT)) req = currdef.ofp_msg.new() req.header.type = currdef.OFPT_GET_CONFIG_REQUEST for m in of_proto.querywithreply(req, connection, self): yield m pprint(common.dump(self.openflow_reply)) req = currdef.ofp_role_request.new(role = currdef.OFPCR_ROLE_NOCHANGE) for m in of_proto.querywithreply(req, connection, self): yield m pprint(common.dump(self.openflow_reply)) req = currdef.ofp_msg.new() req.header.type = currdef.OFPT_GET_ASYNC_REQUEST for m in of_proto.querywithreply(req, connection, self): yield m pprint(common.dump(self.openflow_reply)) for m in mgt_conn.shutdown(False): yield m
async def main(self): connected = OpenflowConnectionStateEvent.createMatcher() ev = await connected pprint(common.dump(ev.connection.openflow_featuresreply, tostr=True)) connection = ev.connection currdef = connection.openflowdef openflow_reply = await of_proto.querymultipart( currdef.ofp_multipart_request.new(type=currdef.OFPMP_DESC), connection, self) for msg in openflow_reply: pprint(common.dump(msg, tostr=True)) openflow_reply = await of_proto.querymultipart( currdef.ofp_multipart_request.new(type=currdef.OFPMP_PORT_DESC), connection, self) for msg in openflow_reply: pprint(common.dump(msg, tostr=True)) req = currdef.ofp_flow_stats_request.new( table_id=currdef.OFPTT_ALL, out_port=currdef.OFPP_ANY, out_group=currdef.OFPG_ANY, match=currdef.ofp_match_oxm.new()) openflow_reply = await of_proto.querymultipart(req, connection, self) for msg in openflow_reply: pprint( common.dump(msg, dumpextra=True, typeinfo=common.DUMPTYPE_FLAT, tostr=True)) req = currdef.ofp_msg.new() req.header.type = currdef.OFPT_GET_CONFIG_REQUEST openflow_reply = await of_proto.querywithreply(req, connection, self) pprint(common.dump(openflow_reply, tostr=True)) req = currdef.ofp_role_request.new(role=currdef.OFPCR_ROLE_NOCHANGE) openflow_reply = await of_proto.querywithreply(req, connection, self) pprint(common.dump(openflow_reply, tostr=True)) req = currdef.ofp_msg.new() req.header.type = currdef.OFPT_GET_ASYNC_REQUEST openflow_reply = await of_proto.querywithreply(req, connection, self) pprint(common.dump(openflow_reply, tostr=True)) await mgt_conn.shutdown(False)
def main(self): connected = OpenflowConnectionStateEvent.createMatcher() yield (connected, ) pprint(common.dump(self.event.connection.openflow_featuresreply)) connection = self.event.connection currdef = connection.openflowdef for m in of_proto.batch( (currdef.nx_set_flow_format.new(format=currdef.NXFF_NXM), ), connection, self): yield m for msg in self.openflow_reply: pprint(common.dump(msg)) for m in of_proto.querymultipart( currdef.ofp_stats_request.new(type=currdef.OFPST_DESC), connection, self): yield m for msg in self.openflow_reply: pprint(common.dump(msg)) req = currdef.ofp_flow_stats_request.new( table_id=currdef.OFPTT_ALL, out_port=currdef.OFPP_NONE, match=currdef.ofp_match.new(wildcards=currdef.OFPFW_ALL)) for m in of_proto.querymultipart(req, connection, self): yield m for msg in self.openflow_reply: pprint( common.dump(msg, dumpextra=True, typeinfo=common.DUMPTYPE_FLAT)) req = currdef.nx_flow_stats_request.new(table_id=currdef.OFPTT_ALL, out_port=currdef.OFPP_NONE) for m in of_proto.querymultipart(req, connection, self): yield m for msg in self.openflow_reply: pprint( common.dump(msg, dumpextra=True, typeinfo=common.DUMPTYPE_FLAT)) req = currdef.ofp_msg.new() req.header.type = currdef.OFPT_GET_CONFIG_REQUEST for m in of_proto.querywithreply(req, connection, self): yield m pprint(common.dump(self.openflow_reply)) for m in mgt_conn.shutdown(False): yield m