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>
21 #define REPLY_MSG_ID_BASE msg_id_base
22 static u16 msg_id_base;
31 echo_send_attach (echo_main_t * em)
33 vl_api_app_attach_t *bmp;
34 bmp = vl_msg_api_alloc (sizeof (*bmp));
35 clib_memset (bmp, 0, sizeof (*bmp));
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;
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;
54 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & bmp);
58 echo_send_detach (echo_main_t * em)
60 vl_api_application_detach_t *bmp;
61 bmp = vl_msg_api_alloc (sizeof (*bmp));
62 clib_memset (bmp, 0, sizeof (*bmp));
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);
68 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & bmp);
72 echo_send_add_cert_key (echo_main_t * em)
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;
78 bmp = vl_msg_api_alloc (sizeof (*bmp) + cert_len + key_len);
79 clib_memset (bmp, 0, sizeof (*bmp) + cert_len + key_len);
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);
89 vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & bmp);
93 echo_send_del_cert_key (echo_main_t * em)
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));
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);
107 echo_transport_needs_crypto (transport_proto_t proto)
109 return proto == TRANSPORT_PROTO_TLS || proto == TRANSPORT_PROTO_DTLS ||
110 proto == TRANSPORT_PROTO_QUIC;
114 echo_msg_add_crypto_ext_config (echo_main_t *em, uword *offset)
116 transport_endpt_ext_cfg_t cfg;
119 c = echo_segment_alloc_chunk (ECHO_MQ_SEG_HANDLE, 0, sizeof (cfg), offset);
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);
132 echo_send_listen (echo_main_t * em, ip46_address_t * ip)
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;
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);
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);
154 echo_send_unbind (echo_main_t * em, echo_session_t * s)
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;
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;
165 mp->handle = s->vpp_session_handle;
167 app_send_ctrl_evt_to_vpp (mq, app_evt);
171 echo_send_connect (echo_main_t * em, void *args)
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;
178 clib_atomic_sub_fetch (&em->max_sim_connects, 1);
179 while (em->max_sim_connects <= 0)
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);
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);
201 echo_send_disconnect_session (echo_main_t * em, void *args)
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;
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);
216 if (!(s = echo_get_session_from_handle (em, mp->handle)))
218 em->proto_cb_vft->sent_disconnect_cb (s);
228 echo_segment_attach (u64 segment_handle, char *name, ssvm_segment_type_t type,
231 fifo_segment_create_args_t _a, *a = &_a;
232 echo_main_t *em = &echo_main;
233 fifo_segment_main_t *sm;
236 clib_memset (a, 0, sizeof (*a));
237 a->segment_name = (char *) name;
238 a->segment_type = type;
240 if (type == SSVM_SEGMENT_MEMFD)
243 sm = &em->segment_main;
245 if ((rv = fifo_segment_attach (sm, a)))
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);
253 vec_free (a->new_segment_indices);
258 echo_segment_lookup (u64 segment_handle)
260 echo_main_t *em = &echo_main;
263 ECHO_LOG (3, "Check if segment mapped 0x%lx...", segment_handle);
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);
269 return ((u32) *segment_idxp);
271 ECHO_LOG (2, "Segment not mapped (0x%lx)", segment_handle);
276 echo_segment_detach (u64 segment_handle)
278 echo_main_t *em = &echo_main;
279 fifo_segment_main_t *sm;
281 u32 segment_index = echo_segment_lookup (segment_handle);
282 if (segment_index == (u32) ~0)
285 sm = &em->segment_main;
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);
294 echo_attach_session (uword segment_handle, uword rxf_offset, uword txf_offset,
295 uword mq_offset, echo_session_t *s)
297 echo_main_t *em = &echo_main;
298 u32 fs_index, eqs_index;
301 fs_index = echo_segment_lookup (segment_handle);
302 if (fs_index == (u32) ~0)
304 ECHO_LOG (0, "ERROR: segment for session %u is not mounted!",
309 if (mq_offset != (uword) ~0)
311 eqs_index = echo_segment_lookup (ECHO_MQ_SEG_HANDLE);
312 ASSERT (eqs_index != (u32) ~0);
315 clib_spinlock_lock (&em->segment_handles_lock);
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;
325 if (mq_offset != (uword) ~0)
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);
332 clib_spinlock_unlock (&em->segment_handles_lock);
338 echo_segment_attach_mq (uword segment_handle, uword mq_offset, u32 mq_index,
341 echo_main_t *em = &echo_main;
345 fs_index = echo_segment_lookup (segment_handle);
346 if (fs_index == (u32) ~0)
348 ECHO_LOG (0, "ERROR: mq segment %lx for is not attached!",
353 clib_spinlock_lock (&em->segment_handles_lock);
355 fs = fifo_segment_get_segment (&em->segment_main, fs_index);
356 *mq = fifo_segment_msg_q_attach (fs, mq_offset, mq_index);
358 clib_spinlock_unlock (&em->segment_handles_lock);
364 echo_segment_alloc_chunk (uword segment_handle, u32 slice_index, u32 size,
367 echo_main_t *em = &echo_main;
372 fs_index = echo_segment_lookup (segment_handle);
373 if (fs_index == (u32) ~0)
375 ECHO_LOG (0, "ERROR: mq segment %lx for is not attached!",
380 clib_spinlock_lock (&em->segment_handles_lock);
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);
386 clib_spinlock_unlock (&em->segment_handles_lock);
393 * Binary API callbacks
398 vl_api_app_add_cert_key_pair_reply_t_handler
399 (vl_api_app_add_cert_key_pair_reply_t * mp)
401 echo_main_t *em = &echo_main;
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));
409 /* No concurrency here, only bapi thread writes */
410 if (em->state != STATE_ATTACHED_NO_CERT)
412 ECHO_FAIL (ECHO_FAIL_VL_API_CERT_KEY_ADD_REPLY, "Wrong state");
415 em->ckpair_index = clib_net_to_host_u32 (mp->index);
416 em->state = STATE_ATTACHED;
420 vl_api_app_del_cert_key_pair_reply_t_handler
421 (vl_api_app_del_cert_key_pair_reply_t * mp)
423 echo_main_t *em = &echo_main;
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));
431 em->state = STATE_CLEANED_CERT_KEY;
435 vl_api_app_attach_reply_t_handler (vl_api_app_attach_reply_t * mp)
437 echo_main_t *em = &echo_main;
441 char *segment_name = 0;
443 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
444 ECHO_LOG (2, "Attached returned app %u", htons (mp->app_index));
448 ECHO_FAIL (ECHO_FAIL_VL_API_APP_ATTACH, "attach failed: %U",
449 format_api_error, clib_net_to_host_u32 (mp->retval));
455 ECHO_FAIL (ECHO_FAIL_VL_API_NULL_APP_MQ, "NULL app_mq");
461 vec_validate (fds, mp->n_fds);
462 if (vl_socket_client_recv_fd_msg (fds, mp->n_fds, 5))
464 ECHO_FAIL (ECHO_FAIL_VL_API_RECV_FD_MSG,
465 "vl_socket_client_recv_fd_msg failed");
469 if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
470 if (echo_segment_attach (ECHO_MQ_SEG_HANDLE, 0, SSVM_SEGMENT_MEMFD,
473 ECHO_FAIL (ECHO_FAIL_VL_API_SVM_FIFO_SEG_ATTACH,
474 "svm_fifo_segment_attach failed on SSVM_SEGMENT_MEMFD");
477 echo_segment_attach_mq (ECHO_MQ_SEG_HANDLE, mp->vpp_ctrl_mq,
478 mp->vpp_ctrl_mq_thread, &em->ctrl_mq);
480 if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
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++]);
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);
493 vec_free (segment_name);
495 echo_segment_attach_mq (segment_handle, mp->app_mq, 0, &em->app_mq);
497 if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
498 svm_msg_q_set_eventfd (em->app_mq, fds[n_fds++]);
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,
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);
515 vec_free (segment_name);
517 ECHO_LOG (2, "Mapped segment 0x%lx", segment_handle);
519 em->state = STATE_ATTACHED_NO_CERT;
522 for (i = clib_max (n_fds - 1, 0); i < vec_len (fds); i++)
528 vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
533 ECHO_FAIL (ECHO_FAIL_VL_API_DETACH_REPLY,
534 "app detach returned with err: %d", mp->retval);
537 echo_main.state = STATE_DETACHED;
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)
546 #define vl_print(handle, ...) fformat (handle, __VA_ARGS__)
548 #include <vnet/session/session.api.h>
551 #define vl_calcsizefun
552 #include <vnet/session/session.api.h>
553 #undef vl_calcsizefun
556 #include <vnet/session/session.api.h>
559 #define vl_api_version(n, v) static u32 api_version = v;
560 #include <vnet/session/session.api.h>
561 #undef vl_api_version
564 echo_api_hookup (echo_main_t * em)
566 u8 *name = format (0, "session_%08x%c", api_version, 0);
568 REPLY_MSG_ID_BASE = vl_client_get_first_plugin_msg_id ((char *) name);
572 if (REPLY_MSG_ID_BASE == (u16) ~0)
576 vl_msg_api_set_handlers ( \
577 REPLY_MSG_ID_BASE + VL_API_##N, #n, vl_api_##n##_t_handler, \
578 vl_noop_handler, vl_api_##n##_t_endian, vl_api_##n##_t_print, \
579 sizeof (vl_api_##n##_t), 1, vl_api_##n##_t_print_json, \
580 vl_api_##n##_t_tojson, vl_api_##n##_t_fromjson, \
581 vl_api_##n##_t_calc_size);
582 foreach_quic_echo_msg;
587 * fd.io coding-style-patch-verification: ON
590 * eval: (c-set-style "gnu")