blob: 478fc7a933bef0271cfe9cb014da20803f8f7f37 [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 Coras2d0e3de2020-10-23 16:31:40 -070019typedef struct ct_main_
Florin Coras653e43f2019-03-04 10:56:23 -080020{
Florin Coras2d0e3de2020-10-23 16:31:40 -070021 ct_connection_t **connections; /**< Per-worker connection pools */
22 u32 n_workers; /**< Number of vpp workers */
23 u32 n_sessions; /**< Cumulative sessions counter */
24} ct_main_t;
Florin Coras653e43f2019-03-04 10:56:23 -080025
Florin Coras2d0e3de2020-10-23 16:31:40 -070026static ct_main_t ct_main;
Florin Coras653e43f2019-03-04 10:56:23 -080027
28static ct_connection_t *
Florin Coras2d0e3de2020-10-23 16:31:40 -070029ct_connection_alloc (u32 thread_index)
Florin Corasd4295e62019-02-22 13:11:38 -080030{
31 ct_connection_t *ct;
32
Florin Coras2d0e3de2020-10-23 16:31:40 -070033 pool_get_zero (ct_main.connections[thread_index], ct);
34 ct->c_c_index = ct - ct_main.connections[thread_index];
35 ct->c_thread_index = thread_index;
Florin Corasd4295e62019-02-22 13:11:38 -080036 ct->client_wrk = ~0;
37 ct->server_wrk = ~0;
38 return ct;
39}
40
Florin Coras653e43f2019-03-04 10:56:23 -080041static ct_connection_t *
Florin Coras2d0e3de2020-10-23 16:31:40 -070042ct_connection_get (u32 ct_index, u32 thread_index)
Florin Corasd4295e62019-02-22 13:11:38 -080043{
Florin Coras2d0e3de2020-10-23 16:31:40 -070044 if (pool_is_free_index (ct_main.connections[thread_index], ct_index))
Florin Corasd4295e62019-02-22 13:11:38 -080045 return 0;
Florin Coras2d0e3de2020-10-23 16:31:40 -070046 return pool_elt_at_index (ct_main.connections[thread_index], ct_index);
Florin Corasba7d8f52019-02-22 13:11:38 -080047}
48
Florin Coras653e43f2019-03-04 10:56:23 -080049static void
Florin Corasd4295e62019-02-22 13:11:38 -080050ct_connection_free (ct_connection_t * ct)
Florin Corasba7d8f52019-02-22 13:11:38 -080051{
Florin Corasba7d8f52019-02-22 13:11:38 -080052 if (CLIB_DEBUG)
Florin Coras2d0e3de2020-10-23 16:31:40 -070053 {
54 u32 thread_index = ct->c_thread_index;
55 memset (ct, 0xfc, sizeof (*ct));
56 pool_put (ct_main.connections[thread_index], ct);
57 return;
58 }
59 pool_put (ct_main.connections[ct->c_thread_index], ct);
Florin Corasba7d8f52019-02-22 13:11:38 -080060}
61
Florin Coras2b81e3c2019-02-27 07:55:46 -080062session_t *
63ct_session_get_peer (session_t * s)
64{
65 ct_connection_t *ct, *peer_ct;
Florin Coras2d0e3de2020-10-23 16:31:40 -070066 ct = ct_connection_get (s->connection_index, s->thread_index);
67 peer_ct = ct_connection_get (ct->peer_index, s->thread_index);
68 return session_get (peer_ct->c_s_index, s->thread_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -080069}
70
Florin Corasba7d8f52019-02-22 13:11:38 -080071void
Florin Coras2b81e3c2019-02-27 07:55:46 -080072ct_session_endpoint (session_t * ll, session_endpoint_t * sep)
Florin Corasba7d8f52019-02-22 13:11:38 -080073{
Florin Corasd4295e62019-02-22 13:11:38 -080074 ct_connection_t *ct;
75 ct = (ct_connection_t *) session_get_transport (ll);
76 sep->transport_proto = ct->actual_tp;
77 sep->port = ct->c_lcl_port;
78 sep->is_ip4 = ct->c_is_ip4;
Florin Corasea7e7082020-07-07 17:57:28 -070079 ip_copy (&sep->ip, &ct->c_lcl_ip, ct->c_is_ip4);
Florin Corasba7d8f52019-02-22 13:11:38 -080080}
81
Florin Corasba7d8f52019-02-22 13:11:38 -080082int
Florin Coras2b81e3c2019-02-27 07:55:46 -080083ct_session_connect_notify (session_t * ss)
Florin Corasba7d8f52019-02-22 13:11:38 -080084{
Florin Coras2d0e3de2020-10-23 16:31:40 -070085 u32 ss_index, opaque, thread_index;
Florin Coras2b81e3c2019-02-27 07:55:46 -080086 ct_connection_t *sct, *cct;
87 app_worker_t *client_wrk;
Florin Corasba7d8f52019-02-22 13:11:38 -080088 segment_manager_t *sm;
Florin Coras88001c62019-04-24 14:44:46 -070089 fifo_segment_t *seg;
Florin Corasba7d8f52019-02-22 13:11:38 -080090 u64 segment_handle;
Florin Coras00e01d32019-10-21 16:07:46 -070091 int err = 0;
Florin Coras2b81e3c2019-02-27 07:55:46 -080092 session_t *cs;
Florin Corasba7d8f52019-02-22 13:11:38 -080093
Florin Coras2b81e3c2019-02-27 07:55:46 -080094 ss_index = ss->session_index;
Florin Coras2d0e3de2020-10-23 16:31:40 -070095 thread_index = ss->thread_index;
Florin Coras2b81e3c2019-02-27 07:55:46 -080096 sct = (ct_connection_t *) session_get_transport (ss);
97 client_wrk = app_worker_get (sct->client_wrk);
Florin Corasdd7d24b2020-08-14 17:51:13 -070098 opaque = sct->client_opaque;
Florin Corasba7d8f52019-02-22 13:11:38 -080099
Florin Coras2b81e3c2019-02-27 07:55:46 -0800100 sm = segment_manager_get (ss->rx_fifo->segment_manager);
101 seg = segment_manager_get_segment_w_lock (sm, ss->rx_fifo->segment_index);
Florin Corasba7d8f52019-02-22 13:11:38 -0800102 segment_handle = segment_manager_segment_handle (sm, seg);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800103
Florin Coras00e01d32019-10-21 16:07:46 -0700104 if ((err = app_worker_add_segment_notify (client_wrk, segment_handle)))
Florin Corasba7d8f52019-02-22 13:11:38 -0800105 {
106 clib_warning ("failed to notify client %u of new segment",
Florin Coras2b81e3c2019-02-27 07:55:46 -0800107 sct->client_wrk);
Florin Corasba7d8f52019-02-22 13:11:38 -0800108 segment_manager_segment_reader_unlock (sm);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800109 session_close (ss);
Florin Corasdd7d24b2020-08-14 17:51:13 -0700110 goto error;
Florin Corasba7d8f52019-02-22 13:11:38 -0800111 }
112 else
113 {
114 segment_manager_segment_reader_unlock (sm);
115 }
116
Florin Coras2b81e3c2019-02-27 07:55:46 -0800117 /* Alloc client session */
Florin Coras2d0e3de2020-10-23 16:31:40 -0700118 cct = ct_connection_get (sct->peer_index, thread_index);
Florin Corasba7d8f52019-02-22 13:11:38 -0800119
Florin Coras2d0e3de2020-10-23 16:31:40 -0700120 cs = session_alloc (thread_index);
121 ss = session_get (ss_index, thread_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800122 cs->session_type = ss->session_type;
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +0200123 cs->listener_handle = SESSION_INVALID_HANDLE;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800124 cs->session_state = SESSION_STATE_CONNECTING;
125 cs->app_wrk_index = client_wrk->wrk_index;
126 cs->connection_index = cct->c_c_index;
127
128 cct->c_s_index = cs->session_index;
Florin Coras653e43f2019-03-04 10:56:23 -0800129 cct->client_rx_fifo = ss->tx_fifo;
130 cct->client_tx_fifo = ss->rx_fifo;
131
132 cct->client_rx_fifo->refcnt++;
133 cct->client_tx_fifo->refcnt++;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800134
135 /* This will allocate fifos for the session. They won't be used for
136 * exchanging data but they will be used to close the connection if
137 * the segment manager/worker is freed */
Florin Corasdd7d24b2020-08-14 17:51:13 -0700138 if ((err = app_worker_init_connected (client_wrk, cs)))
Florin Coras2b81e3c2019-02-27 07:55:46 -0800139 {
140 session_close (ss);
Florin Corasdd7d24b2020-08-14 17:51:13 -0700141 session_free (cs);
142 goto error;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800143 }
144
Florin Corasdd7d24b2020-08-14 17:51:13 -0700145 cs->session_state = SESSION_STATE_CONNECTING;
146
147 if (app_worker_connect_notify (client_wrk, cs, err, opaque))
Florin Coras2b81e3c2019-02-27 07:55:46 -0800148 {
149 session_close (ss);
Florin Corasdd7d24b2020-08-14 17:51:13 -0700150 segment_manager_dealloc_fifos (cs->rx_fifo, cs->tx_fifo);
151 session_free (cs);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800152 return -1;
153 }
154
Florin Coras2d0e3de2020-10-23 16:31:40 -0700155 cs = session_get (cct->c_s_index, cct->c_thread_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800156 cs->session_state = SESSION_STATE_READY;
157
Florin Corasba7d8f52019-02-22 13:11:38 -0800158 return 0;
Florin Corasdd7d24b2020-08-14 17:51:13 -0700159
160error:
161 app_worker_connect_notify (client_wrk, 0, err, opaque);
162 return -1;
Florin Corasba7d8f52019-02-22 13:11:38 -0800163}
164
Florin Coras653e43f2019-03-04 10:56:23 -0800165static int
Florin Coras2d0e3de2020-10-23 16:31:40 -0700166ct_init_accepted_session (app_worker_t * server_wrk,
167 ct_connection_t * ct, session_t * ls,
168 session_t * ll)
Florin Corasba7d8f52019-02-22 13:11:38 -0800169{
Florin Coras653e43f2019-03-04 10:56:23 -0800170 u32 round_rx_fifo_sz, round_tx_fifo_sz, sm_index, seg_size;
Florin Coras88001c62019-04-24 14:44:46 -0700171 segment_manager_props_t *props;
Florin Coras653e43f2019-03-04 10:56:23 -0800172 application_t *server;
Florin Corasba7d8f52019-02-22 13:11:38 -0800173 segment_manager_t *sm;
Florin Coras653e43f2019-03-04 10:56:23 -0800174 u32 margin = 16 << 10;
Florin Coras88001c62019-04-24 14:44:46 -0700175 fifo_segment_t *seg;
Florin Corasba7d8f52019-02-22 13:11:38 -0800176 u64 segment_handle;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800177 int seg_index, rv;
Florin Corasba7d8f52019-02-22 13:11:38 -0800178
Florin Corasba7d8f52019-02-22 13:11:38 -0800179 server = application_get (server_wrk->app_index);
Florin Corasba7d8f52019-02-22 13:11:38 -0800180
181 props = application_segment_manager_properties (server);
Florin Corasba7d8f52019-02-22 13:11:38 -0800182 round_rx_fifo_sz = 1 << max_log2 (props->rx_fifo_size);
183 round_tx_fifo_sz = 1 << max_log2 (props->tx_fifo_size);
Florin Corasf22f4e52019-12-19 16:10:58 -0800184 /* Increase size because of inefficient chunk allocations. Depending on
185 * how data is consumed, it may happen that more chunks than needed are
186 * allocated.
187 * TODO should remove once allocations are done more efficiently */
188 seg_size = 4 * (round_rx_fifo_sz + round_tx_fifo_sz + margin);
Florin Corasba7d8f52019-02-22 13:11:38 -0800189
Florin Coras2b81e3c2019-02-27 07:55:46 -0800190 sm = app_worker_get_listen_segment_manager (server_wrk, ll);
Florin Corasba7d8f52019-02-22 13:11:38 -0800191 seg_index = segment_manager_add_segment (sm, seg_size);
192 if (seg_index < 0)
193 {
194 clib_warning ("failed to add new cut-through segment");
195 return seg_index;
196 }
197 seg = segment_manager_get_segment_w_lock (sm, seg_index);
Florin Corasba7d8f52019-02-22 13:11:38 -0800198
Florin Coras62ddc032019-12-08 18:30:42 -0800199 rv = segment_manager_try_alloc_fifos (seg, ls->thread_index,
200 props->rx_fifo_size,
Florin Coras2b81e3c2019-02-27 07:55:46 -0800201 props->tx_fifo_size, &ls->rx_fifo,
202 &ls->tx_fifo);
Florin Corasba7d8f52019-02-22 13:11:38 -0800203 if (rv)
204 {
205 clib_warning ("failed to add fifos in cut-through segment");
206 segment_manager_segment_reader_unlock (sm);
207 goto failed;
208 }
Florin Coras2b81e3c2019-02-27 07:55:46 -0800209
Florin Corasba7d8f52019-02-22 13:11:38 -0800210 sm_index = segment_manager_index (sm);
Florin Corasc547e912020-12-08 17:50:45 -0800211 ls->rx_fifo->shr->master_session_index = ls->session_index;
212 ls->tx_fifo->shr->master_session_index = ls->session_index;
Florin Corasce252ca2020-11-04 13:08:35 -0800213 ls->rx_fifo->master_thread_index = ls->thread_index;
214 ls->tx_fifo->master_thread_index = ls->thread_index;
Florin Corasba7d8f52019-02-22 13:11:38 -0800215 ls->rx_fifo->segment_manager = sm_index;
216 ls->tx_fifo->segment_manager = sm_index;
217 ls->rx_fifo->segment_index = seg_index;
218 ls->tx_fifo->segment_index = seg_index;
Florin Corasba7d8f52019-02-22 13:11:38 -0800219
220 segment_handle = segment_manager_segment_handle (sm, seg);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800221 if ((rv = app_worker_add_segment_notify (server_wrk, segment_handle)))
Florin Corasba7d8f52019-02-22 13:11:38 -0800222 {
223 clib_warning ("failed to notify server of new segment");
224 segment_manager_segment_reader_unlock (sm);
225 goto failed;
226 }
227 segment_manager_segment_reader_unlock (sm);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800228 ct->segment_handle = segment_handle;
Florin Corasba7d8f52019-02-22 13:11:38 -0800229
230 return 0;
231
232failed:
Florin Coras2d0e3de2020-10-23 16:31:40 -0700233 segment_manager_lock_and_del_segment (sm, seg_index);
Florin Corasba7d8f52019-02-22 13:11:38 -0800234 return rv;
235}
236
Florin Coras2d0e3de2020-10-23 16:31:40 -0700237typedef struct ct_accept_rpc_args
Florin Corasba7d8f52019-02-22 13:11:38 -0800238{
Florin Coras2d0e3de2020-10-23 16:31:40 -0700239 u32 ll_s_index;
240 u32 thread_index;
241 ip46_address_t ip;
242 u16 port;
243 u8 is_ip4;
244 u32 opaque;
245 u32 client_wrk_index;
246} ct_accept_rpc_args_t;
247
248static void
249ct_accept_rpc_wrk_handler (void *accept_args)
250{
251 ct_accept_rpc_args_t *args = (ct_accept_rpc_args_t *) accept_args;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800252 ct_connection_t *sct, *cct, *ll_ct;
253 app_worker_t *server_wrk;
Florin Coras2d0e3de2020-10-23 16:31:40 -0700254 session_t *ss, *ll;
255 u32 cct_index;
Florin Corasba7d8f52019-02-22 13:11:38 -0800256
Florin Coras2d0e3de2020-10-23 16:31:40 -0700257 ll = listen_session_get (args->ll_s_index);
Florin Corasba7d8f52019-02-22 13:11:38 -0800258
Florin Coras2d0e3de2020-10-23 16:31:40 -0700259 cct = ct_connection_alloc (args->thread_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800260 cct_index = cct->c_c_index;
Florin Coras2d0e3de2020-10-23 16:31:40 -0700261 sct = ct_connection_alloc (args->thread_index);
262 ll_ct = ct_connection_get (ll->connection_index, 0 /* listener thread */ );
Florin Corasba7d8f52019-02-22 13:11:38 -0800263
Florin Coras2b81e3c2019-02-27 07:55:46 -0800264 /*
265 * Alloc and init client transport
266 */
Florin Coras2d0e3de2020-10-23 16:31:40 -0700267 cct = ct_connection_get (cct_index, args->thread_index);
268 cct->c_rmt_port = args->port;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800269 cct->c_lcl_port = 0;
Florin Coras2d0e3de2020-10-23 16:31:40 -0700270 cct->c_is_ip4 = args->is_ip4;
271 clib_memcpy (&cct->c_rmt_ip, &args->ip, sizeof (args->ip));
Florin Coras2b81e3c2019-02-27 07:55:46 -0800272 cct->actual_tp = ll_ct->actual_tp;
Florin Coras653e43f2019-03-04 10:56:23 -0800273 cct->is_client = 1;
Florin Coras06ac4bb2020-10-28 16:41:26 -0700274 cct->c_s_index = ~0;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800275
276 /*
277 * Init server transport
278 */
Florin Coras2b81e3c2019-02-27 07:55:46 -0800279 sct->c_rmt_port = 0;
280 sct->c_lcl_port = ll_ct->c_lcl_port;
Florin Coras2d0e3de2020-10-23 16:31:40 -0700281 sct->c_is_ip4 = args->is_ip4;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800282 clib_memcpy (&sct->c_lcl_ip, &ll_ct->c_lcl_ip, sizeof (ll_ct->c_lcl_ip));
Florin Coras2d0e3de2020-10-23 16:31:40 -0700283 sct->client_wrk = args->client_wrk_index;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800284 sct->c_proto = TRANSPORT_PROTO_NONE;
Florin Coras2d0e3de2020-10-23 16:31:40 -0700285 sct->client_opaque = args->opaque;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800286 sct->actual_tp = ll_ct->actual_tp;
287
288 sct->peer_index = cct->c_c_index;
289 cct->peer_index = sct->c_c_index;
290
291 /*
292 * Accept server session. Client session is created only after
293 * server confirms accept.
294 */
Florin Coras2d0e3de2020-10-23 16:31:40 -0700295 ss = session_alloc (args->thread_index);
296 ll = listen_session_get (args->ll_s_index);
Florin Coras9f1a5432019-03-10 21:03:20 -0700297 ss->session_type = session_type_from_proto_and_ip (TRANSPORT_PROTO_NONE,
298 sct->c_is_ip4);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800299 ss->connection_index = sct->c_c_index;
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +0200300 ss->listener_handle = listen_session_get_handle (ll);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800301 ss->session_state = SESSION_STATE_CREATED;
302
303 server_wrk = application_listener_select_worker (ll);
304 ss->app_wrk_index = server_wrk->wrk_index;
305
306 sct->c_s_index = ss->session_index;
307 sct->server_wrk = ss->app_wrk_index;
308
Florin Coras2d0e3de2020-10-23 16:31:40 -0700309 if (ct_init_accepted_session (server_wrk, sct, ss, ll))
Florin Corasba7d8f52019-02-22 13:11:38 -0800310 {
Florin Coras2b81e3c2019-02-27 07:55:46 -0800311 ct_connection_free (sct);
312 session_free (ss);
Florin Coras2d0e3de2020-10-23 16:31:40 -0700313 return;
Florin Corasba7d8f52019-02-22 13:11:38 -0800314 }
315
Florin Coras2b81e3c2019-02-27 07:55:46 -0800316 ss->session_state = SESSION_STATE_ACCEPTING;
317 if (app_worker_accept_notify (server_wrk, ss))
318 {
Florin Coras2b81e3c2019-02-27 07:55:46 -0800319 ct_connection_free (sct);
Florin Coras12813d52020-04-09 20:59:20 +0000320 segment_manager_dealloc_fifos (ss->rx_fifo, ss->tx_fifo);
321 session_free (ss);
Florin Coras2d0e3de2020-10-23 16:31:40 -0700322 return;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800323 }
324
Florin Coras2b81e3c2019-02-27 07:55:46 -0800325 cct->segment_handle = sct->segment_handle;
Florin Coras2d0e3de2020-10-23 16:31:40 -0700326
327 clib_mem_free (args);
328}
329
330static int
331ct_connect (app_worker_t * client_wrk, session_t * ll,
332 session_endpoint_cfg_t * sep)
333{
334 ct_accept_rpc_args_t *args;
335 ct_main_t *cm = &ct_main;
336 u32 thread_index;
337
338 /* Simple round-robin policy for spreading sessions over workers. We skip
339 * thread index 0, i.e., offset the index by 1, when we have workers as it
340 * is the one dedicated to main thread. Note that n_workers does not include
341 * main thread */
342 cm->n_sessions += 1;
343 thread_index = cm->n_workers ? (cm->n_sessions % cm->n_workers) + 1 : 0;
344
345 args = clib_mem_alloc (sizeof (*args));
346 args->ll_s_index = ll->session_index;
347 args->thread_index = thread_index;
348 clib_memcpy_fast (&args->ip, &sep->ip, sizeof (ip46_address_t));
349 args->port = sep->port;
350 args->is_ip4 = sep->is_ip4;
351 args->opaque = sep->opaque;
352 args->client_wrk_index = client_wrk->wrk_index;
353
354 session_send_rpc_evt_to_thread (thread_index, ct_accept_rpc_wrk_handler,
355 args);
Florin Corasba7d8f52019-02-22 13:11:38 -0800356 return 0;
357}
358
Florin Coras653e43f2019-03-04 10:56:23 -0800359static u32
Florin Corasd4295e62019-02-22 13:11:38 -0800360ct_start_listen (u32 app_listener_index, transport_endpoint_t * tep)
361{
362 session_endpoint_cfg_t *sep;
363 ct_connection_t *ct;
364
365 sep = (session_endpoint_cfg_t *) tep;
Florin Coras2d0e3de2020-10-23 16:31:40 -0700366 ct = ct_connection_alloc (0);
Florin Corasd4295e62019-02-22 13:11:38 -0800367 ct->server_wrk = sep->app_wrk_index;
368 ct->c_is_ip4 = sep->is_ip4;
369 clib_memcpy (&ct->c_lcl_ip, &sep->ip, sizeof (sep->ip));
370 ct->c_lcl_port = sep->port;
Florin Corasa8375082020-10-25 19:06:57 -0700371 ct->c_s_index = app_listener_index;
Florin Corasd4295e62019-02-22 13:11:38 -0800372 ct->actual_tp = sep->transport_proto;
373 return ct->c_c_index;
374}
375
Florin Coras653e43f2019-03-04 10:56:23 -0800376static u32
Florin Corasd4295e62019-02-22 13:11:38 -0800377ct_stop_listen (u32 ct_index)
378{
379 ct_connection_t *ct;
Florin Coras2d0e3de2020-10-23 16:31:40 -0700380 ct = ct_connection_get (ct_index, 0);
Florin Corasd4295e62019-02-22 13:11:38 -0800381 ct_connection_free (ct);
382 return 0;
383}
384
Florin Coras653e43f2019-03-04 10:56:23 -0800385static transport_connection_t *
Florin Corasd4295e62019-02-22 13:11:38 -0800386ct_listener_get (u32 ct_index)
387{
Florin Coras2d0e3de2020-10-23 16:31:40 -0700388 return (transport_connection_t *) ct_connection_get (ct_index, 0);
Florin Corasd4295e62019-02-22 13:11:38 -0800389}
390
Florin Coras653e43f2019-03-04 10:56:23 -0800391static int
Florin Coras2b81e3c2019-02-27 07:55:46 -0800392ct_session_connect (transport_endpoint_cfg_t * tep)
393{
394 session_endpoint_cfg_t *sep_ext;
395 session_endpoint_t *sep;
396 app_worker_t *app_wrk;
397 session_handle_t lh;
398 application_t *app;
399 app_listener_t *al;
400 u32 table_index;
401 session_t *ll;
402 u8 fib_proto;
403
404 sep_ext = (session_endpoint_cfg_t *) tep;
405 sep = (session_endpoint_t *) tep;
406 app_wrk = app_worker_get (sep_ext->app_wrk_index);
407 app = application_get (app_wrk->app_index);
408
409 sep->transport_proto = sep_ext->original_tp;
410 table_index = application_local_session_table (app);
411 lh = session_lookup_local_endpoint (table_index, sep);
412 if (lh == SESSION_DROP_HANDLE)
Florin Coras00e01d32019-10-21 16:07:46 -0700413 return SESSION_E_FILTERED;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800414
415 if (lh == SESSION_INVALID_HANDLE)
416 goto global_scope;
417
418 ll = listen_session_get_from_handle (lh);
419 al = app_listener_get_w_session (ll);
420
421 /*
422 * Break loop if rule in local table points to connecting app. This
423 * can happen if client is a generic proxy. Route connect through
424 * global table instead.
425 */
426 if (al->app_index == app->app_index)
427 goto global_scope;
428
429 return ct_connect (app_wrk, ll, sep_ext);
430
431 /*
432 * If nothing found, check the global scope for locally attached
433 * destinations. Make sure first that we're allowed to.
434 */
435
436global_scope:
437 if (session_endpoint_is_local (sep))
Florin Coras00e01d32019-10-21 16:07:46 -0700438 return SESSION_E_NOROUTE;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800439
440 if (!application_has_global_scope (app))
Florin Coras00e01d32019-10-21 16:07:46 -0700441 return SESSION_E_SCOPE;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800442
443 fib_proto = session_endpoint_fib_proto (sep);
Florin Coras95cd8642019-12-30 21:53:19 -0800444 table_index = session_lookup_get_index_for_fib (fib_proto, sep->fib_index);
Florin Coras9f1a5432019-03-10 21:03:20 -0700445 ll = session_lookup_listener_wildcard (table_index, sep);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800446
447 if (ll)
448 return ct_connect (app_wrk, ll, sep_ext);
449
450 /* Failed to connect but no error */
451 return 1;
452}
453
Florin Coras653e43f2019-03-04 10:56:23 -0800454static void
Florin Coras2b81e3c2019-02-27 07:55:46 -0800455ct_session_close (u32 ct_index, u32 thread_index)
456{
457 ct_connection_t *ct, *peer_ct;
Florin Coras653e43f2019-03-04 10:56:23 -0800458 app_worker_t *app_wrk;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800459 session_t *s;
460
Florin Coras2d0e3de2020-10-23 16:31:40 -0700461 ct = ct_connection_get (ct_index, thread_index);
462 peer_ct = ct_connection_get (ct->peer_index, thread_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800463 if (peer_ct)
464 {
465 peer_ct->peer_index = ~0;
Florin Coras06ac4bb2020-10-28 16:41:26 -0700466 /* Make sure session was allocated */
467 if (peer_ct->c_s_index != ~0)
468 session_transport_closing_notify (&peer_ct->connection);
469 else
470 ct_connection_free (peer_ct);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800471 }
472
Florin Coras2d0e3de2020-10-23 16:31:40 -0700473 s = session_get (ct->c_s_index, ct->c_thread_index);
Florin Coras653e43f2019-03-04 10:56:23 -0800474 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
475 if (app_wrk)
476 app_worker_del_segment_notify (app_wrk, ct->segment_handle);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800477 session_free_w_fifos (s);
Florin Coras653e43f2019-03-04 10:56:23 -0800478 if (ct->is_client)
479 segment_manager_dealloc_fifos (ct->client_rx_fifo, ct->client_tx_fifo);
480
Florin Coras2b81e3c2019-02-27 07:55:46 -0800481 ct_connection_free (ct);
482}
483
Florin Coras653e43f2019-03-04 10:56:23 -0800484static transport_connection_t *
Florin Coras2b81e3c2019-02-27 07:55:46 -0800485ct_session_get (u32 ct_index, u32 thread_index)
486{
Florin Coras2d0e3de2020-10-23 16:31:40 -0700487 return (transport_connection_t *) ct_connection_get (ct_index,
488 thread_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800489}
490
Florin Corasd4295e62019-02-22 13:11:38 -0800491static u8 *
492format_ct_connection_id (u8 * s, va_list * args)
493{
494 ct_connection_t *ct = va_arg (*args, ct_connection_t *);
495 if (!ct)
496 return s;
497 if (ct->c_is_ip4)
498 {
499 s = format (s, "[%d:%d][CT:%U] %U:%d->%U:%d", ct->c_thread_index,
500 ct->c_s_index, format_transport_proto_short, ct->actual_tp,
501 format_ip4_address, &ct->c_lcl_ip4,
502 clib_net_to_host_u16 (ct->c_lcl_port), format_ip4_address,
503 &ct->c_rmt_ip4, clib_net_to_host_u16 (ct->c_rmt_port));
504 }
505 else
506 {
507 s = format (s, "[%d:%d][CT:%U] %U:%d->%U:%d", ct->c_thread_index,
508 ct->c_s_index, format_transport_proto_short, ct->actual_tp,
509 format_ip6_address, &ct->c_lcl_ip6,
510 clib_net_to_host_u16 (ct->c_lcl_port), format_ip6_address,
511 &ct->c_rmt_ip6, clib_net_to_host_u16 (ct->c_rmt_port));
512 }
513
514 return s;
515}
516
Florin Corasf940f8a2019-03-06 10:44:38 -0800517static int
Florin Coras9f86d222020-03-23 15:34:22 +0000518ct_custom_tx (void *session, transport_send_params_t * sp)
Florin Corasf940f8a2019-03-06 10:44:38 -0800519{
520 session_t *s = (session_t *) session;
521 if (session_has_transport (s))
522 return 0;
Florin Coras1b9d2c22021-01-26 14:10:43 -0800523 /* If event enqueued towards peer, remove from scheduler and remove
524 * session tx flag, i.e., accept new tx events. Unset fifo flag now to
525 * avoid missing events if peer did not clear fifo flag yet, which is
526 * interpreted as successful notification and session is descheduled. */
527 svm_fifo_unset_event (s->tx_fifo);
Florin Corasd6894562020-10-28 00:37:15 -0700528 if (!ct_session_tx (s))
Florin Coras1b9d2c22021-01-26 14:10:43 -0800529 sp->flags = TRANSPORT_SND_F_DESCHED;
530
Florin Corasd6894562020-10-28 00:37:15 -0700531 /* The scheduler uses packet count as a means of upper bounding the amount
532 * of work done per dispatch. So make it look like we have sent something */
533 return 1;
Florin Corasf940f8a2019-03-06 10:44:38 -0800534}
535
Florin Coras317b8e02019-04-17 09:57:46 -0700536static int
537ct_app_rx_evt (transport_connection_t * tc)
538{
539 ct_connection_t *ct = (ct_connection_t *) tc, *peer_ct;
540 session_t *ps;
541
Florin Coras2d0e3de2020-10-23 16:31:40 -0700542 peer_ct = ct_connection_get (ct->peer_index, tc->thread_index);
Florin Coras317b8e02019-04-17 09:57:46 -0700543 if (!peer_ct)
544 return -1;
545 ps = session_get (peer_ct->c_s_index, peer_ct->c_thread_index);
546 return session_dequeue_notify (ps);
547}
548
Florin Coras653e43f2019-03-04 10:56:23 -0800549static u8 *
Florin Corasd4295e62019-02-22 13:11:38 -0800550format_ct_listener (u8 * s, va_list * args)
551{
552 u32 tc_index = va_arg (*args, u32);
Aloys Augustina0abbff2019-07-12 12:16:16 +0200553 u32 __clib_unused thread_index = va_arg (*args, u32);
Florin Corasd4295e62019-02-22 13:11:38 -0800554 u32 __clib_unused verbose = va_arg (*args, u32);
Florin Coras2d0e3de2020-10-23 16:31:40 -0700555 ct_connection_t *ct = ct_connection_get (tc_index, 0);
Florin Coras7bf6ed62020-09-23 12:02:08 -0700556 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_ct_connection_id, ct);
Florin Corasd4295e62019-02-22 13:11:38 -0800557 if (verbose)
Florin Coras7bf6ed62020-09-23 12:02:08 -0700558 s = format (s, "%-" SESSION_CLI_STATE_LEN "s", "LISTEN");
Florin Corasd4295e62019-02-22 13:11:38 -0800559 return s;
560}
561
Florin Coras653e43f2019-03-04 10:56:23 -0800562static u8 *
Florin Coras2b81e3c2019-02-27 07:55:46 -0800563format_ct_connection (u8 * s, va_list * args)
564{
565 ct_connection_t *ct = va_arg (*args, ct_connection_t *);
566 u32 verbose = va_arg (*args, u32);
567
568 if (!ct)
569 return s;
Florin Coras7bf6ed62020-09-23 12:02:08 -0700570 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_ct_connection_id, ct);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800571 if (verbose)
572 {
Florin Coras7bf6ed62020-09-23 12:02:08 -0700573 s = format (s, "%-" SESSION_CLI_STATE_LEN "s", "ESTABLISHED");
Florin Coras2b81e3c2019-02-27 07:55:46 -0800574 if (verbose > 1)
575 {
576 s = format (s, "\n");
577 }
578 }
579 return s;
580}
581
Florin Coras653e43f2019-03-04 10:56:23 -0800582static u8 *
Florin Coras2b81e3c2019-02-27 07:55:46 -0800583format_ct_session (u8 * s, va_list * args)
584{
585 u32 ct_index = va_arg (*args, u32);
Florin Coras2d0e3de2020-10-23 16:31:40 -0700586 u32 thread_index = va_arg (*args, u32);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800587 u32 verbose = va_arg (*args, u32);
588 ct_connection_t *ct;
589
Florin Coras2d0e3de2020-10-23 16:31:40 -0700590 ct = ct_connection_get (ct_index, thread_index);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800591 if (!ct)
592 {
593 s = format (s, "empty\n");
594 return s;
595 }
596
597 s = format (s, "%U", format_ct_connection, ct, verbose);
598 return s;
599}
600
Florin Coras2d0e3de2020-10-23 16:31:40 -0700601clib_error_t *
602ct_enable_disable (vlib_main_t * vm, u8 is_en)
603{
604 ct_main.n_workers = vlib_num_workers ();
605 vec_validate (ct_main.connections, ct_main.n_workers);
606 return 0;
607}
608
Florin Corasd4295e62019-02-22 13:11:38 -0800609/* *INDENT-OFF* */
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200610static const transport_proto_vft_t cut_thru_proto = {
Florin Coras2d0e3de2020-10-23 16:31:40 -0700611 .enable = ct_enable_disable,
Florin Corasd4295e62019-02-22 13:11:38 -0800612 .start_listen = ct_start_listen,
613 .stop_listen = ct_stop_listen,
614 .get_listener = ct_listener_get,
Florin Coras2b81e3c2019-02-27 07:55:46 -0800615 .connect = ct_session_connect,
616 .close = ct_session_close,
617 .get_connection = ct_session_get,
Florin Corasf940f8a2019-03-06 10:44:38 -0800618 .custom_tx = ct_custom_tx,
Florin Coras317b8e02019-04-17 09:57:46 -0700619 .app_rx_evt = ct_app_rx_evt,
Florin Corasd4295e62019-02-22 13:11:38 -0800620 .format_listener = format_ct_listener,
Florin Coras2b81e3c2019-02-27 07:55:46 -0800621 .format_connection = format_ct_session,
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200622 .transport_options = {
Florin Coras07063b82020-03-13 04:44:51 +0000623 .name = "ct",
624 .short_name = "C",
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200625 .tx_type = TRANSPORT_TX_INTERNAL,
626 .service_type = TRANSPORT_SERVICE_APP,
627 },
Florin Corasd4295e62019-02-22 13:11:38 -0800628};
629/* *INDENT-ON* */
630
Florin Coras653e43f2019-03-04 10:56:23 -0800631int
632ct_session_tx (session_t * s)
633{
634 ct_connection_t *ct, *peer_ct;
635 session_t *peer_s;
636
637 ct = (ct_connection_t *) session_get_transport (s);
Florin Coras2d0e3de2020-10-23 16:31:40 -0700638 peer_ct = ct_connection_get (ct->peer_index, ct->c_thread_index);
Florin Coras653e43f2019-03-04 10:56:23 -0800639 if (!peer_ct)
Florin Corasbcdc50d2020-08-25 19:07:02 -0700640 return 0;
Florin Coras2d0e3de2020-10-23 16:31:40 -0700641 peer_s = session_get (peer_ct->c_s_index, peer_ct->c_thread_index);
Florin Coras653e43f2019-03-04 10:56:23 -0800642 if (peer_s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
643 return 0;
Florin Corasd6894562020-10-28 00:37:15 -0700644 return session_enqueue_notify (peer_s);
Florin Coras653e43f2019-03-04 10:56:23 -0800645}
646
Florin Corasd4295e62019-02-22 13:11:38 -0800647static clib_error_t *
648ct_transport_init (vlib_main_t * vm)
649{
650 transport_register_protocol (TRANSPORT_PROTO_NONE, &cut_thru_proto,
651 FIB_PROTOCOL_IP4, ~0);
Florin Coras653e43f2019-03-04 10:56:23 -0800652 transport_register_protocol (TRANSPORT_PROTO_NONE, &cut_thru_proto,
653 FIB_PROTOCOL_IP6, ~0);
Florin Corasd4295e62019-02-22 13:11:38 -0800654 return 0;
655}
656
657VLIB_INIT_FUNCTION (ct_transport_init);
658
Florin Corasba7d8f52019-02-22 13:11:38 -0800659/*
660 * fd.io coding-style-patch-verification: ON
661 *
662 * Local Variables:
663 * eval: (c-set-style "gnu")
664 * End:
665 */