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 typedef struct ct_main_
21 ct_connection_t **connections; /**< Per-worker connection pools */
22 u32 n_workers; /**< Number of vpp workers */
23 u32 n_sessions; /**< Cumulative sessions counter */
26 static ct_main_t ct_main;
28 static ct_connection_t *
29 ct_connection_alloc (u32 thread_index)
33 pool_get_zero (ct_main.connections[thread_index], ct);
34 ct->c_c_index = ct - ct_main.connections[thread_index];
35 ct->c_thread_index = thread_index;
41 static ct_connection_t *
42 ct_connection_get (u32 ct_index, u32 thread_index)
44 if (pool_is_free_index (ct_main.connections[thread_index], ct_index))
46 return pool_elt_at_index (ct_main.connections[thread_index], ct_index);
50 ct_connection_free (ct_connection_t * ct)
54 u32 thread_index = ct->c_thread_index;
55 memset (ct, 0xfc, sizeof (*ct));
56 pool_put (ct_main.connections[thread_index], ct);
59 pool_put (ct_main.connections[ct->c_thread_index], ct);
63 ct_session_get_peer (session_t * s)
65 ct_connection_t *ct, *peer_ct;
66 ct = ct_connection_get (s->connection_index, s->thread_index);
67 peer_ct = ct_connection_get (ct->peer_index, s->thread_index);
68 return session_get (peer_ct->c_s_index, s->thread_index);
72 ct_session_endpoint (session_t * ll, session_endpoint_t * sep)
75 ct = (ct_connection_t *) session_get_transport (ll);
76 sep->transport_proto = ct->actual_tp;
77 sep->port = ct->c_lcl_port;
78 sep->is_ip4 = ct->c_is_ip4;
79 ip_copy (&sep->ip, &ct->c_lcl_ip, ct->c_is_ip4);
83 ct_session_connect_notify (session_t * ss)
85 u32 ss_index, opaque, thread_index;
86 ct_connection_t *sct, *cct;
87 app_worker_t *client_wrk;
88 segment_manager_t *sm;
94 ss_index = ss->session_index;
95 thread_index = ss->thread_index;
96 sct = (ct_connection_t *) session_get_transport (ss);
97 client_wrk = app_worker_get (sct->client_wrk);
98 opaque = sct->client_opaque;
100 sm = segment_manager_get (ss->rx_fifo->segment_manager);
101 seg = segment_manager_get_segment_w_lock (sm, ss->rx_fifo->segment_index);
102 segment_handle = segment_manager_segment_handle (sm, seg);
104 if ((err = app_worker_add_segment_notify (client_wrk, segment_handle)))
106 clib_warning ("failed to notify client %u of new segment",
108 segment_manager_segment_reader_unlock (sm);
114 segment_manager_segment_reader_unlock (sm);
117 /* Alloc client session */
118 cct = ct_connection_get (sct->peer_index, thread_index);
120 cs = session_alloc (thread_index);
121 ss = session_get (ss_index, thread_index);
122 cs->session_type = ss->session_type;
123 cs->listener_handle = SESSION_INVALID_HANDLE;
124 cs->session_state = SESSION_STATE_CONNECTING;
125 cs->app_wrk_index = client_wrk->wrk_index;
126 cs->connection_index = cct->c_c_index;
128 cct->c_s_index = cs->session_index;
129 cct->client_rx_fifo = ss->tx_fifo;
130 cct->client_tx_fifo = ss->rx_fifo;
132 cct->client_rx_fifo->refcnt++;
133 cct->client_tx_fifo->refcnt++;
135 /* This will allocate fifos for the session. They won't be used for
136 * exchanging data but they will be used to close the connection if
137 * the segment manager/worker is freed */
138 if ((err = app_worker_init_connected (client_wrk, cs)))
145 cs->session_state = SESSION_STATE_CONNECTING;
147 if (app_worker_connect_notify (client_wrk, cs, err, opaque))
150 segment_manager_dealloc_fifos (cs->rx_fifo, cs->tx_fifo);
155 cs = session_get (cct->c_s_index, cct->c_thread_index);
156 cs->session_state = SESSION_STATE_READY;
161 app_worker_connect_notify (client_wrk, 0, err, opaque);
166 ct_init_accepted_session (app_worker_t * server_wrk,
167 ct_connection_t * ct, session_t * ls,
170 u32 round_rx_fifo_sz, round_tx_fifo_sz, sm_index, seg_size;
171 segment_manager_props_t *props;
172 application_t *server;
173 segment_manager_t *sm;
174 u32 margin = 16 << 10;
179 server = application_get (server_wrk->app_index);
181 props = application_segment_manager_properties (server);
182 round_rx_fifo_sz = 1 << max_log2 (props->rx_fifo_size);
183 round_tx_fifo_sz = 1 << max_log2 (props->tx_fifo_size);
184 /* Increase size because of inefficient chunk allocations. Depending on
185 * how data is consumed, it may happen that more chunks than needed are
187 * TODO should remove once allocations are done more efficiently */
188 seg_size = 4 * (round_rx_fifo_sz + round_tx_fifo_sz + margin);
190 sm = app_worker_get_listen_segment_manager (server_wrk, ll);
191 seg_index = segment_manager_add_segment (sm, seg_size);
194 clib_warning ("failed to add new cut-through segment");
197 seg = segment_manager_get_segment_w_lock (sm, seg_index);
199 rv = segment_manager_try_alloc_fifos (seg, ls->thread_index,
201 props->tx_fifo_size, &ls->rx_fifo,
205 clib_warning ("failed to add fifos in cut-through segment");
206 segment_manager_segment_reader_unlock (sm);
210 sm_index = segment_manager_index (sm);
211 ls->rx_fifo->master_session_index = ls->session_index;
212 ls->tx_fifo->master_session_index = ls->session_index;
213 ls->rx_fifo->master_thread_index = ls->thread_index;
214 ls->tx_fifo->master_thread_index = ls->thread_index;
215 ls->rx_fifo->segment_manager = sm_index;
216 ls->tx_fifo->segment_manager = sm_index;
217 ls->rx_fifo->segment_index = seg_index;
218 ls->tx_fifo->segment_index = seg_index;
220 segment_handle = segment_manager_segment_handle (sm, seg);
221 if ((rv = app_worker_add_segment_notify (server_wrk, segment_handle)))
223 clib_warning ("failed to notify server of new segment");
224 segment_manager_segment_reader_unlock (sm);
227 segment_manager_segment_reader_unlock (sm);
228 ct->segment_handle = segment_handle;
233 segment_manager_lock_and_del_segment (sm, seg_index);
237 typedef struct ct_accept_rpc_args
245 u32 client_wrk_index;
246 } ct_accept_rpc_args_t;
249 ct_accept_rpc_wrk_handler (void *accept_args)
251 ct_accept_rpc_args_t *args = (ct_accept_rpc_args_t *) accept_args;
252 ct_connection_t *sct, *cct, *ll_ct;
253 app_worker_t *server_wrk;
257 ll = listen_session_get (args->ll_s_index);
259 cct = ct_connection_alloc (args->thread_index);
260 cct_index = cct->c_c_index;
261 sct = ct_connection_alloc (args->thread_index);
262 ll_ct = ct_connection_get (ll->connection_index, 0 /* listener thread */ );
265 * Alloc and init client transport
267 cct = ct_connection_get (cct_index, args->thread_index);
268 cct->c_rmt_port = args->port;
270 cct->c_is_ip4 = args->is_ip4;
271 clib_memcpy (&cct->c_rmt_ip, &args->ip, sizeof (args->ip));
272 cct->actual_tp = ll_ct->actual_tp;
277 * Init server transport
280 sct->c_lcl_port = ll_ct->c_lcl_port;
281 sct->c_is_ip4 = args->is_ip4;
282 clib_memcpy (&sct->c_lcl_ip, &ll_ct->c_lcl_ip, sizeof (ll_ct->c_lcl_ip));
283 sct->client_wrk = args->client_wrk_index;
284 sct->c_proto = TRANSPORT_PROTO_NONE;
285 sct->client_opaque = args->opaque;
286 sct->actual_tp = ll_ct->actual_tp;
288 sct->peer_index = cct->c_c_index;
289 cct->peer_index = sct->c_c_index;
292 * Accept server session. Client session is created only after
293 * server confirms accept.
295 ss = session_alloc (args->thread_index);
296 ll = listen_session_get (args->ll_s_index);
297 ss->session_type = session_type_from_proto_and_ip (TRANSPORT_PROTO_NONE,
299 ss->connection_index = sct->c_c_index;
300 ss->listener_handle = listen_session_get_handle (ll);
301 ss->session_state = SESSION_STATE_CREATED;
303 server_wrk = application_listener_select_worker (ll);
304 ss->app_wrk_index = server_wrk->wrk_index;
306 sct->c_s_index = ss->session_index;
307 sct->server_wrk = ss->app_wrk_index;
309 if (ct_init_accepted_session (server_wrk, sct, ss, ll))
311 ct_connection_free (sct);
316 ss->session_state = SESSION_STATE_ACCEPTING;
317 if (app_worker_accept_notify (server_wrk, ss))
319 ct_connection_free (sct);
320 segment_manager_dealloc_fifos (ss->rx_fifo, ss->tx_fifo);
325 cct->segment_handle = sct->segment_handle;
327 clib_mem_free (args);
331 ct_connect (app_worker_t * client_wrk, session_t * ll,
332 session_endpoint_cfg_t * sep)
334 ct_accept_rpc_args_t *args;
335 ct_main_t *cm = &ct_main;
338 /* Simple round-robin policy for spreading sessions over workers. We skip
339 * thread index 0, i.e., offset the index by 1, when we have workers as it
340 * is the one dedicated to main thread. Note that n_workers does not include
343 thread_index = cm->n_workers ? (cm->n_sessions % cm->n_workers) + 1 : 0;
345 args = clib_mem_alloc (sizeof (*args));
346 args->ll_s_index = ll->session_index;
347 args->thread_index = thread_index;
348 clib_memcpy_fast (&args->ip, &sep->ip, sizeof (ip46_address_t));
349 args->port = sep->port;
350 args->is_ip4 = sep->is_ip4;
351 args->opaque = sep->opaque;
352 args->client_wrk_index = client_wrk->wrk_index;
354 session_send_rpc_evt_to_thread (thread_index, ct_accept_rpc_wrk_handler,
360 ct_start_listen (u32 app_listener_index, transport_endpoint_t * tep)
362 session_endpoint_cfg_t *sep;
365 sep = (session_endpoint_cfg_t *) tep;
366 ct = ct_connection_alloc (0);
367 ct->server_wrk = sep->app_wrk_index;
368 ct->c_is_ip4 = sep->is_ip4;
369 clib_memcpy (&ct->c_lcl_ip, &sep->ip, sizeof (sep->ip));
370 ct->c_lcl_port = sep->port;
371 ct->c_s_index = app_listener_index;
372 ct->actual_tp = sep->transport_proto;
373 return ct->c_c_index;
377 ct_stop_listen (u32 ct_index)
380 ct = ct_connection_get (ct_index, 0);
381 ct_connection_free (ct);
385 static transport_connection_t *
386 ct_listener_get (u32 ct_index)
388 return (transport_connection_t *) ct_connection_get (ct_index, 0);
392 ct_session_connect (transport_endpoint_cfg_t * tep)
394 session_endpoint_cfg_t *sep_ext;
395 session_endpoint_t *sep;
396 app_worker_t *app_wrk;
404 sep_ext = (session_endpoint_cfg_t *) tep;
405 sep = (session_endpoint_t *) tep;
406 app_wrk = app_worker_get (sep_ext->app_wrk_index);
407 app = application_get (app_wrk->app_index);
409 sep->transport_proto = sep_ext->original_tp;
410 table_index = application_local_session_table (app);
411 lh = session_lookup_local_endpoint (table_index, sep);
412 if (lh == SESSION_DROP_HANDLE)
413 return SESSION_E_FILTERED;
415 if (lh == SESSION_INVALID_HANDLE)
418 ll = listen_session_get_from_handle (lh);
419 al = app_listener_get_w_session (ll);
422 * Break loop if rule in local table points to connecting app. This
423 * can happen if client is a generic proxy. Route connect through
424 * global table instead.
426 if (al->app_index == app->app_index)
429 return ct_connect (app_wrk, ll, sep_ext);
432 * If nothing found, check the global scope for locally attached
433 * destinations. Make sure first that we're allowed to.
437 if (session_endpoint_is_local (sep))
438 return SESSION_E_NOROUTE;
440 if (!application_has_global_scope (app))
441 return SESSION_E_SCOPE;
443 fib_proto = session_endpoint_fib_proto (sep);
444 table_index = session_lookup_get_index_for_fib (fib_proto, sep->fib_index);
445 ll = session_lookup_listener_wildcard (table_index, sep);
448 return ct_connect (app_wrk, ll, sep_ext);
450 /* Failed to connect but no error */
455 ct_session_close (u32 ct_index, u32 thread_index)
457 ct_connection_t *ct, *peer_ct;
458 app_worker_t *app_wrk;
461 ct = ct_connection_get (ct_index, thread_index);
462 peer_ct = ct_connection_get (ct->peer_index, thread_index);
465 peer_ct->peer_index = ~0;
466 /* Make sure session was allocated */
467 if (peer_ct->c_s_index != ~0)
468 session_transport_closing_notify (&peer_ct->connection);
470 ct_connection_free (peer_ct);
473 s = session_get (ct->c_s_index, ct->c_thread_index);
474 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
476 app_worker_del_segment_notify (app_wrk, ct->segment_handle);
477 session_free_w_fifos (s);
479 segment_manager_dealloc_fifos (ct->client_rx_fifo, ct->client_tx_fifo);
481 ct_connection_free (ct);
484 static transport_connection_t *
485 ct_session_get (u32 ct_index, u32 thread_index)
487 return (transport_connection_t *) ct_connection_get (ct_index,
492 format_ct_connection_id (u8 * s, va_list * args)
494 ct_connection_t *ct = va_arg (*args, ct_connection_t *);
499 s = format (s, "[%d:%d][CT:%U] %U:%d->%U:%d", ct->c_thread_index,
500 ct->c_s_index, format_transport_proto_short, ct->actual_tp,
501 format_ip4_address, &ct->c_lcl_ip4,
502 clib_net_to_host_u16 (ct->c_lcl_port), format_ip4_address,
503 &ct->c_rmt_ip4, clib_net_to_host_u16 (ct->c_rmt_port));
507 s = format (s, "[%d:%d][CT:%U] %U:%d->%U:%d", ct->c_thread_index,
508 ct->c_s_index, format_transport_proto_short, ct->actual_tp,
509 format_ip6_address, &ct->c_lcl_ip6,
510 clib_net_to_host_u16 (ct->c_lcl_port), format_ip6_address,
511 &ct->c_rmt_ip6, clib_net_to_host_u16 (ct->c_rmt_port));
518 ct_custom_tx (void *session, transport_send_params_t * sp)
520 session_t *s = (session_t *) session;
521 if (session_has_transport (s))
523 /* If event enqueued towards peer, remove from scheduler and
524 * remove session tx flag, i.e., accept new tx events */
525 if (!ct_session_tx (s))
527 sp->flags = TRANSPORT_SND_F_DESCHED;
528 svm_fifo_unset_event (s->tx_fifo);
530 /* The scheduler uses packet count as a means of upper bounding the amount
531 * of work done per dispatch. So make it look like we have sent something */
536 ct_app_rx_evt (transport_connection_t * tc)
538 ct_connection_t *ct = (ct_connection_t *) tc, *peer_ct;
541 peer_ct = ct_connection_get (ct->peer_index, tc->thread_index);
544 ps = session_get (peer_ct->c_s_index, peer_ct->c_thread_index);
545 return session_dequeue_notify (ps);
549 format_ct_listener (u8 * s, va_list * args)
551 u32 tc_index = va_arg (*args, u32);
552 u32 __clib_unused thread_index = va_arg (*args, u32);
553 u32 __clib_unused verbose = va_arg (*args, u32);
554 ct_connection_t *ct = ct_connection_get (tc_index, 0);
555 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_ct_connection_id, ct);
557 s = format (s, "%-" SESSION_CLI_STATE_LEN "s", "LISTEN");
562 format_ct_connection (u8 * s, va_list * args)
564 ct_connection_t *ct = va_arg (*args, ct_connection_t *);
565 u32 verbose = va_arg (*args, u32);
569 s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_ct_connection_id, ct);
572 s = format (s, "%-" SESSION_CLI_STATE_LEN "s", "ESTABLISHED");
575 s = format (s, "\n");
582 format_ct_session (u8 * s, va_list * args)
584 u32 ct_index = va_arg (*args, u32);
585 u32 thread_index = va_arg (*args, u32);
586 u32 verbose = va_arg (*args, u32);
589 ct = ct_connection_get (ct_index, thread_index);
592 s = format (s, "empty\n");
596 s = format (s, "%U", format_ct_connection, ct, verbose);
601 ct_enable_disable (vlib_main_t * vm, u8 is_en)
603 ct_main.n_workers = vlib_num_workers ();
604 vec_validate (ct_main.connections, ct_main.n_workers);
609 static const transport_proto_vft_t cut_thru_proto = {
610 .enable = ct_enable_disable,
611 .start_listen = ct_start_listen,
612 .stop_listen = ct_stop_listen,
613 .get_listener = ct_listener_get,
614 .connect = ct_session_connect,
615 .close = ct_session_close,
616 .get_connection = ct_session_get,
617 .custom_tx = ct_custom_tx,
618 .app_rx_evt = ct_app_rx_evt,
619 .format_listener = format_ct_listener,
620 .format_connection = format_ct_session,
621 .transport_options = {
624 .tx_type = TRANSPORT_TX_INTERNAL,
625 .service_type = TRANSPORT_SERVICE_APP,
631 ct_session_tx (session_t * s)
633 ct_connection_t *ct, *peer_ct;
636 ct = (ct_connection_t *) session_get_transport (s);
637 peer_ct = ct_connection_get (ct->peer_index, ct->c_thread_index);
640 peer_s = session_get (peer_ct->c_s_index, peer_ct->c_thread_index);
641 if (peer_s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
643 return session_enqueue_notify (peer_s);
646 static clib_error_t *
647 ct_transport_init (vlib_main_t * vm)
649 transport_register_protocol (TRANSPORT_PROTO_NONE, &cut_thru_proto,
650 FIB_PROTOCOL_IP4, ~0);
651 transport_register_protocol (TRANSPORT_PROTO_NONE, &cut_thru_proto,
652 FIB_PROTOCOL_IP6, ~0);
656 VLIB_INIT_FUNCTION (ct_transport_init);
659 * fd.io coding-style-patch-verification: ON
662 * eval: (c-set-style "gnu")