blob: 6ee91f9c54baf8a46c39aef4d0796e87bdfa0add [file] [log] [blame]
Florin Coras4399c2e2018-01-25 06:34:42 -08001/*
2 * echo_client.c - vpp built-in echo client 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 <vlibapi/api.h>
20#include <vlibmemory/api.h>
21#include <vnet/session-apps/echo_client.h>
22
23echo_client_main_t echo_client_main;
24
25#define ECHO_CLIENT_DBG (0)
26
27static void
28signal_evt_to_cli_i (int *code)
29{
30 echo_client_main_t *ecm = &echo_client_main;
31 ASSERT (vlib_get_thread_index () == 0);
32 vlib_process_signal_event (ecm->vlib_main, ecm->cli_node_index, *code, 0);
33}
34
35static void
36signal_evt_to_cli (int code)
37{
38 if (vlib_get_thread_index () != 0)
39 vl_api_rpc_call_main_thread (signal_evt_to_cli_i, (u8 *) & code,
40 sizeof (code));
41 else
42 signal_evt_to_cli_i (&code);
43}
44
45static void
Florin Coras48750892018-05-16 09:28:02 -070046send_data_chunk (echo_client_main_t * ecm, eclient_session_t * s)
Florin Coras4399c2e2018-01-25 06:34:42 -080047{
48 u8 *test_data = ecm->connect_test_data;
Florin Coras7fb0fe12018-04-09 09:24:52 -070049 int test_buf_len, test_buf_offset, rv;
Florin Coras4399c2e2018-01-25 06:34:42 -080050 u32 bytes_this_chunk;
Florin Coras4399c2e2018-01-25 06:34:42 -080051
Florin Coras7fb0fe12018-04-09 09:24:52 -070052 test_buf_len = vec_len (test_data);
Florin Coras6c354942018-04-18 00:05:21 -070053 ASSERT (test_buf_len > 0);
Florin Coras7fb0fe12018-04-09 09:24:52 -070054 test_buf_offset = s->bytes_sent % test_buf_len;
55 bytes_this_chunk = clib_min (test_buf_len - test_buf_offset,
56 s->bytes_to_send);
Florin Coras4399c2e2018-01-25 06:34:42 -080057
Florin Coras7fb0fe12018-04-09 09:24:52 -070058 if (!ecm->is_dgram)
Florin Coras8e43d042018-05-04 15:46:57 -070059 {
60 if (ecm->no_copy)
61 {
62 svm_fifo_t *f = s->data.tx_fifo;
63 rv = clib_min (svm_fifo_max_enqueue (f), bytes_this_chunk);
64 svm_fifo_enqueue_nocopy (f, rv);
65 if (svm_fifo_set_event (f))
66 {
67 session_fifo_event_t evt;
68 evt.fifo = f;
69 evt.event_type = FIFO_EVENT_APP_TX;
70 svm_queue_add (s->data.vpp_evt_q, (u8 *) & evt, 0);
71 }
72 }
73 else
74 rv = app_send_stream (&s->data, test_data + test_buf_offset,
75 bytes_this_chunk, 0);
76 }
Florin Coras7fb0fe12018-04-09 09:24:52 -070077 else
Florin Coras8e43d042018-05-04 15:46:57 -070078 {
79 if (ecm->no_copy)
80 {
81 session_dgram_hdr_t hdr;
82 svm_fifo_t *f = s->data.tx_fifo;
83 app_session_transport_t *at = &s->data.transport;
84 u32 max_enqueue = svm_fifo_max_enqueue (f);
85
86 if (max_enqueue <= sizeof (session_dgram_hdr_t))
87 return;
88
89 max_enqueue -= sizeof (session_dgram_hdr_t);
90 rv = clib_min (max_enqueue, bytes_this_chunk);
91
92 hdr.data_length = rv;
93 hdr.data_offset = 0;
94 clib_memcpy (&hdr.rmt_ip, &at->rmt_ip, sizeof (ip46_address_t));
95 hdr.is_ip4 = at->is_ip4;
96 hdr.rmt_port = at->rmt_port;
97 clib_memcpy (&hdr.lcl_ip, &at->lcl_ip, sizeof (ip46_address_t));
98 hdr.lcl_port = at->lcl_port;
99 svm_fifo_enqueue_nowait (f, sizeof (hdr), (u8 *) & hdr);
100 svm_fifo_enqueue_nocopy (f, rv);
101 if (svm_fifo_set_event (f))
102 {
103 session_fifo_event_t evt;
104 evt.fifo = f;
105 evt.event_type = FIFO_EVENT_APP_TX;
106 svm_queue_add (s->data.vpp_evt_q, (u8 *) & evt, 0);
107 }
108 }
109 else
110 rv = app_send_dgram (&s->data, test_data + test_buf_offset,
111 bytes_this_chunk, 0);
112 }
Florin Coras4399c2e2018-01-25 06:34:42 -0800113
114 /* If we managed to enqueue data... */
115 if (rv > 0)
116 {
117 /* Account for it... */
118 s->bytes_to_send -= rv;
119 s->bytes_sent += rv;
120
121 if (ECHO_CLIENT_DBG)
122 {
123 /* *INDENT-OFF* */
124 ELOG_TYPE_DECLARE (e) =
125 {
126 .format = "tx-enq: xfer %d bytes, sent %u remain %u",
127 .format_args = "i4i4i4",
128 };
129 /* *INDENT-ON* */
130 struct
131 {
132 u32 data[3];
133 } *ed;
134 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
135 ed->data[0] = rv;
136 ed->data[1] = s->bytes_sent;
137 ed->data[2] = s->bytes_to_send;
138 }
Florin Coras4399c2e2018-01-25 06:34:42 -0800139 }
140}
141
142static void
Florin Coras48750892018-05-16 09:28:02 -0700143receive_data_chunk (echo_client_main_t * ecm, eclient_session_t * s)
Florin Coras4399c2e2018-01-25 06:34:42 -0800144{
Florin Coras7fb0fe12018-04-09 09:24:52 -0700145 svm_fifo_t *rx_fifo = s->data.rx_fifo;
146 u32 thread_index = vlib_get_thread_index ();
Florin Coras4399c2e2018-01-25 06:34:42 -0800147 int n_read, i;
148
149 if (ecm->test_bytes)
150 {
Florin Coras7fb0fe12018-04-09 09:24:52 -0700151 if (!ecm->is_dgram)
152 n_read = app_recv_stream (&s->data, ecm->rx_buf[thread_index],
153 vec_len (ecm->rx_buf[thread_index]));
154 else
155 n_read = app_recv_dgram (&s->data, ecm->rx_buf[thread_index],
156 vec_len (ecm->rx_buf[thread_index]));
Florin Coras4399c2e2018-01-25 06:34:42 -0800157 }
158 else
159 {
160 n_read = svm_fifo_max_dequeue (rx_fifo);
161 svm_fifo_dequeue_drop (rx_fifo, n_read);
162 }
163
164 if (n_read > 0)
165 {
166 if (ECHO_CLIENT_DBG)
167 {
168 /* *INDENT-OFF* */
169 ELOG_TYPE_DECLARE (e) =
170 {
171 .format = "rx-deq: %d bytes",
172 .format_args = "i4",
173 };
174 /* *INDENT-ON* */
175 struct
176 {
177 u32 data[1];
178 } *ed;
179 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
180 ed->data[0] = n_read;
181 }
182
183 if (ecm->test_bytes)
184 {
185 for (i = 0; i < n_read; i++)
186 {
Florin Coras7fb0fe12018-04-09 09:24:52 -0700187 if (ecm->rx_buf[thread_index][i]
Florin Coras4399c2e2018-01-25 06:34:42 -0800188 != ((s->bytes_received + i) & 0xff))
189 {
190 clib_warning ("read %d error at byte %lld, 0x%x not 0x%x",
191 n_read, s->bytes_received + i,
Florin Coras7fb0fe12018-04-09 09:24:52 -0700192 ecm->rx_buf[thread_index][i],
Florin Coras4399c2e2018-01-25 06:34:42 -0800193 ((s->bytes_received + i) & 0xff));
194 ecm->test_failed = 1;
195 }
196 }
197 }
Florin Coras7fb0fe12018-04-09 09:24:52 -0700198 ASSERT (n_read <= s->bytes_to_receive);
Florin Coras4399c2e2018-01-25 06:34:42 -0800199 s->bytes_to_receive -= n_read;
200 s->bytes_received += n_read;
201 }
202}
203
204static uword
205echo_client_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
206 vlib_frame_t * frame)
207{
208 echo_client_main_t *ecm = &echo_client_main;
209 int my_thread_index = vlib_get_thread_index ();
Florin Coras48750892018-05-16 09:28:02 -0700210 eclient_session_t *sp;
Florin Coras4399c2e2018-01-25 06:34:42 -0800211 int i;
212 int delete_session;
213 u32 *connection_indices;
214 u32 *connections_this_batch;
215 u32 nconnections_this_batch;
216
217 connection_indices = ecm->connection_index_by_thread[my_thread_index];
218 connections_this_batch =
219 ecm->connections_this_batch_by_thread[my_thread_index];
220
221 if ((ecm->run_test == 0) ||
222 ((vec_len (connection_indices) == 0)
223 && vec_len (connections_this_batch) == 0))
224 return 0;
225
226 /* Grab another pile of connections */
227 if (PREDICT_FALSE (vec_len (connections_this_batch) == 0))
228 {
229 nconnections_this_batch =
230 clib_min (ecm->connections_per_batch, vec_len (connection_indices));
231
232 ASSERT (nconnections_this_batch > 0);
233 vec_validate (connections_this_batch, nconnections_this_batch - 1);
234 clib_memcpy (connections_this_batch,
235 connection_indices + vec_len (connection_indices)
236 - nconnections_this_batch,
237 nconnections_this_batch * sizeof (u32));
238 _vec_len (connection_indices) -= nconnections_this_batch;
239 }
240
241 if (PREDICT_FALSE (ecm->prev_conns != ecm->connections_per_batch
242 && ecm->prev_conns == vec_len (connections_this_batch)))
243 {
244 ecm->repeats++;
245 ecm->prev_conns = vec_len (connections_this_batch);
246 if (ecm->repeats == 500000)
247 {
248 clib_warning ("stuck clients");
249 }
250 }
251 else
252 {
253 ecm->prev_conns = vec_len (connections_this_batch);
254 ecm->repeats = 0;
255 }
256
257 for (i = 0; i < vec_len (connections_this_batch); i++)
258 {
259 delete_session = 1;
260
261 sp = pool_elt_at_index (ecm->sessions, connections_this_batch[i]);
262
263 if (sp->bytes_to_send > 0)
264 {
265 send_data_chunk (ecm, sp);
266 delete_session = 0;
267 }
268 if (sp->bytes_to_receive > 0)
269 {
Florin Coras4399c2e2018-01-25 06:34:42 -0800270 delete_session = 0;
271 }
272 if (PREDICT_FALSE (delete_session == 1))
273 {
Florin Coras4399c2e2018-01-25 06:34:42 -0800274 stream_session_t *s;
275
276 __sync_fetch_and_add (&ecm->tx_total, sp->bytes_sent);
277 __sync_fetch_and_add (&ecm->rx_total, sp->bytes_received);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700278 s = session_get_from_handle_if_valid (sp->vpp_session_handle);
Florin Coras4399c2e2018-01-25 06:34:42 -0800279
280 if (s)
281 {
282 vnet_disconnect_args_t _a, *a = &_a;
283 a->handle = session_handle (s);
284 a->app_index = ecm->app_index;
285 vnet_disconnect_session (a);
286
287 vec_delete (connections_this_batch, 1, i);
288 i--;
289 __sync_fetch_and_add (&ecm->ready_connections, -1);
290 }
291 else
Florin Coras2f8d8fa2018-01-26 06:36:04 -0800292 {
293 clib_warning ("session AWOL?");
294 vec_delete (connections_this_batch, 1, i);
295 }
Florin Coras4399c2e2018-01-25 06:34:42 -0800296
297 /* Kick the debug CLI process */
298 if (ecm->ready_connections == 0)
299 {
300 signal_evt_to_cli (2);
301 }
302 }
303 }
304
305 ecm->connection_index_by_thread[my_thread_index] = connection_indices;
306 ecm->connections_this_batch_by_thread[my_thread_index] =
307 connections_this_batch;
308 return 0;
309}
310
311/* *INDENT-OFF* */
312VLIB_REGISTER_NODE (echo_clients_node) =
313{
314 .function = echo_client_node_fn,
315 .name = "echo-clients",
316 .type = VLIB_NODE_TYPE_INPUT,
317 .state = VLIB_NODE_STATE_DISABLED,
318};
319/* *INDENT-ON* */
320
321static int
322create_api_loopback (echo_client_main_t * ecm)
323{
324 api_main_t *am = &api_main;
325 vl_shmem_hdr_t *shmem_hdr;
326
327 shmem_hdr = am->shmem_hdr;
328 ecm->vl_input_queue = shmem_hdr->vl_input_queue;
329 ecm->my_client_index = vl_api_memclnt_create_internal ("echo_client",
330 ecm->vl_input_queue);
331 return 0;
332}
333
334static int
335echo_clients_init (vlib_main_t * vm)
336{
337 echo_client_main_t *ecm = &echo_client_main;
338 vlib_thread_main_t *vtm = vlib_get_thread_main ();
339 u32 num_threads;
340 int i;
341
342 if (create_api_loopback (ecm))
343 return -1;
344
345 num_threads = 1 /* main thread */ + vtm->n_threads;
346
Florin Coras7fb0fe12018-04-09 09:24:52 -0700347 /* Init test data. Big buffer */
348 vec_validate (ecm->connect_test_data, 4 * 1024 * 1024 - 1);
Florin Coras4399c2e2018-01-25 06:34:42 -0800349 for (i = 0; i < vec_len (ecm->connect_test_data); i++)
350 ecm->connect_test_data[i] = i & 0xff;
351
352 vec_validate (ecm->rx_buf, num_threads - 1);
353 for (i = 0; i < num_threads; i++)
354 vec_validate (ecm->rx_buf[i], vec_len (ecm->connect_test_data) - 1);
355
356 ecm->is_init = 1;
357
358 vec_validate (ecm->connection_index_by_thread, vtm->n_vlib_mains);
359 vec_validate (ecm->connections_this_batch_by_thread, vtm->n_vlib_mains);
360 vec_validate (ecm->vpp_event_queue, vtm->n_vlib_mains);
361
362 return 0;
363}
364
365static int
366echo_clients_session_connected_callback (u32 app_index, u32 api_context,
367 stream_session_t * s, u8 is_fail)
368{
369 echo_client_main_t *ecm = &echo_client_main;
Florin Coras48750892018-05-16 09:28:02 -0700370 eclient_session_t *session;
Florin Coras4399c2e2018-01-25 06:34:42 -0800371 u32 session_index;
Florin Coras40903ac2018-06-10 14:41:23 -0700372 u8 thread_index = s->thread_index;
Florin Coras4399c2e2018-01-25 06:34:42 -0800373
374 if (is_fail)
375 {
376 clib_warning ("connection %d failed!", api_context);
377 signal_evt_to_cli (-1);
378 return 0;
379 }
380
Florin Coras40903ac2018-06-10 14:41:23 -0700381 ASSERT (thread_index == vlib_get_thread_index ()
382 || session_transport_service_type (s) == TRANSPORT_SERVICE_CL);
Florin Coras4399c2e2018-01-25 06:34:42 -0800383
384 if (!ecm->vpp_event_queue[thread_index])
385 ecm->vpp_event_queue[thread_index] =
386 session_manager_get_vpp_event_queue (thread_index);
387
388 /*
389 * Setup session
390 */
391 clib_spinlock_lock_if_init (&ecm->sessions_lock);
392 pool_get (ecm->sessions, session);
393 clib_spinlock_unlock_if_init (&ecm->sessions_lock);
394
395 memset (session, 0, sizeof (*session));
396 session_index = session - ecm->sessions;
397 session->bytes_to_send = ecm->bytes_to_send;
398 session->bytes_to_receive = ecm->no_return ? 0ULL : ecm->bytes_to_send;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700399 session->data.rx_fifo = s->server_rx_fifo;
400 session->data.rx_fifo->client_session_index = session_index;
401 session->data.tx_fifo = s->server_tx_fifo;
402 session->data.tx_fifo->client_session_index = session_index;
403 session->data.vpp_evt_q = ecm->vpp_event_queue[thread_index];
Florin Coras4399c2e2018-01-25 06:34:42 -0800404 session->vpp_session_handle = session_handle (s);
405
Florin Coras7fb0fe12018-04-09 09:24:52 -0700406 if (ecm->is_dgram)
407 {
408 transport_connection_t *tc;
409 tc = session_get_transport (s);
410 clib_memcpy (&session->data.transport, tc,
411 sizeof (session->data.transport));
412 session->data.is_dgram = 1;
413 }
414
Florin Coras4399c2e2018-01-25 06:34:42 -0800415 vec_add1 (ecm->connection_index_by_thread[thread_index], session_index);
416 __sync_fetch_and_add (&ecm->ready_connections, 1);
417 if (ecm->ready_connections == ecm->expected_connections)
418 {
419 ecm->run_test = 1;
420 /* Signal the CLI process that the action is starting... */
421 signal_evt_to_cli (1);
422 }
423
424 return 0;
425}
426
427static void
428echo_clients_session_reset_callback (stream_session_t * s)
429{
430 if (s->session_state == SESSION_STATE_READY)
431 clib_warning ("Reset active connection %U", format_stream_session, s, 2);
432 stream_session_cleanup (s);
433 return;
434}
435
436static int
437echo_clients_session_create_callback (stream_session_t * s)
438{
439 return 0;
440}
441
442static void
443echo_clients_session_disconnect_callback (stream_session_t * s)
444{
445 echo_client_main_t *ecm = &echo_client_main;
446 vnet_disconnect_args_t _a, *a = &_a;
447 a->handle = session_handle (s);
448 a->app_index = ecm->app_index;
449 vnet_disconnect_session (a);
450 return;
451}
452
453static int
454echo_clients_rx_callback (stream_session_t * s)
455{
Florin Coras7fb0fe12018-04-09 09:24:52 -0700456 echo_client_main_t *ecm = &echo_client_main;
Florin Coras48750892018-05-16 09:28:02 -0700457 eclient_session_t *sp;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700458
459 sp = pool_elt_at_index (ecm->sessions,
460 s->server_rx_fifo->client_session_index);
461 receive_data_chunk (ecm, sp);
462
463 if (svm_fifo_max_dequeue (s->server_rx_fifo))
464 {
465 session_fifo_event_t evt;
466 svm_queue_t *q;
467 if (svm_fifo_set_event (s->server_rx_fifo))
468 {
469 evt.fifo = s->server_rx_fifo;
470 evt.event_type = FIFO_EVENT_BUILTIN_RX;
471 q = session_manager_get_vpp_event_queue (s->thread_index);
472 if (PREDICT_FALSE (q->cursize == q->maxsize))
473 clib_warning ("out of event queue space");
474 else if (svm_queue_add (q, (u8 *) & evt, 0))
475 clib_warning ("failed to enqueue self-tap");
476 }
477 }
Florin Coras4399c2e2018-01-25 06:34:42 -0800478 return 0;
479}
480
Florin Corasa332c462018-01-31 06:52:17 -0800481int
482echo_client_add_segment_callback (u32 client_index, const ssvm_private_t * sp)
483{
484 /* New heaps may be added */
485 return 0;
486}
487
Florin Coras4399c2e2018-01-25 06:34:42 -0800488/* *INDENT-OFF* */
489static session_cb_vft_t echo_clients = {
490 .session_reset_callback = echo_clients_session_reset_callback,
491 .session_connected_callback = echo_clients_session_connected_callback,
492 .session_accept_callback = echo_clients_session_create_callback,
493 .session_disconnect_callback = echo_clients_session_disconnect_callback,
Florin Coras371ca502018-02-21 12:07:41 -0800494 .builtin_app_rx_callback = echo_clients_rx_callback,
Florin Corasa332c462018-01-31 06:52:17 -0800495 .add_segment_callback = echo_client_add_segment_callback
Florin Coras4399c2e2018-01-25 06:34:42 -0800496};
497/* *INDENT-ON* */
498
499static clib_error_t *
500echo_clients_attach (u8 * appns_id, u64 appns_flags, u64 appns_secret)
501{
Florin Coras2f8d8fa2018-01-26 06:36:04 -0800502 u32 prealloc_fifos, segment_size = 256 << 20;
Florin Coras4399c2e2018-01-25 06:34:42 -0800503 echo_client_main_t *ecm = &echo_client_main;
504 vnet_app_attach_args_t _a, *a = &_a;
505 u64 options[16];
506 clib_error_t *error = 0;
507
508 memset (a, 0, sizeof (*a));
509 memset (options, 0, sizeof (options));
510
511 a->api_client_index = ecm->my_client_index;
512 a->session_cb_vft = &echo_clients;
513
514 prealloc_fifos = ecm->prealloc_fifos ? ecm->expected_connections : 1;
515
516 if (ecm->private_segment_size)
517 segment_size = ecm->private_segment_size;
518
519 options[APP_OPTIONS_ACCEPT_COOKIE] = 0x12345678;
520 options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
Florin Corasa332c462018-01-31 06:52:17 -0800521 options[APP_OPTIONS_ADD_SEGMENT_SIZE] = segment_size;
Florin Coras4399c2e2018-01-25 06:34:42 -0800522 options[APP_OPTIONS_RX_FIFO_SIZE] = ecm->fifo_size;
523 options[APP_OPTIONS_TX_FIFO_SIZE] = ecm->fifo_size;
524 options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = ecm->private_segment_count;
525 options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = prealloc_fifos;
Florin Coras4399c2e2018-01-25 06:34:42 -0800526 options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
Florin Coras58d36f02018-03-09 13:05:53 -0800527 options[APP_OPTIONS_TLS_ENGINE] = ecm->tls_engine;
Florin Coras4399c2e2018-01-25 06:34:42 -0800528 if (appns_id)
529 {
530 options[APP_OPTIONS_FLAGS] |= appns_flags;
531 options[APP_OPTIONS_NAMESPACE_SECRET] = appns_secret;
532 }
533 a->options = options;
534 a->namespace_id = appns_id;
535
536 if ((error = vnet_application_attach (a)))
537 return error;
538
539 ecm->app_index = a->app_index;
540 return 0;
541}
542
543static int
544echo_clients_detach ()
545{
546 echo_client_main_t *ecm = &echo_client_main;
547 vnet_app_detach_args_t _da, *da = &_da;
548 int rv;
549
550 da->app_index = ecm->app_index;
551 rv = vnet_application_detach (da);
552 ecm->test_client_attached = 0;
553 ecm->app_index = ~0;
554 return rv;
555}
556
557static void *
558echo_client_thread_fn (void *arg)
559{
560 return 0;
561}
562
563/** Start a transmit thread */
564int
565echo_clients_start_tx_pthread (echo_client_main_t * ecm)
566{
567 if (ecm->client_thread_handle == 0)
568 {
569 int rv = pthread_create (&ecm->client_thread_handle,
570 NULL /*attr */ ,
571 echo_client_thread_fn, 0);
572 if (rv)
573 {
574 ecm->client_thread_handle = 0;
575 return -1;
576 }
577 }
578 return 0;
579}
580
581clib_error_t *
582echo_clients_connect (vlib_main_t * vm, u32 n_clients)
583{
584 echo_client_main_t *ecm = &echo_client_main;
585 vnet_connect_args_t _a, *a = &_a;
586 clib_error_t *error = 0;
587 int i;
Florin Coras8f89dd02018-03-05 16:53:07 -0800588
589 memset (a, 0, sizeof (*a));
Florin Coras4399c2e2018-01-25 06:34:42 -0800590 for (i = 0; i < n_clients; i++)
591 {
Florin Coras4399c2e2018-01-25 06:34:42 -0800592 a->uri = (char *) ecm->connect_uri;
593 a->api_context = i;
594 a->app_index = ecm->app_index;
Florin Coras4399c2e2018-01-25 06:34:42 -0800595
596 if ((error = vnet_connect_uri (a)))
597 return error;
598
599 /* Crude pacing for call setups */
600 if ((i % 4) == 0)
601 vlib_process_suspend (vm, 10e-6);
602 ASSERT (i + 1 >= ecm->ready_connections);
603 while (i + 1 - ecm->ready_connections > 1000)
604 {
605 vlib_process_suspend (vm, 100e-6);
606 }
607 }
608 return 0;
609}
610
611#define ec_cli_output(_fmt, _args...) \
Florin Coras7fb0fe12018-04-09 09:24:52 -0700612 if (!ecm->no_output) \
Florin Coras4399c2e2018-01-25 06:34:42 -0800613 vlib_cli_output(vm, _fmt, ##_args)
614
615static clib_error_t *
616echo_clients_command_fn (vlib_main_t * vm,
617 unformat_input_t * input, vlib_cli_command_t * cmd)
618{
619 echo_client_main_t *ecm = &echo_client_main;
620 vlib_thread_main_t *thread_main = vlib_get_thread_main ();
Florin Coras4399c2e2018-01-25 06:34:42 -0800621 u64 tmp, total_bytes, appns_flags = 0, appns_secret = 0;
622 f64 test_timeout = 20.0, syn_timeout = 20.0, delta;
Florin Coras2f8d8fa2018-01-26 06:36:04 -0800623 char *default_uri = "tcp://6.0.1.1/1234";
624 uword *event_data = 0, event_type;
Florin Coras4399c2e2018-01-25 06:34:42 -0800625 f64 time_before_connects;
626 u32 n_clients = 1;
627 int preallocate_sessions = 0;
628 char *transfer_type;
629 clib_error_t *error = 0;
Florin Coras2f8d8fa2018-01-26 06:36:04 -0800630 u8 *appns_id = 0;
Florin Coras4399c2e2018-01-25 06:34:42 -0800631 int i;
632
633 ecm->bytes_to_send = 8192;
634 ecm->no_return = 0;
635 ecm->fifo_size = 64 << 10;
636 ecm->connections_per_batch = 1000;
637 ecm->private_segment_count = 0;
638 ecm->private_segment_size = 0;
639 ecm->no_output = 0;
640 ecm->test_bytes = 0;
641 ecm->test_failed = 0;
642 ecm->vlib_main = vm;
Florin Coras58d36f02018-03-09 13:05:53 -0800643 ecm->tls_engine = TLS_ENGINE_OPENSSL;
Florin Coras8e43d042018-05-04 15:46:57 -0700644 ecm->no_copy = 0;
Florin Coras58d36f02018-03-09 13:05:53 -0800645
Florin Coras4399c2e2018-01-25 06:34:42 -0800646 if (thread_main->n_vlib_mains > 1)
647 clib_spinlock_init (&ecm->sessions_lock);
648 vec_free (ecm->connect_uri);
649
650 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
651 {
652 if (unformat (input, "uri %s", &ecm->connect_uri))
653 ;
654 else if (unformat (input, "nclients %d", &n_clients))
655 ;
656 else if (unformat (input, "mbytes %lld", &tmp))
657 ecm->bytes_to_send = tmp << 20;
658 else if (unformat (input, "gbytes %lld", &tmp))
659 ecm->bytes_to_send = tmp << 30;
660 else if (unformat (input, "bytes %lld", &ecm->bytes_to_send))
661 ;
662 else if (unformat (input, "test-timeout %f", &test_timeout))
663 ;
664 else if (unformat (input, "syn-timeout %f", &syn_timeout))
665 ;
666 else if (unformat (input, "no-return"))
667 ecm->no_return = 1;
668 else if (unformat (input, "fifo-size %d", &ecm->fifo_size))
669 ecm->fifo_size <<= 10;
670 else if (unformat (input, "private-segment-count %d",
671 &ecm->private_segment_count))
672 ;
673 else if (unformat (input, "private-segment-size %U",
674 unformat_memory_size, &tmp))
675 {
676 if (tmp >= 0x100000000ULL)
677 return clib_error_return
678 (0, "private segment size %lld (%llu) too large", tmp, tmp);
679 ecm->private_segment_size = tmp;
680 }
681 else if (unformat (input, "preallocate-fifos"))
682 ecm->prealloc_fifos = 1;
683 else if (unformat (input, "preallocate-sessions"))
684 preallocate_sessions = 1;
685 else
686 if (unformat (input, "client-batch %d", &ecm->connections_per_batch))
687 ;
688 else if (unformat (input, "appns %_%v%_", &appns_id))
689 ;
690 else if (unformat (input, "all-scope"))
691 appns_flags |= (APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE
692 | APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE);
693 else if (unformat (input, "local-scope"))
694 appns_flags = APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
695 else if (unformat (input, "global-scope"))
696 appns_flags = APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
697 else if (unformat (input, "secret %lu", &appns_secret))
698 ;
699 else if (unformat (input, "no-output"))
700 ecm->no_output = 1;
701 else if (unformat (input, "test-bytes"))
702 ecm->test_bytes = 1;
Florin Coras58d36f02018-03-09 13:05:53 -0800703 else if (unformat (input, "tls-engine %d", &ecm->tls_engine))
704 ;
Florin Coras4399c2e2018-01-25 06:34:42 -0800705 else
Florin Corasa332c462018-01-31 06:52:17 -0800706 return clib_error_return (0, "failed: unknown input `%U'",
Florin Coras4399c2e2018-01-25 06:34:42 -0800707 format_unformat_error, input);
708 }
709
710 /* Store cli process node index for signalling */
711 ecm->cli_node_index =
712 vlib_get_current_process (vm)->node_runtime.node_index;
713
714 if (ecm->is_init == 0)
715 {
716 if (echo_clients_init (vm))
717 return clib_error_return (0, "failed init");
718 }
719
720
721 ecm->ready_connections = 0;
722 ecm->expected_connections = n_clients;
723 ecm->rx_total = 0;
724 ecm->tx_total = 0;
725
726 if (!ecm->connect_uri)
727 {
Florin Coras2f8d8fa2018-01-26 06:36:04 -0800728 clib_warning ("No uri provided. Using default: %s", default_uri);
729 ecm->connect_uri = format (0, "%s%c", default_uri, 0);
Florin Coras4399c2e2018-01-25 06:34:42 -0800730 }
731
Florin Coras7fb0fe12018-04-09 09:24:52 -0700732 if (ecm->connect_uri[0] == 'u' && ecm->connect_uri[3] != 'c')
733 ecm->is_dgram = 1;
734
Florin Coras4399c2e2018-01-25 06:34:42 -0800735#if ECHO_CLIENT_PTHREAD
736 echo_clients_start_tx_pthread ();
737#endif
738
739 vlib_worker_thread_barrier_sync (vm);
740 vnet_session_enable_disable (vm, 1 /* turn on session and transports */ );
741 vlib_worker_thread_barrier_release (vm);
742
743 if (ecm->test_client_attached == 0)
744 {
745 if ((error = echo_clients_attach (appns_id, appns_flags, appns_secret)))
746 {
747 vec_free (appns_id);
748 clib_error_report (error);
749 return error;
750 }
751 vec_free (appns_id);
752 }
753 ecm->test_client_attached = 1;
754
755 /* Turn on the builtin client input nodes */
756 for (i = 0; i < thread_main->n_vlib_mains; i++)
757 vlib_node_set_state (vlib_mains[i], echo_clients_node.index,
758 VLIB_NODE_STATE_POLLING);
759
760 if (preallocate_sessions)
Florin Coras48750892018-05-16 09:28:02 -0700761 pool_init_fixed (ecm->sessions, 1.1 * n_clients);
Florin Coras4399c2e2018-01-25 06:34:42 -0800762
763 /* Fire off connect requests */
764 time_before_connects = vlib_time_now (vm);
765 if ((error = echo_clients_connect (vm, n_clients)))
766 return error;
767
768 /* Park until the sessions come up, or ten seconds elapse... */
769 vlib_process_wait_for_event_or_clock (vm, syn_timeout);
770 event_type = vlib_process_get_events (vm, &event_data);
771 switch (event_type)
772 {
773 case ~0:
774 ec_cli_output ("Timeout with only %d sessions active...",
775 ecm->ready_connections);
776 error = clib_error_return (0, "failed: syn timeout with %d sessions",
777 ecm->ready_connections);
778 goto cleanup;
779
780 case 1:
781 delta = vlib_time_now (vm) - time_before_connects;
782 if (delta != 0.0)
783 ec_cli_output ("%d three-way handshakes in %.2f seconds %.2f/s",
784 n_clients, delta, ((f64) n_clients) / delta);
785
786 ecm->test_start_time = vlib_time_now (ecm->vlib_main);
787 ec_cli_output ("Test started at %.6f", ecm->test_start_time);
788 break;
789
790 default:
791 ec_cli_output ("unexpected event(1): %d", event_type);
792 error = clib_error_return (0, "failed: unexpected event(1): %d",
793 event_type);
794 goto cleanup;
795 }
796
797 /* Now wait for the sessions to finish... */
798 vlib_process_wait_for_event_or_clock (vm, test_timeout);
799 event_type = vlib_process_get_events (vm, &event_data);
800 switch (event_type)
801 {
802 case ~0:
803 ec_cli_output ("Timeout with %d sessions still active...",
804 ecm->ready_connections);
805 error = clib_error_return (0, "failed: timeout with %d sessions",
806 ecm->ready_connections);
807 goto cleanup;
808
809 case 2:
810 ecm->test_end_time = vlib_time_now (vm);
811 ec_cli_output ("Test finished at %.6f", ecm->test_end_time);
812 break;
813
814 default:
815 ec_cli_output ("unexpected event(2): %d", event_type);
816 error = clib_error_return (0, "failed: unexpected event(2): %d",
817 event_type);
818 goto cleanup;
819 }
820
821 delta = ecm->test_end_time - ecm->test_start_time;
822 if (delta != 0.0)
823 {
824 total_bytes = (ecm->no_return ? ecm->tx_total : ecm->rx_total);
825 transfer_type = ecm->no_return ? "half-duplex" : "full-duplex";
826 ec_cli_output ("%lld bytes (%lld mbytes, %lld gbytes) in %.2f seconds",
827 total_bytes, total_bytes / (1ULL << 20),
828 total_bytes / (1ULL << 30), delta);
829 ec_cli_output ("%.2f bytes/second %s", ((f64) total_bytes) / (delta),
830 transfer_type);
831 ec_cli_output ("%.4f gbit/second %s",
832 (((f64) total_bytes * 8.0) / delta / 1e9),
833 transfer_type);
834 }
835 else
836 {
837 ec_cli_output ("zero delta-t?");
838 error = clib_error_return (0, "failed: zero delta-t");
839 goto cleanup;
840 }
841
842 if (ecm->test_bytes && ecm->test_failed)
843 error = clib_error_return (0, "failed: test bytes");
844
845cleanup:
846 ecm->run_test = 0;
847 for (i = 0; i < vec_len (ecm->connection_index_by_thread); i++)
848 {
849 vec_reset_length (ecm->connection_index_by_thread[i]);
850 vec_reset_length (ecm->connections_this_batch_by_thread[i]);
851 }
852
853 pool_free (ecm->sessions);
854
855 /* Detach the application, so we can use different fifo sizes next time */
856 if (ecm->test_client_attached)
857 {
858 if (echo_clients_detach ())
859 {
860 error = clib_error_return (0, "failed: app detach");
861 ec_cli_output ("WARNING: app detach failed...");
862 }
863 }
864 if (error)
865 ec_cli_output ("test failed");
Florin Coras2f8d8fa2018-01-26 06:36:04 -0800866 vec_free (ecm->connect_uri);
Florin Coras4399c2e2018-01-25 06:34:42 -0800867 return error;
868}
869
870/* *INDENT-OFF* */
871VLIB_CLI_COMMAND (echo_clients_command, static) =
872{
873 .path = "test echo clients",
874 .short_help = "test echo clients [nclients %d][[m|g]bytes <bytes>]"
875 "[test-timeout <time>][syn-timeout <time>][no-return][fifo-size <size>]"
876 "[private-segment-count <count>][private-segment-size <bytes>[m|g]]"
877 "[preallocate-fifos][preallocate-sessions][client-batch <batch-size>]"
878 "[uri <tcp://ip/port>][test-bytes][no-output]",
879 .function = echo_clients_command_fn,
880 .is_mp_safe = 1,
881};
882/* *INDENT-ON* */
883
884clib_error_t *
885echo_clients_main_init (vlib_main_t * vm)
886{
887 echo_client_main_t *ecm = &echo_client_main;
888 ecm->is_init = 0;
889 return 0;
890}
891
892VLIB_INIT_FUNCTION (echo_clients_main_init);
893
894/*
895 * fd.io coding-style-patch-verification: ON
896 *
897 * Local Variables:
898 * eval: (c-set-style "gnu")
899 * End:
900 */