blob: eb9743971652295779aada359c5c103533ad8742 [file] [log] [blame]
Dave Barach68b0fb02017-02-28 15:15:56 -05001/*
2 * Copyright (c) 2017 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
Florin Coras6792ec02017-03-13 03:49:51 -070016#include <math.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050017#include <vlib/vlib.h>
18#include <vnet/vnet.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050019#include <vppinfra/elog.h>
Florin Coras561af9b2017-12-09 10:19:43 -080020#include <vnet/session/transport.h>
Florin Coras8e43d042018-05-04 15:46:57 -070021#include <vnet/session/session.h>
Florin Coras6792ec02017-03-13 03:49:51 -070022#include <vnet/session/application.h>
Florin Coras52207f12018-07-12 14:48:06 -070023#include <vnet/session/application_interface.h>
Florin Corase69f4952017-03-07 10:06:24 -080024#include <vnet/session/session_debug.h>
Florin Corase86a8ed2018-01-05 03:20:25 -080025#include <svm/queue.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050026
Florin Coras52207f12018-07-12 14:48:06 -070027static void
28session_mq_accepted_reply_handler (void *data)
29{
30 session_accepted_reply_msg_t *mp = (session_accepted_reply_msg_t *) data;
31 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
Florin Coras21795132018-09-09 09:40:51 -070032 app_worker_t *app_wrk;
Florin Coras52207f12018-07-12 14:48:06 -070033 local_session_t *ls;
34 stream_session_t *s;
35
36 /* Server isn't interested, kill the session */
37 if (mp->retval)
38 {
39 a->app_index = mp->context;
40 a->handle = mp->handle;
41 vnet_disconnect_session (a);
42 return;
43 }
44
45 if (session_handle_is_local (mp->handle))
46 {
47 ls = application_get_local_session_from_handle (mp->handle);
Florin Corasab2f6db2018-08-31 14:31:41 -070048 if (!ls)
Florin Coras52207f12018-07-12 14:48:06 -070049 {
Florin Corasab2f6db2018-08-31 14:31:41 -070050 clib_warning ("unknown local handle 0x%lx", mp->handle);
51 return;
52 }
53 app_wrk = app_worker_get (ls->app_wrk_index);
Florin Coras21795132018-09-09 09:40:51 -070054 if (app_wrk->app_index != mp->context)
Florin Corasab2f6db2018-08-31 14:31:41 -070055 {
56 clib_warning ("server %u doesn't own local handle 0x%lx",
Florin Coras52207f12018-07-12 14:48:06 -070057 mp->context, mp->handle);
58 return;
59 }
60 if (application_local_session_connect_notify (ls))
61 return;
62 ls->session_state = SESSION_STATE_READY;
63 }
64 else
65 {
66 s = session_get_from_handle_if_valid (mp->handle);
67 if (!s)
68 {
69 clib_warning ("session doesn't exist");
70 return;
71 }
Florin Coras21795132018-09-09 09:40:51 -070072 app_wrk = app_worker_get (s->app_wrk_index);
73 if (app_wrk->app_index != mp->context)
Florin Coras52207f12018-07-12 14:48:06 -070074 {
75 clib_warning ("app doesn't own session");
76 return;
77 }
78 s->session_state = SESSION_STATE_READY;
Florin Coras84099552018-07-22 12:59:30 -070079 if (!svm_fifo_is_empty (s->server_rx_fifo))
Florin Coras21795132018-09-09 09:40:51 -070080 app_worker_lock_and_send_event (app_wrk, s, FIFO_EVENT_APP_RX);
Florin Coras52207f12018-07-12 14:48:06 -070081 }
82}
83
84static void
85session_mq_reset_reply_handler (void *data)
86{
87 session_reset_reply_msg_t *mp;
Florin Coras15531972018-08-12 23:50:53 -070088 app_worker_t *app_wrk;
Florin Coras52207f12018-07-12 14:48:06 -070089 stream_session_t *s;
Florin Coras15531972018-08-12 23:50:53 -070090 application_t *app;
Florin Coras52207f12018-07-12 14:48:06 -070091 u32 index, thread_index;
92
93 mp = (session_reset_reply_msg_t *) data;
94 app = application_lookup (mp->client_index);
95 if (!app)
96 return;
97
98 session_parse_handle (mp->handle, &index, &thread_index);
99 s = session_get_if_valid (index, thread_index);
Florin Coras15531972018-08-12 23:50:53 -0700100 if (!s)
Florin Coras52207f12018-07-12 14:48:06 -0700101 {
102 clib_warning ("Invalid session!");
103 return;
104 }
Florin Coras15531972018-08-12 23:50:53 -0700105 app_wrk = app_worker_get (s->app_wrk_index);
106 if (!app_wrk || app_wrk->app_index != app->app_index)
107 {
108 clib_warning ("App % does not own handle 0x%lx!", app->app_index,
109 mp->handle);
110 return;
111 }
Florin Coras52207f12018-07-12 14:48:06 -0700112
113 /* Client objected to resetting the session, log and continue */
114 if (mp->retval)
115 {
116 clib_warning ("client retval %d", mp->retval);
117 return;
118 }
119
120 /* This comes as a response to a reset, transport only waiting for
121 * confirmation to remove connection state, no need to disconnect */
122 stream_session_cleanup (s);
123}
124
125static void
126session_mq_disconnected_handler (void *data)
127{
128 session_disconnected_reply_msg_t *rmp;
129 vnet_disconnect_args_t _a, *a = &_a;
130 svm_msg_q_msg_t _msg, *msg = &_msg;
131 session_disconnected_msg_t *mp;
Florin Coras15531972018-08-12 23:50:53 -0700132 app_worker_t *app_wrk;
Florin Coras52207f12018-07-12 14:48:06 -0700133 session_event_t *evt;
Florin Coras3d0fadc2018-07-17 05:35:47 -0700134 stream_session_t *s;
Florin Coras52207f12018-07-12 14:48:06 -0700135 application_t *app;
136 int rv = 0;
137
138 mp = (session_disconnected_msg_t *) data;
Florin Corasc3638fe2018-08-24 13:58:49 -0700139 if (!(s = session_get_from_handle_if_valid (mp->handle)))
140 {
141 clib_warning ("could not disconnect handle %llu", mp->handle);
142 return;
143 }
Florin Coras15531972018-08-12 23:50:53 -0700144 app_wrk = app_worker_get (s->app_wrk_index);
145 app = application_lookup (mp->client_index);
Florin Corasc3638fe2018-08-24 13:58:49 -0700146 if (!(app_wrk && app && app->app_index == app_wrk->app_index))
Florin Coras52207f12018-07-12 14:48:06 -0700147 {
Florin Corasc3638fe2018-08-24 13:58:49 -0700148 clib_warning ("could not disconnect session: %llu app: %u",
Florin Coras15531972018-08-12 23:50:53 -0700149 mp->handle, mp->client_index);
Florin Coras3d0fadc2018-07-17 05:35:47 -0700150 return;
Florin Coras52207f12018-07-12 14:48:06 -0700151 }
Florin Coras3d0fadc2018-07-17 05:35:47 -0700152
153 a->handle = mp->handle;
Florin Coras15531972018-08-12 23:50:53 -0700154 a->app_index = app_wrk->wrk_index;
Florin Coras3d0fadc2018-07-17 05:35:47 -0700155 rv = vnet_disconnect_session (a);
Florin Coras52207f12018-07-12 14:48:06 -0700156
Florin Coras15531972018-08-12 23:50:53 -0700157 svm_msg_q_lock_and_alloc_msg_w_ring (app_wrk->event_queue,
Florin Coras52207f12018-07-12 14:48:06 -0700158 SESSION_MQ_CTRL_EVT_RING,
159 SVM_Q_WAIT, msg);
Florin Coras15531972018-08-12 23:50:53 -0700160 svm_msg_q_unlock (app_wrk->event_queue);
161 evt = svm_msg_q_msg_data (app_wrk->event_queue, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400162 clib_memset (evt, 0, sizeof (*evt));
Florin Coras52207f12018-07-12 14:48:06 -0700163 evt->event_type = SESSION_CTRL_EVT_DISCONNECTED;
164 rmp = (session_disconnected_reply_msg_t *) evt->data;
165 rmp->handle = mp->handle;
166 rmp->context = mp->context;
167 rmp->retval = rv;
Florin Coras15531972018-08-12 23:50:53 -0700168 svm_msg_q_add (app_wrk->event_queue, msg, SVM_Q_WAIT);
Florin Coras52207f12018-07-12 14:48:06 -0700169}
170
171static void
172session_mq_disconnected_reply_handler (void *data)
173{
174 session_disconnected_reply_msg_t *mp;
175 vnet_disconnect_args_t _a, *a = &_a;
176 application_t *app;
177
178 mp = (session_disconnected_reply_msg_t *) data;
179
180 /* Client objected to disconnecting the session, log and continue */
181 if (mp->retval)
182 {
183 clib_warning ("client retval %d", mp->retval);
184 return;
185 }
186
187 /* Disconnect has been confirmed. Confirm close to transport */
188 app = application_lookup (mp->context);
189 if (app)
190 {
191 a->handle = mp->handle;
Florin Coras15531972018-08-12 23:50:53 -0700192 a->app_index = app->app_index;
Florin Coras52207f12018-07-12 14:48:06 -0700193 vnet_disconnect_session (a);
194 }
195}
196
Dave Barach68b0fb02017-02-28 15:15:56 -0500197vlib_node_registration_t session_queue_node;
198
199typedef struct
200{
201 u32 session_index;
202 u32 server_thread_index;
203} session_queue_trace_t;
204
205/* packet trace format function */
206static u8 *
207format_session_queue_trace (u8 * s, va_list * args)
208{
209 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
210 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
211 session_queue_trace_t *t = va_arg (*args, session_queue_trace_t *);
212
213 s = format (s, "SESSION_QUEUE: session index %d, server thread index %d",
214 t->session_index, t->server_thread_index);
215 return s;
216}
217
Florin Coras6792ec02017-03-13 03:49:51 -0700218#define foreach_session_queue_error \
219_(TX, "Packets transmitted") \
Florin Coras93992a92017-05-24 18:03:56 -0700220_(TIMER, "Timer events") \
221_(NO_BUFFER, "Out of buffers")
Dave Barach68b0fb02017-02-28 15:15:56 -0500222
223typedef enum
224{
225#define _(sym,str) SESSION_QUEUE_ERROR_##sym,
226 foreach_session_queue_error
227#undef _
228 SESSION_QUEUE_N_ERROR,
229} session_queue_error_t;
230
231static char *session_queue_error_strings[] = {
232#define _(sym,string) string,
233 foreach_session_queue_error
234#undef _
235};
236
Florin Coras0d60a0f2018-06-29 02:02:08 -0700237enum
238{
239 SESSION_TX_NO_BUFFERS = -2,
240 SESSION_TX_NO_DATA,
241 SESSION_TX_OK
242};
243
Florin Coras66cf5e02018-06-08 09:17:39 -0700244static void
245session_tx_trace_frame (vlib_main_t * vm, vlib_node_runtime_t * node,
246 u32 next_index, u32 * to_next, u16 n_segs,
247 stream_session_t * s, u32 n_trace)
Florin Corasf6d68ed2017-05-07 19:12:02 -0700248{
Florin Coras8e43d042018-05-04 15:46:57 -0700249 session_queue_trace_t *t;
Florin Coras66cf5e02018-06-08 09:17:39 -0700250 vlib_buffer_t *b;
251 int i;
252
253 for (i = 0; i < clib_min (n_trace, n_segs); i++)
254 {
255 b = vlib_get_buffer (vm, to_next[i - n_segs]);
256 vlib_trace_buffer (vm, node, next_index, b, 1 /* follow_chain */ );
Florin Coras66cf5e02018-06-08 09:17:39 -0700257 t = vlib_add_trace (vm, node, b, sizeof (*t));
258 t->session_index = s->session_index;
259 t->server_thread_index = s->thread_index;
260 }
Florin Coras4df38712018-06-20 12:44:16 -0700261 vlib_set_trace_count (vm, node, n_trace - i);
Florin Coras8e43d042018-05-04 15:46:57 -0700262}
Florin Corasf6d68ed2017-05-07 19:12:02 -0700263
Florin Coras8e43d042018-05-04 15:46:57 -0700264always_inline void
265session_tx_fifo_chain_tail (vlib_main_t * vm, session_tx_context_t * ctx,
266 vlib_buffer_t * b, u16 * n_bufs, u8 peek_data)
267{
268 session_manager_main_t *smm = &session_manager_main;
269 vlib_buffer_t *chain_b, *prev_b;
270 u32 chain_bi0, to_deq, left_from_seg;
271 u16 len_to_deq, n_bytes_read;
272 u8 *data, j;
Florin Corasb2215d62017-08-01 16:56:58 -0700273
Florin Coras8e43d042018-05-04 15:46:57 -0700274 b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
275 b->total_length_not_including_first_buffer = 0;
276
277 chain_b = b;
278 left_from_seg = clib_min (ctx->snd_mss - b->current_length,
279 ctx->left_to_snd);
Florin Corasb2215d62017-08-01 16:56:58 -0700280 to_deq = left_from_seg;
Florin Coras8e43d042018-05-04 15:46:57 -0700281 for (j = 1; j < ctx->n_bufs_per_seg; j++)
Florin Corasf6d68ed2017-05-07 19:12:02 -0700282 {
Florin Coras8e43d042018-05-04 15:46:57 -0700283 prev_b = chain_b;
284 len_to_deq = clib_min (to_deq, ctx->deq_per_buf);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700285
286 *n_bufs -= 1;
Florin Coras8e43d042018-05-04 15:46:57 -0700287 chain_bi0 = smm->tx_buffers[ctx->s->thread_index][*n_bufs];
288 _vec_len (smm->tx_buffers[ctx->s->thread_index]) = *n_bufs;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700289
Florin Coras8e43d042018-05-04 15:46:57 -0700290 chain_b = vlib_get_buffer (vm, chain_bi0);
291 chain_b->current_data = 0;
292 data = vlib_buffer_get_current (chain_b);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700293 if (peek_data)
294 {
Florin Coras8e43d042018-05-04 15:46:57 -0700295 n_bytes_read = svm_fifo_peek (ctx->s->server_tx_fifo,
296 ctx->tx_offset, len_to_deq, data);
297 ctx->tx_offset += n_bytes_read;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700298 }
299 else
300 {
Florin Coras8e43d042018-05-04 15:46:57 -0700301 if (ctx->transport_vft->tx_type == TRANSPORT_TX_DGRAM)
Florin Coras7fb0fe12018-04-09 09:24:52 -0700302 {
Florin Coras8e43d042018-05-04 15:46:57 -0700303 svm_fifo_t *f = ctx->s->server_tx_fifo;
304 session_dgram_hdr_t *hdr = &ctx->hdr;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700305 u16 deq_now;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700306 deq_now = clib_min (hdr->data_length - hdr->data_offset,
Florin Coras8e43d042018-05-04 15:46:57 -0700307 len_to_deq);
308 n_bytes_read = svm_fifo_peek (f, hdr->data_offset, deq_now,
309 data);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700310 ASSERT (n_bytes_read > 0);
311
312 hdr->data_offset += n_bytes_read;
313 if (hdr->data_offset == hdr->data_length)
shubing guoaea5f392018-08-30 13:29:49 +0800314 {
315 u32 offset = hdr->data_length + SESSION_CONN_HDR_LEN;
316 svm_fifo_dequeue_drop (f, offset);
317 }
Florin Coras7fb0fe12018-04-09 09:24:52 -0700318 }
319 else
Florin Coras8e43d042018-05-04 15:46:57 -0700320 n_bytes_read = svm_fifo_dequeue_nowait (ctx->s->server_tx_fifo,
321 len_to_deq, data);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700322 }
Florin Coras8e43d042018-05-04 15:46:57 -0700323 ASSERT (n_bytes_read == len_to_deq);
324 chain_b->current_length = n_bytes_read;
325 b->total_length_not_including_first_buffer += chain_b->current_length;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700326
327 /* update previous buffer */
Florin Coras8e43d042018-05-04 15:46:57 -0700328 prev_b->next_buffer = chain_bi0;
329 prev_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700330
331 /* update current buffer */
Florin Coras8e43d042018-05-04 15:46:57 -0700332 chain_b->next_buffer = 0;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700333
Florin Corasb2215d62017-08-01 16:56:58 -0700334 to_deq -= n_bytes_read;
335 if (to_deq == 0)
Florin Corasf6d68ed2017-05-07 19:12:02 -0700336 break;
337 }
Florin Coras1f152cd2017-08-18 19:28:03 -0700338 ASSERT (to_deq == 0
Florin Coras8e43d042018-05-04 15:46:57 -0700339 && b->total_length_not_including_first_buffer == left_from_seg);
340 ctx->left_to_snd -= left_from_seg;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700341}
342
Dave Barach68b0fb02017-02-28 15:15:56 -0500343always_inline int
Florin Coras8e43d042018-05-04 15:46:57 -0700344session_output_try_get_buffers (vlib_main_t * vm,
Florin Corasd79b41e2017-03-04 05:37:52 -0800345 session_manager_main_t * smm,
Florin Coras8e43d042018-05-04 15:46:57 -0700346 u32 thread_index, u16 * n_bufs, u32 wanted)
Dave Barach68b0fb02017-02-28 15:15:56 -0500347{
Florin Corasf08f26d2018-05-10 13:20:47 -0700348 u32 n_alloc;
349 vec_validate_aligned (smm->tx_buffers[thread_index], wanted - 1,
Florin Coras8e43d042018-05-04 15:46:57 -0700350 CLIB_CACHE_LINE_BYTES);
Florin Corasf08f26d2018-05-10 13:20:47 -0700351 n_alloc = vlib_buffer_alloc (vm, &smm->tx_buffers[thread_index][*n_bufs],
352 wanted - *n_bufs);
353 *n_bufs += n_alloc;
Florin Coras8e43d042018-05-04 15:46:57 -0700354 _vec_len (smm->tx_buffers[thread_index]) = *n_bufs;
Florin Corasf08f26d2018-05-10 13:20:47 -0700355 return n_alloc;
Florin Coras8e43d042018-05-04 15:46:57 -0700356}
357
358always_inline void
359session_tx_fill_buffer (vlib_main_t * vm, session_tx_context_t * ctx,
360 vlib_buffer_t * b, u16 * n_bufs, u8 peek_data)
361{
362 u32 len_to_deq;
363 u8 *data0;
364 int n_bytes_read;
365
366 /*
367 * Start with the first buffer in chain
368 */
369 b->error = 0;
370 b->flags = VNET_BUFFER_F_LOCALLY_ORIGINATED;
371 b->current_data = 0;
Florin Coras8e43d042018-05-04 15:46:57 -0700372
373 data0 = vlib_buffer_make_headroom (b, MAX_HDRS_LEN);
374 len_to_deq = clib_min (ctx->left_to_snd, ctx->deq_per_first_buf);
375
Florin Coras7fb0fe12018-04-09 09:24:52 -0700376 if (peek_data)
377 {
Florin Coras8e43d042018-05-04 15:46:57 -0700378 n_bytes_read = svm_fifo_peek (ctx->s->server_tx_fifo, ctx->tx_offset,
379 len_to_deq, data0);
380 ASSERT (n_bytes_read > 0);
381 /* Keep track of progress locally, transport is also supposed to
382 * increment it independently when pushing the header */
383 ctx->tx_offset += n_bytes_read;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700384 }
385 else
386 {
Florin Coras8e43d042018-05-04 15:46:57 -0700387 if (ctx->transport_vft->tx_type == TRANSPORT_TX_DGRAM)
388 {
389 session_dgram_hdr_t *hdr = &ctx->hdr;
390 svm_fifo_t *f = ctx->s->server_tx_fifo;
391 u16 deq_now;
392 u32 offset;
393
394 ASSERT (hdr->data_length > hdr->data_offset);
395 deq_now = clib_min (hdr->data_length - hdr->data_offset,
396 len_to_deq);
397 offset = hdr->data_offset + SESSION_CONN_HDR_LEN;
398 n_bytes_read = svm_fifo_peek (f, offset, deq_now, data0);
399 ASSERT (n_bytes_read > 0);
400
401 if (ctx->s->session_state == SESSION_STATE_LISTENING)
402 {
403 ip_copy (&ctx->tc->rmt_ip, &hdr->rmt_ip, ctx->tc->is_ip4);
404 ctx->tc->rmt_port = hdr->rmt_port;
405 }
406 hdr->data_offset += n_bytes_read;
407 if (hdr->data_offset == hdr->data_length)
408 {
409 offset = hdr->data_length + SESSION_CONN_HDR_LEN;
410 svm_fifo_dequeue_drop (f, offset);
411 }
412 }
Florin Coras7fb0fe12018-04-09 09:24:52 -0700413 else
414 {
Florin Coras8e43d042018-05-04 15:46:57 -0700415 n_bytes_read = svm_fifo_dequeue_nowait (ctx->s->server_tx_fifo,
416 len_to_deq, data0);
417 ASSERT (n_bytes_read > 0);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700418 }
419 }
Florin Coras8e43d042018-05-04 15:46:57 -0700420 b->current_length = n_bytes_read;
421 ctx->left_to_snd -= n_bytes_read;
Dave Barach68b0fb02017-02-28 15:15:56 -0500422
Florin Coras8e43d042018-05-04 15:46:57 -0700423 /*
424 * Fill in the remaining buffers in the chain, if any
425 */
426 if (PREDICT_FALSE (ctx->n_bufs_per_seg > 1 && ctx->left_to_snd))
427 session_tx_fifo_chain_tail (vm, ctx, b, n_bufs, peek_data);
Dave Barach68b0fb02017-02-28 15:15:56 -0500428
Florin Coras8e43d042018-05-04 15:46:57 -0700429 /* *INDENT-OFF* */
Florin Coras8b20bf52018-06-14 14:55:50 -0700430 SESSION_EVT_DBG(SESSION_EVT_DEQ, ctx->s, ({
431 ed->data[0] = FIFO_EVENT_APP_TX;
432 ed->data[1] = ctx->max_dequeue;
Florin Coras8e43d042018-05-04 15:46:57 -0700433 ed->data[2] = len_to_deq;
Florin Coras8b20bf52018-06-14 14:55:50 -0700434 ed->data[3] = ctx->left_to_snd;
Florin Coras8e43d042018-05-04 15:46:57 -0700435 }));
436 /* *INDENT-ON* */
437}
438
439always_inline u8
440session_tx_not_ready (stream_session_t * s, u8 peek_data)
441{
442 if (peek_data)
Florin Corase04c2992017-03-01 08:17:34 -0800443 {
Florin Coras8e43d042018-05-04 15:46:57 -0700444 /* Can retransmit for closed sessions but can't send new data if
445 * session is not ready or closed */
446 if (s->session_state < SESSION_STATE_READY)
447 return 1;
Florin Corasca1c8f32018-05-23 21:01:30 -0700448 if (s->session_state == SESSION_STATE_CLOSED)
449 return 2;
Florin Corase04c2992017-03-01 08:17:34 -0800450 }
Florin Coras8e43d042018-05-04 15:46:57 -0700451 return 0;
452}
Dave Barach68b0fb02017-02-28 15:15:56 -0500453
Florin Coras8e43d042018-05-04 15:46:57 -0700454always_inline transport_connection_t *
455session_tx_get_transport (session_tx_context_t * ctx, u8 peek_data)
456{
457 if (peek_data)
458 {
459 return ctx->transport_vft->get_connection (ctx->s->connection_index,
460 ctx->s->thread_index);
461 }
462 else
463 {
464 if (ctx->s->session_state == SESSION_STATE_LISTENING)
465 return ctx->transport_vft->get_listener (ctx->s->connection_index);
466 else
467 {
468 return ctx->transport_vft->get_connection (ctx->s->connection_index,
469 ctx->s->thread_index);
470 }
471 }
472}
Florin Coras6a9b68b2017-11-21 04:20:42 -0800473
Florin Coras8e43d042018-05-04 15:46:57 -0700474always_inline void
475session_tx_set_dequeue_params (vlib_main_t * vm, session_tx_context_t * ctx,
Florin Corasf08f26d2018-05-10 13:20:47 -0700476 u32 max_segs, u8 peek_data)
Florin Coras8e43d042018-05-04 15:46:57 -0700477{
478 u32 n_bytes_per_buf, n_bytes_per_seg;
479 ctx->max_dequeue = svm_fifo_max_dequeue (ctx->s->server_tx_fifo);
Dave Barach68b0fb02017-02-28 15:15:56 -0500480 if (peek_data)
481 {
Florin Coras9d063042017-09-14 03:08:00 -0400482 /* Offset in rx fifo from where to peek data */
Florin Coras8e43d042018-05-04 15:46:57 -0700483 ctx->tx_offset = ctx->transport_vft->tx_fifo_offset (ctx->tc);
484 if (PREDICT_FALSE (ctx->tx_offset >= ctx->max_dequeue))
485 {
486 ctx->max_len_to_snd = 0;
487 return;
488 }
489 ctx->max_dequeue -= ctx->tx_offset;
Dave Barach68b0fb02017-02-28 15:15:56 -0500490 }
Florin Coras7fb0fe12018-04-09 09:24:52 -0700491 else
492 {
Florin Coras8e43d042018-05-04 15:46:57 -0700493 if (ctx->transport_vft->tx_type == TRANSPORT_TX_DGRAM)
Florin Coras7fb0fe12018-04-09 09:24:52 -0700494 {
Florin Coras8e43d042018-05-04 15:46:57 -0700495 if (ctx->max_dequeue <= sizeof (ctx->hdr))
496 {
497 ctx->max_len_to_snd = 0;
498 return;
499 }
500 svm_fifo_peek (ctx->s->server_tx_fifo, 0, sizeof (ctx->hdr),
501 (u8 *) & ctx->hdr);
502 ASSERT (ctx->hdr.data_length > ctx->hdr.data_offset);
503 ctx->max_dequeue = ctx->hdr.data_length - ctx->hdr.data_offset;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700504 }
505 }
Florin Coras8e43d042018-05-04 15:46:57 -0700506 ASSERT (ctx->max_dequeue > 0);
Florin Coras6792ec02017-03-13 03:49:51 -0700507
508 /* Ensure we're not writing more than transport window allows */
Florin Coras8e43d042018-05-04 15:46:57 -0700509 if (ctx->max_dequeue < ctx->snd_space)
Florin Coras3e350af2017-03-30 02:54:28 -0700510 {
511 /* Constrained by tx queue. Try to send only fully formed segments */
Florin Coras8e43d042018-05-04 15:46:57 -0700512 ctx->max_len_to_snd =
513 (ctx->max_dequeue > ctx->snd_mss) ?
514 ctx->max_dequeue - ctx->max_dequeue % ctx->snd_mss : ctx->max_dequeue;
Florin Coras3e350af2017-03-30 02:54:28 -0700515 /* TODO Nagle ? */
516 }
517 else
518 {
Florin Coras1f152cd2017-08-18 19:28:03 -0700519 /* Expectation is that snd_space0 is already a multiple of snd_mss */
Florin Coras8e43d042018-05-04 15:46:57 -0700520 ctx->max_len_to_snd = ctx->snd_space;
Florin Coras3e350af2017-03-30 02:54:28 -0700521 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500522
Florin Corasf08f26d2018-05-10 13:20:47 -0700523 /* Check if we're tx constrained by the node */
524 ctx->n_segs_per_evt = ceil ((f64) ctx->max_len_to_snd / ctx->snd_mss);
525 if (ctx->n_segs_per_evt > max_segs)
526 {
527 ctx->n_segs_per_evt = max_segs;
528 ctx->max_len_to_snd = max_segs * ctx->snd_mss;
529 }
530
Florin Coras8e43d042018-05-04 15:46:57 -0700531 n_bytes_per_buf = vlib_buffer_free_list_buffer_size (vm,
532 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
Florin Corase87216f2017-08-17 16:59:22 -0700533 ASSERT (n_bytes_per_buf > MAX_HDRS_LEN);
Florin Coras8e43d042018-05-04 15:46:57 -0700534 n_bytes_per_seg = MAX_HDRS_LEN + ctx->snd_mss;
Florin Corasf08f26d2018-05-10 13:20:47 -0700535 ctx->n_bufs_per_seg = ceil ((f64) n_bytes_per_seg / n_bytes_per_buf);
Florin Coras8e43d042018-05-04 15:46:57 -0700536 ctx->deq_per_buf = clib_min (ctx->snd_mss, n_bytes_per_buf);
537 ctx->deq_per_first_buf = clib_min (ctx->snd_mss,
538 n_bytes_per_buf - MAX_HDRS_LEN);
539}
Florin Corasf6d68ed2017-05-07 19:12:02 -0700540
Florin Coras8e43d042018-05-04 15:46:57 -0700541always_inline int
542session_tx_fifo_read_and_snd_i (vlib_main_t * vm, vlib_node_runtime_t * node,
Florin Coras52207f12018-07-12 14:48:06 -0700543 session_event_t * e,
Florin Coras8e43d042018-05-04 15:46:57 -0700544 stream_session_t * s, int *n_tx_packets,
545 u8 peek_data)
546{
Florin Coras66cf5e02018-06-08 09:17:39 -0700547 u32 next_index, next0, next1, *to_next, n_left_to_next;
Florin Corasf08f26d2018-05-10 13:20:47 -0700548 u32 n_trace = vlib_get_trace_count (vm, node), n_bufs_needed = 0;
549 u32 thread_index = s->thread_index, n_left, pbi;
Florin Coras8e43d042018-05-04 15:46:57 -0700550 session_manager_main_t *smm = &session_manager_main;
551 session_tx_context_t *ctx = &smm->ctx[thread_index];
552 transport_proto_t tp;
553 vlib_buffer_t *pb;
Florin Corasca1c8f32018-05-23 21:01:30 -0700554 u16 n_bufs, rv;
Florin Coras8e43d042018-05-04 15:46:57 -0700555
Florin Corasca1c8f32018-05-23 21:01:30 -0700556 if (PREDICT_FALSE ((rv = session_tx_not_ready (s, peek_data))))
Florin Coras8e43d042018-05-04 15:46:57 -0700557 {
Florin Corasca1c8f32018-05-23 21:01:30 -0700558 if (rv < 2)
559 vec_add1 (smm->pending_event_vector[thread_index], *e);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700560 return SESSION_TX_NO_DATA;
Florin Coras8e43d042018-05-04 15:46:57 -0700561 }
562
563 next_index = smm->session_type_to_next[s->session_type];
Florin Coras66cf5e02018-06-08 09:17:39 -0700564 next0 = next1 = next_index;
Florin Coras8e43d042018-05-04 15:46:57 -0700565
566 tp = session_get_transport_proto (s);
567 ctx->s = s;
568 ctx->transport_vft = transport_protocol_get_vft (tp);
569 ctx->tc = session_tx_get_transport (ctx, peek_data);
570 ctx->snd_mss = ctx->transport_vft->send_mss (ctx->tc);
Florin Corasd67f1122018-05-21 17:47:40 -0700571 ctx->snd_space =
572 transport_connection_max_tx_burst (ctx->tc, vm->clib_time.last_cpu_time);
Florin Coras8e43d042018-05-04 15:46:57 -0700573 if (ctx->snd_space == 0 || ctx->snd_mss == 0)
574 {
575 vec_add1 (smm->pending_event_vector[thread_index], *e);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700576 return SESSION_TX_NO_DATA;
Florin Coras8e43d042018-05-04 15:46:57 -0700577 }
578
579 /* Allow enqueuing of a new event */
580 svm_fifo_unset_event (s->server_tx_fifo);
581
582 /* Check how much we can pull. */
Florin Corasf08f26d2018-05-10 13:20:47 -0700583 session_tx_set_dequeue_params (vm, ctx, VLIB_FRAME_SIZE - *n_tx_packets,
584 peek_data);
585
Florin Coras8e43d042018-05-04 15:46:57 -0700586 if (PREDICT_FALSE (!ctx->max_len_to_snd))
Florin Coras0d60a0f2018-06-29 02:02:08 -0700587 return SESSION_TX_NO_DATA;
Dave Barach68b0fb02017-02-28 15:15:56 -0500588
589 n_bufs = vec_len (smm->tx_buffers[thread_index]);
Florin Corasf08f26d2018-05-10 13:20:47 -0700590 n_bufs_needed = ctx->n_segs_per_evt * ctx->n_bufs_per_seg;
Florin Coras8e43d042018-05-04 15:46:57 -0700591
592 /*
593 * Make sure we have at least one full frame of buffers ready
594 */
Florin Corasf08f26d2018-05-10 13:20:47 -0700595 if (n_bufs < n_bufs_needed)
Dave Barach68b0fb02017-02-28 15:15:56 -0500596 {
Florin Coras8e43d042018-05-04 15:46:57 -0700597 session_output_try_get_buffers (vm, smm, thread_index, &n_bufs,
Florin Corasf08f26d2018-05-10 13:20:47 -0700598 ctx->n_bufs_per_seg * VLIB_FRAME_SIZE);
599 if (PREDICT_FALSE (n_bufs < n_bufs_needed))
Dave Barach68b0fb02017-02-28 15:15:56 -0500600 {
Florin Coras8e43d042018-05-04 15:46:57 -0700601 vec_add1 (smm->pending_event_vector[thread_index], *e);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700602 return SESSION_TX_NO_BUFFERS;
Dave Barach68b0fb02017-02-28 15:15:56 -0500603 }
Florin Coras8e43d042018-05-04 15:46:57 -0700604 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500605
Florin Coras8e43d042018-05-04 15:46:57 -0700606 /*
607 * Write until we fill up a frame
608 */
609 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Florin Corasf08f26d2018-05-10 13:20:47 -0700610 if (PREDICT_FALSE (ctx->n_segs_per_evt > n_left_to_next))
Florin Coras8e43d042018-05-04 15:46:57 -0700611 {
Florin Corasf08f26d2018-05-10 13:20:47 -0700612 ctx->n_segs_per_evt = n_left_to_next;
613 ctx->max_len_to_snd = ctx->snd_mss * n_left_to_next;
614 }
615 ctx->left_to_snd = ctx->max_len_to_snd;
616 n_left = ctx->n_segs_per_evt;
Florin Coras66cf5e02018-06-08 09:17:39 -0700617
618 while (n_left >= 4)
619 {
620 vlib_buffer_t *b0, *b1;
621 u32 bi0, bi1;
622
623 pbi = smm->tx_buffers[thread_index][n_bufs - 3];
624 pb = vlib_get_buffer (vm, pbi);
625 vlib_prefetch_buffer_header (pb, STORE);
626 pbi = smm->tx_buffers[thread_index][n_bufs - 4];
627 pb = vlib_get_buffer (vm, pbi);
628 vlib_prefetch_buffer_header (pb, STORE);
629
630 to_next[0] = bi0 = smm->tx_buffers[thread_index][--n_bufs];
631 to_next[1] = bi1 = smm->tx_buffers[thread_index][--n_bufs];
632
633 b0 = vlib_get_buffer (vm, bi0);
634 b1 = vlib_get_buffer (vm, bi1);
635
636 session_tx_fill_buffer (vm, ctx, b0, &n_bufs, peek_data);
637 session_tx_fill_buffer (vm, ctx, b1, &n_bufs, peek_data);
638
639 ctx->transport_vft->push_header (ctx->tc, b0);
640 ctx->transport_vft->push_header (ctx->tc, b1);
641
642 to_next += 2;
643 n_left_to_next -= 2;
644 n_left -= 2;
645
646 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
647 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b1);
648
649 vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
650 n_left_to_next, bi0, bi1, next0,
651 next1);
652 }
Florin Corasf08f26d2018-05-10 13:20:47 -0700653 while (n_left)
654 {
Florin Coras66cf5e02018-06-08 09:17:39 -0700655 vlib_buffer_t *b0;
656 u32 bi0;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700657
Florin Coras91ca4622018-06-30 11:27:59 -0700658 if (n_left > 1)
659 {
660 pbi = smm->tx_buffers[thread_index][n_bufs - 2];
661 pb = vlib_get_buffer (vm, pbi);
662 vlib_prefetch_buffer_header (pb, STORE);
663 }
664
Florin Coras66cf5e02018-06-08 09:17:39 -0700665 to_next[0] = bi0 = smm->tx_buffers[thread_index][--n_bufs];
666 b0 = vlib_get_buffer (vm, bi0);
667 session_tx_fill_buffer (vm, ctx, b0, &n_bufs, peek_data);
Florin Coras81a13db2018-03-16 08:48:31 -0700668
Florin Coras66cf5e02018-06-08 09:17:39 -0700669 /* Ask transport to push header after current_length and
670 * total_length_not_including_first_buffer are updated */
671 ctx->transport_vft->push_header (ctx->tc, b0);
Florin Corasf6359c82017-06-19 12:26:09 -0400672
Florin Coras66cf5e02018-06-08 09:17:39 -0700673 to_next += 1;
674 n_left_to_next -= 1;
675 n_left -= 1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500676
Florin Coras66cf5e02018-06-08 09:17:39 -0700677 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700678
Florin Coras66cf5e02018-06-08 09:17:39 -0700679 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
680 n_left_to_next, bi0, next0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500681 }
Florin Coras66cf5e02018-06-08 09:17:39 -0700682
683 if (PREDICT_FALSE (n_trace > 0))
684 session_tx_trace_frame (vm, node, next_index, to_next,
685 ctx->n_segs_per_evt, s, n_trace);
686
Florin Coras8e43d042018-05-04 15:46:57 -0700687 _vec_len (smm->tx_buffers[thread_index]) = n_bufs;
Florin Corasf08f26d2018-05-10 13:20:47 -0700688 *n_tx_packets += ctx->n_segs_per_evt;
Florin Corasd67f1122018-05-21 17:47:40 -0700689 transport_connection_update_tx_stats (ctx->tc, ctx->max_len_to_snd);
Florin Coras8e43d042018-05-04 15:46:57 -0700690 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
Dave Barach68b0fb02017-02-28 15:15:56 -0500691
Florin Coras6792ec02017-03-13 03:49:51 -0700692 /* If we couldn't dequeue all bytes mark as partially read */
Florin Corasf08f26d2018-05-10 13:20:47 -0700693 ASSERT (ctx->left_to_snd == 0);
Florin Coras8e43d042018-05-04 15:46:57 -0700694 if (ctx->max_len_to_snd < ctx->max_dequeue)
695 if (svm_fifo_set_event (s->server_tx_fifo))
696 vec_add1 (smm->pending_event_vector[thread_index], *e);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700697
Florin Coras8e43d042018-05-04 15:46:57 -0700698 if (!peek_data && ctx->transport_vft->tx_type == TRANSPORT_TX_DGRAM)
Dave Barach68b0fb02017-02-28 15:15:56 -0500699 {
Florin Coras7fb0fe12018-04-09 09:24:52 -0700700 /* Fix dgram pre header */
Florin Coras8e43d042018-05-04 15:46:57 -0700701 if (ctx->max_len_to_snd < ctx->max_dequeue)
702 svm_fifo_overwrite_head (s->server_tx_fifo, (u8 *) & ctx->hdr,
Florin Coras7fb0fe12018-04-09 09:24:52 -0700703 sizeof (session_dgram_pre_hdr_t));
704 /* More data needs to be read */
Florin Coras8e43d042018-05-04 15:46:57 -0700705 else if (svm_fifo_max_dequeue (s->server_tx_fifo) > 0)
706 if (svm_fifo_set_event (s->server_tx_fifo))
707 vec_add1 (smm->pending_event_vector[thread_index], *e);
Dave Barach68b0fb02017-02-28 15:15:56 -0500708 }
Florin Coras0d60a0f2018-06-29 02:02:08 -0700709 return SESSION_TX_OK;
Dave Barach68b0fb02017-02-28 15:15:56 -0500710}
711
712int
Florin Corasd79b41e2017-03-04 05:37:52 -0800713session_tx_fifo_peek_and_snd (vlib_main_t * vm, vlib_node_runtime_t * node,
Florin Coras52207f12018-07-12 14:48:06 -0700714 session_event_t * e,
Florin Coras0d60a0f2018-06-29 02:02:08 -0700715 stream_session_t * s, int *n_tx_pkts)
Dave Barach68b0fb02017-02-28 15:15:56 -0500716{
Florin Coras0d60a0f2018-06-29 02:02:08 -0700717 return session_tx_fifo_read_and_snd_i (vm, node, e, s, n_tx_pkts, 1);
Dave Barach68b0fb02017-02-28 15:15:56 -0500718}
719
720int
Florin Corasd79b41e2017-03-04 05:37:52 -0800721session_tx_fifo_dequeue_and_snd (vlib_main_t * vm, 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)
Dave Barach68b0fb02017-02-28 15:15:56 -0500724{
Florin Coras0d60a0f2018-06-29 02:02:08 -0700725 return session_tx_fifo_read_and_snd_i (vm, node, e, s, n_tx_pkts, 0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500726}
727
Florin Coras371ca502018-02-21 12:07:41 -0800728int
729session_tx_fifo_dequeue_internal (vlib_main_t * vm,
730 vlib_node_runtime_t * node,
Florin Coras52207f12018-07-12 14:48:06 -0700731 session_event_t * e,
Florin Coras0d60a0f2018-06-29 02:02:08 -0700732 stream_session_t * s, int *n_tx_pkts)
Florin Coras371ca502018-02-21 12:07:41 -0800733{
734 application_t *app;
Florin Corasef915342018-09-29 10:23:06 -0700735 if (PREDICT_FALSE (s->session_state == SESSION_STATE_CLOSED))
736 return 0;
Florin Corasab2f6db2018-08-31 14:31:41 -0700737 app = application_get (s->t_app_index);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700738 svm_fifo_unset_event (s->server_tx_fifo);
739 return app->cb_fns.builtin_app_tx_callback (s);
Florin Coras371ca502018-02-21 12:07:41 -0800740}
741
Dave Barach2c25a622017-06-26 11:35:07 -0400742always_inline stream_session_t *
Florin Coras52207f12018-07-12 14:48:06 -0700743session_event_get_session (session_event_t * e, u8 thread_index)
Florin Corasa5464812017-04-19 13:00:05 -0700744{
Florin Coras3cbc04b2017-10-02 00:18:51 -0700745 return session_get_if_valid (e->fifo->master_session_index, thread_index);
Florin Corasa5464812017-04-19 13:00:05 -0700746}
747
Florin Corasd67f1122018-05-21 17:47:40 -0700748static void
749session_update_dispatch_period (session_manager_main_t * smm, f64 now,
750 u32 thread_index)
751{
752 f64 sample, prev_period = smm->dispatch_period[thread_index], a = 0.8;
753
754 sample = now - smm->last_vlib_time[thread_index];
755 smm->dispatch_period[thread_index] = a * sample + (1 - a) * prev_period;
756 smm->last_vlib_time[thread_index] = now;
757}
758
Florin Coras0d60a0f2018-06-29 02:02:08 -0700759static uword
760session_queue_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
761 vlib_frame_t * frame)
762{
763 session_manager_main_t *smm = vnet_get_session_manager_main ();
Florin Coras3c2fed52018-07-04 04:15:05 -0700764 u32 thread_index = vm->thread_index, n_to_dequeue, n_events;
Florin Coras52207f12018-07-12 14:48:06 -0700765 session_event_t *pending_events, *e;
766 session_event_t *fifo_events;
Florin Coras3c2fed52018-07-04 04:15:05 -0700767 svm_msg_q_msg_t _msg, *msg = &_msg;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700768 f64 now = vlib_time_now (vm);
Florin Coras3c2fed52018-07-04 04:15:05 -0700769 int n_tx_packets = 0, i, rv;
Florin Coras15531972018-08-12 23:50:53 -0700770 app_worker_t *app_wrk;
Florin Coras3c2fed52018-07-04 04:15:05 -0700771 application_t *app;
772 svm_msg_q_t *mq;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700773 void (*fp) (void *);
774
775 SESSION_EVT_DBG (SESSION_EVT_POLL_GAP_TRACK, smm, thread_index);
776
777 /*
778 * Update transport time
779 */
Florin Corasd67f1122018-05-21 17:47:40 -0700780 session_update_dispatch_period (smm, now, thread_index);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700781 transport_update_time (now, thread_index);
782
783 /*
Florin Coras3c2fed52018-07-04 04:15:05 -0700784 * Get vpp queue events that we can dequeue without blocking
Florin Coras0d60a0f2018-06-29 02:02:08 -0700785 */
Florin Coras3c2fed52018-07-04 04:15:05 -0700786 mq = smm->vpp_event_queues[thread_index];
Florin Coras0d60a0f2018-06-29 02:02:08 -0700787 fifo_events = smm->free_event_vector[thread_index];
Florin Coras3c2fed52018-07-04 04:15:05 -0700788 n_to_dequeue = svm_msg_q_size (mq);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700789 pending_events = smm->pending_event_vector[thread_index];
790
791 if (!n_to_dequeue && !vec_len (pending_events)
792 && !vec_len (smm->pending_disconnects[thread_index]))
793 return 0;
794
795 SESSION_EVT_DBG (SESSION_EVT_DEQ_NODE, 0);
796
797 /*
798 * If we didn't manage to process previous events try going
799 * over them again without dequeuing new ones.
800 * XXX: Handle senders to sessions that can't keep up
801 */
802 if (0 && vec_len (pending_events) >= 100)
803 {
804 clib_warning ("too many fifo events unsolved");
805 goto skip_dequeue;
806 }
807
808 /* See you in the next life, don't be late
Florin Coras3c2fed52018-07-04 04:15:05 -0700809 * XXX: we may need priorities here */
810 if (svm_msg_q_try_lock (mq))
Florin Coras0d60a0f2018-06-29 02:02:08 -0700811 return 0;
812
813 for (i = 0; i < n_to_dequeue; i++)
814 {
815 vec_add2 (fifo_events, e, 1);
Florin Coras3c2fed52018-07-04 04:15:05 -0700816 svm_msg_q_sub_w_lock (mq, msg);
817 clib_memcpy (e, svm_msg_q_msg_data (mq, msg), sizeof (*e));
818 svm_msg_q_free_msg (mq, msg);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700819 }
820
Florin Coras3c2fed52018-07-04 04:15:05 -0700821 svm_msg_q_unlock (mq);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700822
823 vec_append (fifo_events, pending_events);
824 vec_append (fifo_events, smm->pending_disconnects[thread_index]);
825
826 _vec_len (pending_events) = 0;
827 smm->pending_event_vector[thread_index] = pending_events;
828 _vec_len (smm->pending_disconnects[thread_index]) = 0;
829
830skip_dequeue:
831 n_events = vec_len (fifo_events);
832 for (i = 0; i < n_events; i++)
833 {
834 stream_session_t *s; /* $$$ prefetch 1 ahead maybe */
Florin Coras52207f12018-07-12 14:48:06 -0700835 session_event_t *e;
Florin Coras0e88e852018-09-17 22:09:02 -0700836 u8 want_tx_evt;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700837
838 e = &fifo_events[i];
839 switch (e->event_type)
840 {
841 case FIFO_EVENT_APP_TX:
842 /* Don't try to send more that one frame per dispatch cycle */
843 if (n_tx_packets == VLIB_FRAME_SIZE)
844 {
845 vec_add1 (smm->pending_event_vector[thread_index], *e);
846 break;
847 }
848
849 s = session_event_get_session (e, thread_index);
850 if (PREDICT_FALSE (!s))
851 {
Florin Coras326b81e2018-10-04 19:03:05 -0700852 clib_warning ("session was freed!");
Florin Coras0d60a0f2018-06-29 02:02:08 -0700853 continue;
854 }
855
Florin Coras0e88e852018-09-17 22:09:02 -0700856 want_tx_evt = svm_fifo_want_tx_evt (s->server_tx_fifo);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700857 /* Spray packets in per session type frames, since they go to
858 * different nodes */
859 rv = (smm->session_tx_fns[s->session_type]) (vm, node, e, s,
860 &n_tx_packets);
861 if (PREDICT_TRUE (rv == SESSION_TX_OK))
862 {
Florin Coras0e88e852018-09-17 22:09:02 -0700863 if (PREDICT_FALSE (want_tx_evt))
864 {
865 svm_fifo_set_want_tx_evt (s->server_tx_fifo, 0);
866 session_dequeue_notify (s);
867 }
Florin Coras0d60a0f2018-06-29 02:02:08 -0700868 }
869 else if (PREDICT_FALSE (rv == SESSION_TX_NO_BUFFERS))
870 {
871 vlib_node_increment_counter (vm, node->node_index,
872 SESSION_QUEUE_ERROR_NO_BUFFER, 1);
873 continue;
874 }
875 break;
876 case FIFO_EVENT_DISCONNECT:
877 /* Make sure stream disconnects run after the pending list is
878 * drained */
879 s = session_get_from_handle (e->session_handle);
880 if (!e->postponed)
881 {
882 e->postponed = 1;
883 vec_add1 (smm->pending_disconnects[thread_index], *e);
884 continue;
885 }
886 /* If tx queue is still not empty, wait */
887 if (svm_fifo_max_dequeue (s->server_tx_fifo))
888 {
889 vec_add1 (smm->pending_disconnects[thread_index], *e);
890 continue;
891 }
892
893 stream_session_disconnect_transport (s);
894 break;
895 case FIFO_EVENT_BUILTIN_RX:
896 s = session_event_get_session (e, thread_index);
Florin Corasef915342018-09-29 10:23:06 -0700897 if (PREDICT_FALSE (!s || s->session_state >= SESSION_STATE_CLOSING))
Florin Coras0d60a0f2018-06-29 02:02:08 -0700898 continue;
899 svm_fifo_unset_event (s->server_rx_fifo);
Florin Coras15531972018-08-12 23:50:53 -0700900 app_wrk = app_worker_get (s->app_wrk_index);
901 app = application_get (app_wrk->app_index);
Florin Coras0d60a0f2018-06-29 02:02:08 -0700902 app->cb_fns.builtin_app_rx_callback (s);
903 break;
Florin Corasef915342018-09-29 10:23:06 -0700904 case FIFO_EVENT_BUILTIN_TX:
905 s = session_get_from_handle_if_valid (e->session_handle);
906 if (PREDICT_TRUE (s != 0))
907 session_tx_fifo_dequeue_internal (vm, node, e, s, &n_tx_packets);
908 break;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700909 case FIFO_EVENT_RPC:
910 fp = e->rpc_args.fp;
911 (*fp) (e->rpc_args.arg);
912 break;
Florin Coras52207f12018-07-12 14:48:06 -0700913 case SESSION_CTRL_EVT_DISCONNECTED:
914 session_mq_disconnected_handler (e->data);
915 break;
916 case SESSION_CTRL_EVT_ACCEPTED_REPLY:
917 session_mq_accepted_reply_handler (e->data);
918 break;
919 case SESSION_CTRL_EVT_CONNECTED_REPLY:
920 break;
921 case SESSION_CTRL_EVT_DISCONNECTED_REPLY:
922 session_mq_disconnected_reply_handler (e->data);
923 break;
924 case SESSION_CTRL_EVT_RESET_REPLY:
925 session_mq_reset_reply_handler (e->data);
926 break;
Florin Coras0d60a0f2018-06-29 02:02:08 -0700927 default:
928 clib_warning ("unhandled event type %d", e->event_type);
929 }
930 }
931
932 _vec_len (fifo_events) = 0;
933 smm->free_event_vector[thread_index] = fifo_events;
934
935 vlib_node_increment_counter (vm, session_queue_node.index,
936 SESSION_QUEUE_ERROR_TX, n_tx_packets);
937
938 SESSION_EVT_DBG (SESSION_EVT_DISPATCH_END, smm, thread_index);
939
940 return n_tx_packets;
941}
942
943/* *INDENT-OFF* */
944VLIB_REGISTER_NODE (session_queue_node) =
945{
946 .function = session_queue_node_fn,
947 .name = "session-queue",
948 .format_trace = format_session_queue_trace,
949 .type = VLIB_NODE_TYPE_INPUT,
950 .n_errors = ARRAY_LEN (session_queue_error_strings),
951 .error_strings = session_queue_error_strings,
952 .state = VLIB_NODE_STATE_DISABLED,
953};
954/* *INDENT-ON* */
955
Dave Barachacd2a6a2017-05-16 17:41:34 -0400956void
957dump_thread_0_event_queue (void)
958{
959 session_manager_main_t *smm = vnet_get_session_manager_main ();
960 vlib_main_t *vm = &vlib_global_main;
961 u32 my_thread_index = vm->thread_index;
Florin Coras52207f12018-07-12 14:48:06 -0700962 session_event_t _e, *e = &_e;
Florin Coras3c2fed52018-07-04 04:15:05 -0700963 svm_msg_q_ring_t *ring;
Dave Barachacd2a6a2017-05-16 17:41:34 -0400964 stream_session_t *s0;
Florin Coras3c2fed52018-07-04 04:15:05 -0700965 svm_msg_q_msg_t *msg;
966 svm_msg_q_t *mq;
Dave Barachacd2a6a2017-05-16 17:41:34 -0400967 int i, index;
Dave Barachacd2a6a2017-05-16 17:41:34 -0400968
Florin Coras3c2fed52018-07-04 04:15:05 -0700969 mq = smm->vpp_event_queues[my_thread_index];
970 index = mq->q->head;
Dave Barachacd2a6a2017-05-16 17:41:34 -0400971
Florin Coras3c2fed52018-07-04 04:15:05 -0700972 for (i = 0; i < mq->q->cursize; i++)
Dave Barachacd2a6a2017-05-16 17:41:34 -0400973 {
Florin Coras3c2fed52018-07-04 04:15:05 -0700974 msg = (svm_msg_q_msg_t *) (&mq->q->data[0] + mq->q->elsize * index);
975 ring = svm_msg_q_ring (mq, msg->ring_index);
976 clib_memcpy (e, svm_msg_q_msg_data (mq, msg), ring->elsize);
Dave Barachacd2a6a2017-05-16 17:41:34 -0400977
978 switch (e->event_type)
979 {
980 case FIFO_EVENT_APP_TX:
981 s0 = session_event_get_session (e, my_thread_index);
982 fformat (stdout, "[%04d] TX session %d\n", i, s0->session_index);
983 break;
984
985 case FIFO_EVENT_DISCONNECT:
Florin Corascea194d2017-10-02 00:18:51 -0700986 s0 = session_get_from_handle (e->session_handle);
Dave Barachacd2a6a2017-05-16 17:41:34 -0400987 fformat (stdout, "[%04d] disconnect session %d\n", i,
988 s0->session_index);
989 break;
990
991 case FIFO_EVENT_BUILTIN_RX:
992 s0 = session_event_get_session (e, my_thread_index);
993 fformat (stdout, "[%04d] builtin_rx %d\n", i, s0->session_index);
994 break;
995
996 case FIFO_EVENT_RPC:
997 fformat (stdout, "[%04d] RPC call %llx with %llx\n",
998 i, (u64) (e->rpc_args.fp), (u64) (e->rpc_args.arg));
999 break;
1000
1001 default:
1002 fformat (stdout, "[%04d] unhandled event type %d\n",
1003 i, e->event_type);
1004 break;
1005 }
1006
1007 index++;
1008
Florin Coras3c2fed52018-07-04 04:15:05 -07001009 if (index == mq->q->maxsize)
Dave Barachacd2a6a2017-05-16 17:41:34 -04001010 index = 0;
1011 }
1012}
1013
Florin Coras6534b7a2017-07-18 05:38:03 -04001014static u8
Florin Coras52207f12018-07-12 14:48:06 -07001015session_node_cmp_event (session_event_t * e, svm_fifo_t * f)
Florin Coras6534b7a2017-07-18 05:38:03 -04001016{
1017 stream_session_t *s;
1018 switch (e->event_type)
1019 {
1020 case FIFO_EVENT_APP_RX:
1021 case FIFO_EVENT_APP_TX:
1022 case FIFO_EVENT_BUILTIN_RX:
1023 if (e->fifo == f)
1024 return 1;
1025 break;
1026 case FIFO_EVENT_DISCONNECT:
1027 break;
1028 case FIFO_EVENT_RPC:
Florin Corascea194d2017-10-02 00:18:51 -07001029 s = session_get_from_handle (e->session_handle);
Florin Coras6534b7a2017-07-18 05:38:03 -04001030 if (!s)
1031 {
1032 clib_warning ("session has event but doesn't exist!");
1033 break;
1034 }
1035 if (s->server_rx_fifo == f || s->server_tx_fifo == f)
1036 return 1;
1037 break;
1038 default:
1039 break;
1040 }
1041 return 0;
1042}
1043
1044u8
Florin Coras52207f12018-07-12 14:48:06 -07001045session_node_lookup_fifo_event (svm_fifo_t * f, session_event_t * e)
Florin Coras6534b7a2017-07-18 05:38:03 -04001046{
1047 session_manager_main_t *smm = vnet_get_session_manager_main ();
Florin Coras3c2fed52018-07-04 04:15:05 -07001048 svm_msg_q_t *mq;
Florin Coras52207f12018-07-12 14:48:06 -07001049 session_event_t *pending_event_vector, *evt;
Florin Coras6534b7a2017-07-18 05:38:03 -04001050 int i, index, found = 0;
Florin Coras3c2fed52018-07-04 04:15:05 -07001051 svm_msg_q_msg_t *msg;
1052 svm_msg_q_ring_t *ring;
Florin Coras6534b7a2017-07-18 05:38:03 -04001053 u8 thread_index;
1054
1055 ASSERT (e);
1056 thread_index = f->master_thread_index;
1057 /*
1058 * Search evt queue
1059 */
Florin Coras3c2fed52018-07-04 04:15:05 -07001060 mq = smm->vpp_event_queues[thread_index];
1061 index = mq->q->head;
1062 for (i = 0; i < mq->q->cursize; i++)
Florin Coras6534b7a2017-07-18 05:38:03 -04001063 {
Florin Coras3c2fed52018-07-04 04:15:05 -07001064 msg = (svm_msg_q_msg_t *) (&mq->q->data[0] + mq->q->elsize * index);
1065 ring = svm_msg_q_ring (mq, msg->ring_index);
1066 clib_memcpy (e, svm_msg_q_msg_data (mq, msg), ring->elsize);
Florin Coras6534b7a2017-07-18 05:38:03 -04001067 found = session_node_cmp_event (e, f);
1068 if (found)
Florin Coras371ca502018-02-21 12:07:41 -08001069 return 1;
Florin Coras3c2fed52018-07-04 04:15:05 -07001070 if (++index == mq->q->maxsize)
Florin Coras6534b7a2017-07-18 05:38:03 -04001071 index = 0;
1072 }
1073 /*
1074 * Search pending events vector
1075 */
1076 pending_event_vector = smm->pending_event_vector[thread_index];
1077 vec_foreach (evt, pending_event_vector)
1078 {
1079 found = session_node_cmp_event (evt, f);
1080 if (found)
1081 {
1082 clib_memcpy (e, evt, sizeof (*evt));
1083 break;
1084 }
1085 }
1086 return found;
1087}
1088
Dave Barach2a863912017-11-28 10:11:42 -05001089static clib_error_t *
1090session_queue_exit (vlib_main_t * vm)
1091{
1092 if (vec_len (vlib_mains) < 2)
1093 return 0;
1094
1095 /*
1096 * Shut off (especially) worker-thread session nodes.
1097 * Otherwise, vpp can crash as the main thread unmaps the
1098 * API segment.
1099 */
1100 vlib_worker_thread_barrier_sync (vm);
1101 session_node_enable_disable (0 /* is_enable */ );
1102 vlib_worker_thread_barrier_release (vm);
1103 return 0;
1104}
1105
1106VLIB_MAIN_LOOP_EXIT_FUNCTION (session_queue_exit);
1107
Florin Corasfd542f12018-05-16 19:28:24 -07001108static uword
1109session_queue_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
1110 vlib_frame_t * f)
1111{
1112 f64 now, timeout = 1.0;
1113 uword *event_data = 0;
1114 uword event_type;
1115
1116 while (1)
1117 {
1118 vlib_process_wait_for_event_or_clock (vm, timeout);
1119 now = vlib_time_now (vm);
1120 event_type = vlib_process_get_events (vm, (uword **) & event_data);
1121
1122 switch (event_type)
1123 {
1124 case SESSION_Q_PROCESS_FLUSH_FRAMES:
1125 /* Flush the frames by updating all transports times */
1126 transport_update_time (now, 0);
1127 break;
1128 case SESSION_Q_PROCESS_STOP:
1129 timeout = 100000.0;
1130 break;
1131 case ~0:
1132 /* Timed out. Update time for all transports to trigger all
1133 * outstanding retransmits. */
1134 transport_update_time (now, 0);
1135 break;
1136 }
1137 vec_reset_length (event_data);
1138 }
1139 return 0;
1140}
1141
1142/* *INDENT-OFF* */
1143VLIB_REGISTER_NODE (session_queue_process_node) =
1144{
1145 .function = session_queue_process,
1146 .type = VLIB_NODE_TYPE_PROCESS,
1147 .name = "session-queue-process",
1148 .state = VLIB_NODE_STATE_DISABLED,
1149};
1150/* *INDENT-ON* */
1151
1152
Dave Barach68b0fb02017-02-28 15:15:56 -05001153/*
1154 * fd.io coding-style-patch-verification: ON
1155 *
1156 * Local Variables:
1157 * eval: (c-set-style "gnu")
1158 * End:
1159 */