vpp_echo_bapi.c revision 7028a015
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
27void
28echo_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] = 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      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
77void
78echo_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
90void
91echo_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
104void
105echo_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
118void
119echo_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
132void
133echo_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
152static int
153ssvm_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
172static inline void
173echo_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
189static void
190vl_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 (ECHO_FAIL_VL_API_APP_ATTACH, "attach failed: %U",
203		 format_api_error, clib_net_to_host_u32 (mp->retval));
204      return;
205    }
206
207  if (mp->segment_name_length == 0)
208    {
209      ECHO_FAIL (ECHO_FAIL_VL_API_MISSING_SEGMENT_NAME,
210		 "segment_name_length zero");
211      return;
212    }
213
214  ASSERT (mp->app_event_queue_address);
215  em->our_event_queue = uword_to_pointer (mp->app_event_queue_address,
216					  svm_msg_q_t *);
217
218  if (mp->n_fds)
219    {
220      vec_validate (fds, mp->n_fds);
221      if (vl_socket_client_recv_fd_msg (fds, mp->n_fds, 5))
222	{
223	  ECHO_FAIL (ECHO_FAIL_VL_API_RECV_FD_MSG,
224		     "vl_socket_client_recv_fd_msg failed");
225	  goto failed;
226	}
227
228      if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
229	if (ssvm_segment_attach (0, SSVM_SEGMENT_MEMFD, fds[n_fds++]))
230	  {
231	    ECHO_FAIL (ECHO_FAIL_VL_API_SVM_FIFO_SEG_ATTACH,
232		       "svm_fifo_segment_attach failed on SSVM_SEGMENT_MEMFD");
233	    goto failed;
234	  }
235
236      if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
237	if (ssvm_segment_attach ((char *) mp->segment_name,
238				 SSVM_SEGMENT_MEMFD, fds[n_fds++]))
239	  {
240	    ECHO_FAIL (ECHO_FAIL_VL_API_SVM_FIFO_SEG_ATTACH,
241		       "svm_fifo_segment_attach ('%s') "
242		       "failed on SSVM_SEGMENT_MEMFD", mp->segment_name);
243	    goto failed;
244	  }
245      if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
246	svm_msg_q_set_consumer_eventfd (em->our_event_queue, fds[n_fds++]);
247
248      vec_free (fds);
249    }
250  else
251    {
252      if (ssvm_segment_attach ((char *) mp->segment_name, SSVM_SEGMENT_SHM,
253			       -1))
254	{
255	  ECHO_FAIL (ECHO_FAIL_VL_API_SVM_FIFO_SEG_ATTACH,
256		     "svm_fifo_segment_attach ('%s') "
257		     "failed on SSVM_SEGMENT_SHM", mp->segment_name);
258	  return;
259	}
260    }
261  echo_segment_handle_add_del (em, segment_handle, 1 /* add */ );
262  ECHO_LOG (1, "Mapped segment 0x%lx", segment_handle);
263
264  em->state = STATE_ATTACHED;
265  return;
266failed:
267  for (i = clib_max (n_fds - 1, 0); i < vec_len (fds); i++)
268    close (fds[i]);
269  vec_free (fds);
270}
271
272static void
273vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
274					   mp)
275{
276  if (mp->retval)
277    {
278      ECHO_FAIL (ECHO_FAIL_VL_API_DETACH_REPLY,
279		 "app detach returned with err: %d", mp->retval);
280      return;
281    }
282  echo_main.state = STATE_DETACHED;
283}
284
285
286static void
287vl_api_unmap_segment_t_handler (vl_api_unmap_segment_t * mp)
288{
289  echo_main_t *em = &echo_main;
290  u64 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
291  echo_segment_handle_add_del (em, segment_handle, 0 /* add */ );
292  ECHO_LOG (1, "Unmaped segment 0x%lx", segment_handle);
293}
294
295static void
296vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
297{
298  fifo_segment_main_t *sm = &echo_main.segment_main;
299  fifo_segment_create_args_t _a, *a = &_a;
300  echo_main_t *em = &echo_main;
301  int *fds = 0, i;
302  char *seg_name = (char *) mp->segment_name;
303  u64 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
304
305  if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
306    {
307      vec_validate (fds, 1);
308      if (vl_socket_client_recv_fd_msg (fds, 1, 5))
309	{
310	  ECHO_FAIL (ECHO_FAIL_VL_API_RECV_FD_MSG,
311		     "vl_socket_client_recv_fd_msg failed");
312	  goto failed;
313	}
314
315      if (ssvm_segment_attach (seg_name, SSVM_SEGMENT_MEMFD, fds[0]))
316	{
317	  ECHO_FAIL (ECHO_FAIL_VL_API_SVM_FIFO_SEG_ATTACH,
318		     "svm_fifo_segment_attach ('%s') "
319		     "failed on SSVM_SEGMENT_MEMFD", seg_name);
320	  goto failed;
321	}
322      vec_free (fds);
323    }
324  else
325    {
326      clib_memset (a, 0, sizeof (*a));
327      a->segment_name = seg_name;
328      a->segment_size = mp->segment_size;
329      /* Attach to the segment vpp created */
330      if (fifo_segment_attach (sm, a))
331	{
332	  ECHO_FAIL (ECHO_FAIL_VL_API_FIFO_SEG_ATTACH,
333		     "fifo_segment_attach ('%s') failed", seg_name);
334	  goto failed;
335	}
336    }
337  echo_segment_handle_add_del (em, segment_handle, 1 /* add */ );
338  ECHO_LOG (1, "Mapped segment 0x%lx", segment_handle);
339  return;
340
341failed:
342  for (i = 0; i < vec_len (fds); i++)
343    close (fds[i]);
344  vec_free (fds);
345}
346
347static void
348vl_api_bind_uri_reply_t_handler (vl_api_bind_uri_reply_t * mp)
349{
350  if (mp->retval)
351    {
352      ECHO_FAIL (ECHO_FAIL_VL_API_BIND_URI_REPLY, "bind failed: %U",
353		 format_api_error, clib_net_to_host_u32 (mp->retval));
354    }
355}
356
357static void
358vl_api_unbind_uri_reply_t_handler (vl_api_unbind_uri_reply_t * mp)
359{
360  echo_session_t *listen_session;
361  echo_main_t *em = &echo_main;
362  if (mp->retval != 0)
363    {
364      ECHO_FAIL (ECHO_FAIL_VL_API_UNBIND_REPLY, "unbind_uri returned %d",
365		 ntohl (mp->retval));
366      return;
367    }
368  listen_session = pool_elt_at_index (em->sessions, em->listen_session_index);
369  em->proto_cb_vft->cleanup_cb (listen_session, 0 /* parent_died */ );
370  em->state = STATE_DISCONNECTED;
371}
372
373static void
374vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
375					   mp)
376{
377  echo_main_t *em = &echo_main;
378  echo_session_t *s;
379
380  if (mp->retval)
381    {
382      ECHO_FAIL (ECHO_FAIL_VL_API_DISCONNECT_SESSION_REPLY,
383		 "vpp complained about disconnect: %d", ntohl (mp->retval));
384      return;
385    }
386
387  ECHO_LOG (1, "Got disonnected reply for session 0x%lx", mp->handle);
388  if (!(s = echo_get_session_from_handle (em, mp->handle)))
389    return;
390  em->proto_cb_vft->disconnected_reply_cb (s);
391}
392
393static void
394  vl_api_application_tls_cert_add_reply_t_handler
395  (vl_api_application_tls_cert_add_reply_t * mp)
396{
397  if (mp->retval)
398    ECHO_FAIL (ECHO_FAIL_VL_API_TLS_CERT_ADD_REPLY,
399	       "failed to add application tls cert");
400}
401
402static void
403  vl_api_application_tls_key_add_reply_t_handler
404  (vl_api_application_tls_key_add_reply_t * mp)
405{
406  if (mp->retval)
407    ECHO_FAIL (ECHO_FAIL_VL_API_TLS_KEY_ADD_REPLY,
408	       "failed to add application tls key");
409}
410
411static void
412vl_api_connect_uri_reply_t_handler (vl_api_connect_uri_reply_t * mp)
413{
414  echo_main_t *em = &echo_main;
415  if (mp->retval && (em->proto_cb_vft->connected_cb))
416    em->proto_cb_vft->connected_cb ((session_connected_bundled_msg_t *) mp,
417				    mp->context, 1 /* is_failed */ );
418}
419
420#define foreach_quic_echo_msg                                           \
421_(BIND_URI_REPLY, bind_uri_reply)                                       \
422_(UNBIND_URI_REPLY, unbind_uri_reply)                                   \
423_(DISCONNECT_SESSION_REPLY, disconnect_session_reply)                   \
424_(APPLICATION_ATTACH_REPLY, application_attach_reply)                   \
425_(APPLICATION_DETACH_REPLY, application_detach_reply)                   \
426_(MAP_ANOTHER_SEGMENT, map_another_segment)                             \
427_(UNMAP_SEGMENT, unmap_segment)                                         \
428_(APPLICATION_TLS_CERT_ADD_REPLY, application_tls_cert_add_reply)       \
429_(APPLICATION_TLS_KEY_ADD_REPLY, application_tls_key_add_reply)         \
430_(CONNECT_URI_REPLY, connect_uri_reply)         \
431
432void
433echo_api_hookup (echo_main_t * em)
434{
435#define _(N,n)                                                  \
436    vl_msg_api_set_handlers(VL_API_##N, #n,                     \
437                           vl_api_##n##_t_handler,              \
438                           vl_noop_handler,                     \
439                           vl_api_##n##_t_endian,               \
440                           vl_api_##n##_t_print,                \
441                           sizeof(vl_api_##n##_t), 1);
442  foreach_quic_echo_msg;
443#undef _
444}
445
446/*
447 * fd.io coding-style-patch-verification: ON
448 *
449 * Local Variables:
450 * eval: (c-set-style "gnu")
451 * End:
452 */
453