Skip to content

Commit

Permalink
Make counter to be adaptive to its type.
Browse files Browse the repository at this point in the history
  • Loading branch information
r12f committed Jan 8, 2024
1 parent b497789 commit 77a14f0
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions dash-pipeline/SAI/sai_api_gen.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#!/usr/bin/env python3

from typing import Iterator


try:
import os
import json
Expand Down Expand Up @@ -537,8 +540,8 @@ def parse_p4rt(self, p4rt_counter: Dict[str, Any], var_ref_graph: P4VarRefGraph)
self.type = counter_storage_type.name
self.field = counter_storage_type.sai_attribute_value_field

counter_unit: str = p4rt_counter['spec']['unit']
if counter_unit in ['BYTES', 'PACKETS', 'BOTH']:
counter_unit = str(p4rt_counter['spec']['unit']).lower()
if counter_unit in ['bytes', 'packets', 'both']:
self.counter_type = counter_unit.lower()
else:
raise ValueError(f'Unknown counter unit: {counter_unit}')
Expand Down Expand Up @@ -582,6 +585,18 @@ def __parse_sai_counter_annotation(self, p4rt_counter: Dict[str, Any]) -> None:
else:
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"
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


@sai_parser_from_p4rt
class SAIAPITableKey(SAIAPITableAttribute):
Expand Down Expand Up @@ -982,7 +997,7 @@ def __parse_sai_counters_from_p4rt(self, p4rt_value: Dict[str, Any], var_ref_gra
all_p4rt_counters = p4rt_value[COUNTERS_TAG]
for p4rt_counter in all_p4rt_counters:
counter = SAICounter.from_p4rt(p4rt_counter, var_ref_graph)
self.sai_counters.append(counter)
self.sai_counters.extend(counter.generate_final_counter_on_type())

def __parse_sai_apis_from_p4rt(self, program: Dict[str, Any], ignore_tables: List[str]) -> None:
# Group all counters by action name.
Expand Down

0 comments on commit 77a14f0

Please sign in to comment.