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