hsa: Refactor quic_echo to allow other protocols
[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_application_attach_t *bmp;
31   vl_api_application_tls_cert_add_t *cert_mp;
32   vl_api_application_tls_key_add_t *key_mp;
33
34   bmp = vl_msg_api_alloc (sizeof (*bmp));
35   clib_memset (bmp, 0, sizeof (*bmp));
36
37   bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_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] = 16;
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] = 256;
48   if (em->appns_id)
49     {
50       bmp->namespace_id_len = vec_len (em->appns_id);
51       clib_memcpy_fast (bmp->namespace_id, em->appns_id,
52                         bmp->namespace_id_len);
53       bmp->options[APP_OPTIONS_FLAGS] |= em->appns_flags;
54       bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = em->appns_secret;
55     }
56   vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & bmp);
57
58   cert_mp = vl_msg_api_alloc (sizeof (*cert_mp) + test_srv_crt_rsa_len);
59   clib_memset (cert_mp, 0, sizeof (*cert_mp));
60   cert_mp->_vl_msg_id = ntohs (VL_API_APPLICATION_TLS_CERT_ADD);
61   cert_mp->client_index = em->my_client_index;
62   cert_mp->context = ntohl (0xfeedface);
63   cert_mp->cert_len = clib_host_to_net_u16 (test_srv_crt_rsa_len);
64   clib_memcpy_fast (cert_mp->cert, test_srv_crt_rsa, test_srv_crt_rsa_len);
65   vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & cert_mp);
66
67   key_mp = vl_msg_api_alloc (sizeof (*key_mp) + test_srv_key_rsa_len);
68   clib_memset (key_mp, 0, sizeof (*key_mp) + test_srv_key_rsa_len);
69   key_mp->_vl_msg_id = ntohs (VL_API_APPLICATION_TLS_KEY_ADD);
70   key_mp->client_index = em->my_client_index;
71   key_mp->context = ntohl (0xfeedface);
72   key_mp->key_len = clib_host_to_net_u16 (test_srv_key_rsa_len);
73   clib_memcpy_fast (key_mp->key, test_srv_key_rsa, test_srv_key_rsa_len);
74   vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & key_mp);
75 }
76
77 void
78 echo_send_detach (echo_main_t * em)
79 {
80   vl_api_application_detach_t *bmp;
81   bmp = vl_msg_api_alloc (sizeof (*bmp));
82   clib_memset (bmp, 0, sizeof (*bmp));
83
84   bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
85   bmp->client_index = em->my_client_index;
86   bmp->context = ntohl (0xfeedface);
87   vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & bmp);
88 }
89
90 void
91 echo_send_listen (echo_main_t * em)
92 {
93   vl_api_bind_uri_t *bmp;
94   bmp = vl_msg_api_alloc (sizeof (*bmp));
95   clib_memset (bmp, 0, sizeof (*bmp));
96
97   bmp->_vl_msg_id = ntohs (VL_API_BIND_URI);
98   bmp->client_index = em->my_client_index;
99   bmp->context = ntohl (0xfeedface);
100   memcpy (bmp->uri, em->uri, vec_len (em->uri));
101   vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & bmp);
102 }
103
104 void
105 echo_send_unbind (echo_main_t * em)
106 {
107   vl_api_unbind_uri_t *ump;
108
109   ump = vl_msg_api_alloc (sizeof (*ump));
110   clib_memset (ump, 0, sizeof (*ump));
111
112   ump->_vl_msg_id = ntohs (VL_API_UNBIND_URI);
113   ump->client_index = em->my_client_index;
114   memcpy (ump->uri, em->uri, vec_len (em->uri));
115   vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & ump);
116 }
117
118 void
119 echo_send_connect (u8 * uri, u32 opaque)
120 {
121   echo_main_t *em = &echo_main;
122   vl_api_connect_uri_t *cmp;
123   cmp = vl_msg_api_alloc (sizeof (*cmp));
124   clib_memset (cmp, 0, sizeof (*cmp));
125   cmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI);
126   cmp->client_index = em->my_client_index;
127   cmp->context = ntohl (opaque);
128   memcpy (cmp->uri, uri, vec_len (uri));
129   vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & cmp);
130 }
131
132 void
133 echo_send_disconnect_session (u64 handle, u32 opaque)
134 {
135   echo_main_t *em = &echo_main;
136   vl_api_disconnect_session_t *dmp;
137   dmp = vl_msg_api_alloc (sizeof (*dmp));
138   clib_memset (dmp, 0, sizeof (*dmp));
139   dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
140   dmp->client_index = em->my_client_index;
141   dmp->handle = handle;
142   ECHO_LOG (1, "Disconnect session 0x%lx", dmp->handle);
143   vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & dmp);
144 }
145
146 /*
147  *
148  *  Helpers
149  *
150  */
151
152 static int
153 ssvm_segment_attach (char *name, ssvm_segment_type_t type, int fd)
154 {
155   fifo_segment_create_args_t _a, *a = &_a;
156   fifo_segment_main_t *sm = &echo_main.segment_main;
157   int rv;
158
159   clib_memset (a, 0, sizeof (*a));
160   a->segment_name = (char *) name;
161   a->segment_type = type;
162
163   if (type == SSVM_SEGMENT_MEMFD)
164     a->memfd_fd = fd;
165
166   if ((rv = fifo_segment_attach (sm, a)))
167     return rv;
168   vec_reset_length (a->new_segment_indices);
169   return 0;
170 }
171
172 static inline void
173 echo_segment_handle_add_del (echo_main_t * em, u64 segment_handle, u8 add)
174 {
175   clib_spinlock_lock (&em->segment_handles_lock);
176   if (add)
177     hash_set (em->shared_segment_handles, segment_handle, 1);
178   else
179     hash_unset (em->shared_segment_handles, segment_handle);
180   clib_spinlock_unlock (&em->segment_handles_lock);
181 }
182
183 /*
184  *
185  *  Binary API callbacks
186  *
187  */
188
189 static void
190 vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
191                                            mp)
192 {
193   echo_main_t *em = &echo_main;
194   int *fds = 0, i;
195   u32 n_fds = 0;
196   u64 segment_handle;
197   segment_handle = clib_net_to_host_u64 (mp->segment_handle);
198   ECHO_LOG (1, "Attached returned app %u", htons (mp->app_index));
199
200   if (mp->retval)
201     {
202       ECHO_FAIL ("attach failed: %U", format_api_error,
203                  clib_net_to_host_u32 (mp->retval));
204       return;
205     }
206
207   if (mp->segment_name_length == 0)
208     {
209       ECHO_FAIL ("segment_name_length zero");
210       return;
211     }
212
213   ASSERT (mp->app_event_queue_address);
214   em->our_event_queue = uword_to_pointer (mp->app_event_queue_address,
215                                           svm_msg_q_t *);
216
217   if (mp->n_fds)
218     {
219       vec_validate (fds, mp->n_fds);
220       if (vl_socket_client_recv_fd_msg (fds, mp->n_fds, 5))
221         {
222           ECHO_FAIL ("vl_socket_client_recv_fd_msg failed");
223           goto failed;
224         }
225
226       if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
227         if (ssvm_segment_attach (0, SSVM_SEGMENT_MEMFD, fds[n_fds++]))
228           {
229             ECHO_FAIL ("svm_fifo_segment_attach failed");
230             goto failed;
231           }
232
233       if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
234         if (ssvm_segment_attach ((char *) mp->segment_name,
235                                  SSVM_SEGMENT_MEMFD, fds[n_fds++]))
236           {
237             ECHO_FAIL ("svm_fifo_segment_attach ('%s') failed",
238                        mp->segment_name);
239             goto failed;
240           }
241       if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
242         svm_msg_q_set_consumer_eventfd (em->our_event_queue, fds[n_fds++]);
243
244       vec_free (fds);
245     }
246   else
247     {
248       if (ssvm_segment_attach ((char *) mp->segment_name, SSVM_SEGMENT_SHM,
249                                -1))
250         {
251           ECHO_FAIL ("svm_fifo_segment_attach ('%s') failed",
252                      mp->segment_name);
253           return;
254         }
255     }
256   echo_segment_handle_add_del (em, segment_handle, 1 /* add */ );
257   ECHO_LOG (1, "Mapped segment 0x%lx", segment_handle);
258
259   em->state = STATE_ATTACHED;
260   return;
261 failed:
262   for (i = clib_max (n_fds - 1, 0); i < vec_len (fds); i++)
263     close (fds[i]);
264   vec_free (fds);
265 }
266
267 static void
268 vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
269                                            mp)
270 {
271   if (mp->retval)
272     {
273       ECHO_FAIL ("detach returned with err: %d", mp->retval);
274       return;
275     }
276   echo_main.state = STATE_DETACHED;
277 }
278
279
280 static void
281 vl_api_unmap_segment_t_handler (vl_api_unmap_segment_t * mp)
282 {
283   echo_main_t *em = &echo_main;
284   u64 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
285   echo_segment_handle_add_del (em, segment_handle, 0 /* add */ );
286   ECHO_LOG (1, "Unmaped segment 0x%lx", segment_handle);
287 }
288
289 static void
290 vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
291 {
292   fifo_segment_main_t *sm = &echo_main.segment_main;
293   fifo_segment_create_args_t _a, *a = &_a;
294   echo_main_t *em = &echo_main;
295   int *fds = 0, i;
296   char *seg_name = (char *) mp->segment_name;
297   u64 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
298
299   if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
300     {
301       vec_validate (fds, 1);
302       if (vl_socket_client_recv_fd_msg (fds, 1, 5))
303         {
304           ECHO_FAIL ("vl_socket_client_recv_fd_msg failed");
305           goto failed;
306         }
307
308       if (ssvm_segment_attach (seg_name, SSVM_SEGMENT_MEMFD, fds[0]))
309         {
310           ECHO_FAIL ("svm_fifo_segment_attach ('%s')"
311                      "failed on SSVM_SEGMENT_MEMFD", seg_name);
312           goto failed;
313         }
314       vec_free (fds);
315     }
316   else
317     {
318       clib_memset (a, 0, sizeof (*a));
319       a->segment_name = seg_name;
320       a->segment_size = mp->segment_size;
321       /* Attach to the segment vpp created */
322       if (fifo_segment_attach (sm, a))
323         {
324           ECHO_FAIL ("svm_fifo_segment_attach ('%s') failed", seg_name);
325           goto failed;
326         }
327     }
328   echo_segment_handle_add_del (em, segment_handle, 1 /* add */ );
329   ECHO_LOG (1, "Mapped segment 0x%lx", segment_handle);
330   return;
331
332 failed:
333   for (i = 0; i < vec_len (fds); i++)
334     close (fds[i]);
335   vec_free (fds);
336 }
337
338 static void
339 vl_api_bind_uri_reply_t_handler (vl_api_bind_uri_reply_t * mp)
340 {
341   if (mp->retval)
342     {
343       ECHO_FAIL ("bind failed: %U", format_api_error,
344                  clib_net_to_host_u32 (mp->retval));
345     }
346 }
347
348 static void
349 vl_api_unbind_uri_reply_t_handler (vl_api_unbind_uri_reply_t * mp)
350 {
351   echo_session_t *listen_session;
352   echo_main_t *em = &echo_main;
353   if (mp->retval != 0)
354     {
355       ECHO_FAIL ("returned %d", ntohl (mp->retval));
356       return;
357     }
358   listen_session = pool_elt_at_index (em->sessions, em->listen_session_index);
359   em->proto_cb_vft->cleanup_cb (listen_session, 0 /* parent_died */ );
360   em->state = STATE_DISCONNECTED;
361 }
362
363 static void
364 vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
365                                            mp)
366 {
367   echo_main_t *em = &echo_main;
368   echo_session_t *s;
369
370   if (mp->retval)
371     {
372       ECHO_FAIL ("vpp complained about disconnect: %d", ntohl (mp->retval));
373       return;
374     }
375
376   ECHO_LOG (1, "Got disonnected reply for session 0x%lx", mp->handle);
377   if (!(s = echo_get_session_from_handle (em, mp->handle)))
378     return;
379   em->proto_cb_vft->disconnected_reply_cb (s);
380 }
381
382 static void
383   vl_api_application_tls_cert_add_reply_t_handler
384   (vl_api_application_tls_cert_add_reply_t * mp)
385 {
386   if (mp->retval)
387     ECHO_FAIL ("failed to add tls cert");
388 }
389
390 static void
391   vl_api_application_tls_key_add_reply_t_handler
392   (vl_api_application_tls_key_add_reply_t * mp)
393 {
394   if (mp->retval)
395     ECHO_FAIL ("failed to add tls key");
396 }
397
398 static void
399 vl_api_connect_uri_reply_t_handler (vl_api_connect_uri_reply_t * mp)
400 {
401   echo_main_t *em = &echo_main;
402   if (mp->retval && (em->proto_cb_vft->connected_cb))
403     em->proto_cb_vft->connected_cb ((session_connected_bundled_msg_t *) mp,
404                                     mp->context, 1 /* is_failed */ );
405 }
406
407 #define foreach_quic_echo_msg                                           \
408 _(BIND_URI_REPLY, bind_uri_reply)                                       \
409 _(UNBIND_URI_REPLY, unbind_uri_reply)                                   \
410 _(DISCONNECT_SESSION_REPLY, disconnect_session_reply)                   \
411 _(APPLICATION_ATTACH_REPLY, application_attach_reply)                   \
412 _(APPLICATION_DETACH_REPLY, application_detach_reply)                   \
413 _(MAP_ANOTHER_SEGMENT, map_another_segment)                             \
414 _(UNMAP_SEGMENT, unmap_segment)                                         \
415 _(APPLICATION_TLS_CERT_ADD_REPLY, application_tls_cert_add_reply)       \
416 _(APPLICATION_TLS_KEY_ADD_REPLY, application_tls_key_add_reply)         \
417 _(CONNECT_URI_REPLY, connect_uri_reply)         \
418
419 void
420 echo_api_hookup (echo_main_t * em)
421 {
422 #define _(N,n)                                                  \
423     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
424                            vl_api_##n##_t_handler,              \
425                            vl_noop_handler,                     \
426                            vl_api_##n##_t_endian,               \
427                            vl_api_##n##_t_print,                \
428                            sizeof(vl_api_##n##_t), 1);
429   foreach_quic_echo_msg;
430 #undef _
431 }
432
433 /*
434  * fd.io coding-style-patch-verification: ON
435  *
436  * Local Variables:
437  * eval: (c-set-style "gnu")
438  * End:
439  */