blob: 9c5b17d98e6d4eb53e09dbe3ab44c93d9cc06e06 [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,
Florin Corasb2215d62017-08-01 16:56:58 -070078 u32 left_from_seg, u32 * left_to_snd0,
79 u16 * n_bufs, u32 * rx_offset, u16 deq_per_buf,
80 u8 peek_data)
Florin Corasf6d68ed2017-05-07 19:12:02 -070081{
82 vlib_buffer_t *chain_b0, *prev_b0;
Florin Corasb2215d62017-08-01 16:56:58 -070083 u32 chain_bi0, to_deq;
Florin Corasf6d68ed2017-05-07 19:12:02 -070084 u16 len_to_deq0, n_bytes_read;
85 u8 *data0, j;
86
Florin Corasb2215d62017-08-01 16:56:58 -070087 b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
88 b0->total_length_not_including_first_buffer = 0;
89
Florin Corasf6d68ed2017-05-07 19:12:02 -070090 chain_bi0 = bi0;
91 chain_b0 = b0;
Florin Corasb2215d62017-08-01 16:56:58 -070092 to_deq = left_from_seg;
Florin Corasf6d68ed2017-05-07 19:12:02 -070093 for (j = 1; j < n_bufs_per_seg; j++)
94 {
95 prev_b0 = chain_b0;
Florin Corasb2215d62017-08-01 16:56:58 -070096 len_to_deq0 = clib_min (to_deq, deq_per_buf);
Florin Corasf6d68ed2017-05-07 19:12:02 -070097
98 *n_bufs -= 1;
99 chain_bi0 = smm->tx_buffers[thread_index][*n_bufs];
100 _vec_len (smm->tx_buffers[thread_index]) = *n_bufs;
101
102 chain_b0 = vlib_get_buffer (vm, chain_bi0);
103 chain_b0->current_data = 0;
104 data0 = vlib_buffer_get_current (chain_b0);
105 if (peek_data)
106 {
107 n_bytes_read = svm_fifo_peek (fifo, *rx_offset, len_to_deq0, data0);
108 *rx_offset += n_bytes_read;
109 }
110 else
111 {
112 n_bytes_read = svm_fifo_dequeue_nowait (fifo, len_to_deq0, data0);
113 }
114 ASSERT (n_bytes_read == len_to_deq0);
115 chain_b0->current_length = n_bytes_read;
116 b0->total_length_not_including_first_buffer += chain_b0->current_length;
117
118 /* update previous buffer */
119 prev_b0->next_buffer = chain_bi0;
120 prev_b0->flags |= VLIB_BUFFER_NEXT_PRESENT;
121
122 /* update current buffer */
123 chain_b0->next_buffer = 0;
124
Florin Corasb2215d62017-08-01 16:56:58 -0700125 to_deq -= n_bytes_read;
126 if (to_deq == 0)
Florin Corasf6d68ed2017-05-07 19:12:02 -0700127 break;
128 }
Florin Corasb2215d62017-08-01 16:56:58 -0700129 ASSERT (to_deq == 0);
130 *left_to_snd0 -= left_from_seg;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700131}
132
Dave Barach68b0fb02017-02-28 15:15:56 -0500133always_inline int
Florin Corasd79b41e2017-03-04 05:37:52 -0800134session_tx_fifo_read_and_snd_i (vlib_main_t * vm, vlib_node_runtime_t * node,
135 session_manager_main_t * smm,
136 session_fifo_event_t * e0,
137 stream_session_t * s0, u32 thread_index,
138 int *n_tx_packets, u8 peek_data)
Dave Barach68b0fb02017-02-28 15:15:56 -0500139{
140 u32 n_trace = vlib_get_trace_count (vm, node);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700141 u32 left_to_snd0, max_len_to_snd0, len_to_deq0, snd_space0;
142 u32 n_bufs_per_evt, n_frames_per_evt;
Dave Barach68b0fb02017-02-28 15:15:56 -0500143 transport_connection_t *tc0;
144 transport_proto_vft_t *transport_vft;
145 u32 next_index, next0, *to_next, n_left_to_next, bi0;
146 vlib_buffer_t *b0;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700147 u32 rx_offset = 0, max_dequeue0, n_bytes_per_seg;
148 u16 snd_mss0, n_bufs_per_seg, n_bufs;
Dave Barach68b0fb02017-02-28 15:15:56 -0500149 u8 *data0;
Florin Coras6792ec02017-03-13 03:49:51 -0700150 int i, n_bytes_read;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700151 u32 n_bytes_per_buf, deq_per_buf;
Florin Coras93992a92017-05-24 18:03:56 -0700152 u32 buffers_allocated, buffers_allocated_this_call;
Dave Barach68b0fb02017-02-28 15:15:56 -0500153
154 next_index = next0 = session_type_to_next[s0->session_type];
155
156 transport_vft = session_get_transport_vft (s0->session_type);
157 tc0 = transport_vft->get_connection (s0->connection_index, thread_index);
158
159 /* Make sure we have space to send and there's something to dequeue */
Dave Barach68b0fb02017-02-28 15:15:56 -0500160 snd_mss0 = transport_vft->send_mss (tc0);
Florin Corasc8343412017-05-04 14:25:50 -0700161 snd_space0 = transport_vft->send_space (tc0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500162
Florin Corase04c2992017-03-01 08:17:34 -0800163 /* Can't make any progress */
Florin Coras6792ec02017-03-13 03:49:51 -0700164 if (snd_space0 == 0 || snd_mss0 == 0)
Florin Corase04c2992017-03-01 08:17:34 -0800165 {
Dave Barachacd2a6a2017-05-16 17:41:34 -0400166 vec_add1 (smm->pending_event_vector[thread_index], *e0);
Florin Corase04c2992017-03-01 08:17:34 -0800167 return 0;
168 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500169
Dave Barach68b0fb02017-02-28 15:15:56 -0500170 if (peek_data)
171 {
172 /* Offset in rx fifo from where to peek data */
Florin Corasd79b41e2017-03-04 05:37:52 -0800173 rx_offset = transport_vft->tx_fifo_offset (tc0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500174 }
175
Florin Coras6792ec02017-03-13 03:49:51 -0700176 /* Check how much we can pull. If buffering, subtract the offset */
177 max_dequeue0 = svm_fifo_max_dequeue (s0->server_tx_fifo) - rx_offset;
178
Florin Coras6792ec02017-03-13 03:49:51 -0700179 /* Nothing to read return */
180 if (max_dequeue0 == 0)
Florin Corasf03a59a2017-06-09 21:07:32 -0700181 {
182 svm_fifo_unset_event (s0->server_tx_fifo);
183 return 0;
184 }
Florin Coras6792ec02017-03-13 03:49:51 -0700185
186 /* Ensure we're not writing more than transport window allows */
Florin Coras3e350af2017-03-30 02:54:28 -0700187 if (max_dequeue0 < snd_space0)
188 {
189 /* Constrained by tx queue. Try to send only fully formed segments */
190 max_len_to_snd0 = (max_dequeue0 > snd_mss0) ?
191 max_dequeue0 - max_dequeue0 % snd_mss0 : max_dequeue0;
192 /* TODO Nagle ? */
193 }
194 else
195 {
196 max_len_to_snd0 = snd_space0;
197 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500198
Florin Coras93992a92017-05-24 18:03:56 -0700199 n_bytes_per_buf = vlib_buffer_free_list_buffer_size
200 (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700201 n_bytes_per_seg = MAX_HDRS_LEN + snd_mss0;
202 n_bufs_per_seg = ceil ((double) n_bytes_per_seg / n_bytes_per_buf);
203 n_bufs_per_evt = (ceil ((double) max_len_to_snd0 / n_bytes_per_seg))
204 * n_bufs_per_seg;
205 n_frames_per_evt = ceil ((double) n_bufs_per_evt / VLIB_FRAME_SIZE);
206
207 deq_per_buf = clib_min (snd_mss0, n_bytes_per_buf);
Dave Barach68b0fb02017-02-28 15:15:56 -0500208
209 n_bufs = vec_len (smm->tx_buffers[thread_index]);
210 left_to_snd0 = max_len_to_snd0;
211 for (i = 0; i < n_frames_per_evt; i++)
212 {
213 /* Make sure we have at least one full frame of buffers ready */
214 if (PREDICT_FALSE (n_bufs < VLIB_FRAME_SIZE))
215 {
216 vec_validate (smm->tx_buffers[thread_index],
Florin Coras93992a92017-05-24 18:03:56 -0700217 n_bufs + 2 * VLIB_FRAME_SIZE - 1);
Dave Barach68b0fb02017-02-28 15:15:56 -0500218
Florin Coras93992a92017-05-24 18:03:56 -0700219 buffers_allocated = 0;
220 do
Dave Barach68b0fb02017-02-28 15:15:56 -0500221 {
Florin Coras93992a92017-05-24 18:03:56 -0700222 buffers_allocated_this_call =
223 vlib_buffer_alloc
224 (vm,
225 &smm->tx_buffers[thread_index][n_bufs + buffers_allocated],
226 2 * VLIB_FRAME_SIZE - buffers_allocated);
227 buffers_allocated += buffers_allocated_this_call;
Dave Barach68b0fb02017-02-28 15:15:56 -0500228 }
Florin Coras93992a92017-05-24 18:03:56 -0700229 while (buffers_allocated_this_call > 0
230 && ((buffers_allocated + n_bufs < VLIB_FRAME_SIZE)));
231
232 n_bufs += buffers_allocated;
Dave Barach68b0fb02017-02-28 15:15:56 -0500233 _vec_len (smm->tx_buffers[thread_index]) = n_bufs;
Florin Coras93992a92017-05-24 18:03:56 -0700234
235 if (PREDICT_FALSE (n_bufs < VLIB_FRAME_SIZE))
236 {
237 vec_add1 (smm->pending_event_vector[thread_index], *e0);
238 return -1;
239 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500240 }
Florin Coras93992a92017-05-24 18:03:56 -0700241 /* Allow enqueuing of a new event */
242 svm_fifo_unset_event (s0->server_tx_fifo);
Dave Barach68b0fb02017-02-28 15:15:56 -0500243
244 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700245 while (left_to_snd0 && n_left_to_next >= n_bufs_per_seg)
Dave Barach68b0fb02017-02-28 15:15:56 -0500246 {
Florin Corasf6d68ed2017-05-07 19:12:02 -0700247 /*
248 * Handle first buffer in chain separately
249 */
250
Dave Barach68b0fb02017-02-28 15:15:56 -0500251 /* Get free buffer */
Florin Coras93992a92017-05-24 18:03:56 -0700252 ASSERT (n_bufs >= 1);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700253 bi0 = smm->tx_buffers[thread_index][--n_bufs];
Florin Coras93992a92017-05-24 18:03:56 -0700254 ASSERT (bi0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500255 _vec_len (smm->tx_buffers[thread_index]) = n_bufs;
256
Florin Corasf6359c82017-06-19 12:26:09 -0400257 /* usual speculation, or the enqueue_x1 macro will barf */
258 to_next[0] = bi0;
259 to_next += 1;
260 n_left_to_next -= 1;
261
Dave Barach68b0fb02017-02-28 15:15:56 -0500262 b0 = vlib_get_buffer (vm, bi0);
263 b0->error = 0;
264 b0->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID
Damjan Marion213b5aa2017-07-13 21:19:27 +0200265 | VNET_BUFFER_F_LOCALLY_ORIGINATED;
Dave Barach68b0fb02017-02-28 15:15:56 -0500266 b0->current_data = 0;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700267 b0->total_length_not_including_first_buffer = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500268
Florin Corasf6d68ed2017-05-07 19:12:02 -0700269 len_to_deq0 = clib_min (left_to_snd0, deq_per_buf);
270
271 data0 = vlib_buffer_make_headroom (b0, MAX_HDRS_LEN);
272 if (peek_data)
273 {
274 n_bytes_read = svm_fifo_peek (s0->server_tx_fifo, rx_offset,
275 len_to_deq0, data0);
276 /* Keep track of progress locally, transport is also supposed to
277 * increment it independently when pushing the header */
278 rx_offset += n_bytes_read;
279 }
280 else
281 {
282 n_bytes_read = svm_fifo_dequeue_nowait (s0->server_tx_fifo,
283 len_to_deq0, data0);
284 }
285
286 if (n_bytes_read <= 0)
287 goto dequeue_fail;
288
289 b0->current_length = n_bytes_read;
290
291 left_to_snd0 -= n_bytes_read;
292 *n_tx_packets = *n_tx_packets + 1;
293
294 /*
295 * Fill in the remaining buffers in the chain, if any
296 */
297 if (PREDICT_FALSE (n_bufs_per_seg > 1))
Florin Corasb2215d62017-08-01 16:56:58 -0700298 {
299 u32 left_for_seg;
300 left_for_seg = clib_min (snd_mss0 - n_bytes_read, left_to_snd0);
301 session_tx_fifo_chain_tail (smm, vm, thread_index,
302 s0->server_tx_fifo, b0, bi0,
303 n_bufs_per_seg, left_for_seg,
304 &left_to_snd0, &n_bufs, &rx_offset,
305 deq_per_buf, peek_data);
306 }
Florin Corasf6d68ed2017-05-07 19:12:02 -0700307
308 /* Ask transport to push header after current_length and
309 * total_length_not_including_first_buffer are updated */
310 transport_vft->push_header (tc0, b0);
311
312 /* *INDENT-OFF* */
313 SESSION_EVT_DBG(SESSION_EVT_DEQ, s0, ({
314 ed->data[0] = e0->event_id;
315 ed->data[1] = max_dequeue0;
316 ed->data[2] = len_to_deq0;
317 ed->data[3] = left_to_snd0;
318 }));
319 /* *INDENT-ON* */
320
Dave Barach68b0fb02017-02-28 15:15:56 -0500321
322 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
323 if (PREDICT_FALSE (n_trace > 0))
324 {
325 session_queue_trace_t *t0;
326 vlib_trace_buffer (vm, node, next_index, b0,
327 1 /* follow_chain */ );
328 vlib_set_trace_count (vm, node, --n_trace);
329 t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
330 t0->session_index = s0->session_index;
331 t0->server_thread_index = s0->thread_index;
332 }
333
Dave Barach68b0fb02017-02-28 15:15:56 -0500334 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
335 to_next, n_left_to_next,
336 bi0, next0);
337 }
338 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
339 }
340
Florin Coras6792ec02017-03-13 03:49:51 -0700341 /* If we couldn't dequeue all bytes mark as partially read */
342 if (max_len_to_snd0 < max_dequeue0)
Dave Barach68b0fb02017-02-28 15:15:56 -0500343 {
Florin Coras6792ec02017-03-13 03:49:51 -0700344 /* If we don't already have new event */
345 if (svm_fifo_set_event (s0->server_tx_fifo))
346 {
Dave Barachacd2a6a2017-05-16 17:41:34 -0400347 vec_add1 (smm->pending_event_vector[thread_index], *e0);
Florin Coras6792ec02017-03-13 03:49:51 -0700348 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500349 }
350 return 0;
351
352dequeue_fail:
Florin Coras6792ec02017-03-13 03:49:51 -0700353 /*
354 * Can't read from fifo. If we don't already have an event, save as partially
355 * read, return buff to free list and return
356 */
357 clib_warning ("dequeue fail");
Dave Barach68b0fb02017-02-28 15:15:56 -0500358
Florin Coras6792ec02017-03-13 03:49:51 -0700359 if (svm_fifo_set_event (s0->server_tx_fifo))
360 {
Dave Barachacd2a6a2017-05-16 17:41:34 -0400361 vec_add1 (smm->pending_event_vector[thread_index], *e0);
Florin Coras6792ec02017-03-13 03:49:51 -0700362 }
363 vlib_put_next_frame (vm, node, next_index, n_left_to_next + 1);
Dave Barach68b0fb02017-02-28 15:15:56 -0500364 _vec_len (smm->tx_buffers[thread_index]) += 1;
365
Dave Barach68b0fb02017-02-28 15:15:56 -0500366 return 0;
367}
368
369int
Florin Corasd79b41e2017-03-04 05:37:52 -0800370session_tx_fifo_peek_and_snd (vlib_main_t * vm, vlib_node_runtime_t * node,
371 session_manager_main_t * smm,
372 session_fifo_event_t * e0,
373 stream_session_t * s0, u32 thread_index,
374 int *n_tx_pkts)
Dave Barach68b0fb02017-02-28 15:15:56 -0500375{
Florin Corasd79b41e2017-03-04 05:37:52 -0800376 return session_tx_fifo_read_and_snd_i (vm, node, smm, e0, s0, thread_index,
377 n_tx_pkts, 1);
Dave Barach68b0fb02017-02-28 15:15:56 -0500378}
379
380int
Florin Corasd79b41e2017-03-04 05:37:52 -0800381session_tx_fifo_dequeue_and_snd (vlib_main_t * vm, vlib_node_runtime_t * node,
382 session_manager_main_t * smm,
383 session_fifo_event_t * e0,
384 stream_session_t * s0, u32 thread_index,
385 int *n_tx_pkts)
Dave Barach68b0fb02017-02-28 15:15:56 -0500386{
Florin Corasd79b41e2017-03-04 05:37:52 -0800387 return session_tx_fifo_read_and_snd_i (vm, node, smm, e0, s0, thread_index,
388 n_tx_pkts, 0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500389}
390
Dave Barach2c25a622017-06-26 11:35:07 -0400391always_inline stream_session_t *
392session_event_get_session (session_fifo_event_t * e, u8 thread_index)
Florin Corasa5464812017-04-19 13:00:05 -0700393{
Dave Barach2c25a622017-06-26 11:35:07 -0400394 ASSERT (e->fifo->master_thread_index == thread_index);
395 return stream_session_get_if_valid (e->fifo->master_session_index,
396 thread_index);
Florin Corasa5464812017-04-19 13:00:05 -0700397}
398
Dave Barachacd2a6a2017-05-16 17:41:34 -0400399void
400dump_thread_0_event_queue (void)
401{
402 session_manager_main_t *smm = vnet_get_session_manager_main ();
403 vlib_main_t *vm = &vlib_global_main;
404 u32 my_thread_index = vm->thread_index;
405 session_fifo_event_t _e, *e = &_e;
406 stream_session_t *s0;
407 int i, index;
408 i8 *headp;
409
410 unix_shared_memory_queue_t *q;
411 q = smm->vpp_event_queues[my_thread_index];
412
413 index = q->head;
414
415 for (i = 0; i < q->cursize; i++)
416 {
417 headp = (i8 *) (&q->data[0] + q->elsize * index);
418 clib_memcpy (e, headp, q->elsize);
419
420 switch (e->event_type)
421 {
422 case FIFO_EVENT_APP_TX:
423 s0 = session_event_get_session (e, my_thread_index);
424 fformat (stdout, "[%04d] TX session %d\n", i, s0->session_index);
425 break;
426
427 case FIFO_EVENT_DISCONNECT:
428 s0 = stream_session_get_from_handle (e->session_handle);
429 fformat (stdout, "[%04d] disconnect session %d\n", i,
430 s0->session_index);
431 break;
432
433 case FIFO_EVENT_BUILTIN_RX:
434 s0 = session_event_get_session (e, my_thread_index);
435 fformat (stdout, "[%04d] builtin_rx %d\n", i, s0->session_index);
436 break;
437
438 case FIFO_EVENT_RPC:
439 fformat (stdout, "[%04d] RPC call %llx with %llx\n",
440 i, (u64) (e->rpc_args.fp), (u64) (e->rpc_args.arg));
441 break;
442
443 default:
444 fformat (stdout, "[%04d] unhandled event type %d\n",
445 i, e->event_type);
446 break;
447 }
448
449 index++;
450
451 if (index == q->maxsize)
452 index = 0;
453 }
454}
455
Florin Coras6534b7a2017-07-18 05:38:03 -0400456static u8
457session_node_cmp_event (session_fifo_event_t * e, svm_fifo_t * f)
458{
459 stream_session_t *s;
460 switch (e->event_type)
461 {
462 case FIFO_EVENT_APP_RX:
463 case FIFO_EVENT_APP_TX:
464 case FIFO_EVENT_BUILTIN_RX:
465 if (e->fifo == f)
466 return 1;
467 break;
468 case FIFO_EVENT_DISCONNECT:
469 break;
470 case FIFO_EVENT_RPC:
471 s = stream_session_get_from_handle (e->session_handle);
472 if (!s)
473 {
474 clib_warning ("session has event but doesn't exist!");
475 break;
476 }
477 if (s->server_rx_fifo == f || s->server_tx_fifo == f)
478 return 1;
479 break;
480 default:
481 break;
482 }
483 return 0;
484}
485
486u8
487session_node_lookup_fifo_event (svm_fifo_t * f, session_fifo_event_t * e)
488{
489 session_manager_main_t *smm = vnet_get_session_manager_main ();
490 unix_shared_memory_queue_t *q;
491 session_fifo_event_t *pending_event_vector, *evt;
492 int i, index, found = 0;
493 i8 *headp;
494 u8 thread_index;
495
496 ASSERT (e);
497 thread_index = f->master_thread_index;
498 /*
499 * Search evt queue
500 */
501 q = smm->vpp_event_queues[thread_index];
502 index = q->head;
503 for (i = 0; i < q->cursize; i++)
504 {
505 headp = (i8 *) (&q->data[0] + q->elsize * index);
506 clib_memcpy (e, headp, q->elsize);
507 found = session_node_cmp_event (e, f);
508 if (found)
509 break;
510 if (++index == q->maxsize)
511 index = 0;
512 }
513 /*
514 * Search pending events vector
515 */
516 pending_event_vector = smm->pending_event_vector[thread_index];
517 vec_foreach (evt, pending_event_vector)
518 {
519 found = session_node_cmp_event (evt, f);
520 if (found)
521 {
522 clib_memcpy (e, evt, sizeof (*evt));
523 break;
524 }
525 }
526 return found;
527}
528
Dave Barach68b0fb02017-02-28 15:15:56 -0500529static uword
530session_queue_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
531 vlib_frame_t * frame)
532{
533 session_manager_main_t *smm = vnet_get_session_manager_main ();
Dave Barachacd2a6a2017-05-16 17:41:34 -0400534 session_fifo_event_t *my_pending_event_vector, *e;
535 session_fifo_event_t *my_fifo_events;
Florin Corase04c2992017-03-01 08:17:34 -0800536 u32 n_to_dequeue, n_events;
Dave Barach68b0fb02017-02-28 15:15:56 -0500537 unix_shared_memory_queue_t *q;
Florin Coras6792ec02017-03-13 03:49:51 -0700538 application_t *app;
Dave Barach68b0fb02017-02-28 15:15:56 -0500539 int n_tx_packets = 0;
Damjan Marion586afd72017-04-05 19:18:20 +0200540 u32 my_thread_index = vm->thread_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500541 int i, rv;
Florin Coras3e350af2017-03-30 02:54:28 -0700542 f64 now = vlib_time_now (vm);
Dave Barache5f1d272017-05-10 13:34:04 -0400543 void (*fp) (void *);
Florin Coras3e350af2017-03-30 02:54:28 -0700544
545 SESSION_EVT_DBG (SESSION_EVT_POLL_GAP_TRACK, smm, my_thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500546
547 /*
548 * Update TCP time
549 */
Florin Coras3e350af2017-03-30 02:54:28 -0700550 tcp_update_time (now, my_thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500551
552 /*
553 * Get vpp queue events
554 */
555 q = smm->vpp_event_queues[my_thread_index];
556 if (PREDICT_FALSE (q == 0))
557 return 0;
558
Dave Barachacd2a6a2017-05-16 17:41:34 -0400559 my_fifo_events = smm->free_event_vector[my_thread_index];
560
Dave Barach68b0fb02017-02-28 15:15:56 -0500561 /* min number of events we can dequeue without blocking */
562 n_to_dequeue = q->cursize;
Dave Barachacd2a6a2017-05-16 17:41:34 -0400563 my_pending_event_vector = smm->pending_event_vector[my_thread_index];
Dave Barach68b0fb02017-02-28 15:15:56 -0500564
Dave Barachacd2a6a2017-05-16 17:41:34 -0400565 if (n_to_dequeue == 0 && vec_len (my_pending_event_vector) == 0)
Florin Corase04c2992017-03-01 08:17:34 -0800566 return 0;
567
Florin Coras6792ec02017-03-13 03:49:51 -0700568 SESSION_EVT_DBG (SESSION_EVT_DEQ_NODE, 0);
569
Florin Corase04c2992017-03-01 08:17:34 -0800570 /*
571 * If we didn't manage to process previous events try going
Dave Barach68b0fb02017-02-28 15:15:56 -0500572 * over them again without dequeuing new ones.
Florin Corase04c2992017-03-01 08:17:34 -0800573 */
574 /* XXX: Block senders to sessions that can't keep up */
Dave Barachacd2a6a2017-05-16 17:41:34 -0400575 if (0 && vec_len (my_pending_event_vector) >= 100)
Florin Coras6792ec02017-03-13 03:49:51 -0700576 {
577 clib_warning ("too many fifo events unsolved");
578 goto skip_dequeue;
579 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500580
581 /* See you in the next life, don't be late */
582 if (pthread_mutex_trylock (&q->mutex))
583 return 0;
584
585 for (i = 0; i < n_to_dequeue; i++)
586 {
587 vec_add2 (my_fifo_events, e, 1);
588 unix_shared_memory_queue_sub_raw (q, (u8 *) e);
589 }
590
591 /* The other side of the connection is not polling */
592 if (q->cursize < (q->maxsize / 8))
593 (void) pthread_cond_broadcast (&q->condvar);
594 pthread_mutex_unlock (&q->mutex);
595
Dave Barachacd2a6a2017-05-16 17:41:34 -0400596 vec_append (my_fifo_events, my_pending_event_vector);
597
598 _vec_len (my_pending_event_vector) = 0;
599 smm->pending_event_vector[my_thread_index] = my_pending_event_vector;
Dave Barach68b0fb02017-02-28 15:15:56 -0500600
601skip_dequeue:
Florin Corase04c2992017-03-01 08:17:34 -0800602 n_events = vec_len (my_fifo_events);
603 for (i = 0; i < n_events; i++)
Dave Barach68b0fb02017-02-28 15:15:56 -0500604 {
Florin Corasa5464812017-04-19 13:00:05 -0700605 stream_session_t *s0; /* $$$ prefetch 1 ahead maybe */
Dave Barach68b0fb02017-02-28 15:15:56 -0500606 session_fifo_event_t *e0;
607
608 e0 = &my_fifo_events[i];
Dave Barach68b0fb02017-02-28 15:15:56 -0500609
610 switch (e0->event_type)
611 {
Florin Corasa5464812017-04-19 13:00:05 -0700612 case FIFO_EVENT_APP_TX:
613 s0 = session_event_get_session (e0, my_thread_index);
614
615 if (CLIB_DEBUG && !s0)
616 {
617 clib_warning ("It's dead, Jim!");
618 continue;
619 }
Florin Corasb2215d62017-08-01 16:56:58 -0700620 /* Can retransmit for closed sessions but can't do anything if
621 * session is not ready or closed */
622 if (PREDICT_FALSE (s0->session_state < SESSION_STATE_READY))
Florin Corasa5464812017-04-19 13:00:05 -0700623 continue;
Dave Barach68b0fb02017-02-28 15:15:56 -0500624 /* Spray packets in per session type frames, since they go to
625 * different nodes */
Florin Corase69f4952017-03-07 10:06:24 -0800626 rv = (smm->session_tx_fns[s0->session_type]) (vm, node, smm, e0, s0,
Dave Barach68b0fb02017-02-28 15:15:56 -0500627 my_thread_index,
628 &n_tx_packets);
Florin Coras6792ec02017-03-13 03:49:51 -0700629 /* Out of buffers */
Florin Coras93992a92017-05-24 18:03:56 -0700630 if (PREDICT_FALSE (rv < 0))
Dave Barachacd2a6a2017-05-16 17:41:34 -0400631 {
Florin Coras93992a92017-05-24 18:03:56 -0700632 vlib_node_increment_counter (vm, node->node_index,
633 SESSION_QUEUE_ERROR_NO_BUFFER, 1);
Dave Barachacd2a6a2017-05-16 17:41:34 -0400634 continue;
635 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500636 break;
Florin Corasa5464812017-04-19 13:00:05 -0700637 case FIFO_EVENT_DISCONNECT:
638 s0 = stream_session_get_from_handle (e0->session_handle);
Florin Coras6792ec02017-03-13 03:49:51 -0700639 stream_session_disconnect (s0);
640 break;
641 case FIFO_EVENT_BUILTIN_RX:
Florin Corasa5464812017-04-19 13:00:05 -0700642 s0 = session_event_get_session (e0, my_thread_index);
Florin Coras6792ec02017-03-13 03:49:51 -0700643 svm_fifo_unset_event (s0->server_rx_fifo);
Florin Coras6792ec02017-03-13 03:49:51 -0700644 app = application_get (s0->app_index);
645 app->cb_fns.builtin_server_rx_callback (s0);
646 break;
Dave Barache5f1d272017-05-10 13:34:04 -0400647 case FIFO_EVENT_RPC:
648 fp = e0->rpc_args.fp;
649 (*fp) (e0->rpc_args.arg);
650 break;
651
Dave Barach68b0fb02017-02-28 15:15:56 -0500652 default:
653 clib_warning ("unhandled event type %d", e0->event_type);
654 }
655 }
656
Dave Barachacd2a6a2017-05-16 17:41:34 -0400657 _vec_len (my_fifo_events) = 0;
658 smm->free_event_vector[my_thread_index] = my_fifo_events;
Dave Barach68b0fb02017-02-28 15:15:56 -0500659
660 vlib_node_increment_counter (vm, session_queue_node.index,
661 SESSION_QUEUE_ERROR_TX, n_tx_packets);
662
Florin Coras6792ec02017-03-13 03:49:51 -0700663 SESSION_EVT_DBG (SESSION_EVT_DEQ_NODE, 1);
664
Dave Barach68b0fb02017-02-28 15:15:56 -0500665 return n_tx_packets;
666}
667
668/* *INDENT-OFF* */
669VLIB_REGISTER_NODE (session_queue_node) =
670{
671 .function = session_queue_node_fn,
672 .name = "session-queue",
673 .format_trace = format_session_queue_trace,
674 .type = VLIB_NODE_TYPE_INPUT,
675 .n_errors = ARRAY_LEN (session_queue_error_strings),
676 .error_strings = session_queue_error_strings,
677 .n_next_nodes = SESSION_QUEUE_N_NEXT,
Florin Corase04c2992017-03-01 08:17:34 -0800678 .state = VLIB_NODE_STATE_DISABLED,
Dave Barach68b0fb02017-02-28 15:15:56 -0500679 .next_nodes =
680 {
681 [SESSION_QUEUE_NEXT_DROP] = "error-drop",
682 [SESSION_QUEUE_NEXT_IP4_LOOKUP] = "ip4-lookup",
683 [SESSION_QUEUE_NEXT_IP6_LOOKUP] = "ip6-lookup",
684 [SESSION_QUEUE_NEXT_TCP_IP4_OUTPUT] = "tcp4-output",
685 [SESSION_QUEUE_NEXT_TCP_IP6_OUTPUT] = "tcp6-output",
686 },
687};
688/* *INDENT-ON* */
689
690/*
691 * fd.io coding-style-patch-verification: ON
692 *
693 * Local Variables:
694 * eval: (c-set-style "gnu")
695 * End:
696 */