blob: baabb05943d90aeee4b07d1bac90f99ad35342ac [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;
Florin Coras84099552018-07-22 12:59:30 -070071 if (!svm_fifo_is_empty (s->server_rx_fifo))
72 {
73 application_t *app;
74 app = application_get (s->app_index);
75 application_send_event (app, s, FIFO_EVENT_APP_RX);
76 }
Florin Coras52207f12018-07-12 14:48:06 -070077 }
78}
79
80static void
81session_mq_reset_reply_handler (void *data)
82{
83 session_reset_reply_msg_t *mp;
84 application_t *app;
85 stream_session_t *s;
86 u32 index, thread_index;
87
88 mp = (session_reset_reply_msg_t *) data;
89 app = application_lookup (mp->client_index);
90 if (!app)
91 return;
92
93 session_parse_handle (mp->handle, &index, &thread_index);
94 s = session_get_if_valid (index, thread_index);
95 if (s == 0 || app->index != s->app_index)
96 {
97 clib_warning ("Invalid session!");
98 return;
99 }
100
101 /* Client objected to resetting the session, log and continue */
102 if (mp->retval)
103 {
104 clib_warning ("client retval %d", mp->retval);
105 return;
106 }
107
108 /* This comes as a response to a reset, transport only waiting for
109 * confirmation to remove connection state, no need to disconnect */
110 stream_session_cleanup (s);
111}
112
113static void
114session_mq_disconnected_handler (void *data)
115{
116 session_disconnected_reply_msg_t *rmp;
117 vnet_disconnect_args_t _a, *a = &_a;
118 svm_msg_q_msg_t _msg, *msg = &_msg;
119 session_disconnected_msg_t *mp;
120 session_event_t *evt;
Florin Coras3d0fadc2018-07-17 05:35:47 -0700121 stream_session_t *s;
Florin Coras52207f12018-07-12 14:48:06 -0700122 application_t *app;
123 int rv = 0;
124
125 mp = (session_disconnected_msg_t *) data;
126 app = application_lookup (mp->client_index);
Florin Coras3d0fadc2018-07-17 05:35:47 -0700127 s = session_get_from_handle_if_valid (mp->handle);
128 if (!(app && s && s->app_index == app->index))
Florin Coras52207f12018-07-12 14:48:06 -0700129 {
Florin Coras3d0fadc2018-07-17 05:35:47 -0700130 clib_warning ("could not disconnect session: %llu app: %u", mp->handle,
131 mp->client_index);
132 return;
Florin Coras52207f12018-07-12 14:48:06 -0700133 }
Florin Coras3d0fadc2018-07-17 05:35:47 -0700134
135 a->handle = mp->handle;
136 a->app_index = app->index;
137 rv = vnet_disconnect_session (a);
Florin Coras52207f12018-07-12 14:48:06 -0700138
139 svm_msg_q_lock_and_alloc_msg_w_ring (app->event_queue,
140 SESSION_MQ_CTRL_EVT_RING,
141 SVM_Q_WAIT, msg);
142 svm_msg_q_unlock (app->event_queue);
143 evt = svm_msg_q_msg_data (app->event_queue, msg);
144 memset (evt, 0, sizeof (*evt));
145 evt->event_type = SESSION_CTRL_EVT_DISCONNECTED;
146 rmp = (session_disconnected_reply_msg_t *) evt->data;
147 rmp->handle = mp->handle;
148 rmp->context = mp->context;
149 rmp->retval = rv;
150 svm_msg_q_add (app->event_queue, msg, SVM_Q_WAIT);
151}
152
153static void
154session_mq_disconnected_reply_handler (void *data)
155{
156 session_disconnected_reply_msg_t *mp;
157 vnet_disconnect_args_t _a, *a = &_a;
158 application_t *app;
159
160 mp = (session_disconnected_reply_msg_t *) data;
161
162 /* Client objected to disconnecting the session, log and continue */
163 if (mp->retval)
164 {
165 clib_warning ("client retval %d", mp->retval);
166 return;
167 }
168
169 /* Disconnect has been confirmed. Confirm close to transport */
170 app = application_lookup (mp->context);
171 if (app)
172 {
173 a->handle = mp->handle;
174 a->app_index = app->index;
175 vnet_disconnect_session (a);
176 }
177}
178
Dave Barach68b0fb02017-02-28 15:15:56 -0500179vlib_node_registration_t session_queue_node;
180
181typedef struct
182{
183 u32 session_index;
184 u32 server_thread_index;
185} session_queue_trace_t;
186
187/* packet trace format function */
188static u8 *
189format_session_queue_trace (u8 * s, va_list * args)
190{
191 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
192 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
193 session_queue_trace_t *t = va_arg (*args, session_queue_trace_t *);
194
195 s = format (s, "SESSION_QUEUE: session index %d, server thread index %d",
196 t->session_index, t->server_thread_index);
197 return s;
198}
199
Florin Coras6792ec02017-03-13 03:49:51 -0700200#define foreach_session_queue_error \
201_(TX, "Packets transmitted") \
Florin Coras93992a92017-05-24 18:03:56 -0700202_(TIMER, "Timer events") \
203_(NO_BUFFER, "Out of buffers")
Dave Barach68b0fb02017-02-28 15:15:56 -0500204
205typedef enum
206{
207#define _(sym,str) SESSION_QUEUE_ERROR_##sym,
208 foreach_session_queue_error
209#undef _
210 SESSION_QUEUE_N_ERROR,
211} session_queue_error_t;
212
213static char *session_queue_error_strings[] = {
214#define _(sym,string) string,
215 foreach_session_queue_error
216#undef _
217};
218
Florin Coras0d60a0f2018-06-29 02:02:08 -0700219enum
220{
221 SESSION_TX_NO_BUFFERS = -2,
222 SESSION_TX_NO_DATA,
223 SESSION_TX_OK
224};
225
Florin Coras66cf5e02018-06-08 09:17:39 -0700226static void
227session_tx_trace_frame (vlib_main_t * vm, vlib_node_runtime_t * node,
228 u32 next_index, u32 * to_next, u16 n_segs,
229 stream_session_t * s, u32 n_trace)
Florin Corasf6d68ed2017-05-07 19:12:02 -0700230{
Florin Coras8e43d042018-05-04 15:46:57 -0700231 session_queue_trace_t *t;
Florin Coras66cf5e02018-06-08 09:17:39 -0700232 vlib_buffer_t *b;
233 int i;
234
235 for (i = 0; i < clib_min (n_trace, n_segs); i++)
236 {
237 b = vlib_get_buffer (vm, to_next[i - n_segs]);
238 vlib_trace_buffer (vm, node, next_index, b, 1 /* follow_chain */ );
Florin Coras66cf5e02018-06-08 09:17:39 -0700239 t = vlib_add_trace (vm, node, b, sizeof (*t));
240 t->session_index = s->session_index;
241 t->server_thread_index = s->thread_index;
242 }
Florin Coras4df38712018-06-20 12:44:16 -0700243 vlib_set_trace_count (vm, node, n_trace - i);
Florin Coras8e43d042018-05-04 15:46:57 -0700244}
Florin Corasf6d68ed2017-05-07 19:12:02 -0700245
Florin Coras8e43d042018-05-04 15:46:57 -0700246always_inline void
247session_tx_fifo_chain_tail (vlib_main_t * vm, session_tx_context_t * ctx,
248 vlib_buffer_t * b, u16 * n_bufs, u8 peek_data)
249{
250 session_manager_main_t *smm = &session_manager_main;
251 vlib_buffer_t *chain_b, *prev_b;
252 u32 chain_bi0, to_deq, left_from_seg;
253 u16 len_to_deq, n_bytes_read;
254 u8 *data, j;
Florin Corasb2215d62017-08-01 16:56:58 -0700255
Florin Coras8e43d042018-05-04 15:46:57 -0700256 b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
257 b->total_length_not_including_first_buffer = 0;
258
259 chain_b = b;
260 left_from_seg = clib_min (ctx->snd_mss - b->current_length,
261 ctx->left_to_snd);
Florin Corasb2215d62017-08-01 16:56:58 -0700262 to_deq = left_from_seg;
Florin Coras8e43d042018-05-04 15:46:57 -0700263 for (j = 1; j < ctx->n_bufs_per_seg; j++)
Florin Corasf6d68ed2017-05-07 19:12:02 -0700264 {
Florin Coras8e43d042018-05-04 15:46:57 -0700265 prev_b = chain_b;
266 len_to_deq = clib_min (to_deq, ctx->deq_per_buf);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700267
268 *n_bufs -= 1;
Florin Coras8e43d042018-05-04 15:46:57 -0700269 chain_bi0 = smm->tx_buffers[ctx->s->thread_index][*n_bufs];
270 _vec_len (smm->tx_buffers[ctx->s->thread_index]) = *n_bufs;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700271
Florin Coras8e43d042018-05-04 15:46:57 -0700272 chain_b = vlib_get_buffer (vm, chain_bi0);
273 chain_b->current_data = 0;
274 data = vlib_buffer_get_current (chain_b);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700275 if (peek_data)
276 {
Florin Coras8e43d042018-05-04 15:46:57 -0700277 n_bytes_read = svm_fifo_peek (ctx->s->server_tx_fifo,
278 ctx->tx_offset, len_to_deq, data);
279 ctx->tx_offset += n_bytes_read;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700280 }
281 else
282 {
Florin Coras8e43d042018-05-04 15:46:57 -0700283 if (ctx->transport_vft->tx_type == TRANSPORT_TX_DGRAM)
Florin Coras7fb0fe12018-04-09 09:24:52 -0700284 {
Florin Coras8e43d042018-05-04 15:46:57 -0700285 svm_fifo_t *f = ctx->s->server_tx_fifo;
286 session_dgram_hdr_t *hdr = &ctx->hdr;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700287 u16 deq_now;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700288 deq_now = clib_min (hdr->data_length - hdr->data_offset,
Florin Coras8e43d042018-05-04 15:46:57 -0700289 len_to_deq);
290 n_bytes_read = svm_fifo_peek (f, hdr->data_offset, deq_now,
291 data);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700292 ASSERT (n_bytes_read > 0);
293
294 hdr->data_offset += n_bytes_read;
295 if (hdr->data_offset == hdr->data_length)
Florin Coras8e43d042018-05-04 15:46:57 -0700296 svm_fifo_dequeue_drop (f, hdr->data_length);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700297 }
298 else
Florin Coras8e43d042018-05-04 15:46:57 -0700299 n_bytes_read = svm_fifo_dequeue_nowait (ctx->s->server_tx_fifo,
300 len_to_deq, data);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700301 }
Florin Coras8e43d042018-05-04 15:46:57 -0700302 ASSERT (n_bytes_read == len_to_deq);
303 chain_b->current_length = n_bytes_read;
304 b->total_length_not_including_first_buffer += chain_b->current_length;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700305
306 /* update previous buffer */
Florin Coras8e43d042018-05-04 15:46:57 -0700307 prev_b->next_buffer = chain_bi0;
308 prev_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700309
310 /* update current buffer */
Florin Coras8e43d042018-05-04 15:46:57 -0700311 chain_b->next_buffer = 0;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700312
Florin Corasb2215d62017-08-01 16:56:58 -0700313 to_deq -= n_bytes_read;
314 if (to_deq == 0)
Florin Corasf6d68ed2017-05-07 19:12:02 -0700315 break;
316 }
Florin Coras1f152cd2017-08-18 19:28:03 -0700317 ASSERT (to_deq == 0
Florin Coras8e43d042018-05-04 15:46:57 -0700318 && b->total_length_not_including_first_buffer == left_from_seg);
319 ctx->left_to_snd -= left_from_seg;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700320}
321
Dave Barach68b0fb02017-02-28 15:15:56 -0500322always_inline int
Florin Coras8e43d042018-05-04 15:46:57 -0700323session_output_try_get_buffers (vlib_main_t * vm,
Florin Corasd79b41e2017-03-04 05:37:52 -0800324 session_manager_main_t * smm,
Florin Coras8e43d042018-05-04 15:46:57 -0700325 u32 thread_index, u16 * n_bufs, u32 wanted)
Dave Barach68b0fb02017-02-28 15:15:56 -0500326{
Florin Corasf08f26d2018-05-10 13:20:47 -0700327 u32 n_alloc;
328 vec_validate_aligned (smm->tx_buffers[thread_index], wanted - 1,
Florin Coras8e43d042018-05-04 15:46:57 -0700329 CLIB_CACHE_LINE_BYTES);
Florin Corasf08f26d2018-05-10 13:20:47 -0700330 n_alloc = vlib_buffer_alloc (vm, &smm->tx_buffers[thread_index][*n_bufs],
331 wanted - *n_bufs);
332 *n_bufs += n_alloc;
Florin Coras8e43d042018-05-04 15:46:57 -0700333 _vec_len (smm->tx_buffers[thread_index]) = *n_bufs;
Florin Corasf08f26d2018-05-10 13:20:47 -0700334 return n_alloc;
Florin Coras8e43d042018-05-04 15:46:57 -0700335}
336
337always_inline void
338session_tx_fill_buffer (vlib_main_t * vm, session_tx_context_t * ctx,
339 vlib_buffer_t * b, u16 * n_bufs, u8 peek_data)
340{
341 u32 len_to_deq;
342 u8 *data0;
343 int n_bytes_read;
344
345 /*
346 * Start with the first buffer in chain
347 */
348 b->error = 0;
349 b->flags = VNET_BUFFER_F_LOCALLY_ORIGINATED;
350 b->current_data = 0;
Florin Coras8e43d042018-05-04 15:46:57 -0700351
352 data0 = vlib_buffer_make_headroom (b, MAX_HDRS_LEN);
353 len_to_deq = clib_min (ctx->left_to_snd, ctx->deq_per_first_buf);
354
Florin Coras7fb0fe12018-04-09 09:24:52 -0700355 if (peek_data)
356 {
Florin Coras8e43d042018-05-04 15:46:57 -0700357 n_bytes_read = svm_fifo_peek (ctx->s->server_tx_fifo, ctx->tx_offset,
358 len_to_deq, data0);
359 ASSERT (n_bytes_read > 0);
360 /* Keep track of progress locally, transport is also supposed to
361 * increment it independently when pushing the header */
362 ctx->tx_offset += n_bytes_read;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700363 }
364 else
365 {
Florin Coras8e43d042018-05-04 15:46:57 -0700366 if (ctx->transport_vft->tx_type == TRANSPORT_TX_DGRAM)
367 {
368 session_dgram_hdr_t *hdr = &ctx->hdr;
369 svm_fifo_t *f = ctx->s->server_tx_fifo;
370 u16 deq_now;
371 u32 offset;
372
373 ASSERT (hdr->data_length > hdr->data_offset);
374 deq_now = clib_min (hdr->data_length - hdr->data_offset,
375 len_to_deq);
376 offset = hdr->data_offset + SESSION_CONN_HDR_LEN;
377 n_bytes_read = svm_fifo_peek (f, offset, deq_now, data0);
378 ASSERT (n_bytes_read > 0);
379
380 if (ctx->s->session_state == SESSION_STATE_LISTENING)
381 {
382 ip_copy (&ctx->tc->rmt_ip, &hdr->rmt_ip, ctx->tc->is_ip4);
383 ctx->tc->rmt_port = hdr->rmt_port;
384 }
385 hdr->data_offset += n_bytes_read;
386 if (hdr->data_offset == hdr->data_length)
387 {
388 offset = hdr->data_length + SESSION_CONN_HDR_LEN;
389 svm_fifo_dequeue_drop (f, offset);
390 }
391 }
Florin Coras7fb0fe12018-04-09 09:24:52 -0700392 else
393 {
Florin Coras8e43d042018-05-04 15:46:57 -0700394 n_bytes_read = svm_fifo_dequeue_nowait (ctx->s->server_tx_fifo,
395 len_to_deq, data0);
396 ASSERT (n_bytes_read > 0);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700397 }
398 }
Florin Coras8e43d042018-05-04 15:46:57 -0700399 b->current_length = n_bytes_read;
400 ctx->left_to_snd -= n_bytes_read;
Dave Barach68b0fb02017-02-28 15:15:56 -0500401
Florin Coras8e43d042018-05-04 15:46:57 -0700402 /*
403 * Fill in the remaining buffers in the chain, if any
404 */
405 if (PREDICT_FALSE (ctx->n_bufs_per_seg > 1 && ctx->left_to_snd))
406 session_tx_fifo_chain_tail (vm, ctx, b, n_bufs, peek_data);
Dave Barach68b0fb02017-02-28 15:15:56 -0500407
Florin Coras8e43d042018-05-04 15:46:57 -0700408 /* *INDENT-OFF* */
Florin Coras8b20bf52018-06-14 14:55:50 -0700409 SESSION_EVT_DBG(SESSION_EVT_DEQ, ctx->s, ({
410 ed->data[0] = FIFO_EVENT_APP_TX;
411 ed->data[1] = ctx->max_dequeue;
Florin Coras8e43d042018-05-04 15:46:57 -0700412 ed->data[2] = len_to_deq;
Florin Coras8b20bf52018-06-14 14:55:50 -0700413 ed->data[3] = ctx->left_to_snd;
Florin Coras8e43d042018-05-04 15:46:57 -0700414 }));
415 /* *INDENT-ON* */
416}
417
418always_inline u8
419session_tx_not_ready (stream_session_t * s, u8 peek_data)
420{
421 if (peek_data)
Florin Corase04c2992017-03-01 08:17:34 -0800422 {
Florin Coras8e43d042018-05-04 15:46:57 -0700423 /* Can retransmit for closed sessions but can't send new data if
424 * session is not ready or closed */
425 if (s->session_state < SESSION_STATE_READY)
426 return 1;
Florin Corasca1c8f32018-05-23 21:01:30 -0700427 if (s->session_state == SESSION_STATE_CLOSED)
428 return 2;
Florin Corase04c2992017-03-01 08:17:34 -0800429 }
Florin Coras8e43d042018-05-04 15:46:57 -0700430 return 0;
431}
Dave Barach68b0fb02017-02-28 15:15:56 -0500432
Florin Coras8e43d042018-05-04 15:46:57 -0700433always_inline transport_connection_t *
434session_tx_get_transport (session_tx_context_t * ctx, u8 peek_data)
435{
436 if (peek_data)
437 {
438 return ctx->transport_vft->get_connection (ctx->s->connection_index,
439 ctx->s->thread_index);
440 }
441 else
442 {
443 if (ctx->s->session_state == SESSION_STATE_LISTENING)
444 return ctx->transport_vft->get_listener (ctx->s->connection_index);
445 else
446 {
447 return ctx->transport_vft->get_connection (ctx->s->connection_index,
448 ctx->s->thread_index);
449 }
450 }
451}
Florin Coras6a9b68b2017-11-21 04:20:42 -0800452
Florin Coras8e43d042018-05-04 15:46:57 -0700453always_inline void
454session_tx_set_dequeue_params (vlib_main_t * vm, session_tx_context_t * ctx,
Florin Corasf08f26d2018-05-10 13:20:47 -0700455 u32 max_segs, u8 peek_data)
Florin Coras8e43d042018-05-04 15:46:57 -0700456{
457 u32 n_bytes_per_buf, n_bytes_per_seg;
458 ctx->max_dequeue = svm_fifo_max_dequeue (ctx->s->server_tx_fifo);
Dave Barach68b0fb02017-02-28 15:15:56 -0500459 if (peek_data)
460 {
Florin Coras9d063042017-09-14 03:08:00 -0400461 /* Offset in rx fifo from where to peek data */
Florin Coras8e43d042018-05-04 15:46:57 -0700462 ctx->tx_offset = ctx->transport_vft->tx_fifo_offset (ctx->tc);
463 if (PREDICT_FALSE (ctx->tx_offset >= ctx->max_dequeue))
464 {
465 ctx->max_len_to_snd = 0;
466 return;
467 }
468 ctx->max_dequeue -= ctx->tx_offset;
Dave Barach68b0fb02017-02-28 15:15:56 -0500469 }
Florin Coras7fb0fe12018-04-09 09:24:52 -0700470 else
471 {
Florin Coras8e43d042018-05-04 15:46:57 -0700472 if (ctx->transport_vft->tx_type == TRANSPORT_TX_DGRAM)
Florin Coras7fb0fe12018-04-09 09:24:52 -0700473 {
Florin Coras8e43d042018-05-04 15:46:57 -0700474 if (ctx->max_dequeue <= sizeof (ctx->hdr))
475 {
476 ctx->max_len_to_snd = 0;
477 return;
478 }
479 svm_fifo_peek (ctx->s->server_tx_fifo, 0, sizeof (ctx->hdr),
480 (u8 *) & ctx->hdr);
481 ASSERT (ctx->hdr.data_length > ctx->hdr.data_offset);
482 ctx->max_dequeue = ctx->hdr.data_length - ctx->hdr.data_offset;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700483 }
484 }
Florin Coras8e43d042018-05-04 15:46:57 -0700485 ASSERT (ctx->max_dequeue > 0);
Florin Coras6792ec02017-03-13 03:49:51 -0700486
487 /* Ensure we're not writing more than transport window allows */
Florin Coras8e43d042018-05-04 15:46:57 -0700488 if (ctx->max_dequeue < ctx->snd_space)
Florin Coras3e350af2017-03-30 02:54:28 -0700489 {
490 /* Constrained by tx queue. Try to send only fully formed segments */
Florin Coras8e43d042018-05-04 15:46:57 -0700491 ctx->max_len_to_snd =
492 (ctx->max_dequeue > ctx->snd_mss) ?
493 ctx->max_dequeue - ctx->max_dequeue % ctx->snd_mss : ctx->max_dequeue;
Florin Coras3e350af2017-03-30 02:54:28 -0700494 /* TODO Nagle ? */
495 }
496 else
497 {
Florin Coras1f152cd2017-08-18 19:28:03 -0700498 /* Expectation is that snd_space0 is already a multiple of snd_mss */
Florin Coras8e43d042018-05-04 15:46:57 -0700499 ctx->max_len_to_snd = ctx->snd_space;
Florin Coras3e350af2017-03-30 02:54:28 -0700500 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500501
Florin Corasf08f26d2018-05-10 13:20:47 -0700502 /* Check if we're tx constrained by the node */
503 ctx->n_segs_per_evt = ceil ((f64) ctx->max_len_to_snd / ctx->snd_mss);
504 if (ctx->n_segs_per_evt > max_segs)
505 {
506 ctx->n_segs_per_evt = max_segs;
507 ctx->max_len_to_snd = max_segs * ctx->snd_mss;
508 }
509
Florin Coras8e43d042018-05-04 15:46:57 -0700510 n_bytes_per_buf = vlib_buffer_free_list_buffer_size (vm,
511 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
Florin Corase87216f2017-08-17 16:59:22 -0700512 ASSERT (n_bytes_per_buf > MAX_HDRS_LEN);
Florin Coras8e43d042018-05-04 15:46:57 -0700513 n_bytes_per_seg = MAX_HDRS_LEN + ctx->snd_mss;
Florin Corasf08f26d2018-05-10 13:20:47 -0700514 ctx->n_bufs_per_seg = ceil ((f64) n_bytes_per_seg / n_bytes_per_buf);
Florin Coras8e43d042018-05-04 15:46:57 -0700515 ctx->deq_per_buf = clib_min (ctx->snd_mss, n_bytes_per_buf);
516 ctx->deq_per_first_buf = clib_min (ctx->snd_mss,
517 n_bytes_per_buf - MAX_HDRS_LEN);
518}
Florin Corasf6d68ed2017-05-07 19:12:02 -0700519
Florin Coras8e43d042018-05-04 15:46:57 -0700520always_inline int
521session_tx_fifo_read_and_snd_i (vlib_main_t * vm, vlib_node_runtime_t * node,
Florin Coras52207f12018-07-12 14:48:06 -0700522 session_event_t * e,
Florin Coras8e43d042018-05-04 15:46:57 -0700523 stream_session_t * s, int *n_tx_packets,
524 u8 peek_data)
525{
Florin Coras66cf5e02018-06-08 09:17:39 -0700526 u32 next_index, next0, next1, *to_next, n_left_to_next;
Florin Corasf08f26d2018-05-10 13:20:47 -0700527 u32 n_trace = vlib_get_trace_count (vm, node), n_bufs_needed = 0;
528 u32 thread_index = s->thread_index, n_left, pbi;
Florin Coras8e43d042018-05-04 15:46:57 -0700529 session_manager_main_t *smm = &session_manager_main;
530 session_tx_context_t *ctx = &smm->ctx[thread_index];
531 transport_proto_t tp;
532 vlib_buffer_t *pb;
Florin Corasca1c8f32018-05-23 21:01:30 -0700533 u16 n_bufs, rv;
Florin Coras8e43d042018-05-04 15:46:57 -0700534
Florin Corasca1c8f32018-05-23 21:01:30 -0700535 if (PREDICT_FALSE ((rv = session_tx_not_ready (s, peek_data))))
Florin Coras8e43d042018-05-04 15:46:57 -0700536 {
Florin Corasca1c8f32018-05-23 21:01:30 -0700537 if (rv < 2)
538 vec_add1 (smm->pending_event_vector[thread_index], *e);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700539 return SESSION_TX_NO_DATA;
Florin Coras8e43d042018-05-04 15:46:57 -0700540 }
541
542 next_index = smm->session_type_to_next[s->session_type];
Florin Coras66cf5e02018-06-08 09:17:39 -0700543 next0 = next1 = next_index;
Florin Coras8e43d042018-05-04 15:46:57 -0700544
545 tp = session_get_transport_proto (s);
546 ctx->s = s;
547 ctx->transport_vft = transport_protocol_get_vft (tp);
548 ctx->tc = session_tx_get_transport (ctx, peek_data);
549 ctx->snd_mss = ctx->transport_vft->send_mss (ctx->tc);
550 ctx->snd_space = ctx->transport_vft->send_space (ctx->tc);
551 if (ctx->snd_space == 0 || ctx->snd_mss == 0)
552 {
553 vec_add1 (smm->pending_event_vector[thread_index], *e);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700554 return SESSION_TX_NO_DATA;
Florin Coras8e43d042018-05-04 15:46:57 -0700555 }
556
557 /* Allow enqueuing of a new event */
558 svm_fifo_unset_event (s->server_tx_fifo);
559
560 /* Check how much we can pull. */
Florin Corasf08f26d2018-05-10 13:20:47 -0700561 session_tx_set_dequeue_params (vm, ctx, VLIB_FRAME_SIZE - *n_tx_packets,
562 peek_data);
563
Florin Coras8e43d042018-05-04 15:46:57 -0700564 if (PREDICT_FALSE (!ctx->max_len_to_snd))
Florin Coras0d60a0f2018-06-29 02:02:08 -0700565 return SESSION_TX_NO_DATA;
Dave Barach68b0fb02017-02-28 15:15:56 -0500566
567 n_bufs = vec_len (smm->tx_buffers[thread_index]);
Florin Corasf08f26d2018-05-10 13:20:47 -0700568 n_bufs_needed = ctx->n_segs_per_evt * ctx->n_bufs_per_seg;
Florin Coras8e43d042018-05-04 15:46:57 -0700569
570 /*
571 * Make sure we have at least one full frame of buffers ready
572 */
Florin Corasf08f26d2018-05-10 13:20:47 -0700573 if (n_bufs < n_bufs_needed)
Dave Barach68b0fb02017-02-28 15:15:56 -0500574 {
Florin Coras8e43d042018-05-04 15:46:57 -0700575 session_output_try_get_buffers (vm, smm, thread_index, &n_bufs,
Florin Corasf08f26d2018-05-10 13:20:47 -0700576 ctx->n_bufs_per_seg * VLIB_FRAME_SIZE);
577 if (PREDICT_FALSE (n_bufs < n_bufs_needed))
Dave Barach68b0fb02017-02-28 15:15:56 -0500578 {
Florin Coras8e43d042018-05-04 15:46:57 -0700579 vec_add1 (smm->pending_event_vector[thread_index], *e);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700580 return SESSION_TX_NO_BUFFERS;
Dave Barach68b0fb02017-02-28 15:15:56 -0500581 }
Florin Coras8e43d042018-05-04 15:46:57 -0700582 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500583
Florin Coras8e43d042018-05-04 15:46:57 -0700584 /*
585 * Write until we fill up a frame
586 */
587 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Florin Corasf08f26d2018-05-10 13:20:47 -0700588 if (PREDICT_FALSE (ctx->n_segs_per_evt > n_left_to_next))
Florin Coras8e43d042018-05-04 15:46:57 -0700589 {
Florin Corasf08f26d2018-05-10 13:20:47 -0700590 ctx->n_segs_per_evt = n_left_to_next;
591 ctx->max_len_to_snd = ctx->snd_mss * n_left_to_next;
592 }
593 ctx->left_to_snd = ctx->max_len_to_snd;
594 n_left = ctx->n_segs_per_evt;
Florin Coras66cf5e02018-06-08 09:17:39 -0700595
596 while (n_left >= 4)
597 {
598 vlib_buffer_t *b0, *b1;
599 u32 bi0, bi1;
600
601 pbi = smm->tx_buffers[thread_index][n_bufs - 3];
602 pb = vlib_get_buffer (vm, pbi);
603 vlib_prefetch_buffer_header (pb, STORE);
604 pbi = smm->tx_buffers[thread_index][n_bufs - 4];
605 pb = vlib_get_buffer (vm, pbi);
606 vlib_prefetch_buffer_header (pb, STORE);
607
608 to_next[0] = bi0 = smm->tx_buffers[thread_index][--n_bufs];
609 to_next[1] = bi1 = smm->tx_buffers[thread_index][--n_bufs];
610
611 b0 = vlib_get_buffer (vm, bi0);
612 b1 = vlib_get_buffer (vm, bi1);
613
614 session_tx_fill_buffer (vm, ctx, b0, &n_bufs, peek_data);
615 session_tx_fill_buffer (vm, ctx, b1, &n_bufs, peek_data);
616
617 ctx->transport_vft->push_header (ctx->tc, b0);
618 ctx->transport_vft->push_header (ctx->tc, b1);
619
620 to_next += 2;
621 n_left_to_next -= 2;
622 n_left -= 2;
623
624 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
625 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b1);
626
627 vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
628 n_left_to_next, bi0, bi1, next0,
629 next1);
630 }
Florin Corasf08f26d2018-05-10 13:20:47 -0700631 while (n_left)
632 {
Florin Coras66cf5e02018-06-08 09:17:39 -0700633 vlib_buffer_t *b0;
634 u32 bi0;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700635
Florin Coras91ca4622018-06-30 11:27:59 -0700636 if (n_left > 1)
637 {
638 pbi = smm->tx_buffers[thread_index][n_bufs - 2];
639 pb = vlib_get_buffer (vm, pbi);
640 vlib_prefetch_buffer_header (pb, STORE);
641 }
642
Florin Coras66cf5e02018-06-08 09:17:39 -0700643 to_next[0] = bi0 = smm->tx_buffers[thread_index][--n_bufs];
644 b0 = vlib_get_buffer (vm, bi0);
645 session_tx_fill_buffer (vm, ctx, b0, &n_bufs, peek_data);
Florin Coras81a13db2018-03-16 08:48:31 -0700646
Florin Coras66cf5e02018-06-08 09:17:39 -0700647 /* Ask transport to push header after current_length and
648 * total_length_not_including_first_buffer are updated */
649 ctx->transport_vft->push_header (ctx->tc, b0);
Florin Corasf6359c82017-06-19 12:26:09 -0400650
Florin Coras66cf5e02018-06-08 09:17:39 -0700651 to_next += 1;
652 n_left_to_next -= 1;
653 n_left -= 1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500654
Florin Coras66cf5e02018-06-08 09:17:39 -0700655 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700656
Florin Coras66cf5e02018-06-08 09:17:39 -0700657 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
658 n_left_to_next, bi0, next0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500659 }
Florin Coras66cf5e02018-06-08 09:17:39 -0700660
661 if (PREDICT_FALSE (n_trace > 0))
662 session_tx_trace_frame (vm, node, next_index, to_next,
663 ctx->n_segs_per_evt, s, n_trace);
664
Florin Coras8e43d042018-05-04 15:46:57 -0700665 _vec_len (smm->tx_buffers[thread_index]) = n_bufs;
Florin Corasf08f26d2018-05-10 13:20:47 -0700666 *n_tx_packets += ctx->n_segs_per_evt;
Florin Coras8e43d042018-05-04 15:46:57 -0700667 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
Dave Barach68b0fb02017-02-28 15:15:56 -0500668
Florin Coras6792ec02017-03-13 03:49:51 -0700669 /* If we couldn't dequeue all bytes mark as partially read */
Florin Corasf08f26d2018-05-10 13:20:47 -0700670 ASSERT (ctx->left_to_snd == 0);
Florin Coras8e43d042018-05-04 15:46:57 -0700671 if (ctx->max_len_to_snd < ctx->max_dequeue)
672 if (svm_fifo_set_event (s->server_tx_fifo))
673 vec_add1 (smm->pending_event_vector[thread_index], *e);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700674
Florin Coras8e43d042018-05-04 15:46:57 -0700675 if (!peek_data && ctx->transport_vft->tx_type == TRANSPORT_TX_DGRAM)
Dave Barach68b0fb02017-02-28 15:15:56 -0500676 {
Florin Coras7fb0fe12018-04-09 09:24:52 -0700677 /* Fix dgram pre header */
Florin Coras8e43d042018-05-04 15:46:57 -0700678 if (ctx->max_len_to_snd < ctx->max_dequeue)
679 svm_fifo_overwrite_head (s->server_tx_fifo, (u8 *) & ctx->hdr,
Florin Coras7fb0fe12018-04-09 09:24:52 -0700680 sizeof (session_dgram_pre_hdr_t));
681 /* More data needs to be read */
Florin Coras8e43d042018-05-04 15:46:57 -0700682 else if (svm_fifo_max_dequeue (s->server_tx_fifo) > 0)
683 if (svm_fifo_set_event (s->server_tx_fifo))
684 vec_add1 (smm->pending_event_vector[thread_index], *e);
Dave Barach68b0fb02017-02-28 15:15:56 -0500685 }
Florin Coras0d60a0f2018-06-29 02:02:08 -0700686 return SESSION_TX_OK;
Dave Barach68b0fb02017-02-28 15:15:56 -0500687}
688
689int
Florin Corasd79b41e2017-03-04 05:37:52 -0800690session_tx_fifo_peek_and_snd (vlib_main_t * vm, vlib_node_runtime_t * node,
Florin Coras52207f12018-07-12 14:48:06 -0700691 session_event_t * e,
Florin Coras0d60a0f2018-06-29 02:02:08 -0700692 stream_session_t * s, int *n_tx_pkts)
Dave Barach68b0fb02017-02-28 15:15:56 -0500693{
Florin Coras0d60a0f2018-06-29 02:02:08 -0700694 return session_tx_fifo_read_and_snd_i (vm, node, e, s, n_tx_pkts, 1);
Dave Barach68b0fb02017-02-28 15:15:56 -0500695}
696
697int
Florin Corasd79b41e2017-03-04 05:37:52 -0800698session_tx_fifo_dequeue_and_snd (vlib_main_t * vm, vlib_node_runtime_t * node,
Florin Coras52207f12018-07-12 14:48:06 -0700699 session_event_t * e,
Florin Coras0d60a0f2018-06-29 02:02:08 -0700700 stream_session_t * s, int *n_tx_pkts)
Dave Barach68b0fb02017-02-28 15:15:56 -0500701{
Florin Coras0d60a0f2018-06-29 02:02:08 -0700702 return session_tx_fifo_read_and_snd_i (vm, node, e, s, n_tx_pkts, 0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500703}
704
Florin Coras371ca502018-02-21 12:07:41 -0800705int
706session_tx_fifo_dequeue_internal (vlib_main_t * vm,
707 vlib_node_runtime_t * node,
Florin Coras52207f12018-07-12 14:48:06 -0700708 session_event_t * e,
Florin Coras0d60a0f2018-06-29 02:02:08 -0700709 stream_session_t * s, int *n_tx_pkts)
Florin Coras371ca502018-02-21 12:07:41 -0800710{
711 application_t *app;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700712 app = application_get (s->opaque);
713 svm_fifo_unset_event (s->server_tx_fifo);
714 return app->cb_fns.builtin_app_tx_callback (s);
Florin Coras371ca502018-02-21 12:07:41 -0800715}
716
Dave Barach2c25a622017-06-26 11:35:07 -0400717always_inline stream_session_t *
Florin Coras52207f12018-07-12 14:48:06 -0700718session_event_get_session (session_event_t * e, u8 thread_index)
Florin Corasa5464812017-04-19 13:00:05 -0700719{
Florin Coras3cbc04b2017-10-02 00:18:51 -0700720 return session_get_if_valid (e->fifo->master_session_index, thread_index);
Florin Corasa5464812017-04-19 13:00:05 -0700721}
722
Florin Coras0d60a0f2018-06-29 02:02:08 -0700723static uword
724session_queue_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
725 vlib_frame_t * frame)
726{
727 session_manager_main_t *smm = vnet_get_session_manager_main ();
Florin Coras3c2fed52018-07-04 04:15:05 -0700728 u32 thread_index = vm->thread_index, n_to_dequeue, n_events;
Florin Coras52207f12018-07-12 14:48:06 -0700729 session_event_t *pending_events, *e;
730 session_event_t *fifo_events;
Florin Coras3c2fed52018-07-04 04:15:05 -0700731 svm_msg_q_msg_t _msg, *msg = &_msg;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700732 f64 now = vlib_time_now (vm);
Florin Coras3c2fed52018-07-04 04:15:05 -0700733 int n_tx_packets = 0, i, rv;
734 application_t *app;
735 svm_msg_q_t *mq;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700736 void (*fp) (void *);
737
738 SESSION_EVT_DBG (SESSION_EVT_POLL_GAP_TRACK, smm, thread_index);
739
740 /*
741 * Update transport time
742 */
743 transport_update_time (now, thread_index);
744
745 /*
Florin Coras3c2fed52018-07-04 04:15:05 -0700746 * Get vpp queue events that we can dequeue without blocking
Florin Coras0d60a0f2018-06-29 02:02:08 -0700747 */
Florin Coras3c2fed52018-07-04 04:15:05 -0700748 mq = smm->vpp_event_queues[thread_index];
Florin Coras0d60a0f2018-06-29 02:02:08 -0700749 fifo_events = smm->free_event_vector[thread_index];
Florin Coras3c2fed52018-07-04 04:15:05 -0700750 n_to_dequeue = svm_msg_q_size (mq);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700751 pending_events = smm->pending_event_vector[thread_index];
752
753 if (!n_to_dequeue && !vec_len (pending_events)
754 && !vec_len (smm->pending_disconnects[thread_index]))
755 return 0;
756
757 SESSION_EVT_DBG (SESSION_EVT_DEQ_NODE, 0);
758
759 /*
760 * If we didn't manage to process previous events try going
761 * over them again without dequeuing new ones.
762 * XXX: Handle senders to sessions that can't keep up
763 */
764 if (0 && vec_len (pending_events) >= 100)
765 {
766 clib_warning ("too many fifo events unsolved");
767 goto skip_dequeue;
768 }
769
770 /* See you in the next life, don't be late
Florin Coras3c2fed52018-07-04 04:15:05 -0700771 * XXX: we may need priorities here */
772 if (svm_msg_q_try_lock (mq))
Florin Coras0d60a0f2018-06-29 02:02:08 -0700773 return 0;
774
775 for (i = 0; i < n_to_dequeue; i++)
776 {
777 vec_add2 (fifo_events, e, 1);
Florin Coras3c2fed52018-07-04 04:15:05 -0700778 svm_msg_q_sub_w_lock (mq, msg);
779 clib_memcpy (e, svm_msg_q_msg_data (mq, msg), sizeof (*e));
780 svm_msg_q_free_msg (mq, msg);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700781 }
782
Florin Coras3c2fed52018-07-04 04:15:05 -0700783 svm_msg_q_unlock (mq);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700784
785 vec_append (fifo_events, pending_events);
786 vec_append (fifo_events, smm->pending_disconnects[thread_index]);
787
788 _vec_len (pending_events) = 0;
789 smm->pending_event_vector[thread_index] = pending_events;
790 _vec_len (smm->pending_disconnects[thread_index]) = 0;
791
792skip_dequeue:
793 n_events = vec_len (fifo_events);
794 for (i = 0; i < n_events; i++)
795 {
796 stream_session_t *s; /* $$$ prefetch 1 ahead maybe */
Florin Coras52207f12018-07-12 14:48:06 -0700797 session_event_t *e;
Florin Coras46f001d2018-07-11 05:25:06 -0700798 u32 to_dequeue;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700799
800 e = &fifo_events[i];
801 switch (e->event_type)
802 {
803 case FIFO_EVENT_APP_TX:
804 /* Don't try to send more that one frame per dispatch cycle */
805 if (n_tx_packets == VLIB_FRAME_SIZE)
806 {
807 vec_add1 (smm->pending_event_vector[thread_index], *e);
808 break;
809 }
810
811 s = session_event_get_session (e, thread_index);
812 if (PREDICT_FALSE (!s))
813 {
814 clib_warning ("It's dead, Jim!");
815 continue;
816 }
Florin Coras46f001d2018-07-11 05:25:06 -0700817 to_dequeue = svm_fifo_max_dequeue (s->server_tx_fifo);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700818
819 /* Spray packets in per session type frames, since they go to
820 * different nodes */
821 rv = (smm->session_tx_fns[s->session_type]) (vm, node, e, s,
822 &n_tx_packets);
823 if (PREDICT_TRUE (rv == SESSION_TX_OK))
824 {
Florin Coras46f001d2018-07-11 05:25:06 -0700825 /* Notify app there's tx space if not polling */
826 if (PREDICT_FALSE (to_dequeue == s->server_tx_fifo->nitems
827 && !svm_fifo_has_event (s->server_tx_fifo)))
828 session_dequeue_notify (s);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700829 }
830 else if (PREDICT_FALSE (rv == SESSION_TX_NO_BUFFERS))
831 {
832 vlib_node_increment_counter (vm, node->node_index,
833 SESSION_QUEUE_ERROR_NO_BUFFER, 1);
834 continue;
835 }
836 break;
837 case FIFO_EVENT_DISCONNECT:
838 /* Make sure stream disconnects run after the pending list is
839 * drained */
840 s = session_get_from_handle (e->session_handle);
841 if (!e->postponed)
842 {
843 e->postponed = 1;
844 vec_add1 (smm->pending_disconnects[thread_index], *e);
845 continue;
846 }
847 /* If tx queue is still not empty, wait */
848 if (svm_fifo_max_dequeue (s->server_tx_fifo))
849 {
850 vec_add1 (smm->pending_disconnects[thread_index], *e);
851 continue;
852 }
853
854 stream_session_disconnect_transport (s);
855 break;
856 case FIFO_EVENT_BUILTIN_RX:
857 s = session_event_get_session (e, thread_index);
858 if (PREDICT_FALSE (!s))
859 continue;
860 svm_fifo_unset_event (s->server_rx_fifo);
861 app = application_get (s->app_index);
862 app->cb_fns.builtin_app_rx_callback (s);
863 break;
864 case FIFO_EVENT_RPC:
865 fp = e->rpc_args.fp;
866 (*fp) (e->rpc_args.arg);
867 break;
Florin Coras52207f12018-07-12 14:48:06 -0700868 case SESSION_CTRL_EVT_DISCONNECTED:
869 session_mq_disconnected_handler (e->data);
870 break;
871 case SESSION_CTRL_EVT_ACCEPTED_REPLY:
872 session_mq_accepted_reply_handler (e->data);
873 break;
874 case SESSION_CTRL_EVT_CONNECTED_REPLY:
875 break;
876 case SESSION_CTRL_EVT_DISCONNECTED_REPLY:
877 session_mq_disconnected_reply_handler (e->data);
878 break;
879 case SESSION_CTRL_EVT_RESET_REPLY:
880 session_mq_reset_reply_handler (e->data);
881 break;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700882 default:
883 clib_warning ("unhandled event type %d", e->event_type);
884 }
885 }
886
887 _vec_len (fifo_events) = 0;
888 smm->free_event_vector[thread_index] = fifo_events;
889
890 vlib_node_increment_counter (vm, session_queue_node.index,
891 SESSION_QUEUE_ERROR_TX, n_tx_packets);
892
893 SESSION_EVT_DBG (SESSION_EVT_DISPATCH_END, smm, thread_index);
894
895 return n_tx_packets;
896}
897
898/* *INDENT-OFF* */
899VLIB_REGISTER_NODE (session_queue_node) =
900{
901 .function = session_queue_node_fn,
902 .name = "session-queue",
903 .format_trace = format_session_queue_trace,
904 .type = VLIB_NODE_TYPE_INPUT,
905 .n_errors = ARRAY_LEN (session_queue_error_strings),
906 .error_strings = session_queue_error_strings,
907 .state = VLIB_NODE_STATE_DISABLED,
908};
909/* *INDENT-ON* */
910
Dave Barachacd2a6a2017-05-16 17:41:34 -0400911void
912dump_thread_0_event_queue (void)
913{
914 session_manager_main_t *smm = vnet_get_session_manager_main ();
915 vlib_main_t *vm = &vlib_global_main;
916 u32 my_thread_index = vm->thread_index;
Florin Coras52207f12018-07-12 14:48:06 -0700917 session_event_t _e, *e = &_e;
Florin Coras3c2fed52018-07-04 04:15:05 -0700918 svm_msg_q_ring_t *ring;
Dave Barachacd2a6a2017-05-16 17:41:34 -0400919 stream_session_t *s0;
Florin Coras3c2fed52018-07-04 04:15:05 -0700920 svm_msg_q_msg_t *msg;
921 svm_msg_q_t *mq;
Dave Barachacd2a6a2017-05-16 17:41:34 -0400922 int i, index;
Dave Barachacd2a6a2017-05-16 17:41:34 -0400923
Florin Coras3c2fed52018-07-04 04:15:05 -0700924 mq = smm->vpp_event_queues[my_thread_index];
925 index = mq->q->head;
Dave Barachacd2a6a2017-05-16 17:41:34 -0400926
Florin Coras3c2fed52018-07-04 04:15:05 -0700927 for (i = 0; i < mq->q->cursize; i++)
Dave Barachacd2a6a2017-05-16 17:41:34 -0400928 {
Florin Coras3c2fed52018-07-04 04:15:05 -0700929 msg = (svm_msg_q_msg_t *) (&mq->q->data[0] + mq->q->elsize * index);
930 ring = svm_msg_q_ring (mq, msg->ring_index);
931 clib_memcpy (e, svm_msg_q_msg_data (mq, msg), ring->elsize);
Dave Barachacd2a6a2017-05-16 17:41:34 -0400932
933 switch (e->event_type)
934 {
935 case FIFO_EVENT_APP_TX:
936 s0 = session_event_get_session (e, my_thread_index);
937 fformat (stdout, "[%04d] TX session %d\n", i, s0->session_index);
938 break;
939
940 case FIFO_EVENT_DISCONNECT:
Florin Corascea194d2017-10-02 00:18:51 -0700941 s0 = session_get_from_handle (e->session_handle);
Dave Barachacd2a6a2017-05-16 17:41:34 -0400942 fformat (stdout, "[%04d] disconnect session %d\n", i,
943 s0->session_index);
944 break;
945
946 case FIFO_EVENT_BUILTIN_RX:
947 s0 = session_event_get_session (e, my_thread_index);
948 fformat (stdout, "[%04d] builtin_rx %d\n", i, s0->session_index);
949 break;
950
951 case FIFO_EVENT_RPC:
952 fformat (stdout, "[%04d] RPC call %llx with %llx\n",
953 i, (u64) (e->rpc_args.fp), (u64) (e->rpc_args.arg));
954 break;
955
956 default:
957 fformat (stdout, "[%04d] unhandled event type %d\n",
958 i, e->event_type);
959 break;
960 }
961
962 index++;
963
Florin Coras3c2fed52018-07-04 04:15:05 -0700964 if (index == mq->q->maxsize)
Dave Barachacd2a6a2017-05-16 17:41:34 -0400965 index = 0;
966 }
967}
968
Florin Coras6534b7a2017-07-18 05:38:03 -0400969static u8
Florin Coras52207f12018-07-12 14:48:06 -0700970session_node_cmp_event (session_event_t * e, svm_fifo_t * f)
Florin Coras6534b7a2017-07-18 05:38:03 -0400971{
972 stream_session_t *s;
973 switch (e->event_type)
974 {
975 case FIFO_EVENT_APP_RX:
976 case FIFO_EVENT_APP_TX:
977 case FIFO_EVENT_BUILTIN_RX:
978 if (e->fifo == f)
979 return 1;
980 break;
981 case FIFO_EVENT_DISCONNECT:
982 break;
983 case FIFO_EVENT_RPC:
Florin Corascea194d2017-10-02 00:18:51 -0700984 s = session_get_from_handle (e->session_handle);
Florin Coras6534b7a2017-07-18 05:38:03 -0400985 if (!s)
986 {
987 clib_warning ("session has event but doesn't exist!");
988 break;
989 }
990 if (s->server_rx_fifo == f || s->server_tx_fifo == f)
991 return 1;
992 break;
993 default:
994 break;
995 }
996 return 0;
997}
998
999u8
Florin Coras52207f12018-07-12 14:48:06 -07001000session_node_lookup_fifo_event (svm_fifo_t * f, session_event_t * e)
Florin Coras6534b7a2017-07-18 05:38:03 -04001001{
1002 session_manager_main_t *smm = vnet_get_session_manager_main ();
Florin Coras3c2fed52018-07-04 04:15:05 -07001003 svm_msg_q_t *mq;
Florin Coras52207f12018-07-12 14:48:06 -07001004 session_event_t *pending_event_vector, *evt;
Florin Coras6534b7a2017-07-18 05:38:03 -04001005 int i, index, found = 0;
Florin Coras3c2fed52018-07-04 04:15:05 -07001006 svm_msg_q_msg_t *msg;
1007 svm_msg_q_ring_t *ring;
Florin Coras6534b7a2017-07-18 05:38:03 -04001008 u8 thread_index;
1009
1010 ASSERT (e);
1011 thread_index = f->master_thread_index;
1012 /*
1013 * Search evt queue
1014 */
Florin Coras3c2fed52018-07-04 04:15:05 -07001015 mq = smm->vpp_event_queues[thread_index];
1016 index = mq->q->head;
1017 for (i = 0; i < mq->q->cursize; i++)
Florin Coras6534b7a2017-07-18 05:38:03 -04001018 {
Florin Coras3c2fed52018-07-04 04:15:05 -07001019 msg = (svm_msg_q_msg_t *) (&mq->q->data[0] + mq->q->elsize * index);
1020 ring = svm_msg_q_ring (mq, msg->ring_index);
1021 clib_memcpy (e, svm_msg_q_msg_data (mq, msg), ring->elsize);
Florin Coras6534b7a2017-07-18 05:38:03 -04001022 found = session_node_cmp_event (e, f);
1023 if (found)
Florin Coras371ca502018-02-21 12:07:41 -08001024 return 1;
Florin Coras3c2fed52018-07-04 04:15:05 -07001025 if (++index == mq->q->maxsize)
Florin Coras6534b7a2017-07-18 05:38:03 -04001026 index = 0;
1027 }
1028 /*
1029 * Search pending events vector
1030 */
1031 pending_event_vector = smm->pending_event_vector[thread_index];
1032 vec_foreach (evt, pending_event_vector)
1033 {
1034 found = session_node_cmp_event (evt, f);
1035 if (found)
1036 {
1037 clib_memcpy (e, evt, sizeof (*evt));
1038 break;
1039 }
1040 }
1041 return found;
1042}
1043
Dave Barach2a863912017-11-28 10:11:42 -05001044static clib_error_t *
1045session_queue_exit (vlib_main_t * vm)
1046{
1047 if (vec_len (vlib_mains) < 2)
1048 return 0;
1049
1050 /*
1051 * Shut off (especially) worker-thread session nodes.
1052 * Otherwise, vpp can crash as the main thread unmaps the
1053 * API segment.
1054 */
1055 vlib_worker_thread_barrier_sync (vm);
1056 session_node_enable_disable (0 /* is_enable */ );
1057 vlib_worker_thread_barrier_release (vm);
1058 return 0;
1059}
1060
1061VLIB_MAIN_LOOP_EXIT_FUNCTION (session_queue_exit);
1062
Florin Corasfd542f12018-05-16 19:28:24 -07001063static uword
1064session_queue_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
1065 vlib_frame_t * f)
1066{
1067 f64 now, timeout = 1.0;
1068 uword *event_data = 0;
1069 uword event_type;
1070
1071 while (1)
1072 {
1073 vlib_process_wait_for_event_or_clock (vm, timeout);
1074 now = vlib_time_now (vm);
1075 event_type = vlib_process_get_events (vm, (uword **) & event_data);
1076
1077 switch (event_type)
1078 {
1079 case SESSION_Q_PROCESS_FLUSH_FRAMES:
1080 /* Flush the frames by updating all transports times */
1081 transport_update_time (now, 0);
1082 break;
1083 case SESSION_Q_PROCESS_STOP:
1084 timeout = 100000.0;
1085 break;
1086 case ~0:
1087 /* Timed out. Update time for all transports to trigger all
1088 * outstanding retransmits. */
1089 transport_update_time (now, 0);
1090 break;
1091 }
1092 vec_reset_length (event_data);
1093 }
1094 return 0;
1095}
1096
1097/* *INDENT-OFF* */
1098VLIB_REGISTER_NODE (session_queue_process_node) =
1099{
1100 .function = session_queue_process,
1101 .type = VLIB_NODE_TYPE_PROCESS,
1102 .name = "session-queue-process",
1103 .state = VLIB_NODE_STATE_DISABLED,
1104};
1105/* *INDENT-ON* */
1106
1107
Dave Barach68b0fb02017-02-28 15:15:56 -05001108/*
1109 * fd.io coding-style-patch-verification: ON
1110 *
1111 * Local Variables:
1112 * eval: (c-set-style "gnu")
1113 * End:
1114 */