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 | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 27 | u64 session_handle; |
| 28 | u64 node_index; |
| 29 | u8 *data; |
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 | |
| 32 | typedef struct |
| 33 | { |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 34 | u8 **rx_buf; |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 35 | svm_queue_t **vpp_queue; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 36 | u64 byte_index; |
| 37 | |
| 38 | uword *handler_by_get_request; |
| 39 | |
| 40 | u32 *free_http_cli_process_node_indices; |
| 41 | |
| 42 | /* Sever's event queue */ |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 43 | svm_queue_t *vl_input_queue; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 44 | |
| 45 | /* API client handle */ |
| 46 | u32 my_client_index; |
| 47 | |
| 48 | u32 app_index; |
| 49 | |
| 50 | /* process node index for evnt scheduling */ |
| 51 | u32 node_index; |
Florin Coras | 1d6d085 | 2017-11-17 14:26:01 -0800 | [diff] [blame] | 52 | |
| 53 | u32 prealloc_fifos; |
| 54 | u32 private_segment_size; |
| 55 | u32 fifo_size; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 56 | u8 *uri; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 57 | vlib_main_t *vlib_main; |
| 58 | } http_server_main_t; |
| 59 | |
| 60 | http_server_main_t http_server_main; |
| 61 | |
| 62 | static void |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 63 | free_http_process (http_server_args * args) |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 64 | { |
| 65 | vlib_node_runtime_t *rt; |
| 66 | vlib_main_t *vm = &vlib_global_main; |
| 67 | http_server_main_t *hsm = &http_server_main; |
| 68 | vlib_node_t *n; |
| 69 | u32 node_index; |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 70 | http_server_args **save_args; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 71 | |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 72 | node_index = args->node_index; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 73 | ASSERT (node_index != 0); |
| 74 | |
| 75 | n = vlib_get_node (vm, node_index); |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 76 | rt = vlib_node_get_runtime (vm, n->index); |
| 77 | save_args = vlib_node_get_runtime_data (vm, n->index); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 78 | |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 79 | /* Reset process session pointer */ |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 80 | clib_mem_free (*save_args); |
| 81 | *save_args = 0; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 82 | |
| 83 | /* Turn off the process node */ |
| 84 | vlib_node_set_state (vm, rt->node_index, VLIB_NODE_STATE_DISABLED); |
| 85 | |
| 86 | /* add node index to the freelist */ |
| 87 | vec_add1 (hsm->free_http_cli_process_node_indices, node_index); |
| 88 | } |
| 89 | |
Florin Coras | 898cd8f | 2018-06-08 02:02:13 -0700 | [diff] [blame^] | 90 | /* *INDENT-OFF* */ |
| 91 | static const char *http_response = |
| 92 | "HTTP/1.1 200 OK\r\n" |
| 93 | "Content-Type: text/html\r\n" |
| 94 | "Expires: Mon, 11 Jan 1970 10:10:10 GMT\r\n" |
| 95 | "Connection: keep-alive \r\n" |
| 96 | "Pragma: no-cache\r\n" |
| 97 | "Content-Length: %d\r\n\r\n%s"; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 98 | |
Florin Coras | 898cd8f | 2018-06-08 02:02:13 -0700 | [diff] [blame^] | 99 | static const char *http_error_template = |
| 100 | "HTTP/1.1 %s\r\n" |
| 101 | "Content-Type: text/html\r\n" |
| 102 | "Expires: Mon, 11 Jan 1970 10:10:10 GMT\r\n" |
| 103 | "Connection: close\r\n" |
| 104 | "Pragma: no-cache\r\n" |
| 105 | "Content-Length: 0\r\n\r\n"; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 106 | |
| 107 | /* Header, including incantation to suppress favicon.ico requests */ |
Florin Coras | 898cd8f | 2018-06-08 02:02:13 -0700 | [diff] [blame^] | 108 | static const char *html_header_template = |
| 109 | "<html><head><title>%v</title></head>" |
| 110 | "<link rel=\"icon\" href=\"data:,\">" |
| 111 | "<body><pre>"; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 112 | |
Florin Coras | 898cd8f | 2018-06-08 02:02:13 -0700 | [diff] [blame^] | 113 | static const char *html_footer = |
| 114 | "</pre></body></html>\r\n"; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 115 | |
Florin Coras | 898cd8f | 2018-06-08 02:02:13 -0700 | [diff] [blame^] | 116 | static const char *html_header_static = |
| 117 | "<html><head><title>static reply</title></head>" |
| 118 | "<link rel=\"icon\" href=\"data:,\">" |
| 119 | "<body><pre>hello</pre></body></html>\r\n"; |
| 120 | /* *INDENT-ON* */ |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 121 | |
| 122 | static u8 *static_http; |
| 123 | |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 124 | static void |
| 125 | http_cli_output (uword arg, u8 * buffer, uword buffer_bytes) |
| 126 | { |
| 127 | u8 **output_vecp = (u8 **) arg; |
| 128 | u8 *output_vec; |
| 129 | u32 offset; |
| 130 | |
| 131 | output_vec = *output_vecp; |
| 132 | |
| 133 | offset = vec_len (output_vec); |
| 134 | vec_validate (output_vec, offset + buffer_bytes - 1); |
| 135 | clib_memcpy (output_vec + offset, buffer, buffer_bytes); |
| 136 | |
| 137 | *output_vecp = output_vec; |
| 138 | } |
| 139 | |
| 140 | void |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 141 | send_data (stream_session_t * s, u8 * data) |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 142 | { |
| 143 | session_fifo_event_t evt; |
| 144 | u32 offset, bytes_to_send; |
| 145 | f64 delay = 10e-3; |
| 146 | http_server_main_t *hsm = &http_server_main; |
| 147 | vlib_main_t *vm = hsm->vlib_main; |
| 148 | f64 last_sent_timer = vlib_time_now (vm); |
| 149 | |
| 150 | bytes_to_send = vec_len (data); |
| 151 | offset = 0; |
| 152 | |
| 153 | while (bytes_to_send > 0) |
| 154 | { |
| 155 | int actual_transfer; |
| 156 | |
| 157 | actual_transfer = svm_fifo_enqueue_nowait |
| 158 | (s->server_tx_fifo, bytes_to_send, data + offset); |
| 159 | |
| 160 | /* Made any progress? */ |
| 161 | if (actual_transfer <= 0) |
| 162 | { |
| 163 | vlib_process_suspend (vm, delay); |
| 164 | /* 10s deadman timer */ |
| 165 | if (vlib_time_now (vm) > last_sent_timer + 10.0) |
| 166 | { |
| 167 | /* $$$$ FC: reset transport session here? */ |
| 168 | break; |
| 169 | } |
| 170 | /* Exponential backoff, within reason */ |
| 171 | if (delay < 1.0) |
| 172 | delay = delay * 2.0; |
| 173 | } |
| 174 | else |
| 175 | { |
| 176 | last_sent_timer = vlib_time_now (vm); |
| 177 | offset += actual_transfer; |
| 178 | bytes_to_send -= actual_transfer; |
| 179 | |
| 180 | if (svm_fifo_set_event (s->server_tx_fifo)) |
| 181 | { |
| 182 | /* Fabricate TX event, send to vpp */ |
| 183 | evt.fifo = s->server_tx_fifo; |
| 184 | evt.event_type = FIFO_EVENT_APP_TX; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 185 | |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 186 | svm_queue_add (hsm->vpp_queue[s->thread_index], |
| 187 | (u8 *) & evt, 0 /* do wait for mutex */ ); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 188 | } |
| 189 | delay = 10e-3; |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | static void |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 195 | send_error (stream_session_t * s, char *str) |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 196 | { |
| 197 | u8 *data; |
| 198 | |
| 199 | data = format (0, http_error_template, str); |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 200 | send_data (s, data); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 201 | vec_free (data); |
| 202 | } |
| 203 | |
| 204 | static uword |
| 205 | http_cli_process (vlib_main_t * vm, |
| 206 | vlib_node_runtime_t * rt, vlib_frame_t * f) |
| 207 | { |
| 208 | http_server_main_t *hsm = &http_server_main; |
| 209 | u8 *request = 0, *reply = 0; |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 210 | http_server_args **save_args; |
| 211 | http_server_args *args; |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 212 | stream_session_t *s; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 213 | unformat_input_t input; |
| 214 | int i; |
| 215 | u8 *http = 0, *html = 0; |
| 216 | |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 217 | save_args = vlib_node_get_runtime_data (hsm->vlib_main, rt->node_index); |
| 218 | args = *save_args; |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 219 | s = session_get_from_handle (args->session_handle); |
| 220 | ASSERT (s); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 221 | |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 222 | request = (u8 *) (void *) (args->data); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 223 | if (vec_len (request) < 7) |
| 224 | { |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 225 | send_error (s, "400 Bad Request"); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 226 | goto out; |
| 227 | } |
| 228 | |
| 229 | for (i = 0; i < vec_len (request) - 4; i++) |
| 230 | { |
| 231 | if (request[i] == 'G' && |
| 232 | request[i + 1] == 'E' && |
| 233 | request[i + 2] == 'T' && request[i + 3] == ' ') |
| 234 | goto found; |
| 235 | } |
| 236 | bad_request: |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 237 | send_error (s, "400 Bad Request"); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 238 | goto out; |
| 239 | |
| 240 | found: |
| 241 | /* Lose "GET " */ |
| 242 | vec_delete (request, i + 5, 0); |
| 243 | |
| 244 | /* Replace slashes with spaces, stop at the end of the path */ |
| 245 | i = 0; |
| 246 | while (1) |
| 247 | { |
| 248 | if (request[i] == '/') |
| 249 | request[i] = ' '; |
| 250 | else if (request[i] == ' ') |
| 251 | { |
| 252 | /* vlib_cli_input is vector-based, no need for a NULL */ |
| 253 | _vec_len (request) = i; |
| 254 | break; |
| 255 | } |
| 256 | i++; |
| 257 | /* Should never happen */ |
| 258 | if (i == vec_len (request)) |
| 259 | goto bad_request; |
| 260 | } |
| 261 | |
| 262 | /* Generate the html header */ |
| 263 | html = format (0, html_header_template, request /* title */ ); |
| 264 | |
| 265 | /* Run the command */ |
| 266 | unformat_init_vector (&input, request); |
| 267 | vlib_cli_input (vm, &input, http_cli_output, (uword) & reply); |
| 268 | unformat_free (&input); |
| 269 | request = 0; |
| 270 | |
| 271 | /* Generate the html page */ |
| 272 | html = format (html, "%v", reply); |
| 273 | html = format (html, html_footer); |
| 274 | /* And the http reply */ |
| 275 | http = format (0, http_response, vec_len (html), html); |
| 276 | |
| 277 | /* Send it */ |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 278 | send_data (s, http); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 279 | |
| 280 | out: |
| 281 | /* Cleanup */ |
| 282 | vec_free (request); |
| 283 | vec_free (reply); |
| 284 | vec_free (html); |
| 285 | vec_free (http); |
| 286 | |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 287 | free_http_process (args); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 288 | return (0); |
| 289 | } |
| 290 | |
| 291 | static void |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 292 | alloc_http_process (http_server_args * args) |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 293 | { |
| 294 | char *name; |
| 295 | vlib_node_t *n; |
| 296 | http_server_main_t *hsm = &http_server_main; |
| 297 | vlib_main_t *vm = hsm->vlib_main; |
| 298 | uword l = vec_len (hsm->free_http_cli_process_node_indices); |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 299 | http_server_args **save_args; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 300 | |
| 301 | if (vec_len (hsm->free_http_cli_process_node_indices) > 0) |
| 302 | { |
| 303 | 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] | 304 | vlib_node_set_state (vm, n->index, VLIB_NODE_STATE_POLLING); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 305 | _vec_len (hsm->free_http_cli_process_node_indices) = l - 1; |
| 306 | } |
| 307 | else |
| 308 | { |
| 309 | static vlib_node_registration_t r = { |
| 310 | .function = http_cli_process, |
| 311 | .type = VLIB_NODE_TYPE_PROCESS, |
| 312 | .process_log2_n_stack_bytes = 16, |
| 313 | .runtime_data_bytes = sizeof (void *), |
| 314 | }; |
| 315 | |
| 316 | name = (char *) format (0, "http-cli-%d", l); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 317 | r.name = name; |
| 318 | vlib_register_node (vm, &r); |
| 319 | vec_free (name); |
| 320 | |
| 321 | n = vlib_get_node (vm, r.index); |
| 322 | } |
| 323 | |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 324 | /* Save the node index in the args. It won't be zero. */ |
| 325 | args->node_index = n->index; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 326 | |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 327 | /* Save the args (pointer) in the node runtime */ |
| 328 | save_args = vlib_node_get_runtime_data (vm, n->index); |
| 329 | *save_args = args; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 330 | |
| 331 | vlib_start_process (vm, n->runtime_index); |
| 332 | } |
| 333 | |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 334 | static void |
| 335 | alloc_http_process_callback (void *cb_args) |
| 336 | { |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 337 | alloc_http_process ((http_server_args *) cb_args); |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | static int |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 341 | session_rx_request (stream_session_t * s) |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 342 | { |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 343 | http_server_main_t *hsm = &http_server_main; |
| 344 | svm_fifo_t *rx_fifo; |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 345 | u32 max_dequeue; |
| 346 | int actual_transfer; |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 347 | |
| 348 | rx_fifo = s->server_rx_fifo; |
| 349 | max_dequeue = svm_fifo_max_dequeue (rx_fifo); |
| 350 | svm_fifo_unset_event (rx_fifo); |
| 351 | if (PREDICT_FALSE (max_dequeue == 0)) |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 352 | return -1; |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 353 | |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 354 | vec_validate (hsm->rx_buf[s->thread_index], max_dequeue - 1); |
| 355 | _vec_len (hsm->rx_buf[s->thread_index]) = max_dequeue; |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 356 | |
| 357 | actual_transfer = svm_fifo_dequeue_nowait (rx_fifo, max_dequeue, |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 358 | hsm->rx_buf[s->thread_index]); |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 359 | ASSERT (actual_transfer > 0); |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 360 | _vec_len (hsm->rx_buf[s->thread_index]) = actual_transfer; |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | static int |
| 365 | http_server_rx_callback (stream_session_t * s) |
| 366 | { |
| 367 | http_server_main_t *hsm = &http_server_main; |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 368 | http_server_args *args; |
JingLiuZTE | 7cafe76 | 2017-11-08 15:01:27 +0800 | [diff] [blame] | 369 | int rv; |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 370 | |
JingLiuZTE | 7cafe76 | 2017-11-08 15:01:27 +0800 | [diff] [blame] | 371 | rv = session_rx_request (s); |
| 372 | if (rv) |
| 373 | return rv; |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 374 | |
| 375 | /* send the command to a new/recycled vlib process */ |
| 376 | args = clib_mem_alloc (sizeof (*args)); |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 377 | args->data = vec_dup (hsm->rx_buf[s->thread_index]); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 378 | args->session_handle = session_handle (s); |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 379 | |
| 380 | /* Send an RPC request via the thread-0 input node */ |
| 381 | if (vlib_get_thread_index () != 0) |
| 382 | { |
| 383 | session_fifo_event_t evt; |
| 384 | evt.rpc_args.fp = alloc_http_process_callback; |
| 385 | evt.rpc_args.arg = args; |
| 386 | evt.event_type = FIFO_EVENT_RPC; |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 387 | svm_queue_add |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 388 | (session_manager_get_vpp_event_queue (0 /* main thread */ ), |
| 389 | (u8 *) & evt, 0 /* do wait for mutex */ ); |
| 390 | } |
| 391 | else |
| 392 | alloc_http_process (args); |
| 393 | return 0; |
| 394 | } |
| 395 | |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 396 | static int |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 397 | http_server_rx_callback_static (stream_session_t * s) |
| 398 | { |
| 399 | http_server_main_t *hsm = &http_server_main; |
| 400 | u8 *request = 0; |
| 401 | int i; |
JingLiuZTE | 7cafe76 | 2017-11-08 15:01:27 +0800 | [diff] [blame] | 402 | int rv; |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 403 | |
JingLiuZTE | 7cafe76 | 2017-11-08 15:01:27 +0800 | [diff] [blame] | 404 | rv = session_rx_request (s); |
| 405 | if (rv) |
| 406 | return rv; |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 407 | |
| 408 | request = hsm->rx_buf[s->thread_index]; |
| 409 | if (vec_len (request) < 7) |
| 410 | { |
| 411 | send_error (s, "400 Bad Request"); |
| 412 | goto out; |
| 413 | } |
| 414 | |
| 415 | for (i = 0; i < vec_len (request) - 4; i++) |
| 416 | { |
| 417 | if (request[i] == 'G' && |
| 418 | request[i + 1] == 'E' && |
| 419 | request[i + 2] == 'T' && request[i + 3] == ' ') |
| 420 | goto found; |
| 421 | } |
| 422 | send_error (s, "400 Bad Request"); |
| 423 | goto out; |
| 424 | |
| 425 | found: |
| 426 | |
| 427 | /* Send it */ |
| 428 | send_data (s, static_http); |
| 429 | |
| 430 | out: |
| 431 | /* Cleanup */ |
| 432 | vec_free (request); |
Florin Coras | 156e5ca | 2017-11-13 12:07:38 -0800 | [diff] [blame] | 433 | hsm->rx_buf[s->thread_index] = request; |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 434 | return 0; |
| 435 | } |
| 436 | |
| 437 | static int |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 438 | http_server_session_accept_callback (stream_session_t * s) |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 439 | { |
| 440 | http_server_main_t *bsm = &http_server_main; |
| 441 | |
| 442 | bsm->vpp_queue[s->thread_index] = |
| 443 | session_manager_get_vpp_event_queue (s->thread_index); |
| 444 | s->session_state = SESSION_STATE_READY; |
| 445 | bsm->byte_index = 0; |
| 446 | return 0; |
| 447 | } |
| 448 | |
| 449 | static void |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 450 | http_server_session_disconnect_callback (stream_session_t * s) |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 451 | { |
| 452 | http_server_main_t *bsm = &http_server_main; |
| 453 | vnet_disconnect_args_t _a, *a = &_a; |
| 454 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 455 | a->handle = session_handle (s); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 456 | a->app_index = bsm->app_index; |
| 457 | vnet_disconnect_session (a); |
| 458 | } |
| 459 | |
| 460 | static void |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 461 | http_server_session_reset_callback (stream_session_t * s) |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 462 | { |
| 463 | clib_warning ("called.. "); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 464 | stream_session_cleanup (s); |
| 465 | } |
| 466 | |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 467 | static int |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 468 | http_server_session_connected_callback (u32 app_index, u32 api_context, |
| 469 | stream_session_t * s, u8 is_fail) |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 470 | { |
| 471 | clib_warning ("called..."); |
| 472 | return -1; |
| 473 | } |
| 474 | |
| 475 | static int |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 476 | http_server_add_segment_callback (u32 client_index, const ssvm_private_t * sp) |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 477 | { |
| 478 | clib_warning ("called..."); |
| 479 | return -1; |
| 480 | } |
| 481 | |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 482 | static session_cb_vft_t http_server_session_cb_vft = { |
| 483 | .session_accept_callback = http_server_session_accept_callback, |
| 484 | .session_disconnect_callback = http_server_session_disconnect_callback, |
| 485 | .session_connected_callback = http_server_session_connected_callback, |
| 486 | .add_segment_callback = http_server_add_segment_callback, |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 487 | .builtin_app_rx_callback = http_server_rx_callback, |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 488 | .session_reset_callback = http_server_session_reset_callback |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 489 | }; |
| 490 | |
| 491 | /* Abuse VPP's input queue */ |
| 492 | static int |
| 493 | create_api_loopback (vlib_main_t * vm) |
| 494 | { |
| 495 | http_server_main_t *hsm = &http_server_main; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 496 | api_main_t *am = &api_main; |
| 497 | vl_shmem_hdr_t *shmem_hdr; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 498 | |
| 499 | shmem_hdr = am->shmem_hdr; |
| 500 | hsm->vl_input_queue = shmem_hdr->vl_input_queue; |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 501 | hsm->my_client_index = |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 502 | vl_api_memclnt_create_internal ("http_server", hsm->vl_input_queue); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 503 | return 0; |
| 504 | } |
| 505 | |
| 506 | static int |
| 507 | server_attach () |
| 508 | { |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 509 | vnet_app_add_tls_cert_args_t _a_cert, *a_cert = &_a_cert; |
| 510 | 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] | 511 | http_server_main_t *hsm = &http_server_main; |
Florin Coras | ff6e769 | 2017-12-11 04:59:01 -0800 | [diff] [blame] | 512 | u64 options[APP_OPTIONS_N_OPTIONS]; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 513 | vnet_app_attach_args_t _a, *a = &_a; |
Florin Coras | ff6e769 | 2017-12-11 04:59:01 -0800 | [diff] [blame] | 514 | u32 segment_size = 128 << 20; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 515 | |
| 516 | memset (a, 0, sizeof (*a)); |
| 517 | memset (options, 0, sizeof (options)); |
| 518 | |
Florin Coras | ff6e769 | 2017-12-11 04:59:01 -0800 | [diff] [blame] | 519 | if (hsm->private_segment_size) |
| 520 | segment_size = hsm->private_segment_size; |
| 521 | |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 522 | a->api_client_index = hsm->my_client_index; |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 523 | a->session_cb_vft = &http_server_session_cb_vft; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 524 | a->options = options; |
Florin Coras | ff6e769 | 2017-12-11 04:59:01 -0800 | [diff] [blame] | 525 | a->options[APP_OPTIONS_SEGMENT_SIZE] = segment_size; |
| 526 | a->options[APP_OPTIONS_RX_FIFO_SIZE] = |
Florin Coras | 1d6d085 | 2017-11-17 14:26:01 -0800 | [diff] [blame] | 527 | hsm->fifo_size ? hsm->fifo_size : 8 << 10; |
Florin Coras | ff6e769 | 2017-12-11 04:59:01 -0800 | [diff] [blame] | 528 | a->options[APP_OPTIONS_TX_FIFO_SIZE] = |
Florin Coras | 1d6d085 | 2017-11-17 14:26:01 -0800 | [diff] [blame] | 529 | hsm->fifo_size ? hsm->fifo_size : 32 << 10; |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 530 | a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN; |
Florin Coras | 1d6d085 | 2017-11-17 14:26:01 -0800 | [diff] [blame] | 531 | a->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = hsm->prealloc_fifos; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 532 | |
| 533 | if (vnet_application_attach (a)) |
| 534 | { |
| 535 | clib_warning ("failed to attach server"); |
| 536 | return -1; |
| 537 | } |
| 538 | hsm->app_index = a->app_index; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 539 | |
| 540 | memset (a_cert, 0, sizeof (*a_cert)); |
| 541 | a_cert->app_index = a->app_index; |
| 542 | vec_validate (a_cert->cert, test_srv_crt_rsa_len); |
| 543 | clib_memcpy (a_cert->cert, test_srv_crt_rsa, test_srv_crt_rsa_len); |
| 544 | vnet_app_add_tls_cert (a_cert); |
| 545 | |
| 546 | memset (a_key, 0, sizeof (*a_key)); |
| 547 | a_key->app_index = a->app_index; |
| 548 | vec_validate (a_key->key, test_srv_key_rsa_len); |
| 549 | clib_memcpy (a_key->key, test_srv_key_rsa, test_srv_key_rsa_len); |
| 550 | vnet_app_add_tls_key (a_key); |
| 551 | |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 552 | return 0; |
| 553 | } |
| 554 | |
| 555 | static int |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 556 | http_server_listen () |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 557 | { |
| 558 | http_server_main_t *hsm = &http_server_main; |
| 559 | vnet_bind_args_t _a, *a = &_a; |
| 560 | memset (a, 0, sizeof (*a)); |
| 561 | a->app_index = hsm->app_index; |
| 562 | a->uri = "tcp://0.0.0.0/80"; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 563 | if (hsm->uri) |
| 564 | a->uri = (char *) hsm->uri; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 565 | return vnet_bind_uri (a); |
| 566 | } |
| 567 | |
| 568 | static int |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 569 | http_server_create (vlib_main_t * vm) |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 570 | { |
| 571 | http_server_main_t *hsm = &http_server_main; |
| 572 | u32 num_threads; |
| 573 | vlib_thread_main_t *vtm = vlib_get_thread_main (); |
| 574 | |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 575 | ASSERT (hsm->my_client_index == (u32) ~ 0); |
| 576 | if (create_api_loopback (vm)) |
| 577 | return -1; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 578 | |
| 579 | num_threads = 1 /* main thread */ + vtm->n_threads; |
| 580 | vec_validate (http_server_main.vpp_queue, num_threads - 1); |
| 581 | |
| 582 | if (server_attach ()) |
| 583 | { |
| 584 | clib_warning ("failed to attach server"); |
| 585 | return -1; |
| 586 | } |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 587 | if (http_server_listen ()) |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 588 | { |
| 589 | clib_warning ("failed to start listening"); |
| 590 | return -1; |
| 591 | } |
| 592 | return 0; |
| 593 | } |
| 594 | |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 595 | static clib_error_t * |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 596 | http_server_create_command_fn (vlib_main_t * vm, |
| 597 | unformat_input_t * input, |
| 598 | vlib_cli_command_t * cmd) |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 599 | { |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 600 | http_server_main_t *hsm = &http_server_main; |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 601 | int rv, is_static = 0; |
Florin Coras | 1d6d085 | 2017-11-17 14:26:01 -0800 | [diff] [blame] | 602 | u64 seg_size; |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 603 | u8 *html; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 604 | |
Florin Coras | 1d6d085 | 2017-11-17 14:26:01 -0800 | [diff] [blame] | 605 | hsm->prealloc_fifos = 0; |
| 606 | hsm->private_segment_size = 0; |
| 607 | hsm->fifo_size = 0; |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 608 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 609 | { |
| 610 | if (unformat (input, "static")) |
| 611 | is_static = 1; |
Florin Coras | 1d6d085 | 2017-11-17 14:26:01 -0800 | [diff] [blame] | 612 | else if (unformat (input, "prealloc-fifos %d", &hsm->prealloc_fifos)) |
| 613 | ; |
| 614 | else if (unformat (input, "private-segment-size %U", |
| 615 | unformat_memory_size, &seg_size)) |
| 616 | { |
| 617 | if (seg_size >= 0x100000000ULL) |
| 618 | { |
| 619 | vlib_cli_output (vm, "private segment size %llu, too large", |
| 620 | seg_size); |
| 621 | return 0; |
| 622 | } |
| 623 | hsm->private_segment_size = seg_size; |
| 624 | } |
| 625 | else if (unformat (input, "fifo-size %d", &hsm->fifo_size)) |
| 626 | hsm->fifo_size <<= 10; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 627 | else if (unformat (input, "uri %s", &hsm->uri)) |
| 628 | ; |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 629 | else |
| 630 | return clib_error_return (0, "unknown input `%U'", |
| 631 | format_unformat_error, input); |
| 632 | } |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 633 | if (hsm->my_client_index != (u32) ~ 0) |
| 634 | return clib_error_return (0, "test http server is already running"); |
| 635 | |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 636 | vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */ ); |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 637 | |
| 638 | if (is_static) |
| 639 | { |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 640 | http_server_session_cb_vft.builtin_app_rx_callback = |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 641 | http_server_rx_callback_static; |
| 642 | html = format (0, html_header_static); |
| 643 | static_http = format (0, http_response, vec_len (html), html); |
| 644 | } |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 645 | rv = http_server_create (vm); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 646 | switch (rv) |
| 647 | { |
| 648 | case 0: |
| 649 | break; |
| 650 | default: |
| 651 | return clib_error_return (0, "server_create returned %d", rv); |
| 652 | } |
| 653 | return 0; |
| 654 | } |
| 655 | |
| 656 | /* *INDENT-OFF* */ |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 657 | VLIB_CLI_COMMAND (http_server_create_command, static) = |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 658 | { |
| 659 | .path = "test http server", |
| 660 | .short_help = "test http server", |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 661 | .function = http_server_create_command_fn, |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 662 | }; |
| 663 | /* *INDENT-ON* */ |
| 664 | |
| 665 | static clib_error_t * |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 666 | http_server_main_init (vlib_main_t * vm) |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 667 | { |
| 668 | http_server_main_t *hsm = &http_server_main; |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 669 | vlib_thread_main_t *vtm = vlib_get_thread_main (); |
| 670 | u32 num_threads; |
| 671 | |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 672 | hsm->my_client_index = ~0; |
| 673 | hsm->vlib_main = vm; |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 674 | num_threads = 1 /* main thread */ + vtm->n_threads; |
| 675 | vec_validate (hsm->rx_buf, num_threads - 1); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 676 | return 0; |
| 677 | } |
| 678 | |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 679 | VLIB_INIT_FUNCTION (http_server_main_init); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 680 | |
| 681 | /* |
| 682 | * fd.io coding-style-patch-verification: ON |
| 683 | * |
| 684 | * Local Variables: |
| 685 | * eval: (c-set-style "gnu") |
| 686 | * End: |
| 687 | */ |