blob: a5a8ad19488e3e2362e9444d36f0b7f7d5360295 [file] [log] [blame]
Dave Wallacee4d5a652018-06-24 21:21:21 -04001/*
Florin Coras5e062572019-03-14 19:07:51 -07002 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
Dave Wallacee4d5a652018-06-24 21:21:21 -04003 * 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 __vcl_test_h__
17#define __vcl_test_h__
18
19#include <netdb.h>
20#include <errno.h>
21#include <stdlib.h>
22#include <string.h>
23#include <vcl/vppcom.h>
Dave Wallacee4d5a652018-06-24 21:21:21 -040024
Florin Coras6d4bb422018-09-04 22:07:27 -070025#define vtfail(_fn, _rv) \
26{ \
27 errno = -_rv; \
28 perror ("ERROR when calling " _fn); \
29 fprintf (stderr, "\nERROR: " _fn " failed (errno = %d)!\n", -_rv); \
30 exit (1); \
31}
32
33#define vterr(_fn, _rv) \
34{ \
35 errno = -_rv; \
36 fprintf (stderr, "\nERROR: " _fn " failed (errno = %d)!\n", -_rv); \
37}
38
39#define vtwrn(_fmt, _args...) \
40 fprintf (stderr, "\nERROR: " _fmt "\n", ##_args) \
41
42#define vtinf(_fmt, _args...) \
43 fprintf (stdout, "vt<w%u>: " _fmt "\n", __wrk_index, ##_args)
44
45#define vt_atomic_add(_ptr, _val) \
46 __atomic_fetch_add (_ptr, _val, __ATOMIC_RELEASE)
47
Florin Coras1502fc32018-10-05 00:50:30 -070048#define VCL_TEST_TOKEN_HELP "#H"
49#define VCL_TEST_TOKEN_EXIT "#X"
50#define VCL_TEST_TOKEN_VERBOSE "#V"
51#define VCL_TEST_TOKEN_TXBUF_SIZE "#T:"
52#define VCL_TEST_TOKEN_NUM_TEST_SESS "#I:"
53#define VCL_TEST_TOKEN_NUM_WRITES "#N:"
54#define VCL_TEST_TOKEN_RXBUF_SIZE "#R:"
55#define VCL_TEST_TOKEN_SHOW_CFG "#C"
56#define VCL_TEST_TOKEN_RUN_UNI "#U"
57#define VCL_TEST_TOKEN_RUN_BI "#B"
58
59#define VCL_TEST_SERVER_PORT 22000
60#define VCL_TEST_LOCALHOST_IPADDR "127.0.0.1"
61
62#define VCL_TEST_CFG_CTRL_MAGIC 0xfeedface
63#define VCL_TEST_CFG_NUM_WRITES_DEF 1000000
64#define VCL_TEST_CFG_TXBUF_SIZE_DEF 8192
65#define VCL_TEST_CFG_RXBUF_SIZE_DEF (64*VCL_TEST_CFG_TXBUF_SIZE_DEF)
66#define VCL_TEST_CFG_BUF_SIZE_MIN 128
67#define VCL_TEST_CFG_MAX_TEST_SESS 32
68#define VCL_TEST_CFG_MAX_EPOLL_EVENTS 16
69
70#define VCL_TEST_DELAY_DISCONNECT 1
71#define VCL_TEST_SEPARATOR_STRING \
72 " -----------------------------\n"
73typedef enum
74{
75 VCL_TEST_TYPE_NONE,
76 VCL_TEST_TYPE_ECHO,
77 VCL_TEST_TYPE_UNI,
78 VCL_TEST_TYPE_BI,
79 VCL_TEST_TYPE_EXIT,
80} vcl_test_t;
81
82typedef struct __attribute__ ((packed))
83{
84 uint32_t magic;
85 uint32_t seq_num;
86 uint32_t test;
87 uint32_t ctrl_handle;
88 uint32_t num_test_sessions;
89 uint32_t verbose;
90 uint32_t address_ip6;
91 uint32_t transport_udp;
Ping Yu34a3a082018-11-30 19:16:17 -050092 uint32_t transport_tls;
Florin Coras1502fc32018-10-05 00:50:30 -070093 uint64_t rxbuf_size;
94 uint64_t txbuf_size;
95 uint64_t num_writes;
96 uint64_t total_bytes;
97} vcl_test_cfg_t;
98
99typedef struct
100{
101 uint64_t rx_xacts;
102 uint64_t rx_bytes;
103 uint32_t rx_eagain;
104 uint32_t rx_incomp;
105 uint64_t tx_xacts;
106 uint64_t tx_bytes;
107 uint32_t tx_eagain;
108 uint32_t tx_incomp;
109 struct timespec start;
110 struct timespec stop;
111} vcl_test_stats_t;
112
113typedef struct
114{
115 int fd;
116 uint32_t txbuf_size;
117 char *txbuf;
118 uint32_t rxbuf_size;
119 char *rxbuf;
120 vcl_test_cfg_t cfg;
121 vcl_test_stats_t stats;
122} vcl_test_session_t;
123
Ping Yu34a3a082018-11-30 19:16:17 -0500124
125/*
126 * TLS server cert and keys to be used for testing only
127 */
128char vcl_test_crt_rsa[] =
129 "-----BEGIN CERTIFICATE-----\r\n"
130 "MIID5zCCAs+gAwIBAgIJALeMYCEHrTtJMA0GCSqGSIb3DQEBCwUAMIGJMQswCQYD\r\n"
131 "VQQGEwJVUzELMAkGA1UECAwCQ0ExETAPBgNVBAcMCFNhbiBKb3NlMQ4wDAYDVQQK\r\n"
132 "DAVDaXNjbzEOMAwGA1UECwwFZmQuaW8xFjAUBgNVBAMMDXRlc3R0bHMuZmQuaW8x\r\n"
133 "IjAgBgkqhkiG9w0BCQEWE3ZwcC1kZXZAbGlzdHMuZmQuaW8wHhcNMTgwMzA1MjEx\r\n"
134 "NTEyWhcNMjgwMzAyMjExNTEyWjCBiTELMAkGA1UEBhMCVVMxCzAJBgNVBAgMAkNB\r\n"
135 "MREwDwYDVQQHDAhTYW4gSm9zZTEOMAwGA1UECgwFQ2lzY28xDjAMBgNVBAsMBWZk\r\n"
136 "LmlvMRYwFAYDVQQDDA10ZXN0dGxzLmZkLmlvMSIwIAYJKoZIhvcNAQkBFhN2cHAt\r\n"
137 "ZGV2QGxpc3RzLmZkLmlvMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA\r\n"
138 "4C1k8a1DuStgggqT4o09fP9sJ2dC54bxhS/Xk2VEfaIZ222WSo4X/syRVfVy9Yah\r\n"
139 "cpI1zJ/RDxaZSFhgA+nPZBrFMsrULkrdAOpOVj8eDEp9JuWdO2ODSoFnCvLxcYWB\r\n"
140 "Yc5kHryJpEaGJl1sFQSesnzMFty/59ta0stk0Fp8r5NhIjWvSovGzPo6Bhz+VS2c\r\n"
141 "ebIZh4x1t2hHaFcgm0qJoJ6DceReWCW8w+yOVovTolGGq+bpb2Hn7MnRSZ2K2NdL\r\n"
142 "+aLXpkZbS/AODP1FF2vTO1mYL290LO7/51vJmPXNKSDYMy5EvILr5/VqtjsFCwRL\r\n"
143 "Q4jcM/+GeHSAFWx4qIv0BwIDAQABo1AwTjAdBgNVHQ4EFgQUWa1SOB37xmT53tZQ\r\n"
144 "aXuLLhRI7U8wHwYDVR0jBBgwFoAUWa1SOB37xmT53tZQaXuLLhRI7U8wDAYDVR0T\r\n"
145 "BAUwAwEB/zANBgkqhkiG9w0BAQsFAAOCAQEAoUht13W4ya27NVzQuCMvqPWL3VM4\r\n"
146 "3xbPFk02FaGz/WupPu276zGlzJAZrbuDcQowwwU1Ni1Yygxl96s1c2M5rHDTrOKG\r\n"
147 "rK0hbkSFBo+i6I8u4HiiQ4rYmG0Hv6+sXn3of0HsbtDPGgWZoipPWDljPYEURu3e\r\n"
148 "3HRe/Dtsj9CakBoSDzs8ndWaBR+f4sM9Tk1cjD46Gq2T/qpSPXqKxEUXlzhdCAn4\r\n"
149 "twub17Bq2kykHpppCwPg5M+v30tHG/R2Go15MeFWbEJthFk3TZMjKL7UFs7fH+x2\r\n"
150 "wSonXb++jY+KmCb93C+soABBizE57g/KmiR2IxQ/LMjDik01RSUIaM0lLA==\r\n"
151 "-----END CERTIFICATE-----\r\n";
152uint32_t vcl_test_crt_rsa_len = sizeof (vcl_test_crt_rsa);
153
154char vcl_test_key_rsa[] =
155 "-----BEGIN PRIVATE KEY-----\r\n"
156 "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDgLWTxrUO5K2CC\r\n"
157 "CpPijT18/2wnZ0LnhvGFL9eTZUR9ohnbbZZKjhf+zJFV9XL1hqFykjXMn9EPFplI\r\n"
158 "WGAD6c9kGsUyytQuSt0A6k5WPx4MSn0m5Z07Y4NKgWcK8vFxhYFhzmQevImkRoYm\r\n"
159 "XWwVBJ6yfMwW3L/n21rSy2TQWnyvk2EiNa9Ki8bM+joGHP5VLZx5shmHjHW3aEdo\r\n"
160 "VyCbSomgnoNx5F5YJbzD7I5Wi9OiUYar5ulvYefsydFJnYrY10v5otemRltL8A4M\r\n"
161 "/UUXa9M7WZgvb3Qs7v/nW8mY9c0pINgzLkS8guvn9Wq2OwULBEtDiNwz/4Z4dIAV\r\n"
162 "bHioi/QHAgMBAAECggEBAMzGipP8+oT166U+NlJXRFifFVN1DvdhG9PWnOxGL+c3\r\n"
163 "ILmBBC08WQzmHshPemBvR6DZkA1H23cV5JTiLWrFtC00CvhXsLRMrE5+uWotI6yE\r\n"
164 "iofybMroHvD6/X5R510UX9hQ6MHu5ShLR5VZ9zXHz5MpTmB/60jG5dLx+jgcwBK8\r\n"
165 "LuGv2YB/WCUwT9QJ3YU2eaingnXtz/MrFbkbltrqlnBdlD+kTtw6Yac9y1XuuQXc\r\n"
166 "BPeulLNDuPolJVWbUvDBZrpt2dXTgz8ws1sv+wCNE0xwQJsqW4Nx3QkpibUL9RUr\r\n"
167 "CVbKlNfa9lopT6nGKlgX69R/uH35yh9AOsfasro6w0ECgYEA82UJ8u/+ORah+0sF\r\n"
168 "Q0FfW5MTdi7OAUHOz16pUsGlaEv0ERrjZxmAkHA/VRwpvDBpx4alCv0Hc39PFLIk\r\n"
169 "nhSsM2BEuBkTAs6/GaoNAiBtQVE/hN7awNRWVmlieS0go3Y3dzaE9IUMyj8sPOFT\r\n"
170 "5JdJ6BM69PHKCkY3dKdnnfpFEuECgYEA68mRpteunF1mdZgXs+WrN+uLlRrQR20F\r\n"
171 "ZyMYiUCH2Dtn26EzA2moy7FipIIrQcX/j+KhYNGM3e7MU4LymIO29E18mn8JODnH\r\n"
172 "sQOXzBTsf8A4yIVMkcuQD3bfb0JiUGYUPOidTp2N7IJA7+6Yc3vQOyb74lnKnJoO\r\n"
173 "gougPT2wS+cCgYAn7muzb6xFsXDhyW0Tm6YJYBfRS9yAWEuVufINobeBZPSl2cN1\r\n"
174 "Jrnw+HlrfTNbrJWuJmjtZJXUXQ6cVp2rUbjutNyRV4vG6iRwEXYQ40EJdkr1gZpi\r\n"
175 "CHQhuShuuPih2MNAy7EEbM+sXrDjTBR3bFqzuHPzu7dp+BshCFX3lRfAAQKBgGQt\r\n"
176 "K5i7IhCFDjb/+3IPLgOAK7mZvsvZ4eXD33TQ2eZgtut1PXtBtNl17/b85uv293Fm\r\n"
177 "VDISVcsk3eLNS8zIiT6afUoWlxAwXEs0v5WRfjl4radkGvgGiJpJYvyeM67877RB\r\n"
178 "EDSKc/X8ESLfOB44iGvZUEMG6zJFscx9DgN25iQZAoGAbyd+JEWwdVH9/K3IH1t2\r\n"
179 "PBkZX17kNWv+iVM1WyFjbe++vfKZCrOJiyiqhDeEqgrP3AuNMlaaduC3VRC3G5oV\r\n"
180 "Mj1tlhDWQ/qhvKdCKNdIVQYDE75nw+FRWV8yYkHAnXYW3tNoweDIwixE0hkPR1bc\r\n"
181 "oEjPLVNtx8SOj/M4rhaPT3I=\r\n" "-----END PRIVATE KEY-----\r\n";
182uint32_t vcl_test_key_rsa_len = sizeof (vcl_test_key_rsa);
183
Florin Coras1502fc32018-10-05 00:50:30 -0700184static inline void
185vcl_test_stats_accumulate (vcl_test_stats_t * accum, vcl_test_stats_t * incr)
186{
187 accum->rx_xacts += incr->rx_xacts;
188 accum->rx_bytes += incr->rx_bytes;
189 accum->rx_eagain += incr->rx_eagain;
190 accum->rx_incomp += incr->rx_incomp;
191 accum->tx_xacts += incr->tx_xacts;
192 accum->tx_bytes += incr->tx_bytes;
193 accum->tx_eagain += incr->tx_eagain;
194 accum->tx_incomp += incr->tx_incomp;
195}
196
197static inline void
198vcl_test_cfg_init (vcl_test_cfg_t * cfg)
199{
200 cfg->magic = VCL_TEST_CFG_CTRL_MAGIC;
201 cfg->test = VCL_TEST_TYPE_NONE;
202 cfg->ctrl_handle = ~0;
203 cfg->num_test_sessions = 1;
204 cfg->verbose = 0;
205 cfg->rxbuf_size = VCL_TEST_CFG_RXBUF_SIZE_DEF;
206 cfg->num_writes = VCL_TEST_CFG_NUM_WRITES_DEF;
207 cfg->txbuf_size = VCL_TEST_CFG_TXBUF_SIZE_DEF;
208 cfg->total_bytes = cfg->num_writes * cfg->txbuf_size;
209}
210
Dave Wallacee4d5a652018-06-24 21:21:21 -0400211static inline int
Florin Coras1502fc32018-10-05 00:50:30 -0700212vcl_test_cfg_verify (vcl_test_cfg_t * cfg, vcl_test_cfg_t * valid_cfg)
213{
214 /* Note: txbuf & rxbuf on server are the same buffer,
215 * so txbuf_size is not included in this check.
216 */
217 return ((cfg->magic == valid_cfg->magic)
218 && (cfg->test == valid_cfg->test)
219 && (cfg->verbose == valid_cfg->verbose)
220 && (cfg->rxbuf_size == valid_cfg->rxbuf_size)
221 && (cfg->num_writes == valid_cfg->num_writes)
222 && (cfg->total_bytes == valid_cfg->total_bytes));
223}
224
225static inline void
226vcl_test_buf_alloc (vcl_test_cfg_t * cfg, uint8_t is_rxbuf, uint8_t ** buf,
227 uint32_t * bufsize)
228{
229 uint32_t alloc_size = is_rxbuf ? cfg->rxbuf_size : cfg->txbuf_size;
230 uint8_t *lb = realloc (*buf, (size_t) alloc_size);
231
232 if (lb)
233 {
234 if (is_rxbuf)
235 cfg->rxbuf_size = *bufsize = alloc_size;
236 else
237 cfg->txbuf_size = *bufsize = alloc_size;
238
239 *buf = lb;
240 }
241 else
242 {
243 vtwrn ("realloc failed. using buffer size %d instead of %u",
244 *bufsize, alloc_size);
245 }
246}
247
248static inline void
249vcl_test_session_buf_alloc (vcl_test_session_t * socket)
250{
251 socket->rxbuf_size = socket->cfg.rxbuf_size;
252 socket->txbuf_size = socket->cfg.txbuf_size;
253 vcl_test_buf_alloc (&socket->cfg, 0 /* is_rxbuf */ ,
254 (uint8_t **) & socket->txbuf, &socket->txbuf_size);
255 vcl_test_buf_alloc (&socket->cfg, 1 /* is_rxbuf */ ,
256 (uint8_t **) & socket->rxbuf, &socket->rxbuf_size);
257}
258
259static inline char *
260vcl_test_type_str (vcl_test_t t)
261{
262 switch (t)
263 {
264 case VCL_TEST_TYPE_NONE:
265 return "NONE";
266
267 case VCL_TEST_TYPE_ECHO:
268 return "ECHO";
269
270 case VCL_TEST_TYPE_UNI:
271 return "UNI";
272
273 case VCL_TEST_TYPE_BI:
274 return "BI";
275
276 case VCL_TEST_TYPE_EXIT:
277 return "EXIT";
278
279 default:
280 return "Unknown";
281 }
282}
283
284static inline void
285vcl_test_cfg_dump (vcl_test_cfg_t * cfg, uint8_t is_client)
286{
287 char *spc = " ";
288
289 printf (" test config (%p):\n"
290 VCL_TEST_SEPARATOR_STRING
291 " magic: 0x%08x\n"
292 " seq_num: 0x%08x\n"
293 "%-5s test: %s (%d)\n"
294 " ctrl handle: %d (0x%x)\n"
295 "%-5s num test sockets: %u (0x%08x)\n"
296 "%-5s verbose: %s (%d)\n"
297 "%-5s rxbuf size: %lu (0x%08lx)\n"
298 "%-5s txbuf size: %lu (0x%08lx)\n"
299 "%-5s num writes: %lu (0x%08lx)\n"
300 " client tx bytes: %lu (0x%08lx)\n"
301 VCL_TEST_SEPARATOR_STRING,
302 (void *) cfg, cfg->magic, cfg->seq_num,
303 is_client && (cfg->test == VCL_TEST_TYPE_UNI) ?
304 "'" VCL_TEST_TOKEN_RUN_UNI "'" :
305 is_client && (cfg->test == VCL_TEST_TYPE_BI) ?
306 "'" VCL_TEST_TOKEN_RUN_BI "'" : spc,
307 vcl_test_type_str (cfg->test), cfg->test,
308 cfg->ctrl_handle, cfg->ctrl_handle,
309 is_client ? "'" VCL_TEST_TOKEN_NUM_TEST_SESS "'" : spc,
310 cfg->num_test_sessions, cfg->num_test_sessions,
311 is_client ? "'" VCL_TEST_TOKEN_VERBOSE "'" : spc,
312 cfg->verbose ? "on" : "off", cfg->verbose,
313 is_client ? "'" VCL_TEST_TOKEN_RXBUF_SIZE "'" : spc,
314 cfg->rxbuf_size, cfg->rxbuf_size,
315 is_client ? "'" VCL_TEST_TOKEN_TXBUF_SIZE "'" : spc,
316 cfg->txbuf_size, cfg->txbuf_size,
317 is_client ? "'" VCL_TEST_TOKEN_NUM_WRITES "'" : spc,
318 cfg->num_writes, cfg->num_writes,
319 cfg->total_bytes, cfg->total_bytes);
320}
321
322static inline void
323vcl_test_stats_dump (char *header, vcl_test_stats_t * stats,
324 uint8_t show_rx, uint8_t show_tx, uint8_t verbose)
325{
326 struct timespec diff;
327 double duration, rate;
328 uint64_t total_bytes;
329
330 if ((stats->stop.tv_nsec - stats->start.tv_nsec) < 0)
331 {
332 diff.tv_sec = stats->stop.tv_sec - stats->start.tv_sec - 1;
333 diff.tv_nsec = stats->stop.tv_nsec - stats->start.tv_nsec + 1e9;
334 }
335 else
336 {
337 diff.tv_sec = stats->stop.tv_sec - stats->start.tv_sec;
338 diff.tv_nsec = stats->stop.tv_nsec - stats->start.tv_nsec;
339 }
340 duration = (double) diff.tv_sec + (1e-9 * diff.tv_nsec);
341
342 total_bytes = stats->tx_bytes + stats->rx_bytes;
343 rate = (double) total_bytes *8 / duration / 1e9;
344 printf ("\n%s: Streamed %lu bytes\n"
345 " in %lf seconds (%lf Gbps %s-duplex)!\n",
346 header, total_bytes, duration, rate,
347 (show_rx && show_tx) ? "full" : "half");
348
349 if (show_tx)
350 {
351 printf (VCL_TEST_SEPARATOR_STRING
352 " tx stats (0x%p):\n"
353 VCL_TEST_SEPARATOR_STRING
354 " writes: %lu (0x%08lx)\n"
355 " tx bytes: %lu (0x%08lx)\n"
356 " tx eagain: %u (0x%08x)\n"
357 " tx incomplete: %u (0x%08x)\n",
358 (void *) stats, stats->tx_xacts, stats->tx_xacts,
359 stats->tx_bytes, stats->tx_bytes,
360 stats->tx_eagain, stats->tx_eagain,
361 stats->tx_incomp, stats->tx_incomp);
362 }
363 if (show_rx)
364 {
365 printf (VCL_TEST_SEPARATOR_STRING
366 " rx stats (0x%p):\n"
367 VCL_TEST_SEPARATOR_STRING
368 " reads: %lu (0x%08lx)\n"
369 " rx bytes: %lu (0x%08lx)\n"
370 " rx eagain: %u (0x%08x)\n"
371 " rx incomplete: %u (0x%08x)\n",
372 (void *) stats, stats->rx_xacts, stats->rx_xacts,
373 stats->rx_bytes, stats->rx_bytes,
374 stats->rx_eagain, stats->rx_eagain,
375 stats->rx_incomp, stats->rx_incomp);
376 }
377 if (verbose)
378 printf (" start.tv_sec: %ld\n"
379 " start.tv_nsec: %ld\n"
380 " stop.tv_sec: %ld\n"
381 " stop.tv_nsec: %ld\n",
382 stats->start.tv_sec, stats->start.tv_nsec,
383 stats->stop.tv_sec, stats->stop.tv_nsec);
384
385 printf (VCL_TEST_SEPARATOR_STRING);
386}
387
388static inline int
389vcl_comp_tspec (struct timespec *a, struct timespec *b)
390{
391 if (a->tv_sec < b->tv_sec)
392 return -1;
393 else if (a->tv_sec > b->tv_sec)
394 return 1;
395 else if (a->tv_nsec < b->tv_nsec)
396 return -1;
397 else if (a->tv_nsec > b->tv_nsec)
398 return 1;
399 else
400 return 0;
401}
402
403static inline int
404vcl_test_read (int fd, uint8_t * buf, uint32_t nbytes,
405 vcl_test_stats_t * stats)
Dave Wallacee4d5a652018-06-24 21:21:21 -0400406{
407 int rx_bytes, errno_val;
Florin Coras1502fc32018-10-05 00:50:30 -0700408
Dave Wallacee4d5a652018-06-24 21:21:21 -0400409 do
410 {
411 if (stats)
Florin Coras1502fc32018-10-05 00:50:30 -0700412 stats->rx_xacts++;
Dave Wallacee4d5a652018-06-24 21:21:21 -0400413 rx_bytes = vppcom_session_read (fd, buf, nbytes);
414
415 if (rx_bytes < 0)
Florin Coras1502fc32018-10-05 00:50:30 -0700416 {
417 errno = -rx_bytes;
418 rx_bytes = -1;
419 }
Dave Wallacee4d5a652018-06-24 21:21:21 -0400420 if (stats)
Florin Coras1502fc32018-10-05 00:50:30 -0700421 {
422 if ((rx_bytes == 0) ||
423 ((rx_bytes < 0)
424 && ((errno == EAGAIN) || (errno == EWOULDBLOCK))))
425 stats->rx_eagain++;
426 else if (rx_bytes < nbytes)
427 stats->rx_incomp++;
428 }
Dave Wallacee4d5a652018-06-24 21:21:21 -0400429 }
430 while ((rx_bytes == 0) ||
Florin Coras1502fc32018-10-05 00:50:30 -0700431 ((rx_bytes < 0) && ((errno == EAGAIN) || (errno == EWOULDBLOCK))));
432
Dave Wallacee4d5a652018-06-24 21:21:21 -0400433 if (rx_bytes < 0)
434 {
Florin Coras2cba8532018-09-11 16:33:36 -0700435 vterr ("vppcom_session_read()", -errno);
436 }
437 else if (stats)
438 stats->rx_bytes += rx_bytes;
439
440 return (rx_bytes);
441}
442
443static inline int
Florin Coras1502fc32018-10-05 00:50:30 -0700444vcl_test_read_ds (int fd, vppcom_data_segments_t ds, vcl_test_stats_t * stats)
Florin Coras2cba8532018-09-11 16:33:36 -0700445{
446 int rx_bytes, errno_val;
447
448 do
449 {
450 if (stats)
Florin Coras1502fc32018-10-05 00:50:30 -0700451 stats->rx_xacts++;
Florin Coras2cba8532018-09-11 16:33:36 -0700452 rx_bytes = vppcom_session_read_segments (fd, ds);
453
454 if (rx_bytes < 0)
Florin Coras1502fc32018-10-05 00:50:30 -0700455 {
456 errno = -rx_bytes;
457 rx_bytes = -1;
458 }
Florin Coras2cba8532018-09-11 16:33:36 -0700459 if (stats)
Florin Coras1502fc32018-10-05 00:50:30 -0700460 {
461 if ((rx_bytes == 0) ||
462 ((rx_bytes < 0)
463 && ((errno == EAGAIN) || (errno == EWOULDBLOCK))))
464 stats->rx_eagain++;
465 }
Florin Coras2cba8532018-09-11 16:33:36 -0700466 }
467 while ((rx_bytes == 0) ||
Florin Coras1502fc32018-10-05 00:50:30 -0700468 ((rx_bytes < 0) && ((errno == EAGAIN) || (errno == EWOULDBLOCK))));
Florin Coras2cba8532018-09-11 16:33:36 -0700469
470 if (rx_bytes < 0)
471 {
472 vterr ("vppcom_session_read()", -errno);
Dave Wallacee4d5a652018-06-24 21:21:21 -0400473 }
474 else if (stats)
475 stats->rx_bytes += rx_bytes;
476
477 return (rx_bytes);
478}
479
480static inline int
Florin Coras1502fc32018-10-05 00:50:30 -0700481vcl_test_write (int fd, uint8_t * buf, uint32_t nbytes,
482 vcl_test_stats_t * stats, uint32_t verbose)
Dave Wallacee4d5a652018-06-24 21:21:21 -0400483{
Florin Coras0e88e852018-09-17 22:09:02 -0700484 int tx_bytes = 0, nbytes_left = nbytes, rv;
Dave Wallacee4d5a652018-06-24 21:21:21 -0400485
486 do
487 {
488 if (stats)
Florin Coras1502fc32018-10-05 00:50:30 -0700489 stats->tx_xacts++;
Dave Wallacee4d5a652018-06-24 21:21:21 -0400490 rv = vppcom_session_write (fd, buf, nbytes_left);
491 if (rv < 0)
Florin Coras1502fc32018-10-05 00:50:30 -0700492 {
493 errno = -rv;
494 rv = -1;
495 }
Dave Wallacee4d5a652018-06-24 21:21:21 -0400496 if (rv < 0)
Florin Coras1502fc32018-10-05 00:50:30 -0700497 {
498 if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
499 {
500 if (stats)
501 stats->tx_eagain++;
Florin Corasaa27eb92018-10-13 12:20:01 -0700502 break;
Florin Coras1502fc32018-10-05 00:50:30 -0700503 }
504 else
505 break;
506 }
Dave Wallacee4d5a652018-06-24 21:21:21 -0400507 tx_bytes += rv;
Florin Coras1502fc32018-10-05 00:50:30 -0700508
Dave Wallacee4d5a652018-06-24 21:21:21 -0400509 if (tx_bytes != nbytes)
Florin Coras1502fc32018-10-05 00:50:30 -0700510 {
511 nbytes_left = nbytes_left - rv;
wuzhouhuic7886c52019-03-10 11:35:46 +0800512 buf += rv;
Florin Coras1502fc32018-10-05 00:50:30 -0700513 if (stats)
514 stats->tx_incomp++;
515 }
516
517 }
518 while (tx_bytes != nbytes);
Dave Wallacee4d5a652018-06-24 21:21:21 -0400519
520 if (tx_bytes < 0)
521 {
Florin Coras0e88e852018-09-17 22:09:02 -0700522 vterr ("vpcom_session_write", -errno);
Dave Wallacee4d5a652018-06-24 21:21:21 -0400523 }
524 else if (stats)
525 stats->tx_bytes += tx_bytes;
Florin Coras1502fc32018-10-05 00:50:30 -0700526
Dave Wallacee4d5a652018-06-24 21:21:21 -0400527 return (tx_bytes);
528}
529
530#endif /* __vcl_test_h__ */
Florin Coras1502fc32018-10-05 00:50:30 -0700531
532/*
533 * fd.io coding-style-patch-verification: ON
534 *
535 * Local Variables:
536 * eval: (c-set-style "gnu")
537 * End:
538 */