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;
35 HTTP_STATE_ESTABLISHED,
37 } http_session_state_t;
40 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
41 #define _(type, name) type name;
42 foreach_app_session_field
46 u32 vpp_session_index;
47 u64 vpp_session_handle;
52 http_session_t **sessions;
53 clib_rwlock_t sessions_lock;
54 u32 **session_to_http_session;
56 svm_msg_q_t **vpp_queue;
58 uword *handler_by_get_request;
60 u32 *free_http_cli_process_node_indices;
62 /* Sever's event queue */
63 svm_queue_t *vl_input_queue;
65 /* API client handle */
70 /* process node index for evnt scheduling */
74 u32 private_segment_size;
78 vlib_main_t *vlib_main;
81 http_server_main_t http_server_main;
84 http_server_sessions_reader_lock (void)
86 clib_rwlock_reader_lock (&http_server_main.sessions_lock);
90 http_server_sessions_reader_unlock (void)
92 clib_rwlock_reader_unlock (&http_server_main.sessions_lock);
96 http_server_sessions_writer_lock (void)
98 clib_rwlock_writer_lock (&http_server_main.sessions_lock);
102 http_server_sessions_writer_unlock (void)
104 clib_rwlock_writer_unlock (&http_server_main.sessions_lock);
108 http_server_session_lookup_add (u32 thread_index, u32 s_index, u32 hs_index)
110 http_server_main_t *hsm = &http_server_main;
111 vec_validate (hsm->session_to_http_session[thread_index], s_index);
112 hsm->session_to_http_session[thread_index][s_index] = hs_index;
116 http_server_session_lookup_del (u32 thread_index, u32 s_index)
118 http_server_main_t *hsm = &http_server_main;
119 hsm->session_to_http_session[thread_index][s_index] = ~0;
122 static http_session_t *
123 http_server_session_lookup (u32 thread_index, u32 s_index)
125 http_server_main_t *hsm = &http_server_main;
128 if (s_index < vec_len (hsm->session_to_http_session[thread_index]))
130 hs_index = hsm->session_to_http_session[thread_index][s_index];
131 if (hs_index < vec_len (hsm->sessions[thread_index]))
132 return &hsm->sessions[thread_index][hs_index];
137 static http_session_t *
138 http_server_session_alloc (u32 thread_index)
140 http_server_main_t *hsm = &http_server_main;
142 pool_get (hsm->sessions[thread_index], hs);
143 memset (hs, 0, sizeof (*hs));
144 hs->session_index = hs - hsm->sessions[thread_index];
145 hs->thread_index = thread_index;
149 static http_session_t *
150 http_server_session_get (u32 thread_index, u32 hs_index)
152 http_server_main_t *hsm = &http_server_main;
153 return pool_elt_at_index (hsm->sessions[thread_index], hs_index);
157 http_server_session_free (http_session_t * hs)
159 http_server_main_t *hsm = &http_server_main;
160 pool_put (hsm->sessions[hs->thread_index], hs);
162 memset (hs, 0xfa, sizeof (*hs));
166 http_server_session_cleanup (http_session_t * hs)
170 http_server_session_lookup_del (hs->thread_index, hs->vpp_session_index);
171 vec_free (hs->rx_buf);
172 http_server_session_free (hs);
176 http_process_free (http_server_args * args)
178 vlib_node_runtime_t *rt;
179 vlib_main_t *vm = &vlib_global_main;
180 http_server_main_t *hsm = &http_server_main;
183 http_server_args **save_args;
185 node_index = args->node_index;
186 ASSERT (node_index != 0);
188 n = vlib_get_node (vm, node_index);
189 rt = vlib_node_get_runtime (vm, n->index);
190 save_args = vlib_node_get_runtime_data (vm, n->index);
192 /* Reset process session pointer */
193 clib_mem_free (*save_args);
196 /* Turn off the process node */
197 vlib_node_set_state (vm, rt->node_index, VLIB_NODE_STATE_DISABLED);
199 /* add node index to the freelist */
200 vec_add1 (hsm->free_http_cli_process_node_indices, node_index);
204 static const char *http_ok =
205 "HTTP/1.1 200 OK\r\n";
207 static const char *http_response =
208 "Content-Type: text/html\r\n"
209 "Expires: Mon, 11 Jan 1970 10:10:10 GMT\r\n"
210 "Connection: close \r\n"
211 "Pragma: no-cache\r\n"
212 "Content-Length: %d\r\n\r\n%s";
214 static const char *http_error_template =
216 "Content-Type: text/html\r\n"
217 "Expires: Mon, 11 Jan 1970 10:10:10 GMT\r\n"
218 "Connection: close\r\n"
219 "Pragma: no-cache\r\n"
220 "Content-Length: 0\r\n\r\n";
222 /* Header, including incantation to suppress favicon.ico requests */
223 static const char *html_header_template =
224 "<html><head><title>%v</title></head>"
225 "<link rel=\"icon\" href=\"data:,\">"
228 static const char *html_footer =
229 "</pre></body></html>\r\n";
231 static const char *html_header_static =
232 "<html><head><title>static reply</title></head>"
233 "<link rel=\"icon\" href=\"data:,\">"
234 "<body><pre>hello</pre></body></html>\r\n";
237 static u8 *static_http;
238 static u8 *static_ok;
241 http_cli_output (uword arg, u8 * buffer, uword buffer_bytes)
243 u8 **output_vecp = (u8 **) arg;
247 output_vec = *output_vecp;
249 offset = vec_len (output_vec);
250 vec_validate (output_vec, offset + buffer_bytes - 1);
251 clib_memcpy_fast (output_vec + offset, buffer, buffer_bytes);
253 *output_vecp = output_vec;
257 send_data (http_session_t * hs, u8 * data)
259 http_server_main_t *hsm = &http_server_main;
260 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
261 vlib_main_t *vm = vlib_get_main ();
262 f64 last_sent_timer = vlib_time_now (vm);
263 u32 offset, bytes_to_send;
266 bytes_to_send = vec_len (data);
269 while (bytes_to_send > 0)
273 actual_transfer = svm_fifo_enqueue_nowait
274 (hs->tx_fifo, bytes_to_send, data + offset);
276 /* Made any progress? */
277 if (actual_transfer <= 0)
279 vlib_process_suspend (vm, delay);
280 /* 10s deadman timer */
281 if (vlib_time_now (vm) > last_sent_timer + 10.0)
283 a->handle = hs->vpp_session_handle;
284 a->app_index = hsm->app_index;
285 vnet_disconnect_session (a);
288 /* Exponential backoff, within reason */
294 last_sent_timer = vlib_time_now (vm);
295 offset += actual_transfer;
296 bytes_to_send -= actual_transfer;
298 if (svm_fifo_set_event (hs->tx_fifo))
299 session_send_io_evt_to_thread (hs->tx_fifo,
300 SESSION_IO_EVT_TX_FLUSH);
307 send_error (http_session_t * hs, char *str)
311 data = format (0, http_error_template, str);
312 send_data (hs, data);
317 http_cli_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
320 u8 *request = 0, *reply = 0, *http = 0, *html = 0;
321 http_server_main_t *hsm = &http_server_main;
322 http_server_args **save_args;
323 http_server_args *args;
324 unformat_input_t input;
328 save_args = vlib_node_get_runtime_data (hsm->vlib_main, rt->node_index);
331 http_server_sessions_reader_lock ();
333 hs = http_server_session_get (args->thread_index, args->hs_index);
336 request = hs->rx_buf;
337 if (vec_len (request) < 7)
339 send_error (hs, "400 Bad Request");
343 for (i = 0; i < vec_len (request) - 4; i++)
345 if (request[i] == 'G' &&
346 request[i + 1] == 'E' &&
347 request[i + 2] == 'T' && request[i + 3] == ' ')
351 send_error (hs, "400 Bad Request");
356 vec_delete (request, i + 5, 0);
358 /* Replace slashes with spaces, stop at the end of the path */
362 if (request[i] == '/')
364 else if (request[i] == ' ')
366 /* vlib_cli_input is vector-based, no need for a NULL */
367 _vec_len (request) = i;
371 /* Should never happen */
372 if (i == vec_len (request))
376 /* Generate the html header */
377 html = format (0, html_header_template, request /* title */ );
379 /* Run the command */
380 unformat_init_vector (&input, vec_dup (request));
381 vlib_cli_input (vm, &input, http_cli_output, (uword) & reply);
382 unformat_free (&input);
385 /* Generate the html page */
386 html = format (html, "%v", reply);
387 html = format (html, html_footer);
388 /* And the http reply */
389 http = format (0, http_ok, vec_len (http_ok));
390 http = format (http, http_response, vec_len (html), html);
393 send_data (hs, http);
397 http_server_sessions_reader_unlock ();
402 http_process_free (args);
407 alloc_http_process (http_server_args * args)
411 http_server_main_t *hsm = &http_server_main;
412 vlib_main_t *vm = hsm->vlib_main;
413 uword l = vec_len (hsm->free_http_cli_process_node_indices);
414 http_server_args **save_args;
416 if (vec_len (hsm->free_http_cli_process_node_indices) > 0)
418 n = vlib_get_node (vm, hsm->free_http_cli_process_node_indices[l - 1]);
419 vlib_node_set_state (vm, n->index, VLIB_NODE_STATE_POLLING);
420 _vec_len (hsm->free_http_cli_process_node_indices) = l - 1;
424 static vlib_node_registration_t r = {
425 .function = http_cli_process,
426 .type = VLIB_NODE_TYPE_PROCESS,
427 .process_log2_n_stack_bytes = 16,
428 .runtime_data_bytes = sizeof (void *),
431 name = (char *) format (0, "http-cli-%d", l);
433 vlib_register_node (vm, &r);
436 n = vlib_get_node (vm, r.index);
439 /* Save the node index in the args. It won't be zero. */
440 args->node_index = n->index;
442 /* Save the args (pointer) in the node runtime */
443 save_args = vlib_node_get_runtime_data (vm, n->index);
446 vlib_start_process (vm, n->runtime_index);
450 alloc_http_process_callback (void *cb_args)
452 alloc_http_process ((http_server_args *) cb_args);
456 session_rx_request (http_session_t * hs)
458 u32 max_dequeue, cursize;
461 cursize = vec_len (hs->rx_buf);
462 max_dequeue = svm_fifo_max_dequeue (hs->rx_fifo);
463 if (PREDICT_FALSE (max_dequeue == 0))
466 vec_validate (hs->rx_buf, cursize + max_dequeue - 1);
467 n_read = app_recv_stream_raw (hs->rx_fifo, hs->rx_buf + cursize,
468 max_dequeue, 0, 0 /* peek */ );
469 ASSERT (n_read == max_dequeue);
470 if (svm_fifo_is_empty (hs->rx_fifo))
471 svm_fifo_unset_event (hs->rx_fifo);
473 _vec_len (hs->rx_buf) = cursize + n_read;
478 http_server_rx_callback (session_t * s)
480 http_server_args *args;
484 http_server_sessions_reader_lock ();
486 hs = http_server_session_lookup (s->thread_index, s->session_index);
487 if (!hs || hs->session_state != HTTP_STATE_ESTABLISHED)
490 rv = session_rx_request (hs);
494 /* send the command to a new/recycled vlib process */
495 args = clib_mem_alloc (sizeof (*args));
496 args->hs_index = hs->session_index;
497 args->thread_index = hs->thread_index;
499 http_server_sessions_reader_unlock ();
501 /* Send an RPC request via the thread-0 input node */
502 if (vlib_get_thread_index () != 0)
503 session_send_rpc_evt_to_thread (0, alloc_http_process_callback, args);
505 alloc_http_process (args);
510 http_server_rx_callback_static (session_t * s)
512 http_server_main_t *hsm = &http_server_main;
513 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
519 hs = http_server_session_lookup (s->thread_index, s->session_index);
520 if (!hs || hs->session_state == HTTP_STATE_CLOSED)
523 /* ok 200 was sent */
524 if (hs->session_state == HTTP_STATE_OK_SENT)
527 rv = session_rx_request (hs);
531 request = hs->rx_buf;
532 request_len = vec_len (request);
533 if (vec_len (request) < 7)
535 send_error (hs, "400 Bad Request");
539 for (i = 0; i < request_len - 4; i++)
541 if (request[i] == 'G' &&
542 request[i + 1] == 'E' &&
543 request[i + 2] == 'T' && request[i + 3] == ' ')
546 send_error (hs, "400 Bad Request");
551 /* check for the end sequence: /r/n/r/n */
552 if (request[request_len - 1] != 0xa || request[request_len - 3] != 0xa
553 || request[request_len - 2] != 0xd || request[request_len - 4] != 0xd)
556 /* send 200 OK first */
557 send_data (hs, static_ok);
558 hs->session_state = HTTP_STATE_OK_SENT;
562 send_data (hs, static_http);
563 http_server_session_cleanup (hs);
566 a->handle = session_handle (s);
567 a->app_index = hsm->app_index;
568 vnet_disconnect_session (a);
572 svm_fifo_set_event (hs->rx_fifo);
573 session_send_io_evt_to_thread (hs->rx_fifo, FIFO_EVENT_BUILTIN_RX);
581 http_server_session_accept_callback (session_t * s)
583 http_server_main_t *hsm = &http_server_main;
586 hsm->vpp_queue[s->thread_index] =
587 session_manager_get_vpp_event_queue (s->thread_index);
590 http_server_sessions_writer_lock ();
592 hs = http_server_session_alloc (s->thread_index);
593 http_server_session_lookup_add (s->thread_index, s->session_index,
595 hs->rx_fifo = s->rx_fifo;
596 hs->tx_fifo = s->tx_fifo;
597 hs->vpp_session_index = s->session_index;
598 hs->vpp_session_handle = session_handle (s);
599 hs->session_state = HTTP_STATE_ESTABLISHED;
602 http_server_sessions_writer_unlock ();
604 s->session_state = SESSION_STATE_READY;
609 http_server_session_disconnect_callback (session_t * s)
611 http_server_main_t *hsm = &http_server_main;
612 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
616 http_server_sessions_writer_lock ();
618 hs = http_server_session_lookup (s->thread_index, s->session_index);
619 http_server_session_cleanup (hs);
622 http_server_sessions_writer_unlock ();
624 a->handle = session_handle (s);
625 a->app_index = hsm->app_index;
626 vnet_disconnect_session (a);
630 http_server_session_reset_callback (session_t * s)
632 http_server_main_t *hsm = &http_server_main;
633 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
637 http_server_sessions_writer_lock ();
639 hs = http_server_session_lookup (s->thread_index, s->session_index);
640 http_server_session_cleanup (hs);
643 http_server_sessions_writer_unlock ();
645 a->handle = session_handle (s);
646 a->app_index = hsm->app_index;
647 vnet_disconnect_session (a);
651 http_server_session_connected_callback (u32 app_index, u32 api_context,
652 session_t * s, u8 is_fail)
654 clib_warning ("called...");
659 http_server_add_segment_callback (u32 client_index, u64 segment_handle)
661 clib_warning ("called...");
665 static session_cb_vft_t http_server_session_cb_vft = {
666 .session_accept_callback = http_server_session_accept_callback,
667 .session_disconnect_callback = http_server_session_disconnect_callback,
668 .session_connected_callback = http_server_session_connected_callback,
669 .add_segment_callback = http_server_add_segment_callback,
670 .builtin_app_rx_callback = http_server_rx_callback,
671 .session_reset_callback = http_server_session_reset_callback
675 http_server_attach ()
677 vnet_app_add_tls_cert_args_t _a_cert, *a_cert = &_a_cert;
678 vnet_app_add_tls_key_args_t _a_key, *a_key = &_a_key;
679 http_server_main_t *hsm = &http_server_main;
680 u64 options[APP_OPTIONS_N_OPTIONS];
681 vnet_app_attach_args_t _a, *a = &_a;
682 u32 segment_size = 128 << 20;
684 clib_memset (a, 0, sizeof (*a));
685 clib_memset (options, 0, sizeof (options));
687 if (hsm->private_segment_size)
688 segment_size = hsm->private_segment_size;
690 a->api_client_index = ~0;
691 a->name = format (0, "test_http_server");
692 a->session_cb_vft = &http_server_session_cb_vft;
693 a->options = options;
694 a->options[APP_OPTIONS_SEGMENT_SIZE] = segment_size;
695 a->options[APP_OPTIONS_RX_FIFO_SIZE] =
696 hsm->fifo_size ? hsm->fifo_size : 8 << 10;
697 a->options[APP_OPTIONS_TX_FIFO_SIZE] =
698 hsm->fifo_size ? hsm->fifo_size : 32 << 10;
699 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
700 a->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = hsm->prealloc_fifos;
702 if (vnet_application_attach (a))
705 clib_warning ("failed to attach server");
709 hsm->app_index = a->app_index;
711 clib_memset (a_cert, 0, sizeof (*a_cert));
712 a_cert->app_index = a->app_index;
713 vec_validate (a_cert->cert, test_srv_crt_rsa_len);
714 clib_memcpy_fast (a_cert->cert, test_srv_crt_rsa, test_srv_crt_rsa_len);
715 vnet_app_add_tls_cert (a_cert);
717 clib_memset (a_key, 0, sizeof (*a_key));
718 a_key->app_index = a->app_index;
719 vec_validate (a_key->key, test_srv_key_rsa_len);
720 clib_memcpy_fast (a_key->key, test_srv_key_rsa, test_srv_key_rsa_len);
721 vnet_app_add_tls_key (a_key);
727 http_server_listen ()
729 http_server_main_t *hsm = &http_server_main;
730 vnet_listen_args_t _a, *a = &_a;
731 clib_memset (a, 0, sizeof (*a));
732 a->app_index = hsm->app_index;
733 a->uri = "tcp://0.0.0.0/80";
735 a->uri = (char *) hsm->uri;
736 return vnet_bind_uri (a);
740 http_server_create (vlib_main_t * vm)
742 vlib_thread_main_t *vtm = vlib_get_thread_main ();
743 http_server_main_t *hsm = &http_server_main;
746 num_threads = 1 /* main thread */ + vtm->n_threads;
747 vec_validate (hsm->vpp_queue, num_threads - 1);
748 vec_validate (hsm->sessions, num_threads - 1);
749 vec_validate (hsm->session_to_http_session, num_threads - 1);
751 clib_rwlock_init (&hsm->sessions_lock);
753 if (http_server_attach ())
755 clib_warning ("failed to attach server");
758 if (http_server_listen ())
760 clib_warning ("failed to start listening");
766 static clib_error_t *
767 http_server_create_command_fn (vlib_main_t * vm,
768 unformat_input_t * input,
769 vlib_cli_command_t * cmd)
771 http_server_main_t *hsm = &http_server_main;
776 hsm->prealloc_fifos = 0;
777 hsm->private_segment_size = 0;
780 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
782 if (unformat (input, "static"))
784 else if (unformat (input, "prealloc-fifos %d", &hsm->prealloc_fifos))
786 else if (unformat (input, "private-segment-size %U",
787 unformat_memory_size, &seg_size))
789 if (seg_size >= 0x100000000ULL)
791 vlib_cli_output (vm, "private segment size %llu, too large",
795 hsm->private_segment_size = seg_size;
797 else if (unformat (input, "fifo-size %d", &hsm->fifo_size))
798 hsm->fifo_size <<= 10;
799 else if (unformat (input, "uri %s", &hsm->uri))
802 return clib_error_return (0, "unknown input `%U'",
803 format_unformat_error, input);
805 if (hsm->my_client_index != (u32) ~ 0)
806 return clib_error_return (0, "test http server is already running");
808 vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */ );
812 http_server_session_cb_vft.builtin_app_rx_callback =
813 http_server_rx_callback_static;
814 html = format (0, html_header_static);
815 static_http = format (0, http_response, vec_len (html), html);
816 static_ok = format (0, http_ok);
818 rv = http_server_create (vm);
824 return clib_error_return (0, "server_create returned %d", rv);
830 VLIB_CLI_COMMAND (http_server_create_command, static) =
832 .path = "test http server",
833 .short_help = "test http server",
834 .function = http_server_create_command_fn,
838 static clib_error_t *
839 http_server_main_init (vlib_main_t * vm)
841 http_server_main_t *hsm = &http_server_main;
843 hsm->my_client_index = ~0;
848 VLIB_INIT_FUNCTION (http_server_main_init);
851 * fd.io coding-style-patch-verification: ON
854 * eval: (c-set-style "gnu")