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 */
54 u32 private_segment_size;
56 vlib_main_t *vlib_main;
59 http_server_main_t http_server_main;
62 free_http_process (builtin_http_server_args * args)
64 vlib_node_runtime_t *rt;
65 vlib_main_t *vm = &vlib_global_main;
66 http_server_main_t *hsm = &http_server_main;
69 builtin_http_server_args **save_args;
71 node_index = args->node_index;
72 ASSERT (node_index != 0);
74 n = vlib_get_node (vm, node_index);
75 rt = vlib_node_get_runtime (vm, n->index);
76 save_args = vlib_node_get_runtime_data (vm, n->index);
78 /* Reset process session pointer */
79 clib_mem_free (*save_args);
82 /* Turn off the process node */
83 vlib_node_set_state (vm, rt->node_index, VLIB_NODE_STATE_DISABLED);
85 /* add node index to the freelist */
86 vec_add1 (hsm->free_http_cli_process_node_indices, node_index);
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";
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";
102 /* Header, including incantation to suppress favicon.ico requests */
104 *html_header_template = "<html><head><title>%v</title>"
105 "</head><link rel=\"icon\" href=\"data:,\"><body><pre>";
107 static const char *html_footer = "</pre></body></html>\r\n";
110 *html_header_static = "<html><head><title>static reply</title></head>"
111 "<link rel=\"icon\" href=\"data:,\"><body><pre>hello</pre></body>"
114 static u8 *static_http;
117 http_cli_output (uword arg, u8 * buffer, uword buffer_bytes)
119 u8 **output_vecp = (u8 **) arg;
123 output_vec = *output_vecp;
125 offset = vec_len (output_vec);
126 vec_validate (output_vec, offset + buffer_bytes - 1);
127 clib_memcpy (output_vec + offset, buffer, buffer_bytes);
129 *output_vecp = output_vec;
133 send_data (stream_session_t * s, u8 * data)
135 session_fifo_event_t evt;
136 u32 offset, bytes_to_send;
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);
142 bytes_to_send = vec_len (data);
145 while (bytes_to_send > 0)
149 actual_transfer = svm_fifo_enqueue_nowait
150 (s->server_tx_fifo, bytes_to_send, data + offset);
152 /* Made any progress? */
153 if (actual_transfer <= 0)
155 vlib_process_suspend (vm, delay);
156 /* 10s deadman timer */
157 if (vlib_time_now (vm) > last_sent_timer + 10.0)
159 /* $$$$ FC: reset transport session here? */
162 /* Exponential backoff, within reason */
168 last_sent_timer = vlib_time_now (vm);
169 offset += actual_transfer;
170 bytes_to_send -= actual_transfer;
172 if (svm_fifo_set_event (s->server_tx_fifo))
174 /* Fabricate TX event, send to vpp */
175 evt.fifo = s->server_tx_fifo;
176 evt.event_type = FIFO_EVENT_APP_TX;
178 unix_shared_memory_queue_add (hsm->vpp_queue[s->thread_index],
180 0 /* do wait for mutex */ );
188 send_error (stream_session_t * s, char *str)
192 data = format (0, http_error_template, str);
198 http_cli_process (vlib_main_t * vm,
199 vlib_node_runtime_t * rt, vlib_frame_t * f)
201 http_server_main_t *hsm = &http_server_main;
202 u8 *request = 0, *reply = 0;
203 builtin_http_server_args **save_args;
204 builtin_http_server_args *args;
206 unformat_input_t input;
208 u8 *http = 0, *html = 0;
210 save_args = vlib_node_get_runtime_data (hsm->vlib_main, rt->node_index);
212 s = session_get_from_handle (args->session_handle);
215 request = (u8 *) (void *) (args->data);
216 if (vec_len (request) < 7)
218 send_error (s, "400 Bad Request");
222 for (i = 0; i < vec_len (request) - 4; i++)
224 if (request[i] == 'G' &&
225 request[i + 1] == 'E' &&
226 request[i + 2] == 'T' && request[i + 3] == ' ')
230 send_error (s, "400 Bad Request");
235 vec_delete (request, i + 5, 0);
237 /* Replace slashes with spaces, stop at the end of the path */
241 if (request[i] == '/')
243 else if (request[i] == ' ')
245 /* vlib_cli_input is vector-based, no need for a NULL */
246 _vec_len (request) = i;
250 /* Should never happen */
251 if (i == vec_len (request))
255 /* Generate the html header */
256 html = format (0, html_header_template, request /* title */ );
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);
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);
280 free_http_process (args);
285 alloc_http_process (builtin_http_server_args * args)
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);
292 builtin_http_server_args **save_args;
294 if (vec_len (hsm->free_http_cli_process_node_indices) > 0)
296 n = vlib_get_node (vm, hsm->free_http_cli_process_node_indices[l - 1]);
297 vlib_node_set_state (vm, n->index, VLIB_NODE_STATE_POLLING);
298 _vec_len (hsm->free_http_cli_process_node_indices) = l - 1;
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 *),
309 name = (char *) format (0, "http-cli-%d", l);
311 vlib_register_node (vm, &r);
314 n = vlib_get_node (vm, r.index);
317 /* Save the node index in the args. It won't be zero. */
318 args->node_index = n->index;
320 /* Save the args (pointer) in the node runtime */
321 save_args = vlib_node_get_runtime_data (vm, n->index);
324 vlib_start_process (vm, n->runtime_index);
328 alloc_http_process_callback (void *cb_args)
330 alloc_http_process ((builtin_http_server_args *) cb_args);
334 session_rx_request (stream_session_t * s)
336 http_server_main_t *hsm = &http_server_main;
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))
347 vec_validate (hsm->rx_buf[s->thread_index], max_dequeue - 1);
348 _vec_len (hsm->rx_buf[s->thread_index]) = max_dequeue;
350 actual_transfer = svm_fifo_dequeue_nowait (rx_fifo, max_dequeue,
351 hsm->rx_buf[s->thread_index]);
352 ASSERT (actual_transfer > 0);
353 _vec_len (hsm->rx_buf[s->thread_index]) = actual_transfer;
358 http_server_rx_callback (stream_session_t * s)
360 http_server_main_t *hsm = &http_server_main;
361 builtin_http_server_args *args;
364 rv = session_rx_request (s);
368 /* send the command to a new/recycled vlib process */
369 args = clib_mem_alloc (sizeof (*args));
370 args->data = vec_dup (hsm->rx_buf[s->thread_index]);
371 args->session_handle = session_handle (s);
373 /* Send an RPC request via the thread-0 input node */
374 if (vlib_get_thread_index () != 0)
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;
380 unix_shared_memory_queue_add
381 (session_manager_get_vpp_event_queue (0 /* main thread */ ),
382 (u8 *) & evt, 0 /* do wait for mutex */ );
385 alloc_http_process (args);
390 http_server_rx_callback_static (stream_session_t * s)
392 http_server_main_t *hsm = &http_server_main;
397 rv = session_rx_request (s);
401 request = hsm->rx_buf[s->thread_index];
402 if (vec_len (request) < 7)
404 send_error (s, "400 Bad Request");
408 for (i = 0; i < vec_len (request) - 4; i++)
410 if (request[i] == 'G' &&
411 request[i + 1] == 'E' &&
412 request[i + 2] == 'T' && request[i + 3] == ' ')
415 send_error (s, "400 Bad Request");
421 send_data (s, static_http);
426 hsm->rx_buf[s->thread_index] = request;
431 builtin_session_accept_callback (stream_session_t * s)
433 http_server_main_t *bsm = &http_server_main;
435 bsm->vpp_queue[s->thread_index] =
436 session_manager_get_vpp_event_queue (s->thread_index);
437 s->session_state = SESSION_STATE_READY;
443 builtin_session_disconnect_callback (stream_session_t * s)
445 http_server_main_t *bsm = &http_server_main;
446 vnet_disconnect_args_t _a, *a = &_a;
448 a->handle = session_handle (s);
449 a->app_index = bsm->app_index;
450 vnet_disconnect_session (a);
454 builtin_session_reset_callback (stream_session_t * s)
456 clib_warning ("called.. ");
458 stream_session_cleanup (s);
462 builtin_session_connected_callback (u32 app_index, u32 api_context,
463 stream_session_t * s, u8 is_fail)
465 clib_warning ("called...");
470 builtin_add_segment_callback (u32 client_index,
471 const u8 * seg_name, u32 seg_size)
473 clib_warning ("called...");
478 builtin_redirect_connect_callback (u32 client_index, void *mp)
480 clib_warning ("called...");
484 static session_cb_vft_t builtin_session_cb_vft = {
485 .session_accept_callback = builtin_session_accept_callback,
486 .session_disconnect_callback = builtin_session_disconnect_callback,
487 .session_connected_callback = builtin_session_connected_callback,
488 .add_segment_callback = builtin_add_segment_callback,
489 .redirect_connect_callback = builtin_redirect_connect_callback,
490 .builtin_server_rx_callback = http_server_rx_callback,
491 .session_reset_callback = builtin_session_reset_callback
494 /* Abuse VPP's input queue */
496 create_api_loopback (vlib_main_t * vm)
498 http_server_main_t *hsm = &http_server_main;
499 api_main_t *am = &api_main;
500 vl_shmem_hdr_t *shmem_hdr;
502 shmem_hdr = am->shmem_hdr;
503 hsm->vl_input_queue = shmem_hdr->vl_input_queue;
504 hsm->my_client_index =
505 vl_api_memclnt_create_internal ("test_http_server", hsm->vl_input_queue);
512 http_server_main_t *hsm = &http_server_main;
513 u8 segment_name[128];
514 u64 options[SESSION_OPTIONS_N_OPTIONS];
515 vnet_app_attach_args_t _a, *a = &_a;
517 memset (a, 0, sizeof (*a));
518 memset (options, 0, sizeof (options));
520 a->api_client_index = hsm->my_client_index;
521 a->session_cb_vft = &builtin_session_cb_vft;
522 a->options = options;
523 a->options[SESSION_OPTIONS_SEGMENT_SIZE] = 128 << 20;
524 a->options[SESSION_OPTIONS_RX_FIFO_SIZE] =
525 hsm->fifo_size ? hsm->fifo_size : 8 << 10;
526 a->options[SESSION_OPTIONS_TX_FIFO_SIZE] =
527 hsm->fifo_size ? hsm->fifo_size : 32 << 10;
528 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
529 a->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = hsm->prealloc_fifos;
530 a->options[APP_OPTIONS_PRIVATE_SEGMENT_SIZE] = hsm->private_segment_size;
531 a->segment_name = segment_name;
532 a->segment_name_length = ARRAY_LEN (segment_name);
534 if (vnet_application_attach (a))
536 clib_warning ("failed to attach server");
539 hsm->app_index = a->app_index;
546 http_server_main_t *hsm = &http_server_main;
547 vnet_bind_args_t _a, *a = &_a;
548 memset (a, 0, sizeof (*a));
549 a->app_index = hsm->app_index;
550 a->uri = "tcp://0.0.0.0/80";
551 return vnet_bind_uri (a);
555 server_create (vlib_main_t * vm)
557 http_server_main_t *hsm = &http_server_main;
559 vlib_thread_main_t *vtm = vlib_get_thread_main ();
561 ASSERT (hsm->my_client_index == (u32) ~ 0);
562 if (create_api_loopback (vm))
565 num_threads = 1 /* main thread */ + vtm->n_threads;
566 vec_validate (http_server_main.vpp_queue, num_threads - 1);
568 if (server_attach ())
570 clib_warning ("failed to attach server");
573 if (server_listen ())
575 clib_warning ("failed to start listening");
581 static clib_error_t *
582 server_create_command_fn (vlib_main_t * vm,
583 unformat_input_t * input, vlib_cli_command_t * cmd)
585 http_server_main_t *hsm = &http_server_main;
586 int rv, is_static = 0;
590 hsm->prealloc_fifos = 0;
591 hsm->private_segment_size = 0;
593 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
595 if (unformat (input, "static"))
597 else if (unformat (input, "prealloc-fifos %d", &hsm->prealloc_fifos))
599 else if (unformat (input, "private-segment-size %U",
600 unformat_memory_size, &seg_size))
602 if (seg_size >= 0x100000000ULL)
604 vlib_cli_output (vm, "private segment size %llu, too large",
608 hsm->private_segment_size = seg_size;
610 else if (unformat (input, "fifo-size %d", &hsm->fifo_size))
611 hsm->fifo_size <<= 10;
613 return clib_error_return (0, "unknown input `%U'",
614 format_unformat_error, input);
616 if (hsm->my_client_index != (u32) ~ 0)
617 return clib_error_return (0, "test http server is already running");
619 vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */ );
623 builtin_session_cb_vft.builtin_server_rx_callback =
624 http_server_rx_callback_static;
625 html = format (0, html_header_static);
626 static_http = format (0, http_response, vec_len (html), html);
628 rv = server_create (vm);
634 return clib_error_return (0, "server_create returned %d", rv);
640 VLIB_CLI_COMMAND (server_create_command, static) =
642 .path = "test http server",
643 .short_help = "test http server",
644 .function = server_create_command_fn,
648 static clib_error_t *
649 builtin_http_server_main_init (vlib_main_t * vm)
651 http_server_main_t *hsm = &http_server_main;
652 vlib_thread_main_t *vtm = vlib_get_thread_main ();
655 hsm->my_client_index = ~0;
657 num_threads = 1 /* main thread */ + vtm->n_threads;
658 vec_validate (hsm->rx_buf, num_threads - 1);
662 VLIB_INIT_FUNCTION (builtin_http_server_main_init);
665 * fd.io coding-style-patch-verification: ON
668 * eval: (c-set-style "gnu")