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_app_attach_t *bmp;
31 bmp = vl_msg_api_alloc (sizeof (*bmp));
32 clib_memset (bmp, 0, sizeof (*bmp));
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;
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;
51 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & bmp);
55 echo_send_detach (echo_main_t * em)
57 vl_api_application_detach_t *bmp;
58 bmp = vl_msg_api_alloc (sizeof (*bmp));
59 clib_memset (bmp, 0, sizeof (*bmp));
61 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
62 bmp->client_index = em->my_client_index;
63 bmp->context = ntohl (0xfeedface);
65 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & bmp);
69 echo_send_add_cert_key (echo_main_t * em)
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;
75 bmp = vl_msg_api_alloc (sizeof (*bmp) + cert_len + key_len);
76 clib_memset (bmp, 0, sizeof (*bmp) + cert_len + key_len);
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);
86 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & bmp);
90 echo_send_del_cert_key (echo_main_t * em)
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));
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);
104 echo_send_listen (echo_main_t * em, ip46_address_t * ip)
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;
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);
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);
126 echo_send_unbind (echo_main_t * em, echo_session_t * s)
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;
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;
137 mp->handle = s->vpp_session_handle;
139 app_send_ctrl_evt_to_vpp (mq, app_evt);
143 echo_send_connect (echo_main_t * em, void *args)
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;
150 clib_atomic_sub_fetch (&em->max_sim_connects, 1);
151 while (em->max_sim_connects <= 0)
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);
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);
173 echo_send_disconnect_session (echo_main_t * em, void *args)
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;
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);
188 if (!(s = echo_get_session_from_handle (em, mp->handle)))
190 em->proto_cb_vft->sent_disconnect_cb (s);
200 echo_segment_attach (u64 segment_handle, char *name, ssvm_segment_type_t type,
203 fifo_segment_create_args_t _a, *a = &_a;
204 echo_main_t *em = &echo_main;
205 fifo_segment_main_t *sm;
208 clib_memset (a, 0, sizeof (*a));
209 a->segment_name = (char *) name;
210 a->segment_type = type;
212 if (type == SSVM_SEGMENT_MEMFD)
215 sm = &em->segment_main;
217 if ((rv = fifo_segment_attach (sm, a)))
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);
225 vec_free (a->new_segment_indices);
230 echo_segment_lookup (u64 segment_handle)
232 echo_main_t *em = &echo_main;
235 ECHO_LOG (3, "Check if segment mapped 0x%lx...", segment_handle);
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);
243 ECHO_LOG (2, "Segment not mapped (0x%lx)", segment_handle);
244 return ((u32) *segment_idxp);
248 echo_segment_detach (u64 segment_handle)
250 echo_main_t *em = &echo_main;
251 fifo_segment_main_t *sm;
253 u32 segment_index = echo_segment_lookup (segment_handle);
254 if (segment_index == (u32) ~0)
257 sm = &em->segment_main;
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);
266 echo_attach_session (uword segment_handle, uword rxf_offset, uword txf_offset,
267 uword mq_offset, echo_session_t *s)
269 echo_main_t *em = &echo_main;
270 u32 fs_index, eqs_index;
273 fs_index = echo_segment_lookup (segment_handle);
274 if (fs_index == (u32) ~0)
276 ECHO_LOG (0, "ERROR: segment for session %u is not mounted!",
281 if (mq_offset != (uword) ~0)
283 eqs_index = echo_segment_lookup (ECHO_MQ_SEG_HANDLE);
284 ASSERT (eqs_index != (u32) ~0);
287 clib_spinlock_lock (&em->segment_handles_lock);
289 fs = fifo_segment_get_segment (&em->segment_main, fs_index);
290 s->rx_fifo = fifo_segment_alloc_fifo_w_offset (fs, rxf_offset);
291 s->tx_fifo = fifo_segment_alloc_fifo_w_offset (fs, txf_offset);
292 s->rx_fifo->segment_index = fs_index;
293 s->tx_fifo->segment_index = fs_index;
294 s->rx_fifo->shr->client_session_index = s->session_index;
295 s->tx_fifo->shr->client_session_index = s->session_index;
297 if (mq_offset != (uword) ~0)
299 fs = fifo_segment_get_segment (&em->segment_main, eqs_index);
300 s->vpp_evt_q = fifo_segment_msg_q_attach (fs, mq_offset,
301 s->rx_fifo->shr->slice_index);
304 clib_spinlock_unlock (&em->segment_handles_lock);
310 echo_segment_attach_mq (uword segment_handle, uword mq_offset, u32 mq_index,
313 echo_main_t *em = &echo_main;
317 fs_index = echo_segment_lookup (segment_handle);
318 if (fs_index == (u32) ~0)
320 ECHO_LOG (0, "ERROR: mq segment %lx for is not attached!",
325 clib_spinlock_lock (&em->segment_handles_lock);
327 fs = fifo_segment_get_segment (&em->segment_main, fs_index);
328 *mq = fifo_segment_msg_q_attach (fs, mq_offset, mq_index);
330 clib_spinlock_unlock (&em->segment_handles_lock);
337 * Binary API callbacks
342 vl_api_app_add_cert_key_pair_reply_t_handler
343 (vl_api_app_add_cert_key_pair_reply_t * mp)
345 echo_main_t *em = &echo_main;
348 ECHO_FAIL (ECHO_FAIL_VL_API_CERT_KEY_ADD_REPLY,
349 "Adding cert and key returned %d",
350 clib_net_to_host_u32 (mp->retval));
353 /* No concurrency here, only bapi thread writes */
354 if (em->state != STATE_ATTACHED_NO_CERT)
356 ECHO_FAIL (ECHO_FAIL_VL_API_CERT_KEY_ADD_REPLY, "Wrong state");
359 em->ckpair_index = clib_net_to_host_u32 (mp->index);
360 em->state = STATE_ATTACHED;
364 vl_api_app_del_cert_key_pair_reply_t_handler
365 (vl_api_app_del_cert_key_pair_reply_t * mp)
367 echo_main_t *em = &echo_main;
370 ECHO_FAIL (ECHO_FAIL_VL_API_CERT_KEY_DEL_REPLY,
371 "Delete cert and key returned %d",
372 clib_net_to_host_u32 (mp->retval));
375 em->state = STATE_CLEANED_CERT_KEY;
379 vl_api_app_attach_reply_t_handler (vl_api_app_attach_reply_t * mp)
381 echo_main_t *em = &echo_main;
385 char *segment_name = 0;
387 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
388 ECHO_LOG (2, "Attached returned app %u", htons (mp->app_index));
392 ECHO_FAIL (ECHO_FAIL_VL_API_APP_ATTACH, "attach failed: %U",
393 format_api_error, clib_net_to_host_u32 (mp->retval));
399 ECHO_FAIL (ECHO_FAIL_VL_API_NULL_APP_MQ, "NULL app_mq");
405 vec_validate (fds, mp->n_fds);
406 if (vl_socket_client_recv_fd_msg (fds, mp->n_fds, 5))
408 ECHO_FAIL (ECHO_FAIL_VL_API_RECV_FD_MSG,
409 "vl_socket_client_recv_fd_msg failed");
413 if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
414 if (echo_segment_attach (ECHO_MQ_SEG_HANDLE, 0, SSVM_SEGMENT_MEMFD,
417 ECHO_FAIL (ECHO_FAIL_VL_API_SVM_FIFO_SEG_ATTACH,
418 "svm_fifo_segment_attach failed on SSVM_SEGMENT_MEMFD");
421 echo_segment_attach_mq (ECHO_MQ_SEG_HANDLE, mp->vpp_ctrl_mq,
422 mp->vpp_ctrl_mq_thread, &em->ctrl_mq);
424 if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
426 segment_name = vl_api_from_api_to_new_c_string (&mp->segment_name);
427 rv = echo_segment_attach (segment_handle, segment_name,
428 SSVM_SEGMENT_MEMFD, fds[n_fds++]);
431 ECHO_FAIL (ECHO_FAIL_VL_API_SVM_FIFO_SEG_ATTACH,
432 "svm_fifo_segment_attach ('%s') "
433 "failed on SSVM_SEGMENT_MEMFD", segment_name);
434 vec_free (segment_name);
437 vec_free (segment_name);
439 echo_segment_attach_mq (segment_handle, mp->app_mq, 0, &em->app_mq);
441 if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
442 svm_msg_q_set_eventfd (em->app_mq, fds[n_fds++]);
448 segment_name = vl_api_from_api_to_new_c_string (&mp->segment_name);
449 rv = echo_segment_attach (segment_handle, segment_name, SSVM_SEGMENT_SHM,
453 ECHO_FAIL (ECHO_FAIL_VL_API_SVM_FIFO_SEG_ATTACH,
454 "svm_fifo_segment_attach ('%s') "
455 "failed on SSVM_SEGMENT_SHM", segment_name);
456 vec_free (segment_name);
459 vec_free (segment_name);
461 ECHO_LOG (2, "Mapped segment 0x%lx", segment_handle);
463 em->state = STATE_ATTACHED_NO_CERT;
466 for (i = clib_max (n_fds - 1, 0); i < vec_len (fds); i++)
472 vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
477 ECHO_FAIL (ECHO_FAIL_VL_API_DETACH_REPLY,
478 "app detach returned with err: %d", mp->retval);
481 echo_main.state = STATE_DETACHED;
484 #define foreach_quic_echo_msg \
485 _(APP_ATTACH_REPLY, app_attach_reply) \
486 _(APPLICATION_DETACH_REPLY, application_detach_reply) \
487 _(APP_ADD_CERT_KEY_PAIR_REPLY, app_add_cert_key_pair_reply) \
488 _(APP_DEL_CERT_KEY_PAIR_REPLY, app_del_cert_key_pair_reply)
491 echo_api_hookup (echo_main_t * em)
494 vl_msg_api_set_handlers(VL_API_##N, #n, \
495 vl_api_##n##_t_handler, \
497 vl_api_##n##_t_endian, \
498 vl_api_##n##_t_print, \
499 sizeof(vl_api_##n##_t), 1);
500 foreach_quic_echo_msg;
505 * fd.io coding-style-patch-verification: ON
508 * eval: (c-set-style "gnu")