blob: 2c425abeaf3fe5dc9d2caee2bb3454c2a930beb1 [file] [log] [blame]
Dave Barach68b0fb02017-02-28 15:15:56 -05001/*
2 * Copyright (c) 2017 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
Florin Coras6792ec02017-03-13 03:49:51 -070016#include <math.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050017#include <vlib/vlib.h>
18#include <vnet/vnet.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050019#include <vppinfra/elog.h>
Florin Coras561af9b2017-12-09 10:19:43 -080020#include <vnet/session/transport.h>
Florin Coras8e43d042018-05-04 15:46:57 -070021#include <vnet/session/session.h>
Florin Coras6792ec02017-03-13 03:49:51 -070022#include <vnet/session/application.h>
Florin Coras52207f12018-07-12 14:48:06 -070023#include <vnet/session/application_interface.h>
Florin Corase69f4952017-03-07 10:06:24 -080024#include <vnet/session/session_debug.h>
Florin Corase86a8ed2018-01-05 03:20:25 -080025#include <svm/queue.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050026
Florin Coras52207f12018-07-12 14:48:06 -070027static void
28session_mq_accepted_reply_handler (void *data)
29{
30 session_accepted_reply_msg_t *mp = (session_accepted_reply_msg_t *) data;
31 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
32 local_session_t *ls;
33 stream_session_t *s;
34
35 /* Server isn't interested, kill the session */
36 if (mp->retval)
37 {
38 a->app_index = mp->context;
39 a->handle = mp->handle;
40 vnet_disconnect_session (a);
41 return;
42 }
43
44 if (session_handle_is_local (mp->handle))
45 {
46 ls = application_get_local_session_from_handle (mp->handle);
Florin Coras15531972018-08-12 23:50:53 -070047 if (!ls || ls->app_wrk_index != mp->context)
Florin Coras52207f12018-07-12 14:48:06 -070048 {
49 clib_warning ("server %u doesn't own local handle %llu",
50 mp->context, mp->handle);
51 return;
52 }
53 if (application_local_session_connect_notify (ls))
54 return;
55 ls->session_state = SESSION_STATE_READY;
56 }
57 else
58 {
59 s = session_get_from_handle_if_valid (mp->handle);
60 if (!s)
61 {
62 clib_warning ("session doesn't exist");
63 return;
64 }
Florin Coras15531972018-08-12 23:50:53 -070065 if (s->app_wrk_index != mp->context)
Florin Coras52207f12018-07-12 14:48:06 -070066 {
67 clib_warning ("app doesn't own session");
68 return;
69 }
70 s->session_state = SESSION_STATE_READY;
Florin Coras84099552018-07-22 12:59:30 -070071 if (!svm_fifo_is_empty (s->server_rx_fifo))
72 {
Florin Coras15531972018-08-12 23:50:53 -070073 app_worker_t *app;
74 app = app_worker_get (s->app_wrk_index);
Florin Coras84099552018-07-22 12:59:30 -070075 application_send_event (app, s, FIFO_EVENT_APP_RX);
76 }
Florin Coras52207f12018-07-12 14:48:06 -070077 }
78}
79
80static void
81session_mq_reset_reply_handler (void *data)
82{
83 session_reset_reply_msg_t *mp;
Florin Coras15531972018-08-12 23:50:53 -070084 app_worker_t *app_wrk;
Florin Coras52207f12018-07-12 14:48:06 -070085 stream_session_t *s;
Florin Coras15531972018-08-12 23:50:53 -070086 application_t *app;
Florin Coras52207f12018-07-12 14:48:06 -070087 u32 index, thread_index;
88
89 mp = (session_reset_reply_msg_t *) data;
90 app = application_lookup (mp->client_index);
91 if (!app)
92 return;
93
94 session_parse_handle (mp->handle, &index, &thread_index);
95 s = session_get_if_valid (index, thread_index);
Florin Coras15531972018-08-12 23:50:53 -070096 if (!s)
Florin Coras52207f12018-07-12 14:48:06 -070097 {
98 clib_warning ("Invalid session!");
99 return;
100 }
Florin Coras15531972018-08-12 23:50:53 -0700101 app_wrk = app_worker_get (s->app_wrk_index);
102 if (!app_wrk || app_wrk->app_index != app->app_index)
103 {
104 clib_warning ("App % does not own handle 0x%lx!", app->app_index,
105 mp->handle);
106 return;
107 }
Florin Coras52207f12018-07-12 14:48:06 -0700108
109 /* Client objected to resetting the session, log and continue */
110 if (mp->retval)
111 {
112 clib_warning ("client retval %d", mp->retval);
113 return;
114 }
115
116 /* This comes as a response to a reset, transport only waiting for
117 * confirmation to remove connection state, no need to disconnect */
118 stream_session_cleanup (s);
119}
120
121static void
122session_mq_disconnected_handler (void *data)
123{
124 session_disconnected_reply_msg_t *rmp;
125 vnet_disconnect_args_t _a, *a = &_a;
126 svm_msg_q_msg_t _msg, *msg = &_msg;
127 session_disconnected_msg_t *mp;
Florin Coras15531972018-08-12 23:50:53 -0700128 app_worker_t *app_wrk;
Florin Coras52207f12018-07-12 14:48:06 -0700129 session_event_t *evt;
Florin Coras3d0fadc2018-07-17 05:35:47 -0700130 stream_session_t *s;
Florin Coras52207f12018-07-12 14:48:06 -0700131 application_t *app;
132 int rv = 0;
133
134 mp = (session_disconnected_msg_t *) data;
Florin Corasc3638fe2018-08-24 13:58:49 -0700135 if (!(s = session_get_from_handle_if_valid (mp->handle)))
136 {
137 clib_warning ("could not disconnect handle %llu", mp->handle);
138 return;
139 }
Florin Coras15531972018-08-12 23:50:53 -0700140 app_wrk = app_worker_get (s->app_wrk_index);
141 app = application_lookup (mp->client_index);
Florin Corasc3638fe2018-08-24 13:58:49 -0700142 if (!(app_wrk && app && app->app_index == app_wrk->app_index))
Florin Coras52207f12018-07-12 14:48:06 -0700143 {
Florin Corasc3638fe2018-08-24 13:58:49 -0700144 clib_warning ("could not disconnect session: %llu app: %u",
Florin Coras15531972018-08-12 23:50:53 -0700145 mp->handle, mp->client_index);
Florin Coras3d0fadc2018-07-17 05:35:47 -0700146 return;
Florin Coras52207f12018-07-12 14:48:06 -0700147 }
Florin Coras3d0fadc2018-07-17 05:35:47 -0700148
149 a->handle = mp->handle;
Florin Coras15531972018-08-12 23:50:53 -0700150 a->app_index = app_wrk->wrk_index;
Florin Coras3d0fadc2018-07-17 05:35:47 -0700151 rv = vnet_disconnect_session (a);
Florin Coras52207f12018-07-12 14:48:06 -0700152
Florin Coras15531972018-08-12 23:50:53 -0700153 svm_msg_q_lock_and_alloc_msg_w_ring (app_wrk->event_queue,
Florin Coras52207f12018-07-12 14:48:06 -0700154 SESSION_MQ_CTRL_EVT_RING,
155 SVM_Q_WAIT, msg);
Florin Coras15531972018-08-12 23:50:53 -0700156 svm_msg_q_unlock (app_wrk->event_queue);
157 evt = svm_msg_q_msg_data (app_wrk->event_queue, msg);
Florin Coras52207f12018-07-12 14:48:06 -0700158 memset (evt, 0, sizeof (*evt));
159 evt->event_type = SESSION_CTRL_EVT_DISCONNECTED;
160 rmp = (session_disconnected_reply_msg_t *) evt->data;
161 rmp->handle = mp->handle;
162 rmp->context = mp->context;
163 rmp->retval = rv;
Florin Coras15531972018-08-12 23:50:53 -0700164 svm_msg_q_add (app_wrk->event_queue, msg, SVM_Q_WAIT);
Florin Coras52207f12018-07-12 14:48:06 -0700165}
166
167static void
168session_mq_disconnected_reply_handler (void *data)
169{
170 session_disconnected_reply_msg_t *mp;
171 vnet_disconnect_args_t _a, *a = &_a;
172 application_t *app;
173
174 mp = (session_disconnected_reply_msg_t *) data;
175
176 /* Client objected to disconnecting the session, log and continue */
177 if (mp->retval)
178 {
179 clib_warning ("client retval %d", mp->retval);
180 return;
181 }
182
183 /* Disconnect has been confirmed. Confirm close to transport */
184 app = application_lookup (mp->context);
185 if (app)
186 {
187 a->handle = mp->handle;
Florin Coras15531972018-08-12 23:50:53 -0700188 a->app_index = app->app_index;
Florin Coras52207f12018-07-12 14:48:06 -0700189 vnet_disconnect_session (a);
190 }
191}
192
Dave Barach68b0fb02017-02-28 15:15:56 -0500193vlib_node_registration_t session_queue_node;
194
195typedef struct
196{
197 u32 session_index;
198 u32 server_thread_index;
199} session_queue_trace_t;
200
201/* packet trace format function */
202static u8 *
203format_session_queue_trace (u8 * s, va_list * args)
204{
205 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
206 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
207 session_queue_trace_t *t = va_arg (*args, session_queue_trace_t *);
208
209 s = format (s, "SESSION_QUEUE: session index %d, server thread index %d",
210 t->session_index, t->server_thread_index);
211 return s;
212}
213
Florin Coras6792ec02017-03-13 03:49:51 -0700214#define foreach_session_queue_error \
215_(TX, "Packets transmitted") \
Florin Coras93992a92017-05-24 18:03:56 -0700216_(TIMER, "Timer events") \
217_(NO_BUFFER, "Out of buffers")
Dave Barach68b0fb02017-02-28 15:15:56 -0500218
219typedef enum
220{
221#define _(sym,str) SESSION_QUEUE_ERROR_##sym,
222 foreach_session_queue_error
223#undef _
224 SESSION_QUEUE_N_ERROR,
225} session_queue_error_t;
226
227static char *session_queue_error_strings[] = {
228#define _(sym,string) string,
229 foreach_session_queue_error
230#undef _
231};
232
Florin Coras0d60a0f2018-06-29 02:02:08 -0700233enum
234{
235 SESSION_TX_NO_BUFFERS = -2,
236 SESSION_TX_NO_DATA,
237 SESSION_TX_OK
238};
239
Florin Coras66cf5e02018-06-08 09:17:39 -0700240static void
241session_tx_trace_frame (vlib_main_t * vm, vlib_node_runtime_t * node,
242 u32 next_index, u32 * to_next, u16 n_segs,
243 stream_session_t * s, u32 n_trace)
Florin Corasf6d68ed2017-05-07 19:12:02 -0700244{
Florin Coras8e43d042018-05-04 15:46:57 -0700245 session_queue_trace_t *t;
Florin Coras66cf5e02018-06-08 09:17:39 -0700246 vlib_buffer_t *b;
247 int i;
248
249 for (i = 0; i < clib_min (n_trace, n_segs); i++)
250 {
251 b = vlib_get_buffer (vm, to_next[i - n_segs]);
252 vlib_trace_buffer (vm, node, next_index, b, 1 /* follow_chain */ );
Florin Coras66cf5e02018-06-08 09:17:39 -0700253 t = vlib_add_trace (vm, node, b, sizeof (*t));
254 t->session_index = s->session_index;
255 t->server_thread_index = s->thread_index;
256 }
Florin Coras4df38712018-06-20 12:44:16 -0700257 vlib_set_trace_count (vm, node, n_trace - i);
Florin Coras8e43d042018-05-04 15:46:57 -0700258}
Florin Corasf6d68ed2017-05-07 19:12:02 -0700259
Florin Coras8e43d042018-05-04 15:46:57 -0700260always_inline void
261session_tx_fifo_chain_tail (vlib_main_t * vm, session_tx_context_t * ctx,
262 vlib_buffer_t * b, u16 * n_bufs, u8 peek_data)
263{
264 session_manager_main_t *smm = &session_manager_main;
265 vlib_buffer_t *chain_b, *prev_b;
266 u32 chain_bi0, to_deq, left_from_seg;
267 u16 len_to_deq, n_bytes_read;
268 u8 *data, j;
Florin Corasb2215d62017-08-01 16:56:58 -0700269
Florin Coras8e43d042018-05-04 15:46:57 -0700270 b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
271 b->total_length_not_including_first_buffer = 0;
272
273 chain_b = b;
274 left_from_seg = clib_min (ctx->snd_mss - b->current_length,
275 ctx->left_to_snd);
Florin Corasb2215d62017-08-01 16:56:58 -0700276 to_deq = left_from_seg;
Florin Coras8e43d042018-05-04 15:46:57 -0700277 for (j = 1; j < ctx->n_bufs_per_seg; j++)
Florin Corasf6d68ed2017-05-07 19:12:02 -0700278 {
Florin Coras8e43d042018-05-04 15:46:57 -0700279 prev_b = chain_b;
280 len_to_deq = clib_min (to_deq, ctx->deq_per_buf);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700281
282 *n_bufs -= 1;
Florin Coras8e43d042018-05-04 15:46:57 -0700283 chain_bi0 = smm->tx_buffers[ctx->s->thread_index][*n_bufs];
284 _vec_len (smm->tx_buffers[ctx->s->thread_index]) = *n_bufs;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700285
Florin Coras8e43d042018-05-04 15:46:57 -0700286 chain_b = vlib_get_buffer (vm, chain_bi0);
287 chain_b->current_data = 0;
288 data = vlib_buffer_get_current (chain_b);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700289 if (peek_data)
290 {
Florin Coras8e43d042018-05-04 15:46:57 -0700291 n_bytes_read = svm_fifo_peek (ctx->s->server_tx_fifo,
292 ctx->tx_offset, len_to_deq, data);
293 ctx->tx_offset += n_bytes_read;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700294 }
295 else
296 {
Florin Coras8e43d042018-05-04 15:46:57 -0700297 if (ctx->transport_vft->tx_type == TRANSPORT_TX_DGRAM)
Florin Coras7fb0fe12018-04-09 09:24:52 -0700298 {
Florin Coras8e43d042018-05-04 15:46:57 -0700299 svm_fifo_t *f = ctx->s->server_tx_fifo;
300 session_dgram_hdr_t *hdr = &ctx->hdr;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700301 u16 deq_now;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700302 deq_now = clib_min (hdr->data_length - hdr->data_offset,
Florin Coras8e43d042018-05-04 15:46:57 -0700303 len_to_deq);
304 n_bytes_read = svm_fifo_peek (f, hdr->data_offset, deq_now,
305 data);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700306 ASSERT (n_bytes_read > 0);
307
308 hdr->data_offset += n_bytes_read;
309 if (hdr->data_offset == hdr->data_length)
shubing guoaea5f392018-08-30 13:29:49 +0800310 {
311 u32 offset = hdr->data_length + SESSION_CONN_HDR_LEN;
312 svm_fifo_dequeue_drop (f, offset);
313 }
Florin Coras7fb0fe12018-04-09 09:24:52 -0700314 }
315 else
Florin Coras8e43d042018-05-04 15:46:57 -0700316 n_bytes_read = svm_fifo_dequeue_nowait (ctx->s->server_tx_fifo,
317 len_to_deq, data);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700318 }
Florin Coras8e43d042018-05-04 15:46:57 -0700319 ASSERT (n_bytes_read == len_to_deq);
320 chain_b->current_length = n_bytes_read;
321 b->total_length_not_including_first_buffer += chain_b->current_length;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700322
323 /* update previous buffer */
Florin Coras8e43d042018-05-04 15:46:57 -0700324 prev_b->next_buffer = chain_bi0;
325 prev_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700326
327 /* update current buffer */
Florin Coras8e43d042018-05-04 15:46:57 -0700328 chain_b->next_buffer = 0;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700329
Florin Corasb2215d62017-08-01 16:56:58 -0700330 to_deq -= n_bytes_read;
331 if (to_deq == 0)
Florin Corasf6d68ed2017-05-07 19:12:02 -0700332 break;
333 }
Florin Coras1f152cd2017-08-18 19:28:03 -0700334 ASSERT (to_deq == 0
Florin Coras8e43d042018-05-04 15:46:57 -0700335 && b->total_length_not_including_first_buffer == left_from_seg);
336 ctx->left_to_snd -= left_from_seg;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700337}
338
Dave Barach68b0fb02017-02-28 15:15:56 -0500339always_inline int
Florin Coras8e43d042018-05-04 15:46:57 -0700340session_output_try_get_buffers (vlib_main_t * vm,
Florin Corasd79b41e2017-03-04 05:37:52 -0800341 session_manager_main_t * smm,
Florin Coras8e43d042018-05-04 15:46:57 -0700342 u32 thread_index, u16 * n_bufs, u32 wanted)
Dave Barach68b0fb02017-02-28 15:15:56 -0500343{
Florin Corasf08f26d2018-05-10 13:20:47 -0700344 u32 n_alloc;
345 vec_validate_aligned (smm->tx_buffers[thread_index], wanted - 1,
Florin Coras8e43d042018-05-04 15:46:57 -0700346 CLIB_CACHE_LINE_BYTES);
Florin Corasf08f26d2018-05-10 13:20:47 -0700347 n_alloc = vlib_buffer_alloc (vm, &smm->tx_buffers[thread_index][*n_bufs],
348 wanted - *n_bufs);
349 *n_bufs += n_alloc;
Florin Coras8e43d042018-05-04 15:46:57 -0700350 _vec_len (smm->tx_buffers[thread_index]) = *n_bufs;
Florin Corasf08f26d2018-05-10 13:20:47 -0700351 return n_alloc;
Florin Coras8e43d042018-05-04 15:46:57 -0700352}
353
354always_inline void
355session_tx_fill_buffer (vlib_main_t * vm, session_tx_context_t * ctx,
356 vlib_buffer_t * b, u16 * n_bufs, u8 peek_data)
357{
358 u32 len_to_deq;
359 u8 *data0;
360 int n_bytes_read;
361
362 /*
363 * Start with the first buffer in chain
364 */
365 b->error = 0;
366 b->flags = VNET_BUFFER_F_LOCALLY_ORIGINATED;
367 b->current_data = 0;
Florin Coras8e43d042018-05-04 15:46:57 -0700368
369 data0 = vlib_buffer_make_headroom (b, MAX_HDRS_LEN);
370 len_to_deq = clib_min (ctx->left_to_snd, ctx->deq_per_first_buf);
371
Florin Coras7fb0fe12018-04-09 09:24:52 -0700372 if (peek_data)
373 {
Florin Coras8e43d042018-05-04 15:46:57 -0700374 n_bytes_read = svm_fifo_peek (ctx->s->server_tx_fifo, ctx->tx_offset,
375 len_to_deq, data0);
376 ASSERT (n_bytes_read > 0);
377 /* Keep track of progress locally, transport is also supposed to
378 * increment it independently when pushing the header */
379 ctx->tx_offset += n_bytes_read;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700380 }
381 else
382 {
Florin Coras8e43d042018-05-04 15:46:57 -0700383 if (ctx->transport_vft->tx_type == TRANSPORT_TX_DGRAM)
384 {
385 session_dgram_hdr_t *hdr = &ctx->hdr;
386 svm_fifo_t *f = ctx->s->server_tx_fifo;
387 u16 deq_now;
388 u32 offset;
389
390 ASSERT (hdr->data_length > hdr->data_offset);
391 deq_now = clib_min (hdr->data_length - hdr->data_offset,
392 len_to_deq);
393 offset = hdr->data_offset + SESSION_CONN_HDR_LEN;
394 n_bytes_read = svm_fifo_peek (f, offset, deq_now, data0);
395 ASSERT (n_bytes_read > 0);
396
397 if (ctx->s->session_state == SESSION_STATE_LISTENING)
398 {
399 ip_copy (&ctx->tc->rmt_ip, &hdr->rmt_ip, ctx->tc->is_ip4);
400 ctx->tc->rmt_port = hdr->rmt_port;
401 }
402 hdr->data_offset += n_bytes_read;
403 if (hdr->data_offset == hdr->data_length)
404 {
405 offset = hdr->data_length + SESSION_CONN_HDR_LEN;
406 svm_fifo_dequeue_drop (f, offset);
407 }
408 }
Florin Coras7fb0fe12018-04-09 09:24:52 -0700409 else
410 {
Florin Coras8e43d042018-05-04 15:46:57 -0700411 n_bytes_read = svm_fifo_dequeue_nowait (ctx->s->server_tx_fifo,
412 len_to_deq, data0);
413 ASSERT (n_bytes_read > 0);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700414 }
415 }
Florin Coras8e43d042018-05-04 15:46:57 -0700416 b->current_length = n_bytes_read;
417 ctx->left_to_snd -= n_bytes_read;
Dave Barach68b0fb02017-02-28 15:15:56 -0500418
Florin Coras8e43d042018-05-04 15:46:57 -0700419 /*
420 * Fill in the remaining buffers in the chain, if any
421 */
422 if (PREDICT_FALSE (ctx->n_bufs_per_seg > 1 && ctx->left_to_snd))
423 session_tx_fifo_chain_tail (vm, ctx, b, n_bufs, peek_data);
Dave Barach68b0fb02017-02-28 15:15:56 -0500424
Florin Coras8e43d042018-05-04 15:46:57 -0700425 /* *INDENT-OFF* */
Florin Coras8b20bf52018-06-14 14:55:50 -0700426 SESSION_EVT_DBG(SESSION_EVT_DEQ, ctx->s, ({
427 ed->data[0] = FIFO_EVENT_APP_TX;
428 ed->data[1] = ctx->max_dequeue;
Florin Coras8e43d042018-05-04 15:46:57 -0700429 ed->data[2] = len_to_deq;
Florin Coras8b20bf52018-06-14 14:55:50 -0700430 ed->data[3] = ctx->left_to_snd;
Florin Coras8e43d042018-05-04 15:46:57 -0700431 }));
432 /* *INDENT-ON* */
433}
434
435always_inline u8
436session_tx_not_ready (stream_session_t * s, u8 peek_data)
437{
438 if (peek_data)
Florin Corase04c2992017-03-01 08:17:34 -0800439 {
Florin Coras8e43d042018-05-04 15:46:57 -0700440 /* Can retransmit for closed sessions but can't send new data if
441 * session is not ready or closed */
442 if (s->session_state < SESSION_STATE_READY)
443 return 1;
Florin Corasca1c8f32018-05-23 21:01:30 -0700444 if (s->session_state == SESSION_STATE_CLOSED)
445 return 2;
Florin Corase04c2992017-03-01 08:17:34 -0800446 }
Florin Coras8e43d042018-05-04 15:46:57 -0700447 return 0;
448}
Dave Barach68b0fb02017-02-28 15:15:56 -0500449
Florin Coras8e43d042018-05-04 15:46:57 -0700450always_inline transport_connection_t *
451session_tx_get_transport (session_tx_context_t * ctx, u8 peek_data)
452{
453 if (peek_data)
454 {
455 return ctx->transport_vft->get_connection (ctx->s->connection_index,
456 ctx->s->thread_index);
457 }
458 else
459 {
460 if (ctx->s->session_state == SESSION_STATE_LISTENING)
461 return ctx->transport_vft->get_listener (ctx->s->connection_index);
462 else
463 {
464 return ctx->transport_vft->get_connection (ctx->s->connection_index,
465 ctx->s->thread_index);
466 }
467 }
468}
Florin Coras6a9b68b2017-11-21 04:20:42 -0800469
Florin Coras8e43d042018-05-04 15:46:57 -0700470always_inline void
471session_tx_set_dequeue_params (vlib_main_t * vm, session_tx_context_t * ctx,
Florin Corasf08f26d2018-05-10 13:20:47 -0700472 u32 max_segs, u8 peek_data)
Florin Coras8e43d042018-05-04 15:46:57 -0700473{
474 u32 n_bytes_per_buf, n_bytes_per_seg;
475 ctx->max_dequeue = svm_fifo_max_dequeue (ctx->s->server_tx_fifo);
Dave Barach68b0fb02017-02-28 15:15:56 -0500476 if (peek_data)
477 {
Florin Coras9d063042017-09-14 03:08:00 -0400478 /* Offset in rx fifo from where to peek data */
Florin Coras8e43d042018-05-04 15:46:57 -0700479 ctx->tx_offset = ctx->transport_vft->tx_fifo_offset (ctx->tc);
480 if (PREDICT_FALSE (ctx->tx_offset >= ctx->max_dequeue))
481 {
482 ctx->max_len_to_snd = 0;
483 return;
484 }
485 ctx->max_dequeue -= ctx->tx_offset;
Dave Barach68b0fb02017-02-28 15:15:56 -0500486 }
Florin Coras7fb0fe12018-04-09 09:24:52 -0700487 else
488 {
Florin Coras8e43d042018-05-04 15:46:57 -0700489 if (ctx->transport_vft->tx_type == TRANSPORT_TX_DGRAM)
Florin Coras7fb0fe12018-04-09 09:24:52 -0700490 {
Florin Coras8e43d042018-05-04 15:46:57 -0700491 if (ctx->max_dequeue <= sizeof (ctx->hdr))
492 {
493 ctx->max_len_to_snd = 0;
494 return;
495 }
496 svm_fifo_peek (ctx->s->server_tx_fifo, 0, sizeof (ctx->hdr),
497 (u8 *) & ctx->hdr);
498 ASSERT (ctx->hdr.data_length > ctx->hdr.data_offset);
499 ctx->max_dequeue = ctx->hdr.data_length - ctx->hdr.data_offset;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700500 }
501 }
Florin Coras8e43d042018-05-04 15:46:57 -0700502 ASSERT (ctx->max_dequeue > 0);
Florin Coras6792ec02017-03-13 03:49:51 -0700503
504 /* Ensure we're not writing more than transport window allows */
Florin Coras8e43d042018-05-04 15:46:57 -0700505 if (ctx->max_dequeue < ctx->snd_space)
Florin Coras3e350af2017-03-30 02:54:28 -0700506 {
507 /* Constrained by tx queue. Try to send only fully formed segments */
Florin Coras8e43d042018-05-04 15:46:57 -0700508 ctx->max_len_to_snd =
509 (ctx->max_dequeue > ctx->snd_mss) ?
510 ctx->max_dequeue - ctx->max_dequeue % ctx->snd_mss : ctx->max_dequeue;
Florin Coras3e350af2017-03-30 02:54:28 -0700511 /* TODO Nagle ? */
512 }
513 else
514 {
Florin Coras1f152cd2017-08-18 19:28:03 -0700515 /* Expectation is that snd_space0 is already a multiple of snd_mss */
Florin Coras8e43d042018-05-04 15:46:57 -0700516 ctx->max_len_to_snd = ctx->snd_space;
Florin Coras3e350af2017-03-30 02:54:28 -0700517 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500518
Florin Corasf08f26d2018-05-10 13:20:47 -0700519 /* Check if we're tx constrained by the node */
520 ctx->n_segs_per_evt = ceil ((f64) ctx->max_len_to_snd / ctx->snd_mss);
521 if (ctx->n_segs_per_evt > max_segs)
522 {
523 ctx->n_segs_per_evt = max_segs;
524 ctx->max_len_to_snd = max_segs * ctx->snd_mss;
525 }
526
Florin Coras8e43d042018-05-04 15:46:57 -0700527 n_bytes_per_buf = vlib_buffer_free_list_buffer_size (vm,
528 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
Florin Corase87216f2017-08-17 16:59:22 -0700529 ASSERT (n_bytes_per_buf > MAX_HDRS_LEN);
Florin Coras8e43d042018-05-04 15:46:57 -0700530 n_bytes_per_seg = MAX_HDRS_LEN + ctx->snd_mss;
Florin Corasf08f26d2018-05-10 13:20:47 -0700531 ctx->n_bufs_per_seg = ceil ((f64) n_bytes_per_seg / n_bytes_per_buf);
Florin Coras8e43d042018-05-04 15:46:57 -0700532 ctx->deq_per_buf = clib_min (ctx->snd_mss, n_bytes_per_buf);
533 ctx->deq_per_first_buf = clib_min (ctx->snd_mss,
534 n_bytes_per_buf - MAX_HDRS_LEN);
535}
Florin Corasf6d68ed2017-05-07 19:12:02 -0700536
Florin Coras8e43d042018-05-04 15:46:57 -0700537always_inline int
538session_tx_fifo_read_and_snd_i (vlib_main_t * vm, vlib_node_runtime_t * node,
Florin Coras52207f12018-07-12 14:48:06 -0700539 session_event_t * e,
Florin Coras8e43d042018-05-04 15:46:57 -0700540 stream_session_t * s, int *n_tx_packets,
541 u8 peek_data)
542{
Florin Coras66cf5e02018-06-08 09:17:39 -0700543 u32 next_index, next0, next1, *to_next, n_left_to_next;
Florin Corasf08f26d2018-05-10 13:20:47 -0700544 u32 n_trace = vlib_get_trace_count (vm, node), n_bufs_needed = 0;
545 u32 thread_index = s->thread_index, n_left, pbi;
Florin Coras8e43d042018-05-04 15:46:57 -0700546 session_manager_main_t *smm = &session_manager_main;
547 session_tx_context_t *ctx = &smm->ctx[thread_index];
548 transport_proto_t tp;
549 vlib_buffer_t *pb;
Florin Corasca1c8f32018-05-23 21:01:30 -0700550 u16 n_bufs, rv;
Florin Coras8e43d042018-05-04 15:46:57 -0700551
Florin Corasca1c8f32018-05-23 21:01:30 -0700552 if (PREDICT_FALSE ((rv = session_tx_not_ready (s, peek_data))))
Florin Coras8e43d042018-05-04 15:46:57 -0700553 {
Florin Corasca1c8f32018-05-23 21:01:30 -0700554 if (rv < 2)
555 vec_add1 (smm->pending_event_vector[thread_index], *e);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700556 return SESSION_TX_NO_DATA;
Florin Coras8e43d042018-05-04 15:46:57 -0700557 }
558
559 next_index = smm->session_type_to_next[s->session_type];
Florin Coras66cf5e02018-06-08 09:17:39 -0700560 next0 = next1 = next_index;
Florin Coras8e43d042018-05-04 15:46:57 -0700561
562 tp = session_get_transport_proto (s);
563 ctx->s = s;
564 ctx->transport_vft = transport_protocol_get_vft (tp);
565 ctx->tc = session_tx_get_transport (ctx, peek_data);
566 ctx->snd_mss = ctx->transport_vft->send_mss (ctx->tc);
567 ctx->snd_space = ctx->transport_vft->send_space (ctx->tc);
568 if (ctx->snd_space == 0 || ctx->snd_mss == 0)
569 {
570 vec_add1 (smm->pending_event_vector[thread_index], *e);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700571 return SESSION_TX_NO_DATA;
Florin Coras8e43d042018-05-04 15:46:57 -0700572 }
573
574 /* Allow enqueuing of a new event */
575 svm_fifo_unset_event (s->server_tx_fifo);
576
577 /* Check how much we can pull. */
Florin Corasf08f26d2018-05-10 13:20:47 -0700578 session_tx_set_dequeue_params (vm, ctx, VLIB_FRAME_SIZE - *n_tx_packets,
579 peek_data);
580
Florin Coras8e43d042018-05-04 15:46:57 -0700581 if (PREDICT_FALSE (!ctx->max_len_to_snd))
Florin Coras0d60a0f2018-06-29 02:02:08 -0700582 return SESSION_TX_NO_DATA;
Dave Barach68b0fb02017-02-28 15:15:56 -0500583
584 n_bufs = vec_len (smm->tx_buffers[thread_index]);
Florin Corasf08f26d2018-05-10 13:20:47 -0700585 n_bufs_needed = ctx->n_segs_per_evt * ctx->n_bufs_per_seg;
Florin Coras8e43d042018-05-04 15:46:57 -0700586
587 /*
588 * Make sure we have at least one full frame of buffers ready
589 */
Florin Corasf08f26d2018-05-10 13:20:47 -0700590 if (n_bufs < n_bufs_needed)
Dave Barach68b0fb02017-02-28 15:15:56 -0500591 {
Florin Coras8e43d042018-05-04 15:46:57 -0700592 session_output_try_get_buffers (vm, smm, thread_index, &n_bufs,
Florin Corasf08f26d2018-05-10 13:20:47 -0700593 ctx->n_bufs_per_seg * VLIB_FRAME_SIZE);
594 if (PREDICT_FALSE (n_bufs < n_bufs_needed))
Dave Barach68b0fb02017-02-28 15:15:56 -0500595 {
Florin Coras8e43d042018-05-04 15:46:57 -0700596 vec_add1 (smm->pending_event_vector[thread_index], *e);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700597 return SESSION_TX_NO_BUFFERS;
Dave Barach68b0fb02017-02-28 15:15:56 -0500598 }
Florin Coras8e43d042018-05-04 15:46:57 -0700599 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500600
Florin Coras8e43d042018-05-04 15:46:57 -0700601 /*
602 * Write until we fill up a frame
603 */
604 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Florin Corasf08f26d2018-05-10 13:20:47 -0700605 if (PREDICT_FALSE (ctx->n_segs_per_evt > n_left_to_next))
Florin Coras8e43d042018-05-04 15:46:57 -0700606 {
Florin Corasf08f26d2018-05-10 13:20:47 -0700607 ctx->n_segs_per_evt = n_left_to_next;
608 ctx->max_len_to_snd = ctx->snd_mss * n_left_to_next;
609 }
610 ctx->left_to_snd = ctx->max_len_to_snd;
611 n_left = ctx->n_segs_per_evt;
Florin Coras66cf5e02018-06-08 09:17:39 -0700612
613 while (n_left >= 4)
614 {
615 vlib_buffer_t *b0, *b1;
616 u32 bi0, bi1;
617
618 pbi = smm->tx_buffers[thread_index][n_bufs - 3];
619 pb = vlib_get_buffer (vm, pbi);
620 vlib_prefetch_buffer_header (pb, STORE);
621 pbi = smm->tx_buffers[thread_index][n_bufs - 4];
622 pb = vlib_get_buffer (vm, pbi);
623 vlib_prefetch_buffer_header (pb, STORE);
624
625 to_next[0] = bi0 = smm->tx_buffers[thread_index][--n_bufs];
626 to_next[1] = bi1 = smm->tx_buffers[thread_index][--n_bufs];
627
628 b0 = vlib_get_buffer (vm, bi0);
629 b1 = vlib_get_buffer (vm, bi1);
630
631 session_tx_fill_buffer (vm, ctx, b0, &n_bufs, peek_data);
632 session_tx_fill_buffer (vm, ctx, b1, &n_bufs, peek_data);
633
634 ctx->transport_vft->push_header (ctx->tc, b0);
635 ctx->transport_vft->push_header (ctx->tc, b1);
636
637 to_next += 2;
638 n_left_to_next -= 2;
639 n_left -= 2;
640
641 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
642 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b1);
643
644 vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
645 n_left_to_next, bi0, bi1, next0,
646 next1);
647 }
Florin Corasf08f26d2018-05-10 13:20:47 -0700648 while (n_left)
649 {
Florin Coras66cf5e02018-06-08 09:17:39 -0700650 vlib_buffer_t *b0;
651 u32 bi0;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700652
Florin Coras91ca4622018-06-30 11:27:59 -0700653 if (n_left > 1)
654 {
655 pbi = smm->tx_buffers[thread_index][n_bufs - 2];
656 pb = vlib_get_buffer (vm, pbi);
657 vlib_prefetch_buffer_header (pb, STORE);
658 }
659
Florin Coras66cf5e02018-06-08 09:17:39 -0700660 to_next[0] = bi0 = smm->tx_buffers[thread_index][--n_bufs];
661 b0 = vlib_get_buffer (vm, bi0);
662 session_tx_fill_buffer (vm, ctx, b0, &n_bufs, peek_data);
Florin Coras81a13db2018-03-16 08:48:31 -0700663
Florin Coras66cf5e02018-06-08 09:17:39 -0700664 /* Ask transport to push header after current_length and
665 * total_length_not_including_first_buffer are updated */
666 ctx->transport_vft->push_header (ctx->tc, b0);
Florin Corasf6359c82017-06-19 12:26:09 -0400667
Florin Coras66cf5e02018-06-08 09:17:39 -0700668 to_next += 1;
669 n_left_to_next -= 1;
670 n_left -= 1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500671
Florin Coras66cf5e02018-06-08 09:17:39 -0700672 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700673
Florin Coras66cf5e02018-06-08 09:17:39 -0700674 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
675 n_left_to_next, bi0, next0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500676 }
Florin Coras66cf5e02018-06-08 09:17:39 -0700677
678 if (PREDICT_FALSE (n_trace > 0))
679 session_tx_trace_frame (vm, node, next_index, to_next,
680 ctx->n_segs_per_evt, s, n_trace);
681
Florin Coras8e43d042018-05-04 15:46:57 -0700682 _vec_len (smm->tx_buffers[thread_index]) = n_bufs;
Florin Corasf08f26d2018-05-10 13:20:47 -0700683 *n_tx_packets += ctx->n_segs_per_evt;
Florin Coras8e43d042018-05-04 15:46:57 -0700684 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
Dave Barach68b0fb02017-02-28 15:15:56 -0500685
Florin Coras6792ec02017-03-13 03:49:51 -0700686 /* If we couldn't dequeue all bytes mark as partially read */
Florin Corasf08f26d2018-05-10 13:20:47 -0700687 ASSERT (ctx->left_to_snd == 0);
Florin Coras8e43d042018-05-04 15:46:57 -0700688 if (ctx->max_len_to_snd < ctx->max_dequeue)
689 if (svm_fifo_set_event (s->server_tx_fifo))
690 vec_add1 (smm->pending_event_vector[thread_index], *e);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700691
Florin Coras8e43d042018-05-04 15:46:57 -0700692 if (!peek_data && ctx->transport_vft->tx_type == TRANSPORT_TX_DGRAM)
Dave Barach68b0fb02017-02-28 15:15:56 -0500693 {
Florin Coras7fb0fe12018-04-09 09:24:52 -0700694 /* Fix dgram pre header */
Florin Coras8e43d042018-05-04 15:46:57 -0700695 if (ctx->max_len_to_snd < ctx->max_dequeue)
696 svm_fifo_overwrite_head (s->server_tx_fifo, (u8 *) & ctx->hdr,
Florin Coras7fb0fe12018-04-09 09:24:52 -0700697 sizeof (session_dgram_pre_hdr_t));
698 /* More data needs to be read */
Florin Coras8e43d042018-05-04 15:46:57 -0700699 else if (svm_fifo_max_dequeue (s->server_tx_fifo) > 0)
700 if (svm_fifo_set_event (s->server_tx_fifo))
701 vec_add1 (smm->pending_event_vector[thread_index], *e);
Dave Barach68b0fb02017-02-28 15:15:56 -0500702 }
Florin Coras0d60a0f2018-06-29 02:02:08 -0700703 return SESSION_TX_OK;
Dave Barach68b0fb02017-02-28 15:15:56 -0500704}
705
706int
Florin Corasd79b41e2017-03-04 05:37:52 -0800707session_tx_fifo_peek_and_snd (vlib_main_t * vm, vlib_node_runtime_t * node,
Florin Coras52207f12018-07-12 14:48:06 -0700708 session_event_t * e,
Florin Coras0d60a0f2018-06-29 02:02:08 -0700709 stream_session_t * s, int *n_tx_pkts)
Dave Barach68b0fb02017-02-28 15:15:56 -0500710{
Florin Coras0d60a0f2018-06-29 02:02:08 -0700711 return session_tx_fifo_read_and_snd_i (vm, node, e, s, n_tx_pkts, 1);
Dave Barach68b0fb02017-02-28 15:15:56 -0500712}
713
714int
Florin Corasd79b41e2017-03-04 05:37:52 -0800715session_tx_fifo_dequeue_and_snd (vlib_main_t * vm, vlib_node_runtime_t * node,
Florin Coras52207f12018-07-12 14:48:06 -0700716 session_event_t * e,
Florin Coras0d60a0f2018-06-29 02:02:08 -0700717 stream_session_t * s, int *n_tx_pkts)
Dave Barach68b0fb02017-02-28 15:15:56 -0500718{
Florin Coras0d60a0f2018-06-29 02:02:08 -0700719 return session_tx_fifo_read_and_snd_i (vm, node, e, s, n_tx_pkts, 0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500720}
721
Florin Coras371ca502018-02-21 12:07:41 -0800722int
723session_tx_fifo_dequeue_internal (vlib_main_t * vm,
724 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)
Florin Coras371ca502018-02-21 12:07:41 -0800727{
728 application_t *app;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700729 app = application_get (s->opaque);
730 svm_fifo_unset_event (s->server_tx_fifo);
731 return app->cb_fns.builtin_app_tx_callback (s);
Florin Coras371ca502018-02-21 12:07:41 -0800732}
733
Dave Barach2c25a622017-06-26 11:35:07 -0400734always_inline stream_session_t *
Florin Coras52207f12018-07-12 14:48:06 -0700735session_event_get_session (session_event_t * e, u8 thread_index)
Florin Corasa5464812017-04-19 13:00:05 -0700736{
Florin Coras3cbc04b2017-10-02 00:18:51 -0700737 return session_get_if_valid (e->fifo->master_session_index, thread_index);
Florin Corasa5464812017-04-19 13:00:05 -0700738}
739
Florin Coras0d60a0f2018-06-29 02:02:08 -0700740static uword
741session_queue_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
742 vlib_frame_t * frame)
743{
744 session_manager_main_t *smm = vnet_get_session_manager_main ();
Florin Coras3c2fed52018-07-04 04:15:05 -0700745 u32 thread_index = vm->thread_index, n_to_dequeue, n_events;
Florin Coras52207f12018-07-12 14:48:06 -0700746 session_event_t *pending_events, *e;
747 session_event_t *fifo_events;
Florin Coras3c2fed52018-07-04 04:15:05 -0700748 svm_msg_q_msg_t _msg, *msg = &_msg;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700749 f64 now = vlib_time_now (vm);
Florin Coras3c2fed52018-07-04 04:15:05 -0700750 int n_tx_packets = 0, i, rv;
Florin Coras15531972018-08-12 23:50:53 -0700751 app_worker_t *app_wrk;
Florin Coras3c2fed52018-07-04 04:15:05 -0700752 application_t *app;
753 svm_msg_q_t *mq;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700754 void (*fp) (void *);
755
756 SESSION_EVT_DBG (SESSION_EVT_POLL_GAP_TRACK, smm, thread_index);
757
758 /*
759 * Update transport time
760 */
761 transport_update_time (now, thread_index);
762
763 /*
Florin Coras3c2fed52018-07-04 04:15:05 -0700764 * Get vpp queue events that we can dequeue without blocking
Florin Coras0d60a0f2018-06-29 02:02:08 -0700765 */
Florin Coras3c2fed52018-07-04 04:15:05 -0700766 mq = smm->vpp_event_queues[thread_index];
Florin Coras0d60a0f2018-06-29 02:02:08 -0700767 fifo_events = smm->free_event_vector[thread_index];
Florin Coras3c2fed52018-07-04 04:15:05 -0700768 n_to_dequeue = svm_msg_q_size (mq);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700769 pending_events = smm->pending_event_vector[thread_index];
770
771 if (!n_to_dequeue && !vec_len (pending_events)
772 && !vec_len (smm->pending_disconnects[thread_index]))
773 return 0;
774
775 SESSION_EVT_DBG (SESSION_EVT_DEQ_NODE, 0);
776
777 /*
778 * If we didn't manage to process previous events try going
779 * over them again without dequeuing new ones.
780 * XXX: Handle senders to sessions that can't keep up
781 */
782 if (0 && vec_len (pending_events) >= 100)
783 {
784 clib_warning ("too many fifo events unsolved");
785 goto skip_dequeue;
786 }
787
788 /* See you in the next life, don't be late
Florin Coras3c2fed52018-07-04 04:15:05 -0700789 * XXX: we may need priorities here */
790 if (svm_msg_q_try_lock (mq))
Florin Coras0d60a0f2018-06-29 02:02:08 -0700791 return 0;
792
793 for (i = 0; i < n_to_dequeue; i++)
794 {
795 vec_add2 (fifo_events, e, 1);
Florin Coras3c2fed52018-07-04 04:15:05 -0700796 svm_msg_q_sub_w_lock (mq, msg);
797 clib_memcpy (e, svm_msg_q_msg_data (mq, msg), sizeof (*e));
798 svm_msg_q_free_msg (mq, msg);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700799 }
800
Florin Coras3c2fed52018-07-04 04:15:05 -0700801 svm_msg_q_unlock (mq);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700802
803 vec_append (fifo_events, pending_events);
804 vec_append (fifo_events, smm->pending_disconnects[thread_index]);
805
806 _vec_len (pending_events) = 0;
807 smm->pending_event_vector[thread_index] = pending_events;
808 _vec_len (smm->pending_disconnects[thread_index]) = 0;
809
810skip_dequeue:
811 n_events = vec_len (fifo_events);
812 for (i = 0; i < n_events; i++)
813 {
814 stream_session_t *s; /* $$$ prefetch 1 ahead maybe */
Florin Coras52207f12018-07-12 14:48:06 -0700815 session_event_t *e;
Florin Coras54693d22018-07-17 10:46:29 -0700816 u8 is_full;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700817
818 e = &fifo_events[i];
819 switch (e->event_type)
820 {
821 case FIFO_EVENT_APP_TX:
822 /* Don't try to send more that one frame per dispatch cycle */
823 if (n_tx_packets == VLIB_FRAME_SIZE)
824 {
825 vec_add1 (smm->pending_event_vector[thread_index], *e);
826 break;
827 }
828
829 s = session_event_get_session (e, thread_index);
830 if (PREDICT_FALSE (!s))
831 {
832 clib_warning ("It's dead, Jim!");
833 continue;
834 }
Florin Coras54693d22018-07-17 10:46:29 -0700835 is_full = svm_fifo_is_full (s->server_tx_fifo);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700836
837 /* Spray packets in per session type frames, since they go to
838 * different nodes */
839 rv = (smm->session_tx_fns[s->session_type]) (vm, node, e, s,
840 &n_tx_packets);
841 if (PREDICT_TRUE (rv == SESSION_TX_OK))
842 {
Florin Coras46f001d2018-07-11 05:25:06 -0700843 /* Notify app there's tx space if not polling */
Florin Coras54693d22018-07-17 10:46:29 -0700844 if (PREDICT_FALSE (is_full
Florin Coras46f001d2018-07-11 05:25:06 -0700845 && !svm_fifo_has_event (s->server_tx_fifo)))
846 session_dequeue_notify (s);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700847 }
848 else if (PREDICT_FALSE (rv == SESSION_TX_NO_BUFFERS))
849 {
850 vlib_node_increment_counter (vm, node->node_index,
851 SESSION_QUEUE_ERROR_NO_BUFFER, 1);
852 continue;
853 }
854 break;
855 case FIFO_EVENT_DISCONNECT:
856 /* Make sure stream disconnects run after the pending list is
857 * drained */
858 s = session_get_from_handle (e->session_handle);
859 if (!e->postponed)
860 {
861 e->postponed = 1;
862 vec_add1 (smm->pending_disconnects[thread_index], *e);
863 continue;
864 }
865 /* If tx queue is still not empty, wait */
866 if (svm_fifo_max_dequeue (s->server_tx_fifo))
867 {
868 vec_add1 (smm->pending_disconnects[thread_index], *e);
869 continue;
870 }
871
872 stream_session_disconnect_transport (s);
873 break;
874 case FIFO_EVENT_BUILTIN_RX:
875 s = session_event_get_session (e, thread_index);
876 if (PREDICT_FALSE (!s))
877 continue;
878 svm_fifo_unset_event (s->server_rx_fifo);
Florin Coras15531972018-08-12 23:50:53 -0700879 app_wrk = app_worker_get (s->app_wrk_index);
880 app = application_get (app_wrk->app_index);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700881 app->cb_fns.builtin_app_rx_callback (s);
882 break;
883 case FIFO_EVENT_RPC:
884 fp = e->rpc_args.fp;
885 (*fp) (e->rpc_args.arg);
886 break;
Florin Coras52207f12018-07-12 14:48:06 -0700887 case SESSION_CTRL_EVT_DISCONNECTED:
888 session_mq_disconnected_handler (e->data);
889 break;
890 case SESSION_CTRL_EVT_ACCEPTED_REPLY:
891 session_mq_accepted_reply_handler (e->data);
892 break;
893 case SESSION_CTRL_EVT_CONNECTED_REPLY:
894 break;
895 case SESSION_CTRL_EVT_DISCONNECTED_REPLY:
896 session_mq_disconnected_reply_handler (e->data);
897 break;
898 case SESSION_CTRL_EVT_RESET_REPLY:
899 session_mq_reset_reply_handler (e->data);
900 break;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700901 default:
902 clib_warning ("unhandled event type %d", e->event_type);
903 }
904 }
905
906 _vec_len (fifo_events) = 0;
907 smm->free_event_vector[thread_index] = fifo_events;
908
909 vlib_node_increment_counter (vm, session_queue_node.index,
910 SESSION_QUEUE_ERROR_TX, n_tx_packets);
911
912 SESSION_EVT_DBG (SESSION_EVT_DISPATCH_END, smm, thread_index);
913
914 return n_tx_packets;
915}
916
917/* *INDENT-OFF* */
918VLIB_REGISTER_NODE (session_queue_node) =
919{
920 .function = session_queue_node_fn,
921 .name = "session-queue",
922 .format_trace = format_session_queue_trace,
923 .type = VLIB_NODE_TYPE_INPUT,
924 .n_errors = ARRAY_LEN (session_queue_error_strings),
925 .error_strings = session_queue_error_strings,
926 .state = VLIB_NODE_STATE_DISABLED,
927};
928/* *INDENT-ON* */
929
Dave Barachacd2a6a2017-05-16 17:41:34 -0400930void
931dump_thread_0_event_queue (void)
932{
933 session_manager_main_t *smm = vnet_get_session_manager_main ();
934 vlib_main_t *vm = &vlib_global_main;
935 u32 my_thread_index = vm->thread_index;
Florin Coras52207f12018-07-12 14:48:06 -0700936 session_event_t _e, *e = &_e;
Florin Coras3c2fed52018-07-04 04:15:05 -0700937 svm_msg_q_ring_t *ring;
Dave Barachacd2a6a2017-05-16 17:41:34 -0400938 stream_session_t *s0;
Florin Coras3c2fed52018-07-04 04:15:05 -0700939 svm_msg_q_msg_t *msg;
940 svm_msg_q_t *mq;
Dave Barachacd2a6a2017-05-16 17:41:34 -0400941 int i, index;
Dave Barachacd2a6a2017-05-16 17:41:34 -0400942
Florin Coras3c2fed52018-07-04 04:15:05 -0700943 mq = smm->vpp_event_queues[my_thread_index];
944 index = mq->q->head;
Dave Barachacd2a6a2017-05-16 17:41:34 -0400945
Florin Coras3c2fed52018-07-04 04:15:05 -0700946 for (i = 0; i < mq->q->cursize; i++)
Dave Barachacd2a6a2017-05-16 17:41:34 -0400947 {
Florin Coras3c2fed52018-07-04 04:15:05 -0700948 msg = (svm_msg_q_msg_t *) (&mq->q->data[0] + mq->q->elsize * index);
949 ring = svm_msg_q_ring (mq, msg->ring_index);
950 clib_memcpy (e, svm_msg_q_msg_data (mq, msg), ring->elsize);
Dave Barachacd2a6a2017-05-16 17:41:34 -0400951
952 switch (e->event_type)
953 {
954 case FIFO_EVENT_APP_TX:
955 s0 = session_event_get_session (e, my_thread_index);
956 fformat (stdout, "[%04d] TX session %d\n", i, s0->session_index);
957 break;
958
959 case FIFO_EVENT_DISCONNECT:
Florin Corascea194d2017-10-02 00:18:51 -0700960 s0 = session_get_from_handle (e->session_handle);
Dave Barachacd2a6a2017-05-16 17:41:34 -0400961 fformat (stdout, "[%04d] disconnect session %d\n", i,
962 s0->session_index);
963 break;
964
965 case FIFO_EVENT_BUILTIN_RX:
966 s0 = session_event_get_session (e, my_thread_index);
967 fformat (stdout, "[%04d] builtin_rx %d\n", i, s0->session_index);
968 break;
969
970 case FIFO_EVENT_RPC:
971 fformat (stdout, "[%04d] RPC call %llx with %llx\n",
972 i, (u64) (e->rpc_args.fp), (u64) (e->rpc_args.arg));
973 break;
974
975 default:
976 fformat (stdout, "[%04d] unhandled event type %d\n",
977 i, e->event_type);
978 break;
979 }
980
981 index++;
982
Florin Coras3c2fed52018-07-04 04:15:05 -0700983 if (index == mq->q->maxsize)
Dave Barachacd2a6a2017-05-16 17:41:34 -0400984 index = 0;
985 }
986}
987
Florin Coras6534b7a2017-07-18 05:38:03 -0400988static u8
Florin Coras52207f12018-07-12 14:48:06 -0700989session_node_cmp_event (session_event_t * e, svm_fifo_t * f)
Florin Coras6534b7a2017-07-18 05:38:03 -0400990{
991 stream_session_t *s;
992 switch (e->event_type)
993 {
994 case FIFO_EVENT_APP_RX:
995 case FIFO_EVENT_APP_TX:
996 case FIFO_EVENT_BUILTIN_RX:
997 if (e->fifo == f)
998 return 1;
999 break;
1000 case FIFO_EVENT_DISCONNECT:
1001 break;
1002 case FIFO_EVENT_RPC:
Florin Corascea194d2017-10-02 00:18:51 -07001003 s = session_get_from_handle (e->session_handle);
Florin Coras6534b7a2017-07-18 05:38:03 -04001004 if (!s)
1005 {
1006 clib_warning ("session has event but doesn't exist!");
1007 break;
1008 }
1009 if (s->server_rx_fifo == f || s->server_tx_fifo == f)
1010 return 1;
1011 break;
1012 default:
1013 break;
1014 }
1015 return 0;
1016}
1017
1018u8
Florin Coras52207f12018-07-12 14:48:06 -07001019session_node_lookup_fifo_event (svm_fifo_t * f, session_event_t * e)
Florin Coras6534b7a2017-07-18 05:38:03 -04001020{
1021 session_manager_main_t *smm = vnet_get_session_manager_main ();
Florin Coras3c2fed52018-07-04 04:15:05 -07001022 svm_msg_q_t *mq;
Florin Coras52207f12018-07-12 14:48:06 -07001023 session_event_t *pending_event_vector, *evt;
Florin Coras6534b7a2017-07-18 05:38:03 -04001024 int i, index, found = 0;
Florin Coras3c2fed52018-07-04 04:15:05 -07001025 svm_msg_q_msg_t *msg;
1026 svm_msg_q_ring_t *ring;
Florin Coras6534b7a2017-07-18 05:38:03 -04001027 u8 thread_index;
1028
1029 ASSERT (e);
1030 thread_index = f->master_thread_index;
1031 /*
1032 * Search evt queue
1033 */
Florin Coras3c2fed52018-07-04 04:15:05 -07001034 mq = smm->vpp_event_queues[thread_index];
1035 index = mq->q->head;
1036 for (i = 0; i < mq->q->cursize; i++)
Florin Coras6534b7a2017-07-18 05:38:03 -04001037 {
Florin Coras3c2fed52018-07-04 04:15:05 -07001038 msg = (svm_msg_q_msg_t *) (&mq->q->data[0] + mq->q->elsize * index);
1039 ring = svm_msg_q_ring (mq, msg->ring_index);
1040 clib_memcpy (e, svm_msg_q_msg_data (mq, msg), ring->elsize);
Florin Coras6534b7a2017-07-18 05:38:03 -04001041 found = session_node_cmp_event (e, f);
1042 if (found)
Florin Coras371ca502018-02-21 12:07:41 -08001043 return 1;
Florin Coras3c2fed52018-07-04 04:15:05 -07001044 if (++index == mq->q->maxsize)
Florin Coras6534b7a2017-07-18 05:38:03 -04001045 index = 0;
1046 }
1047 /*
1048 * Search pending events vector
1049 */
1050 pending_event_vector = smm->pending_event_vector[thread_index];
1051 vec_foreach (evt, pending_event_vector)
1052 {
1053 found = session_node_cmp_event (evt, f);
1054 if (found)
1055 {
1056 clib_memcpy (e, evt, sizeof (*evt));
1057 break;
1058 }
1059 }
1060 return found;
1061}
1062
Dave Barach2a863912017-11-28 10:11:42 -05001063static clib_error_t *
1064session_queue_exit (vlib_main_t * vm)
1065{
1066 if (vec_len (vlib_mains) < 2)
1067 return 0;
1068
1069 /*
1070 * Shut off (especially) worker-thread session nodes.
1071 * Otherwise, vpp can crash as the main thread unmaps the
1072 * API segment.
1073 */
1074 vlib_worker_thread_barrier_sync (vm);
1075 session_node_enable_disable (0 /* is_enable */ );
1076 vlib_worker_thread_barrier_release (vm);
1077 return 0;
1078}
1079
1080VLIB_MAIN_LOOP_EXIT_FUNCTION (session_queue_exit);
1081
Florin Corasfd542f12018-05-16 19:28:24 -07001082static uword
1083session_queue_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
1084 vlib_frame_t * f)
1085{
1086 f64 now, timeout = 1.0;
1087 uword *event_data = 0;
1088 uword event_type;
1089
1090 while (1)
1091 {
1092 vlib_process_wait_for_event_or_clock (vm, timeout);
1093 now = vlib_time_now (vm);
1094 event_type = vlib_process_get_events (vm, (uword **) & event_data);
1095
1096 switch (event_type)
1097 {
1098 case SESSION_Q_PROCESS_FLUSH_FRAMES:
1099 /* Flush the frames by updating all transports times */
1100 transport_update_time (now, 0);
1101 break;
1102 case SESSION_Q_PROCESS_STOP:
1103 timeout = 100000.0;
1104 break;
1105 case ~0:
1106 /* Timed out. Update time for all transports to trigger all
1107 * outstanding retransmits. */
1108 transport_update_time (now, 0);
1109 break;
1110 }
1111 vec_reset_length (event_data);
1112 }
1113 return 0;
1114}
1115
1116/* *INDENT-OFF* */
1117VLIB_REGISTER_NODE (session_queue_process_node) =
1118{
1119 .function = session_queue_process,
1120 .type = VLIB_NODE_TYPE_PROCESS,
1121 .name = "session-queue-process",
1122 .state = VLIB_NODE_STATE_DISABLED,
1123};
1124/* *INDENT-ON* */
1125
1126
Dave Barach68b0fb02017-02-28 15:15:56 -05001127/*
1128 * fd.io coding-style-patch-verification: ON
1129 *
1130 * Local Variables:
1131 * eval: (c-set-style "gnu")
1132 * End:
1133 */