From 245e66c299fd9749600e2d145d74b029f58bd2bd Mon Sep 17 00:00:00 2001 From: Mukesh MV Date: Wed, 6 Nov 2024 22:41:57 -0800 Subject: [PATCH] [DASH] Add meter rule bulk support and test to syncd Signed-off-by: Mukesh MV --- syncd/VendorSai.cpp | 8 +++ tests/TestDash.cpp | 85 ++++++++++++++++++++++++++++++++ unittest/syncd/TestVendorSai.cpp | 65 ++++++++++++++++++++++++ 3 files changed, 158 insertions(+) diff --git a/syncd/VendorSai.cpp b/syncd/VendorSai.cpp index a256ff404..c0cb5683a 100644 --- a/syncd/VendorSai.cpp +++ b/syncd/VendorSai.cpp @@ -493,6 +493,10 @@ sai_status_t VendorSai::bulkCreate( ptr = m_apis.dash_acl_api->create_dash_acl_rules; break; + case SAI_OBJECT_TYPE_METER_RULE: + ptr = m_apis.dash_meter_api->create_meter_rules; + break; + default: SWSS_LOG_ERROR("not implemented %s, FIXME", sai_serialize_object_type(object_type).c_str()); return SAI_STATUS_NOT_IMPLEMENTED; @@ -572,6 +576,10 @@ sai_status_t VendorSai::bulkRemove( ptr = m_apis.dash_acl_api->remove_dash_acl_rules; break; + case SAI_OBJECT_TYPE_METER_RULE: + ptr = m_apis.dash_meter_api->remove_meter_rules; + break; + default: SWSS_LOG_ERROR("not implemented %s, FIXME", sai_serialize_object_type(object_type).c_str()); return SAI_STATUS_NOT_IMPLEMENTED; diff --git a/tests/TestDash.cpp b/tests/TestDash.cpp index 623d64712..4e9b05f6a 100644 --- a/tests/TestDash.cpp +++ b/tests/TestDash.cpp @@ -1639,3 +1639,88 @@ TEST(APIBulk, outbound_ca_to_pa_entry) ASSERT_SUCCESS(counter_api->remove_counter(counter0)); ASSERT_SUCCESS(counter_api->remove_counter(counter1)); } + +TEST(APIBulk, meter_rule) +{ + SWSS_LOG_ENTER(); + + auto switchid = TestDashEnv::instance()->getSwitchOid(); + + sai_dash_meter_api_t *dash_meter_api = nullptr; + ASSERT_SUCCESS(sai_api_query((sai_api_t)SAI_API_DASH_METER, (void**)&dash_meter_api)); + + sai_attribute_t attr; + sai_object_id_t meter_policy0, meter_policy1; + attr.id = SAI_METER_POLICY_ATTR_IP_ADDR_FAMILY; + attr.value.s32 = SAI_IP_ADDR_FAMILY_IPV4; + ASSERT_SUCCESS(dash_meter_api->create_meter_policy(&meter_policy0, switchid, 1, &attr)); + ASSERT_SUCCESS(dash_meter_api->create_meter_policy(&meter_policy1, switchid, 1, &attr)); + + + sai_ip_address_t dst0 = {}; + sai_ip_address_t mask0 = {}; + sai_ip_address_t dst1 = {}; + sai_ip_address_t mask1 = {}; + dst0.addr_family = dst1.addr_family = mask0.addr_family = mask1.addr_family = SAI_IP_ADDR_FAMILY_IPV4; + inet_pton(AF_INET, "192.1.1.0", &dst0.addr.ip4); + inet_pton(AF_INET, "255.255.255.0", &mask0.addr.ip4); + inet_pton(AF_INET, "192.15.0.0", &dst1.addr.ip4); + inet_pton(AF_INET, "255.255.0.0", &mask1.addr.ip4); + + sai_attribute_t attrs0[] = { + {.id = SAI_METER_RULE_ATTR_METER_POLICY_ID, .value = (sai_attribute_value_t){.oid = meter_policy0}}, + {.id = SAI_METER_RULE_ATTR_DIP, .value = (sai_attribute_value_t){.ipaddr = dst0}}, + {.id = SAI_METER_RULE_ATTR_DIP_MASK, .value = (sai_attribute_value_t){.ipaddr = mask0}}, + {.id = SAI_METER_RULE_ATTR_METER_CLASS, .value = (sai_attribute_value_t){.u32 = 100}}, + {.id = SAI_METER_RULE_ATTR_PRIORITY, .value = (sai_attribute_value_t){.u32 = 1}}, + }; + + sai_attribute_t attrs1[] = { + {.id = SAI_METER_RULE_ATTR_METER_POLICY_ID, .value = (sai_attribute_value_t){.oid = meter_policy1}}, + {.id = SAI_METER_RULE_ATTR_DIP, .value = (sai_attribute_value_t){.ipaddr = dst1}}, + {.id = SAI_METER_RULE_ATTR_DIP_MASK, .value = (sai_attribute_value_t){.ipaddr = mask1}}, + {.id = SAI_METER_RULE_ATTR_METER_CLASS, .value = (sai_attribute_value_t){.u32 = 200}}, + {.id = SAI_METER_RULE_ATTR_PRIORITY, .value = (sai_attribute_value_t){.u32 = 2}}, + }; + + const sai_attribute_t *attr_list[] = { + attrs0, + attrs1, + }; + constexpr uint32_t meter_rules_count = sizeof(attr_list) / sizeof(sai_attribute_t*); + constexpr uint32_t meter_rule_attrs_count = sizeof(attrs0) / sizeof(sai_attribute_t); + + uint32_t attr_count[meter_rules_count] = {meter_rule_attrs_count, meter_rule_attrs_count}; + sai_object_id_t meter_rules[meter_rules_count]; + sai_status_t statuses[meter_rules_count] = {}; + + ASSERT_SUCCESS(dash_meter_api->create_meter_rules(switchid, meter_rules_count, attr_count, attr_list, SAI_BULK_OP_ERROR_MODE_STOP_ON_ERROR, meter_rules, statuses)); + for (uint32_t i = 0; i < meter_rules_count; i++) { + ASSERT_SUCCESS(statuses[i]); + } + + for (uint32_t i = 0; i < meter_rules_count; i++) { + sai_attribute_t attrs[meter_rule_attrs_count] = { + {.id = SAI_METER_RULE_ATTR_METER_POLICY_ID, .value = {}}, + {.id = SAI_METER_RULE_ATTR_DIP, .value = {}}, + {.id = SAI_METER_RULE_ATTR_DIP_MASK, .value = {}}, + {.id = SAI_METER_RULE_ATTR_METER_CLASS, .value = {}}, + {.id = SAI_METER_RULE_ATTR_PRIORITY, .value = {}}, + }; + + ASSERT_SUCCESS(dash_meter_api->get_meter_rule_attribute(meter_rules[i], meter_rule_attrs_count, attrs)); + ASSERT_EQ(attrs[0].value.oid, attr_list[i][0].value.oid); + ASSERT_EQ(attrs[1].value.ipaddr, attr_list[i][1].value.ipaddr); + ASSERT_EQ(attrs[2].value.ipaddr, attr_list[i][2].value.ipaddr); + ASSERT_EQ(attrs[3].value.u32, attr_list[i][3].value.u32); + ASSERT_EQ(attrs[4].value.u32, attr_list[i][4].value.u32); + } + + ASSERT_SUCCESS(dash_meter_api->remove_meter_rules(meter_rules_count, meter_rules, SAI_BULK_OP_ERROR_MODE_STOP_ON_ERROR, statuses)); + for (uint32_t i = 0; i < meter_rules_count; i++) { + ASSERT_SUCCESS(statuses[i]); + } + + ASSERT_SUCCESS(dash_meter_api->remove_meter_policy(meter_policy0)); + ASSERT_SUCCESS(dash_meter_api->remove_meter_policy(meter_policy1)); +} diff --git a/unittest/syncd/TestVendorSai.cpp b/unittest/syncd/TestVendorSai.cpp index 9f6245677..63977e713 100644 --- a/unittest/syncd/TestVendorSai.cpp +++ b/unittest/syncd/TestVendorSai.cpp @@ -1478,3 +1478,68 @@ TEST_F(VendorSaiTest, bulk_meter_bucket_entry) EXPECT_EQ(SAI_STATUS_NOT_SUPPORTED, m_vsai->bulkSet(0, e, nullptr, SAI_BULK_OP_ERROR_MODE_STOP_ON_ERROR, nullptr)); } + +TEST(VendorSai, bulk_meter_rules) +{ + VendorSai sai; + sai.apiInitialize(0, &test_services); + + sai_object_id_t switchid = create_switch(sai); + + sai_attribute_t attr; + sai_object_id_t meter_policy0, meter_policy1; + attr.id = SAI_METER_POLICY_ATTR_IP_ADDR_FAMILY; + attr.value.s32 = SAI_IP_ADDR_FAMILY_IPV4; + EXPECT_EQ(SAI_STATUS_SUCCESS, sai.create((sai_object_type_t)SAI_OBJECT_TYPE_METER_POLICY, &meter_policy0, switchid, 1, &attr)); + EXPECT_EQ(SAI_STATUS_SUCCESS, sai.create((sai_object_type_t)SAI_OBJECT_TYPE_METER_POLICY, &meter_policy1, switchid, 1, &attr)); + + sai_ip_address_t dst0 = {}; + sai_ip_address_t mask0 = {}; + sai_ip_address_t dst1 = {}; + sai_ip_address_t mask1 = {}; + dst0.addr_family = dst1.addr_family = mask0.addr_family = mask1.addr_family = SAI_IP_ADDR_FAMILY_IPV4; + inet_pton(AF_INET, "192.1.1.0", &dst0.addr.ip4); + inet_pton(AF_INET, "255.255.255.0", &mask0.addr.ip4); + inet_pton(AF_INET, "192.15.0.0", &dst1.addr.ip4); + inet_pton(AF_INET, "255.255.0.0", &mask1.addr.ip4); + + sai_attribute_t attrs0[] = { + {.id = SAI_METER_RULE_ATTR_METER_POLICY_ID, .value = (sai_attribute_value_t){.oid = meter_policy0}}, + {.id = SAI_METER_RULE_ATTR_DIP, .value = (sai_attribute_value_t){.ipaddr = dst0}}, + {.id = SAI_METER_RULE_ATTR_DIP_MASK, .value = (sai_attribute_value_t){.ipaddr = mask0}}, + {.id = SAI_METER_RULE_ATTR_METER_CLASS, .value = (sai_attribute_value_t){.u32 = 100}}, + {.id = SAI_METER_RULE_ATTR_PRIORITY, .value = (sai_attribute_value_t){.u32 = 1}}, + }; + + sai_attribute_t attrs1[] = { + {.id = SAI_METER_RULE_ATTR_METER_POLICY_ID, .value = (sai_attribute_value_t){.oid = meter_policy1}}, + {.id = SAI_METER_RULE_ATTR_DIP, .value = (sai_attribute_value_t){.ipaddr = dst1}}, + {.id = SAI_METER_RULE_ATTR_DIP_MASK, .value = (sai_attribute_value_t){.ipaddr = mask1}}, + {.id = SAI_METER_RULE_ATTR_METER_CLASS, .value = (sai_attribute_value_t){.u32 = 200}}, + {.id = SAI_METER_RULE_ATTR_PRIORITY, .value = (sai_attribute_value_t){.u32 = 2}}, + }; + + const sai_attribute_t *attr_list[] = { + attrs0, + attrs1, + }; + constexpr uint32_t meter_rules_count = sizeof(attr_list) / sizeof(sai_attribute_t*); + constexpr uint32_t meter_rule_attrs_count = sizeof(attrs0) / sizeof(sai_attribute_t); + + uint32_t attr_count[meter_rules_count] = {meter_rule_attrs_count, meter_rule_attrs_count}; + sai_object_id_t meter_rules[meter_rules_count]; + sai_status_t statuses[meter_rules_count] = {}; + + EXPECT_EQ(SAI_STATUS_SUCCESS, sai.bulkCreate((sai_object_type_t)SAI_OBJECT_TYPE_METER_RULE, switchid, meter_rules_count, attr_count, attr_list, SAI_BULK_OP_ERROR_MODE_STOP_ON_ERROR, meter_rules, statuses)); + for (uint32_t i = 0; i < meter_rules_count; i++) { + EXPECT_EQ(SAI_STATUS_SUCCESS, statuses[i]); + } + + EXPECT_EQ(SAI_STATUS_SUCCESS, sai.bulkRemove((sai_object_type_t)SAI_OBJECT_TYPE_METER_RULE, meter_rules_count, meter_rules, SAI_BULK_OP_ERROR_MODE_STOP_ON_ERROR, statuses)); + for (uint32_t i = 0; i < meter_rules_count; i++) { + EXPECT_EQ(SAI_STATUS_SUCCESS, statuses[i]); + } + + EXPECT_EQ(SAI_STATUS_SUCCESS, sai.remove((sai_object_type_t)SAI_OBJECT_TYPE_METER_POLICY, meter_policy0)); + EXPECT_EQ(SAI_STATUS_SUCCESS, sai.remove((sai_object_type_t)SAI_OBJECT_TYPE_METER_POLICY, meter_policy1)); +}