From f9393a8c15fbd63b7a0938269afa8a35ddfc4738 Mon Sep 17 00:00:00 2001 From: Andrej Kozemcak Date: Thu, 14 Feb 2019 11:53:05 +0100 Subject: Move bapi to scvpp plugin. Change-Id: I87be68ddad4827d6dfa04aad5ea725efae97157a Signed-off-by: Andrej Kozemcak --- src/plugins/CMakeLists.txt | 13 +- src/plugins/bapi/bapi.c | 104 --------- src/plugins/bapi/bapi.h | 84 -------- src/plugins/bapi/bapi_interface.c | 252 ---------------------- src/plugins/bapi/bapi_interface.h | 52 ----- src/plugins/bapi/bapi_ip.c | 219 ------------------- src/plugins/bapi/bapi_ip.h | 46 ---- src/plugins/openconfig/openconfig_interfaces.c | 28 +-- src/plugins/openconfig/openconfig_interfaces.h | 6 +- src/plugins/openconfig/openconfig_local_routing.c | 26 +-- src/plugins/openconfig/openconfig_local_routing.h | 6 +- src/scvpp/src/CMakeLists.txt | 4 + src/scvpp/src/sc_vpp_comm.c | 47 ++++ src/scvpp/src/sc_vpp_comm.h | 49 +++++ src/scvpp/src/sc_vpp_interface.c | 252 ++++++++++++++++++++++ src/scvpp/src/sc_vpp_interface.h | 52 +++++ src/scvpp/src/sc_vpp_ip.c | 222 +++++++++++++++++++ src/scvpp/src/sc_vpp_ip.h | 46 ++++ 18 files changed, 707 insertions(+), 801 deletions(-) delete mode 100644 src/plugins/bapi/bapi.c delete mode 100644 src/plugins/bapi/bapi.h delete mode 100644 src/plugins/bapi/bapi_interface.c delete mode 100644 src/plugins/bapi/bapi_interface.h delete mode 100644 src/plugins/bapi/bapi_ip.c delete mode 100644 src/plugins/bapi/bapi_ip.h create mode 100644 src/scvpp/src/sc_vpp_interface.c create mode 100644 src/scvpp/src/sc_vpp_interface.h create mode 100644 src/scvpp/src/sc_vpp_ip.c create mode 100644 src/scvpp/src/sc_vpp_ip.h diff --git a/src/plugins/CMakeLists.txt b/src/plugins/CMakeLists.txt index 23c120f..a54ebf0 100644 --- a/src/plugins/CMakeLists.txt +++ b/src/plugins/CMakeLists.txt @@ -42,16 +42,6 @@ if (NOT SR_PLUGINS_DIR) message(FATAL_ERROR "Cannot get sysrepo plugins directory due to missing pkg-config, set SR_PLUGINS_DIR manually.") endif() -set(BAPI_SRC - ./bapi/bapi.c - ./bapi/bapi_interface.c - ./bapi/bapi_ip.c -) - -add_library(bapi SHARED ${BAPI_SRC}) -target_include_directories(bapi PUBLIC ${VPP_INCLUDE_DIRS} ./bapi) -target_link_libraries(bapi ${VPP_LIBRARIES}) - # plugins sources set(PLUGINS_SOURCES sc_plugins.c @@ -64,8 +54,7 @@ set(PLUGINS_SOURCES # build the source code into shared library add_library(vpp-plugins SHARED ${PLUGINS_SOURCES}) -target_link_libraries(vpp-plugins ${SYSREPO_LIBRARIES} ${SCVPP_LIBRARIES} bapi) +target_link_libraries(vpp-plugins ${SYSREPO_LIBRARIES} ${SCVPP_LIBRARIES}) # install the plugin into plugins dir install(TARGETS vpp-plugins DESTINATION ${SR_PLUGINS_DIR}) -install(TARGETS bapi DESTINATION ${CMAKE_INSTALL_LIBDIR}) diff --git a/src/plugins/bapi/bapi.c b/src/plugins/bapi/bapi.c deleted file mode 100644 index 49107ff..0000000 --- a/src/plugins/bapi/bapi.c +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Copyright (c) 2018 PANTHEON.tech. - * - * 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 "bapi.h" - -#include -#include -#include -#include -#include - -DEFINE_VAPI_MSG_IDS_VPE_API_JSON - -static char *api_prefix = NULL; -static const int max_outstanding_requests = 64; -static const int response_queue_size = 32; - -vapi_ctx_t g_vapi_ctx; -vapi_mode_e g_vapi_mode = VAPI_MODE_NONBLOCKING; - -/// -/// Connect to binary api. -/// -vapi_error_e bin_api_connect(const char *client_name, vapi_mode_e vapi_mode) { - vapi_error_e rv = vapi_ctx_alloc(&g_vapi_ctx); - if (VAPI_OK != rv) { - SC_LOG_DBG_MSG("cannot allocate context"); - return rv; - } - - rv = vapi_connect (g_vapi_ctx, client_name, api_prefix, max_outstanding_requests, - response_queue_size, vapi_mode, true); - - if (VAPI_OK != rv) { - SC_LOG_DBG_MSG("error: connecting to vlib"); - vapi_ctx_free(g_vapi_ctx); - return rv; - } - - return rv; -} - -/// -/// Disconnect from binary api. -/// -vapi_error_e bin_api_disconnect(void) { - vapi_error_e rv = vapi_disconnect (g_vapi_ctx); - if (VAPI_OK != rv) { - SC_LOG_DBG("error: (rc:%d)", rv); - //return rv; - } - - vapi_ctx_free (g_vapi_ctx); - - return rv; -} - -bool bapi_aton(const char *cp, u8 * buf) -{ - ARG_CHECK2(false, cp, buf); - - struct in_addr addr; - int ret = inet_aton(cp, &addr); - - if (0 == ret) - { - SC_LOG_DBG("error: ipv4 address %s", cp); - return false; - } - - memcpy(buf, &addr, sizeof (addr)); - return true; -} - -char* bapi_ntoa(u8 * buf) -{ - struct in_addr addr; - memcpy(&addr, buf, sizeof(addr)); - return inet_ntoa(addr); -} - -vapi_error_e -vapi_retval_cb(const char* func_name, i32 retval) -{ - if (retval) - { - SC_LOG_DBG("%s: bad retval=%d", func_name, retval); - } - - return VAPI_OK; -} diff --git a/src/plugins/bapi/bapi.h b/src/plugins/bapi/bapi.h deleted file mode 100644 index eeebe7a..0000000 --- a/src/plugins/bapi/bapi.h +++ /dev/null @@ -1,84 +0,0 @@ -/* - * Copyright (c) 2018 PANTHEON.tech. - * - * 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. - */ - - -#ifndef __BAPI_H__ -#define __BAPI_H__ - - -#include "sc_vpp_comm.h" -#include -#include -#include - -/**********************************GLOBALS**********************************/ -extern vapi_ctx_t g_vapi_ctx; -extern vapi_mode_e g_vapi_mode; - -/**********************************MACROS**********************************/ -#define VAPI_RETVAL_CB(api_name) \ -vapi_error_e \ -api_name##_cb (vapi_ctx_t ctx, void *caller_ctx, vapi_error_e rv, bool is_last, \ - vapi_payload_##api_name##_reply * reply) \ -{ \ - return vapi_retval_cb(__FUNCTION__, reply->retval); \ -} - -#define VAPI_COPY_CB(api_name) \ -vapi_error_e \ -api_name##_cb (vapi_ctx_t ctx, void *caller_ctx, vapi_error_e rv, bool is_last, \ - vapi_payload_##api_name##_reply * reply) \ -{ \ - if (caller_ctx) \ - { \ - vapi_payload_##api_name##_reply * passed = (vapi_payload_##api_name##_reply *)caller_ctx; \ - *passed = *reply; \ - } \ - return VAPI_OK; \ -}\ - -#define VAPI_CALL_MODE(call_code, vapi_mode) \ - do \ - { \ - if (VAPI_MODE_BLOCKING == (vapi_mode)) \ - { \ - rv = call_code; \ - } \ - else \ - { \ - while (VAPI_EAGAIN == (rv = call_code)); \ - rv = vapi_dispatch (g_vapi_ctx); \ - } \ - } \ - while (0) - -#define VAPI_CALL(call_code) VAPI_CALL_MODE(call_code, g_vapi_mode) - - -/**********************************FUNCTIONS**********************************/ -extern vapi_error_e bin_api_connect(const char *client_name, vapi_mode_e mode); -extern vapi_error_e bin_api_disconnect(void); - - -//returns true on success -bool bapi_aton(const char *cp, u8 * buf); -char * bapi_ntoa(u8 * buf); - -vapi_error_e -vapi_retval_cb(const char* func_name, i32 retval); - - -#endif //__BAPI_H__ diff --git a/src/plugins/bapi/bapi_interface.c b/src/plugins/bapi/bapi_interface.c deleted file mode 100644 index 5e035fa..0000000 --- a/src/plugins/bapi/bapi_interface.c +++ /dev/null @@ -1,252 +0,0 @@ -/* - * Copyright (c) 2018 PANTHEON.tech. - * - * 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 "bapi_interface.h" - -#include -#include -#include - -#include "bapi.h" - -DEFINE_VAPI_MSG_IDS_INTERFACE_API_JSON - -void sw_interface_details_query_set_name(sw_interface_details_query_t * query, - const char * interface_name) -{ - assert(query && interface_name); - - memset(query, 0, sizeof(*query)); - - strncpy((char*) query->sw_interface_details.interface_name, interface_name, - sizeof(query->sw_interface_details.interface_name)); -} - -static vapi_error_e -get_interface_id_cb (struct vapi_ctx_s *ctx, void *callback_ctx, - vapi_error_e rv, bool is_last, - vapi_payload_sw_interface_details * reply) -{ - sw_interface_details_query_t *dctx = callback_ctx; - assert(dctx); - - if (!dctx->interface_found) - { - if (is_last) - { - assert(NULL == reply); - } - else - { - assert(NULL != reply); - SC_LOG_DBG("Interface dump entry: [%u]: %s\n", reply->sw_if_index, - reply->interface_name); - - if (0 == strcmp((const char*)dctx->sw_interface_details.interface_name, - (const char*)reply->interface_name)) - { - dctx->interface_found = true; - dctx->sw_interface_details = *reply; - } - } - } - - return VAPI_OK; -} - -bool get_interface_id(sw_interface_details_query_t * sw_interface_details_query) -{ - ARG_CHECK(false, sw_interface_details_query); - sw_interface_details_query->interface_found = false; - - vapi_msg_sw_interface_dump *mp = vapi_alloc_sw_interface_dump (g_vapi_ctx); - assert(NULL != mp); - - mp->payload.name_filter_valid = true; - memcpy(mp->payload.name_filter, sw_interface_details_query->sw_interface_details.interface_name, - sizeof(mp->payload.name_filter)); - - vapi_error_e rv; - VAPI_CALL(vapi_sw_interface_dump(g_vapi_ctx, mp, get_interface_id_cb, sw_interface_details_query)); - - if (VAPI_OK != rv) { - SC_LOG_DBG_MSG("vapi_sw_interface_dump"); - return false; - } - - if (!sw_interface_details_query->interface_found) - SC_LOG_ERR("interface name %s: Can't find index", - sw_interface_details_query->sw_interface_details.interface_name); - - return sw_interface_details_query->interface_found; -} - -static vapi_error_e -get_interface_name_cb (struct vapi_ctx_s *ctx, void *callback_ctx, - vapi_error_e rv, bool is_last, - vapi_payload_sw_interface_details * reply) -{ - sw_interface_details_query_t *dctx = callback_ctx; - assert(dctx); - - if (!dctx->interface_found) - { - if (is_last) - { - assert(NULL == reply); - } - else - { - assert(reply && dctx); - SC_LOG_DBG("Interface dump entry: [%u]: %s\n", reply->sw_if_index, reply->interface_name); - - if (dctx->sw_interface_details.sw_if_index == reply->sw_if_index) - { - dctx->interface_found = true; - dctx->sw_interface_details = *reply; - } - } - } - - return VAPI_OK; -} - -bool get_interface_name(sw_interface_details_query_t * sw_interface_details_query) -{ - ARG_CHECK(false, sw_interface_details_query); - sw_interface_details_query->interface_found = false; - - vapi_msg_sw_interface_dump *mp = vapi_alloc_sw_interface_dump (g_vapi_ctx); - assert(NULL != mp); - - vapi_error_e rv; - VAPI_CALL(vapi_sw_interface_dump (g_vapi_ctx, mp, get_interface_name_cb, sw_interface_details_query)); - - if (VAPI_OK != rv) { - SC_LOG_DBG_MSG("vapi_sw_interface_dump"); - return false; - } - - if (!sw_interface_details_query->interface_found) - SC_LOG_ERR("interface index %u: Can't find name", - sw_interface_details_query->sw_interface_details.sw_if_index); - - return sw_interface_details_query->interface_found; -} - -static vapi_error_e -sw_interface_dump_cb (struct vapi_ctx_s *ctx, void *callback_ctx, - vapi_error_e rv, bool is_last, - vapi_payload_sw_interface_details * reply) -{ - if (is_last) - { - assert (NULL == reply); - } - else - { - assert (NULL != reply); - SC_LOG_DBG("Interface dump entry: [%u]: %s\n", reply->sw_if_index, - reply->interface_name); - } - - return VAPI_OK; -} - -vapi_error_e bin_api_sw_interface_dump(const char * interface_name) -{ - vapi_msg_sw_interface_dump *mp = vapi_alloc_sw_interface_dump (g_vapi_ctx); - assert(NULL != mp); - - if (NULL != interface_name) - { - mp->payload.name_filter_valid = true; - strncpy((char *)mp->payload.name_filter, interface_name, sizeof(mp->payload.name_filter)); - } - else - { - mp->payload.name_filter_valid = false; - memset(mp->payload.name_filter, 0, sizeof (mp->payload.name_filter)); - } - - vapi_error_e rv; - VAPI_CALL(vapi_sw_interface_dump (g_vapi_ctx, mp, sw_interface_dump_cb, NULL)); - - return rv; -} - - -VAPI_RETVAL_CB(sw_interface_set_flags); - -vapi_error_e bin_api_sw_interface_set_flags(uint32_t if_index, uint8_t up) -{ - vapi_msg_sw_interface_set_flags *mp = vapi_alloc_sw_interface_set_flags (g_vapi_ctx); - assert(NULL != mp); - - mp->payload.sw_if_index = if_index; - mp->payload.admin_up_down = up; - - vapi_error_e rv; - VAPI_CALL(vapi_sw_interface_set_flags(g_vapi_ctx, mp, sw_interface_set_flags_cb, NULL)); - - return rv; -} - -VAPI_RETVAL_CB(sw_interface_set_l2_bridge); - -vapi_error_e bin_api_sw_interface_set_l2_bridge(u32 bd_id, u32 rx_sw_if_index, - bool enable) -{ - vapi_msg_sw_interface_set_l2_bridge *mp = - vapi_alloc_sw_interface_set_l2_bridge (g_vapi_ctx); - assert(NULL != mp); - //set interface l2 bridge [bvi|uu-fwd] [shg] - /* - typedef struct __attribute__ ((__packed__)) { - u32 rx_sw_if_index; - u32 bd_id; - vapi_enum_l2_port_type port_type; - u8 shg; - u8 enable; - } vapi_payload_sw_interface_set_l2_bridge; - */ - mp->payload.enable = enable; - mp->payload.bd_id = bd_id; - mp->payload.rx_sw_if_index = rx_sw_if_index; - - vapi_error_e rv; - VAPI_CALL(vapi_sw_interface_set_l2_bridge (g_vapi_ctx, mp, - sw_interface_set_l2_bridge_cb, NULL)); - - return rv; -} - -VAPI_COPY_CB(create_loopback) - -vapi_error_e bin_api_create_loopback(vapi_payload_create_loopback_reply *reply) -{ - ARG_CHECK(VAPI_EINVAL, reply); - - vapi_msg_create_loopback *mp = vapi_alloc_create_loopback (g_vapi_ctx); - assert(NULL != mp); - - //mp->payload.mac_address = - - vapi_error_e rv; - VAPI_CALL(vapi_create_loopback (g_vapi_ctx, mp, create_loopback_cb, reply)); - - return rv; -} diff --git a/src/plugins/bapi/bapi_interface.h b/src/plugins/bapi/bapi_interface.h deleted file mode 100644 index 45cc2ef..0000000 --- a/src/plugins/bapi/bapi_interface.h +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright (c) 2018 PANTHEON.tech. - * - * 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. - */ - -#ifndef __BAPI_INTERFACE_H__ -#define __BAPI_INTERFACE_H__ - - -#include - - -typedef struct { - bool interface_found; - vapi_payload_sw_interface_details sw_interface_details; -} sw_interface_details_query_t; - -extern void sw_interface_details_query_set_name(sw_interface_details_query_t * query, - const char * interface_name); - -//input - sw_interface_details_query shall contain interface_name -extern bool get_interface_id(sw_interface_details_query_t * sw_interface_details_query); - -//input - sw_interface_details_query shall contain sw_if_index -extern bool get_interface_name(sw_interface_details_query_t * sw_interface_details_query); - - - -extern vapi_error_e bin_api_sw_interface_dump(const char * interface_name); - -extern vapi_error_e bin_api_sw_interface_set_flags(u32 if_index, u8 up); - -extern vapi_error_e bin_api_sw_interface_set_l2_bridge(u32 bd_id, - u32 rx_sw_if_index, - bool enable); - -extern vapi_error_e bin_api_create_loopback( - vapi_payload_create_loopback_reply *reply); - - -#endif /* __BAPI_INTERFACE_H__ */ diff --git a/src/plugins/bapi/bapi_ip.c b/src/plugins/bapi/bapi_ip.c deleted file mode 100644 index aa8baa5..0000000 --- a/src/plugins/bapi/bapi_ip.c +++ /dev/null @@ -1,219 +0,0 @@ -/* - * Copyright (c) 2018 PANTHEON.tech. - * - * 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 "bapi_ip.h" -#include "bapi.h" - -#include "bapi_interface.h" - -#include - -DEFINE_VAPI_MSG_IDS_IP_API_JSON - -VAPI_RETVAL_CB(sw_interface_add_del_address); - -vapi_error_e -bin_api_sw_interface_add_del_address(u32 sw_if_index, bool is_add, - const char * ip_address, u8 address_length) -{ - ARG_CHECK(VAPI_EINVAL, ip_address); - - vapi_msg_sw_interface_add_del_address *mp = - vapi_alloc_sw_interface_add_del_address (g_vapi_ctx); - assert(NULL != mp); - - mp->payload.sw_if_index = sw_if_index; - mp->payload.is_add = is_add; - mp->payload.is_ipv6 = 0; - mp->payload.address_length = address_length; - if (!bapi_aton(ip_address, mp->payload.address)) - return VAPI_EINVAL; - - vapi_error_e rv; - VAPI_CALL(vapi_sw_interface_add_del_address (g_vapi_ctx, mp, sw_interface_add_del_address_cb, NULL)); - - return rv; -} - -vapi_error_e -bin_api_sw_interface_del_all_address(u32 sw_if_index) -{ - vapi_msg_sw_interface_add_del_address *mp = - vapi_alloc_sw_interface_add_del_address (g_vapi_ctx); - assert(NULL != mp); - - mp->payload.sw_if_index = sw_if_index; - mp->payload.is_add = 0; - mp->payload.del_all = 1; - - vapi_error_e rv; - VAPI_CALL(vapi_sw_interface_add_del_address (g_vapi_ctx, mp, - sw_interface_add_del_address_cb, NULL)); - - return rv; -} - -VAPI_COPY_CB(ip_add_del_route) - -vapi_error_e bin_api_ip_add_del_route( - vapi_payload_ip_add_del_route_reply * reply, - const char* dst_address, - uint8_t dst_address_length, - const char* next_hop, - uint8_t is_add, - uint32_t table_id, - const char *interface_name) -{ - ARG_CHECK4(VAPI_EINVAL, reply, dst_address, next_hop, interface_name); - - SC_LOG_DBG("Interface: %s", interface_name); - - sw_interface_details_query_t query = {0}; - sw_interface_details_query_set_name(&query, interface_name); - - if (!get_interface_id(&query)) - { - return VAPI_EINVAL; - } - - vapi_msg_ip_add_del_route *mp = vapi_alloc_ip_add_del_route (g_vapi_ctx, 1); - assert(NULL != mp); - - //ip route add 2.2.2.2/24 via 5.5.5.5 - //show ip fib table 0 2.2.2.0/24 detail - - mp->payload.is_add = is_add; - mp->payload.dst_address_length = dst_address_length; - mp->payload.table_id = table_id; - mp->payload.next_hop_sw_if_index = query.sw_interface_details.sw_if_index; - SC_LOG_DBG("Interface: %s, index: %d", interface_name, query.sw_interface_details.sw_if_index); - - if (!bapi_aton(dst_address, mp->payload.dst_address)) - return VAPI_EINVAL; - if (!bapi_aton(next_hop, mp->payload.next_hop_address)) - return VAPI_EINVAL; - - vapi_error_e rv ; - VAPI_CALL(vapi_ip_add_del_route(g_vapi_ctx, mp, ip_add_del_route_cb, reply)); - - return rv; -} - -static vapi_error_e -ip_fib_dump_cb (struct vapi_ctx_s *ctx, void *callback_ctx, - vapi_error_e rv, bool is_last, - vapi_payload_ip_fib_details * reply) -{ - if (is_last) - { - assert (NULL == reply); - } - else - { - assert (NULL != reply); - - /* - typedef struct __attribute__ ((__packed__)) { - u32 table_id; - u8 table_name[64]; - u8 address_length; - u8 address[4]; - u32 count; - u32 stats_index; - vapi_type_fib_path path[0]; - } vapi_payload_ip_fib_details; - */ - printf ("ip_fib_dump_cb table %u details: network %s/%u\n", - reply->table_id, bapi_ntoa(reply->address), reply->address_length); - - for (u32 i = 0; i < reply->count; ++i) - { - /* - typedef struct __attribute__((__packed__)) { - u32 sw_if_index; - u32 table_id; - u8 weight; - u8 preference; - u8 is_local; - u8 is_drop; - u8 is_udp_encap; - u8 is_unreach; - u8 is_prohibit; - u8 is_resolve_host; - u8 is_resolve_attached; - u8 is_dvr; - u8 is_source_lookup; - u8 afi; - u8 next_hop[16]; - u32 next_hop_id; - u32 rpf_id; - u32 via_label; - u8 n_labels; - vapi_type_fib_mpls_label label_stack[16]; - } vapi_type_fib_path; - - │493 case FIB_PATH_TYPE_ATTACHED_NEXT_HOP: │ -b+>│494 s = format (s, "%U", format_ip46_address, │ - │495 &path->attached_next_hop.fp_nh, │ - │496 IP46_TYPE_ANY); - - */ - - printf("\tnext hop: %s\n", bapi_ntoa(reply->path[i].next_hop)); - } - } - - return VAPI_OK; -} - -vapi_error_e -bin_api_ip_fib_dump() -{ - vapi_msg_ip_fib_dump *mp = vapi_alloc_ip_fib_dump (g_vapi_ctx); - assert(NULL != mp); - - //ip route add 2.2.2.2/24 via 5.5.5.5 - //show ip fib table 0 2.2.2.0/24 detail - - vapi_error_e rv; - VAPI_CALL(vapi_ip_fib_dump(g_vapi_ctx, mp, ip_fib_dump_cb, NULL)); - - return rv; -} - -VAPI_RETVAL_CB(ip_table_add_del) - -vapi_error_e -bin_api_table_add_del(u8 is_add, u32 table_id) -{ - vapi_msg_ip_table_add_del *mp = vapi_alloc_ip_table_add_del(g_vapi_ctx); - assert(NULL != mp); - - mp->payload.is_add = is_add; - mp->payload.table_id = table_id; - - vapi_error_e rv; - VAPI_CALL(vapi_ip_table_add_del(g_vapi_ctx, mp, ip_table_add_del_cb, NULL)); - - return rv; -} - - - - - - - diff --git a/src/plugins/bapi/bapi_ip.h b/src/plugins/bapi/bapi_ip.h deleted file mode 100644 index 0940fe8..0000000 --- a/src/plugins/bapi/bapi_ip.h +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright (c) 2018 PANTHEON.tech. - * - * 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. - */ - - -#ifndef __BAPI_IP_H__ -#define __BAPI_IP_H__ - -#include -#include - - -extern vapi_error_e bin_api_sw_interface_add_del_address( - u32 sw_if_index, - bool is_add, - const char * ip_address, - u8 address_length); - -extern vapi_error_e bin_api_sw_interface_del_all_address(u32 sw_if_index); - -//ip_add_del_route -extern vapi_error_e bin_api_ip_add_del_route( - vapi_payload_ip_add_del_route_reply * reply, - const char* dst_address, - u8 dst_address_length, - const char* next_address, - u8 is_add, - u32 table_id, - const char *interface); - -extern vapi_error_e bin_api_ip_fib_dump(); -extern vapi_error_e bin_api_table_add_del(u8 is_add, u32 table_id); - -#endif /* __BAPI_IP_H__ */ diff --git a/src/plugins/openconfig/openconfig_interfaces.c b/src/plugins/openconfig/openconfig_interfaces.c index df975b6..e401e07 100644 --- a/src/plugins/openconfig/openconfig_interfaces.c +++ b/src/plugins/openconfig/openconfig_interfaces.c @@ -17,10 +17,8 @@ #include "openconfig_interfaces.h" #include "sys_util.h" #include "sc_vpp_comm.h" - -#include "../bapi/bapi.h" -#include "../bapi/bapi_interface.h" -#include "../bapi/bapi_ip.h" +#include "sc_vpp_interface.h" +#include "sc_vpp_ip.h" #include #include @@ -211,7 +209,7 @@ int openconfig_interface_mod_cb( __attribute__((unused)) sr_notif_event_t event, __attribute__((unused)) void *private_ctx) { - SRP_LOG_DBG("Inerafce module subscribe: %s", module_name); + SRP_LOG_INF("Module subscribe: %s", module_name); return SR_ERR_OK; } @@ -437,13 +435,13 @@ static vapi_error_e sysr_sw_interface_dump(sys_sw_interface_dump_ctx * dctx) { ARG_CHECK(VAPI_EINVAL, dctx); - vapi_msg_sw_interface_dump *dump = vapi_alloc_sw_interface_dump(g_vapi_ctx); + vapi_msg_sw_interface_dump *dump = vapi_alloc_sw_interface_dump(g_vapi_ctx_instance); vapi_error_e rv; dump->payload.name_filter_valid = true; strcpy((char*)dump->payload.name_filter, (const char *)dctx->sw_interface_details_query.sw_interface_details.interface_name); - VAPI_CALL(vapi_sw_interface_dump(g_vapi_ctx, dump, sw_interface_dump_vapi_cb, + VAPI_CALL(vapi_sw_interface_dump(g_vapi_ctx_instance, dump, sw_interface_dump_vapi_cb, dctx)); if (VAPI_OK != rv) { @@ -455,7 +453,9 @@ static vapi_error_e sysr_sw_interface_dump(sys_sw_interface_dump_ctx * dctx) int openconfig_interfaces_interfaces_interface_state_cb( const char *xpath, sr_val_t **values, - size_t *values_cnt, uint64_t request_id, + size_t *values_cnt, + __attribute__((unused)) uint64_t request_id, + __attribute__((unused)) const char *original_xpath, __attribute__((unused)) void *private_ctx) { sr_xpath_ctx_t state = {0}; @@ -515,7 +515,7 @@ int openconfig_interfaces_interfaces_interface_subinterfaces_subinterface_oc_ip_ return rc; } - char * address_ip = bapi_ntoa(reply->ip); + char * address_ip = sc_ntoa(reply->ip); sr_val_build_xpath(&vals[0], "%s/openconfig-if-ip:ip", sysr_values_ctx->xpath_root); @@ -556,7 +556,7 @@ ip_address_dump_cb (struct vapi_ctx_s *ctx, void *callback_ctx, printf ("ip address dump entry:" "\tsw_if_index[%u]" "\tip[%s/%u]\n" - , reply->sw_if_index, bapi_ntoa(reply->ip), + , reply->sw_if_index, sc_ntoa(reply->ip), reply->prefix_length); openconfig_interfaces_interfaces_interface_subinterfaces_subinterface_oc_ip_ipv4_oc_ip_addresses_oc_ip_address_oc_ip_state_vapi_cb(reply, dctx); @@ -569,6 +569,7 @@ ip_address_dump_cb (struct vapi_ctx_s *ctx, void *callback_ctx, int openconfig_interfaces_interfaces_interface_subinterfaces_subinterface_oc_ip_ipv4_oc_ip_addresses_oc_ip_address_oc_ip_state_cb( const char *xpath, sr_val_t **values, size_t *values_cnt, __attribute__((unused)) uint64_t request_id, + __attribute__((unused)) const char *original_xpath, __attribute__((unused)) void *private_ctx) { sr_xpath_ctx_t state = {0}; @@ -619,11 +620,11 @@ int openconfig_interfaces_interfaces_interface_subinterfaces_subinterface_oc_ip_ return SR_ERR_INVAL_ARG; } - vapi_msg_ip_address_dump *mp = vapi_alloc_ip_address_dump (g_vapi_ctx); + vapi_msg_ip_address_dump *mp = vapi_alloc_ip_address_dump (g_vapi_ctx_instance); mp->payload.sw_if_index = query.sw_interface_details.sw_if_index; mp->payload.is_ipv6 = 0; - rv = vapi_ip_address_dump(g_vapi_ctx, mp, ip_address_dump_cb, &dctx); + rv = vapi_ip_address_dump(g_vapi_ctx_instance, mp, ip_address_dump_cb, &dctx); if (VAPI_OK != rv) { SRP_LOG_ERR_MSG("VAPI call failed"); @@ -640,6 +641,7 @@ int openconfig_interfaces_interfaces_interface_subinterfaces_subinterface_oc_ip_ int openconfig_interfaces_interfaces_interface_subinterfaces_subinterface_state_cb( const char *xpath, sr_val_t **values, size_t *values_cnt, __attribute__((unused)) uint64_t request_id, + __attribute__((unused)) const char *original_xpath, __attribute__((unused)) void *private_ctx) { sr_xpath_ctx_t state = {0}; @@ -692,7 +694,7 @@ int openconfig_interfaces_interfaces_interface_subinterfaces_subinterface_state_ return SR_ERR_OK; } -VAPI_RETVAL_CB(sw_interface_add_del_address); +// VAPI_RETVAL_CB(sw_interface_add_del_address); static int oi_int_ipv4_conf(const char *interface_name, const char *address_ip, u8 prefix_length, diff --git a/src/plugins/openconfig/openconfig_interfaces.h b/src/plugins/openconfig/openconfig_interfaces.h index e64b3e2..df5e31c 100644 --- a/src/plugins/openconfig/openconfig_interfaces.h +++ b/src/plugins/openconfig/openconfig_interfaces.h @@ -31,15 +31,15 @@ int openconfig_interfaces_interfaces_interface_config_cb( int openconfig_interfaces_interfaces_interface_subinterfaces_subinterface_oc_ip_ipv4_oc_ip_addresses_oc_ip_address_oc_ip_state_cb( const char *xpath, sr_val_t **values, size_t *values_cnt, - uint64_t request_id, void *private_ctx); + uint64_t request_id, const char *original_xpath, void *private_ctx); int openconfig_interfaces_interfaces_interface_subinterfaces_subinterface_state_cb( const char *xpath, sr_val_t **values, size_t *values_cnt, - uint64_t request_id, void *private_ctx); + uint64_t request_id, const char *original_xpath, void *private_ctx); int openconfig_interfaces_interfaces_interface_state_cb( const char *xpath, sr_val_t **values, size_t *values_cnt, - uint64_t request_id, void *private_ctx); + uint64_t request_id, const char *original_xpath, void *private_ctx); int openconfig_interfaces_interfaces_interface_subinterfaces_subinterface_oc_ip_ipv4_oc_ip_addresses_oc_ip_address_oc_ip_config_cb( sr_session_ctx_t *ds, const char *xpath, sr_notif_event_t event, diff --git a/src/plugins/openconfig/openconfig_local_routing.c b/src/plugins/openconfig/openconfig_local_routing.c index 4c3a0fd..daf39cc 100644 --- a/src/plugins/openconfig/openconfig_local_routing.c +++ b/src/plugins/openconfig/openconfig_local_routing.c @@ -17,10 +17,8 @@ #include "openconfig_local_routing.h" #include "sys_util.h" #include "sc_vpp_comm.h" - -#include "../bapi/bapi.h" -#include "../bapi/bapi_interface.h" -#include "../bapi/bapi_ip.h" +#include "sc_vpp_interface.h" +#include "sc_vpp_ip.h" #include #include @@ -420,7 +418,8 @@ bool address_prefix_init(address_prefix_t* address_prefix, char* str_prefix) return false; } - return bapi_aton(str_prefix, address_prefix->address); + return sc_aton(str_prefix, address_prefix->address, + sizeof(address_prefix->address)); } // XPATH: /openconfig-local-routing:local-routes/static-routes/static/state @@ -443,7 +442,7 @@ int openconfig_local_routing_local_routes_static_routes_static_state_vapi_cb( //Filling the structure snprintf(address_prefix, sizeof(address_prefix), "%s/%u", - bapi_ntoa(reply->address), reply->address_length); + sc_ntoa(reply->address), reply->address_length); sr_val_build_xpath(&vals[0], "%s/prefix", sysr_ip_fib_details_ctx->sysr_values_ctx.xpath_root); @@ -482,6 +481,7 @@ ip_routing_dump_cb (struct vapi_ctx_s *ctx, void *callback_ctx, int openconfig_local_routing_local_routes_static_routes_static_state_cb( const char *xpath, sr_val_t **values, size_t *values_cnt, __attribute__((unused)) uint64_t request_id, + __attribute__((unused)) const char *original_xpath, __attribute__((unused)) void *private_ctx) { sr_xpath_ctx_t state = {0}; @@ -510,9 +510,9 @@ int openconfig_local_routing_local_routes_static_routes_static_state_cb( } - vapi_msg_ip_fib_dump *mp = vapi_alloc_ip_fib_dump (g_vapi_ctx); + vapi_msg_ip_fib_dump *mp = vapi_alloc_ip_fib_dump (g_vapi_ctx_instance); - VAPI_CALL(vapi_ip_fib_dump(g_vapi_ctx, mp, ip_routing_dump_cb, &dctx)); + VAPI_CALL(vapi_ip_fib_dump(g_vapi_ctx_instance, mp, ip_routing_dump_cb, &dctx)); if(VAPI_OK != rv) { SRP_LOG_ERR_MSG("VAPI call failed"); @@ -550,7 +550,7 @@ int openconfig_local_routing_local_routes_static_routes_static_next_hops_next_ho sr_val_set_str_data(&vals[0], SR_STRING_T, sysr_ip_fib_details_ctx->next_hop_index); - strncpy(next_hop, bapi_ntoa(reply->next_hop), sizeof(next_hop)); + strncpy(next_hop, sc_ntoa(reply->next_hop), sizeof(next_hop)); sr_val_build_xpath(&vals[1], "%s/next-hop", sysr_ip_fib_details_ctx->sysr_values_ctx.xpath_root); sr_val_set_str_data(&vals[1], SR_STRING_T, next_hop); @@ -683,8 +683,8 @@ int next_hop_inner( return SR_ERR_INVAL_ARG; } - vapi_msg_ip_fib_dump *mp = vapi_alloc_ip_fib_dump (g_vapi_ctx); - VAPI_CALL(vapi_ip_fib_dump(g_vapi_ctx, mp, ip_routing_next_hop_dump_cb, &dctx)); + vapi_msg_ip_fib_dump *mp = vapi_alloc_ip_fib_dump (g_vapi_ctx_instance); + VAPI_CALL(vapi_ip_fib_dump(g_vapi_ctx_instance, mp, ip_routing_next_hop_dump_cb, &dctx)); if (VAPI_OK != rv) { SRP_LOG_ERR_MSG("VAPI call failed"); @@ -715,7 +715,7 @@ int next_hop_inner( int openconfig_local_routing_local_routes_static_routes_static_next_hops_next_hop_state_cb( const char *xpath, sr_val_t **values, size_t *values_cnt, - uint64_t request_id, void *private_ctx) + uint64_t request_id, const char *original_xpath, void *private_ctx) { return next_hop_inner(false, xpath, values, values_cnt, request_id, private_ctx); @@ -723,7 +723,7 @@ int openconfig_local_routing_local_routes_static_routes_static_next_hops_next_ho int openconfig_local_routing_local_routes_static_routes_static_next_hops_next_hop_interface_ref_state_cb( const char *xpath, sr_val_t **values, size_t *values_cnt, - uint64_t request_id, void *private_ctx) + uint64_t request_id, const char *original_xpath, void *private_ctx) { return next_hop_inner(true, xpath, values, values_cnt, request_id, private_ctx); diff --git a/src/plugins/openconfig/openconfig_local_routing.h b/src/plugins/openconfig/openconfig_local_routing.h index 4cde542..4cd2966 100644 --- a/src/plugins/openconfig/openconfig_local_routing.h +++ b/src/plugins/openconfig/openconfig_local_routing.h @@ -34,14 +34,14 @@ int openconfig_local_routing_local_routes_static_routes_static_next_hops_next_ho int openconfig_local_routing_local_routes_static_routes_static_state_cb( const char *xpath, sr_val_t **values, size_t *values_cnt, - uint64_t request_id, void *private_ctx); + uint64_t request_id, const char *original_xpath, void *private_ctx); int openconfig_local_routing_local_routes_static_routes_static_next_hops_next_hop_state_cb( const char *xpath, sr_val_t **values, size_t *values_cnt, - uint64_t request_id, void *private_ctx); + uint64_t request_id, const char *original_xpath, void *private_ctx); int openconfig_local_routing_local_routes_static_routes_static_next_hops_next_hop_interface_ref_state_cb( const char *xpath, sr_val_t **values, size_t *values_cnt, - uint64_t request_id, void *private_ctx); + uint64_t request_id, const char *original_xpath, void *private_ctx); #endif /* __OPENCONFIG_LOCAL_ROUTING_H__ */ diff --git a/src/scvpp/src/CMakeLists.txt b/src/scvpp/src/CMakeLists.txt index 1a90220..c2a2c40 100644 --- a/src/scvpp/src/CMakeLists.txt +++ b/src/scvpp/src/CMakeLists.txt @@ -18,11 +18,15 @@ include(GNUInstallDirs) # scvpp sources set(SCVPP_SOURCES sc_vpp_comm.c + sc_vpp_interface.c + sc_vpp_ip.c ) # scvpp public headers set(SCVPP_HEADERS sc_vpp_comm.h + sc_vpp_interface.h + sc_vpp_ip.h ) set(CMAKE_C_FLAGS " -g -O0 -fpic -fPIC -std=gnu99 -Wl,-rpath-link=/usr/lib") diff --git a/src/scvpp/src/sc_vpp_comm.c b/src/scvpp/src/sc_vpp_comm.c index 2ebdb7a..32e0303 100644 --- a/src/scvpp/src/sc_vpp_comm.c +++ b/src/scvpp/src/sc_vpp_comm.c @@ -14,11 +14,18 @@ */ #include "sc_vpp_comm.h" +#include +#include +#include +#include +#include + #define APP_NAME "sweetcomb_vpp" #define MAX_OUTSTANDING_REQUESTS 4 #define RESPONSE_QUEUE_SIZE 2 vapi_ctx_t g_vapi_ctx_instance = NULL; +vapi_mode_e g_vapi_mode = VAPI_MODE_NONBLOCKING; ////////////////////////// int sc_connect_vpp() @@ -72,4 +79,44 @@ int sc_end_with(const char* str, const char* end) return 0; } +bool sc_aton(const char *cp, u8 * buf, size_t length) +{ + ARG_CHECK2(false, cp, buf); + + struct in_addr addr; + int ret = inet_aton(cp, &addr); + + if (0 == ret) + { + SC_LOG_DBG("error: ipv4 address %s", cp); + return false; + } + + if (sizeof(addr) > length) { + SC_LOG_DBG_MSG("error: small buffer"); + return false; + } + + memcpy(buf, &addr, sizeof (addr)); + return true; +} + +char* sc_ntoa(const u8 * buf) +{ + ARG_CHECK(NULL, buf); + + struct in_addr addr; + memcpy(&addr, buf, sizeof(addr)); + return inet_ntoa(addr); +} +vapi_error_e +vapi_retval_cb(const char* func_name, i32 retval) +{ + if (retval) + { + SC_LOG_DBG("%s: bad retval=%d", func_name, retval); + } + + return VAPI_OK; +} diff --git a/src/scvpp/src/sc_vpp_comm.h b/src/scvpp/src/sc_vpp_comm.h index 3799571..ceb3a80 100644 --- a/src/scvpp/src/sc_vpp_comm.h +++ b/src/scvpp/src/sc_vpp_comm.h @@ -17,6 +17,7 @@ #ifndef __SC_VPP_COMMM_H__ #define __SC_VPP_COMMM_H__ #include +#include #include DEFINE_VAPI_MSG_IDS_VPE_API_JSON; @@ -24,6 +25,8 @@ DEFINE_VAPI_MSG_IDS_VPE_API_JSON; #include #include //for SC_LOG_DBG +extern vapi_mode_e g_vapi_mode; + #define VPP_INTFC_NAME_LEN 64 #define VPP_TAPV2_NAME_LEN VPP_INTFC_NAME_LEN #define VPP_IP4_ADDRESS_LEN 4 @@ -61,6 +64,7 @@ DEFINE_VAPI_MSG_IDS_VPE_API_JSON; #define SC_INVOKE_END SC_LOG_DBG("inovke %s end,with return OK.",SC_THIS_FUNC); #define SC_INVOKE_ENDX(...) SC_LOG_DBG("inovke %s end,with %s.",SC_THIS_FUNC, ##__VA_ARGS__) +/**********************************MACROS**********************************/ #define ARG_CHECK(retval, arg) \ do \ { \ @@ -118,6 +122,51 @@ do { \ } \ } while(0); +#define VAPI_RETVAL_CB(api_name) \ +static vapi_error_e \ +api_name##_cb (vapi_ctx_t ctx, void *caller_ctx, vapi_error_e rv, bool is_last, \ + vapi_payload_##api_name##_reply * reply) \ +{ \ + return vapi_retval_cb(__FUNCTION__, reply->retval); \ +} + +#define VAPI_COPY_CB(api_name) \ +static vapi_error_e \ +api_name##_cb (vapi_ctx_t ctx, void *caller_ctx, vapi_error_e rv, bool is_last, \ + vapi_payload_##api_name##_reply * reply) \ +{ \ + if (caller_ctx) \ + { \ + vapi_payload_##api_name##_reply * passed = (vapi_payload_##api_name##_reply *)caller_ctx; \ + *passed = *reply; \ + } \ + return VAPI_OK; \ +}\ + +#define VAPI_CALL_MODE(call_code, vapi_mode) \ + do \ + { \ + if (VAPI_MODE_BLOCKING == (vapi_mode)) \ + { \ + rv = call_code; \ + } \ + else \ + { \ + while (VAPI_EAGAIN == (rv = call_code)); \ + rv = vapi_dispatch (g_vapi_ctx_instance); \ + } \ + } \ + while (0) + +#define VAPI_CALL(call_code) VAPI_CALL_MODE(call_code, g_vapi_mode) + +//returns true on success +bool sc_aton(const char *cp, u8 * buf, size_t length); +char * sc_ntoa(const u8 * buf); + +vapi_error_e +vapi_retval_cb(const char* func_name, i32 retval); + /////////////////////////// //VPP接口 int sc_connect_vpp(); diff --git a/src/scvpp/src/sc_vpp_interface.c b/src/scvpp/src/sc_vpp_interface.c new file mode 100644 index 0000000..333ddd7 --- /dev/null +++ b/src/scvpp/src/sc_vpp_interface.c @@ -0,0 +1,252 @@ +/* + * Copyright (c) 2018 PANTHEON.tech. + * + * 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 "sc_vpp_interface.h" + +#include +#include +#include + +#include "sc_vpp_comm.h" + +DEFINE_VAPI_MSG_IDS_INTERFACE_API_JSON + +void sw_interface_details_query_set_name(sw_interface_details_query_t * query, + const char * interface_name) +{ + assert(query && interface_name); + + memset(query, 0, sizeof(*query)); + + strncpy((char*) query->sw_interface_details.interface_name, interface_name, + sizeof(query->sw_interface_details.interface_name)); +} + +static vapi_error_e +get_interface_id_cb (struct vapi_ctx_s *ctx, void *callback_ctx, + vapi_error_e rv, bool is_last, + vapi_payload_sw_interface_details * reply) +{ + sw_interface_details_query_t *dctx = callback_ctx; + assert(dctx); + + if (!dctx->interface_found) + { + if (is_last) + { + assert(NULL == reply); + } + else + { + assert(NULL != reply); + SC_LOG_DBG("Interface dump entry: [%u]: %s\n", reply->sw_if_index, + reply->interface_name); + + if (0 == strcmp((const char*)dctx->sw_interface_details.interface_name, + (const char*)reply->interface_name)) + { + dctx->interface_found = true; + dctx->sw_interface_details = *reply; + } + } + } + + return VAPI_OK; +} + +bool get_interface_id(sw_interface_details_query_t * sw_interface_details_query) +{ + ARG_CHECK(false, sw_interface_details_query); + sw_interface_details_query->interface_found = false; + + vapi_msg_sw_interface_dump *mp = vapi_alloc_sw_interface_dump (g_vapi_ctx_instance); + assert(NULL != mp); + + mp->payload.name_filter_valid = true; + memcpy(mp->payload.name_filter, sw_interface_details_query->sw_interface_details.interface_name, + sizeof(mp->payload.name_filter)); + + vapi_error_e rv; + VAPI_CALL(vapi_sw_interface_dump(g_vapi_ctx_instance, mp, get_interface_id_cb, sw_interface_details_query)); + + if (VAPI_OK != rv) { + SC_LOG_DBG_MSG("vapi_sw_interface_dump"); + return false; + } + + if (!sw_interface_details_query->interface_found) + SC_LOG_ERR("interface name %s: Can't find index", + sw_interface_details_query->sw_interface_details.interface_name); + + return sw_interface_details_query->interface_found; +} + +static vapi_error_e +get_interface_name_cb (struct vapi_ctx_s *ctx, void *callback_ctx, + vapi_error_e rv, bool is_last, + vapi_payload_sw_interface_details * reply) +{ + sw_interface_details_query_t *dctx = callback_ctx; + assert(dctx); + + if (!dctx->interface_found) + { + if (is_last) + { + assert(NULL == reply); + } + else + { + assert(reply && dctx); + SC_LOG_DBG("Interface dump entry: [%u]: %s\n", reply->sw_if_index, reply->interface_name); + + if (dctx->sw_interface_details.sw_if_index == reply->sw_if_index) + { + dctx->interface_found = true; + dctx->sw_interface_details = *reply; + } + } + } + + return VAPI_OK; +} + +bool get_interface_name(sw_interface_details_query_t * sw_interface_details_query) +{ + ARG_CHECK(false, sw_interface_details_query); + sw_interface_details_query->interface_found = false; + + vapi_msg_sw_interface_dump *mp = vapi_alloc_sw_interface_dump (g_vapi_ctx_instance); + assert(NULL != mp); + + vapi_error_e rv; + VAPI_CALL(vapi_sw_interface_dump (g_vapi_ctx_instance, mp, get_interface_name_cb, sw_interface_details_query)); + + if (VAPI_OK != rv) { + SC_LOG_DBG_MSG("vapi_sw_interface_dump"); + return false; + } + + if (!sw_interface_details_query->interface_found) + SC_LOG_ERR("interface index %u: Can't find name", + sw_interface_details_query->sw_interface_details.sw_if_index); + + return sw_interface_details_query->interface_found; +} + +static vapi_error_e +sw_interface_dump_cb (struct vapi_ctx_s *ctx, void *callback_ctx, + vapi_error_e rv, bool is_last, + vapi_payload_sw_interface_details * reply) +{ + if (is_last) + { + assert (NULL == reply); + } + else + { + assert (NULL != reply); + SC_LOG_DBG("Interface dump entry: [%u]: %s\n", reply->sw_if_index, + reply->interface_name); + } + + return VAPI_OK; +} + +vapi_error_e bin_api_sw_interface_dump(const char * interface_name) +{ + vapi_msg_sw_interface_dump *mp = vapi_alloc_sw_interface_dump (g_vapi_ctx_instance); + assert(NULL != mp); + + if (NULL != interface_name) + { + mp->payload.name_filter_valid = true; + strncpy((char *)mp->payload.name_filter, interface_name, sizeof(mp->payload.name_filter)); + } + else + { + mp->payload.name_filter_valid = false; + memset(mp->payload.name_filter, 0, sizeof (mp->payload.name_filter)); + } + + vapi_error_e rv; + VAPI_CALL(vapi_sw_interface_dump (g_vapi_ctx_instance, mp, sw_interface_dump_cb, NULL)); + + return rv; +} + + +VAPI_RETVAL_CB(sw_interface_set_flags); + +vapi_error_e bin_api_sw_interface_set_flags(uint32_t if_index, uint8_t up) +{ + vapi_msg_sw_interface_set_flags *mp = vapi_alloc_sw_interface_set_flags (g_vapi_ctx_instance); + assert(NULL != mp); + + mp->payload.sw_if_index = if_index; + mp->payload.admin_up_down = up; + + vapi_error_e rv; + VAPI_CALL(vapi_sw_interface_set_flags(g_vapi_ctx_instance, mp, sw_interface_set_flags_cb, NULL)); + + return rv; +} + +VAPI_RETVAL_CB(sw_interface_set_l2_bridge); + +vapi_error_e bin_api_sw_interface_set_l2_bridge(u32 bd_id, u32 rx_sw_if_index, + bool enable) +{ + vapi_msg_sw_interface_set_l2_bridge *mp = + vapi_alloc_sw_interface_set_l2_bridge (g_vapi_ctx_instance); + assert(NULL != mp); + //set interface l2 bridge [bvi|uu-fwd] [shg] + /* + typedef struct __attribute__ ((__packed__)) { + u32 rx_sw_if_index; + u32 bd_id; + vapi_enum_l2_port_type port_type; + u8 shg; + u8 enable; + } vapi_payload_sw_interface_set_l2_bridge; + */ + mp->payload.enable = enable; + mp->payload.bd_id = bd_id; + mp->payload.rx_sw_if_index = rx_sw_if_index; + + vapi_error_e rv; + VAPI_CALL(vapi_sw_interface_set_l2_bridge (g_vapi_ctx_instance, mp, + sw_interface_set_l2_bridge_cb, NULL)); + + return rv; +} + +VAPI_COPY_CB(create_loopback) + +vapi_error_e bin_api_create_loopback(vapi_payload_create_loopback_reply *reply) +{ + ARG_CHECK(VAPI_EINVAL, reply); + + vapi_msg_create_loopback *mp = vapi_alloc_create_loopback (g_vapi_ctx_instance); + assert(NULL != mp); + + //mp->payload.mac_address = + + vapi_error_e rv; + VAPI_CALL(vapi_create_loopback (g_vapi_ctx_instance, mp, create_loopback_cb, reply)); + + return rv; +} diff --git a/src/scvpp/src/sc_vpp_interface.h b/src/scvpp/src/sc_vpp_interface.h new file mode 100644 index 0000000..45cc2ef --- /dev/null +++ b/src/scvpp/src/sc_vpp_interface.h @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2018 PANTHEON.tech. + * + * 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. + */ + +#ifndef __BAPI_INTERFACE_H__ +#define __BAPI_INTERFACE_H__ + + +#include + + +typedef struct { + bool interface_found; + vapi_payload_sw_interface_details sw_interface_details; +} sw_interface_details_query_t; + +extern void sw_interface_details_query_set_name(sw_interface_details_query_t * query, + const char * interface_name); + +//input - sw_interface_details_query shall contain interface_name +extern bool get_interface_id(sw_interface_details_query_t * sw_interface_details_query); + +//input - sw_interface_details_query shall contain sw_if_index +extern bool get_interface_name(sw_interface_details_query_t * sw_interface_details_query); + + + +extern vapi_error_e bin_api_sw_interface_dump(const char * interface_name); + +extern vapi_error_e bin_api_sw_interface_set_flags(u32 if_index, u8 up); + +extern vapi_error_e bin_api_sw_interface_set_l2_bridge(u32 bd_id, + u32 rx_sw_if_index, + bool enable); + +extern vapi_error_e bin_api_create_loopback( + vapi_payload_create_loopback_reply *reply); + + +#endif /* __BAPI_INTERFACE_H__ */ diff --git a/src/scvpp/src/sc_vpp_ip.c b/src/scvpp/src/sc_vpp_ip.c new file mode 100644 index 0000000..a09bf3a --- /dev/null +++ b/src/scvpp/src/sc_vpp_ip.c @@ -0,0 +1,222 @@ +/* + * Copyright (c) 2018 PANTHEON.tech. + * + * 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 "sc_vpp_comm.h" +#include "sc_vpp_ip.h" + +#include "sc_vpp_interface.h" + +#include + +DEFINE_VAPI_MSG_IDS_IP_API_JSON + +VAPI_RETVAL_CB(sw_interface_add_del_address); + +vapi_error_e +bin_api_sw_interface_add_del_address(u32 sw_if_index, bool is_add, + const char * ip_address, u8 address_length) +{ + ARG_CHECK(VAPI_EINVAL, ip_address); + + vapi_msg_sw_interface_add_del_address *mp = + vapi_alloc_sw_interface_add_del_address (g_vapi_ctx_instance); + assert(NULL != mp); + + mp->payload.sw_if_index = sw_if_index; + mp->payload.is_add = is_add; + mp->payload.is_ipv6 = 0; + mp->payload.address_length = address_length; + if (!sc_aton(ip_address, mp->payload.address, + sizeof(mp->payload.address))) + return VAPI_EINVAL; + + vapi_error_e rv; + VAPI_CALL(vapi_sw_interface_add_del_address (g_vapi_ctx_instance, mp, sw_interface_add_del_address_cb, NULL)); + + return rv; +} + +vapi_error_e +bin_api_sw_interface_del_all_address(u32 sw_if_index) +{ + vapi_msg_sw_interface_add_del_address *mp = + vapi_alloc_sw_interface_add_del_address (g_vapi_ctx_instance); + assert(NULL != mp); + + mp->payload.sw_if_index = sw_if_index; + mp->payload.is_add = 0; + mp->payload.del_all = 1; + + vapi_error_e rv; + VAPI_CALL(vapi_sw_interface_add_del_address (g_vapi_ctx_instance, mp, + sw_interface_add_del_address_cb, NULL)); + + return rv; +} + +VAPI_COPY_CB(ip_add_del_route) + +vapi_error_e bin_api_ip_add_del_route( + vapi_payload_ip_add_del_route_reply * reply, + const char* dst_address, + uint8_t dst_address_length, + const char* next_hop, + uint8_t is_add, + uint32_t table_id, + const char *interface_name) +{ + ARG_CHECK4(VAPI_EINVAL, reply, dst_address, next_hop, interface_name); + + SC_LOG_DBG("Interface: %s", interface_name); + + sw_interface_details_query_t query = {0}; + sw_interface_details_query_set_name(&query, interface_name); + + if (!get_interface_id(&query)) + { + return VAPI_EINVAL; + } + + vapi_msg_ip_add_del_route *mp = vapi_alloc_ip_add_del_route (g_vapi_ctx_instance, 1); + assert(NULL != mp); + + //ip route add 2.2.2.2/24 via 5.5.5.5 + //show ip fib table 0 2.2.2.0/24 detail + + mp->payload.is_add = is_add; + mp->payload.dst_address_length = dst_address_length; + mp->payload.table_id = table_id; + mp->payload.next_hop_sw_if_index = query.sw_interface_details.sw_if_index; + SC_LOG_DBG("Interface: %s, index: %d", interface_name, query.sw_interface_details.sw_if_index); + + if (!sc_aton(dst_address, mp->payload.dst_address, + sizeof(mp->payload.dst_address))) + return VAPI_EINVAL; + if (!sc_aton(next_hop, mp->payload.next_hop_address, + sizeof(mp->payload.next_hop_address))) + return VAPI_EINVAL; + + vapi_error_e rv ; + VAPI_CALL(vapi_ip_add_del_route(g_vapi_ctx_instance, mp, ip_add_del_route_cb, reply)); + + return rv; +} + +static vapi_error_e +ip_fib_dump_cb (struct vapi_ctx_s *ctx, void *callback_ctx, + vapi_error_e rv, bool is_last, + vapi_payload_ip_fib_details * reply) +{ + if (is_last) + { + assert (NULL == reply); + } + else + { + assert (NULL != reply); + + /* + typedef struct __attribute__ ((__packed__)) { + u32 table_id; + u8 table_name[64]; + u8 address_length; + u8 address[4]; + u32 count; + u32 stats_index; + vapi_type_fib_path path[0]; + } vapi_payload_ip_fib_details; + */ + printf ("ip_fib_dump_cb table %u details: network %s/%u\n", + reply->table_id, sc_ntoa(reply->address), reply->address_length); + + for (u32 i = 0; i < reply->count; ++i) + { + /* + typedef struct __attribute__((__packed__)) { + u32 sw_if_index; + u32 table_id; + u8 weight; + u8 preference; + u8 is_local; + u8 is_drop; + u8 is_udp_encap; + u8 is_unreach; + u8 is_prohibit; + u8 is_resolve_host; + u8 is_resolve_attached; + u8 is_dvr; + u8 is_source_lookup; + u8 afi; + u8 next_hop[16]; + u32 next_hop_id; + u32 rpf_id; + u32 via_label; + u8 n_labels; + vapi_type_fib_mpls_label label_stack[16]; + } vapi_type_fib_path; + + │493 case FIB_PATH_TYPE_ATTACHED_NEXT_HOP: │ +b+>│494 s = format (s, "%U", format_ip46_address, │ + │495 &path->attached_next_hop.fp_nh, │ + │496 IP46_TYPE_ANY); + + */ + + printf("\tnext hop: %s\n", sc_ntoa(reply->path[i].next_hop)); + } + } + + return VAPI_OK; +} + +vapi_error_e +bin_api_ip_fib_dump() +{ + vapi_msg_ip_fib_dump *mp = vapi_alloc_ip_fib_dump (g_vapi_ctx_instance); + assert(NULL != mp); + + //ip route add 2.2.2.2/24 via 5.5.5.5 + //show ip fib table 0 2.2.2.0/24 detail + + vapi_error_e rv; + VAPI_CALL(vapi_ip_fib_dump(g_vapi_ctx_instance, mp, ip_fib_dump_cb, NULL)); + + return rv; +} + +VAPI_RETVAL_CB(ip_table_add_del) + +vapi_error_e +bin_api_table_add_del(u8 is_add, u32 table_id) +{ + vapi_msg_ip_table_add_del *mp = vapi_alloc_ip_table_add_del(g_vapi_ctx_instance); + assert(NULL != mp); + + mp->payload.is_add = is_add; + mp->payload.table_id = table_id; + + vapi_error_e rv; + VAPI_CALL(vapi_ip_table_add_del(g_vapi_ctx_instance, mp, ip_table_add_del_cb, NULL)); + + return rv; +} + + + + + + + diff --git a/src/scvpp/src/sc_vpp_ip.h b/src/scvpp/src/sc_vpp_ip.h new file mode 100644 index 0000000..0940fe8 --- /dev/null +++ b/src/scvpp/src/sc_vpp_ip.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2018 PANTHEON.tech. + * + * 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. + */ + + +#ifndef __BAPI_IP_H__ +#define __BAPI_IP_H__ + +#include +#include + + +extern vapi_error_e bin_api_sw_interface_add_del_address( + u32 sw_if_index, + bool is_add, + const char * ip_address, + u8 address_length); + +extern vapi_error_e bin_api_sw_interface_del_all_address(u32 sw_if_index); + +//ip_add_del_route +extern vapi_error_e bin_api_ip_add_del_route( + vapi_payload_ip_add_del_route_reply * reply, + const char* dst_address, + u8 dst_address_length, + const char* next_address, + u8 is_add, + u32 table_id, + const char *interface); + +extern vapi_error_e bin_api_ip_fib_dump(); +extern vapi_error_e bin_api_table_add_del(u8 is_add, u32 table_id); + +#endif /* __BAPI_IP_H__ */ -- cgit 1.2.3-korg