def benchmark_modification(nb_headers, nb_fields, mod_type): """ This method generate the P4 program to benchmark packet modification :param nb_headers: the number of generic headers included in the program :type nb_headers: int :param nb_fields: the number of fields (16 bits) in each header :type tbl_size: int :param nb_fields: modification type ['add', 'rm', 'mod'] :type tbl_size: str :returns: bool -- True if there is no error """ out_dir = 'output' if not os.path.exists(out_dir): os.makedirs(out_dir) fwd_tbl = 'forward_table' program = add_headers_and_parsers(nb_headers, nb_fields) if mod_type == 'add': action_name = 'add_headers' program += benchmark_add_header_overhead(action_name, nb_headers) elif mod_type == 'rm': action_name = 'remove_headers' program += benchmark_remove_header_overhead(action_name, nb_headers) elif mod_type == 'mod': action_name = 'mod_headers' program += benchmark_modify_header_overhead(action_name, nb_headers) program += forward_table() table_name = 'test_tbl' match = 'ptp.reserved2 : exact;' program += add_table(table_name, match, '\t\t{0};'.format(action_name), 4) program += control(fwd_tbl, apply_table(table_name)) with open('%s/main.p4' % out_dir, 'w') as out: out.write(program) commands = add_default_rule(table_name, action_name) commands += cli_commands(fwd_tbl) with open('%s/commands.txt' % out_dir, 'w') as out: out.write(commands) copy_scripts(out_dir) get_packetmod_pcap(nb_headers, nb_fields, mod_type, out_dir) generate_pisces_command(nb_headers, out_dir, mod_type) return True
def benchmark_field_write(nb_operations, do_checksum=False): """ This method generate the P4 program to benchmark packet modification :param nb_operations: the number of Set-Field actions :type nb_operations: int :returns: bool -- True if there is no error """ out_dir = 'output' if not os.path.exists(out_dir): os.makedirs(out_dir) fwd_tbl = 'forward_table' nb_headers = 1 program = add_headers_and_parsers(nb_headers, nb_operations) program += nop_action_14() program += forward_table() action_name = 'mod_headers' program += write_to_custom_header(action_name, nb_operations) table_name = 'test_tbl' match = 'ptp.reserved2 : exact;' actions = '\t\t_nop;\n\t\t{0};'.format(action_name) program += add_table(table_name, match, actions, 4, 14) program += control(fwd_tbl, apply_table(table_name)) with open('%s/main.p4' % out_dir, 'w') as out: out.write(program) commands = add_default_rule(table_name, '_nop') # commands += add_rule(table_name, action_name, 319) commands += add_default_rule(table_name, action_name) commands += cli_commands(fwd_tbl) with open('%s/commands.txt' % out_dir, 'w') as out: out.write(commands) copy_scripts(out_dir) set_custom_field_pcap(nb_operations, out_dir, packet_size=256) generate_pisces_command(nb_operations, out_dir, do_checksum) return True