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 <vlibmemory/api.h>
18 #include <vnet/session/application.h>
19 #include <vnet/session/application_interface.h>
21 /* define message IDs */
22 #include <vpp/api/vpe_msg_enum.h>
24 /* define message structures */
26 #include <vpp/api/vpe_all_api_h.h>
29 /* define generated endian-swappers */
31 #include <vpp/api/vpe_all_api_h.h>
34 /* instantiate all the print functions we know about */
35 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
37 #include <vpp/api/vpe_all_api_h.h>
43 } http_process_event_t;
48 unix_shared_memory_queue_t **vpp_queue;
51 uword *handler_by_get_request;
53 u32 *free_http_cli_process_node_indices;
55 /* Sever's event queue */
56 unix_shared_memory_queue_t *vl_input_queue;
58 /* API client handle */
63 /* process node index for evnt scheduling */
65 vlib_main_t *vlib_main;
68 http_server_main_t http_server_main;
71 free_http_process (stream_session_t * s)
73 vlib_node_runtime_t *rt;
74 vlib_main_t *vm = &vlib_global_main;
75 http_server_main_t *hsm = &http_server_main;
78 stream_session_t **save_s;
80 node_index = (u64) (s->opaque[0]);
81 ASSERT (node_index != 0);
83 n = vlib_get_node (vm, node_index);
84 rt = vlib_node_get_runtime (vm, n->runtime_index);
85 save_s = (stream_session_t **) vlib_node_get_runtime (vm, n->runtime_index);
87 /* Reset session saved node index */
89 /* Reset process session pointer */
92 /* Turn off the process node */
93 vlib_node_set_state (vm, rt->node_index, VLIB_NODE_STATE_DISABLED);
95 /* add node index to the freelist */
96 vec_add1 (hsm->free_http_cli_process_node_indices, node_index);
100 *http_response = "HTTP/1.1 200 OK\r\n"
101 "Content-Type: text/html\r\n"
102 "Expires: Mon, 11 Jan 1970 10:10:10 GMT\r\n"
103 "Connection: close\r\n"
104 "Pragma: no-cache\r\n" "Content-Length: %d\r\n\r\n%s";
107 *http_error_template = "HTTP/1.1 %s\r\n"
108 "Content-Type: text/html\r\n"
109 "Expires: Mon, 11 Jan 1970 10:10:10 GMT\r\n"
110 "Connection: close\r\n" "Pragma: no-cache\r\n" "Content-Length: 0\r\n\r\n";
112 /* Header, including incantation to suppress favicon.ico requests */
114 *html_header_template = "<html><head><title>%v</title>"
115 "</head><link rel=\"icon\" href=\"data:,\"><body><pre>";
117 static const char *html_footer = "</pre></body></html>\r\n";
120 http_cli_output (uword arg, u8 * buffer, uword buffer_bytes)
122 u8 **output_vecp = (u8 **) arg;
126 output_vec = *output_vecp;
128 offset = vec_len (output_vec);
129 vec_validate (output_vec, offset + buffer_bytes - 1);
130 clib_memcpy (output_vec + offset, buffer, buffer_bytes);
132 *output_vecp = output_vec;
136 send_data (stream_session_t * s, u8 * data)
138 session_fifo_event_t evt;
139 u32 offset, bytes_to_send;
141 http_server_main_t *hsm = &http_server_main;
142 vlib_main_t *vm = hsm->vlib_main;
143 f64 last_sent_timer = vlib_time_now (vm);
145 bytes_to_send = vec_len (data);
148 while (bytes_to_send > 0)
152 actual_transfer = svm_fifo_enqueue_nowait
153 (s->server_tx_fifo, bytes_to_send, data + offset);
155 /* Made any progress? */
156 if (actual_transfer <= 0)
158 vlib_process_suspend (vm, delay);
159 /* 10s deadman timer */
160 if (vlib_time_now (vm) > last_sent_timer + 10.0)
162 /* $$$$ FC: reset transport session here? */
165 /* Exponential backoff, within reason */
171 last_sent_timer = vlib_time_now (vm);
172 offset += actual_transfer;
173 bytes_to_send -= actual_transfer;
175 if (svm_fifo_set_event (s->server_tx_fifo))
177 /* Fabricate TX event, send to vpp */
178 evt.fifo = s->server_tx_fifo;
179 evt.event_type = FIFO_EVENT_APP_TX;
182 unix_shared_memory_queue_add (hsm->vpp_queue[s->thread_index],
184 0 /* do wait for mutex */ );
192 send_error (stream_session_t * s, char *str)
196 data = format (0, http_error_template, str);
202 http_cli_process (vlib_main_t * vm,
203 vlib_node_runtime_t * rt, vlib_frame_t * f)
205 http_server_main_t *hsm = &http_server_main;
206 u8 *request = 0, *reply = 0;
207 stream_session_t **save_s;
209 unformat_input_t input;
211 u8 *http = 0, *html = 0;
213 save_s = vlib_node_get_runtime_data (hsm->vlib_main, rt->node_index);
216 request = (u8 *) (void *) (s->opaque[1]);
219 if (vec_len (request) < 7)
221 send_error (s, "400 Bad Request");
225 for (i = 0; i < vec_len (request) - 4; i++)
227 if (request[i] == 'G' &&
228 request[i + 1] == 'E' &&
229 request[i + 2] == 'T' && request[i + 3] == ' ')
233 send_error (s, "400 Bad Request");
238 vec_delete (request, i + 5, 0);
240 /* Replace slashes with spaces, stop at the end of the path */
244 if (request[i] == '/')
246 else if (request[i] == ' ')
248 /* vlib_cli_input is vector-based, no need for a NULL */
249 _vec_len (request) = i;
253 /* Should never happen */
254 if (i == vec_len (request))
258 /* Generate the html header */
259 html = format (0, html_header_template, request /* title */ );
261 /* Run the command */
262 unformat_init_vector (&input, request);
263 vlib_cli_input (vm, &input, http_cli_output, (uword) & reply);
264 unformat_free (&input);
267 /* Generate the html page */
268 html = format (html, "%v", reply);
269 html = format (html, html_footer);
270 /* And the http reply */
271 http = format (0, http_response, vec_len (html), html);
283 free_http_process (s);
288 alloc_http_process (stream_session_t * s)
292 http_server_main_t *hsm = &http_server_main;
293 vlib_main_t *vm = hsm->vlib_main;
294 uword l = vec_len (hsm->free_http_cli_process_node_indices);
295 stream_session_t **save_s;
297 if (vec_len (hsm->free_http_cli_process_node_indices) > 0)
299 n = vlib_get_node (vm, hsm->free_http_cli_process_node_indices[l - 1]);
301 vlib_node_set_state (vm, n->index, VLIB_NODE_STATE_POLLING);
303 _vec_len (hsm->free_http_cli_process_node_indices) = l - 1;
307 static vlib_node_registration_t r = {
308 .function = http_cli_process,
309 .type = VLIB_NODE_TYPE_PROCESS,
310 .process_log2_n_stack_bytes = 16,
311 .runtime_data_bytes = sizeof (void *),
314 name = (char *) format (0, "http-cli-%d", l);
317 vlib_register_node (vm, &r);
320 n = vlib_get_node (vm, r.index);
323 /* Save the node index in the stream_session_t. It won't be zero. */
324 s->opaque[0] = (u64) n->index;
326 /* Save the stream_session_t (pointer) in the node runtime */
327 save_s = vlib_node_get_runtime_data (vm, n->index);
330 vlib_start_process (vm, n->runtime_index);
334 builtin_session_accept_callback (stream_session_t * s)
336 http_server_main_t *bsm = &http_server_main;
338 bsm->vpp_queue[s->thread_index] =
339 session_manager_get_vpp_event_queue (s->thread_index);
340 s->session_state = SESSION_STATE_READY;
346 builtin_session_disconnect_callback (stream_session_t * s)
348 http_server_main_t *bsm = &http_server_main;
349 vnet_disconnect_args_t _a, *a = &_a;
351 a->handle = stream_session_handle (s);
352 a->app_index = bsm->app_index;
353 vnet_disconnect_session (a);
357 builtin_session_reset_callback (stream_session_t * s)
359 clib_warning ("called.. ");
361 stream_session_cleanup (s);
366 builtin_session_connected_callback (u32 app_index, u32 api_context,
367 stream_session_t * s, u8 is_fail)
369 clib_warning ("called...");
374 builtin_add_segment_callback (u32 client_index,
375 const u8 * seg_name, u32 seg_size)
377 clib_warning ("called...");
382 builtin_redirect_connect_callback (u32 client_index, void *mp)
384 clib_warning ("called...");
389 alloc_http_process_callback (void *s_arg)
391 stream_session_t *s = (stream_session_t *) s_arg;
392 alloc_http_process (s);
396 http_server_rx_callback (stream_session_t * s)
400 http_server_main_t *hsm = &http_server_main;
403 rx_fifo = s->server_rx_fifo;
405 max_dequeue = svm_fifo_max_dequeue (rx_fifo);
407 svm_fifo_unset_event (rx_fifo);
409 if (PREDICT_FALSE (max_dequeue == 0))
412 vec_validate (hsm->rx_buf, max_dequeue - 1);
413 _vec_len (hsm->rx_buf) = max_dequeue;
415 actual_transfer = svm_fifo_dequeue_nowait (rx_fifo, max_dequeue,
417 ASSERT (actual_transfer > 0);
419 _vec_len (hsm->rx_buf) = actual_transfer;
421 /* send the command to a new/recycled vlib process */
422 s->opaque[1] = (u64) vec_dup (hsm->rx_buf);
424 /* Send an RPC request via the thread-0 input node */
425 if (vlib_get_thread_index () != 0)
427 session_fifo_event_t evt;
428 evt.rpc_args.fp = alloc_http_process_callback;
429 evt.rpc_args.arg = s;
430 evt.event_type = FIFO_EVENT_RPC;
431 unix_shared_memory_queue_add
432 (session_manager_get_vpp_event_queue (0 /* main thread */ ),
433 (u8 *) & evt, 0 /* do wait for mutex */ );
436 alloc_http_process (s);
440 static session_cb_vft_t builtin_session_cb_vft = {
441 .session_accept_callback = builtin_session_accept_callback,
442 .session_disconnect_callback = builtin_session_disconnect_callback,
443 .session_connected_callback = builtin_session_connected_callback,
444 .add_segment_callback = builtin_add_segment_callback,
445 .redirect_connect_callback = builtin_redirect_connect_callback,
446 .builtin_server_rx_callback = http_server_rx_callback,
447 .session_reset_callback = builtin_session_reset_callback
450 /* Abuse VPP's input queue */
452 create_api_loopback (vlib_main_t * vm)
454 http_server_main_t *hsm = &http_server_main;
455 vl_api_memclnt_create_t _m, *mp = &_m;
456 extern void vl_api_memclnt_create_t_handler (vl_api_memclnt_create_t *);
457 api_main_t *am = &api_main;
458 vl_shmem_hdr_t *shmem_hdr;
459 uword *event_data = 0, event_type;
463 * Create a "loopback" API client connection
464 * Don't do things like this unless you know what you're doing...
467 shmem_hdr = am->shmem_hdr;
468 hsm->vl_input_queue = shmem_hdr->vl_input_queue;
469 memset (mp, 0, sizeof (*mp));
470 mp->_vl_msg_id = VL_API_MEMCLNT_CREATE;
471 mp->context = 0xFEEDFACE;
472 mp->input_queue = (u64) hsm->vl_input_queue;
473 strncpy ((char *) mp->name, "tcp_http_server", sizeof (mp->name) - 1);
475 vl_api_memclnt_create_t_handler (mp);
478 hsm->node_index = vlib_get_current_process (vm)->node_runtime.node_index;
479 vlib_process_wait_for_event_or_clock (vm, 1.0);
480 event_type = vlib_process_get_events (vm, &event_data);
490 clib_warning ("unknown event_type %d", event_type);
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_BUILTIN_APP;
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 if (hsm->my_client_index == (u32) ~ 0)
549 if (create_api_loopback (vm))
553 num_threads = 1 /* main thread */ + vtm->n_threads;
554 vec_validate (http_server_main.vpp_queue, num_threads - 1);
556 if (server_attach ())
558 clib_warning ("failed to attach server");
561 if (server_listen ())
563 clib_warning ("failed to start listening");
569 /* Get our api client index */
571 vl_api_memclnt_create_reply_t_handler (vl_api_memclnt_create_reply_t * mp)
573 vlib_main_t *vm = vlib_get_main ();
574 http_server_main_t *hsm = &http_server_main;
575 hsm->my_client_index = mp->index;
576 vlib_process_signal_event (vm, hsm->node_index, 1 /* evt */ ,
580 #define foreach_tcp_http_server_api_msg \
581 _(MEMCLNT_CREATE_REPLY, memclnt_create_reply) \
583 static clib_error_t *
584 tcp_http_server_api_hookup (vlib_main_t * vm)
586 vl_msg_api_msg_config_t _c, *c = &_c;
588 /* Hook up client-side static APIs to our handlers */
589 #define _(N,n) do { \
590 c->id = VL_API_##N; \
592 c->handler = vl_api_##n##_t_handler; \
593 c->cleanup = vl_noop_handler; \
594 c->endian = vl_api_##n##_t_endian; \
595 c->print = vl_api_##n##_t_print; \
596 c->size = sizeof(vl_api_##n##_t); \
597 c->traced = 1; /* trace, so these msgs print */ \
598 c->replay = 0; /* don't replay client create/delete msgs */ \
599 c->message_bounce = 0; /* don't bounce this message */ \
600 vl_msg_api_config(c);} while (0);
602 foreach_tcp_http_server_api_msg;
608 static clib_error_t *
609 server_create_command_fn (vlib_main_t * vm,
610 unformat_input_t * input, vlib_cli_command_t * cmd)
614 tcp_http_server_api_hookup (vm);
615 vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */ );
616 rv = server_create (vm);
622 return clib_error_return (0, "server_create returned %d", rv);
628 VLIB_CLI_COMMAND (server_create_command, static) =
630 .path = "test http server",
631 .short_help = "test http server",
632 .function = server_create_command_fn,
636 static clib_error_t *
637 builtin_http_server_main_init (vlib_main_t * vm)
639 http_server_main_t *hsm = &http_server_main;
640 hsm->my_client_index = ~0;
646 VLIB_INIT_FUNCTION (builtin_http_server_main_init);
649 * fd.io coding-style-patch-verification: ON
652 * eval: (c-set-style "gnu")