blob: 9378e5e638076777c1c0d0c2983efc84ad4a4c93 [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 Corasd4295e62019-02-22 13:11:38 -080019ct_connection_t *connections;
20
21ct_connection_t *
22ct_connection_alloc (void)
23{
24 ct_connection_t *ct;
25
26 pool_get_zero (connections, ct);
27 ct->c_c_index = ct - connections;
28 ct->c_thread_index = 0;
29 ct->client_wrk = ~0;
30 ct->server_wrk = ~0;
31 return ct;
32}
33
34ct_connection_t *
35ct_connection_get (u32 ct_index)
36{
37 if (pool_is_free_index (connections, ct_index))
38 return 0;
39 return pool_elt_at_index (connections, ct_index);
Florin Corasba7d8f52019-02-22 13:11:38 -080040}
41
42void
Florin Corasd4295e62019-02-22 13:11:38 -080043ct_connection_free (ct_connection_t * ct)
Florin Corasba7d8f52019-02-22 13:11:38 -080044{
Florin Corasba7d8f52019-02-22 13:11:38 -080045 if (CLIB_DEBUG)
Florin Corasd4295e62019-02-22 13:11:38 -080046 memset (ct, 0xfc, sizeof (*ct));
47 pool_put (connections, ct);
Florin Corasba7d8f52019-02-22 13:11:38 -080048}
49
Florin Coras2b81e3c2019-02-27 07:55:46 -080050session_t *
51ct_session_get_peer (session_t * s)
52{
53 ct_connection_t *ct, *peer_ct;
54 ct = ct_connection_get (s->connection_index);
55 peer_ct = ct_connection_get (ct->peer_index);
56 return session_get (peer_ct->c_s_index, 0);
57}
58
Florin Corasba7d8f52019-02-22 13:11:38 -080059void
Florin Coras2b81e3c2019-02-27 07:55:46 -080060ct_session_endpoint (session_t * ll, session_endpoint_t * sep)
Florin Corasba7d8f52019-02-22 13:11:38 -080061{
Florin Corasd4295e62019-02-22 13:11:38 -080062 ct_connection_t *ct;
63 ct = (ct_connection_t *) session_get_transport (ll);
64 sep->transport_proto = ct->actual_tp;
65 sep->port = ct->c_lcl_port;
66 sep->is_ip4 = ct->c_is_ip4;
Florin Corasba7d8f52019-02-22 13:11:38 -080067}
68
Florin Corasba7d8f52019-02-22 13:11:38 -080069int
Florin Coras2b81e3c2019-02-27 07:55:46 -080070ct_session_connect_notify (session_t * ss)
Florin Corasba7d8f52019-02-22 13:11:38 -080071{
72 svm_fifo_segment_private_t *seg;
Florin Coras2b81e3c2019-02-27 07:55:46 -080073 ct_connection_t *sct, *cct;
74 app_worker_t *client_wrk;
Florin Corasba7d8f52019-02-22 13:11:38 -080075 segment_manager_t *sm;
Florin Corasba7d8f52019-02-22 13:11:38 -080076 u64 segment_handle;
Florin Coras2b81e3c2019-02-27 07:55:46 -080077 int is_fail = 0;
78 session_t *cs;
79 u32 ss_index;
Florin Corasba7d8f52019-02-22 13:11:38 -080080
Florin Coras2b81e3c2019-02-27 07:55:46 -080081 ss_index = ss->session_index;
82 sct = (ct_connection_t *) session_get_transport (ss);
83 client_wrk = app_worker_get (sct->client_wrk);
Florin Corasba7d8f52019-02-22 13:11:38 -080084
Florin Coras2b81e3c2019-02-27 07:55:46 -080085 sm = segment_manager_get (ss->rx_fifo->segment_manager);
86 seg = segment_manager_get_segment_w_lock (sm, ss->rx_fifo->segment_index);
Florin Corasba7d8f52019-02-22 13:11:38 -080087 segment_handle = segment_manager_segment_handle (sm, seg);
Florin Coras2b81e3c2019-02-27 07:55:46 -080088
89 if (app_worker_add_segment_notify (client_wrk, segment_handle))
Florin Corasba7d8f52019-02-22 13:11:38 -080090 {
91 clib_warning ("failed to notify client %u of new segment",
Florin Coras2b81e3c2019-02-27 07:55:46 -080092 sct->client_wrk);
Florin Corasba7d8f52019-02-22 13:11:38 -080093 segment_manager_segment_reader_unlock (sm);
Florin Coras2b81e3c2019-02-27 07:55:46 -080094 session_close (ss);
Florin Corasba7d8f52019-02-22 13:11:38 -080095 is_fail = 1;
96 }
97 else
98 {
99 segment_manager_segment_reader_unlock (sm);
100 }
101
Florin Coras2b81e3c2019-02-27 07:55:46 -0800102 /* Alloc client session */
103 cct = ct_connection_get (sct->peer_index);
Florin Corasba7d8f52019-02-22 13:11:38 -0800104
Florin Coras2b81e3c2019-02-27 07:55:46 -0800105 cs = session_alloc (0);
106 ss = session_get (ss_index, 0);
107 cs->session_type = ss->session_type;
108 cs->connection_index = sct->c_c_index;
109 cs->listener_index = SESSION_INVALID_INDEX;
110 cs->session_state = SESSION_STATE_CONNECTING;
111 cs->app_wrk_index = client_wrk->wrk_index;
112 cs->connection_index = cct->c_c_index;
113
114 cct->c_s_index = cs->session_index;
115
116 /* This will allocate fifos for the session. They won't be used for
117 * exchanging data but they will be used to close the connection if
118 * the segment manager/worker is freed */
119 if (app_worker_init_connected (client_wrk, cs))
120 {
121 session_close (ss);
122 return -1;
123 }
124
125 if (app_worker_connect_notify (client_wrk, is_fail ? 0 : cs,
126 sct->client_opaque))
127 {
128 session_close (ss);
129 return -1;
130 }
131
132 cs = session_get (cct->c_s_index, 0);
133 cs->session_state = SESSION_STATE_READY;
134
Florin Corasba7d8f52019-02-22 13:11:38 -0800135 return 0;
136}
137
138static void
Florin Coras2b81e3c2019-02-27 07:55:46 -0800139ct_session_fix_eventds (svm_msg_q_t * sq, svm_msg_q_t * cq)
Florin Corasba7d8f52019-02-22 13:11:38 -0800140{
141 int fd;
142
143 /*
144 * segment manager initializes only the producer eventds, since vpp is
145 * typically the producer. But for local sessions, we also pass to the
146 * apps the mqs they listen on for events from peer apps, so they are also
147 * consumer fds.
148 */
149 fd = svm_msg_q_get_producer_eventfd (sq);
150 svm_msg_q_set_consumer_eventfd (sq, fd);
151 fd = svm_msg_q_get_producer_eventfd (cq);
152 svm_msg_q_set_consumer_eventfd (cq, fd);
153}
154
155int
Florin Coras2b81e3c2019-02-27 07:55:46 -0800156ct_init_local_session (app_worker_t * client_wrk, app_worker_t * server_wrk,
157 ct_connection_t * ct, session_t * ls, session_t * ll)
Florin Corasba7d8f52019-02-22 13:11:38 -0800158{
159 u32 seg_size, evt_q_sz, evt_q_elts, margin = 16 << 10;
160 u32 round_rx_fifo_sz, round_tx_fifo_sz, sm_index;
161 segment_manager_properties_t *props, *cprops;
Florin Corasba7d8f52019-02-22 13:11:38 -0800162 svm_fifo_segment_private_t *seg;
163 application_t *server, *client;
164 segment_manager_t *sm;
Florin Corasba7d8f52019-02-22 13:11:38 -0800165 svm_msg_q_t *sq, *cq;
166 u64 segment_handle;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800167 int seg_index, rv;
Florin Corasba7d8f52019-02-22 13:11:38 -0800168
Florin Corasba7d8f52019-02-22 13:11:38 -0800169 server = application_get (server_wrk->app_index);
170 client = application_get (client_wrk->app_index);
171
172 props = application_segment_manager_properties (server);
173 cprops = application_segment_manager_properties (client);
174 evt_q_elts = props->evt_q_size + cprops->evt_q_size;
175 evt_q_sz = segment_manager_evt_q_expected_size (evt_q_elts);
176 round_rx_fifo_sz = 1 << max_log2 (props->rx_fifo_size);
177 round_tx_fifo_sz = 1 << max_log2 (props->tx_fifo_size);
178 seg_size = round_rx_fifo_sz + round_tx_fifo_sz + evt_q_sz + margin;
179
Florin Coras2b81e3c2019-02-27 07:55:46 -0800180 sm = app_worker_get_listen_segment_manager (server_wrk, ll);
Florin Corasba7d8f52019-02-22 13:11:38 -0800181 seg_index = segment_manager_add_segment (sm, seg_size);
182 if (seg_index < 0)
183 {
184 clib_warning ("failed to add new cut-through segment");
185 return seg_index;
186 }
187 seg = segment_manager_get_segment_w_lock (sm, seg_index);
188 sq = segment_manager_alloc_queue (seg, props);
189 cq = segment_manager_alloc_queue (seg, cprops);
190
191 if (props->use_mq_eventfd)
Florin Coras2b81e3c2019-02-27 07:55:46 -0800192 ct_session_fix_eventds (sq, cq);
Florin Corasba7d8f52019-02-22 13:11:38 -0800193
Florin Coras2b81e3c2019-02-27 07:55:46 -0800194 ct->server_evt_q = pointer_to_uword (sq);
195 ct->client_evt_q = pointer_to_uword (cq);
Florin Corasba7d8f52019-02-22 13:11:38 -0800196 rv = segment_manager_try_alloc_fifos (seg, props->rx_fifo_size,
Florin Coras2b81e3c2019-02-27 07:55:46 -0800197 props->tx_fifo_size, &ls->rx_fifo,
198 &ls->tx_fifo);
Florin Corasba7d8f52019-02-22 13:11:38 -0800199 if (rv)
200 {
201 clib_warning ("failed to add fifos in cut-through segment");
202 segment_manager_segment_reader_unlock (sm);
203 goto failed;
204 }
Florin Coras2b81e3c2019-02-27 07:55:46 -0800205
Florin Corasba7d8f52019-02-22 13:11:38 -0800206 sm_index = segment_manager_index (sm);
207 ls->rx_fifo->ct_session_index = ls->session_index;
208 ls->tx_fifo->ct_session_index = ls->session_index;
209 ls->rx_fifo->segment_manager = sm_index;
210 ls->tx_fifo->segment_manager = sm_index;
211 ls->rx_fifo->segment_index = seg_index;
212 ls->tx_fifo->segment_index = seg_index;
Florin Corasba7d8f52019-02-22 13:11:38 -0800213
214 segment_handle = segment_manager_segment_handle (sm, seg);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800215 if ((rv = app_worker_add_segment_notify (server_wrk, segment_handle)))
Florin Corasba7d8f52019-02-22 13:11:38 -0800216 {
217 clib_warning ("failed to notify server of new segment");
218 segment_manager_segment_reader_unlock (sm);
219 goto failed;
220 }
221 segment_manager_segment_reader_unlock (sm);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800222 ct->segment_handle = segment_handle;
Florin Corasba7d8f52019-02-22 13:11:38 -0800223
224 return 0;
225
226failed:
Florin Coras2b81e3c2019-02-27 07:55:46 -0800227 segment_manager_del_segment (sm, seg);
Florin Corasba7d8f52019-02-22 13:11:38 -0800228 return rv;
229}
230
231int
Florin Coras2b81e3c2019-02-27 07:55:46 -0800232ct_connect (app_worker_t * client_wrk, session_t * ll,
233 session_endpoint_cfg_t * sep)
Florin Corasba7d8f52019-02-22 13:11:38 -0800234{
Florin Coras2b81e3c2019-02-27 07:55:46 -0800235 u32 cct_index, ll_index, ll_ct_index;
236 ct_connection_t *sct, *cct, *ll_ct;
237 app_worker_t *server_wrk;
238 session_t *ss;
Florin Corasba7d8f52019-02-22 13:11:38 -0800239
Florin Coras2b81e3c2019-02-27 07:55:46 -0800240 ll_index = ll->session_index;
241 ll_ct_index = ll->connection_index;
Florin Corasba7d8f52019-02-22 13:11:38 -0800242
Florin Coras2b81e3c2019-02-27 07:55:46 -0800243 cct = ct_connection_alloc ();
244 cct_index = cct->c_c_index;
245 sct = ct_connection_alloc ();
246 ll_ct = ct_connection_get (ll_ct_index);
Florin Corasba7d8f52019-02-22 13:11:38 -0800247
Florin Coras2b81e3c2019-02-27 07:55:46 -0800248 /*
249 * Alloc and init client transport
250 */
251 cct = ct_connection_get (cct_index);
252 cct->c_thread_index = 0;
253 cct->c_rmt_port = sep->port;
254 cct->c_lcl_port = 0;
255 cct->c_is_ip4 = sep->is_ip4;
256 clib_memcpy (&cct->c_rmt_ip, &sep->ip, sizeof (sep->ip));
257 cct->actual_tp = ll_ct->actual_tp;
258
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);
281 ss->session_type = ll->session_type;
282 ss->connection_index = sct->c_c_index;
283 ss->listener_index = ll->session_index;
284 ss->session_state = SESSION_STATE_CREATED;
285
286 server_wrk = application_listener_select_worker (ll);
287 ss->app_wrk_index = server_wrk->wrk_index;
288
289 sct->c_s_index = ss->session_index;
290 sct->server_wrk = ss->app_wrk_index;
291
292 if (ct_init_local_session (client_wrk, server_wrk, sct, ss, ll))
Florin Corasba7d8f52019-02-22 13:11:38 -0800293 {
Florin Coras2b81e3c2019-02-27 07:55:46 -0800294 clib_warning ("failed");
295 ct_connection_free (sct);
296 session_free (ss);
297 return -1;
Florin Corasba7d8f52019-02-22 13:11:38 -0800298 }
299
Florin Coras2b81e3c2019-02-27 07:55:46 -0800300 ss->session_state = SESSION_STATE_ACCEPTING;
301 if (app_worker_accept_notify (server_wrk, ss))
302 {
303 clib_warning ("failed");
304 ct_connection_free (sct);
305 session_free_w_fifos (ss);
306 return -1;
307 }
308
309 cct->client_evt_q = sct->client_evt_q;
310 cct->server_evt_q = sct->server_evt_q;
311 cct->segment_handle = sct->segment_handle;
Florin Corasba7d8f52019-02-22 13:11:38 -0800312
313 return 0;
314}
315
Florin Corasd4295e62019-02-22 13:11:38 -0800316u32
317ct_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;
329 return ct->c_c_index;
330}
331
332u32
333ct_stop_listen (u32 ct_index)
334{
335 ct_connection_t *ct;
336 ct = ct_connection_get (ct_index);
337 ct_connection_free (ct);
338 return 0;
339}
340
341transport_connection_t *
342ct_listener_get (u32 ct_index)
343{
344 return (transport_connection_t *) ct_connection_get (ct_index);
345}
346
Florin Coras2b81e3c2019-02-27 07:55:46 -0800347int
348ct_session_connect (transport_endpoint_cfg_t * tep)
349{
350 session_endpoint_cfg_t *sep_ext;
351 session_endpoint_t *sep;
352 app_worker_t *app_wrk;
353 session_handle_t lh;
354 application_t *app;
355 app_listener_t *al;
356 u32 table_index;
357 session_t *ll;
358 u8 fib_proto;
359
360 sep_ext = (session_endpoint_cfg_t *) tep;
361 sep = (session_endpoint_t *) tep;
362 app_wrk = app_worker_get (sep_ext->app_wrk_index);
363 app = application_get (app_wrk->app_index);
364
365 sep->transport_proto = sep_ext->original_tp;
366 table_index = application_local_session_table (app);
367 lh = session_lookup_local_endpoint (table_index, sep);
368 if (lh == SESSION_DROP_HANDLE)
369 return VNET_API_ERROR_APP_CONNECT_FILTERED;
370
371 if (lh == SESSION_INVALID_HANDLE)
372 goto global_scope;
373
374 ll = listen_session_get_from_handle (lh);
375 al = app_listener_get_w_session (ll);
376
377 /*
378 * Break loop if rule in local table points to connecting app. This
379 * can happen if client is a generic proxy. Route connect through
380 * global table instead.
381 */
382 if (al->app_index == app->app_index)
383 goto global_scope;
384
385 return ct_connect (app_wrk, ll, sep_ext);
386
387 /*
388 * If nothing found, check the global scope for locally attached
389 * destinations. Make sure first that we're allowed to.
390 */
391
392global_scope:
393 if (session_endpoint_is_local (sep))
394 return VNET_API_ERROR_SESSION_CONNECT;
395
396 if (!application_has_global_scope (app))
397 return VNET_API_ERROR_APP_CONNECT_SCOPE;
398
399 fib_proto = session_endpoint_fib_proto (sep);
400 table_index = application_session_table (app, fib_proto);
401 ll = session_lookup_listener (table_index, sep);
402
403 if (ll)
404 return ct_connect (app_wrk, ll, sep_ext);
405
406 /* Failed to connect but no error */
407 return 1;
408}
409
410void
411ct_session_close (u32 ct_index, u32 thread_index)
412{
413 ct_connection_t *ct, *peer_ct;
414 session_t *s;
415
416 ct = ct_connection_get (ct_index);
417 peer_ct = ct_connection_get (ct->peer_index);
418 if (peer_ct)
419 {
420 peer_ct->peer_index = ~0;
421 session_transport_closing_notify (&peer_ct->connection);
422 }
423
424 s = session_get (ct->c_s_index, 0);
425 app_worker_del_segment_notify (app_worker_get (s->app_wrk_index),
426 ct->segment_handle);
427 session_free_w_fifos (s);
428 ct_connection_free (ct);
429}
430
431transport_connection_t *
432ct_session_get (u32 ct_index, u32 thread_index)
433{
434 return (transport_connection_t *) ct_connection_get (ct_index);
435}
436
Florin Corasd4295e62019-02-22 13:11:38 -0800437static u8 *
438format_ct_connection_id (u8 * s, va_list * args)
439{
440 ct_connection_t *ct = va_arg (*args, ct_connection_t *);
441 if (!ct)
442 return s;
443 if (ct->c_is_ip4)
444 {
445 s = format (s, "[%d:%d][CT:%U] %U:%d->%U:%d", ct->c_thread_index,
446 ct->c_s_index, format_transport_proto_short, ct->actual_tp,
447 format_ip4_address, &ct->c_lcl_ip4,
448 clib_net_to_host_u16 (ct->c_lcl_port), format_ip4_address,
449 &ct->c_rmt_ip4, clib_net_to_host_u16 (ct->c_rmt_port));
450 }
451 else
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_ip6_address, &ct->c_lcl_ip6,
456 clib_net_to_host_u16 (ct->c_lcl_port), format_ip6_address,
457 &ct->c_rmt_ip6, clib_net_to_host_u16 (ct->c_rmt_port));
458 }
459
460 return s;
461}
462
463u8 *
464format_ct_listener (u8 * s, va_list * args)
465{
466 u32 tc_index = va_arg (*args, u32);
467 u32 __clib_unused verbose = va_arg (*args, u32);
468 ct_connection_t *ct = ct_connection_get (tc_index);
469 s = format (s, "%-50U", format_ct_connection_id, ct);
470 if (verbose)
471 s = format (s, "%-15s", "LISTEN");
472 return s;
473}
474
Florin Coras2b81e3c2019-02-27 07:55:46 -0800475u8 *
476format_ct_connection (u8 * s, va_list * args)
477{
478 ct_connection_t *ct = va_arg (*args, ct_connection_t *);
479 u32 verbose = va_arg (*args, u32);
480
481 if (!ct)
482 return s;
483 s = format (s, "%-50U", format_ct_connection_id, ct);
484 if (verbose)
485 {
486 s = format (s, "%-15s", "ESTABLISHED");
487 if (verbose > 1)
488 {
489 s = format (s, "\n");
490 }
491 }
492 return s;
493}
494
495u8 *
496format_ct_session (u8 * s, va_list * args)
497{
498 u32 ct_index = va_arg (*args, u32);
499 u32 __clib_unused thread_index = va_arg (*args, u32);
500 u32 verbose = va_arg (*args, u32);
501 ct_connection_t *ct;
502
503 ct = ct_connection_get (ct_index);
504 if (!ct)
505 {
506 s = format (s, "empty\n");
507 return s;
508 }
509
510 s = format (s, "%U", format_ct_connection, ct, verbose);
511 return s;
512}
513
Florin Corasd4295e62019-02-22 13:11:38 -0800514/* *INDENT-OFF* */
515const static transport_proto_vft_t cut_thru_proto = {
516 .start_listen = ct_start_listen,
517 .stop_listen = ct_stop_listen,
518 .get_listener = ct_listener_get,
Florin Coras2b81e3c2019-02-27 07:55:46 -0800519 .connect = ct_session_connect,
520 .close = ct_session_close,
521 .get_connection = ct_session_get,
Florin Corasd4295e62019-02-22 13:11:38 -0800522 .tx_type = TRANSPORT_TX_INTERNAL,
523 .service_type = TRANSPORT_SERVICE_APP,
524 .format_listener = format_ct_listener,
Florin Coras2b81e3c2019-02-27 07:55:46 -0800525 .format_connection = format_ct_session,
Florin Corasd4295e62019-02-22 13:11:38 -0800526};
527/* *INDENT-ON* */
528
529static clib_error_t *
530ct_transport_init (vlib_main_t * vm)
531{
532 transport_register_protocol (TRANSPORT_PROTO_NONE, &cut_thru_proto,
533 FIB_PROTOCOL_IP4, ~0);
534 return 0;
535}
536
537VLIB_INIT_FUNCTION (ct_transport_init);
538
Florin Corasba7d8f52019-02-22 13:11:38 -0800539/*
540 * fd.io coding-style-patch-verification: ON
541 *
542 * Local Variables:
543 * eval: (c-set-style "gnu")
544 * End:
545 */