summaryrefslogtreecommitdiffstats
path: root/src/vnet/session/node.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/vnet/session/node.c')
-rw-r--r--src/vnet/session/node.c435
1 files changed, 435 insertions, 0 deletions
diff --git a/src/vnet/session/node.c b/src/vnet/session/node.c
new file mode 100644
index 00000000000..e467f4e99e2
--- /dev/null
+++ b/src/vnet/session/node.c
@@ -0,0 +1,435 @@
+/*
+ * Copyright (c) 2017 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 <vlib/vlib.h>
+#include <vnet/vnet.h>
+#include <vnet/pg/pg.h>
+#include <vnet/ip/ip.h>
+
+#include <vnet/tcp/tcp.h>
+
+#include <vppinfra/hash.h>
+#include <vppinfra/error.h>
+#include <vppinfra/elog.h>
+#include <vlibmemory/unix_shared_memory_queue.h>
+
+#include <vnet/udp/udp_packet.h>
+#include <vnet/lisp-cp/packets.h>
+#include <math.h>
+
+vlib_node_registration_t session_queue_node;
+
+typedef struct
+{
+ u32 session_index;
+ u32 server_thread_index;
+} session_queue_trace_t;
+
+/* packet trace format function */
+static u8 *
+format_session_queue_trace (u8 * s, va_list * args)
+{
+ CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
+ CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
+ session_queue_trace_t *t = va_arg (*args, session_queue_trace_t *);
+
+ s = format (s, "SESSION_QUEUE: session index %d, server thread index %d",
+ t->session_index, t->server_thread_index);
+ return s;
+}
+
+vlib_node_registration_t session_queue_node;
+
+#define foreach_session_queue_error \
+_(TX, "Packets transmitted") \
+_(TIMER, "Timer events")
+
+typedef enum
+{
+#define _(sym,str) SESSION_QUEUE_ERROR_##sym,
+ foreach_session_queue_error
+#undef _
+ SESSION_QUEUE_N_ERROR,
+} session_queue_error_t;
+
+static char *session_queue_error_strings[] = {
+#define _(sym,string) string,
+ foreach_session_queue_error
+#undef _
+};
+
+static u32 session_type_to_next[] = {
+ SESSION_QUEUE_NEXT_TCP_IP4_OUTPUT,
+ SESSION_QUEUE_NEXT_IP4_LOOKUP,
+ SESSION_QUEUE_NEXT_TCP_IP6_OUTPUT,
+ SESSION_QUEUE_NEXT_IP6_LOOKUP,
+};
+
+always_inline int
+session_fifo_rx_i (vlib_main_t * vm, vlib_node_runtime_t * node,
+ session_manager_main_t * smm, session_fifo_event_t * e0,
+ stream_session_t * s0, u32 thread_index, int *n_tx_packets,
+ u8 peek_data)
+{
+ u32 n_trace = vlib_get_trace_count (vm, node);
+ u32 left_to_snd0, max_len_to_snd0, len_to_deq0, n_bufs, snd_space0;
+ u32 n_frame_bytes, n_frames_per_evt;
+ transport_connection_t *tc0;
+ transport_proto_vft_t *transport_vft;
+ u32 next_index, next0, *to_next, n_left_to_next, bi0;
+ vlib_buffer_t *b0;
+ u32 rx_offset;
+ u16 snd_mss0;
+ u8 *data0;
+ int i;
+
+ next_index = next0 = session_type_to_next[s0->session_type];
+
+ transport_vft = session_get_transport_vft (s0->session_type);
+ tc0 = transport_vft->get_connection (s0->connection_index, thread_index);
+
+ /* Make sure we have space to send and there's something to dequeue */
+ snd_space0 = transport_vft->send_space (tc0);
+ snd_mss0 = transport_vft->send_mss (tc0);
+
+ if (snd_space0 == 0 || svm_fifo_max_dequeue (s0->server_tx_fifo) == 0
+ || snd_mss0 == 0)
+ return 0;
+
+ ASSERT (e0->enqueue_length > 0);
+
+ /* Ensure we're not writing more than transport window allows */
+ max_len_to_snd0 = clib_min (e0->enqueue_length, snd_space0);
+
+ if (peek_data)
+ {
+ /* Offset in rx fifo from where to peek data */
+ rx_offset = transport_vft->rx_fifo_offset (tc0);
+ }
+
+ /* TODO check if transport is willing to send len_to_snd0
+ * bytes (Nagle) */
+
+ n_frame_bytes = snd_mss0 * VLIB_FRAME_SIZE;
+ n_frames_per_evt = ceil ((double) max_len_to_snd0 / n_frame_bytes);
+
+ n_bufs = vec_len (smm->tx_buffers[thread_index]);
+ left_to_snd0 = max_len_to_snd0;
+ for (i = 0; i < n_frames_per_evt; i++)
+ {
+ /* Make sure we have at least one full frame of buffers ready */
+ if (PREDICT_FALSE (n_bufs < VLIB_FRAME_SIZE))
+ {
+ vec_validate (smm->tx_buffers[thread_index],
+ n_bufs + VLIB_FRAME_SIZE - 1);
+ n_bufs +=
+ vlib_buffer_alloc (vm, &smm->tx_buffers[thread_index][n_bufs],
+ VLIB_FRAME_SIZE);
+
+ /* buffer shortage
+ * XXX 0.9 because when debugging we might not get a full frame */
+ if (PREDICT_FALSE (n_bufs < 0.9 * VLIB_FRAME_SIZE))
+ {
+ /* Keep track of how much we've dequeued and exit */
+ e0->enqueue_length -= max_len_to_snd0 - left_to_snd0;
+ return -1;
+ }
+
+ _vec_len (smm->tx_buffers[thread_index]) = n_bufs;
+ }
+
+ vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
+ while (left_to_snd0 && n_left_to_next)
+ {
+ /* Get free buffer */
+ n_bufs--;
+ bi0 = smm->tx_buffers[thread_index][n_bufs];
+ _vec_len (smm->tx_buffers[thread_index]) = n_bufs;
+
+ b0 = vlib_get_buffer (vm, bi0);
+ b0->error = 0;
+ b0->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID
+ | VNET_BUFFER_LOCALLY_ORIGINATED;
+ b0->current_data = 0;
+
+ /* RX on the local interface. tx in default fib */
+ vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
+ vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
+
+ /* usual speculation, or the enqueue_x1 macro will barf */
+ to_next[0] = bi0;
+ to_next += 1;
+ n_left_to_next -= 1;
+
+ VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
+ if (PREDICT_FALSE (n_trace > 0))
+ {
+ session_queue_trace_t *t0;
+ vlib_trace_buffer (vm, node, next_index, b0,
+ 1 /* follow_chain */ );
+ vlib_set_trace_count (vm, node, --n_trace);
+ t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
+ t0->session_index = s0->session_index;
+ t0->server_thread_index = s0->thread_index;
+ }
+
+ if (1)
+ {
+ ELOG_TYPE_DECLARE (e) =
+ {
+ .format = "evt-dequeue: id %d length %d",.format_args =
+ "i4i4",};
+ struct
+ {
+ u32 data[2];
+ } *ed;
+ ed = ELOG_DATA (&vm->elog_main, e);
+ ed->data[0] = e0->event_id;
+ ed->data[1] = e0->enqueue_length;
+ }
+
+ len_to_deq0 = (left_to_snd0 < snd_mss0) ? left_to_snd0 : snd_mss0;
+
+ /* Make room for headers */
+ data0 = vlib_buffer_make_headroom (b0, MAX_HDRS_LEN);
+
+ /* Dequeue the data
+ * TODO 1) peek instead of dequeue
+ * 2) buffer chains */
+ if (peek_data)
+ {
+ int n_bytes_read;
+ n_bytes_read = svm_fifo_peek (s0->server_tx_fifo, s0->pid,
+ rx_offset, len_to_deq0, data0);
+ if (n_bytes_read < 0)
+ goto dequeue_fail;
+
+ /* Keep track of progress locally, transport is also supposed to
+ * increment it independently when pushing header */
+ rx_offset += n_bytes_read;
+ }
+ else
+ {
+ if (svm_fifo_dequeue_nowait (s0->server_tx_fifo, s0->pid,
+ len_to_deq0, data0) < 0)
+ goto dequeue_fail;
+ }
+
+ b0->current_length = len_to_deq0;
+
+ /* Ask transport to push header */
+ transport_vft->push_header (tc0, b0);
+
+ left_to_snd0 -= len_to_deq0;
+ *n_tx_packets = *n_tx_packets + 1;
+
+ vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
+ to_next, n_left_to_next,
+ bi0, next0);
+ }
+ vlib_put_next_frame (vm, node, next_index, n_left_to_next);
+ }
+
+ /* If we couldn't dequeue all bytes store progress */
+ if (max_len_to_snd0 < e0->enqueue_length)
+ {
+ e0->enqueue_length -= max_len_to_snd0;
+ vec_add1 (smm->evts_partially_read[thread_index], *e0);
+ }
+ return 0;
+
+dequeue_fail:
+ /* Can't read from fifo. Store event rx progress, save as partially read,
+ * return buff to free list and return */
+ e0->enqueue_length -= max_len_to_snd0 - left_to_snd0;
+ vec_add1 (smm->evts_partially_read[thread_index], *e0);
+
+ to_next -= 1;
+ n_left_to_next += 1;
+ _vec_len (smm->tx_buffers[thread_index]) += 1;
+
+ clib_warning ("dequeue fail");
+ return 0;
+}
+
+int
+session_fifo_rx_peek (vlib_main_t * vm, vlib_node_runtime_t * node,
+ session_manager_main_t * smm, session_fifo_event_t * e0,
+ stream_session_t * s0, u32 thread_index, int *n_tx_pkts)
+{
+ return session_fifo_rx_i (vm, node, smm, e0, s0, thread_index, n_tx_pkts,
+ 1);
+}
+
+int
+session_fifo_rx_dequeue (vlib_main_t * vm, vlib_node_runtime_t * node,
+ session_manager_main_t * smm,
+ session_fifo_event_t * e0, stream_session_t * s0,
+ u32 thread_index, int *n_tx_pkts)
+{
+ return session_fifo_rx_i (vm, node, smm, e0, s0, thread_index, n_tx_pkts,
+ 0);
+}
+
+static uword
+session_queue_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
+ vlib_frame_t * frame)
+{
+ session_manager_main_t *smm = vnet_get_session_manager_main ();
+ session_fifo_event_t *my_fifo_events, *e;
+ u32 n_to_dequeue;
+ unix_shared_memory_queue_t *q;
+ int n_tx_packets = 0;
+ u32 my_thread_index = vm->cpu_index;
+ int i, rv;
+
+ /*
+ * Update TCP time
+ */
+ tcp_update_time (vlib_time_now (vm), my_thread_index);
+
+ /*
+ * Get vpp queue events
+ */
+ q = smm->vpp_event_queues[my_thread_index];
+ if (PREDICT_FALSE (q == 0))
+ return 0;
+
+ /* min number of events we can dequeue without blocking */
+ n_to_dequeue = q->cursize;
+ if (n_to_dequeue == 0)
+ return 0;
+
+ my_fifo_events = smm->fifo_events[my_thread_index];
+
+ /* If we didn't manage to process previous events try going
+ * over them again without dequeuing new ones.
+ * XXX: Block senders to sessions that can't keep up */
+ if (vec_len (my_fifo_events) >= 100)
+ goto skip_dequeue;
+
+ /* See you in the next life, don't be late */
+ if (pthread_mutex_trylock (&q->mutex))
+ return 0;
+
+ for (i = 0; i < n_to_dequeue; i++)
+ {
+ vec_add2 (my_fifo_events, e, 1);
+ unix_shared_memory_queue_sub_raw (q, (u8 *) e);
+ }
+
+ /* The other side of the connection is not polling */
+ if (q->cursize < (q->maxsize / 8))
+ (void) pthread_cond_broadcast (&q->condvar);
+ pthread_mutex_unlock (&q->mutex);
+
+ smm->fifo_events[my_thread_index] = my_fifo_events;
+
+skip_dequeue:
+
+ for (i = 0; i < n_to_dequeue; i++)
+ {
+ svm_fifo_t *f0; /* $$$ prefetch 1 ahead maybe */
+ stream_session_t *s0;
+ u32 server_session_index0, server_thread_index0;
+ session_fifo_event_t *e0;
+
+ e0 = &my_fifo_events[i];
+ f0 = e0->fifo;
+ server_session_index0 = f0->server_session_index;
+ server_thread_index0 = f0->server_thread_index;
+
+ /* $$$ add multiple event queues, per vpp worker thread */
+ ASSERT (server_thread_index0 == my_thread_index);
+
+ s0 = pool_elt_at_index (smm->sessions[my_thread_index],
+ server_session_index0);
+
+ ASSERT (s0->thread_index == my_thread_index);
+
+ switch (e0->event_type)
+ {
+ case FIFO_EVENT_SERVER_TX:
+ /* Spray packets in per session type frames, since they go to
+ * different nodes */
+ rv = (smm->session_rx_fns[s0->session_type]) (vm, node, smm, e0, s0,
+ my_thread_index,
+ &n_tx_packets);
+ if (rv < 0)
+ goto done;
+
+ break;
+
+ default:
+ clib_warning ("unhandled event type %d", e0->event_type);
+ }
+ }
+
+done:
+
+ /* Couldn't process all events. Probably out of buffers */
+ if (PREDICT_FALSE (i < n_to_dequeue))
+ {
+ session_fifo_event_t *partially_read =
+ smm->evts_partially_read[my_thread_index];
+ vec_add (partially_read, &my_fifo_events[i], n_to_dequeue - i);
+ vec_free (my_fifo_events);
+ smm->fifo_events[my_thread_index] = partially_read;
+ smm->evts_partially_read[my_thread_index] = 0;
+ }
+ else
+ {
+ vec_free (smm->fifo_events[my_thread_index]);
+ smm->fifo_events[my_thread_index] =
+ smm->evts_partially_read[my_thread_index];
+ smm->evts_partially_read[my_thread_index] = 0;
+ }
+
+ vlib_node_increment_counter (vm, session_queue_node.index,
+ SESSION_QUEUE_ERROR_TX, n_tx_packets);
+
+ return n_tx_packets;
+}
+
+/* *INDENT-OFF* */
+VLIB_REGISTER_NODE (session_queue_node) =
+{
+ .function = session_queue_node_fn,
+ .name = "session-queue",
+ .format_trace = format_session_queue_trace,
+ .type = VLIB_NODE_TYPE_INPUT,
+ .n_errors = ARRAY_LEN (session_queue_error_strings),
+ .error_strings = session_queue_error_strings,
+ .n_next_nodes = SESSION_QUEUE_N_NEXT,
+ /* .state = VLIB_NODE_STATE_DISABLED, enable on-demand? */
+ /* edit / add dispositions here */
+ .next_nodes =
+ {
+ [SESSION_QUEUE_NEXT_DROP] = "error-drop",
+ [SESSION_QUEUE_NEXT_IP4_LOOKUP] = "ip4-lookup",
+ [SESSION_QUEUE_NEXT_IP6_LOOKUP] = "ip6-lookup",
+ [SESSION_QUEUE_NEXT_TCP_IP4_OUTPUT] = "tcp4-output",
+ [SESSION_QUEUE_NEXT_TCP_IP6_OUTPUT] = "tcp6-output",
+ },
+};
+/* *INDENT-ON* */
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */