def action_generate(parent, field_to_mod, mod_field_vals): """ Create an action to modify the field indicated in field_to_mod @param parent Must implement, assertTrue @param field_to_mod The field to modify as a string name @param mod_field_vals Hash of values to use for modified values """ act = None if field_to_mod in ['pktlen']: return None if field_to_mod == 'dl_dst': act = action.action_set_dl_dst() act.dl_addr = parse.parse_mac(mod_field_vals['dl_dst']) elif field_to_mod == 'dl_src': act = action.action_set_dl_src() act.dl_addr = parse.parse_mac(mod_field_vals['dl_src']) elif field_to_mod == 'dl_vlan_enable': if not mod_field_vals['dl_vlan_enable']: # Strip VLAN tag act = action.action_strip_vlan() # Add VLAN tag is handled by dl_vlan field # Will return None in this case elif field_to_mod == 'dl_vlan': act = action.action_set_vlan_vid() act.vlan_vid = mod_field_vals['dl_vlan'] elif field_to_mod == 'dl_vlan_pcp': act = action.action_set_vlan_pcp() act.vlan_pcp = mod_field_vals['dl_vlan_pcp'] elif field_to_mod == 'ip_src': act = action.action_set_nw_src() act.nw_addr = parse.parse_ip(mod_field_vals['ip_src']) elif field_to_mod == 'ip_dst': act = action.action_set_nw_dst() act.nw_addr = parse.parse_ip(mod_field_vals['ip_dst']) elif field_to_mod == 'ip_tos': act = action.action_set_nw_tos() act.nw_tos = mod_field_vals['ip_tos'] elif field_to_mod == 'tcp_sport': act = action.action_set_tp_src() act.tp_port = mod_field_vals['tcp_sport'] elif field_to_mod == 'tcp_dport': act = action.action_set_tp_dst() act.tp_port = mod_field_vals['tcp_dport'] elif field_to_mod == 'udp_sport': act = action.action_set_tp_src() act.tp_port = mod_field_vals['udp_sport'] elif field_to_mod == 'udp_dport': act = action.action_set_tp_dst() act.tp_port = mod_field_vals['udp_dport'] else: parent.assertTrue(0, "Unknown field to modify: " + str(field_to_mod)) return act
def action_generate(parent, field_to_mod, mod_field_vals): """ Create an action to modify the field indicated in field_to_mod @param parent Must implement, assertTrue @param field_to_mod The field to modify as a string name @param mod_field_vals Hash of values to use for modified values """ act = None if field_to_mod in ['pktlen']: return None if field_to_mod == 'dl_dst': act = action.action_set_dl_dst() act.dl_addr = parse.parse_mac(mod_field_vals['dl_dst']) elif field_to_mod == 'dl_src': act = action.action_set_dl_src() act.dl_addr = parse.parse_mac(mod_field_vals['dl_src']) elif field_to_mod == 'dl_vlan_enable': if not mod_field_vals['dl_vlan_enable']: # Strip VLAN tag act = action.action_strip_vlan() # Add VLAN tag is handled by dl_vlan field # Will return None in this case elif field_to_mod == 'dl_vlan': act = action.action_set_vlan_vid() act.vlan_vid = mod_field_vals['dl_vlan'] elif field_to_mod == 'dl_vlan_pcp': act = action.action_set_vlan_pcp() act.vlan_pcp = mod_field_vals['dl_vlan_pcp'] elif field_to_mod == 'ip_src': act = action.action_set_nw_src() act.nw_addr = parse.parse_ip(mod_field_vals['ip_src']) elif field_to_mod == 'ip_dst': act = action.action_set_nw_dst() act.nw_addr = parse.parse_ip(mod_field_vals['ip_dst']) elif field_to_mod == 'ip_tos': act = action.action_set_nw_tos() act.nw_tos = mod_field_vals['ip_tos'] elif field_to_mod == 'tcp_sport': act = action.action_set_tp_src() act.tp_port = mod_field_vals['tcp_sport'] elif field_to_mod == 'tcp_dport': act = action.action_set_tp_dst() act.tp_port = mod_field_vals['tcp_dport'] else: parent.assertTrue(0, "Unknown field to modify: " + str(field_to_mod)) return act
def runTest(self): table_id = testutils.EX_ACL_TABLE port_in = exact_port_map.keys()[0] egr_port = exact_port_map.keys()[1] #"clear swtich;" testutils.delete_all_flows(self.controller, self.logger) #make packet; pkt = testutils.simple_tcp_packet(dl_src='00:01:02:03:04:05', dl_dst='00:06:07:08:09:0a') exp_pkt = testutils.simple_tcp_packet(dl_src='00:01:02:03:04:05', dl_dst='aa:aa:aa:aa:aa:aa') # get match list match_ls = testutils.packet_to_exact_flow_match(pkt = pkt, table_id = table_id, ing_port = port_in) act = action.action_set_field() field = match.eth_dst(parse.parse_mac("aa:aa:aa:aa:aa:aa")) act.field.add(field) exact_table_output(self, table_id, match_ls, actions = [act], egr_port = egr_port) testutils.do_barrier(self.controller) "send a packet from port_in " self.dataplane.send(port_in, str(pkt)) "poll from the egr_port port" (port_rec, pkt_rec, _) = self.dataplane.poll(port_number=egr_port, timeout=1) self.assertTrue(pkt is not None,"rec none packets") self.assertEqual(str(exp_pkt), str(pkt_rec), 'retruned pkt not equal to the original pkt')
def get_oxm(field_obj): """ Returns an oxm and an arg-dict for updating an arg-list to simple_tcp_packet """ if field_obj.field == "OFPXMT_OFB_VLAN_VID": return (ofp.oxm.vlan_vid(field_obj.testval), {"vlan_vid": field_obj.testval, "dl_vlan_enable": True}) elif field_obj.field == "OFPXMT_OFB_ETH_DST": return (ofp.oxm.eth_dst(parse_mac(field_obj.testval)), {"eth_dst": field_obj.testval})
def runTest(self): of_ports = ipv6_port_map.keys() of_ports.sort() ing_port = of_ports[0] egr_port = of_ports[3] # Remove all entries Add entry match all rc = testutils.delete_all_flows(self.controller, ipv6_logger) self.assertEqual(rc, 0, "Failed to delete all flows") # Add entry match request = message.flow_mod() request.match.type = ofp.OFPMT_OXM port = match.in_port(of_ports[0]) eth_type = match.eth_type(IPV6_ETHERTYPE) eth_dst = match.eth_dst(parse.parse_mac("00:01:02:03:04:05")) ipv6_src = match.ipv6_src( ipaddr.IPv6Address('fe80::2420:52ff:fe8f:5189')) request.match_fields.tlvs.append(port) request.match_fields.tlvs.append(eth_type) request.match_fields.tlvs.append(eth_dst) request.match_fields.tlvs.append(ipv6_src) act = action.action_output() act.port = of_ports[3] inst = instruction.instruction_apply_actions() inst.actions.add(act) request.instructions.add(inst) request.buffer_id = 0xffffffff request.priority = 1000 ipv6_logger.debug("Adding flow ") rv = self.controller.message_send(request) self.assertTrue(rv != -1, "Failed to insert test flow") #Send packet pkt = testutils.simple_ipv6_packet(dl_dst='00:01:02:03:04:05', ip_src='fe80::2420:52ff:fe8f:5189') ipv6_logger.info("Sending IPv6 packet to " + str(ing_port)) ipv6_logger.debug("Data: " + str(pkt).encode('hex')) self.dataplane.send(ing_port, str(pkt)) #Receive packet exp_pkt = testutils.simple_ipv6_packet() testutils.receive_pkt_verify(self, egr_port, exp_pkt) #Remove flows rc = testutils.delete_all_flows(self.controller, ipv6_logger) self.assertEqual(rc, 0, "Failed to delete all flows")
def runTest(self): of_ports = ipv6_port_map.keys() of_ports.sort() ing_port = of_ports[0] egr_port = of_ports[3] # Remove all entries Add entry match all rc = testutils.delete_all_flows(self.controller, ipv6_logger) self.assertEqual(rc, 0, "Failed to delete all flows") # Add entry match request = message.flow_mod() request.match.type = ofp.OFPMT_OXM port = match.in_port(of_ports[0]) eth_type = match.eth_type(IPV6_ETHERTYPE) eth_dst = match.eth_dst(parse.parse_mac("00:01:02:03:04:05")) ipv6_src = match.ipv6_src(ipaddr.IPv6Address('fe80::2420:52ff:fe8f:5189')) request.match_fields.tlvs.append(port) request.match_fields.tlvs.append(eth_type) request.match_fields.tlvs.append(eth_dst) request.match_fields.tlvs.append(ipv6_src) act = action.action_output() act.port = of_ports[3] inst = instruction.instruction_apply_actions() inst.actions.add(act) request.instructions.add(inst) request.buffer_id = 0xffffffff request.priority = 1000 ipv6_logger.debug("Adding flow ") rv = self.controller.message_send(request) self.assertTrue(rv != -1, "Failed to insert test flow") #Send packet pkt = testutils.simple_ipv6_packet(dl_dst='00:01:02:03:04:05',ip_src='fe80::2420:52ff:fe8f:5189') ipv6_logger.info("Sending IPv6 packet to " + str(ing_port)) ipv6_logger.debug("Data: " + str(pkt).encode('hex')) self.dataplane.send(ing_port, str(pkt)) #Receive packet exp_pkt = testutils.simple_ipv6_packet() testutils.receive_pkt_verify(self, egr_port, exp_pkt) #Remove flows rc = testutils.delete_all_flows(self.controller, ipv6_logger) self.assertEqual(rc, 0, "Failed to delete all flows")
def runTest(self): """ Add four flow entries: First Table; Match IP Src A; goto Second Table Second Table; Match IP Src A; send to 1, goto Third Table Third Table; Match IP Src A; clear action, goto Fourth Table Fourth Table; Match IP Src A; send to 2 Then send in 2 packets: IP A, TCP C; expect out port 1 IP A, TCP B; expect out port 1 @param self object instance @param EX_ACL_TABLE first table @param WC_ACL_TABLE second table @param WC_SERV_TABLE third table @param EX_L2_TABLE fourth table """ of_ports = testutils.clear_switch(self, pa_port_map.keys(), pa_logger) # Set up first match #write_goto(self, testutils.EX_ACL_TABLE, testutils.WC_ACL_TABLE) act = action.action_set_field() field = match.eth_src(parse.parse_mac("aa:aa:aa:aa:aa:aa")) act.field.add(field) testutils.write_goto_action(self, testutils.WC_ACL_TABLE, testutils.WC_SERV_TABLE, act=act) # Set up second match testutils.write_goto_output(self, testutils.WC_SERV_TABLE, testutils.EX_L3_TABLE, of_ports[0], of_ports[2]) # Set up third match, "Clear Action" inst = instruction.instruction_clear_actions() testutils.write_goto(self, testutils.EX_L3_TABLE, testutils.WC_L3_TABLE, of_ports[2], add_inst=inst) # Set up fourth match testutils.write_output(self, testutils.WC_L3_TABLE, of_ports[1]) #write_output(self, testutils.EX_L2_TABLE, 4) # Generate a packet matching flow 1, 2, and 3; rcv on port[1] testutils.reply_check_dp(self, tcp_sport=1234, ing_port=of_ports[2], egr_port=of_ports[1])
def runTest(self): """ Add four flow entries: First Table; Match IP Src A; goto Second Table Second Table; Match IP Src A; send to 1, goto Third Table Third Table; Match IP Src A; clear action, goto Fourth Table Fourth Table; Match IP Src A; send to 2 Then send in 2 packets: IP A, TCP C; expect out port 1 IP A, TCP B; expect out port 1 @param self object instance @param EX_ACL_TABLE first table @param WC_ACL_TABLE second table @param WC_SERV_TABLE third table @param EX_L2_TABLE fourth table """ of_ports = testutils.clear_switch(self, pa_port_map.keys(), pa_logger) # Set up first match #write_goto(self, testutils.EX_ACL_TABLE, testutils.WC_ACL_TABLE) act = action.action_set_field() field = match.eth_src(parse.parse_mac("aa:aa:aa:aa:aa:aa")) act.field.add(field) testutils.write_goto_action(self, testutils.WC_ACL_TABLE, testutils.WC_SERV_TABLE ,act = act) # Set up second match testutils.write_goto_output(self, testutils.WC_SERV_TABLE, testutils.EX_L3_TABLE, of_ports[0], of_ports[2]) # Set up third match, "Clear Action" inst = instruction.instruction_clear_actions() testutils.write_goto(self, testutils.EX_L3_TABLE, testutils.WC_L3_TABLE, of_ports[2], add_inst=inst) # Set up fourth match testutils.write_output(self, testutils.WC_L3_TABLE, of_ports[1]) #write_output(self, testutils.EX_L2_TABLE, 4) # Generate a packet matching flow 1, 2, and 3; rcv on port[1] testutils.reply_check_dp(self, tcp_sport=1234, ing_port = of_ports[2], egr_port = of_ports[1])
def create_action(**kwargs): a = kwargs.get('action') if a == ofp.OFPAT_OUTPUT: act = action.action_output() act.port = kwargs.get('port', 1) return act if a == ofp.OFPAT_GROUP: act = action.action_group() act.group_id = kwargs.get('group_id', 0) return act if a == ofp.OFPAT_SET_TP_SRC: act = action.action_set_tp_src() act.tp_port = kwargs.get('tp_port', 0) return act if a == ofp.OFPAT_SET_DL_DST: act = action.action_set_dl_dst() dl_tmp = kwargs.get('dl_dst', 1) act.dl_addr = parse.parse_mac(dl_tmp) return act if a == ofp.OFPAT_SET_NW_DST: act = action.action_set_nw_dst() act.nw_addr = parse.parse_ip(kwargs.get('nw_dst', '192.168.3.1' )) return act
def create_action(**kwargs): a = kwargs.get('action') if a == ofp.OFPAT_OUTPUT: act = action.action_output() act.port = kwargs.get('port', 1) return act if a == ofp.OFPAT_GROUP: act = action.action_group() act.group_id = kwargs.get('group_id', 0) return act if a == ofp.OFPAT_SET_TP_SRC: act = action.action_set_tp_src() act.tp_port = kwargs.get('tp_port', 0) return act if a == ofp.OFPAT_SET_DL_DST: act = action.action_set_dl_dst() dl_tmp = kwargs.get('dl_dst', 1) act.dl_addr = parse.parse_mac(dl_tmp) return act if a == ofp.OFPAT_SET_NW_DST: act = action.action_set_nw_dst() act.nw_addr = parse.parse_ip(kwargs.get('nw_dst', '192.168.3.1')) return act
def runTest(self): table_id = testutils.EX_ACL_TABLE port_in = exact_port_map.keys()[0] egr_port = exact_port_map.keys()[1] #"clear swtich;" testutils.delete_all_flows(self.controller, self.logger) #make packet; pkt = testutils.simple_tcp_packet(dl_src='00:01:02:03:04:05', dl_dst='00:06:07:08:09:0a') exp_pkt = testutils.simple_tcp_packet(dl_src='00:01:02:03:04:05', dl_dst='aa:aa:aa:aa:aa:aa') # get match list match_ls = testutils.packet_to_exact_flow_match(pkt=pkt, table_id=table_id, ing_port=port_in) act = action.action_set_field() field = match.eth_dst(parse.parse_mac("aa:aa:aa:aa:aa:aa")) act.field.add(field) exact_table_output(self, table_id, match_ls, actions=[act], egr_port=egr_port) testutils.do_barrier(self.controller) "send a packet from port_in " self.dataplane.send(port_in, str(pkt)) "poll from the egr_port port" (port_rec, pkt_rec, _) = self.dataplane.poll(port_number=egr_port, timeout=1) self.assertTrue(pkt is not None, "rec none packets") self.assertEqual(str(exp_pkt), str(pkt_rec), 'retruned pkt not equal to the original pkt')
def parse_mac_words(mac): a = parse_mac(mac) return (a[0] << 8) | a[1], (a[2] << 24) | (a[3] << 16) | (a[4] << 8) | a[5]
def test_set_field(testcase, table_id, ports, property_type): msg = "Checking if table {0} supports OFPAT_SET_FIELD." logging.info(msg.format(table_id)) # Check if table_id supports OFPAT_SET_FIELD. supporting_tables = tables_supporting_action(testcase, ofp.const.OFPAT_SET_FIELD, property_type) logging.info("Tables {0} support the OFPAT_OFPAT_SET_FIELD.".format(supporting_tables)) if table_id not in supporting_tables: logging.info("Table does not support OFPAT_OFPAT_SET_FIELD type. Skipping check.") return False # If a property is not a *_MISS property set the priority to one. priority = 0 if property_type == ofp.const.OFPTFPT_WRITE_ACTIONS: priority = 1 if property_type == ofp.const.OFPTFPT_APPLY_ACTIONS: priority = 1 if property_type==ofp.const.OFPTFPT_WRITE_ACTIONS_MISS: property_type=ofp.const.OFPTFPT_WRITE_SETFIELD_MISS elif property_type==ofp.const.OFPTFPT_WRITE_ACTIONS: property_type=ofp.const.OFPTFPT_WRITE_SETFIELD elif property_type==ofp.const.OFPTFPT_APPLY_ACTIONS: property_type=ofp.const.OFPTFPT_APPLY_SETFIELD elif property_type==ofp.const.OFPTFPT_APPLY_ACTIONS_MISS: property_type=ofp.const.OFPTFPT_APPLY_SETFIELD_MISS oxm_ids = get_oxm_ids(testcase, table_id, property_type) if oxm_ids is []: logging.warn("DUT supports set-field action, but no oxm types.") return False # Generate an expected_packet and associated oxm tlv packet = simple_tcp_packet() expected_packet = None oxm_under_test = None ipv4 = "127.127.127.127" mac = "aa:bb:cc:dd:ee:ff" oxm_packets = [ (ofp.oxm.ipv4_src(value=parse_ip(ipv4)), simple_tcp_packet(ip_src=ipv4)), (ofp.oxm.ipv4_dst(value=parse_ip(ipv4)), simple_tcp_packet(ip_dst=ipv4)), (ofp.oxm.eth_src(value=parse_mac(mac)), simple_tcp_packet(eth_src=mac)), (ofp.oxm.eth_dst(value=parse_mac(mac)), simple_tcp_packet(eth_src=mac)) ] for oxm_pkt in oxm_packets: if oxm_pkt[0].type_len in oxm_ids: oxm_under_test, expected_packet = oxm_pkt if oxm_under_test is None: logging.warn("DUT doesn't support common oxm set-field types.") return False actions = [ ofp.action.set_field(field=oxm_under_test), ofp.action.output(port=ports[1], max_len=128) ] instructions = [] if property_type == ofp.const.OFPTFPT_WRITE_ACTIONS: instructions.append(ofp.instruction.write_actions(actions=actions)) elif property_type == ofp.const.OFPTFPT_WRITE_ACTIONS_MISS: instructions.append(ofp.instruction.write_actions(actions=actions)) else: instructions.append(ofp.instruction.apply_actions(actions=actions)) req = ofp.message.flow_add(table_id=table_id, instructions=instructions, buffer_id=ofp.const.OFP_NO_BUFFER, priority=priority) testcase.controller.message_send(req) err, _ = testcase.controller.poll(exp_msg=ofp.const.OFPT_ERROR) testcase.assertIsNone(err, "Unexpected ofp_error_msg received: %s." % err) logging.info("Installed ofp_flow_mod table entry.") packet = simple_tcp_packet() testcase.dataplane.send(ports[0], str(packet)) verify_packet(testcase, str(expected_packet), ports[1]) return True