blob: 58d31ccb87cf685c6f0f97bdac6483ace21761be [file] [log] [blame]
Dave Barach68b0fb02017-02-28 15:15:56 -05001/*
2 * Copyright (c) 2017 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
Florin Coras6792ec02017-03-13 03:49:51 -070016#include <math.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050017#include <vlib/vlib.h>
18#include <vnet/vnet.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050019#include <vppinfra/elog.h>
Florin Coras561af9b2017-12-09 10:19:43 -080020#include <vnet/session/transport.h>
Florin Coras8e43d042018-05-04 15:46:57 -070021#include <vnet/session/session.h>
Florin Coras6792ec02017-03-13 03:49:51 -070022#include <vnet/session/application.h>
Florin Coras52207f12018-07-12 14:48:06 -070023#include <vnet/session/application_interface.h>
Florin Corase69f4952017-03-07 10:06:24 -080024#include <vnet/session/session_debug.h>
Florin Corase86a8ed2018-01-05 03:20:25 -080025#include <svm/queue.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050026
Florin Coras52207f12018-07-12 14:48:06 -070027static void
28session_mq_accepted_reply_handler (void *data)
29{
30 session_accepted_reply_msg_t *mp = (session_accepted_reply_msg_t *) data;
31 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
Florin Coras21795132018-09-09 09:40:51 -070032 app_worker_t *app_wrk;
Florin Coras52207f12018-07-12 14:48:06 -070033 local_session_t *ls;
34 stream_session_t *s;
35
36 /* Server isn't interested, kill the session */
37 if (mp->retval)
38 {
39 a->app_index = mp->context;
40 a->handle = mp->handle;
41 vnet_disconnect_session (a);
42 return;
43 }
44
45 if (session_handle_is_local (mp->handle))
46 {
47 ls = application_get_local_session_from_handle (mp->handle);
Florin Corasab2f6db2018-08-31 14:31:41 -070048 if (!ls)
Florin Coras52207f12018-07-12 14:48:06 -070049 {
Florin Corasab2f6db2018-08-31 14:31:41 -070050 clib_warning ("unknown local handle 0x%lx", mp->handle);
51 return;
52 }
53 app_wrk = app_worker_get (ls->app_wrk_index);
Florin Coras21795132018-09-09 09:40:51 -070054 if (app_wrk->app_index != mp->context)
Florin Corasab2f6db2018-08-31 14:31:41 -070055 {
56 clib_warning ("server %u doesn't own local handle 0x%lx",
Florin Coras52207f12018-07-12 14:48:06 -070057 mp->context, mp->handle);
58 return;
59 }
60 if (application_local_session_connect_notify (ls))
61 return;
62 ls->session_state = SESSION_STATE_READY;
63 }
64 else
65 {
66 s = session_get_from_handle_if_valid (mp->handle);
Florin Corase2ea1932018-12-17 23:08:14 -080067 /* Closed while waiting for app to reply */
68 if (!s || s->session_state > SESSION_STATE_READY)
69 return;
Florin Coras21795132018-09-09 09:40:51 -070070 app_wrk = app_worker_get (s->app_wrk_index);
71 if (app_wrk->app_index != mp->context)
Florin Coras52207f12018-07-12 14:48:06 -070072 {
73 clib_warning ("app doesn't own session");
74 return;
75 }
76 s->session_state = SESSION_STATE_READY;
Florin Coras84099552018-07-22 12:59:30 -070077 if (!svm_fifo_is_empty (s->server_rx_fifo))
Florin Coras21795132018-09-09 09:40:51 -070078 app_worker_lock_and_send_event (app_wrk, s, FIFO_EVENT_APP_RX);
Florin Coras52207f12018-07-12 14:48:06 -070079 }
80}
81
82static void
83session_mq_reset_reply_handler (void *data)
84{
Florin Coras4af830c2018-12-04 09:21:36 -080085 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
Florin Coras52207f12018-07-12 14:48:06 -070086 session_reset_reply_msg_t *mp;
Florin Coras15531972018-08-12 23:50:53 -070087 app_worker_t *app_wrk;
Florin Coras52207f12018-07-12 14:48:06 -070088 stream_session_t *s;
Florin Coras15531972018-08-12 23:50:53 -070089 application_t *app;
Florin Coras52207f12018-07-12 14:48:06 -070090 u32 index, thread_index;
91
92 mp = (session_reset_reply_msg_t *) data;
Florin Coras3c7d4f92018-12-14 11:28:43 -080093 app = application_lookup (mp->context);
Florin Coras52207f12018-07-12 14:48:06 -070094 if (!app)
95 return;
96
97 session_parse_handle (mp->handle, &index, &thread_index);
98 s = session_get_if_valid (index, thread_index);
Florin Coras3c7d4f92018-12-14 11:28:43 -080099
100 /* Session was already closed or already cleaned up */
101 if (!s || s->session_state != SESSION_STATE_TRANSPORT_CLOSING)
102 return;
103
Florin Coras15531972018-08-12 23:50:53 -0700104 app_wrk = app_worker_get (s->app_wrk_index);
105 if (!app_wrk || app_wrk->app_index != app->app_index)
106 {
107 clib_warning ("App % does not own handle 0x%lx!", app->app_index,
108 mp->handle);
109 return;
110 }
Florin Coras52207f12018-07-12 14:48:06 -0700111
112 /* Client objected to resetting the session, log and continue */
113 if (mp->retval)
114 {
115 clib_warning ("client retval %d", mp->retval);
116 return;
117 }
118
119 /* This comes as a response to a reset, transport only waiting for
120 * confirmation to remove connection state, no need to disconnect */
Florin Coras4af830c2018-12-04 09:21:36 -0800121 a->handle = mp->handle;
122 a->app_index = app->app_index;
123 vnet_disconnect_session (a);
Florin Coras52207f12018-07-12 14:48:06 -0700124}
125
126static void
127session_mq_disconnected_handler (void *data)
128{
129 session_disconnected_reply_msg_t *rmp;
130 vnet_disconnect_args_t _a, *a = &_a;
131 svm_msg_q_msg_t _msg, *msg = &_msg;
132 session_disconnected_msg_t *mp;
Florin Coras15531972018-08-12 23:50:53 -0700133 app_worker_t *app_wrk;
Florin Coras52207f12018-07-12 14:48:06 -0700134 session_event_t *evt;
Florin Coras3d0fadc2018-07-17 05:35:47 -0700135 stream_session_t *s;
Florin Coras52207f12018-07-12 14:48:06 -0700136 application_t *app;
137 int rv = 0;
138
139 mp = (session_disconnected_msg_t *) data;
Florin Corasc3638fe2018-08-24 13:58:49 -0700140 if (!(s = session_get_from_handle_if_valid (mp->handle)))
141 {
142 clib_warning ("could not disconnect handle %llu", mp->handle);
143 return;
144 }
Florin Coras15531972018-08-12 23:50:53 -0700145 app_wrk = app_worker_get (s->app_wrk_index);
146 app = application_lookup (mp->client_index);
Florin Corasc3638fe2018-08-24 13:58:49 -0700147 if (!(app_wrk && app && app->app_index == app_wrk->app_index))
Florin Coras52207f12018-07-12 14:48:06 -0700148 {
Florin Corasc3638fe2018-08-24 13:58:49 -0700149 clib_warning ("could not disconnect session: %llu app: %u",
Florin Coras15531972018-08-12 23:50:53 -0700150 mp->handle, mp->client_index);
Florin Coras3d0fadc2018-07-17 05:35:47 -0700151 return;
Florin Coras52207f12018-07-12 14:48:06 -0700152 }
Florin Coras3d0fadc2018-07-17 05:35:47 -0700153
154 a->handle = mp->handle;
Florin Coras15531972018-08-12 23:50:53 -0700155 a->app_index = app_wrk->wrk_index;
Florin Coras3d0fadc2018-07-17 05:35:47 -0700156 rv = vnet_disconnect_session (a);
Florin Coras52207f12018-07-12 14:48:06 -0700157
Florin Coras15531972018-08-12 23:50:53 -0700158 svm_msg_q_lock_and_alloc_msg_w_ring (app_wrk->event_queue,
Florin Coras52207f12018-07-12 14:48:06 -0700159 SESSION_MQ_CTRL_EVT_RING,
160 SVM_Q_WAIT, msg);
Florin Coras15531972018-08-12 23:50:53 -0700161 svm_msg_q_unlock (app_wrk->event_queue);
162 evt = svm_msg_q_msg_data (app_wrk->event_queue, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400163 clib_memset (evt, 0, sizeof (*evt));
Florin Coras52207f12018-07-12 14:48:06 -0700164 evt->event_type = SESSION_CTRL_EVT_DISCONNECTED;
165 rmp = (session_disconnected_reply_msg_t *) evt->data;
166 rmp->handle = mp->handle;
167 rmp->context = mp->context;
168 rmp->retval = rv;
Florin Coras15531972018-08-12 23:50:53 -0700169 svm_msg_q_add (app_wrk->event_queue, msg, SVM_Q_WAIT);
Florin Coras52207f12018-07-12 14:48:06 -0700170}
171
172static void
173session_mq_disconnected_reply_handler (void *data)
174{
175 session_disconnected_reply_msg_t *mp;
176 vnet_disconnect_args_t _a, *a = &_a;
177 application_t *app;
178
179 mp = (session_disconnected_reply_msg_t *) data;
180
181 /* Client objected to disconnecting the session, log and continue */
182 if (mp->retval)
183 {
184 clib_warning ("client retval %d", mp->retval);
185 return;
186 }
187
188 /* Disconnect has been confirmed. Confirm close to transport */
189 app = application_lookup (mp->context);
190 if (app)
191 {
192 a->handle = mp->handle;
Florin Coras15531972018-08-12 23:50:53 -0700193 a->app_index = app->app_index;
Florin Coras52207f12018-07-12 14:48:06 -0700194 vnet_disconnect_session (a);
195 }
196}
197
Dave Barach68b0fb02017-02-28 15:15:56 -0500198vlib_node_registration_t session_queue_node;
199
200typedef struct
201{
202 u32 session_index;
203 u32 server_thread_index;
204} session_queue_trace_t;
205
206/* packet trace format function */
207static u8 *
208format_session_queue_trace (u8 * s, va_list * args)
209{
210 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
211 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
212 session_queue_trace_t *t = va_arg (*args, session_queue_trace_t *);
213
214 s = format (s, "SESSION_QUEUE: session index %d, server thread index %d",
215 t->session_index, t->server_thread_index);
216 return s;
217}
218
Florin Coras6792ec02017-03-13 03:49:51 -0700219#define foreach_session_queue_error \
220_(TX, "Packets transmitted") \
Florin Coras93992a92017-05-24 18:03:56 -0700221_(TIMER, "Timer events") \
222_(NO_BUFFER, "Out of buffers")
Dave Barach68b0fb02017-02-28 15:15:56 -0500223
224typedef enum
225{
226#define _(sym,str) SESSION_QUEUE_ERROR_##sym,
227 foreach_session_queue_error
228#undef _
229 SESSION_QUEUE_N_ERROR,
230} session_queue_error_t;
231
232static char *session_queue_error_strings[] = {
233#define _(sym,string) string,
234 foreach_session_queue_error
235#undef _
236};
237
Florin Coras0d60a0f2018-06-29 02:02:08 -0700238enum
239{
240 SESSION_TX_NO_BUFFERS = -2,
241 SESSION_TX_NO_DATA,
242 SESSION_TX_OK
243};
244
Florin Coras66cf5e02018-06-08 09:17:39 -0700245static void
246session_tx_trace_frame (vlib_main_t * vm, vlib_node_runtime_t * node,
247 u32 next_index, u32 * to_next, u16 n_segs,
248 stream_session_t * s, u32 n_trace)
Florin Corasf6d68ed2017-05-07 19:12:02 -0700249{
Florin Coras8e43d042018-05-04 15:46:57 -0700250 session_queue_trace_t *t;
Florin Coras66cf5e02018-06-08 09:17:39 -0700251 vlib_buffer_t *b;
252 int i;
253
254 for (i = 0; i < clib_min (n_trace, n_segs); i++)
255 {
256 b = vlib_get_buffer (vm, to_next[i - n_segs]);
257 vlib_trace_buffer (vm, node, next_index, b, 1 /* follow_chain */ );
Florin Coras66cf5e02018-06-08 09:17:39 -0700258 t = vlib_add_trace (vm, node, b, sizeof (*t));
259 t->session_index = s->session_index;
260 t->server_thread_index = s->thread_index;
261 }
Florin Coras4df38712018-06-20 12:44:16 -0700262 vlib_set_trace_count (vm, node, n_trace - i);
Florin Coras8e43d042018-05-04 15:46:57 -0700263}
Florin Corasf6d68ed2017-05-07 19:12:02 -0700264
Florin Coras8e43d042018-05-04 15:46:57 -0700265always_inline void
266session_tx_fifo_chain_tail (vlib_main_t * vm, session_tx_context_t * ctx,
267 vlib_buffer_t * b, u16 * n_bufs, u8 peek_data)
268{
Florin Coras8e43d042018-05-04 15:46:57 -0700269 vlib_buffer_t *chain_b, *prev_b;
270 u32 chain_bi0, to_deq, left_from_seg;
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700271 session_manager_worker_t *wrk;
Florin Coras8e43d042018-05-04 15:46:57 -0700272 u16 len_to_deq, n_bytes_read;
273 u8 *data, j;
Florin Corasb2215d62017-08-01 16:56:58 -0700274
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700275 wrk = session_manager_get_worker (ctx->s->thread_index);
Florin Coras8e43d042018-05-04 15:46:57 -0700276 b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
277 b->total_length_not_including_first_buffer = 0;
278
279 chain_b = b;
280 left_from_seg = clib_min (ctx->snd_mss - b->current_length,
281 ctx->left_to_snd);
Florin Corasb2215d62017-08-01 16:56:58 -0700282 to_deq = left_from_seg;
Florin Coras8e43d042018-05-04 15:46:57 -0700283 for (j = 1; j < ctx->n_bufs_per_seg; j++)
Florin Corasf6d68ed2017-05-07 19:12:02 -0700284 {
Florin Coras8e43d042018-05-04 15:46:57 -0700285 prev_b = chain_b;
286 len_to_deq = clib_min (to_deq, ctx->deq_per_buf);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700287
288 *n_bufs -= 1;
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700289 chain_bi0 = wrk->tx_buffers[*n_bufs];
290 _vec_len (wrk->tx_buffers) = *n_bufs;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700291
Florin Coras8e43d042018-05-04 15:46:57 -0700292 chain_b = vlib_get_buffer (vm, chain_bi0);
293 chain_b->current_data = 0;
294 data = vlib_buffer_get_current (chain_b);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700295 if (peek_data)
296 {
Florin Coras8e43d042018-05-04 15:46:57 -0700297 n_bytes_read = svm_fifo_peek (ctx->s->server_tx_fifo,
298 ctx->tx_offset, len_to_deq, data);
299 ctx->tx_offset += n_bytes_read;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700300 }
301 else
302 {
Florin Coras8e43d042018-05-04 15:46:57 -0700303 if (ctx->transport_vft->tx_type == TRANSPORT_TX_DGRAM)
Florin Coras7fb0fe12018-04-09 09:24:52 -0700304 {
Florin Coras8e43d042018-05-04 15:46:57 -0700305 svm_fifo_t *f = ctx->s->server_tx_fifo;
306 session_dgram_hdr_t *hdr = &ctx->hdr;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700307 u16 deq_now;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700308 deq_now = clib_min (hdr->data_length - hdr->data_offset,
Florin Coras8e43d042018-05-04 15:46:57 -0700309 len_to_deq);
310 n_bytes_read = svm_fifo_peek (f, hdr->data_offset, deq_now,
311 data);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700312 ASSERT (n_bytes_read > 0);
313
314 hdr->data_offset += n_bytes_read;
315 if (hdr->data_offset == hdr->data_length)
shubing guoaea5f392018-08-30 13:29:49 +0800316 {
317 u32 offset = hdr->data_length + SESSION_CONN_HDR_LEN;
318 svm_fifo_dequeue_drop (f, offset);
319 }
Florin Coras7fb0fe12018-04-09 09:24:52 -0700320 }
321 else
Florin Coras8e43d042018-05-04 15:46:57 -0700322 n_bytes_read = svm_fifo_dequeue_nowait (ctx->s->server_tx_fifo,
323 len_to_deq, data);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700324 }
Florin Coras8e43d042018-05-04 15:46:57 -0700325 ASSERT (n_bytes_read == len_to_deq);
326 chain_b->current_length = n_bytes_read;
327 b->total_length_not_including_first_buffer += chain_b->current_length;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700328
329 /* update previous buffer */
Florin Coras8e43d042018-05-04 15:46:57 -0700330 prev_b->next_buffer = chain_bi0;
331 prev_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700332
333 /* update current buffer */
Florin Coras8e43d042018-05-04 15:46:57 -0700334 chain_b->next_buffer = 0;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700335
Florin Corasb2215d62017-08-01 16:56:58 -0700336 to_deq -= n_bytes_read;
337 if (to_deq == 0)
Florin Corasf6d68ed2017-05-07 19:12:02 -0700338 break;
339 }
Florin Coras1f152cd2017-08-18 19:28:03 -0700340 ASSERT (to_deq == 0
Florin Coras8e43d042018-05-04 15:46:57 -0700341 && b->total_length_not_including_first_buffer == left_from_seg);
342 ctx->left_to_snd -= left_from_seg;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700343}
344
Dave Barach68b0fb02017-02-28 15:15:56 -0500345always_inline int
Florin Coras8e43d042018-05-04 15:46:57 -0700346session_output_try_get_buffers (vlib_main_t * vm,
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700347 session_manager_worker_t * wrk,
Florin Coras8e43d042018-05-04 15:46:57 -0700348 u32 thread_index, u16 * n_bufs, u32 wanted)
Dave Barach68b0fb02017-02-28 15:15:56 -0500349{
Florin Corasf08f26d2018-05-10 13:20:47 -0700350 u32 n_alloc;
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700351 vec_validate_aligned (wrk->tx_buffers, wanted - 1, CLIB_CACHE_LINE_BYTES);
352 n_alloc = vlib_buffer_alloc (vm, &wrk->tx_buffers[*n_bufs],
Florin Corasf08f26d2018-05-10 13:20:47 -0700353 wanted - *n_bufs);
354 *n_bufs += n_alloc;
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700355 _vec_len (wrk->tx_buffers) = *n_bufs;
Florin Corasf08f26d2018-05-10 13:20:47 -0700356 return n_alloc;
Florin Coras8e43d042018-05-04 15:46:57 -0700357}
358
359always_inline void
360session_tx_fill_buffer (vlib_main_t * vm, session_tx_context_t * ctx,
361 vlib_buffer_t * b, u16 * n_bufs, u8 peek_data)
362{
363 u32 len_to_deq;
364 u8 *data0;
365 int n_bytes_read;
366
367 /*
368 * Start with the first buffer in chain
369 */
370 b->error = 0;
371 b->flags = VNET_BUFFER_F_LOCALLY_ORIGINATED;
372 b->current_data = 0;
Florin Coras8e43d042018-05-04 15:46:57 -0700373
374 data0 = vlib_buffer_make_headroom (b, MAX_HDRS_LEN);
375 len_to_deq = clib_min (ctx->left_to_snd, ctx->deq_per_first_buf);
376
Florin Coras7fb0fe12018-04-09 09:24:52 -0700377 if (peek_data)
378 {
Florin Coras8e43d042018-05-04 15:46:57 -0700379 n_bytes_read = svm_fifo_peek (ctx->s->server_tx_fifo, ctx->tx_offset,
380 len_to_deq, data0);
381 ASSERT (n_bytes_read > 0);
382 /* Keep track of progress locally, transport is also supposed to
383 * increment it independently when pushing the header */
384 ctx->tx_offset += n_bytes_read;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700385 }
386 else
387 {
Florin Coras8e43d042018-05-04 15:46:57 -0700388 if (ctx->transport_vft->tx_type == TRANSPORT_TX_DGRAM)
389 {
390 session_dgram_hdr_t *hdr = &ctx->hdr;
391 svm_fifo_t *f = ctx->s->server_tx_fifo;
392 u16 deq_now;
393 u32 offset;
394
395 ASSERT (hdr->data_length > hdr->data_offset);
396 deq_now = clib_min (hdr->data_length - hdr->data_offset,
397 len_to_deq);
398 offset = hdr->data_offset + SESSION_CONN_HDR_LEN;
399 n_bytes_read = svm_fifo_peek (f, offset, deq_now, data0);
400 ASSERT (n_bytes_read > 0);
401
402 if (ctx->s->session_state == SESSION_STATE_LISTENING)
403 {
404 ip_copy (&ctx->tc->rmt_ip, &hdr->rmt_ip, ctx->tc->is_ip4);
405 ctx->tc->rmt_port = hdr->rmt_port;
406 }
407 hdr->data_offset += n_bytes_read;
408 if (hdr->data_offset == hdr->data_length)
409 {
410 offset = hdr->data_length + SESSION_CONN_HDR_LEN;
411 svm_fifo_dequeue_drop (f, offset);
412 }
413 }
Florin Coras7fb0fe12018-04-09 09:24:52 -0700414 else
415 {
Florin Coras8e43d042018-05-04 15:46:57 -0700416 n_bytes_read = svm_fifo_dequeue_nowait (ctx->s->server_tx_fifo,
417 len_to_deq, data0);
418 ASSERT (n_bytes_read > 0);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700419 }
420 }
Florin Coras8e43d042018-05-04 15:46:57 -0700421 b->current_length = n_bytes_read;
422 ctx->left_to_snd -= n_bytes_read;
Dave Barach68b0fb02017-02-28 15:15:56 -0500423
Florin Coras8e43d042018-05-04 15:46:57 -0700424 /*
425 * Fill in the remaining buffers in the chain, if any
426 */
427 if (PREDICT_FALSE (ctx->n_bufs_per_seg > 1 && ctx->left_to_snd))
428 session_tx_fifo_chain_tail (vm, ctx, b, n_bufs, peek_data);
Dave Barach68b0fb02017-02-28 15:15:56 -0500429
Florin Coras8e43d042018-05-04 15:46:57 -0700430 /* *INDENT-OFF* */
Florin Coras8b20bf52018-06-14 14:55:50 -0700431 SESSION_EVT_DBG(SESSION_EVT_DEQ, ctx->s, ({
432 ed->data[0] = FIFO_EVENT_APP_TX;
433 ed->data[1] = ctx->max_dequeue;
Florin Coras8e43d042018-05-04 15:46:57 -0700434 ed->data[2] = len_to_deq;
Florin Coras8b20bf52018-06-14 14:55:50 -0700435 ed->data[3] = ctx->left_to_snd;
Florin Coras8e43d042018-05-04 15:46:57 -0700436 }));
437 /* *INDENT-ON* */
438}
439
440always_inline u8
441session_tx_not_ready (stream_session_t * s, u8 peek_data)
442{
443 if (peek_data)
Florin Corase04c2992017-03-01 08:17:34 -0800444 {
Florin Coras8e43d042018-05-04 15:46:57 -0700445 /* Can retransmit for closed sessions but can't send new data if
446 * session is not ready or closed */
447 if (s->session_state < SESSION_STATE_READY)
448 return 1;
Florin Corasca1c8f32018-05-23 21:01:30 -0700449 if (s->session_state == SESSION_STATE_CLOSED)
450 return 2;
Florin Corase04c2992017-03-01 08:17:34 -0800451 }
Florin Coras8e43d042018-05-04 15:46:57 -0700452 return 0;
453}
Dave Barach68b0fb02017-02-28 15:15:56 -0500454
Florin Coras8e43d042018-05-04 15:46:57 -0700455always_inline transport_connection_t *
456session_tx_get_transport (session_tx_context_t * ctx, u8 peek_data)
457{
458 if (peek_data)
459 {
460 return ctx->transport_vft->get_connection (ctx->s->connection_index,
461 ctx->s->thread_index);
462 }
463 else
464 {
465 if (ctx->s->session_state == SESSION_STATE_LISTENING)
466 return ctx->transport_vft->get_listener (ctx->s->connection_index);
467 else
468 {
469 return ctx->transport_vft->get_connection (ctx->s->connection_index,
470 ctx->s->thread_index);
471 }
472 }
473}
Florin Coras6a9b68b2017-11-21 04:20:42 -0800474
Florin Coras8e43d042018-05-04 15:46:57 -0700475always_inline void
476session_tx_set_dequeue_params (vlib_main_t * vm, session_tx_context_t * ctx,
Florin Corasf08f26d2018-05-10 13:20:47 -0700477 u32 max_segs, u8 peek_data)
Florin Coras8e43d042018-05-04 15:46:57 -0700478{
479 u32 n_bytes_per_buf, n_bytes_per_seg;
480 ctx->max_dequeue = svm_fifo_max_dequeue (ctx->s->server_tx_fifo);
Dave Barach68b0fb02017-02-28 15:15:56 -0500481 if (peek_data)
482 {
Florin Coras9d063042017-09-14 03:08:00 -0400483 /* Offset in rx fifo from where to peek data */
Florin Coras8e43d042018-05-04 15:46:57 -0700484 ctx->tx_offset = ctx->transport_vft->tx_fifo_offset (ctx->tc);
485 if (PREDICT_FALSE (ctx->tx_offset >= ctx->max_dequeue))
486 {
487 ctx->max_len_to_snd = 0;
488 return;
489 }
490 ctx->max_dequeue -= ctx->tx_offset;
Dave Barach68b0fb02017-02-28 15:15:56 -0500491 }
Florin Coras7fb0fe12018-04-09 09:24:52 -0700492 else
493 {
Florin Coras8e43d042018-05-04 15:46:57 -0700494 if (ctx->transport_vft->tx_type == TRANSPORT_TX_DGRAM)
Florin Coras7fb0fe12018-04-09 09:24:52 -0700495 {
Florin Coras8e43d042018-05-04 15:46:57 -0700496 if (ctx->max_dequeue <= sizeof (ctx->hdr))
497 {
498 ctx->max_len_to_snd = 0;
499 return;
500 }
501 svm_fifo_peek (ctx->s->server_tx_fifo, 0, sizeof (ctx->hdr),
502 (u8 *) & ctx->hdr);
503 ASSERT (ctx->hdr.data_length > ctx->hdr.data_offset);
504 ctx->max_dequeue = ctx->hdr.data_length - ctx->hdr.data_offset;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700505 }
506 }
Florin Coras8e43d042018-05-04 15:46:57 -0700507 ASSERT (ctx->max_dequeue > 0);
Florin Coras6792ec02017-03-13 03:49:51 -0700508
509 /* Ensure we're not writing more than transport window allows */
Florin Coras8e43d042018-05-04 15:46:57 -0700510 if (ctx->max_dequeue < ctx->snd_space)
Florin Coras3e350af2017-03-30 02:54:28 -0700511 {
512 /* Constrained by tx queue. Try to send only fully formed segments */
Florin Coras8e43d042018-05-04 15:46:57 -0700513 ctx->max_len_to_snd =
514 (ctx->max_dequeue > ctx->snd_mss) ?
515 ctx->max_dequeue - ctx->max_dequeue % ctx->snd_mss : ctx->max_dequeue;
Florin Coras3e350af2017-03-30 02:54:28 -0700516 /* TODO Nagle ? */
517 }
518 else
519 {
Florin Coras1f152cd2017-08-18 19:28:03 -0700520 /* Expectation is that snd_space0 is already a multiple of snd_mss */
Florin Coras8e43d042018-05-04 15:46:57 -0700521 ctx->max_len_to_snd = ctx->snd_space;
Florin Coras3e350af2017-03-30 02:54:28 -0700522 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500523
Florin Corasf08f26d2018-05-10 13:20:47 -0700524 /* Check if we're tx constrained by the node */
525 ctx->n_segs_per_evt = ceil ((f64) ctx->max_len_to_snd / ctx->snd_mss);
526 if (ctx->n_segs_per_evt > max_segs)
527 {
528 ctx->n_segs_per_evt = max_segs;
529 ctx->max_len_to_snd = max_segs * ctx->snd_mss;
530 }
531
Florin Coras8e43d042018-05-04 15:46:57 -0700532 n_bytes_per_buf = vlib_buffer_free_list_buffer_size (vm,
533 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
Florin Corase87216f2017-08-17 16:59:22 -0700534 ASSERT (n_bytes_per_buf > MAX_HDRS_LEN);
Florin Coras8e43d042018-05-04 15:46:57 -0700535 n_bytes_per_seg = MAX_HDRS_LEN + ctx->snd_mss;
Florin Corasf08f26d2018-05-10 13:20:47 -0700536 ctx->n_bufs_per_seg = ceil ((f64) n_bytes_per_seg / n_bytes_per_buf);
Florin Coras8e43d042018-05-04 15:46:57 -0700537 ctx->deq_per_buf = clib_min (ctx->snd_mss, n_bytes_per_buf);
538 ctx->deq_per_first_buf = clib_min (ctx->snd_mss,
539 n_bytes_per_buf - MAX_HDRS_LEN);
540}
Florin Corasf6d68ed2017-05-07 19:12:02 -0700541
Florin Coras8e43d042018-05-04 15:46:57 -0700542always_inline int
543session_tx_fifo_read_and_snd_i (vlib_main_t * vm, vlib_node_runtime_t * node,
Florin Coras52207f12018-07-12 14:48:06 -0700544 session_event_t * e,
Florin Coras8e43d042018-05-04 15:46:57 -0700545 stream_session_t * s, int *n_tx_packets,
546 u8 peek_data)
547{
Florin Coras66cf5e02018-06-08 09:17:39 -0700548 u32 next_index, next0, next1, *to_next, n_left_to_next;
Florin Corasf08f26d2018-05-10 13:20:47 -0700549 u32 n_trace = vlib_get_trace_count (vm, node), n_bufs_needed = 0;
550 u32 thread_index = s->thread_index, n_left, pbi;
Florin Coras8e43d042018-05-04 15:46:57 -0700551 session_manager_main_t *smm = &session_manager_main;
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700552 session_manager_worker_t *wrk = &smm->wrk[thread_index];
553 session_tx_context_t *ctx = &wrk->ctx;
Florin Coras8e43d042018-05-04 15:46:57 -0700554 transport_proto_t tp;
555 vlib_buffer_t *pb;
Florin Corasca1c8f32018-05-23 21:01:30 -0700556 u16 n_bufs, rv;
Florin Coras8e43d042018-05-04 15:46:57 -0700557
Florin Corasca1c8f32018-05-23 21:01:30 -0700558 if (PREDICT_FALSE ((rv = session_tx_not_ready (s, peek_data))))
Florin Coras8e43d042018-05-04 15:46:57 -0700559 {
Florin Corasca1c8f32018-05-23 21:01:30 -0700560 if (rv < 2)
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700561 vec_add1 (wrk->pending_event_vector, *e);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700562 return SESSION_TX_NO_DATA;
Florin Coras8e43d042018-05-04 15:46:57 -0700563 }
564
565 next_index = smm->session_type_to_next[s->session_type];
Florin Coras66cf5e02018-06-08 09:17:39 -0700566 next0 = next1 = next_index;
Florin Coras8e43d042018-05-04 15:46:57 -0700567
568 tp = session_get_transport_proto (s);
569 ctx->s = s;
570 ctx->transport_vft = transport_protocol_get_vft (tp);
571 ctx->tc = session_tx_get_transport (ctx, peek_data);
572 ctx->snd_mss = ctx->transport_vft->send_mss (ctx->tc);
Florin Coras42ceddb2018-12-12 10:56:01 -0800573
574 if (PREDICT_FALSE (e->event_type == SESSION_IO_EVT_TX_FLUSH))
575 {
576 if (ctx->transport_vft->flush_data)
577 ctx->transport_vft->flush_data (ctx->tc);
578 }
579
580 ctx->snd_space = transport_connection_snd_space (ctx->tc,
581 vm->
582 clib_time.last_cpu_time,
583 ctx->snd_mss);
Florin Coras8e43d042018-05-04 15:46:57 -0700584 if (ctx->snd_space == 0 || ctx->snd_mss == 0)
585 {
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700586 vec_add1 (wrk->pending_event_vector, *e);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700587 return SESSION_TX_NO_DATA;
Florin Coras8e43d042018-05-04 15:46:57 -0700588 }
589
590 /* Allow enqueuing of a new event */
591 svm_fifo_unset_event (s->server_tx_fifo);
592
593 /* Check how much we can pull. */
Florin Corasf08f26d2018-05-10 13:20:47 -0700594 session_tx_set_dequeue_params (vm, ctx, VLIB_FRAME_SIZE - *n_tx_packets,
595 peek_data);
596
Florin Coras8e43d042018-05-04 15:46:57 -0700597 if (PREDICT_FALSE (!ctx->max_len_to_snd))
Florin Coras0d60a0f2018-06-29 02:02:08 -0700598 return SESSION_TX_NO_DATA;
Dave Barach68b0fb02017-02-28 15:15:56 -0500599
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700600 n_bufs = vec_len (wrk->tx_buffers);
Florin Corasf08f26d2018-05-10 13:20:47 -0700601 n_bufs_needed = ctx->n_segs_per_evt * ctx->n_bufs_per_seg;
Florin Coras8e43d042018-05-04 15:46:57 -0700602
603 /*
604 * Make sure we have at least one full frame of buffers ready
605 */
Florin Corasf08f26d2018-05-10 13:20:47 -0700606 if (n_bufs < n_bufs_needed)
Dave Barach68b0fb02017-02-28 15:15:56 -0500607 {
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700608 session_output_try_get_buffers (vm, wrk, thread_index, &n_bufs,
Florin Corasf08f26d2018-05-10 13:20:47 -0700609 ctx->n_bufs_per_seg * VLIB_FRAME_SIZE);
610 if (PREDICT_FALSE (n_bufs < n_bufs_needed))
Dave Barach68b0fb02017-02-28 15:15:56 -0500611 {
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700612 vec_add1 (wrk->pending_event_vector, *e);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700613 return SESSION_TX_NO_BUFFERS;
Dave Barach68b0fb02017-02-28 15:15:56 -0500614 }
Florin Coras8e43d042018-05-04 15:46:57 -0700615 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500616
Florin Coras8e43d042018-05-04 15:46:57 -0700617 /*
618 * Write until we fill up a frame
619 */
620 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Florin Corasf08f26d2018-05-10 13:20:47 -0700621 if (PREDICT_FALSE (ctx->n_segs_per_evt > n_left_to_next))
Florin Coras8e43d042018-05-04 15:46:57 -0700622 {
Florin Corasf08f26d2018-05-10 13:20:47 -0700623 ctx->n_segs_per_evt = n_left_to_next;
624 ctx->max_len_to_snd = ctx->snd_mss * n_left_to_next;
625 }
626 ctx->left_to_snd = ctx->max_len_to_snd;
627 n_left = ctx->n_segs_per_evt;
Florin Coras66cf5e02018-06-08 09:17:39 -0700628
629 while (n_left >= 4)
630 {
631 vlib_buffer_t *b0, *b1;
632 u32 bi0, bi1;
633
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700634 pbi = wrk->tx_buffers[n_bufs - 3];
Florin Coras66cf5e02018-06-08 09:17:39 -0700635 pb = vlib_get_buffer (vm, pbi);
636 vlib_prefetch_buffer_header (pb, STORE);
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700637 pbi = wrk->tx_buffers[n_bufs - 4];
Florin Coras66cf5e02018-06-08 09:17:39 -0700638 pb = vlib_get_buffer (vm, pbi);
639 vlib_prefetch_buffer_header (pb, STORE);
640
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700641 to_next[0] = bi0 = wrk->tx_buffers[--n_bufs];
642 to_next[1] = bi1 = wrk->tx_buffers[--n_bufs];
Florin Coras66cf5e02018-06-08 09:17:39 -0700643
644 b0 = vlib_get_buffer (vm, bi0);
645 b1 = vlib_get_buffer (vm, bi1);
646
647 session_tx_fill_buffer (vm, ctx, b0, &n_bufs, peek_data);
648 session_tx_fill_buffer (vm, ctx, b1, &n_bufs, peek_data);
649
650 ctx->transport_vft->push_header (ctx->tc, b0);
651 ctx->transport_vft->push_header (ctx->tc, b1);
652
653 to_next += 2;
654 n_left_to_next -= 2;
655 n_left -= 2;
656
657 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
658 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b1);
659
660 vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
661 n_left_to_next, bi0, bi1, next0,
662 next1);
663 }
Florin Corasf08f26d2018-05-10 13:20:47 -0700664 while (n_left)
665 {
Florin Coras66cf5e02018-06-08 09:17:39 -0700666 vlib_buffer_t *b0;
667 u32 bi0;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700668
Florin Coras91ca4622018-06-30 11:27:59 -0700669 if (n_left > 1)
670 {
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700671 pbi = wrk->tx_buffers[n_bufs - 2];
Florin Coras91ca4622018-06-30 11:27:59 -0700672 pb = vlib_get_buffer (vm, pbi);
673 vlib_prefetch_buffer_header (pb, STORE);
674 }
675
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700676 to_next[0] = bi0 = wrk->tx_buffers[--n_bufs];
Florin Coras66cf5e02018-06-08 09:17:39 -0700677 b0 = vlib_get_buffer (vm, bi0);
678 session_tx_fill_buffer (vm, ctx, b0, &n_bufs, peek_data);
Florin Coras81a13db2018-03-16 08:48:31 -0700679
Florin Coras66cf5e02018-06-08 09:17:39 -0700680 /* Ask transport to push header after current_length and
681 * total_length_not_including_first_buffer are updated */
682 ctx->transport_vft->push_header (ctx->tc, b0);
Florin Corasf6359c82017-06-19 12:26:09 -0400683
Florin Coras66cf5e02018-06-08 09:17:39 -0700684 to_next += 1;
685 n_left_to_next -= 1;
686 n_left -= 1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500687
Florin Coras66cf5e02018-06-08 09:17:39 -0700688 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700689
Florin Coras66cf5e02018-06-08 09:17:39 -0700690 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
691 n_left_to_next, bi0, next0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500692 }
Florin Coras66cf5e02018-06-08 09:17:39 -0700693
694 if (PREDICT_FALSE (n_trace > 0))
695 session_tx_trace_frame (vm, node, next_index, to_next,
696 ctx->n_segs_per_evt, s, n_trace);
697
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700698 _vec_len (wrk->tx_buffers) = n_bufs;
Florin Corasf08f26d2018-05-10 13:20:47 -0700699 *n_tx_packets += ctx->n_segs_per_evt;
Florin Corasd67f1122018-05-21 17:47:40 -0700700 transport_connection_update_tx_stats (ctx->tc, ctx->max_len_to_snd);
Florin Coras8e43d042018-05-04 15:46:57 -0700701 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
Dave Barach68b0fb02017-02-28 15:15:56 -0500702
Florin Coras6792ec02017-03-13 03:49:51 -0700703 /* If we couldn't dequeue all bytes mark as partially read */
Florin Corasf08f26d2018-05-10 13:20:47 -0700704 ASSERT (ctx->left_to_snd == 0);
Florin Coras8e43d042018-05-04 15:46:57 -0700705 if (ctx->max_len_to_snd < ctx->max_dequeue)
706 if (svm_fifo_set_event (s->server_tx_fifo))
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700707 vec_add1 (wrk->pending_event_vector, *e);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700708
Florin Coras8e43d042018-05-04 15:46:57 -0700709 if (!peek_data && ctx->transport_vft->tx_type == TRANSPORT_TX_DGRAM)
Dave Barach68b0fb02017-02-28 15:15:56 -0500710 {
Florin Coras7fb0fe12018-04-09 09:24:52 -0700711 /* Fix dgram pre header */
Florin Coras8e43d042018-05-04 15:46:57 -0700712 if (ctx->max_len_to_snd < ctx->max_dequeue)
713 svm_fifo_overwrite_head (s->server_tx_fifo, (u8 *) & ctx->hdr,
Florin Coras7fb0fe12018-04-09 09:24:52 -0700714 sizeof (session_dgram_pre_hdr_t));
715 /* More data needs to be read */
Florin Coras8e43d042018-05-04 15:46:57 -0700716 else if (svm_fifo_max_dequeue (s->server_tx_fifo) > 0)
717 if (svm_fifo_set_event (s->server_tx_fifo))
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700718 vec_add1 (wrk->pending_event_vector, *e);
Dave Barach68b0fb02017-02-28 15:15:56 -0500719 }
Florin Coras0d60a0f2018-06-29 02:02:08 -0700720 return SESSION_TX_OK;
Dave Barach68b0fb02017-02-28 15:15:56 -0500721}
722
723int
Florin Corasd79b41e2017-03-04 05:37:52 -0800724session_tx_fifo_peek_and_snd (vlib_main_t * vm, vlib_node_runtime_t * node,
Florin Coras52207f12018-07-12 14:48:06 -0700725 session_event_t * e,
Florin Coras0d60a0f2018-06-29 02:02:08 -0700726 stream_session_t * s, int *n_tx_pkts)
Dave Barach68b0fb02017-02-28 15:15:56 -0500727{
Florin Coras0d60a0f2018-06-29 02:02:08 -0700728 return session_tx_fifo_read_and_snd_i (vm, node, e, s, n_tx_pkts, 1);
Dave Barach68b0fb02017-02-28 15:15:56 -0500729}
730
731int
Florin Corasd79b41e2017-03-04 05:37:52 -0800732session_tx_fifo_dequeue_and_snd (vlib_main_t * vm, vlib_node_runtime_t * node,
Florin Coras52207f12018-07-12 14:48:06 -0700733 session_event_t * e,
Florin Coras0d60a0f2018-06-29 02:02:08 -0700734 stream_session_t * s, int *n_tx_pkts)
Dave Barach68b0fb02017-02-28 15:15:56 -0500735{
Florin Coras0d60a0f2018-06-29 02:02:08 -0700736 return session_tx_fifo_read_and_snd_i (vm, node, e, s, n_tx_pkts, 0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500737}
738
Florin Coras371ca502018-02-21 12:07:41 -0800739int
740session_tx_fifo_dequeue_internal (vlib_main_t * vm,
741 vlib_node_runtime_t * node,
Florin Coras52207f12018-07-12 14:48:06 -0700742 session_event_t * e,
Florin Coras0d60a0f2018-06-29 02:02:08 -0700743 stream_session_t * s, int *n_tx_pkts)
Florin Coras371ca502018-02-21 12:07:41 -0800744{
745 application_t *app;
Florin Corasef915342018-09-29 10:23:06 -0700746 if (PREDICT_FALSE (s->session_state == SESSION_STATE_CLOSED))
747 return 0;
Florin Corasab2f6db2018-08-31 14:31:41 -0700748 app = application_get (s->t_app_index);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700749 svm_fifo_unset_event (s->server_tx_fifo);
750 return app->cb_fns.builtin_app_tx_callback (s);
Florin Coras371ca502018-02-21 12:07:41 -0800751}
752
Dave Barach2c25a622017-06-26 11:35:07 -0400753always_inline stream_session_t *
Florin Coras52207f12018-07-12 14:48:06 -0700754session_event_get_session (session_event_t * e, u8 thread_index)
Florin Corasa5464812017-04-19 13:00:05 -0700755{
Florin Coras3cbc04b2017-10-02 00:18:51 -0700756 return session_get_if_valid (e->fifo->master_session_index, thread_index);
Florin Corasa5464812017-04-19 13:00:05 -0700757}
758
Florin Corasd67f1122018-05-21 17:47:40 -0700759static void
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700760session_update_dispatch_period (session_manager_worker_t * wrk, f64 now,
Florin Corasd67f1122018-05-21 17:47:40 -0700761 u32 thread_index)
762{
Florin Corasefefc6b2018-11-07 12:49:19 -0800763 if (wrk->last_tx_packets)
Florin Corasc44a5582018-11-01 16:30:54 -0700764 {
765 f64 sample = now - wrk->last_vlib_time;
Florin Corasc44a5582018-11-01 16:30:54 -0700766 wrk->dispatch_period = (wrk->dispatch_period + sample) * 0.5;
767 }
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700768 wrk->last_vlib_time = now;
Florin Corasd67f1122018-05-21 17:47:40 -0700769}
770
Florin Coras0d60a0f2018-06-29 02:02:08 -0700771static uword
772session_queue_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
773 vlib_frame_t * frame)
774{
775 session_manager_main_t *smm = vnet_get_session_manager_main ();
Florin Coras3c2fed52018-07-04 04:15:05 -0700776 u32 thread_index = vm->thread_index, n_to_dequeue, n_events;
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700777 session_manager_worker_t *wrk = &smm->wrk[thread_index];
Florin Coras5f56d732018-10-30 10:21:59 -0700778 session_event_t *e, *fifo_events;
Florin Coras3c2fed52018-07-04 04:15:05 -0700779 svm_msg_q_msg_t _msg, *msg = &_msg;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700780 f64 now = vlib_time_now (vm);
Florin Coras3c2fed52018-07-04 04:15:05 -0700781 int n_tx_packets = 0, i, rv;
Florin Coras15531972018-08-12 23:50:53 -0700782 app_worker_t *app_wrk;
Florin Coras3c2fed52018-07-04 04:15:05 -0700783 application_t *app;
784 svm_msg_q_t *mq;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700785 void (*fp) (void *);
786
787 SESSION_EVT_DBG (SESSION_EVT_POLL_GAP_TRACK, smm, thread_index);
788
789 /*
790 * Update transport time
791 */
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700792 session_update_dispatch_period (wrk, now, thread_index);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700793 transport_update_time (now, thread_index);
794
Florin Coras0d60a0f2018-06-29 02:02:08 -0700795 SESSION_EVT_DBG (SESSION_EVT_DEQ_NODE, 0);
796
Florin Coras5f56d732018-10-30 10:21:59 -0700797 /* Make sure postponed events are handled first */
798 fifo_events = wrk->free_event_vector;
799 vec_append (fifo_events, wrk->postponed_event_vector);
Florin Coras36ee9f12018-11-02 12:52:10 -0700800 _vec_len (wrk->postponed_event_vector) = 0;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700801
Florin Coras5f56d732018-10-30 10:21:59 -0700802 /* Try to dequeue what is available. Don't wait for lock.
803 * XXX: we may need priorities here */
804 mq = wrk->vpp_event_queue;
805 n_to_dequeue = svm_msg_q_size (mq);
806 if (n_to_dequeue && svm_msg_q_try_lock (mq) == 0)
807 {
808 for (i = 0; i < n_to_dequeue; i++)
809 {
810 vec_add2 (fifo_events, e, 1);
811 svm_msg_q_sub_w_lock (mq, msg);
Florin Coras3c7d4f92018-12-14 11:28:43 -0800812 /* Works because reply messages are smaller than a session evt.
813 * If we ever need to support bigger messages this needs to be
814 * fixed */
Dave Barach178cf492018-11-13 16:34:13 -0500815 clib_memcpy_fast (e, svm_msg_q_msg_data (mq, msg), sizeof (*e));
Florin Coras5f56d732018-10-30 10:21:59 -0700816 svm_msg_q_free_msg (mq, msg);
817 }
818 svm_msg_q_unlock (mq);
819 }
820
821 vec_append (fifo_events, wrk->pending_event_vector);
822 vec_append (fifo_events, wrk->pending_disconnects);
823
Florin Coras5f56d732018-10-30 10:21:59 -0700824 _vec_len (wrk->pending_event_vector) = 0;
Florin Coras36ee9f12018-11-02 12:52:10 -0700825 _vec_len (wrk->pending_disconnects) = 0;
Florin Coras5f56d732018-10-30 10:21:59 -0700826
Florin Coras0d60a0f2018-06-29 02:02:08 -0700827 n_events = vec_len (fifo_events);
Florin Coras5f56d732018-10-30 10:21:59 -0700828 if (PREDICT_FALSE (!n_events))
829 return 0;
830
Florin Coras0d60a0f2018-06-29 02:02:08 -0700831 for (i = 0; i < n_events; i++)
832 {
833 stream_session_t *s; /* $$$ prefetch 1 ahead maybe */
Florin Coras52207f12018-07-12 14:48:06 -0700834 session_event_t *e;
Florin Coras0e88e852018-09-17 22:09:02 -0700835 u8 want_tx_evt;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700836
837 e = &fifo_events[i];
838 switch (e->event_type)
839 {
Florin Coras42ceddb2018-12-12 10:56:01 -0800840 case SESSION_IO_EVT_TX_FLUSH:
Florin Coras0d60a0f2018-06-29 02:02:08 -0700841 case FIFO_EVENT_APP_TX:
842 /* Don't try to send more that one frame per dispatch cycle */
843 if (n_tx_packets == VLIB_FRAME_SIZE)
844 {
Florin Coras5f56d732018-10-30 10:21:59 -0700845 vec_add1 (wrk->postponed_event_vector, *e);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700846 break;
847 }
848
849 s = session_event_get_session (e, thread_index);
850 if (PREDICT_FALSE (!s))
851 {
Florin Coras326b81e2018-10-04 19:03:05 -0700852 clib_warning ("session was freed!");
Florin Coras0d60a0f2018-06-29 02:02:08 -0700853 continue;
854 }
855
Florin Coras0e88e852018-09-17 22:09:02 -0700856 want_tx_evt = svm_fifo_want_tx_evt (s->server_tx_fifo);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700857 /* Spray packets in per session type frames, since they go to
858 * different nodes */
859 rv = (smm->session_tx_fns[s->session_type]) (vm, node, e, s,
860 &n_tx_packets);
861 if (PREDICT_TRUE (rv == SESSION_TX_OK))
862 {
Florin Coras0e88e852018-09-17 22:09:02 -0700863 if (PREDICT_FALSE (want_tx_evt))
864 {
865 svm_fifo_set_want_tx_evt (s->server_tx_fifo, 0);
866 session_dequeue_notify (s);
867 }
Florin Coras0d60a0f2018-06-29 02:02:08 -0700868 }
869 else if (PREDICT_FALSE (rv == SESSION_TX_NO_BUFFERS))
870 {
871 vlib_node_increment_counter (vm, node->node_index,
872 SESSION_QUEUE_ERROR_NO_BUFFER, 1);
873 continue;
874 }
875 break;
876 case FIFO_EVENT_DISCONNECT:
877 /* Make sure stream disconnects run after the pending list is
878 * drained */
Florin Coras3c7d4f92018-12-14 11:28:43 -0800879 s = session_get_from_handle_if_valid (e->session_handle);
880 if (PREDICT_FALSE (!s))
881 break;
882
Florin Coras0d60a0f2018-06-29 02:02:08 -0700883 if (!e->postponed)
884 {
885 e->postponed = 1;
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700886 vec_add1 (wrk->pending_disconnects, *e);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700887 continue;
888 }
889 /* If tx queue is still not empty, wait */
890 if (svm_fifo_max_dequeue (s->server_tx_fifo))
891 {
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700892 vec_add1 (wrk->pending_disconnects, *e);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700893 continue;
894 }
895
896 stream_session_disconnect_transport (s);
897 break;
898 case FIFO_EVENT_BUILTIN_RX:
899 s = session_event_get_session (e, thread_index);
Florin Corasef915342018-09-29 10:23:06 -0700900 if (PREDICT_FALSE (!s || s->session_state >= SESSION_STATE_CLOSING))
Florin Coras0d60a0f2018-06-29 02:02:08 -0700901 continue;
902 svm_fifo_unset_event (s->server_rx_fifo);
Florin Coras15531972018-08-12 23:50:53 -0700903 app_wrk = app_worker_get (s->app_wrk_index);
904 app = application_get (app_wrk->app_index);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700905 app->cb_fns.builtin_app_rx_callback (s);
906 break;
Florin Corasef915342018-09-29 10:23:06 -0700907 case FIFO_EVENT_BUILTIN_TX:
908 s = session_get_from_handle_if_valid (e->session_handle);
909 if (PREDICT_TRUE (s != 0))
910 session_tx_fifo_dequeue_internal (vm, node, e, s, &n_tx_packets);
911 break;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700912 case FIFO_EVENT_RPC:
913 fp = e->rpc_args.fp;
914 (*fp) (e->rpc_args.arg);
915 break;
Florin Coras52207f12018-07-12 14:48:06 -0700916 case SESSION_CTRL_EVT_DISCONNECTED:
917 session_mq_disconnected_handler (e->data);
918 break;
919 case SESSION_CTRL_EVT_ACCEPTED_REPLY:
920 session_mq_accepted_reply_handler (e->data);
921 break;
922 case SESSION_CTRL_EVT_CONNECTED_REPLY:
923 break;
924 case SESSION_CTRL_EVT_DISCONNECTED_REPLY:
925 session_mq_disconnected_reply_handler (e->data);
926 break;
927 case SESSION_CTRL_EVT_RESET_REPLY:
928 session_mq_reset_reply_handler (e->data);
929 break;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700930 default:
931 clib_warning ("unhandled event type %d", e->event_type);
932 }
933 }
934
935 _vec_len (fifo_events) = 0;
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700936 wrk->free_event_vector = fifo_events;
Florin Corasc44a5582018-11-01 16:30:54 -0700937 wrk->last_tx_packets = n_tx_packets;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700938
939 vlib_node_increment_counter (vm, session_queue_node.index,
940 SESSION_QUEUE_ERROR_TX, n_tx_packets);
941
942 SESSION_EVT_DBG (SESSION_EVT_DISPATCH_END, smm, thread_index);
943
944 return n_tx_packets;
945}
946
947/* *INDENT-OFF* */
948VLIB_REGISTER_NODE (session_queue_node) =
949{
950 .function = session_queue_node_fn,
951 .name = "session-queue",
952 .format_trace = format_session_queue_trace,
953 .type = VLIB_NODE_TYPE_INPUT,
954 .n_errors = ARRAY_LEN (session_queue_error_strings),
955 .error_strings = session_queue_error_strings,
956 .state = VLIB_NODE_STATE_DISABLED,
957};
958/* *INDENT-ON* */
959
Dave Barachacd2a6a2017-05-16 17:41:34 -0400960void
961dump_thread_0_event_queue (void)
962{
963 session_manager_main_t *smm = vnet_get_session_manager_main ();
964 vlib_main_t *vm = &vlib_global_main;
965 u32 my_thread_index = vm->thread_index;
Florin Coras52207f12018-07-12 14:48:06 -0700966 session_event_t _e, *e = &_e;
Florin Coras3c2fed52018-07-04 04:15:05 -0700967 svm_msg_q_ring_t *ring;
Dave Barachacd2a6a2017-05-16 17:41:34 -0400968 stream_session_t *s0;
Florin Coras3c2fed52018-07-04 04:15:05 -0700969 svm_msg_q_msg_t *msg;
970 svm_msg_q_t *mq;
Dave Barachacd2a6a2017-05-16 17:41:34 -0400971 int i, index;
Dave Barachacd2a6a2017-05-16 17:41:34 -0400972
Florin Coras5a7ca7b2018-10-30 12:01:48 -0700973 mq = smm->wrk[my_thread_index].vpp_event_queue;
Florin Coras3c2fed52018-07-04 04:15:05 -0700974 index = mq->q->head;
Dave Barachacd2a6a2017-05-16 17:41:34 -0400975
Florin Coras3c2fed52018-07-04 04:15:05 -0700976 for (i = 0; i < mq->q->cursize; i++)
Dave Barachacd2a6a2017-05-16 17:41:34 -0400977 {
Florin Coras3c2fed52018-07-04 04:15:05 -0700978 msg = (svm_msg_q_msg_t *) (&mq->q->data[0] + mq->q->elsize * index);
979 ring = svm_msg_q_ring (mq, msg->ring_index);
Dave Barach178cf492018-11-13 16:34:13 -0500980 clib_memcpy_fast (e, svm_msg_q_msg_data (mq, msg), ring->elsize);
Dave Barachacd2a6a2017-05-16 17:41:34 -0400981
982 switch (e->event_type)
983 {
984 case FIFO_EVENT_APP_TX:
985 s0 = session_event_get_session (e, my_thread_index);
986 fformat (stdout, "[%04d] TX session %d\n", i, s0->session_index);
987 break;
988
989 case FIFO_EVENT_DISCONNECT:
Florin Corascea194d2017-10-02 00:18:51 -0700990 s0 = session_get_from_handle (e->session_handle);
Dave Barachacd2a6a2017-05-16 17:41:34 -0400991 fformat (stdout, "[%04d] disconnect session %d\n", i,
992 s0->session_index);
993 break;
994
995 case FIFO_EVENT_BUILTIN_RX:
996 s0 = session_event_get_session (e, my_thread_index);
997 fformat (stdout, "[%04d] builtin_rx %d\n", i, s0->session_index);
998 break;
999
1000 case FIFO_EVENT_RPC:
1001 fformat (stdout, "[%04d] RPC call %llx with %llx\n",
1002 i, (u64) (e->rpc_args.fp), (u64) (e->rpc_args.arg));
1003 break;
1004
1005 default:
1006 fformat (stdout, "[%04d] unhandled event type %d\n",
1007 i, e->event_type);
1008 break;
1009 }
1010
1011 index++;
1012
Florin Coras3c2fed52018-07-04 04:15:05 -07001013 if (index == mq->q->maxsize)
Dave Barachacd2a6a2017-05-16 17:41:34 -04001014 index = 0;
1015 }
1016}
1017
Florin Coras6534b7a2017-07-18 05:38:03 -04001018static u8
Florin Coras52207f12018-07-12 14:48:06 -07001019session_node_cmp_event (session_event_t * e, svm_fifo_t * f)
Florin Coras6534b7a2017-07-18 05:38:03 -04001020{
1021 stream_session_t *s;
1022 switch (e->event_type)
1023 {
1024 case FIFO_EVENT_APP_RX:
1025 case FIFO_EVENT_APP_TX:
1026 case FIFO_EVENT_BUILTIN_RX:
1027 if (e->fifo == f)
1028 return 1;
1029 break;
1030 case FIFO_EVENT_DISCONNECT:
1031 break;
1032 case FIFO_EVENT_RPC:
Florin Corascea194d2017-10-02 00:18:51 -07001033 s = session_get_from_handle (e->session_handle);
Florin Coras6534b7a2017-07-18 05:38:03 -04001034 if (!s)
1035 {
1036 clib_warning ("session has event but doesn't exist!");
1037 break;
1038 }
1039 if (s->server_rx_fifo == f || s->server_tx_fifo == f)
1040 return 1;
1041 break;
1042 default:
1043 break;
1044 }
1045 return 0;
1046}
1047
1048u8
Florin Coras52207f12018-07-12 14:48:06 -07001049session_node_lookup_fifo_event (svm_fifo_t * f, session_event_t * e)
Florin Coras6534b7a2017-07-18 05:38:03 -04001050{
Florin Coras52207f12018-07-12 14:48:06 -07001051 session_event_t *pending_event_vector, *evt;
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001052 session_manager_worker_t *wrk;
Florin Coras6534b7a2017-07-18 05:38:03 -04001053 int i, index, found = 0;
Florin Coras3c2fed52018-07-04 04:15:05 -07001054 svm_msg_q_msg_t *msg;
1055 svm_msg_q_ring_t *ring;
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001056 svm_msg_q_t *mq;
Florin Coras6534b7a2017-07-18 05:38:03 -04001057 u8 thread_index;
1058
1059 ASSERT (e);
1060 thread_index = f->master_thread_index;
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001061 wrk = session_manager_get_worker (thread_index);
1062
Florin Coras6534b7a2017-07-18 05:38:03 -04001063 /*
1064 * Search evt queue
1065 */
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001066 mq = wrk->vpp_event_queue;
Florin Coras3c2fed52018-07-04 04:15:05 -07001067 index = mq->q->head;
1068 for (i = 0; i < mq->q->cursize; i++)
Florin Coras6534b7a2017-07-18 05:38:03 -04001069 {
Florin Coras3c2fed52018-07-04 04:15:05 -07001070 msg = (svm_msg_q_msg_t *) (&mq->q->data[0] + mq->q->elsize * index);
1071 ring = svm_msg_q_ring (mq, msg->ring_index);
Dave Barach178cf492018-11-13 16:34:13 -05001072 clib_memcpy_fast (e, svm_msg_q_msg_data (mq, msg), ring->elsize);
Florin Coras6534b7a2017-07-18 05:38:03 -04001073 found = session_node_cmp_event (e, f);
1074 if (found)
Florin Coras371ca502018-02-21 12:07:41 -08001075 return 1;
Florin Coras3c2fed52018-07-04 04:15:05 -07001076 if (++index == mq->q->maxsize)
Florin Coras6534b7a2017-07-18 05:38:03 -04001077 index = 0;
1078 }
1079 /*
1080 * Search pending events vector
1081 */
Florin Coras5a7ca7b2018-10-30 12:01:48 -07001082 pending_event_vector = wrk->pending_event_vector;
Florin Coras6534b7a2017-07-18 05:38:03 -04001083 vec_foreach (evt, pending_event_vector)
1084 {
1085 found = session_node_cmp_event (evt, f);
1086 if (found)
1087 {
Dave Barach178cf492018-11-13 16:34:13 -05001088 clib_memcpy_fast (e, evt, sizeof (*evt));
Florin Coras6534b7a2017-07-18 05:38:03 -04001089 break;
1090 }
1091 }
1092 return found;
1093}
1094
Dave Barach2a863912017-11-28 10:11:42 -05001095static clib_error_t *
1096session_queue_exit (vlib_main_t * vm)
1097{
1098 if (vec_len (vlib_mains) < 2)
1099 return 0;
1100
1101 /*
1102 * Shut off (especially) worker-thread session nodes.
1103 * Otherwise, vpp can crash as the main thread unmaps the
1104 * API segment.
1105 */
1106 vlib_worker_thread_barrier_sync (vm);
1107 session_node_enable_disable (0 /* is_enable */ );
1108 vlib_worker_thread_barrier_release (vm);
1109 return 0;
1110}
1111
1112VLIB_MAIN_LOOP_EXIT_FUNCTION (session_queue_exit);
1113
Florin Corasfd542f12018-05-16 19:28:24 -07001114static uword
1115session_queue_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
1116 vlib_frame_t * f)
1117{
1118 f64 now, timeout = 1.0;
1119 uword *event_data = 0;
1120 uword event_type;
1121
1122 while (1)
1123 {
1124 vlib_process_wait_for_event_or_clock (vm, timeout);
1125 now = vlib_time_now (vm);
1126 event_type = vlib_process_get_events (vm, (uword **) & event_data);
1127
1128 switch (event_type)
1129 {
1130 case SESSION_Q_PROCESS_FLUSH_FRAMES:
1131 /* Flush the frames by updating all transports times */
1132 transport_update_time (now, 0);
1133 break;
1134 case SESSION_Q_PROCESS_STOP:
1135 timeout = 100000.0;
1136 break;
1137 case ~0:
1138 /* Timed out. Update time for all transports to trigger all
1139 * outstanding retransmits. */
1140 transport_update_time (now, 0);
1141 break;
1142 }
1143 vec_reset_length (event_data);
1144 }
1145 return 0;
1146}
1147
1148/* *INDENT-OFF* */
1149VLIB_REGISTER_NODE (session_queue_process_node) =
1150{
1151 .function = session_queue_process,
1152 .type = VLIB_NODE_TYPE_PROCESS,
1153 .name = "session-queue-process",
1154 .state = VLIB_NODE_STATE_DISABLED,
1155};
1156/* *INDENT-ON* */
1157
1158
Dave Barach68b0fb02017-02-28 15:15:56 -05001159/*
1160 * fd.io coding-style-patch-verification: ON
1161 *
1162 * Local Variables:
1163 * eval: (c-set-style "gnu")
1164 * End:
1165 */