vcl: ldp support SO_ORIGINAL_DST
[vpp.git] / src / vnet / session / session_api.c
1 /*
2  * Copyright (c) 2015-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/vnet.h>
17 #include <vlibmemory/api.h>
18 #include <vnet/session/application.h>
19 #include <vnet/session/application_interface.h>
20 #include <vnet/session/application_local.h>
21 #include <vnet/session/session_rules_table.h>
22 #include <vnet/session/session_table.h>
23 #include <vnet/session/session.h>
24 #include <vnet/ip/ip_types_api.h>
25
26 #include <vnet/format_fns.h>
27 #include <vnet/session/session.api_enum.h>
28 #include <vnet/session/session.api_types.h>
29
30 #define REPLY_MSG_ID_BASE session_main.msg_id_base
31 #include <vlibapi/api_helper_macros.h>
32
33 static transport_proto_t
34 api_session_transport_proto_decode (const vl_api_transport_proto_t * api_tp)
35 {
36   switch (*api_tp)
37     {
38     case TRANSPORT_PROTO_API_TCP:
39       return TRANSPORT_PROTO_TCP;
40     case TRANSPORT_PROTO_API_UDP:
41       return TRANSPORT_PROTO_UDP;
42     case TRANSPORT_PROTO_API_TLS:
43       return TRANSPORT_PROTO_TLS;
44     case TRANSPORT_PROTO_API_QUIC:
45       return TRANSPORT_PROTO_QUIC;
46     default:
47       return TRANSPORT_PROTO_NONE;
48     }
49 }
50
51 static vl_api_transport_proto_t
52 api_session_transport_proto_encode (const transport_proto_t tp)
53 {
54   switch (tp)
55     {
56     case TRANSPORT_PROTO_TCP:
57       return TRANSPORT_PROTO_API_TCP;
58     case TRANSPORT_PROTO_UDP:
59       return TRANSPORT_PROTO_API_UDP;
60     case TRANSPORT_PROTO_TLS:
61       return TRANSPORT_PROTO_API_TLS;
62     case TRANSPORT_PROTO_QUIC:
63       return TRANSPORT_PROTO_API_QUIC;
64     default:
65       return TRANSPORT_PROTO_API_NONE;
66     }
67 }
68
69 static int
70 session_send_fds (vl_api_registration_t * reg, int fds[], int n_fds)
71 {
72   clib_error_t *error;
73   if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
74     return SESSION_E_BAPI_NO_FD;
75   error = vl_api_send_fd_msg (reg, fds, n_fds);
76   if (error)
77     {
78       clib_error_report (error);
79       return SESSION_E_BAPI_SEND_FD;
80     }
81   return 0;
82 }
83
84 static int
85 mq_send_session_accepted_cb (session_t * s)
86 {
87   app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
88   session_accepted_msg_t m = { 0 };
89   fifo_segment_t *eq_seg;
90   session_t *listener;
91   application_t *app;
92
93   app = application_get (app_wrk->app_index);
94
95   m.context = app->app_index;
96   m.server_rx_fifo = fifo_segment_fifo_offset (s->rx_fifo);
97   m.server_tx_fifo = fifo_segment_fifo_offset (s->tx_fifo);
98   m.segment_handle = session_segment_handle (s);
99   m.flags = s->flags;
100
101   eq_seg = application_get_rx_mqs_segment (app);
102
103   if (session_has_transport (s))
104     {
105       listener = listen_session_get_from_handle (s->listener_handle);
106       m.listener_handle = app_listen_session_handle (listener);
107       if (application_is_proxy (app))
108         {
109           listener =
110             app_worker_first_listener (app_wrk, session_get_fib_proto (s),
111                                        session_get_transport_proto (s));
112           if (listener)
113             m.listener_handle = listen_session_get_handle (listener);
114         }
115       m.vpp_event_queue_address =
116         fifo_segment_msg_q_offset (eq_seg, s->thread_index);
117       m.mq_index = s->thread_index;
118       m.handle = session_handle (s);
119
120       session_get_endpoint (s, &m.rmt, 0 /* is_lcl */);
121       session_get_endpoint (s, &m.lcl, 1 /* is_lcl */);
122     }
123   else
124     {
125       ct_connection_t *ct;
126
127       ct = (ct_connection_t *) session_get_transport (s);
128       listener = listen_session_get_from_handle (s->listener_handle);
129       m.listener_handle = app_listen_session_handle (listener);
130       m.rmt.is_ip4 = session_type_is_ip4 (listener->session_type);
131       m.rmt.port = ct->c_rmt_port;
132       m.lcl.port = ct->c_lcl_port;
133       m.handle = session_handle (s);
134       m.vpp_event_queue_address =
135         fifo_segment_msg_q_offset (eq_seg, s->thread_index);
136       m.mq_index = s->thread_index;
137     }
138
139   if (application_original_dst_is_enabled (app))
140     {
141       session_get_original_dst (&m.lcl, &m.rmt,
142                                 session_get_transport_proto (s),
143                                 &m.original_dst_ip4, &m.original_dst_port);
144     }
145
146   app_wrk_send_ctrl_evt (app_wrk, SESSION_CTRL_EVT_ACCEPTED, &m, sizeof (m));
147
148   return 0;
149 }
150
151 static inline void
152 mq_send_session_close_evt (app_worker_t * app_wrk, session_handle_t sh,
153                            session_evt_type_t evt_type)
154 {
155   session_disconnected_msg_t m = { 0 };
156
157   m.handle = sh;
158   m.context = app_wrk->api_client_index;
159
160   app_wrk_send_ctrl_evt (app_wrk, evt_type, &m, sizeof (m));
161 }
162
163 static inline void
164 mq_notify_close_subscribers (u32 app_index, session_handle_t sh,
165                              svm_fifo_t * f, session_evt_type_t evt_type)
166 {
167   app_worker_t *app_wrk;
168   application_t *app;
169   int i;
170
171   app = application_get (app_index);
172   if (!app)
173     return;
174
175   for (i = 0; i < f->shr->n_subscribers; i++)
176     {
177       if (!(app_wrk = application_get_worker (app, f->shr->subscribers[i])))
178         continue;
179       mq_send_session_close_evt (app_wrk, sh, SESSION_CTRL_EVT_DISCONNECTED);
180     }
181 }
182
183 static void
184 mq_send_session_disconnected_cb (session_t * s)
185 {
186   app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
187   session_handle_t sh = session_handle (s);
188
189   mq_send_session_close_evt (app_wrk, session_handle (s),
190                              SESSION_CTRL_EVT_DISCONNECTED);
191
192   if (svm_fifo_n_subscribers (s->rx_fifo))
193     mq_notify_close_subscribers (app_wrk->app_index, sh, s->rx_fifo,
194                                  SESSION_CTRL_EVT_DISCONNECTED);
195 }
196
197 static void
198 mq_send_session_reset_cb (session_t * s)
199 {
200   app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
201   session_handle_t sh = session_handle (s);
202
203   mq_send_session_close_evt (app_wrk, sh, SESSION_CTRL_EVT_RESET);
204
205   if (svm_fifo_n_subscribers (s->rx_fifo))
206     mq_notify_close_subscribers (app_wrk->app_index, sh, s->rx_fifo,
207                                  SESSION_CTRL_EVT_RESET);
208 }
209
210 int
211 mq_send_session_connected_cb (u32 app_wrk_index, u32 api_context,
212                               session_t * s, session_error_t err)
213 {
214   session_connected_msg_t m = { 0 };
215   transport_connection_t *tc;
216   fifo_segment_t *eq_seg;
217   app_worker_t *app_wrk;
218   application_t *app;
219
220   app_wrk = app_worker_get (app_wrk_index);
221
222   m.context = api_context;
223   m.retval = err;
224
225   if (err)
226     goto snd_msg;
227
228   app = application_get (app_wrk->app_index);
229   eq_seg = application_get_rx_mqs_segment (app);
230
231   if (session_has_transport (s))
232     {
233       tc = session_get_transport (s);
234       if (!tc)
235         {
236           clib_warning ("failed to retrieve transport!");
237           m.retval = SESSION_E_REFUSED;
238           goto snd_msg;
239         }
240
241       m.handle = session_handle (s);
242       m.vpp_event_queue_address =
243         fifo_segment_msg_q_offset (eq_seg, s->thread_index);
244
245       session_get_endpoint (s, &m.lcl, 1 /* is_lcl */);
246
247       m.server_rx_fifo = fifo_segment_fifo_offset (s->rx_fifo);
248       m.server_tx_fifo = fifo_segment_fifo_offset (s->tx_fifo);
249       m.segment_handle = session_segment_handle (s);
250       m.mq_index = s->thread_index;
251     }
252   else
253     {
254       ct_connection_t *cct;
255       session_t *ss;
256
257       cct = (ct_connection_t *) session_get_transport (s);
258       m.handle = session_handle (s);
259       m.lcl.port = cct->c_lcl_port;
260       m.lcl.is_ip4 = cct->c_is_ip4;
261       m.vpp_event_queue_address =
262         fifo_segment_msg_q_offset (eq_seg, s->thread_index);
263       m.server_rx_fifo = fifo_segment_fifo_offset (s->rx_fifo);
264       m.server_tx_fifo = fifo_segment_fifo_offset (s->tx_fifo);
265       m.segment_handle = session_segment_handle (s);
266       ss = ct_session_get_peer (s);
267       m.ct_rx_fifo = fifo_segment_fifo_offset (ss->tx_fifo);
268       m.ct_tx_fifo = fifo_segment_fifo_offset (ss->rx_fifo);
269       m.ct_segment_handle = session_segment_handle (ss);
270       m.mq_index = s->thread_index;
271     }
272
273   /* Setup client session index in advance, in case data arrives
274    * before the app processes message and updates it */
275   s->rx_fifo->shr->client_session_index = api_context;
276   s->tx_fifo->shr->client_session_index = api_context;
277
278 snd_msg:
279
280   app_wrk_send_ctrl_evt (app_wrk, SESSION_CTRL_EVT_CONNECTED, &m, sizeof (m));
281
282   return 0;
283 }
284
285 int
286 mq_send_session_bound_cb (u32 app_wrk_index, u32 api_context,
287                           session_handle_t handle, int rv)
288 {
289   session_bound_msg_t m = { 0 };
290   transport_endpoint_t tep;
291   fifo_segment_t *eq_seg;
292   app_worker_t *app_wrk;
293   application_t *app;
294   app_listener_t *al;
295   session_t *ls = 0;
296
297   app_wrk = app_worker_get (app_wrk_index);
298
299   m.context = api_context;
300   m.retval = rv;
301
302   if (rv)
303     goto snd_msg;
304
305   m.handle = handle;
306   al = app_listener_get_w_handle (handle);
307   if (al->session_index != SESSION_INVALID_INDEX)
308     ls = app_listener_get_session (al);
309   else
310     ls = app_listener_get_local_session (al);
311
312   session_get_endpoint (ls, &tep, 1 /* is_lcl */);
313   m.lcl_port = tep.port;
314   m.lcl_is_ip4 = tep.is_ip4;
315   clib_memcpy_fast (m.lcl_ip, &tep.ip, sizeof (tep.ip));
316   app = application_get (app_wrk->app_index);
317   eq_seg = application_get_rx_mqs_segment (app);
318   m.vpp_evt_q = fifo_segment_msg_q_offset (eq_seg, ls->thread_index);
319   m.mq_index = ls->thread_index;
320
321   if (session_transport_service_type (ls) == TRANSPORT_SERVICE_CL &&
322       ls->rx_fifo)
323     {
324       m.mq_index = transport_cl_thread ();
325       m.vpp_evt_q = fifo_segment_msg_q_offset (eq_seg, m.mq_index);
326       m.rx_fifo = fifo_segment_fifo_offset (ls->rx_fifo);
327       m.tx_fifo = fifo_segment_fifo_offset (ls->tx_fifo);
328       m.segment_handle = session_segment_handle (ls);
329     }
330
331 snd_msg:
332
333   app_wrk_send_ctrl_evt (app_wrk, SESSION_CTRL_EVT_BOUND, &m, sizeof (m));
334
335   return 0;
336 }
337
338 void
339 mq_send_unlisten_reply (app_worker_t * app_wrk, session_handle_t sh,
340                         u32 context, int rv)
341 {
342   session_unlisten_reply_msg_t m = { 0 };
343
344   m.context = context;
345   m.handle = sh;
346   m.retval = rv;
347   app_wrk_send_ctrl_evt (app_wrk, SESSION_CTRL_EVT_UNLISTEN_REPLY, &m,
348                          sizeof (m));
349 }
350
351 static void
352 mq_send_session_migrate_cb (session_t * s, session_handle_t new_sh)
353 {
354   session_migrated_msg_t m = { 0 };
355   fifo_segment_t *eq_seg;
356   app_worker_t *app_wrk;
357   application_t *app;
358   u32 thread_index;
359
360   thread_index = session_thread_from_handle (new_sh);
361   app_wrk = app_worker_get (s->app_wrk_index);
362   app = application_get (app_wrk->app_index);
363   eq_seg = application_get_rx_mqs_segment (app);
364
365   m.handle = session_handle (s);
366   m.new_handle = new_sh;
367   m.vpp_thread_index = thread_index;
368   m.vpp_evt_q = fifo_segment_msg_q_offset (eq_seg, thread_index);
369   m.segment_handle = SESSION_INVALID_HANDLE;
370
371   app_wrk_send_ctrl_evt (app_wrk, SESSION_CTRL_EVT_MIGRATED, &m, sizeof (m));
372 }
373
374 static int
375 mq_send_add_segment_cb (u32 app_wrk_index, u64 segment_handle)
376 {
377   session_app_add_segment_msg_t m = { 0 };
378   vl_api_registration_t *reg;
379   app_worker_t *app_wrk;
380   fifo_segment_t *fs;
381   ssvm_private_t *sp;
382   u8 fd_flags = 0;
383
384   app_wrk = app_worker_get (app_wrk_index);
385
386   reg = vl_mem_api_client_index_to_registration (app_wrk->api_client_index);
387   if (!reg)
388     {
389       clib_warning ("no api registration for client: %u",
390                     app_wrk->api_client_index);
391       return -1;
392     }
393
394   fs = segment_manager_get_segment_w_handle (segment_handle);
395   sp = &fs->ssvm;
396   if (ssvm_type (sp) == SSVM_SEGMENT_MEMFD)
397     {
398       if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
399         {
400           clib_warning ("can't send memfd fd");
401           return -1;
402         }
403
404       fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
405     }
406
407   m.segment_size = sp->ssvm_size;
408   m.fd_flags = fd_flags;
409   m.segment_handle = segment_handle;
410   strncpy ((char *) m.segment_name, (char *) sp->name,
411            sizeof (m.segment_name) - 1);
412
413   app_wrk_send_ctrl_evt_fd (app_wrk, SESSION_CTRL_EVT_APP_ADD_SEGMENT, &m,
414                             sizeof (m), sp->fd);
415
416   return 0;
417 }
418
419 static int
420 mq_send_del_segment_cb (u32 app_wrk_index, u64 segment_handle)
421 {
422   session_app_del_segment_msg_t m = { 0 };
423   vl_api_registration_t *reg;
424   app_worker_t *app_wrk;
425
426   app_wrk = app_worker_get (app_wrk_index);
427   reg = vl_mem_api_client_index_to_registration (app_wrk->api_client_index);
428   if (!reg)
429     {
430       clib_warning ("no registration: %u", app_wrk->api_client_index);
431       return -1;
432     }
433
434   m.segment_handle = segment_handle;
435
436   app_wrk_send_ctrl_evt (app_wrk, SESSION_CTRL_EVT_APP_DEL_SEGMENT, &m,
437                          sizeof (m));
438
439   return 0;
440 }
441
442 static void
443 mq_send_session_cleanup_cb (session_t * s, session_cleanup_ntf_t ntf)
444 {
445   session_cleanup_msg_t m = { 0 };
446   app_worker_t *app_wrk;
447
448   /* Propagate transport cleanup notifications only if app didn't close */
449   if (ntf == SESSION_CLEANUP_TRANSPORT
450       && s->session_state != SESSION_STATE_TRANSPORT_DELETED)
451     return;
452
453   app_wrk = app_worker_get_if_valid (s->app_wrk_index);
454   if (!app_wrk)
455     return;
456
457   m.handle = session_handle (s);
458   m.type = ntf;
459
460   app_wrk_send_ctrl_evt (app_wrk, SESSION_CTRL_EVT_CLEANUP, &m, sizeof (m));
461 }
462
463 static session_cb_vft_t session_mq_cb_vft = {
464   .session_accept_callback = mq_send_session_accepted_cb,
465   .session_disconnect_callback = mq_send_session_disconnected_cb,
466   .session_connected_callback = mq_send_session_connected_cb,
467   .session_reset_callback = mq_send_session_reset_cb,
468   .session_migrate_callback = mq_send_session_migrate_cb,
469   .session_cleanup_callback = mq_send_session_cleanup_cb,
470   .add_segment_callback = mq_send_add_segment_cb,
471   .del_segment_callback = mq_send_del_segment_cb,
472 };
473
474 static void
475 vl_api_session_enable_disable_t_handler (vl_api_session_enable_disable_t * mp)
476 {
477   vl_api_session_enable_disable_reply_t *rmp;
478   vlib_main_t *vm = vlib_get_main ();
479   int rv = 0;
480
481   vnet_session_enable_disable (vm, mp->is_enable);
482   REPLY_MACRO (VL_API_SESSION_ENABLE_DISABLE_REPLY);
483 }
484
485 static void
486 vl_api_session_sapi_enable_disable_t_handler (
487   vl_api_session_sapi_enable_disable_t *mp)
488 {
489   vl_api_session_sapi_enable_disable_reply_t *rmp;
490   int rv = 0;
491
492   rv = appns_sapi_enable_disable (mp->is_enable);
493   REPLY_MACRO (VL_API_SESSION_SAPI_ENABLE_DISABLE_REPLY);
494 }
495
496 static void
497 vl_api_app_attach_t_handler (vl_api_app_attach_t * mp)
498 {
499   int rv = 0, *fds = 0, n_fds = 0, n_workers, i;
500   fifo_segment_t *segp, *rx_mqs_seg = 0;
501   vnet_app_attach_args_t _a, *a = &_a;
502   vl_api_app_attach_reply_t *rmp;
503   u8 fd_flags = 0, ctrl_thread;
504   vl_api_registration_t *reg;
505   svm_msg_q_t *rx_mq;
506   application_t *app;
507
508   reg = vl_api_client_index_to_registration (mp->client_index);
509   if (!reg)
510     return;
511
512   n_workers = vlib_num_workers ();
513   if (!session_main_is_enabled () || appns_sapi_enabled ())
514     {
515       rv = VNET_API_ERROR_FEATURE_DISABLED;
516       goto done;
517     }
518   /* Only support binary api with socket transport */
519   if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
520     {
521       rv = VNET_API_ERROR_APP_UNSUPPORTED_CFG;
522       goto done;
523     }
524
525   STATIC_ASSERT (sizeof (u64) * APP_OPTIONS_N_OPTIONS <=
526                  sizeof (mp->options),
527                  "Out of options, fix api message definition");
528
529   clib_memset (a, 0, sizeof (*a));
530   a->api_client_index = mp->client_index;
531   a->options = mp->options;
532   a->session_cb_vft = &session_mq_cb_vft;
533   a->namespace_id = vl_api_from_api_to_new_vec (mp, &mp->namespace_id);
534
535   if ((rv = vnet_application_attach (a)))
536     {
537       clib_warning ("attach returned: %d", rv);
538       vec_free (a->namespace_id);
539       goto done;
540     }
541   vec_free (a->namespace_id);
542
543   vec_validate (fds, 3 /* segs + tx evtfd */ + n_workers);
544
545   /* Send rx mqs segment */
546   app = application_get (a->app_index);
547   rx_mqs_seg = application_get_rx_mqs_segment (app);
548
549   fd_flags |= SESSION_FD_F_VPP_MQ_SEGMENT;
550   fds[n_fds] = rx_mqs_seg->ssvm.fd;
551   n_fds += 1;
552
553   /* Send fifo segment fd if needed */
554   if (ssvm_type (a->segment) == SSVM_SEGMENT_MEMFD)
555     {
556       fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
557       fds[n_fds] = a->segment->fd;
558       n_fds += 1;
559     }
560   if (a->options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD)
561     {
562       fd_flags |= SESSION_FD_F_MQ_EVENTFD;
563       fds[n_fds] = svm_msg_q_get_eventfd (a->app_evt_q);
564       n_fds += 1;
565     }
566
567   if (application_use_private_rx_mqs ())
568     {
569       fd_flags |= SESSION_FD_F_VPP_MQ_EVENTFD;
570       for (i = 0; i < n_workers + 1; i++)
571         {
572           rx_mq = application_rx_mq_get (app, i);
573           fds[n_fds] = svm_msg_q_get_eventfd (rx_mq);
574           n_fds += 1;
575         }
576     }
577
578 done:
579   /* *INDENT-OFF* */
580   REPLY_MACRO3 (
581     VL_API_APP_ATTACH_REPLY,
582     ((!rv) ? vec_len (((fifo_segment_t *) a->segment)->ssvm.name) : 0), ({
583       if (!rv)
584         {
585           ctrl_thread = n_workers ? 1 : 0;
586           segp = (fifo_segment_t *) a->segment;
587           rmp->app_index = clib_host_to_net_u32 (a->app_index);
588           rmp->app_mq = fifo_segment_msg_q_offset (segp, 0);
589           rmp->vpp_ctrl_mq =
590             fifo_segment_msg_q_offset (rx_mqs_seg, ctrl_thread);
591           rmp->vpp_ctrl_mq_thread = ctrl_thread;
592           rmp->n_fds = n_fds;
593           rmp->fd_flags = fd_flags;
594           if (vec_len (segp->ssvm.name))
595             {
596               vl_api_vec_to_api_string (segp->ssvm.name, &rmp->segment_name);
597             }
598           rmp->segment_size = segp->ssvm.ssvm_size;
599           rmp->segment_handle = clib_host_to_net_u64 (a->segment_handle);
600         }
601     }));
602   /* *INDENT-ON* */
603
604   if (n_fds)
605     session_send_fds (reg, fds, n_fds);
606   vec_free (fds);
607 }
608
609 static void
610 vl_api_app_worker_add_del_t_handler (vl_api_app_worker_add_del_t * mp)
611 {
612   int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0;
613   vl_api_app_worker_add_del_reply_t *rmp;
614   vl_api_registration_t *reg;
615   application_t *app;
616   u8 fd_flags = 0;
617
618   if (!session_main_is_enabled () || appns_sapi_enabled ())
619     {
620       rv = VNET_API_ERROR_FEATURE_DISABLED;
621       goto done;
622     }
623
624   reg = vl_api_client_index_to_registration (mp->client_index);
625   if (!reg)
626     return;
627
628   app = application_get_if_valid (clib_net_to_host_u32 (mp->app_index));
629   if (!app)
630     {
631       rv = VNET_API_ERROR_INVALID_VALUE;
632       goto done;
633     }
634
635   vnet_app_worker_add_del_args_t args = {
636     .app_index = app->app_index,
637     .wrk_map_index = clib_net_to_host_u32 (mp->wrk_index),
638     .api_client_index = mp->client_index,
639     .is_add = mp->is_add
640   };
641   rv = vnet_app_worker_add_del (&args);
642   if (rv)
643     {
644       clib_warning ("app worker add/del returned: %d", rv);
645       goto done;
646     }
647
648   if (!mp->is_add)
649     goto done;
650
651   /* Send fifo segment fd if needed */
652   if (ssvm_type (args.segment) == SSVM_SEGMENT_MEMFD)
653     {
654       fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
655       fds[n_fds] = args.segment->fd;
656       n_fds += 1;
657     }
658   if (application_segment_manager_properties (app)->use_mq_eventfd)
659     {
660       fd_flags |= SESSION_FD_F_MQ_EVENTFD;
661       fds[n_fds] = svm_msg_q_get_eventfd (args.evt_q);
662       n_fds += 1;
663     }
664
665   /* *INDENT-OFF* */
666 done:
667   REPLY_MACRO3 (
668     VL_API_APP_WORKER_ADD_DEL_REPLY,
669     ((!rv && mp->is_add) ? vec_len (args.segment->name) : 0), ({
670       rmp->is_add = mp->is_add;
671       rmp->wrk_index = clib_host_to_net_u32 (args.wrk_map_index);
672       rmp->segment_handle = clib_host_to_net_u64 (args.segment_handle);
673       if (!rv && mp->is_add)
674         {
675           rmp->app_event_queue_address =
676             fifo_segment_msg_q_offset ((fifo_segment_t *) args.segment, 0);
677           rmp->n_fds = n_fds;
678           rmp->fd_flags = fd_flags;
679           if (vec_len (args.segment->name))
680             {
681               vl_api_vec_to_api_string (args.segment->name,
682                                         &rmp->segment_name);
683             }
684         }
685     }));
686   /* *INDENT-ON* */
687
688   if (n_fds)
689     session_send_fds (reg, fds, n_fds);
690 }
691
692 static void
693 vl_api_application_detach_t_handler (vl_api_application_detach_t * mp)
694 {
695   vl_api_application_detach_reply_t *rmp;
696   int rv = VNET_API_ERROR_INVALID_VALUE_2;
697   vnet_app_detach_args_t _a, *a = &_a;
698   application_t *app;
699
700   if (!session_main_is_enabled () || appns_sapi_enabled ())
701     {
702       rv = VNET_API_ERROR_FEATURE_DISABLED;
703       goto done;
704     }
705
706   app = application_lookup (mp->client_index);
707   if (app)
708     {
709       a->app_index = app->app_index;
710       a->api_client_index = mp->client_index;
711       rv = vnet_application_detach (a);
712     }
713
714 done:
715   REPLY_MACRO (VL_API_APPLICATION_DETACH_REPLY);
716 }
717
718 static void
719 vl_api_app_namespace_add_del_t_handler (vl_api_app_namespace_add_del_t * mp)
720 {
721   vl_api_app_namespace_add_del_reply_t *rmp;
722   u32 appns_index = 0;
723   u8 *ns_id = 0;
724   int rv = 0;
725   if (session_main_is_enabled () == 0)
726     {
727       rv = VNET_API_ERROR_FEATURE_DISABLED;
728       goto done;
729     }
730
731   ns_id = vl_api_from_api_to_new_vec (mp, &mp->namespace_id);
732
733   vnet_app_namespace_add_del_args_t args = {
734     .ns_id = ns_id,
735     .sock_name = 0,
736     .secret = clib_net_to_host_u64 (mp->secret),
737     .sw_if_index = clib_net_to_host_u32 (mp->sw_if_index),
738     .ip4_fib_id = clib_net_to_host_u32 (mp->ip4_fib_id),
739     .ip6_fib_id = clib_net_to_host_u32 (mp->ip6_fib_id),
740     .is_add = 1
741   };
742   rv = vnet_app_namespace_add_del (&args);
743   if (!rv)
744     {
745       appns_index = app_namespace_index_from_id (ns_id);
746       if (appns_index == APP_NAMESPACE_INVALID_INDEX)
747         {
748           clib_warning ("app ns lookup failed");
749           rv = VNET_API_ERROR_UNSPECIFIED;
750         }
751     }
752   vec_free (ns_id);
753
754   /* *INDENT-OFF* */
755 done:
756   REPLY_MACRO2 (VL_API_APP_NAMESPACE_ADD_DEL_REPLY, ({
757     if (!rv)
758       rmp->appns_index = clib_host_to_net_u32 (appns_index);
759   }));
760   /* *INDENT-ON* */
761 }
762
763 static void
764 vl_api_app_namespace_add_del_v2_t_handler (
765   vl_api_app_namespace_add_del_v2_t *mp)
766 {
767   vl_api_app_namespace_add_del_v2_reply_t *rmp;
768   u8 *ns_id = 0;
769   u32 appns_index = 0;
770   int rv = 0;
771
772   if (session_main_is_enabled () == 0)
773     {
774       rv = VNET_API_ERROR_FEATURE_DISABLED;
775       goto done;
776     }
777
778   mp->namespace_id[sizeof (mp->namespace_id) - 1] = 0;
779   ns_id = format (0, "%s", &mp->namespace_id);
780
781   vnet_app_namespace_add_del_args_t args = {
782     .ns_id = ns_id,
783     .sock_name = 0,
784     .secret = clib_net_to_host_u64 (mp->secret),
785     .sw_if_index = clib_net_to_host_u32 (mp->sw_if_index),
786     .ip4_fib_id = clib_net_to_host_u32 (mp->ip4_fib_id),
787     .ip6_fib_id = clib_net_to_host_u32 (mp->ip6_fib_id),
788     .is_add = 1
789   };
790   rv = vnet_app_namespace_add_del (&args);
791   if (!rv)
792     {
793       appns_index = app_namespace_index_from_id (ns_id);
794       if (appns_index == APP_NAMESPACE_INVALID_INDEX)
795         {
796           clib_warning ("app ns lookup failed id:%s", ns_id);
797           rv = VNET_API_ERROR_UNSPECIFIED;
798         }
799     }
800   vec_free (ns_id);
801
802 done:
803   REPLY_MACRO2 (VL_API_APP_NAMESPACE_ADD_DEL_V2_REPLY, ({
804                   if (!rv)
805                     rmp->appns_index = clib_host_to_net_u32 (appns_index);
806                 }));
807 }
808
809 static void
810 vl_api_app_namespace_add_del_v4_t_handler (
811   vl_api_app_namespace_add_del_v4_t *mp)
812 {
813   vl_api_app_namespace_add_del_v4_reply_t *rmp;
814   u8 *ns_id = 0, *sock_name = 0;
815   u32 appns_index = 0;
816   int rv = 0;
817   if (session_main_is_enabled () == 0)
818     {
819       rv = VNET_API_ERROR_FEATURE_DISABLED;
820       goto done;
821     }
822   mp->namespace_id[sizeof (mp->namespace_id) - 1] = 0;
823   ns_id = format (0, "%s", &mp->namespace_id);
824   sock_name = vl_api_from_api_to_new_vec (mp, &mp->sock_name);
825   vnet_app_namespace_add_del_args_t args = {
826     .ns_id = ns_id,
827     .sock_name = sock_name,
828     .secret = clib_net_to_host_u64 (mp->secret),
829     .sw_if_index = clib_net_to_host_u32 (mp->sw_if_index),
830     .ip4_fib_id = clib_net_to_host_u32 (mp->ip4_fib_id),
831     .ip6_fib_id = clib_net_to_host_u32 (mp->ip6_fib_id),
832     .is_add = mp->is_add,
833   };
834   rv = vnet_app_namespace_add_del (&args);
835   if (!rv && mp->is_add)
836     {
837       appns_index = app_namespace_index_from_id (ns_id);
838       if (appns_index == APP_NAMESPACE_INVALID_INDEX)
839         {
840           clib_warning ("app ns lookup failed id:%s", ns_id);
841           rv = VNET_API_ERROR_UNSPECIFIED;
842         }
843     }
844   vec_free (ns_id);
845   vec_free (sock_name);
846 done:
847   REPLY_MACRO2 (VL_API_APP_NAMESPACE_ADD_DEL_V4_REPLY, ({
848                   if (!rv)
849                     rmp->appns_index = clib_host_to_net_u32 (appns_index);
850                 }));
851 }
852
853 static void
854 vl_api_app_namespace_add_del_v3_t_handler (
855   vl_api_app_namespace_add_del_v3_t *mp)
856 {
857   vl_api_app_namespace_add_del_v3_reply_t *rmp;
858   u8 *ns_id = 0, *sock_name = 0, *api_sock_name = 0;
859   u32 appns_index = 0;
860   int rv = 0;
861   if (session_main_is_enabled () == 0)
862     {
863       rv = VNET_API_ERROR_FEATURE_DISABLED;
864       goto done;
865     }
866   mp->namespace_id[sizeof (mp->namespace_id) - 1] = 0;
867   ns_id = format (0, "%s", &mp->namespace_id);
868   api_sock_name = vl_api_from_api_to_new_vec (mp, &mp->sock_name);
869   mp->netns[sizeof (mp->netns) - 1] = 0;
870   if (strlen ((char *) mp->netns) != 0)
871     {
872       sock_name =
873         format (0, "abstract:%v,netns_name=%s", api_sock_name, &mp->netns);
874     }
875   else
876     {
877       sock_name = api_sock_name;
878       api_sock_name = 0; // for vec_free
879     }
880
881   vnet_app_namespace_add_del_args_t args = {
882     .ns_id = ns_id,
883     .sock_name = sock_name,
884     .secret = clib_net_to_host_u64 (mp->secret),
885     .sw_if_index = clib_net_to_host_u32 (mp->sw_if_index),
886     .ip4_fib_id = clib_net_to_host_u32 (mp->ip4_fib_id),
887     .ip6_fib_id = clib_net_to_host_u32 (mp->ip6_fib_id),
888     .is_add = mp->is_add,
889   };
890   rv = vnet_app_namespace_add_del (&args);
891   if (!rv && mp->is_add)
892     {
893       appns_index = app_namespace_index_from_id (ns_id);
894       if (appns_index == APP_NAMESPACE_INVALID_INDEX)
895         {
896           clib_warning ("app ns lookup failed id:%s", ns_id);
897           rv = VNET_API_ERROR_UNSPECIFIED;
898         }
899     }
900   vec_free (ns_id);
901   vec_free (sock_name);
902   vec_free (api_sock_name);
903 done:
904   REPLY_MACRO2 (VL_API_APP_NAMESPACE_ADD_DEL_V3_REPLY, ({
905                   if (!rv)
906                     rmp->appns_index = clib_host_to_net_u32 (appns_index);
907                 }));
908 }
909
910 static void
911 vl_api_session_rule_add_del_t_handler (vl_api_session_rule_add_del_t * mp)
912 {
913   vl_api_session_rule_add_del_reply_t *rmp;
914   session_rule_add_del_args_t args;
915   session_rule_table_add_del_args_t *table_args = &args.table_args;
916   int rv = 0;
917
918   clib_memset (&args, 0, sizeof (args));
919
920   ip_prefix_decode (&mp->lcl, &table_args->lcl);
921   ip_prefix_decode (&mp->rmt, &table_args->rmt);
922
923   table_args->lcl_port = mp->lcl_port;
924   table_args->rmt_port = mp->rmt_port;
925   table_args->action_index = clib_net_to_host_u32 (mp->action_index);
926   table_args->is_add = mp->is_add;
927   mp->tag[sizeof (mp->tag) - 1] = 0;
928   table_args->tag = format (0, "%s", mp->tag);
929   args.appns_index = clib_net_to_host_u32 (mp->appns_index);
930   args.scope = mp->scope;
931   args.transport_proto =
932     api_session_transport_proto_decode (&mp->transport_proto) ==
933     TRANSPORT_PROTO_UDP ? 1 : 0;
934
935   rv = vnet_session_rule_add_del (&args);
936   if (rv)
937     clib_warning ("rule add del returned: %d", rv);
938   vec_free (table_args->tag);
939   REPLY_MACRO (VL_API_SESSION_RULE_ADD_DEL_REPLY);
940 }
941
942 static void
943 send_session_rule_details4 (mma_rule_16_t * rule, u8 is_local,
944                             u8 transport_proto, u32 appns_index, u8 * tag,
945                             vl_api_registration_t * reg, u32 context)
946 {
947   vl_api_session_rules_details_t *rmp = 0;
948   session_mask_or_match_4_t *match =
949     (session_mask_or_match_4_t *) & rule->match;
950   session_mask_or_match_4_t *mask =
951     (session_mask_or_match_4_t *) & rule->mask;
952   fib_prefix_t lcl, rmt;
953
954   rmp = vl_msg_api_alloc (sizeof (*rmp));
955   clib_memset (rmp, 0, sizeof (*rmp));
956   rmp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_SESSION_RULES_DETAILS);
957   rmp->context = context;
958
959   clib_memset (&lcl, 0, sizeof (lcl));
960   clib_memset (&rmt, 0, sizeof (rmt));
961   ip_set (&lcl.fp_addr, &match->lcl_ip, 1);
962   ip_set (&rmt.fp_addr, &match->rmt_ip, 1);
963   lcl.fp_len = ip4_mask_to_preflen (&mask->lcl_ip);
964   rmt.fp_len = ip4_mask_to_preflen (&mask->rmt_ip);
965
966   ip_prefix_encode (&lcl, &rmp->lcl);
967   ip_prefix_encode (&rmt, &rmp->rmt);
968   rmp->lcl_port = match->lcl_port;
969   rmp->rmt_port = match->rmt_port;
970   rmp->action_index = clib_host_to_net_u32 (rule->action_index);
971   rmp->scope =
972     is_local ? SESSION_RULE_SCOPE_API_LOCAL : SESSION_RULE_SCOPE_API_GLOBAL;
973   rmp->transport_proto = api_session_transport_proto_encode (transport_proto);
974   rmp->appns_index = clib_host_to_net_u32 (appns_index);
975   if (tag)
976     {
977       clib_memcpy_fast (rmp->tag, tag, vec_len (tag));
978       rmp->tag[vec_len (tag)] = 0;
979     }
980
981   vl_api_send_msg (reg, (u8 *) rmp);
982 }
983
984 static void
985 send_session_rule_details6 (mma_rule_40_t * rule, u8 is_local,
986                             u8 transport_proto, u32 appns_index, u8 * tag,
987                             vl_api_registration_t * reg, u32 context)
988 {
989   vl_api_session_rules_details_t *rmp = 0;
990   session_mask_or_match_6_t *match =
991     (session_mask_or_match_6_t *) & rule->match;
992   session_mask_or_match_6_t *mask =
993     (session_mask_or_match_6_t *) & rule->mask;
994   fib_prefix_t lcl, rmt;
995
996   rmp = vl_msg_api_alloc (sizeof (*rmp));
997   clib_memset (rmp, 0, sizeof (*rmp));
998   rmp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_SESSION_RULES_DETAILS);
999   rmp->context = context;
1000
1001   clib_memset (&lcl, 0, sizeof (lcl));
1002   clib_memset (&rmt, 0, sizeof (rmt));
1003   ip_set (&lcl.fp_addr, &match->lcl_ip, 0);
1004   ip_set (&rmt.fp_addr, &match->rmt_ip, 0);
1005   lcl.fp_len = ip6_mask_to_preflen (&mask->lcl_ip);
1006   rmt.fp_len = ip6_mask_to_preflen (&mask->rmt_ip);
1007
1008   ip_prefix_encode (&lcl, &rmp->lcl);
1009   ip_prefix_encode (&rmt, &rmp->rmt);
1010   rmp->lcl_port = match->lcl_port;
1011   rmp->rmt_port = match->rmt_port;
1012   rmp->action_index = clib_host_to_net_u32 (rule->action_index);
1013   rmp->scope =
1014     is_local ? SESSION_RULE_SCOPE_API_LOCAL : SESSION_RULE_SCOPE_API_GLOBAL;
1015   rmp->transport_proto = api_session_transport_proto_encode (transport_proto);
1016   rmp->appns_index = clib_host_to_net_u32 (appns_index);
1017   if (tag)
1018     {
1019       clib_memcpy_fast (rmp->tag, tag, vec_len (tag));
1020       rmp->tag[vec_len (tag)] = 0;
1021     }
1022
1023   vl_api_send_msg (reg, (u8 *) rmp);
1024 }
1025
1026 static void
1027 send_session_rules_table_details (session_rules_table_t * srt, u8 fib_proto,
1028                                   u8 tp, u8 is_local, u32 appns_index,
1029                                   vl_api_registration_t * reg, u32 context)
1030 {
1031   mma_rule_16_t *rule16;
1032   mma_rule_40_t *rule40;
1033   mma_rules_table_16_t *srt16;
1034   mma_rules_table_40_t *srt40;
1035   u32 ri;
1036
1037   if (is_local || fib_proto == FIB_PROTOCOL_IP4)
1038     {
1039       u8 *tag = 0;
1040       /* *INDENT-OFF* */
1041       srt16 = &srt->session_rules_tables_16;
1042       pool_foreach (rule16, srt16->rules)  {
1043         ri = mma_rules_table_rule_index_16 (srt16, rule16);
1044         tag = session_rules_table_rule_tag (srt, ri, 1);
1045         send_session_rule_details4 (rule16, is_local, tp, appns_index, tag,
1046                                     reg, context);
1047       }
1048       /* *INDENT-ON* */
1049     }
1050   if (is_local || fib_proto == FIB_PROTOCOL_IP6)
1051     {
1052       u8 *tag = 0;
1053       /* *INDENT-OFF* */
1054       srt40 = &srt->session_rules_tables_40;
1055       pool_foreach (rule40, srt40->rules)  {
1056         ri = mma_rules_table_rule_index_40 (srt40, rule40);
1057         tag = session_rules_table_rule_tag (srt, ri, 1);
1058         send_session_rule_details6 (rule40, is_local, tp, appns_index, tag,
1059                                     reg, context);
1060       }
1061       /* *INDENT-ON* */
1062     }
1063 }
1064
1065 static void
1066 vl_api_session_rules_dump_t_handler (vl_api_session_rules_dump_t * mp)
1067 {
1068   vl_api_registration_t *reg;
1069   session_table_t *st;
1070   u8 tp;
1071
1072   reg = vl_api_client_index_to_registration (mp->client_index);
1073   if (!reg)
1074     return;
1075
1076   /* *INDENT-OFF* */
1077   session_table_foreach (st, ({
1078     for (tp = 0; tp < TRANSPORT_N_PROTOS; tp++)
1079       {
1080         send_session_rules_table_details (&st->session_rules[tp],
1081                                           st->active_fib_proto, tp,
1082                                           st->is_local, st->appns_index, reg,
1083                                           mp->context);
1084       }
1085   }));
1086   /* *INDENT-ON* */
1087 }
1088
1089 static void
1090 vl_api_app_add_cert_key_pair_t_handler (vl_api_app_add_cert_key_pair_t * mp)
1091 {
1092   vl_api_app_add_cert_key_pair_reply_t *rmp;
1093   vnet_app_add_cert_key_pair_args_t _a, *a = &_a;
1094   u32 certkey_len, key_len, cert_len;
1095   int rv = 0;
1096   if (session_main_is_enabled () == 0)
1097     {
1098       rv = VNET_API_ERROR_FEATURE_DISABLED;
1099       goto done;
1100     }
1101
1102   cert_len = clib_net_to_host_u16 (mp->cert_len);
1103   if (cert_len > 10000)
1104     {
1105       rv = VNET_API_ERROR_INVALID_VALUE;
1106       goto done;
1107     }
1108
1109   certkey_len = clib_net_to_host_u16 (mp->certkey_len);
1110   if (certkey_len < cert_len)
1111     {
1112       rv = VNET_API_ERROR_INVALID_VALUE;
1113       goto done;
1114     }
1115
1116   key_len = certkey_len - cert_len;
1117   if (key_len > 10000)
1118     {
1119       rv = VNET_API_ERROR_INVALID_VALUE;
1120       goto done;
1121     }
1122
1123   clib_memset (a, 0, sizeof (*a));
1124   a->cert = mp->certkey;
1125   a->key = mp->certkey + cert_len;
1126   a->cert_len = cert_len;
1127   a->key_len = key_len;
1128   rv = vnet_app_add_cert_key_pair (a);
1129
1130 done:
1131   /* *INDENT-OFF* */
1132   REPLY_MACRO2 (VL_API_APP_ADD_CERT_KEY_PAIR_REPLY, ({
1133     if (!rv)
1134       rmp->index = clib_host_to_net_u32 (a->index);
1135   }));
1136   /* *INDENT-ON* */
1137 }
1138
1139 static void
1140 vl_api_app_del_cert_key_pair_t_handler (vl_api_app_del_cert_key_pair_t * mp)
1141 {
1142   vl_api_app_del_cert_key_pair_reply_t *rmp;
1143   u32 ckpair_index;
1144   int rv = 0;
1145   if (session_main_is_enabled () == 0)
1146     {
1147       rv = VNET_API_ERROR_FEATURE_DISABLED;
1148       goto done;
1149     }
1150   ckpair_index = clib_net_to_host_u32 (mp->index);
1151   rv = vnet_app_del_cert_key_pair (ckpair_index);
1152
1153 done:
1154   REPLY_MACRO (VL_API_APP_DEL_CERT_KEY_PAIR_REPLY);
1155 }
1156
1157 static clib_error_t *
1158 application_reaper_cb (u32 client_index)
1159 {
1160   application_t *app = application_lookup (client_index);
1161   vnet_app_detach_args_t _a, *a = &_a;
1162   if (app)
1163     {
1164       a->app_index = app->app_index;
1165       a->api_client_index = client_index;
1166       vnet_application_detach (a);
1167     }
1168   return 0;
1169 }
1170
1171 VL_MSG_API_REAPER_FUNCTION (application_reaper_cb);
1172
1173 /*
1174  * Socket api functions
1175  */
1176
1177 static int
1178 mq_send_add_segment_sapi_cb (u32 app_wrk_index, u64 segment_handle)
1179 {
1180   session_app_add_segment_msg_t m = { 0 };
1181   app_worker_t *app_wrk;
1182   fifo_segment_t *fs;
1183   ssvm_private_t *sp;
1184   u8 fd_flags = 0;
1185
1186   app_wrk = app_worker_get (app_wrk_index);
1187
1188   fs = segment_manager_get_segment_w_handle (segment_handle);
1189   sp = &fs->ssvm;
1190   ASSERT (ssvm_type (sp) == SSVM_SEGMENT_MEMFD);
1191
1192   fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
1193
1194   m.segment_size = sp->ssvm_size;
1195   m.fd_flags = fd_flags;
1196   m.segment_handle = segment_handle;
1197   strncpy ((char *) m.segment_name, (char *) sp->name,
1198            sizeof (m.segment_name) - 1);
1199
1200   app_wrk_send_ctrl_evt_fd (app_wrk, SESSION_CTRL_EVT_APP_ADD_SEGMENT, &m,
1201                             sizeof (m), sp->fd);
1202
1203   return 0;
1204 }
1205
1206 static int
1207 mq_send_del_segment_sapi_cb (u32 app_wrk_index, u64 segment_handle)
1208 {
1209   session_app_del_segment_msg_t m = { 0 };
1210   app_worker_t *app_wrk;
1211
1212   app_wrk = app_worker_get (app_wrk_index);
1213
1214   m.segment_handle = segment_handle;
1215
1216   app_wrk_send_ctrl_evt (app_wrk, SESSION_CTRL_EVT_APP_DEL_SEGMENT, &m,
1217                          sizeof (m));
1218
1219   return 0;
1220 }
1221
1222 static session_cb_vft_t session_mq_sapi_cb_vft = {
1223   .session_accept_callback = mq_send_session_accepted_cb,
1224   .session_disconnect_callback = mq_send_session_disconnected_cb,
1225   .session_connected_callback = mq_send_session_connected_cb,
1226   .session_reset_callback = mq_send_session_reset_cb,
1227   .session_migrate_callback = mq_send_session_migrate_cb,
1228   .session_cleanup_callback = mq_send_session_cleanup_cb,
1229   .add_segment_callback = mq_send_add_segment_sapi_cb,
1230   .del_segment_callback = mq_send_del_segment_sapi_cb,
1231 };
1232
1233 static void
1234 session_api_attach_handler (app_namespace_t * app_ns, clib_socket_t * cs,
1235                             app_sapi_attach_msg_t * mp)
1236 {
1237   int rv = 0, *fds = 0, n_fds = 0, i, n_workers;
1238   vnet_app_attach_args_t _a, *a = &_a;
1239   app_sapi_attach_reply_msg_t *rmp;
1240   u8 fd_flags = 0, ctrl_thread;
1241   app_ns_api_handle_t *handle;
1242   fifo_segment_t *rx_mqs_seg;
1243   app_sapi_msg_t msg = { 0 };
1244   app_worker_t *app_wrk;
1245   application_t *app;
1246   svm_msg_q_t *rx_mq;
1247
1248   /* Make sure name is null terminated */
1249   mp->name[63] = 0;
1250
1251   clib_memset (a, 0, sizeof (*a));
1252   a->api_client_index = appns_sapi_socket_handle (app_ns, cs);
1253   a->name = format (0, "%s", (char *) mp->name);
1254   a->options = mp->options;
1255   a->session_cb_vft = &session_mq_sapi_cb_vft;
1256   a->use_sock_api = 1;
1257   a->options[APP_OPTIONS_NAMESPACE] = app_namespace_index (app_ns);
1258
1259   if ((rv = vnet_application_attach (a)))
1260     {
1261       clib_warning ("attach returned: %d", rv);
1262       goto done;
1263     }
1264
1265   n_workers = vlib_num_workers ();
1266   vec_validate (fds, 3 /* segs + tx evtfd */ + n_workers);
1267
1268   /* Send event queues segment */
1269   app = application_get (a->app_index);
1270   rx_mqs_seg = application_get_rx_mqs_segment (app);
1271
1272   fd_flags |= SESSION_FD_F_VPP_MQ_SEGMENT;
1273   fds[n_fds] = rx_mqs_seg->ssvm.fd;
1274   n_fds += 1;
1275
1276   /* Send fifo segment fd if needed */
1277   if (ssvm_type (a->segment) == SSVM_SEGMENT_MEMFD)
1278     {
1279       fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
1280       fds[n_fds] = a->segment->fd;
1281       n_fds += 1;
1282     }
1283   if (a->options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD)
1284     {
1285       fd_flags |= SESSION_FD_F_MQ_EVENTFD;
1286       fds[n_fds] = svm_msg_q_get_eventfd (a->app_evt_q);
1287       n_fds += 1;
1288     }
1289
1290   if (application_use_private_rx_mqs ())
1291     {
1292       fd_flags |= SESSION_FD_F_VPP_MQ_EVENTFD;
1293       for (i = 0; i < n_workers + 1; i++)
1294         {
1295           rx_mq = application_rx_mq_get (app, i);
1296           fds[n_fds] = svm_msg_q_get_eventfd (rx_mq);
1297           n_fds += 1;
1298         }
1299     }
1300
1301 done:
1302
1303   msg.type = APP_SAPI_MSG_TYPE_ATTACH_REPLY;
1304   rmp = &msg.attach_reply;
1305   rmp->retval = rv;
1306   if (!rv)
1307     {
1308       ctrl_thread = n_workers ? 1 : 0;
1309       rmp->app_index = a->app_index;
1310       rmp->app_mq =
1311         fifo_segment_msg_q_offset ((fifo_segment_t *) a->segment, 0);
1312       rmp->vpp_ctrl_mq = fifo_segment_msg_q_offset (rx_mqs_seg, ctrl_thread);
1313       rmp->vpp_ctrl_mq_thread = ctrl_thread;
1314       rmp->n_fds = n_fds;
1315       rmp->fd_flags = fd_flags;
1316       /* No segment name and size since we only support memfds
1317        * in this configuration */
1318       rmp->segment_handle = a->segment_handle;
1319       rmp->api_client_handle = a->api_client_index;
1320
1321       /* Update app index for socket */
1322       handle = (app_ns_api_handle_t *) & cs->private_data;
1323       app_wrk = application_get_worker (app, 0);
1324       handle->aah_app_wrk_index = app_wrk->wrk_index;
1325     }
1326
1327   clib_socket_sendmsg (cs, &msg, sizeof (msg), fds, n_fds);
1328   vec_free (a->name);
1329   vec_free (fds);
1330 }
1331
1332 void
1333 sapi_socket_close_w_handle (u32 api_handle)
1334 {
1335   app_namespace_t *app_ns = app_namespace_get (api_handle >> 16);
1336   u16 sock_index = api_handle & 0xffff;
1337   app_ns_api_handle_t *handle;
1338   clib_socket_t *cs;
1339   clib_file_t *cf;
1340
1341   cs = appns_sapi_get_socket (app_ns, sock_index);
1342   if (!cs)
1343     return;
1344
1345   handle = (app_ns_api_handle_t *) & cs->private_data;
1346   cf = clib_file_get (&file_main, handle->aah_file_index);
1347   clib_file_del (&file_main, cf);
1348
1349   clib_socket_close (cs);
1350   appns_sapi_free_socket (app_ns, cs);
1351 }
1352
1353 static void
1354 sapi_add_del_worker_handler (app_namespace_t * app_ns,
1355                              clib_socket_t * cs,
1356                              app_sapi_worker_add_del_msg_t * mp)
1357 {
1358   int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0;
1359   app_sapi_worker_add_del_reply_msg_t *rmp;
1360   app_ns_api_handle_t *handle;
1361   app_sapi_msg_t msg = { 0 };
1362   app_worker_t *app_wrk;
1363   u32 sapi_handle = -1;
1364   application_t *app;
1365   u8 fd_flags = 0;
1366
1367   app = application_get_if_valid (mp->app_index);
1368   if (!app)
1369     {
1370       rv = VNET_API_ERROR_INVALID_VALUE;
1371       goto done;
1372     }
1373
1374   sapi_handle = appns_sapi_socket_handle (app_ns, cs);
1375
1376   vnet_app_worker_add_del_args_t args = {
1377     .app_index = app->app_index,
1378     .wrk_map_index = mp->wrk_index,
1379     .api_client_index = sapi_handle,
1380     .is_add = mp->is_add
1381   };
1382   rv = vnet_app_worker_add_del (&args);
1383   if (rv)
1384     {
1385       clib_warning ("app worker add/del returned: %d", rv);
1386       goto done;
1387     }
1388
1389   if (!mp->is_add)
1390     goto done;
1391
1392   /* Send fifo segment fd if needed */
1393   if (ssvm_type (args.segment) == SSVM_SEGMENT_MEMFD)
1394     {
1395       fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
1396       fds[n_fds] = args.segment->fd;
1397       n_fds += 1;
1398     }
1399   if (application_segment_manager_properties (app)->use_mq_eventfd)
1400     {
1401       fd_flags |= SESSION_FD_F_MQ_EVENTFD;
1402       fds[n_fds] = svm_msg_q_get_eventfd (args.evt_q);
1403       n_fds += 1;
1404     }
1405
1406 done:
1407
1408   msg.type = APP_SAPI_MSG_TYPE_ADD_DEL_WORKER_REPLY;
1409   rmp = &msg.worker_add_del_reply;
1410   rmp->retval = rv;
1411   rmp->is_add = mp->is_add;
1412   rmp->api_client_handle = sapi_handle;
1413   rmp->wrk_index = args.wrk_map_index;
1414   rmp->segment_handle = args.segment_handle;
1415   if (!rv && mp->is_add)
1416     {
1417       /* No segment name and size. This supports only memfds */
1418       rmp->app_event_queue_address =
1419         fifo_segment_msg_q_offset ((fifo_segment_t *) args.segment, 0);
1420       rmp->n_fds = n_fds;
1421       rmp->fd_flags = fd_flags;
1422
1423       /* Update app index for socket */
1424       handle = (app_ns_api_handle_t *) & cs->private_data;
1425       app_wrk = application_get_worker (app, args.wrk_map_index);
1426       handle->aah_app_wrk_index = app_wrk->wrk_index;
1427     }
1428
1429   clib_socket_sendmsg (cs, &msg, sizeof (msg), fds, n_fds);
1430 }
1431
1432 /* This is a workaround for the case when session layer starts reading
1433  * the socket before the client actualy sends the data
1434  */
1435 static clib_error_t *
1436 sapi_socket_receive_wait (clib_socket_t *cs, u8 *msg, u32 msg_len)
1437 {
1438   clib_error_t *err;
1439   int n_tries = 5;
1440
1441   while (1)
1442     {
1443       err = clib_socket_recvmsg (cs, msg, msg_len, 0, 0);
1444       if (!err)
1445         break;
1446
1447       if (!n_tries)
1448         return err;
1449
1450       n_tries--;
1451       usleep (1);
1452     }
1453
1454   return err;
1455 }
1456
1457 static void
1458 sapi_add_del_cert_key_handler (app_namespace_t *app_ns, clib_socket_t *cs,
1459                                app_sapi_cert_key_add_del_msg_t *mp)
1460 {
1461   vnet_app_add_cert_key_pair_args_t _a, *a = &_a;
1462   app_sapi_cert_key_add_del_reply_msg_t *rmp;
1463   app_sapi_msg_t msg = { 0 };
1464   int rv = 0;
1465
1466   if (mp->is_add)
1467     {
1468       const u32 max_certkey_len = 2e4, max_cert_len = 1e4, max_key_len = 1e4;
1469       clib_error_t *err;
1470       u8 *certkey = 0;
1471       u32 key_len;
1472
1473       if (mp->certkey_len > max_certkey_len)
1474         {
1475           rv = SESSION_E_INVALID;
1476           goto send_reply;
1477         }
1478
1479       vec_validate (certkey, mp->certkey_len - 1);
1480
1481       err = sapi_socket_receive_wait (cs, certkey, mp->certkey_len);
1482       if (err)
1483         {
1484           clib_error_report (err);
1485           rv = SESSION_E_INVALID;
1486           goto send_reply;
1487         }
1488
1489       if (mp->cert_len > max_cert_len)
1490         {
1491           rv = SESSION_E_INVALID;
1492           goto send_reply;
1493         }
1494
1495       if (mp->certkey_len < mp->cert_len)
1496         {
1497           rv = SESSION_E_INVALID;
1498           goto send_reply;
1499         }
1500
1501       key_len = mp->certkey_len - mp->cert_len;
1502       if (key_len > max_key_len)
1503         {
1504           rv = SESSION_E_INVALID;
1505           goto send_reply;
1506         }
1507
1508       clib_memset (a, 0, sizeof (*a));
1509       a->cert = certkey;
1510       a->key = certkey + mp->cert_len;
1511       a->cert_len = mp->cert_len;
1512       a->key_len = key_len;
1513       rv = vnet_app_add_cert_key_pair (a);
1514
1515       vec_free (certkey);
1516     }
1517   else
1518     {
1519       rv = vnet_app_del_cert_key_pair (mp->index);
1520     }
1521
1522 send_reply:
1523
1524   msg.type = APP_SAPI_MSG_TYPE_ADD_DEL_CERT_KEY_REPLY;
1525   rmp = &msg.cert_key_add_del_reply;
1526   rmp->retval = rv;
1527   rmp->context = mp->context;
1528   if (!rv && mp->is_add)
1529     rmp->index = a->index;
1530
1531   clib_socket_sendmsg (cs, &msg, sizeof (msg), 0, 0);
1532 }
1533
1534 static void
1535 sapi_socket_detach (app_namespace_t * app_ns, clib_socket_t * cs)
1536 {
1537   app_ns_api_handle_t *handle;
1538   app_worker_t *app_wrk;
1539   u32 api_client_handle;
1540
1541   api_client_handle = appns_sapi_socket_handle (app_ns, cs);
1542
1543   /* Cleanup everything because app worker closed socket or crashed */
1544   handle = (app_ns_api_handle_t *) & cs->private_data;
1545   app_wrk = app_worker_get_if_valid (handle->aah_app_wrk_index);
1546   if (!app_wrk)
1547     return;
1548
1549   vnet_app_worker_add_del_args_t args = {
1550     .app_index = app_wrk->app_index,
1551     .wrk_map_index = app_wrk->wrk_map_index,
1552     .api_client_index = api_client_handle,
1553     .is_add = 0
1554   };
1555   /* Send rpc to main thread for worker barrier */
1556   vlib_rpc_call_main_thread (vnet_app_worker_add_del, (u8 *) & args,
1557                              sizeof (args));
1558 }
1559
1560 static clib_error_t *
1561 sapi_sock_read_ready (clib_file_t * cf)
1562 {
1563   app_ns_api_handle_t *handle = (app_ns_api_handle_t *) & cf->private_data;
1564   vlib_main_t *vm = vlib_get_main ();
1565   app_sapi_msg_t msg = { 0 };
1566   app_namespace_t *app_ns;
1567   clib_error_t *err = 0;
1568   clib_socket_t *cs;
1569
1570   app_ns = app_namespace_get (handle->aah_app_ns_index);
1571   cs = appns_sapi_get_socket (app_ns, handle->aah_sock_index);
1572   if (!cs)
1573     goto error;
1574
1575   err = clib_socket_recvmsg (cs, &msg, sizeof (msg), 0, 0);
1576   if (err)
1577     {
1578       clib_error_free (err);
1579       sapi_socket_detach (app_ns, cs);
1580       goto error;
1581     }
1582
1583   handle = (app_ns_api_handle_t *) & cs->private_data;
1584
1585   vlib_worker_thread_barrier_sync (vm);
1586
1587   switch (msg.type)
1588     {
1589     case APP_SAPI_MSG_TYPE_ATTACH:
1590       session_api_attach_handler (app_ns, cs, &msg.attach);
1591       break;
1592     case APP_SAPI_MSG_TYPE_ADD_DEL_WORKER:
1593       sapi_add_del_worker_handler (app_ns, cs, &msg.worker_add_del);
1594       break;
1595     case APP_SAPI_MSG_TYPE_ADD_DEL_CERT_KEY:
1596       sapi_add_del_cert_key_handler (app_ns, cs, &msg.cert_key_add_del);
1597       break;
1598     default:
1599       clib_warning ("app wrk %u unknown message type: %u",
1600                     handle->aah_app_wrk_index, msg.type);
1601       break;
1602     }
1603
1604   vlib_worker_thread_barrier_release (vm);
1605
1606 error:
1607   return 0;
1608 }
1609
1610 static clib_error_t *
1611 sapi_sock_write_ready (clib_file_t * cf)
1612 {
1613   app_ns_api_handle_t *handle = (app_ns_api_handle_t *) & cf->private_data;
1614   clib_warning ("called for app ns %u", handle->aah_app_ns_index);
1615   return 0;
1616 }
1617
1618 static clib_error_t *
1619 sapi_sock_error (clib_file_t * cf)
1620 {
1621   app_ns_api_handle_t *handle = (app_ns_api_handle_t *) & cf->private_data;
1622   app_namespace_t *app_ns;
1623   clib_socket_t *cs;
1624
1625   app_ns = app_namespace_get (handle->aah_app_ns_index);
1626   cs = appns_sapi_get_socket (app_ns, handle->aah_sock_index);
1627   if (!cs)
1628     return 0;
1629
1630   sapi_socket_detach (app_ns, cs);
1631   return 0;
1632 }
1633
1634 static clib_error_t *
1635 sapi_sock_accept_ready (clib_file_t * scf)
1636 {
1637   app_ns_api_handle_t handle = *(app_ns_api_handle_t *) & scf->private_data;
1638   app_namespace_t *app_ns;
1639   clib_file_t cf = { 0 };
1640   clib_error_t *err = 0;
1641   clib_socket_t *ccs, *scs;
1642
1643   /* Listener files point to namespace */
1644   app_ns = app_namespace_get (handle.aah_app_ns_index);
1645
1646   /*
1647    * Initialize client socket
1648    */
1649   ccs = appns_sapi_alloc_socket (app_ns);
1650
1651   /* Grab server socket after client is initialized  */
1652   scs = appns_sapi_get_socket (app_ns, handle.aah_sock_index);
1653   if (!scs)
1654     goto error;
1655
1656   err = clib_socket_accept (scs, ccs);
1657   if (err)
1658     {
1659       clib_error_report (err);
1660       goto error;
1661     }
1662
1663   cf.read_function = sapi_sock_read_ready;
1664   cf.write_function = sapi_sock_write_ready;
1665   cf.error_function = sapi_sock_error;
1666   cf.file_descriptor = ccs->fd;
1667   /* File points to app namespace and socket */
1668   handle.aah_sock_index = appns_sapi_socket_index (app_ns, ccs);
1669   cf.private_data = handle.as_u64;
1670   cf.description = format (0, "app sock conn fd: %d", ccs->fd);
1671
1672   /* Poll until we get an attach message. Socket points to file and
1673    * application that owns the socket */
1674   handle.aah_app_wrk_index = APP_INVALID_INDEX;
1675   handle.aah_file_index = clib_file_add (&file_main, &cf);
1676   ccs->private_data = handle.as_u64;
1677
1678   return err;
1679
1680 error:
1681   appns_sapi_free_socket (app_ns, ccs);
1682   return err;
1683 }
1684
1685 void
1686 appns_sapi_del_ns_socket (app_namespace_t *app_ns)
1687 {
1688   app_ns_api_handle_t *handle;
1689   clib_socket_t *cs;
1690
1691   pool_foreach (cs, app_ns->app_sockets)
1692     {
1693       handle = (app_ns_api_handle_t *) &cs->private_data;
1694       clib_file_del_by_index (&file_main, handle->aah_file_index);
1695
1696       clib_socket_close (cs);
1697       clib_socket_free (cs);
1698     }
1699   pool_free (app_ns->app_sockets);
1700 }
1701
1702 int
1703 appns_sapi_add_ns_socket (app_namespace_t * app_ns)
1704 {
1705   char *subdir = "/app_ns_sockets/";
1706   app_ns_api_handle_t *handle;
1707   clib_file_t cf = { 0 };
1708   struct stat file_stat;
1709   clib_error_t *err;
1710   clib_socket_t *cs;
1711   char dir[4096];
1712
1713   snprintf (dir, sizeof (dir), "%s%s", vlib_unix_get_runtime_dir (), subdir);
1714
1715   if (!app_ns->sock_name)
1716     app_ns->sock_name = format (0, "%s%v%c", dir, app_ns->ns_id, 0);
1717
1718   /*
1719    * Create and initialize socket to listen on
1720    */
1721   cs = appns_sapi_alloc_socket (app_ns);
1722   cs->config = (char *) vec_dup (app_ns->sock_name);
1723   cs->flags = CLIB_SOCKET_F_IS_SERVER |
1724     CLIB_SOCKET_F_ALLOW_GROUP_WRITE |
1725     CLIB_SOCKET_F_SEQPACKET | CLIB_SOCKET_F_PASSCRED;
1726
1727   if (clib_socket_prefix_get_type (cs->config) == CLIB_SOCKET_TYPE_UNIX)
1728     {
1729       err = vlib_unix_recursive_mkdir ((char *) dir);
1730       if (err)
1731         {
1732           clib_error_report (err);
1733           return VNET_API_ERROR_SYSCALL_ERROR_1;
1734         }
1735     }
1736
1737   if ((err = clib_socket_init (cs)))
1738     {
1739       clib_error_report (err);
1740       return -1;
1741     }
1742
1743   if (clib_socket_prefix_get_type (cs->config) == CLIB_SOCKET_TYPE_UNIX &&
1744       stat ((char *) app_ns->sock_name, &file_stat) == -1)
1745     return -1;
1746
1747   /*
1748    * Start polling it
1749    */
1750   cf.read_function = sapi_sock_accept_ready;
1751   cf.file_descriptor = cs->fd;
1752   /* File points to namespace */
1753   handle = (app_ns_api_handle_t *) & cf.private_data;
1754   handle->aah_app_ns_index = app_namespace_index (app_ns);
1755   handle->aah_sock_index = appns_sapi_socket_index (app_ns, cs);
1756   cf.description = format (0, "app sock listener: %s", app_ns->sock_name);
1757
1758   /* Socket points to clib file index */
1759   handle = (app_ns_api_handle_t *) & cs->private_data;
1760   handle->aah_file_index = clib_file_add (&file_main, &cf);
1761   handle->aah_app_wrk_index = APP_INVALID_INDEX;
1762
1763   return 0;
1764 }
1765
1766 #include <vnet/session/session.api.c>
1767 static clib_error_t *
1768 session_api_hookup (vlib_main_t *vm)
1769 {
1770   /*
1771    * Set up the (msg_name, crc, message-id) table
1772    */
1773   REPLY_MSG_ID_BASE = setup_message_id_table ();
1774
1775   return 0;
1776 }
1777
1778 VLIB_API_INIT_FUNCTION (session_api_hookup);
1779
1780 /*
1781  * fd.io coding-style-patch-verification: ON
1782  *
1783  * Local Variables:
1784  * eval: (c-set-style "gnu")
1785  * End:
1786  */