From b78beb8242af1034eabb3d41d5bc109f37444512 Mon Sep 17 00:00:00 2001 From: Kamil Cudnik Date: Thu, 17 Nov 2016 11:39:37 -0800 Subject: [PATCH] Add serialization support for tunnel map (#112) * Add serialization support for tunnel map * Add tunnel map list to attribute list * Fix syncd logging on main enter * Remove custom range traps from virtual switch --- meta/sai_meta_sanity.cpp | 3 + meta/sai_meta_tunnel.cpp | 7 +-- meta/saiserialize.cpp | 124 +++++++++++++++++++++++++++++++++++--- meta/tests.cpp | 56 +++++++++++++++++ vslib/src/sai_vs_init.cpp | 2 - 5 files changed, 179 insertions(+), 13 deletions(-) diff --git a/meta/sai_meta_sanity.cpp b/meta/sai_meta_sanity.cpp index 32a972463d3e..b353c453c618 100644 --- a/meta/sai_meta_sanity.cpp +++ b/meta/sai_meta_sanity.cpp @@ -327,6 +327,7 @@ std::unordered_map get_serialization_type_map() SER_MAP_SET(PORT_BREAKOUT); SER_MAP_SET(QOS_MAP_LIST); + SER_MAP_SET(TUNNEL_MAP_LIST); return map; } @@ -862,6 +863,7 @@ std::unordered_map> get_attr_n ATTR_MAP_SET(TUNNEL,SAI_TUNNEL_ATTR_DECAP_TTL_MODE); ATTR_MAP_SET(TUNNEL,SAI_TUNNEL_ATTR_DECAP_DSCP_MODE); ATTR_MAP_SET(TUNNEL_MAP,SAI_TUNNEL_MAP_ATTR_TYPE); + ATTR_MAP_SET(TUNNEL_MAP,SAI_TUNNEL_MAP_ATTR_MAP_TO_VALUE_LIST); ATTR_MAP_SET(TUNNEL_TABLE_ENTRY,SAI_TUNNEL_TERM_TABLE_ENTRY_ATTR_VR_ID); ATTR_MAP_SET(TUNNEL_TABLE_ENTRY,SAI_TUNNEL_TERM_TABLE_ENTRY_ATTR_TYPE); ATTR_MAP_SET(TUNNEL_TABLE_ENTRY,SAI_TUNNEL_TERM_TABLE_ENTRY_ATTR_DST_IP); @@ -1077,6 +1079,7 @@ void metadata_sanity_check(const sai_attr_metadata_t& md) case SAI_SERIALIZATION_TYPE_UINT32_RANGE: case SAI_SERIALIZATION_TYPE_UINT32_LIST: case SAI_SERIALIZATION_TYPE_QOS_MAP_LIST: + case SAI_SERIALIZATION_TYPE_TUNNEL_MAP_LIST: case SAI_SERIALIZATION_TYPE_ACL_FIELD_DATA_BOOL: case SAI_SERIALIZATION_TYPE_ACL_FIELD_DATA_UINT8: diff --git a/meta/sai_meta_tunnel.cpp b/meta/sai_meta_tunnel.cpp index bc3885bdc8b0..08f5019d9e9b 100644 --- a/meta/sai_meta_tunnel.cpp +++ b/meta/sai_meta_tunnel.cpp @@ -512,12 +512,12 @@ const sai_attr_metadata_t sai_tunnel_map_attr_metadata[] = { // TODO some extra logic may be needed }, -/* + { .objecttype = SAI_OBJECT_TYPE_TUNNEL_MAP, .attrid = SAI_TUNNEL_MAP_ATTR_MAP_TO_VALUE_LIST, - .nameid = "SAI_TUNNEL_MAP_ATTR_MAP_TO_VALUE_LIST", - .serializationtype = SAI_SERIALIZATION_TYPE_TUNNEL_MAP_LIST, // TODO declare + .attridname = "SAI_TUNNEL_MAP_ATTR_MAP_TO_VALUE_LIST", + .serializationtype = SAI_SERIALIZATION_TYPE_TUNNEL_MAP_LIST, .flags = SAI_ATTR_FLAGS_MANDATORY_ON_CREATE | SAI_ATTR_FLAGS_CREATE_ONLY, .allowedobjecttypes = { }, .allownullobjectid = false, @@ -530,7 +530,6 @@ const sai_attr_metadata_t sai_tunnel_map_attr_metadata[] = { // TODO some extra logic may be needed on map }, -*/ }; const size_t sai_tunnel_map_attr_metadata_count = sizeof(sai_tunnel_map_attr_metadata)/sizeof(sai_attr_metadata_t); diff --git a/meta/saiserialize.cpp b/meta/saiserialize.cpp index dd321bca0bcc..14d07dedd190 100644 --- a/meta/saiserialize.cpp +++ b/meta/saiserialize.cpp @@ -893,14 +893,59 @@ std::string sai_serialize_qos_map_list( return j.dump(); } +json sai_serialize_tunnel_map_params( + _In_ const sai_tunnel_map_params_t& params) +{ + json j; + + j["oecn"] = params.oecn; + j["uecn"] = params.uecn; + j["vni"] = params.vni_id; + j["vlan"] = sai_serialize_vlan_id(params.vlan_id); + + return j; +} + +json sai_serialize_tunnel_map( + _In_ const sai_tunnel_map_t& tunnelmap) +{ + json j; + + j["key"] = sai_serialize_tunnel_map_params(tunnelmap.key); + j["value"] = sai_serialize_tunnel_map_params(tunnelmap.value);; + + return j; +} + std::string sai_serialize_tunnel_map_list( - _In_ const sai_qos_map_list_t& qosmap, + _In_ const sai_tunnel_map_list_t& tunnelmap, _In_ bool countOnly) { SWSS_LOG_ENTER(); - // TODO - return "TODOFIXME"; + json j; + + j["count"] = tunnelmap.count; + + if (tunnelmap.list == NULL || countOnly) + { + j["list"] = nullptr; + + return j.dump(); + } + + json arr = json::array(); + + for (uint32_t i = 0; i < tunnelmap.count; ++i) + { + json item = sai_serialize_tunnel_map(tunnelmap.list[i]); + + arr.push_back(item); + } + + j["list"] = arr; + + return j.dump(); } template @@ -1113,8 +1158,8 @@ std::string sai_serialize_attr_value( case SAI_SERIALIZATION_TYPE_QOS_MAP_LIST: return sai_serialize_qos_map_list(attr.value.qosmap, countOnly); - //case SAI_SERIALIZATION_TYPE_TUNNEL_MAP_LIST: - // return sai_serialize_tunnel_map_list(attr.value.tunnelmap, countOnly); + case SAI_SERIALIZATION_TYPE_TUNNEL_MAP_LIST: + return sai_serialize_tunnel_map_list(attr.value.tunnelmap, countOnly); // ACL FIELD DATA @@ -1664,6 +1709,71 @@ void sai_deserialize_qos_map_list( } } +void sai_deserialize_tunnel_map_params( + _In_ const json& j, + _Out_ sai_tunnel_map_params_t& params) +{ + SWSS_LOG_ENTER(); + + SWSS_LOG_DEBUG("%s", j.dump().c_str()); + + params.oecn = j["oecn"]; + params.uecn = j["uecn"]; + params.vni_id = j["vni"]; + + sai_deserialize_vlan_id(j["vlan"], params.vlan_id); +} + +void sai_deserialize_tunnel_map( + _In_ const json& j, + _Out_ sai_tunnel_map_t& tunnelmap) +{ + SWSS_LOG_ENTER(); + + sai_deserialize_tunnel_map_params(j["key"], tunnelmap.key); + sai_deserialize_tunnel_map_params(j["value"], tunnelmap.value); +} + +void sai_deserialize_tunnel_map_list( + _In_ const std::string& s, + _Out_ sai_tunnel_map_list_t& tunnelmap, + _In_ bool countOnly) +{ + SWSS_LOG_ENTER(); + + json j = json::parse(s); + + tunnelmap.count = j["count"]; + + if (countOnly) + { + return; + } + + if (j["list"] == nullptr) + { + tunnelmap.list = NULL; + return; + } + + json arr = j["list"]; + + if (arr.size() != (size_t)tunnelmap.count) + { + SWSS_LOG_ERROR("tunnel map count mismatch %lu vs %u", arr.size(), tunnelmap.count); + throw std::runtime_error("tunnel map count mismatch"); + } + + tunnelmap.list = sai_alloc_n_of_ptr_type(tunnelmap.count, tunnelmap.list); + + for (uint32_t i = 0; i < tunnelmap.count; ++i) + { + const json& item = arr[i]; + + sai_deserialize_tunnel_map(item, tunnelmap.list[i]); + } +} + void sai_deserialize_ipv6( _In_ const std::string& s, _Out_ sai_ip6_t& ipaddr) @@ -1974,8 +2084,8 @@ void sai_deserialize_attr_value( case SAI_SERIALIZATION_TYPE_QOS_MAP_LIST: return sai_deserialize_qos_map_list(s, attr.value.qosmap, countOnly); - //case SAI_SERIALIZATION_TYPE_TUNNEL_MAP_LIST: - // return sai_deserialize_tunnel_map_list(s, attr.value.tunnelmap, countOnly); + case SAI_SERIALIZATION_TYPE_TUNNEL_MAP_LIST: + return sai_deserialize_tunnel_map_list(s, attr.value.tunnelmap, countOnly); // ACL FIELD DATA diff --git a/meta/tests.cpp b/meta/tests.cpp index f06af1ca8453..6b2f7e2bf0cc 100644 --- a/meta/tests.cpp +++ b/meta/tests.cpp @@ -4004,6 +4004,8 @@ void test_serialize_qos_map() std::string ret = "{\"count\":1,\"list\":[{\"key\":{\"color\":\"SAI_PACKET_COLOR_RED\",\"dot1p\":3,\"dscp\":2,\"pg\":5,\"prio\":4,\"qidx\":6,\"tc\":1},\"value\":{\"color\":\"SAI_PACKET_COLOR_GREEN\",\"dot1p\":33,\"dscp\":22,\"pg\":55,\"prio\":44,\"qidx\":66,\"tc\":11}}]}"; + ASSERT_TRUE(s, ret); + s = sai_serialize_attr_value(*meta, attr, true); std::string ret2 = "{\"count\":1,\"list\":null}"; @@ -4035,6 +4037,59 @@ void test_serialize_qos_map() ASSERT_TRUE(l.value.color, SAI_PACKET_COLOR_GREEN); } +void test_serialize_tunnel_map() +{ + SWSS_LOG_ENTER(); + + meta_init_db(); + + sai_attribute_t attr; + const sai_attr_metadata_t* meta; + std::string s; + + attr.id = SAI_TUNNEL_MAP_ATTR_MAP_TO_VALUE_LIST; + + sai_tunnel_map_t tm = { + .key = { .oecn = 1, .uecn = 2, .vlan_id = 3, .vni_id = 4 }, + .value = { .oecn = 11, .uecn = 22, .vlan_id = 33, .vni_id = 44 } }; + + attr.value.tunnelmap.count = 1; + attr.value.tunnelmap.list = &tm; + + meta = get_attribute_metadata(SAI_OBJECT_TYPE_TUNNEL_MAP, attr.id); + + s = sai_serialize_attr_value(*meta, attr); + + std::string ret = "{\"count\":1,\"list\":[{\"key\":{\"oecn\":1,\"uecn\":2,\"vlan\":\"3\",\"vni\":4},\"value\":{\"oecn\":11,\"uecn\":22,\"vlan\":\"33\",\"vni\":44}}]}"; + + ASSERT_TRUE(s, ret); + + s = sai_serialize_attr_value(*meta, attr, true); + + std::string ret2 = "{\"count\":1,\"list\":null}"; + ASSERT_TRUE(s, ret2); + + // deserialize + + memset(&attr, 0, sizeof(attr)); + + sai_deserialize_attr_value(ret, *meta, attr); + + ASSERT_TRUE(attr.value.tunnelmap.count, 1); + + auto &l = attr.value.tunnelmap.list[0]; + + ASSERT_TRUE(l.key.oecn, 1); + ASSERT_TRUE(l.key.uecn, 2); + ASSERT_TRUE(l.key.vlan_id, 3); + ASSERT_TRUE(l.key.vni_id, 4); + + ASSERT_TRUE(l.value.oecn, 11); + ASSERT_TRUE(l.value.uecn, 22); + ASSERT_TRUE(l.value.vlan_id, 33); + ASSERT_TRUE(l.value.vni_id, 44); +} + template void deserialize_number( _In_ const std::string& s, @@ -4140,6 +4195,7 @@ int main() test_serialize_oid_list(); test_serialize_acl_action(); test_serialize_qos_map(); + test_serialize_tunnel_map(); // attributes tests diff --git a/vslib/src/sai_vs_init.cpp b/vslib/src/sai_vs_init.cpp index e119a20bed0d..6db93f7225c7 100644 --- a/vslib/src/sai_vs_init.cpp +++ b/vslib/src/sai_vs_init.cpp @@ -408,7 +408,6 @@ sai_status_t create_hostif_traps() SAI_HOSTIF_TRAP_ID_IGMP_TYPE_V2_REPORT, SAI_HOSTIF_TRAP_ID_IGMP_TYPE_V3_REPORT, SAI_HOSTIF_TRAP_ID_SAMPLEPACKET, - SAI_HOSTIF_TRAP_ID_SWITCH_CUSTOM_RANGE_BASE, SAI_HOSTIF_TRAP_ID_ARP_REQUEST, SAI_HOSTIF_TRAP_ID_ARP_RESPONSE, SAI_HOSTIF_TRAP_ID_DHCP, @@ -428,7 +427,6 @@ sai_status_t create_hostif_traps() SAI_HOSTIF_TRAP_ID_IP2ME, SAI_HOSTIF_TRAP_ID_SSH, SAI_HOSTIF_TRAP_ID_SNMP, - SAI_HOSTIF_TRAP_ID_ROUTER_CUSTOM_RANGE_BASE, SAI_HOSTIF_TRAP_ID_L3_MTU_ERROR, SAI_HOSTIF_TRAP_ID_TTL_ERROR };