blob: 3cb743d10e0004c4d94b773d7b905756e2393e33 [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 Coras2d0e3de2020-10-23 16:31:40 -070073} ct_main_t;
Florin Coras653e43f2019-03-04 10:56:23 -080074
Florin Coras2d0e3de2020-10-23 16:31:40 -070075static ct_main_t ct_main;
Florin Coras653e43f2019-03-04 10:56:23 -080076
Florin Coras7fe00692021-11-15 14:01:02 -080077static inline ct_worker_t *
78ct_worker_get (u32 thread_index)
79{
80 return &ct_main.wrk[thread_index];
81}
82
Florin Coras653e43f2019-03-04 10:56:23 -080083static ct_connection_t *
Florin Coras2d0e3de2020-10-23 16:31:40 -070084ct_connection_alloc (u32 thread_index)
Florin Corasd4295e62019-02-22 13:11:38 -080085{
Florin Coras7fe00692021-11-15 14:01:02 -080086 ct_worker_t *wrk = ct_worker_get (thread_index);
Florin Corasd4295e62019-02-22 13:11:38 -080087 ct_connection_t *ct;
88
Florin Coras9688b3b2022-04-07 12:58:13 -070089 pool_get_aligned_safe (wrk->connections, ct, CLIB_CACHE_LINE_BYTES);
90 clib_memset (ct, 0, sizeof (*ct));
Florin Coras7fe00692021-11-15 14:01:02 -080091 ct->c_c_index = ct - wrk->connections;
Florin Coras2d0e3de2020-10-23 16:31:40 -070092 ct->c_thread_index = thread_index;
Florin Corasd4295e62019-02-22 13:11:38 -080093 ct->client_wrk = ~0;
94 ct->server_wrk = ~0;
Florin Corasda78c5a2021-06-09 14:55:24 -070095 ct->seg_ctx_index = ~0;
96 ct->ct_seg_index = ~0;
Florin Corasd4295e62019-02-22 13:11:38 -080097 return ct;
98}
99
Florin Coras653e43f2019-03-04 10:56:23 -0800100static ct_connection_t *
Florin Coras2d0e3de2020-10-23 16:31:40 -0700101ct_connection_get (u32 ct_index, u32 thread_index)
Florin Corasd4295e62019-02-22 13:11:38 -0800102{
Florin Coras7fe00692021-11-15 14:01:02 -0800103 ct_worker_t *wrk = ct_worker_get (thread_index);
104
105 if (pool_is_free_index (wrk->connections, ct_index))
Florin Corasd4295e62019-02-22 13:11:38 -0800106 return 0;
Florin Coras7fe00692021-11-15 14:01:02 -0800107 return pool_elt_at_index (wrk->connections, ct_index);
Florin Corasba7d8f52019-02-22 13:11:38 -0800108}
109
Florin Coras653e43f2019-03-04 10:56:23 -0800110static void
Florin Corasd4295e62019-02-22 13:11:38 -0800111ct_connection_free (ct_connection_t * ct)
Florin Corasba7d8f52019-02-22 13:11:38 -0800112{
Florin Coras7fe00692021-11-15 14:01:02 -0800113 ct_worker_t *wrk = ct_worker_get (ct->c_thread_index);
114
Florin Corasba7d8f52019-02-22 13:11:38 -0800115 if (CLIB_DEBUG)
Florin Coras2d0e3de2020-10-23 16:31:40 -0700116 {
Florin Coras7fe00692021-11-15 14:01:02 -0800117 clib_memset (ct, 0xfc, sizeof (*ct));
118 pool_put (wrk->connections, ct);
Florin Coras2d0e3de2020-10-23 16:31:40 -0700119 return;
120 }
Florin Coras7fe00692021-11-15 14:01:02 -0800121 pool_put (wrk->connections, ct);
Florin Corasba7d8f52019-02-22 13:11:38 -0800122}
123
Florin Coras374df7a2021-05-13 11:37:43 -0700124static ct_connection_t *
125ct_half_open_alloc (void)
126{
127 ct_main_t *cm = &ct_main;
128 u32 *hip;
129
130 clib_spinlock_lock (&cm->ho_reuseable_lock);
131 vec_foreach (hip, cm->ho_reusable)
Florin Coras55686e12023-03-20 09:58:01 -0700132 pool_put_index (cm->wrk[cm->fwrk_thread].connections, *hip);
Florin Coras374df7a2021-05-13 11:37:43 -0700133 vec_reset_length (cm->ho_reusable);
134 clib_spinlock_unlock (&cm->ho_reuseable_lock);
135
Florin Coras55686e12023-03-20 09:58:01 -0700136 return ct_connection_alloc (cm->fwrk_thread);
137}
138
139static ct_connection_t *
140ct_half_open_get (u32 ho_index)
141{
142 ct_main_t *cm = &ct_main;
143 return ct_connection_get (ho_index, cm->fwrk_thread);
Florin Coras374df7a2021-05-13 11:37:43 -0700144}
145
146void
147ct_half_open_add_reusable (u32 ho_index)
148{
149 ct_main_t *cm = &ct_main;
150
151 clib_spinlock_lock (&cm->ho_reuseable_lock);
152 vec_add1 (cm->ho_reusable, ho_index);
153 clib_spinlock_unlock (&cm->ho_reuseable_lock);
154}
155
Florin Coras2b81e3c2019-02-27 07:55:46 -0800156session_t *
157ct_session_get_peer (session_t * s)
158{
159 ct_connection_t *ct, *peer_ct;
Florin Coras2d0e3de2020-10-23 16:31:40 -0700160 ct = ct_connection_get (s->connection_index, s->thread_index);
161 peer_ct = ct_connection_get (ct->peer_index, s->thread_index);
162 return session_get (peer_ct->c_s_index, s->thread_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800163}
164
Florin Corasba7d8f52019-02-22 13:11:38 -0800165void
Florin Coras2b81e3c2019-02-27 07:55:46 -0800166ct_session_endpoint (session_t * ll, session_endpoint_t * sep)
Florin Corasba7d8f52019-02-22 13:11:38 -0800167{
Florin Corasd4295e62019-02-22 13:11:38 -0800168 ct_connection_t *ct;
169 ct = (ct_connection_t *) session_get_transport (ll);
170 sep->transport_proto = ct->actual_tp;
171 sep->port = ct->c_lcl_port;
172 sep->is_ip4 = ct->c_is_ip4;
Florin Corasea7e7082020-07-07 17:57:28 -0700173 ip_copy (&sep->ip, &ct->c_lcl_ip, ct->c_is_ip4);
Florin Corasba7d8f52019-02-22 13:11:38 -0800174}
175
Florin Corasda78c5a2021-06-09 14:55:24 -0700176static void
Florin Coras98952382021-11-14 15:33:59 -0800177ct_set_invalid_app_wrk (ct_connection_t *ct, u8 is_client)
178{
179 ct_connection_t *peer_ct;
180
181 peer_ct = ct_connection_get (ct->peer_index, ct->c_thread_index);
182
183 if (is_client)
184 {
185 ct->client_wrk = APP_INVALID_INDEX;
186 if (peer_ct)
187 ct->client_wrk = APP_INVALID_INDEX;
188 }
189 else
190 {
191 ct->server_wrk = APP_INVALID_INDEX;
192 if (peer_ct)
193 ct->server_wrk = APP_INVALID_INDEX;
194 }
195}
196
Florin Coras93752662023-11-20 14:46:10 -0800197static inline u64
198ct_client_seg_handle (u64 server_sh, u32 client_wrk_index)
199{
200 return (((u64) client_wrk_index << 56) | server_sh);
201}
202
Florin Coras98952382021-11-14 15:33:59 -0800203static void
Florin Corasda78c5a2021-06-09 14:55:24 -0700204ct_session_dealloc_fifos (ct_connection_t *ct, svm_fifo_t *rx_fifo,
205 svm_fifo_t *tx_fifo)
Florin Corasba7d8f52019-02-22 13:11:38 -0800206{
Florin Corasda78c5a2021-06-09 14:55:24 -0700207 ct_segments_ctx_t *seg_ctx;
208 ct_main_t *cm = &ct_main;
Florin Corasba7d8f52019-02-22 13:11:38 -0800209 segment_manager_t *sm;
Florin Corasda78c5a2021-06-09 14:55:24 -0700210 app_worker_t *app_wrk;
211 ct_segment_t *ct_seg;
212 fifo_segment_t *fs;
213 u32 seg_index;
Florin Coras98952382021-11-14 15:33:59 -0800214 session_t *s;
Florin Coras6973c732021-06-17 15:53:38 -0700215 int cnt;
Florin Corasda78c5a2021-06-09 14:55:24 -0700216
217 /*
218 * Cleanup fifos
219 */
220
221 sm = segment_manager_get (rx_fifo->segment_manager);
222 seg_index = rx_fifo->segment_index;
223
224 fs = segment_manager_get_segment_w_lock (sm, seg_index);
225 fifo_segment_free_fifo (fs, rx_fifo);
226 fifo_segment_free_fifo (fs, tx_fifo);
227 segment_manager_segment_reader_unlock (sm);
228
229 /*
Florin Coras6973c732021-06-17 15:53:38 -0700230 * Atomically update segment context with readers lock
Florin Corasda78c5a2021-06-09 14:55:24 -0700231 */
232
233 clib_rwlock_reader_lock (&cm->app_segs_lock);
234
235 seg_ctx = pool_elt_at_index (cm->app_seg_ctxs, ct->seg_ctx_index);
236 ct_seg = pool_elt_at_index (seg_ctx->segments, ct->ct_seg_index);
237
238 if (ct->flags & CT_CONN_F_CLIENT)
239 {
240 cnt =
241 __atomic_sub_fetch (&ct_seg->client_n_sessions, 1, __ATOMIC_RELAXED);
Florin Corasda78c5a2021-06-09 14:55:24 -0700242 }
243 else
244 {
245 cnt =
246 __atomic_sub_fetch (&ct_seg->server_n_sessions, 1, __ATOMIC_RELAXED);
Florin Corasda78c5a2021-06-09 14:55:24 -0700247 }
248
Florin Corasda78c5a2021-06-09 14:55:24 -0700249 clib_rwlock_reader_unlock (&cm->app_segs_lock);
250
251 /*
252 * No need to do any app updates, return
253 */
Florin Coras6973c732021-06-17 15:53:38 -0700254 ASSERT (cnt >= 0);
255 if (cnt)
256 return;
257
258 /*
259 * Grab exclusive lock and update flags unless some other thread
260 * added more sessions
261 */
262 clib_rwlock_writer_lock (&cm->app_segs_lock);
263
264 seg_ctx = pool_elt_at_index (cm->app_seg_ctxs, ct->seg_ctx_index);
265 ct_seg = pool_elt_at_index (seg_ctx->segments, ct->ct_seg_index);
266 if (ct->flags & CT_CONN_F_CLIENT)
267 {
268 cnt = ct_seg->client_n_sessions;
Florin Coras98952382021-11-14 15:33:59 -0800269 if (cnt)
270 goto done;
271 ct_seg->flags |= CT_SEGMENT_F_CLIENT_DETACHED;
272 s = session_get (ct->c_s_index, ct->c_thread_index);
273 if (s->app_wrk_index == APP_INVALID_INDEX)
274 ct_set_invalid_app_wrk (ct, 1 /* is_client */);
Florin Coras6973c732021-06-17 15:53:38 -0700275 }
276 else
277 {
278 cnt = ct_seg->server_n_sessions;
Florin Coras98952382021-11-14 15:33:59 -0800279 if (cnt)
280 goto done;
281 ct_seg->flags |= CT_SEGMENT_F_SERVER_DETACHED;
282 s = session_get (ct->c_s_index, ct->c_thread_index);
283 if (s->app_wrk_index == APP_INVALID_INDEX)
284 ct_set_invalid_app_wrk (ct, 0 /* is_client */);
Florin Coras6973c732021-06-17 15:53:38 -0700285 }
286
Florin Coras98952382021-11-14 15:33:59 -0800287 if (!(ct_seg->flags & CT_SEGMENT_F_CLIENT_DETACHED) ||
288 !(ct_seg->flags & CT_SEGMENT_F_SERVER_DETACHED))
289 goto done;
290
Florin Coras6973c732021-06-17 15:53:38 -0700291 /*
292 * Remove segment context because both client and server detached
293 */
294
Florin Coras98952382021-11-14 15:33:59 -0800295 pool_put_index (seg_ctx->segments, ct->ct_seg_index);
Florin Coras6973c732021-06-17 15:53:38 -0700296
297 /*
Florin Coras98952382021-11-14 15:33:59 -0800298 * No more segment indices left, remove the segments context
Florin Coras6973c732021-06-17 15:53:38 -0700299 */
Florin Coras98952382021-11-14 15:33:59 -0800300 if (!pool_elts (seg_ctx->segments))
Florin Corasda78c5a2021-06-09 14:55:24 -0700301 {
Florin Coras98952382021-11-14 15:33:59 -0800302 u64 table_handle = seg_ctx->client_wrk << 16 | seg_ctx->server_wrk;
303 table_handle = (u64) seg_ctx->sm_index << 32 | table_handle;
304 hash_unset (cm->app_segs_ctxs_table, table_handle);
305 pool_free (seg_ctx->segments);
306 pool_put_index (cm->app_seg_ctxs, ct->seg_ctx_index);
Florin Corasda78c5a2021-06-09 14:55:24 -0700307 }
Florin Coras98952382021-11-14 15:33:59 -0800308
309 /*
310 * Segment to be removed so notify both apps
311 */
312
313 app_wrk = app_worker_get_if_valid (ct->client_wrk);
314 /* Determine if client app still needs notification, i.e., if it is
315 * still attached. If client detached and this is the last ct session
316 * on this segment, then its connects segment manager should also be
317 * detached, so do not send notification */
318 if (app_wrk)
319 {
320 segment_manager_t *csm;
321 csm = app_worker_get_connect_segment_manager (app_wrk);
322 if (!segment_manager_app_detached (csm))
Florin Coras93752662023-11-20 14:46:10 -0800323 app_worker_del_segment_notify (
324 app_wrk, ct_client_seg_handle (ct->segment_handle, ct->client_wrk));
Florin Coras98952382021-11-14 15:33:59 -0800325 }
326
Florin Coras13987da2021-12-07 14:47:12 -0800327 /* Notify server app and free segment */
Florin Corasda78c5a2021-06-09 14:55:24 -0700328 segment_manager_lock_and_del_segment (sm, seg_index);
329
330 /* Cleanup segment manager if needed. If server detaches there's a chance
331 * the client's sessions will hold up segment removal */
332 if (segment_manager_app_detached (sm) && !segment_manager_has_fifos (sm))
333 segment_manager_free_safe (sm);
Florin Coras98952382021-11-14 15:33:59 -0800334
335done:
336
337 clib_rwlock_writer_unlock (&cm->app_segs_lock);
Florin Corasda78c5a2021-06-09 14:55:24 -0700338}
339
Florin Corasa7bd8262021-11-25 12:14:25 -0800340static void
341ct_session_force_disconnect_server (ct_connection_t *sct)
342{
343 sct->peer_index = ~0;
344 session_transport_closing_notify (&sct->connection);
345}
346
Florin Corasda78c5a2021-06-09 14:55:24 -0700347int
Florin Coras6973c732021-06-17 15:53:38 -0700348ct_session_connect_notify (session_t *ss, session_error_t err)
Florin Corasda78c5a2021-06-09 14:55:24 -0700349{
Florin Coras6973c732021-06-17 15:53:38 -0700350 u32 ss_index, opaque, thread_index;
Florin Corasda78c5a2021-06-09 14:55:24 -0700351 ct_connection_t *sct, *cct;
Florin Corasda78c5a2021-06-09 14:55:24 -0700352 app_worker_t *client_wrk;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800353 session_t *cs;
Florin Corasba7d8f52019-02-22 13:11:38 -0800354
Florin Coras2b81e3c2019-02-27 07:55:46 -0800355 ss_index = ss->session_index;
Florin Coras2d0e3de2020-10-23 16:31:40 -0700356 thread_index = ss->thread_index;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800357 sct = (ct_connection_t *) session_get_transport (ss);
358 client_wrk = app_worker_get (sct->client_wrk);
Florin Corasdd7d24b2020-08-14 17:51:13 -0700359 opaque = sct->client_opaque;
Florin Corasba7d8f52019-02-22 13:11:38 -0800360
Florin Coras2d0e3de2020-10-23 16:31:40 -0700361 cct = ct_connection_get (sct->peer_index, thread_index);
Florin Corasba7d8f52019-02-22 13:11:38 -0800362
Florin Coras374df7a2021-05-13 11:37:43 -0700363 /* Client closed while waiting for reply from server */
Florin Coras6973c732021-06-17 15:53:38 -0700364 if (PREDICT_FALSE (!cct))
Florin Coras374df7a2021-05-13 11:37:43 -0700365 {
Florin Corasa7bd8262021-11-25 12:14:25 -0800366 ct_session_force_disconnect_server (sct);
Florin Coras374df7a2021-05-13 11:37:43 -0700367 return 0;
368 }
369
370 session_half_open_delete_notify (&cct->connection);
371 cct->flags &= ~CT_CONN_F_HALF_OPEN;
372
Florin Coras6973c732021-06-17 15:53:38 -0700373 if (PREDICT_FALSE (err))
374 goto connect_error;
Florin Corasda78c5a2021-06-09 14:55:24 -0700375
376 /*
Florin Corasa7bd8262021-11-25 12:14:25 -0800377 * Alloc client session, server session assumed to be established
Florin Corasda78c5a2021-06-09 14:55:24 -0700378 */
379
Florin Corasa7bd8262021-11-25 12:14:25 -0800380 ASSERT (ss->session_state >= SESSION_STATE_READY);
381
Florin Coras2d0e3de2020-10-23 16:31:40 -0700382 cs = session_alloc (thread_index);
383 ss = session_get (ss_index, thread_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800384 cs->session_type = ss->session_type;
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +0200385 cs->listener_handle = SESSION_INVALID_HANDLE;
Steven Luongd810a6e2022-10-25 13:09:11 -0700386 session_set_state (cs, SESSION_STATE_CONNECTING);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800387 cs->app_wrk_index = client_wrk->wrk_index;
388 cs->connection_index = cct->c_c_index;
Florin Coras2ceb8182023-08-31 17:33:47 -0700389 cs->opaque = opaque;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800390 cct->c_s_index = cs->session_index;
391
392 /* This will allocate fifos for the session. They won't be used for
393 * exchanging data but they will be used to close the connection if
394 * the segment manager/worker is freed */
Florin Corasdd7d24b2020-08-14 17:51:13 -0700395 if ((err = app_worker_init_connected (client_wrk, cs)))
Florin Coras2b81e3c2019-02-27 07:55:46 -0800396 {
Florin Corasdd7d24b2020-08-14 17:51:13 -0700397 session_free (cs);
Florin Corasa7bd8262021-11-25 12:14:25 -0800398 ct_session_force_disconnect_server (sct);
Florin Coras6973c732021-06-17 15:53:38 -0700399 err = SESSION_E_ALLOC;
400 goto connect_error;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800401 }
402
Steven Luongd810a6e2022-10-25 13:09:11 -0700403 session_set_state (cs, SESSION_STATE_CONNECTING);
Florin Corasdd7d24b2020-08-14 17:51:13 -0700404
Florin Coras6973c732021-06-17 15:53:38 -0700405 if (app_worker_connect_notify (client_wrk, cs, 0, opaque))
Florin Coras2b81e3c2019-02-27 07:55:46 -0800406 {
Florin Coras6973c732021-06-17 15:53:38 -0700407 segment_manager_dealloc_fifos (cs->rx_fifo, cs->tx_fifo);
Florin Corasdd7d24b2020-08-14 17:51:13 -0700408 session_free (cs);
Florin Corasa7bd8262021-11-25 12:14:25 -0800409 ct_session_force_disconnect_server (sct);
Florin Coras6973c732021-06-17 15:53:38 -0700410 goto cleanup_client;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800411 }
412
Florin Coras2d0e3de2020-10-23 16:31:40 -0700413 cs = session_get (cct->c_s_index, cct->c_thread_index);
Steven Luongd810a6e2022-10-25 13:09:11 -0700414 session_set_state (cs, SESSION_STATE_READY);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800415
Florin Corasba7d8f52019-02-22 13:11:38 -0800416 return 0;
Florin Corasdd7d24b2020-08-14 17:51:13 -0700417
Florin Coras6973c732021-06-17 15:53:38 -0700418connect_error:
419
420 app_worker_connect_notify (client_wrk, 0, err, cct->client_opaque);
421
422cleanup_client:
423
424 if (cct->client_rx_fifo)
425 ct_session_dealloc_fifos (cct, cct->client_rx_fifo, cct->client_tx_fifo);
426 ct_connection_free (cct);
Florin Corasdd7d24b2020-08-14 17:51:13 -0700427 return -1;
Florin Corasba7d8f52019-02-22 13:11:38 -0800428}
429
Florin Coras6973c732021-06-17 15:53:38 -0700430static inline ct_segment_t *
431ct_lookup_free_segment (ct_main_t *cm, segment_manager_t *sm,
432 u32 seg_ctx_index)
Florin Corasba7d8f52019-02-22 13:11:38 -0800433{
Florin Corasda78c5a2021-06-09 14:55:24 -0700434 uword free_bytes, max_free_bytes;
435 ct_segment_t *ct_seg, *res = 0;
Florin Coras6973c732021-06-17 15:53:38 -0700436 ct_segments_ctx_t *seg_ctx;
Florin Corasda78c5a2021-06-09 14:55:24 -0700437 fifo_segment_t *fs;
438 u32 max_fifos;
439
Florin Coras6973c732021-06-17 15:53:38 -0700440 seg_ctx = pool_elt_at_index (cm->app_seg_ctxs, seg_ctx_index);
441 max_free_bytes = seg_ctx->fifo_pair_bytes;
442
Florin Corasda78c5a2021-06-09 14:55:24 -0700443 pool_foreach (ct_seg, seg_ctx->segments)
444 {
445 /* Client or server has detached so segment cannot be used */
Florin Corasda78c5a2021-06-09 14:55:24 -0700446 fs = segment_manager_get_segment (sm, ct_seg->segment_index);
447 free_bytes = fifo_segment_available_bytes (fs);
Florin Coras6973c732021-06-17 15:53:38 -0700448 max_fifos = fifo_segment_size (fs) / seg_ctx->fifo_pair_bytes;
Florin Corasda78c5a2021-06-09 14:55:24 -0700449 if (free_bytes > max_free_bytes &&
450 fifo_segment_num_fifos (fs) / 2 < max_fifos)
451 {
452 max_free_bytes = free_bytes;
453 res = ct_seg;
454 }
455 }
456
457 return res;
458}
459
Florin Coras6973c732021-06-17 15:53:38 -0700460static ct_segment_t *
461ct_alloc_segment (ct_main_t *cm, app_worker_t *server_wrk, u64 table_handle,
462 segment_manager_t *sm, u32 client_wrk_index)
463{
464 u32 seg_ctx_index = ~0, sm_index, pair_bytes;
Florin Coras93752662023-11-20 14:46:10 -0800465 u64 seg_size, seg_handle, client_seg_handle;
Florin Coras6973c732021-06-17 15:53:38 -0700466 segment_manager_props_t *props;
467 const u32 margin = 16 << 10;
468 ct_segments_ctx_t *seg_ctx;
469 app_worker_t *client_wrk;
Florin Coras6973c732021-06-17 15:53:38 -0700470 application_t *server;
471 ct_segment_t *ct_seg;
472 uword *spp;
473 int fs_index;
474
475 server = application_get (server_wrk->app_index);
476 props = application_segment_manager_properties (server);
477 sm_index = segment_manager_index (sm);
478 pair_bytes = props->rx_fifo_size + props->tx_fifo_size + margin;
479
480 /*
481 * Make sure another thread did not alloc a segment while acquiring the lock
482 */
483
484 spp = hash_get (cm->app_segs_ctxs_table, table_handle);
485 if (spp)
486 {
487 seg_ctx_index = *spp;
488 ct_seg = ct_lookup_free_segment (cm, sm, seg_ctx_index);
489 if (ct_seg)
490 return ct_seg;
491 }
492
493 /*
494 * No segment, try to alloc one and notify the server and the client.
495 * Make sure the segment is not used for other fifos
496 */
497 seg_size = clib_max (props->segment_size, 128 << 20);
498 fs_index =
499 segment_manager_add_segment2 (sm, seg_size, FIFO_SEGMENT_F_CUSTOM_USE);
500 if (fs_index < 0)
501 return 0;
502
503 if (seg_ctx_index == ~0)
504 {
505 pool_get_zero (cm->app_seg_ctxs, seg_ctx);
506 seg_ctx_index = seg_ctx - cm->app_seg_ctxs;
507 hash_set (cm->app_segs_ctxs_table, table_handle, seg_ctx_index);
508 seg_ctx->server_wrk = server_wrk->wrk_index;
509 seg_ctx->client_wrk = client_wrk_index;
510 seg_ctx->sm_index = sm_index;
511 seg_ctx->fifo_pair_bytes = pair_bytes;
512 }
513 else
514 {
515 seg_ctx = pool_elt_at_index (cm->app_seg_ctxs, seg_ctx_index);
516 }
517
518 pool_get_zero (seg_ctx->segments, ct_seg);
519 ct_seg->segment_index = fs_index;
520 ct_seg->server_n_sessions = 0;
521 ct_seg->client_n_sessions = 0;
522 ct_seg->ct_seg_index = ct_seg - seg_ctx->segments;
523 ct_seg->seg_ctx_index = seg_ctx_index;
524
525 /* New segment, notify the server and client */
526 seg_handle = segment_manager_make_segment_handle (sm_index, fs_index);
527 if (app_worker_add_segment_notify (server_wrk, seg_handle))
528 goto error;
529
530 client_wrk = app_worker_get (client_wrk_index);
Florin Coras93752662023-11-20 14:46:10 -0800531 /* Make sure client workers do not have overlapping segment handles.
532 * Ideally, we should attach fs to client worker segment manager and
533 * create a new handle but that's not currently possible. */
534 client_seg_handle = ct_client_seg_handle (seg_handle, client_wrk_index);
535 if (app_worker_add_segment_notify (client_wrk, client_seg_handle))
Florin Coras6973c732021-06-17 15:53:38 -0700536 {
537 app_worker_del_segment_notify (server_wrk, seg_handle);
538 goto error;
539 }
540
541 return ct_seg;
542
543error:
544
545 segment_manager_lock_and_del_segment (sm, fs_index);
546 pool_put_index (seg_ctx->segments, ct_seg->seg_ctx_index);
547 return 0;
548}
549
Florin Corasda78c5a2021-06-09 14:55:24 -0700550static int
551ct_init_accepted_session (app_worker_t *server_wrk, ct_connection_t *ct,
552 session_t *ls, session_t *ll)
553{
Florin Coras88001c62019-04-24 14:44:46 -0700554 segment_manager_props_t *props;
Florin Coras6973c732021-06-17 15:53:38 -0700555 u64 seg_handle, table_handle;
556 u32 sm_index, fs_index = ~0;
Florin Corasda78c5a2021-06-09 14:55:24 -0700557 ct_segments_ctx_t *seg_ctx;
558 ct_main_t *cm = &ct_main;
Florin Coras653e43f2019-03-04 10:56:23 -0800559 application_t *server;
Florin Corasba7d8f52019-02-22 13:11:38 -0800560 segment_manager_t *sm;
Florin Corasda78c5a2021-06-09 14:55:24 -0700561 ct_segment_t *ct_seg;
562 fifo_segment_t *fs;
Florin Corasda78c5a2021-06-09 14:55:24 -0700563 uword *spp;
Florin Coras6973c732021-06-17 15:53:38 -0700564 int rv;
Florin Corasba7d8f52019-02-22 13:11:38 -0800565
Florin Coras2b81e3c2019-02-27 07:55:46 -0800566 sm = app_worker_get_listen_segment_manager (server_wrk, ll);
Florin Corasda78c5a2021-06-09 14:55:24 -0700567 sm_index = segment_manager_index (sm);
568 server = application_get (server_wrk->app_index);
569 props = application_segment_manager_properties (server);
Florin Corasba7d8f52019-02-22 13:11:38 -0800570
Florin Corasda78c5a2021-06-09 14:55:24 -0700571 table_handle = ct->client_wrk << 16 | server_wrk->wrk_index;
Florin Coras6973c732021-06-17 15:53:38 -0700572 table_handle = (u64) sm_index << 32 | table_handle;
Florin Corasda78c5a2021-06-09 14:55:24 -0700573
574 /*
575 * Check if we already have a segment that can hold the fifos
576 */
577
578 clib_rwlock_reader_lock (&cm->app_segs_lock);
579
580 spp = hash_get (cm->app_segs_ctxs_table, table_handle);
581 if (spp)
582 {
Florin Coras6973c732021-06-17 15:53:38 -0700583 ct_seg = ct_lookup_free_segment (cm, sm, *spp);
Florin Corasda78c5a2021-06-09 14:55:24 -0700584 if (ct_seg)
585 {
Florin Coras6973c732021-06-17 15:53:38 -0700586 ct->seg_ctx_index = ct_seg->seg_ctx_index;
587 ct->ct_seg_index = ct_seg->ct_seg_index;
Florin Corasda78c5a2021-06-09 14:55:24 -0700588 fs_index = ct_seg->segment_index;
Florin Coras98952382021-11-14 15:33:59 -0800589 ct_seg->flags &=
590 ~(CT_SEGMENT_F_SERVER_DETACHED | CT_SEGMENT_F_CLIENT_DETACHED);
Florin Corasda78c5a2021-06-09 14:55:24 -0700591 __atomic_add_fetch (&ct_seg->server_n_sessions, 1, __ATOMIC_RELAXED);
Florin Coras6973c732021-06-17 15:53:38 -0700592 __atomic_add_fetch (&ct_seg->client_n_sessions, 1, __ATOMIC_RELAXED);
Florin Corasda78c5a2021-06-09 14:55:24 -0700593 }
594 }
595
596 clib_rwlock_reader_unlock (&cm->app_segs_lock);
597
598 /*
Florin Coras6973c732021-06-17 15:53:38 -0700599 * If not, grab exclusive lock and allocate segment
Florin Corasda78c5a2021-06-09 14:55:24 -0700600 */
Florin Coras6973c732021-06-17 15:53:38 -0700601 if (fs_index == ~0)
Florin Corasda78c5a2021-06-09 14:55:24 -0700602 {
Florin Corasda78c5a2021-06-09 14:55:24 -0700603 clib_rwlock_writer_lock (&cm->app_segs_lock);
604
Florin Coras6973c732021-06-17 15:53:38 -0700605 ct_seg =
606 ct_alloc_segment (cm, server_wrk, table_handle, sm, ct->client_wrk);
607 if (!ct_seg)
Florin Corasda78c5a2021-06-09 14:55:24 -0700608 {
Florin Coras6973c732021-06-17 15:53:38 -0700609 clib_rwlock_writer_unlock (&cm->app_segs_lock);
610 return -1;
Florin Corasda78c5a2021-06-09 14:55:24 -0700611 }
Florin Corasda78c5a2021-06-09 14:55:24 -0700612
Florin Coras6973c732021-06-17 15:53:38 -0700613 ct->seg_ctx_index = ct_seg->seg_ctx_index;
614 ct->ct_seg_index = ct_seg->ct_seg_index;
Florin Corasda78c5a2021-06-09 14:55:24 -0700615 ct_seg->server_n_sessions += 1;
Florin Coras6973c732021-06-17 15:53:38 -0700616 ct_seg->client_n_sessions += 1;
617 fs_index = ct_seg->segment_index;
Florin Corasda78c5a2021-06-09 14:55:24 -0700618
619 clib_rwlock_writer_unlock (&cm->app_segs_lock);
Florin Corasda78c5a2021-06-09 14:55:24 -0700620 }
621
622 /*
623 * Allocate and initialize the fifos
624 */
625 fs = segment_manager_get_segment_w_lock (sm, fs_index);
626 rv = segment_manager_try_alloc_fifos (
627 fs, ls->thread_index, props->rx_fifo_size, props->tx_fifo_size,
628 &ls->rx_fifo, &ls->tx_fifo);
Florin Corasba7d8f52019-02-22 13:11:38 -0800629 if (rv)
630 {
Florin Corasba7d8f52019-02-22 13:11:38 -0800631 segment_manager_segment_reader_unlock (sm);
Florin Coras6973c732021-06-17 15:53:38 -0700632
633 clib_rwlock_reader_lock (&cm->app_segs_lock);
634
635 seg_ctx = pool_elt_at_index (cm->app_seg_ctxs, ct->seg_ctx_index);
636 ct_seg = pool_elt_at_index (seg_ctx->segments, ct->ct_seg_index);
637 __atomic_sub_fetch (&ct_seg->server_n_sessions, 1, __ATOMIC_RELAXED);
638 __atomic_sub_fetch (&ct_seg->client_n_sessions, 1, __ATOMIC_RELAXED);
639
640 clib_rwlock_reader_unlock (&cm->app_segs_lock);
641
642 return rv;
Florin Corasba7d8f52019-02-22 13:11:38 -0800643 }
Florin Coras2b81e3c2019-02-27 07:55:46 -0800644
Florin Corasc547e912020-12-08 17:50:45 -0800645 ls->rx_fifo->shr->master_session_index = ls->session_index;
646 ls->tx_fifo->shr->master_session_index = ls->session_index;
Florin Corasce252ca2020-11-04 13:08:35 -0800647 ls->rx_fifo->master_thread_index = ls->thread_index;
648 ls->tx_fifo->master_thread_index = ls->thread_index;
Florin Corasba7d8f52019-02-22 13:11:38 -0800649
Florin Corasda78c5a2021-06-09 14:55:24 -0700650 seg_handle = segment_manager_segment_handle (sm, fs);
Florin Corasba7d8f52019-02-22 13:11:38 -0800651 segment_manager_segment_reader_unlock (sm);
Florin Corasda78c5a2021-06-09 14:55:24 -0700652
653 ct->segment_handle = seg_handle;
Florin Corasba7d8f52019-02-22 13:11:38 -0800654
655 return 0;
Florin Corasba7d8f52019-02-22 13:11:38 -0800656}
657
Florin Coras2d0e3de2020-10-23 16:31:40 -0700658static void
Florin Corasc215e5a2021-11-15 19:01:52 -0800659ct_accept_one (u32 thread_index, u32 ho_index)
Florin Coras2d0e3de2020-10-23 16:31:40 -0700660{
Florin Corasba026412021-06-12 11:56:19 -0700661 ct_connection_t *sct, *cct, *ho;
662 transport_connection_t *ll_ct;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800663 app_worker_t *server_wrk;
Florin Corasc215e5a2021-11-15 19:01:52 -0800664 u32 cct_index, ll_index;
Florin Coras2d0e3de2020-10-23 16:31:40 -0700665 session_t *ss, *ll;
Florin Corasba7d8f52019-02-22 13:11:38 -0800666
Florin Coras374df7a2021-05-13 11:37:43 -0700667 /*
668 * Alloc client ct and initialize from ho
669 */
Florin Coras374df7a2021-05-13 11:37:43 -0700670 cct = ct_connection_alloc (thread_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800671 cct_index = cct->c_c_index;
Florin Coras374df7a2021-05-13 11:37:43 -0700672
Florin Coras55686e12023-03-20 09:58:01 -0700673 ho = ct_half_open_get (ho_index);
Florin Coras374df7a2021-05-13 11:37:43 -0700674
675 /* Unlikely but half-open session and transport could have been freed */
676 if (PREDICT_FALSE (!ho))
677 {
678 ct_connection_free (cct);
679 return;
680 }
681
682 clib_memcpy (cct, ho, sizeof (*ho));
683 cct->c_c_index = cct_index;
684 cct->c_thread_index = thread_index;
685 cct->flags |= CT_CONN_F_HALF_OPEN;
686
687 /* Notify session layer that half-open is on a different thread
688 * and mark ho connection index reusable. Avoids another rpc
689 */
690 session_half_open_migrate_notify (&cct->connection);
691 session_half_open_migrated_notify (&cct->connection);
692 ct_half_open_add_reusable (ho_index);
Florin Corasba7d8f52019-02-22 13:11:38 -0800693
Florin Coras2b81e3c2019-02-27 07:55:46 -0800694 /*
Florin Coras374df7a2021-05-13 11:37:43 -0700695 * Alloc and init server transport
Florin Coras2b81e3c2019-02-27 07:55:46 -0800696 */
Florin Coras374df7a2021-05-13 11:37:43 -0700697
698 ll_index = cct->peer_index;
699 ll = listen_session_get (ll_index);
700 sct = ct_connection_alloc (thread_index);
Florin Corasba026412021-06-12 11:56:19 -0700701 /* Transport not necessarily ct but it might, so grab after sct alloc */
702 ll_ct = listen_session_get_transport (ll);
Florin Coras374df7a2021-05-13 11:37:43 -0700703
704 /* Make sure cct is valid after sct alloc */
705 cct = ct_connection_get (cct_index, thread_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800706
Florin Coras2b81e3c2019-02-27 07:55:46 -0800707 sct->c_rmt_port = 0;
Florin Corasba026412021-06-12 11:56:19 -0700708 sct->c_lcl_port = ll_ct->lcl_port;
Florin Coras374df7a2021-05-13 11:37:43 -0700709 sct->c_is_ip4 = cct->c_is_ip4;
Florin Corasd9e98702021-10-11 18:10:41 -0700710 clib_memcpy (&sct->c_lcl_ip, &cct->c_rmt_ip, sizeof (cct->c_rmt_ip));
Florin Coras374df7a2021-05-13 11:37:43 -0700711 sct->client_wrk = cct->client_wrk;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800712 sct->c_proto = TRANSPORT_PROTO_NONE;
Florin Coras374df7a2021-05-13 11:37:43 -0700713 sct->client_opaque = cct->client_opaque;
Florin Corasba026412021-06-12 11:56:19 -0700714 sct->actual_tp = cct->actual_tp;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800715
716 sct->peer_index = cct->c_c_index;
717 cct->peer_index = sct->c_c_index;
718
719 /*
720 * Accept server session. Client session is created only after
721 * server confirms accept.
722 */
Florin Coras374df7a2021-05-13 11:37:43 -0700723 ss = session_alloc (thread_index);
724 ll = listen_session_get (ll_index);
Florin Coras9f1a5432019-03-10 21:03:20 -0700725 ss->session_type = session_type_from_proto_and_ip (TRANSPORT_PROTO_NONE,
726 sct->c_is_ip4);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800727 ss->connection_index = sct->c_c_index;
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +0200728 ss->listener_handle = listen_session_get_handle (ll);
Steven Luongd810a6e2022-10-25 13:09:11 -0700729 session_set_state (ss, SESSION_STATE_CREATED);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800730
731 server_wrk = application_listener_select_worker (ll);
732 ss->app_wrk_index = server_wrk->wrk_index;
733
734 sct->c_s_index = ss->session_index;
735 sct->server_wrk = ss->app_wrk_index;
736
Florin Coras2d0e3de2020-10-23 16:31:40 -0700737 if (ct_init_accepted_session (server_wrk, sct, ss, ll))
Florin Corasba7d8f52019-02-22 13:11:38 -0800738 {
Florin Coras6973c732021-06-17 15:53:38 -0700739 ct_session_connect_notify (ss, SESSION_E_ALLOC);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800740 ct_connection_free (sct);
741 session_free (ss);
Florin Coras2d0e3de2020-10-23 16:31:40 -0700742 return;
Florin Corasba7d8f52019-02-22 13:11:38 -0800743 }
744
Florin Coras98952382021-11-14 15:33:59 -0800745 cct->server_wrk = sct->server_wrk;
Florin Coras6973c732021-06-17 15:53:38 -0700746 cct->seg_ctx_index = sct->seg_ctx_index;
747 cct->ct_seg_index = sct->ct_seg_index;
748 cct->client_rx_fifo = ss->tx_fifo;
749 cct->client_tx_fifo = ss->rx_fifo;
750 cct->client_rx_fifo->refcnt++;
751 cct->client_tx_fifo->refcnt++;
Florin Coras93752662023-11-20 14:46:10 -0800752 cct->segment_handle =
753 ct_client_seg_handle (sct->segment_handle, cct->client_wrk);
Florin Coras6973c732021-06-17 15:53:38 -0700754
Steven Luongd810a6e2022-10-25 13:09:11 -0700755 session_set_state (ss, SESSION_STATE_ACCEPTING);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800756 if (app_worker_accept_notify (server_wrk, ss))
757 {
Florin Coras6973c732021-06-17 15:53:38 -0700758 ct_session_connect_notify (ss, SESSION_E_REFUSED);
Florin Corasda78c5a2021-06-09 14:55:24 -0700759 ct_session_dealloc_fifos (sct, ss->rx_fifo, ss->tx_fifo);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800760 ct_connection_free (sct);
Florin Coras12813d52020-04-09 20:59:20 +0000761 session_free (ss);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800762 }
Florin Coras2d0e3de2020-10-23 16:31:40 -0700763}
764
Florin Corasc215e5a2021-11-15 19:01:52 -0800765static void
766ct_accept_rpc_wrk_handler (void *rpc_args)
767{
Florin Coras55686e12023-03-20 09:58:01 -0700768 u32 thread_index, n_connects, i, n_pending;
Florin Corasc215e5a2021-11-15 19:01:52 -0800769 const u32 max_connects = 32;
770 ct_worker_t *wrk;
Florin Coras55686e12023-03-20 09:58:01 -0700771 u8 need_rpc = 0;
Florin Corasc215e5a2021-11-15 19:01:52 -0800772
773 thread_index = pointer_to_uword (rpc_args);
774 wrk = ct_worker_get (thread_index);
775
Florin Coras55686e12023-03-20 09:58:01 -0700776 /* Connects could be handled without worker barrier so grab lock */
777 clib_spinlock_lock (&wrk->pending_connects_lock);
778
Florin Corasc215e5a2021-11-15 19:01:52 -0800779 n_pending = clib_fifo_elts (wrk->pending_connects);
780 n_connects = clib_min (n_pending, max_connects);
Florin Coras55686e12023-03-20 09:58:01 -0700781 vec_validate (wrk->new_connects, n_connects);
Florin Corasc215e5a2021-11-15 19:01:52 -0800782
783 for (i = 0; i < n_connects; i++)
Florin Coras55686e12023-03-20 09:58:01 -0700784 clib_fifo_sub1 (wrk->pending_connects, wrk->new_connects[i]);
Florin Corasc215e5a2021-11-15 19:01:52 -0800785
786 if (n_pending == n_connects)
787 wrk->have_connects = 0;
788 else
Florin Coras55686e12023-03-20 09:58:01 -0700789 need_rpc = 1;
790
791 clib_spinlock_unlock (&wrk->pending_connects_lock);
792
793 for (i = 0; i < n_connects; i++)
794 ct_accept_one (thread_index, wrk->new_connects[i]);
795
796 if (need_rpc)
Florin Corasc215e5a2021-11-15 19:01:52 -0800797 session_send_rpc_evt_to_thread_force (
798 thread_index, ct_accept_rpc_wrk_handler,
799 uword_to_pointer (thread_index, void *));
800}
801
Florin Coras55686e12023-03-20 09:58:01 -0700802static void
803ct_fwrk_flush_connects (void *rpc_args)
Florin Coras2d0e3de2020-10-23 16:31:40 -0700804{
Florin Coras55686e12023-03-20 09:58:01 -0700805 u32 thread_index, fwrk_index, n_workers;
Florin Coras2d0e3de2020-10-23 16:31:40 -0700806 ct_main_t *cm = &ct_main;
Florin Corasc215e5a2021-11-15 19:01:52 -0800807 ct_worker_t *wrk;
Florin Coras55686e12023-03-20 09:58:01 -0700808 u8 need_rpc;
809
Florin Coras1315d142023-04-04 11:51:37 -0700810 fwrk_index = cm->fwrk_thread;
Florin Coras55686e12023-03-20 09:58:01 -0700811 n_workers = vec_len (cm->fwrk_pending_connects);
812
813 for (thread_index = fwrk_index; thread_index < n_workers; thread_index++)
814 {
Florin Coras1315d142023-04-04 11:51:37 -0700815 if (!vec_len (cm->fwrk_pending_connects[thread_index]))
816 continue;
817
Florin Coras55686e12023-03-20 09:58:01 -0700818 wrk = ct_worker_get (thread_index);
819
820 /* Connects can be done without worker barrier, grab dst worker lock */
821 if (thread_index != fwrk_index)
822 clib_spinlock_lock (&wrk->pending_connects_lock);
823
824 clib_fifo_add (wrk->pending_connects,
825 cm->fwrk_pending_connects[thread_index],
826 vec_len (cm->fwrk_pending_connects[thread_index]));
827 if (!wrk->have_connects)
828 {
829 wrk->have_connects = 1;
830 need_rpc = 1;
831 }
832
833 if (thread_index != fwrk_index)
834 clib_spinlock_unlock (&wrk->pending_connects_lock);
835
836 vec_reset_length (cm->fwrk_pending_connects[thread_index]);
837
838 if (need_rpc)
839 session_send_rpc_evt_to_thread_force (
840 thread_index, ct_accept_rpc_wrk_handler,
841 uword_to_pointer (thread_index, void *));
842 }
843
844 cm->fwrk_have_flush = 0;
845}
846
847static void
848ct_program_connect_to_wrk (u32 ho_index)
849{
850 ct_main_t *cm = &ct_main;
851 u32 thread_index;
Florin Coras2d0e3de2020-10-23 16:31:40 -0700852
853 /* Simple round-robin policy for spreading sessions over workers. We skip
854 * thread index 0, i.e., offset the index by 1, when we have workers as it
855 * is the one dedicated to main thread. Note that n_workers does not include
856 * main thread */
857 cm->n_sessions += 1;
858 thread_index = cm->n_workers ? (cm->n_sessions % cm->n_workers) + 1 : 0;
859
Florin Coras55686e12023-03-20 09:58:01 -0700860 /* Pospone flushing of connect request to dst worker until after session
861 * layer fully initializes the half-open session. */
862 vec_add1 (cm->fwrk_pending_connects[thread_index], ho_index);
863 if (!cm->fwrk_have_flush)
864 {
865 session_send_rpc_evt_to_thread_force (
866 cm->fwrk_thread, ct_fwrk_flush_connects,
867 uword_to_pointer (thread_index, void *));
868 cm->fwrk_have_flush = 1;
869 }
870}
871
872static int
873ct_connect (app_worker_t *client_wrk, session_t *ll,
874 session_endpoint_cfg_t *sep)
875{
876 ct_connection_t *ho;
877 u32 ho_index;
878
Florin Coras374df7a2021-05-13 11:37:43 -0700879 /*
880 * Alloc and init client half-open transport
881 */
Florin Coras2d0e3de2020-10-23 16:31:40 -0700882
Florin Coras374df7a2021-05-13 11:37:43 -0700883 ho = ct_half_open_alloc ();
884 ho_index = ho->c_c_index;
885 ho->c_rmt_port = sep->port;
886 ho->c_lcl_port = 0;
887 ho->c_is_ip4 = sep->is_ip4;
888 ho->client_opaque = sep->opaque;
889 ho->client_wrk = client_wrk->wrk_index;
890 ho->peer_index = ll->session_index;
891 ho->c_proto = TRANSPORT_PROTO_NONE;
892 ho->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
Florin Corasd5a58822021-05-14 20:11:14 -0700893 clib_memcpy (&ho->c_rmt_ip, &sep->ip, sizeof (sep->ip));
Florin Coras374df7a2021-05-13 11:37:43 -0700894 ho->flags |= CT_CONN_F_CLIENT;
895 ho->c_s_index = ~0;
Florin Corasfeb67022021-11-11 09:24:34 -0800896 ho->actual_tp = sep->original_tp;
Florin Coras374df7a2021-05-13 11:37:43 -0700897
898 /*
Florin Coras55686e12023-03-20 09:58:01 -0700899 * Program connect on a worker, connected reply comes
Florin Coras374df7a2021-05-13 11:37:43 -0700900 * after server accepts the connection.
901 */
Florin Coras55686e12023-03-20 09:58:01 -0700902 ct_program_connect_to_wrk (ho_index);
Florin Coras374df7a2021-05-13 11:37:43 -0700903
904 return ho_index;
Florin Corasba7d8f52019-02-22 13:11:38 -0800905}
906
Florin Coras653e43f2019-03-04 10:56:23 -0800907static u32
Florin Coras0bce71e2022-02-10 11:57:06 -0800908ct_start_listen (u32 app_listener_index, transport_endpoint_cfg_t *tep)
Florin Corasd4295e62019-02-22 13:11:38 -0800909{
910 session_endpoint_cfg_t *sep;
911 ct_connection_t *ct;
912
913 sep = (session_endpoint_cfg_t *) tep;
Florin Coras2d0e3de2020-10-23 16:31:40 -0700914 ct = ct_connection_alloc (0);
Florin Corasd4295e62019-02-22 13:11:38 -0800915 ct->server_wrk = sep->app_wrk_index;
916 ct->c_is_ip4 = sep->is_ip4;
917 clib_memcpy (&ct->c_lcl_ip, &sep->ip, sizeof (sep->ip));
918 ct->c_lcl_port = sep->port;
Florin Corasa8375082020-10-25 19:06:57 -0700919 ct->c_s_index = app_listener_index;
Florin Corasd4295e62019-02-22 13:11:38 -0800920 ct->actual_tp = sep->transport_proto;
921 return ct->c_c_index;
922}
923
Florin Coras653e43f2019-03-04 10:56:23 -0800924static u32
Florin Corasd4295e62019-02-22 13:11:38 -0800925ct_stop_listen (u32 ct_index)
926{
927 ct_connection_t *ct;
Florin Coras2d0e3de2020-10-23 16:31:40 -0700928 ct = ct_connection_get (ct_index, 0);
Florin Corasd4295e62019-02-22 13:11:38 -0800929 ct_connection_free (ct);
930 return 0;
931}
932
Florin Coras653e43f2019-03-04 10:56:23 -0800933static transport_connection_t *
Florin Corasd4295e62019-02-22 13:11:38 -0800934ct_listener_get (u32 ct_index)
935{
Florin Coras2d0e3de2020-10-23 16:31:40 -0700936 return (transport_connection_t *) ct_connection_get (ct_index, 0);
Florin Corasd4295e62019-02-22 13:11:38 -0800937}
938
Florin Coras374df7a2021-05-13 11:37:43 -0700939static transport_connection_t *
Florin Coras55686e12023-03-20 09:58:01 -0700940ct_session_half_open_get (u32 ct_index)
Florin Coras374df7a2021-05-13 11:37:43 -0700941{
Florin Coras55686e12023-03-20 09:58:01 -0700942 return (transport_connection_t *) ct_half_open_get (ct_index);
Florin Coras374df7a2021-05-13 11:37:43 -0700943}
944
945static void
946ct_session_cleanup (u32 conn_index, u32 thread_index)
947{
948 ct_connection_t *ct, *peer_ct;
949
950 ct = ct_connection_get (conn_index, thread_index);
951 if (!ct)
952 return;
953
954 peer_ct = ct_connection_get (ct->peer_index, thread_index);
955 if (peer_ct)
956 peer_ct->peer_index = ~0;
957
958 ct_connection_free (ct);
959}
960
961static void
962ct_cleanup_ho (u32 ho_index)
963{
Florin Coras55686e12023-03-20 09:58:01 -0700964 ct_connection_t *ho;
965
966 ho = ct_half_open_get (ho_index);
967 ct_connection_free (ho);
Florin Coras374df7a2021-05-13 11:37:43 -0700968}
969
Florin Coras653e43f2019-03-04 10:56:23 -0800970static int
Florin Coras2b81e3c2019-02-27 07:55:46 -0800971ct_session_connect (transport_endpoint_cfg_t * tep)
972{
973 session_endpoint_cfg_t *sep_ext;
Florin Coras374df7a2021-05-13 11:37:43 -0700974 session_endpoint_t _sep, *sep = &_sep;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800975 app_worker_t *app_wrk;
976 session_handle_t lh;
977 application_t *app;
978 app_listener_t *al;
979 u32 table_index;
980 session_t *ll;
981 u8 fib_proto;
982
983 sep_ext = (session_endpoint_cfg_t *) tep;
Florin Coras374df7a2021-05-13 11:37:43 -0700984 _sep = *(session_endpoint_t *) tep;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800985 app_wrk = app_worker_get (sep_ext->app_wrk_index);
986 app = application_get (app_wrk->app_index);
987
988 sep->transport_proto = sep_ext->original_tp;
989 table_index = application_local_session_table (app);
990 lh = session_lookup_local_endpoint (table_index, sep);
991 if (lh == SESSION_DROP_HANDLE)
Florin Coras00e01d32019-10-21 16:07:46 -0700992 return SESSION_E_FILTERED;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800993
994 if (lh == SESSION_INVALID_HANDLE)
995 goto global_scope;
996
997 ll = listen_session_get_from_handle (lh);
Florin Coras97fef282023-12-21 19:41:12 -0800998 al = app_listener_get (ll->al_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800999
1000 /*
1001 * Break loop if rule in local table points to connecting app. This
1002 * can happen if client is a generic proxy. Route connect through
1003 * global table instead.
1004 */
1005 if (al->app_index == app->app_index)
1006 goto global_scope;
1007
1008 return ct_connect (app_wrk, ll, sep_ext);
1009
1010 /*
1011 * If nothing found, check the global scope for locally attached
1012 * destinations. Make sure first that we're allowed to.
1013 */
1014
1015global_scope:
1016 if (session_endpoint_is_local (sep))
Florin Coras00e01d32019-10-21 16:07:46 -07001017 return SESSION_E_NOROUTE;
Florin Coras2b81e3c2019-02-27 07:55:46 -08001018
1019 if (!application_has_global_scope (app))
Florin Coras00e01d32019-10-21 16:07:46 -07001020 return SESSION_E_SCOPE;
Florin Coras2b81e3c2019-02-27 07:55:46 -08001021
1022 fib_proto = session_endpoint_fib_proto (sep);
Florin Coras95cd8642019-12-30 21:53:19 -08001023 table_index = session_lookup_get_index_for_fib (fib_proto, sep->fib_index);
Florin Coras9f1a5432019-03-10 21:03:20 -07001024 ll = session_lookup_listener_wildcard (table_index, sep);
Florin Coras2b81e3c2019-02-27 07:55:46 -08001025
Florin Coras821b5002021-06-02 21:32:39 -07001026 /* Avoid connecting app to own listener */
Florin Coras97fef282023-12-21 19:41:12 -08001027 if (ll)
1028 {
1029 al = app_listener_get (ll->al_index);
1030 if (al->app_index != app->app_index)
1031 return ct_connect (app_wrk, ll, sep_ext);
1032 }
Florin Coras2b81e3c2019-02-27 07:55:46 -08001033
1034 /* Failed to connect but no error */
Florin Coras374df7a2021-05-13 11:37:43 -07001035 return SESSION_E_LOCAL_CONNECT;
Florin Coras2b81e3c2019-02-27 07:55:46 -08001036}
1037
Florin Coras9439a362021-11-25 16:18:39 -08001038static inline int
1039ct_close_is_reset (ct_connection_t *ct, session_t *s)
1040{
Florin Coras6d14c0c2023-12-14 11:46:11 -08001041 if (ct->flags & CT_CONN_F_RESET)
1042 return 1;
Florin Coras9439a362021-11-25 16:18:39 -08001043 if (ct->flags & CT_CONN_F_CLIENT)
1044 return (svm_fifo_max_dequeue (ct->client_rx_fifo) > 0);
1045 else
1046 return (svm_fifo_max_dequeue (s->rx_fifo) > 0);
1047}
1048
Florin Coras653e43f2019-03-04 10:56:23 -08001049static void
Florin Coras0242d302022-12-22 15:03:44 -08001050ct_session_cleanup_server_session (session_t *s)
1051{
1052 ct_connection_t *ct;
1053
1054 ct = (ct_connection_t *) session_get_transport (s);
1055 ct_session_dealloc_fifos (ct, s->rx_fifo, s->tx_fifo);
1056 session_free (s);
1057 ct_connection_free (ct);
1058}
1059
1060static void
Florin Coras440c7b52021-11-09 08:38:24 -08001061ct_session_postponed_cleanup (ct_connection_t *ct)
1062{
Florin Coras9439a362021-11-25 16:18:39 -08001063 ct_connection_t *peer_ct;
Florin Coras440c7b52021-11-09 08:38:24 -08001064 app_worker_t *app_wrk;
1065 session_t *s;
1066
1067 s = session_get (ct->c_s_index, ct->c_thread_index);
1068 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
1069
Florin Coras9439a362021-11-25 16:18:39 -08001070 peer_ct = ct_connection_get (ct->peer_index, ct->c_thread_index);
1071 if (peer_ct)
1072 {
1073 if (ct_close_is_reset (ct, s))
1074 session_transport_reset_notify (&peer_ct->connection);
1075 else
1076 session_transport_closing_notify (&peer_ct->connection);
1077 }
1078 session_transport_closed_notify (&ct->connection);
1079
Florin Coras0242d302022-12-22 15:03:44 -08001080 /* It would be cleaner to call session_transport_delete_notify
1081 * but then we can't control session cleanup lower */
1082 session_set_state (s, SESSION_STATE_TRANSPORT_DELETED);
1083 if (app_wrk)
1084 app_worker_cleanup_notify (app_wrk, s, SESSION_CLEANUP_TRANSPORT);
1085
Florin Coras440c7b52021-11-09 08:38:24 -08001086 if (ct->flags & CT_CONN_F_CLIENT)
1087 {
Florin Coras440c7b52021-11-09 08:38:24 -08001088 /* Normal free for client session as the fifos are allocated through
1089 * the connects segment manager in a segment that's not shared with
1090 * the server */
Florin Coras440c7b52021-11-09 08:38:24 -08001091 ct_session_dealloc_fifos (ct, ct->client_rx_fifo, ct->client_tx_fifo);
Florin Coras0242d302022-12-22 15:03:44 -08001092 session_program_cleanup (s);
1093 ct_connection_free (ct);
Florin Coras440c7b52021-11-09 08:38:24 -08001094 }
1095 else
1096 {
1097 /* Manual session and fifo segment cleanup to avoid implicit
1098 * segment manager cleanups and notifications */
Florin Coras440c7b52021-11-09 08:38:24 -08001099 if (app_wrk)
1100 {
Florin Coras0242d302022-12-22 15:03:44 -08001101 /* Remove custom cleanup notify infra when/if switching to normal
1102 * session cleanup. Note that ct is freed in the cb function */
1103 app_worker_cleanup_notify_custom (app_wrk, s,
1104 SESSION_CLEANUP_SESSION,
1105 ct_session_cleanup_server_session);
Florin Coras440c7b52021-11-09 08:38:24 -08001106 }
Florin Coras0242d302022-12-22 15:03:44 -08001107 else
1108 {
1109 ct_connection_free (ct);
1110 }
Florin Coras440c7b52021-11-09 08:38:24 -08001111 }
Florin Coras440c7b52021-11-09 08:38:24 -08001112}
1113
1114static void
1115ct_handle_cleanups (void *args)
1116{
1117 uword thread_index = pointer_to_uword (args);
1118 const u32 max_cleanups = 100;
Florin Coras440c7b52021-11-09 08:38:24 -08001119 ct_cleanup_req_t *req;
1120 ct_connection_t *ct;
1121 u32 n_to_handle = 0;
Florin Coras7fe00692021-11-15 14:01:02 -08001122 ct_worker_t *wrk;
Florin Coras440c7b52021-11-09 08:38:24 -08001123 session_t *s;
1124
Florin Coras7fe00692021-11-15 14:01:02 -08001125 wrk = ct_worker_get (thread_index);
1126 wrk->have_cleanups = 0;
1127 n_to_handle = clib_fifo_elts (wrk->pending_cleanups);
Florin Coras440c7b52021-11-09 08:38:24 -08001128 n_to_handle = clib_min (n_to_handle, max_cleanups);
1129
1130 while (n_to_handle)
1131 {
Florin Coras7fe00692021-11-15 14:01:02 -08001132 clib_fifo_sub2 (wrk->pending_cleanups, req);
Florin Coras440c7b52021-11-09 08:38:24 -08001133 ct = ct_connection_get (req->ct_index, thread_index);
1134 s = session_get (ct->c_s_index, ct->c_thread_index);
Florin Corase439b1e2024-02-14 16:14:46 -08001135 if (svm_fifo_has_event (s->tx_fifo) || (s->flags & SESSION_F_RX_EVT))
Florin Coras7fe00692021-11-15 14:01:02 -08001136 clib_fifo_add1 (wrk->pending_cleanups, *req);
Florin Corase439b1e2024-02-14 16:14:46 -08001137 else
1138 ct_session_postponed_cleanup (ct);
Florin Coras440c7b52021-11-09 08:38:24 -08001139 n_to_handle -= 1;
1140 }
1141
Florin Coras7fe00692021-11-15 14:01:02 -08001142 if (clib_fifo_elts (wrk->pending_cleanups))
Florin Coras440c7b52021-11-09 08:38:24 -08001143 {
Florin Coras7fe00692021-11-15 14:01:02 -08001144 wrk->have_cleanups = 1;
Florin Coras440c7b52021-11-09 08:38:24 -08001145 session_send_rpc_evt_to_thread_force (
1146 thread_index, ct_handle_cleanups,
1147 uword_to_pointer (thread_index, void *));
1148 }
1149}
1150
1151static void
1152ct_program_cleanup (ct_connection_t *ct)
1153{
Florin Coras440c7b52021-11-09 08:38:24 -08001154 ct_cleanup_req_t *req;
1155 uword thread_index;
Florin Coras7fe00692021-11-15 14:01:02 -08001156 ct_worker_t *wrk;
Florin Coras440c7b52021-11-09 08:38:24 -08001157
1158 thread_index = ct->c_thread_index;
Florin Coras7fe00692021-11-15 14:01:02 -08001159 wrk = ct_worker_get (ct->c_thread_index);
1160
1161 clib_fifo_add2 (wrk->pending_cleanups, req);
Florin Coras440c7b52021-11-09 08:38:24 -08001162 req->ct_index = ct->c_c_index;
1163
Florin Coras7fe00692021-11-15 14:01:02 -08001164 if (wrk->have_cleanups)
Florin Coras440c7b52021-11-09 08:38:24 -08001165 return;
1166
Florin Coras7fe00692021-11-15 14:01:02 -08001167 wrk->have_cleanups = 1;
Florin Coras440c7b52021-11-09 08:38:24 -08001168 session_send_rpc_evt_to_thread_force (
1169 thread_index, ct_handle_cleanups, uword_to_pointer (thread_index, void *));
1170}
1171
1172static void
Florin Coras2b81e3c2019-02-27 07:55:46 -08001173ct_session_close (u32 ct_index, u32 thread_index)
1174{
1175 ct_connection_t *ct, *peer_ct;
1176 session_t *s;
1177
Florin Coras2d0e3de2020-10-23 16:31:40 -07001178 ct = ct_connection_get (ct_index, thread_index);
Florin Coras6973c732021-06-17 15:53:38 -07001179 s = session_get (ct->c_s_index, ct->c_thread_index);
Florin Coras2d0e3de2020-10-23 16:31:40 -07001180 peer_ct = ct_connection_get (ct->peer_index, thread_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -08001181 if (peer_ct)
1182 {
1183 peer_ct->peer_index = ~0;
Florin Coras06ac4bb2020-10-28 16:41:26 -07001184 /* Make sure session was allocated */
Florin Coras374df7a2021-05-13 11:37:43 -07001185 if (peer_ct->flags & CT_CONN_F_HALF_OPEN)
1186 {
Florin Coras6973c732021-06-17 15:53:38 -07001187 ct_session_connect_notify (s, SESSION_E_REFUSED);
Florin Corasa7bd8262021-11-25 12:14:25 -08001188 ct->peer_index = ~0;
Florin Coras374df7a2021-05-13 11:37:43 -07001189 }
Florin Coras9439a362021-11-25 16:18:39 -08001190 else if (peer_ct->c_s_index == ~0)
Florin Coras440c7b52021-11-09 08:38:24 -08001191 {
1192 /* should not happen */
1193 clib_warning ("ct peer without session");
1194 ct_connection_free (peer_ct);
1195 }
Florin Coras2b81e3c2019-02-27 07:55:46 -08001196 }
1197
Florin Coras440c7b52021-11-09 08:38:24 -08001198 /* Do not send closed notify to make sure pending tx events are
1199 * still delivered and program cleanup */
1200 ct_program_cleanup (ct);
Florin Coras2b81e3c2019-02-27 07:55:46 -08001201}
1202
Florin Coras6d14c0c2023-12-14 11:46:11 -08001203static void
1204ct_session_reset (u32 ct_index, u32 thread_index)
1205{
1206 ct_connection_t *ct;
1207 ct = ct_connection_get (ct_index, thread_index);
1208 ct->flags |= CT_CONN_F_RESET;
1209 ct_session_close (ct_index, thread_index);
1210}
1211
Florin Coras653e43f2019-03-04 10:56:23 -08001212static transport_connection_t *
Florin Coras2b81e3c2019-02-27 07:55:46 -08001213ct_session_get (u32 ct_index, u32 thread_index)
1214{
Florin Coras2d0e3de2020-10-23 16:31:40 -07001215 return (transport_connection_t *) ct_connection_get (ct_index,
1216 thread_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -08001217}
1218
Florin Corasd4295e62019-02-22 13:11:38 -08001219static u8 *
1220format_ct_connection_id (u8 * s, va_list * args)
1221{
1222 ct_connection_t *ct = va_arg (*args, ct_connection_t *);
1223 if (!ct)
1224 return s;
1225 if (ct->c_is_ip4)
1226 {
1227 s = format (s, "[%d:%d][CT:%U] %U:%d->%U:%d", ct->c_thread_index,
1228 ct->c_s_index, format_transport_proto_short, ct->actual_tp,
1229 format_ip4_address, &ct->c_lcl_ip4,
1230 clib_net_to_host_u16 (ct->c_lcl_port), format_ip4_address,
1231 &ct->c_rmt_ip4, clib_net_to_host_u16 (ct->c_rmt_port));
1232 }
1233 else
1234 {
1235 s = format (s, "[%d:%d][CT:%U] %U:%d->%U:%d", ct->c_thread_index,
1236 ct->c_s_index, format_transport_proto_short, ct->actual_tp,
1237 format_ip6_address, &ct->c_lcl_ip6,
1238 clib_net_to_host_u16 (ct->c_lcl_port), format_ip6_address,
1239 &ct->c_rmt_ip6, clib_net_to_host_u16 (ct->c_rmt_port));
1240 }
1241
1242 return s;
1243}
1244
Florin Corasf940f8a2019-03-06 10:44:38 -08001245static int
Florin Coras9f86d222020-03-23 15:34:22 +00001246ct_custom_tx (void *session, transport_send_params_t * sp)
Florin Corasf940f8a2019-03-06 10:44:38 -08001247{
1248 session_t *s = (session_t *) session;
Florin Coras074f3972021-12-07 15:12:06 -08001249 if (session_has_transport (s))
Florin Corasf940f8a2019-03-06 10:44:38 -08001250 return 0;
Florin Coras1b9d2c22021-01-26 14:10:43 -08001251 /* If event enqueued towards peer, remove from scheduler and remove
1252 * session tx flag, i.e., accept new tx events. Unset fifo flag now to
1253 * avoid missing events if peer did not clear fifo flag yet, which is
1254 * interpreted as successful notification and session is descheduled. */
1255 svm_fifo_unset_event (s->tx_fifo);
Florin Corasd6894562020-10-28 00:37:15 -07001256 if (!ct_session_tx (s))
Florin Coras1b9d2c22021-01-26 14:10:43 -08001257 sp->flags = TRANSPORT_SND_F_DESCHED;
1258
Florin Corasd6894562020-10-28 00:37:15 -07001259 /* The scheduler uses packet count as a means of upper bounding the amount
1260 * of work done per dispatch. So make it look like we have sent something */
1261 return 1;
Florin Corasf940f8a2019-03-06 10:44:38 -08001262}
1263
Florin Coras317b8e02019-04-17 09:57:46 -07001264static int
1265ct_app_rx_evt (transport_connection_t * tc)
1266{
1267 ct_connection_t *ct = (ct_connection_t *) tc, *peer_ct;
Florin Coras9439a362021-11-25 16:18:39 -08001268 session_t *ps, *s;
Florin Coras317b8e02019-04-17 09:57:46 -07001269
Florin Coras9439a362021-11-25 16:18:39 -08001270 s = session_get (ct->c_s_index, ct->c_thread_index);
1271 if (session_has_transport (s) || s->session_state < SESSION_STATE_READY)
1272 return -1;
Florin Coras2d0e3de2020-10-23 16:31:40 -07001273 peer_ct = ct_connection_get (ct->peer_index, tc->thread_index);
Florin Coras9439a362021-11-25 16:18:39 -08001274 if (!peer_ct || (peer_ct->flags & CT_CONN_F_HALF_OPEN))
Florin Coras317b8e02019-04-17 09:57:46 -07001275 return -1;
1276 ps = session_get (peer_ct->c_s_index, peer_ct->c_thread_index);
Florin Coras9439a362021-11-25 16:18:39 -08001277 if (ps->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
1278 return -1;
Florin Coras317b8e02019-04-17 09:57:46 -07001279 return session_dequeue_notify (ps);
1280}
1281
Florin Coras653e43f2019-03-04 10:56:23 -08001282static u8 *
Florin Corasd4295e62019-02-22 13:11:38 -08001283format_ct_listener (u8 * s, va_list * args)
1284{
1285 u32 tc_index = va_arg (*args, u32);
Aloys Augustina0abbff2019-07-12 12:16:16 +02001286 u32 __clib_unused thread_index = va_arg (*args, u32);
Florin Corasd4295e62019-02-22 13:11:38 -08001287 u32 __clib_unused verbose = va_arg (*args, u32);
Florin Coras2d0e3de2020-10-23 16:31:40 -07001288 ct_connection_t *ct = ct_connection_get (tc_index, 0);
Florin Coras7bf6ed62020-09-23 12:02:08 -07001289 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_ct_connection_id, ct);
Florin Corasd4295e62019-02-22 13:11:38 -08001290 if (verbose)
Florin Coras7bf6ed62020-09-23 12:02:08 -07001291 s = format (s, "%-" SESSION_CLI_STATE_LEN "s", "LISTEN");
Florin Corasd4295e62019-02-22 13:11:38 -08001292 return s;
1293}
1294
Florin Coras653e43f2019-03-04 10:56:23 -08001295static u8 *
Florin Coras374df7a2021-05-13 11:37:43 -07001296format_ct_half_open (u8 *s, va_list *args)
1297{
1298 u32 ho_index = va_arg (*args, u32);
1299 u32 verbose = va_arg (*args, u32);
Florin Coras55686e12023-03-20 09:58:01 -07001300 ct_connection_t *ct = ct_half_open_get (ho_index);
Florin Coras374df7a2021-05-13 11:37:43 -07001301 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_ct_connection_id, ct);
1302 if (verbose)
1303 s = format (s, "%-" SESSION_CLI_STATE_LEN "s", "HALF-OPEN");
1304 return s;
1305}
1306
1307static u8 *
Florin Coras2b81e3c2019-02-27 07:55:46 -08001308format_ct_connection (u8 * s, va_list * args)
1309{
1310 ct_connection_t *ct = va_arg (*args, ct_connection_t *);
1311 u32 verbose = va_arg (*args, u32);
1312
1313 if (!ct)
1314 return s;
Florin Coras7bf6ed62020-09-23 12:02:08 -07001315 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_ct_connection_id, ct);
Florin Coras2b81e3c2019-02-27 07:55:46 -08001316 if (verbose)
1317 {
Florin Coras7bf6ed62020-09-23 12:02:08 -07001318 s = format (s, "%-" SESSION_CLI_STATE_LEN "s", "ESTABLISHED");
Florin Coras2b81e3c2019-02-27 07:55:46 -08001319 if (verbose > 1)
1320 {
1321 s = format (s, "\n");
1322 }
1323 }
1324 return s;
1325}
1326
Florin Coras653e43f2019-03-04 10:56:23 -08001327static u8 *
Florin Coras2b81e3c2019-02-27 07:55:46 -08001328format_ct_session (u8 * s, va_list * args)
1329{
1330 u32 ct_index = va_arg (*args, u32);
Florin Coras2d0e3de2020-10-23 16:31:40 -07001331 u32 thread_index = va_arg (*args, u32);
Florin Coras2b81e3c2019-02-27 07:55:46 -08001332 u32 verbose = va_arg (*args, u32);
1333 ct_connection_t *ct;
1334
Florin Coras2d0e3de2020-10-23 16:31:40 -07001335 ct = ct_connection_get (ct_index, thread_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -08001336 if (!ct)
1337 {
1338 s = format (s, "empty\n");
1339 return s;
1340 }
1341
1342 s = format (s, "%U", format_ct_connection, ct, verbose);
1343 return s;
1344}
1345
Florin Coras2d0e3de2020-10-23 16:31:40 -07001346clib_error_t *
1347ct_enable_disable (vlib_main_t * vm, u8 is_en)
1348{
Florin Coras7fe00692021-11-15 14:01:02 -08001349 vlib_thread_main_t *vtm = &vlib_thread_main;
Florin Coras374df7a2021-05-13 11:37:43 -07001350 ct_main_t *cm = &ct_main;
Florin Coras55686e12023-03-20 09:58:01 -07001351 ct_worker_t *wrk;
Florin Coras374df7a2021-05-13 11:37:43 -07001352
1353 cm->n_workers = vlib_num_workers ();
Florin Coras55686e12023-03-20 09:58:01 -07001354 cm->fwrk_thread = transport_cl_thread ();
Florin Coras7fe00692021-11-15 14:01:02 -08001355 vec_validate (cm->wrk, vtm->n_vlib_mains);
Florin Coras55686e12023-03-20 09:58:01 -07001356 vec_foreach (wrk, cm->wrk)
1357 clib_spinlock_init (&wrk->pending_connects_lock);
Florin Coras374df7a2021-05-13 11:37:43 -07001358 clib_spinlock_init (&cm->ho_reuseable_lock);
Florin Corasda78c5a2021-06-09 14:55:24 -07001359 clib_rwlock_init (&cm->app_segs_lock);
Florin Coras55686e12023-03-20 09:58:01 -07001360 vec_validate (cm->fwrk_pending_connects, cm->n_workers);
Florin Coras2d0e3de2020-10-23 16:31:40 -07001361 return 0;
1362}
1363
Nathan Skrzypczake971bc92019-06-19 13:42:37 +02001364static const transport_proto_vft_t cut_thru_proto = {
Florin Coras2d0e3de2020-10-23 16:31:40 -07001365 .enable = ct_enable_disable,
Florin Corasd4295e62019-02-22 13:11:38 -08001366 .start_listen = ct_start_listen,
1367 .stop_listen = ct_stop_listen,
Florin Coras374df7a2021-05-13 11:37:43 -07001368 .get_connection = ct_session_get,
Florin Corasd4295e62019-02-22 13:11:38 -08001369 .get_listener = ct_listener_get,
Florin Coras55686e12023-03-20 09:58:01 -07001370 .get_half_open = ct_session_half_open_get,
Florin Coras374df7a2021-05-13 11:37:43 -07001371 .cleanup = ct_session_cleanup,
1372 .cleanup_ho = ct_cleanup_ho,
Florin Coras2b81e3c2019-02-27 07:55:46 -08001373 .connect = ct_session_connect,
1374 .close = ct_session_close,
Florin Coras6d14c0c2023-12-14 11:46:11 -08001375 .reset = ct_session_reset,
Florin Corasf940f8a2019-03-06 10:44:38 -08001376 .custom_tx = ct_custom_tx,
Florin Coras317b8e02019-04-17 09:57:46 -07001377 .app_rx_evt = ct_app_rx_evt,
Florin Corasd4295e62019-02-22 13:11:38 -08001378 .format_listener = format_ct_listener,
Florin Coras374df7a2021-05-13 11:37:43 -07001379 .format_half_open = format_ct_half_open,
Florin Coras2b81e3c2019-02-27 07:55:46 -08001380 .format_connection = format_ct_session,
Nathan Skrzypczake971bc92019-06-19 13:42:37 +02001381 .transport_options = {
Florin Coras07063b82020-03-13 04:44:51 +00001382 .name = "ct",
1383 .short_name = "C",
Nathan Skrzypczake971bc92019-06-19 13:42:37 +02001384 .tx_type = TRANSPORT_TX_INTERNAL,
Florin Coras374df7a2021-05-13 11:37:43 -07001385 .service_type = TRANSPORT_SERVICE_VC,
Nathan Skrzypczake971bc92019-06-19 13:42:37 +02001386 },
Florin Corasd4295e62019-02-22 13:11:38 -08001387};
Florin Corasd4295e62019-02-22 13:11:38 -08001388
Florin Coras074f3972021-12-07 15:12:06 -08001389static inline int
1390ct_session_can_tx (session_t *s)
1391{
1392 return (s->session_state == SESSION_STATE_READY ||
1393 s->session_state == SESSION_STATE_CLOSING ||
1394 s->session_state == SESSION_STATE_APP_CLOSED);
1395}
1396
Florin Coras653e43f2019-03-04 10:56:23 -08001397int
1398ct_session_tx (session_t * s)
1399{
1400 ct_connection_t *ct, *peer_ct;
1401 session_t *peer_s;
1402
Florin Coras074f3972021-12-07 15:12:06 -08001403 if (!ct_session_can_tx (s))
1404 return 0;
Florin Coras653e43f2019-03-04 10:56:23 -08001405 ct = (ct_connection_t *) session_get_transport (s);
Florin Coras2d0e3de2020-10-23 16:31:40 -07001406 peer_ct = ct_connection_get (ct->peer_index, ct->c_thread_index);
Florin Coras653e43f2019-03-04 10:56:23 -08001407 if (!peer_ct)
Florin Corasbcdc50d2020-08-25 19:07:02 -07001408 return 0;
Florin Coras2d0e3de2020-10-23 16:31:40 -07001409 peer_s = session_get (peer_ct->c_s_index, peer_ct->c_thread_index);
Florin Coras653e43f2019-03-04 10:56:23 -08001410 if (peer_s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
1411 return 0;
Florin Corase439b1e2024-02-14 16:14:46 -08001412 peer_s->flags |= SESSION_F_RX_EVT;
Florin Corasd6894562020-10-28 00:37:15 -07001413 return session_enqueue_notify (peer_s);
Florin Coras653e43f2019-03-04 10:56:23 -08001414}
1415
Florin Corasd4295e62019-02-22 13:11:38 -08001416static clib_error_t *
1417ct_transport_init (vlib_main_t * vm)
1418{
1419 transport_register_protocol (TRANSPORT_PROTO_NONE, &cut_thru_proto,
1420 FIB_PROTOCOL_IP4, ~0);
Florin Coras653e43f2019-03-04 10:56:23 -08001421 transport_register_protocol (TRANSPORT_PROTO_NONE, &cut_thru_proto,
1422 FIB_PROTOCOL_IP6, ~0);
Florin Corasd4295e62019-02-22 13:11:38 -08001423 return 0;
1424}
1425
1426VLIB_INIT_FUNCTION (ct_transport_init);
1427
Florin Corasba7d8f52019-02-22 13:11:38 -08001428/*
1429 * fd.io coding-style-patch-verification: ON
1430 *
1431 * Local Variables:
1432 * eval: (c-set-style "gnu")
1433 * End:
1434 */