svm: allow mq attachments at random offsets
[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   svm_fifo_shared_t *rx_fifo, *tx_fifo;
270   echo_main_t *em = &echo_main;
271   u32 fs_index, eqs_index;
272   fifo_segment_t *fs;
273
274   fs_index = echo_segment_lookup (segment_handle);
275   if (fs_index == (u32) ~0)
276     {
277       ECHO_LOG (0, "ERROR: segment for session %u is not mounted!",
278                 s->session_index);
279       return -1;
280     }
281
282   if (mq_offset != (uword) ~0)
283     {
284       eqs_index = echo_segment_lookup (ECHO_MQ_SEG_HANDLE);
285       ASSERT (eqs_index != (u32) ~0);
286     }
287
288   rx_fifo = uword_to_pointer (rxf_offset, svm_fifo_shared_t *);
289   tx_fifo = uword_to_pointer (txf_offset, svm_fifo_shared_t *);
290   rx_fifo->client_session_index = s->session_index;
291   tx_fifo->client_session_index = s->session_index;
292
293   clib_spinlock_lock (&em->segment_handles_lock);
294
295   fs = fifo_segment_get_segment (&em->segment_main, fs_index);
296   s->rx_fifo = fifo_segment_alloc_fifo_w_shared (fs, rx_fifo);
297   s->tx_fifo = fifo_segment_alloc_fifo_w_shared (fs, tx_fifo);
298
299   if (mq_offset != (uword) ~0)
300     {
301       fs = fifo_segment_get_segment (&em->segment_main, eqs_index);
302       s->vpp_evt_q =
303         fifo_segment_msg_q_attach (fs, mq_offset, rx_fifo->slice_index);
304     }
305
306   clib_spinlock_unlock (&em->segment_handles_lock);
307
308   return 0;
309 }
310
311 int
312 echo_segment_attach_mq (uword segment_handle, uword mq_offset, u32 mq_index,
313                         svm_msg_q_t **mq)
314 {
315   echo_main_t *em = &echo_main;
316   fifo_segment_t *fs;
317   u32 fs_index;
318
319   fs_index = echo_segment_lookup (segment_handle);
320   if (fs_index == (u32) ~0)
321     {
322       ECHO_LOG (0, "ERROR: mq segment %lx for is not attached!",
323                 segment_handle);
324       return -1;
325     }
326
327   clib_spinlock_lock (&em->segment_handles_lock);
328
329   fs = fifo_segment_get_segment (&em->segment_main, fs_index);
330   *mq = fifo_segment_msg_q_attach (fs, mq_offset, mq_index);
331
332   clib_spinlock_unlock (&em->segment_handles_lock);
333
334   return 0;
335 }
336
337 /*
338  *
339  *  Binary API callbacks
340  *
341  */
342
343 static void
344   vl_api_app_add_cert_key_pair_reply_t_handler
345   (vl_api_app_add_cert_key_pair_reply_t * mp)
346 {
347   echo_main_t *em = &echo_main;
348   if (mp->retval)
349     {
350       ECHO_FAIL (ECHO_FAIL_VL_API_CERT_KEY_ADD_REPLY,
351                  "Adding cert and key returned %d",
352                  clib_net_to_host_u32 (mp->retval));
353       return;
354     }
355   /* No concurrency here, only bapi thread writes */
356   if (em->state != STATE_ATTACHED_NO_CERT)
357     {
358       ECHO_FAIL (ECHO_FAIL_VL_API_CERT_KEY_ADD_REPLY, "Wrong state");
359       return;
360     }
361   em->ckpair_index = clib_net_to_host_u32 (mp->index);
362   em->state = STATE_ATTACHED;
363 }
364
365 static void
366   vl_api_app_del_cert_key_pair_reply_t_handler
367   (vl_api_app_del_cert_key_pair_reply_t * mp)
368 {
369   echo_main_t *em = &echo_main;
370   if (mp->retval)
371     {
372       ECHO_FAIL (ECHO_FAIL_VL_API_CERT_KEY_DEL_REPLY,
373                  "Delete cert and key returned %d",
374                  clib_net_to_host_u32 (mp->retval));
375       return;
376     }
377   em->state = STATE_CLEANED_CERT_KEY;
378 }
379
380 static void
381 vl_api_app_attach_reply_t_handler (vl_api_app_attach_reply_t * mp)
382 {
383   echo_main_t *em = &echo_main;
384   int *fds = 0, i, rv;
385   u32 n_fds = 0;
386   u64 segment_handle;
387   char *segment_name = 0;
388
389   segment_handle = clib_net_to_host_u64 (mp->segment_handle);
390   ECHO_LOG (2, "Attached returned app %u", htons (mp->app_index));
391
392   if (mp->retval)
393     {
394       ECHO_FAIL (ECHO_FAIL_VL_API_APP_ATTACH, "attach failed: %U",
395                  format_api_error, clib_net_to_host_u32 (mp->retval));
396       return;
397     }
398
399   if (!mp->app_mq)
400     {
401       ECHO_FAIL (ECHO_FAIL_VL_API_NULL_APP_MQ, "NULL app_mq");
402       return;
403     }
404
405   if (mp->n_fds)
406     {
407       vec_validate (fds, mp->n_fds);
408       if (vl_socket_client_recv_fd_msg (fds, mp->n_fds, 5))
409         {
410           ECHO_FAIL (ECHO_FAIL_VL_API_RECV_FD_MSG,
411                      "vl_socket_client_recv_fd_msg failed");
412           goto failed;
413         }
414
415       if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
416         if (echo_segment_attach (ECHO_MQ_SEG_HANDLE, 0, SSVM_SEGMENT_MEMFD,
417                                  fds[n_fds++]))
418           {
419             ECHO_FAIL (ECHO_FAIL_VL_API_SVM_FIFO_SEG_ATTACH,
420                        "svm_fifo_segment_attach failed on SSVM_SEGMENT_MEMFD");
421             goto failed;
422           }
423       echo_segment_attach_mq (ECHO_MQ_SEG_HANDLE, mp->vpp_ctrl_mq,
424                               mp->vpp_ctrl_mq_thread, &em->ctrl_mq);
425
426       if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
427         {
428           segment_name = vl_api_from_api_to_new_c_string (&mp->segment_name);
429           rv = echo_segment_attach (segment_handle, segment_name,
430                                     SSVM_SEGMENT_MEMFD, fds[n_fds++]);
431           if (rv != 0)
432             {
433               ECHO_FAIL (ECHO_FAIL_VL_API_SVM_FIFO_SEG_ATTACH,
434                          "svm_fifo_segment_attach ('%s') "
435                          "failed on SSVM_SEGMENT_MEMFD", segment_name);
436               vec_free (segment_name);
437               goto failed;
438             }
439           vec_free (segment_name);
440         }
441       echo_segment_attach_mq (segment_handle, mp->app_mq, 0, &em->app_mq);
442
443       if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
444         svm_msg_q_set_consumer_eventfd (em->app_mq, fds[n_fds++]);
445
446       vec_free (fds);
447     }
448   else
449     {
450       segment_name = vl_api_from_api_to_new_c_string (&mp->segment_name);
451       rv = echo_segment_attach (segment_handle, segment_name, SSVM_SEGMENT_SHM,
452                                 -1);
453       if (rv != 0)
454         {
455           ECHO_FAIL (ECHO_FAIL_VL_API_SVM_FIFO_SEG_ATTACH,
456                      "svm_fifo_segment_attach ('%s') "
457                      "failed on SSVM_SEGMENT_SHM", segment_name);
458           vec_free (segment_name);
459           goto failed;
460         }
461       vec_free (segment_name);
462     }
463   ECHO_LOG (2, "Mapped segment 0x%lx", segment_handle);
464
465   em->state = STATE_ATTACHED_NO_CERT;
466   return;
467 failed:
468   for (i = clib_max (n_fds - 1, 0); i < vec_len (fds); i++)
469     close (fds[i]);
470   vec_free (fds);
471 }
472
473 static void
474 vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
475                                            mp)
476 {
477   if (mp->retval)
478     {
479       ECHO_FAIL (ECHO_FAIL_VL_API_DETACH_REPLY,
480                  "app detach returned with err: %d", mp->retval);
481       return;
482     }
483   echo_main.state = STATE_DETACHED;
484 }
485
486 #define foreach_quic_echo_msg                                    \
487 _(APP_ATTACH_REPLY, app_attach_reply)                            \
488 _(APPLICATION_DETACH_REPLY, application_detach_reply)            \
489 _(APP_ADD_CERT_KEY_PAIR_REPLY, app_add_cert_key_pair_reply)      \
490 _(APP_DEL_CERT_KEY_PAIR_REPLY, app_del_cert_key_pair_reply)
491
492 void
493 echo_api_hookup (echo_main_t * em)
494 {
495 #define _(N,n)                                                  \
496     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
497                            vl_api_##n##_t_handler,              \
498                            vl_noop_handler,                     \
499                            vl_api_##n##_t_endian,               \
500                            vl_api_##n##_t_print,                \
501                            sizeof(vl_api_##n##_t), 1);
502   foreach_quic_echo_msg;
503 #undef _
504 }
505
506 /*
507  * fd.io coding-style-patch-verification: ON
508  *
509  * Local Variables:
510  * eval: (c-set-style "gnu")
511  * End:
512  */