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