session: avoid session handle conflict with vcl
[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
60 static int
61 session_send_memfd_fd (vl_api_registration_t * reg, const ssvm_private_t * sp)
62 {
63   clib_error_t *error;
64   if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
65     {
66       clib_warning ("can't send memfd fd");
67       return -1;
68     }
69   error = vl_api_send_fd_msg (reg, sp->fd);
70   if (error)
71     {
72       clib_error_report (error);
73       return -1;
74     }
75   return 0;
76 }
77
78 static int
79 send_add_segment_callback (u32 api_client_index, const ssvm_private_t * sp)
80 {
81   vl_api_map_another_segment_t *mp;
82   vl_api_registration_t *reg;
83
84   reg = vl_mem_api_client_index_to_registration (api_client_index);
85   if (!reg)
86     {
87       clib_warning ("no registration: %u", api_client_index);
88       return -1;
89     }
90
91   if (ssvm_type (sp) == SSVM_SEGMENT_MEMFD
92       && vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
93     {
94       clib_warning ("can't send memfd fd");
95       return -1;
96     }
97
98   mp = vl_msg_api_alloc_as_if_client (sizeof (*mp));
99   memset (mp, 0, sizeof (*mp));
100   mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_MAP_ANOTHER_SEGMENT);
101   mp->segment_size = sp->ssvm_size;
102   strncpy ((char *) mp->segment_name, (char *) sp->name,
103            sizeof (mp->segment_name) - 1);
104
105   vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
106
107   if (ssvm_type (sp) == SSVM_SEGMENT_MEMFD)
108     return session_send_memfd_fd (reg, sp);
109
110   return 0;
111 }
112
113 static int
114 send_del_segment_callback (u32 api_client_index, const ssvm_private_t * fs)
115 {
116   vl_api_unmap_segment_t *mp;
117   vl_api_registration_t *reg;
118
119   reg = vl_mem_api_client_index_to_registration (api_client_index);
120   if (!reg)
121     {
122       clib_warning ("no registration: %u", api_client_index);
123       return -1;
124     }
125
126   if (ssvm_type (fs) == SSVM_SEGMENT_MEMFD
127       && vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
128     {
129       clib_warning ("can't send memfd fd");
130       return -1;
131     }
132
133   mp = vl_msg_api_alloc_as_if_client (sizeof (*mp));
134   memset (mp, 0, sizeof (*mp));
135   mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_UNMAP_SEGMENT);
136   strncpy ((char *) mp->segment_name, (char *) fs->name,
137            sizeof (mp->segment_name) - 1);
138
139   vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
140
141   if (ssvm_type (fs) == SSVM_SEGMENT_MEMFD)
142     return session_send_memfd_fd (reg, fs);
143
144   return 0;
145 }
146
147 static int
148 send_session_accept_callback (stream_session_t * s)
149 {
150   application_t *server = application_get (s->app_index);
151   transport_proto_vft_t *tp_vft;
152   vl_api_accept_session_t *mp;
153   vl_api_registration_t *reg;
154   transport_connection_t *tc;
155   stream_session_t *listener;
156   svm_queue_t *vpp_queue;
157
158   reg = vl_mem_api_client_index_to_registration (server->api_client_index);
159   if (!reg)
160     {
161       clib_warning ("no registration: %u", server->api_client_index);
162       return -1;
163     }
164
165   mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
166   memset (mp, 0, sizeof (*mp));
167
168   mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_ACCEPT_SESSION);
169   mp->context = server->index;
170   mp->server_rx_fifo = pointer_to_uword (s->server_rx_fifo);
171   mp->server_tx_fifo = pointer_to_uword (s->server_tx_fifo);
172
173   if (session_has_transport (s))
174     {
175       listener = listen_session_get (s->session_type, s->listener_index);
176       mp->listener_handle = listen_session_get_handle (listener);
177       if (application_is_proxy (server))
178         {
179           listener =
180             application_first_listener (server, session_get_fib_proto (s),
181                                         session_get_transport_proto (s));
182           if (listener)
183             mp->listener_handle = listen_session_get_handle (listener);
184         }
185       vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
186       mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
187       mp->handle = session_handle (s);
188       tp_vft = transport_protocol_get_vft (session_get_transport_proto (s));
189       tc = tp_vft->get_connection (s->connection_index, s->thread_index);
190       mp->port = tc->rmt_port;
191       mp->is_ip4 = tc->is_ip4;
192       clib_memcpy (&mp->ip, &tc->rmt_ip, sizeof (tc->rmt_ip));
193     }
194   else
195     {
196       local_session_t *ls = (local_session_t *) s;
197       local_session_t *ll;
198       if (application_local_session_listener_has_transport (ls))
199         {
200           listener = listen_session_get (ls->listener_session_type,
201                                          ls->listener_index);
202           mp->listener_handle = listen_session_get_handle (listener);
203           mp->is_ip4 = session_type_is_ip4 (listener->session_type);
204         }
205       else
206         {
207           ll = application_get_local_listen_session (server,
208                                                      ls->listener_index);
209           if (ll->transport_listener_index != ~0)
210             {
211               listener = listen_session_get (ll->listener_session_type,
212                                              ll->transport_listener_index);
213               mp->listener_handle = listen_session_get_handle (listener);
214             }
215           else
216             {
217               mp->listener_handle = application_local_session_handle (ll);
218             }
219           mp->is_ip4 = session_type_is_ip4 (ll->listener_session_type);
220         }
221       mp->handle = application_local_session_handle (ls);
222       mp->port = ls->port;
223       mp->vpp_event_queue_address = ls->client_evt_q;
224       mp->server_event_queue_address = ls->server_evt_q;
225     }
226   vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
227
228   return 0;
229 }
230
231 void
232 send_local_session_disconnect_callback (u32 app_index, local_session_t * ls)
233 {
234   application_t *app = application_get (app_index);
235   vl_api_disconnect_session_t *mp;
236   vl_api_registration_t *reg;
237
238   reg = vl_mem_api_client_index_to_registration (app->api_client_index);
239   if (!reg)
240     {
241       clib_warning ("no registration: %u", app->api_client_index);
242       return;
243     }
244
245   mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
246   memset (mp, 0, sizeof (*mp));
247   mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_DISCONNECT_SESSION);
248   mp->handle = application_local_session_handle (ls);
249   mp->context = app->api_client_index;
250   vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
251 }
252
253 static void
254 send_session_disconnect_callback (stream_session_t * s)
255 {
256   application_t *app = application_get (s->app_index);
257   vl_api_disconnect_session_t *mp;
258   vl_api_registration_t *reg;
259
260   reg = vl_mem_api_client_index_to_registration (app->api_client_index);
261   if (!reg)
262     {
263       clib_warning ("no registration: %u", app->api_client_index);
264       return;
265     }
266
267   mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
268   memset (mp, 0, sizeof (*mp));
269   mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_DISCONNECT_SESSION);
270   mp->handle = session_handle (s);
271   mp->context = app->api_client_index;
272   vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
273 }
274
275 static void
276 send_session_reset_callback (stream_session_t * s)
277 {
278   application_t *app = application_get (s->app_index);
279   vl_api_registration_t *reg;
280   vl_api_reset_session_t *mp;
281
282   reg = vl_mem_api_client_index_to_registration (app->api_client_index);
283   if (!reg)
284     {
285       clib_warning ("no registration: %u", app->api_client_index);
286       return;
287     }
288
289   mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
290   memset (mp, 0, sizeof (*mp));
291   mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_RESET_SESSION);
292   mp->handle = session_handle (s);
293   vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
294 }
295
296 int
297 send_session_connected_callback (u32 app_index, u32 api_context,
298                                  stream_session_t * s, u8 is_fail)
299 {
300   vl_api_connect_session_reply_t *mp;
301   transport_connection_t *tc;
302   vl_api_registration_t *reg;
303   svm_queue_t *vpp_queue;
304   application_t *app;
305
306   app = application_get (app_index);
307   reg = vl_mem_api_client_index_to_registration (app->api_client_index);
308   if (!reg)
309     {
310       clib_warning ("no registration: %u", app->api_client_index);
311       return -1;
312     }
313
314   mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
315   mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_CONNECT_SESSION_REPLY);
316   mp->context = api_context;
317
318   if (is_fail)
319     goto done;
320
321   if (session_has_transport (s))
322     {
323       tc = session_get_transport (s);
324       if (!tc)
325         {
326           is_fail = 1;
327           goto done;
328         }
329
330       vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
331       mp->handle = session_handle (s);
332       mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
333       clib_memcpy (mp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
334       mp->is_ip4 = tc->is_ip4;
335       mp->lcl_port = tc->lcl_port;
336       mp->server_rx_fifo = pointer_to_uword (s->server_rx_fifo);
337       mp->server_tx_fifo = pointer_to_uword (s->server_tx_fifo);
338     }
339   else
340     {
341       local_session_t *ls = (local_session_t *) s;
342       mp->handle = application_local_session_handle (ls);
343       mp->lcl_port = ls->port;
344       mp->vpp_event_queue_address = ls->server_evt_q;
345       mp->client_event_queue_address = ls->client_evt_q;
346       mp->server_rx_fifo = pointer_to_uword (s->server_tx_fifo);
347       mp->server_tx_fifo = pointer_to_uword (s->server_rx_fifo);
348     }
349
350 done:
351   mp->retval = is_fail ?
352     clib_host_to_net_u32 (VNET_API_ERROR_SESSION_CONNECT) : 0;
353   vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
354   return 0;
355 }
356
357 static session_cb_vft_t session_cb_vft = {
358   .session_accept_callback = send_session_accept_callback,
359   .session_disconnect_callback = send_session_disconnect_callback,
360   .session_connected_callback = send_session_connected_callback,
361   .session_reset_callback = send_session_reset_callback,
362   .add_segment_callback = send_add_segment_callback,
363   .del_segment_callback = send_del_segment_callback,
364 };
365
366 static void
367 vl_api_session_enable_disable_t_handler (vl_api_session_enable_disable_t * mp)
368 {
369   vl_api_session_enable_disable_reply_t *rmp;
370   vlib_main_t *vm = vlib_get_main ();
371   int rv = 0;
372
373   vnet_session_enable_disable (vm, mp->is_enable);
374   REPLY_MACRO (VL_API_SESSION_ENABLE_DISABLE_REPLY);
375 }
376
377 static void
378 vl_api_application_attach_t_handler (vl_api_application_attach_t * mp)
379 {
380   vl_api_application_attach_reply_t *rmp;
381   ssvm_private_t *segp, *evt_q_segment;
382   vnet_app_attach_args_t _a, *a = &_a;
383   vl_api_registration_t *reg;
384   clib_error_t *error = 0;
385   int rv = 0;
386
387   reg = vl_api_client_index_to_registration (mp->client_index);
388   if (!reg)
389     return;
390
391   if (session_manager_is_enabled () == 0)
392     {
393       rv = VNET_API_ERROR_FEATURE_DISABLED;
394       goto done;
395     }
396
397   STATIC_ASSERT (sizeof (u64) * APP_OPTIONS_N_OPTIONS <=
398                  sizeof (mp->options),
399                  "Out of options, fix api message definition");
400
401   memset (a, 0, sizeof (*a));
402   a->api_client_index = mp->client_index;
403   a->options = mp->options;
404   a->session_cb_vft = &session_cb_vft;
405
406   if (mp->namespace_id_len > 64)
407     {
408       rv = VNET_API_ERROR_INVALID_VALUE;
409       goto done;
410     }
411
412   if (mp->namespace_id_len)
413     {
414       vec_validate (a->namespace_id, mp->namespace_id_len - 1);
415       clib_memcpy (a->namespace_id, mp->namespace_id, mp->namespace_id_len);
416     }
417
418   if ((error = vnet_application_attach (a)))
419     {
420       rv = clib_error_get_code (error);
421       clib_error_report (error);
422     }
423   vec_free (a->namespace_id);
424
425 done:
426
427   /* *INDENT-OFF* */
428   REPLY_MACRO2 (VL_API_APPLICATION_ATTACH_REPLY, ({
429     if (!rv)
430       {
431         segp = a->segment;
432         rmp->segment_name_length = 0;
433         rmp->segment_size = segp->ssvm_size;
434         if (vec_len (segp->name))
435           {
436             memcpy (rmp->segment_name, segp->name, vec_len (segp->name));
437             rmp->segment_name_length = vec_len (segp->name);
438           }
439         rmp->app_event_queue_address = a->app_event_queue_address;
440       }
441   }));
442   /* *INDENT-ON* */
443
444   if (rv)
445     return;
446
447   /* Send fifo segment fd if needed */
448   if (ssvm_type (a->segment) == SSVM_SEGMENT_MEMFD)
449     session_send_memfd_fd (reg, a->segment);
450   /* Send event queues segment */
451   if ((evt_q_segment = session_manager_get_evt_q_segment ()))
452     session_send_memfd_fd (reg, evt_q_segment);
453 }
454
455 static void
456 vl_api_application_detach_t_handler (vl_api_application_detach_t * mp)
457 {
458   vl_api_application_detach_reply_t *rmp;
459   int rv = VNET_API_ERROR_INVALID_VALUE_2;
460   vnet_app_detach_args_t _a, *a = &_a;
461   application_t *app;
462
463   if (session_manager_is_enabled () == 0)
464     {
465       rv = VNET_API_ERROR_FEATURE_DISABLED;
466       goto done;
467     }
468
469   app = application_lookup (mp->client_index);
470   if (app)
471     {
472       a->app_index = app->index;
473       rv = vnet_application_detach (a);
474     }
475
476 done:
477   REPLY_MACRO (VL_API_APPLICATION_DETACH_REPLY);
478 }
479
480 static void
481 vl_api_bind_uri_t_handler (vl_api_bind_uri_t * mp)
482 {
483   vl_api_bind_uri_reply_t *rmp;
484   vnet_bind_args_t _a, *a = &_a;
485   application_t *app;
486   int rv;
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       memset (a, 0, sizeof (*a));
498       a->uri = (char *) mp->uri;
499       a->app_index = app->index;
500       rv = vnet_bind_uri (a);
501     }
502   else
503     {
504       rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
505     }
506
507 done:
508   REPLY_MACRO (VL_API_BIND_URI_REPLY);
509 }
510
511 static void
512 vl_api_unbind_uri_t_handler (vl_api_unbind_uri_t * mp)
513 {
514   vl_api_unbind_uri_reply_t *rmp;
515   application_t *app;
516   vnet_unbind_args_t _a, *a = &_a;
517   int rv;
518
519   if (session_manager_is_enabled () == 0)
520     {
521       rv = VNET_API_ERROR_FEATURE_DISABLED;
522       goto done;
523     }
524
525   app = application_lookup (mp->client_index);
526   if (app)
527     {
528       a->uri = (char *) mp->uri;
529       a->app_index = app->index;
530       rv = vnet_unbind_uri (a);
531     }
532   else
533     {
534       rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
535     }
536
537 done:
538   REPLY_MACRO (VL_API_UNBIND_URI_REPLY);
539 }
540
541 static void
542 vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp)
543 {
544   vl_api_connect_session_reply_t *rmp;
545   vnet_connect_args_t _a, *a = &_a;
546   application_t *app;
547   clib_error_t *error = 0;
548   int rv = 0;
549
550   if (session_manager_is_enabled () == 0)
551     {
552       rv = VNET_API_ERROR_FEATURE_DISABLED;
553       goto done;
554     }
555
556   app = application_lookup (mp->client_index);
557   if (app)
558     {
559       a->uri = (char *) mp->uri;
560       a->api_context = mp->context;
561       a->app_index = app->index;
562       a->mp = mp;
563       if ((error = vnet_connect_uri (a)))
564         {
565           rv = clib_error_get_code (error);
566           if (rv != VNET_API_ERROR_SESSION_REDIRECT)
567             clib_error_report (error);
568         }
569     }
570   else
571     {
572       rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
573     }
574
575   /*
576    * Don't reply to stream (tcp) connects. The reply will come once
577    * the connection is established. In case of the redirects, the reply
578    * will come from the server app.
579    */
580   if (rv == 0 || rv == VNET_API_ERROR_SESSION_REDIRECT)
581     return;
582
583 done:
584   /* *INDENT-OFF* */
585   REPLY_MACRO (VL_API_CONNECT_SESSION_REPLY);
586   /* *INDENT-ON* */
587 }
588
589 static void
590 vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
591 {
592   vl_api_disconnect_session_reply_t *rmp;
593   vnet_disconnect_args_t _a, *a = &_a;
594   application_t *app;
595   int rv = 0;
596
597   if (session_manager_is_enabled () == 0)
598     {
599       rv = VNET_API_ERROR_FEATURE_DISABLED;
600       goto done;
601     }
602
603   app = application_lookup (mp->client_index);
604   if (app)
605     {
606       a->handle = mp->handle;
607       a->app_index = app->index;
608       rv = vnet_disconnect_session (a);
609     }
610   else
611     {
612       rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
613     }
614
615 done:
616   REPLY_MACRO2 (VL_API_DISCONNECT_SESSION_REPLY, rmp->handle = mp->handle);
617 }
618
619 static void
620 vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
621                                            mp)
622 {
623   vnet_disconnect_args_t _a, *a = &_a;
624   application_t *app;
625
626   /* Client objected to disconnecting the session, log and continue */
627   if (mp->retval)
628     {
629       clib_warning ("client retval %d", mp->retval);
630       return;
631     }
632
633   /* Disconnect has been confirmed. Confirm close to transport */
634   app = application_lookup (mp->context);
635   if (app)
636     {
637       a->handle = mp->handle;
638       a->app_index = app->index;
639       vnet_disconnect_session (a);
640     }
641 }
642
643 static void
644 vl_api_reset_session_reply_t_handler (vl_api_reset_session_reply_t * mp)
645 {
646   application_t *app;
647   stream_session_t *s;
648   u32 index, thread_index;
649
650   app = application_lookup (mp->client_index);
651   if (!app)
652     return;
653
654   session_parse_handle (mp->handle, &index, &thread_index);
655   s = session_get_if_valid (index, thread_index);
656   if (s == 0 || app->index != s->app_index)
657     {
658       clib_warning ("Invalid session!");
659       return;
660     }
661
662   /* Client objected to resetting the session, log and continue */
663   if (mp->retval)
664     {
665       clib_warning ("client retval %d", mp->retval);
666       return;
667     }
668
669   /* This comes as a response to a reset, transport only waiting for
670    * confirmation to remove connection state, no need to disconnect */
671   stream_session_cleanup (s);
672 }
673
674 static void
675 vl_api_accept_session_reply_t_handler (vl_api_accept_session_reply_t * mp)
676 {
677   vnet_disconnect_args_t _a = { 0 }, *a = &_a;
678   local_session_t *ls;
679   stream_session_t *s;
680
681   /* Server isn't interested, kill the session */
682   if (mp->retval)
683     {
684       a->app_index = mp->context;
685       a->handle = mp->handle;
686       vnet_disconnect_session (a);
687       return;
688     }
689
690   if (session_handle_is_local (mp->handle))
691     {
692       ls = application_get_local_session_from_handle (mp->handle);
693       if (!ls || ls->app_index != mp->context)
694         {
695           clib_warning ("server %u doesn't own local handle %llu",
696                         mp->context, mp->handle);
697           return;
698         }
699       if (application_local_session_connect_notify (ls))
700         return;
701       ls->session_state = SESSION_STATE_READY;
702     }
703   else
704     {
705       s = session_get_from_handle_if_valid (mp->handle);
706       if (!s)
707         {
708           clib_warning ("session doesn't exist");
709           return;
710         }
711       if (s->app_index != mp->context)
712         {
713           clib_warning ("app doesn't own session");
714           return;
715         }
716       s->session_state = SESSION_STATE_READY;
717     }
718 }
719
720 static void
721 vl_api_map_another_segment_reply_t_handler (vl_api_map_another_segment_reply_t
722                                             * mp)
723 {
724   clib_warning ("not implemented");
725 }
726
727 static void
728 vl_api_bind_sock_t_handler (vl_api_bind_sock_t * mp)
729 {
730   vl_api_bind_sock_reply_t *rmp;
731   vnet_bind_args_t _a, *a = &_a;
732   int rv = 0;
733   clib_error_t *error;
734   application_t *app = 0;
735   stream_session_t *s;
736   transport_connection_t *tc = 0;
737   ip46_address_t *ip46;
738
739   if (session_manager_is_enabled () == 0)
740     {
741       rv = VNET_API_ERROR_FEATURE_DISABLED;
742       goto done;
743     }
744
745   app = application_lookup (mp->client_index);
746   if (!app)
747     {
748       rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
749       goto done;
750     }
751
752   ip46 = (ip46_address_t *) mp->ip;
753   memset (a, 0, sizeof (*a));
754   a->sep.is_ip4 = mp->is_ip4;
755   a->sep.ip = *ip46;
756   a->sep.port = mp->port;
757   a->sep.fib_index = mp->vrf;
758   a->sep.sw_if_index = ENDPOINT_INVALID_INDEX;
759   a->sep.transport_proto = mp->proto;
760   a->app_index = app->index;
761
762   if ((error = vnet_bind (a)))
763     {
764       rv = clib_error_get_code (error);
765       clib_error_report (error);
766     }
767
768 done:
769   /* *INDENT-OFF* */
770   REPLY_MACRO2 (VL_API_BIND_SOCK_REPLY,({
771     if (!rv)
772       {
773         rmp->handle = a->handle;
774         rmp->lcl_port = mp->port;
775         if (app && application_has_global_scope (app))
776           {
777             s = listen_session_get_from_handle (a->handle);
778             tc = listen_session_get_transport (s);
779             rmp->lcl_is_ip4 = tc->is_ip4;
780             clib_memcpy (rmp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
781           }
782       }
783   }));
784   /* *INDENT-ON* */
785 }
786
787 static void
788 vl_api_unbind_sock_t_handler (vl_api_unbind_sock_t * mp)
789 {
790   vl_api_unbind_sock_reply_t *rmp;
791   vnet_unbind_args_t _a, *a = &_a;
792   application_t *app;
793   clib_error_t *error;
794   int rv = 0;
795
796   if (session_manager_is_enabled () == 0)
797     {
798       rv = VNET_API_ERROR_FEATURE_DISABLED;
799       goto done;
800     }
801
802   app = application_lookup (mp->client_index);
803   if (app)
804     {
805       a->app_index = app->index;
806       a->handle = mp->handle;
807       if ((error = vnet_unbind (a)))
808         {
809           rv = clib_error_get_code (error);
810           clib_error_report (error);
811         }
812     }
813
814 done:
815   REPLY_MACRO (VL_API_UNBIND_SOCK_REPLY);
816 }
817
818 static void
819 vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
820 {
821   vl_api_connect_session_reply_t *rmp;
822   vnet_connect_args_t _a, *a = &_a;
823   application_t *app;
824   clib_error_t *error = 0;
825   int rv = 0;
826
827   if (session_manager_is_enabled () == 0)
828     {
829       rv = VNET_API_ERROR_FEATURE_DISABLED;
830       goto done;
831     }
832
833   app = application_lookup (mp->client_index);
834   if (app)
835     {
836       svm_queue_t *client_q;
837       ip46_address_t *ip46 = (ip46_address_t *) mp->ip;
838
839       client_q = vl_api_client_index_to_input_queue (mp->client_index);
840       mp->client_queue_address = pointer_to_uword (client_q);
841       a->sep.is_ip4 = mp->is_ip4;
842       a->sep.ip = *ip46;
843       a->sep.port = mp->port;
844       a->sep.transport_proto = mp->proto;
845       a->sep.fib_index = mp->vrf;
846       a->sep.sw_if_index = ENDPOINT_INVALID_INDEX;
847       a->api_context = mp->context;
848       a->app_index = app->index;
849       a->mp = mp;
850       if ((error = vnet_connect (a)))
851         {
852           rv = clib_error_get_code (error);
853           if (rv != VNET_API_ERROR_SESSION_REDIRECT)
854             clib_error_report (error);
855         }
856     }
857   else
858     {
859       rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
860     }
861
862   if (rv == 0 || rv == VNET_API_ERROR_SESSION_REDIRECT)
863     return;
864
865   /* Got some error, relay it */
866
867 done:
868   REPLY_MACRO (VL_API_CONNECT_SESSION_REPLY);
869 }
870
871 static void
872 vl_api_app_namespace_add_del_t_handler (vl_api_app_namespace_add_del_t * mp)
873 {
874   vl_api_app_namespace_add_del_reply_t *rmp;
875   clib_error_t *error = 0;
876   u32 appns_index = 0;
877   u8 *ns_id = 0;
878   int rv = 0;
879   if (!session_manager_is_enabled ())
880     {
881       rv = VNET_API_ERROR_FEATURE_DISABLED;
882       goto done;
883     }
884
885   if (mp->namespace_id_len > ARRAY_LEN (mp->namespace_id))
886     {
887       rv = VNET_API_ERROR_INVALID_VALUE;
888       goto done;
889     }
890
891   vec_validate (ns_id, mp->namespace_id_len - 1);
892   clib_memcpy (ns_id, mp->namespace_id, mp->namespace_id_len);
893   vnet_app_namespace_add_del_args_t args = {
894     .ns_id = ns_id,
895     .secret = clib_net_to_host_u64 (mp->secret),
896     .sw_if_index = clib_net_to_host_u32 (mp->sw_if_index),
897     .ip4_fib_id = clib_net_to_host_u32 (mp->ip4_fib_id),
898     .ip6_fib_id = clib_net_to_host_u32 (mp->ip6_fib_id),
899     .is_add = 1
900   };
901   error = vnet_app_namespace_add_del (&args);
902   if (error)
903     {
904       rv = clib_error_get_code (error);
905       clib_error_report (error);
906     }
907   else
908     {
909       appns_index = app_namespace_index_from_id (ns_id);
910       if (appns_index == APP_NAMESPACE_INVALID_INDEX)
911         {
912           clib_warning ("app ns lookup failed");
913           rv = VNET_API_ERROR_UNSPECIFIED;
914         }
915     }
916   vec_free (ns_id);
917
918   /* *INDENT-OFF* */
919 done:
920   REPLY_MACRO2 (VL_API_APP_NAMESPACE_ADD_DEL_REPLY, ({
921     if (!rv)
922       rmp->appns_index = clib_host_to_net_u32 (appns_index);
923   }));
924   /* *INDENT-ON* */
925 }
926
927 static void
928 vl_api_session_rule_add_del_t_handler (vl_api_session_rule_add_del_t * mp)
929 {
930   vl_api_session_rule_add_del_reply_t *rmp;
931   session_rule_add_del_args_t args;
932   session_rule_table_add_del_args_t *table_args = &args.table_args;
933   clib_error_t *error;
934   u8 fib_proto;
935   int rv = 0;
936
937   memset (&args, 0, sizeof (args));
938   fib_proto = mp->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
939
940   table_args->lcl.fp_len = mp->lcl_plen;
941   table_args->lcl.fp_proto = fib_proto;
942   table_args->rmt.fp_len = mp->rmt_plen;
943   table_args->rmt.fp_proto = fib_proto;
944   table_args->lcl_port = mp->lcl_port;
945   table_args->rmt_port = mp->rmt_port;
946   table_args->action_index = clib_net_to_host_u32 (mp->action_index);
947   table_args->is_add = mp->is_add;
948   mp->tag[sizeof (mp->tag) - 1] = 0;
949   table_args->tag = format (0, "%s", mp->tag);
950   args.appns_index = clib_net_to_host_u32 (mp->appns_index);
951   args.scope = mp->scope;
952   args.transport_proto = mp->transport_proto;
953
954   memset (&table_args->lcl.fp_addr, 0, sizeof (table_args->lcl.fp_addr));
955   memset (&table_args->rmt.fp_addr, 0, sizeof (table_args->rmt.fp_addr));
956   ip_set (&table_args->lcl.fp_addr, mp->lcl_ip, mp->is_ip4);
957   ip_set (&table_args->rmt.fp_addr, mp->rmt_ip, mp->is_ip4);
958   error = vnet_session_rule_add_del (&args);
959   if (error)
960     {
961       rv = clib_error_get_code (error);
962       clib_error_report (error);
963     }
964   vec_free (table_args->tag);
965   REPLY_MACRO (VL_API_SESSION_RULE_ADD_DEL_REPLY);
966 }
967
968 static void
969 send_session_rule_details4 (mma_rule_16_t * rule, u8 is_local,
970                             u8 transport_proto, u32 appns_index, u8 * tag,
971                             vl_api_registration_t * reg, u32 context)
972 {
973   vl_api_session_rules_details_t *rmp = 0;
974   session_mask_or_match_4_t *match =
975     (session_mask_or_match_4_t *) & rule->match;
976   session_mask_or_match_4_t *mask =
977     (session_mask_or_match_4_t *) & rule->mask;
978
979   rmp = vl_msg_api_alloc (sizeof (*rmp));
980   memset (rmp, 0, sizeof (*rmp));
981   rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS);
982   rmp->context = context;
983
984   rmp->is_ip4 = 1;
985   clib_memcpy (rmp->lcl_ip, &match->lcl_ip, sizeof (match->lcl_ip));
986   clib_memcpy (rmp->rmt_ip, &match->rmt_ip, sizeof (match->rmt_ip));
987   rmp->lcl_plen = ip4_mask_to_preflen (&mask->lcl_ip);
988   rmp->rmt_plen = ip4_mask_to_preflen (&mask->rmt_ip);
989   rmp->lcl_port = match->lcl_port;
990   rmp->rmt_port = match->rmt_port;
991   rmp->action_index = clib_host_to_net_u32 (rule->action_index);
992   rmp->scope =
993     is_local ? SESSION_RULE_SCOPE_LOCAL : SESSION_RULE_SCOPE_GLOBAL;
994   rmp->transport_proto = transport_proto;
995   rmp->appns_index = clib_host_to_net_u32 (appns_index);
996   if (tag)
997     {
998       clib_memcpy (rmp->tag, tag, vec_len (tag));
999       rmp->tag[vec_len (tag)] = 0;
1000     }
1001
1002   vl_api_send_msg (reg, (u8 *) rmp);
1003 }
1004
1005 static void
1006 send_session_rule_details6 (mma_rule_40_t * rule, u8 is_local,
1007                             u8 transport_proto, u32 appns_index, u8 * tag,
1008                             vl_api_registration_t * reg, u32 context)
1009 {
1010   vl_api_session_rules_details_t *rmp = 0;
1011   session_mask_or_match_6_t *match =
1012     (session_mask_or_match_6_t *) & rule->match;
1013   session_mask_or_match_6_t *mask =
1014     (session_mask_or_match_6_t *) & rule->mask;
1015
1016   rmp = vl_msg_api_alloc (sizeof (*rmp));
1017   memset (rmp, 0, sizeof (*rmp));
1018   rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS);
1019   rmp->context = context;
1020
1021   rmp->is_ip4 = 0;
1022   clib_memcpy (rmp->lcl_ip, &match->lcl_ip, sizeof (match->lcl_ip));
1023   clib_memcpy (rmp->rmt_ip, &match->rmt_ip, sizeof (match->rmt_ip));
1024   rmp->lcl_plen = ip6_mask_to_preflen (&mask->lcl_ip);
1025   rmp->rmt_plen = ip6_mask_to_preflen (&mask->rmt_ip);
1026   rmp->lcl_port = match->lcl_port;
1027   rmp->rmt_port = match->rmt_port;
1028   rmp->action_index = clib_host_to_net_u32 (rule->action_index);
1029   rmp->scope =
1030     is_local ? SESSION_RULE_SCOPE_LOCAL : SESSION_RULE_SCOPE_GLOBAL;
1031   rmp->transport_proto = transport_proto;
1032   rmp->appns_index = clib_host_to_net_u32 (appns_index);
1033   if (tag)
1034     {
1035       clib_memcpy (rmp->tag, tag, vec_len (tag));
1036       rmp->tag[vec_len (tag)] = 0;
1037     }
1038
1039   vl_api_send_msg (reg, (u8 *) rmp);
1040 }
1041
1042 static void
1043 send_session_rules_table_details (session_rules_table_t * srt, u8 fib_proto,
1044                                   u8 tp, u8 is_local, u32 appns_index,
1045                                   vl_api_registration_t * reg, u32 context)
1046 {
1047   mma_rule_16_t *rule16;
1048   mma_rule_40_t *rule40;
1049   mma_rules_table_16_t *srt16;
1050   mma_rules_table_40_t *srt40;
1051   u32 ri;
1052
1053   if (is_local || fib_proto == FIB_PROTOCOL_IP4)
1054     {
1055       u8 *tag = 0;
1056       /* *INDENT-OFF* */
1057       srt16 = &srt->session_rules_tables_16;
1058       pool_foreach (rule16, srt16->rules, ({
1059         ri = mma_rules_table_rule_index_16 (srt16, rule16);
1060         tag = session_rules_table_rule_tag (srt, ri, 1);
1061         send_session_rule_details4 (rule16, is_local, tp, appns_index, tag,
1062                                     reg, context);
1063       }));
1064       /* *INDENT-ON* */
1065     }
1066   if (is_local || fib_proto == FIB_PROTOCOL_IP6)
1067     {
1068       u8 *tag = 0;
1069       /* *INDENT-OFF* */
1070       srt40 = &srt->session_rules_tables_40;
1071       pool_foreach (rule40, srt40->rules, ({
1072         ri = mma_rules_table_rule_index_40 (srt40, rule40);
1073         tag = session_rules_table_rule_tag (srt, ri, 1);
1074         send_session_rule_details6 (rule40, is_local, tp, appns_index, tag,
1075                                     reg, context);
1076       }));
1077       /* *INDENT-ON* */
1078     }
1079 }
1080
1081 static void
1082 vl_api_session_rules_dump_t_handler (vl_api_one_map_server_dump_t * mp)
1083 {
1084   vl_api_registration_t *reg;
1085   session_table_t *st;
1086   u8 tp;
1087
1088   reg = vl_api_client_index_to_registration (mp->client_index);
1089   if (!reg)
1090     return;
1091
1092   /* *INDENT-OFF* */
1093   session_table_foreach (st, ({
1094     for (tp = 0; tp < TRANSPORT_N_PROTO; tp++)
1095       {
1096         send_session_rules_table_details (&st->session_rules[tp],
1097                                           st->active_fib_proto, tp,
1098                                           st->is_local, st->appns_index, reg,
1099                                           mp->context);
1100       }
1101   }));
1102   /* *INDENT-ON* */
1103 }
1104
1105 static clib_error_t *
1106 application_reaper_cb (u32 client_index)
1107 {
1108   application_t *app = application_lookup (client_index);
1109   vnet_app_detach_args_t _a, *a = &_a;
1110   if (app)
1111     {
1112       a->app_index = app->index;
1113       vnet_application_detach (a);
1114     }
1115   return 0;
1116 }
1117
1118 VL_MSG_API_REAPER_FUNCTION (application_reaper_cb);
1119
1120 #define vl_msg_name_crc_list
1121 #include <vnet/vnet_all_api_h.h>
1122 #undef vl_msg_name_crc_list
1123
1124 static void
1125 setup_message_id_table (api_main_t * am)
1126 {
1127 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1128   foreach_vl_msg_name_crc_session;
1129 #undef _
1130 }
1131
1132 /*
1133  * session_api_hookup
1134  * Add uri's API message handlers to the table.
1135  * vlib has alread mapped shared memory and
1136  * added the client registration handlers.
1137  * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
1138  */
1139 static clib_error_t *
1140 session_api_hookup (vlib_main_t * vm)
1141 {
1142   api_main_t *am = &api_main;
1143
1144 #define _(N,n)                                                  \
1145     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
1146                            vl_api_##n##_t_handler,              \
1147                            vl_noop_handler,                     \
1148                            vl_api_##n##_t_endian,               \
1149                            vl_api_##n##_t_print,                \
1150                            sizeof(vl_api_##n##_t), 1);
1151   foreach_session_api_msg;
1152 #undef _
1153
1154   /*
1155    * Messages which bounce off the data-plane to
1156    * an API client. Simply tells the message handling infra not
1157    * to free the message.
1158    *
1159    * Bounced message handlers MUST NOT block the data plane
1160    */
1161   am->message_bounce[VL_API_CONNECT_URI] = 1;
1162   am->message_bounce[VL_API_CONNECT_SOCK] = 1;
1163
1164   /*
1165    * Set up the (msg_name, crc, message-id) table
1166    */
1167   setup_message_id_table (am);
1168
1169   return 0;
1170 }
1171
1172 VLIB_API_INIT_FUNCTION (session_api_hookup);
1173
1174 /*
1175  * fd.io coding-style-patch-verification: ON
1176  *
1177  * Local Variables:
1178  * eval: (c-set-style "gnu")
1179  * End:
1180  */