u64 data_len;
u64 data_offset;
u32 vpp_session_index;
+ union
+ {
+ /** threshold after which connection is closed */
+ f64 close_threshold;
+ /** rate at which accepted sessions are marked for random close */
+ u32 close_rate;
+ };
u8 *uri;
} hts_session_t;
+typedef struct hts_listen_cfg_
+{
+ u8 *uri;
+ u32 vrf;
+ f64 rnd_close;
+ u8 is_del;
+} hts_listen_cfg_t;
+
typedef struct hs_main_
{
hts_session_t **sessions;
u8 debug_level;
u8 no_zc;
u8 *default_uri;
+ u32 seed;
} hts_main_t;
static hts_main_t hts_main;
hts_main_t *htm = &hts_main;
u32 thread = hs->thread_index;
+ if (htm->debug_level > 0)
+ clib_warning ("Freeing session %u", hs->session_index);
+
if (CLIB_DEBUG)
clib_memset (hs, 0xfa, sizeof (*hs));
pool_put (htm->sessions[thread], hs);
}
+static void
+hts_disconnect_transport (hts_session_t *hs)
+{
+ vnet_disconnect_args_t _a = { 0 }, *a = &_a;
+ hts_main_t *htm = &hts_main;
+ session_t *ts;
+
+ if (htm->debug_level > 0)
+ clib_warning ("Actively closing session %u", hs->session_index);
+
+ ts = session_get (hs->vpp_session_index, hs->thread_index);
+ a->handle = session_handle (ts);
+ a->app_index = htm->app_index;
+ vnet_disconnect_session (a);
+}
+
static void
hts_session_tx_zc (hts_session_t *hs, session_t *ts)
{
hts_session_tx_zc (hs, ts);
else
hts_session_tx_no_zc (hs, ts);
+
+ if (hs->close_threshold > 0)
+ {
+ if ((f64) hs->data_offset / hs->data_len > hs->close_threshold)
+ hts_disconnect_transport (hs);
+ }
}
static void
msg.type = HTTP_MSG_REPLY;
msg.code = status;
- msg.content_type = HTTP_CONTENT_TEXT_HTML;
+ msg.content_type = HTTP_CONTENT_APP_OCTET_STREAM;
msg.data.type = HTTP_MSG_DATA_INLINE;
msg.data.len = hs->data_len;
}
static int
-try_test_file (hts_session_t *hs, u8 *request)
+try_test_file (hts_session_t *hs, u8 *target)
{
char *test_str = "test_file";
hts_main_t *htm = &hts_main;
uword file_size;
int rc = 0;
- if (memcmp (request, test_str, clib_strnlen (test_str, 9)))
+ if (memcmp (target, test_str, clib_strnlen (test_str, 9)))
return -1;
- unformat_init_vector (&input, vec_dup (request));
+ unformat_init_vector (&input, vec_dup (target));
if (!unformat (&input, "test_file_%U", unformat_memory_size, &file_size))
{
rc = -1;
clib_warning ("Requested file size %U", format_memory_size, file_size);
hs->data_len = file_size;
+ hs->data_offset = 0;
+
+ if (hs->close_threshold > 0)
+ {
+ /* Disconnect if the header is already enough to fill the quota */
+ if ((f64) 30 / hs->data_len > hs->close_threshold)
+ {
+ hts_disconnect_transport (hs);
+ goto done;
+ }
+ }
hts_start_send_data (hs, HTTP_STATUS_OK);
static int
hts_ts_rx_callback (session_t *ts)
{
+ hts_main_t *htm = &hts_main;
hts_session_t *hs;
- u8 *request = 0;
+ u8 *target = 0;
http_msg_t msg;
int rv;
goto done;
}
- if (!msg.data.len)
+ if (msg.data.target_path_len == 0 ||
+ msg.data.target_form != HTTP_TARGET_ORIGIN_FORM)
{
hts_start_send_data (hs, HTTP_STATUS_BAD_REQUEST);
goto done;
}
- vec_validate (request, msg.data.len - 1);
- rv = svm_fifo_dequeue (ts->rx_fifo, msg.data.len, request);
+ vec_validate (target, msg.data.target_path_len - 1);
+ rv = svm_fifo_peek (ts->rx_fifo, msg.data.target_path_offset,
+ msg.data.target_path_len, target);
+ ASSERT (rv == msg.data.target_path_len);
- if (try_test_file (hs, request))
+ if (htm->debug_level)
+ clib_warning ("Request target: %v", target);
+
+ if (try_test_file (hs, target))
hts_start_send_data (hs, HTTP_STATUS_NOT_FOUND);
-done:
+ vec_free (target);
+done:
+ svm_fifo_dequeue_drop (ts->rx_fifo, msg.data.len);
return 0;
}
static int
hts_ts_accept_callback (session_t *ts)
{
- hts_session_t *hs;
+ hts_main_t *htm = &hts_main;
+ hts_session_t *hs, *lhs;
+ session_t *ls;
hs = hts_session_alloc (ts->thread_index);
hs->vpp_session_index = ts->session_index;
ts->opaque = hs->session_index;
ts->session_state = SESSION_STATE_READY;
+ /* Check if listener configured for random closes */
+ ls = listen_session_get_from_handle (ts->listener_handle);
+ lhs = hts_session_get (0, ls->opaque);
+
+ if (lhs->close_rate)
+ {
+ /* overload listener's data_offset as session counter */
+ u32 cnt = __atomic_add_fetch (&lhs->data_offset, 1, __ATOMIC_RELEASE);
+ if ((cnt % lhs->close_rate) == 0)
+ hs->close_threshold = random_f64 (&htm->seed);
+ }
+
+ if (htm->debug_level > 0)
+ clib_warning ("Accepted session %u close threshold %.2f", ts->opaque,
+ hs->close_threshold);
+
return 0;
}
}
static void
-hts_ts_disconnect_callback (session_t *s)
+hts_ts_disconnect_callback (session_t *ts)
{
hts_main_t *htm = &hts_main;
vnet_disconnect_args_t _a = { 0 }, *a = &_a;
- a->handle = session_handle (s);
+ if (htm->debug_level > 0)
+ clib_warning ("Transport closing session %u", ts->opaque);
+
+ a->handle = session_handle (ts);
a->app_index = htm->app_index;
vnet_disconnect_session (a);
}
static void
-hts_ts_reset_callback (session_t *s)
+hts_ts_reset_callback (session_t *ts)
{
hts_main_t *htm = &hts_main;
vnet_disconnect_args_t _a = { 0 }, *a = &_a;
- a->handle = session_handle (s);
+ if (htm->debug_level > 0)
+ clib_warning ("Transport reset session %u", ts->opaque);
+
+ a->handle = session_handle (ts);
a->app_index = htm->app_index;
vnet_disconnect_session (a);
}
proto == TRANSPORT_PROTO_QUIC;
}
-static clib_error_t *
-hts_listen (hts_main_t *htm, u8 *listen_uri, u8 is_del)
+static int
+hts_start_listen (hts_main_t *htm, session_endpoint_cfg_t *sep, u8 *uri,
+ f64 rnd_close)
{
- session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL;
vnet_listen_args_t _a, *a = &_a;
- u8 need_crypto, *uri;
+ u8 need_crypto;
hts_session_t *hls;
session_t *ls;
- uword *p;
+ u32 thread_index = 0;
int rv;
- uri = listen_uri ? listen_uri : htm->default_uri;
- p = hash_get_mem (htm->uri_to_handle, uri);
-
- if (is_del)
- {
- if (!p)
- return clib_error_return (0, "not listening on %v", uri);
-
- hls = hts_session_get (0, *p);
- ls = listen_session_get (hls->vpp_session_index);
-
- vnet_unlisten_args_t ua = {
- .handle = listen_session_get_handle (ls),
- .app_index = htm->app_index,
- .wrk_map_index = 0 /* default wrk */
- };
-
- hash_unset_mem (htm->uri_to_handle, uri);
-
- if (vnet_unlisten (&ua))
- return clib_error_return (0, "failed to unlisten");
-
- vec_free (hls->uri);
- hts_session_free (hls);
-
- return 0;
- }
-
- if (p)
- return clib_error_return (0, "already listening %v", uri);
-
- if (parse_uri ((char *) uri, &sep))
- return clib_error_return (0, "failed to parse uri %v", uri);
-
clib_memset (a, 0, sizeof (*a));
a->app_index = htm->app_index;
- need_crypto = hts_transport_needs_crypto (sep.transport_proto);
+ need_crypto = hts_transport_needs_crypto (sep->transport_proto);
- sep.transport_proto = TRANSPORT_PROTO_HTTP;
- clib_memcpy (&a->sep_ext, &sep, sizeof (sep));
+ sep->transport_proto = TRANSPORT_PROTO_HTTP;
+ clib_memcpy (&a->sep_ext, sep, sizeof (*sep));
if (need_crypto)
{
clib_mem_free (a->sep_ext.ext_cfg);
if (rv)
- return clib_error_return (0, "failed to listen on %v", uri);
+ return rv;
- hls = hts_session_alloc (0);
+ hls = hts_session_alloc (thread_index);
hls->uri = vec_dup (uri);
+ hls->close_rate = (f64) 1 / rnd_close;
ls = listen_session_get_from_handle (a->handle);
hls->vpp_session_index = ls->session_index;
hash_set_mem (htm->uri_to_handle, hls->uri, hls->session_index);
+ /* opaque holds index of hls, which is used in `hts_ts_accept_callback`
+ * to get back the pointer to hls */
+ ls->opaque = hls - htm->sessions[thread_index];
+
return 0;
}
+static int
+hts_stop_listen (hts_main_t *htm, u32 hls_index)
+{
+ hts_session_t *hls;
+ session_t *ls;
+
+ hls = hts_session_get (0, hls_index);
+ ls = listen_session_get (hls->vpp_session_index);
+
+ vnet_unlisten_args_t ua = {
+ .handle = listen_session_get_handle (ls),
+ .app_index = htm->app_index,
+ .wrk_map_index = 0 /* default wrk */
+ };
+
+ hash_unset_mem (htm->uri_to_handle, hls->uri);
+
+ if (vnet_unlisten (&ua))
+ return -1;
+
+ vec_free (hls->uri);
+ hts_session_free (hls);
+
+ return 0;
+}
+
+static clib_error_t *
+hts_listen (hts_main_t *htm, hts_listen_cfg_t *lcfg)
+{
+ session_endpoint_cfg_t sep = SESSION_ENDPOINT_CFG_NULL;
+ clib_error_t *error = 0;
+ u8 *uri, *uri_key;
+ uword *p;
+ int rv;
+
+ uri = lcfg->uri ? lcfg->uri : htm->default_uri;
+ uri_key = format (0, "vrf%u-%s", lcfg->vrf, uri);
+ p = hash_get_mem (htm->uri_to_handle, uri_key);
+
+ if (lcfg->is_del)
+ {
+ if (!p)
+ error = clib_error_return (0, "not listening on %v", uri);
+ else if (hts_stop_listen (htm, p[0]))
+ error = clib_error_return (0, "failed to unlisten");
+ goto done;
+ }
+
+ if (p)
+ {
+ error = clib_error_return (0, "already listening %v", uri);
+ goto done;
+ }
+
+ if (parse_uri ((char *) uri, &sep))
+ {
+ error = clib_error_return (0, "failed to parse uri %v", uri);
+ goto done;
+ }
+
+ if (lcfg->vrf)
+ {
+ fib_protocol_t fp;
+ u32 fib_index;
+
+ fp = sep.is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
+ fib_index = fib_table_find (fp, lcfg->vrf);
+ if (fib_index == ~0)
+ {
+ error = clib_error_return (0, "no such vrf %u", lcfg->vrf);
+ goto done;
+ }
+ sep.fib_index = fib_index;
+ }
+
+ if ((rv = hts_start_listen (htm, &sep, uri_key, lcfg->rnd_close)))
+ {
+ error = clib_error_return (0, "failed to listen on %v: %U", uri,
+ format_session_error, rv);
+ }
+
+done:
+
+ vec_free (uri_key);
+ return error;
+}
+
static int
hts_create (vlib_main_t *vm)
{
{
unformat_input_t _line_input, *line_input = &_line_input;
hts_main_t *htm = &hts_main;
+ hts_listen_cfg_t lcfg = {};
clib_error_t *error = 0;
- u8 is_del = 0;
u64 mem_size;
- u8 *uri = 0;
/* Get a line of input. */
if (!unformat_user (input, unformat_line_input, line_input))
else if (unformat (line_input, "fifo-size %U", unformat_memory_size,
&mem_size))
htm->fifo_size = mem_size;
- else if (unformat (line_input, "uri %s", &uri))
- ;
else if (unformat (line_input, "no-zc"))
htm->no_zc = 1;
else if (unformat (line_input, "debug"))
htm->debug_level = 1;
+ else if (unformat (line_input, "vrf %u", &lcfg.vrf))
+ ;
+ else if (unformat (line_input, "uri %s", &lcfg.uri))
+ ;
+ else if (unformat (line_input, "rnd-close %f", &lcfg.rnd_close))
+ {
+ if (lcfg.rnd_close > 1.0)
+ {
+ error = clib_error_return (0, "invalid rnd close value %f",
+ lcfg.rnd_close);
+ break;
+ }
+ }
else if (unformat (line_input, "del"))
- is_del = 1;
+ lcfg.is_del = 1;
else
{
error = clib_error_return (0, "unknown input `%U'",
}
}
- error = hts_listen (htm, uri, is_del);
+ error = hts_listen (htm, &lcfg);
done:
- vec_free (uri);
+ vec_free (lcfg.uri);
return error;
}