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