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