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