blob: 9bfed996e1b0fe15c7dcda82490635382a16cf55 [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
Damjan Marion7cd468a2016-12-19 23:05:39 +010044volatile int sigterm_received = 0;
45volatile u32 result_ready;
46volatile u16 result_msg_id;
47
48/* M_NOALLOC: construct, but don't yet send a message */
49
50#define M_NOALLOC(T,t) \
51 do { \
52 result_ready = 0; \
Dave Barachb7b92992018-10-17 10:38:51 -040053 clib_memset (mp, 0, sizeof (*mp)); \
Damjan Marion7cd468a2016-12-19 23:05:39 +010054 mp->_vl_msg_id = ntohs (VL_API_##T); \
55 mp->client_index = am->my_client_index; \
56 } while(0);
57
58
59
Andrey "Zed" Zaikin334167f2018-04-09 16:42:42 +030060void
61wrap_vac_callback (unsigned char *data, int len)
Damjan Marion7cd468a2016-12-19 23:05:39 +010062{
Damjan Marion7cd468a2016-12-19 23:05:39 +010063 result_ready = 1;
64 result_msg_id = ntohs(*((u16 *)data));
Damjan Marion7cd468a2016-12-19 23:05:39 +010065}
66
Ole Troan73710c72018-06-04 22:27:49 +020067static void
68test_connect ()
69{
70 static int i;
71 int rv = vac_connect("vac_client", NULL, wrap_vac_callback, 32 /* rx queue-length*/);
72 if (rv != 0) {
73 printf("Connect failed: %d\n", rv);
74 exit(rv);
75 }
76 printf(".");
77 vac_disconnect();
78 i++;
79}
80
81static void
82test_messages (void)
Damjan Marion7cd468a2016-12-19 23:05:39 +010083{
Dave Barach39d69112019-11-27 11:42:13 -050084 api_main_t * am = vlibapi_get_main();
Damjan Marion7cd468a2016-12-19 23:05:39 +010085 vl_api_show_version_t message;
86 vl_api_show_version_t *mp;
87 int async = 1;
Damjan Marion7cd468a2016-12-19 23:05:39 +010088
Ole Troan73710c72018-06-04 22:27:49 +020089 int rv = vac_connect("vac_client", NULL, wrap_vac_callback, 32 /* rx queue-length*/);
Damjan Marion7cd468a2016-12-19 23:05:39 +010090 if (rv != 0) {
91 printf("Connect failed: %d\n", rv);
92 exit(rv);
93 }
Damjan Marion7cd468a2016-12-19 23:05:39 +010094
Ole Troan3c70c052020-08-10 16:25:21 +020095 double timestamp_start = unix_time_now_nsec() * 1e-6;
Damjan Marion7cd468a2016-12-19 23:05:39 +010096
97 /*
98 * Test vpe_api_write and vpe_api_read to send and recv message for an
99 * API
100 */
101 int i;
102 long int no_msgs = 10000;
103 mp = &message;
104
105 for (i = 0; i < no_msgs; i++) {
106 /* Construct the API message */
107 M_NOALLOC(SHOW_VERSION, show_version);
Damjan Marion5fec1e82017-04-13 19:13:47 +0200108 vac_write((char *)mp, sizeof(*mp));
Damjan Marion7cd468a2016-12-19 23:05:39 +0100109#ifndef __COVERITY__
110 /* As given, async is always 1. Shut up Coverity about it */
111 if (!async)
112 while (result_ready == 0);
113#endif
114 }
115 if (async) {
116 vl_api_control_ping_t control;
117 vl_api_control_ping_t *mp;
118 mp = &control;
119 M_NOALLOC(CONTROL_PING, control_ping);
Damjan Marion5fec1e82017-04-13 19:13:47 +0200120 vac_write((char *)mp, sizeof(*mp));
Damjan Marion7cd468a2016-12-19 23:05:39 +0100121
122 while (result_msg_id != VL_API_CONTROL_PING_REPLY);
123 }
124
Ole Troan3c70c052020-08-10 16:25:21 +0200125 double timestamp_end = unix_time_now_nsec() * 1e-6;
126 printf("\nTook %.2f msec, %.0f msgs/msec \n", (timestamp_end - timestamp_start),
127 no_msgs/(timestamp_end - timestamp_start));
Damjan Marion5fec1e82017-04-13 19:13:47 +0200128 printf("Exiting...\n");
129 vac_disconnect();
Ole Troan73710c72018-06-04 22:27:49 +0200130}
131
Ole Troan3c70c052020-08-10 16:25:21 +0200132static void
133test_stats (void)
134{
135 clib_mem_trace_enable_disable(1);
136 clib_mem_trace (1);
137
138 int rv = stat_segment_connect (STAT_SEGMENT_SOCKET_FILE);
139 assert(rv == 0);
140
141 u32 *dir;
142 int i, j, k;
143 stat_segment_data_t *res;
144 u8 **pattern = 0;
145 vec_add1(pattern, (u8 *)"/if/names");
146 vec_add1(pattern, (u8 *)"/err");
147
148 dir = stat_segment_ls ((u8 **)pattern);
149
150 res = stat_segment_dump (dir);
151 for (i = 0; i < vec_len (res); i++) {
152 switch (res[i].type) {
153 case STAT_DIR_TYPE_NAME_VECTOR:
154 if (res[i].name_vector == 0)
155 continue;
156 for (k = 0; k < vec_len (res[i].name_vector); k++)
157 if (res[i].name_vector[k])
158 fformat (stdout, "[%d]: %s %s\n", k, res[i].name_vector[k],
159 res[i].name);
160 break;
161 case STAT_DIR_TYPE_ERROR_INDEX:
162 for (j = 0; j < vec_len (res[i].error_vector); j++)
163 fformat (stdout, "%llu %s\n", res[i].error_vector[j],
164 res[i].name);
165 break;
166 default:
167 assert(0);
168 }
169 }
170 stat_segment_data_free (res);
171 stat_segment_disconnect();
172
173 vec_free(pattern);
174 vec_free(dir);
175
176 (void) clib_mem_trace_enable_disable (0);
Damjan Marion4537c302020-09-28 19:03:37 +0200177 u8 *leak_report = format (0, "%U", format_clib_mem_heap, 0,
Ole Troan3c70c052020-08-10 16:25:21 +0200178 1 /* verbose, i.e. print leaks */ );
179 printf("%s", leak_report);
180 vec_free (leak_report);
181 clib_mem_trace (0);
182}
183
Ole Troan73710c72018-06-04 22:27:49 +0200184int main (int argc, char ** argv)
185{
Ole Troan3c70c052020-08-10 16:25:21 +0200186 clib_mem_init (0, 3ULL << 30);
187 test_stats();
188
Ole Troan73710c72018-06-04 22:27:49 +0200189 int i;
190
191 for (i = 0; i < 1000; i++) {
192 test_connect();
193 }
194 test_messages();
Damjan Marion7cd468a2016-12-19 23:05:39 +0100195 exit (0);
196}