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