hsa: fifo detach cleanup
[vpp.git] / src / plugins / hs_apps / sapi / vpp_echo_bapi.c
1 /*
2  * Copyright (c) 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 <stdio.h>
17 #include <signal.h>
18
19 #include <hs_apps/sapi/vpp_echo_common.h>
20
21 /*
22  *
23  *  Binary API Messages
24  *
25  */
26
27 void
28 echo_send_attach (echo_main_t * em)
29 {
30   vl_api_app_attach_t *bmp;
31   bmp = vl_msg_api_alloc (sizeof (*bmp));
32   clib_memset (bmp, 0, sizeof (*bmp));
33
34   bmp->_vl_msg_id = ntohs (VL_API_APP_ATTACH);
35   bmp->client_index = em->my_client_index;
36   bmp->context = ntohl (0xfeedface);
37   bmp->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_ACCEPT_REDIRECT;
38   bmp->options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_ADD_SEGMENT;
39   bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = em->prealloc_fifo_pairs;
40   bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = em->fifo_size;
41   bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = em->fifo_size;
42   bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = 128 << 20;
43   bmp->options[APP_OPTIONS_SEGMENT_SIZE] = 256 << 20;
44   bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = em->evt_q_size;
45   if (em->appns_id)
46     {
47       vl_api_vec_to_api_string (em->appns_id, &bmp->namespace_id);
48       bmp->options[APP_OPTIONS_FLAGS] |= em->appns_flags;
49       bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = em->appns_secret;
50     }
51   vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & bmp);
52 }
53
54 void
55 echo_send_detach (echo_main_t * em)
56 {
57   vl_api_application_detach_t *bmp;
58   bmp = vl_msg_api_alloc (sizeof (*bmp));
59   clib_memset (bmp, 0, sizeof (*bmp));
60
61   bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
62   bmp->client_index = em->my_client_index;
63   bmp->context = ntohl (0xfeedface);
64
65   vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & bmp);
66 }
67
68 void
69 echo_send_add_cert_key (echo_main_t * em)
70 {
71   u32 cert_len = test_srv_crt_rsa_len;
72   u32 key_len = test_srv_key_rsa_len;
73   vl_api_app_add_cert_key_pair_t *bmp;
74
75   bmp = vl_msg_api_alloc (sizeof (*bmp) + cert_len + key_len);
76   clib_memset (bmp, 0, sizeof (*bmp) + cert_len + key_len);
77
78   bmp->_vl_msg_id = ntohs (VL_API_APP_ADD_CERT_KEY_PAIR);
79   bmp->client_index = em->my_client_index;
80   bmp->context = ntohl (0xfeedface);
81   bmp->cert_len = clib_host_to_net_u16 (cert_len);
82   bmp->certkey_len = clib_host_to_net_u16 (key_len + cert_len);
83   clib_memcpy_fast (bmp->certkey, test_srv_crt_rsa, cert_len);
84   clib_memcpy_fast (bmp->certkey + cert_len, test_srv_key_rsa, key_len);
85
86   vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & bmp);
87 }
88
89 void
90 echo_send_del_cert_key (echo_main_t * em)
91 {
92   vl_api_app_del_cert_key_pair_t *bmp;
93   bmp = vl_msg_api_alloc (sizeof (*bmp));
94   clib_memset (bmp, 0, sizeof (*bmp));
95
96   bmp->_vl_msg_id = ntohs (VL_API_APP_DEL_CERT_KEY_PAIR);
97   bmp->client_index = em->my_client_index;
98   bmp->context = ntohl (0xfeedface);
99   bmp->index = clib_host_to_net_u32 (em->ckpair_index);
100   vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & bmp);
101 }
102
103 void
104 echo_send_listen (echo_main_t * em, ip46_address_t * ip)
105 {
106   app_session_evt_t _app_evt, *app_evt = &_app_evt;
107   session_listen_msg_t *mp;
108   svm_msg_q_t *mq = em->ctrl_mq;
109
110   app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_LISTEN);
111   mp = (session_listen_msg_t *) app_evt->evt->data;
112   memset (mp, 0, sizeof (*mp));
113   mp->client_index = em->my_client_index;
114   mp->context = ntohl (0xfeedface);
115   mp->wrk_index = 0;
116   mp->is_ip4 = em->uri_elts.is_ip4;
117   clib_memcpy_fast (&mp->ip, ip, sizeof (mp->ip));
118   mp->port = em->uri_elts.port;
119   mp->proto = em->uri_elts.transport_proto;
120   mp->ckpair_index = em->ckpair_index;
121   mp->crypto_engine = em->crypto_engine;
122   app_send_ctrl_evt_to_vpp (mq, app_evt);
123 }
124
125 void
126 echo_send_unbind (echo_main_t * em, echo_session_t * s)
127 {
128   app_session_evt_t _app_evt, *app_evt = &_app_evt;
129   session_unlisten_msg_t *mp;
130   svm_msg_q_t *mq = em->ctrl_mq;
131
132   app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_UNLISTEN);
133   mp = (session_unlisten_msg_t *) app_evt->evt->data;
134   memset (mp, 0, sizeof (*mp));
135   mp->client_index = em->my_client_index;
136   mp->wrk_index = 0;
137   mp->handle = s->vpp_session_handle;
138   mp->context = 0;
139   app_send_ctrl_evt_to_vpp (mq, app_evt);
140 }
141
142 void
143 echo_send_connect (echo_main_t * em, void *args)
144 {
145   app_session_evt_t _app_evt, *app_evt = &_app_evt;
146   session_connect_msg_t *mp;
147   echo_connect_args_t *a = (echo_connect_args_t *) args;
148   svm_msg_q_t *mq = em->ctrl_mq;
149
150   clib_atomic_sub_fetch (&em->max_sim_connects, 1);
151   while (em->max_sim_connects <= 0)
152     ;
153
154   app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_CONNECT);
155   mp = (session_connect_msg_t *) app_evt->evt->data;
156   memset (mp, 0, sizeof (*mp));
157   mp->client_index = em->my_client_index;
158   mp->context = ntohl (a->context);
159   mp->wrk_index = 0;
160   mp->is_ip4 = em->uri_elts.is_ip4;
161   clib_memcpy_fast (&mp->ip, &a->ip, sizeof (mp->ip));
162   clib_memcpy_fast (&mp->lcl_ip, &a->lcl_ip, sizeof (mp->ip));
163   mp->port = em->uri_elts.port;
164   mp->proto = em->uri_elts.transport_proto;
165   mp->parent_handle = a->parent_session_handle;
166   mp->ckpair_index = em->ckpair_index;
167   mp->crypto_engine = em->crypto_engine;
168   mp->flags = em->connect_flag;
169   app_send_ctrl_evt_to_vpp (mq, app_evt);
170 }
171
172 void
173 echo_send_disconnect_session (echo_main_t * em, void *args)
174 {
175   echo_session_t *s;
176   app_session_evt_t _app_evt, *app_evt = &_app_evt;
177   session_disconnect_msg_t *mp;
178   svm_msg_q_t *mq = em->ctrl_mq;
179   echo_disconnect_args_t *a = (echo_disconnect_args_t *) args;
180
181   app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_DISCONNECT);
182   mp = (session_disconnect_msg_t *) app_evt->evt->data;
183   memset (mp, 0, sizeof (*mp));
184   mp->client_index = em->my_client_index;
185   mp->handle = a->session_handle;
186   app_send_ctrl_evt_to_vpp (mq, app_evt);
187
188   if (!(s = echo_get_session_from_handle (em, mp->handle)))
189     return;
190   em->proto_cb_vft->sent_disconnect_cb (s);
191 }
192
193 /*
194  *
195  *  Helpers
196  *
197  */
198
199 int
200 echo_segment_attach (u64 segment_handle, char *name, ssvm_segment_type_t type,
201                      int fd)
202 {
203   fifo_segment_create_args_t _a, *a = &_a;
204   echo_main_t *em = &echo_main;
205   fifo_segment_main_t *sm;
206   int rv;
207
208   clib_memset (a, 0, sizeof (*a));
209   a->segment_name = (char *) name;
210   a->segment_type = type;
211
212   if (type == SSVM_SEGMENT_MEMFD)
213     a->memfd_fd = fd;
214
215   sm = &em->segment_main;
216
217   if ((rv = fifo_segment_attach (sm, a)))
218     return rv;
219
220   clib_spinlock_lock (&em->segment_handles_lock);
221   hash_set (em->shared_segment_handles, segment_handle,
222             a->new_segment_indices[0]);
223   clib_spinlock_unlock (&em->segment_handles_lock);
224
225   vec_free (a->new_segment_indices);
226   return 0;
227 }
228
229 u32
230 echo_segment_lookup (u64 segment_handle)
231 {
232   echo_main_t *em = &echo_main;
233   uword *segment_idxp;
234
235   ECHO_LOG (3, "Check if segment mapped 0x%lx...", segment_handle);
236
237   clib_spinlock_lock (&em->segment_handles_lock);
238   segment_idxp = hash_get (em->shared_segment_handles, segment_handle);
239   clib_spinlock_unlock (&em->segment_handles_lock);
240   if (!segment_idxp)
241     return ~0;
242
243   ECHO_LOG (2, "Segment not mapped (0x%lx)", segment_handle);
244   return ((u32) *segment_idxp);
245 }
246
247 void
248 echo_segment_detach (u64 segment_handle)
249 {
250   echo_main_t *em = &echo_main;
251   fifo_segment_main_t *sm;
252
253   u32 segment_index = echo_segment_lookup (segment_handle);
254   if (segment_index == (u32) ~0)
255     return;
256
257   sm = &em->segment_main;
258
259   clib_spinlock_lock (&em->segment_handles_lock);
260   fifo_segment_delete (sm, fifo_segment_get_segment (sm, segment_index));
261   hash_unset (em->shared_segment_handles, segment_handle);
262   clib_spinlock_unlock (&em->segment_handles_lock);
263 }
264
265 int
266 echo_attach_session (uword segment_handle, uword rxf_offset, uword txf_offset,
267                      uword mq_offset, echo_session_t *s)
268 {
269   echo_main_t *em = &echo_main;
270   u32 fs_index, eqs_index;
271   fifo_segment_t *fs;
272
273   fs_index = echo_segment_lookup (segment_handle);
274   if (fs_index == (u32) ~0)
275     {
276       ECHO_LOG (0, "ERROR: segment for session %u is not mounted!",
277                 s->session_index);
278       return -1;
279     }
280
281   if (mq_offset != (uword) ~0)
282     {
283       eqs_index = echo_segment_lookup (ECHO_MQ_SEG_HANDLE);
284       ASSERT (eqs_index != (u32) ~0);
285     }
286
287   clib_spinlock_lock (&em->segment_handles_lock);
288
289   fs = fifo_segment_get_segment (&em->segment_main, fs_index);
290   s->rx_fifo = fifo_segment_alloc_fifo_w_offset (fs, rxf_offset);
291   s->tx_fifo = fifo_segment_alloc_fifo_w_offset (fs, txf_offset);
292   s->rx_fifo->segment_index = fs_index;
293   s->tx_fifo->segment_index = fs_index;
294   s->rx_fifo->shr->client_session_index = s->session_index;
295   s->tx_fifo->shr->client_session_index = s->session_index;
296
297   if (mq_offset != (uword) ~0)
298     {
299       fs = fifo_segment_get_segment (&em->segment_main, eqs_index);
300       s->vpp_evt_q = fifo_segment_msg_q_attach (fs, mq_offset,
301                                                 s->rx_fifo->shr->slice_index);
302     }
303
304   clib_spinlock_unlock (&em->segment_handles_lock);
305
306   return 0;
307 }
308
309 int
310 echo_segment_attach_mq (uword segment_handle, uword mq_offset, u32 mq_index,
311                         svm_msg_q_t **mq)
312 {
313   echo_main_t *em = &echo_main;
314   fifo_segment_t *fs;
315   u32 fs_index;
316
317   fs_index = echo_segment_lookup (segment_handle);
318   if (fs_index == (u32) ~0)
319     {
320       ECHO_LOG (0, "ERROR: mq segment %lx for is not attached!",
321                 segment_handle);
322       return -1;
323     }
324
325   clib_spinlock_lock (&em->segment_handles_lock);
326
327   fs = fifo_segment_get_segment (&em->segment_main, fs_index);
328   *mq = fifo_segment_msg_q_attach (fs, mq_offset, mq_index);
329
330   clib_spinlock_unlock (&em->segment_handles_lock);
331
332   return 0;
333 }
334
335 /*
336  *
337  *  Binary API callbacks
338  *
339  */
340
341 static void
342   vl_api_app_add_cert_key_pair_reply_t_handler
343   (vl_api_app_add_cert_key_pair_reply_t * mp)
344 {
345   echo_main_t *em = &echo_main;
346   if (mp->retval)
347     {
348       ECHO_FAIL (ECHO_FAIL_VL_API_CERT_KEY_ADD_REPLY,
349                  "Adding cert and key returned %d",
350                  clib_net_to_host_u32 (mp->retval));
351       return;
352     }
353   /* No concurrency here, only bapi thread writes */
354   if (em->state != STATE_ATTACHED_NO_CERT)
355     {
356       ECHO_FAIL (ECHO_FAIL_VL_API_CERT_KEY_ADD_REPLY, "Wrong state");
357       return;
358     }
359   em->ckpair_index = clib_net_to_host_u32 (mp->index);
360   em->state = STATE_ATTACHED;
361 }
362
363 static void
364   vl_api_app_del_cert_key_pair_reply_t_handler
365   (vl_api_app_del_cert_key_pair_reply_t * mp)
366 {
367   echo_main_t *em = &echo_main;
368   if (mp->retval)
369     {
370       ECHO_FAIL (ECHO_FAIL_VL_API_CERT_KEY_DEL_REPLY,
371                  "Delete cert and key returned %d",
372                  clib_net_to_host_u32 (mp->retval));
373       return;
374     }
375   em->state = STATE_CLEANED_CERT_KEY;
376 }
377
378 static void
379 vl_api_app_attach_reply_t_handler (vl_api_app_attach_reply_t * mp)
380 {
381   echo_main_t *em = &echo_main;
382   int *fds = 0, i, rv;
383   u32 n_fds = 0;
384   u64 segment_handle;
385   char *segment_name = 0;
386
387   segment_handle = clib_net_to_host_u64 (mp->segment_handle);
388   ECHO_LOG (2, "Attached returned app %u", htons (mp->app_index));
389
390   if (mp->retval)
391     {
392       ECHO_FAIL (ECHO_FAIL_VL_API_APP_ATTACH, "attach failed: %U",
393                  format_api_error, clib_net_to_host_u32 (mp->retval));
394       return;
395     }
396
397   if (!mp->app_mq)
398     {
399       ECHO_FAIL (ECHO_FAIL_VL_API_NULL_APP_MQ, "NULL app_mq");
400       return;
401     }
402
403   if (mp->n_fds)
404     {
405       vec_validate (fds, mp->n_fds);
406       if (vl_socket_client_recv_fd_msg (fds, mp->n_fds, 5))
407         {
408           ECHO_FAIL (ECHO_FAIL_VL_API_RECV_FD_MSG,
409                      "vl_socket_client_recv_fd_msg failed");
410           goto failed;
411         }
412
413       if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
414         if (echo_segment_attach (ECHO_MQ_SEG_HANDLE, 0, SSVM_SEGMENT_MEMFD,
415                                  fds[n_fds++]))
416           {
417             ECHO_FAIL (ECHO_FAIL_VL_API_SVM_FIFO_SEG_ATTACH,
418                        "svm_fifo_segment_attach failed on SSVM_SEGMENT_MEMFD");
419             goto failed;
420           }
421       echo_segment_attach_mq (ECHO_MQ_SEG_HANDLE, mp->vpp_ctrl_mq,
422                               mp->vpp_ctrl_mq_thread, &em->ctrl_mq);
423
424       if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
425         {
426           segment_name = vl_api_from_api_to_new_c_string (&mp->segment_name);
427           rv = echo_segment_attach (segment_handle, segment_name,
428                                     SSVM_SEGMENT_MEMFD, fds[n_fds++]);
429           if (rv != 0)
430             {
431               ECHO_FAIL (ECHO_FAIL_VL_API_SVM_FIFO_SEG_ATTACH,
432                          "svm_fifo_segment_attach ('%s') "
433                          "failed on SSVM_SEGMENT_MEMFD", segment_name);
434               vec_free (segment_name);
435               goto failed;
436             }
437           vec_free (segment_name);
438         }
439       echo_segment_attach_mq (segment_handle, mp->app_mq, 0, &em->app_mq);
440
441       if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
442         svm_msg_q_set_eventfd (em->app_mq, fds[n_fds++]);
443
444       vec_free (fds);
445     }
446   else
447     {
448       segment_name = vl_api_from_api_to_new_c_string (&mp->segment_name);
449       rv = echo_segment_attach (segment_handle, segment_name, SSVM_SEGMENT_SHM,
450                                 -1);
451       if (rv != 0)
452         {
453           ECHO_FAIL (ECHO_FAIL_VL_API_SVM_FIFO_SEG_ATTACH,
454                      "svm_fifo_segment_attach ('%s') "
455                      "failed on SSVM_SEGMENT_SHM", segment_name);
456           vec_free (segment_name);
457           goto failed;
458         }
459       vec_free (segment_name);
460     }
461   ECHO_LOG (2, "Mapped segment 0x%lx", segment_handle);
462
463   em->state = STATE_ATTACHED_NO_CERT;
464   return;
465 failed:
466   for (i = clib_max (n_fds - 1, 0); i < vec_len (fds); i++)
467     close (fds[i]);
468   vec_free (fds);
469 }
470
471 static void
472 vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
473                                            mp)
474 {
475   if (mp->retval)
476     {
477       ECHO_FAIL (ECHO_FAIL_VL_API_DETACH_REPLY,
478                  "app detach returned with err: %d", mp->retval);
479       return;
480     }
481   echo_main.state = STATE_DETACHED;
482 }
483
484 #define foreach_quic_echo_msg                                    \
485 _(APP_ATTACH_REPLY, app_attach_reply)                            \
486 _(APPLICATION_DETACH_REPLY, application_detach_reply)            \
487 _(APP_ADD_CERT_KEY_PAIR_REPLY, app_add_cert_key_pair_reply)      \
488 _(APP_DEL_CERT_KEY_PAIR_REPLY, app_del_cert_key_pair_reply)
489
490 void
491 echo_api_hookup (echo_main_t * em)
492 {
493 #define _(N,n)                                                  \
494     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
495                            vl_api_##n##_t_handler,              \
496                            vl_noop_handler,                     \
497                            vl_api_##n##_t_endian,               \
498                            vl_api_##n##_t_print,                \
499                            sizeof(vl_api_##n##_t), 1);
500   foreach_quic_echo_msg;
501 #undef _
502 }
503
504 /*
505  * fd.io coding-style-patch-verification: ON
506  *
507  * Local Variables:
508  * eval: (c-set-style "gnu")
509  * End:
510  */