Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * builtin_client.c - vpp built-in tcp client/connect 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 <vnet/plugin/plugin.h> |
| 20 | #include <vnet/tcp/builtin_client.h> |
| 21 | |
| 22 | #include <vlibapi/api.h> |
| 23 | #include <vlibmemory/api.h> |
| 24 | #include <vlibsocket/api.h> |
| 25 | #include <vpp/app/version.h> |
| 26 | |
| 27 | /* define message IDs */ |
| 28 | #include <vpp/api/vpe_msg_enum.h> |
| 29 | |
| 30 | /* define message structures */ |
| 31 | #define vl_typedefs |
| 32 | #include <vpp/api/vpe_all_api_h.h> |
| 33 | #undef vl_typedefs |
| 34 | |
| 35 | /* define generated endian-swappers */ |
| 36 | #define vl_endianfun |
| 37 | #include <vpp/api/vpe_all_api_h.h> |
| 38 | #undef vl_endianfun |
| 39 | |
| 40 | /* instantiate all the print functions we know about */ |
| 41 | #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__) |
| 42 | #define vl_printfun |
| 43 | #include <vpp/api/vpe_all_api_h.h> |
| 44 | #undef vl_printfun |
| 45 | |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 46 | #define TCP_BUILTIN_CLIENT_DBG (0) |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 47 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 48 | static void |
| 49 | send_test_chunk (tclient_main_t * tm, session_t * s) |
| 50 | { |
| 51 | u8 *test_data = tm->connect_test_data; |
Dave Barach | 45ce3fb | 2017-03-28 12:31:33 -0400 | [diff] [blame] | 52 | int test_buf_offset; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 53 | u32 bytes_this_chunk; |
| 54 | session_fifo_event_t evt; |
| 55 | static int serial_number = 0; |
| 56 | int rv; |
Florin Coras | 82b13a8 | 2017-04-25 11:58:06 -0700 | [diff] [blame] | 57 | |
| 58 | ASSERT (vec_len (test_data) > 0); |
| 59 | |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 60 | test_buf_offset = s->bytes_sent % vec_len (test_data); |
| 61 | bytes_this_chunk = vec_len (test_data) - test_buf_offset; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 62 | |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 63 | bytes_this_chunk = bytes_this_chunk < s->bytes_to_send |
| 64 | ? bytes_this_chunk : s->bytes_to_send; |
| 65 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 66 | rv = svm_fifo_enqueue_nowait (s->server_tx_fifo, bytes_this_chunk, |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 67 | test_data + test_buf_offset); |
| 68 | |
| 69 | /* If we managed to enqueue data... */ |
| 70 | if (rv > 0) |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 71 | { |
Dave Barach | 0194f1a | 2017-05-15 10:11:39 -0400 | [diff] [blame] | 72 | /* Account for it... */ |
| 73 | s->bytes_to_send -= rv; |
| 74 | s->bytes_sent += rv; |
| 75 | |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 76 | if (TCP_BUILTIN_CLIENT_DBG) |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 77 | { |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 78 | /* *INDENT-OFF* */ |
| 79 | ELOG_TYPE_DECLARE (e) = |
| 80 | { |
Dave Barach | 0194f1a | 2017-05-15 10:11:39 -0400 | [diff] [blame] | 81 | .format = "tx-enq: xfer %d bytes, sent %u remain %u", |
| 82 | .format_args = "i4i4i4", |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 83 | }; |
| 84 | /* *INDENT-ON* */ |
| 85 | struct |
| 86 | { |
Dave Barach | 0194f1a | 2017-05-15 10:11:39 -0400 | [diff] [blame] | 87 | u32 data[3]; |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 88 | } *ed; |
| 89 | ed = ELOG_DATA (&vlib_global_main.elog_main, e); |
| 90 | ed->data[0] = rv; |
Dave Barach | 0194f1a | 2017-05-15 10:11:39 -0400 | [diff] [blame] | 91 | ed->data[1] = s->bytes_sent; |
| 92 | ed->data[2] = s->bytes_to_send; |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 93 | } |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 94 | |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 95 | /* Poke the session layer */ |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 96 | if (svm_fifo_set_event (s->server_tx_fifo)) |
| 97 | { |
| 98 | /* Fabricate TX event, send to vpp */ |
| 99 | evt.fifo = s->server_tx_fifo; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 100 | evt.event_type = FIFO_EVENT_APP_TX; |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 101 | evt.event_id = serial_number++; |
| 102 | |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 103 | if (unix_shared_memory_queue_add (tm->vpp_event_queue, (u8 *) & evt, |
| 104 | 0 /* do wait for mutex */ )) |
| 105 | clib_warning ("could not enqueue event"); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | static void |
| 111 | receive_test_chunk (tclient_main_t * tm, session_t * s) |
| 112 | { |
| 113 | svm_fifo_t *rx_fifo = s->server_rx_fifo; |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 114 | int n_read, test_bytes = 0; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 115 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 116 | /* Allow enqueuing of new event */ |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 117 | // svm_fifo_unset_event (rx_fifo); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 118 | |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 119 | if (test_bytes) |
| 120 | { |
| 121 | n_read = svm_fifo_dequeue_nowait (rx_fifo, vec_len (tm->rx_buf), |
| 122 | tm->rx_buf); |
| 123 | } |
| 124 | else |
| 125 | { |
| 126 | n_read = svm_fifo_max_dequeue (rx_fifo); |
| 127 | svm_fifo_dequeue_drop (rx_fifo, n_read); |
| 128 | } |
| 129 | |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 130 | if (n_read > 0) |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 131 | { |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 132 | if (TCP_BUILTIN_CLIENT_DBG) |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 133 | { |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 134 | /* *INDENT-OFF* */ |
| 135 | ELOG_TYPE_DECLARE (e) = |
| 136 | { |
| 137 | .format = "rx-deq: %d bytes", |
| 138 | .format_args = "i4", |
| 139 | }; |
| 140 | /* *INDENT-ON* */ |
| 141 | struct |
| 142 | { |
| 143 | u32 data[1]; |
| 144 | } *ed; |
| 145 | ed = ELOG_DATA (&vlib_global_main.elog_main, e); |
| 146 | ed->data[0] = n_read; |
| 147 | } |
| 148 | |
| 149 | if (test_bytes) |
| 150 | { |
| 151 | int i; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 152 | for (i = 0; i < n_read; i++) |
| 153 | { |
| 154 | if (tm->rx_buf[i] != ((s->bytes_received + i) & 0xff)) |
| 155 | { |
| 156 | clib_warning ("read %d error at byte %lld, 0x%x not 0x%x", |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 157 | n_read, s->bytes_received + i, tm->rx_buf[i], |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 158 | ((s->bytes_received + i) & 0xff)); |
| 159 | } |
| 160 | } |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 161 | } |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 162 | s->bytes_to_receive -= n_read; |
| 163 | s->bytes_received += n_read; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 164 | } |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 167 | static uword |
| 168 | builtin_client_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 169 | vlib_frame_t * frame) |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 170 | { |
| 171 | tclient_main_t *tm = &tclient_main; |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 172 | int my_thread_index = vlib_get_thread_index (); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 173 | session_t *sp; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 174 | int i; |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 175 | int delete_session; |
| 176 | u32 *connection_indices; |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 177 | u32 *connections_this_batch; |
| 178 | u32 nconnections_this_batch; |
Dave Barach | 0194f1a | 2017-05-15 10:11:39 -0400 | [diff] [blame] | 179 | |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 180 | connection_indices = tm->connection_index_by_thread[my_thread_index]; |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 181 | connections_this_batch = |
| 182 | tm->connections_this_batch_by_thread[my_thread_index]; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 183 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 184 | if ((tm->run_test == 0) || |
| 185 | ((vec_len (connection_indices) == 0) |
| 186 | && vec_len (connections_this_batch) == 0)) |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 187 | return 0; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 188 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 189 | /* Grab another pile of connections */ |
| 190 | if (PREDICT_FALSE (vec_len (connections_this_batch) == 0)) |
| 191 | { |
| 192 | nconnections_this_batch = |
| 193 | clib_min (tm->connections_per_batch, vec_len (connection_indices)); |
| 194 | |
| 195 | ASSERT (nconnections_this_batch > 0); |
| 196 | vec_validate (connections_this_batch, nconnections_this_batch - 1); |
| 197 | clib_memcpy (connections_this_batch, |
| 198 | connection_indices + vec_len (connection_indices) |
| 199 | - nconnections_this_batch, |
| 200 | nconnections_this_batch * sizeof (u32)); |
| 201 | _vec_len (connection_indices) -= nconnections_this_batch; |
| 202 | } |
| 203 | |
| 204 | if (PREDICT_FALSE (tm->prev_conns != tm->connections_per_batch |
| 205 | && tm->prev_conns == vec_len (connections_this_batch))) |
| 206 | { |
| 207 | tm->repeats++; |
| 208 | tm->prev_conns = vec_len (connections_this_batch); |
| 209 | if (tm->repeats == 500000) |
| 210 | { |
| 211 | clib_warning ("stuck clients"); |
| 212 | } |
| 213 | } |
| 214 | else |
| 215 | { |
| 216 | tm->prev_conns = vec_len (connections_this_batch); |
| 217 | tm->repeats = 0; |
| 218 | } |
| 219 | |
| 220 | for (i = 0; i < vec_len (connections_this_batch); i++) |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 221 | { |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 222 | delete_session = 1; |
| 223 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 224 | sp = pool_elt_at_index (tm->sessions, connections_this_batch[i]); |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 225 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 226 | if (sp->bytes_to_send > 0) |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 227 | { |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 228 | send_test_chunk (tm, sp); |
| 229 | delete_session = 0; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 230 | } |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 231 | if (sp->bytes_to_receive > 0) |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 232 | { |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 233 | receive_test_chunk (tm, sp); |
| 234 | delete_session = 0; |
| 235 | } |
| 236 | if (PREDICT_FALSE (delete_session == 1)) |
| 237 | { |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 238 | u32 index, thread_index; |
| 239 | stream_session_t *s; |
| 240 | |
| 241 | __sync_fetch_and_add (&tm->tx_total, sp->bytes_sent); |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 242 | __sync_fetch_and_add (&tm->rx_total, sp->bytes_received); |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 243 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 244 | stream_session_parse_handle (sp->vpp_session_handle, |
| 245 | &index, &thread_index); |
| 246 | s = stream_session_get_if_valid (index, thread_index); |
| 247 | |
| 248 | if (s) |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 249 | { |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 250 | stream_session_disconnect (s); |
| 251 | vec_delete (connections_this_batch, 1, i); |
| 252 | i--; |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 253 | __sync_fetch_and_add (&tm->ready_connections, -1); |
| 254 | } |
| 255 | else |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 256 | clib_warning ("session AWOL?"); |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 257 | |
| 258 | /* Kick the debug CLI process */ |
| 259 | if (tm->ready_connections == 0) |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 260 | { |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 261 | tm->test_end_time = vlib_time_now (vm); |
| 262 | vlib_process_signal_event (vm, tm->cli_node_index, |
| 263 | 2, 0 /* data */ ); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 264 | } |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 265 | } |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 266 | } |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 267 | |
| 268 | tm->connection_index_by_thread[my_thread_index] = connection_indices; |
| 269 | tm->connections_this_batch_by_thread[my_thread_index] = |
| 270 | connections_this_batch; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 271 | return 0; |
| 272 | } |
| 273 | |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 274 | /* *INDENT-OFF* */ |
| 275 | VLIB_REGISTER_NODE (builtin_client_node) = |
| 276 | { |
| 277 | .function = builtin_client_node_fn, |
| 278 | .name = "builtin-tcp-client", |
| 279 | .type = VLIB_NODE_TYPE_INPUT, |
| 280 | .state = VLIB_NODE_STATE_DISABLED, |
| 281 | }; |
| 282 | /* *INDENT-ON* */ |
| 283 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 284 | /* So we don't get "no handler for... " msgs */ |
| 285 | static void |
| 286 | vl_api_memclnt_create_reply_t_handler (vl_api_memclnt_create_reply_t * mp) |
| 287 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 288 | vlib_main_t *vm = vlib_get_main (); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 289 | tclient_main_t *tm = &tclient_main; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 290 | tm->my_client_index = mp->index; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 291 | vlib_process_signal_event (vm, tm->cli_node_index, 1 /* evt */ , |
Dave Barach | 0194f1a | 2017-05-15 10:11:39 -0400 | [diff] [blame] | 292 | 0 /* data */ ); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 295 | static int |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 296 | create_api_loopback (tclient_main_t * tm) |
| 297 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 298 | vlib_main_t *vm = vlib_get_main (); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 299 | vl_api_memclnt_create_t _m, *mp = &_m; |
| 300 | extern void vl_api_memclnt_create_t_handler (vl_api_memclnt_create_t *); |
| 301 | api_main_t *am = &api_main; |
| 302 | vl_shmem_hdr_t *shmem_hdr; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 303 | uword *event_data = 0, event_type; |
| 304 | int resolved = 0; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 305 | |
| 306 | /* |
| 307 | * Create a "loopback" API client connection |
| 308 | * Don't do things like this unless you know what you're doing... |
| 309 | */ |
| 310 | |
| 311 | shmem_hdr = am->shmem_hdr; |
| 312 | tm->vl_input_queue = shmem_hdr->vl_input_queue; |
| 313 | memset (mp, 0, sizeof (*mp)); |
| 314 | mp->_vl_msg_id = VL_API_MEMCLNT_CREATE; |
| 315 | mp->context = 0xFEEDFACE; |
Damjan Marion | 7bee80c | 2017-04-26 15:32:12 +0200 | [diff] [blame] | 316 | mp->input_queue = pointer_to_uword (tm->vl_input_queue); |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 317 | strncpy ((char *) mp->name, "tcp_clients_tester", sizeof (mp->name) - 1); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 318 | |
| 319 | vl_api_memclnt_create_t_handler (mp); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 320 | |
| 321 | /* Wait for reply */ |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 322 | vlib_process_wait_for_event_or_clock (vm, 1.0); |
| 323 | event_type = vlib_process_get_events (vm, &event_data); |
| 324 | switch (event_type) |
| 325 | { |
| 326 | case 1: |
| 327 | resolved = 1; |
| 328 | break; |
| 329 | case ~0: |
| 330 | /* timed out */ |
| 331 | break; |
| 332 | default: |
| 333 | clib_warning ("unknown event_type %d", event_type); |
| 334 | } |
| 335 | if (!resolved) |
| 336 | return -1; |
| 337 | return 0; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | #define foreach_tclient_static_api_msg \ |
| 341 | _(MEMCLNT_CREATE_REPLY, memclnt_create_reply) \ |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 342 | |
| 343 | static clib_error_t * |
| 344 | tclient_api_hookup (vlib_main_t * vm) |
| 345 | { |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 346 | vl_msg_api_msg_config_t _c, *c = &_c; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 347 | |
| 348 | /* Hook up client-side static APIs to our handlers */ |
| 349 | #define _(N,n) do { \ |
| 350 | c->id = VL_API_##N; \ |
| 351 | c->name = #n; \ |
| 352 | c->handler = vl_api_##n##_t_handler; \ |
| 353 | c->cleanup = vl_noop_handler; \ |
| 354 | c->endian = vl_api_##n##_t_endian; \ |
| 355 | c->print = vl_api_##n##_t_print; \ |
| 356 | c->size = sizeof(vl_api_##n##_t); \ |
| 357 | c->traced = 1; /* trace, so these msgs print */ \ |
| 358 | c->replay = 0; /* don't replay client create/delete msgs */ \ |
| 359 | c->message_bounce = 0; /* don't bounce this message */ \ |
| 360 | vl_msg_api_config(c);} while (0); |
| 361 | |
| 362 | foreach_tclient_static_api_msg; |
| 363 | #undef _ |
| 364 | |
| 365 | return 0; |
| 366 | } |
| 367 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 368 | static int |
| 369 | tcp_test_clients_init (vlib_main_t * vm) |
| 370 | { |
| 371 | tclient_main_t *tm = &tclient_main; |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 372 | vlib_thread_main_t *thread_main = vlib_get_thread_main (); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 373 | int i; |
| 374 | |
| 375 | tclient_api_hookup (vm); |
| 376 | if (create_api_loopback (tm)) |
| 377 | return -1; |
| 378 | |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 379 | /* Init test data. Big buffer */ |
| 380 | vec_validate (tm->connect_test_data, 1024 * 1024 - 1); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 381 | for (i = 0; i < vec_len (tm->connect_test_data); i++) |
| 382 | tm->connect_test_data[i] = i & 0xff; |
| 383 | |
| 384 | tm->session_index_by_vpp_handles = hash_create (0, sizeof (uword)); |
| 385 | vec_validate (tm->rx_buf, vec_len (tm->connect_test_data) - 1); |
| 386 | |
| 387 | tm->is_init = 1; |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 388 | tm->vlib_main = vm; |
| 389 | |
| 390 | vec_validate (tm->connection_index_by_thread, thread_main->n_vlib_mains); |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 391 | vec_validate (tm->connections_this_batch_by_thread, |
| 392 | thread_main->n_vlib_mains); |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 393 | return 0; |
| 394 | } |
| 395 | |
| 396 | static int |
| 397 | builtin_session_connected_callback (u32 app_index, u32 api_context, |
| 398 | stream_session_t * s, u8 is_fail) |
| 399 | { |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 400 | tclient_main_t *tm = &tclient_main; |
| 401 | session_t *session; |
| 402 | u32 session_index; |
| 403 | int i; |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 404 | |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 405 | if (is_fail) |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 406 | { |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 407 | clib_warning ("connection %d failed!", api_context); |
| 408 | vlib_process_signal_event (tm->vlib_main, tm->cli_node_index, -1, |
| 409 | 0 /* data */ ); |
| 410 | return -1; |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 411 | } |
| 412 | |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 413 | /* Mark vpp session as connected */ |
| 414 | s->session_state = SESSION_STATE_READY; |
| 415 | |
| 416 | tm->our_event_queue = session_manager_get_vpp_event_queue (s->thread_index); |
| 417 | tm->vpp_event_queue = session_manager_get_vpp_event_queue (s->thread_index); |
| 418 | |
| 419 | /* |
| 420 | * Setup session |
| 421 | */ |
| 422 | pool_get (tm->sessions, session); |
| 423 | memset (session, 0, sizeof (*session)); |
| 424 | session_index = session - tm->sessions; |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 425 | session->bytes_to_send = tm->bytes_to_send; |
| 426 | session->bytes_to_receive = tm->no_return ? 0ULL : tm->bytes_to_send; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 427 | session->server_rx_fifo = s->server_rx_fifo; |
| 428 | session->server_rx_fifo->client_session_index = session_index; |
| 429 | session->server_tx_fifo = s->server_tx_fifo; |
| 430 | session->server_tx_fifo->client_session_index = session_index; |
| 431 | session->vpp_session_handle = stream_session_handle (s); |
| 432 | |
| 433 | /* Add it to the session lookup table */ |
| 434 | hash_set (tm->session_index_by_vpp_handles, session->vpp_session_handle, |
| 435 | session_index); |
| 436 | |
| 437 | if (tm->ready_connections == tm->expected_connections - 1) |
| 438 | { |
| 439 | vlib_thread_main_t *thread_main = vlib_get_thread_main (); |
| 440 | int thread_index; |
| 441 | |
| 442 | thread_index = 0; |
| 443 | for (i = 0; i < pool_elts (tm->sessions); i++) |
| 444 | { |
| 445 | vec_add1 (tm->connection_index_by_thread[thread_index], i); |
| 446 | thread_index++; |
| 447 | if (thread_index == thread_main->n_vlib_mains) |
| 448 | thread_index = 0; |
| 449 | } |
| 450 | } |
| 451 | __sync_fetch_and_add (&tm->ready_connections, 1); |
| 452 | if (tm->ready_connections == tm->expected_connections) |
| 453 | { |
| 454 | tm->run_test = 1; |
| 455 | tm->test_start_time = vlib_time_now (tm->vlib_main); |
| 456 | /* Signal the CLI process that the action is starting... */ |
| 457 | vlib_process_signal_event (tm->vlib_main, tm->cli_node_index, 1, |
| 458 | 0 /* data */ ); |
| 459 | } |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 460 | |
| 461 | return 0; |
| 462 | } |
| 463 | |
| 464 | static void |
| 465 | builtin_session_reset_callback (stream_session_t * s) |
| 466 | { |
| 467 | return; |
| 468 | } |
| 469 | |
| 470 | static int |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 471 | builtin_session_create_callback (stream_session_t * s) |
| 472 | { |
| 473 | return 0; |
| 474 | } |
| 475 | |
| 476 | static void |
| 477 | builtin_session_disconnect_callback (stream_session_t * s) |
| 478 | { |
| 479 | return; |
| 480 | } |
| 481 | |
| 482 | static int |
| 483 | builtin_server_rx_callback (stream_session_t * s) |
| 484 | { |
| 485 | return 0; |
| 486 | } |
| 487 | |
| 488 | /* *INDENT-OFF* */ |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 489 | static session_cb_vft_t builtin_clients = { |
| 490 | .session_reset_callback = builtin_session_reset_callback, |
| 491 | .session_connected_callback = builtin_session_connected_callback, |
| 492 | .session_accept_callback = builtin_session_create_callback, |
| 493 | .session_disconnect_callback = builtin_session_disconnect_callback, |
| 494 | .builtin_server_rx_callback = builtin_server_rx_callback |
| 495 | }; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 496 | /* *INDENT-ON* */ |
| 497 | |
| 498 | static int |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 499 | attach_builtin_test_clients_app (void) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 500 | { |
Dave Barach | 0194f1a | 2017-05-15 10:11:39 -0400 | [diff] [blame] | 501 | tclient_main_t *tm = &tclient_main; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 502 | vnet_app_attach_args_t _a, *a = &_a; |
| 503 | u8 segment_name[128]; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 504 | u32 segment_name_length, prealloc_fifos; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 505 | u64 options[16]; |
| 506 | |
| 507 | segment_name_length = ARRAY_LEN (segment_name); |
| 508 | |
| 509 | memset (a, 0, sizeof (*a)); |
| 510 | memset (options, 0, sizeof (options)); |
| 511 | |
Dave Barach | 0194f1a | 2017-05-15 10:11:39 -0400 | [diff] [blame] | 512 | a->api_client_index = tm->my_client_index; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 513 | a->segment_name = segment_name; |
| 514 | a->segment_name_length = segment_name_length; |
| 515 | a->session_cb_vft = &builtin_clients; |
| 516 | |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 517 | prealloc_fifos = tm->prealloc_fifos ? tm->expected_connections : 1; |
| 518 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 519 | options[SESSION_OPTIONS_ACCEPT_COOKIE] = 0x12345678; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 520 | options[SESSION_OPTIONS_SEGMENT_SIZE] = (2ULL << 32); |
| 521 | options[SESSION_OPTIONS_RX_FIFO_SIZE] = tm->fifo_size; |
| 522 | options[SESSION_OPTIONS_TX_FIFO_SIZE] = tm->fifo_size / 2; |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 523 | options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = tm->private_segment_count; |
| 524 | options[APP_OPTIONS_PRIVATE_SEGMENT_SIZE] = tm->private_segment_size; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 525 | options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = prealloc_fifos; |
| 526 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 527 | options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_BUILTIN_APP; |
| 528 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 529 | a->options = options; |
| 530 | |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 531 | if (vnet_application_attach (a)) |
| 532 | return -1; |
| 533 | |
| 534 | tm->app_index = a->app_index; |
| 535 | return 0; |
| 536 | } |
| 537 | |
| 538 | static void * |
| 539 | tclient_thread_fn (void *arg) |
| 540 | { |
| 541 | return 0; |
| 542 | } |
| 543 | |
| 544 | /** Start a transmit thread */ |
| 545 | int |
| 546 | start_tx_pthread (tclient_main_t * tm) |
| 547 | { |
| 548 | if (tm->client_thread_handle == 0) |
| 549 | { |
| 550 | int rv = pthread_create (&tm->client_thread_handle, |
| 551 | NULL /*attr */ , |
| 552 | tclient_thread_fn, 0); |
| 553 | if (rv) |
| 554 | { |
| 555 | tm->client_thread_handle = 0; |
| 556 | return -1; |
| 557 | } |
| 558 | } |
| 559 | return 0; |
| 560 | } |
| 561 | |
| 562 | void |
| 563 | clients_connect (vlib_main_t * vm, u8 * uri, u32 n_clients) |
| 564 | { |
| 565 | tclient_main_t *tm = &tclient_main; |
| 566 | vnet_connect_args_t _a, *a = &_a; |
| 567 | int i; |
| 568 | for (i = 0; i < n_clients; i++) |
| 569 | { |
| 570 | memset (a, 0, sizeof (*a)); |
| 571 | |
| 572 | a->uri = (char *) uri; |
| 573 | a->api_context = i; |
| 574 | a->app_index = tm->app_index; |
| 575 | a->mp = 0; |
| 576 | vnet_connect_uri (a); |
| 577 | |
| 578 | /* Crude pacing for call setups, 100k/sec */ |
| 579 | vlib_process_suspend (vm, 10e-6); |
| 580 | } |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 581 | } |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 582 | |
| 583 | static clib_error_t * |
| 584 | test_tcp_clients_command_fn (vlib_main_t * vm, |
| 585 | unformat_input_t * input, |
| 586 | vlib_cli_command_t * cmd) |
| 587 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 588 | tclient_main_t *tm = &tclient_main; |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 589 | vlib_thread_main_t *thread_main = vlib_get_thread_main (); |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 590 | uword *event_data = 0, event_type; |
| 591 | u8 *default_connect_uri = (u8 *) "tcp://6.0.1.1/1234", *uri; |
| 592 | u64 tmp, total_bytes; |
| 593 | f64 cli_timeout = 20.0, delta; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 594 | u32 n_clients = 1; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 595 | char *transfer_type; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 596 | int i; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 597 | |
| 598 | tm->bytes_to_send = 8192; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 599 | tm->no_return = 0; |
| 600 | tm->fifo_size = 64 << 10; |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 601 | tm->connections_per_batch = 1000; |
| 602 | tm->private_segment_count = 0; |
| 603 | tm->private_segment_size = 0; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 604 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 605 | vec_free (tm->connect_uri); |
| 606 | |
| 607 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 608 | { |
| 609 | if (unformat (input, "nclients %d", &n_clients)) |
| 610 | ; |
Dave Barach | 0194f1a | 2017-05-15 10:11:39 -0400 | [diff] [blame] | 611 | else if (unformat (input, "mbytes %lld", &tmp)) |
| 612 | tm->bytes_to_send = tmp << 20; |
| 613 | else if (unformat (input, "gbytes %lld", &tmp)) |
| 614 | tm->bytes_to_send = tmp << 30; |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 615 | else if (unformat (input, "bytes %lld", &tm->bytes_to_send)) |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 616 | ; |
| 617 | else if (unformat (input, "uri %s", &tm->connect_uri)) |
| 618 | ; |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 619 | else if (unformat (input, "cli-timeout %f", &cli_timeout)) |
| 620 | ; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 621 | else if (unformat (input, "no-return")) |
| 622 | tm->no_return = 1; |
| 623 | else if (unformat (input, "fifo-size %d", &tm->fifo_size)) |
| 624 | tm->fifo_size <<= 10; |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 625 | else if (unformat (input, "private-segment-count %d", |
| 626 | &tm->private_segment_count)) |
| 627 | ; |
| 628 | else if (unformat (input, "private-segment-size %dm", &tmp)) |
| 629 | tm->private_segment_size = tmp << 20; |
| 630 | else if (unformat (input, "private-segment-size %dg", &tmp)) |
| 631 | tm->private_segment_size = tmp << 30; |
| 632 | else if (unformat (input, "private-segment-size %d", &tmp)) |
| 633 | tm->private_segment_size = tmp; |
| 634 | else if (unformat (input, "preallocate-fifos")) |
| 635 | tm->prealloc_fifos = 1; |
| 636 | else |
| 637 | if (unformat (input, "client-batch %d", &tm->connections_per_batch)) |
| 638 | ; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 639 | else |
| 640 | return clib_error_return (0, "unknown input `%U'", |
| 641 | format_unformat_error, input); |
| 642 | } |
| 643 | |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 644 | /* Store cli process node index for signalling */ |
| 645 | tm->cli_node_index = vlib_get_current_process (vm)->node_runtime.node_index; |
| 646 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 647 | if (tm->is_init == 0) |
| 648 | { |
| 649 | if (tcp_test_clients_init (vm)) |
| 650 | return clib_error_return (0, "failed init"); |
| 651 | } |
| 652 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 653 | tm->ready_connections = 0; |
| 654 | tm->expected_connections = n_clients; |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 655 | tm->rx_total = 0; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 656 | tm->tx_total = 0; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 657 | |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 658 | uri = default_connect_uri; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 659 | if (tm->connect_uri) |
| 660 | uri = tm->connect_uri; |
| 661 | |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 662 | #if TCP_BUILTIN_CLIENT_PTHREAD |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 663 | start_tx_pthread (); |
| 664 | #endif |
| 665 | |
| 666 | vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */ ); |
| 667 | |
| 668 | if (tm->test_client_attached == 0) |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 669 | { |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 670 | if (attach_builtin_test_clients_app ()) |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 671 | { |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 672 | return clib_error_return (0, "app attach failed"); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 673 | } |
| 674 | } |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 675 | tm->test_client_attached = 1; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 676 | |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 677 | /* Turn on the builtin client input nodes */ |
| 678 | for (i = 0; i < thread_main->n_vlib_mains; i++) |
| 679 | vlib_node_set_state (vlib_mains[i], builtin_client_node.index, |
| 680 | VLIB_NODE_STATE_POLLING); |
| 681 | |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 682 | /* Fire off connect requests */ |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 683 | clients_connect (vm, uri, n_clients); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 684 | |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 685 | /* Park until the sessions come up, or ten seconds elapse... */ |
| 686 | vlib_process_wait_for_event_or_clock (vm, 10.0 /* timeout, seconds */ ); |
| 687 | event_type = vlib_process_get_events (vm, &event_data); |
| 688 | |
| 689 | switch (event_type) |
| 690 | { |
| 691 | case ~0: |
| 692 | vlib_cli_output (vm, "Timeout with only %d sessions active...", |
| 693 | tm->ready_connections); |
| 694 | goto cleanup; |
| 695 | |
| 696 | case 1: |
| 697 | vlib_cli_output (vm, "Test started at %.6f", tm->test_start_time); |
| 698 | break; |
| 699 | |
| 700 | default: |
| 701 | vlib_cli_output (vm, "unexpected event(1): %d", event_type); |
| 702 | goto cleanup; |
| 703 | } |
| 704 | |
| 705 | /* Now wait for the sessions to finish... */ |
| 706 | vlib_process_wait_for_event_or_clock (vm, cli_timeout); |
| 707 | event_type = vlib_process_get_events (vm, &event_data); |
| 708 | |
| 709 | switch (event_type) |
| 710 | { |
| 711 | case ~0: |
| 712 | vlib_cli_output (vm, "Timeout with %d sessions still active...", |
| 713 | tm->ready_connections); |
| 714 | goto cleanup; |
| 715 | |
| 716 | case 2: |
| 717 | vlib_cli_output (vm, "Test finished at %.6f", tm->test_end_time); |
| 718 | break; |
| 719 | |
| 720 | default: |
| 721 | vlib_cli_output (vm, "unexpected event(2): %d", event_type); |
| 722 | goto cleanup; |
| 723 | } |
| 724 | |
| 725 | delta = tm->test_end_time - tm->test_start_time; |
| 726 | |
| 727 | if (delta != 0.0) |
| 728 | { |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 729 | total_bytes = (tm->no_return ? tm->tx_total : tm->rx_total); |
| 730 | transfer_type = tm->no_return ? "half-duplex" : "full-duplex"; |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 731 | vlib_cli_output (vm, |
| 732 | "%lld bytes (%lld mbytes, %lld gbytes) in %.2f seconds", |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 733 | total_bytes, total_bytes / (1ULL << 20), |
| 734 | total_bytes / (1ULL << 30), delta); |
| 735 | vlib_cli_output (vm, "%.2f bytes/second %s", |
| 736 | ((f64) total_bytes) / (delta), transfer_type); |
| 737 | vlib_cli_output (vm, "%.4f gbit/second %s", |
| 738 | (((f64) total_bytes * 8.0) / delta / 1e9), |
| 739 | transfer_type); |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 740 | } |
| 741 | else |
| 742 | vlib_cli_output (vm, "zero delta-t?"); |
| 743 | |
| 744 | cleanup: |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 745 | tm->run_test = 0; |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 746 | for (i = 0; i < vec_len (tm->connection_index_by_thread); i++) |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 747 | { |
| 748 | vec_reset_length (tm->connection_index_by_thread[i]); |
| 749 | vec_reset_length (tm->connections_this_batch_by_thread[i]); |
| 750 | } |
| 751 | pool_free (tm->sessions); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 752 | |
| 753 | return 0; |
| 754 | } |
| 755 | |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 756 | /* *INDENT-OFF* */ |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 757 | VLIB_CLI_COMMAND (test_clients_command, static) = |
| 758 | { |
| 759 | .path = "test tcp clients", |
Dave Barach | 0194f1a | 2017-05-15 10:11:39 -0400 | [diff] [blame] | 760 | .short_help = "test tcp clients [nclients %d]" |
| 761 | "[iterations %d] [bytes %d] [uri tcp://6.0.1.1/1234]", |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 762 | .function = test_tcp_clients_command_fn, |
| 763 | }; |
| 764 | /* *INDENT-ON* */ |
| 765 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 766 | clib_error_t * |
| 767 | tcp_test_clients_main_init (vlib_main_t * vm) |
| 768 | { |
| 769 | tclient_main_t *tm = &tclient_main; |
| 770 | tm->is_init = 0; |
| 771 | return 0; |
| 772 | } |
| 773 | |
| 774 | VLIB_INIT_FUNCTION (tcp_test_clients_main_init); |
| 775 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 776 | /* |
| 777 | * fd.io coding-style-patch-verification: ON |
| 778 | * |
| 779 | * Local Variables: |
| 780 | * eval: (c-set-style "gnu") |
| 781 | * End: |
| 782 | */ |