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