session: use msg queue for events
[vpp.git] / src / vnet / session-apps / echo_client.c
1 /*
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
23 echo_client_main_t echo_client_main;
24
25 #define ECHO_CLIENT_DBG (0)
26
27 static void
28 signal_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
35 static void
36 signal_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
45 static void
46 send_data_chunk (echo_client_main_t * ecm, eclient_session_t * s)
47 {
48   u8 *test_data = ecm->connect_test_data;
49   int test_buf_len, test_buf_offset, rv;
50   u32 bytes_this_chunk;
51
52   test_buf_len = vec_len (test_data);
53   ASSERT (test_buf_len > 0);
54   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);
57
58   if (!ecm->is_dgram)
59     {
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           session_send_io_evt_to_thread_custom (f, s->thread_index,
66                                                 FIFO_EVENT_APP_TX);
67         }
68       else
69         rv = app_send_stream (&s->data, test_data + test_buf_offset,
70                               bytes_this_chunk, 0);
71     }
72   else
73     {
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);
96           session_send_io_evt_to_thread_custom (f, s->thread_index,
97                                                 FIFO_EVENT_APP_TX);
98         }
99       else
100         rv = app_send_dgram (&s->data, test_data + test_buf_offset,
101                              bytes_this_chunk, 0);
102     }
103
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         }
129     }
130 }
131
132 static void
133 receive_data_chunk (echo_client_main_t * ecm, eclient_session_t * s)
134 {
135   svm_fifo_t *rx_fifo = s->data.rx_fifo;
136   u32 thread_index = vlib_get_thread_index ();
137   int n_read, i;
138
139   if (ecm->test_bytes)
140     {
141       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]));
147     }
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             {
177               if (ecm->rx_buf[thread_index][i]
178                   != ((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,
182                                 ecm->rx_buf[thread_index][i],
183                                 ((s->bytes_received + i) & 0xff));
184                   ecm->test_failed = 1;
185                 }
186             }
187         }
188       ASSERT (n_read <= s->bytes_to_receive);
189       s->bytes_to_receive -= n_read;
190       s->bytes_received += n_read;
191     }
192 }
193
194 static uword
195 echo_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 ();
200   eclient_session_t *sp;
201   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
211   if ((ecm->run_test == 0) ||
212       ((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         {
260           delete_session = 0;
261         }
262       if (PREDICT_FALSE (delete_session == 1))
263         {
264           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);
268           s = session_get_from_handle_if_valid (sp->vpp_session_handle);
269
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
282             {
283               clib_warning ("session AWOL?");
284               vec_delete (connections_this_batch, 1, i);
285             }
286
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* */
302 VLIB_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
311 static int
312 create_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
324 static int
325 echo_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
337   /* Init test data. Big buffer */
338   vec_validate (ecm->connect_test_data, 4 * 1024 * 1024 - 1);
339   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
355 static int
356 echo_clients_session_connected_callback (u32 app_index, u32 api_context,
357                                          stream_session_t * s, u8 is_fail)
358 {
359   echo_client_main_t *ecm = &echo_client_main;
360   eclient_session_t *session;
361   u32 session_index;
362   u8 thread_index = s->thread_index;
363
364   if (is_fail)
365     {
366       clib_warning ("connection %d failed!", api_context);
367       signal_evt_to_cli (-1);
368       return 0;
369     }
370
371   ASSERT (thread_index == vlib_get_thread_index ()
372           || session_transport_service_type (s) == TRANSPORT_SERVICE_CL);
373
374   if (!ecm->vpp_event_queue[thread_index])
375     ecm->vpp_event_queue[thread_index] =
376       session_manager_get_vpp_event_queue (thread_index);
377
378   /*
379    * Setup session
380    */
381   clib_spinlock_lock_if_init (&ecm->sessions_lock);
382   pool_get (ecm->sessions, session);
383   clib_spinlock_unlock_if_init (&ecm->sessions_lock);
384
385   memset (session, 0, sizeof (*session));
386   session_index = session - ecm->sessions;
387   session->bytes_to_send = ecm->bytes_to_send;
388   session->bytes_to_receive = ecm->no_return ? 0ULL : ecm->bytes_to_send;
389   session->data.rx_fifo = s->server_rx_fifo;
390   session->data.rx_fifo->client_session_index = session_index;
391   session->data.tx_fifo = s->server_tx_fifo;
392   session->data.tx_fifo->client_session_index = session_index;
393   session->data.vpp_evt_q = ecm->vpp_event_queue[thread_index];
394   session->vpp_session_handle = session_handle (s);
395
396   if (ecm->is_dgram)
397     {
398       transport_connection_t *tc;
399       tc = session_get_transport (s);
400       clib_memcpy (&session->data.transport, tc,
401                    sizeof (session->data.transport));
402       session->data.is_dgram = 1;
403     }
404
405   vec_add1 (ecm->connection_index_by_thread[thread_index], session_index);
406   __sync_fetch_and_add (&ecm->ready_connections, 1);
407   if (ecm->ready_connections == ecm->expected_connections)
408     {
409       ecm->run_test = 1;
410       /* Signal the CLI process that the action is starting... */
411       signal_evt_to_cli (1);
412     }
413
414   return 0;
415 }
416
417 static void
418 echo_clients_session_reset_callback (stream_session_t * s)
419 {
420   if (s->session_state == SESSION_STATE_READY)
421     clib_warning ("Reset active connection %U", format_stream_session, s, 2);
422   stream_session_cleanup (s);
423   return;
424 }
425
426 static int
427 echo_clients_session_create_callback (stream_session_t * s)
428 {
429   return 0;
430 }
431
432 static void
433 echo_clients_session_disconnect_callback (stream_session_t * s)
434 {
435   echo_client_main_t *ecm = &echo_client_main;
436   vnet_disconnect_args_t _a, *a = &_a;
437   a->handle = session_handle (s);
438   a->app_index = ecm->app_index;
439   vnet_disconnect_session (a);
440   return;
441 }
442
443 static int
444 echo_clients_rx_callback (stream_session_t * s)
445 {
446   echo_client_main_t *ecm = &echo_client_main;
447   eclient_session_t *sp;
448
449   sp = pool_elt_at_index (ecm->sessions,
450                           s->server_rx_fifo->client_session_index);
451   receive_data_chunk (ecm, sp);
452
453   if (svm_fifo_max_dequeue (s->server_rx_fifo))
454     {
455       if (svm_fifo_set_event (s->server_rx_fifo))
456         session_send_io_evt_to_thread (s->server_rx_fifo,
457                                        FIFO_EVENT_BUILTIN_RX);
458     }
459   return 0;
460 }
461
462 int
463 echo_client_add_segment_callback (u32 client_index, const ssvm_private_t * sp)
464 {
465   /* New heaps may be added */
466   return 0;
467 }
468
469 /* *INDENT-OFF* */
470 static session_cb_vft_t echo_clients = {
471   .session_reset_callback = echo_clients_session_reset_callback,
472   .session_connected_callback = echo_clients_session_connected_callback,
473   .session_accept_callback = echo_clients_session_create_callback,
474   .session_disconnect_callback = echo_clients_session_disconnect_callback,
475   .builtin_app_rx_callback = echo_clients_rx_callback,
476   .add_segment_callback = echo_client_add_segment_callback
477 };
478 /* *INDENT-ON* */
479
480 static clib_error_t *
481 echo_clients_attach (u8 * appns_id, u64 appns_flags, u64 appns_secret)
482 {
483   u32 prealloc_fifos, segment_size = 256 << 20;
484   echo_client_main_t *ecm = &echo_client_main;
485   vnet_app_attach_args_t _a, *a = &_a;
486   u64 options[16];
487   clib_error_t *error = 0;
488
489   memset (a, 0, sizeof (*a));
490   memset (options, 0, sizeof (options));
491
492   a->api_client_index = ecm->my_client_index;
493   a->session_cb_vft = &echo_clients;
494
495   prealloc_fifos = ecm->prealloc_fifos ? ecm->expected_connections : 1;
496
497   if (ecm->private_segment_size)
498     segment_size = ecm->private_segment_size;
499
500   options[APP_OPTIONS_ACCEPT_COOKIE] = 0x12345678;
501   options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
502   options[APP_OPTIONS_ADD_SEGMENT_SIZE] = segment_size;
503   options[APP_OPTIONS_RX_FIFO_SIZE] = ecm->fifo_size;
504   options[APP_OPTIONS_TX_FIFO_SIZE] = ecm->fifo_size;
505   options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = ecm->private_segment_count;
506   options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = prealloc_fifos;
507   options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
508   options[APP_OPTIONS_TLS_ENGINE] = ecm->tls_engine;
509   if (appns_id)
510     {
511       options[APP_OPTIONS_FLAGS] |= appns_flags;
512       options[APP_OPTIONS_NAMESPACE_SECRET] = appns_secret;
513     }
514   a->options = options;
515   a->namespace_id = appns_id;
516
517   if ((error = vnet_application_attach (a)))
518     return error;
519
520   ecm->app_index = a->app_index;
521   return 0;
522 }
523
524 static int
525 echo_clients_detach ()
526 {
527   echo_client_main_t *ecm = &echo_client_main;
528   vnet_app_detach_args_t _da, *da = &_da;
529   int rv;
530
531   da->app_index = ecm->app_index;
532   rv = vnet_application_detach (da);
533   ecm->test_client_attached = 0;
534   ecm->app_index = ~0;
535   return rv;
536 }
537
538 static void *
539 echo_client_thread_fn (void *arg)
540 {
541   return 0;
542 }
543
544 /** Start a transmit thread */
545 int
546 echo_clients_start_tx_pthread (echo_client_main_t * ecm)
547 {
548   if (ecm->client_thread_handle == 0)
549     {
550       int rv = pthread_create (&ecm->client_thread_handle,
551                                NULL /*attr */ ,
552                                echo_client_thread_fn, 0);
553       if (rv)
554         {
555           ecm->client_thread_handle = 0;
556           return -1;
557         }
558     }
559   return 0;
560 }
561
562 clib_error_t *
563 echo_clients_connect (vlib_main_t * vm, u32 n_clients)
564 {
565   echo_client_main_t *ecm = &echo_client_main;
566   vnet_connect_args_t _a, *a = &_a;
567   clib_error_t *error = 0;
568   int i;
569
570   memset (a, 0, sizeof (*a));
571   for (i = 0; i < n_clients; i++)
572     {
573       a->uri = (char *) ecm->connect_uri;
574       a->api_context = i;
575       a->app_index = ecm->app_index;
576
577       if ((error = vnet_connect_uri (a)))
578         return error;
579
580       /* Crude pacing for call setups  */
581       if ((i % 4) == 0)
582         vlib_process_suspend (vm, 10e-6);
583       ASSERT (i + 1 >= ecm->ready_connections);
584       while (i + 1 - ecm->ready_connections > 1000)
585         {
586           vlib_process_suspend (vm, 100e-6);
587         }
588     }
589   return 0;
590 }
591
592 #define ec_cli_output(_fmt, _args...)                   \
593   if (!ecm->no_output)                                  \
594     vlib_cli_output(vm, _fmt, ##_args)
595
596 static clib_error_t *
597 echo_clients_command_fn (vlib_main_t * vm,
598                          unformat_input_t * input, vlib_cli_command_t * cmd)
599 {
600   echo_client_main_t *ecm = &echo_client_main;
601   vlib_thread_main_t *thread_main = vlib_get_thread_main ();
602   u64 tmp, total_bytes, appns_flags = 0, appns_secret = 0;
603   f64 test_timeout = 20.0, syn_timeout = 20.0, delta;
604   char *default_uri = "tcp://6.0.1.1/1234";
605   uword *event_data = 0, event_type;
606   f64 time_before_connects;
607   u32 n_clients = 1;
608   int preallocate_sessions = 0;
609   char *transfer_type;
610   clib_error_t *error = 0;
611   u8 *appns_id = 0;
612   int i;
613
614   ecm->bytes_to_send = 8192;
615   ecm->no_return = 0;
616   ecm->fifo_size = 64 << 10;
617   ecm->connections_per_batch = 1000;
618   ecm->private_segment_count = 0;
619   ecm->private_segment_size = 0;
620   ecm->no_output = 0;
621   ecm->test_bytes = 0;
622   ecm->test_failed = 0;
623   ecm->vlib_main = vm;
624   ecm->tls_engine = TLS_ENGINE_OPENSSL;
625   ecm->no_copy = 0;
626
627   if (thread_main->n_vlib_mains > 1)
628     clib_spinlock_init (&ecm->sessions_lock);
629   vec_free (ecm->connect_uri);
630
631   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
632     {
633       if (unformat (input, "uri %s", &ecm->connect_uri))
634         ;
635       else if (unformat (input, "nclients %d", &n_clients))
636         ;
637       else if (unformat (input, "mbytes %lld", &tmp))
638         ecm->bytes_to_send = tmp << 20;
639       else if (unformat (input, "gbytes %lld", &tmp))
640         ecm->bytes_to_send = tmp << 30;
641       else if (unformat (input, "bytes %lld", &ecm->bytes_to_send))
642         ;
643       else if (unformat (input, "test-timeout %f", &test_timeout))
644         ;
645       else if (unformat (input, "syn-timeout %f", &syn_timeout))
646         ;
647       else if (unformat (input, "no-return"))
648         ecm->no_return = 1;
649       else if (unformat (input, "fifo-size %d", &ecm->fifo_size))
650         ecm->fifo_size <<= 10;
651       else if (unformat (input, "private-segment-count %d",
652                          &ecm->private_segment_count))
653         ;
654       else if (unformat (input, "private-segment-size %U",
655                          unformat_memory_size, &tmp))
656         {
657           if (tmp >= 0x100000000ULL)
658             return clib_error_return
659               (0, "private segment size %lld (%llu) too large", tmp, tmp);
660           ecm->private_segment_size = tmp;
661         }
662       else if (unformat (input, "preallocate-fifos"))
663         ecm->prealloc_fifos = 1;
664       else if (unformat (input, "preallocate-sessions"))
665         preallocate_sessions = 1;
666       else
667         if (unformat (input, "client-batch %d", &ecm->connections_per_batch))
668         ;
669       else if (unformat (input, "appns %_%v%_", &appns_id))
670         ;
671       else if (unformat (input, "all-scope"))
672         appns_flags |= (APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE
673                         | APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE);
674       else if (unformat (input, "local-scope"))
675         appns_flags = APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
676       else if (unformat (input, "global-scope"))
677         appns_flags = APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
678       else if (unformat (input, "secret %lu", &appns_secret))
679         ;
680       else if (unformat (input, "no-output"))
681         ecm->no_output = 1;
682       else if (unformat (input, "test-bytes"))
683         ecm->test_bytes = 1;
684       else if (unformat (input, "tls-engine %d", &ecm->tls_engine))
685         ;
686       else
687         return clib_error_return (0, "failed: unknown input `%U'",
688                                   format_unformat_error, input);
689     }
690
691   /* Store cli process node index for signalling */
692   ecm->cli_node_index =
693     vlib_get_current_process (vm)->node_runtime.node_index;
694
695   if (ecm->is_init == 0)
696     {
697       if (echo_clients_init (vm))
698         return clib_error_return (0, "failed init");
699     }
700
701
702   ecm->ready_connections = 0;
703   ecm->expected_connections = n_clients;
704   ecm->rx_total = 0;
705   ecm->tx_total = 0;
706
707   if (!ecm->connect_uri)
708     {
709       clib_warning ("No uri provided. Using default: %s", default_uri);
710       ecm->connect_uri = format (0, "%s%c", default_uri, 0);
711     }
712
713   if (ecm->connect_uri[0] == 'u' && ecm->connect_uri[3] != 'c')
714     ecm->is_dgram = 1;
715
716 #if ECHO_CLIENT_PTHREAD
717   echo_clients_start_tx_pthread ();
718 #endif
719
720   vlib_worker_thread_barrier_sync (vm);
721   vnet_session_enable_disable (vm, 1 /* turn on session and transports */ );
722   vlib_worker_thread_barrier_release (vm);
723
724   if (ecm->test_client_attached == 0)
725     {
726       if ((error = echo_clients_attach (appns_id, appns_flags, appns_secret)))
727         {
728           vec_free (appns_id);
729           clib_error_report (error);
730           return error;
731         }
732       vec_free (appns_id);
733     }
734   ecm->test_client_attached = 1;
735
736   /* Turn on the builtin client input nodes */
737   for (i = 0; i < thread_main->n_vlib_mains; i++)
738     vlib_node_set_state (vlib_mains[i], echo_clients_node.index,
739                          VLIB_NODE_STATE_POLLING);
740
741   if (preallocate_sessions)
742     pool_init_fixed (ecm->sessions, 1.1 * n_clients);
743
744   /* Fire off connect requests */
745   time_before_connects = vlib_time_now (vm);
746   if ((error = echo_clients_connect (vm, n_clients)))
747     return error;
748
749   /* Park until the sessions come up, or ten seconds elapse... */
750   vlib_process_wait_for_event_or_clock (vm, syn_timeout);
751   event_type = vlib_process_get_events (vm, &event_data);
752   switch (event_type)
753     {
754     case ~0:
755       ec_cli_output ("Timeout with only %d sessions active...",
756                      ecm->ready_connections);
757       error = clib_error_return (0, "failed: syn timeout with %d sessions",
758                                  ecm->ready_connections);
759       goto cleanup;
760
761     case 1:
762       delta = vlib_time_now (vm) - time_before_connects;
763       if (delta != 0.0)
764         ec_cli_output ("%d three-way handshakes in %.2f seconds %.2f/s",
765                        n_clients, delta, ((f64) n_clients) / delta);
766
767       ecm->test_start_time = vlib_time_now (ecm->vlib_main);
768       ec_cli_output ("Test started at %.6f", ecm->test_start_time);
769       break;
770
771     default:
772       ec_cli_output ("unexpected event(1): %d", event_type);
773       error = clib_error_return (0, "failed: unexpected event(1): %d",
774                                  event_type);
775       goto cleanup;
776     }
777
778   /* Now wait for the sessions to finish... */
779   vlib_process_wait_for_event_or_clock (vm, test_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 %d sessions still active...",
785                      ecm->ready_connections);
786       error = clib_error_return (0, "failed: timeout with %d sessions",
787                                  ecm->ready_connections);
788       goto cleanup;
789
790     case 2:
791       ecm->test_end_time = vlib_time_now (vm);
792       ec_cli_output ("Test finished at %.6f", ecm->test_end_time);
793       break;
794
795     default:
796       ec_cli_output ("unexpected event(2): %d", event_type);
797       error = clib_error_return (0, "failed: unexpected event(2): %d",
798                                  event_type);
799       goto cleanup;
800     }
801
802   delta = ecm->test_end_time - ecm->test_start_time;
803   if (delta != 0.0)
804     {
805       total_bytes = (ecm->no_return ? ecm->tx_total : ecm->rx_total);
806       transfer_type = ecm->no_return ? "half-duplex" : "full-duplex";
807       ec_cli_output ("%lld bytes (%lld mbytes, %lld gbytes) in %.2f seconds",
808                      total_bytes, total_bytes / (1ULL << 20),
809                      total_bytes / (1ULL << 30), delta);
810       ec_cli_output ("%.2f bytes/second %s", ((f64) total_bytes) / (delta),
811                      transfer_type);
812       ec_cli_output ("%.4f gbit/second %s",
813                      (((f64) total_bytes * 8.0) / delta / 1e9),
814                      transfer_type);
815     }
816   else
817     {
818       ec_cli_output ("zero delta-t?");
819       error = clib_error_return (0, "failed: zero delta-t");
820       goto cleanup;
821     }
822
823   if (ecm->test_bytes && ecm->test_failed)
824     error = clib_error_return (0, "failed: test bytes");
825
826 cleanup:
827   ecm->run_test = 0;
828   for (i = 0; i < vec_len (ecm->connection_index_by_thread); i++)
829     {
830       vec_reset_length (ecm->connection_index_by_thread[i]);
831       vec_reset_length (ecm->connections_this_batch_by_thread[i]);
832     }
833
834   pool_free (ecm->sessions);
835
836   /* Detach the application, so we can use different fifo sizes next time */
837   if (ecm->test_client_attached)
838     {
839       if (echo_clients_detach ())
840         {
841           error = clib_error_return (0, "failed: app detach");
842           ec_cli_output ("WARNING: app detach failed...");
843         }
844     }
845   if (error)
846     ec_cli_output ("test failed");
847   vec_free (ecm->connect_uri);
848   return error;
849 }
850
851 /* *INDENT-OFF* */
852 VLIB_CLI_COMMAND (echo_clients_command, static) =
853 {
854   .path = "test echo clients",
855   .short_help = "test echo clients [nclients %d][[m|g]bytes <bytes>]"
856       "[test-timeout <time>][syn-timeout <time>][no-return][fifo-size <size>]"
857       "[private-segment-count <count>][private-segment-size <bytes>[m|g]]"
858       "[preallocate-fifos][preallocate-sessions][client-batch <batch-size>]"
859       "[uri <tcp://ip/port>][test-bytes][no-output]",
860   .function = echo_clients_command_fn,
861   .is_mp_safe = 1,
862 };
863 /* *INDENT-ON* */
864
865 clib_error_t *
866 echo_clients_main_init (vlib_main_t * vm)
867 {
868   echo_client_main_t *ecm = &echo_client_main;
869   ecm->is_init = 0;
870   return 0;
871 }
872
873 VLIB_INIT_FUNCTION (echo_clients_main_init);
874
875 /*
876  * fd.io coding-style-patch-verification: ON
877  *
878  * Local Variables:
879  * eval: (c-set-style "gnu")
880  * End:
881  */