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