blob: 927bf91a040468ba68360ba34fd50f0029b87424 [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 Corasba7d8f52019-02-22 13:11:38 -080089}
90
Florin Corasba7d8f52019-02-22 13:11:38 -080091int
Florin Coras2b81e3c2019-02-27 07:55:46 -080092ct_session_connect_notify (session_t * ss)
Florin Corasba7d8f52019-02-22 13:11:38 -080093{
Florin Coras2b81e3c2019-02-27 07:55:46 -080094 ct_connection_t *sct, *cct;
95 app_worker_t *client_wrk;
Florin Corasba7d8f52019-02-22 13:11:38 -080096 segment_manager_t *sm;
Florin Coras88001c62019-04-24 14:44:46 -070097 fifo_segment_t *seg;
Florin Corasba7d8f52019-02-22 13:11:38 -080098 u64 segment_handle;
Florin Coras00e01d32019-10-21 16:07:46 -070099 int err = 0;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800100 session_t *cs;
101 u32 ss_index;
Florin Corasba7d8f52019-02-22 13:11:38 -0800102
Florin Coras2b81e3c2019-02-27 07:55:46 -0800103 ss_index = ss->session_index;
104 sct = (ct_connection_t *) session_get_transport (ss);
105 client_wrk = app_worker_get (sct->client_wrk);
Florin Corasba7d8f52019-02-22 13:11:38 -0800106
Florin Coras2b81e3c2019-02-27 07:55:46 -0800107 sm = segment_manager_get (ss->rx_fifo->segment_manager);
108 seg = segment_manager_get_segment_w_lock (sm, ss->rx_fifo->segment_index);
Florin Corasba7d8f52019-02-22 13:11:38 -0800109 segment_handle = segment_manager_segment_handle (sm, seg);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800110
Florin Coras00e01d32019-10-21 16:07:46 -0700111 if ((err = app_worker_add_segment_notify (client_wrk, segment_handle)))
Florin Corasba7d8f52019-02-22 13:11:38 -0800112 {
113 clib_warning ("failed to notify client %u of new segment",
Florin Coras2b81e3c2019-02-27 07:55:46 -0800114 sct->client_wrk);
Florin Corasba7d8f52019-02-22 13:11:38 -0800115 segment_manager_segment_reader_unlock (sm);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800116 session_close (ss);
Florin Corasba7d8f52019-02-22 13:11:38 -0800117 }
118 else
119 {
120 segment_manager_segment_reader_unlock (sm);
121 }
122
Florin Coras2b81e3c2019-02-27 07:55:46 -0800123 /* Alloc client session */
124 cct = ct_connection_get (sct->peer_index);
Florin Corasba7d8f52019-02-22 13:11:38 -0800125
Florin Coras2b81e3c2019-02-27 07:55:46 -0800126 cs = session_alloc (0);
127 ss = session_get (ss_index, 0);
128 cs->session_type = ss->session_type;
129 cs->connection_index = sct->c_c_index;
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +0200130 cs->listener_handle = SESSION_INVALID_HANDLE;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800131 cs->session_state = SESSION_STATE_CONNECTING;
132 cs->app_wrk_index = client_wrk->wrk_index;
133 cs->connection_index = cct->c_c_index;
134
135 cct->c_s_index = cs->session_index;
Florin Coras653e43f2019-03-04 10:56:23 -0800136 cct->client_rx_fifo = ss->tx_fifo;
137 cct->client_tx_fifo = ss->rx_fifo;
138
139 cct->client_rx_fifo->refcnt++;
140 cct->client_tx_fifo->refcnt++;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800141
142 /* This will allocate fifos for the session. They won't be used for
143 * exchanging data but they will be used to close the connection if
144 * the segment manager/worker is freed */
145 if (app_worker_init_connected (client_wrk, cs))
146 {
147 session_close (ss);
148 return -1;
149 }
150
Florin Coras00e01d32019-10-21 16:07:46 -0700151 if (app_worker_connect_notify (client_wrk, cs, err, sct->client_opaque))
Florin Coras2b81e3c2019-02-27 07:55:46 -0800152 {
153 session_close (ss);
154 return -1;
155 }
156
157 cs = session_get (cct->c_s_index, 0);
158 cs->session_state = SESSION_STATE_READY;
159
Florin Corasba7d8f52019-02-22 13:11:38 -0800160 return 0;
161}
162
Florin Coras653e43f2019-03-04 10:56:23 -0800163static int
Florin Coras2b81e3c2019-02-27 07:55:46 -0800164ct_init_local_session (app_worker_t * client_wrk, app_worker_t * server_wrk,
165 ct_connection_t * ct, session_t * ls, session_t * ll)
Florin Corasba7d8f52019-02-22 13:11:38 -0800166{
Florin Coras653e43f2019-03-04 10:56:23 -0800167 u32 round_rx_fifo_sz, round_tx_fifo_sz, sm_index, seg_size;
Florin Coras88001c62019-04-24 14:44:46 -0700168 segment_manager_props_t *props;
Florin Coras653e43f2019-03-04 10:56:23 -0800169 application_t *server;
Florin Corasba7d8f52019-02-22 13:11:38 -0800170 segment_manager_t *sm;
Florin Coras653e43f2019-03-04 10:56:23 -0800171 u32 margin = 16 << 10;
Florin Coras88001c62019-04-24 14:44:46 -0700172 fifo_segment_t *seg;
Florin Corasba7d8f52019-02-22 13:11:38 -0800173 u64 segment_handle;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800174 int seg_index, rv;
Florin Corasba7d8f52019-02-22 13:11:38 -0800175
Florin Corasba7d8f52019-02-22 13:11:38 -0800176 server = application_get (server_wrk->app_index);
Florin Corasba7d8f52019-02-22 13:11:38 -0800177
178 props = application_segment_manager_properties (server);
Florin Corasba7d8f52019-02-22 13:11:38 -0800179 round_rx_fifo_sz = 1 << max_log2 (props->rx_fifo_size);
180 round_tx_fifo_sz = 1 << max_log2 (props->tx_fifo_size);
Florin Corasf22f4e52019-12-19 16:10:58 -0800181 /* Increase size because of inefficient chunk allocations. Depending on
182 * how data is consumed, it may happen that more chunks than needed are
183 * allocated.
184 * TODO should remove once allocations are done more efficiently */
185 seg_size = 4 * (round_rx_fifo_sz + round_tx_fifo_sz + margin);
Florin Corasba7d8f52019-02-22 13:11:38 -0800186
Florin Coras2b81e3c2019-02-27 07:55:46 -0800187 sm = app_worker_get_listen_segment_manager (server_wrk, ll);
Florin Corasba7d8f52019-02-22 13:11:38 -0800188 seg_index = segment_manager_add_segment (sm, seg_size);
189 if (seg_index < 0)
190 {
191 clib_warning ("failed to add new cut-through segment");
192 return seg_index;
193 }
194 seg = segment_manager_get_segment_w_lock (sm, seg_index);
Florin Corasba7d8f52019-02-22 13:11:38 -0800195
Florin Coras62ddc032019-12-08 18:30:42 -0800196 rv = segment_manager_try_alloc_fifos (seg, ls->thread_index,
197 props->rx_fifo_size,
Florin Coras2b81e3c2019-02-27 07:55:46 -0800198 props->tx_fifo_size, &ls->rx_fifo,
199 &ls->tx_fifo);
Florin Corasba7d8f52019-02-22 13:11:38 -0800200 if (rv)
201 {
202 clib_warning ("failed to add fifos in cut-through segment");
203 segment_manager_segment_reader_unlock (sm);
204 goto failed;
205 }
Florin Coras2b81e3c2019-02-27 07:55:46 -0800206
Florin Corasba7d8f52019-02-22 13:11:38 -0800207 sm_index = segment_manager_index (sm);
Florin Coras653e43f2019-03-04 10:56:23 -0800208 ls->rx_fifo->master_session_index = ls->session_index;
209 ls->tx_fifo->master_session_index = ls->session_index;
Florin Corasba7d8f52019-02-22 13:11:38 -0800210 ls->rx_fifo->segment_manager = sm_index;
211 ls->tx_fifo->segment_manager = sm_index;
212 ls->rx_fifo->segment_index = seg_index;
213 ls->tx_fifo->segment_index = seg_index;
Florin Corasba7d8f52019-02-22 13:11:38 -0800214
215 segment_handle = segment_manager_segment_handle (sm, seg);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800216 if ((rv = app_worker_add_segment_notify (server_wrk, segment_handle)))
Florin Corasba7d8f52019-02-22 13:11:38 -0800217 {
218 clib_warning ("failed to notify server of new segment");
219 segment_manager_segment_reader_unlock (sm);
220 goto failed;
221 }
222 segment_manager_segment_reader_unlock (sm);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800223 ct->segment_handle = segment_handle;
Florin Corasba7d8f52019-02-22 13:11:38 -0800224
225 return 0;
226
227failed:
Florin Coras2b81e3c2019-02-27 07:55:46 -0800228 segment_manager_del_segment (sm, seg);
Florin Corasba7d8f52019-02-22 13:11:38 -0800229 return rv;
230}
231
Florin Coras653e43f2019-03-04 10:56:23 -0800232static int
Florin Coras2b81e3c2019-02-27 07:55:46 -0800233ct_connect (app_worker_t * client_wrk, session_t * ll,
234 session_endpoint_cfg_t * sep)
Florin Corasba7d8f52019-02-22 13:11:38 -0800235{
Florin Coras2b81e3c2019-02-27 07:55:46 -0800236 u32 cct_index, ll_index, ll_ct_index;
237 ct_connection_t *sct, *cct, *ll_ct;
238 app_worker_t *server_wrk;
239 session_t *ss;
Florin Corasba7d8f52019-02-22 13:11:38 -0800240
Florin Coras2b81e3c2019-02-27 07:55:46 -0800241 ll_index = ll->session_index;
242 ll_ct_index = ll->connection_index;
Florin Corasba7d8f52019-02-22 13:11:38 -0800243
Florin Coras2b81e3c2019-02-27 07:55:46 -0800244 cct = ct_connection_alloc ();
245 cct_index = cct->c_c_index;
246 sct = ct_connection_alloc ();
247 ll_ct = ct_connection_get (ll_ct_index);
Florin Corasba7d8f52019-02-22 13:11:38 -0800248
Florin Coras2b81e3c2019-02-27 07:55:46 -0800249 /*
250 * Alloc and init client transport
251 */
252 cct = ct_connection_get (cct_index);
253 cct->c_thread_index = 0;
254 cct->c_rmt_port = sep->port;
255 cct->c_lcl_port = 0;
256 cct->c_is_ip4 = sep->is_ip4;
257 clib_memcpy (&cct->c_rmt_ip, &sep->ip, sizeof (sep->ip));
258 cct->actual_tp = ll_ct->actual_tp;
Florin Coras653e43f2019-03-04 10:56:23 -0800259 cct->is_client = 1;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800260
261 /*
262 * Init server transport
263 */
264 sct->c_thread_index = 0;
265 sct->c_rmt_port = 0;
266 sct->c_lcl_port = ll_ct->c_lcl_port;
267 sct->c_is_ip4 = sep->is_ip4;
268 clib_memcpy (&sct->c_lcl_ip, &ll_ct->c_lcl_ip, sizeof (ll_ct->c_lcl_ip));
269 sct->client_wrk = client_wrk->wrk_index;
270 sct->c_proto = TRANSPORT_PROTO_NONE;
271 sct->client_opaque = sep->opaque;
272 sct->actual_tp = ll_ct->actual_tp;
273
274 sct->peer_index = cct->c_c_index;
275 cct->peer_index = sct->c_c_index;
276
277 /*
278 * Accept server session. Client session is created only after
279 * server confirms accept.
280 */
281 ss = session_alloc (0);
282 ll = listen_session_get (ll_index);
Florin Coras9f1a5432019-03-10 21:03:20 -0700283 ss->session_type = session_type_from_proto_and_ip (TRANSPORT_PROTO_NONE,
284 sct->c_is_ip4);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800285 ss->connection_index = sct->c_c_index;
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +0200286 ss->listener_handle = listen_session_get_handle (ll);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800287 ss->session_state = SESSION_STATE_CREATED;
288
289 server_wrk = application_listener_select_worker (ll);
290 ss->app_wrk_index = server_wrk->wrk_index;
291
292 sct->c_s_index = ss->session_index;
293 sct->server_wrk = ss->app_wrk_index;
294
295 if (ct_init_local_session (client_wrk, server_wrk, sct, ss, ll))
Florin Corasba7d8f52019-02-22 13:11:38 -0800296 {
Florin Coras2b81e3c2019-02-27 07:55:46 -0800297 ct_connection_free (sct);
298 session_free (ss);
299 return -1;
Florin Corasba7d8f52019-02-22 13:11:38 -0800300 }
301
Florin Coras2b81e3c2019-02-27 07:55:46 -0800302 ss->session_state = SESSION_STATE_ACCEPTING;
303 if (app_worker_accept_notify (server_wrk, ss))
304 {
Florin Coras2b81e3c2019-02-27 07:55:46 -0800305 ct_connection_free (sct);
Florin Coras12813d52020-04-09 20:59:20 +0000306 segment_manager_dealloc_fifos (ss->rx_fifo, ss->tx_fifo);
307 session_free (ss);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800308 return -1;
309 }
310
Florin Coras2b81e3c2019-02-27 07:55:46 -0800311 cct->segment_handle = sct->segment_handle;
Florin Coras653e43f2019-03-04 10:56:23 -0800312 ct_enable_disable_main_pre_input_node (1 /* is_add */ );
Florin Corasba7d8f52019-02-22 13:11:38 -0800313 return 0;
314}
315
Florin Coras653e43f2019-03-04 10:56:23 -0800316static u32
Florin Corasd4295e62019-02-22 13:11:38 -0800317ct_start_listen (u32 app_listener_index, transport_endpoint_t * tep)
318{
319 session_endpoint_cfg_t *sep;
320 ct_connection_t *ct;
321
322 sep = (session_endpoint_cfg_t *) tep;
323 ct = ct_connection_alloc ();
324 ct->server_wrk = sep->app_wrk_index;
325 ct->c_is_ip4 = sep->is_ip4;
326 clib_memcpy (&ct->c_lcl_ip, &sep->ip, sizeof (sep->ip));
327 ct->c_lcl_port = sep->port;
328 ct->actual_tp = sep->transport_proto;
Florin Coras653e43f2019-03-04 10:56:23 -0800329 ct_enable_disable_main_pre_input_node (1 /* is_add */ );
Florin Corasd4295e62019-02-22 13:11:38 -0800330 return ct->c_c_index;
331}
332
Florin Coras653e43f2019-03-04 10:56:23 -0800333static u32
Florin Corasd4295e62019-02-22 13:11:38 -0800334ct_stop_listen (u32 ct_index)
335{
336 ct_connection_t *ct;
337 ct = ct_connection_get (ct_index);
338 ct_connection_free (ct);
Florin Coras653e43f2019-03-04 10:56:23 -0800339 ct_enable_disable_main_pre_input_node (0 /* is_add */ );
Florin Corasd4295e62019-02-22 13:11:38 -0800340 return 0;
341}
342
Florin Coras653e43f2019-03-04 10:56:23 -0800343static transport_connection_t *
Florin Corasd4295e62019-02-22 13:11:38 -0800344ct_listener_get (u32 ct_index)
345{
346 return (transport_connection_t *) ct_connection_get (ct_index);
347}
348
Florin Coras653e43f2019-03-04 10:56:23 -0800349static int
Florin Coras2b81e3c2019-02-27 07:55:46 -0800350ct_session_connect (transport_endpoint_cfg_t * tep)
351{
352 session_endpoint_cfg_t *sep_ext;
353 session_endpoint_t *sep;
354 app_worker_t *app_wrk;
355 session_handle_t lh;
356 application_t *app;
357 app_listener_t *al;
358 u32 table_index;
359 session_t *ll;
360 u8 fib_proto;
361
362 sep_ext = (session_endpoint_cfg_t *) tep;
363 sep = (session_endpoint_t *) tep;
364 app_wrk = app_worker_get (sep_ext->app_wrk_index);
365 app = application_get (app_wrk->app_index);
366
367 sep->transport_proto = sep_ext->original_tp;
368 table_index = application_local_session_table (app);
369 lh = session_lookup_local_endpoint (table_index, sep);
370 if (lh == SESSION_DROP_HANDLE)
Florin Coras00e01d32019-10-21 16:07:46 -0700371 return SESSION_E_FILTERED;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800372
373 if (lh == SESSION_INVALID_HANDLE)
374 goto global_scope;
375
376 ll = listen_session_get_from_handle (lh);
377 al = app_listener_get_w_session (ll);
378
379 /*
380 * Break loop if rule in local table points to connecting app. This
381 * can happen if client is a generic proxy. Route connect through
382 * global table instead.
383 */
384 if (al->app_index == app->app_index)
385 goto global_scope;
386
387 return ct_connect (app_wrk, ll, sep_ext);
388
389 /*
390 * If nothing found, check the global scope for locally attached
391 * destinations. Make sure first that we're allowed to.
392 */
393
394global_scope:
395 if (session_endpoint_is_local (sep))
Florin Coras00e01d32019-10-21 16:07:46 -0700396 return SESSION_E_NOROUTE;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800397
398 if (!application_has_global_scope (app))
Florin Coras00e01d32019-10-21 16:07:46 -0700399 return SESSION_E_SCOPE;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800400
401 fib_proto = session_endpoint_fib_proto (sep);
Florin Coras95cd8642019-12-30 21:53:19 -0800402 table_index = session_lookup_get_index_for_fib (fib_proto, sep->fib_index);
Florin Coras9f1a5432019-03-10 21:03:20 -0700403 ll = session_lookup_listener_wildcard (table_index, sep);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800404
405 if (ll)
406 return ct_connect (app_wrk, ll, sep_ext);
407
408 /* Failed to connect but no error */
409 return 1;
410}
411
Florin Coras653e43f2019-03-04 10:56:23 -0800412static void
Florin Coras2b81e3c2019-02-27 07:55:46 -0800413ct_session_close (u32 ct_index, u32 thread_index)
414{
415 ct_connection_t *ct, *peer_ct;
Florin Coras653e43f2019-03-04 10:56:23 -0800416 app_worker_t *app_wrk;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800417 session_t *s;
418
419 ct = ct_connection_get (ct_index);
420 peer_ct = ct_connection_get (ct->peer_index);
421 if (peer_ct)
422 {
423 peer_ct->peer_index = ~0;
424 session_transport_closing_notify (&peer_ct->connection);
425 }
426
427 s = session_get (ct->c_s_index, 0);
Florin Coras653e43f2019-03-04 10:56:23 -0800428 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
429 if (app_wrk)
430 app_worker_del_segment_notify (app_wrk, ct->segment_handle);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800431 session_free_w_fifos (s);
Florin Coras653e43f2019-03-04 10:56:23 -0800432 if (ct->is_client)
433 segment_manager_dealloc_fifos (ct->client_rx_fifo, ct->client_tx_fifo);
434
Florin Coras2b81e3c2019-02-27 07:55:46 -0800435 ct_connection_free (ct);
Florin Coras653e43f2019-03-04 10:56:23 -0800436 ct_enable_disable_main_pre_input_node (0 /* is_add */ );
Florin Coras2b81e3c2019-02-27 07:55:46 -0800437}
438
Florin Coras653e43f2019-03-04 10:56:23 -0800439static transport_connection_t *
Florin Coras2b81e3c2019-02-27 07:55:46 -0800440ct_session_get (u32 ct_index, u32 thread_index)
441{
442 return (transport_connection_t *) ct_connection_get (ct_index);
443}
444
Florin Corasd4295e62019-02-22 13:11:38 -0800445static u8 *
446format_ct_connection_id (u8 * s, va_list * args)
447{
448 ct_connection_t *ct = va_arg (*args, ct_connection_t *);
449 if (!ct)
450 return s;
451 if (ct->c_is_ip4)
452 {
453 s = format (s, "[%d:%d][CT:%U] %U:%d->%U:%d", ct->c_thread_index,
454 ct->c_s_index, format_transport_proto_short, ct->actual_tp,
455 format_ip4_address, &ct->c_lcl_ip4,
456 clib_net_to_host_u16 (ct->c_lcl_port), format_ip4_address,
457 &ct->c_rmt_ip4, clib_net_to_host_u16 (ct->c_rmt_port));
458 }
459 else
460 {
461 s = format (s, "[%d:%d][CT:%U] %U:%d->%U:%d", ct->c_thread_index,
462 ct->c_s_index, format_transport_proto_short, ct->actual_tp,
463 format_ip6_address, &ct->c_lcl_ip6,
464 clib_net_to_host_u16 (ct->c_lcl_port), format_ip6_address,
465 &ct->c_rmt_ip6, clib_net_to_host_u16 (ct->c_rmt_port));
466 }
467
468 return s;
469}
470
Florin Corasf940f8a2019-03-06 10:44:38 -0800471static int
Florin Coras9f86d222020-03-23 15:34:22 +0000472ct_custom_tx (void *session, transport_send_params_t * sp)
Florin Corasf940f8a2019-03-06 10:44:38 -0800473{
474 session_t *s = (session_t *) session;
475 if (session_has_transport (s))
476 return 0;
477 return ct_session_tx (s);
478}
479
Florin Coras317b8e02019-04-17 09:57:46 -0700480static int
481ct_app_rx_evt (transport_connection_t * tc)
482{
483 ct_connection_t *ct = (ct_connection_t *) tc, *peer_ct;
484 session_t *ps;
485
486 peer_ct = ct_connection_get (ct->peer_index);
487 if (!peer_ct)
488 return -1;
489 ps = session_get (peer_ct->c_s_index, peer_ct->c_thread_index);
490 return session_dequeue_notify (ps);
491}
492
Florin Coras653e43f2019-03-04 10:56:23 -0800493static u8 *
Florin Corasd4295e62019-02-22 13:11:38 -0800494format_ct_listener (u8 * s, va_list * args)
495{
496 u32 tc_index = va_arg (*args, u32);
Aloys Augustina0abbff2019-07-12 12:16:16 +0200497 u32 __clib_unused thread_index = va_arg (*args, u32);
Florin Corasd4295e62019-02-22 13:11:38 -0800498 u32 __clib_unused verbose = va_arg (*args, u32);
499 ct_connection_t *ct = ct_connection_get (tc_index);
500 s = format (s, "%-50U", format_ct_connection_id, ct);
501 if (verbose)
502 s = format (s, "%-15s", "LISTEN");
503 return s;
504}
505
Florin Coras653e43f2019-03-04 10:56:23 -0800506static u8 *
Florin Coras2b81e3c2019-02-27 07:55:46 -0800507format_ct_connection (u8 * s, va_list * args)
508{
509 ct_connection_t *ct = va_arg (*args, ct_connection_t *);
510 u32 verbose = va_arg (*args, u32);
511
512 if (!ct)
513 return s;
514 s = format (s, "%-50U", format_ct_connection_id, ct);
515 if (verbose)
516 {
517 s = format (s, "%-15s", "ESTABLISHED");
518 if (verbose > 1)
519 {
520 s = format (s, "\n");
521 }
522 }
523 return s;
524}
525
Florin Coras653e43f2019-03-04 10:56:23 -0800526static u8 *
Florin Coras2b81e3c2019-02-27 07:55:46 -0800527format_ct_session (u8 * s, va_list * args)
528{
529 u32 ct_index = va_arg (*args, u32);
530 u32 __clib_unused thread_index = va_arg (*args, u32);
531 u32 verbose = va_arg (*args, u32);
532 ct_connection_t *ct;
533
534 ct = ct_connection_get (ct_index);
535 if (!ct)
536 {
537 s = format (s, "empty\n");
538 return s;
539 }
540
541 s = format (s, "%U", format_ct_connection, ct, verbose);
542 return s;
543}
544
Florin Corasd4295e62019-02-22 13:11:38 -0800545/* *INDENT-OFF* */
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200546static const transport_proto_vft_t cut_thru_proto = {
Florin Corasd4295e62019-02-22 13:11:38 -0800547 .start_listen = ct_start_listen,
548 .stop_listen = ct_stop_listen,
549 .get_listener = ct_listener_get,
Florin Coras2b81e3c2019-02-27 07:55:46 -0800550 .connect = ct_session_connect,
551 .close = ct_session_close,
552 .get_connection = ct_session_get,
Florin Corasf940f8a2019-03-06 10:44:38 -0800553 .custom_tx = ct_custom_tx,
Florin Coras317b8e02019-04-17 09:57:46 -0700554 .app_rx_evt = ct_app_rx_evt,
Florin Corasd4295e62019-02-22 13:11:38 -0800555 .format_listener = format_ct_listener,
Florin Coras2b81e3c2019-02-27 07:55:46 -0800556 .format_connection = format_ct_session,
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200557 .transport_options = {
Florin Coras07063b82020-03-13 04:44:51 +0000558 .name = "ct",
559 .short_name = "C",
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200560 .tx_type = TRANSPORT_TX_INTERNAL,
561 .service_type = TRANSPORT_SERVICE_APP,
562 },
Florin Corasd4295e62019-02-22 13:11:38 -0800563};
564/* *INDENT-ON* */
565
Florin Coras653e43f2019-03-04 10:56:23 -0800566int
567ct_session_tx (session_t * s)
568{
569 ct_connection_t *ct, *peer_ct;
570 session_t *peer_s;
571
572 ct = (ct_connection_t *) session_get_transport (s);
573 peer_ct = ct_connection_get (ct->peer_index);
574 if (!peer_ct)
575 return -1;
576 peer_s = session_get (peer_ct->c_s_index, 0);
577 if (peer_s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
578 return 0;
579 return session_enqueue_notify (peer_s);
580}
581
Florin Corasd4295e62019-02-22 13:11:38 -0800582static clib_error_t *
583ct_transport_init (vlib_main_t * vm)
584{
585 transport_register_protocol (TRANSPORT_PROTO_NONE, &cut_thru_proto,
586 FIB_PROTOCOL_IP4, ~0);
Florin Coras653e43f2019-03-04 10:56:23 -0800587 transport_register_protocol (TRANSPORT_PROTO_NONE, &cut_thru_proto,
588 FIB_PROTOCOL_IP6, ~0);
Florin Corasd4295e62019-02-22 13:11:38 -0800589 return 0;
590}
591
592VLIB_INIT_FUNCTION (ct_transport_init);
593
Florin Corasba7d8f52019-02-22 13:11:38 -0800594/*
595 * fd.io coding-style-patch-verification: ON
596 *
597 * Local Variables:
598 * eval: (c-set-style "gnu")
599 * End:
600 */