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