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