diff options
Diffstat (limited to 'src/scvpp')
-rw-r--r-- | src/scvpp/src/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/scvpp/src/sc_vpp_v3po.c | 107 | ||||
-rw-r--r-- | src/scvpp/src/sc_vpp_v3po.h | 26 | ||||
-rw-r--r-- | src/scvpp/tests/scvpp_test.c | 127 |
4 files changed, 247 insertions, 15 deletions
diff --git a/src/scvpp/src/CMakeLists.txt b/src/scvpp/src/CMakeLists.txt index baa03ed..af4a945 100644 --- a/src/scvpp/src/CMakeLists.txt +++ b/src/scvpp/src/CMakeLists.txt @@ -20,6 +20,7 @@ set(SCVPP_SOURCES sc_vpp_comm.c sc_vpp_interface.c sc_vpp_ip.c + sc_vpp_v3po.c ) # scvpp public headers @@ -27,6 +28,7 @@ set(SCVPP_HEADERS sc_vpp_comm.h sc_vpp_interface.h sc_vpp_ip.h + sc_vpp_v3po.h ) set(CMAKE_C_FLAGS " -g -O0 -fpic -fPIC -std=gnu99 -Wl,-rpath-link=/usr/lib") diff --git a/src/scvpp/src/sc_vpp_v3po.c b/src/scvpp/src/sc_vpp_v3po.c new file mode 100644 index 0000000..d312331 --- /dev/null +++ b/src/scvpp/src/sc_vpp_v3po.c @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <assert.h> + +#include "sc_vpp_comm.h" +#include "sc_vpp_v3po.h" +#include "sc_vpp_interface.h" + +/* + * tap-v2 interfaces + */ + +DEFINE_VAPI_MSG_IDS_TAPV2_API_JSON + +// Dump tapv2 + +//typedef struct __attribute__ ((__packed__)) { +// u32 sw_if_index; +// u32 id; +// u8 dev_name[64]; +// u16 tx_ring_sz; +// u16 rx_ring_sz; +// u8 host_mac_addr[6]; +// u8 host_if_name[64]; +// u8 host_namespace[64]; +// u8 host_bridge[64]; +// u8 host_ip4_addr[4]; +// u8 host_ip4_prefix_len; +// u8 host_ip6_addr[16]; +// u8 host_ip6_prefix_len; +// u32 tap_flags; +//} vapi_payload_sw_interface_tap_v2_details; + +// Delete tapv2 + +VAPI_RETVAL_CB(tap_delete_v2); + +static vapi_error_e bin_api_delete_tapv2(u32 sw_if_index) +{ + vapi_msg_tap_delete_v2 *mp; + vapi_error_e rv; + + mp = vapi_alloc_tap_delete_v2(g_vapi_ctx_instance); + assert(NULL != mp); + + mp->payload.sw_if_index = sw_if_index; + + VAPI_CALL(vapi_tap_delete_v2(g_vapi_ctx_instance, mp, tap_delete_v2_cb, + NULL)); + if (rv != VAPI_OK) + return -EAGAIN; + + return rv; +} + +int delete_tapv2(char *iface_name) +{ + int rc; + sw_interface_details_query_t query = {0}; + + sw_interface_details_query_set_name(&query, iface_name); + + rc = get_interface_id(&query); + if (!rc) + return -1; + + rc = bin_api_delete_tapv2(query.sw_interface_details.sw_if_index); + if (VAPI_OK != rc) + return -1; + + return 0; +} + +// Create tapv2 + +VAPI_RETVAL_CB(tap_create_v2); + +int create_tapv2(tapv2_create_t *query) +{ + vapi_msg_tap_create_v2 *mp; + vapi_error_e rv; + + mp = vapi_alloc_tap_create_v2(g_vapi_ctx_instance); + assert(NULL != mp); + + memcpy(&mp->payload, query, sizeof(tapv2_create_t)); + + VAPI_CALL(vapi_tap_create_v2(g_vapi_ctx_instance, mp, tap_create_v2_cb, + NULL)); + if (rv != VAPI_OK) + return -EAGAIN; + + return 0; +} diff --git a/src/scvpp/src/sc_vpp_v3po.h b/src/scvpp/src/sc_vpp_v3po.h new file mode 100644 index 0000000..9c13569 --- /dev/null +++ b/src/scvpp/src/sc_vpp_v3po.h @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _V3PO__INTERFACE_H__ +#define _V3PO__INTERFACE_H__ + +#include <vapi/tapv2.api.vapi.h> + +typedef vapi_payload_tap_create_v2 tapv2_create_t; + +int create_tapv2(tapv2_create_t *query); +int delete_tapv2(char *iface_name); + +#endif /* __V3PO_INTERFACE_H__ */ diff --git a/src/scvpp/tests/scvpp_test.c b/src/scvpp/tests/scvpp_test.c index 81938f0..adc2b55 100644 --- a/src/scvpp/tests/scvpp_test.c +++ b/src/scvpp/tests/scvpp_test.c @@ -20,35 +20,132 @@ #include <cmocka.h> #include "sc_vpp_comm.h" +#include "sc_vpp_interface.h" +#include "sc_vpp_ip.h" +#include "sc_vpp_v3po.h" +//TODO Check with future function get_interface_state +static void test_enable_disable(void **state) +{ + int rc; + + rc = interface_enable("tap0", 1); + assert_int_equal(rc, 0); + + rc = interface_enable("tap0", 0); + assert_int_equal(rc, 0); +} -static int -scvpp_test_setup(void **state) +//TODO would need to make sure tap0 is index 1 +//TODO delete eventually because get_interface_id will not be extern +static void test_name2index(void **state) { - //sc_vpp_connect - return 0; + int rc; + const char iface_name[] = "tap0"; + sw_interface_details_query_t query = {0}; + sw_interface_details_query_set_name(&query, iface_name); + + rc = get_interface_id(&query); + assert_int_equal(rc, 1); + + assert_string_equal(iface_name, query.sw_interface_details.interface_name); + assert_int_equal(query.sw_interface_details.sw_if_index, 1); } -static int -scvpp_test_teardown(void **state) +static void test_add_ipv4(void **state) { - //sc_vpp_disconnect - return 0; + const char ip_q[VPP_IP4_ADDRESS_STRING_LEN] = "192.168.100.144"; + char ip_r[VPP_IP4_ADDRESS_STRING_LEN]; + u8 prefix_q = 24; + u8 prefix_r; + int rc; + + //add ipv4 on tap0 + rc = ipv46_config_add_remove("tap0", ip_q, prefix_q, false, true); + assert_int_equal(rc, 0); + + rc = ipv46_address_dump("tap0", ip_r, &prefix_r, false); + assert_int_equal(rc, 0); + + assert_string_equal(ip_q, ip_r); + assert_int_equal(prefix_q, prefix_r); } -static void -scvpp_interface_test(void **state) +static void test_remove_ipv4(void **state) { - //call interface functions + const char ip_q[VPP_IP4_ADDRESS_STRING_LEN] = "192.168.100.144"; + char ip_r[VPP_IP4_ADDRESS_STRING_LEN]; + u8 prefix_q = 24; + u8 prefix_r; + int rc; + + //add ipv4 on tap0 + rc = ipv46_config_add_remove("tap0", ip_q, prefix_q, false, true); + assert_int_equal(rc, 0); + + //remove ipv4 on tap0 + rc = ipv46_config_add_remove("tap0", ip_q, prefix_q, false, false); + assert_int_equal(rc, 0); + + //dump ipv4 + rc = ipv46_address_dump("tap0", ip_r, &prefix_r, false); + assert_int_equal(rc, 0); + + assert_string_equal(ip_r, "0.0.0.0"); } +static void test_sc_ntoa(void **state) +{ + u8 buf[4] = {192, 168, 100, 44}; + char *res; -int -main() + res = sc_ntoa(buf); + assert_string_equal(res, "192.168.100.44"); +} + +static void test_create_tapv2(void **state) { + tapv2_create_t query = {0}; + int rc; + + query.id = 1; + query.use_random_mac = 1; + + rc = create_tapv2(&query); + assert_int_equal(rc, 0); + + //TODO dump_tav2 and compare values + + rc = delete_tapv2("tap1"); + assert_int_equal(rc, 0); +} + +int main() +{ + tapv2_create_t query = {0}; const struct CMUnitTest tests[] = { - cmocka_unit_test_setup_teardown(scvpp_interface_test, scvpp_test_setup, scvpp_test_teardown), + cmocka_unit_test_setup_teardown(test_enable_disable, NULL, NULL), + cmocka_unit_test_setup_teardown(test_name2index, NULL, NULL), + cmocka_unit_test_setup_teardown(test_add_ipv4, NULL, NULL), + cmocka_unit_test_setup_teardown(test_remove_ipv4, NULL, NULL), + cmocka_unit_test_setup_teardown(test_sc_ntoa, NULL, NULL), + cmocka_unit_test_setup_teardown(test_create_tapv2, NULL, NULL), }; - return cmocka_run_group_tests(tests, NULL, NULL); + if (sc_connect_vpp() != 0) + fprintf(stderr, "Error connecting to VPP\n"); + + /* Create interface tap0 to test several functions */ + query.id = 0; + query.use_random_mac = 1; + if (create_tapv2(&query) != 0) + fprintf(stderr, "Error creating tap0\n"); + + cmocka_run_group_tests(tests, NULL, NULL); + + /* Delete tap0 */ + if (delete_tapv2("tap0") != 0) + fprintf(stderr, "Failed deleting tap0\n"); + + return sc_disconnect_vpp(); } |