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