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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
15 #include <vnet/vnet.h>
16 #include <vnet/plugin/plugin.h>
17 #include <vnet/sctp/builtin_client.h>
19 #include <vlibapi/api.h>
20 #include <vlibmemory/api.h>
21 #include <vpp/app/version.h>
23 tclient_main_t tclient_main;
25 #define SCTP_BUILTIN_CLIENT_DBG (0)
28 signal_evt_to_cli_i (int *code)
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);
36 signal_evt_to_cli (int code)
38 if (vlib_get_thread_index () != 0)
39 vl_api_rpc_call_main_thread (signal_evt_to_cli_i, (u8 *) & code,
42 signal_evt_to_cli_i (&code);
46 send_test_chunk (tclient_main_t * tm, session_t * s)
48 u8 *test_data = tm->connect_test_data;
51 session_fifo_event_t evt;
55 ASSERT (vec_len (test_data) > 0);
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;
62 txf = s->server_tx_fifo;
63 rv = svm_fifo_enqueue_nowait (txf, bytes_this_chunk,
64 test_data + test_buf_offset);
66 /* If we managed to enqueue data... */
69 /* Account for it... */
70 s->bytes_to_send -= rv;
73 if (SCTP_BUILTIN_CLIENT_DBG)
76 ELOG_TYPE_DECLARE (e) =
78 .format = "tx-enq: xfer %d bytes, sent %u remain %u",
79 .format_args = "i4i4i4",
86 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
88 ed->data[1] = s->bytes_sent;
89 ed->data[2] = s->bytes_to_send;
92 /* Poke the session layer */
93 if (svm_fifo_set_event (txf))
95 /* Fabricate TX event, send to vpp */
97 evt.event_type = FIFO_EVENT_APP_TX;
100 (tm->vpp_event_queue[txf->master_thread_index], (u8 *) & evt,
101 0 /* do wait for mutex */ ))
102 clib_warning ("could not enqueue event");
108 receive_test_chunk (tclient_main_t * tm, session_t * s)
110 svm_fifo_t *rx_fifo = s->server_rx_fifo;
111 u32 my_thread_index = vlib_get_thread_index ();
114 /* Allow enqueuing of new event */
115 // svm_fifo_unset_event (rx_fifo);
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]);
125 n_read = svm_fifo_max_dequeue (rx_fifo);
126 svm_fifo_dequeue_drop (rx_fifo, n_read);
129 if (SCTP_BUILTIN_CLIENT_DBG)
130 clib_warning ("Receiving test chunk; n_read = %d", n_read);
134 if (SCTP_BUILTIN_CLIENT_DBG)
137 ELOG_TYPE_DECLARE (e) =
139 .format = "rx-deq: %d bytes",
147 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
148 ed->data[0] = n_read;
153 for (i = 0; i < n_read; i++)
155 if (tm->rx_buf[my_thread_index][i]
156 != ((s->bytes_received + i) & 0xff))
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));
167 if (s->bytes_to_receive < n_read)
169 s->bytes_to_receive = 0;
170 s->bytes_received += s->bytes_received;
174 s->bytes_to_receive -= n_read;
175 s->bytes_received += n_read;
181 builtin_client_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
182 vlib_frame_t * frame)
184 tclient_main_t *tm = &tclient_main;
185 int my_thread_index = vlib_get_thread_index ();
189 u32 *connection_indices;
190 u32 *connections_this_batch;
191 u32 nconnections_this_batch;
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];
197 if ((tm->run_test == 0) ||
198 ((vec_len (connection_indices) == 0)
199 && vec_len (connections_this_batch) == 0))
202 /* Grab another pile of connections */
203 if (PREDICT_FALSE (vec_len (connections_this_batch) == 0))
205 nconnections_this_batch =
206 clib_min (tm->connections_per_batch, vec_len (connection_indices));
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;
217 if (PREDICT_FALSE (tm->prev_conns != tm->connections_per_batch
218 && tm->prev_conns == vec_len (connections_this_batch)))
221 tm->prev_conns = vec_len (connections_this_batch);
222 if (tm->repeats == 500000)
224 clib_warning ("stuck clients");
229 tm->prev_conns = vec_len (connections_this_batch);
233 for (i = 0; i < vec_len (connections_this_batch); i++)
237 sp = pool_elt_at_index (tm->sessions, connections_this_batch[i]);
239 if (sp->bytes_to_send > 0)
241 send_test_chunk (tm, sp);
245 if (sp->bytes_to_receive > 0)
247 receive_test_chunk (tm, sp);
250 if (PREDICT_FALSE (delete_session == 1))
252 u32 index, thread_index;
255 __sync_fetch_and_add (&tm->tx_total, sp->bytes_sent);
256 __sync_fetch_and_add (&tm->rx_total, sp->bytes_received);
258 session_parse_handle (sp->vpp_session_handle,
259 &index, &thread_index);
260 s = session_get_if_valid (index, thread_index);
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);
269 vec_delete (connections_this_batch, 1, i);
271 __sync_fetch_and_add (&tm->ready_connections, -1);
274 clib_warning ("session AWOL?");
276 /* Kick the debug CLI process */
277 if (tm->ready_connections == 0)
279 signal_evt_to_cli (2);
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;
291 VLIB_REGISTER_NODE (builtin_sctp_client_node) =
293 .function = builtin_client_node_fn,
294 .name = "builtin-sctp-client",
295 .type = VLIB_NODE_TYPE_INPUT,
296 .state = VLIB_NODE_STATE_DISABLED,
301 create_api_loopback (tclient_main_t * tm)
303 api_main_t *am = &api_main;
304 vl_shmem_hdr_t *shmem_hdr;
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);
314 sctp_test_clients_init (vlib_main_t * vm)
316 tclient_main_t *tm = &tclient_main;
317 vlib_thread_main_t *vtm = vlib_get_thread_main ();
321 if (create_api_loopback (tm))
324 num_threads = 1 /* main thread */ + vtm->n_threads;
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;
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);
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);
345 builtin_session_connected_callback (u32 app_index, u32 api_context,
346 stream_session_t * s, u8 is_fail)
348 tclient_main_t *tm = &tclient_main;
351 u8 thread_index = vlib_get_thread_index ();
355 clib_warning ("connection %d failed!", api_context);
356 signal_evt_to_cli (-1);
360 ASSERT (s->thread_index == thread_index);
362 if (!tm->vpp_event_queue[thread_index])
363 tm->vpp_event_queue[thread_index] =
364 session_manager_get_vpp_event_queue (thread_index);
369 clib_spinlock_lock_if_init (&tm->sessions_lock);
370 pool_get (tm->sessions, session);
371 clib_spinlock_unlock_if_init (&tm->sessions_lock);
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);
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)
388 /* Signal the CLI process that the action is starting... */
389 signal_evt_to_cli (1);
396 builtin_session_reset_callback (stream_session_t * s)
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);
405 builtin_session_create_callback (stream_session_t * s)
411 builtin_session_disconnect_callback (stream_session_t * s)
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);
422 builtin_server_rx_callback (stream_session_t * s)
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
437 static clib_error_t *
438 attach_builtin_test_clients_app (u8 * appns_id, u64 appns_flags,
441 u32 prealloc_fifos, segment_size = 2 << 20;
442 tclient_main_t *tm = &tclient_main;
443 vnet_app_attach_args_t _a, *a = &_a;
445 clib_error_t *error = 0;
447 memset (a, 0, sizeof (*a));
448 memset (options, 0, sizeof (options));
450 a->api_client_index = tm->my_client_index;
451 a->session_cb_vft = &builtin_clients;
453 prealloc_fifos = tm->prealloc_fifos ? tm->expected_connections : 1;
455 if (tm->private_segment_size)
456 segment_size = tm->private_segment_size;
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;
465 options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
468 options[APP_OPTIONS_FLAGS] |= appns_flags;
469 options[APP_OPTIONS_NAMESPACE_SECRET] = appns_secret;
471 a->options = options;
472 a->namespace_id = appns_id;
474 if ((error = vnet_application_attach (a)))
477 tm->app_index = a->app_index;
482 tclient_thread_fn (void *arg)
487 /** Start a transmit thread */
489 start_tx_pthread_sctp (tclient_main_t * tm)
491 if (tm->client_thread_handle == 0)
493 int rv = pthread_create (&tm->client_thread_handle,
495 tclient_thread_fn, 0);
498 tm->client_thread_handle = 0;
506 clients_connect_sctp (vlib_main_t * vm, u8 * uri, u32 n_clients)
508 tclient_main_t *tm = &tclient_main;
509 vnet_connect_args_t _a, *a = &_a;
510 clib_error_t *error = 0;
512 for (i = 0; i < n_clients; i++)
514 memset (a, 0, sizeof (*a));
516 a->uri = (char *) uri;
518 a->app_index = tm->app_index;
521 if ((error = vnet_connect_uri (a)))
525 /* Crude pacing for call setups */
527 vlib_process_suspend (vm, 10e-6);
528 ASSERT (i + 1 >= tm->ready_connections);
529 while (i + 1 - tm->ready_connections > 1000)
531 vlib_process_suspend (vm, 100e-6);
537 #define CLI_OUTPUT(_fmt, _args...) \
538 if (!tm->no_output) \
539 vlib_cli_output(vm, _fmt, ##_args)
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)
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;
554 int preallocate_sessions = 0;
556 clib_error_t *error = 0;
559 tm->bytes_to_send = 8192;
561 tm->fifo_size = 64 << 10;
562 tm->connections_per_batch = 1000;
563 tm->private_segment_count = 0;
564 tm->private_segment_size = 0;
569 if (thread_main->n_vlib_mains > 1)
570 clib_spinlock_init (&tm->sessions_lock);
571 vec_free (tm->connect_uri);
573 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
575 if (unformat (input, "nclients %d", &n_clients))
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))
583 else if (unformat (input, "uri %s", &tm->connect_uri))
585 else if (unformat (input, "test-timeout %f", &test_timeout))
587 else if (unformat (input, "syn-timeout %f", &syn_timeout))
589 else if (unformat (input, "no-return"))
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))
596 else if (unformat (input, "private-segment-size %U",
597 unformat_memory_size, &tmp))
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;
604 else if (unformat (input, "preallocate-fifos"))
605 tm->prealloc_fifos = 1;
606 else if (unformat (input, "preallocate-sessions"))
607 preallocate_sessions = 1;
609 if (unformat (input, "client-batch %d", &tm->connections_per_batch))
611 else if (unformat (input, "appns %_%v%_", &appns_id))
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))
622 else if (unformat (input, "no-output"))
624 else if (unformat (input, "test-bytes"))
627 return clib_error_return (0, "unknown input `%U'",
628 format_unformat_error, input);
631 /* Store cli process node index for signalling */
632 tm->cli_node_index = vlib_get_current_process (vm)->node_runtime.node_index;
634 if (tm->is_init == 0)
636 if (sctp_test_clients_init (vm))
637 return clib_error_return (0, "failed init");
640 tm->ready_connections = 0;
641 tm->expected_connections = n_clients;
645 uri = default_connect_uri;
647 uri = tm->connect_uri;
649 #if SCTP_BUILTIN_CLIENT_PTHREAD
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);
657 if (tm->test_client_attached == 0)
659 if ((error = attach_builtin_test_clients_app (appns_id, appns_flags,
663 clib_error_report (error);
668 tm->test_client_attached = 1;
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);
675 if (preallocate_sessions)
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);
684 /* Fire off connect requests */
685 time_before_connects = vlib_time_now (vm);
686 if ((error = clients_connect_sctp (vm, uri, n_clients)))
689 /* Park until the sessions come up, or ten seconds elapse... */
690 vlib_process_wait_for_event_or_clock (vm, syn_timeout);
692 event_type = vlib_process_get_events (vm, &event_data);
696 CLI_OUTPUT ("Timeout with only %d sessions active...",
697 tm->ready_connections);
699 clib_error_return (0, "failed: syn timeout (%f) with %d sessions",
700 syn_timeout, tm->ready_connections);
704 delta = vlib_time_now (vm) - time_before_connects;
706 CLI_OUTPUT ("%d three-way handshakes in %.2f seconds %.2f/s",
707 n_clients, delta, ((f64) n_clients) / delta);
709 tm->test_start_time = vlib_time_now (tm->vlib_main);
710 CLI_OUTPUT ("Test started at %.6f", tm->test_start_time);
714 CLI_OUTPUT ("unexpected event(1): %d", event_type);
715 error = clib_error_return (0, "failed: unexpected event(1): %d",
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);
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);
733 tm->test_end_time = vlib_time_now (vm);
734 CLI_OUTPUT ("Test finished at %.6f", tm->test_end_time);
738 CLI_OUTPUT ("unexpected event(2): %d", event_type);
739 error = clib_error_return (0, "failed: unexpected event(2): %d",
744 delta = tm->test_end_time - tm->test_start_time;
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),
755 CLI_OUTPUT ("%.4f gbit/second %s",
756 (((f64) total_bytes * 8.0) / delta / 1e9), transfer_type);
760 CLI_OUTPUT ("zero delta-t?");
761 error = clib_error_return (0, "failed: zero delta-t");
765 if (tm->test_bytes && tm->test_failed)
766 error = clib_error_return (0, "failed: test bytes");
770 for (i = 0; i < vec_len (tm->connection_index_by_thread); i++)
772 vec_reset_length (tm->connection_index_by_thread[i]);
773 vec_reset_length (tm->connections_this_batch_by_thread[i]);
776 pool_free (tm->sessions);
778 /* Detach the application, so we can use different fifo sizes next time */
779 if (tm->test_client_attached)
781 vnet_app_detach_args_t _da, *da = &_da;
784 da->app_index = tm->app_index;
785 rv = vnet_application_detach (da);
788 error = clib_error_return (0, "failed: app detach");
789 CLI_OUTPUT ("WARNING: app detach failed...");
791 tm->test_client_attached = 0;
795 CLI_OUTPUT ("test failed");
800 VLIB_CLI_COMMAND (test_clients_command, static) =
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,
814 sctp_test_clients_main_init (vlib_main_t * vm)
816 tclient_main_t *tm = &tclient_main;
821 VLIB_INIT_FUNCTION (sctp_test_clients_main_init);
824 * fd.io coding-style-patch-verification: ON
827 * eval: (c-set-style "gnu")