session: api to add new transport types
[vpp.git] / src / vnet / session / application_local.c
1 /*
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
19 static ct_connection_t *connections;
20
21 static void
22 ct_enable_disable_main_pre_input_node (u8 is_add)
23 {
24   u32 n_conns;
25
26   if (!vlib_num_workers ())
27     return;
28
29   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
43 static ct_connection_t *
44 ct_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
56 static ct_connection_t *
57 ct_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);
62 }
63
64 static void
65 ct_connection_free (ct_connection_t * ct)
66 {
67   if (CLIB_DEBUG)
68     memset (ct, 0xfc, sizeof (*ct));
69   pool_put (connections, ct);
70 }
71
72 session_t *
73 ct_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
81 void
82 ct_session_endpoint (session_t * ll, session_endpoint_t * sep)
83 {
84   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;
89 }
90
91 int
92 ct_session_connect_notify (session_t * ss)
93 {
94   ct_connection_t *sct, *cct;
95   app_worker_t *client_wrk;
96   segment_manager_t *sm;
97   fifo_segment_t *seg;
98   u64 segment_handle;
99   int is_fail = 0;
100   session_t *cs;
101   u32 ss_index;
102
103   ss_index = ss->session_index;
104   sct = (ct_connection_t *) session_get_transport (ss);
105   client_wrk = app_worker_get (sct->client_wrk);
106
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);
110
111   if (app_worker_add_segment_notify (client_wrk, segment_handle))
112     {
113       clib_warning ("failed to notify client %u of new segment",
114                     sct->client_wrk);
115       segment_manager_segment_reader_unlock (sm);
116       session_close (ss);
117       is_fail = 1;
118     }
119   else
120     {
121       segment_manager_segment_reader_unlock (sm);
122     }
123
124   /* Alloc client session */
125   cct = ct_connection_get (sct->peer_index);
126
127   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;
131   cs->listener_handle = SESSION_INVALID_HANDLE;
132   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;
137   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++;
142
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
162   return 0;
163 }
164
165 static int
166 ct_init_local_session (app_worker_t * client_wrk, app_worker_t * server_wrk,
167                        ct_connection_t * ct, session_t * ls, session_t * ll)
168 {
169   u32 round_rx_fifo_sz, round_tx_fifo_sz, sm_index, seg_size;
170   segment_manager_props_t *props;
171   application_t *server;
172   segment_manager_t *sm;
173   u32 margin = 16 << 10;
174   fifo_segment_t *seg;
175   u64 segment_handle;
176   int seg_index, rv;
177
178   server = application_get (server_wrk->app_index);
179
180   props = application_segment_manager_properties (server);
181   round_rx_fifo_sz = 1 << max_log2 (props->rx_fifo_size);
182   round_tx_fifo_sz = 1 << max_log2 (props->tx_fifo_size);
183   /* Increase size because of inefficient chunk allocations. Depending on
184    * how data is consumed, it may happen that more chunks than needed are
185    * allocated.
186    * TODO should remove once allocations are done more efficiently */
187   seg_size = 4 * (round_rx_fifo_sz + round_tx_fifo_sz + margin);
188
189   sm = app_worker_get_listen_segment_manager (server_wrk, ll);
190   seg_index = segment_manager_add_segment (sm, seg_size);
191   if (seg_index < 0)
192     {
193       clib_warning ("failed to add new cut-through segment");
194       return seg_index;
195     }
196   seg = segment_manager_get_segment_w_lock (sm, seg_index);
197
198   rv = segment_manager_try_alloc_fifos (seg, ls->thread_index,
199                                         props->rx_fifo_size,
200                                         props->tx_fifo_size, &ls->rx_fifo,
201                                         &ls->tx_fifo);
202   if (rv)
203     {
204       clib_warning ("failed to add fifos in cut-through segment");
205       segment_manager_segment_reader_unlock (sm);
206       goto failed;
207     }
208
209   sm_index = segment_manager_index (sm);
210   ls->rx_fifo->master_session_index = ls->session_index;
211   ls->tx_fifo->master_session_index = ls->session_index;
212   ls->rx_fifo->segment_manager = sm_index;
213   ls->tx_fifo->segment_manager = sm_index;
214   ls->rx_fifo->segment_index = seg_index;
215   ls->tx_fifo->segment_index = seg_index;
216
217   segment_handle = segment_manager_segment_handle (sm, seg);
218   if ((rv = app_worker_add_segment_notify (server_wrk, segment_handle)))
219     {
220       clib_warning ("failed to notify server of new segment");
221       segment_manager_segment_reader_unlock (sm);
222       goto failed;
223     }
224   segment_manager_segment_reader_unlock (sm);
225   ct->segment_handle = segment_handle;
226
227   return 0;
228
229 failed:
230   segment_manager_del_segment (sm, seg);
231   return rv;
232 }
233
234 static int
235 ct_connect (app_worker_t * client_wrk, session_t * ll,
236             session_endpoint_cfg_t * sep)
237 {
238   u32 cct_index, ll_index, ll_ct_index;
239   ct_connection_t *sct, *cct, *ll_ct;
240   app_worker_t *server_wrk;
241   session_t *ss;
242
243   ll_index = ll->session_index;
244   ll_ct_index = ll->connection_index;
245
246   cct = ct_connection_alloc ();
247   cct_index = cct->c_c_index;
248   sct = ct_connection_alloc ();
249   ll_ct = ct_connection_get (ll_ct_index);
250
251   /*
252    * Alloc and init client transport
253    */
254   cct = ct_connection_get (cct_index);
255   cct->c_thread_index = 0;
256   cct->c_rmt_port = sep->port;
257   cct->c_lcl_port = 0;
258   cct->c_is_ip4 = sep->is_ip4;
259   clib_memcpy (&cct->c_rmt_ip, &sep->ip, sizeof (sep->ip));
260   cct->actual_tp = ll_ct->actual_tp;
261   cct->is_client = 1;
262
263   /*
264    * Init server transport
265    */
266   sct->c_thread_index = 0;
267   sct->c_rmt_port = 0;
268   sct->c_lcl_port = ll_ct->c_lcl_port;
269   sct->c_is_ip4 = sep->is_ip4;
270   clib_memcpy (&sct->c_lcl_ip, &ll_ct->c_lcl_ip, sizeof (ll_ct->c_lcl_ip));
271   sct->client_wrk = client_wrk->wrk_index;
272   sct->c_proto = TRANSPORT_PROTO_NONE;
273   sct->client_opaque = sep->opaque;
274   sct->actual_tp = ll_ct->actual_tp;
275
276   sct->peer_index = cct->c_c_index;
277   cct->peer_index = sct->c_c_index;
278
279   /*
280    * Accept server session. Client session is created only after
281    * server confirms accept.
282    */
283   ss = session_alloc (0);
284   ll = listen_session_get (ll_index);
285   ss->session_type = session_type_from_proto_and_ip (TRANSPORT_PROTO_NONE,
286                                                      sct->c_is_ip4);
287   ss->connection_index = sct->c_c_index;
288   ss->listener_handle = listen_session_get_handle (ll);
289   ss->session_state = SESSION_STATE_CREATED;
290
291   server_wrk = application_listener_select_worker (ll);
292   ss->app_wrk_index = server_wrk->wrk_index;
293
294   sct->c_s_index = ss->session_index;
295   sct->server_wrk = ss->app_wrk_index;
296
297   if (ct_init_local_session (client_wrk, server_wrk, sct, ss, ll))
298     {
299       clib_warning ("failed");
300       ct_connection_free (sct);
301       session_free (ss);
302       return -1;
303     }
304
305   ss->session_state = SESSION_STATE_ACCEPTING;
306   if (app_worker_accept_notify (server_wrk, ss))
307     {
308       clib_warning ("failed");
309       ct_connection_free (sct);
310       session_free_w_fifos (ss);
311       return -1;
312     }
313
314   cct->segment_handle = sct->segment_handle;
315   ct_enable_disable_main_pre_input_node (1 /* is_add */ );
316   return 0;
317 }
318
319 static u32
320 ct_start_listen (u32 app_listener_index, transport_endpoint_t * tep)
321 {
322   session_endpoint_cfg_t *sep;
323   ct_connection_t *ct;
324
325   sep = (session_endpoint_cfg_t *) tep;
326   ct = ct_connection_alloc ();
327   ct->server_wrk = sep->app_wrk_index;
328   ct->c_is_ip4 = sep->is_ip4;
329   clib_memcpy (&ct->c_lcl_ip, &sep->ip, sizeof (sep->ip));
330   ct->c_lcl_port = sep->port;
331   ct->actual_tp = sep->transport_proto;
332   ct_enable_disable_main_pre_input_node (1 /* is_add */ );
333   return ct->c_c_index;
334 }
335
336 static u32
337 ct_stop_listen (u32 ct_index)
338 {
339   ct_connection_t *ct;
340   ct = ct_connection_get (ct_index);
341   ct_connection_free (ct);
342   ct_enable_disable_main_pre_input_node (0 /* is_add */ );
343   return 0;
344 }
345
346 static transport_connection_t *
347 ct_listener_get (u32 ct_index)
348 {
349   return (transport_connection_t *) ct_connection_get (ct_index);
350 }
351
352 static int
353 ct_session_connect (transport_endpoint_cfg_t * tep)
354 {
355   session_endpoint_cfg_t *sep_ext;
356   session_endpoint_t *sep;
357   app_worker_t *app_wrk;
358   session_handle_t lh;
359   application_t *app;
360   app_listener_t *al;
361   u32 table_index;
362   session_t *ll;
363   u8 fib_proto;
364
365   sep_ext = (session_endpoint_cfg_t *) tep;
366   sep = (session_endpoint_t *) tep;
367   app_wrk = app_worker_get (sep_ext->app_wrk_index);
368   app = application_get (app_wrk->app_index);
369
370   sep->transport_proto = sep_ext->original_tp;
371   table_index = application_local_session_table (app);
372   lh = session_lookup_local_endpoint (table_index, sep);
373   if (lh == SESSION_DROP_HANDLE)
374     return VNET_API_ERROR_APP_CONNECT_FILTERED;
375
376   if (lh == SESSION_INVALID_HANDLE)
377     goto global_scope;
378
379   ll = listen_session_get_from_handle (lh);
380   al = app_listener_get_w_session (ll);
381
382   /*
383    * Break loop if rule in local table points to connecting app. This
384    * can happen if client is a generic proxy. Route connect through
385    * global table instead.
386    */
387   if (al->app_index == app->app_index)
388     goto global_scope;
389
390   return ct_connect (app_wrk, ll, sep_ext);
391
392   /*
393    * If nothing found, check the global scope for locally attached
394    * destinations. Make sure first that we're allowed to.
395    */
396
397 global_scope:
398   if (session_endpoint_is_local (sep))
399     return VNET_API_ERROR_SESSION_CONNECT;
400
401   if (!application_has_global_scope (app))
402     return VNET_API_ERROR_APP_CONNECT_SCOPE;
403
404   fib_proto = session_endpoint_fib_proto (sep);
405   table_index = session_lookup_get_index_for_fib (fib_proto, sep->fib_index);
406   ll = session_lookup_listener_wildcard (table_index, sep);
407
408   if (ll)
409     return ct_connect (app_wrk, ll, sep_ext);
410
411   /* Failed to connect but no error */
412   return 1;
413 }
414
415 static void
416 ct_session_close (u32 ct_index, u32 thread_index)
417 {
418   ct_connection_t *ct, *peer_ct;
419   app_worker_t *app_wrk;
420   session_t *s;
421
422   ct = ct_connection_get (ct_index);
423   peer_ct = ct_connection_get (ct->peer_index);
424   if (peer_ct)
425     {
426       peer_ct->peer_index = ~0;
427       session_transport_closing_notify (&peer_ct->connection);
428     }
429
430   s = session_get (ct->c_s_index, 0);
431   app_wrk = app_worker_get_if_valid (s->app_wrk_index);
432   if (app_wrk)
433     app_worker_del_segment_notify (app_wrk, ct->segment_handle);
434   session_free_w_fifos (s);
435   if (ct->is_client)
436     segment_manager_dealloc_fifos (ct->client_rx_fifo, ct->client_tx_fifo);
437
438   ct_connection_free (ct);
439   ct_enable_disable_main_pre_input_node (0 /* is_add */ );
440 }
441
442 static transport_connection_t *
443 ct_session_get (u32 ct_index, u32 thread_index)
444 {
445   return (transport_connection_t *) ct_connection_get (ct_index);
446 }
447
448 static u8 *
449 format_ct_connection_id (u8 * s, va_list * args)
450 {
451   ct_connection_t *ct = va_arg (*args, ct_connection_t *);
452   if (!ct)
453     return s;
454   if (ct->c_is_ip4)
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_ip4_address, &ct->c_lcl_ip4,
459                   clib_net_to_host_u16 (ct->c_lcl_port), format_ip4_address,
460                   &ct->c_rmt_ip4, clib_net_to_host_u16 (ct->c_rmt_port));
461     }
462   else
463     {
464       s = format (s, "[%d:%d][CT:%U] %U:%d->%U:%d", ct->c_thread_index,
465                   ct->c_s_index, format_transport_proto_short, ct->actual_tp,
466                   format_ip6_address, &ct->c_lcl_ip6,
467                   clib_net_to_host_u16 (ct->c_lcl_port), format_ip6_address,
468                   &ct->c_rmt_ip6, clib_net_to_host_u16 (ct->c_rmt_port));
469     }
470
471   return s;
472 }
473
474 static int
475 ct_custom_tx (void *session, u32 max_burst_size)
476 {
477   session_t *s = (session_t *) session;
478   if (session_has_transport (s))
479     return 0;
480   return ct_session_tx (s);
481 }
482
483 static int
484 ct_app_rx_evt (transport_connection_t * tc)
485 {
486   ct_connection_t *ct = (ct_connection_t *) tc, *peer_ct;
487   session_t *ps;
488
489   peer_ct = ct_connection_get (ct->peer_index);
490   if (!peer_ct)
491     return -1;
492   ps = session_get (peer_ct->c_s_index, peer_ct->c_thread_index);
493   return session_dequeue_notify (ps);
494 }
495
496 static u8 *
497 format_ct_listener (u8 * s, va_list * args)
498 {
499   u32 tc_index = va_arg (*args, u32);
500   u32 __clib_unused thread_index = va_arg (*args, u32);
501   u32 __clib_unused verbose = va_arg (*args, u32);
502   ct_connection_t *ct = ct_connection_get (tc_index);
503   s = format (s, "%-50U", format_ct_connection_id, ct);
504   if (verbose)
505     s = format (s, "%-15s", "LISTEN");
506   return s;
507 }
508
509 static u8 *
510 format_ct_connection (u8 * s, va_list * args)
511 {
512   ct_connection_t *ct = va_arg (*args, ct_connection_t *);
513   u32 verbose = va_arg (*args, u32);
514
515   if (!ct)
516     return s;
517   s = format (s, "%-50U", format_ct_connection_id, ct);
518   if (verbose)
519     {
520       s = format (s, "%-15s", "ESTABLISHED");
521       if (verbose > 1)
522         {
523           s = format (s, "\n");
524         }
525     }
526   return s;
527 }
528
529 static u8 *
530 format_ct_session (u8 * s, va_list * args)
531 {
532   u32 ct_index = va_arg (*args, u32);
533   u32 __clib_unused thread_index = va_arg (*args, u32);
534   u32 verbose = va_arg (*args, u32);
535   ct_connection_t *ct;
536
537   ct = ct_connection_get (ct_index);
538   if (!ct)
539     {
540       s = format (s, "empty\n");
541       return s;
542     }
543
544   s = format (s, "%U", format_ct_connection, ct, verbose);
545   return s;
546 }
547
548 /* *INDENT-OFF* */
549 static const transport_proto_vft_t cut_thru_proto = {
550   .start_listen = ct_start_listen,
551   .stop_listen = ct_stop_listen,
552   .get_listener = ct_listener_get,
553   .connect = ct_session_connect,
554   .close = ct_session_close,
555   .get_connection = ct_session_get,
556   .custom_tx = ct_custom_tx,
557   .app_rx_evt = ct_app_rx_evt,
558   .format_listener = format_ct_listener,
559   .format_connection = format_ct_session,
560   .transport_options = {
561     .name = "ct",
562     .short_name = "C",
563     .tx_type = TRANSPORT_TX_INTERNAL,
564     .service_type = TRANSPORT_SERVICE_APP,
565   },
566 };
567 /* *INDENT-ON* */
568
569 int
570 ct_session_tx (session_t * s)
571 {
572   ct_connection_t *ct, *peer_ct;
573   session_t *peer_s;
574
575   ct = (ct_connection_t *) session_get_transport (s);
576   peer_ct = ct_connection_get (ct->peer_index);
577   if (!peer_ct)
578     return -1;
579   peer_s = session_get (peer_ct->c_s_index, 0);
580   if (peer_s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
581     return 0;
582   return session_enqueue_notify (peer_s);
583 }
584
585 static clib_error_t *
586 ct_transport_init (vlib_main_t * vm)
587 {
588   transport_register_protocol (TRANSPORT_PROTO_NONE, &cut_thru_proto,
589                                FIB_PROTOCOL_IP4, ~0);
590   transport_register_protocol (TRANSPORT_PROTO_NONE, &cut_thru_proto,
591                                FIB_PROTOCOL_IP6, ~0);
592   return 0;
593 }
594
595 VLIB_INIT_FUNCTION (ct_transport_init);
596
597 /*
598  * fd.io coding-style-patch-verification: ON
599  *
600  * Local Variables:
601  * eval: (c-set-style "gnu")
602  * End:
603  */