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