blob: 1471696554788b2ee9924a59708394df36b2aa15 [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 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 Corase86a8ed2018-01-05 03:20:25 -080023#include <svm/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
Florin Corasf6d68ed2017-05-07 19:12:02 -070067always_inline void
68session_tx_fifo_chain_tail (session_manager_main_t * smm, vlib_main_t * vm,
69 u8 thread_index, svm_fifo_t * fifo,
70 vlib_buffer_t * b0, u32 bi0, u8 n_bufs_per_seg,
Florin Corasb2215d62017-08-01 16:56:58 -070071 u32 left_from_seg, u32 * left_to_snd0,
Florin Coras1f152cd2017-08-18 19:28:03 -070072 u16 * n_bufs, u32 * tx_offset, u16 deq_per_buf,
Florin Coras7fb0fe12018-04-09 09:24:52 -070073 u8 peek_data, transport_tx_fn_type_t tx_type)
Florin Corasf6d68ed2017-05-07 19:12:02 -070074{
75 vlib_buffer_t *chain_b0, *prev_b0;
Florin Corasb2215d62017-08-01 16:56:58 -070076 u32 chain_bi0, to_deq;
Florin Corasf6d68ed2017-05-07 19:12:02 -070077 u16 len_to_deq0, n_bytes_read;
78 u8 *data0, j;
79
Florin Corasb2215d62017-08-01 16:56:58 -070080 b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
81 b0->total_length_not_including_first_buffer = 0;
82
Florin Corasf6d68ed2017-05-07 19:12:02 -070083 chain_bi0 = bi0;
84 chain_b0 = b0;
Florin Corasb2215d62017-08-01 16:56:58 -070085 to_deq = left_from_seg;
Florin Corasf6d68ed2017-05-07 19:12:02 -070086 for (j = 1; j < n_bufs_per_seg; j++)
87 {
88 prev_b0 = chain_b0;
Florin Corasb2215d62017-08-01 16:56:58 -070089 len_to_deq0 = clib_min (to_deq, deq_per_buf);
Florin Corasf6d68ed2017-05-07 19:12:02 -070090
91 *n_bufs -= 1;
92 chain_bi0 = smm->tx_buffers[thread_index][*n_bufs];
93 _vec_len (smm->tx_buffers[thread_index]) = *n_bufs;
94
95 chain_b0 = vlib_get_buffer (vm, chain_bi0);
96 chain_b0->current_data = 0;
97 data0 = vlib_buffer_get_current (chain_b0);
98 if (peek_data)
99 {
Florin Coras1f152cd2017-08-18 19:28:03 -0700100 n_bytes_read = svm_fifo_peek (fifo, *tx_offset, len_to_deq0, data0);
101 *tx_offset += n_bytes_read;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700102 }
103 else
104 {
Florin Coras7fb0fe12018-04-09 09:24:52 -0700105 if (tx_type == TRANSPORT_TX_DGRAM)
106 {
107 session_dgram_hdr_t *hdr;
108 u16 deq_now;
109 hdr = (session_dgram_hdr_t *) svm_fifo_head (fifo);
110 deq_now = clib_min (hdr->data_length - hdr->data_offset,
111 len_to_deq0);
112 n_bytes_read = svm_fifo_peek (fifo, hdr->data_offset, deq_now,
113 data0);
114 ASSERT (n_bytes_read > 0);
115
116 hdr->data_offset += n_bytes_read;
117 if (hdr->data_offset == hdr->data_length)
118 svm_fifo_dequeue_drop (fifo, hdr->data_length);
119 }
120 else
121 n_bytes_read = svm_fifo_dequeue_nowait (fifo, len_to_deq0, data0);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700122 }
123 ASSERT (n_bytes_read == len_to_deq0);
124 chain_b0->current_length = n_bytes_read;
125 b0->total_length_not_including_first_buffer += chain_b0->current_length;
126
127 /* update previous buffer */
128 prev_b0->next_buffer = chain_bi0;
129 prev_b0->flags |= VLIB_BUFFER_NEXT_PRESENT;
130
131 /* update current buffer */
132 chain_b0->next_buffer = 0;
133
Florin Corasb2215d62017-08-01 16:56:58 -0700134 to_deq -= n_bytes_read;
135 if (to_deq == 0)
Florin Corasf6d68ed2017-05-07 19:12:02 -0700136 break;
137 }
Florin Coras1f152cd2017-08-18 19:28:03 -0700138 ASSERT (to_deq == 0
139 && b0->total_length_not_including_first_buffer == left_from_seg);
Florin Corasb2215d62017-08-01 16:56:58 -0700140 *left_to_snd0 -= left_from_seg;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700141}
142
Dave Barach68b0fb02017-02-28 15:15:56 -0500143always_inline int
Florin Corasd79b41e2017-03-04 05:37:52 -0800144session_tx_fifo_read_and_snd_i (vlib_main_t * vm, vlib_node_runtime_t * node,
145 session_manager_main_t * smm,
146 session_fifo_event_t * e0,
147 stream_session_t * s0, u32 thread_index,
148 int *n_tx_packets, u8 peek_data)
Dave Barach68b0fb02017-02-28 15:15:56 -0500149{
150 u32 n_trace = vlib_get_trace_count (vm, node);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700151 u32 left_to_snd0, max_len_to_snd0, len_to_deq0, snd_space0;
Florin Corase87216f2017-08-17 16:59:22 -0700152 u32 n_bufs_per_evt, n_frames_per_evt, n_bufs_per_frame;
Dave Barach68b0fb02017-02-28 15:15:56 -0500153 transport_connection_t *tc0;
154 transport_proto_vft_t *transport_vft;
Florin Coras561af9b2017-12-09 10:19:43 -0800155 transport_proto_t tp;
Dave Barach68b0fb02017-02-28 15:15:56 -0500156 u32 next_index, next0, *to_next, n_left_to_next, bi0;
157 vlib_buffer_t *b0;
Florin Coras1f152cd2017-08-18 19:28:03 -0700158 u32 tx_offset = 0, max_dequeue0, n_bytes_per_seg, left_for_seg;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700159 u16 snd_mss0, n_bufs_per_seg, n_bufs;
Dave Barach68b0fb02017-02-28 15:15:56 -0500160 u8 *data0;
Florin Coras6792ec02017-03-13 03:49:51 -0700161 int i, n_bytes_read;
Florin Corase87216f2017-08-17 16:59:22 -0700162 u32 n_bytes_per_buf, deq_per_buf, deq_per_first_buf;
Florin Coras81a13db2018-03-16 08:48:31 -0700163 u32 bufs_alloc, bufs_now;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700164 session_dgram_hdr_t hdr;
Dave Barach68b0fb02017-02-28 15:15:56 -0500165
Florin Coras561af9b2017-12-09 10:19:43 -0800166 next_index = next0 = smm->session_type_to_next[s0->session_type];
Florin Coras561af9b2017-12-09 10:19:43 -0800167 tp = session_get_transport_proto (s0);
168 transport_vft = transport_protocol_get_vft (tp);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700169 if (peek_data)
170 {
171 if (PREDICT_FALSE (s0->session_state < SESSION_STATE_READY))
172 {
173 /* Can retransmit for closed sessions but can't send new data if
174 * session is not ready or closed */
175 vec_add1 (smm->pending_event_vector[thread_index], *e0);
176 return 0;
177 }
178 tc0 =
179 transport_vft->get_connection (s0->connection_index, thread_index);
180 }
181 else
182 {
183 if (s0->session_state == SESSION_STATE_LISTENING)
184 tc0 = transport_vft->get_listener (s0->connection_index);
185 else
186 {
187 if (PREDICT_FALSE (s0->session_state == SESSION_STATE_CLOSED))
188 return 0;
189 tc0 = transport_vft->get_connection (s0->connection_index,
190 thread_index);
191 }
192 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500193
194 /* Make sure we have space to send and there's something to dequeue */
Dave Barach68b0fb02017-02-28 15:15:56 -0500195 snd_mss0 = transport_vft->send_mss (tc0);
Florin Corasc8343412017-05-04 14:25:50 -0700196 snd_space0 = transport_vft->send_space (tc0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500197
Florin Corase04c2992017-03-01 08:17:34 -0800198 /* Can't make any progress */
Florin Coras6792ec02017-03-13 03:49:51 -0700199 if (snd_space0 == 0 || snd_mss0 == 0)
Florin Corase04c2992017-03-01 08:17:34 -0800200 {
Dave Barachacd2a6a2017-05-16 17:41:34 -0400201 vec_add1 (smm->pending_event_vector[thread_index], *e0);
Florin Corase04c2992017-03-01 08:17:34 -0800202 return 0;
203 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500204
Florin Coras6a9b68b2017-11-21 04:20:42 -0800205 /* Allow enqueuing of a new event */
206 svm_fifo_unset_event (s0->server_tx_fifo);
207
Florin Coras9d063042017-09-14 03:08:00 -0400208 /* Check how much we can pull. */
209 max_dequeue0 = svm_fifo_max_dequeue (s0->server_tx_fifo);
Dave Barach68b0fb02017-02-28 15:15:56 -0500210 if (peek_data)
211 {
Florin Coras9d063042017-09-14 03:08:00 -0400212 /* Offset in rx fifo from where to peek data */
Florin Coras1f152cd2017-08-18 19:28:03 -0700213 tx_offset = transport_vft->tx_fifo_offset (tc0);
Florin Coras9d063042017-09-14 03:08:00 -0400214 if (PREDICT_FALSE (tx_offset >= max_dequeue0))
Florin Coras7fb0fe12018-04-09 09:24:52 -0700215 return 0;
216 max_dequeue0 -= tx_offset;
Dave Barach68b0fb02017-02-28 15:15:56 -0500217 }
Florin Coras7fb0fe12018-04-09 09:24:52 -0700218 else
219 {
220 if (transport_vft->tx_type == TRANSPORT_TX_DGRAM)
221 {
222 if (max_dequeue0 < sizeof (hdr))
223 return 0;
224 svm_fifo_peek (s0->server_tx_fifo, 0, sizeof (hdr), (u8 *) & hdr);
225 ASSERT (hdr.data_length > hdr.data_offset);
226 max_dequeue0 = hdr.data_length - hdr.data_offset;
227 }
228 }
229 ASSERT (max_dequeue0 > 0);
Florin Coras6792ec02017-03-13 03:49:51 -0700230
231 /* Ensure we're not writing more than transport window allows */
Florin Coras3e350af2017-03-30 02:54:28 -0700232 if (max_dequeue0 < snd_space0)
233 {
234 /* Constrained by tx queue. Try to send only fully formed segments */
235 max_len_to_snd0 = (max_dequeue0 > snd_mss0) ?
236 max_dequeue0 - max_dequeue0 % snd_mss0 : max_dequeue0;
237 /* TODO Nagle ? */
238 }
239 else
240 {
Florin Coras1f152cd2017-08-18 19:28:03 -0700241 /* Expectation is that snd_space0 is already a multiple of snd_mss */
Florin Coras3e350af2017-03-30 02:54:28 -0700242 max_len_to_snd0 = snd_space0;
243 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500244
Florin Coras93992a92017-05-24 18:03:56 -0700245 n_bytes_per_buf = vlib_buffer_free_list_buffer_size
246 (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
Florin Corase87216f2017-08-17 16:59:22 -0700247 ASSERT (n_bytes_per_buf > MAX_HDRS_LEN);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700248 n_bytes_per_seg = MAX_HDRS_LEN + snd_mss0;
249 n_bufs_per_seg = ceil ((double) n_bytes_per_seg / n_bytes_per_buf);
Florin Corase87216f2017-08-17 16:59:22 -0700250 n_bufs_per_evt = ceil ((double) max_len_to_snd0 / n_bytes_per_seg);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700251 n_frames_per_evt = ceil ((double) n_bufs_per_evt / VLIB_FRAME_SIZE);
Florin Corase87216f2017-08-17 16:59:22 -0700252 n_bufs_per_frame = n_bufs_per_seg * VLIB_FRAME_SIZE;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700253
254 deq_per_buf = clib_min (snd_mss0, n_bytes_per_buf);
Florin Corase87216f2017-08-17 16:59:22 -0700255 deq_per_first_buf = clib_min (snd_mss0, n_bytes_per_buf - MAX_HDRS_LEN);
Dave Barach68b0fb02017-02-28 15:15:56 -0500256
257 n_bufs = vec_len (smm->tx_buffers[thread_index]);
258 left_to_snd0 = max_len_to_snd0;
259 for (i = 0; i < n_frames_per_evt; i++)
260 {
261 /* Make sure we have at least one full frame of buffers ready */
Florin Corase87216f2017-08-17 16:59:22 -0700262 if (PREDICT_FALSE (n_bufs < n_bufs_per_frame))
Dave Barach68b0fb02017-02-28 15:15:56 -0500263 {
264 vec_validate (smm->tx_buffers[thread_index],
Florin Corase87216f2017-08-17 16:59:22 -0700265 n_bufs + n_bufs_per_frame - 1);
Florin Coras81a13db2018-03-16 08:48:31 -0700266 bufs_alloc = 0;
Florin Coras93992a92017-05-24 18:03:56 -0700267 do
Dave Barach68b0fb02017-02-28 15:15:56 -0500268 {
Florin Coras81a13db2018-03-16 08:48:31 -0700269 bufs_now =
270 vlib_buffer_alloc (vm,
271 &smm->tx_buffers[thread_index][n_bufs +
272 bufs_alloc],
273 n_bufs_per_frame - bufs_alloc);
274 bufs_alloc += bufs_now;
Dave Barach68b0fb02017-02-28 15:15:56 -0500275 }
Florin Coras81a13db2018-03-16 08:48:31 -0700276 while (bufs_now > 0 && ((bufs_alloc + n_bufs < n_bufs_per_frame)));
Florin Coras93992a92017-05-24 18:03:56 -0700277
Florin Coras81a13db2018-03-16 08:48:31 -0700278 n_bufs += bufs_alloc;
Dave Barach68b0fb02017-02-28 15:15:56 -0500279 _vec_len (smm->tx_buffers[thread_index]) = n_bufs;
Florin Coras93992a92017-05-24 18:03:56 -0700280
Florin Corase87216f2017-08-17 16:59:22 -0700281 if (PREDICT_FALSE (n_bufs < n_bufs_per_frame))
Florin Coras93992a92017-05-24 18:03:56 -0700282 {
283 vec_add1 (smm->pending_event_vector[thread_index], *e0);
284 return -1;
285 }
Florin Corase87216f2017-08-17 16:59:22 -0700286 ASSERT (n_bufs >= n_bufs_per_frame);
Dave Barach68b0fb02017-02-28 15:15:56 -0500287 }
288
289 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Florin Corase87216f2017-08-17 16:59:22 -0700290 while (left_to_snd0 && n_left_to_next)
Dave Barach68b0fb02017-02-28 15:15:56 -0500291 {
Florin Corasf6d68ed2017-05-07 19:12:02 -0700292 /*
293 * Handle first buffer in chain separately
294 */
295
Florin Coras81a13db2018-03-16 08:48:31 -0700296 len_to_deq0 = clib_min (left_to_snd0, deq_per_first_buf);
297 if (left_to_snd0 > len_to_deq0 && n_left_to_next > 1)
298 {
299 vlib_buffer_t *pb;
300 u32 pbi = smm->tx_buffers[thread_index][n_bufs - 2];
301 pb = vlib_get_buffer (vm, pbi);
302 vlib_prefetch_buffer_header (pb, LOAD);
303 }
304
Dave Barach68b0fb02017-02-28 15:15:56 -0500305 /* Get free buffer */
Florin Coras93992a92017-05-24 18:03:56 -0700306 ASSERT (n_bufs >= 1);
Florin Corasf6d68ed2017-05-07 19:12:02 -0700307 bi0 = smm->tx_buffers[thread_index][--n_bufs];
Dave Barach68b0fb02017-02-28 15:15:56 -0500308 _vec_len (smm->tx_buffers[thread_index]) = n_bufs;
309
Florin Corasf6359c82017-06-19 12:26:09 -0400310 /* usual speculation, or the enqueue_x1 macro will barf */
311 to_next[0] = bi0;
312 to_next += 1;
313 n_left_to_next -= 1;
314
Dave Barach68b0fb02017-02-28 15:15:56 -0500315 b0 = vlib_get_buffer (vm, bi0);
316 b0->error = 0;
Florin Coras81a13db2018-03-16 08:48:31 -0700317 b0->flags = VNET_BUFFER_F_LOCALLY_ORIGINATED;
Dave Barach68b0fb02017-02-28 15:15:56 -0500318 b0->current_data = 0;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700319 b0->total_length_not_including_first_buffer = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500320
Florin Corasf6d68ed2017-05-07 19:12:02 -0700321 data0 = vlib_buffer_make_headroom (b0, MAX_HDRS_LEN);
322 if (peek_data)
323 {
Florin Coras1f152cd2017-08-18 19:28:03 -0700324 n_bytes_read = svm_fifo_peek (s0->server_tx_fifo, tx_offset,
Florin Corasf6d68ed2017-05-07 19:12:02 -0700325 len_to_deq0, data0);
Florin Coras9d063042017-09-14 03:08:00 -0400326 if (n_bytes_read <= 0)
327 goto dequeue_fail;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700328 /* Keep track of progress locally, transport is also supposed to
329 * increment it independently when pushing the header */
Florin Coras1f152cd2017-08-18 19:28:03 -0700330 tx_offset += n_bytes_read;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700331 }
332 else
333 {
Florin Coras7fb0fe12018-04-09 09:24:52 -0700334 if (transport_vft->tx_type == TRANSPORT_TX_DGRAM)
335 {
336 svm_fifo_t *f = s0->server_tx_fifo;
337 u16 deq_now;
338 u32 offset;
339
340 ASSERT (hdr.data_length > hdr.data_offset);
341 deq_now = clib_min (hdr.data_length - hdr.data_offset,
342 len_to_deq0);
343 offset = hdr.data_offset + SESSION_CONN_HDR_LEN;
344 n_bytes_read = svm_fifo_peek (f, offset, deq_now, data0);
345 if (PREDICT_FALSE (n_bytes_read <= 0))
346 goto dequeue_fail;
347
348 if (s0->session_state == SESSION_STATE_LISTENING)
349 {
350 ip_copy (&tc0->rmt_ip, &hdr.rmt_ip, tc0->is_ip4);
351 tc0->rmt_port = hdr.rmt_port;
352 }
353 hdr.data_offset += n_bytes_read;
354 if (hdr.data_offset == hdr.data_length)
355 {
356 offset = hdr.data_length + SESSION_CONN_HDR_LEN;
357 svm_fifo_dequeue_drop (f, offset);
358 }
359 }
360 else
361 {
362 n_bytes_read = svm_fifo_dequeue_nowait (s0->server_tx_fifo,
363 len_to_deq0, data0);
364 if (n_bytes_read <= 0)
365 goto dequeue_fail;
366 }
Florin Corasf6d68ed2017-05-07 19:12:02 -0700367 }
368
Florin Corasf6d68ed2017-05-07 19:12:02 -0700369 b0->current_length = n_bytes_read;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700370 left_to_snd0 -= n_bytes_read;
371 *n_tx_packets = *n_tx_packets + 1;
372
373 /*
374 * Fill in the remaining buffers in the chain, if any
375 */
Florin Corase87216f2017-08-17 16:59:22 -0700376 if (PREDICT_FALSE (n_bufs_per_seg > 1 && left_to_snd0))
Florin Corasb2215d62017-08-01 16:56:58 -0700377 {
Florin Corasb2215d62017-08-01 16:56:58 -0700378 left_for_seg = clib_min (snd_mss0 - n_bytes_read, left_to_snd0);
379 session_tx_fifo_chain_tail (smm, vm, thread_index,
380 s0->server_tx_fifo, b0, bi0,
381 n_bufs_per_seg, left_for_seg,
Florin Coras1f152cd2017-08-18 19:28:03 -0700382 &left_to_snd0, &n_bufs, &tx_offset,
Florin Coras7fb0fe12018-04-09 09:24:52 -0700383 deq_per_buf, peek_data,
384 transport_vft->tx_type);
Florin Corasb2215d62017-08-01 16:56:58 -0700385 }
Florin Corasf6d68ed2017-05-07 19:12:02 -0700386
387 /* Ask transport to push header after current_length and
388 * total_length_not_including_first_buffer are updated */
389 transport_vft->push_header (tc0, b0);
390
391 /* *INDENT-OFF* */
392 SESSION_EVT_DBG(SESSION_EVT_DEQ, s0, ({
Florin Corasec44e342017-10-16 20:47:56 -0700393 ed->data[0] = e0->event_type;
Florin Corasf6d68ed2017-05-07 19:12:02 -0700394 ed->data[1] = max_dequeue0;
395 ed->data[2] = len_to_deq0;
396 ed->data[3] = left_to_snd0;
397 }));
398 /* *INDENT-ON* */
399
Dave Barach68b0fb02017-02-28 15:15:56 -0500400 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
Florin Coras4eeeaaf2017-09-05 14:03:37 -0400401
Dave Barach68b0fb02017-02-28 15:15:56 -0500402 if (PREDICT_FALSE (n_trace > 0))
403 {
404 session_queue_trace_t *t0;
405 vlib_trace_buffer (vm, node, next_index, b0,
406 1 /* follow_chain */ );
407 vlib_set_trace_count (vm, node, --n_trace);
408 t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
409 t0->session_index = s0->session_index;
410 t0->server_thread_index = s0->thread_index;
411 }
412
Dave Barach68b0fb02017-02-28 15:15:56 -0500413 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
414 to_next, n_left_to_next,
415 bi0, next0);
416 }
417 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
418 }
419
Florin Coras6792ec02017-03-13 03:49:51 -0700420 /* If we couldn't dequeue all bytes mark as partially read */
421 if (max_len_to_snd0 < max_dequeue0)
Florin Coras7fb0fe12018-04-09 09:24:52 -0700422 if (svm_fifo_set_event (s0->server_tx_fifo))
423 vec_add1 (smm->pending_event_vector[thread_index], *e0);
424
425 if (!peek_data && transport_vft->tx_type == TRANSPORT_TX_DGRAM)
Dave Barach68b0fb02017-02-28 15:15:56 -0500426 {
Florin Coras7fb0fe12018-04-09 09:24:52 -0700427 /* Fix dgram pre header */
428 if (max_len_to_snd0 < max_dequeue0)
429 svm_fifo_overwrite_head (s0->server_tx_fifo, (u8 *) & hdr,
430 sizeof (session_dgram_pre_hdr_t));
431 /* More data needs to be read */
432 else if (svm_fifo_max_dequeue (s0->server_tx_fifo) > 0)
433 vec_add1 (smm->pending_event_vector[thread_index], *e0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500434 }
435 return 0;
436
437dequeue_fail:
Florin Coras6792ec02017-03-13 03:49:51 -0700438 /*
439 * Can't read from fifo. If we don't already have an event, save as partially
440 * read, return buff to free list and return
441 */
442 clib_warning ("dequeue fail");
Florin Coras6792ec02017-03-13 03:49:51 -0700443 if (svm_fifo_set_event (s0->server_tx_fifo))
444 {
Dave Barachacd2a6a2017-05-16 17:41:34 -0400445 vec_add1 (smm->pending_event_vector[thread_index], *e0);
Florin Coras6792ec02017-03-13 03:49:51 -0700446 }
447 vlib_put_next_frame (vm, node, next_index, n_left_to_next + 1);
Dave Barach68b0fb02017-02-28 15:15:56 -0500448 _vec_len (smm->tx_buffers[thread_index]) += 1;
449
Dave Barach68b0fb02017-02-28 15:15:56 -0500450 return 0;
451}
452
453int
Florin Corasd79b41e2017-03-04 05:37:52 -0800454session_tx_fifo_peek_and_snd (vlib_main_t * vm, vlib_node_runtime_t * node,
455 session_manager_main_t * smm,
456 session_fifo_event_t * e0,
457 stream_session_t * s0, u32 thread_index,
458 int *n_tx_pkts)
Dave Barach68b0fb02017-02-28 15:15:56 -0500459{
Florin Corasd79b41e2017-03-04 05:37:52 -0800460 return session_tx_fifo_read_and_snd_i (vm, node, smm, e0, s0, thread_index,
461 n_tx_pkts, 1);
Dave Barach68b0fb02017-02-28 15:15:56 -0500462}
463
464int
Florin Corasd79b41e2017-03-04 05:37:52 -0800465session_tx_fifo_dequeue_and_snd (vlib_main_t * vm, vlib_node_runtime_t * node,
466 session_manager_main_t * smm,
467 session_fifo_event_t * e0,
468 stream_session_t * s0, u32 thread_index,
469 int *n_tx_pkts)
Dave Barach68b0fb02017-02-28 15:15:56 -0500470{
Florin Corasd79b41e2017-03-04 05:37:52 -0800471 return session_tx_fifo_read_and_snd_i (vm, node, smm, e0, s0, thread_index,
472 n_tx_pkts, 0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500473}
474
Florin Coras371ca502018-02-21 12:07:41 -0800475int
476session_tx_fifo_dequeue_internal (vlib_main_t * vm,
477 vlib_node_runtime_t * node,
478 session_manager_main_t * smm,
479 session_fifo_event_t * e0,
480 stream_session_t * s0, u32 thread_index,
481 int *n_tx_pkts)
482{
483 application_t *app;
484 app = application_get (s0->opaque);
485 svm_fifo_unset_event (s0->server_tx_fifo);
486 return app->cb_fns.builtin_app_tx_callback (s0);
487}
488
Dave Barach2c25a622017-06-26 11:35:07 -0400489always_inline stream_session_t *
490session_event_get_session (session_fifo_event_t * e, u8 thread_index)
Florin Corasa5464812017-04-19 13:00:05 -0700491{
Florin Coras3cbc04b2017-10-02 00:18:51 -0700492 return session_get_if_valid (e->fifo->master_session_index, thread_index);
Florin Corasa5464812017-04-19 13:00:05 -0700493}
494
Dave Barachacd2a6a2017-05-16 17:41:34 -0400495void
496dump_thread_0_event_queue (void)
497{
498 session_manager_main_t *smm = vnet_get_session_manager_main ();
499 vlib_main_t *vm = &vlib_global_main;
500 u32 my_thread_index = vm->thread_index;
501 session_fifo_event_t _e, *e = &_e;
502 stream_session_t *s0;
503 int i, index;
504 i8 *headp;
505
Florin Corase86a8ed2018-01-05 03:20:25 -0800506 svm_queue_t *q;
Dave Barachacd2a6a2017-05-16 17:41:34 -0400507 q = smm->vpp_event_queues[my_thread_index];
508
509 index = q->head;
510
511 for (i = 0; i < q->cursize; i++)
512 {
513 headp = (i8 *) (&q->data[0] + q->elsize * index);
514 clib_memcpy (e, headp, q->elsize);
515
516 switch (e->event_type)
517 {
518 case FIFO_EVENT_APP_TX:
519 s0 = session_event_get_session (e, my_thread_index);
520 fformat (stdout, "[%04d] TX session %d\n", i, s0->session_index);
521 break;
522
523 case FIFO_EVENT_DISCONNECT:
Florin Corascea194d2017-10-02 00:18:51 -0700524 s0 = session_get_from_handle (e->session_handle);
Dave Barachacd2a6a2017-05-16 17:41:34 -0400525 fformat (stdout, "[%04d] disconnect session %d\n", i,
526 s0->session_index);
527 break;
528
529 case FIFO_EVENT_BUILTIN_RX:
530 s0 = session_event_get_session (e, my_thread_index);
531 fformat (stdout, "[%04d] builtin_rx %d\n", i, s0->session_index);
532 break;
533
534 case FIFO_EVENT_RPC:
535 fformat (stdout, "[%04d] RPC call %llx with %llx\n",
536 i, (u64) (e->rpc_args.fp), (u64) (e->rpc_args.arg));
537 break;
538
539 default:
540 fformat (stdout, "[%04d] unhandled event type %d\n",
541 i, e->event_type);
542 break;
543 }
544
545 index++;
546
547 if (index == q->maxsize)
548 index = 0;
549 }
550}
551
Florin Coras6534b7a2017-07-18 05:38:03 -0400552static u8
553session_node_cmp_event (session_fifo_event_t * e, svm_fifo_t * f)
554{
555 stream_session_t *s;
556 switch (e->event_type)
557 {
558 case FIFO_EVENT_APP_RX:
559 case FIFO_EVENT_APP_TX:
560 case FIFO_EVENT_BUILTIN_RX:
561 if (e->fifo == f)
562 return 1;
563 break;
564 case FIFO_EVENT_DISCONNECT:
565 break;
566 case FIFO_EVENT_RPC:
Florin Corascea194d2017-10-02 00:18:51 -0700567 s = session_get_from_handle (e->session_handle);
Florin Coras6534b7a2017-07-18 05:38:03 -0400568 if (!s)
569 {
570 clib_warning ("session has event but doesn't exist!");
571 break;
572 }
573 if (s->server_rx_fifo == f || s->server_tx_fifo == f)
574 return 1;
575 break;
576 default:
577 break;
578 }
579 return 0;
580}
581
582u8
583session_node_lookup_fifo_event (svm_fifo_t * f, session_fifo_event_t * e)
584{
585 session_manager_main_t *smm = vnet_get_session_manager_main ();
Florin Corase86a8ed2018-01-05 03:20:25 -0800586 svm_queue_t *q;
Florin Coras6534b7a2017-07-18 05:38:03 -0400587 session_fifo_event_t *pending_event_vector, *evt;
588 int i, index, found = 0;
589 i8 *headp;
590 u8 thread_index;
591
592 ASSERT (e);
593 thread_index = f->master_thread_index;
594 /*
595 * Search evt queue
596 */
597 q = smm->vpp_event_queues[thread_index];
598 index = q->head;
599 for (i = 0; i < q->cursize; i++)
600 {
601 headp = (i8 *) (&q->data[0] + q->elsize * index);
602 clib_memcpy (e, headp, q->elsize);
603 found = session_node_cmp_event (e, f);
604 if (found)
Florin Coras371ca502018-02-21 12:07:41 -0800605 return 1;
Florin Coras6534b7a2017-07-18 05:38:03 -0400606 if (++index == q->maxsize)
607 index = 0;
608 }
609 /*
610 * Search pending events vector
611 */
612 pending_event_vector = smm->pending_event_vector[thread_index];
613 vec_foreach (evt, pending_event_vector)
614 {
615 found = session_node_cmp_event (evt, f);
616 if (found)
617 {
618 clib_memcpy (e, evt, sizeof (*evt));
619 break;
620 }
621 }
622 return found;
623}
624
Dave Barach68b0fb02017-02-28 15:15:56 -0500625static uword
626session_queue_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
627 vlib_frame_t * frame)
628{
629 session_manager_main_t *smm = vnet_get_session_manager_main ();
Florin Coras3cbc04b2017-10-02 00:18:51 -0700630 session_fifo_event_t *my_pending_event_vector, *pending_disconnects, *e;
Dave Barachacd2a6a2017-05-16 17:41:34 -0400631 session_fifo_event_t *my_fifo_events;
Florin Corase04c2992017-03-01 08:17:34 -0800632 u32 n_to_dequeue, n_events;
Florin Corase86a8ed2018-01-05 03:20:25 -0800633 svm_queue_t *q;
Florin Coras6792ec02017-03-13 03:49:51 -0700634 application_t *app;
Dave Barach68b0fb02017-02-28 15:15:56 -0500635 int n_tx_packets = 0;
Damjan Marion586afd72017-04-05 19:18:20 +0200636 u32 my_thread_index = vm->thread_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500637 int i, rv;
Florin Coras3e350af2017-03-30 02:54:28 -0700638 f64 now = vlib_time_now (vm);
Dave Barache5f1d272017-05-10 13:34:04 -0400639 void (*fp) (void *);
Florin Coras3e350af2017-03-30 02:54:28 -0700640
641 SESSION_EVT_DBG (SESSION_EVT_POLL_GAP_TRACK, smm, my_thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500642
643 /*
Florin Coras561af9b2017-12-09 10:19:43 -0800644 * Update transport time
Dave Barach68b0fb02017-02-28 15:15:56 -0500645 */
Florin Coras561af9b2017-12-09 10:19:43 -0800646 transport_update_time (now, my_thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500647
648 /*
649 * Get vpp queue events
650 */
651 q = smm->vpp_event_queues[my_thread_index];
652 if (PREDICT_FALSE (q == 0))
653 return 0;
654
Dave Barachacd2a6a2017-05-16 17:41:34 -0400655 my_fifo_events = smm->free_event_vector[my_thread_index];
656
Dave Barach68b0fb02017-02-28 15:15:56 -0500657 /* min number of events we can dequeue without blocking */
658 n_to_dequeue = q->cursize;
Dave Barachacd2a6a2017-05-16 17:41:34 -0400659 my_pending_event_vector = smm->pending_event_vector[my_thread_index];
Florin Coras3cbc04b2017-10-02 00:18:51 -0700660 pending_disconnects = smm->pending_disconnects[my_thread_index];
Dave Barach68b0fb02017-02-28 15:15:56 -0500661
Florin Coras3cbc04b2017-10-02 00:18:51 -0700662 if (!n_to_dequeue && !vec_len (my_pending_event_vector)
663 && !vec_len (pending_disconnects))
Florin Corase04c2992017-03-01 08:17:34 -0800664 return 0;
665
Florin Coras6792ec02017-03-13 03:49:51 -0700666 SESSION_EVT_DBG (SESSION_EVT_DEQ_NODE, 0);
667
Florin Corase04c2992017-03-01 08:17:34 -0800668 /*
669 * If we didn't manage to process previous events try going
Dave Barach68b0fb02017-02-28 15:15:56 -0500670 * over them again without dequeuing new ones.
Florin Corase04c2992017-03-01 08:17:34 -0800671 */
672 /* XXX: Block senders to sessions that can't keep up */
Dave Barachacd2a6a2017-05-16 17:41:34 -0400673 if (0 && vec_len (my_pending_event_vector) >= 100)
Florin Coras6792ec02017-03-13 03:49:51 -0700674 {
675 clib_warning ("too many fifo events unsolved");
676 goto skip_dequeue;
677 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500678
679 /* See you in the next life, don't be late */
680 if (pthread_mutex_trylock (&q->mutex))
681 return 0;
682
683 for (i = 0; i < n_to_dequeue; i++)
684 {
685 vec_add2 (my_fifo_events, e, 1);
Florin Corase86a8ed2018-01-05 03:20:25 -0800686 svm_queue_sub_raw (q, (u8 *) e);
Dave Barach68b0fb02017-02-28 15:15:56 -0500687 }
688
689 /* The other side of the connection is not polling */
690 if (q->cursize < (q->maxsize / 8))
691 (void) pthread_cond_broadcast (&q->condvar);
692 pthread_mutex_unlock (&q->mutex);
693
Dave Barachacd2a6a2017-05-16 17:41:34 -0400694 vec_append (my_fifo_events, my_pending_event_vector);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700695 vec_append (my_fifo_events, smm->pending_disconnects[my_thread_index]);
Dave Barachacd2a6a2017-05-16 17:41:34 -0400696
697 _vec_len (my_pending_event_vector) = 0;
698 smm->pending_event_vector[my_thread_index] = my_pending_event_vector;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700699 _vec_len (smm->pending_disconnects[my_thread_index]) = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500700
701skip_dequeue:
Florin Corase04c2992017-03-01 08:17:34 -0800702 n_events = vec_len (my_fifo_events);
703 for (i = 0; i < n_events; i++)
Dave Barach68b0fb02017-02-28 15:15:56 -0500704 {
Florin Corasa5464812017-04-19 13:00:05 -0700705 stream_session_t *s0; /* $$$ prefetch 1 ahead maybe */
Dave Barach68b0fb02017-02-28 15:15:56 -0500706 session_fifo_event_t *e0;
707
708 e0 = &my_fifo_events[i];
Dave Barach68b0fb02017-02-28 15:15:56 -0500709
710 switch (e0->event_type)
711 {
Florin Corasa5464812017-04-19 13:00:05 -0700712 case FIFO_EVENT_APP_TX:
713 s0 = session_event_get_session (e0, my_thread_index);
714
Florin Coras9d063042017-09-14 03:08:00 -0400715 if (PREDICT_FALSE (!s0))
Florin Corasa5464812017-04-19 13:00:05 -0700716 {
717 clib_warning ("It's dead, Jim!");
718 continue;
719 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500720 /* Spray packets in per session type frames, since they go to
721 * different nodes */
Florin Corase69f4952017-03-07 10:06:24 -0800722 rv = (smm->session_tx_fns[s0->session_type]) (vm, node, smm, e0, s0,
Dave Barach68b0fb02017-02-28 15:15:56 -0500723 my_thread_index,
724 &n_tx_packets);
Florin Coras6792ec02017-03-13 03:49:51 -0700725 /* Out of buffers */
Florin Coras93992a92017-05-24 18:03:56 -0700726 if (PREDICT_FALSE (rv < 0))
Dave Barachacd2a6a2017-05-16 17:41:34 -0400727 {
Florin Coras93992a92017-05-24 18:03:56 -0700728 vlib_node_increment_counter (vm, node->node_index,
729 SESSION_QUEUE_ERROR_NO_BUFFER, 1);
Dave Barachacd2a6a2017-05-16 17:41:34 -0400730 continue;
731 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500732 break;
Florin Corasa5464812017-04-19 13:00:05 -0700733 case FIFO_EVENT_DISCONNECT:
Florin Coras3cbc04b2017-10-02 00:18:51 -0700734 /* Make sure disconnects run after the pending list is drained */
735 if (!e0->postponed)
736 {
737 e0->postponed = 1;
738 vec_add1 (smm->pending_disconnects[my_thread_index], *e0);
739 continue;
740 }
Florin Corascea194d2017-10-02 00:18:51 -0700741 s0 = session_get_from_handle (e0->session_handle);
Florin Coras2f8d8fa2018-01-26 06:36:04 -0800742 stream_session_disconnect_transport (s0);
Florin Coras6792ec02017-03-13 03:49:51 -0700743 break;
744 case FIFO_EVENT_BUILTIN_RX:
Florin Corasa5464812017-04-19 13:00:05 -0700745 s0 = session_event_get_session (e0, my_thread_index);
Florin Corasc87c91d2017-08-16 19:55:49 -0700746 if (PREDICT_FALSE (!s0))
747 continue;
Florin Coras6792ec02017-03-13 03:49:51 -0700748 svm_fifo_unset_event (s0->server_rx_fifo);
Florin Coras6792ec02017-03-13 03:49:51 -0700749 app = application_get (s0->app_index);
Florin Coras371ca502018-02-21 12:07:41 -0800750 app->cb_fns.builtin_app_rx_callback (s0);
Florin Coras6792ec02017-03-13 03:49:51 -0700751 break;
Dave Barache5f1d272017-05-10 13:34:04 -0400752 case FIFO_EVENT_RPC:
753 fp = e0->rpc_args.fp;
754 (*fp) (e0->rpc_args.arg);
755 break;
756
Dave Barach68b0fb02017-02-28 15:15:56 -0500757 default:
758 clib_warning ("unhandled event type %d", e0->event_type);
759 }
760 }
761
Dave Barachacd2a6a2017-05-16 17:41:34 -0400762 _vec_len (my_fifo_events) = 0;
763 smm->free_event_vector[my_thread_index] = my_fifo_events;
Dave Barach68b0fb02017-02-28 15:15:56 -0500764
765 vlib_node_increment_counter (vm, session_queue_node.index,
766 SESSION_QUEUE_ERROR_TX, n_tx_packets);
767
Florin Coras6792ec02017-03-13 03:49:51 -0700768 SESSION_EVT_DBG (SESSION_EVT_DEQ_NODE, 1);
769
Dave Barach68b0fb02017-02-28 15:15:56 -0500770 return n_tx_packets;
771}
772
773/* *INDENT-OFF* */
774VLIB_REGISTER_NODE (session_queue_node) =
775{
776 .function = session_queue_node_fn,
777 .name = "session-queue",
778 .format_trace = format_session_queue_trace,
779 .type = VLIB_NODE_TYPE_INPUT,
780 .n_errors = ARRAY_LEN (session_queue_error_strings),
781 .error_strings = session_queue_error_strings,
Florin Corase04c2992017-03-01 08:17:34 -0800782 .state = VLIB_NODE_STATE_DISABLED,
Dave Barach68b0fb02017-02-28 15:15:56 -0500783};
784/* *INDENT-ON* */
785
Dave Barach2a863912017-11-28 10:11:42 -0500786static clib_error_t *
787session_queue_exit (vlib_main_t * vm)
788{
789 if (vec_len (vlib_mains) < 2)
790 return 0;
791
792 /*
793 * Shut off (especially) worker-thread session nodes.
794 * Otherwise, vpp can crash as the main thread unmaps the
795 * API segment.
796 */
797 vlib_worker_thread_barrier_sync (vm);
798 session_node_enable_disable (0 /* is_enable */ );
799 vlib_worker_thread_barrier_release (vm);
800 return 0;
801}
802
803VLIB_MAIN_LOOP_EXIT_FUNCTION (session_queue_exit);
804
Dave Barach68b0fb02017-02-28 15:15:56 -0500805/*
806 * fd.io coding-style-patch-verification: ON
807 *
808 * Local Variables:
809 * eval: (c-set-style "gnu")
810 * End:
811 */