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