http: add http protocol plugin
Basic HTTP/1.1 server side implementation. Type: feature Signed-off-by: Florin Coras <fcoras@cisco.com> Change-Id: I06bddaf7f11e28db802b4cd7ef8160c78cb019b6
This commit is contained in:
@ -773,6 +773,11 @@ I: hsi
|
||||
M: Florin Coras <fcoras@cisco.com>
|
||||
F: src/plugins/hsi/
|
||||
|
||||
Plugin - HTTP
|
||||
I: http
|
||||
M: Florin Coras <fcoras@cisco.com>
|
||||
F: src/plugins/http
|
||||
|
||||
cJSON
|
||||
I: cjson
|
||||
M: Ole Troan <ot@cisco.com>
|
||||
|
18
src/plugins/http/CMakeLists.txt
Normal file
18
src/plugins/http/CMakeLists.txt
Normal file
@ -0,0 +1,18 @@
|
||||
# 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_timer.c
|
||||
)
|
1002
src/plugins/http/http.c
Normal file
1002
src/plugins/http/http.c
Normal file
File diff suppressed because it is too large
Load Diff
194
src/plugins/http/http.h
Normal file
194
src/plugins/http/http.h
Normal file
@ -0,0 +1,194 @@
|
||||
/*
|
||||
* 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>
|
||||
|
||||
#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_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_req_state_
|
||||
{
|
||||
HTTP_REQ_STATE_WAIT_METHOD,
|
||||
HTTP_REQ_STATE_WAIT_APP,
|
||||
HTTP_REQ_STATE_SEND_MORE_DATA,
|
||||
HTTP_REQ_N_STATES,
|
||||
} http_req_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 \
|
||||
_ (TEXT_HTML, "text/html") \
|
||||
_ (TEXT_CSS, "text/css") \
|
||||
_ (TEXT_JS, "text/javascript") \
|
||||
_ (TEXT_JSON, "application/json")
|
||||
|
||||
typedef enum http_content_type_
|
||||
{
|
||||
#define _(s, str) HTTP_CONTENT_##s,
|
||||
foreach_http_content_type
|
||||
#undef _
|
||||
} http_content_type_t;
|
||||
|
||||
#define foreach_http_status_code \
|
||||
_ (200, OK, "200 OK") \
|
||||
_ (400, BAD_REQUEST, "400 Bad Request") \
|
||||
_ (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 struct http_msg_data_
|
||||
{
|
||||
http_content_type_t content_type;
|
||||
u32 len;
|
||||
u32 offset;
|
||||
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_msg_data_t data;
|
||||
} http_msg_t;
|
||||
|
||||
typedef struct http_buffer_
|
||||
{
|
||||
svm_fifo_t *src;
|
||||
svm_fifo_seg_t *segs;
|
||||
u32 len;
|
||||
u32 cur_seg;
|
||||
u32 offset;
|
||||
} http_buffer_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_hc_index connection.c_index
|
||||
|
||||
http_conn_state_t state;
|
||||
u32 timer_handle;
|
||||
|
||||
/*
|
||||
* Current request
|
||||
*/
|
||||
http_req_state_t req_state;
|
||||
http_req_method_t method;
|
||||
u8 *rx_buf;
|
||||
u32 rx_buf_offset;
|
||||
http_buffer_t tx_buf;
|
||||
} 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_ctx_pool;
|
||||
u32 app_index;
|
||||
|
||||
clib_timebase_t timebase;
|
||||
|
||||
/*
|
||||
* Runtime config
|
||||
*/
|
||||
u8 debug_level;
|
||||
|
||||
/*
|
||||
* Config
|
||||
*/
|
||||
u64 first_seg_size;
|
||||
u32 fifo_size;
|
||||
} http_main_t;
|
||||
|
||||
#endif /* SRC_PLUGINS_HTTP_HTTP_H_ */
|
||||
|
||||
/*
|
||||
* fd.io coding-style-patch-verification: ON
|
||||
*
|
||||
* Local Variables:
|
||||
* eval: (c-set-style "gnu")
|
||||
* End:
|
||||
*/
|
91
src/plugins/http/http_timer.c
Normal file
91
src/plugins/http/http_timer.c
Normal file
@ -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:
|
||||
*/
|
91
src/plugins/http/http_timer.h
Normal file
91
src/plugins/http/http_timer.h
Normal file
@ -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:
|
||||
*/
|
@ -2000,7 +2000,7 @@ session_main_init (vlib_main_t * vm)
|
||||
smm->poll_main = 0;
|
||||
smm->use_private_rx_mqs = 0;
|
||||
smm->no_adaptive = 0;
|
||||
smm->last_transport_proto_type = TRANSPORT_PROTO_SRTP;
|
||||
smm->last_transport_proto_type = TRANSPORT_PROTO_HTTP;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -21,10 +21,8 @@
|
||||
#include <vnet/tcp/tcp_debug.h>
|
||||
#include <vppinfra/bihash_24_8.h>
|
||||
|
||||
|
||||
#define TRANSPORT_MAX_HDRS_LEN 140 /* Max number of bytes for headers */
|
||||
|
||||
|
||||
typedef enum transport_dequeue_type_
|
||||
{
|
||||
TRANSPORT_TX_PEEK, /**< reliable transport protos */
|
||||
@ -166,7 +164,8 @@ STATIC_ASSERT (sizeof (transport_connection_t) <= 128,
|
||||
_ (TLS, "tls", "J") \
|
||||
_ (QUIC, "quic", "Q") \
|
||||
_ (DTLS, "dtls", "D") \
|
||||
_ (SRTP, "srtp", "R")
|
||||
_ (SRTP, "srtp", "R") \
|
||||
_ (HTTP, "http", "H")
|
||||
|
||||
typedef enum _transport_proto
|
||||
{
|
||||
|
Reference in New Issue
Block a user