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