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