blob: 1ae92edf1ead32ef7bf97e19aa985e327e7bbb56 [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 <vppinfra/elog.h>
Florin Coras561af9b2017-12-09 10:19:43 -080020#include <vnet/session/transport.h>
Florin Coras8e43d042018-05-04 15:46:57 -070021#include <vnet/session/session.h>
Florin Coras6792ec02017-03-13 03:49:51 -070022#include <vnet/session/application.h>
Florin Coras52207f12018-07-12 14:48:06 -070023#include <vnet/session/application_interface.h>
Florin Corase69f4952017-03-07 10:06:24 -080024#include <vnet/session/session_debug.h>
Florin Corase86a8ed2018-01-05 03:20:25 -080025#include <svm/queue.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050026
Florin Coras52207f12018-07-12 14:48:06 -070027static void
28session_mq_accepted_reply_handler (void *data)
29{
30 session_accepted_reply_msg_t *mp = (session_accepted_reply_msg_t *) data;
31 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
32 local_session_t *ls;
33 stream_session_t *s;
34
35 /* Server isn't interested, kill the session */
36 if (mp->retval)
37 {
38 a->app_index = mp->context;
39 a->handle = mp->handle;
40 vnet_disconnect_session (a);
41 return;
42 }
43
44 if (session_handle_is_local (mp->handle))
45 {
46 ls = application_get_local_session_from_handle (mp->handle);
47 if (!ls || ls->app_index != mp->context)
48 {
49 clib_warning ("server %u doesn't own local handle %llu",
50 mp->context, mp->handle);
51 return;
52 }
53 if (application_local_session_connect_notify (ls))
54 return;
55 ls->session_state = SESSION_STATE_READY;
56 }
57 else
58 {
59 s = session_get_from_handle_if_valid (mp->handle);
60 if (!s)
61 {
62 clib_warning ("session doesn't exist");
63 return;
64 }
65 if (s->app_index != mp->context)
66 {
67 clib_warning ("app doesn't own session");
68 return;
69 }
70 s->session_state = SESSION_STATE_READY;
71 }
72}
73
74static void
75session_mq_reset_reply_handler (void *data)
76{
77 session_reset_reply_msg_t *mp;
78 application_t *app;
79 stream_session_t *s;
80 u32 index, thread_index;
81
82 mp = (session_reset_reply_msg_t *) data;
83 app = application_lookup (mp->client_index);
84 if (!app)
85 return;
86
87 session_parse_handle (mp->handle, &index, &thread_index);
88 s = session_get_if_valid (index, thread_index);
89 if (s == 0 || app->index != s->app_index)
90 {
91 clib_warning ("Invalid session!");
92 return;
93 }
94
95 /* Client objected to resetting the session, log and continue */
96 if (mp->retval)
97 {
98 clib_warning ("client retval %d", mp->retval);
99 return;
100 }
101
102 /* This comes as a response to a reset, transport only waiting for
103 * confirmation to remove connection state, no need to disconnect */
104 stream_session_cleanup (s);
105}
106
107static void
108session_mq_disconnected_handler (void *data)
109{
110 session_disconnected_reply_msg_t *rmp;
111 vnet_disconnect_args_t _a, *a = &_a;
112 svm_msg_q_msg_t _msg, *msg = &_msg;
113 session_disconnected_msg_t *mp;
114 session_event_t *evt;
Florin Coras3d0fadc2018-07-17 05:35:47 -0700115 stream_session_t *s;
Florin Coras52207f12018-07-12 14:48:06 -0700116 application_t *app;
117 int rv = 0;
118
119 mp = (session_disconnected_msg_t *) data;
120 app = application_lookup (mp->client_index);
Florin Coras3d0fadc2018-07-17 05:35:47 -0700121 s = session_get_from_handle_if_valid (mp->handle);
122 if (!(app && s && s->app_index == app->index))
Florin Coras52207f12018-07-12 14:48:06 -0700123 {
Florin Coras3d0fadc2018-07-17 05:35:47 -0700124 clib_warning ("could not disconnect session: %llu app: %u", mp->handle,
125 mp->client_index);
126 return;
Florin Coras52207f12018-07-12 14:48:06 -0700127 }
Florin Coras3d0fadc2018-07-17 05:35:47 -0700128
129 a->handle = mp->handle;
130 a->app_index = app->index;
131 rv = vnet_disconnect_session (a);
Florin Coras52207f12018-07-12 14:48:06 -0700132
133 svm_msg_q_lock_and_alloc_msg_w_ring (app->event_queue,
134 SESSION_MQ_CTRL_EVT_RING,
135 SVM_Q_WAIT, msg);
136 svm_msg_q_unlock (app->event_queue);
137 evt = svm_msg_q_msg_data (app->event_queue, msg);
138 memset (evt, 0, sizeof (*evt));
139 evt->event_type = SESSION_CTRL_EVT_DISCONNECTED;
140 rmp = (session_disconnected_reply_msg_t *) evt->data;
141 rmp->handle = mp->handle;
142 rmp->context = mp->context;
143 rmp->retval = rv;
144 svm_msg_q_add (app->event_queue, msg, SVM_Q_WAIT);
145}
146
147static void
148session_mq_disconnected_reply_handler (void *data)
149{
150 session_disconnected_reply_msg_t *mp;
151 vnet_disconnect_args_t _a, *a = &_a;
152 application_t *app;
153
154 mp = (session_disconnected_reply_msg_t *) data;
155
156 /* Client objected to disconnecting the session, log and continue */
157 if (mp->retval)
158 {
159 clib_warning ("client retval %d", mp->retval);
160 return;
161 }
162
163 /* Disconnect has been confirmed. Confirm close to transport */
164 app = application_lookup (mp->context);
165 if (app)
166 {
167 a->handle = mp->handle;
168 a->app_index = app->index;
169 vnet_disconnect_session (a);
170 }
171}
172
Dave Barach68b0fb02017-02-28 15:15:56 -0500173vlib_node_registration_t session_queue_node;
174
175typedef struct
176{
177 u32 session_index;
178 u32 server_thread_index;
179} session_queue_trace_t;
180
181/* packet trace format function */
182static u8 *
183format_session_queue_trace (u8 * s, va_list * args)
184{
185 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
186 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
187 session_queue_trace_t *t = va_arg (*args, session_queue_trace_t *);
188
189 s = format (s, "SESSION_QUEUE: session index %d, server thread index %d",
190 t->session_index, t->server_thread_index);
191 return s;
192}
193
Florin Coras6792ec02017-03-13 03:49:51 -0700194#define foreach_session_queue_error \
195_(TX, "Packets transmitted") \
Florin Coras93992a92017-05-24 18:03:56 -0700196_(TIMER, "Timer events") \
197_(NO_BUFFER, "Out of buffers")
Dave Barach68b0fb02017-02-28 15:15:56 -0500198
199typedef enum
200{
201#define _(sym,str) SESSION_QUEUE_ERROR_##sym,
202 foreach_session_queue_error
203#undef _
204 SESSION_QUEUE_N_ERROR,
205} session_queue_error_t;
206
207static char *session_queue_error_strings[] = {
208#define _(sym,string) string,
209 foreach_session_queue_error
210#undef _
211};
212
Florin Coras0d60a0f2018-06-29 02:02:08 -0700213enum
214{
215 SESSION_TX_NO_BUFFERS = -2,
216 SESSION_TX_NO_DATA,
217 SESSION_TX_OK
218};
219
Florin Coras66cf5e02018-06-08 09:17:39 -0700220static void
221session_tx_trace_frame (vlib_main_t * vm, vlib_node_runtime_t * node,
222 u32 next_index, u32 * to_next, u16 n_segs,
223 stream_session_t * s, u32 n_trace)
Florin Corasf6d68ed2017-05-07 19:12:02 -0700224{
Florin Coras8e43d042018-05-04 15:46:57 -0700225 session_queue_trace_t *t;
Florin Coras66cf5e02018-06-08 09:17:39 -0700226 vlib_buffer_t *b;
227 int i;
228
229 for (i = 0; i < clib_min (n_trace, n_segs); i++)
230 {
231 b = vlib_get_buffer (vm, to_next[i - n_segs]);
232 vlib_trace_buffer (vm, node, next_index, b, 1 /* follow_chain */ );
Florin Coras66cf5e02018-06-08 09:17:39 -0700233 t = vlib_add_trace (vm, node, b, sizeof (*t));
234 t->session_index = s->session_index;
235 t->server_thread_index = s->thread_index;
236 }
Florin Coras4df38712018-06-20 12:44:16 -0700237 vlib_set_trace_count (vm, node, n_trace - i);
Florin Coras8e43d042018-05-04 15:46:57 -0700238}
Florin Corasf6d68ed2017-05-07 19:12:02 -0700239
Florin Coras8e43d042018-05-04 15:46:57 -0700240always_inline void
241session_tx_fifo_chain_tail (vlib_main_t * vm, session_tx_context_t * ctx,
242 vlib_buffer_t * b, u16 * n_bufs, u8 peek_data)
243{
244 session_manager_main_t *smm = &session_manager_main;
245 vlib_buffer_t *chain_b, *prev_b;
246 u32 chain_bi0, to_deq, left_from_seg;
247 u16 len_to_deq, n_bytes_read;
248 u8 *data, j;
Florin Corasb2215d62017-08-01 16:56:58 -0700249
Florin Coras8e43d042018-05-04 15:46:57 -0700250 b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
251 b->total_length_not_including_first_buffer = 0;
252
253 chain_b = b;
254 left_from_seg = clib_min (ctx->snd_mss - b->current_length,
255 ctx->left_to_snd);
Florin Corasb2215d62017-08-01 16:56:58 -0700256 to_deq = left_from_seg;
Florin Coras8e43d042018-05-04 15:46:57 -0700257 for (j = 1; j < ctx->n_bufs_per_seg; j++)
Florin Corasf6d68ed2017-05-07 19:12:02 -0700258 {
Florin Coras8e43d042018-05-04 15:46:57 -0700259 prev_b = chain_b;
260 len_to_deq = clib_min (to_deq, ctx->deq_per_buf);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700261
262 *n_bufs -= 1;
Florin Coras8e43d042018-05-04 15:46:57 -0700263 chain_bi0 = smm->tx_buffers[ctx->s->thread_index][*n_bufs];
264 _vec_len (smm->tx_buffers[ctx->s->thread_index]) = *n_bufs;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700265
Florin Coras8e43d042018-05-04 15:46:57 -0700266 chain_b = vlib_get_buffer (vm, chain_bi0);
267 chain_b->current_data = 0;
268 data = vlib_buffer_get_current (chain_b);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700269 if (peek_data)
270 {
Florin Coras8e43d042018-05-04 15:46:57 -0700271 n_bytes_read = svm_fifo_peek (ctx->s->server_tx_fifo,
272 ctx->tx_offset, len_to_deq, data);
273 ctx->tx_offset += n_bytes_read;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700274 }
275 else
276 {
Florin Coras8e43d042018-05-04 15:46:57 -0700277 if (ctx->transport_vft->tx_type == TRANSPORT_TX_DGRAM)
Florin Coras7fb0fe12018-04-09 09:24:52 -0700278 {
Florin Coras8e43d042018-05-04 15:46:57 -0700279 svm_fifo_t *f = ctx->s->server_tx_fifo;
280 session_dgram_hdr_t *hdr = &ctx->hdr;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700281 u16 deq_now;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700282 deq_now = clib_min (hdr->data_length - hdr->data_offset,
Florin Coras8e43d042018-05-04 15:46:57 -0700283 len_to_deq);
284 n_bytes_read = svm_fifo_peek (f, hdr->data_offset, deq_now,
285 data);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700286 ASSERT (n_bytes_read > 0);
287
288 hdr->data_offset += n_bytes_read;
289 if (hdr->data_offset == hdr->data_length)
Florin Coras8e43d042018-05-04 15:46:57 -0700290 svm_fifo_dequeue_drop (f, hdr->data_length);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700291 }
292 else
Florin Coras8e43d042018-05-04 15:46:57 -0700293 n_bytes_read = svm_fifo_dequeue_nowait (ctx->s->server_tx_fifo,
294 len_to_deq, data);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700295 }
Florin Coras8e43d042018-05-04 15:46:57 -0700296 ASSERT (n_bytes_read == len_to_deq);
297 chain_b->current_length = n_bytes_read;
298 b->total_length_not_including_first_buffer += chain_b->current_length;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700299
300 /* update previous buffer */
Florin Coras8e43d042018-05-04 15:46:57 -0700301 prev_b->next_buffer = chain_bi0;
302 prev_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700303
304 /* update current buffer */
Florin Coras8e43d042018-05-04 15:46:57 -0700305 chain_b->next_buffer = 0;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700306
Florin Corasb2215d62017-08-01 16:56:58 -0700307 to_deq -= n_bytes_read;
308 if (to_deq == 0)
Florin Corasf6d68ed2017-05-07 19:12:02 -0700309 break;
310 }
Florin Coras1f152cd2017-08-18 19:28:03 -0700311 ASSERT (to_deq == 0
Florin Coras8e43d042018-05-04 15:46:57 -0700312 && b->total_length_not_including_first_buffer == left_from_seg);
313 ctx->left_to_snd -= left_from_seg;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700314}
315
Dave Barach68b0fb02017-02-28 15:15:56 -0500316always_inline int
Florin Coras8e43d042018-05-04 15:46:57 -0700317session_output_try_get_buffers (vlib_main_t * vm,
Florin Corasd79b41e2017-03-04 05:37:52 -0800318 session_manager_main_t * smm,
Florin Coras8e43d042018-05-04 15:46:57 -0700319 u32 thread_index, u16 * n_bufs, u32 wanted)
Dave Barach68b0fb02017-02-28 15:15:56 -0500320{
Florin Corasf08f26d2018-05-10 13:20:47 -0700321 u32 n_alloc;
322 vec_validate_aligned (smm->tx_buffers[thread_index], wanted - 1,
Florin Coras8e43d042018-05-04 15:46:57 -0700323 CLIB_CACHE_LINE_BYTES);
Florin Corasf08f26d2018-05-10 13:20:47 -0700324 n_alloc = vlib_buffer_alloc (vm, &smm->tx_buffers[thread_index][*n_bufs],
325 wanted - *n_bufs);
326 *n_bufs += n_alloc;
Florin Coras8e43d042018-05-04 15:46:57 -0700327 _vec_len (smm->tx_buffers[thread_index]) = *n_bufs;
Florin Corasf08f26d2018-05-10 13:20:47 -0700328 return n_alloc;
Florin Coras8e43d042018-05-04 15:46:57 -0700329}
330
331always_inline void
332session_tx_fill_buffer (vlib_main_t * vm, session_tx_context_t * ctx,
333 vlib_buffer_t * b, u16 * n_bufs, u8 peek_data)
334{
335 u32 len_to_deq;
336 u8 *data0;
337 int n_bytes_read;
338
339 /*
340 * Start with the first buffer in chain
341 */
342 b->error = 0;
343 b->flags = VNET_BUFFER_F_LOCALLY_ORIGINATED;
344 b->current_data = 0;
Florin Coras8e43d042018-05-04 15:46:57 -0700345
346 data0 = vlib_buffer_make_headroom (b, MAX_HDRS_LEN);
347 len_to_deq = clib_min (ctx->left_to_snd, ctx->deq_per_first_buf);
348
Florin Coras7fb0fe12018-04-09 09:24:52 -0700349 if (peek_data)
350 {
Florin Coras8e43d042018-05-04 15:46:57 -0700351 n_bytes_read = svm_fifo_peek (ctx->s->server_tx_fifo, ctx->tx_offset,
352 len_to_deq, data0);
353 ASSERT (n_bytes_read > 0);
354 /* Keep track of progress locally, transport is also supposed to
355 * increment it independently when pushing the header */
356 ctx->tx_offset += n_bytes_read;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700357 }
358 else
359 {
Florin Coras8e43d042018-05-04 15:46:57 -0700360 if (ctx->transport_vft->tx_type == TRANSPORT_TX_DGRAM)
361 {
362 session_dgram_hdr_t *hdr = &ctx->hdr;
363 svm_fifo_t *f = ctx->s->server_tx_fifo;
364 u16 deq_now;
365 u32 offset;
366
367 ASSERT (hdr->data_length > hdr->data_offset);
368 deq_now = clib_min (hdr->data_length - hdr->data_offset,
369 len_to_deq);
370 offset = hdr->data_offset + SESSION_CONN_HDR_LEN;
371 n_bytes_read = svm_fifo_peek (f, offset, deq_now, data0);
372 ASSERT (n_bytes_read > 0);
373
374 if (ctx->s->session_state == SESSION_STATE_LISTENING)
375 {
376 ip_copy (&ctx->tc->rmt_ip, &hdr->rmt_ip, ctx->tc->is_ip4);
377 ctx->tc->rmt_port = hdr->rmt_port;
378 }
379 hdr->data_offset += n_bytes_read;
380 if (hdr->data_offset == hdr->data_length)
381 {
382 offset = hdr->data_length + SESSION_CONN_HDR_LEN;
383 svm_fifo_dequeue_drop (f, offset);
384 }
385 }
Florin Coras7fb0fe12018-04-09 09:24:52 -0700386 else
387 {
Florin Coras8e43d042018-05-04 15:46:57 -0700388 n_bytes_read = svm_fifo_dequeue_nowait (ctx->s->server_tx_fifo,
389 len_to_deq, data0);
390 ASSERT (n_bytes_read > 0);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700391 }
392 }
Florin Coras8e43d042018-05-04 15:46:57 -0700393 b->current_length = n_bytes_read;
394 ctx->left_to_snd -= n_bytes_read;
Dave Barach68b0fb02017-02-28 15:15:56 -0500395
Florin Coras8e43d042018-05-04 15:46:57 -0700396 /*
397 * Fill in the remaining buffers in the chain, if any
398 */
399 if (PREDICT_FALSE (ctx->n_bufs_per_seg > 1 && ctx->left_to_snd))
400 session_tx_fifo_chain_tail (vm, ctx, b, n_bufs, peek_data);
Dave Barach68b0fb02017-02-28 15:15:56 -0500401
Florin Coras8e43d042018-05-04 15:46:57 -0700402 /* *INDENT-OFF* */
Florin Coras8b20bf52018-06-14 14:55:50 -0700403 SESSION_EVT_DBG(SESSION_EVT_DEQ, ctx->s, ({
404 ed->data[0] = FIFO_EVENT_APP_TX;
405 ed->data[1] = ctx->max_dequeue;
Florin Coras8e43d042018-05-04 15:46:57 -0700406 ed->data[2] = len_to_deq;
Florin Coras8b20bf52018-06-14 14:55:50 -0700407 ed->data[3] = ctx->left_to_snd;
Florin Coras8e43d042018-05-04 15:46:57 -0700408 }));
409 /* *INDENT-ON* */
410}
411
412always_inline u8
413session_tx_not_ready (stream_session_t * s, u8 peek_data)
414{
415 if (peek_data)
Florin Corase04c2992017-03-01 08:17:34 -0800416 {
Florin Coras8e43d042018-05-04 15:46:57 -0700417 /* Can retransmit for closed sessions but can't send new data if
418 * session is not ready or closed */
419 if (s->session_state < SESSION_STATE_READY)
420 return 1;
Florin Corasca1c8f32018-05-23 21:01:30 -0700421 if (s->session_state == SESSION_STATE_CLOSED)
422 return 2;
Florin Corase04c2992017-03-01 08:17:34 -0800423 }
Florin Coras8e43d042018-05-04 15:46:57 -0700424 return 0;
425}
Dave Barach68b0fb02017-02-28 15:15:56 -0500426
Florin Coras8e43d042018-05-04 15:46:57 -0700427always_inline transport_connection_t *
428session_tx_get_transport (session_tx_context_t * ctx, u8 peek_data)
429{
430 if (peek_data)
431 {
432 return ctx->transport_vft->get_connection (ctx->s->connection_index,
433 ctx->s->thread_index);
434 }
435 else
436 {
437 if (ctx->s->session_state == SESSION_STATE_LISTENING)
438 return ctx->transport_vft->get_listener (ctx->s->connection_index);
439 else
440 {
441 return ctx->transport_vft->get_connection (ctx->s->connection_index,
442 ctx->s->thread_index);
443 }
444 }
445}
Florin Coras6a9b68b2017-11-21 04:20:42 -0800446
Florin Coras8e43d042018-05-04 15:46:57 -0700447always_inline void
448session_tx_set_dequeue_params (vlib_main_t * vm, session_tx_context_t * ctx,
Florin Corasf08f26d2018-05-10 13:20:47 -0700449 u32 max_segs, u8 peek_data)
Florin Coras8e43d042018-05-04 15:46:57 -0700450{
451 u32 n_bytes_per_buf, n_bytes_per_seg;
452 ctx->max_dequeue = svm_fifo_max_dequeue (ctx->s->server_tx_fifo);
Dave Barach68b0fb02017-02-28 15:15:56 -0500453 if (peek_data)
454 {
Florin Coras9d063042017-09-14 03:08:00 -0400455 /* Offset in rx fifo from where to peek data */
Florin Coras8e43d042018-05-04 15:46:57 -0700456 ctx->tx_offset = ctx->transport_vft->tx_fifo_offset (ctx->tc);
457 if (PREDICT_FALSE (ctx->tx_offset >= ctx->max_dequeue))
458 {
459 ctx->max_len_to_snd = 0;
460 return;
461 }
462 ctx->max_dequeue -= ctx->tx_offset;
Dave Barach68b0fb02017-02-28 15:15:56 -0500463 }
Florin Coras7fb0fe12018-04-09 09:24:52 -0700464 else
465 {
Florin Coras8e43d042018-05-04 15:46:57 -0700466 if (ctx->transport_vft->tx_type == TRANSPORT_TX_DGRAM)
Florin Coras7fb0fe12018-04-09 09:24:52 -0700467 {
Florin Coras8e43d042018-05-04 15:46:57 -0700468 if (ctx->max_dequeue <= sizeof (ctx->hdr))
469 {
470 ctx->max_len_to_snd = 0;
471 return;
472 }
473 svm_fifo_peek (ctx->s->server_tx_fifo, 0, sizeof (ctx->hdr),
474 (u8 *) & ctx->hdr);
475 ASSERT (ctx->hdr.data_length > ctx->hdr.data_offset);
476 ctx->max_dequeue = ctx->hdr.data_length - ctx->hdr.data_offset;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700477 }
478 }
Florin Coras8e43d042018-05-04 15:46:57 -0700479 ASSERT (ctx->max_dequeue > 0);
Florin Coras6792ec02017-03-13 03:49:51 -0700480
481 /* Ensure we're not writing more than transport window allows */
Florin Coras8e43d042018-05-04 15:46:57 -0700482 if (ctx->max_dequeue < ctx->snd_space)
Florin Coras3e350af2017-03-30 02:54:28 -0700483 {
484 /* Constrained by tx queue. Try to send only fully formed segments */
Florin Coras8e43d042018-05-04 15:46:57 -0700485 ctx->max_len_to_snd =
486 (ctx->max_dequeue > ctx->snd_mss) ?
487 ctx->max_dequeue - ctx->max_dequeue % ctx->snd_mss : ctx->max_dequeue;
Florin Coras3e350af2017-03-30 02:54:28 -0700488 /* TODO Nagle ? */
489 }
490 else
491 {
Florin Coras1f152cd2017-08-18 19:28:03 -0700492 /* Expectation is that snd_space0 is already a multiple of snd_mss */
Florin Coras8e43d042018-05-04 15:46:57 -0700493 ctx->max_len_to_snd = ctx->snd_space;
Florin Coras3e350af2017-03-30 02:54:28 -0700494 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500495
Florin Corasf08f26d2018-05-10 13:20:47 -0700496 /* Check if we're tx constrained by the node */
497 ctx->n_segs_per_evt = ceil ((f64) ctx->max_len_to_snd / ctx->snd_mss);
498 if (ctx->n_segs_per_evt > max_segs)
499 {
500 ctx->n_segs_per_evt = max_segs;
501 ctx->max_len_to_snd = max_segs * ctx->snd_mss;
502 }
503
Florin Coras8e43d042018-05-04 15:46:57 -0700504 n_bytes_per_buf = vlib_buffer_free_list_buffer_size (vm,
505 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
Florin Corase87216f2017-08-17 16:59:22 -0700506 ASSERT (n_bytes_per_buf > MAX_HDRS_LEN);
Florin Coras8e43d042018-05-04 15:46:57 -0700507 n_bytes_per_seg = MAX_HDRS_LEN + ctx->snd_mss;
Florin Corasf08f26d2018-05-10 13:20:47 -0700508 ctx->n_bufs_per_seg = ceil ((f64) n_bytes_per_seg / n_bytes_per_buf);
Florin Coras8e43d042018-05-04 15:46:57 -0700509 ctx->deq_per_buf = clib_min (ctx->snd_mss, n_bytes_per_buf);
510 ctx->deq_per_first_buf = clib_min (ctx->snd_mss,
511 n_bytes_per_buf - MAX_HDRS_LEN);
512}
Florin Corasf6d68ed2017-05-07 19:12:02 -0700513
Florin Coras8e43d042018-05-04 15:46:57 -0700514always_inline int
515session_tx_fifo_read_and_snd_i (vlib_main_t * vm, vlib_node_runtime_t * node,
Florin Coras52207f12018-07-12 14:48:06 -0700516 session_event_t * e,
Florin Coras8e43d042018-05-04 15:46:57 -0700517 stream_session_t * s, int *n_tx_packets,
518 u8 peek_data)
519{
Florin Coras66cf5e02018-06-08 09:17:39 -0700520 u32 next_index, next0, next1, *to_next, n_left_to_next;
Florin Corasf08f26d2018-05-10 13:20:47 -0700521 u32 n_trace = vlib_get_trace_count (vm, node), n_bufs_needed = 0;
522 u32 thread_index = s->thread_index, n_left, pbi;
Florin Coras8e43d042018-05-04 15:46:57 -0700523 session_manager_main_t *smm = &session_manager_main;
524 session_tx_context_t *ctx = &smm->ctx[thread_index];
525 transport_proto_t tp;
526 vlib_buffer_t *pb;
Florin Corasca1c8f32018-05-23 21:01:30 -0700527 u16 n_bufs, rv;
Florin Coras8e43d042018-05-04 15:46:57 -0700528
Florin Corasca1c8f32018-05-23 21:01:30 -0700529 if (PREDICT_FALSE ((rv = session_tx_not_ready (s, peek_data))))
Florin Coras8e43d042018-05-04 15:46:57 -0700530 {
Florin Corasca1c8f32018-05-23 21:01:30 -0700531 if (rv < 2)
532 vec_add1 (smm->pending_event_vector[thread_index], *e);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700533 return SESSION_TX_NO_DATA;
Florin Coras8e43d042018-05-04 15:46:57 -0700534 }
535
536 next_index = smm->session_type_to_next[s->session_type];
Florin Coras66cf5e02018-06-08 09:17:39 -0700537 next0 = next1 = next_index;
Florin Coras8e43d042018-05-04 15:46:57 -0700538
539 tp = session_get_transport_proto (s);
540 ctx->s = s;
541 ctx->transport_vft = transport_protocol_get_vft (tp);
542 ctx->tc = session_tx_get_transport (ctx, peek_data);
543 ctx->snd_mss = ctx->transport_vft->send_mss (ctx->tc);
544 ctx->snd_space = ctx->transport_vft->send_space (ctx->tc);
545 if (ctx->snd_space == 0 || ctx->snd_mss == 0)
546 {
547 vec_add1 (smm->pending_event_vector[thread_index], *e);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700548 return SESSION_TX_NO_DATA;
Florin Coras8e43d042018-05-04 15:46:57 -0700549 }
550
551 /* Allow enqueuing of a new event */
552 svm_fifo_unset_event (s->server_tx_fifo);
553
554 /* Check how much we can pull. */
Florin Corasf08f26d2018-05-10 13:20:47 -0700555 session_tx_set_dequeue_params (vm, ctx, VLIB_FRAME_SIZE - *n_tx_packets,
556 peek_data);
557
Florin Coras8e43d042018-05-04 15:46:57 -0700558 if (PREDICT_FALSE (!ctx->max_len_to_snd))
Florin Coras0d60a0f2018-06-29 02:02:08 -0700559 return SESSION_TX_NO_DATA;
Dave Barach68b0fb02017-02-28 15:15:56 -0500560
561 n_bufs = vec_len (smm->tx_buffers[thread_index]);
Florin Corasf08f26d2018-05-10 13:20:47 -0700562 n_bufs_needed = ctx->n_segs_per_evt * ctx->n_bufs_per_seg;
Florin Coras8e43d042018-05-04 15:46:57 -0700563
564 /*
565 * Make sure we have at least one full frame of buffers ready
566 */
Florin Corasf08f26d2018-05-10 13:20:47 -0700567 if (n_bufs < n_bufs_needed)
Dave Barach68b0fb02017-02-28 15:15:56 -0500568 {
Florin Coras8e43d042018-05-04 15:46:57 -0700569 session_output_try_get_buffers (vm, smm, thread_index, &n_bufs,
Florin Corasf08f26d2018-05-10 13:20:47 -0700570 ctx->n_bufs_per_seg * VLIB_FRAME_SIZE);
571 if (PREDICT_FALSE (n_bufs < n_bufs_needed))
Dave Barach68b0fb02017-02-28 15:15:56 -0500572 {
Florin Coras8e43d042018-05-04 15:46:57 -0700573 vec_add1 (smm->pending_event_vector[thread_index], *e);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700574 return SESSION_TX_NO_BUFFERS;
Dave Barach68b0fb02017-02-28 15:15:56 -0500575 }
Florin Coras8e43d042018-05-04 15:46:57 -0700576 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500577
Florin Coras8e43d042018-05-04 15:46:57 -0700578 /*
579 * Write until we fill up a frame
580 */
581 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Florin Corasf08f26d2018-05-10 13:20:47 -0700582 if (PREDICT_FALSE (ctx->n_segs_per_evt > n_left_to_next))
Florin Coras8e43d042018-05-04 15:46:57 -0700583 {
Florin Corasf08f26d2018-05-10 13:20:47 -0700584 ctx->n_segs_per_evt = n_left_to_next;
585 ctx->max_len_to_snd = ctx->snd_mss * n_left_to_next;
586 }
587 ctx->left_to_snd = ctx->max_len_to_snd;
588 n_left = ctx->n_segs_per_evt;
Florin Coras66cf5e02018-06-08 09:17:39 -0700589
590 while (n_left >= 4)
591 {
592 vlib_buffer_t *b0, *b1;
593 u32 bi0, bi1;
594
595 pbi = smm->tx_buffers[thread_index][n_bufs - 3];
596 pb = vlib_get_buffer (vm, pbi);
597 vlib_prefetch_buffer_header (pb, STORE);
598 pbi = smm->tx_buffers[thread_index][n_bufs - 4];
599 pb = vlib_get_buffer (vm, pbi);
600 vlib_prefetch_buffer_header (pb, STORE);
601
602 to_next[0] = bi0 = smm->tx_buffers[thread_index][--n_bufs];
603 to_next[1] = bi1 = smm->tx_buffers[thread_index][--n_bufs];
604
605 b0 = vlib_get_buffer (vm, bi0);
606 b1 = vlib_get_buffer (vm, bi1);
607
608 session_tx_fill_buffer (vm, ctx, b0, &n_bufs, peek_data);
609 session_tx_fill_buffer (vm, ctx, b1, &n_bufs, peek_data);
610
611 ctx->transport_vft->push_header (ctx->tc, b0);
612 ctx->transport_vft->push_header (ctx->tc, b1);
613
614 to_next += 2;
615 n_left_to_next -= 2;
616 n_left -= 2;
617
618 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
619 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b1);
620
621 vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
622 n_left_to_next, bi0, bi1, next0,
623 next1);
624 }
Florin Corasf08f26d2018-05-10 13:20:47 -0700625 while (n_left)
626 {
Florin Coras66cf5e02018-06-08 09:17:39 -0700627 vlib_buffer_t *b0;
628 u32 bi0;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700629
Florin Coras91ca4622018-06-30 11:27:59 -0700630 if (n_left > 1)
631 {
632 pbi = smm->tx_buffers[thread_index][n_bufs - 2];
633 pb = vlib_get_buffer (vm, pbi);
634 vlib_prefetch_buffer_header (pb, STORE);
635 }
636
Florin Coras66cf5e02018-06-08 09:17:39 -0700637 to_next[0] = bi0 = smm->tx_buffers[thread_index][--n_bufs];
638 b0 = vlib_get_buffer (vm, bi0);
639 session_tx_fill_buffer (vm, ctx, b0, &n_bufs, peek_data);
Florin Coras81a13db2018-03-16 08:48:31 -0700640
Florin Coras66cf5e02018-06-08 09:17:39 -0700641 /* Ask transport to push header after current_length and
642 * total_length_not_including_first_buffer are updated */
643 ctx->transport_vft->push_header (ctx->tc, b0);
Florin Corasf6359c82017-06-19 12:26:09 -0400644
Florin Coras66cf5e02018-06-08 09:17:39 -0700645 to_next += 1;
646 n_left_to_next -= 1;
647 n_left -= 1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500648
Florin Coras66cf5e02018-06-08 09:17:39 -0700649 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700650
Florin Coras66cf5e02018-06-08 09:17:39 -0700651 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
652 n_left_to_next, bi0, next0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500653 }
Florin Coras66cf5e02018-06-08 09:17:39 -0700654
655 if (PREDICT_FALSE (n_trace > 0))
656 session_tx_trace_frame (vm, node, next_index, to_next,
657 ctx->n_segs_per_evt, s, n_trace);
658
Florin Coras8e43d042018-05-04 15:46:57 -0700659 _vec_len (smm->tx_buffers[thread_index]) = n_bufs;
Florin Corasf08f26d2018-05-10 13:20:47 -0700660 *n_tx_packets += ctx->n_segs_per_evt;
Florin Coras8e43d042018-05-04 15:46:57 -0700661 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
Dave Barach68b0fb02017-02-28 15:15:56 -0500662
Florin Coras6792ec02017-03-13 03:49:51 -0700663 /* If we couldn't dequeue all bytes mark as partially read */
Florin Corasf08f26d2018-05-10 13:20:47 -0700664 ASSERT (ctx->left_to_snd == 0);
Florin Coras8e43d042018-05-04 15:46:57 -0700665 if (ctx->max_len_to_snd < ctx->max_dequeue)
666 if (svm_fifo_set_event (s->server_tx_fifo))
667 vec_add1 (smm->pending_event_vector[thread_index], *e);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700668
Florin Coras8e43d042018-05-04 15:46:57 -0700669 if (!peek_data && ctx->transport_vft->tx_type == TRANSPORT_TX_DGRAM)
Dave Barach68b0fb02017-02-28 15:15:56 -0500670 {
Florin Coras7fb0fe12018-04-09 09:24:52 -0700671 /* Fix dgram pre header */
Florin Coras8e43d042018-05-04 15:46:57 -0700672 if (ctx->max_len_to_snd < ctx->max_dequeue)
673 svm_fifo_overwrite_head (s->server_tx_fifo, (u8 *) & ctx->hdr,
Florin Coras7fb0fe12018-04-09 09:24:52 -0700674 sizeof (session_dgram_pre_hdr_t));
675 /* More data needs to be read */
Florin Coras8e43d042018-05-04 15:46:57 -0700676 else if (svm_fifo_max_dequeue (s->server_tx_fifo) > 0)
677 if (svm_fifo_set_event (s->server_tx_fifo))
678 vec_add1 (smm->pending_event_vector[thread_index], *e);
Dave Barach68b0fb02017-02-28 15:15:56 -0500679 }
Florin Coras0d60a0f2018-06-29 02:02:08 -0700680 return SESSION_TX_OK;
Dave Barach68b0fb02017-02-28 15:15:56 -0500681}
682
683int
Florin Corasd79b41e2017-03-04 05:37:52 -0800684session_tx_fifo_peek_and_snd (vlib_main_t * vm, vlib_node_runtime_t * node,
Florin Coras52207f12018-07-12 14:48:06 -0700685 session_event_t * e,
Florin Coras0d60a0f2018-06-29 02:02:08 -0700686 stream_session_t * s, int *n_tx_pkts)
Dave Barach68b0fb02017-02-28 15:15:56 -0500687{
Florin Coras0d60a0f2018-06-29 02:02:08 -0700688 return session_tx_fifo_read_and_snd_i (vm, node, e, s, n_tx_pkts, 1);
Dave Barach68b0fb02017-02-28 15:15:56 -0500689}
690
691int
Florin Corasd79b41e2017-03-04 05:37:52 -0800692session_tx_fifo_dequeue_and_snd (vlib_main_t * vm, vlib_node_runtime_t * node,
Florin Coras52207f12018-07-12 14:48:06 -0700693 session_event_t * e,
Florin Coras0d60a0f2018-06-29 02:02:08 -0700694 stream_session_t * s, int *n_tx_pkts)
Dave Barach68b0fb02017-02-28 15:15:56 -0500695{
Florin Coras0d60a0f2018-06-29 02:02:08 -0700696 return session_tx_fifo_read_and_snd_i (vm, node, e, s, n_tx_pkts, 0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500697}
698
Florin Coras371ca502018-02-21 12:07:41 -0800699int
700session_tx_fifo_dequeue_internal (vlib_main_t * vm,
701 vlib_node_runtime_t * node,
Florin Coras52207f12018-07-12 14:48:06 -0700702 session_event_t * e,
Florin Coras0d60a0f2018-06-29 02:02:08 -0700703 stream_session_t * s, int *n_tx_pkts)
Florin Coras371ca502018-02-21 12:07:41 -0800704{
705 application_t *app;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700706 app = application_get (s->opaque);
707 svm_fifo_unset_event (s->server_tx_fifo);
708 return app->cb_fns.builtin_app_tx_callback (s);
Florin Coras371ca502018-02-21 12:07:41 -0800709}
710
Dave Barach2c25a622017-06-26 11:35:07 -0400711always_inline stream_session_t *
Florin Coras52207f12018-07-12 14:48:06 -0700712session_event_get_session (session_event_t * e, u8 thread_index)
Florin Corasa5464812017-04-19 13:00:05 -0700713{
Florin Coras3cbc04b2017-10-02 00:18:51 -0700714 return session_get_if_valid (e->fifo->master_session_index, thread_index);
Florin Corasa5464812017-04-19 13:00:05 -0700715}
716
Florin Coras0d60a0f2018-06-29 02:02:08 -0700717static uword
718session_queue_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
719 vlib_frame_t * frame)
720{
721 session_manager_main_t *smm = vnet_get_session_manager_main ();
Florin Coras3c2fed52018-07-04 04:15:05 -0700722 u32 thread_index = vm->thread_index, n_to_dequeue, n_events;
Florin Coras52207f12018-07-12 14:48:06 -0700723 session_event_t *pending_events, *e;
724 session_event_t *fifo_events;
Florin Coras3c2fed52018-07-04 04:15:05 -0700725 svm_msg_q_msg_t _msg, *msg = &_msg;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700726 f64 now = vlib_time_now (vm);
Florin Coras3c2fed52018-07-04 04:15:05 -0700727 int n_tx_packets = 0, i, rv;
728 application_t *app;
729 svm_msg_q_t *mq;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700730 void (*fp) (void *);
731
732 SESSION_EVT_DBG (SESSION_EVT_POLL_GAP_TRACK, smm, thread_index);
733
734 /*
735 * Update transport time
736 */
737 transport_update_time (now, thread_index);
738
739 /*
Florin Coras3c2fed52018-07-04 04:15:05 -0700740 * Get vpp queue events that we can dequeue without blocking
Florin Coras0d60a0f2018-06-29 02:02:08 -0700741 */
Florin Coras3c2fed52018-07-04 04:15:05 -0700742 mq = smm->vpp_event_queues[thread_index];
Florin Coras0d60a0f2018-06-29 02:02:08 -0700743 fifo_events = smm->free_event_vector[thread_index];
Florin Coras3c2fed52018-07-04 04:15:05 -0700744 n_to_dequeue = svm_msg_q_size (mq);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700745 pending_events = smm->pending_event_vector[thread_index];
746
747 if (!n_to_dequeue && !vec_len (pending_events)
748 && !vec_len (smm->pending_disconnects[thread_index]))
749 return 0;
750
751 SESSION_EVT_DBG (SESSION_EVT_DEQ_NODE, 0);
752
753 /*
754 * If we didn't manage to process previous events try going
755 * over them again without dequeuing new ones.
756 * XXX: Handle senders to sessions that can't keep up
757 */
758 if (0 && vec_len (pending_events) >= 100)
759 {
760 clib_warning ("too many fifo events unsolved");
761 goto skip_dequeue;
762 }
763
764 /* See you in the next life, don't be late
Florin Coras3c2fed52018-07-04 04:15:05 -0700765 * XXX: we may need priorities here */
766 if (svm_msg_q_try_lock (mq))
Florin Coras0d60a0f2018-06-29 02:02:08 -0700767 return 0;
768
769 for (i = 0; i < n_to_dequeue; i++)
770 {
771 vec_add2 (fifo_events, e, 1);
Florin Coras3c2fed52018-07-04 04:15:05 -0700772 svm_msg_q_sub_w_lock (mq, msg);
773 clib_memcpy (e, svm_msg_q_msg_data (mq, msg), sizeof (*e));
774 svm_msg_q_free_msg (mq, msg);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700775 }
776
Florin Coras3c2fed52018-07-04 04:15:05 -0700777 svm_msg_q_unlock (mq);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700778
779 vec_append (fifo_events, pending_events);
780 vec_append (fifo_events, smm->pending_disconnects[thread_index]);
781
782 _vec_len (pending_events) = 0;
783 smm->pending_event_vector[thread_index] = pending_events;
784 _vec_len (smm->pending_disconnects[thread_index]) = 0;
785
786skip_dequeue:
787 n_events = vec_len (fifo_events);
788 for (i = 0; i < n_events; i++)
789 {
790 stream_session_t *s; /* $$$ prefetch 1 ahead maybe */
Florin Coras52207f12018-07-12 14:48:06 -0700791 session_event_t *e;
Florin Coras46f001d2018-07-11 05:25:06 -0700792 u32 to_dequeue;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700793
794 e = &fifo_events[i];
795 switch (e->event_type)
796 {
797 case FIFO_EVENT_APP_TX:
798 /* Don't try to send more that one frame per dispatch cycle */
799 if (n_tx_packets == VLIB_FRAME_SIZE)
800 {
801 vec_add1 (smm->pending_event_vector[thread_index], *e);
802 break;
803 }
804
805 s = session_event_get_session (e, thread_index);
806 if (PREDICT_FALSE (!s))
807 {
808 clib_warning ("It's dead, Jim!");
809 continue;
810 }
Florin Coras46f001d2018-07-11 05:25:06 -0700811 to_dequeue = svm_fifo_max_dequeue (s->server_tx_fifo);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700812
813 /* Spray packets in per session type frames, since they go to
814 * different nodes */
815 rv = (smm->session_tx_fns[s->session_type]) (vm, node, e, s,
816 &n_tx_packets);
817 if (PREDICT_TRUE (rv == SESSION_TX_OK))
818 {
Florin Coras46f001d2018-07-11 05:25:06 -0700819 /* Notify app there's tx space if not polling */
820 if (PREDICT_FALSE (to_dequeue == s->server_tx_fifo->nitems
821 && !svm_fifo_has_event (s->server_tx_fifo)))
822 session_dequeue_notify (s);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700823 }
824 else if (PREDICT_FALSE (rv == SESSION_TX_NO_BUFFERS))
825 {
826 vlib_node_increment_counter (vm, node->node_index,
827 SESSION_QUEUE_ERROR_NO_BUFFER, 1);
828 continue;
829 }
830 break;
831 case FIFO_EVENT_DISCONNECT:
832 /* Make sure stream disconnects run after the pending list is
833 * drained */
834 s = session_get_from_handle (e->session_handle);
835 if (!e->postponed)
836 {
837 e->postponed = 1;
838 vec_add1 (smm->pending_disconnects[thread_index], *e);
839 continue;
840 }
841 /* If tx queue is still not empty, wait */
842 if (svm_fifo_max_dequeue (s->server_tx_fifo))
843 {
844 vec_add1 (smm->pending_disconnects[thread_index], *e);
845 continue;
846 }
847
848 stream_session_disconnect_transport (s);
849 break;
850 case FIFO_EVENT_BUILTIN_RX:
851 s = session_event_get_session (e, thread_index);
852 if (PREDICT_FALSE (!s))
853 continue;
854 svm_fifo_unset_event (s->server_rx_fifo);
855 app = application_get (s->app_index);
856 app->cb_fns.builtin_app_rx_callback (s);
857 break;
858 case FIFO_EVENT_RPC:
859 fp = e->rpc_args.fp;
860 (*fp) (e->rpc_args.arg);
861 break;
Florin Coras52207f12018-07-12 14:48:06 -0700862 case SESSION_CTRL_EVT_DISCONNECTED:
863 session_mq_disconnected_handler (e->data);
864 break;
865 case SESSION_CTRL_EVT_ACCEPTED_REPLY:
866 session_mq_accepted_reply_handler (e->data);
867 break;
868 case SESSION_CTRL_EVT_CONNECTED_REPLY:
869 break;
870 case SESSION_CTRL_EVT_DISCONNECTED_REPLY:
871 session_mq_disconnected_reply_handler (e->data);
872 break;
873 case SESSION_CTRL_EVT_RESET_REPLY:
874 session_mq_reset_reply_handler (e->data);
875 break;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700876 default:
877 clib_warning ("unhandled event type %d", e->event_type);
878 }
879 }
880
881 _vec_len (fifo_events) = 0;
882 smm->free_event_vector[thread_index] = fifo_events;
883
884 vlib_node_increment_counter (vm, session_queue_node.index,
885 SESSION_QUEUE_ERROR_TX, n_tx_packets);
886
887 SESSION_EVT_DBG (SESSION_EVT_DISPATCH_END, smm, thread_index);
888
889 return n_tx_packets;
890}
891
892/* *INDENT-OFF* */
893VLIB_REGISTER_NODE (session_queue_node) =
894{
895 .function = session_queue_node_fn,
896 .name = "session-queue",
897 .format_trace = format_session_queue_trace,
898 .type = VLIB_NODE_TYPE_INPUT,
899 .n_errors = ARRAY_LEN (session_queue_error_strings),
900 .error_strings = session_queue_error_strings,
901 .state = VLIB_NODE_STATE_DISABLED,
902};
903/* *INDENT-ON* */
904
Dave Barachacd2a6a2017-05-16 17:41:34 -0400905void
906dump_thread_0_event_queue (void)
907{
908 session_manager_main_t *smm = vnet_get_session_manager_main ();
909 vlib_main_t *vm = &vlib_global_main;
910 u32 my_thread_index = vm->thread_index;
Florin Coras52207f12018-07-12 14:48:06 -0700911 session_event_t _e, *e = &_e;
Florin Coras3c2fed52018-07-04 04:15:05 -0700912 svm_msg_q_ring_t *ring;
Dave Barachacd2a6a2017-05-16 17:41:34 -0400913 stream_session_t *s0;
Florin Coras3c2fed52018-07-04 04:15:05 -0700914 svm_msg_q_msg_t *msg;
915 svm_msg_q_t *mq;
Dave Barachacd2a6a2017-05-16 17:41:34 -0400916 int i, index;
Dave Barachacd2a6a2017-05-16 17:41:34 -0400917
Florin Coras3c2fed52018-07-04 04:15:05 -0700918 mq = smm->vpp_event_queues[my_thread_index];
919 index = mq->q->head;
Dave Barachacd2a6a2017-05-16 17:41:34 -0400920
Florin Coras3c2fed52018-07-04 04:15:05 -0700921 for (i = 0; i < mq->q->cursize; i++)
Dave Barachacd2a6a2017-05-16 17:41:34 -0400922 {
Florin Coras3c2fed52018-07-04 04:15:05 -0700923 msg = (svm_msg_q_msg_t *) (&mq->q->data[0] + mq->q->elsize * index);
924 ring = svm_msg_q_ring (mq, msg->ring_index);
925 clib_memcpy (e, svm_msg_q_msg_data (mq, msg), ring->elsize);
Dave Barachacd2a6a2017-05-16 17:41:34 -0400926
927 switch (e->event_type)
928 {
929 case FIFO_EVENT_APP_TX:
930 s0 = session_event_get_session (e, my_thread_index);
931 fformat (stdout, "[%04d] TX session %d\n", i, s0->session_index);
932 break;
933
934 case FIFO_EVENT_DISCONNECT:
Florin Corascea194d2017-10-02 00:18:51 -0700935 s0 = session_get_from_handle (e->session_handle);
Dave Barachacd2a6a2017-05-16 17:41:34 -0400936 fformat (stdout, "[%04d] disconnect session %d\n", i,
937 s0->session_index);
938 break;
939
940 case FIFO_EVENT_BUILTIN_RX:
941 s0 = session_event_get_session (e, my_thread_index);
942 fformat (stdout, "[%04d] builtin_rx %d\n", i, s0->session_index);
943 break;
944
945 case FIFO_EVENT_RPC:
946 fformat (stdout, "[%04d] RPC call %llx with %llx\n",
947 i, (u64) (e->rpc_args.fp), (u64) (e->rpc_args.arg));
948 break;
949
950 default:
951 fformat (stdout, "[%04d] unhandled event type %d\n",
952 i, e->event_type);
953 break;
954 }
955
956 index++;
957
Florin Coras3c2fed52018-07-04 04:15:05 -0700958 if (index == mq->q->maxsize)
Dave Barachacd2a6a2017-05-16 17:41:34 -0400959 index = 0;
960 }
961}
962
Florin Coras6534b7a2017-07-18 05:38:03 -0400963static u8
Florin Coras52207f12018-07-12 14:48:06 -0700964session_node_cmp_event (session_event_t * e, svm_fifo_t * f)
Florin Coras6534b7a2017-07-18 05:38:03 -0400965{
966 stream_session_t *s;
967 switch (e->event_type)
968 {
969 case FIFO_EVENT_APP_RX:
970 case FIFO_EVENT_APP_TX:
971 case FIFO_EVENT_BUILTIN_RX:
972 if (e->fifo == f)
973 return 1;
974 break;
975 case FIFO_EVENT_DISCONNECT:
976 break;
977 case FIFO_EVENT_RPC:
Florin Corascea194d2017-10-02 00:18:51 -0700978 s = session_get_from_handle (e->session_handle);
Florin Coras6534b7a2017-07-18 05:38:03 -0400979 if (!s)
980 {
981 clib_warning ("session has event but doesn't exist!");
982 break;
983 }
984 if (s->server_rx_fifo == f || s->server_tx_fifo == f)
985 return 1;
986 break;
987 default:
988 break;
989 }
990 return 0;
991}
992
993u8
Florin Coras52207f12018-07-12 14:48:06 -0700994session_node_lookup_fifo_event (svm_fifo_t * f, session_event_t * e)
Florin Coras6534b7a2017-07-18 05:38:03 -0400995{
996 session_manager_main_t *smm = vnet_get_session_manager_main ();
Florin Coras3c2fed52018-07-04 04:15:05 -0700997 svm_msg_q_t *mq;
Florin Coras52207f12018-07-12 14:48:06 -0700998 session_event_t *pending_event_vector, *evt;
Florin Coras6534b7a2017-07-18 05:38:03 -0400999 int i, index, found = 0;
Florin Coras3c2fed52018-07-04 04:15:05 -07001000 svm_msg_q_msg_t *msg;
1001 svm_msg_q_ring_t *ring;
Florin Coras6534b7a2017-07-18 05:38:03 -04001002 u8 thread_index;
1003
1004 ASSERT (e);
1005 thread_index = f->master_thread_index;
1006 /*
1007 * Search evt queue
1008 */
Florin Coras3c2fed52018-07-04 04:15:05 -07001009 mq = smm->vpp_event_queues[thread_index];
1010 index = mq->q->head;
1011 for (i = 0; i < mq->q->cursize; i++)
Florin Coras6534b7a2017-07-18 05:38:03 -04001012 {
Florin Coras3c2fed52018-07-04 04:15:05 -07001013 msg = (svm_msg_q_msg_t *) (&mq->q->data[0] + mq->q->elsize * index);
1014 ring = svm_msg_q_ring (mq, msg->ring_index);
1015 clib_memcpy (e, svm_msg_q_msg_data (mq, msg), ring->elsize);
Florin Coras6534b7a2017-07-18 05:38:03 -04001016 found = session_node_cmp_event (e, f);
1017 if (found)
Florin Coras371ca502018-02-21 12:07:41 -08001018 return 1;
Florin Coras3c2fed52018-07-04 04:15:05 -07001019 if (++index == mq->q->maxsize)
Florin Coras6534b7a2017-07-18 05:38:03 -04001020 index = 0;
1021 }
1022 /*
1023 * Search pending events vector
1024 */
1025 pending_event_vector = smm->pending_event_vector[thread_index];
1026 vec_foreach (evt, pending_event_vector)
1027 {
1028 found = session_node_cmp_event (evt, f);
1029 if (found)
1030 {
1031 clib_memcpy (e, evt, sizeof (*evt));
1032 break;
1033 }
1034 }
1035 return found;
1036}
1037
Dave Barach2a863912017-11-28 10:11:42 -05001038static clib_error_t *
1039session_queue_exit (vlib_main_t * vm)
1040{
1041 if (vec_len (vlib_mains) < 2)
1042 return 0;
1043
1044 /*
1045 * Shut off (especially) worker-thread session nodes.
1046 * Otherwise, vpp can crash as the main thread unmaps the
1047 * API segment.
1048 */
1049 vlib_worker_thread_barrier_sync (vm);
1050 session_node_enable_disable (0 /* is_enable */ );
1051 vlib_worker_thread_barrier_release (vm);
1052 return 0;
1053}
1054
1055VLIB_MAIN_LOOP_EXIT_FUNCTION (session_queue_exit);
1056
Florin Corasfd542f12018-05-16 19:28:24 -07001057static uword
1058session_queue_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
1059 vlib_frame_t * f)
1060{
1061 f64 now, timeout = 1.0;
1062 uword *event_data = 0;
1063 uword event_type;
1064
1065 while (1)
1066 {
1067 vlib_process_wait_for_event_or_clock (vm, timeout);
1068 now = vlib_time_now (vm);
1069 event_type = vlib_process_get_events (vm, (uword **) & event_data);
1070
1071 switch (event_type)
1072 {
1073 case SESSION_Q_PROCESS_FLUSH_FRAMES:
1074 /* Flush the frames by updating all transports times */
1075 transport_update_time (now, 0);
1076 break;
1077 case SESSION_Q_PROCESS_STOP:
1078 timeout = 100000.0;
1079 break;
1080 case ~0:
1081 /* Timed out. Update time for all transports to trigger all
1082 * outstanding retransmits. */
1083 transport_update_time (now, 0);
1084 break;
1085 }
1086 vec_reset_length (event_data);
1087 }
1088 return 0;
1089}
1090
1091/* *INDENT-OFF* */
1092VLIB_REGISTER_NODE (session_queue_process_node) =
1093{
1094 .function = session_queue_process,
1095 .type = VLIB_NODE_TYPE_PROCESS,
1096 .name = "session-queue-process",
1097 .state = VLIB_NODE_STATE_DISABLED,
1098};
1099/* *INDENT-ON* */
1100
1101
Dave Barach68b0fb02017-02-28 15:15:56 -05001102/*
1103 * fd.io coding-style-patch-verification: ON
1104 *
1105 * Local Variables:
1106 * eval: (c-set-style "gnu")
1107 * End:
1108 */