blob: beca5dbe689cab91ae3a25ce14c62c998ff55578 [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 Coras653e43f2019-03-04 10:56:23 -080019static ct_connection_t *connections;
Florin Corasd4295e62019-02-22 13:11:38 -080020
Florin Coras653e43f2019-03-04 10:56:23 -080021static void
22ct_enable_disable_main_pre_input_node (u8 is_add)
23{
24 u32 n_conns;
25
Florin Coras6575a9a2019-04-02 17:06:05 -070026 if (!vlib_num_workers ())
27 return;
28
Florin Coras653e43f2019-03-04 10:56:23 -080029 n_conns = pool_elts (connections);
30 if (n_conns > 2)
31 return;
32
33 if (n_conns > 0 && is_add)
34 vlib_node_set_state (vlib_get_main (),
35 session_queue_pre_input_node.index,
36 VLIB_NODE_STATE_POLLING);
37 else if (n_conns == 0)
38 vlib_node_set_state (vlib_get_main (),
39 session_queue_pre_input_node.index,
40 VLIB_NODE_STATE_DISABLED);
41}
42
43static ct_connection_t *
Florin Corasd4295e62019-02-22 13:11:38 -080044ct_connection_alloc (void)
45{
46 ct_connection_t *ct;
47
48 pool_get_zero (connections, ct);
49 ct->c_c_index = ct - connections;
50 ct->c_thread_index = 0;
51 ct->client_wrk = ~0;
52 ct->server_wrk = ~0;
53 return ct;
54}
55
Florin Coras653e43f2019-03-04 10:56:23 -080056static ct_connection_t *
Florin Corasd4295e62019-02-22 13:11:38 -080057ct_connection_get (u32 ct_index)
58{
59 if (pool_is_free_index (connections, ct_index))
60 return 0;
61 return pool_elt_at_index (connections, ct_index);
Florin Corasba7d8f52019-02-22 13:11:38 -080062}
63
Florin Coras653e43f2019-03-04 10:56:23 -080064static void
Florin Corasd4295e62019-02-22 13:11:38 -080065ct_connection_free (ct_connection_t * ct)
Florin Corasba7d8f52019-02-22 13:11:38 -080066{
Florin Corasba7d8f52019-02-22 13:11:38 -080067 if (CLIB_DEBUG)
Florin Corasd4295e62019-02-22 13:11:38 -080068 memset (ct, 0xfc, sizeof (*ct));
69 pool_put (connections, ct);
Florin Corasba7d8f52019-02-22 13:11:38 -080070}
71
Florin Coras2b81e3c2019-02-27 07:55:46 -080072session_t *
73ct_session_get_peer (session_t * s)
74{
75 ct_connection_t *ct, *peer_ct;
76 ct = ct_connection_get (s->connection_index);
77 peer_ct = ct_connection_get (ct->peer_index);
78 return session_get (peer_ct->c_s_index, 0);
79}
80
Florin Corasba7d8f52019-02-22 13:11:38 -080081void
Florin Coras2b81e3c2019-02-27 07:55:46 -080082ct_session_endpoint (session_t * ll, session_endpoint_t * sep)
Florin Corasba7d8f52019-02-22 13:11:38 -080083{
Florin Corasd4295e62019-02-22 13:11:38 -080084 ct_connection_t *ct;
85 ct = (ct_connection_t *) session_get_transport (ll);
86 sep->transport_proto = ct->actual_tp;
87 sep->port = ct->c_lcl_port;
88 sep->is_ip4 = ct->c_is_ip4;
Florin Corasea7e7082020-07-07 17:57:28 -070089 ip_copy (&sep->ip, &ct->c_lcl_ip, ct->c_is_ip4);
Florin Corasba7d8f52019-02-22 13:11:38 -080090}
91
Florin Corasba7d8f52019-02-22 13:11:38 -080092int
Florin Coras2b81e3c2019-02-27 07:55:46 -080093ct_session_connect_notify (session_t * ss)
Florin Corasba7d8f52019-02-22 13:11:38 -080094{
Florin Coras2b81e3c2019-02-27 07:55:46 -080095 ct_connection_t *sct, *cct;
96 app_worker_t *client_wrk;
Florin Corasba7d8f52019-02-22 13:11:38 -080097 segment_manager_t *sm;
Florin Corasdd7d24b2020-08-14 17:51:13 -070098 u32 ss_index, opaque;
Florin Coras88001c62019-04-24 14:44:46 -070099 fifo_segment_t *seg;
Florin Corasba7d8f52019-02-22 13:11:38 -0800100 u64 segment_handle;
Florin Coras00e01d32019-10-21 16:07:46 -0700101 int err = 0;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800102 session_t *cs;
Florin Corasba7d8f52019-02-22 13:11:38 -0800103
Florin Coras2b81e3c2019-02-27 07:55:46 -0800104 ss_index = ss->session_index;
105 sct = (ct_connection_t *) session_get_transport (ss);
106 client_wrk = app_worker_get (sct->client_wrk);
Florin Corasdd7d24b2020-08-14 17:51:13 -0700107 opaque = sct->client_opaque;
Florin Corasba7d8f52019-02-22 13:11:38 -0800108
Florin Coras2b81e3c2019-02-27 07:55:46 -0800109 sm = segment_manager_get (ss->rx_fifo->segment_manager);
110 seg = segment_manager_get_segment_w_lock (sm, ss->rx_fifo->segment_index);
Florin Corasba7d8f52019-02-22 13:11:38 -0800111 segment_handle = segment_manager_segment_handle (sm, seg);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800112
Florin Coras00e01d32019-10-21 16:07:46 -0700113 if ((err = app_worker_add_segment_notify (client_wrk, segment_handle)))
Florin Corasba7d8f52019-02-22 13:11:38 -0800114 {
115 clib_warning ("failed to notify client %u of new segment",
Florin Coras2b81e3c2019-02-27 07:55:46 -0800116 sct->client_wrk);
Florin Corasba7d8f52019-02-22 13:11:38 -0800117 segment_manager_segment_reader_unlock (sm);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800118 session_close (ss);
Florin Corasdd7d24b2020-08-14 17:51:13 -0700119 goto error;
Florin Corasba7d8f52019-02-22 13:11:38 -0800120 }
121 else
122 {
123 segment_manager_segment_reader_unlock (sm);
124 }
125
Florin Coras2b81e3c2019-02-27 07:55:46 -0800126 /* Alloc client session */
127 cct = ct_connection_get (sct->peer_index);
Florin Corasba7d8f52019-02-22 13:11:38 -0800128
Florin Coras2b81e3c2019-02-27 07:55:46 -0800129 cs = session_alloc (0);
130 ss = session_get (ss_index, 0);
131 cs->session_type = ss->session_type;
132 cs->connection_index = sct->c_c_index;
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +0200133 cs->listener_handle = SESSION_INVALID_HANDLE;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800134 cs->session_state = SESSION_STATE_CONNECTING;
135 cs->app_wrk_index = client_wrk->wrk_index;
136 cs->connection_index = cct->c_c_index;
137
138 cct->c_s_index = cs->session_index;
Florin Coras653e43f2019-03-04 10:56:23 -0800139 cct->client_rx_fifo = ss->tx_fifo;
140 cct->client_tx_fifo = ss->rx_fifo;
141
142 cct->client_rx_fifo->refcnt++;
143 cct->client_tx_fifo->refcnt++;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800144
145 /* This will allocate fifos for the session. They won't be used for
146 * exchanging data but they will be used to close the connection if
147 * the segment manager/worker is freed */
Florin Corasdd7d24b2020-08-14 17:51:13 -0700148 if ((err = app_worker_init_connected (client_wrk, cs)))
Florin Coras2b81e3c2019-02-27 07:55:46 -0800149 {
150 session_close (ss);
Florin Corasdd7d24b2020-08-14 17:51:13 -0700151 session_free (cs);
152 goto error;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800153 }
154
Florin Corasdd7d24b2020-08-14 17:51:13 -0700155 cs->session_state = SESSION_STATE_CONNECTING;
156
157 if (app_worker_connect_notify (client_wrk, cs, err, opaque))
Florin Coras2b81e3c2019-02-27 07:55:46 -0800158 {
159 session_close (ss);
Florin Corasdd7d24b2020-08-14 17:51:13 -0700160 segment_manager_dealloc_fifos (cs->rx_fifo, cs->tx_fifo);
161 session_free (cs);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800162 return -1;
163 }
164
165 cs = session_get (cct->c_s_index, 0);
166 cs->session_state = SESSION_STATE_READY;
167
Florin Corasba7d8f52019-02-22 13:11:38 -0800168 return 0;
Florin Corasdd7d24b2020-08-14 17:51:13 -0700169
170error:
171 app_worker_connect_notify (client_wrk, 0, err, opaque);
172 return -1;
Florin Corasba7d8f52019-02-22 13:11:38 -0800173}
174
Florin Coras653e43f2019-03-04 10:56:23 -0800175static int
Florin Coras2b81e3c2019-02-27 07:55:46 -0800176ct_init_local_session (app_worker_t * client_wrk, app_worker_t * server_wrk,
177 ct_connection_t * ct, session_t * ls, session_t * ll)
Florin Corasba7d8f52019-02-22 13:11:38 -0800178{
Florin Coras653e43f2019-03-04 10:56:23 -0800179 u32 round_rx_fifo_sz, round_tx_fifo_sz, sm_index, seg_size;
Florin Coras88001c62019-04-24 14:44:46 -0700180 segment_manager_props_t *props;
Florin Coras653e43f2019-03-04 10:56:23 -0800181 application_t *server;
Florin Corasba7d8f52019-02-22 13:11:38 -0800182 segment_manager_t *sm;
Florin Coras653e43f2019-03-04 10:56:23 -0800183 u32 margin = 16 << 10;
Florin Coras88001c62019-04-24 14:44:46 -0700184 fifo_segment_t *seg;
Florin Corasba7d8f52019-02-22 13:11:38 -0800185 u64 segment_handle;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800186 int seg_index, rv;
Florin Corasba7d8f52019-02-22 13:11:38 -0800187
Florin Corasba7d8f52019-02-22 13:11:38 -0800188 server = application_get (server_wrk->app_index);
Florin Corasba7d8f52019-02-22 13:11:38 -0800189
190 props = application_segment_manager_properties (server);
Florin Corasba7d8f52019-02-22 13:11:38 -0800191 round_rx_fifo_sz = 1 << max_log2 (props->rx_fifo_size);
192 round_tx_fifo_sz = 1 << max_log2 (props->tx_fifo_size);
Florin Corasf22f4e52019-12-19 16:10:58 -0800193 /* Increase size because of inefficient chunk allocations. Depending on
194 * how data is consumed, it may happen that more chunks than needed are
195 * allocated.
196 * TODO should remove once allocations are done more efficiently */
197 seg_size = 4 * (round_rx_fifo_sz + round_tx_fifo_sz + margin);
Florin Corasba7d8f52019-02-22 13:11:38 -0800198
Florin Coras2b81e3c2019-02-27 07:55:46 -0800199 sm = app_worker_get_listen_segment_manager (server_wrk, ll);
Florin Corasba7d8f52019-02-22 13:11:38 -0800200 seg_index = segment_manager_add_segment (sm, seg_size);
201 if (seg_index < 0)
202 {
203 clib_warning ("failed to add new cut-through segment");
204 return seg_index;
205 }
206 seg = segment_manager_get_segment_w_lock (sm, seg_index);
Florin Corasba7d8f52019-02-22 13:11:38 -0800207
Florin Coras62ddc032019-12-08 18:30:42 -0800208 rv = segment_manager_try_alloc_fifos (seg, ls->thread_index,
209 props->rx_fifo_size,
Florin Coras2b81e3c2019-02-27 07:55:46 -0800210 props->tx_fifo_size, &ls->rx_fifo,
211 &ls->tx_fifo);
Florin Corasba7d8f52019-02-22 13:11:38 -0800212 if (rv)
213 {
214 clib_warning ("failed to add fifos in cut-through segment");
215 segment_manager_segment_reader_unlock (sm);
216 goto failed;
217 }
Florin Coras2b81e3c2019-02-27 07:55:46 -0800218
Florin Corasba7d8f52019-02-22 13:11:38 -0800219 sm_index = segment_manager_index (sm);
Florin Coras653e43f2019-03-04 10:56:23 -0800220 ls->rx_fifo->master_session_index = ls->session_index;
221 ls->tx_fifo->master_session_index = ls->session_index;
Florin Corasba7d8f52019-02-22 13:11:38 -0800222 ls->rx_fifo->segment_manager = sm_index;
223 ls->tx_fifo->segment_manager = sm_index;
224 ls->rx_fifo->segment_index = seg_index;
225 ls->tx_fifo->segment_index = seg_index;
Florin Corasba7d8f52019-02-22 13:11:38 -0800226
Florin Coras97d39e32020-09-04 08:57:27 -0700227 /* Disable ooo lookups on the cut-through fifos. TODO remove once init of
228 * chunk lookup rbtrees is delegated to transports */
229 svm_fifo_free_chunk_lookup (ls->tx_fifo);
230
Florin Corasba7d8f52019-02-22 13:11:38 -0800231 segment_handle = segment_manager_segment_handle (sm, seg);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800232 if ((rv = app_worker_add_segment_notify (server_wrk, segment_handle)))
Florin Corasba7d8f52019-02-22 13:11:38 -0800233 {
234 clib_warning ("failed to notify server of new segment");
235 segment_manager_segment_reader_unlock (sm);
236 goto failed;
237 }
238 segment_manager_segment_reader_unlock (sm);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800239 ct->segment_handle = segment_handle;
Florin Corasba7d8f52019-02-22 13:11:38 -0800240
241 return 0;
242
243failed:
Florin Coras2b81e3c2019-02-27 07:55:46 -0800244 segment_manager_del_segment (sm, seg);
Florin Corasba7d8f52019-02-22 13:11:38 -0800245 return rv;
246}
247
Florin Coras653e43f2019-03-04 10:56:23 -0800248static int
Florin Coras2b81e3c2019-02-27 07:55:46 -0800249ct_connect (app_worker_t * client_wrk, session_t * ll,
250 session_endpoint_cfg_t * sep)
Florin Corasba7d8f52019-02-22 13:11:38 -0800251{
Florin Coras2b81e3c2019-02-27 07:55:46 -0800252 u32 cct_index, ll_index, ll_ct_index;
253 ct_connection_t *sct, *cct, *ll_ct;
254 app_worker_t *server_wrk;
255 session_t *ss;
Florin Corasba7d8f52019-02-22 13:11:38 -0800256
Florin Coras2b81e3c2019-02-27 07:55:46 -0800257 ll_index = ll->session_index;
258 ll_ct_index = ll->connection_index;
Florin Corasba7d8f52019-02-22 13:11:38 -0800259
Florin Coras2b81e3c2019-02-27 07:55:46 -0800260 cct = ct_connection_alloc ();
261 cct_index = cct->c_c_index;
262 sct = ct_connection_alloc ();
263 ll_ct = ct_connection_get (ll_ct_index);
Florin Corasba7d8f52019-02-22 13:11:38 -0800264
Florin Coras2b81e3c2019-02-27 07:55:46 -0800265 /*
266 * Alloc and init client transport
267 */
268 cct = ct_connection_get (cct_index);
269 cct->c_thread_index = 0;
270 cct->c_rmt_port = sep->port;
271 cct->c_lcl_port = 0;
272 cct->c_is_ip4 = sep->is_ip4;
273 clib_memcpy (&cct->c_rmt_ip, &sep->ip, sizeof (sep->ip));
274 cct->actual_tp = ll_ct->actual_tp;
Florin Coras653e43f2019-03-04 10:56:23 -0800275 cct->is_client = 1;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800276
277 /*
278 * Init server transport
279 */
280 sct->c_thread_index = 0;
281 sct->c_rmt_port = 0;
282 sct->c_lcl_port = ll_ct->c_lcl_port;
283 sct->c_is_ip4 = sep->is_ip4;
284 clib_memcpy (&sct->c_lcl_ip, &ll_ct->c_lcl_ip, sizeof (ll_ct->c_lcl_ip));
285 sct->client_wrk = client_wrk->wrk_index;
286 sct->c_proto = TRANSPORT_PROTO_NONE;
287 sct->client_opaque = sep->opaque;
288 sct->actual_tp = ll_ct->actual_tp;
289
290 sct->peer_index = cct->c_c_index;
291 cct->peer_index = sct->c_c_index;
292
293 /*
294 * Accept server session. Client session is created only after
295 * server confirms accept.
296 */
297 ss = session_alloc (0);
298 ll = listen_session_get (ll_index);
Florin Coras9f1a5432019-03-10 21:03:20 -0700299 ss->session_type = session_type_from_proto_and_ip (TRANSPORT_PROTO_NONE,
300 sct->c_is_ip4);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800301 ss->connection_index = sct->c_c_index;
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +0200302 ss->listener_handle = listen_session_get_handle (ll);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800303 ss->session_state = SESSION_STATE_CREATED;
304
305 server_wrk = application_listener_select_worker (ll);
306 ss->app_wrk_index = server_wrk->wrk_index;
307
308 sct->c_s_index = ss->session_index;
309 sct->server_wrk = ss->app_wrk_index;
310
311 if (ct_init_local_session (client_wrk, server_wrk, sct, ss, ll))
Florin Corasba7d8f52019-02-22 13:11:38 -0800312 {
Florin Coras2b81e3c2019-02-27 07:55:46 -0800313 ct_connection_free (sct);
314 session_free (ss);
315 return -1;
Florin Corasba7d8f52019-02-22 13:11:38 -0800316 }
317
Florin Coras2b81e3c2019-02-27 07:55:46 -0800318 ss->session_state = SESSION_STATE_ACCEPTING;
319 if (app_worker_accept_notify (server_wrk, ss))
320 {
Florin Coras2b81e3c2019-02-27 07:55:46 -0800321 ct_connection_free (sct);
Florin Coras12813d52020-04-09 20:59:20 +0000322 segment_manager_dealloc_fifos (ss->rx_fifo, ss->tx_fifo);
323 session_free (ss);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800324 return -1;
325 }
326
Florin Coras2b81e3c2019-02-27 07:55:46 -0800327 cct->segment_handle = sct->segment_handle;
Florin Coras653e43f2019-03-04 10:56:23 -0800328 ct_enable_disable_main_pre_input_node (1 /* is_add */ );
Florin Corasba7d8f52019-02-22 13:11:38 -0800329 return 0;
330}
331
Florin Coras653e43f2019-03-04 10:56:23 -0800332static u32
Florin Corasd4295e62019-02-22 13:11:38 -0800333ct_start_listen (u32 app_listener_index, transport_endpoint_t * tep)
334{
335 session_endpoint_cfg_t *sep;
336 ct_connection_t *ct;
337
338 sep = (session_endpoint_cfg_t *) tep;
339 ct = ct_connection_alloc ();
340 ct->server_wrk = sep->app_wrk_index;
341 ct->c_is_ip4 = sep->is_ip4;
342 clib_memcpy (&ct->c_lcl_ip, &sep->ip, sizeof (sep->ip));
343 ct->c_lcl_port = sep->port;
344 ct->actual_tp = sep->transport_proto;
Florin Coras653e43f2019-03-04 10:56:23 -0800345 ct_enable_disable_main_pre_input_node (1 /* is_add */ );
Florin Corasd4295e62019-02-22 13:11:38 -0800346 return ct->c_c_index;
347}
348
Florin Coras653e43f2019-03-04 10:56:23 -0800349static u32
Florin Corasd4295e62019-02-22 13:11:38 -0800350ct_stop_listen (u32 ct_index)
351{
352 ct_connection_t *ct;
353 ct = ct_connection_get (ct_index);
354 ct_connection_free (ct);
Florin Coras653e43f2019-03-04 10:56:23 -0800355 ct_enable_disable_main_pre_input_node (0 /* is_add */ );
Florin Corasd4295e62019-02-22 13:11:38 -0800356 return 0;
357}
358
Florin Coras653e43f2019-03-04 10:56:23 -0800359static transport_connection_t *
Florin Corasd4295e62019-02-22 13:11:38 -0800360ct_listener_get (u32 ct_index)
361{
362 return (transport_connection_t *) ct_connection_get (ct_index);
363}
364
Florin Coras653e43f2019-03-04 10:56:23 -0800365static int
Florin Coras2b81e3c2019-02-27 07:55:46 -0800366ct_session_connect (transport_endpoint_cfg_t * tep)
367{
368 session_endpoint_cfg_t *sep_ext;
369 session_endpoint_t *sep;
370 app_worker_t *app_wrk;
371 session_handle_t lh;
372 application_t *app;
373 app_listener_t *al;
374 u32 table_index;
375 session_t *ll;
376 u8 fib_proto;
377
378 sep_ext = (session_endpoint_cfg_t *) tep;
379 sep = (session_endpoint_t *) tep;
380 app_wrk = app_worker_get (sep_ext->app_wrk_index);
381 app = application_get (app_wrk->app_index);
382
383 sep->transport_proto = sep_ext->original_tp;
384 table_index = application_local_session_table (app);
385 lh = session_lookup_local_endpoint (table_index, sep);
386 if (lh == SESSION_DROP_HANDLE)
Florin Coras00e01d32019-10-21 16:07:46 -0700387 return SESSION_E_FILTERED;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800388
389 if (lh == SESSION_INVALID_HANDLE)
390 goto global_scope;
391
392 ll = listen_session_get_from_handle (lh);
393 al = app_listener_get_w_session (ll);
394
395 /*
396 * Break loop if rule in local table points to connecting app. This
397 * can happen if client is a generic proxy. Route connect through
398 * global table instead.
399 */
400 if (al->app_index == app->app_index)
401 goto global_scope;
402
403 return ct_connect (app_wrk, ll, sep_ext);
404
405 /*
406 * If nothing found, check the global scope for locally attached
407 * destinations. Make sure first that we're allowed to.
408 */
409
410global_scope:
411 if (session_endpoint_is_local (sep))
Florin Coras00e01d32019-10-21 16:07:46 -0700412 return SESSION_E_NOROUTE;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800413
414 if (!application_has_global_scope (app))
Florin Coras00e01d32019-10-21 16:07:46 -0700415 return SESSION_E_SCOPE;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800416
417 fib_proto = session_endpoint_fib_proto (sep);
Florin Coras95cd8642019-12-30 21:53:19 -0800418 table_index = session_lookup_get_index_for_fib (fib_proto, sep->fib_index);
Florin Coras9f1a5432019-03-10 21:03:20 -0700419 ll = session_lookup_listener_wildcard (table_index, sep);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800420
421 if (ll)
422 return ct_connect (app_wrk, ll, sep_ext);
423
424 /* Failed to connect but no error */
425 return 1;
426}
427
Florin Coras653e43f2019-03-04 10:56:23 -0800428static void
Florin Coras2b81e3c2019-02-27 07:55:46 -0800429ct_session_close (u32 ct_index, u32 thread_index)
430{
431 ct_connection_t *ct, *peer_ct;
Florin Coras653e43f2019-03-04 10:56:23 -0800432 app_worker_t *app_wrk;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800433 session_t *s;
434
435 ct = ct_connection_get (ct_index);
436 peer_ct = ct_connection_get (ct->peer_index);
437 if (peer_ct)
438 {
439 peer_ct->peer_index = ~0;
440 session_transport_closing_notify (&peer_ct->connection);
441 }
442
443 s = session_get (ct->c_s_index, 0);
Florin Coras653e43f2019-03-04 10:56:23 -0800444 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
445 if (app_wrk)
446 app_worker_del_segment_notify (app_wrk, ct->segment_handle);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800447 session_free_w_fifos (s);
Florin Coras653e43f2019-03-04 10:56:23 -0800448 if (ct->is_client)
449 segment_manager_dealloc_fifos (ct->client_rx_fifo, ct->client_tx_fifo);
450
Florin Coras2b81e3c2019-02-27 07:55:46 -0800451 ct_connection_free (ct);
Florin Coras653e43f2019-03-04 10:56:23 -0800452 ct_enable_disable_main_pre_input_node (0 /* is_add */ );
Florin Coras2b81e3c2019-02-27 07:55:46 -0800453}
454
Florin Coras653e43f2019-03-04 10:56:23 -0800455static transport_connection_t *
Florin Coras2b81e3c2019-02-27 07:55:46 -0800456ct_session_get (u32 ct_index, u32 thread_index)
457{
458 return (transport_connection_t *) ct_connection_get (ct_index);
459}
460
Florin Corasd4295e62019-02-22 13:11:38 -0800461static u8 *
462format_ct_connection_id (u8 * s, va_list * args)
463{
464 ct_connection_t *ct = va_arg (*args, ct_connection_t *);
465 if (!ct)
466 return s;
467 if (ct->c_is_ip4)
468 {
469 s = format (s, "[%d:%d][CT:%U] %U:%d->%U:%d", ct->c_thread_index,
470 ct->c_s_index, format_transport_proto_short, ct->actual_tp,
471 format_ip4_address, &ct->c_lcl_ip4,
472 clib_net_to_host_u16 (ct->c_lcl_port), format_ip4_address,
473 &ct->c_rmt_ip4, clib_net_to_host_u16 (ct->c_rmt_port));
474 }
475 else
476 {
477 s = format (s, "[%d:%d][CT:%U] %U:%d->%U:%d", ct->c_thread_index,
478 ct->c_s_index, format_transport_proto_short, ct->actual_tp,
479 format_ip6_address, &ct->c_lcl_ip6,
480 clib_net_to_host_u16 (ct->c_lcl_port), format_ip6_address,
481 &ct->c_rmt_ip6, clib_net_to_host_u16 (ct->c_rmt_port));
482 }
483
484 return s;
485}
486
Florin Corasf940f8a2019-03-06 10:44:38 -0800487static int
Florin Coras9f86d222020-03-23 15:34:22 +0000488ct_custom_tx (void *session, transport_send_params_t * sp)
Florin Corasf940f8a2019-03-06 10:44:38 -0800489{
490 session_t *s = (session_t *) session;
491 if (session_has_transport (s))
492 return 0;
Florin Corasdd7d24b2020-08-14 17:51:13 -0700493 /* As all other sessions, cut-throughs are scheduled by vpp for tx so let
494 * the scheduler's custom tx logic decide when to deschedule, i.e., after
495 * fifo is emptied. */
Florin Corasf940f8a2019-03-06 10:44:38 -0800496 return ct_session_tx (s);
497}
498
Florin Coras317b8e02019-04-17 09:57:46 -0700499static int
500ct_app_rx_evt (transport_connection_t * tc)
501{
502 ct_connection_t *ct = (ct_connection_t *) tc, *peer_ct;
503 session_t *ps;
504
505 peer_ct = ct_connection_get (ct->peer_index);
506 if (!peer_ct)
507 return -1;
508 ps = session_get (peer_ct->c_s_index, peer_ct->c_thread_index);
509 return session_dequeue_notify (ps);
510}
511
Florin Coras653e43f2019-03-04 10:56:23 -0800512static u8 *
Florin Corasd4295e62019-02-22 13:11:38 -0800513format_ct_listener (u8 * s, va_list * args)
514{
515 u32 tc_index = va_arg (*args, u32);
Aloys Augustina0abbff2019-07-12 12:16:16 +0200516 u32 __clib_unused thread_index = va_arg (*args, u32);
Florin Corasd4295e62019-02-22 13:11:38 -0800517 u32 __clib_unused verbose = va_arg (*args, u32);
518 ct_connection_t *ct = ct_connection_get (tc_index);
Florin Coras7bf6ed62020-09-23 12:02:08 -0700519 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_ct_connection_id, ct);
Florin Corasd4295e62019-02-22 13:11:38 -0800520 if (verbose)
Florin Coras7bf6ed62020-09-23 12:02:08 -0700521 s = format (s, "%-" SESSION_CLI_STATE_LEN "s", "LISTEN");
Florin Corasd4295e62019-02-22 13:11:38 -0800522 return s;
523}
524
Florin Coras653e43f2019-03-04 10:56:23 -0800525static u8 *
Florin Coras2b81e3c2019-02-27 07:55:46 -0800526format_ct_connection (u8 * s, va_list * args)
527{
528 ct_connection_t *ct = va_arg (*args, ct_connection_t *);
529 u32 verbose = va_arg (*args, u32);
530
531 if (!ct)
532 return s;
Florin Coras7bf6ed62020-09-23 12:02:08 -0700533 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_ct_connection_id, ct);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800534 if (verbose)
535 {
Florin Coras7bf6ed62020-09-23 12:02:08 -0700536 s = format (s, "%-" SESSION_CLI_STATE_LEN "s", "ESTABLISHED");
Florin Coras2b81e3c2019-02-27 07:55:46 -0800537 if (verbose > 1)
538 {
539 s = format (s, "\n");
540 }
541 }
542 return s;
543}
544
Florin Coras653e43f2019-03-04 10:56:23 -0800545static u8 *
Florin Coras2b81e3c2019-02-27 07:55:46 -0800546format_ct_session (u8 * s, va_list * args)
547{
548 u32 ct_index = va_arg (*args, u32);
549 u32 __clib_unused thread_index = va_arg (*args, u32);
550 u32 verbose = va_arg (*args, u32);
551 ct_connection_t *ct;
552
553 ct = ct_connection_get (ct_index);
554 if (!ct)
555 {
556 s = format (s, "empty\n");
557 return s;
558 }
559
560 s = format (s, "%U", format_ct_connection, ct, verbose);
561 return s;
562}
563
Florin Corasd4295e62019-02-22 13:11:38 -0800564/* *INDENT-OFF* */
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200565static const transport_proto_vft_t cut_thru_proto = {
Florin Corasd4295e62019-02-22 13:11:38 -0800566 .start_listen = ct_start_listen,
567 .stop_listen = ct_stop_listen,
568 .get_listener = ct_listener_get,
Florin Coras2b81e3c2019-02-27 07:55:46 -0800569 .connect = ct_session_connect,
570 .close = ct_session_close,
571 .get_connection = ct_session_get,
Florin Corasf940f8a2019-03-06 10:44:38 -0800572 .custom_tx = ct_custom_tx,
Florin Coras317b8e02019-04-17 09:57:46 -0700573 .app_rx_evt = ct_app_rx_evt,
Florin Corasd4295e62019-02-22 13:11:38 -0800574 .format_listener = format_ct_listener,
Florin Coras2b81e3c2019-02-27 07:55:46 -0800575 .format_connection = format_ct_session,
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200576 .transport_options = {
Florin Coras07063b82020-03-13 04:44:51 +0000577 .name = "ct",
578 .short_name = "C",
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200579 .tx_type = TRANSPORT_TX_INTERNAL,
580 .service_type = TRANSPORT_SERVICE_APP,
581 },
Florin Corasd4295e62019-02-22 13:11:38 -0800582};
583/* *INDENT-ON* */
584
Florin Coras653e43f2019-03-04 10:56:23 -0800585int
586ct_session_tx (session_t * s)
587{
588 ct_connection_t *ct, *peer_ct;
589 session_t *peer_s;
590
591 ct = (ct_connection_t *) session_get_transport (s);
592 peer_ct = ct_connection_get (ct->peer_index);
593 if (!peer_ct)
Florin Corasbcdc50d2020-08-25 19:07:02 -0700594 return 0;
Florin Coras653e43f2019-03-04 10:56:23 -0800595 peer_s = session_get (peer_ct->c_s_index, 0);
596 if (peer_s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
597 return 0;
Florin Corasbcdc50d2020-08-25 19:07:02 -0700598 session_enqueue_notify (peer_s);
599 /* The scheduler uses packet count as a means of upper bounding the amount
600 * of work done per dispatch. So make it look like we have sent something */
601 return 1;
Florin Coras653e43f2019-03-04 10:56:23 -0800602}
603
Florin Corasd4295e62019-02-22 13:11:38 -0800604static clib_error_t *
605ct_transport_init (vlib_main_t * vm)
606{
607 transport_register_protocol (TRANSPORT_PROTO_NONE, &cut_thru_proto,
608 FIB_PROTOCOL_IP4, ~0);
Florin Coras653e43f2019-03-04 10:56:23 -0800609 transport_register_protocol (TRANSPORT_PROTO_NONE, &cut_thru_proto,
610 FIB_PROTOCOL_IP6, ~0);
Florin Corasd4295e62019-02-22 13:11:38 -0800611 return 0;
612}
613
614VLIB_INIT_FUNCTION (ct_transport_init);
615
Florin Corasba7d8f52019-02-22 13:11:38 -0800616/*
617 * fd.io coding-style-patch-verification: ON
618 *
619 * Local Variables:
620 * eval: (c-set-style "gnu")
621 * End:
622 */