Skip to content

Commit

Permalink
[sai-gen] Remove all name-based type guessing heuristics (#483)
Browse files Browse the repository at this point in the history
* Remove ip family guessing.

* Remove mac guessing.

* Remove IPv4 address guessing.

* Remove 32-bits ID type guessing.

* remove type guessing for uppercase object name

* Remove 16-bits id type guessing.

* clean up script.

* doc update.
  • Loading branch information
r12f authored Dec 13, 2023
1 parent 276b52a commit 801cecc
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 47 deletions.
22 changes: 6 additions & 16 deletions dash-pipeline/SAI/sai_api_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,28 +123,18 @@ def get_sai_type(sai_type):
return SAITypeSolver.sai_type_info_registry[sai_type]

@staticmethod
def get_object_sai_type(object_size, object_name):
object_name = object_name.lower()

def get_object_sai_type(object_size):
sai_type_name = ""

if object_size == 1:
sai_type_name = 'bool'
elif object_size <= 8:
sai_type_name = 'sai_uint8_t'
elif object_size == 16 and ('_id' in object_name):
sai_type_name = 'sai_object_id_t'
elif object_size <= 16:
sai_type_name = 'sai_uint16_t'
elif object_size == 32 and ('ip_addr_family' in object_name):
sai_type_name = 'sai_ip_addr_family_t'
elif object_size == 32 and ('ip' in object_name):
sai_type_name = 'sai_ip_address_t'
elif object_size == 32 and ('_id' in object_name):
sai_type_name = 'sai_object_id_t'
elif object_size <= 32:
sai_type_name = 'sai_uint32_t'
elif object_size == 48 and ('mac' in object_name):
elif object_size == 48:
sai_type_name = 'sai_mac_t'
elif object_size <= 64:
sai_type_name = 'sai_uint64_t'
Expand All @@ -156,9 +146,9 @@ def get_object_sai_type(object_size, object_name):
return SAITypeSolver.get_sai_type(sai_type_name)

@staticmethod
def get_match_key_sai_type(match_type, key_size, key_name):
def get_match_key_sai_type(match_type, key_size):
if match_type == 'exact' or match_type == 'optional' or match_type == 'ternary':
return SAITypeSolver.get_object_sai_type(key_size, key_name)
return SAITypeSolver.get_object_sai_type(key_size)
elif match_type == 'lpm':
return SAITypeSolver.__get_lpm_match_key_sai_type(key_size)
elif match_type == 'list':
Expand Down Expand Up @@ -416,7 +406,7 @@ def parse_p4rt(self, p4rt_table_key):
if self.type != 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.name)
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.field_func_prefix
Expand Down Expand Up @@ -494,7 +484,7 @@ def parse_p4rt(self, p4rt_table_action_param):
if self.type != None:
sai_type_info = SAITypeSolver.get_sai_type(self.type)
else:
sai_type_info = SAITypeSolver.get_object_sai_type(self.bitwidth, self.name)
sai_type_info = SAITypeSolver.get_object_sai_type(self.bitwidth)
self.type = sai_type_info.name

self.field = sai_type_info.field_func_prefix
Expand Down
12 changes: 11 additions & 1 deletion dash-pipeline/bmv2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,14 @@ Available tags are:
- `isobject`: When set to "true", a top level objects in SAI that attached to switch will be generated. Otherwise, a new type of entry will be generated, if nothing else helps us to determine this table is an object table.
- `ignored`: When set to "true", we skip this table in SAI API generation.

For more details, please check the SAI API generation script: [sai_api_gen.py](../SAI/sai_api_gen.py).
For more details, please check the SAI API generation script: [sai_api_gen.py](../SAI/sai_api_gen.py).

## SAI header and implementation generation

The SAI header and implementation files are generated by the [sai_api_gen.py](../SAI/sai_api_gen.py) script. The script takes the P4 runtime json as input and generates the SAI header and implementation files in the [SAI](../SAI) directory.

### SAI type solving

Since the p4 runtime json does not contain the type information, [sai_api_gen.py](../SAI/sai_api_gen.py) has a very basic heuristic picking up the SAI type for the values. The heuristic is intended to stay simple and purely based on the size of the values and match type.

