Skip to content

Commit

Permalink
[meta]: Add support for ip prefix (sonic-net#402)
Browse files Browse the repository at this point in the history
Signed-off-by: Marian Pritsak <[email protected]>
  • Loading branch information
marian-pritsak authored and lguohan committed Dec 14, 2018
1 parent 31fd65e commit bb3ff64
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 36 deletions.
42 changes: 42 additions & 0 deletions meta/sai_meta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1377,6 +1377,25 @@ sai_status_t meta_generic_validation_create(

break;

case SAI_ATTR_VALUE_TYPE_IP_PREFIX:

{
switch (value.ipprefix.addr_family)
{
case SAI_IP_ADDR_FAMILY_IPV4:
case SAI_IP_ADDR_FAMILY_IPV6:
break;

default:

SWSS_LOG_ERROR("invalid address family: %d", value.ipprefix.addr_family);

return SAI_STATUS_INVALID_PARAMETER;
}

break;
}

default:

META_LOG_THROW(md, "serialization type is not supported yet FIXME");
Expand Down Expand Up @@ -2074,6 +2093,25 @@ sai_status_t meta_generic_validation_set(

break;

case SAI_ATTR_VALUE_TYPE_IP_PREFIX:

{
switch (value.ipprefix.addr_family)
{
case SAI_IP_ADDR_FAMILY_IPV4:
case SAI_IP_ADDR_FAMILY_IPV6:
break;

default:

SWSS_LOG_ERROR("invalid address family: %d", value.ipprefix.addr_family);

return SAI_STATUS_INVALID_PARAMETER;
}

break;
}

default:

META_LOG_THROW(md, "serialization type is not supported yet FIXME");
Expand Down Expand Up @@ -2665,6 +2703,7 @@ void meta_generic_validation_post_create(
case SAI_ATTR_VALUE_TYPE_IPV4:
case SAI_ATTR_VALUE_TYPE_IPV6:
case SAI_ATTR_VALUE_TYPE_IP_ADDRESS:
case SAI_ATTR_VALUE_TYPE_IP_PREFIX:
case SAI_ATTR_VALUE_TYPE_POINTER:
// primitives
break;
Expand Down Expand Up @@ -2817,6 +2856,7 @@ void meta_generic_validation_post_remove(
case SAI_ATTR_VALUE_TYPE_IPV4:
case SAI_ATTR_VALUE_TYPE_IPV6:
case SAI_ATTR_VALUE_TYPE_IP_ADDRESS:
case SAI_ATTR_VALUE_TYPE_IP_PREFIX:
case SAI_ATTR_VALUE_TYPE_POINTER:
// primitives, ok
break;
Expand Down Expand Up @@ -3000,6 +3040,7 @@ void meta_generic_validation_post_set(
case SAI_ATTR_VALUE_TYPE_IPV6:
case SAI_ATTR_VALUE_TYPE_POINTER:
case SAI_ATTR_VALUE_TYPE_IP_ADDRESS:
case SAI_ATTR_VALUE_TYPE_IP_PREFIX:
// primitives, ok
break;

Expand Down Expand Up @@ -3333,6 +3374,7 @@ void meta_generic_validation_post_get(
case SAI_ATTR_VALUE_TYPE_IPV6:
case SAI_ATTR_VALUE_TYPE_POINTER:
case SAI_ATTR_VALUE_TYPE_IP_ADDRESS:
case SAI_ATTR_VALUE_TYPE_IP_PREFIX:
// primitives, ok
break;

Expand Down
83 changes: 47 additions & 36 deletions meta/saiserialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ sai_status_t transfer_attribute(
transfer_primitive(src_attr.value.ipaddr, dst_attr.value.ipaddr);
break;

case SAI_ATTR_VALUE_TYPE_IP_PREFIX:
transfer_primitive(src_attr.value.ipprefix, dst_attr.value.ipprefix);
break;

case SAI_ATTR_VALUE_TYPE_OBJECT_ID:
transfer_primitive(src_attr.value.oid, dst_attr.value.oid);
break;
Expand Down Expand Up @@ -1346,6 +1350,9 @@ std::string sai_serialize_attr_value(
case SAI_ATTR_VALUE_TYPE_IP_ADDRESS:
return sai_serialize_ip_address(attr.value.ipaddr);

case SAI_ATTR_VALUE_TYPE_IP_PREFIX:
return sai_serialize_ip_prefix(attr.value.ipprefix);

case SAI_ATTR_VALUE_TYPE_OBJECT_ID:
return sai_serialize_object_id(attr.value.oid);

Expand Down Expand Up @@ -2102,6 +2109,42 @@ void sai_deserialize_ip_address(
SWSS_LOG_THROW("invalid ip address %s", s.c_str());
}

void sai_deserialize_ip_prefix(
_In_ const std::string &s,
_Out_ sai_ip_prefix_t &ip_prefix)
{
SWSS_LOG_ENTER();

auto tokens = swss::tokenize(s, '/');

if (tokens.size() != 2)
{
SWSS_LOG_THROW("invalid ip prefix %s", s.c_str());
}

uint8_t mask;
sai_deserialize_number(tokens[1], mask);

const std::string &ip = tokens[0];

if (inet_pton(AF_INET, ip.c_str(), &ip_prefix.addr.ip4) == 1)
{
ip_prefix.addr_family = SAI_IP_ADDR_FAMILY_IPV4;

sai_populate_ip_mask(mask, (uint8_t*)&ip_prefix.mask.ip4, false);
}
else if (inet_pton(AF_INET6, ip.c_str(), ip_prefix.addr.ip6) == 1)
{
ip_prefix.addr_family = SAI_IP_ADDR_FAMILY_IPV6;

sai_populate_ip_mask(mask, ip_prefix.mask.ip6, true);
}
else
{
SWSS_LOG_THROW("invalid ip prefix %s", s.c_str());
}
}

void sai_deserialize_ip_address_list(
_In_ const std::string& s,
_Out_ sai_ip_address_list_t& list,
Expand Down Expand Up @@ -2341,6 +2384,9 @@ void sai_deserialize_attr_value(
case SAI_ATTR_VALUE_TYPE_IP_ADDRESS:
return sai_deserialize_ip_address(s, attr.value.ipaddr);

case SAI_ATTR_VALUE_TYPE_IP_PREFIX:
return sai_deserialize_ip_prefix(s, attr.value.ipprefix);

case SAI_ATTR_VALUE_TYPE_OBJECT_ID:
return sai_deserialize_object_id(s, attr.value.oid);

Expand Down Expand Up @@ -2421,42 +2467,6 @@ void sai_deserialize_attr_value(
}
}

void sai_deserialize_ip_prefix(
_In_ const std::string &s,
_Out_ sai_ip_prefix_t &ip_prefix)
{
SWSS_LOG_ENTER();

auto tokens = swss::tokenize(s, '/');

if (tokens.size() != 2)
{
SWSS_LOG_THROW("invalid ip prefix %s", s.c_str());
}

uint8_t mask;
sai_deserialize_number(tokens[1], mask);

const std::string &ip = tokens[0];

if (inet_pton(AF_INET, ip.c_str(), &ip_prefix.addr.ip4) == 1)
{
ip_prefix.addr_family = SAI_IP_ADDR_FAMILY_IPV4;

sai_populate_ip_mask(mask, (uint8_t*)&ip_prefix.mask.ip4, false);
}
else if (inet_pton(AF_INET6, ip.c_str(), ip_prefix.addr.ip6) == 1)
{
ip_prefix.addr_family = SAI_IP_ADDR_FAMILY_IPV6;

sai_populate_ip_mask(mask, ip_prefix.mask.ip6, true);
}
else
{
SWSS_LOG_THROW("invalid ip prefix %s", s.c_str());
}
}

void sai_deserialize_status(
_In_ const std::string& s,
_Out_ sai_status_t& status)
Expand Down Expand Up @@ -2778,6 +2788,7 @@ void sai_deserialize_free_attribute_value(
case SAI_ATTR_VALUE_TYPE_IPV6:
case SAI_ATTR_VALUE_TYPE_POINTER:
case SAI_ATTR_VALUE_TYPE_IP_ADDRESS:
case SAI_ATTR_VALUE_TYPE_IP_PREFIX:
case SAI_ATTR_VALUE_TYPE_OBJECT_ID:
break;

Expand Down

0 comments on commit bb3ff64

Please sign in to comment.