Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [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> |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 17 | #include <vnet/session/application.h> |
| 18 | #include <vnet/session/application_interface.h> |
| 19 | |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 20 | typedef enum |
| 21 | { |
| 22 | EVENT_WAKEUP = 1, |
| 23 | } http_process_event_t; |
| 24 | |
| 25 | typedef struct |
| 26 | { |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 27 | u32 hs_index; |
| 28 | u32 thread_index; |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 29 | u64 node_index; |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 30 | } http_server_args; |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 31 | |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 32 | typedef enum |
| 33 | { |
| 34 | HTTP_STATE_CLOSED, |
| 35 | HTTP_STATE_ESTABLISHED, |
| 36 | HTTP_STATE_OK_SENT, |
| 37 | } http_session_state_t; |
| 38 | typedef struct |
| 39 | { |
| 40 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); |
| 41 | #define _(type, name) type name; |
| 42 | foreach_app_session_field |
| 43 | #undef _ |
| 44 | u32 thread_index; |
| 45 | u8 *rx_buf; |
| 46 | u32 vpp_session_index; |
| 47 | u64 vpp_session_handle; |
| 48 | } http_session_t; |
| 49 | |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 50 | typedef struct |
| 51 | { |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 52 | http_session_t **sessions; |
| 53 | clib_rwlock_t sessions_lock; |
| 54 | u32 **session_to_http_session; |
| 55 | |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 56 | svm_msg_q_t **vpp_queue; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 57 | |
| 58 | uword *handler_by_get_request; |
| 59 | |
| 60 | u32 *free_http_cli_process_node_indices; |
| 61 | |
| 62 | /* Sever's event queue */ |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 63 | svm_queue_t *vl_input_queue; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 64 | |
| 65 | /* API client handle */ |
| 66 | u32 my_client_index; |
| 67 | |
| 68 | u32 app_index; |
| 69 | |
| 70 | /* process node index for evnt scheduling */ |
| 71 | u32 node_index; |
Florin Coras | 1d6d085 | 2017-11-17 14:26:01 -0800 | [diff] [blame] | 72 | |
| 73 | u32 prealloc_fifos; |
| 74 | u32 private_segment_size; |
| 75 | u32 fifo_size; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 76 | u8 *uri; |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 77 | u32 is_static; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 78 | vlib_main_t *vlib_main; |
| 79 | } http_server_main_t; |
| 80 | |
| 81 | http_server_main_t http_server_main; |
| 82 | |
| 83 | static void |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 84 | http_server_sessions_reader_lock (void) |
| 85 | { |
| 86 | clib_rwlock_reader_lock (&http_server_main.sessions_lock); |
| 87 | } |
| 88 | |
| 89 | static void |
| 90 | http_server_sessions_reader_unlock (void) |
| 91 | { |
| 92 | clib_rwlock_reader_unlock (&http_server_main.sessions_lock); |
| 93 | } |
| 94 | |
| 95 | static void |
| 96 | http_server_sessions_writer_lock (void) |
| 97 | { |
| 98 | clib_rwlock_writer_lock (&http_server_main.sessions_lock); |
| 99 | } |
| 100 | |
| 101 | static void |
| 102 | http_server_sessions_writer_unlock (void) |
| 103 | { |
| 104 | clib_rwlock_writer_unlock (&http_server_main.sessions_lock); |
| 105 | } |
| 106 | |
| 107 | static void |
| 108 | http_server_session_lookup_add (u32 thread_index, u32 s_index, u32 hs_index) |
| 109 | { |
| 110 | http_server_main_t *hsm = &http_server_main; |
| 111 | vec_validate (hsm->session_to_http_session[thread_index], s_index); |
| 112 | hsm->session_to_http_session[thread_index][s_index] = hs_index; |
| 113 | } |
| 114 | |
| 115 | static void |
| 116 | http_server_session_lookup_del (u32 thread_index, u32 s_index) |
| 117 | { |
| 118 | http_server_main_t *hsm = &http_server_main; |
| 119 | hsm->session_to_http_session[thread_index][s_index] = ~0; |
| 120 | } |
| 121 | |
| 122 | static http_session_t * |
| 123 | http_server_session_lookup (u32 thread_index, u32 s_index) |
| 124 | { |
| 125 | http_server_main_t *hsm = &http_server_main; |
| 126 | u32 hs_index; |
| 127 | |
| 128 | if (s_index < vec_len (hsm->session_to_http_session[thread_index])) |
| 129 | { |
| 130 | hs_index = hsm->session_to_http_session[thread_index][s_index]; |
| 131 | if (hs_index < vec_len (hsm->sessions[thread_index])) |
| 132 | return &hsm->sessions[thread_index][hs_index]; |
| 133 | } |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | static http_session_t * |
| 138 | http_server_session_alloc (u32 thread_index) |
| 139 | { |
| 140 | http_server_main_t *hsm = &http_server_main; |
| 141 | http_session_t *hs; |
| 142 | pool_get (hsm->sessions[thread_index], hs); |
| 143 | memset (hs, 0, sizeof (*hs)); |
| 144 | hs->session_index = hs - hsm->sessions[thread_index]; |
| 145 | hs->thread_index = thread_index; |
| 146 | return hs; |
| 147 | } |
| 148 | |
| 149 | static http_session_t * |
| 150 | http_server_session_get (u32 thread_index, u32 hs_index) |
| 151 | { |
| 152 | http_server_main_t *hsm = &http_server_main; |
| 153 | return pool_elt_at_index (hsm->sessions[thread_index], hs_index); |
| 154 | } |
| 155 | |
| 156 | static void |
| 157 | http_server_session_free (http_session_t * hs) |
| 158 | { |
| 159 | http_server_main_t *hsm = &http_server_main; |
| 160 | pool_put (hsm->sessions[hs->thread_index], hs); |
| 161 | if (CLIB_DEBUG) |
| 162 | memset (hs, 0xfa, sizeof (*hs)); |
| 163 | } |
| 164 | |
| 165 | static void |
| 166 | http_server_session_cleanup (http_session_t * hs) |
| 167 | { |
| 168 | if (!hs) |
| 169 | return; |
| 170 | http_server_session_lookup_del (hs->thread_index, hs->vpp_session_index); |
| 171 | vec_free (hs->rx_buf); |
| 172 | http_server_session_free (hs); |
| 173 | } |
| 174 | |
| 175 | static void |
| 176 | http_process_free (http_server_args * args) |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 177 | { |
| 178 | vlib_node_runtime_t *rt; |
| 179 | vlib_main_t *vm = &vlib_global_main; |
| 180 | http_server_main_t *hsm = &http_server_main; |
| 181 | vlib_node_t *n; |
| 182 | u32 node_index; |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 183 | http_server_args **save_args; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 184 | |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 185 | node_index = args->node_index; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 186 | ASSERT (node_index != 0); |
| 187 | |
| 188 | n = vlib_get_node (vm, node_index); |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 189 | rt = vlib_node_get_runtime (vm, n->index); |
| 190 | save_args = vlib_node_get_runtime_data (vm, n->index); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 191 | |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 192 | /* Reset process session pointer */ |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 193 | clib_mem_free (*save_args); |
| 194 | *save_args = 0; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 195 | |
| 196 | /* Turn off the process node */ |
| 197 | vlib_node_set_state (vm, rt->node_index, VLIB_NODE_STATE_DISABLED); |
| 198 | |
| 199 | /* add node index to the freelist */ |
| 200 | vec_add1 (hsm->free_http_cli_process_node_indices, node_index); |
| 201 | } |
| 202 | |
Florin Coras | 898cd8f | 2018-06-08 02:02:13 -0700 | [diff] [blame] | 203 | /* *INDENT-OFF* */ |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 204 | static const char *http_ok = |
| 205 | "HTTP/1.1 200 OK\r\n"; |
| 206 | |
Florin Coras | 898cd8f | 2018-06-08 02:02:13 -0700 | [diff] [blame] | 207 | static const char *http_response = |
Florin Coras | 898cd8f | 2018-06-08 02:02:13 -0700 | [diff] [blame] | 208 | "Content-Type: text/html\r\n" |
| 209 | "Expires: Mon, 11 Jan 1970 10:10:10 GMT\r\n" |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 210 | "Connection: close \r\n" |
Florin Coras | 898cd8f | 2018-06-08 02:02:13 -0700 | [diff] [blame] | 211 | "Pragma: no-cache\r\n" |
| 212 | "Content-Length: %d\r\n\r\n%s"; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 213 | |
Florin Coras | 898cd8f | 2018-06-08 02:02:13 -0700 | [diff] [blame] | 214 | static const char *http_error_template = |
| 215 | "HTTP/1.1 %s\r\n" |
| 216 | "Content-Type: text/html\r\n" |
| 217 | "Expires: Mon, 11 Jan 1970 10:10:10 GMT\r\n" |
| 218 | "Connection: close\r\n" |
| 219 | "Pragma: no-cache\r\n" |
| 220 | "Content-Length: 0\r\n\r\n"; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 221 | |
| 222 | /* Header, including incantation to suppress favicon.ico requests */ |
Florin Coras | 898cd8f | 2018-06-08 02:02:13 -0700 | [diff] [blame] | 223 | static const char *html_header_template = |
| 224 | "<html><head><title>%v</title></head>" |
| 225 | "<link rel=\"icon\" href=\"data:,\">" |
| 226 | "<body><pre>"; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 227 | |
Florin Coras | 898cd8f | 2018-06-08 02:02:13 -0700 | [diff] [blame] | 228 | static const char *html_footer = |
| 229 | "</pre></body></html>\r\n"; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 230 | |
Florin Coras | 898cd8f | 2018-06-08 02:02:13 -0700 | [diff] [blame] | 231 | static const char *html_header_static = |
| 232 | "<html><head><title>static reply</title></head>" |
| 233 | "<link rel=\"icon\" href=\"data:,\">" |
| 234 | "<body><pre>hello</pre></body></html>\r\n"; |
| 235 | /* *INDENT-ON* */ |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 236 | |
| 237 | static u8 *static_http; |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 238 | static u8 *static_ok; |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 239 | |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 240 | static void |
| 241 | http_cli_output (uword arg, u8 * buffer, uword buffer_bytes) |
| 242 | { |
| 243 | u8 **output_vecp = (u8 **) arg; |
| 244 | u8 *output_vec; |
| 245 | u32 offset; |
| 246 | |
| 247 | output_vec = *output_vecp; |
| 248 | |
| 249 | offset = vec_len (output_vec); |
| 250 | vec_validate (output_vec, offset + buffer_bytes - 1); |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 251 | clib_memcpy_fast (output_vec + offset, buffer, buffer_bytes); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 252 | |
| 253 | *output_vecp = output_vec; |
| 254 | } |
| 255 | |
| 256 | void |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 257 | send_data (http_session_t * hs, u8 * data) |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 258 | { |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 259 | http_server_main_t *hsm = &http_server_main; |
| 260 | vnet_disconnect_args_t _a = { 0 }, *a = &_a; |
| 261 | vlib_main_t *vm = vlib_get_main (); |
| 262 | f64 last_sent_timer = vlib_time_now (vm); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 263 | u32 offset, bytes_to_send; |
| 264 | f64 delay = 10e-3; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 265 | |
| 266 | bytes_to_send = vec_len (data); |
| 267 | offset = 0; |
| 268 | |
| 269 | while (bytes_to_send > 0) |
| 270 | { |
| 271 | int actual_transfer; |
| 272 | |
| 273 | actual_transfer = svm_fifo_enqueue_nowait |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 274 | (hs->tx_fifo, bytes_to_send, data + offset); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 275 | |
| 276 | /* Made any progress? */ |
| 277 | if (actual_transfer <= 0) |
| 278 | { |
| 279 | vlib_process_suspend (vm, delay); |
| 280 | /* 10s deadman timer */ |
| 281 | if (vlib_time_now (vm) > last_sent_timer + 10.0) |
| 282 | { |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 283 | a->handle = hs->vpp_session_handle; |
| 284 | a->app_index = hsm->app_index; |
| 285 | vnet_disconnect_session (a); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 286 | break; |
| 287 | } |
| 288 | /* Exponential backoff, within reason */ |
| 289 | if (delay < 1.0) |
| 290 | delay = delay * 2.0; |
| 291 | } |
| 292 | else |
| 293 | { |
| 294 | last_sent_timer = vlib_time_now (vm); |
| 295 | offset += actual_transfer; |
| 296 | bytes_to_send -= actual_transfer; |
| 297 | |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 298 | if (svm_fifo_set_event (hs->tx_fifo)) |
| 299 | session_send_io_evt_to_thread (hs->tx_fifo, |
| 300 | SESSION_IO_EVT_TX_FLUSH); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 301 | delay = 10e-3; |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | static void |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 307 | send_error (http_session_t * hs, char *str) |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 308 | { |
| 309 | u8 *data; |
| 310 | |
| 311 | data = format (0, http_error_template, str); |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 312 | send_data (hs, data); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 313 | vec_free (data); |
| 314 | } |
| 315 | |
| 316 | static uword |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 317 | http_cli_process (vlib_main_t * vm, vlib_node_runtime_t * rt, |
| 318 | vlib_frame_t * f) |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 319 | { |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 320 | u8 *request = 0, *reply = 0, *http = 0, *html = 0; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 321 | http_server_main_t *hsm = &http_server_main; |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 322 | http_server_args **save_args; |
| 323 | http_server_args *args; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 324 | unformat_input_t input; |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 325 | http_session_t *hs; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 326 | int i; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 327 | |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 328 | save_args = vlib_node_get_runtime_data (hsm->vlib_main, rt->node_index); |
| 329 | args = *save_args; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 330 | |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 331 | http_server_sessions_reader_lock (); |
| 332 | |
| 333 | hs = http_server_session_get (args->thread_index, args->hs_index); |
| 334 | ASSERT (hs); |
| 335 | |
| 336 | request = hs->rx_buf; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 337 | if (vec_len (request) < 7) |
| 338 | { |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 339 | send_error (hs, "400 Bad Request"); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 340 | goto out; |
| 341 | } |
| 342 | |
| 343 | for (i = 0; i < vec_len (request) - 4; i++) |
| 344 | { |
| 345 | if (request[i] == 'G' && |
| 346 | request[i + 1] == 'E' && |
| 347 | request[i + 2] == 'T' && request[i + 3] == ' ') |
| 348 | goto found; |
| 349 | } |
| 350 | bad_request: |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 351 | send_error (hs, "400 Bad Request"); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 352 | goto out; |
| 353 | |
| 354 | found: |
| 355 | /* Lose "GET " */ |
| 356 | vec_delete (request, i + 5, 0); |
| 357 | |
| 358 | /* Replace slashes with spaces, stop at the end of the path */ |
| 359 | i = 0; |
| 360 | while (1) |
| 361 | { |
| 362 | if (request[i] == '/') |
| 363 | request[i] = ' '; |
| 364 | else if (request[i] == ' ') |
| 365 | { |
| 366 | /* vlib_cli_input is vector-based, no need for a NULL */ |
| 367 | _vec_len (request) = i; |
| 368 | break; |
| 369 | } |
| 370 | i++; |
| 371 | /* Should never happen */ |
| 372 | if (i == vec_len (request)) |
| 373 | goto bad_request; |
| 374 | } |
| 375 | |
| 376 | /* Generate the html header */ |
| 377 | html = format (0, html_header_template, request /* title */ ); |
| 378 | |
| 379 | /* Run the command */ |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 380 | unformat_init_vector (&input, vec_dup (request)); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 381 | vlib_cli_input (vm, &input, http_cli_output, (uword) & reply); |
| 382 | unformat_free (&input); |
| 383 | request = 0; |
| 384 | |
| 385 | /* Generate the html page */ |
| 386 | html = format (html, "%v", reply); |
| 387 | html = format (html, html_footer); |
| 388 | /* And the http reply */ |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 389 | http = format (0, http_ok, vec_len (http_ok)); |
| 390 | http = format (http, http_response, vec_len (html), html); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 391 | |
| 392 | /* Send it */ |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 393 | send_data (hs, http); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 394 | |
| 395 | out: |
| 396 | /* Cleanup */ |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 397 | http_server_sessions_reader_unlock (); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 398 | vec_free (reply); |
| 399 | vec_free (html); |
| 400 | vec_free (http); |
| 401 | |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 402 | http_process_free (args); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 403 | return (0); |
| 404 | } |
| 405 | |
| 406 | static void |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 407 | alloc_http_process (http_server_args * args) |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 408 | { |
| 409 | char *name; |
| 410 | vlib_node_t *n; |
| 411 | http_server_main_t *hsm = &http_server_main; |
| 412 | vlib_main_t *vm = hsm->vlib_main; |
| 413 | uword l = vec_len (hsm->free_http_cli_process_node_indices); |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 414 | http_server_args **save_args; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 415 | |
| 416 | if (vec_len (hsm->free_http_cli_process_node_indices) > 0) |
| 417 | { |
| 418 | n = vlib_get_node (vm, hsm->free_http_cli_process_node_indices[l - 1]); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 419 | vlib_node_set_state (vm, n->index, VLIB_NODE_STATE_POLLING); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 420 | _vec_len (hsm->free_http_cli_process_node_indices) = l - 1; |
| 421 | } |
| 422 | else |
| 423 | { |
| 424 | static vlib_node_registration_t r = { |
| 425 | .function = http_cli_process, |
| 426 | .type = VLIB_NODE_TYPE_PROCESS, |
| 427 | .process_log2_n_stack_bytes = 16, |
| 428 | .runtime_data_bytes = sizeof (void *), |
| 429 | }; |
| 430 | |
| 431 | name = (char *) format (0, "http-cli-%d", l); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 432 | r.name = name; |
| 433 | vlib_register_node (vm, &r); |
| 434 | vec_free (name); |
| 435 | |
| 436 | n = vlib_get_node (vm, r.index); |
| 437 | } |
| 438 | |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 439 | /* Save the node index in the args. It won't be zero. */ |
| 440 | args->node_index = n->index; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 441 | |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 442 | /* Save the args (pointer) in the node runtime */ |
| 443 | save_args = vlib_node_get_runtime_data (vm, n->index); |
| 444 | *save_args = args; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 445 | |
| 446 | vlib_start_process (vm, n->runtime_index); |
| 447 | } |
| 448 | |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 449 | static void |
| 450 | alloc_http_process_callback (void *cb_args) |
| 451 | { |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 452 | alloc_http_process ((http_server_args *) cb_args); |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | static int |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 456 | session_rx_request (http_session_t * hs) |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 457 | { |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 458 | u32 max_dequeue, cursize; |
| 459 | int n_read; |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 460 | |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 461 | cursize = vec_len (hs->rx_buf); |
| 462 | max_dequeue = svm_fifo_max_dequeue (hs->rx_fifo); |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 463 | if (PREDICT_FALSE (max_dequeue == 0)) |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 464 | return -1; |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 465 | |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 466 | vec_validate (hs->rx_buf, cursize + max_dequeue - 1); |
| 467 | n_read = app_recv_stream_raw (hs->rx_fifo, hs->rx_buf + cursize, |
| 468 | max_dequeue, 0, 0 /* peek */ ); |
| 469 | ASSERT (n_read == max_dequeue); |
| 470 | if (svm_fifo_is_empty (hs->rx_fifo)) |
| 471 | svm_fifo_unset_event (hs->rx_fifo); |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 472 | |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 473 | _vec_len (hs->rx_buf) = cursize + n_read; |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 474 | return 0; |
| 475 | } |
| 476 | |
| 477 | static int |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 478 | http_server_rx_callback (session_t * s) |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 479 | { |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 480 | http_server_args *args; |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 481 | http_session_t *hs; |
JingLiuZTE | 7cafe76 | 2017-11-08 15:01:27 +0800 | [diff] [blame] | 482 | int rv; |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 483 | |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 484 | http_server_sessions_reader_lock (); |
| 485 | |
| 486 | hs = http_server_session_lookup (s->thread_index, s->session_index); |
| 487 | if (!hs || hs->session_state != HTTP_STATE_ESTABLISHED) |
| 488 | return -1; |
| 489 | |
| 490 | rv = session_rx_request (hs); |
JingLiuZTE | 7cafe76 | 2017-11-08 15:01:27 +0800 | [diff] [blame] | 491 | if (rv) |
| 492 | return rv; |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 493 | |
| 494 | /* send the command to a new/recycled vlib process */ |
| 495 | args = clib_mem_alloc (sizeof (*args)); |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 496 | args->hs_index = hs->session_index; |
| 497 | args->thread_index = hs->thread_index; |
| 498 | |
| 499 | http_server_sessions_reader_unlock (); |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 500 | |
| 501 | /* Send an RPC request via the thread-0 input node */ |
| 502 | if (vlib_get_thread_index () != 0) |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 503 | session_send_rpc_evt_to_thread (0, alloc_http_process_callback, args); |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 504 | else |
| 505 | alloc_http_process (args); |
| 506 | return 0; |
| 507 | } |
| 508 | |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 509 | static int |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 510 | http_server_rx_callback_static (session_t * s) |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 511 | { |
| 512 | http_server_main_t *hsm = &http_server_main; |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 513 | vnet_disconnect_args_t _a = { 0 }, *a = &_a; |
| 514 | http_session_t *hs; |
| 515 | u32 request_len; |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 516 | u8 *request = 0; |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 517 | int i, rv; |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 518 | |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 519 | hs = http_server_session_lookup (s->thread_index, s->session_index); |
| 520 | if (!hs || hs->session_state == HTTP_STATE_CLOSED) |
| 521 | return 0; |
| 522 | |
| 523 | /* ok 200 was sent */ |
| 524 | if (hs->session_state == HTTP_STATE_OK_SENT) |
| 525 | goto send_data; |
| 526 | |
| 527 | rv = session_rx_request (hs); |
JingLiuZTE | 7cafe76 | 2017-11-08 15:01:27 +0800 | [diff] [blame] | 528 | if (rv) |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 529 | goto wait_for_data; |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 530 | |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 531 | request = hs->rx_buf; |
| 532 | request_len = vec_len (request); |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 533 | if (vec_len (request) < 7) |
| 534 | { |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 535 | send_error (hs, "400 Bad Request"); |
| 536 | goto close_session; |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 537 | } |
| 538 | |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 539 | for (i = 0; i < request_len - 4; i++) |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 540 | { |
| 541 | if (request[i] == 'G' && |
| 542 | request[i + 1] == 'E' && |
| 543 | request[i + 2] == 'T' && request[i + 3] == ' ') |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 544 | goto find_end; |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 545 | } |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 546 | send_error (hs, "400 Bad Request"); |
| 547 | goto close_session; |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 548 | |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 549 | find_end: |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 550 | |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 551 | /* check for the end sequence: /r/n/r/n */ |
| 552 | if (request[request_len - 1] != 0xa || request[request_len - 3] != 0xa |
| 553 | || request[request_len - 2] != 0xd || request[request_len - 4] != 0xd) |
| 554 | goto wait_for_data; |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 555 | |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 556 | /* send 200 OK first */ |
| 557 | send_data (hs, static_ok); |
| 558 | hs->session_state = HTTP_STATE_OK_SENT; |
| 559 | goto postpone; |
| 560 | |
| 561 | send_data: |
| 562 | send_data (hs, static_http); |
| 563 | http_server_session_cleanup (hs); |
| 564 | |
| 565 | close_session: |
| 566 | a->handle = session_handle (s); |
| 567 | a->app_index = hsm->app_index; |
| 568 | vnet_disconnect_session (a); |
| 569 | return 0; |
| 570 | |
| 571 | postpone: |
| 572 | svm_fifo_set_event (hs->rx_fifo); |
| 573 | session_send_io_evt_to_thread (hs->rx_fifo, FIFO_EVENT_BUILTIN_RX); |
| 574 | return 0; |
| 575 | |
| 576 | wait_for_data: |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 577 | return 0; |
| 578 | } |
| 579 | |
| 580 | static int |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 581 | http_server_session_accept_callback (session_t * s) |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 582 | { |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 583 | http_server_main_t *hsm = &http_server_main; |
| 584 | http_session_t *hs; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 585 | |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 586 | hsm->vpp_queue[s->thread_index] = |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 587 | session_manager_get_vpp_event_queue (s->thread_index); |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 588 | |
| 589 | if (!hsm->is_static) |
| 590 | http_server_sessions_writer_lock (); |
| 591 | |
| 592 | hs = http_server_session_alloc (s->thread_index); |
| 593 | http_server_session_lookup_add (s->thread_index, s->session_index, |
| 594 | hs->session_index); |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 595 | hs->rx_fifo = s->rx_fifo; |
| 596 | hs->tx_fifo = s->tx_fifo; |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 597 | hs->vpp_session_index = s->session_index; |
| 598 | hs->vpp_session_handle = session_handle (s); |
| 599 | hs->session_state = HTTP_STATE_ESTABLISHED; |
| 600 | |
| 601 | if (!hsm->is_static) |
| 602 | http_server_sessions_writer_unlock (); |
| 603 | |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 604 | s->session_state = SESSION_STATE_READY; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 605 | return 0; |
| 606 | } |
| 607 | |
| 608 | static void |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 609 | http_server_session_disconnect_callback (session_t * s) |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 610 | { |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 611 | http_server_main_t *hsm = &http_server_main; |
Florin Coras | 4af830c | 2018-12-04 09:21:36 -0800 | [diff] [blame] | 612 | vnet_disconnect_args_t _a = { 0 }, *a = &_a; |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 613 | http_session_t *hs; |
| 614 | |
| 615 | if (!hsm->is_static) |
| 616 | http_server_sessions_writer_lock (); |
| 617 | |
| 618 | hs = http_server_session_lookup (s->thread_index, s->session_index); |
| 619 | http_server_session_cleanup (hs); |
| 620 | |
| 621 | if (!hsm->is_static) |
| 622 | http_server_sessions_writer_unlock (); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 623 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 624 | a->handle = session_handle (s); |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 625 | a->app_index = hsm->app_index; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 626 | vnet_disconnect_session (a); |
| 627 | } |
| 628 | |
| 629 | static void |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 630 | http_server_session_reset_callback (session_t * s) |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 631 | { |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 632 | http_server_main_t *hsm = &http_server_main; |
Florin Coras | 4af830c | 2018-12-04 09:21:36 -0800 | [diff] [blame] | 633 | vnet_disconnect_args_t _a = { 0 }, *a = &_a; |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 634 | http_session_t *hs; |
| 635 | |
| 636 | if (!hsm->is_static) |
| 637 | http_server_sessions_writer_lock (); |
| 638 | |
| 639 | hs = http_server_session_lookup (s->thread_index, s->session_index); |
| 640 | http_server_session_cleanup (hs); |
| 641 | |
| 642 | if (!hsm->is_static) |
| 643 | http_server_sessions_writer_unlock (); |
| 644 | |
Florin Coras | 4af830c | 2018-12-04 09:21:36 -0800 | [diff] [blame] | 645 | a->handle = session_handle (s); |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 646 | a->app_index = hsm->app_index; |
Florin Coras | 4af830c | 2018-12-04 09:21:36 -0800 | [diff] [blame] | 647 | vnet_disconnect_session (a); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 648 | } |
| 649 | |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 650 | static int |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 651 | http_server_session_connected_callback (u32 app_index, u32 api_context, |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 652 | session_t * s, u8 is_fail) |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 653 | { |
| 654 | clib_warning ("called..."); |
| 655 | return -1; |
| 656 | } |
| 657 | |
| 658 | static int |
Florin Coras | fa76a76 | 2018-11-29 12:40:10 -0800 | [diff] [blame] | 659 | http_server_add_segment_callback (u32 client_index, u64 segment_handle) |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 660 | { |
| 661 | clib_warning ("called..."); |
| 662 | return -1; |
| 663 | } |
| 664 | |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 665 | static session_cb_vft_t http_server_session_cb_vft = { |
| 666 | .session_accept_callback = http_server_session_accept_callback, |
| 667 | .session_disconnect_callback = http_server_session_disconnect_callback, |
| 668 | .session_connected_callback = http_server_session_connected_callback, |
| 669 | .add_segment_callback = http_server_add_segment_callback, |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 670 | .builtin_app_rx_callback = http_server_rx_callback, |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 671 | .session_reset_callback = http_server_session_reset_callback |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 672 | }; |
| 673 | |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 674 | static int |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 675 | http_server_attach () |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 676 | { |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 677 | vnet_app_add_tls_cert_args_t _a_cert, *a_cert = &_a_cert; |
| 678 | vnet_app_add_tls_key_args_t _a_key, *a_key = &_a_key; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 679 | http_server_main_t *hsm = &http_server_main; |
Florin Coras | ff6e769 | 2017-12-11 04:59:01 -0800 | [diff] [blame] | 680 | u64 options[APP_OPTIONS_N_OPTIONS]; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 681 | vnet_app_attach_args_t _a, *a = &_a; |
Florin Coras | ff6e769 | 2017-12-11 04:59:01 -0800 | [diff] [blame] | 682 | u32 segment_size = 128 << 20; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 683 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 684 | clib_memset (a, 0, sizeof (*a)); |
| 685 | clib_memset (options, 0, sizeof (options)); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 686 | |
Florin Coras | ff6e769 | 2017-12-11 04:59:01 -0800 | [diff] [blame] | 687 | if (hsm->private_segment_size) |
| 688 | segment_size = hsm->private_segment_size; |
| 689 | |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 690 | a->api_client_index = ~0; |
| 691 | a->name = format (0, "test_http_server"); |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 692 | a->session_cb_vft = &http_server_session_cb_vft; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 693 | a->options = options; |
Florin Coras | ff6e769 | 2017-12-11 04:59:01 -0800 | [diff] [blame] | 694 | a->options[APP_OPTIONS_SEGMENT_SIZE] = segment_size; |
| 695 | a->options[APP_OPTIONS_RX_FIFO_SIZE] = |
Florin Coras | 1d6d085 | 2017-11-17 14:26:01 -0800 | [diff] [blame] | 696 | hsm->fifo_size ? hsm->fifo_size : 8 << 10; |
Florin Coras | ff6e769 | 2017-12-11 04:59:01 -0800 | [diff] [blame] | 697 | a->options[APP_OPTIONS_TX_FIFO_SIZE] = |
Florin Coras | 1d6d085 | 2017-11-17 14:26:01 -0800 | [diff] [blame] | 698 | hsm->fifo_size ? hsm->fifo_size : 32 << 10; |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 699 | a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN; |
Florin Coras | 1d6d085 | 2017-11-17 14:26:01 -0800 | [diff] [blame] | 700 | a->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = hsm->prealloc_fifos; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 701 | |
| 702 | if (vnet_application_attach (a)) |
| 703 | { |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 704 | vec_free (a->name); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 705 | clib_warning ("failed to attach server"); |
| 706 | return -1; |
| 707 | } |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 708 | vec_free (a->name); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 709 | hsm->app_index = a->app_index; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 710 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 711 | clib_memset (a_cert, 0, sizeof (*a_cert)); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 712 | a_cert->app_index = a->app_index; |
| 713 | vec_validate (a_cert->cert, test_srv_crt_rsa_len); |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 714 | 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] | 715 | vnet_app_add_tls_cert (a_cert); |
| 716 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 717 | clib_memset (a_key, 0, sizeof (*a_key)); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 718 | a_key->app_index = a->app_index; |
| 719 | vec_validate (a_key->key, test_srv_key_rsa_len); |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 720 | 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] | 721 | vnet_app_add_tls_key (a_key); |
| 722 | |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 723 | return 0; |
| 724 | } |
| 725 | |
| 726 | static int |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 727 | http_server_listen () |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 728 | { |
| 729 | http_server_main_t *hsm = &http_server_main; |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame^] | 730 | vnet_listen_args_t _a, *a = &_a; |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 731 | clib_memset (a, 0, sizeof (*a)); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 732 | a->app_index = hsm->app_index; |
| 733 | a->uri = "tcp://0.0.0.0/80"; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 734 | if (hsm->uri) |
| 735 | a->uri = (char *) hsm->uri; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 736 | return vnet_bind_uri (a); |
| 737 | } |
| 738 | |
| 739 | static int |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 740 | http_server_create (vlib_main_t * vm) |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 741 | { |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 742 | vlib_thread_main_t *vtm = vlib_get_thread_main (); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 743 | http_server_main_t *hsm = &http_server_main; |
| 744 | u32 num_threads; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 745 | |
| 746 | num_threads = 1 /* main thread */ + vtm->n_threads; |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 747 | vec_validate (hsm->vpp_queue, num_threads - 1); |
| 748 | vec_validate (hsm->sessions, num_threads - 1); |
| 749 | vec_validate (hsm->session_to_http_session, num_threads - 1); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 750 | |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 751 | clib_rwlock_init (&hsm->sessions_lock); |
| 752 | |
| 753 | if (http_server_attach ()) |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 754 | { |
| 755 | clib_warning ("failed to attach server"); |
| 756 | return -1; |
| 757 | } |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 758 | if (http_server_listen ()) |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 759 | { |
| 760 | clib_warning ("failed to start listening"); |
| 761 | return -1; |
| 762 | } |
| 763 | return 0; |
| 764 | } |
| 765 | |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 766 | static clib_error_t * |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 767 | http_server_create_command_fn (vlib_main_t * vm, |
| 768 | unformat_input_t * input, |
| 769 | vlib_cli_command_t * cmd) |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 770 | { |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 771 | http_server_main_t *hsm = &http_server_main; |
Florin Coras | 1d6d085 | 2017-11-17 14:26:01 -0800 | [diff] [blame] | 772 | u64 seg_size; |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 773 | u8 *html; |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 774 | int rv; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 775 | |
Florin Coras | 1d6d085 | 2017-11-17 14:26:01 -0800 | [diff] [blame] | 776 | hsm->prealloc_fifos = 0; |
| 777 | hsm->private_segment_size = 0; |
| 778 | hsm->fifo_size = 0; |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 779 | hsm->is_static = 0; |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 780 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 781 | { |
| 782 | if (unformat (input, "static")) |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 783 | hsm->is_static = 1; |
Florin Coras | 1d6d085 | 2017-11-17 14:26:01 -0800 | [diff] [blame] | 784 | else if (unformat (input, "prealloc-fifos %d", &hsm->prealloc_fifos)) |
| 785 | ; |
| 786 | else if (unformat (input, "private-segment-size %U", |
| 787 | unformat_memory_size, &seg_size)) |
| 788 | { |
| 789 | if (seg_size >= 0x100000000ULL) |
| 790 | { |
| 791 | vlib_cli_output (vm, "private segment size %llu, too large", |
| 792 | seg_size); |
| 793 | return 0; |
| 794 | } |
| 795 | hsm->private_segment_size = seg_size; |
| 796 | } |
| 797 | else if (unformat (input, "fifo-size %d", &hsm->fifo_size)) |
| 798 | hsm->fifo_size <<= 10; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 799 | else if (unformat (input, "uri %s", &hsm->uri)) |
| 800 | ; |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 801 | else |
| 802 | return clib_error_return (0, "unknown input `%U'", |
| 803 | format_unformat_error, input); |
| 804 | } |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 805 | if (hsm->my_client_index != (u32) ~ 0) |
| 806 | return clib_error_return (0, "test http server is already running"); |
| 807 | |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 808 | vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */ ); |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 809 | |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 810 | if (hsm->is_static) |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 811 | { |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 812 | http_server_session_cb_vft.builtin_app_rx_callback = |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 813 | http_server_rx_callback_static; |
| 814 | html = format (0, html_header_static); |
| 815 | static_http = format (0, http_response, vec_len (html), html); |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 816 | static_ok = format (0, http_ok); |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 817 | } |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 818 | rv = http_server_create (vm); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 819 | switch (rv) |
| 820 | { |
| 821 | case 0: |
| 822 | break; |
| 823 | default: |
| 824 | return clib_error_return (0, "server_create returned %d", rv); |
| 825 | } |
| 826 | return 0; |
| 827 | } |
| 828 | |
| 829 | /* *INDENT-OFF* */ |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 830 | VLIB_CLI_COMMAND (http_server_create_command, static) = |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 831 | { |
| 832 | .path = "test http server", |
| 833 | .short_help = "test http server", |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 834 | .function = http_server_create_command_fn, |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 835 | }; |
| 836 | /* *INDENT-ON* */ |
| 837 | |
| 838 | static clib_error_t * |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 839 | http_server_main_init (vlib_main_t * vm) |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 840 | { |
| 841 | http_server_main_t *hsm = &http_server_main; |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 842 | |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 843 | hsm->my_client_index = ~0; |
| 844 | hsm->vlib_main = vm; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 845 | return 0; |
| 846 | } |
| 847 | |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 848 | VLIB_INIT_FUNCTION (http_server_main_init); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 849 | |
| 850 | /* |
| 851 | * fd.io coding-style-patch-verification: ON |
| 852 | * |
| 853 | * Local Variables: |
| 854 | * eval: (c-set-style "gnu") |
| 855 | * End: |
| 856 | */ |