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