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