blob: c39f787c093e69b652a5eef4333ac51df1dad57c [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;
Dave Barach178cf492018-11-13 16:34:13 -050089 clib_memcpy_fast (&hdr.rmt_ip, &at->rmt_ip,
90 sizeof (ip46_address_t));
Florin Coras8e43d042018-05-04 15:46:57 -070091 hdr.is_ip4 = at->is_ip4;
92 hdr.rmt_port = at->rmt_port;
Dave Barach178cf492018-11-13 16:34:13 -050093 clib_memcpy_fast (&hdr.lcl_ip, &at->lcl_ip,
94 sizeof (ip46_address_t));
Florin Coras8e43d042018-05-04 15:46:57 -070095 hdr.lcl_port = at->lcl_port;
96 svm_fifo_enqueue_nowait (f, sizeof (hdr), (u8 *) & hdr);
97 svm_fifo_enqueue_nocopy (f, rv);
Florin Coras3c2fed52018-07-04 04:15:05 -070098 session_send_io_evt_to_thread_custom (f, s->thread_index,
99 FIFO_EVENT_APP_TX);
Florin Coras8e43d042018-05-04 15:46:57 -0700100 }
101 else
102 rv = app_send_dgram (&s->data, test_data + test_buf_offset,
103 bytes_this_chunk, 0);
104 }
Florin Coras4399c2e2018-01-25 06:34:42 -0800105
106 /* If we managed to enqueue data... */
107 if (rv > 0)
108 {
109 /* Account for it... */
110 s->bytes_to_send -= rv;
111 s->bytes_sent += rv;
112
113 if (ECHO_CLIENT_DBG)
114 {
115 /* *INDENT-OFF* */
116 ELOG_TYPE_DECLARE (e) =
117 {
118 .format = "tx-enq: xfer %d bytes, sent %u remain %u",
119 .format_args = "i4i4i4",
120 };
121 /* *INDENT-ON* */
122 struct
123 {
124 u32 data[3];
125 } *ed;
126 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
127 ed->data[0] = rv;
128 ed->data[1] = s->bytes_sent;
129 ed->data[2] = s->bytes_to_send;
130 }
Florin Coras4399c2e2018-01-25 06:34:42 -0800131 }
132}
133
134static void
Florin Coras48750892018-05-16 09:28:02 -0700135receive_data_chunk (echo_client_main_t * ecm, eclient_session_t * s)
Florin Coras4399c2e2018-01-25 06:34:42 -0800136{
Florin Coras7fb0fe12018-04-09 09:24:52 -0700137 svm_fifo_t *rx_fifo = s->data.rx_fifo;
138 u32 thread_index = vlib_get_thread_index ();
Florin Coras4399c2e2018-01-25 06:34:42 -0800139 int n_read, i;
140
141 if (ecm->test_bytes)
142 {
Florin Coras7fb0fe12018-04-09 09:24:52 -0700143 if (!ecm->is_dgram)
144 n_read = app_recv_stream (&s->data, ecm->rx_buf[thread_index],
145 vec_len (ecm->rx_buf[thread_index]));
146 else
147 n_read = app_recv_dgram (&s->data, ecm->rx_buf[thread_index],
148 vec_len (ecm->rx_buf[thread_index]));
Florin Coras4399c2e2018-01-25 06:34:42 -0800149 }
150 else
151 {
152 n_read = svm_fifo_max_dequeue (rx_fifo);
153 svm_fifo_dequeue_drop (rx_fifo, n_read);
154 }
155
156 if (n_read > 0)
157 {
158 if (ECHO_CLIENT_DBG)
159 {
160 /* *INDENT-OFF* */
161 ELOG_TYPE_DECLARE (e) =
162 {
163 .format = "rx-deq: %d bytes",
164 .format_args = "i4",
165 };
166 /* *INDENT-ON* */
167 struct
168 {
169 u32 data[1];
170 } *ed;
171 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
172 ed->data[0] = n_read;
173 }
174
175 if (ecm->test_bytes)
176 {
177 for (i = 0; i < n_read; i++)
178 {
Florin Coras7fb0fe12018-04-09 09:24:52 -0700179 if (ecm->rx_buf[thread_index][i]
Florin Coras4399c2e2018-01-25 06:34:42 -0800180 != ((s->bytes_received + i) & 0xff))
181 {
182 clib_warning ("read %d error at byte %lld, 0x%x not 0x%x",
183 n_read, s->bytes_received + i,
Florin Coras7fb0fe12018-04-09 09:24:52 -0700184 ecm->rx_buf[thread_index][i],
Florin Coras4399c2e2018-01-25 06:34:42 -0800185 ((s->bytes_received + i) & 0xff));
186 ecm->test_failed = 1;
187 }
188 }
189 }
Florin Coras7fb0fe12018-04-09 09:24:52 -0700190 ASSERT (n_read <= s->bytes_to_receive);
Florin Coras4399c2e2018-01-25 06:34:42 -0800191 s->bytes_to_receive -= n_read;
192 s->bytes_received += n_read;
193 }
194}
195
196static uword
197echo_client_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
198 vlib_frame_t * frame)
199{
200 echo_client_main_t *ecm = &echo_client_main;
201 int my_thread_index = vlib_get_thread_index ();
Florin Coras48750892018-05-16 09:28:02 -0700202 eclient_session_t *sp;
Florin Coras4399c2e2018-01-25 06:34:42 -0800203 int i;
204 int delete_session;
205 u32 *connection_indices;
206 u32 *connections_this_batch;
207 u32 nconnections_this_batch;
208
209 connection_indices = ecm->connection_index_by_thread[my_thread_index];
210 connections_this_batch =
211 ecm->connections_this_batch_by_thread[my_thread_index];
212
Florin Coraseb97e5f2018-10-15 21:35:42 -0700213 if ((ecm->run_test != ECHO_CLIENTS_RUNNING) ||
Florin Coras4399c2e2018-01-25 06:34:42 -0800214 ((vec_len (connection_indices) == 0)
215 && vec_len (connections_this_batch) == 0))
216 return 0;
217
218 /* Grab another pile of connections */
219 if (PREDICT_FALSE (vec_len (connections_this_batch) == 0))
220 {
221 nconnections_this_batch =
222 clib_min (ecm->connections_per_batch, vec_len (connection_indices));
223
224 ASSERT (nconnections_this_batch > 0);
225 vec_validate (connections_this_batch, nconnections_this_batch - 1);
Dave Barach178cf492018-11-13 16:34:13 -0500226 clib_memcpy_fast (connections_this_batch,
227 connection_indices + vec_len (connection_indices)
228 - nconnections_this_batch,
229 nconnections_this_batch * sizeof (u32));
Florin Coras4399c2e2018-01-25 06:34:42 -0800230 _vec_len (connection_indices) -= nconnections_this_batch;
231 }
232
233 if (PREDICT_FALSE (ecm->prev_conns != ecm->connections_per_batch
234 && ecm->prev_conns == vec_len (connections_this_batch)))
235 {
236 ecm->repeats++;
237 ecm->prev_conns = vec_len (connections_this_batch);
238 if (ecm->repeats == 500000)
239 {
240 clib_warning ("stuck clients");
241 }
242 }
243 else
244 {
245 ecm->prev_conns = vec_len (connections_this_batch);
246 ecm->repeats = 0;
247 }
248
249 for (i = 0; i < vec_len (connections_this_batch); i++)
250 {
251 delete_session = 1;
252
253 sp = pool_elt_at_index (ecm->sessions, connections_this_batch[i]);
254
255 if (sp->bytes_to_send > 0)
256 {
257 send_data_chunk (ecm, sp);
258 delete_session = 0;
259 }
260 if (sp->bytes_to_receive > 0)
261 {
Florin Coras4399c2e2018-01-25 06:34:42 -0800262 delete_session = 0;
263 }
264 if (PREDICT_FALSE (delete_session == 1))
265 {
Florin Coras288eaab2019-02-03 15:26:14 -0800266 session_t *s;
Florin Coras4399c2e2018-01-25 06:34:42 -0800267
Sirshak Das2f6d7bb2018-10-03 22:53:51 +0000268 clib_atomic_fetch_add (&ecm->tx_total, sp->bytes_sent);
269 clib_atomic_fetch_add (&ecm->rx_total, sp->bytes_received);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700270 s = session_get_from_handle_if_valid (sp->vpp_session_handle);
Florin Coras4399c2e2018-01-25 06:34:42 -0800271
272 if (s)
273 {
274 vnet_disconnect_args_t _a, *a = &_a;
275 a->handle = session_handle (s);
276 a->app_index = ecm->app_index;
277 vnet_disconnect_session (a);
278
279 vec_delete (connections_this_batch, 1, i);
280 i--;
Sirshak Das2f6d7bb2018-10-03 22:53:51 +0000281 clib_atomic_fetch_add (&ecm->ready_connections, -1);
Florin Coras4399c2e2018-01-25 06:34:42 -0800282 }
283 else
Florin Coras2f8d8fa2018-01-26 06:36:04 -0800284 {
285 clib_warning ("session AWOL?");
286 vec_delete (connections_this_batch, 1, i);
287 }
Florin Coras4399c2e2018-01-25 06:34:42 -0800288
289 /* Kick the debug CLI process */
290 if (ecm->ready_connections == 0)
291 {
292 signal_evt_to_cli (2);
293 }
294 }
295 }
296
297 ecm->connection_index_by_thread[my_thread_index] = connection_indices;
298 ecm->connections_this_batch_by_thread[my_thread_index] =
299 connections_this_batch;
300 return 0;
301}
302
303/* *INDENT-OFF* */
304VLIB_REGISTER_NODE (echo_clients_node) =
305{
306 .function = echo_client_node_fn,
307 .name = "echo-clients",
308 .type = VLIB_NODE_TYPE_INPUT,
309 .state = VLIB_NODE_STATE_DISABLED,
310};
311/* *INDENT-ON* */
312
313static int
314create_api_loopback (echo_client_main_t * ecm)
315{
316 api_main_t *am = &api_main;
317 vl_shmem_hdr_t *shmem_hdr;
318
319 shmem_hdr = am->shmem_hdr;
320 ecm->vl_input_queue = shmem_hdr->vl_input_queue;
321 ecm->my_client_index = vl_api_memclnt_create_internal ("echo_client",
322 ecm->vl_input_queue);
323 return 0;
324}
325
326static int
327echo_clients_init (vlib_main_t * vm)
328{
329 echo_client_main_t *ecm = &echo_client_main;
330 vlib_thread_main_t *vtm = vlib_get_thread_main ();
331 u32 num_threads;
332 int i;
333
334 if (create_api_loopback (ecm))
335 return -1;
336
337 num_threads = 1 /* main thread */ + vtm->n_threads;
338
Florin Coras7fb0fe12018-04-09 09:24:52 -0700339 /* Init test data. Big buffer */
340 vec_validate (ecm->connect_test_data, 4 * 1024 * 1024 - 1);
Florin Coras4399c2e2018-01-25 06:34:42 -0800341 for (i = 0; i < vec_len (ecm->connect_test_data); i++)
342 ecm->connect_test_data[i] = i & 0xff;
343
344 vec_validate (ecm->rx_buf, num_threads - 1);
345 for (i = 0; i < num_threads; i++)
346 vec_validate (ecm->rx_buf[i], vec_len (ecm->connect_test_data) - 1);
347
348 ecm->is_init = 1;
349
350 vec_validate (ecm->connection_index_by_thread, vtm->n_vlib_mains);
351 vec_validate (ecm->connections_this_batch_by_thread, vtm->n_vlib_mains);
352 vec_validate (ecm->vpp_event_queue, vtm->n_vlib_mains);
353
354 return 0;
355}
356
357static int
358echo_clients_session_connected_callback (u32 app_index, u32 api_context,
Florin Coras288eaab2019-02-03 15:26:14 -0800359 session_t * s, u8 is_fail)
Florin Coras4399c2e2018-01-25 06:34:42 -0800360{
361 echo_client_main_t *ecm = &echo_client_main;
Florin Coras48750892018-05-16 09:28:02 -0700362 eclient_session_t *session;
Florin Coras4399c2e2018-01-25 06:34:42 -0800363 u32 session_index;
Florin Coras52207f12018-07-12 14:48:06 -0700364 u8 thread_index;
Florin Coras4399c2e2018-01-25 06:34:42 -0800365
Florin Coraseb97e5f2018-10-15 21:35:42 -0700366 if (PREDICT_FALSE (ecm->run_test != ECHO_CLIENTS_STARTING))
367 return -1;
368
Florin Coras4399c2e2018-01-25 06:34:42 -0800369 if (is_fail)
370 {
371 clib_warning ("connection %d failed!", api_context);
Florin Corasc01d5782018-10-17 14:53:11 -0700372 ecm->run_test = ECHO_CLIENTS_EXITING;
Florin Coras4399c2e2018-01-25 06:34:42 -0800373 signal_evt_to_cli (-1);
374 return 0;
375 }
376
Florin Coras52207f12018-07-12 14:48:06 -0700377 thread_index = s->thread_index;
Florin Coras40903ac2018-06-10 14:41:23 -0700378 ASSERT (thread_index == vlib_get_thread_index ()
379 || session_transport_service_type (s) == TRANSPORT_SERVICE_CL);
Florin Coras4399c2e2018-01-25 06:34:42 -0800380
381 if (!ecm->vpp_event_queue[thread_index])
382 ecm->vpp_event_queue[thread_index] =
383 session_manager_get_vpp_event_queue (thread_index);
384
385 /*
386 * Setup session
387 */
388 clib_spinlock_lock_if_init (&ecm->sessions_lock);
389 pool_get (ecm->sessions, session);
390 clib_spinlock_unlock_if_init (&ecm->sessions_lock);
391
Dave Barachb7b92992018-10-17 10:38:51 -0400392 clib_memset (session, 0, sizeof (*session));
Florin Coras4399c2e2018-01-25 06:34:42 -0800393 session_index = session - ecm->sessions;
394 session->bytes_to_send = ecm->bytes_to_send;
395 session->bytes_to_receive = ecm->no_return ? 0ULL : ecm->bytes_to_send;
Florin Coras288eaab2019-02-03 15:26:14 -0800396 session->data.rx_fifo = s->rx_fifo;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700397 session->data.rx_fifo->client_session_index = session_index;
Florin Coras288eaab2019-02-03 15:26:14 -0800398 session->data.tx_fifo = s->tx_fifo;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700399 session->data.tx_fifo->client_session_index = session_index;
400 session->data.vpp_evt_q = ecm->vpp_event_queue[thread_index];
Florin Coras4399c2e2018-01-25 06:34:42 -0800401 session->vpp_session_handle = session_handle (s);
402
Florin Coras7fb0fe12018-04-09 09:24:52 -0700403 if (ecm->is_dgram)
404 {
405 transport_connection_t *tc;
406 tc = session_get_transport (s);
Dave Barach178cf492018-11-13 16:34:13 -0500407 clib_memcpy_fast (&session->data.transport, tc,
408 sizeof (session->data.transport));
Florin Coras7fb0fe12018-04-09 09:24:52 -0700409 session->data.is_dgram = 1;
410 }
411
Florin Coras4399c2e2018-01-25 06:34:42 -0800412 vec_add1 (ecm->connection_index_by_thread[thread_index], session_index);
Sirshak Das2f6d7bb2018-10-03 22:53:51 +0000413 clib_atomic_fetch_add (&ecm->ready_connections, 1);
Florin Coras4399c2e2018-01-25 06:34:42 -0800414 if (ecm->ready_connections == ecm->expected_connections)
415 {
Florin Coraseb97e5f2018-10-15 21:35:42 -0700416 ecm->run_test = ECHO_CLIENTS_RUNNING;
Florin Coras4399c2e2018-01-25 06:34:42 -0800417 /* Signal the CLI process that the action is starting... */
418 signal_evt_to_cli (1);
419 }
420
421 return 0;
422}
423
424static void
Florin Coras288eaab2019-02-03 15:26:14 -0800425echo_clients_session_reset_callback (session_t * s)
Florin Coras4399c2e2018-01-25 06:34:42 -0800426{
Florin Coras4af830c2018-12-04 09:21:36 -0800427 echo_client_main_t *ecm = &echo_client_main;
428 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
429
Florin Coras4399c2e2018-01-25 06:34:42 -0800430 if (s->session_state == SESSION_STATE_READY)
431 clib_warning ("Reset active connection %U", format_stream_session, s, 2);
Florin Coras4af830c2018-12-04 09:21:36 -0800432
433 a->handle = session_handle (s);
434 a->app_index = ecm->app_index;
435 vnet_disconnect_session (a);
Florin Coras4399c2e2018-01-25 06:34:42 -0800436 return;
437}
438
439static int
Florin Coras288eaab2019-02-03 15:26:14 -0800440echo_clients_session_create_callback (session_t * s)
Florin Coras4399c2e2018-01-25 06:34:42 -0800441{
442 return 0;
443}
444
445static void
Florin Coras288eaab2019-02-03 15:26:14 -0800446echo_clients_session_disconnect_callback (session_t * s)
Florin Coras4399c2e2018-01-25 06:34:42 -0800447{
448 echo_client_main_t *ecm = &echo_client_main;
Florin Coras4af830c2018-12-04 09:21:36 -0800449 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
Florin Coras4399c2e2018-01-25 06:34:42 -0800450 a->handle = session_handle (s);
451 a->app_index = ecm->app_index;
452 vnet_disconnect_session (a);
453 return;
454}
455
Florin Corasc01d5782018-10-17 14:53:11 -0700456void
Florin Coras288eaab2019-02-03 15:26:14 -0800457echo_clients_session_disconnect (session_t * s)
Florin Corasc01d5782018-10-17 14:53:11 -0700458{
459 echo_client_main_t *ecm = &echo_client_main;
Florin Coras4af830c2018-12-04 09:21:36 -0800460 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
Florin Corasc01d5782018-10-17 14:53:11 -0700461 a->handle = session_handle (s);
462 a->app_index = ecm->app_index;
463 vnet_disconnect_session (a);
464}
465
Florin Coras4399c2e2018-01-25 06:34:42 -0800466static int
Florin Coras288eaab2019-02-03 15:26:14 -0800467echo_clients_rx_callback (session_t * s)
Florin Coras4399c2e2018-01-25 06:34:42 -0800468{
Florin Coras7fb0fe12018-04-09 09:24:52 -0700469 echo_client_main_t *ecm = &echo_client_main;
Florin Coras48750892018-05-16 09:28:02 -0700470 eclient_session_t *sp;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700471
Florin Coraseb97e5f2018-10-15 21:35:42 -0700472 if (PREDICT_FALSE (ecm->run_test != ECHO_CLIENTS_RUNNING))
473 {
474 echo_clients_session_disconnect (s);
475 return -1;
476 }
477
Florin Coras288eaab2019-02-03 15:26:14 -0800478 sp = pool_elt_at_index (ecm->sessions, s->rx_fifo->client_session_index);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700479 receive_data_chunk (ecm, sp);
480
Florin Coras288eaab2019-02-03 15:26:14 -0800481 if (svm_fifo_max_dequeue (s->rx_fifo))
Florin Coras7fb0fe12018-04-09 09:24:52 -0700482 {
Florin Coras288eaab2019-02-03 15:26:14 -0800483 if (svm_fifo_set_event (s->rx_fifo))
484 session_send_io_evt_to_thread (s->rx_fifo, FIFO_EVENT_BUILTIN_RX);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700485 }
Florin Coras4399c2e2018-01-25 06:34:42 -0800486 return 0;
487}
488
Florin Corasa332c462018-01-31 06:52:17 -0800489int
Florin Corasfa76a762018-11-29 12:40:10 -0800490echo_client_add_segment_callback (u32 client_index, u64 segment_handle)
Florin Corasa332c462018-01-31 06:52:17 -0800491{
492 /* New heaps may be added */
493 return 0;
494}
495
Florin Coras4399c2e2018-01-25 06:34:42 -0800496/* *INDENT-OFF* */
497static session_cb_vft_t echo_clients = {
498 .session_reset_callback = echo_clients_session_reset_callback,
499 .session_connected_callback = echo_clients_session_connected_callback,
500 .session_accept_callback = echo_clients_session_create_callback,
501 .session_disconnect_callback = echo_clients_session_disconnect_callback,
Florin Coras371ca502018-02-21 12:07:41 -0800502 .builtin_app_rx_callback = echo_clients_rx_callback,
Florin Corasa332c462018-01-31 06:52:17 -0800503 .add_segment_callback = echo_client_add_segment_callback
Florin Coras4399c2e2018-01-25 06:34:42 -0800504};
505/* *INDENT-ON* */
506
507static clib_error_t *
508echo_clients_attach (u8 * appns_id, u64 appns_flags, u64 appns_secret)
509{
Florin Coras2f8d8fa2018-01-26 06:36:04 -0800510 u32 prealloc_fifos, segment_size = 256 << 20;
Florin Coras4399c2e2018-01-25 06:34:42 -0800511 echo_client_main_t *ecm = &echo_client_main;
512 vnet_app_attach_args_t _a, *a = &_a;
513 u64 options[16];
514 clib_error_t *error = 0;
515
Dave Barachb7b92992018-10-17 10:38:51 -0400516 clib_memset (a, 0, sizeof (*a));
517 clib_memset (options, 0, sizeof (options));
Florin Coras4399c2e2018-01-25 06:34:42 -0800518
519 a->api_client_index = ecm->my_client_index;
520 a->session_cb_vft = &echo_clients;
521
522 prealloc_fifos = ecm->prealloc_fifos ? ecm->expected_connections : 1;
523
524 if (ecm->private_segment_size)
525 segment_size = ecm->private_segment_size;
526
527 options[APP_OPTIONS_ACCEPT_COOKIE] = 0x12345678;
528 options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
Florin Corasa332c462018-01-31 06:52:17 -0800529 options[APP_OPTIONS_ADD_SEGMENT_SIZE] = segment_size;
Florin Coras4399c2e2018-01-25 06:34:42 -0800530 options[APP_OPTIONS_RX_FIFO_SIZE] = ecm->fifo_size;
531 options[APP_OPTIONS_TX_FIFO_SIZE] = ecm->fifo_size;
532 options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = ecm->private_segment_count;
533 options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = prealloc_fifos;
Florin Coras4399c2e2018-01-25 06:34:42 -0800534 options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
Florin Coras58d36f02018-03-09 13:05:53 -0800535 options[APP_OPTIONS_TLS_ENGINE] = ecm->tls_engine;
Florin Coras4399c2e2018-01-25 06:34:42 -0800536 if (appns_id)
537 {
538 options[APP_OPTIONS_FLAGS] |= appns_flags;
539 options[APP_OPTIONS_NAMESPACE_SECRET] = appns_secret;
540 }
541 a->options = options;
542 a->namespace_id = appns_id;
543
544 if ((error = vnet_application_attach (a)))
545 return error;
546
547 ecm->app_index = a->app_index;
548 return 0;
549}
550
551static int
552echo_clients_detach ()
553{
554 echo_client_main_t *ecm = &echo_client_main;
555 vnet_app_detach_args_t _da, *da = &_da;
556 int rv;
557
558 da->app_index = ecm->app_index;
Florin Coras4af830c2018-12-04 09:21:36 -0800559 da->api_client_index = ~0;
Florin Coras4399c2e2018-01-25 06:34:42 -0800560 rv = vnet_application_detach (da);
561 ecm->test_client_attached = 0;
562 ecm->app_index = ~0;
563 return rv;
564}
565
566static void *
567echo_client_thread_fn (void *arg)
568{
569 return 0;
570}
571
572/** Start a transmit thread */
573int
574echo_clients_start_tx_pthread (echo_client_main_t * ecm)
575{
576 if (ecm->client_thread_handle == 0)
577 {
578 int rv = pthread_create (&ecm->client_thread_handle,
579 NULL /*attr */ ,
580 echo_client_thread_fn, 0);
581 if (rv)
582 {
583 ecm->client_thread_handle = 0;
584 return -1;
585 }
586 }
587 return 0;
588}
589
590clib_error_t *
591echo_clients_connect (vlib_main_t * vm, u32 n_clients)
592{
593 echo_client_main_t *ecm = &echo_client_main;
594 vnet_connect_args_t _a, *a = &_a;
595 clib_error_t *error = 0;
596 int i;
Florin Coras8f89dd02018-03-05 16:53:07 -0800597
Dave Barachb7b92992018-10-17 10:38:51 -0400598 clib_memset (a, 0, sizeof (*a));
Florin Coras4399c2e2018-01-25 06:34:42 -0800599 for (i = 0; i < n_clients; i++)
600 {
Florin Coras4399c2e2018-01-25 06:34:42 -0800601 a->uri = (char *) ecm->connect_uri;
602 a->api_context = i;
603 a->app_index = ecm->app_index;
Florin Coras4399c2e2018-01-25 06:34:42 -0800604
605 if ((error = vnet_connect_uri (a)))
606 return error;
607
608 /* Crude pacing for call setups */
609 if ((i % 4) == 0)
610 vlib_process_suspend (vm, 10e-6);
611 ASSERT (i + 1 >= ecm->ready_connections);
612 while (i + 1 - ecm->ready_connections > 1000)
613 {
614 vlib_process_suspend (vm, 100e-6);
615 }
616 }
617 return 0;
618}
619
620#define ec_cli_output(_fmt, _args...) \
Florin Coras7fb0fe12018-04-09 09:24:52 -0700621 if (!ecm->no_output) \
Florin Coras4399c2e2018-01-25 06:34:42 -0800622 vlib_cli_output(vm, _fmt, ##_args)
623
624static clib_error_t *
625echo_clients_command_fn (vlib_main_t * vm,
626 unformat_input_t * input, vlib_cli_command_t * cmd)
627{
628 echo_client_main_t *ecm = &echo_client_main;
629 vlib_thread_main_t *thread_main = vlib_get_thread_main ();
Florin Coras4399c2e2018-01-25 06:34:42 -0800630 u64 tmp, total_bytes, appns_flags = 0, appns_secret = 0;
631 f64 test_timeout = 20.0, syn_timeout = 20.0, delta;
Florin Coras2f8d8fa2018-01-26 06:36:04 -0800632 char *default_uri = "tcp://6.0.1.1/1234";
633 uword *event_data = 0, event_type;
Florin Coras4399c2e2018-01-25 06:34:42 -0800634 f64 time_before_connects;
635 u32 n_clients = 1;
636 int preallocate_sessions = 0;
637 char *transfer_type;
638 clib_error_t *error = 0;
Florin Coras2f8d8fa2018-01-26 06:36:04 -0800639 u8 *appns_id = 0;
Florin Coras4399c2e2018-01-25 06:34:42 -0800640 int i;
641
642 ecm->bytes_to_send = 8192;
643 ecm->no_return = 0;
644 ecm->fifo_size = 64 << 10;
645 ecm->connections_per_batch = 1000;
646 ecm->private_segment_count = 0;
647 ecm->private_segment_size = 0;
648 ecm->no_output = 0;
649 ecm->test_bytes = 0;
650 ecm->test_failed = 0;
651 ecm->vlib_main = vm;
Florin Coras58d36f02018-03-09 13:05:53 -0800652 ecm->tls_engine = TLS_ENGINE_OPENSSL;
Florin Coras8e43d042018-05-04 15:46:57 -0700653 ecm->no_copy = 0;
Florin Coraseb97e5f2018-10-15 21:35:42 -0700654 ecm->run_test = ECHO_CLIENTS_STARTING;
Florin Coras58d36f02018-03-09 13:05:53 -0800655
Florin Coras4399c2e2018-01-25 06:34:42 -0800656 if (thread_main->n_vlib_mains > 1)
657 clib_spinlock_init (&ecm->sessions_lock);
658 vec_free (ecm->connect_uri);
659
660 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
661 {
662 if (unformat (input, "uri %s", &ecm->connect_uri))
663 ;
664 else if (unformat (input, "nclients %d", &n_clients))
665 ;
666 else if (unformat (input, "mbytes %lld", &tmp))
667 ecm->bytes_to_send = tmp << 20;
668 else if (unformat (input, "gbytes %lld", &tmp))
669 ecm->bytes_to_send = tmp << 30;
670 else if (unformat (input, "bytes %lld", &ecm->bytes_to_send))
671 ;
672 else if (unformat (input, "test-timeout %f", &test_timeout))
673 ;
674 else if (unformat (input, "syn-timeout %f", &syn_timeout))
675 ;
676 else if (unformat (input, "no-return"))
677 ecm->no_return = 1;
678 else if (unformat (input, "fifo-size %d", &ecm->fifo_size))
679 ecm->fifo_size <<= 10;
680 else if (unformat (input, "private-segment-count %d",
681 &ecm->private_segment_count))
682 ;
683 else if (unformat (input, "private-segment-size %U",
684 unformat_memory_size, &tmp))
685 {
686 if (tmp >= 0x100000000ULL)
687 return clib_error_return
688 (0, "private segment size %lld (%llu) too large", tmp, tmp);
689 ecm->private_segment_size = tmp;
690 }
691 else if (unformat (input, "preallocate-fifos"))
692 ecm->prealloc_fifos = 1;
693 else if (unformat (input, "preallocate-sessions"))
694 preallocate_sessions = 1;
695 else
696 if (unformat (input, "client-batch %d", &ecm->connections_per_batch))
697 ;
698 else if (unformat (input, "appns %_%v%_", &appns_id))
699 ;
700 else if (unformat (input, "all-scope"))
701 appns_flags |= (APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE
702 | APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE);
703 else if (unformat (input, "local-scope"))
704 appns_flags = APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
705 else if (unformat (input, "global-scope"))
706 appns_flags = APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
707 else if (unformat (input, "secret %lu", &appns_secret))
708 ;
709 else if (unformat (input, "no-output"))
710 ecm->no_output = 1;
711 else if (unformat (input, "test-bytes"))
712 ecm->test_bytes = 1;
Florin Coras58d36f02018-03-09 13:05:53 -0800713 else if (unformat (input, "tls-engine %d", &ecm->tls_engine))
714 ;
Florin Coras4399c2e2018-01-25 06:34:42 -0800715 else
Florin Corasa332c462018-01-31 06:52:17 -0800716 return clib_error_return (0, "failed: unknown input `%U'",
Florin Coras4399c2e2018-01-25 06:34:42 -0800717 format_unformat_error, input);
718 }
719
720 /* Store cli process node index for signalling */
721 ecm->cli_node_index =
722 vlib_get_current_process (vm)->node_runtime.node_index;
723
724 if (ecm->is_init == 0)
725 {
726 if (echo_clients_init (vm))
727 return clib_error_return (0, "failed init");
728 }
729
730
731 ecm->ready_connections = 0;
732 ecm->expected_connections = n_clients;
733 ecm->rx_total = 0;
734 ecm->tx_total = 0;
735
736 if (!ecm->connect_uri)
737 {
Florin Coras2f8d8fa2018-01-26 06:36:04 -0800738 clib_warning ("No uri provided. Using default: %s", default_uri);
739 ecm->connect_uri = format (0, "%s%c", default_uri, 0);
Florin Coras4399c2e2018-01-25 06:34:42 -0800740 }
741
Florin Coras7fb0fe12018-04-09 09:24:52 -0700742 if (ecm->connect_uri[0] == 'u' && ecm->connect_uri[3] != 'c')
743 ecm->is_dgram = 1;
744
Florin Coras4399c2e2018-01-25 06:34:42 -0800745#if ECHO_CLIENT_PTHREAD
746 echo_clients_start_tx_pthread ();
747#endif
748
749 vlib_worker_thread_barrier_sync (vm);
750 vnet_session_enable_disable (vm, 1 /* turn on session and transports */ );
751 vlib_worker_thread_barrier_release (vm);
752
753 if (ecm->test_client_attached == 0)
754 {
755 if ((error = echo_clients_attach (appns_id, appns_flags, appns_secret)))
756 {
757 vec_free (appns_id);
758 clib_error_report (error);
759 return error;
760 }
761 vec_free (appns_id);
762 }
763 ecm->test_client_attached = 1;
764
765 /* Turn on the builtin client input nodes */
766 for (i = 0; i < thread_main->n_vlib_mains; i++)
767 vlib_node_set_state (vlib_mains[i], echo_clients_node.index,
768 VLIB_NODE_STATE_POLLING);
769
770 if (preallocate_sessions)
Florin Coras48750892018-05-16 09:28:02 -0700771 pool_init_fixed (ecm->sessions, 1.1 * n_clients);
Florin Coras4399c2e2018-01-25 06:34:42 -0800772
773 /* Fire off connect requests */
774 time_before_connects = vlib_time_now (vm);
775 if ((error = echo_clients_connect (vm, n_clients)))
Florin Corasc977e7c2018-10-16 20:30:31 -0700776 goto cleanup;
Florin Coras4399c2e2018-01-25 06:34:42 -0800777
778 /* Park until the sessions come up, or ten seconds elapse... */
779 vlib_process_wait_for_event_or_clock (vm, syn_timeout);
780 event_type = vlib_process_get_events (vm, &event_data);
781 switch (event_type)
782 {
783 case ~0:
784 ec_cli_output ("Timeout with only %d sessions active...",
785 ecm->ready_connections);
786 error = clib_error_return (0, "failed: syn timeout with %d sessions",
787 ecm->ready_connections);
788 goto cleanup;
789
790 case 1:
791 delta = vlib_time_now (vm) - time_before_connects;
792 if (delta != 0.0)
793 ec_cli_output ("%d three-way handshakes in %.2f seconds %.2f/s",
794 n_clients, delta, ((f64) n_clients) / delta);
795
796 ecm->test_start_time = vlib_time_now (ecm->vlib_main);
797 ec_cli_output ("Test started at %.6f", ecm->test_start_time);
798 break;
799
800 default:
801 ec_cli_output ("unexpected event(1): %d", event_type);
802 error = clib_error_return (0, "failed: unexpected event(1): %d",
803 event_type);
804 goto cleanup;
805 }
806
807 /* Now wait for the sessions to finish... */
808 vlib_process_wait_for_event_or_clock (vm, test_timeout);
809 event_type = vlib_process_get_events (vm, &event_data);
810 switch (event_type)
811 {
812 case ~0:
813 ec_cli_output ("Timeout with %d sessions still active...",
814 ecm->ready_connections);
815 error = clib_error_return (0, "failed: timeout with %d sessions",
816 ecm->ready_connections);
817 goto cleanup;
818
819 case 2:
820 ecm->test_end_time = vlib_time_now (vm);
821 ec_cli_output ("Test finished at %.6f", ecm->test_end_time);
822 break;
823
824 default:
825 ec_cli_output ("unexpected event(2): %d", event_type);
826 error = clib_error_return (0, "failed: unexpected event(2): %d",
827 event_type);
828 goto cleanup;
829 }
830
831 delta = ecm->test_end_time - ecm->test_start_time;
832 if (delta != 0.0)
833 {
834 total_bytes = (ecm->no_return ? ecm->tx_total : ecm->rx_total);
835 transfer_type = ecm->no_return ? "half-duplex" : "full-duplex";
836 ec_cli_output ("%lld bytes (%lld mbytes, %lld gbytes) in %.2f seconds",
837 total_bytes, total_bytes / (1ULL << 20),
838 total_bytes / (1ULL << 30), delta);
839 ec_cli_output ("%.2f bytes/second %s", ((f64) total_bytes) / (delta),
840 transfer_type);
841 ec_cli_output ("%.4f gbit/second %s",
842 (((f64) total_bytes * 8.0) / delta / 1e9),
843 transfer_type);
844 }
845 else
846 {
847 ec_cli_output ("zero delta-t?");
848 error = clib_error_return (0, "failed: zero delta-t");
849 goto cleanup;
850 }
851
852 if (ecm->test_bytes && ecm->test_failed)
853 error = clib_error_return (0, "failed: test bytes");
854
855cleanup:
Florin Coraseb97e5f2018-10-15 21:35:42 -0700856 ecm->run_test = ECHO_CLIENTS_EXITING;
Florin Corasef915342018-09-29 10:23:06 -0700857 vlib_process_wait_for_event_or_clock (vm, 10e-3);
Florin Coras4399c2e2018-01-25 06:34:42 -0800858 for (i = 0; i < vec_len (ecm->connection_index_by_thread); i++)
859 {
860 vec_reset_length (ecm->connection_index_by_thread[i]);
861 vec_reset_length (ecm->connections_this_batch_by_thread[i]);
862 }
863
864 pool_free (ecm->sessions);
865
866 /* Detach the application, so we can use different fifo sizes next time */
867 if (ecm->test_client_attached)
868 {
869 if (echo_clients_detach ())
870 {
871 error = clib_error_return (0, "failed: app detach");
872 ec_cli_output ("WARNING: app detach failed...");
873 }
874 }
875 if (error)
876 ec_cli_output ("test failed");
Florin Coras2f8d8fa2018-01-26 06:36:04 -0800877 vec_free (ecm->connect_uri);
Florin Coras4399c2e2018-01-25 06:34:42 -0800878 return error;
879}
880
881/* *INDENT-OFF* */
882VLIB_CLI_COMMAND (echo_clients_command, static) =
883{
884 .path = "test echo clients",
885 .short_help = "test echo clients [nclients %d][[m|g]bytes <bytes>]"
886 "[test-timeout <time>][syn-timeout <time>][no-return][fifo-size <size>]"
887 "[private-segment-count <count>][private-segment-size <bytes>[m|g]]"
888 "[preallocate-fifos][preallocate-sessions][client-batch <batch-size>]"
889 "[uri <tcp://ip/port>][test-bytes][no-output]",
890 .function = echo_clients_command_fn,
891 .is_mp_safe = 1,
892};
893/* *INDENT-ON* */
894
895clib_error_t *
896echo_clients_main_init (vlib_main_t * vm)
897{
898 echo_client_main_t *ecm = &echo_client_main;
899 ecm->is_init = 0;
900 return 0;
901}
902
903VLIB_INIT_FUNCTION (echo_clients_main_init);
904
905/*
906 * fd.io coding-style-patch-verification: ON
907 *
908 * Local Variables:
909 * eval: (c-set-style "gnu")
910 * End:
911 */