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 /* Nothing more to rx, propagate to app */
233 if (!svm_fifo_max_dequeue_cons (ts->rx_fifo))
234 session_transport_closing_notify (&hc->connection);
238 http_ts_reset_callback (session_t *ts)
242 hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
244 hc->state = HTTP_CONN_STATE_CLOSED;
245 http_buffer_free (&hc->tx_buf);
246 hc->req_state = HTTP_REQ_STATE_WAIT_METHOD;
247 session_transport_reset_notify (&hc->connection);
249 http_disconnect_transport (hc);
253 * http error boilerplate
255 static const char *http_error_template = "HTTP/1.1 %s\r\n"
257 "Content-Type: text/html\r\n"
258 "Connection: close\r\n"
259 "Pragma: no-cache\r\n"
260 "Content-Length: 0\r\n\r\n";
263 * http response boilerplate
265 static const char *http_response_template = "HTTP/1.1 200 OK\r\n"
267 "Expires: %U GMT\r\n"
268 "Server: VPP Static\r\n"
269 "Content-Type: %s\r\n"
270 "Content-Length: %lu\r\n\r\n";
273 send_data (http_conn_t *hc, u8 *data, u32 length, u32 offset)
275 const u32 max_burst = 64 << 10;
280 ts = session_get_from_handle (hc->h_tc_session_handle);
282 to_send = clib_min (length - offset, max_burst);
283 sent = svm_fifo_enqueue (ts->tx_fifo, to_send, data + offset);
288 if (svm_fifo_set_event (ts->tx_fifo))
289 session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX);
291 return (offset + sent);
295 send_error (http_conn_t *hc, http_status_code_t ec)
297 http_main_t *hm = &http_main;
301 if (ec >= HTTP_N_STATUS)
302 ec = HTTP_STATUS_INTERNAL_ERROR;
304 now = clib_timebase_now (&hm->timebase);
305 data = format (0, http_error_template, http_status_code_str[ec],
306 format_clib_timebase_time, now);
307 send_data (hc, data, vec_len (data), 0);
312 read_request (http_conn_t *hc)
314 u32 max_deq, cursize;
318 ts = session_get_from_handle (hc->h_tc_session_handle);
320 cursize = vec_len (hc->rx_buf);
321 max_deq = svm_fifo_max_dequeue (ts->rx_fifo);
322 if (PREDICT_FALSE (max_deq == 0))
325 vec_validate (hc->rx_buf, cursize + max_deq - 1);
326 n_read = svm_fifo_dequeue (ts->rx_fifo, max_deq, hc->rx_buf + cursize);
327 ASSERT (n_read == max_deq);
329 if (svm_fifo_is_empty (ts->rx_fifo))
330 svm_fifo_unset_event (ts->rx_fifo);
332 _vec_len (hc->rx_buf) = cursize + n_read;
337 v_find_index (u8 *vec, u32 offset, char *str)
339 int start_index = offset;
340 u32 slen = (u32) strnlen_s_inline (str, 8);
341 u32 vlen = vec_len (vec);
348 for (; start_index < (vlen - slen); start_index++)
350 if (!memcmp (vec + start_index, str, slen))
358 * waiting for request method from peer - parse request method and data
361 state_wait_method (http_conn_t *hc, transport_send_params_t *sp)
363 http_status_code_t ec;
364 app_worker_t *app_wrk;
371 rv = read_request (hc);
373 /* Nothing yet, wait for data or timer expire */
377 if (vec_len (hc->rx_buf) < 8)
379 ec = HTTP_STATUS_BAD_REQUEST;
383 if ((i = v_find_index (hc->rx_buf, 0, "GET ")) >= 0)
385 hc->method = HTTP_REQ_GET;
386 hc->rx_buf_offset = i + 5;
388 i = v_find_index (hc->rx_buf, hc->rx_buf_offset, "HTTP");
391 ec = HTTP_STATUS_BAD_REQUEST;
395 len = i - hc->rx_buf_offset - 1;
397 else if ((i = v_find_index (hc->rx_buf, 0, "POST ")) >= 0)
399 hc->method = HTTP_REQ_POST;
400 hc->rx_buf_offset = i + 6;
401 len = vec_len (hc->rx_buf) - hc->rx_buf_offset - 1;
405 HTTP_DBG (0, "Unknown http method");
406 ec = HTTP_STATUS_METHOD_NOT_ALLOWED;
410 buf = &hc->rx_buf[hc->rx_buf_offset];
412 msg.type = HTTP_MSG_REQUEST;
413 msg.method_type = hc->method;
414 msg.content_type = HTTP_CONTENT_TEXT_HTML;
415 msg.data.type = HTTP_MSG_DATA_INLINE;
418 svm_fifo_seg_t segs[2] = { { (u8 *) &msg, sizeof (msg) }, { buf, len } };
420 as = session_get_from_handle (hc->h_pa_session_handle);
421 rv = svm_fifo_enqueue_segments (as->rx_fifo, segs, 2, 0 /* allow partial */);
422 if (rv < 0 || rv != sizeof (msg) + len)
424 clib_warning ("failed app enqueue");
425 /* This should not happen as we only handle 1 request per session,
426 * and fifo is allocated, but going forward we should consider
431 vec_free (hc->rx_buf);
432 hc->req_state = HTTP_REQ_STATE_WAIT_APP;
434 app_wrk = app_worker_get_if_valid (as->app_wrk_index);
435 app_worker_lock_and_send_event (app_wrk, as, SESSION_IO_EVT_RX);
442 session_transport_closing_notify (&hc->connection);
443 http_disconnect_transport (hc);
449 * waiting for data from app
452 state_wait_app (http_conn_t *hc, transport_send_params_t *sp)
454 http_main_t *hm = &http_main;
455 http_status_code_t ec;
463 as = session_get_from_handle (hc->h_pa_session_handle);
465 rv = svm_fifo_dequeue (as->tx_fifo, sizeof (msg), (u8 *) &msg);
466 ASSERT (rv == sizeof (msg));
468 if (msg.type != HTTP_MSG_REPLY || msg.data.type > HTTP_MSG_DATA_PTR)
470 clib_warning ("unexpected msg type from app %u", msg.type);
471 ec = HTTP_STATUS_INTERNAL_ERROR;
475 if (msg.code != HTTP_STATUS_OK)
481 http_buffer_init (&hc->tx_buf, msg_to_buf_type[msg.data.type], as->tx_fifo,
485 * Add headers. For now:
491 now = clib_timebase_now (&hm->timebase);
492 header = format (0, http_response_template,
494 format_clib_timebase_time, now,
496 format_clib_timebase_time, now + 600.0,
498 http_content_type_str[msg.content_type],
502 offset = send_data (hc, header, vec_len (header), 0);
503 if (offset != vec_len (header))
505 clib_warning ("couldn't send response header!");
506 ec = HTTP_STATUS_INTERNAL_ERROR;
511 /* Start sending the actual data */
512 hc->req_state = HTTP_REQ_STATE_SEND_MORE_DATA;
514 ASSERT (sp->max_burst_size >= offset);
515 sp->max_burst_size -= offset;
522 hc->req_state = HTTP_REQ_STATE_WAIT_METHOD;
523 session_transport_closing_notify (&hc->connection);
524 http_disconnect_transport (hc);
526 /* stop state machine processing */
531 state_send_more_data (http_conn_t *hc, transport_send_params_t *sp)
533 u32 max_send = 64 << 10, n_segs;
534 http_buffer_t *hb = &hc->tx_buf;
539 max_send = clib_min (max_send, sp->max_burst_size);
540 ts = session_get_from_handle (hc->h_tc_session_handle);
541 if ((seg = http_buffer_get_segs (hb, max_send, &n_segs)))
542 sent = svm_fifo_enqueue_segments (ts->tx_fifo, seg, n_segs,
543 1 /* allow partial */);
547 /* Ask scheduler to notify app of deq event if needed */
548 sp->bytes_dequeued += http_buffer_drain (hb, sent);
549 sp->max_burst_size -= sent;
552 /* Not finished sending all data */
553 if (!http_buffer_is_drained (hb))
555 if (sent && svm_fifo_set_event (ts->tx_fifo))
556 session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX);
558 if (svm_fifo_max_enqueue (ts->tx_fifo) < HTTP_FIFO_THRESH)
560 /* Deschedule http session and wait for deq notification if
561 * underlying ts tx fifo almost full */
562 svm_fifo_add_want_deq_ntf (ts->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
563 transport_connection_deschedule (&hc->connection);
564 sp->flags |= TRANSPORT_SND_F_DESCHED;
569 if (sent && svm_fifo_set_event (ts->tx_fifo))
570 session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX_FLUSH);
572 /* Finished transaction, back to HTTP_REQ_STATE_WAIT_METHOD */
573 hc->req_state = HTTP_REQ_STATE_WAIT_METHOD;
574 http_buffer_free (&hc->tx_buf);
580 typedef int (*http_sm_handler) (http_conn_t *, transport_send_params_t *sp);
582 static http_sm_handler req_state_funcs[HTTP_REQ_N_STATES] = {
583 /* Waiting for GET, POST, etc. */
585 /* Wait for data from app */
588 state_send_more_data,
592 http_req_run_state_machine (http_conn_t *hc, transport_send_params_t *sp)
598 rv = req_state_funcs[hc->req_state](hc, sp);
604 /* Reset the session expiration timer */
605 http_conn_timer_update (hc);
609 http_ts_rx_callback (session_t *ts)
613 hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
615 if (hc->req_state != HTTP_REQ_STATE_WAIT_METHOD)
617 clib_warning ("tcp data in req state %u", hc->req_state);
621 http_req_run_state_machine (hc, 0);
623 if (hc->state == HTTP_CONN_STATE_TRANSPORT_CLOSED)
625 if (!svm_fifo_max_dequeue_cons (ts->rx_fifo))
626 session_transport_closing_notify (&hc->connection);
632 http_ts_builtin_tx_callback (session_t *ts)
636 hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
637 transport_connection_reschedule (&hc->connection);
643 http_ts_cleanup_callback (session_t *ts, session_cleanup_ntf_t ntf)
647 if (ntf == SESSION_CLEANUP_TRANSPORT)
650 hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
653 clib_warning ("no http connection for %u", ts->session_index);
657 vec_free (hc->rx_buf);
659 http_buffer_free (&hc->tx_buf);
660 http_conn_timer_stop (hc);
662 session_transport_delete_notify (&hc->connection);
667 http_add_segment_callback (u32 client_index, u64 segment_handle)
669 /* No-op for builtin */
674 http_del_segment_callback (u32 client_index, u64 segment_handle)
679 static session_cb_vft_t http_app_cb_vft = {
680 .session_accept_callback = http_ts_accept_callback,
681 .session_disconnect_callback = http_ts_disconnect_callback,
682 .session_connected_callback = http_ts_connected_callback,
683 .session_reset_callback = http_ts_reset_callback,
684 .session_cleanup_callback = http_ts_cleanup_callback,
685 .add_segment_callback = http_add_segment_callback,
686 .del_segment_callback = http_del_segment_callback,
687 .builtin_app_rx_callback = http_ts_rx_callback,
688 .builtin_app_tx_callback = http_ts_builtin_tx_callback,
691 static clib_error_t *
692 http_transport_enable (vlib_main_t *vm, u8 is_en)
694 vnet_app_detach_args_t _da, *da = &_da;
695 vnet_app_attach_args_t _a, *a = &_a;
696 u64 options[APP_OPTIONS_N_OPTIONS];
697 http_main_t *hm = &http_main;
701 da->app_index = hm->app_index;
702 da->api_client_index = APP_INVALID_INDEX;
703 vnet_application_detach (da);
707 vec_validate (hm->wrk, vlib_num_workers ());
709 clib_memset (a, 0, sizeof (*a));
710 clib_memset (options, 0, sizeof (options));
712 a->session_cb_vft = &http_app_cb_vft;
713 a->api_client_index = APP_INVALID_INDEX;
714 a->options = options;
715 a->name = format (0, "http");
716 a->options[APP_OPTIONS_SEGMENT_SIZE] = hm->first_seg_size;
717 a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = hm->add_seg_size;
718 a->options[APP_OPTIONS_RX_FIFO_SIZE] = hm->fifo_size;
719 a->options[APP_OPTIONS_TX_FIFO_SIZE] = hm->fifo_size;
720 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
721 a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
722 a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_IS_TRANSPORT_APP;
724 if (vnet_application_attach (a))
725 return clib_error_return (0, "failed to attach http app");
727 hm->app_index = a->app_index;
730 clib_timebase_init (&hm->timebase, 0 /* GMT */, CLIB_TIMEBASE_DAYLIGHT_NONE,
731 &vm->clib_time /* share the system clock */);
733 http_timers_init (vm, http_conn_timeout_cb);
739 http_transport_connect (transport_endpoint_cfg_t *tep)
745 http_start_listen (u32 app_listener_index, transport_endpoint_cfg_t *tep)
747 vnet_listen_args_t _args = {}, *args = &_args;
748 session_t *ts_listener, *app_listener;
749 http_main_t *hm = &http_main;
750 session_endpoint_cfg_t *sep;
751 app_worker_t *app_wrk;
752 transport_proto_t tp;
758 sep = (session_endpoint_cfg_t *) tep;
760 app_wrk = app_worker_get (sep->app_wrk_index);
761 app = application_get (app_wrk->app_index);
763 args->app_index = hm->app_index;
764 args->sep_ext = *sep;
765 args->sep_ext.ns_index = app->ns_index;
766 tp = sep->ext_cfg ? TRANSPORT_PROTO_TLS : TRANSPORT_PROTO_TCP;
767 args->sep_ext.transport_proto = tp;
769 if (vnet_listen (args))
770 return SESSION_INVALID_INDEX;
772 lhc_index = http_listener_alloc ();
773 lhc = http_listener_get (lhc_index);
775 /* Grab transport connection listener and link to http listener */
776 lhc->h_tc_session_handle = args->handle;
777 al = app_listener_get_w_handle (lhc->h_tc_session_handle);
778 ts_listener = app_listener_get_session (al);
779 ts_listener->opaque = lhc_index;
781 /* Grab application listener and link to http listener */
782 app_listener = listen_session_get (app_listener_index);
783 lhc->h_pa_wrk_index = sep->app_wrk_index;
784 lhc->h_pa_session_handle = listen_session_get_handle (app_listener);
785 lhc->c_s_index = app_listener_index;
786 lhc->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
792 http_stop_listen (u32 listener_index)
797 lhc = http_listener_get (listener_index);
799 vnet_unlisten_args_t a = {
800 .handle = lhc->h_tc_session_handle,
801 .app_index = http_main.app_index,
802 .wrk_map_index = 0 /* default wrk */
805 if ((rv = vnet_unlisten (&a)))
806 clib_warning ("unlisten returned %d", rv);
808 http_listener_free (lhc);
814 http_transport_close (u32 hc_index, u32 thread_index)
819 HTTP_DBG (1, "App disconnecting %x", hc_index);
821 hc = http_conn_get_w_thread (hc_index, thread_index);
822 as = session_get_from_handle (hc->h_pa_session_handle);
824 /* Nothing more to send, confirm close */
825 if (!svm_fifo_max_dequeue_cons (as->tx_fifo))
827 session_transport_closed_notify (&hc->connection);
828 http_disconnect_transport (hc);
832 /* Wait for all data to be written to ts */
833 hc->state = HTTP_CONN_STATE_APP_CLOSED;
837 static transport_connection_t *
838 http_transport_get_connection (u32 hc_index, u32 thread_index)
840 http_conn_t *hc = http_conn_get_w_thread (hc_index, thread_index);
841 return &hc->connection;
844 static transport_connection_t *
845 http_transport_get_listener (u32 listener_index)
847 http_conn_t *lhc = http_listener_get (listener_index);
848 return &lhc->connection;
852 http_app_tx_callback (void *session, transport_send_params_t *sp)
854 session_t *as = (session_t *) session;
855 u32 max_burst_sz, sent;
858 hc = http_conn_get_w_thread (as->connection_index, as->thread_index);
859 if (hc->req_state < HTTP_REQ_STATE_WAIT_APP)
861 if (hc->state != HTTP_CONN_STATE_CLOSED)
862 clib_warning ("app data req state %u session state %u", hc->req_state,
864 svm_fifo_dequeue_drop_all (as->tx_fifo);
868 max_burst_sz = sp->max_burst_size * TRANSPORT_PACER_MIN_MSS;
869 sp->max_burst_size = max_burst_sz;
871 http_req_run_state_machine (hc, sp);
873 if (hc->state == HTTP_CONN_STATE_APP_CLOSED)
875 if (!svm_fifo_max_dequeue_cons (as->tx_fifo))
876 http_disconnect_transport (hc);
879 sent = max_burst_sz - sp->max_burst_size;
881 return sent > 0 ? clib_max (sent / TRANSPORT_PACER_MIN_MSS, 1) : 0;
885 http_transport_get_endpoint (u32 hc_index, u32 thread_index,
886 transport_endpoint_t *tep, u8 is_lcl)
888 http_conn_t *hc = http_conn_get_w_thread (hc_index, thread_index);
891 ts = session_get_from_handle (hc->h_tc_session_handle);
892 session_get_endpoint (ts, tep, is_lcl);
896 format_http_connection (u8 *s, va_list *args)
898 http_conn_t *hc = va_arg (*args, http_conn_t *);
901 ts = session_get_from_handle (hc->h_tc_session_handle);
902 s = format (s, "[%d:%d][H] app_wrk %u ts %d:%d", hc->c_thread_index,
903 hc->c_s_index, hc->h_pa_wrk_index, ts->thread_index,
910 format_http_listener (u8 *s, va_list *args)
912 http_conn_t *lhc = va_arg (*args, http_conn_t *);
916 al = app_listener_get_w_handle (lhc->h_tc_session_handle);
917 lts = app_listener_get_session (al);
918 s = format (s, "[%d:%d][H] app_wrk %u ts %d:%d", lhc->c_thread_index,
919 lhc->c_s_index, lhc->h_pa_wrk_index, lts->thread_index,
926 format_http_conn_state (u8 *s, va_list *args)
928 http_conn_t *hc = va_arg (*args, http_conn_t *);
932 case HTTP_CONN_STATE_LISTEN:
933 s = format (s, "LISTEN");
935 case HTTP_CONN_STATE_CONNECTING:
936 s = format (s, "CONNECTING");
938 case HTTP_CONN_STATE_ESTABLISHED:
939 s = format (s, "ESTABLISHED");
941 case HTTP_CONN_STATE_TRANSPORT_CLOSED:
942 s = format (s, "TRANSPORT_CLOSED");
944 case HTTP_CONN_STATE_APP_CLOSED:
945 s = format (s, "APP_CLOSED");
947 case HTTP_CONN_STATE_CLOSED:
948 s = format (s, "CLOSED");
956 format_http_transport_connection (u8 *s, va_list *args)
958 u32 tc_index = va_arg (*args, u32);
959 u32 thread_index = va_arg (*args, u32);
960 u32 verbose = va_arg (*args, u32);
963 hc = http_conn_get_w_thread (tc_index, thread_index);
965 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_http_connection, hc);
969 format (s, "%-" SESSION_CLI_STATE_LEN "U", format_http_conn_state, hc);
971 s = format (s, "\n");
978 format_http_transport_listener (u8 *s, va_list *args)
980 u32 tc_index = va_arg (*args, u32);
981 u32 __clib_unused thread_index = va_arg (*args, u32);
982 u32 __clib_unused verbose = va_arg (*args, u32);
983 http_conn_t *lhc = http_listener_get (tc_index);
985 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_http_listener, lhc);
988 format (s, "%-" SESSION_CLI_STATE_LEN "U", format_http_conn_state, lhc);
992 static const transport_proto_vft_t http_proto = {
993 .enable = http_transport_enable,
994 .connect = http_transport_connect,
995 .start_listen = http_start_listen,
996 .stop_listen = http_stop_listen,
997 .close = http_transport_close,
998 .custom_tx = http_app_tx_callback,
999 .get_connection = http_transport_get_connection,
1000 .get_listener = http_transport_get_listener,
1001 .get_transport_endpoint = http_transport_get_endpoint,
1002 .format_connection = format_http_transport_connection,
1003 .format_listener = format_http_transport_listener,
1004 .transport_options = {
1007 .tx_type = TRANSPORT_TX_INTERNAL,
1008 .service_type = TRANSPORT_SERVICE_APP,
1012 static clib_error_t *
1013 http_transport_init (vlib_main_t *vm)
1015 http_main_t *hm = &http_main;
1017 transport_register_protocol (TRANSPORT_PROTO_HTTP, &http_proto,
1018 FIB_PROTOCOL_IP4, ~0);
1019 transport_register_protocol (TRANSPORT_PROTO_HTTP, &http_proto,
1020 FIB_PROTOCOL_IP6, ~0);
1022 /* Default values, configurable via startup conf */
1023 hm->add_seg_size = 256 << 20;
1024 hm->first_seg_size = 32 << 20;
1025 hm->fifo_size = 512 << 10;
1030 VLIB_INIT_FUNCTION (http_transport_init);
1032 static clib_error_t *
1033 http_config_fn (vlib_main_t *vm, unformat_input_t *input)
1035 http_main_t *hm = &http_main;
1038 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1040 if (unformat (input, "first-segment-size %U", unformat_memory_size,
1043 hm->first_seg_size = clib_max (mem_sz, 1 << 20);
1044 if (hm->first_seg_size != mem_sz)
1045 clib_warning ("first seg size too small %u", mem_sz);
1047 else if (unformat (input, "add-segment-size %U", unformat_memory_size,
1050 hm->add_seg_size = clib_max (mem_sz, 1 << 20);
1051 if (hm->add_seg_size != mem_sz)
1052 clib_warning ("add seg size too small %u", mem_sz);
1054 else if (unformat (input, "fifo-size %U", unformat_memory_size, &mem_sz))
1056 hm->fifo_size = clib_clamp (mem_sz, 4 << 10, 2 << 30);
1057 if (hm->fifo_size != mem_sz)
1058 clib_warning ("invalid fifo size %lu", mem_sz);
1061 return clib_error_return (0, "unknown input `%U'",
1062 format_unformat_error, input);
1067 VLIB_EARLY_CONFIG_FUNCTION (http_config_fn, "http");
1069 VLIB_PLUGIN_REGISTER () = {
1070 .version = VPP_BUILD_VER,
1071 .description = "Hypertext Transfer Protocol (HTTP)",
1072 .default_disabled = 0,
1076 * fd.io coding-style-patch-verification: ON
1079 * eval: (c-set-style "gnu")