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