session/tls: remove unused t_app_index field
[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   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
40 static ct_connection_t *
41 ct_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
53 static ct_connection_t *
54 ct_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);
59 }
60
61 static void
62 ct_connection_free (ct_connection_t * ct)
63 {
64   if (CLIB_DEBUG)
65     memset (ct, 0xfc, sizeof (*ct));
66   pool_put (connections, ct);
67 }
68
69 session_t *
70 ct_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
78 void
79 ct_session_endpoint (session_t * ll, session_endpoint_t * sep)
80 {
81   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;
86 }
87
88 int
89 ct_session_connect_notify (session_t * ss)
90 {
91   svm_fifo_segment_private_t *seg;
92   ct_connection_t *sct, *cct;
93   app_worker_t *client_wrk;
94   segment_manager_t *sm;
95   u64 segment_handle;
96   int is_fail = 0;
97   session_t *cs;
98   u32 ss_index;
99
100   ss_index = ss->session_index;
101   sct = (ct_connection_t *) session_get_transport (ss);
102   client_wrk = app_worker_get (sct->client_wrk);
103
104   sm = segment_manager_get (ss->rx_fifo->segment_manager);
105   seg = segment_manager_get_segment_w_lock (sm, ss->rx_fifo->segment_index);
106   segment_handle = segment_manager_segment_handle (sm, seg);
107
108   if (app_worker_add_segment_notify (client_wrk, segment_handle))
109     {
110       clib_warning ("failed to notify client %u of new segment",
111                     sct->client_wrk);
112       segment_manager_segment_reader_unlock (sm);
113       session_close (ss);
114       is_fail = 1;
115     }
116   else
117     {
118       segment_manager_segment_reader_unlock (sm);
119     }
120
121   /* Alloc client session */
122   cct = ct_connection_get (sct->peer_index);
123
124   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;
134   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++;
139
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
159   return 0;
160 }
161
162 static int
163 ct_init_local_session (app_worker_t * client_wrk, app_worker_t * server_wrk,
164                        ct_connection_t * ct, session_t * ls, session_t * ll)
165 {
166   u32 round_rx_fifo_sz, round_tx_fifo_sz, sm_index, seg_size;
167   segment_manager_properties_t *props;
168   svm_fifo_segment_private_t *seg;
169   application_t *server;
170   segment_manager_t *sm;
171   u32 margin = 16 << 10;
172   u64 segment_handle;
173   int seg_index, rv;
174
175   server = application_get (server_wrk->app_index);
176
177   props = application_segment_manager_properties (server);
178   round_rx_fifo_sz = 1 << max_log2 (props->rx_fifo_size);
179   round_tx_fifo_sz = 1 << max_log2 (props->tx_fifo_size);
180   seg_size = round_rx_fifo_sz + round_tx_fifo_sz + margin;
181
182   sm = app_worker_get_listen_segment_manager (server_wrk, ll);
183   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);
190
191   rv = segment_manager_try_alloc_fifos (seg, props->rx_fifo_size,
192                                         props->tx_fifo_size, &ls->rx_fifo,
193                                         &ls->tx_fifo);
194   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     }
200
201   sm_index = segment_manager_index (sm);
202   ls->rx_fifo->master_session_index = ls->session_index;
203   ls->tx_fifo->master_session_index = ls->session_index;
204   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;
208
209   segment_handle = segment_manager_segment_handle (sm, seg);
210   if ((rv = app_worker_add_segment_notify (server_wrk, segment_handle)))
211     {
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);
217   ct->segment_handle = segment_handle;
218
219   return 0;
220
221 failed:
222   segment_manager_del_segment (sm, seg);
223   return rv;
224 }
225
226 static int
227 ct_connect (app_worker_t * client_wrk, session_t * ll,
228             session_endpoint_cfg_t * sep)
229 {
230   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;
234
235   ll_index = ll->session_index;
236   ll_ct_index = ll->connection_index;
237
238   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);
242
243   /*
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;
253   cct->is_client = 1;
254
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);
277   ss->session_type = ll->session_type;
278   ss->connection_index = sct->c_c_index;
279   ss->listener_index = ll->session_index;
280   ss->session_state = SESSION_STATE_CREATED;
281
282   server_wrk = application_listener_select_worker (ll);
283   ss->app_wrk_index = server_wrk->wrk_index;
284
285   sct->c_s_index = ss->session_index;
286   sct->server_wrk = ss->app_wrk_index;
287
288   if (ct_init_local_session (client_wrk, server_wrk, sct, ss, ll))
289     {
290       clib_warning ("failed");
291       ct_connection_free (sct);
292       session_free (ss);
293       return -1;
294     }
295
296   ss->session_state = SESSION_STATE_ACCEPTING;
297   if (app_worker_accept_notify (server_wrk, ss))
298     {
299       clib_warning ("failed");
300       ct_connection_free (sct);
301       session_free_w_fifos (ss);
302       return -1;
303     }
304
305   cct->segment_handle = sct->segment_handle;
306   ct_enable_disable_main_pre_input_node (1 /* is_add */ );
307   return 0;
308 }
309
310 static u32
311 ct_start_listen (u32 app_listener_index, transport_endpoint_t * tep)
312 {
313   session_endpoint_cfg_t *sep;
314   ct_connection_t *ct;
315
316   sep = (session_endpoint_cfg_t *) tep;
317   ct = ct_connection_alloc ();
318   ct->server_wrk = sep->app_wrk_index;
319   ct->c_is_ip4 = sep->is_ip4;
320   clib_memcpy (&ct->c_lcl_ip, &sep->ip, sizeof (sep->ip));
321   ct->c_lcl_port = sep->port;
322   ct->actual_tp = sep->transport_proto;
323   ct_enable_disable_main_pre_input_node (1 /* is_add */ );
324   return ct->c_c_index;
325 }
326
327 static u32
328 ct_stop_listen (u32 ct_index)
329 {
330   ct_connection_t *ct;
331   ct = ct_connection_get (ct_index);
332   ct_connection_free (ct);
333   ct_enable_disable_main_pre_input_node (0 /* is_add */ );
334   return 0;
335 }
336
337 static transport_connection_t *
338 ct_listener_get (u32 ct_index)
339 {
340   return (transport_connection_t *) ct_connection_get (ct_index);
341 }
342
343 static int
344 ct_session_connect (transport_endpoint_cfg_t * tep)
345 {
346   session_endpoint_cfg_t *sep_ext;
347   session_endpoint_t *sep;
348   app_worker_t *app_wrk;
349   session_handle_t lh;
350   application_t *app;
351   app_listener_t *al;
352   u32 table_index;
353   session_t *ll;
354   u8 fib_proto;
355
356   sep_ext = (session_endpoint_cfg_t *) tep;
357   sep = (session_endpoint_t *) tep;
358   app_wrk = app_worker_get (sep_ext->app_wrk_index);
359   app = application_get (app_wrk->app_index);
360
361   sep->transport_proto = sep_ext->original_tp;
362   table_index = application_local_session_table (app);
363   lh = session_lookup_local_endpoint (table_index, sep);
364   if (lh == SESSION_DROP_HANDLE)
365     return VNET_API_ERROR_APP_CONNECT_FILTERED;
366
367   if (lh == SESSION_INVALID_HANDLE)
368     goto global_scope;
369
370   ll = listen_session_get_from_handle (lh);
371   al = app_listener_get_w_session (ll);
372
373   /*
374    * Break loop if rule in local table points to connecting app. This
375    * can happen if client is a generic proxy. Route connect through
376    * global table instead.
377    */
378   if (al->app_index == app->app_index)
379     goto global_scope;
380
381   return ct_connect (app_wrk, ll, sep_ext);
382
383   /*
384    * If nothing found, check the global scope for locally attached
385    * destinations. Make sure first that we're allowed to.
386    */
387
388 global_scope:
389   if (session_endpoint_is_local (sep))
390     return VNET_API_ERROR_SESSION_CONNECT;
391
392   if (!application_has_global_scope (app))
393     return VNET_API_ERROR_APP_CONNECT_SCOPE;
394
395   fib_proto = session_endpoint_fib_proto (sep);
396   table_index = application_session_table (app, fib_proto);
397   ll = session_lookup_listener (table_index, sep);
398
399   if (ll)
400     return ct_connect (app_wrk, ll, sep_ext);
401
402   /* Failed to connect but no error */
403   return 1;
404 }
405
406 static void
407 ct_session_close (u32 ct_index, u32 thread_index)
408 {
409   ct_connection_t *ct, *peer_ct;
410   app_worker_t *app_wrk;
411   session_t *s;
412
413   ct = ct_connection_get (ct_index);
414   peer_ct = ct_connection_get (ct->peer_index);
415   if (peer_ct)
416     {
417       peer_ct->peer_index = ~0;
418       session_transport_closing_notify (&peer_ct->connection);
419     }
420
421   s = session_get (ct->c_s_index, 0);
422   app_wrk = app_worker_get_if_valid (s->app_wrk_index);
423   if (app_wrk)
424     app_worker_del_segment_notify (app_wrk, ct->segment_handle);
425   session_free_w_fifos (s);
426   if (ct->is_client)
427     segment_manager_dealloc_fifos (ct->client_rx_fifo, ct->client_tx_fifo);
428
429   ct_connection_free (ct);
430   ct_enable_disable_main_pre_input_node (0 /* is_add */ );
431 }
432
433 static transport_connection_t *
434 ct_session_get (u32 ct_index, u32 thread_index)
435 {
436   return (transport_connection_t *) ct_connection_get (ct_index);
437 }
438
439 static u8 *
440 format_ct_connection_id (u8 * s, va_list * args)
441 {
442   ct_connection_t *ct = va_arg (*args, ct_connection_t *);
443   if (!ct)
444     return s;
445   if (ct->c_is_ip4)
446     {
447       s = format (s, "[%d:%d][CT:%U] %U:%d->%U:%d", ct->c_thread_index,
448                   ct->c_s_index, format_transport_proto_short, ct->actual_tp,
449                   format_ip4_address, &ct->c_lcl_ip4,
450                   clib_net_to_host_u16 (ct->c_lcl_port), format_ip4_address,
451                   &ct->c_rmt_ip4, clib_net_to_host_u16 (ct->c_rmt_port));
452     }
453   else
454     {
455       s = format (s, "[%d:%d][CT:%U] %U:%d->%U:%d", ct->c_thread_index,
456                   ct->c_s_index, format_transport_proto_short, ct->actual_tp,
457                   format_ip6_address, &ct->c_lcl_ip6,
458                   clib_net_to_host_u16 (ct->c_lcl_port), format_ip6_address,
459                   &ct->c_rmt_ip6, clib_net_to_host_u16 (ct->c_rmt_port));
460     }
461
462   return s;
463 }
464
465 static int
466 ct_custom_tx (void *session)
467 {
468   session_t *s = (session_t *) session;
469   if (session_has_transport (s))
470     return 0;
471   return ct_session_tx (s);
472 }
473
474 static u8 *
475 format_ct_listener (u8 * s, va_list * args)
476 {
477   u32 tc_index = va_arg (*args, u32);
478   u32 __clib_unused verbose = va_arg (*args, u32);
479   ct_connection_t *ct = ct_connection_get (tc_index);
480   s = format (s, "%-50U", format_ct_connection_id, ct);
481   if (verbose)
482     s = format (s, "%-15s", "LISTEN");
483   return s;
484 }
485
486 static u8 *
487 format_ct_connection (u8 * s, va_list * args)
488 {
489   ct_connection_t *ct = va_arg (*args, ct_connection_t *);
490   u32 verbose = va_arg (*args, u32);
491
492   if (!ct)
493     return s;
494   s = format (s, "%-50U", format_ct_connection_id, ct);
495   if (verbose)
496     {
497       s = format (s, "%-15s", "ESTABLISHED");
498       if (verbose > 1)
499         {
500           s = format (s, "\n");
501         }
502     }
503   return s;
504 }
505
506 static u8 *
507 format_ct_session (u8 * s, va_list * args)
508 {
509   u32 ct_index = va_arg (*args, u32);
510   u32 __clib_unused thread_index = va_arg (*args, u32);
511   u32 verbose = va_arg (*args, u32);
512   ct_connection_t *ct;
513
514   ct = ct_connection_get (ct_index);
515   if (!ct)
516     {
517       s = format (s, "empty\n");
518       return s;
519     }
520
521   s = format (s, "%U", format_ct_connection, ct, verbose);
522   return s;
523 }
524
525 /* *INDENT-OFF* */
526 const static transport_proto_vft_t cut_thru_proto = {
527   .start_listen = ct_start_listen,
528   .stop_listen = ct_stop_listen,
529   .get_listener = ct_listener_get,
530   .connect = ct_session_connect,
531   .close = ct_session_close,
532   .get_connection = ct_session_get,
533   .custom_tx = ct_custom_tx,
534   .tx_type = TRANSPORT_TX_INTERNAL,
535   .service_type = TRANSPORT_SERVICE_APP,
536   .format_listener = format_ct_listener,
537   .format_connection = format_ct_session,
538 };
539 /* *INDENT-ON* */
540
541 int
542 ct_session_tx (session_t * s)
543 {
544   ct_connection_t *ct, *peer_ct;
545   session_t *peer_s;
546
547   ct = (ct_connection_t *) session_get_transport (s);
548   peer_ct = ct_connection_get (ct->peer_index);
549   if (!peer_ct)
550     return -1;
551   peer_s = session_get (peer_ct->c_s_index, 0);
552   if (peer_s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
553     return 0;
554   return session_enqueue_notify (peer_s);
555 }
556
557 static clib_error_t *
558 ct_transport_init (vlib_main_t * vm)
559 {
560   transport_register_protocol (TRANSPORT_PROTO_NONE, &cut_thru_proto,
561                                FIB_PROTOCOL_IP4, ~0);
562   transport_register_protocol (TRANSPORT_PROTO_NONE, &cut_thru_proto,
563                                FIB_PROTOCOL_IP6, ~0);
564   return 0;
565 }
566
567 VLIB_INIT_FUNCTION (ct_transport_init);
568
569 /*
570  * fd.io coding-style-patch-verification: ON
571  *
572  * Local Variables:
573  * eval: (c-set-style "gnu")
574  * End:
575  */