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