session: use vpp to switch io events for ct sessions
[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   cs->t_app_index = client_wrk->app_index;
133
134   cct->c_s_index = cs->session_index;
135   cct->client_rx_fifo = ss->tx_fifo;
136   cct->client_tx_fifo = ss->rx_fifo;
137
138   cct->client_rx_fifo->refcnt++;
139   cct->client_tx_fifo->refcnt++;
140
141   /* This will allocate fifos for the session. They won't be used for
142    * exchanging data but they will be used to close the connection if
143    * the segment manager/worker is freed */
144   if (app_worker_init_connected (client_wrk, cs))
145     {
146       session_close (ss);
147       return -1;
148     }
149
150   if (app_worker_connect_notify (client_wrk, is_fail ? 0 : cs,
151                                  sct->client_opaque))
152     {
153       session_close (ss);
154       return -1;
155     }
156
157   cs = session_get (cct->c_s_index, 0);
158   cs->session_state = SESSION_STATE_READY;
159
160   return 0;
161 }
162
163 static int
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)
166 {
167   u32 round_rx_fifo_sz, round_tx_fifo_sz, sm_index, seg_size;
168   segment_manager_properties_t *props;
169   svm_fifo_segment_private_t *seg;
170   application_t *server;
171   segment_manager_t *sm;
172   u32 margin = 16 << 10;
173   u64 segment_handle;
174   int seg_index, rv;
175
176   server = application_get (server_wrk->app_index);
177
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   seg_size = round_rx_fifo_sz + round_tx_fifo_sz + margin;
182
183   sm = app_worker_get_listen_segment_manager (server_wrk, ll);
184   seg_index = segment_manager_add_segment (sm, seg_size);
185   if (seg_index < 0)
186     {
187       clib_warning ("failed to add new cut-through segment");
188       return seg_index;
189     }
190   seg = segment_manager_get_segment_w_lock (sm, seg_index);
191
192   rv = segment_manager_try_alloc_fifos (seg, props->rx_fifo_size,
193                                         props->tx_fifo_size, &ls->rx_fifo,
194                                         &ls->tx_fifo);
195   if (rv)
196     {
197       clib_warning ("failed to add fifos in cut-through segment");
198       segment_manager_segment_reader_unlock (sm);
199       goto failed;
200     }
201
202   sm_index = segment_manager_index (sm);
203   ls->rx_fifo->master_session_index = ls->session_index;
204   ls->tx_fifo->master_session_index = ls->session_index;
205   ls->rx_fifo->segment_manager = sm_index;
206   ls->tx_fifo->segment_manager = sm_index;
207   ls->rx_fifo->segment_index = seg_index;
208   ls->tx_fifo->segment_index = seg_index;
209
210   segment_handle = segment_manager_segment_handle (sm, seg);
211   if ((rv = app_worker_add_segment_notify (server_wrk, segment_handle)))
212     {
213       clib_warning ("failed to notify server of new segment");
214       segment_manager_segment_reader_unlock (sm);
215       goto failed;
216     }
217   segment_manager_segment_reader_unlock (sm);
218   ct->segment_handle = segment_handle;
219
220   return 0;
221
222 failed:
223   segment_manager_del_segment (sm, seg);
224   return rv;
225 }
226
227 static int
228 ct_connect (app_worker_t * client_wrk, session_t * ll,
229             session_endpoint_cfg_t * sep)
230 {
231   u32 cct_index, ll_index, ll_ct_index;
232   ct_connection_t *sct, *cct, *ll_ct;
233   app_worker_t *server_wrk;
234   session_t *ss;
235
236   ll_index = ll->session_index;
237   ll_ct_index = ll->connection_index;
238
239   cct = ct_connection_alloc ();
240   cct_index = cct->c_c_index;
241   sct = ct_connection_alloc ();
242   ll_ct = ct_connection_get (ll_ct_index);
243
244   /*
245    * Alloc and init client transport
246    */
247   cct = ct_connection_get (cct_index);
248   cct->c_thread_index = 0;
249   cct->c_rmt_port = sep->port;
250   cct->c_lcl_port = 0;
251   cct->c_is_ip4 = sep->is_ip4;
252   clib_memcpy (&cct->c_rmt_ip, &sep->ip, sizeof (sep->ip));
253   cct->actual_tp = ll_ct->actual_tp;
254   cct->is_client = 1;
255
256   /*
257    * Init server transport
258    */
259   sct->c_thread_index = 0;
260   sct->c_rmt_port = 0;
261   sct->c_lcl_port = ll_ct->c_lcl_port;
262   sct->c_is_ip4 = sep->is_ip4;
263   clib_memcpy (&sct->c_lcl_ip, &ll_ct->c_lcl_ip, sizeof (ll_ct->c_lcl_ip));
264   sct->client_wrk = client_wrk->wrk_index;
265   sct->c_proto = TRANSPORT_PROTO_NONE;
266   sct->client_opaque = sep->opaque;
267   sct->actual_tp = ll_ct->actual_tp;
268
269   sct->peer_index = cct->c_c_index;
270   cct->peer_index = sct->c_c_index;
271
272   /*
273    * Accept server session. Client session is created only after
274    * server confirms accept.
275    */
276   ss = session_alloc (0);
277   ll = listen_session_get (ll_index);
278   ss->session_type = ll->session_type;
279   ss->connection_index = sct->c_c_index;
280   ss->listener_index = ll->session_index;
281   ss->session_state = SESSION_STATE_CREATED;
282
283   server_wrk = application_listener_select_worker (ll);
284   ss->app_wrk_index = server_wrk->wrk_index;
285   ss->t_app_index = server_wrk->app_index;
286
287   sct->c_s_index = ss->session_index;
288   sct->server_wrk = ss->app_wrk_index;
289
290   if (ct_init_local_session (client_wrk, server_wrk, sct, ss, ll))
291     {
292       clib_warning ("failed");
293       ct_connection_free (sct);
294       session_free (ss);
295       return -1;
296     }
297
298   ss->session_state = SESSION_STATE_ACCEPTING;
299   if (app_worker_accept_notify (server_wrk, ss))
300     {
301       clib_warning ("failed");
302       ct_connection_free (sct);
303       session_free_w_fifos (ss);
304       return -1;
305     }
306
307   cct->segment_handle = sct->segment_handle;
308   ct_enable_disable_main_pre_input_node (1 /* is_add */ );
309   return 0;
310 }
311
312 static u32
313 ct_start_listen (u32 app_listener_index, transport_endpoint_t * tep)
314 {
315   session_endpoint_cfg_t *sep;
316   ct_connection_t *ct;
317
318   sep = (session_endpoint_cfg_t *) tep;
319   ct = ct_connection_alloc ();
320   ct->server_wrk = sep->app_wrk_index;
321   ct->c_is_ip4 = sep->is_ip4;
322   clib_memcpy (&ct->c_lcl_ip, &sep->ip, sizeof (sep->ip));
323   ct->c_lcl_port = sep->port;
324   ct->actual_tp = sep->transport_proto;
325   ct_enable_disable_main_pre_input_node (1 /* is_add */ );
326   return ct->c_c_index;
327 }
328
329 static u32
330 ct_stop_listen (u32 ct_index)
331 {
332   ct_connection_t *ct;
333   ct = ct_connection_get (ct_index);
334   ct_connection_free (ct);
335   ct_enable_disable_main_pre_input_node (0 /* is_add */ );
336   return 0;
337 }
338
339 static transport_connection_t *
340 ct_listener_get (u32 ct_index)
341 {
342   return (transport_connection_t *) ct_connection_get (ct_index);
343 }
344
345 static int
346 ct_session_connect (transport_endpoint_cfg_t * tep)
347 {
348   session_endpoint_cfg_t *sep_ext;
349   session_endpoint_t *sep;
350   app_worker_t *app_wrk;
351   session_handle_t lh;
352   application_t *app;
353   app_listener_t *al;
354   u32 table_index;
355   session_t *ll;
356   u8 fib_proto;
357
358   sep_ext = (session_endpoint_cfg_t *) tep;
359   sep = (session_endpoint_t *) tep;
360   app_wrk = app_worker_get (sep_ext->app_wrk_index);
361   app = application_get (app_wrk->app_index);
362
363   sep->transport_proto = sep_ext->original_tp;
364   table_index = application_local_session_table (app);
365   lh = session_lookup_local_endpoint (table_index, sep);
366   if (lh == SESSION_DROP_HANDLE)
367     return VNET_API_ERROR_APP_CONNECT_FILTERED;
368
369   if (lh == SESSION_INVALID_HANDLE)
370     goto global_scope;
371
372   ll = listen_session_get_from_handle (lh);
373   al = app_listener_get_w_session (ll);
374
375   /*
376    * Break loop if rule in local table points to connecting app. This
377    * can happen if client is a generic proxy. Route connect through
378    * global table instead.
379    */
380   if (al->app_index == app->app_index)
381     goto global_scope;
382
383   return ct_connect (app_wrk, ll, sep_ext);
384
385   /*
386    * If nothing found, check the global scope for locally attached
387    * destinations. Make sure first that we're allowed to.
388    */
389
390 global_scope:
391   if (session_endpoint_is_local (sep))
392     return VNET_API_ERROR_SESSION_CONNECT;
393
394   if (!application_has_global_scope (app))
395     return VNET_API_ERROR_APP_CONNECT_SCOPE;
396
397   fib_proto = session_endpoint_fib_proto (sep);
398   table_index = application_session_table (app, fib_proto);
399   ll = session_lookup_listener (table_index, sep);
400
401   if (ll)
402     return ct_connect (app_wrk, ll, sep_ext);
403
404   /* Failed to connect but no error */
405   return 1;
406 }
407
408 static void
409 ct_session_close (u32 ct_index, u32 thread_index)
410 {
411   ct_connection_t *ct, *peer_ct;
412   app_worker_t *app_wrk;
413   session_t *s;
414
415   ct = ct_connection_get (ct_index);
416   peer_ct = ct_connection_get (ct->peer_index);
417   if (peer_ct)
418     {
419       peer_ct->peer_index = ~0;
420       session_transport_closing_notify (&peer_ct->connection);
421     }
422
423   s = session_get (ct->c_s_index, 0);
424   app_wrk = app_worker_get_if_valid (s->app_wrk_index);
425   if (app_wrk)
426     app_worker_del_segment_notify (app_wrk, ct->segment_handle);
427   session_free_w_fifos (s);
428   if (ct->is_client)
429     segment_manager_dealloc_fifos (ct->client_rx_fifo, ct->client_tx_fifo);
430
431   ct_connection_free (ct);
432   ct_enable_disable_main_pre_input_node (0 /* is_add */ );
433 }
434
435 static transport_connection_t *
436 ct_session_get (u32 ct_index, u32 thread_index)
437 {
438   return (transport_connection_t *) ct_connection_get (ct_index);
439 }
440
441 static u8 *
442 format_ct_connection_id (u8 * s, va_list * args)
443 {
444   ct_connection_t *ct = va_arg (*args, ct_connection_t *);
445   if (!ct)
446     return s;
447   if (ct->c_is_ip4)
448     {
449       s = format (s, "[%d:%d][CT:%U] %U:%d->%U:%d", ct->c_thread_index,
450                   ct->c_s_index, format_transport_proto_short, ct->actual_tp,
451                   format_ip4_address, &ct->c_lcl_ip4,
452                   clib_net_to_host_u16 (ct->c_lcl_port), format_ip4_address,
453                   &ct->c_rmt_ip4, clib_net_to_host_u16 (ct->c_rmt_port));
454     }
455   else
456     {
457       s = format (s, "[%d:%d][CT:%U] %U:%d->%U:%d", ct->c_thread_index,
458                   ct->c_s_index, format_transport_proto_short, ct->actual_tp,
459                   format_ip6_address, &ct->c_lcl_ip6,
460                   clib_net_to_host_u16 (ct->c_lcl_port), format_ip6_address,
461                   &ct->c_rmt_ip6, clib_net_to_host_u16 (ct->c_rmt_port));
462     }
463
464   return s;
465 }
466
467 static u8 *
468 format_ct_listener (u8 * s, va_list * args)
469 {
470   u32 tc_index = va_arg (*args, u32);
471   u32 __clib_unused verbose = va_arg (*args, u32);
472   ct_connection_t *ct = ct_connection_get (tc_index);
473   s = format (s, "%-50U", format_ct_connection_id, ct);
474   if (verbose)
475     s = format (s, "%-15s", "LISTEN");
476   return s;
477 }
478
479 static u8 *
480 format_ct_connection (u8 * s, va_list * args)
481 {
482   ct_connection_t *ct = va_arg (*args, ct_connection_t *);
483   u32 verbose = va_arg (*args, u32);
484
485   if (!ct)
486     return s;
487   s = format (s, "%-50U", format_ct_connection_id, ct);
488   if (verbose)
489     {
490       s = format (s, "%-15s", "ESTABLISHED");
491       if (verbose > 1)
492         {
493           s = format (s, "\n");
494         }
495     }
496   return s;
497 }
498
499 static u8 *
500 format_ct_session (u8 * s, va_list * args)
501 {
502   u32 ct_index = va_arg (*args, u32);
503   u32 __clib_unused thread_index = va_arg (*args, u32);
504   u32 verbose = va_arg (*args, u32);
505   ct_connection_t *ct;
506
507   ct = ct_connection_get (ct_index);
508   if (!ct)
509     {
510       s = format (s, "empty\n");
511       return s;
512     }
513
514   s = format (s, "%U", format_ct_connection, ct, verbose);
515   return s;
516 }
517
518 /* *INDENT-OFF* */
519 const static transport_proto_vft_t cut_thru_proto = {
520   .start_listen = ct_start_listen,
521   .stop_listen = ct_stop_listen,
522   .get_listener = ct_listener_get,
523   .connect = ct_session_connect,
524   .close = ct_session_close,
525   .get_connection = ct_session_get,
526   .tx_type = TRANSPORT_TX_INTERNAL,
527   .service_type = TRANSPORT_SERVICE_APP,
528   .format_listener = format_ct_listener,
529   .format_connection = format_ct_session,
530 };
531 /* *INDENT-ON* */
532
533 int
534 ct_session_tx (session_t * s)
535 {
536   ct_connection_t *ct, *peer_ct;
537   session_t *peer_s;
538
539   ct = (ct_connection_t *) session_get_transport (s);
540   peer_ct = ct_connection_get (ct->peer_index);
541   if (!peer_ct)
542     return -1;
543   peer_s = session_get (peer_ct->c_s_index, 0);
544   if (peer_s->session_state >= SESSION_STATE_TRANSPORT_CLOSING)
545     return 0;
546   return session_enqueue_notify (peer_s);
547 }
548
549 static clib_error_t *
550 ct_transport_init (vlib_main_t * vm)
551 {
552   transport_register_protocol (TRANSPORT_PROTO_NONE, &cut_thru_proto,
553                                FIB_PROTOCOL_IP4, ~0);
554   transport_register_protocol (TRANSPORT_PROTO_NONE, &cut_thru_proto,
555                                FIB_PROTOCOL_IP6, ~0);
556   return 0;
557 }
558
559 VLIB_INIT_FUNCTION (ct_transport_init);
560
561 /*
562  * fd.io coding-style-patch-verification: ON
563  *
564  * Local Variables:
565  * eval: (c-set-style "gnu")
566  * End:
567  */