If any more dedicated type is needed, please use the `@SaiVal` annotation to specify the type. Please see the [P4 annotations for SAI code generation](#p4-annotations-for-sai-code-generation) section for more details.
15 changes: 8 additions & 7 deletions dash-pipeline/bmv2/dash_outbound.p4
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ control outbound(inout headers_t hdr,
meta.meter_policy_en = meter_policy_en;
meta.route_meter_class = meter_class;
}
action route_vnet(bit<16> dst_vnet_id,
action route_vnet(@SaiVal[type="sai_object_id_t"] bit<16> dst_vnet_id,
bit<1> meter_policy_en,
bit<16> meter_class) {
meta.dst_vnet_id = dst_vnet_id;
Expand All @@ -23,6 +23,7 @@ control outbound(inout headers_t hdr,

action route_vnet_direct(bit<16> dst_vnet_id,
bit<1> overlay_ip_is_v6,
@SaiVal[type="sai_ip_address_t"]
IPv4ORv6Address overlay_ip,
bit<1> meter_policy_en,
bit<16> meter_class) {
Expand Down Expand Up @@ -95,7 +96,7 @@ control outbound(inout headers_t hdr,
@SaiTable[name = "outbound_routing", api = "dash_outbound_routing"]
table routing {
key = {
meta.eni_id : exact @SaiVal[name = "eni_id"];
meta.eni_id : exact @SaiVal[name = "eni_id", type="sai_object_id_t"];
meta.is_overlay_ip_v6 : exact @SaiVal[name = "destination_is_v6"];
meta.dst_ip_addr : lpm @SaiVal[name = "destination"];
}
Expand All @@ -119,7 +120,7 @@ control outbound(inout headers_t hdr,
#endif // TARGET_DPDK_PNA
}

action set_tunnel(IPv4Address underlay_dip,
action set_tunnel(@SaiVal[type="sai_ip_address_t"] IPv4Address underlay_dip,
@SaiVal[type="sai_dash_encapsulation_t"] dash_encapsulation_t dash_encapsulation,
bit<16> meter_class,
bit<1> meter_class_override) {
Expand All @@ -129,7 +130,7 @@ control outbound(inout headers_t hdr,
meta.encap_data.dash_encapsulation = dash_encapsulation;
}

action set_tunnel_mapping(IPv4Address underlay_dip,
action set_tunnel_mapping(@SaiVal[type="sai_ip_address_t"] IPv4Address underlay_dip,
EthernetAddress overlay_dmac,
bit<1> use_dst_vnet_vni,
bit<16> meter_class,
Expand All @@ -144,7 +145,7 @@ control outbound(inout headers_t hdr,
meter_class_override);
}

action set_private_link_mapping(IPv4Address underlay_dip,
action set_private_link_mapping(@SaiVal[type="sai_ip_address_t"] IPv4Address underlay_dip,
IPv6Address overlay_sip,
IPv6Address overlay_dip,
@SaiVal[type="sai_dash_encapsulation_t"] dash_encapsulation_t dash_encapsulation,
Expand Down Expand Up @@ -179,7 +180,7 @@ control outbound(inout headers_t hdr,
table ca_to_pa {
key = {
/* Flow for express route */
meta.dst_vnet_id: exact @SaiVal[name = "dst_vnet_id"];
meta.dst_vnet_id: exact @SaiVal[name = "dst_vnet_id", type="sai_object_id_t"];
meta.is_lkup_dst_ip_v6 : exact @SaiVal[name = "dip_is_v6"];
meta.lkup_dst_ip_addr : exact @SaiVal[name = "dip"];
}
Expand Down Expand Up @@ -208,7 +209,7 @@ control outbound(inout headers_t hdr,
@SaiTable[name = "vnet", api = "dash_vnet"]
table vnet {
key = {
meta.vnet_id : exact @SaiVal[name = "vnet_id"];
meta.vnet_id : exact @SaiVal[name = "vnet_id", type="sai_object_id_t"];
}

actions = {
Expand Down
45 changes: 22 additions & 23 deletions dash-pipeline/bmv2/dash_pipeline.p4
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ control dash_ingress(
@SaiTable[name = "vip", api = "dash_vip"]
table vip {
key = {
hdr.ipv4.dst_addr : exact @SaiVal[name = "VIP"];
hdr.ipv4.dst_addr : exact @SaiVal[name = "VIP", type="sai_ip_address_t"];
}

actions = {
Expand Down Expand Up @@ -94,11 +94,11 @@ control dash_ingress(
}

#define ACL_GROUPS_PARAM(prefix) \
bit<16> ## prefix ##_stage1_dash_acl_group_id, \
bit<16> ## prefix ##_stage2_dash_acl_group_id, \
bit<16> ## prefix ##_stage3_dash_acl_group_id, \
bit<16> ## prefix ##_stage4_dash_acl_group_id, \
bit<16> ## prefix ##_stage5_dash_acl_group_id
@SaiVal[type="sai_object_id_t"] bit<16> ## prefix ##_stage1_dash_acl_group_id, \
@SaiVal[type="sai_object_id_t"] bit<16> ## prefix ##_stage2_dash_acl_group_id, \
@SaiVal[type="sai_object_id_t"] bit<16> ## prefix ##_stage3_dash_acl_group_id, \
@SaiVal[type="sai_object_id_t"] bit<16> ## prefix ##_stage4_dash_acl_group_id, \
@SaiVal[type="sai_object_id_t"] bit<16> ## prefix ##_stage5_dash_acl_group_id

#define ACL_GROUPS_COPY_TO_META(prefix) \
meta.stage1_dash_acl_group_id = ## prefix ##_stage1_dash_acl_group_id; \
Expand All @@ -111,15 +111,14 @@ control dash_ingress(
bit<32> pps,
bit<32> flows,
bit<1> admin_state,
IPv4Address vm_underlay_dip,
@SaiVal[type="sai_uint32_t"]
bit<24> vm_vni,
bit<16> vnet_id,
@SaiVal[type="sai_ip_address_t"] IPv4Address vm_underlay_dip,
@SaiVal[type="sai_uint32_t"] bit<24> vm_vni,
@SaiVal[type="sai_object_id_t"] bit<16> vnet_id,
IPv6Address pl_sip,
IPv6Address pl_sip_mask,
IPv4Address pl_underlay_sip,
bit<16> v4_meter_policy_id,
bit<16> v6_meter_policy_id,
@SaiVal[type="sai_ip_address_t"] IPv4Address pl_underlay_sip,
@SaiVal[type="sai_object_id_t"] bit<16> v4_meter_policy_id,
@SaiVal[type="sai_object_id_t"] bit<16> v6_meter_policy_id,
ACL_GROUPS_PARAM(inbound_v4),
ACL_GROUPS_PARAM(inbound_v6),
ACL_GROUPS_PARAM(outbound_v4),
Expand Down Expand Up @@ -157,7 +156,7 @@ control dash_ingress(
@SaiTable[name = "eni", api = "dash_eni", api_order=1]
table eni {
key = {
meta.eni_id : exact @SaiVal[name = "eni_id"];
meta.eni_id : exact @SaiVal[name = "eni_id", type="sai_object_id_t"];
}

actions = {
Expand Down Expand Up @@ -189,7 +188,7 @@ control dash_ingress(
@SaiTable[ignored = "true"]
table eni_meter {
key = {
meta.eni_id : exact @SaiVal[name = "eni_id"];
meta.eni_id : exact @SaiVal[name = "eni_id", type="sai_object_id_t"];
meta.direction : exact @SaiVal[name = "direction"];
meta.dropped : exact @SaiVal[name = "dropped"];
}
Expand All @@ -209,15 +208,15 @@ control dash_ingress(
action permit() {
}

action vxlan_decap_pa_validate(bit<16> src_vnet_id) {
action vxlan_decap_pa_validate(@SaiVal[type="sai_object_id_t"] bit<16> src_vnet_id) {
meta.vnet_id = src_vnet_id;
}

@SaiTable[name = "pa_validation", api = "dash_pa_validation"]
table pa_validation {
key = {
meta.vnet_id: exact @SaiVal[name = "vnet_id"];
hdr.ipv4.src_addr : exact @SaiVal[name = "sip"];
meta.vnet_id: exact @SaiVal[name = "vnet_id", type="sai_object_id_t"];
hdr.ipv4.src_addr : exact @SaiVal[name = "sip", type="sai_ip_address_t"];
}

actions = {
Expand All @@ -231,9 +230,9 @@ control dash_ingress(
@SaiTable[name = "inbound_routing", api = "dash_inbound_routing"]
table inbound_routing {
key = {
meta.eni_id: exact @SaiVal[name = "eni_id"];
meta.eni_id: exact @SaiVal[name = "eni_id", type="sai_object_id_t"];
hdr.vxlan.vni : exact @SaiVal[name = "VNI"];
hdr.ipv4.src_addr : ternary @SaiVal[name = "sip"];
hdr.ipv4.src_addr : ternary @SaiVal[name = "sip", type="sai_ip_address_t"];
}
actions = {
vxlan_decap(hdr);
Expand Down Expand Up @@ -274,7 +273,7 @@ control dash_ingress(
table meter_rule {
key = {
meta.meter_policy_id: exact @SaiVal[name = "meter_policy_id", type="sai_object_id_t", isresourcetype="true", objects="METER_POLICY"];
hdr.ipv4.dst_addr : ternary @SaiVal[name = "dip"];
hdr.ipv4.dst_addr : ternary @SaiVal[name = "dip", type="sai_ip_address_t"];
}

actions = {
Expand All @@ -301,7 +300,7 @@ control dash_ingress(
@SaiTable[name = "meter_bucket", api = "dash_meter", api_order = 0, isobject="true"]
table meter_bucket {
key = {
meta.eni_id: exact @SaiVal[name = "eni_id"];
meta.eni_id: exact @SaiVal[name = "eni_id", type="sai_object_id_t"];
meta.meter_class: exact @SaiVal[name = "meter_class"];
}
actions = {
Expand All @@ -311,7 +310,7 @@ control dash_ingress(
const default_action = NoAction();
}

action set_eni(bit<16> eni_id) {
action set_eni(@SaiVal[type="sai_object_id_t"] bit<16> eni_id) {
meta.eni_id = eni_id;
}

Expand Down

0 comments on commit 801cecc

Please sign in to comment.