Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015-2017 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | |
| 16 | #include <vnet/vnet.h> |
| 17 | #include <vlibmemory/api.h> |
| 18 | #include <vnet/session/application.h> |
| 19 | #include <vnet/session/application_interface.h> |
| 20 | |
| 21 | typedef struct |
| 22 | { |
| 23 | /* |
| 24 | * Server app parameters |
| 25 | */ |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 26 | svm_msg_q_t **vpp_queue; |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 27 | svm_queue_t *vl_input_queue; /**< Sever's event queue */ |
| 28 | |
| 29 | u32 app_index; /**< Server app index */ |
| 30 | u32 my_client_index; /**< API client handle */ |
| 31 | u32 node_index; /**< process node index for evnt scheduling */ |
| 32 | |
| 33 | /* |
| 34 | * Config params |
| 35 | */ |
| 36 | u8 no_echo; /**< Don't echo traffic */ |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 37 | u32 fifo_size; /**< Fifo size */ |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 38 | u32 rcv_buffer_size; /**< Rcv buffer size */ |
| 39 | u32 prealloc_fifos; /**< Preallocate fifos */ |
| 40 | u32 private_segment_count; /**< Number of private segments */ |
| 41 | u32 private_segment_size; /**< Size of private segments */ |
| 42 | char *server_uri; /**< Server URI */ |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 43 | u32 tls_engine; /**< TLS engine: mbedtls/openssl */ |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 44 | u8 is_dgram; /**< set if transport is dgram */ |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 45 | /* |
| 46 | * Test state |
| 47 | */ |
| 48 | u8 **rx_buf; /**< Per-thread RX buffer */ |
| 49 | u64 byte_index; |
| 50 | u32 **rx_retries; |
| 51 | |
| 52 | vlib_main_t *vlib_main; |
| 53 | } echo_server_main_t; |
| 54 | |
| 55 | echo_server_main_t echo_server_main; |
| 56 | |
| 57 | int |
| 58 | echo_server_session_accept_callback (stream_session_t * s) |
| 59 | { |
| 60 | echo_server_main_t *esm = &echo_server_main; |
| 61 | |
| 62 | esm->vpp_queue[s->thread_index] = |
| 63 | session_manager_get_vpp_event_queue (s->thread_index); |
| 64 | s->session_state = SESSION_STATE_READY; |
| 65 | esm->byte_index = 0; |
Florin Coras | 6f1c48d | 2018-05-02 14:34:32 -0700 | [diff] [blame] | 66 | ASSERT (vec_len (esm->rx_retries) > s->thread_index); |
| 67 | vec_validate (esm->rx_retries[s->thread_index], s->session_index); |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 68 | esm->rx_retries[s->thread_index][s->session_index] = 0; |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | void |
| 73 | echo_server_session_disconnect_callback (stream_session_t * s) |
| 74 | { |
| 75 | echo_server_main_t *esm = &echo_server_main; |
| 76 | vnet_disconnect_args_t _a, *a = &_a; |
| 77 | |
| 78 | a->handle = session_handle (s); |
| 79 | a->app_index = esm->app_index; |
| 80 | vnet_disconnect_session (a); |
| 81 | } |
| 82 | |
| 83 | void |
| 84 | echo_server_session_reset_callback (stream_session_t * s) |
| 85 | { |
| 86 | clib_warning ("Reset session %U", format_stream_session, s, 2); |
| 87 | stream_session_cleanup (s); |
| 88 | } |
| 89 | |
| 90 | int |
| 91 | echo_server_session_connected_callback (u32 app_index, u32 api_context, |
| 92 | stream_session_t * s, u8 is_fail) |
| 93 | { |
| 94 | clib_warning ("called..."); |
| 95 | return -1; |
| 96 | } |
| 97 | |
| 98 | int |
| 99 | echo_server_add_segment_callback (u32 client_index, const ssvm_private_t * sp) |
| 100 | { |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 101 | /* New heaps may be added */ |
| 102 | return 0; |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | int |
| 106 | echo_server_redirect_connect_callback (u32 client_index, void *mp) |
| 107 | { |
| 108 | clib_warning ("called..."); |
| 109 | return -1; |
| 110 | } |
| 111 | |
| 112 | void |
| 113 | test_bytes (echo_server_main_t * esm, int actual_transfer) |
| 114 | { |
| 115 | int i; |
| 116 | u32 my_thread_id = vlib_get_thread_index (); |
| 117 | |
| 118 | for (i = 0; i < actual_transfer; i++) |
| 119 | { |
| 120 | if (esm->rx_buf[my_thread_id][i] != ((esm->byte_index + i) & 0xff)) |
| 121 | { |
| 122 | clib_warning ("at %lld expected %d got %d", esm->byte_index + i, |
| 123 | (esm->byte_index + i) & 0xff, |
| 124 | esm->rx_buf[my_thread_id][i]); |
| 125 | } |
| 126 | } |
| 127 | esm->byte_index += actual_transfer; |
| 128 | } |
| 129 | |
| 130 | /* |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 131 | * If no-echo, just drop the data and be done with it. |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 132 | */ |
| 133 | int |
| 134 | echo_server_builtin_server_rx_callback_no_echo (stream_session_t * s) |
| 135 | { |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 136 | svm_fifo_t *rx_fifo = s->server_rx_fifo; |
| 137 | svm_fifo_dequeue_drop (rx_fifo, svm_fifo_max_dequeue (rx_fifo)); |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | int |
| 142 | echo_server_rx_callback (stream_session_t * s) |
| 143 | { |
| 144 | u32 n_written, max_dequeue, max_enqueue, max_transfer; |
| 145 | int actual_transfer; |
| 146 | svm_fifo_t *tx_fifo, *rx_fifo; |
| 147 | echo_server_main_t *esm = &echo_server_main; |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 148 | u32 thread_index = vlib_get_thread_index (); |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 149 | app_session_transport_t at; |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 150 | |
| 151 | ASSERT (s->thread_index == thread_index); |
| 152 | |
| 153 | rx_fifo = s->server_rx_fifo; |
| 154 | tx_fifo = s->server_tx_fifo; |
| 155 | |
| 156 | ASSERT (rx_fifo->master_thread_index == thread_index); |
| 157 | ASSERT (tx_fifo->master_thread_index == thread_index); |
| 158 | |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 159 | max_enqueue = svm_fifo_max_enqueue (tx_fifo); |
| 160 | if (!esm->is_dgram) |
| 161 | { |
| 162 | max_dequeue = svm_fifo_max_dequeue (rx_fifo); |
| 163 | } |
| 164 | else |
| 165 | { |
| 166 | session_dgram_pre_hdr_t ph; |
| 167 | svm_fifo_peek (rx_fifo, 0, sizeof (ph), (u8 *) & ph); |
| 168 | max_dequeue = ph.data_length - ph.data_offset; |
| 169 | if (!esm->vpp_queue[s->thread_index]) |
| 170 | { |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 171 | svm_msg_q_t *mq; |
| 172 | mq = session_manager_get_vpp_event_queue (s->thread_index); |
| 173 | esm->vpp_queue[s->thread_index] = mq; |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 174 | } |
| 175 | max_enqueue -= sizeof (session_dgram_hdr_t); |
| 176 | } |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 177 | |
| 178 | if (PREDICT_FALSE (max_dequeue == 0)) |
| 179 | return 0; |
| 180 | |
| 181 | /* Number of bytes we're going to copy */ |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 182 | max_transfer = clib_min (max_dequeue, max_enqueue); |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 183 | |
| 184 | /* No space in tx fifo */ |
| 185 | if (PREDICT_FALSE (max_transfer == 0)) |
| 186 | { |
| 187 | /* XXX timeout for session that are stuck */ |
| 188 | |
| 189 | rx_event: |
| 190 | /* Program self-tap to retry */ |
| 191 | if (svm_fifo_set_event (rx_fifo)) |
| 192 | { |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 193 | if (session_send_io_evt_to_thread (rx_fifo, FIFO_EVENT_BUILTIN_RX)) |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 194 | clib_warning ("failed to enqueue self-tap"); |
| 195 | |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 196 | vec_validate (esm->rx_retries[s->thread_index], s->session_index); |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 197 | if (esm->rx_retries[thread_index][s->session_index] == 500000) |
| 198 | { |
| 199 | clib_warning ("session stuck: %U", format_stream_session, s, 2); |
| 200 | } |
| 201 | if (esm->rx_retries[thread_index][s->session_index] < 500001) |
| 202 | esm->rx_retries[thread_index][s->session_index]++; |
| 203 | } |
| 204 | |
| 205 | return 0; |
| 206 | } |
| 207 | |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 208 | vec_validate (esm->rx_buf[thread_index], max_transfer); |
| 209 | if (!esm->is_dgram) |
| 210 | { |
| 211 | actual_transfer = app_recv_stream_raw (rx_fifo, |
| 212 | esm->rx_buf[thread_index], |
| 213 | max_transfer, |
Florin Coras | 460dce6 | 2018-07-27 05:45:06 -0700 | [diff] [blame] | 214 | 0 /* don't clear event */ , |
| 215 | 0 /* peek */ ); |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 216 | } |
| 217 | else |
| 218 | { |
| 219 | actual_transfer = app_recv_dgram_raw (rx_fifo, |
| 220 | esm->rx_buf[thread_index], |
| 221 | max_transfer, &at, |
Florin Coras | 460dce6 | 2018-07-27 05:45:06 -0700 | [diff] [blame] | 222 | 0 /* don't clear event */ , |
| 223 | 0 /* peek */ ); |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 224 | } |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 225 | ASSERT (actual_transfer == max_transfer); |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 226 | /* test_bytes (esm, actual_transfer); */ |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 227 | |
| 228 | /* |
| 229 | * Echo back |
| 230 | */ |
| 231 | |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 232 | if (!esm->is_dgram) |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 233 | { |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 234 | n_written = app_send_stream_raw (tx_fifo, |
| 235 | esm->vpp_queue[thread_index], |
| 236 | esm->rx_buf[thread_index], |
Florin Coras | 460dce6 | 2018-07-27 05:45:06 -0700 | [diff] [blame] | 237 | actual_transfer, FIFO_EVENT_APP_TX, 0); |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 238 | } |
| 239 | else |
| 240 | { |
| 241 | n_written = app_send_dgram_raw (tx_fifo, &at, |
| 242 | esm->vpp_queue[s->thread_index], |
| 243 | esm->rx_buf[thread_index], |
Florin Coras | 460dce6 | 2018-07-27 05:45:06 -0700 | [diff] [blame] | 244 | actual_transfer, FIFO_EVENT_APP_TX, 0); |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 245 | } |
| 246 | |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 247 | if (n_written != max_transfer) |
| 248 | clib_warning ("short trout! written %u read %u", n_written, max_transfer); |
| 249 | |
| 250 | if (PREDICT_FALSE (svm_fifo_max_dequeue (rx_fifo))) |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 251 | goto rx_event; |
| 252 | |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | static session_cb_vft_t echo_server_session_cb_vft = { |
| 257 | .session_accept_callback = echo_server_session_accept_callback, |
| 258 | .session_disconnect_callback = echo_server_session_disconnect_callback, |
| 259 | .session_connected_callback = echo_server_session_connected_callback, |
| 260 | .add_segment_callback = echo_server_add_segment_callback, |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 261 | .builtin_app_rx_callback = echo_server_rx_callback, |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 262 | .session_reset_callback = echo_server_session_reset_callback |
| 263 | }; |
| 264 | |
| 265 | /* Abuse VPP's input queue */ |
| 266 | static int |
| 267 | create_api_loopback (vlib_main_t * vm) |
| 268 | { |
| 269 | echo_server_main_t *esm = &echo_server_main; |
| 270 | api_main_t *am = &api_main; |
| 271 | vl_shmem_hdr_t *shmem_hdr; |
| 272 | |
| 273 | shmem_hdr = am->shmem_hdr; |
| 274 | esm->vl_input_queue = shmem_hdr->vl_input_queue; |
| 275 | esm->my_client_index = vl_api_memclnt_create_internal ("echo_server", |
| 276 | esm->vl_input_queue); |
| 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | static int |
| 281 | echo_server_attach (u8 * appns_id, u64 appns_flags, u64 appns_secret) |
| 282 | { |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 283 | vnet_app_add_tls_cert_args_t _a_cert, *a_cert = &_a_cert; |
| 284 | vnet_app_add_tls_key_args_t _a_key, *a_key = &_a_key; |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 285 | echo_server_main_t *esm = &echo_server_main; |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 286 | vnet_app_attach_args_t _a, *a = &_a; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 287 | u64 options[APP_OPTIONS_N_OPTIONS]; |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 288 | u32 segment_size = 512 << 20; |
| 289 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 290 | clib_memset (a, 0, sizeof (*a)); |
| 291 | clib_memset (options, 0, sizeof (options)); |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 292 | |
| 293 | if (esm->no_echo) |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 294 | echo_server_session_cb_vft.builtin_app_rx_callback = |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 295 | echo_server_builtin_server_rx_callback_no_echo; |
| 296 | else |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 297 | echo_server_session_cb_vft.builtin_app_rx_callback = |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 298 | echo_server_rx_callback; |
| 299 | |
| 300 | if (esm->private_segment_size) |
| 301 | segment_size = esm->private_segment_size; |
| 302 | |
| 303 | a->api_client_index = esm->my_client_index; |
| 304 | a->session_cb_vft = &echo_server_session_cb_vft; |
| 305 | a->options = options; |
| 306 | a->options[APP_OPTIONS_SEGMENT_SIZE] = segment_size; |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 307 | a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = segment_size; |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 308 | a->options[APP_OPTIONS_RX_FIFO_SIZE] = esm->fifo_size; |
| 309 | a->options[APP_OPTIONS_TX_FIFO_SIZE] = esm->fifo_size; |
| 310 | a->options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = esm->private_segment_count; |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 311 | a->options[APP_OPTIONS_TLS_ENGINE] = esm->tls_engine; |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 312 | a->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = |
| 313 | esm->prealloc_fifos ? esm->prealloc_fifos : 1; |
| 314 | |
| 315 | a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN; |
| 316 | if (appns_id) |
| 317 | { |
| 318 | a->namespace_id = appns_id; |
| 319 | a->options[APP_OPTIONS_FLAGS] |= appns_flags; |
| 320 | a->options[APP_OPTIONS_NAMESPACE_SECRET] = appns_secret; |
| 321 | } |
| 322 | |
| 323 | if (vnet_application_attach (a)) |
| 324 | { |
| 325 | clib_warning ("failed to attach server"); |
| 326 | return -1; |
| 327 | } |
| 328 | esm->app_index = a->app_index; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 329 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 330 | clib_memset (a_cert, 0, sizeof (*a_cert)); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 331 | a_cert->app_index = a->app_index; |
| 332 | vec_validate (a_cert->cert, test_srv_crt_rsa_len); |
| 333 | clib_memcpy (a_cert->cert, test_srv_crt_rsa, test_srv_crt_rsa_len); |
| 334 | vnet_app_add_tls_cert (a_cert); |
| 335 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 336 | clib_memset (a_key, 0, sizeof (*a_key)); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 337 | a_key->app_index = a->app_index; |
| 338 | vec_validate (a_key->key, test_srv_key_rsa_len); |
| 339 | clib_memcpy (a_key->key, test_srv_key_rsa, test_srv_key_rsa_len); |
| 340 | vnet_app_add_tls_key (a_key); |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | static int |
| 345 | echo_server_detach (void) |
| 346 | { |
| 347 | echo_server_main_t *esm = &echo_server_main; |
| 348 | vnet_app_detach_args_t _da, *da = &_da; |
| 349 | int rv; |
| 350 | |
| 351 | da->app_index = esm->app_index; |
| 352 | rv = vnet_application_detach (da); |
| 353 | esm->app_index = ~0; |
| 354 | return rv; |
| 355 | } |
| 356 | |
| 357 | static int |
| 358 | echo_server_listen () |
| 359 | { |
| 360 | echo_server_main_t *esm = &echo_server_main; |
| 361 | vnet_bind_args_t _a, *a = &_a; |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 362 | clib_memset (a, 0, sizeof (*a)); |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 363 | a->app_index = esm->app_index; |
| 364 | a->uri = esm->server_uri; |
| 365 | return vnet_bind_uri (a); |
| 366 | } |
| 367 | |
| 368 | static int |
| 369 | echo_server_create (vlib_main_t * vm, u8 * appns_id, u64 appns_flags, |
| 370 | u64 appns_secret) |
| 371 | { |
| 372 | echo_server_main_t *esm = &echo_server_main; |
| 373 | vlib_thread_main_t *vtm = vlib_get_thread_main (); |
| 374 | u32 num_threads; |
| 375 | int i; |
| 376 | |
| 377 | if (esm->my_client_index == (u32) ~ 0) |
| 378 | { |
| 379 | if (create_api_loopback (vm)) |
| 380 | { |
| 381 | clib_warning ("failed to create api loopback"); |
| 382 | return -1; |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | num_threads = 1 /* main thread */ + vtm->n_threads; |
| 387 | vec_validate (echo_server_main.vpp_queue, num_threads - 1); |
| 388 | vec_validate (esm->rx_buf, num_threads - 1); |
| 389 | vec_validate (esm->rx_retries, num_threads - 1); |
Florin Coras | c1a448b | 2018-04-20 10:51:49 -0700 | [diff] [blame] | 390 | for (i = 0; i < vec_len (esm->rx_retries); i++) |
| 391 | vec_validate (esm->rx_retries[i], |
| 392 | pool_elts (session_manager_main.sessions[i])); |
Florin Coras | 81a13db | 2018-03-16 08:48:31 -0700 | [diff] [blame] | 393 | esm->rcv_buffer_size = clib_max (esm->rcv_buffer_size, esm->fifo_size); |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 394 | for (i = 0; i < num_threads; i++) |
| 395 | vec_validate (esm->rx_buf[i], esm->rcv_buffer_size); |
| 396 | |
| 397 | if (echo_server_attach (appns_id, appns_flags, appns_secret)) |
| 398 | { |
| 399 | clib_warning ("failed to attach server"); |
| 400 | return -1; |
| 401 | } |
| 402 | if (echo_server_listen ()) |
| 403 | { |
| 404 | clib_warning ("failed to start listening"); |
| 405 | if (echo_server_detach ()) |
| 406 | clib_warning ("failed to detach"); |
| 407 | return -1; |
| 408 | } |
| 409 | return 0; |
| 410 | } |
| 411 | |
| 412 | static clib_error_t * |
| 413 | echo_server_create_command_fn (vlib_main_t * vm, unformat_input_t * input, |
| 414 | vlib_cli_command_t * cmd) |
| 415 | { |
| 416 | echo_server_main_t *esm = &echo_server_main; |
| 417 | u8 server_uri_set = 0, *appns_id = 0; |
| 418 | u64 tmp, appns_flags = 0, appns_secret = 0; |
| 419 | char *default_uri = "tcp://0.0.0.0/1234"; |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 420 | int rv, is_stop = 0; |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 421 | |
| 422 | esm->no_echo = 0; |
| 423 | esm->fifo_size = 64 << 10; |
| 424 | esm->rcv_buffer_size = 128 << 10; |
| 425 | esm->prealloc_fifos = 0; |
| 426 | esm->private_segment_count = 0; |
| 427 | esm->private_segment_size = 0; |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 428 | esm->tls_engine = TLS_ENGINE_OPENSSL; |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 429 | esm->is_dgram = 0; |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 430 | vec_free (esm->server_uri); |
| 431 | |
| 432 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 433 | { |
| 434 | if (unformat (input, "uri %s", &esm->server_uri)) |
| 435 | server_uri_set = 1; |
| 436 | else if (unformat (input, "no-echo")) |
| 437 | esm->no_echo = 1; |
| 438 | else if (unformat (input, "fifo-size %d", &esm->fifo_size)) |
| 439 | esm->fifo_size <<= 10; |
| 440 | else if (unformat (input, "rcv-buf-size %d", &esm->rcv_buffer_size)) |
| 441 | ; |
| 442 | else if (unformat (input, "prealloc-fifos %d", &esm->prealloc_fifos)) |
| 443 | ; |
| 444 | else if (unformat (input, "private-segment-count %d", |
| 445 | &esm->private_segment_count)) |
| 446 | ; |
| 447 | else if (unformat (input, "private-segment-size %U", |
| 448 | unformat_memory_size, &tmp)) |
| 449 | { |
| 450 | if (tmp >= 0x100000000ULL) |
| 451 | return clib_error_return |
| 452 | (0, "private segment size %lld (%llu) too large", tmp, tmp); |
| 453 | esm->private_segment_size = tmp; |
| 454 | } |
| 455 | else if (unformat (input, "appns %_%v%_", &appns_id)) |
| 456 | ; |
| 457 | else if (unformat (input, "all-scope")) |
| 458 | appns_flags |= (APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE |
| 459 | | APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE); |
| 460 | else if (unformat (input, "local-scope")) |
| 461 | appns_flags |= APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE; |
| 462 | else if (unformat (input, "global-scope")) |
| 463 | appns_flags |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE; |
| 464 | else if (unformat (input, "secret %lu", &appns_secret)) |
| 465 | ; |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 466 | else if (unformat (input, "stop")) |
| 467 | is_stop = 1; |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 468 | else if (unformat (input, "tls-engine %d", &esm->tls_engine)) |
| 469 | ; |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 470 | else |
| 471 | return clib_error_return (0, "failed: unknown input `%U'", |
| 472 | format_unformat_error, input); |
| 473 | } |
| 474 | |
Florin Coras | f8f516a | 2018-02-08 15:10:09 -0800 | [diff] [blame] | 475 | if (is_stop) |
| 476 | { |
| 477 | if (esm->app_index == (u32) ~ 0) |
| 478 | { |
| 479 | clib_warning ("server not running"); |
| 480 | return clib_error_return (0, "failed: server not running"); |
| 481 | } |
| 482 | rv = echo_server_detach (); |
| 483 | if (rv) |
| 484 | { |
| 485 | clib_warning ("failed: detach"); |
| 486 | return clib_error_return (0, "failed: server detach %d", rv); |
| 487 | } |
| 488 | return 0; |
| 489 | } |
| 490 | |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 491 | vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */ ); |
| 492 | |
| 493 | if (!server_uri_set) |
| 494 | { |
| 495 | clib_warning ("No uri provided! Using default: %s", default_uri); |
| 496 | esm->server_uri = (char *) format (0, "%s%c", default_uri, 0); |
| 497 | } |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 498 | if (esm->server_uri[0] == 'u' && esm->server_uri[3] != 'c') |
| 499 | esm->is_dgram = 1; |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 500 | |
| 501 | rv = echo_server_create (vm, appns_id, appns_flags, appns_secret); |
| 502 | vec_free (appns_id); |
| 503 | if (rv) |
| 504 | { |
| 505 | vec_free (esm->server_uri); |
| 506 | return clib_error_return (0, "failed: server_create returned %d", rv); |
| 507 | } |
| 508 | |
| 509 | return 0; |
| 510 | } |
| 511 | |
| 512 | /* *INDENT-OFF* */ |
| 513 | VLIB_CLI_COMMAND (echo_server_create_command, static) = |
| 514 | { |
| 515 | .path = "test echo server", |
| 516 | .short_help = "test echo server proto <proto> [no echo][fifo-size <mbytes>]" |
| 517 | "[rcv-buf-size <bytes>][prealloc-fifos <count>]" |
| 518 | "[private-segment-count <count>][private-segment-size <bytes[m|g]>]" |
| 519 | "[uri <tcp://ip/port>]", |
| 520 | .function = echo_server_create_command_fn, |
| 521 | }; |
| 522 | /* *INDENT-ON* */ |
| 523 | |
| 524 | clib_error_t * |
| 525 | echo_server_main_init (vlib_main_t * vm) |
| 526 | { |
| 527 | echo_server_main_t *esm = &echo_server_main; |
| 528 | esm->my_client_index = ~0; |
| 529 | return 0; |
| 530 | } |
| 531 | |
| 532 | VLIB_INIT_FUNCTION (echo_server_main_init); |
| 533 | |
| 534 | /* |
| 535 | * fd.io coding-style-patch-verification: ON |
| 536 | * |
| 537 | * Local Variables: |
| 538 | * eval: (c-set-style "gnu") |
| 539 | * End: |
| 540 | */ |