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 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];
446 clib_error_t *error = 0;
448 segment_name_length = ARRAY_LEN (segment_name);
450 memset (a, 0, sizeof (*a));
451 memset (options, 0, sizeof (options));
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;
458 prealloc_fifos = tm->prealloc_fifos ? tm->expected_connections : 1;
460 if (tm->private_segment_size)
461 segment_size = tm->private_segment_size;
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;
470 options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
473 options[APP_OPTIONS_FLAGS] |= appns_flags;
474 options[APP_OPTIONS_NAMESPACE_SECRET] = appns_secret;
476 a->options = options;
477 a->namespace_id = appns_id;
479 if ((error = vnet_application_attach (a)))
482 tm->app_index = a->app_index;
487 tclient_thread_fn (void *arg)
492 /** Start a transmit thread */
494 start_tx_pthread_sctp (tclient_main_t * tm)
496 if (tm->client_thread_handle == 0)
498 int rv = pthread_create (&tm->client_thread_handle,
500 tclient_thread_fn, 0);
503 tm->client_thread_handle = 0;
511 clients_connect_sctp (vlib_main_t * vm, u8 * uri, u32 n_clients)
513 tclient_main_t *tm = &tclient_main;
514 vnet_connect_args_t _a, *a = &_a;
515 clib_error_t *error = 0;
517 for (i = 0; i < n_clients; i++)
519 memset (a, 0, sizeof (*a));
521 a->uri = (char *) uri;
523 a->app_index = tm->app_index;
526 if ((error = vnet_connect_uri (a)))
530 /* Crude pacing for call setups */
532 vlib_process_suspend (vm, 10e-6);
533 ASSERT (i + 1 >= tm->ready_connections);
534 while (i + 1 - tm->ready_connections > 1000)
536 vlib_process_suspend (vm, 100e-6);
542 #define CLI_OUTPUT(_fmt, _args...) \
543 if (!tm->no_output) \
544 vlib_cli_output(vm, _fmt, ##_args)
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)
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;
559 int preallocate_sessions = 0;
561 clib_error_t *error = 0;
564 tm->bytes_to_send = 8192;
566 tm->fifo_size = 64 << 10;
567 tm->connections_per_batch = 1000;
568 tm->private_segment_count = 0;
569 tm->private_segment_size = 0;
574 if (thread_main->n_vlib_mains > 1)
575 clib_spinlock_init (&tm->sessions_lock);
576 vec_free (tm->connect_uri);
578 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
580 if (unformat (input, "nclients %d", &n_clients))
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))
588 else if (unformat (input, "uri %s", &tm->connect_uri))
590 else if (unformat (input, "test-timeout %f", &test_timeout))
592 else if (unformat (input, "syn-timeout %f", &syn_timeout))
594 else if (unformat (input, "no-return"))
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))
601 else if (unformat (input, "private-segment-size %U",
602 unformat_memory_size, &tmp))
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;
609 else if (unformat (input, "preallocate-fifos"))
610 tm->prealloc_fifos = 1;
611 else if (unformat (input, "preallocate-sessions"))
612 preallocate_sessions = 1;
614 if (unformat (input, "client-batch %d", &tm->connections_per_batch))
616 else if (unformat (input, "appns %_%v%_", &appns_id))
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))
627 else if (unformat (input, "no-output"))
629 else if (unformat (input, "test-bytes"))
632 return clib_error_return (0, "unknown input `%U'",
633 format_unformat_error, input);
636 /* Store cli process node index for signalling */
637 tm->cli_node_index = vlib_get_current_process (vm)->node_runtime.node_index;
639 if (tm->is_init == 0)
641 if (sctp_test_clients_init (vm))
642 return clib_error_return (0, "failed init");
645 tm->ready_connections = 0;
646 tm->expected_connections = n_clients;
650 uri = default_connect_uri;
652 uri = tm->connect_uri;
654 #if SCTP_BUILTIN_CLIENT_PTHREAD
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);
662 if (tm->test_client_attached == 0)
664 if ((error = attach_builtin_test_clients_app (appns_id, appns_flags,
668 clib_error_report (error);
673 tm->test_client_attached = 1;
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);
680 if (preallocate_sessions)
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);
689 /* Fire off connect requests */
690 time_before_connects = vlib_time_now (vm);
691 if ((error = clients_connect_sctp (vm, uri, n_clients)))
694 /* Park until the sessions come up, or ten seconds elapse... */
695 vlib_process_wait_for_event_or_clock (vm, syn_timeout);
697 event_type = vlib_process_get_events (vm, &event_data);
701 CLI_OUTPUT ("Timeout with only %d sessions active...",
702 tm->ready_connections);
704 clib_error_return (0, "failed: syn timeout (%f) with %d sessions",
705 syn_timeout, tm->ready_connections);
709 delta = vlib_time_now (vm) - time_before_connects;
711 CLI_OUTPUT ("%d three-way handshakes in %.2f seconds %.2f/s",
712 n_clients, delta, ((f64) n_clients) / delta);
714 tm->test_start_time = vlib_time_now (tm->vlib_main);
715 CLI_OUTPUT ("Test started at %.6f", tm->test_start_time);
719 CLI_OUTPUT ("unexpected event(1): %d", event_type);
720 error = clib_error_return (0, "failed: unexpected event(1): %d",
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);
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);
738 tm->test_end_time = vlib_time_now (vm);
739 CLI_OUTPUT ("Test finished at %.6f", tm->test_end_time);
743 CLI_OUTPUT ("unexpected event(2): %d", event_type);
744 error = clib_error_return (0, "failed: unexpected event(2): %d",
749 delta = tm->test_end_time - tm->test_start_time;
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),
760 CLI_OUTPUT ("%.4f gbit/second %s",
761 (((f64) total_bytes * 8.0) / delta / 1e9), transfer_type);
765 CLI_OUTPUT ("zero delta-t?");
766 error = clib_error_return (0, "failed: zero delta-t");
770 if (tm->test_bytes && tm->test_failed)
771 error = clib_error_return (0, "failed: test bytes");
775 for (i = 0; i < vec_len (tm->connection_index_by_thread); i++)
777 vec_reset_length (tm->connection_index_by_thread[i]);
778 vec_reset_length (tm->connections_this_batch_by_thread[i]);
781 pool_free (tm->sessions);
783 /* Detach the application, so we can use different fifo sizes next time */
784 if (tm->test_client_attached)
786 vnet_app_detach_args_t _da, *da = &_da;
789 da->app_index = tm->app_index;
790 rv = vnet_application_detach (da);
793 error = clib_error_return (0, "failed: app detach");
794 CLI_OUTPUT ("WARNING: app detach failed...");
796 tm->test_client_attached = 0;
800 CLI_OUTPUT ("test failed");
805 VLIB_CLI_COMMAND (test_clients_command, static) =
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,
819 sctp_test_clients_main_init (vlib_main_t * vm)
821 tclient_main_t *tm = &tclient_main;
826 VLIB_INIT_FUNCTION (sctp_test_clients_main_init);
829 * fd.io coding-style-patch-verification: ON
832 * eval: (c-set-style "gnu")