blob: 07eeae82915418db0b55e596ab9dc2eb4dffc025 [file] [log] [blame]
Dave Barach68b0fb02017-02-28 15:15:56 -05001/*
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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15
Florin Coras6792ec02017-03-13 03:49:51 -070016#include <math.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050017#include <vlib/vlib.h>
18#include <vnet/vnet.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050019#include <vnet/tcp/tcp.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050020#include <vppinfra/elog.h>
Florin Coras6792ec02017-03-13 03:49:51 -070021#include <vnet/session/application.h>
Florin Corase69f4952017-03-07 10:06:24 -080022#include <vnet/session/session_debug.h>
Florin Coras6792ec02017-03-13 03:49:51 -070023#include <vlibmemory/unix_shared_memory_queue.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050024
25vlib_node_registration_t session_queue_node;
26
27typedef struct
28{
29 u32 session_index;
30 u32 server_thread_index;
31} session_queue_trace_t;
32
33/* packet trace format function */
34static u8 *
35format_session_queue_trace (u8 * s, va_list * args)
36{
37 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
38 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
39 session_queue_trace_t *t = va_arg (*args, session_queue_trace_t *);
40
41 s = format (s, "SESSION_QUEUE: session index %d, server thread index %d",
42 t->session_index, t->server_thread_index);
43 return s;
44}
45
46vlib_node_registration_t session_queue_node;
47
Florin Coras6792ec02017-03-13 03:49:51 -070048#define foreach_session_queue_error \
49_(TX, "Packets transmitted") \
Florin Coras93992a92017-05-24 18:03:56 -070050_(TIMER, "Timer events") \
51_(NO_BUFFER, "Out of buffers")
Dave Barach68b0fb02017-02-28 15:15:56 -050052
53typedef enum
54{
55#define _(sym,str) SESSION_QUEUE_ERROR_##sym,
56 foreach_session_queue_error
57#undef _
58 SESSION_QUEUE_N_ERROR,
59} session_queue_error_t;
60
61static char *session_queue_error_strings[] = {
62#define _(sym,string) string,
63 foreach_session_queue_error
64#undef _
65};
66
67static u32 session_type_to_next[] = {
68 SESSION_QUEUE_NEXT_TCP_IP4_OUTPUT,
69 SESSION_QUEUE_NEXT_IP4_LOOKUP,
70 SESSION_QUEUE_NEXT_TCP_IP6_OUTPUT,
71 SESSION_QUEUE_NEXT_IP6_LOOKUP,
72};
73
Florin Corasf6d68ed2017-05-07 19:12:02 -070074always_inline void
75session_tx_fifo_chain_tail (session_manager_main_t * smm, vlib_main_t * vm,
76 u8 thread_index, svm_fifo_t * fifo,
77 vlib_buffer_t * b0, u32 bi0, u8 n_bufs_per_seg,
78 u32 * left_to_snd0, u16 * n_bufs, u32 * rx_offset,
79 u16 deq_per_buf, u8 peek_data)
80{
81 vlib_buffer_t *chain_b0, *prev_b0;
82 u32 chain_bi0;
83 u16 len_to_deq0, n_bytes_read;
84 u8 *data0, j;
85
86 chain_bi0 = bi0;
87 chain_b0 = b0;
88 for (j = 1; j < n_bufs_per_seg; j++)
89 {
90 prev_b0 = chain_b0;
91 len_to_deq0 = clib_min (*left_to_snd0, deq_per_buf);
92
93 *n_bufs -= 1;
94 chain_bi0 = smm->tx_buffers[thread_index][*n_bufs];
95 _vec_len (smm->tx_buffers[thread_index]) = *n_bufs;
96
97 chain_b0 = vlib_get_buffer (vm, chain_bi0);
98 chain_b0->current_data = 0;
99 data0 = vlib_buffer_get_current (chain_b0);
100 if (peek_data)
101 {
102 n_bytes_read = svm_fifo_peek (fifo, *rx_offset, len_to_deq0, data0);
103 *rx_offset += n_bytes_read;
104 }
105 else
106 {
107 n_bytes_read = svm_fifo_dequeue_nowait (fifo, len_to_deq0, data0);
108 }
109 ASSERT (n_bytes_read == len_to_deq0);
110 chain_b0->current_length = n_bytes_read;
111 b0->total_length_not_including_first_buffer += chain_b0->current_length;
112
113 /* update previous buffer */
114 prev_b0->next_buffer = chain_bi0;
115 prev_b0->flags |= VLIB_BUFFER_NEXT_PRESENT;
116
117 /* update current buffer */
118 chain_b0->next_buffer = 0;
119
120 *left_to_snd0 -= n_bytes_read;
121 if (*left_to_snd0 == 0)
122 break;
123 }
124}
125
Dave Barach68b0fb02017-02-28 15:15:56 -0500126always_inline int
Florin Corasd79b41e2017-03-04 05:37:52 -0800127session_tx_fifo_read_and_snd_i (vlib_main_t * vm, vlib_node_runtime_t * node,
128 session_manager_main_t * smm,
129 session_fifo_event_t * e0,
130 stream_session_t * s0, u32 thread_index,
131 int *n_tx_packets, u8 peek_data)
Dave Barach68b0fb02017-02-28 15:15:56 -0500132{
133 u32 n_trace = vlib_get_trace_count (vm, node);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700134 u32 left_to_snd0, max_len_to_snd0, len_to_deq0, snd_space0;
135 u32 n_bufs_per_evt, n_frames_per_evt;
Dave Barach68b0fb02017-02-28 15:15:56 -0500136 transport_connection_t *tc0;
137 transport_proto_vft_t *transport_vft;
138 u32 next_index, next0, *to_next, n_left_to_next, bi0;
139 vlib_buffer_t *b0;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700140 u32 rx_offset = 0, max_dequeue0, n_bytes_per_seg;
141 u16 snd_mss0, n_bufs_per_seg, n_bufs;
Dave Barach68b0fb02017-02-28 15:15:56 -0500142 u8 *data0;
Florin Coras6792ec02017-03-13 03:49:51 -0700143 int i, n_bytes_read;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700144 u32 n_bytes_per_buf, deq_per_buf;
Florin Coras93992a92017-05-24 18:03:56 -0700145 u32 buffers_allocated, buffers_allocated_this_call;
Dave Barach68b0fb02017-02-28 15:15:56 -0500146
147 next_index = next0 = session_type_to_next[s0->session_type];
148
149 transport_vft = session_get_transport_vft (s0->session_type);
150 tc0 = transport_vft->get_connection (s0->connection_index, thread_index);
151
152 /* Make sure we have space to send and there's something to dequeue */
Dave Barach68b0fb02017-02-28 15:15:56 -0500153 snd_mss0 = transport_vft->send_mss (tc0);
Florin Corasc8343412017-05-04 14:25:50 -0700154 snd_space0 = transport_vft->send_space (tc0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500155
Florin Corase04c2992017-03-01 08:17:34 -0800156 /* Can't make any progress */
Florin Coras6792ec02017-03-13 03:49:51 -0700157 if (snd_space0 == 0 || snd_mss0 == 0)
Florin Corase04c2992017-03-01 08:17:34 -0800158 {
Dave Barachacd2a6a2017-05-16 17:41:34 -0400159 vec_add1 (smm->pending_event_vector[thread_index], *e0);
Florin Corase04c2992017-03-01 08:17:34 -0800160 return 0;
161 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500162
Dave Barach68b0fb02017-02-28 15:15:56 -0500163 if (peek_data)
164 {
165 /* Offset in rx fifo from where to peek data */
Florin Corasd79b41e2017-03-04 05:37:52 -0800166 rx_offset = transport_vft->tx_fifo_offset (tc0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500167 }
168
Florin Coras6792ec02017-03-13 03:49:51 -0700169 /* Check how much we can pull. If buffering, subtract the offset */
170 max_dequeue0 = svm_fifo_max_dequeue (s0->server_tx_fifo) - rx_offset;
171
Florin Coras6792ec02017-03-13 03:49:51 -0700172 /* Nothing to read return */
173 if (max_dequeue0 == 0)
Florin Coras3e350af2017-03-30 02:54:28 -0700174 return 0;
Florin Coras6792ec02017-03-13 03:49:51 -0700175
176 /* Ensure we're not writing more than transport window allows */
Florin Coras3e350af2017-03-30 02:54:28 -0700177 if (max_dequeue0 < snd_space0)
178 {
179 /* Constrained by tx queue. Try to send only fully formed segments */
180 max_len_to_snd0 = (max_dequeue0 > snd_mss0) ?
181 max_dequeue0 - max_dequeue0 % snd_mss0 : max_dequeue0;
182 /* TODO Nagle ? */
183 }
184 else
185 {
186 max_len_to_snd0 = snd_space0;
187 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500188
Florin Coras93992a92017-05-24 18:03:56 -0700189 n_bytes_per_buf = vlib_buffer_free_list_buffer_size
190 (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700191 n_bytes_per_seg = MAX_HDRS_LEN + snd_mss0;
192 n_bufs_per_seg = ceil ((double) n_bytes_per_seg / n_bytes_per_buf);
193 n_bufs_per_evt = (ceil ((double) max_len_to_snd0 / n_bytes_per_seg))
194 * n_bufs_per_seg;
195 n_frames_per_evt = ceil ((double) n_bufs_per_evt / VLIB_FRAME_SIZE);
196
197 deq_per_buf = clib_min (snd_mss0, n_bytes_per_buf);
Dave Barach68b0fb02017-02-28 15:15:56 -0500198
199 n_bufs = vec_len (smm->tx_buffers[thread_index]);
200 left_to_snd0 = max_len_to_snd0;
201 for (i = 0; i < n_frames_per_evt; i++)
202 {
203 /* Make sure we have at least one full frame of buffers ready */
204 if (PREDICT_FALSE (n_bufs < VLIB_FRAME_SIZE))
205 {
206 vec_validate (smm->tx_buffers[thread_index],
Florin Coras93992a92017-05-24 18:03:56 -0700207 n_bufs + 2 * VLIB_FRAME_SIZE - 1);
Dave Barach68b0fb02017-02-28 15:15:56 -0500208
Florin Coras93992a92017-05-24 18:03:56 -0700209 buffers_allocated = 0;
210 do
Dave Barach68b0fb02017-02-28 15:15:56 -0500211 {
Florin Coras93992a92017-05-24 18:03:56 -0700212 buffers_allocated_this_call =
213 vlib_buffer_alloc
214 (vm,
215 &smm->tx_buffers[thread_index][n_bufs + buffers_allocated],
216 2 * VLIB_FRAME_SIZE - buffers_allocated);
217 buffers_allocated += buffers_allocated_this_call;
Dave Barach68b0fb02017-02-28 15:15:56 -0500218 }
Florin Coras93992a92017-05-24 18:03:56 -0700219 while (buffers_allocated_this_call > 0
220 && ((buffers_allocated + n_bufs < VLIB_FRAME_SIZE)));
221
222 n_bufs += buffers_allocated;
Dave Barach68b0fb02017-02-28 15:15:56 -0500223
224 _vec_len (smm->tx_buffers[thread_index]) = n_bufs;
Florin Coras93992a92017-05-24 18:03:56 -0700225
226 if (PREDICT_FALSE (n_bufs < VLIB_FRAME_SIZE))
227 {
228 vec_add1 (smm->pending_event_vector[thread_index], *e0);
229 return -1;
230 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500231 }
Florin Coras93992a92017-05-24 18:03:56 -0700232 /* Allow enqueuing of a new event */
233 svm_fifo_unset_event (s0->server_tx_fifo);
Dave Barach68b0fb02017-02-28 15:15:56 -0500234
235 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700236 while (left_to_snd0 && n_left_to_next >= n_bufs_per_seg)
Dave Barach68b0fb02017-02-28 15:15:56 -0500237 {
Florin Corasf6d68ed2017-05-07 19:12:02 -0700238 /*
239 * Handle first buffer in chain separately
240 */
241
Dave Barach68b0fb02017-02-28 15:15:56 -0500242 /* Get free buffer */
Florin Coras93992a92017-05-24 18:03:56 -0700243 ASSERT (n_bufs >= 1);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700244 bi0 = smm->tx_buffers[thread_index][--n_bufs];
Florin Coras93992a92017-05-24 18:03:56 -0700245 ASSERT (bi0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500246 _vec_len (smm->tx_buffers[thread_index]) = n_bufs;
247
248 b0 = vlib_get_buffer (vm, bi0);
249 b0->error = 0;
250 b0->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID
251 | VNET_BUFFER_LOCALLY_ORIGINATED;
252 b0->current_data = 0;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700253 b0->total_length_not_including_first_buffer = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500254
255 /* RX on the local interface. tx in default fib */
256 vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
257 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
258
Florin Corasf6d68ed2017-05-07 19:12:02 -0700259 len_to_deq0 = clib_min (left_to_snd0, deq_per_buf);
260
261 data0 = vlib_buffer_make_headroom (b0, MAX_HDRS_LEN);
262 if (peek_data)
263 {
264 n_bytes_read = svm_fifo_peek (s0->server_tx_fifo, rx_offset,
265 len_to_deq0, data0);
266 /* Keep track of progress locally, transport is also supposed to
267 * increment it independently when pushing the header */
268 rx_offset += n_bytes_read;
269 }
270 else
271 {
272 n_bytes_read = svm_fifo_dequeue_nowait (s0->server_tx_fifo,
273 len_to_deq0, data0);
274 }
275
276 if (n_bytes_read <= 0)
277 goto dequeue_fail;
278
279 b0->current_length = n_bytes_read;
280
281 left_to_snd0 -= n_bytes_read;
282 *n_tx_packets = *n_tx_packets + 1;
283
284 /*
285 * Fill in the remaining buffers in the chain, if any
286 */
287 if (PREDICT_FALSE (n_bufs_per_seg > 1))
288 session_tx_fifo_chain_tail (smm, vm, thread_index,
289 s0->server_tx_fifo, b0, bi0,
290 n_bufs_per_seg, &left_to_snd0,
291 &n_bufs, &rx_offset, deq_per_buf,
292 peek_data);
293
294 /* Ask transport to push header after current_length and
295 * total_length_not_including_first_buffer are updated */
296 transport_vft->push_header (tc0, b0);
297
298 /* *INDENT-OFF* */
299 SESSION_EVT_DBG(SESSION_EVT_DEQ, s0, ({
300 ed->data[0] = e0->event_id;
301 ed->data[1] = max_dequeue0;
302 ed->data[2] = len_to_deq0;
303 ed->data[3] = left_to_snd0;
304 }));
305 /* *INDENT-ON* */
306
Dave Barach68b0fb02017-02-28 15:15:56 -0500307 /* usual speculation, or the enqueue_x1 macro will barf */
308 to_next[0] = bi0;
309 to_next += 1;
310 n_left_to_next -= 1;
311
312 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
313 if (PREDICT_FALSE (n_trace > 0))
314 {
315 session_queue_trace_t *t0;
316 vlib_trace_buffer (vm, node, next_index, b0,
317 1 /* follow_chain */ );
318 vlib_set_trace_count (vm, node, --n_trace);
319 t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
320 t0->session_index = s0->session_index;
321 t0->server_thread_index = s0->thread_index;
322 }
323
Dave Barach68b0fb02017-02-28 15:15:56 -0500324 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
325 to_next, n_left_to_next,
326 bi0, next0);
327 }
328 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
329 }
330
Florin Coras6792ec02017-03-13 03:49:51 -0700331 /* If we couldn't dequeue all bytes mark as partially read */
332 if (max_len_to_snd0 < max_dequeue0)
Dave Barach68b0fb02017-02-28 15:15:56 -0500333 {
Florin Coras6792ec02017-03-13 03:49:51 -0700334 /* If we don't already have new event */
335 if (svm_fifo_set_event (s0->server_tx_fifo))
336 {
Dave Barachacd2a6a2017-05-16 17:41:34 -0400337 vec_add1 (smm->pending_event_vector[thread_index], *e0);
Florin Coras6792ec02017-03-13 03:49:51 -0700338 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500339 }
340 return 0;
341
342dequeue_fail:
Florin Coras6792ec02017-03-13 03:49:51 -0700343 /*
344 * Can't read from fifo. If we don't already have an event, save as partially
345 * read, return buff to free list and return
346 */
347 clib_warning ("dequeue fail");
Dave Barach68b0fb02017-02-28 15:15:56 -0500348
Florin Coras6792ec02017-03-13 03:49:51 -0700349 if (svm_fifo_set_event (s0->server_tx_fifo))
350 {
Dave Barachacd2a6a2017-05-16 17:41:34 -0400351 vec_add1 (smm->pending_event_vector[thread_index], *e0);
Florin Coras6792ec02017-03-13 03:49:51 -0700352 }
353 vlib_put_next_frame (vm, node, next_index, n_left_to_next + 1);
Dave Barach68b0fb02017-02-28 15:15:56 -0500354 _vec_len (smm->tx_buffers[thread_index]) += 1;
355
Dave Barach68b0fb02017-02-28 15:15:56 -0500356 return 0;
357}
358
359int
Florin Corasd79b41e2017-03-04 05:37:52 -0800360session_tx_fifo_peek_and_snd (vlib_main_t * vm, vlib_node_runtime_t * node,
361 session_manager_main_t * smm,
362 session_fifo_event_t * e0,
363 stream_session_t * s0, u32 thread_index,
364 int *n_tx_pkts)
Dave Barach68b0fb02017-02-28 15:15:56 -0500365{
Florin Corasd79b41e2017-03-04 05:37:52 -0800366 return session_tx_fifo_read_and_snd_i (vm, node, smm, e0, s0, thread_index,
367 n_tx_pkts, 1);
Dave Barach68b0fb02017-02-28 15:15:56 -0500368}
369
370int
Florin Corasd79b41e2017-03-04 05:37:52 -0800371session_tx_fifo_dequeue_and_snd (vlib_main_t * vm, vlib_node_runtime_t * node,
372 session_manager_main_t * smm,
373 session_fifo_event_t * e0,
374 stream_session_t * s0, u32 thread_index,
375 int *n_tx_pkts)
Dave Barach68b0fb02017-02-28 15:15:56 -0500376{
Florin Corasd79b41e2017-03-04 05:37:52 -0800377 return session_tx_fifo_read_and_snd_i (vm, node, smm, e0, s0, thread_index,
378 n_tx_pkts, 0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500379}
380
Florin Corasa5464812017-04-19 13:00:05 -0700381stream_session_t *
382session_event_get_session (session_fifo_event_t * e0, u8 thread_index)
383{
384 svm_fifo_t *f0;
385 stream_session_t *s0;
386 u32 session_index0;
387
388 f0 = e0->fifo;
389 session_index0 = f0->master_session_index;
390
391 /* $$$ add multiple event queues, per vpp worker thread */
392 ASSERT (f0->master_thread_index == thread_index);
393
394 s0 = stream_session_get_if_valid (session_index0, thread_index);
395
396 ASSERT (s0->thread_index == thread_index);
397
398 return s0;
399}
400
Dave Barachacd2a6a2017-05-16 17:41:34 -0400401void
402dump_thread_0_event_queue (void)
403{
404 session_manager_main_t *smm = vnet_get_session_manager_main ();
405 vlib_main_t *vm = &vlib_global_main;
406 u32 my_thread_index = vm->thread_index;
407 session_fifo_event_t _e, *e = &_e;
408 stream_session_t *s0;
409 int i, index;
410 i8 *headp;
411
412 unix_shared_memory_queue_t *q;
413 q = smm->vpp_event_queues[my_thread_index];
414
415 index = q->head;
416
417 for (i = 0; i < q->cursize; i++)
418 {
419 headp = (i8 *) (&q->data[0] + q->elsize * index);
420 clib_memcpy (e, headp, q->elsize);
421
422 switch (e->event_type)
423 {
424 case FIFO_EVENT_APP_TX:
425 s0 = session_event_get_session (e, my_thread_index);
426 fformat (stdout, "[%04d] TX session %d\n", i, s0->session_index);
427 break;
428
429 case FIFO_EVENT_DISCONNECT:
430 s0 = stream_session_get_from_handle (e->session_handle);
431 fformat (stdout, "[%04d] disconnect session %d\n", i,
432 s0->session_index);
433 break;
434
435 case FIFO_EVENT_BUILTIN_RX:
436 s0 = session_event_get_session (e, my_thread_index);
437 fformat (stdout, "[%04d] builtin_rx %d\n", i, s0->session_index);
438 break;
439
440 case FIFO_EVENT_RPC:
441 fformat (stdout, "[%04d] RPC call %llx with %llx\n",
442 i, (u64) (e->rpc_args.fp), (u64) (e->rpc_args.arg));
443 break;
444
445 default:
446 fformat (stdout, "[%04d] unhandled event type %d\n",
447 i, e->event_type);
448 break;
449 }
450
451 index++;
452
453 if (index == q->maxsize)
454 index = 0;
455 }
456}
457
Dave Barach68b0fb02017-02-28 15:15:56 -0500458static uword
459session_queue_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
460 vlib_frame_t * frame)
461{
462 session_manager_main_t *smm = vnet_get_session_manager_main ();
Dave Barachacd2a6a2017-05-16 17:41:34 -0400463 session_fifo_event_t *my_pending_event_vector, *e;
464 session_fifo_event_t *my_fifo_events;
Florin Corase04c2992017-03-01 08:17:34 -0800465 u32 n_to_dequeue, n_events;
Dave Barach68b0fb02017-02-28 15:15:56 -0500466 unix_shared_memory_queue_t *q;
Florin Coras6792ec02017-03-13 03:49:51 -0700467 application_t *app;
Dave Barach68b0fb02017-02-28 15:15:56 -0500468 int n_tx_packets = 0;
Damjan Marion586afd72017-04-05 19:18:20 +0200469 u32 my_thread_index = vm->thread_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500470 int i, rv;
Florin Coras3e350af2017-03-30 02:54:28 -0700471 f64 now = vlib_time_now (vm);
Dave Barache5f1d272017-05-10 13:34:04 -0400472 void (*fp) (void *);
Florin Coras3e350af2017-03-30 02:54:28 -0700473
474 SESSION_EVT_DBG (SESSION_EVT_POLL_GAP_TRACK, smm, my_thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500475
476 /*
477 * Update TCP time
478 */
Florin Coras3e350af2017-03-30 02:54:28 -0700479 tcp_update_time (now, my_thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500480
481 /*
482 * Get vpp queue events
483 */
484 q = smm->vpp_event_queues[my_thread_index];
485 if (PREDICT_FALSE (q == 0))
486 return 0;
487
Dave Barachacd2a6a2017-05-16 17:41:34 -0400488 my_fifo_events = smm->free_event_vector[my_thread_index];
489
Dave Barach68b0fb02017-02-28 15:15:56 -0500490 /* min number of events we can dequeue without blocking */
491 n_to_dequeue = q->cursize;
Dave Barachacd2a6a2017-05-16 17:41:34 -0400492 my_pending_event_vector = smm->pending_event_vector[my_thread_index];
Dave Barach68b0fb02017-02-28 15:15:56 -0500493
Dave Barachacd2a6a2017-05-16 17:41:34 -0400494 if (n_to_dequeue == 0 && vec_len (my_pending_event_vector) == 0)
Florin Corase04c2992017-03-01 08:17:34 -0800495 return 0;
496
Florin Coras6792ec02017-03-13 03:49:51 -0700497 SESSION_EVT_DBG (SESSION_EVT_DEQ_NODE, 0);
498
Florin Corase04c2992017-03-01 08:17:34 -0800499 /*
500 * If we didn't manage to process previous events try going
Dave Barach68b0fb02017-02-28 15:15:56 -0500501 * over them again without dequeuing new ones.
Florin Corase04c2992017-03-01 08:17:34 -0800502 */
503 /* XXX: Block senders to sessions that can't keep up */
Dave Barachacd2a6a2017-05-16 17:41:34 -0400504 if (0 && vec_len (my_pending_event_vector) >= 100)
Florin Coras6792ec02017-03-13 03:49:51 -0700505 {
506 clib_warning ("too many fifo events unsolved");
507 goto skip_dequeue;
508 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500509
510 /* See you in the next life, don't be late */
511 if (pthread_mutex_trylock (&q->mutex))
512 return 0;
513
514 for (i = 0; i < n_to_dequeue; i++)
515 {
516 vec_add2 (my_fifo_events, e, 1);
517 unix_shared_memory_queue_sub_raw (q, (u8 *) e);
518 }
519
520 /* The other side of the connection is not polling */
521 if (q->cursize < (q->maxsize / 8))
522 (void) pthread_cond_broadcast (&q->condvar);
523 pthread_mutex_unlock (&q->mutex);
524
Dave Barachacd2a6a2017-05-16 17:41:34 -0400525 vec_append (my_fifo_events, my_pending_event_vector);
526
527 _vec_len (my_pending_event_vector) = 0;
528 smm->pending_event_vector[my_thread_index] = my_pending_event_vector;
Dave Barach68b0fb02017-02-28 15:15:56 -0500529
530skip_dequeue:
Florin Corase04c2992017-03-01 08:17:34 -0800531 n_events = vec_len (my_fifo_events);
532 for (i = 0; i < n_events; i++)
Dave Barach68b0fb02017-02-28 15:15:56 -0500533 {
Florin Corasa5464812017-04-19 13:00:05 -0700534 stream_session_t *s0; /* $$$ prefetch 1 ahead maybe */
Dave Barach68b0fb02017-02-28 15:15:56 -0500535 session_fifo_event_t *e0;
536
537 e0 = &my_fifo_events[i];
Dave Barach68b0fb02017-02-28 15:15:56 -0500538
539 switch (e0->event_type)
540 {
Florin Corasa5464812017-04-19 13:00:05 -0700541 case FIFO_EVENT_APP_TX:
542 s0 = session_event_get_session (e0, my_thread_index);
543
544 if (CLIB_DEBUG && !s0)
545 {
546 clib_warning ("It's dead, Jim!");
547 continue;
548 }
549
550 if (PREDICT_FALSE (s0->session_state == SESSION_STATE_CLOSED))
551 continue;
Dave Barach68b0fb02017-02-28 15:15:56 -0500552 /* Spray packets in per session type frames, since they go to
553 * different nodes */
Florin Corase69f4952017-03-07 10:06:24 -0800554 rv = (smm->session_tx_fns[s0->session_type]) (vm, node, smm, e0, s0,
Dave Barach68b0fb02017-02-28 15:15:56 -0500555 my_thread_index,
556 &n_tx_packets);
Florin Coras6792ec02017-03-13 03:49:51 -0700557 /* Out of buffers */
Florin Coras93992a92017-05-24 18:03:56 -0700558 if (PREDICT_FALSE (rv < 0))
Dave Barachacd2a6a2017-05-16 17:41:34 -0400559 {
Florin Coras93992a92017-05-24 18:03:56 -0700560 vlib_node_increment_counter (vm, node->node_index,
561 SESSION_QUEUE_ERROR_NO_BUFFER, 1);
Dave Barachacd2a6a2017-05-16 17:41:34 -0400562 continue;
563 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500564 break;
Florin Corasa5464812017-04-19 13:00:05 -0700565 case FIFO_EVENT_DISCONNECT:
566 s0 = stream_session_get_from_handle (e0->session_handle);
Florin Coras6792ec02017-03-13 03:49:51 -0700567 stream_session_disconnect (s0);
568 break;
569 case FIFO_EVENT_BUILTIN_RX:
Florin Corasa5464812017-04-19 13:00:05 -0700570 s0 = session_event_get_session (e0, my_thread_index);
Florin Coras6792ec02017-03-13 03:49:51 -0700571 svm_fifo_unset_event (s0->server_rx_fifo);
572 /* Get session's server */
573 app = application_get (s0->app_index);
574 app->cb_fns.builtin_server_rx_callback (s0);
575 break;
Dave Barache5f1d272017-05-10 13:34:04 -0400576 case FIFO_EVENT_RPC:
577 fp = e0->rpc_args.fp;
578 (*fp) (e0->rpc_args.arg);
579 break;
580
Dave Barach68b0fb02017-02-28 15:15:56 -0500581 default:
582 clib_warning ("unhandled event type %d", e0->event_type);
583 }
584 }
585
Dave Barachacd2a6a2017-05-16 17:41:34 -0400586 _vec_len (my_fifo_events) = 0;
587 smm->free_event_vector[my_thread_index] = my_fifo_events;
Dave Barach68b0fb02017-02-28 15:15:56 -0500588
589 vlib_node_increment_counter (vm, session_queue_node.index,
590 SESSION_QUEUE_ERROR_TX, n_tx_packets);
591
Florin Coras6792ec02017-03-13 03:49:51 -0700592 SESSION_EVT_DBG (SESSION_EVT_DEQ_NODE, 1);
593
Dave Barach68b0fb02017-02-28 15:15:56 -0500594 return n_tx_packets;
595}
596
597/* *INDENT-OFF* */
598VLIB_REGISTER_NODE (session_queue_node) =
599{
600 .function = session_queue_node_fn,
601 .name = "session-queue",
602 .format_trace = format_session_queue_trace,
603 .type = VLIB_NODE_TYPE_INPUT,
604 .n_errors = ARRAY_LEN (session_queue_error_strings),
605 .error_strings = session_queue_error_strings,
606 .n_next_nodes = SESSION_QUEUE_N_NEXT,
Florin Corase04c2992017-03-01 08:17:34 -0800607 .state = VLIB_NODE_STATE_DISABLED,
Dave Barach68b0fb02017-02-28 15:15:56 -0500608 .next_nodes =
609 {
610 [SESSION_QUEUE_NEXT_DROP] = "error-drop",
611 [SESSION_QUEUE_NEXT_IP4_LOOKUP] = "ip4-lookup",
612 [SESSION_QUEUE_NEXT_IP6_LOOKUP] = "ip6-lookup",
613 [SESSION_QUEUE_NEXT_TCP_IP4_OUTPUT] = "tcp4-output",
614 [SESSION_QUEUE_NEXT_TCP_IP6_OUTPUT] = "tcp6-output",
615 },
616};
617/* *INDENT-ON* */
618
619/*
620 * fd.io coding-style-patch-verification: ON
621 *
622 * Local Variables:
623 * eval: (c-set-style "gnu")
624 * End:
625 */