blob: 6e74761fe2439f07e1e16277d269dcc70549b3e2 [file] [log] [blame]
Damjan Marion7cd468a2016-12-19 23:05:39 +01001/*
2 *------------------------------------------------------------------
Ole Troan3c70c052020-08-10 16:25:21 +02003 * test.c -- VPP API/Stats tests
Damjan Marion7cd468a2016-12-19 23:05:39 +01004 *
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 Troan3c70c052020-08-10 16:25:21 +020021#include <assert.h>
Damjan Marion7cd468a2016-12-19 23:05:39 +010022#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 Marion7cd468a2016-12-19 23:05:39 +010029
30#include <vnet/vnet.h>
31#include <vlib/vlib.h>
32#include <vlib/unix/unix.h>
33#include <vlibapi/api.h>
Ole Troan3c70c052020-08-10 16:25:21 +020034#include <vppinfra/time.h>
Damjan Marion7cd468a2016-12-19 23:05:39 +010035#include <vpp/api/vpe_msg_enum.h>
36#include <signal.h>
Damjan Marion5fec1e82017-04-13 19:13:47 +020037#include "vppapiclient.h"
Ole Troan3c70c052020-08-10 16:25:21 +020038#include "stat_client.h"
Damjan Marion7cd468a2016-12-19 23:05:39 +010039
40#define vl_typedefs /* define message structures */
41#include <vpp/api/vpe_all_api_h.h>
42#undef vl_typedefs
43
44/* we are not linking with vlib */
45vlib_main_t vlib_global_main;
46vlib_main_t **vlib_mains;
47
48volatile int sigterm_received = 0;
49volatile u32 result_ready;
50volatile u16 result_msg_id;
51
52/* M_NOALLOC: construct, but don't yet send a message */
53
54#define M_NOALLOC(T,t) \
55 do { \
56 result_ready = 0; \
Dave Barachb7b92992018-10-17 10:38:51 -040057 clib_memset (mp, 0, sizeof (*mp)); \
Damjan Marion7cd468a2016-12-19 23:05:39 +010058 mp->_vl_msg_id = ntohs (VL_API_##T); \
59 mp->client_index = am->my_client_index; \
60 } while(0);
61
62
63
Andrey "Zed" Zaikin334167f2018-04-09 16:42:42 +030064void
65wrap_vac_callback (unsigned char *data, int len)
Damjan Marion7cd468a2016-12-19 23:05:39 +010066{
Damjan Marion7cd468a2016-12-19 23:05:39 +010067 result_ready = 1;
68 result_msg_id = ntohs(*((u16 *)data));
Damjan Marion7cd468a2016-12-19 23:05:39 +010069}
70
Ole Troan73710c72018-06-04 22:27:49 +020071static void
72test_connect ()
73{
74 static int i;
75 int rv = vac_connect("vac_client", NULL, wrap_vac_callback, 32 /* rx queue-length*/);
76 if (rv != 0) {
77 printf("Connect failed: %d\n", rv);
78 exit(rv);
79 }
80 printf(".");
81 vac_disconnect();
82 i++;
83}
84
85static void
86test_messages (void)
Damjan Marion7cd468a2016-12-19 23:05:39 +010087{
Dave Barach39d69112019-11-27 11:42:13 -050088 api_main_t * am = vlibapi_get_main();
Damjan Marion7cd468a2016-12-19 23:05:39 +010089 vl_api_show_version_t message;
90 vl_api_show_version_t *mp;
91 int async = 1;
Damjan Marion7cd468a2016-12-19 23:05:39 +010092
Ole Troan73710c72018-06-04 22:27:49 +020093 int rv = vac_connect("vac_client", NULL, wrap_vac_callback, 32 /* rx queue-length*/);
Damjan Marion7cd468a2016-12-19 23:05:39 +010094 if (rv != 0) {
95 printf("Connect failed: %d\n", rv);
96 exit(rv);
97 }
Damjan Marion7cd468a2016-12-19 23:05:39 +010098
Ole Troan3c70c052020-08-10 16:25:21 +020099 double timestamp_start = unix_time_now_nsec() * 1e-6;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100100
101 /*
102 * Test vpe_api_write and vpe_api_read to send and recv message for an
103 * API
104 */
105 int i;
106 long int no_msgs = 10000;
107 mp = &message;
108
109 for (i = 0; i < no_msgs; i++) {
110 /* Construct the API message */
111 M_NOALLOC(SHOW_VERSION, show_version);
Damjan Marion5fec1e82017-04-13 19:13:47 +0200112 vac_write((char *)mp, sizeof(*mp));
Damjan Marion7cd468a2016-12-19 23:05:39 +0100113#ifndef __COVERITY__
114 /* As given, async is always 1. Shut up Coverity about it */
115 if (!async)
116 while (result_ready == 0);
117#endif
118 }
119 if (async) {
120 vl_api_control_ping_t control;
121 vl_api_control_ping_t *mp;
122 mp = &control;
123 M_NOALLOC(CONTROL_PING, control_ping);
Damjan Marion5fec1e82017-04-13 19:13:47 +0200124 vac_write((char *)mp, sizeof(*mp));
Damjan Marion7cd468a2016-12-19 23:05:39 +0100125
126 while (result_msg_id != VL_API_CONTROL_PING_REPLY);
127 }
128
Ole Troan3c70c052020-08-10 16:25:21 +0200129 double timestamp_end = unix_time_now_nsec() * 1e-6;
130 printf("\nTook %.2f msec, %.0f msgs/msec \n", (timestamp_end - timestamp_start),
131 no_msgs/(timestamp_end - timestamp_start));
Damjan Marion5fec1e82017-04-13 19:13:47 +0200132 printf("Exiting...\n");
133 vac_disconnect();
Ole Troan73710c72018-06-04 22:27:49 +0200134}
135
Ole Troan3c70c052020-08-10 16:25:21 +0200136static void
137test_stats (void)
138{
139 clib_mem_trace_enable_disable(1);
140 clib_mem_trace (1);
141
142 int rv = stat_segment_connect (STAT_SEGMENT_SOCKET_FILE);
143 assert(rv == 0);
144
145 u32 *dir;
146 int i, j, k;
147 stat_segment_data_t *res;
148 u8 **pattern = 0;
149 vec_add1(pattern, (u8 *)"/if/names");
150 vec_add1(pattern, (u8 *)"/err");
151
152 dir = stat_segment_ls ((u8 **)pattern);
153
154 res = stat_segment_dump (dir);
155 for (i = 0; i < vec_len (res); i++) {
156 switch (res[i].type) {
157 case STAT_DIR_TYPE_NAME_VECTOR:
158 if (res[i].name_vector == 0)
159 continue;
160 for (k = 0; k < vec_len (res[i].name_vector); k++)
161 if (res[i].name_vector[k])
162 fformat (stdout, "[%d]: %s %s\n", k, res[i].name_vector[k],
163 res[i].name);
164 break;
165 case STAT_DIR_TYPE_ERROR_INDEX:
166 for (j = 0; j < vec_len (res[i].error_vector); j++)
167 fformat (stdout, "%llu %s\n", res[i].error_vector[j],
168 res[i].name);
169 break;
170 default:
171 assert(0);
172 }
173 }
174 stat_segment_data_free (res);
175 stat_segment_disconnect();
176
177 vec_free(pattern);
178 vec_free(dir);
179
180 (void) clib_mem_trace_enable_disable (0);
181 u8 *leak_report = format (0, "%U", format_mheap, clib_mem_get_heap (),
182 1 /* verbose, i.e. print leaks */ );
183 printf("%s", leak_report);
184 vec_free (leak_report);
185 clib_mem_trace (0);
186}
187
Ole Troan73710c72018-06-04 22:27:49 +0200188int main (int argc, char ** argv)
189{
Ole Troan3c70c052020-08-10 16:25:21 +0200190 clib_mem_init (0, 3ULL << 30);
191 test_stats();
192
Ole Troan73710c72018-06-04 22:27:49 +0200193 int i;
194
195 for (i = 0; i < 1000; i++) {
196 test_connect();
197 }
198 test_messages();
Damjan Marion7cd468a2016-12-19 23:05:39 +0100199 exit (0);
200}