blob: b6c1b2f04f34e267a91ed5305ba46be91cc1ae40 [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 Corase69f4952017-03-07 10:06:24 -080023#include <vnet/session/session_debug.h>
Florin Corase86a8ed2018-01-05 03:20:25 -080024#include <svm/queue.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050025
26vlib_node_registration_t session_queue_node;
27
28typedef struct
29{
30 u32 session_index;
31 u32 server_thread_index;
32} session_queue_trace_t;
33
34/* packet trace format function */
35static u8 *
36format_session_queue_trace (u8 * s, va_list * args)
37{
38 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
39 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
40 session_queue_trace_t *t = va_arg (*args, session_queue_trace_t *);
41
42 s = format (s, "SESSION_QUEUE: session index %d, server thread index %d",
43 t->session_index, t->server_thread_index);
44 return s;
45}
46
47vlib_node_registration_t session_queue_node;
48
Florin Coras6792ec02017-03-13 03:49:51 -070049#define foreach_session_queue_error \
50_(TX, "Packets transmitted") \
Florin Coras93992a92017-05-24 18:03:56 -070051_(TIMER, "Timer events") \
52_(NO_BUFFER, "Out of buffers")
Dave Barach68b0fb02017-02-28 15:15:56 -050053
54typedef enum
55{
56#define _(sym,str) SESSION_QUEUE_ERROR_##sym,
57 foreach_session_queue_error
58#undef _
59 SESSION_QUEUE_N_ERROR,
60} session_queue_error_t;
61
62static char *session_queue_error_strings[] = {
63#define _(sym,string) string,
64 foreach_session_queue_error
65#undef _
66};
67
Florin Corasf6d68ed2017-05-07 19:12:02 -070068always_inline void
Florin Coras8e43d042018-05-04 15:46:57 -070069session_tx_trace_buffer (vlib_main_t * vm, vlib_node_runtime_t * node,
70 u32 next_index, vlib_buffer_t * b,
71 stream_session_t * s, u32 * n_trace)
Florin Corasf6d68ed2017-05-07 19:12:02 -070072{
Florin Coras8e43d042018-05-04 15:46:57 -070073 session_queue_trace_t *t;
74 vlib_trace_buffer (vm, node, next_index, b, 1 /* follow_chain */ );
75 vlib_set_trace_count (vm, node, --*n_trace);
76 t = vlib_add_trace (vm, node, b, sizeof (*t));
77 t->session_index = s->session_index;
78 t->server_thread_index = s->thread_index;
79}
Florin Corasf6d68ed2017-05-07 19:12:02 -070080
Florin Coras8e43d042018-05-04 15:46:57 -070081always_inline void
82session_tx_fifo_chain_tail (vlib_main_t * vm, session_tx_context_t * ctx,
83 vlib_buffer_t * b, u16 * n_bufs, u8 peek_data)
84{
85 session_manager_main_t *smm = &session_manager_main;
86 vlib_buffer_t *chain_b, *prev_b;
87 u32 chain_bi0, to_deq, left_from_seg;
88 u16 len_to_deq, n_bytes_read;
89 u8 *data, j;
Florin Corasb2215d62017-08-01 16:56:58 -070090
Florin Coras8e43d042018-05-04 15:46:57 -070091 b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
92 b->total_length_not_including_first_buffer = 0;
93
94 chain_b = b;
95 left_from_seg = clib_min (ctx->snd_mss - b->current_length,
96 ctx->left_to_snd);
Florin Corasb2215d62017-08-01 16:56:58 -070097 to_deq = left_from_seg;
Florin Coras8e43d042018-05-04 15:46:57 -070098 for (j = 1; j < ctx->n_bufs_per_seg; j++)
Florin Corasf6d68ed2017-05-07 19:12:02 -070099 {
Florin Coras8e43d042018-05-04 15:46:57 -0700100 prev_b = chain_b;
101 len_to_deq = clib_min (to_deq, ctx->deq_per_buf);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700102
103 *n_bufs -= 1;
Florin Coras8e43d042018-05-04 15:46:57 -0700104 chain_bi0 = smm->tx_buffers[ctx->s->thread_index][*n_bufs];
105 _vec_len (smm->tx_buffers[ctx->s->thread_index]) = *n_bufs;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700106
Florin Coras8e43d042018-05-04 15:46:57 -0700107 chain_b = vlib_get_buffer (vm, chain_bi0);
108 chain_b->current_data = 0;
109 data = vlib_buffer_get_current (chain_b);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700110 if (peek_data)
111 {
Florin Coras8e43d042018-05-04 15:46:57 -0700112 n_bytes_read = svm_fifo_peek (ctx->s->server_tx_fifo,
113 ctx->tx_offset, len_to_deq, data);
114 ctx->tx_offset += n_bytes_read;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700115 }
116 else
117 {
Florin Coras8e43d042018-05-04 15:46:57 -0700118 if (ctx->transport_vft->tx_type == TRANSPORT_TX_DGRAM)
Florin Coras7fb0fe12018-04-09 09:24:52 -0700119 {
Florin Coras8e43d042018-05-04 15:46:57 -0700120 svm_fifo_t *f = ctx->s->server_tx_fifo;
121 session_dgram_hdr_t *hdr = &ctx->hdr;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700122 u16 deq_now;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700123 deq_now = clib_min (hdr->data_length - hdr->data_offset,
Florin Coras8e43d042018-05-04 15:46:57 -0700124 len_to_deq);
125 n_bytes_read = svm_fifo_peek (f, hdr->data_offset, deq_now,
126 data);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700127 ASSERT (n_bytes_read > 0);
128
129 hdr->data_offset += n_bytes_read;
130 if (hdr->data_offset == hdr->data_length)
Florin Coras8e43d042018-05-04 15:46:57 -0700131 svm_fifo_dequeue_drop (f, hdr->data_length);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700132 }
133 else
Florin Coras8e43d042018-05-04 15:46:57 -0700134 n_bytes_read = svm_fifo_dequeue_nowait (ctx->s->server_tx_fifo,
135 len_to_deq, data);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700136 }
Florin Coras8e43d042018-05-04 15:46:57 -0700137 ASSERT (n_bytes_read == len_to_deq);
138 chain_b->current_length = n_bytes_read;
139 b->total_length_not_including_first_buffer += chain_b->current_length;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700140
141 /* update previous buffer */
Florin Coras8e43d042018-05-04 15:46:57 -0700142 prev_b->next_buffer = chain_bi0;
143 prev_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700144
145 /* update current buffer */
Florin Coras8e43d042018-05-04 15:46:57 -0700146 chain_b->next_buffer = 0;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700147
Florin Corasb2215d62017-08-01 16:56:58 -0700148 to_deq -= n_bytes_read;
149 if (to_deq == 0)
Florin Corasf6d68ed2017-05-07 19:12:02 -0700150 break;
151 }
Florin Coras1f152cd2017-08-18 19:28:03 -0700152 ASSERT (to_deq == 0
Florin Coras8e43d042018-05-04 15:46:57 -0700153 && b->total_length_not_including_first_buffer == left_from_seg);
154 ctx->left_to_snd -= left_from_seg;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700155}
156
Dave Barach68b0fb02017-02-28 15:15:56 -0500157always_inline int
Florin Coras8e43d042018-05-04 15:46:57 -0700158session_output_try_get_buffers (vlib_main_t * vm,
Florin Corasd79b41e2017-03-04 05:37:52 -0800159 session_manager_main_t * smm,
Florin Coras8e43d042018-05-04 15:46:57 -0700160 u32 thread_index, u16 * n_bufs, u32 wanted)
Dave Barach68b0fb02017-02-28 15:15:56 -0500161{
Florin Corasf08f26d2018-05-10 13:20:47 -0700162 u32 n_alloc;
163 vec_validate_aligned (smm->tx_buffers[thread_index], wanted - 1,
Florin Coras8e43d042018-05-04 15:46:57 -0700164 CLIB_CACHE_LINE_BYTES);
Florin Corasf08f26d2018-05-10 13:20:47 -0700165 n_alloc = vlib_buffer_alloc (vm, &smm->tx_buffers[thread_index][*n_bufs],
166 wanted - *n_bufs);
167 *n_bufs += n_alloc;
Florin Coras8e43d042018-05-04 15:46:57 -0700168 _vec_len (smm->tx_buffers[thread_index]) = *n_bufs;
Florin Corasf08f26d2018-05-10 13:20:47 -0700169 return n_alloc;
Florin Coras8e43d042018-05-04 15:46:57 -0700170}
171
172always_inline void
173session_tx_fill_buffer (vlib_main_t * vm, session_tx_context_t * ctx,
174 vlib_buffer_t * b, u16 * n_bufs, u8 peek_data)
175{
176 u32 len_to_deq;
177 u8 *data0;
178 int n_bytes_read;
179
180 /*
181 * Start with the first buffer in chain
182 */
183 b->error = 0;
184 b->flags = VNET_BUFFER_F_LOCALLY_ORIGINATED;
185 b->current_data = 0;
186 b->total_length_not_including_first_buffer = 0;
187
188 data0 = vlib_buffer_make_headroom (b, MAX_HDRS_LEN);
189 len_to_deq = clib_min (ctx->left_to_snd, ctx->deq_per_first_buf);
190
Florin Coras7fb0fe12018-04-09 09:24:52 -0700191 if (peek_data)
192 {
Florin Coras8e43d042018-05-04 15:46:57 -0700193 n_bytes_read = svm_fifo_peek (ctx->s->server_tx_fifo, ctx->tx_offset,
194 len_to_deq, data0);
195 ASSERT (n_bytes_read > 0);
196 /* Keep track of progress locally, transport is also supposed to
197 * increment it independently when pushing the header */
198 ctx->tx_offset += n_bytes_read;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700199 }
200 else
201 {
Florin Coras8e43d042018-05-04 15:46:57 -0700202 if (ctx->transport_vft->tx_type == TRANSPORT_TX_DGRAM)
203 {
204 session_dgram_hdr_t *hdr = &ctx->hdr;
205 svm_fifo_t *f = ctx->s->server_tx_fifo;
206 u16 deq_now;
207 u32 offset;
208
209 ASSERT (hdr->data_length > hdr->data_offset);
210 deq_now = clib_min (hdr->data_length - hdr->data_offset,
211 len_to_deq);
212 offset = hdr->data_offset + SESSION_CONN_HDR_LEN;
213 n_bytes_read = svm_fifo_peek (f, offset, deq_now, data0);
214 ASSERT (n_bytes_read > 0);
215
216 if (ctx->s->session_state == SESSION_STATE_LISTENING)
217 {
218 ip_copy (&ctx->tc->rmt_ip, &hdr->rmt_ip, ctx->tc->is_ip4);
219 ctx->tc->rmt_port = hdr->rmt_port;
220 }
221 hdr->data_offset += n_bytes_read;
222 if (hdr->data_offset == hdr->data_length)
223 {
224 offset = hdr->data_length + SESSION_CONN_HDR_LEN;
225 svm_fifo_dequeue_drop (f, offset);
226 }
227 }
Florin Coras7fb0fe12018-04-09 09:24:52 -0700228 else
229 {
Florin Coras8e43d042018-05-04 15:46:57 -0700230 n_bytes_read = svm_fifo_dequeue_nowait (ctx->s->server_tx_fifo,
231 len_to_deq, data0);
232 ASSERT (n_bytes_read > 0);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700233 }
234 }
Florin Coras8e43d042018-05-04 15:46:57 -0700235 b->current_length = n_bytes_read;
236 ctx->left_to_snd -= n_bytes_read;
Dave Barach68b0fb02017-02-28 15:15:56 -0500237
Florin Coras8e43d042018-05-04 15:46:57 -0700238 /*
239 * Fill in the remaining buffers in the chain, if any
240 */
241 if (PREDICT_FALSE (ctx->n_bufs_per_seg > 1 && ctx->left_to_snd))
242 session_tx_fifo_chain_tail (vm, ctx, b, n_bufs, peek_data);
Dave Barach68b0fb02017-02-28 15:15:56 -0500243
Florin Coras8e43d042018-05-04 15:46:57 -0700244 /* *INDENT-OFF* */
245 SESSION_EVT_DBG(SESSION_EVT_DEQ, s, ({
246 ed->data[0] = e->event_type;
247 ed->data[1] = max_dequeue;
248 ed->data[2] = len_to_deq;
249 ed->data[3] = left_to_snd;
250 }));
251 /* *INDENT-ON* */
252}
253
254always_inline u8
255session_tx_not_ready (stream_session_t * s, u8 peek_data)
256{
257 if (peek_data)
Florin Corase04c2992017-03-01 08:17:34 -0800258 {
Florin Coras8e43d042018-05-04 15:46:57 -0700259 /* Can retransmit for closed sessions but can't send new data if
260 * session is not ready or closed */
261 if (s->session_state < SESSION_STATE_READY)
262 return 1;
Florin Corasca1c8f32018-05-23 21:01:30 -0700263 if (s->session_state == SESSION_STATE_CLOSED)
264 return 2;
Florin Corase04c2992017-03-01 08:17:34 -0800265 }
Florin Coras8e43d042018-05-04 15:46:57 -0700266 return 0;
267}
Dave Barach68b0fb02017-02-28 15:15:56 -0500268
Florin Coras8e43d042018-05-04 15:46:57 -0700269always_inline transport_connection_t *
270session_tx_get_transport (session_tx_context_t * ctx, u8 peek_data)
271{
272 if (peek_data)
273 {
274 return ctx->transport_vft->get_connection (ctx->s->connection_index,
275 ctx->s->thread_index);
276 }
277 else
278 {
279 if (ctx->s->session_state == SESSION_STATE_LISTENING)
280 return ctx->transport_vft->get_listener (ctx->s->connection_index);
281 else
282 {
283 return ctx->transport_vft->get_connection (ctx->s->connection_index,
284 ctx->s->thread_index);
285 }
286 }
287}
Florin Coras6a9b68b2017-11-21 04:20:42 -0800288
Florin Coras8e43d042018-05-04 15:46:57 -0700289always_inline void
290session_tx_set_dequeue_params (vlib_main_t * vm, session_tx_context_t * ctx,
Florin Corasf08f26d2018-05-10 13:20:47 -0700291 u32 max_segs, u8 peek_data)
Florin Coras8e43d042018-05-04 15:46:57 -0700292{
293 u32 n_bytes_per_buf, n_bytes_per_seg;
294 ctx->max_dequeue = svm_fifo_max_dequeue (ctx->s->server_tx_fifo);
Dave Barach68b0fb02017-02-28 15:15:56 -0500295 if (peek_data)
296 {
Florin Coras9d063042017-09-14 03:08:00 -0400297 /* Offset in rx fifo from where to peek data */
Florin Coras8e43d042018-05-04 15:46:57 -0700298 ctx->tx_offset = ctx->transport_vft->tx_fifo_offset (ctx->tc);
299 if (PREDICT_FALSE (ctx->tx_offset >= ctx->max_dequeue))
300 {
301 ctx->max_len_to_snd = 0;
302 return;
303 }
304 ctx->max_dequeue -= ctx->tx_offset;
Dave Barach68b0fb02017-02-28 15:15:56 -0500305 }
Florin Coras7fb0fe12018-04-09 09:24:52 -0700306 else
307 {
Florin Coras8e43d042018-05-04 15:46:57 -0700308 if (ctx->transport_vft->tx_type == TRANSPORT_TX_DGRAM)
Florin Coras7fb0fe12018-04-09 09:24:52 -0700309 {
Florin Coras8e43d042018-05-04 15:46:57 -0700310 if (ctx->max_dequeue <= sizeof (ctx->hdr))
311 {
312 ctx->max_len_to_snd = 0;
313 return;
314 }
315 svm_fifo_peek (ctx->s->server_tx_fifo, 0, sizeof (ctx->hdr),
316 (u8 *) & ctx->hdr);
317 ASSERT (ctx->hdr.data_length > ctx->hdr.data_offset);
318 ctx->max_dequeue = ctx->hdr.data_length - ctx->hdr.data_offset;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700319 }
320 }
Florin Coras8e43d042018-05-04 15:46:57 -0700321 ASSERT (ctx->max_dequeue > 0);
Florin Coras6792ec02017-03-13 03:49:51 -0700322
323 /* Ensure we're not writing more than transport window allows */
Florin Coras8e43d042018-05-04 15:46:57 -0700324 if (ctx->max_dequeue < ctx->snd_space)
Florin Coras3e350af2017-03-30 02:54:28 -0700325 {
326 /* Constrained by tx queue. Try to send only fully formed segments */
Florin Coras8e43d042018-05-04 15:46:57 -0700327 ctx->max_len_to_snd =
328 (ctx->max_dequeue > ctx->snd_mss) ?
329 ctx->max_dequeue - ctx->max_dequeue % ctx->snd_mss : ctx->max_dequeue;
Florin Coras3e350af2017-03-30 02:54:28 -0700330 /* TODO Nagle ? */
331 }
332 else
333 {
Florin Coras1f152cd2017-08-18 19:28:03 -0700334 /* Expectation is that snd_space0 is already a multiple of snd_mss */
Florin Coras8e43d042018-05-04 15:46:57 -0700335 ctx->max_len_to_snd = ctx->snd_space;
Florin Coras3e350af2017-03-30 02:54:28 -0700336 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500337
Florin Corasf08f26d2018-05-10 13:20:47 -0700338 /* Check if we're tx constrained by the node */
339 ctx->n_segs_per_evt = ceil ((f64) ctx->max_len_to_snd / ctx->snd_mss);
340 if (ctx->n_segs_per_evt > max_segs)
341 {
342 ctx->n_segs_per_evt = max_segs;
343 ctx->max_len_to_snd = max_segs * ctx->snd_mss;
344 }
345
Florin Coras8e43d042018-05-04 15:46:57 -0700346 n_bytes_per_buf = vlib_buffer_free_list_buffer_size (vm,
347 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
Florin Corase87216f2017-08-17 16:59:22 -0700348 ASSERT (n_bytes_per_buf > MAX_HDRS_LEN);
Florin Coras8e43d042018-05-04 15:46:57 -0700349 n_bytes_per_seg = MAX_HDRS_LEN + ctx->snd_mss;
Florin Corasf08f26d2018-05-10 13:20:47 -0700350 ctx->n_bufs_per_seg = ceil ((f64) n_bytes_per_seg / n_bytes_per_buf);
Florin Coras8e43d042018-05-04 15:46:57 -0700351 ctx->deq_per_buf = clib_min (ctx->snd_mss, n_bytes_per_buf);
352 ctx->deq_per_first_buf = clib_min (ctx->snd_mss,
353 n_bytes_per_buf - MAX_HDRS_LEN);
354}
Florin Corasf6d68ed2017-05-07 19:12:02 -0700355
Florin Coras8e43d042018-05-04 15:46:57 -0700356always_inline int
357session_tx_fifo_read_and_snd_i (vlib_main_t * vm, vlib_node_runtime_t * node,
358 session_fifo_event_t * e,
359 stream_session_t * s, int *n_tx_packets,
360 u8 peek_data)
361{
362 u32 next_index, next0, next1, next2, next3, *to_next, n_left_to_next;
Florin Corasf08f26d2018-05-10 13:20:47 -0700363 u32 n_trace = vlib_get_trace_count (vm, node), n_bufs_needed = 0;
364 u32 thread_index = s->thread_index, n_left, pbi;
Florin Coras8e43d042018-05-04 15:46:57 -0700365 session_manager_main_t *smm = &session_manager_main;
366 session_tx_context_t *ctx = &smm->ctx[thread_index];
367 transport_proto_t tp;
368 vlib_buffer_t *pb;
Florin Corasca1c8f32018-05-23 21:01:30 -0700369 u16 n_bufs, rv;
Florin Coras8e43d042018-05-04 15:46:57 -0700370
Florin Corasca1c8f32018-05-23 21:01:30 -0700371 if (PREDICT_FALSE ((rv = session_tx_not_ready (s, peek_data))))
Florin Coras8e43d042018-05-04 15:46:57 -0700372 {
Florin Corasca1c8f32018-05-23 21:01:30 -0700373 if (rv < 2)
374 vec_add1 (smm->pending_event_vector[thread_index], *e);
Florin Coras8e43d042018-05-04 15:46:57 -0700375 return 0;
376 }
377
378 next_index = smm->session_type_to_next[s->session_type];
379 next0 = next1 = next2 = next3 = next_index;
380
381 tp = session_get_transport_proto (s);
382 ctx->s = s;
383 ctx->transport_vft = transport_protocol_get_vft (tp);
384 ctx->tc = session_tx_get_transport (ctx, peek_data);
385 ctx->snd_mss = ctx->transport_vft->send_mss (ctx->tc);
386 ctx->snd_space = ctx->transport_vft->send_space (ctx->tc);
387 if (ctx->snd_space == 0 || ctx->snd_mss == 0)
388 {
389 vec_add1 (smm->pending_event_vector[thread_index], *e);
390 return 0;
391 }
392
393 /* Allow enqueuing of a new event */
394 svm_fifo_unset_event (s->server_tx_fifo);
395
396 /* Check how much we can pull. */
Florin Corasf08f26d2018-05-10 13:20:47 -0700397 session_tx_set_dequeue_params (vm, ctx, VLIB_FRAME_SIZE - *n_tx_packets,
398 peek_data);
399
Florin Coras8e43d042018-05-04 15:46:57 -0700400 if (PREDICT_FALSE (!ctx->max_len_to_snd))
401 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500402
403 n_bufs = vec_len (smm->tx_buffers[thread_index]);
Florin Corasf08f26d2018-05-10 13:20:47 -0700404 n_bufs_needed = ctx->n_segs_per_evt * ctx->n_bufs_per_seg;
Florin Coras8e43d042018-05-04 15:46:57 -0700405
406 /*
407 * Make sure we have at least one full frame of buffers ready
408 */
Florin Corasf08f26d2018-05-10 13:20:47 -0700409 if (n_bufs < n_bufs_needed)
Dave Barach68b0fb02017-02-28 15:15:56 -0500410 {
Florin Coras8e43d042018-05-04 15:46:57 -0700411 session_output_try_get_buffers (vm, smm, thread_index, &n_bufs,
Florin Corasf08f26d2018-05-10 13:20:47 -0700412 ctx->n_bufs_per_seg * VLIB_FRAME_SIZE);
413 if (PREDICT_FALSE (n_bufs < n_bufs_needed))
Dave Barach68b0fb02017-02-28 15:15:56 -0500414 {
Florin Coras8e43d042018-05-04 15:46:57 -0700415 vec_add1 (smm->pending_event_vector[thread_index], *e);
416 return -1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500417 }
Florin Coras8e43d042018-05-04 15:46:57 -0700418 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500419
Florin Coras8e43d042018-05-04 15:46:57 -0700420 /*
421 * Write until we fill up a frame
422 */
423 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Florin Corasf08f26d2018-05-10 13:20:47 -0700424 if (PREDICT_FALSE (ctx->n_segs_per_evt > n_left_to_next))
Florin Coras8e43d042018-05-04 15:46:57 -0700425 {
Florin Corasf08f26d2018-05-10 13:20:47 -0700426 ctx->n_segs_per_evt = n_left_to_next;
427 ctx->max_len_to_snd = ctx->snd_mss * n_left_to_next;
428 }
429 ctx->left_to_snd = ctx->max_len_to_snd;
430 n_left = ctx->n_segs_per_evt;
431 while (n_left)
432 {
433 while (n_left >= 4)
Dave Barach68b0fb02017-02-28 15:15:56 -0500434 {
Florin Coras8e43d042018-05-04 15:46:57 -0700435 vlib_buffer_t *b0, *b1;
436 u32 bi0, bi1;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700437
Florin Coras8e43d042018-05-04 15:46:57 -0700438 pbi = smm->tx_buffers[thread_index][n_bufs - 3];
439 pb = vlib_get_buffer (vm, pbi);
440 vlib_prefetch_buffer_header (pb, STORE);
441 pbi = smm->tx_buffers[thread_index][n_bufs - 4];
442 pb = vlib_get_buffer (vm, pbi);
443 vlib_prefetch_buffer_header (pb, STORE);
Florin Coras81a13db2018-03-16 08:48:31 -0700444
Florin Coras8e43d042018-05-04 15:46:57 -0700445 to_next[0] = bi0 = smm->tx_buffers[thread_index][--n_bufs];
446 to_next[1] = bi1 = smm->tx_buffers[thread_index][--n_bufs];
Florin Corasf6359c82017-06-19 12:26:09 -0400447
Dave Barach68b0fb02017-02-28 15:15:56 -0500448 b0 = vlib_get_buffer (vm, bi0);
Florin Coras8e43d042018-05-04 15:46:57 -0700449 b1 = vlib_get_buffer (vm, bi1);
Dave Barach68b0fb02017-02-28 15:15:56 -0500450
Florin Coras8e43d042018-05-04 15:46:57 -0700451 session_tx_fill_buffer (vm, ctx, b0, &n_bufs, peek_data);
452 session_tx_fill_buffer (vm, ctx, b1, &n_bufs, peek_data);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700453
Florin Coras8e43d042018-05-04 15:46:57 -0700454 ctx->transport_vft->push_header (ctx->tc, b0);
455 ctx->transport_vft->push_header (ctx->tc, b1);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700456
Florin Coras8e43d042018-05-04 15:46:57 -0700457 to_next += 2;
458 n_left_to_next -= 2;
Florin Corasf08f26d2018-05-10 13:20:47 -0700459 n_left -= 2;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700460
Dave Barach68b0fb02017-02-28 15:15:56 -0500461 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
Florin Coras8e43d042018-05-04 15:46:57 -0700462 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b1);
Florin Coras4eeeaaf2017-09-05 14:03:37 -0400463
Dave Barach68b0fb02017-02-28 15:15:56 -0500464 if (PREDICT_FALSE (n_trace > 0))
465 {
Florin Coras8e43d042018-05-04 15:46:57 -0700466 session_tx_trace_buffer (vm, node, next_index, b0, s, &n_trace);
467 if (n_trace)
468 session_tx_trace_buffer (vm, node, next_index, b1, s,
469 &n_trace);
Dave Barach68b0fb02017-02-28 15:15:56 -0500470 }
471
Florin Coras8e43d042018-05-04 15:46:57 -0700472 vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
473 n_left_to_next, bi0, bi1,
474 next0, next1);
Dave Barach68b0fb02017-02-28 15:15:56 -0500475 }
Florin Corasf08f26d2018-05-10 13:20:47 -0700476 while (n_left)
Florin Coras8e43d042018-05-04 15:46:57 -0700477 {
478 vlib_buffer_t *b0;
479 u32 bi0;
480
481 ASSERT (n_bufs >= 1);
482 to_next[0] = bi0 = smm->tx_buffers[thread_index][--n_bufs];
483 b0 = vlib_get_buffer (vm, bi0);
484 session_tx_fill_buffer (vm, ctx, b0, &n_bufs, peek_data);
485
486 /* Ask transport to push header after current_length and
487 * total_length_not_including_first_buffer are updated */
488 ctx->transport_vft->push_header (ctx->tc, b0);
489
490 to_next += 1;
491 n_left_to_next -= 1;
Florin Corasf08f26d2018-05-10 13:20:47 -0700492 n_left -= 1;
Florin Coras8e43d042018-05-04 15:46:57 -0700493
494 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
495 if (PREDICT_FALSE (n_trace > 0))
496 session_tx_trace_buffer (vm, node, next_index, b0, s, &n_trace);
497
498 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
499 n_left_to_next, bi0, next0);
500 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500501 }
Florin Coras8e43d042018-05-04 15:46:57 -0700502 _vec_len (smm->tx_buffers[thread_index]) = n_bufs;
Florin Corasf08f26d2018-05-10 13:20:47 -0700503 *n_tx_packets += ctx->n_segs_per_evt;
Florin Coras8e43d042018-05-04 15:46:57 -0700504 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
Dave Barach68b0fb02017-02-28 15:15:56 -0500505
Florin Coras6792ec02017-03-13 03:49:51 -0700506 /* If we couldn't dequeue all bytes mark as partially read */
Florin Corasf08f26d2018-05-10 13:20:47 -0700507 ASSERT (ctx->left_to_snd == 0);
Florin Coras8e43d042018-05-04 15:46:57 -0700508 if (ctx->max_len_to_snd < ctx->max_dequeue)
509 if (svm_fifo_set_event (s->server_tx_fifo))
510 vec_add1 (smm->pending_event_vector[thread_index], *e);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700511
Florin Coras8e43d042018-05-04 15:46:57 -0700512 if (!peek_data && ctx->transport_vft->tx_type == TRANSPORT_TX_DGRAM)
Dave Barach68b0fb02017-02-28 15:15:56 -0500513 {
Florin Coras7fb0fe12018-04-09 09:24:52 -0700514 /* Fix dgram pre header */
Florin Coras8e43d042018-05-04 15:46:57 -0700515 if (ctx->max_len_to_snd < ctx->max_dequeue)
516 svm_fifo_overwrite_head (s->server_tx_fifo, (u8 *) & ctx->hdr,
Florin Coras7fb0fe12018-04-09 09:24:52 -0700517 sizeof (session_dgram_pre_hdr_t));
518 /* More data needs to be read */
Florin Coras8e43d042018-05-04 15:46:57 -0700519 else if (svm_fifo_max_dequeue (s->server_tx_fifo) > 0)
520 if (svm_fifo_set_event (s->server_tx_fifo))
521 vec_add1 (smm->pending_event_vector[thread_index], *e);
Dave Barach68b0fb02017-02-28 15:15:56 -0500522 }
523 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500524}
525
526int
Florin Corasd79b41e2017-03-04 05:37:52 -0800527session_tx_fifo_peek_and_snd (vlib_main_t * vm, vlib_node_runtime_t * node,
Florin Corasd79b41e2017-03-04 05:37:52 -0800528 session_fifo_event_t * e0,
Florin Coras8e43d042018-05-04 15:46:57 -0700529 stream_session_t * s0, int *n_tx_pkts)
Dave Barach68b0fb02017-02-28 15:15:56 -0500530{
Florin Coras8e43d042018-05-04 15:46:57 -0700531 return session_tx_fifo_read_and_snd_i (vm, node, e0, s0, n_tx_pkts, 1);
Dave Barach68b0fb02017-02-28 15:15:56 -0500532}
533
534int
Florin Corasd79b41e2017-03-04 05:37:52 -0800535session_tx_fifo_dequeue_and_snd (vlib_main_t * vm, vlib_node_runtime_t * node,
Florin Corasd79b41e2017-03-04 05:37:52 -0800536 session_fifo_event_t * e0,
Florin Coras8e43d042018-05-04 15:46:57 -0700537 stream_session_t * s0, int *n_tx_pkts)
Dave Barach68b0fb02017-02-28 15:15:56 -0500538{
Florin Coras8e43d042018-05-04 15:46:57 -0700539 return session_tx_fifo_read_and_snd_i (vm, node, e0, s0, n_tx_pkts, 0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500540}
541
Florin Coras371ca502018-02-21 12:07:41 -0800542int
543session_tx_fifo_dequeue_internal (vlib_main_t * vm,
544 vlib_node_runtime_t * node,
Florin Coras371ca502018-02-21 12:07:41 -0800545 session_fifo_event_t * e0,
Florin Coras8e43d042018-05-04 15:46:57 -0700546 stream_session_t * s0, int *n_tx_pkts)
Florin Coras371ca502018-02-21 12:07:41 -0800547{
548 application_t *app;
549 app = application_get (s0->opaque);
550 svm_fifo_unset_event (s0->server_tx_fifo);
551 return app->cb_fns.builtin_app_tx_callback (s0);
552}
553
Dave Barach2c25a622017-06-26 11:35:07 -0400554always_inline stream_session_t *
555session_event_get_session (session_fifo_event_t * e, u8 thread_index)
Florin Corasa5464812017-04-19 13:00:05 -0700556{
Florin Coras3cbc04b2017-10-02 00:18:51 -0700557 return session_get_if_valid (e->fifo->master_session_index, thread_index);
Florin Corasa5464812017-04-19 13:00:05 -0700558}
559
Dave Barachacd2a6a2017-05-16 17:41:34 -0400560void
561dump_thread_0_event_queue (void)
562{
563 session_manager_main_t *smm = vnet_get_session_manager_main ();
564 vlib_main_t *vm = &vlib_global_main;
565 u32 my_thread_index = vm->thread_index;
566 session_fifo_event_t _e, *e = &_e;
567 stream_session_t *s0;
568 int i, index;
569 i8 *headp;
570
Florin Corase86a8ed2018-01-05 03:20:25 -0800571 svm_queue_t *q;
Dave Barachacd2a6a2017-05-16 17:41:34 -0400572 q = smm->vpp_event_queues[my_thread_index];
573
574 index = q->head;
575
576 for (i = 0; i < q->cursize; i++)
577 {
578 headp = (i8 *) (&q->data[0] + q->elsize * index);
579 clib_memcpy (e, headp, q->elsize);
580
581 switch (e->event_type)
582 {
583 case FIFO_EVENT_APP_TX:
584 s0 = session_event_get_session (e, my_thread_index);
585 fformat (stdout, "[%04d] TX session %d\n", i, s0->session_index);
586 break;
587
588 case FIFO_EVENT_DISCONNECT:
Florin Corascea194d2017-10-02 00:18:51 -0700589 s0 = session_get_from_handle (e->session_handle);
Dave Barachacd2a6a2017-05-16 17:41:34 -0400590 fformat (stdout, "[%04d] disconnect session %d\n", i,
591 s0->session_index);
592 break;
593
594 case FIFO_EVENT_BUILTIN_RX:
595 s0 = session_event_get_session (e, my_thread_index);
596 fformat (stdout, "[%04d] builtin_rx %d\n", i, s0->session_index);
597 break;
598
599 case FIFO_EVENT_RPC:
600 fformat (stdout, "[%04d] RPC call %llx with %llx\n",
601 i, (u64) (e->rpc_args.fp), (u64) (e->rpc_args.arg));
602 break;
603
604 default:
605 fformat (stdout, "[%04d] unhandled event type %d\n",
606 i, e->event_type);
607 break;
608 }
609
610 index++;
611
612 if (index == q->maxsize)
613 index = 0;
614 }
615}
616
Florin Coras6534b7a2017-07-18 05:38:03 -0400617static u8
618session_node_cmp_event (session_fifo_event_t * e, svm_fifo_t * f)
619{
620 stream_session_t *s;
621 switch (e->event_type)
622 {
623 case FIFO_EVENT_APP_RX:
624 case FIFO_EVENT_APP_TX:
625 case FIFO_EVENT_BUILTIN_RX:
626 if (e->fifo == f)
627 return 1;
628 break;
629 case FIFO_EVENT_DISCONNECT:
630 break;
631 case FIFO_EVENT_RPC:
Florin Corascea194d2017-10-02 00:18:51 -0700632 s = session_get_from_handle (e->session_handle);
Florin Coras6534b7a2017-07-18 05:38:03 -0400633 if (!s)
634 {
635 clib_warning ("session has event but doesn't exist!");
636 break;
637 }
638 if (s->server_rx_fifo == f || s->server_tx_fifo == f)
639 return 1;
640 break;
641 default:
642 break;
643 }
644 return 0;
645}
646
647u8
648session_node_lookup_fifo_event (svm_fifo_t * f, session_fifo_event_t * e)
649{
650 session_manager_main_t *smm = vnet_get_session_manager_main ();
Florin Corase86a8ed2018-01-05 03:20:25 -0800651 svm_queue_t *q;
Florin Coras6534b7a2017-07-18 05:38:03 -0400652 session_fifo_event_t *pending_event_vector, *evt;
653 int i, index, found = 0;
654 i8 *headp;
655 u8 thread_index;
656
657 ASSERT (e);
658 thread_index = f->master_thread_index;
659 /*
660 * Search evt queue
661 */
662 q = smm->vpp_event_queues[thread_index];
663 index = q->head;
664 for (i = 0; i < q->cursize; i++)
665 {
666 headp = (i8 *) (&q->data[0] + q->elsize * index);
667 clib_memcpy (e, headp, q->elsize);
668 found = session_node_cmp_event (e, f);
669 if (found)
Florin Coras371ca502018-02-21 12:07:41 -0800670 return 1;
Florin Coras6534b7a2017-07-18 05:38:03 -0400671 if (++index == q->maxsize)
672 index = 0;
673 }
674 /*
675 * Search pending events vector
676 */
677 pending_event_vector = smm->pending_event_vector[thread_index];
678 vec_foreach (evt, pending_event_vector)
679 {
680 found = session_node_cmp_event (evt, f);
681 if (found)
682 {
683 clib_memcpy (e, evt, sizeof (*evt));
684 break;
685 }
686 }
687 return found;
688}
689
Dave Barach68b0fb02017-02-28 15:15:56 -0500690static uword
691session_queue_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
692 vlib_frame_t * frame)
693{
694 session_manager_main_t *smm = vnet_get_session_manager_main ();
Florin Corasb2371c22018-05-31 17:14:10 -0700695 session_fifo_event_t *my_pending_event_vector, *e;
Dave Barachacd2a6a2017-05-16 17:41:34 -0400696 session_fifo_event_t *my_fifo_events;
Florin Corase04c2992017-03-01 08:17:34 -0800697 u32 n_to_dequeue, n_events;
Florin Corase86a8ed2018-01-05 03:20:25 -0800698 svm_queue_t *q;
Florin Coras6792ec02017-03-13 03:49:51 -0700699 application_t *app;
Dave Barach68b0fb02017-02-28 15:15:56 -0500700 int n_tx_packets = 0;
Florin Corasf08f26d2018-05-10 13:20:47 -0700701 u32 thread_index = vm->thread_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500702 int i, rv;
Florin Coras3e350af2017-03-30 02:54:28 -0700703 f64 now = vlib_time_now (vm);
Dave Barache5f1d272017-05-10 13:34:04 -0400704 void (*fp) (void *);
Florin Coras3e350af2017-03-30 02:54:28 -0700705
Florin Corasf08f26d2018-05-10 13:20:47 -0700706 SESSION_EVT_DBG (SESSION_EVT_POLL_GAP_TRACK, smm, thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500707
708 /*
Florin Coras561af9b2017-12-09 10:19:43 -0800709 * Update transport time
Dave Barach68b0fb02017-02-28 15:15:56 -0500710 */
Florin Corasf08f26d2018-05-10 13:20:47 -0700711 transport_update_time (now, thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500712
713 /*
714 * Get vpp queue events
715 */
Florin Corasf08f26d2018-05-10 13:20:47 -0700716 q = smm->vpp_event_queues[thread_index];
Dave Barach68b0fb02017-02-28 15:15:56 -0500717 if (PREDICT_FALSE (q == 0))
718 return 0;
719
Florin Corasf08f26d2018-05-10 13:20:47 -0700720 my_fifo_events = smm->free_event_vector[thread_index];
Dave Barachacd2a6a2017-05-16 17:41:34 -0400721
Dave Barach68b0fb02017-02-28 15:15:56 -0500722 /* min number of events we can dequeue without blocking */
723 n_to_dequeue = q->cursize;
Florin Corasf08f26d2018-05-10 13:20:47 -0700724 my_pending_event_vector = smm->pending_event_vector[thread_index];
Dave Barach68b0fb02017-02-28 15:15:56 -0500725
Florin Coras3cbc04b2017-10-02 00:18:51 -0700726 if (!n_to_dequeue && !vec_len (my_pending_event_vector)
Florin Corasb2371c22018-05-31 17:14:10 -0700727 && !vec_len (smm->pending_disconnects[thread_index]))
Florin Corase04c2992017-03-01 08:17:34 -0800728 return 0;
729
Florin Coras6792ec02017-03-13 03:49:51 -0700730 SESSION_EVT_DBG (SESSION_EVT_DEQ_NODE, 0);
731
Florin Corase04c2992017-03-01 08:17:34 -0800732 /*
733 * If we didn't manage to process previous events try going
Dave Barach68b0fb02017-02-28 15:15:56 -0500734 * over them again without dequeuing new ones.
Florin Corase04c2992017-03-01 08:17:34 -0800735 */
736 /* XXX: Block senders to sessions that can't keep up */
Dave Barachacd2a6a2017-05-16 17:41:34 -0400737 if (0 && vec_len (my_pending_event_vector) >= 100)
Florin Coras6792ec02017-03-13 03:49:51 -0700738 {
739 clib_warning ("too many fifo events unsolved");
740 goto skip_dequeue;
741 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500742
743 /* See you in the next life, don't be late */
744 if (pthread_mutex_trylock (&q->mutex))
745 return 0;
746
747 for (i = 0; i < n_to_dequeue; i++)
748 {
749 vec_add2 (my_fifo_events, e, 1);
Florin Corase86a8ed2018-01-05 03:20:25 -0800750 svm_queue_sub_raw (q, (u8 *) e);
Dave Barach68b0fb02017-02-28 15:15:56 -0500751 }
752
753 /* The other side of the connection is not polling */
754 if (q->cursize < (q->maxsize / 8))
755 (void) pthread_cond_broadcast (&q->condvar);
756 pthread_mutex_unlock (&q->mutex);
757
Dave Barachacd2a6a2017-05-16 17:41:34 -0400758 vec_append (my_fifo_events, my_pending_event_vector);
Florin Corasf08f26d2018-05-10 13:20:47 -0700759 vec_append (my_fifo_events, smm->pending_disconnects[thread_index]);
Dave Barachacd2a6a2017-05-16 17:41:34 -0400760
761 _vec_len (my_pending_event_vector) = 0;
Florin Corasf08f26d2018-05-10 13:20:47 -0700762 smm->pending_event_vector[thread_index] = my_pending_event_vector;
763 _vec_len (smm->pending_disconnects[thread_index]) = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500764
765skip_dequeue:
Florin Corase04c2992017-03-01 08:17:34 -0800766 n_events = vec_len (my_fifo_events);
767 for (i = 0; i < n_events; i++)
Dave Barach68b0fb02017-02-28 15:15:56 -0500768 {
Florin Corasa5464812017-04-19 13:00:05 -0700769 stream_session_t *s0; /* $$$ prefetch 1 ahead maybe */
Dave Barach68b0fb02017-02-28 15:15:56 -0500770 session_fifo_event_t *e0;
771
772 e0 = &my_fifo_events[i];
Dave Barach68b0fb02017-02-28 15:15:56 -0500773 switch (e0->event_type)
774 {
Florin Corasa5464812017-04-19 13:00:05 -0700775 case FIFO_EVENT_APP_TX:
Florin Corasf08f26d2018-05-10 13:20:47 -0700776 if (n_tx_packets == VLIB_FRAME_SIZE)
777 {
778 vec_add1 (smm->pending_event_vector[thread_index], *e0);
779 break;
780 }
Florin Corasa5464812017-04-19 13:00:05 -0700781
Florin Corasf08f26d2018-05-10 13:20:47 -0700782 s0 = session_event_get_session (e0, thread_index);
Florin Coras9d063042017-09-14 03:08:00 -0400783 if (PREDICT_FALSE (!s0))
Florin Corasa5464812017-04-19 13:00:05 -0700784 {
785 clib_warning ("It's dead, Jim!");
786 continue;
787 }
Florin Corasf08f26d2018-05-10 13:20:47 -0700788
Dave Barach68b0fb02017-02-28 15:15:56 -0500789 /* Spray packets in per session type frames, since they go to
790 * different nodes */
Florin Coras8e43d042018-05-04 15:46:57 -0700791 rv = (smm->session_tx_fns[s0->session_type]) (vm, node, e0, s0,
Dave Barach68b0fb02017-02-28 15:15:56 -0500792 &n_tx_packets);
Florin Coras6792ec02017-03-13 03:49:51 -0700793 /* Out of buffers */
Florin Coras93992a92017-05-24 18:03:56 -0700794 if (PREDICT_FALSE (rv < 0))
Dave Barachacd2a6a2017-05-16 17:41:34 -0400795 {
Florin Coras93992a92017-05-24 18:03:56 -0700796 vlib_node_increment_counter (vm, node->node_index,
797 SESSION_QUEUE_ERROR_NO_BUFFER, 1);
Dave Barachacd2a6a2017-05-16 17:41:34 -0400798 continue;
799 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500800 break;
Florin Corasa5464812017-04-19 13:00:05 -0700801 case FIFO_EVENT_DISCONNECT:
Florin Corasf08f26d2018-05-10 13:20:47 -0700802 /* Make sure stream disconnects run after the pending list is drained */
Florin Coras8e43d042018-05-04 15:46:57 -0700803 s0 = session_get_from_handle (e0->session_handle);
Florin Corasb2371c22018-05-31 17:14:10 -0700804 if (!e0->postponed)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700805 {
806 e0->postponed = 1;
Florin Corasf08f26d2018-05-10 13:20:47 -0700807 vec_add1 (smm->pending_disconnects[thread_index], *e0);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700808 continue;
809 }
Florin Corasb2371c22018-05-31 17:14:10 -0700810 /* If tx queue is still not empty, wait a bit */
811 if (svm_fifo_max_dequeue (s0->server_tx_fifo)
812 && e0->postponed < 200)
813 {
814 e0->postponed += 1;
815 vec_add1 (smm->pending_disconnects[thread_index], *e0);
816 continue;
817 }
Florin Corasf08f26d2018-05-10 13:20:47 -0700818
Florin Coras2f8d8fa2018-01-26 06:36:04 -0800819 stream_session_disconnect_transport (s0);
Florin Coras6792ec02017-03-13 03:49:51 -0700820 break;
821 case FIFO_EVENT_BUILTIN_RX:
Florin Corasf08f26d2018-05-10 13:20:47 -0700822 s0 = session_event_get_session (e0, thread_index);
Florin Corasc87c91d2017-08-16 19:55:49 -0700823 if (PREDICT_FALSE (!s0))
824 continue;
Florin Coras6792ec02017-03-13 03:49:51 -0700825 svm_fifo_unset_event (s0->server_rx_fifo);
Florin Coras6792ec02017-03-13 03:49:51 -0700826 app = application_get (s0->app_index);
Florin Coras371ca502018-02-21 12:07:41 -0800827 app->cb_fns.builtin_app_rx_callback (s0);
Florin Coras6792ec02017-03-13 03:49:51 -0700828 break;
Dave Barache5f1d272017-05-10 13:34:04 -0400829 case FIFO_EVENT_RPC:
830 fp = e0->rpc_args.fp;
831 (*fp) (e0->rpc_args.arg);
832 break;
833
Dave Barach68b0fb02017-02-28 15:15:56 -0500834 default:
835 clib_warning ("unhandled event type %d", e0->event_type);
836 }
837 }
838
Dave Barachacd2a6a2017-05-16 17:41:34 -0400839 _vec_len (my_fifo_events) = 0;
Florin Corasf08f26d2018-05-10 13:20:47 -0700840 smm->free_event_vector[thread_index] = my_fifo_events;
Dave Barach68b0fb02017-02-28 15:15:56 -0500841
842 vlib_node_increment_counter (vm, session_queue_node.index,
843 SESSION_QUEUE_ERROR_TX, n_tx_packets);
844
Florin Coras6792ec02017-03-13 03:49:51 -0700845 SESSION_EVT_DBG (SESSION_EVT_DEQ_NODE, 1);
846
Dave Barach68b0fb02017-02-28 15:15:56 -0500847 return n_tx_packets;
848}
849
850/* *INDENT-OFF* */
851VLIB_REGISTER_NODE (session_queue_node) =
852{
853 .function = session_queue_node_fn,
854 .name = "session-queue",
855 .format_trace = format_session_queue_trace,
856 .type = VLIB_NODE_TYPE_INPUT,
857 .n_errors = ARRAY_LEN (session_queue_error_strings),
858 .error_strings = session_queue_error_strings,
Florin Corase04c2992017-03-01 08:17:34 -0800859 .state = VLIB_NODE_STATE_DISABLED,
Dave Barach68b0fb02017-02-28 15:15:56 -0500860};
861/* *INDENT-ON* */
862
Dave Barach2a863912017-11-28 10:11:42 -0500863static clib_error_t *
864session_queue_exit (vlib_main_t * vm)
865{
866 if (vec_len (vlib_mains) < 2)
867 return 0;
868
869 /*
870 * Shut off (especially) worker-thread session nodes.
871 * Otherwise, vpp can crash as the main thread unmaps the
872 * API segment.
873 */
874 vlib_worker_thread_barrier_sync (vm);
875 session_node_enable_disable (0 /* is_enable */ );
876 vlib_worker_thread_barrier_release (vm);
877 return 0;
878}
879
880VLIB_MAIN_LOOP_EXIT_FUNCTION (session_queue_exit);
881
Florin Corasfd542f12018-05-16 19:28:24 -0700882static uword
883session_queue_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
884 vlib_frame_t * f)
885{
886 f64 now, timeout = 1.0;
887 uword *event_data = 0;
888 uword event_type;
889
890 while (1)
891 {
892 vlib_process_wait_for_event_or_clock (vm, timeout);
893 now = vlib_time_now (vm);
894 event_type = vlib_process_get_events (vm, (uword **) & event_data);
895
896 switch (event_type)
897 {
898 case SESSION_Q_PROCESS_FLUSH_FRAMES:
899 /* Flush the frames by updating all transports times */
900 transport_update_time (now, 0);
901 break;
902 case SESSION_Q_PROCESS_STOP:
903 timeout = 100000.0;
904 break;
905 case ~0:
906 /* Timed out. Update time for all transports to trigger all
907 * outstanding retransmits. */
908 transport_update_time (now, 0);
909 break;
910 }
911 vec_reset_length (event_data);
912 }
913 return 0;
914}
915
916/* *INDENT-OFF* */
917VLIB_REGISTER_NODE (session_queue_process_node) =
918{
919 .function = session_queue_process,
920 .type = VLIB_NODE_TYPE_PROCESS,
921 .name = "session-queue-process",
922 .state = VLIB_NODE_STATE_DISABLED,
923};
924/* *INDENT-ON* */
925
926
Dave Barach68b0fb02017-02-28 15:15:56 -0500927/*
928 * fd.io coding-style-patch-verification: ON
929 *
930 * Local Variables:
931 * eval: (c-set-style "gnu")
932 * End:
933 */