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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vnet/session/application_local.h>
17 #include <vnet/session/session.h>
19 static ct_connection_t *connections;
22 ct_enable_disable_main_pre_input_node (u8 is_add)
26 if (!vlib_num_workers ())
29 n_conns = pool_elts (connections);
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);
43 static ct_connection_t *
44 ct_connection_alloc (void)
48 pool_get_zero (connections, ct);
49 ct->c_c_index = ct - connections;
50 ct->c_thread_index = 0;
56 static ct_connection_t *
57 ct_connection_get (u32 ct_index)
59 if (pool_is_free_index (connections, ct_index))
61 return pool_elt_at_index (connections, ct_index);
65 ct_connection_free (ct_connection_t * ct)
68 memset (ct, 0xfc, sizeof (*ct));
69 pool_put (connections, ct);
73 ct_session_get_peer (session_t * s)
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);
82 ct_session_endpoint (session_t * ll, session_endpoint_t * sep)
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;
92 ct_session_connect_notify (session_t * ss)
94 ct_connection_t *sct, *cct;
95 app_worker_t *client_wrk;
96 segment_manager_t *sm;
103 ss_index = ss->session_index;
104 sct = (ct_connection_t *) session_get_transport (ss);
105 client_wrk = app_worker_get (sct->client_wrk);
107 sm = segment_manager_get (ss->rx_fifo->segment_manager);
108 seg = segment_manager_get_segment_w_lock (sm, ss->rx_fifo->segment_index);
109 segment_handle = segment_manager_segment_handle (sm, seg);
111 if ((err = app_worker_add_segment_notify (client_wrk, segment_handle)))
113 clib_warning ("failed to notify client %u of new segment",
115 segment_manager_segment_reader_unlock (sm);
120 segment_manager_segment_reader_unlock (sm);
123 /* Alloc client session */
124 cct = ct_connection_get (sct->peer_index);
126 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;
130 cs->listener_handle = SESSION_INVALID_HANDLE;
131 cs->session_state = SESSION_STATE_CONNECTING;
132 cs->app_wrk_index = client_wrk->wrk_index;
133 cs->connection_index = cct->c_c_index;
135 cct->c_s_index = cs->session_index;
136 cct->client_rx_fifo = ss->tx_fifo;
137 cct->client_tx_fifo = ss->rx_fifo;
139 cct->client_rx_fifo->refcnt++;
140 cct->client_tx_fifo->refcnt++;
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))
151 if (app_worker_connect_notify (client_wrk, cs, err, sct->client_opaque))
157 cs = session_get (cct->c_s_index, 0);
158 cs->session_state = SESSION_STATE_READY;
164 ct_init_local_session (app_worker_t * client_wrk, app_worker_t * server_wrk,
165 ct_connection_t * ct, session_t * ls, session_t * ll)
167 u32 round_rx_fifo_sz, round_tx_fifo_sz, sm_index, seg_size;
168 segment_manager_props_t *props;
169 application_t *server;
170 segment_manager_t *sm;
171 u32 margin = 16 << 10;
176 server = application_get (server_wrk->app_index);
178 props = application_segment_manager_properties (server);
179 round_rx_fifo_sz = 1 << max_log2 (props->rx_fifo_size);
180 round_tx_fifo_sz = 1 << max_log2 (props->tx_fifo_size);
181 /* Increase size because of inefficient chunk allocations. Depending on
182 * how data is consumed, it may happen that more chunks than needed are
184 * TODO should remove once allocations are done more efficiently */
185 seg_size = 4 * (round_rx_fifo_sz + round_tx_fifo_sz + margin);
187 sm = app_worker_get_listen_segment_manager (server_wrk, ll);
188 seg_index = segment_manager_add_segment (sm, seg_size);
191 clib_warning ("failed to add new cut-through segment");
194 seg = segment_manager_get_segment_w_lock (sm, seg_index);
196 rv = segment_manager_try_alloc_fifos (seg, ls->thread_index,
198 props->tx_fifo_size, &ls->rx_fifo,
202 clib_warning ("failed to add fifos in cut-through segment");
203 segment_manager_segment_reader_unlock (sm);
207 sm_index = segment_manager_index (sm);
208 ls->rx_fifo->master_session_index = ls->session_index;
209 ls->tx_fifo->master_session_index = ls->session_index;
210 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;
215 segment_handle = segment_manager_segment_handle (sm, seg);
216 if ((rv = app_worker_add_segment_notify (server_wrk, segment_handle)))
218 clib_warning ("failed to notify server of new segment");
219 segment_manager_segment_reader_unlock (sm);
222 segment_manager_segment_reader_unlock (sm);
223 ct->segment_handle = segment_handle;
228 segment_manager_del_segment (sm, seg);
233 ct_connect (app_worker_t * client_wrk, session_t * ll,
234 session_endpoint_cfg_t * sep)
236 u32 cct_index, ll_index, ll_ct_index;
237 ct_connection_t *sct, *cct, *ll_ct;
238 app_worker_t *server_wrk;
241 ll_index = ll->session_index;
242 ll_ct_index = ll->connection_index;
244 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);
250 * Alloc and init client transport
252 cct = ct_connection_get (cct_index);
253 cct->c_thread_index = 0;
254 cct->c_rmt_port = sep->port;
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;
262 * Init server transport
264 sct->c_thread_index = 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;
274 sct->peer_index = cct->c_c_index;
275 cct->peer_index = sct->c_c_index;
278 * Accept server session. Client session is created only after
279 * server confirms accept.
281 ss = session_alloc (0);
282 ll = listen_session_get (ll_index);
283 ss->session_type = session_type_from_proto_and_ip (TRANSPORT_PROTO_NONE,
285 ss->connection_index = sct->c_c_index;
286 ss->listener_handle = listen_session_get_handle (ll);
287 ss->session_state = SESSION_STATE_CREATED;
289 server_wrk = application_listener_select_worker (ll);
290 ss->app_wrk_index = server_wrk->wrk_index;
292 sct->c_s_index = ss->session_index;
293 sct->server_wrk = ss->app_wrk_index;
295 if (ct_init_local_session (client_wrk, server_wrk, sct, ss, ll))
297 clib_warning ("failed");
298 ct_connection_free (sct);
303 ss->session_state = SESSION_STATE_ACCEPTING;
304 if (app_worker_accept_notify (server_wrk, ss))
306 clib_warning ("failed");
307 ct_connection_free (sct);
308 session_free_w_fifos (ss);
312 cct->segment_handle = sct->segment_handle;
313 ct_enable_disable_main_pre_input_node (1 /* is_add */ );
318 ct_start_listen (u32 app_listener_index, transport_endpoint_t * tep)
320 session_endpoint_cfg_t *sep;
323 sep = (session_endpoint_cfg_t *) tep;
324 ct = ct_connection_alloc ();
325 ct->server_wrk = sep->app_wrk_index;
326 ct->c_is_ip4 = sep->is_ip4;
327 clib_memcpy (&ct->c_lcl_ip, &sep->ip, sizeof (sep->ip));
328 ct->c_lcl_port = sep->port;
329 ct->actual_tp = sep->transport_proto;
330 ct_enable_disable_main_pre_input_node (1 /* is_add */ );
331 return ct->c_c_index;
335 ct_stop_listen (u32 ct_index)
338 ct = ct_connection_get (ct_index);
339 ct_connection_free (ct);
340 ct_enable_disable_main_pre_input_node (0 /* is_add */ );
344 static transport_connection_t *
345 ct_listener_get (u32 ct_index)
347 return (transport_connection_t *) ct_connection_get (ct_index);
351 ct_session_connect (transport_endpoint_cfg_t * tep)
353 session_endpoint_cfg_t *sep_ext;
354 session_endpoint_t *sep;
355 app_worker_t *app_wrk;
363 sep_ext = (session_endpoint_cfg_t *) tep;
364 sep = (session_endpoint_t *) tep;
365 app_wrk = app_worker_get (sep_ext->app_wrk_index);
366 app = application_get (app_wrk->app_index);
368 sep->transport_proto = sep_ext->original_tp;
369 table_index = application_local_session_table (app);
370 lh = session_lookup_local_endpoint (table_index, sep);
371 if (lh == SESSION_DROP_HANDLE)
372 return SESSION_E_FILTERED;
374 if (lh == SESSION_INVALID_HANDLE)
377 ll = listen_session_get_from_handle (lh);
378 al = app_listener_get_w_session (ll);
381 * Break loop if rule in local table points to connecting app. This
382 * can happen if client is a generic proxy. Route connect through
383 * global table instead.
385 if (al->app_index == app->app_index)
388 return ct_connect (app_wrk, ll, sep_ext);
391 * If nothing found, check the global scope for locally attached
392 * destinations. Make sure first that we're allowed to.
396 if (session_endpoint_is_local (sep))
397 return SESSION_E_NOROUTE;
399 if (!application_has_global_scope (app))
400 return SESSION_E_SCOPE;
402 fib_proto = session_endpoint_fib_proto (sep);
403 table_index = session_lookup_get_index_for_fib (fib_proto, sep->fib_index);
404 ll = session_lookup_listener_wildcard (table_index, sep);
407 return ct_connect (app_wrk, ll, sep_ext);
409 /* Failed to connect but no error */
414 ct_session_close (u32 ct_index, u32 thread_index)
416 ct_connection_t *ct, *peer_ct;
417 app_worker_t *app_wrk;
420 ct = ct_connection_get (ct_index);
421 peer_ct = ct_connection_get (ct->peer_index);
424 peer_ct->peer_index = ~0;
425 session_transport_closing_notify (&peer_ct->connection);
428 s = session_get (ct->c_s_index, 0);
429 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
431 app_worker_del_segment_notify (app_wrk, ct->segment_handle);
432 session_free_w_fifos (s);
434 segment_manager_dealloc_fifos (ct->client_rx_fifo, ct->client_tx_fifo);
436 ct_connection_free (ct);
437 ct_enable_disable_main_pre_input_node (0 /* is_add */ );
440 static transport_connection_t *
441 ct_session_get (u32 ct_index, u32 thread_index)
443 return (transport_connection_t *) ct_connection_get (ct_index);
447 format_ct_connection_id (u8 * s, va_list * args)
449 ct_connection_t *ct = va_arg (*args, ct_connection_t *);
454 s = format (s, "[%d:%d][CT:%U] %U:%d->%U:%d", ct->c_thread_index,
455 ct->c_s_index, format_transport_proto_short, ct->actual_tp,
456 format_ip4_address, &ct->c_lcl_ip4,
457 clib_net_to_host_u16 (ct->c_lcl_port), format_ip4_address,
458 &ct->c_rmt_ip4, clib_net_to_host_u16 (ct->c_rmt_port));
462 s = format (s, "[%d:%d][CT:%U] %U:%d->%U:%d", ct->c_thread_index,
463 ct->c_s_index, format_transport_proto_short, ct->actual_tp,
464 format_ip6_address, &ct->c_lcl_ip6,
465 clib_net_to_host_u16 (ct->c_lcl_port), format_ip6_address,
466 &ct->c_rmt_ip6, clib_net_to_host_u16 (ct->c_rmt_port));
473 ct_custom_tx (void *session, transport_send_params_t * sp)
475 session_t *s = (session_t *) session;
476 if (session_has_transport (s))
478 return ct_session_tx (s);
482 ct_app_rx_evt (transport_connection_t * tc)
484 ct_connection_t *ct = (ct_connection_t *) tc, *peer_ct;
487 peer_ct = ct_connection_get (ct->peer_index);
490 ps = session_get (peer_ct->c_s_index, peer_ct->c_thread_index);
491 return session_dequeue_notify (ps);
495 format_ct_listener (u8 * s, va_list * args)
497 u32 tc_index = va_arg (*args, u32);
498 u32 __clib_unused thread_index = va_arg (*args, u32);
499 u32 __clib_unused verbose = va_arg (*args, u32);
500 ct_connection_t *ct = ct_connection_get (tc_index);
501 s = format (s, "%-50U", format_ct_connection_id, ct);
503 s = format (s, "%-15s", "LISTEN");
508 format_ct_connection (u8 * s, va_list * args)
510 ct_connection_t *ct = va_arg (*args, ct_connection_t *);
511 u32 verbose = va_arg (*args, u32);
515 s = format (s, "%-50U", format_ct_connection_id, ct);
518 s = format (s, "%-15s", "ESTABLISHED");
521 s = format (s, "\n");
528 format_ct_session (u8 * s, va_list * args)
530 u32 ct_index = va_arg (*args, u32);
531 u32 __clib_unused thread_index = va_arg (*args, u32);
532 u32 verbose = va_arg (*args, u32);
535 ct = ct_connection_get (ct_index);
538 s = format (s, "empty\n");
542 s = format (s, "%U", format_ct_connection, ct, verbose);
547 static const transport_proto_vft_t cut_thru_proto = {
548 .start_listen = ct_start_listen,
549 .stop_listen = ct_stop_listen,
550 .get_listener = ct_listener_get,
551 .connect = ct_session_connect,
552 .close = ct_session_close,
553 .get_connection = ct_session_get,
554 .custom_tx = ct_custom_tx,
555 .app_rx_evt = ct_app_rx_evt,
556 .format_listener = format_ct_listener,
557 .format_connection = format_ct_session,
558 .transport_options = {
561 .tx_type = TRANSPORT_TX_INTERNAL,
562 .service_type = TRANSPORT_SERVICE_APP,
568 ct_session_tx (session_t * s)
570 ct_connection_t *ct, *peer_ct;
573 ct = (ct_connection_t *) session_get_transport (s);
574 peer_ct = ct_connection_get (ct->peer_index);
577 peer_s = session_get (peer_ct->c_s_index, 0);
578 if (peer_s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
580 return session_enqueue_notify (peer_s);
583 static clib_error_t *
584 ct_transport_init (vlib_main_t * vm)
586 transport_register_protocol (TRANSPORT_PROTO_NONE, &cut_thru_proto,
587 FIB_PROTOCOL_IP4, ~0);
588 transport_register_protocol (TRANSPORT_PROTO_NONE, &cut_thru_proto,
589 FIB_PROTOCOL_IP6, ~0);
593 VLIB_INIT_FUNCTION (ct_transport_init);
596 * fd.io coding-style-patch-verification: ON
599 * eval: (c-set-style "gnu")