blob: f81e6bb067edeff37a1e58b2609f23cacf5a9741 [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 */
56} ct_worker_t;
57
Florin Coras2d0e3de2020-10-23 16:31:40 -070058typedef struct ct_main_
Florin Coras653e43f2019-03-04 10:56:23 -080059{
Florin Coras7fe00692021-11-15 14:01:02 -080060 ct_worker_t *wrk; /**< Per-worker state */
Florin Coras2d0e3de2020-10-23 16:31:40 -070061 u32 n_workers; /**< Number of vpp workers */
62 u32 n_sessions; /**< Cumulative sessions counter */
Florin Coras374df7a2021-05-13 11:37:43 -070063 u32 *ho_reusable; /**< Vector of reusable ho indices */
64 clib_spinlock_t ho_reuseable_lock; /**< Lock for reusable ho indices */
Florin Corasda78c5a2021-06-09 14:55:24 -070065 clib_rwlock_t app_segs_lock; /**< RW lock for seg contexts */
66 uword *app_segs_ctxs_table; /**< App handle to segment pool map */
67 ct_segments_ctx_t *app_seg_ctxs; /**< Pool of ct segment contexts */
Florin Coras2d0e3de2020-10-23 16:31:40 -070068} ct_main_t;
Florin Coras653e43f2019-03-04 10:56:23 -080069
Florin Coras2d0e3de2020-10-23 16:31:40 -070070static ct_main_t ct_main;
Florin Coras653e43f2019-03-04 10:56:23 -080071
Florin Coras7fe00692021-11-15 14:01:02 -080072static inline ct_worker_t *
73ct_worker_get (u32 thread_index)
74{
75 return &ct_main.wrk[thread_index];
76}
77
Florin Coras653e43f2019-03-04 10:56:23 -080078static ct_connection_t *
Florin Coras2d0e3de2020-10-23 16:31:40 -070079ct_connection_alloc (u32 thread_index)
Florin Corasd4295e62019-02-22 13:11:38 -080080{
Florin Coras7fe00692021-11-15 14:01:02 -080081 ct_worker_t *wrk = ct_worker_get (thread_index);
Florin Corasd4295e62019-02-22 13:11:38 -080082 ct_connection_t *ct;
83
Florin Coras9688b3b2022-04-07 12:58:13 -070084 pool_get_aligned_safe (wrk->connections, ct, CLIB_CACHE_LINE_BYTES);
85 clib_memset (ct, 0, sizeof (*ct));
Florin Coras7fe00692021-11-15 14:01:02 -080086 ct->c_c_index = ct - wrk->connections;
Florin Coras2d0e3de2020-10-23 16:31:40 -070087 ct->c_thread_index = thread_index;
Florin Corasd4295e62019-02-22 13:11:38 -080088 ct->client_wrk = ~0;
89 ct->server_wrk = ~0;
Florin Corasda78c5a2021-06-09 14:55:24 -070090 ct->seg_ctx_index = ~0;
91 ct->ct_seg_index = ~0;
Florin Corasd4295e62019-02-22 13:11:38 -080092 return ct;
93}
94
Florin Coras653e43f2019-03-04 10:56:23 -080095static ct_connection_t *
Florin Coras2d0e3de2020-10-23 16:31:40 -070096ct_connection_get (u32 ct_index, u32 thread_index)
Florin Corasd4295e62019-02-22 13:11:38 -080097{
Florin Coras7fe00692021-11-15 14:01:02 -080098 ct_worker_t *wrk = ct_worker_get (thread_index);
99
100 if (pool_is_free_index (wrk->connections, ct_index))
Florin Corasd4295e62019-02-22 13:11:38 -0800101 return 0;
Florin Coras7fe00692021-11-15 14:01:02 -0800102 return pool_elt_at_index (wrk->connections, ct_index);
Florin Corasba7d8f52019-02-22 13:11:38 -0800103}
104
Florin Coras653e43f2019-03-04 10:56:23 -0800105static void
Florin Corasd4295e62019-02-22 13:11:38 -0800106ct_connection_free (ct_connection_t * ct)
Florin Corasba7d8f52019-02-22 13:11:38 -0800107{
Florin Coras7fe00692021-11-15 14:01:02 -0800108 ct_worker_t *wrk = ct_worker_get (ct->c_thread_index);
109
Florin Corasba7d8f52019-02-22 13:11:38 -0800110 if (CLIB_DEBUG)
Florin Coras2d0e3de2020-10-23 16:31:40 -0700111 {
Florin Coras7fe00692021-11-15 14:01:02 -0800112 clib_memset (ct, 0xfc, sizeof (*ct));
113 pool_put (wrk->connections, ct);
Florin Coras2d0e3de2020-10-23 16:31:40 -0700114 return;
115 }
Florin Coras7fe00692021-11-15 14:01:02 -0800116 pool_put (wrk->connections, ct);
Florin Corasba7d8f52019-02-22 13:11:38 -0800117}
118
Florin Coras374df7a2021-05-13 11:37:43 -0700119static ct_connection_t *
120ct_half_open_alloc (void)
121{
122 ct_main_t *cm = &ct_main;
123 u32 *hip;
124
125 clib_spinlock_lock (&cm->ho_reuseable_lock);
126 vec_foreach (hip, cm->ho_reusable)
Florin Coras7fe00692021-11-15 14:01:02 -0800127 pool_put_index (cm->wrk[0].connections, *hip);
Florin Coras374df7a2021-05-13 11:37:43 -0700128 vec_reset_length (cm->ho_reusable);
129 clib_spinlock_unlock (&cm->ho_reuseable_lock);
130
131 return ct_connection_alloc (0);
132}
133
134void
135ct_half_open_add_reusable (u32 ho_index)
136{
137 ct_main_t *cm = &ct_main;
138
139 clib_spinlock_lock (&cm->ho_reuseable_lock);
140 vec_add1 (cm->ho_reusable, ho_index);
141 clib_spinlock_unlock (&cm->ho_reuseable_lock);
142}
143
Florin Coras2b81e3c2019-02-27 07:55:46 -0800144session_t *
145ct_session_get_peer (session_t * s)
146{
147 ct_connection_t *ct, *peer_ct;
Florin Coras2d0e3de2020-10-23 16:31:40 -0700148 ct = ct_connection_get (s->connection_index, s->thread_index);
149 peer_ct = ct_connection_get (ct->peer_index, s->thread_index);
150 return session_get (peer_ct->c_s_index, s->thread_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800151}
152
Florin Corasba7d8f52019-02-22 13:11:38 -0800153void
Florin Coras2b81e3c2019-02-27 07:55:46 -0800154ct_session_endpoint (session_t * ll, session_endpoint_t * sep)
Florin Corasba7d8f52019-02-22 13:11:38 -0800155{
Florin Corasd4295e62019-02-22 13:11:38 -0800156 ct_connection_t *ct;
157 ct = (ct_connection_t *) session_get_transport (ll);
158 sep->transport_proto = ct->actual_tp;
159 sep->port = ct->c_lcl_port;
160 sep->is_ip4 = ct->c_is_ip4;
Florin Corasea7e7082020-07-07 17:57:28 -0700161 ip_copy (&sep->ip, &ct->c_lcl_ip, ct->c_is_ip4);
Florin Corasba7d8f52019-02-22 13:11:38 -0800162}
163
Florin Corasda78c5a2021-06-09 14:55:24 -0700164static void
Florin Coras98952382021-11-14 15:33:59 -0800165ct_set_invalid_app_wrk (ct_connection_t *ct, u8 is_client)
166{
167 ct_connection_t *peer_ct;
168
169 peer_ct = ct_connection_get (ct->peer_index, ct->c_thread_index);
170
171 if (is_client)
172 {
173 ct->client_wrk = APP_INVALID_INDEX;
174 if (peer_ct)
175 ct->client_wrk = APP_INVALID_INDEX;
176 }
177 else
178 {
179 ct->server_wrk = APP_INVALID_INDEX;
180 if (peer_ct)
181 ct->server_wrk = APP_INVALID_INDEX;
182 }
183}
184
185static void
Florin Corasda78c5a2021-06-09 14:55:24 -0700186ct_session_dealloc_fifos (ct_connection_t *ct, svm_fifo_t *rx_fifo,
187 svm_fifo_t *tx_fifo)
Florin Corasba7d8f52019-02-22 13:11:38 -0800188{
Florin Corasda78c5a2021-06-09 14:55:24 -0700189 ct_segments_ctx_t *seg_ctx;
190 ct_main_t *cm = &ct_main;
Florin Corasba7d8f52019-02-22 13:11:38 -0800191 segment_manager_t *sm;
Florin Corasda78c5a2021-06-09 14:55:24 -0700192 app_worker_t *app_wrk;
193 ct_segment_t *ct_seg;
194 fifo_segment_t *fs;
195 u32 seg_index;
Florin Coras98952382021-11-14 15:33:59 -0800196 session_t *s;
Florin Coras6973c732021-06-17 15:53:38 -0700197 int cnt;
Florin Corasda78c5a2021-06-09 14:55:24 -0700198
199 /*
200 * Cleanup fifos
201 */
202
203 sm = segment_manager_get (rx_fifo->segment_manager);
204 seg_index = rx_fifo->segment_index;
205
206 fs = segment_manager_get_segment_w_lock (sm, seg_index);
207 fifo_segment_free_fifo (fs, rx_fifo);
208 fifo_segment_free_fifo (fs, tx_fifo);
209 segment_manager_segment_reader_unlock (sm);
210
211 /*
Florin Coras6973c732021-06-17 15:53:38 -0700212 * Atomically update segment context with readers lock
Florin Corasda78c5a2021-06-09 14:55:24 -0700213 */
214
215 clib_rwlock_reader_lock (&cm->app_segs_lock);
216
217 seg_ctx = pool_elt_at_index (cm->app_seg_ctxs, ct->seg_ctx_index);
218 ct_seg = pool_elt_at_index (seg_ctx->segments, ct->ct_seg_index);
219
220 if (ct->flags & CT_CONN_F_CLIENT)
221 {
222 cnt =
223 __atomic_sub_fetch (&ct_seg->client_n_sessions, 1, __ATOMIC_RELAXED);
Florin Corasda78c5a2021-06-09 14:55:24 -0700224 }
225 else
226 {
227 cnt =
228 __atomic_sub_fetch (&ct_seg->server_n_sessions, 1, __ATOMIC_RELAXED);
Florin Corasda78c5a2021-06-09 14:55:24 -0700229 }
230
Florin Corasda78c5a2021-06-09 14:55:24 -0700231 clib_rwlock_reader_unlock (&cm->app_segs_lock);
232
233 /*
234 * No need to do any app updates, return
235 */
Florin Coras6973c732021-06-17 15:53:38 -0700236 ASSERT (cnt >= 0);
237 if (cnt)
238 return;
239
240 /*
241 * Grab exclusive lock and update flags unless some other thread
242 * added more sessions
243 */
244 clib_rwlock_writer_lock (&cm->app_segs_lock);
245
246 seg_ctx = pool_elt_at_index (cm->app_seg_ctxs, ct->seg_ctx_index);
247 ct_seg = pool_elt_at_index (seg_ctx->segments, ct->ct_seg_index);
248 if (ct->flags & CT_CONN_F_CLIENT)
249 {
250 cnt = ct_seg->client_n_sessions;
Florin Coras98952382021-11-14 15:33:59 -0800251 if (cnt)
252 goto done;
253 ct_seg->flags |= CT_SEGMENT_F_CLIENT_DETACHED;
254 s = session_get (ct->c_s_index, ct->c_thread_index);
255 if (s->app_wrk_index == APP_INVALID_INDEX)
256 ct_set_invalid_app_wrk (ct, 1 /* is_client */);
Florin Coras6973c732021-06-17 15:53:38 -0700257 }
258 else
259 {
260 cnt = ct_seg->server_n_sessions;
Florin Coras98952382021-11-14 15:33:59 -0800261 if (cnt)
262 goto done;
263 ct_seg->flags |= CT_SEGMENT_F_SERVER_DETACHED;
264 s = session_get (ct->c_s_index, ct->c_thread_index);
265 if (s->app_wrk_index == APP_INVALID_INDEX)
266 ct_set_invalid_app_wrk (ct, 0 /* is_client */);
Florin Coras6973c732021-06-17 15:53:38 -0700267 }
268
Florin Coras98952382021-11-14 15:33:59 -0800269 if (!(ct_seg->flags & CT_SEGMENT_F_CLIENT_DETACHED) ||
270 !(ct_seg->flags & CT_SEGMENT_F_SERVER_DETACHED))
271 goto done;
272
Florin Coras6973c732021-06-17 15:53:38 -0700273 /*
274 * Remove segment context because both client and server detached
275 */
276
Florin Coras98952382021-11-14 15:33:59 -0800277 pool_put_index (seg_ctx->segments, ct->ct_seg_index);
Florin Coras6973c732021-06-17 15:53:38 -0700278
279 /*
Florin Coras98952382021-11-14 15:33:59 -0800280 * No more segment indices left, remove the segments context
Florin Coras6973c732021-06-17 15:53:38 -0700281 */
Florin Coras98952382021-11-14 15:33:59 -0800282 if (!pool_elts (seg_ctx->segments))
Florin Corasda78c5a2021-06-09 14:55:24 -0700283 {
Florin Coras98952382021-11-14 15:33:59 -0800284 u64 table_handle = seg_ctx->client_wrk << 16 | seg_ctx->server_wrk;
285 table_handle = (u64) seg_ctx->sm_index << 32 | table_handle;
286 hash_unset (cm->app_segs_ctxs_table, table_handle);
287 pool_free (seg_ctx->segments);
288 pool_put_index (cm->app_seg_ctxs, ct->seg_ctx_index);
Florin Corasda78c5a2021-06-09 14:55:24 -0700289 }
Florin Coras98952382021-11-14 15:33:59 -0800290
291 /*
292 * Segment to be removed so notify both apps
293 */
294
295 app_wrk = app_worker_get_if_valid (ct->client_wrk);
296 /* Determine if client app still needs notification, i.e., if it is
297 * still attached. If client detached and this is the last ct session
298 * on this segment, then its connects segment manager should also be
299 * detached, so do not send notification */
300 if (app_wrk)
301 {
302 segment_manager_t *csm;
303 csm = app_worker_get_connect_segment_manager (app_wrk);
304 if (!segment_manager_app_detached (csm))
305 app_worker_del_segment_notify (app_wrk, ct->segment_handle);
306 }
307
Florin Coras13987da2021-12-07 14:47:12 -0800308 /* Notify server app and free segment */
Florin Corasda78c5a2021-06-09 14:55:24 -0700309 segment_manager_lock_and_del_segment (sm, seg_index);
310
311 /* Cleanup segment manager if needed. If server detaches there's a chance
312 * the client's sessions will hold up segment removal */
313 if (segment_manager_app_detached (sm) && !segment_manager_has_fifos (sm))
314 segment_manager_free_safe (sm);
Florin Coras98952382021-11-14 15:33:59 -0800315
316done:
317
318 clib_rwlock_writer_unlock (&cm->app_segs_lock);
Florin Corasda78c5a2021-06-09 14:55:24 -0700319}
320
Florin Corasa7bd8262021-11-25 12:14:25 -0800321static void
322ct_session_force_disconnect_server (ct_connection_t *sct)
323{
324 sct->peer_index = ~0;
325 session_transport_closing_notify (&sct->connection);
326}
327
Florin Corasda78c5a2021-06-09 14:55:24 -0700328int
Florin Coras6973c732021-06-17 15:53:38 -0700329ct_session_connect_notify (session_t *ss, session_error_t err)
Florin Corasda78c5a2021-06-09 14:55:24 -0700330{
Florin Coras6973c732021-06-17 15:53:38 -0700331 u32 ss_index, opaque, thread_index;
Florin Corasda78c5a2021-06-09 14:55:24 -0700332 ct_connection_t *sct, *cct;
Florin Corasda78c5a2021-06-09 14:55:24 -0700333 app_worker_t *client_wrk;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800334 session_t *cs;
Florin Corasba7d8f52019-02-22 13:11:38 -0800335
Florin Coras2b81e3c2019-02-27 07:55:46 -0800336 ss_index = ss->session_index;
Florin Coras2d0e3de2020-10-23 16:31:40 -0700337 thread_index = ss->thread_index;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800338 sct = (ct_connection_t *) session_get_transport (ss);
339 client_wrk = app_worker_get (sct->client_wrk);
Florin Corasdd7d24b2020-08-14 17:51:13 -0700340 opaque = sct->client_opaque;
Florin Corasba7d8f52019-02-22 13:11:38 -0800341
Florin Coras2d0e3de2020-10-23 16:31:40 -0700342 cct = ct_connection_get (sct->peer_index, thread_index);
Florin Corasba7d8f52019-02-22 13:11:38 -0800343
Florin Coras374df7a2021-05-13 11:37:43 -0700344 /* Client closed while waiting for reply from server */
Florin Coras6973c732021-06-17 15:53:38 -0700345 if (PREDICT_FALSE (!cct))
Florin Coras374df7a2021-05-13 11:37:43 -0700346 {
Florin Corasa7bd8262021-11-25 12:14:25 -0800347 ct_session_force_disconnect_server (sct);
Florin Coras374df7a2021-05-13 11:37:43 -0700348 return 0;
349 }
350
351 session_half_open_delete_notify (&cct->connection);
352 cct->flags &= ~CT_CONN_F_HALF_OPEN;
353
Florin Coras6973c732021-06-17 15:53:38 -0700354 if (PREDICT_FALSE (err))
355 goto connect_error;
Florin Corasda78c5a2021-06-09 14:55:24 -0700356
357 /*
Florin Corasa7bd8262021-11-25 12:14:25 -0800358 * Alloc client session, server session assumed to be established
Florin Corasda78c5a2021-06-09 14:55:24 -0700359 */
360
Florin Corasa7bd8262021-11-25 12:14:25 -0800361 ASSERT (ss->session_state >= SESSION_STATE_READY);
362
Florin Coras2d0e3de2020-10-23 16:31:40 -0700363 cs = session_alloc (thread_index);
364 ss = session_get (ss_index, thread_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800365 cs->session_type = ss->session_type;
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +0200366 cs->listener_handle = SESSION_INVALID_HANDLE;
Steven Luongd810a6e2022-10-25 13:09:11 -0700367 session_set_state (cs, SESSION_STATE_CONNECTING);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800368 cs->app_wrk_index = client_wrk->wrk_index;
369 cs->connection_index = cct->c_c_index;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800370 cct->c_s_index = cs->session_index;
371
372 /* This will allocate fifos for the session. They won't be used for
373 * exchanging data but they will be used to close the connection if
374 * the segment manager/worker is freed */
Florin Corasdd7d24b2020-08-14 17:51:13 -0700375 if ((err = app_worker_init_connected (client_wrk, cs)))
Florin Coras2b81e3c2019-02-27 07:55:46 -0800376 {
Florin Corasdd7d24b2020-08-14 17:51:13 -0700377 session_free (cs);
Florin Corasa7bd8262021-11-25 12:14:25 -0800378 ct_session_force_disconnect_server (sct);
Florin Coras6973c732021-06-17 15:53:38 -0700379 err = SESSION_E_ALLOC;
380 goto connect_error;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800381 }
382
Steven Luongd810a6e2022-10-25 13:09:11 -0700383 session_set_state (cs, SESSION_STATE_CONNECTING);
Florin Corasdd7d24b2020-08-14 17:51:13 -0700384
Florin Coras6973c732021-06-17 15:53:38 -0700385 if (app_worker_connect_notify (client_wrk, cs, 0, opaque))
Florin Coras2b81e3c2019-02-27 07:55:46 -0800386 {
Florin Coras6973c732021-06-17 15:53:38 -0700387 segment_manager_dealloc_fifos (cs->rx_fifo, cs->tx_fifo);
Florin Corasdd7d24b2020-08-14 17:51:13 -0700388 session_free (cs);
Florin Corasa7bd8262021-11-25 12:14:25 -0800389 ct_session_force_disconnect_server (sct);
Florin Coras6973c732021-06-17 15:53:38 -0700390 goto cleanup_client;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800391 }
392
Florin Coras2d0e3de2020-10-23 16:31:40 -0700393 cs = session_get (cct->c_s_index, cct->c_thread_index);
Steven Luongd810a6e2022-10-25 13:09:11 -0700394 session_set_state (cs, SESSION_STATE_READY);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800395
Florin Corasba7d8f52019-02-22 13:11:38 -0800396 return 0;
Florin Corasdd7d24b2020-08-14 17:51:13 -0700397
Florin Coras6973c732021-06-17 15:53:38 -0700398connect_error:
399
400 app_worker_connect_notify (client_wrk, 0, err, cct->client_opaque);
401
402cleanup_client:
403
404 if (cct->client_rx_fifo)
405 ct_session_dealloc_fifos (cct, cct->client_rx_fifo, cct->client_tx_fifo);
406 ct_connection_free (cct);
Florin Corasdd7d24b2020-08-14 17:51:13 -0700407 return -1;
Florin Corasba7d8f52019-02-22 13:11:38 -0800408}
409
Florin Coras6973c732021-06-17 15:53:38 -0700410static inline ct_segment_t *
411ct_lookup_free_segment (ct_main_t *cm, segment_manager_t *sm,
412 u32 seg_ctx_index)
Florin Corasba7d8f52019-02-22 13:11:38 -0800413{
Florin Corasda78c5a2021-06-09 14:55:24 -0700414 uword free_bytes, max_free_bytes;
415 ct_segment_t *ct_seg, *res = 0;
Florin Coras6973c732021-06-17 15:53:38 -0700416 ct_segments_ctx_t *seg_ctx;
Florin Corasda78c5a2021-06-09 14:55:24 -0700417 fifo_segment_t *fs;
418 u32 max_fifos;
419
Florin Coras6973c732021-06-17 15:53:38 -0700420 seg_ctx = pool_elt_at_index (cm->app_seg_ctxs, seg_ctx_index);
421 max_free_bytes = seg_ctx->fifo_pair_bytes;
422
Florin Corasda78c5a2021-06-09 14:55:24 -0700423 pool_foreach (ct_seg, seg_ctx->segments)
424 {
425 /* Client or server has detached so segment cannot be used */
Florin Corasda78c5a2021-06-09 14:55:24 -0700426 fs = segment_manager_get_segment (sm, ct_seg->segment_index);
427 free_bytes = fifo_segment_available_bytes (fs);
Florin Coras6973c732021-06-17 15:53:38 -0700428 max_fifos = fifo_segment_size (fs) / seg_ctx->fifo_pair_bytes;
Florin Corasda78c5a2021-06-09 14:55:24 -0700429 if (free_bytes > max_free_bytes &&
430 fifo_segment_num_fifos (fs) / 2 < max_fifos)
431 {
432 max_free_bytes = free_bytes;
433 res = ct_seg;
434 }
435 }
436
437 return res;
438}
439
Florin Coras6973c732021-06-17 15:53:38 -0700440static ct_segment_t *
441ct_alloc_segment (ct_main_t *cm, app_worker_t *server_wrk, u64 table_handle,
442 segment_manager_t *sm, u32 client_wrk_index)
443{
444 u32 seg_ctx_index = ~0, sm_index, pair_bytes;
445 segment_manager_props_t *props;
446 const u32 margin = 16 << 10;
447 ct_segments_ctx_t *seg_ctx;
448 app_worker_t *client_wrk;
449 u64 seg_size, seg_handle;
450 application_t *server;
451 ct_segment_t *ct_seg;
452 uword *spp;
453 int fs_index;
454
455 server = application_get (server_wrk->app_index);
456 props = application_segment_manager_properties (server);
457 sm_index = segment_manager_index (sm);
458 pair_bytes = props->rx_fifo_size + props->tx_fifo_size + margin;
459
460 /*
461 * Make sure another thread did not alloc a segment while acquiring the lock
462 */
463
464 spp = hash_get (cm->app_segs_ctxs_table, table_handle);
465 if (spp)
466 {
467 seg_ctx_index = *spp;
468 ct_seg = ct_lookup_free_segment (cm, sm, seg_ctx_index);
469 if (ct_seg)
470 return ct_seg;
471 }
472
473 /*
474 * No segment, try to alloc one and notify the server and the client.
475 * Make sure the segment is not used for other fifos
476 */
477 seg_size = clib_max (props->segment_size, 128 << 20);
478 fs_index =
479 segment_manager_add_segment2 (sm, seg_size, FIFO_SEGMENT_F_CUSTOM_USE);
480 if (fs_index < 0)
481 return 0;
482
483 if (seg_ctx_index == ~0)
484 {
485 pool_get_zero (cm->app_seg_ctxs, seg_ctx);
486 seg_ctx_index = seg_ctx - cm->app_seg_ctxs;
487 hash_set (cm->app_segs_ctxs_table, table_handle, seg_ctx_index);
488 seg_ctx->server_wrk = server_wrk->wrk_index;
489 seg_ctx->client_wrk = client_wrk_index;
490 seg_ctx->sm_index = sm_index;
491 seg_ctx->fifo_pair_bytes = pair_bytes;
492 }
493 else
494 {
495 seg_ctx = pool_elt_at_index (cm->app_seg_ctxs, seg_ctx_index);
496 }
497
498 pool_get_zero (seg_ctx->segments, ct_seg);
499 ct_seg->segment_index = fs_index;
500 ct_seg->server_n_sessions = 0;
501 ct_seg->client_n_sessions = 0;
502 ct_seg->ct_seg_index = ct_seg - seg_ctx->segments;
503 ct_seg->seg_ctx_index = seg_ctx_index;
504
505 /* New segment, notify the server and client */
506 seg_handle = segment_manager_make_segment_handle (sm_index, fs_index);
507 if (app_worker_add_segment_notify (server_wrk, seg_handle))
508 goto error;
509
510 client_wrk = app_worker_get (client_wrk_index);
511 if (app_worker_add_segment_notify (client_wrk, seg_handle))
512 {
513 app_worker_del_segment_notify (server_wrk, seg_handle);
514 goto error;
515 }
516
517 return ct_seg;
518
519error:
520
521 segment_manager_lock_and_del_segment (sm, fs_index);
522 pool_put_index (seg_ctx->segments, ct_seg->seg_ctx_index);
523 return 0;
524}
525
Florin Corasda78c5a2021-06-09 14:55:24 -0700526static int
527ct_init_accepted_session (app_worker_t *server_wrk, ct_connection_t *ct,
528 session_t *ls, session_t *ll)
529{
Florin Coras88001c62019-04-24 14:44:46 -0700530 segment_manager_props_t *props;
Florin Coras6973c732021-06-17 15:53:38 -0700531 u64 seg_handle, table_handle;
532 u32 sm_index, fs_index = ~0;
Florin Corasda78c5a2021-06-09 14:55:24 -0700533 ct_segments_ctx_t *seg_ctx;
534 ct_main_t *cm = &ct_main;
Florin Coras653e43f2019-03-04 10:56:23 -0800535 application_t *server;
Florin Corasba7d8f52019-02-22 13:11:38 -0800536 segment_manager_t *sm;
Florin Corasda78c5a2021-06-09 14:55:24 -0700537 ct_segment_t *ct_seg;
538 fifo_segment_t *fs;
Florin Corasda78c5a2021-06-09 14:55:24 -0700539 uword *spp;
Florin Coras6973c732021-06-17 15:53:38 -0700540 int rv;
Florin Corasba7d8f52019-02-22 13:11:38 -0800541
Florin Coras2b81e3c2019-02-27 07:55:46 -0800542 sm = app_worker_get_listen_segment_manager (server_wrk, ll);
Florin Corasda78c5a2021-06-09 14:55:24 -0700543 sm_index = segment_manager_index (sm);
544 server = application_get (server_wrk->app_index);
545 props = application_segment_manager_properties (server);
Florin Corasba7d8f52019-02-22 13:11:38 -0800546
Florin Corasda78c5a2021-06-09 14:55:24 -0700547 table_handle = ct->client_wrk << 16 | server_wrk->wrk_index;
Florin Coras6973c732021-06-17 15:53:38 -0700548 table_handle = (u64) sm_index << 32 | table_handle;
Florin Corasda78c5a2021-06-09 14:55:24 -0700549
550 /*
551 * Check if we already have a segment that can hold the fifos
552 */
553
554 clib_rwlock_reader_lock (&cm->app_segs_lock);
555
556 spp = hash_get (cm->app_segs_ctxs_table, table_handle);
557 if (spp)
558 {
Florin Coras6973c732021-06-17 15:53:38 -0700559 ct_seg = ct_lookup_free_segment (cm, sm, *spp);
Florin Corasda78c5a2021-06-09 14:55:24 -0700560 if (ct_seg)
561 {
Florin Coras6973c732021-06-17 15:53:38 -0700562 ct->seg_ctx_index = ct_seg->seg_ctx_index;
563 ct->ct_seg_index = ct_seg->ct_seg_index;
Florin Corasda78c5a2021-06-09 14:55:24 -0700564 fs_index = ct_seg->segment_index;
Florin Coras98952382021-11-14 15:33:59 -0800565 ct_seg->flags &=
566 ~(CT_SEGMENT_F_SERVER_DETACHED | CT_SEGMENT_F_CLIENT_DETACHED);
Florin Corasda78c5a2021-06-09 14:55:24 -0700567 __atomic_add_fetch (&ct_seg->server_n_sessions, 1, __ATOMIC_RELAXED);
Florin Coras6973c732021-06-17 15:53:38 -0700568 __atomic_add_fetch (&ct_seg->client_n_sessions, 1, __ATOMIC_RELAXED);
Florin Corasda78c5a2021-06-09 14:55:24 -0700569 }
570 }
571
572 clib_rwlock_reader_unlock (&cm->app_segs_lock);
573
574 /*
Florin Coras6973c732021-06-17 15:53:38 -0700575 * If not, grab exclusive lock and allocate segment
Florin Corasda78c5a2021-06-09 14:55:24 -0700576 */
Florin Coras6973c732021-06-17 15:53:38 -0700577 if (fs_index == ~0)
Florin Corasda78c5a2021-06-09 14:55:24 -0700578 {
Florin Corasda78c5a2021-06-09 14:55:24 -0700579 clib_rwlock_writer_lock (&cm->app_segs_lock);
580
Florin Coras6973c732021-06-17 15:53:38 -0700581 ct_seg =
582 ct_alloc_segment (cm, server_wrk, table_handle, sm, ct->client_wrk);
583 if (!ct_seg)
Florin Corasda78c5a2021-06-09 14:55:24 -0700584 {
Florin Coras6973c732021-06-17 15:53:38 -0700585 clib_rwlock_writer_unlock (&cm->app_segs_lock);
586 return -1;
Florin Corasda78c5a2021-06-09 14:55:24 -0700587 }
Florin Corasda78c5a2021-06-09 14:55:24 -0700588
Florin Coras6973c732021-06-17 15:53:38 -0700589 ct->seg_ctx_index = ct_seg->seg_ctx_index;
590 ct->ct_seg_index = ct_seg->ct_seg_index;
Florin Corasda78c5a2021-06-09 14:55:24 -0700591 ct_seg->server_n_sessions += 1;
Florin Coras6973c732021-06-17 15:53:38 -0700592 ct_seg->client_n_sessions += 1;
593 fs_index = ct_seg->segment_index;
Florin Corasda78c5a2021-06-09 14:55:24 -0700594
595 clib_rwlock_writer_unlock (&cm->app_segs_lock);
Florin Corasda78c5a2021-06-09 14:55:24 -0700596 }
597
598 /*
599 * Allocate and initialize the fifos
600 */
601 fs = segment_manager_get_segment_w_lock (sm, fs_index);
602 rv = segment_manager_try_alloc_fifos (
603 fs, ls->thread_index, props->rx_fifo_size, props->tx_fifo_size,
604 &ls->rx_fifo, &ls->tx_fifo);
Florin Corasba7d8f52019-02-22 13:11:38 -0800605 if (rv)
606 {
Florin Corasba7d8f52019-02-22 13:11:38 -0800607 segment_manager_segment_reader_unlock (sm);
Florin Coras6973c732021-06-17 15:53:38 -0700608
609 clib_rwlock_reader_lock (&cm->app_segs_lock);
610
611 seg_ctx = pool_elt_at_index (cm->app_seg_ctxs, ct->seg_ctx_index);
612 ct_seg = pool_elt_at_index (seg_ctx->segments, ct->ct_seg_index);
613 __atomic_sub_fetch (&ct_seg->server_n_sessions, 1, __ATOMIC_RELAXED);
614 __atomic_sub_fetch (&ct_seg->client_n_sessions, 1, __ATOMIC_RELAXED);
615
616 clib_rwlock_reader_unlock (&cm->app_segs_lock);
617
618 return rv;
Florin Corasba7d8f52019-02-22 13:11:38 -0800619 }
Florin Coras2b81e3c2019-02-27 07:55:46 -0800620
Florin Corasc547e912020-12-08 17:50:45 -0800621 ls->rx_fifo->shr->master_session_index = ls->session_index;
622 ls->tx_fifo->shr->master_session_index = ls->session_index;
Florin Corasce252ca2020-11-04 13:08:35 -0800623 ls->rx_fifo->master_thread_index = ls->thread_index;
624 ls->tx_fifo->master_thread_index = ls->thread_index;
Florin Corasba7d8f52019-02-22 13:11:38 -0800625
Florin Corasda78c5a2021-06-09 14:55:24 -0700626 seg_handle = segment_manager_segment_handle (sm, fs);
Florin Corasba7d8f52019-02-22 13:11:38 -0800627 segment_manager_segment_reader_unlock (sm);
Florin Corasda78c5a2021-06-09 14:55:24 -0700628
629 ct->segment_handle = seg_handle;
Florin Corasba7d8f52019-02-22 13:11:38 -0800630
631 return 0;
Florin Corasba7d8f52019-02-22 13:11:38 -0800632}
633
Florin Coras2d0e3de2020-10-23 16:31:40 -0700634static void
Florin Corasc215e5a2021-11-15 19:01:52 -0800635ct_accept_one (u32 thread_index, u32 ho_index)
Florin Coras2d0e3de2020-10-23 16:31:40 -0700636{
Florin Corasba026412021-06-12 11:56:19 -0700637 ct_connection_t *sct, *cct, *ho;
638 transport_connection_t *ll_ct;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800639 app_worker_t *server_wrk;
Florin Corasc215e5a2021-11-15 19:01:52 -0800640 u32 cct_index, ll_index;
Florin Coras2d0e3de2020-10-23 16:31:40 -0700641 session_t *ss, *ll;
Florin Corasba7d8f52019-02-22 13:11:38 -0800642
Florin Coras374df7a2021-05-13 11:37:43 -0700643 /*
644 * Alloc client ct and initialize from ho
645 */
Florin Coras374df7a2021-05-13 11:37:43 -0700646 cct = ct_connection_alloc (thread_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800647 cct_index = cct->c_c_index;
Florin Coras374df7a2021-05-13 11:37:43 -0700648
Florin Coras374df7a2021-05-13 11:37:43 -0700649 ho = ct_connection_get (ho_index, 0);
650
651 /* Unlikely but half-open session and transport could have been freed */
652 if (PREDICT_FALSE (!ho))
653 {
654 ct_connection_free (cct);
655 return;
656 }
657
658 clib_memcpy (cct, ho, sizeof (*ho));
659 cct->c_c_index = cct_index;
660 cct->c_thread_index = thread_index;
661 cct->flags |= CT_CONN_F_HALF_OPEN;
662
663 /* Notify session layer that half-open is on a different thread
664 * and mark ho connection index reusable. Avoids another rpc
665 */
666 session_half_open_migrate_notify (&cct->connection);
667 session_half_open_migrated_notify (&cct->connection);
668 ct_half_open_add_reusable (ho_index);
Florin Corasba7d8f52019-02-22 13:11:38 -0800669
Florin Coras2b81e3c2019-02-27 07:55:46 -0800670 /*
Florin Coras374df7a2021-05-13 11:37:43 -0700671 * Alloc and init server transport
Florin Coras2b81e3c2019-02-27 07:55:46 -0800672 */
Florin Coras374df7a2021-05-13 11:37:43 -0700673
674 ll_index = cct->peer_index;
675 ll = listen_session_get (ll_index);
676 sct = ct_connection_alloc (thread_index);
Florin Corasba026412021-06-12 11:56:19 -0700677 /* Transport not necessarily ct but it might, so grab after sct alloc */
678 ll_ct = listen_session_get_transport (ll);
Florin Coras374df7a2021-05-13 11:37:43 -0700679
680 /* Make sure cct is valid after sct alloc */
681 cct = ct_connection_get (cct_index, thread_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800682
Florin Coras2b81e3c2019-02-27 07:55:46 -0800683 sct->c_rmt_port = 0;
Florin Corasba026412021-06-12 11:56:19 -0700684 sct->c_lcl_port = ll_ct->lcl_port;
Florin Coras374df7a2021-05-13 11:37:43 -0700685 sct->c_is_ip4 = cct->c_is_ip4;
Florin Corasd9e98702021-10-11 18:10:41 -0700686 clib_memcpy (&sct->c_lcl_ip, &cct->c_rmt_ip, sizeof (cct->c_rmt_ip));
Florin Coras374df7a2021-05-13 11:37:43 -0700687 sct->client_wrk = cct->client_wrk;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800688 sct->c_proto = TRANSPORT_PROTO_NONE;
Florin Coras374df7a2021-05-13 11:37:43 -0700689 sct->client_opaque = cct->client_opaque;
Florin Corasba026412021-06-12 11:56:19 -0700690 sct->actual_tp = cct->actual_tp;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800691
692 sct->peer_index = cct->c_c_index;
693 cct->peer_index = sct->c_c_index;
694
695 /*
696 * Accept server session. Client session is created only after
697 * server confirms accept.
698 */
Florin Coras374df7a2021-05-13 11:37:43 -0700699 ss = session_alloc (thread_index);
700 ll = listen_session_get (ll_index);
Florin Coras9f1a5432019-03-10 21:03:20 -0700701 ss->session_type = session_type_from_proto_and_ip (TRANSPORT_PROTO_NONE,
702 sct->c_is_ip4);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800703 ss->connection_index = sct->c_c_index;
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +0200704 ss->listener_handle = listen_session_get_handle (ll);
Steven Luongd810a6e2022-10-25 13:09:11 -0700705 session_set_state (ss, SESSION_STATE_CREATED);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800706
707 server_wrk = application_listener_select_worker (ll);
708 ss->app_wrk_index = server_wrk->wrk_index;
709
710 sct->c_s_index = ss->session_index;
711 sct->server_wrk = ss->app_wrk_index;
712
Florin Coras2d0e3de2020-10-23 16:31:40 -0700713 if (ct_init_accepted_session (server_wrk, sct, ss, ll))
Florin Corasba7d8f52019-02-22 13:11:38 -0800714 {
Florin Coras6973c732021-06-17 15:53:38 -0700715 ct_session_connect_notify (ss, SESSION_E_ALLOC);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800716 ct_connection_free (sct);
717 session_free (ss);
Florin Coras2d0e3de2020-10-23 16:31:40 -0700718 return;
Florin Corasba7d8f52019-02-22 13:11:38 -0800719 }
720
Florin Coras98952382021-11-14 15:33:59 -0800721 cct->server_wrk = sct->server_wrk;
Florin Coras6973c732021-06-17 15:53:38 -0700722 cct->seg_ctx_index = sct->seg_ctx_index;
723 cct->ct_seg_index = sct->ct_seg_index;
724 cct->client_rx_fifo = ss->tx_fifo;
725 cct->client_tx_fifo = ss->rx_fifo;
726 cct->client_rx_fifo->refcnt++;
727 cct->client_tx_fifo->refcnt++;
728 cct->segment_handle = sct->segment_handle;
729
Steven Luongd810a6e2022-10-25 13:09:11 -0700730 session_set_state (ss, SESSION_STATE_ACCEPTING);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800731 if (app_worker_accept_notify (server_wrk, ss))
732 {
Florin Coras6973c732021-06-17 15:53:38 -0700733 ct_session_connect_notify (ss, SESSION_E_REFUSED);
Florin Corasda78c5a2021-06-09 14:55:24 -0700734 ct_session_dealloc_fifos (sct, ss->rx_fifo, ss->tx_fifo);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800735 ct_connection_free (sct);
Florin Coras12813d52020-04-09 20:59:20 +0000736 session_free (ss);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800737 }
Florin Coras2d0e3de2020-10-23 16:31:40 -0700738}
739
Florin Corasc215e5a2021-11-15 19:01:52 -0800740static void
741ct_accept_rpc_wrk_handler (void *rpc_args)
742{
743 u32 thread_index, ho_index, n_connects, i, n_pending;
744 const u32 max_connects = 32;
745 ct_worker_t *wrk;
746
747 thread_index = pointer_to_uword (rpc_args);
748 wrk = ct_worker_get (thread_index);
749
750 /* Sub without lock as main enqueues with worker barrier */
751 n_pending = clib_fifo_elts (wrk->pending_connects);
752 n_connects = clib_min (n_pending, max_connects);
753
754 for (i = 0; i < n_connects; i++)
755 {
756 clib_fifo_sub1 (wrk->pending_connects, ho_index);
757 ct_accept_one (thread_index, ho_index);
758 }
759
760 if (n_pending == n_connects)
761 wrk->have_connects = 0;
762 else
763 session_send_rpc_evt_to_thread_force (
764 thread_index, ct_accept_rpc_wrk_handler,
765 uword_to_pointer (thread_index, void *));
766}
767
Florin Coras2d0e3de2020-10-23 16:31:40 -0700768static int
769ct_connect (app_worker_t * client_wrk, session_t * ll,
770 session_endpoint_cfg_t * sep)
771{
Florin Coras374df7a2021-05-13 11:37:43 -0700772 u32 thread_index, ho_index;
Florin Coras2d0e3de2020-10-23 16:31:40 -0700773 ct_main_t *cm = &ct_main;
Florin Coras374df7a2021-05-13 11:37:43 -0700774 ct_connection_t *ho;
Florin Corasc215e5a2021-11-15 19:01:52 -0800775 ct_worker_t *wrk;
Florin Coras2d0e3de2020-10-23 16:31:40 -0700776
777 /* Simple round-robin policy for spreading sessions over workers. We skip
778 * thread index 0, i.e., offset the index by 1, when we have workers as it
779 * is the one dedicated to main thread. Note that n_workers does not include
780 * main thread */
781 cm->n_sessions += 1;
782 thread_index = cm->n_workers ? (cm->n_sessions % cm->n_workers) + 1 : 0;
783
Florin Coras374df7a2021-05-13 11:37:43 -0700784 /*
785 * Alloc and init client half-open transport
786 */
Florin Coras2d0e3de2020-10-23 16:31:40 -0700787
Florin Coras374df7a2021-05-13 11:37:43 -0700788 ho = ct_half_open_alloc ();
789 ho_index = ho->c_c_index;
790 ho->c_rmt_port = sep->port;
791 ho->c_lcl_port = 0;
792 ho->c_is_ip4 = sep->is_ip4;
793 ho->client_opaque = sep->opaque;
794 ho->client_wrk = client_wrk->wrk_index;
795 ho->peer_index = ll->session_index;
796 ho->c_proto = TRANSPORT_PROTO_NONE;
797 ho->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
Florin Corasd5a58822021-05-14 20:11:14 -0700798 clib_memcpy (&ho->c_rmt_ip, &sep->ip, sizeof (sep->ip));
Florin Coras374df7a2021-05-13 11:37:43 -0700799 ho->flags |= CT_CONN_F_CLIENT;
800 ho->c_s_index = ~0;
Florin Corasfeb67022021-11-11 09:24:34 -0800801 ho->actual_tp = sep->original_tp;
Florin Coras374df7a2021-05-13 11:37:43 -0700802
803 /*
804 * Accept connection on thread selected above. Connected reply comes
805 * after server accepts the connection.
806 */
807
Florin Corasc215e5a2021-11-15 19:01:52 -0800808 wrk = ct_worker_get (thread_index);
809
810 /* Worker barrier held, add without additional lock */
811 clib_fifo_add1 (wrk->pending_connects, ho_index);
812 if (!wrk->have_connects)
813 {
814 wrk->have_connects = 1;
815 session_send_rpc_evt_to_thread_force (
816 thread_index, ct_accept_rpc_wrk_handler,
817 uword_to_pointer (thread_index, void *));
818 }
Florin Coras374df7a2021-05-13 11:37:43 -0700819
820 return ho_index;
Florin Corasba7d8f52019-02-22 13:11:38 -0800821}
822
Florin Coras653e43f2019-03-04 10:56:23 -0800823static u32
Florin Coras0bce71e2022-02-10 11:57:06 -0800824ct_start_listen (u32 app_listener_index, transport_endpoint_cfg_t *tep)
Florin Corasd4295e62019-02-22 13:11:38 -0800825{
826 session_endpoint_cfg_t *sep;
827 ct_connection_t *ct;
828
829 sep = (session_endpoint_cfg_t *) tep;
Florin Coras2d0e3de2020-10-23 16:31:40 -0700830 ct = ct_connection_alloc (0);
Florin Corasd4295e62019-02-22 13:11:38 -0800831 ct->server_wrk = sep->app_wrk_index;
832 ct->c_is_ip4 = sep->is_ip4;
833 clib_memcpy (&ct->c_lcl_ip, &sep->ip, sizeof (sep->ip));
834 ct->c_lcl_port = sep->port;
Florin Corasa8375082020-10-25 19:06:57 -0700835 ct->c_s_index = app_listener_index;
Florin Corasd4295e62019-02-22 13:11:38 -0800836 ct->actual_tp = sep->transport_proto;
837 return ct->c_c_index;
838}
839
Florin Coras653e43f2019-03-04 10:56:23 -0800840static u32
Florin Corasd4295e62019-02-22 13:11:38 -0800841ct_stop_listen (u32 ct_index)
842{
843 ct_connection_t *ct;
Florin Coras2d0e3de2020-10-23 16:31:40 -0700844 ct = ct_connection_get (ct_index, 0);
Florin Corasd4295e62019-02-22 13:11:38 -0800845 ct_connection_free (ct);
846 return 0;
847}
848
Florin Coras653e43f2019-03-04 10:56:23 -0800849static transport_connection_t *
Florin Corasd4295e62019-02-22 13:11:38 -0800850ct_listener_get (u32 ct_index)
851{
Florin Coras2d0e3de2020-10-23 16:31:40 -0700852 return (transport_connection_t *) ct_connection_get (ct_index, 0);
Florin Corasd4295e62019-02-22 13:11:38 -0800853}
854
Florin Coras374df7a2021-05-13 11:37:43 -0700855static transport_connection_t *
856ct_half_open_get (u32 ct_index)
857{
858 return (transport_connection_t *) ct_connection_get (ct_index, 0);
859}
860
861static void
862ct_session_cleanup (u32 conn_index, u32 thread_index)
863{
864 ct_connection_t *ct, *peer_ct;
865
866 ct = ct_connection_get (conn_index, thread_index);
867 if (!ct)
868 return;
869
870 peer_ct = ct_connection_get (ct->peer_index, thread_index);
871 if (peer_ct)
872 peer_ct->peer_index = ~0;
873
874 ct_connection_free (ct);
875}
876
877static void
878ct_cleanup_ho (u32 ho_index)
879{
880 ct_connection_free (ct_connection_get (ho_index, 0));
881}
882
Florin Coras653e43f2019-03-04 10:56:23 -0800883static int
Florin Coras2b81e3c2019-02-27 07:55:46 -0800884ct_session_connect (transport_endpoint_cfg_t * tep)
885{
886 session_endpoint_cfg_t *sep_ext;
Florin Coras374df7a2021-05-13 11:37:43 -0700887 session_endpoint_t _sep, *sep = &_sep;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800888 app_worker_t *app_wrk;
889 session_handle_t lh;
890 application_t *app;
891 app_listener_t *al;
892 u32 table_index;
893 session_t *ll;
894 u8 fib_proto;
895
896 sep_ext = (session_endpoint_cfg_t *) tep;
Florin Coras374df7a2021-05-13 11:37:43 -0700897 _sep = *(session_endpoint_t *) tep;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800898 app_wrk = app_worker_get (sep_ext->app_wrk_index);
899 app = application_get (app_wrk->app_index);
900
901 sep->transport_proto = sep_ext->original_tp;
902 table_index = application_local_session_table (app);
903 lh = session_lookup_local_endpoint (table_index, sep);
904 if (lh == SESSION_DROP_HANDLE)
Florin Coras00e01d32019-10-21 16:07:46 -0700905 return SESSION_E_FILTERED;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800906
907 if (lh == SESSION_INVALID_HANDLE)
908 goto global_scope;
909
910 ll = listen_session_get_from_handle (lh);
911 al = app_listener_get_w_session (ll);
912
913 /*
914 * Break loop if rule in local table points to connecting app. This
915 * can happen if client is a generic proxy. Route connect through
916 * global table instead.
917 */
918 if (al->app_index == app->app_index)
919 goto global_scope;
920
921 return ct_connect (app_wrk, ll, sep_ext);
922
923 /*
924 * If nothing found, check the global scope for locally attached
925 * destinations. Make sure first that we're allowed to.
926 */
927
928global_scope:
929 if (session_endpoint_is_local (sep))
Florin Coras00e01d32019-10-21 16:07:46 -0700930 return SESSION_E_NOROUTE;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800931
932 if (!application_has_global_scope (app))
Florin Coras00e01d32019-10-21 16:07:46 -0700933 return SESSION_E_SCOPE;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800934
935 fib_proto = session_endpoint_fib_proto (sep);
Florin Coras95cd8642019-12-30 21:53:19 -0800936 table_index = session_lookup_get_index_for_fib (fib_proto, sep->fib_index);
Florin Coras9f1a5432019-03-10 21:03:20 -0700937 ll = session_lookup_listener_wildcard (table_index, sep);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800938
Florin Coras821b5002021-06-02 21:32:39 -0700939 /* Avoid connecting app to own listener */
940 if (ll && ll->app_index != app->app_index)
Florin Coras2b81e3c2019-02-27 07:55:46 -0800941 return ct_connect (app_wrk, ll, sep_ext);
942
943 /* Failed to connect but no error */
Florin Coras374df7a2021-05-13 11:37:43 -0700944 return SESSION_E_LOCAL_CONNECT;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800945}
946
Florin Coras9439a362021-11-25 16:18:39 -0800947static inline int
948ct_close_is_reset (ct_connection_t *ct, session_t *s)
949{
950 if (ct->flags & CT_CONN_F_CLIENT)
951 return (svm_fifo_max_dequeue (ct->client_rx_fifo) > 0);
952 else
953 return (svm_fifo_max_dequeue (s->rx_fifo) > 0);
954}
955
Florin Coras653e43f2019-03-04 10:56:23 -0800956static void
Florin Coras440c7b52021-11-09 08:38:24 -0800957ct_session_postponed_cleanup (ct_connection_t *ct)
958{
Florin Coras9439a362021-11-25 16:18:39 -0800959 ct_connection_t *peer_ct;
Florin Coras440c7b52021-11-09 08:38:24 -0800960 app_worker_t *app_wrk;
961 session_t *s;
962
963 s = session_get (ct->c_s_index, ct->c_thread_index);
964 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
965
Florin Coras9439a362021-11-25 16:18:39 -0800966 peer_ct = ct_connection_get (ct->peer_index, ct->c_thread_index);
967 if (peer_ct)
968 {
969 if (ct_close_is_reset (ct, s))
970 session_transport_reset_notify (&peer_ct->connection);
971 else
972 session_transport_closing_notify (&peer_ct->connection);
973 }
974 session_transport_closed_notify (&ct->connection);
975
Florin Coras440c7b52021-11-09 08:38:24 -0800976 if (ct->flags & CT_CONN_F_CLIENT)
977 {
978 if (app_wrk)
979 app_worker_cleanup_notify (app_wrk, s, SESSION_CLEANUP_TRANSPORT);
980
981 /* Normal free for client session as the fifos are allocated through
982 * the connects segment manager in a segment that's not shared with
983 * the server */
Florin Coras440c7b52021-11-09 08:38:24 -0800984 ct_session_dealloc_fifos (ct, ct->client_rx_fifo, ct->client_tx_fifo);
Florin Coras98952382021-11-14 15:33:59 -0800985 session_free_w_fifos (s);
Florin Coras440c7b52021-11-09 08:38:24 -0800986 }
987 else
988 {
989 /* Manual session and fifo segment cleanup to avoid implicit
990 * segment manager cleanups and notifications */
991 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
992 if (app_wrk)
993 {
994 app_worker_cleanup_notify (app_wrk, s, SESSION_CLEANUP_TRANSPORT);
995 app_worker_cleanup_notify (app_wrk, s, SESSION_CLEANUP_SESSION);
996 }
997
998 ct_session_dealloc_fifos (ct, s->rx_fifo, s->tx_fifo);
999 session_free (s);
1000 }
1001
1002 ct_connection_free (ct);
1003}
1004
1005static void
1006ct_handle_cleanups (void *args)
1007{
1008 uword thread_index = pointer_to_uword (args);
1009 const u32 max_cleanups = 100;
Florin Coras440c7b52021-11-09 08:38:24 -08001010 ct_cleanup_req_t *req;
1011 ct_connection_t *ct;
1012 u32 n_to_handle = 0;
Florin Coras7fe00692021-11-15 14:01:02 -08001013 ct_worker_t *wrk;
Florin Coras440c7b52021-11-09 08:38:24 -08001014 session_t *s;
1015
Florin Coras7fe00692021-11-15 14:01:02 -08001016 wrk = ct_worker_get (thread_index);
1017 wrk->have_cleanups = 0;
1018 n_to_handle = clib_fifo_elts (wrk->pending_cleanups);
Florin Coras440c7b52021-11-09 08:38:24 -08001019 n_to_handle = clib_min (n_to_handle, max_cleanups);
1020
1021 while (n_to_handle)
1022 {
Florin Coras7fe00692021-11-15 14:01:02 -08001023 clib_fifo_sub2 (wrk->pending_cleanups, req);
Florin Coras440c7b52021-11-09 08:38:24 -08001024 ct = ct_connection_get (req->ct_index, thread_index);
1025 s = session_get (ct->c_s_index, ct->c_thread_index);
1026 if (!svm_fifo_has_event (s->tx_fifo))
1027 ct_session_postponed_cleanup (ct);
1028 else
Florin Coras7fe00692021-11-15 14:01:02 -08001029 clib_fifo_add1 (wrk->pending_cleanups, *req);
Florin Coras440c7b52021-11-09 08:38:24 -08001030 n_to_handle -= 1;
1031 }
1032
Florin Coras7fe00692021-11-15 14:01:02 -08001033 if (clib_fifo_elts (wrk->pending_cleanups))
Florin Coras440c7b52021-11-09 08:38:24 -08001034 {
Florin Coras7fe00692021-11-15 14:01:02 -08001035 wrk->have_cleanups = 1;
Florin Coras440c7b52021-11-09 08:38:24 -08001036 session_send_rpc_evt_to_thread_force (
1037 thread_index, ct_handle_cleanups,
1038 uword_to_pointer (thread_index, void *));
1039 }
1040}
1041
1042static void
1043ct_program_cleanup (ct_connection_t *ct)
1044{
Florin Coras440c7b52021-11-09 08:38:24 -08001045 ct_cleanup_req_t *req;
1046 uword thread_index;
Florin Coras7fe00692021-11-15 14:01:02 -08001047 ct_worker_t *wrk;
Florin Coras440c7b52021-11-09 08:38:24 -08001048
1049 thread_index = ct->c_thread_index;
Florin Coras7fe00692021-11-15 14:01:02 -08001050 wrk = ct_worker_get (ct->c_thread_index);
1051
1052 clib_fifo_add2 (wrk->pending_cleanups, req);
Florin Coras440c7b52021-11-09 08:38:24 -08001053 req->ct_index = ct->c_c_index;
1054
Florin Coras7fe00692021-11-15 14:01:02 -08001055 if (wrk->have_cleanups)
Florin Coras440c7b52021-11-09 08:38:24 -08001056 return;
1057
Florin Coras7fe00692021-11-15 14:01:02 -08001058 wrk->have_cleanups = 1;
Florin Coras440c7b52021-11-09 08:38:24 -08001059 session_send_rpc_evt_to_thread_force (
1060 thread_index, ct_handle_cleanups, uword_to_pointer (thread_index, void *));
1061}
1062
1063static void
Florin Coras2b81e3c2019-02-27 07:55:46 -08001064ct_session_close (u32 ct_index, u32 thread_index)
1065{
1066 ct_connection_t *ct, *peer_ct;
1067 session_t *s;
1068
Florin Coras2d0e3de2020-10-23 16:31:40 -07001069 ct = ct_connection_get (ct_index, thread_index);
Florin Coras6973c732021-06-17 15:53:38 -07001070 s = session_get (ct->c_s_index, ct->c_thread_index);
Florin Coras2d0e3de2020-10-23 16:31:40 -07001071 peer_ct = ct_connection_get (ct->peer_index, thread_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -08001072 if (peer_ct)
1073 {
1074 peer_ct->peer_index = ~0;
Florin Coras06ac4bb2020-10-28 16:41:26 -07001075 /* Make sure session was allocated */
Florin Coras374df7a2021-05-13 11:37:43 -07001076 if (peer_ct->flags & CT_CONN_F_HALF_OPEN)
1077 {
Florin Coras6973c732021-06-17 15:53:38 -07001078 ct_session_connect_notify (s, SESSION_E_REFUSED);
Florin Corasa7bd8262021-11-25 12:14:25 -08001079 ct->peer_index = ~0;
Florin Coras374df7a2021-05-13 11:37:43 -07001080 }
Florin Coras9439a362021-11-25 16:18:39 -08001081 else if (peer_ct->c_s_index == ~0)
Florin Coras440c7b52021-11-09 08:38:24 -08001082 {
1083 /* should not happen */
1084 clib_warning ("ct peer without session");
1085 ct_connection_free (peer_ct);
1086 }
Florin Coras2b81e3c2019-02-27 07:55:46 -08001087 }
1088
Florin Coras440c7b52021-11-09 08:38:24 -08001089 /* Do not send closed notify to make sure pending tx events are
1090 * still delivered and program cleanup */
1091 ct_program_cleanup (ct);
Florin Coras2b81e3c2019-02-27 07:55:46 -08001092}
1093
Florin Coras653e43f2019-03-04 10:56:23 -08001094static transport_connection_t *
Florin Coras2b81e3c2019-02-27 07:55:46 -08001095ct_session_get (u32 ct_index, u32 thread_index)
1096{
Florin Coras2d0e3de2020-10-23 16:31:40 -07001097 return (transport_connection_t *) ct_connection_get (ct_index,
1098 thread_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -08001099}
1100
Florin Corasd4295e62019-02-22 13:11:38 -08001101static u8 *
1102format_ct_connection_id (u8 * s, va_list * args)
1103{
1104 ct_connection_t *ct = va_arg (*args, ct_connection_t *);
1105 if (!ct)
1106 return s;
1107 if (ct->c_is_ip4)
1108 {
1109 s = format (s, "[%d:%d][CT:%U] %U:%d->%U:%d", ct->c_thread_index,
1110 ct->c_s_index, format_transport_proto_short, ct->actual_tp,
1111 format_ip4_address, &ct->c_lcl_ip4,
1112 clib_net_to_host_u16 (ct->c_lcl_port), format_ip4_address,
1113 &ct->c_rmt_ip4, clib_net_to_host_u16 (ct->c_rmt_port));
1114 }
1115 else
1116 {
1117 s = format (s, "[%d:%d][CT:%U] %U:%d->%U:%d", ct->c_thread_index,
1118 ct->c_s_index, format_transport_proto_short, ct->actual_tp,
1119 format_ip6_address, &ct->c_lcl_ip6,
1120 clib_net_to_host_u16 (ct->c_lcl_port), format_ip6_address,
1121 &ct->c_rmt_ip6, clib_net_to_host_u16 (ct->c_rmt_port));
1122 }
1123
1124 return s;
1125}
1126
Florin Corasf940f8a2019-03-06 10:44:38 -08001127static int
Florin Coras9f86d222020-03-23 15:34:22 +00001128ct_custom_tx (void *session, transport_send_params_t * sp)
Florin Corasf940f8a2019-03-06 10:44:38 -08001129{
1130 session_t *s = (session_t *) session;
Florin Coras074f3972021-12-07 15:12:06 -08001131 if (session_has_transport (s))
Florin Corasf940f8a2019-03-06 10:44:38 -08001132 return 0;
Florin Coras1b9d2c22021-01-26 14:10:43 -08001133 /* If event enqueued towards peer, remove from scheduler and remove
1134 * session tx flag, i.e., accept new tx events. Unset fifo flag now to
1135 * avoid missing events if peer did not clear fifo flag yet, which is
1136 * interpreted as successful notification and session is descheduled. */
1137 svm_fifo_unset_event (s->tx_fifo);
Florin Corasd6894562020-10-28 00:37:15 -07001138 if (!ct_session_tx (s))
Florin Coras1b9d2c22021-01-26 14:10:43 -08001139 sp->flags = TRANSPORT_SND_F_DESCHED;
1140
Florin Corasd6894562020-10-28 00:37:15 -07001141 /* The scheduler uses packet count as a means of upper bounding the amount
1142 * of work done per dispatch. So make it look like we have sent something */
1143 return 1;
Florin Corasf940f8a2019-03-06 10:44:38 -08001144}
1145
Florin Coras317b8e02019-04-17 09:57:46 -07001146static int
1147ct_app_rx_evt (transport_connection_t * tc)
1148{
1149 ct_connection_t *ct = (ct_connection_t *) tc, *peer_ct;
Florin Coras9439a362021-11-25 16:18:39 -08001150 session_t *ps, *s;
Florin Coras317b8e02019-04-17 09:57:46 -07001151
Florin Coras9439a362021-11-25 16:18:39 -08001152 s = session_get (ct->c_s_index, ct->c_thread_index);
1153 if (session_has_transport (s) || s->session_state < SESSION_STATE_READY)
1154 return -1;
Florin Coras2d0e3de2020-10-23 16:31:40 -07001155 peer_ct = ct_connection_get (ct->peer_index, tc->thread_index);
Florin Coras9439a362021-11-25 16:18:39 -08001156 if (!peer_ct || (peer_ct->flags & CT_CONN_F_HALF_OPEN))
Florin Coras317b8e02019-04-17 09:57:46 -07001157 return -1;
1158 ps = session_get (peer_ct->c_s_index, peer_ct->c_thread_index);
Florin Coras9439a362021-11-25 16:18:39 -08001159 if (ps->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
1160 return -1;
Florin Coras317b8e02019-04-17 09:57:46 -07001161 return session_dequeue_notify (ps);
1162}
1163
Florin Coras653e43f2019-03-04 10:56:23 -08001164static u8 *
Florin Corasd4295e62019-02-22 13:11:38 -08001165format_ct_listener (u8 * s, va_list * args)
1166{
1167 u32 tc_index = va_arg (*args, u32);
Aloys Augustina0abbff2019-07-12 12:16:16 +02001168 u32 __clib_unused thread_index = va_arg (*args, u32);
Florin Corasd4295e62019-02-22 13:11:38 -08001169 u32 __clib_unused verbose = va_arg (*args, u32);
Florin Coras2d0e3de2020-10-23 16:31:40 -07001170 ct_connection_t *ct = ct_connection_get (tc_index, 0);
Florin Coras7bf6ed62020-09-23 12:02:08 -07001171 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_ct_connection_id, ct);
Florin Corasd4295e62019-02-22 13:11:38 -08001172 if (verbose)
Florin Coras7bf6ed62020-09-23 12:02:08 -07001173 s = format (s, "%-" SESSION_CLI_STATE_LEN "s", "LISTEN");
Florin Corasd4295e62019-02-22 13:11:38 -08001174 return s;
1175}
1176
Florin Coras653e43f2019-03-04 10:56:23 -08001177static u8 *
Florin Coras374df7a2021-05-13 11:37:43 -07001178format_ct_half_open (u8 *s, va_list *args)
1179{
1180 u32 ho_index = va_arg (*args, u32);
1181 u32 verbose = va_arg (*args, u32);
1182 ct_connection_t *ct = ct_connection_get (ho_index, 0);
1183 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_ct_connection_id, ct);
1184 if (verbose)
1185 s = format (s, "%-" SESSION_CLI_STATE_LEN "s", "HALF-OPEN");
1186 return s;
1187}
1188
1189static u8 *
Florin Coras2b81e3c2019-02-27 07:55:46 -08001190format_ct_connection (u8 * s, va_list * args)
1191{
1192 ct_connection_t *ct = va_arg (*args, ct_connection_t *);
1193 u32 verbose = va_arg (*args, u32);
1194
1195 if (!ct)
1196 return s;
Florin Coras7bf6ed62020-09-23 12:02:08 -07001197 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_ct_connection_id, ct);
Florin Coras2b81e3c2019-02-27 07:55:46 -08001198 if (verbose)
1199 {
Florin Coras7bf6ed62020-09-23 12:02:08 -07001200 s = format (s, "%-" SESSION_CLI_STATE_LEN "s", "ESTABLISHED");
Florin Coras2b81e3c2019-02-27 07:55:46 -08001201 if (verbose > 1)
1202 {
1203 s = format (s, "\n");
1204 }
1205 }
1206 return s;
1207}
1208
Florin Coras653e43f2019-03-04 10:56:23 -08001209static u8 *
Florin Coras2b81e3c2019-02-27 07:55:46 -08001210format_ct_session (u8 * s, va_list * args)
1211{
1212 u32 ct_index = va_arg (*args, u32);
Florin Coras2d0e3de2020-10-23 16:31:40 -07001213 u32 thread_index = va_arg (*args, u32);
Florin Coras2b81e3c2019-02-27 07:55:46 -08001214 u32 verbose = va_arg (*args, u32);
1215 ct_connection_t *ct;
1216
Florin Coras2d0e3de2020-10-23 16:31:40 -07001217 ct = ct_connection_get (ct_index, thread_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -08001218 if (!ct)
1219 {
1220 s = format (s, "empty\n");
1221 return s;
1222 }
1223
1224 s = format (s, "%U", format_ct_connection, ct, verbose);
1225 return s;
1226}
1227
Florin Coras2d0e3de2020-10-23 16:31:40 -07001228clib_error_t *
1229ct_enable_disable (vlib_main_t * vm, u8 is_en)
1230{
Florin Coras7fe00692021-11-15 14:01:02 -08001231 vlib_thread_main_t *vtm = &vlib_thread_main;
Florin Coras374df7a2021-05-13 11:37:43 -07001232 ct_main_t *cm = &ct_main;
1233
1234 cm->n_workers = vlib_num_workers ();
Florin Coras7fe00692021-11-15 14:01:02 -08001235 vec_validate (cm->wrk, vtm->n_vlib_mains);
Florin Coras374df7a2021-05-13 11:37:43 -07001236 clib_spinlock_init (&cm->ho_reuseable_lock);
Florin Corasda78c5a2021-06-09 14:55:24 -07001237 clib_rwlock_init (&cm->app_segs_lock);
Florin Coras2d0e3de2020-10-23 16:31:40 -07001238 return 0;
1239}
1240
Florin Corasd4295e62019-02-22 13:11:38 -08001241/* *INDENT-OFF* */
Nathan Skrzypczake971bc92019-06-19 13:42:37 +02001242static const transport_proto_vft_t cut_thru_proto = {
Florin Coras2d0e3de2020-10-23 16:31:40 -07001243 .enable = ct_enable_disable,
Florin Corasd4295e62019-02-22 13:11:38 -08001244 .start_listen = ct_start_listen,
1245 .stop_listen = ct_stop_listen,
Florin Coras374df7a2021-05-13 11:37:43 -07001246 .get_connection = ct_session_get,
Florin Corasd4295e62019-02-22 13:11:38 -08001247 .get_listener = ct_listener_get,
Florin Coras374df7a2021-05-13 11:37:43 -07001248 .get_half_open = ct_half_open_get,
1249 .cleanup = ct_session_cleanup,
1250 .cleanup_ho = ct_cleanup_ho,
Florin Coras2b81e3c2019-02-27 07:55:46 -08001251 .connect = ct_session_connect,
1252 .close = ct_session_close,
Florin Corasf940f8a2019-03-06 10:44:38 -08001253 .custom_tx = ct_custom_tx,
Florin Coras317b8e02019-04-17 09:57:46 -07001254 .app_rx_evt = ct_app_rx_evt,
Florin Corasd4295e62019-02-22 13:11:38 -08001255 .format_listener = format_ct_listener,
Florin Coras374df7a2021-05-13 11:37:43 -07001256 .format_half_open = format_ct_half_open,
Florin Coras2b81e3c2019-02-27 07:55:46 -08001257 .format_connection = format_ct_session,
Nathan Skrzypczake971bc92019-06-19 13:42:37 +02001258 .transport_options = {
Florin Coras07063b82020-03-13 04:44:51 +00001259 .name = "ct",
1260 .short_name = "C",
Nathan Skrzypczake971bc92019-06-19 13:42:37 +02001261 .tx_type = TRANSPORT_TX_INTERNAL,
Florin Coras374df7a2021-05-13 11:37:43 -07001262 .service_type = TRANSPORT_SERVICE_VC,
Nathan Skrzypczake971bc92019-06-19 13:42:37 +02001263 },
Florin Corasd4295e62019-02-22 13:11:38 -08001264};
1265/* *INDENT-ON* */
1266
Florin Coras074f3972021-12-07 15:12:06 -08001267static inline int
1268ct_session_can_tx (session_t *s)
1269{
1270 return (s->session_state == SESSION_STATE_READY ||
1271 s->session_state == SESSION_STATE_CLOSING ||
1272 s->session_state == SESSION_STATE_APP_CLOSED);
1273}
1274
Florin Coras653e43f2019-03-04 10:56:23 -08001275int
1276ct_session_tx (session_t * s)
1277{
1278 ct_connection_t *ct, *peer_ct;
1279 session_t *peer_s;
1280
Florin Coras074f3972021-12-07 15:12:06 -08001281 if (!ct_session_can_tx (s))
1282 return 0;
Florin Coras653e43f2019-03-04 10:56:23 -08001283 ct = (ct_connection_t *) session_get_transport (s);
Florin Coras2d0e3de2020-10-23 16:31:40 -07001284 peer_ct = ct_connection_get (ct->peer_index, ct->c_thread_index);
Florin Coras653e43f2019-03-04 10:56:23 -08001285 if (!peer_ct)
Florin Corasbcdc50d2020-08-25 19:07:02 -07001286 return 0;
Florin Coras2d0e3de2020-10-23 16:31:40 -07001287 peer_s = session_get (peer_ct->c_s_index, peer_ct->c_thread_index);
Florin Coras653e43f2019-03-04 10:56:23 -08001288 if (peer_s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
1289 return 0;
Florin Corasd6894562020-10-28 00:37:15 -07001290 return session_enqueue_notify (peer_s);
Florin Coras653e43f2019-03-04 10:56:23 -08001291}
1292
Florin Corasd4295e62019-02-22 13:11:38 -08001293static clib_error_t *
1294ct_transport_init (vlib_main_t * vm)
1295{
1296 transport_register_protocol (TRANSPORT_PROTO_NONE, &cut_thru_proto,
1297 FIB_PROTOCOL_IP4, ~0);
Florin Coras653e43f2019-03-04 10:56:23 -08001298 transport_register_protocol (TRANSPORT_PROTO_NONE, &cut_thru_proto,
1299 FIB_PROTOCOL_IP6, ~0);
Florin Corasd4295e62019-02-22 13:11:38 -08001300 return 0;
1301}
1302
1303VLIB_INIT_FUNCTION (ct_transport_init);
1304
Florin Corasba7d8f52019-02-22 13:11:38 -08001305/*
1306 * fd.io coding-style-patch-verification: ON
1307 *
1308 * Local Variables:
1309 * eval: (c-set-style "gnu")
1310 * End:
1311 */