blob: e3451531a484ca56e6b4c0b59d0f78caba845447 [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
197static void
Florin Corasda78c5a2021-06-09 14:55:24 -0700198ct_session_dealloc_fifos (ct_connection_t *ct, svm_fifo_t *rx_fifo,
199 svm_fifo_t *tx_fifo)
Florin Corasba7d8f52019-02-22 13:11:38 -0800200{
Florin Corasda78c5a2021-06-09 14:55:24 -0700201 ct_segments_ctx_t *seg_ctx;
202 ct_main_t *cm = &ct_main;
Florin Corasba7d8f52019-02-22 13:11:38 -0800203 segment_manager_t *sm;
Florin Corasda78c5a2021-06-09 14:55:24 -0700204 app_worker_t *app_wrk;
205 ct_segment_t *ct_seg;
206 fifo_segment_t *fs;
207 u32 seg_index;
Florin Coras98952382021-11-14 15:33:59 -0800208 session_t *s;
Florin Coras6973c732021-06-17 15:53:38 -0700209 int cnt;
Florin Corasda78c5a2021-06-09 14:55:24 -0700210
211 /*
212 * Cleanup fifos
213 */
214
215 sm = segment_manager_get (rx_fifo->segment_manager);
216 seg_index = rx_fifo->segment_index;
217
218 fs = segment_manager_get_segment_w_lock (sm, seg_index);
219 fifo_segment_free_fifo (fs, rx_fifo);
220 fifo_segment_free_fifo (fs, tx_fifo);
221 segment_manager_segment_reader_unlock (sm);
222
223 /*
Florin Coras6973c732021-06-17 15:53:38 -0700224 * Atomically update segment context with readers lock
Florin Corasda78c5a2021-06-09 14:55:24 -0700225 */
226
227 clib_rwlock_reader_lock (&cm->app_segs_lock);
228
229 seg_ctx = pool_elt_at_index (cm->app_seg_ctxs, ct->seg_ctx_index);
230 ct_seg = pool_elt_at_index (seg_ctx->segments, ct->ct_seg_index);
231
232 if (ct->flags & CT_CONN_F_CLIENT)
233 {
234 cnt =
235 __atomic_sub_fetch (&ct_seg->client_n_sessions, 1, __ATOMIC_RELAXED);
Florin Corasda78c5a2021-06-09 14:55:24 -0700236 }
237 else
238 {
239 cnt =
240 __atomic_sub_fetch (&ct_seg->server_n_sessions, 1, __ATOMIC_RELAXED);
Florin Corasda78c5a2021-06-09 14:55:24 -0700241 }
242
Florin Corasda78c5a2021-06-09 14:55:24 -0700243 clib_rwlock_reader_unlock (&cm->app_segs_lock);
244
245 /*
246 * No need to do any app updates, return
247 */
Florin Coras6973c732021-06-17 15:53:38 -0700248 ASSERT (cnt >= 0);
249 if (cnt)
250 return;
251
252 /*
253 * Grab exclusive lock and update flags unless some other thread
254 * added more sessions
255 */
256 clib_rwlock_writer_lock (&cm->app_segs_lock);
257
258 seg_ctx = pool_elt_at_index (cm->app_seg_ctxs, ct->seg_ctx_index);
259 ct_seg = pool_elt_at_index (seg_ctx->segments, ct->ct_seg_index);
260 if (ct->flags & CT_CONN_F_CLIENT)
261 {
262 cnt = ct_seg->client_n_sessions;
Florin Coras98952382021-11-14 15:33:59 -0800263 if (cnt)
264 goto done;
265 ct_seg->flags |= CT_SEGMENT_F_CLIENT_DETACHED;
266 s = session_get (ct->c_s_index, ct->c_thread_index);
267 if (s->app_wrk_index == APP_INVALID_INDEX)
268 ct_set_invalid_app_wrk (ct, 1 /* is_client */);
Florin Coras6973c732021-06-17 15:53:38 -0700269 }
270 else
271 {
272 cnt = ct_seg->server_n_sessions;
Florin Coras98952382021-11-14 15:33:59 -0800273 if (cnt)
274 goto done;
275 ct_seg->flags |= CT_SEGMENT_F_SERVER_DETACHED;
276 s = session_get (ct->c_s_index, ct->c_thread_index);
277 if (s->app_wrk_index == APP_INVALID_INDEX)
278 ct_set_invalid_app_wrk (ct, 0 /* is_client */);
Florin Coras6973c732021-06-17 15:53:38 -0700279 }
280
Florin Coras98952382021-11-14 15:33:59 -0800281 if (!(ct_seg->flags & CT_SEGMENT_F_CLIENT_DETACHED) ||
282 !(ct_seg->flags & CT_SEGMENT_F_SERVER_DETACHED))
283 goto done;
284
Florin Coras6973c732021-06-17 15:53:38 -0700285 /*
286 * Remove segment context because both client and server detached
287 */
288
Florin Coras98952382021-11-14 15:33:59 -0800289 pool_put_index (seg_ctx->segments, ct->ct_seg_index);
Florin Coras6973c732021-06-17 15:53:38 -0700290
291 /*
Florin Coras98952382021-11-14 15:33:59 -0800292 * No more segment indices left, remove the segments context
Florin Coras6973c732021-06-17 15:53:38 -0700293 */
Florin Coras98952382021-11-14 15:33:59 -0800294 if (!pool_elts (seg_ctx->segments))
Florin Corasda78c5a2021-06-09 14:55:24 -0700295 {
Florin Coras98952382021-11-14 15:33:59 -0800296 u64 table_handle = seg_ctx->client_wrk << 16 | seg_ctx->server_wrk;
297 table_handle = (u64) seg_ctx->sm_index << 32 | table_handle;
298 hash_unset (cm->app_segs_ctxs_table, table_handle);
299 pool_free (seg_ctx->segments);
300 pool_put_index (cm->app_seg_ctxs, ct->seg_ctx_index);
Florin Corasda78c5a2021-06-09 14:55:24 -0700301 }
Florin Coras98952382021-11-14 15:33:59 -0800302
303 /*
304 * Segment to be removed so notify both apps
305 */
306
307 app_wrk = app_worker_get_if_valid (ct->client_wrk);
308 /* Determine if client app still needs notification, i.e., if it is
309 * still attached. If client detached and this is the last ct session
310 * on this segment, then its connects segment manager should also be
311 * detached, so do not send notification */
312 if (app_wrk)
313 {
314 segment_manager_t *csm;
315 csm = app_worker_get_connect_segment_manager (app_wrk);
316 if (!segment_manager_app_detached (csm))
317 app_worker_del_segment_notify (app_wrk, ct->segment_handle);
318 }
319
Florin Coras13987da2021-12-07 14:47:12 -0800320 /* Notify server app and free segment */
Florin Corasda78c5a2021-06-09 14:55:24 -0700321 segment_manager_lock_and_del_segment (sm, seg_index);
322
323 /* Cleanup segment manager if needed. If server detaches there's a chance
324 * the client's sessions will hold up segment removal */
325 if (segment_manager_app_detached (sm) && !segment_manager_has_fifos (sm))
326 segment_manager_free_safe (sm);
Florin Coras98952382021-11-14 15:33:59 -0800327
328done:
329
330 clib_rwlock_writer_unlock (&cm->app_segs_lock);
Florin Corasda78c5a2021-06-09 14:55:24 -0700331}
332
Florin Corasa7bd8262021-11-25 12:14:25 -0800333static void
334ct_session_force_disconnect_server (ct_connection_t *sct)
335{
336 sct->peer_index = ~0;
337 session_transport_closing_notify (&sct->connection);
338}
339
Florin Corasda78c5a2021-06-09 14:55:24 -0700340int
Florin Coras6973c732021-06-17 15:53:38 -0700341ct_session_connect_notify (session_t *ss, session_error_t err)
Florin Corasda78c5a2021-06-09 14:55:24 -0700342{
Florin Coras6973c732021-06-17 15:53:38 -0700343 u32 ss_index, opaque, thread_index;
Florin Corasda78c5a2021-06-09 14:55:24 -0700344 ct_connection_t *sct, *cct;
Florin Corasda78c5a2021-06-09 14:55:24 -0700345 app_worker_t *client_wrk;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800346 session_t *cs;
Florin Corasba7d8f52019-02-22 13:11:38 -0800347
Florin Coras2b81e3c2019-02-27 07:55:46 -0800348 ss_index = ss->session_index;
Florin Coras2d0e3de2020-10-23 16:31:40 -0700349 thread_index = ss->thread_index;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800350 sct = (ct_connection_t *) session_get_transport (ss);
351 client_wrk = app_worker_get (sct->client_wrk);
Florin Corasdd7d24b2020-08-14 17:51:13 -0700352 opaque = sct->client_opaque;
Florin Corasba7d8f52019-02-22 13:11:38 -0800353
Florin Coras2d0e3de2020-10-23 16:31:40 -0700354 cct = ct_connection_get (sct->peer_index, thread_index);
Florin Corasba7d8f52019-02-22 13:11:38 -0800355
Florin Coras374df7a2021-05-13 11:37:43 -0700356 /* Client closed while waiting for reply from server */
Florin Coras6973c732021-06-17 15:53:38 -0700357 if (PREDICT_FALSE (!cct))
Florin Coras374df7a2021-05-13 11:37:43 -0700358 {
Florin Corasa7bd8262021-11-25 12:14:25 -0800359 ct_session_force_disconnect_server (sct);
Florin Coras374df7a2021-05-13 11:37:43 -0700360 return 0;
361 }
362
363 session_half_open_delete_notify (&cct->connection);
364 cct->flags &= ~CT_CONN_F_HALF_OPEN;
365
Florin Coras6973c732021-06-17 15:53:38 -0700366 if (PREDICT_FALSE (err))
367 goto connect_error;
Florin Corasda78c5a2021-06-09 14:55:24 -0700368
369 /*
Florin Corasa7bd8262021-11-25 12:14:25 -0800370 * Alloc client session, server session assumed to be established
Florin Corasda78c5a2021-06-09 14:55:24 -0700371 */
372
Florin Corasa7bd8262021-11-25 12:14:25 -0800373 ASSERT (ss->session_state >= SESSION_STATE_READY);
374
Florin Coras2d0e3de2020-10-23 16:31:40 -0700375 cs = session_alloc (thread_index);
376 ss = session_get (ss_index, thread_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800377 cs->session_type = ss->session_type;
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +0200378 cs->listener_handle = SESSION_INVALID_HANDLE;
Steven Luongd810a6e2022-10-25 13:09:11 -0700379 session_set_state (cs, SESSION_STATE_CONNECTING);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800380 cs->app_wrk_index = client_wrk->wrk_index;
381 cs->connection_index = cct->c_c_index;
Florin Coras2ceb8182023-08-31 17:33:47 -0700382 cs->opaque = opaque;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800383 cct->c_s_index = cs->session_index;
384
385 /* This will allocate fifos for the session. They won't be used for
386 * exchanging data but they will be used to close the connection if
387 * the segment manager/worker is freed */
Florin Corasdd7d24b2020-08-14 17:51:13 -0700388 if ((err = app_worker_init_connected (client_wrk, cs)))
Florin Coras2b81e3c2019-02-27 07:55:46 -0800389 {
Florin Corasdd7d24b2020-08-14 17:51:13 -0700390 session_free (cs);
Florin Corasa7bd8262021-11-25 12:14:25 -0800391 ct_session_force_disconnect_server (sct);
Florin Coras6973c732021-06-17 15:53:38 -0700392 err = SESSION_E_ALLOC;
393 goto connect_error;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800394 }
395
Steven Luongd810a6e2022-10-25 13:09:11 -0700396 session_set_state (cs, SESSION_STATE_CONNECTING);
Florin Corasdd7d24b2020-08-14 17:51:13 -0700397
Florin Coras6973c732021-06-17 15:53:38 -0700398 if (app_worker_connect_notify (client_wrk, cs, 0, opaque))
Florin Coras2b81e3c2019-02-27 07:55:46 -0800399 {
Florin Coras6973c732021-06-17 15:53:38 -0700400 segment_manager_dealloc_fifos (cs->rx_fifo, cs->tx_fifo);
Florin Corasdd7d24b2020-08-14 17:51:13 -0700401 session_free (cs);
Florin Corasa7bd8262021-11-25 12:14:25 -0800402 ct_session_force_disconnect_server (sct);
Florin Coras6973c732021-06-17 15:53:38 -0700403 goto cleanup_client;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800404 }
405
Florin Coras2d0e3de2020-10-23 16:31:40 -0700406 cs = session_get (cct->c_s_index, cct->c_thread_index);
Steven Luongd810a6e2022-10-25 13:09:11 -0700407 session_set_state (cs, SESSION_STATE_READY);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800408
Florin Corasba7d8f52019-02-22 13:11:38 -0800409 return 0;
Florin Corasdd7d24b2020-08-14 17:51:13 -0700410
Florin Coras6973c732021-06-17 15:53:38 -0700411connect_error:
412
413 app_worker_connect_notify (client_wrk, 0, err, cct->client_opaque);
414
415cleanup_client:
416
417 if (cct->client_rx_fifo)
418 ct_session_dealloc_fifos (cct, cct->client_rx_fifo, cct->client_tx_fifo);
419 ct_connection_free (cct);
Florin Corasdd7d24b2020-08-14 17:51:13 -0700420 return -1;
Florin Corasba7d8f52019-02-22 13:11:38 -0800421}
422
Florin Coras6973c732021-06-17 15:53:38 -0700423static inline ct_segment_t *
424ct_lookup_free_segment (ct_main_t *cm, segment_manager_t *sm,
425 u32 seg_ctx_index)
Florin Corasba7d8f52019-02-22 13:11:38 -0800426{
Florin Corasda78c5a2021-06-09 14:55:24 -0700427 uword free_bytes, max_free_bytes;
428 ct_segment_t *ct_seg, *res = 0;
Florin Coras6973c732021-06-17 15:53:38 -0700429 ct_segments_ctx_t *seg_ctx;
Florin Corasda78c5a2021-06-09 14:55:24 -0700430 fifo_segment_t *fs;
431 u32 max_fifos;
432
Florin Coras6973c732021-06-17 15:53:38 -0700433 seg_ctx = pool_elt_at_index (cm->app_seg_ctxs, seg_ctx_index);
434 max_free_bytes = seg_ctx->fifo_pair_bytes;
435
Florin Corasda78c5a2021-06-09 14:55:24 -0700436 pool_foreach (ct_seg, seg_ctx->segments)
437 {
438 /* Client or server has detached so segment cannot be used */
Florin Corasda78c5a2021-06-09 14:55:24 -0700439 fs = segment_manager_get_segment (sm, ct_seg->segment_index);
440 free_bytes = fifo_segment_available_bytes (fs);
Florin Coras6973c732021-06-17 15:53:38 -0700441 max_fifos = fifo_segment_size (fs) / seg_ctx->fifo_pair_bytes;
Florin Corasda78c5a2021-06-09 14:55:24 -0700442 if (free_bytes > max_free_bytes &&
443 fifo_segment_num_fifos (fs) / 2 < max_fifos)
444 {
445 max_free_bytes = free_bytes;
446 res = ct_seg;
447 }
448 }
449
450 return res;
451}
452
Florin Coras6973c732021-06-17 15:53:38 -0700453static ct_segment_t *
454ct_alloc_segment (ct_main_t *cm, app_worker_t *server_wrk, u64 table_handle,
455 segment_manager_t *sm, u32 client_wrk_index)
456{
457 u32 seg_ctx_index = ~0, sm_index, pair_bytes;
458 segment_manager_props_t *props;
459 const u32 margin = 16 << 10;
460 ct_segments_ctx_t *seg_ctx;
461 app_worker_t *client_wrk;
462 u64 seg_size, seg_handle;
463 application_t *server;
464 ct_segment_t *ct_seg;
465 uword *spp;
466 int fs_index;
467
468 server = application_get (server_wrk->app_index);
469 props = application_segment_manager_properties (server);
470 sm_index = segment_manager_index (sm);
471 pair_bytes = props->rx_fifo_size + props->tx_fifo_size + margin;
472
473 /*
474 * Make sure another thread did not alloc a segment while acquiring the lock
475 */
476
477 spp = hash_get (cm->app_segs_ctxs_table, table_handle);
478 if (spp)
479 {
480 seg_ctx_index = *spp;
481 ct_seg = ct_lookup_free_segment (cm, sm, seg_ctx_index);
482 if (ct_seg)
483 return ct_seg;
484 }
485
486 /*
487 * No segment, try to alloc one and notify the server and the client.
488 * Make sure the segment is not used for other fifos
489 */
490 seg_size = clib_max (props->segment_size, 128 << 20);
491 fs_index =
492 segment_manager_add_segment2 (sm, seg_size, FIFO_SEGMENT_F_CUSTOM_USE);
493 if (fs_index < 0)
494 return 0;
495
496 if (seg_ctx_index == ~0)
497 {
498 pool_get_zero (cm->app_seg_ctxs, seg_ctx);
499 seg_ctx_index = seg_ctx - cm->app_seg_ctxs;
500 hash_set (cm->app_segs_ctxs_table, table_handle, seg_ctx_index);
501 seg_ctx->server_wrk = server_wrk->wrk_index;
502 seg_ctx->client_wrk = client_wrk_index;
503 seg_ctx->sm_index = sm_index;
504 seg_ctx->fifo_pair_bytes = pair_bytes;
505 }
506 else
507 {
508 seg_ctx = pool_elt_at_index (cm->app_seg_ctxs, seg_ctx_index);
509 }
510
511 pool_get_zero (seg_ctx->segments, ct_seg);
512 ct_seg->segment_index = fs_index;
513 ct_seg->server_n_sessions = 0;
514 ct_seg->client_n_sessions = 0;
515 ct_seg->ct_seg_index = ct_seg - seg_ctx->segments;
516 ct_seg->seg_ctx_index = seg_ctx_index;
517
518 /* New segment, notify the server and client */
519 seg_handle = segment_manager_make_segment_handle (sm_index, fs_index);
520 if (app_worker_add_segment_notify (server_wrk, seg_handle))
521 goto error;
522
523 client_wrk = app_worker_get (client_wrk_index);
524 if (app_worker_add_segment_notify (client_wrk, seg_handle))
525 {
526 app_worker_del_segment_notify (server_wrk, seg_handle);
527 goto error;
528 }
529
530 return ct_seg;
531
532error:
533
534 segment_manager_lock_and_del_segment (sm, fs_index);
535 pool_put_index (seg_ctx->segments, ct_seg->seg_ctx_index);
536 return 0;
537}
538
Florin Corasda78c5a2021-06-09 14:55:24 -0700539static int
540ct_init_accepted_session (app_worker_t *server_wrk, ct_connection_t *ct,
541 session_t *ls, session_t *ll)
542{
Florin Coras88001c62019-04-24 14:44:46 -0700543 segment_manager_props_t *props;
Florin Coras6973c732021-06-17 15:53:38 -0700544 u64 seg_handle, table_handle;
545 u32 sm_index, fs_index = ~0;
Florin Corasda78c5a2021-06-09 14:55:24 -0700546 ct_segments_ctx_t *seg_ctx;
547 ct_main_t *cm = &ct_main;
Florin Coras653e43f2019-03-04 10:56:23 -0800548 application_t *server;
Florin Corasba7d8f52019-02-22 13:11:38 -0800549 segment_manager_t *sm;
Florin Corasda78c5a2021-06-09 14:55:24 -0700550 ct_segment_t *ct_seg;
551 fifo_segment_t *fs;
Florin Corasda78c5a2021-06-09 14:55:24 -0700552 uword *spp;
Florin Coras6973c732021-06-17 15:53:38 -0700553 int rv;
Florin Corasba7d8f52019-02-22 13:11:38 -0800554
Florin Coras2b81e3c2019-02-27 07:55:46 -0800555 sm = app_worker_get_listen_segment_manager (server_wrk, ll);
Florin Corasda78c5a2021-06-09 14:55:24 -0700556 sm_index = segment_manager_index (sm);
557 server = application_get (server_wrk->app_index);
558 props = application_segment_manager_properties (server);
Florin Corasba7d8f52019-02-22 13:11:38 -0800559
Florin Corasda78c5a2021-06-09 14:55:24 -0700560 table_handle = ct->client_wrk << 16 | server_wrk->wrk_index;
Florin Coras6973c732021-06-17 15:53:38 -0700561 table_handle = (u64) sm_index << 32 | table_handle;
Florin Corasda78c5a2021-06-09 14:55:24 -0700562
563 /*
564 * Check if we already have a segment that can hold the fifos
565 */
566
567 clib_rwlock_reader_lock (&cm->app_segs_lock);
568
569 spp = hash_get (cm->app_segs_ctxs_table, table_handle);
570 if (spp)
571 {
Florin Coras6973c732021-06-17 15:53:38 -0700572 ct_seg = ct_lookup_free_segment (cm, sm, *spp);
Florin Corasda78c5a2021-06-09 14:55:24 -0700573 if (ct_seg)
574 {
Florin Coras6973c732021-06-17 15:53:38 -0700575 ct->seg_ctx_index = ct_seg->seg_ctx_index;
576 ct->ct_seg_index = ct_seg->ct_seg_index;
Florin Corasda78c5a2021-06-09 14:55:24 -0700577 fs_index = ct_seg->segment_index;
Florin Coras98952382021-11-14 15:33:59 -0800578 ct_seg->flags &=
579 ~(CT_SEGMENT_F_SERVER_DETACHED | CT_SEGMENT_F_CLIENT_DETACHED);
Florin Corasda78c5a2021-06-09 14:55:24 -0700580 __atomic_add_fetch (&ct_seg->server_n_sessions, 1, __ATOMIC_RELAXED);
Florin Coras6973c732021-06-17 15:53:38 -0700581 __atomic_add_fetch (&ct_seg->client_n_sessions, 1, __ATOMIC_RELAXED);
Florin Corasda78c5a2021-06-09 14:55:24 -0700582 }
583 }
584
585 clib_rwlock_reader_unlock (&cm->app_segs_lock);
586
587 /*
Florin Coras6973c732021-06-17 15:53:38 -0700588 * If not, grab exclusive lock and allocate segment
Florin Corasda78c5a2021-06-09 14:55:24 -0700589 */
Florin Coras6973c732021-06-17 15:53:38 -0700590 if (fs_index == ~0)
Florin Corasda78c5a2021-06-09 14:55:24 -0700591 {
Florin Corasda78c5a2021-06-09 14:55:24 -0700592 clib_rwlock_writer_lock (&cm->app_segs_lock);
593
Florin Coras6973c732021-06-17 15:53:38 -0700594 ct_seg =
595 ct_alloc_segment (cm, server_wrk, table_handle, sm, ct->client_wrk);
596 if (!ct_seg)
Florin Corasda78c5a2021-06-09 14:55:24 -0700597 {
Florin Coras6973c732021-06-17 15:53:38 -0700598 clib_rwlock_writer_unlock (&cm->app_segs_lock);
599 return -1;
Florin Corasda78c5a2021-06-09 14:55:24 -0700600 }
Florin Corasda78c5a2021-06-09 14:55:24 -0700601
Florin Coras6973c732021-06-17 15:53:38 -0700602 ct->seg_ctx_index = ct_seg->seg_ctx_index;
603 ct->ct_seg_index = ct_seg->ct_seg_index;
Florin Corasda78c5a2021-06-09 14:55:24 -0700604 ct_seg->server_n_sessions += 1;
Florin Coras6973c732021-06-17 15:53:38 -0700605 ct_seg->client_n_sessions += 1;
606 fs_index = ct_seg->segment_index;
Florin Corasda78c5a2021-06-09 14:55:24 -0700607
608 clib_rwlock_writer_unlock (&cm->app_segs_lock);
Florin Corasda78c5a2021-06-09 14:55:24 -0700609 }
610
611 /*
612 * Allocate and initialize the fifos
613 */
614 fs = segment_manager_get_segment_w_lock (sm, fs_index);
615 rv = segment_manager_try_alloc_fifos (
616 fs, ls->thread_index, props->rx_fifo_size, props->tx_fifo_size,
617 &ls->rx_fifo, &ls->tx_fifo);
Florin Corasba7d8f52019-02-22 13:11:38 -0800618 if (rv)
619 {
Florin Corasba7d8f52019-02-22 13:11:38 -0800620 segment_manager_segment_reader_unlock (sm);
Florin Coras6973c732021-06-17 15:53:38 -0700621
622 clib_rwlock_reader_lock (&cm->app_segs_lock);
623
624 seg_ctx = pool_elt_at_index (cm->app_seg_ctxs, ct->seg_ctx_index);
625 ct_seg = pool_elt_at_index (seg_ctx->segments, ct->ct_seg_index);
626 __atomic_sub_fetch (&ct_seg->server_n_sessions, 1, __ATOMIC_RELAXED);
627 __atomic_sub_fetch (&ct_seg->client_n_sessions, 1, __ATOMIC_RELAXED);
628
629 clib_rwlock_reader_unlock (&cm->app_segs_lock);
630
631 return rv;
Florin Corasba7d8f52019-02-22 13:11:38 -0800632 }
Florin Coras2b81e3c2019-02-27 07:55:46 -0800633
Florin Corasc547e912020-12-08 17:50:45 -0800634 ls->rx_fifo->shr->master_session_index = ls->session_index;
635 ls->tx_fifo->shr->master_session_index = ls->session_index;
Florin Corasce252ca2020-11-04 13:08:35 -0800636 ls->rx_fifo->master_thread_index = ls->thread_index;
637 ls->tx_fifo->master_thread_index = ls->thread_index;
Florin Corasba7d8f52019-02-22 13:11:38 -0800638
Florin Corasda78c5a2021-06-09 14:55:24 -0700639 seg_handle = segment_manager_segment_handle (sm, fs);
Florin Corasba7d8f52019-02-22 13:11:38 -0800640 segment_manager_segment_reader_unlock (sm);
Florin Corasda78c5a2021-06-09 14:55:24 -0700641
642 ct->segment_handle = seg_handle;
Florin Corasba7d8f52019-02-22 13:11:38 -0800643
644 return 0;
Florin Corasba7d8f52019-02-22 13:11:38 -0800645}
646
Florin Coras2d0e3de2020-10-23 16:31:40 -0700647static void
Florin Corasc215e5a2021-11-15 19:01:52 -0800648ct_accept_one (u32 thread_index, u32 ho_index)
Florin Coras2d0e3de2020-10-23 16:31:40 -0700649{
Florin Corasba026412021-06-12 11:56:19 -0700650 ct_connection_t *sct, *cct, *ho;
651 transport_connection_t *ll_ct;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800652 app_worker_t *server_wrk;
Florin Corasc215e5a2021-11-15 19:01:52 -0800653 u32 cct_index, ll_index;
Florin Coras2d0e3de2020-10-23 16:31:40 -0700654 session_t *ss, *ll;
Florin Corasba7d8f52019-02-22 13:11:38 -0800655
Florin Coras374df7a2021-05-13 11:37:43 -0700656 /*
657 * Alloc client ct and initialize from ho
658 */
Florin Coras374df7a2021-05-13 11:37:43 -0700659 cct = ct_connection_alloc (thread_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800660 cct_index = cct->c_c_index;
Florin Coras374df7a2021-05-13 11:37:43 -0700661
Florin Coras55686e12023-03-20 09:58:01 -0700662 ho = ct_half_open_get (ho_index);
Florin Coras374df7a2021-05-13 11:37:43 -0700663
664 /* Unlikely but half-open session and transport could have been freed */
665 if (PREDICT_FALSE (!ho))
666 {
667 ct_connection_free (cct);
668 return;
669 }
670
671 clib_memcpy (cct, ho, sizeof (*ho));
672 cct->c_c_index = cct_index;
673 cct->c_thread_index = thread_index;
674 cct->flags |= CT_CONN_F_HALF_OPEN;
675
676 /* Notify session layer that half-open is on a different thread
677 * and mark ho connection index reusable. Avoids another rpc
678 */
679 session_half_open_migrate_notify (&cct->connection);
680 session_half_open_migrated_notify (&cct->connection);
681 ct_half_open_add_reusable (ho_index);
Florin Corasba7d8f52019-02-22 13:11:38 -0800682
Florin Coras2b81e3c2019-02-27 07:55:46 -0800683 /*
Florin Coras374df7a2021-05-13 11:37:43 -0700684 * Alloc and init server transport
Florin Coras2b81e3c2019-02-27 07:55:46 -0800685 */
Florin Coras374df7a2021-05-13 11:37:43 -0700686
687 ll_index = cct->peer_index;
688 ll = listen_session_get (ll_index);
689 sct = ct_connection_alloc (thread_index);
Florin Corasba026412021-06-12 11:56:19 -0700690 /* Transport not necessarily ct but it might, so grab after sct alloc */
691 ll_ct = listen_session_get_transport (ll);
Florin Coras374df7a2021-05-13 11:37:43 -0700692
693 /* Make sure cct is valid after sct alloc */
694 cct = ct_connection_get (cct_index, thread_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800695
Florin Coras2b81e3c2019-02-27 07:55:46 -0800696 sct->c_rmt_port = 0;
Florin Corasba026412021-06-12 11:56:19 -0700697 sct->c_lcl_port = ll_ct->lcl_port;
Florin Coras374df7a2021-05-13 11:37:43 -0700698 sct->c_is_ip4 = cct->c_is_ip4;
Florin Corasd9e98702021-10-11 18:10:41 -0700699 clib_memcpy (&sct->c_lcl_ip, &cct->c_rmt_ip, sizeof (cct->c_rmt_ip));
Florin Coras374df7a2021-05-13 11:37:43 -0700700 sct->client_wrk = cct->client_wrk;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800701 sct->c_proto = TRANSPORT_PROTO_NONE;
Florin Coras374df7a2021-05-13 11:37:43 -0700702 sct->client_opaque = cct->client_opaque;
Florin Corasba026412021-06-12 11:56:19 -0700703 sct->actual_tp = cct->actual_tp;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800704
705 sct->peer_index = cct->c_c_index;
706 cct->peer_index = sct->c_c_index;
707
708 /*
709 * Accept server session. Client session is created only after
710 * server confirms accept.
711 */
Florin Coras374df7a2021-05-13 11:37:43 -0700712 ss = session_alloc (thread_index);
713 ll = listen_session_get (ll_index);
Florin Coras9f1a5432019-03-10 21:03:20 -0700714 ss->session_type = session_type_from_proto_and_ip (TRANSPORT_PROTO_NONE,
715 sct->c_is_ip4);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800716 ss->connection_index = sct->c_c_index;
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +0200717 ss->listener_handle = listen_session_get_handle (ll);
Steven Luongd810a6e2022-10-25 13:09:11 -0700718 session_set_state (ss, SESSION_STATE_CREATED);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800719
720 server_wrk = application_listener_select_worker (ll);
721 ss->app_wrk_index = server_wrk->wrk_index;
722
723 sct->c_s_index = ss->session_index;
724 sct->server_wrk = ss->app_wrk_index;
725
Florin Coras2d0e3de2020-10-23 16:31:40 -0700726 if (ct_init_accepted_session (server_wrk, sct, ss, ll))
Florin Corasba7d8f52019-02-22 13:11:38 -0800727 {
Florin Coras6973c732021-06-17 15:53:38 -0700728 ct_session_connect_notify (ss, SESSION_E_ALLOC);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800729 ct_connection_free (sct);
730 session_free (ss);
Florin Coras2d0e3de2020-10-23 16:31:40 -0700731 return;
Florin Corasba7d8f52019-02-22 13:11:38 -0800732 }
733
Florin Coras98952382021-11-14 15:33:59 -0800734 cct->server_wrk = sct->server_wrk;
Florin Coras6973c732021-06-17 15:53:38 -0700735 cct->seg_ctx_index = sct->seg_ctx_index;
736 cct->ct_seg_index = sct->ct_seg_index;
737 cct->client_rx_fifo = ss->tx_fifo;
738 cct->client_tx_fifo = ss->rx_fifo;
739 cct->client_rx_fifo->refcnt++;
740 cct->client_tx_fifo->refcnt++;
741 cct->segment_handle = sct->segment_handle;
742
Steven Luongd810a6e2022-10-25 13:09:11 -0700743 session_set_state (ss, SESSION_STATE_ACCEPTING);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800744 if (app_worker_accept_notify (server_wrk, ss))
745 {
Florin Coras6973c732021-06-17 15:53:38 -0700746 ct_session_connect_notify (ss, SESSION_E_REFUSED);
Florin Corasda78c5a2021-06-09 14:55:24 -0700747 ct_session_dealloc_fifos (sct, ss->rx_fifo, ss->tx_fifo);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800748 ct_connection_free (sct);
Florin Coras12813d52020-04-09 20:59:20 +0000749 session_free (ss);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800750 }
Florin Coras2d0e3de2020-10-23 16:31:40 -0700751}
752
Florin Corasc215e5a2021-11-15 19:01:52 -0800753static void
754ct_accept_rpc_wrk_handler (void *rpc_args)
755{
Florin Coras55686e12023-03-20 09:58:01 -0700756 u32 thread_index, n_connects, i, n_pending;
Florin Corasc215e5a2021-11-15 19:01:52 -0800757 const u32 max_connects = 32;
758 ct_worker_t *wrk;
Florin Coras55686e12023-03-20 09:58:01 -0700759 u8 need_rpc = 0;
Florin Corasc215e5a2021-11-15 19:01:52 -0800760
761 thread_index = pointer_to_uword (rpc_args);
762 wrk = ct_worker_get (thread_index);
763
Florin Coras55686e12023-03-20 09:58:01 -0700764 /* Connects could be handled without worker barrier so grab lock */
765 clib_spinlock_lock (&wrk->pending_connects_lock);
766
Florin Corasc215e5a2021-11-15 19:01:52 -0800767 n_pending = clib_fifo_elts (wrk->pending_connects);
768 n_connects = clib_min (n_pending, max_connects);
Florin Coras55686e12023-03-20 09:58:01 -0700769 vec_validate (wrk->new_connects, n_connects);
Florin Corasc215e5a2021-11-15 19:01:52 -0800770
771 for (i = 0; i < n_connects; i++)
Florin Coras55686e12023-03-20 09:58:01 -0700772 clib_fifo_sub1 (wrk->pending_connects, wrk->new_connects[i]);
Florin Corasc215e5a2021-11-15 19:01:52 -0800773
774 if (n_pending == n_connects)
775 wrk->have_connects = 0;
776 else
Florin Coras55686e12023-03-20 09:58:01 -0700777 need_rpc = 1;
778
779 clib_spinlock_unlock (&wrk->pending_connects_lock);
780
781 for (i = 0; i < n_connects; i++)
782 ct_accept_one (thread_index, wrk->new_connects[i]);
783
784 if (need_rpc)
Florin Corasc215e5a2021-11-15 19:01:52 -0800785 session_send_rpc_evt_to_thread_force (
786 thread_index, ct_accept_rpc_wrk_handler,
787 uword_to_pointer (thread_index, void *));
788}
789
Florin Coras55686e12023-03-20 09:58:01 -0700790static void
791ct_fwrk_flush_connects (void *rpc_args)
Florin Coras2d0e3de2020-10-23 16:31:40 -0700792{
Florin Coras55686e12023-03-20 09:58:01 -0700793 u32 thread_index, fwrk_index, n_workers;
Florin Coras2d0e3de2020-10-23 16:31:40 -0700794 ct_main_t *cm = &ct_main;
Florin Corasc215e5a2021-11-15 19:01:52 -0800795 ct_worker_t *wrk;
Florin Coras55686e12023-03-20 09:58:01 -0700796 u8 need_rpc;
797
Florin Coras1315d142023-04-04 11:51:37 -0700798 fwrk_index = cm->fwrk_thread;
Florin Coras55686e12023-03-20 09:58:01 -0700799 n_workers = vec_len (cm->fwrk_pending_connects);
800
801 for (thread_index = fwrk_index; thread_index < n_workers; thread_index++)
802 {
Florin Coras1315d142023-04-04 11:51:37 -0700803 if (!vec_len (cm->fwrk_pending_connects[thread_index]))
804 continue;
805
Florin Coras55686e12023-03-20 09:58:01 -0700806 wrk = ct_worker_get (thread_index);
807
808 /* Connects can be done without worker barrier, grab dst worker lock */
809 if (thread_index != fwrk_index)
810 clib_spinlock_lock (&wrk->pending_connects_lock);
811
812 clib_fifo_add (wrk->pending_connects,
813 cm->fwrk_pending_connects[thread_index],
814 vec_len (cm->fwrk_pending_connects[thread_index]));
815 if (!wrk->have_connects)
816 {
817 wrk->have_connects = 1;
818 need_rpc = 1;
819 }
820
821 if (thread_index != fwrk_index)
822 clib_spinlock_unlock (&wrk->pending_connects_lock);
823
824 vec_reset_length (cm->fwrk_pending_connects[thread_index]);
825
826 if (need_rpc)
827 session_send_rpc_evt_to_thread_force (
828 thread_index, ct_accept_rpc_wrk_handler,
829 uword_to_pointer (thread_index, void *));
830 }
831
832 cm->fwrk_have_flush = 0;
833}
834
835static void
836ct_program_connect_to_wrk (u32 ho_index)
837{
838 ct_main_t *cm = &ct_main;
839 u32 thread_index;
Florin Coras2d0e3de2020-10-23 16:31:40 -0700840
841 /* Simple round-robin policy for spreading sessions over workers. We skip
842 * thread index 0, i.e., offset the index by 1, when we have workers as it
843 * is the one dedicated to main thread. Note that n_workers does not include
844 * main thread */
845 cm->n_sessions += 1;
846 thread_index = cm->n_workers ? (cm->n_sessions % cm->n_workers) + 1 : 0;
847
Florin Coras55686e12023-03-20 09:58:01 -0700848 /* Pospone flushing of connect request to dst worker until after session
849 * layer fully initializes the half-open session. */
850 vec_add1 (cm->fwrk_pending_connects[thread_index], ho_index);
851 if (!cm->fwrk_have_flush)
852 {
853 session_send_rpc_evt_to_thread_force (
854 cm->fwrk_thread, ct_fwrk_flush_connects,
855 uword_to_pointer (thread_index, void *));
856 cm->fwrk_have_flush = 1;
857 }
858}
859
860static int
861ct_connect (app_worker_t *client_wrk, session_t *ll,
862 session_endpoint_cfg_t *sep)
863{
864 ct_connection_t *ho;
865 u32 ho_index;
866
Florin Coras374df7a2021-05-13 11:37:43 -0700867 /*
868 * Alloc and init client half-open transport
869 */
Florin Coras2d0e3de2020-10-23 16:31:40 -0700870
Florin Coras374df7a2021-05-13 11:37:43 -0700871 ho = ct_half_open_alloc ();
872 ho_index = ho->c_c_index;
873 ho->c_rmt_port = sep->port;
874 ho->c_lcl_port = 0;
875 ho->c_is_ip4 = sep->is_ip4;
876 ho->client_opaque = sep->opaque;
877 ho->client_wrk = client_wrk->wrk_index;
878 ho->peer_index = ll->session_index;
879 ho->c_proto = TRANSPORT_PROTO_NONE;
880 ho->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
Florin Corasd5a58822021-05-14 20:11:14 -0700881 clib_memcpy (&ho->c_rmt_ip, &sep->ip, sizeof (sep->ip));
Florin Coras374df7a2021-05-13 11:37:43 -0700882 ho->flags |= CT_CONN_F_CLIENT;
883 ho->c_s_index = ~0;
Florin Corasfeb67022021-11-11 09:24:34 -0800884 ho->actual_tp = sep->original_tp;
Florin Coras374df7a2021-05-13 11:37:43 -0700885
886 /*
Florin Coras55686e12023-03-20 09:58:01 -0700887 * Program connect on a worker, connected reply comes
Florin Coras374df7a2021-05-13 11:37:43 -0700888 * after server accepts the connection.
889 */
Florin Coras55686e12023-03-20 09:58:01 -0700890 ct_program_connect_to_wrk (ho_index);
Florin Coras374df7a2021-05-13 11:37:43 -0700891
892 return ho_index;
Florin Corasba7d8f52019-02-22 13:11:38 -0800893}
894
Florin Coras653e43f2019-03-04 10:56:23 -0800895static u32
Florin Coras0bce71e2022-02-10 11:57:06 -0800896ct_start_listen (u32 app_listener_index, transport_endpoint_cfg_t *tep)
Florin Corasd4295e62019-02-22 13:11:38 -0800897{
898 session_endpoint_cfg_t *sep;
899 ct_connection_t *ct;
900
901 sep = (session_endpoint_cfg_t *) tep;
Florin Coras2d0e3de2020-10-23 16:31:40 -0700902 ct = ct_connection_alloc (0);
Florin Corasd4295e62019-02-22 13:11:38 -0800903 ct->server_wrk = sep->app_wrk_index;
904 ct->c_is_ip4 = sep->is_ip4;
905 clib_memcpy (&ct->c_lcl_ip, &sep->ip, sizeof (sep->ip));
906 ct->c_lcl_port = sep->port;
Florin Corasa8375082020-10-25 19:06:57 -0700907 ct->c_s_index = app_listener_index;
Florin Corasd4295e62019-02-22 13:11:38 -0800908 ct->actual_tp = sep->transport_proto;
909 return ct->c_c_index;
910}
911
Florin Coras653e43f2019-03-04 10:56:23 -0800912static u32
Florin Corasd4295e62019-02-22 13:11:38 -0800913ct_stop_listen (u32 ct_index)
914{
915 ct_connection_t *ct;
Florin Coras2d0e3de2020-10-23 16:31:40 -0700916 ct = ct_connection_get (ct_index, 0);
Florin Corasd4295e62019-02-22 13:11:38 -0800917 ct_connection_free (ct);
918 return 0;
919}
920
Florin Coras653e43f2019-03-04 10:56:23 -0800921static transport_connection_t *
Florin Corasd4295e62019-02-22 13:11:38 -0800922ct_listener_get (u32 ct_index)
923{
Florin Coras2d0e3de2020-10-23 16:31:40 -0700924 return (transport_connection_t *) ct_connection_get (ct_index, 0);
Florin Corasd4295e62019-02-22 13:11:38 -0800925}
926
Florin Coras374df7a2021-05-13 11:37:43 -0700927static transport_connection_t *
Florin Coras55686e12023-03-20 09:58:01 -0700928ct_session_half_open_get (u32 ct_index)
Florin Coras374df7a2021-05-13 11:37:43 -0700929{
Florin Coras55686e12023-03-20 09:58:01 -0700930 return (transport_connection_t *) ct_half_open_get (ct_index);
Florin Coras374df7a2021-05-13 11:37:43 -0700931}
932
933static void
934ct_session_cleanup (u32 conn_index, u32 thread_index)
935{
936 ct_connection_t *ct, *peer_ct;
937
938 ct = ct_connection_get (conn_index, thread_index);
939 if (!ct)
940 return;
941
942 peer_ct = ct_connection_get (ct->peer_index, thread_index);
943 if (peer_ct)
944 peer_ct->peer_index = ~0;
945
946 ct_connection_free (ct);
947}
948
949static void
950ct_cleanup_ho (u32 ho_index)
951{
Florin Coras55686e12023-03-20 09:58:01 -0700952 ct_connection_t *ho;
953
954 ho = ct_half_open_get (ho_index);
955 ct_connection_free (ho);
Florin Coras374df7a2021-05-13 11:37:43 -0700956}
957
Florin Coras653e43f2019-03-04 10:56:23 -0800958static int
Florin Coras2b81e3c2019-02-27 07:55:46 -0800959ct_session_connect (transport_endpoint_cfg_t * tep)
960{
961 session_endpoint_cfg_t *sep_ext;
Florin Coras374df7a2021-05-13 11:37:43 -0700962 session_endpoint_t _sep, *sep = &_sep;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800963 app_worker_t *app_wrk;
964 session_handle_t lh;
965 application_t *app;
966 app_listener_t *al;
967 u32 table_index;
968 session_t *ll;
969 u8 fib_proto;
970
971 sep_ext = (session_endpoint_cfg_t *) tep;
Florin Coras374df7a2021-05-13 11:37:43 -0700972 _sep = *(session_endpoint_t *) tep;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800973 app_wrk = app_worker_get (sep_ext->app_wrk_index);
974 app = application_get (app_wrk->app_index);
975
976 sep->transport_proto = sep_ext->original_tp;
977 table_index = application_local_session_table (app);
978 lh = session_lookup_local_endpoint (table_index, sep);
979 if (lh == SESSION_DROP_HANDLE)
Florin Coras00e01d32019-10-21 16:07:46 -0700980 return SESSION_E_FILTERED;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800981
982 if (lh == SESSION_INVALID_HANDLE)
983 goto global_scope;
984
985 ll = listen_session_get_from_handle (lh);
986 al = app_listener_get_w_session (ll);
987
988 /*
989 * Break loop if rule in local table points to connecting app. This
990 * can happen if client is a generic proxy. Route connect through
991 * global table instead.
992 */
993 if (al->app_index == app->app_index)
994 goto global_scope;
995
996 return ct_connect (app_wrk, ll, sep_ext);
997
998 /*
999 * If nothing found, check the global scope for locally attached
1000 * destinations. Make sure first that we're allowed to.
1001 */
1002
1003global_scope:
1004 if (session_endpoint_is_local (sep))
Florin Coras00e01d32019-10-21 16:07:46 -07001005 return SESSION_E_NOROUTE;
Florin Coras2b81e3c2019-02-27 07:55:46 -08001006
1007 if (!application_has_global_scope (app))
Florin Coras00e01d32019-10-21 16:07:46 -07001008 return SESSION_E_SCOPE;
Florin Coras2b81e3c2019-02-27 07:55:46 -08001009
1010 fib_proto = session_endpoint_fib_proto (sep);
Florin Coras95cd8642019-12-30 21:53:19 -08001011 table_index = session_lookup_get_index_for_fib (fib_proto, sep->fib_index);
Florin Coras9f1a5432019-03-10 21:03:20 -07001012 ll = session_lookup_listener_wildcard (table_index, sep);
Florin Coras2b81e3c2019-02-27 07:55:46 -08001013
Florin Coras821b5002021-06-02 21:32:39 -07001014 /* Avoid connecting app to own listener */
1015 if (ll && ll->app_index != app->app_index)
Florin Coras2b81e3c2019-02-27 07:55:46 -08001016 return ct_connect (app_wrk, ll, sep_ext);
1017
1018 /* Failed to connect but no error */
Florin Coras374df7a2021-05-13 11:37:43 -07001019 return SESSION_E_LOCAL_CONNECT;
Florin Coras2b81e3c2019-02-27 07:55:46 -08001020}
1021
Florin Coras9439a362021-11-25 16:18:39 -08001022static inline int
1023ct_close_is_reset (ct_connection_t *ct, session_t *s)
1024{
1025 if (ct->flags & CT_CONN_F_CLIENT)
1026 return (svm_fifo_max_dequeue (ct->client_rx_fifo) > 0);
1027 else
1028 return (svm_fifo_max_dequeue (s->rx_fifo) > 0);
1029}
1030
Florin Coras653e43f2019-03-04 10:56:23 -08001031static void
Florin Coras0242d302022-12-22 15:03:44 -08001032ct_session_cleanup_server_session (session_t *s)
1033{
1034 ct_connection_t *ct;
1035
1036 ct = (ct_connection_t *) session_get_transport (s);
1037 ct_session_dealloc_fifos (ct, s->rx_fifo, s->tx_fifo);
1038 session_free (s);
1039 ct_connection_free (ct);
1040}
1041
1042static void
Florin Coras440c7b52021-11-09 08:38:24 -08001043ct_session_postponed_cleanup (ct_connection_t *ct)
1044{
Florin Coras9439a362021-11-25 16:18:39 -08001045 ct_connection_t *peer_ct;
Florin Coras440c7b52021-11-09 08:38:24 -08001046 app_worker_t *app_wrk;
1047 session_t *s;
1048
1049 s = session_get (ct->c_s_index, ct->c_thread_index);
1050 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
1051
Florin Coras9439a362021-11-25 16:18:39 -08001052 peer_ct = ct_connection_get (ct->peer_index, ct->c_thread_index);
1053 if (peer_ct)
1054 {
1055 if (ct_close_is_reset (ct, s))
1056 session_transport_reset_notify (&peer_ct->connection);
1057 else
1058 session_transport_closing_notify (&peer_ct->connection);
1059 }
1060 session_transport_closed_notify (&ct->connection);
1061
Florin Coras0242d302022-12-22 15:03:44 -08001062 /* It would be cleaner to call session_transport_delete_notify
1063 * but then we can't control session cleanup lower */
1064 session_set_state (s, SESSION_STATE_TRANSPORT_DELETED);
1065 if (app_wrk)
1066 app_worker_cleanup_notify (app_wrk, s, SESSION_CLEANUP_TRANSPORT);
1067
Florin Coras440c7b52021-11-09 08:38:24 -08001068 if (ct->flags & CT_CONN_F_CLIENT)
1069 {
Florin Coras440c7b52021-11-09 08:38:24 -08001070 /* Normal free for client session as the fifos are allocated through
1071 * the connects segment manager in a segment that's not shared with
1072 * the server */
Florin Coras440c7b52021-11-09 08:38:24 -08001073 ct_session_dealloc_fifos (ct, ct->client_rx_fifo, ct->client_tx_fifo);
Florin Coras0242d302022-12-22 15:03:44 -08001074 session_program_cleanup (s);
1075 ct_connection_free (ct);
Florin Coras440c7b52021-11-09 08:38:24 -08001076 }
1077 else
1078 {
1079 /* Manual session and fifo segment cleanup to avoid implicit
1080 * segment manager cleanups and notifications */
Florin Coras440c7b52021-11-09 08:38:24 -08001081 if (app_wrk)
1082 {
Florin Coras0242d302022-12-22 15:03:44 -08001083 /* Remove custom cleanup notify infra when/if switching to normal
1084 * session cleanup. Note that ct is freed in the cb function */
1085 app_worker_cleanup_notify_custom (app_wrk, s,
1086 SESSION_CLEANUP_SESSION,
1087 ct_session_cleanup_server_session);
Florin Coras440c7b52021-11-09 08:38:24 -08001088 }
Florin Coras0242d302022-12-22 15:03:44 -08001089 else
1090 {
1091 ct_connection_free (ct);
1092 }
Florin Coras440c7b52021-11-09 08:38:24 -08001093 }
Florin Coras440c7b52021-11-09 08:38:24 -08001094}
1095
1096static void
1097ct_handle_cleanups (void *args)
1098{
1099 uword thread_index = pointer_to_uword (args);
1100 const u32 max_cleanups = 100;
Florin Coras440c7b52021-11-09 08:38:24 -08001101 ct_cleanup_req_t *req;
1102 ct_connection_t *ct;
1103 u32 n_to_handle = 0;
Florin Coras7fe00692021-11-15 14:01:02 -08001104 ct_worker_t *wrk;
Florin Coras440c7b52021-11-09 08:38:24 -08001105 session_t *s;
1106
Florin Coras7fe00692021-11-15 14:01:02 -08001107 wrk = ct_worker_get (thread_index);
1108 wrk->have_cleanups = 0;
1109 n_to_handle = clib_fifo_elts (wrk->pending_cleanups);
Florin Coras440c7b52021-11-09 08:38:24 -08001110 n_to_handle = clib_min (n_to_handle, max_cleanups);
1111
1112 while (n_to_handle)
1113 {
Florin Coras7fe00692021-11-15 14:01:02 -08001114 clib_fifo_sub2 (wrk->pending_cleanups, req);
Florin Coras440c7b52021-11-09 08:38:24 -08001115 ct = ct_connection_get (req->ct_index, thread_index);
1116 s = session_get (ct->c_s_index, ct->c_thread_index);
1117 if (!svm_fifo_has_event (s->tx_fifo))
1118 ct_session_postponed_cleanup (ct);
1119 else
Florin Coras7fe00692021-11-15 14:01:02 -08001120 clib_fifo_add1 (wrk->pending_cleanups, *req);
Florin Coras440c7b52021-11-09 08:38:24 -08001121 n_to_handle -= 1;
1122 }
1123
Florin Coras7fe00692021-11-15 14:01:02 -08001124 if (clib_fifo_elts (wrk->pending_cleanups))
Florin Coras440c7b52021-11-09 08:38:24 -08001125 {
Florin Coras7fe00692021-11-15 14:01:02 -08001126 wrk->have_cleanups = 1;
Florin Coras440c7b52021-11-09 08:38:24 -08001127 session_send_rpc_evt_to_thread_force (
1128 thread_index, ct_handle_cleanups,
1129 uword_to_pointer (thread_index, void *));
1130 }
1131}
1132
1133static void
1134ct_program_cleanup (ct_connection_t *ct)
1135{
Florin Coras440c7b52021-11-09 08:38:24 -08001136 ct_cleanup_req_t *req;
1137 uword thread_index;
Florin Coras7fe00692021-11-15 14:01:02 -08001138 ct_worker_t *wrk;
Florin Coras440c7b52021-11-09 08:38:24 -08001139
1140 thread_index = ct->c_thread_index;
Florin Coras7fe00692021-11-15 14:01:02 -08001141 wrk = ct_worker_get (ct->c_thread_index);
1142
1143 clib_fifo_add2 (wrk->pending_cleanups, req);
Florin Coras440c7b52021-11-09 08:38:24 -08001144 req->ct_index = ct->c_c_index;
1145
Florin Coras7fe00692021-11-15 14:01:02 -08001146 if (wrk->have_cleanups)
Florin Coras440c7b52021-11-09 08:38:24 -08001147 return;
1148
Florin Coras7fe00692021-11-15 14:01:02 -08001149 wrk->have_cleanups = 1;
Florin Coras440c7b52021-11-09 08:38:24 -08001150 session_send_rpc_evt_to_thread_force (
1151 thread_index, ct_handle_cleanups, uword_to_pointer (thread_index, void *));
1152}
1153
1154static void
Florin Coras2b81e3c2019-02-27 07:55:46 -08001155ct_session_close (u32 ct_index, u32 thread_index)
1156{
1157 ct_connection_t *ct, *peer_ct;
1158 session_t *s;
1159
Florin Coras2d0e3de2020-10-23 16:31:40 -07001160 ct = ct_connection_get (ct_index, thread_index);
Florin Coras6973c732021-06-17 15:53:38 -07001161 s = session_get (ct->c_s_index, ct->c_thread_index);
Florin Coras2d0e3de2020-10-23 16:31:40 -07001162 peer_ct = ct_connection_get (ct->peer_index, thread_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -08001163 if (peer_ct)
1164 {
1165 peer_ct->peer_index = ~0;
Florin Coras06ac4bb2020-10-28 16:41:26 -07001166 /* Make sure session was allocated */
Florin Coras374df7a2021-05-13 11:37:43 -07001167 if (peer_ct->flags & CT_CONN_F_HALF_OPEN)
1168 {
Florin Coras6973c732021-06-17 15:53:38 -07001169 ct_session_connect_notify (s, SESSION_E_REFUSED);
Florin Corasa7bd8262021-11-25 12:14:25 -08001170 ct->peer_index = ~0;
Florin Coras374df7a2021-05-13 11:37:43 -07001171 }
Florin Coras9439a362021-11-25 16:18:39 -08001172 else if (peer_ct->c_s_index == ~0)
Florin Coras440c7b52021-11-09 08:38:24 -08001173 {
1174 /* should not happen */
1175 clib_warning ("ct peer without session");
1176 ct_connection_free (peer_ct);
1177 }
Florin Coras2b81e3c2019-02-27 07:55:46 -08001178 }
1179
Florin Coras440c7b52021-11-09 08:38:24 -08001180 /* Do not send closed notify to make sure pending tx events are
1181 * still delivered and program cleanup */
1182 ct_program_cleanup (ct);
Florin Coras2b81e3c2019-02-27 07:55:46 -08001183}
1184
Florin Coras653e43f2019-03-04 10:56:23 -08001185static transport_connection_t *
Florin Coras2b81e3c2019-02-27 07:55:46 -08001186ct_session_get (u32 ct_index, u32 thread_index)
1187{
Florin Coras2d0e3de2020-10-23 16:31:40 -07001188 return (transport_connection_t *) ct_connection_get (ct_index,
1189 thread_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -08001190}
1191
Florin Corasd4295e62019-02-22 13:11:38 -08001192static u8 *
1193format_ct_connection_id (u8 * s, va_list * args)
1194{
1195 ct_connection_t *ct = va_arg (*args, ct_connection_t *);
1196 if (!ct)
1197 return s;
1198 if (ct->c_is_ip4)
1199 {
1200 s = format (s, "[%d:%d][CT:%U] %U:%d->%U:%d", ct->c_thread_index,
1201 ct->c_s_index, format_transport_proto_short, ct->actual_tp,
1202 format_ip4_address, &ct->c_lcl_ip4,
1203 clib_net_to_host_u16 (ct->c_lcl_port), format_ip4_address,
1204 &ct->c_rmt_ip4, clib_net_to_host_u16 (ct->c_rmt_port));
1205 }
1206 else
1207 {
1208 s = format (s, "[%d:%d][CT:%U] %U:%d->%U:%d", ct->c_thread_index,
1209 ct->c_s_index, format_transport_proto_short, ct->actual_tp,
1210 format_ip6_address, &ct->c_lcl_ip6,
1211 clib_net_to_host_u16 (ct->c_lcl_port), format_ip6_address,
1212 &ct->c_rmt_ip6, clib_net_to_host_u16 (ct->c_rmt_port));
1213 }
1214
1215 return s;
1216}
1217
Florin Corasf940f8a2019-03-06 10:44:38 -08001218static int
Florin Coras9f86d222020-03-23 15:34:22 +00001219ct_custom_tx (void *session, transport_send_params_t * sp)
Florin Corasf940f8a2019-03-06 10:44:38 -08001220{
1221 session_t *s = (session_t *) session;
Florin Coras074f3972021-12-07 15:12:06 -08001222 if (session_has_transport (s))
Florin Corasf940f8a2019-03-06 10:44:38 -08001223 return 0;
Florin Coras1b9d2c22021-01-26 14:10:43 -08001224 /* If event enqueued towards peer, remove from scheduler and remove
1225 * session tx flag, i.e., accept new tx events. Unset fifo flag now to
1226 * avoid missing events if peer did not clear fifo flag yet, which is
1227 * interpreted as successful notification and session is descheduled. */
1228 svm_fifo_unset_event (s->tx_fifo);
Florin Corasd6894562020-10-28 00:37:15 -07001229 if (!ct_session_tx (s))
Florin Coras1b9d2c22021-01-26 14:10:43 -08001230 sp->flags = TRANSPORT_SND_F_DESCHED;
1231
Florin Corasd6894562020-10-28 00:37:15 -07001232 /* The scheduler uses packet count as a means of upper bounding the amount
1233 * of work done per dispatch. So make it look like we have sent something */
1234 return 1;
Florin Corasf940f8a2019-03-06 10:44:38 -08001235}
1236
Florin Coras317b8e02019-04-17 09:57:46 -07001237static int
1238ct_app_rx_evt (transport_connection_t * tc)
1239{
1240 ct_connection_t *ct = (ct_connection_t *) tc, *peer_ct;
Florin Coras9439a362021-11-25 16:18:39 -08001241 session_t *ps, *s;
Florin Coras317b8e02019-04-17 09:57:46 -07001242
Florin Coras9439a362021-11-25 16:18:39 -08001243 s = session_get (ct->c_s_index, ct->c_thread_index);
1244 if (session_has_transport (s) || s->session_state < SESSION_STATE_READY)
1245 return -1;
Florin Coras2d0e3de2020-10-23 16:31:40 -07001246 peer_ct = ct_connection_get (ct->peer_index, tc->thread_index);
Florin Coras9439a362021-11-25 16:18:39 -08001247 if (!peer_ct || (peer_ct->flags & CT_CONN_F_HALF_OPEN))
Florin Coras317b8e02019-04-17 09:57:46 -07001248 return -1;
1249 ps = session_get (peer_ct->c_s_index, peer_ct->c_thread_index);
Florin Coras9439a362021-11-25 16:18:39 -08001250 if (ps->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
1251 return -1;
Florin Coras317b8e02019-04-17 09:57:46 -07001252 return session_dequeue_notify (ps);
1253}
1254
Florin Coras653e43f2019-03-04 10:56:23 -08001255static u8 *
Florin Corasd4295e62019-02-22 13:11:38 -08001256format_ct_listener (u8 * s, va_list * args)
1257{
1258 u32 tc_index = va_arg (*args, u32);
Aloys Augustina0abbff2019-07-12 12:16:16 +02001259 u32 __clib_unused thread_index = va_arg (*args, u32);
Florin Corasd4295e62019-02-22 13:11:38 -08001260 u32 __clib_unused verbose = va_arg (*args, u32);
Florin Coras2d0e3de2020-10-23 16:31:40 -07001261 ct_connection_t *ct = ct_connection_get (tc_index, 0);
Florin Coras7bf6ed62020-09-23 12:02:08 -07001262 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_ct_connection_id, ct);
Florin Corasd4295e62019-02-22 13:11:38 -08001263 if (verbose)
Florin Coras7bf6ed62020-09-23 12:02:08 -07001264 s = format (s, "%-" SESSION_CLI_STATE_LEN "s", "LISTEN");
Florin Corasd4295e62019-02-22 13:11:38 -08001265 return s;
1266}
1267
Florin Coras653e43f2019-03-04 10:56:23 -08001268static u8 *
Florin Coras374df7a2021-05-13 11:37:43 -07001269format_ct_half_open (u8 *s, va_list *args)
1270{
1271 u32 ho_index = va_arg (*args, u32);
1272 u32 verbose = va_arg (*args, u32);
Florin Coras55686e12023-03-20 09:58:01 -07001273 ct_connection_t *ct = ct_half_open_get (ho_index);
Florin Coras374df7a2021-05-13 11:37:43 -07001274 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_ct_connection_id, ct);
1275 if (verbose)
1276 s = format (s, "%-" SESSION_CLI_STATE_LEN "s", "HALF-OPEN");
1277 return s;
1278}
1279
1280static u8 *
Florin Coras2b81e3c2019-02-27 07:55:46 -08001281format_ct_connection (u8 * s, va_list * args)
1282{
1283 ct_connection_t *ct = va_arg (*args, ct_connection_t *);
1284 u32 verbose = va_arg (*args, u32);
1285
1286 if (!ct)
1287 return s;
Florin Coras7bf6ed62020-09-23 12:02:08 -07001288 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_ct_connection_id, ct);
Florin Coras2b81e3c2019-02-27 07:55:46 -08001289 if (verbose)
1290 {
Florin Coras7bf6ed62020-09-23 12:02:08 -07001291 s = format (s, "%-" SESSION_CLI_STATE_LEN "s", "ESTABLISHED");
Florin Coras2b81e3c2019-02-27 07:55:46 -08001292 if (verbose > 1)
1293 {
1294 s = format (s, "\n");
1295 }
1296 }
1297 return s;
1298}
1299
Florin Coras653e43f2019-03-04 10:56:23 -08001300static u8 *
Florin Coras2b81e3c2019-02-27 07:55:46 -08001301format_ct_session (u8 * s, va_list * args)
1302{
1303 u32 ct_index = va_arg (*args, u32);
Florin Coras2d0e3de2020-10-23 16:31:40 -07001304 u32 thread_index = va_arg (*args, u32);
Florin Coras2b81e3c2019-02-27 07:55:46 -08001305 u32 verbose = va_arg (*args, u32);
1306 ct_connection_t *ct;
1307
Florin Coras2d0e3de2020-10-23 16:31:40 -07001308 ct = ct_connection_get (ct_index, thread_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -08001309 if (!ct)
1310 {
1311 s = format (s, "empty\n");
1312 return s;
1313 }
1314
1315 s = format (s, "%U", format_ct_connection, ct, verbose);
1316 return s;
1317}
1318
Florin Coras2d0e3de2020-10-23 16:31:40 -07001319clib_error_t *
1320ct_enable_disable (vlib_main_t * vm, u8 is_en)
1321{
Florin Coras7fe00692021-11-15 14:01:02 -08001322 vlib_thread_main_t *vtm = &vlib_thread_main;
Florin Coras374df7a2021-05-13 11:37:43 -07001323 ct_main_t *cm = &ct_main;
Florin Coras55686e12023-03-20 09:58:01 -07001324 ct_worker_t *wrk;
Florin Coras374df7a2021-05-13 11:37:43 -07001325
1326 cm->n_workers = vlib_num_workers ();
Florin Coras55686e12023-03-20 09:58:01 -07001327 cm->fwrk_thread = transport_cl_thread ();
Florin Coras7fe00692021-11-15 14:01:02 -08001328 vec_validate (cm->wrk, vtm->n_vlib_mains);
Florin Coras55686e12023-03-20 09:58:01 -07001329 vec_foreach (wrk, cm->wrk)
1330 clib_spinlock_init (&wrk->pending_connects_lock);
Florin Coras374df7a2021-05-13 11:37:43 -07001331 clib_spinlock_init (&cm->ho_reuseable_lock);
Florin Corasda78c5a2021-06-09 14:55:24 -07001332 clib_rwlock_init (&cm->app_segs_lock);
Florin Coras55686e12023-03-20 09:58:01 -07001333 vec_validate (cm->fwrk_pending_connects, cm->n_workers);
Florin Coras2d0e3de2020-10-23 16:31:40 -07001334 return 0;
1335}
1336
Florin Corasd4295e62019-02-22 13:11:38 -08001337/* *INDENT-OFF* */
Nathan Skrzypczake971bc92019-06-19 13:42:37 +02001338static const transport_proto_vft_t cut_thru_proto = {
Florin Coras2d0e3de2020-10-23 16:31:40 -07001339 .enable = ct_enable_disable,
Florin Corasd4295e62019-02-22 13:11:38 -08001340 .start_listen = ct_start_listen,
1341 .stop_listen = ct_stop_listen,
Florin Coras374df7a2021-05-13 11:37:43 -07001342 .get_connection = ct_session_get,
Florin Corasd4295e62019-02-22 13:11:38 -08001343 .get_listener = ct_listener_get,
Florin Coras55686e12023-03-20 09:58:01 -07001344 .get_half_open = ct_session_half_open_get,
Florin Coras374df7a2021-05-13 11:37:43 -07001345 .cleanup = ct_session_cleanup,
1346 .cleanup_ho = ct_cleanup_ho,
Florin Coras2b81e3c2019-02-27 07:55:46 -08001347 .connect = ct_session_connect,
1348 .close = ct_session_close,
Florin Corasf940f8a2019-03-06 10:44:38 -08001349 .custom_tx = ct_custom_tx,
Florin Coras317b8e02019-04-17 09:57:46 -07001350 .app_rx_evt = ct_app_rx_evt,
Florin Corasd4295e62019-02-22 13:11:38 -08001351 .format_listener = format_ct_listener,
Florin Coras374df7a2021-05-13 11:37:43 -07001352 .format_half_open = format_ct_half_open,
Florin Coras2b81e3c2019-02-27 07:55:46 -08001353 .format_connection = format_ct_session,
Nathan Skrzypczake971bc92019-06-19 13:42:37 +02001354 .transport_options = {
Florin Coras07063b82020-03-13 04:44:51 +00001355 .name = "ct",
1356 .short_name = "C",
Nathan Skrzypczake971bc92019-06-19 13:42:37 +02001357 .tx_type = TRANSPORT_TX_INTERNAL,
Florin Coras374df7a2021-05-13 11:37:43 -07001358 .service_type = TRANSPORT_SERVICE_VC,
Nathan Skrzypczake971bc92019-06-19 13:42:37 +02001359 },
Florin Corasd4295e62019-02-22 13:11:38 -08001360};
1361/* *INDENT-ON* */
1362
Florin Coras074f3972021-12-07 15:12:06 -08001363static inline int
1364ct_session_can_tx (session_t *s)
1365{
1366 return (s->session_state == SESSION_STATE_READY ||
1367 s->session_state == SESSION_STATE_CLOSING ||
1368 s->session_state == SESSION_STATE_APP_CLOSED);
1369}
1370
Florin Coras653e43f2019-03-04 10:56:23 -08001371int
1372ct_session_tx (session_t * s)
1373{
1374 ct_connection_t *ct, *peer_ct;
1375 session_t *peer_s;
1376
Florin Coras074f3972021-12-07 15:12:06 -08001377 if (!ct_session_can_tx (s))
1378 return 0;
Florin Coras653e43f2019-03-04 10:56:23 -08001379 ct = (ct_connection_t *) session_get_transport (s);
Florin Coras2d0e3de2020-10-23 16:31:40 -07001380 peer_ct = ct_connection_get (ct->peer_index, ct->c_thread_index);
Florin Coras653e43f2019-03-04 10:56:23 -08001381 if (!peer_ct)
Florin Corasbcdc50d2020-08-25 19:07:02 -07001382 return 0;
Florin Coras2d0e3de2020-10-23 16:31:40 -07001383 peer_s = session_get (peer_ct->c_s_index, peer_ct->c_thread_index);
Florin Coras653e43f2019-03-04 10:56:23 -08001384 if (peer_s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
1385 return 0;
Florin Corasd6894562020-10-28 00:37:15 -07001386 return session_enqueue_notify (peer_s);
Florin Coras653e43f2019-03-04 10:56:23 -08001387}
1388
Florin Corasd4295e62019-02-22 13:11:38 -08001389static clib_error_t *
1390ct_transport_init (vlib_main_t * vm)
1391{
1392 transport_register_protocol (TRANSPORT_PROTO_NONE, &cut_thru_proto,
1393 FIB_PROTOCOL_IP4, ~0);
Florin Coras653e43f2019-03-04 10:56:23 -08001394 transport_register_protocol (TRANSPORT_PROTO_NONE, &cut_thru_proto,
1395 FIB_PROTOCOL_IP6, ~0);
Florin Corasd4295e62019-02-22 13:11:38 -08001396 return 0;
1397}
1398
1399VLIB_INIT_FUNCTION (ct_transport_init);
1400
Florin Corasba7d8f52019-02-22 13:11:38 -08001401/*
1402 * fd.io coding-style-patch-verification: ON
1403 *
1404 * Local Variables:
1405 * eval: (c-set-style "gnu")
1406 * End:
1407 */