session: use generic session pool for listeners
[vpp.git] / src / vnet / session / session_api.c
1 /*
2  * Copyright (c) 2015-2016 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <vnet/vnet.h>
17 #include <vlibmemory/api.h>
18 #include <vnet/session/application.h>
19 #include <vnet/session/application_interface.h>
20 #include <vnet/session/session_rules_table.h>
21 #include <vnet/session/session_table.h>
22
23 #include <vnet/vnet_msg_enum.h>
24
25 #define vl_typedefs             /* define message structures */
26 #include <vnet/vnet_all_api_h.h>
27 #undef vl_typedefs
28
29 #define vl_endianfun            /* define message structures */
30 #include <vnet/vnet_all_api_h.h>
31 #undef vl_endianfun
32
33 /* instantiate all the print functions we know about */
34 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
35 #define vl_printfun
36 #include <vnet/vnet_all_api_h.h>
37 #undef vl_printfun
38
39 #include <vlibapi/api_helper_macros.h>
40
41 #define foreach_session_api_msg                                         \
42 _(MAP_ANOTHER_SEGMENT_REPLY, map_another_segment_reply)                 \
43 _(APPLICATION_ATTACH, application_attach)                               \
44 _(APPLICATION_DETACH, application_detach)                               \
45 _(BIND_URI, bind_uri)                                                   \
46 _(UNBIND_URI, unbind_uri)                                               \
47 _(CONNECT_URI, connect_uri)                                             \
48 _(DISCONNECT_SESSION, disconnect_session)                               \
49 _(DISCONNECT_SESSION_REPLY, disconnect_session_reply)                   \
50 _(ACCEPT_SESSION_REPLY, accept_session_reply)                           \
51 _(RESET_SESSION_REPLY, reset_session_reply)                             \
52 _(BIND_SOCK, bind_sock)                                                 \
53 _(UNBIND_SOCK, unbind_sock)                                             \
54 _(CONNECT_SOCK, connect_sock)                                           \
55 _(SESSION_ENABLE_DISABLE, session_enable_disable)                       \
56 _(APP_NAMESPACE_ADD_DEL, app_namespace_add_del)                         \
57 _(SESSION_RULE_ADD_DEL, session_rule_add_del)                           \
58 _(SESSION_RULES_DUMP, session_rules_dump)                               \
59 _(APPLICATION_TLS_CERT_ADD, application_tls_cert_add)                   \
60 _(APPLICATION_TLS_KEY_ADD, application_tls_key_add)                     \
61
62 static int
63 session_send_memfd_fd (vl_api_registration_t * reg, const ssvm_private_t * sp)
64 {
65   clib_error_t *error;
66   if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
67     {
68       clib_warning ("can't send memfd fd");
69       return -1;
70     }
71   error = vl_api_send_fd_msg (reg, sp->fd);
72   if (error)
73     {
74       clib_error_report (error);
75       return -1;
76     }
77   return 0;
78 }
79
80 static int
81 send_add_segment_callback (u32 api_client_index, const ssvm_private_t * sp)
82 {
83   vl_api_map_another_segment_t *mp;
84   vl_api_registration_t *reg;
85
86   reg = vl_mem_api_client_index_to_registration (api_client_index);
87   if (!reg)
88     {
89       clib_warning ("no registration: %u", api_client_index);
90       return -1;
91     }
92
93   if (ssvm_type (sp) == SSVM_SEGMENT_MEMFD
94       && vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
95     {
96       clib_warning ("can't send memfd fd");
97       return -1;
98     }
99
100   mp = vl_msg_api_alloc_as_if_client (sizeof (*mp));
101   memset (mp, 0, sizeof (*mp));
102   mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_MAP_ANOTHER_SEGMENT);
103   mp->segment_size = sp->ssvm_size;
104   strncpy ((char *) mp->segment_name, (char *) sp->name,
105            sizeof (mp->segment_name) - 1);
106
107   vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
108
109   if (ssvm_type (sp) == SSVM_SEGMENT_MEMFD)
110     return session_send_memfd_fd (reg, sp);
111
112   return 0;
113 }
114
115 static int
116 send_del_segment_callback (u32 api_client_index, const ssvm_private_t * fs)
117 {
118   vl_api_unmap_segment_t *mp;
119   vl_api_registration_t *reg;
120
121   reg = vl_mem_api_client_index_to_registration (api_client_index);
122   if (!reg)
123     {
124       clib_warning ("no registration: %u", api_client_index);
125       return -1;
126     }
127
128   if (ssvm_type (fs) == SSVM_SEGMENT_MEMFD
129       && vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
130     {
131       clib_warning ("can't send memfd fd");
132       return -1;
133     }
134
135   mp = vl_msg_api_alloc_as_if_client (sizeof (*mp));
136   memset (mp, 0, sizeof (*mp));
137   mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_UNMAP_SEGMENT);
138   strncpy ((char *) mp->segment_name, (char *) fs->name,
139            sizeof (mp->segment_name) - 1);
140
141   vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
142
143   if (ssvm_type (fs) == SSVM_SEGMENT_MEMFD)
144     return session_send_memfd_fd (reg, fs);
145
146   return 0;
147 }
148
149 static int
150 send_session_accept_callback (stream_session_t * s)
151 {
152   application_t *server = application_get (s->app_index);
153   transport_proto_vft_t *tp_vft;
154   vl_api_accept_session_t *mp;
155   vl_api_registration_t *reg;
156   transport_connection_t *tc;
157   stream_session_t *listener;
158   svm_queue_t *vpp_queue;
159
160   reg = vl_mem_api_client_index_to_registration (server->api_client_index);
161   if (!reg)
162     {
163       clib_warning ("no registration: %u", server->api_client_index);
164       return -1;
165     }
166
167   mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
168   memset (mp, 0, sizeof (*mp));
169
170   mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_ACCEPT_SESSION);
171   mp->context = server->index;
172   mp->server_rx_fifo = pointer_to_uword (s->server_rx_fifo);
173   mp->server_tx_fifo = pointer_to_uword (s->server_tx_fifo);
174
175   if (session_has_transport (s))
176     {
177       listener = listen_session_get (s->listener_index);
178       mp->listener_handle = listen_session_get_handle (listener);
179       if (application_is_proxy (server))
180         {
181           listener =
182             application_first_listener (server, session_get_fib_proto (s),
183                                         session_get_transport_proto (s));
184           if (listener)
185             mp->listener_handle = listen_session_get_handle (listener);
186         }
187       vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
188       mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
189       mp->handle = session_handle (s);
190       tp_vft = transport_protocol_get_vft (session_get_transport_proto (s));
191       tc = tp_vft->get_connection (s->connection_index, s->thread_index);
192       mp->port = tc->rmt_port;
193       mp->is_ip4 = tc->is_ip4;
194       clib_memcpy (&mp->ip, &tc->rmt_ip, sizeof (tc->rmt_ip));
195     }
196   else
197     {
198       local_session_t *ls = (local_session_t *) s;
199       local_session_t *ll;
200       if (application_local_session_listener_has_transport (ls))
201         {
202           listener = listen_session_get (ls->listener_index);
203           mp->listener_handle = listen_session_get_handle (listener);
204           mp->is_ip4 = session_type_is_ip4 (listener->session_type);
205         }
206       else
207         {
208           ll = application_get_local_listen_session (server,
209                                                      ls->listener_index);
210           if (ll->transport_listener_index != ~0)
211             {
212               listener = listen_session_get (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       if ((error = vnet_connect_uri (a)))
563         {
564           rv = clib_error_get_code (error);
565           clib_error_report (error);
566         }
567     }
568   else
569     {
570       rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
571     }
572
573   /*
574    * Don't reply to stream (tcp) connects. The reply will come once
575    * the connection is established. In case of the redirects, the reply
576    * will come from the server app.
577    */
578   if (rv == 0)
579     return;
580
581 done:
582   /* *INDENT-OFF* */
583   REPLY_MACRO (VL_API_CONNECT_SESSION_REPLY);
584   /* *INDENT-ON* */
585 }
586
587 static void
588 vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
589 {
590   vl_api_disconnect_session_reply_t *rmp;
591   vnet_disconnect_args_t _a, *a = &_a;
592   application_t *app;
593   int rv = 0;
594
595   if (session_manager_is_enabled () == 0)
596     {
597       rv = VNET_API_ERROR_FEATURE_DISABLED;
598       goto done;
599     }
600
601   app = application_lookup (mp->client_index);
602   if (app)
603     {
604       a->handle = mp->handle;
605       a->app_index = app->index;
606       rv = vnet_disconnect_session (a);
607     }
608   else
609     {
610       rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
611     }
612
613 done:
614   REPLY_MACRO2 (VL_API_DISCONNECT_SESSION_REPLY, rmp->handle = mp->handle);
615 }
616
617 static void
618 vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
619                                            mp)
620 {
621   vnet_disconnect_args_t _a, *a = &_a;
622   application_t *app;
623
624   /* Client objected to disconnecting the session, log and continue */
625   if (mp->retval)
626     {
627       clib_warning ("client retval %d", mp->retval);
628       return;
629     }
630
631   /* Disconnect has been confirmed. Confirm close to transport */
632   app = application_lookup (mp->context);
633   if (app)
634     {
635       a->handle = mp->handle;
636       a->app_index = app->index;
637       vnet_disconnect_session (a);
638     }
639 }
640
641 static void
642 vl_api_reset_session_reply_t_handler (vl_api_reset_session_reply_t * mp)
643 {
644   application_t *app;
645   stream_session_t *s;
646   u32 index, thread_index;
647
648   app = application_lookup (mp->client_index);
649   if (!app)
650     return;
651
652   session_parse_handle (mp->handle, &index, &thread_index);
653   s = session_get_if_valid (index, thread_index);
654   if (s == 0 || app->index != s->app_index)
655     {
656       clib_warning ("Invalid session!");
657       return;
658     }
659
660   /* Client objected to resetting the session, log and continue */
661   if (mp->retval)
662     {
663       clib_warning ("client retval %d", mp->retval);
664       return;
665     }
666
667   /* This comes as a response to a reset, transport only waiting for
668    * confirmation to remove connection state, no need to disconnect */
669   stream_session_cleanup (s);
670 }
671
672 static void
673 vl_api_accept_session_reply_t_handler (vl_api_accept_session_reply_t * mp)
674 {
675   vnet_disconnect_args_t _a = { 0 }, *a = &_a;
676   local_session_t *ls;
677   stream_session_t *s;
678
679   /* Server isn't interested, kill the session */
680   if (mp->retval)
681     {
682       a->app_index = mp->context;
683       a->handle = mp->handle;
684       vnet_disconnect_session (a);
685       return;
686     }
687
688   if (session_handle_is_local (mp->handle))
689     {
690       ls = application_get_local_session_from_handle (mp->handle);
691       if (!ls || ls->app_index != mp->context)
692         {
693           clib_warning ("server %u doesn't own local handle %llu",
694                         mp->context, mp->handle);
695           return;
696         }
697       if (application_local_session_connect_notify (ls))
698         return;
699       ls->session_state = SESSION_STATE_READY;
700     }
701   else
702     {
703       s = session_get_from_handle_if_valid (mp->handle);
704       if (!s)
705         {
706           clib_warning ("session doesn't exist");
707           return;
708         }
709       if (s->app_index != mp->context)
710         {
711           clib_warning ("app doesn't own session");
712           return;
713         }
714       s->session_state = SESSION_STATE_READY;
715     }
716 }
717
718 static void
719 vl_api_map_another_segment_reply_t_handler (vl_api_map_another_segment_reply_t
720                                             * mp)
721 {
722   clib_warning ("not implemented");
723 }
724
725 static void
726 vl_api_bind_sock_t_handler (vl_api_bind_sock_t * mp)
727 {
728   vl_api_bind_sock_reply_t *rmp;
729   vnet_bind_args_t _a, *a = &_a;
730   int rv = 0;
731   clib_error_t *error;
732   application_t *app = 0;
733   stream_session_t *s;
734   transport_connection_t *tc = 0;
735   ip46_address_t *ip46;
736
737   if (session_manager_is_enabled () == 0)
738     {
739       rv = VNET_API_ERROR_FEATURE_DISABLED;
740       goto done;
741     }
742
743   app = application_lookup (mp->client_index);
744   if (!app)
745     {
746       rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
747       goto done;
748     }
749
750   ip46 = (ip46_address_t *) mp->ip;
751   memset (a, 0, sizeof (*a));
752   a->sep.is_ip4 = mp->is_ip4;
753   a->sep.ip = *ip46;
754   a->sep.port = mp->port;
755   a->sep.fib_index = mp->vrf;
756   a->sep.sw_if_index = ENDPOINT_INVALID_INDEX;
757   a->sep.transport_proto = mp->proto;
758   a->app_index = app->index;
759
760   if ((error = vnet_bind (a)))
761     {
762       rv = clib_error_get_code (error);
763       clib_error_report (error);
764     }
765
766 done:
767   /* *INDENT-OFF* */
768   REPLY_MACRO2 (VL_API_BIND_SOCK_REPLY,({
769     if (!rv)
770       {
771         rmp->handle = a->handle;
772         rmp->lcl_port = mp->port;
773         rmp->lcl_is_ip4 = mp->is_ip4;
774         if (app && application_has_global_scope (app))
775           {
776             s = listen_session_get_from_handle (a->handle);
777             tc = listen_session_get_transport (s);
778             rmp->lcl_is_ip4 = tc->is_ip4;
779             clib_memcpy (rmp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
780           }
781       }
782   }));
783   /* *INDENT-ON* */
784 }
785
786 static void
787 vl_api_unbind_sock_t_handler (vl_api_unbind_sock_t * mp)
788 {
789   vl_api_unbind_sock_reply_t *rmp;
790   vnet_unbind_args_t _a, *a = &_a;
791   application_t *app;
792   clib_error_t *error;
793   int rv = 0;
794
795   if (session_manager_is_enabled () == 0)
796     {
797       rv = VNET_API_ERROR_FEATURE_DISABLED;
798       goto done;
799     }
800
801   app = application_lookup (mp->client_index);
802   if (app)
803     {
804       a->app_index = app->index;
805       a->handle = mp->handle;
806       if ((error = vnet_unbind (a)))
807         {
808           rv = clib_error_get_code (error);
809           clib_error_report (error);
810         }
811     }
812
813 done:
814   REPLY_MACRO (VL_API_UNBIND_SOCK_REPLY);
815 }
816
817 static void
818 vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
819 {
820   vl_api_connect_session_reply_t *rmp;
821   vnet_connect_args_t _a, *a = &_a;
822   application_t *app;
823   clib_error_t *error = 0;
824   int rv = 0;
825
826   if (session_manager_is_enabled () == 0)
827     {
828       rv = VNET_API_ERROR_FEATURE_DISABLED;
829       goto done;
830     }
831
832   app = application_lookup (mp->client_index);
833   if (app)
834     {
835       svm_queue_t *client_q;
836       ip46_address_t *ip46 = (ip46_address_t *) mp->ip;
837
838       memset (a, 0, sizeof (*a));
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       if (mp->hostname_len)
848         {
849           vec_validate (a->sep.hostname, mp->hostname_len - 1);
850           clib_memcpy (a->sep.hostname, mp->hostname, mp->hostname_len);
851         }
852       a->api_context = mp->context;
853       a->app_index = app->index;
854       if ((error = vnet_connect (a)))
855         {
856           rv = clib_error_get_code (error);
857           clib_error_report (error);
858         }
859       vec_free (a->sep.hostname);
860     }
861   else
862     {
863       rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
864     }
865
866   if (rv == 0)
867     return;
868
869   /* Got some error, relay it */
870
871 done:
872   REPLY_MACRO (VL_API_CONNECT_SESSION_REPLY);
873 }
874
875 static void
876 vl_api_app_namespace_add_del_t_handler (vl_api_app_namespace_add_del_t * mp)
877 {
878   vl_api_app_namespace_add_del_reply_t *rmp;
879   clib_error_t *error = 0;
880   u32 appns_index = 0;
881   u8 *ns_id = 0;
882   int rv = 0;
883   if (!session_manager_is_enabled ())
884     {
885       rv = VNET_API_ERROR_FEATURE_DISABLED;
886       goto done;
887     }
888
889   if (mp->namespace_id_len > ARRAY_LEN (mp->namespace_id))
890     {
891       rv = VNET_API_ERROR_INVALID_VALUE;
892       goto done;
893     }
894
895   vec_validate (ns_id, mp->namespace_id_len - 1);
896   clib_memcpy (ns_id, mp->namespace_id, mp->namespace_id_len);
897   vnet_app_namespace_add_del_args_t args = {
898     .ns_id = ns_id,
899     .secret = clib_net_to_host_u64 (mp->secret),
900     .sw_if_index = clib_net_to_host_u32 (mp->sw_if_index),
901     .ip4_fib_id = clib_net_to_host_u32 (mp->ip4_fib_id),
902     .ip6_fib_id = clib_net_to_host_u32 (mp->ip6_fib_id),
903     .is_add = 1
904   };
905   error = vnet_app_namespace_add_del (&args);
906   if (error)
907     {
908       rv = clib_error_get_code (error);
909       clib_error_report (error);
910     }
911   else
912     {
913       appns_index = app_namespace_index_from_id (ns_id);
914       if (appns_index == APP_NAMESPACE_INVALID_INDEX)
915         {
916           clib_warning ("app ns lookup failed");
917           rv = VNET_API_ERROR_UNSPECIFIED;
918         }
919     }
920   vec_free (ns_id);
921
922   /* *INDENT-OFF* */
923 done:
924   REPLY_MACRO2 (VL_API_APP_NAMESPACE_ADD_DEL_REPLY, ({
925     if (!rv)
926       rmp->appns_index = clib_host_to_net_u32 (appns_index);
927   }));
928   /* *INDENT-ON* */
929 }
930
931 static void
932 vl_api_session_rule_add_del_t_handler (vl_api_session_rule_add_del_t * mp)
933 {
934   vl_api_session_rule_add_del_reply_t *rmp;
935   session_rule_add_del_args_t args;
936   session_rule_table_add_del_args_t *table_args = &args.table_args;
937   clib_error_t *error;
938   u8 fib_proto;
939   int rv = 0;
940
941   memset (&args, 0, sizeof (args));
942   fib_proto = mp->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
943
944   table_args->lcl.fp_len = mp->lcl_plen;
945   table_args->lcl.fp_proto = fib_proto;
946   table_args->rmt.fp_len = mp->rmt_plen;
947   table_args->rmt.fp_proto = fib_proto;
948   table_args->lcl_port = mp->lcl_port;
949   table_args->rmt_port = mp->rmt_port;
950   table_args->action_index = clib_net_to_host_u32 (mp->action_index);
951   table_args->is_add = mp->is_add;
952   mp->tag[sizeof (mp->tag) - 1] = 0;
953   table_args->tag = format (0, "%s", mp->tag);
954   args.appns_index = clib_net_to_host_u32 (mp->appns_index);
955   args.scope = mp->scope;
956   args.transport_proto = mp->transport_proto;
957
958   memset (&table_args->lcl.fp_addr, 0, sizeof (table_args->lcl.fp_addr));
959   memset (&table_args->rmt.fp_addr, 0, sizeof (table_args->rmt.fp_addr));
960   ip_set (&table_args->lcl.fp_addr, mp->lcl_ip, mp->is_ip4);
961   ip_set (&table_args->rmt.fp_addr, mp->rmt_ip, mp->is_ip4);
962   error = vnet_session_rule_add_del (&args);
963   if (error)
964     {
965       rv = clib_error_get_code (error);
966       clib_error_report (error);
967     }
968   vec_free (table_args->tag);
969   REPLY_MACRO (VL_API_SESSION_RULE_ADD_DEL_REPLY);
970 }
971
972 static void
973 send_session_rule_details4 (mma_rule_16_t * rule, u8 is_local,
974                             u8 transport_proto, u32 appns_index, u8 * tag,
975                             vl_api_registration_t * reg, u32 context)
976 {
977   vl_api_session_rules_details_t *rmp = 0;
978   session_mask_or_match_4_t *match =
979     (session_mask_or_match_4_t *) & rule->match;
980   session_mask_or_match_4_t *mask =
981     (session_mask_or_match_4_t *) & rule->mask;
982
983   rmp = vl_msg_api_alloc (sizeof (*rmp));
984   memset (rmp, 0, sizeof (*rmp));
985   rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS);
986   rmp->context = context;
987
988   rmp->is_ip4 = 1;
989   clib_memcpy (rmp->lcl_ip, &match->lcl_ip, sizeof (match->lcl_ip));
990   clib_memcpy (rmp->rmt_ip, &match->rmt_ip, sizeof (match->rmt_ip));
991   rmp->lcl_plen = ip4_mask_to_preflen (&mask->lcl_ip);
992   rmp->rmt_plen = ip4_mask_to_preflen (&mask->rmt_ip);
993   rmp->lcl_port = match->lcl_port;
994   rmp->rmt_port = match->rmt_port;
995   rmp->action_index = clib_host_to_net_u32 (rule->action_index);
996   rmp->scope =
997     is_local ? SESSION_RULE_SCOPE_LOCAL : SESSION_RULE_SCOPE_GLOBAL;
998   rmp->transport_proto = transport_proto;
999   rmp->appns_index = clib_host_to_net_u32 (appns_index);
1000   if (tag)
1001     {
1002       clib_memcpy (rmp->tag, tag, vec_len (tag));
1003       rmp->tag[vec_len (tag)] = 0;
1004     }
1005
1006   vl_api_send_msg (reg, (u8 *) rmp);
1007 }
1008
1009 static void
1010 send_session_rule_details6 (mma_rule_40_t * rule, u8 is_local,
1011                             u8 transport_proto, u32 appns_index, u8 * tag,
1012                             vl_api_registration_t * reg, u32 context)
1013 {
1014   vl_api_session_rules_details_t *rmp = 0;
1015   session_mask_or_match_6_t *match =
1016     (session_mask_or_match_6_t *) & rule->match;
1017   session_mask_or_match_6_t *mask =
1018     (session_mask_or_match_6_t *) & rule->mask;
1019
1020   rmp = vl_msg_api_alloc (sizeof (*rmp));
1021   memset (rmp, 0, sizeof (*rmp));
1022   rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS);
1023   rmp->context = context;
1024
1025   rmp->is_ip4 = 0;
1026   clib_memcpy (rmp->lcl_ip, &match->lcl_ip, sizeof (match->lcl_ip));
1027   clib_memcpy (rmp->rmt_ip, &match->rmt_ip, sizeof (match->rmt_ip));
1028   rmp->lcl_plen = ip6_mask_to_preflen (&mask->lcl_ip);
1029   rmp->rmt_plen = ip6_mask_to_preflen (&mask->rmt_ip);
1030   rmp->lcl_port = match->lcl_port;
1031   rmp->rmt_port = match->rmt_port;
1032   rmp->action_index = clib_host_to_net_u32 (rule->action_index);
1033   rmp->scope =
1034     is_local ? SESSION_RULE_SCOPE_LOCAL : SESSION_RULE_SCOPE_GLOBAL;
1035   rmp->transport_proto = transport_proto;
1036   rmp->appns_index = clib_host_to_net_u32 (appns_index);
1037   if (tag)
1038     {
1039       clib_memcpy (rmp->tag, tag, vec_len (tag));
1040       rmp->tag[vec_len (tag)] = 0;
1041     }
1042
1043   vl_api_send_msg (reg, (u8 *) rmp);
1044 }
1045
1046 static void
1047 send_session_rules_table_details (session_rules_table_t * srt, u8 fib_proto,
1048                                   u8 tp, u8 is_local, u32 appns_index,
1049                                   vl_api_registration_t * reg, u32 context)
1050 {
1051   mma_rule_16_t *rule16;
1052   mma_rule_40_t *rule40;
1053   mma_rules_table_16_t *srt16;
1054   mma_rules_table_40_t *srt40;
1055   u32 ri;
1056
1057   if (is_local || fib_proto == FIB_PROTOCOL_IP4)
1058     {
1059       u8 *tag = 0;
1060       /* *INDENT-OFF* */
1061       srt16 = &srt->session_rules_tables_16;
1062       pool_foreach (rule16, srt16->rules, ({
1063         ri = mma_rules_table_rule_index_16 (srt16, rule16);
1064         tag = session_rules_table_rule_tag (srt, ri, 1);
1065         send_session_rule_details4 (rule16, is_local, tp, appns_index, tag,
1066                                     reg, context);
1067       }));
1068       /* *INDENT-ON* */
1069     }
1070   if (is_local || fib_proto == FIB_PROTOCOL_IP6)
1071     {
1072       u8 *tag = 0;
1073       /* *INDENT-OFF* */
1074       srt40 = &srt->session_rules_tables_40;
1075       pool_foreach (rule40, srt40->rules, ({
1076         ri = mma_rules_table_rule_index_40 (srt40, rule40);
1077         tag = session_rules_table_rule_tag (srt, ri, 1);
1078         send_session_rule_details6 (rule40, is_local, tp, appns_index, tag,
1079                                     reg, context);
1080       }));
1081       /* *INDENT-ON* */
1082     }
1083 }
1084
1085 static void
1086 vl_api_session_rules_dump_t_handler (vl_api_one_map_server_dump_t * mp)
1087 {
1088   vl_api_registration_t *reg;
1089   session_table_t *st;
1090   u8 tp;
1091
1092   reg = vl_api_client_index_to_registration (mp->client_index);
1093   if (!reg)
1094     return;
1095
1096   /* *INDENT-OFF* */
1097   session_table_foreach (st, ({
1098     for (tp = 0; tp < TRANSPORT_N_PROTO; tp++)
1099       {
1100         send_session_rules_table_details (&st->session_rules[tp],
1101                                           st->active_fib_proto, tp,
1102                                           st->is_local, st->appns_index, reg,
1103                                           mp->context);
1104       }
1105   }));
1106   /* *INDENT-ON* */
1107 }
1108
1109 static void
1110 vl_api_application_tls_cert_add_t_handler (vl_api_application_tls_cert_add_t *
1111                                            mp)
1112 {
1113   vl_api_app_namespace_add_del_reply_t *rmp;
1114   vnet_app_add_tls_cert_args_t _a, *a = &_a;
1115   clib_error_t *error;
1116   application_t *app;
1117   u32 cert_len;
1118   int rv = 0;
1119   if (!session_manager_is_enabled ())
1120     {
1121       rv = VNET_API_ERROR_FEATURE_DISABLED;
1122       goto done;
1123     }
1124   if (!(app = application_lookup (mp->client_index)))
1125     {
1126       rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1127       goto done;
1128     }
1129   memset (a, 0, sizeof (*a));
1130   a->app_index = app->index;
1131   cert_len = clib_net_to_host_u16 (mp->cert_len);
1132   if (cert_len > 10000)
1133     {
1134       rv = VNET_API_ERROR_INVALID_VALUE;
1135       goto done;
1136     }
1137   vec_validate (a->cert, cert_len);
1138   clib_memcpy (a->cert, mp->cert, cert_len);
1139   if ((error = vnet_app_add_tls_cert (a)))
1140     {
1141       rv = clib_error_get_code (error);
1142       clib_error_report (error);
1143     }
1144   vec_free (a->cert);
1145 done:
1146   REPLY_MACRO (VL_API_APPLICATION_TLS_CERT_ADD_REPLY);
1147 }
1148
1149 static void
1150 vl_api_application_tls_key_add_t_handler (vl_api_application_tls_key_add_t *
1151                                           mp)
1152 {
1153   vl_api_app_namespace_add_del_reply_t *rmp;
1154   vnet_app_add_tls_key_args_t _a, *a = &_a;
1155   clib_error_t *error;
1156   application_t *app;
1157   u32 key_len;
1158   int rv = 0;
1159   if (!session_manager_is_enabled ())
1160     {
1161       rv = VNET_API_ERROR_FEATURE_DISABLED;
1162       goto done;
1163     }
1164   if (!(app = application_lookup (mp->client_index)))
1165     {
1166       rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1167       goto done;
1168     }
1169   memset (a, 0, sizeof (*a));
1170   a->app_index = app->index;
1171   key_len = clib_net_to_host_u16 (mp->key_len);
1172   if (key_len > 10000)
1173     {
1174       rv = VNET_API_ERROR_INVALID_VALUE;
1175       goto done;
1176     }
1177   vec_validate (a->key, key_len);
1178   clib_memcpy (a->key, mp->key, key_len);
1179   if ((error = vnet_app_add_tls_key (a)))
1180     {
1181       rv = clib_error_get_code (error);
1182       clib_error_report (error);
1183     }
1184   vec_free (a->key);
1185 done:
1186   REPLY_MACRO (VL_API_APPLICATION_TLS_KEY_ADD_REPLY);
1187 }
1188
1189 static clib_error_t *
1190 application_reaper_cb (u32 client_index)
1191 {
1192   application_t *app = application_lookup (client_index);
1193   vnet_app_detach_args_t _a, *a = &_a;
1194   if (app)
1195     {
1196       a->app_index = app->index;
1197       vnet_application_detach (a);
1198     }
1199   return 0;
1200 }
1201
1202 VL_MSG_API_REAPER_FUNCTION (application_reaper_cb);
1203
1204 #define vl_msg_name_crc_list
1205 #include <vnet/vnet_all_api_h.h>
1206 #undef vl_msg_name_crc_list
1207
1208 static void
1209 setup_message_id_table (api_main_t * am)
1210 {
1211 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1212   foreach_vl_msg_name_crc_session;
1213 #undef _
1214 }
1215
1216 /*
1217  * session_api_hookup
1218  * Add uri's API message handlers to the table.
1219  * vlib has alread mapped shared memory and
1220  * added the client registration handlers.
1221  * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
1222  */
1223 static clib_error_t *
1224 session_api_hookup (vlib_main_t * vm)
1225 {
1226   api_main_t *am = &api_main;
1227
1228 #define _(N,n)                                                  \
1229     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
1230                            vl_api_##n##_t_handler,              \
1231                            vl_noop_handler,                     \
1232                            vl_api_##n##_t_endian,               \
1233                            vl_api_##n##_t_print,                \
1234                            sizeof(vl_api_##n##_t), 1);
1235   foreach_session_api_msg;
1236 #undef _
1237
1238   /*
1239    * Messages which bounce off the data-plane to
1240    * an API client. Simply tells the message handling infra not
1241    * to free the message.
1242    *
1243    * Bounced message handlers MUST NOT block the data plane
1244    */
1245   am->message_bounce[VL_API_CONNECT_URI] = 1;
1246   am->message_bounce[VL_API_CONNECT_SOCK] = 1;
1247
1248   /*
1249    * Set up the (msg_name, crc, message-id) table
1250    */
1251   setup_message_id_table (am);
1252
1253   return 0;
1254 }
1255
1256 VLIB_API_INIT_FUNCTION (session_api_hookup);
1257
1258 /*
1259  * fd.io coding-style-patch-verification: ON
1260  *
1261  * Local Variables:
1262  * eval: (c-set-style "gnu")
1263  * End:
1264  */