session: return local transport endpoint in connect reply
[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
20 #include <vnet/vnet_msg_enum.h>
21 #include "application_interface.h"
22
23 #define vl_typedefs             /* define message structures */
24 #include <vnet/vnet_all_api_h.h>
25 #undef vl_typedefs
26
27 #define vl_endianfun            /* define message structures */
28 #include <vnet/vnet_all_api_h.h>
29 #undef vl_endianfun
30
31 /* instantiate all the print functions we know about */
32 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
33 #define vl_printfun
34 #include <vnet/vnet_all_api_h.h>
35 #undef vl_printfun
36
37 #include <vlibapi/api_helper_macros.h>
38
39 #define foreach_session_api_msg                                         \
40 _(MAP_ANOTHER_SEGMENT_REPLY, map_another_segment_reply)                 \
41 _(APPLICATION_ATTACH, application_attach)                               \
42 _(APPLICATION_DETACH, application_detach)                               \
43 _(BIND_URI, bind_uri)                                                   \
44 _(UNBIND_URI, unbind_uri)                                               \
45 _(CONNECT_URI, connect_uri)                                             \
46 _(DISCONNECT_SESSION, disconnect_session)                               \
47 _(DISCONNECT_SESSION_REPLY, disconnect_session_reply)                   \
48 _(ACCEPT_SESSION_REPLY, accept_session_reply)                           \
49 _(RESET_SESSION_REPLY, reset_session_reply)                             \
50 _(BIND_SOCK, bind_sock)                                                 \
51 _(UNBIND_SOCK, unbind_sock)                                             \
52 _(CONNECT_SOCK, connect_sock)                                           \
53 _(SESSION_ENABLE_DISABLE, session_enable_disable)                       \
54 _(APP_NAMESPACE_ADD_DEL, app_namespace_add_del)                         \
55
56 static int
57 send_add_segment_callback (u32 api_client_index, const u8 * segment_name,
58                            u32 segment_size)
59 {
60   vl_api_map_another_segment_t *mp;
61   unix_shared_memory_queue_t *q;
62
63   q = vl_api_client_index_to_input_queue (api_client_index);
64
65   if (!q)
66     return -1;
67
68   mp = vl_msg_api_alloc (sizeof (*mp));
69   memset (mp, 0, sizeof (*mp));
70   mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_MAP_ANOTHER_SEGMENT);
71   mp->segment_size = segment_size;
72   strncpy ((char *) mp->segment_name, (char *) segment_name,
73            sizeof (mp->segment_name) - 1);
74
75   vl_msg_api_send_shmem (q, (u8 *) & mp);
76
77   return 0;
78 }
79
80 static int
81 send_session_accept_callback (stream_session_t * s)
82 {
83   vl_api_accept_session_t *mp;
84   unix_shared_memory_queue_t *q, *vpp_queue;
85   application_t *server = application_get (s->app_index);
86   transport_connection_t *tc;
87   transport_proto_vft_t *tp_vft;
88   stream_session_t *listener;
89
90   q = vl_api_client_index_to_input_queue (server->api_client_index);
91   vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
92
93   if (!q)
94     return -1;
95
96   mp = vl_msg_api_alloc (sizeof (*mp));
97   memset (mp, 0, sizeof (*mp));
98
99   mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_ACCEPT_SESSION);
100   mp->context = server->index;
101   listener = listen_session_get (s->session_type, s->listener_index);
102   tp_vft = transport_protocol_get_vft (s->session_type);
103   tc = tp_vft->get_connection (s->connection_index, s->thread_index);
104   mp->listener_handle = listen_session_get_handle (listener);
105   mp->handle = session_handle (s);
106   mp->server_rx_fifo = pointer_to_uword (s->server_rx_fifo);
107   mp->server_tx_fifo = pointer_to_uword (s->server_tx_fifo);
108   mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
109   mp->port = tc->rmt_port;
110   mp->is_ip4 = tc->is_ip4;
111   clib_memcpy (&mp->ip, &tc->rmt_ip, sizeof (tc->rmt_ip));
112   vl_msg_api_send_shmem (q, (u8 *) & mp);
113
114   return 0;
115 }
116
117 static void
118 send_session_disconnect_callback (stream_session_t * s)
119 {
120   vl_api_disconnect_session_t *mp;
121   unix_shared_memory_queue_t *q;
122   application_t *app = application_get (s->app_index);
123
124   q = vl_api_client_index_to_input_queue (app->api_client_index);
125
126   if (!q)
127     return;
128
129   mp = vl_msg_api_alloc (sizeof (*mp));
130   memset (mp, 0, sizeof (*mp));
131   mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_DISCONNECT_SESSION);
132   mp->handle = session_handle (s);
133   vl_msg_api_send_shmem (q, (u8 *) & mp);
134 }
135
136 static void
137 send_session_reset_callback (stream_session_t * s)
138 {
139   vl_api_reset_session_t *mp;
140   unix_shared_memory_queue_t *q;
141   application_t *app = application_get (s->app_index);
142
143   q = vl_api_client_index_to_input_queue (app->api_client_index);
144
145   if (!q)
146     return;
147
148   mp = vl_msg_api_alloc (sizeof (*mp));
149   memset (mp, 0, sizeof (*mp));
150   mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_RESET_SESSION);
151   mp->handle = session_handle (s);
152   vl_msg_api_send_shmem (q, (u8 *) & mp);
153 }
154
155 int
156 send_session_connected_callback (u32 app_index, u32 api_context,
157                                  stream_session_t * s, u8 is_fail)
158 {
159   vl_api_connect_session_reply_t *mp;
160   unix_shared_memory_queue_t *q;
161   application_t *app;
162   unix_shared_memory_queue_t *vpp_queue;
163   transport_connection_t *tc;
164
165   app = application_get (app_index);
166   q = vl_api_client_index_to_input_queue (app->api_client_index);
167
168   if (!q)
169     return -1;
170
171   tc = session_get_transport (s);
172   if (!tc)
173     is_fail = 1;
174   mp = vl_msg_api_alloc (sizeof (*mp));
175   mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_CONNECT_SESSION_REPLY);
176   mp->context = api_context;
177   if (!is_fail)
178     {
179       vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
180       mp->server_rx_fifo = pointer_to_uword (s->server_rx_fifo);
181       mp->server_tx_fifo = pointer_to_uword (s->server_tx_fifo);
182       mp->handle = session_handle (s);
183       mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
184       clib_memcpy (mp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
185       mp->is_ip4 = tc->is_ip4;
186       mp->lcl_port = tc->lcl_port;
187       mp->retval = 0;
188     }
189   else
190     {
191       mp->retval = clib_host_to_net_u32 (VNET_API_ERROR_SESSION_CONNECT);
192     }
193
194   vl_msg_api_send_shmem (q, (u8 *) & mp);
195   return 0;
196 }
197
198 /**
199  * Redirect a connect_uri message to the indicated server.
200  * Only sent if the server has bound the related port with
201  * URI_OPTIONS_FLAGS_USE_FIFO
202  */
203 static int
204 redirect_connect_callback (u32 server_api_client_index, void *mp_arg)
205 {
206   vl_api_connect_sock_t *mp = mp_arg;
207   unix_shared_memory_queue_t *server_q, *client_q;
208   vlib_main_t *vm = vlib_get_main ();
209   f64 timeout = vlib_time_now (vm) + 0.5;
210   application_t *app;
211   int rv = 0;
212
213   server_q = vl_api_client_index_to_input_queue (server_api_client_index);
214
215   if (!server_q)
216     {
217       rv = VNET_API_ERROR_INVALID_VALUE;
218       goto out;
219     }
220
221   client_q = vl_api_client_index_to_input_queue (mp->client_index);
222   if (!client_q)
223     {
224       rv = VNET_API_ERROR_INVALID_VALUE_2;
225       goto out;
226     }
227
228   /* Tell the server the client's API queue address, so it can reply */
229   mp->client_queue_address = pointer_to_uword (client_q);
230   app = application_lookup (mp->client_index);
231   if (!app)
232     {
233       clib_warning ("no client application");
234       return -1;
235     }
236
237   mp->options[SESSION_OPTIONS_RX_FIFO_SIZE] = app->sm_properties.rx_fifo_size;
238   mp->options[SESSION_OPTIONS_TX_FIFO_SIZE] = app->sm_properties.tx_fifo_size;
239
240   /*
241    * Bounce message handlers MUST NOT block the data-plane.
242    * Spin waiting for the queue lock, but
243    */
244
245   while (vlib_time_now (vm) < timeout)
246     {
247       rv =
248         unix_shared_memory_queue_add (server_q, (u8 *) & mp, 1 /*nowait */ );
249       switch (rv)
250         {
251           /* correctly enqueued */
252         case 0:
253           return VNET_API_ERROR_SESSION_REDIRECT;
254
255           /* continue spinning, wait for pthread_mutex_trylock to work */
256         case -1:
257           continue;
258
259           /* queue stuffed, drop the msg */
260         case -2:
261           rv = VNET_API_ERROR_QUEUE_FULL;
262           goto out;
263         }
264     }
265 out:
266   /* Dispose of the message */
267   vl_msg_api_free (mp);
268   return rv;
269 }
270
271 static session_cb_vft_t session_cb_vft = {
272   .session_accept_callback = send_session_accept_callback,
273   .session_disconnect_callback = send_session_disconnect_callback,
274   .session_connected_callback = send_session_connected_callback,
275   .session_reset_callback = send_session_reset_callback,
276   .add_segment_callback = send_add_segment_callback,
277   .redirect_connect_callback = redirect_connect_callback
278 };
279
280 static void
281 vl_api_session_enable_disable_t_handler (vl_api_session_enable_disable_t * mp)
282 {
283   vl_api_session_enable_disable_reply_t *rmp;
284   vlib_main_t *vm = vlib_get_main ();
285   int rv = 0;
286
287   vnet_session_enable_disable (vm, mp->is_enable);
288   REPLY_MACRO (VL_API_SESSION_ENABLE_DISABLE_REPLY);
289 }
290
291 static void
292 vl_api_application_attach_t_handler (vl_api_application_attach_t * mp)
293 {
294   vl_api_application_attach_reply_t *rmp;
295   vnet_app_attach_args_t _a, *a = &_a;
296   clib_error_t *error = 0;
297   int rv = 0;
298
299   if (session_manager_is_enabled () == 0)
300     {
301       rv = VNET_API_ERROR_FEATURE_DISABLED;
302       goto done;
303     }
304
305   STATIC_ASSERT (sizeof (u64) * SESSION_OPTIONS_N_OPTIONS <=
306                  sizeof (mp->options),
307                  "Out of options, fix api message definition");
308
309   memset (a, 0, sizeof (*a));
310   a->api_client_index = mp->client_index;
311   a->options = mp->options;
312   a->session_cb_vft = &session_cb_vft;
313
314   if (mp->namespace_id_len > 64)
315     {
316       rv = VNET_API_ERROR_INVALID_VALUE;
317       goto done;
318     }
319
320   if (mp->namespace_id_len)
321     {
322       vec_validate (a->namespace_id, mp->namespace_id_len);
323       clib_memcpy (a->namespace_id, mp->namespace_id, mp->namespace_id_len);
324     }
325
326   if ((error = vnet_application_attach (a)))
327     {
328       rv = clib_error_get_code (error);
329       clib_error_report (error);
330     }
331   vec_free (a->namespace_id);
332
333 done:
334
335   /* *INDENT-OFF* */
336   REPLY_MACRO2 (VL_API_APPLICATION_ATTACH_REPLY, ({
337     if (!rv)
338       {
339         rmp->segment_name_length = 0;
340         rmp->segment_size = a->segment_size;
341         if (a->segment_name_length)
342           {
343             memcpy (rmp->segment_name, a->segment_name,
344                     a->segment_name_length);
345             rmp->segment_name_length = a->segment_name_length;
346           }
347         rmp->app_event_queue_address = a->app_event_queue_address;
348       }
349   }));
350   /* *INDENT-ON* */
351 }
352
353 static void
354 vl_api_application_detach_t_handler (vl_api_application_detach_t * mp)
355 {
356   vl_api_application_detach_reply_t *rmp;
357   int rv = VNET_API_ERROR_INVALID_VALUE_2;
358   vnet_app_detach_args_t _a, *a = &_a;
359   application_t *app;
360
361   if (session_manager_is_enabled () == 0)
362     {
363       rv = VNET_API_ERROR_FEATURE_DISABLED;
364       goto done;
365     }
366
367   app = application_lookup (mp->client_index);
368   if (app)
369     {
370       a->app_index = app->index;
371       rv = vnet_application_detach (a);
372     }
373
374 done:
375   REPLY_MACRO (VL_API_APPLICATION_DETACH_REPLY);
376 }
377
378 static void
379 vl_api_bind_uri_t_handler (vl_api_bind_uri_t * mp)
380 {
381   vl_api_bind_uri_reply_t *rmp;
382   vnet_bind_args_t _a, *a = &_a;
383   application_t *app;
384   int rv;
385
386   if (session_manager_is_enabled () == 0)
387     {
388       rv = VNET_API_ERROR_FEATURE_DISABLED;
389       goto done;
390     }
391
392   app = application_lookup (mp->client_index);
393   if (app)
394     {
395       memset (a, 0, sizeof (*a));
396       a->uri = (char *) mp->uri;
397       a->app_index = app->index;
398       rv = vnet_bind_uri (a);
399     }
400   else
401     {
402       rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
403     }
404
405 done:
406   REPLY_MACRO (VL_API_BIND_URI_REPLY);
407 }
408
409 static void
410 vl_api_unbind_uri_t_handler (vl_api_unbind_uri_t * mp)
411 {
412   vl_api_unbind_uri_reply_t *rmp;
413   application_t *app;
414   vnet_unbind_args_t _a, *a = &_a;
415   int rv;
416
417   if (session_manager_is_enabled () == 0)
418     {
419       rv = VNET_API_ERROR_FEATURE_DISABLED;
420       goto done;
421     }
422
423   app = application_lookup (mp->client_index);
424   if (app)
425     {
426       a->uri = (char *) mp->uri;
427       a->app_index = app->index;
428       rv = vnet_unbind_uri (a);
429     }
430   else
431     {
432       rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
433     }
434
435 done:
436   REPLY_MACRO (VL_API_UNBIND_URI_REPLY);
437 }
438
439 static void
440 vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp)
441 {
442   vl_api_connect_session_reply_t *rmp;
443   vnet_connect_args_t _a, *a = &_a;
444   application_t *app;
445   clib_error_t *error = 0;
446   int rv = 0;
447
448   if (session_manager_is_enabled () == 0)
449     {
450       rv = VNET_API_ERROR_FEATURE_DISABLED;
451       goto done;
452     }
453
454   app = application_lookup (mp->client_index);
455   if (app)
456     {
457       a->uri = (char *) mp->uri;
458       a->api_context = mp->context;
459       a->app_index = app->index;
460       a->mp = mp;
461       if ((error = vnet_connect_uri (a)))
462         {
463           rv = clib_error_get_code (error);
464           if (rv != VNET_API_ERROR_SESSION_REDIRECT)
465             clib_error_report (error);
466         }
467     }
468   else
469     {
470       rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
471     }
472
473   /*
474    * Don't reply to stream (tcp) connects. The reply will come once
475    * the connection is established. In case of the redirects, the reply
476    * will come from the server app.
477    */
478   if (rv == 0 || rv == VNET_API_ERROR_SESSION_REDIRECT)
479     return;
480
481 done:
482   /* *INDENT-OFF* */
483   REPLY_MACRO (VL_API_CONNECT_SESSION_REPLY);
484   /* *INDENT-ON* */
485 }
486
487 static void
488 vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
489 {
490   vl_api_disconnect_session_reply_t *rmp;
491   vnet_disconnect_args_t _a, *a = &_a;
492   application_t *app;
493   int rv = 0;
494
495   if (session_manager_is_enabled () == 0)
496     {
497       rv = VNET_API_ERROR_FEATURE_DISABLED;
498       goto done;
499     }
500
501   app = application_lookup (mp->client_index);
502   if (app)
503     {
504       a->handle = mp->handle;
505       a->app_index = app->index;
506       rv = vnet_disconnect_session (a);
507     }
508   else
509     {
510       rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
511     }
512
513 done:
514   REPLY_MACRO (VL_API_DISCONNECT_SESSION_REPLY);
515 }
516
517 static void
518 vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
519                                            mp)
520 {
521   vnet_disconnect_args_t _a, *a = &_a;
522   application_t *app;
523
524   /* Client objected to disconnecting the session, log and continue */
525   if (mp->retval)
526     {
527       clib_warning ("client retval %d", mp->retval);
528       return;
529     }
530
531   /* Disconnect has been confirmed. Confirm close to transport */
532   app = application_lookup (mp->client_index);
533   if (app)
534     {
535       a->handle = mp->handle;
536       a->app_index = app->index;
537       vnet_disconnect_session (a);
538     }
539 }
540
541 static void
542 vl_api_reset_session_reply_t_handler (vl_api_reset_session_reply_t * mp)
543 {
544   application_t *app;
545   stream_session_t *s;
546   u32 index, thread_index;
547
548   app = application_lookup (mp->client_index);
549   if (!app)
550     return;
551
552   session_parse_handle (mp->handle, &index, &thread_index);
553   s = session_get_if_valid (index, thread_index);
554   if (s == 0 || app->index != s->app_index)
555     {
556       clib_warning ("Invalid session!");
557       return;
558     }
559
560   /* Client objected to resetting the session, log and continue */
561   if (mp->retval)
562     {
563       clib_warning ("client retval %d", mp->retval);
564       return;
565     }
566
567   /* This comes as a response to a reset, transport only waiting for
568    * confirmation to remove connection state, no need to disconnect */
569   stream_session_cleanup (s);
570 }
571
572 static void
573 vl_api_accept_session_reply_t_handler (vl_api_accept_session_reply_t * mp)
574 {
575   stream_session_t *s;
576   u32 session_index, thread_index;
577   vnet_disconnect_args_t _a, *a = &_a;
578
579   /* Server isn't interested, kill the session */
580   if (mp->retval)
581     {
582       a->app_index = mp->context;
583       a->handle = mp->handle;
584       vnet_disconnect_session (a);
585     }
586   else
587     {
588       session_parse_handle (mp->handle, &session_index, &thread_index);
589       s = session_get_if_valid (session_index, thread_index);
590       if (!s)
591         {
592           clib_warning ("session doesn't exist");
593           return;
594         }
595       if (s->app_index != mp->context)
596         {
597           clib_warning ("app doesn't own session");
598           return;
599         }
600       s->session_state = SESSION_STATE_READY;
601     }
602 }
603
604 static void
605 vl_api_map_another_segment_reply_t_handler (vl_api_map_another_segment_reply_t
606                                             * mp)
607 {
608   clib_warning ("not implemented");
609 }
610
611 static void
612 vl_api_bind_sock_t_handler (vl_api_bind_sock_t * mp)
613 {
614   vl_api_bind_sock_reply_t *rmp;
615   vnet_bind_args_t _a, *a = &_a;
616   int rv = 0;
617   clib_error_t *error;
618   application_t *app;
619
620   if (session_manager_is_enabled () == 0)
621     {
622       rv = VNET_API_ERROR_FEATURE_DISABLED;
623       goto done;
624     }
625
626   app = application_lookup (mp->client_index);
627   if (app)
628     {
629       ip46_address_t *ip46 = (ip46_address_t *) mp->ip;
630       memset (a, 0, sizeof (*a));
631       a->sep.is_ip4 = mp->is_ip4;
632       a->sep.ip = *ip46;
633       a->sep.port = mp->port;
634       a->sep.fib_index = mp->vrf;
635       a->sep.sw_if_index = ENDPOINT_INVALID_INDEX;
636       a->sep.transport_proto = mp->proto;
637       a->app_index = app->index;
638
639       if ((error = vnet_bind (a)))
640         {
641           rv = clib_error_get_code (error);
642           clib_error_report (error);
643         }
644     }
645 done:
646   /* *INDENT-OFF* */
647   REPLY_MACRO2 (VL_API_BIND_SOCK_REPLY,({
648     if (!rv)
649       rmp->handle = a->handle;
650   }));
651   /* *INDENT-ON* */
652 }
653
654 static void
655 vl_api_unbind_sock_t_handler (vl_api_unbind_sock_t * mp)
656 {
657   vl_api_unbind_sock_reply_t *rmp;
658   vnet_unbind_args_t _a, *a = &_a;
659   application_t *app;
660   clib_error_t *error;
661   int rv = 0;
662
663   if (session_manager_is_enabled () == 0)
664     {
665       rv = VNET_API_ERROR_FEATURE_DISABLED;
666       goto done;
667     }
668
669   app = application_lookup (mp->client_index);
670   if (app)
671     {
672       a->app_index = mp->client_index;
673       a->handle = mp->handle;
674       if ((error = vnet_unbind (a)))
675         {
676           rv = clib_error_get_code (error);
677           clib_error_report (error);
678         }
679     }
680
681 done:
682   REPLY_MACRO (VL_API_UNBIND_SOCK_REPLY);
683 }
684
685 static void
686 vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
687 {
688   vl_api_connect_session_reply_t *rmp;
689   vnet_connect_args_t _a, *a = &_a;
690   application_t *app;
691   clib_error_t *error = 0;
692   int rv = 0;
693
694   if (session_manager_is_enabled () == 0)
695     {
696       rv = VNET_API_ERROR_FEATURE_DISABLED;
697       goto done;
698     }
699
700   app = application_lookup (mp->client_index);
701   if (app)
702     {
703       unix_shared_memory_queue_t *client_q;
704       ip46_address_t *ip46 = (ip46_address_t *) mp->ip;
705
706       client_q = vl_api_client_index_to_input_queue (mp->client_index);
707       mp->client_queue_address = pointer_to_uword (client_q);
708       a->sep.is_ip4 = mp->is_ip4;
709       a->sep.ip = *ip46;
710       a->sep.port = mp->port;
711       a->sep.transport_proto = mp->proto;
712       a->sep.fib_index = mp->vrf;
713       a->sep.sw_if_index = ENDPOINT_INVALID_INDEX;
714       a->api_context = mp->context;
715       a->app_index = app->index;
716       a->mp = mp;
717       if ((error = vnet_connect (a)))
718         {
719           rv = clib_error_get_code (error);
720           if (rv != VNET_API_ERROR_SESSION_REDIRECT)
721             clib_error_report (error);
722         }
723     }
724   else
725     {
726       rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
727     }
728
729   if (rv == 0 || rv == VNET_API_ERROR_SESSION_REDIRECT)
730     return;
731
732   /* Got some error, relay it */
733
734 done:
735   REPLY_MACRO (VL_API_CONNECT_SESSION_REPLY);
736 }
737
738 static void
739 vl_api_app_namespace_add_del_t_handler (vl_api_app_namespace_add_del_t * mp)
740 {
741   vl_api_app_namespace_add_del_reply_t *rmp;
742   u8 *ns_id = 0;
743   clib_error_t *error = 0;
744   int rv = 0;
745   if (!session_manager_is_enabled ())
746     {
747       rv = VNET_API_ERROR_FEATURE_DISABLED;
748       goto done;
749     }
750
751   if (mp->namespace_id_len > ARRAY_LEN (mp->namespace_id))
752     {
753       rv = VNET_API_ERROR_INVALID_VALUE;
754       goto done;
755     }
756
757   vec_validate (ns_id, mp->namespace_id_len - 1);
758   clib_memcpy (ns_id, mp->namespace_id, mp->namespace_id_len);
759   vnet_app_namespace_add_del_args_t args = {
760     .ns_id = ns_id,
761     .secret = mp->secret,
762     .sw_if_index = clib_net_to_host_u32 (mp->sw_if_index),
763     .ip4_fib_id = clib_net_to_host_u32 (mp->ip4_fib_id),
764     .ip6_fib_id = clib_net_to_host_u32 (mp->ip6_fib_id),
765     .is_add = 1
766   };
767   error = vnet_app_namespace_add_del (&args);
768   if (error)
769     {
770       rv = clib_error_get_code (error);
771       clib_error_report (error);
772     }
773   vec_free (ns_id);
774 done:
775   REPLY_MACRO (VL_API_APP_NAMESPACE_ADD_DEL_REPLY);
776 }
777
778 static clib_error_t *
779 application_reaper_cb (u32 client_index)
780 {
781   application_t *app = application_lookup (client_index);
782   vnet_app_detach_args_t _a, *a = &_a;
783   if (app)
784     {
785       a->app_index = app->index;
786       vnet_application_detach (a);
787     }
788   return 0;
789 }
790
791 VL_MSG_API_REAPER_FUNCTION (application_reaper_cb);
792
793 #define vl_msg_name_crc_list
794 #include <vnet/vnet_all_api_h.h>
795 #undef vl_msg_name_crc_list
796
797 static void
798 setup_message_id_table (api_main_t * am)
799 {
800 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
801   foreach_vl_msg_name_crc_session;
802 #undef _
803 }
804
805 /*
806  * session_api_hookup
807  * Add uri's API message handlers to the table.
808  * vlib has alread mapped shared memory and
809  * added the client registration handlers.
810  * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
811  */
812 static clib_error_t *
813 session_api_hookup (vlib_main_t * vm)
814 {
815   api_main_t *am = &api_main;
816
817 #define _(N,n)                                                  \
818     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
819                            vl_api_##n##_t_handler,              \
820                            vl_noop_handler,                     \
821                            vl_api_##n##_t_endian,               \
822                            vl_api_##n##_t_print,                \
823                            sizeof(vl_api_##n##_t), 1);
824   foreach_session_api_msg;
825 #undef _
826
827   /*
828    * Messages which bounce off the data-plane to
829    * an API client. Simply tells the message handling infra not
830    * to free the message.
831    *
832    * Bounced message handlers MUST NOT block the data plane
833    */
834   am->message_bounce[VL_API_CONNECT_URI] = 1;
835   am->message_bounce[VL_API_CONNECT_SOCK] = 1;
836
837   /*
838    * Set up the (msg_name, crc, message-id) table
839    */
840   setup_message_id_table (am);
841
842   return 0;
843 }
844
845 VLIB_API_INIT_FUNCTION (session_api_hookup);
846
847 /*
848  * fd.io coding-style-patch-verification: ON
849  *
850  * Local Variables:
851  * eval: (c-set-style "gnu")
852  * End:
853  */