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 http_cli_output (uword arg, u8 * buffer, uword buffer_bytes)
108 u8 **output_vecp = (u8 **) arg;
112 output_vec = *output_vecp;
114 offset = vec_len (output_vec);
115 vec_validate (output_vec, offset + buffer_bytes - 1);
116 clib_memcpy (output_vec + offset, buffer, buffer_bytes);
118 *output_vecp = output_vec;
122 send_data (builtin_http_server_args * args, u8 * data)
124 session_fifo_event_t evt;
125 u32 offset, bytes_to_send;
127 http_server_main_t *hsm = &http_server_main;
128 vlib_main_t *vm = hsm->vlib_main;
129 f64 last_sent_timer = vlib_time_now (vm);
132 s = session_get_from_handle (args->session_handle);
134 bytes_to_send = vec_len (data);
137 while (bytes_to_send > 0)
141 actual_transfer = svm_fifo_enqueue_nowait
142 (s->server_tx_fifo, bytes_to_send, data + offset);
144 /* Made any progress? */
145 if (actual_transfer <= 0)
147 vlib_process_suspend (vm, delay);
148 /* 10s deadman timer */
149 if (vlib_time_now (vm) > last_sent_timer + 10.0)
151 /* $$$$ FC: reset transport session here? */
154 /* Exponential backoff, within reason */
160 last_sent_timer = vlib_time_now (vm);
161 offset += actual_transfer;
162 bytes_to_send -= actual_transfer;
164 if (svm_fifo_set_event (s->server_tx_fifo))
166 /* Fabricate TX event, send to vpp */
167 evt.fifo = s->server_tx_fifo;
168 evt.event_type = FIFO_EVENT_APP_TX;
170 unix_shared_memory_queue_add (hsm->vpp_queue[s->thread_index],
172 0 /* do wait for mutex */ );
180 send_error (builtin_http_server_args * args, char *str)
184 data = format (0, http_error_template, str);
185 send_data (args, data);
190 http_cli_process (vlib_main_t * vm,
191 vlib_node_runtime_t * rt, vlib_frame_t * f)
193 http_server_main_t *hsm = &http_server_main;
194 u8 *request = 0, *reply = 0;
195 builtin_http_server_args **save_args;
196 builtin_http_server_args *args;
197 unformat_input_t input;
199 u8 *http = 0, *html = 0;
201 save_args = vlib_node_get_runtime_data (hsm->vlib_main, rt->node_index);
204 request = (u8 *) (void *) (args->data);
205 if (vec_len (request) < 7)
207 send_error (args, "400 Bad Request");
211 for (i = 0; i < vec_len (request) - 4; i++)
213 if (request[i] == 'G' &&
214 request[i + 1] == 'E' &&
215 request[i + 2] == 'T' && request[i + 3] == ' ')
219 send_error (args, "400 Bad Request");
224 vec_delete (request, i + 5, 0);
226 /* Replace slashes with spaces, stop at the end of the path */
230 if (request[i] == '/')
232 else if (request[i] == ' ')
234 /* vlib_cli_input is vector-based, no need for a NULL */
235 _vec_len (request) = i;
239 /* Should never happen */
240 if (i == vec_len (request))
244 /* Generate the html header */
245 html = format (0, html_header_template, request /* title */ );
247 /* Run the command */
248 unformat_init_vector (&input, request);
249 vlib_cli_input (vm, &input, http_cli_output, (uword) & reply);
250 unformat_free (&input);
253 /* Generate the html page */
254 html = format (html, "%v", reply);
255 html = format (html, html_footer);
256 /* And the http reply */
257 http = format (0, http_response, vec_len (html), html);
260 send_data (args, http);
269 free_http_process (args);
274 alloc_http_process (builtin_http_server_args * args)
278 http_server_main_t *hsm = &http_server_main;
279 vlib_main_t *vm = hsm->vlib_main;
280 uword l = vec_len (hsm->free_http_cli_process_node_indices);
281 builtin_http_server_args **save_args;
283 if (vec_len (hsm->free_http_cli_process_node_indices) > 0)
285 n = vlib_get_node (vm, hsm->free_http_cli_process_node_indices[l - 1]);
286 vlib_node_set_state (vm, n->index, VLIB_NODE_STATE_POLLING);
287 _vec_len (hsm->free_http_cli_process_node_indices) = l - 1;
291 static vlib_node_registration_t r = {
292 .function = http_cli_process,
293 .type = VLIB_NODE_TYPE_PROCESS,
294 .process_log2_n_stack_bytes = 16,
295 .runtime_data_bytes = sizeof (void *),
298 name = (char *) format (0, "http-cli-%d", l);
300 vlib_register_node (vm, &r);
303 n = vlib_get_node (vm, r.index);
306 /* Save the node index in the args. It won't be zero. */
307 args->node_index = n->index;
309 /* Save the args (pointer) in the node runtime */
310 save_args = vlib_node_get_runtime_data (vm, n->index);
313 vlib_start_process (vm, n->runtime_index);
317 alloc_http_process_callback (void *cb_args)
319 alloc_http_process ((builtin_http_server_args *) cb_args);
323 http_server_rx_callback (stream_session_t * s)
327 http_server_main_t *hsm = &http_server_main;
329 builtin_http_server_args *args;
331 rx_fifo = s->server_rx_fifo;
332 max_dequeue = svm_fifo_max_dequeue (rx_fifo);
333 svm_fifo_unset_event (rx_fifo);
334 if (PREDICT_FALSE (max_dequeue == 0))
337 vec_validate (hsm->rx_buf, max_dequeue - 1);
338 _vec_len (hsm->rx_buf) = max_dequeue;
340 actual_transfer = svm_fifo_dequeue_nowait (rx_fifo, max_dequeue,
342 ASSERT (actual_transfer > 0);
343 _vec_len (hsm->rx_buf) = actual_transfer;
345 /* send the command to a new/recycled vlib process */
346 args = clib_mem_alloc (sizeof (*args));
347 args->data = vec_dup (hsm->rx_buf);
348 args->session_handle = session_handle (s);
350 /* Send an RPC request via the thread-0 input node */
351 if (vlib_get_thread_index () != 0)
353 session_fifo_event_t evt;
354 evt.rpc_args.fp = alloc_http_process_callback;
355 evt.rpc_args.arg = args;
356 evt.event_type = FIFO_EVENT_RPC;
357 unix_shared_memory_queue_add
358 (session_manager_get_vpp_event_queue (0 /* main thread */ ),
359 (u8 *) & evt, 0 /* do wait for mutex */ );
362 alloc_http_process (args);
367 builtin_session_accept_callback (stream_session_t * s)
369 http_server_main_t *bsm = &http_server_main;
371 bsm->vpp_queue[s->thread_index] =
372 session_manager_get_vpp_event_queue (s->thread_index);
373 s->session_state = SESSION_STATE_READY;
379 builtin_session_disconnect_callback (stream_session_t * s)
381 http_server_main_t *bsm = &http_server_main;
382 vnet_disconnect_args_t _a, *a = &_a;
384 a->handle = session_handle (s);
385 a->app_index = bsm->app_index;
386 vnet_disconnect_session (a);
390 builtin_session_reset_callback (stream_session_t * s)
392 clib_warning ("called.. ");
394 stream_session_cleanup (s);
398 builtin_session_connected_callback (u32 app_index, u32 api_context,
399 stream_session_t * s, u8 is_fail)
401 clib_warning ("called...");
406 builtin_add_segment_callback (u32 client_index,
407 const u8 * seg_name, u32 seg_size)
409 clib_warning ("called...");
414 builtin_redirect_connect_callback (u32 client_index, void *mp)
416 clib_warning ("called...");
420 static session_cb_vft_t builtin_session_cb_vft = {
421 .session_accept_callback = builtin_session_accept_callback,
422 .session_disconnect_callback = builtin_session_disconnect_callback,
423 .session_connected_callback = builtin_session_connected_callback,
424 .add_segment_callback = builtin_add_segment_callback,
425 .redirect_connect_callback = builtin_redirect_connect_callback,
426 .builtin_server_rx_callback = http_server_rx_callback,
427 .session_reset_callback = builtin_session_reset_callback
430 /* Abuse VPP's input queue */
432 create_api_loopback (vlib_main_t * vm)
434 http_server_main_t *hsm = &http_server_main;
435 api_main_t *am = &api_main;
436 vl_shmem_hdr_t *shmem_hdr;
438 shmem_hdr = am->shmem_hdr;
439 hsm->vl_input_queue = shmem_hdr->vl_input_queue;
440 hsm->my_client_index =
441 vl_api_memclnt_create_internal ("tcp_test_client", hsm->vl_input_queue);
448 http_server_main_t *hsm = &http_server_main;
449 u8 segment_name[128];
450 u64 options[SESSION_OPTIONS_N_OPTIONS];
451 vnet_app_attach_args_t _a, *a = &_a;
453 memset (a, 0, sizeof (*a));
454 memset (options, 0, sizeof (options));
456 a->api_client_index = hsm->my_client_index;
457 a->session_cb_vft = &builtin_session_cb_vft;
458 a->options = options;
459 a->options[SESSION_OPTIONS_SEGMENT_SIZE] = 128 << 20;
460 a->options[SESSION_OPTIONS_RX_FIFO_SIZE] = 8 << 10;
461 a->options[SESSION_OPTIONS_TX_FIFO_SIZE] = 32 << 10;
462 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_BUILTIN_APP;
463 a->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = 16;
464 a->segment_name = segment_name;
465 a->segment_name_length = ARRAY_LEN (segment_name);
467 if (vnet_application_attach (a))
469 clib_warning ("failed to attach server");
472 hsm->app_index = a->app_index;
479 http_server_main_t *hsm = &http_server_main;
480 vnet_bind_args_t _a, *a = &_a;
481 memset (a, 0, sizeof (*a));
482 a->app_index = hsm->app_index;
483 a->uri = "tcp://0.0.0.0/80";
484 return vnet_bind_uri (a);
488 server_create (vlib_main_t * vm)
490 http_server_main_t *hsm = &http_server_main;
492 vlib_thread_main_t *vtm = vlib_get_thread_main ();
494 ASSERT (hsm->my_client_index == (u32) ~ 0);
495 if (create_api_loopback (vm))
498 num_threads = 1 /* main thread */ + vtm->n_threads;
499 vec_validate (http_server_main.vpp_queue, num_threads - 1);
501 if (server_attach ())
503 clib_warning ("failed to attach server");
506 if (server_listen ())
508 clib_warning ("failed to start listening");
514 static clib_error_t *
515 server_create_command_fn (vlib_main_t * vm,
516 unformat_input_t * input, vlib_cli_command_t * cmd)
518 http_server_main_t *hsm = &http_server_main;
521 if (hsm->my_client_index != (u32) ~ 0)
522 return clib_error_return (0, "test http server is already running");
524 vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */ );
525 rv = server_create (vm);
531 return clib_error_return (0, "server_create returned %d", rv);
537 VLIB_CLI_COMMAND (server_create_command, static) =
539 .path = "test http server",
540 .short_help = "test http server",
541 .function = server_create_command_fn,
545 static clib_error_t *
546 builtin_http_server_main_init (vlib_main_t * vm)
548 http_server_main_t *hsm = &http_server_main;
549 hsm->my_client_index = ~0;
555 VLIB_INIT_FUNCTION (builtin_http_server_main_init);
558 * fd.io coding-style-patch-verification: ON
561 * eval: (c-set-style "gnu")