hsa: Add ckpair & crypto engine in vpp_echo
[vpp.git] / src / plugins / hs_apps / sapi / vpp_echo_common.h
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 #ifndef __included_vpp_echo_common_h__
17 #define __included_vpp_echo_common_h__
18
19 #include <vnet/session/application_interface.h>
20 #include <vpp/api/vpe_msg_enum.h>
21
22 #define vl_typedefs             /* define message structures */
23 #include <vpp/api/vpe_all_api_h.h>
24 #undef vl_typedefs
25
26 /* declare message handlers for each api */
27
28 #define vl_endianfun            /* define message structures */
29 #include <vpp/api/vpe_all_api_h.h>
30 #undef vl_endianfun
31
32 /* instantiate all the print functions we know about */
33 #define vl_print(handle, ...)
34 #define vl_printfun
35 #include <vpp/api/vpe_all_api_h.h>
36 #undef vl_printfun
37
38 #define TIMEOUT 10.0
39
40 #define foreach_echo_fail_code                                          \
41   _(ECHO_FAIL_NONE, "ECHO_FAIL_NONE")                                   \
42   _(ECHO_FAIL_USAGE, "ECHO_FAIL_USAGE")                                 \
43   _(ECHO_FAIL_SEND_IO_EVT, "ECHO_FAIL_SEND_IO_EVT")                     \
44   _(ECHO_FAIL_SOCKET_CONNECT, "ECHO_FAIL_SOCKET_CONNECT")               \
45   _(ECHO_FAIL_INIT_SHM_API, "ECHO_FAIL_INIT_SHM_API")                   \
46   _(ECHO_FAIL_SHMEM_CONNECT, "ECHO_FAIL_SHMEM_CONNECT")                 \
47   _(ECHO_FAIL_TEST_BYTES_ERR, "ECHO_FAIL_TEST_BYTES_ERR")               \
48   _(ECHO_FAIL_BIND, "ECHO_FAIL_BIND")                                   \
49   _(ECHO_FAIL_SESSION_ACCEPTED_BAD_LISTENER,                            \
50     "ECHO_FAIL_SESSION_ACCEPTED_BAD_LISTENER")                          \
51   _(ECHO_FAIL_ACCEPTED_WAIT_FOR_SEG_ALLOC,                              \
52     "ECHO_FAIL_ACCEPTED_WAIT_FOR_SEG_ALLOC")                            \
53   _(ECHO_FAIL_SESSION_CONNECT, "ECHO_FAIL_SESSION_CONNECT")             \
54   _(ECHO_FAIL_CONNECTED_WAIT_FOR_SEG_ALLOC,                             \
55     "ECHO_FAIL_CONNECTED_WAIT_FOR_SEG_ALLOC")                           \
56   _(ECHO_FAIL_APP_ATTACH, "ECHO_FAIL_APP_ATTACH")                       \
57   _(ECHO_FAIL_SERVER_DISCONNECT_TIMEOUT,                                \
58     "ECHO_FAIL_SERVER_DISCONNECT_TIMEOUT")                              \
59   _(ECHO_FAIL_INVALID_URI, "ECHO_FAIL_INVALID_URI")                     \
60   _(ECHO_FAIL_PROTOCOL_NOT_SUPPORTED,                                   \
61     "ECHO_FAIL_PROTOCOL_NOT_SUPPORTED")                                 \
62   _(ECHO_FAIL_CONNECT_TO_VPP, "ECHO_FAIL_CONNECT_TO_VPP")               \
63   _(ECHO_FAIL_ATTACH_TO_VPP, "ECHO_FAIL_ATTACH_TO_VPP")                 \
64   _(ECHO_FAIL_1ST_PTHREAD_CREATE, "ECHO_FAIL_1ST_PTHREAD_CREATE")       \
65   _(ECHO_FAIL_PTHREAD_CREATE, "ECHO_FAIL_PTHREAD_CREATE")               \
66   _(ECHO_FAIL_DETACH, "ECHO_FAIL_DETACH")                               \
67   _(ECHO_FAIL_DEL_CERT_KEY, "ECHO_FAIL_DEL_CERT_KEY")                               \
68   _(ECHO_FAIL_MQ_PTHREAD, "ECHO_FAIL_MQ_PTHREAD")                       \
69   _(ECHO_FAIL_VL_API_APP_ATTACH, "ECHO_FAIL_VL_API_APP_ATTACH")         \
70   _(ECHO_FAIL_VL_API_MISSING_SEGMENT_NAME,                              \
71     "ECHO_FAIL_VL_API_MISSING_SEGMENT_NAME")                            \
72   _(ECHO_FAIL_VL_API_NULL_APP_MQ, "ECHO_FAIL_VL_API_NULL_APP_MQ")       \
73   _(ECHO_FAIL_VL_API_RECV_FD_MSG, "ECHO_FAIL_VL_API_RECV_FD_MSG")       \
74   _(ECHO_FAIL_VL_API_SVM_FIFO_SEG_ATTACH,                               \
75     "ECHO_FAIL_VL_API_SVM_FIFO_SEG_ATTACH")                             \
76   _(ECHO_FAIL_VL_API_FIFO_SEG_ATTACH,                                   \
77     "ECHO_FAIL_VL_API_FIFO_SEG_ATTACH")                                 \
78   _(ECHO_FAIL_VL_API_DETACH_REPLY, "ECHO_FAIL_VL_API_DETACH_REPLY")     \
79   _(ECHO_FAIL_VL_API_BIND_URI_REPLY, "ECHO_FAIL_VL_API_BIND_URI_REPLY") \
80   _(ECHO_FAIL_VL_API_UNBIND_REPLY, "ECHO_FAIL_VL_API_UNBIND_REPLY")     \
81   _(ECHO_FAIL_SESSION_DISCONNECT, "ECHO_FAIL_SESSION_DISCONNECT")       \
82   _(ECHO_FAIL_SESSION_RESET, "ECHO_FAIL_SESSION_RESET")                 \
83   _(ECHO_FAIL_VL_API_CERT_KEY_ADD_REPLY,                                \
84     "ECHO_FAIL_VL_API_CERT_KEY_ADD_REPLY")                              \
85   _(ECHO_FAIL_VL_API_CERT_KEY_DEL_REPLY,                                \
86     "ECHO_FAIL_VL_API_CERT_KEY_DEL_REPLY")                              \
87   _(ECHO_FAIL_GET_SESSION_FROM_HANDLE,                                  \
88     "ECHO_FAIL_GET_SESSION_FROM_HANDLE")                                \
89   _(ECHO_FAIL_QUIC_WRONG_CONNECT, "ECHO_FAIL_QUIC_WRONG_CONNECT")       \
90   _(ECHO_FAIL_QUIC_WRONG_ACCEPT, "ECHO_FAIL_QUIC_WRONG_ACCEPT")         \
91   _(ECHO_FAIL_TCP_BAPI_CONNECT, "ECHO_FAIL_TCP_BAPI_CONNECT")           \
92   _(ECHO_FAIL_UDP_BAPI_CONNECT, "ECHO_FAIL_UDP_BAPI_CONNECT")           \
93   _(ECHO_FAIL_MISSING_START_EVENT, "ECHO_FAIL_MISSING_START_EVENT")     \
94   _(ECHO_FAIL_MISSING_END_EVENT, "ECHO_FAIL_MISSING_END_EVENT")         \
95   _(ECHO_FAIL_TEST_ASSERT_RX_TOTAL, "ECHO_FAIL_TEST_ASSERT_RX_TOTAL")   \
96   _(ECHO_FAIL_TEST_ASSERT_TX_TOTAL, "ECHO_FAIL_TEST_ASSERT_TX_TOTAL")   \
97   _(ECHO_FAIL_TEST_ASSERT_ALL_SESSIONS_CLOSED,                          \
98     "ECHO_FAIL_TEST_ASSERT_ALL_SESSIONS_CLOSED")
99
100 typedef enum
101 {
102 #define _(sym, str) sym,
103   foreach_echo_fail_code
104 #undef _
105 } echo_fail_t;
106
107 extern char *echo_fail_code_str[];
108
109 #define CHECK_SAME(fail, expected, result, _fmt, _args...)      \
110 do {                                                            \
111   if ((expected) != (result))                                   \
112     ECHO_FAIL ((fail), "expected same (%lld, got %lld) : "_fmt, \
113                (u64)(expected), (u64)(result), ##_args);        \
114 } while (0)
115
116 #define CHECK_DIFF(fail, expected, result, _fmt, _args...)      \
117 do {                                                            \
118   if ((expected) == (result))                                   \
119     ECHO_FAIL ((fail), "expected different (both %lld) : "_fmt, \
120                (u64)(expected), ##_args);                       \
121 } while (0)
122
123 #define ECHO_FAIL(fail, _fmt, _args...)                                 \
124 do {                                                                    \
125     echo_main_t *em = &echo_main;                                       \
126     em->has_failed = (fail);                                            \
127     if (vec_len(em->fail_descr))                                        \
128       em->fail_descr = format(em->fail_descr, " | %s (%u): "_fmt,       \
129                               echo_fail_code_str[fail], fail, ##_args); \
130     else                                                                \
131       em->fail_descr = format(0, "%s (%u): "_fmt,                       \
132                               echo_fail_code_str[fail], fail, ##_args); \
133     em->time_to_stop = 1;                                               \
134     if (em->log_lvl > 0)                                                \
135       clib_warning ("%v", em->fail_descr);                              \
136 } while (0)
137
138 #define ECHO_LOG(lvl, _fmt,_args...)    \
139   {                                     \
140     echo_main_t *em = &echo_main;       \
141     if (em->log_lvl > lvl)              \
142          clib_warning (_fmt, ##_args);  \
143   }
144
145 #define ECHO_REGISTER_PROTO(proto, vft)         \
146   static void __clib_constructor                \
147   vpp_echo_init_##proto ()                      \
148   {                                             \
149     echo_main_t *em = &echo_main;               \
150     em->available_proto_cb_vft[proto] = &vft;   \
151   }
152
153 typedef struct
154 {
155   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
156 #define _(type, name) type name;
157   foreach_app_session_field
158 #undef _
159   u64 vpp_session_handle;
160   u64 bytes_sent;
161   u64 bytes_to_send;
162   volatile u64 bytes_received;
163   volatile u64 bytes_to_receive;
164   f64 start;
165   u32 listener_index;           /* listener index in echo session pool */
166   u32 idle_cycles;              /* consecutive enq/deq with no data */
167   volatile u64 accepted_session_count;  /* sessions we accepted (as a listener) */
168 } echo_session_t;
169
170 typedef enum
171 {
172   ECHO_NO_DATA_SOURCE,
173   ECHO_TEST_DATA_SOURCE,
174   ECHO_RX_DATA_SOURCE,
175   ECHO_INVALID_DATA_SOURCE
176 } data_source_t;
177
178 enum echo_close_f_t
179 {
180   ECHO_CLOSE_F_INVALID = 0,
181   ECHO_CLOSE_F_PASSIVE,         /* wait for close msg */
182   ECHO_CLOSE_F_ACTIVE,          /* send close msg */
183   ECHO_CLOSE_F_NONE,            /* don't bother sending close msg */
184 };
185
186 enum quic_session_type_t
187 {
188   ECHO_SESSION_TYPE_QUIC,
189   ECHO_SESSION_TYPE_STREAM,
190   ECHO_SESSION_TYPE_LISTEN,
191 };
192
193 enum quic_session_state_t
194 {
195   ECHO_SESSION_STATE_INITIAL,
196   ECHO_SESSION_STATE_READY,
197   ECHO_SESSION_STATE_AWAIT_CLOSING,     /* Data transfer is done, wait for close evt */
198   ECHO_SESSION_STATE_AWAIT_DATA,        /* Peer closed, wait for outstanding data */
199   ECHO_SESSION_STATE_CLOSING,   /* told vpp to close */
200   ECHO_SESSION_STATE_CLOSED,    /* closed in vpp */
201 };
202
203 typedef enum
204 {
205   STATE_START,
206   STATE_ATTACHED_NO_CERT,
207   STATE_ATTACHED,
208   STATE_LISTEN,
209   STATE_READY,
210   STATE_DATA_DONE,
211   STATE_DISCONNECTED,
212   STATE_CLEANED_CERT_KEY,
213   STATE_DETACHED
214 } connection_state_t;
215
216 typedef enum echo_test_evt_
217 {
218   ECHO_EVT_START = 1,           /* app starts */
219   ECHO_EVT_FIRST_QCONNECT = (1 << 1),   /* First connect Quic session sent */
220   ECHO_EVT_LAST_QCONNECTED = (1 << 2),  /* All Quic session are connected */
221   ECHO_EVT_FIRST_SCONNECT = (1 << 3),   /* First connect Stream session sent */
222   ECHO_EVT_LAST_SCONNECTED = (1 << 4),  /* All Stream session are connected */
223   ECHO_EVT_LAST_BYTE = (1 << 5),        /* Last byte received */
224   ECHO_EVT_EXIT = (1 << 6),     /* app exits */
225 } echo_test_evt_t;
226
227 typedef union session_connected_bundled_msg_
228 {
229   session_connected_msg_t *mp;
230   vl_api_connect_uri_reply_t *bmp;
231 } session_connected_bundled_msg_t;
232
233 typedef struct echo_proto_cb_vft_
234 {
235   void (*connected_cb) (session_connected_bundled_msg_t * mp, u32 session_index, u8 is_failed); /* Session is connected */
236   void (*accepted_cb) (session_accepted_msg_t * mp, echo_session_t * session);  /* Session got accepted */
237   void (*bound_uri_cb) (session_bound_msg_t * mp, echo_session_t * session);    /* Session got bound */
238   void (*reset_cb) (session_reset_msg_t * mp, echo_session_t * s);      /* Received RESET on session */
239   void (*disconnected_cb) (session_disconnected_msg_t * mp, echo_session_t * s);        /* Received DISCONNECT on session */
240   void (*sent_disconnect_cb) (echo_session_t * s);      /* ACK disconnect we sent to vpp */
241   void (*cleanup_cb) (echo_session_t * s, u8 parent_died);      /* Session should be cleaned up (parent listener may be dead) */
242   /* Add CLI options */
243   int (*process_opts_cb) (unformat_input_t * a);
244   void (*set_defaults_before_opts_cb) (void);
245   void (*set_defaults_after_opts_cb) (void);
246   void (*print_usage_cb) (void);
247 } echo_proto_cb_vft_t;
248
249 typedef enum
250 {
251   RETURN_PACKETS_NOTEST,
252   RETURN_PACKETS_LOG_WRONG,
253   RETURN_PACKETS_ASSERT,
254 } test_return_packets_t;
255
256 typedef struct teardown_stat_
257 {
258   u32 q;                        /* quic sessions */
259   u32 s;                        /* stream sessions */
260 } teardown_stat_t;
261
262 typedef struct
263 {
264   svm_queue_t *vl_input_queue;  /* vpe input queue */
265   u32 my_client_index;          /* API client handle */
266   u8 *uri;                      /* The URI we're playing with */
267   echo_session_t *sessions;     /* Session pool */
268   svm_msg_q_t *app_mq;          /* Our receiveing event queue */
269   svm_msg_q_t *ctrl_mq;         /* Our control queue (towards vpp) */
270   clib_time_t clib_time;        /* For deadman timers */
271   u8 *socket_name;
272   int i_am_master;
273   u32 listen_session_index;     /* Index of vpp listener session */
274
275   uword *session_index_by_vpp_handles;  /* Hash table : quic_echo s_id -> vpp s_handle */
276   clib_spinlock_t sid_vpp_handles_lock; /* Hash table lock */
277
278   uword *shared_segment_handles;        /* Hash table : segment_names -> 1 */
279   clib_spinlock_t segment_handles_lock; /* Hash table lock */
280   echo_proto_cb_vft_t *proto_cb_vft;
281   svm_msg_q_t *rpc_msq_queue;   /* MQ between quic_echo threads */
282   fifo_segment_main_t segment_main;
283
284   /* State of the connection, shared between msg RX thread and main thread */
285   volatile connection_state_t state;
286   volatile u8 time_to_stop;     /* Signal variables */
287   u8 rx_results_diff;           /* Rx results will be different than cfg */
288   u8 tx_results_diff;           /* Tx results will be different than cfg */
289   u8 has_failed;                /* stores the exit code */
290   u8 *fail_descr;               /* vector containing fail description */
291
292   /** Flag that decides if socket, instead of svm, api is used to connect to
293    * vpp. If sock api is used, shm binary api is subsequently bootstrapped
294    * and all other messages are exchanged using shm IPC. */
295   u8 use_sock_api;
296
297   u8 *connect_test_data;
298   u8 test_return_packets;
299   u64 bytes_to_send;            /* target per stream */
300   u64 bytes_to_receive;         /* target per stream */
301   u32 fifo_size;
302   u32 prealloc_fifo_pairs;
303   u64 rx_buf_size;
304   u64 tx_buf_size;
305   data_source_t data_source;    /* Use no/dummy/mirrored data */
306   u8 send_stream_disconnects;   /* actively send disconnect */
307   u8 output_json;               /* Output stats as JSON */
308   volatile u8 wait_for_gdb;     /* Wait for gdb to attach */
309   u8 log_lvl;                   /* Verbosity of the logging */
310   int max_test_msg;             /* Limit the number of incorrect data messages */
311   u32 evt_q_size;               /* Size of the vpp MQ (app<->vpp events) */
312   u32 ckpair_index;             /* Cert key pair used */
313   u8 crypto_engine;             /* crypto engine used */
314
315   u8 *appns_id;
316   u64 appns_flags;
317   u64 appns_secret;
318
319   pthread_t *data_thread_handles;       /* vec of data thread handles */
320   pthread_t mq_thread_handle;   /* Message queue thread handle */
321   u32 *volatile data_thread_args;
322
323   u32 n_connects;               /* Target number of connects to send */
324   u32 n_sessions;               /* Number of sessions to prealloc */
325   u32 n_clients;                /* Target number of clients doing RX/TX */
326   u32 n_rx_threads;             /* Number of data threads */
327
328   volatile u32 n_clients_connected;     /* Number of STREAM sessions connected */
329   volatile u32 nxt_available_sidx;      /* next unused prealloced session_index */
330
331   /* VNET_API_ERROR_FOO -> "Foo" hash table */
332   uword *error_string_by_error_number;
333   echo_proto_cb_vft_t *available_proto_cb_vft[TRANSPORT_N_PROTO];
334
335   struct
336   {
337     u64 tx_total;
338     u64 rx_total;
339     u64 tx_expected;
340     u64 rx_expected;
341     teardown_stat_t reset_count;        /* received reset from vpp */
342     teardown_stat_t close_count;        /* received close from vpp */
343     teardown_stat_t active_count;       /* sent close to vpp */
344     teardown_stat_t clean_count;        /* cleaned up stale session */
345   } stats;
346
347   struct                        /* Event based timing : start & end depend on CLI specified events */
348   {
349     f64 start_time;
350     f64 end_time;
351     u8 events_sent;
352     u8 start_event;
353     u8 end_event;
354   } timing;
355
356   struct
357   {
358     u32 transport_proto;
359     ip46_address_t ip;
360     u32 port;
361     u8 is_ip4;
362   } uri_elts;
363 } echo_main_t;
364
365 extern echo_main_t echo_main;
366
367 typedef void (*echo_rpc_t) (void *arg, u32 opaque);
368
369 typedef struct
370 {
371   void *fp;
372   void *arg;
373   u32 opaque;
374 } echo_rpc_msg_t;
375
376 u8 *format_ip4_address (u8 * s, va_list * args);
377 u8 *format_ip6_address (u8 * s, va_list * args);
378 u8 *format_ip46_address (u8 * s, va_list * args);
379 u8 *format_api_error (u8 * s, va_list * args);
380 void init_error_string_table ();
381 u8 *echo_format_session (u8 * s, va_list * args);
382 u8 *echo_format_session_type (u8 * s, va_list * args);
383 u8 *echo_format_session_state (u8 * s, va_list * args);
384 u8 *echo_format_app_state (u8 * s, va_list * args);
385 uword echo_unformat_close (unformat_input_t * input, va_list * args);
386 uword echo_unformat_timing_event (unformat_input_t * input, va_list * args);
387 u8 *echo_format_timing_event (u8 * s, va_list * args);
388 uword unformat_transport_proto (unformat_input_t * input, va_list * args);
389 u8 *format_transport_proto (u8 * s, va_list * args);
390 uword unformat_ip4_address (unformat_input_t * input, va_list * args);
391 uword unformat_ip6_address (unformat_input_t * input, va_list * args);
392
393 void echo_session_handle_add_del (echo_main_t * em, u64 handle, u32 sid);
394 echo_session_t *echo_session_new (echo_main_t * em);
395 int echo_send_rpc (echo_main_t * em, void *fp, void *arg, u32 opaque);
396 echo_session_t *echo_get_session_from_handle (echo_main_t * em, u64 handle);
397 int wait_for_segment_allocation (u64 segment_handle);
398 int wait_for_state_change (echo_main_t * em, connection_state_t state,
399                            f64 timeout);
400 void echo_notify_event (echo_main_t * em, echo_test_evt_t e);
401 void echo_session_print_stats (echo_main_t * em, echo_session_t * session);
402 u8 *echo_format_crypto_engine (u8 * s, va_list * args);
403 uword echo_unformat_crypto_engine (unformat_input_t * input, va_list * args);
404
405 /* Binary API */
406
407 void echo_send_attach (echo_main_t * em);
408 void echo_send_detach (echo_main_t * em);
409 void echo_send_listen (echo_main_t * em);
410 void echo_send_unbind (echo_main_t * em, echo_session_t * s);
411 void echo_send_connect (u64 vpp_session_handle, u32 opaque);
412 void echo_send_disconnect_session (u64 handle, u32 opaque);
413 void echo_api_hookup (echo_main_t * em);
414 void echo_send_add_cert_key (echo_main_t * em);
415 void echo_send_del_cert_key (echo_main_t * em);
416
417 #endif /* __included_vpp_echo_common_h__ */
418
419 /*
420  * fd.io coding-style-patch-verification: ON
421  *
422  * Local Variables:
423  * eval: (c-set-style "gnu")
424  * End:
425  */