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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <hs_apps/sapi/vpp_echo_common.h>
28 echo_send_attach (echo_main_t * em)
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;
34 bmp = vl_msg_api_alloc (sizeof (*bmp));
35 clib_memset (bmp, 0, sizeof (*bmp));
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;
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;
56 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & bmp);
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);
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);
78 echo_send_detach (echo_main_t * em)
80 vl_api_application_detach_t *bmp;
81 bmp = vl_msg_api_alloc (sizeof (*bmp));
82 clib_memset (bmp, 0, sizeof (*bmp));
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);
91 echo_send_listen (echo_main_t * em)
93 vl_api_bind_uri_t *bmp;
94 bmp = vl_msg_api_alloc (sizeof (*bmp));
95 clib_memset (bmp, 0, sizeof (*bmp));
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);
105 echo_send_unbind (echo_main_t * em)
107 vl_api_unbind_uri_t *ump;
109 ump = vl_msg_api_alloc (sizeof (*ump));
110 clib_memset (ump, 0, sizeof (*ump));
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);
119 echo_send_connect (u8 * uri, u32 opaque)
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);
133 echo_send_disconnect_session (u64 handle, u32 opaque)
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);
153 ssvm_segment_attach (char *name, ssvm_segment_type_t type, int fd)
155 fifo_segment_create_args_t _a, *a = &_a;
156 fifo_segment_main_t *sm = &echo_main.segment_main;
159 clib_memset (a, 0, sizeof (*a));
160 a->segment_name = (char *) name;
161 a->segment_type = type;
163 if (type == SSVM_SEGMENT_MEMFD)
166 if ((rv = fifo_segment_attach (sm, a)))
168 vec_reset_length (a->new_segment_indices);
173 echo_segment_handle_add_del (echo_main_t * em, u64 segment_handle, u8 add)
175 clib_spinlock_lock (&em->segment_handles_lock);
177 hash_set (em->shared_segment_handles, segment_handle, 1);
179 hash_unset (em->shared_segment_handles, segment_handle);
180 clib_spinlock_unlock (&em->segment_handles_lock);
185 * Binary API callbacks
190 vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
193 echo_main_t *em = &echo_main;
197 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
198 ECHO_LOG (1, "Attached returned app %u", htons (mp->app_index));
202 ECHO_FAIL (ECHO_FAIL_VL_API_APP_ATTACH, "attach failed: %U",
203 format_api_error, clib_net_to_host_u32 (mp->retval));
207 if (mp->segment_name_length == 0)
209 ECHO_FAIL (ECHO_FAIL_VL_API_MISSING_SEGMENT_NAME,
210 "segment_name_length zero");
214 ASSERT (mp->app_event_queue_address);
215 em->our_event_queue = uword_to_pointer (mp->app_event_queue_address,
220 vec_validate (fds, mp->n_fds);
221 if (vl_socket_client_recv_fd_msg (fds, mp->n_fds, 5))
223 ECHO_FAIL (ECHO_FAIL_VL_API_RECV_FD_MSG,
224 "vl_socket_client_recv_fd_msg failed");
228 if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
229 if (ssvm_segment_attach (0, SSVM_SEGMENT_MEMFD, fds[n_fds++]))
231 ECHO_FAIL (ECHO_FAIL_VL_API_SVM_FIFO_SEG_ATTACH,
232 "svm_fifo_segment_attach failed on SSVM_SEGMENT_MEMFD");
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++]))
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);
245 if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
246 svm_msg_q_set_consumer_eventfd (em->our_event_queue, fds[n_fds++]);
252 if (ssvm_segment_attach ((char *) mp->segment_name, SSVM_SEGMENT_SHM,
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);
261 echo_segment_handle_add_del (em, segment_handle, 1 /* add */ );
262 ECHO_LOG (1, "Mapped segment 0x%lx", segment_handle);
264 em->state = STATE_ATTACHED;
267 for (i = clib_max (n_fds - 1, 0); i < vec_len (fds); i++)
273 vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
278 ECHO_FAIL (ECHO_FAIL_VL_API_DETACH_REPLY,
279 "app detach returned with err: %d", mp->retval);
282 echo_main.state = STATE_DETACHED;
287 vl_api_unmap_segment_t_handler (vl_api_unmap_segment_t * mp)
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);
296 vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
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;
302 char *seg_name = (char *) mp->segment_name;
303 u64 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
305 if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
307 vec_validate (fds, 1);
308 if (vl_socket_client_recv_fd_msg (fds, 1, 5))
310 ECHO_FAIL (ECHO_FAIL_VL_API_RECV_FD_MSG,
311 "vl_socket_client_recv_fd_msg failed");
315 if (ssvm_segment_attach (seg_name, SSVM_SEGMENT_MEMFD, fds[0]))
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);
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))
332 ECHO_FAIL (ECHO_FAIL_VL_API_FIFO_SEG_ATTACH,
333 "fifo_segment_attach ('%s') failed", seg_name);
337 echo_segment_handle_add_del (em, segment_handle, 1 /* add */ );
338 ECHO_LOG (1, "Mapped segment 0x%lx", segment_handle);
342 for (i = 0; i < vec_len (fds); i++)
348 vl_api_bind_uri_reply_t_handler (vl_api_bind_uri_reply_t * mp)
352 ECHO_FAIL (ECHO_FAIL_VL_API_BIND_URI_REPLY, "bind failed: %U",
353 format_api_error, clib_net_to_host_u32 (mp->retval));
358 vl_api_unbind_uri_reply_t_handler (vl_api_unbind_uri_reply_t * mp)
360 echo_session_t *listen_session;
361 echo_main_t *em = &echo_main;
364 ECHO_FAIL (ECHO_FAIL_VL_API_UNBIND_REPLY, "unbind_uri returned %d",
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;
374 vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
377 echo_main_t *em = &echo_main;
382 ECHO_FAIL (ECHO_FAIL_VL_API_DISCONNECT_SESSION_REPLY,
383 "vpp complained about disconnect: %d", ntohl (mp->retval));
387 ECHO_LOG (1, "Got disonnected reply for session 0x%lx", mp->handle);
388 if (!(s = echo_get_session_from_handle (em, mp->handle)))
390 em->proto_cb_vft->disconnected_reply_cb (s);
394 vl_api_application_tls_cert_add_reply_t_handler
395 (vl_api_application_tls_cert_add_reply_t * mp)
398 ECHO_FAIL (ECHO_FAIL_VL_API_TLS_CERT_ADD_REPLY,
399 "failed to add application tls cert");
403 vl_api_application_tls_key_add_reply_t_handler
404 (vl_api_application_tls_key_add_reply_t * mp)
407 ECHO_FAIL (ECHO_FAIL_VL_API_TLS_KEY_ADD_REPLY,
408 "failed to add application tls key");
412 vl_api_connect_uri_reply_t_handler (vl_api_connect_uri_reply_t * mp)
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 */ );
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) \
433 echo_api_hookup (echo_main_t * em)
436 vl_msg_api_set_handlers(VL_API_##N, #n, \
437 vl_api_##n##_t_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;
447 * fd.io coding-style-patch-verification: ON
450 * eval: (c-set-style "gnu")