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