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