blob: 3ac2ba4cfbc223db98cec4c749c0685f68d03d93 [file] [log] [blame]
Florin Corasba7d8f52019-02-22 13:11:38 -08001/*
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 Corasda78c5a2021-06-09 14:55:24 -070019typedef 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
25typedef struct ct_segment_
26{
Florin Corasda78c5a2021-06-09 14:55:24 -070027 u32 client_n_sessions;
28 u32 server_n_sessions;
Florin Coras6973c732021-06-17 15:53:38 -070029 u32 seg_ctx_index;
30 u32 ct_seg_index;
31 u32 segment_index;
Florin Corasda78c5a2021-06-09 14:55:24 -070032 ct_segment_flags_t flags;
33} ct_segment_t;
34
35typedef struct ct_segments_
36{
37 u32 sm_index;
38 u32 server_wrk;
39 u32 client_wrk;
Florin Coras6973c732021-06-17 15:53:38 -070040 u32 fifo_pair_bytes;
Florin Corasda78c5a2021-06-09 14:55:24 -070041 ct_segment_t *segments;
42} ct_segments_ctx_t;
43
Florin Coras440c7b52021-11-09 08:38:24 -080044typedef struct ct_cleanup_req_
45{
46 u32 ct_index;
47} ct_cleanup_req_t;
48
Florin Coras7fe00692021-11-15 14:01:02 -080049typedef struct ct_worker_
50{
51 ct_connection_t *connections; /**< Per-worker connection pools */
Florin Corasc215e5a2021-11-15 19:01:52 -080052 u32 *pending_connects; /**< Fifo of pending ho indices */
Florin Coras7fe00692021-11-15 14:01:02 -080053 ct_cleanup_req_t *pending_cleanups; /**< Fifo of pending indices */
Florin Corasc215e5a2021-11-15 19:01:52 -080054 u8 have_connects; /**< Set if connect rpc pending */
Florin Coras7fe00692021-11-15 14:01:02 -080055 u8 have_cleanups; /**< Set if cleanup rpc pending */
Florin Coras55686e12023-03-20 09:58:01 -070056 clib_spinlock_t pending_connects_lock; /**< Lock for pending connects */
57 u32 *new_connects; /**< Burst of connects to be done */
Florin Coras7fe00692021-11-15 14:01:02 -080058} ct_worker_t;
59
Florin Coras2d0e3de2020-10-23 16:31:40 -070060typedef struct ct_main_
Florin Coras653e43f2019-03-04 10:56:23 -080061{
Florin Coras7fe00692021-11-15 14:01:02 -080062 ct_worker_t *wrk; /**< Per-worker state */
Florin Coras2d0e3de2020-10-23 16:31:40 -070063 u32 n_workers; /**< Number of vpp workers */
64 u32 n_sessions; /**< Cumulative sessions counter */
Florin Coras374df7a2021-05-13 11:37:43 -070065 u32 *ho_reusable; /**< Vector of reusable ho indices */
66 clib_spinlock_t ho_reuseable_lock; /**< Lock for reusable ho indices */
Florin Corasda78c5a2021-06-09 14:55:24 -070067 clib_rwlock_t app_segs_lock; /**< RW lock for seg contexts */
68 uword *app_segs_ctxs_table; /**< App handle to segment pool map */
69 ct_segments_ctx_t *app_seg_ctxs; /**< Pool of ct segment contexts */
Florin Coras55686e12023-03-20 09:58:01 -070070 u32 **fwrk_pending_connects; /**< First wrk pending half-opens */
71 u32 fwrk_thread; /**< First worker thread */
72 u8 fwrk_have_flush; /**< Flag for connect flush rpc */
Florin Coras75e8e1e2024-07-02 04:34:54 -070073 u8 is_init;
Florin Coras2d0e3de2020-10-23 16:31:40 -070074} ct_main_t;
Florin Coras653e43f2019-03-04 10:56:23 -080075
Florin Coras2d0e3de2020-10-23 16:31:40 -070076static ct_main_t ct_main;
Florin Coras653e43f2019-03-04 10:56:23 -080077
Florin Coras7fe00692021-11-15 14:01:02 -080078static inline ct_worker_t *
79ct_worker_get (u32 thread_index)
80{
81 return &ct_main.wrk[thread_index];
82}
83
Florin Coras653e43f2019-03-04 10:56:23 -080084static ct_connection_t *
Florin Coras2d0e3de2020-10-23 16:31:40 -070085ct_connection_alloc (u32 thread_index)
Florin Corasd4295e62019-02-22 13:11:38 -080086{
Florin Coras7fe00692021-11-15 14:01:02 -080087 ct_worker_t *wrk = ct_worker_get (thread_index);
Florin Corasd4295e62019-02-22 13:11:38 -080088 ct_connection_t *ct;
89
Florin Coras9688b3b2022-04-07 12:58:13 -070090 pool_get_aligned_safe (wrk->connections, ct, CLIB_CACHE_LINE_BYTES);
91 clib_memset (ct, 0, sizeof (*ct));
Florin Coras7fe00692021-11-15 14:01:02 -080092 ct->c_c_index = ct - wrk->connections;
Florin Coras2d0e3de2020-10-23 16:31:40 -070093 ct->c_thread_index = thread_index;
Florin Corasd4295e62019-02-22 13:11:38 -080094 ct->client_wrk = ~0;
95 ct->server_wrk = ~0;
Florin Corasda78c5a2021-06-09 14:55:24 -070096 ct->seg_ctx_index = ~0;
97 ct->ct_seg_index = ~0;
Florin Corasd4295e62019-02-22 13:11:38 -080098 return ct;
99}
100
Florin Coras653e43f2019-03-04 10:56:23 -0800101static ct_connection_t *
Florin Coras2d0e3de2020-10-23 16:31:40 -0700102ct_connection_get (u32 ct_index, u32 thread_index)
Florin Corasd4295e62019-02-22 13:11:38 -0800103{
Florin Coras7fe00692021-11-15 14:01:02 -0800104 ct_worker_t *wrk = ct_worker_get (thread_index);
105
106 if (pool_is_free_index (wrk->connections, ct_index))
Florin Corasd4295e62019-02-22 13:11:38 -0800107 return 0;
Florin Coras7fe00692021-11-15 14:01:02 -0800108 return pool_elt_at_index (wrk->connections, ct_index);
Florin Corasba7d8f52019-02-22 13:11:38 -0800109}
110
Florin Coras653e43f2019-03-04 10:56:23 -0800111static void
Florin Corasd4295e62019-02-22 13:11:38 -0800112ct_connection_free (ct_connection_t * ct)
Florin Corasba7d8f52019-02-22 13:11:38 -0800113{
Florin Coras7fe00692021-11-15 14:01:02 -0800114 ct_worker_t *wrk = ct_worker_get (ct->c_thread_index);
115
Florin Corasba7d8f52019-02-22 13:11:38 -0800116 if (CLIB_DEBUG)
Florin Coras2d0e3de2020-10-23 16:31:40 -0700117 {
Florin Coras7fe00692021-11-15 14:01:02 -0800118 clib_memset (ct, 0xfc, sizeof (*ct));
119 pool_put (wrk->connections, ct);
Florin Coras2d0e3de2020-10-23 16:31:40 -0700120 return;
121 }
Florin Coras7fe00692021-11-15 14:01:02 -0800122 pool_put (wrk->connections, ct);
Florin Corasba7d8f52019-02-22 13:11:38 -0800123}
124
Florin Coras374df7a2021-05-13 11:37:43 -0700125static ct_connection_t *
126ct_half_open_alloc (void)
127{
128 ct_main_t *cm = &ct_main;
129 u32 *hip;
130
131 clib_spinlock_lock (&cm->ho_reuseable_lock);
132 vec_foreach (hip, cm->ho_reusable)
Florin Coras55686e12023-03-20 09:58:01 -0700133 pool_put_index (cm->wrk[cm->fwrk_thread].connections, *hip);
Florin Coras374df7a2021-05-13 11:37:43 -0700134 vec_reset_length (cm->ho_reusable);
135 clib_spinlock_unlock (&cm->ho_reuseable_lock);
136
Florin Coras55686e12023-03-20 09:58:01 -0700137 return ct_connection_alloc (cm->fwrk_thread);
138}
139
140static ct_connection_t *
141ct_half_open_get (u32 ho_index)
142{
143 ct_main_t *cm = &ct_main;
144 return ct_connection_get (ho_index, cm->fwrk_thread);
Florin Coras374df7a2021-05-13 11:37:43 -0700145}
146
147void
148ct_half_open_add_reusable (u32 ho_index)
149{
150 ct_main_t *cm = &ct_main;
151
152 clib_spinlock_lock (&cm->ho_reuseable_lock);
153 vec_add1 (cm->ho_reusable, ho_index);
154 clib_spinlock_unlock (&cm->ho_reuseable_lock);
155}
156
Florin Coras2b81e3c2019-02-27 07:55:46 -0800157session_t *
158ct_session_get_peer (session_t * s)
159{
160 ct_connection_t *ct, *peer_ct;
Florin Coras2d0e3de2020-10-23 16:31:40 -0700161 ct = ct_connection_get (s->connection_index, s->thread_index);
162 peer_ct = ct_connection_get (ct->peer_index, s->thread_index);
163 return session_get (peer_ct->c_s_index, s->thread_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800164}
165
Florin Corasba7d8f52019-02-22 13:11:38 -0800166void
Florin Coras2b81e3c2019-02-27 07:55:46 -0800167ct_session_endpoint (session_t * ll, session_endpoint_t * sep)
Florin Corasba7d8f52019-02-22 13:11:38 -0800168{
Florin Corasd4295e62019-02-22 13:11:38 -0800169 ct_connection_t *ct;
170 ct = (ct_connection_t *) session_get_transport (ll);
171 sep->transport_proto = ct->actual_tp;
172 sep->port = ct->c_lcl_port;
173 sep->is_ip4 = ct->c_is_ip4;
Florin Corasea7e7082020-07-07 17:57:28 -0700174 ip_copy (&sep->ip, &ct->c_lcl_ip, ct->c_is_ip4);
Florin Corasba7d8f52019-02-22 13:11:38 -0800175}
176
Florin Corasda78c5a2021-06-09 14:55:24 -0700177static void
Florin Coras98952382021-11-14 15:33:59 -0800178ct_set_invalid_app_wrk (ct_connection_t *ct, u8 is_client)
179{
180 ct_connection_t *peer_ct;
181
182 peer_ct = ct_connection_get (ct->peer_index, ct->c_thread_index);
183
184 if (is_client)
185 {
186 ct->client_wrk = APP_INVALID_INDEX;
187 if (peer_ct)
188 ct->client_wrk = APP_INVALID_INDEX;
189 }
190 else
191 {
192 ct->server_wrk = APP_INVALID_INDEX;
193 if (peer_ct)
194 ct->server_wrk = APP_INVALID_INDEX;
195 }
196}
197
Florin Coras93752662023-11-20 14:46:10 -0800198static inline u64
199ct_client_seg_handle (u64 server_sh, u32 client_wrk_index)
200{
201 return (((u64) client_wrk_index << 56) | server_sh);
202}
203
Florin Coras98952382021-11-14 15:33:59 -0800204static void
Florin Corasda78c5a2021-06-09 14:55:24 -0700205ct_session_dealloc_fifos (ct_connection_t *ct, svm_fifo_t *rx_fifo,
206 svm_fifo_t *tx_fifo)
Florin Corasba7d8f52019-02-22 13:11:38 -0800207{
Florin Corasda78c5a2021-06-09 14:55:24 -0700208 ct_segments_ctx_t *seg_ctx;
209 ct_main_t *cm = &ct_main;
Florin Corasba7d8f52019-02-22 13:11:38 -0800210 segment_manager_t *sm;
Florin Corasda78c5a2021-06-09 14:55:24 -0700211 app_worker_t *app_wrk;
212 ct_segment_t *ct_seg;
213 fifo_segment_t *fs;
214 u32 seg_index;
Florin Coras98952382021-11-14 15:33:59 -0800215 session_t *s;
Florin Coras6973c732021-06-17 15:53:38 -0700216 int cnt;
Florin Corasda78c5a2021-06-09 14:55:24 -0700217
218 /*
219 * Cleanup fifos
220 */
221
222 sm = segment_manager_get (rx_fifo->segment_manager);
223 seg_index = rx_fifo->segment_index;
224
225 fs = segment_manager_get_segment_w_lock (sm, seg_index);
226 fifo_segment_free_fifo (fs, rx_fifo);
227 fifo_segment_free_fifo (fs, tx_fifo);
228 segment_manager_segment_reader_unlock (sm);
229
230 /*
Florin Coras6973c732021-06-17 15:53:38 -0700231 * Atomically update segment context with readers lock
Florin Corasda78c5a2021-06-09 14:55:24 -0700232 */
233
234 clib_rwlock_reader_lock (&cm->app_segs_lock);
235
236 seg_ctx = pool_elt_at_index (cm->app_seg_ctxs, ct->seg_ctx_index);
237 ct_seg = pool_elt_at_index (seg_ctx->segments, ct->ct_seg_index);
238
239 if (ct->flags & CT_CONN_F_CLIENT)
240 {
241 cnt =
242 __atomic_sub_fetch (&ct_seg->client_n_sessions, 1, __ATOMIC_RELAXED);
Florin Corasda78c5a2021-06-09 14:55:24 -0700243 }
244 else
245 {
246 cnt =
247 __atomic_sub_fetch (&ct_seg->server_n_sessions, 1, __ATOMIC_RELAXED);
Florin Corasda78c5a2021-06-09 14:55:24 -0700248 }
249
Florin Corasda78c5a2021-06-09 14:55:24 -0700250 clib_rwlock_reader_unlock (&cm->app_segs_lock);
251
252 /*
253 * No need to do any app updates, return
254 */
Florin Coras6973c732021-06-17 15:53:38 -0700255 ASSERT (cnt >= 0);
256 if (cnt)
257 return;
258
259 /*
260 * Grab exclusive lock and update flags unless some other thread
261 * added more sessions
262 */
263 clib_rwlock_writer_lock (&cm->app_segs_lock);
264
265 seg_ctx = pool_elt_at_index (cm->app_seg_ctxs, ct->seg_ctx_index);
266 ct_seg = pool_elt_at_index (seg_ctx->segments, ct->ct_seg_index);
267 if (ct->flags & CT_CONN_F_CLIENT)
268 {
269 cnt = ct_seg->client_n_sessions;
Florin Coras98952382021-11-14 15:33:59 -0800270 if (cnt)
271 goto done;
272 ct_seg->flags |= CT_SEGMENT_F_CLIENT_DETACHED;
273 s = session_get (ct->c_s_index, ct->c_thread_index);
274 if (s->app_wrk_index == APP_INVALID_INDEX)
275 ct_set_invalid_app_wrk (ct, 1 /* is_client */);
Florin Coras6973c732021-06-17 15:53:38 -0700276 }
277 else
278 {
279 cnt = ct_seg->server_n_sessions;
Florin Coras98952382021-11-14 15:33:59 -0800280 if (cnt)
281 goto done;
282 ct_seg->flags |= CT_SEGMENT_F_SERVER_DETACHED;
283 s = session_get (ct->c_s_index, ct->c_thread_index);
284 if (s->app_wrk_index == APP_INVALID_INDEX)
285 ct_set_invalid_app_wrk (ct, 0 /* is_client */);
Florin Coras6973c732021-06-17 15:53:38 -0700286 }
287
Florin Coras98952382021-11-14 15:33:59 -0800288 if (!(ct_seg->flags & CT_SEGMENT_F_CLIENT_DETACHED) ||
289 !(ct_seg->flags & CT_SEGMENT_F_SERVER_DETACHED))
290 goto done;
291
Florin Coras6973c732021-06-17 15:53:38 -0700292 /*
293 * Remove segment context because both client and server detached
294 */
295
Florin Coras98952382021-11-14 15:33:59 -0800296 pool_put_index (seg_ctx->segments, ct->ct_seg_index);
Florin Coras6973c732021-06-17 15:53:38 -0700297
298 /*
Florin Coras98952382021-11-14 15:33:59 -0800299 * No more segment indices left, remove the segments context
Florin Coras6973c732021-06-17 15:53:38 -0700300 */
Florin Coras98952382021-11-14 15:33:59 -0800301 if (!pool_elts (seg_ctx->segments))
Florin Corasda78c5a2021-06-09 14:55:24 -0700302 {
Florin Coras98952382021-11-14 15:33:59 -0800303 u64 table_handle = seg_ctx->client_wrk << 16 | seg_ctx->server_wrk;
304 table_handle = (u64) seg_ctx->sm_index << 32 | table_handle;
305 hash_unset (cm->app_segs_ctxs_table, table_handle);
306 pool_free (seg_ctx->segments);
307 pool_put_index (cm->app_seg_ctxs, ct->seg_ctx_index);
Florin Corasda78c5a2021-06-09 14:55:24 -0700308 }
Florin Coras98952382021-11-14 15:33:59 -0800309
310 /*
311 * Segment to be removed so notify both apps
312 */
313
314 app_wrk = app_worker_get_if_valid (ct->client_wrk);
315 /* Determine if client app still needs notification, i.e., if it is
316 * still attached. If client detached and this is the last ct session
317 * on this segment, then its connects segment manager should also be
318 * detached, so do not send notification */
319 if (app_wrk)
320 {
321 segment_manager_t *csm;
322 csm = app_worker_get_connect_segment_manager (app_wrk);
323 if (!segment_manager_app_detached (csm))
Florin Coras93752662023-11-20 14:46:10 -0800324 app_worker_del_segment_notify (
325 app_wrk, ct_client_seg_handle (ct->segment_handle, ct->client_wrk));
Florin Coras98952382021-11-14 15:33:59 -0800326 }
327
Florin Coras13987da2021-12-07 14:47:12 -0800328 /* Notify server app and free segment */
Florin Corasda78c5a2021-06-09 14:55:24 -0700329 segment_manager_lock_and_del_segment (sm, seg_index);
330
331 /* Cleanup segment manager if needed. If server detaches there's a chance
332 * the client's sessions will hold up segment removal */
333 if (segment_manager_app_detached (sm) && !segment_manager_has_fifos (sm))
334 segment_manager_free_safe (sm);
Florin Coras98952382021-11-14 15:33:59 -0800335
336done:
337
338 clib_rwlock_writer_unlock (&cm->app_segs_lock);
Florin Corasda78c5a2021-06-09 14:55:24 -0700339}
340
Florin Corasa7bd8262021-11-25 12:14:25 -0800341static void
342ct_session_force_disconnect_server (ct_connection_t *sct)
343{
344 sct->peer_index = ~0;
345 session_transport_closing_notify (&sct->connection);
346}
347
Florin Corasda78c5a2021-06-09 14:55:24 -0700348int
Florin Coras6973c732021-06-17 15:53:38 -0700349ct_session_connect_notify (session_t *ss, session_error_t err)
Florin Corasda78c5a2021-06-09 14:55:24 -0700350{
Florin Coras6973c732021-06-17 15:53:38 -0700351 u32 ss_index, opaque, thread_index;
Florin Corasda78c5a2021-06-09 14:55:24 -0700352 ct_connection_t *sct, *cct;
Florin Corasda78c5a2021-06-09 14:55:24 -0700353 app_worker_t *client_wrk;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800354 session_t *cs;
Florin Corasba7d8f52019-02-22 13:11:38 -0800355
Florin Coras2b81e3c2019-02-27 07:55:46 -0800356 ss_index = ss->session_index;
Florin Coras2d0e3de2020-10-23 16:31:40 -0700357 thread_index = ss->thread_index;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800358 sct = (ct_connection_t *) session_get_transport (ss);
359 client_wrk = app_worker_get (sct->client_wrk);
Florin Corasdd7d24b2020-08-14 17:51:13 -0700360 opaque = sct->client_opaque;
Florin Corasba7d8f52019-02-22 13:11:38 -0800361
Florin Coras2d0e3de2020-10-23 16:31:40 -0700362 cct = ct_connection_get (sct->peer_index, thread_index);
Florin Corasba7d8f52019-02-22 13:11:38 -0800363
Florin Coras374df7a2021-05-13 11:37:43 -0700364 /* Client closed while waiting for reply from server */
Florin Coras6973c732021-06-17 15:53:38 -0700365 if (PREDICT_FALSE (!cct))
Florin Coras374df7a2021-05-13 11:37:43 -0700366 {
Florin Corasa7bd8262021-11-25 12:14:25 -0800367 ct_session_force_disconnect_server (sct);
Florin Coras374df7a2021-05-13 11:37:43 -0700368 return 0;
369 }
370
371 session_half_open_delete_notify (&cct->connection);
372 cct->flags &= ~CT_CONN_F_HALF_OPEN;
373
Florin Coras6973c732021-06-17 15:53:38 -0700374 if (PREDICT_FALSE (err))
375 goto connect_error;
Florin Corasda78c5a2021-06-09 14:55:24 -0700376
377 /*
Florin Corasa7bd8262021-11-25 12:14:25 -0800378 * Alloc client session, server session assumed to be established
Florin Corasda78c5a2021-06-09 14:55:24 -0700379 */
380
Florin Corasa7bd8262021-11-25 12:14:25 -0800381 ASSERT (ss->session_state >= SESSION_STATE_READY);
382
Florin Coras2d0e3de2020-10-23 16:31:40 -0700383 cs = session_alloc (thread_index);
384 ss = session_get (ss_index, thread_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800385 cs->session_type = ss->session_type;
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +0200386 cs->listener_handle = SESSION_INVALID_HANDLE;
Steven Luongd810a6e2022-10-25 13:09:11 -0700387 session_set_state (cs, SESSION_STATE_CONNECTING);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800388 cs->app_wrk_index = client_wrk->wrk_index;
389 cs->connection_index = cct->c_c_index;
Florin Coras2ceb8182023-08-31 17:33:47 -0700390 cs->opaque = opaque;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800391 cct->c_s_index = cs->session_index;
392
393 /* This will allocate fifos for the session. They won't be used for
394 * exchanging data but they will be used to close the connection if
395 * the segment manager/worker is freed */
Florin Corasdd7d24b2020-08-14 17:51:13 -0700396 if ((err = app_worker_init_connected (client_wrk, cs)))
Florin Coras2b81e3c2019-02-27 07:55:46 -0800397 {
Florin Corasdd7d24b2020-08-14 17:51:13 -0700398 session_free (cs);
Florin Corasa7bd8262021-11-25 12:14:25 -0800399 ct_session_force_disconnect_server (sct);
Florin Coras6973c732021-06-17 15:53:38 -0700400 err = SESSION_E_ALLOC;
401 goto connect_error;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800402 }
403
Steven Luongd810a6e2022-10-25 13:09:11 -0700404 session_set_state (cs, SESSION_STATE_CONNECTING);
Florin Corasdd7d24b2020-08-14 17:51:13 -0700405
Florin Coras6973c732021-06-17 15:53:38 -0700406 if (app_worker_connect_notify (client_wrk, cs, 0, opaque))
Florin Coras2b81e3c2019-02-27 07:55:46 -0800407 {
Florin Coras6973c732021-06-17 15:53:38 -0700408 segment_manager_dealloc_fifos (cs->rx_fifo, cs->tx_fifo);
Florin Corasdd7d24b2020-08-14 17:51:13 -0700409 session_free (cs);
Florin Corasa7bd8262021-11-25 12:14:25 -0800410 ct_session_force_disconnect_server (sct);
Florin Coras6973c732021-06-17 15:53:38 -0700411 goto cleanup_client;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800412 }
413
Florin Coras2d0e3de2020-10-23 16:31:40 -0700414 cs = session_get (cct->c_s_index, cct->c_thread_index);
Steven Luongd810a6e2022-10-25 13:09:11 -0700415 session_set_state (cs, SESSION_STATE_READY);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800416
Florin Corasba7d8f52019-02-22 13:11:38 -0800417 return 0;
Florin Corasdd7d24b2020-08-14 17:51:13 -0700418
Florin Coras6973c732021-06-17 15:53:38 -0700419connect_error:
420
421 app_worker_connect_notify (client_wrk, 0, err, cct->client_opaque);
422
423cleanup_client:
424
425 if (cct->client_rx_fifo)
426 ct_session_dealloc_fifos (cct, cct->client_rx_fifo, cct->client_tx_fifo);
427 ct_connection_free (cct);
Florin Corasdd7d24b2020-08-14 17:51:13 -0700428 return -1;
Florin Corasba7d8f52019-02-22 13:11:38 -0800429}
430
Florin Coras6973c732021-06-17 15:53:38 -0700431static inline ct_segment_t *
432ct_lookup_free_segment (ct_main_t *cm, segment_manager_t *sm,
433 u32 seg_ctx_index)
Florin Corasba7d8f52019-02-22 13:11:38 -0800434{
Florin Corasda78c5a2021-06-09 14:55:24 -0700435 uword free_bytes, max_free_bytes;
436 ct_segment_t *ct_seg, *res = 0;
Florin Coras6973c732021-06-17 15:53:38 -0700437 ct_segments_ctx_t *seg_ctx;
Florin Corasda78c5a2021-06-09 14:55:24 -0700438 fifo_segment_t *fs;
439 u32 max_fifos;
440
Florin Coras6973c732021-06-17 15:53:38 -0700441 seg_ctx = pool_elt_at_index (cm->app_seg_ctxs, seg_ctx_index);
442 max_free_bytes = seg_ctx->fifo_pair_bytes;
443
Florin Corasda78c5a2021-06-09 14:55:24 -0700444 pool_foreach (ct_seg, seg_ctx->segments)
445 {
446 /* Client or server has detached so segment cannot be used */
Florin Corasda78c5a2021-06-09 14:55:24 -0700447 fs = segment_manager_get_segment (sm, ct_seg->segment_index);
448 free_bytes = fifo_segment_available_bytes (fs);
Florin Coras6973c732021-06-17 15:53:38 -0700449 max_fifos = fifo_segment_size (fs) / seg_ctx->fifo_pair_bytes;
Florin Corasda78c5a2021-06-09 14:55:24 -0700450 if (free_bytes > max_free_bytes &&
451 fifo_segment_num_fifos (fs) / 2 < max_fifos)
452 {
453 max_free_bytes = free_bytes;
454 res = ct_seg;
455 }
456 }
457
458 return res;
459}
460
Florin Coras6973c732021-06-17 15:53:38 -0700461static ct_segment_t *
462ct_alloc_segment (ct_main_t *cm, app_worker_t *server_wrk, u64 table_handle,
463 segment_manager_t *sm, u32 client_wrk_index)
464{
465 u32 seg_ctx_index = ~0, sm_index, pair_bytes;
Florin Coras93752662023-11-20 14:46:10 -0800466 u64 seg_size, seg_handle, client_seg_handle;
Florin Coras6973c732021-06-17 15:53:38 -0700467 segment_manager_props_t *props;
468 const u32 margin = 16 << 10;
469 ct_segments_ctx_t *seg_ctx;
470 app_worker_t *client_wrk;
Florin Coras6973c732021-06-17 15:53:38 -0700471 application_t *server;
472 ct_segment_t *ct_seg;
473 uword *spp;
474 int fs_index;
475
476 server = application_get (server_wrk->app_index);
477 props = application_segment_manager_properties (server);
478 sm_index = segment_manager_index (sm);
479 pair_bytes = props->rx_fifo_size + props->tx_fifo_size + margin;
480
481 /*
482 * Make sure another thread did not alloc a segment while acquiring the lock
483 */
484
485 spp = hash_get (cm->app_segs_ctxs_table, table_handle);
486 if (spp)
487 {
488 seg_ctx_index = *spp;
489 ct_seg = ct_lookup_free_segment (cm, sm, seg_ctx_index);
490 if (ct_seg)
491 return ct_seg;
492 }
493
494 /*
495 * No segment, try to alloc one and notify the server and the client.
496 * Make sure the segment is not used for other fifos
497 */
498 seg_size = clib_max (props->segment_size, 128 << 20);
499 fs_index =
500 segment_manager_add_segment2 (sm, seg_size, FIFO_SEGMENT_F_CUSTOM_USE);
501 if (fs_index < 0)
502 return 0;
503
504 if (seg_ctx_index == ~0)
505 {
506 pool_get_zero (cm->app_seg_ctxs, seg_ctx);
507 seg_ctx_index = seg_ctx - cm->app_seg_ctxs;
508 hash_set (cm->app_segs_ctxs_table, table_handle, seg_ctx_index);
509 seg_ctx->server_wrk = server_wrk->wrk_index;
510 seg_ctx->client_wrk = client_wrk_index;
511 seg_ctx->sm_index = sm_index;
512 seg_ctx->fifo_pair_bytes = pair_bytes;
513 }
514 else
515 {
516 seg_ctx = pool_elt_at_index (cm->app_seg_ctxs, seg_ctx_index);
517 }
518
519 pool_get_zero (seg_ctx->segments, ct_seg);
520 ct_seg->segment_index = fs_index;
521 ct_seg->server_n_sessions = 0;
522 ct_seg->client_n_sessions = 0;
523 ct_seg->ct_seg_index = ct_seg - seg_ctx->segments;
524 ct_seg->seg_ctx_index = seg_ctx_index;
525
526 /* New segment, notify the server and client */
527 seg_handle = segment_manager_make_segment_handle (sm_index, fs_index);
528 if (app_worker_add_segment_notify (server_wrk, seg_handle))
529 goto error;
530
531 client_wrk = app_worker_get (client_wrk_index);
Florin Coras93752662023-11-20 14:46:10 -0800532 /* Make sure client workers do not have overlapping segment handles.
533 * Ideally, we should attach fs to client worker segment manager and
534 * create a new handle but that's not currently possible. */
535 client_seg_handle = ct_client_seg_handle (seg_handle, client_wrk_index);
536 if (app_worker_add_segment_notify (client_wrk, client_seg_handle))
Florin Coras6973c732021-06-17 15:53:38 -0700537 {
538 app_worker_del_segment_notify (server_wrk, seg_handle);
539 goto error;
540 }
541
542 return ct_seg;
543
544error:
545
546 segment_manager_lock_and_del_segment (sm, fs_index);
547 pool_put_index (seg_ctx->segments, ct_seg->seg_ctx_index);
548 return 0;
549}
550
Florin Corasda78c5a2021-06-09 14:55:24 -0700551static int
552ct_init_accepted_session (app_worker_t *server_wrk, ct_connection_t *ct,
553 session_t *ls, session_t *ll)
554{
Florin Coras88001c62019-04-24 14:44:46 -0700555 segment_manager_props_t *props;
Florin Coras6973c732021-06-17 15:53:38 -0700556 u64 seg_handle, table_handle;
557 u32 sm_index, fs_index = ~0;
Florin Corasda78c5a2021-06-09 14:55:24 -0700558 ct_segments_ctx_t *seg_ctx;
559 ct_main_t *cm = &ct_main;
Florin Coras653e43f2019-03-04 10:56:23 -0800560 application_t *server;
Florin Corasba7d8f52019-02-22 13:11:38 -0800561 segment_manager_t *sm;
Florin Corasda78c5a2021-06-09 14:55:24 -0700562 ct_segment_t *ct_seg;
563 fifo_segment_t *fs;
Florin Corasda78c5a2021-06-09 14:55:24 -0700564 uword *spp;
Florin Coras6973c732021-06-17 15:53:38 -0700565 int rv;
Florin Corasba7d8f52019-02-22 13:11:38 -0800566
Florin Coras2b81e3c2019-02-27 07:55:46 -0800567 sm = app_worker_get_listen_segment_manager (server_wrk, ll);
Florin Corasda78c5a2021-06-09 14:55:24 -0700568 sm_index = segment_manager_index (sm);
569 server = application_get (server_wrk->app_index);
570 props = application_segment_manager_properties (server);
Florin Corasba7d8f52019-02-22 13:11:38 -0800571
Florin Corasda78c5a2021-06-09 14:55:24 -0700572 table_handle = ct->client_wrk << 16 | server_wrk->wrk_index;
Florin Coras6973c732021-06-17 15:53:38 -0700573 table_handle = (u64) sm_index << 32 | table_handle;
Florin Corasda78c5a2021-06-09 14:55:24 -0700574
575 /*
576 * Check if we already have a segment that can hold the fifos
577 */
578
579 clib_rwlock_reader_lock (&cm->app_segs_lock);
580
581 spp = hash_get (cm->app_segs_ctxs_table, table_handle);
582 if (spp)
583 {
Florin Coras6973c732021-06-17 15:53:38 -0700584 ct_seg = ct_lookup_free_segment (cm, sm, *spp);
Florin Corasda78c5a2021-06-09 14:55:24 -0700585 if (ct_seg)
586 {
Florin Coras6973c732021-06-17 15:53:38 -0700587 ct->seg_ctx_index = ct_seg->seg_ctx_index;
588 ct->ct_seg_index = ct_seg->ct_seg_index;
Florin Corasda78c5a2021-06-09 14:55:24 -0700589 fs_index = ct_seg->segment_index;
Florin Coras98952382021-11-14 15:33:59 -0800590 ct_seg->flags &=
591 ~(CT_SEGMENT_F_SERVER_DETACHED | CT_SEGMENT_F_CLIENT_DETACHED);
Florin Corasda78c5a2021-06-09 14:55:24 -0700592 __atomic_add_fetch (&ct_seg->server_n_sessions, 1, __ATOMIC_RELAXED);
Florin Coras6973c732021-06-17 15:53:38 -0700593 __atomic_add_fetch (&ct_seg->client_n_sessions, 1, __ATOMIC_RELAXED);
Florin Corasda78c5a2021-06-09 14:55:24 -0700594 }
595 }
596
597 clib_rwlock_reader_unlock (&cm->app_segs_lock);
598
599 /*
Florin Coras6973c732021-06-17 15:53:38 -0700600 * If not, grab exclusive lock and allocate segment
Florin Corasda78c5a2021-06-09 14:55:24 -0700601 */
Florin Coras6973c732021-06-17 15:53:38 -0700602 if (fs_index == ~0)
Florin Corasda78c5a2021-06-09 14:55:24 -0700603 {
Florin Corasda78c5a2021-06-09 14:55:24 -0700604 clib_rwlock_writer_lock (&cm->app_segs_lock);
605
Florin Coras6973c732021-06-17 15:53:38 -0700606 ct_seg =
607 ct_alloc_segment (cm, server_wrk, table_handle, sm, ct->client_wrk);
608 if (!ct_seg)
Florin Corasda78c5a2021-06-09 14:55:24 -0700609 {
Florin Coras6973c732021-06-17 15:53:38 -0700610 clib_rwlock_writer_unlock (&cm->app_segs_lock);
611 return -1;
Florin Corasda78c5a2021-06-09 14:55:24 -0700612 }
Florin Corasda78c5a2021-06-09 14:55:24 -0700613
Florin Coras6973c732021-06-17 15:53:38 -0700614 ct->seg_ctx_index = ct_seg->seg_ctx_index;
615 ct->ct_seg_index = ct_seg->ct_seg_index;
Florin Corasda78c5a2021-06-09 14:55:24 -0700616 ct_seg->server_n_sessions += 1;
Florin Coras6973c732021-06-17 15:53:38 -0700617 ct_seg->client_n_sessions += 1;
618 fs_index = ct_seg->segment_index;
Florin Corasda78c5a2021-06-09 14:55:24 -0700619
620 clib_rwlock_writer_unlock (&cm->app_segs_lock);
Florin Corasda78c5a2021-06-09 14:55:24 -0700621 }
622
623 /*
624 * Allocate and initialize the fifos
625 */
626 fs = segment_manager_get_segment_w_lock (sm, fs_index);
627 rv = segment_manager_try_alloc_fifos (
628 fs, ls->thread_index, props->rx_fifo_size, props->tx_fifo_size,
629 &ls->rx_fifo, &ls->tx_fifo);
Florin Corasba7d8f52019-02-22 13:11:38 -0800630 if (rv)
631 {
Florin Corasba7d8f52019-02-22 13:11:38 -0800632 segment_manager_segment_reader_unlock (sm);
Florin Coras6973c732021-06-17 15:53:38 -0700633
634 clib_rwlock_reader_lock (&cm->app_segs_lock);
635
636 seg_ctx = pool_elt_at_index (cm->app_seg_ctxs, ct->seg_ctx_index);
637 ct_seg = pool_elt_at_index (seg_ctx->segments, ct->ct_seg_index);
638 __atomic_sub_fetch (&ct_seg->server_n_sessions, 1, __ATOMIC_RELAXED);
639 __atomic_sub_fetch (&ct_seg->client_n_sessions, 1, __ATOMIC_RELAXED);
640
641 clib_rwlock_reader_unlock (&cm->app_segs_lock);
642
643 return rv;
Florin Corasba7d8f52019-02-22 13:11:38 -0800644 }
Florin Coras2b81e3c2019-02-27 07:55:46 -0800645
Florin Corasc547e912020-12-08 17:50:45 -0800646 ls->rx_fifo->shr->master_session_index = ls->session_index;
647 ls->tx_fifo->shr->master_session_index = ls->session_index;
Florin Corasce252ca2020-11-04 13:08:35 -0800648 ls->rx_fifo->master_thread_index = ls->thread_index;
649 ls->tx_fifo->master_thread_index = ls->thread_index;
Florin Corasba7d8f52019-02-22 13:11:38 -0800650
Florin Corasda78c5a2021-06-09 14:55:24 -0700651 seg_handle = segment_manager_segment_handle (sm, fs);
Florin Corasba7d8f52019-02-22 13:11:38 -0800652 segment_manager_segment_reader_unlock (sm);
Florin Corasda78c5a2021-06-09 14:55:24 -0700653
654 ct->segment_handle = seg_handle;
Florin Corasba7d8f52019-02-22 13:11:38 -0800655
656 return 0;
Florin Corasba7d8f52019-02-22 13:11:38 -0800657}
658
Florin Coras2d0e3de2020-10-23 16:31:40 -0700659static void
Florin Corasc215e5a2021-11-15 19:01:52 -0800660ct_accept_one (u32 thread_index, u32 ho_index)
Florin Coras2d0e3de2020-10-23 16:31:40 -0700661{
Florin Corasba026412021-06-12 11:56:19 -0700662 ct_connection_t *sct, *cct, *ho;
663 transport_connection_t *ll_ct;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800664 app_worker_t *server_wrk;
Florin Corasc215e5a2021-11-15 19:01:52 -0800665 u32 cct_index, ll_index;
Florin Coras2d0e3de2020-10-23 16:31:40 -0700666 session_t *ss, *ll;
Florin Corasba7d8f52019-02-22 13:11:38 -0800667
Florin Coras374df7a2021-05-13 11:37:43 -0700668 /*
669 * Alloc client ct and initialize from ho
670 */
Florin Coras374df7a2021-05-13 11:37:43 -0700671 cct = ct_connection_alloc (thread_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800672 cct_index = cct->c_c_index;
Florin Coras374df7a2021-05-13 11:37:43 -0700673
Florin Coras55686e12023-03-20 09:58:01 -0700674 ho = ct_half_open_get (ho_index);
Florin Coras374df7a2021-05-13 11:37:43 -0700675
676 /* Unlikely but half-open session and transport could have been freed */
677 if (PREDICT_FALSE (!ho))
678 {
679 ct_connection_free (cct);
680 return;
681 }
682
683 clib_memcpy (cct, ho, sizeof (*ho));
684 cct->c_c_index = cct_index;
685 cct->c_thread_index = thread_index;
686 cct->flags |= CT_CONN_F_HALF_OPEN;
687
688 /* Notify session layer that half-open is on a different thread
689 * and mark ho connection index reusable. Avoids another rpc
690 */
691 session_half_open_migrate_notify (&cct->connection);
692 session_half_open_migrated_notify (&cct->connection);
693 ct_half_open_add_reusable (ho_index);
Florin Corasba7d8f52019-02-22 13:11:38 -0800694
Florin Coras2b81e3c2019-02-27 07:55:46 -0800695 /*
Florin Coras374df7a2021-05-13 11:37:43 -0700696 * Alloc and init server transport
Florin Coras2b81e3c2019-02-27 07:55:46 -0800697 */
Florin Coras374df7a2021-05-13 11:37:43 -0700698
699 ll_index = cct->peer_index;
700 ll = listen_session_get (ll_index);
701 sct = ct_connection_alloc (thread_index);
Florin Corasba026412021-06-12 11:56:19 -0700702 /* Transport not necessarily ct but it might, so grab after sct alloc */
703 ll_ct = listen_session_get_transport (ll);
Florin Coras374df7a2021-05-13 11:37:43 -0700704
705 /* Make sure cct is valid after sct alloc */
706 cct = ct_connection_get (cct_index, thread_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800707
Florin Coras2b81e3c2019-02-27 07:55:46 -0800708 sct->c_rmt_port = 0;
Florin Corasba026412021-06-12 11:56:19 -0700709 sct->c_lcl_port = ll_ct->lcl_port;
Florin Coras374df7a2021-05-13 11:37:43 -0700710 sct->c_is_ip4 = cct->c_is_ip4;
Florin Corasd9e98702021-10-11 18:10:41 -0700711 clib_memcpy (&sct->c_lcl_ip, &cct->c_rmt_ip, sizeof (cct->c_rmt_ip));
Florin Coras374df7a2021-05-13 11:37:43 -0700712 sct->client_wrk = cct->client_wrk;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800713 sct->c_proto = TRANSPORT_PROTO_NONE;
Florin Coras374df7a2021-05-13 11:37:43 -0700714 sct->client_opaque = cct->client_opaque;
Florin Corasba026412021-06-12 11:56:19 -0700715 sct->actual_tp = cct->actual_tp;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800716
717 sct->peer_index = cct->c_c_index;
718 cct->peer_index = sct->c_c_index;
719
720 /*
721 * Accept server session. Client session is created only after
722 * server confirms accept.
723 */
Florin Coras374df7a2021-05-13 11:37:43 -0700724 ss = session_alloc (thread_index);
725 ll = listen_session_get (ll_index);
Florin Coras9f1a5432019-03-10 21:03:20 -0700726 ss->session_type = session_type_from_proto_and_ip (TRANSPORT_PROTO_NONE,
727 sct->c_is_ip4);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800728 ss->connection_index = sct->c_c_index;
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +0200729 ss->listener_handle = listen_session_get_handle (ll);
Steven Luongd810a6e2022-10-25 13:09:11 -0700730 session_set_state (ss, SESSION_STATE_CREATED);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800731
732 server_wrk = application_listener_select_worker (ll);
733 ss->app_wrk_index = server_wrk->wrk_index;
734
735 sct->c_s_index = ss->session_index;
736 sct->server_wrk = ss->app_wrk_index;
737
Florin Coras2d0e3de2020-10-23 16:31:40 -0700738 if (ct_init_accepted_session (server_wrk, sct, ss, ll))
Florin Corasba7d8f52019-02-22 13:11:38 -0800739 {
Florin Coras6973c732021-06-17 15:53:38 -0700740 ct_session_connect_notify (ss, SESSION_E_ALLOC);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800741 ct_connection_free (sct);
742 session_free (ss);
Florin Coras2d0e3de2020-10-23 16:31:40 -0700743 return;
Florin Corasba7d8f52019-02-22 13:11:38 -0800744 }
745
Florin Coras98952382021-11-14 15:33:59 -0800746 cct->server_wrk = sct->server_wrk;
Florin Coras6973c732021-06-17 15:53:38 -0700747 cct->seg_ctx_index = sct->seg_ctx_index;
748 cct->ct_seg_index = sct->ct_seg_index;
749 cct->client_rx_fifo = ss->tx_fifo;
750 cct->client_tx_fifo = ss->rx_fifo;
751 cct->client_rx_fifo->refcnt++;
752 cct->client_tx_fifo->refcnt++;
Florin Coras93752662023-11-20 14:46:10 -0800753 cct->segment_handle =
754 ct_client_seg_handle (sct->segment_handle, cct->client_wrk);
Florin Coras6973c732021-06-17 15:53:38 -0700755
Steven Luongd810a6e2022-10-25 13:09:11 -0700756 session_set_state (ss, SESSION_STATE_ACCEPTING);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800757 if (app_worker_accept_notify (server_wrk, ss))
758 {
Florin Coras6973c732021-06-17 15:53:38 -0700759 ct_session_connect_notify (ss, SESSION_E_REFUSED);
Florin Corasda78c5a2021-06-09 14:55:24 -0700760 ct_session_dealloc_fifos (sct, ss->rx_fifo, ss->tx_fifo);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800761 ct_connection_free (sct);
Florin Coras12813d52020-04-09 20:59:20 +0000762 session_free (ss);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800763 }
Florin Coras2d0e3de2020-10-23 16:31:40 -0700764}
765
Florin Corasc215e5a2021-11-15 19:01:52 -0800766static void
767ct_accept_rpc_wrk_handler (void *rpc_args)
768{
Florin Coras55686e12023-03-20 09:58:01 -0700769 u32 thread_index, n_connects, i, n_pending;
Florin Corasc215e5a2021-11-15 19:01:52 -0800770 const u32 max_connects = 32;
771 ct_worker_t *wrk;
Florin Coras55686e12023-03-20 09:58:01 -0700772 u8 need_rpc = 0;
Florin Corasc215e5a2021-11-15 19:01:52 -0800773
774 thread_index = pointer_to_uword (rpc_args);
775 wrk = ct_worker_get (thread_index);
776
Florin Coras55686e12023-03-20 09:58:01 -0700777 /* Connects could be handled without worker barrier so grab lock */
778 clib_spinlock_lock (&wrk->pending_connects_lock);
779
Florin Corasc215e5a2021-11-15 19:01:52 -0800780 n_pending = clib_fifo_elts (wrk->pending_connects);
781 n_connects = clib_min (n_pending, max_connects);
Florin Coras55686e12023-03-20 09:58:01 -0700782 vec_validate (wrk->new_connects, n_connects);
Florin Corasc215e5a2021-11-15 19:01:52 -0800783
784 for (i = 0; i < n_connects; i++)
Florin Coras55686e12023-03-20 09:58:01 -0700785 clib_fifo_sub1 (wrk->pending_connects, wrk->new_connects[i]);
Florin Corasc215e5a2021-11-15 19:01:52 -0800786
787 if (n_pending == n_connects)
788 wrk->have_connects = 0;
789 else
Florin Coras55686e12023-03-20 09:58:01 -0700790 need_rpc = 1;
791
792 clib_spinlock_unlock (&wrk->pending_connects_lock);
793
794 for (i = 0; i < n_connects; i++)
795 ct_accept_one (thread_index, wrk->new_connects[i]);
796
797 if (need_rpc)
Florin Corasc215e5a2021-11-15 19:01:52 -0800798 session_send_rpc_evt_to_thread_force (
799 thread_index, ct_accept_rpc_wrk_handler,
800 uword_to_pointer (thread_index, void *));
801}
802
Florin Coras55686e12023-03-20 09:58:01 -0700803static void
804ct_fwrk_flush_connects (void *rpc_args)
Florin Coras2d0e3de2020-10-23 16:31:40 -0700805{
Florin Coras55686e12023-03-20 09:58:01 -0700806 u32 thread_index, fwrk_index, n_workers;
Florin Coras2d0e3de2020-10-23 16:31:40 -0700807 ct_main_t *cm = &ct_main;
Florin Corasc215e5a2021-11-15 19:01:52 -0800808 ct_worker_t *wrk;
Florin Coras55686e12023-03-20 09:58:01 -0700809 u8 need_rpc;
810
Florin Coras1315d142023-04-04 11:51:37 -0700811 fwrk_index = cm->fwrk_thread;
Florin Coras55686e12023-03-20 09:58:01 -0700812 n_workers = vec_len (cm->fwrk_pending_connects);
813
814 for (thread_index = fwrk_index; thread_index < n_workers; thread_index++)
815 {
Florin Coras1315d142023-04-04 11:51:37 -0700816 if (!vec_len (cm->fwrk_pending_connects[thread_index]))
817 continue;
818
Florin Coras55686e12023-03-20 09:58:01 -0700819 wrk = ct_worker_get (thread_index);
820
821 /* Connects can be done without worker barrier, grab dst worker lock */
822 if (thread_index != fwrk_index)
823 clib_spinlock_lock (&wrk->pending_connects_lock);
824
825 clib_fifo_add (wrk->pending_connects,
826 cm->fwrk_pending_connects[thread_index],
827 vec_len (cm->fwrk_pending_connects[thread_index]));
828 if (!wrk->have_connects)
829 {
830 wrk->have_connects = 1;
831 need_rpc = 1;
832 }
833
834 if (thread_index != fwrk_index)
835 clib_spinlock_unlock (&wrk->pending_connects_lock);
836
837 vec_reset_length (cm->fwrk_pending_connects[thread_index]);
838
839 if (need_rpc)
840 session_send_rpc_evt_to_thread_force (
841 thread_index, ct_accept_rpc_wrk_handler,
842 uword_to_pointer (thread_index, void *));
843 }
844
845 cm->fwrk_have_flush = 0;
846}
847
848static void
849ct_program_connect_to_wrk (u32 ho_index)
850{
851 ct_main_t *cm = &ct_main;
852 u32 thread_index;
Florin Coras2d0e3de2020-10-23 16:31:40 -0700853
854 /* Simple round-robin policy for spreading sessions over workers. We skip
855 * thread index 0, i.e., offset the index by 1, when we have workers as it
856 * is the one dedicated to main thread. Note that n_workers does not include
857 * main thread */
858 cm->n_sessions += 1;
859 thread_index = cm->n_workers ? (cm->n_sessions % cm->n_workers) + 1 : 0;
860
Florin Coras55686e12023-03-20 09:58:01 -0700861 /* Pospone flushing of connect request to dst worker until after session
862 * layer fully initializes the half-open session. */
863 vec_add1 (cm->fwrk_pending_connects[thread_index], ho_index);
864 if (!cm->fwrk_have_flush)
865 {
866 session_send_rpc_evt_to_thread_force (
867 cm->fwrk_thread, ct_fwrk_flush_connects,
868 uword_to_pointer (thread_index, void *));
869 cm->fwrk_have_flush = 1;
870 }
871}
872
873static int
874ct_connect (app_worker_t *client_wrk, session_t *ll,
875 session_endpoint_cfg_t *sep)
876{
877 ct_connection_t *ho;
878 u32 ho_index;
879
Florin Coras374df7a2021-05-13 11:37:43 -0700880 /*
881 * Alloc and init client half-open transport
882 */
Florin Coras2d0e3de2020-10-23 16:31:40 -0700883
Florin Coras374df7a2021-05-13 11:37:43 -0700884 ho = ct_half_open_alloc ();
885 ho_index = ho->c_c_index;
886 ho->c_rmt_port = sep->port;
887 ho->c_lcl_port = 0;
888 ho->c_is_ip4 = sep->is_ip4;
889 ho->client_opaque = sep->opaque;
890 ho->client_wrk = client_wrk->wrk_index;
891 ho->peer_index = ll->session_index;
892 ho->c_proto = TRANSPORT_PROTO_NONE;
893 ho->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
Florin Corasd5a58822021-05-14 20:11:14 -0700894 clib_memcpy (&ho->c_rmt_ip, &sep->ip, sizeof (sep->ip));
Florin Coras374df7a2021-05-13 11:37:43 -0700895 ho->flags |= CT_CONN_F_CLIENT;
896 ho->c_s_index = ~0;
Florin Corasfeb67022021-11-11 09:24:34 -0800897 ho->actual_tp = sep->original_tp;
Florin Coras374df7a2021-05-13 11:37:43 -0700898
899 /*
Florin Coras55686e12023-03-20 09:58:01 -0700900 * Program connect on a worker, connected reply comes
Florin Coras374df7a2021-05-13 11:37:43 -0700901 * after server accepts the connection.
902 */
Florin Coras55686e12023-03-20 09:58:01 -0700903 ct_program_connect_to_wrk (ho_index);
Florin Coras374df7a2021-05-13 11:37:43 -0700904
905 return ho_index;
Florin Corasba7d8f52019-02-22 13:11:38 -0800906}
907
Florin Coras653e43f2019-03-04 10:56:23 -0800908static u32
Florin Coras0bce71e2022-02-10 11:57:06 -0800909ct_start_listen (u32 app_listener_index, transport_endpoint_cfg_t *tep)
Florin Corasd4295e62019-02-22 13:11:38 -0800910{
911 session_endpoint_cfg_t *sep;
912 ct_connection_t *ct;
913
914 sep = (session_endpoint_cfg_t *) tep;
Florin Coras2d0e3de2020-10-23 16:31:40 -0700915 ct = ct_connection_alloc (0);
Florin Corasd4295e62019-02-22 13:11:38 -0800916 ct->server_wrk = sep->app_wrk_index;
917 ct->c_is_ip4 = sep->is_ip4;
918 clib_memcpy (&ct->c_lcl_ip, &sep->ip, sizeof (sep->ip));
919 ct->c_lcl_port = sep->port;
Florin Corasa8375082020-10-25 19:06:57 -0700920 ct->c_s_index = app_listener_index;
Florin Corasd4295e62019-02-22 13:11:38 -0800921 ct->actual_tp = sep->transport_proto;
922 return ct->c_c_index;
923}
924
Florin Coras653e43f2019-03-04 10:56:23 -0800925static u32
Florin Corasd4295e62019-02-22 13:11:38 -0800926ct_stop_listen (u32 ct_index)
927{
928 ct_connection_t *ct;
Florin Coras2d0e3de2020-10-23 16:31:40 -0700929 ct = ct_connection_get (ct_index, 0);
Florin Corasd4295e62019-02-22 13:11:38 -0800930 ct_connection_free (ct);
931 return 0;
932}
933
Florin Coras653e43f2019-03-04 10:56:23 -0800934static transport_connection_t *
Florin Corasd4295e62019-02-22 13:11:38 -0800935ct_listener_get (u32 ct_index)
936{
Florin Coras2d0e3de2020-10-23 16:31:40 -0700937 return (transport_connection_t *) ct_connection_get (ct_index, 0);
Florin Corasd4295e62019-02-22 13:11:38 -0800938}
939
Florin Coras374df7a2021-05-13 11:37:43 -0700940static transport_connection_t *
Florin Coras55686e12023-03-20 09:58:01 -0700941ct_session_half_open_get (u32 ct_index)
Florin Coras374df7a2021-05-13 11:37:43 -0700942{
Florin Coras55686e12023-03-20 09:58:01 -0700943 return (transport_connection_t *) ct_half_open_get (ct_index);
Florin Coras374df7a2021-05-13 11:37:43 -0700944}
945
946static void
947ct_session_cleanup (u32 conn_index, u32 thread_index)
948{
949 ct_connection_t *ct, *peer_ct;
950
951 ct = ct_connection_get (conn_index, thread_index);
952 if (!ct)
953 return;
954
955 peer_ct = ct_connection_get (ct->peer_index, thread_index);
956 if (peer_ct)
957 peer_ct->peer_index = ~0;
958
959 ct_connection_free (ct);
960}
961
962static void
963ct_cleanup_ho (u32 ho_index)
964{
Florin Coras55686e12023-03-20 09:58:01 -0700965 ct_connection_t *ho;
966
967 ho = ct_half_open_get (ho_index);
968 ct_connection_free (ho);
Florin Coras374df7a2021-05-13 11:37:43 -0700969}
970
Florin Coras653e43f2019-03-04 10:56:23 -0800971static int
Florin Coras2b81e3c2019-02-27 07:55:46 -0800972ct_session_connect (transport_endpoint_cfg_t * tep)
973{
974 session_endpoint_cfg_t *sep_ext;
Florin Coras374df7a2021-05-13 11:37:43 -0700975 session_endpoint_t _sep, *sep = &_sep;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800976 app_worker_t *app_wrk;
977 session_handle_t lh;
978 application_t *app;
979 app_listener_t *al;
980 u32 table_index;
981 session_t *ll;
982 u8 fib_proto;
983
984 sep_ext = (session_endpoint_cfg_t *) tep;
Florin Coras374df7a2021-05-13 11:37:43 -0700985 _sep = *(session_endpoint_t *) tep;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800986 app_wrk = app_worker_get (sep_ext->app_wrk_index);
987 app = application_get (app_wrk->app_index);
988
989 sep->transport_proto = sep_ext->original_tp;
990 table_index = application_local_session_table (app);
991 lh = session_lookup_local_endpoint (table_index, sep);
992 if (lh == SESSION_DROP_HANDLE)
Florin Coras00e01d32019-10-21 16:07:46 -0700993 return SESSION_E_FILTERED;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800994
995 if (lh == SESSION_INVALID_HANDLE)
996 goto global_scope;
997
998 ll = listen_session_get_from_handle (lh);
Florin Coras97fef282023-12-21 19:41:12 -0800999 al = app_listener_get (ll->al_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -08001000
1001 /*
1002 * Break loop if rule in local table points to connecting app. This
1003 * can happen if client is a generic proxy. Route connect through
1004 * global table instead.
1005 */
1006 if (al->app_index == app->app_index)
1007 goto global_scope;
1008
1009 return ct_connect (app_wrk, ll, sep_ext);
1010
1011 /*
1012 * If nothing found, check the global scope for locally attached
1013 * destinations. Make sure first that we're allowed to.
1014 */
1015
1016global_scope:
1017 if (session_endpoint_is_local (sep))
Florin Coras00e01d32019-10-21 16:07:46 -07001018 return SESSION_E_NOROUTE;
Florin Coras2b81e3c2019-02-27 07:55:46 -08001019
1020 if (!application_has_global_scope (app))
Florin Coras00e01d32019-10-21 16:07:46 -07001021 return SESSION_E_SCOPE;
Florin Coras2b81e3c2019-02-27 07:55:46 -08001022
1023 fib_proto = session_endpoint_fib_proto (sep);
Florin Coras95cd8642019-12-30 21:53:19 -08001024 table_index = session_lookup_get_index_for_fib (fib_proto, sep->fib_index);
Florin Coras9f1a5432019-03-10 21:03:20 -07001025 ll = session_lookup_listener_wildcard (table_index, sep);
Florin Coras2b81e3c2019-02-27 07:55:46 -08001026
Florin Coras821b5002021-06-02 21:32:39 -07001027 /* Avoid connecting app to own listener */
Florin Coras97fef282023-12-21 19:41:12 -08001028 if (ll)
1029 {
1030 al = app_listener_get (ll->al_index);
1031 if (al->app_index != app->app_index)
1032 return ct_connect (app_wrk, ll, sep_ext);
1033 }
Florin Coras2b81e3c2019-02-27 07:55:46 -08001034
1035 /* Failed to connect but no error */
Florin Coras374df7a2021-05-13 11:37:43 -07001036 return SESSION_E_LOCAL_CONNECT;
Florin Coras2b81e3c2019-02-27 07:55:46 -08001037}
1038
Florin Coras9439a362021-11-25 16:18:39 -08001039static inline int
1040ct_close_is_reset (ct_connection_t *ct, session_t *s)
1041{
Florin Coras6d14c0c2023-12-14 11:46:11 -08001042 if (ct->flags & CT_CONN_F_RESET)
1043 return 1;
Florin Coras9439a362021-11-25 16:18:39 -08001044 if (ct->flags & CT_CONN_F_CLIENT)
1045 return (svm_fifo_max_dequeue (ct->client_rx_fifo) > 0);
1046 else
1047 return (svm_fifo_max_dequeue (s->rx_fifo) > 0);
1048}
1049
Florin Coras653e43f2019-03-04 10:56:23 -08001050static void
Florin Coras0242d302022-12-22 15:03:44 -08001051ct_session_cleanup_server_session (session_t *s)
1052{
1053 ct_connection_t *ct;
1054
1055 ct = (ct_connection_t *) session_get_transport (s);
1056 ct_session_dealloc_fifos (ct, s->rx_fifo, s->tx_fifo);
1057 session_free (s);
1058 ct_connection_free (ct);
1059}
1060
1061static void
Florin Coras440c7b52021-11-09 08:38:24 -08001062ct_session_postponed_cleanup (ct_connection_t *ct)
1063{
Florin Coras9439a362021-11-25 16:18:39 -08001064 ct_connection_t *peer_ct;
Florin Coras440c7b52021-11-09 08:38:24 -08001065 app_worker_t *app_wrk;
1066 session_t *s;
1067
1068 s = session_get (ct->c_s_index, ct->c_thread_index);
1069 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
1070
Florin Coras9439a362021-11-25 16:18:39 -08001071 peer_ct = ct_connection_get (ct->peer_index, ct->c_thread_index);
1072 if (peer_ct)
1073 {
1074 if (ct_close_is_reset (ct, s))
1075 session_transport_reset_notify (&peer_ct->connection);
1076 else
1077 session_transport_closing_notify (&peer_ct->connection);
1078 }
1079 session_transport_closed_notify (&ct->connection);
1080
Florin Coras0242d302022-12-22 15:03:44 -08001081 /* It would be cleaner to call session_transport_delete_notify
1082 * but then we can't control session cleanup lower */
1083 session_set_state (s, SESSION_STATE_TRANSPORT_DELETED);
1084 if (app_wrk)
1085 app_worker_cleanup_notify (app_wrk, s, SESSION_CLEANUP_TRANSPORT);
1086
Florin Coras440c7b52021-11-09 08:38:24 -08001087 if (ct->flags & CT_CONN_F_CLIENT)
1088 {
Florin Coras440c7b52021-11-09 08:38:24 -08001089 /* Normal free for client session as the fifos are allocated through
1090 * the connects segment manager in a segment that's not shared with
1091 * the server */
Florin Coras440c7b52021-11-09 08:38:24 -08001092 ct_session_dealloc_fifos (ct, ct->client_rx_fifo, ct->client_tx_fifo);
Florin Coras0242d302022-12-22 15:03:44 -08001093 session_program_cleanup (s);
1094 ct_connection_free (ct);
Florin Coras440c7b52021-11-09 08:38:24 -08001095 }
1096 else
1097 {
1098 /* Manual session and fifo segment cleanup to avoid implicit
1099 * segment manager cleanups and notifications */
Florin Coras440c7b52021-11-09 08:38:24 -08001100 if (app_wrk)
1101 {
Florin Coras0242d302022-12-22 15:03:44 -08001102 /* Remove custom cleanup notify infra when/if switching to normal
1103 * session cleanup. Note that ct is freed in the cb function */
1104 app_worker_cleanup_notify_custom (app_wrk, s,
1105 SESSION_CLEANUP_SESSION,
1106 ct_session_cleanup_server_session);
Florin Coras440c7b52021-11-09 08:38:24 -08001107 }
Florin Coras0242d302022-12-22 15:03:44 -08001108 else
1109 {
1110 ct_connection_free (ct);
1111 }
Florin Coras440c7b52021-11-09 08:38:24 -08001112 }
Florin Coras440c7b52021-11-09 08:38:24 -08001113}
1114
1115static void
1116ct_handle_cleanups (void *args)
1117{
1118 uword thread_index = pointer_to_uword (args);
1119 const u32 max_cleanups = 100;
Florin Coras440c7b52021-11-09 08:38:24 -08001120 ct_cleanup_req_t *req;
1121 ct_connection_t *ct;
1122 u32 n_to_handle = 0;
Florin Coras7fe00692021-11-15 14:01:02 -08001123 ct_worker_t *wrk;
Florin Coras440c7b52021-11-09 08:38:24 -08001124 session_t *s;
1125
Florin Coras7fe00692021-11-15 14:01:02 -08001126 wrk = ct_worker_get (thread_index);
1127 wrk->have_cleanups = 0;
1128 n_to_handle = clib_fifo_elts (wrk->pending_cleanups);
Florin Coras440c7b52021-11-09 08:38:24 -08001129 n_to_handle = clib_min (n_to_handle, max_cleanups);
1130
1131 while (n_to_handle)
1132 {
Florin Coras7fe00692021-11-15 14:01:02 -08001133 clib_fifo_sub2 (wrk->pending_cleanups, req);
Florin Coras440c7b52021-11-09 08:38:24 -08001134 ct = ct_connection_get (req->ct_index, thread_index);
1135 s = session_get (ct->c_s_index, ct->c_thread_index);
Florin Corase439b1e2024-02-14 16:14:46 -08001136 if (svm_fifo_has_event (s->tx_fifo) || (s->flags & SESSION_F_RX_EVT))
Florin Coras7fe00692021-11-15 14:01:02 -08001137 clib_fifo_add1 (wrk->pending_cleanups, *req);
Florin Corase439b1e2024-02-14 16:14:46 -08001138 else
1139 ct_session_postponed_cleanup (ct);
Florin Coras440c7b52021-11-09 08:38:24 -08001140 n_to_handle -= 1;
1141 }
1142
Florin Coras7fe00692021-11-15 14:01:02 -08001143 if (clib_fifo_elts (wrk->pending_cleanups))
Florin Coras440c7b52021-11-09 08:38:24 -08001144 {
Florin Coras7fe00692021-11-15 14:01:02 -08001145 wrk->have_cleanups = 1;
Florin Coras440c7b52021-11-09 08:38:24 -08001146 session_send_rpc_evt_to_thread_force (
1147 thread_index, ct_handle_cleanups,
1148 uword_to_pointer (thread_index, void *));
1149 }
1150}
1151
1152static void
1153ct_program_cleanup (ct_connection_t *ct)
1154{
Florin Coras440c7b52021-11-09 08:38:24 -08001155 ct_cleanup_req_t *req;
1156 uword thread_index;
Florin Coras7fe00692021-11-15 14:01:02 -08001157 ct_worker_t *wrk;
Florin Coras440c7b52021-11-09 08:38:24 -08001158
1159 thread_index = ct->c_thread_index;
Florin Coras7fe00692021-11-15 14:01:02 -08001160 wrk = ct_worker_get (ct->c_thread_index);
1161
1162 clib_fifo_add2 (wrk->pending_cleanups, req);
Florin Coras440c7b52021-11-09 08:38:24 -08001163 req->ct_index = ct->c_c_index;
1164
Florin Coras7fe00692021-11-15 14:01:02 -08001165 if (wrk->have_cleanups)
Florin Coras440c7b52021-11-09 08:38:24 -08001166 return;
1167
Florin Coras7fe00692021-11-15 14:01:02 -08001168 wrk->have_cleanups = 1;
Florin Coras440c7b52021-11-09 08:38:24 -08001169 session_send_rpc_evt_to_thread_force (
1170 thread_index, ct_handle_cleanups, uword_to_pointer (thread_index, void *));
1171}
1172
1173static void
Florin Coras2b81e3c2019-02-27 07:55:46 -08001174ct_session_close (u32 ct_index, u32 thread_index)
1175{
1176 ct_connection_t *ct, *peer_ct;
1177 session_t *s;
1178
Florin Coras2d0e3de2020-10-23 16:31:40 -07001179 ct = ct_connection_get (ct_index, thread_index);
Florin Coras6973c732021-06-17 15:53:38 -07001180 s = session_get (ct->c_s_index, ct->c_thread_index);
Florin Coras2d0e3de2020-10-23 16:31:40 -07001181 peer_ct = ct_connection_get (ct->peer_index, thread_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -08001182 if (peer_ct)
1183 {
1184 peer_ct->peer_index = ~0;
Florin Coras06ac4bb2020-10-28 16:41:26 -07001185 /* Make sure session was allocated */
Florin Coras374df7a2021-05-13 11:37:43 -07001186 if (peer_ct->flags & CT_CONN_F_HALF_OPEN)
1187 {
Florin Coras6973c732021-06-17 15:53:38 -07001188 ct_session_connect_notify (s, SESSION_E_REFUSED);
Florin Corasa7bd8262021-11-25 12:14:25 -08001189 ct->peer_index = ~0;
Florin Coras374df7a2021-05-13 11:37:43 -07001190 }
Florin Coras9439a362021-11-25 16:18:39 -08001191 else if (peer_ct->c_s_index == ~0)
Florin Coras440c7b52021-11-09 08:38:24 -08001192 {
1193 /* should not happen */
1194 clib_warning ("ct peer without session");
1195 ct_connection_free (peer_ct);
1196 }
Florin Coras2b81e3c2019-02-27 07:55:46 -08001197 }
1198
Florin Coras440c7b52021-11-09 08:38:24 -08001199 /* Do not send closed notify to make sure pending tx events are
1200 * still delivered and program cleanup */
1201 ct_program_cleanup (ct);
Florin Coras2b81e3c2019-02-27 07:55:46 -08001202}
1203
Florin Coras6d14c0c2023-12-14 11:46:11 -08001204static void
1205ct_session_reset (u32 ct_index, u32 thread_index)
1206{
1207 ct_connection_t *ct;
1208 ct = ct_connection_get (ct_index, thread_index);
1209 ct->flags |= CT_CONN_F_RESET;
1210 ct_session_close (ct_index, thread_index);
1211}
1212
Florin Coras653e43f2019-03-04 10:56:23 -08001213static transport_connection_t *
Florin Coras2b81e3c2019-02-27 07:55:46 -08001214ct_session_get (u32 ct_index, u32 thread_index)
1215{
Florin Coras2d0e3de2020-10-23 16:31:40 -07001216 return (transport_connection_t *) ct_connection_get (ct_index,
1217 thread_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -08001218}
1219
Florin Corasd4295e62019-02-22 13:11:38 -08001220static u8 *
1221format_ct_connection_id (u8 * s, va_list * args)
1222{
1223 ct_connection_t *ct = va_arg (*args, ct_connection_t *);
1224 if (!ct)
1225 return s;
1226 if (ct->c_is_ip4)
1227 {
1228 s = format (s, "[%d:%d][CT:%U] %U:%d->%U:%d", ct->c_thread_index,
1229 ct->c_s_index, format_transport_proto_short, ct->actual_tp,
1230 format_ip4_address, &ct->c_lcl_ip4,
1231 clib_net_to_host_u16 (ct->c_lcl_port), format_ip4_address,
1232 &ct->c_rmt_ip4, clib_net_to_host_u16 (ct->c_rmt_port));
1233 }
1234 else
1235 {
1236 s = format (s, "[%d:%d][CT:%U] %U:%d->%U:%d", ct->c_thread_index,
1237 ct->c_s_index, format_transport_proto_short, ct->actual_tp,
1238 format_ip6_address, &ct->c_lcl_ip6,
1239 clib_net_to_host_u16 (ct->c_lcl_port), format_ip6_address,
1240 &ct->c_rmt_ip6, clib_net_to_host_u16 (ct->c_rmt_port));
1241 }
1242
1243 return s;
1244}
1245
Florin Corasf940f8a2019-03-06 10:44:38 -08001246static int
Florin Coras9f86d222020-03-23 15:34:22 +00001247ct_custom_tx (void *session, transport_send_params_t * sp)
Florin Corasf940f8a2019-03-06 10:44:38 -08001248{
1249 session_t *s = (session_t *) session;
Florin Coras074f3972021-12-07 15:12:06 -08001250 if (session_has_transport (s))
Florin Corasf940f8a2019-03-06 10:44:38 -08001251 return 0;
Florin Coras1b9d2c22021-01-26 14:10:43 -08001252 /* If event enqueued towards peer, remove from scheduler and remove
1253 * session tx flag, i.e., accept new tx events. Unset fifo flag now to
1254 * avoid missing events if peer did not clear fifo flag yet, which is
1255 * interpreted as successful notification and session is descheduled. */
1256 svm_fifo_unset_event (s->tx_fifo);
Florin Corasd6894562020-10-28 00:37:15 -07001257 if (!ct_session_tx (s))
Florin Coras1b9d2c22021-01-26 14:10:43 -08001258 sp->flags = TRANSPORT_SND_F_DESCHED;
1259
Florin Corasd6894562020-10-28 00:37:15 -07001260 /* The scheduler uses packet count as a means of upper bounding the amount
1261 * of work done per dispatch. So make it look like we have sent something */
1262 return 1;
Florin Corasf940f8a2019-03-06 10:44:38 -08001263}
1264
Florin Coras317b8e02019-04-17 09:57:46 -07001265static int
1266ct_app_rx_evt (transport_connection_t * tc)
1267{
1268 ct_connection_t *ct = (ct_connection_t *) tc, *peer_ct;
Florin Coras9439a362021-11-25 16:18:39 -08001269 session_t *ps, *s;
Florin Coras317b8e02019-04-17 09:57:46 -07001270
Florin Coras9439a362021-11-25 16:18:39 -08001271 s = session_get (ct->c_s_index, ct->c_thread_index);
1272 if (session_has_transport (s) || s->session_state < SESSION_STATE_READY)
1273 return -1;
Florin Coras2d0e3de2020-10-23 16:31:40 -07001274 peer_ct = ct_connection_get (ct->peer_index, tc->thread_index);
Florin Coras9439a362021-11-25 16:18:39 -08001275 if (!peer_ct || (peer_ct->flags & CT_CONN_F_HALF_OPEN))
Florin Coras317b8e02019-04-17 09:57:46 -07001276 return -1;
1277 ps = session_get (peer_ct->c_s_index, peer_ct->c_thread_index);
Florin Coras9439a362021-11-25 16:18:39 -08001278 if (ps->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
1279 return -1;
Florin Coras317b8e02019-04-17 09:57:46 -07001280 return session_dequeue_notify (ps);
1281}
1282
Florin Coras653e43f2019-03-04 10:56:23 -08001283static u8 *
Florin Corasd4295e62019-02-22 13:11:38 -08001284format_ct_listener (u8 * s, va_list * args)
1285{
1286 u32 tc_index = va_arg (*args, u32);
Aloys Augustina0abbff2019-07-12 12:16:16 +02001287 u32 __clib_unused thread_index = va_arg (*args, u32);
Florin Corasd4295e62019-02-22 13:11:38 -08001288 u32 __clib_unused verbose = va_arg (*args, u32);
Florin Coras2d0e3de2020-10-23 16:31:40 -07001289 ct_connection_t *ct = ct_connection_get (tc_index, 0);
Florin Coras7bf6ed62020-09-23 12:02:08 -07001290 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_ct_connection_id, ct);
Florin Corasd4295e62019-02-22 13:11:38 -08001291 if (verbose)
Florin Coras7bf6ed62020-09-23 12:02:08 -07001292 s = format (s, "%-" SESSION_CLI_STATE_LEN "s", "LISTEN");
Florin Corasd4295e62019-02-22 13:11:38 -08001293 return s;
1294}
1295
Florin Coras653e43f2019-03-04 10:56:23 -08001296static u8 *
Florin Coras374df7a2021-05-13 11:37:43 -07001297format_ct_half_open (u8 *s, va_list *args)
1298{
1299 u32 ho_index = va_arg (*args, u32);
1300 u32 verbose = va_arg (*args, u32);
Florin Coras55686e12023-03-20 09:58:01 -07001301 ct_connection_t *ct = ct_half_open_get (ho_index);
Florin Coras374df7a2021-05-13 11:37:43 -07001302 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_ct_connection_id, ct);
1303 if (verbose)
1304 s = format (s, "%-" SESSION_CLI_STATE_LEN "s", "HALF-OPEN");
1305 return s;
1306}
1307
1308static u8 *
Florin Coras2b81e3c2019-02-27 07:55:46 -08001309format_ct_connection (u8 * s, va_list * args)
1310{
1311 ct_connection_t *ct = va_arg (*args, ct_connection_t *);
1312 u32 verbose = va_arg (*args, u32);
1313
1314 if (!ct)
1315 return s;
Florin Coras7bf6ed62020-09-23 12:02:08 -07001316 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_ct_connection_id, ct);
Florin Coras2b81e3c2019-02-27 07:55:46 -08001317 if (verbose)
1318 {
Florin Coras7bf6ed62020-09-23 12:02:08 -07001319 s = format (s, "%-" SESSION_CLI_STATE_LEN "s", "ESTABLISHED");
Florin Coras2b81e3c2019-02-27 07:55:46 -08001320 if (verbose > 1)
1321 {
1322 s = format (s, "\n");
1323 }
1324 }
1325 return s;
1326}
1327
Florin Coras653e43f2019-03-04 10:56:23 -08001328static u8 *
Florin Coras2b81e3c2019-02-27 07:55:46 -08001329format_ct_session (u8 * s, va_list * args)
1330{
1331 u32 ct_index = va_arg (*args, u32);
Florin Coras2d0e3de2020-10-23 16:31:40 -07001332 u32 thread_index = va_arg (*args, u32);
Florin Coras2b81e3c2019-02-27 07:55:46 -08001333 u32 verbose = va_arg (*args, u32);
1334 ct_connection_t *ct;
1335
Florin Coras2d0e3de2020-10-23 16:31:40 -07001336 ct = ct_connection_get (ct_index, thread_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -08001337 if (!ct)
1338 {
1339 s = format (s, "empty\n");
1340 return s;
1341 }
1342
1343 s = format (s, "%U", format_ct_connection, ct, verbose);
1344 return s;
1345}
1346
Florin Coras2d0e3de2020-10-23 16:31:40 -07001347clib_error_t *
1348ct_enable_disable (vlib_main_t * vm, u8 is_en)
1349{
Florin Coras7fe00692021-11-15 14:01:02 -08001350 vlib_thread_main_t *vtm = &vlib_thread_main;
Florin Coras374df7a2021-05-13 11:37:43 -07001351 ct_main_t *cm = &ct_main;
Florin Coras55686e12023-03-20 09:58:01 -07001352 ct_worker_t *wrk;
Florin Coras374df7a2021-05-13 11:37:43 -07001353
Steven Luongd2d41bc2024-06-28 15:12:11 -07001354 if (is_en == 0)
1355 return 0;
1356
Florin Coras75e8e1e2024-07-02 04:34:54 -07001357 if (cm->is_init)
1358 return 0;
1359
Florin Coras374df7a2021-05-13 11:37:43 -07001360 cm->n_workers = vlib_num_workers ();
Florin Coras55686e12023-03-20 09:58:01 -07001361 cm->fwrk_thread = transport_cl_thread ();
Florin Coras7fe00692021-11-15 14:01:02 -08001362 vec_validate (cm->wrk, vtm->n_vlib_mains);
Florin Coras55686e12023-03-20 09:58:01 -07001363 vec_validate (cm->fwrk_pending_connects, cm->n_workers);
Florin Coras75e8e1e2024-07-02 04:34:54 -07001364 vec_foreach (wrk, cm->wrk)
1365 clib_spinlock_init (&wrk->pending_connects_lock);
1366 clib_spinlock_init (&cm->ho_reuseable_lock);
1367 clib_rwlock_init (&cm->app_segs_lock);
1368 cm->is_init = 1;
1369
Florin Coras2d0e3de2020-10-23 16:31:40 -07001370 return 0;
1371}
1372
Nathan Skrzypczake971bc92019-06-19 13:42:37 +02001373static const transport_proto_vft_t cut_thru_proto = {
Florin Coras2d0e3de2020-10-23 16:31:40 -07001374 .enable = ct_enable_disable,
Florin Corasd4295e62019-02-22 13:11:38 -08001375 .start_listen = ct_start_listen,
1376 .stop_listen = ct_stop_listen,
Florin Coras374df7a2021-05-13 11:37:43 -07001377 .get_connection = ct_session_get,
Florin Corasd4295e62019-02-22 13:11:38 -08001378 .get_listener = ct_listener_get,
Florin Coras55686e12023-03-20 09:58:01 -07001379 .get_half_open = ct_session_half_open_get,
Florin Coras374df7a2021-05-13 11:37:43 -07001380 .cleanup = ct_session_cleanup,
1381 .cleanup_ho = ct_cleanup_ho,
Florin Coras2b81e3c2019-02-27 07:55:46 -08001382 .connect = ct_session_connect,
1383 .close = ct_session_close,
Florin Coras6d14c0c2023-12-14 11:46:11 -08001384 .reset = ct_session_reset,
Florin Corasf940f8a2019-03-06 10:44:38 -08001385 .custom_tx = ct_custom_tx,
Florin Coras317b8e02019-04-17 09:57:46 -07001386 .app_rx_evt = ct_app_rx_evt,
Florin Corasd4295e62019-02-22 13:11:38 -08001387 .format_listener = format_ct_listener,
Florin Coras374df7a2021-05-13 11:37:43 -07001388 .format_half_open = format_ct_half_open,
Florin Coras2b81e3c2019-02-27 07:55:46 -08001389 .format_connection = format_ct_session,
Nathan Skrzypczake971bc92019-06-19 13:42:37 +02001390 .transport_options = {
Florin Coras07063b82020-03-13 04:44:51 +00001391 .name = "ct",
1392 .short_name = "C",
Nathan Skrzypczake971bc92019-06-19 13:42:37 +02001393 .tx_type = TRANSPORT_TX_INTERNAL,
Florin Coras374df7a2021-05-13 11:37:43 -07001394 .service_type = TRANSPORT_SERVICE_VC,
Nathan Skrzypczake971bc92019-06-19 13:42:37 +02001395 },
Florin Corasd4295e62019-02-22 13:11:38 -08001396};
Florin Corasd4295e62019-02-22 13:11:38 -08001397
Florin Coras074f3972021-12-07 15:12:06 -08001398static inline int
1399ct_session_can_tx (session_t *s)
1400{
1401 return (s->session_state == SESSION_STATE_READY ||
1402 s->session_state == SESSION_STATE_CLOSING ||
1403 s->session_state == SESSION_STATE_APP_CLOSED);
1404}
1405
Florin Coras653e43f2019-03-04 10:56:23 -08001406int
1407ct_session_tx (session_t * s)
1408{
1409 ct_connection_t *ct, *peer_ct;
1410 session_t *peer_s;
1411
Florin Coras074f3972021-12-07 15:12:06 -08001412 if (!ct_session_can_tx (s))
1413 return 0;
Florin Coras653e43f2019-03-04 10:56:23 -08001414 ct = (ct_connection_t *) session_get_transport (s);
Florin Coras2d0e3de2020-10-23 16:31:40 -07001415 peer_ct = ct_connection_get (ct->peer_index, ct->c_thread_index);
Florin Coras653e43f2019-03-04 10:56:23 -08001416 if (!peer_ct)
Florin Corasbcdc50d2020-08-25 19:07:02 -07001417 return 0;
Florin Coras2d0e3de2020-10-23 16:31:40 -07001418 peer_s = session_get (peer_ct->c_s_index, peer_ct->c_thread_index);
Florin Coras653e43f2019-03-04 10:56:23 -08001419 if (peer_s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
1420 return 0;
Florin Corase439b1e2024-02-14 16:14:46 -08001421 peer_s->flags |= SESSION_F_RX_EVT;
Florin Corasd6894562020-10-28 00:37:15 -07001422 return session_enqueue_notify (peer_s);
Florin Coras653e43f2019-03-04 10:56:23 -08001423}
1424
Florin Corasd4295e62019-02-22 13:11:38 -08001425static clib_error_t *
1426ct_transport_init (vlib_main_t * vm)
1427{
1428 transport_register_protocol (TRANSPORT_PROTO_NONE, &cut_thru_proto,
1429 FIB_PROTOCOL_IP4, ~0);
Florin Coras653e43f2019-03-04 10:56:23 -08001430 transport_register_protocol (TRANSPORT_PROTO_NONE, &cut_thru_proto,
1431 FIB_PROTOCOL_IP6, ~0);
Florin Corasd4295e62019-02-22 13:11:38 -08001432 return 0;
1433}
1434
1435VLIB_INIT_FUNCTION (ct_transport_init);
1436
Florin Corasba7d8f52019-02-22 13:11:38 -08001437/*
1438 * fd.io coding-style-patch-verification: ON
1439 *
1440 * Local Variables:
1441 * eval: (c-set-style "gnu")
1442 * End:
1443 */