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