FD.io VPP  v21.10.1-2-g0a485f517
Vector Packet Processing
test.c
Go to the documentation of this file.
1 /*
2  *------------------------------------------------------------------
3  * test.c -- VPP API/Stats tests
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>
21 #include <assert.h>
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 
29 
30 #include <vnet/vnet.h>
31 #include <vlib/vlib.h>
32 #include <vlib/unix/unix.h>
33 #include <vlibapi/api.h>
34 #include <vppinfra/time.h>
35 #include <vpp/api/vpe_msg_enum.h>
36 #include <signal.h>
37 #include "vppapiclient.h"
38 #include "stat_client.h"
39 
40 #define vl_typedefs /* define message structures */
41 #include <vpp/api/vpe_all_api_h.h>
42 #undef vl_typedefs
43 
44 volatile int sigterm_received = 0;
45 volatile u32 result_ready;
46 volatile 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; \
53  clib_memset (mp, 0, sizeof (*mp)); \
54  mp->_vl_msg_id = ntohs (VL_API_##T); \
55  mp->client_index = am->my_client_index; \
56  } while(0);
57 
58 
59 
60 void
61 wrap_vac_callback (unsigned char *data, int len)
62 {
63  result_ready = 1;
64  result_msg_id = ntohs(*((u16 *)data));
65 }
66 
67 static void
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(".");
78  i++;
79 }
80 
81 static void
83 {
85  vl_api_show_version_t message;
87  int async = 1;
88 
89  int rv = vac_connect("vac_client", NULL, wrap_vac_callback, 32 /* rx queue-length*/);
90  if (rv != 0) {
91  printf("Connect failed: %d\n", rv);
92  exit(rv);
93  }
94 
95  double timestamp_start = unix_time_now_nsec() * 1e-6;
96 
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);
108  vac_write((char *)mp, sizeof(*mp));
109 #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;
118  mp = &control;
119  M_NOALLOC(CONTROL_PING, control_ping);
120  vac_write((char *)mp, sizeof(*mp));
121 
122  while (result_msg_id != VL_API_CONTROL_PING_REPLY);
123  }
124 
125  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));
128  printf("Exiting...\n");
129  vac_disconnect();
130 }
131 
132 static void
134 {
136  clib_mem_trace (1);
137 
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) {
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;
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  }
172 
173  vec_free(pattern);
174  vec_free(dir);
175 
177  u8 *leak_report = format (0, "%U", format_clib_mem_heap, 0,
178  1 /* verbose, i.e. print leaks */ );
179  printf("%s", leak_report);
180  vec_free (leak_report);
181  clib_mem_trace (0);
182 }
183 
184 int main (int argc, char ** argv)
185 {
186  clib_mem_init (0, 3ULL << 30);
187  test_stats();
188 
189  int i;
190 
191  for (i = 0; i < 1000; i++) {
192  test_connect();
193  }
194  test_messages();
195  exit (0);
196 }
vlib.h
ntohs
#define ntohs(x)
Definition: af_xdp.bpf.c:29
assert
#define assert(x)
Definition: dlmalloc.c:31
name
string name[64]
Definition: fib.api:25
result_msg_id
volatile u16 result_msg_id
Definition: test.c:46
STAT_DIR_TYPE_NAME_VECTOR
@ STAT_DIR_TYPE_NAME_VECTOR
Definition: stat_segment_shared.h:26
format_clib_mem_heap
u8 * format_clib_mem_heap(u8 *s, va_list *va)
Definition: mem_dlmalloc.c:422
result_ready
volatile u32 result_ready
Definition: test.c:45
main
int main()
Definition: test.c:202
u16
unsigned short u16
Definition: types.h:57
am
app_main_t * am
Definition: application.c:489
STAT_SEGMENT_SOCKET_FILE
#define STAT_SEGMENT_SOCKET_FILE
Definition: stat_client.h:32
api.h
wrap_vac_callback
void wrap_vac_callback(unsigned char *data, int len)
Definition: test.c:61
clib_mem_trace_enable_disable
uword clib_mem_trace_enable_disable(uword enable)
Definition: mem_dlmalloc.c:525
stat_segment_ls
uint32_t * stat_segment_ls(uint8_t **patterns)
Definition: stat_client.c:405
stat_segment_disconnect
void stat_segment_disconnect(void)
Definition: stat_client.c:165
M_NOALLOC
#define M_NOALLOC(T, t)
Definition: test.c:50
test_connect
static void test_connect()
Definition: test.c:68
vec_len
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
Definition: vec_bootstrap.h:142
stat_segment_data_free
void stat_segment_data_free(stat_segment_data_t *res)
Definition: stat_client.c:315
len
u8 len
Definition: ip_types.api:103
vec_add1
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:606
stat_segment_connect
int stat_segment_connect(const char *socket_name)
Definition: stat_client.c:151
sigterm_received
volatile int sigterm_received
Definition: test.c:44
unix_time_now_nsec
static u64 unix_time_now_nsec(void)
Definition: time.h:270
vlibapi_get_main
static api_main_t * vlibapi_get_main(void)
Definition: api_common.h:390
time.h
test_stats
static void test_stats(void)
Definition: test.c:133
vac_disconnect
int vac_disconnect(void)
Definition: client.c:359
vpe_all_api_h.h
api_main_t
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:228
data
u8 data[128]
Definition: ipsec_types.api:95
vec_free
#define vec_free(V)
Free vector's memory (no header).
Definition: vec.h:395
stat_segment_data_t
Definition: stat_client.h:35
vl_api_control_ping_t
Control ping from client to api server request.
Definition: vpe.api:60
vl_api_show_version_t
show version
Definition: vpe.api:170
format
description fragment has unexpected format
Definition: map.api:433
u32
unsigned int u32
Definition: types.h:88
test_messages
static void test_messages(void)
Definition: test.c:82
vpe_msg_enum.h
vppapiclient.h
fformat
__clib_export word fformat(FILE *f, char *fmt,...)
Definition: format.c:466
clib_mem_init
void * clib_mem_init(void *base, uword size)
Definition: mem_dlmalloc.c:266
u8
unsigned char u8
Definition: types.h:56
vac_write
int vac_write(char *p, int l)
Definition: client.c:512
unix.h
i
int i
Definition: flowhash_template.h:376
stat_client.h
rv
int __clib_unused rv
Definition: application.c:491
stat_segment_dump
stat_segment_data_t * stat_segment_dump(uint32_t *stats)
Definition: stat_client.c:442
vnet.h
STAT_DIR_TYPE_ERROR_INDEX
@ STAT_DIR_TYPE_ERROR_INDEX
Definition: stat_segment_shared.h:25
clib_mem_trace
void clib_mem_trace(int enable)
Definition: mem_dlmalloc.c:503
type
vl_api_fib_path_type_t type
Definition: fib_types.api:123
vac_connect
int vac_connect(char *name, char *chroot_prefix, vac_callback_t cb, int rx_qlen)
Definition: client.c:288