blob: c0ab1bf096fd14a0a5474cc5edbe7f5772781c6a [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 Corasf03a59a2017-06-09 21:07:32 -0700174 {
175 svm_fifo_unset_event (s0->server_tx_fifo);
176 return 0;
177 }
Florin Coras6792ec02017-03-13 03:49:51 -0700178
179 /* Ensure we're not writing more than transport window allows */
Florin Coras3e350af2017-03-30 02:54:28 -0700180 if (max_dequeue0 < snd_space0)
181 {
182 /* Constrained by tx queue. Try to send only fully formed segments */
183 max_len_to_snd0 = (max_dequeue0 > snd_mss0) ?
184 max_dequeue0 - max_dequeue0 % snd_mss0 : max_dequeue0;
185 /* TODO Nagle ? */
186 }
187 else
188 {
189 max_len_to_snd0 = snd_space0;
190 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500191
Florin Coras93992a92017-05-24 18:03:56 -0700192 n_bytes_per_buf = vlib_buffer_free_list_buffer_size
193 (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700194 n_bytes_per_seg = MAX_HDRS_LEN + snd_mss0;
195 n_bufs_per_seg = ceil ((double) n_bytes_per_seg / n_bytes_per_buf);
196 n_bufs_per_evt = (ceil ((double) max_len_to_snd0 / n_bytes_per_seg))
197 * n_bufs_per_seg;
198 n_frames_per_evt = ceil ((double) n_bufs_per_evt / VLIB_FRAME_SIZE);
199
200 deq_per_buf = clib_min (snd_mss0, n_bytes_per_buf);
Dave Barach68b0fb02017-02-28 15:15:56 -0500201
202 n_bufs = vec_len (smm->tx_buffers[thread_index]);
203 left_to_snd0 = max_len_to_snd0;
204 for (i = 0; i < n_frames_per_evt; i++)
205 {
206 /* Make sure we have at least one full frame of buffers ready */
207 if (PREDICT_FALSE (n_bufs < VLIB_FRAME_SIZE))
208 {
209 vec_validate (smm->tx_buffers[thread_index],
Florin Coras93992a92017-05-24 18:03:56 -0700210 n_bufs + 2 * VLIB_FRAME_SIZE - 1);
Dave Barach68b0fb02017-02-28 15:15:56 -0500211
Florin Coras93992a92017-05-24 18:03:56 -0700212 buffers_allocated = 0;
213 do
Dave Barach68b0fb02017-02-28 15:15:56 -0500214 {
Florin Coras93992a92017-05-24 18:03:56 -0700215 buffers_allocated_this_call =
216 vlib_buffer_alloc
217 (vm,
218 &smm->tx_buffers[thread_index][n_bufs + buffers_allocated],
219 2 * VLIB_FRAME_SIZE - buffers_allocated);
220 buffers_allocated += buffers_allocated_this_call;
Dave Barach68b0fb02017-02-28 15:15:56 -0500221 }
Florin Coras93992a92017-05-24 18:03:56 -0700222 while (buffers_allocated_this_call > 0
223 && ((buffers_allocated + n_bufs < VLIB_FRAME_SIZE)));
224
225 n_bufs += buffers_allocated;
Dave Barach68b0fb02017-02-28 15:15:56 -0500226
227 _vec_len (smm->tx_buffers[thread_index]) = n_bufs;
Florin Coras93992a92017-05-24 18:03:56 -0700228
229 if (PREDICT_FALSE (n_bufs < VLIB_FRAME_SIZE))
230 {
231 vec_add1 (smm->pending_event_vector[thread_index], *e0);
232 return -1;
233 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500234 }
Florin Coras93992a92017-05-24 18:03:56 -0700235 /* Allow enqueuing of a new event */
236 svm_fifo_unset_event (s0->server_tx_fifo);
Dave Barach68b0fb02017-02-28 15:15:56 -0500237
238 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700239 while (left_to_snd0 && n_left_to_next >= n_bufs_per_seg)
Dave Barach68b0fb02017-02-28 15:15:56 -0500240 {
Florin Corasf6d68ed2017-05-07 19:12:02 -0700241 /*
242 * Handle first buffer in chain separately
243 */
244
Dave Barach68b0fb02017-02-28 15:15:56 -0500245 /* Get free buffer */
Florin Coras93992a92017-05-24 18:03:56 -0700246 ASSERT (n_bufs >= 1);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700247 bi0 = smm->tx_buffers[thread_index][--n_bufs];
Florin Coras93992a92017-05-24 18:03:56 -0700248 ASSERT (bi0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500249 _vec_len (smm->tx_buffers[thread_index]) = n_bufs;
250
251 b0 = vlib_get_buffer (vm, bi0);
252 b0->error = 0;
253 b0->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID
254 | VNET_BUFFER_LOCALLY_ORIGINATED;
255 b0->current_data = 0;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700256 b0->total_length_not_including_first_buffer = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500257
258 /* RX on the local interface. tx in default fib */
259 vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
260 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
261
Florin Corasf6d68ed2017-05-07 19:12:02 -0700262 len_to_deq0 = clib_min (left_to_snd0, deq_per_buf);
263
264 data0 = vlib_buffer_make_headroom (b0, MAX_HDRS_LEN);
265 if (peek_data)
266 {
267 n_bytes_read = svm_fifo_peek (s0->server_tx_fifo, rx_offset,
268 len_to_deq0, data0);
269 /* Keep track of progress locally, transport is also supposed to
270 * increment it independently when pushing the header */
271 rx_offset += n_bytes_read;
272 }
273 else
274 {
275 n_bytes_read = svm_fifo_dequeue_nowait (s0->server_tx_fifo,
276 len_to_deq0, data0);
277 }
278
279 if (n_bytes_read <= 0)
280 goto dequeue_fail;
281
282 b0->current_length = n_bytes_read;
283
284 left_to_snd0 -= n_bytes_read;
285 *n_tx_packets = *n_tx_packets + 1;
286
287 /*
288 * Fill in the remaining buffers in the chain, if any
289 */
290 if (PREDICT_FALSE (n_bufs_per_seg > 1))
291 session_tx_fifo_chain_tail (smm, vm, thread_index,
292 s0->server_tx_fifo, b0, bi0,
293 n_bufs_per_seg, &left_to_snd0,
294 &n_bufs, &rx_offset, deq_per_buf,
295 peek_data);
296
297 /* Ask transport to push header after current_length and
298 * total_length_not_including_first_buffer are updated */
299 transport_vft->push_header (tc0, b0);
300
301 /* *INDENT-OFF* */
302 SESSION_EVT_DBG(SESSION_EVT_DEQ, s0, ({
303 ed->data[0] = e0->event_id;
304 ed->data[1] = max_dequeue0;
305 ed->data[2] = len_to_deq0;
306 ed->data[3] = left_to_snd0;
307 }));
308 /* *INDENT-ON* */
309
Dave Barach68b0fb02017-02-28 15:15:56 -0500310 /* usual speculation, or the enqueue_x1 macro will barf */
311 to_next[0] = bi0;
312 to_next += 1;
313 n_left_to_next -= 1;
314
315 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
316 if (PREDICT_FALSE (n_trace > 0))
317 {
318 session_queue_trace_t *t0;
319 vlib_trace_buffer (vm, node, next_index, b0,
320 1 /* follow_chain */ );
321 vlib_set_trace_count (vm, node, --n_trace);
322 t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
323 t0->session_index = s0->session_index;
324 t0->server_thread_index = s0->thread_index;
325 }
326
Dave Barach68b0fb02017-02-28 15:15:56 -0500327 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
328 to_next, n_left_to_next,
329 bi0, next0);
330 }
331 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
332 }
333
Florin Coras6792ec02017-03-13 03:49:51 -0700334 /* If we couldn't dequeue all bytes mark as partially read */
335 if (max_len_to_snd0 < max_dequeue0)
Dave Barach68b0fb02017-02-28 15:15:56 -0500336 {
Florin Coras6792ec02017-03-13 03:49:51 -0700337 /* If we don't already have new event */
338 if (svm_fifo_set_event (s0->server_tx_fifo))
339 {
Dave Barachacd2a6a2017-05-16 17:41:34 -0400340 vec_add1 (smm->pending_event_vector[thread_index], *e0);
Florin Coras6792ec02017-03-13 03:49:51 -0700341 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500342 }
343 return 0;
344
345dequeue_fail:
Florin Coras6792ec02017-03-13 03:49:51 -0700346 /*
347 * Can't read from fifo. If we don't already have an event, save as partially
348 * read, return buff to free list and return
349 */
350 clib_warning ("dequeue fail");
Dave Barach68b0fb02017-02-28 15:15:56 -0500351
Florin Coras6792ec02017-03-13 03:49:51 -0700352 if (svm_fifo_set_event (s0->server_tx_fifo))
353 {
Dave Barachacd2a6a2017-05-16 17:41:34 -0400354 vec_add1 (smm->pending_event_vector[thread_index], *e0);
Florin Coras6792ec02017-03-13 03:49:51 -0700355 }
356 vlib_put_next_frame (vm, node, next_index, n_left_to_next + 1);
Dave Barach68b0fb02017-02-28 15:15:56 -0500357 _vec_len (smm->tx_buffers[thread_index]) += 1;
358
Dave Barach68b0fb02017-02-28 15:15:56 -0500359 return 0;
360}
361
362int
Florin Corasd79b41e2017-03-04 05:37:52 -0800363session_tx_fifo_peek_and_snd (vlib_main_t * vm, vlib_node_runtime_t * node,
364 session_manager_main_t * smm,
365 session_fifo_event_t * e0,
366 stream_session_t * s0, u32 thread_index,
367 int *n_tx_pkts)
Dave Barach68b0fb02017-02-28 15:15:56 -0500368{
Florin Corasd79b41e2017-03-04 05:37:52 -0800369 return session_tx_fifo_read_and_snd_i (vm, node, smm, e0, s0, thread_index,
370 n_tx_pkts, 1);
Dave Barach68b0fb02017-02-28 15:15:56 -0500371}
372
373int
Florin Corasd79b41e2017-03-04 05:37:52 -0800374session_tx_fifo_dequeue_and_snd (vlib_main_t * vm, vlib_node_runtime_t * node,
375 session_manager_main_t * smm,
376 session_fifo_event_t * e0,
377 stream_session_t * s0, u32 thread_index,
378 int *n_tx_pkts)
Dave Barach68b0fb02017-02-28 15:15:56 -0500379{
Florin Corasd79b41e2017-03-04 05:37:52 -0800380 return session_tx_fifo_read_and_snd_i (vm, node, smm, e0, s0, thread_index,
381 n_tx_pkts, 0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500382}
383
Florin Corasa5464812017-04-19 13:00:05 -0700384stream_session_t *
385session_event_get_session (session_fifo_event_t * e0, u8 thread_index)
386{
387 svm_fifo_t *f0;
388 stream_session_t *s0;
389 u32 session_index0;
390
391 f0 = e0->fifo;
392 session_index0 = f0->master_session_index;
393
394 /* $$$ add multiple event queues, per vpp worker thread */
395 ASSERT (f0->master_thread_index == thread_index);
396
397 s0 = stream_session_get_if_valid (session_index0, thread_index);
398
Florin Corasf03a59a2017-06-09 21:07:32 -0700399 ASSERT (s0 == 0 || s0->thread_index == thread_index);
Florin Corasa5464812017-04-19 13:00:05 -0700400
401 return s0;
402}
403
Dave Barachacd2a6a2017-05-16 17:41:34 -0400404void
405dump_thread_0_event_queue (void)
406{
407 session_manager_main_t *smm = vnet_get_session_manager_main ();
408 vlib_main_t *vm = &vlib_global_main;
409 u32 my_thread_index = vm->thread_index;
410 session_fifo_event_t _e, *e = &_e;
411 stream_session_t *s0;
412 int i, index;
413 i8 *headp;
414
415 unix_shared_memory_queue_t *q;
416 q = smm->vpp_event_queues[my_thread_index];
417
418 index = q->head;
419
420 for (i = 0; i < q->cursize; i++)
421 {
422 headp = (i8 *) (&q->data[0] + q->elsize * index);
423 clib_memcpy (e, headp, q->elsize);
424
425 switch (e->event_type)
426 {
427 case FIFO_EVENT_APP_TX:
428 s0 = session_event_get_session (e, my_thread_index);
429 fformat (stdout, "[%04d] TX session %d\n", i, s0->session_index);
430 break;
431
432 case FIFO_EVENT_DISCONNECT:
433 s0 = stream_session_get_from_handle (e->session_handle);
434 fformat (stdout, "[%04d] disconnect session %d\n", i,
435 s0->session_index);
436 break;
437
438 case FIFO_EVENT_BUILTIN_RX:
439 s0 = session_event_get_session (e, my_thread_index);
440 fformat (stdout, "[%04d] builtin_rx %d\n", i, s0->session_index);
441 break;
442
443 case FIFO_EVENT_RPC:
444 fformat (stdout, "[%04d] RPC call %llx with %llx\n",
445 i, (u64) (e->rpc_args.fp), (u64) (e->rpc_args.arg));
446 break;
447
448 default:
449 fformat (stdout, "[%04d] unhandled event type %d\n",
450 i, e->event_type);
451 break;
452 }
453
454 index++;
455
456 if (index == q->maxsize)
457 index = 0;
458 }
459}
460
Dave Barach68b0fb02017-02-28 15:15:56 -0500461static uword
462session_queue_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
463 vlib_frame_t * frame)
464{
465 session_manager_main_t *smm = vnet_get_session_manager_main ();
Dave Barachacd2a6a2017-05-16 17:41:34 -0400466 session_fifo_event_t *my_pending_event_vector, *e;
467 session_fifo_event_t *my_fifo_events;
Florin Corase04c2992017-03-01 08:17:34 -0800468 u32 n_to_dequeue, n_events;
Dave Barach68b0fb02017-02-28 15:15:56 -0500469 unix_shared_memory_queue_t *q;
Florin Coras6792ec02017-03-13 03:49:51 -0700470 application_t *app;
Dave Barach68b0fb02017-02-28 15:15:56 -0500471 int n_tx_packets = 0;
Damjan Marion586afd72017-04-05 19:18:20 +0200472 u32 my_thread_index = vm->thread_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500473 int i, rv;
Florin Coras3e350af2017-03-30 02:54:28 -0700474 f64 now = vlib_time_now (vm);
Dave Barache5f1d272017-05-10 13:34:04 -0400475 void (*fp) (void *);
Florin Coras3e350af2017-03-30 02:54:28 -0700476
477 SESSION_EVT_DBG (SESSION_EVT_POLL_GAP_TRACK, smm, my_thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500478
479 /*
480 * Update TCP time
481 */
Florin Coras3e350af2017-03-30 02:54:28 -0700482 tcp_update_time (now, my_thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500483
484 /*
485 * Get vpp queue events
486 */
487 q = smm->vpp_event_queues[my_thread_index];
488 if (PREDICT_FALSE (q == 0))
489 return 0;
490
Dave Barachacd2a6a2017-05-16 17:41:34 -0400491 my_fifo_events = smm->free_event_vector[my_thread_index];
492
Dave Barach68b0fb02017-02-28 15:15:56 -0500493 /* min number of events we can dequeue without blocking */
494 n_to_dequeue = q->cursize;
Dave Barachacd2a6a2017-05-16 17:41:34 -0400495 my_pending_event_vector = smm->pending_event_vector[my_thread_index];
Dave Barach68b0fb02017-02-28 15:15:56 -0500496
Dave Barachacd2a6a2017-05-16 17:41:34 -0400497 if (n_to_dequeue == 0 && vec_len (my_pending_event_vector) == 0)
Florin Corase04c2992017-03-01 08:17:34 -0800498 return 0;
499
Florin Coras6792ec02017-03-13 03:49:51 -0700500 SESSION_EVT_DBG (SESSION_EVT_DEQ_NODE, 0);
501
Florin Corase04c2992017-03-01 08:17:34 -0800502 /*
503 * If we didn't manage to process previous events try going
Dave Barach68b0fb02017-02-28 15:15:56 -0500504 * over them again without dequeuing new ones.
Florin Corase04c2992017-03-01 08:17:34 -0800505 */
506 /* XXX: Block senders to sessions that can't keep up */
Dave Barachacd2a6a2017-05-16 17:41:34 -0400507 if (0 && vec_len (my_pending_event_vector) >= 100)
Florin Coras6792ec02017-03-13 03:49:51 -0700508 {
509 clib_warning ("too many fifo events unsolved");
510 goto skip_dequeue;
511 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500512
513 /* See you in the next life, don't be late */
514 if (pthread_mutex_trylock (&q->mutex))
515 return 0;
516
517 for (i = 0; i < n_to_dequeue; i++)
518 {
519 vec_add2 (my_fifo_events, e, 1);
520 unix_shared_memory_queue_sub_raw (q, (u8 *) e);
521 }
522
523 /* The other side of the connection is not polling */
524 if (q->cursize < (q->maxsize / 8))
525 (void) pthread_cond_broadcast (&q->condvar);
526 pthread_mutex_unlock (&q->mutex);
527
Dave Barachacd2a6a2017-05-16 17:41:34 -0400528 vec_append (my_fifo_events, my_pending_event_vector);
529
530 _vec_len (my_pending_event_vector) = 0;
531 smm->pending_event_vector[my_thread_index] = my_pending_event_vector;
Dave Barach68b0fb02017-02-28 15:15:56 -0500532
533skip_dequeue:
Florin Corase04c2992017-03-01 08:17:34 -0800534 n_events = vec_len (my_fifo_events);
535 for (i = 0; i < n_events; i++)
Dave Barach68b0fb02017-02-28 15:15:56 -0500536 {
Florin Corasa5464812017-04-19 13:00:05 -0700537 stream_session_t *s0; /* $$$ prefetch 1 ahead maybe */
Dave Barach68b0fb02017-02-28 15:15:56 -0500538 session_fifo_event_t *e0;
539
540 e0 = &my_fifo_events[i];
Dave Barach68b0fb02017-02-28 15:15:56 -0500541
542 switch (e0->event_type)
543 {
Florin Corasa5464812017-04-19 13:00:05 -0700544 case FIFO_EVENT_APP_TX:
545 s0 = session_event_get_session (e0, my_thread_index);
546
547 if (CLIB_DEBUG && !s0)
548 {
549 clib_warning ("It's dead, Jim!");
550 continue;
551 }
552
553 if (PREDICT_FALSE (s0->session_state == SESSION_STATE_CLOSED))
554 continue;
Dave Barach68b0fb02017-02-28 15:15:56 -0500555 /* Spray packets in per session type frames, since they go to
556 * different nodes */
Florin Corase69f4952017-03-07 10:06:24 -0800557 rv = (smm->session_tx_fns[s0->session_type]) (vm, node, smm, e0, s0,
Dave Barach68b0fb02017-02-28 15:15:56 -0500558 my_thread_index,
559 &n_tx_packets);
Florin Coras6792ec02017-03-13 03:49:51 -0700560 /* Out of buffers */
Florin Coras93992a92017-05-24 18:03:56 -0700561 if (PREDICT_FALSE (rv < 0))
Dave Barachacd2a6a2017-05-16 17:41:34 -0400562 {
Florin Coras93992a92017-05-24 18:03:56 -0700563 vlib_node_increment_counter (vm, node->node_index,
564 SESSION_QUEUE_ERROR_NO_BUFFER, 1);
Dave Barachacd2a6a2017-05-16 17:41:34 -0400565 continue;
566 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500567 break;
Florin Corasa5464812017-04-19 13:00:05 -0700568 case FIFO_EVENT_DISCONNECT:
569 s0 = stream_session_get_from_handle (e0->session_handle);
Florin Coras6792ec02017-03-13 03:49:51 -0700570 stream_session_disconnect (s0);
571 break;
572 case FIFO_EVENT_BUILTIN_RX:
Florin Corasa5464812017-04-19 13:00:05 -0700573 s0 = session_event_get_session (e0, my_thread_index);
Florin Coras6792ec02017-03-13 03:49:51 -0700574 svm_fifo_unset_event (s0->server_rx_fifo);
575 /* Get session's server */
576 app = application_get (s0->app_index);
577 app->cb_fns.builtin_server_rx_callback (s0);
578 break;
Dave Barache5f1d272017-05-10 13:34:04 -0400579 case FIFO_EVENT_RPC:
580 fp = e0->rpc_args.fp;
581 (*fp) (e0->rpc_args.arg);
582 break;
583
Dave Barach68b0fb02017-02-28 15:15:56 -0500584 default:
585 clib_warning ("unhandled event type %d", e0->event_type);
586 }
587 }
588
Dave Barachacd2a6a2017-05-16 17:41:34 -0400589 _vec_len (my_fifo_events) = 0;
590 smm->free_event_vector[my_thread_index] = my_fifo_events;
Dave Barach68b0fb02017-02-28 15:15:56 -0500591
592 vlib_node_increment_counter (vm, session_queue_node.index,
593 SESSION_QUEUE_ERROR_TX, n_tx_packets);
594
Florin Coras6792ec02017-03-13 03:49:51 -0700595 SESSION_EVT_DBG (SESSION_EVT_DEQ_NODE, 1);
596
Dave Barach68b0fb02017-02-28 15:15:56 -0500597 return n_tx_packets;
598}
599
600/* *INDENT-OFF* */
601VLIB_REGISTER_NODE (session_queue_node) =
602{
603 .function = session_queue_node_fn,
604 .name = "session-queue",
605 .format_trace = format_session_queue_trace,
606 .type = VLIB_NODE_TYPE_INPUT,
607 .n_errors = ARRAY_LEN (session_queue_error_strings),
608 .error_strings = session_queue_error_strings,
609 .n_next_nodes = SESSION_QUEUE_N_NEXT,
Florin Corase04c2992017-03-01 08:17:34 -0800610 .state = VLIB_NODE_STATE_DISABLED,
Dave Barach68b0fb02017-02-28 15:15:56 -0500611 .next_nodes =
612 {
613 [SESSION_QUEUE_NEXT_DROP] = "error-drop",
614 [SESSION_QUEUE_NEXT_IP4_LOOKUP] = "ip4-lookup",
615 [SESSION_QUEUE_NEXT_IP6_LOOKUP] = "ip6-lookup",
616 [SESSION_QUEUE_NEXT_TCP_IP4_OUTPUT] = "tcp4-output",
617 [SESSION_QUEUE_NEXT_TCP_IP6_OUTPUT] = "tcp6-output",
618 },
619};
620/* *INDENT-ON* */
621
622/*
623 * fd.io coding-style-patch-verification: ON
624 *
625 * Local Variables:
626 * eval: (c-set-style "gnu")
627 * End:
628 */