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