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): logging = get_logger() logging.info("Running Grp30No80 Flood bits Test") of_ports = config["port_map"].keys() of_ports.sort() for i in range(len(of_ports)): #Retrieve Port Configuration --- logging.info("Sending Features Request") (hw_addr, port_config, advert) = \ port_config_get(self.controller, of_ports[i]) #Modify Port Configuration logging.info("Setting OFPPC_NO_FLOOD bit to 0 on %s" % str(of_ports[i])) logging.info("Port config is set to: {0}".format( bin(port_config & ~ofp.OFPPC_NO_FLOOD))) if (i % 2): rv = port_config_set(self.controller, of_ports[i], port_config & ~ofp.OFPPC_NO_FLOOD, ofp.OFPPC_NO_FLOOD) else: rv = port_config_set(self.controller, of_ports[i], port_config | ofp.OFPPC_NO_FLOOD, ofp.OFPPC_NO_FLOOD) self.assertTrue(rv != -1, "Error sending port mod") self.assertEqual(do_barrier(self.controller), 0, "Barrier failed") #Sending a flow with output action on flood ports logging.info("Sending flow_mod message..") pkt = simple_tcp_packet() match = parse.packet_to_flow_match(pkt) self.assertTrue(match is not None, "Could not generate flow match from pkt") match.wildcards = ofp.OFPFW_ALL ^ ofp.OFPFW_NW_DST_MASK match.nw_dst = parse.parse_ip("244.0.0.1") flow_mod_msg = message.flow_mod() flow_mod_msg.match = match flow_mod_msg.command = ofp.OFPFC_ADD act = action.action_output() act.port = ofp.OFPP_FLOOD self.assertTrue(flow_mod_msg.actions.add(act), "Could not add action") rv = self.controller.message_send(flow_mod_msg.pack()) self.assertTrue(rv != -1, "Error installing flow mod") self.assertEqual(do_barrier(self.controller), 0, "Barrier failed") #Sending packet to check if flood ports respond pkt_check = simple_tcp_packet(ip_dst="244.0.0.1") self.dataplane.send(of_ports[0], str(pkt_check)) yes_ports = no_ports = set(of_ports) for i in range(len(of_ports)): if (i % 2): no_ports = no_ports.difference([of_ports[i]]) else: yes_ports = yes_ports.difference([of_ports[i]]) receive_pkt_check(self.dataplane, pkt_check, yes_ports, no_ports, self)
def make_match(dl_type=MT_TEST_DL_TYPE, ip_src=MT_TEST_IP): """ Make matching entry template """ match = ofp.ofp_match() match.length = ofp.OFPMT_STANDARD_LENGTH testutils.wildcard_all_set(match) match.wildcards -= ofp.OFPFW_DL_TYPE match.nw_src = parse.parse_ip(ip_src) match.nw_src_mask = 0 # Match nw_src match.dl_type = dl_type return match
def runTest(self): logging = get_logger() logging.info("Running Grp30No80 Flood bits Test") of_ports = config["port_map"].keys() of_ports.sort() for i in range (len(of_ports)): #Retrieve Port Configuration --- logging.info("Sending Features Request") (hw_addr, port_config, advert) = \ port_config_get(self.controller, of_ports[i]) #Modify Port Configuration logging.info("Setting OFPPC_NO_FLOOD bit to 0 on %s" %str(of_ports[i])) logging.info("Port config is set to: {0}".format(bin(port_config & ~ofp.OFPPC_NO_FLOOD))) if (i%2): rv = port_config_set(self.controller, of_ports[i], port_config & ~ofp.OFPPC_NO_FLOOD, ofp.OFPPC_NO_FLOOD) else: rv = port_config_set(self.controller, of_ports[i], port_config | ofp.OFPPC_NO_FLOOD, ofp.OFPPC_NO_FLOOD) self.assertTrue(rv != -1, "Error sending port mod") self.assertEqual(do_barrier(self.controller), 0, "Barrier failed") #Sending a flow with output action on flood ports logging.info("Sending flow_mod message..") pkt=simple_tcp_packet() match = parse.packet_to_flow_match(pkt) self.assertTrue(match is not None, "Could not generate flow match from pkt") match.wildcards = ofp.OFPFW_ALL ^ ofp.OFPFW_NW_DST_MASK match.nw_dst = parse.parse_ip("244.0.0.1") flow_mod_msg = message.flow_mod() flow_mod_msg.match = match flow_mod_msg.command = ofp.OFPFC_ADD act=action.action_output() act.port = ofp.OFPP_FLOOD self.assertTrue(flow_mod_msg.actions.add(act), "Could not add action") rv = self.controller.message_send(flow_mod_msg.pack()) self.assertTrue(rv != -1, "Error installing flow mod") self.assertEqual(do_barrier(self.controller), 0, "Barrier failed") #Sending packet to check if flood ports respond pkt_check=simple_tcp_packet(ip_dst="244.0.0.1") self.dataplane.send(of_ports[0], str(pkt_check)) yes_ports = no_ports = set(of_ports) for i in range(len(of_ports)): if (i%2): no_ports = no_ports.difference([of_ports[i]]) else: yes_ports = yes_ports.difference([of_ports[i]]) receive_pkt_check(self.dataplane,pkt_check,yes_ports,no_ports,self)
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): of_ports = pa_port_map.keys() of_ports.sort() self.assertTrue(len(of_ports) > 2, "Not enough ports for test") # Clear flow table rv = testutils.initialize_table_config(self.controller, pa_logger) self.assertEqual(rv, 0, "Failed to initialize table config") rv = testutils.delete_all_flows(self.controller, pa_logger) self.assertEqual(rv, 0, "Failed to delete all flows") # Set up first match match = ofp.ofp_match() match.length = ofp.OFPMT_STANDARD_LENGTH testutils.wildcard_all_set(match) match.wildcards -= ofp.OFPFW_DL_TYPE match.nw_src_mask = 0 # Match nw_src match.dl_type = 0x800 match.nw_src = parse.parse_ip("192.168.1.10") act = action.action_output() act.port = of_ports[0] request = message.flow_mod() request.match = match request.buffer_id = 0xffffffff request.table_id = 0 inst = instruction.instruction_write_actions() self.assertTrue(inst.actions.add(act), "Could not add action") self.assertTrue(request.instructions.add(inst), "Could not add inst1") inst = instruction.instruction_goto_table() inst.table_id = 1 self.assertTrue(request.instructions.add(inst), "Could not add inst2") pa_logger.info("Inserting flow 1") rv = self.controller.message_send(request) # pa_logger.debug(request.show()) self.assertTrue(rv != -1, "Error installing flow mod") # Set up second match match = ofp.ofp_match() match.length = ofp.OFPMT_STANDARD_LENGTH testutils.wildcard_all_set(match) match.wildcards -= ofp.OFPFW_DL_TYPE match.wildcards -= ofp.OFPFW_TP_SRC match.dl_type = 0x800 match.nw_proto = 6 # TCP match.tp_src = 80 act = action.action_output() act.port = of_ports[1] request = message.flow_mod() request.match = match request.buffer_id = 0xffffffff request.table_id = 1 inst = instruction.instruction_write_actions() self.assertTrue(inst.actions.add(act), "Could not add action") self.assertTrue(request.instructions.add(inst), "Could not add inst3") pa_logger.info("Inserting flow 2") # pa_logger.debug(request.show()) rv = self.controller.message_send(request) self.assertTrue(rv != -1, "Error installing flow mod") testutils.do_barrier(self.controller) # Generate a packet matching only flow 1; rcv on port[0] pkt = testutils.simple_tcp_packet(ip_src='192.168.1.10', tcp_sport=10) self.dataplane.send(of_ports[2], str(pkt)) (rcv_port, rcv_pkt, _) = self.dataplane.poll(timeout=5) self.assertTrue(rcv_pkt is not None, "Did not receive packet") pa_logger.debug("Packet len " + str(len(rcv_pkt)) + " in on " + str(rcv_port)) self.assertEqual(rcv_port, of_ports[0], "Unexpected receive port") # Generate a packet matching both flow 1 and flow 2; rcv on port[1] pkt = testutils.simple_tcp_packet(ip_src='192.168.1.10', tcp_sport=80) self.dataplane.send(of_ports[2], str(pkt)) (rcv_port, rcv_pkt, _) = self.dataplane.poll(timeout=5) self.assertTrue(rcv_pkt is not None, "Did not receive packet") pa_logger.debug("Packet len " + str(len(rcv_pkt)) + " in on " + str(rcv_port)) self.assertEqual(rcv_port, of_ports[1], "Unexpected receive port")
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