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