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