session: add support for memfd segments
[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 prealloc_fifos, segment_size = 2 << 20;
442   tclient_main_t *tm = &tclient_main;
443   vnet_app_attach_args_t _a, *a = &_a;
444   u64 options[16];
445   clib_error_t *error = 0;
446
447   memset (a, 0, sizeof (*a));
448   memset (options, 0, sizeof (options));
449
450   a->api_client_index = tm->my_client_index;
451   a->session_cb_vft = &builtin_clients;
452
453   prealloc_fifos = tm->prealloc_fifos ? tm->expected_connections : 1;
454
455   if (tm->private_segment_size)
456     segment_size = tm->private_segment_size;
457
458   options[APP_OPTIONS_ACCEPT_COOKIE] = 0x12345678;
459   options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
460   options[APP_OPTIONS_RX_FIFO_SIZE] = tm->fifo_size;
461   options[APP_OPTIONS_TX_FIFO_SIZE] = tm->fifo_size;
462   options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = tm->private_segment_count;
463   options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = prealloc_fifos;
464
465   options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
466   if (appns_id)
467     {
468       options[APP_OPTIONS_FLAGS] |= appns_flags;
469       options[APP_OPTIONS_NAMESPACE_SECRET] = appns_secret;
470     }
471   a->options = options;
472   a->namespace_id = appns_id;
473
474   if ((error = vnet_application_attach (a)))
475     return error;
476
477   tm->app_index = a->app_index;
478   return 0;
479 }
480
481 static void *
482 tclient_thread_fn (void *arg)
483 {
484   return 0;
485 }
486
487 /** Start a transmit thread */
488 int
489 start_tx_pthread_sctp (tclient_main_t * tm)
490 {
491   if (tm->client_thread_handle == 0)
492     {
493       int rv = pthread_create (&tm->client_thread_handle,
494                                NULL /*attr */ ,
495                                tclient_thread_fn, 0);
496       if (rv)
497         {
498           tm->client_thread_handle = 0;
499           return -1;
500         }
501     }
502   return 0;
503 }
504
505 clib_error_t *
506 clients_connect_sctp (vlib_main_t * vm, u8 * uri, u32 n_clients)
507 {
508   tclient_main_t *tm = &tclient_main;
509   vnet_connect_args_t _a, *a = &_a;
510   clib_error_t *error = 0;
511   int i;
512   for (i = 0; i < n_clients; i++)
513     {
514       memset (a, 0, sizeof (*a));
515
516       a->uri = (char *) uri;
517       a->api_context = i;
518       a->app_index = tm->app_index;
519       a->mp = 0;
520
521       if ((error = vnet_connect_uri (a)))
522         return error;
523
524
525       /* Crude pacing for call setups  */
526       if ((i % 4) == 0)
527         vlib_process_suspend (vm, 10e-6);
528       ASSERT (i + 1 >= tm->ready_connections);
529       while (i + 1 - tm->ready_connections > 1000)
530         {
531           vlib_process_suspend (vm, 100e-6);
532         }
533     }
534   return 0;
535 }
536
537 #define CLI_OUTPUT(_fmt, _args...)                      \
538   if (!tm->no_output)                                   \
539     vlib_cli_output(vm, _fmt, ##_args)
540
541 static clib_error_t *
542 test_sctp_clients_command_fn (vlib_main_t * vm,
543                               unformat_input_t * input,
544                               vlib_cli_command_t * cmd)
545 {
546   tclient_main_t *tm = &tclient_main;
547   vlib_thread_main_t *thread_main = vlib_get_thread_main ();
548   uword *event_data = 0, event_type;
549   u8 *default_connect_uri = (u8 *) "sctp://6.0.1.1/1234", *uri, *appns_id = 0;
550   u64 tmp, total_bytes, appns_flags = 0, appns_secret = 0;
551   f64 test_timeout = 20.0, syn_timeout = 20.0, delta;
552   f64 time_before_connects;
553   u32 n_clients = 1;
554   int preallocate_sessions = 0;
555   char *transfer_type;
556   clib_error_t *error = 0;
557   int i;
558
559   tm->bytes_to_send = 8192;
560   tm->no_return = 0;
561   tm->fifo_size = 64 << 10;
562   tm->connections_per_batch = 1000;
563   tm->private_segment_count = 0;
564   tm->private_segment_size = 0;
565   tm->no_output = 0;
566   tm->test_bytes = 0;
567   tm->test_failed = 0;
568   tm->vlib_main = vm;
569   if (thread_main->n_vlib_mains > 1)
570     clib_spinlock_init (&tm->sessions_lock);
571   vec_free (tm->connect_uri);
572
573   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
574     {
575       if (unformat (input, "nclients %d", &n_clients))
576         ;
577       else if (unformat (input, "mbytes %lld", &tmp))
578         tm->bytes_to_send = tmp << 20;
579       else if (unformat (input, "gbytes %lld", &tmp))
580         tm->bytes_to_send = tmp << 30;
581       else if (unformat (input, "bytes %lld", &tm->bytes_to_send))
582         ;
583       else if (unformat (input, "uri %s", &tm->connect_uri))
584         ;
585       else if (unformat (input, "test-timeout %f", &test_timeout))
586         ;
587       else if (unformat (input, "syn-timeout %f", &syn_timeout))
588         ;
589       else if (unformat (input, "no-return"))
590         tm->no_return = 1;
591       else if (unformat (input, "fifo-size %d", &tm->fifo_size))
592         tm->fifo_size <<= 10;
593       else if (unformat (input, "private-segment-count %d",
594                          &tm->private_segment_count))
595         ;
596       else if (unformat (input, "private-segment-size %U",
597                          unformat_memory_size, &tmp))
598         {
599           if (tmp >= 0x100000000ULL)
600             return clib_error_return
601               (0, "private segment size %lld (%llu) too large", tmp, tmp);
602           tm->private_segment_size = tmp;
603         }
604       else if (unformat (input, "preallocate-fifos"))
605         tm->prealloc_fifos = 1;
606       else if (unformat (input, "preallocate-sessions"))
607         preallocate_sessions = 1;
608       else
609         if (unformat (input, "client-batch %d", &tm->connections_per_batch))
610         ;
611       else if (unformat (input, "appns %_%v%_", &appns_id))
612         ;
613       else if (unformat (input, "all-scope"))
614         appns_flags |= (APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE
615                         | APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE);
616       else if (unformat (input, "local-scope"))
617         appns_flags = APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
618       else if (unformat (input, "global-scope"))
619         appns_flags = APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
620       else if (unformat (input, "secret %lu", &appns_secret))
621         ;
622       else if (unformat (input, "no-output"))
623         tm->no_output = 1;
624       else if (unformat (input, "test-bytes"))
625         tm->test_bytes = 1;
626       else
627         return clib_error_return (0, "unknown input `%U'",
628                                   format_unformat_error, input);
629     }
630
631   /* Store cli process node index for signalling */
632   tm->cli_node_index = vlib_get_current_process (vm)->node_runtime.node_index;
633
634   if (tm->is_init == 0)
635     {
636       if (sctp_test_clients_init (vm))
637         return clib_error_return (0, "failed init");
638     }
639
640   tm->ready_connections = 0;
641   tm->expected_connections = n_clients;
642   tm->rx_total = 0;
643   tm->tx_total = 0;
644
645   uri = default_connect_uri;
646   if (tm->connect_uri)
647     uri = tm->connect_uri;
648
649 #if SCTP_BUILTIN_CLIENT_PTHREAD
650   start_tx_pthread ();
651 #endif
652
653   vlib_worker_thread_barrier_sync (vm);
654   vnet_session_enable_disable (vm, 1 /* turn on SCTP, etc. */ );
655   vlib_worker_thread_barrier_release (vm);
656
657   if (tm->test_client_attached == 0)
658     {
659       if ((error = attach_builtin_test_clients_app (appns_id, appns_flags,
660                                                     appns_secret)))
661         {
662           vec_free (appns_id);
663           clib_error_report (error);
664           return error;
665         }
666       vec_free (appns_id);
667     }
668   tm->test_client_attached = 1;
669
670   /* Turn on the builtin client input nodes */
671   for (i = 0; i < thread_main->n_vlib_mains; i++)
672     vlib_node_set_state (vlib_mains[i], builtin_sctp_client_node.index,
673                          VLIB_NODE_STATE_POLLING);
674
675   if (preallocate_sessions)
676     {
677       session_t *sp __attribute__ ((unused));
678       for (i = 0; i < n_clients; i++)
679         pool_get (tm->sessions, sp);
680       for (i = 0; i < n_clients; i++)
681         pool_put_index (tm->sessions, i);
682     }
683
684   /* Fire off connect requests */
685   time_before_connects = vlib_time_now (vm);
686   if ((error = clients_connect_sctp (vm, uri, n_clients)))
687     return error;
688
689   /* Park until the sessions come up, or ten seconds elapse... */
690   vlib_process_wait_for_event_or_clock (vm, syn_timeout);
691
692   event_type = vlib_process_get_events (vm, &event_data);
693   switch (event_type)
694     {
695     case ~0:
696       CLI_OUTPUT ("Timeout with only %d sessions active...",
697                   tm->ready_connections);
698       error =
699         clib_error_return (0, "failed: syn timeout (%f) with %d sessions",
700                            syn_timeout, tm->ready_connections);
701       goto cleanup;
702
703     case 1:
704       delta = vlib_time_now (vm) - time_before_connects;
705       if (delta != 0.0)
706         CLI_OUTPUT ("%d three-way handshakes in %.2f seconds %.2f/s",
707                     n_clients, delta, ((f64) n_clients) / delta);
708
709       tm->test_start_time = vlib_time_now (tm->vlib_main);
710       CLI_OUTPUT ("Test started at %.6f", tm->test_start_time);
711       break;
712
713     default:
714       CLI_OUTPUT ("unexpected event(1): %d", event_type);
715       error = clib_error_return (0, "failed: unexpected event(1): %d",
716                                  event_type);
717       goto cleanup;
718     }
719
720   /* Now wait for the sessions to finish... */
721   vlib_process_wait_for_event_or_clock (vm, test_timeout);
722   event_type = vlib_process_get_events (vm, &event_data);
723   switch (event_type)
724     {
725     case ~0:
726       CLI_OUTPUT ("Timeout with %d sessions still active...",
727                   tm->ready_connections);
728       error = clib_error_return (0, "failed: timeout with %d sessions",
729                                  tm->ready_connections);
730       goto cleanup;
731
732     case 2:
733       tm->test_end_time = vlib_time_now (vm);
734       CLI_OUTPUT ("Test finished at %.6f", tm->test_end_time);
735       break;
736
737     default:
738       CLI_OUTPUT ("unexpected event(2): %d", event_type);
739       error = clib_error_return (0, "failed: unexpected event(2): %d",
740                                  event_type);
741       goto cleanup;
742     }
743
744   delta = tm->test_end_time - tm->test_start_time;
745
746   if (delta != 0.0)
747     {
748       total_bytes = (tm->no_return ? tm->tx_total : tm->rx_total);
749       transfer_type = tm->no_return ? "half-duplex" : "full-duplex";
750       CLI_OUTPUT ("%lld bytes (%lld mbytes, %lld gbytes) in %.2f seconds",
751                   total_bytes, total_bytes / (1ULL << 20),
752                   total_bytes / (1ULL << 30), delta);
753       CLI_OUTPUT ("%.2f bytes/second %s", ((f64) total_bytes) / (delta),
754                   transfer_type);
755       CLI_OUTPUT ("%.4f gbit/second %s",
756                   (((f64) total_bytes * 8.0) / delta / 1e9), transfer_type);
757     }
758   else
759     {
760       CLI_OUTPUT ("zero delta-t?");
761       error = clib_error_return (0, "failed: zero delta-t");
762       goto cleanup;
763     }
764
765   if (tm->test_bytes && tm->test_failed)
766     error = clib_error_return (0, "failed: test bytes");
767
768 cleanup:
769   tm->run_test = 0;
770   for (i = 0; i < vec_len (tm->connection_index_by_thread); i++)
771     {
772       vec_reset_length (tm->connection_index_by_thread[i]);
773       vec_reset_length (tm->connections_this_batch_by_thread[i]);
774     }
775
776   pool_free (tm->sessions);
777
778   /* Detach the application, so we can use different fifo sizes next time */
779   if (tm->test_client_attached)
780     {
781       vnet_app_detach_args_t _da, *da = &_da;
782       int rv;
783
784       da->app_index = tm->app_index;
785       rv = vnet_application_detach (da);
786       if (rv)
787         {
788           error = clib_error_return (0, "failed: app detach");
789           CLI_OUTPUT ("WARNING: app detach failed...");
790         }
791       tm->test_client_attached = 0;
792       tm->app_index = ~0;
793     }
794   if (error)
795     CLI_OUTPUT ("test failed");
796   return error;
797 }
798
799 /* *INDENT-OFF* */
800 VLIB_CLI_COMMAND (test_clients_command, static) =
801 {
802   .path = "test sctp clients",
803   .short_help = "test sctp clients [nclients %d] [[m|g]bytes <bytes>] "
804       "[test-timeout <time>][syn-timeout <time>][no-return][fifo-size <size>]"
805       "[private-segment-count <count>][private-segment-size <bytes>[m|g]]"
806       "[preallocate-fifos][preallocate-sessions][client-batch <batch-size>]"
807       "[uri <sctp://ip/port>][test-bytes][no-output]",
808   .function = test_sctp_clients_command_fn,
809   .is_mp_safe = 1,
810 };
811 /* *INDENT-ON* */
812
813 clib_error_t *
814 sctp_test_clients_main_init (vlib_main_t * vm)
815 {
816   tclient_main_t *tm = &tclient_main;
817   tm->is_init = 0;
818   return 0;
819 }
820
821 VLIB_INIT_FUNCTION (sctp_test_clients_main_init);
822
823 /*
824  * fd.io coding-style-patch-verification: ON
825  *
826  * Local Variables:
827  * eval: (c-set-style "gnu")
828  * End:
829  */