diff options
author | YohanPipereau <ypiperea@cisco.com> | 2019-03-06 14:01:58 +0100 |
---|---|---|
committer | YohanPipereau <ypiperea@cisco.com> | 2019-03-29 14:39:36 +0100 |
commit | a760dfb253161911fc3aa3c8b879c461d53ade6e (patch) | |
tree | 0e79953f4ed5615879a58f49a74df3f9c6739a42 /src/scvpp/src/ip.c | |
parent | 2b9b6b9b130b75799a40989c0ebe5040fa3e45fb (diff) |
Sweetcomb global cleanup
-Merge IETF and Openconfig to use SCVPP 2
-Move L2 bridge from sc_vpp_interface to sc_vpp_v3po
-Implement tav2 dump
-Make openconfig-interfaces functions static
-Try one more dispatch after failure in VAPI_CALL
-Add error return code for scvpp
-Remove unused length maccros
-Return appropriate error code for interface dump when interface not found
-Improve scvpp test suite
-Change get_interface_id prototype
-Use interface_dump_iface in openconfig_interface.
-No more vapi types in openconfig_interfaces.c
-Move openconfig_local_routing VAPI operations to sc_vpp_ip
-Implement a multiple dump with a stack data structure
-Comment out state_cb code from openconfig_local_routing to use new
functions later.
-Rename YANG model to their fully qualified name : <module>@<revision>
-Remove headers almost empty and put registration declaration in
sc_model.h
-Shorten vapi context global variable name
-Reorganize scvpp unit test suite
-Add instructions to Makefile to install/uninstall YANG models in sysrepo.
-Add this new instructions to README.md.
-Reimplement interface_dump_all
-Use a common message at INFO Log Level to know when sysrepo callbacks
are triggered
-Remove old structure to perform dump of all interfaces
-Reimplement get_interface_name and add scvpp test for it
-Clean sys_util
-Use UNUSED maccro everywhere to have lighter prototypes
-Have ietf-interfaces:interfaces-state work with new dump function
-Add setup and teardown for NAT tests
-Remove unused tapv2 dump
-Remove useless sysrepo module callback
-Remove xpath_find_first_key usage in openconfig-interfaces
-Remove xpath_find_first_key in oc_local_routing and in the rest of
sweetcomb
-Reorganize scvpp include dir and fix scvpp_test new warnings
-Fix scvpp tests for ip routes
-Factorize scvpp nat and test return code of its function
-Correct test_dump_if_all if there is an existing hardware interface
-Implement a per-prefix dump in scvpp
-free changes iterator in ietf-interfaces
-Add new XPATH in oc local-routing
-Introduce helper methods for sysrepo config callbacks
-Factorize config callback
-Refactor the openconfig-local-routing config callback
-Use common foreach_change to iterate over changes in all models
-Create a sample directory gathering example of configurations supposed
to work with sweetcomb
-Fix state callback of oc-local-routing
-Add new sample for get operation on next-hop
-foreach_elt maccro condition forgets to read one element
Change-Id: I8e87fce577a00337977588f057a6e095a20f457c
Signed-off-by: YohanPipereau <ypiperea@cisco.com>
Diffstat (limited to 'src/scvpp/src/ip.c')
-rw-r--r-- | src/scvpp/src/ip.c | 259 |
1 files changed, 259 insertions, 0 deletions
diff --git a/src/scvpp/src/ip.c b/src/scvpp/src/ip.c new file mode 100644 index 0000000..2dc991c --- /dev/null +++ b/src/scvpp/src/ip.c @@ -0,0 +1,259 @@ +/* + * 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 <scvpp/comm.h> +#include <scvpp/ip.h> +#include <scvpp/interface.h> + +#include <assert.h> +#include <stdio.h> + +// Use VAPI macros to define symbols +DEFINE_VAPI_MSG_IDS_IP_API_JSON + +VAPI_RETVAL_CB(sw_interface_add_del_address); + +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); + + 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 = is_ipv6; + mp->payload.del_all = del_all; + mp->payload.address_length = address_length; + if (sc_aton(ip_address, mp->payload.address, VPP_IP4_ADDRESS_LEN)) + return VAPI_EINVAL; + + 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) + +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 *next_interface) +{ + vapi_msg_ip_add_del_route *mp; + uint32_t sw_if_index; + vapi_error_e rv ; + int rc; + + ARG_CHECK2(VAPI_EINVAL, reply, dst_address); + + //Require interface or next hop IP or both + if (!next_interface && !next_hop) + return VAPI_EINVAL; + + mp = vapi_alloc_ip_add_del_route(g_vapi_ctx, 1); + assert(NULL != mp); + + if (next_interface) { + rc = get_interface_id(next_interface, &sw_if_index); + if (rc < 0) + return VAPI_EINVAL; + } + + mp->payload.is_add = is_add; + mp->payload.table_id = table_id; + mp->payload.is_ipv6 = false; + mp->payload.is_local = false; + sc_aton(dst_address, mp->payload.dst_address, VPP_IP4_ADDRESS_LEN); + mp->payload.dst_address_length = dst_address_length; + if (next_interface) //interface is not mandatory + mp->payload.next_hop_sw_if_index = sw_if_index; + if (next_hop) //next hop ip is not mandatory + sc_aton(next_hop, mp->payload.next_hop_address, VPP_IP4_ADDRESS_LEN); + + VAPI_CALL(vapi_ip_add_del_route(g_vapi_ctx, mp, + ip_add_del_route_cb, reply)); + + 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) +{ + UNUSED(rv); + vapi_payload_ip_address_details *passed; + + ARG_CHECK3(VAPI_EINVAL, ctx, callback_ctx, reply); + + //copy dump reply in callback context + if (!is_last) { + 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); + assert(mp != NULL); + + mp->payload.sw_if_index = sw_if_index; + mp->payload.is_ipv6 = is_ipv6; + + VAPI_CALL(vapi_ip_address_dump(g_vapi_ctx, mp, ip_address_dump_cb, + dctx)); + if (rv != VAPI_OK) + return rv; + + return VAPI_OK; +} + +///VAPI_DUMP_LIST_CB(ip_fib); can not be used because of path flexible array + +static vapi_error_e +ip_fib_all_cb(vapi_ctx_t ctx, void *caller_ctx, vapi_error_e rv, bool is_last, + vapi_payload_ip_fib_details *reply) +{ + UNUSED(ctx); UNUSED(rv); UNUSED(is_last); + struct elt **stackp; + ARG_CHECK2(VAPI_EINVAL, caller_ctx, reply); + + stackp = (struct elt**) caller_ctx; + push(stackp, reply, sizeof(*reply)+reply->count*sizeof(vapi_type_fib_path)); + + return VAPI_OK; +} + +struct elt* ipv4_fib_dump_all() +{ + struct elt *stack = NULL; + vapi_msg_ip_fib_dump *mp; + vapi_error_e rv; + + mp = vapi_alloc_ip_fib_dump(g_vapi_ctx); + assert(mp != NULL); + + VAPI_CALL(vapi_ip_fib_dump(g_vapi_ctx, mp, ip_fib_all_cb, &stack)); + if(VAPI_OK != rv) + return NULL; + + return stack; +} + +int ipv4_fib_dump_prefix(const char *prefix_xpath, fib_dump_t **reply) +{ + struct elt *stack = NULL; + char prefix[VPP_IP4_PREFIX_STRING_LEN]; + fib_dump_t *dump; + int rc = -SCVPP_NOT_FOUND; + + stack = ipv4_fib_dump_all(); + if (!stack) + return rc; + + foreach_stack_elt(stack) { + dump = (fib_dump_t *) data; + + if (rc == -SCVPP_NOT_FOUND) { + snprintf(prefix, VPP_IP4_PREFIX_STRING_LEN, "%s/%u", + sc_ntoa(dump->address), dump->address_length); + if (!strncmp(prefix_xpath, prefix, VPP_IP4_PREFIX_STRING_LEN)) { + *reply = dump; + rc = SCVPP_OK; + continue; + } + } + + free(dump); + } + + return rc; +} + +int ipv46_address_dump(const char *interface_name, char *ip_addr, + u8 *prefix_len, bool is_ipv6) +{ + vapi_payload_ip_address_details dctx = {0}; + uint32_t sw_if_index; + vapi_error_e rv; + int rc; + + rc = get_interface_id(interface_name, &sw_if_index); + if (rc < 0) + return rc; + + rv = bin_api_ip_address_dump(sw_if_index, is_ipv6, &dctx); + if (rv != VAPI_OK) + return -SCVPP_EINVAL; + + strcpy(ip_addr, sc_ntoa(dctx.ip)); //IP string + *prefix_len = dctx.prefix_length; //prefix length + + return SCVPP_OK; +} + +int ipv46_config_add_remove(const char *if_name, const char *addr, + uint8_t prefix, bool is_ipv6, bool add) +{ + vapi_error_e rv; + uint32_t sw_if_index; + int rc; + + ARG_CHECK2(-1, if_name, addr); + + rc = get_interface_id(if_name, &sw_if_index); + if (rc < 0) + return rc; + + /* add del addr */ + rv = bin_api_sw_interface_add_del_address(sw_if_index, add, is_ipv6, 0, + prefix, addr); + if (rv != VAPI_OK) + return -SCVPP_EINVAL; + + return SCVPP_OK; +} + +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 -SCVPP_EINVAL; + + return SCVPP_OK; +} |