diff options
author | YohanPipereau <ypiperea@cisco.com> | 2019-02-28 17:21:51 +0100 |
---|---|---|
committer | YohanPipereau <ypiperea@cisco.com> | 2019-03-05 16:36:48 +0100 |
commit | 3430b042873f2eac94acd323125eaaa4a5f7f3e9 (patch) | |
tree | 45335d45b94259f53426413bc5333a36dcfb8f07 /src/scvpp | |
parent | 793682e6fcb9e39891c59c696e5f8e9c18f9d8e7 (diff) |
Merge IETF and Openconfig to use SCVPP
scvpp should eventually be the only interface with VAPI and should not
depend on sysrepo.
-All sysrepo error codes in scvpp codes have been changed for errno error
codes. scvpp might eventually needs its own error codes.
-All log messages in scvpp have been removed as error codes are the only
way of reporting failures in a library.
-Move VAPI symbol definition to SCVPP.
In scvpp, unused maccros SC_VPP_VAPI_RECV and SC_REGISTER_RPC_EVT_HANDLER have
been removed.
Regarding plugins update:
-Use Openconfig way to convert interface name to interface index.
-Use Openconfig way to enable/disable an interface.
-Use Openconfig way of configuring interface IPs but use more arguments like
IETF.
-Use Openconfig way of adding a new route.
-Use Openconfig way of dumping an IP.
-Use common interface dump operation for get_name and get_id.
-Delete unused create loopback
Change-Id: Icc513a064a2528c2b4cbda2b0dd57755a3b08ef9
Signed-off-by: Yohan Pipereau <ypiperea@cisco.com>
Diffstat (limited to 'src/scvpp')
-rw-r--r-- | src/scvpp/src/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/scvpp/src/sc_vpp_comm.c | 100 | ||||
-rw-r--r-- | src/scvpp/src/sc_vpp_comm.h | 89 | ||||
-rw-r--r-- | src/scvpp/src/sc_vpp_interface.c | 250 | ||||
-rw-r--r-- | src/scvpp/src/sc_vpp_interface.h | 43 | ||||
-rw-r--r-- | src/scvpp/src/sc_vpp_ip.c | 177 | ||||
-rw-r--r-- | src/scvpp/src/sc_vpp_ip.h | 28 |
7 files changed, 359 insertions, 330 deletions
diff --git a/src/scvpp/src/CMakeLists.txt b/src/scvpp/src/CMakeLists.txt index c2a2c40..baa03ed 100644 --- a/src/scvpp/src/CMakeLists.txt +++ b/src/scvpp/src/CMakeLists.txt @@ -32,7 +32,7 @@ set(SCVPP_HEADERS set(CMAKE_C_FLAGS " -g -O0 -fpic -fPIC -std=gnu99 -Wl,-rpath-link=/usr/lib") # libraries to link with -set(LINK_LIBRARIES sysrepo vlibmemoryclient vapiclient vppapiclient svm vppinfra pthread rt dl) +set(LINK_LIBRARIES vlibmemoryclient vapiclient vppapiclient svm vppinfra pthread rt dl) # build instructions add_library(scvpp SHARED ${SCVPP_SOURCES}) diff --git a/src/scvpp/src/sc_vpp_comm.c b/src/scvpp/src/sc_vpp_comm.c index 32e0303..241b4ce 100644 --- a/src/scvpp/src/sc_vpp_comm.c +++ b/src/scvpp/src/sc_vpp_comm.c @@ -26,60 +26,53 @@ vapi_ctx_t g_vapi_ctx_instance = NULL; vapi_mode_e g_vapi_mode = VAPI_MODE_NONBLOCKING; -////////////////////////// int sc_connect_vpp() { - SC_INVOKE_BEGIN; - // SC_LOG_DBG("*******cts %p \n", g_vapi_ctx_instance); - if (g_vapi_ctx_instance == NULL) - { - vapi_error_e rv = vapi_ctx_alloc(&g_vapi_ctx_instance); - rv = vapi_connect(g_vapi_ctx_instance, APP_NAME, NULL, MAX_OUTSTANDING_REQUESTS, RESPONSE_QUEUE_SIZE, VAPI_MODE_BLOCKING, true); - if (rv != VAPI_OK) - { - SC_LOG_ERR("*connect %s failed with error code %d", APP_NAME, rv); - vapi_ctx_free(g_vapi_ctx_instance); - g_vapi_ctx_instance = NULL; - return -1; - } - SC_LOG_DBG("*connected %s ok", APP_NAME); - } - else - { - SC_LOG_DBG("connection %s keeping", APP_NAME); - } - SC_INVOKE_END; - return 0; + if (g_vapi_ctx_instance == NULL) + { + vapi_error_e rv = vapi_ctx_alloc(&g_vapi_ctx_instance); + rv = vapi_connect(g_vapi_ctx_instance, APP_NAME, NULL, + MAX_OUTSTANDING_REQUESTS, RESPONSE_QUEUE_SIZE, + VAPI_MODE_BLOCKING, true); + if (rv != VAPI_OK) + { + vapi_ctx_free(g_vapi_ctx_instance); + g_vapi_ctx_instance = NULL; + return -1; + } + } + + return 0; } int sc_disconnect_vpp() { - if (NULL != g_vapi_ctx_instance) - { - vapi_disconnect(g_vapi_ctx_instance); - vapi_ctx_free(g_vapi_ctx_instance); - g_vapi_ctx_instance = NULL; - } - return 0; + if (NULL != g_vapi_ctx_instance) + { + vapi_disconnect(g_vapi_ctx_instance); + vapi_ctx_free(g_vapi_ctx_instance); + g_vapi_ctx_instance = NULL; + } + return 0; } int sc_end_with(const char* str, const char* end) { - if (str != NULL && end != NULL) - { - int l1 = strlen(str); - int l2 = strlen(end); - if (l1 >= l2) - { - if (strcmp(str + l1 - l2, end) == 0) - return 1; - } - } - return 0; + if (str != NULL && end != NULL) + { + int l1 = strlen(str); + int l2 = strlen(end); + if (l1 >= l2) + { + if (strcmp(str + l1 - l2, end) == 0) + return 1; + } + } + return 0; } -bool sc_aton(const char *cp, u8 * buf, size_t length) +int sc_aton(const char *cp, u8 * buf, size_t length) { ARG_CHECK2(false, cp, buf); @@ -87,18 +80,14 @@ bool sc_aton(const char *cp, u8 * buf, size_t length) int ret = inet_aton(cp, &addr); if (0 == ret) - { - SC_LOG_DBG("error: ipv4 address %s", cp); - return false; - } + return -EINVAL; - if (sizeof(addr) > length) { - SC_LOG_DBG_MSG("error: small buffer"); - return false; - } + if (sizeof(addr) > length) + return -EINVAL; memcpy(buf, &addr, sizeof (addr)); - return true; + + return 0; } char* sc_ntoa(const u8 * buf) @@ -109,14 +98,3 @@ char* sc_ntoa(const u8 * buf) 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 ceb3a80..649295d 100644 --- a/src/scvpp/src/sc_vpp_comm.h +++ b/src/scvpp/src/sc_vpp_comm.h @@ -16,16 +16,15 @@ #ifndef __SC_VPP_COMMM_H__ #define __SC_VPP_COMMM_H__ + +#include <errno.h> + #include <vapi/vapi.h> #include <vapi/vapi_common.h> #include <vapi/vpe.api.vapi.h> -DEFINE_VAPI_MSG_IDS_VPE_API_JSON; -#include <sysrepo.h> -#include <sysrepo/values.h> -#include <sysrepo/plugins.h> //for SC_LOG_DBG - -extern vapi_mode_e g_vapi_mode; +// Use VAPI macros to define symbols +DEFINE_VAPI_MSG_IDS_VPE_API_JSON; #define VPP_INTFC_NAME_LEN 64 #define VPP_TAPV2_NAME_LEN VPP_INTFC_NAME_LEN @@ -39,38 +38,12 @@ extern vapi_mode_e g_vapi_mode; #define VPP_IKEV2_PSK_LEN VPP_INTFC_NAME_LEN #define VPP_IKEV2_ID_LEN 32 -#ifndef SC_THIS_FUNC -#ifdef __FUNCTION__ -#define SC_THIS_FUNC __FUNCTION__ -#else -#define SC_THIS_FUNC __func__ -#endif -#endif - -#ifndef SC_NOLOG -#define SC_LOG_DBG SRP_LOG_DBG -#define SC_LOG_ERR SRP_LOG_ERR -#define SC_LOG_DBG_MSG SRP_LOG_DBG_MSG -#define SC_LOG_ERR_MSG SRP_LOG_ERR_MSG -#else -#define SC_LOG_DBG //printf -#define SC_LOG_DBG //SRP_LOG_DBG -#define SC_LOG_ERR //SRP_LOG_ERR -#define SC_LOG_DBG_MSG //SRP_LOG_DBG_MSG -#define SC_LOG_ERR_MSG //SRP_LOG_ERR_MSG -#endif - -#define SC_INVOKE_BEGIN SC_LOG_DBG("inovke %s bein.",SC_THIS_FUNC); -#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 \ { \ if (NULL == (arg)) \ { \ - SC_LOG_ERR_MSG(#arg ":NULL pointer passed."); \ return (retval); \ } \ } \ @@ -91,43 +64,12 @@ extern vapi_mode_e g_vapi_mode; ARG_CHECK(retval, arg3); \ ARG_CHECK(retval, arg4) -/** - * when use tihs must fist DEFINE_VAPI_MSG_IDS_VXLAN_API_JSON - */ -#define SC_VPP_VAPI_RECV \ -do { \ - size_t size; \ - int recv_vapimsgid = -1; \ - rv = vapi_recv (g_vapi_ctx_instance, (void *) &resp, &size, 0, 0); \ - recv_vapimsgid = vapi_lookup_vapi_msg_id_t(g_vapi_ctx_instance, ntohs(resp->header._vl_msg_id) ); \ - if(recv_vapimsgid <= vapi_msg_id_get_next_index_reply \ - || recv_vapimsgid >= vapi_get_message_count ()) { \ - SC_LOG_DBG("***recv error msgid[%d] not in [0-%d) ,try again!***\n", \ - recv_vapimsgid, vapi_get_message_count ()); \ - } else { \ - SC_LOG_DBG("recv msgid [%d]\n", recv_vapimsgid); \ - break; \ - } \ - } while(1); - -#define SC_REGISTER_RPC_EVT_HANDLER(rpc_evt_handle) \ -do { \ - sr_error_t rc = rpc_evt_handle(session, &subscription); \ - if (SR_ERR_OK != rc) \ - { \ - SC_LOG_ERR("load plugin failed: %s", sr_strerror(rc)); \ - sr_unsubscribe(session, subscription); \ - SC_INVOKE_ENDX(sr_strerror(rc)); \ - return rc; \ - } \ -} 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); \ + return reply->retval; \ } #define VAPI_COPY_CB(api_name) \ @@ -160,21 +102,18 @@ api_name##_cb (vapi_ctx_t ctx, void *caller_ctx, vapi_error_e rv, bool is_last, #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); +int 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 + */ + +extern vapi_ctx_t g_vapi_ctx_instance; +extern vapi_mode_e g_vapi_mode; -/////////////////////////// -//VPP接口 int sc_connect_vpp(); int sc_disconnect_vpp(); int sc_end_with(const char* str, const char* end); -extern vapi_ctx_t g_vapi_ctx_instance; -#endif //__SC_VPP_COMMM_H__ - - - +#endif //__SC_VPP_COMMM_H__ diff --git a/src/scvpp/src/sc_vpp_interface.c b/src/scvpp/src/sc_vpp_interface.c index 333ddd7..73b29ae 100644 --- a/src/scvpp/src/sc_vpp_interface.c +++ b/src/scvpp/src/sc_vpp_interface.c @@ -14,15 +14,18 @@ * limitations under the License. */ -#include "sc_vpp_interface.h" - #include <assert.h> #include <stdbool.h> + #include <vapi/l2.api.vapi.h> #include "sc_vpp_comm.h" +#include "sc_vpp_interface.h" + +// Use VAPI macros to define symbols DEFINE_VAPI_MSG_IDS_INTERFACE_API_JSON +DEFINE_VAPI_MSG_IDS_L2_API_JSON; void sw_interface_details_query_set_name(sw_interface_details_query_t * query, const char * interface_name) @@ -36,66 +39,109 @@ void sw_interface_details_query_set_name(sw_interface_details_query_t * query, } 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_dump_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); + vapi_payload_sw_interface_details *passed; - 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); + ARG_CHECK(-EINVAL, callback_ctx); - 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; - } - } - } + passed = (vapi_payload_sw_interface_details *) callback_ctx; + + //Interface is found if index of query equals index of reply + if (passed->sw_if_index != reply->sw_if_index) + return -EINVAL; + + //copy + *passed = *reply; return VAPI_OK; } -bool get_interface_id(sw_interface_details_query_t * sw_interface_details_query) +static vapi_error_e +bin_api_sw_interface_dump(vapi_payload_sw_interface_details *details) { - ARG_CHECK(false, sw_interface_details_query); - sw_interface_details_query->interface_found = false; + vapi_msg_sw_interface_dump *mp; + vapi_error_e rv; - vapi_msg_sw_interface_dump *mp = vapi_alloc_sw_interface_dump (g_vapi_ctx_instance); + mp = vapi_alloc_sw_interface_dump(g_vapi_ctx_instance); + + mp->payload.name_filter_valid = 0; + memset(mp->payload.name_filter, 0, sizeof(mp->payload.name_filter)); 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_CALL(vapi_sw_interface_dump(g_vapi_ctx_instance, mp, sw_interface_dump_cb, details)); + if (!rv) + return -EINVAL; - vapi_error_e rv; - VAPI_CALL(vapi_sw_interface_dump(g_vapi_ctx_instance, mp, get_interface_id_cb, sw_interface_details_query)); + return rv; +} - if (VAPI_OK != rv) { - SC_LOG_DBG_MSG("vapi_sw_interface_dump"); - return false; +static vapi_error_e +interface_dump_all_cb(struct vapi_ctx_s *ctx, void *callback_ctx, + vapi_error_e rv, bool is_last, + vapi_payload_sw_interface_details * reply) +{ + dump_all_ctx *dctx = callback_ctx; + + if (is_last) + return VAPI_OK; + + if(dctx->capacity == 0 && dctx->intfcArray == NULL) { + dctx->capacity = 10; + dctx->intfcArray = (vpp_interface_t*)malloc( sizeof(vpp_interface_t)*dctx->capacity ); + } + if(dctx->num_ifs >= dctx->capacity-1) { + dctx->capacity += 10; + dctx->intfcArray = (vpp_interface_t*)realloc(dctx->intfcArray, sizeof(vpp_interface_t)*dctx->capacity ); } - 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); + vpp_interface_t * iface = &dctx->intfcArray[dctx->num_ifs]; - return sw_interface_details_query->interface_found; + iface->sw_if_index = reply->sw_if_index; + strncpy(iface->interface_name, reply->interface_name, VPP_INTFC_NAME_LEN); + iface->l2_address_length = reply->l2_address_length; + memcpy(iface->l2_address, reply->l2_address, reply->l2_address_length ); + iface->link_speed = reply->link_speed; + + iface->link_mtu = reply->link_mtu; + iface->admin_up_down = reply->admin_up_down; + iface->link_up_down = reply->link_up_down; + + dctx->num_ifs += 1; + + return VAPI_OK; +} + +int interface_dump_all(dump_all_ctx * dctx) +{ + vapi_msg_sw_interface_dump *dump; + vapi_error_e rv; + + ARG_CHECK(-1, dctx); + + if(dctx == NULL) + return -1; + + dctx->intfcArray = NULL; + dctx->capacity = 0; + dctx->num_ifs = 0; + + dump = vapi_alloc_sw_interface_dump(g_vapi_ctx_instance); + + dump->payload.name_filter_valid = 0; + memset(dump->payload.name_filter, 0, sizeof(dump->payload.name_filter)); + while (VAPI_EAGAIN == + (rv = + vapi_sw_interface_dump(g_vapi_ctx_instance, dump, interface_dump_all_cb, + dctx))); + + return dctx->num_ifs; } static vapi_error_e -get_interface_name_cb (struct vapi_ctx_s *ctx, void *callback_ctx, +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) { @@ -110,10 +156,10 @@ get_interface_name_cb (struct vapi_ctx_s *ctx, void *callback_ctx, } else { - assert(reply && dctx); - SC_LOG_DBG("Interface dump entry: [%u]: %s\n", reply->sw_if_index, reply->interface_name); + assert(NULL != reply); - if (dctx->sw_interface_details.sw_if_index == reply->sw_if_index) + 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; @@ -124,74 +170,48 @@ get_interface_name_cb (struct vapi_ctx_s *ctx, void *callback_ctx, return VAPI_OK; } -bool get_interface_name(sw_interface_details_query_t * sw_interface_details_query) +// return error code instead of boolean +int get_interface_id(sw_interface_details_query_t * sw_interface_details_query) { + vapi_error_e rv; + 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)); + 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)); - if (VAPI_OK != rv) { - SC_LOG_DBG_MSG("vapi_sw_interface_dump"); + VAPI_CALL(vapi_sw_interface_dump(g_vapi_ctx_instance, mp, get_interface_id_cb, sw_interface_details_query)); + if (VAPI_OK != rv) 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) +int get_interface_name(sw_interface_details_query_t *query) { - 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); - } + vapi_error_e rv; - return VAPI_OK; -} + ARG_CHECK(-EINVAL, query); -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); + query->interface_found = false; - 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)); - } + rv = bin_api_sw_interface_dump(&query->sw_interface_details); + if (rv == VAPI_OK) + query->interface_found = true; - vapi_error_e rv; - VAPI_CALL(vapi_sw_interface_dump (g_vapi_ctx_instance, mp, sw_interface_dump_cb, NULL)); - - return rv; + return query->interface_found; } - VAPI_RETVAL_CB(sw_interface_set_flags); -vapi_error_e bin_api_sw_interface_set_flags(uint32_t if_index, uint8_t up) +static 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); @@ -207,46 +227,42 @@ vapi_error_e bin_api_sw_interface_set_flags(uint32_t if_index, uint8_t up) 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) +//set interface l2 bridge <interface> <bridge-domain-id> [bvi|uu-fwd] [shg] +static 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); + vapi_msg_sw_interface_set_l2_bridge *mp; + vapi_error_e rv; + + mp = vapi_alloc_sw_interface_set_l2_bridge (g_vapi_ctx_instance); assert(NULL != mp); - //set interface l2 bridge <interface> <bridge-domain-id> [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) +int interface_enable(const char *interface_name, const bool enable) { - ARG_CHECK(VAPI_EINVAL, reply); + ARG_CHECK(-1, interface_name); - vapi_msg_create_loopback *mp = vapi_alloc_create_loopback (g_vapi_ctx_instance); - assert(NULL != mp); + int rc = 0; + sw_interface_details_query_t query = {0}; + sw_interface_details_query_set_name(&query, interface_name); - //mp->payload.mac_address = + rc = get_interface_id(&query); + if (!rc) + return -1; - vapi_error_e rv; - VAPI_CALL(vapi_create_loopback (g_vapi_ctx_instance, mp, create_loopback_cb, reply)); + rc = bin_api_sw_interface_set_flags(query.sw_interface_details.sw_if_index, + enable); + if (VAPI_OK != rc) + return -1; - return rv; + return 0; } diff --git a/src/scvpp/src/sc_vpp_interface.h b/src/scvpp/src/sc_vpp_interface.h index 45cc2ef..32b61bc 100644 --- a/src/scvpp/src/sc_vpp_interface.h +++ b/src/scvpp/src/sc_vpp_interface.h @@ -17,36 +17,45 @@ #ifndef __BAPI_INTERFACE_H__ #define __BAPI_INTERFACE_H__ - #include <vapi/interface.api.vapi.h> +int interface_enable(const char *interface_name, const bool enable); +//TODO remove the following structures ASAP typedef struct { bool interface_found; vapi_payload_sw_interface_details sw_interface_details; } sw_interface_details_query_t; +typedef struct _vpp_interface_t +{ + u32 sw_if_index; + char interface_name[VPP_INTFC_NAME_LEN]; + u8 l2_address[VPP_MAC_ADDRESS_LEN]; + u32 l2_address_length; + u64 link_speed; + u16 link_mtu; + u8 admin_up_down; + u8 link_up_down; +} vpp_interface_t; + +typedef struct _dump_all_ctx +{ + int num_ifs; + int capacity; + vpp_interface_t * intfcArray; +} dump_all_ctx; + +/* return the number of interfaces or a negative error code */ +extern int interface_dump_all(dump_all_ctx * dctx); + 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); +extern int 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); - +extern int get_interface_name(sw_interface_details_query_t * sw_interface_details_query); #endif /* __BAPI_INTERFACE_H__ */ diff --git a/src/scvpp/src/sc_vpp_ip.c b/src/scvpp/src/sc_vpp_ip.c index a09bf3a..77e79fb 100644 --- a/src/scvpp/src/sc_vpp_ip.c +++ b/src/scvpp/src/sc_vpp_ip.c @@ -21,47 +21,52 @@ #include <assert.h> +// Use VAPI macros to define symbols 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) +static vapi_error_e +bin_api_sw_interface_add_del_address(u32 sw_if_index, bool is_add, bool is_ipv6, + bool del_all, u8 address_length, + const char *ip_address) { + vapi_msg_sw_interface_add_del_address *mp; + vapi_error_e rv; + 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); + 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.is_ipv6 = is_ipv6; + mp->payload.del_all = del_all; mp->payload.address_length = address_length; - if (!sc_aton(ip_address, mp->payload.address, - sizeof(mp->payload.address))) + 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)); + 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 +static 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); + vapi_msg_sw_interface_add_del_address *mp; + vapi_error_e rv; + + 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, + VAPI_CALL(vapi_sw_interface_add_del_address(g_vapi_ctx_instance, mp, sw_interface_add_del_address_cb, NULL)); return rv; @@ -69,28 +74,23 @@ bin_api_sw_interface_del_all_address(u32 sw_if_index) 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) +static 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); + sw_interface_details_query_t query = {0}; + vapi_msg_ip_add_del_route *mp; - SC_LOG_DBG("Interface: %s", interface_name); + ARG_CHECK4(VAPI_EINVAL, reply, dst_address, next_hop, 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); + 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 @@ -100,12 +100,11 @@ vapi_error_e bin_api_ip_add_del_route( 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, + 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, + if (sc_aton(next_hop, mp->payload.next_hop_address, sizeof(mp->payload.next_hop_address))) return VAPI_EINVAL; @@ -116,9 +115,8 @@ vapi_error_e bin_api_ip_add_del_route( } 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) +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) { @@ -139,8 +137,6 @@ ip_fib_dump_cb (struct vapi_ctx_s *ctx, void *callback_ctx, 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) { @@ -175,14 +171,13 @@ b+>│494 s = format (s, "%U", format_ip46_address, */ - printf("\tnext hop: %s\n", sc_ntoa(reply->path[i].next_hop)); } } return VAPI_OK; } -vapi_error_e +static vapi_error_e bin_api_ip_fib_dump() { vapi_msg_ip_fib_dump *mp = vapi_alloc_ip_fib_dump (g_vapi_ctx_instance); @@ -199,7 +194,7 @@ bin_api_ip_fib_dump() VAPI_RETVAL_CB(ip_table_add_del) -vapi_error_e +static 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); @@ -214,9 +209,111 @@ bin_api_table_add_del(u8 is_add, u32 table_id) return rv; } +static vapi_error_e +ip_address_dump_cb (struct vapi_ctx_s *ctx, void *callback_ctx, vapi_error_e rv, + bool is_last, vapi_payload_ip_address_details *reply) +{ + ARG_CHECK3(VAPI_EINVAL, ctx, callback_ctx, reply); + + //copy dump reply in callback context + if (!is_last && callback_ctx) { + vapi_payload_ip_address_details *passed = + (vapi_payload_ip_address_details *) callback_ctx; + *passed = *reply; + } + + return VAPI_OK; +} + +static vapi_error_e +bin_api_ip_address_dump(u32 sw_if_index, bool is_ipv6, + vapi_payload_ip_address_details *dctx) +{ + vapi_msg_ip_address_dump *mp; + vapi_error_e rv; + + mp = vapi_alloc_ip_address_dump(g_vapi_ctx_instance); + mp->payload.sw_if_index = sw_if_index; + mp->payload.is_ipv6 = is_ipv6; + + VAPI_CALL(vapi_ip_address_dump(g_vapi_ctx_instance, mp, ip_address_dump_cb, + dctx)); + if (rv != VAPI_OK) + return rv; + + return VAPI_OK; +} + +/* + * @brief Dump IPv4/IPv6 address from an interface. + * @param interface_name Name of the interface to dump. + * @param ip_addr pointer where dump will store IP. + * @param prefix_len pointer where dump will store prefix + */ +int ipv46_address_dump(const char *interface_name, char *ip_addr, + u8 *prefix_len, bool is_ipv6) +{ + vapi_payload_ip_address_details dctx = {0}; + sw_interface_details_query_t query = {0}; + vapi_error_e rv; + + sw_interface_details_query_set_name(&query, interface_name); + + if (!get_interface_id(&query)) + return -EINVAL; + + rv = bin_api_ip_address_dump(query.sw_interface_details.sw_if_index, false, &dctx); + if (rv != VAPI_OK) + return -EAGAIN; + strcpy(ip_addr, sc_ntoa(dctx.ip)); //IP string + *prefix_len = dctx.prefix_length; //prefix length + return 0; +} +/** + * @brief Add or remove IPv4/IPv6 address to/from an interface. + */ +int ipv46_config_add_remove(const char *if_name, const char *addr, + uint8_t prefix, bool is_ipv6, bool add) +{ + vapi_error_e rv; + int rc; + ARG_CHECK2(-1, if_name, addr); + /* get interface index */ + sw_interface_details_query_t query = {0}; + sw_interface_details_query_set_name(&query, if_name); + rc = get_interface_id(&query); + if (!rc) + return -EINVAL; + + /* add del addr */ + rv = bin_api_sw_interface_add_del_address(query.sw_interface_details.sw_if_index, + add, is_ipv6, 0, prefix, addr); + if (rv != VAPI_OK) + return -EINVAL; + + return 0; +} +/* + * @brief Add or remove an IP route + */ +int ipv46_config_add_del_route(const char* dst_address, u8 dst_address_length, + const char* next_address, u8 is_add, + u32 table_id, const char *interface) +{ + vapi_payload_ip_add_del_route_reply reply = {0}; + vapi_error_e rv; + + rv = bin_api_ip_add_del_route(&reply, dst_address, dst_address_length, + next_address, is_add, table_id, interface); + if (VAPI_OK != rv || reply.retval > 0) { + return -EINVAL; + } + + return 0; +} diff --git a/src/scvpp/src/sc_vpp_ip.h b/src/scvpp/src/sc_vpp_ip.h index 0940fe8..a9f19d4 100644 --- a/src/scvpp/src/sc_vpp_ip.h +++ b/src/scvpp/src/sc_vpp_ip.h @@ -21,26 +21,16 @@ #include <vapi/interface.api.vapi.h> #include <vapi/ip.api.vapi.h> +/* If no IP has been found ip_addr will be "0.0.0.0" */ +extern int ipv46_address_dump(const char *interface_name, char *ip_addr, + u8 *prefix_len, bool is_ipv6); -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 int ipv46_config_add_remove(const char *if_name, const char *addr, + uint8_t prefix, bool is_ipv6, bool add); -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); +extern int +ipv46_config_add_del_route(const char* dst_address, u8 dst_address_length, + const char* next_address, u8 is_add, u32 table_id, + const char *interface); #endif /* __BAPI_IP_H__ */ |