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