blob: b7e6f0174450647f966c32776d90a8b54c529b4b [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 <signal.h>
Damjan Marion5fec1e82017-04-13 19:13:47 +020036#include "vppapiclient.h"
Ole Troan3c70c052020-08-10 16:25:21 +020037#include "stat_client.h"
Damjan Marion7cd468a2016-12-19 23:05:39 +010038
Florin Corasa1400ce2021-09-15 09:02:08 -070039#include <vlibmemory/vlib.api_enum.h>
40#include <vlibmemory/vlib.api_types.h>
41
Ole Troan3459ece2021-09-27 17:11:34 +020042#include <vpp/api/vpe.api_enum.h>
43#include <vpp/api/vpe.api_types.h>
Damjan Marion7cd468a2016-12-19 23:05:39 +010044
Damjan Marion7cd468a2016-12-19 23:05:39 +010045volatile int sigterm_received = 0;
46volatile u32 result_ready;
47volatile u16 result_msg_id;
48
49/* M_NOALLOC: construct, but don't yet send a message */
50
51#define M_NOALLOC(T,t) \
52 do { \
53 result_ready = 0; \
Dave Barachb7b92992018-10-17 10:38:51 -040054 clib_memset (mp, 0, sizeof (*mp)); \
Damjan Marion7cd468a2016-12-19 23:05:39 +010055 mp->_vl_msg_id = ntohs (VL_API_##T); \
56 mp->client_index = am->my_client_index; \
57 } while(0);
58
59
60
Andrey "Zed" Zaikin334167f2018-04-09 16:42:42 +030061void
62wrap_vac_callback (unsigned char *data, int len)
Damjan Marion7cd468a2016-12-19 23:05:39 +010063{
Damjan Marion7cd468a2016-12-19 23:05:39 +010064 result_ready = 1;
65 result_msg_id = ntohs(*((u16 *)data));
Damjan Marion7cd468a2016-12-19 23:05:39 +010066}
67
Ole Troan73710c72018-06-04 22:27:49 +020068static void
69test_connect ()
70{
71 static int i;
72 int rv = vac_connect("vac_client", NULL, wrap_vac_callback, 32 /* rx queue-length*/);
73 if (rv != 0) {
74 printf("Connect failed: %d\n", rv);
75 exit(rv);
76 }
77 printf(".");
78 vac_disconnect();
79 i++;
80}
81
82static void
83test_messages (void)
Damjan Marion7cd468a2016-12-19 23:05:39 +010084{
Dave Barach39d69112019-11-27 11:42:13 -050085 api_main_t * am = vlibapi_get_main();
Damjan Marion7cd468a2016-12-19 23:05:39 +010086 vl_api_show_version_t message;
87 vl_api_show_version_t *mp;
88 int async = 1;
Damjan Marion7cd468a2016-12-19 23:05:39 +010089
Ole Troan73710c72018-06-04 22:27:49 +020090 int rv = vac_connect("vac_client", NULL, wrap_vac_callback, 32 /* rx queue-length*/);
Damjan Marion7cd468a2016-12-19 23:05:39 +010091 if (rv != 0) {
92 printf("Connect failed: %d\n", rv);
93 exit(rv);
94 }
Damjan Marion7cd468a2016-12-19 23:05:39 +010095
Ole Troan3c70c052020-08-10 16:25:21 +020096 double timestamp_start = unix_time_now_nsec() * 1e-6;
Damjan Marion7cd468a2016-12-19 23:05:39 +010097
98 /*
99 * Test vpe_api_write and vpe_api_read to send and recv message for an
100 * API
101 */
102 int i;
103 long int no_msgs = 10000;
104 mp = &message;
105
106 for (i = 0; i < no_msgs; i++) {
107 /* Construct the API message */
108 M_NOALLOC(SHOW_VERSION, show_version);
Damjan Marion5fec1e82017-04-13 19:13:47 +0200109 vac_write((char *)mp, sizeof(*mp));
Damjan Marion7cd468a2016-12-19 23:05:39 +0100110#ifndef __COVERITY__
111 /* As given, async is always 1. Shut up Coverity about it */
112 if (!async)
113 while (result_ready == 0);
114#endif
115 }
116 if (async) {
117 vl_api_control_ping_t control;
118 vl_api_control_ping_t *mp;
119 mp = &control;
120 M_NOALLOC(CONTROL_PING, control_ping);
Damjan Marion5fec1e82017-04-13 19:13:47 +0200121 vac_write((char *)mp, sizeof(*mp));
Damjan Marion7cd468a2016-12-19 23:05:39 +0100122
123 while (result_msg_id != VL_API_CONTROL_PING_REPLY);
124 }
125
Ole Troan3c70c052020-08-10 16:25:21 +0200126 double timestamp_end = unix_time_now_nsec() * 1e-6;
127 printf("\nTook %.2f msec, %.0f msgs/msec \n", (timestamp_end - timestamp_start),
128 no_msgs/(timestamp_end - timestamp_start));
Damjan Marion5fec1e82017-04-13 19:13:47 +0200129 printf("Exiting...\n");
130 vac_disconnect();
Ole Troan73710c72018-06-04 22:27:49 +0200131}
132
Ole Troan3c70c052020-08-10 16:25:21 +0200133static void
134test_stats (void)
135{
136 clib_mem_trace_enable_disable(1);
137 clib_mem_trace (1);
138
139 int rv = stat_segment_connect (STAT_SEGMENT_SOCKET_FILE);
140 assert(rv == 0);
141
142 u32 *dir;
143 int i, j, k;
144 stat_segment_data_t *res;
145 u8 **pattern = 0;
146 vec_add1(pattern, (u8 *)"/if/names");
147 vec_add1(pattern, (u8 *)"/err");
148
149 dir = stat_segment_ls ((u8 **)pattern);
150
151 res = stat_segment_dump (dir);
152 for (i = 0; i < vec_len (res); i++) {
153 switch (res[i].type) {
154 case STAT_DIR_TYPE_NAME_VECTOR:
155 if (res[i].name_vector == 0)
156 continue;
157 for (k = 0; k < vec_len (res[i].name_vector); k++)
158 if (res[i].name_vector[k])
159 fformat (stdout, "[%d]: %s %s\n", k, res[i].name_vector[k],
160 res[i].name);
161 break;
162 case STAT_DIR_TYPE_ERROR_INDEX:
163 for (j = 0; j < vec_len (res[i].error_vector); j++)
164 fformat (stdout, "%llu %s\n", res[i].error_vector[j],
165 res[i].name);
166 break;
167 default:
168 assert(0);
169 }
170 }
171 stat_segment_data_free (res);
172 stat_segment_disconnect();
173
174 vec_free(pattern);
175 vec_free(dir);
176
177 (void) clib_mem_trace_enable_disable (0);
Damjan Marion4537c302020-09-28 19:03:37 +0200178 u8 *leak_report = format (0, "%U", format_clib_mem_heap, 0,
Ole Troan3c70c052020-08-10 16:25:21 +0200179 1 /* verbose, i.e. print leaks */ );
180 printf("%s", leak_report);
181 vec_free (leak_report);
182 clib_mem_trace (0);
183}
184
Ole Troan73710c72018-06-04 22:27:49 +0200185int main (int argc, char ** argv)
186{
Ole Troan3c70c052020-08-10 16:25:21 +0200187 clib_mem_init (0, 3ULL << 30);
188 test_stats();
189
Ole Troan73710c72018-06-04 22:27:49 +0200190 int i;
191
192 for (i = 0; i < 1000; i++) {
193 test_connect();
194 }
195 test_messages();
Damjan Marion7cd468a2016-12-19 23:05:39 +0100196 exit (0);
197}