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