udp/session: refactor to support dgram mode
[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, 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   ASSERT (vec_len (test_data) > 0);
53   test_buf_len = vec_len (test_data);
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     rv = app_send_stream (&s->data, test_data + test_buf_offset,
60                           bytes_this_chunk, 0);
61   else
62     rv = app_send_dgram (&s->data, test_data + test_buf_offset,
63                          bytes_this_chunk, 0);
64
65   /* If we managed to enqueue data... */
66   if (rv > 0)
67     {
68       /* Account for it... */
69       s->bytes_to_send -= rv;
70       s->bytes_sent += rv;
71
72       if (ECHO_CLIENT_DBG)
73         {
74           /* *INDENT-OFF* */
75           ELOG_TYPE_DECLARE (e) =
76             {
77               .format = "tx-enq: xfer %d bytes, sent %u remain %u",
78               .format_args = "i4i4i4",
79             };
80           /* *INDENT-ON* */
81           struct
82           {
83             u32 data[3];
84           } *ed;
85           ed = ELOG_DATA (&vlib_global_main.elog_main, e);
86           ed->data[0] = rv;
87           ed->data[1] = s->bytes_sent;
88           ed->data[2] = s->bytes_to_send;
89         }
90     }
91 }
92
93 static void
94 receive_data_chunk (echo_client_main_t * ecm, session_t * s)
95 {
96   svm_fifo_t *rx_fifo = s->data.rx_fifo;
97   u32 thread_index = vlib_get_thread_index ();
98   int n_read, i;
99
100   if (ecm->test_bytes)
101     {
102       if (!ecm->is_dgram)
103         n_read = app_recv_stream (&s->data, ecm->rx_buf[thread_index],
104                                   vec_len (ecm->rx_buf[thread_index]));
105       else
106         n_read = app_recv_dgram (&s->data, ecm->rx_buf[thread_index],
107                                  vec_len (ecm->rx_buf[thread_index]));
108     }
109   else
110     {
111       n_read = svm_fifo_max_dequeue (rx_fifo);
112       svm_fifo_dequeue_drop (rx_fifo, n_read);
113     }
114
115   if (n_read > 0)
116     {
117       if (ECHO_CLIENT_DBG)
118         {
119           /* *INDENT-OFF* */
120           ELOG_TYPE_DECLARE (e) =
121             {
122               .format = "rx-deq: %d bytes",
123               .format_args = "i4",
124             };
125           /* *INDENT-ON* */
126           struct
127           {
128             u32 data[1];
129           } *ed;
130           ed = ELOG_DATA (&vlib_global_main.elog_main, e);
131           ed->data[0] = n_read;
132         }
133
134       if (ecm->test_bytes)
135         {
136           for (i = 0; i < n_read; i++)
137             {
138               if (ecm->rx_buf[thread_index][i]
139                   != ((s->bytes_received + i) & 0xff))
140                 {
141                   clib_warning ("read %d error at byte %lld, 0x%x not 0x%x",
142                                 n_read, s->bytes_received + i,
143                                 ecm->rx_buf[thread_index][i],
144                                 ((s->bytes_received + i) & 0xff));
145                   ecm->test_failed = 1;
146                 }
147             }
148         }
149       ASSERT (n_read <= s->bytes_to_receive);
150       s->bytes_to_receive -= n_read;
151       s->bytes_received += n_read;
152     }
153 }
154
155 static uword
156 echo_client_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
157                      vlib_frame_t * frame)
158 {
159   echo_client_main_t *ecm = &echo_client_main;
160   int my_thread_index = vlib_get_thread_index ();
161   session_t *sp;
162   int i;
163   int delete_session;
164   u32 *connection_indices;
165   u32 *connections_this_batch;
166   u32 nconnections_this_batch;
167
168   connection_indices = ecm->connection_index_by_thread[my_thread_index];
169   connections_this_batch =
170     ecm->connections_this_batch_by_thread[my_thread_index];
171
172   if ((ecm->run_test == 0) ||
173       ((vec_len (connection_indices) == 0)
174        && vec_len (connections_this_batch) == 0))
175     return 0;
176
177   /* Grab another pile of connections */
178   if (PREDICT_FALSE (vec_len (connections_this_batch) == 0))
179     {
180       nconnections_this_batch =
181         clib_min (ecm->connections_per_batch, vec_len (connection_indices));
182
183       ASSERT (nconnections_this_batch > 0);
184       vec_validate (connections_this_batch, nconnections_this_batch - 1);
185       clib_memcpy (connections_this_batch,
186                    connection_indices + vec_len (connection_indices)
187                    - nconnections_this_batch,
188                    nconnections_this_batch * sizeof (u32));
189       _vec_len (connection_indices) -= nconnections_this_batch;
190     }
191
192   if (PREDICT_FALSE (ecm->prev_conns != ecm->connections_per_batch
193                      && ecm->prev_conns == vec_len (connections_this_batch)))
194     {
195       ecm->repeats++;
196       ecm->prev_conns = vec_len (connections_this_batch);
197       if (ecm->repeats == 500000)
198         {
199           clib_warning ("stuck clients");
200         }
201     }
202   else
203     {
204       ecm->prev_conns = vec_len (connections_this_batch);
205       ecm->repeats = 0;
206     }
207
208   for (i = 0; i < vec_len (connections_this_batch); i++)
209     {
210       delete_session = 1;
211
212       sp = pool_elt_at_index (ecm->sessions, connections_this_batch[i]);
213
214       if (sp->bytes_to_send > 0)
215         {
216           send_data_chunk (ecm, sp);
217           delete_session = 0;
218         }
219       if (sp->bytes_to_receive > 0)
220         {
221           delete_session = 0;
222         }
223       if (PREDICT_FALSE (delete_session == 1))
224         {
225           stream_session_t *s;
226
227           __sync_fetch_and_add (&ecm->tx_total, sp->bytes_sent);
228           __sync_fetch_and_add (&ecm->rx_total, sp->bytes_received);
229           s = session_get_from_handle_if_valid (sp->vpp_session_handle);
230
231           if (s)
232             {
233               vnet_disconnect_args_t _a, *a = &_a;
234               a->handle = session_handle (s);
235               a->app_index = ecm->app_index;
236               vnet_disconnect_session (a);
237
238               vec_delete (connections_this_batch, 1, i);
239               i--;
240               __sync_fetch_and_add (&ecm->ready_connections, -1);
241             }
242           else
243             {
244               clib_warning ("session AWOL?");
245               vec_delete (connections_this_batch, 1, i);
246             }
247
248           /* Kick the debug CLI process */
249           if (ecm->ready_connections == 0)
250             {
251               signal_evt_to_cli (2);
252             }
253         }
254     }
255
256   ecm->connection_index_by_thread[my_thread_index] = connection_indices;
257   ecm->connections_this_batch_by_thread[my_thread_index] =
258     connections_this_batch;
259   return 0;
260 }
261
262 /* *INDENT-OFF* */
263 VLIB_REGISTER_NODE (echo_clients_node) =
264 {
265   .function = echo_client_node_fn,
266   .name = "echo-clients",
267   .type = VLIB_NODE_TYPE_INPUT,
268   .state = VLIB_NODE_STATE_DISABLED,
269 };
270 /* *INDENT-ON* */
271
272 static int
273 create_api_loopback (echo_client_main_t * ecm)
274 {
275   api_main_t *am = &api_main;
276   vl_shmem_hdr_t *shmem_hdr;
277
278   shmem_hdr = am->shmem_hdr;
279   ecm->vl_input_queue = shmem_hdr->vl_input_queue;
280   ecm->my_client_index = vl_api_memclnt_create_internal ("echo_client",
281                                                          ecm->vl_input_queue);
282   return 0;
283 }
284
285 static int
286 echo_clients_init (vlib_main_t * vm)
287 {
288   echo_client_main_t *ecm = &echo_client_main;
289   vlib_thread_main_t *vtm = vlib_get_thread_main ();
290   u32 num_threads;
291   int i;
292
293   if (create_api_loopback (ecm))
294     return -1;
295
296   num_threads = 1 /* main thread */  + vtm->n_threads;
297
298   /* Init test data. Big buffer */
299   vec_validate (ecm->connect_test_data, 4 * 1024 * 1024 - 1);
300   for (i = 0; i < vec_len (ecm->connect_test_data); i++)
301     ecm->connect_test_data[i] = i & 0xff;
302
303   vec_validate (ecm->rx_buf, num_threads - 1);
304   for (i = 0; i < num_threads; i++)
305     vec_validate (ecm->rx_buf[i], vec_len (ecm->connect_test_data) - 1);
306
307   ecm->is_init = 1;
308
309   vec_validate (ecm->connection_index_by_thread, vtm->n_vlib_mains);
310   vec_validate (ecm->connections_this_batch_by_thread, vtm->n_vlib_mains);
311   vec_validate (ecm->vpp_event_queue, vtm->n_vlib_mains);
312
313   return 0;
314 }
315
316 static int
317 echo_clients_session_connected_callback (u32 app_index, u32 api_context,
318                                          stream_session_t * s, u8 is_fail)
319 {
320   echo_client_main_t *ecm = &echo_client_main;
321   session_t *session;
322   u32 session_index;
323   u8 thread_index = vlib_get_thread_index ();
324
325   if (is_fail)
326     {
327       clib_warning ("connection %d failed!", api_context);
328       signal_evt_to_cli (-1);
329       return 0;
330     }
331
332   ASSERT (s->thread_index == thread_index);
333
334   if (!ecm->vpp_event_queue[thread_index])
335     ecm->vpp_event_queue[thread_index] =
336       session_manager_get_vpp_event_queue (thread_index);
337
338   /*
339    * Setup session
340    */
341   clib_spinlock_lock_if_init (&ecm->sessions_lock);
342   pool_get (ecm->sessions, session);
343   clib_spinlock_unlock_if_init (&ecm->sessions_lock);
344
345   memset (session, 0, sizeof (*session));
346   session_index = session - ecm->sessions;
347   session->bytes_to_send = ecm->bytes_to_send;
348   session->bytes_to_receive = ecm->no_return ? 0ULL : ecm->bytes_to_send;
349   session->data.rx_fifo = s->server_rx_fifo;
350   session->data.rx_fifo->client_session_index = session_index;
351   session->data.tx_fifo = s->server_tx_fifo;
352   session->data.tx_fifo->client_session_index = session_index;
353   session->data.vpp_evt_q = ecm->vpp_event_queue[thread_index];
354   session->vpp_session_handle = session_handle (s);
355
356   if (ecm->is_dgram)
357     {
358       transport_connection_t *tc;
359       tc = session_get_transport (s);
360       clib_memcpy (&session->data.transport, tc,
361                    sizeof (session->data.transport));
362       session->data.is_dgram = 1;
363     }
364
365   vec_add1 (ecm->connection_index_by_thread[thread_index], session_index);
366   __sync_fetch_and_add (&ecm->ready_connections, 1);
367   if (ecm->ready_connections == ecm->expected_connections)
368     {
369       ecm->run_test = 1;
370       /* Signal the CLI process that the action is starting... */
371       signal_evt_to_cli (1);
372     }
373
374   return 0;
375 }
376
377 static void
378 echo_clients_session_reset_callback (stream_session_t * s)
379 {
380   if (s->session_state == SESSION_STATE_READY)
381     clib_warning ("Reset active connection %U", format_stream_session, s, 2);
382   stream_session_cleanup (s);
383   return;
384 }
385
386 static int
387 echo_clients_session_create_callback (stream_session_t * s)
388 {
389   return 0;
390 }
391
392 static void
393 echo_clients_session_disconnect_callback (stream_session_t * s)
394 {
395   echo_client_main_t *ecm = &echo_client_main;
396   vnet_disconnect_args_t _a, *a = &_a;
397   a->handle = session_handle (s);
398   a->app_index = ecm->app_index;
399   vnet_disconnect_session (a);
400   return;
401 }
402
403 static int
404 echo_clients_rx_callback (stream_session_t * s)
405 {
406   echo_client_main_t *ecm = &echo_client_main;
407   session_t *sp;
408
409   sp = pool_elt_at_index (ecm->sessions,
410                           s->server_rx_fifo->client_session_index);
411   receive_data_chunk (ecm, sp);
412
413   if (svm_fifo_max_dequeue (s->server_rx_fifo))
414     {
415       session_fifo_event_t evt;
416       svm_queue_t *q;
417       if (svm_fifo_set_event (s->server_rx_fifo))
418         {
419           evt.fifo = s->server_rx_fifo;
420           evt.event_type = FIFO_EVENT_BUILTIN_RX;
421           q = session_manager_get_vpp_event_queue (s->thread_index);
422           if (PREDICT_FALSE (q->cursize == q->maxsize))
423             clib_warning ("out of event queue space");
424           else if (svm_queue_add (q, (u8 *) & evt, 0))
425             clib_warning ("failed to enqueue self-tap");
426         }
427     }
428   return 0;
429 }
430
431 int
432 echo_client_add_segment_callback (u32 client_index, const ssvm_private_t * sp)
433 {
434   /* New heaps may be added */
435   return 0;
436 }
437
438 /* *INDENT-OFF* */
439 static session_cb_vft_t echo_clients = {
440   .session_reset_callback = echo_clients_session_reset_callback,
441   .session_connected_callback = echo_clients_session_connected_callback,
442   .session_accept_callback = echo_clients_session_create_callback,
443   .session_disconnect_callback = echo_clients_session_disconnect_callback,
444   .builtin_app_rx_callback = echo_clients_rx_callback,
445   .add_segment_callback = echo_client_add_segment_callback
446 };
447 /* *INDENT-ON* */
448
449 static clib_error_t *
450 echo_clients_attach (u8 * appns_id, u64 appns_flags, u64 appns_secret)
451 {
452   u32 prealloc_fifos, segment_size = 256 << 20;
453   echo_client_main_t *ecm = &echo_client_main;
454   vnet_app_attach_args_t _a, *a = &_a;
455   u64 options[16];
456   clib_error_t *error = 0;
457
458   memset (a, 0, sizeof (*a));
459   memset (options, 0, sizeof (options));
460
461   a->api_client_index = ecm->my_client_index;
462   a->session_cb_vft = &echo_clients;
463
464   prealloc_fifos = ecm->prealloc_fifos ? ecm->expected_connections : 1;
465
466   if (ecm->private_segment_size)
467     segment_size = ecm->private_segment_size;
468
469   options[APP_OPTIONS_ACCEPT_COOKIE] = 0x12345678;
470   options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
471   options[APP_OPTIONS_ADD_SEGMENT_SIZE] = segment_size;
472   options[APP_OPTIONS_RX_FIFO_SIZE] = ecm->fifo_size;
473   options[APP_OPTIONS_TX_FIFO_SIZE] = ecm->fifo_size;
474   options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = ecm->private_segment_count;
475   options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = prealloc_fifos;
476   options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
477   options[APP_OPTIONS_TLS_ENGINE] = ecm->tls_engine;
478   if (appns_id)
479     {
480       options[APP_OPTIONS_FLAGS] |= appns_flags;
481       options[APP_OPTIONS_NAMESPACE_SECRET] = appns_secret;
482     }
483   a->options = options;
484   a->namespace_id = appns_id;
485
486   if ((error = vnet_application_attach (a)))
487     return error;
488
489   ecm->app_index = a->app_index;
490   return 0;
491 }
492
493 static int
494 echo_clients_detach ()
495 {
496   echo_client_main_t *ecm = &echo_client_main;
497   vnet_app_detach_args_t _da, *da = &_da;
498   int rv;
499
500   da->app_index = ecm->app_index;
501   rv = vnet_application_detach (da);
502   ecm->test_client_attached = 0;
503   ecm->app_index = ~0;
504   return rv;
505 }
506
507 static void *
508 echo_client_thread_fn (void *arg)
509 {
510   return 0;
511 }
512
513 /** Start a transmit thread */
514 int
515 echo_clients_start_tx_pthread (echo_client_main_t * ecm)
516 {
517   if (ecm->client_thread_handle == 0)
518     {
519       int rv = pthread_create (&ecm->client_thread_handle,
520                                NULL /*attr */ ,
521                                echo_client_thread_fn, 0);
522       if (rv)
523         {
524           ecm->client_thread_handle = 0;
525           return -1;
526         }
527     }
528   return 0;
529 }
530
531 clib_error_t *
532 echo_clients_connect (vlib_main_t * vm, u32 n_clients)
533 {
534   echo_client_main_t *ecm = &echo_client_main;
535   vnet_connect_args_t _a, *a = &_a;
536   clib_error_t *error = 0;
537   int i;
538
539   memset (a, 0, sizeof (*a));
540   for (i = 0; i < n_clients; i++)
541     {
542       a->uri = (char *) ecm->connect_uri;
543       a->api_context = i;
544       a->app_index = ecm->app_index;
545
546       if ((error = vnet_connect_uri (a)))
547         return error;
548
549       /* Crude pacing for call setups  */
550       if ((i % 4) == 0)
551         vlib_process_suspend (vm, 10e-6);
552       ASSERT (i + 1 >= ecm->ready_connections);
553       while (i + 1 - ecm->ready_connections > 1000)
554         {
555           vlib_process_suspend (vm, 100e-6);
556         }
557     }
558   return 0;
559 }
560
561 #define ec_cli_output(_fmt, _args...)                   \
562   if (!ecm->no_output)                                  \
563     vlib_cli_output(vm, _fmt, ##_args)
564
565 static clib_error_t *
566 echo_clients_command_fn (vlib_main_t * vm,
567                          unformat_input_t * input, vlib_cli_command_t * cmd)
568 {
569   echo_client_main_t *ecm = &echo_client_main;
570   vlib_thread_main_t *thread_main = vlib_get_thread_main ();
571   u64 tmp, total_bytes, appns_flags = 0, appns_secret = 0;
572   f64 test_timeout = 20.0, syn_timeout = 20.0, delta;
573   char *default_uri = "tcp://6.0.1.1/1234";
574   uword *event_data = 0, event_type;
575   f64 time_before_connects;
576   u32 n_clients = 1;
577   int preallocate_sessions = 0;
578   char *transfer_type;
579   clib_error_t *error = 0;
580   u8 *appns_id = 0;
581   int i;
582
583   ecm->bytes_to_send = 8192;
584   ecm->no_return = 0;
585   ecm->fifo_size = 64 << 10;
586   ecm->connections_per_batch = 1000;
587   ecm->private_segment_count = 0;
588   ecm->private_segment_size = 0;
589   ecm->no_output = 0;
590   ecm->test_bytes = 0;
591   ecm->test_failed = 0;
592   ecm->vlib_main = vm;
593   ecm->tls_engine = TLS_ENGINE_OPENSSL;
594
595   if (thread_main->n_vlib_mains > 1)
596     clib_spinlock_init (&ecm->sessions_lock);
597   vec_free (ecm->connect_uri);
598
599   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
600     {
601       if (unformat (input, "uri %s", &ecm->connect_uri))
602         ;
603       else if (unformat (input, "nclients %d", &n_clients))
604         ;
605       else if (unformat (input, "mbytes %lld", &tmp))
606         ecm->bytes_to_send = tmp << 20;
607       else if (unformat (input, "gbytes %lld", &tmp))
608         ecm->bytes_to_send = tmp << 30;
609       else if (unformat (input, "bytes %lld", &ecm->bytes_to_send))
610         ;
611       else if (unformat (input, "test-timeout %f", &test_timeout))
612         ;
613       else if (unformat (input, "syn-timeout %f", &syn_timeout))
614         ;
615       else if (unformat (input, "no-return"))
616         ecm->no_return = 1;
617       else if (unformat (input, "fifo-size %d", &ecm->fifo_size))
618         ecm->fifo_size <<= 10;
619       else if (unformat (input, "private-segment-count %d",
620                          &ecm->private_segment_count))
621         ;
622       else if (unformat (input, "private-segment-size %U",
623                          unformat_memory_size, &tmp))
624         {
625           if (tmp >= 0x100000000ULL)
626             return clib_error_return
627               (0, "private segment size %lld (%llu) too large", tmp, tmp);
628           ecm->private_segment_size = tmp;
629         }
630       else if (unformat (input, "preallocate-fifos"))
631         ecm->prealloc_fifos = 1;
632       else if (unformat (input, "preallocate-sessions"))
633         preallocate_sessions = 1;
634       else
635         if (unformat (input, "client-batch %d", &ecm->connections_per_batch))
636         ;
637       else if (unformat (input, "appns %_%v%_", &appns_id))
638         ;
639       else if (unformat (input, "all-scope"))
640         appns_flags |= (APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE
641                         | APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE);
642       else if (unformat (input, "local-scope"))
643         appns_flags = APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
644       else if (unformat (input, "global-scope"))
645         appns_flags = APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
646       else if (unformat (input, "secret %lu", &appns_secret))
647         ;
648       else if (unformat (input, "no-output"))
649         ecm->no_output = 1;
650       else if (unformat (input, "test-bytes"))
651         ecm->test_bytes = 1;
652       else if (unformat (input, "tls-engine %d", &ecm->tls_engine))
653         ;
654       else
655         return clib_error_return (0, "failed: unknown input `%U'",
656                                   format_unformat_error, input);
657     }
658
659   /* Store cli process node index for signalling */
660   ecm->cli_node_index =
661     vlib_get_current_process (vm)->node_runtime.node_index;
662
663   if (ecm->is_init == 0)
664     {
665       if (echo_clients_init (vm))
666         return clib_error_return (0, "failed init");
667     }
668
669
670   ecm->ready_connections = 0;
671   ecm->expected_connections = n_clients;
672   ecm->rx_total = 0;
673   ecm->tx_total = 0;
674
675   if (!ecm->connect_uri)
676     {
677       clib_warning ("No uri provided. Using default: %s", default_uri);
678       ecm->connect_uri = format (0, "%s%c", default_uri, 0);
679     }
680
681   if (ecm->connect_uri[0] == 'u' && ecm->connect_uri[3] != 'c')
682     ecm->is_dgram = 1;
683
684 #if ECHO_CLIENT_PTHREAD
685   echo_clients_start_tx_pthread ();
686 #endif
687
688   vlib_worker_thread_barrier_sync (vm);
689   vnet_session_enable_disable (vm, 1 /* turn on session and transports */ );
690   vlib_worker_thread_barrier_release (vm);
691
692   if (ecm->test_client_attached == 0)
693     {
694       if ((error = echo_clients_attach (appns_id, appns_flags, appns_secret)))
695         {
696           vec_free (appns_id);
697           clib_error_report (error);
698           return error;
699         }
700       vec_free (appns_id);
701     }
702   ecm->test_client_attached = 1;
703
704   /* Turn on the builtin client input nodes */
705   for (i = 0; i < thread_main->n_vlib_mains; i++)
706     vlib_node_set_state (vlib_mains[i], echo_clients_node.index,
707                          VLIB_NODE_STATE_POLLING);
708
709   if (preallocate_sessions)
710     {
711       session_t *sp __attribute__ ((unused));
712       for (i = 0; i < n_clients; i++)
713         pool_get (ecm->sessions, sp);
714       for (i = 0; i < n_clients; i++)
715         pool_put_index (ecm->sessions, i);
716     }
717
718   /* Fire off connect requests */
719   time_before_connects = vlib_time_now (vm);
720   if ((error = echo_clients_connect (vm, n_clients)))
721     return error;
722
723   /* Park until the sessions come up, or ten seconds elapse... */
724   vlib_process_wait_for_event_or_clock (vm, syn_timeout);
725   event_type = vlib_process_get_events (vm, &event_data);
726   switch (event_type)
727     {
728     case ~0:
729       ec_cli_output ("Timeout with only %d sessions active...",
730                      ecm->ready_connections);
731       error = clib_error_return (0, "failed: syn timeout with %d sessions",
732                                  ecm->ready_connections);
733       goto cleanup;
734
735     case 1:
736       delta = vlib_time_now (vm) - time_before_connects;
737       if (delta != 0.0)
738         ec_cli_output ("%d three-way handshakes in %.2f seconds %.2f/s",
739                        n_clients, delta, ((f64) n_clients) / delta);
740
741       ecm->test_start_time = vlib_time_now (ecm->vlib_main);
742       ec_cli_output ("Test started at %.6f", ecm->test_start_time);
743       break;
744
745     default:
746       ec_cli_output ("unexpected event(1): %d", event_type);
747       error = clib_error_return (0, "failed: unexpected event(1): %d",
748                                  event_type);
749       goto cleanup;
750     }
751
752   /* Now wait for the sessions to finish... */
753   vlib_process_wait_for_event_or_clock (vm, test_timeout);
754   event_type = vlib_process_get_events (vm, &event_data);
755   switch (event_type)
756     {
757     case ~0:
758       ec_cli_output ("Timeout with %d sessions still active...",
759                      ecm->ready_connections);
760       error = clib_error_return (0, "failed: timeout with %d sessions",
761                                  ecm->ready_connections);
762       goto cleanup;
763
764     case 2:
765       ecm->test_end_time = vlib_time_now (vm);
766       ec_cli_output ("Test finished at %.6f", ecm->test_end_time);
767       break;
768
769     default:
770       ec_cli_output ("unexpected event(2): %d", event_type);
771       error = clib_error_return (0, "failed: unexpected event(2): %d",
772                                  event_type);
773       goto cleanup;
774     }
775
776   delta = ecm->test_end_time - ecm->test_start_time;
777   if (delta != 0.0)
778     {
779       total_bytes = (ecm->no_return ? ecm->tx_total : ecm->rx_total);
780       transfer_type = ecm->no_return ? "half-duplex" : "full-duplex";
781       ec_cli_output ("%lld bytes (%lld mbytes, %lld gbytes) in %.2f seconds",
782                      total_bytes, total_bytes / (1ULL << 20),
783                      total_bytes / (1ULL << 30), delta);
784       ec_cli_output ("%.2f bytes/second %s", ((f64) total_bytes) / (delta),
785                      transfer_type);
786       ec_cli_output ("%.4f gbit/second %s",
787                      (((f64) total_bytes * 8.0) / delta / 1e9),
788                      transfer_type);
789     }
790   else
791     {
792       ec_cli_output ("zero delta-t?");
793       error = clib_error_return (0, "failed: zero delta-t");
794       goto cleanup;
795     }
796
797   if (ecm->test_bytes && ecm->test_failed)
798     error = clib_error_return (0, "failed: test bytes");
799
800 cleanup:
801   ecm->run_test = 0;
802   for (i = 0; i < vec_len (ecm->connection_index_by_thread); i++)
803     {
804       vec_reset_length (ecm->connection_index_by_thread[i]);
805       vec_reset_length (ecm->connections_this_batch_by_thread[i]);
806     }
807
808   pool_free (ecm->sessions);
809
810   /* Detach the application, so we can use different fifo sizes next time */
811   if (ecm->test_client_attached)
812     {
813       if (echo_clients_detach ())
814         {
815           error = clib_error_return (0, "failed: app detach");
816           ec_cli_output ("WARNING: app detach failed...");
817         }
818     }
819   if (error)
820     ec_cli_output ("test failed");
821   vec_free (ecm->connect_uri);
822   return error;
823 }
824
825 /* *INDENT-OFF* */
826 VLIB_CLI_COMMAND (echo_clients_command, static) =
827 {
828   .path = "test echo clients",
829   .short_help = "test echo clients [nclients %d][[m|g]bytes <bytes>]"
830       "[test-timeout <time>][syn-timeout <time>][no-return][fifo-size <size>]"
831       "[private-segment-count <count>][private-segment-size <bytes>[m|g]]"
832       "[preallocate-fifos][preallocate-sessions][client-batch <batch-size>]"
833       "[uri <tcp://ip/port>][test-bytes][no-output]",
834   .function = echo_clients_command_fn,
835   .is_mp_safe = 1,
836 };
837 /* *INDENT-ON* */
838
839 clib_error_t *
840 echo_clients_main_init (vlib_main_t * vm)
841 {
842   echo_client_main_t *ecm = &echo_client_main;
843   ecm->is_init = 0;
844   return 0;
845 }
846
847 VLIB_INIT_FUNCTION (echo_clients_main_init);
848
849 /*
850  * fd.io coding-style-patch-verification: ON
851  *
852  * Local Variables:
853  * eval: (c-set-style "gnu")
854  * End:
855  */