Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[sai-gen] Add counter id generation support for counters. #503

Merged
merged 2 commits into from
Jan 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 35 additions & 20 deletions dash-pipeline/SAI/sai_api_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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 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")
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']:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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 = []
Expand Down