diff --git a/meta/DummySaiInterface.cpp b/meta/DummySaiInterface.cpp index b15454bb2..51e9e7d51 100644 --- a/meta/DummySaiInterface.cpp +++ b/meta/DummySaiInterface.cpp @@ -23,10 +23,33 @@ void DummySaiInterface::setStatus( sai_status_t DummySaiInterface::apiInitialize( _In_ uint64_t flags, - _In_ const sai_service_method_table_t *service_method_table) + _In_ const sai_service_method_table_t *smt) { SWSS_LOG_ENTER(); + if (smt) + { + if (smt->profile_get_value) + { + SWSS_LOG_NOTICE("Dummy: profile_get_value(NULL): %s", smt->profile_get_value(0, NULL)); + SWSS_LOG_NOTICE("Dummy: profile_get_value(FOO): %s", smt->profile_get_value(0, "FOO")); + SWSS_LOG_NOTICE("Dummy: profile_get_value(FOO): %s", smt->profile_get_value(0, "CAR")); + } + + if (smt->profile_get_next_value) + { + + const char *var = NULL; + const char *val = NULL; + + SWSS_LOG_NOTICE("Dummy: profile_get_next_value: %d", smt->profile_get_next_value(0, NULL, NULL)); + SWSS_LOG_NOTICE("Dummy: profile_get_next_value: %d", smt->profile_get_next_value(0, NULL, &val)); + SWSS_LOG_NOTICE("Dummy: profile_get_next_value: %d", smt->profile_get_next_value(0, &var, NULL)); + SWSS_LOG_NOTICE("Dummy: profile_get_next_value: %d", smt->profile_get_next_value(0, &var, &val)); + SWSS_LOG_NOTICE("Dummy: profile_get_next_value: %d", smt->profile_get_next_value(0, &var, &val)); + } + } + return SAI_STATUS_SUCCESS; } diff --git a/proxylib/Proxy.cpp b/proxylib/Proxy.cpp index 60ecb43bd..ff789c5fd 100644 --- a/proxylib/Proxy.cpp +++ b/proxylib/Proxy.cpp @@ -26,11 +26,15 @@ Proxy::Proxy( { SWSS_LOG_ENTER(); + m_configFile = "config.ini"; // TODO to command line + // TODO to move hard coded addresses to config m_selectableChannel = std::make_shared("tcp://127.0.0.1:5555"); m_notifications = std::make_shared("tcp://127.0.0.1:5556"); + loadProfileMap(); + m_smt.profileGetValue = std::bind(&Proxy::profileGetValue, this, _1, _2); m_smt.profileGetNextValue = std::bind(&Proxy::profileGetNextValue, this, _1, _2, _3); @@ -55,6 +59,8 @@ Proxy::~Proxy() { SWSS_LOG_ENTER(); + // TODO call stop() + if (m_apiInitialized) { SWSS_LOG_NOTICE("calling api uninitialize"); @@ -73,6 +79,47 @@ Proxy::~Proxy() m_notifications = nullptr; } +void Proxy::loadProfileMap() +{ + SWSS_LOG_ENTER(); + + std::ifstream profile(m_configFile.c_str()); + + if (!profile.is_open()) + { + SWSS_LOG_WARN("failed to open profile map file: %s: %s", + m_configFile.c_str(), + strerror(errno)); + + return; + } + + std::string line; + + while (getline(profile, line)) + { + if (line.size() > 0 && (line[0] == '#' || line[0] == ';')) + { + continue; + } + + size_t pos = line.find("="); + + if (pos == std::string::npos) + { + SWSS_LOG_WARN("not found '=' in line %s", line.c_str()); + continue; + } + + std::string key = line.substr(0, pos); + std::string value = line.substr(pos + 1); + + m_profileMap[key] = value; + + SWSS_LOG_NOTICE("insert: %s:%s", key.c_str(), value.c_str()); + } +} + const char* Proxy::profileGetValue( _In_ sai_switch_profile_id_t profile_id, _In_ const char* variable) diff --git a/proxylib/Proxy.h b/proxylib/Proxy.h index 45f696ebe..8fc3b6424 100644 --- a/proxylib/Proxy.h +++ b/proxylib/Proxy.h @@ -98,6 +98,11 @@ namespace saiproxy void processClearStats( _In_ const swss::KeyOpFieldsValuesTuple &kco); + + private: + + void loadProfileMap(); + private: syncd::ServiceMethodTable m_smt; @@ -116,6 +121,8 @@ namespace saiproxy std::shared_ptr m_notifications; + std::string m_configFile; + /** * @brief Mutex for synchronizing api execution and notifications */ diff --git a/proxylib/sai_proxy.h b/proxylib/sai_proxy.h deleted file mode 100644 index e9810523e..000000000 --- a/proxylib/sai_proxy.h +++ /dev/null @@ -1,450 +0,0 @@ -#pragma once - -extern "C" { -#include "sai.h" -#include "saiextensions.h" -} - -#include "meta/SaiInterface.h" - -#include "swss/logger.h" - -#include - -#define PRIVATE __attribute__((visibility("hidden"))) - -PRIVATE extern const sai_acl_api_t proxy_acl_api; -PRIVATE extern const sai_ars_api_t proxy_ars_api; -PRIVATE extern const sai_ars_profile_api_t proxy_ars_profile_api; -PRIVATE extern const sai_bfd_api_t proxy_bfd_api; -PRIVATE extern const sai_bmtor_api_t proxy_bmtor_api; -PRIVATE extern const sai_generic_programmable_api_t proxy_generic_programmable_api; -PRIVATE extern const sai_bridge_api_t proxy_bridge_api; -PRIVATE extern const sai_buffer_api_t proxy_buffer_api; -PRIVATE extern const sai_counter_api_t proxy_counter_api; -PRIVATE extern const sai_dash_vip_api_t proxy_dash_vip_api; -PRIVATE extern const sai_dash_pa_validation_api_t proxy_dash_pa_validation_api; -PRIVATE extern const sai_dash_vnet_api_t proxy_dash_vnet_api; -PRIVATE extern const sai_dash_outbound_routing_api_t proxy_dash_outbound_routing_api; -PRIVATE extern const sai_dash_outbound_ca_to_pa_api_t proxy_dash_outbound_ca_to_pa_api; -PRIVATE extern const sai_dash_inbound_routing_api_t proxy_dash_inbound_routing_api; -PRIVATE extern const sai_dash_eni_api_t proxy_dash_eni_api; -PRIVATE extern const sai_dash_direction_lookup_api_t proxy_dash_direction_lookup_api; -PRIVATE extern const sai_dash_acl_api_t proxy_dash_acl_api; -PRIVATE extern const sai_debug_counter_api_t proxy_debug_counter_api; -PRIVATE extern const sai_dtel_api_t proxy_dtel_api; -PRIVATE extern const sai_fdb_api_t proxy_fdb_api; -PRIVATE extern const sai_hash_api_t proxy_hash_api; -PRIVATE extern const sai_hostif_api_t proxy_hostif_api; -PRIVATE extern const sai_ipmc_api_t proxy_ipmc_api; -PRIVATE extern const sai_ipmc_group_api_t proxy_ipmc_group_api; -PRIVATE extern const sai_isolation_group_api_t proxy_isolation_group_api; -PRIVATE extern const sai_l2mc_api_t proxy_l2mc_api; -PRIVATE extern const sai_l2mc_group_api_t proxy_l2mc_group_api; -PRIVATE extern const sai_lag_api_t proxy_lag_api; -PRIVATE extern const sai_macsec_api_t proxy_macsec_api; -PRIVATE extern const sai_mcast_fdb_api_t proxy_mcast_fdb_api; -PRIVATE extern const sai_mirror_api_t proxy_mirror_api; -PRIVATE extern const sai_mpls_api_t proxy_mpls_api; -PRIVATE extern const sai_nat_api_t proxy_nat_api; -PRIVATE extern const sai_neighbor_api_t proxy_neighbor_api; -PRIVATE extern const sai_next_hop_api_t proxy_next_hop_api; -PRIVATE extern const sai_next_hop_group_api_t proxy_next_hop_group_api; -PRIVATE extern const sai_policer_api_t proxy_policer_api; -PRIVATE extern const sai_port_api_t proxy_port_api; -PRIVATE extern const sai_qos_map_api_t proxy_qos_map_api; -PRIVATE extern const sai_queue_api_t proxy_queue_api; -PRIVATE extern const sai_route_api_t proxy_route_api; -PRIVATE extern const sai_router_interface_api_t proxy_router_interface_api; -PRIVATE extern const sai_rpf_group_api_t proxy_rpf_group_api; -PRIVATE extern const sai_samplepacket_api_t proxy_samplepacket_api; -PRIVATE extern const sai_scheduler_api_t proxy_scheduler_api; -PRIVATE extern const sai_scheduler_group_api_t proxy_scheduler_group_api; -PRIVATE extern const sai_srv6_api_t proxy_srv6_api; -PRIVATE extern const sai_stp_api_t proxy_stp_api; -PRIVATE extern const sai_switch_api_t proxy_switch_api; -PRIVATE extern const sai_system_port_api_t proxy_system_port_api; -PRIVATE extern const sai_tam_api_t proxy_tam_api; -PRIVATE extern const sai_tunnel_api_t proxy_tunnel_api; -PRIVATE extern const sai_udf_api_t proxy_udf_api; -PRIVATE extern const sai_virtual_router_api_t proxy_virtual_router_api; -PRIVATE extern const sai_vlan_api_t proxy_vlan_api; -PRIVATE extern const sai_wred_api_t proxy_wred_api; -PRIVATE extern const sai_my_mac_api_t proxy_my_mac_api; -PRIVATE extern const sai_ipsec_api_t proxy_ipsec_api; -PRIVATE extern const sai_twamp_api_t proxy_twamp_api; -PRIVATE extern const sai_dash_meter_api_t proxy_dash_meter_api; -PRIVATE extern const sai_poe_api_t proxy_poe_api; - -PRIVATE extern std::shared_ptr proxy_sai; - -// QUAD OID - -#define PROXY_CREATE(OT,ot) \ - static sai_status_t proxy_create_ ## ot( \ - _Out_ sai_object_id_t *object_id, \ - _In_ sai_object_id_t switch_id, \ - _In_ uint32_t attr_count, \ - _In_ const sai_attribute_t *attr_list) \ -{ \ - SWSS_LOG_ENTER(); \ - return proxy_sai->create( \ - (sai_object_type_t)SAI_OBJECT_TYPE_ ## OT, \ - object_id, \ - switch_id, \ - attr_count, \ - attr_list); \ -} - -#define PROXY_REMOVE(OT,ot) \ - static sai_status_t proxy_remove_ ## ot( \ - _In_ sai_object_id_t object_id) \ -{ \ - SWSS_LOG_ENTER(); \ - return proxy_sai->remove( \ - (sai_object_type_t)SAI_OBJECT_TYPE_ ## OT, \ - object_id); \ -} - -#define PROXY_SET(OT,ot) \ - static sai_status_t proxy_set_ ## ot ## _attribute( \ - _In_ sai_object_id_t object_id, \ - _In_ const sai_attribute_t *attr) \ -{ \ - SWSS_LOG_ENTER(); \ - return proxy_sai->set( \ - (sai_object_type_t)SAI_OBJECT_TYPE_ ## OT, \ - object_id, \ - attr); \ -} - -#define PROXY_GET(OT,ot) \ - static sai_status_t proxy_get_ ## ot ## _attribute( \ - _In_ sai_object_id_t object_id, \ - _In_ uint32_t attr_count, \ - _Inout_ sai_attribute_t *attr_list) \ -{ \ - SWSS_LOG_ENTER(); \ - return proxy_sai->get( \ - (sai_object_type_t)SAI_OBJECT_TYPE_ ## OT, \ - object_id, \ - attr_count, \ - attr_list); \ -} - -// QUAD DECLARE - -#define PROXY_GENERIC_QUAD(OT,ot) \ - PROXY_CREATE(OT,ot); \ - PROXY_REMOVE(OT,ot); \ - PROXY_SET(OT,ot); \ - PROXY_GET(OT,ot); - -// QUAD ENTRY - -#define PROXY_CREATE_ENTRY(OT,ot) \ - static sai_status_t proxy_create_ ## ot( \ - _In_ const sai_ ## ot ##_t *entry, \ - _In_ uint32_t attr_count, \ - _In_ const sai_attribute_t *attr_list) \ -{ \ - SWSS_LOG_ENTER(); \ - return proxy_sai->create( \ - entry, \ - attr_count, \ - attr_list); \ -} - -#define PROXY_REMOVE_ENTRY(OT,ot) \ - static sai_status_t proxy_remove_ ## ot( \ - _In_ const sai_ ## ot ## _t *entry) \ -{ \ - SWSS_LOG_ENTER(); \ - return proxy_sai->remove( \ - entry); \ -} - -#define PROXY_SET_ENTRY(OT,ot) \ - static sai_status_t proxy_set_ ## ot ## _attribute( \ - _In_ const sai_ ## ot ## _t *entry, \ - _In_ const sai_attribute_t *attr) \ -{ \ - SWSS_LOG_ENTER(); \ - return proxy_sai->set( \ - entry, \ - attr); \ -} - -#define PROXY_GET_ENTRY(OT,ot) \ - static sai_status_t proxy_get_ ## ot ## _attribute( \ - _In_ const sai_ ## ot ## _t *entry, \ - _In_ uint32_t attr_count, \ - _Inout_ sai_attribute_t *attr_list) \ -{ \ - SWSS_LOG_ENTER(); \ - return proxy_sai->get( \ - entry, \ - attr_count, \ - attr_list); \ -} - -// QUAD ENTRY DECLARE - -#define PROXY_GENERIC_QUAD_ENTRY(OT,ot) \ - PROXY_CREATE_ENTRY(OT,ot); \ - PROXY_REMOVE_ENTRY(OT,ot); \ - PROXY_SET_ENTRY(OT,ot); \ - PROXY_GET_ENTRY(OT,ot); - -// QUAD API - -#define PROXY_GENERIC_QUAD_API(ot) \ - proxy_create_ ## ot, \ - proxy_remove_ ## ot, \ - proxy_set_ ## ot ##_attribute, \ - proxy_get_ ## ot ##_attribute, - -// STATS - -#define PROXY_GET_STATS(OT,ot) \ - static sai_status_t proxy_get_ ## ot ## _stats( \ - _In_ sai_object_id_t object_id, \ - _In_ uint32_t number_of_counters, \ - _In_ const sai_stat_id_t *counter_ids, \ - _Out_ uint64_t *counters) \ -{ \ - SWSS_LOG_ENTER(); \ - return proxy_sai->getStats( \ - (sai_object_type_t)SAI_OBJECT_TYPE_ ## OT, \ - object_id, \ - number_of_counters, \ - counter_ids, \ - counters); \ -} - -#define PROXY_GET_STATS_EXT(OT,ot) \ - static sai_status_t proxy_get_ ## ot ## _stats_ext( \ - _In_ sai_object_id_t object_id, \ - _In_ uint32_t number_of_counters, \ - _In_ const sai_stat_id_t *counter_ids, \ - _In_ sai_stats_mode_t mode, \ - _Out_ uint64_t *counters) \ -{ \ - SWSS_LOG_ENTER(); \ - return proxy_sai->getStatsExt( \ - (sai_object_type_t)SAI_OBJECT_TYPE_ ## OT, \ - object_id, \ - number_of_counters, \ - counter_ids, \ - mode, \ - counters); \ -} - -#define PROXY_CLEAR_STATS(OT,ot) \ - static sai_status_t proxy_clear_ ## ot ## _stats( \ - _In_ sai_object_id_t object_id, \ - _In_ uint32_t number_of_counters, \ - _In_ const sai_stat_id_t *counter_ids) \ -{ \ - SWSS_LOG_ENTER(); \ - return proxy_sai->clearStats( \ - (sai_object_type_t)SAI_OBJECT_TYPE_ ## OT, \ - object_id, \ - number_of_counters, \ - counter_ids); \ -} - -// STATS DECLARE - -#define PROXY_GENERIC_STATS(OT, ot) \ - PROXY_GET_STATS(OT,ot); \ - PROXY_GET_STATS_EXT(OT,ot); \ - PROXY_CLEAR_STATS(OT,ot); - -// STATS API - -#define PROXY_GENERIC_STATS_API(ot) \ - proxy_get_ ## ot ## _stats, \ - proxy_get_ ## ot ## _stats_ext, \ - proxy_clear_ ## ot ## _stats, - -// BULK QUAD - -#define PROXY_BULK_CREATE(OT,fname) \ - static sai_status_t proxy_bulk_create_ ## fname( \ - _In_ sai_object_id_t switch_id, \ - _In_ uint32_t object_count, \ - _In_ const uint32_t *attr_count, \ - _In_ const sai_attribute_t **attr_list, \ - _In_ sai_bulk_op_error_mode_t mode, \ - _Out_ sai_object_id_t *object_id, \ - _Out_ sai_status_t *object_statuses) \ -{ \ - SWSS_LOG_ENTER(); \ - return proxy_sai->bulkCreate( \ - (sai_object_type_t)SAI_OBJECT_TYPE_ ## OT, \ - switch_id, \ - object_count, \ - attr_count, \ - attr_list, \ - mode, \ - object_id, \ - object_statuses); \ -} - -#define PROXY_BULK_REMOVE(OT,fname) \ - static sai_status_t proxy_bulk_remove_ ## fname( \ - _In_ uint32_t object_count, \ - _In_ const sai_object_id_t *object_id, \ - _In_ sai_bulk_op_error_mode_t mode, \ - _Out_ sai_status_t *object_statuses) \ -{ \ - SWSS_LOG_ENTER(); \ - return proxy_sai->bulkRemove( \ - (sai_object_type_t)SAI_OBJECT_TYPE_ ## OT, \ - object_count, \ - object_id, \ - mode, \ - object_statuses); \ -} - -#define PROXY_BULK_SET(OT,fname) \ - static sai_status_t proxy_bulk_set_ ## fname( \ - _In_ uint32_t object_count, \ - _In_ const sai_object_id_t *object_id, \ - _In_ const sai_attribute_t *attr_list, \ - _In_ sai_bulk_op_error_mode_t mode, \ - _Out_ sai_status_t *object_statuses) \ -{ \ - SWSS_LOG_ENTER(); \ - return proxy_sai->bulkSet( \ - (sai_object_type_t)SAI_OBJECT_TYPE_ ## OT, \ - object_count, \ - object_id, \ - attr_list, \ - mode, \ - object_statuses); \ -} - -#define PROXY_BULK_GET(OT,fname) \ - static sai_status_t proxy_bulk_get_ ## fname( \ - _In_ uint32_t object_count, \ - _In_ const sai_object_id_t *object_id, \ - _In_ const uint32_t *attr_count, \ - _Inout_ sai_attribute_t **attr_list, \ - _In_ sai_bulk_op_error_mode_t mode, \ - _Out_ sai_status_t *object_statuses) \ -{ \ - SWSS_LOG_ENTER(); \ - SWSS_LOG_ERROR("not implemented"); \ - return SAI_STATUS_NOT_IMPLEMENTED; \ -} - -// BULK QUAD DECLARE - -#define PROXY_BULK_QUAD(OT,ot) \ - PROXY_BULK_CREATE(OT,ot); \ - PROXY_BULK_REMOVE(OT,ot); \ - PROXY_BULK_SET(OT,ot); \ - PROXY_BULK_GET(OT,ot); - -// BULK QUAD ENTRY - -#define PROXY_BULK_CREATE_ENTRY(OT,ot) \ - PROXY_BULK_CREATE_ENTRY_EX(OT, ot, ot) - -#define PROXY_BULK_CREATE_ENTRY_EX(OT,ot,fname) \ - static sai_status_t proxy_bulk_create_ ## fname(\ - _In_ uint32_t object_count, \ - _In_ const sai_ ## ot ## _t *entry, \ - _In_ const uint32_t *attr_count, \ - _In_ const sai_attribute_t **attr_list, \ - _In_ sai_bulk_op_error_mode_t mode, \ - _Out_ sai_status_t *object_statuses) \ -{ \ - SWSS_LOG_ENTER(); \ - return proxy_sai->bulkCreate( \ - object_count, \ - entry, \ - attr_count, \ - attr_list, \ - mode, \ - object_statuses); \ -} - -#define PROXY_BULK_REMOVE_ENTRY(OT,ot) \ - PROXY_BULK_REMOVE_ENTRY_EX(OT, ot, ot) - -#define PROXY_BULK_REMOVE_ENTRY_EX(OT,ot,fname) \ - static sai_status_t proxy_bulk_remove_ ## fname(\ - _In_ uint32_t object_count, \ - _In_ const sai_ ## ot ##_t *entry, \ - _In_ sai_bulk_op_error_mode_t mode, \ - _Out_ sai_status_t *object_statuses) \ -{ \ - SWSS_LOG_ENTER(); \ - return proxy_sai->bulkRemove( \ - object_count, \ - entry, \ - mode, \ - object_statuses); \ -} - -#define PROXY_BULK_SET_ENTRY(OT,ot) \ - static sai_status_t proxy_bulk_set_ ## ot( \ - _In_ uint32_t object_count, \ - _In_ const sai_ ## ot ## _t *entry, \ - _In_ const sai_attribute_t *attr_list, \ - _In_ sai_bulk_op_error_mode_t mode, \ - _Out_ sai_status_t *object_statuses) \ -{ \ - SWSS_LOG_ENTER(); \ - return proxy_sai->bulkSet( \ - object_count, \ - entry, \ - attr_list, \ - mode, \ - object_statuses); \ -} - -#define PROXY_BULK_GET_ENTRY(OT,ot) \ - static sai_status_t proxy_bulk_get_ ## ot( \ - _In_ uint32_t object_count, \ - _In_ const sai_ ## ot ## _t *entry, \ - _In_ const uint32_t *attr_count, \ - _Inout_ sai_attribute_t **attr_list, \ - _In_ sai_bulk_op_error_mode_t mode, \ - _Out_ sai_status_t *object_statuses) \ -{ \ - SWSS_LOG_ENTER(); \ - SWSS_LOG_ERROR("not implemented"); \ - return SAI_STATUS_NOT_IMPLEMENTED; \ -} - -// BULK QUAD ENTRY DECLARE - -#define PROXY_BULK_QUAD_ENTRY(OT,ot) \ - PROXY_BULK_CREATE_ENTRY(OT,ot); \ - PROXY_BULK_REMOVE_ENTRY(OT,ot); \ - PROXY_BULK_SET_ENTRY(OT,ot); \ - PROXY_BULK_GET_ENTRY(OT,ot); - -// BULK QUAD API - -#define PROXY_BULK_QUAD_API(ot) \ - proxy_bulk_create_ ## ot, \ - proxy_bulk_remove_ ## ot, \ - proxy_bulk_set_ ## ot, \ - proxy_bulk_get_ ## ot, - -// BULK get/set DECLARE - -#define PROXY_BULK_GET_SET(OT,ot) \ - PROXY_BULK_GET(OT,ot); \ - PROXY_BULK_SET(OT,ot); - -// BULK get/set API - -#define PROXY_BULK_GET_SET_API(ot) \ - proxy_bulk_get_ ## ot, \ - proxy_bulk_set_ ## ot, diff --git a/unittest/proxylib/Makefile.am b/unittest/proxylib/Makefile.am index b1b8b73a7..a7c5d8ac6 100644 --- a/unittest/proxylib/Makefile.am +++ b/unittest/proxylib/Makefile.am @@ -7,6 +7,7 @@ LDADD_GTEST = -L/usr/src/gtest -lgtest -lgtest_main tests_SOURCES = \ ../../meta/DummySaiInterface.cpp \ main.cpp \ + TestProxy.cpp \ TestSai.cpp tests_CXXFLAGS = $(DBGFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS_COMMON) diff --git a/unittest/proxylib/TestProxy.cpp b/unittest/proxylib/TestProxy.cpp new file mode 100644 index 000000000..00060593b --- /dev/null +++ b/unittest/proxylib/TestProxy.cpp @@ -0,0 +1,25 @@ +#include + +#include +#include +#include + +#include + +#include "meta/DummySaiInterface.h" + +#include "Sai.h" +#include "Proxy.h" + +using namespace saiproxy; + +TEST(Proxy, ctr) +{ + Sai sai; + + std::shared_ptr dummy = std::make_shared(); + + // will test loadProfileMap + + auto proxy = std::make_shared(dummy); +} diff --git a/unittest/proxylib/config.ini b/unittest/proxylib/config.ini new file mode 100644 index 000000000..da5334b45 --- /dev/null +++ b/unittest/proxylib/config.ini @@ -0,0 +1,8 @@ +# test proxu config comment +; comment + +invalid line + +VARIABLE=VALUE +FOO=bar +BAR=baz