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