blob: 78116178d2b03545414a68a8132cfe294e445f42 [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 <vnet/tcp/tcp.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050020#include <vppinfra/elog.h>
Florin Coras6792ec02017-03-13 03:49:51 -070021#include <vnet/session/application.h>
Florin Corase69f4952017-03-07 10:06:24 -080022#include <vnet/session/session_debug.h>
Florin Coras6792ec02017-03-13 03:49:51 -070023#include <vlibmemory/unix_shared_memory_queue.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050024
25vlib_node_registration_t session_queue_node;
26
27typedef struct
28{
29 u32 session_index;
30 u32 server_thread_index;
31} session_queue_trace_t;
32
33/* packet trace format function */
34static u8 *
35format_session_queue_trace (u8 * s, va_list * args)
36{
37 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
38 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
39 session_queue_trace_t *t = va_arg (*args, session_queue_trace_t *);
40
41 s = format (s, "SESSION_QUEUE: session index %d, server thread index %d",
42 t->session_index, t->server_thread_index);
43 return s;
44}
45
46vlib_node_registration_t session_queue_node;
47
Florin Coras6792ec02017-03-13 03:49:51 -070048#define foreach_session_queue_error \
49_(TX, "Packets transmitted") \
Florin Coras93992a92017-05-24 18:03:56 -070050_(TIMER, "Timer events") \
51_(NO_BUFFER, "Out of buffers")
Dave Barach68b0fb02017-02-28 15:15:56 -050052
53typedef enum
54{
55#define _(sym,str) SESSION_QUEUE_ERROR_##sym,
56 foreach_session_queue_error
57#undef _
58 SESSION_QUEUE_N_ERROR,
59} session_queue_error_t;
60
61static char *session_queue_error_strings[] = {
62#define _(sym,string) string,
63 foreach_session_queue_error
64#undef _
65};
66
67static u32 session_type_to_next[] = {
68 SESSION_QUEUE_NEXT_TCP_IP4_OUTPUT,
69 SESSION_QUEUE_NEXT_IP4_LOOKUP,
70 SESSION_QUEUE_NEXT_TCP_IP6_OUTPUT,
71 SESSION_QUEUE_NEXT_IP6_LOOKUP,
72};
73
Florin Corasf6d68ed2017-05-07 19:12:02 -070074always_inline void
75session_tx_fifo_chain_tail (session_manager_main_t * smm, vlib_main_t * vm,
76 u8 thread_index, svm_fifo_t * fifo,
77 vlib_buffer_t * b0, u32 bi0, u8 n_bufs_per_seg,
Florin Corasb2215d62017-08-01 16:56:58 -070078 u32 left_from_seg, u32 * left_to_snd0,
Florin Coras1f152cd2017-08-18 19:28:03 -070079 u16 * n_bufs, u32 * tx_offset, u16 deq_per_buf,
Florin Corasb2215d62017-08-01 16:56:58 -070080 u8 peek_data)
Florin Corasf6d68ed2017-05-07 19:12:02 -070081{
82 vlib_buffer_t *chain_b0, *prev_b0;
Florin Corasb2215d62017-08-01 16:56:58 -070083 u32 chain_bi0, to_deq;
Florin Corasf6d68ed2017-05-07 19:12:02 -070084 u16 len_to_deq0, n_bytes_read;
85 u8 *data0, j;
86
Florin Corasb2215d62017-08-01 16:56:58 -070087 b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
88 b0->total_length_not_including_first_buffer = 0;
89
Florin Corasf6d68ed2017-05-07 19:12:02 -070090 chain_bi0 = bi0;
91 chain_b0 = b0;
Florin Corasb2215d62017-08-01 16:56:58 -070092 to_deq = left_from_seg;
Florin Corasf6d68ed2017-05-07 19:12:02 -070093 for (j = 1; j < n_bufs_per_seg; j++)
94 {
95 prev_b0 = chain_b0;
Florin Corasb2215d62017-08-01 16:56:58 -070096 len_to_deq0 = clib_min (to_deq, deq_per_buf);
Florin Corasf6d68ed2017-05-07 19:12:02 -070097
98 *n_bufs -= 1;
99 chain_bi0 = smm->tx_buffers[thread_index][*n_bufs];
100 _vec_len (smm->tx_buffers[thread_index]) = *n_bufs;
101
102 chain_b0 = vlib_get_buffer (vm, chain_bi0);
103 chain_b0->current_data = 0;
104 data0 = vlib_buffer_get_current (chain_b0);
105 if (peek_data)
106 {
Florin Coras1f152cd2017-08-18 19:28:03 -0700107 n_bytes_read = svm_fifo_peek (fifo, *tx_offset, len_to_deq0, data0);
108 *tx_offset += n_bytes_read;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700109 }
110 else
111 {
112 n_bytes_read = svm_fifo_dequeue_nowait (fifo, len_to_deq0, data0);
113 }
114 ASSERT (n_bytes_read == len_to_deq0);
115 chain_b0->current_length = n_bytes_read;
116 b0->total_length_not_including_first_buffer += chain_b0->current_length;
117
118 /* update previous buffer */
119 prev_b0->next_buffer = chain_bi0;
120 prev_b0->flags |= VLIB_BUFFER_NEXT_PRESENT;
121
122 /* update current buffer */
123 chain_b0->next_buffer = 0;
124
Florin Corasb2215d62017-08-01 16:56:58 -0700125 to_deq -= n_bytes_read;
126 if (to_deq == 0)
Florin Corasf6d68ed2017-05-07 19:12:02 -0700127 break;
128 }
Florin Coras1f152cd2017-08-18 19:28:03 -0700129 ASSERT (to_deq == 0
130 && b0->total_length_not_including_first_buffer == left_from_seg);
Florin Corasb2215d62017-08-01 16:56:58 -0700131 *left_to_snd0 -= left_from_seg;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700132}
133
Dave Barach68b0fb02017-02-28 15:15:56 -0500134always_inline int
Florin Corasd79b41e2017-03-04 05:37:52 -0800135session_tx_fifo_read_and_snd_i (vlib_main_t * vm, vlib_node_runtime_t * node,
136 session_manager_main_t * smm,
137 session_fifo_event_t * e0,
138 stream_session_t * s0, u32 thread_index,
139 int *n_tx_packets, u8 peek_data)
Dave Barach68b0fb02017-02-28 15:15:56 -0500140{
141 u32 n_trace = vlib_get_trace_count (vm, node);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700142 u32 left_to_snd0, max_len_to_snd0, len_to_deq0, snd_space0;
Florin Corase87216f2017-08-17 16:59:22 -0700143 u32 n_bufs_per_evt, n_frames_per_evt, n_bufs_per_frame;
Dave Barach68b0fb02017-02-28 15:15:56 -0500144 transport_connection_t *tc0;
145 transport_proto_vft_t *transport_vft;
146 u32 next_index, next0, *to_next, n_left_to_next, bi0;
147 vlib_buffer_t *b0;
Florin Coras1f152cd2017-08-18 19:28:03 -0700148 u32 tx_offset = 0, max_dequeue0, n_bytes_per_seg, left_for_seg;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700149 u16 snd_mss0, n_bufs_per_seg, n_bufs;
Dave Barach68b0fb02017-02-28 15:15:56 -0500150 u8 *data0;
Florin Coras6792ec02017-03-13 03:49:51 -0700151 int i, n_bytes_read;
Florin Corase87216f2017-08-17 16:59:22 -0700152 u32 n_bytes_per_buf, deq_per_buf, deq_per_first_buf;
Florin Coras93992a92017-05-24 18:03:56 -0700153 u32 buffers_allocated, buffers_allocated_this_call;
Dave Barach68b0fb02017-02-28 15:15:56 -0500154
155 next_index = next0 = session_type_to_next[s0->session_type];
156
Florin Coras3cbc04b2017-10-02 00:18:51 -0700157 transport_vft = transport_protocol_get_vft (s0->session_type);
Dave Barach68b0fb02017-02-28 15:15:56 -0500158 tc0 = transport_vft->get_connection (s0->connection_index, thread_index);
159
160 /* Make sure we have space to send and there's something to dequeue */
Dave Barach68b0fb02017-02-28 15:15:56 -0500161 snd_mss0 = transport_vft->send_mss (tc0);
Florin Corasc8343412017-05-04 14:25:50 -0700162 snd_space0 = transport_vft->send_space (tc0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500163
Florin Corase04c2992017-03-01 08:17:34 -0800164 /* Can't make any progress */
Florin Coras6792ec02017-03-13 03:49:51 -0700165 if (snd_space0 == 0 || snd_mss0 == 0)
Florin Corase04c2992017-03-01 08:17:34 -0800166 {
Dave Barachacd2a6a2017-05-16 17:41:34 -0400167 vec_add1 (smm->pending_event_vector[thread_index], *e0);
Florin Corase04c2992017-03-01 08:17:34 -0800168 return 0;
169 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500170
Florin Coras6a9b68b2017-11-21 04:20:42 -0800171 /* Allow enqueuing of a new event */
172 svm_fifo_unset_event (s0->server_tx_fifo);
173
Florin Coras9d063042017-09-14 03:08:00 -0400174 /* Check how much we can pull. */
175 max_dequeue0 = svm_fifo_max_dequeue (s0->server_tx_fifo);
176
Dave Barach68b0fb02017-02-28 15:15:56 -0500177 if (peek_data)
178 {
Florin Coras9d063042017-09-14 03:08:00 -0400179 /* Offset in rx fifo from where to peek data */
Florin Coras1f152cd2017-08-18 19:28:03 -0700180 tx_offset = transport_vft->tx_fifo_offset (tc0);
Florin Coras9d063042017-09-14 03:08:00 -0400181 if (PREDICT_FALSE (tx_offset >= max_dequeue0))
182 max_dequeue0 = 0;
183 else
184 max_dequeue0 -= tx_offset;
Dave Barach68b0fb02017-02-28 15:15:56 -0500185 }
186
Florin Coras6792ec02017-03-13 03:49:51 -0700187 /* Nothing to read return */
188 if (max_dequeue0 == 0)
Florin Coras6a9b68b2017-11-21 04:20:42 -0800189 return 0;
Florin Coras6792ec02017-03-13 03:49:51 -0700190
191 /* Ensure we're not writing more than transport window allows */
Florin Coras3e350af2017-03-30 02:54:28 -0700192 if (max_dequeue0 < snd_space0)
193 {
194 /* Constrained by tx queue. Try to send only fully formed segments */
195 max_len_to_snd0 = (max_dequeue0 > snd_mss0) ?
196 max_dequeue0 - max_dequeue0 % snd_mss0 : max_dequeue0;
197 /* TODO Nagle ? */
198 }
199 else
200 {
Florin Coras1f152cd2017-08-18 19:28:03 -0700201 /* Expectation is that snd_space0 is already a multiple of snd_mss */
Florin Coras3e350af2017-03-30 02:54:28 -0700202 max_len_to_snd0 = snd_space0;
203 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500204
Florin Coras93992a92017-05-24 18:03:56 -0700205 n_bytes_per_buf = vlib_buffer_free_list_buffer_size
206 (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
Florin Corase87216f2017-08-17 16:59:22 -0700207 ASSERT (n_bytes_per_buf > MAX_HDRS_LEN);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700208 n_bytes_per_seg = MAX_HDRS_LEN + snd_mss0;
209 n_bufs_per_seg = ceil ((double) n_bytes_per_seg / n_bytes_per_buf);
Florin Corase87216f2017-08-17 16:59:22 -0700210 n_bufs_per_evt = ceil ((double) max_len_to_snd0 / n_bytes_per_seg);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700211 n_frames_per_evt = ceil ((double) n_bufs_per_evt / VLIB_FRAME_SIZE);
Florin Corase87216f2017-08-17 16:59:22 -0700212 n_bufs_per_frame = n_bufs_per_seg * VLIB_FRAME_SIZE;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700213
214 deq_per_buf = clib_min (snd_mss0, n_bytes_per_buf);
Florin Corase87216f2017-08-17 16:59:22 -0700215 deq_per_first_buf = clib_min (snd_mss0, n_bytes_per_buf - MAX_HDRS_LEN);
Dave Barach68b0fb02017-02-28 15:15:56 -0500216
217 n_bufs = vec_len (smm->tx_buffers[thread_index]);
218 left_to_snd0 = max_len_to_snd0;
219 for (i = 0; i < n_frames_per_evt; i++)
220 {
221 /* Make sure we have at least one full frame of buffers ready */
Florin Corase87216f2017-08-17 16:59:22 -0700222 if (PREDICT_FALSE (n_bufs < n_bufs_per_frame))
Dave Barach68b0fb02017-02-28 15:15:56 -0500223 {
224 vec_validate (smm->tx_buffers[thread_index],
Florin Corase87216f2017-08-17 16:59:22 -0700225 n_bufs + n_bufs_per_frame - 1);
Florin Coras93992a92017-05-24 18:03:56 -0700226 buffers_allocated = 0;
227 do
Dave Barach68b0fb02017-02-28 15:15:56 -0500228 {
Florin Corase87216f2017-08-17 16:59:22 -0700229 buffers_allocated_this_call = vlib_buffer_alloc (vm,
230 &smm->tx_buffers
231 [thread_index]
232 [n_bufs +
233 buffers_allocated],
234 n_bufs_per_frame
235 -
236 buffers_allocated);
Florin Coras93992a92017-05-24 18:03:56 -0700237 buffers_allocated += buffers_allocated_this_call;
Dave Barach68b0fb02017-02-28 15:15:56 -0500238 }
Florin Coras93992a92017-05-24 18:03:56 -0700239 while (buffers_allocated_this_call > 0
Florin Corase87216f2017-08-17 16:59:22 -0700240 && ((buffers_allocated + n_bufs < n_bufs_per_frame)));
Florin Coras93992a92017-05-24 18:03:56 -0700241
242 n_bufs += buffers_allocated;
Dave Barach68b0fb02017-02-28 15:15:56 -0500243 _vec_len (smm->tx_buffers[thread_index]) = n_bufs;
Florin Coras93992a92017-05-24 18:03:56 -0700244
Florin Corase87216f2017-08-17 16:59:22 -0700245 if (PREDICT_FALSE (n_bufs < n_bufs_per_frame))
Florin Coras93992a92017-05-24 18:03:56 -0700246 {
247 vec_add1 (smm->pending_event_vector[thread_index], *e0);
248 return -1;
249 }
Florin Corase87216f2017-08-17 16:59:22 -0700250 ASSERT (n_bufs >= n_bufs_per_frame);
Dave Barach68b0fb02017-02-28 15:15:56 -0500251 }
252
253 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Florin Corase87216f2017-08-17 16:59:22 -0700254 while (left_to_snd0 && n_left_to_next)
Dave Barach68b0fb02017-02-28 15:15:56 -0500255 {
Florin Corasf6d68ed2017-05-07 19:12:02 -0700256 /*
257 * Handle first buffer in chain separately
258 */
259
Dave Barach68b0fb02017-02-28 15:15:56 -0500260 /* Get free buffer */
Florin Coras93992a92017-05-24 18:03:56 -0700261 ASSERT (n_bufs >= 1);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700262 bi0 = smm->tx_buffers[thread_index][--n_bufs];
Dave Barach68b0fb02017-02-28 15:15:56 -0500263 _vec_len (smm->tx_buffers[thread_index]) = n_bufs;
264
Florin Corasf6359c82017-06-19 12:26:09 -0400265 /* usual speculation, or the enqueue_x1 macro will barf */
266 to_next[0] = bi0;
267 to_next += 1;
268 n_left_to_next -= 1;
269
Dave Barach68b0fb02017-02-28 15:15:56 -0500270 b0 = vlib_get_buffer (vm, bi0);
271 b0->error = 0;
Florin Coras4eeeaaf2017-09-05 14:03:37 -0400272 b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
Dave Barach68b0fb02017-02-28 15:15:56 -0500273 b0->current_data = 0;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700274 b0->total_length_not_including_first_buffer = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500275
Florin Corase87216f2017-08-17 16:59:22 -0700276 len_to_deq0 = clib_min (left_to_snd0, deq_per_first_buf);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700277 data0 = vlib_buffer_make_headroom (b0, MAX_HDRS_LEN);
278 if (peek_data)
279 {
Florin Coras1f152cd2017-08-18 19:28:03 -0700280 n_bytes_read = svm_fifo_peek (s0->server_tx_fifo, tx_offset,
Florin Corasf6d68ed2017-05-07 19:12:02 -0700281 len_to_deq0, data0);
Florin Coras9d063042017-09-14 03:08:00 -0400282 if (n_bytes_read <= 0)
283 goto dequeue_fail;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700284 /* Keep track of progress locally, transport is also supposed to
285 * increment it independently when pushing the header */
Florin Coras1f152cd2017-08-18 19:28:03 -0700286 tx_offset += n_bytes_read;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700287 }
288 else
289 {
290 n_bytes_read = svm_fifo_dequeue_nowait (s0->server_tx_fifo,
291 len_to_deq0, data0);
Florin Coras9d063042017-09-14 03:08:00 -0400292 if (n_bytes_read <= 0)
293 goto dequeue_fail;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700294 }
295
Florin Corasf6d68ed2017-05-07 19:12:02 -0700296 b0->current_length = n_bytes_read;
297
298 left_to_snd0 -= n_bytes_read;
299 *n_tx_packets = *n_tx_packets + 1;
300
301 /*
302 * Fill in the remaining buffers in the chain, if any
303 */
Florin Corase87216f2017-08-17 16:59:22 -0700304 if (PREDICT_FALSE (n_bufs_per_seg > 1 && left_to_snd0))
Florin Corasb2215d62017-08-01 16:56:58 -0700305 {
Florin Corasb2215d62017-08-01 16:56:58 -0700306 left_for_seg = clib_min (snd_mss0 - n_bytes_read, left_to_snd0);
307 session_tx_fifo_chain_tail (smm, vm, thread_index,
308 s0->server_tx_fifo, b0, bi0,
309 n_bufs_per_seg, left_for_seg,
Florin Coras1f152cd2017-08-18 19:28:03 -0700310 &left_to_snd0, &n_bufs, &tx_offset,
Florin Corasb2215d62017-08-01 16:56:58 -0700311 deq_per_buf, peek_data);
312 }
Florin Corasf6d68ed2017-05-07 19:12:02 -0700313
314 /* Ask transport to push header after current_length and
315 * total_length_not_including_first_buffer are updated */
316 transport_vft->push_header (tc0, b0);
317
318 /* *INDENT-OFF* */
319 SESSION_EVT_DBG(SESSION_EVT_DEQ, s0, ({
Florin Corasec44e342017-10-16 20:47:56 -0700320 ed->data[0] = e0->event_type;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700321 ed->data[1] = max_dequeue0;
322 ed->data[2] = len_to_deq0;
323 ed->data[3] = left_to_snd0;
324 }));
325 /* *INDENT-ON* */
326
Dave Barach68b0fb02017-02-28 15:15:56 -0500327 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
Florin Coras4eeeaaf2017-09-05 14:03:37 -0400328 if (VLIB_BUFFER_TRACE_TRAJECTORY)
329 b0->pre_data[1] = 3;
330
Dave Barach68b0fb02017-02-28 15:15:56 -0500331 if (PREDICT_FALSE (n_trace > 0))
332 {
333 session_queue_trace_t *t0;
334 vlib_trace_buffer (vm, node, next_index, b0,
335 1 /* follow_chain */ );
336 vlib_set_trace_count (vm, node, --n_trace);
337 t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
338 t0->session_index = s0->session_index;
339 t0->server_thread_index = s0->thread_index;
340 }
341
Dave Barach68b0fb02017-02-28 15:15:56 -0500342 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
343 to_next, n_left_to_next,
344 bi0, next0);
345 }
346 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
347 }
348
Florin Coras6792ec02017-03-13 03:49:51 -0700349 /* If we couldn't dequeue all bytes mark as partially read */
350 if (max_len_to_snd0 < max_dequeue0)
Dave Barach68b0fb02017-02-28 15:15:56 -0500351 {
Florin Coras6792ec02017-03-13 03:49:51 -0700352 /* If we don't already have new event */
353 if (svm_fifo_set_event (s0->server_tx_fifo))
354 {
Dave Barachacd2a6a2017-05-16 17:41:34 -0400355 vec_add1 (smm->pending_event_vector[thread_index], *e0);
Florin Coras6792ec02017-03-13 03:49:51 -0700356 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500357 }
358 return 0;
359
360dequeue_fail:
Florin Coras6792ec02017-03-13 03:49:51 -0700361 /*
362 * Can't read from fifo. If we don't already have an event, save as partially
363 * read, return buff to free list and return
364 */
365 clib_warning ("dequeue fail");
Dave Barach68b0fb02017-02-28 15:15:56 -0500366
Florin Coras6792ec02017-03-13 03:49:51 -0700367 if (svm_fifo_set_event (s0->server_tx_fifo))
368 {
Dave Barachacd2a6a2017-05-16 17:41:34 -0400369 vec_add1 (smm->pending_event_vector[thread_index], *e0);
Florin Coras6792ec02017-03-13 03:49:51 -0700370 }
371 vlib_put_next_frame (vm, node, next_index, n_left_to_next + 1);
Dave Barach68b0fb02017-02-28 15:15:56 -0500372 _vec_len (smm->tx_buffers[thread_index]) += 1;
373
Dave Barach68b0fb02017-02-28 15:15:56 -0500374 return 0;
375}
376
377int
Florin Corasd79b41e2017-03-04 05:37:52 -0800378session_tx_fifo_peek_and_snd (vlib_main_t * vm, vlib_node_runtime_t * node,
379 session_manager_main_t * smm,
380 session_fifo_event_t * e0,
381 stream_session_t * s0, u32 thread_index,
382 int *n_tx_pkts)
Dave Barach68b0fb02017-02-28 15:15:56 -0500383{
Florin Corasd79b41e2017-03-04 05:37:52 -0800384 return session_tx_fifo_read_and_snd_i (vm, node, smm, e0, s0, thread_index,
385 n_tx_pkts, 1);
Dave Barach68b0fb02017-02-28 15:15:56 -0500386}
387
388int
Florin Corasd79b41e2017-03-04 05:37:52 -0800389session_tx_fifo_dequeue_and_snd (vlib_main_t * vm, vlib_node_runtime_t * node,
390 session_manager_main_t * smm,
391 session_fifo_event_t * e0,
392 stream_session_t * s0, u32 thread_index,
393 int *n_tx_pkts)
Dave Barach68b0fb02017-02-28 15:15:56 -0500394{
Florin Corasd79b41e2017-03-04 05:37:52 -0800395 return session_tx_fifo_read_and_snd_i (vm, node, smm, e0, s0, thread_index,
396 n_tx_pkts, 0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500397}
398
Dave Barach2c25a622017-06-26 11:35:07 -0400399always_inline stream_session_t *
400session_event_get_session (session_fifo_event_t * e, u8 thread_index)
Florin Corasa5464812017-04-19 13:00:05 -0700401{
Florin Coras3cbc04b2017-10-02 00:18:51 -0700402 return session_get_if_valid (e->fifo->master_session_index, thread_index);
Florin Corasa5464812017-04-19 13:00:05 -0700403}
404
Dave Barachacd2a6a2017-05-16 17:41:34 -0400405void
406dump_thread_0_event_queue (void)
407{
408 session_manager_main_t *smm = vnet_get_session_manager_main ();
409 vlib_main_t *vm = &vlib_global_main;
410 u32 my_thread_index = vm->thread_index;
411 session_fifo_event_t _e, *e = &_e;
412 stream_session_t *s0;
413 int i, index;
414 i8 *headp;
415
416 unix_shared_memory_queue_t *q;
417 q = smm->vpp_event_queues[my_thread_index];
418
419 index = q->head;
420
421 for (i = 0; i < q->cursize; i++)
422 {
423 headp = (i8 *) (&q->data[0] + q->elsize * index);
424 clib_memcpy (e, headp, q->elsize);
425
426 switch (e->event_type)
427 {
428 case FIFO_EVENT_APP_TX:
429 s0 = session_event_get_session (e, my_thread_index);
430 fformat (stdout, "[%04d] TX session %d\n", i, s0->session_index);
431 break;
432
433 case FIFO_EVENT_DISCONNECT:
Florin Corascea194d2017-10-02 00:18:51 -0700434 s0 = session_get_from_handle (e->session_handle);
Dave Barachacd2a6a2017-05-16 17:41:34 -0400435 fformat (stdout, "[%04d] disconnect session %d\n", i,
436 s0->session_index);
437 break;
438
439 case FIFO_EVENT_BUILTIN_RX:
440 s0 = session_event_get_session (e, my_thread_index);
441 fformat (stdout, "[%04d] builtin_rx %d\n", i, s0->session_index);
442 break;
443
444 case FIFO_EVENT_RPC:
445 fformat (stdout, "[%04d] RPC call %llx with %llx\n",
446 i, (u64) (e->rpc_args.fp), (u64) (e->rpc_args.arg));
447 break;
448
449 default:
450 fformat (stdout, "[%04d] unhandled event type %d\n",
451 i, e->event_type);
452 break;
453 }
454
455 index++;
456
457 if (index == q->maxsize)
458 index = 0;
459 }
460}
461
Florin Coras6534b7a2017-07-18 05:38:03 -0400462static u8
463session_node_cmp_event (session_fifo_event_t * e, svm_fifo_t * f)
464{
465 stream_session_t *s;
466 switch (e->event_type)
467 {
468 case FIFO_EVENT_APP_RX:
469 case FIFO_EVENT_APP_TX:
470 case FIFO_EVENT_BUILTIN_RX:
471 if (e->fifo == f)
472 return 1;
473 break;
474 case FIFO_EVENT_DISCONNECT:
475 break;
476 case FIFO_EVENT_RPC:
Florin Corascea194d2017-10-02 00:18:51 -0700477 s = session_get_from_handle (e->session_handle);
Florin Coras6534b7a2017-07-18 05:38:03 -0400478 if (!s)
479 {
480 clib_warning ("session has event but doesn't exist!");
481 break;
482 }
483 if (s->server_rx_fifo == f || s->server_tx_fifo == f)
484 return 1;
485 break;
486 default:
487 break;
488 }
489 return 0;
490}
491
492u8
493session_node_lookup_fifo_event (svm_fifo_t * f, session_fifo_event_t * e)
494{
495 session_manager_main_t *smm = vnet_get_session_manager_main ();
496 unix_shared_memory_queue_t *q;
497 session_fifo_event_t *pending_event_vector, *evt;
498 int i, index, found = 0;
499 i8 *headp;
500 u8 thread_index;
501
502 ASSERT (e);
503 thread_index = f->master_thread_index;
504 /*
505 * Search evt queue
506 */
507 q = smm->vpp_event_queues[thread_index];
508 index = q->head;
509 for (i = 0; i < q->cursize; i++)
510 {
511 headp = (i8 *) (&q->data[0] + q->elsize * index);
512 clib_memcpy (e, headp, q->elsize);
513 found = session_node_cmp_event (e, f);
514 if (found)
515 break;
516 if (++index == q->maxsize)
517 index = 0;
518 }
519 /*
520 * Search pending events vector
521 */
522 pending_event_vector = smm->pending_event_vector[thread_index];
523 vec_foreach (evt, pending_event_vector)
524 {
525 found = session_node_cmp_event (evt, f);
526 if (found)
527 {
528 clib_memcpy (e, evt, sizeof (*evt));
529 break;
530 }
531 }
532 return found;
533}
534
Dave Barach68b0fb02017-02-28 15:15:56 -0500535static uword
536session_queue_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
537 vlib_frame_t * frame)
538{
539 session_manager_main_t *smm = vnet_get_session_manager_main ();
Florin Coras3cbc04b2017-10-02 00:18:51 -0700540 session_fifo_event_t *my_pending_event_vector, *pending_disconnects, *e;
Dave Barachacd2a6a2017-05-16 17:41:34 -0400541 session_fifo_event_t *my_fifo_events;
Florin Corase04c2992017-03-01 08:17:34 -0800542 u32 n_to_dequeue, n_events;
Dave Barach68b0fb02017-02-28 15:15:56 -0500543 unix_shared_memory_queue_t *q;
Florin Coras6792ec02017-03-13 03:49:51 -0700544 application_t *app;
Dave Barach68b0fb02017-02-28 15:15:56 -0500545 int n_tx_packets = 0;
Damjan Marion586afd72017-04-05 19:18:20 +0200546 u32 my_thread_index = vm->thread_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500547 int i, rv;
Florin Coras3e350af2017-03-30 02:54:28 -0700548 f64 now = vlib_time_now (vm);
Dave Barache5f1d272017-05-10 13:34:04 -0400549 void (*fp) (void *);
Florin Coras3e350af2017-03-30 02:54:28 -0700550
551 SESSION_EVT_DBG (SESSION_EVT_POLL_GAP_TRACK, smm, my_thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500552
553 /*
554 * Update TCP time
555 */
Florin Coras3e350af2017-03-30 02:54:28 -0700556 tcp_update_time (now, my_thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500557
558 /*
559 * Get vpp queue events
560 */
561 q = smm->vpp_event_queues[my_thread_index];
562 if (PREDICT_FALSE (q == 0))
563 return 0;
564
Dave Barachacd2a6a2017-05-16 17:41:34 -0400565 my_fifo_events = smm->free_event_vector[my_thread_index];
566
Dave Barach68b0fb02017-02-28 15:15:56 -0500567 /* min number of events we can dequeue without blocking */
568 n_to_dequeue = q->cursize;
Dave Barachacd2a6a2017-05-16 17:41:34 -0400569 my_pending_event_vector = smm->pending_event_vector[my_thread_index];
Florin Coras3cbc04b2017-10-02 00:18:51 -0700570 pending_disconnects = smm->pending_disconnects[my_thread_index];
Dave Barach68b0fb02017-02-28 15:15:56 -0500571
Florin Coras3cbc04b2017-10-02 00:18:51 -0700572 if (!n_to_dequeue && !vec_len (my_pending_event_vector)
573 && !vec_len (pending_disconnects))
Florin Corase04c2992017-03-01 08:17:34 -0800574 return 0;
575
Florin Coras6792ec02017-03-13 03:49:51 -0700576 SESSION_EVT_DBG (SESSION_EVT_DEQ_NODE, 0);
577
Florin Corase04c2992017-03-01 08:17:34 -0800578 /*
579 * If we didn't manage to process previous events try going
Dave Barach68b0fb02017-02-28 15:15:56 -0500580 * over them again without dequeuing new ones.
Florin Corase04c2992017-03-01 08:17:34 -0800581 */
582 /* XXX: Block senders to sessions that can't keep up */
Dave Barachacd2a6a2017-05-16 17:41:34 -0400583 if (0 && vec_len (my_pending_event_vector) >= 100)
Florin Coras6792ec02017-03-13 03:49:51 -0700584 {
585 clib_warning ("too many fifo events unsolved");
586 goto skip_dequeue;
587 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500588
589 /* See you in the next life, don't be late */
590 if (pthread_mutex_trylock (&q->mutex))
591 return 0;
592
593 for (i = 0; i < n_to_dequeue; i++)
594 {
595 vec_add2 (my_fifo_events, e, 1);
596 unix_shared_memory_queue_sub_raw (q, (u8 *) e);
597 }
598
599 /* The other side of the connection is not polling */
600 if (q->cursize < (q->maxsize / 8))
601 (void) pthread_cond_broadcast (&q->condvar);
602 pthread_mutex_unlock (&q->mutex);
603
Dave Barachacd2a6a2017-05-16 17:41:34 -0400604 vec_append (my_fifo_events, my_pending_event_vector);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700605 vec_append (my_fifo_events, smm->pending_disconnects[my_thread_index]);
Dave Barachacd2a6a2017-05-16 17:41:34 -0400606
607 _vec_len (my_pending_event_vector) = 0;
608 smm->pending_event_vector[my_thread_index] = my_pending_event_vector;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700609 _vec_len (smm->pending_disconnects[my_thread_index]) = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500610
611skip_dequeue:
Florin Corase04c2992017-03-01 08:17:34 -0800612 n_events = vec_len (my_fifo_events);
613 for (i = 0; i < n_events; i++)
Dave Barach68b0fb02017-02-28 15:15:56 -0500614 {
Florin Corasa5464812017-04-19 13:00:05 -0700615 stream_session_t *s0; /* $$$ prefetch 1 ahead maybe */
Dave Barach68b0fb02017-02-28 15:15:56 -0500616 session_fifo_event_t *e0;
617
618 e0 = &my_fifo_events[i];
Dave Barach68b0fb02017-02-28 15:15:56 -0500619
620 switch (e0->event_type)
621 {
Florin Corasa5464812017-04-19 13:00:05 -0700622 case FIFO_EVENT_APP_TX:
623 s0 = session_event_get_session (e0, my_thread_index);
624
Florin Coras9d063042017-09-14 03:08:00 -0400625 if (PREDICT_FALSE (!s0))
Florin Corasa5464812017-04-19 13:00:05 -0700626 {
627 clib_warning ("It's dead, Jim!");
628 continue;
629 }
Florin Corasb2215d62017-08-01 16:56:58 -0700630 /* Can retransmit for closed sessions but can't do anything if
631 * session is not ready or closed */
632 if (PREDICT_FALSE (s0->session_state < SESSION_STATE_READY))
Florin Corasa5464812017-04-19 13:00:05 -0700633 continue;
Dave Barach68b0fb02017-02-28 15:15:56 -0500634 /* Spray packets in per session type frames, since they go to
635 * different nodes */
Florin Corase69f4952017-03-07 10:06:24 -0800636 rv = (smm->session_tx_fns[s0->session_type]) (vm, node, smm, e0, s0,
Dave Barach68b0fb02017-02-28 15:15:56 -0500637 my_thread_index,
638 &n_tx_packets);
Florin Coras6792ec02017-03-13 03:49:51 -0700639 /* Out of buffers */
Florin Coras93992a92017-05-24 18:03:56 -0700640 if (PREDICT_FALSE (rv < 0))
Dave Barachacd2a6a2017-05-16 17:41:34 -0400641 {
Florin Coras93992a92017-05-24 18:03:56 -0700642 vlib_node_increment_counter (vm, node->node_index,
643 SESSION_QUEUE_ERROR_NO_BUFFER, 1);
Dave Barachacd2a6a2017-05-16 17:41:34 -0400644 continue;
645 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500646 break;
Florin Corasa5464812017-04-19 13:00:05 -0700647 case FIFO_EVENT_DISCONNECT:
Florin Coras3cbc04b2017-10-02 00:18:51 -0700648 /* Make sure disconnects run after the pending list is drained */
649 if (!e0->postponed)
650 {
651 e0->postponed = 1;
652 vec_add1 (smm->pending_disconnects[my_thread_index], *e0);
653 continue;
654 }
Florin Corascea194d2017-10-02 00:18:51 -0700655 s0 = session_get_from_handle (e0->session_handle);
Florin Coras6792ec02017-03-13 03:49:51 -0700656 stream_session_disconnect (s0);
657 break;
658 case FIFO_EVENT_BUILTIN_RX:
Florin Corasa5464812017-04-19 13:00:05 -0700659 s0 = session_event_get_session (e0, my_thread_index);
Florin Corasc87c91d2017-08-16 19:55:49 -0700660 if (PREDICT_FALSE (!s0))
661 continue;
Florin Coras6792ec02017-03-13 03:49:51 -0700662 svm_fifo_unset_event (s0->server_rx_fifo);
Florin Coras6792ec02017-03-13 03:49:51 -0700663 app = application_get (s0->app_index);
664 app->cb_fns.builtin_server_rx_callback (s0);
665 break;
Dave Barache5f1d272017-05-10 13:34:04 -0400666 case FIFO_EVENT_RPC:
667 fp = e0->rpc_args.fp;
668 (*fp) (e0->rpc_args.arg);
669 break;
670
Dave Barach68b0fb02017-02-28 15:15:56 -0500671 default:
672 clib_warning ("unhandled event type %d", e0->event_type);
673 }
674 }
675
Dave Barachacd2a6a2017-05-16 17:41:34 -0400676 _vec_len (my_fifo_events) = 0;
677 smm->free_event_vector[my_thread_index] = my_fifo_events;
Dave Barach68b0fb02017-02-28 15:15:56 -0500678
679 vlib_node_increment_counter (vm, session_queue_node.index,
680 SESSION_QUEUE_ERROR_TX, n_tx_packets);
681
Florin Coras6792ec02017-03-13 03:49:51 -0700682 SESSION_EVT_DBG (SESSION_EVT_DEQ_NODE, 1);
683
Dave Barach68b0fb02017-02-28 15:15:56 -0500684 return n_tx_packets;
685}
686
687/* *INDENT-OFF* */
688VLIB_REGISTER_NODE (session_queue_node) =
689{
690 .function = session_queue_node_fn,
691 .name = "session-queue",
692 .format_trace = format_session_queue_trace,
693 .type = VLIB_NODE_TYPE_INPUT,
694 .n_errors = ARRAY_LEN (session_queue_error_strings),
695 .error_strings = session_queue_error_strings,
696 .n_next_nodes = SESSION_QUEUE_N_NEXT,
Florin Corase04c2992017-03-01 08:17:34 -0800697 .state = VLIB_NODE_STATE_DISABLED,
Dave Barach68b0fb02017-02-28 15:15:56 -0500698 .next_nodes =
699 {
700 [SESSION_QUEUE_NEXT_DROP] = "error-drop",
701 [SESSION_QUEUE_NEXT_IP4_LOOKUP] = "ip4-lookup",
702 [SESSION_QUEUE_NEXT_IP6_LOOKUP] = "ip6-lookup",
703 [SESSION_QUEUE_NEXT_TCP_IP4_OUTPUT] = "tcp4-output",
704 [SESSION_QUEUE_NEXT_TCP_IP6_OUTPUT] = "tcp6-output",
705 },
706};
707/* *INDENT-ON* */
708
709/*
710 * fd.io coding-style-patch-verification: ON
711 *
712 * Local Variables:
713 * eval: (c-set-style "gnu")
714 * End:
715 */