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