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