blob: 83def8eb73087108633f4a704f96ad3ae6aeecb3 [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 Coras7fe00692021-11-15 14:01:02 -080084 pool_get_zero (wrk->connections, ct);
85 ct->c_c_index = ct - wrk->connections;
Florin Coras2d0e3de2020-10-23 16:31:40 -070086 ct->c_thread_index = thread_index;
Florin Corasd4295e62019-02-22 13:11:38 -080087 ct->client_wrk = ~0;
88 ct->server_wrk = ~0;
Florin Corasda78c5a2021-06-09 14:55:24 -070089 ct->seg_ctx_index = ~0;
90 ct->ct_seg_index = ~0;
Florin Corasd4295e62019-02-22 13:11:38 -080091 return ct;
92}
93
Florin Coras653e43f2019-03-04 10:56:23 -080094static ct_connection_t *
Florin Coras2d0e3de2020-10-23 16:31:40 -070095ct_connection_get (u32 ct_index, u32 thread_index)
Florin Corasd4295e62019-02-22 13:11:38 -080096{
Florin Coras7fe00692021-11-15 14:01:02 -080097 ct_worker_t *wrk = ct_worker_get (thread_index);
98
99 if (pool_is_free_index (wrk->connections, ct_index))
Florin Corasd4295e62019-02-22 13:11:38 -0800100 return 0;
Florin Coras7fe00692021-11-15 14:01:02 -0800101 return pool_elt_at_index (wrk->connections, ct_index);
Florin Corasba7d8f52019-02-22 13:11:38 -0800102}
103
Florin Coras653e43f2019-03-04 10:56:23 -0800104static void
Florin Corasd4295e62019-02-22 13:11:38 -0800105ct_connection_free (ct_connection_t * ct)
Florin Corasba7d8f52019-02-22 13:11:38 -0800106{
Florin Coras7fe00692021-11-15 14:01:02 -0800107 ct_worker_t *wrk = ct_worker_get (ct->c_thread_index);
108
Florin Corasba7d8f52019-02-22 13:11:38 -0800109 if (CLIB_DEBUG)
Florin Coras2d0e3de2020-10-23 16:31:40 -0700110 {
Florin Coras7fe00692021-11-15 14:01:02 -0800111 clib_memset (ct, 0xfc, sizeof (*ct));
112 pool_put (wrk->connections, ct);
Florin Coras2d0e3de2020-10-23 16:31:40 -0700113 return;
114 }
Florin Coras7fe00692021-11-15 14:01:02 -0800115 pool_put (wrk->connections, ct);
Florin Corasba7d8f52019-02-22 13:11:38 -0800116}
117
Florin Coras374df7a2021-05-13 11:37:43 -0700118static ct_connection_t *
119ct_half_open_alloc (void)
120{
121 ct_main_t *cm = &ct_main;
122 u32 *hip;
123
124 clib_spinlock_lock (&cm->ho_reuseable_lock);
125 vec_foreach (hip, cm->ho_reusable)
Florin Coras7fe00692021-11-15 14:01:02 -0800126 pool_put_index (cm->wrk[0].connections, *hip);
Florin Coras374df7a2021-05-13 11:37:43 -0700127 vec_reset_length (cm->ho_reusable);
128 clib_spinlock_unlock (&cm->ho_reuseable_lock);
129
130 return ct_connection_alloc (0);
131}
132
133void
134ct_half_open_add_reusable (u32 ho_index)
135{
136 ct_main_t *cm = &ct_main;
137
138 clib_spinlock_lock (&cm->ho_reuseable_lock);
139 vec_add1 (cm->ho_reusable, ho_index);
140 clib_spinlock_unlock (&cm->ho_reuseable_lock);
141}
142
Florin Coras2b81e3c2019-02-27 07:55:46 -0800143session_t *
144ct_session_get_peer (session_t * s)
145{
146 ct_connection_t *ct, *peer_ct;
Florin Coras2d0e3de2020-10-23 16:31:40 -0700147 ct = ct_connection_get (s->connection_index, s->thread_index);
148 peer_ct = ct_connection_get (ct->peer_index, s->thread_index);
149 return session_get (peer_ct->c_s_index, s->thread_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800150}
151
Florin Corasba7d8f52019-02-22 13:11:38 -0800152void
Florin Coras2b81e3c2019-02-27 07:55:46 -0800153ct_session_endpoint (session_t * ll, session_endpoint_t * sep)
Florin Corasba7d8f52019-02-22 13:11:38 -0800154{
Florin Corasd4295e62019-02-22 13:11:38 -0800155 ct_connection_t *ct;
156 ct = (ct_connection_t *) session_get_transport (ll);
157 sep->transport_proto = ct->actual_tp;
158 sep->port = ct->c_lcl_port;
159 sep->is_ip4 = ct->c_is_ip4;
Florin Corasea7e7082020-07-07 17:57:28 -0700160 ip_copy (&sep->ip, &ct->c_lcl_ip, ct->c_is_ip4);
Florin Corasba7d8f52019-02-22 13:11:38 -0800161}
162
Florin Corasda78c5a2021-06-09 14:55:24 -0700163static void
Florin Coras98952382021-11-14 15:33:59 -0800164ct_set_invalid_app_wrk (ct_connection_t *ct, u8 is_client)
165{
166 ct_connection_t *peer_ct;
167
168 peer_ct = ct_connection_get (ct->peer_index, ct->c_thread_index);
169
170 if (is_client)
171 {
172 ct->client_wrk = APP_INVALID_INDEX;
173 if (peer_ct)
174 ct->client_wrk = APP_INVALID_INDEX;
175 }
176 else
177 {
178 ct->server_wrk = APP_INVALID_INDEX;
179 if (peer_ct)
180 ct->server_wrk = APP_INVALID_INDEX;
181 }
182}
183
184static void
Florin Corasda78c5a2021-06-09 14:55:24 -0700185ct_session_dealloc_fifos (ct_connection_t *ct, svm_fifo_t *rx_fifo,
186 svm_fifo_t *tx_fifo)
Florin Corasba7d8f52019-02-22 13:11:38 -0800187{
Florin Corasda78c5a2021-06-09 14:55:24 -0700188 ct_segments_ctx_t *seg_ctx;
189 ct_main_t *cm = &ct_main;
Florin Corasba7d8f52019-02-22 13:11:38 -0800190 segment_manager_t *sm;
Florin Corasda78c5a2021-06-09 14:55:24 -0700191 app_worker_t *app_wrk;
192 ct_segment_t *ct_seg;
193 fifo_segment_t *fs;
194 u32 seg_index;
Florin Coras98952382021-11-14 15:33:59 -0800195 session_t *s;
Florin Coras6973c732021-06-17 15:53:38 -0700196 int cnt;
Florin Corasda78c5a2021-06-09 14:55:24 -0700197
198 /*
199 * Cleanup fifos
200 */
201
202 sm = segment_manager_get (rx_fifo->segment_manager);
203 seg_index = rx_fifo->segment_index;
204
205 fs = segment_manager_get_segment_w_lock (sm, seg_index);
206 fifo_segment_free_fifo (fs, rx_fifo);
207 fifo_segment_free_fifo (fs, tx_fifo);
208 segment_manager_segment_reader_unlock (sm);
209
210 /*
Florin Coras6973c732021-06-17 15:53:38 -0700211 * Atomically update segment context with readers lock
Florin Corasda78c5a2021-06-09 14:55:24 -0700212 */
213
214 clib_rwlock_reader_lock (&cm->app_segs_lock);
215
216 seg_ctx = pool_elt_at_index (cm->app_seg_ctxs, ct->seg_ctx_index);
217 ct_seg = pool_elt_at_index (seg_ctx->segments, ct->ct_seg_index);
218
219 if (ct->flags & CT_CONN_F_CLIENT)
220 {
221 cnt =
222 __atomic_sub_fetch (&ct_seg->client_n_sessions, 1, __ATOMIC_RELAXED);
Florin Corasda78c5a2021-06-09 14:55:24 -0700223 }
224 else
225 {
226 cnt =
227 __atomic_sub_fetch (&ct_seg->server_n_sessions, 1, __ATOMIC_RELAXED);
Florin Corasda78c5a2021-06-09 14:55:24 -0700228 }
229
Florin Corasda78c5a2021-06-09 14:55:24 -0700230 clib_rwlock_reader_unlock (&cm->app_segs_lock);
231
232 /*
233 * No need to do any app updates, return
234 */
Florin Coras6973c732021-06-17 15:53:38 -0700235 ASSERT (cnt >= 0);
236 if (cnt)
237 return;
238
239 /*
240 * Grab exclusive lock and update flags unless some other thread
241 * added more sessions
242 */
243 clib_rwlock_writer_lock (&cm->app_segs_lock);
244
245 seg_ctx = pool_elt_at_index (cm->app_seg_ctxs, ct->seg_ctx_index);
246 ct_seg = pool_elt_at_index (seg_ctx->segments, ct->ct_seg_index);
247 if (ct->flags & CT_CONN_F_CLIENT)
248 {
249 cnt = ct_seg->client_n_sessions;
Florin Coras98952382021-11-14 15:33:59 -0800250 if (cnt)
251 goto done;
252 ct_seg->flags |= CT_SEGMENT_F_CLIENT_DETACHED;
253 s = session_get (ct->c_s_index, ct->c_thread_index);
254 if (s->app_wrk_index == APP_INVALID_INDEX)
255 ct_set_invalid_app_wrk (ct, 1 /* is_client */);
Florin Coras6973c732021-06-17 15:53:38 -0700256 }
257 else
258 {
259 cnt = ct_seg->server_n_sessions;
Florin Coras98952382021-11-14 15:33:59 -0800260 if (cnt)
261 goto done;
262 ct_seg->flags |= CT_SEGMENT_F_SERVER_DETACHED;
263 s = session_get (ct->c_s_index, ct->c_thread_index);
264 if (s->app_wrk_index == APP_INVALID_INDEX)
265 ct_set_invalid_app_wrk (ct, 0 /* is_client */);
Florin Coras6973c732021-06-17 15:53:38 -0700266 }
267
Florin Coras98952382021-11-14 15:33:59 -0800268 if (!(ct_seg->flags & CT_SEGMENT_F_CLIENT_DETACHED) ||
269 !(ct_seg->flags & CT_SEGMENT_F_SERVER_DETACHED))
270 goto done;
271
Florin Coras6973c732021-06-17 15:53:38 -0700272 /*
273 * Remove segment context because both client and server detached
274 */
275
Florin Coras98952382021-11-14 15:33:59 -0800276 pool_put_index (seg_ctx->segments, ct->ct_seg_index);
Florin Coras6973c732021-06-17 15:53:38 -0700277
278 /*
Florin Coras98952382021-11-14 15:33:59 -0800279 * No more segment indices left, remove the segments context
Florin Coras6973c732021-06-17 15:53:38 -0700280 */
Florin Coras98952382021-11-14 15:33:59 -0800281 if (!pool_elts (seg_ctx->segments))
Florin Corasda78c5a2021-06-09 14:55:24 -0700282 {
Florin Coras98952382021-11-14 15:33:59 -0800283 u64 table_handle = seg_ctx->client_wrk << 16 | seg_ctx->server_wrk;
284 table_handle = (u64) seg_ctx->sm_index << 32 | table_handle;
285 hash_unset (cm->app_segs_ctxs_table, table_handle);
286 pool_free (seg_ctx->segments);
287 pool_put_index (cm->app_seg_ctxs, ct->seg_ctx_index);
Florin Corasda78c5a2021-06-09 14:55:24 -0700288 }
Florin Coras98952382021-11-14 15:33:59 -0800289
290 /*
291 * Segment to be removed so notify both apps
292 */
293
294 app_wrk = app_worker_get_if_valid (ct->client_wrk);
295 /* Determine if client app still needs notification, i.e., if it is
296 * still attached. If client detached and this is the last ct session
297 * on this segment, then its connects segment manager should also be
298 * detached, so do not send notification */
299 if (app_wrk)
300 {
301 segment_manager_t *csm;
302 csm = app_worker_get_connect_segment_manager (app_wrk);
303 if (!segment_manager_app_detached (csm))
304 app_worker_del_segment_notify (app_wrk, ct->segment_handle);
305 }
306
Florin Coras13987da2021-12-07 14:47:12 -0800307 /* Notify server app and free segment */
Florin Corasda78c5a2021-06-09 14:55:24 -0700308 segment_manager_lock_and_del_segment (sm, seg_index);
309
310 /* Cleanup segment manager if needed. If server detaches there's a chance
311 * the client's sessions will hold up segment removal */
312 if (segment_manager_app_detached (sm) && !segment_manager_has_fifos (sm))
313 segment_manager_free_safe (sm);
Florin Coras98952382021-11-14 15:33:59 -0800314
315done:
316
317 clib_rwlock_writer_unlock (&cm->app_segs_lock);
Florin Corasda78c5a2021-06-09 14:55:24 -0700318}
319
Florin Corasa7bd8262021-11-25 12:14:25 -0800320static void
321ct_session_force_disconnect_server (ct_connection_t *sct)
322{
323 sct->peer_index = ~0;
324 session_transport_closing_notify (&sct->connection);
325}
326
Florin Corasda78c5a2021-06-09 14:55:24 -0700327int
Florin Coras6973c732021-06-17 15:53:38 -0700328ct_session_connect_notify (session_t *ss, session_error_t err)
Florin Corasda78c5a2021-06-09 14:55:24 -0700329{
Florin Coras6973c732021-06-17 15:53:38 -0700330 u32 ss_index, opaque, thread_index;
Florin Corasda78c5a2021-06-09 14:55:24 -0700331 ct_connection_t *sct, *cct;
Florin Corasda78c5a2021-06-09 14:55:24 -0700332 app_worker_t *client_wrk;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800333 session_t *cs;
Florin Corasba7d8f52019-02-22 13:11:38 -0800334
Florin Coras2b81e3c2019-02-27 07:55:46 -0800335 ss_index = ss->session_index;
Florin Coras2d0e3de2020-10-23 16:31:40 -0700336 thread_index = ss->thread_index;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800337 sct = (ct_connection_t *) session_get_transport (ss);
338 client_wrk = app_worker_get (sct->client_wrk);
Florin Corasdd7d24b2020-08-14 17:51:13 -0700339 opaque = sct->client_opaque;
Florin Corasba7d8f52019-02-22 13:11:38 -0800340
Florin Coras2d0e3de2020-10-23 16:31:40 -0700341 cct = ct_connection_get (sct->peer_index, thread_index);
Florin Corasba7d8f52019-02-22 13:11:38 -0800342
Florin Coras374df7a2021-05-13 11:37:43 -0700343 /* Client closed while waiting for reply from server */
Florin Coras6973c732021-06-17 15:53:38 -0700344 if (PREDICT_FALSE (!cct))
Florin Coras374df7a2021-05-13 11:37:43 -0700345 {
Florin Corasa7bd8262021-11-25 12:14:25 -0800346 ct_session_force_disconnect_server (sct);
Florin Coras374df7a2021-05-13 11:37:43 -0700347 return 0;
348 }
349
350 session_half_open_delete_notify (&cct->connection);
351 cct->flags &= ~CT_CONN_F_HALF_OPEN;
352
Florin Coras6973c732021-06-17 15:53:38 -0700353 if (PREDICT_FALSE (err))
354 goto connect_error;
Florin Corasda78c5a2021-06-09 14:55:24 -0700355
356 /*
Florin Corasa7bd8262021-11-25 12:14:25 -0800357 * Alloc client session, server session assumed to be established
Florin Corasda78c5a2021-06-09 14:55:24 -0700358 */
359
Florin Corasa7bd8262021-11-25 12:14:25 -0800360 ASSERT (ss->session_state >= SESSION_STATE_READY);
361
Florin Coras2d0e3de2020-10-23 16:31:40 -0700362 cs = session_alloc (thread_index);
363 ss = session_get (ss_index, thread_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800364 cs->session_type = ss->session_type;
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +0200365 cs->listener_handle = SESSION_INVALID_HANDLE;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800366 cs->session_state = SESSION_STATE_CONNECTING;
367 cs->app_wrk_index = client_wrk->wrk_index;
368 cs->connection_index = cct->c_c_index;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800369 cct->c_s_index = cs->session_index;
370
371 /* This will allocate fifos for the session. They won't be used for
372 * exchanging data but they will be used to close the connection if
373 * the segment manager/worker is freed */
Florin Corasdd7d24b2020-08-14 17:51:13 -0700374 if ((err = app_worker_init_connected (client_wrk, cs)))
Florin Coras2b81e3c2019-02-27 07:55:46 -0800375 {
Florin Corasdd7d24b2020-08-14 17:51:13 -0700376 session_free (cs);
Florin Corasa7bd8262021-11-25 12:14:25 -0800377 ct_session_force_disconnect_server (sct);
Florin Coras6973c732021-06-17 15:53:38 -0700378 err = SESSION_E_ALLOC;
379 goto connect_error;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800380 }
381
Florin Corasdd7d24b2020-08-14 17:51:13 -0700382 cs->session_state = SESSION_STATE_CONNECTING;
383
Florin Coras6973c732021-06-17 15:53:38 -0700384 if (app_worker_connect_notify (client_wrk, cs, 0, opaque))
Florin Coras2b81e3c2019-02-27 07:55:46 -0800385 {
Florin Coras6973c732021-06-17 15:53:38 -0700386 segment_manager_dealloc_fifos (cs->rx_fifo, cs->tx_fifo);
Florin Corasdd7d24b2020-08-14 17:51:13 -0700387 session_free (cs);
Florin Corasa7bd8262021-11-25 12:14:25 -0800388 ct_session_force_disconnect_server (sct);
Florin Coras6973c732021-06-17 15:53:38 -0700389 goto cleanup_client;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800390 }
391
Florin Coras2d0e3de2020-10-23 16:31:40 -0700392 cs = session_get (cct->c_s_index, cct->c_thread_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800393 cs->session_state = SESSION_STATE_READY;
394
Florin Corasba7d8f52019-02-22 13:11:38 -0800395 return 0;
Florin Corasdd7d24b2020-08-14 17:51:13 -0700396
Florin Coras6973c732021-06-17 15:53:38 -0700397connect_error:
398
399 app_worker_connect_notify (client_wrk, 0, err, cct->client_opaque);
400
401cleanup_client:
402
403 if (cct->client_rx_fifo)
404 ct_session_dealloc_fifos (cct, cct->client_rx_fifo, cct->client_tx_fifo);
405 ct_connection_free (cct);
Florin Corasdd7d24b2020-08-14 17:51:13 -0700406 return -1;
Florin Corasba7d8f52019-02-22 13:11:38 -0800407}
408
Florin Coras6973c732021-06-17 15:53:38 -0700409static inline ct_segment_t *
410ct_lookup_free_segment (ct_main_t *cm, segment_manager_t *sm,
411 u32 seg_ctx_index)
Florin Corasba7d8f52019-02-22 13:11:38 -0800412{
Florin Corasda78c5a2021-06-09 14:55:24 -0700413 uword free_bytes, max_free_bytes;
414 ct_segment_t *ct_seg, *res = 0;
Florin Coras6973c732021-06-17 15:53:38 -0700415 ct_segments_ctx_t *seg_ctx;
Florin Corasda78c5a2021-06-09 14:55:24 -0700416 fifo_segment_t *fs;
417 u32 max_fifos;
418
Florin Coras6973c732021-06-17 15:53:38 -0700419 seg_ctx = pool_elt_at_index (cm->app_seg_ctxs, seg_ctx_index);
420 max_free_bytes = seg_ctx->fifo_pair_bytes;
421
Florin Corasda78c5a2021-06-09 14:55:24 -0700422 pool_foreach (ct_seg, seg_ctx->segments)
423 {
424 /* Client or server has detached so segment cannot be used */
Florin Corasda78c5a2021-06-09 14:55:24 -0700425 fs = segment_manager_get_segment (sm, ct_seg->segment_index);
426 free_bytes = fifo_segment_available_bytes (fs);
Florin Coras6973c732021-06-17 15:53:38 -0700427 max_fifos = fifo_segment_size (fs) / seg_ctx->fifo_pair_bytes;
Florin Corasda78c5a2021-06-09 14:55:24 -0700428 if (free_bytes > max_free_bytes &&
429 fifo_segment_num_fifos (fs) / 2 < max_fifos)
430 {
431 max_free_bytes = free_bytes;
432 res = ct_seg;
433 }
434 }
435
436 return res;
437}
438
Florin Coras6973c732021-06-17 15:53:38 -0700439static ct_segment_t *
440ct_alloc_segment (ct_main_t *cm, app_worker_t *server_wrk, u64 table_handle,
441 segment_manager_t *sm, u32 client_wrk_index)
442{
443 u32 seg_ctx_index = ~0, sm_index, pair_bytes;
444 segment_manager_props_t *props;
445 const u32 margin = 16 << 10;
446 ct_segments_ctx_t *seg_ctx;
447 app_worker_t *client_wrk;
448 u64 seg_size, seg_handle;
449 application_t *server;
450 ct_segment_t *ct_seg;
451 uword *spp;
452 int fs_index;
453
454 server = application_get (server_wrk->app_index);
455 props = application_segment_manager_properties (server);
456 sm_index = segment_manager_index (sm);
457 pair_bytes = props->rx_fifo_size + props->tx_fifo_size + margin;
458
459 /*
460 * Make sure another thread did not alloc a segment while acquiring the lock
461 */
462
463 spp = hash_get (cm->app_segs_ctxs_table, table_handle);
464 if (spp)
465 {
466 seg_ctx_index = *spp;
467 ct_seg = ct_lookup_free_segment (cm, sm, seg_ctx_index);
468 if (ct_seg)
469 return ct_seg;
470 }
471
472 /*
473 * No segment, try to alloc one and notify the server and the client.
474 * Make sure the segment is not used for other fifos
475 */
476 seg_size = clib_max (props->segment_size, 128 << 20);
477 fs_index =
478 segment_manager_add_segment2 (sm, seg_size, FIFO_SEGMENT_F_CUSTOM_USE);
479 if (fs_index < 0)
480 return 0;
481
482 if (seg_ctx_index == ~0)
483 {
484 pool_get_zero (cm->app_seg_ctxs, seg_ctx);
485 seg_ctx_index = seg_ctx - cm->app_seg_ctxs;
486 hash_set (cm->app_segs_ctxs_table, table_handle, seg_ctx_index);
487 seg_ctx->server_wrk = server_wrk->wrk_index;
488 seg_ctx->client_wrk = client_wrk_index;
489 seg_ctx->sm_index = sm_index;
490 seg_ctx->fifo_pair_bytes = pair_bytes;
491 }
492 else
493 {
494 seg_ctx = pool_elt_at_index (cm->app_seg_ctxs, seg_ctx_index);
495 }
496
497 pool_get_zero (seg_ctx->segments, ct_seg);
498 ct_seg->segment_index = fs_index;
499 ct_seg->server_n_sessions = 0;
500 ct_seg->client_n_sessions = 0;
501 ct_seg->ct_seg_index = ct_seg - seg_ctx->segments;
502 ct_seg->seg_ctx_index = seg_ctx_index;
503
504 /* New segment, notify the server and client */
505 seg_handle = segment_manager_make_segment_handle (sm_index, fs_index);
506 if (app_worker_add_segment_notify (server_wrk, seg_handle))
507 goto error;
508
509 client_wrk = app_worker_get (client_wrk_index);
510 if (app_worker_add_segment_notify (client_wrk, seg_handle))
511 {
512 app_worker_del_segment_notify (server_wrk, seg_handle);
513 goto error;
514 }
515
516 return ct_seg;
517
518error:
519
520 segment_manager_lock_and_del_segment (sm, fs_index);
521 pool_put_index (seg_ctx->segments, ct_seg->seg_ctx_index);
522 return 0;
523}
524
Florin Corasda78c5a2021-06-09 14:55:24 -0700525static int
526ct_init_accepted_session (app_worker_t *server_wrk, ct_connection_t *ct,
527 session_t *ls, session_t *ll)
528{
Florin Coras88001c62019-04-24 14:44:46 -0700529 segment_manager_props_t *props;
Florin Coras6973c732021-06-17 15:53:38 -0700530 u64 seg_handle, table_handle;
531 u32 sm_index, fs_index = ~0;
Florin Corasda78c5a2021-06-09 14:55:24 -0700532 ct_segments_ctx_t *seg_ctx;
533 ct_main_t *cm = &ct_main;
Florin Coras653e43f2019-03-04 10:56:23 -0800534 application_t *server;
Florin Corasba7d8f52019-02-22 13:11:38 -0800535 segment_manager_t *sm;
Florin Corasda78c5a2021-06-09 14:55:24 -0700536 ct_segment_t *ct_seg;
537 fifo_segment_t *fs;
Florin Corasda78c5a2021-06-09 14:55:24 -0700538 uword *spp;
Florin Coras6973c732021-06-17 15:53:38 -0700539 int rv;
Florin Corasba7d8f52019-02-22 13:11:38 -0800540
Florin Coras2b81e3c2019-02-27 07:55:46 -0800541 sm = app_worker_get_listen_segment_manager (server_wrk, ll);
Florin Corasda78c5a2021-06-09 14:55:24 -0700542 sm_index = segment_manager_index (sm);
543 server = application_get (server_wrk->app_index);
544 props = application_segment_manager_properties (server);
Florin Corasba7d8f52019-02-22 13:11:38 -0800545
Florin Corasda78c5a2021-06-09 14:55:24 -0700546 table_handle = ct->client_wrk << 16 | server_wrk->wrk_index;
Florin Coras6973c732021-06-17 15:53:38 -0700547 table_handle = (u64) sm_index << 32 | table_handle;
Florin Corasda78c5a2021-06-09 14:55:24 -0700548
549 /*
550 * Check if we already have a segment that can hold the fifos
551 */
552
553 clib_rwlock_reader_lock (&cm->app_segs_lock);
554
555 spp = hash_get (cm->app_segs_ctxs_table, table_handle);
556 if (spp)
557 {
Florin Coras6973c732021-06-17 15:53:38 -0700558 ct_seg = ct_lookup_free_segment (cm, sm, *spp);
Florin Corasda78c5a2021-06-09 14:55:24 -0700559 if (ct_seg)
560 {
Florin Coras6973c732021-06-17 15:53:38 -0700561 ct->seg_ctx_index = ct_seg->seg_ctx_index;
562 ct->ct_seg_index = ct_seg->ct_seg_index;
Florin Corasda78c5a2021-06-09 14:55:24 -0700563 fs_index = ct_seg->segment_index;
Florin Coras98952382021-11-14 15:33:59 -0800564 ct_seg->flags &=
565 ~(CT_SEGMENT_F_SERVER_DETACHED | CT_SEGMENT_F_CLIENT_DETACHED);
Florin Corasda78c5a2021-06-09 14:55:24 -0700566 __atomic_add_fetch (&ct_seg->server_n_sessions, 1, __ATOMIC_RELAXED);
Florin Coras6973c732021-06-17 15:53:38 -0700567 __atomic_add_fetch (&ct_seg->client_n_sessions, 1, __ATOMIC_RELAXED);
Florin Corasda78c5a2021-06-09 14:55:24 -0700568 }
569 }
570
571 clib_rwlock_reader_unlock (&cm->app_segs_lock);
572
573 /*
Florin Coras6973c732021-06-17 15:53:38 -0700574 * If not, grab exclusive lock and allocate segment
Florin Corasda78c5a2021-06-09 14:55:24 -0700575 */
Florin Coras6973c732021-06-17 15:53:38 -0700576 if (fs_index == ~0)
Florin Corasda78c5a2021-06-09 14:55:24 -0700577 {
Florin Corasda78c5a2021-06-09 14:55:24 -0700578 clib_rwlock_writer_lock (&cm->app_segs_lock);
579
Florin Coras6973c732021-06-17 15:53:38 -0700580 ct_seg =
581 ct_alloc_segment (cm, server_wrk, table_handle, sm, ct->client_wrk);
582 if (!ct_seg)
Florin Corasda78c5a2021-06-09 14:55:24 -0700583 {
Florin Coras6973c732021-06-17 15:53:38 -0700584 clib_rwlock_writer_unlock (&cm->app_segs_lock);
585 return -1;
Florin Corasda78c5a2021-06-09 14:55:24 -0700586 }
Florin Corasda78c5a2021-06-09 14:55:24 -0700587
Florin Coras6973c732021-06-17 15:53:38 -0700588 ct->seg_ctx_index = ct_seg->seg_ctx_index;
589 ct->ct_seg_index = ct_seg->ct_seg_index;
Florin Corasda78c5a2021-06-09 14:55:24 -0700590 ct_seg->server_n_sessions += 1;
Florin Coras6973c732021-06-17 15:53:38 -0700591 ct_seg->client_n_sessions += 1;
592 fs_index = ct_seg->segment_index;
Florin Corasda78c5a2021-06-09 14:55:24 -0700593
594 clib_rwlock_writer_unlock (&cm->app_segs_lock);
Florin Corasda78c5a2021-06-09 14:55:24 -0700595 }
596
597 /*
598 * Allocate and initialize the fifos
599 */
600 fs = segment_manager_get_segment_w_lock (sm, fs_index);
601 rv = segment_manager_try_alloc_fifos (
602 fs, ls->thread_index, props->rx_fifo_size, props->tx_fifo_size,
603 &ls->rx_fifo, &ls->tx_fifo);
Florin Corasba7d8f52019-02-22 13:11:38 -0800604 if (rv)
605 {
Florin Corasba7d8f52019-02-22 13:11:38 -0800606 segment_manager_segment_reader_unlock (sm);
Florin Coras6973c732021-06-17 15:53:38 -0700607
608 clib_rwlock_reader_lock (&cm->app_segs_lock);
609
610 seg_ctx = pool_elt_at_index (cm->app_seg_ctxs, ct->seg_ctx_index);
611 ct_seg = pool_elt_at_index (seg_ctx->segments, ct->ct_seg_index);
612 __atomic_sub_fetch (&ct_seg->server_n_sessions, 1, __ATOMIC_RELAXED);
613 __atomic_sub_fetch (&ct_seg->client_n_sessions, 1, __ATOMIC_RELAXED);
614
615 clib_rwlock_reader_unlock (&cm->app_segs_lock);
616
617 return rv;
Florin Corasba7d8f52019-02-22 13:11:38 -0800618 }
Florin Coras2b81e3c2019-02-27 07:55:46 -0800619
Florin Corasc547e912020-12-08 17:50:45 -0800620 ls->rx_fifo->shr->master_session_index = ls->session_index;
621 ls->tx_fifo->shr->master_session_index = ls->session_index;
Florin Corasce252ca2020-11-04 13:08:35 -0800622 ls->rx_fifo->master_thread_index = ls->thread_index;
623 ls->tx_fifo->master_thread_index = ls->thread_index;
Florin Corasba7d8f52019-02-22 13:11:38 -0800624
Florin Corasda78c5a2021-06-09 14:55:24 -0700625 seg_handle = segment_manager_segment_handle (sm, fs);
Florin Corasba7d8f52019-02-22 13:11:38 -0800626 segment_manager_segment_reader_unlock (sm);
Florin Corasda78c5a2021-06-09 14:55:24 -0700627
628 ct->segment_handle = seg_handle;
Florin Corasba7d8f52019-02-22 13:11:38 -0800629
630 return 0;
Florin Corasba7d8f52019-02-22 13:11:38 -0800631}
632
Florin Coras2d0e3de2020-10-23 16:31:40 -0700633static void
Florin Corasc215e5a2021-11-15 19:01:52 -0800634ct_accept_one (u32 thread_index, u32 ho_index)
Florin Coras2d0e3de2020-10-23 16:31:40 -0700635{
Florin Corasba026412021-06-12 11:56:19 -0700636 ct_connection_t *sct, *cct, *ho;
637 transport_connection_t *ll_ct;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800638 app_worker_t *server_wrk;
Florin Corasc215e5a2021-11-15 19:01:52 -0800639 u32 cct_index, ll_index;
Florin Coras2d0e3de2020-10-23 16:31:40 -0700640 session_t *ss, *ll;
Florin Corasba7d8f52019-02-22 13:11:38 -0800641
Florin Coras374df7a2021-05-13 11:37:43 -0700642 /*
643 * Alloc client ct and initialize from ho
644 */
Florin Coras374df7a2021-05-13 11:37:43 -0700645 cct = ct_connection_alloc (thread_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800646 cct_index = cct->c_c_index;
Florin Coras374df7a2021-05-13 11:37:43 -0700647
Florin Coras374df7a2021-05-13 11:37:43 -0700648 ho = ct_connection_get (ho_index, 0);
649
650 /* Unlikely but half-open session and transport could have been freed */
651 if (PREDICT_FALSE (!ho))
652 {
653 ct_connection_free (cct);
654 return;
655 }
656
657 clib_memcpy (cct, ho, sizeof (*ho));
658 cct->c_c_index = cct_index;
659 cct->c_thread_index = thread_index;
660 cct->flags |= CT_CONN_F_HALF_OPEN;
661
662 /* Notify session layer that half-open is on a different thread
663 * and mark ho connection index reusable. Avoids another rpc
664 */
665 session_half_open_migrate_notify (&cct->connection);
666 session_half_open_migrated_notify (&cct->connection);
667 ct_half_open_add_reusable (ho_index);
Florin Corasba7d8f52019-02-22 13:11:38 -0800668
Florin Coras2b81e3c2019-02-27 07:55:46 -0800669 /*
Florin Coras374df7a2021-05-13 11:37:43 -0700670 * Alloc and init server transport
Florin Coras2b81e3c2019-02-27 07:55:46 -0800671 */
Florin Coras374df7a2021-05-13 11:37:43 -0700672
673 ll_index = cct->peer_index;
674 ll = listen_session_get (ll_index);
675 sct = ct_connection_alloc (thread_index);
Florin Corasba026412021-06-12 11:56:19 -0700676 /* Transport not necessarily ct but it might, so grab after sct alloc */
677 ll_ct = listen_session_get_transport (ll);
Florin Coras374df7a2021-05-13 11:37:43 -0700678
679 /* Make sure cct is valid after sct alloc */
680 cct = ct_connection_get (cct_index, thread_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800681
Florin Coras2b81e3c2019-02-27 07:55:46 -0800682 sct->c_rmt_port = 0;
Florin Corasba026412021-06-12 11:56:19 -0700683 sct->c_lcl_port = ll_ct->lcl_port;
Florin Coras374df7a2021-05-13 11:37:43 -0700684 sct->c_is_ip4 = cct->c_is_ip4;
Florin Corasd9e98702021-10-11 18:10:41 -0700685 clib_memcpy (&sct->c_lcl_ip, &cct->c_rmt_ip, sizeof (cct->c_rmt_ip));
Florin Coras374df7a2021-05-13 11:37:43 -0700686 sct->client_wrk = cct->client_wrk;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800687 sct->c_proto = TRANSPORT_PROTO_NONE;
Florin Coras374df7a2021-05-13 11:37:43 -0700688 sct->client_opaque = cct->client_opaque;
Florin Corasba026412021-06-12 11:56:19 -0700689 sct->actual_tp = cct->actual_tp;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800690
691 sct->peer_index = cct->c_c_index;
692 cct->peer_index = sct->c_c_index;
693
694 /*
695 * Accept server session. Client session is created only after
696 * server confirms accept.
697 */
Florin Coras374df7a2021-05-13 11:37:43 -0700698 ss = session_alloc (thread_index);
699 ll = listen_session_get (ll_index);
Florin Coras9f1a5432019-03-10 21:03:20 -0700700 ss->session_type = session_type_from_proto_and_ip (TRANSPORT_PROTO_NONE,
701 sct->c_is_ip4);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800702 ss->connection_index = sct->c_c_index;
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +0200703 ss->listener_handle = listen_session_get_handle (ll);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800704 ss->session_state = SESSION_STATE_CREATED;
705
706 server_wrk = application_listener_select_worker (ll);
707 ss->app_wrk_index = server_wrk->wrk_index;
708
709 sct->c_s_index = ss->session_index;
710 sct->server_wrk = ss->app_wrk_index;
711
Florin Coras2d0e3de2020-10-23 16:31:40 -0700712 if (ct_init_accepted_session (server_wrk, sct, ss, ll))
Florin Corasba7d8f52019-02-22 13:11:38 -0800713 {
Florin Coras6973c732021-06-17 15:53:38 -0700714 ct_session_connect_notify (ss, SESSION_E_ALLOC);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800715 ct_connection_free (sct);
716 session_free (ss);
Florin Coras2d0e3de2020-10-23 16:31:40 -0700717 return;
Florin Corasba7d8f52019-02-22 13:11:38 -0800718 }
719
Florin Coras98952382021-11-14 15:33:59 -0800720 cct->server_wrk = sct->server_wrk;
Florin Coras6973c732021-06-17 15:53:38 -0700721 cct->seg_ctx_index = sct->seg_ctx_index;
722 cct->ct_seg_index = sct->ct_seg_index;
723 cct->client_rx_fifo = ss->tx_fifo;
724 cct->client_tx_fifo = ss->rx_fifo;
725 cct->client_rx_fifo->refcnt++;
726 cct->client_tx_fifo->refcnt++;
727 cct->segment_handle = sct->segment_handle;
728
Florin Coras2b81e3c2019-02-27 07:55:46 -0800729 ss->session_state = SESSION_STATE_ACCEPTING;
730 if (app_worker_accept_notify (server_wrk, ss))
731 {
Florin Coras6973c732021-06-17 15:53:38 -0700732 ct_session_connect_notify (ss, SESSION_E_REFUSED);
Florin Corasda78c5a2021-06-09 14:55:24 -0700733 ct_session_dealloc_fifos (sct, ss->rx_fifo, ss->tx_fifo);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800734 ct_connection_free (sct);
Florin Coras12813d52020-04-09 20:59:20 +0000735 session_free (ss);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800736 }
Florin Coras2d0e3de2020-10-23 16:31:40 -0700737}
738
Florin Corasc215e5a2021-11-15 19:01:52 -0800739static void
740ct_accept_rpc_wrk_handler (void *rpc_args)
741{
742 u32 thread_index, ho_index, n_connects, i, n_pending;
743 const u32 max_connects = 32;
744 ct_worker_t *wrk;
745
746 thread_index = pointer_to_uword (rpc_args);
747 wrk = ct_worker_get (thread_index);
748
749 /* Sub without lock as main enqueues with worker barrier */
750 n_pending = clib_fifo_elts (wrk->pending_connects);
751 n_connects = clib_min (n_pending, max_connects);
752
753 for (i = 0; i < n_connects; i++)
754 {
755 clib_fifo_sub1 (wrk->pending_connects, ho_index);
756 ct_accept_one (thread_index, ho_index);
757 }
758
759 if (n_pending == n_connects)
760 wrk->have_connects = 0;
761 else
762 session_send_rpc_evt_to_thread_force (
763 thread_index, ct_accept_rpc_wrk_handler,
764 uword_to_pointer (thread_index, void *));
765}
766
Florin Coras2d0e3de2020-10-23 16:31:40 -0700767static int
768ct_connect (app_worker_t * client_wrk, session_t * ll,
769 session_endpoint_cfg_t * sep)
770{
Florin Coras374df7a2021-05-13 11:37:43 -0700771 u32 thread_index, ho_index;
Florin Coras2d0e3de2020-10-23 16:31:40 -0700772 ct_main_t *cm = &ct_main;
Florin Coras374df7a2021-05-13 11:37:43 -0700773 ct_connection_t *ho;
Florin Corasc215e5a2021-11-15 19:01:52 -0800774 ct_worker_t *wrk;
Florin Coras2d0e3de2020-10-23 16:31:40 -0700775
776 /* Simple round-robin policy for spreading sessions over workers. We skip
777 * thread index 0, i.e., offset the index by 1, when we have workers as it
778 * is the one dedicated to main thread. Note that n_workers does not include
779 * main thread */
780 cm->n_sessions += 1;
781 thread_index = cm->n_workers ? (cm->n_sessions % cm->n_workers) + 1 : 0;
782
Florin Coras374df7a2021-05-13 11:37:43 -0700783 /*
784 * Alloc and init client half-open transport
785 */
Florin Coras2d0e3de2020-10-23 16:31:40 -0700786
Florin Coras374df7a2021-05-13 11:37:43 -0700787 ho = ct_half_open_alloc ();
788 ho_index = ho->c_c_index;
789 ho->c_rmt_port = sep->port;
790 ho->c_lcl_port = 0;
791 ho->c_is_ip4 = sep->is_ip4;
792 ho->client_opaque = sep->opaque;
793 ho->client_wrk = client_wrk->wrk_index;
794 ho->peer_index = ll->session_index;
795 ho->c_proto = TRANSPORT_PROTO_NONE;
796 ho->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
Florin Corasd5a58822021-05-14 20:11:14 -0700797 clib_memcpy (&ho->c_rmt_ip, &sep->ip, sizeof (sep->ip));
Florin Coras374df7a2021-05-13 11:37:43 -0700798 ho->flags |= CT_CONN_F_CLIENT;
799 ho->c_s_index = ~0;
Florin Corasfeb67022021-11-11 09:24:34 -0800800 ho->actual_tp = sep->original_tp;
Florin Coras374df7a2021-05-13 11:37:43 -0700801
802 /*
803 * Accept connection on thread selected above. Connected reply comes
804 * after server accepts the connection.
805 */
806
Florin Corasc215e5a2021-11-15 19:01:52 -0800807 wrk = ct_worker_get (thread_index);
808
809 /* Worker barrier held, add without additional lock */
810 clib_fifo_add1 (wrk->pending_connects, ho_index);
811 if (!wrk->have_connects)
812 {
813 wrk->have_connects = 1;
814 session_send_rpc_evt_to_thread_force (
815 thread_index, ct_accept_rpc_wrk_handler,
816 uword_to_pointer (thread_index, void *));
817 }
Florin Coras374df7a2021-05-13 11:37:43 -0700818
819 return ho_index;
Florin Corasba7d8f52019-02-22 13:11:38 -0800820}
821
Florin Coras653e43f2019-03-04 10:56:23 -0800822static u32
Florin Corasd4295e62019-02-22 13:11:38 -0800823ct_start_listen (u32 app_listener_index, transport_endpoint_t * tep)
824{
825 session_endpoint_cfg_t *sep;
826 ct_connection_t *ct;
827
828 sep = (session_endpoint_cfg_t *) tep;
Florin Coras2d0e3de2020-10-23 16:31:40 -0700829 ct = ct_connection_alloc (0);
Florin Corasd4295e62019-02-22 13:11:38 -0800830 ct->server_wrk = sep->app_wrk_index;
831 ct->c_is_ip4 = sep->is_ip4;
832 clib_memcpy (&ct->c_lcl_ip, &sep->ip, sizeof (sep->ip));
833 ct->c_lcl_port = sep->port;
Florin Corasa8375082020-10-25 19:06:57 -0700834 ct->c_s_index = app_listener_index;
Florin Corasd4295e62019-02-22 13:11:38 -0800835 ct->actual_tp = sep->transport_proto;
836 return ct->c_c_index;
837}
838
Florin Coras653e43f2019-03-04 10:56:23 -0800839static u32
Florin Corasd4295e62019-02-22 13:11:38 -0800840ct_stop_listen (u32 ct_index)
841{
842 ct_connection_t *ct;
Florin Coras2d0e3de2020-10-23 16:31:40 -0700843 ct = ct_connection_get (ct_index, 0);
Florin Corasd4295e62019-02-22 13:11:38 -0800844 ct_connection_free (ct);
845 return 0;
846}
847
Florin Coras653e43f2019-03-04 10:56:23 -0800848static transport_connection_t *
Florin Corasd4295e62019-02-22 13:11:38 -0800849ct_listener_get (u32 ct_index)
850{
Florin Coras2d0e3de2020-10-23 16:31:40 -0700851 return (transport_connection_t *) ct_connection_get (ct_index, 0);
Florin Corasd4295e62019-02-22 13:11:38 -0800852}
853
Florin Coras374df7a2021-05-13 11:37:43 -0700854static transport_connection_t *
855ct_half_open_get (u32 ct_index)
856{
857 return (transport_connection_t *) ct_connection_get (ct_index, 0);
858}
859
860static void
861ct_session_cleanup (u32 conn_index, u32 thread_index)
862{
863 ct_connection_t *ct, *peer_ct;
864
865 ct = ct_connection_get (conn_index, thread_index);
866 if (!ct)
867 return;
868
869 peer_ct = ct_connection_get (ct->peer_index, thread_index);
870 if (peer_ct)
871 peer_ct->peer_index = ~0;
872
873 ct_connection_free (ct);
874}
875
876static void
877ct_cleanup_ho (u32 ho_index)
878{
879 ct_connection_free (ct_connection_get (ho_index, 0));
880}
881
Florin Coras653e43f2019-03-04 10:56:23 -0800882static int
Florin Coras2b81e3c2019-02-27 07:55:46 -0800883ct_session_connect (transport_endpoint_cfg_t * tep)
884{
885 session_endpoint_cfg_t *sep_ext;
Florin Coras374df7a2021-05-13 11:37:43 -0700886 session_endpoint_t _sep, *sep = &_sep;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800887 app_worker_t *app_wrk;
888 session_handle_t lh;
889 application_t *app;
890 app_listener_t *al;
891 u32 table_index;
892 session_t *ll;
893 u8 fib_proto;
894
895 sep_ext = (session_endpoint_cfg_t *) tep;
Florin Coras374df7a2021-05-13 11:37:43 -0700896 _sep = *(session_endpoint_t *) tep;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800897 app_wrk = app_worker_get (sep_ext->app_wrk_index);
898 app = application_get (app_wrk->app_index);
899
900 sep->transport_proto = sep_ext->original_tp;
901 table_index = application_local_session_table (app);
902 lh = session_lookup_local_endpoint (table_index, sep);
903 if (lh == SESSION_DROP_HANDLE)
Florin Coras00e01d32019-10-21 16:07:46 -0700904 return SESSION_E_FILTERED;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800905
906 if (lh == SESSION_INVALID_HANDLE)
907 goto global_scope;
908
909 ll = listen_session_get_from_handle (lh);
910 al = app_listener_get_w_session (ll);
911
912 /*
913 * Break loop if rule in local table points to connecting app. This
914 * can happen if client is a generic proxy. Route connect through
915 * global table instead.
916 */
917 if (al->app_index == app->app_index)
918 goto global_scope;
919
920 return ct_connect (app_wrk, ll, sep_ext);
921
922 /*
923 * If nothing found, check the global scope for locally attached
924 * destinations. Make sure first that we're allowed to.
925 */
926
927global_scope:
928 if (session_endpoint_is_local (sep))
Florin Coras00e01d32019-10-21 16:07:46 -0700929 return SESSION_E_NOROUTE;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800930
931 if (!application_has_global_scope (app))
Florin Coras00e01d32019-10-21 16:07:46 -0700932 return SESSION_E_SCOPE;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800933
934 fib_proto = session_endpoint_fib_proto (sep);
Florin Coras95cd8642019-12-30 21:53:19 -0800935 table_index = session_lookup_get_index_for_fib (fib_proto, sep->fib_index);
Florin Coras9f1a5432019-03-10 21:03:20 -0700936 ll = session_lookup_listener_wildcard (table_index, sep);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800937
Florin Coras821b5002021-06-02 21:32:39 -0700938 /* Avoid connecting app to own listener */
939 if (ll && ll->app_index != app->app_index)
Florin Coras2b81e3c2019-02-27 07:55:46 -0800940 return ct_connect (app_wrk, ll, sep_ext);
941
942 /* Failed to connect but no error */
Florin Coras374df7a2021-05-13 11:37:43 -0700943 return SESSION_E_LOCAL_CONNECT;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800944}
945
Florin Coras9439a362021-11-25 16:18:39 -0800946static inline int
947ct_close_is_reset (ct_connection_t *ct, session_t *s)
948{
949 if (ct->flags & CT_CONN_F_CLIENT)
950 return (svm_fifo_max_dequeue (ct->client_rx_fifo) > 0);
951 else
952 return (svm_fifo_max_dequeue (s->rx_fifo) > 0);
953}
954
Florin Coras653e43f2019-03-04 10:56:23 -0800955static void
Florin Coras440c7b52021-11-09 08:38:24 -0800956ct_session_postponed_cleanup (ct_connection_t *ct)
957{
Florin Coras9439a362021-11-25 16:18:39 -0800958 ct_connection_t *peer_ct;
Florin Coras440c7b52021-11-09 08:38:24 -0800959 app_worker_t *app_wrk;
960 session_t *s;
961
962 s = session_get (ct->c_s_index, ct->c_thread_index);
963 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
964
Florin Coras9439a362021-11-25 16:18:39 -0800965 peer_ct = ct_connection_get (ct->peer_index, ct->c_thread_index);
966 if (peer_ct)
967 {
968 if (ct_close_is_reset (ct, s))
969 session_transport_reset_notify (&peer_ct->connection);
970 else
971 session_transport_closing_notify (&peer_ct->connection);
972 }
973 session_transport_closed_notify (&ct->connection);
974
Florin Coras440c7b52021-11-09 08:38:24 -0800975 if (ct->flags & CT_CONN_F_CLIENT)
976 {
977 if (app_wrk)
978 app_worker_cleanup_notify (app_wrk, s, SESSION_CLEANUP_TRANSPORT);
979
980 /* Normal free for client session as the fifos are allocated through
981 * the connects segment manager in a segment that's not shared with
982 * the server */
Florin Coras440c7b52021-11-09 08:38:24 -0800983 ct_session_dealloc_fifos (ct, ct->client_rx_fifo, ct->client_tx_fifo);
Florin Coras98952382021-11-14 15:33:59 -0800984 session_free_w_fifos (s);
Florin Coras440c7b52021-11-09 08:38:24 -0800985 }
986 else
987 {
988 /* Manual session and fifo segment cleanup to avoid implicit
989 * segment manager cleanups and notifications */
990 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
991 if (app_wrk)
992 {
993 app_worker_cleanup_notify (app_wrk, s, SESSION_CLEANUP_TRANSPORT);
994 app_worker_cleanup_notify (app_wrk, s, SESSION_CLEANUP_SESSION);
995 }
996
997 ct_session_dealloc_fifos (ct, s->rx_fifo, s->tx_fifo);
998 session_free (s);
999 }
1000
1001 ct_connection_free (ct);
1002}
1003
1004static void
1005ct_handle_cleanups (void *args)
1006{
1007 uword thread_index = pointer_to_uword (args);
1008 const u32 max_cleanups = 100;
Florin Coras440c7b52021-11-09 08:38:24 -08001009 ct_cleanup_req_t *req;
1010 ct_connection_t *ct;
1011 u32 n_to_handle = 0;
Florin Coras7fe00692021-11-15 14:01:02 -08001012 ct_worker_t *wrk;
Florin Coras440c7b52021-11-09 08:38:24 -08001013 session_t *s;
1014
Florin Coras7fe00692021-11-15 14:01:02 -08001015 wrk = ct_worker_get (thread_index);
1016 wrk->have_cleanups = 0;
1017 n_to_handle = clib_fifo_elts (wrk->pending_cleanups);
Florin Coras440c7b52021-11-09 08:38:24 -08001018 n_to_handle = clib_min (n_to_handle, max_cleanups);
1019
1020 while (n_to_handle)
1021 {
Florin Coras7fe00692021-11-15 14:01:02 -08001022 clib_fifo_sub2 (wrk->pending_cleanups, req);
Florin Coras440c7b52021-11-09 08:38:24 -08001023 ct = ct_connection_get (req->ct_index, thread_index);
1024 s = session_get (ct->c_s_index, ct->c_thread_index);
1025 if (!svm_fifo_has_event (s->tx_fifo))
1026 ct_session_postponed_cleanup (ct);
1027 else
Florin Coras7fe00692021-11-15 14:01:02 -08001028 clib_fifo_add1 (wrk->pending_cleanups, *req);
Florin Coras440c7b52021-11-09 08:38:24 -08001029 n_to_handle -= 1;
1030 }
1031
Florin Coras7fe00692021-11-15 14:01:02 -08001032 if (clib_fifo_elts (wrk->pending_cleanups))
Florin Coras440c7b52021-11-09 08:38:24 -08001033 {
Florin Coras7fe00692021-11-15 14:01:02 -08001034 wrk->have_cleanups = 1;
Florin Coras440c7b52021-11-09 08:38:24 -08001035 session_send_rpc_evt_to_thread_force (
1036 thread_index, ct_handle_cleanups,
1037 uword_to_pointer (thread_index, void *));
1038 }
1039}
1040
1041static void
1042ct_program_cleanup (ct_connection_t *ct)
1043{
Florin Coras440c7b52021-11-09 08:38:24 -08001044 ct_cleanup_req_t *req;
1045 uword thread_index;
Florin Coras7fe00692021-11-15 14:01:02 -08001046 ct_worker_t *wrk;
Florin Coras440c7b52021-11-09 08:38:24 -08001047
1048 thread_index = ct->c_thread_index;
Florin Coras7fe00692021-11-15 14:01:02 -08001049 wrk = ct_worker_get (ct->c_thread_index);
1050
1051 clib_fifo_add2 (wrk->pending_cleanups, req);
Florin Coras440c7b52021-11-09 08:38:24 -08001052 req->ct_index = ct->c_c_index;
1053
Florin Coras7fe00692021-11-15 14:01:02 -08001054 if (wrk->have_cleanups)
Florin Coras440c7b52021-11-09 08:38:24 -08001055 return;
1056
Florin Coras7fe00692021-11-15 14:01:02 -08001057 wrk->have_cleanups = 1;
Florin Coras440c7b52021-11-09 08:38:24 -08001058 session_send_rpc_evt_to_thread_force (
1059 thread_index, ct_handle_cleanups, uword_to_pointer (thread_index, void *));
1060}
1061
1062static void
Florin Coras2b81e3c2019-02-27 07:55:46 -08001063ct_session_close (u32 ct_index, u32 thread_index)
1064{
1065 ct_connection_t *ct, *peer_ct;
1066 session_t *s;
1067
Florin Coras2d0e3de2020-10-23 16:31:40 -07001068 ct = ct_connection_get (ct_index, thread_index);
Florin Coras6973c732021-06-17 15:53:38 -07001069 s = session_get (ct->c_s_index, ct->c_thread_index);
Florin Coras2d0e3de2020-10-23 16:31:40 -07001070 peer_ct = ct_connection_get (ct->peer_index, thread_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -08001071 if (peer_ct)
1072 {
1073 peer_ct->peer_index = ~0;
Florin Coras06ac4bb2020-10-28 16:41:26 -07001074 /* Make sure session was allocated */
Florin Coras374df7a2021-05-13 11:37:43 -07001075 if (peer_ct->flags & CT_CONN_F_HALF_OPEN)
1076 {
Florin Coras6973c732021-06-17 15:53:38 -07001077 ct_session_connect_notify (s, SESSION_E_REFUSED);
Florin Corasa7bd8262021-11-25 12:14:25 -08001078 ct->peer_index = ~0;
Florin Coras374df7a2021-05-13 11:37:43 -07001079 }
Florin Coras9439a362021-11-25 16:18:39 -08001080 else if (peer_ct->c_s_index == ~0)
Florin Coras440c7b52021-11-09 08:38:24 -08001081 {
1082 /* should not happen */
1083 clib_warning ("ct peer without session");
1084 ct_connection_free (peer_ct);
1085 }
Florin Coras2b81e3c2019-02-27 07:55:46 -08001086 }
1087
Florin Coras440c7b52021-11-09 08:38:24 -08001088 /* Do not send closed notify to make sure pending tx events are
1089 * still delivered and program cleanup */
1090 ct_program_cleanup (ct);
Florin Coras2b81e3c2019-02-27 07:55:46 -08001091}
1092
Florin Coras653e43f2019-03-04 10:56:23 -08001093static transport_connection_t *
Florin Coras2b81e3c2019-02-27 07:55:46 -08001094ct_session_get (u32 ct_index, u32 thread_index)
1095{
Florin Coras2d0e3de2020-10-23 16:31:40 -07001096 return (transport_connection_t *) ct_connection_get (ct_index,
1097 thread_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -08001098}
1099
Florin Corasd4295e62019-02-22 13:11:38 -08001100static u8 *
1101format_ct_connection_id (u8 * s, va_list * args)
1102{
1103 ct_connection_t *ct = va_arg (*args, ct_connection_t *);
1104 if (!ct)
1105 return s;
1106 if (ct->c_is_ip4)
1107 {
1108 s = format (s, "[%d:%d][CT:%U] %U:%d->%U:%d", ct->c_thread_index,
1109 ct->c_s_index, format_transport_proto_short, ct->actual_tp,
1110 format_ip4_address, &ct->c_lcl_ip4,
1111 clib_net_to_host_u16 (ct->c_lcl_port), format_ip4_address,
1112 &ct->c_rmt_ip4, clib_net_to_host_u16 (ct->c_rmt_port));
1113 }
1114 else
1115 {
1116 s = format (s, "[%d:%d][CT:%U] %U:%d->%U:%d", ct->c_thread_index,
1117 ct->c_s_index, format_transport_proto_short, ct->actual_tp,
1118 format_ip6_address, &ct->c_lcl_ip6,
1119 clib_net_to_host_u16 (ct->c_lcl_port), format_ip6_address,
1120 &ct->c_rmt_ip6, clib_net_to_host_u16 (ct->c_rmt_port));
1121 }
1122
1123 return s;
1124}
1125
Florin Corasf940f8a2019-03-06 10:44:38 -08001126static int
Florin Coras9f86d222020-03-23 15:34:22 +00001127ct_custom_tx (void *session, transport_send_params_t * sp)
Florin Corasf940f8a2019-03-06 10:44:38 -08001128{
1129 session_t *s = (session_t *) session;
Florin Coras074f3972021-12-07 15:12:06 -08001130 if (session_has_transport (s))
Florin Corasf940f8a2019-03-06 10:44:38 -08001131 return 0;
Florin Coras1b9d2c22021-01-26 14:10:43 -08001132 /* If event enqueued towards peer, remove from scheduler and remove
1133 * session tx flag, i.e., accept new tx events. Unset fifo flag now to
1134 * avoid missing events if peer did not clear fifo flag yet, which is
1135 * interpreted as successful notification and session is descheduled. */
1136 svm_fifo_unset_event (s->tx_fifo);
Florin Corasd6894562020-10-28 00:37:15 -07001137 if (!ct_session_tx (s))
Florin Coras1b9d2c22021-01-26 14:10:43 -08001138 sp->flags = TRANSPORT_SND_F_DESCHED;
1139
Florin Corasd6894562020-10-28 00:37:15 -07001140 /* The scheduler uses packet count as a means of upper bounding the amount
1141 * of work done per dispatch. So make it look like we have sent something */
1142 return 1;
Florin Corasf940f8a2019-03-06 10:44:38 -08001143}
1144
Florin Coras317b8e02019-04-17 09:57:46 -07001145static int
1146ct_app_rx_evt (transport_connection_t * tc)
1147{
1148 ct_connection_t *ct = (ct_connection_t *) tc, *peer_ct;
Florin Coras9439a362021-11-25 16:18:39 -08001149 session_t *ps, *s;
Florin Coras317b8e02019-04-17 09:57:46 -07001150
Florin Coras9439a362021-11-25 16:18:39 -08001151 s = session_get (ct->c_s_index, ct->c_thread_index);
1152 if (session_has_transport (s) || s->session_state < SESSION_STATE_READY)
1153 return -1;
Florin Coras2d0e3de2020-10-23 16:31:40 -07001154 peer_ct = ct_connection_get (ct->peer_index, tc->thread_index);
Florin Coras9439a362021-11-25 16:18:39 -08001155 if (!peer_ct || (peer_ct->flags & CT_CONN_F_HALF_OPEN))
Florin Coras317b8e02019-04-17 09:57:46 -07001156 return -1;
1157 ps = session_get (peer_ct->c_s_index, peer_ct->c_thread_index);
Florin Coras9439a362021-11-25 16:18:39 -08001158 if (ps->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
1159 return -1;
Florin Coras317b8e02019-04-17 09:57:46 -07001160 return session_dequeue_notify (ps);
1161}
1162
Florin Coras653e43f2019-03-04 10:56:23 -08001163static u8 *
Florin Corasd4295e62019-02-22 13:11:38 -08001164format_ct_listener (u8 * s, va_list * args)
1165{
1166 u32 tc_index = va_arg (*args, u32);
Aloys Augustina0abbff2019-07-12 12:16:16 +02001167 u32 __clib_unused thread_index = va_arg (*args, u32);
Florin Corasd4295e62019-02-22 13:11:38 -08001168 u32 __clib_unused verbose = va_arg (*args, u32);
Florin Coras2d0e3de2020-10-23 16:31:40 -07001169 ct_connection_t *ct = ct_connection_get (tc_index, 0);
Florin Coras7bf6ed62020-09-23 12:02:08 -07001170 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_ct_connection_id, ct);
Florin Corasd4295e62019-02-22 13:11:38 -08001171 if (verbose)
Florin Coras7bf6ed62020-09-23 12:02:08 -07001172 s = format (s, "%-" SESSION_CLI_STATE_LEN "s", "LISTEN");
Florin Corasd4295e62019-02-22 13:11:38 -08001173 return s;
1174}
1175
Florin Coras653e43f2019-03-04 10:56:23 -08001176static u8 *
Florin Coras374df7a2021-05-13 11:37:43 -07001177format_ct_half_open (u8 *s, va_list *args)
1178{
1179 u32 ho_index = va_arg (*args, u32);
1180 u32 verbose = va_arg (*args, u32);
1181 ct_connection_t *ct = ct_connection_get (ho_index, 0);
1182 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_ct_connection_id, ct);
1183 if (verbose)
1184 s = format (s, "%-" SESSION_CLI_STATE_LEN "s", "HALF-OPEN");
1185 return s;
1186}
1187
1188static u8 *
Florin Coras2b81e3c2019-02-27 07:55:46 -08001189format_ct_connection (u8 * s, va_list * args)
1190{
1191 ct_connection_t *ct = va_arg (*args, ct_connection_t *);
1192 u32 verbose = va_arg (*args, u32);
1193
1194 if (!ct)
1195 return s;
Florin Coras7bf6ed62020-09-23 12:02:08 -07001196 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_ct_connection_id, ct);
Florin Coras2b81e3c2019-02-27 07:55:46 -08001197 if (verbose)
1198 {
Florin Coras7bf6ed62020-09-23 12:02:08 -07001199 s = format (s, "%-" SESSION_CLI_STATE_LEN "s", "ESTABLISHED");
Florin Coras2b81e3c2019-02-27 07:55:46 -08001200 if (verbose > 1)
1201 {
1202 s = format (s, "\n");
1203 }
1204 }
1205 return s;
1206}
1207
Florin Coras653e43f2019-03-04 10:56:23 -08001208static u8 *
Florin Coras2b81e3c2019-02-27 07:55:46 -08001209format_ct_session (u8 * s, va_list * args)
1210{
1211 u32 ct_index = va_arg (*args, u32);
Florin Coras2d0e3de2020-10-23 16:31:40 -07001212 u32 thread_index = va_arg (*args, u32);
Florin Coras2b81e3c2019-02-27 07:55:46 -08001213 u32 verbose = va_arg (*args, u32);
1214 ct_connection_t *ct;
1215
Florin Coras2d0e3de2020-10-23 16:31:40 -07001216 ct = ct_connection_get (ct_index, thread_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -08001217 if (!ct)
1218 {
1219 s = format (s, "empty\n");
1220 return s;
1221 }
1222
1223 s = format (s, "%U", format_ct_connection, ct, verbose);
1224 return s;
1225}
1226
Florin Coras2d0e3de2020-10-23 16:31:40 -07001227clib_error_t *
1228ct_enable_disable (vlib_main_t * vm, u8 is_en)
1229{
Florin Coras7fe00692021-11-15 14:01:02 -08001230 vlib_thread_main_t *vtm = &vlib_thread_main;
Florin Coras374df7a2021-05-13 11:37:43 -07001231 ct_main_t *cm = &ct_main;
1232
1233 cm->n_workers = vlib_num_workers ();
Florin Coras7fe00692021-11-15 14:01:02 -08001234 vec_validate (cm->wrk, vtm->n_vlib_mains);
Florin Coras374df7a2021-05-13 11:37:43 -07001235 clib_spinlock_init (&cm->ho_reuseable_lock);
Florin Corasda78c5a2021-06-09 14:55:24 -07001236 clib_rwlock_init (&cm->app_segs_lock);
Florin Coras2d0e3de2020-10-23 16:31:40 -07001237 return 0;
1238}
1239
Florin Corasd4295e62019-02-22 13:11:38 -08001240/* *INDENT-OFF* */
Nathan Skrzypczake971bc92019-06-19 13:42:37 +02001241static const transport_proto_vft_t cut_thru_proto = {
Florin Coras2d0e3de2020-10-23 16:31:40 -07001242 .enable = ct_enable_disable,
Florin Corasd4295e62019-02-22 13:11:38 -08001243 .start_listen = ct_start_listen,
1244 .stop_listen = ct_stop_listen,
Florin Coras374df7a2021-05-13 11:37:43 -07001245 .get_connection = ct_session_get,
Florin Corasd4295e62019-02-22 13:11:38 -08001246 .get_listener = ct_listener_get,
Florin Coras374df7a2021-05-13 11:37:43 -07001247 .get_half_open = ct_half_open_get,
1248 .cleanup = ct_session_cleanup,
1249 .cleanup_ho = ct_cleanup_ho,
Florin Coras2b81e3c2019-02-27 07:55:46 -08001250 .connect = ct_session_connect,
1251 .close = ct_session_close,
Florin Corasf940f8a2019-03-06 10:44:38 -08001252 .custom_tx = ct_custom_tx,
Florin Coras317b8e02019-04-17 09:57:46 -07001253 .app_rx_evt = ct_app_rx_evt,
Florin Corasd4295e62019-02-22 13:11:38 -08001254 .format_listener = format_ct_listener,
Florin Coras374df7a2021-05-13 11:37:43 -07001255 .format_half_open = format_ct_half_open,
Florin Coras2b81e3c2019-02-27 07:55:46 -08001256 .format_connection = format_ct_session,
Nathan Skrzypczake971bc92019-06-19 13:42:37 +02001257 .transport_options = {
Florin Coras07063b82020-03-13 04:44:51 +00001258 .name = "ct",
1259 .short_name = "C",
Nathan Skrzypczake971bc92019-06-19 13:42:37 +02001260 .tx_type = TRANSPORT_TX_INTERNAL,
Florin Coras374df7a2021-05-13 11:37:43 -07001261 .service_type = TRANSPORT_SERVICE_VC,
Nathan Skrzypczake971bc92019-06-19 13:42:37 +02001262 },
Florin Corasd4295e62019-02-22 13:11:38 -08001263};
1264/* *INDENT-ON* */
1265
Florin Coras074f3972021-12-07 15:12:06 -08001266static inline int
1267ct_session_can_tx (session_t *s)
1268{
1269 return (s->session_state == SESSION_STATE_READY ||
1270 s->session_state == SESSION_STATE_CLOSING ||
1271 s->session_state == SESSION_STATE_APP_CLOSED);
1272}
1273
Florin Coras653e43f2019-03-04 10:56:23 -08001274int
1275ct_session_tx (session_t * s)
1276{
1277 ct_connection_t *ct, *peer_ct;
1278 session_t *peer_s;
1279
Florin Coras074f3972021-12-07 15:12:06 -08001280 if (!ct_session_can_tx (s))
1281 return 0;
Florin Coras653e43f2019-03-04 10:56:23 -08001282 ct = (ct_connection_t *) session_get_transport (s);
Florin Coras2d0e3de2020-10-23 16:31:40 -07001283 peer_ct = ct_connection_get (ct->peer_index, ct->c_thread_index);
Florin Coras653e43f2019-03-04 10:56:23 -08001284 if (!peer_ct)
Florin Corasbcdc50d2020-08-25 19:07:02 -07001285 return 0;
Florin Coras2d0e3de2020-10-23 16:31:40 -07001286 peer_s = session_get (peer_ct->c_s_index, peer_ct->c_thread_index);
Florin Coras653e43f2019-03-04 10:56:23 -08001287 if (peer_s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
1288 return 0;
Florin Corasd6894562020-10-28 00:37:15 -07001289 return session_enqueue_notify (peer_s);
Florin Coras653e43f2019-03-04 10:56:23 -08001290}
1291
Florin Corasd4295e62019-02-22 13:11:38 -08001292static clib_error_t *
1293ct_transport_init (vlib_main_t * vm)
1294{
1295 transport_register_protocol (TRANSPORT_PROTO_NONE, &cut_thru_proto,
1296 FIB_PROTOCOL_IP4, ~0);
Florin Coras653e43f2019-03-04 10:56:23 -08001297 transport_register_protocol (TRANSPORT_PROTO_NONE, &cut_thru_proto,
1298 FIB_PROTOCOL_IP6, ~0);
Florin Corasd4295e62019-02-22 13:11:38 -08001299 return 0;
1300}
1301
1302VLIB_INIT_FUNCTION (ct_transport_init);
1303
Florin Corasba7d8f52019-02-22 13:11:38 -08001304/*
1305 * fd.io coding-style-patch-verification: ON
1306 *
1307 * Local Variables:
1308 * eval: (c-set-style "gnu")
1309 * End:
1310 */