blob: 1fd7ad0cd9fefa7d0387db1d777d53763de4474b [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);
Florin Coras3c2fed52018-07-04 04:15:05 -070065 session_send_io_evt_to_thread_custom (f, s->thread_index,
66 FIFO_EVENT_APP_TX);
Florin Coras8e43d042018-05-04 15:46:57 -070067 }
68 else
69 rv = app_send_stream (&s->data, test_data + test_buf_offset,
70 bytes_this_chunk, 0);
71 }
Florin Coras7fb0fe12018-04-09 09:24:52 -070072 else
Florin Coras8e43d042018-05-04 15:46:57 -070073 {
74 if (ecm->no_copy)
75 {
76 session_dgram_hdr_t hdr;
77 svm_fifo_t *f = s->data.tx_fifo;
78 app_session_transport_t *at = &s->data.transport;
79 u32 max_enqueue = svm_fifo_max_enqueue (f);
80
81 if (max_enqueue <= sizeof (session_dgram_hdr_t))
82 return;
83
84 max_enqueue -= sizeof (session_dgram_hdr_t);
85 rv = clib_min (max_enqueue, bytes_this_chunk);
86
87 hdr.data_length = rv;
88 hdr.data_offset = 0;
89 clib_memcpy (&hdr.rmt_ip, &at->rmt_ip, sizeof (ip46_address_t));
90 hdr.is_ip4 = at->is_ip4;
91 hdr.rmt_port = at->rmt_port;
92 clib_memcpy (&hdr.lcl_ip, &at->lcl_ip, sizeof (ip46_address_t));
93 hdr.lcl_port = at->lcl_port;
94 svm_fifo_enqueue_nowait (f, sizeof (hdr), (u8 *) & hdr);
95 svm_fifo_enqueue_nocopy (f, rv);
Florin Coras3c2fed52018-07-04 04:15:05 -070096 session_send_io_evt_to_thread_custom (f, s->thread_index,
97 FIFO_EVENT_APP_TX);
Florin Coras8e43d042018-05-04 15:46:57 -070098 }
99 else
100 rv = app_send_dgram (&s->data, test_data + test_buf_offset,
101 bytes_this_chunk, 0);
102 }
Florin Coras4399c2e2018-01-25 06:34:42 -0800103
104 /* If we managed to enqueue data... */
105 if (rv > 0)
106 {
107 /* Account for it... */
108 s->bytes_to_send -= rv;
109 s->bytes_sent += rv;
110
111 if (ECHO_CLIENT_DBG)
112 {
113 /* *INDENT-OFF* */
114 ELOG_TYPE_DECLARE (e) =
115 {
116 .format = "tx-enq: xfer %d bytes, sent %u remain %u",
117 .format_args = "i4i4i4",
118 };
119 /* *INDENT-ON* */
120 struct
121 {
122 u32 data[3];
123 } *ed;
124 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
125 ed->data[0] = rv;
126 ed->data[1] = s->bytes_sent;
127 ed->data[2] = s->bytes_to_send;
128 }
Florin Coras4399c2e2018-01-25 06:34:42 -0800129 }
130}
131
132static void
Florin Coras48750892018-05-16 09:28:02 -0700133receive_data_chunk (echo_client_main_t * ecm, eclient_session_t * s)
Florin Coras4399c2e2018-01-25 06:34:42 -0800134{
Florin Coras7fb0fe12018-04-09 09:24:52 -0700135 svm_fifo_t *rx_fifo = s->data.rx_fifo;
136 u32 thread_index = vlib_get_thread_index ();
Florin Coras4399c2e2018-01-25 06:34:42 -0800137 int n_read, i;
138
139 if (ecm->test_bytes)
140 {
Florin Coras7fb0fe12018-04-09 09:24:52 -0700141 if (!ecm->is_dgram)
142 n_read = app_recv_stream (&s->data, ecm->rx_buf[thread_index],
143 vec_len (ecm->rx_buf[thread_index]));
144 else
145 n_read = app_recv_dgram (&s->data, ecm->rx_buf[thread_index],
146 vec_len (ecm->rx_buf[thread_index]));
Florin Coras4399c2e2018-01-25 06:34:42 -0800147 }
148 else
149 {
150 n_read = svm_fifo_max_dequeue (rx_fifo);
151 svm_fifo_dequeue_drop (rx_fifo, n_read);
152 }
153
154 if (n_read > 0)
155 {
156 if (ECHO_CLIENT_DBG)
157 {
158 /* *INDENT-OFF* */
159 ELOG_TYPE_DECLARE (e) =
160 {
161 .format = "rx-deq: %d bytes",
162 .format_args = "i4",
163 };
164 /* *INDENT-ON* */
165 struct
166 {
167 u32 data[1];
168 } *ed;
169 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
170 ed->data[0] = n_read;
171 }
172
173 if (ecm->test_bytes)
174 {
175 for (i = 0; i < n_read; i++)
176 {
Florin Coras7fb0fe12018-04-09 09:24:52 -0700177 if (ecm->rx_buf[thread_index][i]
Florin Coras4399c2e2018-01-25 06:34:42 -0800178 != ((s->bytes_received + i) & 0xff))
179 {
180 clib_warning ("read %d error at byte %lld, 0x%x not 0x%x",
181 n_read, s->bytes_received + i,
Florin Coras7fb0fe12018-04-09 09:24:52 -0700182 ecm->rx_buf[thread_index][i],
Florin Coras4399c2e2018-01-25 06:34:42 -0800183 ((s->bytes_received + i) & 0xff));
184 ecm->test_failed = 1;
185 }
186 }
187 }
Florin Coras7fb0fe12018-04-09 09:24:52 -0700188 ASSERT (n_read <= s->bytes_to_receive);
Florin Coras4399c2e2018-01-25 06:34:42 -0800189 s->bytes_to_receive -= n_read;
190 s->bytes_received += n_read;
191 }
192}
193
194static uword
195echo_client_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
196 vlib_frame_t * frame)
197{
198 echo_client_main_t *ecm = &echo_client_main;
199 int my_thread_index = vlib_get_thread_index ();
Florin Coras48750892018-05-16 09:28:02 -0700200 eclient_session_t *sp;
Florin Coras4399c2e2018-01-25 06:34:42 -0800201 int i;
202 int delete_session;
203 u32 *connection_indices;
204 u32 *connections_this_batch;
205 u32 nconnections_this_batch;
206
207 connection_indices = ecm->connection_index_by_thread[my_thread_index];
208 connections_this_batch =
209 ecm->connections_this_batch_by_thread[my_thread_index];
210
Florin Coraseb97e5f2018-10-15 21:35:42 -0700211 if ((ecm->run_test != ECHO_CLIENTS_RUNNING) ||
Florin Coras4399c2e2018-01-25 06:34:42 -0800212 ((vec_len (connection_indices) == 0)
213 && vec_len (connections_this_batch) == 0))
214 return 0;
215
216 /* Grab another pile of connections */
217 if (PREDICT_FALSE (vec_len (connections_this_batch) == 0))
218 {
219 nconnections_this_batch =
220 clib_min (ecm->connections_per_batch, vec_len (connection_indices));
221
222 ASSERT (nconnections_this_batch > 0);
223 vec_validate (connections_this_batch, nconnections_this_batch - 1);
224 clib_memcpy (connections_this_batch,
225 connection_indices + vec_len (connection_indices)
226 - nconnections_this_batch,
227 nconnections_this_batch * sizeof (u32));
228 _vec_len (connection_indices) -= nconnections_this_batch;
229 }
230
231 if (PREDICT_FALSE (ecm->prev_conns != ecm->connections_per_batch
232 && ecm->prev_conns == vec_len (connections_this_batch)))
233 {
234 ecm->repeats++;
235 ecm->prev_conns = vec_len (connections_this_batch);
236 if (ecm->repeats == 500000)
237 {
238 clib_warning ("stuck clients");
239 }
240 }
241 else
242 {
243 ecm->prev_conns = vec_len (connections_this_batch);
244 ecm->repeats = 0;
245 }
246
247 for (i = 0; i < vec_len (connections_this_batch); i++)
248 {
249 delete_session = 1;
250
251 sp = pool_elt_at_index (ecm->sessions, connections_this_batch[i]);
252
253 if (sp->bytes_to_send > 0)
254 {
255 send_data_chunk (ecm, sp);
256 delete_session = 0;
257 }
258 if (sp->bytes_to_receive > 0)
259 {
Florin Coras4399c2e2018-01-25 06:34:42 -0800260 delete_session = 0;
261 }
262 if (PREDICT_FALSE (delete_session == 1))
263 {
Florin Coras4399c2e2018-01-25 06:34:42 -0800264 stream_session_t *s;
265
266 __sync_fetch_and_add (&ecm->tx_total, sp->bytes_sent);
267 __sync_fetch_and_add (&ecm->rx_total, sp->bytes_received);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700268 s = session_get_from_handle_if_valid (sp->vpp_session_handle);
Florin Coras4399c2e2018-01-25 06:34:42 -0800269
270 if (s)
271 {
272 vnet_disconnect_args_t _a, *a = &_a;
273 a->handle = session_handle (s);
274 a->app_index = ecm->app_index;
275 vnet_disconnect_session (a);
276
277 vec_delete (connections_this_batch, 1, i);
278 i--;
279 __sync_fetch_and_add (&ecm->ready_connections, -1);
280 }
281 else
Florin Coras2f8d8fa2018-01-26 06:36:04 -0800282 {
283 clib_warning ("session AWOL?");
284 vec_delete (connections_this_batch, 1, i);
285 }
Florin Coras4399c2e2018-01-25 06:34:42 -0800286
287 /* Kick the debug CLI process */
288 if (ecm->ready_connections == 0)
289 {
290 signal_evt_to_cli (2);
291 }
292 }
293 }
294
295 ecm->connection_index_by_thread[my_thread_index] = connection_indices;
296 ecm->connections_this_batch_by_thread[my_thread_index] =
297 connections_this_batch;
298 return 0;
299}
300
301/* *INDENT-OFF* */
302VLIB_REGISTER_NODE (echo_clients_node) =
303{
304 .function = echo_client_node_fn,
305 .name = "echo-clients",
306 .type = VLIB_NODE_TYPE_INPUT,
307 .state = VLIB_NODE_STATE_DISABLED,
308};
309/* *INDENT-ON* */
310
311static int
312create_api_loopback (echo_client_main_t * ecm)
313{
314 api_main_t *am = &api_main;
315 vl_shmem_hdr_t *shmem_hdr;
316
317 shmem_hdr = am->shmem_hdr;
318 ecm->vl_input_queue = shmem_hdr->vl_input_queue;
319 ecm->my_client_index = vl_api_memclnt_create_internal ("echo_client",
320 ecm->vl_input_queue);
321 return 0;
322}
323
324static int
325echo_clients_init (vlib_main_t * vm)
326{
327 echo_client_main_t *ecm = &echo_client_main;
328 vlib_thread_main_t *vtm = vlib_get_thread_main ();
329 u32 num_threads;
330 int i;
331
332 if (create_api_loopback (ecm))
333 return -1;
334
335 num_threads = 1 /* main thread */ + vtm->n_threads;
336
Florin Coras7fb0fe12018-04-09 09:24:52 -0700337 /* Init test data. Big buffer */
338 vec_validate (ecm->connect_test_data, 4 * 1024 * 1024 - 1);
Florin Coras4399c2e2018-01-25 06:34:42 -0800339 for (i = 0; i < vec_len (ecm->connect_test_data); i++)
340 ecm->connect_test_data[i] = i & 0xff;
341
342 vec_validate (ecm->rx_buf, num_threads - 1);
343 for (i = 0; i < num_threads; i++)
344 vec_validate (ecm->rx_buf[i], vec_len (ecm->connect_test_data) - 1);
345
346 ecm->is_init = 1;
347
348 vec_validate (ecm->connection_index_by_thread, vtm->n_vlib_mains);
349 vec_validate (ecm->connections_this_batch_by_thread, vtm->n_vlib_mains);
350 vec_validate (ecm->vpp_event_queue, vtm->n_vlib_mains);
351
352 return 0;
353}
354
Florin Coraseb97e5f2018-10-15 21:35:42 -0700355static void
356echo_clients_session_disconnect (stream_session_t * s)
357{
358 echo_client_main_t *ecm = &echo_client_main;
359 vnet_disconnect_args_t _a, *a = &_a;
360 a->handle = session_handle (s);
361 a->app_index = ecm->app_index;
362 vnet_disconnect_session (a);
363}
364
Florin Coras4399c2e2018-01-25 06:34:42 -0800365static 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 Coras52207f12018-07-12 14:48:06 -0700372 u8 thread_index;
Florin Coras4399c2e2018-01-25 06:34:42 -0800373
Florin Coraseb97e5f2018-10-15 21:35:42 -0700374 if (PREDICT_FALSE (ecm->run_test != ECHO_CLIENTS_STARTING))
375 return -1;
376
Florin Coras4399c2e2018-01-25 06:34:42 -0800377 if (is_fail)
378 {
379 clib_warning ("connection %d failed!", api_context);
380 signal_evt_to_cli (-1);
381 return 0;
382 }
383
Florin Coras52207f12018-07-12 14:48:06 -0700384 thread_index = s->thread_index;
Florin Coras40903ac2018-06-10 14:41:23 -0700385 ASSERT (thread_index == vlib_get_thread_index ()
386 || session_transport_service_type (s) == TRANSPORT_SERVICE_CL);
Florin Coras4399c2e2018-01-25 06:34:42 -0800387
388 if (!ecm->vpp_event_queue[thread_index])
389 ecm->vpp_event_queue[thread_index] =
390 session_manager_get_vpp_event_queue (thread_index);
391
392 /*
393 * Setup session
394 */
395 clib_spinlock_lock_if_init (&ecm->sessions_lock);
396 pool_get (ecm->sessions, session);
397 clib_spinlock_unlock_if_init (&ecm->sessions_lock);
398
399 memset (session, 0, sizeof (*session));
400 session_index = session - ecm->sessions;
401 session->bytes_to_send = ecm->bytes_to_send;
402 session->bytes_to_receive = ecm->no_return ? 0ULL : ecm->bytes_to_send;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700403 session->data.rx_fifo = s->server_rx_fifo;
404 session->data.rx_fifo->client_session_index = session_index;
405 session->data.tx_fifo = s->server_tx_fifo;
406 session->data.tx_fifo->client_session_index = session_index;
407 session->data.vpp_evt_q = ecm->vpp_event_queue[thread_index];
Florin Coras4399c2e2018-01-25 06:34:42 -0800408 session->vpp_session_handle = session_handle (s);
409
Florin Coras7fb0fe12018-04-09 09:24:52 -0700410 if (ecm->is_dgram)
411 {
412 transport_connection_t *tc;
413 tc = session_get_transport (s);
414 clib_memcpy (&session->data.transport, tc,
415 sizeof (session->data.transport));
416 session->data.is_dgram = 1;
417 }
418
Florin Coras4399c2e2018-01-25 06:34:42 -0800419 vec_add1 (ecm->connection_index_by_thread[thread_index], session_index);
420 __sync_fetch_and_add (&ecm->ready_connections, 1);
421 if (ecm->ready_connections == ecm->expected_connections)
422 {
Florin Coraseb97e5f2018-10-15 21:35:42 -0700423 ecm->run_test = ECHO_CLIENTS_RUNNING;
Florin Coras4399c2e2018-01-25 06:34:42 -0800424 /* Signal the CLI process that the action is starting... */
425 signal_evt_to_cli (1);
426 }
427
428 return 0;
429}
430
431static void
432echo_clients_session_reset_callback (stream_session_t * s)
433{
434 if (s->session_state == SESSION_STATE_READY)
435 clib_warning ("Reset active connection %U", format_stream_session, s, 2);
436 stream_session_cleanup (s);
437 return;
438}
439
440static int
441echo_clients_session_create_callback (stream_session_t * s)
442{
443 return 0;
444}
445
446static void
447echo_clients_session_disconnect_callback (stream_session_t * s)
448{
449 echo_client_main_t *ecm = &echo_client_main;
450 vnet_disconnect_args_t _a, *a = &_a;
451 a->handle = session_handle (s);
452 a->app_index = ecm->app_index;
453 vnet_disconnect_session (a);
454 return;
455}
456
457static int
458echo_clients_rx_callback (stream_session_t * s)
459{
Florin Coras7fb0fe12018-04-09 09:24:52 -0700460 echo_client_main_t *ecm = &echo_client_main;
Florin Coras48750892018-05-16 09:28:02 -0700461 eclient_session_t *sp;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700462
Florin Coraseb97e5f2018-10-15 21:35:42 -0700463 if (PREDICT_FALSE (ecm->run_test != ECHO_CLIENTS_RUNNING))
464 {
465 echo_clients_session_disconnect (s);
466 return -1;
467 }
468
Florin Coras7fb0fe12018-04-09 09:24:52 -0700469 sp = pool_elt_at_index (ecm->sessions,
470 s->server_rx_fifo->client_session_index);
471 receive_data_chunk (ecm, sp);
472
473 if (svm_fifo_max_dequeue (s->server_rx_fifo))
474 {
Florin Coras7fb0fe12018-04-09 09:24:52 -0700475 if (svm_fifo_set_event (s->server_rx_fifo))
Florin Coras3c2fed52018-07-04 04:15:05 -0700476 session_send_io_evt_to_thread (s->server_rx_fifo,
477 FIFO_EVENT_BUILTIN_RX);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700478 }
Florin Coras4399c2e2018-01-25 06:34:42 -0800479 return 0;
480}
481
Florin Corasa332c462018-01-31 06:52:17 -0800482int
483echo_client_add_segment_callback (u32 client_index, const ssvm_private_t * sp)
484{
485 /* New heaps may be added */
486 return 0;
487}
488
Florin Coras4399c2e2018-01-25 06:34:42 -0800489/* *INDENT-OFF* */
490static session_cb_vft_t echo_clients = {
491 .session_reset_callback = echo_clients_session_reset_callback,
492 .session_connected_callback = echo_clients_session_connected_callback,
493 .session_accept_callback = echo_clients_session_create_callback,
494 .session_disconnect_callback = echo_clients_session_disconnect_callback,
Florin Coras371ca502018-02-21 12:07:41 -0800495 .builtin_app_rx_callback = echo_clients_rx_callback,
Florin Corasa332c462018-01-31 06:52:17 -0800496 .add_segment_callback = echo_client_add_segment_callback
Florin Coras4399c2e2018-01-25 06:34:42 -0800497};
498/* *INDENT-ON* */
499
500static clib_error_t *
501echo_clients_attach (u8 * appns_id, u64 appns_flags, u64 appns_secret)
502{
Florin Coras2f8d8fa2018-01-26 06:36:04 -0800503 u32 prealloc_fifos, segment_size = 256 << 20;
Florin Coras4399c2e2018-01-25 06:34:42 -0800504 echo_client_main_t *ecm = &echo_client_main;
505 vnet_app_attach_args_t _a, *a = &_a;
506 u64 options[16];
507 clib_error_t *error = 0;
508
509 memset (a, 0, sizeof (*a));
510 memset (options, 0, sizeof (options));
511
512 a->api_client_index = ecm->my_client_index;
513 a->session_cb_vft = &echo_clients;
514
515 prealloc_fifos = ecm->prealloc_fifos ? ecm->expected_connections : 1;
516
517 if (ecm->private_segment_size)
518 segment_size = ecm->private_segment_size;
519
520 options[APP_OPTIONS_ACCEPT_COOKIE] = 0x12345678;
521 options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
Florin Corasa332c462018-01-31 06:52:17 -0800522 options[APP_OPTIONS_ADD_SEGMENT_SIZE] = segment_size;
Florin Coras4399c2e2018-01-25 06:34:42 -0800523 options[APP_OPTIONS_RX_FIFO_SIZE] = ecm->fifo_size;
524 options[APP_OPTIONS_TX_FIFO_SIZE] = ecm->fifo_size;
525 options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = ecm->private_segment_count;
526 options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = prealloc_fifos;
Florin Coras4399c2e2018-01-25 06:34:42 -0800527 options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
Florin Coras58d36f02018-03-09 13:05:53 -0800528 options[APP_OPTIONS_TLS_ENGINE] = ecm->tls_engine;
Florin Coras4399c2e2018-01-25 06:34:42 -0800529 if (appns_id)
530 {
531 options[APP_OPTIONS_FLAGS] |= appns_flags;
532 options[APP_OPTIONS_NAMESPACE_SECRET] = appns_secret;
533 }
534 a->options = options;
535 a->namespace_id = appns_id;
536
537 if ((error = vnet_application_attach (a)))
538 return error;
539
540 ecm->app_index = a->app_index;
541 return 0;
542}
543
544static int
545echo_clients_detach ()
546{
547 echo_client_main_t *ecm = &echo_client_main;
548 vnet_app_detach_args_t _da, *da = &_da;
549 int rv;
550
551 da->app_index = ecm->app_index;
552 rv = vnet_application_detach (da);
553 ecm->test_client_attached = 0;
554 ecm->app_index = ~0;
555 return rv;
556}
557
558static void *
559echo_client_thread_fn (void *arg)
560{
561 return 0;
562}
563
564/** Start a transmit thread */
565int
566echo_clients_start_tx_pthread (echo_client_main_t * ecm)
567{
568 if (ecm->client_thread_handle == 0)
569 {
570 int rv = pthread_create (&ecm->client_thread_handle,
571 NULL /*attr */ ,
572 echo_client_thread_fn, 0);
573 if (rv)
574 {
575 ecm->client_thread_handle = 0;
576 return -1;
577 }
578 }
579 return 0;
580}
581
582clib_error_t *
583echo_clients_connect (vlib_main_t * vm, u32 n_clients)
584{
585 echo_client_main_t *ecm = &echo_client_main;
586 vnet_connect_args_t _a, *a = &_a;
587 clib_error_t *error = 0;
588 int i;
Florin Coras8f89dd02018-03-05 16:53:07 -0800589
590 memset (a, 0, sizeof (*a));
Florin Coras4399c2e2018-01-25 06:34:42 -0800591 for (i = 0; i < n_clients; i++)
592 {
Florin Coras4399c2e2018-01-25 06:34:42 -0800593 a->uri = (char *) ecm->connect_uri;
594 a->api_context = i;
595 a->app_index = ecm->app_index;
Florin Coras4399c2e2018-01-25 06:34:42 -0800596
597 if ((error = vnet_connect_uri (a)))
598 return error;
599
600 /* Crude pacing for call setups */
601 if ((i % 4) == 0)
602 vlib_process_suspend (vm, 10e-6);
603 ASSERT (i + 1 >= ecm->ready_connections);
604 while (i + 1 - ecm->ready_connections > 1000)
605 {
606 vlib_process_suspend (vm, 100e-6);
607 }
608 }
609 return 0;
610}
611
612#define ec_cli_output(_fmt, _args...) \
Florin Coras7fb0fe12018-04-09 09:24:52 -0700613 if (!ecm->no_output) \
Florin Coras4399c2e2018-01-25 06:34:42 -0800614 vlib_cli_output(vm, _fmt, ##_args)
615
616static clib_error_t *
617echo_clients_command_fn (vlib_main_t * vm,
618 unformat_input_t * input, vlib_cli_command_t * cmd)
619{
620 echo_client_main_t *ecm = &echo_client_main;
621 vlib_thread_main_t *thread_main = vlib_get_thread_main ();
Florin Coras4399c2e2018-01-25 06:34:42 -0800622 u64 tmp, total_bytes, appns_flags = 0, appns_secret = 0;
623 f64 test_timeout = 20.0, syn_timeout = 20.0, delta;
Florin Coras2f8d8fa2018-01-26 06:36:04 -0800624 char *default_uri = "tcp://6.0.1.1/1234";
625 uword *event_data = 0, event_type;
Florin Coras4399c2e2018-01-25 06:34:42 -0800626 f64 time_before_connects;
627 u32 n_clients = 1;
628 int preallocate_sessions = 0;
629 char *transfer_type;
630 clib_error_t *error = 0;
Florin Coras2f8d8fa2018-01-26 06:36:04 -0800631 u8 *appns_id = 0;
Florin Coras4399c2e2018-01-25 06:34:42 -0800632 int i;
633
634 ecm->bytes_to_send = 8192;
635 ecm->no_return = 0;
636 ecm->fifo_size = 64 << 10;
637 ecm->connections_per_batch = 1000;
638 ecm->private_segment_count = 0;
639 ecm->private_segment_size = 0;
640 ecm->no_output = 0;
641 ecm->test_bytes = 0;
642 ecm->test_failed = 0;
643 ecm->vlib_main = vm;
Florin Coras58d36f02018-03-09 13:05:53 -0800644 ecm->tls_engine = TLS_ENGINE_OPENSSL;
Florin Coras8e43d042018-05-04 15:46:57 -0700645 ecm->no_copy = 0;
Florin Coraseb97e5f2018-10-15 21:35:42 -0700646 ecm->run_test = ECHO_CLIENTS_STARTING;
Florin Coras58d36f02018-03-09 13:05:53 -0800647
Florin Coras4399c2e2018-01-25 06:34:42 -0800648 if (thread_main->n_vlib_mains > 1)
649 clib_spinlock_init (&ecm->sessions_lock);
650 vec_free (ecm->connect_uri);
651
652 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
653 {
654 if (unformat (input, "uri %s", &ecm->connect_uri))
655 ;
656 else if (unformat (input, "nclients %d", &n_clients))
657 ;
658 else if (unformat (input, "mbytes %lld", &tmp))
659 ecm->bytes_to_send = tmp << 20;
660 else if (unformat (input, "gbytes %lld", &tmp))
661 ecm->bytes_to_send = tmp << 30;
662 else if (unformat (input, "bytes %lld", &ecm->bytes_to_send))
663 ;
664 else if (unformat (input, "test-timeout %f", &test_timeout))
665 ;
666 else if (unformat (input, "syn-timeout %f", &syn_timeout))
667 ;
668 else if (unformat (input, "no-return"))
669 ecm->no_return = 1;
670 else if (unformat (input, "fifo-size %d", &ecm->fifo_size))
671 ecm->fifo_size <<= 10;
672 else if (unformat (input, "private-segment-count %d",
673 &ecm->private_segment_count))
674 ;
675 else if (unformat (input, "private-segment-size %U",
676 unformat_memory_size, &tmp))
677 {
678 if (tmp >= 0x100000000ULL)
679 return clib_error_return
680 (0, "private segment size %lld (%llu) too large", tmp, tmp);
681 ecm->private_segment_size = tmp;
682 }
683 else if (unformat (input, "preallocate-fifos"))
684 ecm->prealloc_fifos = 1;
685 else if (unformat (input, "preallocate-sessions"))
686 preallocate_sessions = 1;
687 else
688 if (unformat (input, "client-batch %d", &ecm->connections_per_batch))
689 ;
690 else if (unformat (input, "appns %_%v%_", &appns_id))
691 ;
692 else if (unformat (input, "all-scope"))
693 appns_flags |= (APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE
694 | APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE);
695 else if (unformat (input, "local-scope"))
696 appns_flags = APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
697 else if (unformat (input, "global-scope"))
698 appns_flags = APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
699 else if (unformat (input, "secret %lu", &appns_secret))
700 ;
701 else if (unformat (input, "no-output"))
702 ecm->no_output = 1;
703 else if (unformat (input, "test-bytes"))
704 ecm->test_bytes = 1;
Florin Coras58d36f02018-03-09 13:05:53 -0800705 else if (unformat (input, "tls-engine %d", &ecm->tls_engine))
706 ;
Florin Coras4399c2e2018-01-25 06:34:42 -0800707 else
Florin Corasa332c462018-01-31 06:52:17 -0800708 return clib_error_return (0, "failed: unknown input `%U'",
Florin Coras4399c2e2018-01-25 06:34:42 -0800709 format_unformat_error, input);
710 }
711
712 /* Store cli process node index for signalling */
713 ecm->cli_node_index =
714 vlib_get_current_process (vm)->node_runtime.node_index;
715
716 if (ecm->is_init == 0)
717 {
718 if (echo_clients_init (vm))
719 return clib_error_return (0, "failed init");
720 }
721
722
723 ecm->ready_connections = 0;
724 ecm->expected_connections = n_clients;
725 ecm->rx_total = 0;
726 ecm->tx_total = 0;
727
728 if (!ecm->connect_uri)
729 {
Florin Coras2f8d8fa2018-01-26 06:36:04 -0800730 clib_warning ("No uri provided. Using default: %s", default_uri);
731 ecm->connect_uri = format (0, "%s%c", default_uri, 0);
Florin Coras4399c2e2018-01-25 06:34:42 -0800732 }
733
Florin Coras7fb0fe12018-04-09 09:24:52 -0700734 if (ecm->connect_uri[0] == 'u' && ecm->connect_uri[3] != 'c')
735 ecm->is_dgram = 1;
736
Florin Coras4399c2e2018-01-25 06:34:42 -0800737#if ECHO_CLIENT_PTHREAD
738 echo_clients_start_tx_pthread ();
739#endif
740
741 vlib_worker_thread_barrier_sync (vm);
742 vnet_session_enable_disable (vm, 1 /* turn on session and transports */ );
743 vlib_worker_thread_barrier_release (vm);
744
745 if (ecm->test_client_attached == 0)
746 {
747 if ((error = echo_clients_attach (appns_id, appns_flags, appns_secret)))
748 {
749 vec_free (appns_id);
750 clib_error_report (error);
751 return error;
752 }
753 vec_free (appns_id);
754 }
755 ecm->test_client_attached = 1;
756
757 /* Turn on the builtin client input nodes */
758 for (i = 0; i < thread_main->n_vlib_mains; i++)
759 vlib_node_set_state (vlib_mains[i], echo_clients_node.index,
760 VLIB_NODE_STATE_POLLING);
761
762 if (preallocate_sessions)
Florin Coras48750892018-05-16 09:28:02 -0700763 pool_init_fixed (ecm->sessions, 1.1 * n_clients);
Florin Coras4399c2e2018-01-25 06:34:42 -0800764
765 /* Fire off connect requests */
766 time_before_connects = vlib_time_now (vm);
767 if ((error = echo_clients_connect (vm, n_clients)))
Florin Corasc977e7c2018-10-16 20:30:31 -0700768 goto cleanup;
Florin Coras4399c2e2018-01-25 06:34:42 -0800769
770 /* Park until the sessions come up, or ten seconds elapse... */
771 vlib_process_wait_for_event_or_clock (vm, syn_timeout);
772 event_type = vlib_process_get_events (vm, &event_data);
773 switch (event_type)
774 {
775 case ~0:
776 ec_cli_output ("Timeout with only %d sessions active...",
777 ecm->ready_connections);
778 error = clib_error_return (0, "failed: syn timeout with %d sessions",
779 ecm->ready_connections);
780 goto cleanup;
781
782 case 1:
783 delta = vlib_time_now (vm) - time_before_connects;
784 if (delta != 0.0)
785 ec_cli_output ("%d three-way handshakes in %.2f seconds %.2f/s",
786 n_clients, delta, ((f64) n_clients) / delta);
787
788 ecm->test_start_time = vlib_time_now (ecm->vlib_main);
789 ec_cli_output ("Test started at %.6f", ecm->test_start_time);
790 break;
791
792 default:
793 ec_cli_output ("unexpected event(1): %d", event_type);
794 error = clib_error_return (0, "failed: unexpected event(1): %d",
795 event_type);
796 goto cleanup;
797 }
798
799 /* Now wait for the sessions to finish... */
800 vlib_process_wait_for_event_or_clock (vm, test_timeout);
801 event_type = vlib_process_get_events (vm, &event_data);
802 switch (event_type)
803 {
804 case ~0:
805 ec_cli_output ("Timeout with %d sessions still active...",
806 ecm->ready_connections);
807 error = clib_error_return (0, "failed: timeout with %d sessions",
808 ecm->ready_connections);
809 goto cleanup;
810
811 case 2:
812 ecm->test_end_time = vlib_time_now (vm);
813 ec_cli_output ("Test finished at %.6f", ecm->test_end_time);
814 break;
815
816 default:
817 ec_cli_output ("unexpected event(2): %d", event_type);
818 error = clib_error_return (0, "failed: unexpected event(2): %d",
819 event_type);
820 goto cleanup;
821 }
822
823 delta = ecm->test_end_time - ecm->test_start_time;
824 if (delta != 0.0)
825 {
826 total_bytes = (ecm->no_return ? ecm->tx_total : ecm->rx_total);
827 transfer_type = ecm->no_return ? "half-duplex" : "full-duplex";
828 ec_cli_output ("%lld bytes (%lld mbytes, %lld gbytes) in %.2f seconds",
829 total_bytes, total_bytes / (1ULL << 20),
830 total_bytes / (1ULL << 30), delta);
831 ec_cli_output ("%.2f bytes/second %s", ((f64) total_bytes) / (delta),
832 transfer_type);
833 ec_cli_output ("%.4f gbit/second %s",
834 (((f64) total_bytes * 8.0) / delta / 1e9),
835 transfer_type);
836 }
837 else
838 {
839 ec_cli_output ("zero delta-t?");
840 error = clib_error_return (0, "failed: zero delta-t");
841 goto cleanup;
842 }
843
844 if (ecm->test_bytes && ecm->test_failed)
845 error = clib_error_return (0, "failed: test bytes");
846
847cleanup:
Florin Coraseb97e5f2018-10-15 21:35:42 -0700848 ecm->run_test = ECHO_CLIENTS_EXITING;
Florin Corasef915342018-09-29 10:23:06 -0700849 vlib_process_wait_for_event_or_clock (vm, 10e-3);
Florin Coras4399c2e2018-01-25 06:34:42 -0800850 for (i = 0; i < vec_len (ecm->connection_index_by_thread); i++)
851 {
852 vec_reset_length (ecm->connection_index_by_thread[i]);
853 vec_reset_length (ecm->connections_this_batch_by_thread[i]);
854 }
855
856 pool_free (ecm->sessions);
857
858 /* Detach the application, so we can use different fifo sizes next time */
859 if (ecm->test_client_attached)
860 {
861 if (echo_clients_detach ())
862 {
863 error = clib_error_return (0, "failed: app detach");
864 ec_cli_output ("WARNING: app detach failed...");
865 }
866 }
867 if (error)
868 ec_cli_output ("test failed");
Florin Coras2f8d8fa2018-01-26 06:36:04 -0800869 vec_free (ecm->connect_uri);
Florin Coras4399c2e2018-01-25 06:34:42 -0800870 return error;
871}
872
873/* *INDENT-OFF* */
874VLIB_CLI_COMMAND (echo_clients_command, static) =
875{
876 .path = "test echo clients",
877 .short_help = "test echo clients [nclients %d][[m|g]bytes <bytes>]"
878 "[test-timeout <time>][syn-timeout <time>][no-return][fifo-size <size>]"
879 "[private-segment-count <count>][private-segment-size <bytes>[m|g]]"
880 "[preallocate-fifos][preallocate-sessions][client-batch <batch-size>]"
881 "[uri <tcp://ip/port>][test-bytes][no-output]",
882 .function = echo_clients_command_fn,
883 .is_mp_safe = 1,
884};
885/* *INDENT-ON* */
886
887clib_error_t *
888echo_clients_main_init (vlib_main_t * vm)
889{
890 echo_client_main_t *ecm = &echo_client_main;
891 ecm->is_init = 0;
892 return 0;
893}
894
895VLIB_INIT_FUNCTION (echo_clients_main_init);
896
897/*
898 * fd.io coding-style-patch-verification: ON
899 *
900 * Local Variables:
901 * eval: (c-set-style "gnu")
902 * End:
903 */