Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
Signed-off-by: Ze Gan <zegan@microsoft.com>
  • Loading branch information
Pterosaur committed Jun 4, 2024
1 parent 52c4dcf commit 822e90b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
30 changes: 23 additions & 7 deletions dash-pipeline/SAI/src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,27 @@ namespace dash

int leadingNonZeroBits(const sai_ip6_t& ipv6);

inline int getPrefixLength(const sai_ip_prefix_t &value)
{
switch(value.addr_family)
{
case SAI_IP_ADDR_FAMILY_IPV4:
// LPM entry match field prefix length calculation needs to be fixed to accomodate 128 bit size.
// So the 96 is added to the prefix length.
return leadingNonZeroBits(htonl(value.mask.ip4)) + 96;
case SAI_IP_ADDR_FAMILY_IPV6:
return leadingNonZeroBits(value.mask.ip6);
default:
assert(0 && "unrecognzed value.ipaddr.addr_family");
}
return 0;
}

inline int getPrefixLength(const sai_attribute_value_t &value)
{
return getPrefixLength(value.ipprefix);
}

template<typename T>
void ipPrefixSetVal(const sai_attribute_value_t &value, T &t, int bits = -1)
{
Expand All @@ -256,12 +277,7 @@ namespace dash
correctIpPrefix(&val, &value.mask.ip4, 4);

t->set_value(&val, 4);

val = htonl(value.mask.ip4);

// LPM entry match field prefix length calculation needs to be fixed to accomodate 128 bit size.
// So the 96 is added to the prefix length.
t->set_prefix_len(leadingNonZeroBits(val) + 96);
t->set_prefix_len(getPrefixLength(value));
}
break;

Expand All @@ -274,7 +290,7 @@ namespace dash
correctIpPrefix(ip, value.mask.ip6, 16);

t->set_value(ip, 16);
t->set_prefix_len(leadingNonZeroBits(value.mask.ip6));
t->set_prefix_len(getPrefixLength(value));
}
break;

Expand Down
24 changes: 24 additions & 0 deletions dash-pipeline/SAI/templates/saiapi.cpp.j2
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ static sai_status_t dash_sai_create_{{ table.name }}(
auto mf_exact = mf->mutable_exact();
{{key.field}}SetVal(attr_list[i].value, mf_exact, {{key.bitwidth}});
{% elif key.match_type == 'lpm' %}
{% if key.field == 'ipPrefix' %}
if (getPrefixLength(attr_list[i].value) == 0)
{
// https://github.com/p4lang/PI/blob/24e0a3c08c964e36d235973556b90e0ae922b894/proto/frontend/src/device_mgr.cpp#L2242-L2246
DASH_LOG_WARN("Invalid reprsentation of 'don't care' LPM match, omit match field instead of using a prefix length of 0");
return SAI_STATUS_SUCCESS;
}
{% endif %}
auto mf_lpm = mf->mutable_lpm();
{{key.field}}SetVal(attr_list[i].value, mf_lpm, {{key.bitwidth}});
{% elif key.match_type == 'list' %}
Expand Down Expand Up @@ -401,6 +409,14 @@ static sai_status_t dash_sai_create_{{ table.name }}(
{{key.field}}SetVal(static_cast<uint{{key.bitwidth}}_t>(tableEntry->{{ key.name | lower }}), mf_exact, {{key.bitwidth}});
{% endif %}
{% elif key.match_type == 'lpm' %}
{% if key.field == 'ipPrefix' %}
if (getPrefixLength(tableEntry->{{ key.name | lower }}) == 0)
{
// https://github.com/p4lang/PI/blob/24e0a3c08c964e36d235973556b90e0ae922b894/proto/frontend/src/device_mgr.cpp#L2242-L2246
DASH_LOG_WARN("Invalid reprsentation of 'don't care' LPM match, omit match field instead of using a prefix length of 0");
return SAI_STATUS_SUCCESS;
}
{% endif %}
auto mf_lpm = mf->mutable_lpm();
{{key.field}}SetVal(tableEntry->{{ key.name | lower }}, mf_lpm, {{key.bitwidth}});
{% elif key.match_type == 'ternary' %}
Expand Down Expand Up @@ -582,6 +598,14 @@ static sai_status_t dash_sai_remove_{{ table.name }}(
{% endif %}
//{{key.field}}SetVal(tableEntry->{{ key.name | lower }}, mf_exact, {{key.bitwidth}});
{% elif key.match_type == 'lpm' %}
{% if key.field == 'ipPrefix' %}
if (getPrefixLength(tableEntry->{{ key.name | lower }}) == 0)
{
// https://github.com/p4lang/PI/blob/24e0a3c08c964e36d235973556b90e0ae922b894/proto/frontend/src/device_mgr.cpp#L2242-L2246
DASH_LOG_WARN("Invalid reprsentation of 'don't care' LPM match, omit match field instead of using a prefix length of 0");
return SAI_STATUS_SUCCESS;
}
{% endif %}
auto mf_lpm = mf->mutable_lpm();
{{key.field}}SetVal(tableEntry->{{ key.name | lower }}, mf_lpm, {{key.bitwidth}});
{% elif key.match_type == 'ternary' %}
Expand Down

0 comments on commit 822e90b

Please sign in to comment.