api: multiple connections per process
[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
25 #include <vnet/vnet_msg_enum.h>
26
27 #define vl_typedefs             /* define message structures */
28 #include <vnet/vnet_all_api_h.h>
29 #undef vl_typedefs
30
31 #define vl_endianfun            /* define message structures */
32 #include <vnet/vnet_all_api_h.h>
33 #undef vl_endianfun
34
35 /* instantiate all the print functions we know about */
36 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
37 #define vl_printfun
38 #include <vnet/vnet_all_api_h.h>
39 #undef vl_printfun
40
41 #include <vlibapi/api_helper_macros.h>
42
43 #define foreach_session_api_msg                                         \
44 _(MAP_ANOTHER_SEGMENT_REPLY, map_another_segment_reply)                 \
45 _(APPLICATION_ATTACH, application_attach)                               \
46 _(APP_ATTACH, app_attach)                                               \
47 _(APPLICATION_DETACH, application_detach)                               \
48 _(BIND_URI, bind_uri)                                                   \
49 _(UNBIND_URI, unbind_uri)                                               \
50 _(CONNECT_URI, connect_uri)                                             \
51 _(DISCONNECT_SESSION, disconnect_session)                               \
52 _(DISCONNECT_SESSION_REPLY, disconnect_session_reply)                   \
53 _(BIND_SOCK, bind_sock)                                                 \
54 _(UNBIND_SOCK, unbind_sock)                                             \
55 _(CONNECT_SOCK, connect_sock)                                           \
56 _(SESSION_ENABLE_DISABLE, session_enable_disable)                       \
57 _(APP_NAMESPACE_ADD_DEL, app_namespace_add_del)                         \
58 _(SESSION_RULE_ADD_DEL, session_rule_add_del)                           \
59 _(SESSION_RULES_DUMP, session_rules_dump)                               \
60 _(APPLICATION_TLS_CERT_ADD, application_tls_cert_add)                   \
61 _(APPLICATION_TLS_KEY_ADD, application_tls_key_add)                     \
62 _(APP_ADD_CERT_KEY_PAIR, app_add_cert_key_pair)                         \
63 _(APP_DEL_CERT_KEY_PAIR, app_del_cert_key_pair)                         \
64 _(APP_WORKER_ADD_DEL, app_worker_add_del)                               \
65
66 static int
67 session_send_fds (vl_api_registration_t * reg, int fds[], int n_fds)
68 {
69   clib_error_t *error;
70   if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
71     {
72       clib_warning ("can't send memfd fd");
73       return -1;
74     }
75   error = vl_api_send_fd_msg (reg, fds, n_fds);
76   if (error)
77     {
78       clib_error_report (error);
79       return -1;
80     }
81   return 0;
82 }
83
84 static int
85 send_add_segment_callback (u32 api_client_index, u64 segment_handle)
86 {
87   int fds[SESSION_N_FD_TYPE], n_fds = 0;
88   vl_api_map_another_segment_t *mp;
89   vl_api_registration_t *reg;
90   fifo_segment_t *fs;
91   ssvm_private_t *sp;
92   u8 fd_flags = 0;
93
94   reg = vl_mem_api_client_index_to_registration (api_client_index);
95   if (!reg)
96     {
97       clib_warning ("no api registration for client: %u", api_client_index);
98       return -1;
99     }
100
101   fs = segment_manager_get_segment_w_handle (segment_handle);
102   sp = &fs->ssvm;
103   if (ssvm_type (sp) == SSVM_SEGMENT_MEMFD)
104     {
105       if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
106         {
107           clib_warning ("can't send memfd fd");
108           return -1;
109         }
110
111       fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
112       fds[n_fds] = sp->fd;
113       n_fds += 1;
114     }
115
116   mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
117   clib_memset (mp, 0, sizeof (*mp));
118   mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_MAP_ANOTHER_SEGMENT);
119   mp->segment_size = sp->ssvm_size;
120   mp->fd_flags = fd_flags;
121   mp->segment_handle = clib_host_to_net_u64 (segment_handle);
122   strncpy ((char *) mp->segment_name, (char *) sp->name,
123            sizeof (mp->segment_name) - 1);
124
125   vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
126
127   if (n_fds)
128     return session_send_fds (reg, fds, n_fds);
129
130   return 0;
131 }
132
133 static int
134 send_del_segment_callback (u32 api_client_index, u64 segment_handle)
135 {
136   vl_api_unmap_segment_t *mp;
137   vl_api_registration_t *reg;
138
139   reg = vl_mem_api_client_index_to_registration (api_client_index);
140   if (!reg)
141     {
142       clib_warning ("no registration: %u", api_client_index);
143       return -1;
144     }
145
146   mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
147   clib_memset (mp, 0, sizeof (*mp));
148   mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_UNMAP_SEGMENT);
149   mp->segment_handle = clib_host_to_net_u64 (segment_handle);
150   vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
151
152   return 0;
153 }
154
155 static int
156 mq_try_lock_and_alloc_msg (svm_msg_q_t * app_mq, svm_msg_q_msg_t * msg)
157 {
158   int rv;
159   u8 try = 0;
160   while (try < 100)
161     {
162       rv = svm_msg_q_lock_and_alloc_msg_w_ring (app_mq,
163                                                 SESSION_MQ_CTRL_EVT_RING,
164                                                 SVM_Q_NOWAIT, msg);
165       if (!rv)
166         return 0;
167       try++;
168       usleep (1);
169     }
170   clib_warning ("failed to alloc msg");
171   return -1;
172 }
173
174 static int
175 mq_send_session_accepted_cb (session_t * s)
176 {
177   app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
178   svm_msg_q_msg_t _msg, *msg = &_msg;
179   svm_msg_q_t *vpp_queue, *app_mq;
180   session_t *listener;
181   session_accepted_msg_t *mp;
182   session_event_t *evt;
183   application_t *app;
184
185   app = application_get (app_wrk->app_index);
186   app_mq = app_wrk->event_queue;
187   if (mq_try_lock_and_alloc_msg (app_mq, msg))
188     return -1;
189
190   evt = svm_msg_q_msg_data (app_mq, msg);
191   clib_memset (evt, 0, sizeof (*evt));
192   evt->event_type = SESSION_CTRL_EVT_ACCEPTED;
193   mp = (session_accepted_msg_t *) evt->data;
194   clib_memset (mp, 0, sizeof (*mp));
195   mp->context = app->app_index;
196   mp->server_rx_fifo = pointer_to_uword (s->rx_fifo);
197   mp->server_tx_fifo = pointer_to_uword (s->tx_fifo);
198   mp->segment_handle = session_segment_handle (s);
199
200   if (session_has_transport (s))
201     {
202       listener = listen_session_get_from_handle (s->listener_handle);
203       mp->listener_handle = app_listen_session_handle (listener);
204       if (application_is_proxy (app))
205         {
206           listener =
207             app_worker_first_listener (app_wrk, session_get_fib_proto (s),
208                                        session_get_transport_proto (s));
209           if (listener)
210             mp->listener_handle = listen_session_get_handle (listener);
211         }
212       vpp_queue = session_main_get_vpp_event_queue (s->thread_index);
213       mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
214       mp->handle = session_handle (s);
215
216       session_get_endpoint (s, &mp->rmt, 0 /* is_lcl */ );
217     }
218   else
219     {
220       ct_connection_t *ct;
221
222       ct = (ct_connection_t *) session_get_transport (s);
223       listener = listen_session_get_from_handle (s->listener_handle);
224       mp->listener_handle = app_listen_session_handle (listener);
225       mp->rmt.is_ip4 = session_type_is_ip4 (listener->session_type);
226       mp->rmt.port = ct->c_rmt_port;
227       mp->handle = session_handle (s);
228       vpp_queue = session_main_get_vpp_event_queue (0);
229       mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
230     }
231   svm_msg_q_add_and_unlock (app_mq, msg);
232
233   return 0;
234 }
235
236 static inline void
237 mq_send_session_close_evt (app_worker_t * app_wrk, session_handle_t sh,
238                            session_evt_type_t evt_type)
239 {
240   svm_msg_q_msg_t _msg, *msg = &_msg;
241   session_disconnected_msg_t *mp;
242   svm_msg_q_t *app_mq;
243   session_event_t *evt;
244
245   app_mq = app_wrk->event_queue;
246   if (mq_try_lock_and_alloc_msg (app_mq, msg))
247     return;
248   evt = svm_msg_q_msg_data (app_mq, msg);
249   clib_memset (evt, 0, sizeof (*evt));
250   evt->event_type = evt_type;
251   mp = (session_disconnected_msg_t *) evt->data;
252   mp->handle = sh;
253   mp->context = app_wrk->api_client_index;
254   svm_msg_q_add_and_unlock (app_mq, msg);
255 }
256
257 static inline void
258 mq_notify_close_subscribers (u32 app_index, session_handle_t sh,
259                              svm_fifo_t * f, session_evt_type_t evt_type)
260 {
261   app_worker_t *app_wrk;
262   application_t *app;
263   int i;
264
265   app = application_get (app_index);
266   if (!app)
267     return;
268
269   for (i = 0; i < f->n_subscribers; i++)
270     {
271       if (!(app_wrk = application_get_worker (app, f->subscribers[i])))
272         continue;
273       mq_send_session_close_evt (app_wrk, sh, SESSION_CTRL_EVT_DISCONNECTED);
274     }
275 }
276
277 static void
278 mq_send_session_disconnected_cb (session_t * s)
279 {
280   app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
281   session_handle_t sh = session_handle (s);
282
283   mq_send_session_close_evt (app_wrk, session_handle (s),
284                              SESSION_CTRL_EVT_DISCONNECTED);
285
286   if (svm_fifo_n_subscribers (s->rx_fifo))
287     mq_notify_close_subscribers (app_wrk->app_index, sh, s->rx_fifo,
288                                  SESSION_CTRL_EVT_DISCONNECTED);
289 }
290
291 static void
292 mq_send_session_reset_cb (session_t * s)
293 {
294   app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
295   session_handle_t sh = session_handle (s);
296
297   mq_send_session_close_evt (app_wrk, sh, SESSION_CTRL_EVT_RESET);
298
299   if (svm_fifo_n_subscribers (s->rx_fifo))
300     mq_notify_close_subscribers (app_wrk->app_index, sh, s->rx_fifo,
301                                  SESSION_CTRL_EVT_RESET);
302 }
303
304 int
305 mq_send_session_connected_cb (u32 app_wrk_index, u32 api_context,
306                               session_t * s, u8 is_fail)
307 {
308   svm_msg_q_msg_t _msg, *msg = &_msg;
309   session_connected_msg_t *mp;
310   svm_msg_q_t *vpp_mq, *app_mq;
311   transport_connection_t *tc;
312   app_worker_t *app_wrk;
313   session_event_t *evt;
314
315   app_wrk = app_worker_get (app_wrk_index);
316   app_mq = app_wrk->event_queue;
317   if (!app_mq)
318     {
319       clib_warning ("app %u with api index: %u not attached",
320                     app_wrk->app_index, app_wrk->api_client_index);
321       return -1;
322     }
323
324   if (mq_try_lock_and_alloc_msg (app_mq, msg))
325     return -1;
326
327   evt = svm_msg_q_msg_data (app_mq, msg);
328   clib_memset (evt, 0, sizeof (*evt));
329   evt->event_type = SESSION_CTRL_EVT_CONNECTED;
330   mp = (session_connected_msg_t *) evt->data;
331   clib_memset (mp, 0, sizeof (*mp));
332   mp->context = api_context;
333
334   if (is_fail)
335     goto done;
336
337   if (session_has_transport (s))
338     {
339       tc = session_get_transport (s);
340       if (!tc)
341         {
342           is_fail = 1;
343           goto done;
344         }
345
346       vpp_mq = session_main_get_vpp_event_queue (s->thread_index);
347       mp->handle = session_handle (s);
348       mp->vpp_event_queue_address = pointer_to_uword (vpp_mq);
349
350       session_get_endpoint (s, &mp->lcl, 1 /* is_lcl */ );
351
352       mp->server_rx_fifo = pointer_to_uword (s->rx_fifo);
353       mp->server_tx_fifo = pointer_to_uword (s->tx_fifo);
354       mp->segment_handle = session_segment_handle (s);
355     }
356   else
357     {
358       ct_connection_t *cct;
359       session_t *ss;
360
361       cct = (ct_connection_t *) session_get_transport (s);
362       mp->handle = session_handle (s);
363       mp->lcl.port = cct->c_lcl_port;
364       mp->lcl.is_ip4 = cct->c_is_ip4;
365       vpp_mq = session_main_get_vpp_event_queue (0);
366       mp->vpp_event_queue_address = pointer_to_uword (vpp_mq);
367       mp->server_rx_fifo = pointer_to_uword (s->rx_fifo);
368       mp->server_tx_fifo = pointer_to_uword (s->tx_fifo);
369       mp->segment_handle = session_segment_handle (s);
370       ss = ct_session_get_peer (s);
371       mp->ct_rx_fifo = pointer_to_uword (ss->tx_fifo);
372       mp->ct_tx_fifo = pointer_to_uword (ss->rx_fifo);
373       mp->ct_segment_handle = session_segment_handle (ss);
374     }
375
376 done:
377   mp->retval = is_fail ?
378     clib_host_to_net_u32 (VNET_API_ERROR_SESSION_CONNECT) : 0;
379
380   svm_msg_q_add_and_unlock (app_mq, msg);
381   return 0;
382 }
383
384 int
385 mq_send_session_bound_cb (u32 app_wrk_index, u32 api_context,
386                           session_handle_t handle, int rv)
387 {
388   svm_msg_q_msg_t _msg, *msg = &_msg;
389   svm_msg_q_t *app_mq, *vpp_evt_q;
390   transport_endpoint_t tep;
391   session_bound_msg_t *mp;
392   app_worker_t *app_wrk;
393   session_event_t *evt;
394   app_listener_t *al;
395   session_t *ls = 0;
396   app_wrk = app_worker_get (app_wrk_index);
397   app_mq = app_wrk->event_queue;
398   if (!app_mq)
399     {
400       clib_warning ("app %u with api index: %u not attached",
401                     app_wrk->app_index, app_wrk->api_client_index);
402       return -1;
403     }
404
405   if (mq_try_lock_and_alloc_msg (app_mq, msg))
406     return -1;
407
408   evt = svm_msg_q_msg_data (app_mq, msg);
409   clib_memset (evt, 0, sizeof (*evt));
410   evt->event_type = SESSION_CTRL_EVT_BOUND;
411   mp = (session_bound_msg_t *) evt->data;
412   mp->context = api_context;
413
414   if (rv)
415     goto done;
416
417   mp->handle = handle;
418   al = app_listener_get_w_handle (handle);
419   if (al->session_index != SESSION_INVALID_INDEX)
420     ls = app_listener_get_session (al);
421   else
422     ls = app_listener_get_local_session (al);
423
424   session_get_endpoint (ls, &tep, 1 /* is_lcl */ );
425   mp->lcl_port = tep.port;
426   mp->lcl_is_ip4 = tep.is_ip4;
427   clib_memcpy_fast (mp->lcl_ip, &tep.ip, sizeof (tep.ip));
428
429   vpp_evt_q = session_main_get_vpp_event_queue (0);
430   mp->vpp_evt_q = pointer_to_uword (vpp_evt_q);
431
432   if (session_transport_service_type (ls) == TRANSPORT_SERVICE_CL)
433     {
434       mp->rx_fifo = pointer_to_uword (ls->rx_fifo);
435       mp->tx_fifo = pointer_to_uword (ls->tx_fifo);
436     }
437
438 done:
439   mp->retval = rv;
440   svm_msg_q_add_and_unlock (app_mq, msg);
441   return 0;
442 }
443
444 void
445 mq_send_unlisten_reply (app_worker_t * app_wrk, session_handle_t sh,
446                         u32 context, int rv)
447 {
448   svm_msg_q_msg_t _msg, *msg = &_msg;
449   session_unlisten_reply_msg_t *ump;
450   svm_msg_q_t *app_mq;
451   session_event_t *evt;
452
453   app_mq = app_wrk->event_queue;
454   if (mq_try_lock_and_alloc_msg (app_mq, msg))
455     return;
456
457   evt = svm_msg_q_msg_data (app_mq, msg);
458   clib_memset (evt, 0, sizeof (*evt));
459   evt->event_type = SESSION_CTRL_EVT_UNLISTEN_REPLY;
460   ump = (session_unlisten_reply_msg_t *) evt->data;
461   ump->context = context;
462   ump->handle = sh;
463   ump->retval = rv;
464   svm_msg_q_add_and_unlock (app_mq, msg);
465 }
466
467 static void
468 mq_send_session_migrate_cb (session_t * s, session_handle_t new_sh)
469 {
470   clib_warning ("not supported");
471 }
472
473 static session_cb_vft_t session_mq_cb_vft = {
474   .session_accept_callback = mq_send_session_accepted_cb,
475   .session_disconnect_callback = mq_send_session_disconnected_cb,
476   .session_connected_callback = mq_send_session_connected_cb,
477   .session_reset_callback = mq_send_session_reset_cb,
478   .session_migrate_callback = mq_send_session_migrate_cb,
479   .add_segment_callback = send_add_segment_callback,
480   .del_segment_callback = send_del_segment_callback,
481 };
482
483 static void
484 vl_api_session_enable_disable_t_handler (vl_api_session_enable_disable_t * mp)
485 {
486   vl_api_session_enable_disable_reply_t *rmp;
487   vlib_main_t *vm = vlib_get_main ();
488   int rv = 0;
489
490   vnet_session_enable_disable (vm, mp->is_enable);
491   REPLY_MACRO (VL_API_SESSION_ENABLE_DISABLE_REPLY);
492 }
493
494 /* ### WILL BE DEPRECATED POST 20.01 ### */
495 static void
496 vl_api_application_attach_t_handler (vl_api_application_attach_t * mp)
497 {
498   int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0;
499   vl_api_application_attach_reply_t *rmp;
500   ssvm_private_t *segp, *evt_q_segment;
501   vnet_app_attach_args_t _a, *a = &_a;
502   vl_api_registration_t *reg;
503   u8 fd_flags = 0;
504
505   reg = vl_api_client_index_to_registration (mp->client_index);
506   if (!reg)
507     return;
508
509   if (session_main_is_enabled () == 0)
510     {
511       rv = VNET_API_ERROR_FEATURE_DISABLED;
512       goto done;
513     }
514
515   STATIC_ASSERT (sizeof (u64) * APP_OPTIONS_N_OPTIONS <=
516                  sizeof (mp->options),
517                  "Out of options, fix api message definition");
518
519   clib_memset (a, 0, sizeof (*a));
520   a->api_client_index = mp->client_index;
521   a->options = mp->options;
522   a->session_cb_vft = &session_mq_cb_vft;
523   if (mp->namespace_id_len > 64)
524     {
525       rv = VNET_API_ERROR_INVALID_VALUE;
526       goto done;
527     }
528
529   if (mp->namespace_id_len)
530     {
531       vec_validate (a->namespace_id, mp->namespace_id_len - 1);
532       clib_memcpy_fast (a->namespace_id, mp->namespace_id,
533                         mp->namespace_id_len);
534     }
535
536   if ((rv = vnet_application_attach (a)))
537     {
538       clib_warning ("attach returned: %d", rv);
539       vec_free (a->namespace_id);
540       goto done;
541     }
542   vec_free (a->namespace_id);
543
544   /* Send event queues segment */
545   if ((evt_q_segment = session_main_get_evt_q_segment ()))
546     {
547       fd_flags |= SESSION_FD_F_VPP_MQ_SEGMENT;
548       fds[n_fds] = evt_q_segment->fd;
549       n_fds += 1;
550     }
551   /* Send fifo segment fd if needed */
552   if (ssvm_type (a->segment) == SSVM_SEGMENT_MEMFD)
553     {
554       fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
555       fds[n_fds] = a->segment->fd;
556       n_fds += 1;
557     }
558   if (a->options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD)
559     {
560       fd_flags |= SESSION_FD_F_MQ_EVENTFD;
561       fds[n_fds] = svm_msg_q_get_producer_eventfd (a->app_evt_q);
562       n_fds += 1;
563     }
564
565 done:
566
567   /* *INDENT-OFF* */
568   REPLY_MACRO2 (VL_API_APPLICATION_ATTACH_REPLY, ({
569     if (!rv)
570       {
571         segp = a->segment;
572         rmp->app_index = clib_host_to_net_u32 (a->app_index);
573         rmp->segment_name_length = 0;
574         rmp->segment_size = segp->ssvm_size;
575         if (vec_len (segp->name))
576           {
577             memcpy (rmp->segment_name, segp->name, vec_len (segp->name));
578             rmp->segment_name_length = vec_len (segp->name);
579           }
580         rmp->app_event_queue_address = pointer_to_uword (a->app_evt_q);
581         rmp->n_fds = n_fds;
582         rmp->fd_flags = fd_flags;
583         rmp->segment_handle = clib_host_to_net_u64 (a->segment_handle);
584       }
585   }));
586   /* *INDENT-ON* */
587
588   if (n_fds)
589     session_send_fds (reg, fds, n_fds);
590 }
591
592 static void
593 vl_api_app_attach_t_handler (vl_api_app_attach_t * mp)
594 {
595   int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0;
596   vl_api_app_attach_reply_t *rmp;
597   ssvm_private_t *segp, *evt_q_segment;
598   vnet_app_attach_args_t _a, *a = &_a;
599   u8 fd_flags = 0, ctrl_thread;
600   vl_api_registration_t *reg;
601   svm_msg_q_t *ctrl_mq;
602
603   reg = vl_api_client_index_to_registration (mp->client_index);
604   if (!reg)
605     return;
606
607   if (session_main_is_enabled () == 0)
608     {
609       rv = VNET_API_ERROR_FEATURE_DISABLED;
610       goto done;
611     }
612
613   STATIC_ASSERT (sizeof (u64) * APP_OPTIONS_N_OPTIONS <=
614                  sizeof (mp->options),
615                  "Out of options, fix api message definition");
616
617   clib_memset (a, 0, sizeof (*a));
618   a->api_client_index = mp->client_index;
619   a->options = mp->options;
620   a->session_cb_vft = &session_mq_cb_vft;
621   if (mp->namespace_id_len > 64)
622     {
623       rv = VNET_API_ERROR_INVALID_VALUE;
624       goto done;
625     }
626
627   if (mp->namespace_id_len)
628     {
629       vec_validate (a->namespace_id, mp->namespace_id_len - 1);
630       clib_memcpy_fast (a->namespace_id, mp->namespace_id,
631                         mp->namespace_id_len);
632     }
633
634   if ((rv = vnet_application_attach (a)))
635     {
636       clib_warning ("attach returned: %d", rv);
637       vec_free (a->namespace_id);
638       goto done;
639     }
640   vec_free (a->namespace_id);
641
642   /* Send event queues segment */
643   if ((evt_q_segment = session_main_get_evt_q_segment ()))
644     {
645       fd_flags |= SESSION_FD_F_VPP_MQ_SEGMENT;
646       fds[n_fds] = evt_q_segment->fd;
647       n_fds += 1;
648     }
649   /* Send fifo segment fd if needed */
650   if (ssvm_type (a->segment) == SSVM_SEGMENT_MEMFD)
651     {
652       fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
653       fds[n_fds] = a->segment->fd;
654       n_fds += 1;
655     }
656   if (a->options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD)
657     {
658       fd_flags |= SESSION_FD_F_MQ_EVENTFD;
659       fds[n_fds] = svm_msg_q_get_producer_eventfd (a->app_evt_q);
660       n_fds += 1;
661     }
662
663 done:
664   /* *INDENT-OFF* */
665   REPLY_MACRO2 (VL_API_APP_ATTACH_REPLY, ({
666     if (!rv)
667       {
668         ctrl_thread = vlib_num_workers () ? 1 : 0;
669         ctrl_mq = session_main_get_vpp_event_queue (ctrl_thread);
670         segp = a->segment;
671         rmp->app_index = clib_host_to_net_u32 (a->app_index);
672         rmp->app_mq = pointer_to_uword (a->app_evt_q);
673         rmp->vpp_ctrl_mq = pointer_to_uword (ctrl_mq);
674         rmp->vpp_ctrl_mq_thread = ctrl_thread;
675         rmp->n_fds = n_fds;
676         rmp->fd_flags = fd_flags;
677         if (vec_len (segp->name))
678           {
679             memcpy (rmp->segment_name, segp->name, vec_len (segp->name));
680             rmp->segment_name_length = vec_len (segp->name);
681           }
682         rmp->segment_size = segp->ssvm_size;
683         rmp->segment_handle = clib_host_to_net_u64 (a->segment_handle);
684       }
685   }));
686   /* *INDENT-ON* */
687
688   if (n_fds)
689     session_send_fds (reg, fds, n_fds);
690 }
691
692 /* ### WILL BE DEPRECATED POST 20.01 ### */
693 static void
694 vl_api_application_detach_t_handler (vl_api_application_detach_t * mp)
695 {
696   vl_api_application_detach_reply_t *rmp;
697   int rv = VNET_API_ERROR_INVALID_VALUE_2;
698   vnet_app_detach_args_t _a, *a = &_a;
699   application_t *app;
700
701   if (session_main_is_enabled () == 0)
702     {
703       rv = VNET_API_ERROR_FEATURE_DISABLED;
704       goto done;
705     }
706
707   app = application_lookup (mp->client_index);
708   if (app)
709     {
710       a->app_index = app->app_index;
711       a->api_client_index = mp->client_index;
712       rv = vnet_application_detach (a);
713     }
714
715 done:
716   REPLY_MACRO (VL_API_APPLICATION_DETACH_REPLY);
717 }
718
719 /* ### WILL BE DEPRECATED POST 20.01 ### */
720 static void
721 vl_api_bind_uri_t_handler (vl_api_bind_uri_t * mp)
722 {
723   vl_api_bind_uri_reply_t *rmp;
724   vnet_listen_args_t _a, *a = &_a;
725   application_t *app = 0;
726   app_worker_t *app_wrk;
727   int rv;
728
729   if (session_main_is_enabled () == 0)
730     {
731       rv = VNET_API_ERROR_FEATURE_DISABLED;
732       goto done;
733     }
734
735   app = application_lookup (mp->client_index);
736   if (app)
737     {
738       clib_memset (a, 0, sizeof (*a));
739       a->uri = (char *) mp->uri;
740       a->app_index = app->app_index;
741       rv = vnet_bind_uri (a);
742     }
743   else
744     {
745       rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
746     }
747
748 done:
749
750   REPLY_MACRO (VL_API_BIND_URI_REPLY);
751
752   if (app)
753     {
754       app_wrk = application_get_worker (app, 0);
755       mq_send_session_bound_cb (app_wrk->wrk_index, mp->context, a->handle,
756                                 rv);
757     }
758 }
759
760 /* ### WILL BE DEPRECATED POST 20.01 ### */
761 static void
762 vl_api_unbind_uri_t_handler (vl_api_unbind_uri_t * mp)
763 {
764   vl_api_unbind_uri_reply_t *rmp;
765   application_t *app;
766   vnet_unlisten_args_t _a, *a = &_a;
767   int rv;
768
769   if (session_main_is_enabled () == 0)
770     {
771       rv = VNET_API_ERROR_FEATURE_DISABLED;
772       goto done;
773     }
774
775   app = application_lookup (mp->client_index);
776   if (app)
777     {
778       a->uri = (char *) mp->uri;
779       a->app_index = app->app_index;
780       a->wrk_map_index = 0;
781       rv = vnet_unbind_uri (a);
782     }
783   else
784     {
785       rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
786     }
787
788 done:
789   REPLY_MACRO (VL_API_UNBIND_URI_REPLY);
790 }
791
792 /* ### WILL BE DEPRECATED POST 20.01 ### */
793 static void
794 vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp)
795 {
796   vl_api_connect_uri_reply_t *rmp;
797   vnet_connect_args_t _a, *a = &_a;
798   application_t *app;
799   int rv = 0;
800
801   if (session_main_is_enabled () == 0)
802     {
803       rv = VNET_API_ERROR_FEATURE_DISABLED;
804       goto done;
805     }
806
807   app = application_lookup (mp->client_index);
808   if (app)
809     {
810       clib_memset (a, 0, sizeof (*a));
811       a->uri = (char *) mp->uri;
812       a->api_context = mp->context;
813       a->app_index = app->app_index;
814       if ((rv = vnet_connect_uri (a)))
815         clib_warning ("connect_uri returned: %d", rv);
816     }
817   else
818     {
819       rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
820     }
821
822   /*
823    * Don't reply to stream (tcp) connects. The reply will come once
824    * the connection is established. In case of the redirects, the reply
825    * will come from the server app.
826    */
827   if (rv == 0)
828     return;
829
830 done:
831   REPLY_MACRO (VL_API_CONNECT_URI_REPLY);
832 }
833
834 /* ### WILL BE DEPRECATED POST 20.01 ### */
835 static void
836 vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
837 {
838   vl_api_disconnect_session_reply_t *rmp;
839   vnet_disconnect_args_t _a, *a = &_a;
840   application_t *app;
841   int rv = 0;
842
843   if (session_main_is_enabled () == 0)
844     {
845       rv = VNET_API_ERROR_FEATURE_DISABLED;
846       goto done;
847     }
848
849   app = application_lookup (mp->client_index);
850   if (app)
851     {
852       a->handle = mp->handle;
853       a->app_index = app->app_index;
854       rv = vnet_disconnect_session (a);
855     }
856   else
857     {
858       rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
859     }
860
861 done:
862   REPLY_MACRO2 (VL_API_DISCONNECT_SESSION_REPLY, rmp->handle = mp->handle);
863 }
864
865 /* ### WILL BE DEPRECATED POST 20.01 ### */
866 static void
867 vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
868                                            mp)
869 {
870   vnet_disconnect_args_t _a, *a = &_a;
871   application_t *app;
872
873   /* Client objected to disconnecting the session, log and continue */
874   if (mp->retval)
875     {
876       clib_warning ("client retval %d", mp->retval);
877       return;
878     }
879
880   /* Disconnect has been confirmed. Confirm close to transport */
881   app = application_lookup (mp->context);
882   if (app)
883     {
884       a->handle = mp->handle;
885       a->app_index = app->app_index;
886       vnet_disconnect_session (a);
887     }
888 }
889
890 static void
891 vl_api_map_another_segment_reply_t_handler (vl_api_map_another_segment_reply_t
892                                             * mp)
893 {
894   clib_warning ("not implemented");
895 }
896
897 /* ### WILL BE DEPRECATED POST 20.01 ### */
898 static void
899 vl_api_bind_sock_t_handler (vl_api_bind_sock_t * mp)
900 {
901   vnet_listen_args_t _a, *a = &_a;
902   vl_api_bind_sock_reply_t *rmp;
903   application_t *app = 0;
904   app_worker_t *app_wrk;
905   ip46_address_t *ip46;
906   int rv = 0;
907
908   if (session_main_is_enabled () == 0)
909     {
910       rv = VNET_API_ERROR_FEATURE_DISABLED;
911       goto done;
912     }
913
914   app = application_lookup (mp->client_index);
915   if (!app)
916     {
917       rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
918       goto done;
919     }
920
921   ip46 = (ip46_address_t *) mp->ip;
922   clib_memset (a, 0, sizeof (*a));
923   a->sep.is_ip4 = mp->is_ip4;
924   a->sep.ip = *ip46;
925   a->sep.port = mp->port;
926   a->sep.fib_index = mp->vrf;
927   a->sep.sw_if_index = ENDPOINT_INVALID_INDEX;
928   a->sep.transport_proto = mp->proto;
929   a->app_index = app->app_index;
930   a->wrk_map_index = mp->wrk_index;
931
932   if ((rv = vnet_listen (a)))
933     clib_warning ("listen returned: %d", rv);
934
935 done:
936   /* Actual reply sent only over mq */
937   REPLY_MACRO (VL_API_BIND_SOCK_REPLY);
938
939   if (app)
940     {
941       app_wrk = application_get_worker (app, mp->wrk_index);
942       mq_send_session_bound_cb (app_wrk->wrk_index, mp->context, a->handle,
943                                 rv);
944     }
945 }
946
947 /* ### WILL BE DEPRECATED POST 20.01 ### */
948 static void
949 vl_api_unbind_sock_t_handler (vl_api_unbind_sock_t * mp)
950 {
951   vl_api_unbind_sock_reply_t *rmp;
952   vnet_unlisten_args_t _a, *a = &_a;
953   app_worker_t *app_wrk;
954   application_t *app = 0;
955   int rv = 0;
956
957   if (session_main_is_enabled () == 0)
958     {
959       rv = VNET_API_ERROR_FEATURE_DISABLED;
960       goto done;
961     }
962
963   app = application_lookup (mp->client_index);
964   if (app)
965     {
966       a->app_index = app->app_index;
967       a->handle = mp->handle;
968       a->wrk_map_index = mp->wrk_index;
969       if ((rv = vnet_unlisten (a)))
970         clib_warning ("unlisten returned: %d", rv);
971     }
972   else
973     {
974       rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
975     }
976
977 done:
978   REPLY_MACRO (VL_API_UNBIND_SOCK_REPLY);
979
980   if (!app)
981     return;
982
983   app_wrk = application_get_worker (app, a->wrk_map_index);
984   if (!app_wrk)
985     return;
986
987   mq_send_unlisten_reply (app_wrk, mp->handle, mp->context, rv);
988 }
989
990 /* ### WILL BE DEPRECATED POST 20.01 ### */
991 static void
992 vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
993 {
994   vl_api_connect_sock_reply_t *rmp;
995   vnet_connect_args_t _a, *a = &_a;
996   application_t *app = 0;
997   int rv = 0;
998
999   if (session_main_is_enabled () == 0)
1000     {
1001       rv = VNET_API_ERROR_FEATURE_DISABLED;
1002       goto done;
1003     }
1004
1005   app = application_lookup (mp->client_index);
1006   if (app)
1007     {
1008       svm_queue_t *client_q;
1009       ip46_address_t *ip46 = (ip46_address_t *) mp->ip;
1010
1011       clib_memset (a, 0, sizeof (*a));
1012       client_q = vl_api_client_index_to_input_queue (mp->client_index);
1013       mp->client_queue_address = pointer_to_uword (client_q);
1014       a->sep.is_ip4 = mp->is_ip4;
1015       a->sep.ip = *ip46;
1016       a->sep.port = mp->port;
1017       a->sep.transport_proto = mp->proto;
1018       a->sep.peer.fib_index = mp->vrf;
1019       a->sep.peer.sw_if_index = ENDPOINT_INVALID_INDEX;
1020       a->sep_ext.parent_handle = mp->parent_handle;
1021       if (mp->hostname_len)
1022         {
1023           vec_validate (a->sep_ext.hostname, mp->hostname_len - 1);
1024           clib_memcpy_fast (a->sep_ext.hostname, mp->hostname,
1025                             mp->hostname_len);
1026         }
1027       a->api_context = mp->context;
1028       a->app_index = app->app_index;
1029       a->wrk_map_index = mp->wrk_index;
1030       if ((rv = vnet_connect (a)))
1031         clib_warning ("connect returned: %U", format_vnet_api_errno, rv);
1032       vec_free (a->sep_ext.hostname);
1033     }
1034   else
1035     {
1036       rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1037     }
1038
1039   if (rv == 0)
1040     return;
1041
1042   /* Got some error, relay it */
1043
1044 done:
1045   REPLY_MACRO (VL_API_CONNECT_SOCK_REPLY);
1046
1047   if (app)
1048     {
1049       app_worker_t *app_wrk = application_get_worker (app, mp->wrk_index);
1050       mq_send_session_connected_cb (app_wrk->wrk_index, mp->context, 0, 1);
1051     }
1052 }
1053
1054 static void
1055 vl_api_app_worker_add_del_t_handler (vl_api_app_worker_add_del_t * mp)
1056 {
1057   int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0;
1058   vl_api_app_worker_add_del_reply_t *rmp;
1059   vl_api_registration_t *reg;
1060   application_t *app;
1061   u8 fd_flags = 0;
1062
1063   if (session_main_is_enabled () == 0)
1064     {
1065       rv = VNET_API_ERROR_FEATURE_DISABLED;
1066       goto done;
1067     }
1068
1069   reg = vl_api_client_index_to_registration (mp->client_index);
1070   if (!reg)
1071     return;
1072
1073   app = application_get_if_valid (clib_net_to_host_u32 (mp->app_index));
1074   if (!app)
1075     {
1076       rv = VNET_API_ERROR_INVALID_VALUE;
1077       goto done;
1078     }
1079
1080   vnet_app_worker_add_del_args_t args = {
1081     .app_index = app->app_index,
1082     .wrk_map_index = clib_net_to_host_u32 (mp->wrk_index),
1083     .api_client_index = mp->client_index,
1084     .is_add = mp->is_add
1085   };
1086   rv = vnet_app_worker_add_del (&args);
1087   if (rv)
1088     {
1089       clib_warning ("app worker add/del returned: %d", rv);
1090       goto done;
1091     }
1092
1093   if (!mp->is_add)
1094     goto done;
1095
1096   /* Send fifo segment fd if needed */
1097   if (ssvm_type (args.segment) == SSVM_SEGMENT_MEMFD)
1098     {
1099       fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
1100       fds[n_fds] = args.segment->fd;
1101       n_fds += 1;
1102     }
1103   if (application_segment_manager_properties (app)->use_mq_eventfd)
1104     {
1105       fd_flags |= SESSION_FD_F_MQ_EVENTFD;
1106       fds[n_fds] = svm_msg_q_get_producer_eventfd (args.evt_q);
1107       n_fds += 1;
1108     }
1109
1110   /* *INDENT-OFF* */
1111 done:
1112   REPLY_MACRO2 (VL_API_APP_WORKER_ADD_DEL_REPLY, ({
1113     rmp->is_add = mp->is_add;
1114     rmp->wrk_index = clib_host_to_net_u32 (args.wrk_map_index);
1115     rmp->segment_handle = clib_host_to_net_u64 (args.segment_handle);
1116     if (!rv && mp->is_add)
1117       {
1118         if (vec_len (args.segment->name))
1119           {
1120             memcpy (rmp->segment_name, args.segment->name,
1121                     vec_len (args.segment->name));
1122             rmp->segment_name_length = vec_len (args.segment->name);
1123           }
1124         rmp->app_event_queue_address = pointer_to_uword (args.evt_q);
1125         rmp->n_fds = n_fds;
1126         rmp->fd_flags = fd_flags;
1127       }
1128   }));
1129   /* *INDENT-ON* */
1130
1131   if (n_fds)
1132     session_send_fds (reg, fds, n_fds);
1133 }
1134
1135 static void
1136 vl_api_app_namespace_add_del_t_handler (vl_api_app_namespace_add_del_t * mp)
1137 {
1138   vl_api_app_namespace_add_del_reply_t *rmp;
1139   u32 appns_index = 0;
1140   u8 *ns_id = 0;
1141   int rv = 0;
1142   if (session_main_is_enabled () == 0)
1143     {
1144       rv = VNET_API_ERROR_FEATURE_DISABLED;
1145       goto done;
1146     }
1147
1148   if (mp->namespace_id_len > ARRAY_LEN (mp->namespace_id))
1149     {
1150       rv = VNET_API_ERROR_INVALID_VALUE;
1151       goto done;
1152     }
1153
1154   vec_validate (ns_id, mp->namespace_id_len - 1);
1155   clib_memcpy_fast (ns_id, mp->namespace_id, mp->namespace_id_len);
1156   vnet_app_namespace_add_del_args_t args = {
1157     .ns_id = ns_id,
1158     .secret = clib_net_to_host_u64 (mp->secret),
1159     .sw_if_index = clib_net_to_host_u32 (mp->sw_if_index),
1160     .ip4_fib_id = clib_net_to_host_u32 (mp->ip4_fib_id),
1161     .ip6_fib_id = clib_net_to_host_u32 (mp->ip6_fib_id),
1162     .is_add = 1
1163   };
1164   rv = vnet_app_namespace_add_del (&args);
1165   if (!rv)
1166     {
1167       appns_index = app_namespace_index_from_id (ns_id);
1168       if (appns_index == APP_NAMESPACE_INVALID_INDEX)
1169         {
1170           clib_warning ("app ns lookup failed");
1171           rv = VNET_API_ERROR_UNSPECIFIED;
1172         }
1173     }
1174   vec_free (ns_id);
1175
1176   /* *INDENT-OFF* */
1177 done:
1178   REPLY_MACRO2 (VL_API_APP_NAMESPACE_ADD_DEL_REPLY, ({
1179     if (!rv)
1180       rmp->appns_index = clib_host_to_net_u32 (appns_index);
1181   }));
1182   /* *INDENT-ON* */
1183 }
1184
1185 static void
1186 vl_api_session_rule_add_del_t_handler (vl_api_session_rule_add_del_t * mp)
1187 {
1188   vl_api_session_rule_add_del_reply_t *rmp;
1189   session_rule_add_del_args_t args;
1190   session_rule_table_add_del_args_t *table_args = &args.table_args;
1191   u8 fib_proto;
1192   int rv = 0;
1193
1194   clib_memset (&args, 0, sizeof (args));
1195   fib_proto = mp->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
1196
1197   table_args->lcl.fp_len = mp->lcl_plen;
1198   table_args->lcl.fp_proto = fib_proto;
1199   table_args->rmt.fp_len = mp->rmt_plen;
1200   table_args->rmt.fp_proto = fib_proto;
1201   table_args->lcl_port = mp->lcl_port;
1202   table_args->rmt_port = mp->rmt_port;
1203   table_args->action_index = clib_net_to_host_u32 (mp->action_index);
1204   table_args->is_add = mp->is_add;
1205   mp->tag[sizeof (mp->tag) - 1] = 0;
1206   table_args->tag = format (0, "%s", mp->tag);
1207   args.appns_index = clib_net_to_host_u32 (mp->appns_index);
1208   args.scope = mp->scope;
1209   args.transport_proto = mp->transport_proto;
1210
1211   clib_memset (&table_args->lcl.fp_addr, 0, sizeof (table_args->lcl.fp_addr));
1212   clib_memset (&table_args->rmt.fp_addr, 0, sizeof (table_args->rmt.fp_addr));
1213   ip_set (&table_args->lcl.fp_addr, mp->lcl_ip, mp->is_ip4);
1214   ip_set (&table_args->rmt.fp_addr, mp->rmt_ip, mp->is_ip4);
1215   rv = vnet_session_rule_add_del (&args);
1216   if (rv)
1217     clib_warning ("rule add del returned: %d", rv);
1218   vec_free (table_args->tag);
1219   REPLY_MACRO (VL_API_SESSION_RULE_ADD_DEL_REPLY);
1220 }
1221
1222 static void
1223 send_session_rule_details4 (mma_rule_16_t * rule, u8 is_local,
1224                             u8 transport_proto, u32 appns_index, u8 * tag,
1225                             vl_api_registration_t * reg, u32 context)
1226 {
1227   vl_api_session_rules_details_t *rmp = 0;
1228   session_mask_or_match_4_t *match =
1229     (session_mask_or_match_4_t *) & rule->match;
1230   session_mask_or_match_4_t *mask =
1231     (session_mask_or_match_4_t *) & rule->mask;
1232
1233   rmp = vl_msg_api_alloc (sizeof (*rmp));
1234   clib_memset (rmp, 0, sizeof (*rmp));
1235   rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS);
1236   rmp->context = context;
1237
1238   rmp->is_ip4 = 1;
1239   clib_memcpy_fast (rmp->lcl_ip, &match->lcl_ip, sizeof (match->lcl_ip));
1240   clib_memcpy_fast (rmp->rmt_ip, &match->rmt_ip, sizeof (match->rmt_ip));
1241   rmp->lcl_plen = ip4_mask_to_preflen (&mask->lcl_ip);
1242   rmp->rmt_plen = ip4_mask_to_preflen (&mask->rmt_ip);
1243   rmp->lcl_port = match->lcl_port;
1244   rmp->rmt_port = match->rmt_port;
1245   rmp->action_index = clib_host_to_net_u32 (rule->action_index);
1246   rmp->scope =
1247     is_local ? SESSION_RULE_SCOPE_LOCAL : SESSION_RULE_SCOPE_GLOBAL;
1248   rmp->transport_proto = transport_proto;
1249   rmp->appns_index = clib_host_to_net_u32 (appns_index);
1250   if (tag)
1251     {
1252       clib_memcpy_fast (rmp->tag, tag, vec_len (tag));
1253       rmp->tag[vec_len (tag)] = 0;
1254     }
1255
1256   vl_api_send_msg (reg, (u8 *) rmp);
1257 }
1258
1259 static void
1260 send_session_rule_details6 (mma_rule_40_t * rule, u8 is_local,
1261                             u8 transport_proto, u32 appns_index, u8 * tag,
1262                             vl_api_registration_t * reg, u32 context)
1263 {
1264   vl_api_session_rules_details_t *rmp = 0;
1265   session_mask_or_match_6_t *match =
1266     (session_mask_or_match_6_t *) & rule->match;
1267   session_mask_or_match_6_t *mask =
1268     (session_mask_or_match_6_t *) & rule->mask;
1269
1270   rmp = vl_msg_api_alloc (sizeof (*rmp));
1271   clib_memset (rmp, 0, sizeof (*rmp));
1272   rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS);
1273   rmp->context = context;
1274
1275   rmp->is_ip4 = 0;
1276   clib_memcpy_fast (rmp->lcl_ip, &match->lcl_ip, sizeof (match->lcl_ip));
1277   clib_memcpy_fast (rmp->rmt_ip, &match->rmt_ip, sizeof (match->rmt_ip));
1278   rmp->lcl_plen = ip6_mask_to_preflen (&mask->lcl_ip);
1279   rmp->rmt_plen = ip6_mask_to_preflen (&mask->rmt_ip);
1280   rmp->lcl_port = match->lcl_port;
1281   rmp->rmt_port = match->rmt_port;
1282   rmp->action_index = clib_host_to_net_u32 (rule->action_index);
1283   rmp->scope =
1284     is_local ? SESSION_RULE_SCOPE_LOCAL : SESSION_RULE_SCOPE_GLOBAL;
1285   rmp->transport_proto = transport_proto;
1286   rmp->appns_index = clib_host_to_net_u32 (appns_index);
1287   if (tag)
1288     {
1289       clib_memcpy_fast (rmp->tag, tag, vec_len (tag));
1290       rmp->tag[vec_len (tag)] = 0;
1291     }
1292
1293   vl_api_send_msg (reg, (u8 *) rmp);
1294 }
1295
1296 static void
1297 send_session_rules_table_details (session_rules_table_t * srt, u8 fib_proto,
1298                                   u8 tp, u8 is_local, u32 appns_index,
1299                                   vl_api_registration_t * reg, u32 context)
1300 {
1301   mma_rule_16_t *rule16;
1302   mma_rule_40_t *rule40;
1303   mma_rules_table_16_t *srt16;
1304   mma_rules_table_40_t *srt40;
1305   u32 ri;
1306
1307   if (is_local || fib_proto == FIB_PROTOCOL_IP4)
1308     {
1309       u8 *tag = 0;
1310       /* *INDENT-OFF* */
1311       srt16 = &srt->session_rules_tables_16;
1312       pool_foreach (rule16, srt16->rules, ({
1313         ri = mma_rules_table_rule_index_16 (srt16, rule16);
1314         tag = session_rules_table_rule_tag (srt, ri, 1);
1315         send_session_rule_details4 (rule16, is_local, tp, appns_index, tag,
1316                                     reg, context);
1317       }));
1318       /* *INDENT-ON* */
1319     }
1320   if (is_local || fib_proto == FIB_PROTOCOL_IP6)
1321     {
1322       u8 *tag = 0;
1323       /* *INDENT-OFF* */
1324       srt40 = &srt->session_rules_tables_40;
1325       pool_foreach (rule40, srt40->rules, ({
1326         ri = mma_rules_table_rule_index_40 (srt40, rule40);
1327         tag = session_rules_table_rule_tag (srt, ri, 1);
1328         send_session_rule_details6 (rule40, is_local, tp, appns_index, tag,
1329                                     reg, context);
1330       }));
1331       /* *INDENT-ON* */
1332     }
1333 }
1334
1335 static void
1336 vl_api_session_rules_dump_t_handler (vl_api_one_map_server_dump_t * mp)
1337 {
1338   vl_api_registration_t *reg;
1339   session_table_t *st;
1340   u8 tp;
1341
1342   reg = vl_api_client_index_to_registration (mp->client_index);
1343   if (!reg)
1344     return;
1345
1346   /* *INDENT-OFF* */
1347   session_table_foreach (st, ({
1348     for (tp = 0; tp < TRANSPORT_N_PROTO; tp++)
1349       {
1350         send_session_rules_table_details (&st->session_rules[tp],
1351                                           st->active_fib_proto, tp,
1352                                           st->is_local, st->appns_index, reg,
1353                                           mp->context);
1354       }
1355   }));
1356   /* *INDENT-ON* */
1357 }
1358
1359 static void
1360 vl_api_app_add_cert_key_pair_t_handler (vl_api_app_add_cert_key_pair_t * mp)
1361 {
1362   vl_api_app_add_cert_key_pair_reply_t *rmp;
1363   vnet_app_add_cert_key_pair_args_t _a, *a = &_a;
1364   u32 certkey_len, key_len, cert_len;
1365   int rv = 0;
1366   if (session_main_is_enabled () == 0)
1367     {
1368       rv = VNET_API_ERROR_FEATURE_DISABLED;
1369       goto done;
1370     }
1371
1372   cert_len = clib_net_to_host_u16 (mp->cert_len);
1373   if (cert_len > 10000)
1374     {
1375       rv = VNET_API_ERROR_INVALID_VALUE;
1376       goto done;
1377     }
1378
1379   certkey_len = clib_net_to_host_u16 (mp->certkey_len);
1380   if (certkey_len < cert_len)
1381     {
1382       rv = VNET_API_ERROR_INVALID_VALUE;
1383       goto done;
1384     }
1385
1386   key_len = certkey_len - cert_len;
1387   if (key_len > 10000)
1388     {
1389       rv = VNET_API_ERROR_INVALID_VALUE;
1390       goto done;
1391     }
1392
1393   clib_memset (a, 0, sizeof (*a));
1394   vec_validate (a->cert, cert_len);
1395   vec_validate (a->key, key_len);
1396   clib_memcpy_fast (a->cert, mp->certkey, cert_len);
1397   clib_memcpy_fast (a->key, mp->certkey + cert_len, key_len);
1398   rv = vnet_app_add_cert_key_pair (a);
1399   vec_free (a->cert);
1400   vec_free (a->key);
1401
1402 done:
1403   /* *INDENT-OFF* */
1404   REPLY_MACRO2 (VL_API_APP_ADD_CERT_KEY_PAIR_REPLY, ({
1405     if (!rv)
1406       rmp->index = clib_host_to_net_u32 (a->index);
1407   }));
1408   /* *INDENT-ON* */
1409 }
1410
1411 static void
1412 vl_api_app_del_cert_key_pair_t_handler (vl_api_app_del_cert_key_pair_t * mp)
1413 {
1414   vl_api_app_del_cert_key_pair_reply_t *rmp;
1415   u32 ckpair_index;
1416   int rv = 0;
1417   if (session_main_is_enabled () == 0)
1418     {
1419       rv = VNET_API_ERROR_FEATURE_DISABLED;
1420       goto done;
1421     }
1422   ckpair_index = clib_net_to_host_u32 (mp->index);
1423   rv = vnet_app_del_cert_key_pair (ckpair_index);
1424
1425 done:
1426   REPLY_MACRO (VL_API_APP_DEL_CERT_KEY_PAIR_REPLY);
1427 }
1428
1429 /* ### WILL BE DEPRECATED POST 20.01 ### */
1430 static void
1431 vl_api_application_tls_cert_add_t_handler (vl_api_application_tls_cert_add_t *
1432                                            mp)
1433 {
1434   vl_api_application_tls_cert_add_reply_t *rmp;
1435   app_cert_key_pair_t *ckpair;
1436   application_t *app;
1437   u32 cert_len;
1438   int rv = 0;
1439   if (session_main_is_enabled () == 0)
1440     {
1441       rv = VNET_API_ERROR_FEATURE_DISABLED;
1442       goto done;
1443     }
1444   if (!(app = application_lookup (mp->client_index)))
1445     {
1446       rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1447       goto done;
1448     }
1449   cert_len = clib_net_to_host_u16 (mp->cert_len);
1450   if (cert_len > 10000)
1451     {
1452       rv = VNET_API_ERROR_INVALID_VALUE;
1453       goto done;
1454     }
1455   ckpair = app_cert_key_pair_get_default ();
1456   vec_validate (ckpair->cert, cert_len);
1457   clib_memcpy_fast (ckpair->cert, mp->cert, cert_len);
1458
1459 done:
1460   REPLY_MACRO (VL_API_APPLICATION_TLS_CERT_ADD_REPLY);
1461 }
1462
1463 /* ### WILL BE DEPRECATED POST 20.01 ### */
1464 static void
1465 vl_api_application_tls_key_add_t_handler (vl_api_application_tls_key_add_t *
1466                                           mp)
1467 {
1468   vl_api_application_tls_key_add_reply_t *rmp;
1469   app_cert_key_pair_t *ckpair;
1470   application_t *app;
1471   u32 key_len;
1472   int rv = 0;
1473   if (session_main_is_enabled () == 0)
1474     {
1475       rv = VNET_API_ERROR_FEATURE_DISABLED;
1476       goto done;
1477     }
1478   if (!(app = application_lookup (mp->client_index)))
1479     {
1480       rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1481       goto done;
1482     }
1483   key_len = clib_net_to_host_u16 (mp->key_len);
1484   if (key_len > 10000)
1485     {
1486       rv = VNET_API_ERROR_INVALID_VALUE;
1487       goto done;
1488     }
1489   ckpair = app_cert_key_pair_get_default ();
1490   vec_validate (ckpair->key, key_len);
1491   clib_memcpy_fast (ckpair->key, mp->key, key_len);
1492 done:
1493   REPLY_MACRO (VL_API_APPLICATION_TLS_KEY_ADD_REPLY);
1494 }
1495
1496 static clib_error_t *
1497 application_reaper_cb (u32 client_index)
1498 {
1499   application_t *app = application_lookup (client_index);
1500   vnet_app_detach_args_t _a, *a = &_a;
1501   if (app)
1502     {
1503       a->app_index = app->app_index;
1504       a->api_client_index = client_index;
1505       vnet_application_detach (a);
1506     }
1507   return 0;
1508 }
1509
1510 VL_MSG_API_REAPER_FUNCTION (application_reaper_cb);
1511
1512 #define vl_msg_name_crc_list
1513 #include <vnet/vnet_all_api_h.h>
1514 #undef vl_msg_name_crc_list
1515
1516 static void
1517 setup_message_id_table (api_main_t * am)
1518 {
1519 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1520   foreach_vl_msg_name_crc_session;
1521 #undef _
1522 }
1523
1524 /*
1525  * session_api_hookup
1526  * Add uri's API message handlers to the table.
1527  * vlib has already mapped shared memory and
1528  * added the client registration handlers.
1529  * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
1530  */
1531 static clib_error_t *
1532 session_api_hookup (vlib_main_t * vm)
1533 {
1534   api_main_t *am = vlibapi_get_main ();
1535
1536 #define _(N,n)                                                  \
1537     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
1538                            vl_api_##n##_t_handler,              \
1539                            vl_noop_handler,                     \
1540                            vl_api_##n##_t_endian,               \
1541                            vl_api_##n##_t_print,                \
1542                            sizeof(vl_api_##n##_t), 1);
1543   foreach_session_api_msg;
1544 #undef _
1545
1546   /*
1547    * Messages which bounce off the data-plane to
1548    * an API client. Simply tells the message handling infra not
1549    * to free the message.
1550    *
1551    * Bounced message handlers MUST NOT block the data plane
1552    */
1553   am->message_bounce[VL_API_CONNECT_URI] = 1;
1554   am->message_bounce[VL_API_CONNECT_SOCK] = 1;
1555
1556   /*
1557    * Set up the (msg_name, crc, message-id) table
1558    */
1559   setup_message_id_table (am);
1560
1561   return 0;
1562 }
1563
1564 VLIB_API_INIT_FUNCTION (session_api_hookup);
1565
1566 /*
1567  * fd.io coding-style-patch-verification: ON
1568  *
1569  * Local Variables:
1570  * eval: (c-set-style "gnu")
1571  * End:
1572  */