#include <vnet/session/application_local.h>
#include <vnet/session/session_debug.h>
#include <svm/queue.h>
+#include <sys/timerfd.h>
#define app_check_thread_and_barrier(_fn, _arg) \
if (!vlib_thread_is_main_w_barrier ()) \
return; \
}
+static void
+session_wrk_timerfd_update (session_worker_t *wrk, u64 time_ns)
+{
+ struct itimerspec its;
+
+ its.it_value.tv_sec = 0;
+ its.it_value.tv_nsec = time_ns;
+ its.it_interval.tv_sec = 0;
+ its.it_interval.tv_nsec = its.it_value.tv_nsec;
+
+ if (timerfd_settime (wrk->timerfd, 0, &its, NULL) == -1)
+ clib_warning ("timerfd_settime");
+}
+
+always_inline u64
+session_wrk_tfd_timeout (session_wrk_state_t state, u32 thread_index)
+{
+ if (state == SESSION_WRK_INTERRUPT)
+ return thread_index ? 1e6 : vlib_num_workers () ? 5e8 : 1e6;
+ else if (state == SESSION_WRK_IDLE)
+ return thread_index ? 1e8 : vlib_num_workers () ? 5e8 : 1e8;
+ else
+ return 0;
+}
+
+static inline void
+session_wrk_set_state (session_worker_t *wrk, session_wrk_state_t state)
+{
+ u64 time_ns;
+
+ wrk->state = state;
+ if (wrk->timerfd == -1)
+ return;
+ time_ns = session_wrk_tfd_timeout (state, wrk->vm->thread_index);
+ session_wrk_timerfd_update (wrk, time_ns);
+}
+
+static transport_endpt_ext_cfg_t *
+session_mq_get_ext_config (application_t *app, uword offset)
+{
+ svm_fifo_chunk_t *c;
+ fifo_segment_t *fs;
+
+ fs = application_get_rx_mqs_segment (app);
+ c = fs_chunk_ptr (fs->h, offset);
+ return (transport_endpt_ext_cfg_t *) c->data;
+}
+
+static void
+session_mq_free_ext_config (application_t *app, uword offset)
+{
+ svm_fifo_chunk_t *c;
+ fifo_segment_t *fs;
+
+ fs = application_get_rx_mqs_segment (app);
+ c = fs_chunk_ptr (fs->h, offset);
+ fifo_segment_collect_chunk (fs, 0 /* only one slice */, c);
+}
+
static void
session_mq_listen_handler (void *data)
{
clib_memset (a, 0, sizeof (*a));
a->sep.is_ip4 = mp->is_ip4;
- clib_memcpy_fast (&a->sep.ip, &mp->ip, sizeof (mp->ip));
+ ip_copy (&a->sep.ip, &mp->ip, mp->is_ip4);
a->sep.port = mp->port;
a->sep.fib_index = mp->vrf;
a->sep.sw_if_index = ENDPOINT_INVALID_INDEX;
a->sep.transport_proto = mp->proto;
- a->sep_ext.ckpair_index = mp->ckpair_index;
- a->sep_ext.crypto_engine = mp->crypto_engine;
a->app_index = app->app_index;
a->wrk_map_index = mp->wrk_index;
+ a->sep_ext.transport_flags = mp->flags;
+
+ if (mp->ext_config)
+ a->sep_ext.ext_cfg = session_mq_get_ext_config (app, mp->ext_config);
if ((rv = vnet_listen (a)))
- clib_warning ("listen returned: %d", rv);
+ clib_warning ("listen returned: %U", format_session_error, rv);
app_wrk = application_get_worker (app, mp->wrk_index);
mq_send_session_bound_cb (app_wrk->wrk_index, mp->context, a->handle, rv);
- return;
+
+ if (mp->ext_config)
+ session_mq_free_ext_config (app, mp->ext_config);
}
static void
}
static void
-session_mq_connect_handler (void *data)
+session_mq_connect_one (session_connect_msg_t *mp)
{
- session_connect_msg_t *mp = (session_connect_msg_t *) data;
vnet_connect_args_t _a, *a = &_a;
app_worker_t *app_wrk;
application_t *app;
int rv;
- app_check_thread_and_barrier (session_mq_connect_handler, mp);
-
app = application_lookup (mp->client_index);
if (!app)
return;
a->sep.transport_proto = mp->proto;
a->sep.peer.fib_index = mp->vrf;
clib_memcpy_fast (&a->sep.peer.ip, &mp->lcl_ip, sizeof (mp->lcl_ip));
- a->sep.peer.sw_if_index = ENDPOINT_INVALID_INDEX;
- a->sep_ext.parent_handle = mp->parent_handle;
- a->sep_ext.ckpair_index = mp->ckpair_index;
- a->sep_ext.crypto_engine = mp->crypto_engine;
- a->sep_ext.flags = mp->flags;
- if (mp->hostname_len)
+ if (mp->is_ip4)
{
- vec_validate (a->sep_ext.hostname, mp->hostname_len - 1);
- clib_memcpy_fast (a->sep_ext.hostname, mp->hostname, mp->hostname_len);
+ ip46_address_mask_ip4 (&a->sep.ip);
+ ip46_address_mask_ip4 (&a->sep.peer.ip);
}
+ a->sep.peer.port = mp->lcl_port;
+ a->sep.peer.sw_if_index = ENDPOINT_INVALID_INDEX;
+ a->sep_ext.parent_handle = mp->parent_handle;
+ a->sep_ext.transport_flags = mp->flags;
a->api_context = mp->context;
a->app_index = app->app_index;
a->wrk_map_index = mp->wrk_index;
+ if (mp->ext_config)
+ a->sep_ext.ext_cfg = session_mq_get_ext_config (app, mp->ext_config);
+
if ((rv = vnet_connect (a)))
{
- clib_warning ("connect returned: %U", format_vnet_api_errno, rv);
+ clib_warning ("connect returned: %U", format_session_error, rv);
app_wrk = application_get_worker (app, mp->wrk_index);
- mq_send_session_connected_cb (app_wrk->wrk_index, mp->context, 0,
- /* is_fail */ 1);
+ mq_send_session_connected_cb (app_wrk->wrk_index, mp->context, 0, rv);
+ }
+
+ if (mp->ext_config)
+ session_mq_free_ext_config (app, mp->ext_config);
+}
+
+static void
+session_mq_handle_connects_rpc (void *arg)
+{
+ u32 max_connects = 32, n_connects = 0;
+ vlib_main_t *vm = vlib_get_main ();
+ session_evt_elt_t *he, *elt, *next;
+ session_worker_t *fwrk, *wrk;
+
+ ASSERT (vlib_get_thread_index () == 0);
+
+ /* Pending connects on linked list pertaining to first worker */
+ fwrk = session_main_get_worker (1);
+ if (!fwrk->n_pending_connects)
+ goto update_state;
+
+ vlib_worker_thread_barrier_sync (vm);
+
+ he = clib_llist_elt (fwrk->event_elts, fwrk->pending_connects);
+ elt = clib_llist_next (fwrk->event_elts, evt_list, he);
+
+ /* Avoid holding the barrier for too long */
+ while (n_connects < max_connects && elt != he)
+ {
+ next = clib_llist_next (fwrk->event_elts, evt_list, elt);
+ clib_llist_remove (fwrk->event_elts, evt_list, elt);
+ session_mq_connect_one (session_evt_ctrl_data (fwrk, elt));
+ session_evt_ctrl_data_free (fwrk, elt);
+ clib_llist_put (fwrk->event_elts, elt);
+ elt = next;
+ n_connects += 1;
+ }
+
+ /* Decrement with worker barrier */
+ fwrk->n_pending_connects -= n_connects;
+
+ vlib_worker_thread_barrier_release (vm);
+
+update_state:
+
+ /* Switch worker to poll mode if it was in interrupt mode and had work or
+ * back to interrupt if threshold of loops without a connect is passed.
+ * While in poll mode, reprogram connects rpc */
+ wrk = session_main_get_worker (0);
+ if (wrk->state != SESSION_WRK_POLLING)
+ {
+ if (n_connects)
+ {
+ session_wrk_set_state (wrk, SESSION_WRK_POLLING);
+ vlib_node_set_state (vm, session_queue_node.index,
+ VLIB_NODE_STATE_POLLING);
+ wrk->no_connect_loops = 0;
+ }
+ }
+ else
+ {
+ if (!n_connects)
+ {
+ if (++wrk->no_connect_loops > 1e5)
+ {
+ session_wrk_set_state (wrk, SESSION_WRK_INTERRUPT);
+ vlib_node_set_state (vm, session_queue_node.index,
+ VLIB_NODE_STATE_INTERRUPT);
+ }
+ }
+ else
+ wrk->no_connect_loops = 0;
}
- vec_free (a->sep_ext.hostname);
+ if (wrk->state == SESSION_WRK_POLLING)
+ {
+ elt = session_evt_alloc_ctrl (wrk);
+ elt->evt.event_type = SESSION_CTRL_EVT_RPC;
+ elt->evt.rpc_args.fp = session_mq_handle_connects_rpc;
+ }
+}
+
+static void
+session_mq_connect_handler (session_worker_t *wrk, session_evt_elt_t *elt)
+{
+ u32 thread_index = wrk - session_main.wrk;
+ session_evt_elt_t *he;
+
+ /* No workers, so just deal with the connect now */
+ if (PREDICT_FALSE (!thread_index))
+ {
+ session_mq_connect_one (session_evt_ctrl_data (wrk, elt));
+ return;
+ }
+
+ if (PREDICT_FALSE (thread_index != 1))
+ {
+ clib_warning ("Connect on wrong thread. Dropping");
+ return;
+ }
+
+ /* Add to pending list to be handled by main thread */
+ he = clib_llist_elt (wrk->event_elts, wrk->pending_connects);
+ clib_llist_add_tail (wrk->event_elts, evt_list, elt, he);
+
+ /* Decremented with worker barrier */
+ wrk->n_pending_connects += 1;
+ if (wrk->n_pending_connects == 1)
+ {
+ vlib_node_set_interrupt_pending (vlib_get_main_by_index (0),
+ session_queue_node.index);
+ session_send_rpc_evt_to_thread (0, session_mq_handle_connects_rpc, 0);
+ }
}
static void
{
clib_warning ("connect_uri returned: %d", rv);
app_wrk = application_get_worker (app, 0 /* default wrk only */ );
- mq_send_session_connected_cb (app_wrk->wrk_index, mp->context, 0,
- /* is_fail */ 1);
+ mq_send_session_connected_cb (app_wrk->wrk_index, mp->context, 0, rv);
}
}
+static void
+session_mq_shutdown_handler (void *data)
+{
+ session_shutdown_msg_t *mp = (session_shutdown_msg_t *) data;
+ vnet_shutdown_args_t _a, *a = &_a;
+ application_t *app;
+
+ app = application_lookup (mp->client_index);
+ if (!app)
+ return;
+
+ a->app_index = app->app_index;
+ a->handle = mp->handle;
+ vnet_shutdown_session (a);
+}
+
static void
session_mq_disconnect_handler (void *data)
{
}
static void
-session_mq_unlisten_handler (void *data)
+session_mq_unlisten_rpc (session_unlisten_msg_t *mp)
{
- session_unlisten_msg_t *mp = (session_unlisten_msg_t *) data;
+ vlib_main_t *vm = vlib_get_main ();
vnet_unlisten_args_t _a, *a = &_a;
app_worker_t *app_wrk;
+ session_handle_t sh;
application_t *app;
+ u32 context;
int rv;
- app_check_thread_and_barrier (session_mq_unlisten_handler, mp);
+ sh = mp->handle;
+ context = mp->context;
app = application_lookup (mp->client_index);
if (!app)
clib_memset (a, 0, sizeof (*a));
a->app_index = app->app_index;
- a->handle = mp->handle;
+ a->handle = sh;
a->wrk_map_index = mp->wrk_index;
+
+ vlib_worker_thread_barrier_sync (vm);
+
if ((rv = vnet_unlisten (a)))
clib_warning ("unlisten returned: %d", rv);
+ vlib_worker_thread_barrier_release (vm);
+
app_wrk = application_get_worker (app, a->wrk_map_index);
if (!app_wrk)
return;
- mq_send_unlisten_reply (app_wrk, mp->handle, mp->context, rv);
+ mq_send_unlisten_reply (app_wrk, sh, context, rv);
+ clib_mem_free (mp);
+}
+
+static void
+session_mq_unlisten_handler (session_worker_t *wrk, session_evt_elt_t *elt)
+{
+ u32 thread_index = wrk - session_main.wrk;
+ session_unlisten_msg_t *mp, *arg;
+
+ mp = session_evt_ctrl_data (wrk, elt);
+ arg = clib_mem_alloc (sizeof (session_unlisten_msg_t));
+ clib_memcpy_fast (arg, mp, sizeof (*arg));
+
+ if (PREDICT_FALSE (!thread_index))
+ {
+ session_mq_unlisten_rpc (arg);
+ return;
+ }
+
+ session_send_rpc_evt_to_thread_force (0, session_mq_unlisten_rpc, arg);
}
static void
if (!session_has_transport (s))
{
s->session_state = SESSION_STATE_READY;
- if (ct_session_connect_notify (s))
+ if (ct_session_connect_notify (s, SESSION_E_NONE))
return;
}
else
evt->event_type = SESSION_CTRL_EVT_WORKER_UPDATE_REPLY;
rmp = (session_worker_update_reply_msg_t *) evt->data;
rmp->handle = mp->handle;
- rmp->rx_fifo = pointer_to_uword (s->rx_fifo);
- rmp->tx_fifo = pointer_to_uword (s->tx_fifo);
+ if (s->rx_fifo)
+ rmp->rx_fifo = fifo_segment_fifo_offset (s->rx_fifo);
+ if (s->tx_fifo)
+ rmp->tx_fifo = fifo_segment_fifo_offset (s->tx_fifo);
rmp->segment_handle = session_segment_handle (s);
svm_msg_q_add_and_unlock (app_wrk->event_queue, msg);
app_worker_close_notify (app_wrk, s);
}
+static void
+session_mq_app_wrk_rpc_handler (void *data)
+{
+ session_app_wrk_rpc_msg_t *mp = (session_app_wrk_rpc_msg_t *) data;
+ svm_msg_q_msg_t _msg, *msg = &_msg;
+ session_app_wrk_rpc_msg_t *rmp;
+ app_worker_t *app_wrk;
+ session_event_t *evt;
+ application_t *app;
+
+ app = application_lookup (mp->client_index);
+ if (!app)
+ return;
+
+ app_wrk = application_get_worker (app, mp->wrk_index);
+
+ svm_msg_q_lock_and_alloc_msg_w_ring (app_wrk->event_queue,
+ SESSION_MQ_CTRL_EVT_RING, SVM_Q_WAIT,
+ msg);
+ evt = svm_msg_q_msg_data (app_wrk->event_queue, msg);
+ clib_memset (evt, 0, sizeof (*evt));
+ evt->event_type = SESSION_CTRL_EVT_APP_WRK_RPC;
+ rmp = (session_app_wrk_rpc_msg_t *) evt->data;
+ clib_memcpy (rmp->data, mp->data, sizeof (mp->data));
+ svm_msg_q_add_and_unlock (app_wrk->event_queue, msg);
+}
+
+static void
+session_mq_transport_attr_handler (void *data)
+{
+ session_transport_attr_msg_t *mp = (session_transport_attr_msg_t *) data;
+ session_transport_attr_reply_msg_t *rmp;
+ svm_msg_q_msg_t _msg, *msg = &_msg;
+ app_worker_t *app_wrk;
+ session_event_t *evt;
+ application_t *app;
+ session_t *s;
+ int rv;
+
+ app = application_lookup (mp->client_index);
+ if (!app)
+ return;
+
+ if (!(s = session_get_from_handle_if_valid (mp->handle)))
+ {
+ clib_warning ("invalid handle %llu", mp->handle);
+ return;
+ }
+ app_wrk = app_worker_get (s->app_wrk_index);
+ if (app_wrk->app_index != app->app_index)
+ {
+ clib_warning ("app %u does not own session %llu", app->app_index,
+ mp->handle);
+ return;
+ }
+
+ rv = session_transport_attribute (s, mp->is_get, &mp->attr);
+
+ svm_msg_q_lock_and_alloc_msg_w_ring (
+ app_wrk->event_queue, SESSION_MQ_CTRL_EVT_RING, SVM_Q_WAIT, msg);
+ evt = svm_msg_q_msg_data (app_wrk->event_queue, msg);
+ clib_memset (evt, 0, sizeof (*evt));
+ evt->event_type = SESSION_CTRL_EVT_TRANSPORT_ATTR_REPLY;
+ rmp = (session_transport_attr_reply_msg_t *) evt->data;
+ rmp->handle = mp->handle;
+ rmp->retval = rv;
+ rmp->is_get = mp->is_get;
+ if (!rv && mp->is_get)
+ rmp->attr = mp->attr;
+ svm_msg_q_add_and_unlock (app_wrk->event_queue, msg);
+}
+
vlib_node_registration_t session_queue_node;
typedef struct
u32 next_index, u32 * to_next, u16 n_segs,
session_t * s, u32 n_trace)
{
- session_queue_trace_t *t;
- vlib_buffer_t *b;
- int i;
-
- for (i = 0; i < clib_min (n_trace, n_segs); i++)
+ while (n_trace && n_segs)
{
- b = vlib_get_buffer (vm, to_next[i]);
- vlib_trace_buffer (vm, node, next_index, b, 1 /* follow_chain */ );
- t = vlib_add_trace (vm, node, b, sizeof (*t));
- t->session_index = s->session_index;
- t->server_thread_index = s->thread_index;
+ vlib_buffer_t *b = vlib_get_buffer (vm, to_next[0]);
+ if (PREDICT_TRUE
+ (vlib_trace_buffer
+ (vm, node, next_index, b, 1 /* follow_chain */ )))
+ {
+ session_queue_trace_t *t =
+ vlib_add_trace (vm, node, b, sizeof (*t));
+ t->session_index = s->session_index;
+ t->server_thread_index = s->thread_index;
+ n_trace--;
+ }
+ to_next++;
+ n_segs--;
}
- vlib_set_trace_count (vm, node, n_trace - i);
+ vlib_set_trace_count (vm, node, n_trace);
}
always_inline void
{
vlib_buffer_t *chain_b, *prev_b;
u32 chain_bi0, to_deq, left_from_seg;
- session_worker_t *wrk;
u16 len_to_deq, n_bytes_read;
u8 *data, j;
- wrk = session_main_get_worker (ctx->s->thread_index);
b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
b->total_length_not_including_first_buffer = 0;
len_to_deq = clib_min (to_deq, ctx->deq_per_buf);
*n_bufs -= 1;
- chain_bi0 = wrk->tx_buffers[*n_bufs];
+ chain_bi0 = ctx->tx_buffers[*n_bufs];
chain_b = vlib_get_buffer (vm, chain_bi0);
chain_b->current_data = 0;
data = vlib_buffer_get_current (chain_b);
svm_fifo_t *f = ctx->s->tx_fifo;
session_dgram_hdr_t *hdr = &ctx->hdr;
u16 deq_now;
+ u32 offset;
+
deq_now = clib_min (hdr->data_length - hdr->data_offset,
len_to_deq);
- n_bytes_read = svm_fifo_peek (f, hdr->data_offset, deq_now,
- data);
+ offset = hdr->data_offset + SESSION_CONN_HDR_LEN;
+ n_bytes_read = svm_fifo_peek (f, offset, deq_now, data);
ASSERT (n_bytes_read > 0);
hdr->data_offset += n_bytes_read;
if (hdr->data_offset == hdr->data_length)
{
- u32 offset = hdr->data_length + SESSION_CONN_HDR_LEN;
+ offset = hdr->data_length + SESSION_CONN_HDR_LEN;
svm_fifo_dequeue_drop (f, offset);
+ if (ctx->left_to_snd > n_bytes_read)
+ svm_fifo_peek (ctx->s->tx_fifo, 0, sizeof (ctx->hdr),
+ (u8 *) & ctx->hdr);
}
+ else if (ctx->left_to_snd == n_bytes_read)
+ svm_fifo_overwrite_head (ctx->s->tx_fifo, (u8 *) & ctx->hdr,
+ sizeof (session_dgram_pre_hdr_t));
}
else
n_bytes_read = svm_fifo_dequeue (ctx->s->tx_fifo,
n_bytes_read = svm_fifo_peek (f, offset, deq_now, data0);
ASSERT (n_bytes_read > 0);
- if (ctx->s->session_state == SESSION_STATE_LISTENING)
+ if (transport_connection_is_cless (ctx->tc))
{
ip_copy (&ctx->tc->rmt_ip, &hdr->rmt_ip, ctx->tc->is_ip4);
ctx->tc->rmt_port = hdr->rmt_port;
{
offset = hdr->data_length + SESSION_CONN_HDR_LEN;
svm_fifo_dequeue_drop (f, offset);
+ if (ctx->left_to_snd > n_bytes_read)
+ svm_fifo_peek (ctx->s->tx_fifo, 0, sizeof (ctx->hdr),
+ (u8 *) & ctx->hdr);
}
+ else if (ctx->left_to_snd == n_bytes_read)
+ svm_fifo_overwrite_head (ctx->s->tx_fifo, (u8 *) & ctx->hdr,
+ sizeof (session_dgram_pre_hdr_t));
}
else
{
/* Can retransmit for closed sessions but can't send new data if
* session is not ready or closed */
else if (s->session_state < SESSION_STATE_READY)
- return 1;
+ {
+ /* Allow accepting session to send custom packets.
+ * For instance, tcp want to send acks in established, but
+ * the app has not called accept() yet */
+ if (s->session_state == SESSION_STATE_ACCEPTING &&
+ (s->flags & SESSION_F_CUSTOM_TX))
+ return 0;
+ return 1;
+ }
else if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSED)
{
/* Allow closed transports to still send custom packets.
u32 max_segs, u8 peek_data)
{
u32 n_bytes_per_buf, n_bytes_per_seg;
+
+ n_bytes_per_buf = vlib_buffer_get_default_data_size (vm);
ctx->max_dequeue = svm_fifo_max_dequeue_cons (ctx->s->tx_fifo);
+
if (peek_data)
{
/* Offset in rx fifo from where to peek data */
{
if (ctx->transport_vft->transport_options.tx_type == TRANSPORT_TX_DGRAM)
{
+ u32 len, chain_limit;
+
if (ctx->max_dequeue <= sizeof (ctx->hdr))
{
ctx->max_len_to_snd = 0;
return;
}
+
svm_fifo_peek (ctx->s->tx_fifo, 0, sizeof (ctx->hdr),
(u8 *) & ctx->hdr);
ASSERT (ctx->hdr.data_length > ctx->hdr.data_offset);
- ctx->max_dequeue = ctx->hdr.data_length - ctx->hdr.data_offset;
+ len = ctx->hdr.data_length - ctx->hdr.data_offset;
+
+ /* Process multiple dgrams if smaller than min (buf_space, mss).
+ * This avoids handling multiple dgrams if they require buffer
+ * chains */
+ chain_limit = clib_min (n_bytes_per_buf - TRANSPORT_MAX_HDRS_LEN,
+ ctx->sp.snd_mss);
+ if (ctx->hdr.data_length <= chain_limit)
+ {
+ u32 first_dgram_len, dgram_len, offset, max_offset;
+ session_dgram_hdr_t hdr;
+
+ ctx->sp.snd_mss = clib_min (ctx->sp.snd_mss, len);
+ offset = ctx->hdr.data_length + sizeof (session_dgram_hdr_t);
+ first_dgram_len = len;
+ max_offset = clib_min (ctx->max_dequeue, 16 << 10);
+
+ while (offset < max_offset)
+ {
+ svm_fifo_peek (ctx->s->tx_fifo, offset, sizeof (ctx->hdr),
+ (u8 *) & hdr);
+ ASSERT (hdr.data_length > hdr.data_offset);
+ dgram_len = hdr.data_length - hdr.data_offset;
+ if (len + dgram_len > ctx->max_dequeue
+ || first_dgram_len != dgram_len)
+ break;
+ len += dgram_len;
+ offset += sizeof (hdr) + hdr.data_length;
+ }
+ }
+
+ ctx->max_dequeue = len;
}
}
ASSERT (ctx->max_dequeue > 0);
ctx->max_len_to_snd = max_segs * ctx->sp.snd_mss;
}
- n_bytes_per_buf = vlib_buffer_get_default_data_size (vm);
ASSERT (n_bytes_per_buf > TRANSPORT_MAX_HDRS_LEN);
- n_bytes_per_seg = TRANSPORT_MAX_HDRS_LEN + ctx->sp.snd_mss;
- ctx->n_bufs_per_seg = ceil ((f64) n_bytes_per_seg / n_bytes_per_buf);
+ if (ctx->n_segs_per_evt > 1)
+ {
+ u32 n_bytes_last_seg, n_bufs_last_seg;
+
+ n_bytes_per_seg = TRANSPORT_MAX_HDRS_LEN + ctx->sp.snd_mss;
+ n_bytes_last_seg = TRANSPORT_MAX_HDRS_LEN + ctx->max_len_to_snd
+ - ((ctx->n_segs_per_evt - 1) * ctx->sp.snd_mss);
+ ctx->n_bufs_per_seg = ceil ((f64) n_bytes_per_seg / n_bytes_per_buf);
+ n_bufs_last_seg = ceil ((f64) n_bytes_last_seg / n_bytes_per_buf);
+ ctx->n_bufs_needed = ((ctx->n_segs_per_evt - 1) * ctx->n_bufs_per_seg)
+ + n_bufs_last_seg;
+ }
+ else
+ {
+ n_bytes_per_seg = TRANSPORT_MAX_HDRS_LEN + ctx->max_len_to_snd;
+ ctx->n_bufs_per_seg = ceil ((f64) n_bytes_per_seg / n_bytes_per_buf);
+ ctx->n_bufs_needed = ctx->n_bufs_per_seg;
+ }
+
ctx->deq_per_buf = clib_min (ctx->sp.snd_mss, n_bytes_per_buf);
ctx->deq_per_first_buf = clib_min (ctx->sp.snd_mss,
n_bytes_per_buf -
session_evt_elt_t * elt,
int *n_tx_packets, u8 peek_data)
{
- u32 n_trace, n_bufs_needed = 0, n_left, pbi, next_index, max_burst;
+ u32 n_trace, n_left, pbi, next_index, max_burst;
session_tx_context_t *ctx = &wrk->ctx;
session_main_t *smm = &session_main;
session_event_t *e = &elt->evt;
}
next_index = smm->session_type_to_next[ctx->s->session_type];
- max_burst = VLIB_FRAME_SIZE - *n_tx_packets;
+ max_burst = SESSION_NODE_FRAME_SIZE - *n_tx_packets;
tp = session_get_transport_proto (ctx->s);
ctx->transport_vft = transport_protocol_get_vft (tp);
{
u32 n_custom_tx;
ctx->s->flags &= ~SESSION_F_CUSTOM_TX;
- n_custom_tx = ctx->transport_vft->custom_tx (ctx->tc, max_burst);
+ ctx->sp.max_burst_size = max_burst;
+ n_custom_tx = ctx->transport_vft->custom_tx (ctx->tc, &ctx->sp);
*n_tx_packets += n_custom_tx;
if (PREDICT_FALSE
(ctx->s->session_state >= SESSION_STATE_TRANSPORT_CLOSED))
return SESSION_TX_OK;
max_burst -= n_custom_tx;
- if (!max_burst)
+ if (!max_burst || (ctx->s->flags & SESSION_F_CUSTOM_TX))
{
session_evt_add_old (wrk, elt);
return SESSION_TX_OK;
if (!ctx->sp.snd_space)
{
- /* This flow queue is "empty" so it should be re-evaluated before
- * the ones that have data to send. */
- if (PREDICT_TRUE (!ctx->sp.flags))
- session_evt_add_head_old (wrk, elt);
+ /* If the deschedule flag was set, remove session from scheduler.
+ * Transport is responsible for rescheduling this session. */
+ if (ctx->sp.flags & TRANSPORT_SND_F_DESCHED)
+ transport_connection_deschedule (ctx->tc);
/* Request to postpone the session, e.g., zero-wnd and transport
* is not currently probing */
else if (ctx->sp.flags & TRANSPORT_SND_F_POSTPONE)
session_evt_add_old (wrk, elt);
- /* If the deschedule flag was set, remove session from scheduler.
- * Transport is responsible for rescheduling this session. */
+ /* This flow queue is "empty" so it should be re-evaluated before
+ * the ones that have data to send. */
else
- transport_connection_deschedule (ctx->tc);
+ session_evt_add_head_old (wrk, elt);
return SESSION_TX_NO_DATA;
}
return SESSION_TX_NO_DATA;
}
- n_bufs_needed = ctx->n_segs_per_evt * ctx->n_bufs_per_seg;
- vec_validate_aligned (wrk->tx_buffers, n_bufs_needed - 1,
+ vec_validate_aligned (ctx->tx_buffers, ctx->n_bufs_needed - 1,
CLIB_CACHE_LINE_BYTES);
- n_bufs = vlib_buffer_alloc (vm, wrk->tx_buffers, n_bufs_needed);
- if (PREDICT_FALSE (n_bufs < n_bufs_needed))
+ n_bufs = vlib_buffer_alloc (vm, ctx->tx_buffers, ctx->n_bufs_needed);
+ if (PREDICT_FALSE (n_bufs < ctx->n_bufs_needed))
{
if (n_bufs)
- vlib_buffer_free (vm, wrk->tx_buffers, n_bufs);
+ vlib_buffer_free (vm, ctx->tx_buffers, n_bufs);
session_evt_add_head_old (wrk, elt);
vlib_node_increment_counter (wrk->vm, node->node_index,
SESSION_QUEUE_ERROR_NO_BUFFER, 1);
return SESSION_TX_NO_BUFFERS;
}
+ if (transport_connection_is_tx_paced (ctx->tc))
+ transport_connection_tx_pacer_update_bytes (ctx->tc, ctx->max_len_to_snd);
+
ctx->left_to_snd = ctx->max_len_to_snd;
n_left = ctx->n_segs_per_evt;
vlib_buffer_t *b0, *b1;
u32 bi0, bi1;
- pbi = wrk->tx_buffers[n_bufs - 3];
+ pbi = ctx->tx_buffers[n_bufs - 3];
pb = vlib_get_buffer (vm, pbi);
vlib_prefetch_buffer_header (pb, STORE);
- pbi = wrk->tx_buffers[n_bufs - 4];
+ pbi = ctx->tx_buffers[n_bufs - 4];
pb = vlib_get_buffer (vm, pbi);
vlib_prefetch_buffer_header (pb, STORE);
- bi0 = wrk->tx_buffers[--n_bufs];
- bi1 = wrk->tx_buffers[--n_bufs];
+ bi0 = ctx->tx_buffers[--n_bufs];
+ bi1 = ctx->tx_buffers[--n_bufs];
b0 = vlib_get_buffer (vm, bi0);
b1 = vlib_get_buffer (vm, bi1);
n_left -= 2;
- VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
- VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b1);
-
vec_add1 (wrk->pending_tx_buffers, bi0);
vec_add1 (wrk->pending_tx_buffers, bi1);
vec_add1 (wrk->pending_tx_nexts, next_index);
if (n_left > 1)
{
- pbi = wrk->tx_buffers[n_bufs - 2];
+ pbi = ctx->tx_buffers[n_bufs - 2];
pb = vlib_get_buffer (vm, pbi);
vlib_prefetch_buffer_header (pb, STORE);
}
- bi0 = wrk->tx_buffers[--n_bufs];
+ bi0 = ctx->tx_buffers[--n_bufs];
b0 = vlib_get_buffer (vm, bi0);
session_tx_fill_buffer (vm, ctx, b0, &n_bufs, peek_data);
n_left -= 1;
- VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
-
vec_add1 (wrk->pending_tx_buffers, bi0);
vec_add1 (wrk->pending_tx_nexts, next_index);
}
ctx->n_segs_per_evt, ctx->s, n_trace);
if (PREDICT_FALSE (n_bufs))
- vlib_buffer_free (vm, wrk->tx_buffers, n_bufs);
+ vlib_buffer_free (vm, ctx->tx_buffers, n_bufs);
*n_tx_packets += ctx->n_segs_per_evt;
- transport_connection_update_tx_bytes (ctx->tc, ctx->max_len_to_snd);
SESSION_EVT (SESSION_EVT_DEQ, ctx->s, ctx->max_len_to_snd, ctx->max_dequeue,
ctx->s->tx_fifo->has_event, wrk->last_vlib_time);
else
session_tx_maybe_reschedule (wrk, ctx, elt);
- if (!peek_data
- && ctx->transport_vft->transport_options.tx_type == TRANSPORT_TX_DGRAM)
+ if (!peek_data)
{
- /* Fix dgram pre header */
- if (ctx->max_len_to_snd < ctx->max_dequeue)
- svm_fifo_overwrite_head (ctx->s->tx_fifo, (u8 *) & ctx->hdr,
- sizeof (session_dgram_pre_hdr_t));
- /* More data needs to be read */
- else if (svm_fifo_max_dequeue_cons (ctx->s->tx_fifo) > 0)
- if (svm_fifo_set_event (ctx->s->tx_fifo))
- session_evt_add_old (wrk, elt);
-
- if (svm_fifo_needs_deq_ntf (ctx->s->tx_fifo, ctx->max_len_to_snd))
+ u32 n_dequeued = ctx->max_len_to_snd;
+ if (ctx->transport_vft->transport_options.tx_type == TRANSPORT_TX_DGRAM)
+ n_dequeued += ctx->n_segs_per_evt * SESSION_CONN_HDR_LEN;
+ if (svm_fifo_needs_deq_ntf (ctx->s->tx_fifo, n_dequeued))
session_dequeue_notify (ctx->s);
}
return SESSION_TX_OK;
int
session_tx_fifo_dequeue_internal (session_worker_t * wrk,
vlib_node_runtime_t * node,
- session_evt_elt_t * e, int *n_tx_packets)
+ session_evt_elt_t * elt, int *n_tx_packets)
{
+ transport_send_params_t *sp = &wrk->ctx.sp;
session_t *s = wrk->ctx.s;
+ u32 n_packets;
if (PREDICT_FALSE (s->session_state >= SESSION_STATE_TRANSPORT_CLOSED))
return 0;
- svm_fifo_unset_event (s->tx_fifo);
- return transport_custom_tx (session_get_transport_proto (s), s,
- VLIB_FRAME_SIZE - *n_tx_packets);
+
+ /* Clear custom-tx flag used to request reschedule for tx */
+ s->flags &= ~SESSION_F_CUSTOM_TX;
+
+ sp->max_burst_size = clib_min (SESSION_NODE_FRAME_SIZE - *n_tx_packets,
+ TRANSPORT_PACER_MAX_BURST_PKTS);
+
+ n_packets = transport_custom_tx (session_get_transport_proto (s), s, sp);
+ *n_tx_packets += n_packets;
+
+ if (s->flags & SESSION_F_CUSTOM_TX)
+ {
+ session_evt_add_old (wrk, elt);
+ }
+ else if (!(sp->flags & TRANSPORT_SND_F_DESCHED))
+ {
+ svm_fifo_unset_event (s->tx_fifo);
+ if (svm_fifo_max_dequeue_cons (s->tx_fifo))
+ if (svm_fifo_set_event (s->tx_fifo))
+ session_evt_add_head_old (wrk, elt);
+ }
+
+ if (sp->max_burst_size &&
+ svm_fifo_needs_deq_ntf (s->tx_fifo, sp->max_burst_size))
+ session_dequeue_notify (s);
+
+ return n_packets;
}
always_inline session_t *
-session_event_get_session (session_event_t * e, u8 thread_index)
+session_event_get_session (session_worker_t * wrk, session_event_t * e)
{
- return session_get_if_valid (e->session_index, thread_index);
+ if (PREDICT_FALSE (pool_is_free_index (wrk->sessions, e->session_index)))
+ return 0;
+
+ ASSERT (session_is_valid (e->session_index, wrk->vm->thread_index));
+ return pool_elt_at_index (wrk->sessions, e->session_index);
}
always_inline void
fp = e->rpc_args.fp;
(*fp) (e->rpc_args.arg);
break;
+ case SESSION_CTRL_EVT_HALF_CLOSE:
+ s = session_get_from_handle_if_valid (e->session_handle);
+ if (PREDICT_FALSE (!s))
+ break;
+ session_transport_half_close (s);
+ break;
case SESSION_CTRL_EVT_CLOSE:
s = session_get_from_handle_if_valid (e->session_handle);
if (PREDICT_FALSE (!s))
session_mq_listen_uri_handler (session_evt_ctrl_data (wrk, elt));
break;
case SESSION_CTRL_EVT_UNLISTEN:
- session_mq_unlisten_handler (session_evt_ctrl_data (wrk, elt));
+ session_mq_unlisten_handler (wrk, elt);
break;
case SESSION_CTRL_EVT_CONNECT:
- session_mq_connect_handler (session_evt_ctrl_data (wrk, elt));
+ session_mq_connect_handler (wrk, elt);
break;
case SESSION_CTRL_EVT_CONNECT_URI:
session_mq_connect_uri_handler (session_evt_ctrl_data (wrk, elt));
break;
+ case SESSION_CTRL_EVT_SHUTDOWN:
+ session_mq_shutdown_handler (session_evt_ctrl_data (wrk, elt));
+ break;
case SESSION_CTRL_EVT_DISCONNECT:
session_mq_disconnect_handler (session_evt_ctrl_data (wrk, elt));
break;
case SESSION_CTRL_EVT_APP_DETACH:
app_mq_detach_handler (session_evt_ctrl_data (wrk, elt));
break;
+ case SESSION_CTRL_EVT_APP_WRK_RPC:
+ session_mq_app_wrk_rpc_handler (session_evt_ctrl_data (wrk, elt));
+ break;
+ case SESSION_CTRL_EVT_TRANSPORT_ATTR:
+ session_mq_transport_attr_handler (session_evt_ctrl_data (wrk, elt));
+ break;
default:
clib_warning ("unhandled event type %d", e->event_type);
}
/* Regrab elements in case pool moved */
- elt = pool_elt_at_index (wrk->event_elts, ei);
+ elt = clib_llist_elt (wrk->event_elts, ei);
if (!clib_llist_elt_is_linked (elt, evt_list))
{
e = &elt->evt;
if (e->event_type >= SESSION_CTRL_EVT_BOUND)
session_evt_ctrl_data_free (wrk, elt);
- session_evt_elt_free (wrk, elt);
+ clib_llist_put (wrk->event_elts, elt);
}
+ SESSION_EVT (SESSION_EVT_COUNTS, CNT_CTRL_EVTS, 1, wrk);
}
always_inline void
session_event_dispatch_io (session_worker_t * wrk, vlib_node_runtime_t * node,
- session_evt_elt_t * elt, u32 thread_index,
- int *n_tx_packets)
+ session_evt_elt_t * elt, int *n_tx_packets)
{
session_main_t *smm = &session_main;
app_worker_t *app_wrk;
{
case SESSION_IO_EVT_TX_FLUSH:
case SESSION_IO_EVT_TX:
- s = session_event_get_session (e, thread_index);
+ s = session_event_get_session (wrk, e);
if (PREDICT_FALSE (!s))
break;
- CLIB_PREFETCH (s->tx_fifo, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
+ CLIB_PREFETCH (s->tx_fifo, sizeof (*(s->tx_fifo)), LOAD);
wrk->ctx.s = s;
/* Spray packets in per session type frames, since they go to
* different nodes */
(smm->session_tx_fns[s->session_type]) (wrk, node, elt, n_tx_packets);
break;
case SESSION_IO_EVT_RX:
- s = session_event_get_session (e, thread_index);
+ s = session_event_get_session (wrk, e);
if (!s)
break;
transport_app_rx_evt (session_get_transport_proto (s),
s->connection_index, s->thread_index);
break;
case SESSION_IO_EVT_BUILTIN_RX:
- s = session_event_get_session (e, thread_index);
+ s = session_event_get_session (wrk, e);
if (PREDICT_FALSE (!s || s->session_state >= SESSION_STATE_CLOSING))
break;
svm_fifo_unset_event (s->rx_fifo);
clib_warning ("unhandled event type %d", e->event_type);
}
+ SESSION_EVT (SESSION_IO_EVT_COUNTS, e->event_type, 1, wrk);
+
/* Regrab elements in case pool moved */
- elt = pool_elt_at_index (wrk->event_elts, ei);
+ elt = clib_llist_elt (wrk->event_elts, ei);
if (!clib_llist_elt_is_linked (elt, evt_list))
- session_evt_elt_free (wrk, elt);
+ clib_llist_put (wrk->event_elts, elt);
}
/* *INDENT-OFF* */
session_flush_pending_tx_buffers (session_worker_t * wrk,
vlib_node_runtime_t * node)
{
- vlib_buffer_enqueue_to_next (wrk->vm, node, wrk->pending_tx_buffers,
- wrk->pending_tx_nexts,
- vec_len (wrk->pending_tx_nexts));
+ vlib_buffer_enqueue_to_next_vec (wrk->vm, node, &wrk->pending_tx_buffers,
+ &wrk->pending_tx_nexts,
+ vec_len (wrk->pending_tx_nexts));
vec_reset_length (wrk->pending_tx_buffers);
vec_reset_length (wrk->pending_tx_nexts);
}
+int
+session_wrk_handle_mq (session_worker_t *wrk, svm_msg_q_t *mq)
+{
+ svm_msg_q_msg_t _msg, *msg = &_msg;
+ u32 i, n_to_dequeue = 0;
+ session_event_t *evt;
+
+ n_to_dequeue = svm_msg_q_size (mq);
+ for (i = 0; i < n_to_dequeue; i++)
+ {
+ svm_msg_q_sub_raw (mq, msg);
+ evt = svm_msg_q_msg_data (mq, msg);
+ session_evt_add_to_list (wrk, evt);
+ svm_msg_q_free_msg (mq, msg);
+ }
+
+ return n_to_dequeue;
+}
+
+static void
+session_wrk_update_state (session_worker_t *wrk)
+{
+ vlib_main_t *vm = wrk->vm;
+
+ if (wrk->state == SESSION_WRK_POLLING)
+ {
+ if (clib_llist_elts (wrk->event_elts) == 4 &&
+ vlib_last_vectors_per_main_loop (vm) < 1)
+ {
+ session_wrk_set_state (wrk, SESSION_WRK_INTERRUPT);
+ vlib_node_set_state (vm, session_queue_node.index,
+ VLIB_NODE_STATE_INTERRUPT);
+ }
+ }
+ else if (wrk->state == SESSION_WRK_INTERRUPT)
+ {
+ if (clib_llist_elts (wrk->event_elts) > 4 ||
+ vlib_last_vectors_per_main_loop (vm) > 1)
+ {
+ session_wrk_set_state (wrk, SESSION_WRK_POLLING);
+ vlib_node_set_state (vm, session_queue_node.index,
+ VLIB_NODE_STATE_POLLING);
+ }
+ else if (PREDICT_FALSE (!pool_elts (wrk->sessions)))
+ {
+ session_wrk_set_state (wrk, SESSION_WRK_IDLE);
+ }
+ }
+ else
+ {
+ if (clib_llist_elts (wrk->event_elts))
+ {
+ session_wrk_set_state (wrk, SESSION_WRK_INTERRUPT);
+ }
+ }
+}
+
static uword
session_queue_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
vlib_frame_t * frame)
{
+ u32 thread_index = vm->thread_index, __clib_unused n_evts;
+ session_evt_elt_t *elt, *ctrl_he, *new_he, *old_he;
session_main_t *smm = vnet_get_session_main ();
- u32 thread_index = vm->thread_index, n_to_dequeue;
session_worker_t *wrk = &smm->wrk[thread_index];
- session_evt_elt_t *elt, *ctrl_he, *new_he, *old_he;
clib_llist_index_t ei, next_ei, old_ti;
- svm_msg_q_msg_t _msg, *msg = &_msg;
- int i, n_tx_packets;
- session_event_t *evt;
- svm_msg_q_t *mq;
+ int n_tx_packets;
SESSION_EVT (SESSION_EVT_DISPATCH_START, wrk);
- wrk->last_vlib_time = vlib_time_now (vm);
- wrk->last_vlib_us_time = wrk->last_vlib_time * CLIB_US_TIME_FREQ;
+ session_wrk_update_time (wrk, vlib_time_now (vm));
/*
* Update transport time
*/
transport_update_time (wrk->last_vlib_time, thread_index);
n_tx_packets = vec_len (wrk->pending_tx_buffers);
+ SESSION_EVT (SESSION_EVT_DSP_CNTRS, UPDATE_TIME, wrk);
/*
- * Dequeue and handle new events
+ * Dequeue new internal mq events
*/
- /* Try to dequeue what is available. Don't wait for lock.
- * XXX: we may need priorities here */
- mq = wrk->vpp_event_queue;
- n_to_dequeue = svm_msg_q_size (mq);
- if (n_to_dequeue && svm_msg_q_try_lock (mq) == 0)
- {
- for (i = 0; i < n_to_dequeue; i++)
- {
- svm_msg_q_sub_w_lock (mq, msg);
- evt = svm_msg_q_msg_data (mq, msg);
- session_evt_add_to_list (wrk, evt);
- svm_msg_q_free_msg (mq, msg);
- }
- svm_msg_q_unlock (mq);
- }
+ n_evts = session_wrk_handle_mq (wrk, wrk->vpp_event_queue);
+ SESSION_EVT (SESSION_EVT_DSP_CNTRS, MQ_DEQ, wrk, n_evts);
/*
* Handle control events
*/
- ctrl_he = pool_elt_at_index (wrk->event_elts, wrk->ctrl_head);
+ ei = wrk->ctrl_head;
+ ctrl_he = clib_llist_elt (wrk->event_elts, ei);
+ next_ei = clib_llist_next_index (ctrl_he, evt_list);
+ old_ti = clib_llist_prev_index (ctrl_he, evt_list);
+ while (ei != old_ti)
+ {
+ ei = next_ei;
+ elt = clib_llist_elt (wrk->event_elts, next_ei);
+ next_ei = clib_llist_next_index (elt, evt_list);
+ clib_llist_remove (wrk->event_elts, evt_list, elt);
+ session_event_dispatch_ctrl (wrk, elt);
+ }
- /* *INDENT-OFF* */
- clib_llist_foreach_safe (wrk->event_elts, evt_list, ctrl_he, elt, ({
- clib_llist_remove (wrk->event_elts, evt_list, elt);
- session_event_dispatch_ctrl (wrk, elt);
- }));
- /* *INDENT-ON* */
+ SESSION_EVT (SESSION_EVT_DSP_CNTRS, CTRL_EVTS, wrk);
/*
* Handle the new io events.
*/
- new_he = pool_elt_at_index (wrk->event_elts, wrk->new_head);
- old_he = pool_elt_at_index (wrk->event_elts, wrk->old_head);
+ new_he = clib_llist_elt (wrk->event_elts, wrk->new_head);
+ old_he = clib_llist_elt (wrk->event_elts, wrk->old_head);
old_ti = clib_llist_prev_index (old_he, evt_list);
ei = clib_llist_next_index (new_he, evt_list);
- while (ei != wrk->new_head && n_tx_packets < VLIB_FRAME_SIZE)
+ while (ei != wrk->new_head && n_tx_packets < SESSION_NODE_FRAME_SIZE)
{
- elt = pool_elt_at_index (wrk->event_elts, ei);
+ elt = clib_llist_elt (wrk->event_elts, ei);
ei = clib_llist_next_index (elt, evt_list);
clib_llist_remove (wrk->event_elts, evt_list, elt);
- session_event_dispatch_io (wrk, node, elt, thread_index, &n_tx_packets);
+ session_event_dispatch_io (wrk, node, elt, &n_tx_packets);
}
+ SESSION_EVT (SESSION_EVT_DSP_CNTRS, NEW_IO_EVTS, wrk);
+
/*
* Handle the old io events, if we had any prior to processing the new ones
*/
if (old_ti != wrk->old_head)
{
- old_he = pool_elt_at_index (wrk->event_elts, wrk->old_head);
+ old_he = clib_llist_elt (wrk->event_elts, wrk->old_head);
ei = clib_llist_next_index (old_he, evt_list);
- while (n_tx_packets < VLIB_FRAME_SIZE)
+ while (n_tx_packets < SESSION_NODE_FRAME_SIZE)
{
- elt = pool_elt_at_index (wrk->event_elts, ei);
+ elt = clib_llist_elt (wrk->event_elts, ei);
next_ei = clib_llist_next_index (elt, evt_list);
clib_llist_remove (wrk->event_elts, evt_list, elt);
- session_event_dispatch_io (wrk, node, elt, thread_index,
- &n_tx_packets);
+ session_event_dispatch_io (wrk, node, elt, &n_tx_packets);
if (ei == old_ti)
break;
};
}
+ SESSION_EVT (SESSION_EVT_DSP_CNTRS, OLD_IO_EVTS, wrk);
+
if (vec_len (wrk->pending_tx_buffers))
session_flush_pending_tx_buffers (wrk, node);
SESSION_EVT (SESSION_EVT_DISPATCH_END, wrk, n_tx_packets);
+ if (wrk->flags & SESSION_WRK_F_ADAPTIVE)
+ session_wrk_update_state (wrk);
+
return n_tx_packets;
}
};
/* *INDENT-ON* */
-void
-dump_thread_0_event_queue (void)
+static clib_error_t *
+session_wrk_tfd_read_ready (clib_file_t *cf)
{
- session_main_t *smm = vnet_get_session_main ();
- vlib_main_t *vm = &vlib_global_main;
- u32 my_thread_index = vm->thread_index;
- session_event_t _e, *e = &_e;
- svm_msg_q_ring_t *ring;
- session_t *s0;
- svm_msg_q_msg_t *msg;
- svm_msg_q_t *mq;
- int i, index;
-
- mq = smm->wrk[my_thread_index].vpp_event_queue;
- index = mq->q->head;
-
- for (i = 0; i < mq->q->cursize; i++)
- {
- msg = (svm_msg_q_msg_t *) (&mq->q->data[0] + mq->q->elsize * index);
- ring = svm_msg_q_ring (mq, msg->ring_index);
- clib_memcpy_fast (e, svm_msg_q_msg_data (mq, msg), ring->elsize);
-
- switch (e->event_type)
- {
- case SESSION_IO_EVT_TX:
- s0 = session_event_get_session (e, my_thread_index);
- fformat (stdout, "[%04d] TX session %d\n", i, s0->session_index);
- break;
-
- case SESSION_CTRL_EVT_CLOSE:
- s0 = session_get_from_handle (e->session_handle);
- fformat (stdout, "[%04d] disconnect session %d\n", i,
- s0->session_index);
- break;
-
- case SESSION_IO_EVT_BUILTIN_RX:
- s0 = session_event_get_session (e, my_thread_index);
- fformat (stdout, "[%04d] builtin_rx %d\n", i, s0->session_index);
- break;
-
- case SESSION_CTRL_EVT_RPC:
- fformat (stdout, "[%04d] RPC call %llx with %llx\n",
- i, (u64) (uword) (e->rpc_args.fp),
- (u64) (uword) (e->rpc_args.arg));
- break;
-
- default:
- fformat (stdout, "[%04d] unhandled event type %d\n",
- i, e->event_type);
- break;
- }
-
- index++;
+ session_worker_t *wrk = session_main_get_worker (cf->private_data);
+ u64 buf;
+ int rv;
- if (index == mq->q->maxsize)
- index = 0;
- }
+ vlib_node_set_interrupt_pending (wrk->vm, session_queue_node.index);
+ rv = read (wrk->timerfd, &buf, sizeof (buf));
+ if (rv < 0 && errno != EAGAIN)
+ clib_unix_warning ("failed");
+ return 0;
}
-static u8
-session_node_cmp_event (session_event_t * e, svm_fifo_t * f)
+static clib_error_t *
+session_wrk_tfd_write_ready (clib_file_t *cf)
{
- session_t *s;
- switch (e->event_type)
- {
- case SESSION_IO_EVT_RX:
- case SESSION_IO_EVT_TX:
- case SESSION_IO_EVT_BUILTIN_RX:
- case SESSION_IO_EVT_BUILTIN_TX:
- case SESSION_IO_EVT_TX_FLUSH:
- if (e->session_index == f->master_session_index)
- return 1;
- break;
- case SESSION_CTRL_EVT_CLOSE:
- break;
- case SESSION_CTRL_EVT_RPC:
- s = session_get_from_handle (e->session_handle);
- if (!s)
- {
- clib_warning ("session has event but doesn't exist!");
- break;
- }
- if (s->rx_fifo == f || s->tx_fifo == f)
- return 1;
- break;
- default:
- break;
- }
return 0;
}
-u8
-session_node_lookup_fifo_event (svm_fifo_t * f, session_event_t * e)
+void
+session_wrk_enable_adaptive_mode (session_worker_t *wrk)
{
- session_evt_elt_t *elt;
- session_worker_t *wrk;
- int i, index, found = 0;
- svm_msg_q_msg_t *msg;
- svm_msg_q_ring_t *ring;
- svm_msg_q_t *mq;
- u8 thread_index;
+ u32 thread_index = wrk->vm->thread_index;
+ clib_file_t template = { 0 };
- ASSERT (e);
- thread_index = f->master_thread_index;
- wrk = session_main_get_worker (thread_index);
+ if ((wrk->timerfd = timerfd_create (CLOCK_MONOTONIC, TFD_NONBLOCK)) < 0)
+ clib_warning ("timerfd_create");
- /*
- * Search evt queue
- */
- mq = wrk->vpp_event_queue;
- index = mq->q->head;
- for (i = 0; i < mq->q->cursize; i++)
- {
- msg = (svm_msg_q_msg_t *) (&mq->q->data[0] + mq->q->elsize * index);
- ring = svm_msg_q_ring (mq, msg->ring_index);
- clib_memcpy_fast (e, svm_msg_q_msg_data (mq, msg), ring->elsize);
- found = session_node_cmp_event (e, f);
- if (found)
- return 1;
- index = (index + 1) % mq->q->maxsize;
- }
- /*
- * Search pending events vector
- */
+ template.read_function = session_wrk_tfd_read_ready;
+ template.write_function = session_wrk_tfd_write_ready;
+ template.file_descriptor = wrk->timerfd;
+ template.private_data = thread_index;
+ template.polling_thread_index = thread_index;
+ template.description = format (0, "session-wrk-tfd-%u", thread_index);
- /* *INDENT-OFF* */
- clib_llist_foreach (wrk->event_elts, evt_list,
- pool_elt_at_index (wrk->event_elts, wrk->new_head),
- elt, ({
- found = session_node_cmp_event (&elt->evt, f);
- if (found)
- {
- clib_memcpy_fast (e, &elt->evt, sizeof (*e));
- goto done;
- }
- }));
- /* *INDENT-ON* */
-
- /* *INDENT-OFF* */
- clib_llist_foreach (wrk->event_elts, evt_list,
- pool_elt_at_index (wrk->event_elts, wrk->old_head),
- elt, ({
- found = session_node_cmp_event (&elt->evt, f);
- if (found)
- {
- clib_memcpy_fast (e, &elt->evt, sizeof (*e));
- goto done;
- }
- }));
- /* *INDENT-ON* */
-
-done:
- return found;
+ wrk->timerfd_file = clib_file_add (&file_main, &template);
+ wrk->flags |= SESSION_WRK_F_ADAPTIVE;
}
static clib_error_t *
session_queue_exit (vlib_main_t * vm)
{
- if (vec_len (vlib_mains) < 2)
+ if (vlib_get_n_threads () < 2)
return 0;
/*
VLIB_MAIN_LOOP_EXIT_FUNCTION (session_queue_exit);
+static uword
+session_queue_run_on_main (vlib_main_t * vm)
+{
+ vlib_node_runtime_t *node;
+
+ node = vlib_node_get_runtime (vm, session_queue_node.index);
+ return session_queue_node_fn (vm, node, 0);
+}
+
static uword
session_queue_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
vlib_frame_t * f)
{
- f64 now, timeout = 1.0;
uword *event_data = 0;
+ f64 timeout = 1.0;
uword 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);
switch (event_type)
{
- case SESSION_Q_PROCESS_FLUSH_FRAMES:
- /* Flush the frames by updating all transports times */
- transport_update_time (now, 0);
+ case SESSION_Q_PROCESS_RUN_ON_MAIN:
+ /* Run session queue node on main thread */
+ session_queue_run_on_main (vm);
break;
case SESSION_Q_PROCESS_STOP:
+ vlib_node_set_state (vm, session_queue_process_node.index,
+ VLIB_NODE_STATE_DISABLED);
timeout = 100000.0;
break;
case ~0:
- /* Timed out. Update time for all transports to trigger all
- * outstanding retransmits. */
- transport_update_time (now, 0);
+ /* Timed out. Run on main to ensure all events are handled */
+ session_queue_run_on_main (vm);
break;
}
vec_reset_length (event_data);