blob: bffae62ff797869cafeb823192dd23dda3c11650 [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 Coras2b81e3c2019-02-27 07:55:46 -080028static void session_mq_accepted_reply_handler (void *data);
29
30static void
31accepted_notify_cb (void *data, u32 data_len)
32{
33 session_mq_accepted_reply_handler (data);
34}
35
Florin Coras52207f12018-07-12 14:48:06 -070036static void
37session_mq_accepted_reply_handler (void *data)
38{
39 session_accepted_reply_msg_t *mp = (session_accepted_reply_msg_t *) data;
40 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
Florin Coras288eaab2019-02-03 15:26:14 -080041 session_state_t old_state;
Florin Coras21795132018-09-09 09:40:51 -070042 app_worker_t *app_wrk;
Florin Coras288eaab2019-02-03 15:26:14 -080043 session_t *s;
Florin Coras52207f12018-07-12 14:48:06 -070044
45 /* Server isn't interested, kill the session */
46 if (mp->retval)
47 {
48 a->app_index = mp->context;
49 a->handle = mp->handle;
50 vnet_disconnect_session (a);
51 return;
52 }
53
Florin Coras2b81e3c2019-02-27 07:55:46 -080054 /* Mail this back from the main thread. We're not polling in main
55 * thread so we're using other workers for notifications. */
56 if (vlib_num_workers () && vlib_get_thread_index () != 0
57 && session_thread_from_handle (mp->handle) == 0)
Florin Coras52207f12018-07-12 14:48:06 -070058 {
Florin Coras2b81e3c2019-02-27 07:55:46 -080059 vl_api_rpc_call_main_thread (accepted_notify_cb, data,
60 sizeof (session_accepted_reply_msg_t));
61 return;
62 }
63
64 s = session_get_from_handle_if_valid (mp->handle);
65 if (!s)
66 return;
67
68 app_wrk = app_worker_get (s->app_wrk_index);
69 if (app_wrk->app_index != mp->context)
70 {
71 clib_warning ("app doesn't own session");
72 return;
73 }
74
75 if (!session_has_transport (s))
76 {
Florin Coras653e43f2019-03-04 10:56:23 -080077 s->session_state = SESSION_STATE_READY;
Florin Coras2b81e3c2019-02-27 07:55:46 -080078 if (ct_session_connect_notify (s))
Florin Coras52207f12018-07-12 14:48:06 -070079 return;
Florin Coras52207f12018-07-12 14:48:06 -070080 }
81 else
82 {
Florin Corasb0f662f2018-12-27 14:51:46 -080083 old_state = s->session_state;
Florin Coras52207f12018-07-12 14:48:06 -070084 s->session_state = SESSION_STATE_READY;
Sirshak Das28aa5392019-02-05 01:33:33 -060085
86 if (!svm_fifo_is_empty_prod (s->rx_fifo))
Florin Corasf6c43132019-03-01 12:41:21 -080087 app_worker_lock_and_send_event (app_wrk, s, SESSION_IO_EVT_RX);
Florin Corasb0f662f2018-12-27 14:51:46 -080088
89 /* Closed while waiting for app to reply. Resend disconnect */
90 if (old_state >= SESSION_STATE_TRANSPORT_CLOSING)
91 {
Florin Coras69b68ef2019-04-02 11:38:51 -070092 app_worker_close_notify (app_wrk, s);
Florin Corasb0f662f2018-12-27 14:51:46 -080093 s->session_state = old_state;
94 return;
95 }
Florin Coras52207f12018-07-12 14:48:06 -070096 }
97}
98
99static void
100session_mq_reset_reply_handler (void *data)
101{
Florin Coras4af830c2018-12-04 09:21:36 -0800102 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
Florin Coras52207f12018-07-12 14:48:06 -0700103 session_reset_reply_msg_t *mp;
Florin Coras15531972018-08-12 23:50:53 -0700104 app_worker_t *app_wrk;
Florin Coras288eaab2019-02-03 15:26:14 -0800105 session_t *s;
Florin Coras15531972018-08-12 23:50:53 -0700106 application_t *app;
Florin Coras52207f12018-07-12 14:48:06 -0700107 u32 index, thread_index;
108
109 mp = (session_reset_reply_msg_t *) data;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800110 app = application_lookup (mp->context);
Florin Coras52207f12018-07-12 14:48:06 -0700111 if (!app)
112 return;
113
114 session_parse_handle (mp->handle, &index, &thread_index);
115 s = session_get_if_valid (index, thread_index);
Florin Coras3c7d4f92018-12-14 11:28:43 -0800116
117 /* Session was already closed or already cleaned up */
118 if (!s || s->session_state != SESSION_STATE_TRANSPORT_CLOSING)
119 return;
120
Florin Coras15531972018-08-12 23:50:53 -0700121 app_wrk = app_worker_get (s->app_wrk_index);
122 if (!app_wrk || app_wrk->app_index != app->app_index)
123 {
124 clib_warning ("App % does not own handle 0x%lx!", app->app_index,
125 mp->handle);
126 return;
127 }
Florin Coras52207f12018-07-12 14:48:06 -0700128
129 /* Client objected to resetting the session, log and continue */
130 if (mp->retval)
131 {
132 clib_warning ("client retval %d", mp->retval);
133 return;
134 }
135
136 /* This comes as a response to a reset, transport only waiting for
137 * confirmation to remove connection state, no need to disconnect */
Florin Coras4af830c2018-12-04 09:21:36 -0800138 a->handle = mp->handle;
139 a->app_index = app->app_index;
140 vnet_disconnect_session (a);
Florin Coras52207f12018-07-12 14:48:06 -0700141}
142
143static void
144session_mq_disconnected_handler (void *data)
145{
146 session_disconnected_reply_msg_t *rmp;
147 vnet_disconnect_args_t _a, *a = &_a;
148 svm_msg_q_msg_t _msg, *msg = &_msg;
149 session_disconnected_msg_t *mp;
Florin Coras15531972018-08-12 23:50:53 -0700150 app_worker_t *app_wrk;
Florin Coras52207f12018-07-12 14:48:06 -0700151 session_event_t *evt;
Florin Coras288eaab2019-02-03 15:26:14 -0800152 session_t *s;
Florin Coras52207f12018-07-12 14:48:06 -0700153 application_t *app;
154 int rv = 0;
155
156 mp = (session_disconnected_msg_t *) data;
Florin Corasc3638fe2018-08-24 13:58:49 -0700157 if (!(s = session_get_from_handle_if_valid (mp->handle)))
158 {
159 clib_warning ("could not disconnect handle %llu", mp->handle);
160 return;
161 }
Florin Coras15531972018-08-12 23:50:53 -0700162 app_wrk = app_worker_get (s->app_wrk_index);
163 app = application_lookup (mp->client_index);
Florin Corasc3638fe2018-08-24 13:58:49 -0700164 if (!(app_wrk && app && app->app_index == app_wrk->app_index))
Florin Coras52207f12018-07-12 14:48:06 -0700165 {
Florin Corasc3638fe2018-08-24 13:58:49 -0700166 clib_warning ("could not disconnect session: %llu app: %u",
Florin Coras15531972018-08-12 23:50:53 -0700167 mp->handle, mp->client_index);
Florin Coras3d0fadc2018-07-17 05:35:47 -0700168 return;
Florin Coras52207f12018-07-12 14:48:06 -0700169 }
Florin Coras3d0fadc2018-07-17 05:35:47 -0700170
171 a->handle = mp->handle;
Florin Coras15531972018-08-12 23:50:53 -0700172 a->app_index = app_wrk->wrk_index;
Florin Coras3d0fadc2018-07-17 05:35:47 -0700173 rv = vnet_disconnect_session (a);
Florin Coras52207f12018-07-12 14:48:06 -0700174
Florin Coras15531972018-08-12 23:50:53 -0700175 svm_msg_q_lock_and_alloc_msg_w_ring (app_wrk->event_queue,
Florin Coras52207f12018-07-12 14:48:06 -0700176 SESSION_MQ_CTRL_EVT_RING,
177 SVM_Q_WAIT, msg);
Florin Coras15531972018-08-12 23:50:53 -0700178 evt = svm_msg_q_msg_data (app_wrk->event_queue, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400179 clib_memset (evt, 0, sizeof (*evt));
Florin Coras30e79c22019-01-02 19:31:22 -0800180 evt->event_type = SESSION_CTRL_EVT_DISCONNECTED_REPLY;
Florin Coras52207f12018-07-12 14:48:06 -0700181 rmp = (session_disconnected_reply_msg_t *) evt->data;
182 rmp->handle = mp->handle;
183 rmp->context = mp->context;
184 rmp->retval = rv;
Florin Coras2b5fed82019-07-25 14:51:09 -0700185 svm_msg_q_add_and_unlock (app_wrk->event_queue, msg);
Florin Coras52207f12018-07-12 14:48:06 -0700186}
187
188static void
189session_mq_disconnected_reply_handler (void *data)
190{
191 session_disconnected_reply_msg_t *mp;
192 vnet_disconnect_args_t _a, *a = &_a;
193 application_t *app;
194
195 mp = (session_disconnected_reply_msg_t *) data;
196
197 /* Client objected to disconnecting the session, log and continue */
198 if (mp->retval)
199 {
200 clib_warning ("client retval %d", mp->retval);
201 return;
202 }
203
204 /* Disconnect has been confirmed. Confirm close to transport */
205 app = application_lookup (mp->context);
206 if (app)
207 {
208 a->handle = mp->handle;
Florin Coras15531972018-08-12 23:50:53 -0700209 a->app_index = app->app_index;
Florin Coras52207f12018-07-12 14:48:06 -0700210 vnet_disconnect_session (a);
211 }
212}
213
Florin Coras30e79c22019-01-02 19:31:22 -0800214static void
215session_mq_worker_update_handler (void *data)
216{
217 session_worker_update_msg_t *mp = (session_worker_update_msg_t *) data;
218 session_worker_update_reply_msg_t *rmp;
219 svm_msg_q_msg_t _msg, *msg = &_msg;
220 app_worker_t *app_wrk;
221 u32 owner_app_wrk_map;
222 session_event_t *evt;
Florin Coras288eaab2019-02-03 15:26:14 -0800223 session_t *s;
Florin Coras30e79c22019-01-02 19:31:22 -0800224 application_t *app;
225
226 app = application_lookup (mp->client_index);
227 if (!app)
228 return;
229 if (!(s = session_get_from_handle_if_valid (mp->handle)))
230 {
231 clib_warning ("invalid handle %llu", mp->handle);
232 return;
233 }
234 app_wrk = app_worker_get (s->app_wrk_index);
235 if (app_wrk->app_index != app->app_index)
236 {
237 clib_warning ("app %u does not own session %llu", app->app_index,
238 mp->handle);
239 return;
240 }
241 owner_app_wrk_map = app_wrk->wrk_map_index;
242 app_wrk = application_get_worker (app, mp->wrk_index);
243
244 /* This needs to come from the new owner */
245 if (mp->req_wrk_index == owner_app_wrk_map)
246 {
247 session_req_worker_update_msg_t *wump;
248
249 svm_msg_q_lock_and_alloc_msg_w_ring (app_wrk->event_queue,
250 SESSION_MQ_CTRL_EVT_RING,
251 SVM_Q_WAIT, msg);
Florin Coras30e79c22019-01-02 19:31:22 -0800252 evt = svm_msg_q_msg_data (app_wrk->event_queue, msg);
253 clib_memset (evt, 0, sizeof (*evt));
254 evt->event_type = SESSION_CTRL_EVT_REQ_WORKER_UPDATE;
255 wump = (session_req_worker_update_msg_t *) evt->data;
256 wump->session_handle = mp->handle;
Florin Coras2b5fed82019-07-25 14:51:09 -0700257 svm_msg_q_add_and_unlock (app_wrk->event_queue, msg);
Florin Coras30e79c22019-01-02 19:31:22 -0800258 return;
259 }
260
261 app_worker_own_session (app_wrk, s);
262
263 /*
264 * Send reply
265 */
266 svm_msg_q_lock_and_alloc_msg_w_ring (app_wrk->event_queue,
267 SESSION_MQ_CTRL_EVT_RING,
268 SVM_Q_WAIT, msg);
Florin Coras30e79c22019-01-02 19:31:22 -0800269 evt = svm_msg_q_msg_data (app_wrk->event_queue, msg);
270 clib_memset (evt, 0, sizeof (*evt));
271 evt->event_type = SESSION_CTRL_EVT_WORKER_UPDATE_REPLY;
272 rmp = (session_worker_update_reply_msg_t *) evt->data;
273 rmp->handle = mp->handle;
Florin Coras288eaab2019-02-03 15:26:14 -0800274 rmp->rx_fifo = pointer_to_uword (s->rx_fifo);
275 rmp->tx_fifo = pointer_to_uword (s->tx_fifo);
Florin Coras30e79c22019-01-02 19:31:22 -0800276 rmp->segment_handle = session_segment_handle (s);
Florin Coras2b5fed82019-07-25 14:51:09 -0700277 svm_msg_q_add_and_unlock (app_wrk->event_queue, msg);
Florin Coras30e79c22019-01-02 19:31:22 -0800278
279 /*
280 * Retransmit messages that may have been lost
281 */
Florin Coras288eaab2019-02-03 15:26:14 -0800282 if (s->tx_fifo && !svm_fifo_is_empty (s->tx_fifo))
Florin Corasf6c43132019-03-01 12:41:21 -0800283 session_send_io_evt_to_thread (s->tx_fifo, SESSION_IO_EVT_TX);
Florin Coras30e79c22019-01-02 19:31:22 -0800284
Florin Coras288eaab2019-02-03 15:26:14 -0800285 if (s->rx_fifo && !svm_fifo_is_empty (s->rx_fifo))
Florin Corasf6c43132019-03-01 12:41:21 -0800286 app_worker_lock_and_send_event (app_wrk, s, SESSION_IO_EVT_RX);
Florin Coras30e79c22019-01-02 19:31:22 -0800287
288 if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
Florin Coras69b68ef2019-04-02 11:38:51 -0700289 app_worker_close_notify (app_wrk, s);
Florin Coras30e79c22019-01-02 19:31:22 -0800290}
291
Dave Barach68b0fb02017-02-28 15:15:56 -0500292vlib_node_registration_t session_queue_node;
293
294typedef struct
295{
296 u32 session_index;
297 u32 server_thread_index;
298} session_queue_trace_t;
299
300/* packet trace format function */
301static u8 *
302format_session_queue_trace (u8 * s, va_list * args)
303{
304 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
305 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
306 session_queue_trace_t *t = va_arg (*args, session_queue_trace_t *);
307
308 s = format (s, "SESSION_QUEUE: session index %d, server thread index %d",
309 t->session_index, t->server_thread_index);
310 return s;
311}
312
Florin Coras6792ec02017-03-13 03:49:51 -0700313#define foreach_session_queue_error \
314_(TX, "Packets transmitted") \
Florin Coras93992a92017-05-24 18:03:56 -0700315_(TIMER, "Timer events") \
316_(NO_BUFFER, "Out of buffers")
Dave Barach68b0fb02017-02-28 15:15:56 -0500317
318typedef enum
319{
320#define _(sym,str) SESSION_QUEUE_ERROR_##sym,
321 foreach_session_queue_error
322#undef _
323 SESSION_QUEUE_N_ERROR,
324} session_queue_error_t;
325
326static char *session_queue_error_strings[] = {
327#define _(sym,string) string,
328 foreach_session_queue_error
329#undef _
330};
331
Florin Coras0d60a0f2018-06-29 02:02:08 -0700332enum
333{
334 SESSION_TX_NO_BUFFERS = -2,
335 SESSION_TX_NO_DATA,
336 SESSION_TX_OK
337};
338
Florin Coras66cf5e02018-06-08 09:17:39 -0700339static void
340session_tx_trace_frame (vlib_main_t * vm, vlib_node_runtime_t * node,
341 u32 next_index, u32 * to_next, u16 n_segs,
Florin Coras288eaab2019-02-03 15:26:14 -0800342 session_t * s, u32 n_trace)
Florin Corasf6d68ed2017-05-07 19:12:02 -0700343{
Florin Coras8e43d042018-05-04 15:46:57 -0700344 session_queue_trace_t *t;
Florin Coras66cf5e02018-06-08 09:17:39 -0700345 vlib_buffer_t *b;
346 int i;
347
348 for (i = 0; i < clib_min (n_trace, n_segs); i++)
349 {
350 b = vlib_get_buffer (vm, to_next[i - n_segs]);
351 vlib_trace_buffer (vm, node, next_index, b, 1 /* follow_chain */ );
Florin Coras66cf5e02018-06-08 09:17:39 -0700352 t = vlib_add_trace (vm, node, b, sizeof (*t));
353 t->session_index = s->session_index;
354 t->server_thread_index = s->thread_index;
355 }
Florin Coras4df38712018-06-20 12:44:16 -0700356 vlib_set_trace_count (vm, node, n_trace - i);
Florin Coras8e43d042018-05-04 15:46:57 -0700357}
Florin Corasf6d68ed2017-05-07 19:12:02 -0700358
Florin Coras8e43d042018-05-04 15:46:57 -0700359always_inline void
360session_tx_fifo_chain_tail (vlib_main_t * vm, session_tx_context_t * ctx,
361 vlib_buffer_t * b, u16 * n_bufs, u8 peek_data)
362{
Florin Coras8e43d042018-05-04 15:46:57 -0700363 vlib_buffer_t *chain_b, *prev_b;
364 u32 chain_bi0, to_deq, left_from_seg;
Florin Coras31c99552019-03-01 13:00:58 -0800365 session_worker_t *wrk;
Florin Coras8e43d042018-05-04 15:46:57 -0700366 u16 len_to_deq, n_bytes_read;
367 u8 *data, j;
Florin Corasb2215d62017-08-01 16:56:58 -0700368
Florin Coras31c99552019-03-01 13:00:58 -0800369 wrk = session_main_get_worker (ctx->s->thread_index);
Florin Coras8e43d042018-05-04 15:46:57 -0700370 b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
371 b->total_length_not_including_first_buffer = 0;
372
373 chain_b = b;
374 left_from_seg = clib_min (ctx->snd_mss - b->current_length,
375 ctx->left_to_snd);
Florin Corasb2215d62017-08-01 16:56:58 -0700376 to_deq = left_from_seg;
Florin Coras8e43d042018-05-04 15:46:57 -0700377 for (j = 1; j < ctx->n_bufs_per_seg; j++)
Florin Corasf6d68ed2017-05-07 19:12:02 -0700378 {
Florin Coras8e43d042018-05-04 15:46:57 -0700379 prev_b = chain_b;
380 len_to_deq = clib_min (to_deq, ctx->deq_per_buf);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700381
382 *n_bufs -= 1;
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700383 chain_bi0 = wrk->tx_buffers[*n_bufs];
Florin Coras8e43d042018-05-04 15:46:57 -0700384 chain_b = vlib_get_buffer (vm, chain_bi0);
385 chain_b->current_data = 0;
386 data = vlib_buffer_get_current (chain_b);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700387 if (peek_data)
388 {
Florin Coras288eaab2019-02-03 15:26:14 -0800389 n_bytes_read = svm_fifo_peek (ctx->s->tx_fifo,
Florin Coras8e43d042018-05-04 15:46:57 -0700390 ctx->tx_offset, len_to_deq, data);
391 ctx->tx_offset += n_bytes_read;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700392 }
393 else
394 {
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200395 if (ctx->transport_vft->transport_options.tx_type ==
396 TRANSPORT_TX_DGRAM)
Florin Coras7fb0fe12018-04-09 09:24:52 -0700397 {
Florin Coras288eaab2019-02-03 15:26:14 -0800398 svm_fifo_t *f = ctx->s->tx_fifo;
Florin Coras8e43d042018-05-04 15:46:57 -0700399 session_dgram_hdr_t *hdr = &ctx->hdr;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700400 u16 deq_now;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700401 deq_now = clib_min (hdr->data_length - hdr->data_offset,
Florin Coras8e43d042018-05-04 15:46:57 -0700402 len_to_deq);
403 n_bytes_read = svm_fifo_peek (f, hdr->data_offset, deq_now,
404 data);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700405 ASSERT (n_bytes_read > 0);
406
407 hdr->data_offset += n_bytes_read;
408 if (hdr->data_offset == hdr->data_length)
shubing guoaea5f392018-08-30 13:29:49 +0800409 {
410 u32 offset = hdr->data_length + SESSION_CONN_HDR_LEN;
411 svm_fifo_dequeue_drop (f, offset);
412 }
Florin Coras7fb0fe12018-04-09 09:24:52 -0700413 }
414 else
Florin Coras87b15ce2019-04-28 21:16:30 -0700415 n_bytes_read = svm_fifo_dequeue (ctx->s->tx_fifo,
416 len_to_deq, data);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700417 }
Florin Coras8e43d042018-05-04 15:46:57 -0700418 ASSERT (n_bytes_read == len_to_deq);
419 chain_b->current_length = n_bytes_read;
420 b->total_length_not_including_first_buffer += chain_b->current_length;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700421
422 /* update previous buffer */
Florin Coras8e43d042018-05-04 15:46:57 -0700423 prev_b->next_buffer = chain_bi0;
424 prev_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700425
426 /* update current buffer */
Florin Coras8e43d042018-05-04 15:46:57 -0700427 chain_b->next_buffer = 0;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700428
Florin Corasb2215d62017-08-01 16:56:58 -0700429 to_deq -= n_bytes_read;
430 if (to_deq == 0)
Florin Corasf6d68ed2017-05-07 19:12:02 -0700431 break;
432 }
Florin Coras1f152cd2017-08-18 19:28:03 -0700433 ASSERT (to_deq == 0
Florin Coras8e43d042018-05-04 15:46:57 -0700434 && b->total_length_not_including_first_buffer == left_from_seg);
435 ctx->left_to_snd -= left_from_seg;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700436}
437
Florin Coras8e43d042018-05-04 15:46:57 -0700438always_inline void
439session_tx_fill_buffer (vlib_main_t * vm, session_tx_context_t * ctx,
440 vlib_buffer_t * b, u16 * n_bufs, u8 peek_data)
441{
442 u32 len_to_deq;
443 u8 *data0;
444 int n_bytes_read;
445
446 /*
447 * Start with the first buffer in chain
448 */
449 b->error = 0;
450 b->flags = VNET_BUFFER_F_LOCALLY_ORIGINATED;
451 b->current_data = 0;
Florin Coras8e43d042018-05-04 15:46:57 -0700452
Florin Coras1ee78302019-02-05 15:51:15 -0800453 data0 = vlib_buffer_make_headroom (b, TRANSPORT_MAX_HDRS_LEN);
Florin Coras8e43d042018-05-04 15:46:57 -0700454 len_to_deq = clib_min (ctx->left_to_snd, ctx->deq_per_first_buf);
455
Florin Coras7fb0fe12018-04-09 09:24:52 -0700456 if (peek_data)
457 {
Florin Coras288eaab2019-02-03 15:26:14 -0800458 n_bytes_read = svm_fifo_peek (ctx->s->tx_fifo, ctx->tx_offset,
Florin Coras8e43d042018-05-04 15:46:57 -0700459 len_to_deq, data0);
460 ASSERT (n_bytes_read > 0);
461 /* Keep track of progress locally, transport is also supposed to
462 * increment it independently when pushing the header */
463 ctx->tx_offset += n_bytes_read;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700464 }
465 else
466 {
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200467 if (ctx->transport_vft->transport_options.tx_type == TRANSPORT_TX_DGRAM)
Florin Coras8e43d042018-05-04 15:46:57 -0700468 {
469 session_dgram_hdr_t *hdr = &ctx->hdr;
Florin Coras288eaab2019-02-03 15:26:14 -0800470 svm_fifo_t *f = ctx->s->tx_fifo;
Florin Coras8e43d042018-05-04 15:46:57 -0700471 u16 deq_now;
472 u32 offset;
473
474 ASSERT (hdr->data_length > hdr->data_offset);
475 deq_now = clib_min (hdr->data_length - hdr->data_offset,
476 len_to_deq);
477 offset = hdr->data_offset + SESSION_CONN_HDR_LEN;
478 n_bytes_read = svm_fifo_peek (f, offset, deq_now, data0);
479 ASSERT (n_bytes_read > 0);
480
481 if (ctx->s->session_state == SESSION_STATE_LISTENING)
482 {
483 ip_copy (&ctx->tc->rmt_ip, &hdr->rmt_ip, ctx->tc->is_ip4);
484 ctx->tc->rmt_port = hdr->rmt_port;
485 }
486 hdr->data_offset += n_bytes_read;
487 if (hdr->data_offset == hdr->data_length)
488 {
489 offset = hdr->data_length + SESSION_CONN_HDR_LEN;
490 svm_fifo_dequeue_drop (f, offset);
491 }
492 }
Florin Coras7fb0fe12018-04-09 09:24:52 -0700493 else
494 {
Florin Coras87b15ce2019-04-28 21:16:30 -0700495 n_bytes_read = svm_fifo_dequeue (ctx->s->tx_fifo,
496 len_to_deq, data0);
Florin Coras8e43d042018-05-04 15:46:57 -0700497 ASSERT (n_bytes_read > 0);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700498 }
499 }
Florin Coras8e43d042018-05-04 15:46:57 -0700500 b->current_length = n_bytes_read;
501 ctx->left_to_snd -= n_bytes_read;
Dave Barach68b0fb02017-02-28 15:15:56 -0500502
Florin Coras8e43d042018-05-04 15:46:57 -0700503 /*
504 * Fill in the remaining buffers in the chain, if any
505 */
506 if (PREDICT_FALSE (ctx->n_bufs_per_seg > 1 && ctx->left_to_snd))
507 session_tx_fifo_chain_tail (vm, ctx, b, n_bufs, peek_data);
Florin Coras8e43d042018-05-04 15:46:57 -0700508}
509
510always_inline u8
Florin Coras288eaab2019-02-03 15:26:14 -0800511session_tx_not_ready (session_t * s, u8 peek_data)
Florin Coras8e43d042018-05-04 15:46:57 -0700512{
513 if (peek_data)
Florin Corase04c2992017-03-01 08:17:34 -0800514 {
Florin Coras8e43d042018-05-04 15:46:57 -0700515 /* Can retransmit for closed sessions but can't send new data if
516 * session is not ready or closed */
517 if (s->session_state < SESSION_STATE_READY)
518 return 1;
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800519 if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSED)
Florin Corasca1c8f32018-05-23 21:01:30 -0700520 return 2;
Florin Corase04c2992017-03-01 08:17:34 -0800521 }
Florin Coras8e43d042018-05-04 15:46:57 -0700522 return 0;
523}
Dave Barach68b0fb02017-02-28 15:15:56 -0500524
Florin Coras8e43d042018-05-04 15:46:57 -0700525always_inline transport_connection_t *
526session_tx_get_transport (session_tx_context_t * ctx, u8 peek_data)
527{
528 if (peek_data)
529 {
530 return ctx->transport_vft->get_connection (ctx->s->connection_index,
531 ctx->s->thread_index);
532 }
533 else
534 {
535 if (ctx->s->session_state == SESSION_STATE_LISTENING)
536 return ctx->transport_vft->get_listener (ctx->s->connection_index);
537 else
538 {
539 return ctx->transport_vft->get_connection (ctx->s->connection_index,
540 ctx->s->thread_index);
541 }
542 }
543}
Florin Coras6a9b68b2017-11-21 04:20:42 -0800544
Florin Coras8e43d042018-05-04 15:46:57 -0700545always_inline void
546session_tx_set_dequeue_params (vlib_main_t * vm, session_tx_context_t * ctx,
Florin Corasf08f26d2018-05-10 13:20:47 -0700547 u32 max_segs, u8 peek_data)
Florin Coras8e43d042018-05-04 15:46:57 -0700548{
549 u32 n_bytes_per_buf, n_bytes_per_seg;
Sirshak Das28aa5392019-02-05 01:33:33 -0600550 ctx->max_dequeue = svm_fifo_max_dequeue_cons (ctx->s->tx_fifo);
Dave Barach68b0fb02017-02-28 15:15:56 -0500551 if (peek_data)
552 {
Florin Coras9d063042017-09-14 03:08:00 -0400553 /* Offset in rx fifo from where to peek data */
Florin Coras8e43d042018-05-04 15:46:57 -0700554 ctx->tx_offset = ctx->transport_vft->tx_fifo_offset (ctx->tc);
555 if (PREDICT_FALSE (ctx->tx_offset >= ctx->max_dequeue))
556 {
557 ctx->max_len_to_snd = 0;
558 return;
559 }
560 ctx->max_dequeue -= ctx->tx_offset;
Dave Barach68b0fb02017-02-28 15:15:56 -0500561 }
Florin Coras7fb0fe12018-04-09 09:24:52 -0700562 else
563 {
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200564 if (ctx->transport_vft->transport_options.tx_type == TRANSPORT_TX_DGRAM)
Florin Coras7fb0fe12018-04-09 09:24:52 -0700565 {
Florin Coras8e43d042018-05-04 15:46:57 -0700566 if (ctx->max_dequeue <= sizeof (ctx->hdr))
567 {
568 ctx->max_len_to_snd = 0;
569 return;
570 }
Florin Coras288eaab2019-02-03 15:26:14 -0800571 svm_fifo_peek (ctx->s->tx_fifo, 0, sizeof (ctx->hdr),
Florin Coras8e43d042018-05-04 15:46:57 -0700572 (u8 *) & ctx->hdr);
573 ASSERT (ctx->hdr.data_length > ctx->hdr.data_offset);
574 ctx->max_dequeue = ctx->hdr.data_length - ctx->hdr.data_offset;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700575 }
576 }
Florin Coras8e43d042018-05-04 15:46:57 -0700577 ASSERT (ctx->max_dequeue > 0);
Florin Coras6792ec02017-03-13 03:49:51 -0700578
579 /* Ensure we're not writing more than transport window allows */
Florin Coras8e43d042018-05-04 15:46:57 -0700580 if (ctx->max_dequeue < ctx->snd_space)
Florin Coras3e350af2017-03-30 02:54:28 -0700581 {
582 /* Constrained by tx queue. Try to send only fully formed segments */
Florin Coras8e43d042018-05-04 15:46:57 -0700583 ctx->max_len_to_snd =
584 (ctx->max_dequeue > ctx->snd_mss) ?
585 ctx->max_dequeue - ctx->max_dequeue % ctx->snd_mss : ctx->max_dequeue;
Florin Coras3e350af2017-03-30 02:54:28 -0700586 /* TODO Nagle ? */
587 }
588 else
589 {
Florin Coras1f152cd2017-08-18 19:28:03 -0700590 /* Expectation is that snd_space0 is already a multiple of snd_mss */
Florin Coras8e43d042018-05-04 15:46:57 -0700591 ctx->max_len_to_snd = ctx->snd_space;
Florin Coras3e350af2017-03-30 02:54:28 -0700592 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500593
Florin Corasf08f26d2018-05-10 13:20:47 -0700594 /* Check if we're tx constrained by the node */
595 ctx->n_segs_per_evt = ceil ((f64) ctx->max_len_to_snd / ctx->snd_mss);
596 if (ctx->n_segs_per_evt > max_segs)
597 {
598 ctx->n_segs_per_evt = max_segs;
599 ctx->max_len_to_snd = max_segs * ctx->snd_mss;
600 }
601
Damjan Marion8934a042019-02-09 23:29:26 +0100602 n_bytes_per_buf = vlib_buffer_get_default_data_size (vm);
Florin Coras1ee78302019-02-05 15:51:15 -0800603 ASSERT (n_bytes_per_buf > TRANSPORT_MAX_HDRS_LEN);
604 n_bytes_per_seg = TRANSPORT_MAX_HDRS_LEN + ctx->snd_mss;
Florin Corasf08f26d2018-05-10 13:20:47 -0700605 ctx->n_bufs_per_seg = ceil ((f64) n_bytes_per_seg / n_bytes_per_buf);
Florin Coras8e43d042018-05-04 15:46:57 -0700606 ctx->deq_per_buf = clib_min (ctx->snd_mss, n_bytes_per_buf);
607 ctx->deq_per_first_buf = clib_min (ctx->snd_mss,
Florin Coras1ee78302019-02-05 15:51:15 -0800608 n_bytes_per_buf -
609 TRANSPORT_MAX_HDRS_LEN);
Florin Coras8e43d042018-05-04 15:46:57 -0700610}
Florin Corasf6d68ed2017-05-07 19:12:02 -0700611
Florin Coras8e43d042018-05-04 15:46:57 -0700612always_inline int
Florin Coras60183db2019-07-20 15:53:16 -0700613session_tx_fifo_read_and_snd_i (session_worker_t * wrk,
614 vlib_node_runtime_t * node,
615 session_evt_elt_t * elt,
616 int *n_tx_packets, u8 peek_data)
Florin Coras8e43d042018-05-04 15:46:57 -0700617{
Florin Coras26dd6de2019-07-23 23:54:47 -0700618 u32 next_index, next0, next1, *to_next, n_left_to_next, max_burst;
Florin Coras60183db2019-07-20 15:53:16 -0700619 u32 n_trace, n_bufs_needed = 0, n_left, pbi;
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700620 session_tx_context_t *ctx = &wrk->ctx;
Florin Coras60183db2019-07-20 15:53:16 -0700621 session_main_t *smm = &session_main;
Florin Coras2062ec02019-07-15 13:15:18 -0700622 session_event_t *e = &elt->evt;
Florin Coras60183db2019-07-20 15:53:16 -0700623 vlib_main_t *vm = wrk->vm;
Florin Coras8e43d042018-05-04 15:46:57 -0700624 transport_proto_t tp;
625 vlib_buffer_t *pb;
Florin Corasca1c8f32018-05-23 21:01:30 -0700626 u16 n_bufs, rv;
Florin Coras8e43d042018-05-04 15:46:57 -0700627
Florin Coras1bcad5c2019-01-09 20:04:38 -0800628 if (PREDICT_FALSE ((rv = session_tx_not_ready (ctx->s, peek_data))))
Florin Coras8e43d042018-05-04 15:46:57 -0700629 {
Florin Corasca1c8f32018-05-23 21:01:30 -0700630 if (rv < 2)
Florin Coras60183db2019-07-20 15:53:16 -0700631 session_evt_add_old (wrk, elt);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700632 return SESSION_TX_NO_DATA;
Florin Coras8e43d042018-05-04 15:46:57 -0700633 }
634
Florin Coras1bcad5c2019-01-09 20:04:38 -0800635 next_index = smm->session_type_to_next[ctx->s->session_type];
Florin Coras66cf5e02018-06-08 09:17:39 -0700636 next0 = next1 = next_index;
Florin Coras26dd6de2019-07-23 23:54:47 -0700637 max_burst = VLIB_FRAME_SIZE - *n_tx_packets;
Florin Coras8e43d042018-05-04 15:46:57 -0700638
Florin Coras1bcad5c2019-01-09 20:04:38 -0800639 tp = session_get_transport_proto (ctx->s);
Florin Coras8e43d042018-05-04 15:46:57 -0700640 ctx->transport_vft = transport_protocol_get_vft (tp);
641 ctx->tc = session_tx_get_transport (ctx, peek_data);
642 ctx->snd_mss = ctx->transport_vft->send_mss (ctx->tc);
Florin Coras42ceddb2018-12-12 10:56:01 -0800643
644 if (PREDICT_FALSE (e->event_type == SESSION_IO_EVT_TX_FLUSH))
645 {
646 if (ctx->transport_vft->flush_data)
647 ctx->transport_vft->flush_data (ctx->tc);
648 }
649
Florin Coras26dd6de2019-07-23 23:54:47 -0700650 if (ctx->s->flags & SESSION_F_CUSTOM_TX)
651 {
652 u32 n_custom_tx;
653 ctx->s->flags &= ~SESSION_F_CUSTOM_TX;
654 n_custom_tx = ctx->transport_vft->custom_tx (ctx->tc, max_burst);
655 *n_tx_packets += n_custom_tx;
656 max_burst -= n_custom_tx;
657 if (!max_burst)
658 {
659 session_evt_add_old (wrk, elt);
660 return SESSION_TX_OK;
661 }
662 }
663
Florin Coras42ceddb2018-12-12 10:56:01 -0800664 ctx->snd_space = transport_connection_snd_space (ctx->tc,
Florin Corasb0ffbee2019-07-21 19:23:46 -0700665 vm->clib_time.
David Johnsond9818dd2018-12-14 14:53:41 -0500666 last_cpu_time,
Florin Coras42ceddb2018-12-12 10:56:01 -0800667 ctx->snd_mss);
Florin Coras60183db2019-07-20 15:53:16 -0700668
Florin Coras8e43d042018-05-04 15:46:57 -0700669 if (ctx->snd_space == 0 || ctx->snd_mss == 0)
670 {
Florin Coras60183db2019-07-20 15:53:16 -0700671 session_evt_add_old (wrk, elt);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700672 return SESSION_TX_NO_DATA;
Florin Coras8e43d042018-05-04 15:46:57 -0700673 }
674
675 /* Allow enqueuing of a new event */
Florin Coras288eaab2019-02-03 15:26:14 -0800676 svm_fifo_unset_event (ctx->s->tx_fifo);
Florin Coras8e43d042018-05-04 15:46:57 -0700677
678 /* Check how much we can pull. */
Florin Coras26dd6de2019-07-23 23:54:47 -0700679 session_tx_set_dequeue_params (vm, ctx, max_burst, peek_data);
Florin Corasf08f26d2018-05-10 13:20:47 -0700680
Florin Coras8e43d042018-05-04 15:46:57 -0700681 if (PREDICT_FALSE (!ctx->max_len_to_snd))
Florin Coras0d60a0f2018-06-29 02:02:08 -0700682 return SESSION_TX_NO_DATA;
Dave Barach68b0fb02017-02-28 15:15:56 -0500683
Florin Corasf08f26d2018-05-10 13:20:47 -0700684 n_bufs_needed = ctx->n_segs_per_evt * ctx->n_bufs_per_seg;
Florin Coras2068fd42019-01-31 14:35:50 -0800685 vec_validate_aligned (wrk->tx_buffers, n_bufs_needed - 1,
686 CLIB_CACHE_LINE_BYTES);
687 n_bufs = vlib_buffer_alloc (vm, wrk->tx_buffers, n_bufs_needed);
688 if (PREDICT_FALSE (n_bufs < n_bufs_needed))
Dave Barach68b0fb02017-02-28 15:15:56 -0500689 {
Florin Coras2068fd42019-01-31 14:35:50 -0800690 if (n_bufs)
691 vlib_buffer_free (vm, wrk->tx_buffers, n_bufs);
Florin Coras60183db2019-07-20 15:53:16 -0700692 session_evt_add_old (wrk, elt);
Florin Corasb0ffbee2019-07-21 19:23:46 -0700693 vlib_node_increment_counter (wrk->vm, node->node_index,
694 SESSION_QUEUE_ERROR_NO_BUFFER, 1);
Florin Coras2068fd42019-01-31 14:35:50 -0800695 return SESSION_TX_NO_BUFFERS;
Florin Coras8e43d042018-05-04 15:46:57 -0700696 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500697
Florin Coras8e43d042018-05-04 15:46:57 -0700698 /*
699 * Write until we fill up a frame
700 */
701 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Florin Corasf08f26d2018-05-10 13:20:47 -0700702 if (PREDICT_FALSE (ctx->n_segs_per_evt > n_left_to_next))
Florin Coras8e43d042018-05-04 15:46:57 -0700703 {
Florin Corasf08f26d2018-05-10 13:20:47 -0700704 ctx->n_segs_per_evt = n_left_to_next;
705 ctx->max_len_to_snd = ctx->snd_mss * n_left_to_next;
706 }
707 ctx->left_to_snd = ctx->max_len_to_snd;
708 n_left = ctx->n_segs_per_evt;
Florin Coras66cf5e02018-06-08 09:17:39 -0700709
710 while (n_left >= 4)
711 {
712 vlib_buffer_t *b0, *b1;
713 u32 bi0, bi1;
714
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700715 pbi = wrk->tx_buffers[n_bufs - 3];
Florin Coras66cf5e02018-06-08 09:17:39 -0700716 pb = vlib_get_buffer (vm, pbi);
717 vlib_prefetch_buffer_header (pb, STORE);
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700718 pbi = wrk->tx_buffers[n_bufs - 4];
Florin Coras66cf5e02018-06-08 09:17:39 -0700719 pb = vlib_get_buffer (vm, pbi);
720 vlib_prefetch_buffer_header (pb, STORE);
721
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700722 to_next[0] = bi0 = wrk->tx_buffers[--n_bufs];
723 to_next[1] = bi1 = wrk->tx_buffers[--n_bufs];
Florin Coras66cf5e02018-06-08 09:17:39 -0700724
725 b0 = vlib_get_buffer (vm, bi0);
726 b1 = vlib_get_buffer (vm, bi1);
727
728 session_tx_fill_buffer (vm, ctx, b0, &n_bufs, peek_data);
729 session_tx_fill_buffer (vm, ctx, b1, &n_bufs, peek_data);
730
731 ctx->transport_vft->push_header (ctx->tc, b0);
732 ctx->transport_vft->push_header (ctx->tc, b1);
733
734 to_next += 2;
735 n_left_to_next -= 2;
736 n_left -= 2;
737
738 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
739 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b1);
740
741 vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
742 n_left_to_next, bi0, bi1, next0,
743 next1);
744 }
Florin Corasf08f26d2018-05-10 13:20:47 -0700745 while (n_left)
746 {
Florin Coras66cf5e02018-06-08 09:17:39 -0700747 vlib_buffer_t *b0;
748 u32 bi0;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700749
Florin Coras91ca4622018-06-30 11:27:59 -0700750 if (n_left > 1)
751 {
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700752 pbi = wrk->tx_buffers[n_bufs - 2];
Florin Coras91ca4622018-06-30 11:27:59 -0700753 pb = vlib_get_buffer (vm, pbi);
754 vlib_prefetch_buffer_header (pb, STORE);
755 }
756
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700757 to_next[0] = bi0 = wrk->tx_buffers[--n_bufs];
Florin Coras66cf5e02018-06-08 09:17:39 -0700758 b0 = vlib_get_buffer (vm, bi0);
759 session_tx_fill_buffer (vm, ctx, b0, &n_bufs, peek_data);
Florin Coras81a13db2018-03-16 08:48:31 -0700760
Florin Coras66cf5e02018-06-08 09:17:39 -0700761 /* Ask transport to push header after current_length and
762 * total_length_not_including_first_buffer are updated */
763 ctx->transport_vft->push_header (ctx->tc, b0);
Florin Corasf6359c82017-06-19 12:26:09 -0400764
Florin Coras66cf5e02018-06-08 09:17:39 -0700765 to_next += 1;
766 n_left_to_next -= 1;
767 n_left -= 1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500768
Florin Coras66cf5e02018-06-08 09:17:39 -0700769 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700770
Florin Coras66cf5e02018-06-08 09:17:39 -0700771 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
772 n_left_to_next, bi0, next0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500773 }
Florin Coras66cf5e02018-06-08 09:17:39 -0700774
Florin Coras60183db2019-07-20 15:53:16 -0700775 if (PREDICT_FALSE ((n_trace = vlib_get_trace_count (vm, node)) > 0))
Florin Coras66cf5e02018-06-08 09:17:39 -0700776 session_tx_trace_frame (vm, node, next_index, to_next,
Florin Coras1bcad5c2019-01-09 20:04:38 -0800777 ctx->n_segs_per_evt, ctx->s, n_trace);
Florin Coras60183db2019-07-20 15:53:16 -0700778
Florin Coras2068fd42019-01-31 14:35:50 -0800779 if (PREDICT_FALSE (n_bufs))
Florin Coras60183db2019-07-20 15:53:16 -0700780 vlib_buffer_free (vm, wrk->tx_buffers, n_bufs);
781
Florin Corasf08f26d2018-05-10 13:20:47 -0700782 *n_tx_packets += ctx->n_segs_per_evt;
Florin Corasd67f1122018-05-21 17:47:40 -0700783 transport_connection_update_tx_stats (ctx->tc, ctx->max_len_to_snd);
Florin Coras8e43d042018-05-04 15:46:57 -0700784 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
Dave Barach68b0fb02017-02-28 15:15:56 -0500785
Florin Corascca96182019-07-19 07:34:13 -0700786 SESSION_EVT (SESSION_EVT_DEQ, ctx->s, ctx->max_len_to_snd, ctx->max_dequeue,
787 ctx->s->tx_fifo->has_event, wrk->last_vlib_time);
788
Florin Coras6792ec02017-03-13 03:49:51 -0700789 /* If we couldn't dequeue all bytes mark as partially read */
Florin Corasf08f26d2018-05-10 13:20:47 -0700790 ASSERT (ctx->left_to_snd == 0);
Florin Coras8e43d042018-05-04 15:46:57 -0700791 if (ctx->max_len_to_snd < ctx->max_dequeue)
Florin Coras288eaab2019-02-03 15:26:14 -0800792 if (svm_fifo_set_event (ctx->s->tx_fifo))
Florin Coras60183db2019-07-20 15:53:16 -0700793 session_evt_add_old (wrk, elt);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700794
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200795 if (!peek_data
796 && ctx->transport_vft->transport_options.tx_type == TRANSPORT_TX_DGRAM)
Dave Barach68b0fb02017-02-28 15:15:56 -0500797 {
Florin Coras7fb0fe12018-04-09 09:24:52 -0700798 /* Fix dgram pre header */
Florin Coras8e43d042018-05-04 15:46:57 -0700799 if (ctx->max_len_to_snd < ctx->max_dequeue)
Florin Coras288eaab2019-02-03 15:26:14 -0800800 svm_fifo_overwrite_head (ctx->s->tx_fifo, (u8 *) & ctx->hdr,
Florin Coras7fb0fe12018-04-09 09:24:52 -0700801 sizeof (session_dgram_pre_hdr_t));
802 /* More data needs to be read */
Sirshak Das28aa5392019-02-05 01:33:33 -0600803 else if (svm_fifo_max_dequeue_cons (ctx->s->tx_fifo) > 0)
Florin Coras288eaab2019-02-03 15:26:14 -0800804 if (svm_fifo_set_event (ctx->s->tx_fifo))
Florin Coras60183db2019-07-20 15:53:16 -0700805 session_evt_add_old (wrk, elt);
Florin Coras0e573f52019-05-07 16:28:16 -0700806
Florin Coras2d379d82019-06-28 12:45:12 -0700807 if (svm_fifo_needs_deq_ntf (ctx->s->tx_fifo, ctx->max_len_to_snd))
Florin Coras0e573f52019-05-07 16:28:16 -0700808 session_dequeue_notify (ctx->s);
Dave Barach68b0fb02017-02-28 15:15:56 -0500809 }
Florin Coras0d60a0f2018-06-29 02:02:08 -0700810 return SESSION_TX_OK;
Dave Barach68b0fb02017-02-28 15:15:56 -0500811}
812
813int
Florin Coras60183db2019-07-20 15:53:16 -0700814session_tx_fifo_peek_and_snd (session_worker_t * wrk,
815 vlib_node_runtime_t * node,
816 session_evt_elt_t * e, int *n_tx_packets)
Dave Barach68b0fb02017-02-28 15:15:56 -0500817{
Florin Coras60183db2019-07-20 15:53:16 -0700818 return session_tx_fifo_read_and_snd_i (wrk, node, e, n_tx_packets, 1);
Dave Barach68b0fb02017-02-28 15:15:56 -0500819}
820
821int
Florin Coras60183db2019-07-20 15:53:16 -0700822session_tx_fifo_dequeue_and_snd (session_worker_t * wrk,
823 vlib_node_runtime_t * node,
824 session_evt_elt_t * e, int *n_tx_packets)
Dave Barach68b0fb02017-02-28 15:15:56 -0500825{
Florin Coras60183db2019-07-20 15:53:16 -0700826 return session_tx_fifo_read_and_snd_i (wrk, node, e, n_tx_packets, 0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500827}
828
Florin Coras371ca502018-02-21 12:07:41 -0800829int
Florin Coras60183db2019-07-20 15:53:16 -0700830session_tx_fifo_dequeue_internal (session_worker_t * wrk,
Florin Coras371ca502018-02-21 12:07:41 -0800831 vlib_node_runtime_t * node,
Florin Coras60183db2019-07-20 15:53:16 -0700832 session_evt_elt_t * e, int *n_tx_packets)
Florin Coras371ca502018-02-21 12:07:41 -0800833{
Florin Coras288eaab2019-02-03 15:26:14 -0800834 session_t *s = wrk->ctx.s;
Florin Coras1bcad5c2019-01-09 20:04:38 -0800835
Florin Corasc0737e92019-03-04 14:19:39 -0800836 if (PREDICT_FALSE (s->session_state >= SESSION_STATE_TRANSPORT_CLOSED))
Florin Corasef915342018-09-29 10:23:06 -0700837 return 0;
Florin Coras288eaab2019-02-03 15:26:14 -0800838 svm_fifo_unset_event (s->tx_fifo);
Florin Coras26dd6de2019-07-23 23:54:47 -0700839 return transport_custom_tx (session_get_transport_proto (s), s,
840 VLIB_FRAME_SIZE - *n_tx_packets);
Florin Coras371ca502018-02-21 12:07:41 -0800841}
842
Florin Coras288eaab2019-02-03 15:26:14 -0800843always_inline session_t *
Florin Coras52207f12018-07-12 14:48:06 -0700844session_event_get_session (session_event_t * e, u8 thread_index)
Florin Corasa5464812017-04-19 13:00:05 -0700845{
Florin Corasc0737e92019-03-04 14:19:39 -0800846 return session_get_if_valid (e->session_index, thread_index);
Florin Corasa5464812017-04-19 13:00:05 -0700847}
848
Florin Coras60183db2019-07-20 15:53:16 -0700849always_inline void
850session_event_dispatch (session_worker_t * wrk, vlib_node_runtime_t * node,
851 session_evt_elt_t * elt, u32 thread_index,
852 int *n_tx_packets)
Florin Corasd67f1122018-05-21 17:47:40 -0700853{
Florin Coras60183db2019-07-20 15:53:16 -0700854 session_main_t *smm = &session_main;
855 app_worker_t *app_wrk;
Florin Corasb0ffbee2019-07-21 19:23:46 -0700856 clib_llist_index_t ei;
Florin Coras60183db2019-07-20 15:53:16 -0700857 void (*fp) (void *);
858 session_event_t *e;
859 session_t *s;
Florin Coras60183db2019-07-20 15:53:16 -0700860
Florin Corasb0ffbee2019-07-21 19:23:46 -0700861 ei = clib_llist_entry_index (wrk->event_elts, elt);
Florin Coras60183db2019-07-20 15:53:16 -0700862 e = &elt->evt;
Florin Corasb0ffbee2019-07-21 19:23:46 -0700863
Florin Coras60183db2019-07-20 15:53:16 -0700864 switch (e->event_type)
Florin Corasc44a5582018-11-01 16:30:54 -0700865 {
Florin Coras60183db2019-07-20 15:53:16 -0700866 case SESSION_IO_EVT_TX_FLUSH:
867 case SESSION_IO_EVT_TX:
Florin Coras60183db2019-07-20 15:53:16 -0700868 s = session_event_get_session (e, thread_index);
869 if (PREDICT_FALSE (!s))
870 {
Florin Corasb0ffbee2019-07-21 19:23:46 -0700871 clib_warning ("session %u was freed!", e->session_index);
Florin Coras60183db2019-07-20 15:53:16 -0700872 break;
873 }
874 CLIB_PREFETCH (s->tx_fifo, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
875 wrk->ctx.s = s;
876 /* Spray packets in per session type frames, since they go to
877 * different nodes */
Florin Corasb0ffbee2019-07-21 19:23:46 -0700878 (smm->session_tx_fns[s->session_type]) (wrk, node, elt, n_tx_packets);
Florin Coras60183db2019-07-20 15:53:16 -0700879 break;
880 case SESSION_IO_EVT_RX:
881 s = session_event_get_session (e, thread_index);
882 if (!s)
883 break;
884 transport_app_rx_evt (session_get_transport_proto (s),
885 s->connection_index, s->thread_index);
886 break;
887 case SESSION_CTRL_EVT_CLOSE:
888 s = session_get_from_handle_if_valid (e->session_handle);
889 if (PREDICT_FALSE (!s))
890 break;
891 session_transport_close (s);
892 break;
893 case SESSION_IO_EVT_BUILTIN_RX:
894 s = session_event_get_session (e, thread_index);
895 if (PREDICT_FALSE (!s || s->session_state >= SESSION_STATE_CLOSING))
896 break;
897 svm_fifo_unset_event (s->rx_fifo);
898 app_wrk = app_worker_get (s->app_wrk_index);
899 app_worker_builtin_rx (app_wrk, s);
900 break;
901 case SESSION_IO_EVT_BUILTIN_TX:
902 s = session_get_from_handle_if_valid (e->session_handle);
903 wrk->ctx.s = s;
904 if (PREDICT_TRUE (s != 0))
905 session_tx_fifo_dequeue_internal (wrk, node, elt, n_tx_packets);
906 break;
907 case SESSION_CTRL_EVT_RPC:
908 fp = e->rpc_args.fp;
909 (*fp) (e->rpc_args.arg);
910 break;
911 case SESSION_CTRL_EVT_DISCONNECTED:
912 session_mq_disconnected_handler (e->data);
913 break;
914 case SESSION_CTRL_EVT_ACCEPTED_REPLY:
915 session_mq_accepted_reply_handler (e->data);
916 break;
917 case SESSION_CTRL_EVT_CONNECTED_REPLY:
918 break;
919 case SESSION_CTRL_EVT_DISCONNECTED_REPLY:
920 session_mq_disconnected_reply_handler (e->data);
921 break;
922 case SESSION_CTRL_EVT_RESET_REPLY:
923 session_mq_reset_reply_handler (e->data);
924 break;
925 case SESSION_CTRL_EVT_WORKER_UPDATE:
926 session_mq_worker_update_handler (e->data);
927 break;
928 default:
929 clib_warning ("unhandled event type %d", e->event_type);
Florin Corasc44a5582018-11-01 16:30:54 -0700930 }
Florin Corasb0ffbee2019-07-21 19:23:46 -0700931
932 /* Regrab elements in case pool moved */
933 elt = pool_elt_at_index (wrk->event_elts, ei);
934 if (!clib_llist_elt_is_linked (elt, evt_list))
935 session_evt_elt_free (wrk, elt);
Florin Corasd67f1122018-05-21 17:47:40 -0700936}
937
Florin Coras0d60a0f2018-06-29 02:02:08 -0700938static uword
939session_queue_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
940 vlib_frame_t * frame)
941{
Florin Coras31c99552019-03-01 13:00:58 -0800942 session_main_t *smm = vnet_get_session_main ();
Florin Coras2062ec02019-07-15 13:15:18 -0700943 u32 thread_index = vm->thread_index, n_to_dequeue;
Florin Coras31c99552019-03-01 13:00:58 -0800944 session_worker_t *wrk = &smm->wrk[thread_index];
Florin Corasb0ffbee2019-07-21 19:23:46 -0700945 session_evt_elt_t *elt, *ctrl_he, *new_he, *old_he;
Florin Coras3c2fed52018-07-04 04:15:05 -0700946 svm_msg_q_msg_t _msg, *msg = &_msg;
Florin Corasb0ffbee2019-07-21 19:23:46 -0700947 clib_llist_index_t old_ti;
Florin Coras60183db2019-07-20 15:53:16 -0700948 int i, n_tx_packets = 0;
Florin Corasb0ffbee2019-07-21 19:23:46 -0700949 session_event_t *evt;
Florin Coras3c2fed52018-07-04 04:15:05 -0700950 svm_msg_q_t *mq;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700951
Florin Corascca96182019-07-19 07:34:13 -0700952 SESSION_EVT (SESSION_EVT_DISPATCH_START, wrk);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700953
Florin Coras60183db2019-07-20 15:53:16 -0700954 wrk->last_vlib_time = vlib_time_now (vm);
955
Florin Coras0d60a0f2018-06-29 02:02:08 -0700956 /*
957 * Update transport time
958 */
Florin Coras60183db2019-07-20 15:53:16 -0700959 transport_update_time (wrk->last_vlib_time, thread_index);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700960
Florin Corasb0ffbee2019-07-21 19:23:46 -0700961 /*
962 * Dequeue and handle new events
963 */
Florin Coras0d60a0f2018-06-29 02:02:08 -0700964
Florin Coras5f56d732018-10-30 10:21:59 -0700965 /* Try to dequeue what is available. Don't wait for lock.
966 * XXX: we may need priorities here */
967 mq = wrk->vpp_event_queue;
968 n_to_dequeue = svm_msg_q_size (mq);
969 if (n_to_dequeue && svm_msg_q_try_lock (mq) == 0)
970 {
971 for (i = 0; i < n_to_dequeue; i++)
972 {
Florin Coras5f56d732018-10-30 10:21:59 -0700973 svm_msg_q_sub_w_lock (mq, msg);
Florin Corasb0ffbee2019-07-21 19:23:46 -0700974 evt = svm_msg_q_msg_data (mq, msg);
975 if (evt->event_type > SESSION_IO_EVT_BUILTIN_TX)
976 elt = session_evt_alloc_ctrl (wrk);
977 else
978 elt = session_evt_alloc_new (wrk);
Florin Coras3c7d4f92018-12-14 11:28:43 -0800979 /* Works because reply messages are smaller than a session evt.
980 * If we ever need to support bigger messages this needs to be
981 * fixed */
Florin Corasb0ffbee2019-07-21 19:23:46 -0700982 clib_memcpy_fast (&elt->evt, evt, sizeof (elt->evt));
Florin Coras5f56d732018-10-30 10:21:59 -0700983 svm_msg_q_free_msg (mq, msg);
984 }
985 svm_msg_q_unlock (mq);
986 }
987
Florin Corasb0ffbee2019-07-21 19:23:46 -0700988 /*
989 * Handle control events
990 */
991
992 ctrl_he = pool_elt_at_index (wrk->event_elts, wrk->ctrl_head);
993
994 /* *INDENT-OFF* */
995 clib_llist_foreach_safe (wrk->event_elts, evt_list, ctrl_he, elt, ({
996 clib_llist_remove (wrk->event_elts, evt_list, elt);
997 session_event_dispatch (wrk, node, elt, thread_index, &n_tx_packets);
998 }));
999 /* *INDENT-ON* */
1000
1001 /*
1002 * Handle the new io events.
1003 */
1004
1005 new_he = pool_elt_at_index (wrk->event_elts, wrk->new_head);
1006
1007 /* *INDENT-OFF* */
1008 clib_llist_foreach_safe (wrk->event_elts, evt_list, new_he, elt, ({
1009 session_evt_type_t et;
1010
1011 et = elt->evt.event_type;
1012 clib_llist_remove (wrk->event_elts, evt_list, elt);
1013
1014 /* Postpone tx events if we can't handle them this dispatch cycle */
1015 if (n_tx_packets >= VLIB_FRAME_SIZE
1016 && (et == SESSION_IO_EVT_TX || et == SESSION_IO_EVT_TX_FLUSH))
1017 {
1018 clib_llist_add (wrk->event_elts, evt_list, elt, new_he);
1019 continue;
1020 }
1021
1022 session_event_dispatch (wrk, node, elt, thread_index, &n_tx_packets);
1023 }));
1024 /* *INDENT-ON* */
1025
1026 /*
1027 * Handle the old io events
1028 */
1029
Florin Coras60183db2019-07-20 15:53:16 -07001030 old_he = pool_elt_at_index (wrk->event_elts, wrk->old_head);
Florin Corasb0ffbee2019-07-21 19:23:46 -07001031 old_ti = clib_llist_prev_index (old_he, evt_list);
Florin Coras5f56d732018-10-30 10:21:59 -07001032
Florin Corasb0ffbee2019-07-21 19:23:46 -07001033 while (!clib_llist_is_empty (wrk->event_elts, evt_list, old_he))
Florin Coras0d60a0f2018-06-29 02:02:08 -07001034 {
Florin Coras2062ec02019-07-15 13:15:18 -07001035 clib_llist_index_t ei;
Florin Coras0d60a0f2018-06-29 02:02:08 -07001036
Florin Corasb0ffbee2019-07-21 19:23:46 -07001037 clib_llist_pop_first (wrk->event_elts, evt_list, elt, old_he);
Florin Coras2062ec02019-07-15 13:15:18 -07001038 ei = clib_llist_entry_index (wrk->event_elts, elt);
Florin Coras60183db2019-07-20 15:53:16 -07001039 session_event_dispatch (wrk, node, elt, thread_index, &n_tx_packets);
Florin Coras2062ec02019-07-15 13:15:18 -07001040
Florin Corasb0ffbee2019-07-21 19:23:46 -07001041 old_he = pool_elt_at_index (wrk->event_elts, wrk->old_head);
1042 if (n_tx_packets >= VLIB_FRAME_SIZE || ei == old_ti)
1043 break;
1044 };
Florin Coras0d60a0f2018-06-29 02:02:08 -07001045
Florin Coras0d60a0f2018-06-29 02:02:08 -07001046 vlib_node_increment_counter (vm, session_queue_node.index,
1047 SESSION_QUEUE_ERROR_TX, n_tx_packets);
1048
Florin Corascca96182019-07-19 07:34:13 -07001049 SESSION_EVT (SESSION_EVT_DISPATCH_END, wrk, n_tx_packets);
Florin Coras0d60a0f2018-06-29 02:02:08 -07001050
1051 return n_tx_packets;
1052}
1053
1054/* *INDENT-OFF* */
1055VLIB_REGISTER_NODE (session_queue_node) =
1056{
1057 .function = session_queue_node_fn,
1058 .name = "session-queue",
1059 .format_trace = format_session_queue_trace,
1060 .type = VLIB_NODE_TYPE_INPUT,
1061 .n_errors = ARRAY_LEN (session_queue_error_strings),
1062 .error_strings = session_queue_error_strings,
1063 .state = VLIB_NODE_STATE_DISABLED,
1064};
1065/* *INDENT-ON* */
1066
Dave Barachacd2a6a2017-05-16 17:41:34 -04001067void
1068dump_thread_0_event_queue (void)
1069{
Florin Coras31c99552019-03-01 13:00:58 -08001070 session_main_t *smm = vnet_get_session_main ();
Dave Barachacd2a6a2017-05-16 17:41:34 -04001071 vlib_main_t *vm = &vlib_global_main;
1072 u32 my_thread_index = vm->thread_index;
Florin Coras52207f12018-07-12 14:48:06 -07001073 session_event_t _e, *e = &_e;
Florin Coras3c2fed52018-07-04 04:15:05 -07001074 svm_msg_q_ring_t *ring;
Florin Coras288eaab2019-02-03 15:26:14 -08001075 session_t *s0;
Florin Coras3c2fed52018-07-04 04:15:05 -07001076 svm_msg_q_msg_t *msg;
1077 svm_msg_q_t *mq;
Dave Barachacd2a6a2017-05-16 17:41:34 -04001078 int i, index;
Dave Barachacd2a6a2017-05-16 17:41:34 -04001079
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001080 mq = smm->wrk[my_thread_index].vpp_event_queue;
Florin Coras3c2fed52018-07-04 04:15:05 -07001081 index = mq->q->head;
Dave Barachacd2a6a2017-05-16 17:41:34 -04001082
Florin Coras3c2fed52018-07-04 04:15:05 -07001083 for (i = 0; i < mq->q->cursize; i++)
Dave Barachacd2a6a2017-05-16 17:41:34 -04001084 {
Florin Coras3c2fed52018-07-04 04:15:05 -07001085 msg = (svm_msg_q_msg_t *) (&mq->q->data[0] + mq->q->elsize * index);
1086 ring = svm_msg_q_ring (mq, msg->ring_index);
Dave Barach178cf492018-11-13 16:34:13 -05001087 clib_memcpy_fast (e, svm_msg_q_msg_data (mq, msg), ring->elsize);
Dave Barachacd2a6a2017-05-16 17:41:34 -04001088
1089 switch (e->event_type)
1090 {
Florin Corasf6c43132019-03-01 12:41:21 -08001091 case SESSION_IO_EVT_TX:
Dave Barachacd2a6a2017-05-16 17:41:34 -04001092 s0 = session_event_get_session (e, my_thread_index);
1093 fformat (stdout, "[%04d] TX session %d\n", i, s0->session_index);
1094 break;
1095
Florin Corasf6c43132019-03-01 12:41:21 -08001096 case SESSION_CTRL_EVT_CLOSE:
Florin Corascea194d2017-10-02 00:18:51 -07001097 s0 = session_get_from_handle (e->session_handle);
Dave Barachacd2a6a2017-05-16 17:41:34 -04001098 fformat (stdout, "[%04d] disconnect session %d\n", i,
1099 s0->session_index);
1100 break;
1101
Florin Corasf6c43132019-03-01 12:41:21 -08001102 case SESSION_IO_EVT_BUILTIN_RX:
Dave Barachacd2a6a2017-05-16 17:41:34 -04001103 s0 = session_event_get_session (e, my_thread_index);
1104 fformat (stdout, "[%04d] builtin_rx %d\n", i, s0->session_index);
1105 break;
1106
Florin Corasf6c43132019-03-01 12:41:21 -08001107 case SESSION_CTRL_EVT_RPC:
Dave Barachacd2a6a2017-05-16 17:41:34 -04001108 fformat (stdout, "[%04d] RPC call %llx with %llx\n",
David Johnsond9818dd2018-12-14 14:53:41 -05001109 i, (u64) (uword) (e->rpc_args.fp),
1110 (u64) (uword) (e->rpc_args.arg));
Dave Barachacd2a6a2017-05-16 17:41:34 -04001111 break;
1112
1113 default:
1114 fformat (stdout, "[%04d] unhandled event type %d\n",
1115 i, e->event_type);
1116 break;
1117 }
1118
1119 index++;
1120
Florin Coras3c2fed52018-07-04 04:15:05 -07001121 if (index == mq->q->maxsize)
Dave Barachacd2a6a2017-05-16 17:41:34 -04001122 index = 0;
1123 }
1124}
1125
Florin Coras6534b7a2017-07-18 05:38:03 -04001126static u8
Florin Coras52207f12018-07-12 14:48:06 -07001127session_node_cmp_event (session_event_t * e, svm_fifo_t * f)
Florin Coras6534b7a2017-07-18 05:38:03 -04001128{
Florin Coras288eaab2019-02-03 15:26:14 -08001129 session_t *s;
Florin Coras6534b7a2017-07-18 05:38:03 -04001130 switch (e->event_type)
1131 {
Florin Corasf6c43132019-03-01 12:41:21 -08001132 case SESSION_IO_EVT_RX:
1133 case SESSION_IO_EVT_TX:
1134 case SESSION_IO_EVT_BUILTIN_RX:
Florin Corasc0737e92019-03-04 14:19:39 -08001135 if (e->session_index == f->master_session_index)
Florin Coras6534b7a2017-07-18 05:38:03 -04001136 return 1;
1137 break;
Florin Corasf6c43132019-03-01 12:41:21 -08001138 case SESSION_CTRL_EVT_CLOSE:
Florin Coras6534b7a2017-07-18 05:38:03 -04001139 break;
Florin Corasf6c43132019-03-01 12:41:21 -08001140 case SESSION_CTRL_EVT_RPC:
Florin Corascea194d2017-10-02 00:18:51 -07001141 s = session_get_from_handle (e->session_handle);
Florin Coras6534b7a2017-07-18 05:38:03 -04001142 if (!s)
1143 {
1144 clib_warning ("session has event but doesn't exist!");
1145 break;
1146 }
Florin Coras288eaab2019-02-03 15:26:14 -08001147 if (s->rx_fifo == f || s->tx_fifo == f)
Florin Coras6534b7a2017-07-18 05:38:03 -04001148 return 1;
1149 break;
1150 default:
1151 break;
1152 }
1153 return 0;
1154}
1155
1156u8
Florin Coras52207f12018-07-12 14:48:06 -07001157session_node_lookup_fifo_event (svm_fifo_t * f, session_event_t * e)
Florin Coras6534b7a2017-07-18 05:38:03 -04001158{
Florin Coras2062ec02019-07-15 13:15:18 -07001159 session_evt_elt_t *elt;
Florin Coras31c99552019-03-01 13:00:58 -08001160 session_worker_t *wrk;
Florin Coras6534b7a2017-07-18 05:38:03 -04001161 int i, index, found = 0;
Florin Coras3c2fed52018-07-04 04:15:05 -07001162 svm_msg_q_msg_t *msg;
1163 svm_msg_q_ring_t *ring;
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001164 svm_msg_q_t *mq;
Florin Coras6534b7a2017-07-18 05:38:03 -04001165 u8 thread_index;
1166
1167 ASSERT (e);
1168 thread_index = f->master_thread_index;
Florin Coras31c99552019-03-01 13:00:58 -08001169 wrk = session_main_get_worker (thread_index);
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001170
Florin Coras6534b7a2017-07-18 05:38:03 -04001171 /*
1172 * Search evt queue
1173 */
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001174 mq = wrk->vpp_event_queue;
Florin Coras3c2fed52018-07-04 04:15:05 -07001175 index = mq->q->head;
1176 for (i = 0; i < mq->q->cursize; i++)
Florin Coras6534b7a2017-07-18 05:38:03 -04001177 {
Florin Coras3c2fed52018-07-04 04:15:05 -07001178 msg = (svm_msg_q_msg_t *) (&mq->q->data[0] + mq->q->elsize * index);
1179 ring = svm_msg_q_ring (mq, msg->ring_index);
Dave Barach178cf492018-11-13 16:34:13 -05001180 clib_memcpy_fast (e, svm_msg_q_msg_data (mq, msg), ring->elsize);
Florin Coras6534b7a2017-07-18 05:38:03 -04001181 found = session_node_cmp_event (e, f);
1182 if (found)
Florin Coras371ca502018-02-21 12:07:41 -08001183 return 1;
Florin Coras3c2fed52018-07-04 04:15:05 -07001184 if (++index == mq->q->maxsize)
Florin Coras6534b7a2017-07-18 05:38:03 -04001185 index = 0;
1186 }
1187 /*
1188 * Search pending events vector
1189 */
Florin Coras2062ec02019-07-15 13:15:18 -07001190
1191 /* *INDENT-OFF* */
1192 clib_llist_foreach (wrk->event_elts, evt_list,
Florin Corasb0ffbee2019-07-21 19:23:46 -07001193 pool_elt_at_index (wrk->event_elts, wrk->old_head),
1194 elt, ({
Florin Coras2062ec02019-07-15 13:15:18 -07001195 found = session_node_cmp_event (&elt->evt, f);
Florin Coras6534b7a2017-07-18 05:38:03 -04001196 if (found)
1197 {
Florin Coras2062ec02019-07-15 13:15:18 -07001198 clib_memcpy_fast (e, &elt->evt, sizeof (*e));
Florin Coras6534b7a2017-07-18 05:38:03 -04001199 break;
1200 }
Florin Coras2062ec02019-07-15 13:15:18 -07001201
1202 }));
1203 /* *INDENT-ON* */
1204
Florin Coras6534b7a2017-07-18 05:38:03 -04001205 return found;
1206}
1207
Dave Barach2a863912017-11-28 10:11:42 -05001208static clib_error_t *
1209session_queue_exit (vlib_main_t * vm)
1210{
1211 if (vec_len (vlib_mains) < 2)
1212 return 0;
1213
1214 /*
1215 * Shut off (especially) worker-thread session nodes.
1216 * Otherwise, vpp can crash as the main thread unmaps the
1217 * API segment.
1218 */
1219 vlib_worker_thread_barrier_sync (vm);
1220 session_node_enable_disable (0 /* is_enable */ );
1221 vlib_worker_thread_barrier_release (vm);
1222 return 0;
1223}
1224
1225VLIB_MAIN_LOOP_EXIT_FUNCTION (session_queue_exit);
1226
Florin Corasfd542f12018-05-16 19:28:24 -07001227static uword
1228session_queue_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
1229 vlib_frame_t * f)
1230{
1231 f64 now, timeout = 1.0;
1232 uword *event_data = 0;
1233 uword event_type;
1234
1235 while (1)
1236 {
1237 vlib_process_wait_for_event_or_clock (vm, timeout);
1238 now = vlib_time_now (vm);
1239 event_type = vlib_process_get_events (vm, (uword **) & event_data);
1240
1241 switch (event_type)
1242 {
1243 case SESSION_Q_PROCESS_FLUSH_FRAMES:
1244 /* Flush the frames by updating all transports times */
1245 transport_update_time (now, 0);
1246 break;
1247 case SESSION_Q_PROCESS_STOP:
1248 timeout = 100000.0;
1249 break;
1250 case ~0:
1251 /* Timed out. Update time for all transports to trigger all
1252 * outstanding retransmits. */
1253 transport_update_time (now, 0);
1254 break;
1255 }
1256 vec_reset_length (event_data);
1257 }
1258 return 0;
1259}
1260
1261/* *INDENT-OFF* */
1262VLIB_REGISTER_NODE (session_queue_process_node) =
1263{
1264 .function = session_queue_process,
1265 .type = VLIB_NODE_TYPE_PROCESS,
1266 .name = "session-queue-process",
1267 .state = VLIB_NODE_STATE_DISABLED,
1268};
1269/* *INDENT-ON* */
1270
Florin Coras653e43f2019-03-04 10:56:23 -08001271static_always_inline uword
1272session_queue_pre_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1273 vlib_frame_t * frame)
1274{
1275 session_main_t *sm = &session_main;
1276 if (!sm->wrk[0].vpp_event_queue)
1277 return 0;
1278 return session_queue_node_fn (vm, node, frame);
1279}
1280
1281/* *INDENT-OFF* */
1282VLIB_REGISTER_NODE (session_queue_pre_input_node) =
1283{
1284 .function = session_queue_pre_input_inline,
1285 .type = VLIB_NODE_TYPE_PRE_INPUT,
1286 .name = "session-queue-main",
1287 .state = VLIB_NODE_STATE_DISABLED,
1288};
1289/* *INDENT-ON* */
1290
Dave Barach68b0fb02017-02-28 15:15:56 -05001291/*
1292 * fd.io coding-style-patch-verification: ON
1293 *
1294 * Local Variables:
1295 * eval: (c-set-style "gnu")
1296 * End:
1297 */