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