blob: 83cdbc1be6d48f79ea6aba210176ca6fd4e3a056 [file] [log] [blame]
Florin Coras6792ec02017-03-13 03:49:51 -07001/*
2 * builtin_client.c - vpp built-in tcp client/connect code
3 *
4 * Copyright (c) 2017 by Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include <vnet/vnet.h>
19#include <vnet/plugin/plugin.h>
20#include <vnet/tcp/builtin_client.h>
21
22#include <vlibapi/api.h>
23#include <vlibmemory/api.h>
24#include <vlibsocket/api.h>
25#include <vpp/app/version.h>
26
27/* define message IDs */
28#include <vpp/api/vpe_msg_enum.h>
29
30/* define message structures */
31#define vl_typedefs
32#include <vpp/api/vpe_all_api_h.h>
33#undef vl_typedefs
34
35/* define generated endian-swappers */
36#define vl_endianfun
37#include <vpp/api/vpe_all_api_h.h>
38#undef vl_endianfun
39
40/* instantiate all the print functions we know about */
41#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
42#define vl_printfun
43#include <vpp/api/vpe_all_api_h.h>
44#undef vl_printfun
45
46static void
47send_test_chunk (tclient_main_t * tm, session_t * s)
48{
49 u8 *test_data = tm->connect_test_data;
Dave Barach45ce3fb2017-03-28 12:31:33 -040050 int test_buf_offset;
Florin Coras6792ec02017-03-13 03:49:51 -070051 u32 bytes_this_chunk;
52 session_fifo_event_t evt;
53 static int serial_number = 0;
54 int rv;
55
56 while (s->bytes_to_send > 0)
57 {
Dave Barach45ce3fb2017-03-28 12:31:33 -040058
59 test_buf_offset = s->bytes_sent % vec_len (test_data);
60 bytes_this_chunk = vec_len (test_data) - test_buf_offset;
61
62 bytes_this_chunk = bytes_this_chunk < s->bytes_to_send
63 ? bytes_this_chunk : s->bytes_to_send;
Florin Coras6792ec02017-03-13 03:49:51 -070064
65 rv = svm_fifo_enqueue_nowait (s->server_tx_fifo, 0 /*pid */ ,
66 bytes_this_chunk,
67 test_data + test_buf_offset);
68
69 if (rv > 0)
70 {
71 s->bytes_to_send -= rv;
Dave Barach45ce3fb2017-03-28 12:31:33 -040072 s->bytes_sent += rv;
Florin Coras6792ec02017-03-13 03:49:51 -070073
74 if (svm_fifo_set_event (s->server_tx_fifo))
75 {
76 /* Fabricate TX event, send to vpp */
77 evt.fifo = s->server_tx_fifo;
78 evt.event_type = FIFO_EVENT_SERVER_TX;
79 evt.event_id = serial_number++;
80
81 unix_shared_memory_queue_add (tm->vpp_event_queue, (u8 *) & evt,
82 0 /* do wait for mutex */ );
83 }
84 }
85 }
86}
87
88static void
89receive_test_chunk (tclient_main_t * tm, session_t * s)
90{
91 svm_fifo_t *rx_fifo = s->server_rx_fifo;
92 int n_read, bytes, i;
93
94 bytes = svm_fifo_max_dequeue (rx_fifo);
95 /* Allow enqueuing of new event */
96 svm_fifo_unset_event (rx_fifo);
97
98 /* Read the bytes */
99 do
100 {
101 n_read = svm_fifo_dequeue_nowait (rx_fifo, 0, vec_len (tm->rx_buf),
102 tm->rx_buf);
103 if (n_read > 0)
104 {
105 bytes -= n_read;
106 for (i = 0; i < n_read; i++)
107 {
108 if (tm->rx_buf[i] != ((s->bytes_received + i) & 0xff))
109 {
110 clib_warning ("read %d error at byte %lld, 0x%x not 0x%x",
111 n_read, s->bytes_received + i,
112 tm->rx_buf[i],
113 ((s->bytes_received + i) & 0xff));
114 }
115 }
116 s->bytes_to_receive -= n_read;
117 s->bytes_received += n_read;
118 }
119
120 }
121 while (n_read < 0 || bytes > 0);
122}
123
124static void *
125tclient_thread_fn (void *arg)
126{
127 tclient_main_t *tm = &tclient_main;
128 vl_api_disconnect_session_t *dmp;
129 session_t *sp;
130 struct timespec ts, tsrem;
131 int i;
132 int try_tx, try_rx;
133 u32 *session_indices = 0;
134
135 /* stats thread wants no signals. */
136 {
137 sigset_t s;
138 sigfillset (&s);
139 pthread_sigmask (SIG_SETMASK, &s, 0);
140 }
141
142 while (1)
143 {
144 /* Wait until we're told to get busy */
145 while (tm->run_test == 0
146 || (tm->ready_connections != tm->expected_connections))
147 {
148 ts.tv_sec = 0;
149 ts.tv_nsec = 100000000;
150 while (nanosleep (&ts, &tsrem) < 0)
151 ts = tsrem;
152 }
153 tm->run_test = 0;
154
155 clib_warning ("Run %d iterations", tm->n_iterations);
156
157 for (i = 0; i < tm->n_iterations; i++)
158 {
159 session_t *sp;
160
161 do
162 {
163 try_tx = try_rx = 0;
164
165 /* *INDENT-OFF* */
166 pool_foreach (sp, tm->sessions, ({
167 if (sp->bytes_to_send > 0)
168 {
169 send_test_chunk (tm, sp);
170 try_tx = 1;
171 }
172 }));
173 pool_foreach (sp, tm->sessions, ({
174 if (sp->bytes_to_receive > 0)
175 {
176 receive_test_chunk (tm, sp);
177 try_rx = 1;
178 }
179 }));
180 /* *INDENT-ON* */
181
182 }
183 while (try_tx || try_rx);
184 }
185 clib_warning ("Done %d iterations", tm->n_iterations);
186
187 /* Disconnect sessions... */
188 vec_reset_length (session_indices);
189 pool_foreach (sp, tm->sessions, (
190 {
191 vec_add1 (session_indices,
192 sp - tm->sessions);
193 }
194 ));
195
196 for (i = 0; i < vec_len (session_indices); i++)
197 {
198 sp = pool_elt_at_index (tm->sessions, session_indices[i]);
199 dmp = vl_msg_api_alloc_as_if_client (sizeof (*dmp));
200 memset (dmp, 0, sizeof (*dmp));
201 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
202 dmp->client_index = tm->my_client_index;
203 dmp->session_index = sp->vpp_session_index;
204 dmp->session_thread_index = sp->vpp_session_thread;
205 vl_msg_api_send_shmem (tm->vl_input_queue, (u8 *) & dmp);
206 pool_put (tm->sessions, sp);
207 }
208 }
209 /* NOTREACHED */
210 return 0;
211}
212
213/* So we don't get "no handler for... " msgs */
214static void
215vl_api_memclnt_create_reply_t_handler (vl_api_memclnt_create_reply_t * mp)
216{
217 tclient_main_t *tm = &tclient_main;
218
219 tm->my_client_index = mp->index;
220}
221
222static void
223vl_api_connect_uri_reply_t_handler (vl_api_connect_uri_reply_t * mp)
224{
225 tclient_main_t *tm = &tclient_main;
226 session_t *session;
227 u32 session_index;
228 u64 key;
229 i32 retval = /* clib_net_to_host_u32 ( */ mp->retval /*) */ ;
230
231 if (retval < 0)
232 {
233 clib_warning ("connection failed: retval %d", retval);
234 return;
235 }
236
237 tm->our_event_queue = (unix_shared_memory_queue_t *)
238 mp->vpp_event_queue_address;
239
240 tm->vpp_event_queue = (unix_shared_memory_queue_t *)
241 mp->vpp_event_queue_address;
242
243 /*
244 * Setup session
245 */
246 pool_get (tm->sessions, session);
247 memset (session, 0, sizeof (*session));
248 session_index = session - tm->sessions;
249 session->bytes_to_receive = session->bytes_to_send = tm->bytes_to_send;
250
251 session->server_rx_fifo = (svm_fifo_t *) mp->server_rx_fifo;
252 session->server_rx_fifo->client_session_index = session_index;
253 session->server_tx_fifo = (svm_fifo_t *) mp->server_tx_fifo;
254 session->server_tx_fifo->client_session_index = session_index;
255
256 session->vpp_session_index = mp->session_index;
257 session->vpp_session_thread = mp->session_thread_index;
258
259 /* Add it to the session lookup table */
260 key = (((u64) mp->session_thread_index) << 32) | (u64) mp->session_index;
261 hash_set (tm->session_index_by_vpp_handles, key, session_index);
262
263 tm->ready_connections++;
264}
265
266static void
267create_api_loopback (tclient_main_t * tm)
268{
269 vl_api_memclnt_create_t _m, *mp = &_m;
270 extern void vl_api_memclnt_create_t_handler (vl_api_memclnt_create_t *);
271 api_main_t *am = &api_main;
272 vl_shmem_hdr_t *shmem_hdr;
273
274 /*
275 * Create a "loopback" API client connection
276 * Don't do things like this unless you know what you're doing...
277 */
278
279 shmem_hdr = am->shmem_hdr;
280 tm->vl_input_queue = shmem_hdr->vl_input_queue;
281 memset (mp, 0, sizeof (*mp));
282 mp->_vl_msg_id = VL_API_MEMCLNT_CREATE;
283 mp->context = 0xFEEDFACE;
284 mp->input_queue = (u64) tm->vl_input_queue;
285 strncpy ((char *) mp->name, "tcp_tester", sizeof (mp->name) - 1);
286
287 vl_api_memclnt_create_t_handler (mp);
288}
289
290#define foreach_tclient_static_api_msg \
291_(MEMCLNT_CREATE_REPLY, memclnt_create_reply) \
292_(CONNECT_URI_REPLY, connect_uri_reply)
293
294static clib_error_t *
295tclient_api_hookup (vlib_main_t * vm)
296{
297 tclient_main_t *tm = &tclient_main;
298 vl_msg_api_msg_config_t _c, *c = &_c;
299 int i;
300
301 /* Init test data */
302 vec_validate (tm->connect_test_data, 64 * 1024 - 1);
303 for (i = 0; i < vec_len (tm->connect_test_data); i++)
304 tm->connect_test_data[i] = i & 0xff;
305
306 tm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
307 vec_validate (tm->rx_buf, vec_len (tm->connect_test_data) - 1);
308
309 /* Hook up client-side static APIs to our handlers */
310#define _(N,n) do { \
311 c->id = VL_API_##N; \
312 c->name = #n; \
313 c->handler = vl_api_##n##_t_handler; \
314 c->cleanup = vl_noop_handler; \
315 c->endian = vl_api_##n##_t_endian; \
316 c->print = vl_api_##n##_t_print; \
317 c->size = sizeof(vl_api_##n##_t); \
318 c->traced = 1; /* trace, so these msgs print */ \
319 c->replay = 0; /* don't replay client create/delete msgs */ \
320 c->message_bounce = 0; /* don't bounce this message */ \
321 vl_msg_api_config(c);} while (0);
322
323 foreach_tclient_static_api_msg;
324#undef _
325
326 return 0;
327}
328
329VLIB_API_INIT_FUNCTION (tclient_api_hookup);
330
331static clib_error_t *
332test_tcp_clients_command_fn (vlib_main_t * vm,
333 unformat_input_t * input,
334 vlib_cli_command_t * cmd)
335{
336 u8 *connect_uri = (u8 *) "tcp://6.0.1.2/1234";
337 u8 *uri;
338 tclient_main_t *tm = &tclient_main;
339 int i;
340 u32 n_clients = 1;
341
342 tm->bytes_to_send = 8192;
343 tm->n_iterations = 1;
344 vec_free (tm->connect_uri);
345
346 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
347 {
348 if (unformat (input, "nclients %d", &n_clients))
349 ;
350 else if (unformat (input, "iterations %d", &tm->n_iterations))
351 ;
352 else if (unformat (input, "bytes %d", &tm->bytes_to_send))
353 ;
354 else if (unformat (input, "uri %s", &tm->connect_uri))
355 ;
356 else
357 return clib_error_return (0, "unknown input `%U'",
358 format_unformat_error, input);
359 }
360
361 tm->ready_connections = 0;
362 tm->expected_connections = n_clients;
363 uri = connect_uri;
364 if (tm->connect_uri)
365 uri = tm->connect_uri;
366
367 create_api_loopback (tm);
368
369 /* Start a transmit thread */
370 if (tm->client_thread_handle == 0)
371 {
372 int rv = pthread_create (&tm->client_thread_handle,
373 NULL /*attr */ , tclient_thread_fn, 0);
374 if (rv)
375 {
376 tm->client_thread_handle = 0;
377 return clib_error_return (0, "pthread_create returned %d", rv);
378 }
379 }
380
381 /* Fire off connect requests, in something approaching a normal manner */
382 for (i = 0; i < n_clients; i++)
383 {
384 vl_api_connect_uri_t *cmp;
385 cmp = vl_msg_api_alloc_as_if_client (sizeof (*cmp));
386 memset (cmp, 0, sizeof (*cmp));
387
388 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI);
389 cmp->client_index = tm->my_client_index;
390 cmp->context = ntohl (0xfeedface);
391 memcpy (cmp->uri, uri, strlen ((char *) uri) + 1);
392 vl_msg_api_send_shmem (tm->vl_input_queue, (u8 *) & cmp);
393 }
394
395 tm->run_test = 1;
396
397 return 0;
398}
399
400/* *INDENT-OFF* */
401VLIB_CLI_COMMAND (test_clients_command, static) =
402{
403 .path = "test tcp clients",
404 .short_help = "test tcp clients",
405 .function = test_tcp_clients_command_fn,
406};
407/* *INDENT-ON* */
408
409/*
410 * fd.io coding-style-patch-verification: ON
411 *
412 * Local Variables:
413 * eval: (c-set-style "gnu")
414 * End:
415 */