blob: 3c62dade0f5f2df02d068d7ebc68336a1e918272 [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 Coras2d0e3de2020-10-23 16:31:40 -070044typedef struct ct_main_
Florin Coras653e43f2019-03-04 10:56:23 -080045{
Florin Coras2d0e3de2020-10-23 16:31:40 -070046 ct_connection_t **connections; /**< Per-worker connection pools */
47 u32 n_workers; /**< Number of vpp workers */
48 u32 n_sessions; /**< Cumulative sessions counter */
Florin Coras374df7a2021-05-13 11:37:43 -070049 u32 *ho_reusable; /**< Vector of reusable ho indices */
50 clib_spinlock_t ho_reuseable_lock; /**< Lock for reusable ho indices */
Florin Corasda78c5a2021-06-09 14:55:24 -070051 clib_rwlock_t app_segs_lock; /**< RW lock for seg contexts */
52 uword *app_segs_ctxs_table; /**< App handle to segment pool map */
53 ct_segments_ctx_t *app_seg_ctxs; /**< Pool of ct segment contexts */
Florin Coras2d0e3de2020-10-23 16:31:40 -070054} ct_main_t;
Florin Coras653e43f2019-03-04 10:56:23 -080055
Florin Coras2d0e3de2020-10-23 16:31:40 -070056static ct_main_t ct_main;
Florin Coras653e43f2019-03-04 10:56:23 -080057
58static ct_connection_t *
Florin Coras2d0e3de2020-10-23 16:31:40 -070059ct_connection_alloc (u32 thread_index)
Florin Corasd4295e62019-02-22 13:11:38 -080060{
61 ct_connection_t *ct;
62
Florin Coras2d0e3de2020-10-23 16:31:40 -070063 pool_get_zero (ct_main.connections[thread_index], ct);
64 ct->c_c_index = ct - ct_main.connections[thread_index];
65 ct->c_thread_index = thread_index;
Florin Corasd4295e62019-02-22 13:11:38 -080066 ct->client_wrk = ~0;
67 ct->server_wrk = ~0;
Florin Corasda78c5a2021-06-09 14:55:24 -070068 ct->seg_ctx_index = ~0;
69 ct->ct_seg_index = ~0;
Florin Corasd4295e62019-02-22 13:11:38 -080070 return ct;
71}
72
Florin Coras653e43f2019-03-04 10:56:23 -080073static ct_connection_t *
Florin Coras2d0e3de2020-10-23 16:31:40 -070074ct_connection_get (u32 ct_index, u32 thread_index)
Florin Corasd4295e62019-02-22 13:11:38 -080075{
Florin Coras2d0e3de2020-10-23 16:31:40 -070076 if (pool_is_free_index (ct_main.connections[thread_index], ct_index))
Florin Corasd4295e62019-02-22 13:11:38 -080077 return 0;
Florin Coras2d0e3de2020-10-23 16:31:40 -070078 return pool_elt_at_index (ct_main.connections[thread_index], ct_index);
Florin Corasba7d8f52019-02-22 13:11:38 -080079}
80
Florin Coras653e43f2019-03-04 10:56:23 -080081static void
Florin Corasd4295e62019-02-22 13:11:38 -080082ct_connection_free (ct_connection_t * ct)
Florin Corasba7d8f52019-02-22 13:11:38 -080083{
Florin Corasba7d8f52019-02-22 13:11:38 -080084 if (CLIB_DEBUG)
Florin Coras2d0e3de2020-10-23 16:31:40 -070085 {
86 u32 thread_index = ct->c_thread_index;
87 memset (ct, 0xfc, sizeof (*ct));
88 pool_put (ct_main.connections[thread_index], ct);
89 return;
90 }
91 pool_put (ct_main.connections[ct->c_thread_index], ct);
Florin Corasba7d8f52019-02-22 13:11:38 -080092}
93
Florin Coras374df7a2021-05-13 11:37:43 -070094static ct_connection_t *
95ct_half_open_alloc (void)
96{
97 ct_main_t *cm = &ct_main;
98 u32 *hip;
99
100 clib_spinlock_lock (&cm->ho_reuseable_lock);
101 vec_foreach (hip, cm->ho_reusable)
102 pool_put_index (cm->connections[0], *hip);
103 vec_reset_length (cm->ho_reusable);
104 clib_spinlock_unlock (&cm->ho_reuseable_lock);
105
106 return ct_connection_alloc (0);
107}
108
109void
110ct_half_open_add_reusable (u32 ho_index)
111{
112 ct_main_t *cm = &ct_main;
113
114 clib_spinlock_lock (&cm->ho_reuseable_lock);
115 vec_add1 (cm->ho_reusable, ho_index);
116 clib_spinlock_unlock (&cm->ho_reuseable_lock);
117}
118
Florin Coras2b81e3c2019-02-27 07:55:46 -0800119session_t *
120ct_session_get_peer (session_t * s)
121{
122 ct_connection_t *ct, *peer_ct;
Florin Coras2d0e3de2020-10-23 16:31:40 -0700123 ct = ct_connection_get (s->connection_index, s->thread_index);
124 peer_ct = ct_connection_get (ct->peer_index, s->thread_index);
125 return session_get (peer_ct->c_s_index, s->thread_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800126}
127
Florin Corasba7d8f52019-02-22 13:11:38 -0800128void
Florin Coras2b81e3c2019-02-27 07:55:46 -0800129ct_session_endpoint (session_t * ll, session_endpoint_t * sep)
Florin Corasba7d8f52019-02-22 13:11:38 -0800130{
Florin Corasd4295e62019-02-22 13:11:38 -0800131 ct_connection_t *ct;
132 ct = (ct_connection_t *) session_get_transport (ll);
133 sep->transport_proto = ct->actual_tp;
134 sep->port = ct->c_lcl_port;
135 sep->is_ip4 = ct->c_is_ip4;
Florin Corasea7e7082020-07-07 17:57:28 -0700136 ip_copy (&sep->ip, &ct->c_lcl_ip, ct->c_is_ip4);
Florin Corasba7d8f52019-02-22 13:11:38 -0800137}
138
Florin Corasda78c5a2021-06-09 14:55:24 -0700139static void
140ct_session_dealloc_fifos (ct_connection_t *ct, svm_fifo_t *rx_fifo,
141 svm_fifo_t *tx_fifo)
Florin Corasba7d8f52019-02-22 13:11:38 -0800142{
Florin Corasda78c5a2021-06-09 14:55:24 -0700143 ct_segments_ctx_t *seg_ctx;
144 ct_main_t *cm = &ct_main;
Florin Corasba7d8f52019-02-22 13:11:38 -0800145 segment_manager_t *sm;
Florin Corasda78c5a2021-06-09 14:55:24 -0700146 app_worker_t *app_wrk;
147 ct_segment_t *ct_seg;
148 fifo_segment_t *fs;
Florin Coras6973c732021-06-17 15:53:38 -0700149 u8 del_segment = 0;
Florin Corasda78c5a2021-06-09 14:55:24 -0700150 u32 seg_index;
Florin Coras6973c732021-06-17 15:53:38 -0700151 int cnt;
Florin Corasda78c5a2021-06-09 14:55:24 -0700152
153 /*
154 * Cleanup fifos
155 */
156
157 sm = segment_manager_get (rx_fifo->segment_manager);
158 seg_index = rx_fifo->segment_index;
159
160 fs = segment_manager_get_segment_w_lock (sm, seg_index);
161 fifo_segment_free_fifo (fs, rx_fifo);
162 fifo_segment_free_fifo (fs, tx_fifo);
163 segment_manager_segment_reader_unlock (sm);
164
165 /*
Florin Coras6973c732021-06-17 15:53:38 -0700166 * Atomically update segment context with readers lock
Florin Corasda78c5a2021-06-09 14:55:24 -0700167 */
168
169 clib_rwlock_reader_lock (&cm->app_segs_lock);
170
171 seg_ctx = pool_elt_at_index (cm->app_seg_ctxs, ct->seg_ctx_index);
172 ct_seg = pool_elt_at_index (seg_ctx->segments, ct->ct_seg_index);
173
174 if (ct->flags & CT_CONN_F_CLIENT)
175 {
176 cnt =
177 __atomic_sub_fetch (&ct_seg->client_n_sessions, 1, __ATOMIC_RELAXED);
Florin Corasda78c5a2021-06-09 14:55:24 -0700178 }
179 else
180 {
181 cnt =
182 __atomic_sub_fetch (&ct_seg->server_n_sessions, 1, __ATOMIC_RELAXED);
Florin Corasda78c5a2021-06-09 14:55:24 -0700183 }
184
Florin Corasda78c5a2021-06-09 14:55:24 -0700185 clib_rwlock_reader_unlock (&cm->app_segs_lock);
186
187 /*
188 * No need to do any app updates, return
189 */
Florin Coras6973c732021-06-17 15:53:38 -0700190 ASSERT (cnt >= 0);
191 if (cnt)
192 return;
193
194 /*
195 * Grab exclusive lock and update flags unless some other thread
196 * added more sessions
197 */
198 clib_rwlock_writer_lock (&cm->app_segs_lock);
199
200 seg_ctx = pool_elt_at_index (cm->app_seg_ctxs, ct->seg_ctx_index);
201 ct_seg = pool_elt_at_index (seg_ctx->segments, ct->ct_seg_index);
202 if (ct->flags & CT_CONN_F_CLIENT)
203 {
204 cnt = ct_seg->client_n_sessions;
205 if (!cnt)
206 ct_seg->flags |= CT_SEGMENT_F_CLIENT_DETACHED;
207 }
208 else
209 {
210 cnt = ct_seg->server_n_sessions;
211 if (!cnt)
212 ct_seg->flags |= CT_SEGMENT_F_SERVER_DETACHED;
213 }
214
215 /*
216 * Remove segment context because both client and server detached
217 */
218
219 if (!cnt && (ct_seg->flags & CT_SEGMENT_F_CLIENT_DETACHED) &&
220 (ct_seg->flags & CT_SEGMENT_F_SERVER_DETACHED))
221 {
222 pool_put_index (seg_ctx->segments, ct->ct_seg_index);
223
224 /*
225 * No more segment indices left, remove the segments context
226 */
227 if (!pool_elts (seg_ctx->segments))
228 {
229 u64 table_handle = seg_ctx->client_wrk << 16 | seg_ctx->server_wrk;
230 table_handle = (u64) seg_ctx->sm_index << 32 | table_handle;
231 hash_unset (cm->app_segs_ctxs_table, table_handle);
232 pool_free (seg_ctx->segments);
233 pool_put_index (cm->app_seg_ctxs, ct->seg_ctx_index);
234 }
235 del_segment = 1;
236 }
237
238 clib_rwlock_writer_unlock (&cm->app_segs_lock);
239
240 /*
241 * Session counter went to zero, notify the app that detached
242 */
Florin Corasda78c5a2021-06-09 14:55:24 -0700243 if (cnt)
244 return;
245
246 if (ct->flags & CT_CONN_F_CLIENT)
247 {
248 app_wrk = app_worker_get_if_valid (ct->client_wrk);
249 /* Determine if client app still needs notification, i.e., if it is
250 * still attached. If client detached and this is the last ct session
251 * on this segment, then its connects segment manager should also be
252 * detached, so do not send notification */
253 if (app_wrk)
254 {
255 segment_manager_t *csm;
256 csm = app_worker_get_connect_segment_manager (app_wrk);
257 if (!segment_manager_app_detached (csm))
258 app_worker_del_segment_notify (app_wrk, ct->segment_handle);
259 }
260 }
261 else if (!segment_manager_app_detached (sm))
262 {
263 app_wrk = app_worker_get (ct->server_wrk);
264 app_worker_del_segment_notify (app_wrk, ct->segment_handle);
265 }
266
Florin Coras6973c732021-06-17 15:53:38 -0700267 if (!del_segment)
Florin Corasda78c5a2021-06-09 14:55:24 -0700268 return;
269
Florin Corasda78c5a2021-06-09 14:55:24 -0700270 segment_manager_lock_and_del_segment (sm, seg_index);
271
272 /* Cleanup segment manager if needed. If server detaches there's a chance
273 * the client's sessions will hold up segment removal */
274 if (segment_manager_app_detached (sm) && !segment_manager_has_fifos (sm))
275 segment_manager_free_safe (sm);
276}
277
278int
Florin Coras6973c732021-06-17 15:53:38 -0700279ct_session_connect_notify (session_t *ss, session_error_t err)
Florin Corasda78c5a2021-06-09 14:55:24 -0700280{
Florin Coras6973c732021-06-17 15:53:38 -0700281 u32 ss_index, opaque, thread_index;
Florin Corasda78c5a2021-06-09 14:55:24 -0700282 ct_connection_t *sct, *cct;
Florin Corasda78c5a2021-06-09 14:55:24 -0700283 app_worker_t *client_wrk;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800284 session_t *cs;
Florin Corasba7d8f52019-02-22 13:11:38 -0800285
Florin Coras2b81e3c2019-02-27 07:55:46 -0800286 ss_index = ss->session_index;
Florin Coras2d0e3de2020-10-23 16:31:40 -0700287 thread_index = ss->thread_index;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800288 sct = (ct_connection_t *) session_get_transport (ss);
289 client_wrk = app_worker_get (sct->client_wrk);
Florin Corasdd7d24b2020-08-14 17:51:13 -0700290 opaque = sct->client_opaque;
Florin Corasba7d8f52019-02-22 13:11:38 -0800291
Florin Coras2d0e3de2020-10-23 16:31:40 -0700292 cct = ct_connection_get (sct->peer_index, thread_index);
Florin Corasba7d8f52019-02-22 13:11:38 -0800293
Florin Coras374df7a2021-05-13 11:37:43 -0700294 /* Client closed while waiting for reply from server */
Florin Coras6973c732021-06-17 15:53:38 -0700295 if (PREDICT_FALSE (!cct))
Florin Coras374df7a2021-05-13 11:37:43 -0700296 {
297 session_transport_closing_notify (&sct->connection);
298 session_transport_delete_notify (&sct->connection);
299 ct_connection_free (sct);
300 return 0;
301 }
302
303 session_half_open_delete_notify (&cct->connection);
304 cct->flags &= ~CT_CONN_F_HALF_OPEN;
305
Florin Coras6973c732021-06-17 15:53:38 -0700306 if (PREDICT_FALSE (err))
307 goto connect_error;
Florin Corasda78c5a2021-06-09 14:55:24 -0700308
309 /*
310 * Alloc client session
311 */
312
Florin Coras2d0e3de2020-10-23 16:31:40 -0700313 cs = session_alloc (thread_index);
314 ss = session_get (ss_index, thread_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800315 cs->session_type = ss->session_type;
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +0200316 cs->listener_handle = SESSION_INVALID_HANDLE;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800317 cs->session_state = SESSION_STATE_CONNECTING;
318 cs->app_wrk_index = client_wrk->wrk_index;
319 cs->connection_index = cct->c_c_index;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800320 cct->c_s_index = cs->session_index;
321
322 /* This will allocate fifos for the session. They won't be used for
323 * exchanging data but they will be used to close the connection if
324 * the segment manager/worker is freed */
Florin Corasdd7d24b2020-08-14 17:51:13 -0700325 if ((err = app_worker_init_connected (client_wrk, cs)))
Florin Coras2b81e3c2019-02-27 07:55:46 -0800326 {
Florin Corasdd7d24b2020-08-14 17:51:13 -0700327 session_free (cs);
Florin Coras6973c732021-06-17 15:53:38 -0700328 session_close (ss);
329 err = SESSION_E_ALLOC;
330 goto connect_error;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800331 }
332
Florin Corasdd7d24b2020-08-14 17:51:13 -0700333 cs->session_state = SESSION_STATE_CONNECTING;
334
Florin Coras6973c732021-06-17 15:53:38 -0700335 if (app_worker_connect_notify (client_wrk, cs, 0, opaque))
Florin Coras2b81e3c2019-02-27 07:55:46 -0800336 {
Florin Coras6973c732021-06-17 15:53:38 -0700337 segment_manager_dealloc_fifos (cs->rx_fifo, cs->tx_fifo);
Florin Corasdd7d24b2020-08-14 17:51:13 -0700338 session_free (cs);
Florin Coras6973c732021-06-17 15:53:38 -0700339 session_close (ss);
340 goto cleanup_client;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800341 }
342
Florin Coras2d0e3de2020-10-23 16:31:40 -0700343 cs = session_get (cct->c_s_index, cct->c_thread_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800344 cs->session_state = SESSION_STATE_READY;
345
Florin Corasba7d8f52019-02-22 13:11:38 -0800346 return 0;
Florin Corasdd7d24b2020-08-14 17:51:13 -0700347
Florin Coras6973c732021-06-17 15:53:38 -0700348connect_error:
349
350 app_worker_connect_notify (client_wrk, 0, err, cct->client_opaque);
351
352cleanup_client:
353
354 if (cct->client_rx_fifo)
355 ct_session_dealloc_fifos (cct, cct->client_rx_fifo, cct->client_tx_fifo);
356 ct_connection_free (cct);
Florin Corasdd7d24b2020-08-14 17:51:13 -0700357 return -1;
Florin Corasba7d8f52019-02-22 13:11:38 -0800358}
359
Florin Coras6973c732021-06-17 15:53:38 -0700360static inline ct_segment_t *
361ct_lookup_free_segment (ct_main_t *cm, segment_manager_t *sm,
362 u32 seg_ctx_index)
Florin Corasba7d8f52019-02-22 13:11:38 -0800363{
Florin Corasda78c5a2021-06-09 14:55:24 -0700364 uword free_bytes, max_free_bytes;
365 ct_segment_t *ct_seg, *res = 0;
Florin Coras6973c732021-06-17 15:53:38 -0700366 ct_segments_ctx_t *seg_ctx;
Florin Corasda78c5a2021-06-09 14:55:24 -0700367 fifo_segment_t *fs;
368 u32 max_fifos;
369
Florin Coras6973c732021-06-17 15:53:38 -0700370 seg_ctx = pool_elt_at_index (cm->app_seg_ctxs, seg_ctx_index);
371 max_free_bytes = seg_ctx->fifo_pair_bytes;
372
Florin Corasda78c5a2021-06-09 14:55:24 -0700373 pool_foreach (ct_seg, seg_ctx->segments)
374 {
375 /* Client or server has detached so segment cannot be used */
376 if ((ct_seg->flags & CT_SEGMENT_F_SERVER_DETACHED) ||
377 (ct_seg->flags & CT_SEGMENT_F_CLIENT_DETACHED))
378 continue;
379 fs = segment_manager_get_segment (sm, ct_seg->segment_index);
380 free_bytes = fifo_segment_available_bytes (fs);
Florin Coras6973c732021-06-17 15:53:38 -0700381 max_fifos = fifo_segment_size (fs) / seg_ctx->fifo_pair_bytes;
Florin Corasda78c5a2021-06-09 14:55:24 -0700382 if (free_bytes > max_free_bytes &&
383 fifo_segment_num_fifos (fs) / 2 < max_fifos)
384 {
385 max_free_bytes = free_bytes;
386 res = ct_seg;
387 }
388 }
389
390 return res;
391}
392
Florin Coras6973c732021-06-17 15:53:38 -0700393static ct_segment_t *
394ct_alloc_segment (ct_main_t *cm, app_worker_t *server_wrk, u64 table_handle,
395 segment_manager_t *sm, u32 client_wrk_index)
396{
397 u32 seg_ctx_index = ~0, sm_index, pair_bytes;
398 segment_manager_props_t *props;
399 const u32 margin = 16 << 10;
400 ct_segments_ctx_t *seg_ctx;
401 app_worker_t *client_wrk;
402 u64 seg_size, seg_handle;
403 application_t *server;
404 ct_segment_t *ct_seg;
405 uword *spp;
406 int fs_index;
407
408 server = application_get (server_wrk->app_index);
409 props = application_segment_manager_properties (server);
410 sm_index = segment_manager_index (sm);
411 pair_bytes = props->rx_fifo_size + props->tx_fifo_size + margin;
412
413 /*
414 * Make sure another thread did not alloc a segment while acquiring the lock
415 */
416
417 spp = hash_get (cm->app_segs_ctxs_table, table_handle);
418 if (spp)
419 {
420 seg_ctx_index = *spp;
421 ct_seg = ct_lookup_free_segment (cm, sm, seg_ctx_index);
422 if (ct_seg)
423 return ct_seg;
424 }
425
426 /*
427 * No segment, try to alloc one and notify the server and the client.
428 * Make sure the segment is not used for other fifos
429 */
430 seg_size = clib_max (props->segment_size, 128 << 20);
431 fs_index =
432 segment_manager_add_segment2 (sm, seg_size, FIFO_SEGMENT_F_CUSTOM_USE);
433 if (fs_index < 0)
434 return 0;
435
436 if (seg_ctx_index == ~0)
437 {
438 pool_get_zero (cm->app_seg_ctxs, seg_ctx);
439 seg_ctx_index = seg_ctx - cm->app_seg_ctxs;
440 hash_set (cm->app_segs_ctxs_table, table_handle, seg_ctx_index);
441 seg_ctx->server_wrk = server_wrk->wrk_index;
442 seg_ctx->client_wrk = client_wrk_index;
443 seg_ctx->sm_index = sm_index;
444 seg_ctx->fifo_pair_bytes = pair_bytes;
445 }
446 else
447 {
448 seg_ctx = pool_elt_at_index (cm->app_seg_ctxs, seg_ctx_index);
449 }
450
451 pool_get_zero (seg_ctx->segments, ct_seg);
452 ct_seg->segment_index = fs_index;
453 ct_seg->server_n_sessions = 0;
454 ct_seg->client_n_sessions = 0;
455 ct_seg->ct_seg_index = ct_seg - seg_ctx->segments;
456 ct_seg->seg_ctx_index = seg_ctx_index;
457
458 /* New segment, notify the server and client */
459 seg_handle = segment_manager_make_segment_handle (sm_index, fs_index);
460 if (app_worker_add_segment_notify (server_wrk, seg_handle))
461 goto error;
462
463 client_wrk = app_worker_get (client_wrk_index);
464 if (app_worker_add_segment_notify (client_wrk, seg_handle))
465 {
466 app_worker_del_segment_notify (server_wrk, seg_handle);
467 goto error;
468 }
469
470 return ct_seg;
471
472error:
473
474 segment_manager_lock_and_del_segment (sm, fs_index);
475 pool_put_index (seg_ctx->segments, ct_seg->seg_ctx_index);
476 return 0;
477}
478
Florin Corasda78c5a2021-06-09 14:55:24 -0700479static int
480ct_init_accepted_session (app_worker_t *server_wrk, ct_connection_t *ct,
481 session_t *ls, session_t *ll)
482{
Florin Coras88001c62019-04-24 14:44:46 -0700483 segment_manager_props_t *props;
Florin Coras6973c732021-06-17 15:53:38 -0700484 u64 seg_handle, table_handle;
485 u32 sm_index, fs_index = ~0;
Florin Corasda78c5a2021-06-09 14:55:24 -0700486 ct_segments_ctx_t *seg_ctx;
487 ct_main_t *cm = &ct_main;
Florin Coras653e43f2019-03-04 10:56:23 -0800488 application_t *server;
Florin Corasba7d8f52019-02-22 13:11:38 -0800489 segment_manager_t *sm;
Florin Corasda78c5a2021-06-09 14:55:24 -0700490 ct_segment_t *ct_seg;
491 fifo_segment_t *fs;
Florin Corasda78c5a2021-06-09 14:55:24 -0700492 uword *spp;
Florin Coras6973c732021-06-17 15:53:38 -0700493 int rv;
Florin Corasba7d8f52019-02-22 13:11:38 -0800494
Florin Coras2b81e3c2019-02-27 07:55:46 -0800495 sm = app_worker_get_listen_segment_manager (server_wrk, ll);
Florin Corasda78c5a2021-06-09 14:55:24 -0700496 sm_index = segment_manager_index (sm);
497 server = application_get (server_wrk->app_index);
498 props = application_segment_manager_properties (server);
Florin Corasba7d8f52019-02-22 13:11:38 -0800499
Florin Corasda78c5a2021-06-09 14:55:24 -0700500 table_handle = ct->client_wrk << 16 | server_wrk->wrk_index;
Florin Coras6973c732021-06-17 15:53:38 -0700501 table_handle = (u64) sm_index << 32 | table_handle;
Florin Corasda78c5a2021-06-09 14:55:24 -0700502
503 /*
504 * Check if we already have a segment that can hold the fifos
505 */
506
507 clib_rwlock_reader_lock (&cm->app_segs_lock);
508
509 spp = hash_get (cm->app_segs_ctxs_table, table_handle);
510 if (spp)
511 {
Florin Coras6973c732021-06-17 15:53:38 -0700512 ct_seg = ct_lookup_free_segment (cm, sm, *spp);
Florin Corasda78c5a2021-06-09 14:55:24 -0700513 if (ct_seg)
514 {
Florin Coras6973c732021-06-17 15:53:38 -0700515 ct->seg_ctx_index = ct_seg->seg_ctx_index;
516 ct->ct_seg_index = ct_seg->ct_seg_index;
Florin Corasda78c5a2021-06-09 14:55:24 -0700517 fs_index = ct_seg->segment_index;
518 __atomic_add_fetch (&ct_seg->server_n_sessions, 1, __ATOMIC_RELAXED);
Florin Coras6973c732021-06-17 15:53:38 -0700519 __atomic_add_fetch (&ct_seg->client_n_sessions, 1, __ATOMIC_RELAXED);
Florin Corasda78c5a2021-06-09 14:55:24 -0700520 }
521 }
522
523 clib_rwlock_reader_unlock (&cm->app_segs_lock);
524
525 /*
Florin Coras6973c732021-06-17 15:53:38 -0700526 * If not, grab exclusive lock and allocate segment
Florin Corasda78c5a2021-06-09 14:55:24 -0700527 */
Florin Coras6973c732021-06-17 15:53:38 -0700528 if (fs_index == ~0)
Florin Corasda78c5a2021-06-09 14:55:24 -0700529 {
Florin Corasda78c5a2021-06-09 14:55:24 -0700530 clib_rwlock_writer_lock (&cm->app_segs_lock);
531
Florin Coras6973c732021-06-17 15:53:38 -0700532 ct_seg =
533 ct_alloc_segment (cm, server_wrk, table_handle, sm, ct->client_wrk);
534 if (!ct_seg)
Florin Corasda78c5a2021-06-09 14:55:24 -0700535 {
Florin Coras6973c732021-06-17 15:53:38 -0700536 clib_rwlock_writer_unlock (&cm->app_segs_lock);
537 return -1;
Florin Corasda78c5a2021-06-09 14:55:24 -0700538 }
Florin Corasda78c5a2021-06-09 14:55:24 -0700539
Florin Coras6973c732021-06-17 15:53:38 -0700540 ct->seg_ctx_index = ct_seg->seg_ctx_index;
541 ct->ct_seg_index = ct_seg->ct_seg_index;
Florin Corasda78c5a2021-06-09 14:55:24 -0700542 ct_seg->server_n_sessions += 1;
Florin Coras6973c732021-06-17 15:53:38 -0700543 ct_seg->client_n_sessions += 1;
544 fs_index = ct_seg->segment_index;
Florin Corasda78c5a2021-06-09 14:55:24 -0700545
546 clib_rwlock_writer_unlock (&cm->app_segs_lock);
Florin Corasda78c5a2021-06-09 14:55:24 -0700547 }
548
549 /*
550 * Allocate and initialize the fifos
551 */
552 fs = segment_manager_get_segment_w_lock (sm, fs_index);
553 rv = segment_manager_try_alloc_fifos (
554 fs, ls->thread_index, props->rx_fifo_size, props->tx_fifo_size,
555 &ls->rx_fifo, &ls->tx_fifo);
Florin Corasba7d8f52019-02-22 13:11:38 -0800556 if (rv)
557 {
Florin Corasba7d8f52019-02-22 13:11:38 -0800558 segment_manager_segment_reader_unlock (sm);
Florin Coras6973c732021-06-17 15:53:38 -0700559
560 clib_rwlock_reader_lock (&cm->app_segs_lock);
561
562 seg_ctx = pool_elt_at_index (cm->app_seg_ctxs, ct->seg_ctx_index);
563 ct_seg = pool_elt_at_index (seg_ctx->segments, ct->ct_seg_index);
564 __atomic_sub_fetch (&ct_seg->server_n_sessions, 1, __ATOMIC_RELAXED);
565 __atomic_sub_fetch (&ct_seg->client_n_sessions, 1, __ATOMIC_RELAXED);
566
567 clib_rwlock_reader_unlock (&cm->app_segs_lock);
568
569 return rv;
Florin Corasba7d8f52019-02-22 13:11:38 -0800570 }
Florin Coras2b81e3c2019-02-27 07:55:46 -0800571
Florin Corasc547e912020-12-08 17:50:45 -0800572 ls->rx_fifo->shr->master_session_index = ls->session_index;
573 ls->tx_fifo->shr->master_session_index = ls->session_index;
Florin Corasce252ca2020-11-04 13:08:35 -0800574 ls->rx_fifo->master_thread_index = ls->thread_index;
575 ls->tx_fifo->master_thread_index = ls->thread_index;
Florin Corasba7d8f52019-02-22 13:11:38 -0800576 ls->rx_fifo->segment_manager = sm_index;
577 ls->tx_fifo->segment_manager = sm_index;
Florin Corasda78c5a2021-06-09 14:55:24 -0700578 ls->rx_fifo->segment_index = fs_index;
579 ls->tx_fifo->segment_index = fs_index;
Florin Corasba7d8f52019-02-22 13:11:38 -0800580
Florin Corasda78c5a2021-06-09 14:55:24 -0700581 seg_handle = segment_manager_segment_handle (sm, fs);
Florin Corasba7d8f52019-02-22 13:11:38 -0800582 segment_manager_segment_reader_unlock (sm);
Florin Corasda78c5a2021-06-09 14:55:24 -0700583
584 ct->segment_handle = seg_handle;
Florin Corasba7d8f52019-02-22 13:11:38 -0800585
586 return 0;
Florin Corasba7d8f52019-02-22 13:11:38 -0800587}
588
Florin Coras2d0e3de2020-10-23 16:31:40 -0700589static void
590ct_accept_rpc_wrk_handler (void *accept_args)
591{
Florin Coras374df7a2021-05-13 11:37:43 -0700592 u32 cct_index, ho_index, thread_index, ll_index;
Florin Corasba026412021-06-12 11:56:19 -0700593 ct_connection_t *sct, *cct, *ho;
594 transport_connection_t *ll_ct;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800595 app_worker_t *server_wrk;
Florin Coras2d0e3de2020-10-23 16:31:40 -0700596 session_t *ss, *ll;
Florin Corasba7d8f52019-02-22 13:11:38 -0800597
Florin Coras374df7a2021-05-13 11:37:43 -0700598 /*
599 * Alloc client ct and initialize from ho
600 */
601 thread_index = vlib_get_thread_index ();
602 cct = ct_connection_alloc (thread_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800603 cct_index = cct->c_c_index;
Florin Coras374df7a2021-05-13 11:37:43 -0700604
605 ho_index = pointer_to_uword (accept_args);
606 ho = ct_connection_get (ho_index, 0);
607
608 /* Unlikely but half-open session and transport could have been freed */
609 if (PREDICT_FALSE (!ho))
610 {
611 ct_connection_free (cct);
612 return;
613 }
614
615 clib_memcpy (cct, ho, sizeof (*ho));
616 cct->c_c_index = cct_index;
617 cct->c_thread_index = thread_index;
618 cct->flags |= CT_CONN_F_HALF_OPEN;
619
620 /* Notify session layer that half-open is on a different thread
621 * and mark ho connection index reusable. Avoids another rpc
622 */
623 session_half_open_migrate_notify (&cct->connection);
624 session_half_open_migrated_notify (&cct->connection);
625 ct_half_open_add_reusable (ho_index);
Florin Corasba7d8f52019-02-22 13:11:38 -0800626
Florin Coras2b81e3c2019-02-27 07:55:46 -0800627 /*
Florin Coras374df7a2021-05-13 11:37:43 -0700628 * Alloc and init server transport
Florin Coras2b81e3c2019-02-27 07:55:46 -0800629 */
Florin Coras374df7a2021-05-13 11:37:43 -0700630
631 ll_index = cct->peer_index;
632 ll = listen_session_get (ll_index);
633 sct = ct_connection_alloc (thread_index);
Florin Corasba026412021-06-12 11:56:19 -0700634 /* Transport not necessarily ct but it might, so grab after sct alloc */
635 ll_ct = listen_session_get_transport (ll);
Florin Coras374df7a2021-05-13 11:37:43 -0700636
637 /* Make sure cct is valid after sct alloc */
638 cct = ct_connection_get (cct_index, thread_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800639
Florin Coras2b81e3c2019-02-27 07:55:46 -0800640 sct->c_rmt_port = 0;
Florin Corasba026412021-06-12 11:56:19 -0700641 sct->c_lcl_port = ll_ct->lcl_port;
Florin Coras374df7a2021-05-13 11:37:43 -0700642 sct->c_is_ip4 = cct->c_is_ip4;
Florin Corasba026412021-06-12 11:56:19 -0700643 clib_memcpy (&sct->c_lcl_ip, &ll_ct->lcl_ip, sizeof (ll_ct->lcl_ip));
Florin Coras374df7a2021-05-13 11:37:43 -0700644 sct->client_wrk = cct->client_wrk;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800645 sct->c_proto = TRANSPORT_PROTO_NONE;
Florin Coras374df7a2021-05-13 11:37:43 -0700646 sct->client_opaque = cct->client_opaque;
Florin Corasba026412021-06-12 11:56:19 -0700647 sct->actual_tp = cct->actual_tp;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800648
649 sct->peer_index = cct->c_c_index;
650 cct->peer_index = sct->c_c_index;
651
652 /*
653 * Accept server session. Client session is created only after
654 * server confirms accept.
655 */
Florin Coras374df7a2021-05-13 11:37:43 -0700656 ss = session_alloc (thread_index);
657 ll = listen_session_get (ll_index);
Florin Coras9f1a5432019-03-10 21:03:20 -0700658 ss->session_type = session_type_from_proto_and_ip (TRANSPORT_PROTO_NONE,
659 sct->c_is_ip4);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800660 ss->connection_index = sct->c_c_index;
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +0200661 ss->listener_handle = listen_session_get_handle (ll);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800662 ss->session_state = SESSION_STATE_CREATED;
663
664 server_wrk = application_listener_select_worker (ll);
665 ss->app_wrk_index = server_wrk->wrk_index;
666
667 sct->c_s_index = ss->session_index;
668 sct->server_wrk = ss->app_wrk_index;
669
Florin Coras2d0e3de2020-10-23 16:31:40 -0700670 if (ct_init_accepted_session (server_wrk, sct, ss, ll))
Florin Corasba7d8f52019-02-22 13:11:38 -0800671 {
Florin Coras6973c732021-06-17 15:53:38 -0700672 ct_session_connect_notify (ss, SESSION_E_ALLOC);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800673 ct_connection_free (sct);
674 session_free (ss);
Florin Coras2d0e3de2020-10-23 16:31:40 -0700675 return;
Florin Corasba7d8f52019-02-22 13:11:38 -0800676 }
677
Florin Coras6973c732021-06-17 15:53:38 -0700678 cct->seg_ctx_index = sct->seg_ctx_index;
679 cct->ct_seg_index = sct->ct_seg_index;
680 cct->client_rx_fifo = ss->tx_fifo;
681 cct->client_tx_fifo = ss->rx_fifo;
682 cct->client_rx_fifo->refcnt++;
683 cct->client_tx_fifo->refcnt++;
684 cct->segment_handle = sct->segment_handle;
685
Florin Coras2b81e3c2019-02-27 07:55:46 -0800686 ss->session_state = SESSION_STATE_ACCEPTING;
687 if (app_worker_accept_notify (server_wrk, ss))
688 {
Florin Coras6973c732021-06-17 15:53:38 -0700689 ct_session_connect_notify (ss, SESSION_E_REFUSED);
Florin Corasda78c5a2021-06-09 14:55:24 -0700690 ct_session_dealloc_fifos (sct, ss->rx_fifo, ss->tx_fifo);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800691 ct_connection_free (sct);
Florin Coras12813d52020-04-09 20:59:20 +0000692 session_free (ss);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800693 }
Florin Coras2d0e3de2020-10-23 16:31:40 -0700694}
695
696static int
697ct_connect (app_worker_t * client_wrk, session_t * ll,
698 session_endpoint_cfg_t * sep)
699{
Florin Coras374df7a2021-05-13 11:37:43 -0700700 u32 thread_index, ho_index;
Florin Coras2d0e3de2020-10-23 16:31:40 -0700701 ct_main_t *cm = &ct_main;
Florin Coras374df7a2021-05-13 11:37:43 -0700702 ct_connection_t *ho;
Florin Coras2d0e3de2020-10-23 16:31:40 -0700703
704 /* Simple round-robin policy for spreading sessions over workers. We skip
705 * thread index 0, i.e., offset the index by 1, when we have workers as it
706 * is the one dedicated to main thread. Note that n_workers does not include
707 * main thread */
708 cm->n_sessions += 1;
709 thread_index = cm->n_workers ? (cm->n_sessions % cm->n_workers) + 1 : 0;
710
Florin Coras374df7a2021-05-13 11:37:43 -0700711 /*
712 * Alloc and init client half-open transport
713 */
Florin Coras2d0e3de2020-10-23 16:31:40 -0700714
Florin Coras374df7a2021-05-13 11:37:43 -0700715 ho = ct_half_open_alloc ();
716 ho_index = ho->c_c_index;
717 ho->c_rmt_port = sep->port;
718 ho->c_lcl_port = 0;
719 ho->c_is_ip4 = sep->is_ip4;
720 ho->client_opaque = sep->opaque;
721 ho->client_wrk = client_wrk->wrk_index;
722 ho->peer_index = ll->session_index;
723 ho->c_proto = TRANSPORT_PROTO_NONE;
724 ho->c_flags |= TRANSPORT_CONNECTION_F_NO_LOOKUP;
Florin Corasd5a58822021-05-14 20:11:14 -0700725 clib_memcpy (&ho->c_rmt_ip, &sep->ip, sizeof (sep->ip));
Florin Coras374df7a2021-05-13 11:37:43 -0700726 ho->flags |= CT_CONN_F_CLIENT;
727 ho->c_s_index = ~0;
Florin Corasba026412021-06-12 11:56:19 -0700728 ho->actual_tp = sep->transport_proto;
Florin Coras374df7a2021-05-13 11:37:43 -0700729
730 /*
731 * Accept connection on thread selected above. Connected reply comes
732 * after server accepts the connection.
733 */
734
735 session_send_rpc_evt_to_thread_force (thread_index,
736 ct_accept_rpc_wrk_handler,
737 uword_to_pointer (ho_index, void *));
738
739 return ho_index;
Florin Corasba7d8f52019-02-22 13:11:38 -0800740}
741
Florin Coras653e43f2019-03-04 10:56:23 -0800742static u32
Florin Corasd4295e62019-02-22 13:11:38 -0800743ct_start_listen (u32 app_listener_index, transport_endpoint_t * tep)
744{
745 session_endpoint_cfg_t *sep;
746 ct_connection_t *ct;
747
748 sep = (session_endpoint_cfg_t *) tep;
Florin Coras2d0e3de2020-10-23 16:31:40 -0700749 ct = ct_connection_alloc (0);
Florin Corasd4295e62019-02-22 13:11:38 -0800750 ct->server_wrk = sep->app_wrk_index;
751 ct->c_is_ip4 = sep->is_ip4;
752 clib_memcpy (&ct->c_lcl_ip, &sep->ip, sizeof (sep->ip));
753 ct->c_lcl_port = sep->port;
Florin Corasa8375082020-10-25 19:06:57 -0700754 ct->c_s_index = app_listener_index;
Florin Corasd4295e62019-02-22 13:11:38 -0800755 ct->actual_tp = sep->transport_proto;
756 return ct->c_c_index;
757}
758
Florin Coras653e43f2019-03-04 10:56:23 -0800759static u32
Florin Corasd4295e62019-02-22 13:11:38 -0800760ct_stop_listen (u32 ct_index)
761{
762 ct_connection_t *ct;
Florin Coras2d0e3de2020-10-23 16:31:40 -0700763 ct = ct_connection_get (ct_index, 0);
Florin Corasd4295e62019-02-22 13:11:38 -0800764 ct_connection_free (ct);
765 return 0;
766}
767
Florin Coras653e43f2019-03-04 10:56:23 -0800768static transport_connection_t *
Florin Corasd4295e62019-02-22 13:11:38 -0800769ct_listener_get (u32 ct_index)
770{
Florin Coras2d0e3de2020-10-23 16:31:40 -0700771 return (transport_connection_t *) ct_connection_get (ct_index, 0);
Florin Corasd4295e62019-02-22 13:11:38 -0800772}
773
Florin Coras374df7a2021-05-13 11:37:43 -0700774static transport_connection_t *
775ct_half_open_get (u32 ct_index)
776{
777 return (transport_connection_t *) ct_connection_get (ct_index, 0);
778}
779
780static void
781ct_session_cleanup (u32 conn_index, u32 thread_index)
782{
783 ct_connection_t *ct, *peer_ct;
784
785 ct = ct_connection_get (conn_index, thread_index);
786 if (!ct)
787 return;
788
789 peer_ct = ct_connection_get (ct->peer_index, thread_index);
790 if (peer_ct)
791 peer_ct->peer_index = ~0;
792
793 ct_connection_free (ct);
794}
795
796static void
797ct_cleanup_ho (u32 ho_index)
798{
799 ct_connection_free (ct_connection_get (ho_index, 0));
800}
801
Florin Coras653e43f2019-03-04 10:56:23 -0800802static int
Florin Coras2b81e3c2019-02-27 07:55:46 -0800803ct_session_connect (transport_endpoint_cfg_t * tep)
804{
805 session_endpoint_cfg_t *sep_ext;
Florin Coras374df7a2021-05-13 11:37:43 -0700806 session_endpoint_t _sep, *sep = &_sep;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800807 app_worker_t *app_wrk;
808 session_handle_t lh;
809 application_t *app;
810 app_listener_t *al;
811 u32 table_index;
812 session_t *ll;
813 u8 fib_proto;
814
815 sep_ext = (session_endpoint_cfg_t *) tep;
Florin Coras374df7a2021-05-13 11:37:43 -0700816 _sep = *(session_endpoint_t *) tep;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800817 app_wrk = app_worker_get (sep_ext->app_wrk_index);
818 app = application_get (app_wrk->app_index);
819
820 sep->transport_proto = sep_ext->original_tp;
821 table_index = application_local_session_table (app);
822 lh = session_lookup_local_endpoint (table_index, sep);
823 if (lh == SESSION_DROP_HANDLE)
Florin Coras00e01d32019-10-21 16:07:46 -0700824 return SESSION_E_FILTERED;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800825
826 if (lh == SESSION_INVALID_HANDLE)
827 goto global_scope;
828
829 ll = listen_session_get_from_handle (lh);
830 al = app_listener_get_w_session (ll);
831
832 /*
833 * Break loop if rule in local table points to connecting app. This
834 * can happen if client is a generic proxy. Route connect through
835 * global table instead.
836 */
837 if (al->app_index == app->app_index)
838 goto global_scope;
839
840 return ct_connect (app_wrk, ll, sep_ext);
841
842 /*
843 * If nothing found, check the global scope for locally attached
844 * destinations. Make sure first that we're allowed to.
845 */
846
847global_scope:
848 if (session_endpoint_is_local (sep))
Florin Coras00e01d32019-10-21 16:07:46 -0700849 return SESSION_E_NOROUTE;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800850
851 if (!application_has_global_scope (app))
Florin Coras00e01d32019-10-21 16:07:46 -0700852 return SESSION_E_SCOPE;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800853
854 fib_proto = session_endpoint_fib_proto (sep);
Florin Coras95cd8642019-12-30 21:53:19 -0800855 table_index = session_lookup_get_index_for_fib (fib_proto, sep->fib_index);
Florin Coras9f1a5432019-03-10 21:03:20 -0700856 ll = session_lookup_listener_wildcard (table_index, sep);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800857
Florin Coras821b5002021-06-02 21:32:39 -0700858 /* Avoid connecting app to own listener */
859 if (ll && ll->app_index != app->app_index)
Florin Coras2b81e3c2019-02-27 07:55:46 -0800860 return ct_connect (app_wrk, ll, sep_ext);
861
862 /* Failed to connect but no error */
Florin Coras374df7a2021-05-13 11:37:43 -0700863 return SESSION_E_LOCAL_CONNECT;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800864}
865
Florin Coras653e43f2019-03-04 10:56:23 -0800866static void
Florin Coras2b81e3c2019-02-27 07:55:46 -0800867ct_session_close (u32 ct_index, u32 thread_index)
868{
869 ct_connection_t *ct, *peer_ct;
Florin Coras653e43f2019-03-04 10:56:23 -0800870 app_worker_t *app_wrk;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800871 session_t *s;
872
Florin Coras2d0e3de2020-10-23 16:31:40 -0700873 ct = ct_connection_get (ct_index, thread_index);
Florin Coras6973c732021-06-17 15:53:38 -0700874 s = session_get (ct->c_s_index, ct->c_thread_index);
Florin Coras2d0e3de2020-10-23 16:31:40 -0700875 peer_ct = ct_connection_get (ct->peer_index, thread_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800876 if (peer_ct)
877 {
878 peer_ct->peer_index = ~0;
Florin Coras06ac4bb2020-10-28 16:41:26 -0700879 /* Make sure session was allocated */
Florin Coras374df7a2021-05-13 11:37:43 -0700880 if (peer_ct->flags & CT_CONN_F_HALF_OPEN)
881 {
Florin Coras6973c732021-06-17 15:53:38 -0700882 ct_session_connect_notify (s, SESSION_E_REFUSED);
Florin Coras374df7a2021-05-13 11:37:43 -0700883 }
884 else if (peer_ct->c_s_index != ~0)
Florin Coras06ac4bb2020-10-28 16:41:26 -0700885 session_transport_closing_notify (&peer_ct->connection);
886 else
887 ct_connection_free (peer_ct);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800888 }
889
Florin Coras374df7a2021-05-13 11:37:43 -0700890 if (ct->flags & CT_CONN_F_CLIENT)
Florin Corasda78c5a2021-06-09 14:55:24 -0700891 {
892 /* Normal free for client session as the fifos are allocated through
893 * the connects segment manager in a segment that's not shared with
894 * the server */
895 session_free_w_fifos (s);
896 ct_session_dealloc_fifos (ct, ct->client_rx_fifo, ct->client_tx_fifo);
897 }
898 else
899 {
900 /* Manual session and fifo segment cleanup to avoid implicit
901 * segment manager cleanups and notifications */
902 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
903 if (app_wrk)
904 app_worker_cleanup_notify (app_wrk, s, SESSION_CLEANUP_SESSION);
905
906 ct_session_dealloc_fifos (ct, s->rx_fifo, s->tx_fifo);
907 session_free (s);
908 }
Florin Coras653e43f2019-03-04 10:56:23 -0800909
Florin Coras2b81e3c2019-02-27 07:55:46 -0800910 ct_connection_free (ct);
911}
912
Florin Coras653e43f2019-03-04 10:56:23 -0800913static transport_connection_t *
Florin Coras2b81e3c2019-02-27 07:55:46 -0800914ct_session_get (u32 ct_index, u32 thread_index)
915{
Florin Coras2d0e3de2020-10-23 16:31:40 -0700916 return (transport_connection_t *) ct_connection_get (ct_index,
917 thread_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800918}
919
Florin Corasd4295e62019-02-22 13:11:38 -0800920static u8 *
921format_ct_connection_id (u8 * s, va_list * args)
922{
923 ct_connection_t *ct = va_arg (*args, ct_connection_t *);
924 if (!ct)
925 return s;
926 if (ct->c_is_ip4)
927 {
928 s = format (s, "[%d:%d][CT:%U] %U:%d->%U:%d", ct->c_thread_index,
929 ct->c_s_index, format_transport_proto_short, ct->actual_tp,
930 format_ip4_address, &ct->c_lcl_ip4,
931 clib_net_to_host_u16 (ct->c_lcl_port), format_ip4_address,
932 &ct->c_rmt_ip4, clib_net_to_host_u16 (ct->c_rmt_port));
933 }
934 else
935 {
936 s = format (s, "[%d:%d][CT:%U] %U:%d->%U:%d", ct->c_thread_index,
937 ct->c_s_index, format_transport_proto_short, ct->actual_tp,
938 format_ip6_address, &ct->c_lcl_ip6,
939 clib_net_to_host_u16 (ct->c_lcl_port), format_ip6_address,
940 &ct->c_rmt_ip6, clib_net_to_host_u16 (ct->c_rmt_port));
941 }
942
943 return s;
944}
945
Florin Corasf940f8a2019-03-06 10:44:38 -0800946static int
Florin Coras9f86d222020-03-23 15:34:22 +0000947ct_custom_tx (void *session, transport_send_params_t * sp)
Florin Corasf940f8a2019-03-06 10:44:38 -0800948{
949 session_t *s = (session_t *) session;
950 if (session_has_transport (s))
951 return 0;
Florin Coras1b9d2c22021-01-26 14:10:43 -0800952 /* If event enqueued towards peer, remove from scheduler and remove
953 * session tx flag, i.e., accept new tx events. Unset fifo flag now to
954 * avoid missing events if peer did not clear fifo flag yet, which is
955 * interpreted as successful notification and session is descheduled. */
956 svm_fifo_unset_event (s->tx_fifo);
Florin Corasd6894562020-10-28 00:37:15 -0700957 if (!ct_session_tx (s))
Florin Coras1b9d2c22021-01-26 14:10:43 -0800958 sp->flags = TRANSPORT_SND_F_DESCHED;
959
Florin Corasd6894562020-10-28 00:37:15 -0700960 /* The scheduler uses packet count as a means of upper bounding the amount
961 * of work done per dispatch. So make it look like we have sent something */
962 return 1;
Florin Corasf940f8a2019-03-06 10:44:38 -0800963}
964
Florin Coras317b8e02019-04-17 09:57:46 -0700965static int
966ct_app_rx_evt (transport_connection_t * tc)
967{
968 ct_connection_t *ct = (ct_connection_t *) tc, *peer_ct;
969 session_t *ps;
970
Florin Coras2d0e3de2020-10-23 16:31:40 -0700971 peer_ct = ct_connection_get (ct->peer_index, tc->thread_index);
Florin Coras317b8e02019-04-17 09:57:46 -0700972 if (!peer_ct)
973 return -1;
974 ps = session_get (peer_ct->c_s_index, peer_ct->c_thread_index);
975 return session_dequeue_notify (ps);
976}
977
Florin Coras653e43f2019-03-04 10:56:23 -0800978static u8 *
Florin Corasd4295e62019-02-22 13:11:38 -0800979format_ct_listener (u8 * s, va_list * args)
980{
981 u32 tc_index = va_arg (*args, u32);
Aloys Augustina0abbff2019-07-12 12:16:16 +0200982 u32 __clib_unused thread_index = va_arg (*args, u32);
Florin Corasd4295e62019-02-22 13:11:38 -0800983 u32 __clib_unused verbose = va_arg (*args, u32);
Florin Coras2d0e3de2020-10-23 16:31:40 -0700984 ct_connection_t *ct = ct_connection_get (tc_index, 0);
Florin Coras7bf6ed62020-09-23 12:02:08 -0700985 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_ct_connection_id, ct);
Florin Corasd4295e62019-02-22 13:11:38 -0800986 if (verbose)
Florin Coras7bf6ed62020-09-23 12:02:08 -0700987 s = format (s, "%-" SESSION_CLI_STATE_LEN "s", "LISTEN");
Florin Corasd4295e62019-02-22 13:11:38 -0800988 return s;
989}
990
Florin Coras653e43f2019-03-04 10:56:23 -0800991static u8 *
Florin Coras374df7a2021-05-13 11:37:43 -0700992format_ct_half_open (u8 *s, va_list *args)
993{
994 u32 ho_index = va_arg (*args, u32);
995 u32 verbose = va_arg (*args, u32);
996 ct_connection_t *ct = ct_connection_get (ho_index, 0);
997 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_ct_connection_id, ct);
998 if (verbose)
999 s = format (s, "%-" SESSION_CLI_STATE_LEN "s", "HALF-OPEN");
1000 return s;
1001}
1002
1003static u8 *
Florin Coras2b81e3c2019-02-27 07:55:46 -08001004format_ct_connection (u8 * s, va_list * args)
1005{
1006 ct_connection_t *ct = va_arg (*args, ct_connection_t *);
1007 u32 verbose = va_arg (*args, u32);
1008
1009 if (!ct)
1010 return s;
Florin Coras7bf6ed62020-09-23 12:02:08 -07001011 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_ct_connection_id, ct);
Florin Coras2b81e3c2019-02-27 07:55:46 -08001012 if (verbose)
1013 {
Florin Coras7bf6ed62020-09-23 12:02:08 -07001014 s = format (s, "%-" SESSION_CLI_STATE_LEN "s", "ESTABLISHED");
Florin Coras2b81e3c2019-02-27 07:55:46 -08001015 if (verbose > 1)
1016 {
1017 s = format (s, "\n");
1018 }
1019 }
1020 return s;
1021}
1022
Florin Coras653e43f2019-03-04 10:56:23 -08001023static u8 *
Florin Coras2b81e3c2019-02-27 07:55:46 -08001024format_ct_session (u8 * s, va_list * args)
1025{
1026 u32 ct_index = va_arg (*args, u32);
Florin Coras2d0e3de2020-10-23 16:31:40 -07001027 u32 thread_index = va_arg (*args, u32);
Florin Coras2b81e3c2019-02-27 07:55:46 -08001028 u32 verbose = va_arg (*args, u32);
1029 ct_connection_t *ct;
1030
Florin Coras2d0e3de2020-10-23 16:31:40 -07001031 ct = ct_connection_get (ct_index, thread_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -08001032 if (!ct)
1033 {
1034 s = format (s, "empty\n");
1035 return s;
1036 }
1037
1038 s = format (s, "%U", format_ct_connection, ct, verbose);
1039 return s;
1040}
1041
Florin Coras2d0e3de2020-10-23 16:31:40 -07001042clib_error_t *
1043ct_enable_disable (vlib_main_t * vm, u8 is_en)
1044{
Florin Coras374df7a2021-05-13 11:37:43 -07001045 ct_main_t *cm = &ct_main;
1046
1047 cm->n_workers = vlib_num_workers ();
1048 vec_validate (cm->connections, cm->n_workers);
1049 clib_spinlock_init (&cm->ho_reuseable_lock);
Florin Corasda78c5a2021-06-09 14:55:24 -07001050 clib_rwlock_init (&cm->app_segs_lock);
Florin Coras2d0e3de2020-10-23 16:31:40 -07001051 return 0;
1052}
1053
Florin Corasd4295e62019-02-22 13:11:38 -08001054/* *INDENT-OFF* */
Nathan Skrzypczake971bc92019-06-19 13:42:37 +02001055static const transport_proto_vft_t cut_thru_proto = {
Florin Coras2d0e3de2020-10-23 16:31:40 -07001056 .enable = ct_enable_disable,
Florin Corasd4295e62019-02-22 13:11:38 -08001057 .start_listen = ct_start_listen,
1058 .stop_listen = ct_stop_listen,
Florin Coras374df7a2021-05-13 11:37:43 -07001059 .get_connection = ct_session_get,
Florin Corasd4295e62019-02-22 13:11:38 -08001060 .get_listener = ct_listener_get,
Florin Coras374df7a2021-05-13 11:37:43 -07001061 .get_half_open = ct_half_open_get,
1062 .cleanup = ct_session_cleanup,
1063 .cleanup_ho = ct_cleanup_ho,
Florin Coras2b81e3c2019-02-27 07:55:46 -08001064 .connect = ct_session_connect,
1065 .close = ct_session_close,
Florin Corasf940f8a2019-03-06 10:44:38 -08001066 .custom_tx = ct_custom_tx,
Florin Coras317b8e02019-04-17 09:57:46 -07001067 .app_rx_evt = ct_app_rx_evt,
Florin Corasd4295e62019-02-22 13:11:38 -08001068 .format_listener = format_ct_listener,
Florin Coras374df7a2021-05-13 11:37:43 -07001069 .format_half_open = format_ct_half_open,
Florin Coras2b81e3c2019-02-27 07:55:46 -08001070 .format_connection = format_ct_session,
Nathan Skrzypczake971bc92019-06-19 13:42:37 +02001071 .transport_options = {
Florin Coras07063b82020-03-13 04:44:51 +00001072 .name = "ct",
1073 .short_name = "C",
Nathan Skrzypczake971bc92019-06-19 13:42:37 +02001074 .tx_type = TRANSPORT_TX_INTERNAL,
Florin Coras374df7a2021-05-13 11:37:43 -07001075 .service_type = TRANSPORT_SERVICE_VC,
Nathan Skrzypczake971bc92019-06-19 13:42:37 +02001076 },
Florin Corasd4295e62019-02-22 13:11:38 -08001077};
1078/* *INDENT-ON* */
1079
Florin Coras653e43f2019-03-04 10:56:23 -08001080int
1081ct_session_tx (session_t * s)
1082{
1083 ct_connection_t *ct, *peer_ct;
1084 session_t *peer_s;
1085
1086 ct = (ct_connection_t *) session_get_transport (s);
Florin Coras2d0e3de2020-10-23 16:31:40 -07001087 peer_ct = ct_connection_get (ct->peer_index, ct->c_thread_index);
Florin Coras653e43f2019-03-04 10:56:23 -08001088 if (!peer_ct)
Florin Corasbcdc50d2020-08-25 19:07:02 -07001089 return 0;
Florin Coras2d0e3de2020-10-23 16:31:40 -07001090 peer_s = session_get (peer_ct->c_s_index, peer_ct->c_thread_index);
Florin Coras653e43f2019-03-04 10:56:23 -08001091 if (peer_s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
1092 return 0;
Florin Corasd6894562020-10-28 00:37:15 -07001093 return session_enqueue_notify (peer_s);
Florin Coras653e43f2019-03-04 10:56:23 -08001094}
1095
Florin Corasd4295e62019-02-22 13:11:38 -08001096static clib_error_t *
1097ct_transport_init (vlib_main_t * vm)
1098{
1099 transport_register_protocol (TRANSPORT_PROTO_NONE, &cut_thru_proto,
1100 FIB_PROTOCOL_IP4, ~0);
Florin Coras653e43f2019-03-04 10:56:23 -08001101 transport_register_protocol (TRANSPORT_PROTO_NONE, &cut_thru_proto,
1102 FIB_PROTOCOL_IP6, ~0);
Florin Corasd4295e62019-02-22 13:11:38 -08001103 return 0;
1104}
1105
1106VLIB_INIT_FUNCTION (ct_transport_init);
1107
Florin Corasba7d8f52019-02-22 13:11:38 -08001108/*
1109 * fd.io coding-style-patch-verification: ON
1110 *
1111 * Local Variables:
1112 * eval: (c-set-style "gnu")
1113 * End:
1114 */