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