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 | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 21 | typedef struct |
| 22 | { |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 23 | /* |
| 24 | * Server app parameters |
| 25 | */ |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 26 | unix_shared_memory_queue_t **vpp_queue; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 27 | unix_shared_memory_queue_t *vl_input_queue; /**< Sever's event queue */ |
| 28 | |
| 29 | u32 app_index; /**< Server app index */ |
| 30 | u32 my_client_index; /**< API client handle */ |
| 31 | u32 node_index; /**< process node index for evnt scheduling */ |
| 32 | |
| 33 | /* |
| 34 | * Config params |
| 35 | */ |
| 36 | u8 no_echo; /**< Don't echo traffic */ |
Florin Coras | 2245c0f | 2017-08-14 08:45:15 -0700 | [diff] [blame] | 37 | u32 fifo_size; /**< Fifo size */ |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 38 | u32 rcv_buffer_size; /**< Rcv buffer size */ |
| 39 | u32 prealloc_fifos; /**< Preallocate fifos */ |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 40 | u32 private_segment_count; /**< Number of private segments */ |
| 41 | u32 private_segment_size; /**< Size of private segments */ |
Florin Coras | 2245c0f | 2017-08-14 08:45:15 -0700 | [diff] [blame] | 42 | char *server_uri; /**< Server URI */ |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 43 | |
| 44 | /* |
| 45 | * Test state |
| 46 | */ |
| 47 | u8 **rx_buf; /**< Per-thread RX buffer */ |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 48 | u64 byte_index; |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 49 | u32 **rx_retries; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 50 | |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 51 | vlib_main_t *vlib_main; |
| 52 | } builtin_server_main_t; |
| 53 | |
| 54 | builtin_server_main_t builtin_server_main; |
| 55 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 56 | int |
| 57 | builtin_session_accept_callback (stream_session_t * s) |
| 58 | { |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 59 | builtin_server_main_t *bsm = &builtin_server_main; |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 60 | |
| 61 | bsm->vpp_queue[s->thread_index] = |
| 62 | session_manager_get_vpp_event_queue (s->thread_index); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 63 | s->session_state = SESSION_STATE_READY; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 64 | bsm->byte_index = 0; |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 65 | vec_validate (bsm->rx_retries[s->thread_index], s->session_index); |
| 66 | bsm->rx_retries[s->thread_index][s->session_index] = 0; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | void |
| 71 | builtin_session_disconnect_callback (stream_session_t * s) |
| 72 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 73 | builtin_server_main_t *bsm = &builtin_server_main; |
| 74 | vnet_disconnect_args_t _a, *a = &_a; |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 75 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 76 | a->handle = stream_session_handle (s); |
| 77 | a->app_index = bsm->app_index; |
| 78 | vnet_disconnect_session (a); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 81 | void |
| 82 | builtin_session_reset_callback (stream_session_t * s) |
| 83 | { |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 84 | clib_warning ("Reset session %U", format_stream_session, s, 2); |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 85 | stream_session_cleanup (s); |
| 86 | } |
| 87 | |
| 88 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 89 | int |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 90 | builtin_session_connected_callback (u32 app_index, u32 api_context, |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 91 | stream_session_t * s, u8 is_fail) |
| 92 | { |
| 93 | clib_warning ("called..."); |
| 94 | return -1; |
| 95 | } |
| 96 | |
| 97 | int |
| 98 | builtin_add_segment_callback (u32 client_index, |
| 99 | const u8 * seg_name, u32 seg_size) |
| 100 | { |
| 101 | clib_warning ("called..."); |
| 102 | return -1; |
| 103 | } |
| 104 | |
| 105 | int |
| 106 | builtin_redirect_connect_callback (u32 client_index, void *mp) |
| 107 | { |
| 108 | clib_warning ("called..."); |
| 109 | return -1; |
| 110 | } |
| 111 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 112 | void |
| 113 | test_bytes (builtin_server_main_t * bsm, int actual_transfer) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 114 | { |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 115 | int i; |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 116 | u32 my_thread_id = vlib_get_thread_index (); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 117 | |
| 118 | for (i = 0; i < actual_transfer; i++) |
| 119 | { |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 120 | 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] | 121 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 122 | clib_warning ("at %lld expected %d got %d", bsm->byte_index + i, |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 123 | (bsm->byte_index + i) & 0xff, |
| 124 | bsm->rx_buf[my_thread_id][i]); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | bsm->byte_index += actual_transfer; |
| 128 | } |
| 129 | |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 130 | /* |
| 131 | * If no-echo, just read the data and be done with it |
| 132 | */ |
| 133 | int |
| 134 | builtin_server_rx_callback_no_echo (stream_session_t * s) |
| 135 | { |
| 136 | builtin_server_main_t *bsm = &builtin_server_main; |
| 137 | u32 my_thread_id = vlib_get_thread_index (); |
| 138 | int actual_transfer; |
| 139 | svm_fifo_t *rx_fifo; |
| 140 | |
| 141 | rx_fifo = s->server_rx_fifo; |
| 142 | |
| 143 | do |
| 144 | { |
| 145 | actual_transfer = |
| 146 | svm_fifo_dequeue_nowait (rx_fifo, bsm->rcv_buffer_size, |
| 147 | bsm->rx_buf[my_thread_id]); |
| 148 | } |
| 149 | while (actual_transfer > 0); |
| 150 | return 0; |
| 151 | } |
| 152 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 153 | int |
| 154 | builtin_server_rx_callback (stream_session_t * s) |
| 155 | { |
| 156 | u32 n_written, max_dequeue, max_enqueue, max_transfer; |
| 157 | int actual_transfer; |
| 158 | svm_fifo_t *tx_fifo, *rx_fifo; |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 159 | builtin_server_main_t *bsm = &builtin_server_main; |
| 160 | session_fifo_event_t evt; |
| 161 | static int serial_number = 0; |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 162 | u32 thread_index = vlib_get_thread_index (); |
| 163 | |
| 164 | ASSERT (s->thread_index == thread_index); |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 165 | |
Dave Barach | acd2a6a | 2017-05-16 17:41:34 -0400 | [diff] [blame] | 166 | rx_fifo = s->server_rx_fifo; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 167 | tx_fifo = s->server_tx_fifo; |
Dave Barach | acd2a6a | 2017-05-16 17:41:34 -0400 | [diff] [blame] | 168 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 169 | ASSERT (rx_fifo->master_thread_index == thread_index); |
| 170 | ASSERT (tx_fifo->master_thread_index == thread_index); |
| 171 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 172 | max_dequeue = svm_fifo_max_dequeue (s->server_rx_fifo); |
| 173 | max_enqueue = svm_fifo_max_enqueue (s->server_tx_fifo); |
| 174 | |
| 175 | if (PREDICT_FALSE (max_dequeue == 0)) |
Dave Barach | acd2a6a | 2017-05-16 17:41:34 -0400 | [diff] [blame] | 176 | return 0; |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 177 | |
| 178 | /* Number of bytes we're going to copy */ |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 179 | max_transfer = (max_dequeue < max_enqueue) ? max_dequeue : max_enqueue; |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 180 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 181 | /* No space in tx fifo */ |
| 182 | if (PREDICT_FALSE (max_transfer == 0)) |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 183 | { |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 184 | /* XXX timeout for session that are stuck */ |
| 185 | |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 186 | rx_event: |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 187 | /* Program self-tap to retry */ |
| 188 | if (svm_fifo_set_event (rx_fifo)) |
| 189 | { |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 190 | unix_shared_memory_queue_t *q; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 191 | evt.fifo = rx_fifo; |
| 192 | evt.event_type = FIFO_EVENT_BUILTIN_RX; |
| 193 | evt.event_id = 0; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 194 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 195 | q = bsm->vpp_queue[thread_index]; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 196 | if (PREDICT_FALSE (q->cursize == q->maxsize)) |
| 197 | clib_warning ("out of event queue space"); |
Florin Coras | 6534b7a | 2017-07-18 05:38:03 -0400 | [diff] [blame] | 198 | else if (unix_shared_memory_queue_add (q, (u8 *) & evt, 0)) |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 199 | clib_warning ("failed to enqueue self-tap"); |
| 200 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 201 | if (bsm->rx_retries[thread_index][s->session_index] == 500000) |
| 202 | { |
| 203 | clib_warning ("session stuck: %U", format_stream_session, s, 2); |
| 204 | } |
Florin Coras | 6534b7a | 2017-07-18 05:38:03 -0400 | [diff] [blame] | 205 | if (bsm->rx_retries[thread_index][s->session_index] < 500001) |
| 206 | bsm->rx_retries[thread_index][s->session_index]++; |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 207 | } |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 208 | |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 209 | return 0; |
| 210 | } |
| 211 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 212 | _vec_len (bsm->rx_buf[thread_index]) = max_transfer; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 213 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 214 | actual_transfer = svm_fifo_dequeue_nowait (rx_fifo, max_transfer, |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 215 | bsm->rx_buf[thread_index]); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 216 | ASSERT (actual_transfer == max_transfer); |
| 217 | |
| 218 | // test_bytes (bsm, actual_transfer); |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 219 | |
| 220 | /* |
| 221 | * Echo back |
| 222 | */ |
| 223 | |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 224 | n_written = svm_fifo_enqueue_nowait (tx_fifo, actual_transfer, |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 225 | bsm->rx_buf[thread_index]); |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 226 | |
| 227 | if (n_written != max_transfer) |
| 228 | clib_warning ("short trout!"); |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 229 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 230 | if (svm_fifo_set_event (tx_fifo)) |
| 231 | { |
| 232 | /* Fabricate TX event, send to vpp */ |
| 233 | evt.fifo = tx_fifo; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 234 | evt.event_type = FIFO_EVENT_APP_TX; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 235 | evt.event_id = serial_number++; |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 236 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 237 | if (unix_shared_memory_queue_add (bsm->vpp_queue[s->thread_index], |
| 238 | (u8 *) & evt, |
| 239 | 0 /* do wait for mutex */ )) |
| 240 | clib_warning ("failed to enqueue tx evt"); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 241 | } |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 242 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 243 | if (PREDICT_FALSE (n_written < max_dequeue)) |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 244 | goto rx_event; |
| 245 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | static session_cb_vft_t builtin_session_cb_vft = { |
| 250 | .session_accept_callback = builtin_session_accept_callback, |
| 251 | .session_disconnect_callback = builtin_session_disconnect_callback, |
| 252 | .session_connected_callback = builtin_session_connected_callback, |
| 253 | .add_segment_callback = builtin_add_segment_callback, |
| 254 | .redirect_connect_callback = builtin_redirect_connect_callback, |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 255 | .builtin_server_rx_callback = builtin_server_rx_callback, |
| 256 | .session_reset_callback = builtin_session_reset_callback |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 257 | }; |
| 258 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 259 | /* Abuse VPP's input queue */ |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 260 | static int |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 261 | create_api_loopback (vlib_main_t * vm) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 262 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 263 | builtin_server_main_t *bsm = &builtin_server_main; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 264 | api_main_t *am = &api_main; |
| 265 | vl_shmem_hdr_t *shmem_hdr; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 266 | |
| 267 | shmem_hdr = am->shmem_hdr; |
| 268 | bsm->vl_input_queue = shmem_hdr->vl_input_queue; |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 269 | bsm->my_client_index = |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 270 | vl_api_memclnt_create_internal ("http_test_server", bsm->vl_input_queue); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 271 | return 0; |
| 272 | } |
| 273 | |
| 274 | static int |
| 275 | server_attach () |
| 276 | { |
| 277 | builtin_server_main_t *bsm = &builtin_server_main; |
| 278 | u8 segment_name[128]; |
| 279 | u64 options[SESSION_OPTIONS_N_OPTIONS]; |
| 280 | vnet_app_attach_args_t _a, *a = &_a; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 281 | |
| 282 | memset (a, 0, sizeof (*a)); |
| 283 | memset (options, 0, sizeof (options)); |
| 284 | |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 285 | if (bsm->no_echo) |
| 286 | builtin_session_cb_vft.builtin_server_rx_callback = |
| 287 | builtin_server_rx_callback_no_echo; |
| 288 | else |
| 289 | builtin_session_cb_vft.builtin_server_rx_callback = |
| 290 | builtin_server_rx_callback; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 291 | a->api_client_index = bsm->my_client_index; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 292 | a->session_cb_vft = &builtin_session_cb_vft; |
| 293 | a->options = options; |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 294 | a->options[SESSION_OPTIONS_SEGMENT_SIZE] = 512 << 20; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 295 | a->options[SESSION_OPTIONS_RX_FIFO_SIZE] = bsm->fifo_size; |
| 296 | a->options[SESSION_OPTIONS_TX_FIFO_SIZE] = bsm->fifo_size; |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 297 | a->options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = bsm->private_segment_count; |
| 298 | a->options[APP_OPTIONS_PRIVATE_SEGMENT_SIZE] = bsm->private_segment_size; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 299 | a->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = |
| 300 | bsm->prealloc_fifos ? bsm->prealloc_fifos : 1; |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 301 | |
| 302 | a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_BUILTIN_APP; |
| 303 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 304 | a->segment_name = segment_name; |
| 305 | a->segment_name_length = ARRAY_LEN (segment_name); |
| 306 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 307 | if (vnet_application_attach (a)) |
| 308 | { |
| 309 | clib_warning ("failed to attach server"); |
| 310 | return -1; |
| 311 | } |
| 312 | bsm->app_index = a->app_index; |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | static int |
| 317 | server_listen () |
| 318 | { |
| 319 | builtin_server_main_t *bsm = &builtin_server_main; |
| 320 | vnet_bind_args_t _a, *a = &_a; |
| 321 | memset (a, 0, sizeof (*a)); |
| 322 | a->app_index = bsm->app_index; |
Florin Coras | 2245c0f | 2017-08-14 08:45:15 -0700 | [diff] [blame] | 323 | a->uri = bsm->server_uri; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 324 | return vnet_bind_uri (a); |
| 325 | } |
| 326 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 327 | static int |
| 328 | server_create (vlib_main_t * vm) |
| 329 | { |
| 330 | builtin_server_main_t *bsm = &builtin_server_main; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 331 | vlib_thread_main_t *vtm = vlib_get_thread_main (); |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 332 | u32 num_threads; |
| 333 | int i; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 334 | |
| 335 | if (bsm->my_client_index == (u32) ~ 0) |
| 336 | { |
| 337 | if (create_api_loopback (vm)) |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 338 | { |
| 339 | clib_warning ("failed to create api loopback"); |
| 340 | return -1; |
| 341 | } |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | num_threads = 1 /* main thread */ + vtm->n_threads; |
| 345 | vec_validate (builtin_server_main.vpp_queue, num_threads - 1); |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 346 | vec_validate (bsm->rx_buf, num_threads - 1); |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 347 | vec_validate (bsm->rx_retries, num_threads - 1); |
| 348 | |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 349 | for (i = 0; i < num_threads; i++) |
| 350 | vec_validate (bsm->rx_buf[i], bsm->rcv_buffer_size); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 351 | |
| 352 | if (server_attach ()) |
| 353 | { |
| 354 | clib_warning ("failed to attach server"); |
| 355 | return -1; |
| 356 | } |
| 357 | if (server_listen ()) |
| 358 | { |
| 359 | clib_warning ("failed to start listening"); |
| 360 | return -1; |
| 361 | } |
| 362 | return 0; |
| 363 | } |
| 364 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 365 | static clib_error_t * |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 366 | server_create_command_fn (vlib_main_t * vm, unformat_input_t * input, |
| 367 | vlib_cli_command_t * cmd) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 368 | { |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 369 | builtin_server_main_t *bsm = &builtin_server_main; |
Florin Coras | 2245c0f | 2017-08-14 08:45:15 -0700 | [diff] [blame] | 370 | u8 server_uri_set = 0; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 371 | int rv; |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 372 | u32 tmp; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 373 | |
| 374 | bsm->no_echo = 0; |
| 375 | bsm->fifo_size = 64 << 10; |
| 376 | bsm->rcv_buffer_size = 128 << 10; |
| 377 | bsm->prealloc_fifos = 0; |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 378 | bsm->private_segment_count = 0; |
| 379 | bsm->private_segment_size = 0; |
Florin Coras | 2245c0f | 2017-08-14 08:45:15 -0700 | [diff] [blame] | 380 | vec_free (bsm->server_uri); |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 381 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 382 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 383 | { |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 384 | if (unformat (input, "no-echo")) |
| 385 | bsm->no_echo = 1; |
| 386 | else if (unformat (input, "fifo-size %d", &bsm->fifo_size)) |
| 387 | bsm->fifo_size <<= 10; |
| 388 | else if (unformat (input, "rcv-buf-size %d", &bsm->rcv_buffer_size)) |
| 389 | ; |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 390 | else if (unformat (input, "prealloc-fifos %d", &bsm->prealloc_fifos)) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 391 | ; |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 392 | else if (unformat (input, "private-segment-count %d", |
| 393 | &bsm->private_segment_count)) |
| 394 | ; |
| 395 | else if (unformat (input, "private-segment-size %dm", &tmp)) |
| 396 | bsm->private_segment_size = tmp << 20; |
| 397 | else if (unformat (input, "private-segment-size %dg", &tmp)) |
| 398 | bsm->private_segment_size = tmp << 30; |
| 399 | else if (unformat (input, "private-segment-size %d", &tmp)) |
| 400 | bsm->private_segment_size = tmp; |
Florin Coras | 2245c0f | 2017-08-14 08:45:15 -0700 | [diff] [blame] | 401 | else if (unformat (input, "uri %s", &bsm->server_uri)) |
| 402 | server_uri_set = 1; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 403 | else |
| 404 | return clib_error_return (0, "unknown input `%U'", |
| 405 | format_unformat_error, input); |
| 406 | } |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 407 | |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 408 | vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */ ); |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 409 | |
Florin Coras | 2245c0f | 2017-08-14 08:45:15 -0700 | [diff] [blame] | 410 | if (!server_uri_set) |
| 411 | bsm->server_uri = (char *) format (0, "tcp://0.0.0.0/1234%c", 0); |
| 412 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 413 | rv = server_create (vm); |
| 414 | switch (rv) |
| 415 | { |
| 416 | case 0: |
| 417 | break; |
| 418 | default: |
| 419 | return clib_error_return (0, "server_create returned %d", rv); |
| 420 | } |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 421 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 422 | return 0; |
| 423 | } |
| 424 | |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 425 | /* *INDENT-OFF* */ |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 426 | VLIB_CLI_COMMAND (server_create_command, static) = |
| 427 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 428 | .path = "test tcp server", |
Florin Coras | 2245c0f | 2017-08-14 08:45:15 -0700 | [diff] [blame] | 429 | .short_help = "test tcp server [no echo][fifo-size <mbytes>] " |
| 430 | "[rcv-buf-size <bytes>][prealloc-fifos <count>]" |
| 431 | "[private-segment-count <count>][private-segment-size <bytes[m|g]>]" |
| 432 | "[uri <tcp://ip/port>]", |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 433 | .function = server_create_command_fn, |
| 434 | }; |
| 435 | /* *INDENT-ON* */ |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 436 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 437 | clib_error_t * |
| 438 | builtin_tcp_server_main_init (vlib_main_t * vm) |
| 439 | { |
| 440 | builtin_server_main_t *bsm = &builtin_server_main; |
| 441 | bsm->my_client_index = ~0; |
| 442 | return 0; |
| 443 | } |
| 444 | |
| 445 | VLIB_INIT_FUNCTION (builtin_tcp_server_main_init); |
| 446 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 447 | /* |
| 448 | * fd.io coding-style-patch-verification: ON |
| 449 | * |
| 450 | * Local Variables: |
| 451 | * eval: (c-set-style "gnu") |
| 452 | * End: |
| 453 | */ |