2 * Copyright (c) 2022 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 <http/http.h>
17 #include <vnet/session/session.h>
18 #include <http/http_timer.h>
20 static http_main_t http_main;
22 #define HTTP_FIFO_THRESH (16 << 10)
24 const char *http_status_code_str[] = {
25 #define _(c, s, str) str,
26 foreach_http_status_code
30 const char *http_content_type_str[] = {
31 #define _(s, str) str,
32 foreach_http_content_type
36 const http_buffer_type_t msg_to_buf_type[] = {
37 [HTTP_MSG_DATA_INLINE] = HTTP_BUFFER_FIFO,
38 [HTTP_MSG_DATA_PTR] = HTTP_BUFFER_PTR,
41 static inline http_worker_t *
42 http_worker_get (u32 thread_index)
44 return &http_main.wrk[thread_index];
48 http_conn_alloc_w_thread (u32 thread_index)
50 http_worker_t *wrk = http_worker_get (thread_index);
53 pool_get_zero (wrk->conn_pool, hc);
54 hc->c_thread_index = thread_index;
55 hc->h_hc_index = hc - wrk->conn_pool;
56 hc->h_pa_session_handle = SESSION_INVALID_HANDLE;
57 hc->h_tc_session_handle = SESSION_INVALID_HANDLE;
58 return hc->h_hc_index;
61 static inline http_conn_t *
62 http_conn_get_w_thread (u32 hc_index, u32 thread_index)
64 http_worker_t *wrk = http_worker_get (thread_index);
65 return pool_elt_at_index (wrk->conn_pool, hc_index);
69 http_conn_free (http_conn_t *hc)
71 http_worker_t *wrk = http_worker_get (hc->c_thread_index);
72 pool_put (wrk->conn_pool, hc);
76 http_listener_alloc (void)
78 http_main_t *hm = &http_main;
81 pool_get_zero (hm->listener_pool, lhc);
82 lhc->c_c_index = lhc - hm->listener_pool;
83 return lhc->c_c_index;
87 http_listener_get (u32 lhc_index)
89 return pool_elt_at_index (http_main.listener_pool, lhc_index);
93 http_listener_free (http_conn_t *lhc)
95 http_main_t *hm = &http_main;
98 memset (lhc, 0xfc, sizeof (*lhc));
99 pool_put (hm->listener_pool, lhc);
103 http_disconnect_transport (http_conn_t *hc)
105 vnet_disconnect_args_t a = {
106 .handle = hc->h_tc_session_handle,
107 .app_index = http_main.app_index,
110 hc->state = HTTP_CONN_STATE_CLOSED;
112 if (vnet_disconnect_session (&a))
113 clib_warning ("disconnect returned");
117 http_conn_timeout_cb (void *hc_handlep)
122 hs_handle = pointer_to_uword (hc_handlep);
123 hc = http_conn_get_w_thread (hs_handle & 0x00FFFFFF, hs_handle >> 24);
125 HTTP_DBG (1, "terminate thread %d index %d hs %llx", hs_handle >> 24,
126 hs_handle & 0x00FFFFFF, hc);
130 hc->timer_handle = ~0;
131 session_transport_closing_notify (&hc->connection);
132 http_disconnect_transport (hc);
136 http_ts_accept_callback (session_t *ts)
138 session_t *ts_listener, *as, *asl;
139 app_worker_t *app_wrk;
140 http_conn_t *lhc, *hc;
141 u32 hc_index, thresh;
144 ts_listener = listen_session_get_from_handle (ts->listener_handle);
145 lhc = http_listener_get (ts_listener->opaque);
147 hc_index = http_conn_alloc_w_thread (ts->thread_index);
148 hc = http_conn_get_w_thread (hc_index, ts->thread_index);
149 clib_memcpy_fast (hc, lhc, sizeof (*lhc));
150 hc->c_thread_index = vlib_get_thread_index ();
151 hc->h_hc_index = hc_index;
153 hc->h_tc_session_handle = session_handle (ts);
154 hc->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
156 hc->state = HTTP_CONN_STATE_ESTABLISHED;
157 hc->req_state = HTTP_REQ_STATE_WAIT_METHOD;
159 ts->session_state = SESSION_STATE_READY;
160 ts->opaque = hc_index;
163 * Alloc session and initialize
165 as = session_alloc (hc->c_thread_index);
166 as->session_state = SESSION_STATE_CREATED;
167 hc->c_s_index = as->session_index;
169 as->app_wrk_index = hc->h_pa_wrk_index;
170 as->connection_index = hc->c_c_index;
171 as->session_state = SESSION_STATE_ACCEPTING;
173 asl = listen_session_get_from_handle (lhc->h_pa_session_handle);
174 as->session_type = asl->session_type;
175 as->listener_handle = lhc->h_pa_session_handle;
178 * Init session fifos and notify app
180 if ((rv = app_worker_init_accepted (as)))
182 HTTP_DBG (1, "failed to allocate fifos");
187 hc->h_pa_session_handle = session_handle (as);
188 hc->h_pa_wrk_index = as->app_wrk_index;
189 app_wrk = app_worker_get (as->app_wrk_index);
191 HTTP_DBG (1, "Accepted on listener %u new connection [%u]%x",
192 ts_listener->opaque, vlib_get_thread_index (), hc_index);
194 if ((rv = app_worker_accept_notify (app_wrk, as)))
196 HTTP_DBG (0, "app accept returned");
201 /* Avoid enqueuing small chunks of data on transport tx notifications. If
202 * the fifo is small (under 16K) we set the threshold to it's size, meaning
203 * a notification will be given when the fifo empties.
205 ts = session_get_from_handle (hc->h_tc_session_handle);
206 thresh = clib_min (svm_fifo_size (ts->tx_fifo), HTTP_FIFO_THRESH);
207 svm_fifo_set_deq_thresh (ts->tx_fifo, thresh);
209 http_conn_timer_start (hc);
215 http_ts_connected_callback (u32 http_app_index, u32 hc_index, session_t *ts,
218 clib_warning ("not supported");
223 http_ts_disconnect_callback (session_t *ts)
227 hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
229 if (hc->state < HTTP_CONN_STATE_TRANSPORT_CLOSED)
230 hc->state = HTTP_CONN_STATE_TRANSPORT_CLOSED;
232 if (!svm_fifo_max_dequeue_cons (ts->rx_fifo))
233 session_transport_closing_notify (&hc->connection);
237 http_ts_reset_callback (session_t *ts)
241 ctx = http_conn_get_w_thread (ts->opaque, ts->thread_index);
243 if (ctx->state < HTTP_CONN_STATE_TRANSPORT_CLOSED)
244 ctx->state = HTTP_CONN_STATE_TRANSPORT_CLOSED;
246 if (!svm_fifo_max_dequeue_cons (ts->rx_fifo))
247 session_transport_reset_notify (&ctx->connection);
251 * http error boilerplate
253 static const char *http_error_template = "HTTP/1.1 %s\r\n"
255 "Content-Type: text/html\r\n"
256 "Connection: close\r\n"
257 "Pragma: no-cache\r\n"
258 "Content-Length: 0\r\n\r\n";
261 * http response boilerplate
263 static const char *http_response_template = "HTTP/1.1 200 OK\r\n"
265 "Expires: %U GMT\r\n"
266 "Server: VPP Static\r\n"
267 "Content-Type: %s\r\n"
268 "Content-Length: %lu\r\n\r\n";
271 send_data (http_conn_t *hc, u8 *data, u32 length, u32 offset)
273 const u32 max_burst = 64 << 10;
278 ts = session_get_from_handle (hc->h_tc_session_handle);
280 to_send = clib_min (length - offset, max_burst);
281 sent = svm_fifo_enqueue (ts->tx_fifo, to_send, data + offset);
286 if (svm_fifo_set_event (ts->tx_fifo))
287 session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX);
289 return (offset + sent);
293 send_error (http_conn_t *hc, http_status_code_t ec)
295 http_main_t *hm = &http_main;
299 if (ec >= HTTP_N_STATUS)
300 ec = HTTP_STATUS_INTERNAL_ERROR;
302 now = clib_timebase_now (&hm->timebase);
303 data = format (0, http_error_template, http_status_code_str[ec],
304 format_clib_timebase_time, now);
305 send_data (hc, data, vec_len (data), 0);
310 read_request (http_conn_t *hc)
312 u32 max_deq, cursize;
316 ts = session_get_from_handle (hc->h_tc_session_handle);
318 cursize = vec_len (hc->rx_buf);
319 max_deq = svm_fifo_max_dequeue (ts->rx_fifo);
320 if (PREDICT_FALSE (max_deq == 0))
323 vec_validate (hc->rx_buf, cursize + max_deq - 1);
324 n_read = svm_fifo_dequeue (ts->rx_fifo, max_deq, hc->rx_buf + cursize);
325 ASSERT (n_read == max_deq);
327 if (svm_fifo_is_empty (ts->rx_fifo))
328 svm_fifo_unset_event (ts->rx_fifo);
330 _vec_len (hc->rx_buf) = cursize + n_read;
335 v_find_index (u8 *vec, u32 offset, char *str)
337 int start_index = offset;
338 u32 slen = (u32) strnlen_s_inline (str, 8);
339 u32 vlen = vec_len (vec);
346 for (; start_index < (vlen - slen); start_index++)
348 if (!memcmp (vec + start_index, str, slen))
356 * waiting for request method from peer - parse request method and data
359 state_wait_method (http_conn_t *hc, transport_send_params_t *sp)
361 http_status_code_t ec;
362 app_worker_t *app_wrk;
369 rv = read_request (hc);
371 /* Nothing yet, wait for data or timer expire */
375 if (vec_len (hc->rx_buf) < 8)
377 ec = HTTP_STATUS_BAD_REQUEST;
381 if ((i = v_find_index (hc->rx_buf, 0, "GET ")) >= 0)
383 hc->method = HTTP_REQ_GET;
384 hc->rx_buf_offset = i + 5;
386 i = v_find_index (hc->rx_buf, hc->rx_buf_offset, "HTTP");
389 ec = HTTP_STATUS_BAD_REQUEST;
393 len = i - hc->rx_buf_offset - 1;
395 else if ((i = v_find_index (hc->rx_buf, 0, "POST ")) >= 0)
397 hc->method = HTTP_REQ_POST;
398 hc->rx_buf_offset = i + 6;
399 len = vec_len (hc->rx_buf) - hc->rx_buf_offset - 1;
403 HTTP_DBG (0, "Unknown http method");
404 ec = HTTP_STATUS_METHOD_NOT_ALLOWED;
408 buf = &hc->rx_buf[hc->rx_buf_offset];
410 msg.type = HTTP_MSG_REQUEST;
411 msg.method_type = hc->method;
412 msg.content_type = HTTP_CONTENT_TEXT_HTML;
413 msg.data.type = HTTP_MSG_DATA_INLINE;
416 svm_fifo_seg_t segs[2] = { { (u8 *) &msg, sizeof (msg) }, { buf, len } };
418 as = session_get_from_handle (hc->h_pa_session_handle);
419 rv = svm_fifo_enqueue_segments (as->rx_fifo, segs, 2, 0 /* allow partial */);
420 if (rv < 0 || rv != sizeof (msg) + len)
422 clib_warning ("failed app enqueue");
423 /* This should not happen as we only handle 1 request per session,
424 * and fifo is allocated, but going forward we should consider
429 vec_free (hc->rx_buf);
430 hc->req_state = HTTP_REQ_STATE_WAIT_APP;
432 app_wrk = app_worker_get_if_valid (as->app_wrk_index);
433 app_worker_lock_and_send_event (app_wrk, as, SESSION_IO_EVT_RX);
440 session_transport_closing_notify (&hc->connection);
441 http_disconnect_transport (hc);
447 * waiting for data from app
450 state_wait_app (http_conn_t *hc, transport_send_params_t *sp)
452 http_main_t *hm = &http_main;
453 http_status_code_t ec;
461 as = session_get_from_handle (hc->h_pa_session_handle);
463 rv = svm_fifo_dequeue (as->tx_fifo, sizeof (msg), (u8 *) &msg);
464 ASSERT (rv == sizeof (msg));
466 if (msg.type != HTTP_MSG_REPLY || msg.data.type > HTTP_MSG_DATA_PTR)
468 clib_warning ("unexpected msg type from app %u", msg.type);
469 ec = HTTP_STATUS_INTERNAL_ERROR;
473 if (msg.code != HTTP_STATUS_OK)
479 http_buffer_init (&hc->tx_buf, msg_to_buf_type[msg.data.type], as->tx_fifo,
483 * Add headers. For now:
489 now = clib_timebase_now (&hm->timebase);
490 header = format (0, http_response_template,
492 format_clib_timebase_time, now,
494 format_clib_timebase_time, now + 600.0,
496 http_content_type_str[msg.content_type],
500 offset = send_data (hc, header, vec_len (header), 0);
501 if (offset != vec_len (header))
503 clib_warning ("couldn't send response header!");
504 ec = HTTP_STATUS_INTERNAL_ERROR;
509 /* Start sending the actual data */
510 hc->req_state = HTTP_REQ_STATE_SEND_MORE_DATA;
512 ASSERT (sp->max_burst_size >= offset);
513 sp->max_burst_size -= offset;
520 hc->req_state = HTTP_REQ_STATE_WAIT_METHOD;
521 session_transport_closing_notify (&hc->connection);
522 http_disconnect_transport (hc);
524 /* stop state machine processing */
529 state_send_more_data (http_conn_t *hc, transport_send_params_t *sp)
531 u32 max_send = 64 << 10, n_segs;
532 http_buffer_t *hb = &hc->tx_buf;
537 max_send = clib_min (max_send, sp->max_burst_size);
538 ts = session_get_from_handle (hc->h_tc_session_handle);
539 if ((seg = http_buffer_get_segs (hb, max_send, &n_segs)))
540 sent = svm_fifo_enqueue_segments (ts->tx_fifo, seg, n_segs,
541 1 /* allow partial */);
545 /* Ask scheduler to notify app of deq event if needed */
546 sp->bytes_dequeued += http_buffer_drain (hb, sent);
547 sp->max_burst_size -= sent;
550 /* Not finished sending all data */
551 if (!http_buffer_is_drained (hb))
553 if (sent && svm_fifo_set_event (ts->tx_fifo))
554 session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX);
556 if (svm_fifo_max_enqueue (ts->tx_fifo) < HTTP_FIFO_THRESH)
558 /* Deschedule http session and wait for deq notification if
559 * underlying ts tx fifo almost full */
560 svm_fifo_add_want_deq_ntf (ts->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
561 transport_connection_deschedule (&hc->connection);
562 sp->flags |= TRANSPORT_SND_F_DESCHED;
567 if (sent && svm_fifo_set_event (ts->tx_fifo))
568 session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX_FLUSH);
570 /* Finished transaction, back to HTTP_REQ_STATE_WAIT_METHOD */
571 hc->req_state = HTTP_REQ_STATE_WAIT_METHOD;
572 http_buffer_free (&hc->tx_buf);
578 typedef int (*http_sm_handler) (http_conn_t *, transport_send_params_t *sp);
580 static http_sm_handler req_state_funcs[HTTP_REQ_N_STATES] = {
581 /* Waiting for GET, POST, etc. */
583 /* Wait for data from app */
586 state_send_more_data,
590 http_req_run_state_machine (http_conn_t *hc, transport_send_params_t *sp)
596 rv = req_state_funcs[hc->req_state](hc, sp);
602 /* Reset the session expiration timer */
603 http_conn_timer_update (hc);
607 http_ts_rx_callback (session_t *ts)
611 hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
613 if (hc->req_state != HTTP_REQ_STATE_WAIT_METHOD)
615 clib_warning ("tcp data in req state %u", hc->req_state);
619 http_req_run_state_machine (hc, 0);
621 if (hc->state == HTTP_CONN_STATE_TRANSPORT_CLOSED)
623 if (!svm_fifo_max_dequeue_cons (ts->rx_fifo))
624 session_transport_closing_notify (&hc->connection);
630 http_ts_builtin_tx_callback (session_t *ts)
634 hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
635 transport_connection_reschedule (&hc->connection);
641 http_ts_cleanup_callback (session_t *ts, session_cleanup_ntf_t ntf)
645 if (ntf == SESSION_CLEANUP_TRANSPORT)
648 hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
651 clib_warning ("no http connection for %u", ts->session_index);
655 vec_free (hc->rx_buf);
657 http_buffer_free (&hc->tx_buf);
658 http_conn_timer_stop (hc);
660 session_transport_delete_notify (&hc->connection);
665 http_add_segment_callback (u32 client_index, u64 segment_handle)
667 /* No-op for builtin */
672 http_del_segment_callback (u32 client_index, u64 segment_handle)
677 static session_cb_vft_t http_app_cb_vft = {
678 .session_accept_callback = http_ts_accept_callback,
679 .session_disconnect_callback = http_ts_disconnect_callback,
680 .session_connected_callback = http_ts_connected_callback,
681 .session_reset_callback = http_ts_reset_callback,
682 .session_cleanup_callback = http_ts_cleanup_callback,
683 .add_segment_callback = http_add_segment_callback,
684 .del_segment_callback = http_del_segment_callback,
685 .builtin_app_rx_callback = http_ts_rx_callback,
686 .builtin_app_tx_callback = http_ts_builtin_tx_callback,
689 static clib_error_t *
690 http_transport_enable (vlib_main_t *vm, u8 is_en)
692 vnet_app_detach_args_t _da, *da = &_da;
693 vnet_app_attach_args_t _a, *a = &_a;
694 u64 options[APP_OPTIONS_N_OPTIONS];
695 http_main_t *hm = &http_main;
699 da->app_index = hm->app_index;
700 da->api_client_index = APP_INVALID_INDEX;
701 vnet_application_detach (da);
705 vec_validate (hm->wrk, vlib_num_workers ());
707 clib_memset (a, 0, sizeof (*a));
708 clib_memset (options, 0, sizeof (options));
710 a->session_cb_vft = &http_app_cb_vft;
711 a->api_client_index = APP_INVALID_INDEX;
712 a->options = options;
713 a->name = format (0, "http");
714 a->options[APP_OPTIONS_SEGMENT_SIZE] = hm->first_seg_size;
715 a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = hm->add_seg_size;
716 a->options[APP_OPTIONS_RX_FIFO_SIZE] = hm->fifo_size;
717 a->options[APP_OPTIONS_TX_FIFO_SIZE] = hm->fifo_size;
718 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
719 a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
720 a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_IS_TRANSPORT_APP;
722 if (vnet_application_attach (a))
723 return clib_error_return (0, "failed to attach http app");
725 hm->app_index = a->app_index;
728 clib_timebase_init (&hm->timebase, 0 /* GMT */, CLIB_TIMEBASE_DAYLIGHT_NONE,
729 &vm->clib_time /* share the system clock */);
731 http_timers_init (vm, http_conn_timeout_cb);
737 http_transport_connect (transport_endpoint_cfg_t *tep)
743 http_start_listen (u32 app_listener_index, transport_endpoint_cfg_t *tep)
745 vnet_listen_args_t _args = {}, *args = &_args;
746 session_t *ts_listener, *app_listener;
747 http_main_t *hm = &http_main;
748 session_endpoint_cfg_t *sep;
749 app_worker_t *app_wrk;
750 transport_proto_t tp;
756 sep = (session_endpoint_cfg_t *) tep;
758 app_wrk = app_worker_get (sep->app_wrk_index);
759 app = application_get (app_wrk->app_index);
761 args->app_index = hm->app_index;
762 args->sep_ext = *sep;
763 args->sep_ext.ns_index = app->ns_index;
764 tp = sep->ext_cfg ? TRANSPORT_PROTO_TLS : TRANSPORT_PROTO_TCP;
765 args->sep_ext.transport_proto = tp;
767 if (vnet_listen (args))
768 return SESSION_INVALID_INDEX;
770 lhc_index = http_listener_alloc ();
771 lhc = http_listener_get (lhc_index);
773 /* Grab transport connection listener and link to http listener */
774 lhc->h_tc_session_handle = args->handle;
775 al = app_listener_get_w_handle (lhc->h_tc_session_handle);
776 ts_listener = app_listener_get_session (al);
777 ts_listener->opaque = lhc_index;
779 /* Grab application listener and link to http listener */
780 app_listener = listen_session_get (app_listener_index);
781 lhc->h_pa_wrk_index = sep->app_wrk_index;
782 lhc->h_pa_session_handle = listen_session_get_handle (app_listener);
783 lhc->c_s_index = app_listener_index;
784 lhc->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
790 http_stop_listen (u32 listener_index)
795 lhc = http_listener_get (listener_index);
797 vnet_unlisten_args_t a = {
798 .handle = lhc->h_tc_session_handle,
799 .app_index = http_main.app_index,
800 .wrk_map_index = 0 /* default wrk */
803 if ((rv = vnet_unlisten (&a)))
804 clib_warning ("unlisten returned %d", rv);
806 http_listener_free (lhc);
812 http_transport_close (u32 hc_index, u32 thread_index)
817 HTTP_DBG (1, "App disconnecting %x", hc_index);
819 hc = http_conn_get_w_thread (hc_index, thread_index);
820 as = session_get_from_handle (hc->h_pa_session_handle);
822 /* Nothing more to send, confirm close */
823 if (!svm_fifo_max_dequeue_cons (as->tx_fifo))
825 session_transport_closed_notify (&hc->connection);
826 http_disconnect_transport (hc);
830 /* Wait for all data to be written to ts */
831 hc->state = HTTP_CONN_STATE_APP_CLOSED;
835 static transport_connection_t *
836 http_transport_get_connection (u32 hc_index, u32 thread_index)
838 http_conn_t *hc = http_conn_get_w_thread (hc_index, thread_index);
839 return &hc->connection;
842 static transport_connection_t *
843 http_transport_get_listener (u32 listener_index)
845 http_conn_t *lhc = http_listener_get (listener_index);
846 return &lhc->connection;
850 http_app_tx_callback (void *session, transport_send_params_t *sp)
852 session_t *as = (session_t *) session;
853 u32 max_burst_sz, sent;
856 hc = http_conn_get_w_thread (as->connection_index, as->thread_index);
857 if (hc->req_state < HTTP_REQ_STATE_WAIT_APP)
859 clib_warning ("app data in req state %u", hc->req_state);
863 max_burst_sz = sp->max_burst_size * TRANSPORT_PACER_MIN_MSS;
864 sp->max_burst_size = max_burst_sz;
866 http_req_run_state_machine (hc, sp);
868 if (hc->state == HTTP_CONN_STATE_CLOSED)
870 if (!svm_fifo_max_dequeue_cons (as->rx_fifo))
871 http_disconnect_transport (hc);
874 sent = max_burst_sz - sp->max_burst_size;
876 return sent > 0 ? clib_max (sent / TRANSPORT_PACER_MIN_MSS, 1) : 0;
880 http_transport_get_endpoint (u32 hc_index, u32 thread_index,
881 transport_endpoint_t *tep, u8 is_lcl)
883 http_conn_t *hc = http_conn_get_w_thread (hc_index, thread_index);
886 ts = session_get_from_handle (hc->h_tc_session_handle);
887 session_get_endpoint (ts, tep, is_lcl);
891 format_http_connection (u8 *s, va_list *args)
893 http_conn_t *hc = va_arg (*args, http_conn_t *);
896 ts = session_get_from_handle (hc->h_tc_session_handle);
897 s = format (s, "[%d:%d][H] app_wrk %u ts %d:%d", hc->c_thread_index,
898 hc->c_s_index, hc->h_pa_wrk_index, ts->thread_index,
905 format_http_listener (u8 *s, va_list *args)
907 http_conn_t *lhc = va_arg (*args, http_conn_t *);
911 al = app_listener_get_w_handle (lhc->h_tc_session_handle);
912 lts = app_listener_get_session (al);
913 s = format (s, "[%d:%d][H] app_wrk %u ts %d:%d", lhc->c_thread_index,
914 lhc->c_s_index, lhc->h_pa_wrk_index, lts->thread_index,
921 format_http_conn_state (u8 *s, va_list *args)
923 http_conn_t *hc = va_arg (*args, http_conn_t *);
927 case HTTP_CONN_STATE_LISTEN:
928 s = format (s, "LISTEN");
930 case HTTP_CONN_STATE_CONNECTING:
931 s = format (s, "CONNECTING");
933 case HTTP_CONN_STATE_ESTABLISHED:
934 s = format (s, "ESTABLISHED");
936 case HTTP_CONN_STATE_TRANSPORT_CLOSED:
937 s = format (s, "TRANSPORT_CLOSED");
939 case HTTP_CONN_STATE_APP_CLOSED:
940 s = format (s, "APP_CLOSED");
942 case HTTP_CONN_STATE_CLOSED:
943 s = format (s, "CLOSED");
951 format_http_transport_connection (u8 *s, va_list *args)
953 u32 tc_index = va_arg (*args, u32);
954 u32 thread_index = va_arg (*args, u32);
955 u32 verbose = va_arg (*args, u32);
958 hc = http_conn_get_w_thread (tc_index, thread_index);
960 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_http_connection, hc);
964 format (s, "%-" SESSION_CLI_STATE_LEN "U", format_http_conn_state, hc);
966 s = format (s, "\n");
973 format_http_transport_listener (u8 *s, va_list *args)
975 u32 tc_index = va_arg (*args, u32);
976 u32 __clib_unused thread_index = va_arg (*args, u32);
977 u32 __clib_unused verbose = va_arg (*args, u32);
978 http_conn_t *lhc = http_listener_get (tc_index);
980 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_http_listener, lhc);
983 format (s, "%-" SESSION_CLI_STATE_LEN "U", format_http_conn_state, lhc);
987 static const transport_proto_vft_t http_proto = {
988 .enable = http_transport_enable,
989 .connect = http_transport_connect,
990 .start_listen = http_start_listen,
991 .stop_listen = http_stop_listen,
992 .close = http_transport_close,
993 .custom_tx = http_app_tx_callback,
994 .get_connection = http_transport_get_connection,
995 .get_listener = http_transport_get_listener,
996 .get_transport_endpoint = http_transport_get_endpoint,
997 .format_connection = format_http_transport_connection,
998 .format_listener = format_http_transport_listener,
999 .transport_options = {
1002 .tx_type = TRANSPORT_TX_INTERNAL,
1003 .service_type = TRANSPORT_SERVICE_APP,
1007 static clib_error_t *
1008 http_transport_init (vlib_main_t *vm)
1010 http_main_t *hm = &http_main;
1012 transport_register_protocol (TRANSPORT_PROTO_HTTP, &http_proto,
1013 FIB_PROTOCOL_IP4, ~0);
1014 transport_register_protocol (TRANSPORT_PROTO_HTTP, &http_proto,
1015 FIB_PROTOCOL_IP6, ~0);
1017 /* Default values, configurable via startup conf */
1018 hm->add_seg_size = 256 << 20;
1019 hm->first_seg_size = 32 << 20;
1020 hm->fifo_size = 512 << 10;
1025 VLIB_INIT_FUNCTION (http_transport_init);
1027 static clib_error_t *
1028 http_config_fn (vlib_main_t *vm, unformat_input_t *input)
1030 http_main_t *hm = &http_main;
1033 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1035 if (unformat (input, "first-segment-size %U", unformat_memory_size,
1038 hm->first_seg_size = clib_max (mem_sz, 1 << 20);
1039 if (hm->first_seg_size != mem_sz)
1040 clib_warning ("first seg size too small %u", mem_sz);
1042 else if (unformat (input, "add-segment-size %U", unformat_memory_size,
1045 hm->add_seg_size = clib_max (mem_sz, 1 << 20);
1046 if (hm->add_seg_size != mem_sz)
1047 clib_warning ("add seg size too small %u", mem_sz);
1049 else if (unformat (input, "fifo-size %U", unformat_memory_size, &mem_sz))
1051 hm->fifo_size = clib_clamp (mem_sz, 4 << 10, 2 << 30);
1052 if (hm->fifo_size != mem_sz)
1053 clib_warning ("invalid fifo size %lu", mem_sz);
1056 return clib_error_return (0, "unknown input `%U'",
1057 format_unformat_error, input);
1062 VLIB_EARLY_CONFIG_FUNCTION (http_config_fn, "http");
1064 VLIB_PLUGIN_REGISTER () = {
1065 .version = VPP_BUILD_VER,
1066 .description = "Hypertext Transfer Protocol (HTTP)",
1067 .default_disabled = 0,
1071 * fd.io coding-style-patch-verification: ON
1074 * eval: (c-set-style "gnu")