vcl/session: use mq for bind replies
[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 _(APP_WORKER_ADD_DEL, app_worker_add_del)                               \
62
63 static int
64 session_send_fds (vl_api_registration_t * reg, int fds[], int n_fds)
65 {
66   clib_error_t *error;
67   if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
68     {
69       clib_warning ("can't send memfd fd");
70       return -1;
71     }
72   error = vl_api_send_fd_msg (reg, fds, n_fds);
73   if (error)
74     {
75       clib_error_report (error);
76       return -1;
77     }
78   return 0;
79 }
80
81 static int
82 send_add_segment_callback (u32 api_client_index, const ssvm_private_t * sp)
83 {
84   int fds[SESSION_N_FD_TYPE], n_fds = 0;
85   vl_api_map_another_segment_t *mp;
86   vl_api_registration_t *reg;
87   u8 fd_flags = 0;
88
89   reg = vl_mem_api_client_index_to_registration (api_client_index);
90   if (!reg)
91     {
92       clib_warning ("no registration: %u", api_client_index);
93       return -1;
94     }
95
96   if (ssvm_type (sp) == SSVM_SEGMENT_MEMFD)
97     {
98       if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
99         {
100           clib_warning ("can't send memfd fd");
101           return -1;
102         }
103
104       fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
105       fds[n_fds] = sp->fd;
106       n_fds += 1;
107     }
108
109   mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
110   memset (mp, 0, sizeof (*mp));
111   mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_MAP_ANOTHER_SEGMENT);
112   mp->segment_size = sp->ssvm_size;
113   mp->fd_flags = fd_flags;
114   strncpy ((char *) mp->segment_name, (char *) sp->name,
115            sizeof (mp->segment_name) - 1);
116
117   vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
118
119   if (n_fds)
120     return session_send_fds (reg, fds, n_fds);
121
122   return 0;
123 }
124
125 static int
126 send_del_segment_callback (u32 api_client_index, const ssvm_private_t * fs)
127 {
128   vl_api_unmap_segment_t *mp;
129   vl_api_registration_t *reg;
130
131   reg = vl_mem_api_client_index_to_registration (api_client_index);
132   if (!reg)
133     {
134       clib_warning ("no registration: %u", api_client_index);
135       return -1;
136     }
137
138   mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
139   memset (mp, 0, sizeof (*mp));
140   mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_UNMAP_SEGMENT);
141   strncpy ((char *) mp->segment_name, (char *) fs->name,
142            sizeof (mp->segment_name) - 1);
143
144   vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
145
146   return 0;
147 }
148
149 static int
150 send_app_cut_through_registration_add (u32 api_client_index, u64 mq_addr,
151                                        u64 peer_mq_addr)
152 {
153   vl_api_app_cut_through_registration_add_t *mp;
154   vl_api_registration_t *reg;
155   svm_msg_q_t *mq, *peer_mq;
156   int fds[2];
157
158   reg = vl_mem_api_client_index_to_registration (api_client_index);
159   if (!reg)
160     {
161       clib_warning ("no registration: %u", 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   mp->_vl_msg_id =
168     clib_host_to_net_u16 (VL_API_APP_CUT_THROUGH_REGISTRATION_ADD);
169
170   mp->evt_q_address = mq_addr;
171   mp->peer_evt_q_address = peer_mq_addr;
172
173   mq = uword_to_pointer (mq_addr, svm_msg_q_t *);
174   peer_mq = uword_to_pointer (peer_mq_addr, svm_msg_q_t *);
175
176   if (svm_msg_q_get_producer_eventfd (mq) != -1)
177     {
178       mp->fd_flags |= SESSION_FD_F_MQ_EVENTFD;
179       mp->n_fds = 2;
180       /* app will overwrite exactly the fds we pass here. So
181        * when we swap mq with peer_mq (accept vs connect) the
182        * fds will still be valid */
183       fds[0] = svm_msg_q_get_consumer_eventfd (mq);
184       fds[1] = svm_msg_q_get_producer_eventfd (peer_mq);
185     }
186
187   vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
188
189   if (mp->n_fds != 0)
190     session_send_fds (reg, fds, mp->n_fds);
191
192   return 0;
193 }
194
195 static int
196 send_session_accept_callback (stream_session_t * s)
197 {
198   app_worker_t *server_wrk = app_worker_get (s->app_wrk_index);
199   transport_proto_vft_t *tp_vft;
200   vl_api_accept_session_t *mp;
201   vl_api_registration_t *reg;
202   transport_connection_t *tc;
203   stream_session_t *listener;
204   svm_msg_q_t *vpp_queue;
205   application_t *server;
206
207   server = application_get (server_wrk->app_index);
208   reg = vl_mem_api_client_index_to_registration (server->api_client_index);
209   if (!reg)
210     {
211       clib_warning ("no registration: %u", server->api_client_index);
212       return -1;
213     }
214
215   mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
216   memset (mp, 0, sizeof (*mp));
217
218   mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_ACCEPT_SESSION);
219   mp->context = server_wrk->wrk_index;
220   mp->server_rx_fifo = pointer_to_uword (s->server_rx_fifo);
221   mp->server_tx_fifo = pointer_to_uword (s->server_tx_fifo);
222
223   if (session_has_transport (s))
224     {
225       listener = listen_session_get (s->listener_index);
226       mp->listener_handle = listen_session_get_handle (listener);
227       if (application_is_proxy (server))
228         {
229           listener =
230             app_worker_first_listener (server_wrk, session_get_fib_proto (s),
231                                        session_get_transport_proto (s));
232           if (listener)
233             mp->listener_handle = listen_session_get_handle (listener);
234         }
235       vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
236       mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
237       mp->handle = session_handle (s);
238       tp_vft = transport_protocol_get_vft (session_get_transport_proto (s));
239       tc = tp_vft->get_connection (s->connection_index, s->thread_index);
240       mp->port = tc->rmt_port;
241       mp->is_ip4 = tc->is_ip4;
242       clib_memcpy (&mp->ip, &tc->rmt_ip, sizeof (tc->rmt_ip));
243     }
244   else
245     {
246       local_session_t *ls = (local_session_t *) s;
247       local_session_t *ll;
248       if (application_local_session_listener_has_transport (ls))
249         {
250           listener = listen_session_get (ls->listener_index);
251           mp->listener_handle = listen_session_get_handle (listener);
252           mp->is_ip4 = session_type_is_ip4 (listener->session_type);
253         }
254       else
255         {
256           ll = application_get_local_listen_session (server_wrk,
257                                                      ls->listener_index);
258           if (ll->transport_listener_index != ~0)
259             {
260               listener = listen_session_get (ll->transport_listener_index);
261               mp->listener_handle = listen_session_get_handle (listener);
262             }
263           else
264             {
265               mp->listener_handle = application_local_session_handle (ll);
266             }
267           mp->is_ip4 = session_type_is_ip4 (ll->listener_session_type);
268         }
269       mp->handle = application_local_session_handle (ls);
270       mp->port = ls->port;
271       mp->vpp_event_queue_address = ls->client_evt_q;
272       mp->server_event_queue_address = ls->server_evt_q;
273     }
274   vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
275
276   return 0;
277 }
278
279 void
280 mq_send_local_session_disconnected_cb (u32 app_wrk_index,
281                                        local_session_t * ls)
282 {
283   app_worker_t *app_wrk = app_worker_get (app_wrk_index);
284   svm_msg_q_msg_t _msg, *msg = &_msg;
285   session_disconnected_msg_t *mp;
286   svm_msg_q_t *app_mq;
287   session_event_t *evt;
288   application_t *app;
289
290   app = application_get (app_wrk->app_index);
291   app_mq = app_wrk->event_queue;
292   svm_msg_q_lock_and_alloc_msg_w_ring (app_mq, SESSION_MQ_CTRL_EVT_RING,
293                                        SVM_Q_WAIT, msg);
294   svm_msg_q_unlock (app_mq);
295   evt = svm_msg_q_msg_data (app_mq, msg);
296   memset (evt, 0, sizeof (*evt));
297   evt->event_type = SESSION_CTRL_EVT_DISCONNECTED;
298   mp = (session_disconnected_msg_t *) evt->data;
299   mp->handle = application_local_session_handle (ls);
300   mp->context = app->api_client_index;
301   svm_msg_q_add (app_mq, msg, SVM_Q_WAIT);
302 }
303
304 static void
305 send_session_disconnect_callback (stream_session_t * s)
306 {
307   app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
308   vl_api_disconnect_session_t *mp;
309   vl_api_registration_t *reg;
310   application_t *app;
311
312   app = application_get (app_wrk->app_index);
313   reg = vl_mem_api_client_index_to_registration (app->api_client_index);
314   if (!reg)
315     {
316       clib_warning ("no registration: %u", app->api_client_index);
317       return;
318     }
319
320   mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
321   memset (mp, 0, sizeof (*mp));
322   mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_DISCONNECT_SESSION);
323   mp->handle = session_handle (s);
324   mp->context = app->api_client_index;
325   vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
326 }
327
328 static void
329 send_session_reset_callback (stream_session_t * s)
330 {
331   app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
332   vl_api_registration_t *reg;
333   vl_api_reset_session_t *mp;
334   application_t *app;
335
336   app = application_get (app_wrk->app_index);
337   reg = vl_mem_api_client_index_to_registration (app->api_client_index);
338   if (!reg)
339     {
340       clib_warning ("no registration: %u", app->api_client_index);
341       return;
342     }
343
344   mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
345   memset (mp, 0, sizeof (*mp));
346   mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_RESET_SESSION);
347   mp->handle = session_handle (s);
348   vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
349 }
350
351 int
352 send_session_connected_callback (u32 app_wrk_index, u32 api_context,
353                                  stream_session_t * s, u8 is_fail)
354 {
355   vl_api_connect_session_reply_t *mp;
356   transport_connection_t *tc;
357   vl_api_registration_t *reg;
358   svm_msg_q_t *vpp_queue;
359   app_worker_t *app_wrk;
360   application_t *app;
361
362   app_wrk = app_worker_get (app_wrk_index);
363   app = application_get (app_wrk->app_index);
364   reg = vl_mem_api_client_index_to_registration (app->api_client_index);
365   if (!reg)
366     {
367       clib_warning ("no registration: %u", app->api_client_index);
368       return -1;
369     }
370
371   mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
372   mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_CONNECT_SESSION_REPLY);
373   mp->context = api_context;
374
375   if (is_fail)
376     goto done;
377
378   if (session_has_transport (s))
379     {
380       tc = session_get_transport (s);
381       if (!tc)
382         {
383           is_fail = 1;
384           goto done;
385         }
386
387       vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
388       mp->handle = session_handle (s);
389       mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
390       clib_memcpy (mp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
391       mp->is_ip4 = tc->is_ip4;
392       mp->lcl_port = tc->lcl_port;
393       mp->server_rx_fifo = pointer_to_uword (s->server_rx_fifo);
394       mp->server_tx_fifo = pointer_to_uword (s->server_tx_fifo);
395     }
396   else
397     {
398       local_session_t *ls = (local_session_t *) s;
399       mp->handle = application_local_session_handle (ls);
400       mp->lcl_port = ls->port;
401       mp->vpp_event_queue_address = ls->server_evt_q;
402       mp->client_event_queue_address = ls->client_evt_q;
403       mp->server_rx_fifo = pointer_to_uword (s->server_tx_fifo);
404       mp->server_tx_fifo = pointer_to_uword (s->server_rx_fifo);
405     }
406
407 done:
408   mp->retval = is_fail ?
409     clib_host_to_net_u32 (VNET_API_ERROR_SESSION_CONNECT) : 0;
410   vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
411   return 0;
412 }
413
414 static session_cb_vft_t session_cb_vft = {
415   .session_accept_callback = send_session_accept_callback,
416   .session_disconnect_callback = send_session_disconnect_callback,
417   .session_connected_callback = send_session_connected_callback,
418   .session_reset_callback = send_session_reset_callback,
419   .add_segment_callback = send_add_segment_callback,
420   .del_segment_callback = send_del_segment_callback,
421 };
422
423 static int
424 mq_send_session_accepted_cb (stream_session_t * s)
425 {
426   app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
427   svm_msg_q_msg_t _msg, *msg = &_msg;
428   svm_msg_q_t *vpp_queue, *app_mq;
429   transport_proto_vft_t *tp_vft;
430   transport_connection_t *tc;
431   stream_session_t *listener;
432   session_accepted_msg_t *mp;
433   session_event_t *evt;
434   application_t *app;
435
436   app = application_get (app_wrk->app_index);
437   app_mq = app_wrk->event_queue;
438   svm_msg_q_lock_and_alloc_msg_w_ring (app_mq, SESSION_MQ_CTRL_EVT_RING,
439                                        SVM_Q_WAIT, msg);
440   svm_msg_q_unlock (app_mq);
441
442   evt = svm_msg_q_msg_data (app_mq, msg);
443   memset (evt, 0, sizeof (*evt));
444   evt->event_type = SESSION_CTRL_EVT_ACCEPTED;
445   mp = (session_accepted_msg_t *) evt->data;
446   mp->context = app_wrk->wrk_index;
447   mp->server_rx_fifo = pointer_to_uword (s->server_rx_fifo);
448   mp->server_tx_fifo = pointer_to_uword (s->server_tx_fifo);
449
450   if (session_has_transport (s))
451     {
452       listener = listen_session_get (s->listener_index);
453       mp->listener_handle = listen_session_get_handle (listener);
454       if (application_is_proxy (app))
455         {
456           listener =
457             app_worker_first_listener (app_wrk, session_get_fib_proto (s),
458                                        session_get_transport_proto (s));
459           if (listener)
460             mp->listener_handle = listen_session_get_handle (listener);
461         }
462       vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
463       mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
464       mp->handle = session_handle (s);
465       tp_vft = transport_protocol_get_vft (session_get_transport_proto (s));
466       tc = tp_vft->get_connection (s->connection_index, s->thread_index);
467       mp->port = tc->rmt_port;
468       mp->is_ip4 = tc->is_ip4;
469       clib_memcpy (&mp->ip, &tc->rmt_ip, sizeof (tc->rmt_ip));
470     }
471   else
472     {
473       local_session_t *ls = (local_session_t *) s;
474       local_session_t *ll;
475       u8 main_thread = vlib_num_workers ()? 1 : 0;
476
477       send_app_cut_through_registration_add (app->api_client_index,
478                                              ls->server_evt_q,
479                                              ls->client_evt_q);
480
481       if (application_local_session_listener_has_transport (ls))
482         {
483           listener = listen_session_get (ls->listener_index);
484           mp->listener_handle = listen_session_get_handle (listener);
485           mp->is_ip4 = session_type_is_ip4 (listener->session_type);
486         }
487       else
488         {
489           ll =
490             application_get_local_listen_session (app_wrk,
491                                                   ls->listener_index);
492           if (ll->transport_listener_index != ~0)
493             {
494               listener = listen_session_get (ll->transport_listener_index);
495               mp->listener_handle = listen_session_get_handle (listener);
496             }
497           else
498             {
499               mp->listener_handle = application_local_session_handle (ll);
500             }
501           mp->is_ip4 = session_type_is_ip4 (ll->listener_session_type);
502         }
503       mp->handle = application_local_session_handle (ls);
504       mp->port = ls->port;
505       vpp_queue = session_manager_get_vpp_event_queue (main_thread);
506       mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
507       mp->client_event_queue_address = ls->client_evt_q;
508       mp->server_event_queue_address = ls->server_evt_q;
509     }
510   svm_msg_q_add (app_mq, msg, SVM_Q_WAIT);
511
512   return 0;
513 }
514
515 static void
516 mq_send_session_disconnected_cb (stream_session_t * s)
517 {
518   app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
519   svm_msg_q_msg_t _msg, *msg = &_msg;
520   session_disconnected_msg_t *mp;
521   svm_msg_q_t *app_mq;
522   session_event_t *evt;
523   application_t *app;
524
525   app = application_get (app_wrk->app_index);
526   app_mq = app_wrk->event_queue;
527   svm_msg_q_lock_and_alloc_msg_w_ring (app_mq, SESSION_MQ_CTRL_EVT_RING,
528                                        SVM_Q_WAIT, msg);
529   svm_msg_q_unlock (app_mq);
530   evt = svm_msg_q_msg_data (app_mq, msg);
531   memset (evt, 0, sizeof (*evt));
532   evt->event_type = SESSION_CTRL_EVT_DISCONNECTED;
533   mp = (session_disconnected_msg_t *) evt->data;
534   mp->handle = session_handle (s);
535   mp->context = app->api_client_index;
536   svm_msg_q_add (app_mq, msg, SVM_Q_WAIT);
537 }
538
539 static void
540 mq_send_session_reset_cb (stream_session_t * s)
541 {
542   app_worker_t *app = app_worker_get (s->app_wrk_index);
543   svm_msg_q_msg_t _msg, *msg = &_msg;
544   session_reset_msg_t *mp;
545   svm_msg_q_t *app_mq;
546   session_event_t *evt;
547
548   app_mq = app->event_queue;
549   svm_msg_q_lock_and_alloc_msg_w_ring (app_mq, SESSION_MQ_CTRL_EVT_RING,
550                                        SVM_Q_WAIT, msg);
551   svm_msg_q_unlock (app_mq);
552   evt = svm_msg_q_msg_data (app_mq, msg);
553   memset (evt, 0, sizeof (*evt));
554   evt->event_type = SESSION_CTRL_EVT_RESET;
555   mp = (session_reset_msg_t *) evt->data;
556   mp->handle = session_handle (s);
557   svm_msg_q_add (app_mq, msg, SVM_Q_WAIT);
558 }
559
560 static int
561 mq_send_session_connected_cb (u32 app_wrk_index, u32 api_context,
562                               stream_session_t * s, u8 is_fail)
563 {
564   svm_msg_q_msg_t _msg, *msg = &_msg;
565   session_connected_msg_t *mp;
566   svm_msg_q_t *vpp_mq, *app_mq;
567   transport_connection_t *tc;
568   app_worker_t *app_wrk;
569   session_event_t *evt;
570   application_t *app;
571
572   app_wrk = app_worker_get (app_wrk_index);
573   app = application_get (app_wrk->app_index);
574   app_mq = app_wrk->event_queue;
575   if (!app_mq)
576     {
577       clib_warning ("app %u with api index: %u not attached", app->app_index,
578                     app->api_client_index);
579       return -1;
580     }
581
582   svm_msg_q_lock_and_alloc_msg_w_ring (app_mq, SESSION_MQ_CTRL_EVT_RING,
583                                        SVM_Q_WAIT, msg);
584   svm_msg_q_unlock (app_mq);
585   evt = svm_msg_q_msg_data (app_mq, msg);
586   memset (evt, 0, sizeof (*evt));
587   evt->event_type = SESSION_CTRL_EVT_CONNECTED;
588   mp = (session_connected_msg_t *) evt->data;
589   mp->context = api_context;
590
591   if (is_fail)
592     goto done;
593
594   if (session_has_transport (s))
595     {
596       tc = session_get_transport (s);
597       if (!tc)
598         {
599           is_fail = 1;
600           goto done;
601         }
602
603       vpp_mq = session_manager_get_vpp_event_queue (s->thread_index);
604       mp->handle = session_handle (s);
605       mp->vpp_event_queue_address = pointer_to_uword (vpp_mq);
606       clib_memcpy (mp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
607       mp->is_ip4 = tc->is_ip4;
608       mp->lcl_port = tc->lcl_port;
609       mp->server_rx_fifo = pointer_to_uword (s->server_rx_fifo);
610       mp->server_tx_fifo = pointer_to_uword (s->server_tx_fifo);
611     }
612   else
613     {
614       local_session_t *ls = (local_session_t *) s;
615       u8 main_thread = vlib_num_workers ()? 1 : 0;
616
617       send_app_cut_through_registration_add (app->api_client_index,
618                                              ls->client_evt_q,
619                                              ls->server_evt_q);
620
621       mp->handle = application_local_session_handle (ls);
622       mp->lcl_port = ls->port;
623       vpp_mq = session_manager_get_vpp_event_queue (main_thread);
624       mp->vpp_event_queue_address = pointer_to_uword (vpp_mq);
625       mp->client_event_queue_address = ls->client_evt_q;
626       mp->server_event_queue_address = ls->server_evt_q;
627       mp->server_rx_fifo = pointer_to_uword (s->server_tx_fifo);
628       mp->server_tx_fifo = pointer_to_uword (s->server_rx_fifo);
629     }
630
631 done:
632   mp->retval = is_fail ?
633     clib_host_to_net_u32 (VNET_API_ERROR_SESSION_CONNECT) : 0;
634
635   svm_msg_q_add (app_mq, msg, SVM_Q_WAIT);
636   return 0;
637 }
638
639 static int
640 mq_send_session_bound_cb (u32 app_wrk_index, u32 api_context,
641                           session_handle_t handle, int rv)
642 {
643   svm_msg_q_msg_t _msg, *msg = &_msg;
644   svm_msg_q_t *app_mq, *vpp_evt_q;
645   transport_connection_t *tc;
646   stream_session_t *ls = 0;
647   session_bound_msg_t *mp;
648   app_worker_t *app_wrk;
649   session_event_t *evt;
650   application_t *app;
651
652   app_wrk = app_worker_get (app_wrk_index);
653   app = application_get (app_wrk->app_index);
654   app_mq = app_wrk->event_queue;
655   if (!app_mq)
656     {
657       clib_warning ("app %u with api index: %u not attached", app->app_index,
658                     app->api_client_index);
659       return -1;
660     }
661
662   svm_msg_q_lock_and_alloc_msg_w_ring (app_mq, SESSION_MQ_CTRL_EVT_RING,
663                                        SVM_Q_WAIT, msg);
664   svm_msg_q_unlock (app_mq);
665   evt = svm_msg_q_msg_data (app_mq, msg);
666   memset (evt, 0, sizeof (*evt));
667   evt->event_type = SESSION_CTRL_EVT_BOUND;
668   mp = (session_bound_msg_t *) evt->data;
669   mp->context = api_context;
670
671   if (rv)
672     goto done;
673
674   mp->handle = handle;
675   if (application_has_global_scope (app))
676     {
677       ls = listen_session_get_from_handle (handle);
678       tc = listen_session_get_transport (ls);
679       mp->lcl_port = tc->lcl_port;
680       mp->lcl_is_ip4 = tc->is_ip4;
681       clib_memcpy (mp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
682     }
683   else
684     {
685       local_session_t *local;
686       local = application_get_local_listen_session_from_handle (handle);
687       mp->lcl_port = local->port;
688       mp->lcl_is_ip4 = session_type_is_ip4 (local->session_type);
689     }
690
691   if (ls && session_transport_service_type (ls) == TRANSPORT_SERVICE_CL)
692     {
693       mp->rx_fifo = pointer_to_uword (ls->server_rx_fifo);
694       mp->tx_fifo = pointer_to_uword (ls->server_tx_fifo);
695       vpp_evt_q = session_manager_get_vpp_event_queue (0);
696       mp->vpp_evt_q = pointer_to_uword (vpp_evt_q);
697     }
698
699 done:
700   mp->retval = rv;
701   svm_msg_q_add (app_mq, msg, SVM_Q_WAIT);
702   return 0;
703 }
704
705 static session_cb_vft_t session_mq_cb_vft = {
706   .session_accept_callback = mq_send_session_accepted_cb,
707   .session_disconnect_callback = mq_send_session_disconnected_cb,
708   .session_connected_callback = mq_send_session_connected_cb,
709   .session_reset_callback = mq_send_session_reset_cb,
710   .add_segment_callback = send_add_segment_callback,
711   .del_segment_callback = send_del_segment_callback,
712 };
713
714 static void
715 vl_api_session_enable_disable_t_handler (vl_api_session_enable_disable_t * mp)
716 {
717   vl_api_session_enable_disable_reply_t *rmp;
718   vlib_main_t *vm = vlib_get_main ();
719   int rv = 0;
720
721   vnet_session_enable_disable (vm, mp->is_enable);
722   REPLY_MACRO (VL_API_SESSION_ENABLE_DISABLE_REPLY);
723 }
724
725 static void
726 vl_api_application_attach_t_handler (vl_api_application_attach_t * mp)
727 {
728   int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0;
729   vl_api_application_attach_reply_t *rmp;
730   ssvm_private_t *segp, *evt_q_segment;
731   vnet_app_attach_args_t _a, *a = &_a;
732   vl_api_registration_t *reg;
733   clib_error_t *error = 0;
734   u8 fd_flags = 0;
735
736   reg = vl_api_client_index_to_registration (mp->client_index);
737   if (!reg)
738     return;
739
740   if (session_manager_is_enabled () == 0)
741     {
742       rv = VNET_API_ERROR_FEATURE_DISABLED;
743       goto done;
744     }
745
746   STATIC_ASSERT (sizeof (u64) * APP_OPTIONS_N_OPTIONS <=
747                  sizeof (mp->options),
748                  "Out of options, fix api message definition");
749
750   memset (a, 0, sizeof (*a));
751   a->api_client_index = mp->client_index;
752   a->options = mp->options;
753
754   if (a->options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_USE_MQ_FOR_CTRL_MSGS)
755     a->session_cb_vft = &session_mq_cb_vft;
756   else
757     a->session_cb_vft = &session_cb_vft;
758
759   if (mp->namespace_id_len > 64)
760     {
761       rv = VNET_API_ERROR_INVALID_VALUE;
762       goto done;
763     }
764
765   if (mp->namespace_id_len)
766     {
767       vec_validate (a->namespace_id, mp->namespace_id_len - 1);
768       clib_memcpy (a->namespace_id, mp->namespace_id, mp->namespace_id_len);
769     }
770
771   if ((error = vnet_application_attach (a)))
772     {
773       rv = clib_error_get_code (error);
774       clib_error_report (error);
775       vec_free (a->namespace_id);
776       goto done;
777     }
778   vec_free (a->namespace_id);
779
780   /* Send event queues segment */
781   if ((evt_q_segment = session_manager_get_evt_q_segment ()))
782     {
783       fd_flags |= SESSION_FD_F_VPP_MQ_SEGMENT;
784       fds[n_fds] = evt_q_segment->fd;
785       n_fds += 1;
786     }
787   /* Send fifo segment fd if needed */
788   if (ssvm_type (a->segment) == SSVM_SEGMENT_MEMFD)
789     {
790       fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
791       fds[n_fds] = a->segment->fd;
792       n_fds += 1;
793     }
794   if (a->options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD)
795     {
796       fd_flags |= SESSION_FD_F_MQ_EVENTFD;
797       fds[n_fds] = svm_msg_q_get_producer_eventfd (a->app_evt_q);
798       n_fds += 1;
799     }
800
801 done:
802
803   /* *INDENT-OFF* */
804   REPLY_MACRO2 (VL_API_APPLICATION_ATTACH_REPLY, ({
805     if (!rv)
806       {
807         segp = a->segment;
808         rmp->segment_name_length = 0;
809         rmp->segment_size = segp->ssvm_size;
810         if (vec_len (segp->name))
811           {
812             memcpy (rmp->segment_name, segp->name, vec_len (segp->name));
813             rmp->segment_name_length = vec_len (segp->name);
814           }
815         rmp->app_event_queue_address = pointer_to_uword (a->app_evt_q);
816         rmp->n_fds = n_fds;
817         rmp->fd_flags = fd_flags;
818       }
819   }));
820   /* *INDENT-ON* */
821
822   if (n_fds)
823     session_send_fds (reg, fds, n_fds);
824 }
825
826 static void
827 vl_api_application_detach_t_handler (vl_api_application_detach_t * mp)
828 {
829   vl_api_application_detach_reply_t *rmp;
830   int rv = VNET_API_ERROR_INVALID_VALUE_2;
831   vnet_app_detach_args_t _a, *a = &_a;
832   application_t *app;
833
834   if (session_manager_is_enabled () == 0)
835     {
836       rv = VNET_API_ERROR_FEATURE_DISABLED;
837       goto done;
838     }
839
840   app = application_lookup (mp->client_index);
841   if (app)
842     {
843       a->app_index = app->app_index;
844       rv = vnet_application_detach (a);
845     }
846
847 done:
848   REPLY_MACRO (VL_API_APPLICATION_DETACH_REPLY);
849 }
850
851 static void
852 vl_api_bind_uri_t_handler (vl_api_bind_uri_t * mp)
853 {
854   transport_connection_t *tc = 0;
855   vnet_bind_args_t _a, *a = &_a;
856   vl_api_bind_uri_reply_t *rmp;
857   stream_session_t *s;
858   application_t *app = 0;
859   svm_msg_q_t *vpp_evt_q;
860   app_worker_t *app_wrk;
861   int rv;
862
863   if (session_manager_is_enabled () == 0)
864     {
865       rv = VNET_API_ERROR_FEATURE_DISABLED;
866       goto done;
867     }
868
869   app = application_lookup (mp->client_index);
870   if (app)
871     {
872       memset (a, 0, sizeof (*a));
873       a->uri = (char *) mp->uri;
874       a->app_index = app->app_index;
875       rv = vnet_bind_uri (a);
876     }
877   else
878     {
879       rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
880     }
881
882 done:
883
884   /* *INDENT-OFF* */
885   REPLY_MACRO2 (VL_API_BIND_URI_REPLY, ({
886     if (!rv)
887       {
888         rmp->handle = a->handle;
889         if (app && application_has_global_scope (app))
890             {
891               s = listen_session_get_from_handle (a->handle);
892               tc = listen_session_get_transport (s);
893               rmp->lcl_is_ip4 = tc->is_ip4;
894               rmp->lcl_port = tc->lcl_port;
895               clib_memcpy (rmp->lcl_ip, &tc->lcl_ip, sizeof(tc->lcl_ip));
896               if (session_transport_service_type (s) == TRANSPORT_SERVICE_CL)
897                 {
898                   rmp->rx_fifo = pointer_to_uword (s->server_rx_fifo);
899                   rmp->tx_fifo = pointer_to_uword (s->server_tx_fifo);
900                   vpp_evt_q = session_manager_get_vpp_event_queue (0);
901                   rmp->vpp_evt_q = pointer_to_uword (vpp_evt_q);
902                 }
903             }
904       }
905   }));
906   /* *INDENT-ON* */
907
908   /* If app uses mq for control messages, send an mq message as well */
909   if (app && application_use_mq_for_ctrl (app))
910     {
911       app_wrk = application_get_worker (app, 0);
912       mq_send_session_bound_cb (app_wrk->wrk_index, mp->context, a->handle,
913                                 rv);
914     }
915 }
916
917 static void
918 vl_api_unbind_uri_t_handler (vl_api_unbind_uri_t * mp)
919 {
920   vl_api_unbind_uri_reply_t *rmp;
921   application_t *app;
922   vnet_unbind_args_t _a, *a = &_a;
923   int rv;
924
925   if (session_manager_is_enabled () == 0)
926     {
927       rv = VNET_API_ERROR_FEATURE_DISABLED;
928       goto done;
929     }
930
931   app = application_lookup (mp->client_index);
932   if (app)
933     {
934       a->uri = (char *) mp->uri;
935       a->app_index = app->app_index;
936       rv = vnet_unbind_uri (a);
937     }
938   else
939     {
940       rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
941     }
942
943 done:
944   REPLY_MACRO (VL_API_UNBIND_URI_REPLY);
945 }
946
947 static void
948 vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp)
949 {
950   vl_api_connect_session_reply_t *rmp;
951   vnet_connect_args_t _a, *a = &_a;
952   application_t *app;
953   clib_error_t *error = 0;
954   int rv = 0;
955
956   if (session_manager_is_enabled () == 0)
957     {
958       rv = VNET_API_ERROR_FEATURE_DISABLED;
959       goto done;
960     }
961
962   app = application_lookup (mp->client_index);
963   if (app)
964     {
965       a->uri = (char *) mp->uri;
966       a->api_context = mp->context;
967       a->app_index = app->app_index;
968       if ((error = vnet_connect_uri (a)))
969         {
970           rv = clib_error_get_code (error);
971           clib_error_report (error);
972         }
973     }
974   else
975     {
976       rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
977     }
978
979   /*
980    * Don't reply to stream (tcp) connects. The reply will come once
981    * the connection is established. In case of the redirects, the reply
982    * will come from the server app.
983    */
984   if (rv == 0)
985     return;
986
987 done:
988   /* *INDENT-OFF* */
989   REPLY_MACRO (VL_API_CONNECT_SESSION_REPLY);
990   /* *INDENT-ON* */
991 }
992
993 static void
994 vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
995 {
996   vl_api_disconnect_session_reply_t *rmp;
997   vnet_disconnect_args_t _a, *a = &_a;
998   application_t *app;
999   int rv = 0;
1000
1001   if (session_manager_is_enabled () == 0)
1002     {
1003       rv = VNET_API_ERROR_FEATURE_DISABLED;
1004       goto done;
1005     }
1006
1007   app = application_lookup (mp->client_index);
1008   if (app)
1009     {
1010       a->handle = mp->handle;
1011       a->app_index = app->app_index;
1012       rv = vnet_disconnect_session (a);
1013     }
1014   else
1015     {
1016       rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1017     }
1018
1019 done:
1020   REPLY_MACRO2 (VL_API_DISCONNECT_SESSION_REPLY, rmp->handle = mp->handle);
1021 }
1022
1023 static void
1024 vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
1025                                            mp)
1026 {
1027   vnet_disconnect_args_t _a, *a = &_a;
1028   application_t *app;
1029
1030   /* Client objected to disconnecting the session, log and continue */
1031   if (mp->retval)
1032     {
1033       clib_warning ("client retval %d", mp->retval);
1034       return;
1035     }
1036
1037   /* Disconnect has been confirmed. Confirm close to transport */
1038   app = application_lookup (mp->context);
1039   if (app)
1040     {
1041       a->handle = mp->handle;
1042       a->app_index = app->app_index;
1043       vnet_disconnect_session (a);
1044     }
1045 }
1046
1047 static void
1048 vl_api_reset_session_reply_t_handler (vl_api_reset_session_reply_t * mp)
1049 {
1050   app_worker_t *app_wrk;
1051   application_t *app;
1052   stream_session_t *s;
1053   u32 index, thread_index;
1054
1055   app = application_lookup (mp->context);
1056   if (!app)
1057     return;
1058
1059   session_parse_handle (mp->handle, &index, &thread_index);
1060   s = session_get_if_valid (index, thread_index);
1061   if (!s)
1062     {
1063       clib_warning ("Invalid session!");
1064       return;
1065     }
1066
1067   app_wrk = app_worker_get (s->app_wrk_index);
1068   if (app_wrk->app_index != app->app_index)
1069     {
1070       clib_warning ("app %u does not own handle 0x%lx", app->app_index,
1071                     mp->handle);
1072       return;
1073     }
1074
1075   /* Client objected to resetting the session, log and continue */
1076   if (mp->retval)
1077     {
1078       clib_warning ("client retval %d", mp->retval);
1079       return;
1080     }
1081
1082   /* This comes as a response to a reset, transport only waiting for
1083    * confirmation to remove connection state, no need to disconnect */
1084   stream_session_cleanup (s);
1085 }
1086
1087 static void
1088 vl_api_accept_session_reply_t_handler (vl_api_accept_session_reply_t * mp)
1089 {
1090   vnet_disconnect_args_t _a = { 0 }, *a = &_a;
1091   local_session_t *ls;
1092   stream_session_t *s;
1093
1094   /* Server isn't interested, kill the session */
1095   if (mp->retval)
1096     {
1097       a->app_index = mp->context;
1098       a->handle = mp->handle;
1099       vnet_disconnect_session (a);
1100       return;
1101     }
1102
1103   if (session_handle_is_local (mp->handle))
1104     {
1105       ls = application_get_local_session_from_handle (mp->handle);
1106       if (!ls || ls->app_wrk_index != mp->context)
1107         {
1108           clib_warning ("server %u doesn't own local handle %llu",
1109                         mp->context, mp->handle);
1110           return;
1111         }
1112       if (application_local_session_connect_notify (ls))
1113         return;
1114       ls->session_state = SESSION_STATE_READY;
1115     }
1116   else
1117     {
1118       s = session_get_from_handle_if_valid (mp->handle);
1119       if (!s)
1120         {
1121           clib_warning ("session doesn't exist");
1122           return;
1123         }
1124       if (s->app_wrk_index != mp->context)
1125         {
1126           clib_warning ("app doesn't own session");
1127           return;
1128         }
1129       s->session_state = SESSION_STATE_READY;
1130     }
1131 }
1132
1133 static void
1134 vl_api_map_another_segment_reply_t_handler (vl_api_map_another_segment_reply_t
1135                                             * mp)
1136 {
1137   clib_warning ("not implemented");
1138 }
1139
1140 static void
1141 vl_api_bind_sock_t_handler (vl_api_bind_sock_t * mp)
1142 {
1143   vl_api_bind_sock_reply_t *rmp;
1144   vnet_bind_args_t _a, *a = &_a;
1145   int rv = 0;
1146   clib_error_t *error;
1147   application_t *app = 0;
1148   app_worker_t *app_wrk;
1149   stream_session_t *s;
1150   transport_connection_t *tc = 0;
1151   ip46_address_t *ip46;
1152   svm_msg_q_t *vpp_evt_q;
1153
1154   if (session_manager_is_enabled () == 0)
1155     {
1156       rv = VNET_API_ERROR_FEATURE_DISABLED;
1157       goto done;
1158     }
1159
1160   app = application_lookup (mp->client_index);
1161   if (!app)
1162     {
1163       rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1164       goto done;
1165     }
1166
1167   ip46 = (ip46_address_t *) mp->ip;
1168   memset (a, 0, sizeof (*a));
1169   a->sep.is_ip4 = mp->is_ip4;
1170   a->sep.ip = *ip46;
1171   a->sep.port = mp->port;
1172   a->sep.fib_index = mp->vrf;
1173   a->sep.sw_if_index = ENDPOINT_INVALID_INDEX;
1174   a->sep.transport_proto = mp->proto;
1175   a->app_index = app->app_index;
1176   a->wrk_map_index = mp->wrk_index;
1177
1178   if ((error = vnet_bind (a)))
1179     {
1180       rv = clib_error_get_code (error);
1181       clib_error_report (error);
1182     }
1183
1184 done:
1185   /* *INDENT-OFF* */
1186   REPLY_MACRO2 (VL_API_BIND_SOCK_REPLY,({
1187     if (!rv)
1188       {
1189         rmp->handle = a->handle;
1190         rmp->lcl_port = mp->port;
1191         rmp->lcl_is_ip4 = mp->is_ip4;
1192         if (app && application_has_global_scope (app))
1193           {
1194             s = listen_session_get_from_handle (a->handle);
1195             tc = listen_session_get_transport (s);
1196             clib_memcpy (rmp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
1197             if (session_transport_service_type (s) == TRANSPORT_SERVICE_CL)
1198               {
1199                 rmp->rx_fifo = pointer_to_uword (s->server_rx_fifo);
1200                 rmp->tx_fifo = pointer_to_uword (s->server_tx_fifo);
1201                 vpp_evt_q = session_manager_get_vpp_event_queue (0);
1202                 rmp->vpp_evt_q = pointer_to_uword (vpp_evt_q);
1203               }
1204           }
1205       }
1206   }));
1207   /* *INDENT-ON* */
1208
1209   /* If app uses mq for control messages, send an mq message as well */
1210   if (app && application_use_mq_for_ctrl (app))
1211     {
1212       app_wrk = application_get_worker (app, mp->wrk_index);
1213       mq_send_session_bound_cb (app_wrk->wrk_index, mp->context, a->handle,
1214                                 rv);
1215     }
1216 }
1217
1218 static void
1219 vl_api_unbind_sock_t_handler (vl_api_unbind_sock_t * mp)
1220 {
1221   vl_api_unbind_sock_reply_t *rmp;
1222   vnet_unbind_args_t _a, *a = &_a;
1223   application_t *app;
1224   clib_error_t *error;
1225   int rv = 0;
1226
1227   if (session_manager_is_enabled () == 0)
1228     {
1229       rv = VNET_API_ERROR_FEATURE_DISABLED;
1230       goto done;
1231     }
1232
1233   app = application_lookup (mp->client_index);
1234   if (app)
1235     {
1236       a->app_index = app->app_index;
1237       a->handle = mp->handle;
1238       if ((error = vnet_unbind (a)))
1239         {
1240           rv = clib_error_get_code (error);
1241           clib_error_report (error);
1242         }
1243     }
1244
1245 done:
1246   REPLY_MACRO (VL_API_UNBIND_SOCK_REPLY);
1247 }
1248
1249 static void
1250 vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
1251 {
1252   vl_api_connect_session_reply_t *rmp;
1253   vnet_connect_args_t _a, *a = &_a;
1254   application_t *app;
1255   clib_error_t *error = 0;
1256   int rv = 0;
1257
1258   if (session_manager_is_enabled () == 0)
1259     {
1260       rv = VNET_API_ERROR_FEATURE_DISABLED;
1261       goto done;
1262     }
1263
1264   app = application_lookup (mp->client_index);
1265   if (app)
1266     {
1267       svm_queue_t *client_q;
1268       ip46_address_t *ip46 = (ip46_address_t *) mp->ip;
1269
1270       memset (a, 0, sizeof (*a));
1271       client_q = vl_api_client_index_to_input_queue (mp->client_index);
1272       mp->client_queue_address = pointer_to_uword (client_q);
1273       a->sep.is_ip4 = mp->is_ip4;
1274       a->sep.ip = *ip46;
1275       a->sep.port = mp->port;
1276       a->sep.transport_proto = mp->proto;
1277       a->sep.fib_index = mp->vrf;
1278       a->sep.sw_if_index = ENDPOINT_INVALID_INDEX;
1279       if (mp->hostname_len)
1280         {
1281           vec_validate (a->sep_ext.hostname, mp->hostname_len - 1);
1282           clib_memcpy (a->sep_ext.hostname, mp->hostname, mp->hostname_len);
1283         }
1284       a->api_context = mp->context;
1285       a->app_index = app->app_index;
1286       a->wrk_map_index = mp->wrk_index;
1287       if ((error = vnet_connect (a)))
1288         {
1289           rv = clib_error_get_code (error);
1290           clib_error_report (error);
1291         }
1292       vec_free (a->sep_ext.hostname);
1293     }
1294   else
1295     {
1296       rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1297     }
1298
1299   if (rv == 0)
1300     return;
1301
1302   /* Got some error, relay it */
1303
1304 done:
1305   REPLY_MACRO (VL_API_CONNECT_SESSION_REPLY);
1306 }
1307
1308 static void
1309 vl_api_app_worker_add_del_t_handler (vl_api_app_worker_add_del_t * mp)
1310 {
1311   int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0;
1312   vl_api_app_worker_add_del_reply_t *rmp;
1313   vl_api_registration_t *reg;
1314   clib_error_t *error = 0;
1315   application_t *app;
1316   u8 fd_flags = 0;
1317
1318   if (!session_manager_is_enabled ())
1319     {
1320       rv = VNET_API_ERROR_FEATURE_DISABLED;
1321       goto done;
1322     }
1323
1324   reg = vl_api_client_index_to_registration (mp->client_index);
1325   if (!reg)
1326     return;
1327
1328   app = application_lookup (mp->app_api_index);
1329   if (!app)
1330     {
1331       rv = VNET_API_ERROR_INVALID_VALUE;
1332       goto done;
1333     }
1334
1335   vnet_app_worker_add_del_args_t args = {
1336     .app_index = app->app_index,
1337     .wrk_index = clib_net_to_host_u32 (mp->wrk_index),
1338     .is_add = mp->is_add
1339   };
1340   error = vnet_app_worker_add_del (&args);
1341   if (error)
1342     {
1343       rv = clib_error_get_code (error);
1344       clib_error_report (error);
1345       goto done;
1346     }
1347
1348   /* Make coverity happy */
1349   ASSERT (args.evt_q && args.segment);
1350
1351   /* Send fifo segment fd if needed */
1352   if (ssvm_type (args.segment) == SSVM_SEGMENT_MEMFD)
1353     {
1354       fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
1355       fds[n_fds] = args.segment->fd;
1356       n_fds += 1;
1357     }
1358   if (application_segment_manager_properties (app)->use_mq_eventfd)
1359     {
1360       fd_flags |= SESSION_FD_F_MQ_EVENTFD;
1361       fds[n_fds] = svm_msg_q_get_producer_eventfd (args.evt_q);
1362       n_fds += 1;
1363     }
1364
1365   /* *INDENT-OFF* */
1366 done:
1367   REPLY_MACRO2 (VL_API_APP_WORKER_ADD_DEL_REPLY, ({
1368     rmp->is_add = mp->is_add;
1369     if (!rv)
1370       {
1371         rmp->wrk_index = clib_host_to_net_u32 (args.wrk_index);
1372         if (vec_len (args.segment->name))
1373           {
1374             memcpy (rmp->segment_name, args.segment->name,
1375                     vec_len (args.segment->name));
1376             rmp->segment_name_length = vec_len (args.segment->name);
1377           }
1378         rmp->app_event_queue_address = pointer_to_uword (args.evt_q);
1379         rmp->n_fds = n_fds;
1380         rmp->fd_flags = fd_flags;
1381       }
1382   }));
1383   /* *INDENT-ON* */
1384
1385   if (n_fds)
1386     session_send_fds (reg, fds, n_fds);
1387 }
1388
1389 static void
1390 vl_api_app_namespace_add_del_t_handler (vl_api_app_namespace_add_del_t * mp)
1391 {
1392   vl_api_app_namespace_add_del_reply_t *rmp;
1393   clib_error_t *error = 0;
1394   u32 appns_index = 0;
1395   u8 *ns_id = 0;
1396   int rv = 0;
1397   if (!session_manager_is_enabled ())
1398     {
1399       rv = VNET_API_ERROR_FEATURE_DISABLED;
1400       goto done;
1401     }
1402
1403   if (mp->namespace_id_len > ARRAY_LEN (mp->namespace_id))
1404     {
1405       rv = VNET_API_ERROR_INVALID_VALUE;
1406       goto done;
1407     }
1408
1409   vec_validate (ns_id, mp->namespace_id_len - 1);
1410   clib_memcpy (ns_id, mp->namespace_id, mp->namespace_id_len);
1411   vnet_app_namespace_add_del_args_t args = {
1412     .ns_id = ns_id,
1413     .secret = clib_net_to_host_u64 (mp->secret),
1414     .sw_if_index = clib_net_to_host_u32 (mp->sw_if_index),
1415     .ip4_fib_id = clib_net_to_host_u32 (mp->ip4_fib_id),
1416     .ip6_fib_id = clib_net_to_host_u32 (mp->ip6_fib_id),
1417     .is_add = 1
1418   };
1419   error = vnet_app_namespace_add_del (&args);
1420   if (error)
1421     {
1422       rv = clib_error_get_code (error);
1423       clib_error_report (error);
1424     }
1425   else
1426     {
1427       appns_index = app_namespace_index_from_id (ns_id);
1428       if (appns_index == APP_NAMESPACE_INVALID_INDEX)
1429         {
1430           clib_warning ("app ns lookup failed");
1431           rv = VNET_API_ERROR_UNSPECIFIED;
1432         }
1433     }
1434   vec_free (ns_id);
1435
1436   /* *INDENT-OFF* */
1437 done:
1438   REPLY_MACRO2 (VL_API_APP_NAMESPACE_ADD_DEL_REPLY, ({
1439     if (!rv)
1440       rmp->appns_index = clib_host_to_net_u32 (appns_index);
1441   }));
1442   /* *INDENT-ON* */
1443 }
1444
1445 static void
1446 vl_api_session_rule_add_del_t_handler (vl_api_session_rule_add_del_t * mp)
1447 {
1448   vl_api_session_rule_add_del_reply_t *rmp;
1449   session_rule_add_del_args_t args;
1450   session_rule_table_add_del_args_t *table_args = &args.table_args;
1451   clib_error_t *error;
1452   u8 fib_proto;
1453   int rv = 0;
1454
1455   memset (&args, 0, sizeof (args));
1456   fib_proto = mp->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
1457
1458   table_args->lcl.fp_len = mp->lcl_plen;
1459   table_args->lcl.fp_proto = fib_proto;
1460   table_args->rmt.fp_len = mp->rmt_plen;
1461   table_args->rmt.fp_proto = fib_proto;
1462   table_args->lcl_port = mp->lcl_port;
1463   table_args->rmt_port = mp->rmt_port;
1464   table_args->action_index = clib_net_to_host_u32 (mp->action_index);
1465   table_args->is_add = mp->is_add;
1466   mp->tag[sizeof (mp->tag) - 1] = 0;
1467   table_args->tag = format (0, "%s", mp->tag);
1468   args.appns_index = clib_net_to_host_u32 (mp->appns_index);
1469   args.scope = mp->scope;
1470   args.transport_proto = mp->transport_proto;
1471
1472   memset (&table_args->lcl.fp_addr, 0, sizeof (table_args->lcl.fp_addr));
1473   memset (&table_args->rmt.fp_addr, 0, sizeof (table_args->rmt.fp_addr));
1474   ip_set (&table_args->lcl.fp_addr, mp->lcl_ip, mp->is_ip4);
1475   ip_set (&table_args->rmt.fp_addr, mp->rmt_ip, mp->is_ip4);
1476   error = vnet_session_rule_add_del (&args);
1477   if (error)
1478     {
1479       rv = clib_error_get_code (error);
1480       clib_error_report (error);
1481     }
1482   vec_free (table_args->tag);
1483   REPLY_MACRO (VL_API_SESSION_RULE_ADD_DEL_REPLY);
1484 }
1485
1486 static void
1487 send_session_rule_details4 (mma_rule_16_t * rule, u8 is_local,
1488                             u8 transport_proto, u32 appns_index, u8 * tag,
1489                             vl_api_registration_t * reg, u32 context)
1490 {
1491   vl_api_session_rules_details_t *rmp = 0;
1492   session_mask_or_match_4_t *match =
1493     (session_mask_or_match_4_t *) & rule->match;
1494   session_mask_or_match_4_t *mask =
1495     (session_mask_or_match_4_t *) & rule->mask;
1496
1497   rmp = vl_msg_api_alloc (sizeof (*rmp));
1498   memset (rmp, 0, sizeof (*rmp));
1499   rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS);
1500   rmp->context = context;
1501
1502   rmp->is_ip4 = 1;
1503   clib_memcpy (rmp->lcl_ip, &match->lcl_ip, sizeof (match->lcl_ip));
1504   clib_memcpy (rmp->rmt_ip, &match->rmt_ip, sizeof (match->rmt_ip));
1505   rmp->lcl_plen = ip4_mask_to_preflen (&mask->lcl_ip);
1506   rmp->rmt_plen = ip4_mask_to_preflen (&mask->rmt_ip);
1507   rmp->lcl_port = match->lcl_port;
1508   rmp->rmt_port = match->rmt_port;
1509   rmp->action_index = clib_host_to_net_u32 (rule->action_index);
1510   rmp->scope =
1511     is_local ? SESSION_RULE_SCOPE_LOCAL : SESSION_RULE_SCOPE_GLOBAL;
1512   rmp->transport_proto = transport_proto;
1513   rmp->appns_index = clib_host_to_net_u32 (appns_index);
1514   if (tag)
1515     {
1516       clib_memcpy (rmp->tag, tag, vec_len (tag));
1517       rmp->tag[vec_len (tag)] = 0;
1518     }
1519
1520   vl_api_send_msg (reg, (u8 *) rmp);
1521 }
1522
1523 static void
1524 send_session_rule_details6 (mma_rule_40_t * rule, u8 is_local,
1525                             u8 transport_proto, u32 appns_index, u8 * tag,
1526                             vl_api_registration_t * reg, u32 context)
1527 {
1528   vl_api_session_rules_details_t *rmp = 0;
1529   session_mask_or_match_6_t *match =
1530     (session_mask_or_match_6_t *) & rule->match;
1531   session_mask_or_match_6_t *mask =
1532     (session_mask_or_match_6_t *) & rule->mask;
1533
1534   rmp = vl_msg_api_alloc (sizeof (*rmp));
1535   memset (rmp, 0, sizeof (*rmp));
1536   rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS);
1537   rmp->context = context;
1538
1539   rmp->is_ip4 = 0;
1540   clib_memcpy (rmp->lcl_ip, &match->lcl_ip, sizeof (match->lcl_ip));
1541   clib_memcpy (rmp->rmt_ip, &match->rmt_ip, sizeof (match->rmt_ip));
1542   rmp->lcl_plen = ip6_mask_to_preflen (&mask->lcl_ip);
1543   rmp->rmt_plen = ip6_mask_to_preflen (&mask->rmt_ip);
1544   rmp->lcl_port = match->lcl_port;
1545   rmp->rmt_port = match->rmt_port;
1546   rmp->action_index = clib_host_to_net_u32 (rule->action_index);
1547   rmp->scope =
1548     is_local ? SESSION_RULE_SCOPE_LOCAL : SESSION_RULE_SCOPE_GLOBAL;
1549   rmp->transport_proto = transport_proto;
1550   rmp->appns_index = clib_host_to_net_u32 (appns_index);
1551   if (tag)
1552     {
1553       clib_memcpy (rmp->tag, tag, vec_len (tag));
1554       rmp->tag[vec_len (tag)] = 0;
1555     }
1556
1557   vl_api_send_msg (reg, (u8 *) rmp);
1558 }
1559
1560 static void
1561 send_session_rules_table_details (session_rules_table_t * srt, u8 fib_proto,
1562                                   u8 tp, u8 is_local, u32 appns_index,
1563                                   vl_api_registration_t * reg, u32 context)
1564 {
1565   mma_rule_16_t *rule16;
1566   mma_rule_40_t *rule40;
1567   mma_rules_table_16_t *srt16;
1568   mma_rules_table_40_t *srt40;
1569   u32 ri;
1570
1571   if (is_local || fib_proto == FIB_PROTOCOL_IP4)
1572     {
1573       u8 *tag = 0;
1574       /* *INDENT-OFF* */
1575       srt16 = &srt->session_rules_tables_16;
1576       pool_foreach (rule16, srt16->rules, ({
1577         ri = mma_rules_table_rule_index_16 (srt16, rule16);
1578         tag = session_rules_table_rule_tag (srt, ri, 1);
1579         send_session_rule_details4 (rule16, is_local, tp, appns_index, tag,
1580                                     reg, context);
1581       }));
1582       /* *INDENT-ON* */
1583     }
1584   if (is_local || fib_proto == FIB_PROTOCOL_IP6)
1585     {
1586       u8 *tag = 0;
1587       /* *INDENT-OFF* */
1588       srt40 = &srt->session_rules_tables_40;
1589       pool_foreach (rule40, srt40->rules, ({
1590         ri = mma_rules_table_rule_index_40 (srt40, rule40);
1591         tag = session_rules_table_rule_tag (srt, ri, 1);
1592         send_session_rule_details6 (rule40, is_local, tp, appns_index, tag,
1593                                     reg, context);
1594       }));
1595       /* *INDENT-ON* */
1596     }
1597 }
1598
1599 static void
1600 vl_api_session_rules_dump_t_handler (vl_api_one_map_server_dump_t * mp)
1601 {
1602   vl_api_registration_t *reg;
1603   session_table_t *st;
1604   u8 tp;
1605
1606   reg = vl_api_client_index_to_registration (mp->client_index);
1607   if (!reg)
1608     return;
1609
1610   /* *INDENT-OFF* */
1611   session_table_foreach (st, ({
1612     for (tp = 0; tp < TRANSPORT_N_PROTO; tp++)
1613       {
1614         send_session_rules_table_details (&st->session_rules[tp],
1615                                           st->active_fib_proto, tp,
1616                                           st->is_local, st->appns_index, reg,
1617                                           mp->context);
1618       }
1619   }));
1620   /* *INDENT-ON* */
1621 }
1622
1623 static void
1624 vl_api_application_tls_cert_add_t_handler (vl_api_application_tls_cert_add_t *
1625                                            mp)
1626 {
1627   vl_api_app_namespace_add_del_reply_t *rmp;
1628   vnet_app_add_tls_cert_args_t _a, *a = &_a;
1629   clib_error_t *error;
1630   application_t *app;
1631   u32 cert_len;
1632   int rv = 0;
1633   if (!session_manager_is_enabled ())
1634     {
1635       rv = VNET_API_ERROR_FEATURE_DISABLED;
1636       goto done;
1637     }
1638   if (!(app = application_lookup (mp->client_index)))
1639     {
1640       rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1641       goto done;
1642     }
1643   memset (a, 0, sizeof (*a));
1644   a->app_index = app->app_index;
1645   cert_len = clib_net_to_host_u16 (mp->cert_len);
1646   if (cert_len > 10000)
1647     {
1648       rv = VNET_API_ERROR_INVALID_VALUE;
1649       goto done;
1650     }
1651   vec_validate (a->cert, cert_len);
1652   clib_memcpy (a->cert, mp->cert, cert_len);
1653   if ((error = vnet_app_add_tls_cert (a)))
1654     {
1655       rv = clib_error_get_code (error);
1656       clib_error_report (error);
1657     }
1658   vec_free (a->cert);
1659 done:
1660   REPLY_MACRO (VL_API_APPLICATION_TLS_CERT_ADD_REPLY);
1661 }
1662
1663 static void
1664 vl_api_application_tls_key_add_t_handler (vl_api_application_tls_key_add_t *
1665                                           mp)
1666 {
1667   vl_api_app_namespace_add_del_reply_t *rmp;
1668   vnet_app_add_tls_key_args_t _a, *a = &_a;
1669   clib_error_t *error;
1670   application_t *app;
1671   u32 key_len;
1672   int rv = 0;
1673   if (!session_manager_is_enabled ())
1674     {
1675       rv = VNET_API_ERROR_FEATURE_DISABLED;
1676       goto done;
1677     }
1678   if (!(app = application_lookup (mp->client_index)))
1679     {
1680       rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1681       goto done;
1682     }
1683   memset (a, 0, sizeof (*a));
1684   a->app_index = app->app_index;
1685   key_len = clib_net_to_host_u16 (mp->key_len);
1686   if (key_len > 10000)
1687     {
1688       rv = VNET_API_ERROR_INVALID_VALUE;
1689       goto done;
1690     }
1691   vec_validate (a->key, key_len);
1692   clib_memcpy (a->key, mp->key, key_len);
1693   if ((error = vnet_app_add_tls_key (a)))
1694     {
1695       rv = clib_error_get_code (error);
1696       clib_error_report (error);
1697     }
1698   vec_free (a->key);
1699 done:
1700   REPLY_MACRO (VL_API_APPLICATION_TLS_KEY_ADD_REPLY);
1701 }
1702
1703 static clib_error_t *
1704 application_reaper_cb (u32 client_index)
1705 {
1706   application_t *app = application_lookup (client_index);
1707   vnet_app_detach_args_t _a, *a = &_a;
1708   if (app)
1709     {
1710       a->app_index = app->app_index;
1711       vnet_application_detach (a);
1712     }
1713   return 0;
1714 }
1715
1716 VL_MSG_API_REAPER_FUNCTION (application_reaper_cb);
1717
1718 #define vl_msg_name_crc_list
1719 #include <vnet/vnet_all_api_h.h>
1720 #undef vl_msg_name_crc_list
1721
1722 static void
1723 setup_message_id_table (api_main_t * am)
1724 {
1725 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1726   foreach_vl_msg_name_crc_session;
1727 #undef _
1728 }
1729
1730 /*
1731  * session_api_hookup
1732  * Add uri's API message handlers to the table.
1733  * vlib has alread mapped shared memory and
1734  * added the client registration handlers.
1735  * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
1736  */
1737 static clib_error_t *
1738 session_api_hookup (vlib_main_t * vm)
1739 {
1740   api_main_t *am = &api_main;
1741
1742 #define _(N,n)                                                  \
1743     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
1744                            vl_api_##n##_t_handler,              \
1745                            vl_noop_handler,                     \
1746                            vl_api_##n##_t_endian,               \
1747                            vl_api_##n##_t_print,                \
1748                            sizeof(vl_api_##n##_t), 1);
1749   foreach_session_api_msg;
1750 #undef _
1751
1752   /*
1753    * Messages which bounce off the data-plane to
1754    * an API client. Simply tells the message handling infra not
1755    * to free the message.
1756    *
1757    * Bounced message handlers MUST NOT block the data plane
1758    */
1759   am->message_bounce[VL_API_CONNECT_URI] = 1;
1760   am->message_bounce[VL_API_CONNECT_SOCK] = 1;
1761
1762   /*
1763    * Set up the (msg_name, crc, message-id) table
1764    */
1765   setup_message_id_table (am);
1766
1767   return 0;
1768 }
1769
1770 VLIB_API_INIT_FUNCTION (session_api_hookup);
1771
1772 /*
1773  * fd.io coding-style-patch-verification: ON
1774  *
1775  * Local Variables:
1776  * eval: (c-set-style "gnu")
1777  * End:
1778  */