blob: 7855bf8c56d8695aaf7225afac7b4d51118f8160 [file] [log] [blame]
Dave Barach68b0fb02017-02-28 15:15:56 -05001/*
Florin Coras288eaab2019-02-03 15:26:14 -08002 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
Dave Barach68b0fb02017-02-28 15:15:56 -05003 * 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 Corasba7d8f52019-02-22 13:11:38 -080024#include <vnet/session/application_local.h>
Florin Corase69f4952017-03-07 10:06:24 -080025#include <vnet/session/session_debug.h>
Florin Corase86a8ed2018-01-05 03:20:25 -080026#include <svm/queue.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050027
Florin Coras52207f12018-07-12 14:48:06 -070028static void
29session_mq_accepted_reply_handler (void *data)
30{
31 session_accepted_reply_msg_t *mp = (session_accepted_reply_msg_t *) data;
32 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
Florin Coras288eaab2019-02-03 15:26:14 -080033 session_state_t old_state;
Florin Coras21795132018-09-09 09:40:51 -070034 app_worker_t *app_wrk;
Florin Coras52207f12018-07-12 14:48:06 -070035 local_session_t *ls;
Florin Coras288eaab2019-02-03 15:26:14 -080036 session_t *s;
Florin Coras52207f12018-07-12 14:48:06 -070037
38 /* Server isn't interested, kill the session */
39 if (mp->retval)
40 {
41 a->app_index = mp->context;
42 a->handle = mp->handle;
43 vnet_disconnect_session (a);
44 return;
45 }
46
47 if (session_handle_is_local (mp->handle))
48 {
Florin Coras623eb562019-02-03 19:28:34 -080049 ls = app_worker_get_local_session_from_handle (mp->handle);
Florin Corasab2f6db2018-08-31 14:31:41 -070050 if (!ls)
Florin Coras52207f12018-07-12 14:48:06 -070051 {
Florin Corasab2f6db2018-08-31 14:31:41 -070052 clib_warning ("unknown local handle 0x%lx", mp->handle);
53 return;
54 }
55 app_wrk = app_worker_get (ls->app_wrk_index);
Florin Coras21795132018-09-09 09:40:51 -070056 if (app_wrk->app_index != mp->context)
Florin Corasab2f6db2018-08-31 14:31:41 -070057 {
58 clib_warning ("server %u doesn't own local handle 0x%lx",
Florin Coras52207f12018-07-12 14:48:06 -070059 mp->context, mp->handle);
60 return;
61 }
Florin Coras623eb562019-02-03 19:28:34 -080062 if (app_worker_local_session_connect_notify (ls))
Florin Coras52207f12018-07-12 14:48:06 -070063 return;
64 ls->session_state = SESSION_STATE_READY;
65 }
66 else
67 {
68 s = session_get_from_handle_if_valid (mp->handle);
Florin Corasb0f662f2018-12-27 14:51:46 -080069 if (!s)
Florin Corase2ea1932018-12-17 23:08:14 -080070 return;
Florin Corasb0f662f2018-12-27 14:51:46 -080071
Florin Coras21795132018-09-09 09:40:51 -070072 app_wrk = app_worker_get (s->app_wrk_index);
73 if (app_wrk->app_index != mp->context)
Florin Coras52207f12018-07-12 14:48:06 -070074 {
75 clib_warning ("app doesn't own session");
76 return;
77 }
Florin Corasb0f662f2018-12-27 14:51:46 -080078
79 old_state = s->session_state;
Florin Coras52207f12018-07-12 14:48:06 -070080 s->session_state = SESSION_STATE_READY;
Florin Coras288eaab2019-02-03 15:26:14 -080081 if (!svm_fifo_is_empty (s->rx_fifo))
Florin Coras21795132018-09-09 09:40:51 -070082 app_worker_lock_and_send_event (app_wrk, s, FIFO_EVENT_APP_RX);
Florin Corasb0f662f2018-12-27 14:51:46 -080083
84 /* Closed while waiting for app to reply. Resend disconnect */
85 if (old_state >= SESSION_STATE_TRANSPORT_CLOSING)
86 {
87 application_t *app = application_get (app_wrk->app_index);
88 app->cb_fns.session_disconnect_callback (s);
89 s->session_state = old_state;
90 return;
91 }
Florin Coras52207f12018-07-12 14:48:06 -070092 }
93}
94
95static void
96session_mq_reset_reply_handler (void *data)
97{
Florin Coras4af830c2018-12-04 09:21:36 -080098 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
Florin Coras52207f12018-07-12 14:48:06 -070099 session_reset_reply_msg_t *mp;
Florin Coras15531972018-08-12 23:50:53 -0700100 app_worker_t *app_wrk;
Florin Coras288eaab2019-02-03 15:26:14 -0800101 session_t *s;
Florin Coras15531972018-08-12 23:50:53 -0700102 application_t *app;
Florin Coras52207f12018-07-12 14:48:06 -0700103 u32 index, thread_index;
104
105 mp = (session_reset_reply_msg_t *) data;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800106 app = application_lookup (mp->context);
Florin Coras52207f12018-07-12 14:48:06 -0700107 if (!app)
108 return;
109
110 session_parse_handle (mp->handle, &index, &thread_index);
111 s = session_get_if_valid (index, thread_index);
Florin Coras3c7d4f92018-12-14 11:28:43 -0800112
113 /* Session was already closed or already cleaned up */
114 if (!s || s->session_state != SESSION_STATE_TRANSPORT_CLOSING)
115 return;
116
Florin Coras15531972018-08-12 23:50:53 -0700117 app_wrk = app_worker_get (s->app_wrk_index);
118 if (!app_wrk || app_wrk->app_index != app->app_index)
119 {
120 clib_warning ("App % does not own handle 0x%lx!", app->app_index,
121 mp->handle);
122 return;
123 }
Florin Coras52207f12018-07-12 14:48:06 -0700124
125 /* Client objected to resetting the session, log and continue */
126 if (mp->retval)
127 {
128 clib_warning ("client retval %d", mp->retval);
129 return;
130 }
131
132 /* This comes as a response to a reset, transport only waiting for
133 * confirmation to remove connection state, no need to disconnect */
Florin Coras4af830c2018-12-04 09:21:36 -0800134 a->handle = mp->handle;
135 a->app_index = app->app_index;
136 vnet_disconnect_session (a);
Florin Coras52207f12018-07-12 14:48:06 -0700137}
138
139static void
140session_mq_disconnected_handler (void *data)
141{
142 session_disconnected_reply_msg_t *rmp;
143 vnet_disconnect_args_t _a, *a = &_a;
144 svm_msg_q_msg_t _msg, *msg = &_msg;
145 session_disconnected_msg_t *mp;
Florin Coras15531972018-08-12 23:50:53 -0700146 app_worker_t *app_wrk;
Florin Coras52207f12018-07-12 14:48:06 -0700147 session_event_t *evt;
Florin Coras288eaab2019-02-03 15:26:14 -0800148 session_t *s;
Florin Coras52207f12018-07-12 14:48:06 -0700149 application_t *app;
150 int rv = 0;
151
152 mp = (session_disconnected_msg_t *) data;
Florin Corasc3638fe2018-08-24 13:58:49 -0700153 if (!(s = session_get_from_handle_if_valid (mp->handle)))
154 {
155 clib_warning ("could not disconnect handle %llu", mp->handle);
156 return;
157 }
Florin Coras15531972018-08-12 23:50:53 -0700158 app_wrk = app_worker_get (s->app_wrk_index);
159 app = application_lookup (mp->client_index);
Florin Corasc3638fe2018-08-24 13:58:49 -0700160 if (!(app_wrk && app && app->app_index == app_wrk->app_index))
Florin Coras52207f12018-07-12 14:48:06 -0700161 {
Florin Corasc3638fe2018-08-24 13:58:49 -0700162 clib_warning ("could not disconnect session: %llu app: %u",
Florin Coras15531972018-08-12 23:50:53 -0700163 mp->handle, mp->client_index);
Florin Coras3d0fadc2018-07-17 05:35:47 -0700164 return;
Florin Coras52207f12018-07-12 14:48:06 -0700165 }
Florin Coras3d0fadc2018-07-17 05:35:47 -0700166
167 a->handle = mp->handle;
Florin Coras15531972018-08-12 23:50:53 -0700168 a->app_index = app_wrk->wrk_index;
Florin Coras3d0fadc2018-07-17 05:35:47 -0700169 rv = vnet_disconnect_session (a);
Florin Coras52207f12018-07-12 14:48:06 -0700170
Florin Coras15531972018-08-12 23:50:53 -0700171 svm_msg_q_lock_and_alloc_msg_w_ring (app_wrk->event_queue,
Florin Coras52207f12018-07-12 14:48:06 -0700172 SESSION_MQ_CTRL_EVT_RING,
173 SVM_Q_WAIT, msg);
Florin Coras15531972018-08-12 23:50:53 -0700174 svm_msg_q_unlock (app_wrk->event_queue);
175 evt = svm_msg_q_msg_data (app_wrk->event_queue, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400176 clib_memset (evt, 0, sizeof (*evt));
Florin Coras30e79c22019-01-02 19:31:22 -0800177 evt->event_type = SESSION_CTRL_EVT_DISCONNECTED_REPLY;
Florin Coras52207f12018-07-12 14:48:06 -0700178 rmp = (session_disconnected_reply_msg_t *) evt->data;
179 rmp->handle = mp->handle;
180 rmp->context = mp->context;
181 rmp->retval = rv;
Florin Coras15531972018-08-12 23:50:53 -0700182 svm_msg_q_add (app_wrk->event_queue, msg, SVM_Q_WAIT);
Florin Coras52207f12018-07-12 14:48:06 -0700183}
184
185static void
186session_mq_disconnected_reply_handler (void *data)
187{
188 session_disconnected_reply_msg_t *mp;
189 vnet_disconnect_args_t _a, *a = &_a;
190 application_t *app;
191
192 mp = (session_disconnected_reply_msg_t *) data;
193
194 /* Client objected to disconnecting the session, log and continue */
195 if (mp->retval)
196 {
197 clib_warning ("client retval %d", mp->retval);
198 return;
199 }
200
201 /* Disconnect has been confirmed. Confirm close to transport */
202 app = application_lookup (mp->context);
203 if (app)
204 {
205 a->handle = mp->handle;
Florin Coras15531972018-08-12 23:50:53 -0700206 a->app_index = app->app_index;
Florin Coras52207f12018-07-12 14:48:06 -0700207 vnet_disconnect_session (a);
208 }
209}
210
Florin Coras30e79c22019-01-02 19:31:22 -0800211static void
212session_mq_worker_update_handler (void *data)
213{
214 session_worker_update_msg_t *mp = (session_worker_update_msg_t *) data;
215 session_worker_update_reply_msg_t *rmp;
216 svm_msg_q_msg_t _msg, *msg = &_msg;
217 app_worker_t *app_wrk;
218 u32 owner_app_wrk_map;
219 session_event_t *evt;
Florin Coras288eaab2019-02-03 15:26:14 -0800220 session_t *s;
Florin Coras30e79c22019-01-02 19:31:22 -0800221 application_t *app;
222
223 app = application_lookup (mp->client_index);
224 if (!app)
225 return;
226 if (!(s = session_get_from_handle_if_valid (mp->handle)))
227 {
228 clib_warning ("invalid handle %llu", mp->handle);
229 return;
230 }
231 app_wrk = app_worker_get (s->app_wrk_index);
232 if (app_wrk->app_index != app->app_index)
233 {
234 clib_warning ("app %u does not own session %llu", app->app_index,
235 mp->handle);
236 return;
237 }
238 owner_app_wrk_map = app_wrk->wrk_map_index;
239 app_wrk = application_get_worker (app, mp->wrk_index);
240
241 /* This needs to come from the new owner */
242 if (mp->req_wrk_index == owner_app_wrk_map)
243 {
244 session_req_worker_update_msg_t *wump;
245
246 svm_msg_q_lock_and_alloc_msg_w_ring (app_wrk->event_queue,
247 SESSION_MQ_CTRL_EVT_RING,
248 SVM_Q_WAIT, msg);
249 svm_msg_q_unlock (app_wrk->event_queue);
250 evt = svm_msg_q_msg_data (app_wrk->event_queue, msg);
251 clib_memset (evt, 0, sizeof (*evt));
252 evt->event_type = SESSION_CTRL_EVT_REQ_WORKER_UPDATE;
253 wump = (session_req_worker_update_msg_t *) evt->data;
254 wump->session_handle = mp->handle;
255 svm_msg_q_add (app_wrk->event_queue, msg, SVM_Q_WAIT);
256 return;
257 }
258
259 app_worker_own_session (app_wrk, s);
260
261 /*
262 * Send reply
263 */
264 svm_msg_q_lock_and_alloc_msg_w_ring (app_wrk->event_queue,
265 SESSION_MQ_CTRL_EVT_RING,
266 SVM_Q_WAIT, msg);
267 svm_msg_q_unlock (app_wrk->event_queue);
268 evt = svm_msg_q_msg_data (app_wrk->event_queue, msg);
269 clib_memset (evt, 0, sizeof (*evt));
270 evt->event_type = SESSION_CTRL_EVT_WORKER_UPDATE_REPLY;
271 rmp = (session_worker_update_reply_msg_t *) evt->data;
272 rmp->handle = mp->handle;
Florin Coras288eaab2019-02-03 15:26:14 -0800273 rmp->rx_fifo = pointer_to_uword (s->rx_fifo);
274 rmp->tx_fifo = pointer_to_uword (s->tx_fifo);
Florin Coras30e79c22019-01-02 19:31:22 -0800275 rmp->segment_handle = session_segment_handle (s);
276 svm_msg_q_add (app_wrk->event_queue, msg, SVM_Q_WAIT);
277
278 /*
279 * Retransmit messages that may have been lost
280 */
Florin Coras288eaab2019-02-03 15:26:14 -0800281 if (s->tx_fifo && !svm_fifo_is_empty (s->tx_fifo))
282 session_send_io_evt_to_thread (s->tx_fifo, FIFO_EVENT_APP_TX);
Florin Coras30e79c22019-01-02 19:31:22 -0800283
Florin Coras288eaab2019-02-03 15:26:14 -0800284 if (s->rx_fifo && !svm_fifo_is_empty (s->rx_fifo))
Florin Coras30e79c22019-01-02 19:31:22 -0800285 app_worker_lock_and_send_event (app_wrk, s, FIFO_EVENT_APP_RX);
286
287 if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
288 app->cb_fns.session_disconnect_callback (s);
289}
290
Dave Barach68b0fb02017-02-28 15:15:56 -0500291vlib_node_registration_t session_queue_node;
292
293typedef struct
294{
295 u32 session_index;
296 u32 server_thread_index;
297} session_queue_trace_t;
298
299/* packet trace format function */
300static u8 *
301format_session_queue_trace (u8 * s, va_list * args)
302{
303 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
304 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
305 session_queue_trace_t *t = va_arg (*args, session_queue_trace_t *);
306
307 s = format (s, "SESSION_QUEUE: session index %d, server thread index %d",
308 t->session_index, t->server_thread_index);
309 return s;
310}
311
Florin Coras6792ec02017-03-13 03:49:51 -0700312#define foreach_session_queue_error \
313_(TX, "Packets transmitted") \
Florin Coras93992a92017-05-24 18:03:56 -0700314_(TIMER, "Timer events") \
315_(NO_BUFFER, "Out of buffers")
Dave Barach68b0fb02017-02-28 15:15:56 -0500316
317typedef enum
318{
319#define _(sym,str) SESSION_QUEUE_ERROR_##sym,
320 foreach_session_queue_error
321#undef _
322 SESSION_QUEUE_N_ERROR,
323} session_queue_error_t;
324
325static char *session_queue_error_strings[] = {
326#define _(sym,string) string,
327 foreach_session_queue_error
328#undef _
329};
330
Florin Coras0d60a0f2018-06-29 02:02:08 -0700331enum
332{
333 SESSION_TX_NO_BUFFERS = -2,
334 SESSION_TX_NO_DATA,
335 SESSION_TX_OK
336};
337
Florin Coras66cf5e02018-06-08 09:17:39 -0700338static void
339session_tx_trace_frame (vlib_main_t * vm, vlib_node_runtime_t * node,
340 u32 next_index, u32 * to_next, u16 n_segs,
Florin Coras288eaab2019-02-03 15:26:14 -0800341 session_t * s, u32 n_trace)
Florin Corasf6d68ed2017-05-07 19:12:02 -0700342{
Florin Coras8e43d042018-05-04 15:46:57 -0700343 session_queue_trace_t *t;
Florin Coras66cf5e02018-06-08 09:17:39 -0700344 vlib_buffer_t *b;
345 int i;
346
347 for (i = 0; i < clib_min (n_trace, n_segs); i++)
348 {
349 b = vlib_get_buffer (vm, to_next[i - n_segs]);
350 vlib_trace_buffer (vm, node, next_index, b, 1 /* follow_chain */ );
Florin Coras66cf5e02018-06-08 09:17:39 -0700351 t = vlib_add_trace (vm, node, b, sizeof (*t));
352 t->session_index = s->session_index;
353 t->server_thread_index = s->thread_index;
354 }
Florin Coras4df38712018-06-20 12:44:16 -0700355 vlib_set_trace_count (vm, node, n_trace - i);
Florin Coras8e43d042018-05-04 15:46:57 -0700356}
Florin Corasf6d68ed2017-05-07 19:12:02 -0700357
Florin Coras8e43d042018-05-04 15:46:57 -0700358always_inline void
359session_tx_fifo_chain_tail (vlib_main_t * vm, session_tx_context_t * ctx,
360 vlib_buffer_t * b, u16 * n_bufs, u8 peek_data)
361{
Florin Coras8e43d042018-05-04 15:46:57 -0700362 vlib_buffer_t *chain_b, *prev_b;
363 u32 chain_bi0, to_deq, left_from_seg;
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700364 session_manager_worker_t *wrk;
Florin Coras8e43d042018-05-04 15:46:57 -0700365 u16 len_to_deq, n_bytes_read;
366 u8 *data, j;
Florin Corasb2215d62017-08-01 16:56:58 -0700367
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700368 wrk = session_manager_get_worker (ctx->s->thread_index);
Florin Coras8e43d042018-05-04 15:46:57 -0700369 b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
370 b->total_length_not_including_first_buffer = 0;
371
372 chain_b = b;
373 left_from_seg = clib_min (ctx->snd_mss - b->current_length,
374 ctx->left_to_snd);
Florin Corasb2215d62017-08-01 16:56:58 -0700375 to_deq = left_from_seg;
Florin Coras8e43d042018-05-04 15:46:57 -0700376 for (j = 1; j < ctx->n_bufs_per_seg; j++)
Florin Corasf6d68ed2017-05-07 19:12:02 -0700377 {
Florin Coras8e43d042018-05-04 15:46:57 -0700378 prev_b = chain_b;
379 len_to_deq = clib_min (to_deq, ctx->deq_per_buf);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700380
381 *n_bufs -= 1;
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700382 chain_bi0 = wrk->tx_buffers[*n_bufs];
Florin Coras8e43d042018-05-04 15:46:57 -0700383 chain_b = vlib_get_buffer (vm, chain_bi0);
384 chain_b->current_data = 0;
385 data = vlib_buffer_get_current (chain_b);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700386 if (peek_data)
387 {
Florin Coras288eaab2019-02-03 15:26:14 -0800388 n_bytes_read = svm_fifo_peek (ctx->s->tx_fifo,
Florin Coras8e43d042018-05-04 15:46:57 -0700389 ctx->tx_offset, len_to_deq, data);
390 ctx->tx_offset += n_bytes_read;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700391 }
392 else
393 {
Florin Coras8e43d042018-05-04 15:46:57 -0700394 if (ctx->transport_vft->tx_type == TRANSPORT_TX_DGRAM)
Florin Coras7fb0fe12018-04-09 09:24:52 -0700395 {
Florin Coras288eaab2019-02-03 15:26:14 -0800396 svm_fifo_t *f = ctx->s->tx_fifo;
Florin Coras8e43d042018-05-04 15:46:57 -0700397 session_dgram_hdr_t *hdr = &ctx->hdr;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700398 u16 deq_now;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700399 deq_now = clib_min (hdr->data_length - hdr->data_offset,
Florin Coras8e43d042018-05-04 15:46:57 -0700400 len_to_deq);
401 n_bytes_read = svm_fifo_peek (f, hdr->data_offset, deq_now,
402 data);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700403 ASSERT (n_bytes_read > 0);
404
405 hdr->data_offset += n_bytes_read;
406 if (hdr->data_offset == hdr->data_length)
shubing guoaea5f392018-08-30 13:29:49 +0800407 {
408 u32 offset = hdr->data_length + SESSION_CONN_HDR_LEN;
409 svm_fifo_dequeue_drop (f, offset);
410 }
Florin Coras7fb0fe12018-04-09 09:24:52 -0700411 }
412 else
Florin Coras288eaab2019-02-03 15:26:14 -0800413 n_bytes_read = svm_fifo_dequeue_nowait (ctx->s->tx_fifo,
Florin Coras8e43d042018-05-04 15:46:57 -0700414 len_to_deq, data);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700415 }
Florin Coras8e43d042018-05-04 15:46:57 -0700416 ASSERT (n_bytes_read == len_to_deq);
417 chain_b->current_length = n_bytes_read;
418 b->total_length_not_including_first_buffer += chain_b->current_length;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700419
420 /* update previous buffer */
Florin Coras8e43d042018-05-04 15:46:57 -0700421 prev_b->next_buffer = chain_bi0;
422 prev_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700423
424 /* update current buffer */
Florin Coras8e43d042018-05-04 15:46:57 -0700425 chain_b->next_buffer = 0;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700426
Florin Corasb2215d62017-08-01 16:56:58 -0700427 to_deq -= n_bytes_read;
428 if (to_deq == 0)
Florin Corasf6d68ed2017-05-07 19:12:02 -0700429 break;
430 }
Florin Coras1f152cd2017-08-18 19:28:03 -0700431 ASSERT (to_deq == 0
Florin Coras8e43d042018-05-04 15:46:57 -0700432 && b->total_length_not_including_first_buffer == left_from_seg);
433 ctx->left_to_snd -= left_from_seg;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700434}
435
Florin Coras8e43d042018-05-04 15:46:57 -0700436always_inline void
437session_tx_fill_buffer (vlib_main_t * vm, session_tx_context_t * ctx,
438 vlib_buffer_t * b, u16 * n_bufs, u8 peek_data)
439{
440 u32 len_to_deq;
441 u8 *data0;
442 int n_bytes_read;
443
444 /*
445 * Start with the first buffer in chain
446 */
447 b->error = 0;
448 b->flags = VNET_BUFFER_F_LOCALLY_ORIGINATED;
449 b->current_data = 0;
Florin Coras8e43d042018-05-04 15:46:57 -0700450
Florin Coras1ee78302019-02-05 15:51:15 -0800451 data0 = vlib_buffer_make_headroom (b, TRANSPORT_MAX_HDRS_LEN);
Florin Coras8e43d042018-05-04 15:46:57 -0700452 len_to_deq = clib_min (ctx->left_to_snd, ctx->deq_per_first_buf);
453
Florin Coras7fb0fe12018-04-09 09:24:52 -0700454 if (peek_data)
455 {
Florin Coras288eaab2019-02-03 15:26:14 -0800456 n_bytes_read = svm_fifo_peek (ctx->s->tx_fifo, ctx->tx_offset,
Florin Coras8e43d042018-05-04 15:46:57 -0700457 len_to_deq, data0);
458 ASSERT (n_bytes_read > 0);
459 /* Keep track of progress locally, transport is also supposed to
460 * increment it independently when pushing the header */
461 ctx->tx_offset += n_bytes_read;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700462 }
463 else
464 {
Florin Coras8e43d042018-05-04 15:46:57 -0700465 if (ctx->transport_vft->tx_type == TRANSPORT_TX_DGRAM)
466 {
467 session_dgram_hdr_t *hdr = &ctx->hdr;
Florin Coras288eaab2019-02-03 15:26:14 -0800468 svm_fifo_t *f = ctx->s->tx_fifo;
Florin Coras8e43d042018-05-04 15:46:57 -0700469 u16 deq_now;
470 u32 offset;
471
472 ASSERT (hdr->data_length > hdr->data_offset);
473 deq_now = clib_min (hdr->data_length - hdr->data_offset,
474 len_to_deq);
475 offset = hdr->data_offset + SESSION_CONN_HDR_LEN;
476 n_bytes_read = svm_fifo_peek (f, offset, deq_now, data0);
477 ASSERT (n_bytes_read > 0);
478
479 if (ctx->s->session_state == SESSION_STATE_LISTENING)
480 {
481 ip_copy (&ctx->tc->rmt_ip, &hdr->rmt_ip, ctx->tc->is_ip4);
482 ctx->tc->rmt_port = hdr->rmt_port;
483 }
484 hdr->data_offset += n_bytes_read;
485 if (hdr->data_offset == hdr->data_length)
486 {
487 offset = hdr->data_length + SESSION_CONN_HDR_LEN;
488 svm_fifo_dequeue_drop (f, offset);
489 }
490 }
Florin Coras7fb0fe12018-04-09 09:24:52 -0700491 else
492 {
Florin Coras288eaab2019-02-03 15:26:14 -0800493 n_bytes_read = svm_fifo_dequeue_nowait (ctx->s->tx_fifo,
Florin Coras8e43d042018-05-04 15:46:57 -0700494 len_to_deq, data0);
495 ASSERT (n_bytes_read > 0);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700496 }
497 }
Florin Coras8e43d042018-05-04 15:46:57 -0700498 b->current_length = n_bytes_read;
499 ctx->left_to_snd -= n_bytes_read;
Dave Barach68b0fb02017-02-28 15:15:56 -0500500
Florin Coras8e43d042018-05-04 15:46:57 -0700501 /*
502 * Fill in the remaining buffers in the chain, if any
503 */
504 if (PREDICT_FALSE (ctx->n_bufs_per_seg > 1 && ctx->left_to_snd))
505 session_tx_fifo_chain_tail (vm, ctx, b, n_bufs, peek_data);
Dave Barach68b0fb02017-02-28 15:15:56 -0500506
Florin Coras8e43d042018-05-04 15:46:57 -0700507 /* *INDENT-OFF* */
Florin Coras8b20bf52018-06-14 14:55:50 -0700508 SESSION_EVT_DBG(SESSION_EVT_DEQ, ctx->s, ({
509 ed->data[0] = FIFO_EVENT_APP_TX;
510 ed->data[1] = ctx->max_dequeue;
Florin Coras8e43d042018-05-04 15:46:57 -0700511 ed->data[2] = len_to_deq;
Florin Coras8b20bf52018-06-14 14:55:50 -0700512 ed->data[3] = ctx->left_to_snd;
Florin Coras8e43d042018-05-04 15:46:57 -0700513 }));
514 /* *INDENT-ON* */
515}
516
517always_inline u8
Florin Coras288eaab2019-02-03 15:26:14 -0800518session_tx_not_ready (session_t * s, u8 peek_data)
Florin Coras8e43d042018-05-04 15:46:57 -0700519{
520 if (peek_data)
Florin Corase04c2992017-03-01 08:17:34 -0800521 {
Florin Coras8e43d042018-05-04 15:46:57 -0700522 /* Can retransmit for closed sessions but can't send new data if
523 * session is not ready or closed */
524 if (s->session_state < SESSION_STATE_READY)
525 return 1;
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800526 if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSED)
Florin Corasca1c8f32018-05-23 21:01:30 -0700527 return 2;
Florin Corase04c2992017-03-01 08:17:34 -0800528 }
Florin Coras8e43d042018-05-04 15:46:57 -0700529 return 0;
530}
Dave Barach68b0fb02017-02-28 15:15:56 -0500531
Florin Coras8e43d042018-05-04 15:46:57 -0700532always_inline transport_connection_t *
533session_tx_get_transport (session_tx_context_t * ctx, u8 peek_data)
534{
535 if (peek_data)
536 {
537 return ctx->transport_vft->get_connection (ctx->s->connection_index,
538 ctx->s->thread_index);
539 }
540 else
541 {
542 if (ctx->s->session_state == SESSION_STATE_LISTENING)
543 return ctx->transport_vft->get_listener (ctx->s->connection_index);
544 else
545 {
546 return ctx->transport_vft->get_connection (ctx->s->connection_index,
547 ctx->s->thread_index);
548 }
549 }
550}
Florin Coras6a9b68b2017-11-21 04:20:42 -0800551
Florin Coras8e43d042018-05-04 15:46:57 -0700552always_inline void
553session_tx_set_dequeue_params (vlib_main_t * vm, session_tx_context_t * ctx,
Florin Corasf08f26d2018-05-10 13:20:47 -0700554 u32 max_segs, u8 peek_data)
Florin Coras8e43d042018-05-04 15:46:57 -0700555{
556 u32 n_bytes_per_buf, n_bytes_per_seg;
Florin Coras288eaab2019-02-03 15:26:14 -0800557 ctx->max_dequeue = svm_fifo_max_dequeue (ctx->s->tx_fifo);
Dave Barach68b0fb02017-02-28 15:15:56 -0500558 if (peek_data)
559 {
Florin Coras9d063042017-09-14 03:08:00 -0400560 /* Offset in rx fifo from where to peek data */
Florin Coras8e43d042018-05-04 15:46:57 -0700561 ctx->tx_offset = ctx->transport_vft->tx_fifo_offset (ctx->tc);
562 if (PREDICT_FALSE (ctx->tx_offset >= ctx->max_dequeue))
563 {
564 ctx->max_len_to_snd = 0;
565 return;
566 }
567 ctx->max_dequeue -= ctx->tx_offset;
Dave Barach68b0fb02017-02-28 15:15:56 -0500568 }
Florin Coras7fb0fe12018-04-09 09:24:52 -0700569 else
570 {
Florin Coras8e43d042018-05-04 15:46:57 -0700571 if (ctx->transport_vft->tx_type == TRANSPORT_TX_DGRAM)
Florin Coras7fb0fe12018-04-09 09:24:52 -0700572 {
Florin Coras8e43d042018-05-04 15:46:57 -0700573 if (ctx->max_dequeue <= sizeof (ctx->hdr))
574 {
575 ctx->max_len_to_snd = 0;
576 return;
577 }
Florin Coras288eaab2019-02-03 15:26:14 -0800578 svm_fifo_peek (ctx->s->tx_fifo, 0, sizeof (ctx->hdr),
Florin Coras8e43d042018-05-04 15:46:57 -0700579 (u8 *) & ctx->hdr);
580 ASSERT (ctx->hdr.data_length > ctx->hdr.data_offset);
581 ctx->max_dequeue = ctx->hdr.data_length - ctx->hdr.data_offset;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700582 }
583 }
Florin Coras8e43d042018-05-04 15:46:57 -0700584 ASSERT (ctx->max_dequeue > 0);
Florin Coras6792ec02017-03-13 03:49:51 -0700585
586 /* Ensure we're not writing more than transport window allows */
Florin Coras8e43d042018-05-04 15:46:57 -0700587 if (ctx->max_dequeue < ctx->snd_space)
Florin Coras3e350af2017-03-30 02:54:28 -0700588 {
589 /* Constrained by tx queue. Try to send only fully formed segments */
Florin Coras8e43d042018-05-04 15:46:57 -0700590 ctx->max_len_to_snd =
591 (ctx->max_dequeue > ctx->snd_mss) ?
592 ctx->max_dequeue - ctx->max_dequeue % ctx->snd_mss : ctx->max_dequeue;
Florin Coras3e350af2017-03-30 02:54:28 -0700593 /* TODO Nagle ? */
594 }
595 else
596 {
Florin Coras1f152cd2017-08-18 19:28:03 -0700597 /* Expectation is that snd_space0 is already a multiple of snd_mss */
Florin Coras8e43d042018-05-04 15:46:57 -0700598 ctx->max_len_to_snd = ctx->snd_space;
Florin Coras3e350af2017-03-30 02:54:28 -0700599 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500600
Florin Corasf08f26d2018-05-10 13:20:47 -0700601 /* Check if we're tx constrained by the node */
602 ctx->n_segs_per_evt = ceil ((f64) ctx->max_len_to_snd / ctx->snd_mss);
603 if (ctx->n_segs_per_evt > max_segs)
604 {
605 ctx->n_segs_per_evt = max_segs;
606 ctx->max_len_to_snd = max_segs * ctx->snd_mss;
607 }
608
Damjan Marion8934a042019-02-09 23:29:26 +0100609 n_bytes_per_buf = vlib_buffer_get_default_data_size (vm);
Florin Coras1ee78302019-02-05 15:51:15 -0800610 ASSERT (n_bytes_per_buf > TRANSPORT_MAX_HDRS_LEN);
611 n_bytes_per_seg = TRANSPORT_MAX_HDRS_LEN + ctx->snd_mss;
Florin Corasf08f26d2018-05-10 13:20:47 -0700612 ctx->n_bufs_per_seg = ceil ((f64) n_bytes_per_seg / n_bytes_per_buf);
Florin Coras8e43d042018-05-04 15:46:57 -0700613 ctx->deq_per_buf = clib_min (ctx->snd_mss, n_bytes_per_buf);
614 ctx->deq_per_first_buf = clib_min (ctx->snd_mss,
Florin Coras1ee78302019-02-05 15:51:15 -0800615 n_bytes_per_buf -
616 TRANSPORT_MAX_HDRS_LEN);
Florin Coras8e43d042018-05-04 15:46:57 -0700617}
Florin Corasf6d68ed2017-05-07 19:12:02 -0700618
Florin Coras8e43d042018-05-04 15:46:57 -0700619always_inline int
620session_tx_fifo_read_and_snd_i (vlib_main_t * vm, vlib_node_runtime_t * node,
Florin Coras1bcad5c2019-01-09 20:04:38 -0800621 session_manager_worker_t * wrk,
622 session_event_t * e, int *n_tx_packets,
Florin Coras8e43d042018-05-04 15:46:57 -0700623 u8 peek_data)
624{
Florin Coras2068fd42019-01-31 14:35:50 -0800625 u32 next_index, next0, next1, *to_next, n_left_to_next, n_left, pbi;
Florin Corasf08f26d2018-05-10 13:20:47 -0700626 u32 n_trace = vlib_get_trace_count (vm, node), n_bufs_needed = 0;
Florin Coras8e43d042018-05-04 15:46:57 -0700627 session_manager_main_t *smm = &session_manager_main;
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700628 session_tx_context_t *ctx = &wrk->ctx;
Florin Coras8e43d042018-05-04 15:46:57 -0700629 transport_proto_t tp;
630 vlib_buffer_t *pb;
Florin Corasca1c8f32018-05-23 21:01:30 -0700631 u16 n_bufs, rv;
Florin Coras8e43d042018-05-04 15:46:57 -0700632
Florin Coras1bcad5c2019-01-09 20:04:38 -0800633 if (PREDICT_FALSE ((rv = session_tx_not_ready (ctx->s, peek_data))))
Florin Coras8e43d042018-05-04 15:46:57 -0700634 {
Florin Corasca1c8f32018-05-23 21:01:30 -0700635 if (rv < 2)
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700636 vec_add1 (wrk->pending_event_vector, *e);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700637 return SESSION_TX_NO_DATA;
Florin Coras8e43d042018-05-04 15:46:57 -0700638 }
639
Florin Coras1bcad5c2019-01-09 20:04:38 -0800640 next_index = smm->session_type_to_next[ctx->s->session_type];
Florin Coras66cf5e02018-06-08 09:17:39 -0700641 next0 = next1 = next_index;
Florin Coras8e43d042018-05-04 15:46:57 -0700642
Florin Coras1bcad5c2019-01-09 20:04:38 -0800643 tp = session_get_transport_proto (ctx->s);
Florin Coras8e43d042018-05-04 15:46:57 -0700644 ctx->transport_vft = transport_protocol_get_vft (tp);
645 ctx->tc = session_tx_get_transport (ctx, peek_data);
646 ctx->snd_mss = ctx->transport_vft->send_mss (ctx->tc);
Florin Coras42ceddb2018-12-12 10:56:01 -0800647
648 if (PREDICT_FALSE (e->event_type == SESSION_IO_EVT_TX_FLUSH))
649 {
650 if (ctx->transport_vft->flush_data)
651 ctx->transport_vft->flush_data (ctx->tc);
652 }
653
654 ctx->snd_space = transport_connection_snd_space (ctx->tc,
David Johnsond9818dd2018-12-14 14:53:41 -0500655 vm->clib_time.
656 last_cpu_time,
Florin Coras42ceddb2018-12-12 10:56:01 -0800657 ctx->snd_mss);
Florin Coras8e43d042018-05-04 15:46:57 -0700658 if (ctx->snd_space == 0 || ctx->snd_mss == 0)
659 {
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700660 vec_add1 (wrk->pending_event_vector, *e);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700661 return SESSION_TX_NO_DATA;
Florin Coras8e43d042018-05-04 15:46:57 -0700662 }
663
664 /* Allow enqueuing of a new event */
Florin Coras288eaab2019-02-03 15:26:14 -0800665 svm_fifo_unset_event (ctx->s->tx_fifo);
Florin Coras8e43d042018-05-04 15:46:57 -0700666
667 /* Check how much we can pull. */
Florin Corasf08f26d2018-05-10 13:20:47 -0700668 session_tx_set_dequeue_params (vm, ctx, VLIB_FRAME_SIZE - *n_tx_packets,
669 peek_data);
670
Florin Coras8e43d042018-05-04 15:46:57 -0700671 if (PREDICT_FALSE (!ctx->max_len_to_snd))
Florin Coras0d60a0f2018-06-29 02:02:08 -0700672 return SESSION_TX_NO_DATA;
Dave Barach68b0fb02017-02-28 15:15:56 -0500673
Florin Corasf08f26d2018-05-10 13:20:47 -0700674 n_bufs_needed = ctx->n_segs_per_evt * ctx->n_bufs_per_seg;
Florin Coras2068fd42019-01-31 14:35:50 -0800675 vec_validate_aligned (wrk->tx_buffers, n_bufs_needed - 1,
676 CLIB_CACHE_LINE_BYTES);
677 n_bufs = vlib_buffer_alloc (vm, wrk->tx_buffers, n_bufs_needed);
678 if (PREDICT_FALSE (n_bufs < n_bufs_needed))
Dave Barach68b0fb02017-02-28 15:15:56 -0500679 {
Florin Coras2068fd42019-01-31 14:35:50 -0800680 if (n_bufs)
681 vlib_buffer_free (vm, wrk->tx_buffers, n_bufs);
682 vec_add1 (wrk->pending_event_vector, *e);
683 return SESSION_TX_NO_BUFFERS;
Florin Coras8e43d042018-05-04 15:46:57 -0700684 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500685
Florin Coras8e43d042018-05-04 15:46:57 -0700686 /*
687 * Write until we fill up a frame
688 */
689 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Florin Corasf08f26d2018-05-10 13:20:47 -0700690 if (PREDICT_FALSE (ctx->n_segs_per_evt > n_left_to_next))
Florin Coras8e43d042018-05-04 15:46:57 -0700691 {
Florin Corasf08f26d2018-05-10 13:20:47 -0700692 ctx->n_segs_per_evt = n_left_to_next;
693 ctx->max_len_to_snd = ctx->snd_mss * n_left_to_next;
694 }
695 ctx->left_to_snd = ctx->max_len_to_snd;
696 n_left = ctx->n_segs_per_evt;
Florin Coras66cf5e02018-06-08 09:17:39 -0700697
698 while (n_left >= 4)
699 {
700 vlib_buffer_t *b0, *b1;
701 u32 bi0, bi1;
702
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700703 pbi = wrk->tx_buffers[n_bufs - 3];
Florin Coras66cf5e02018-06-08 09:17:39 -0700704 pb = vlib_get_buffer (vm, pbi);
705 vlib_prefetch_buffer_header (pb, STORE);
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700706 pbi = wrk->tx_buffers[n_bufs - 4];
Florin Coras66cf5e02018-06-08 09:17:39 -0700707 pb = vlib_get_buffer (vm, pbi);
708 vlib_prefetch_buffer_header (pb, STORE);
709
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700710 to_next[0] = bi0 = wrk->tx_buffers[--n_bufs];
711 to_next[1] = bi1 = wrk->tx_buffers[--n_bufs];
Florin Coras66cf5e02018-06-08 09:17:39 -0700712
713 b0 = vlib_get_buffer (vm, bi0);
714 b1 = vlib_get_buffer (vm, bi1);
715
716 session_tx_fill_buffer (vm, ctx, b0, &n_bufs, peek_data);
717 session_tx_fill_buffer (vm, ctx, b1, &n_bufs, peek_data);
718
719 ctx->transport_vft->push_header (ctx->tc, b0);
720 ctx->transport_vft->push_header (ctx->tc, b1);
721
722 to_next += 2;
723 n_left_to_next -= 2;
724 n_left -= 2;
725
726 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
727 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b1);
728
729 vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
730 n_left_to_next, bi0, bi1, next0,
731 next1);
732 }
Florin Corasf08f26d2018-05-10 13:20:47 -0700733 while (n_left)
734 {
Florin Coras66cf5e02018-06-08 09:17:39 -0700735 vlib_buffer_t *b0;
736 u32 bi0;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700737
Florin Coras91ca4622018-06-30 11:27:59 -0700738 if (n_left > 1)
739 {
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700740 pbi = wrk->tx_buffers[n_bufs - 2];
Florin Coras91ca4622018-06-30 11:27:59 -0700741 pb = vlib_get_buffer (vm, pbi);
742 vlib_prefetch_buffer_header (pb, STORE);
743 }
744
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700745 to_next[0] = bi0 = wrk->tx_buffers[--n_bufs];
Florin Coras66cf5e02018-06-08 09:17:39 -0700746 b0 = vlib_get_buffer (vm, bi0);
747 session_tx_fill_buffer (vm, ctx, b0, &n_bufs, peek_data);
Florin Coras81a13db2018-03-16 08:48:31 -0700748
Florin Coras66cf5e02018-06-08 09:17:39 -0700749 /* Ask transport to push header after current_length and
750 * total_length_not_including_first_buffer are updated */
751 ctx->transport_vft->push_header (ctx->tc, b0);
Florin Corasf6359c82017-06-19 12:26:09 -0400752
Florin Coras66cf5e02018-06-08 09:17:39 -0700753 to_next += 1;
754 n_left_to_next -= 1;
755 n_left -= 1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500756
Florin Coras66cf5e02018-06-08 09:17:39 -0700757 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700758
Florin Coras66cf5e02018-06-08 09:17:39 -0700759 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
760 n_left_to_next, bi0, next0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500761 }
Florin Coras66cf5e02018-06-08 09:17:39 -0700762
763 if (PREDICT_FALSE (n_trace > 0))
764 session_tx_trace_frame (vm, node, next_index, to_next,
Florin Coras1bcad5c2019-01-09 20:04:38 -0800765 ctx->n_segs_per_evt, ctx->s, n_trace);
Florin Coras2068fd42019-01-31 14:35:50 -0800766 if (PREDICT_FALSE (n_bufs))
767 {
768 clib_warning ("not all buffers consumed");
769 vlib_buffer_free (vm, wrk->tx_buffers, n_bufs);
770 }
Florin Corasf08f26d2018-05-10 13:20:47 -0700771 *n_tx_packets += ctx->n_segs_per_evt;
Florin Corasd67f1122018-05-21 17:47:40 -0700772 transport_connection_update_tx_stats (ctx->tc, ctx->max_len_to_snd);
Florin Coras8e43d042018-05-04 15:46:57 -0700773 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
Dave Barach68b0fb02017-02-28 15:15:56 -0500774
Florin Coras6792ec02017-03-13 03:49:51 -0700775 /* If we couldn't dequeue all bytes mark as partially read */
Florin Corasf08f26d2018-05-10 13:20:47 -0700776 ASSERT (ctx->left_to_snd == 0);
Florin Coras8e43d042018-05-04 15:46:57 -0700777 if (ctx->max_len_to_snd < ctx->max_dequeue)
Florin Coras288eaab2019-02-03 15:26:14 -0800778 if (svm_fifo_set_event (ctx->s->tx_fifo))
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700779 vec_add1 (wrk->pending_event_vector, *e);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700780
Florin Coras8e43d042018-05-04 15:46:57 -0700781 if (!peek_data && ctx->transport_vft->tx_type == TRANSPORT_TX_DGRAM)
Dave Barach68b0fb02017-02-28 15:15:56 -0500782 {
Florin Coras7fb0fe12018-04-09 09:24:52 -0700783 /* Fix dgram pre header */
Florin Coras8e43d042018-05-04 15:46:57 -0700784 if (ctx->max_len_to_snd < ctx->max_dequeue)
Florin Coras288eaab2019-02-03 15:26:14 -0800785 svm_fifo_overwrite_head (ctx->s->tx_fifo, (u8 *) & ctx->hdr,
Florin Coras7fb0fe12018-04-09 09:24:52 -0700786 sizeof (session_dgram_pre_hdr_t));
787 /* More data needs to be read */
Florin Coras288eaab2019-02-03 15:26:14 -0800788 else if (svm_fifo_max_dequeue (ctx->s->tx_fifo) > 0)
789 if (svm_fifo_set_event (ctx->s->tx_fifo))
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700790 vec_add1 (wrk->pending_event_vector, *e);
Dave Barach68b0fb02017-02-28 15:15:56 -0500791 }
Florin Coras0d60a0f2018-06-29 02:02:08 -0700792 return SESSION_TX_OK;
Dave Barach68b0fb02017-02-28 15:15:56 -0500793}
794
795int
Florin Corasd79b41e2017-03-04 05:37:52 -0800796session_tx_fifo_peek_and_snd (vlib_main_t * vm, vlib_node_runtime_t * node,
Florin Coras1bcad5c2019-01-09 20:04:38 -0800797 session_manager_worker_t * wrk,
798 session_event_t * e, int *n_tx_pkts)
Dave Barach68b0fb02017-02-28 15:15:56 -0500799{
Florin Coras1bcad5c2019-01-09 20:04:38 -0800800 return session_tx_fifo_read_and_snd_i (vm, node, wrk, e, n_tx_pkts, 1);
Dave Barach68b0fb02017-02-28 15:15:56 -0500801}
802
803int
Florin Corasd79b41e2017-03-04 05:37:52 -0800804session_tx_fifo_dequeue_and_snd (vlib_main_t * vm, vlib_node_runtime_t * node,
Florin Coras1bcad5c2019-01-09 20:04:38 -0800805 session_manager_worker_t * wrk,
806 session_event_t * e, int *n_tx_pkts)
Dave Barach68b0fb02017-02-28 15:15:56 -0500807{
Florin Coras1bcad5c2019-01-09 20:04:38 -0800808 return session_tx_fifo_read_and_snd_i (vm, node, wrk, e, n_tx_pkts, 0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500809}
810
Florin Coras371ca502018-02-21 12:07:41 -0800811int
812session_tx_fifo_dequeue_internal (vlib_main_t * vm,
813 vlib_node_runtime_t * node,
Florin Coras1bcad5c2019-01-09 20:04:38 -0800814 session_manager_worker_t * wrk,
815 session_event_t * e, int *n_tx_pkts)
Florin Coras371ca502018-02-21 12:07:41 -0800816{
Florin Coras288eaab2019-02-03 15:26:14 -0800817 session_t *s = wrk->ctx.s;
Florin Coras371ca502018-02-21 12:07:41 -0800818 application_t *app;
Florin Coras1bcad5c2019-01-09 20:04:38 -0800819
Florin Corasef915342018-09-29 10:23:06 -0700820 if (PREDICT_FALSE (s->session_state == SESSION_STATE_CLOSED))
821 return 0;
Florin Corasab2f6db2018-08-31 14:31:41 -0700822 app = application_get (s->t_app_index);
Florin Coras288eaab2019-02-03 15:26:14 -0800823 svm_fifo_unset_event (s->tx_fifo);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700824 return app->cb_fns.builtin_app_tx_callback (s);
Florin Coras371ca502018-02-21 12:07:41 -0800825}
826
Florin Coras288eaab2019-02-03 15:26:14 -0800827always_inline session_t *
Florin Coras52207f12018-07-12 14:48:06 -0700828session_event_get_session (session_event_t * e, u8 thread_index)
Florin Corasa5464812017-04-19 13:00:05 -0700829{
Florin Coras3cbc04b2017-10-02 00:18:51 -0700830 return session_get_if_valid (e->fifo->master_session_index, thread_index);
Florin Corasa5464812017-04-19 13:00:05 -0700831}
832
Florin Corasd67f1122018-05-21 17:47:40 -0700833static void
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700834session_update_dispatch_period (session_manager_worker_t * wrk, f64 now,
Florin Corasd67f1122018-05-21 17:47:40 -0700835 u32 thread_index)
836{
Florin Corasefefc6b2018-11-07 12:49:19 -0800837 if (wrk->last_tx_packets)
Florin Corasc44a5582018-11-01 16:30:54 -0700838 {
839 f64 sample = now - wrk->last_vlib_time;
Florin Corasc44a5582018-11-01 16:30:54 -0700840 wrk->dispatch_period = (wrk->dispatch_period + sample) * 0.5;
841 }
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700842 wrk->last_vlib_time = now;
Florin Corasd67f1122018-05-21 17:47:40 -0700843}
844
Florin Coras0d60a0f2018-06-29 02:02:08 -0700845static uword
846session_queue_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
847 vlib_frame_t * frame)
848{
849 session_manager_main_t *smm = vnet_get_session_manager_main ();
Florin Coras3c2fed52018-07-04 04:15:05 -0700850 u32 thread_index = vm->thread_index, n_to_dequeue, n_events;
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700851 session_manager_worker_t *wrk = &smm->wrk[thread_index];
Florin Coras5f56d732018-10-30 10:21:59 -0700852 session_event_t *e, *fifo_events;
Florin Coras3c2fed52018-07-04 04:15:05 -0700853 svm_msg_q_msg_t _msg, *msg = &_msg;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700854 f64 now = vlib_time_now (vm);
Florin Coras3c2fed52018-07-04 04:15:05 -0700855 int n_tx_packets = 0, i, rv;
Florin Coras15531972018-08-12 23:50:53 -0700856 app_worker_t *app_wrk;
Florin Coras3c2fed52018-07-04 04:15:05 -0700857 application_t *app;
858 svm_msg_q_t *mq;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700859 void (*fp) (void *);
860
861 SESSION_EVT_DBG (SESSION_EVT_POLL_GAP_TRACK, smm, thread_index);
862
863 /*
864 * Update transport time
865 */
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700866 session_update_dispatch_period (wrk, now, thread_index);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700867 transport_update_time (now, thread_index);
868
Florin Coras0d60a0f2018-06-29 02:02:08 -0700869 SESSION_EVT_DBG (SESSION_EVT_DEQ_NODE, 0);
870
Florin Coras5f56d732018-10-30 10:21:59 -0700871 /* Make sure postponed events are handled first */
872 fifo_events = wrk->free_event_vector;
873 vec_append (fifo_events, wrk->postponed_event_vector);
Florin Coras36ee9f12018-11-02 12:52:10 -0700874 _vec_len (wrk->postponed_event_vector) = 0;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700875
Florin Coras5f56d732018-10-30 10:21:59 -0700876 /* Try to dequeue what is available. Don't wait for lock.
877 * XXX: we may need priorities here */
878 mq = wrk->vpp_event_queue;
879 n_to_dequeue = svm_msg_q_size (mq);
880 if (n_to_dequeue && svm_msg_q_try_lock (mq) == 0)
881 {
882 for (i = 0; i < n_to_dequeue; i++)
883 {
884 vec_add2 (fifo_events, e, 1);
885 svm_msg_q_sub_w_lock (mq, msg);
Florin Coras3c7d4f92018-12-14 11:28:43 -0800886 /* Works because reply messages are smaller than a session evt.
887 * If we ever need to support bigger messages this needs to be
888 * fixed */
Dave Barach178cf492018-11-13 16:34:13 -0500889 clib_memcpy_fast (e, svm_msg_q_msg_data (mq, msg), sizeof (*e));
Florin Coras5f56d732018-10-30 10:21:59 -0700890 svm_msg_q_free_msg (mq, msg);
891 }
892 svm_msg_q_unlock (mq);
893 }
894
895 vec_append (fifo_events, wrk->pending_event_vector);
896 vec_append (fifo_events, wrk->pending_disconnects);
897
Florin Coras5f56d732018-10-30 10:21:59 -0700898 _vec_len (wrk->pending_event_vector) = 0;
Florin Coras36ee9f12018-11-02 12:52:10 -0700899 _vec_len (wrk->pending_disconnects) = 0;
Florin Coras5f56d732018-10-30 10:21:59 -0700900
Florin Coras0d60a0f2018-06-29 02:02:08 -0700901 n_events = vec_len (fifo_events);
Florin Coras5f56d732018-10-30 10:21:59 -0700902 if (PREDICT_FALSE (!n_events))
903 return 0;
904
Florin Coras0d60a0f2018-06-29 02:02:08 -0700905 for (i = 0; i < n_events; i++)
906 {
Florin Coras288eaab2019-02-03 15:26:14 -0800907 session_t *s; /* $$$ prefetch 1 ahead maybe */
Florin Coras52207f12018-07-12 14:48:06 -0700908 session_event_t *e;
Florin Coras1bcad5c2019-01-09 20:04:38 -0800909 u8 need_tx_ntf;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700910
911 e = &fifo_events[i];
912 switch (e->event_type)
913 {
Florin Coras42ceddb2018-12-12 10:56:01 -0800914 case SESSION_IO_EVT_TX_FLUSH:
Florin Coras0d60a0f2018-06-29 02:02:08 -0700915 case FIFO_EVENT_APP_TX:
916 /* Don't try to send more that one frame per dispatch cycle */
917 if (n_tx_packets == VLIB_FRAME_SIZE)
918 {
Florin Coras5f56d732018-10-30 10:21:59 -0700919 vec_add1 (wrk->postponed_event_vector, *e);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700920 break;
921 }
922
923 s = session_event_get_session (e, thread_index);
924 if (PREDICT_FALSE (!s))
925 {
Florin Coras326b81e2018-10-04 19:03:05 -0700926 clib_warning ("session was freed!");
Florin Coras0d60a0f2018-06-29 02:02:08 -0700927 continue;
928 }
Florin Coras1bcad5c2019-01-09 20:04:38 -0800929 wrk->ctx.s = s;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700930 /* Spray packets in per session type frames, since they go to
931 * different nodes */
Florin Coras1bcad5c2019-01-09 20:04:38 -0800932 rv = (smm->session_tx_fns[s->session_type]) (vm, node, wrk, e,
Florin Coras0d60a0f2018-06-29 02:02:08 -0700933 &n_tx_packets);
934 if (PREDICT_TRUE (rv == SESSION_TX_OK))
935 {
Florin Coras288eaab2019-02-03 15:26:14 -0800936 need_tx_ntf = svm_fifo_needs_tx_ntf (s->tx_fifo,
Florin Coras1bcad5c2019-01-09 20:04:38 -0800937 wrk->ctx.max_len_to_snd);
938 if (PREDICT_FALSE (need_tx_ntf))
939 session_dequeue_notify (s);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700940 }
941 else if (PREDICT_FALSE (rv == SESSION_TX_NO_BUFFERS))
942 {
943 vlib_node_increment_counter (vm, node->node_index,
944 SESSION_QUEUE_ERROR_NO_BUFFER, 1);
945 continue;
946 }
947 break;
948 case FIFO_EVENT_DISCONNECT:
Florin Coras3c7d4f92018-12-14 11:28:43 -0800949 s = session_get_from_handle_if_valid (e->session_handle);
950 if (PREDICT_FALSE (!s))
951 break;
952
Florin Corase96bf632018-12-18 22:44:27 -0800953 /* Make sure session disconnects run after the pending list is
954 * drained, i.e., postpone if the first time. If not the first
955 * and the tx queue is still not empty, try to wait for some
956 * dispatch cycles */
957 if (!e->postponed
Florin Coras288eaab2019-02-03 15:26:14 -0800958 || (e->postponed < 200 && svm_fifo_max_dequeue (s->tx_fifo)))
Florin Coras0d60a0f2018-06-29 02:02:08 -0700959 {
Florin Corase96bf632018-12-18 22:44:27 -0800960 e->postponed += 1;
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700961 vec_add1 (wrk->pending_disconnects, *e);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700962 continue;
963 }
964
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800965 session_transport_close (s);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700966 break;
967 case FIFO_EVENT_BUILTIN_RX:
968 s = session_event_get_session (e, thread_index);
Florin Corasef915342018-09-29 10:23:06 -0700969 if (PREDICT_FALSE (!s || s->session_state >= SESSION_STATE_CLOSING))
Florin Coras0d60a0f2018-06-29 02:02:08 -0700970 continue;
Florin Coras288eaab2019-02-03 15:26:14 -0800971 svm_fifo_unset_event (s->rx_fifo);
Florin Coras15531972018-08-12 23:50:53 -0700972 app_wrk = app_worker_get (s->app_wrk_index);
973 app = application_get (app_wrk->app_index);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700974 app->cb_fns.builtin_app_rx_callback (s);
975 break;
Florin Corasef915342018-09-29 10:23:06 -0700976 case FIFO_EVENT_BUILTIN_TX:
977 s = session_get_from_handle_if_valid (e->session_handle);
Florin Coras1bcad5c2019-01-09 20:04:38 -0800978 wrk->ctx.s = s;
Florin Corasef915342018-09-29 10:23:06 -0700979 if (PREDICT_TRUE (s != 0))
Florin Coras1bcad5c2019-01-09 20:04:38 -0800980 session_tx_fifo_dequeue_internal (vm, node, wrk, e,
981 &n_tx_packets);
Florin Corasef915342018-09-29 10:23:06 -0700982 break;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700983 case FIFO_EVENT_RPC:
984 fp = e->rpc_args.fp;
985 (*fp) (e->rpc_args.arg);
986 break;
Florin Coras52207f12018-07-12 14:48:06 -0700987 case SESSION_CTRL_EVT_DISCONNECTED:
988 session_mq_disconnected_handler (e->data);
989 break;
990 case SESSION_CTRL_EVT_ACCEPTED_REPLY:
991 session_mq_accepted_reply_handler (e->data);
992 break;
993 case SESSION_CTRL_EVT_CONNECTED_REPLY:
994 break;
995 case SESSION_CTRL_EVT_DISCONNECTED_REPLY:
996 session_mq_disconnected_reply_handler (e->data);
997 break;
998 case SESSION_CTRL_EVT_RESET_REPLY:
999 session_mq_reset_reply_handler (e->data);
1000 break;
Florin Coras30e79c22019-01-02 19:31:22 -08001001 case SESSION_CTRL_EVT_WORKER_UPDATE:
1002 session_mq_worker_update_handler (e->data);
1003 break;
Florin Coras0d60a0f2018-06-29 02:02:08 -07001004 default:
1005 clib_warning ("unhandled event type %d", e->event_type);
1006 }
1007 }
1008
1009 _vec_len (fifo_events) = 0;
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001010 wrk->free_event_vector = fifo_events;
Florin Corasc44a5582018-11-01 16:30:54 -07001011 wrk->last_tx_packets = n_tx_packets;
Florin Coras0d60a0f2018-06-29 02:02:08 -07001012
1013 vlib_node_increment_counter (vm, session_queue_node.index,
1014 SESSION_QUEUE_ERROR_TX, n_tx_packets);
1015
1016 SESSION_EVT_DBG (SESSION_EVT_DISPATCH_END, smm, thread_index);
1017
1018 return n_tx_packets;
1019}
1020
1021/* *INDENT-OFF* */
1022VLIB_REGISTER_NODE (session_queue_node) =
1023{
1024 .function = session_queue_node_fn,
1025 .name = "session-queue",
1026 .format_trace = format_session_queue_trace,
1027 .type = VLIB_NODE_TYPE_INPUT,
1028 .n_errors = ARRAY_LEN (session_queue_error_strings),
1029 .error_strings = session_queue_error_strings,
1030 .state = VLIB_NODE_STATE_DISABLED,
1031};
1032/* *INDENT-ON* */
1033
Dave Barachacd2a6a2017-05-16 17:41:34 -04001034void
1035dump_thread_0_event_queue (void)
1036{
1037 session_manager_main_t *smm = vnet_get_session_manager_main ();
1038 vlib_main_t *vm = &vlib_global_main;
1039 u32 my_thread_index = vm->thread_index;
Florin Coras52207f12018-07-12 14:48:06 -07001040 session_event_t _e, *e = &_e;
Florin Coras3c2fed52018-07-04 04:15:05 -07001041 svm_msg_q_ring_t *ring;
Florin Coras288eaab2019-02-03 15:26:14 -08001042 session_t *s0;
Florin Coras3c2fed52018-07-04 04:15:05 -07001043 svm_msg_q_msg_t *msg;
1044 svm_msg_q_t *mq;
Dave Barachacd2a6a2017-05-16 17:41:34 -04001045 int i, index;
Dave Barachacd2a6a2017-05-16 17:41:34 -04001046
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001047 mq = smm->wrk[my_thread_index].vpp_event_queue;
Florin Coras3c2fed52018-07-04 04:15:05 -07001048 index = mq->q->head;
Dave Barachacd2a6a2017-05-16 17:41:34 -04001049
Florin Coras3c2fed52018-07-04 04:15:05 -07001050 for (i = 0; i < mq->q->cursize; i++)
Dave Barachacd2a6a2017-05-16 17:41:34 -04001051 {
Florin Coras3c2fed52018-07-04 04:15:05 -07001052 msg = (svm_msg_q_msg_t *) (&mq->q->data[0] + mq->q->elsize * index);
1053 ring = svm_msg_q_ring (mq, msg->ring_index);
Dave Barach178cf492018-11-13 16:34:13 -05001054 clib_memcpy_fast (e, svm_msg_q_msg_data (mq, msg), ring->elsize);
Dave Barachacd2a6a2017-05-16 17:41:34 -04001055
1056 switch (e->event_type)
1057 {
1058 case FIFO_EVENT_APP_TX:
1059 s0 = session_event_get_session (e, my_thread_index);
1060 fformat (stdout, "[%04d] TX session %d\n", i, s0->session_index);
1061 break;
1062
1063 case FIFO_EVENT_DISCONNECT:
Florin Corascea194d2017-10-02 00:18:51 -07001064 s0 = session_get_from_handle (e->session_handle);
Dave Barachacd2a6a2017-05-16 17:41:34 -04001065 fformat (stdout, "[%04d] disconnect session %d\n", i,
1066 s0->session_index);
1067 break;
1068
1069 case FIFO_EVENT_BUILTIN_RX:
1070 s0 = session_event_get_session (e, my_thread_index);
1071 fformat (stdout, "[%04d] builtin_rx %d\n", i, s0->session_index);
1072 break;
1073
1074 case FIFO_EVENT_RPC:
1075 fformat (stdout, "[%04d] RPC call %llx with %llx\n",
David Johnsond9818dd2018-12-14 14:53:41 -05001076 i, (u64) (uword) (e->rpc_args.fp),
1077 (u64) (uword) (e->rpc_args.arg));
Dave Barachacd2a6a2017-05-16 17:41:34 -04001078 break;
1079
1080 default:
1081 fformat (stdout, "[%04d] unhandled event type %d\n",
1082 i, e->event_type);
1083 break;
1084 }
1085
1086 index++;
1087
Florin Coras3c2fed52018-07-04 04:15:05 -07001088 if (index == mq->q->maxsize)
Dave Barachacd2a6a2017-05-16 17:41:34 -04001089 index = 0;
1090 }
1091}
1092
Florin Coras6534b7a2017-07-18 05:38:03 -04001093static u8
Florin Coras52207f12018-07-12 14:48:06 -07001094session_node_cmp_event (session_event_t * e, svm_fifo_t * f)
Florin Coras6534b7a2017-07-18 05:38:03 -04001095{
Florin Coras288eaab2019-02-03 15:26:14 -08001096 session_t *s;
Florin Coras6534b7a2017-07-18 05:38:03 -04001097 switch (e->event_type)
1098 {
1099 case FIFO_EVENT_APP_RX:
1100 case FIFO_EVENT_APP_TX:
1101 case FIFO_EVENT_BUILTIN_RX:
1102 if (e->fifo == f)
1103 return 1;
1104 break;
1105 case FIFO_EVENT_DISCONNECT:
1106 break;
1107 case FIFO_EVENT_RPC:
Florin Corascea194d2017-10-02 00:18:51 -07001108 s = session_get_from_handle (e->session_handle);
Florin Coras6534b7a2017-07-18 05:38:03 -04001109 if (!s)
1110 {
1111 clib_warning ("session has event but doesn't exist!");
1112 break;
1113 }
Florin Coras288eaab2019-02-03 15:26:14 -08001114 if (s->rx_fifo == f || s->tx_fifo == f)
Florin Coras6534b7a2017-07-18 05:38:03 -04001115 return 1;
1116 break;
1117 default:
1118 break;
1119 }
1120 return 0;
1121}
1122
1123u8
Florin Coras52207f12018-07-12 14:48:06 -07001124session_node_lookup_fifo_event (svm_fifo_t * f, session_event_t * e)
Florin Coras6534b7a2017-07-18 05:38:03 -04001125{
Florin Coras52207f12018-07-12 14:48:06 -07001126 session_event_t *pending_event_vector, *evt;
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001127 session_manager_worker_t *wrk;
Florin Coras6534b7a2017-07-18 05:38:03 -04001128 int i, index, found = 0;
Florin Coras3c2fed52018-07-04 04:15:05 -07001129 svm_msg_q_msg_t *msg;
1130 svm_msg_q_ring_t *ring;
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001131 svm_msg_q_t *mq;
Florin Coras6534b7a2017-07-18 05:38:03 -04001132 u8 thread_index;
1133
1134 ASSERT (e);
1135 thread_index = f->master_thread_index;
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001136 wrk = session_manager_get_worker (thread_index);
1137
Florin Coras6534b7a2017-07-18 05:38:03 -04001138 /*
1139 * Search evt queue
1140 */
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001141 mq = wrk->vpp_event_queue;
Florin Coras3c2fed52018-07-04 04:15:05 -07001142 index = mq->q->head;
1143 for (i = 0; i < mq->q->cursize; i++)
Florin Coras6534b7a2017-07-18 05:38:03 -04001144 {
Florin Coras3c2fed52018-07-04 04:15:05 -07001145 msg = (svm_msg_q_msg_t *) (&mq->q->data[0] + mq->q->elsize * index);
1146 ring = svm_msg_q_ring (mq, msg->ring_index);
Dave Barach178cf492018-11-13 16:34:13 -05001147 clib_memcpy_fast (e, svm_msg_q_msg_data (mq, msg), ring->elsize);
Florin Coras6534b7a2017-07-18 05:38:03 -04001148 found = session_node_cmp_event (e, f);
1149 if (found)
Florin Coras371ca502018-02-21 12:07:41 -08001150 return 1;
Florin Coras3c2fed52018-07-04 04:15:05 -07001151 if (++index == mq->q->maxsize)
Florin Coras6534b7a2017-07-18 05:38:03 -04001152 index = 0;
1153 }
1154 /*
1155 * Search pending events vector
1156 */
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001157 pending_event_vector = wrk->pending_event_vector;
Florin Coras6534b7a2017-07-18 05:38:03 -04001158 vec_foreach (evt, pending_event_vector)
1159 {
1160 found = session_node_cmp_event (evt, f);
1161 if (found)
1162 {
Dave Barach178cf492018-11-13 16:34:13 -05001163 clib_memcpy_fast (e, evt, sizeof (*evt));
Florin Coras6534b7a2017-07-18 05:38:03 -04001164 break;
1165 }
1166 }
1167 return found;
1168}
1169
Dave Barach2a863912017-11-28 10:11:42 -05001170static clib_error_t *
1171session_queue_exit (vlib_main_t * vm)
1172{
1173 if (vec_len (vlib_mains) < 2)
1174 return 0;
1175
1176 /*
1177 * Shut off (especially) worker-thread session nodes.
1178 * Otherwise, vpp can crash as the main thread unmaps the
1179 * API segment.
1180 */
1181 vlib_worker_thread_barrier_sync (vm);
1182 session_node_enable_disable (0 /* is_enable */ );
1183 vlib_worker_thread_barrier_release (vm);
1184 return 0;
1185}
1186
1187VLIB_MAIN_LOOP_EXIT_FUNCTION (session_queue_exit);
1188
Florin Corasfd542f12018-05-16 19:28:24 -07001189static uword
1190session_queue_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
1191 vlib_frame_t * f)
1192{
1193 f64 now, timeout = 1.0;
1194 uword *event_data = 0;
1195 uword event_type;
1196
1197 while (1)
1198 {
1199 vlib_process_wait_for_event_or_clock (vm, timeout);
1200 now = vlib_time_now (vm);
1201 event_type = vlib_process_get_events (vm, (uword **) & event_data);
1202
1203 switch (event_type)
1204 {
1205 case SESSION_Q_PROCESS_FLUSH_FRAMES:
1206 /* Flush the frames by updating all transports times */
1207 transport_update_time (now, 0);
1208 break;
1209 case SESSION_Q_PROCESS_STOP:
1210 timeout = 100000.0;
1211 break;
1212 case ~0:
1213 /* Timed out. Update time for all transports to trigger all
1214 * outstanding retransmits. */
1215 transport_update_time (now, 0);
1216 break;
1217 }
1218 vec_reset_length (event_data);
1219 }
1220 return 0;
1221}
1222
1223/* *INDENT-OFF* */
1224VLIB_REGISTER_NODE (session_queue_process_node) =
1225{
1226 .function = session_queue_process,
1227 .type = VLIB_NODE_TYPE_PROCESS,
1228 .name = "session-queue-process",
1229 .state = VLIB_NODE_STATE_DISABLED,
1230};
1231/* *INDENT-ON* */
1232
Dave Barach68b0fb02017-02-28 15:15:56 -05001233/*
1234 * fd.io coding-style-patch-verification: ON
1235 *
1236 * Local Variables:
1237 * eval: (c-set-style "gnu")
1238 * End:
1239 */