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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vnet/vnet.h>
17 #include <vnet/session/application.h>
18 #include <vnet/session/application_interface.h>
23 } http_process_event_t;
30 } builtin_http_server_args;
35 unix_shared_memory_queue_t **vpp_queue;
38 uword *handler_by_get_request;
40 u32 *free_http_cli_process_node_indices;
42 /* Sever's event queue */
43 unix_shared_memory_queue_t *vl_input_queue;
45 /* API client handle */
50 /* process node index for evnt scheduling */
52 vlib_main_t *vlib_main;
55 http_server_main_t http_server_main;
58 free_http_process (builtin_http_server_args * args)
60 vlib_node_runtime_t *rt;
61 vlib_main_t *vm = &vlib_global_main;
62 http_server_main_t *hsm = &http_server_main;
65 builtin_http_server_args **save_args;
67 node_index = args->node_index;
68 ASSERT (node_index != 0);
70 n = vlib_get_node (vm, node_index);
71 rt = vlib_node_get_runtime (vm, n->index);
72 save_args = vlib_node_get_runtime_data (vm, n->index);
74 /* Reset process session pointer */
75 clib_mem_free (*save_args);
78 /* Turn off the process node */
79 vlib_node_set_state (vm, rt->node_index, VLIB_NODE_STATE_DISABLED);
81 /* add node index to the freelist */
82 vec_add1 (hsm->free_http_cli_process_node_indices, node_index);
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";
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";
98 /* Header, including incantation to suppress favicon.ico requests */
100 *html_header_template = "<html><head><title>%v</title>"
101 "</head><link rel=\"icon\" href=\"data:,\"><body><pre>";
103 static const char *html_footer = "</pre></body></html>\r\n";
106 *html_header_static = "<html><head><title>static reply</title></head>"
107 "<link rel=\"icon\" href=\"data:,\"><body><pre>hello</pre></body>"
110 static u8 *static_http;
113 http_cli_output (uword arg, u8 * buffer, uword buffer_bytes)
115 u8 **output_vecp = (u8 **) arg;
119 output_vec = *output_vecp;
121 offset = vec_len (output_vec);
122 vec_validate (output_vec, offset + buffer_bytes - 1);
123 clib_memcpy (output_vec + offset, buffer, buffer_bytes);
125 *output_vecp = output_vec;
129 send_data (stream_session_t * s, u8 * data)
131 session_fifo_event_t evt;
132 u32 offset, bytes_to_send;
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);
138 bytes_to_send = vec_len (data);
141 while (bytes_to_send > 0)
145 actual_transfer = svm_fifo_enqueue_nowait
146 (s->server_tx_fifo, bytes_to_send, data + offset);
148 /* Made any progress? */
149 if (actual_transfer <= 0)
151 vlib_process_suspend (vm, delay);
152 /* 10s deadman timer */
153 if (vlib_time_now (vm) > last_sent_timer + 10.0)
155 /* $$$$ FC: reset transport session here? */
158 /* Exponential backoff, within reason */
164 last_sent_timer = vlib_time_now (vm);
165 offset += actual_transfer;
166 bytes_to_send -= actual_transfer;
168 if (svm_fifo_set_event (s->server_tx_fifo))
170 /* Fabricate TX event, send to vpp */
171 evt.fifo = s->server_tx_fifo;
172 evt.event_type = FIFO_EVENT_APP_TX;
174 unix_shared_memory_queue_add (hsm->vpp_queue[s->thread_index],
176 0 /* do wait for mutex */ );
184 send_error (stream_session_t * s, char *str)
188 data = format (0, http_error_template, str);
194 http_cli_process (vlib_main_t * vm,
195 vlib_node_runtime_t * rt, vlib_frame_t * f)
197 http_server_main_t *hsm = &http_server_main;
198 u8 *request = 0, *reply = 0;
199 builtin_http_server_args **save_args;
200 builtin_http_server_args *args;
202 unformat_input_t input;
204 u8 *http = 0, *html = 0;
206 save_args = vlib_node_get_runtime_data (hsm->vlib_main, rt->node_index);
208 s = session_get_from_handle (args->session_handle);
211 request = (u8 *) (void *) (args->data);
212 if (vec_len (request) < 7)
214 send_error (s, "400 Bad Request");
218 for (i = 0; i < vec_len (request) - 4; i++)
220 if (request[i] == 'G' &&
221 request[i + 1] == 'E' &&
222 request[i + 2] == 'T' && request[i + 3] == ' ')
226 send_error (s, "400 Bad Request");
231 vec_delete (request, i + 5, 0);
233 /* Replace slashes with spaces, stop at the end of the path */
237 if (request[i] == '/')
239 else if (request[i] == ' ')
241 /* vlib_cli_input is vector-based, no need for a NULL */
242 _vec_len (request) = i;
246 /* Should never happen */
247 if (i == vec_len (request))
251 /* Generate the html header */
252 html = format (0, html_header_template, request /* title */ );
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);
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);
276 free_http_process (args);
281 alloc_http_process (builtin_http_server_args * args)
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);
288 builtin_http_server_args **save_args;
290 if (vec_len (hsm->free_http_cli_process_node_indices) > 0)
292 n = vlib_get_node (vm, hsm->free_http_cli_process_node_indices[l - 1]);
293 vlib_node_set_state (vm, n->index, VLIB_NODE_STATE_POLLING);
294 _vec_len (hsm->free_http_cli_process_node_indices) = l - 1;
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 *),
305 name = (char *) format (0, "http-cli-%d", l);
307 vlib_register_node (vm, &r);
310 n = vlib_get_node (vm, r.index);
313 /* Save the node index in the args. It won't be zero. */
314 args->node_index = n->index;
316 /* Save the args (pointer) in the node runtime */
317 save_args = vlib_node_get_runtime_data (vm, n->index);
320 vlib_start_process (vm, n->runtime_index);
324 alloc_http_process_callback (void *cb_args)
326 alloc_http_process ((builtin_http_server_args *) cb_args);
330 session_rx_request (stream_session_t * s)
332 http_server_main_t *hsm = &http_server_main;
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))
343 vec_validate (hsm->rx_buf[s->thread_index], max_dequeue - 1);
344 _vec_len (hsm->rx_buf[s->thread_index]) = max_dequeue;
346 actual_transfer = svm_fifo_dequeue_nowait (rx_fifo, max_dequeue,
347 hsm->rx_buf[s->thread_index]);
348 ASSERT (actual_transfer > 0);
349 _vec_len (hsm->rx_buf[s->thread_index]) = actual_transfer;
354 http_server_rx_callback (stream_session_t * s)
356 http_server_main_t *hsm = &http_server_main;
357 builtin_http_server_args *args;
359 session_rx_request (s);
361 /* send the command to a new/recycled vlib process */
362 args = clib_mem_alloc (sizeof (*args));
363 args->data = vec_dup (hsm->rx_buf[s->thread_index]);
364 args->session_handle = session_handle (s);
366 /* Send an RPC request via the thread-0 input node */
367 if (vlib_get_thread_index () != 0)
369 session_fifo_event_t evt;
370 evt.rpc_args.fp = alloc_http_process_callback;
371 evt.rpc_args.arg = args;
372 evt.event_type = FIFO_EVENT_RPC;
373 unix_shared_memory_queue_add
374 (session_manager_get_vpp_event_queue (0 /* main thread */ ),
375 (u8 *) & evt, 0 /* do wait for mutex */ );
378 alloc_http_process (args);
383 http_server_rx_callback_static (stream_session_t * s)
385 http_server_main_t *hsm = &http_server_main;
389 session_rx_request (s);
391 request = hsm->rx_buf[s->thread_index];
392 if (vec_len (request) < 7)
394 send_error (s, "400 Bad Request");
398 for (i = 0; i < vec_len (request) - 4; i++)
400 if (request[i] == 'G' &&
401 request[i + 1] == 'E' &&
402 request[i + 2] == 'T' && request[i + 3] == ' ')
405 send_error (s, "400 Bad Request");
411 send_data (s, static_http);
420 builtin_session_accept_callback (stream_session_t * s)
422 http_server_main_t *bsm = &http_server_main;
424 bsm->vpp_queue[s->thread_index] =
425 session_manager_get_vpp_event_queue (s->thread_index);
426 s->session_state = SESSION_STATE_READY;
432 builtin_session_disconnect_callback (stream_session_t * s)
434 http_server_main_t *bsm = &http_server_main;
435 vnet_disconnect_args_t _a, *a = &_a;
437 a->handle = session_handle (s);
438 a->app_index = bsm->app_index;
439 vnet_disconnect_session (a);
443 builtin_session_reset_callback (stream_session_t * s)
445 clib_warning ("called.. ");
447 stream_session_cleanup (s);
451 builtin_session_connected_callback (u32 app_index, u32 api_context,
452 stream_session_t * s, u8 is_fail)
454 clib_warning ("called...");
459 builtin_add_segment_callback (u32 client_index,
460 const u8 * seg_name, u32 seg_size)
462 clib_warning ("called...");
467 builtin_redirect_connect_callback (u32 client_index, void *mp)
469 clib_warning ("called...");
473 static session_cb_vft_t builtin_session_cb_vft = {
474 .session_accept_callback = builtin_session_accept_callback,
475 .session_disconnect_callback = builtin_session_disconnect_callback,
476 .session_connected_callback = builtin_session_connected_callback,
477 .add_segment_callback = builtin_add_segment_callback,
478 .redirect_connect_callback = builtin_redirect_connect_callback,
479 .builtin_server_rx_callback = http_server_rx_callback,
480 .session_reset_callback = builtin_session_reset_callback
483 /* Abuse VPP's input queue */
485 create_api_loopback (vlib_main_t * vm)
487 http_server_main_t *hsm = &http_server_main;
488 api_main_t *am = &api_main;
489 vl_shmem_hdr_t *shmem_hdr;
491 shmem_hdr = am->shmem_hdr;
492 hsm->vl_input_queue = shmem_hdr->vl_input_queue;
493 hsm->my_client_index =
494 vl_api_memclnt_create_internal ("test_http_server", hsm->vl_input_queue);
501 http_server_main_t *hsm = &http_server_main;
502 u8 segment_name[128];
503 u64 options[SESSION_OPTIONS_N_OPTIONS];
504 vnet_app_attach_args_t _a, *a = &_a;
506 memset (a, 0, sizeof (*a));
507 memset (options, 0, sizeof (options));
509 a->api_client_index = hsm->my_client_index;
510 a->session_cb_vft = &builtin_session_cb_vft;
511 a->options = options;
512 a->options[SESSION_OPTIONS_SEGMENT_SIZE] = 128 << 20;
513 a->options[SESSION_OPTIONS_RX_FIFO_SIZE] = 8 << 10;
514 a->options[SESSION_OPTIONS_TX_FIFO_SIZE] = 32 << 10;
515 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
516 a->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = 16;
517 a->segment_name = segment_name;
518 a->segment_name_length = ARRAY_LEN (segment_name);
520 if (vnet_application_attach (a))
522 clib_warning ("failed to attach server");
525 hsm->app_index = a->app_index;
532 http_server_main_t *hsm = &http_server_main;
533 vnet_bind_args_t _a, *a = &_a;
534 memset (a, 0, sizeof (*a));
535 a->app_index = hsm->app_index;
536 a->uri = "tcp://0.0.0.0/80";
537 return vnet_bind_uri (a);
541 server_create (vlib_main_t * vm)
543 http_server_main_t *hsm = &http_server_main;
545 vlib_thread_main_t *vtm = vlib_get_thread_main ();
547 ASSERT (hsm->my_client_index == (u32) ~ 0);
548 if (create_api_loopback (vm))
551 num_threads = 1 /* main thread */ + vtm->n_threads;
552 vec_validate (http_server_main.vpp_queue, num_threads - 1);
554 if (server_attach ())
556 clib_warning ("failed to attach server");
559 if (server_listen ())
561 clib_warning ("failed to start listening");
567 static clib_error_t *
568 server_create_command_fn (vlib_main_t * vm,
569 unformat_input_t * input, vlib_cli_command_t * cmd)
571 http_server_main_t *hsm = &http_server_main;
572 int rv, is_static = 0;
575 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
577 if (unformat (input, "static"))
580 return clib_error_return (0, "unknown input `%U'",
581 format_unformat_error, input);
583 if (hsm->my_client_index != (u32) ~ 0)
584 return clib_error_return (0, "test http server is already running");
586 vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */ );
590 builtin_session_cb_vft.builtin_server_rx_callback =
591 http_server_rx_callback_static;
592 html = format (0, html_header_static);
593 static_http = format (0, http_response, vec_len (html), html);
595 rv = server_create (vm);
601 return clib_error_return (0, "server_create returned %d", rv);
607 VLIB_CLI_COMMAND (server_create_command, static) =
609 .path = "test http server",
610 .short_help = "test http server",
611 .function = server_create_command_fn,
615 static clib_error_t *
616 builtin_http_server_main_init (vlib_main_t * vm)
618 http_server_main_t *hsm = &http_server_main;
619 vlib_thread_main_t *vtm = vlib_get_thread_main ();
622 hsm->my_client_index = ~0;
624 num_threads = 1 /* main thread */ + vtm->n_threads;
625 vec_validate (hsm->rx_buf, num_threads - 1);
629 VLIB_INIT_FUNCTION (builtin_http_server_main_init);
632 * fd.io coding-style-patch-verification: ON
635 * eval: (c-set-style "gnu")