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