Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 1 | /* |
| 2 | *------------------------------------------------------------------ |
Ole Troan | 3c70c05 | 2020-08-10 16:25:21 +0200 | [diff] [blame] | 3 | * test.c -- VPP API/Stats tests |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 4 | * |
| 5 | * Copyright (c) 2016 Cisco and/or its affiliates. |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at: |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | *------------------------------------------------------------------ |
| 18 | */ |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
Ole Troan | 3c70c05 | 2020-08-10 16:25:21 +0200 | [diff] [blame] | 21 | #include <assert.h> |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 22 | #include <sys/types.h> |
| 23 | #include <sys/socket.h> |
| 24 | #include <sys/mman.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <netinet/in.h> |
| 27 | #include <netdb.h> |
| 28 | |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 29 | |
| 30 | #include <vnet/vnet.h> |
| 31 | #include <vlib/vlib.h> |
| 32 | #include <vlib/unix/unix.h> |
| 33 | #include <vlibapi/api.h> |
Ole Troan | 3c70c05 | 2020-08-10 16:25:21 +0200 | [diff] [blame] | 34 | #include <vppinfra/time.h> |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 35 | #include <signal.h> |
Damjan Marion | 5fec1e8 | 2017-04-13 19:13:47 +0200 | [diff] [blame] | 36 | #include "vppapiclient.h" |
Ole Troan | 3c70c05 | 2020-08-10 16:25:21 +0200 | [diff] [blame] | 37 | #include "stat_client.h" |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 38 | |
Florin Coras | a1400ce | 2021-09-15 09:02:08 -0700 | [diff] [blame] | 39 | #include <vlibmemory/vlib.api_enum.h> |
| 40 | #include <vlibmemory/vlib.api_types.h> |
Filip Tehlar | f0e67d7 | 2021-07-23 22:03:05 +0000 | [diff] [blame^] | 41 | #include <vlibmemory/memclnt.api_enum.h> |
| 42 | #include <vlibmemory/memclnt.api_types.h> |
Florin Coras | a1400ce | 2021-09-15 09:02:08 -0700 | [diff] [blame] | 43 | |
Ole Troan | 3459ece | 2021-09-27 17:11:34 +0200 | [diff] [blame] | 44 | #include <vpp/api/vpe.api_enum.h> |
| 45 | #include <vpp/api/vpe.api_types.h> |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 46 | |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 47 | volatile int sigterm_received = 0; |
| 48 | volatile u32 result_ready; |
| 49 | volatile u16 result_msg_id; |
| 50 | |
| 51 | /* M_NOALLOC: construct, but don't yet send a message */ |
| 52 | |
| 53 | #define M_NOALLOC(T,t) \ |
| 54 | do { \ |
| 55 | result_ready = 0; \ |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 56 | clib_memset (mp, 0, sizeof (*mp)); \ |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 57 | mp->_vl_msg_id = ntohs (VL_API_##T); \ |
| 58 | mp->client_index = am->my_client_index; \ |
| 59 | } while(0); |
| 60 | |
| 61 | |
| 62 | |
Andrey "Zed" Zaikin | 334167f | 2018-04-09 16:42:42 +0300 | [diff] [blame] | 63 | void |
| 64 | wrap_vac_callback (unsigned char *data, int len) |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 65 | { |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 66 | result_ready = 1; |
| 67 | result_msg_id = ntohs(*((u16 *)data)); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 68 | } |
| 69 | |
Ole Troan | 73710c7 | 2018-06-04 22:27:49 +0200 | [diff] [blame] | 70 | static void |
| 71 | test_connect () |
| 72 | { |
| 73 | static int i; |
| 74 | int rv = vac_connect("vac_client", NULL, wrap_vac_callback, 32 /* rx queue-length*/); |
| 75 | if (rv != 0) { |
| 76 | printf("Connect failed: %d\n", rv); |
| 77 | exit(rv); |
| 78 | } |
| 79 | printf("."); |
| 80 | vac_disconnect(); |
| 81 | i++; |
| 82 | } |
| 83 | |
| 84 | static void |
| 85 | test_messages (void) |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 86 | { |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame] | 87 | api_main_t * am = vlibapi_get_main(); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 88 | vl_api_show_version_t message; |
| 89 | vl_api_show_version_t *mp; |
| 90 | int async = 1; |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 91 | |
Ole Troan | 73710c7 | 2018-06-04 22:27:49 +0200 | [diff] [blame] | 92 | int rv = vac_connect("vac_client", NULL, wrap_vac_callback, 32 /* rx queue-length*/); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 93 | if (rv != 0) { |
| 94 | printf("Connect failed: %d\n", rv); |
| 95 | exit(rv); |
| 96 | } |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 97 | |
Ole Troan | 3c70c05 | 2020-08-10 16:25:21 +0200 | [diff] [blame] | 98 | double timestamp_start = unix_time_now_nsec() * 1e-6; |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 99 | |
| 100 | /* |
| 101 | * Test vpe_api_write and vpe_api_read to send and recv message for an |
| 102 | * API |
| 103 | */ |
| 104 | int i; |
| 105 | long int no_msgs = 10000; |
| 106 | mp = &message; |
| 107 | |
| 108 | for (i = 0; i < no_msgs; i++) { |
| 109 | /* Construct the API message */ |
| 110 | M_NOALLOC(SHOW_VERSION, show_version); |
Damjan Marion | 5fec1e8 | 2017-04-13 19:13:47 +0200 | [diff] [blame] | 111 | vac_write((char *)mp, sizeof(*mp)); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 112 | #ifndef __COVERITY__ |
| 113 | /* As given, async is always 1. Shut up Coverity about it */ |
| 114 | if (!async) |
| 115 | while (result_ready == 0); |
| 116 | #endif |
| 117 | } |
| 118 | if (async) { |
| 119 | vl_api_control_ping_t control; |
| 120 | vl_api_control_ping_t *mp; |
| 121 | mp = &control; |
| 122 | M_NOALLOC(CONTROL_PING, control_ping); |
Damjan Marion | 5fec1e8 | 2017-04-13 19:13:47 +0200 | [diff] [blame] | 123 | vac_write((char *)mp, sizeof(*mp)); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 124 | |
| 125 | while (result_msg_id != VL_API_CONTROL_PING_REPLY); |
| 126 | } |
| 127 | |
Ole Troan | 3c70c05 | 2020-08-10 16:25:21 +0200 | [diff] [blame] | 128 | double timestamp_end = unix_time_now_nsec() * 1e-6; |
| 129 | printf("\nTook %.2f msec, %.0f msgs/msec \n", (timestamp_end - timestamp_start), |
| 130 | no_msgs/(timestamp_end - timestamp_start)); |
Damjan Marion | 5fec1e8 | 2017-04-13 19:13:47 +0200 | [diff] [blame] | 131 | printf("Exiting...\n"); |
| 132 | vac_disconnect(); |
Ole Troan | 73710c7 | 2018-06-04 22:27:49 +0200 | [diff] [blame] | 133 | } |
| 134 | |
Ole Troan | 3c70c05 | 2020-08-10 16:25:21 +0200 | [diff] [blame] | 135 | static void |
| 136 | test_stats (void) |
| 137 | { |
| 138 | clib_mem_trace_enable_disable(1); |
| 139 | clib_mem_trace (1); |
| 140 | |
| 141 | int rv = stat_segment_connect (STAT_SEGMENT_SOCKET_FILE); |
| 142 | assert(rv == 0); |
| 143 | |
| 144 | u32 *dir; |
| 145 | int i, j, k; |
| 146 | stat_segment_data_t *res; |
| 147 | u8 **pattern = 0; |
| 148 | vec_add1(pattern, (u8 *)"/if/names"); |
| 149 | vec_add1(pattern, (u8 *)"/err"); |
| 150 | |
| 151 | dir = stat_segment_ls ((u8 **)pattern); |
| 152 | |
| 153 | res = stat_segment_dump (dir); |
| 154 | for (i = 0; i < vec_len (res); i++) { |
| 155 | switch (res[i].type) { |
| 156 | case STAT_DIR_TYPE_NAME_VECTOR: |
| 157 | if (res[i].name_vector == 0) |
| 158 | continue; |
| 159 | for (k = 0; k < vec_len (res[i].name_vector); k++) |
| 160 | if (res[i].name_vector[k]) |
| 161 | fformat (stdout, "[%d]: %s %s\n", k, res[i].name_vector[k], |
| 162 | res[i].name); |
| 163 | break; |
| 164 | case STAT_DIR_TYPE_ERROR_INDEX: |
| 165 | for (j = 0; j < vec_len (res[i].error_vector); j++) |
| 166 | fformat (stdout, "%llu %s\n", res[i].error_vector[j], |
| 167 | res[i].name); |
| 168 | break; |
| 169 | default: |
| 170 | assert(0); |
| 171 | } |
| 172 | } |
| 173 | stat_segment_data_free (res); |
| 174 | stat_segment_disconnect(); |
| 175 | |
| 176 | vec_free(pattern); |
| 177 | vec_free(dir); |
| 178 | |
| 179 | (void) clib_mem_trace_enable_disable (0); |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 180 | u8 *leak_report = format (0, "%U", format_clib_mem_heap, 0, |
Ole Troan | 3c70c05 | 2020-08-10 16:25:21 +0200 | [diff] [blame] | 181 | 1 /* verbose, i.e. print leaks */ ); |
| 182 | printf("%s", leak_report); |
| 183 | vec_free (leak_report); |
| 184 | clib_mem_trace (0); |
| 185 | } |
| 186 | |
Ole Troan | 73710c7 | 2018-06-04 22:27:49 +0200 | [diff] [blame] | 187 | int main (int argc, char ** argv) |
| 188 | { |
Ole Troan | 3c70c05 | 2020-08-10 16:25:21 +0200 | [diff] [blame] | 189 | clib_mem_init (0, 3ULL << 30); |
| 190 | test_stats(); |
| 191 | |
Ole Troan | 73710c7 | 2018-06-04 22:27:49 +0200 | [diff] [blame] | 192 | int i; |
| 193 | |
| 194 | for (i = 0; i < 1000; i++) { |
| 195 | test_connect(); |
| 196 | } |
| 197 | test_messages(); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 198 | exit (0); |
| 199 | } |