From b3b4b0db7fde0bf338a9bf31464225c92931ad40 Mon Sep 17 00:00:00 2001 From: r12f Date: Tue, 9 Jan 2024 00:15:19 +0000 Subject: [PATCH 1/2] Add counter id generation support for counters. --- dash-pipeline/SAI/sai_api_gen.py | 56 ++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/dash-pipeline/SAI/sai_api_gen.py b/dash-pipeline/SAI/sai_api_gen.py index 69c5dc955..3fb883a65 100755 --- a/dash-pipeline/SAI/sai_api_gen.py +++ b/dash-pipeline/SAI/sai_api_gen.py @@ -501,6 +501,11 @@ def link_ip_is_v6_vars(vars: List['SAIAPITableAttribute']) -> List['SAIAPITableA # Delete all vars with *_is_v6 in their names. return [v for v in vars if '_is_v6' not in v.name] + def set_sai_type(self, sai_type_info: SAITypeInfo) -> None: + self.type = sai_type_info.name + self.field = sai_type_info.sai_attribute_value_field + if self.default == None: + self.default = sai_type_info.default @sai_parser_from_p4rt class SAICounter(SAIAPITableAttribute): @@ -536,9 +541,20 @@ def parse_p4rt(self, p4rt_counter: Dict[str, Any], var_ref_graph: P4VarRefGraph) print("Parsing counter: " + self.name) self.__parse_sai_counter_annotation(p4rt_counter) - counter_storage_type = SAITypeSolver.get_object_sai_type(self.bitwidth) - self.type = counter_storage_type.name - self.field = counter_storage_type.sai_attribute_value_field + # If this counter is marked as SAI attributes, then we need to generate dedicated SAI attributes for this counter. + # In this case, the type needs to be created based on the size. + if self.as_attr: + counter_storage_type = SAITypeSolver.get_object_sai_type(self.bitwidth) + + # Otherwise, this counter should be linked to a SAI counter using a object it. + # In this case, the type needs to be sai_object_id_t. + else: + counter_storage_type = SAITypeSolver.get_sai_type("sai_object_id_t") + self.name = f"{self.name}_counter_id" + self.isreadonly = "false" + self.object_name = "counter" + + self.set_sai_type(counter_storage_type) counter_unit = str(p4rt_counter['spec']['unit']).lower() if counter_unit in ['bytes', 'packets', 'both']: @@ -586,16 +602,22 @@ def __parse_sai_counter_annotation(self, p4rt_counter: Dict[str, Any]) -> None: raise ValueError("Unknown attr annotation " + kv['key']) def generate_final_counter_on_type(self) -> 'Iterator[SAICounter]': - if self.counter_type != 'both': - self.name = f"{self.name}_{self.counter_type}_counter" + # If this counter is not used as an attribute, then we need to treat it as a counter object id. + if not self.as_attr: yield self + + # Otherwise, we need to generate dedicated SAI attributes for this counter. else: - packets_counter = copy.deepcopy(self) - packets_counter.name = f"{packets_counter.name}_packets_counter" - yield packets_counter + if self.counter_type != 'both': + self.name = f"{self.name}_{self.counter_type}_counter" + yield self + else: + packets_counter = copy.deepcopy(self) + packets_counter.name = f"{packets_counter.name}_packets_counter" + yield packets_counter - self.name = f"{self.name}_bytes_counter" - yield self + self.name = f"{self.name}_bytes_counter" + yield self @sai_parser_from_p4rt @@ -645,11 +667,8 @@ def parse_p4rt(self, p4rt_table_key: Dict[str, Any]) -> None: sai_type_info = SAITypeSolver.get_sai_type(self.type) else: sai_type_info = SAITypeSolver.get_match_key_sai_type(self.match_type, self.bitwidth) - self.type = sai_type_info.name - self.field = sai_type_info.sai_attribute_value_field - if self.default == None: - self.default = sai_type_info.default + self.set_sai_type(sai_type_info) return @@ -725,11 +744,8 @@ def parse_p4rt(self, p4rt_table_action_param: Dict[str, Any]) -> None: sai_type_info = SAITypeSolver.get_sai_type(self.type) else: sai_type_info = SAITypeSolver.get_object_sai_type(self.bitwidth) - self.type = sai_type_info.name - self.field = sai_type_info.sai_attribute_value_field - if self.default == None: - self.default = sai_type_info.default + self.set_sai_type(sai_type_info) return @@ -934,8 +950,7 @@ def __build_sai_attributes_after_parsing(self): sai_attributes_by_order.setdefault(action_param.order, []).append(action_param) for counter in self.counters: - if counter.as_attr: - sai_attributes_by_order.setdefault(counter.order, []).append(counter) + sai_attributes_by_order.setdefault(counter.order, []).append(counter) # Merge all attributes into a single list. self.sai_attributes = [] @@ -998,6 +1013,7 @@ def __parse_sai_counters_from_p4rt(self, p4rt_value: Dict[str, Any], var_ref_gra for p4rt_counter in all_p4rt_counters: counter = SAICounter.from_p4rt(p4rt_counter, var_ref_graph) self.sai_counters.extend(counter.generate_final_counter_on_type()) + print(self.sai_counters) def __parse_sai_apis_from_p4rt(self, program: Dict[str, Any], ignore_tables: List[str]) -> None: # Group all counters by action name. From c003af66dc21bbae3e1752a997e644c3890f40ae Mon Sep 17 00:00:00 2001 From: r12f Date: Wed, 10 Jan 2024 02:17:01 +0000 Subject: [PATCH 2/2] addressing comments. --- dash-pipeline/SAI/sai_api_gen.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/dash-pipeline/SAI/sai_api_gen.py b/dash-pipeline/SAI/sai_api_gen.py index 3fb883a65..3ce783aa5 100755 --- a/dash-pipeline/SAI/sai_api_gen.py +++ b/dash-pipeline/SAI/sai_api_gen.py @@ -546,7 +546,7 @@ def parse_p4rt(self, p4rt_counter: Dict[str, Any], var_ref_graph: P4VarRefGraph) if self.as_attr: counter_storage_type = SAITypeSolver.get_object_sai_type(self.bitwidth) - # Otherwise, this counter should be linked to a SAI counter using a object it. + # Otherwise, this counter should be linked to a SAI counter using an object ID. # In this case, the type needs to be sai_object_id_t. else: counter_storage_type = SAITypeSolver.get_sai_type("sai_object_id_t") @@ -1013,7 +1013,6 @@ def __parse_sai_counters_from_p4rt(self, p4rt_value: Dict[str, Any], var_ref_gra for p4rt_counter in all_p4rt_counters: counter = SAICounter.from_p4rt(p4rt_counter, var_ref_graph) self.sai_counters.extend(counter.generate_final_counter_on_type()) - print(self.sai_counters) def __parse_sai_apis_from_p4rt(self, program: Dict[str, Any], ignore_tables: List[str]) -> None: # Group all counters by action name.