session: refactor local connects
[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 ct_connection_t *connections;
20
21 ct_connection_t *
22 ct_connection_alloc (void)
23 {
24   ct_connection_t *ct;
25
26   pool_get_zero (connections, ct);
27   ct->c_c_index = ct - connections;
28   ct->c_thread_index = 0;
29   ct->client_wrk = ~0;
30   ct->server_wrk = ~0;
31   return ct;
32 }
33
34 ct_connection_t *
35 ct_connection_get (u32 ct_index)
36 {
37   if (pool_is_free_index (connections, ct_index))
38     return 0;
39   return pool_elt_at_index (connections, ct_index);
40 }
41
42 void
43 ct_connection_free (ct_connection_t * ct)
44 {
45   if (CLIB_DEBUG)
46     memset (ct, 0xfc, sizeof (*ct));
47   pool_put (connections, ct);
48 }
49
50 session_t *
51 ct_session_get_peer (session_t * s)
52 {
53   ct_connection_t *ct, *peer_ct;
54   ct = ct_connection_get (s->connection_index);
55   peer_ct = ct_connection_get (ct->peer_index);
56   return session_get (peer_ct->c_s_index, 0);
57 }
58
59 void
60 ct_session_endpoint (session_t * ll, session_endpoint_t * sep)
61 {
62   ct_connection_t *ct;
63   ct = (ct_connection_t *) session_get_transport (ll);
64   sep->transport_proto = ct->actual_tp;
65   sep->port = ct->c_lcl_port;
66   sep->is_ip4 = ct->c_is_ip4;
67 }
68
69 int
70 ct_session_connect_notify (session_t * ss)
71 {
72   svm_fifo_segment_private_t *seg;
73   ct_connection_t *sct, *cct;
74   app_worker_t *client_wrk;
75   segment_manager_t *sm;
76   u64 segment_handle;
77   int is_fail = 0;
78   session_t *cs;
79   u32 ss_index;
80
81   ss_index = ss->session_index;
82   sct = (ct_connection_t *) session_get_transport (ss);
83   client_wrk = app_worker_get (sct->client_wrk);
84
85   sm = segment_manager_get (ss->rx_fifo->segment_manager);
86   seg = segment_manager_get_segment_w_lock (sm, ss->rx_fifo->segment_index);
87   segment_handle = segment_manager_segment_handle (sm, seg);
88
89   if (app_worker_add_segment_notify (client_wrk, segment_handle))
90     {
91       clib_warning ("failed to notify client %u of new segment",
92                     sct->client_wrk);
93       segment_manager_segment_reader_unlock (sm);
94       session_close (ss);
95       is_fail = 1;
96     }
97   else
98     {
99       segment_manager_segment_reader_unlock (sm);
100     }
101
102   /* Alloc client session */
103   cct = ct_connection_get (sct->peer_index);
104
105   cs = session_alloc (0);
106   ss = session_get (ss_index, 0);
107   cs->session_type = ss->session_type;
108   cs->connection_index = sct->c_c_index;
109   cs->listener_index = SESSION_INVALID_INDEX;
110   cs->session_state = SESSION_STATE_CONNECTING;
111   cs->app_wrk_index = client_wrk->wrk_index;
112   cs->connection_index = cct->c_c_index;
113
114   cct->c_s_index = cs->session_index;
115
116   /* This will allocate fifos for the session. They won't be used for
117    * exchanging data but they will be used to close the connection if
118    * the segment manager/worker is freed */
119   if (app_worker_init_connected (client_wrk, cs))
120     {
121       session_close (ss);
122       return -1;
123     }
124
125   if (app_worker_connect_notify (client_wrk, is_fail ? 0 : cs,
126                                  sct->client_opaque))
127     {
128       session_close (ss);
129       return -1;
130     }
131
132   cs = session_get (cct->c_s_index, 0);
133   cs->session_state = SESSION_STATE_READY;
134
135   return 0;
136 }
137
138 static void
139 ct_session_fix_eventds (svm_msg_q_t * sq, svm_msg_q_t * cq)
140 {
141   int fd;
142
143   /*
144    * segment manager initializes only the producer eventds, since vpp is
145    * typically the producer. But for local sessions, we also pass to the
146    * apps the mqs they listen on for events from peer apps, so they are also
147    * consumer fds.
148    */
149   fd = svm_msg_q_get_producer_eventfd (sq);
150   svm_msg_q_set_consumer_eventfd (sq, fd);
151   fd = svm_msg_q_get_producer_eventfd (cq);
152   svm_msg_q_set_consumer_eventfd (cq, fd);
153 }
154
155 int
156 ct_init_local_session (app_worker_t * client_wrk, app_worker_t * server_wrk,
157                        ct_connection_t * ct, session_t * ls, session_t * ll)
158 {
159   u32 seg_size, evt_q_sz, evt_q_elts, margin = 16 << 10;
160   u32 round_rx_fifo_sz, round_tx_fifo_sz, sm_index;
161   segment_manager_properties_t *props, *cprops;
162   svm_fifo_segment_private_t *seg;
163   application_t *server, *client;
164   segment_manager_t *sm;
165   svm_msg_q_t *sq, *cq;
166   u64 segment_handle;
167   int seg_index, rv;
168
169   server = application_get (server_wrk->app_index);
170   client = application_get (client_wrk->app_index);
171
172   props = application_segment_manager_properties (server);
173   cprops = application_segment_manager_properties (client);
174   evt_q_elts = props->evt_q_size + cprops->evt_q_size;
175   evt_q_sz = segment_manager_evt_q_expected_size (evt_q_elts);
176   round_rx_fifo_sz = 1 << max_log2 (props->rx_fifo_size);
177   round_tx_fifo_sz = 1 << max_log2 (props->tx_fifo_size);
178   seg_size = round_rx_fifo_sz + round_tx_fifo_sz + evt_q_sz + margin;
179
180   sm = app_worker_get_listen_segment_manager (server_wrk, ll);
181   seg_index = segment_manager_add_segment (sm, seg_size);
182   if (seg_index < 0)
183     {
184       clib_warning ("failed to add new cut-through segment");
185       return seg_index;
186     }
187   seg = segment_manager_get_segment_w_lock (sm, seg_index);
188   sq = segment_manager_alloc_queue (seg, props);
189   cq = segment_manager_alloc_queue (seg, cprops);
190
191   if (props->use_mq_eventfd)
192     ct_session_fix_eventds (sq, cq);
193
194   ct->server_evt_q = pointer_to_uword (sq);
195   ct->client_evt_q = pointer_to_uword (cq);
196   rv = segment_manager_try_alloc_fifos (seg, props->rx_fifo_size,
197                                         props->tx_fifo_size, &ls->rx_fifo,
198                                         &ls->tx_fifo);
199   if (rv)
200     {
201       clib_warning ("failed to add fifos in cut-through segment");
202       segment_manager_segment_reader_unlock (sm);
203       goto failed;
204     }
205
206   sm_index = segment_manager_index (sm);
207   ls->rx_fifo->ct_session_index = ls->session_index;
208   ls->tx_fifo->ct_session_index = ls->session_index;
209   ls->rx_fifo->segment_manager = sm_index;
210   ls->tx_fifo->segment_manager = sm_index;
211   ls->rx_fifo->segment_index = seg_index;
212   ls->tx_fifo->segment_index = seg_index;
213   ls->svm_segment_index = seg_index;
214
215   segment_handle = segment_manager_segment_handle (sm, seg);
216   if ((rv = app_worker_add_segment_notify (server_wrk, segment_handle)))
217     {
218       clib_warning ("failed to notify server of new segment");
219       segment_manager_segment_reader_unlock (sm);
220       goto failed;
221     }
222   segment_manager_segment_reader_unlock (sm);
223   ct->segment_handle = segment_handle;
224
225   return 0;
226
227 failed:
228   segment_manager_del_segment (sm, seg);
229   return rv;
230 }
231
232 int
233 ct_connect (app_worker_t * client_wrk, session_t * ll,
234             session_endpoint_cfg_t * sep)
235 {
236   u32 cct_index, ll_index, ll_ct_index;
237   ct_connection_t *sct, *cct, *ll_ct;
238   app_worker_t *server_wrk;
239   session_t *ss;
240
241   ll_index = ll->session_index;
242   ll_ct_index = ll->connection_index;
243
244   cct = ct_connection_alloc ();
245   cct_index = cct->c_c_index;
246   sct = ct_connection_alloc ();
247   ll_ct = ct_connection_get (ll_ct_index);
248
249   /*
250    * Alloc and init client transport
251    */
252   cct = ct_connection_get (cct_index);
253   cct->c_thread_index = 0;
254   cct->c_rmt_port = sep->port;
255   cct->c_lcl_port = 0;
256   cct->c_is_ip4 = sep->is_ip4;
257   clib_memcpy (&cct->c_rmt_ip, &sep->ip, sizeof (sep->ip));
258   cct->actual_tp = ll_ct->actual_tp;
259
260   /*
261    * Init server transport
262    */
263   sct->c_thread_index = 0;
264   sct->c_rmt_port = 0;
265   sct->c_lcl_port = ll_ct->c_lcl_port;
266   sct->c_is_ip4 = sep->is_ip4;
267   clib_memcpy (&sct->c_lcl_ip, &ll_ct->c_lcl_ip, sizeof (ll_ct->c_lcl_ip));
268   sct->client_wrk = client_wrk->wrk_index;
269   sct->c_proto = TRANSPORT_PROTO_NONE;
270   sct->client_opaque = sep->opaque;
271   sct->actual_tp = ll_ct->actual_tp;
272
273   sct->peer_index = cct->c_c_index;
274   cct->peer_index = sct->c_c_index;
275
276   /*
277    * Accept server session. Client session is created only after
278    * server confirms accept.
279    */
280   ss = session_alloc (0);
281   ll = listen_session_get (ll_index);
282   ss->session_type = ll->session_type;
283   ss->connection_index = sct->c_c_index;
284   ss->listener_index = ll->session_index;
285   ss->session_state = SESSION_STATE_CREATED;
286
287   server_wrk = application_listener_select_worker (ll);
288   ss->app_wrk_index = server_wrk->wrk_index;
289
290   sct->c_s_index = ss->session_index;
291   sct->server_wrk = ss->app_wrk_index;
292
293   if (ct_init_local_session (client_wrk, server_wrk, sct, ss, ll))
294     {
295       clib_warning ("failed");
296       ct_connection_free (sct);
297       session_free (ss);
298       return -1;
299     }
300
301   ss->session_state = SESSION_STATE_ACCEPTING;
302   if (app_worker_accept_notify (server_wrk, ss))
303     {
304       clib_warning ("failed");
305       ct_connection_free (sct);
306       session_free_w_fifos (ss);
307       return -1;
308     }
309
310   cct->client_evt_q = sct->client_evt_q;
311   cct->server_evt_q = sct->server_evt_q;
312   cct->segment_handle = sct->segment_handle;
313
314   return 0;
315 }
316
317 u32
318 ct_start_listen (u32 app_listener_index, transport_endpoint_t * tep)
319 {
320   session_endpoint_cfg_t *sep;
321   ct_connection_t *ct;
322
323   sep = (session_endpoint_cfg_t *) tep;
324   ct = ct_connection_alloc ();
325   ct->server_wrk = sep->app_wrk_index;
326   ct->c_is_ip4 = sep->is_ip4;
327   clib_memcpy (&ct->c_lcl_ip, &sep->ip, sizeof (sep->ip));
328   ct->c_lcl_port = sep->port;
329   ct->actual_tp = sep->transport_proto;
330   return ct->c_c_index;
331 }
332
333 u32
334 ct_stop_listen (u32 ct_index)
335 {
336   ct_connection_t *ct;
337   ct = ct_connection_get (ct_index);
338   ct_connection_free (ct);
339   return 0;
340 }
341
342 transport_connection_t *
343 ct_listener_get (u32 ct_index)
344 {
345   return (transport_connection_t *) ct_connection_get (ct_index);
346 }
347
348 int
349 ct_session_connect (transport_endpoint_cfg_t * tep)
350 {
351   session_endpoint_cfg_t *sep_ext;
352   session_endpoint_t *sep;
353   app_worker_t *app_wrk;
354   session_handle_t lh;
355   application_t *app;
356   app_listener_t *al;
357   u32 table_index;
358   session_t *ll;
359   u8 fib_proto;
360
361   sep_ext = (session_endpoint_cfg_t *) tep;
362   sep = (session_endpoint_t *) tep;
363   app_wrk = app_worker_get (sep_ext->app_wrk_index);
364   app = application_get (app_wrk->app_index);
365
366   sep->transport_proto = sep_ext->original_tp;
367   table_index = application_local_session_table (app);
368   lh = session_lookup_local_endpoint (table_index, sep);
369   if (lh == SESSION_DROP_HANDLE)
370     return VNET_API_ERROR_APP_CONNECT_FILTERED;
371
372   if (lh == SESSION_INVALID_HANDLE)
373     goto global_scope;
374
375   ll = listen_session_get_from_handle (lh);
376   al = app_listener_get_w_session (ll);
377
378   /*
379    * Break loop if rule in local table points to connecting app. This
380    * can happen if client is a generic proxy. Route connect through
381    * global table instead.
382    */
383   if (al->app_index == app->app_index)
384     goto global_scope;
385
386   return ct_connect (app_wrk, ll, sep_ext);
387
388   /*
389    * If nothing found, check the global scope for locally attached
390    * destinations. Make sure first that we're allowed to.
391    */
392
393 global_scope:
394   if (session_endpoint_is_local (sep))
395     return VNET_API_ERROR_SESSION_CONNECT;
396
397   if (!application_has_global_scope (app))
398     return VNET_API_ERROR_APP_CONNECT_SCOPE;
399
400   fib_proto = session_endpoint_fib_proto (sep);
401   table_index = application_session_table (app, fib_proto);
402   ll = session_lookup_listener (table_index, sep);
403
404   if (ll)
405     return ct_connect (app_wrk, ll, sep_ext);
406
407   /* Failed to connect but no error */
408   return 1;
409 }
410
411 void
412 ct_session_close (u32 ct_index, u32 thread_index)
413 {
414   ct_connection_t *ct, *peer_ct;
415   session_t *s;
416
417   ct = ct_connection_get (ct_index);
418   peer_ct = ct_connection_get (ct->peer_index);
419   if (peer_ct)
420     {
421       peer_ct->peer_index = ~0;
422       session_transport_closing_notify (&peer_ct->connection);
423     }
424
425   s = session_get (ct->c_s_index, 0);
426   app_worker_del_segment_notify (app_worker_get (s->app_wrk_index),
427                                  ct->segment_handle);
428   session_free_w_fifos (s);
429   ct_connection_free (ct);
430 }
431
432 transport_connection_t *
433 ct_session_get (u32 ct_index, u32 thread_index)
434 {
435   return (transport_connection_t *) ct_connection_get (ct_index);
436 }
437
438 static u8 *
439 format_ct_connection_id (u8 * s, va_list * args)
440 {
441   ct_connection_t *ct = va_arg (*args, ct_connection_t *);
442   if (!ct)
443     return s;
444   if (ct->c_is_ip4)
445     {
446       s = format (s, "[%d:%d][CT:%U] %U:%d->%U:%d", ct->c_thread_index,
447                   ct->c_s_index, format_transport_proto_short, ct->actual_tp,
448                   format_ip4_address, &ct->c_lcl_ip4,
449                   clib_net_to_host_u16 (ct->c_lcl_port), format_ip4_address,
450                   &ct->c_rmt_ip4, clib_net_to_host_u16 (ct->c_rmt_port));
451     }
452   else
453     {
454       s = format (s, "[%d:%d][CT:%U] %U:%d->%U:%d", ct->c_thread_index,
455                   ct->c_s_index, format_transport_proto_short, ct->actual_tp,
456                   format_ip6_address, &ct->c_lcl_ip6,
457                   clib_net_to_host_u16 (ct->c_lcl_port), format_ip6_address,
458                   &ct->c_rmt_ip6, clib_net_to_host_u16 (ct->c_rmt_port));
459     }
460
461   return s;
462 }
463
464 u8 *
465 format_ct_listener (u8 * s, va_list * args)
466 {
467   u32 tc_index = va_arg (*args, u32);
468   u32 __clib_unused verbose = va_arg (*args, u32);
469   ct_connection_t *ct = ct_connection_get (tc_index);
470   s = format (s, "%-50U", format_ct_connection_id, ct);
471   if (verbose)
472     s = format (s, "%-15s", "LISTEN");
473   return s;
474 }
475
476 u8 *
477 format_ct_connection (u8 * s, va_list * args)
478 {
479   ct_connection_t *ct = va_arg (*args, ct_connection_t *);
480   u32 verbose = va_arg (*args, u32);
481
482   if (!ct)
483     return s;
484   s = format (s, "%-50U", format_ct_connection_id, ct);
485   if (verbose)
486     {
487       s = format (s, "%-15s", "ESTABLISHED");
488       if (verbose > 1)
489         {
490           s = format (s, "\n");
491         }
492     }
493   return s;
494 }
495
496 u8 *
497 format_ct_session (u8 * s, va_list * args)
498 {
499   u32 ct_index = va_arg (*args, u32);
500   u32 __clib_unused thread_index = va_arg (*args, u32);
501   u32 verbose = va_arg (*args, u32);
502   ct_connection_t *ct;
503
504   ct = ct_connection_get (ct_index);
505   if (!ct)
506     {
507       s = format (s, "empty\n");
508       return s;
509     }
510
511   s = format (s, "%U", format_ct_connection, ct, verbose);
512   return s;
513 }
514
515 /* *INDENT-OFF* */
516 const static transport_proto_vft_t cut_thru_proto = {
517   .start_listen = ct_start_listen,
518   .stop_listen = ct_stop_listen,
519   .get_listener = ct_listener_get,
520   .connect = ct_session_connect,
521   .close = ct_session_close,
522   .get_connection = ct_session_get,
523   .tx_type = TRANSPORT_TX_INTERNAL,
524   .service_type = TRANSPORT_SERVICE_APP,
525   .format_listener = format_ct_listener,
526   .format_connection = format_ct_session,
527 };
528 /* *INDENT-ON* */
529
530 static clib_error_t *
531 ct_transport_init (vlib_main_t * vm)
532 {
533   transport_register_protocol (TRANSPORT_PROTO_NONE, &cut_thru_proto,
534                                FIB_PROTOCOL_IP4, ~0);
535   return 0;
536 }
537
538 VLIB_INIT_FUNCTION (ct_transport_init);
539
540 /*
541  * fd.io coding-style-patch-verification: ON
542  *
543  * Local Variables:
544  * eval: (c-set-style "gnu")
545  * End:
546  */