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_ctx_pool, ctx);
82 ctx->c_c_index = ctx - hm->listener_ctx_pool;
83 return ctx->c_c_index;
87 http_listener_get (u32 ctx_index)
89 return pool_elt_at_index (http_main.listener_ctx_pool, ctx_index);
93 http_disconnect_transport (http_conn_t *hc)
95 vnet_disconnect_args_t a = {
96 .handle = hc->h_tc_session_handle,
97 .app_index = http_main.app_index,
100 hc->state = HTTP_CONN_STATE_CLOSED;
102 if (vnet_disconnect_session (&a))
103 clib_warning ("disconnect returned");
107 http_conn_timeout_cb (void *hc_handlep)
112 hs_handle = pointer_to_uword (hc_handlep);
113 hc = http_conn_get_w_thread (hs_handle & 0x00FFFFFF, hs_handle >> 24);
115 HTTP_DBG (1, "terminate thread %d index %d hs %llx", hs_handle >> 24,
116 hs_handle & 0x00FFFFFF, hc);
120 hc->timer_handle = ~0;
121 session_transport_closing_notify (&hc->connection);
122 http_disconnect_transport (hc);
126 http_ts_accept_callback (session_t *ts)
128 session_t *ts_listener, *as, *asl;
129 app_worker_t *app_wrk;
130 http_conn_t *lhc, *hc;
131 u32 hc_index, thresh;
134 ts_listener = listen_session_get_from_handle (ts->listener_handle);
135 lhc = http_listener_get (ts_listener->opaque);
137 hc_index = http_conn_alloc_w_thread (ts->thread_index);
138 hc = http_conn_get_w_thread (hc_index, ts->thread_index);
139 clib_memcpy_fast (hc, lhc, sizeof (*lhc));
140 hc->c_thread_index = vlib_get_thread_index ();
141 hc->h_hc_index = hc_index;
143 hc->h_tc_session_handle = session_handle (ts);
144 hc->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
146 hc->state = HTTP_CONN_STATE_ESTABLISHED;
147 hc->req_state = HTTP_REQ_STATE_WAIT_METHOD;
149 ts->session_state = SESSION_STATE_READY;
150 ts->opaque = hc_index;
153 * Alloc session and initialize
155 as = session_alloc (hc->c_thread_index);
156 as->session_state = SESSION_STATE_CREATED;
157 hc->c_s_index = as->session_index;
159 as->app_wrk_index = hc->h_pa_wrk_index;
160 as->connection_index = hc->c_c_index;
161 as->session_state = SESSION_STATE_ACCEPTING;
163 asl = listen_session_get_from_handle (lhc->h_pa_session_handle);
164 as->session_type = asl->session_type;
165 as->listener_handle = lhc->h_pa_session_handle;
168 * Init session fifos and notify app
170 if ((rv = app_worker_init_accepted (as)))
172 HTTP_DBG (1, "failed to allocate fifos");
177 hc->h_pa_session_handle = session_handle (as);
178 hc->h_pa_wrk_index = as->app_wrk_index;
179 app_wrk = app_worker_get (as->app_wrk_index);
181 HTTP_DBG (1, "Accepted on listener %u new connection [%u]%x",
182 ts_listener->opaque, vlib_get_thread_index (), hc_index);
184 if ((rv = app_worker_accept_notify (app_wrk, as)))
186 HTTP_DBG (0, "app accept returned");
191 /* Avoid enqueuing small chunks of data on transport tx notifications. If
192 * the fifo is small (under 16K) we set the threshold to it's size, meaning
193 * a notification will be given when the fifo empties.
195 ts = session_get_from_handle (hc->h_tc_session_handle);
196 thresh = clib_min (svm_fifo_size (ts->tx_fifo), HTTP_FIFO_THRESH);
197 svm_fifo_set_deq_thresh (ts->tx_fifo, thresh);
199 http_conn_timer_start (hc);
205 http_ts_connected_callback (u32 http_app_index, u32 hc_index, session_t *ts,
208 clib_warning ("not supported");
213 http_ts_disconnect_callback (session_t *ts)
217 hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
219 if (hc->state < HTTP_CONN_STATE_TRANSPORT_CLOSED)
220 hc->state = HTTP_CONN_STATE_TRANSPORT_CLOSED;
222 if (!svm_fifo_max_dequeue_cons (ts->rx_fifo))
223 session_transport_closing_notify (&hc->connection);
227 http_ts_reset_callback (session_t *ts)
231 ctx = http_conn_get_w_thread (ts->opaque, ts->thread_index);
233 if (ctx->state < HTTP_CONN_STATE_TRANSPORT_CLOSED)
234 ctx->state = HTTP_CONN_STATE_TRANSPORT_CLOSED;
236 if (!svm_fifo_max_dequeue_cons (ts->rx_fifo))
237 session_transport_reset_notify (&ctx->connection);
241 * http error boilerplate
243 static const char *http_error_template = "HTTP/1.1 %s\r\n"
245 "Content-Type: text/html\r\n"
246 "Connection: close\r\n"
247 "Pragma: no-cache\r\n"
248 "Content-Length: 0\r\n\r\n";
251 * http response boilerplate
253 static const char *http_response_template = "HTTP/1.1 200 OK\r\n"
255 "Expires: %U GMT\r\n"
256 "Server: VPP Static\r\n"
257 "Content-Type: %s\r\n"
258 "Content-Length: %lu\r\n\r\n";
261 send_data (http_conn_t *hc, u8 *data, u32 length, u32 offset)
263 const u32 max_burst = 64 << 10;
268 ts = session_get_from_handle (hc->h_tc_session_handle);
270 to_send = clib_min (length - offset, max_burst);
271 sent = svm_fifo_enqueue (ts->tx_fifo, to_send, data + offset);
276 if (svm_fifo_set_event (ts->tx_fifo))
277 session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX);
279 return (offset + sent);
283 send_error (http_conn_t *hc, http_status_code_t ec)
285 http_main_t *hm = &http_main;
289 if (ec >= HTTP_N_STATUS)
290 ec = HTTP_STATUS_INTERNAL_ERROR;
292 now = clib_timebase_now (&hm->timebase);
293 data = format (0, http_error_template, http_status_code_str[ec],
294 format_clib_timebase_time, now);
295 send_data (hc, data, vec_len (data), 0);
300 read_request (http_conn_t *hc)
302 u32 max_deq, cursize;
306 ts = session_get_from_handle (hc->h_tc_session_handle);
308 cursize = vec_len (hc->rx_buf);
309 max_deq = svm_fifo_max_dequeue (ts->rx_fifo);
310 if (PREDICT_FALSE (max_deq == 0))
313 vec_validate (hc->rx_buf, cursize + max_deq - 1);
314 n_read = svm_fifo_dequeue (ts->rx_fifo, max_deq, hc->rx_buf + cursize);
315 ASSERT (n_read == max_deq);
317 if (svm_fifo_is_empty (ts->rx_fifo))
318 svm_fifo_unset_event (ts->rx_fifo);
320 _vec_len (hc->rx_buf) = cursize + n_read;
325 v_find_index (u8 *vec, u32 offset, char *str)
327 int start_index = offset;
328 u32 slen = (u32) strnlen_s_inline (str, 8);
329 u32 vlen = vec_len (vec);
336 for (; start_index < (vlen - slen); start_index++)
338 if (!memcmp (vec + start_index, str, slen))
346 * waiting for request method from peer - parse request method and data
349 state_wait_method (http_conn_t *hc, transport_send_params_t *sp)
351 http_status_code_t ec;
352 app_worker_t *app_wrk;
359 rv = read_request (hc);
361 /* Nothing yet, wait for data or timer expire */
365 if (vec_len (hc->rx_buf) < 8)
367 ec = HTTP_STATUS_BAD_REQUEST;
371 if ((i = v_find_index (hc->rx_buf, 0, "GET ")) >= 0)
373 hc->method = HTTP_REQ_GET;
374 hc->rx_buf_offset = i + 5;
376 i = v_find_index (hc->rx_buf, hc->rx_buf_offset, "HTTP");
379 ec = HTTP_STATUS_BAD_REQUEST;
383 len = i - hc->rx_buf_offset - 1;
385 else if ((i = v_find_index (hc->rx_buf, 0, "POST ")) >= 0)
387 hc->method = HTTP_REQ_POST;
388 hc->rx_buf_offset = i + 6;
389 len = vec_len (hc->rx_buf) - hc->rx_buf_offset - 1;
393 HTTP_DBG (0, "Unknown http method");
394 ec = HTTP_STATUS_METHOD_NOT_ALLOWED;
398 buf = &hc->rx_buf[hc->rx_buf_offset];
400 msg.type = HTTP_MSG_REQUEST;
401 msg.method_type = hc->method;
402 msg.content_type = HTTP_CONTENT_TEXT_HTML;
403 msg.data.type = HTTP_MSG_DATA_INLINE;
406 svm_fifo_seg_t segs[2] = { { (u8 *) &msg, sizeof (msg) }, { buf, len } };
408 as = session_get_from_handle (hc->h_pa_session_handle);
409 rv = svm_fifo_enqueue_segments (as->rx_fifo, segs, 2, 0 /* allow partial */);
410 if (rv < 0 || rv != sizeof (msg) + len)
412 clib_warning ("failed app enqueue");
413 /* This should not happen as we only handle 1 request per session,
414 * and fifo is allocated, but going forward we should consider
419 vec_free (hc->rx_buf);
420 hc->req_state = HTTP_REQ_STATE_WAIT_APP;
422 app_wrk = app_worker_get_if_valid (as->app_wrk_index);
423 app_worker_lock_and_send_event (app_wrk, as, SESSION_IO_EVT_RX);
430 session_transport_closing_notify (&hc->connection);
431 http_disconnect_transport (hc);
437 * waiting for data from app
440 state_wait_app (http_conn_t *hc, transport_send_params_t *sp)
442 http_main_t *hm = &http_main;
443 http_status_code_t ec;
451 as = session_get_from_handle (hc->h_pa_session_handle);
453 rv = svm_fifo_dequeue (as->tx_fifo, sizeof (msg), (u8 *) &msg);
454 ASSERT (rv == sizeof (msg));
456 if (msg.type != HTTP_MSG_REPLY || msg.data.type > HTTP_MSG_DATA_PTR)
458 clib_warning ("unexpected msg type from app %u", msg.type);
459 ec = HTTP_STATUS_INTERNAL_ERROR;
463 if (msg.code != HTTP_STATUS_OK)
469 http_buffer_init (&hc->tx_buf, msg_to_buf_type[msg.data.type], as->tx_fifo,
473 * Add headers. For now:
479 now = clib_timebase_now (&hm->timebase);
480 header = format (0, http_response_template,
482 format_clib_timebase_time, now,
484 format_clib_timebase_time, now + 600.0,
486 http_content_type_str[msg.content_type],
490 offset = send_data (hc, header, vec_len (header), 0);
491 if (offset != vec_len (header))
493 clib_warning ("couldn't send response header!");
494 ec = HTTP_STATUS_INTERNAL_ERROR;
499 /* Start sending the actual data */
500 hc->req_state = HTTP_REQ_STATE_SEND_MORE_DATA;
502 ASSERT (sp->max_burst_size >= offset);
503 sp->max_burst_size -= offset;
510 hc->req_state = HTTP_REQ_STATE_WAIT_METHOD;
511 session_transport_closing_notify (&hc->connection);
512 http_disconnect_transport (hc);
514 /* stop state machine processing */
519 state_send_more_data (http_conn_t *hc, transport_send_params_t *sp)
521 u32 max_send = 64 << 10, n_segs;
522 http_buffer_t *hb = &hc->tx_buf;
527 max_send = clib_min (max_send, sp->max_burst_size);
528 ts = session_get_from_handle (hc->h_tc_session_handle);
529 if ((seg = http_buffer_get_segs (hb, max_send, &n_segs)))
530 sent = svm_fifo_enqueue_segments (ts->tx_fifo, seg, n_segs,
531 1 /* allow partial */);
535 /* Ask scheduler to notify app of deq event if needed */
536 sp->bytes_dequeued += http_buffer_drain (hb, sent);
537 sp->max_burst_size -= sent;
540 /* Not finished sending all data */
541 if (!http_buffer_is_drained (hb))
543 if (sent && svm_fifo_set_event (ts->tx_fifo))
544 session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX);
546 if (svm_fifo_max_enqueue (ts->tx_fifo) < HTTP_FIFO_THRESH)
548 /* Deschedule http session and wait for deq notification if
549 * underlying ts tx fifo almost full */
550 svm_fifo_add_want_deq_ntf (ts->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
551 transport_connection_deschedule (&hc->connection);
552 sp->flags |= TRANSPORT_SND_F_DESCHED;
557 if (sent && svm_fifo_set_event (ts->tx_fifo))
558 session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX_FLUSH);
560 /* Finished transaction, back to HTTP_REQ_STATE_WAIT_METHOD */
561 hc->req_state = HTTP_REQ_STATE_WAIT_METHOD;
562 http_buffer_free (&hc->tx_buf);
568 typedef int (*http_sm_handler) (http_conn_t *, transport_send_params_t *sp);
570 static http_sm_handler req_state_funcs[HTTP_REQ_N_STATES] = {
571 /* Waiting for GET, POST, etc. */
573 /* Wait for data from app */
576 state_send_more_data,
580 http_req_run_state_machine (http_conn_t *hc, transport_send_params_t *sp)
586 rv = req_state_funcs[hc->req_state](hc, sp);
592 /* Reset the session expiration timer */
593 http_conn_timer_update (hc);
597 http_ts_rx_callback (session_t *ts)
601 hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
603 if (hc->req_state != HTTP_REQ_STATE_WAIT_METHOD)
605 clib_warning ("tcp data in req state %u", hc->req_state);
609 http_req_run_state_machine (hc, 0);
611 if (hc->state == HTTP_CONN_STATE_TRANSPORT_CLOSED)
613 if (!svm_fifo_max_dequeue_cons (ts->rx_fifo))
614 session_transport_closing_notify (&hc->connection);
620 http_ts_builtin_tx_callback (session_t *ts)
624 hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
625 transport_connection_reschedule (&hc->connection);
631 http_ts_cleanup_callback (session_t *ts, session_cleanup_ntf_t ntf)
635 if (ntf == SESSION_CLEANUP_TRANSPORT)
638 hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
641 clib_warning ("no http connection for %u", ts->session_index);
645 vec_free (hc->rx_buf);
647 http_buffer_free (&hc->tx_buf);
648 http_conn_timer_stop (hc);
650 session_transport_delete_notify (&hc->connection);
655 http_add_segment_callback (u32 client_index, u64 segment_handle)
657 /* No-op for builtin */
662 http_del_segment_callback (u32 client_index, u64 segment_handle)
667 static session_cb_vft_t http_app_cb_vft = {
668 .session_accept_callback = http_ts_accept_callback,
669 .session_disconnect_callback = http_ts_disconnect_callback,
670 .session_connected_callback = http_ts_connected_callback,
671 .session_reset_callback = http_ts_reset_callback,
672 .session_cleanup_callback = http_ts_cleanup_callback,
673 .add_segment_callback = http_add_segment_callback,
674 .del_segment_callback = http_del_segment_callback,
675 .builtin_app_rx_callback = http_ts_rx_callback,
676 .builtin_app_tx_callback = http_ts_builtin_tx_callback,
679 static clib_error_t *
680 http_transport_enable (vlib_main_t *vm, u8 is_en)
682 vnet_app_detach_args_t _da, *da = &_da;
683 vnet_app_attach_args_t _a, *a = &_a;
684 u64 options[APP_OPTIONS_N_OPTIONS];
685 http_main_t *hm = &http_main;
689 da->app_index = hm->app_index;
690 da->api_client_index = APP_INVALID_INDEX;
691 vnet_application_detach (da);
695 vec_validate (hm->wrk, vlib_num_workers ());
697 clib_memset (a, 0, sizeof (*a));
698 clib_memset (options, 0, sizeof (options));
700 a->session_cb_vft = &http_app_cb_vft;
701 a->api_client_index = APP_INVALID_INDEX;
702 a->options = options;
703 a->name = format (0, "http");
704 a->options[APP_OPTIONS_SEGMENT_SIZE] = hm->first_seg_size;
705 a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = hm->add_seg_size;
706 a->options[APP_OPTIONS_RX_FIFO_SIZE] = hm->fifo_size;
707 a->options[APP_OPTIONS_TX_FIFO_SIZE] = hm->fifo_size;
708 a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
709 a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
710 a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_IS_TRANSPORT_APP;
712 if (vnet_application_attach (a))
713 return clib_error_return (0, "failed to attach http app");
715 hm->app_index = a->app_index;
718 clib_timebase_init (&hm->timebase, 0 /* GMT */, CLIB_TIMEBASE_DAYLIGHT_NONE,
719 &vm->clib_time /* share the system clock */);
721 http_timers_init (vm, http_conn_timeout_cb);
727 http_transport_connect (transport_endpoint_cfg_t *tep)
733 http_start_listen (u32 app_listener_index, transport_endpoint_cfg_t *tep)
735 vnet_listen_args_t _args = {}, *args = &_args;
736 session_t *ts_listener, *app_listener;
737 http_main_t *hm = &http_main;
738 session_endpoint_cfg_t *sep;
739 app_worker_t *app_wrk;
740 transport_proto_t tp;
746 sep = (session_endpoint_cfg_t *) tep;
748 app_wrk = app_worker_get (sep->app_wrk_index);
749 app = application_get (app_wrk->app_index);
751 args->app_index = hm->app_index;
752 args->sep_ext = *sep;
753 args->sep_ext.ns_index = app->ns_index;
754 tp = sep->ext_cfg ? TRANSPORT_PROTO_TLS : TRANSPORT_PROTO_TCP;
755 args->sep_ext.transport_proto = tp;
757 if (vnet_listen (args))
758 return SESSION_INVALID_INDEX;
760 lhc_index = http_listener_alloc ();
761 lhc = http_listener_get (lhc_index);
763 /* Grab transport connection listener and link to http listener */
764 lhc->h_tc_session_handle = args->handle;
765 al = app_listener_get_w_handle (lhc->h_tc_session_handle);
766 ts_listener = app_listener_get_session (al);
767 ts_listener->opaque = lhc_index;
769 /* Grab application listener and link to http listener */
770 app_listener = listen_session_get (app_listener_index);
771 lhc->h_pa_wrk_index = sep->app_wrk_index;
772 lhc->h_pa_session_handle = listen_session_get_handle (app_listener);
773 lhc->c_s_index = app_listener_index;
774 lhc->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
780 http_transport_close (u32 hc_index, u32 thread_index)
785 HTTP_DBG (1, "App disconnecting %x", hc_index);
787 hc = http_conn_get_w_thread (hc_index, thread_index);
788 as = session_get_from_handle (hc->h_pa_session_handle);
790 /* Nothing more to send, confirm close */
791 if (!svm_fifo_max_dequeue_cons (as->tx_fifo))
793 session_transport_closed_notify (&hc->connection);
794 http_disconnect_transport (hc);
798 /* Wait for all data to be written to ts */
799 hc->state = HTTP_CONN_STATE_APP_CLOSED;
803 static transport_connection_t *
804 http_transport_get_connection (u32 hc_index, u32 thread_index)
806 http_conn_t *hc = http_conn_get_w_thread (hc_index, thread_index);
807 return &hc->connection;
810 static transport_connection_t *
811 http_transport_get_listener (u32 listener_index)
813 http_conn_t *lhc = http_listener_get (listener_index);
814 return &lhc->connection;
818 http_app_tx_callback (void *session, transport_send_params_t *sp)
820 session_t *as = (session_t *) session;
821 u32 max_burst_sz, sent;
824 hc = http_conn_get_w_thread (as->connection_index, as->thread_index);
825 if (hc->req_state < HTTP_REQ_STATE_WAIT_APP)
827 clib_warning ("app data in req state %u", hc->req_state);
831 max_burst_sz = sp->max_burst_size * TRANSPORT_PACER_MIN_MSS;
832 sp->max_burst_size = max_burst_sz;
834 http_req_run_state_machine (hc, sp);
836 if (hc->state == HTTP_CONN_STATE_CLOSED)
838 if (!svm_fifo_max_dequeue_cons (as->rx_fifo))
839 http_disconnect_transport (hc);
842 sent = max_burst_sz - sp->max_burst_size;
844 return sent > 0 ? clib_max (sent / TRANSPORT_PACER_MIN_MSS, 1) : 0;
848 http_transport_get_endpoint (u32 hc_index, u32 thread_index,
849 transport_endpoint_t *tep, u8 is_lcl)
851 http_conn_t *hc = http_conn_get_w_thread (hc_index, thread_index);
854 ts = session_get_from_handle (hc->h_tc_session_handle);
855 session_get_endpoint (ts, tep, is_lcl);
859 format_http_connection (u8 *s, va_list *args)
861 http_conn_t *hc = va_arg (*args, http_conn_t *);
864 ts = session_get_from_handle (hc->h_tc_session_handle);
865 s = format (s, "[%d:%d][H] app_wrk %u ts %d:%d", hc->c_thread_index,
866 hc->c_s_index, hc->h_pa_wrk_index, ts->thread_index,
873 format_http_listener (u8 *s, va_list *args)
875 http_conn_t *lhc = va_arg (*args, http_conn_t *);
879 al = app_listener_get_w_handle (lhc->h_tc_session_handle);
880 lts = app_listener_get_session (al);
881 s = format (s, "[%d:%d][H] app_wrk %u ts %d:%d", lhc->c_thread_index,
882 lhc->c_s_index, lhc->h_pa_wrk_index, lts->thread_index,
889 format_http_conn_state (u8 *s, va_list *args)
891 http_conn_t *hc = va_arg (*args, http_conn_t *);
895 case HTTP_CONN_STATE_LISTEN:
896 s = format (s, "LISTEN");
898 case HTTP_CONN_STATE_CONNECTING:
899 s = format (s, "CONNECTING");
901 case HTTP_CONN_STATE_ESTABLISHED:
902 s = format (s, "ESTABLISHED");
904 case HTTP_CONN_STATE_TRANSPORT_CLOSED:
905 s = format (s, "TRANSPORT_CLOSED");
907 case HTTP_CONN_STATE_APP_CLOSED:
908 s = format (s, "APP_CLOSED");
910 case HTTP_CONN_STATE_CLOSED:
911 s = format (s, "CLOSED");
919 format_http_transport_connection (u8 *s, va_list *args)
921 u32 tc_index = va_arg (*args, u32);
922 u32 thread_index = va_arg (*args, u32);
923 u32 verbose = va_arg (*args, u32);
926 hc = http_conn_get_w_thread (tc_index, thread_index);
928 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_http_connection, hc);
932 format (s, "%-" SESSION_CLI_STATE_LEN "U", format_http_conn_state, hc);
934 s = format (s, "\n");
941 format_http_transport_listener (u8 *s, va_list *args)
943 u32 tc_index = va_arg (*args, u32);
944 u32 __clib_unused thread_index = va_arg (*args, u32);
945 u32 __clib_unused verbose = va_arg (*args, u32);
946 http_conn_t *lhc = http_listener_get (tc_index);
948 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_http_listener, lhc);
951 format (s, "%-" SESSION_CLI_STATE_LEN "U", format_http_conn_state, lhc);
955 static const transport_proto_vft_t http_proto = {
956 .enable = http_transport_enable,
957 .connect = http_transport_connect,
958 .start_listen = http_start_listen,
959 .close = http_transport_close,
960 .custom_tx = http_app_tx_callback,
961 .get_connection = http_transport_get_connection,
962 .get_listener = http_transport_get_listener,
963 .get_transport_endpoint = http_transport_get_endpoint,
964 .format_connection = format_http_transport_connection,
965 .format_listener = format_http_transport_listener,
966 .transport_options = {
969 .tx_type = TRANSPORT_TX_INTERNAL,
970 .service_type = TRANSPORT_SERVICE_APP,
974 static clib_error_t *
975 http_transport_init (vlib_main_t *vm)
977 http_main_t *hm = &http_main;
979 transport_register_protocol (TRANSPORT_PROTO_HTTP, &http_proto,
980 FIB_PROTOCOL_IP4, ~0);
981 transport_register_protocol (TRANSPORT_PROTO_HTTP, &http_proto,
982 FIB_PROTOCOL_IP6, ~0);
984 /* Default values, configurable via startup conf */
985 hm->add_seg_size = 256 << 20;
986 hm->first_seg_size = 32 << 20;
987 hm->fifo_size = 512 << 10;
992 VLIB_INIT_FUNCTION (http_transport_init);
994 static clib_error_t *
995 http_config_fn (vlib_main_t *vm, unformat_input_t *input)
997 http_main_t *hm = &http_main;
1000 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1002 if (unformat (input, "first-segment-size %U", unformat_memory_size,
1005 hm->first_seg_size = clib_max (mem_sz, 1 << 20);
1006 if (hm->first_seg_size != mem_sz)
1007 clib_warning ("first seg size too small %u", mem_sz);
1009 else if (unformat (input, "add-segment-size %U", unformat_memory_size,
1012 hm->add_seg_size = clib_max (mem_sz, 1 << 20);
1013 if (hm->add_seg_size != mem_sz)
1014 clib_warning ("add seg size too small %u", mem_sz);
1016 else if (unformat (input, "fifo-size %U", unformat_memory_size, &mem_sz))
1018 hm->fifo_size = clib_clamp (mem_sz, 4 << 10, 2 << 30);
1019 if (hm->fifo_size != mem_sz)
1020 clib_warning ("invalid fifo size %lu", mem_sz);
1023 return clib_error_return (0, "unknown input `%U'",
1024 format_unformat_error, input);
1029 VLIB_EARLY_CONFIG_FUNCTION (http_config_fn, "http");
1031 VLIB_PLUGIN_REGISTER () = {
1032 .version = VPP_BUILD_VER,
1033 .description = "Hypertext Transfer Protocol (HTTP)",
1034 .default_disabled = 0,
1038 * fd.io coding-style-patch-verification: ON
1041 * eval: (c-set-style "gnu")