blob: a8478884806b9043ad3bebb188acc91aac71974b [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 svm_msg_q_unlock (app_wrk->event_queue);
179 evt = svm_msg_q_msg_data (app_wrk->event_queue, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400180 clib_memset (evt, 0, sizeof (*evt));
Florin Coras30e79c22019-01-02 19:31:22 -0800181 evt->event_type = SESSION_CTRL_EVT_DISCONNECTED_REPLY;
Florin Coras52207f12018-07-12 14:48:06 -0700182 rmp = (session_disconnected_reply_msg_t *) evt->data;
183 rmp->handle = mp->handle;
184 rmp->context = mp->context;
185 rmp->retval = rv;
Florin Coras15531972018-08-12 23:50:53 -0700186 svm_msg_q_add (app_wrk->event_queue, msg, SVM_Q_WAIT);
Florin Coras52207f12018-07-12 14:48:06 -0700187}
188
189static void
190session_mq_disconnected_reply_handler (void *data)
191{
192 session_disconnected_reply_msg_t *mp;
193 vnet_disconnect_args_t _a, *a = &_a;
194 application_t *app;
195
196 mp = (session_disconnected_reply_msg_t *) data;
197
198 /* Client objected to disconnecting the session, log and continue */
199 if (mp->retval)
200 {
201 clib_warning ("client retval %d", mp->retval);
202 return;
203 }
204
205 /* Disconnect has been confirmed. Confirm close to transport */
206 app = application_lookup (mp->context);
207 if (app)
208 {
209 a->handle = mp->handle;
Florin Coras15531972018-08-12 23:50:53 -0700210 a->app_index = app->app_index;
Florin Coras52207f12018-07-12 14:48:06 -0700211 vnet_disconnect_session (a);
212 }
213}
214
Florin Coras30e79c22019-01-02 19:31:22 -0800215static void
216session_mq_worker_update_handler (void *data)
217{
218 session_worker_update_msg_t *mp = (session_worker_update_msg_t *) data;
219 session_worker_update_reply_msg_t *rmp;
220 svm_msg_q_msg_t _msg, *msg = &_msg;
221 app_worker_t *app_wrk;
222 u32 owner_app_wrk_map;
223 session_event_t *evt;
Florin Coras288eaab2019-02-03 15:26:14 -0800224 session_t *s;
Florin Coras30e79c22019-01-02 19:31:22 -0800225 application_t *app;
226
227 app = application_lookup (mp->client_index);
228 if (!app)
229 return;
230 if (!(s = session_get_from_handle_if_valid (mp->handle)))
231 {
232 clib_warning ("invalid handle %llu", mp->handle);
233 return;
234 }
235 app_wrk = app_worker_get (s->app_wrk_index);
236 if (app_wrk->app_index != app->app_index)
237 {
238 clib_warning ("app %u does not own session %llu", app->app_index,
239 mp->handle);
240 return;
241 }
242 owner_app_wrk_map = app_wrk->wrk_map_index;
243 app_wrk = application_get_worker (app, mp->wrk_index);
244
245 /* This needs to come from the new owner */
246 if (mp->req_wrk_index == owner_app_wrk_map)
247 {
248 session_req_worker_update_msg_t *wump;
249
250 svm_msg_q_lock_and_alloc_msg_w_ring (app_wrk->event_queue,
251 SESSION_MQ_CTRL_EVT_RING,
252 SVM_Q_WAIT, msg);
253 svm_msg_q_unlock (app_wrk->event_queue);
254 evt = svm_msg_q_msg_data (app_wrk->event_queue, msg);
255 clib_memset (evt, 0, sizeof (*evt));
256 evt->event_type = SESSION_CTRL_EVT_REQ_WORKER_UPDATE;
257 wump = (session_req_worker_update_msg_t *) evt->data;
258 wump->session_handle = mp->handle;
259 svm_msg_q_add (app_wrk->event_queue, msg, SVM_Q_WAIT);
260 return;
261 }
262
263 app_worker_own_session (app_wrk, s);
264
265 /*
266 * Send reply
267 */
268 svm_msg_q_lock_and_alloc_msg_w_ring (app_wrk->event_queue,
269 SESSION_MQ_CTRL_EVT_RING,
270 SVM_Q_WAIT, msg);
271 svm_msg_q_unlock (app_wrk->event_queue);
272 evt = svm_msg_q_msg_data (app_wrk->event_queue, msg);
273 clib_memset (evt, 0, sizeof (*evt));
274 evt->event_type = SESSION_CTRL_EVT_WORKER_UPDATE_REPLY;
275 rmp = (session_worker_update_reply_msg_t *) evt->data;
276 rmp->handle = mp->handle;
Florin Coras288eaab2019-02-03 15:26:14 -0800277 rmp->rx_fifo = pointer_to_uword (s->rx_fifo);
278 rmp->tx_fifo = pointer_to_uword (s->tx_fifo);
Florin Coras30e79c22019-01-02 19:31:22 -0800279 rmp->segment_handle = session_segment_handle (s);
280 svm_msg_q_add (app_wrk->event_queue, msg, SVM_Q_WAIT);
281
282 /*
283 * Retransmit messages that may have been lost
284 */
Florin Coras288eaab2019-02-03 15:26:14 -0800285 if (s->tx_fifo && !svm_fifo_is_empty (s->tx_fifo))
Florin Corasf6c43132019-03-01 12:41:21 -0800286 session_send_io_evt_to_thread (s->tx_fifo, SESSION_IO_EVT_TX);
Florin Coras30e79c22019-01-02 19:31:22 -0800287
Florin Coras288eaab2019-02-03 15:26:14 -0800288 if (s->rx_fifo && !svm_fifo_is_empty (s->rx_fifo))
Florin Corasf6c43132019-03-01 12:41:21 -0800289 app_worker_lock_and_send_event (app_wrk, s, SESSION_IO_EVT_RX);
Florin Coras30e79c22019-01-02 19:31:22 -0800290
291 if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
Florin Coras69b68ef2019-04-02 11:38:51 -0700292 app_worker_close_notify (app_wrk, s);
Florin Coras30e79c22019-01-02 19:31:22 -0800293}
294
Dave Barach68b0fb02017-02-28 15:15:56 -0500295vlib_node_registration_t session_queue_node;
296
297typedef struct
298{
299 u32 session_index;
300 u32 server_thread_index;
301} session_queue_trace_t;
302
303/* packet trace format function */
304static u8 *
305format_session_queue_trace (u8 * s, va_list * args)
306{
307 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
308 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
309 session_queue_trace_t *t = va_arg (*args, session_queue_trace_t *);
310
311 s = format (s, "SESSION_QUEUE: session index %d, server thread index %d",
312 t->session_index, t->server_thread_index);
313 return s;
314}
315
Florin Coras6792ec02017-03-13 03:49:51 -0700316#define foreach_session_queue_error \
317_(TX, "Packets transmitted") \
Florin Coras93992a92017-05-24 18:03:56 -0700318_(TIMER, "Timer events") \
319_(NO_BUFFER, "Out of buffers")
Dave Barach68b0fb02017-02-28 15:15:56 -0500320
321typedef enum
322{
323#define _(sym,str) SESSION_QUEUE_ERROR_##sym,
324 foreach_session_queue_error
325#undef _
326 SESSION_QUEUE_N_ERROR,
327} session_queue_error_t;
328
329static char *session_queue_error_strings[] = {
330#define _(sym,string) string,
331 foreach_session_queue_error
332#undef _
333};
334
Florin Coras0d60a0f2018-06-29 02:02:08 -0700335enum
336{
337 SESSION_TX_NO_BUFFERS = -2,
338 SESSION_TX_NO_DATA,
339 SESSION_TX_OK
340};
341
Florin Coras66cf5e02018-06-08 09:17:39 -0700342static void
343session_tx_trace_frame (vlib_main_t * vm, vlib_node_runtime_t * node,
344 u32 next_index, u32 * to_next, u16 n_segs,
Florin Coras288eaab2019-02-03 15:26:14 -0800345 session_t * s, u32 n_trace)
Florin Corasf6d68ed2017-05-07 19:12:02 -0700346{
Florin Coras8e43d042018-05-04 15:46:57 -0700347 session_queue_trace_t *t;
Florin Coras66cf5e02018-06-08 09:17:39 -0700348 vlib_buffer_t *b;
349 int i;
350
351 for (i = 0; i < clib_min (n_trace, n_segs); i++)
352 {
353 b = vlib_get_buffer (vm, to_next[i - n_segs]);
354 vlib_trace_buffer (vm, node, next_index, b, 1 /* follow_chain */ );
Florin Coras66cf5e02018-06-08 09:17:39 -0700355 t = vlib_add_trace (vm, node, b, sizeof (*t));
356 t->session_index = s->session_index;
357 t->server_thread_index = s->thread_index;
358 }
Florin Coras4df38712018-06-20 12:44:16 -0700359 vlib_set_trace_count (vm, node, n_trace - i);
Florin Coras8e43d042018-05-04 15:46:57 -0700360}
Florin Corasf6d68ed2017-05-07 19:12:02 -0700361
Florin Coras8e43d042018-05-04 15:46:57 -0700362always_inline void
363session_tx_fifo_chain_tail (vlib_main_t * vm, session_tx_context_t * ctx,
364 vlib_buffer_t * b, u16 * n_bufs, u8 peek_data)
365{
Florin Coras8e43d042018-05-04 15:46:57 -0700366 vlib_buffer_t *chain_b, *prev_b;
367 u32 chain_bi0, to_deq, left_from_seg;
Florin Coras31c99552019-03-01 13:00:58 -0800368 session_worker_t *wrk;
Florin Coras8e43d042018-05-04 15:46:57 -0700369 u16 len_to_deq, n_bytes_read;
370 u8 *data, j;
Florin Corasb2215d62017-08-01 16:56:58 -0700371
Florin Coras31c99552019-03-01 13:00:58 -0800372 wrk = session_main_get_worker (ctx->s->thread_index);
Florin Coras8e43d042018-05-04 15:46:57 -0700373 b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
374 b->total_length_not_including_first_buffer = 0;
375
376 chain_b = b;
377 left_from_seg = clib_min (ctx->snd_mss - b->current_length,
378 ctx->left_to_snd);
Florin Corasb2215d62017-08-01 16:56:58 -0700379 to_deq = left_from_seg;
Florin Coras8e43d042018-05-04 15:46:57 -0700380 for (j = 1; j < ctx->n_bufs_per_seg; j++)
Florin Corasf6d68ed2017-05-07 19:12:02 -0700381 {
Florin Coras8e43d042018-05-04 15:46:57 -0700382 prev_b = chain_b;
383 len_to_deq = clib_min (to_deq, ctx->deq_per_buf);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700384
385 *n_bufs -= 1;
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700386 chain_bi0 = wrk->tx_buffers[*n_bufs];
Florin Coras8e43d042018-05-04 15:46:57 -0700387 chain_b = vlib_get_buffer (vm, chain_bi0);
388 chain_b->current_data = 0;
389 data = vlib_buffer_get_current (chain_b);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700390 if (peek_data)
391 {
Florin Coras288eaab2019-02-03 15:26:14 -0800392 n_bytes_read = svm_fifo_peek (ctx->s->tx_fifo,
Florin Coras8e43d042018-05-04 15:46:57 -0700393 ctx->tx_offset, len_to_deq, data);
394 ctx->tx_offset += n_bytes_read;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700395 }
396 else
397 {
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200398 if (ctx->transport_vft->transport_options.tx_type ==
399 TRANSPORT_TX_DGRAM)
Florin Coras7fb0fe12018-04-09 09:24:52 -0700400 {
Florin Coras288eaab2019-02-03 15:26:14 -0800401 svm_fifo_t *f = ctx->s->tx_fifo;
Florin Coras8e43d042018-05-04 15:46:57 -0700402 session_dgram_hdr_t *hdr = &ctx->hdr;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700403 u16 deq_now;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700404 deq_now = clib_min (hdr->data_length - hdr->data_offset,
Florin Coras8e43d042018-05-04 15:46:57 -0700405 len_to_deq);
406 n_bytes_read = svm_fifo_peek (f, hdr->data_offset, deq_now,
407 data);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700408 ASSERT (n_bytes_read > 0);
409
410 hdr->data_offset += n_bytes_read;
411 if (hdr->data_offset == hdr->data_length)
shubing guoaea5f392018-08-30 13:29:49 +0800412 {
413 u32 offset = hdr->data_length + SESSION_CONN_HDR_LEN;
414 svm_fifo_dequeue_drop (f, offset);
415 }
Florin Coras7fb0fe12018-04-09 09:24:52 -0700416 }
417 else
Florin Coras87b15ce2019-04-28 21:16:30 -0700418 n_bytes_read = svm_fifo_dequeue (ctx->s->tx_fifo,
419 len_to_deq, data);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700420 }
Florin Coras8e43d042018-05-04 15:46:57 -0700421 ASSERT (n_bytes_read == len_to_deq);
422 chain_b->current_length = n_bytes_read;
423 b->total_length_not_including_first_buffer += chain_b->current_length;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700424
425 /* update previous buffer */
Florin Coras8e43d042018-05-04 15:46:57 -0700426 prev_b->next_buffer = chain_bi0;
427 prev_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700428
429 /* update current buffer */
Florin Coras8e43d042018-05-04 15:46:57 -0700430 chain_b->next_buffer = 0;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700431
Florin Corasb2215d62017-08-01 16:56:58 -0700432 to_deq -= n_bytes_read;
433 if (to_deq == 0)
Florin Corasf6d68ed2017-05-07 19:12:02 -0700434 break;
435 }
Florin Coras1f152cd2017-08-18 19:28:03 -0700436 ASSERT (to_deq == 0
Florin Coras8e43d042018-05-04 15:46:57 -0700437 && b->total_length_not_including_first_buffer == left_from_seg);
438 ctx->left_to_snd -= left_from_seg;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700439}
440
Florin Coras8e43d042018-05-04 15:46:57 -0700441always_inline void
442session_tx_fill_buffer (vlib_main_t * vm, session_tx_context_t * ctx,
443 vlib_buffer_t * b, u16 * n_bufs, u8 peek_data)
444{
445 u32 len_to_deq;
446 u8 *data0;
447 int n_bytes_read;
448
449 /*
450 * Start with the first buffer in chain
451 */
452 b->error = 0;
453 b->flags = VNET_BUFFER_F_LOCALLY_ORIGINATED;
454 b->current_data = 0;
Florin Coras8e43d042018-05-04 15:46:57 -0700455
Florin Coras1ee78302019-02-05 15:51:15 -0800456 data0 = vlib_buffer_make_headroom (b, TRANSPORT_MAX_HDRS_LEN);
Florin Coras8e43d042018-05-04 15:46:57 -0700457 len_to_deq = clib_min (ctx->left_to_snd, ctx->deq_per_first_buf);
458
Florin Coras7fb0fe12018-04-09 09:24:52 -0700459 if (peek_data)
460 {
Florin Coras288eaab2019-02-03 15:26:14 -0800461 n_bytes_read = svm_fifo_peek (ctx->s->tx_fifo, ctx->tx_offset,
Florin Coras8e43d042018-05-04 15:46:57 -0700462 len_to_deq, data0);
463 ASSERT (n_bytes_read > 0);
464 /* Keep track of progress locally, transport is also supposed to
465 * increment it independently when pushing the header */
466 ctx->tx_offset += n_bytes_read;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700467 }
468 else
469 {
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200470 if (ctx->transport_vft->transport_options.tx_type == TRANSPORT_TX_DGRAM)
Florin Coras8e43d042018-05-04 15:46:57 -0700471 {
472 session_dgram_hdr_t *hdr = &ctx->hdr;
Florin Coras288eaab2019-02-03 15:26:14 -0800473 svm_fifo_t *f = ctx->s->tx_fifo;
Florin Coras8e43d042018-05-04 15:46:57 -0700474 u16 deq_now;
475 u32 offset;
476
477 ASSERT (hdr->data_length > hdr->data_offset);
478 deq_now = clib_min (hdr->data_length - hdr->data_offset,
479 len_to_deq);
480 offset = hdr->data_offset + SESSION_CONN_HDR_LEN;
481 n_bytes_read = svm_fifo_peek (f, offset, deq_now, data0);
482 ASSERT (n_bytes_read > 0);
483
484 if (ctx->s->session_state == SESSION_STATE_LISTENING)
485 {
486 ip_copy (&ctx->tc->rmt_ip, &hdr->rmt_ip, ctx->tc->is_ip4);
487 ctx->tc->rmt_port = hdr->rmt_port;
488 }
489 hdr->data_offset += n_bytes_read;
490 if (hdr->data_offset == hdr->data_length)
491 {
492 offset = hdr->data_length + SESSION_CONN_HDR_LEN;
493 svm_fifo_dequeue_drop (f, offset);
494 }
495 }
Florin Coras7fb0fe12018-04-09 09:24:52 -0700496 else
497 {
Florin Coras87b15ce2019-04-28 21:16:30 -0700498 n_bytes_read = svm_fifo_dequeue (ctx->s->tx_fifo,
499 len_to_deq, data0);
Florin Coras8e43d042018-05-04 15:46:57 -0700500 ASSERT (n_bytes_read > 0);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700501 }
502 }
Florin Coras8e43d042018-05-04 15:46:57 -0700503 b->current_length = n_bytes_read;
504 ctx->left_to_snd -= n_bytes_read;
Dave Barach68b0fb02017-02-28 15:15:56 -0500505
Florin Coras8e43d042018-05-04 15:46:57 -0700506 /*
507 * Fill in the remaining buffers in the chain, if any
508 */
509 if (PREDICT_FALSE (ctx->n_bufs_per_seg > 1 && ctx->left_to_snd))
510 session_tx_fifo_chain_tail (vm, ctx, b, n_bufs, peek_data);
Florin Coras8e43d042018-05-04 15:46:57 -0700511}
512
513always_inline u8
Florin Coras288eaab2019-02-03 15:26:14 -0800514session_tx_not_ready (session_t * s, u8 peek_data)
Florin Coras8e43d042018-05-04 15:46:57 -0700515{
516 if (peek_data)
Florin Corase04c2992017-03-01 08:17:34 -0800517 {
Florin Coras8e43d042018-05-04 15:46:57 -0700518 /* Can retransmit for closed sessions but can't send new data if
519 * session is not ready or closed */
520 if (s->session_state < SESSION_STATE_READY)
521 return 1;
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800522 if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSED)
Florin Corasca1c8f32018-05-23 21:01:30 -0700523 return 2;
Florin Corase04c2992017-03-01 08:17:34 -0800524 }
Florin Coras8e43d042018-05-04 15:46:57 -0700525 return 0;
526}
Dave Barach68b0fb02017-02-28 15:15:56 -0500527
Florin Coras8e43d042018-05-04 15:46:57 -0700528always_inline transport_connection_t *
529session_tx_get_transport (session_tx_context_t * ctx, u8 peek_data)
530{
531 if (peek_data)
532 {
533 return ctx->transport_vft->get_connection (ctx->s->connection_index,
534 ctx->s->thread_index);
535 }
536 else
537 {
538 if (ctx->s->session_state == SESSION_STATE_LISTENING)
539 return ctx->transport_vft->get_listener (ctx->s->connection_index);
540 else
541 {
542 return ctx->transport_vft->get_connection (ctx->s->connection_index,
543 ctx->s->thread_index);
544 }
545 }
546}
Florin Coras6a9b68b2017-11-21 04:20:42 -0800547
Florin Coras8e43d042018-05-04 15:46:57 -0700548always_inline void
549session_tx_set_dequeue_params (vlib_main_t * vm, session_tx_context_t * ctx,
Florin Corasf08f26d2018-05-10 13:20:47 -0700550 u32 max_segs, u8 peek_data)
Florin Coras8e43d042018-05-04 15:46:57 -0700551{
552 u32 n_bytes_per_buf, n_bytes_per_seg;
Sirshak Das28aa5392019-02-05 01:33:33 -0600553 ctx->max_dequeue = svm_fifo_max_dequeue_cons (ctx->s->tx_fifo);
Dave Barach68b0fb02017-02-28 15:15:56 -0500554 if (peek_data)
555 {
Florin Coras9d063042017-09-14 03:08:00 -0400556 /* Offset in rx fifo from where to peek data */
Florin Coras8e43d042018-05-04 15:46:57 -0700557 ctx->tx_offset = ctx->transport_vft->tx_fifo_offset (ctx->tc);
558 if (PREDICT_FALSE (ctx->tx_offset >= ctx->max_dequeue))
559 {
560 ctx->max_len_to_snd = 0;
561 return;
562 }
563 ctx->max_dequeue -= ctx->tx_offset;
Dave Barach68b0fb02017-02-28 15:15:56 -0500564 }
Florin Coras7fb0fe12018-04-09 09:24:52 -0700565 else
566 {
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200567 if (ctx->transport_vft->transport_options.tx_type == TRANSPORT_TX_DGRAM)
Florin Coras7fb0fe12018-04-09 09:24:52 -0700568 {
Florin Coras8e43d042018-05-04 15:46:57 -0700569 if (ctx->max_dequeue <= sizeof (ctx->hdr))
570 {
571 ctx->max_len_to_snd = 0;
572 return;
573 }
Florin Coras288eaab2019-02-03 15:26:14 -0800574 svm_fifo_peek (ctx->s->tx_fifo, 0, sizeof (ctx->hdr),
Florin Coras8e43d042018-05-04 15:46:57 -0700575 (u8 *) & ctx->hdr);
576 ASSERT (ctx->hdr.data_length > ctx->hdr.data_offset);
577 ctx->max_dequeue = ctx->hdr.data_length - ctx->hdr.data_offset;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700578 }
579 }
Florin Coras8e43d042018-05-04 15:46:57 -0700580 ASSERT (ctx->max_dequeue > 0);
Florin Coras6792ec02017-03-13 03:49:51 -0700581
582 /* Ensure we're not writing more than transport window allows */
Florin Coras8e43d042018-05-04 15:46:57 -0700583 if (ctx->max_dequeue < ctx->snd_space)
Florin Coras3e350af2017-03-30 02:54:28 -0700584 {
585 /* Constrained by tx queue. Try to send only fully formed segments */
Florin Coras8e43d042018-05-04 15:46:57 -0700586 ctx->max_len_to_snd =
587 (ctx->max_dequeue > ctx->snd_mss) ?
588 ctx->max_dequeue - ctx->max_dequeue % ctx->snd_mss : ctx->max_dequeue;
Florin Coras3e350af2017-03-30 02:54:28 -0700589 /* TODO Nagle ? */
590 }
591 else
592 {
Florin Coras1f152cd2017-08-18 19:28:03 -0700593 /* Expectation is that snd_space0 is already a multiple of snd_mss */
Florin Coras8e43d042018-05-04 15:46:57 -0700594 ctx->max_len_to_snd = ctx->snd_space;
Florin Coras3e350af2017-03-30 02:54:28 -0700595 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500596
Florin Corasf08f26d2018-05-10 13:20:47 -0700597 /* Check if we're tx constrained by the node */
598 ctx->n_segs_per_evt = ceil ((f64) ctx->max_len_to_snd / ctx->snd_mss);
599 if (ctx->n_segs_per_evt > max_segs)
600 {
601 ctx->n_segs_per_evt = max_segs;
602 ctx->max_len_to_snd = max_segs * ctx->snd_mss;
603 }
604
Damjan Marion8934a042019-02-09 23:29:26 +0100605 n_bytes_per_buf = vlib_buffer_get_default_data_size (vm);
Florin Coras1ee78302019-02-05 15:51:15 -0800606 ASSERT (n_bytes_per_buf > TRANSPORT_MAX_HDRS_LEN);
607 n_bytes_per_seg = TRANSPORT_MAX_HDRS_LEN + ctx->snd_mss;
Florin Corasf08f26d2018-05-10 13:20:47 -0700608 ctx->n_bufs_per_seg = ceil ((f64) n_bytes_per_seg / n_bytes_per_buf);
Florin Coras8e43d042018-05-04 15:46:57 -0700609 ctx->deq_per_buf = clib_min (ctx->snd_mss, n_bytes_per_buf);
610 ctx->deq_per_first_buf = clib_min (ctx->snd_mss,
Florin Coras1ee78302019-02-05 15:51:15 -0800611 n_bytes_per_buf -
612 TRANSPORT_MAX_HDRS_LEN);
Florin Coras8e43d042018-05-04 15:46:57 -0700613}
Florin Corasf6d68ed2017-05-07 19:12:02 -0700614
Florin Coras8e43d042018-05-04 15:46:57 -0700615always_inline int
616session_tx_fifo_read_and_snd_i (vlib_main_t * vm, vlib_node_runtime_t * node,
Florin Coras31c99552019-03-01 13:00:58 -0800617 session_worker_t * wrk,
Florin Coras2062ec02019-07-15 13:15:18 -0700618 session_evt_elt_t * elt, int *n_tx_packets,
Florin Coras8e43d042018-05-04 15:46:57 -0700619 u8 peek_data)
620{
Florin Coras2068fd42019-01-31 14:35:50 -0800621 u32 next_index, next0, next1, *to_next, n_left_to_next, n_left, pbi;
Florin Corasf08f26d2018-05-10 13:20:47 -0700622 u32 n_trace = vlib_get_trace_count (vm, node), n_bufs_needed = 0;
Florin Coras31c99552019-03-01 13:00:58 -0800623 session_main_t *smm = &session_main;
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700624 session_tx_context_t *ctx = &wrk->ctx;
Florin Coras2062ec02019-07-15 13:15:18 -0700625 session_event_t *e = &elt->evt;
Florin Coras8e43d042018-05-04 15:46:57 -0700626 transport_proto_t tp;
627 vlib_buffer_t *pb;
Florin Corasca1c8f32018-05-23 21:01:30 -0700628 u16 n_bufs, rv;
Florin Coras8e43d042018-05-04 15:46:57 -0700629
Florin Coras1bcad5c2019-01-09 20:04:38 -0800630 if (PREDICT_FALSE ((rv = session_tx_not_ready (ctx->s, peek_data))))
Florin Coras8e43d042018-05-04 15:46:57 -0700631 {
Florin Corasca1c8f32018-05-23 21:01:30 -0700632 if (rv < 2)
Florin Coras2062ec02019-07-15 13:15:18 -0700633 session_evt_add_pending (wrk, elt);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700634 return SESSION_TX_NO_DATA;
Florin Coras8e43d042018-05-04 15:46:57 -0700635 }
636
Florin Coras1bcad5c2019-01-09 20:04:38 -0800637 next_index = smm->session_type_to_next[ctx->s->session_type];
Florin Coras66cf5e02018-06-08 09:17:39 -0700638 next0 = next1 = next_index;
Florin Coras8e43d042018-05-04 15:46:57 -0700639
Florin Coras1bcad5c2019-01-09 20:04:38 -0800640 tp = session_get_transport_proto (ctx->s);
Florin Coras8e43d042018-05-04 15:46:57 -0700641 ctx->transport_vft = transport_protocol_get_vft (tp);
642 ctx->tc = session_tx_get_transport (ctx, peek_data);
643 ctx->snd_mss = ctx->transport_vft->send_mss (ctx->tc);
Florin Coras42ceddb2018-12-12 10:56:01 -0800644
645 if (PREDICT_FALSE (e->event_type == SESSION_IO_EVT_TX_FLUSH))
646 {
647 if (ctx->transport_vft->flush_data)
648 ctx->transport_vft->flush_data (ctx->tc);
649 }
650
651 ctx->snd_space = transport_connection_snd_space (ctx->tc,
David Johnsond9818dd2018-12-14 14:53:41 -0500652 vm->clib_time.
653 last_cpu_time,
Florin Coras42ceddb2018-12-12 10:56:01 -0800654 ctx->snd_mss);
Florin Coras8e43d042018-05-04 15:46:57 -0700655 if (ctx->snd_space == 0 || ctx->snd_mss == 0)
656 {
Florin Coras2062ec02019-07-15 13:15:18 -0700657 session_evt_add_pending (wrk, elt);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700658 return SESSION_TX_NO_DATA;
Florin Coras8e43d042018-05-04 15:46:57 -0700659 }
660
661 /* Allow enqueuing of a new event */
Florin Coras288eaab2019-02-03 15:26:14 -0800662 svm_fifo_unset_event (ctx->s->tx_fifo);
Florin Coras8e43d042018-05-04 15:46:57 -0700663
664 /* Check how much we can pull. */
Florin Corasf08f26d2018-05-10 13:20:47 -0700665 session_tx_set_dequeue_params (vm, ctx, VLIB_FRAME_SIZE - *n_tx_packets,
666 peek_data);
667
Florin Coras8e43d042018-05-04 15:46:57 -0700668 if (PREDICT_FALSE (!ctx->max_len_to_snd))
Florin Coras0d60a0f2018-06-29 02:02:08 -0700669 return SESSION_TX_NO_DATA;
Dave Barach68b0fb02017-02-28 15:15:56 -0500670
Florin Corasf08f26d2018-05-10 13:20:47 -0700671 n_bufs_needed = ctx->n_segs_per_evt * ctx->n_bufs_per_seg;
Florin Coras2068fd42019-01-31 14:35:50 -0800672 vec_validate_aligned (wrk->tx_buffers, n_bufs_needed - 1,
673 CLIB_CACHE_LINE_BYTES);
674 n_bufs = vlib_buffer_alloc (vm, wrk->tx_buffers, n_bufs_needed);
675 if (PREDICT_FALSE (n_bufs < n_bufs_needed))
Dave Barach68b0fb02017-02-28 15:15:56 -0500676 {
Florin Coras2068fd42019-01-31 14:35:50 -0800677 if (n_bufs)
678 vlib_buffer_free (vm, wrk->tx_buffers, n_bufs);
Florin Coras2062ec02019-07-15 13:15:18 -0700679 session_evt_add_pending (wrk, elt);
Florin Coras2068fd42019-01-31 14:35:50 -0800680 return SESSION_TX_NO_BUFFERS;
Florin Coras8e43d042018-05-04 15:46:57 -0700681 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500682
Florin Coras8e43d042018-05-04 15:46:57 -0700683 /*
684 * Write until we fill up a frame
685 */
686 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Florin Corasf08f26d2018-05-10 13:20:47 -0700687 if (PREDICT_FALSE (ctx->n_segs_per_evt > n_left_to_next))
Florin Coras8e43d042018-05-04 15:46:57 -0700688 {
Florin Corasf08f26d2018-05-10 13:20:47 -0700689 ctx->n_segs_per_evt = n_left_to_next;
690 ctx->max_len_to_snd = ctx->snd_mss * n_left_to_next;
691 }
692 ctx->left_to_snd = ctx->max_len_to_snd;
693 n_left = ctx->n_segs_per_evt;
Florin Coras66cf5e02018-06-08 09:17:39 -0700694
695 while (n_left >= 4)
696 {
697 vlib_buffer_t *b0, *b1;
698 u32 bi0, bi1;
699
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700700 pbi = wrk->tx_buffers[n_bufs - 3];
Florin Coras66cf5e02018-06-08 09:17:39 -0700701 pb = vlib_get_buffer (vm, pbi);
702 vlib_prefetch_buffer_header (pb, STORE);
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700703 pbi = wrk->tx_buffers[n_bufs - 4];
Florin Coras66cf5e02018-06-08 09:17:39 -0700704 pb = vlib_get_buffer (vm, pbi);
705 vlib_prefetch_buffer_header (pb, STORE);
706
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700707 to_next[0] = bi0 = wrk->tx_buffers[--n_bufs];
708 to_next[1] = bi1 = wrk->tx_buffers[--n_bufs];
Florin Coras66cf5e02018-06-08 09:17:39 -0700709
710 b0 = vlib_get_buffer (vm, bi0);
711 b1 = vlib_get_buffer (vm, bi1);
712
713 session_tx_fill_buffer (vm, ctx, b0, &n_bufs, peek_data);
714 session_tx_fill_buffer (vm, ctx, b1, &n_bufs, peek_data);
715
716 ctx->transport_vft->push_header (ctx->tc, b0);
717 ctx->transport_vft->push_header (ctx->tc, b1);
718
719 to_next += 2;
720 n_left_to_next -= 2;
721 n_left -= 2;
722
723 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
724 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b1);
725
726 vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
727 n_left_to_next, bi0, bi1, next0,
728 next1);
729 }
Florin Corasf08f26d2018-05-10 13:20:47 -0700730 while (n_left)
731 {
Florin Coras66cf5e02018-06-08 09:17:39 -0700732 vlib_buffer_t *b0;
733 u32 bi0;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700734
Florin Coras91ca4622018-06-30 11:27:59 -0700735 if (n_left > 1)
736 {
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700737 pbi = wrk->tx_buffers[n_bufs - 2];
Florin Coras91ca4622018-06-30 11:27:59 -0700738 pb = vlib_get_buffer (vm, pbi);
739 vlib_prefetch_buffer_header (pb, STORE);
740 }
741
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700742 to_next[0] = bi0 = wrk->tx_buffers[--n_bufs];
Florin Coras66cf5e02018-06-08 09:17:39 -0700743 b0 = vlib_get_buffer (vm, bi0);
744 session_tx_fill_buffer (vm, ctx, b0, &n_bufs, peek_data);
Florin Coras81a13db2018-03-16 08:48:31 -0700745
Florin Coras66cf5e02018-06-08 09:17:39 -0700746 /* Ask transport to push header after current_length and
747 * total_length_not_including_first_buffer are updated */
748 ctx->transport_vft->push_header (ctx->tc, b0);
Florin Corasf6359c82017-06-19 12:26:09 -0400749
Florin Coras66cf5e02018-06-08 09:17:39 -0700750 to_next += 1;
751 n_left_to_next -= 1;
752 n_left -= 1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500753
Florin Coras66cf5e02018-06-08 09:17:39 -0700754 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700755
Florin Coras66cf5e02018-06-08 09:17:39 -0700756 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
757 n_left_to_next, bi0, next0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500758 }
Florin Coras66cf5e02018-06-08 09:17:39 -0700759
760 if (PREDICT_FALSE (n_trace > 0))
761 session_tx_trace_frame (vm, node, next_index, to_next,
Florin Coras1bcad5c2019-01-09 20:04:38 -0800762 ctx->n_segs_per_evt, ctx->s, n_trace);
Florin Coras2068fd42019-01-31 14:35:50 -0800763 if (PREDICT_FALSE (n_bufs))
764 {
Florin Coras2068fd42019-01-31 14:35:50 -0800765 vlib_buffer_free (vm, wrk->tx_buffers, n_bufs);
766 }
Florin Corasf08f26d2018-05-10 13:20:47 -0700767 *n_tx_packets += ctx->n_segs_per_evt;
Florin Corasd67f1122018-05-21 17:47:40 -0700768 transport_connection_update_tx_stats (ctx->tc, ctx->max_len_to_snd);
Florin Coras8e43d042018-05-04 15:46:57 -0700769 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
Dave Barach68b0fb02017-02-28 15:15:56 -0500770
Florin Corascca96182019-07-19 07:34:13 -0700771 SESSION_EVT (SESSION_EVT_DEQ, ctx->s, ctx->max_len_to_snd, ctx->max_dequeue,
772 ctx->s->tx_fifo->has_event, wrk->last_vlib_time);
773
Florin Coras6792ec02017-03-13 03:49:51 -0700774 /* If we couldn't dequeue all bytes mark as partially read */
Florin Corasf08f26d2018-05-10 13:20:47 -0700775 ASSERT (ctx->left_to_snd == 0);
Florin Coras8e43d042018-05-04 15:46:57 -0700776 if (ctx->max_len_to_snd < ctx->max_dequeue)
Florin Coras288eaab2019-02-03 15:26:14 -0800777 if (svm_fifo_set_event (ctx->s->tx_fifo))
Florin Coras2062ec02019-07-15 13:15:18 -0700778 session_evt_add_pending (wrk, elt);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700779
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200780 if (!peek_data
781 && ctx->transport_vft->transport_options.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 */
Sirshak Das28aa5392019-02-05 01:33:33 -0600788 else if (svm_fifo_max_dequeue_cons (ctx->s->tx_fifo) > 0)
Florin Coras288eaab2019-02-03 15:26:14 -0800789 if (svm_fifo_set_event (ctx->s->tx_fifo))
Florin Coras2062ec02019-07-15 13:15:18 -0700790 session_evt_add_pending (wrk, elt);
Florin Coras0e573f52019-05-07 16:28:16 -0700791
Florin Coras2d379d82019-06-28 12:45:12 -0700792 if (svm_fifo_needs_deq_ntf (ctx->s->tx_fifo, ctx->max_len_to_snd))
Florin Coras0e573f52019-05-07 16:28:16 -0700793 session_dequeue_notify (ctx->s);
Dave Barach68b0fb02017-02-28 15:15:56 -0500794 }
Florin Coras0d60a0f2018-06-29 02:02:08 -0700795 return SESSION_TX_OK;
Dave Barach68b0fb02017-02-28 15:15:56 -0500796}
797
798int
Florin Corasd79b41e2017-03-04 05:37:52 -0800799session_tx_fifo_peek_and_snd (vlib_main_t * vm, vlib_node_runtime_t * node,
Florin Coras31c99552019-03-01 13:00:58 -0800800 session_worker_t * wrk,
Florin Coras2062ec02019-07-15 13:15:18 -0700801 session_evt_elt_t * e, int *n_tx_pkts)
Dave Barach68b0fb02017-02-28 15:15:56 -0500802{
Florin Coras1bcad5c2019-01-09 20:04:38 -0800803 return session_tx_fifo_read_and_snd_i (vm, node, wrk, e, n_tx_pkts, 1);
Dave Barach68b0fb02017-02-28 15:15:56 -0500804}
805
806int
Florin Corasd79b41e2017-03-04 05:37:52 -0800807session_tx_fifo_dequeue_and_snd (vlib_main_t * vm, vlib_node_runtime_t * node,
Florin Coras31c99552019-03-01 13:00:58 -0800808 session_worker_t * wrk,
Florin Coras2062ec02019-07-15 13:15:18 -0700809 session_evt_elt_t * e, int *n_tx_pkts)
Dave Barach68b0fb02017-02-28 15:15:56 -0500810{
Florin Coras1bcad5c2019-01-09 20:04:38 -0800811 return session_tx_fifo_read_and_snd_i (vm, node, wrk, e, n_tx_pkts, 0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500812}
813
Florin Coras371ca502018-02-21 12:07:41 -0800814int
815session_tx_fifo_dequeue_internal (vlib_main_t * vm,
816 vlib_node_runtime_t * node,
Florin Coras31c99552019-03-01 13:00:58 -0800817 session_worker_t * wrk,
Florin Coras2062ec02019-07-15 13:15:18 -0700818 session_evt_elt_t * e, int *n_tx_pkts)
Florin Coras371ca502018-02-21 12:07:41 -0800819{
Florin Coras288eaab2019-02-03 15:26:14 -0800820 session_t *s = wrk->ctx.s;
Florin Coras1bcad5c2019-01-09 20:04:38 -0800821
Florin Corasc0737e92019-03-04 14:19:39 -0800822 if (PREDICT_FALSE (s->session_state >= SESSION_STATE_TRANSPORT_CLOSED))
Florin Corasef915342018-09-29 10:23:06 -0700823 return 0;
Florin Coras288eaab2019-02-03 15:26:14 -0800824 svm_fifo_unset_event (s->tx_fifo);
Florin Corasf940f8a2019-03-06 10:44:38 -0800825 return transport_custom_tx (session_get_transport_proto (s), s);
Florin Coras371ca502018-02-21 12:07:41 -0800826}
827
Florin Coras288eaab2019-02-03 15:26:14 -0800828always_inline session_t *
Florin Coras52207f12018-07-12 14:48:06 -0700829session_event_get_session (session_event_t * e, u8 thread_index)
Florin Corasa5464812017-04-19 13:00:05 -0700830{
Florin Corasc0737e92019-03-04 14:19:39 -0800831 return session_get_if_valid (e->session_index, thread_index);
Florin Corasa5464812017-04-19 13:00:05 -0700832}
833
Florin Corasd67f1122018-05-21 17:47:40 -0700834static void
Florin Coras31c99552019-03-01 13:00:58 -0800835session_update_dispatch_period (session_worker_t * wrk, f64 now,
Florin Corasd67f1122018-05-21 17:47:40 -0700836 u32 thread_index)
837{
Florin Corasefefc6b2018-11-07 12:49:19 -0800838 if (wrk->last_tx_packets)
Florin Corasc44a5582018-11-01 16:30:54 -0700839 {
840 f64 sample = now - wrk->last_vlib_time;
Florin Corasc44a5582018-11-01 16:30:54 -0700841 wrk->dispatch_period = (wrk->dispatch_period + sample) * 0.5;
842 }
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700843 wrk->last_vlib_time = now;
Florin Corasd67f1122018-05-21 17:47:40 -0700844}
845
Florin Coras0d60a0f2018-06-29 02:02:08 -0700846static uword
847session_queue_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
848 vlib_frame_t * frame)
849{
Florin Coras31c99552019-03-01 13:00:58 -0800850 session_main_t *smm = vnet_get_session_main ();
Florin Coras2062ec02019-07-15 13:15:18 -0700851 u32 thread_index = vm->thread_index, n_to_dequeue;
Florin Coras31c99552019-03-01 13:00:58 -0800852 session_worker_t *wrk = &smm->wrk[thread_index];
Florin Coras2062ec02019-07-15 13:15:18 -0700853 session_evt_elt_t *elt, *new_he, *new_te, *pending_he;
854 session_evt_elt_t *disconnects_he, *postponed_he;
Florin Coras3c2fed52018-07-04 04:15:05 -0700855 svm_msg_q_msg_t _msg, *msg = &_msg;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700856 f64 now = vlib_time_now (vm);
Florin Coras3c2fed52018-07-04 04:15:05 -0700857 int n_tx_packets = 0, i, rv;
Florin Coras15531972018-08-12 23:50:53 -0700858 app_worker_t *app_wrk;
Florin Coras3c2fed52018-07-04 04:15:05 -0700859 svm_msg_q_t *mq;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700860 void (*fp) (void *);
861
Florin Corascca96182019-07-19 07:34:13 -0700862 SESSION_EVT (SESSION_EVT_DISPATCH_START, wrk);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700863
864 /*
865 * Update transport time
866 */
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700867 session_update_dispatch_period (wrk, now, thread_index);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700868 transport_update_time (now, thread_index);
869
Florin Coras5f56d732018-10-30 10:21:59 -0700870 /* Make sure postponed events are handled first */
Florin Coras2062ec02019-07-15 13:15:18 -0700871 new_he = pool_elt_at_index (wrk->event_elts, wrk->new_head);
872 new_te = clib_llist_prev (wrk->event_elts, evt_list, new_he);
873
874 postponed_he = pool_elt_at_index (wrk->event_elts, wrk->postponed_head);
875 clib_llist_splice (wrk->event_elts, evt_list, new_te, postponed_he);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700876
Florin Coras5f56d732018-10-30 10:21:59 -0700877 /* Try to dequeue what is available. Don't wait for lock.
878 * XXX: we may need priorities here */
879 mq = wrk->vpp_event_queue;
880 n_to_dequeue = svm_msg_q_size (mq);
881 if (n_to_dequeue && svm_msg_q_try_lock (mq) == 0)
882 {
883 for (i = 0; i < n_to_dequeue; i++)
884 {
Florin Coras2062ec02019-07-15 13:15:18 -0700885 elt = session_evt_elt_alloc (wrk);
Florin Coras5f56d732018-10-30 10:21:59 -0700886 svm_msg_q_sub_w_lock (mq, msg);
Florin Coras3c7d4f92018-12-14 11:28:43 -0800887 /* Works because reply messages are smaller than a session evt.
888 * If we ever need to support bigger messages this needs to be
889 * fixed */
Florin Coras2062ec02019-07-15 13:15:18 -0700890 clib_memcpy_fast (&elt->evt, svm_msg_q_msg_data (mq, msg),
891 sizeof (elt->evt));
Florin Coras5f56d732018-10-30 10:21:59 -0700892 svm_msg_q_free_msg (mq, msg);
Florin Coras2062ec02019-07-15 13:15:18 -0700893 new_he = pool_elt_at_index (wrk->event_elts, wrk->new_head);
894 clib_llist_add_tail (wrk->event_elts, evt_list, elt, new_he);
Florin Coras5f56d732018-10-30 10:21:59 -0700895 }
896 svm_msg_q_unlock (mq);
897 }
898
Florin Coras2062ec02019-07-15 13:15:18 -0700899 pending_he = pool_elt_at_index (wrk->event_elts, wrk->pending_head);
900 postponed_he = pool_elt_at_index (wrk->event_elts, wrk->postponed_head);
901 disconnects_he = pool_elt_at_index (wrk->event_elts, wrk->disconnects_head);
Florin Coras5f56d732018-10-30 10:21:59 -0700902
Florin Coras2062ec02019-07-15 13:15:18 -0700903 new_te = clib_llist_prev (wrk->event_elts, evt_list, new_he);
904 clib_llist_splice (wrk->event_elts, evt_list, new_te, pending_he);
905 new_te = clib_llist_prev (wrk->event_elts, evt_list, new_he);
906 clib_llist_splice (wrk->event_elts, evt_list, new_te, disconnects_he);
Florin Coras5f56d732018-10-30 10:21:59 -0700907
Florin Coras2062ec02019-07-15 13:15:18 -0700908 while (!clib_llist_is_empty (wrk->event_elts, evt_list, new_he))
Florin Coras0d60a0f2018-06-29 02:02:08 -0700909 {
Florin Coras2062ec02019-07-15 13:15:18 -0700910 clib_llist_index_t ei;
Florin Coras52207f12018-07-12 14:48:06 -0700911 session_event_t *e;
Florin Coras0e573f52019-05-07 16:28:16 -0700912 session_t *s;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700913
Florin Coras2062ec02019-07-15 13:15:18 -0700914 clib_llist_pop_first (wrk->event_elts, evt_list, elt, new_he);
915 ei = clib_llist_entry_index (wrk->event_elts, elt);
916 e = &elt->evt;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700917 switch (e->event_type)
918 {
Florin Coras42ceddb2018-12-12 10:56:01 -0800919 case SESSION_IO_EVT_TX_FLUSH:
Florin Corasf6c43132019-03-01 12:41:21 -0800920 case SESSION_IO_EVT_TX:
Florin Coras0d60a0f2018-06-29 02:02:08 -0700921 /* Don't try to send more that one frame per dispatch cycle */
922 if (n_tx_packets == VLIB_FRAME_SIZE)
923 {
Florin Coras2062ec02019-07-15 13:15:18 -0700924 session_evt_add_postponed (wrk, elt);
925 continue;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700926 }
927
928 s = session_event_get_session (e, thread_index);
929 if (PREDICT_FALSE (!s))
930 {
Florin Coras326b81e2018-10-04 19:03:05 -0700931 clib_warning ("session was freed!");
Florin Coras2062ec02019-07-15 13:15:18 -0700932 break;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700933 }
Florin Coras14ed6df2019-03-06 21:13:42 -0800934 CLIB_PREFETCH (s->tx_fifo, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
Florin Coras1bcad5c2019-01-09 20:04:38 -0800935 wrk->ctx.s = s;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700936 /* Spray packets in per session type frames, since they go to
937 * different nodes */
Florin Coras2062ec02019-07-15 13:15:18 -0700938 rv = (smm->session_tx_fns[s->session_type]) (vm, node, wrk, elt,
Florin Coras0d60a0f2018-06-29 02:02:08 -0700939 &n_tx_packets);
Florin Coras0e573f52019-05-07 16:28:16 -0700940 if (PREDICT_FALSE (rv == SESSION_TX_NO_BUFFERS))
Florin Coras0d60a0f2018-06-29 02:02:08 -0700941 {
942 vlib_node_increment_counter (vm, node->node_index,
943 SESSION_QUEUE_ERROR_NO_BUFFER, 1);
Florin Coras2062ec02019-07-15 13:15:18 -0700944 break;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700945 }
946 break;
Florin Coras317b8e02019-04-17 09:57:46 -0700947 case SESSION_IO_EVT_RX:
948 s = session_event_get_session (e, thread_index);
949 if (!s)
950 break;
951 transport_app_rx_evt (session_get_transport_proto (s),
952 s->connection_index, s->thread_index);
953 break;
Florin Corasf6c43132019-03-01 12:41:21 -0800954 case SESSION_CTRL_EVT_CLOSE:
Florin Coras3c7d4f92018-12-14 11:28:43 -0800955 s = session_get_from_handle_if_valid (e->session_handle);
956 if (PREDICT_FALSE (!s))
957 break;
958
Florin Corase96bf632018-12-18 22:44:27 -0800959 /* Make sure session disconnects run after the pending list is
960 * drained, i.e., postpone if the first time. If not the first
961 * and the tx queue is still not empty, try to wait for some
962 * dispatch cycles */
963 if (!e->postponed
Sirshak Das28aa5392019-02-05 01:33:33 -0600964 || (e->postponed < 200
965 && svm_fifo_max_dequeue_cons (s->tx_fifo)))
Florin Coras0d60a0f2018-06-29 02:02:08 -0700966 {
Florin Corase96bf632018-12-18 22:44:27 -0800967 e->postponed += 1;
Florin Coras2062ec02019-07-15 13:15:18 -0700968 session_evt_add_pending (wrk, elt);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700969 continue;
970 }
971
Florin Coras5a2ec8f2018-12-27 11:53:11 -0800972 session_transport_close (s);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700973 break;
Florin Corasf6c43132019-03-01 12:41:21 -0800974 case SESSION_IO_EVT_BUILTIN_RX:
Florin Coras0d60a0f2018-06-29 02:02:08 -0700975 s = session_event_get_session (e, thread_index);
Florin Corasef915342018-09-29 10:23:06 -0700976 if (PREDICT_FALSE (!s || s->session_state >= SESSION_STATE_CLOSING))
Florin Coras2062ec02019-07-15 13:15:18 -0700977 break;
Florin Coras288eaab2019-02-03 15:26:14 -0800978 svm_fifo_unset_event (s->rx_fifo);
Florin Coras15531972018-08-12 23:50:53 -0700979 app_wrk = app_worker_get (s->app_wrk_index);
Florin Corasbf7ce2c2019-03-06 14:44:42 -0800980 app_worker_builtin_rx (app_wrk, s);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700981 break;
Florin Corasf6c43132019-03-01 12:41:21 -0800982 case SESSION_IO_EVT_BUILTIN_TX:
Florin Corasef915342018-09-29 10:23:06 -0700983 s = session_get_from_handle_if_valid (e->session_handle);
Florin Coras1bcad5c2019-01-09 20:04:38 -0800984 wrk->ctx.s = s;
Florin Corasef915342018-09-29 10:23:06 -0700985 if (PREDICT_TRUE (s != 0))
Florin Coras2062ec02019-07-15 13:15:18 -0700986 session_tx_fifo_dequeue_internal (vm, node, wrk, elt,
Florin Coras1bcad5c2019-01-09 20:04:38 -0800987 &n_tx_packets);
Florin Corasef915342018-09-29 10:23:06 -0700988 break;
Florin Corasf6c43132019-03-01 12:41:21 -0800989 case SESSION_CTRL_EVT_RPC:
Florin Coras0d60a0f2018-06-29 02:02:08 -0700990 fp = e->rpc_args.fp;
991 (*fp) (e->rpc_args.arg);
992 break;
Florin Coras52207f12018-07-12 14:48:06 -0700993 case SESSION_CTRL_EVT_DISCONNECTED:
994 session_mq_disconnected_handler (e->data);
995 break;
996 case SESSION_CTRL_EVT_ACCEPTED_REPLY:
997 session_mq_accepted_reply_handler (e->data);
998 break;
999 case SESSION_CTRL_EVT_CONNECTED_REPLY:
1000 break;
1001 case SESSION_CTRL_EVT_DISCONNECTED_REPLY:
1002 session_mq_disconnected_reply_handler (e->data);
1003 break;
1004 case SESSION_CTRL_EVT_RESET_REPLY:
1005 session_mq_reset_reply_handler (e->data);
1006 break;
Florin Coras30e79c22019-01-02 19:31:22 -08001007 case SESSION_CTRL_EVT_WORKER_UPDATE:
1008 session_mq_worker_update_handler (e->data);
1009 break;
Florin Coras0d60a0f2018-06-29 02:02:08 -07001010 default:
1011 clib_warning ("unhandled event type %d", e->event_type);
1012 }
Florin Coras2062ec02019-07-15 13:15:18 -07001013
1014 /* Regrab elements in case pool moved */
1015 elt = pool_elt_at_index (wrk->event_elts, ei);
1016 if (!clib_llist_elt_is_linked (elt, evt_list))
1017 session_evt_elt_free (wrk, elt);
1018
1019 new_he = pool_elt_at_index (wrk->event_elts, wrk->new_head);
Florin Coras0d60a0f2018-06-29 02:02:08 -07001020 }
1021
Florin Corasc44a5582018-11-01 16:30:54 -07001022 wrk->last_tx_packets = n_tx_packets;
Florin Coras0d60a0f2018-06-29 02:02:08 -07001023
1024 vlib_node_increment_counter (vm, session_queue_node.index,
1025 SESSION_QUEUE_ERROR_TX, n_tx_packets);
1026
Florin Corascca96182019-07-19 07:34:13 -07001027 SESSION_EVT (SESSION_EVT_DISPATCH_END, wrk, n_tx_packets);
Florin Coras0d60a0f2018-06-29 02:02:08 -07001028
1029 return n_tx_packets;
1030}
1031
1032/* *INDENT-OFF* */
1033VLIB_REGISTER_NODE (session_queue_node) =
1034{
1035 .function = session_queue_node_fn,
1036 .name = "session-queue",
1037 .format_trace = format_session_queue_trace,
1038 .type = VLIB_NODE_TYPE_INPUT,
1039 .n_errors = ARRAY_LEN (session_queue_error_strings),
1040 .error_strings = session_queue_error_strings,
1041 .state = VLIB_NODE_STATE_DISABLED,
1042};
1043/* *INDENT-ON* */
1044
Dave Barachacd2a6a2017-05-16 17:41:34 -04001045void
1046dump_thread_0_event_queue (void)
1047{
Florin Coras31c99552019-03-01 13:00:58 -08001048 session_main_t *smm = vnet_get_session_main ();
Dave Barachacd2a6a2017-05-16 17:41:34 -04001049 vlib_main_t *vm = &vlib_global_main;
1050 u32 my_thread_index = vm->thread_index;
Florin Coras52207f12018-07-12 14:48:06 -07001051 session_event_t _e, *e = &_e;
Florin Coras3c2fed52018-07-04 04:15:05 -07001052 svm_msg_q_ring_t *ring;
Florin Coras288eaab2019-02-03 15:26:14 -08001053 session_t *s0;
Florin Coras3c2fed52018-07-04 04:15:05 -07001054 svm_msg_q_msg_t *msg;
1055 svm_msg_q_t *mq;
Dave Barachacd2a6a2017-05-16 17:41:34 -04001056 int i, index;
Dave Barachacd2a6a2017-05-16 17:41:34 -04001057
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001058 mq = smm->wrk[my_thread_index].vpp_event_queue;
Florin Coras3c2fed52018-07-04 04:15:05 -07001059 index = mq->q->head;
Dave Barachacd2a6a2017-05-16 17:41:34 -04001060
Florin Coras3c2fed52018-07-04 04:15:05 -07001061 for (i = 0; i < mq->q->cursize; i++)
Dave Barachacd2a6a2017-05-16 17:41:34 -04001062 {
Florin Coras3c2fed52018-07-04 04:15:05 -07001063 msg = (svm_msg_q_msg_t *) (&mq->q->data[0] + mq->q->elsize * index);
1064 ring = svm_msg_q_ring (mq, msg->ring_index);
Dave Barach178cf492018-11-13 16:34:13 -05001065 clib_memcpy_fast (e, svm_msg_q_msg_data (mq, msg), ring->elsize);
Dave Barachacd2a6a2017-05-16 17:41:34 -04001066
1067 switch (e->event_type)
1068 {
Florin Corasf6c43132019-03-01 12:41:21 -08001069 case SESSION_IO_EVT_TX:
Dave Barachacd2a6a2017-05-16 17:41:34 -04001070 s0 = session_event_get_session (e, my_thread_index);
1071 fformat (stdout, "[%04d] TX session %d\n", i, s0->session_index);
1072 break;
1073
Florin Corasf6c43132019-03-01 12:41:21 -08001074 case SESSION_CTRL_EVT_CLOSE:
Florin Corascea194d2017-10-02 00:18:51 -07001075 s0 = session_get_from_handle (e->session_handle);
Dave Barachacd2a6a2017-05-16 17:41:34 -04001076 fformat (stdout, "[%04d] disconnect session %d\n", i,
1077 s0->session_index);
1078 break;
1079
Florin Corasf6c43132019-03-01 12:41:21 -08001080 case SESSION_IO_EVT_BUILTIN_RX:
Dave Barachacd2a6a2017-05-16 17:41:34 -04001081 s0 = session_event_get_session (e, my_thread_index);
1082 fformat (stdout, "[%04d] builtin_rx %d\n", i, s0->session_index);
1083 break;
1084
Florin Corasf6c43132019-03-01 12:41:21 -08001085 case SESSION_CTRL_EVT_RPC:
Dave Barachacd2a6a2017-05-16 17:41:34 -04001086 fformat (stdout, "[%04d] RPC call %llx with %llx\n",
David Johnsond9818dd2018-12-14 14:53:41 -05001087 i, (u64) (uword) (e->rpc_args.fp),
1088 (u64) (uword) (e->rpc_args.arg));
Dave Barachacd2a6a2017-05-16 17:41:34 -04001089 break;
1090
1091 default:
1092 fformat (stdout, "[%04d] unhandled event type %d\n",
1093 i, e->event_type);
1094 break;
1095 }
1096
1097 index++;
1098
Florin Coras3c2fed52018-07-04 04:15:05 -07001099 if (index == mq->q->maxsize)
Dave Barachacd2a6a2017-05-16 17:41:34 -04001100 index = 0;
1101 }
1102}
1103
Florin Coras6534b7a2017-07-18 05:38:03 -04001104static u8
Florin Coras52207f12018-07-12 14:48:06 -07001105session_node_cmp_event (session_event_t * e, svm_fifo_t * f)
Florin Coras6534b7a2017-07-18 05:38:03 -04001106{
Florin Coras288eaab2019-02-03 15:26:14 -08001107 session_t *s;
Florin Coras6534b7a2017-07-18 05:38:03 -04001108 switch (e->event_type)
1109 {
Florin Corasf6c43132019-03-01 12:41:21 -08001110 case SESSION_IO_EVT_RX:
1111 case SESSION_IO_EVT_TX:
1112 case SESSION_IO_EVT_BUILTIN_RX:
Florin Corasc0737e92019-03-04 14:19:39 -08001113 if (e->session_index == f->master_session_index)
Florin Coras6534b7a2017-07-18 05:38:03 -04001114 return 1;
1115 break;
Florin Corasf6c43132019-03-01 12:41:21 -08001116 case SESSION_CTRL_EVT_CLOSE:
Florin Coras6534b7a2017-07-18 05:38:03 -04001117 break;
Florin Corasf6c43132019-03-01 12:41:21 -08001118 case SESSION_CTRL_EVT_RPC:
Florin Corascea194d2017-10-02 00:18:51 -07001119 s = session_get_from_handle (e->session_handle);
Florin Coras6534b7a2017-07-18 05:38:03 -04001120 if (!s)
1121 {
1122 clib_warning ("session has event but doesn't exist!");
1123 break;
1124 }
Florin Coras288eaab2019-02-03 15:26:14 -08001125 if (s->rx_fifo == f || s->tx_fifo == f)
Florin Coras6534b7a2017-07-18 05:38:03 -04001126 return 1;
1127 break;
1128 default:
1129 break;
1130 }
1131 return 0;
1132}
1133
1134u8
Florin Coras52207f12018-07-12 14:48:06 -07001135session_node_lookup_fifo_event (svm_fifo_t * f, session_event_t * e)
Florin Coras6534b7a2017-07-18 05:38:03 -04001136{
Florin Coras2062ec02019-07-15 13:15:18 -07001137 session_evt_elt_t *elt;
Florin Coras31c99552019-03-01 13:00:58 -08001138 session_worker_t *wrk;
Florin Coras6534b7a2017-07-18 05:38:03 -04001139 int i, index, found = 0;
Florin Coras3c2fed52018-07-04 04:15:05 -07001140 svm_msg_q_msg_t *msg;
1141 svm_msg_q_ring_t *ring;
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001142 svm_msg_q_t *mq;
Florin Coras6534b7a2017-07-18 05:38:03 -04001143 u8 thread_index;
1144
1145 ASSERT (e);
1146 thread_index = f->master_thread_index;
Florin Coras31c99552019-03-01 13:00:58 -08001147 wrk = session_main_get_worker (thread_index);
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001148
Florin Coras6534b7a2017-07-18 05:38:03 -04001149 /*
1150 * Search evt queue
1151 */
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001152 mq = wrk->vpp_event_queue;
Florin Coras3c2fed52018-07-04 04:15:05 -07001153 index = mq->q->head;
1154 for (i = 0; i < mq->q->cursize; i++)
Florin Coras6534b7a2017-07-18 05:38:03 -04001155 {
Florin Coras3c2fed52018-07-04 04:15:05 -07001156 msg = (svm_msg_q_msg_t *) (&mq->q->data[0] + mq->q->elsize * index);
1157 ring = svm_msg_q_ring (mq, msg->ring_index);
Dave Barach178cf492018-11-13 16:34:13 -05001158 clib_memcpy_fast (e, svm_msg_q_msg_data (mq, msg), ring->elsize);
Florin Coras6534b7a2017-07-18 05:38:03 -04001159 found = session_node_cmp_event (e, f);
1160 if (found)
Florin Coras371ca502018-02-21 12:07:41 -08001161 return 1;
Florin Coras3c2fed52018-07-04 04:15:05 -07001162 if (++index == mq->q->maxsize)
Florin Coras6534b7a2017-07-18 05:38:03 -04001163 index = 0;
1164 }
1165 /*
1166 * Search pending events vector
1167 */
Florin Coras2062ec02019-07-15 13:15:18 -07001168
1169 /* *INDENT-OFF* */
1170 clib_llist_foreach (wrk->event_elts, evt_list,
1171 session_evt_pending_head (wrk), elt, ({
1172 found = session_node_cmp_event (&elt->evt, f);
Florin Coras6534b7a2017-07-18 05:38:03 -04001173 if (found)
1174 {
Florin Coras2062ec02019-07-15 13:15:18 -07001175 clib_memcpy_fast (e, &elt->evt, sizeof (*e));
Florin Coras6534b7a2017-07-18 05:38:03 -04001176 break;
1177 }
Florin Coras2062ec02019-07-15 13:15:18 -07001178
1179 }));
1180 /* *INDENT-ON* */
1181
Florin Coras6534b7a2017-07-18 05:38:03 -04001182 return found;
1183}
1184
Dave Barach2a863912017-11-28 10:11:42 -05001185static clib_error_t *
1186session_queue_exit (vlib_main_t * vm)
1187{
1188 if (vec_len (vlib_mains) < 2)
1189 return 0;
1190
1191 /*
1192 * Shut off (especially) worker-thread session nodes.
1193 * Otherwise, vpp can crash as the main thread unmaps the
1194 * API segment.
1195 */
1196 vlib_worker_thread_barrier_sync (vm);
1197 session_node_enable_disable (0 /* is_enable */ );
1198 vlib_worker_thread_barrier_release (vm);
1199 return 0;
1200}
1201
1202VLIB_MAIN_LOOP_EXIT_FUNCTION (session_queue_exit);
1203
Florin Corasfd542f12018-05-16 19:28:24 -07001204static uword
1205session_queue_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
1206 vlib_frame_t * f)
1207{
1208 f64 now, timeout = 1.0;
1209 uword *event_data = 0;
1210 uword event_type;
1211
1212 while (1)
1213 {
1214 vlib_process_wait_for_event_or_clock (vm, timeout);
1215 now = vlib_time_now (vm);
1216 event_type = vlib_process_get_events (vm, (uword **) & event_data);
1217
1218 switch (event_type)
1219 {
1220 case SESSION_Q_PROCESS_FLUSH_FRAMES:
1221 /* Flush the frames by updating all transports times */
1222 transport_update_time (now, 0);
1223 break;
1224 case SESSION_Q_PROCESS_STOP:
1225 timeout = 100000.0;
1226 break;
1227 case ~0:
1228 /* Timed out. Update time for all transports to trigger all
1229 * outstanding retransmits. */
1230 transport_update_time (now, 0);
1231 break;
1232 }
1233 vec_reset_length (event_data);
1234 }
1235 return 0;
1236}
1237
1238/* *INDENT-OFF* */
1239VLIB_REGISTER_NODE (session_queue_process_node) =
1240{
1241 .function = session_queue_process,
1242 .type = VLIB_NODE_TYPE_PROCESS,
1243 .name = "session-queue-process",
1244 .state = VLIB_NODE_STATE_DISABLED,
1245};
1246/* *INDENT-ON* */
1247
Florin Coras653e43f2019-03-04 10:56:23 -08001248static_always_inline uword
1249session_queue_pre_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1250 vlib_frame_t * frame)
1251{
1252 session_main_t *sm = &session_main;
1253 if (!sm->wrk[0].vpp_event_queue)
1254 return 0;
1255 return session_queue_node_fn (vm, node, frame);
1256}
1257
1258/* *INDENT-OFF* */
1259VLIB_REGISTER_NODE (session_queue_pre_input_node) =
1260{
1261 .function = session_queue_pre_input_inline,
1262 .type = VLIB_NODE_TYPE_PRE_INPUT,
1263 .name = "session-queue-main",
1264 .state = VLIB_NODE_STATE_DISABLED,
1265};
1266/* *INDENT-ON* */
1267
Dave Barach68b0fb02017-02-28 15:15:56 -05001268/*
1269 * fd.io coding-style-patch-verification: ON
1270 *
1271 * Local Variables:
1272 * eval: (c-set-style "gnu")
1273 * End:
1274 */