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