Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1 | /* |
| 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 Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 16 | #include <math.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 17 | #include <vlib/vlib.h> |
| 18 | #include <vnet/vnet.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 19 | #include <vppinfra/elog.h> |
Florin Coras | 561af9b | 2017-12-09 10:19:43 -0800 | [diff] [blame] | 20 | #include <vnet/session/transport.h> |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 21 | #include <vnet/session/session.h> |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 22 | #include <vnet/session/application.h> |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 23 | #include <vnet/session/session_debug.h> |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 24 | #include <svm/queue.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 25 | |
| 26 | vlib_node_registration_t session_queue_node; |
| 27 | |
| 28 | typedef struct |
| 29 | { |
| 30 | u32 session_index; |
| 31 | u32 server_thread_index; |
| 32 | } session_queue_trace_t; |
| 33 | |
| 34 | /* packet trace format function */ |
| 35 | static u8 * |
| 36 | format_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 | |
| 47 | vlib_node_registration_t session_queue_node; |
| 48 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 49 | #define foreach_session_queue_error \ |
| 50 | _(TX, "Packets transmitted") \ |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 51 | _(TIMER, "Timer events") \ |
| 52 | _(NO_BUFFER, "Out of buffers") |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 53 | |
| 54 | typedef 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 | |
| 62 | static char *session_queue_error_strings[] = { |
| 63 | #define _(sym,string) string, |
| 64 | foreach_session_queue_error |
| 65 | #undef _ |
| 66 | }; |
| 67 | |
Florin Coras | 0d60a0f | 2018-06-29 02:02:08 -0700 | [diff] [blame] | 68 | enum |
| 69 | { |
| 70 | SESSION_TX_NO_BUFFERS = -2, |
| 71 | SESSION_TX_NO_DATA, |
| 72 | SESSION_TX_OK |
| 73 | }; |
| 74 | |
| 75 | |
Florin Coras | 66cf5e0 | 2018-06-08 09:17:39 -0700 | [diff] [blame] | 76 | static void |
| 77 | session_tx_trace_frame (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 78 | u32 next_index, u32 * to_next, u16 n_segs, |
| 79 | stream_session_t * s, u32 n_trace) |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 80 | { |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 81 | session_queue_trace_t *t; |
Florin Coras | 66cf5e0 | 2018-06-08 09:17:39 -0700 | [diff] [blame] | 82 | vlib_buffer_t *b; |
| 83 | int i; |
| 84 | |
| 85 | for (i = 0; i < clib_min (n_trace, n_segs); i++) |
| 86 | { |
| 87 | b = vlib_get_buffer (vm, to_next[i - n_segs]); |
| 88 | vlib_trace_buffer (vm, node, next_index, b, 1 /* follow_chain */ ); |
Florin Coras | 66cf5e0 | 2018-06-08 09:17:39 -0700 | [diff] [blame] | 89 | t = vlib_add_trace (vm, node, b, sizeof (*t)); |
| 90 | t->session_index = s->session_index; |
| 91 | t->server_thread_index = s->thread_index; |
| 92 | } |
Florin Coras | 4df3871 | 2018-06-20 12:44:16 -0700 | [diff] [blame] | 93 | vlib_set_trace_count (vm, node, n_trace - i); |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 94 | } |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 95 | |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 96 | always_inline void |
| 97 | session_tx_fifo_chain_tail (vlib_main_t * vm, session_tx_context_t * ctx, |
| 98 | vlib_buffer_t * b, u16 * n_bufs, u8 peek_data) |
| 99 | { |
| 100 | session_manager_main_t *smm = &session_manager_main; |
| 101 | vlib_buffer_t *chain_b, *prev_b; |
| 102 | u32 chain_bi0, to_deq, left_from_seg; |
| 103 | u16 len_to_deq, n_bytes_read; |
| 104 | u8 *data, j; |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 105 | |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 106 | b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID; |
| 107 | b->total_length_not_including_first_buffer = 0; |
| 108 | |
| 109 | chain_b = b; |
| 110 | left_from_seg = clib_min (ctx->snd_mss - b->current_length, |
| 111 | ctx->left_to_snd); |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 112 | to_deq = left_from_seg; |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 113 | for (j = 1; j < ctx->n_bufs_per_seg; j++) |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 114 | { |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 115 | prev_b = chain_b; |
| 116 | len_to_deq = clib_min (to_deq, ctx->deq_per_buf); |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 117 | |
| 118 | *n_bufs -= 1; |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 119 | chain_bi0 = smm->tx_buffers[ctx->s->thread_index][*n_bufs]; |
| 120 | _vec_len (smm->tx_buffers[ctx->s->thread_index]) = *n_bufs; |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 121 | |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 122 | chain_b = vlib_get_buffer (vm, chain_bi0); |
| 123 | chain_b->current_data = 0; |
| 124 | data = vlib_buffer_get_current (chain_b); |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 125 | if (peek_data) |
| 126 | { |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 127 | n_bytes_read = svm_fifo_peek (ctx->s->server_tx_fifo, |
| 128 | ctx->tx_offset, len_to_deq, data); |
| 129 | ctx->tx_offset += n_bytes_read; |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 130 | } |
| 131 | else |
| 132 | { |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 133 | if (ctx->transport_vft->tx_type == TRANSPORT_TX_DGRAM) |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 134 | { |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 135 | svm_fifo_t *f = ctx->s->server_tx_fifo; |
| 136 | session_dgram_hdr_t *hdr = &ctx->hdr; |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 137 | u16 deq_now; |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 138 | deq_now = clib_min (hdr->data_length - hdr->data_offset, |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 139 | len_to_deq); |
| 140 | n_bytes_read = svm_fifo_peek (f, hdr->data_offset, deq_now, |
| 141 | data); |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 142 | ASSERT (n_bytes_read > 0); |
| 143 | |
| 144 | hdr->data_offset += n_bytes_read; |
| 145 | if (hdr->data_offset == hdr->data_length) |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 146 | svm_fifo_dequeue_drop (f, hdr->data_length); |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 147 | } |
| 148 | else |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 149 | n_bytes_read = svm_fifo_dequeue_nowait (ctx->s->server_tx_fifo, |
| 150 | len_to_deq, data); |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 151 | } |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 152 | ASSERT (n_bytes_read == len_to_deq); |
| 153 | chain_b->current_length = n_bytes_read; |
| 154 | b->total_length_not_including_first_buffer += chain_b->current_length; |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 155 | |
| 156 | /* update previous buffer */ |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 157 | prev_b->next_buffer = chain_bi0; |
| 158 | prev_b->flags |= VLIB_BUFFER_NEXT_PRESENT; |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 159 | |
| 160 | /* update current buffer */ |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 161 | chain_b->next_buffer = 0; |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 162 | |
Florin Coras | b2215d6 | 2017-08-01 16:56:58 -0700 | [diff] [blame] | 163 | to_deq -= n_bytes_read; |
| 164 | if (to_deq == 0) |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 165 | break; |
| 166 | } |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 167 | ASSERT (to_deq == 0 |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 168 | && b->total_length_not_including_first_buffer == left_from_seg); |
| 169 | ctx->left_to_snd -= left_from_seg; |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 172 | always_inline int |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 173 | session_output_try_get_buffers (vlib_main_t * vm, |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 174 | session_manager_main_t * smm, |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 175 | u32 thread_index, u16 * n_bufs, u32 wanted) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 176 | { |
Florin Coras | f08f26d | 2018-05-10 13:20:47 -0700 | [diff] [blame] | 177 | u32 n_alloc; |
| 178 | vec_validate_aligned (smm->tx_buffers[thread_index], wanted - 1, |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 179 | CLIB_CACHE_LINE_BYTES); |
Florin Coras | f08f26d | 2018-05-10 13:20:47 -0700 | [diff] [blame] | 180 | n_alloc = vlib_buffer_alloc (vm, &smm->tx_buffers[thread_index][*n_bufs], |
| 181 | wanted - *n_bufs); |
| 182 | *n_bufs += n_alloc; |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 183 | _vec_len (smm->tx_buffers[thread_index]) = *n_bufs; |
Florin Coras | f08f26d | 2018-05-10 13:20:47 -0700 | [diff] [blame] | 184 | return n_alloc; |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | always_inline void |
| 188 | session_tx_fill_buffer (vlib_main_t * vm, session_tx_context_t * ctx, |
| 189 | vlib_buffer_t * b, u16 * n_bufs, u8 peek_data) |
| 190 | { |
| 191 | u32 len_to_deq; |
| 192 | u8 *data0; |
| 193 | int n_bytes_read; |
| 194 | |
| 195 | /* |
| 196 | * Start with the first buffer in chain |
| 197 | */ |
| 198 | b->error = 0; |
| 199 | b->flags = VNET_BUFFER_F_LOCALLY_ORIGINATED; |
| 200 | b->current_data = 0; |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 201 | |
| 202 | data0 = vlib_buffer_make_headroom (b, MAX_HDRS_LEN); |
| 203 | len_to_deq = clib_min (ctx->left_to_snd, ctx->deq_per_first_buf); |
| 204 | |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 205 | if (peek_data) |
| 206 | { |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 207 | n_bytes_read = svm_fifo_peek (ctx->s->server_tx_fifo, ctx->tx_offset, |
| 208 | len_to_deq, data0); |
| 209 | ASSERT (n_bytes_read > 0); |
| 210 | /* Keep track of progress locally, transport is also supposed to |
| 211 | * increment it independently when pushing the header */ |
| 212 | ctx->tx_offset += n_bytes_read; |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 213 | } |
| 214 | else |
| 215 | { |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 216 | if (ctx->transport_vft->tx_type == TRANSPORT_TX_DGRAM) |
| 217 | { |
| 218 | session_dgram_hdr_t *hdr = &ctx->hdr; |
| 219 | svm_fifo_t *f = ctx->s->server_tx_fifo; |
| 220 | u16 deq_now; |
| 221 | u32 offset; |
| 222 | |
| 223 | ASSERT (hdr->data_length > hdr->data_offset); |
| 224 | deq_now = clib_min (hdr->data_length - hdr->data_offset, |
| 225 | len_to_deq); |
| 226 | offset = hdr->data_offset + SESSION_CONN_HDR_LEN; |
| 227 | n_bytes_read = svm_fifo_peek (f, offset, deq_now, data0); |
| 228 | ASSERT (n_bytes_read > 0); |
| 229 | |
| 230 | if (ctx->s->session_state == SESSION_STATE_LISTENING) |
| 231 | { |
| 232 | ip_copy (&ctx->tc->rmt_ip, &hdr->rmt_ip, ctx->tc->is_ip4); |
| 233 | ctx->tc->rmt_port = hdr->rmt_port; |
| 234 | } |
| 235 | hdr->data_offset += n_bytes_read; |
| 236 | if (hdr->data_offset == hdr->data_length) |
| 237 | { |
| 238 | offset = hdr->data_length + SESSION_CONN_HDR_LEN; |
| 239 | svm_fifo_dequeue_drop (f, offset); |
| 240 | } |
| 241 | } |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 242 | else |
| 243 | { |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 244 | n_bytes_read = svm_fifo_dequeue_nowait (ctx->s->server_tx_fifo, |
| 245 | len_to_deq, data0); |
| 246 | ASSERT (n_bytes_read > 0); |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 247 | } |
| 248 | } |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 249 | b->current_length = n_bytes_read; |
| 250 | ctx->left_to_snd -= n_bytes_read; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 251 | |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 252 | /* |
| 253 | * Fill in the remaining buffers in the chain, if any |
| 254 | */ |
| 255 | if (PREDICT_FALSE (ctx->n_bufs_per_seg > 1 && ctx->left_to_snd)) |
| 256 | session_tx_fifo_chain_tail (vm, ctx, b, n_bufs, peek_data); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 257 | |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 258 | /* *INDENT-OFF* */ |
Florin Coras | 8b20bf5 | 2018-06-14 14:55:50 -0700 | [diff] [blame] | 259 | SESSION_EVT_DBG(SESSION_EVT_DEQ, ctx->s, ({ |
| 260 | ed->data[0] = FIFO_EVENT_APP_TX; |
| 261 | ed->data[1] = ctx->max_dequeue; |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 262 | ed->data[2] = len_to_deq; |
Florin Coras | 8b20bf5 | 2018-06-14 14:55:50 -0700 | [diff] [blame] | 263 | ed->data[3] = ctx->left_to_snd; |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 264 | })); |
| 265 | /* *INDENT-ON* */ |
| 266 | } |
| 267 | |
| 268 | always_inline u8 |
| 269 | session_tx_not_ready (stream_session_t * s, u8 peek_data) |
| 270 | { |
| 271 | if (peek_data) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 272 | { |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 273 | /* Can retransmit for closed sessions but can't send new data if |
| 274 | * session is not ready or closed */ |
| 275 | if (s->session_state < SESSION_STATE_READY) |
| 276 | return 1; |
Florin Coras | ca1c8f3 | 2018-05-23 21:01:30 -0700 | [diff] [blame] | 277 | if (s->session_state == SESSION_STATE_CLOSED) |
| 278 | return 2; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 279 | } |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 280 | return 0; |
| 281 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 282 | |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 283 | always_inline transport_connection_t * |
| 284 | session_tx_get_transport (session_tx_context_t * ctx, u8 peek_data) |
| 285 | { |
| 286 | if (peek_data) |
| 287 | { |
| 288 | return ctx->transport_vft->get_connection (ctx->s->connection_index, |
| 289 | ctx->s->thread_index); |
| 290 | } |
| 291 | else |
| 292 | { |
| 293 | if (ctx->s->session_state == SESSION_STATE_LISTENING) |
| 294 | return ctx->transport_vft->get_listener (ctx->s->connection_index); |
| 295 | else |
| 296 | { |
| 297 | return ctx->transport_vft->get_connection (ctx->s->connection_index, |
| 298 | ctx->s->thread_index); |
| 299 | } |
| 300 | } |
| 301 | } |
Florin Coras | 6a9b68b | 2017-11-21 04:20:42 -0800 | [diff] [blame] | 302 | |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 303 | always_inline void |
| 304 | session_tx_set_dequeue_params (vlib_main_t * vm, session_tx_context_t * ctx, |
Florin Coras | f08f26d | 2018-05-10 13:20:47 -0700 | [diff] [blame] | 305 | u32 max_segs, u8 peek_data) |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 306 | { |
| 307 | u32 n_bytes_per_buf, n_bytes_per_seg; |
| 308 | ctx->max_dequeue = svm_fifo_max_dequeue (ctx->s->server_tx_fifo); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 309 | if (peek_data) |
| 310 | { |
Florin Coras | 9d06304 | 2017-09-14 03:08:00 -0400 | [diff] [blame] | 311 | /* Offset in rx fifo from where to peek data */ |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 312 | ctx->tx_offset = ctx->transport_vft->tx_fifo_offset (ctx->tc); |
| 313 | if (PREDICT_FALSE (ctx->tx_offset >= ctx->max_dequeue)) |
| 314 | { |
| 315 | ctx->max_len_to_snd = 0; |
| 316 | return; |
| 317 | } |
| 318 | ctx->max_dequeue -= ctx->tx_offset; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 319 | } |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 320 | else |
| 321 | { |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 322 | if (ctx->transport_vft->tx_type == TRANSPORT_TX_DGRAM) |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 323 | { |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 324 | if (ctx->max_dequeue <= sizeof (ctx->hdr)) |
| 325 | { |
| 326 | ctx->max_len_to_snd = 0; |
| 327 | return; |
| 328 | } |
| 329 | svm_fifo_peek (ctx->s->server_tx_fifo, 0, sizeof (ctx->hdr), |
| 330 | (u8 *) & ctx->hdr); |
| 331 | ASSERT (ctx->hdr.data_length > ctx->hdr.data_offset); |
| 332 | ctx->max_dequeue = ctx->hdr.data_length - ctx->hdr.data_offset; |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 333 | } |
| 334 | } |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 335 | ASSERT (ctx->max_dequeue > 0); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 336 | |
| 337 | /* Ensure we're not writing more than transport window allows */ |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 338 | if (ctx->max_dequeue < ctx->snd_space) |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 339 | { |
| 340 | /* Constrained by tx queue. Try to send only fully formed segments */ |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 341 | ctx->max_len_to_snd = |
| 342 | (ctx->max_dequeue > ctx->snd_mss) ? |
| 343 | ctx->max_dequeue - ctx->max_dequeue % ctx->snd_mss : ctx->max_dequeue; |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 344 | /* TODO Nagle ? */ |
| 345 | } |
| 346 | else |
| 347 | { |
Florin Coras | 1f152cd | 2017-08-18 19:28:03 -0700 | [diff] [blame] | 348 | /* Expectation is that snd_space0 is already a multiple of snd_mss */ |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 349 | ctx->max_len_to_snd = ctx->snd_space; |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 350 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 351 | |
Florin Coras | f08f26d | 2018-05-10 13:20:47 -0700 | [diff] [blame] | 352 | /* Check if we're tx constrained by the node */ |
| 353 | ctx->n_segs_per_evt = ceil ((f64) ctx->max_len_to_snd / ctx->snd_mss); |
| 354 | if (ctx->n_segs_per_evt > max_segs) |
| 355 | { |
| 356 | ctx->n_segs_per_evt = max_segs; |
| 357 | ctx->max_len_to_snd = max_segs * ctx->snd_mss; |
| 358 | } |
| 359 | |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 360 | n_bytes_per_buf = vlib_buffer_free_list_buffer_size (vm, |
| 361 | VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX); |
Florin Coras | e87216f | 2017-08-17 16:59:22 -0700 | [diff] [blame] | 362 | ASSERT (n_bytes_per_buf > MAX_HDRS_LEN); |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 363 | n_bytes_per_seg = MAX_HDRS_LEN + ctx->snd_mss; |
Florin Coras | f08f26d | 2018-05-10 13:20:47 -0700 | [diff] [blame] | 364 | ctx->n_bufs_per_seg = ceil ((f64) n_bytes_per_seg / n_bytes_per_buf); |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 365 | ctx->deq_per_buf = clib_min (ctx->snd_mss, n_bytes_per_buf); |
| 366 | ctx->deq_per_first_buf = clib_min (ctx->snd_mss, |
| 367 | n_bytes_per_buf - MAX_HDRS_LEN); |
| 368 | } |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 369 | |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 370 | always_inline int |
| 371 | session_tx_fifo_read_and_snd_i (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 372 | session_fifo_event_t * e, |
| 373 | stream_session_t * s, int *n_tx_packets, |
| 374 | u8 peek_data) |
| 375 | { |
Florin Coras | 66cf5e0 | 2018-06-08 09:17:39 -0700 | [diff] [blame] | 376 | u32 next_index, next0, next1, *to_next, n_left_to_next; |
Florin Coras | f08f26d | 2018-05-10 13:20:47 -0700 | [diff] [blame] | 377 | u32 n_trace = vlib_get_trace_count (vm, node), n_bufs_needed = 0; |
| 378 | u32 thread_index = s->thread_index, n_left, pbi; |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 379 | session_manager_main_t *smm = &session_manager_main; |
| 380 | session_tx_context_t *ctx = &smm->ctx[thread_index]; |
| 381 | transport_proto_t tp; |
| 382 | vlib_buffer_t *pb; |
Florin Coras | ca1c8f3 | 2018-05-23 21:01:30 -0700 | [diff] [blame] | 383 | u16 n_bufs, rv; |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 384 | |
Florin Coras | ca1c8f3 | 2018-05-23 21:01:30 -0700 | [diff] [blame] | 385 | if (PREDICT_FALSE ((rv = session_tx_not_ready (s, peek_data)))) |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 386 | { |
Florin Coras | ca1c8f3 | 2018-05-23 21:01:30 -0700 | [diff] [blame] | 387 | if (rv < 2) |
| 388 | vec_add1 (smm->pending_event_vector[thread_index], *e); |
Florin Coras | 0d60a0f | 2018-06-29 02:02:08 -0700 | [diff] [blame] | 389 | return SESSION_TX_NO_DATA; |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | next_index = smm->session_type_to_next[s->session_type]; |
Florin Coras | 66cf5e0 | 2018-06-08 09:17:39 -0700 | [diff] [blame] | 393 | next0 = next1 = next_index; |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 394 | |
| 395 | tp = session_get_transport_proto (s); |
| 396 | ctx->s = s; |
| 397 | ctx->transport_vft = transport_protocol_get_vft (tp); |
| 398 | ctx->tc = session_tx_get_transport (ctx, peek_data); |
| 399 | ctx->snd_mss = ctx->transport_vft->send_mss (ctx->tc); |
| 400 | ctx->snd_space = ctx->transport_vft->send_space (ctx->tc); |
| 401 | if (ctx->snd_space == 0 || ctx->snd_mss == 0) |
| 402 | { |
| 403 | vec_add1 (smm->pending_event_vector[thread_index], *e); |
Florin Coras | 0d60a0f | 2018-06-29 02:02:08 -0700 | [diff] [blame] | 404 | return SESSION_TX_NO_DATA; |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | /* Allow enqueuing of a new event */ |
| 408 | svm_fifo_unset_event (s->server_tx_fifo); |
| 409 | |
| 410 | /* Check how much we can pull. */ |
Florin Coras | f08f26d | 2018-05-10 13:20:47 -0700 | [diff] [blame] | 411 | session_tx_set_dequeue_params (vm, ctx, VLIB_FRAME_SIZE - *n_tx_packets, |
| 412 | peek_data); |
| 413 | |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 414 | if (PREDICT_FALSE (!ctx->max_len_to_snd)) |
Florin Coras | 0d60a0f | 2018-06-29 02:02:08 -0700 | [diff] [blame] | 415 | return SESSION_TX_NO_DATA; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 416 | |
| 417 | n_bufs = vec_len (smm->tx_buffers[thread_index]); |
Florin Coras | f08f26d | 2018-05-10 13:20:47 -0700 | [diff] [blame] | 418 | n_bufs_needed = ctx->n_segs_per_evt * ctx->n_bufs_per_seg; |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 419 | |
| 420 | /* |
| 421 | * Make sure we have at least one full frame of buffers ready |
| 422 | */ |
Florin Coras | f08f26d | 2018-05-10 13:20:47 -0700 | [diff] [blame] | 423 | if (n_bufs < n_bufs_needed) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 424 | { |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 425 | session_output_try_get_buffers (vm, smm, thread_index, &n_bufs, |
Florin Coras | f08f26d | 2018-05-10 13:20:47 -0700 | [diff] [blame] | 426 | ctx->n_bufs_per_seg * VLIB_FRAME_SIZE); |
| 427 | if (PREDICT_FALSE (n_bufs < n_bufs_needed)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 428 | { |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 429 | vec_add1 (smm->pending_event_vector[thread_index], *e); |
Florin Coras | 0d60a0f | 2018-06-29 02:02:08 -0700 | [diff] [blame] | 430 | return SESSION_TX_NO_BUFFERS; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 431 | } |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 432 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 433 | |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 434 | /* |
| 435 | * Write until we fill up a frame |
| 436 | */ |
| 437 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
Florin Coras | f08f26d | 2018-05-10 13:20:47 -0700 | [diff] [blame] | 438 | if (PREDICT_FALSE (ctx->n_segs_per_evt > n_left_to_next)) |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 439 | { |
Florin Coras | f08f26d | 2018-05-10 13:20:47 -0700 | [diff] [blame] | 440 | ctx->n_segs_per_evt = n_left_to_next; |
| 441 | ctx->max_len_to_snd = ctx->snd_mss * n_left_to_next; |
| 442 | } |
| 443 | ctx->left_to_snd = ctx->max_len_to_snd; |
| 444 | n_left = ctx->n_segs_per_evt; |
Florin Coras | 66cf5e0 | 2018-06-08 09:17:39 -0700 | [diff] [blame] | 445 | |
| 446 | while (n_left >= 4) |
| 447 | { |
| 448 | vlib_buffer_t *b0, *b1; |
| 449 | u32 bi0, bi1; |
| 450 | |
| 451 | pbi = smm->tx_buffers[thread_index][n_bufs - 3]; |
| 452 | pb = vlib_get_buffer (vm, pbi); |
| 453 | vlib_prefetch_buffer_header (pb, STORE); |
| 454 | pbi = smm->tx_buffers[thread_index][n_bufs - 4]; |
| 455 | pb = vlib_get_buffer (vm, pbi); |
| 456 | vlib_prefetch_buffer_header (pb, STORE); |
| 457 | |
| 458 | to_next[0] = bi0 = smm->tx_buffers[thread_index][--n_bufs]; |
| 459 | to_next[1] = bi1 = smm->tx_buffers[thread_index][--n_bufs]; |
| 460 | |
| 461 | b0 = vlib_get_buffer (vm, bi0); |
| 462 | b1 = vlib_get_buffer (vm, bi1); |
| 463 | |
| 464 | session_tx_fill_buffer (vm, ctx, b0, &n_bufs, peek_data); |
| 465 | session_tx_fill_buffer (vm, ctx, b1, &n_bufs, peek_data); |
| 466 | |
| 467 | ctx->transport_vft->push_header (ctx->tc, b0); |
| 468 | ctx->transport_vft->push_header (ctx->tc, b1); |
| 469 | |
| 470 | to_next += 2; |
| 471 | n_left_to_next -= 2; |
| 472 | n_left -= 2; |
| 473 | |
| 474 | VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0); |
| 475 | VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b1); |
| 476 | |
| 477 | vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next, |
| 478 | n_left_to_next, bi0, bi1, next0, |
| 479 | next1); |
| 480 | } |
Florin Coras | f08f26d | 2018-05-10 13:20:47 -0700 | [diff] [blame] | 481 | while (n_left) |
| 482 | { |
Florin Coras | 66cf5e0 | 2018-06-08 09:17:39 -0700 | [diff] [blame] | 483 | vlib_buffer_t *b0; |
| 484 | u32 bi0; |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 485 | |
Florin Coras | 91ca462 | 2018-06-30 11:27:59 -0700 | [diff] [blame] | 486 | if (n_left > 1) |
| 487 | { |
| 488 | pbi = smm->tx_buffers[thread_index][n_bufs - 2]; |
| 489 | pb = vlib_get_buffer (vm, pbi); |
| 490 | vlib_prefetch_buffer_header (pb, STORE); |
| 491 | } |
| 492 | |
Florin Coras | 66cf5e0 | 2018-06-08 09:17:39 -0700 | [diff] [blame] | 493 | to_next[0] = bi0 = smm->tx_buffers[thread_index][--n_bufs]; |
| 494 | b0 = vlib_get_buffer (vm, bi0); |
| 495 | session_tx_fill_buffer (vm, ctx, b0, &n_bufs, peek_data); |
Florin Coras | 81a13db | 2018-03-16 08:48:31 -0700 | [diff] [blame] | 496 | |
Florin Coras | 66cf5e0 | 2018-06-08 09:17:39 -0700 | [diff] [blame] | 497 | /* Ask transport to push header after current_length and |
| 498 | * total_length_not_including_first_buffer are updated */ |
| 499 | ctx->transport_vft->push_header (ctx->tc, b0); |
Florin Coras | f6359c8 | 2017-06-19 12:26:09 -0400 | [diff] [blame] | 500 | |
Florin Coras | 66cf5e0 | 2018-06-08 09:17:39 -0700 | [diff] [blame] | 501 | to_next += 1; |
| 502 | n_left_to_next -= 1; |
| 503 | n_left -= 1; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 504 | |
Florin Coras | 66cf5e0 | 2018-06-08 09:17:39 -0700 | [diff] [blame] | 505 | VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0); |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 506 | |
Florin Coras | 66cf5e0 | 2018-06-08 09:17:39 -0700 | [diff] [blame] | 507 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, |
| 508 | n_left_to_next, bi0, next0); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 509 | } |
Florin Coras | 66cf5e0 | 2018-06-08 09:17:39 -0700 | [diff] [blame] | 510 | |
| 511 | if (PREDICT_FALSE (n_trace > 0)) |
| 512 | session_tx_trace_frame (vm, node, next_index, to_next, |
| 513 | ctx->n_segs_per_evt, s, n_trace); |
| 514 | |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 515 | _vec_len (smm->tx_buffers[thread_index]) = n_bufs; |
Florin Coras | f08f26d | 2018-05-10 13:20:47 -0700 | [diff] [blame] | 516 | *n_tx_packets += ctx->n_segs_per_evt; |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 517 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 518 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 519 | /* If we couldn't dequeue all bytes mark as partially read */ |
Florin Coras | f08f26d | 2018-05-10 13:20:47 -0700 | [diff] [blame] | 520 | ASSERT (ctx->left_to_snd == 0); |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 521 | if (ctx->max_len_to_snd < ctx->max_dequeue) |
| 522 | if (svm_fifo_set_event (s->server_tx_fifo)) |
| 523 | vec_add1 (smm->pending_event_vector[thread_index], *e); |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 524 | |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 525 | if (!peek_data && ctx->transport_vft->tx_type == TRANSPORT_TX_DGRAM) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 526 | { |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 527 | /* Fix dgram pre header */ |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 528 | if (ctx->max_len_to_snd < ctx->max_dequeue) |
| 529 | svm_fifo_overwrite_head (s->server_tx_fifo, (u8 *) & ctx->hdr, |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 530 | sizeof (session_dgram_pre_hdr_t)); |
| 531 | /* More data needs to be read */ |
Florin Coras | 8e43d04 | 2018-05-04 15:46:57 -0700 | [diff] [blame] | 532 | else if (svm_fifo_max_dequeue (s->server_tx_fifo) > 0) |
| 533 | if (svm_fifo_set_event (s->server_tx_fifo)) |
| 534 | vec_add1 (smm->pending_event_vector[thread_index], *e); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 535 | } |
Florin Coras | 0d60a0f | 2018-06-29 02:02:08 -0700 | [diff] [blame] | 536 | return SESSION_TX_OK; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | int |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 540 | session_tx_fifo_peek_and_snd (vlib_main_t * vm, vlib_node_runtime_t * node, |
Florin Coras | 0d60a0f | 2018-06-29 02:02:08 -0700 | [diff] [blame] | 541 | session_fifo_event_t * e, |
| 542 | stream_session_t * s, int *n_tx_pkts) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 543 | { |
Florin Coras | 0d60a0f | 2018-06-29 02:02:08 -0700 | [diff] [blame] | 544 | return session_tx_fifo_read_and_snd_i (vm, node, e, s, n_tx_pkts, 1); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | int |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 548 | session_tx_fifo_dequeue_and_snd (vlib_main_t * vm, vlib_node_runtime_t * node, |
Florin Coras | 0d60a0f | 2018-06-29 02:02:08 -0700 | [diff] [blame] | 549 | session_fifo_event_t * e, |
| 550 | stream_session_t * s, int *n_tx_pkts) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 551 | { |
Florin Coras | 0d60a0f | 2018-06-29 02:02:08 -0700 | [diff] [blame] | 552 | return session_tx_fifo_read_and_snd_i (vm, node, e, s, n_tx_pkts, 0); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 553 | } |
| 554 | |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 555 | int |
| 556 | session_tx_fifo_dequeue_internal (vlib_main_t * vm, |
| 557 | vlib_node_runtime_t * node, |
Florin Coras | 0d60a0f | 2018-06-29 02:02:08 -0700 | [diff] [blame] | 558 | session_fifo_event_t * e, |
| 559 | stream_session_t * s, int *n_tx_pkts) |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 560 | { |
| 561 | application_t *app; |
Florin Coras | 0d60a0f | 2018-06-29 02:02:08 -0700 | [diff] [blame] | 562 | app = application_get (s->opaque); |
| 563 | svm_fifo_unset_event (s->server_tx_fifo); |
| 564 | return app->cb_fns.builtin_app_tx_callback (s); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 565 | } |
| 566 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 567 | always_inline stream_session_t * |
| 568 | session_event_get_session (session_fifo_event_t * e, u8 thread_index) |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 569 | { |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 570 | return session_get_if_valid (e->fifo->master_session_index, thread_index); |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 571 | } |
| 572 | |
Florin Coras | 0d60a0f | 2018-06-29 02:02:08 -0700 | [diff] [blame] | 573 | static uword |
| 574 | session_queue_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 575 | vlib_frame_t * frame) |
| 576 | { |
| 577 | session_manager_main_t *smm = vnet_get_session_manager_main (); |
| 578 | session_fifo_event_t *pending_events, *e; |
| 579 | session_fifo_event_t *fifo_events; |
| 580 | u32 n_to_dequeue, n_events; |
| 581 | svm_queue_t *q; |
| 582 | application_t *app; |
| 583 | int n_tx_packets = 0; |
| 584 | u32 thread_index = vm->thread_index; |
| 585 | int i, rv; |
| 586 | f64 now = vlib_time_now (vm); |
| 587 | void (*fp) (void *); |
| 588 | |
| 589 | SESSION_EVT_DBG (SESSION_EVT_POLL_GAP_TRACK, smm, thread_index); |
| 590 | |
| 591 | /* |
| 592 | * Update transport time |
| 593 | */ |
| 594 | transport_update_time (now, thread_index); |
| 595 | |
| 596 | /* |
| 597 | * Get vpp queue events |
| 598 | */ |
| 599 | q = smm->vpp_event_queues[thread_index]; |
| 600 | if (PREDICT_FALSE (q == 0)) |
| 601 | return 0; |
| 602 | |
| 603 | fifo_events = smm->free_event_vector[thread_index]; |
| 604 | |
| 605 | /* min number of events we can dequeue without blocking */ |
| 606 | n_to_dequeue = q->cursize; |
| 607 | pending_events = smm->pending_event_vector[thread_index]; |
| 608 | |
| 609 | if (!n_to_dequeue && !vec_len (pending_events) |
| 610 | && !vec_len (smm->pending_disconnects[thread_index])) |
| 611 | return 0; |
| 612 | |
| 613 | SESSION_EVT_DBG (SESSION_EVT_DEQ_NODE, 0); |
| 614 | |
| 615 | /* |
| 616 | * If we didn't manage to process previous events try going |
| 617 | * over them again without dequeuing new ones. |
| 618 | * XXX: Handle senders to sessions that can't keep up |
| 619 | */ |
| 620 | if (0 && vec_len (pending_events) >= 100) |
| 621 | { |
| 622 | clib_warning ("too many fifo events unsolved"); |
| 623 | goto skip_dequeue; |
| 624 | } |
| 625 | |
| 626 | /* See you in the next life, don't be late |
| 627 | * XXX: we may need priorities here |
| 628 | */ |
| 629 | if (pthread_mutex_trylock (&q->mutex)) |
| 630 | return 0; |
| 631 | |
| 632 | for (i = 0; i < n_to_dequeue; i++) |
| 633 | { |
| 634 | vec_add2 (fifo_events, e, 1); |
| 635 | svm_queue_sub_raw (q, (u8 *) e); |
| 636 | } |
| 637 | |
| 638 | /* The other side of the connection is not polling */ |
| 639 | if (q->cursize < (q->maxsize / 8)) |
| 640 | (void) pthread_cond_broadcast (&q->condvar); |
| 641 | pthread_mutex_unlock (&q->mutex); |
| 642 | |
| 643 | vec_append (fifo_events, pending_events); |
| 644 | vec_append (fifo_events, smm->pending_disconnects[thread_index]); |
| 645 | |
| 646 | _vec_len (pending_events) = 0; |
| 647 | smm->pending_event_vector[thread_index] = pending_events; |
| 648 | _vec_len (smm->pending_disconnects[thread_index]) = 0; |
| 649 | |
| 650 | skip_dequeue: |
| 651 | n_events = vec_len (fifo_events); |
| 652 | for (i = 0; i < n_events; i++) |
| 653 | { |
| 654 | stream_session_t *s; /* $$$ prefetch 1 ahead maybe */ |
| 655 | session_fifo_event_t *e; |
Florin Coras | 46f001d | 2018-07-11 05:25:06 -0700 | [diff] [blame] | 656 | u32 to_dequeue; |
Florin Coras | 0d60a0f | 2018-06-29 02:02:08 -0700 | [diff] [blame] | 657 | |
| 658 | e = &fifo_events[i]; |
| 659 | switch (e->event_type) |
| 660 | { |
| 661 | case FIFO_EVENT_APP_TX: |
| 662 | /* Don't try to send more that one frame per dispatch cycle */ |
| 663 | if (n_tx_packets == VLIB_FRAME_SIZE) |
| 664 | { |
| 665 | vec_add1 (smm->pending_event_vector[thread_index], *e); |
| 666 | break; |
| 667 | } |
| 668 | |
| 669 | s = session_event_get_session (e, thread_index); |
| 670 | if (PREDICT_FALSE (!s)) |
| 671 | { |
| 672 | clib_warning ("It's dead, Jim!"); |
| 673 | continue; |
| 674 | } |
Florin Coras | 46f001d | 2018-07-11 05:25:06 -0700 | [diff] [blame] | 675 | to_dequeue = svm_fifo_max_dequeue (s->server_tx_fifo); |
Florin Coras | 0d60a0f | 2018-06-29 02:02:08 -0700 | [diff] [blame] | 676 | |
| 677 | /* Spray packets in per session type frames, since they go to |
| 678 | * different nodes */ |
| 679 | rv = (smm->session_tx_fns[s->session_type]) (vm, node, e, s, |
| 680 | &n_tx_packets); |
| 681 | if (PREDICT_TRUE (rv == SESSION_TX_OK)) |
| 682 | { |
Florin Coras | 46f001d | 2018-07-11 05:25:06 -0700 | [diff] [blame] | 683 | /* Notify app there's tx space if not polling */ |
| 684 | if (PREDICT_FALSE (to_dequeue == s->server_tx_fifo->nitems |
| 685 | && !svm_fifo_has_event (s->server_tx_fifo))) |
| 686 | session_dequeue_notify (s); |
Florin Coras | 0d60a0f | 2018-06-29 02:02:08 -0700 | [diff] [blame] | 687 | } |
| 688 | else if (PREDICT_FALSE (rv == SESSION_TX_NO_BUFFERS)) |
| 689 | { |
| 690 | vlib_node_increment_counter (vm, node->node_index, |
| 691 | SESSION_QUEUE_ERROR_NO_BUFFER, 1); |
| 692 | continue; |
| 693 | } |
| 694 | break; |
| 695 | case FIFO_EVENT_DISCONNECT: |
| 696 | /* Make sure stream disconnects run after the pending list is |
| 697 | * drained */ |
| 698 | s = session_get_from_handle (e->session_handle); |
| 699 | if (!e->postponed) |
| 700 | { |
| 701 | e->postponed = 1; |
| 702 | vec_add1 (smm->pending_disconnects[thread_index], *e); |
| 703 | continue; |
| 704 | } |
| 705 | /* If tx queue is still not empty, wait */ |
| 706 | if (svm_fifo_max_dequeue (s->server_tx_fifo)) |
| 707 | { |
| 708 | vec_add1 (smm->pending_disconnects[thread_index], *e); |
| 709 | continue; |
| 710 | } |
| 711 | |
| 712 | stream_session_disconnect_transport (s); |
| 713 | break; |
| 714 | case FIFO_EVENT_BUILTIN_RX: |
| 715 | s = session_event_get_session (e, thread_index); |
| 716 | if (PREDICT_FALSE (!s)) |
| 717 | continue; |
| 718 | svm_fifo_unset_event (s->server_rx_fifo); |
| 719 | app = application_get (s->app_index); |
| 720 | app->cb_fns.builtin_app_rx_callback (s); |
| 721 | break; |
| 722 | case FIFO_EVENT_RPC: |
| 723 | fp = e->rpc_args.fp; |
| 724 | (*fp) (e->rpc_args.arg); |
| 725 | break; |
| 726 | |
| 727 | default: |
| 728 | clib_warning ("unhandled event type %d", e->event_type); |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | _vec_len (fifo_events) = 0; |
| 733 | smm->free_event_vector[thread_index] = fifo_events; |
| 734 | |
| 735 | vlib_node_increment_counter (vm, session_queue_node.index, |
| 736 | SESSION_QUEUE_ERROR_TX, n_tx_packets); |
| 737 | |
| 738 | SESSION_EVT_DBG (SESSION_EVT_DISPATCH_END, smm, thread_index); |
| 739 | |
| 740 | return n_tx_packets; |
| 741 | } |
| 742 | |
| 743 | /* *INDENT-OFF* */ |
| 744 | VLIB_REGISTER_NODE (session_queue_node) = |
| 745 | { |
| 746 | .function = session_queue_node_fn, |
| 747 | .name = "session-queue", |
| 748 | .format_trace = format_session_queue_trace, |
| 749 | .type = VLIB_NODE_TYPE_INPUT, |
| 750 | .n_errors = ARRAY_LEN (session_queue_error_strings), |
| 751 | .error_strings = session_queue_error_strings, |
| 752 | .state = VLIB_NODE_STATE_DISABLED, |
| 753 | }; |
| 754 | /* *INDENT-ON* */ |
| 755 | |
Dave Barach | acd2a6a | 2017-05-16 17:41:34 -0400 | [diff] [blame] | 756 | void |
| 757 | dump_thread_0_event_queue (void) |
| 758 | { |
| 759 | session_manager_main_t *smm = vnet_get_session_manager_main (); |
| 760 | vlib_main_t *vm = &vlib_global_main; |
| 761 | u32 my_thread_index = vm->thread_index; |
| 762 | session_fifo_event_t _e, *e = &_e; |
| 763 | stream_session_t *s0; |
| 764 | int i, index; |
| 765 | i8 *headp; |
| 766 | |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 767 | svm_queue_t *q; |
Dave Barach | acd2a6a | 2017-05-16 17:41:34 -0400 | [diff] [blame] | 768 | q = smm->vpp_event_queues[my_thread_index]; |
| 769 | |
| 770 | index = q->head; |
| 771 | |
| 772 | for (i = 0; i < q->cursize; i++) |
| 773 | { |
| 774 | headp = (i8 *) (&q->data[0] + q->elsize * index); |
| 775 | clib_memcpy (e, headp, q->elsize); |
| 776 | |
| 777 | switch (e->event_type) |
| 778 | { |
| 779 | case FIFO_EVENT_APP_TX: |
| 780 | s0 = session_event_get_session (e, my_thread_index); |
| 781 | fformat (stdout, "[%04d] TX session %d\n", i, s0->session_index); |
| 782 | break; |
| 783 | |
| 784 | case FIFO_EVENT_DISCONNECT: |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 785 | s0 = session_get_from_handle (e->session_handle); |
Dave Barach | acd2a6a | 2017-05-16 17:41:34 -0400 | [diff] [blame] | 786 | fformat (stdout, "[%04d] disconnect session %d\n", i, |
| 787 | s0->session_index); |
| 788 | break; |
| 789 | |
| 790 | case FIFO_EVENT_BUILTIN_RX: |
| 791 | s0 = session_event_get_session (e, my_thread_index); |
| 792 | fformat (stdout, "[%04d] builtin_rx %d\n", i, s0->session_index); |
| 793 | break; |
| 794 | |
| 795 | case FIFO_EVENT_RPC: |
| 796 | fformat (stdout, "[%04d] RPC call %llx with %llx\n", |
| 797 | i, (u64) (e->rpc_args.fp), (u64) (e->rpc_args.arg)); |
| 798 | break; |
| 799 | |
| 800 | default: |
| 801 | fformat (stdout, "[%04d] unhandled event type %d\n", |
| 802 | i, e->event_type); |
| 803 | break; |
| 804 | } |
| 805 | |
| 806 | index++; |
| 807 | |
| 808 | if (index == q->maxsize) |
| 809 | index = 0; |
| 810 | } |
| 811 | } |
| 812 | |
Florin Coras | 6534b7a | 2017-07-18 05:38:03 -0400 | [diff] [blame] | 813 | static u8 |
| 814 | session_node_cmp_event (session_fifo_event_t * e, svm_fifo_t * f) |
| 815 | { |
| 816 | stream_session_t *s; |
| 817 | switch (e->event_type) |
| 818 | { |
| 819 | case FIFO_EVENT_APP_RX: |
| 820 | case FIFO_EVENT_APP_TX: |
| 821 | case FIFO_EVENT_BUILTIN_RX: |
| 822 | if (e->fifo == f) |
| 823 | return 1; |
| 824 | break; |
| 825 | case FIFO_EVENT_DISCONNECT: |
| 826 | break; |
| 827 | case FIFO_EVENT_RPC: |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 828 | s = session_get_from_handle (e->session_handle); |
Florin Coras | 6534b7a | 2017-07-18 05:38:03 -0400 | [diff] [blame] | 829 | if (!s) |
| 830 | { |
| 831 | clib_warning ("session has event but doesn't exist!"); |
| 832 | break; |
| 833 | } |
| 834 | if (s->server_rx_fifo == f || s->server_tx_fifo == f) |
| 835 | return 1; |
| 836 | break; |
| 837 | default: |
| 838 | break; |
| 839 | } |
| 840 | return 0; |
| 841 | } |
| 842 | |
| 843 | u8 |
| 844 | session_node_lookup_fifo_event (svm_fifo_t * f, session_fifo_event_t * e) |
| 845 | { |
| 846 | session_manager_main_t *smm = vnet_get_session_manager_main (); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 847 | svm_queue_t *q; |
Florin Coras | 6534b7a | 2017-07-18 05:38:03 -0400 | [diff] [blame] | 848 | session_fifo_event_t *pending_event_vector, *evt; |
| 849 | int i, index, found = 0; |
| 850 | i8 *headp; |
| 851 | u8 thread_index; |
| 852 | |
| 853 | ASSERT (e); |
| 854 | thread_index = f->master_thread_index; |
| 855 | /* |
| 856 | * Search evt queue |
| 857 | */ |
| 858 | q = smm->vpp_event_queues[thread_index]; |
| 859 | index = q->head; |
| 860 | for (i = 0; i < q->cursize; i++) |
| 861 | { |
| 862 | headp = (i8 *) (&q->data[0] + q->elsize * index); |
| 863 | clib_memcpy (e, headp, q->elsize); |
| 864 | found = session_node_cmp_event (e, f); |
| 865 | if (found) |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 866 | return 1; |
Florin Coras | 6534b7a | 2017-07-18 05:38:03 -0400 | [diff] [blame] | 867 | if (++index == q->maxsize) |
| 868 | index = 0; |
| 869 | } |
| 870 | /* |
| 871 | * Search pending events vector |
| 872 | */ |
| 873 | pending_event_vector = smm->pending_event_vector[thread_index]; |
| 874 | vec_foreach (evt, pending_event_vector) |
| 875 | { |
| 876 | found = session_node_cmp_event (evt, f); |
| 877 | if (found) |
| 878 | { |
| 879 | clib_memcpy (e, evt, sizeof (*evt)); |
| 880 | break; |
| 881 | } |
| 882 | } |
| 883 | return found; |
| 884 | } |
| 885 | |
Dave Barach | 2a86391 | 2017-11-28 10:11:42 -0500 | [diff] [blame] | 886 | static clib_error_t * |
| 887 | session_queue_exit (vlib_main_t * vm) |
| 888 | { |
| 889 | if (vec_len (vlib_mains) < 2) |
| 890 | return 0; |
| 891 | |
| 892 | /* |
| 893 | * Shut off (especially) worker-thread session nodes. |
| 894 | * Otherwise, vpp can crash as the main thread unmaps the |
| 895 | * API segment. |
| 896 | */ |
| 897 | vlib_worker_thread_barrier_sync (vm); |
| 898 | session_node_enable_disable (0 /* is_enable */ ); |
| 899 | vlib_worker_thread_barrier_release (vm); |
| 900 | return 0; |
| 901 | } |
| 902 | |
| 903 | VLIB_MAIN_LOOP_EXIT_FUNCTION (session_queue_exit); |
| 904 | |
Florin Coras | fd542f1 | 2018-05-16 19:28:24 -0700 | [diff] [blame] | 905 | static uword |
| 906 | session_queue_process (vlib_main_t * vm, vlib_node_runtime_t * rt, |
| 907 | vlib_frame_t * f) |
| 908 | { |
| 909 | f64 now, timeout = 1.0; |
| 910 | uword *event_data = 0; |
| 911 | uword event_type; |
| 912 | |
| 913 | while (1) |
| 914 | { |
| 915 | vlib_process_wait_for_event_or_clock (vm, timeout); |
| 916 | now = vlib_time_now (vm); |
| 917 | event_type = vlib_process_get_events (vm, (uword **) & event_data); |
| 918 | |
| 919 | switch (event_type) |
| 920 | { |
| 921 | case SESSION_Q_PROCESS_FLUSH_FRAMES: |
| 922 | /* Flush the frames by updating all transports times */ |
| 923 | transport_update_time (now, 0); |
| 924 | break; |
| 925 | case SESSION_Q_PROCESS_STOP: |
| 926 | timeout = 100000.0; |
| 927 | break; |
| 928 | case ~0: |
| 929 | /* Timed out. Update time for all transports to trigger all |
| 930 | * outstanding retransmits. */ |
| 931 | transport_update_time (now, 0); |
| 932 | break; |
| 933 | } |
| 934 | vec_reset_length (event_data); |
| 935 | } |
| 936 | return 0; |
| 937 | } |
| 938 | |
| 939 | /* *INDENT-OFF* */ |
| 940 | VLIB_REGISTER_NODE (session_queue_process_node) = |
| 941 | { |
| 942 | .function = session_queue_process, |
| 943 | .type = VLIB_NODE_TYPE_PROCESS, |
| 944 | .name = "session-queue-process", |
| 945 | .state = VLIB_NODE_STATE_DISABLED, |
| 946 | }; |
| 947 | /* *INDENT-ON* */ |
| 948 | |
| 949 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 950 | /* |
| 951 | * fd.io coding-style-patch-verification: ON |
| 952 | * |
| 953 | * Local Variables: |
| 954 | * eval: (c-set-style "gnu") |
| 955 | * End: |
| 956 | */ |