FD.io VPP  v21.01.1
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 /* we are not linking with vlib */
47 
48 volatile int sigterm_received = 0;
49 volatile u32 result_ready;
50 volatile 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; \
57  clib_memset (mp, 0, sizeof (*mp)); \
58  mp->_vl_msg_id = ntohs (VL_API_##T); \
59  mp->client_index = am->my_client_index; \
60  } while(0);
61 
62 
63 
64 void
65 wrap_vac_callback (unsigned char *data, int len)
66 {
67  result_ready = 1;
68  result_msg_id = ntohs(*((u16 *)data));
69 }
70 
71 static void
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(".");
82  i++;
83 }
84 
85 static void
87 {
89  vl_api_show_version_t message;
91  int async = 1;
92 
93  int rv = vac_connect("vac_client", NULL, wrap_vac_callback, 32 /* rx queue-length*/);
94  if (rv != 0) {
95  printf("Connect failed: %d\n", rv);
96  exit(rv);
97  }
98 
99  double timestamp_start = unix_time_now_nsec() * 1e-6;
100 
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);
112  vac_write((char *)mp, sizeof(*mp));
113 #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;
122  mp = &control;
123  M_NOALLOC(CONTROL_PING, control_ping);
124  vac_write((char *)mp, sizeof(*mp));
125 
126  while (result_msg_id != VL_API_CONTROL_PING_REPLY);
127  }
128 
129  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));
132  printf("Exiting...\n");
133  vac_disconnect();
134 }
135 
136 static void
138 {
140  clib_mem_trace (1);
141 
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) {
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;
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  }
176 
177  vec_free(pattern);
178  vec_free(dir);
179 
181  u8 *leak_report = format (0, "%U", format_clib_mem_heap, 0,
182  1 /* verbose, i.e. print leaks */ );
183  printf("%s", leak_report);
184  vec_free (leak_report);
185  clib_mem_trace (0);
186 }
187 
188 int main (int argc, char ** argv)
189 {
190  clib_mem_init (0, 3ULL << 30);
191  test_stats();
192 
193  int i;
194 
195  for (i = 0; i < 1000; i++) {
196  test_connect();
197  }
198  test_messages();
199  exit (0);
200 }
#define M_NOALLOC(T, t)
Definition: test.c:54
int stat_segment_connect(const char *socket_name)
Definition: stat_client.c:151
#define ntohs(x)
Definition: af_xdp.bpf.c:29
Control ping from client to api server request.
Definition: vpe.api:60
void stat_segment_data_free(stat_segment_data_t *res)
Definition: stat_client.c:278
void * clib_mem_init(void *base, uword size)
Definition: mem_dlmalloc.c:266
static void test_connect()
Definition: test.c:72
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:592
void wrap_vac_callback(unsigned char *data, int len)
Definition: test.c:65
int vac_write(char *p, int l)
Definition: client.c:513
int main()
Definition: test.c:202
unsigned char u8
Definition: types.h:56
u8 data[128]
Definition: ipsec_types.api:90
#define assert(x)
Definition: dlmalloc.c:31
__clib_export word fformat(FILE *f, char *fmt,...)
Definition: format.c:462
static void test_stats(void)
Definition: test.c:137
description fragment has unexpected format
Definition: map.api:433
unsigned int u32
Definition: types.h:88
volatile u16 result_msg_id
Definition: test.c:50
uword clib_mem_trace_enable_disable(uword enable)
Definition: mem_dlmalloc.c:522
volatile u32 result_ready
Definition: test.c:49
vl_api_fib_path_type_t type
Definition: fib_types.api:123
int vac_connect(char *name, char *chroot_prefix, vac_callback_t cb, int rx_qlen)
Definition: client.c:291
int vac_disconnect(void)
Definition: client.c:362
unsigned short u16
Definition: types.h:57
void stat_segment_disconnect(void)
Definition: stat_client.c:165
u8 len
Definition: ip_types.api:103
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:227
#define STAT_SEGMENT_SOCKET_FILE
Definition: stat_client.h:32
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:317
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:380
static void test_messages(void)
Definition: test.c:86
static u64 unix_time_now_nsec(void)
Definition: time.h:270
string name[64]
Definition: ip.api:44
uint32_t * stat_segment_ls(uint8_t **patterns)
Definition: stat_client.c:368
u8 * format_clib_mem_heap(u8 *s, va_list *va)
Definition: mem_dlmalloc.c:422
stat_segment_data_t * stat_segment_dump(uint32_t *stats)
Definition: stat_client.c:405
show version
Definition: vpe.api:170
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
vlib_main_t ** vlib_mains
Definition: test.c:46
volatile int sigterm_received
Definition: test.c:48
static api_main_t * vlibapi_get_main(void)
Definition: api_common.h:389
vlib_main_t vlib_global_main
Definition: test.c:45
void clib_mem_trace(int enable)
Definition: mem_dlmalloc.c:500