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