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 | |
| 46 | static void |
| 47 | send_test_chunk (tclient_main_t * tm, session_t * s) |
| 48 | { |
| 49 | u8 *test_data = tm->connect_test_data; |
| 50 | int test_buf_offset = 0; |
| 51 | u32 bytes_this_chunk; |
| 52 | session_fifo_event_t evt; |
| 53 | static int serial_number = 0; |
| 54 | int rv; |
| 55 | |
| 56 | while (s->bytes_to_send > 0) |
| 57 | { |
| 58 | bytes_this_chunk = vec_len (test_data) < s->bytes_to_send |
| 59 | ? vec_len (test_data) : s->bytes_to_send; |
| 60 | |
| 61 | rv = svm_fifo_enqueue_nowait (s->server_tx_fifo, 0 /*pid */ , |
| 62 | bytes_this_chunk, |
| 63 | test_data + test_buf_offset); |
| 64 | |
| 65 | if (rv > 0) |
| 66 | { |
| 67 | s->bytes_to_send -= rv; |
| 68 | test_buf_offset += rv; |
| 69 | |
| 70 | if (svm_fifo_set_event (s->server_tx_fifo)) |
| 71 | { |
| 72 | /* Fabricate TX event, send to vpp */ |
| 73 | evt.fifo = s->server_tx_fifo; |
| 74 | evt.event_type = FIFO_EVENT_SERVER_TX; |
| 75 | evt.event_id = serial_number++; |
| 76 | |
| 77 | unix_shared_memory_queue_add (tm->vpp_event_queue, (u8 *) & evt, |
| 78 | 0 /* do wait for mutex */ ); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | static void |
| 85 | receive_test_chunk (tclient_main_t * tm, session_t * s) |
| 86 | { |
| 87 | svm_fifo_t *rx_fifo = s->server_rx_fifo; |
| 88 | int n_read, bytes, i; |
| 89 | |
| 90 | bytes = svm_fifo_max_dequeue (rx_fifo); |
| 91 | /* Allow enqueuing of new event */ |
| 92 | svm_fifo_unset_event (rx_fifo); |
| 93 | |
| 94 | /* Read the bytes */ |
| 95 | do |
| 96 | { |
| 97 | n_read = svm_fifo_dequeue_nowait (rx_fifo, 0, vec_len (tm->rx_buf), |
| 98 | tm->rx_buf); |
| 99 | if (n_read > 0) |
| 100 | { |
| 101 | bytes -= n_read; |
| 102 | for (i = 0; i < n_read; i++) |
| 103 | { |
| 104 | if (tm->rx_buf[i] != ((s->bytes_received + i) & 0xff)) |
| 105 | { |
| 106 | clib_warning ("read %d error at byte %lld, 0x%x not 0x%x", |
| 107 | n_read, s->bytes_received + i, |
| 108 | tm->rx_buf[i], |
| 109 | ((s->bytes_received + i) & 0xff)); |
| 110 | } |
| 111 | } |
| 112 | s->bytes_to_receive -= n_read; |
| 113 | s->bytes_received += n_read; |
| 114 | } |
| 115 | |
| 116 | } |
| 117 | while (n_read < 0 || bytes > 0); |
| 118 | } |
| 119 | |
| 120 | static void * |
| 121 | tclient_thread_fn (void *arg) |
| 122 | { |
| 123 | tclient_main_t *tm = &tclient_main; |
| 124 | vl_api_disconnect_session_t *dmp; |
| 125 | session_t *sp; |
| 126 | struct timespec ts, tsrem; |
| 127 | int i; |
| 128 | int try_tx, try_rx; |
| 129 | u32 *session_indices = 0; |
| 130 | |
| 131 | /* stats thread wants no signals. */ |
| 132 | { |
| 133 | sigset_t s; |
| 134 | sigfillset (&s); |
| 135 | pthread_sigmask (SIG_SETMASK, &s, 0); |
| 136 | } |
| 137 | |
| 138 | while (1) |
| 139 | { |
| 140 | /* Wait until we're told to get busy */ |
| 141 | while (tm->run_test == 0 |
| 142 | || (tm->ready_connections != tm->expected_connections)) |
| 143 | { |
| 144 | ts.tv_sec = 0; |
| 145 | ts.tv_nsec = 100000000; |
| 146 | while (nanosleep (&ts, &tsrem) < 0) |
| 147 | ts = tsrem; |
| 148 | } |
| 149 | tm->run_test = 0; |
| 150 | |
| 151 | clib_warning ("Run %d iterations", tm->n_iterations); |
| 152 | |
| 153 | for (i = 0; i < tm->n_iterations; i++) |
| 154 | { |
| 155 | session_t *sp; |
| 156 | |
| 157 | do |
| 158 | { |
| 159 | try_tx = try_rx = 0; |
| 160 | |
| 161 | /* *INDENT-OFF* */ |
| 162 | pool_foreach (sp, tm->sessions, ({ |
| 163 | if (sp->bytes_to_send > 0) |
| 164 | { |
| 165 | send_test_chunk (tm, sp); |
| 166 | try_tx = 1; |
| 167 | } |
| 168 | })); |
| 169 | pool_foreach (sp, tm->sessions, ({ |
| 170 | if (sp->bytes_to_receive > 0) |
| 171 | { |
| 172 | receive_test_chunk (tm, sp); |
| 173 | try_rx = 1; |
| 174 | } |
| 175 | })); |
| 176 | /* *INDENT-ON* */ |
| 177 | |
| 178 | } |
| 179 | while (try_tx || try_rx); |
| 180 | } |
| 181 | clib_warning ("Done %d iterations", tm->n_iterations); |
| 182 | |
| 183 | /* Disconnect sessions... */ |
| 184 | vec_reset_length (session_indices); |
| 185 | pool_foreach (sp, tm->sessions, ( |
| 186 | { |
| 187 | vec_add1 (session_indices, |
| 188 | sp - tm->sessions); |
| 189 | } |
| 190 | )); |
| 191 | |
| 192 | for (i = 0; i < vec_len (session_indices); i++) |
| 193 | { |
| 194 | sp = pool_elt_at_index (tm->sessions, session_indices[i]); |
| 195 | dmp = vl_msg_api_alloc_as_if_client (sizeof (*dmp)); |
| 196 | memset (dmp, 0, sizeof (*dmp)); |
| 197 | dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION); |
| 198 | dmp->client_index = tm->my_client_index; |
| 199 | dmp->session_index = sp->vpp_session_index; |
| 200 | dmp->session_thread_index = sp->vpp_session_thread; |
| 201 | vl_msg_api_send_shmem (tm->vl_input_queue, (u8 *) & dmp); |
| 202 | pool_put (tm->sessions, sp); |
| 203 | } |
| 204 | } |
| 205 | /* NOTREACHED */ |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | /* So we don't get "no handler for... " msgs */ |
| 210 | static void |
| 211 | vl_api_memclnt_create_reply_t_handler (vl_api_memclnt_create_reply_t * mp) |
| 212 | { |
| 213 | tclient_main_t *tm = &tclient_main; |
| 214 | |
| 215 | tm->my_client_index = mp->index; |
| 216 | } |
| 217 | |
| 218 | static void |
| 219 | vl_api_connect_uri_reply_t_handler (vl_api_connect_uri_reply_t * mp) |
| 220 | { |
| 221 | tclient_main_t *tm = &tclient_main; |
| 222 | session_t *session; |
| 223 | u32 session_index; |
| 224 | u64 key; |
| 225 | i32 retval = /* clib_net_to_host_u32 ( */ mp->retval /*) */ ; |
| 226 | |
| 227 | if (retval < 0) |
| 228 | { |
| 229 | clib_warning ("connection failed: retval %d", retval); |
| 230 | return; |
| 231 | } |
| 232 | |
| 233 | tm->our_event_queue = (unix_shared_memory_queue_t *) |
| 234 | mp->vpp_event_queue_address; |
| 235 | |
| 236 | tm->vpp_event_queue = (unix_shared_memory_queue_t *) |
| 237 | mp->vpp_event_queue_address; |
| 238 | |
| 239 | /* |
| 240 | * Setup session |
| 241 | */ |
| 242 | pool_get (tm->sessions, session); |
| 243 | memset (session, 0, sizeof (*session)); |
| 244 | session_index = session - tm->sessions; |
| 245 | session->bytes_to_receive = session->bytes_to_send = tm->bytes_to_send; |
| 246 | |
| 247 | session->server_rx_fifo = (svm_fifo_t *) mp->server_rx_fifo; |
| 248 | session->server_rx_fifo->client_session_index = session_index; |
| 249 | session->server_tx_fifo = (svm_fifo_t *) mp->server_tx_fifo; |
| 250 | session->server_tx_fifo->client_session_index = session_index; |
| 251 | |
| 252 | session->vpp_session_index = mp->session_index; |
| 253 | session->vpp_session_thread = mp->session_thread_index; |
| 254 | |
| 255 | /* Add it to the session lookup table */ |
| 256 | key = (((u64) mp->session_thread_index) << 32) | (u64) mp->session_index; |
| 257 | hash_set (tm->session_index_by_vpp_handles, key, session_index); |
| 258 | |
| 259 | tm->ready_connections++; |
| 260 | } |
| 261 | |
| 262 | static void |
| 263 | create_api_loopback (tclient_main_t * tm) |
| 264 | { |
| 265 | vl_api_memclnt_create_t _m, *mp = &_m; |
| 266 | extern void vl_api_memclnt_create_t_handler (vl_api_memclnt_create_t *); |
| 267 | api_main_t *am = &api_main; |
| 268 | vl_shmem_hdr_t *shmem_hdr; |
| 269 | |
| 270 | /* |
| 271 | * Create a "loopback" API client connection |
| 272 | * Don't do things like this unless you know what you're doing... |
| 273 | */ |
| 274 | |
| 275 | shmem_hdr = am->shmem_hdr; |
| 276 | tm->vl_input_queue = shmem_hdr->vl_input_queue; |
| 277 | memset (mp, 0, sizeof (*mp)); |
| 278 | mp->_vl_msg_id = VL_API_MEMCLNT_CREATE; |
| 279 | mp->context = 0xFEEDFACE; |
| 280 | mp->input_queue = (u64) tm->vl_input_queue; |
| 281 | strncpy ((char *) mp->name, "tcp_tester", sizeof (mp->name) - 1); |
| 282 | |
| 283 | vl_api_memclnt_create_t_handler (mp); |
| 284 | } |
| 285 | |
| 286 | #define foreach_tclient_static_api_msg \ |
| 287 | _(MEMCLNT_CREATE_REPLY, memclnt_create_reply) \ |
| 288 | _(CONNECT_URI_REPLY, connect_uri_reply) |
| 289 | |
| 290 | static clib_error_t * |
| 291 | tclient_api_hookup (vlib_main_t * vm) |
| 292 | { |
| 293 | tclient_main_t *tm = &tclient_main; |
| 294 | vl_msg_api_msg_config_t _c, *c = &_c; |
| 295 | int i; |
| 296 | |
| 297 | /* Init test data */ |
| 298 | vec_validate (tm->connect_test_data, 64 * 1024 - 1); |
| 299 | for (i = 0; i < vec_len (tm->connect_test_data); i++) |
| 300 | tm->connect_test_data[i] = i & 0xff; |
| 301 | |
| 302 | tm->session_index_by_vpp_handles = hash_create (0, sizeof (uword)); |
| 303 | vec_validate (tm->rx_buf, vec_len (tm->connect_test_data) - 1); |
| 304 | |
| 305 | /* Hook up client-side static APIs to our handlers */ |
| 306 | #define _(N,n) do { \ |
| 307 | c->id = VL_API_##N; \ |
| 308 | c->name = #n; \ |
| 309 | c->handler = vl_api_##n##_t_handler; \ |
| 310 | c->cleanup = vl_noop_handler; \ |
| 311 | c->endian = vl_api_##n##_t_endian; \ |
| 312 | c->print = vl_api_##n##_t_print; \ |
| 313 | c->size = sizeof(vl_api_##n##_t); \ |
| 314 | c->traced = 1; /* trace, so these msgs print */ \ |
| 315 | c->replay = 0; /* don't replay client create/delete msgs */ \ |
| 316 | c->message_bounce = 0; /* don't bounce this message */ \ |
| 317 | vl_msg_api_config(c);} while (0); |
| 318 | |
| 319 | foreach_tclient_static_api_msg; |
| 320 | #undef _ |
| 321 | |
| 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | VLIB_API_INIT_FUNCTION (tclient_api_hookup); |
| 326 | |
| 327 | static clib_error_t * |
| 328 | test_tcp_clients_command_fn (vlib_main_t * vm, |
| 329 | unformat_input_t * input, |
| 330 | vlib_cli_command_t * cmd) |
| 331 | { |
| 332 | u8 *connect_uri = (u8 *) "tcp://6.0.1.2/1234"; |
| 333 | u8 *uri; |
| 334 | tclient_main_t *tm = &tclient_main; |
| 335 | int i; |
| 336 | u32 n_clients = 1; |
| 337 | |
| 338 | tm->bytes_to_send = 8192; |
| 339 | tm->n_iterations = 1; |
| 340 | vec_free (tm->connect_uri); |
| 341 | |
| 342 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 343 | { |
| 344 | if (unformat (input, "nclients %d", &n_clients)) |
| 345 | ; |
| 346 | else if (unformat (input, "iterations %d", &tm->n_iterations)) |
| 347 | ; |
| 348 | else if (unformat (input, "bytes %d", &tm->bytes_to_send)) |
| 349 | ; |
| 350 | else if (unformat (input, "uri %s", &tm->connect_uri)) |
| 351 | ; |
| 352 | else |
| 353 | return clib_error_return (0, "unknown input `%U'", |
| 354 | format_unformat_error, input); |
| 355 | } |
| 356 | |
| 357 | tm->ready_connections = 0; |
| 358 | tm->expected_connections = n_clients; |
| 359 | uri = connect_uri; |
| 360 | if (tm->connect_uri) |
| 361 | uri = tm->connect_uri; |
| 362 | |
| 363 | create_api_loopback (tm); |
| 364 | |
| 365 | /* Start a transmit thread */ |
| 366 | if (tm->client_thread_handle == 0) |
| 367 | { |
| 368 | int rv = pthread_create (&tm->client_thread_handle, |
| 369 | NULL /*attr */ , tclient_thread_fn, 0); |
| 370 | if (rv) |
| 371 | { |
| 372 | tm->client_thread_handle = 0; |
| 373 | return clib_error_return (0, "pthread_create returned %d", rv); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | /* Fire off connect requests, in something approaching a normal manner */ |
| 378 | for (i = 0; i < n_clients; i++) |
| 379 | { |
| 380 | vl_api_connect_uri_t *cmp; |
| 381 | cmp = vl_msg_api_alloc_as_if_client (sizeof (*cmp)); |
| 382 | memset (cmp, 0, sizeof (*cmp)); |
| 383 | |
| 384 | cmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI); |
| 385 | cmp->client_index = tm->my_client_index; |
| 386 | cmp->context = ntohl (0xfeedface); |
| 387 | memcpy (cmp->uri, uri, strlen ((char *) uri) + 1); |
| 388 | vl_msg_api_send_shmem (tm->vl_input_queue, (u8 *) & cmp); |
| 389 | } |
| 390 | |
| 391 | tm->run_test = 1; |
| 392 | |
| 393 | return 0; |
| 394 | } |
| 395 | |
| 396 | /* *INDENT-OFF* */ |
| 397 | VLIB_CLI_COMMAND (test_clients_command, static) = |
| 398 | { |
| 399 | .path = "test tcp clients", |
| 400 | .short_help = "test tcp clients", |
| 401 | .function = test_tcp_clients_command_fn, |
| 402 | }; |
| 403 | /* *INDENT-ON* */ |
| 404 | |
| 405 | /* |
| 406 | * fd.io coding-style-patch-verification: ON |
| 407 | * |
| 408 | * Local Variables: |
| 409 | * eval: (c-set-style "gnu") |
| 410 | * End: |
| 411 | */ |