Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2019 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 | |
| 16 | #include <vnet/session/application_local.h> |
| 17 | #include <vnet/session/session.h> |
| 18 | |
Florin Coras | da78c5a | 2021-06-09 14:55:24 -0700 | [diff] [blame] | 19 | typedef enum ct_segment_flags_ |
| 20 | { |
| 21 | CT_SEGMENT_F_CLIENT_DETACHED = 1 << 0, |
| 22 | CT_SEGMENT_F_SERVER_DETACHED = 1 << 1, |
| 23 | } ct_segment_flags_t; |
| 24 | |
| 25 | typedef struct ct_segment_ |
| 26 | { |
Florin Coras | da78c5a | 2021-06-09 14:55:24 -0700 | [diff] [blame] | 27 | u32 client_n_sessions; |
| 28 | u32 server_n_sessions; |
Florin Coras | 6973c73 | 2021-06-17 15:53:38 -0700 | [diff] [blame] | 29 | u32 seg_ctx_index; |
| 30 | u32 ct_seg_index; |
| 31 | u32 segment_index; |
Florin Coras | da78c5a | 2021-06-09 14:55:24 -0700 | [diff] [blame] | 32 | ct_segment_flags_t flags; |
| 33 | } ct_segment_t; |
| 34 | |
| 35 | typedef struct ct_segments_ |
| 36 | { |
| 37 | u32 sm_index; |
| 38 | u32 server_wrk; |
| 39 | u32 client_wrk; |
Florin Coras | 6973c73 | 2021-06-17 15:53:38 -0700 | [diff] [blame] | 40 | u32 fifo_pair_bytes; |
Florin Coras | da78c5a | 2021-06-09 14:55:24 -0700 | [diff] [blame] | 41 | ct_segment_t *segments; |
| 42 | } ct_segments_ctx_t; |
| 43 | |
Florin Coras | 440c7b5 | 2021-11-09 08:38:24 -0800 | [diff] [blame^] | 44 | typedef struct ct_cleanup_req_ |
| 45 | { |
| 46 | u32 ct_index; |
| 47 | } ct_cleanup_req_t; |
| 48 | |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 49 | typedef struct ct_main_ |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 50 | { |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 51 | ct_connection_t **connections; /**< Per-worker connection pools */ |
Florin Coras | 440c7b5 | 2021-11-09 08:38:24 -0800 | [diff] [blame^] | 52 | ct_cleanup_req_t **pending_cleanups; |
| 53 | u8 *heave_cleanups; |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 54 | u32 n_workers; /**< Number of vpp workers */ |
| 55 | u32 n_sessions; /**< Cumulative sessions counter */ |
Florin Coras | 374df7a | 2021-05-13 11:37:43 -0700 | [diff] [blame] | 56 | u32 *ho_reusable; /**< Vector of reusable ho indices */ |
| 57 | clib_spinlock_t ho_reuseable_lock; /**< Lock for reusable ho indices */ |
Florin Coras | da78c5a | 2021-06-09 14:55:24 -0700 | [diff] [blame] | 58 | clib_rwlock_t app_segs_lock; /**< RW lock for seg contexts */ |
| 59 | uword *app_segs_ctxs_table; /**< App handle to segment pool map */ |
| 60 | ct_segments_ctx_t *app_seg_ctxs; /**< Pool of ct segment contexts */ |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 61 | } ct_main_t; |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 62 | |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 63 | static ct_main_t ct_main; |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 64 | |
| 65 | static ct_connection_t * |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 66 | ct_connection_alloc (u32 thread_index) |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 67 | { |
| 68 | ct_connection_t *ct; |
| 69 | |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 70 | pool_get_zero (ct_main.connections[thread_index], ct); |
| 71 | ct->c_c_index = ct - ct_main.connections[thread_index]; |
| 72 | ct->c_thread_index = thread_index; |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 73 | ct->client_wrk = ~0; |
| 74 | ct->server_wrk = ~0; |
Florin Coras | da78c5a | 2021-06-09 14:55:24 -0700 | [diff] [blame] | 75 | ct->seg_ctx_index = ~0; |
| 76 | ct->ct_seg_index = ~0; |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 77 | return ct; |
| 78 | } |
| 79 | |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 80 | static ct_connection_t * |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 81 | ct_connection_get (u32 ct_index, u32 thread_index) |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 82 | { |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 83 | if (pool_is_free_index (ct_main.connections[thread_index], ct_index)) |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 84 | return 0; |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 85 | return pool_elt_at_index (ct_main.connections[thread_index], ct_index); |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 88 | static void |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 89 | ct_connection_free (ct_connection_t * ct) |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 90 | { |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 91 | if (CLIB_DEBUG) |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 92 | { |
| 93 | u32 thread_index = ct->c_thread_index; |
| 94 | memset (ct, 0xfc, sizeof (*ct)); |
| 95 | pool_put (ct_main.connections[thread_index], ct); |
| 96 | return; |
| 97 | } |
| 98 | pool_put (ct_main.connections[ct->c_thread_index], ct); |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Florin Coras | 374df7a | 2021-05-13 11:37:43 -0700 | [diff] [blame] | 101 | static ct_connection_t * |
| 102 | ct_half_open_alloc (void) |
| 103 | { |
| 104 | ct_main_t *cm = &ct_main; |
| 105 | u32 *hip; |
| 106 | |
| 107 | clib_spinlock_lock (&cm->ho_reuseable_lock); |
| 108 | vec_foreach (hip, cm->ho_reusable) |
| 109 | pool_put_index (cm->connections[0], *hip); |
| 110 | vec_reset_length (cm->ho_reusable); |
| 111 | clib_spinlock_unlock (&cm->ho_reuseable_lock); |
| 112 | |
| 113 | return ct_connection_alloc (0); |
| 114 | } |
| 115 | |
| 116 | void |
| 117 | ct_half_open_add_reusable (u32 ho_index) |
| 118 | { |
| 119 | ct_main_t *cm = &ct_main; |
| 120 | |
| 121 | clib_spinlock_lock (&cm->ho_reuseable_lock); |
| 122 | vec_add1 (cm->ho_reusable, ho_index); |
| 123 | clib_spinlock_unlock (&cm->ho_reuseable_lock); |
| 124 | } |
| 125 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 126 | session_t * |
| 127 | ct_session_get_peer (session_t * s) |
| 128 | { |
| 129 | ct_connection_t *ct, *peer_ct; |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 130 | ct = ct_connection_get (s->connection_index, s->thread_index); |
| 131 | peer_ct = ct_connection_get (ct->peer_index, s->thread_index); |
| 132 | return session_get (peer_ct->c_s_index, s->thread_index); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 135 | void |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 136 | ct_session_endpoint (session_t * ll, session_endpoint_t * sep) |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 137 | { |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 138 | ct_connection_t *ct; |
| 139 | ct = (ct_connection_t *) session_get_transport (ll); |
| 140 | sep->transport_proto = ct->actual_tp; |
| 141 | sep->port = ct->c_lcl_port; |
| 142 | sep->is_ip4 = ct->c_is_ip4; |
Florin Coras | ea7e708 | 2020-07-07 17:57:28 -0700 | [diff] [blame] | 143 | ip_copy (&sep->ip, &ct->c_lcl_ip, ct->c_is_ip4); |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 144 | } |
| 145 | |
Florin Coras | da78c5a | 2021-06-09 14:55:24 -0700 | [diff] [blame] | 146 | static void |
| 147 | ct_session_dealloc_fifos (ct_connection_t *ct, svm_fifo_t *rx_fifo, |
| 148 | svm_fifo_t *tx_fifo) |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 149 | { |
Florin Coras | da78c5a | 2021-06-09 14:55:24 -0700 | [diff] [blame] | 150 | ct_segments_ctx_t *seg_ctx; |
| 151 | ct_main_t *cm = &ct_main; |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 152 | segment_manager_t *sm; |
Florin Coras | da78c5a | 2021-06-09 14:55:24 -0700 | [diff] [blame] | 153 | app_worker_t *app_wrk; |
| 154 | ct_segment_t *ct_seg; |
| 155 | fifo_segment_t *fs; |
Florin Coras | 6973c73 | 2021-06-17 15:53:38 -0700 | [diff] [blame] | 156 | u8 del_segment = 0; |
Florin Coras | da78c5a | 2021-06-09 14:55:24 -0700 | [diff] [blame] | 157 | u32 seg_index; |
Florin Coras | 6973c73 | 2021-06-17 15:53:38 -0700 | [diff] [blame] | 158 | int cnt; |
Florin Coras | da78c5a | 2021-06-09 14:55:24 -0700 | [diff] [blame] | 159 | |
| 160 | /* |
| 161 | * Cleanup fifos |
| 162 | */ |
| 163 | |
| 164 | sm = segment_manager_get (rx_fifo->segment_manager); |
| 165 | seg_index = rx_fifo->segment_index; |
| 166 | |
| 167 | fs = segment_manager_get_segment_w_lock (sm, seg_index); |
| 168 | fifo_segment_free_fifo (fs, rx_fifo); |
| 169 | fifo_segment_free_fifo (fs, tx_fifo); |
| 170 | segment_manager_segment_reader_unlock (sm); |
| 171 | |
| 172 | /* |
Florin Coras | 6973c73 | 2021-06-17 15:53:38 -0700 | [diff] [blame] | 173 | * Atomically update segment context with readers lock |
Florin Coras | da78c5a | 2021-06-09 14:55:24 -0700 | [diff] [blame] | 174 | */ |
| 175 | |
| 176 | clib_rwlock_reader_lock (&cm->app_segs_lock); |
| 177 | |
| 178 | seg_ctx = pool_elt_at_index (cm->app_seg_ctxs, ct->seg_ctx_index); |
| 179 | ct_seg = pool_elt_at_index (seg_ctx->segments, ct->ct_seg_index); |
| 180 | |
| 181 | if (ct->flags & CT_CONN_F_CLIENT) |
| 182 | { |
| 183 | cnt = |
| 184 | __atomic_sub_fetch (&ct_seg->client_n_sessions, 1, __ATOMIC_RELAXED); |
Florin Coras | da78c5a | 2021-06-09 14:55:24 -0700 | [diff] [blame] | 185 | } |
| 186 | else |
| 187 | { |
| 188 | cnt = |
| 189 | __atomic_sub_fetch (&ct_seg->server_n_sessions, 1, __ATOMIC_RELAXED); |
Florin Coras | da78c5a | 2021-06-09 14:55:24 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Florin Coras | da78c5a | 2021-06-09 14:55:24 -0700 | [diff] [blame] | 192 | clib_rwlock_reader_unlock (&cm->app_segs_lock); |
| 193 | |
| 194 | /* |
| 195 | * No need to do any app updates, return |
| 196 | */ |
Florin Coras | 6973c73 | 2021-06-17 15:53:38 -0700 | [diff] [blame] | 197 | ASSERT (cnt >= 0); |
| 198 | if (cnt) |
| 199 | return; |
| 200 | |
| 201 | /* |
| 202 | * Grab exclusive lock and update flags unless some other thread |
| 203 | * added more sessions |
| 204 | */ |
| 205 | clib_rwlock_writer_lock (&cm->app_segs_lock); |
| 206 | |
| 207 | seg_ctx = pool_elt_at_index (cm->app_seg_ctxs, ct->seg_ctx_index); |
| 208 | ct_seg = pool_elt_at_index (seg_ctx->segments, ct->ct_seg_index); |
| 209 | if (ct->flags & CT_CONN_F_CLIENT) |
| 210 | { |
| 211 | cnt = ct_seg->client_n_sessions; |
| 212 | if (!cnt) |
| 213 | ct_seg->flags |= CT_SEGMENT_F_CLIENT_DETACHED; |
| 214 | } |
| 215 | else |
| 216 | { |
| 217 | cnt = ct_seg->server_n_sessions; |
| 218 | if (!cnt) |
| 219 | ct_seg->flags |= CT_SEGMENT_F_SERVER_DETACHED; |
| 220 | } |
| 221 | |
| 222 | /* |
| 223 | * Remove segment context because both client and server detached |
| 224 | */ |
| 225 | |
| 226 | if (!cnt && (ct_seg->flags & CT_SEGMENT_F_CLIENT_DETACHED) && |
| 227 | (ct_seg->flags & CT_SEGMENT_F_SERVER_DETACHED)) |
| 228 | { |
| 229 | pool_put_index (seg_ctx->segments, ct->ct_seg_index); |
| 230 | |
| 231 | /* |
| 232 | * No more segment indices left, remove the segments context |
| 233 | */ |
| 234 | if (!pool_elts (seg_ctx->segments)) |
| 235 | { |
| 236 | u64 table_handle = seg_ctx->client_wrk << 16 | seg_ctx->server_wrk; |
| 237 | table_handle = (u64) seg_ctx->sm_index << 32 | table_handle; |
| 238 | hash_unset (cm->app_segs_ctxs_table, table_handle); |
| 239 | pool_free (seg_ctx->segments); |
| 240 | pool_put_index (cm->app_seg_ctxs, ct->seg_ctx_index); |
| 241 | } |
| 242 | del_segment = 1; |
| 243 | } |
| 244 | |
| 245 | clib_rwlock_writer_unlock (&cm->app_segs_lock); |
| 246 | |
| 247 | /* |
| 248 | * Session counter went to zero, notify the app that detached |
| 249 | */ |
Florin Coras | da78c5a | 2021-06-09 14:55:24 -0700 | [diff] [blame] | 250 | if (cnt) |
| 251 | return; |
| 252 | |
| 253 | if (ct->flags & CT_CONN_F_CLIENT) |
| 254 | { |
| 255 | app_wrk = app_worker_get_if_valid (ct->client_wrk); |
| 256 | /* Determine if client app still needs notification, i.e., if it is |
| 257 | * still attached. If client detached and this is the last ct session |
| 258 | * on this segment, then its connects segment manager should also be |
| 259 | * detached, so do not send notification */ |
| 260 | if (app_wrk) |
| 261 | { |
| 262 | segment_manager_t *csm; |
| 263 | csm = app_worker_get_connect_segment_manager (app_wrk); |
| 264 | if (!segment_manager_app_detached (csm)) |
| 265 | app_worker_del_segment_notify (app_wrk, ct->segment_handle); |
| 266 | } |
| 267 | } |
| 268 | else if (!segment_manager_app_detached (sm)) |
| 269 | { |
| 270 | app_wrk = app_worker_get (ct->server_wrk); |
| 271 | app_worker_del_segment_notify (app_wrk, ct->segment_handle); |
| 272 | } |
| 273 | |
Florin Coras | 6973c73 | 2021-06-17 15:53:38 -0700 | [diff] [blame] | 274 | if (!del_segment) |
Florin Coras | da78c5a | 2021-06-09 14:55:24 -0700 | [diff] [blame] | 275 | return; |
| 276 | |
Florin Coras | da78c5a | 2021-06-09 14:55:24 -0700 | [diff] [blame] | 277 | segment_manager_lock_and_del_segment (sm, seg_index); |
| 278 | |
| 279 | /* Cleanup segment manager if needed. If server detaches there's a chance |
| 280 | * the client's sessions will hold up segment removal */ |
| 281 | if (segment_manager_app_detached (sm) && !segment_manager_has_fifos (sm)) |
| 282 | segment_manager_free_safe (sm); |
| 283 | } |
| 284 | |
| 285 | int |
Florin Coras | 6973c73 | 2021-06-17 15:53:38 -0700 | [diff] [blame] | 286 | ct_session_connect_notify (session_t *ss, session_error_t err) |
Florin Coras | da78c5a | 2021-06-09 14:55:24 -0700 | [diff] [blame] | 287 | { |
Florin Coras | 6973c73 | 2021-06-17 15:53:38 -0700 | [diff] [blame] | 288 | u32 ss_index, opaque, thread_index; |
Florin Coras | da78c5a | 2021-06-09 14:55:24 -0700 | [diff] [blame] | 289 | ct_connection_t *sct, *cct; |
Florin Coras | da78c5a | 2021-06-09 14:55:24 -0700 | [diff] [blame] | 290 | app_worker_t *client_wrk; |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 291 | session_t *cs; |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 292 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 293 | ss_index = ss->session_index; |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 294 | thread_index = ss->thread_index; |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 295 | sct = (ct_connection_t *) session_get_transport (ss); |
| 296 | client_wrk = app_worker_get (sct->client_wrk); |
Florin Coras | dd7d24b | 2020-08-14 17:51:13 -0700 | [diff] [blame] | 297 | opaque = sct->client_opaque; |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 298 | |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 299 | cct = ct_connection_get (sct->peer_index, thread_index); |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 300 | |
Florin Coras | 374df7a | 2021-05-13 11:37:43 -0700 | [diff] [blame] | 301 | /* Client closed while waiting for reply from server */ |
Florin Coras | 6973c73 | 2021-06-17 15:53:38 -0700 | [diff] [blame] | 302 | if (PREDICT_FALSE (!cct)) |
Florin Coras | 374df7a | 2021-05-13 11:37:43 -0700 | [diff] [blame] | 303 | { |
| 304 | session_transport_closing_notify (&sct->connection); |
| 305 | session_transport_delete_notify (&sct->connection); |
| 306 | ct_connection_free (sct); |
| 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | session_half_open_delete_notify (&cct->connection); |
| 311 | cct->flags &= ~CT_CONN_F_HALF_OPEN; |
| 312 | |
Florin Coras | 6973c73 | 2021-06-17 15:53:38 -0700 | [diff] [blame] | 313 | if (PREDICT_FALSE (err)) |
| 314 | goto connect_error; |
Florin Coras | da78c5a | 2021-06-09 14:55:24 -0700 | [diff] [blame] | 315 | |
| 316 | /* |
| 317 | * Alloc client session |
| 318 | */ |
| 319 | |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 320 | cs = session_alloc (thread_index); |
| 321 | ss = session_get (ss_index, thread_index); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 322 | cs->session_type = ss->session_type; |
Nathan Skrzypczak | 2f0f96b | 2019-06-13 10:14:28 +0200 | [diff] [blame] | 323 | cs->listener_handle = SESSION_INVALID_HANDLE; |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 324 | cs->session_state = SESSION_STATE_CONNECTING; |
| 325 | cs->app_wrk_index = client_wrk->wrk_index; |
| 326 | cs->connection_index = cct->c_c_index; |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 327 | cct->c_s_index = cs->session_index; |
| 328 | |
| 329 | /* This will allocate fifos for the session. They won't be used for |
| 330 | * exchanging data but they will be used to close the connection if |
| 331 | * the segment manager/worker is freed */ |
Florin Coras | dd7d24b | 2020-08-14 17:51:13 -0700 | [diff] [blame] | 332 | if ((err = app_worker_init_connected (client_wrk, cs))) |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 333 | { |
Florin Coras | dd7d24b | 2020-08-14 17:51:13 -0700 | [diff] [blame] | 334 | session_free (cs); |
Florin Coras | 6973c73 | 2021-06-17 15:53:38 -0700 | [diff] [blame] | 335 | session_close (ss); |
| 336 | err = SESSION_E_ALLOC; |
| 337 | goto connect_error; |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 338 | } |
| 339 | |
Florin Coras | dd7d24b | 2020-08-14 17:51:13 -0700 | [diff] [blame] | 340 | cs->session_state = SESSION_STATE_CONNECTING; |
| 341 | |
Florin Coras | 6973c73 | 2021-06-17 15:53:38 -0700 | [diff] [blame] | 342 | if (app_worker_connect_notify (client_wrk, cs, 0, opaque)) |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 343 | { |
Florin Coras | 6973c73 | 2021-06-17 15:53:38 -0700 | [diff] [blame] | 344 | segment_manager_dealloc_fifos (cs->rx_fifo, cs->tx_fifo); |
Florin Coras | dd7d24b | 2020-08-14 17:51:13 -0700 | [diff] [blame] | 345 | session_free (cs); |
Florin Coras | 6973c73 | 2021-06-17 15:53:38 -0700 | [diff] [blame] | 346 | session_close (ss); |
| 347 | goto cleanup_client; |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 348 | } |
| 349 | |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 350 | cs = session_get (cct->c_s_index, cct->c_thread_index); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 351 | cs->session_state = SESSION_STATE_READY; |
| 352 | |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 353 | return 0; |
Florin Coras | dd7d24b | 2020-08-14 17:51:13 -0700 | [diff] [blame] | 354 | |
Florin Coras | 6973c73 | 2021-06-17 15:53:38 -0700 | [diff] [blame] | 355 | connect_error: |
| 356 | |
| 357 | app_worker_connect_notify (client_wrk, 0, err, cct->client_opaque); |
| 358 | |
| 359 | cleanup_client: |
| 360 | |
| 361 | if (cct->client_rx_fifo) |
| 362 | ct_session_dealloc_fifos (cct, cct->client_rx_fifo, cct->client_tx_fifo); |
| 363 | ct_connection_free (cct); |
Florin Coras | dd7d24b | 2020-08-14 17:51:13 -0700 | [diff] [blame] | 364 | return -1; |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 365 | } |
| 366 | |
Florin Coras | 6973c73 | 2021-06-17 15:53:38 -0700 | [diff] [blame] | 367 | static inline ct_segment_t * |
| 368 | ct_lookup_free_segment (ct_main_t *cm, segment_manager_t *sm, |
| 369 | u32 seg_ctx_index) |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 370 | { |
Florin Coras | da78c5a | 2021-06-09 14:55:24 -0700 | [diff] [blame] | 371 | uword free_bytes, max_free_bytes; |
| 372 | ct_segment_t *ct_seg, *res = 0; |
Florin Coras | 6973c73 | 2021-06-17 15:53:38 -0700 | [diff] [blame] | 373 | ct_segments_ctx_t *seg_ctx; |
Florin Coras | da78c5a | 2021-06-09 14:55:24 -0700 | [diff] [blame] | 374 | fifo_segment_t *fs; |
| 375 | u32 max_fifos; |
| 376 | |
Florin Coras | 6973c73 | 2021-06-17 15:53:38 -0700 | [diff] [blame] | 377 | seg_ctx = pool_elt_at_index (cm->app_seg_ctxs, seg_ctx_index); |
| 378 | max_free_bytes = seg_ctx->fifo_pair_bytes; |
| 379 | |
Florin Coras | da78c5a | 2021-06-09 14:55:24 -0700 | [diff] [blame] | 380 | pool_foreach (ct_seg, seg_ctx->segments) |
| 381 | { |
| 382 | /* Client or server has detached so segment cannot be used */ |
| 383 | if ((ct_seg->flags & CT_SEGMENT_F_SERVER_DETACHED) || |
| 384 | (ct_seg->flags & CT_SEGMENT_F_CLIENT_DETACHED)) |
| 385 | continue; |
| 386 | fs = segment_manager_get_segment (sm, ct_seg->segment_index); |
| 387 | free_bytes = fifo_segment_available_bytes (fs); |
Florin Coras | 6973c73 | 2021-06-17 15:53:38 -0700 | [diff] [blame] | 388 | max_fifos = fifo_segment_size (fs) / seg_ctx->fifo_pair_bytes; |
Florin Coras | da78c5a | 2021-06-09 14:55:24 -0700 | [diff] [blame] | 389 | if (free_bytes > max_free_bytes && |
| 390 | fifo_segment_num_fifos (fs) / 2 < max_fifos) |
| 391 | { |
| 392 | max_free_bytes = free_bytes; |
| 393 | res = ct_seg; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | return res; |
| 398 | } |
| 399 | |
Florin Coras | 6973c73 | 2021-06-17 15:53:38 -0700 | [diff] [blame] | 400 | static ct_segment_t * |
| 401 | ct_alloc_segment (ct_main_t *cm, app_worker_t *server_wrk, u64 table_handle, |
| 402 | segment_manager_t *sm, u32 client_wrk_index) |
| 403 | { |
| 404 | u32 seg_ctx_index = ~0, sm_index, pair_bytes; |
| 405 | segment_manager_props_t *props; |
| 406 | const u32 margin = 16 << 10; |
| 407 | ct_segments_ctx_t *seg_ctx; |
| 408 | app_worker_t *client_wrk; |
| 409 | u64 seg_size, seg_handle; |
| 410 | application_t *server; |
| 411 | ct_segment_t *ct_seg; |
| 412 | uword *spp; |
| 413 | int fs_index; |
| 414 | |
| 415 | server = application_get (server_wrk->app_index); |
| 416 | props = application_segment_manager_properties (server); |
| 417 | sm_index = segment_manager_index (sm); |
| 418 | pair_bytes = props->rx_fifo_size + props->tx_fifo_size + margin; |
| 419 | |
| 420 | /* |
| 421 | * Make sure another thread did not alloc a segment while acquiring the lock |
| 422 | */ |
| 423 | |
| 424 | spp = hash_get (cm->app_segs_ctxs_table, table_handle); |
| 425 | if (spp) |
| 426 | { |
| 427 | seg_ctx_index = *spp; |
| 428 | ct_seg = ct_lookup_free_segment (cm, sm, seg_ctx_index); |
| 429 | if (ct_seg) |
| 430 | return ct_seg; |
| 431 | } |
| 432 | |
| 433 | /* |
| 434 | * No segment, try to alloc one and notify the server and the client. |
| 435 | * Make sure the segment is not used for other fifos |
| 436 | */ |
| 437 | seg_size = clib_max (props->segment_size, 128 << 20); |
| 438 | fs_index = |
| 439 | segment_manager_add_segment2 (sm, seg_size, FIFO_SEGMENT_F_CUSTOM_USE); |
| 440 | if (fs_index < 0) |
| 441 | return 0; |
| 442 | |
| 443 | if (seg_ctx_index == ~0) |
| 444 | { |
| 445 | pool_get_zero (cm->app_seg_ctxs, seg_ctx); |
| 446 | seg_ctx_index = seg_ctx - cm->app_seg_ctxs; |
| 447 | hash_set (cm->app_segs_ctxs_table, table_handle, seg_ctx_index); |
| 448 | seg_ctx->server_wrk = server_wrk->wrk_index; |
| 449 | seg_ctx->client_wrk = client_wrk_index; |
| 450 | seg_ctx->sm_index = sm_index; |
| 451 | seg_ctx->fifo_pair_bytes = pair_bytes; |
| 452 | } |
| 453 | else |
| 454 | { |
| 455 | seg_ctx = pool_elt_at_index (cm->app_seg_ctxs, seg_ctx_index); |
| 456 | } |
| 457 | |
| 458 | pool_get_zero (seg_ctx->segments, ct_seg); |
| 459 | ct_seg->segment_index = fs_index; |
| 460 | ct_seg->server_n_sessions = 0; |
| 461 | ct_seg->client_n_sessions = 0; |
| 462 | ct_seg->ct_seg_index = ct_seg - seg_ctx->segments; |
| 463 | ct_seg->seg_ctx_index = seg_ctx_index; |
| 464 | |
| 465 | /* New segment, notify the server and client */ |
| 466 | seg_handle = segment_manager_make_segment_handle (sm_index, fs_index); |
| 467 | if (app_worker_add_segment_notify (server_wrk, seg_handle)) |
| 468 | goto error; |
| 469 | |
| 470 | client_wrk = app_worker_get (client_wrk_index); |
| 471 | if (app_worker_add_segment_notify (client_wrk, seg_handle)) |
| 472 | { |
| 473 | app_worker_del_segment_notify (server_wrk, seg_handle); |
| 474 | goto error; |
| 475 | } |
| 476 | |
| 477 | return ct_seg; |
| 478 | |
| 479 | error: |
| 480 | |
| 481 | segment_manager_lock_and_del_segment (sm, fs_index); |
| 482 | pool_put_index (seg_ctx->segments, ct_seg->seg_ctx_index); |
| 483 | return 0; |
| 484 | } |
| 485 | |
Florin Coras | da78c5a | 2021-06-09 14:55:24 -0700 | [diff] [blame] | 486 | static int |
| 487 | ct_init_accepted_session (app_worker_t *server_wrk, ct_connection_t *ct, |
| 488 | session_t *ls, session_t *ll) |
| 489 | { |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 490 | segment_manager_props_t *props; |
Florin Coras | 6973c73 | 2021-06-17 15:53:38 -0700 | [diff] [blame] | 491 | u64 seg_handle, table_handle; |
| 492 | u32 sm_index, fs_index = ~0; |
Florin Coras | da78c5a | 2021-06-09 14:55:24 -0700 | [diff] [blame] | 493 | ct_segments_ctx_t *seg_ctx; |
| 494 | ct_main_t *cm = &ct_main; |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 495 | application_t *server; |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 496 | segment_manager_t *sm; |
Florin Coras | da78c5a | 2021-06-09 14:55:24 -0700 | [diff] [blame] | 497 | ct_segment_t *ct_seg; |
| 498 | fifo_segment_t *fs; |
Florin Coras | da78c5a | 2021-06-09 14:55:24 -0700 | [diff] [blame] | 499 | uword *spp; |
Florin Coras | 6973c73 | 2021-06-17 15:53:38 -0700 | [diff] [blame] | 500 | int rv; |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 501 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 502 | sm = app_worker_get_listen_segment_manager (server_wrk, ll); |
Florin Coras | da78c5a | 2021-06-09 14:55:24 -0700 | [diff] [blame] | 503 | sm_index = segment_manager_index (sm); |
| 504 | server = application_get (server_wrk->app_index); |
| 505 | props = application_segment_manager_properties (server); |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 506 | |
Florin Coras | da78c5a | 2021-06-09 14:55:24 -0700 | [diff] [blame] | 507 | table_handle = ct->client_wrk << 16 | server_wrk->wrk_index; |
Florin Coras | 6973c73 | 2021-06-17 15:53:38 -0700 | [diff] [blame] | 508 | table_handle = (u64) sm_index << 32 | table_handle; |
Florin Coras | da78c5a | 2021-06-09 14:55:24 -0700 | [diff] [blame] | 509 | |
| 510 | /* |
| 511 | * Check if we already have a segment that can hold the fifos |
| 512 | */ |
| 513 | |
| 514 | clib_rwlock_reader_lock (&cm->app_segs_lock); |
| 515 | |
| 516 | spp = hash_get (cm->app_segs_ctxs_table, table_handle); |
| 517 | if (spp) |
| 518 | { |
Florin Coras | 6973c73 | 2021-06-17 15:53:38 -0700 | [diff] [blame] | 519 | ct_seg = ct_lookup_free_segment (cm, sm, *spp); |
Florin Coras | da78c5a | 2021-06-09 14:55:24 -0700 | [diff] [blame] | 520 | if (ct_seg) |
| 521 | { |
Florin Coras | 6973c73 | 2021-06-17 15:53:38 -0700 | [diff] [blame] | 522 | ct->seg_ctx_index = ct_seg->seg_ctx_index; |
| 523 | ct->ct_seg_index = ct_seg->ct_seg_index; |
Florin Coras | da78c5a | 2021-06-09 14:55:24 -0700 | [diff] [blame] | 524 | fs_index = ct_seg->segment_index; |
| 525 | __atomic_add_fetch (&ct_seg->server_n_sessions, 1, __ATOMIC_RELAXED); |
Florin Coras | 6973c73 | 2021-06-17 15:53:38 -0700 | [diff] [blame] | 526 | __atomic_add_fetch (&ct_seg->client_n_sessions, 1, __ATOMIC_RELAXED); |
Florin Coras | da78c5a | 2021-06-09 14:55:24 -0700 | [diff] [blame] | 527 | } |
| 528 | } |
| 529 | |
| 530 | clib_rwlock_reader_unlock (&cm->app_segs_lock); |
| 531 | |
| 532 | /* |
Florin Coras | 6973c73 | 2021-06-17 15:53:38 -0700 | [diff] [blame] | 533 | * If not, grab exclusive lock and allocate segment |
Florin Coras | da78c5a | 2021-06-09 14:55:24 -0700 | [diff] [blame] | 534 | */ |
Florin Coras | 6973c73 | 2021-06-17 15:53:38 -0700 | [diff] [blame] | 535 | if (fs_index == ~0) |
Florin Coras | da78c5a | 2021-06-09 14:55:24 -0700 | [diff] [blame] | 536 | { |
Florin Coras | da78c5a | 2021-06-09 14:55:24 -0700 | [diff] [blame] | 537 | clib_rwlock_writer_lock (&cm->app_segs_lock); |
| 538 | |
Florin Coras | 6973c73 | 2021-06-17 15:53:38 -0700 | [diff] [blame] | 539 | ct_seg = |
| 540 | ct_alloc_segment (cm, server_wrk, table_handle, sm, ct->client_wrk); |
| 541 | if (!ct_seg) |
Florin Coras | da78c5a | 2021-06-09 14:55:24 -0700 | [diff] [blame] | 542 | { |
Florin Coras | 6973c73 | 2021-06-17 15:53:38 -0700 | [diff] [blame] | 543 | clib_rwlock_writer_unlock (&cm->app_segs_lock); |
| 544 | return -1; |
Florin Coras | da78c5a | 2021-06-09 14:55:24 -0700 | [diff] [blame] | 545 | } |
Florin Coras | da78c5a | 2021-06-09 14:55:24 -0700 | [diff] [blame] | 546 | |
Florin Coras | 6973c73 | 2021-06-17 15:53:38 -0700 | [diff] [blame] | 547 | ct->seg_ctx_index = ct_seg->seg_ctx_index; |
| 548 | ct->ct_seg_index = ct_seg->ct_seg_index; |
Florin Coras | da78c5a | 2021-06-09 14:55:24 -0700 | [diff] [blame] | 549 | ct_seg->server_n_sessions += 1; |
Florin Coras | 6973c73 | 2021-06-17 15:53:38 -0700 | [diff] [blame] | 550 | ct_seg->client_n_sessions += 1; |
| 551 | fs_index = ct_seg->segment_index; |
Florin Coras | da78c5a | 2021-06-09 14:55:24 -0700 | [diff] [blame] | 552 | |
| 553 | clib_rwlock_writer_unlock (&cm->app_segs_lock); |
Florin Coras | da78c5a | 2021-06-09 14:55:24 -0700 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | /* |
| 557 | * Allocate and initialize the fifos |
| 558 | */ |
| 559 | fs = segment_manager_get_segment_w_lock (sm, fs_index); |
| 560 | rv = segment_manager_try_alloc_fifos ( |
| 561 | fs, ls->thread_index, props->rx_fifo_size, props->tx_fifo_size, |
| 562 | &ls->rx_fifo, &ls->tx_fifo); |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 563 | if (rv) |
| 564 | { |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 565 | segment_manager_segment_reader_unlock (sm); |
Florin Coras | 6973c73 | 2021-06-17 15:53:38 -0700 | [diff] [blame] | 566 | |
| 567 | clib_rwlock_reader_lock (&cm->app_segs_lock); |
| 568 | |
| 569 | seg_ctx = pool_elt_at_index (cm->app_seg_ctxs, ct->seg_ctx_index); |
| 570 | ct_seg = pool_elt_at_index (seg_ctx->segments, ct->ct_seg_index); |
| 571 | __atomic_sub_fetch (&ct_seg->server_n_sessions, 1, __ATOMIC_RELAXED); |
| 572 | __atomic_sub_fetch (&ct_seg->client_n_sessions, 1, __ATOMIC_RELAXED); |
| 573 | |
| 574 | clib_rwlock_reader_unlock (&cm->app_segs_lock); |
| 575 | |
| 576 | return rv; |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 577 | } |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 578 | |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 579 | ls->rx_fifo->shr->master_session_index = ls->session_index; |
| 580 | ls->tx_fifo->shr->master_session_index = ls->session_index; |
Florin Coras | ce252ca | 2020-11-04 13:08:35 -0800 | [diff] [blame] | 581 | ls->rx_fifo->master_thread_index = ls->thread_index; |
| 582 | ls->tx_fifo->master_thread_index = ls->thread_index; |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 583 | ls->rx_fifo->segment_manager = sm_index; |
| 584 | ls->tx_fifo->segment_manager = sm_index; |
Florin Coras | da78c5a | 2021-06-09 14:55:24 -0700 | [diff] [blame] | 585 | ls->rx_fifo->segment_index = fs_index; |
| 586 | ls->tx_fifo->segment_index = fs_index; |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 587 | |
Florin Coras | da78c5a | 2021-06-09 14:55:24 -0700 | [diff] [blame] | 588 | seg_handle = segment_manager_segment_handle (sm, fs); |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 589 | segment_manager_segment_reader_unlock (sm); |
Florin Coras | da78c5a | 2021-06-09 14:55:24 -0700 | [diff] [blame] | 590 | |
| 591 | ct->segment_handle = seg_handle; |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 592 | |
| 593 | return 0; |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 594 | } |
| 595 | |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 596 | static void |
| 597 | ct_accept_rpc_wrk_handler (void *accept_args) |
| 598 | { |
Florin Coras | 374df7a | 2021-05-13 11:37:43 -0700 | [diff] [blame] | 599 | u32 cct_index, ho_index, thread_index, ll_index; |
Florin Coras | ba02641 | 2021-06-12 11:56:19 -0700 | [diff] [blame] | 600 | ct_connection_t *sct, *cct, *ho; |
| 601 | transport_connection_t *ll_ct; |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 602 | app_worker_t *server_wrk; |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 603 | session_t *ss, *ll; |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 604 | |
Florin Coras | 374df7a | 2021-05-13 11:37:43 -0700 | [diff] [blame] | 605 | /* |
| 606 | * Alloc client ct and initialize from ho |
| 607 | */ |
| 608 | thread_index = vlib_get_thread_index (); |
| 609 | cct = ct_connection_alloc (thread_index); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 610 | cct_index = cct->c_c_index; |
Florin Coras | 374df7a | 2021-05-13 11:37:43 -0700 | [diff] [blame] | 611 | |
| 612 | ho_index = pointer_to_uword (accept_args); |
| 613 | ho = ct_connection_get (ho_index, 0); |
| 614 | |
| 615 | /* Unlikely but half-open session and transport could have been freed */ |
| 616 | if (PREDICT_FALSE (!ho)) |
| 617 | { |
| 618 | ct_connection_free (cct); |
| 619 | return; |
| 620 | } |
| 621 | |
| 622 | clib_memcpy (cct, ho, sizeof (*ho)); |
| 623 | cct->c_c_index = cct_index; |
| 624 | cct->c_thread_index = thread_index; |
| 625 | cct->flags |= CT_CONN_F_HALF_OPEN; |
| 626 | |
| 627 | /* Notify session layer that half-open is on a different thread |
| 628 | * and mark ho connection index reusable. Avoids another rpc |
| 629 | */ |
| 630 | session_half_open_migrate_notify (&cct->connection); |
| 631 | session_half_open_migrated_notify (&cct->connection); |
| 632 | ct_half_open_add_reusable (ho_index); |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 633 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 634 | /* |
Florin Coras | 374df7a | 2021-05-13 11:37:43 -0700 | [diff] [blame] | 635 | * Alloc and init server transport |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 636 | */ |
Florin Coras | 374df7a | 2021-05-13 11:37:43 -0700 | [diff] [blame] | 637 | |
| 638 | ll_index = cct->peer_index; |
| 639 | ll = listen_session_get (ll_index); |
| 640 | sct = ct_connection_alloc (thread_index); |
Florin Coras | ba02641 | 2021-06-12 11:56:19 -0700 | [diff] [blame] | 641 | /* Transport not necessarily ct but it might, so grab after sct alloc */ |
| 642 | ll_ct = listen_session_get_transport (ll); |
Florin Coras | 374df7a | 2021-05-13 11:37:43 -0700 | [diff] [blame] | 643 | |
| 644 | /* Make sure cct is valid after sct alloc */ |
| 645 | cct = ct_connection_get (cct_index, thread_index); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 646 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 647 | sct->c_rmt_port = 0; |
Florin Coras | ba02641 | 2021-06-12 11:56:19 -0700 | [diff] [blame] | 648 | sct->c_lcl_port = ll_ct->lcl_port; |
Florin Coras | 374df7a | 2021-05-13 11:37:43 -0700 | [diff] [blame] | 649 | sct->c_is_ip4 = cct->c_is_ip4; |
Florin Coras | d9e9870 | 2021-10-11 18:10:41 -0700 | [diff] [blame] | 650 | clib_memcpy (&sct->c_lcl_ip, &cct->c_rmt_ip, sizeof (cct->c_rmt_ip)); |
Florin Coras | 374df7a | 2021-05-13 11:37:43 -0700 | [diff] [blame] | 651 | sct->client_wrk = cct->client_wrk; |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 652 | sct->c_proto = TRANSPORT_PROTO_NONE; |
Florin Coras | 374df7a | 2021-05-13 11:37:43 -0700 | [diff] [blame] | 653 | sct->client_opaque = cct->client_opaque; |
Florin Coras | ba02641 | 2021-06-12 11:56:19 -0700 | [diff] [blame] | 654 | sct->actual_tp = cct->actual_tp; |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 655 | |
| 656 | sct->peer_index = cct->c_c_index; |
| 657 | cct->peer_index = sct->c_c_index; |
| 658 | |
| 659 | /* |
| 660 | * Accept server session. Client session is created only after |
| 661 | * server confirms accept. |
| 662 | */ |
Florin Coras | 374df7a | 2021-05-13 11:37:43 -0700 | [diff] [blame] | 663 | ss = session_alloc (thread_index); |
| 664 | ll = listen_session_get (ll_index); |
Florin Coras | 9f1a543 | 2019-03-10 21:03:20 -0700 | [diff] [blame] | 665 | ss->session_type = session_type_from_proto_and_ip (TRANSPORT_PROTO_NONE, |
| 666 | sct->c_is_ip4); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 667 | ss->connection_index = sct->c_c_index; |
Nathan Skrzypczak | 2f0f96b | 2019-06-13 10:14:28 +0200 | [diff] [blame] | 668 | ss->listener_handle = listen_session_get_handle (ll); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 669 | ss->session_state = SESSION_STATE_CREATED; |
| 670 | |
| 671 | server_wrk = application_listener_select_worker (ll); |
| 672 | ss->app_wrk_index = server_wrk->wrk_index; |
| 673 | |
| 674 | sct->c_s_index = ss->session_index; |
| 675 | sct->server_wrk = ss->app_wrk_index; |
| 676 | |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 677 | if (ct_init_accepted_session (server_wrk, sct, ss, ll)) |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 678 | { |
Florin Coras | 6973c73 | 2021-06-17 15:53:38 -0700 | [diff] [blame] | 679 | ct_session_connect_notify (ss, SESSION_E_ALLOC); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 680 | ct_connection_free (sct); |
| 681 | session_free (ss); |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 682 | return; |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 683 | } |
| 684 | |
Florin Coras | 6973c73 | 2021-06-17 15:53:38 -0700 | [diff] [blame] | 685 | cct->seg_ctx_index = sct->seg_ctx_index; |
| 686 | cct->ct_seg_index = sct->ct_seg_index; |
| 687 | cct->client_rx_fifo = ss->tx_fifo; |
| 688 | cct->client_tx_fifo = ss->rx_fifo; |
| 689 | cct->client_rx_fifo->refcnt++; |
| 690 | cct->client_tx_fifo->refcnt++; |
| 691 | cct->segment_handle = sct->segment_handle; |
| 692 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 693 | ss->session_state = SESSION_STATE_ACCEPTING; |
| 694 | if (app_worker_accept_notify (server_wrk, ss)) |
| 695 | { |
Florin Coras | 6973c73 | 2021-06-17 15:53:38 -0700 | [diff] [blame] | 696 | ct_session_connect_notify (ss, SESSION_E_REFUSED); |
Florin Coras | da78c5a | 2021-06-09 14:55:24 -0700 | [diff] [blame] | 697 | ct_session_dealloc_fifos (sct, ss->rx_fifo, ss->tx_fifo); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 698 | ct_connection_free (sct); |
Florin Coras | 12813d5 | 2020-04-09 20:59:20 +0000 | [diff] [blame] | 699 | session_free (ss); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 700 | } |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | static int |
| 704 | ct_connect (app_worker_t * client_wrk, session_t * ll, |
| 705 | session_endpoint_cfg_t * sep) |
| 706 | { |
Florin Coras | 374df7a | 2021-05-13 11:37:43 -0700 | [diff] [blame] | 707 | u32 thread_index, ho_index; |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 708 | ct_main_t *cm = &ct_main; |
Florin Coras | 374df7a | 2021-05-13 11:37:43 -0700 | [diff] [blame] | 709 | ct_connection_t *ho; |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 710 | |
| 711 | /* Simple round-robin policy for spreading sessions over workers. We skip |
| 712 | * thread index 0, i.e., offset the index by 1, when we have workers as it |
| 713 | * is the one dedicated to main thread. Note that n_workers does not include |
| 714 | * main thread */ |
| 715 | cm->n_sessions += 1; |
| 716 | thread_index = cm->n_workers ? (cm->n_sessions % cm->n_workers) + 1 : 0; |
| 717 | |
Florin Coras | 374df7a | 2021-05-13 11:37:43 -0700 | [diff] [blame] | 718 | /* |
| 719 | * Alloc and init client half-open transport |
| 720 | */ |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 721 | |
Florin Coras | 374df7a | 2021-05-13 11:37:43 -0700 | [diff] [blame] | 722 | ho = ct_half_open_alloc (); |
| 723 | ho_index = ho->c_c_index; |
| 724 | ho->c_rmt_port = sep->port; |
| 725 | ho->c_lcl_port = 0; |
| 726 | ho->c_is_ip4 = sep->is_ip4; |
| 727 | ho->client_opaque = sep->opaque; |
| 728 | ho->client_wrk = client_wrk->wrk_index; |
| 729 | ho->peer_index = ll->session_index; |
| 730 | ho->c_proto = TRANSPORT_PROTO_NONE; |
| 731 | ho->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP; |
Florin Coras | d5a5882 | 2021-05-14 20:11:14 -0700 | [diff] [blame] | 732 | clib_memcpy (&ho->c_rmt_ip, &sep->ip, sizeof (sep->ip)); |
Florin Coras | 374df7a | 2021-05-13 11:37:43 -0700 | [diff] [blame] | 733 | ho->flags |= CT_CONN_F_CLIENT; |
| 734 | ho->c_s_index = ~0; |
Florin Coras | ba02641 | 2021-06-12 11:56:19 -0700 | [diff] [blame] | 735 | ho->actual_tp = sep->transport_proto; |
Florin Coras | 374df7a | 2021-05-13 11:37:43 -0700 | [diff] [blame] | 736 | |
| 737 | /* |
| 738 | * Accept connection on thread selected above. Connected reply comes |
| 739 | * after server accepts the connection. |
| 740 | */ |
| 741 | |
| 742 | session_send_rpc_evt_to_thread_force (thread_index, |
| 743 | ct_accept_rpc_wrk_handler, |
| 744 | uword_to_pointer (ho_index, void *)); |
| 745 | |
| 746 | return ho_index; |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 747 | } |
| 748 | |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 749 | static u32 |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 750 | ct_start_listen (u32 app_listener_index, transport_endpoint_t * tep) |
| 751 | { |
| 752 | session_endpoint_cfg_t *sep; |
| 753 | ct_connection_t *ct; |
| 754 | |
| 755 | sep = (session_endpoint_cfg_t *) tep; |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 756 | ct = ct_connection_alloc (0); |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 757 | ct->server_wrk = sep->app_wrk_index; |
| 758 | ct->c_is_ip4 = sep->is_ip4; |
| 759 | clib_memcpy (&ct->c_lcl_ip, &sep->ip, sizeof (sep->ip)); |
| 760 | ct->c_lcl_port = sep->port; |
Florin Coras | a837508 | 2020-10-25 19:06:57 -0700 | [diff] [blame] | 761 | ct->c_s_index = app_listener_index; |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 762 | ct->actual_tp = sep->transport_proto; |
| 763 | return ct->c_c_index; |
| 764 | } |
| 765 | |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 766 | static u32 |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 767 | ct_stop_listen (u32 ct_index) |
| 768 | { |
| 769 | ct_connection_t *ct; |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 770 | ct = ct_connection_get (ct_index, 0); |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 771 | ct_connection_free (ct); |
| 772 | return 0; |
| 773 | } |
| 774 | |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 775 | static transport_connection_t * |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 776 | ct_listener_get (u32 ct_index) |
| 777 | { |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 778 | return (transport_connection_t *) ct_connection_get (ct_index, 0); |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 779 | } |
| 780 | |
Florin Coras | 374df7a | 2021-05-13 11:37:43 -0700 | [diff] [blame] | 781 | static transport_connection_t * |
| 782 | ct_half_open_get (u32 ct_index) |
| 783 | { |
| 784 | return (transport_connection_t *) ct_connection_get (ct_index, 0); |
| 785 | } |
| 786 | |
| 787 | static void |
| 788 | ct_session_cleanup (u32 conn_index, u32 thread_index) |
| 789 | { |
| 790 | ct_connection_t *ct, *peer_ct; |
| 791 | |
| 792 | ct = ct_connection_get (conn_index, thread_index); |
| 793 | if (!ct) |
| 794 | return; |
| 795 | |
| 796 | peer_ct = ct_connection_get (ct->peer_index, thread_index); |
| 797 | if (peer_ct) |
| 798 | peer_ct->peer_index = ~0; |
| 799 | |
| 800 | ct_connection_free (ct); |
| 801 | } |
| 802 | |
| 803 | static void |
| 804 | ct_cleanup_ho (u32 ho_index) |
| 805 | { |
| 806 | ct_connection_free (ct_connection_get (ho_index, 0)); |
| 807 | } |
| 808 | |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 809 | static int |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 810 | ct_session_connect (transport_endpoint_cfg_t * tep) |
| 811 | { |
| 812 | session_endpoint_cfg_t *sep_ext; |
Florin Coras | 374df7a | 2021-05-13 11:37:43 -0700 | [diff] [blame] | 813 | session_endpoint_t _sep, *sep = &_sep; |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 814 | app_worker_t *app_wrk; |
| 815 | session_handle_t lh; |
| 816 | application_t *app; |
| 817 | app_listener_t *al; |
| 818 | u32 table_index; |
| 819 | session_t *ll; |
| 820 | u8 fib_proto; |
| 821 | |
| 822 | sep_ext = (session_endpoint_cfg_t *) tep; |
Florin Coras | 374df7a | 2021-05-13 11:37:43 -0700 | [diff] [blame] | 823 | _sep = *(session_endpoint_t *) tep; |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 824 | app_wrk = app_worker_get (sep_ext->app_wrk_index); |
| 825 | app = application_get (app_wrk->app_index); |
| 826 | |
| 827 | sep->transport_proto = sep_ext->original_tp; |
| 828 | table_index = application_local_session_table (app); |
| 829 | lh = session_lookup_local_endpoint (table_index, sep); |
| 830 | if (lh == SESSION_DROP_HANDLE) |
Florin Coras | 00e01d3 | 2019-10-21 16:07:46 -0700 | [diff] [blame] | 831 | return SESSION_E_FILTERED; |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 832 | |
| 833 | if (lh == SESSION_INVALID_HANDLE) |
| 834 | goto global_scope; |
| 835 | |
| 836 | ll = listen_session_get_from_handle (lh); |
| 837 | al = app_listener_get_w_session (ll); |
| 838 | |
| 839 | /* |
| 840 | * Break loop if rule in local table points to connecting app. This |
| 841 | * can happen if client is a generic proxy. Route connect through |
| 842 | * global table instead. |
| 843 | */ |
| 844 | if (al->app_index == app->app_index) |
| 845 | goto global_scope; |
| 846 | |
| 847 | return ct_connect (app_wrk, ll, sep_ext); |
| 848 | |
| 849 | /* |
| 850 | * If nothing found, check the global scope for locally attached |
| 851 | * destinations. Make sure first that we're allowed to. |
| 852 | */ |
| 853 | |
| 854 | global_scope: |
| 855 | if (session_endpoint_is_local (sep)) |
Florin Coras | 00e01d3 | 2019-10-21 16:07:46 -0700 | [diff] [blame] | 856 | return SESSION_E_NOROUTE; |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 857 | |
| 858 | if (!application_has_global_scope (app)) |
Florin Coras | 00e01d3 | 2019-10-21 16:07:46 -0700 | [diff] [blame] | 859 | return SESSION_E_SCOPE; |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 860 | |
| 861 | fib_proto = session_endpoint_fib_proto (sep); |
Florin Coras | 95cd864 | 2019-12-30 21:53:19 -0800 | [diff] [blame] | 862 | table_index = session_lookup_get_index_for_fib (fib_proto, sep->fib_index); |
Florin Coras | 9f1a543 | 2019-03-10 21:03:20 -0700 | [diff] [blame] | 863 | ll = session_lookup_listener_wildcard (table_index, sep); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 864 | |
Florin Coras | 821b500 | 2021-06-02 21:32:39 -0700 | [diff] [blame] | 865 | /* Avoid connecting app to own listener */ |
| 866 | if (ll && ll->app_index != app->app_index) |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 867 | return ct_connect (app_wrk, ll, sep_ext); |
| 868 | |
| 869 | /* Failed to connect but no error */ |
Florin Coras | 374df7a | 2021-05-13 11:37:43 -0700 | [diff] [blame] | 870 | return SESSION_E_LOCAL_CONNECT; |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 871 | } |
| 872 | |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 873 | static void |
Florin Coras | 440c7b5 | 2021-11-09 08:38:24 -0800 | [diff] [blame^] | 874 | ct_session_postponed_cleanup (ct_connection_t *ct) |
| 875 | { |
| 876 | app_worker_t *app_wrk; |
| 877 | session_t *s; |
| 878 | |
| 879 | s = session_get (ct->c_s_index, ct->c_thread_index); |
| 880 | app_wrk = app_worker_get_if_valid (s->app_wrk_index); |
| 881 | |
| 882 | if (ct->flags & CT_CONN_F_CLIENT) |
| 883 | { |
| 884 | if (app_wrk) |
| 885 | app_worker_cleanup_notify (app_wrk, s, SESSION_CLEANUP_TRANSPORT); |
| 886 | |
| 887 | /* Normal free for client session as the fifos are allocated through |
| 888 | * the connects segment manager in a segment that's not shared with |
| 889 | * the server */ |
| 890 | session_free_w_fifos (s); |
| 891 | ct_session_dealloc_fifos (ct, ct->client_rx_fifo, ct->client_tx_fifo); |
| 892 | } |
| 893 | else |
| 894 | { |
| 895 | /* Manual session and fifo segment cleanup to avoid implicit |
| 896 | * segment manager cleanups and notifications */ |
| 897 | app_wrk = app_worker_get_if_valid (s->app_wrk_index); |
| 898 | if (app_wrk) |
| 899 | { |
| 900 | app_worker_cleanup_notify (app_wrk, s, SESSION_CLEANUP_TRANSPORT); |
| 901 | app_worker_cleanup_notify (app_wrk, s, SESSION_CLEANUP_SESSION); |
| 902 | } |
| 903 | |
| 904 | ct_session_dealloc_fifos (ct, s->rx_fifo, s->tx_fifo); |
| 905 | session_free (s); |
| 906 | } |
| 907 | |
| 908 | ct_connection_free (ct); |
| 909 | } |
| 910 | |
| 911 | static void |
| 912 | ct_handle_cleanups (void *args) |
| 913 | { |
| 914 | uword thread_index = pointer_to_uword (args); |
| 915 | const u32 max_cleanups = 100; |
| 916 | ct_main_t *cm = &ct_main; |
| 917 | ct_cleanup_req_t *req; |
| 918 | ct_connection_t *ct; |
| 919 | u32 n_to_handle = 0; |
| 920 | session_t *s; |
| 921 | |
| 922 | cm->heave_cleanups[thread_index] = 0; |
| 923 | n_to_handle = clib_fifo_elts (cm->pending_cleanups[thread_index]); |
| 924 | n_to_handle = clib_min (n_to_handle, max_cleanups); |
| 925 | |
| 926 | while (n_to_handle) |
| 927 | { |
| 928 | clib_fifo_sub2 (cm->pending_cleanups[thread_index], req); |
| 929 | ct = ct_connection_get (req->ct_index, thread_index); |
| 930 | s = session_get (ct->c_s_index, ct->c_thread_index); |
| 931 | if (!svm_fifo_has_event (s->tx_fifo)) |
| 932 | ct_session_postponed_cleanup (ct); |
| 933 | else |
| 934 | clib_fifo_add1 (cm->pending_cleanups[thread_index], *req); |
| 935 | n_to_handle -= 1; |
| 936 | } |
| 937 | |
| 938 | if (clib_fifo_elts (cm->pending_cleanups[thread_index])) |
| 939 | { |
| 940 | cm->heave_cleanups[thread_index] = 1; |
| 941 | session_send_rpc_evt_to_thread_force ( |
| 942 | thread_index, ct_handle_cleanups, |
| 943 | uword_to_pointer (thread_index, void *)); |
| 944 | } |
| 945 | } |
| 946 | |
| 947 | static void |
| 948 | ct_program_cleanup (ct_connection_t *ct) |
| 949 | { |
| 950 | ct_main_t *cm = &ct_main; |
| 951 | ct_cleanup_req_t *req; |
| 952 | uword thread_index; |
| 953 | |
| 954 | thread_index = ct->c_thread_index; |
| 955 | clib_fifo_add2 (cm->pending_cleanups[thread_index], req); |
| 956 | req->ct_index = ct->c_c_index; |
| 957 | |
| 958 | if (cm->heave_cleanups[thread_index]) |
| 959 | return; |
| 960 | |
| 961 | cm->heave_cleanups[thread_index] = 1; |
| 962 | session_send_rpc_evt_to_thread_force ( |
| 963 | thread_index, ct_handle_cleanups, uword_to_pointer (thread_index, void *)); |
| 964 | } |
| 965 | |
| 966 | static void |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 967 | ct_session_close (u32 ct_index, u32 thread_index) |
| 968 | { |
| 969 | ct_connection_t *ct, *peer_ct; |
| 970 | session_t *s; |
| 971 | |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 972 | ct = ct_connection_get (ct_index, thread_index); |
Florin Coras | 6973c73 | 2021-06-17 15:53:38 -0700 | [diff] [blame] | 973 | s = session_get (ct->c_s_index, ct->c_thread_index); |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 974 | peer_ct = ct_connection_get (ct->peer_index, thread_index); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 975 | if (peer_ct) |
| 976 | { |
| 977 | peer_ct->peer_index = ~0; |
Florin Coras | 06ac4bb | 2020-10-28 16:41:26 -0700 | [diff] [blame] | 978 | /* Make sure session was allocated */ |
Florin Coras | 374df7a | 2021-05-13 11:37:43 -0700 | [diff] [blame] | 979 | if (peer_ct->flags & CT_CONN_F_HALF_OPEN) |
| 980 | { |
Florin Coras | 6973c73 | 2021-06-17 15:53:38 -0700 | [diff] [blame] | 981 | ct_session_connect_notify (s, SESSION_E_REFUSED); |
Florin Coras | 374df7a | 2021-05-13 11:37:43 -0700 | [diff] [blame] | 982 | } |
| 983 | else if (peer_ct->c_s_index != ~0) |
Florin Coras | 06ac4bb | 2020-10-28 16:41:26 -0700 | [diff] [blame] | 984 | session_transport_closing_notify (&peer_ct->connection); |
| 985 | else |
Florin Coras | 440c7b5 | 2021-11-09 08:38:24 -0800 | [diff] [blame^] | 986 | { |
| 987 | /* should not happen */ |
| 988 | clib_warning ("ct peer without session"); |
| 989 | ct_connection_free (peer_ct); |
| 990 | } |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 991 | } |
| 992 | |
Florin Coras | 440c7b5 | 2021-11-09 08:38:24 -0800 | [diff] [blame^] | 993 | /* Do not send closed notify to make sure pending tx events are |
| 994 | * still delivered and program cleanup */ |
| 995 | ct_program_cleanup (ct); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 996 | } |
| 997 | |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 998 | static transport_connection_t * |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 999 | ct_session_get (u32 ct_index, u32 thread_index) |
| 1000 | { |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 1001 | return (transport_connection_t *) ct_connection_get (ct_index, |
| 1002 | thread_index); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 1003 | } |
| 1004 | |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 1005 | static u8 * |
| 1006 | format_ct_connection_id (u8 * s, va_list * args) |
| 1007 | { |
| 1008 | ct_connection_t *ct = va_arg (*args, ct_connection_t *); |
| 1009 | if (!ct) |
| 1010 | return s; |
| 1011 | if (ct->c_is_ip4) |
| 1012 | { |
| 1013 | s = format (s, "[%d:%d][CT:%U] %U:%d->%U:%d", ct->c_thread_index, |
| 1014 | ct->c_s_index, format_transport_proto_short, ct->actual_tp, |
| 1015 | format_ip4_address, &ct->c_lcl_ip4, |
| 1016 | clib_net_to_host_u16 (ct->c_lcl_port), format_ip4_address, |
| 1017 | &ct->c_rmt_ip4, clib_net_to_host_u16 (ct->c_rmt_port)); |
| 1018 | } |
| 1019 | else |
| 1020 | { |
| 1021 | s = format (s, "[%d:%d][CT:%U] %U:%d->%U:%d", ct->c_thread_index, |
| 1022 | ct->c_s_index, format_transport_proto_short, ct->actual_tp, |
| 1023 | format_ip6_address, &ct->c_lcl_ip6, |
| 1024 | clib_net_to_host_u16 (ct->c_lcl_port), format_ip6_address, |
| 1025 | &ct->c_rmt_ip6, clib_net_to_host_u16 (ct->c_rmt_port)); |
| 1026 | } |
| 1027 | |
| 1028 | return s; |
| 1029 | } |
| 1030 | |
Florin Coras | f940f8a | 2019-03-06 10:44:38 -0800 | [diff] [blame] | 1031 | static int |
Florin Coras | 9f86d22 | 2020-03-23 15:34:22 +0000 | [diff] [blame] | 1032 | ct_custom_tx (void *session, transport_send_params_t * sp) |
Florin Coras | f940f8a | 2019-03-06 10:44:38 -0800 | [diff] [blame] | 1033 | { |
| 1034 | session_t *s = (session_t *) session; |
| 1035 | if (session_has_transport (s)) |
| 1036 | return 0; |
Florin Coras | 1b9d2c2 | 2021-01-26 14:10:43 -0800 | [diff] [blame] | 1037 | /* If event enqueued towards peer, remove from scheduler and remove |
| 1038 | * session tx flag, i.e., accept new tx events. Unset fifo flag now to |
| 1039 | * avoid missing events if peer did not clear fifo flag yet, which is |
| 1040 | * interpreted as successful notification and session is descheduled. */ |
| 1041 | svm_fifo_unset_event (s->tx_fifo); |
Florin Coras | d689456 | 2020-10-28 00:37:15 -0700 | [diff] [blame] | 1042 | if (!ct_session_tx (s)) |
Florin Coras | 1b9d2c2 | 2021-01-26 14:10:43 -0800 | [diff] [blame] | 1043 | sp->flags = TRANSPORT_SND_F_DESCHED; |
| 1044 | |
Florin Coras | d689456 | 2020-10-28 00:37:15 -0700 | [diff] [blame] | 1045 | /* The scheduler uses packet count as a means of upper bounding the amount |
| 1046 | * of work done per dispatch. So make it look like we have sent something */ |
| 1047 | return 1; |
Florin Coras | f940f8a | 2019-03-06 10:44:38 -0800 | [diff] [blame] | 1048 | } |
| 1049 | |
Florin Coras | 317b8e0 | 2019-04-17 09:57:46 -0700 | [diff] [blame] | 1050 | static int |
| 1051 | ct_app_rx_evt (transport_connection_t * tc) |
| 1052 | { |
| 1053 | ct_connection_t *ct = (ct_connection_t *) tc, *peer_ct; |
| 1054 | session_t *ps; |
| 1055 | |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 1056 | peer_ct = ct_connection_get (ct->peer_index, tc->thread_index); |
Florin Coras | 317b8e0 | 2019-04-17 09:57:46 -0700 | [diff] [blame] | 1057 | if (!peer_ct) |
| 1058 | return -1; |
| 1059 | ps = session_get (peer_ct->c_s_index, peer_ct->c_thread_index); |
| 1060 | return session_dequeue_notify (ps); |
| 1061 | } |
| 1062 | |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 1063 | static u8 * |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 1064 | format_ct_listener (u8 * s, va_list * args) |
| 1065 | { |
| 1066 | u32 tc_index = va_arg (*args, u32); |
Aloys Augustin | a0abbff | 2019-07-12 12:16:16 +0200 | [diff] [blame] | 1067 | u32 __clib_unused thread_index = va_arg (*args, u32); |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 1068 | u32 __clib_unused verbose = va_arg (*args, u32); |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 1069 | ct_connection_t *ct = ct_connection_get (tc_index, 0); |
Florin Coras | 7bf6ed6 | 2020-09-23 12:02:08 -0700 | [diff] [blame] | 1070 | s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_ct_connection_id, ct); |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 1071 | if (verbose) |
Florin Coras | 7bf6ed6 | 2020-09-23 12:02:08 -0700 | [diff] [blame] | 1072 | s = format (s, "%-" SESSION_CLI_STATE_LEN "s", "LISTEN"); |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 1073 | return s; |
| 1074 | } |
| 1075 | |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 1076 | static u8 * |
Florin Coras | 374df7a | 2021-05-13 11:37:43 -0700 | [diff] [blame] | 1077 | format_ct_half_open (u8 *s, va_list *args) |
| 1078 | { |
| 1079 | u32 ho_index = va_arg (*args, u32); |
| 1080 | u32 verbose = va_arg (*args, u32); |
| 1081 | ct_connection_t *ct = ct_connection_get (ho_index, 0); |
| 1082 | s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_ct_connection_id, ct); |
| 1083 | if (verbose) |
| 1084 | s = format (s, "%-" SESSION_CLI_STATE_LEN "s", "HALF-OPEN"); |
| 1085 | return s; |
| 1086 | } |
| 1087 | |
| 1088 | static u8 * |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 1089 | format_ct_connection (u8 * s, va_list * args) |
| 1090 | { |
| 1091 | ct_connection_t *ct = va_arg (*args, ct_connection_t *); |
| 1092 | u32 verbose = va_arg (*args, u32); |
| 1093 | |
| 1094 | if (!ct) |
| 1095 | return s; |
Florin Coras | 7bf6ed6 | 2020-09-23 12:02:08 -0700 | [diff] [blame] | 1096 | s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_ct_connection_id, ct); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 1097 | if (verbose) |
| 1098 | { |
Florin Coras | 7bf6ed6 | 2020-09-23 12:02:08 -0700 | [diff] [blame] | 1099 | s = format (s, "%-" SESSION_CLI_STATE_LEN "s", "ESTABLISHED"); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 1100 | if (verbose > 1) |
| 1101 | { |
| 1102 | s = format (s, "\n"); |
| 1103 | } |
| 1104 | } |
| 1105 | return s; |
| 1106 | } |
| 1107 | |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 1108 | static u8 * |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 1109 | format_ct_session (u8 * s, va_list * args) |
| 1110 | { |
| 1111 | u32 ct_index = va_arg (*args, u32); |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 1112 | u32 thread_index = va_arg (*args, u32); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 1113 | u32 verbose = va_arg (*args, u32); |
| 1114 | ct_connection_t *ct; |
| 1115 | |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 1116 | ct = ct_connection_get (ct_index, thread_index); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 1117 | if (!ct) |
| 1118 | { |
| 1119 | s = format (s, "empty\n"); |
| 1120 | return s; |
| 1121 | } |
| 1122 | |
| 1123 | s = format (s, "%U", format_ct_connection, ct, verbose); |
| 1124 | return s; |
| 1125 | } |
| 1126 | |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 1127 | clib_error_t * |
| 1128 | ct_enable_disable (vlib_main_t * vm, u8 is_en) |
| 1129 | { |
Florin Coras | 374df7a | 2021-05-13 11:37:43 -0700 | [diff] [blame] | 1130 | ct_main_t *cm = &ct_main; |
| 1131 | |
| 1132 | cm->n_workers = vlib_num_workers (); |
| 1133 | vec_validate (cm->connections, cm->n_workers); |
Florin Coras | 440c7b5 | 2021-11-09 08:38:24 -0800 | [diff] [blame^] | 1134 | vec_validate (cm->pending_cleanups, cm->n_workers); |
| 1135 | vec_validate (cm->heave_cleanups, cm->n_workers); |
Florin Coras | 374df7a | 2021-05-13 11:37:43 -0700 | [diff] [blame] | 1136 | clib_spinlock_init (&cm->ho_reuseable_lock); |
Florin Coras | da78c5a | 2021-06-09 14:55:24 -0700 | [diff] [blame] | 1137 | clib_rwlock_init (&cm->app_segs_lock); |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 1138 | return 0; |
| 1139 | } |
| 1140 | |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 1141 | /* *INDENT-OFF* */ |
Nathan Skrzypczak | e971bc9 | 2019-06-19 13:42:37 +0200 | [diff] [blame] | 1142 | static const transport_proto_vft_t cut_thru_proto = { |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 1143 | .enable = ct_enable_disable, |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 1144 | .start_listen = ct_start_listen, |
| 1145 | .stop_listen = ct_stop_listen, |
Florin Coras | 374df7a | 2021-05-13 11:37:43 -0700 | [diff] [blame] | 1146 | .get_connection = ct_session_get, |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 1147 | .get_listener = ct_listener_get, |
Florin Coras | 374df7a | 2021-05-13 11:37:43 -0700 | [diff] [blame] | 1148 | .get_half_open = ct_half_open_get, |
| 1149 | .cleanup = ct_session_cleanup, |
| 1150 | .cleanup_ho = ct_cleanup_ho, |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 1151 | .connect = ct_session_connect, |
| 1152 | .close = ct_session_close, |
Florin Coras | f940f8a | 2019-03-06 10:44:38 -0800 | [diff] [blame] | 1153 | .custom_tx = ct_custom_tx, |
Florin Coras | 317b8e0 | 2019-04-17 09:57:46 -0700 | [diff] [blame] | 1154 | .app_rx_evt = ct_app_rx_evt, |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 1155 | .format_listener = format_ct_listener, |
Florin Coras | 374df7a | 2021-05-13 11:37:43 -0700 | [diff] [blame] | 1156 | .format_half_open = format_ct_half_open, |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 1157 | .format_connection = format_ct_session, |
Nathan Skrzypczak | e971bc9 | 2019-06-19 13:42:37 +0200 | [diff] [blame] | 1158 | .transport_options = { |
Florin Coras | 07063b8 | 2020-03-13 04:44:51 +0000 | [diff] [blame] | 1159 | .name = "ct", |
| 1160 | .short_name = "C", |
Nathan Skrzypczak | e971bc9 | 2019-06-19 13:42:37 +0200 | [diff] [blame] | 1161 | .tx_type = TRANSPORT_TX_INTERNAL, |
Florin Coras | 374df7a | 2021-05-13 11:37:43 -0700 | [diff] [blame] | 1162 | .service_type = TRANSPORT_SERVICE_VC, |
Nathan Skrzypczak | e971bc9 | 2019-06-19 13:42:37 +0200 | [diff] [blame] | 1163 | }, |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 1164 | }; |
| 1165 | /* *INDENT-ON* */ |
| 1166 | |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 1167 | int |
| 1168 | ct_session_tx (session_t * s) |
| 1169 | { |
| 1170 | ct_connection_t *ct, *peer_ct; |
| 1171 | session_t *peer_s; |
| 1172 | |
| 1173 | ct = (ct_connection_t *) session_get_transport (s); |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 1174 | peer_ct = ct_connection_get (ct->peer_index, ct->c_thread_index); |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 1175 | if (!peer_ct) |
Florin Coras | bcdc50d | 2020-08-25 19:07:02 -0700 | [diff] [blame] | 1176 | return 0; |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 1177 | peer_s = session_get (peer_ct->c_s_index, peer_ct->c_thread_index); |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 1178 | if (peer_s->session_state >= SESSION_STATE_TRANSPORT_CLOSING) |
| 1179 | return 0; |
Florin Coras | d689456 | 2020-10-28 00:37:15 -0700 | [diff] [blame] | 1180 | return session_enqueue_notify (peer_s); |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 1181 | } |
| 1182 | |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 1183 | static clib_error_t * |
| 1184 | ct_transport_init (vlib_main_t * vm) |
| 1185 | { |
| 1186 | transport_register_protocol (TRANSPORT_PROTO_NONE, &cut_thru_proto, |
| 1187 | FIB_PROTOCOL_IP4, ~0); |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 1188 | transport_register_protocol (TRANSPORT_PROTO_NONE, &cut_thru_proto, |
| 1189 | FIB_PROTOCOL_IP6, ~0); |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 1190 | return 0; |
| 1191 | } |
| 1192 | |
| 1193 | VLIB_INIT_FUNCTION (ct_transport_init); |
| 1194 | |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 1195 | /* |
| 1196 | * fd.io coding-style-patch-verification: ON |
| 1197 | * |
| 1198 | * Local Variables: |
| 1199 | * eval: (c-set-style "gnu") |
| 1200 | * End: |
| 1201 | */ |