aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/http
diff options
context:
space:
mode:
Diffstat (limited to 'src/plugins/http')
-rw-r--r--src/plugins/http/CMakeLists.txt19
-rw-r--r--src/plugins/http/http.c1544
-rw-r--r--src/plugins/http/http.h288
-rw-r--r--src/plugins/http/http_buffer.c219
-rw-r--r--src/plugins/http/http_buffer.h82
-rw-r--r--src/plugins/http/http_timer.c91
-rw-r--r--src/plugins/http/http_timer.h91
7 files changed, 2334 insertions, 0 deletions
diff --git a/src/plugins/http/CMakeLists.txt b/src/plugins/http/CMakeLists.txt
new file mode 100644
index 00000000000..d9cd84a3955
--- /dev/null
+++ b/src/plugins/http/CMakeLists.txt
@@ -0,0 +1,19 @@
+# Copyright (c) 2022 Cisco and/or its affiliates.
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at:
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+add_vpp_plugin(http
+ SOURCES
+ http.c
+ http_buffer.c
+ http_timer.c
+)
diff --git a/src/plugins/http/http.c b/src/plugins/http/http.c
new file mode 100644
index 00000000000..893dd877c29
--- /dev/null
+++ b/src/plugins/http/http.c
@@ -0,0 +1,1544 @@
+/*
+ * Copyright (c) 2022 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <http/http.h>
+#include <vnet/session/session.h>
+#include <http/http_timer.h>
+
+static http_main_t http_main;
+
+#define HTTP_FIFO_THRESH (16 << 10)
+#define CONTENT_LEN_STR "Content-Length: "
+
+/* HTTP state machine result */
+typedef enum http_sm_result_t_
+{
+ HTTP_SM_STOP = 0,
+ HTTP_SM_CONTINUE = 1,
+ HTTP_SM_ERROR = -1,
+} http_sm_result_t;
+
+const char *http_status_code_str[] = {
+#define _(c, s, str) str,
+ foreach_http_status_code
+#undef _
+};
+
+const char *http_content_type_str[] = {
+#define _(s, ext, str) str,
+ foreach_http_content_type
+#undef _
+};
+
+const http_buffer_type_t msg_to_buf_type[] = {
+ [HTTP_MSG_DATA_INLINE] = HTTP_BUFFER_FIFO,
+ [HTTP_MSG_DATA_PTR] = HTTP_BUFFER_PTR,
+};
+
+u8 *
+format_http_state (u8 *s, va_list *va)
+{
+ http_state_t state = va_arg (*va, http_state_t);
+
+ switch (state)
+ {
+ case HTTP_STATE_IDLE:
+ return format (s, "idle");
+ case HTTP_STATE_WAIT_APP_METHOD:
+ return format (s, "wait app method");
+ case HTTP_STATE_WAIT_SERVER_REPLY:
+ return format (s, "wait server reply");
+ case HTTP_STATE_CLIENT_IO_MORE_DATA:
+ return format (s, "client io more data");
+ case HTTP_STATE_WAIT_CLIENT_METHOD:
+ return format (s, "wait client method");
+ case HTTP_STATE_WAIT_APP_REPLY:
+ return format (s, "wait app reply");
+ case HTTP_STATE_APP_IO_MORE_DATA:
+ return format (s, "app io more data");
+ default:
+ break;
+ }
+ return format (s, "unknown");
+}
+
+#define http_state_change(_hc, _state) \
+ do \
+ { \
+ HTTP_DBG (1, "changing http state %U -> %U", format_http_state, \
+ (_hc)->http_state, format_http_state, _state); \
+ (_hc)->http_state = _state; \
+ } \
+ while (0)
+
+static inline http_worker_t *
+http_worker_get (u32 thread_index)
+{
+ return &http_main.wrk[thread_index];
+}
+
+static inline u32
+http_conn_alloc_w_thread (u32 thread_index)
+{
+ http_worker_t *wrk = http_worker_get (thread_index);
+ http_conn_t *hc;
+
+ pool_get_aligned_safe (wrk->conn_pool, hc, CLIB_CACHE_LINE_BYTES);
+ clib_memset (hc, 0, sizeof (*hc));
+ hc->c_thread_index = thread_index;
+ hc->h_hc_index = hc - wrk->conn_pool;
+ hc->h_pa_session_handle = SESSION_INVALID_HANDLE;
+ hc->h_tc_session_handle = SESSION_INVALID_HANDLE;
+ return hc->h_hc_index;
+}
+
+static inline http_conn_t *
+http_conn_get_w_thread (u32 hc_index, u32 thread_index)
+{
+ http_worker_t *wrk = http_worker_get (thread_index);
+ return pool_elt_at_index (wrk->conn_pool, hc_index);
+}
+
+void
+http_conn_free (http_conn_t *hc)
+{
+ http_worker_t *wrk = http_worker_get (hc->c_thread_index);
+ pool_put (wrk->conn_pool, hc);
+}
+
+static u32
+http_listener_alloc (void)
+{
+ http_main_t *hm = &http_main;
+ http_conn_t *lhc;
+
+ pool_get_zero (hm->listener_pool, lhc);
+ lhc->c_c_index = lhc - hm->listener_pool;
+ return lhc->c_c_index;
+}
+
+http_conn_t *
+http_listener_get (u32 lhc_index)
+{
+ return pool_elt_at_index (http_main.listener_pool, lhc_index);
+}
+
+void
+http_listener_free (http_conn_t *lhc)
+{
+ http_main_t *hm = &http_main;
+
+ vec_free (lhc->app_name);
+ if (CLIB_DEBUG)
+ memset (lhc, 0xfc, sizeof (*lhc));
+ pool_put (hm->listener_pool, lhc);
+}
+
+void
+http_disconnect_transport (http_conn_t *hc)
+{
+ vnet_disconnect_args_t a = {
+ .handle = hc->h_tc_session_handle,
+ .app_index = http_main.app_index,
+ };
+
+ hc->state = HTTP_CONN_STATE_CLOSED;
+
+ if (vnet_disconnect_session (&a))
+ clib_warning ("disconnect returned");
+}
+
+static void
+http_conn_timeout_cb (void *hc_handlep)
+{
+ http_conn_t *hc;
+ uword hs_handle;
+
+ hs_handle = pointer_to_uword (hc_handlep);
+ hc = http_conn_get_w_thread (hs_handle & 0x00FFFFFF, hs_handle >> 24);
+
+ HTTP_DBG (1, "terminate thread %d index %d hs %llx", hs_handle >> 24,
+ hs_handle & 0x00FFFFFF, hc);
+ if (!hc)
+ return;
+
+ hc->timer_handle = ~0;
+ session_transport_closing_notify (&hc->connection);
+ http_disconnect_transport (hc);
+}
+
+int
+http_ts_accept_callback (session_t *ts)
+{
+ session_t *ts_listener, *as, *asl;
+ app_worker_t *app_wrk;
+ http_conn_t *lhc, *hc;
+ u32 hc_index, thresh;
+ int rv;
+
+ ts_listener = listen_session_get_from_handle (ts->listener_handle);
+ lhc = http_listener_get (ts_listener->opaque);
+
+ hc_index = http_conn_alloc_w_thread (ts->thread_index);
+ hc = http_conn_get_w_thread (hc_index, ts->thread_index);
+ clib_memcpy_fast (hc, lhc, sizeof (*lhc));
+ hc->c_thread_index = ts->thread_index;
+ hc->h_hc_index = hc_index;
+
+ hc->h_tc_session_handle = session_handle (ts);
+ hc->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
+
+ hc->state = HTTP_CONN_STATE_ESTABLISHED;
+ http_state_change (hc, HTTP_STATE_WAIT_CLIENT_METHOD);
+
+ ts->session_state = SESSION_STATE_READY;
+ ts->opaque = hc_index;
+
+ /*
+ * Alloc session and initialize
+ */
+ as = session_alloc (hc->c_thread_index);
+ hc->c_s_index = as->session_index;
+
+ as->app_wrk_index = hc->h_pa_wrk_index;
+ as->connection_index = hc->c_c_index;
+ as->session_state = SESSION_STATE_ACCEPTING;
+
+ asl = listen_session_get_from_handle (lhc->h_pa_session_handle);
+ as->session_type = asl->session_type;
+ as->listener_handle = lhc->h_pa_session_handle;
+
+ /*
+ * Init session fifos and notify app
+ */
+ if ((rv = app_worker_init_accepted (as)))
+ {
+ HTTP_DBG (1, "failed to allocate fifos");
+ session_free (as);
+ return rv;
+ }
+
+ hc->h_pa_session_handle = session_handle (as);
+ hc->h_pa_wrk_index = as->app_wrk_index;
+ app_wrk = app_worker_get (as->app_wrk_index);
+
+ HTTP_DBG (1, "Accepted on listener %u new connection [%u]%x",
+ ts_listener->opaque, vlib_get_thread_index (), hc_index);
+
+ if ((rv = app_worker_accept_notify (app_wrk, as)))
+ {
+ HTTP_DBG (0, "app accept returned");
+ session_free (as);
+ return rv;
+ }
+
+ /* Avoid enqueuing small chunks of data on transport tx notifications. If
+ * the fifo is small (under 16K) we set the threshold to it's size, meaning
+ * a notification will be given when the fifo empties.
+ */
+ ts = session_get_from_handle (hc->h_tc_session_handle);
+ thresh = clib_min (svm_fifo_size (ts->tx_fifo), HTTP_FIFO_THRESH);
+ svm_fifo_set_deq_thresh (ts->tx_fifo, thresh);
+
+ http_conn_timer_start (hc);
+
+ return 0;
+}
+
+static int
+http_ts_connected_callback (u32 http_app_index, u32 ho_hc_index, session_t *ts,
+ session_error_t err)
+{
+ u32 new_hc_index;
+ session_t *as;
+ http_conn_t *hc, *ho_hc;
+ app_worker_t *app_wrk;
+ int rv;
+
+ ho_hc = http_conn_get_w_thread (ho_hc_index, 0);
+ ASSERT (ho_hc->state == HTTP_CONN_STATE_CONNECTING);
+
+ if (err)
+ {
+ clib_warning ("half-open hc index %d, error: %U", ho_hc_index,
+ format_session_error, err);
+ app_wrk = app_worker_get_if_valid (ho_hc->h_pa_wrk_index);
+ if (app_wrk)
+ app_worker_connect_notify (app_wrk, 0, err, ho_hc->h_pa_app_api_ctx);
+ return 0;
+ }
+
+ new_hc_index = http_conn_alloc_w_thread (ts->thread_index);
+ hc = http_conn_get_w_thread (new_hc_index, ts->thread_index);
+
+ clib_memcpy_fast (hc, ho_hc, sizeof (*hc));
+
+ hc->c_thread_index = ts->thread_index;
+ hc->h_tc_session_handle = session_handle (ts);
+ hc->c_c_index = new_hc_index;
+ hc->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
+ hc->state = HTTP_CONN_STATE_ESTABLISHED;
+ http_state_change (hc, HTTP_STATE_WAIT_APP_METHOD);
+
+ ts->session_state = SESSION_STATE_READY;
+ ts->opaque = new_hc_index;
+
+ /* allocate app session and initialize */
+
+ as = session_alloc (hc->c_thread_index);
+ hc->c_s_index = as->session_index;
+ as->connection_index = hc->c_c_index;
+ as->app_wrk_index = hc->h_pa_wrk_index;
+ as->session_state = SESSION_STATE_READY;
+ as->opaque = hc->h_pa_app_api_ctx;
+ as->session_type = session_type_from_proto_and_ip (
+ TRANSPORT_PROTO_HTTP, session_type_is_ip4 (ts->session_type));
+
+ HTTP_DBG (1, "half-open hc index %d, hc index %d", ho_hc_index,
+ new_hc_index);
+
+ app_wrk = app_worker_get (hc->h_pa_wrk_index);
+ if (!app_wrk)
+ {
+ clib_warning ("no app worker");
+ return -1;
+ }
+
+ if ((rv = app_worker_init_connected (app_wrk, as)))
+ {
+ HTTP_DBG (1, "failed to allocate fifos");
+ session_free (as);
+ return rv;
+ }
+ app_worker_connect_notify (app_wrk, as, err, hc->h_pa_app_api_ctx);
+ hc->h_pa_session_handle = session_handle (as);
+ http_conn_timer_start (hc);
+
+ return 0;
+}
+
+static void
+http_ts_disconnect_callback (session_t *ts)
+{
+ http_conn_t *hc;
+
+ hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
+
+ if (hc->state < HTTP_CONN_STATE_TRANSPORT_CLOSED)
+ hc->state = HTTP_CONN_STATE_TRANSPORT_CLOSED;
+
+ /* Nothing more to rx, propagate to app */
+ if (!svm_fifo_max_dequeue_cons (ts->rx_fifo))
+ session_transport_closing_notify (&hc->connection);
+}
+
+static void
+http_ts_reset_callback (session_t *ts)
+{
+ http_conn_t *hc;
+
+ hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
+
+ hc->state = HTTP_CONN_STATE_CLOSED;
+ http_buffer_free (&hc->tx_buf);
+ http_state_change (hc, HTTP_STATE_WAIT_CLIENT_METHOD);
+ session_transport_reset_notify (&hc->connection);
+
+ http_disconnect_transport (hc);
+}
+
+/**
+ * http error boilerplate
+ */
+static const char *http_error_template = "HTTP/1.1 %s\r\n"
+ "Date: %U GMT\r\n"
+ "Content-Type: text/html\r\n"
+ "Connection: close\r\n"
+ "Pragma: no-cache\r\n"
+ "Content-Length: 0\r\n\r\n";
+
+static const char *http_redirect_template = "HTTP/1.1 %s\r\n";
+
+/**
+ * http response boilerplate
+ */
+static const char *http_response_template = "HTTP/1.1 %s\r\n"
+ "Date: %U GMT\r\n"
+ "Expires: %U GMT\r\n"
+ "Server: %s\r\n"
+ "Content-Type: %s\r\n"
+ "Content-Length: %lu\r\n\r\n";
+
+static const char *http_request_template = "GET %s HTTP/1.1\r\n"
+ "User-Agent: %s\r\n"
+ "Accept: */*\r\n";
+
+static u32
+http_send_data (http_conn_t *hc, u8 *data, u32 length, u32 offset)
+{
+ const u32 max_burst = 64 << 10;
+ session_t *ts;
+ u32 to_send;
+ int sent;
+
+ ts = session_get_from_handle (hc->h_tc_session_handle);
+
+ to_send = clib_min (length - offset, max_burst);
+ sent = svm_fifo_enqueue (ts->tx_fifo, to_send, data + offset);
+
+ if (sent <= 0)
+ return offset;
+
+ if (svm_fifo_set_event (ts->tx_fifo))
+ session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX);
+
+ return (offset + sent);
+}
+
+static void
+http_send_error (http_conn_t *hc, http_status_code_t ec)
+{
+ http_main_t *hm = &http_main;
+ u8 *data;
+ f64 now;
+
+ if (ec >= HTTP_N_STATUS)
+ ec = HTTP_STATUS_INTERNAL_ERROR;
+
+ now = clib_timebase_now (&hm->timebase);
+ data = format (0, http_error_template, http_status_code_str[ec],
+ format_clib_timebase_time, now);
+ http_send_data (hc, data, vec_len (data), 0);
+ vec_free (data);
+}
+
+static int
+http_read_message (http_conn_t *hc)
+{
+ u32 max_deq, cursize;
+ session_t *ts;
+ int n_read;
+
+ ts = session_get_from_handle (hc->h_tc_session_handle);
+
+ cursize = vec_len (hc->rx_buf);
+ max_deq = svm_fifo_max_dequeue (ts->rx_fifo);
+ if (PREDICT_FALSE (max_deq == 0))
+ return -1;
+
+ vec_validate (hc->rx_buf, cursize + max_deq - 1);
+ n_read = svm_fifo_dequeue (ts->rx_fifo, max_deq, hc->rx_buf + cursize);
+ ASSERT (n_read == max_deq);
+
+ if (svm_fifo_is_empty (ts->rx_fifo))
+ svm_fifo_unset_event (ts->rx_fifo);
+
+ vec_set_len (hc->rx_buf, cursize + n_read);
+ return 0;
+}
+
+static int
+v_find_index (u8 *vec, u32 offset, char *str)
+{
+ int start_index = offset;
+ u32 slen = (u32) strnlen_s_inline (str, 16);
+ u32 vlen = vec_len (vec);
+
+ ASSERT (slen > 0);
+
+ if (vlen <= slen)
+ return -1;
+
+ for (; start_index < (vlen - slen); start_index++)
+ {
+ if (!memcmp (vec + start_index, str, slen))
+ return start_index;
+ }
+
+ return -1;
+}
+
+static int
+http_parse_header (http_conn_t *hc, int *content_length)
+{
+ unformat_input_t input;
+ int i, len;
+ u8 *line;
+
+ i = v_find_index (hc->rx_buf, hc->rx_buf_offset, CONTENT_LEN_STR);
+ if (i < 0)
+ {
+ clib_warning ("cannot find '%s' in the header!", CONTENT_LEN_STR);
+ return -1;
+ }
+
+ hc->rx_buf_offset = i;
+
+ i = v_find_index (hc->rx_buf, hc->rx_buf_offset, "\n");
+ if (i < 0)
+ {
+ clib_warning ("end of line missing; incomplete data");
+ return -1;
+ }
+
+ len = i - hc->rx_buf_offset;
+ line = vec_new (u8, len);
+ clib_memcpy (line, hc->rx_buf + hc->rx_buf_offset, len);
+
+ unformat_init_vector (&input, line);
+ if (!unformat (&input, CONTENT_LEN_STR "%d", content_length))
+ {
+ clib_warning ("failed to unformat content length!");
+ return -1;
+ }
+ unformat_free (&input);
+
+ /* skip rest of the header */
+ hc->rx_buf_offset += len;
+ i = v_find_index (hc->rx_buf, hc->rx_buf_offset, "<html>");
+ if (i < 0)
+ {
+ clib_warning ("<html> tag not found");
+ return -1;
+ }
+ hc->rx_buf_offset = i;
+
+ return 0;
+}
+
+static http_sm_result_t
+http_state_wait_server_reply (http_conn_t *hc, transport_send_params_t *sp)
+{
+ int i, rv, content_length;
+ http_msg_t msg = {};
+ app_worker_t *app_wrk;
+ session_t *as;
+
+ rv = http_read_message (hc);
+
+ /* Nothing yet, wait for data or timer expire */
+ if (rv)
+ {
+ HTTP_DBG (1, "no data to deq");
+ return HTTP_SM_STOP;
+ }
+
+ if (vec_len (hc->rx_buf) < 8)
+ {
+ clib_warning ("response buffer too short");
+ goto error;
+ }
+
+ if ((i = v_find_index (hc->rx_buf, 0, "200 OK")) >= 0)
+ {
+ msg.type = HTTP_MSG_REPLY;
+ msg.content_type = HTTP_CONTENT_TEXT_HTML;
+ msg.code = HTTP_STATUS_OK;
+ msg.data.type = HTTP_MSG_DATA_INLINE;
+ msg.data.len = 0;
+
+ rv = http_parse_header (hc, &content_length);
+ if (rv)
+ {
+ clib_warning ("failed to parse http reply");
+ goto error;
+ }
+ msg.data.len = content_length;
+ u32 dlen = vec_len (hc->rx_buf) - hc->rx_buf_offset;
+ as = session_get_from_handle (hc->h_pa_session_handle);
+ svm_fifo_seg_t segs[2] = { { (u8 *) &msg, sizeof (msg) },
+ { &hc->rx_buf[hc->rx_buf_offset], dlen } };
+
+ rv = svm_fifo_enqueue_segments (as->rx_fifo, segs, 2,
+ 0 /* allow partial */);
+ if (rv < 0)
+ {
+ clib_warning ("error enqueue");
+ return HTTP_SM_ERROR;
+ }
+
+ hc->rx_buf_offset += dlen;
+ hc->to_recv = content_length - dlen;
+
+ if (hc->rx_buf_offset == vec_len (hc->rx_buf))
+ {
+ vec_reset_length (hc->rx_buf);
+ hc->rx_buf_offset = 0;
+ }
+
+ if (hc->to_recv == 0)
+ {
+ hc->rx_buf_offset = 0;
+ vec_reset_length (hc->rx_buf);
+ http_state_change (hc, HTTP_STATE_WAIT_APP_METHOD);
+ }
+ else
+ {
+ http_state_change (hc, HTTP_STATE_CLIENT_IO_MORE_DATA);
+ }
+
+ app_wrk = app_worker_get_if_valid (as->app_wrk_index);
+ if (app_wrk)
+ app_worker_rx_notify (app_wrk, as);
+ return HTTP_SM_STOP;
+ }
+ else
+ {
+ clib_warning ("Unknown http method %v", hc->rx_buf);
+ goto error;
+ }
+
+error:
+ session_transport_closing_notify (&hc->connection);
+ session_transport_closed_notify (&hc->connection);
+ http_disconnect_transport (hc);
+ return HTTP_SM_ERROR;
+}
+
+static http_sm_result_t
+http_state_wait_client_method (http_conn_t *hc, transport_send_params_t *sp)
+{
+ http_status_code_t ec;
+ app_worker_t *app_wrk;
+ http_msg_t msg;
+ session_t *as;
+ int i, rv;
+ u32 len;
+ u8 *buf;
+
+ rv = http_read_message (hc);
+
+ /* Nothing yet, wait for data or timer expire */
+ if (rv)
+ return HTTP_SM_STOP;
+
+ if (vec_len (hc->rx_buf) < 8)
+ {
+ ec = HTTP_STATUS_BAD_REQUEST;
+ goto error;
+ }
+
+ if ((i = v_find_index (hc->rx_buf, 0, "GET ")) >= 0)
+ {
+ hc->method = HTTP_REQ_GET;
+ hc->rx_buf_offset = i + 5;
+
+ i = v_find_index (hc->rx_buf, hc->rx_buf_offset, "HTTP");
+ if (i < 0)
+ {
+ ec = HTTP_STATUS_BAD_REQUEST;
+ goto error;
+ }
+
+ HTTP_DBG (0, "GET method %v", hc->rx_buf);
+ len = i - hc->rx_buf_offset - 1;
+ }
+ else if ((i = v_find_index (hc->rx_buf, 0, "POST ")) >= 0)
+ {
+ hc->method = HTTP_REQ_POST;
+ hc->rx_buf_offset = i + 6;
+ len = vec_len (hc->rx_buf) - hc->rx_buf_offset - 1;
+ HTTP_DBG (0, "POST method %v", hc->rx_buf);
+ }
+ else
+ {
+ HTTP_DBG (0, "Unknown http method %v", hc->rx_buf);
+ ec = HTTP_STATUS_METHOD_NOT_ALLOWED;
+ goto error;
+ }
+
+ buf = &hc->rx_buf[hc->rx_buf_offset];
+
+ msg.type = HTTP_MSG_REQUEST;
+ msg.method_type = hc->method;
+ msg.content_type = HTTP_CONTENT_TEXT_HTML;
+ msg.data.type = HTTP_MSG_DATA_INLINE;
+ msg.data.len = len;
+
+ svm_fifo_seg_t segs[2] = { { (u8 *) &msg, sizeof (msg) }, { buf, len } };
+
+ as = session_get_from_handle (hc->h_pa_session_handle);
+ rv = svm_fifo_enqueue_segments (as->rx_fifo, segs, 2, 0 /* allow partial */);
+ if (rv < 0 || rv != sizeof (msg) + len)
+ {
+ clib_warning ("failed app enqueue");
+ /* This should not happen as we only handle 1 request per session,
+ * and fifo is allocated, but going forward we should consider
+ * rescheduling */
+ return HTTP_SM_ERROR;
+ }
+
+ vec_free (hc->rx_buf);
+ http_state_change (hc, HTTP_STATE_WAIT_APP_REPLY);
+
+ app_wrk = app_worker_get_if_valid (as->app_wrk_index);
+ if (app_wrk)
+ app_worker_rx_notify (app_wrk, as);
+
+ return HTTP_SM_STOP;
+
+error:
+
+ http_send_error (hc, ec);
+ session_transport_closing_notify (&hc->connection);
+ http_disconnect_transport (hc);
+
+ return HTTP_SM_ERROR;
+}
+
+static http_sm_result_t
+http_state_wait_app_reply (http_conn_t *hc, transport_send_params_t *sp)
+{
+ http_main_t *hm = &http_main;
+ u8 *header;
+ u32 offset;
+ f64 now;
+ session_t *as;
+ http_status_code_t sc;
+ http_msg_t msg;
+ int rv;
+
+ as = session_get_from_handle (hc->h_pa_session_handle);
+
+ rv = svm_fifo_dequeue (as->tx_fifo, sizeof (msg), (u8 *) &msg);
+ ASSERT (rv == sizeof (msg));
+
+ if (msg.data.type > HTTP_MSG_DATA_PTR)
+ {
+ clib_warning ("no data");
+ sc = HTTP_STATUS_INTERNAL_ERROR;
+ goto error;
+ }
+
+ if (msg.type != HTTP_MSG_REPLY)
+ {
+ clib_warning ("unexpected message type %d", msg.type);
+ sc = HTTP_STATUS_INTERNAL_ERROR;
+ goto error;
+ }
+
+ http_buffer_init (&hc->tx_buf, msg_to_buf_type[msg.data.type], as->tx_fifo,
+ msg.data.len);
+
+ /*
+ * Add headers. For now:
+ * - current time
+ * - expiration time
+ * - server name
+ * - content type
+ * - data length
+ */
+ now = clib_timebase_now (&hm->timebase);
+
+ switch (msg.code)
+ {
+ case HTTP_STATUS_NOT_FOUND:
+ case HTTP_STATUS_METHOD_NOT_ALLOWED:
+ case HTTP_STATUS_BAD_REQUEST:
+ case HTTP_STATUS_INTERNAL_ERROR:
+ case HTTP_STATUS_OK:
+ header =
+ format (0, http_response_template, http_status_code_str[msg.code],
+ /* Date */
+ format_clib_timebase_time, now,
+ /* Expires */
+ format_clib_timebase_time, now + 600.0,
+ /* Server */
+ hc->app_name,
+ /* Content type */
+ http_content_type_str[msg.content_type],
+ /* Length */
+ msg.data.len);
+ break;
+ case HTTP_STATUS_MOVED:
+ header =
+ format (0, http_redirect_template, http_status_code_str[msg.code]);
+ /* Location: http(s)://new-place already queued up as data */
+ break;
+ default:
+ clib_warning ("unsupported status code: %d", msg.code);
+ return HTTP_SM_ERROR;
+ }
+
+ offset = http_send_data (hc, header, vec_len (header), 0);
+ if (offset != vec_len (header))
+ {
+ clib_warning ("couldn't send response header!");
+ sc = HTTP_STATUS_INTERNAL_ERROR;
+ vec_free (header);
+ goto error;
+ }
+ vec_free (header);
+
+ /* Start sending the actual data */
+ http_state_change (hc, HTTP_STATE_APP_IO_MORE_DATA);
+
+ ASSERT (sp->max_burst_size >= offset);
+ sp->max_burst_size -= offset;
+ return HTTP_SM_CONTINUE;
+
+error:
+ clib_warning ("unexpected msg type from app %u", msg.type);
+ http_send_error (hc, sc);
+ http_state_change (hc, HTTP_STATE_WAIT_CLIENT_METHOD);
+ session_transport_closing_notify (&hc->connection);
+ http_disconnect_transport (hc);
+ return HTTP_SM_STOP;
+}
+
+static http_sm_result_t
+http_state_wait_app_method (http_conn_t *hc, transport_send_params_t *sp)
+{
+ http_msg_t msg;
+ session_t *as;
+ u8 *buf = 0, *request;
+ u32 offset;
+ int rv;
+
+ as = session_get_from_handle (hc->h_pa_session_handle);
+
+ rv = svm_fifo_dequeue (as->tx_fifo, sizeof (msg), (u8 *) &msg);
+ ASSERT (rv == sizeof (msg));
+
+ if (msg.data.type > HTTP_MSG_DATA_PTR)
+ {
+ clib_warning ("no data");
+ goto error;
+ }
+
+ if (msg.type != HTTP_MSG_REQUEST)
+ {
+ clib_warning ("unexpected message type %d", msg.type);
+ goto error;
+ }
+
+ /* currently we support only GET method */
+ if (msg.method_type != HTTP_REQ_GET)
+ {
+ clib_warning ("unsupported method %d", msg.method_type);
+ goto error;
+ }
+
+ vec_validate (buf, msg.data.len - 1);
+ rv = svm_fifo_dequeue (as->tx_fifo, msg.data.len, buf);
+ ASSERT (rv == msg.data.len);
+
+ request = format (0, http_request_template, buf, hc->app_name);
+ offset = http_send_data (hc, request, vec_len (request), 0);
+ if (offset != vec_len (request))
+ {
+ clib_warning ("sending request failed!");
+ goto error;
+ }
+
+ http_state_change (hc, HTTP_STATE_WAIT_SERVER_REPLY);
+
+ vec_free (buf);
+ vec_free (request);
+
+ return HTTP_SM_STOP;
+
+error:
+ svm_fifo_dequeue_drop_all (as->tx_fifo);
+ session_transport_closing_notify (&hc->connection);
+ session_transport_closed_notify (&hc->connection);
+ http_disconnect_transport (hc);
+ return HTTP_SM_ERROR;
+}
+
+static http_sm_result_t
+http_state_client_io_more_data (http_conn_t *hc, transport_send_params_t *sp)
+{
+ session_t *as, *ts;
+ app_worker_t *app_wrk;
+ svm_fifo_seg_t _seg, *seg = &_seg;
+ u32 max_len, max_deq, max_enq, n_segs = 1;
+ int rv, len;
+
+ as = session_get_from_handle (hc->h_pa_session_handle);
+ ts = session_get_from_handle (hc->h_tc_session_handle);
+
+ max_deq = svm_fifo_max_dequeue (ts->rx_fifo);
+ if (max_deq == 0)
+ {
+ HTTP_DBG (1, "no data to deq");
+ return HTTP_SM_STOP;
+ }
+
+ max_enq = svm_fifo_max_enqueue (as->rx_fifo);
+ if (max_enq == 0)
+ {
+ HTTP_DBG (1, "app's rx fifo full");
+ svm_fifo_add_want_deq_ntf (as->rx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
+ return HTTP_SM_STOP;
+ }
+
+ max_len = clib_min (max_enq, max_deq);
+ len = svm_fifo_segments (ts->rx_fifo, 0, seg, &n_segs, max_len);
+ if (len < 0)
+ {
+ HTTP_DBG (1, "svm_fifo_segments() len %d", len);
+ return HTTP_SM_STOP;
+ }
+
+ rv = svm_fifo_enqueue_segments (as->rx_fifo, seg, 1, 0 /* allow partial */);
+ if (rv < 0)
+ {
+ clib_warning ("data enqueue failed, rv: %d", rv);
+ return HTTP_SM_ERROR;
+ }
+
+ svm_fifo_dequeue_drop (ts->rx_fifo, rv);
+ if (rv > hc->to_recv)
+ {
+ clib_warning ("http protocol error: received more data than expected");
+ session_transport_closing_notify (&hc->connection);
+ http_disconnect_transport (hc);
+ http_state_change (hc, HTTP_STATE_WAIT_APP_METHOD);
+ return HTTP_SM_ERROR;
+ }
+ hc->to_recv -= rv;
+ HTTP_DBG (1, "drained %d from ts; remains %d", rv, hc->to_recv);
+
+ if (hc->to_recv == 0)
+ {
+ hc->rx_buf_offset = 0;
+ vec_reset_length (hc->rx_buf);
+ http_state_change (hc, HTTP_STATE_WAIT_APP_METHOD);
+ }
+
+ app_wrk = app_worker_get_if_valid (as->app_wrk_index);
+ if (app_wrk)
+ app_worker_rx_notify (app_wrk, as);
+
+ if (svm_fifo_max_dequeue_cons (ts->rx_fifo))
+ session_enqueue_notify (ts);
+
+ return HTTP_SM_STOP;
+}
+
+static http_sm_result_t
+http_state_app_io_more_data (http_conn_t *hc, transport_send_params_t *sp)
+{
+ u32 max_send = 64 << 10, n_segs;
+ http_buffer_t *hb = &hc->tx_buf;
+ svm_fifo_seg_t *seg;
+ session_t *ts;
+ int sent = 0;
+
+ max_send = clib_min (max_send, sp->max_burst_size);
+ ts = session_get_from_handle (hc->h_tc_session_handle);
+ if ((seg = http_buffer_get_segs (hb, max_send, &n_segs)))
+ sent = svm_fifo_enqueue_segments (ts->tx_fifo, seg, n_segs,
+ 1 /* allow partial */);
+
+ if (sent > 0)
+ {
+ /* Ask scheduler to notify app of deq event if needed */
+ sp->bytes_dequeued += http_buffer_drain (hb, sent);
+ sp->max_burst_size -= sent;
+ }
+
+ /* Not finished sending all data */
+ if (!http_buffer_is_drained (hb))
+ {
+ if (sent && svm_fifo_set_event (ts->tx_fifo))
+ session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX);
+
+ if (svm_fifo_max_enqueue (ts->tx_fifo) < HTTP_FIFO_THRESH)
+ {
+ /* Deschedule http session and wait for deq notification if
+ * underlying ts tx fifo almost full */
+ svm_fifo_add_want_deq_ntf (ts->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
+ transport_connection_deschedule (&hc->connection);
+ sp->flags |= TRANSPORT_SND_F_DESCHED;
+ }
+ }
+ else
+ {
+ if (sent && svm_fifo_set_event (ts->tx_fifo))
+ session_send_io_evt_to_thread (ts->tx_fifo, SESSION_IO_EVT_TX_FLUSH);
+
+ /* Finished transaction, back to HTTP_STATE_WAIT_METHOD */
+ http_state_change (hc, HTTP_STATE_WAIT_CLIENT_METHOD);
+ http_buffer_free (&hc->tx_buf);
+ }
+
+ return HTTP_SM_STOP;
+}
+
+typedef http_sm_result_t (*http_sm_handler) (http_conn_t *,
+ transport_send_params_t *sp);
+
+static http_sm_handler state_funcs[HTTP_N_STATES] = {
+ 0, /* idle state */
+ http_state_wait_app_method,
+ http_state_wait_client_method,
+ http_state_wait_server_reply,
+ http_state_wait_app_reply,
+ http_state_client_io_more_data,
+ http_state_app_io_more_data,
+};
+
+static void
+http_req_run_state_machine (http_conn_t *hc, transport_send_params_t *sp)
+{
+ http_sm_result_t res;
+
+ do
+ {
+ res = state_funcs[hc->http_state](hc, sp);
+ if (res == HTTP_SM_ERROR)
+ {
+ HTTP_DBG (1, "error in state machine %d", res);
+ return;
+ }
+ }
+ while (res == HTTP_SM_CONTINUE);
+
+ /* Reset the session expiration timer */
+ http_conn_timer_update (hc);
+}
+
+static int
+http_ts_rx_callback (session_t *ts)
+{
+ http_conn_t *hc;
+
+ hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
+ if (!hc)
+ {
+ clib_warning ("http connection not found (ts %d)", ts->opaque);
+ return -1;
+ }
+
+ if (hc->state == HTTP_CONN_STATE_CLOSED)
+ {
+ svm_fifo_dequeue_drop_all (ts->tx_fifo);
+ return 0;
+ }
+
+ http_req_run_state_machine (hc, 0);
+
+ if (hc->state == HTTP_CONN_STATE_TRANSPORT_CLOSED)
+ {
+ if (!svm_fifo_max_dequeue_cons (ts->rx_fifo))
+ session_transport_closing_notify (&hc->connection);
+ }
+ return 0;
+}
+
+int
+http_ts_builtin_tx_callback (session_t *ts)
+{
+ http_conn_t *hc;
+
+ hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
+ transport_connection_reschedule (&hc->connection);
+
+ return 0;
+}
+
+static void
+http_ts_cleanup_callback (session_t *ts, session_cleanup_ntf_t ntf)
+{
+ http_conn_t *hc;
+
+ if (ntf == SESSION_CLEANUP_TRANSPORT)
+ return;
+
+ hc = http_conn_get_w_thread (ts->opaque, ts->thread_index);
+ if (!hc)
+ {
+ clib_warning ("no http connection for %u", ts->session_index);
+ return;
+ }
+
+ vec_free (hc->rx_buf);
+
+ http_buffer_free (&hc->tx_buf);
+ http_conn_timer_stop (hc);
+
+ session_transport_delete_notify (&hc->connection);
+ http_conn_free (hc);
+}
+
+int
+http_add_segment_callback (u32 client_index, u64 segment_handle)
+{
+ /* No-op for builtin */
+ return 0;
+}
+
+int
+http_del_segment_callback (u32 client_index, u64 segment_handle)
+{
+ return 0;
+}
+
+static session_cb_vft_t http_app_cb_vft = {
+ .session_accept_callback = http_ts_accept_callback,
+ .session_disconnect_callback = http_ts_disconnect_callback,
+ .session_connected_callback = http_ts_connected_callback,
+ .session_reset_callback = http_ts_reset_callback,
+ .session_cleanup_callback = http_ts_cleanup_callback,
+ .add_segment_callback = http_add_segment_callback,
+ .del_segment_callback = http_del_segment_callback,
+ .builtin_app_rx_callback = http_ts_rx_callback,
+ .builtin_app_tx_callback = http_ts_builtin_tx_callback,
+};
+
+static clib_error_t *
+http_transport_enable (vlib_main_t *vm, u8 is_en)
+{
+ vnet_app_detach_args_t _da, *da = &_da;
+ vnet_app_attach_args_t _a, *a = &_a;
+ u64 options[APP_OPTIONS_N_OPTIONS];
+ http_main_t *hm = &http_main;
+
+ if (!is_en)
+ {
+ da->app_index = hm->app_index;
+ da->api_client_index = APP_INVALID_INDEX;
+ vnet_application_detach (da);
+ return 0;
+ }
+
+ vec_validate (hm->wrk, vlib_num_workers ());
+
+ clib_memset (a, 0, sizeof (*a));
+ clib_memset (options, 0, sizeof (options));
+
+ a->session_cb_vft = &http_app_cb_vft;
+ a->api_client_index = APP_INVALID_INDEX;
+ a->options = options;
+ a->name = format (0, "http");
+ a->options[APP_OPTIONS_SEGMENT_SIZE] = hm->first_seg_size;
+ a->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = hm->add_seg_size;
+ a->options[APP_OPTIONS_RX_FIFO_SIZE] = hm->fifo_size;
+ a->options[APP_OPTIONS_TX_FIFO_SIZE] = hm->fifo_size;
+ a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
+ a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
+ a->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_IS_TRANSPORT_APP;
+
+ if (vnet_application_attach (a))
+ return clib_error_return (0, "failed to attach http app");
+
+ hm->app_index = a->app_index;
+ vec_free (a->name);
+
+ clib_timebase_init (&hm->timebase, 0 /* GMT */, CLIB_TIMEBASE_DAYLIGHT_NONE,
+ &vm->clib_time /* share the system clock */);
+
+ http_timers_init (vm, http_conn_timeout_cb);
+
+ return 0;
+}
+
+static int
+http_transport_connect (transport_endpoint_cfg_t *tep)
+{
+ vnet_connect_args_t _cargs, *cargs = &_cargs;
+ http_main_t *hm = &http_main;
+ session_endpoint_cfg_t *sep = (session_endpoint_cfg_t *) tep;
+ application_t *app;
+ http_conn_t *hc;
+ int error;
+ u32 hc_index;
+ app_worker_t *app_wrk = app_worker_get (sep->app_wrk_index);
+
+ clib_memset (cargs, 0, sizeof (*cargs));
+ clib_memcpy (&cargs->sep_ext, sep, sizeof (session_endpoint_cfg_t));
+ cargs->sep.transport_proto = TRANSPORT_PROTO_TCP;
+ cargs->app_index = hm->app_index;
+ app = application_get (app_wrk->app_index);
+ cargs->sep_ext.ns_index = app->ns_index;
+
+ hc_index = http_conn_alloc_w_thread (0 /* ts->thread_index */);
+ hc = http_conn_get_w_thread (hc_index, 0);
+ hc->h_pa_wrk_index = sep->app_wrk_index;
+ hc->h_pa_app_api_ctx = sep->opaque;
+ hc->state = HTTP_CONN_STATE_CONNECTING;
+ cargs->api_context = hc_index;
+
+ if (vec_len (app->name))
+ hc->app_name = vec_dup (app->name);
+ else
+ hc->app_name = format (0, "VPP HTTP client");
+
+ HTTP_DBG (1, "hc ho_index %x", hc_index);
+
+ if ((error = vnet_connect (cargs)))
+ return error;
+
+ return 0;
+}
+
+static u32
+http_start_listen (u32 app_listener_index, transport_endpoint_cfg_t *tep)
+{
+ vnet_listen_args_t _args = {}, *args = &_args;
+ session_t *ts_listener, *app_listener;
+ http_main_t *hm = &http_main;
+ session_endpoint_cfg_t *sep;
+ app_worker_t *app_wrk;
+ transport_proto_t tp;
+ app_listener_t *al;
+ application_t *app;
+ http_conn_t *lhc;
+ u32 lhc_index;
+
+ sep = (session_endpoint_cfg_t *) tep;
+
+ app_wrk = app_worker_get (sep->app_wrk_index);
+ app = application_get (app_wrk->app_index);
+
+ args->app_index = hm->app_index;
+ args->sep_ext = *sep;
+ args->sep_ext.ns_index = app->ns_index;
+ tp = sep->ext_cfg ? TRANSPORT_PROTO_TLS : TRANSPORT_PROTO_TCP;
+ args->sep_ext.transport_proto = tp;
+
+ if (vnet_listen (args))
+ return SESSION_INVALID_INDEX;
+
+ lhc_index = http_listener_alloc ();
+ lhc = http_listener_get (lhc_index);
+
+ /* Grab transport connection listener and link to http listener */
+ lhc->h_tc_session_handle = args->handle;
+ al = app_listener_get_w_handle (lhc->h_tc_session_handle);
+ ts_listener = app_listener_get_session (al);
+ ts_listener->opaque = lhc_index;
+
+ /* Grab application listener and link to http listener */
+ app_listener = listen_session_get (app_listener_index);
+ lhc->h_pa_wrk_index = sep->app_wrk_index;
+ lhc->h_pa_session_handle = listen_session_get_handle (app_listener);
+ lhc->c_s_index = app_listener_index;
+ lhc->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
+
+ if (vec_len (app->name))
+ lhc->app_name = vec_dup (app->name);
+ else
+ lhc->app_name = format (0, "VPP server app");
+
+ return lhc_index;
+}
+
+static u32
+http_stop_listen (u32 listener_index)
+{
+ http_conn_t *lhc;
+ int rv;
+
+ lhc = http_listener_get (listener_index);
+
+ vnet_unlisten_args_t a = {
+ .handle = lhc->h_tc_session_handle,
+ .app_index = http_main.app_index,
+ .wrk_map_index = 0 /* default wrk */
+ };
+
+ if ((rv = vnet_unlisten (&a)))
+ clib_warning ("unlisten returned %d", rv);
+
+ http_listener_free (lhc);
+
+ return 0;
+}
+
+static void
+http_transport_close (u32 hc_index, u32 thread_index)
+{
+ session_t *as;
+ http_conn_t *hc;
+
+ HTTP_DBG (1, "App disconnecting %x", hc_index);
+
+ hc = http_conn_get_w_thread (hc_index, thread_index);
+ if (hc->state == HTTP_CONN_STATE_CONNECTING)
+ {
+ hc->state = HTTP_CONN_STATE_APP_CLOSED;
+ http_disconnect_transport (hc);
+ return;
+ }
+ else if (hc->state == HTTP_CONN_STATE_CLOSED)
+ {
+ HTTP_DBG (1, "nothing to do, already closed");
+ return;
+ }
+ as = session_get_from_handle (hc->h_pa_session_handle);
+
+ /* Nothing more to send, confirm close */
+ if (!svm_fifo_max_dequeue_cons (as->tx_fifo))
+ {
+ session_transport_closed_notify (&hc->connection);
+ http_disconnect_transport (hc);
+ }
+ else
+ {
+ /* Wait for all data to be written to ts */
+ hc->state = HTTP_CONN_STATE_APP_CLOSED;
+ }
+}
+
+static transport_connection_t *
+http_transport_get_connection (u32 hc_index, u32 thread_index)
+{
+ http_conn_t *hc = http_conn_get_w_thread (hc_index, thread_index);
+ return &hc->connection;
+}
+
+static transport_connection_t *
+http_transport_get_listener (u32 listener_index)
+{
+ http_conn_t *lhc = http_listener_get (listener_index);
+ return &lhc->connection;
+}
+
+static int
+http_app_tx_callback (void *session, transport_send_params_t *sp)
+{
+ session_t *as = (session_t *) session;
+ u32 max_burst_sz, sent;
+ http_conn_t *hc;
+
+ HTTP_DBG (1, "app session conn index %x", as->connection_index);
+
+ hc = http_conn_get_w_thread (as->connection_index, as->thread_index);
+ if (!http_state_is_tx_valid (hc))
+ {
+ if (hc->state != HTTP_CONN_STATE_CLOSED)
+ clib_warning ("app data req state '%U' session state %u",
+ format_http_state, hc->http_state, hc->state);
+ svm_fifo_dequeue_drop_all (as->tx_fifo);
+ return 0;
+ }
+
+ max_burst_sz = sp->max_burst_size * TRANSPORT_PACER_MIN_MSS;
+ sp->max_burst_size = max_burst_sz;
+
+ http_req_run_state_machine (hc, sp);
+
+ if (hc->state == HTTP_CONN_STATE_APP_CLOSED)
+ {
+ if (!svm_fifo_max_dequeue_cons (as->tx_fifo))
+ http_disconnect_transport (hc);
+ }
+
+ sent = max_burst_sz - sp->max_burst_size;
+
+ return sent > 0 ? clib_max (sent / TRANSPORT_PACER_MIN_MSS, 1) : 0;
+}
+
+static void
+http_transport_get_endpoint (u32 hc_index, u32 thread_index,
+ transport_endpoint_t *tep, u8 is_lcl)
+{
+ http_conn_t *hc = http_conn_get_w_thread (hc_index, thread_index);
+ session_t *ts;
+
+ ts = session_get_from_handle (hc->h_tc_session_handle);
+ session_get_endpoint (ts, tep, is_lcl);
+}
+
+static u8 *
+format_http_connection (u8 *s, va_list *args)
+{
+ http_conn_t *hc = va_arg (*args, http_conn_t *);
+ session_t *ts;
+
+ ts = session_get_from_handle (hc->h_tc_session_handle);
+ s = format (s, "[%d:%d][H] app_wrk %u ts %d:%d", hc->c_thread_index,
+ hc->c_s_index, hc->h_pa_wrk_index, ts->thread_index,
+ ts->session_index);
+
+ return s;
+}
+
+static u8 *
+format_http_listener (u8 *s, va_list *args)
+{
+ http_conn_t *lhc = va_arg (*args, http_conn_t *);
+ app_listener_t *al;
+ session_t *lts;
+
+ al = app_listener_get_w_handle (lhc->h_tc_session_handle);
+ lts = app_listener_get_session (al);
+ s = format (s, "[%d:%d][H] app_wrk %u ts %d:%d", lhc->c_thread_index,
+ lhc->c_s_index, lhc->h_pa_wrk_index, lts->thread_index,
+ lts->session_index);
+
+ return s;
+}
+
+static u8 *
+format_http_conn_state (u8 *s, va_list *args)
+{
+ http_conn_t *hc = va_arg (*args, http_conn_t *);
+
+ switch (hc->state)
+ {
+ case HTTP_CONN_STATE_LISTEN:
+ s = format (s, "LISTEN");
+ break;
+ case HTTP_CONN_STATE_CONNECTING:
+ s = format (s, "CONNECTING");
+ break;
+ case HTTP_CONN_STATE_ESTABLISHED:
+ s = format (s, "ESTABLISHED");
+ break;
+ case HTTP_CONN_STATE_TRANSPORT_CLOSED:
+ s = format (s, "TRANSPORT_CLOSED");
+ break;
+ case HTTP_CONN_STATE_APP_CLOSED:
+ s = format (s, "APP_CLOSED");
+ break;
+ case HTTP_CONN_STATE_CLOSED:
+ s = format (s, "CLOSED");
+ break;
+ }
+
+ return s;
+}
+
+static u8 *
+format_http_transport_connection (u8 *s, va_list *args)
+{
+ u32 tc_index = va_arg (*args, u32);
+ u32 thread_index = va_arg (*args, u32);
+ u32 verbose = va_arg (*args, u32);
+ http_conn_t *hc;
+
+ hc = http_conn_get_w_thread (tc_index, thread_index);
+
+ s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_http_connection, hc);
+ if (verbose)
+ {
+ s =
+ format (s, "%-" SESSION_CLI_STATE_LEN "U", format_http_conn_state, hc);
+ if (verbose > 1)
+ s = format (s, "\n");
+ }
+
+ return s;
+}
+
+static u8 *
+format_http_transport_listener (u8 *s, va_list *args)
+{
+ u32 tc_index = va_arg (*args, u32);
+ u32 __clib_unused thread_index = va_arg (*args, u32);
+ u32 __clib_unused verbose = va_arg (*args, u32);
+ http_conn_t *lhc = http_listener_get (tc_index);
+
+ s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_http_listener, lhc);
+ if (verbose)
+ s =
+ format (s, "%-" SESSION_CLI_STATE_LEN "U", format_http_conn_state, lhc);
+ return s;
+}
+
+static const transport_proto_vft_t http_proto = {
+ .enable = http_transport_enable,
+ .connect = http_transport_connect,
+ .start_listen = http_start_listen,
+ .stop_listen = http_stop_listen,
+ .close = http_transport_close,
+ .custom_tx = http_app_tx_callback,
+ .get_connection = http_transport_get_connection,
+ .get_listener = http_transport_get_listener,
+ .get_transport_endpoint = http_transport_get_endpoint,
+ .format_connection = format_http_transport_connection,
+ .format_listener = format_http_transport_listener,
+ .transport_options = {
+ .name = "http",
+ .short_name = "H",
+ .tx_type = TRANSPORT_TX_INTERNAL,
+ .service_type = TRANSPORT_SERVICE_APP,
+ },
+};
+
+static clib_error_t *
+http_transport_init (vlib_main_t *vm)
+{
+ http_main_t *hm = &http_main;
+
+ transport_register_protocol (TRANSPORT_PROTO_HTTP, &http_proto,
+ FIB_PROTOCOL_IP4, ~0);
+ transport_register_protocol (TRANSPORT_PROTO_HTTP, &http_proto,
+ FIB_PROTOCOL_IP6, ~0);
+
+ /* Default values, configurable via startup conf */
+ hm->add_seg_size = 256 << 20;
+ hm->first_seg_size = 32 << 20;
+ hm->fifo_size = 512 << 10;
+
+ return 0;
+}
+
+VLIB_INIT_FUNCTION (http_transport_init);
+
+static clib_error_t *
+http_config_fn (vlib_main_t *vm, unformat_input_t *input)
+{
+ http_main_t *hm = &http_main;
+ uword mem_sz;
+
+ while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
+ {
+ if (unformat (input, "first-segment-size %U", unformat_memory_size,
+ &mem_sz))
+ {
+ hm->first_seg_size = clib_max (mem_sz, 1 << 20);
+ if (hm->first_seg_size != mem_sz)
+ clib_warning ("first seg size too small %u", mem_sz);
+ }
+ else if (unformat (input, "add-segment-size %U", unformat_memory_size,
+ &mem_sz))
+ {
+ hm->add_seg_size = clib_max (mem_sz, 1 << 20);
+ if (hm->add_seg_size != mem_sz)
+ clib_warning ("add seg size too small %u", mem_sz);
+ }
+ else if (unformat (input, "fifo-size %U", unformat_memory_size, &mem_sz))
+ {
+ hm->fifo_size = clib_clamp (mem_sz, 4 << 10, 2 << 30);
+ if (hm->fifo_size != mem_sz)
+ clib_warning ("invalid fifo size %lu", mem_sz);
+ }
+ else
+ return clib_error_return (0, "unknown input `%U'",
+ format_unformat_error, input);
+ }
+ return 0;
+}
+
+VLIB_CONFIG_FUNCTION (http_config_fn, "http");
+
+VLIB_PLUGIN_REGISTER () = {
+ .version = VPP_BUILD_VER,
+ .description = "Hypertext Transfer Protocol (HTTP)",
+ .default_disabled = 0,
+};
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/plugins/http/http.h b/src/plugins/http/http.h
new file mode 100644
index 00000000000..c9912dd6db8
--- /dev/null
+++ b/src/plugins/http/http.h
@@ -0,0 +1,288 @@
+/*
+ * Copyright (c) 2022 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef SRC_PLUGINS_HTTP_HTTP_H_
+#define SRC_PLUGINS_HTTP_HTTP_H_
+
+#include <vnet/plugin/plugin.h>
+#include <vpp/app/version.h>
+
+#include <vppinfra/time_range.h>
+
+#include <vnet/session/application_interface.h>
+#include <vnet/session/application.h>
+#include <http/http_buffer.h>
+
+#define HTTP_DEBUG 0
+
+#if HTTP_DEBUG
+#define HTTP_DBG(_lvl, _fmt, _args...) \
+ if (_lvl <= HTTP_DEBUG) \
+ clib_warning (_fmt, ##_args)
+#else
+#define HTTP_DBG(_lvl, _fmt, _args...)
+#endif
+
+typedef struct http_conn_id_
+{
+ union
+ {
+ session_handle_t app_session_handle;
+ u32 parent_app_api_ctx;
+ };
+ session_handle_t tc_session_handle;
+ u32 parent_app_wrk_index;
+} http_conn_id_t;
+
+STATIC_ASSERT (sizeof (http_conn_id_t) <= TRANSPORT_CONN_ID_LEN,
+ "ctx id must be less than TRANSPORT_CONN_ID_LEN");
+
+typedef enum http_conn_state_
+{
+ HTTP_CONN_STATE_LISTEN,
+ HTTP_CONN_STATE_CONNECTING,
+ HTTP_CONN_STATE_ESTABLISHED,
+ HTTP_CONN_STATE_TRANSPORT_CLOSED,
+ HTTP_CONN_STATE_APP_CLOSED,
+ HTTP_CONN_STATE_CLOSED
+} http_conn_state_t;
+
+typedef enum http_state_
+{
+ HTTP_STATE_IDLE = 0,
+ HTTP_STATE_WAIT_APP_METHOD,
+ HTTP_STATE_WAIT_CLIENT_METHOD,
+ HTTP_STATE_WAIT_SERVER_REPLY,
+ HTTP_STATE_WAIT_APP_REPLY,
+ HTTP_STATE_CLIENT_IO_MORE_DATA,
+ HTTP_STATE_APP_IO_MORE_DATA,
+ HTTP_N_STATES,
+} http_state_t;
+
+typedef enum http_req_method_
+{
+ HTTP_REQ_GET = 0,
+ HTTP_REQ_POST,
+} http_req_method_t;
+
+typedef enum http_msg_type_
+{
+ HTTP_MSG_REQUEST,
+ HTTP_MSG_REPLY
+} http_msg_type_t;
+
+#define foreach_http_content_type \
+ _ (APP_7Z, ".7z", "application / x - 7z - compressed") \
+ _ (APP_DOC, ".doc", "application / msword") \
+ _ (APP_DOCX, ".docx", \
+ "application / vnd.openxmlformats - " \
+ "officedocument.wordprocessingml.document") \
+ _ (APP_EPUB, ".epub", "application / epub + zip") \
+ _ (APP_FONT, ".eot", "application / vnd.ms - fontobject") \
+ _ (APP_JAR, ".jar", "application / java - archive") \
+ _ (APP_JSON, ".json", "application / json") \
+ _ (APP_JSON_LD, ".jsonld", "application / ld + json") \
+ _ (APP_MPKG, ".mpkg", "application / vnd.apple.installer + xml") \
+ _ (APP_ODP, ".odp", "application / vnd.oasis.opendocument.presentation") \
+ _ (APP_ODS, ".ods", "application / vnd.oasis.opendocument.spreadsheet") \
+ _ (APP_ODT, ".odt", "application / vnd.oasis.opendocument.text") \
+ _ (APP_OGX, ".ogx", "application / ogg") \
+ _ (APP_PDF, ".pdf", "application / pdf") \
+ _ (APP_PHP, ".php", "application / x - httpd - php") \
+ _ (APP_PPT, ".ppt", "application / vnd.ms - powerpoint") \
+ _ (APP_PPTX, ".pptx", "application / vnd.ms - powerpoint") \
+ _ (APP_RAR, ".rar", "application / vnd.rar") \
+ _ (APP_RTF, ".rtf", "application / rtf") \
+ _ (APP_SH, ".sh", "application / x - sh") \
+ _ (APP_TAR, ".tar", "application / x - tar") \
+ _ (APP_VSD, ".vsd", "application / vnd.visio") \
+ _ (APP_XHTML, ".xhtml", "application / xhtml + xml") \
+ _ (APP_XLS, ".xls", "application / vnd.ms - excel") \
+ _ (APP_XML, ".xml", "application / xml") \
+ _ (APP_XSLX, ".xlsx", \
+ "application / vnd.openxmlformats - officedocument.spreadsheetml.sheet") \
+ _ (APP_XUL, ".xul", "application / vnd.mozilla.xul + xml") \
+ _ (APP_ZIP, ".zip", "application / zip") \
+ _ (AUDIO_AAC, ".aac", "audio / aac") \
+ _ (AUDIO_CD, ".cda", "application / x - cdf") \
+ _ (AUDIO_WAV, ".wav", "audio / wav") \
+ _ (AUDIO_WEBA, ".weba", "audio / webm") \
+ _ (AUDO_MIDI, ".midi", "audio / midi") \
+ _ (AUDO_MID, ".mid", "audo / midi") \
+ _ (AUDO_MP3, ".mp3", "audio / mpeg") \
+ _ (AUDO_OGA, ".oga", "audio / ogg") \
+ _ (AUDO_OPUS, ".opus", "audio / opus") \
+ _ (APP_OCTET_STREAM, ".bin", "application / octet - stream") \
+ _ (BZIP2, ".bz2", "application / x - bzip2") \
+ _ (BZIP, ".bz", "application / x - bzip") \
+ _ (FONT_OTF, ".otf", "font / otf") \
+ _ (FONT_TTF, ".ttf", "font / ttf") \
+ _ (FONT_WOFF2, ".woff2", "font / woff2") \
+ _ (FONT_WOFF, ".woff", "font / woff") \
+ _ (GZIP, ".gz", "application / gzip") \
+ _ (IMAGE_AVIF, ".avif", "image / avif") \
+ _ (IMAGE_BMP, ".bmp", "image / bmp") \
+ _ (IMAGE_GIF, ".gif", "image / gif") \
+ _ (IMAGE_ICON, ".ico", "image / vnd.microsoft.icon") \
+ _ (IMAGE_JPEG, ".jpeg", "image / jpeg") \
+ _ (IMAGE_JPG, ".jpg", "image / jpeg") \
+ _ (IMAGE_PNG, ".png", "image / png") \
+ _ (IMAGE_SVG, ".svg", "image / svg + xml") \
+ _ (IMAGE_TIFF, ".tiff", "image / tiff") \
+ _ (IMAGE_TIF, ".tif", "image / tiff") \
+ _ (IMAGE_WEBP, ".webp", "image / webp") \
+ _ (SCRIPT_CSH, ".csh", "application / x - csh") \
+ _ (TEXT_ABIWORD, ".abw", "application / x - abiword") \
+ _ (TEXT_ARCHIVE, ".arc", "application / x - freearc") \
+ _ (TEXT_AZW, ".azw", "application / vnd.amazon.ebook") \
+ _ (TEXT_CALENDAR, ".ics", "text / calendar") \
+ _ (TEXT_CSS, ".css", "text / css") \
+ _ (TEXT_CSV, ".csv", "text / csv") \
+ _ (TEXT_HTM, ".htm", "text / html") \
+ _ (TEXT_HTML, ".html", "text / html") \
+ _ (TEXT_JS, ".js", "text / javascript") \
+ _ (TEXT_MJS, ".mjs", "text / javascript") \
+ _ (TEXT_PLAIN, ".txt", "text / plain") \
+ _ (VIDEO_3GP2, ".3g2", "video / 3gpp2") \
+ _ (VIDEO_3GP, ".3gp", "video / 3gpp") \
+ _ (VIDEO_AVI, ".avi", "video / x - msvideo") \
+ _ (VIDEO_MP4, ".mp4", "video / mp4") \
+ _ (VIDEO_MPEG, ".mpeg", "video / mpeg") \
+ _ (VIDEO_OGG, ".ogv", "video / ogg") \
+ _ (VIDEO_TS, ".ts", "video / mp2t") \
+ _ (VIDEO_WEBM, ".webm", "video / webm")
+
+typedef enum http_content_type_
+{
+#define _(s, ext, str) HTTP_CONTENT_##s,
+ foreach_http_content_type
+#undef _
+} http_content_type_t;
+
+#define foreach_http_status_code \
+ _ (200, OK, "200 OK") \
+ _ (301, MOVED, "301 Moved Permanently") \
+ _ (400, BAD_REQUEST, "400 Bad Request") \
+ _ (404, NOT_FOUND, "404 Not Found") \
+ _ (405, METHOD_NOT_ALLOWED, "405 Method Not Allowed") \
+ _ (500, INTERNAL_ERROR, "500 Internal Server Error")
+
+typedef enum http_status_code_
+{
+#define _(c, s, str) HTTP_STATUS_##s,
+ foreach_http_status_code
+#undef _
+ HTTP_N_STATUS
+} http_status_code_t;
+
+typedef enum http_msg_data_type_
+{
+ HTTP_MSG_DATA_INLINE,
+ HTTP_MSG_DATA_PTR
+} http_msg_data_type_t;
+
+typedef struct http_msg_data_
+{
+ http_msg_data_type_t type;
+ u64 len;
+ u8 data[0];
+} http_msg_data_t;
+
+typedef struct http_msg_
+{
+ http_msg_type_t type;
+ union
+ {
+ http_req_method_t method_type;
+ http_status_code_t code;
+ };
+ http_content_type_t content_type;
+ http_msg_data_t data;
+} http_msg_t;
+
+typedef struct http_tc_
+{
+ union
+ {
+ transport_connection_t connection;
+ http_conn_id_t c_http_conn_id;
+ };
+#define h_tc_session_handle c_http_conn_id.tc_session_handle
+#define h_pa_wrk_index c_http_conn_id.parent_app_wrk_index
+#define h_pa_session_handle c_http_conn_id.app_session_handle
+#define h_pa_app_api_ctx c_http_conn_id.parent_app_api_ctx
+#define h_hc_index connection.c_index
+
+ http_conn_state_t state;
+ u32 timer_handle;
+ u8 *app_name;
+
+ /*
+ * Current request
+ */
+ http_state_t http_state;
+ http_req_method_t method;
+ u8 *rx_buf;
+ u32 rx_buf_offset;
+ http_buffer_t tx_buf;
+ u32 to_recv;
+ u32 bytes_dequeued;
+} http_conn_t;
+
+typedef struct http_worker_
+{
+ http_conn_t *conn_pool;
+} http_worker_t;
+
+typedef struct http_main_
+{
+ http_worker_t *wrk;
+ http_conn_t *listener_pool;
+ u32 app_index;
+
+ clib_timebase_t timebase;
+
+ /*
+ * Runtime config
+ */
+ u8 debug_level;
+
+ /*
+ * Config
+ */
+ u64 first_seg_size;
+ u64 add_seg_size;
+ u32 fifo_size;
+} http_main_t;
+
+static inline int
+http_state_is_tx_valid (http_conn_t *hc)
+{
+ http_state_t state = hc->http_state;
+ return (state == HTTP_STATE_APP_IO_MORE_DATA ||
+ state == HTTP_STATE_CLIENT_IO_MORE_DATA ||
+ state == HTTP_STATE_WAIT_APP_REPLY ||
+ state == HTTP_STATE_WAIT_APP_METHOD);
+}
+
+#endif /* SRC_PLUGINS_HTTP_HTTP_H_ */
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/plugins/http/http_buffer.c b/src/plugins/http/http_buffer.c
new file mode 100644
index 00000000000..f3dc308dbf8
--- /dev/null
+++ b/src/plugins/http/http_buffer.c
@@ -0,0 +1,219 @@
+/*
+ * Copyright (c) 2022 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <http/http_buffer.h>
+#include <http/http.h>
+
+static http_buffer_vft_t buf_vfts[HTTP_BUFFER_PTR + 1];
+
+#define HTTP_BUFFER_REGISTER_VFT(type, vft) \
+ static void __attribute__ ((constructor)) http_buf_init_##type (void) \
+ { \
+ buf_vfts[type] = vft; \
+ }
+
+typedef struct http_buffer_fifo_
+{
+ svm_fifo_t *src;
+ svm_fifo_seg_t *segs;
+ u64 len;
+ u64 offset;
+} http_buffer_fifo_t;
+
+STATIC_ASSERT (sizeof (http_buffer_fifo_t) <= HTTP_BUFFER_DATA_SZ, "buf data");
+
+static void
+buf_fifo_init (http_buffer_t *hb, void *data, u64 len)
+{
+ svm_fifo_t *f = (svm_fifo_t *) data;
+ http_buffer_fifo_t *bf;
+
+ bf = (http_buffer_fifo_t *) &hb->data;
+
+ bf->len = len;
+ bf->offset = 0;
+ bf->src = f;
+ bf->segs = 0;
+}
+
+static void
+buf_fifo_free (http_buffer_t *hb)
+{
+ http_buffer_fifo_t *bf = (http_buffer_fifo_t *) &hb->data;
+
+ bf->src = 0;
+ vec_free (bf->segs);
+}
+
+static svm_fifo_seg_t *
+buf_fifo_get_segs (http_buffer_t *hb, u32 max_len, u32 *n_segs)
+{
+ http_buffer_fifo_t *bf = (http_buffer_fifo_t *) &hb->data;
+
+ u32 _n_segs = 5;
+ int len;
+
+ max_len = clib_min (bf->len - bf->offset, (u64) max_len);
+
+ vec_validate (bf->segs, _n_segs);
+
+ len = svm_fifo_segments (bf->src, 0, bf->segs, &_n_segs, max_len);
+ if (len < 0)
+ return 0;
+
+ *n_segs = _n_segs;
+
+ HTTP_DBG (1, "available to send %u n_segs %u", len, *n_segs);
+
+ return bf->segs;
+}
+
+static u32
+buf_fifo_drain (http_buffer_t *hb, u32 len)
+{
+ http_buffer_fifo_t *bf = (http_buffer_fifo_t *) &hb->data;
+
+ bf->offset += len;
+ svm_fifo_dequeue_drop (bf->src, len);
+ HTTP_DBG (1, "drained %u len %u offset %u", len, bf->len, bf->offset);
+
+ return len;
+}
+
+static u8
+buf_fifo_is_drained (http_buffer_t *hb)
+{
+ http_buffer_fifo_t *bf = (http_buffer_fifo_t *) &hb->data;
+
+ ASSERT (bf->offset <= bf->len);
+ return (bf->offset == bf->len);
+}
+
+const static http_buffer_vft_t buf_fifo_vft = {
+ .init = buf_fifo_init,
+ .free = buf_fifo_free,
+ .get_segs = buf_fifo_get_segs,
+ .drain = buf_fifo_drain,
+ .is_drained = buf_fifo_is_drained,
+};
+
+HTTP_BUFFER_REGISTER_VFT (HTTP_BUFFER_FIFO, buf_fifo_vft);
+
+typedef struct http_buffer_ptr_
+{
+ svm_fifo_seg_t *segs;
+ svm_fifo_t *f;
+} http_buffer_ptr_t;
+
+STATIC_ASSERT (sizeof (http_buffer_ptr_t) <= HTTP_BUFFER_DATA_SZ, "buf data");
+
+static void
+buf_ptr_init (http_buffer_t *hb, void *data, u64 len)
+{
+ svm_fifo_t *f = (svm_fifo_t *) data;
+ http_buffer_ptr_t *bf;
+ uword ptr;
+ int rv;
+
+ bf = (http_buffer_ptr_t *) &hb->data;
+
+ /* Peek the pointer, do not drain the fifo until done with transfer */
+ rv = svm_fifo_peek (f, 0, sizeof (ptr), (u8 *) &ptr);
+ ASSERT (rv == sizeof (ptr));
+
+ bf->f = f;
+ bf->segs = 0;
+ vec_validate (bf->segs, 1);
+
+ bf->segs[0].data = uword_to_pointer (ptr, u8 *);
+ bf->segs[0].len = len;
+
+ bf->segs[1] = bf->segs[0];
+}
+
+static void
+buf_ptr_free (http_buffer_t *hb)
+{
+ http_buffer_ptr_t *bf = (http_buffer_ptr_t *) &hb->data;
+
+ bf->f = 0;
+ vec_free (bf->segs);
+}
+
+static svm_fifo_seg_t *
+buf_ptr_get_segs (http_buffer_t *hb, u32 max_len, u32 *n_segs)
+{
+ http_buffer_ptr_t *bf = (http_buffer_ptr_t *) &hb->data;
+
+ *n_segs = 1;
+ bf->segs[1].len = clib_min (bf->segs[0].len, max_len);
+
+ return &bf->segs[1];
+}
+
+static u32
+buf_ptr_drain (http_buffer_t *hb, u32 len)
+{
+ http_buffer_ptr_t *bf = (http_buffer_ptr_t *) &hb->data;
+
+ ASSERT (bf->segs[0].len >= len);
+
+ bf->segs[1].data += len;
+ bf->segs[0].len -= len;
+
+ HTTP_DBG (1, "drained %u left %u", len, bf->segs[1].len);
+
+ if (!bf->segs[0].len)
+ {
+ svm_fifo_dequeue_drop (bf->f, sizeof (uword));
+ return sizeof (uword);
+ }
+
+ return 0;
+}
+
+static u8
+buf_ptr_is_drained (http_buffer_t *hb)
+{
+ http_buffer_ptr_t *bf = (http_buffer_ptr_t *) &hb->data;
+
+ return (bf->segs[0].len == 0);
+}
+
+const static http_buffer_vft_t buf_ptr_vft = {
+ .init = buf_ptr_init,
+ .free = buf_ptr_free,
+ .get_segs = buf_ptr_get_segs,
+ .drain = buf_ptr_drain,
+ .is_drained = buf_ptr_is_drained,
+};
+
+HTTP_BUFFER_REGISTER_VFT (HTTP_BUFFER_PTR, buf_ptr_vft);
+
+void
+http_buffer_init (http_buffer_t *hb, http_buffer_type_t type, svm_fifo_t *f,
+ u64 data_len)
+{
+ hb->vft = &buf_vfts[type];
+ hb->vft->init (hb, f, data_len);
+}
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/plugins/http/http_buffer.h b/src/plugins/http/http_buffer.h
new file mode 100644
index 00000000000..1140be42d6e
--- /dev/null
+++ b/src/plugins/http/http_buffer.h
@@ -0,0 +1,82 @@
+/*
+ * Copyright (c) 2022 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef SRC_PLUGINS_HTTP_HTTP_BUFFER_H_
+#define SRC_PLUGINS_HTTP_HTTP_BUFFER_H_
+
+#include <svm/svm_fifo.h>
+
+#define HTTP_BUFFER_DATA_SZ 32
+
+typedef enum http_buffer_type_
+{
+ HTTP_BUFFER_FIFO,
+ HTTP_BUFFER_PTR,
+} http_buffer_type_t;
+
+typedef struct http_buffer_vft_ http_buffer_vft_t;
+
+typedef struct http_buffer_
+{
+ http_buffer_vft_t *vft;
+ u8 data[HTTP_BUFFER_DATA_SZ];
+} http_buffer_t;
+
+struct http_buffer_vft_
+{
+ void (*init) (http_buffer_t *, void *data, u64 len);
+ void (*free) (http_buffer_t *);
+ svm_fifo_seg_t *(*get_segs) (http_buffer_t *, u32 max_len, u32 *n_segs);
+ u32 (*drain) (http_buffer_t *, u32 len);
+ u8 (*is_drained) (http_buffer_t *);
+};
+
+void http_buffer_init (http_buffer_t *hb, http_buffer_type_t type,
+ svm_fifo_t *f, u64 data_len);
+
+static inline void
+http_buffer_free (http_buffer_t *hb)
+{
+ if (hb->vft)
+ hb->vft->free (hb);
+}
+
+static inline svm_fifo_seg_t *
+http_buffer_get_segs (http_buffer_t *hb, u32 max_len, u32 *n_segs)
+{
+ return hb->vft->get_segs (hb, max_len, n_segs);
+}
+
+static inline u32
+http_buffer_drain (http_buffer_t *hb, u32 len)
+{
+ return hb->vft->drain (hb, len);
+}
+
+static inline u8
+http_buffer_is_drained (http_buffer_t *hb)
+{
+ return hb->vft->is_drained (hb);
+}
+
+#endif /* SRC_PLUGINS_HTTP_HTTP_BUFFER_H_ */
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/plugins/http/http_timer.c b/src/plugins/http/http_timer.c
new file mode 100644
index 00000000000..42fe69076fe
--- /dev/null
+++ b/src/plugins/http/http_timer.c
@@ -0,0 +1,91 @@
+/*
+ * Copyright (c) 2022 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <http/http_timer.h>
+#include <vnet/session/session.h>
+
+http_tw_ctx_t http_tw_ctx;
+
+static void
+http_timer_process_expired_cb (u32 *expired_timers)
+{
+ http_tw_ctx_t *twc = &http_tw_ctx;
+ u32 hs_handle;
+ int i;
+
+ for (i = 0; i < vec_len (expired_timers); i++)
+ {
+ /* Get session handle. The first bit is the timer id */
+ hs_handle = expired_timers[i] & 0x7FFFFFFF;
+ session_send_rpc_evt_to_thread (hs_handle >> 24, twc->cb_fn,
+ uword_to_pointer (hs_handle, void *));
+ }
+}
+
+static uword
+http_timer_process (vlib_main_t *vm, vlib_node_runtime_t *rt, vlib_frame_t *f)
+{
+ http_tw_ctx_t *twc = &http_tw_ctx;
+ f64 now, timeout = 1.0;
+ uword *event_data = 0;
+ uword __clib_unused event_type;
+
+ while (1)
+ {
+ vlib_process_wait_for_event_or_clock (vm, timeout);
+ now = vlib_time_now (vm);
+ event_type = vlib_process_get_events (vm, (uword **) &event_data);
+
+ /* expire timers */
+ clib_spinlock_lock (&twc->tw_lock);
+ tw_timer_expire_timers_2t_1w_2048sl (&twc->tw, now);
+ clib_spinlock_unlock (&twc->tw_lock);
+
+ vec_reset_length (event_data);
+ }
+ return 0;
+}
+
+VLIB_REGISTER_NODE (http_timer_process_node) = {
+ .function = http_timer_process,
+ .type = VLIB_NODE_TYPE_PROCESS,
+ .name = "http-timer-process",
+ .state = VLIB_NODE_STATE_DISABLED,
+};
+
+void
+http_timers_init (vlib_main_t *vm, http_conn_timeout_fn *cb_fn)
+{
+ http_tw_ctx_t *twc = &http_tw_ctx;
+ vlib_node_t *n;
+
+ tw_timer_wheel_init_2t_1w_2048sl (&twc->tw, http_timer_process_expired_cb,
+ 1.0 /* timer interval */, ~0);
+ clib_spinlock_init (&twc->tw_lock);
+ twc->cb_fn = cb_fn;
+
+ vlib_node_set_state (vm, http_timer_process_node.index,
+ VLIB_NODE_STATE_POLLING);
+ n = vlib_get_node (vm, http_timer_process_node.index);
+ vlib_start_process (vm, n->runtime_index);
+}
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/plugins/http/http_timer.h b/src/plugins/http/http_timer.h
new file mode 100644
index 00000000000..eec5a4595fe
--- /dev/null
+++ b/src/plugins/http/http_timer.h
@@ -0,0 +1,91 @@
+/*
+ * Copyright (c) 2022 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef SRC_PLUGINS_HTTP_HTTP_TIMER_H_
+#define SRC_PLUGINS_HTTP_HTTP_TIMER_H_
+
+#include <http/http.h>
+#include <vppinfra/tw_timer_2t_1w_2048sl.h>
+
+#define HTTP_CONN_TIMEOUT 60
+
+typedef void (http_conn_timeout_fn) (void *);
+
+typedef struct http_tw_ctx_
+{
+ tw_timer_wheel_2t_1w_2048sl_t tw;
+ clib_spinlock_t tw_lock;
+ http_conn_timeout_fn *cb_fn;
+} http_tw_ctx_t;
+
+extern http_tw_ctx_t http_tw_ctx;
+
+void http_timers_init (vlib_main_t *vm, http_conn_timeout_fn *cb_fn);
+
+static inline void
+http_conn_timer_start (http_conn_t *hc)
+{
+ http_tw_ctx_t *twc = &http_tw_ctx;
+ u32 hs_handle;
+ u64 timeout;
+
+ timeout = HTTP_CONN_TIMEOUT;
+ hs_handle = hc->c_thread_index << 24 | hc->c_c_index;
+
+ clib_spinlock_lock (&twc->tw_lock);
+ hc->timer_handle =
+ tw_timer_start_2t_1w_2048sl (&twc->tw, hs_handle, 0, timeout);
+ clib_spinlock_unlock (&twc->tw_lock);
+}
+
+static inline void
+http_conn_timer_stop (http_conn_t *hc)
+{
+ http_tw_ctx_t *twc = &http_tw_ctx;
+
+ if (hc->timer_handle == ~0)
+ return;
+
+ clib_spinlock_lock (&twc->tw_lock);
+ tw_timer_stop_2t_1w_2048sl (&twc->tw, hc->timer_handle);
+ hc->timer_handle = ~0;
+ clib_spinlock_unlock (&twc->tw_lock);
+}
+
+static inline void
+http_conn_timer_update (http_conn_t *hc)
+{
+ http_tw_ctx_t *twc = &http_tw_ctx;
+ u64 timeout;
+
+ if (hc->timer_handle == ~0)
+ return;
+
+ timeout = HTTP_CONN_TIMEOUT;
+
+ clib_spinlock_lock (&twc->tw_lock);
+ tw_timer_update_2t_1w_2048sl (&twc->tw, hc->timer_handle, timeout);
+ clib_spinlock_unlock (&twc->tw_lock);
+}
+
+#endif /* SRC_PLUGINS_HTTP_HTTP_TIMER_H_ */
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */