hsa: Refactor quic_echo to allow other protocols
[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 CHECK(expected, result, _fmt, _args...)         \
41     if (expected != result)                             \
42       ECHO_FAIL ("expected %d, got %d : " _fmt, expected, result, ##_args);
43
44 #define ECHO_FAIL(_fmt,_args...)        \
45   {                                     \
46     echo_main_t *em = &echo_main;       \
47     em->has_failed = 1;                 \
48     em->time_to_stop = 1;               \
49     if (em->log_lvl > 0)                \
50          clib_warning ("ECHO-ERROR: "_fmt, ##_args);    \
51   }
52
53 #define ECHO_LOG(lvl, _fmt,_args...)    \
54   {                                     \
55     echo_main_t *em = &echo_main;       \
56     if (em->log_lvl > lvl)              \
57          clib_warning (_fmt, ##_args);  \
58   }
59
60 #define ECHO_REGISTER_PROTO(proto, vft)                 \
61   static void __clib_constructor                        \
62   vpp_echo_init_##proto ()                              \
63   {                                                     \
64     echo_main_t *em = &echo_main;        \
65     em->available_proto_cb_vft[proto] = &vft;          \
66   }
67
68 typedef struct
69 {
70   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
71 #define _(type, name) type name;
72   foreach_app_session_field
73 #undef _
74   u64 vpp_session_handle;
75   u64 bytes_sent;
76   u64 bytes_to_send;
77   volatile u64 bytes_received;
78   volatile u64 bytes_to_receive;
79   f64 start;
80   u32 listener_index;           /* listener index in echo session pool */
81   u32 idle_cycles;              /* consecutive enq/deq with no data */
82   volatile u64 accepted_session_count;  /* sessions we accepted (as a listener) */
83 } echo_session_t;
84
85 typedef enum
86 {
87   ECHO_NO_DATA_SOURCE,
88   ECHO_TEST_DATA_SOURCE,
89   ECHO_RX_DATA_SOURCE,
90   ECHO_INVALID_DATA_SOURCE
91 } data_source_t;
92
93 enum echo_close_f_t
94 {
95   ECHO_CLOSE_F_INVALID = 0,
96   ECHO_CLOSE_F_PASSIVE,         /* wait for close msg */
97   ECHO_CLOSE_F_ACTIVE,          /* send close msg */
98   ECHO_CLOSE_F_NONE,            /* don't bother sending close msg */
99 };
100
101 enum quic_session_type_t
102 {
103   ECHO_SESSION_TYPE_QUIC,
104   ECHO_SESSION_TYPE_STREAM,
105   ECHO_SESSION_TYPE_LISTEN,
106 };
107
108 enum quic_session_state_t
109 {
110   ECHO_SESSION_STATE_INITIAL,
111   ECHO_SESSION_STATE_READY,
112   ECHO_SESSION_STATE_AWAIT_CLOSING,     /* Data transfer is done, wait for close evt */
113   ECHO_SESSION_STATE_AWAIT_DATA,        /* Peer closed, wait for outstanding data */
114   ECHO_SESSION_STATE_CLOSING,   /* told vpp to close */
115   ECHO_SESSION_STATE_CLOSED,    /* closed in vpp */
116 };
117
118 typedef enum
119 {
120   STATE_START,
121   STATE_ATTACHED,
122   STATE_LISTEN,
123   STATE_READY,
124   STATE_DATA_DONE,
125   STATE_DISCONNECTED,
126   STATE_DETACHED
127 } connection_state_t;
128
129 typedef enum echo_test_evt_
130 {
131   ECHO_EVT_START = 1,           /* app starts */
132   ECHO_EVT_FIRST_QCONNECT = (1 << 1),   /* First connect Quic session sent */
133   ECHO_EVT_LAST_QCONNECTED = (1 << 2),  /* All Quic session are connected */
134   ECHO_EVT_FIRST_SCONNECT = (1 << 3),   /* First connect Stream session sent */
135   ECHO_EVT_LAST_SCONNECTED = (1 << 4),  /* All Stream session are connected */
136   ECHO_EVT_LAST_BYTE = (1 << 5),        /* Last byte received */
137   ECHO_EVT_EXIT = (1 << 6),     /* app exits */
138 } echo_test_evt_t;
139
140 typedef union session_connected_bundled_msg_
141 {
142   session_connected_msg_t *mp;
143   vl_api_connect_uri_reply_t *bmp;
144 } session_connected_bundled_msg_t;
145
146 typedef struct echo_proto_cb_vft_
147 {
148   void (*connected_cb) (session_connected_bundled_msg_t * mp, u32 session_index, u8 is_failed); /* Session is connected */
149   void (*accepted_cb) (session_accepted_msg_t * mp, echo_session_t * session);  /* Session got accepted */
150   void (*bound_uri_cb) (session_bound_msg_t * mp, echo_session_t * session);    /* Session got bound */
151   void (*reset_cb) (session_reset_msg_t * mp, echo_session_t * s);      /* Received RESET on session */
152   void (*disconnected_cb) (session_disconnected_msg_t * mp, echo_session_t * s);        /* Received DISCONNECT on session */
153   void (*disconnected_reply_cb) (echo_session_t * s);   /* ACK disconnect we sent to vpp */
154   void (*cleanup_cb) (echo_session_t * s, u8 parent_died);      /* Session should be cleaned up (parent listener may be dead) */
155   /* Add CLI options */
156   int (*process_opts_cb) (unformat_input_t * a);
157   void (*set_defaults_before_opts_cb) (void);
158   void (*set_defaults_after_opts_cb) (void);
159   void (*print_usage_cb) (void);
160 } echo_proto_cb_vft_t;
161
162 typedef enum
163 {
164   RETURN_PACKETS_NOTEST,
165   RETURN_PACKETS_LOG_WRONG,
166   RETURN_PACKETS_ASSERT,
167 } test_return_packets_t;
168
169 typedef struct teardown_stat_
170 {
171   u32 q;                        /* quic sessions */
172   u32 s;                        /* stream sessions */
173 } teardown_stat_t;
174
175 typedef struct
176 {
177   svm_queue_t *vl_input_queue;  /* vpe input queue */
178   u32 my_client_index;          /* API client handle */
179   u8 *uri;                      /* The URI we're playing with */
180   echo_session_t *sessions;     /* Session pool */
181   svm_msg_q_t *our_event_queue; /* Our event queue */
182   clib_time_t clib_time;        /* For deadman timers */
183   u8 *socket_name;
184   int i_am_master;
185   u32 listen_session_index;     /* Index of vpp listener session */
186
187   uword *session_index_by_vpp_handles;  /* Hash table : quic_echo s_id -> vpp s_handle */
188   clib_spinlock_t sid_vpp_handles_lock; /* Hash table lock */
189
190   uword *shared_segment_handles;        /* Hash table : segment_names -> 1 */
191   clib_spinlock_t segment_handles_lock; /* Hash table lock */
192   echo_proto_cb_vft_t *proto_cb_vft;
193   svm_msg_q_t *rpc_msq_queue;   /* MQ between quic_echo threads */
194   fifo_segment_main_t segment_main;
195
196   /* State of the connection, shared between msg RX thread and main thread */
197   volatile connection_state_t state;
198   volatile u8 time_to_stop;     /* Signal variables */
199   u8 has_failed;                /* stores the exit code */
200
201   /** Flag that decides if socket, instead of svm, api is used to connect to
202    * vpp. If sock api is used, shm binary api is subsequently bootstrapped
203    * and all other messages are exchanged using shm IPC. */
204   u8 use_sock_api;
205
206   u8 *connect_test_data;
207   u8 test_return_packets;
208   u64 bytes_to_send;            /* target per stream */
209   u64 bytes_to_receive;         /* target per stream */
210   u32 fifo_size;
211   u32 rx_buf_size;
212   u32 tx_buf_size;
213   data_source_t data_source;    /* Use no/dummy/mirrored data */
214   u8 send_stream_disconnects;   /* actively send disconnect */
215   u8 output_json;               /* Output stats as JSON */
216   u8 log_lvl;                   /* Verbosity of the logging */
217   int max_test_msg;             /* Limit the number of incorrect data messages */
218
219   u8 *appns_id;
220   u64 appns_flags;
221   u64 appns_secret;
222
223   pthread_t *data_thread_handles;       /* vec of data thread handles */
224   pthread_t mq_thread_handle;   /* Message queue thread handle */
225   u32 *volatile data_thread_args;
226
227   u32 n_connects;               /* Target number of connects to send */
228   u32 n_sessions;               /* Number of sessions to prealloc */
229   u32 n_clients;                /* Target number of clients doing RX/TX */
230   u32 n_rx_threads;             /* Number of data threads */
231
232   volatile u32 n_clients_connected;     /* Number of STREAM sessions connected */
233   volatile u32 nxt_available_sidx;      /* next unused prealloced session_index */
234
235   /* VNET_API_ERROR_FOO -> "Foo" hash table */
236   uword *error_string_by_error_number;
237   echo_proto_cb_vft_t *available_proto_cb_vft[TRANSPORT_N_PROTO];
238
239   struct
240   {
241     u64 tx_total;
242     u64 rx_total;
243     teardown_stat_t reset_count;        /* received reset from vpp */
244     teardown_stat_t close_count;        /* received close from vpp */
245     teardown_stat_t active_count;       /* sent close to vpp */
246     teardown_stat_t clean_count;        /* cleaned up stale session */
247   } stats;
248
249   struct                        /* Event based timing : start & end depend on CLI specified events */
250   {
251     f64 start_time;
252     f64 end_time;
253     u8 events_sent;
254     u8 start_event;
255     u8 end_event;
256   } timing;
257
258   struct
259   {
260     u32 transport_proto;
261     ip46_address_t ip;
262     u32 port;
263     u8 is_ip4;
264   } uri_elts;
265 } echo_main_t;
266
267 extern echo_main_t echo_main;
268
269 typedef void (*echo_rpc_t) (void *arg, u32 opaque);
270
271 typedef struct
272 {
273   void *fp;
274   void *arg;
275   u32 opaque;
276 } echo_rpc_msg_t;
277
278 u8 *format_ip4_address (u8 * s, va_list * args);
279 u8 *format_ip6_address (u8 * s, va_list * args);
280 u8 *format_ip46_address (u8 * s, va_list * args);
281 uword unformat_data (unformat_input_t * input, va_list * args);
282 u8 *format_api_error (u8 * s, va_list * args);
283 void init_error_string_table ();
284 u8 *echo_format_app_state (u8 * s, va_list * args);
285 uword echo_unformat_close (unformat_input_t * input, va_list * args);
286 uword echo_unformat_timing_event (unformat_input_t * input, va_list * args);
287 u8 *echo_format_timing_event (u8 * s, va_list * args);
288 uword unformat_transport_proto (unformat_input_t * input, va_list * args);
289 u8 *format_transport_proto (u8 * s, va_list * args);
290 uword unformat_ip4_address (unformat_input_t * input, va_list * args);
291 uword unformat_ip6_address (unformat_input_t * input, va_list * args);
292
293 void echo_session_handle_add_del (echo_main_t * em, u64 handle, u32 sid);
294 echo_session_t *echo_session_new (echo_main_t * em);
295 int echo_send_rpc (echo_main_t * em, void *fp, void *arg, u32 opaque);
296 echo_session_t *echo_get_session_from_handle (echo_main_t * em, u64 handle);
297 int wait_for_segment_allocation (u64 segment_handle);
298 int wait_for_state_change (echo_main_t * em, connection_state_t state,
299                            f64 timeout);
300 void echo_notify_event (echo_main_t * em, echo_test_evt_t e);
301 void echo_session_print_stats (echo_main_t * em, echo_session_t * session);
302
303 /* Binary API */
304
305 void echo_send_attach (echo_main_t * em);
306 void echo_send_detach (echo_main_t * em);
307 void echo_send_listen (echo_main_t * em);
308 void echo_send_unbind (echo_main_t * em);
309 void echo_send_connect (u8 * uri, u32 opaque);
310 void echo_send_disconnect_session (u64 handle, u32 opaque);
311 void echo_api_hookup (echo_main_t * em);
312
313 #endif /* __included_vpp_echo_common_h__ */
314
315 /*
316  * fd.io coding-style-patch-verification: ON
317  *
318  * Local Variables:
319  * eval: (c-set-style "gnu")
320  * End:
321  */