blob: fcb3a3dc016aedb5ff12e287d369b896f624b803 [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)
Florin Coras8e43d042018-05-04 15:46:57 -0700310 svm_fifo_dequeue_drop (f, hdr->data_length);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700311 }
312 else
Florin Coras8e43d042018-05-04 15:46:57 -0700313 n_bytes_read = svm_fifo_dequeue_nowait (ctx->s->server_tx_fifo,
314 len_to_deq, data);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700315 }
Florin Coras8e43d042018-05-04 15:46:57 -0700316 ASSERT (n_bytes_read == len_to_deq);
317 chain_b->current_length = n_bytes_read;
318 b->total_length_not_including_first_buffer += chain_b->current_length;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700319
320 /* update previous buffer */
Florin Coras8e43d042018-05-04 15:46:57 -0700321 prev_b->next_buffer = chain_bi0;
322 prev_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700323
324 /* update current buffer */
Florin Coras8e43d042018-05-04 15:46:57 -0700325 chain_b->next_buffer = 0;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700326
Florin Corasb2215d62017-08-01 16:56:58 -0700327 to_deq -= n_bytes_read;
328 if (to_deq == 0)
Florin Corasf6d68ed2017-05-07 19:12:02 -0700329 break;
330 }
Florin Coras1f152cd2017-08-18 19:28:03 -0700331 ASSERT (to_deq == 0
Florin Coras8e43d042018-05-04 15:46:57 -0700332 && b->total_length_not_including_first_buffer == left_from_seg);
333 ctx->left_to_snd -= left_from_seg;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700334}
335
Dave Barach68b0fb02017-02-28 15:15:56 -0500336always_inline int
Florin Coras8e43d042018-05-04 15:46:57 -0700337session_output_try_get_buffers (vlib_main_t * vm,
Florin Corasd79b41e2017-03-04 05:37:52 -0800338 session_manager_main_t * smm,
Florin Coras8e43d042018-05-04 15:46:57 -0700339 u32 thread_index, u16 * n_bufs, u32 wanted)
Dave Barach68b0fb02017-02-28 15:15:56 -0500340{
Florin Corasf08f26d2018-05-10 13:20:47 -0700341 u32 n_alloc;
342 vec_validate_aligned (smm->tx_buffers[thread_index], wanted - 1,
Florin Coras8e43d042018-05-04 15:46:57 -0700343 CLIB_CACHE_LINE_BYTES);
Florin Corasf08f26d2018-05-10 13:20:47 -0700344 n_alloc = vlib_buffer_alloc (vm, &smm->tx_buffers[thread_index][*n_bufs],
345 wanted - *n_bufs);
346 *n_bufs += n_alloc;
Florin Coras8e43d042018-05-04 15:46:57 -0700347 _vec_len (smm->tx_buffers[thread_index]) = *n_bufs;
Florin Corasf08f26d2018-05-10 13:20:47 -0700348 return n_alloc;
Florin Coras8e43d042018-05-04 15:46:57 -0700349}
350
351always_inline void
352session_tx_fill_buffer (vlib_main_t * vm, session_tx_context_t * ctx,
353 vlib_buffer_t * b, u16 * n_bufs, u8 peek_data)
354{
355 u32 len_to_deq;
356 u8 *data0;
357 int n_bytes_read;
358
359 /*
360 * Start with the first buffer in chain
361 */
362 b->error = 0;
363 b->flags = VNET_BUFFER_F_LOCALLY_ORIGINATED;
364 b->current_data = 0;
Florin Coras8e43d042018-05-04 15:46:57 -0700365
366 data0 = vlib_buffer_make_headroom (b, MAX_HDRS_LEN);
367 len_to_deq = clib_min (ctx->left_to_snd, ctx->deq_per_first_buf);
368
Florin Coras7fb0fe12018-04-09 09:24:52 -0700369 if (peek_data)
370 {
Florin Coras8e43d042018-05-04 15:46:57 -0700371 n_bytes_read = svm_fifo_peek (ctx->s->server_tx_fifo, ctx->tx_offset,
372 len_to_deq, data0);
373 ASSERT (n_bytes_read > 0);
374 /* Keep track of progress locally, transport is also supposed to
375 * increment it independently when pushing the header */
376 ctx->tx_offset += n_bytes_read;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700377 }
378 else
379 {
Florin Coras8e43d042018-05-04 15:46:57 -0700380 if (ctx->transport_vft->tx_type == TRANSPORT_TX_DGRAM)
381 {
382 session_dgram_hdr_t *hdr = &ctx->hdr;
383 svm_fifo_t *f = ctx->s->server_tx_fifo;
384 u16 deq_now;
385 u32 offset;
386
387 ASSERT (hdr->data_length > hdr->data_offset);
388 deq_now = clib_min (hdr->data_length - hdr->data_offset,
389 len_to_deq);
390 offset = hdr->data_offset + SESSION_CONN_HDR_LEN;
391 n_bytes_read = svm_fifo_peek (f, offset, deq_now, data0);
392 ASSERT (n_bytes_read > 0);
393
394 if (ctx->s->session_state == SESSION_STATE_LISTENING)
395 {
396 ip_copy (&ctx->tc->rmt_ip, &hdr->rmt_ip, ctx->tc->is_ip4);
397 ctx->tc->rmt_port = hdr->rmt_port;
398 }
399 hdr->data_offset += n_bytes_read;
400 if (hdr->data_offset == hdr->data_length)
401 {
402 offset = hdr->data_length + SESSION_CONN_HDR_LEN;
403 svm_fifo_dequeue_drop (f, offset);
404 }
405 }
Florin Coras7fb0fe12018-04-09 09:24:52 -0700406 else
407 {
Florin Coras8e43d042018-05-04 15:46:57 -0700408 n_bytes_read = svm_fifo_dequeue_nowait (ctx->s->server_tx_fifo,
409 len_to_deq, data0);
410 ASSERT (n_bytes_read > 0);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700411 }
412 }
Florin Coras8e43d042018-05-04 15:46:57 -0700413 b->current_length = n_bytes_read;
414 ctx->left_to_snd -= n_bytes_read;
Dave Barach68b0fb02017-02-28 15:15:56 -0500415
Florin Coras8e43d042018-05-04 15:46:57 -0700416 /*
417 * Fill in the remaining buffers in the chain, if any
418 */
419 if (PREDICT_FALSE (ctx->n_bufs_per_seg > 1 && ctx->left_to_snd))
420 session_tx_fifo_chain_tail (vm, ctx, b, n_bufs, peek_data);
Dave Barach68b0fb02017-02-28 15:15:56 -0500421
Florin Coras8e43d042018-05-04 15:46:57 -0700422 /* *INDENT-OFF* */
Florin Coras8b20bf52018-06-14 14:55:50 -0700423 SESSION_EVT_DBG(SESSION_EVT_DEQ, ctx->s, ({
424 ed->data[0] = FIFO_EVENT_APP_TX;
425 ed->data[1] = ctx->max_dequeue;
Florin Coras8e43d042018-05-04 15:46:57 -0700426 ed->data[2] = len_to_deq;
Florin Coras8b20bf52018-06-14 14:55:50 -0700427 ed->data[3] = ctx->left_to_snd;
Florin Coras8e43d042018-05-04 15:46:57 -0700428 }));
429 /* *INDENT-ON* */
430}
431
432always_inline u8
433session_tx_not_ready (stream_session_t * s, u8 peek_data)
434{
435 if (peek_data)
Florin Corase04c2992017-03-01 08:17:34 -0800436 {
Florin Coras8e43d042018-05-04 15:46:57 -0700437 /* Can retransmit for closed sessions but can't send new data if
438 * session is not ready or closed */
439 if (s->session_state < SESSION_STATE_READY)
440 return 1;
Florin Corasca1c8f32018-05-23 21:01:30 -0700441 if (s->session_state == SESSION_STATE_CLOSED)
442 return 2;
Florin Corase04c2992017-03-01 08:17:34 -0800443 }
Florin Coras8e43d042018-05-04 15:46:57 -0700444 return 0;
445}
Dave Barach68b0fb02017-02-28 15:15:56 -0500446
Florin Coras8e43d042018-05-04 15:46:57 -0700447always_inline transport_connection_t *
448session_tx_get_transport (session_tx_context_t * ctx, u8 peek_data)
449{
450 if (peek_data)
451 {
452 return ctx->transport_vft->get_connection (ctx->s->connection_index,
453 ctx->s->thread_index);
454 }
455 else
456 {
457 if (ctx->s->session_state == SESSION_STATE_LISTENING)
458 return ctx->transport_vft->get_listener (ctx->s->connection_index);
459 else
460 {
461 return ctx->transport_vft->get_connection (ctx->s->connection_index,
462 ctx->s->thread_index);
463 }
464 }
465}
Florin Coras6a9b68b2017-11-21 04:20:42 -0800466
Florin Coras8e43d042018-05-04 15:46:57 -0700467always_inline void
468session_tx_set_dequeue_params (vlib_main_t * vm, session_tx_context_t * ctx,
Florin Corasf08f26d2018-05-10 13:20:47 -0700469 u32 max_segs, u8 peek_data)
Florin Coras8e43d042018-05-04 15:46:57 -0700470{
471 u32 n_bytes_per_buf, n_bytes_per_seg;
472 ctx->max_dequeue = svm_fifo_max_dequeue (ctx->s->server_tx_fifo);
Dave Barach68b0fb02017-02-28 15:15:56 -0500473 if (peek_data)
474 {
Florin Coras9d063042017-09-14 03:08:00 -0400475 /* Offset in rx fifo from where to peek data */
Florin Coras8e43d042018-05-04 15:46:57 -0700476 ctx->tx_offset = ctx->transport_vft->tx_fifo_offset (ctx->tc);
477 if (PREDICT_FALSE (ctx->tx_offset >= ctx->max_dequeue))
478 {
479 ctx->max_len_to_snd = 0;
480 return;
481 }
482 ctx->max_dequeue -= ctx->tx_offset;
Dave Barach68b0fb02017-02-28 15:15:56 -0500483 }
Florin Coras7fb0fe12018-04-09 09:24:52 -0700484 else
485 {
Florin Coras8e43d042018-05-04 15:46:57 -0700486 if (ctx->transport_vft->tx_type == TRANSPORT_TX_DGRAM)
Florin Coras7fb0fe12018-04-09 09:24:52 -0700487 {
Florin Coras8e43d042018-05-04 15:46:57 -0700488 if (ctx->max_dequeue <= sizeof (ctx->hdr))
489 {
490 ctx->max_len_to_snd = 0;
491 return;
492 }
493 svm_fifo_peek (ctx->s->server_tx_fifo, 0, sizeof (ctx->hdr),
494 (u8 *) & ctx->hdr);
495 ASSERT (ctx->hdr.data_length > ctx->hdr.data_offset);
496 ctx->max_dequeue = ctx->hdr.data_length - ctx->hdr.data_offset;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700497 }
498 }
Florin Coras8e43d042018-05-04 15:46:57 -0700499 ASSERT (ctx->max_dequeue > 0);
Florin Coras6792ec02017-03-13 03:49:51 -0700500
501 /* Ensure we're not writing more than transport window allows */
Florin Coras8e43d042018-05-04 15:46:57 -0700502 if (ctx->max_dequeue < ctx->snd_space)
Florin Coras3e350af2017-03-30 02:54:28 -0700503 {
504 /* Constrained by tx queue. Try to send only fully formed segments */
Florin Coras8e43d042018-05-04 15:46:57 -0700505 ctx->max_len_to_snd =
506 (ctx->max_dequeue > ctx->snd_mss) ?
507 ctx->max_dequeue - ctx->max_dequeue % ctx->snd_mss : ctx->max_dequeue;
Florin Coras3e350af2017-03-30 02:54:28 -0700508 /* TODO Nagle ? */
509 }
510 else
511 {
Florin Coras1f152cd2017-08-18 19:28:03 -0700512 /* Expectation is that snd_space0 is already a multiple of snd_mss */
Florin Coras8e43d042018-05-04 15:46:57 -0700513 ctx->max_len_to_snd = ctx->snd_space;
Florin Coras3e350af2017-03-30 02:54:28 -0700514 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500515
Florin Corasf08f26d2018-05-10 13:20:47 -0700516 /* Check if we're tx constrained by the node */
517 ctx->n_segs_per_evt = ceil ((f64) ctx->max_len_to_snd / ctx->snd_mss);
518 if (ctx->n_segs_per_evt > max_segs)
519 {
520 ctx->n_segs_per_evt = max_segs;
521 ctx->max_len_to_snd = max_segs * ctx->snd_mss;
522 }
523
Florin Coras8e43d042018-05-04 15:46:57 -0700524 n_bytes_per_buf = vlib_buffer_free_list_buffer_size (vm,
525 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
Florin Corase87216f2017-08-17 16:59:22 -0700526 ASSERT (n_bytes_per_buf > MAX_HDRS_LEN);
Florin Coras8e43d042018-05-04 15:46:57 -0700527 n_bytes_per_seg = MAX_HDRS_LEN + ctx->snd_mss;
Florin Corasf08f26d2018-05-10 13:20:47 -0700528 ctx->n_bufs_per_seg = ceil ((f64) n_bytes_per_seg / n_bytes_per_buf);
Florin Coras8e43d042018-05-04 15:46:57 -0700529 ctx->deq_per_buf = clib_min (ctx->snd_mss, n_bytes_per_buf);
530 ctx->deq_per_first_buf = clib_min (ctx->snd_mss,
531 n_bytes_per_buf - MAX_HDRS_LEN);
532}
Florin Corasf6d68ed2017-05-07 19:12:02 -0700533
Florin Coras8e43d042018-05-04 15:46:57 -0700534always_inline int
535session_tx_fifo_read_and_snd_i (vlib_main_t * vm, vlib_node_runtime_t * node,
Florin Coras52207f12018-07-12 14:48:06 -0700536 session_event_t * e,
Florin Coras8e43d042018-05-04 15:46:57 -0700537 stream_session_t * s, int *n_tx_packets,
538 u8 peek_data)
539{
Florin Coras66cf5e02018-06-08 09:17:39 -0700540 u32 next_index, next0, next1, *to_next, n_left_to_next;
Florin Corasf08f26d2018-05-10 13:20:47 -0700541 u32 n_trace = vlib_get_trace_count (vm, node), n_bufs_needed = 0;
542 u32 thread_index = s->thread_index, n_left, pbi;
Florin Coras8e43d042018-05-04 15:46:57 -0700543 session_manager_main_t *smm = &session_manager_main;
544 session_tx_context_t *ctx = &smm->ctx[thread_index];
545 transport_proto_t tp;
546 vlib_buffer_t *pb;
Florin Corasca1c8f32018-05-23 21:01:30 -0700547 u16 n_bufs, rv;
Florin Coras8e43d042018-05-04 15:46:57 -0700548
Florin Corasca1c8f32018-05-23 21:01:30 -0700549 if (PREDICT_FALSE ((rv = session_tx_not_ready (s, peek_data))))
Florin Coras8e43d042018-05-04 15:46:57 -0700550 {
Florin Corasca1c8f32018-05-23 21:01:30 -0700551 if (rv < 2)
552 vec_add1 (smm->pending_event_vector[thread_index], *e);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700553 return SESSION_TX_NO_DATA;
Florin Coras8e43d042018-05-04 15:46:57 -0700554 }
555
556 next_index = smm->session_type_to_next[s->session_type];
Florin Coras66cf5e02018-06-08 09:17:39 -0700557 next0 = next1 = next_index;
Florin Coras8e43d042018-05-04 15:46:57 -0700558
559 tp = session_get_transport_proto (s);
560 ctx->s = s;
561 ctx->transport_vft = transport_protocol_get_vft (tp);
562 ctx->tc = session_tx_get_transport (ctx, peek_data);
563 ctx->snd_mss = ctx->transport_vft->send_mss (ctx->tc);
564 ctx->snd_space = ctx->transport_vft->send_space (ctx->tc);
565 if (ctx->snd_space == 0 || ctx->snd_mss == 0)
566 {
567 vec_add1 (smm->pending_event_vector[thread_index], *e);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700568 return SESSION_TX_NO_DATA;
Florin Coras8e43d042018-05-04 15:46:57 -0700569 }
570
571 /* Allow enqueuing of a new event */
572 svm_fifo_unset_event (s->server_tx_fifo);
573
574 /* Check how much we can pull. */
Florin Corasf08f26d2018-05-10 13:20:47 -0700575 session_tx_set_dequeue_params (vm, ctx, VLIB_FRAME_SIZE - *n_tx_packets,
576 peek_data);
577
Florin Coras8e43d042018-05-04 15:46:57 -0700578 if (PREDICT_FALSE (!ctx->max_len_to_snd))
Florin Coras0d60a0f2018-06-29 02:02:08 -0700579 return SESSION_TX_NO_DATA;
Dave Barach68b0fb02017-02-28 15:15:56 -0500580
581 n_bufs = vec_len (smm->tx_buffers[thread_index]);
Florin Corasf08f26d2018-05-10 13:20:47 -0700582 n_bufs_needed = ctx->n_segs_per_evt * ctx->n_bufs_per_seg;
Florin Coras8e43d042018-05-04 15:46:57 -0700583
584 /*
585 * Make sure we have at least one full frame of buffers ready
586 */
Florin Corasf08f26d2018-05-10 13:20:47 -0700587 if (n_bufs < n_bufs_needed)
Dave Barach68b0fb02017-02-28 15:15:56 -0500588 {
Florin Coras8e43d042018-05-04 15:46:57 -0700589 session_output_try_get_buffers (vm, smm, thread_index, &n_bufs,
Florin Corasf08f26d2018-05-10 13:20:47 -0700590 ctx->n_bufs_per_seg * VLIB_FRAME_SIZE);
591 if (PREDICT_FALSE (n_bufs < n_bufs_needed))
Dave Barach68b0fb02017-02-28 15:15:56 -0500592 {
Florin Coras8e43d042018-05-04 15:46:57 -0700593 vec_add1 (smm->pending_event_vector[thread_index], *e);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700594 return SESSION_TX_NO_BUFFERS;
Dave Barach68b0fb02017-02-28 15:15:56 -0500595 }
Florin Coras8e43d042018-05-04 15:46:57 -0700596 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500597
Florin Coras8e43d042018-05-04 15:46:57 -0700598 /*
599 * Write until we fill up a frame
600 */
601 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Florin Corasf08f26d2018-05-10 13:20:47 -0700602 if (PREDICT_FALSE (ctx->n_segs_per_evt > n_left_to_next))
Florin Coras8e43d042018-05-04 15:46:57 -0700603 {
Florin Corasf08f26d2018-05-10 13:20:47 -0700604 ctx->n_segs_per_evt = n_left_to_next;
605 ctx->max_len_to_snd = ctx->snd_mss * n_left_to_next;
606 }
607 ctx->left_to_snd = ctx->max_len_to_snd;
608 n_left = ctx->n_segs_per_evt;
Florin Coras66cf5e02018-06-08 09:17:39 -0700609
610 while (n_left >= 4)
611 {
612 vlib_buffer_t *b0, *b1;
613 u32 bi0, bi1;
614
615 pbi = smm->tx_buffers[thread_index][n_bufs - 3];
616 pb = vlib_get_buffer (vm, pbi);
617 vlib_prefetch_buffer_header (pb, STORE);
618 pbi = smm->tx_buffers[thread_index][n_bufs - 4];
619 pb = vlib_get_buffer (vm, pbi);
620 vlib_prefetch_buffer_header (pb, STORE);
621
622 to_next[0] = bi0 = smm->tx_buffers[thread_index][--n_bufs];
623 to_next[1] = bi1 = smm->tx_buffers[thread_index][--n_bufs];
624
625 b0 = vlib_get_buffer (vm, bi0);
626 b1 = vlib_get_buffer (vm, bi1);
627
628 session_tx_fill_buffer (vm, ctx, b0, &n_bufs, peek_data);
629 session_tx_fill_buffer (vm, ctx, b1, &n_bufs, peek_data);
630
631 ctx->transport_vft->push_header (ctx->tc, b0);
632 ctx->transport_vft->push_header (ctx->tc, b1);
633
634 to_next += 2;
635 n_left_to_next -= 2;
636 n_left -= 2;
637
638 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
639 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b1);
640
641 vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
642 n_left_to_next, bi0, bi1, next0,
643 next1);
644 }
Florin Corasf08f26d2018-05-10 13:20:47 -0700645 while (n_left)
646 {
Florin Coras66cf5e02018-06-08 09:17:39 -0700647 vlib_buffer_t *b0;
648 u32 bi0;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700649
Florin Coras91ca4622018-06-30 11:27:59 -0700650 if (n_left > 1)
651 {
652 pbi = smm->tx_buffers[thread_index][n_bufs - 2];
653 pb = vlib_get_buffer (vm, pbi);
654 vlib_prefetch_buffer_header (pb, STORE);
655 }
656
Florin Coras66cf5e02018-06-08 09:17:39 -0700657 to_next[0] = bi0 = smm->tx_buffers[thread_index][--n_bufs];
658 b0 = vlib_get_buffer (vm, bi0);
659 session_tx_fill_buffer (vm, ctx, b0, &n_bufs, peek_data);
Florin Coras81a13db2018-03-16 08:48:31 -0700660
Florin Coras66cf5e02018-06-08 09:17:39 -0700661 /* Ask transport to push header after current_length and
662 * total_length_not_including_first_buffer are updated */
663 ctx->transport_vft->push_header (ctx->tc, b0);
Florin Corasf6359c82017-06-19 12:26:09 -0400664
Florin Coras66cf5e02018-06-08 09:17:39 -0700665 to_next += 1;
666 n_left_to_next -= 1;
667 n_left -= 1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500668
Florin Coras66cf5e02018-06-08 09:17:39 -0700669 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700670
Florin Coras66cf5e02018-06-08 09:17:39 -0700671 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
672 n_left_to_next, bi0, next0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500673 }
Florin Coras66cf5e02018-06-08 09:17:39 -0700674
675 if (PREDICT_FALSE (n_trace > 0))
676 session_tx_trace_frame (vm, node, next_index, to_next,
677 ctx->n_segs_per_evt, s, n_trace);
678
Florin Coras8e43d042018-05-04 15:46:57 -0700679 _vec_len (smm->tx_buffers[thread_index]) = n_bufs;
Florin Corasf08f26d2018-05-10 13:20:47 -0700680 *n_tx_packets += ctx->n_segs_per_evt;
Florin Coras8e43d042018-05-04 15:46:57 -0700681 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
Dave Barach68b0fb02017-02-28 15:15:56 -0500682
Florin Coras6792ec02017-03-13 03:49:51 -0700683 /* If we couldn't dequeue all bytes mark as partially read */
Florin Corasf08f26d2018-05-10 13:20:47 -0700684 ASSERT (ctx->left_to_snd == 0);
Florin Coras8e43d042018-05-04 15:46:57 -0700685 if (ctx->max_len_to_snd < ctx->max_dequeue)
686 if (svm_fifo_set_event (s->server_tx_fifo))
687 vec_add1 (smm->pending_event_vector[thread_index], *e);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700688
Florin Coras8e43d042018-05-04 15:46:57 -0700689 if (!peek_data && ctx->transport_vft->tx_type == TRANSPORT_TX_DGRAM)
Dave Barach68b0fb02017-02-28 15:15:56 -0500690 {
Florin Coras7fb0fe12018-04-09 09:24:52 -0700691 /* Fix dgram pre header */
Florin Coras8e43d042018-05-04 15:46:57 -0700692 if (ctx->max_len_to_snd < ctx->max_dequeue)
693 svm_fifo_overwrite_head (s->server_tx_fifo, (u8 *) & ctx->hdr,
Florin Coras7fb0fe12018-04-09 09:24:52 -0700694 sizeof (session_dgram_pre_hdr_t));
695 /* More data needs to be read */
Florin Coras8e43d042018-05-04 15:46:57 -0700696 else if (svm_fifo_max_dequeue (s->server_tx_fifo) > 0)
697 if (svm_fifo_set_event (s->server_tx_fifo))
698 vec_add1 (smm->pending_event_vector[thread_index], *e);
Dave Barach68b0fb02017-02-28 15:15:56 -0500699 }
Florin Coras0d60a0f2018-06-29 02:02:08 -0700700 return SESSION_TX_OK;
Dave Barach68b0fb02017-02-28 15:15:56 -0500701}
702
703int
Florin Corasd79b41e2017-03-04 05:37:52 -0800704session_tx_fifo_peek_and_snd (vlib_main_t * vm, vlib_node_runtime_t * node,
Florin Coras52207f12018-07-12 14:48:06 -0700705 session_event_t * e,
Florin Coras0d60a0f2018-06-29 02:02:08 -0700706 stream_session_t * s, int *n_tx_pkts)
Dave Barach68b0fb02017-02-28 15:15:56 -0500707{
Florin Coras0d60a0f2018-06-29 02:02:08 -0700708 return session_tx_fifo_read_and_snd_i (vm, node, e, s, n_tx_pkts, 1);
Dave Barach68b0fb02017-02-28 15:15:56 -0500709}
710
711int
Florin Corasd79b41e2017-03-04 05:37:52 -0800712session_tx_fifo_dequeue_and_snd (vlib_main_t * vm, vlib_node_runtime_t * node,
Florin Coras52207f12018-07-12 14:48:06 -0700713 session_event_t * e,
Florin Coras0d60a0f2018-06-29 02:02:08 -0700714 stream_session_t * s, int *n_tx_pkts)
Dave Barach68b0fb02017-02-28 15:15:56 -0500715{
Florin Coras0d60a0f2018-06-29 02:02:08 -0700716 return session_tx_fifo_read_and_snd_i (vm, node, e, s, n_tx_pkts, 0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500717}
718
Florin Coras371ca502018-02-21 12:07:41 -0800719int
720session_tx_fifo_dequeue_internal (vlib_main_t * vm,
721 vlib_node_runtime_t * node,
Florin Coras52207f12018-07-12 14:48:06 -0700722 session_event_t * e,
Florin Coras0d60a0f2018-06-29 02:02:08 -0700723 stream_session_t * s, int *n_tx_pkts)
Florin Coras371ca502018-02-21 12:07:41 -0800724{
725 application_t *app;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700726 app = application_get (s->opaque);
727 svm_fifo_unset_event (s->server_tx_fifo);
728 return app->cb_fns.builtin_app_tx_callback (s);
Florin Coras371ca502018-02-21 12:07:41 -0800729}
730
Dave Barach2c25a622017-06-26 11:35:07 -0400731always_inline stream_session_t *
Florin Coras52207f12018-07-12 14:48:06 -0700732session_event_get_session (session_event_t * e, u8 thread_index)
Florin Corasa5464812017-04-19 13:00:05 -0700733{
Florin Coras3cbc04b2017-10-02 00:18:51 -0700734 return session_get_if_valid (e->fifo->master_session_index, thread_index);
Florin Corasa5464812017-04-19 13:00:05 -0700735}
736
Florin Coras0d60a0f2018-06-29 02:02:08 -0700737static uword
738session_queue_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
739 vlib_frame_t * frame)
740{
741 session_manager_main_t *smm = vnet_get_session_manager_main ();
Florin Coras3c2fed52018-07-04 04:15:05 -0700742 u32 thread_index = vm->thread_index, n_to_dequeue, n_events;
Florin Coras52207f12018-07-12 14:48:06 -0700743 session_event_t *pending_events, *e;
744 session_event_t *fifo_events;
Florin Coras3c2fed52018-07-04 04:15:05 -0700745 svm_msg_q_msg_t _msg, *msg = &_msg;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700746 f64 now = vlib_time_now (vm);
Florin Coras3c2fed52018-07-04 04:15:05 -0700747 int n_tx_packets = 0, i, rv;
Florin Coras15531972018-08-12 23:50:53 -0700748 app_worker_t *app_wrk;
Florin Coras3c2fed52018-07-04 04:15:05 -0700749 application_t *app;
750 svm_msg_q_t *mq;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700751 void (*fp) (void *);
752
753 SESSION_EVT_DBG (SESSION_EVT_POLL_GAP_TRACK, smm, thread_index);
754
755 /*
756 * Update transport time
757 */
758 transport_update_time (now, thread_index);
759
760 /*
Florin Coras3c2fed52018-07-04 04:15:05 -0700761 * Get vpp queue events that we can dequeue without blocking
Florin Coras0d60a0f2018-06-29 02:02:08 -0700762 */
Florin Coras3c2fed52018-07-04 04:15:05 -0700763 mq = smm->vpp_event_queues[thread_index];
Florin Coras0d60a0f2018-06-29 02:02:08 -0700764 fifo_events = smm->free_event_vector[thread_index];
Florin Coras3c2fed52018-07-04 04:15:05 -0700765 n_to_dequeue = svm_msg_q_size (mq);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700766 pending_events = smm->pending_event_vector[thread_index];
767
768 if (!n_to_dequeue && !vec_len (pending_events)
769 && !vec_len (smm->pending_disconnects[thread_index]))
770 return 0;
771
772 SESSION_EVT_DBG (SESSION_EVT_DEQ_NODE, 0);
773
774 /*
775 * If we didn't manage to process previous events try going
776 * over them again without dequeuing new ones.
777 * XXX: Handle senders to sessions that can't keep up
778 */
779 if (0 && vec_len (pending_events) >= 100)
780 {
781 clib_warning ("too many fifo events unsolved");
782 goto skip_dequeue;
783 }
784
785 /* See you in the next life, don't be late
Florin Coras3c2fed52018-07-04 04:15:05 -0700786 * XXX: we may need priorities here */
787 if (svm_msg_q_try_lock (mq))
Florin Coras0d60a0f2018-06-29 02:02:08 -0700788 return 0;
789
790 for (i = 0; i < n_to_dequeue; i++)
791 {
792 vec_add2 (fifo_events, e, 1);
Florin Coras3c2fed52018-07-04 04:15:05 -0700793 svm_msg_q_sub_w_lock (mq, msg);
794 clib_memcpy (e, svm_msg_q_msg_data (mq, msg), sizeof (*e));
795 svm_msg_q_free_msg (mq, msg);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700796 }
797
Florin Coras3c2fed52018-07-04 04:15:05 -0700798 svm_msg_q_unlock (mq);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700799
800 vec_append (fifo_events, pending_events);
801 vec_append (fifo_events, smm->pending_disconnects[thread_index]);
802
803 _vec_len (pending_events) = 0;
804 smm->pending_event_vector[thread_index] = pending_events;
805 _vec_len (smm->pending_disconnects[thread_index]) = 0;
806
807skip_dequeue:
808 n_events = vec_len (fifo_events);
809 for (i = 0; i < n_events; i++)
810 {
811 stream_session_t *s; /* $$$ prefetch 1 ahead maybe */
Florin Coras52207f12018-07-12 14:48:06 -0700812 session_event_t *e;
Florin Coras54693d22018-07-17 10:46:29 -0700813 u8 is_full;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700814
815 e = &fifo_events[i];
816 switch (e->event_type)
817 {
818 case FIFO_EVENT_APP_TX:
819 /* Don't try to send more that one frame per dispatch cycle */
820 if (n_tx_packets == VLIB_FRAME_SIZE)
821 {
822 vec_add1 (smm->pending_event_vector[thread_index], *e);
823 break;
824 }
825
826 s = session_event_get_session (e, thread_index);
827 if (PREDICT_FALSE (!s))
828 {
829 clib_warning ("It's dead, Jim!");
830 continue;
831 }
Florin Coras54693d22018-07-17 10:46:29 -0700832 is_full = svm_fifo_is_full (s->server_tx_fifo);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700833
834 /* Spray packets in per session type frames, since they go to
835 * different nodes */
836 rv = (smm->session_tx_fns[s->session_type]) (vm, node, e, s,
837 &n_tx_packets);
838 if (PREDICT_TRUE (rv == SESSION_TX_OK))
839 {
Florin Coras46f001d2018-07-11 05:25:06 -0700840 /* Notify app there's tx space if not polling */
Florin Coras54693d22018-07-17 10:46:29 -0700841 if (PREDICT_FALSE (is_full
Florin Coras46f001d2018-07-11 05:25:06 -0700842 && !svm_fifo_has_event (s->server_tx_fifo)))
843 session_dequeue_notify (s);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700844 }
845 else if (PREDICT_FALSE (rv == SESSION_TX_NO_BUFFERS))
846 {
847 vlib_node_increment_counter (vm, node->node_index,
848 SESSION_QUEUE_ERROR_NO_BUFFER, 1);
849 continue;
850 }
851 break;
852 case FIFO_EVENT_DISCONNECT:
853 /* Make sure stream disconnects run after the pending list is
854 * drained */
855 s = session_get_from_handle (e->session_handle);
856 if (!e->postponed)
857 {
858 e->postponed = 1;
859 vec_add1 (smm->pending_disconnects[thread_index], *e);
860 continue;
861 }
862 /* If tx queue is still not empty, wait */
863 if (svm_fifo_max_dequeue (s->server_tx_fifo))
864 {
865 vec_add1 (smm->pending_disconnects[thread_index], *e);
866 continue;
867 }
868
869 stream_session_disconnect_transport (s);
870 break;
871 case FIFO_EVENT_BUILTIN_RX:
872 s = session_event_get_session (e, thread_index);
873 if (PREDICT_FALSE (!s))
874 continue;
875 svm_fifo_unset_event (s->server_rx_fifo);
Florin Coras15531972018-08-12 23:50:53 -0700876 app_wrk = app_worker_get (s->app_wrk_index);
877 app = application_get (app_wrk->app_index);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700878 app->cb_fns.builtin_app_rx_callback (s);
879 break;
880 case FIFO_EVENT_RPC:
881 fp = e->rpc_args.fp;
882 (*fp) (e->rpc_args.arg);
883 break;
Florin Coras52207f12018-07-12 14:48:06 -0700884 case SESSION_CTRL_EVT_DISCONNECTED:
885 session_mq_disconnected_handler (e->data);
886 break;
887 case SESSION_CTRL_EVT_ACCEPTED_REPLY:
888 session_mq_accepted_reply_handler (e->data);
889 break;
890 case SESSION_CTRL_EVT_CONNECTED_REPLY:
891 break;
892 case SESSION_CTRL_EVT_DISCONNECTED_REPLY:
893 session_mq_disconnected_reply_handler (e->data);
894 break;
895 case SESSION_CTRL_EVT_RESET_REPLY:
896 session_mq_reset_reply_handler (e->data);
897 break;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700898 default:
899 clib_warning ("unhandled event type %d", e->event_type);
900 }
901 }
902
903 _vec_len (fifo_events) = 0;
904 smm->free_event_vector[thread_index] = fifo_events;
905
906 vlib_node_increment_counter (vm, session_queue_node.index,
907 SESSION_QUEUE_ERROR_TX, n_tx_packets);
908
909 SESSION_EVT_DBG (SESSION_EVT_DISPATCH_END, smm, thread_index);
910
911 return n_tx_packets;
912}
913
914/* *INDENT-OFF* */
915VLIB_REGISTER_NODE (session_queue_node) =
916{
917 .function = session_queue_node_fn,
918 .name = "session-queue",
919 .format_trace = format_session_queue_trace,
920 .type = VLIB_NODE_TYPE_INPUT,
921 .n_errors = ARRAY_LEN (session_queue_error_strings),
922 .error_strings = session_queue_error_strings,
923 .state = VLIB_NODE_STATE_DISABLED,
924};
925/* *INDENT-ON* */
926
Dave Barachacd2a6a2017-05-16 17:41:34 -0400927void
928dump_thread_0_event_queue (void)
929{
930 session_manager_main_t *smm = vnet_get_session_manager_main ();
931 vlib_main_t *vm = &vlib_global_main;
932 u32 my_thread_index = vm->thread_index;
Florin Coras52207f12018-07-12 14:48:06 -0700933 session_event_t _e, *e = &_e;
Florin Coras3c2fed52018-07-04 04:15:05 -0700934 svm_msg_q_ring_t *ring;
Dave Barachacd2a6a2017-05-16 17:41:34 -0400935 stream_session_t *s0;
Florin Coras3c2fed52018-07-04 04:15:05 -0700936 svm_msg_q_msg_t *msg;
937 svm_msg_q_t *mq;
Dave Barachacd2a6a2017-05-16 17:41:34 -0400938 int i, index;
Dave Barachacd2a6a2017-05-16 17:41:34 -0400939
Florin Coras3c2fed52018-07-04 04:15:05 -0700940 mq = smm->vpp_event_queues[my_thread_index];
941 index = mq->q->head;
Dave Barachacd2a6a2017-05-16 17:41:34 -0400942
Florin Coras3c2fed52018-07-04 04:15:05 -0700943 for (i = 0; i < mq->q->cursize; i++)
Dave Barachacd2a6a2017-05-16 17:41:34 -0400944 {
Florin Coras3c2fed52018-07-04 04:15:05 -0700945 msg = (svm_msg_q_msg_t *) (&mq->q->data[0] + mq->q->elsize * index);
946 ring = svm_msg_q_ring (mq, msg->ring_index);
947 clib_memcpy (e, svm_msg_q_msg_data (mq, msg), ring->elsize);
Dave Barachacd2a6a2017-05-16 17:41:34 -0400948
949 switch (e->event_type)
950 {
951 case FIFO_EVENT_APP_TX:
952 s0 = session_event_get_session (e, my_thread_index);
953 fformat (stdout, "[%04d] TX session %d\n", i, s0->session_index);
954 break;
955
956 case FIFO_EVENT_DISCONNECT:
Florin Corascea194d2017-10-02 00:18:51 -0700957 s0 = session_get_from_handle (e->session_handle);
Dave Barachacd2a6a2017-05-16 17:41:34 -0400958 fformat (stdout, "[%04d] disconnect session %d\n", i,
959 s0->session_index);
960 break;
961
962 case FIFO_EVENT_BUILTIN_RX:
963 s0 = session_event_get_session (e, my_thread_index);
964 fformat (stdout, "[%04d] builtin_rx %d\n", i, s0->session_index);
965 break;
966
967 case FIFO_EVENT_RPC:
968 fformat (stdout, "[%04d] RPC call %llx with %llx\n",
969 i, (u64) (e->rpc_args.fp), (u64) (e->rpc_args.arg));
970 break;
971
972 default:
973 fformat (stdout, "[%04d] unhandled event type %d\n",
974 i, e->event_type);
975 break;
976 }
977
978 index++;
979
Florin Coras3c2fed52018-07-04 04:15:05 -0700980 if (index == mq->q->maxsize)
Dave Barachacd2a6a2017-05-16 17:41:34 -0400981 index = 0;
982 }
983}
984
Florin Coras6534b7a2017-07-18 05:38:03 -0400985static u8
Florin Coras52207f12018-07-12 14:48:06 -0700986session_node_cmp_event (session_event_t * e, svm_fifo_t * f)
Florin Coras6534b7a2017-07-18 05:38:03 -0400987{
988 stream_session_t *s;
989 switch (e->event_type)
990 {
991 case FIFO_EVENT_APP_RX:
992 case FIFO_EVENT_APP_TX:
993 case FIFO_EVENT_BUILTIN_RX:
994 if (e->fifo == f)
995 return 1;
996 break;
997 case FIFO_EVENT_DISCONNECT:
998 break;
999 case FIFO_EVENT_RPC:
Florin Corascea194d2017-10-02 00:18:51 -07001000 s = session_get_from_handle (e->session_handle);
Florin Coras6534b7a2017-07-18 05:38:03 -04001001 if (!s)
1002 {
1003 clib_warning ("session has event but doesn't exist!");
1004 break;
1005 }
1006 if (s->server_rx_fifo == f || s->server_tx_fifo == f)
1007 return 1;
1008 break;
1009 default:
1010 break;
1011 }
1012 return 0;
1013}
1014
1015u8
Florin Coras52207f12018-07-12 14:48:06 -07001016session_node_lookup_fifo_event (svm_fifo_t * f, session_event_t * e)
Florin Coras6534b7a2017-07-18 05:38:03 -04001017{
1018 session_manager_main_t *smm = vnet_get_session_manager_main ();
Florin Coras3c2fed52018-07-04 04:15:05 -07001019 svm_msg_q_t *mq;
Florin Coras52207f12018-07-12 14:48:06 -07001020 session_event_t *pending_event_vector, *evt;
Florin Coras6534b7a2017-07-18 05:38:03 -04001021 int i, index, found = 0;
Florin Coras3c2fed52018-07-04 04:15:05 -07001022 svm_msg_q_msg_t *msg;
1023 svm_msg_q_ring_t *ring;
Florin Coras6534b7a2017-07-18 05:38:03 -04001024 u8 thread_index;
1025
1026 ASSERT (e);
1027 thread_index = f->master_thread_index;
1028 /*
1029 * Search evt queue
1030 */
Florin Coras3c2fed52018-07-04 04:15:05 -07001031 mq = smm->vpp_event_queues[thread_index];
1032 index = mq->q->head;
1033 for (i = 0; i < mq->q->cursize; i++)
Florin Coras6534b7a2017-07-18 05:38:03 -04001034 {
Florin Coras3c2fed52018-07-04 04:15:05 -07001035 msg = (svm_msg_q_msg_t *) (&mq->q->data[0] + mq->q->elsize * index);
1036 ring = svm_msg_q_ring (mq, msg->ring_index);
1037 clib_memcpy (e, svm_msg_q_msg_data (mq, msg), ring->elsize);
Florin Coras6534b7a2017-07-18 05:38:03 -04001038 found = session_node_cmp_event (e, f);
1039 if (found)
Florin Coras371ca502018-02-21 12:07:41 -08001040 return 1;
Florin Coras3c2fed52018-07-04 04:15:05 -07001041 if (++index == mq->q->maxsize)
Florin Coras6534b7a2017-07-18 05:38:03 -04001042 index = 0;
1043 }
1044 /*
1045 * Search pending events vector
1046 */
1047 pending_event_vector = smm->pending_event_vector[thread_index];
1048 vec_foreach (evt, pending_event_vector)
1049 {
1050 found = session_node_cmp_event (evt, f);
1051 if (found)
1052 {
1053 clib_memcpy (e, evt, sizeof (*evt));
1054 break;
1055 }
1056 }
1057 return found;
1058}
1059
Dave Barach2a863912017-11-28 10:11:42 -05001060static clib_error_t *
1061session_queue_exit (vlib_main_t * vm)
1062{
1063 if (vec_len (vlib_mains) < 2)
1064 return 0;
1065
1066 /*
1067 * Shut off (especially) worker-thread session nodes.
1068 * Otherwise, vpp can crash as the main thread unmaps the
1069 * API segment.
1070 */
1071 vlib_worker_thread_barrier_sync (vm);
1072 session_node_enable_disable (0 /* is_enable */ );
1073 vlib_worker_thread_barrier_release (vm);
1074 return 0;
1075}
1076
1077VLIB_MAIN_LOOP_EXIT_FUNCTION (session_queue_exit);
1078
Florin Corasfd542f12018-05-16 19:28:24 -07001079static uword
1080session_queue_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
1081 vlib_frame_t * f)
1082{
1083 f64 now, timeout = 1.0;
1084 uword *event_data = 0;
1085 uword event_type;
1086
1087 while (1)
1088 {
1089 vlib_process_wait_for_event_or_clock (vm, timeout);
1090 now = vlib_time_now (vm);
1091 event_type = vlib_process_get_events (vm, (uword **) & event_data);
1092
1093 switch (event_type)
1094 {
1095 case SESSION_Q_PROCESS_FLUSH_FRAMES:
1096 /* Flush the frames by updating all transports times */
1097 transport_update_time (now, 0);
1098 break;
1099 case SESSION_Q_PROCESS_STOP:
1100 timeout = 100000.0;
1101 break;
1102 case ~0:
1103 /* Timed out. Update time for all transports to trigger all
1104 * outstanding retransmits. */
1105 transport_update_time (now, 0);
1106 break;
1107 }
1108 vec_reset_length (event_data);
1109 }
1110 return 0;
1111}
1112
1113/* *INDENT-OFF* */
1114VLIB_REGISTER_NODE (session_queue_process_node) =
1115{
1116 .function = session_queue_process,
1117 .type = VLIB_NODE_TYPE_PROCESS,
1118 .name = "session-queue-process",
1119 .state = VLIB_NODE_STATE_DISABLED,
1120};
1121/* *INDENT-ON* */
1122
1123
Dave Barach68b0fb02017-02-28 15:15:56 -05001124/*
1125 * fd.io coding-style-patch-verification: ON
1126 *
1127 * Local Variables:
1128 * eval: (c-set-style "gnu")
1129 * End:
1130 */