blob: 25fb39e094b71b08b468729abbe7c4ff52818146 [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 Coras2b81e3c2019-02-27 07:55:46 -080099 int is_fail = 0;
100 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
111 if (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 is_fail = 1;
118 }
119 else
120 {
121 segment_manager_segment_reader_unlock (sm);
122 }
123
Florin Coras2b81e3c2019-02-27 07:55:46 -0800124 /* Alloc client session */
125 cct = ct_connection_get (sct->peer_index);
Florin Corasba7d8f52019-02-22 13:11:38 -0800126
Florin Coras2b81e3c2019-02-27 07:55:46 -0800127 cs = session_alloc (0);
128 ss = session_get (ss_index, 0);
129 cs->session_type = ss->session_type;
130 cs->connection_index = sct->c_c_index;
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +0200131 cs->listener_handle = SESSION_INVALID_HANDLE;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800132 cs->session_state = SESSION_STATE_CONNECTING;
133 cs->app_wrk_index = client_wrk->wrk_index;
134 cs->connection_index = cct->c_c_index;
135
136 cct->c_s_index = cs->session_index;
Florin Coras653e43f2019-03-04 10:56:23 -0800137 cct->client_rx_fifo = ss->tx_fifo;
138 cct->client_tx_fifo = ss->rx_fifo;
139
140 cct->client_rx_fifo->refcnt++;
141 cct->client_tx_fifo->refcnt++;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800142
143 /* This will allocate fifos for the session. They won't be used for
144 * exchanging data but they will be used to close the connection if
145 * the segment manager/worker is freed */
146 if (app_worker_init_connected (client_wrk, cs))
147 {
148 session_close (ss);
149 return -1;
150 }
151
152 if (app_worker_connect_notify (client_wrk, is_fail ? 0 : cs,
153 sct->client_opaque))
154 {
155 session_close (ss);
156 return -1;
157 }
158
159 cs = session_get (cct->c_s_index, 0);
160 cs->session_state = SESSION_STATE_READY;
161
Florin Corasba7d8f52019-02-22 13:11:38 -0800162 return 0;
163}
164
Florin Coras653e43f2019-03-04 10:56:23 -0800165static int
Florin Coras2b81e3c2019-02-27 07:55:46 -0800166ct_init_local_session (app_worker_t * client_wrk, app_worker_t * server_wrk,
167 ct_connection_t * ct, session_t * ls, session_t * ll)
Florin Corasba7d8f52019-02-22 13:11:38 -0800168{
Florin Coras653e43f2019-03-04 10:56:23 -0800169 u32 round_rx_fifo_sz, round_tx_fifo_sz, sm_index, seg_size;
Florin Coras88001c62019-04-24 14:44:46 -0700170 segment_manager_props_t *props;
Florin Coras653e43f2019-03-04 10:56:23 -0800171 application_t *server;
Florin Corasba7d8f52019-02-22 13:11:38 -0800172 segment_manager_t *sm;
Florin Coras653e43f2019-03-04 10:56:23 -0800173 u32 margin = 16 << 10;
Florin Coras88001c62019-04-24 14:44:46 -0700174 fifo_segment_t *seg;
Florin Corasba7d8f52019-02-22 13:11:38 -0800175 u64 segment_handle;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800176 int seg_index, rv;
Florin Corasba7d8f52019-02-22 13:11:38 -0800177
Florin Corasba7d8f52019-02-22 13:11:38 -0800178 server = application_get (server_wrk->app_index);
Florin Corasba7d8f52019-02-22 13:11:38 -0800179
180 props = application_segment_manager_properties (server);
Florin Corasba7d8f52019-02-22 13:11:38 -0800181 round_rx_fifo_sz = 1 << max_log2 (props->rx_fifo_size);
182 round_tx_fifo_sz = 1 << max_log2 (props->tx_fifo_size);
Florin Coras653e43f2019-03-04 10:56:23 -0800183 seg_size = round_rx_fifo_sz + round_tx_fifo_sz + margin;
Florin Corasba7d8f52019-02-22 13:11:38 -0800184
Florin Coras2b81e3c2019-02-27 07:55:46 -0800185 sm = app_worker_get_listen_segment_manager (server_wrk, ll);
Florin Corasba7d8f52019-02-22 13:11:38 -0800186 seg_index = segment_manager_add_segment (sm, seg_size);
187 if (seg_index < 0)
188 {
189 clib_warning ("failed to add new cut-through segment");
190 return seg_index;
191 }
192 seg = segment_manager_get_segment_w_lock (sm, seg_index);
Florin Corasba7d8f52019-02-22 13:11:38 -0800193
Florin Coras62ddc032019-12-08 18:30:42 -0800194 rv = segment_manager_try_alloc_fifos (seg, ls->thread_index,
195 props->rx_fifo_size,
Florin Coras2b81e3c2019-02-27 07:55:46 -0800196 props->tx_fifo_size, &ls->rx_fifo,
197 &ls->tx_fifo);
Florin Corasba7d8f52019-02-22 13:11:38 -0800198 if (rv)
199 {
200 clib_warning ("failed to add fifos in cut-through segment");
201 segment_manager_segment_reader_unlock (sm);
202 goto failed;
203 }
Florin Coras2b81e3c2019-02-27 07:55:46 -0800204
Florin Corasba7d8f52019-02-22 13:11:38 -0800205 sm_index = segment_manager_index (sm);
Florin Coras653e43f2019-03-04 10:56:23 -0800206 ls->rx_fifo->master_session_index = ls->session_index;
207 ls->tx_fifo->master_session_index = ls->session_index;
Florin Corasba7d8f52019-02-22 13:11:38 -0800208 ls->rx_fifo->segment_manager = sm_index;
209 ls->tx_fifo->segment_manager = sm_index;
210 ls->rx_fifo->segment_index = seg_index;
211 ls->tx_fifo->segment_index = seg_index;
Florin Corasba7d8f52019-02-22 13:11:38 -0800212
213 segment_handle = segment_manager_segment_handle (sm, seg);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800214 if ((rv = app_worker_add_segment_notify (server_wrk, segment_handle)))
Florin Corasba7d8f52019-02-22 13:11:38 -0800215 {
216 clib_warning ("failed to notify server of new segment");
217 segment_manager_segment_reader_unlock (sm);
218 goto failed;
219 }
220 segment_manager_segment_reader_unlock (sm);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800221 ct->segment_handle = segment_handle;
Florin Corasba7d8f52019-02-22 13:11:38 -0800222
223 return 0;
224
225failed:
Florin Coras2b81e3c2019-02-27 07:55:46 -0800226 segment_manager_del_segment (sm, seg);
Florin Corasba7d8f52019-02-22 13:11:38 -0800227 return rv;
228}
229
Florin Coras653e43f2019-03-04 10:56:23 -0800230static int
Florin Coras2b81e3c2019-02-27 07:55:46 -0800231ct_connect (app_worker_t * client_wrk, session_t * ll,
232 session_endpoint_cfg_t * sep)
Florin Corasba7d8f52019-02-22 13:11:38 -0800233{
Florin Coras2b81e3c2019-02-27 07:55:46 -0800234 u32 cct_index, ll_index, ll_ct_index;
235 ct_connection_t *sct, *cct, *ll_ct;
236 app_worker_t *server_wrk;
237 session_t *ss;
Florin Corasba7d8f52019-02-22 13:11:38 -0800238
Florin Coras2b81e3c2019-02-27 07:55:46 -0800239 ll_index = ll->session_index;
240 ll_ct_index = ll->connection_index;
Florin Corasba7d8f52019-02-22 13:11:38 -0800241
Florin Coras2b81e3c2019-02-27 07:55:46 -0800242 cct = ct_connection_alloc ();
243 cct_index = cct->c_c_index;
244 sct = ct_connection_alloc ();
245 ll_ct = ct_connection_get (ll_ct_index);
Florin Corasba7d8f52019-02-22 13:11:38 -0800246
Florin Coras2b81e3c2019-02-27 07:55:46 -0800247 /*
248 * Alloc and init client transport
249 */
250 cct = ct_connection_get (cct_index);
251 cct->c_thread_index = 0;
252 cct->c_rmt_port = sep->port;
253 cct->c_lcl_port = 0;
254 cct->c_is_ip4 = sep->is_ip4;
255 clib_memcpy (&cct->c_rmt_ip, &sep->ip, sizeof (sep->ip));
256 cct->actual_tp = ll_ct->actual_tp;
Florin Coras653e43f2019-03-04 10:56:23 -0800257 cct->is_client = 1;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800258
259 /*
260 * Init server transport
261 */
262 sct->c_thread_index = 0;
263 sct->c_rmt_port = 0;
264 sct->c_lcl_port = ll_ct->c_lcl_port;
265 sct->c_is_ip4 = sep->is_ip4;
266 clib_memcpy (&sct->c_lcl_ip, &ll_ct->c_lcl_ip, sizeof (ll_ct->c_lcl_ip));
267 sct->client_wrk = client_wrk->wrk_index;
268 sct->c_proto = TRANSPORT_PROTO_NONE;
269 sct->client_opaque = sep->opaque;
270 sct->actual_tp = ll_ct->actual_tp;
271
272 sct->peer_index = cct->c_c_index;
273 cct->peer_index = sct->c_c_index;
274
275 /*
276 * Accept server session. Client session is created only after
277 * server confirms accept.
278 */
279 ss = session_alloc (0);
280 ll = listen_session_get (ll_index);
Florin Coras9f1a5432019-03-10 21:03:20 -0700281 ss->session_type = session_type_from_proto_and_ip (TRANSPORT_PROTO_NONE,
282 sct->c_is_ip4);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800283 ss->connection_index = sct->c_c_index;
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +0200284 ss->listener_handle = listen_session_get_handle (ll);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800285 ss->session_state = SESSION_STATE_CREATED;
286
287 server_wrk = application_listener_select_worker (ll);
288 ss->app_wrk_index = server_wrk->wrk_index;
289
290 sct->c_s_index = ss->session_index;
291 sct->server_wrk = ss->app_wrk_index;
292
293 if (ct_init_local_session (client_wrk, server_wrk, sct, ss, ll))
Florin Corasba7d8f52019-02-22 13:11:38 -0800294 {
Florin Coras2b81e3c2019-02-27 07:55:46 -0800295 clib_warning ("failed");
296 ct_connection_free (sct);
297 session_free (ss);
298 return -1;
Florin Corasba7d8f52019-02-22 13:11:38 -0800299 }
300
Florin Coras2b81e3c2019-02-27 07:55:46 -0800301 ss->session_state = SESSION_STATE_ACCEPTING;
302 if (app_worker_accept_notify (server_wrk, ss))
303 {
304 clib_warning ("failed");
305 ct_connection_free (sct);
306 session_free_w_fifos (ss);
307 return -1;
308 }
309
Florin Coras2b81e3c2019-02-27 07:55:46 -0800310 cct->segment_handle = sct->segment_handle;
Florin Coras653e43f2019-03-04 10:56:23 -0800311 ct_enable_disable_main_pre_input_node (1 /* is_add */ );
Florin Corasba7d8f52019-02-22 13:11:38 -0800312 return 0;
313}
314
Florin Coras653e43f2019-03-04 10:56:23 -0800315static u32
Florin Corasd4295e62019-02-22 13:11:38 -0800316ct_start_listen (u32 app_listener_index, transport_endpoint_t * tep)
317{
318 session_endpoint_cfg_t *sep;
319 ct_connection_t *ct;
320
321 sep = (session_endpoint_cfg_t *) tep;
322 ct = ct_connection_alloc ();
323 ct->server_wrk = sep->app_wrk_index;
324 ct->c_is_ip4 = sep->is_ip4;
325 clib_memcpy (&ct->c_lcl_ip, &sep->ip, sizeof (sep->ip));
326 ct->c_lcl_port = sep->port;
327 ct->actual_tp = sep->transport_proto;
Florin Coras653e43f2019-03-04 10:56:23 -0800328 ct_enable_disable_main_pre_input_node (1 /* is_add */ );
Florin Corasd4295e62019-02-22 13:11:38 -0800329 return ct->c_c_index;
330}
331
Florin Coras653e43f2019-03-04 10:56:23 -0800332static u32
Florin Corasd4295e62019-02-22 13:11:38 -0800333ct_stop_listen (u32 ct_index)
334{
335 ct_connection_t *ct;
336 ct = ct_connection_get (ct_index);
337 ct_connection_free (ct);
Florin Coras653e43f2019-03-04 10:56:23 -0800338 ct_enable_disable_main_pre_input_node (0 /* is_add */ );
Florin Corasd4295e62019-02-22 13:11:38 -0800339 return 0;
340}
341
Florin Coras653e43f2019-03-04 10:56:23 -0800342static transport_connection_t *
Florin Corasd4295e62019-02-22 13:11:38 -0800343ct_listener_get (u32 ct_index)
344{
345 return (transport_connection_t *) ct_connection_get (ct_index);
346}
347
Florin Coras653e43f2019-03-04 10:56:23 -0800348static int
Florin Coras2b81e3c2019-02-27 07:55:46 -0800349ct_session_connect (transport_endpoint_cfg_t * tep)
350{
351 session_endpoint_cfg_t *sep_ext;
352 session_endpoint_t *sep;
353 app_worker_t *app_wrk;
354 session_handle_t lh;
355 application_t *app;
356 app_listener_t *al;
357 u32 table_index;
358 session_t *ll;
359 u8 fib_proto;
360
361 sep_ext = (session_endpoint_cfg_t *) tep;
362 sep = (session_endpoint_t *) tep;
363 app_wrk = app_worker_get (sep_ext->app_wrk_index);
364 app = application_get (app_wrk->app_index);
365
366 sep->transport_proto = sep_ext->original_tp;
367 table_index = application_local_session_table (app);
368 lh = session_lookup_local_endpoint (table_index, sep);
369 if (lh == SESSION_DROP_HANDLE)
370 return VNET_API_ERROR_APP_CONNECT_FILTERED;
371
372 if (lh == SESSION_INVALID_HANDLE)
373 goto global_scope;
374
375 ll = listen_session_get_from_handle (lh);
376 al = app_listener_get_w_session (ll);
377
378 /*
379 * Break loop if rule in local table points to connecting app. This
380 * can happen if client is a generic proxy. Route connect through
381 * global table instead.
382 */
383 if (al->app_index == app->app_index)
384 goto global_scope;
385
386 return ct_connect (app_wrk, ll, sep_ext);
387
388 /*
389 * If nothing found, check the global scope for locally attached
390 * destinations. Make sure first that we're allowed to.
391 */
392
393global_scope:
394 if (session_endpoint_is_local (sep))
395 return VNET_API_ERROR_SESSION_CONNECT;
396
397 if (!application_has_global_scope (app))
398 return VNET_API_ERROR_APP_CONNECT_SCOPE;
399
400 fib_proto = session_endpoint_fib_proto (sep);
Florin Coras95cd8642019-12-30 21:53:19 -0800401 table_index = session_lookup_get_index_for_fib (fib_proto, sep->fib_index);
Florin Coras9f1a5432019-03-10 21:03:20 -0700402 ll = session_lookup_listener_wildcard (table_index, sep);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800403
404 if (ll)
405 return ct_connect (app_wrk, ll, sep_ext);
406
407 /* Failed to connect but no error */
408 return 1;
409}
410
Florin Coras653e43f2019-03-04 10:56:23 -0800411static void
Florin Coras2b81e3c2019-02-27 07:55:46 -0800412ct_session_close (u32 ct_index, u32 thread_index)
413{
414 ct_connection_t *ct, *peer_ct;
Florin Coras653e43f2019-03-04 10:56:23 -0800415 app_worker_t *app_wrk;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800416 session_t *s;
417
418 ct = ct_connection_get (ct_index);
419 peer_ct = ct_connection_get (ct->peer_index);
420 if (peer_ct)
421 {
422 peer_ct->peer_index = ~0;
423 session_transport_closing_notify (&peer_ct->connection);
424 }
425
426 s = session_get (ct->c_s_index, 0);
Florin Coras653e43f2019-03-04 10:56:23 -0800427 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
428 if (app_wrk)
429 app_worker_del_segment_notify (app_wrk, ct->segment_handle);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800430 session_free_w_fifos (s);
Florin Coras653e43f2019-03-04 10:56:23 -0800431 if (ct->is_client)
432 segment_manager_dealloc_fifos (ct->client_rx_fifo, ct->client_tx_fifo);
433
Florin Coras2b81e3c2019-02-27 07:55:46 -0800434 ct_connection_free (ct);
Florin Coras653e43f2019-03-04 10:56:23 -0800435 ct_enable_disable_main_pre_input_node (0 /* is_add */ );
Florin Coras2b81e3c2019-02-27 07:55:46 -0800436}
437
Florin Coras653e43f2019-03-04 10:56:23 -0800438static transport_connection_t *
Florin Coras2b81e3c2019-02-27 07:55:46 -0800439ct_session_get (u32 ct_index, u32 thread_index)
440{
441 return (transport_connection_t *) ct_connection_get (ct_index);
442}
443
Florin Corasd4295e62019-02-22 13:11:38 -0800444static u8 *
445format_ct_connection_id (u8 * s, va_list * args)
446{
447 ct_connection_t *ct = va_arg (*args, ct_connection_t *);
448 if (!ct)
449 return s;
450 if (ct->c_is_ip4)
451 {
452 s = format (s, "[%d:%d][CT:%U] %U:%d->%U:%d", ct->c_thread_index,
453 ct->c_s_index, format_transport_proto_short, ct->actual_tp,
454 format_ip4_address, &ct->c_lcl_ip4,
455 clib_net_to_host_u16 (ct->c_lcl_port), format_ip4_address,
456 &ct->c_rmt_ip4, clib_net_to_host_u16 (ct->c_rmt_port));
457 }
458 else
459 {
460 s = format (s, "[%d:%d][CT:%U] %U:%d->%U:%d", ct->c_thread_index,
461 ct->c_s_index, format_transport_proto_short, ct->actual_tp,
462 format_ip6_address, &ct->c_lcl_ip6,
463 clib_net_to_host_u16 (ct->c_lcl_port), format_ip6_address,
464 &ct->c_rmt_ip6, clib_net_to_host_u16 (ct->c_rmt_port));
465 }
466
467 return s;
468}
469
Florin Corasf940f8a2019-03-06 10:44:38 -0800470static int
Florin Coras26dd6de2019-07-23 23:54:47 -0700471ct_custom_tx (void *session, u32 max_burst_size)
Florin Corasf940f8a2019-03-06 10:44:38 -0800472{
473 session_t *s = (session_t *) session;
474 if (session_has_transport (s))
475 return 0;
476 return ct_session_tx (s);
477}
478
Florin Coras317b8e02019-04-17 09:57:46 -0700479static int
480ct_app_rx_evt (transport_connection_t * tc)
481{
482 ct_connection_t *ct = (ct_connection_t *) tc, *peer_ct;
483 session_t *ps;
484
485 peer_ct = ct_connection_get (ct->peer_index);
486 if (!peer_ct)
487 return -1;
488 ps = session_get (peer_ct->c_s_index, peer_ct->c_thread_index);
489 return session_dequeue_notify (ps);
490}
491
Florin Coras653e43f2019-03-04 10:56:23 -0800492static u8 *
Florin Corasd4295e62019-02-22 13:11:38 -0800493format_ct_listener (u8 * s, va_list * args)
494{
495 u32 tc_index = va_arg (*args, u32);
Aloys Augustina0abbff2019-07-12 12:16:16 +0200496 u32 __clib_unused thread_index = va_arg (*args, u32);
Florin Corasd4295e62019-02-22 13:11:38 -0800497 u32 __clib_unused verbose = va_arg (*args, u32);
498 ct_connection_t *ct = ct_connection_get (tc_index);
499 s = format (s, "%-50U", format_ct_connection_id, ct);
500 if (verbose)
501 s = format (s, "%-15s", "LISTEN");
502 return s;
503}
504
Florin Coras653e43f2019-03-04 10:56:23 -0800505static u8 *
Florin Coras2b81e3c2019-02-27 07:55:46 -0800506format_ct_connection (u8 * s, va_list * args)
507{
508 ct_connection_t *ct = va_arg (*args, ct_connection_t *);
509 u32 verbose = va_arg (*args, u32);
510
511 if (!ct)
512 return s;
513 s = format (s, "%-50U", format_ct_connection_id, ct);
514 if (verbose)
515 {
516 s = format (s, "%-15s", "ESTABLISHED");
517 if (verbose > 1)
518 {
519 s = format (s, "\n");
520 }
521 }
522 return s;
523}
524
Florin Coras653e43f2019-03-04 10:56:23 -0800525static u8 *
Florin Coras2b81e3c2019-02-27 07:55:46 -0800526format_ct_session (u8 * s, va_list * args)
527{
528 u32 ct_index = va_arg (*args, u32);
529 u32 __clib_unused thread_index = va_arg (*args, u32);
530 u32 verbose = va_arg (*args, u32);
531 ct_connection_t *ct;
532
533 ct = ct_connection_get (ct_index);
534 if (!ct)
535 {
536 s = format (s, "empty\n");
537 return s;
538 }
539
540 s = format (s, "%U", format_ct_connection, ct, verbose);
541 return s;
542}
543
Florin Corasd4295e62019-02-22 13:11:38 -0800544/* *INDENT-OFF* */
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200545static const transport_proto_vft_t cut_thru_proto = {
Florin Corasd4295e62019-02-22 13:11:38 -0800546 .start_listen = ct_start_listen,
547 .stop_listen = ct_stop_listen,
548 .get_listener = ct_listener_get,
Florin Coras2b81e3c2019-02-27 07:55:46 -0800549 .connect = ct_session_connect,
550 .close = ct_session_close,
551 .get_connection = ct_session_get,
Florin Corasf940f8a2019-03-06 10:44:38 -0800552 .custom_tx = ct_custom_tx,
Florin Coras317b8e02019-04-17 09:57:46 -0700553 .app_rx_evt = ct_app_rx_evt,
Florin Corasd4295e62019-02-22 13:11:38 -0800554 .format_listener = format_ct_listener,
Florin Coras2b81e3c2019-02-27 07:55:46 -0800555 .format_connection = format_ct_session,
Nathan Skrzypczake971bc92019-06-19 13:42:37 +0200556 .transport_options = {
557 .tx_type = TRANSPORT_TX_INTERNAL,
558 .service_type = TRANSPORT_SERVICE_APP,
559 },
Florin Corasd4295e62019-02-22 13:11:38 -0800560};
561/* *INDENT-ON* */
562
Florin Coras653e43f2019-03-04 10:56:23 -0800563int
564ct_session_tx (session_t * s)
565{
566 ct_connection_t *ct, *peer_ct;
567 session_t *peer_s;
568
569 ct = (ct_connection_t *) session_get_transport (s);
570 peer_ct = ct_connection_get (ct->peer_index);
571 if (!peer_ct)
572 return -1;
573 peer_s = session_get (peer_ct->c_s_index, 0);
574 if (peer_s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
575 return 0;
576 return session_enqueue_notify (peer_s);
577}
578
Florin Corasd4295e62019-02-22 13:11:38 -0800579static clib_error_t *
580ct_transport_init (vlib_main_t * vm)
581{
582 transport_register_protocol (TRANSPORT_PROTO_NONE, &cut_thru_proto,
583 FIB_PROTOCOL_IP4, ~0);
Florin Coras653e43f2019-03-04 10:56:23 -0800584 transport_register_protocol (TRANSPORT_PROTO_NONE, &cut_thru_proto,
585 FIB_PROTOCOL_IP6, ~0);
Florin Corasd4295e62019-02-22 13:11:38 -0800586 return 0;
587}
588
589VLIB_INIT_FUNCTION (ct_transport_init);
590
Florin Corasba7d8f52019-02-22 13:11:38 -0800591/*
592 * fd.io coding-style-patch-verification: ON
593 *
594 * Local Variables:
595 * eval: (c-set-style "gnu")
596 * End:
597 */