blob: d1df657229d41713daf895ace011cae586136cf8 [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
26 n_conns = pool_elts (connections);
27 if (n_conns > 2)
28 return;
29
30 if (n_conns > 0 && is_add)
31 vlib_node_set_state (vlib_get_main (),
32 session_queue_pre_input_node.index,
33 VLIB_NODE_STATE_POLLING);
34 else if (n_conns == 0)
35 vlib_node_set_state (vlib_get_main (),
36 session_queue_pre_input_node.index,
37 VLIB_NODE_STATE_DISABLED);
38}
39
40static ct_connection_t *
Florin Corasd4295e62019-02-22 13:11:38 -080041ct_connection_alloc (void)
42{
43 ct_connection_t *ct;
44
45 pool_get_zero (connections, ct);
46 ct->c_c_index = ct - connections;
47 ct->c_thread_index = 0;
48 ct->client_wrk = ~0;
49 ct->server_wrk = ~0;
50 return ct;
51}
52
Florin Coras653e43f2019-03-04 10:56:23 -080053static ct_connection_t *
Florin Corasd4295e62019-02-22 13:11:38 -080054ct_connection_get (u32 ct_index)
55{
56 if (pool_is_free_index (connections, ct_index))
57 return 0;
58 return pool_elt_at_index (connections, ct_index);
Florin Corasba7d8f52019-02-22 13:11:38 -080059}
60
Florin Coras653e43f2019-03-04 10:56:23 -080061static void
Florin Corasd4295e62019-02-22 13:11:38 -080062ct_connection_free (ct_connection_t * ct)
Florin Corasba7d8f52019-02-22 13:11:38 -080063{
Florin Corasba7d8f52019-02-22 13:11:38 -080064 if (CLIB_DEBUG)
Florin Corasd4295e62019-02-22 13:11:38 -080065 memset (ct, 0xfc, sizeof (*ct));
66 pool_put (connections, ct);
Florin Corasba7d8f52019-02-22 13:11:38 -080067}
68
Florin Coras2b81e3c2019-02-27 07:55:46 -080069session_t *
70ct_session_get_peer (session_t * s)
71{
72 ct_connection_t *ct, *peer_ct;
73 ct = ct_connection_get (s->connection_index);
74 peer_ct = ct_connection_get (ct->peer_index);
75 return session_get (peer_ct->c_s_index, 0);
76}
77
Florin Corasba7d8f52019-02-22 13:11:38 -080078void
Florin Coras2b81e3c2019-02-27 07:55:46 -080079ct_session_endpoint (session_t * ll, session_endpoint_t * sep)
Florin Corasba7d8f52019-02-22 13:11:38 -080080{
Florin Corasd4295e62019-02-22 13:11:38 -080081 ct_connection_t *ct;
82 ct = (ct_connection_t *) session_get_transport (ll);
83 sep->transport_proto = ct->actual_tp;
84 sep->port = ct->c_lcl_port;
85 sep->is_ip4 = ct->c_is_ip4;
Florin Corasba7d8f52019-02-22 13:11:38 -080086}
87
Florin Corasba7d8f52019-02-22 13:11:38 -080088int
Florin Coras2b81e3c2019-02-27 07:55:46 -080089ct_session_connect_notify (session_t * ss)
Florin Corasba7d8f52019-02-22 13:11:38 -080090{
91 svm_fifo_segment_private_t *seg;
Florin Coras2b81e3c2019-02-27 07:55:46 -080092 ct_connection_t *sct, *cct;
93 app_worker_t *client_wrk;
Florin Corasba7d8f52019-02-22 13:11:38 -080094 segment_manager_t *sm;
Florin Corasba7d8f52019-02-22 13:11:38 -080095 u64 segment_handle;
Florin Coras2b81e3c2019-02-27 07:55:46 -080096 int is_fail = 0;
97 session_t *cs;
98 u32 ss_index;
Florin Corasba7d8f52019-02-22 13:11:38 -080099
Florin Coras2b81e3c2019-02-27 07:55:46 -0800100 ss_index = ss->session_index;
101 sct = (ct_connection_t *) session_get_transport (ss);
102 client_wrk = app_worker_get (sct->client_wrk);
Florin Corasba7d8f52019-02-22 13:11:38 -0800103
Florin Coras2b81e3c2019-02-27 07:55:46 -0800104 sm = segment_manager_get (ss->rx_fifo->segment_manager);
105 seg = segment_manager_get_segment_w_lock (sm, ss->rx_fifo->segment_index);
Florin Corasba7d8f52019-02-22 13:11:38 -0800106 segment_handle = segment_manager_segment_handle (sm, seg);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800107
108 if (app_worker_add_segment_notify (client_wrk, segment_handle))
Florin Corasba7d8f52019-02-22 13:11:38 -0800109 {
110 clib_warning ("failed to notify client %u of new segment",
Florin Coras2b81e3c2019-02-27 07:55:46 -0800111 sct->client_wrk);
Florin Corasba7d8f52019-02-22 13:11:38 -0800112 segment_manager_segment_reader_unlock (sm);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800113 session_close (ss);
Florin Corasba7d8f52019-02-22 13:11:38 -0800114 is_fail = 1;
115 }
116 else
117 {
118 segment_manager_segment_reader_unlock (sm);
119 }
120
Florin Coras2b81e3c2019-02-27 07:55:46 -0800121 /* Alloc client session */
122 cct = ct_connection_get (sct->peer_index);
Florin Corasba7d8f52019-02-22 13:11:38 -0800123
Florin Coras2b81e3c2019-02-27 07:55:46 -0800124 cs = session_alloc (0);
125 ss = session_get (ss_index, 0);
126 cs->session_type = ss->session_type;
127 cs->connection_index = sct->c_c_index;
128 cs->listener_index = SESSION_INVALID_INDEX;
129 cs->session_state = SESSION_STATE_CONNECTING;
130 cs->app_wrk_index = client_wrk->wrk_index;
131 cs->connection_index = cct->c_c_index;
132
133 cct->c_s_index = cs->session_index;
Florin Coras653e43f2019-03-04 10:56:23 -0800134 cct->client_rx_fifo = ss->tx_fifo;
135 cct->client_tx_fifo = ss->rx_fifo;
136
137 cct->client_rx_fifo->refcnt++;
138 cct->client_tx_fifo->refcnt++;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800139
140 /* This will allocate fifos for the session. They won't be used for
141 * exchanging data but they will be used to close the connection if
142 * the segment manager/worker is freed */
143 if (app_worker_init_connected (client_wrk, cs))
144 {
145 session_close (ss);
146 return -1;
147 }
148
149 if (app_worker_connect_notify (client_wrk, is_fail ? 0 : cs,
150 sct->client_opaque))
151 {
152 session_close (ss);
153 return -1;
154 }
155
156 cs = session_get (cct->c_s_index, 0);
157 cs->session_state = SESSION_STATE_READY;
158
Florin Corasba7d8f52019-02-22 13:11:38 -0800159 return 0;
160}
161
Florin Coras653e43f2019-03-04 10:56:23 -0800162static int
Florin Coras2b81e3c2019-02-27 07:55:46 -0800163ct_init_local_session (app_worker_t * client_wrk, app_worker_t * server_wrk,
164 ct_connection_t * ct, session_t * ls, session_t * ll)
Florin Corasba7d8f52019-02-22 13:11:38 -0800165{
Florin Coras653e43f2019-03-04 10:56:23 -0800166 u32 round_rx_fifo_sz, round_tx_fifo_sz, sm_index, seg_size;
167 segment_manager_properties_t *props;
Florin Corasba7d8f52019-02-22 13:11:38 -0800168 svm_fifo_segment_private_t *seg;
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 Corasba7d8f52019-02-22 13:11:38 -0800172 u64 segment_handle;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800173 int seg_index, rv;
Florin Corasba7d8f52019-02-22 13:11:38 -0800174
Florin Corasba7d8f52019-02-22 13:11:38 -0800175 server = application_get (server_wrk->app_index);
Florin Corasba7d8f52019-02-22 13:11:38 -0800176
177 props = application_segment_manager_properties (server);
Florin Corasba7d8f52019-02-22 13:11:38 -0800178 round_rx_fifo_sz = 1 << max_log2 (props->rx_fifo_size);
179 round_tx_fifo_sz = 1 << max_log2 (props->tx_fifo_size);
Florin Coras653e43f2019-03-04 10:56:23 -0800180 seg_size = round_rx_fifo_sz + round_tx_fifo_sz + margin;
Florin Corasba7d8f52019-02-22 13:11:38 -0800181
Florin Coras2b81e3c2019-02-27 07:55:46 -0800182 sm = app_worker_get_listen_segment_manager (server_wrk, ll);
Florin Corasba7d8f52019-02-22 13:11:38 -0800183 seg_index = segment_manager_add_segment (sm, seg_size);
184 if (seg_index < 0)
185 {
186 clib_warning ("failed to add new cut-through segment");
187 return seg_index;
188 }
189 seg = segment_manager_get_segment_w_lock (sm, seg_index);
Florin Corasba7d8f52019-02-22 13:11:38 -0800190
Florin Corasba7d8f52019-02-22 13:11:38 -0800191 rv = segment_manager_try_alloc_fifos (seg, props->rx_fifo_size,
Florin Coras2b81e3c2019-02-27 07:55:46 -0800192 props->tx_fifo_size, &ls->rx_fifo,
193 &ls->tx_fifo);
Florin Corasba7d8f52019-02-22 13:11:38 -0800194 if (rv)
195 {
196 clib_warning ("failed to add fifos in cut-through segment");
197 segment_manager_segment_reader_unlock (sm);
198 goto failed;
199 }
Florin Coras2b81e3c2019-02-27 07:55:46 -0800200
Florin Corasba7d8f52019-02-22 13:11:38 -0800201 sm_index = segment_manager_index (sm);
Florin Coras653e43f2019-03-04 10:56:23 -0800202 ls->rx_fifo->master_session_index = ls->session_index;
203 ls->tx_fifo->master_session_index = ls->session_index;
Florin Corasba7d8f52019-02-22 13:11:38 -0800204 ls->rx_fifo->segment_manager = sm_index;
205 ls->tx_fifo->segment_manager = sm_index;
206 ls->rx_fifo->segment_index = seg_index;
207 ls->tx_fifo->segment_index = seg_index;
Florin Corasba7d8f52019-02-22 13:11:38 -0800208
209 segment_handle = segment_manager_segment_handle (sm, seg);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800210 if ((rv = app_worker_add_segment_notify (server_wrk, segment_handle)))
Florin Corasba7d8f52019-02-22 13:11:38 -0800211 {
212 clib_warning ("failed to notify server of new segment");
213 segment_manager_segment_reader_unlock (sm);
214 goto failed;
215 }
216 segment_manager_segment_reader_unlock (sm);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800217 ct->segment_handle = segment_handle;
Florin Corasba7d8f52019-02-22 13:11:38 -0800218
219 return 0;
220
221failed:
Florin Coras2b81e3c2019-02-27 07:55:46 -0800222 segment_manager_del_segment (sm, seg);
Florin Corasba7d8f52019-02-22 13:11:38 -0800223 return rv;
224}
225
Florin Coras653e43f2019-03-04 10:56:23 -0800226static int
Florin Coras2b81e3c2019-02-27 07:55:46 -0800227ct_connect (app_worker_t * client_wrk, session_t * ll,
228 session_endpoint_cfg_t * sep)
Florin Corasba7d8f52019-02-22 13:11:38 -0800229{
Florin Coras2b81e3c2019-02-27 07:55:46 -0800230 u32 cct_index, ll_index, ll_ct_index;
231 ct_connection_t *sct, *cct, *ll_ct;
232 app_worker_t *server_wrk;
233 session_t *ss;
Florin Corasba7d8f52019-02-22 13:11:38 -0800234
Florin Coras2b81e3c2019-02-27 07:55:46 -0800235 ll_index = ll->session_index;
236 ll_ct_index = ll->connection_index;
Florin Corasba7d8f52019-02-22 13:11:38 -0800237
Florin Coras2b81e3c2019-02-27 07:55:46 -0800238 cct = ct_connection_alloc ();
239 cct_index = cct->c_c_index;
240 sct = ct_connection_alloc ();
241 ll_ct = ct_connection_get (ll_ct_index);
Florin Corasba7d8f52019-02-22 13:11:38 -0800242
Florin Coras2b81e3c2019-02-27 07:55:46 -0800243 /*
244 * Alloc and init client transport
245 */
246 cct = ct_connection_get (cct_index);
247 cct->c_thread_index = 0;
248 cct->c_rmt_port = sep->port;
249 cct->c_lcl_port = 0;
250 cct->c_is_ip4 = sep->is_ip4;
251 clib_memcpy (&cct->c_rmt_ip, &sep->ip, sizeof (sep->ip));
252 cct->actual_tp = ll_ct->actual_tp;
Florin Coras653e43f2019-03-04 10:56:23 -0800253 cct->is_client = 1;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800254
255 /*
256 * Init server transport
257 */
258 sct->c_thread_index = 0;
259 sct->c_rmt_port = 0;
260 sct->c_lcl_port = ll_ct->c_lcl_port;
261 sct->c_is_ip4 = sep->is_ip4;
262 clib_memcpy (&sct->c_lcl_ip, &ll_ct->c_lcl_ip, sizeof (ll_ct->c_lcl_ip));
263 sct->client_wrk = client_wrk->wrk_index;
264 sct->c_proto = TRANSPORT_PROTO_NONE;
265 sct->client_opaque = sep->opaque;
266 sct->actual_tp = ll_ct->actual_tp;
267
268 sct->peer_index = cct->c_c_index;
269 cct->peer_index = sct->c_c_index;
270
271 /*
272 * Accept server session. Client session is created only after
273 * server confirms accept.
274 */
275 ss = session_alloc (0);
276 ll = listen_session_get (ll_index);
Florin Coras9f1a5432019-03-10 21:03:20 -0700277 ss->session_type = session_type_from_proto_and_ip (TRANSPORT_PROTO_NONE,
278 sct->c_is_ip4);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800279 ss->connection_index = sct->c_c_index;
280 ss->listener_index = ll->session_index;
281 ss->session_state = SESSION_STATE_CREATED;
282
283 server_wrk = application_listener_select_worker (ll);
284 ss->app_wrk_index = server_wrk->wrk_index;
285
286 sct->c_s_index = ss->session_index;
287 sct->server_wrk = ss->app_wrk_index;
288
289 if (ct_init_local_session (client_wrk, server_wrk, sct, ss, ll))
Florin Corasba7d8f52019-02-22 13:11:38 -0800290 {
Florin Coras2b81e3c2019-02-27 07:55:46 -0800291 clib_warning ("failed");
292 ct_connection_free (sct);
293 session_free (ss);
294 return -1;
Florin Corasba7d8f52019-02-22 13:11:38 -0800295 }
296
Florin Coras2b81e3c2019-02-27 07:55:46 -0800297 ss->session_state = SESSION_STATE_ACCEPTING;
298 if (app_worker_accept_notify (server_wrk, ss))
299 {
300 clib_warning ("failed");
301 ct_connection_free (sct);
302 session_free_w_fifos (ss);
303 return -1;
304 }
305
Florin Coras2b81e3c2019-02-27 07:55:46 -0800306 cct->segment_handle = sct->segment_handle;
Florin Coras653e43f2019-03-04 10:56:23 -0800307 ct_enable_disable_main_pre_input_node (1 /* is_add */ );
Florin Corasba7d8f52019-02-22 13:11:38 -0800308 return 0;
309}
310
Florin Coras653e43f2019-03-04 10:56:23 -0800311static u32
Florin Corasd4295e62019-02-22 13:11:38 -0800312ct_start_listen (u32 app_listener_index, transport_endpoint_t * tep)
313{
314 session_endpoint_cfg_t *sep;
315 ct_connection_t *ct;
316
317 sep = (session_endpoint_cfg_t *) tep;
318 ct = ct_connection_alloc ();
319 ct->server_wrk = sep->app_wrk_index;
320 ct->c_is_ip4 = sep->is_ip4;
321 clib_memcpy (&ct->c_lcl_ip, &sep->ip, sizeof (sep->ip));
322 ct->c_lcl_port = sep->port;
323 ct->actual_tp = sep->transport_proto;
Florin Coras653e43f2019-03-04 10:56:23 -0800324 ct_enable_disable_main_pre_input_node (1 /* is_add */ );
Florin Corasd4295e62019-02-22 13:11:38 -0800325 return ct->c_c_index;
326}
327
Florin Coras653e43f2019-03-04 10:56:23 -0800328static u32
Florin Corasd4295e62019-02-22 13:11:38 -0800329ct_stop_listen (u32 ct_index)
330{
331 ct_connection_t *ct;
332 ct = ct_connection_get (ct_index);
333 ct_connection_free (ct);
Florin Coras653e43f2019-03-04 10:56:23 -0800334 ct_enable_disable_main_pre_input_node (0 /* is_add */ );
Florin Corasd4295e62019-02-22 13:11:38 -0800335 return 0;
336}
337
Florin Coras653e43f2019-03-04 10:56:23 -0800338static transport_connection_t *
Florin Corasd4295e62019-02-22 13:11:38 -0800339ct_listener_get (u32 ct_index)
340{
341 return (transport_connection_t *) ct_connection_get (ct_index);
342}
343
Florin Coras653e43f2019-03-04 10:56:23 -0800344static int
Florin Coras2b81e3c2019-02-27 07:55:46 -0800345ct_session_connect (transport_endpoint_cfg_t * tep)
346{
347 session_endpoint_cfg_t *sep_ext;
348 session_endpoint_t *sep;
349 app_worker_t *app_wrk;
350 session_handle_t lh;
351 application_t *app;
352 app_listener_t *al;
353 u32 table_index;
354 session_t *ll;
355 u8 fib_proto;
356
357 sep_ext = (session_endpoint_cfg_t *) tep;
358 sep = (session_endpoint_t *) tep;
359 app_wrk = app_worker_get (sep_ext->app_wrk_index);
360 app = application_get (app_wrk->app_index);
361
362 sep->transport_proto = sep_ext->original_tp;
363 table_index = application_local_session_table (app);
364 lh = session_lookup_local_endpoint (table_index, sep);
365 if (lh == SESSION_DROP_HANDLE)
366 return VNET_API_ERROR_APP_CONNECT_FILTERED;
367
368 if (lh == SESSION_INVALID_HANDLE)
369 goto global_scope;
370
371 ll = listen_session_get_from_handle (lh);
372 al = app_listener_get_w_session (ll);
373
374 /*
375 * Break loop if rule in local table points to connecting app. This
376 * can happen if client is a generic proxy. Route connect through
377 * global table instead.
378 */
379 if (al->app_index == app->app_index)
380 goto global_scope;
381
382 return ct_connect (app_wrk, ll, sep_ext);
383
384 /*
385 * If nothing found, check the global scope for locally attached
386 * destinations. Make sure first that we're allowed to.
387 */
388
389global_scope:
390 if (session_endpoint_is_local (sep))
391 return VNET_API_ERROR_SESSION_CONNECT;
392
393 if (!application_has_global_scope (app))
394 return VNET_API_ERROR_APP_CONNECT_SCOPE;
395
396 fib_proto = session_endpoint_fib_proto (sep);
397 table_index = application_session_table (app, fib_proto);
Florin Coras9f1a5432019-03-10 21:03:20 -0700398 ll = session_lookup_listener_wildcard (table_index, sep);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800399
400 if (ll)
401 return ct_connect (app_wrk, ll, sep_ext);
402
403 /* Failed to connect but no error */
404 return 1;
405}
406
Florin Coras653e43f2019-03-04 10:56:23 -0800407static void
Florin Coras2b81e3c2019-02-27 07:55:46 -0800408ct_session_close (u32 ct_index, u32 thread_index)
409{
410 ct_connection_t *ct, *peer_ct;
Florin Coras653e43f2019-03-04 10:56:23 -0800411 app_worker_t *app_wrk;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800412 session_t *s;
413
414 ct = ct_connection_get (ct_index);
415 peer_ct = ct_connection_get (ct->peer_index);
416 if (peer_ct)
417 {
418 peer_ct->peer_index = ~0;
419 session_transport_closing_notify (&peer_ct->connection);
420 }
421
422 s = session_get (ct->c_s_index, 0);
Florin Coras653e43f2019-03-04 10:56:23 -0800423 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
424 if (app_wrk)
425 app_worker_del_segment_notify (app_wrk, ct->segment_handle);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800426 session_free_w_fifos (s);
Florin Coras653e43f2019-03-04 10:56:23 -0800427 if (ct->is_client)
428 segment_manager_dealloc_fifos (ct->client_rx_fifo, ct->client_tx_fifo);
429
Florin Coras2b81e3c2019-02-27 07:55:46 -0800430 ct_connection_free (ct);
Florin Coras653e43f2019-03-04 10:56:23 -0800431 ct_enable_disable_main_pre_input_node (0 /* is_add */ );
Florin Coras2b81e3c2019-02-27 07:55:46 -0800432}
433
Florin Coras653e43f2019-03-04 10:56:23 -0800434static transport_connection_t *
Florin Coras2b81e3c2019-02-27 07:55:46 -0800435ct_session_get (u32 ct_index, u32 thread_index)
436{
437 return (transport_connection_t *) ct_connection_get (ct_index);
438}
439
Florin Corasd4295e62019-02-22 13:11:38 -0800440static u8 *
441format_ct_connection_id (u8 * s, va_list * args)
442{
443 ct_connection_t *ct = va_arg (*args, ct_connection_t *);
444 if (!ct)
445 return s;
446 if (ct->c_is_ip4)
447 {
448 s = format (s, "[%d:%d][CT:%U] %U:%d->%U:%d", ct->c_thread_index,
449 ct->c_s_index, format_transport_proto_short, ct->actual_tp,
450 format_ip4_address, &ct->c_lcl_ip4,
451 clib_net_to_host_u16 (ct->c_lcl_port), format_ip4_address,
452 &ct->c_rmt_ip4, clib_net_to_host_u16 (ct->c_rmt_port));
453 }
454 else
455 {
456 s = format (s, "[%d:%d][CT:%U] %U:%d->%U:%d", ct->c_thread_index,
457 ct->c_s_index, format_transport_proto_short, ct->actual_tp,
458 format_ip6_address, &ct->c_lcl_ip6,
459 clib_net_to_host_u16 (ct->c_lcl_port), format_ip6_address,
460 &ct->c_rmt_ip6, clib_net_to_host_u16 (ct->c_rmt_port));
461 }
462
463 return s;
464}
465
Florin Corasf940f8a2019-03-06 10:44:38 -0800466static int
467ct_custom_tx (void *session)
468{
469 session_t *s = (session_t *) session;
470 if (session_has_transport (s))
471 return 0;
472 return ct_session_tx (s);
473}
474
Florin Coras653e43f2019-03-04 10:56:23 -0800475static u8 *
Florin Corasd4295e62019-02-22 13:11:38 -0800476format_ct_listener (u8 * s, va_list * args)
477{
478 u32 tc_index = va_arg (*args, u32);
479 u32 __clib_unused verbose = va_arg (*args, u32);
480 ct_connection_t *ct = ct_connection_get (tc_index);
481 s = format (s, "%-50U", format_ct_connection_id, ct);
482 if (verbose)
483 s = format (s, "%-15s", "LISTEN");
484 return s;
485}
486
Florin Coras653e43f2019-03-04 10:56:23 -0800487static u8 *
Florin Coras2b81e3c2019-02-27 07:55:46 -0800488format_ct_connection (u8 * s, va_list * args)
489{
490 ct_connection_t *ct = va_arg (*args, ct_connection_t *);
491 u32 verbose = va_arg (*args, u32);
492
493 if (!ct)
494 return s;
495 s = format (s, "%-50U", format_ct_connection_id, ct);
496 if (verbose)
497 {
498 s = format (s, "%-15s", "ESTABLISHED");
499 if (verbose > 1)
500 {
501 s = format (s, "\n");
502 }
503 }
504 return s;
505}
506
Florin Coras653e43f2019-03-04 10:56:23 -0800507static u8 *
Florin Coras2b81e3c2019-02-27 07:55:46 -0800508format_ct_session (u8 * s, va_list * args)
509{
510 u32 ct_index = va_arg (*args, u32);
511 u32 __clib_unused thread_index = va_arg (*args, u32);
512 u32 verbose = va_arg (*args, u32);
513 ct_connection_t *ct;
514
515 ct = ct_connection_get (ct_index);
516 if (!ct)
517 {
518 s = format (s, "empty\n");
519 return s;
520 }
521
522 s = format (s, "%U", format_ct_connection, ct, verbose);
523 return s;
524}
525
Florin Corasd4295e62019-02-22 13:11:38 -0800526/* *INDENT-OFF* */
527const static transport_proto_vft_t cut_thru_proto = {
528 .start_listen = ct_start_listen,
529 .stop_listen = ct_stop_listen,
530 .get_listener = ct_listener_get,
Florin Coras2b81e3c2019-02-27 07:55:46 -0800531 .connect = ct_session_connect,
532 .close = ct_session_close,
533 .get_connection = ct_session_get,
Florin Corasf940f8a2019-03-06 10:44:38 -0800534 .custom_tx = ct_custom_tx,
Florin Corasd4295e62019-02-22 13:11:38 -0800535 .tx_type = TRANSPORT_TX_INTERNAL,
536 .service_type = TRANSPORT_SERVICE_APP,
537 .format_listener = format_ct_listener,
Florin Coras2b81e3c2019-02-27 07:55:46 -0800538 .format_connection = format_ct_session,
Florin Corasd4295e62019-02-22 13:11:38 -0800539};
540/* *INDENT-ON* */
541
Florin Coras653e43f2019-03-04 10:56:23 -0800542int
543ct_session_tx (session_t * s)
544{
545 ct_connection_t *ct, *peer_ct;
546 session_t *peer_s;
547
548 ct = (ct_connection_t *) session_get_transport (s);
549 peer_ct = ct_connection_get (ct->peer_index);
550 if (!peer_ct)
551 return -1;
552 peer_s = session_get (peer_ct->c_s_index, 0);
553 if (peer_s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
554 return 0;
555 return session_enqueue_notify (peer_s);
556}
557
Florin Corasd4295e62019-02-22 13:11:38 -0800558static clib_error_t *
559ct_transport_init (vlib_main_t * vm)
560{
561 transport_register_protocol (TRANSPORT_PROTO_NONE, &cut_thru_proto,
562 FIB_PROTOCOL_IP4, ~0);
Florin Coras653e43f2019-03-04 10:56:23 -0800563 transport_register_protocol (TRANSPORT_PROTO_NONE, &cut_thru_proto,
564 FIB_PROTOCOL_IP6, ~0);
Florin Corasd4295e62019-02-22 13:11:38 -0800565 return 0;
566}
567
568VLIB_INIT_FUNCTION (ct_transport_init);
569
Florin Corasba7d8f52019-02-22 13:11:38 -0800570/*
571 * fd.io coding-style-patch-verification: ON
572 *
573 * Local Variables:
574 * eval: (c-set-style "gnu")
575 * End:
576 */