def randomize(self): specializations = 0 scope.start_local() while True: self.type = randomizer.getRandom(self.probabilities) if self.type == 0: self.opt_annotations = opt_annotations() self.opt_annotations.randomize() self.non_type_name = non_type_name() self.non_type_name.randomize() self.opt_type_parameters = opt_type_parameters() self.opt_type_parameters.randomize() elif self.type == 1: self.opt_annotations = opt_annotations() self.opt_annotations.randomize() self.function_prototype = function_prototype() self.function_prototype.randomize() if not self.filter(): break if self.type == 0: if self.opt_type_parameters.type_parameter_list is not None: for parameter in self.opt_type_parameters.type_parameter_list.parameter_list: scope.insert_type(parameter.generate_code(), "template", parameter) specializations += 1 self.method_prototypes = method_prototypes() self.method_prototypes.randomize() scope.stop_local() scope.insert_type(self.non_type_name.generate_code(), "extern", self, specializations)
def randomize(self): scope.start_local() while True: self.opt_annotations = opt_annotations() self.opt_annotations.randomize() self.name = name() self.name.randomize() self.struct_field_list = struct_field_list(fromObj=self) self.struct_field_list.randomize() if not self.filter(): break scope.stop_local() scope.insert_type(self.name.generate_code(), "struct", self)
def generate_bmv2_structs(self): headers_t = struct_type_declaration(name="headers_t") _struct_field_list = struct_field_list(fromObj=headers_t) headers_t.struct_field_list = _struct_field_list metadata_t = struct_type_declaration(name="metadata_t") _struct_field_list2 = struct_field_list(fromObj=metadata_t) scope.start_local() _struct_field_list2.randomize() scope.stop_local() metadata_t.struct_field_list = _struct_field_list2 structs = (headers_t, metadata_t) code = "" for struct in structs: code += struct.generate_code() return code
def randomize(self): self.parser_type_declaration = parser_type_declaration() self.parser_type_declaration.randomize() self.opt_constructor_parameters = opt_constructor_parameters() self.opt_constructor_parameters.randomize() scope.start_local() if self.parser_type_declaration.parameter_list.non_empty_parameter_list is not None: for _parameter in self.parser_type_declaration.parameter_list.non_empty_parameter_list.parameter_list: scope.insert_parameter(_parameter.name.generate_code(), _parameter.type_ref.get_ref_type(), _parameter) self.parser_local_elements = parser_local_elements() self.parser_local_elements.randomize() self.parser_states = parser_states() self.parser_states.randomize() scope.stop_local()
def generate(self): scope.start_local() code = "" code += self.output_seed_comment() code += self.generate_netfpga_includes() code += self.generate_netfpga_structs() code += self.generate_random_structs() code += self.generate_random_headers() code += self.generate_random_externs() code += self.generate_extern_variables() code += self.generate_netfpga_parser() code += self.generate_random_parsers() code += self.generate_netfpga_controls() code += self.generate_random_controls() code += self.generate_netfpga_init() scope.stop_local() return code
def generate_netfpga_parser(self): available_structs = scope.get_available_types(only_type="struct") self.struct_name = available_structs.keys()[randomizer.randint( 0, len(available_structs) - 1)] scope.start_local() _parameter = parameter(type_ref=prefixed_type( value={self.struct_name: available_structs[self.struct_name]})) scope.insert_parameter("user_metadata", "struct", _parameter) _parser_states = parser_states() _parser_states.randomize() _parser_local_elements = parser_local_elements() _parser_local_elements.randomize() code = "parser TopParser(packet_in b,\nout Parsed_packet p,\nout " + self.struct_name + " user_metadata,\nout digest_data_t digest_data,\ninout sume_metadata_t sume_metadata) {\n" + _parser_local_elements.generate_code( ) + "\n" + _parser_states.generate_code() + "\n}\n\n" scope.stop_local() return code
def generate_bmv2_parser(self): available_structs = scope.get_available_types(only_type="struct") self.struct_name = available_structs.keys()[randomizer.randint( 0, len(available_structs) - 1)] scope.start_local() _parameter = parameter(type_ref=prefixed_type( value={self.struct_name: available_structs[self.struct_name]})) scope.insert_parameter("meta", "struct", _parameter) _parser_states = parser_states() _parser_states.randomize() _parser_local_elements = parser_local_elements() _parser_local_elements.randomize() code = "parser ParserImpl(packet_in packet,\nout headers_t hdr,\ninout " + self.struct_name + " meta,\ninout standard_metadata_t standard_metadata) {\n" + _parser_local_elements.generate_code( ) + "\n" + _parser_states.generate_code() + "\n}\n\n" scope.stop_local() return code
def generate(self): scope.start_local() code = "" code += self.output_seed_comment() code += self.generate_ebpf_includes() code += self.generate_ebpf_structs() code += self.generate_random_structs() code += self.generate_random_headers() code += self.generate_random_externs() code += self.generate_extern_variables() code += self.generate_ebpf_parser() #TODO:continue implement parsers and controls #code += self.generate_random_parsers() code += self.generate_ebpf_controls() self.generate_random_controls() code += self.generate_ebpf_init() scope.stop_local() return code
def randomize(self): common.usedRandomize() self.control_type_declaration = control_type_declaration() self.control_type_declaration.randomize() scope.start_local() if self.control_type_declaration.parameter_list.non_empty_parameter_list is not None: for _parameter in self.control_type_declaration.parameter_list.non_empty_parameter_list.parameter_list: scope.insert_parameter(_parameter.name.generate_code(), _parameter.type_ref.get_ref_type(), _parameter) self.opt_constructor_parameters = opt_constructor_parameters() self.opt_constructor_parameters.randomize() scope.start_local() self.control_local_declarations = control_declaration_creation() self.control_local_declarations.randomize() scope.stop_local() self.control_body = control_body() self.control_body.randomize() scope.stop_local()
def generate_netfpga_controls(self): available_structs = scope.get_available_types(only_type="struct") scope.start_local() _parameter = parameter(type_ref=prefixed_type( value={self.struct_name: available_structs[self.struct_name]})) scope.insert_parameter("user_metadata", "struct", _parameter) scope.start_local() _control_local_declarations = control_declaration_creation() _control_local_declarations.randomize() available_tables = scope.get_available_tables() table = available_tables.keys()[randomizer.randint( 0, len(available_tables) - 1)] scope.stop_local() code = "control TopPipe(inout Parsed_packet headers,\ninout " + self.struct_name + " user_metadata,\ninout digest_data_t digest_data,\ninout sume_metadata_t sume_metadata) {\n" + _control_local_declarations.generate_code( ) + "\napply {\n" + table + ".apply();\n}\n}\n\n" scope.stop_local() code += "control TopDeparser(packet_out b, in Parsed_packet p, in " + self.struct_name + " user_metadata,\ninout digest_data_t digest_data,\ninout sume_metadata_t sume_metadata) {\napply {\n}\n}\n\n" return code
def generate_netfpga_structs(self): parsed_packet = struct_type_declaration(name="Parsed_packet") _struct_field_list = struct_field_list(fromObj=parsed_packet) _struct_field_list.randomize() parsed_packet.struct_field_list = _struct_field_list digest_data_t = struct_type_declaration(name="digest_data_t") _struct_field_list2 = struct_field_list(fromObj=digest_data_t) _struct_field_list2.randomize() digest_data_t.struct_field_list = _struct_field_list2 user_metadata_t = struct_type_declaration(name="user_metadata_t") _struct_field_list3 = struct_field_list(fromObj=user_metadata_t) scope.start_local() _struct_field_list3.randomize() scope.stop_local() user_metadata_t.struct_field_list = _struct_field_list3 structs = (parsed_packet, digest_data_t, user_metadata_t) code = "" for struct in structs: code += struct.generate_code() return code
def generate_bmv2_controls(self): available_structs = scope.get_available_types(only_type="struct") scope.start_local() _parameter = parameter(type_ref=prefixed_type( value={self.struct_name: available_structs[self.struct_name]})) scope.insert_parameter("meta", "struct", _parameter) scope.start_local() _control_local_declarations = control_declaration_creation() _control_local_declarations.randomize() available_tables = scope.get_available_tables() table = available_tables.keys()[randomizer.randint( 0, len(available_tables) - 1)] scope.stop_local() code = "control IngressImpl(inout headers_t hdr,\ninout " + self.struct_name + " meta,\ninout standard_metadata_t standard_metadata) {\n" + _control_local_declarations.generate_code( ) + "\napply {\n" + table + ".apply();\n}\n}\n\n" scope.stop_local() code += "control EgressImpl(inout headers_t hdr,\ninout " + self.struct_name + " meta,\ninout standard_metadata_t standard_metadata) {\napply {\n\n}\n}\n\n" code += "control VerifyChecksumImpl(inout headers_t hdr, inout " + self.struct_name + " meta) {\napply {\n\n}\n}\n\n" code += "control ComputeChecksumImpl(inout headers_t hdr, inout " + self.struct_name + " meta) {\napply {\n}\n}\n\n" code += "control DeparserImpl(packet_out packet, in headers_t hdr) {\napply {\n}\n}\n\n" return code