2 * Copyright (c) 2017 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <vlib/vlib.h>
17 #include <vnet/vnet.h>
18 #include <vnet/pg/pg.h>
19 #include <vnet/ip/ip.h>
21 #include <vnet/tcp/tcp.h>
23 #include <vppinfra/hash.h>
24 #include <vppinfra/error.h>
25 #include <vppinfra/elog.h>
26 #include <vlibmemory/unix_shared_memory_queue.h>
28 #include <vnet/udp/udp_packet.h>
29 #include <vnet/lisp-cp/packets.h>
32 vlib_node_registration_t session_queue_node;
37 u32 server_thread_index;
38 } session_queue_trace_t;
40 /* packet trace format function */
42 format_session_queue_trace (u8 * s, va_list * args)
44 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
45 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
46 session_queue_trace_t *t = va_arg (*args, session_queue_trace_t *);
48 s = format (s, "SESSION_QUEUE: session index %d, server thread index %d",
49 t->session_index, t->server_thread_index);
53 vlib_node_registration_t session_queue_node;
55 #define foreach_session_queue_error \
56 _(TX, "Packets transmitted") \
57 _(TIMER, "Timer events")
61 #define _(sym,str) SESSION_QUEUE_ERROR_##sym,
62 foreach_session_queue_error
64 SESSION_QUEUE_N_ERROR,
65 } session_queue_error_t;
67 static char *session_queue_error_strings[] = {
68 #define _(sym,string) string,
69 foreach_session_queue_error
73 static u32 session_type_to_next[] = {
74 SESSION_QUEUE_NEXT_TCP_IP4_OUTPUT,
75 SESSION_QUEUE_NEXT_IP4_LOOKUP,
76 SESSION_QUEUE_NEXT_TCP_IP6_OUTPUT,
77 SESSION_QUEUE_NEXT_IP6_LOOKUP,
81 session_fifo_rx_i (vlib_main_t * vm, vlib_node_runtime_t * node,
82 session_manager_main_t * smm, session_fifo_event_t * e0,
83 stream_session_t * s0, u32 thread_index, int *n_tx_packets,
86 u32 n_trace = vlib_get_trace_count (vm, node);
87 u32 left_to_snd0, max_len_to_snd0, len_to_deq0, n_bufs, snd_space0;
88 u32 n_frame_bytes, n_frames_per_evt;
89 transport_connection_t *tc0;
90 transport_proto_vft_t *transport_vft;
91 u32 next_index, next0, *to_next, n_left_to_next, bi0;
98 next_index = next0 = session_type_to_next[s0->session_type];
100 transport_vft = session_get_transport_vft (s0->session_type);
101 tc0 = transport_vft->get_connection (s0->connection_index, thread_index);
103 /* Make sure we have space to send and there's something to dequeue */
104 snd_space0 = transport_vft->send_space (tc0);
105 snd_mss0 = transport_vft->send_mss (tc0);
107 /* Can't make any progress */
108 if (snd_space0 == 0 || svm_fifo_max_dequeue (s0->server_tx_fifo) == 0
111 vec_add1 (smm->evts_partially_read[thread_index], *e0);
115 ASSERT (e0->enqueue_length > 0);
117 /* Ensure we're not writing more than transport window allows */
118 max_len_to_snd0 = clib_min (e0->enqueue_length, snd_space0);
122 /* Offset in rx fifo from where to peek data */
123 rx_offset = transport_vft->rx_fifo_offset (tc0);
126 /* TODO check if transport is willing to send len_to_snd0
129 n_frame_bytes = snd_mss0 * VLIB_FRAME_SIZE;
130 n_frames_per_evt = ceil ((double) max_len_to_snd0 / n_frame_bytes);
132 n_bufs = vec_len (smm->tx_buffers[thread_index]);
133 left_to_snd0 = max_len_to_snd0;
134 for (i = 0; i < n_frames_per_evt; i++)
136 /* Make sure we have at least one full frame of buffers ready */
137 if (PREDICT_FALSE (n_bufs < VLIB_FRAME_SIZE))
139 vec_validate (smm->tx_buffers[thread_index],
140 n_bufs + VLIB_FRAME_SIZE - 1);
142 vlib_buffer_alloc (vm, &smm->tx_buffers[thread_index][n_bufs],
146 * XXX 0.9 because when debugging we might not get a full frame */
147 if (PREDICT_FALSE (n_bufs < 0.9 * VLIB_FRAME_SIZE))
149 /* Keep track of how much we've dequeued and exit */
150 if (left_to_snd0 != max_len_to_snd0)
152 e0->enqueue_length -= max_len_to_snd0 - left_to_snd0;
153 vec_add1 (smm->evts_partially_read[thread_index], *e0);
159 _vec_len (smm->tx_buffers[thread_index]) = n_bufs;
162 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
163 while (left_to_snd0 && n_left_to_next)
165 /* Get free buffer */
167 bi0 = smm->tx_buffers[thread_index][n_bufs];
168 _vec_len (smm->tx_buffers[thread_index]) = n_bufs;
170 b0 = vlib_get_buffer (vm, bi0);
172 b0->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID
173 | VNET_BUFFER_LOCALLY_ORIGINATED;
174 b0->current_data = 0;
176 /* RX on the local interface. tx in default fib */
177 vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
178 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
180 /* usual speculation, or the enqueue_x1 macro will barf */
185 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
186 if (PREDICT_FALSE (n_trace > 0))
188 session_queue_trace_t *t0;
189 vlib_trace_buffer (vm, node, next_index, b0,
190 1 /* follow_chain */ );
191 vlib_set_trace_count (vm, node, --n_trace);
192 t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
193 t0->session_index = s0->session_index;
194 t0->server_thread_index = s0->thread_index;
200 ELOG_TYPE_DECLARE (e) = {
201 .format = "evt-dequeue: id %d length %d",
202 .format_args = "i4i4",
208 ed = ELOG_DATA (&vm->elog_main, e);
209 ed->data[0] = e0->event_id;
210 ed->data[1] = e0->enqueue_length;
214 len_to_deq0 = (left_to_snd0 < snd_mss0) ? left_to_snd0 : snd_mss0;
216 /* Make room for headers */
217 data0 = vlib_buffer_make_headroom (b0, MAX_HDRS_LEN);
220 * TODO 1) peek instead of dequeue
221 * 2) buffer chains */
225 n_bytes_read = svm_fifo_peek (s0->server_tx_fifo, s0->pid,
226 rx_offset, len_to_deq0, data0);
227 if (n_bytes_read < 0)
230 /* Keep track of progress locally, transport is also supposed to
231 * increment it independently when pushing header */
232 rx_offset += n_bytes_read;
236 if (svm_fifo_dequeue_nowait (s0->server_tx_fifo, s0->pid,
237 len_to_deq0, data0) < 0)
241 b0->current_length = len_to_deq0;
243 /* Ask transport to push header */
244 transport_vft->push_header (tc0, b0);
246 left_to_snd0 -= len_to_deq0;
247 *n_tx_packets = *n_tx_packets + 1;
249 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
250 to_next, n_left_to_next,
253 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
256 /* If we couldn't dequeue all bytes store progress */
257 if (max_len_to_snd0 < e0->enqueue_length)
259 e0->enqueue_length -= max_len_to_snd0;
260 vec_add1 (smm->evts_partially_read[thread_index], *e0);
265 /* Can't read from fifo. Store event rx progress, save as partially read,
266 * return buff to free list and return */
267 e0->enqueue_length -= max_len_to_snd0 - left_to_snd0;
268 vec_add1 (smm->evts_partially_read[thread_index], *e0);
272 _vec_len (smm->tx_buffers[thread_index]) += 1;
274 clib_warning ("dequeue fail");
279 session_fifo_rx_peek (vlib_main_t * vm, vlib_node_runtime_t * node,
280 session_manager_main_t * smm, session_fifo_event_t * e0,
281 stream_session_t * s0, u32 thread_index, int *n_tx_pkts)
283 return session_fifo_rx_i (vm, node, smm, e0, s0, thread_index, n_tx_pkts,
288 session_fifo_rx_dequeue (vlib_main_t * vm, vlib_node_runtime_t * node,
289 session_manager_main_t * smm,
290 session_fifo_event_t * e0, stream_session_t * s0,
291 u32 thread_index, int *n_tx_pkts)
293 return session_fifo_rx_i (vm, node, smm, e0, s0, thread_index, n_tx_pkts,
298 session_queue_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
299 vlib_frame_t * frame)
301 session_manager_main_t *smm = vnet_get_session_manager_main ();
302 session_fifo_event_t *my_fifo_events, *e;
303 u32 n_to_dequeue, n_events;
304 unix_shared_memory_queue_t *q;
305 int n_tx_packets = 0;
306 u32 my_thread_index = vm->cpu_index;
312 tcp_update_time (vlib_time_now (vm), my_thread_index);
315 * Get vpp queue events
317 q = smm->vpp_event_queues[my_thread_index];
318 if (PREDICT_FALSE (q == 0))
321 /* min number of events we can dequeue without blocking */
322 n_to_dequeue = q->cursize;
323 my_fifo_events = smm->fifo_events[my_thread_index];
325 if (n_to_dequeue == 0 && vec_len (my_fifo_events) == 0)
329 * If we didn't manage to process previous events try going
330 * over them again without dequeuing new ones.
332 /* XXX: Block senders to sessions that can't keep up */
333 if (vec_len (my_fifo_events) >= 100)
336 /* See you in the next life, don't be late */
337 if (pthread_mutex_trylock (&q->mutex))
340 for (i = 0; i < n_to_dequeue; i++)
342 vec_add2 (my_fifo_events, e, 1);
343 unix_shared_memory_queue_sub_raw (q, (u8 *) e);
346 /* The other side of the connection is not polling */
347 if (q->cursize < (q->maxsize / 8))
348 (void) pthread_cond_broadcast (&q->condvar);
349 pthread_mutex_unlock (&q->mutex);
351 smm->fifo_events[my_thread_index] = my_fifo_events;
354 n_events = vec_len (my_fifo_events);
355 for (i = 0; i < n_events; i++)
357 svm_fifo_t *f0; /* $$$ prefetch 1 ahead maybe */
358 stream_session_t *s0;
359 u32 server_session_index0, server_thread_index0;
360 session_fifo_event_t *e0;
362 e0 = &my_fifo_events[i];
364 server_session_index0 = f0->server_session_index;
365 server_thread_index0 = f0->server_thread_index;
367 /* $$$ add multiple event queues, per vpp worker thread */
368 ASSERT (server_thread_index0 == my_thread_index);
370 s0 = stream_session_get_if_valid (server_session_index0,
374 clib_warning ("It's dead Jim!");
378 ASSERT (s0->thread_index == my_thread_index);
380 switch (e0->event_type)
382 case FIFO_EVENT_SERVER_TX:
383 /* Spray packets in per session type frames, since they go to
385 rv = (smm->session_rx_fns[s0->session_type]) (vm, node, smm, e0, s0,
394 clib_warning ("unhandled event type %d", e0->event_type);
400 /* Couldn't process all events. Probably out of buffers */
401 if (PREDICT_FALSE (i < n_events))
403 session_fifo_event_t *partially_read =
404 smm->evts_partially_read[my_thread_index];
405 vec_add (partially_read, &my_fifo_events[i], n_events - i);
406 vec_free (my_fifo_events);
407 smm->fifo_events[my_thread_index] = partially_read;
408 smm->evts_partially_read[my_thread_index] = 0;
412 vec_free (smm->fifo_events[my_thread_index]);
413 smm->fifo_events[my_thread_index] =
414 smm->evts_partially_read[my_thread_index];
415 smm->evts_partially_read[my_thread_index] = 0;
418 vlib_node_increment_counter (vm, session_queue_node.index,
419 SESSION_QUEUE_ERROR_TX, n_tx_packets);
425 VLIB_REGISTER_NODE (session_queue_node) =
427 .function = session_queue_node_fn,
428 .name = "session-queue",
429 .format_trace = format_session_queue_trace,
430 .type = VLIB_NODE_TYPE_INPUT,
431 .n_errors = ARRAY_LEN (session_queue_error_strings),
432 .error_strings = session_queue_error_strings,
433 .n_next_nodes = SESSION_QUEUE_N_NEXT,
434 .state = VLIB_NODE_STATE_DISABLED,
437 [SESSION_QUEUE_NEXT_DROP] = "error-drop",
438 [SESSION_QUEUE_NEXT_IP4_LOOKUP] = "ip4-lookup",
439 [SESSION_QUEUE_NEXT_IP6_LOOKUP] = "ip6-lookup",
440 [SESSION_QUEUE_NEXT_TCP_IP4_OUTPUT] = "tcp4-output",
441 [SESSION_QUEUE_NEXT_TCP_IP6_OUTPUT] = "tcp6-output",
447 * fd.io coding-style-patch-verification: ON
450 * eval: (c-set-style "gnu")