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