Florin Coras | e04c299 | 2017-03-01 08:17:34 -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 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 21 | /* define message IDs */ |
| 22 | #include <vpp/api/vpe_msg_enum.h> |
| 23 | |
| 24 | /* define message structures */ |
| 25 | #define vl_typedefs |
| 26 | #include <vpp/api/vpe_all_api_h.h> |
| 27 | #undef vl_typedefs |
| 28 | |
| 29 | /* define generated endian-swappers */ |
| 30 | #define vl_endianfun |
| 31 | #include <vpp/api/vpe_all_api_h.h> |
| 32 | #undef vl_endianfun |
| 33 | |
| 34 | /* instantiate all the print functions we know about */ |
| 35 | #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__) |
| 36 | #define vl_printfun |
| 37 | #include <vpp/api/vpe_all_api_h.h> |
| 38 | #undef vl_printfun |
| 39 | |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 40 | typedef struct |
| 41 | { |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 42 | /* |
| 43 | * Server app parameters |
| 44 | */ |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 45 | unix_shared_memory_queue_t **vpp_queue; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 46 | unix_shared_memory_queue_t *vl_input_queue; /**< Sever's event queue */ |
| 47 | |
| 48 | u32 app_index; /**< Server app index */ |
| 49 | u32 my_client_index; /**< API client handle */ |
| 50 | u32 node_index; /**< process node index for evnt scheduling */ |
| 51 | |
| 52 | /* |
| 53 | * Config params |
| 54 | */ |
| 55 | u8 no_echo; /**< Don't echo traffic */ |
| 56 | u32 fifo_size; /**< Fifo size */ |
| 57 | u32 rcv_buffer_size; /**< Rcv buffer size */ |
| 58 | u32 prealloc_fifos; /**< Preallocate fifos */ |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame^] | 59 | u32 private_segment_count; /**< Number of private segments */ |
| 60 | u32 private_segment_size; /**< Size of private segments */ |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 61 | |
| 62 | /* |
| 63 | * Test state |
| 64 | */ |
| 65 | u8 **rx_buf; /**< Per-thread RX buffer */ |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 66 | u64 byte_index; |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame^] | 67 | u32 **rx_retries; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 68 | |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 69 | vlib_main_t *vlib_main; |
| 70 | } builtin_server_main_t; |
| 71 | |
| 72 | builtin_server_main_t builtin_server_main; |
| 73 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 74 | int |
| 75 | builtin_session_accept_callback (stream_session_t * s) |
| 76 | { |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 77 | builtin_server_main_t *bsm = &builtin_server_main; |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 78 | |
| 79 | bsm->vpp_queue[s->thread_index] = |
| 80 | session_manager_get_vpp_event_queue (s->thread_index); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 81 | s->session_state = SESSION_STATE_READY; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 82 | bsm->byte_index = 0; |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame^] | 83 | vec_validate (bsm->rx_retries[s->thread_index], s->session_index); |
| 84 | bsm->rx_retries[s->thread_index][s->session_index] = 0; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | void |
| 89 | builtin_session_disconnect_callback (stream_session_t * s) |
| 90 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 91 | builtin_server_main_t *bsm = &builtin_server_main; |
| 92 | vnet_disconnect_args_t _a, *a = &_a; |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 93 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 94 | a->handle = stream_session_handle (s); |
| 95 | a->app_index = bsm->app_index; |
| 96 | vnet_disconnect_session (a); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 99 | void |
| 100 | builtin_session_reset_callback (stream_session_t * s) |
| 101 | { |
| 102 | clib_warning ("called.. "); |
| 103 | |
| 104 | stream_session_cleanup (s); |
| 105 | } |
| 106 | |
| 107 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 108 | int |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 109 | builtin_session_connected_callback (u32 app_index, u32 api_context, |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 110 | stream_session_t * s, u8 is_fail) |
| 111 | { |
| 112 | clib_warning ("called..."); |
| 113 | return -1; |
| 114 | } |
| 115 | |
| 116 | int |
| 117 | builtin_add_segment_callback (u32 client_index, |
| 118 | const u8 * seg_name, u32 seg_size) |
| 119 | { |
| 120 | clib_warning ("called..."); |
| 121 | return -1; |
| 122 | } |
| 123 | |
| 124 | int |
| 125 | builtin_redirect_connect_callback (u32 client_index, void *mp) |
| 126 | { |
| 127 | clib_warning ("called..."); |
| 128 | return -1; |
| 129 | } |
| 130 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 131 | void |
| 132 | test_bytes (builtin_server_main_t * bsm, int actual_transfer) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 133 | { |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 134 | int i; |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 135 | u32 my_thread_id = vlib_get_thread_index (); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 136 | |
| 137 | for (i = 0; i < actual_transfer; i++) |
| 138 | { |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 139 | if (bsm->rx_buf[my_thread_id][i] != ((bsm->byte_index + i) & 0xff)) |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 140 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 141 | clib_warning ("at %lld expected %d got %d", bsm->byte_index + i, |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 142 | (bsm->byte_index + i) & 0xff, |
| 143 | bsm->rx_buf[my_thread_id][i]); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | bsm->byte_index += actual_transfer; |
| 147 | } |
| 148 | |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 149 | /* |
| 150 | * If no-echo, just read the data and be done with it |
| 151 | */ |
| 152 | int |
| 153 | builtin_server_rx_callback_no_echo (stream_session_t * s) |
| 154 | { |
| 155 | builtin_server_main_t *bsm = &builtin_server_main; |
| 156 | u32 my_thread_id = vlib_get_thread_index (); |
| 157 | int actual_transfer; |
| 158 | svm_fifo_t *rx_fifo; |
| 159 | |
| 160 | rx_fifo = s->server_rx_fifo; |
| 161 | |
| 162 | do |
| 163 | { |
| 164 | actual_transfer = |
| 165 | svm_fifo_dequeue_nowait (rx_fifo, bsm->rcv_buffer_size, |
| 166 | bsm->rx_buf[my_thread_id]); |
| 167 | } |
| 168 | while (actual_transfer > 0); |
| 169 | return 0; |
| 170 | } |
| 171 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 172 | int |
| 173 | builtin_server_rx_callback (stream_session_t * s) |
| 174 | { |
| 175 | u32 n_written, max_dequeue, max_enqueue, max_transfer; |
| 176 | int actual_transfer; |
| 177 | svm_fifo_t *tx_fifo, *rx_fifo; |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 178 | builtin_server_main_t *bsm = &builtin_server_main; |
| 179 | session_fifo_event_t evt; |
| 180 | static int serial_number = 0; |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame^] | 181 | u32 thread_index = vlib_get_thread_index (); |
| 182 | |
| 183 | ASSERT (s->thread_index == thread_index); |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 184 | |
Dave Barach | acd2a6a | 2017-05-16 17:41:34 -0400 | [diff] [blame] | 185 | rx_fifo = s->server_rx_fifo; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 186 | tx_fifo = s->server_tx_fifo; |
Dave Barach | acd2a6a | 2017-05-16 17:41:34 -0400 | [diff] [blame] | 187 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame^] | 188 | ASSERT (rx_fifo->master_thread_index == thread_index); |
| 189 | ASSERT (tx_fifo->master_thread_index == thread_index); |
| 190 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 191 | max_dequeue = svm_fifo_max_dequeue (s->server_rx_fifo); |
| 192 | max_enqueue = svm_fifo_max_enqueue (s->server_tx_fifo); |
| 193 | |
| 194 | if (PREDICT_FALSE (max_dequeue == 0)) |
Dave Barach | acd2a6a | 2017-05-16 17:41:34 -0400 | [diff] [blame] | 195 | return 0; |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 196 | |
| 197 | /* Number of bytes we're going to copy */ |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 198 | max_transfer = (max_dequeue < max_enqueue) ? max_dequeue : max_enqueue; |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 199 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 200 | /* No space in tx fifo */ |
| 201 | if (PREDICT_FALSE (max_transfer == 0)) |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 202 | { |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 203 | /* XXX timeout for session that are stuck */ |
| 204 | |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 205 | rx_event: |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 206 | /* Program self-tap to retry */ |
| 207 | if (svm_fifo_set_event (rx_fifo)) |
| 208 | { |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 209 | unix_shared_memory_queue_t *q; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 210 | evt.fifo = rx_fifo; |
| 211 | evt.event_type = FIFO_EVENT_BUILTIN_RX; |
| 212 | evt.event_id = 0; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 213 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame^] | 214 | q = bsm->vpp_queue[thread_index]; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 215 | if (PREDICT_FALSE (q->cursize == q->maxsize)) |
| 216 | clib_warning ("out of event queue space"); |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame^] | 217 | else if (unix_shared_memory_queue_add (q, (u8 *) & evt, 0 /* don't wait for mutex */ |
| 218 | )) |
| 219 | clib_warning ("failed to enqueue self-tap"); |
| 220 | |
| 221 | bsm->rx_retries[thread_index][s->session_index]++; |
| 222 | if (bsm->rx_retries[thread_index][s->session_index] == 500000) |
| 223 | { |
| 224 | clib_warning ("session stuck: %U", format_stream_session, s, 2); |
| 225 | } |
| 226 | } |
| 227 | else |
| 228 | { |
| 229 | bsm->rx_retries[thread_index][s->session_index] = 0; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 232 | return 0; |
| 233 | } |
| 234 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame^] | 235 | _vec_len (bsm->rx_buf[thread_index]) = max_transfer; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 236 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 237 | actual_transfer = svm_fifo_dequeue_nowait (rx_fifo, max_transfer, |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame^] | 238 | bsm->rx_buf[thread_index]); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 239 | ASSERT (actual_transfer == max_transfer); |
| 240 | |
| 241 | // test_bytes (bsm, actual_transfer); |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 242 | |
| 243 | /* |
| 244 | * Echo back |
| 245 | */ |
| 246 | |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 247 | n_written = svm_fifo_enqueue_nowait (tx_fifo, actual_transfer, |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame^] | 248 | bsm->rx_buf[thread_index]); |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 249 | |
| 250 | if (n_written != max_transfer) |
| 251 | clib_warning ("short trout!"); |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 252 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 253 | if (svm_fifo_set_event (tx_fifo)) |
| 254 | { |
| 255 | /* Fabricate TX event, send to vpp */ |
| 256 | evt.fifo = tx_fifo; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 257 | evt.event_type = FIFO_EVENT_APP_TX; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 258 | evt.event_id = serial_number++; |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 259 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame^] | 260 | if (unix_shared_memory_queue_add (bsm->vpp_queue[s->thread_index], |
| 261 | (u8 *) & evt, |
| 262 | 0 /* do wait for mutex */ )) |
| 263 | clib_warning ("failed to enqueue tx evt"); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 264 | } |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 265 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame^] | 266 | if (PREDICT_FALSE (n_written < max_dequeue)) |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 267 | goto rx_event; |
| 268 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 269 | return 0; |
| 270 | } |
| 271 | |
| 272 | static session_cb_vft_t builtin_session_cb_vft = { |
| 273 | .session_accept_callback = builtin_session_accept_callback, |
| 274 | .session_disconnect_callback = builtin_session_disconnect_callback, |
| 275 | .session_connected_callback = builtin_session_connected_callback, |
| 276 | .add_segment_callback = builtin_add_segment_callback, |
| 277 | .redirect_connect_callback = builtin_redirect_connect_callback, |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 278 | .builtin_server_rx_callback = builtin_server_rx_callback, |
| 279 | .session_reset_callback = builtin_session_reset_callback |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 280 | }; |
| 281 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 282 | /* Abuse VPP's input queue */ |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 283 | static int |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 284 | create_api_loopback (vlib_main_t * vm) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 285 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 286 | builtin_server_main_t *bsm = &builtin_server_main; |
| 287 | vl_api_memclnt_create_t _m, *mp = &_m; |
| 288 | extern void vl_api_memclnt_create_t_handler (vl_api_memclnt_create_t *); |
| 289 | api_main_t *am = &api_main; |
| 290 | vl_shmem_hdr_t *shmem_hdr; |
| 291 | uword *event_data = 0, event_type; |
| 292 | int resolved = 0; |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 293 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 294 | /* |
| 295 | * Create a "loopback" API client connection |
| 296 | * Don't do things like this unless you know what you're doing... |
| 297 | */ |
| 298 | |
| 299 | shmem_hdr = am->shmem_hdr; |
| 300 | bsm->vl_input_queue = shmem_hdr->vl_input_queue; |
| 301 | memset (mp, 0, sizeof (*mp)); |
| 302 | mp->_vl_msg_id = VL_API_MEMCLNT_CREATE; |
| 303 | mp->context = 0xFEEDFACE; |
Damjan Marion | 7bee80c | 2017-04-26 15:32:12 +0200 | [diff] [blame] | 304 | mp->input_queue = pointer_to_uword (bsm->vl_input_queue); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 305 | strncpy ((char *) mp->name, "tcp_test_server", sizeof (mp->name) - 1); |
| 306 | |
| 307 | vl_api_memclnt_create_t_handler (mp); |
| 308 | |
| 309 | /* Wait for reply */ |
| 310 | bsm->node_index = vlib_get_current_process (vm)->node_runtime.node_index; |
| 311 | vlib_process_wait_for_event_or_clock (vm, 1.0); |
| 312 | event_type = vlib_process_get_events (vm, &event_data); |
| 313 | switch (event_type) |
| 314 | { |
| 315 | case 1: |
| 316 | resolved = 1; |
| 317 | break; |
| 318 | case ~0: |
| 319 | /* timed out */ |
| 320 | break; |
| 321 | default: |
| 322 | clib_warning ("unknown event_type %d", event_type); |
| 323 | } |
| 324 | if (!resolved) |
| 325 | return -1; |
| 326 | |
| 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | static int |
| 331 | server_attach () |
| 332 | { |
| 333 | builtin_server_main_t *bsm = &builtin_server_main; |
| 334 | u8 segment_name[128]; |
| 335 | u64 options[SESSION_OPTIONS_N_OPTIONS]; |
| 336 | vnet_app_attach_args_t _a, *a = &_a; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 337 | |
| 338 | memset (a, 0, sizeof (*a)); |
| 339 | memset (options, 0, sizeof (options)); |
| 340 | |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 341 | if (bsm->no_echo) |
| 342 | builtin_session_cb_vft.builtin_server_rx_callback = |
| 343 | builtin_server_rx_callback_no_echo; |
| 344 | else |
| 345 | builtin_session_cb_vft.builtin_server_rx_callback = |
| 346 | builtin_server_rx_callback; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 347 | a->api_client_index = bsm->my_client_index; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 348 | a->session_cb_vft = &builtin_session_cb_vft; |
| 349 | a->options = options; |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 350 | a->options[SESSION_OPTIONS_SEGMENT_SIZE] = 512 << 20; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 351 | a->options[SESSION_OPTIONS_RX_FIFO_SIZE] = bsm->fifo_size; |
| 352 | a->options[SESSION_OPTIONS_TX_FIFO_SIZE] = bsm->fifo_size; |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame^] | 353 | a->options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = bsm->private_segment_count; |
| 354 | a->options[APP_OPTIONS_PRIVATE_SEGMENT_SIZE] = bsm->private_segment_size; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 355 | a->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = |
| 356 | bsm->prealloc_fifos ? bsm->prealloc_fifos : 1; |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame^] | 357 | |
| 358 | a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_BUILTIN_APP; |
| 359 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 360 | a->segment_name = segment_name; |
| 361 | a->segment_name_length = ARRAY_LEN (segment_name); |
| 362 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 363 | if (vnet_application_attach (a)) |
| 364 | { |
| 365 | clib_warning ("failed to attach server"); |
| 366 | return -1; |
| 367 | } |
| 368 | bsm->app_index = a->app_index; |
| 369 | return 0; |
| 370 | } |
| 371 | |
| 372 | static int |
| 373 | server_listen () |
| 374 | { |
| 375 | builtin_server_main_t *bsm = &builtin_server_main; |
| 376 | vnet_bind_args_t _a, *a = &_a; |
| 377 | memset (a, 0, sizeof (*a)); |
| 378 | a->app_index = bsm->app_index; |
| 379 | a->uri = "tcp://0.0.0.0/1234"; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 380 | return vnet_bind_uri (a); |
| 381 | } |
| 382 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 383 | static int |
| 384 | server_create (vlib_main_t * vm) |
| 385 | { |
| 386 | builtin_server_main_t *bsm = &builtin_server_main; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 387 | vlib_thread_main_t *vtm = vlib_get_thread_main (); |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 388 | u32 num_threads; |
| 389 | int i; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 390 | |
| 391 | if (bsm->my_client_index == (u32) ~ 0) |
| 392 | { |
| 393 | if (create_api_loopback (vm)) |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 394 | { |
| 395 | clib_warning ("failed to create api loopback"); |
| 396 | return -1; |
| 397 | } |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | num_threads = 1 /* main thread */ + vtm->n_threads; |
| 401 | vec_validate (builtin_server_main.vpp_queue, num_threads - 1); |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 402 | vec_validate (bsm->rx_buf, num_threads - 1); |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame^] | 403 | vec_validate (bsm->rx_retries, num_threads - 1); |
| 404 | |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 405 | for (i = 0; i < num_threads; i++) |
| 406 | vec_validate (bsm->rx_buf[i], bsm->rcv_buffer_size); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 407 | |
| 408 | if (server_attach ()) |
| 409 | { |
| 410 | clib_warning ("failed to attach server"); |
| 411 | return -1; |
| 412 | } |
| 413 | if (server_listen ()) |
| 414 | { |
| 415 | clib_warning ("failed to start listening"); |
| 416 | return -1; |
| 417 | } |
| 418 | return 0; |
| 419 | } |
| 420 | |
| 421 | /* Get our api client index */ |
| 422 | static void |
| 423 | vl_api_memclnt_create_reply_t_handler (vl_api_memclnt_create_reply_t * mp) |
| 424 | { |
| 425 | vlib_main_t *vm = vlib_get_main (); |
| 426 | builtin_server_main_t *bsm = &builtin_server_main; |
| 427 | bsm->my_client_index = mp->index; |
| 428 | vlib_process_signal_event (vm, bsm->node_index, 1 /* evt */ , |
| 429 | 0 /* data */ ); |
| 430 | } |
| 431 | |
| 432 | #define foreach_tcp_builtin_server_api_msg \ |
| 433 | _(MEMCLNT_CREATE_REPLY, memclnt_create_reply) \ |
| 434 | |
| 435 | static clib_error_t * |
| 436 | tcp_builtin_server_api_hookup (vlib_main_t * vm) |
| 437 | { |
| 438 | vl_msg_api_msg_config_t _c, *c = &_c; |
| 439 | |
| 440 | /* Hook up client-side static APIs to our handlers */ |
| 441 | #define _(N,n) do { \ |
| 442 | c->id = VL_API_##N; \ |
| 443 | c->name = #n; \ |
| 444 | c->handler = vl_api_##n##_t_handler; \ |
| 445 | c->cleanup = vl_noop_handler; \ |
| 446 | c->endian = vl_api_##n##_t_endian; \ |
| 447 | c->print = vl_api_##n##_t_print; \ |
| 448 | c->size = sizeof(vl_api_##n##_t); \ |
| 449 | c->traced = 1; /* trace, so these msgs print */ \ |
| 450 | c->replay = 0; /* don't replay client create/delete msgs */ \ |
| 451 | c->message_bounce = 0; /* don't bounce this message */ \ |
| 452 | vl_msg_api_config(c);} while (0); |
| 453 | |
| 454 | foreach_tcp_builtin_server_api_msg; |
| 455 | #undef _ |
| 456 | |
| 457 | return 0; |
| 458 | } |
| 459 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 460 | static clib_error_t * |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 461 | server_create_command_fn (vlib_main_t * vm, unformat_input_t * input, |
| 462 | vlib_cli_command_t * cmd) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 463 | { |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 464 | builtin_server_main_t *bsm = &builtin_server_main; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 465 | int rv; |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame^] | 466 | u32 tmp; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 467 | |
| 468 | bsm->no_echo = 0; |
| 469 | bsm->fifo_size = 64 << 10; |
| 470 | bsm->rcv_buffer_size = 128 << 10; |
| 471 | bsm->prealloc_fifos = 0; |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame^] | 472 | bsm->private_segment_count = 0; |
| 473 | bsm->private_segment_size = 0; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 474 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 475 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 476 | { |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 477 | if (unformat (input, "no-echo")) |
| 478 | bsm->no_echo = 1; |
| 479 | else if (unformat (input, "fifo-size %d", &bsm->fifo_size)) |
| 480 | bsm->fifo_size <<= 10; |
| 481 | else if (unformat (input, "rcv-buf-size %d", &bsm->rcv_buffer_size)) |
| 482 | ; |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame^] | 483 | else if (unformat (input, "prealloc-fifos %d", &bsm->prealloc_fifos)) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 484 | ; |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame^] | 485 | else if (unformat (input, "private-segment-count %d", |
| 486 | &bsm->private_segment_count)) |
| 487 | ; |
| 488 | else if (unformat (input, "private-segment-size %dm", &tmp)) |
| 489 | bsm->private_segment_size = tmp << 20; |
| 490 | else if (unformat (input, "private-segment-size %dg", &tmp)) |
| 491 | bsm->private_segment_size = tmp << 30; |
| 492 | else if (unformat (input, "private-segment-size %d", &tmp)) |
| 493 | bsm->private_segment_size = tmp; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 494 | else |
| 495 | return clib_error_return (0, "unknown input `%U'", |
| 496 | format_unformat_error, input); |
| 497 | } |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 498 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 499 | tcp_builtin_server_api_hookup (vm); |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 500 | vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */ ); |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 501 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 502 | rv = server_create (vm); |
| 503 | switch (rv) |
| 504 | { |
| 505 | case 0: |
| 506 | break; |
| 507 | default: |
| 508 | return clib_error_return (0, "server_create returned %d", rv); |
| 509 | } |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 510 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 511 | return 0; |
| 512 | } |
| 513 | |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 514 | /* *INDENT-OFF* */ |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 515 | VLIB_CLI_COMMAND (server_create_command, static) = |
| 516 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 517 | .path = "test tcp server", |
| 518 | .short_help = "test tcp server", |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 519 | .function = server_create_command_fn, |
| 520 | }; |
| 521 | /* *INDENT-ON* */ |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 522 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 523 | clib_error_t * |
| 524 | builtin_tcp_server_main_init (vlib_main_t * vm) |
| 525 | { |
| 526 | builtin_server_main_t *bsm = &builtin_server_main; |
| 527 | bsm->my_client_index = ~0; |
| 528 | return 0; |
| 529 | } |
| 530 | |
| 531 | VLIB_INIT_FUNCTION (builtin_tcp_server_main_init); |
| 532 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 533 | /* |
| 534 | * fd.io coding-style-patch-verification: ON |
| 535 | * |
| 536 | * Local Variables: |
| 537 | * eval: (c-set-style "gnu") |
| 538 | * End: |
| 539 | */ |