From ce39d8887388a158b86c198ac55a50632cc5decd Mon Sep 17 00:00:00 2001 From: Mauro Sardara Date: Fri, 20 Jan 2023 14:54:19 +0000 Subject: feat: implement set strategy API for hicn-plugin Ticket: HICN-829 Change-Id: I3dabc38e9cc2e06ebed14b9ed265d027f77b7e5f Signed-off-by: Mauro Sardara (cherry picked from commit da0d60997bce1e40cc0b1c7b7f4f58df56bbead3) --- ctrl/libhicnctrl/src/modules/CMakeLists.txt | 8 +- ctrl/libhicnctrl/src/modules/hicn_plugin.c | 3 +- .../libhicnctrl/src/modules/hicn_plugin/strategy.c | 118 +++++++++++++++++++++ .../libhicnctrl/src/modules/hicn_plugin/strategy.h | 28 +++++ 4 files changed, 153 insertions(+), 4 deletions(-) create mode 100644 ctrl/libhicnctrl/src/modules/hicn_plugin/strategy.c create mode 100644 ctrl/libhicnctrl/src/modules/hicn_plugin/strategy.h (limited to 'ctrl/libhicnctrl') diff --git a/ctrl/libhicnctrl/src/modules/CMakeLists.txt b/ctrl/libhicnctrl/src/modules/CMakeLists.txt index b1b63a5f9..f7bd2f83a 100644 --- a/ctrl/libhicnctrl/src/modules/CMakeLists.txt +++ b/ctrl/libhicnctrl/src/modules/CMakeLists.txt @@ -66,12 +66,14 @@ if(BUILD_HICNPLUGIN AND ${CMAKE_SYSTEM_NAME} MATCHES "Linux") ${CMAKE_CURRENT_SOURCE_DIR}/hicn_plugin.c ${CMAKE_CURRENT_SOURCE_DIR}/hicn_plugin/listener.c ${CMAKE_CURRENT_SOURCE_DIR}/hicn_plugin/route.c + ${CMAKE_CURRENT_SOURCE_DIR}/hicn_plugin/strategy.c ) list(APPEND HICN_PLUGIN_HEADER_FILES - ${CMAKE_CURRENT_SOURCE_DIR}/hicn_light/base.h - ${CMAKE_CURRENT_SOURCE_DIR}/hicn_light/listener.h - ${CMAKE_CURRENT_SOURCE_DIR}/hicn_light/route.h + ${CMAKE_CURRENT_SOURCE_DIR}/hicn_plugin/base.h + ${CMAKE_CURRENT_SOURCE_DIR}/hicn_plugin/listener.h + ${CMAKE_CURRENT_SOURCE_DIR}/hicn_plugin/route.h + ${CMAKE_CURRENT_SOURCE_DIR}/hicn_plugin/strategy.h ) ############################################################## diff --git a/ctrl/libhicnctrl/src/modules/hicn_plugin.c b/ctrl/libhicnctrl/src/modules/hicn_plugin.c index b3963b46c..b8606daf7 100644 --- a/ctrl/libhicnctrl/src/modules/hicn_plugin.c +++ b/ctrl/libhicnctrl/src/modules/hicn_plugin.c @@ -39,6 +39,7 @@ #include "hicn_plugin/base.h" // hc_sock_vpp_data_t #include "hicn_plugin/listener.h" #include "hicn_plugin/route.h" +#include "hicn_plugin/strategy.h" /****************************************************************************** * Message helper types and aliases @@ -241,7 +242,7 @@ int hc_sock_initialize_module(hc_sock_t *s) { s->ops.object_vft[OBJECT_TYPE_WLDR] = HC_MODULE_OBJECT_OPS_EMPTY; s->ops.object_vft[OBJECT_TYPE_POLICY] = HC_MODULE_OBJECT_OPS_EMPTY; s->ops.object_vft[OBJECT_TYPE_ROUTE] = vpp_route_module_ops; - s->ops.object_vft[OBJECT_TYPE_STRATEGY] = HC_MODULE_OBJECT_OPS_EMPTY; + s->ops.object_vft[OBJECT_TYPE_STRATEGY] = vpp_strategy_module_ops; s->ops.object_vft[OBJECT_TYPE_SUBSCRIPTION] = HC_MODULE_OBJECT_OPS_EMPTY; return 0; } diff --git a/ctrl/libhicnctrl/src/modules/hicn_plugin/strategy.c b/ctrl/libhicnctrl/src/modules/hicn_plugin/strategy.c new file mode 100644 index 000000000..f8f6536ff --- /dev/null +++ b/ctrl/libhicnctrl/src/modules/hicn_plugin/strategy.c @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2023 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "base.h" +#include "strategy.h" + +#include + +static int _ip_prefix_encode(const hicn_ip_address_t *address, + int prefix_length, int family, + vapi_type_prefix *out) { + out->len = prefix_length; + int ret = 0; + + switch (family) { + case AF_INET: + memcpy(&out->address.un.ip4[0], &address->v4, 4); + out->address.af = ADDRESS_IP4; + break; + case AF_INET6: + memcpy(&out->address.un.ip6[0], &address->v6, 16); + out->address.af = ADDRESS_IP6; + break; + default: + // This should never happen + ret = -1; + } + + return ret; +} + +static vapi_enum_hicn_strategy _vpp_strategy_libhicn_to_hicnplugin_strategy( + strategy_type_t strategy) { + switch (strategy) { + case STRATEGY_TYPE_LOADBALANCER: + return HICN_STRATEGY_RR; + case STRATEGY_TYPE_LOCAL_REMOTE: + return HICN_STRATEGY_LR; + case STRATEGY_TYPE_REPLICATION: + return HICN_STRATEGY_RP; + case STRATEGY_TYPE_BESTPATH: + return HICN_STRATEGY_MW; + default: + return HICN_STRATEGY_NULL; + } +} + +static vapi_error_e _hicn_strategy_set_cb( + vapi_ctx_t ctx, void *callback_ctx, vapi_error_e rv, bool is_last, + vapi_payload_hicn_api_strategy_set_reply *reply) { + if (reply == NULL || rv != VAPI_OK) return rv; + return reply->retval; +} + +static int _vpp_strategy_set(hc_sock_vpp_data_t *s, + const hc_strategy_t *strategy) { + int ret = -1; + + // Convert libhicn strategy enum to hicnplugin strategy enum and make sure it + // is valid + vapi_enum_hicn_strategy strategy_id = + _vpp_strategy_libhicn_to_hicnplugin_strategy(strategy->type); + + if (strategy_id == HICN_STRATEGY_NULL) { + return -1; + } + + // Construct API message + vapi_msg_hicn_api_strategy_set *msg = + vapi_alloc_hicn_api_strategy_set(s->g_vapi_ctx_instance); + + // Fill it + msg->payload.strategy_id = clib_host_to_net_u32(strategy_id); + ret = _ip_prefix_encode(&strategy->address, strategy->len, strategy->family, + &msg->payload.prefix); + + if (ret != 0) { + return -1; + } + + vapi_lock(); + ret = vapi_hicn_api_strategy_set(s->g_vapi_ctx_instance, msg, + _hicn_strategy_set_cb, NULL); + vapi_unlock(); + + return ret; +} + +int vpp_strategy_create(hc_sock_t *sock, hc_object_t *object, hc_data_t *data) { + return -1; +} + +int vpp_strategy_delete(hc_sock_t *sock, hc_object_t *object, hc_data_t *data) { + return -1; +} + +int vpp_strategy_list(hc_sock_t *sock, hc_object_t *object, hc_data_t *data) { + return -1; +} + +int vpp_strategy_set(hc_sock_t *sock, hc_object_t *object, hc_data_t *data) { + hc_sock_vpp_data_t *s = (hc_sock_vpp_data_t *)sock->data; + return _vpp_strategy_set(s, &object->strategy); +} + +DECLARE_VPP_MODULE_OBJECT_OPS(vpp, strategy); diff --git a/ctrl/libhicnctrl/src/modules/hicn_plugin/strategy.h b/ctrl/libhicnctrl/src/modules/hicn_plugin/strategy.h new file mode 100644 index 000000000..6dd6df6e4 --- /dev/null +++ b/ctrl/libhicnctrl/src/modules/hicn_plugin/strategy.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2023 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * \file modules/hicn_plugin/route.h + * \brief route object VFT for hicn_plugin. + */ + +#ifndef HICNCTRL_MODULE_VPP_STRATEGY_H +#define HICNCTRL_MODULE_VPP_STRATEGY_H + +#include "../../module.h" + +DECLARE_MODULE_OBJECT_OPS_H(vpp, strategy); + +#endif /* HICNCTRL_MODULE_VPP_STRATEGY_H */ -- cgit 1.2.3-korg