blob: 83e63e1a4e722c30b9ad29ad8bac4469959eb553 [file] [log] [blame]
Dave Wallacee4d5a652018-06-24 21:21:21 -04001/*
2 * Copyright (c) 2017 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 __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>
24#include <vcl/sock_test_common.h>
25
Florin Coras6d4bb422018-09-04 22:07:27 -070026#define vtfail(_fn, _rv) \
27{ \
28 errno = -_rv; \
29 perror ("ERROR when calling " _fn); \
30 fprintf (stderr, "\nERROR: " _fn " failed (errno = %d)!\n", -_rv); \
31 exit (1); \
32}
33
34#define vterr(_fn, _rv) \
35{ \
36 errno = -_rv; \
37 fprintf (stderr, "\nERROR: " _fn " failed (errno = %d)!\n", -_rv); \
38}
39
40#define vtwrn(_fmt, _args...) \
41 fprintf (stderr, "\nERROR: " _fmt "\n", ##_args) \
42
43#define vtinf(_fmt, _args...) \
44 fprintf (stdout, "vt<w%u>: " _fmt "\n", __wrk_index, ##_args)
45
46#define vt_atomic_add(_ptr, _val) \
47 __atomic_fetch_add (_ptr, _val, __ATOMIC_RELEASE)
48
Dave Wallacee4d5a652018-06-24 21:21:21 -040049static inline int
50vcl_test_read (int fd, uint8_t *buf, uint32_t nbytes,
51 sock_test_stats_t *stats)
52{
53 int rx_bytes, errno_val;
54
55 do
56 {
57 if (stats)
58 stats->rx_xacts++;
59 rx_bytes = vppcom_session_read (fd, buf, nbytes);
60
61 if (rx_bytes < 0)
62 {
63 errno = -rx_bytes;
64 rx_bytes = -1;
65 }
66 if (stats)
67 {
68 if ((rx_bytes == 0) ||
69 ((rx_bytes < 0) && ((errno == EAGAIN) || (errno == EWOULDBLOCK))))
70 stats->rx_eagain++;
71 else if (rx_bytes < nbytes)
72 stats->rx_incomp++;
73 }
74 }
75 while ((rx_bytes == 0) ||
76 ((rx_bytes < 0) && ((errno == EAGAIN) || (errno == EWOULDBLOCK))));
77
78 if (rx_bytes < 0)
79 {
Florin Coras2cba8532018-09-11 16:33:36 -070080 vterr ("vppcom_session_read()", -errno);
81 }
82 else if (stats)
83 stats->rx_bytes += rx_bytes;
84
85 return (rx_bytes);
86}
87
88static inline int
89vcl_test_read_ds (int fd, vppcom_data_segments_t ds, sock_test_stats_t *stats)
90{
91 int rx_bytes, errno_val;
92
93 do
94 {
95 if (stats)
96 stats->rx_xacts++;
97 rx_bytes = vppcom_session_read_segments (fd, ds);
98
99 if (rx_bytes < 0)
100 {
101 errno = -rx_bytes;
102 rx_bytes = -1;
103 }
104 if (stats)
105 {
106 if ((rx_bytes == 0) ||
107 ((rx_bytes < 0) && ((errno == EAGAIN) || (errno == EWOULDBLOCK))))
108 stats->rx_eagain++;
109 }
110 }
111 while ((rx_bytes == 0) ||
112 ((rx_bytes < 0) && ((errno == EAGAIN) || (errno == EWOULDBLOCK))));
113
114 if (rx_bytes < 0)
115 {
116 vterr ("vppcom_session_read()", -errno);
Dave Wallacee4d5a652018-06-24 21:21:21 -0400117 }
118 else if (stats)
119 stats->rx_bytes += rx_bytes;
120
121 return (rx_bytes);
122}
123
124static inline int
125vcl_test_write (int fd, uint8_t *buf, uint32_t nbytes,
126 sock_test_stats_t *stats, uint32_t verbose)
127{
128 int tx_bytes = 0;
129 int nbytes_left = nbytes;
130 int rv, errno_val;
131
132 do
133 {
134 if (stats)
135 stats->tx_xacts++;
136 rv = vppcom_session_write (fd, buf, nbytes_left);
137 if (rv < 0)
138 {
139 errno = -rv;
140 rv = -1;
141 }
142 if (rv < 0)
143 {
144 if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
145 {
146 if (stats)
147 stats->tx_eagain++;
148 continue;
149 }
150 else
151 break;
152 }
153 tx_bytes += rv;
154
155 if (tx_bytes != nbytes)
156 {
157 nbytes_left = nbytes_left - rv;
158 if (stats)
159 stats->tx_incomp++;
Dave Wallacee4d5a652018-06-24 21:21:21 -0400160 }
161
162 } while (tx_bytes != nbytes);
163
164 if (tx_bytes < 0)
165 {
166 errno_val = errno;
167 perror ("ERROR in sock_test_write()");
168 fprintf (stderr, "SOCK_TEST: ERROR: socket write failed "
169 "(errno = %d)!\n", errno_val);
170 }
171 else if (stats)
172 stats->tx_bytes += tx_bytes;
173
174 return (tx_bytes);
175}
176
177#endif /* __vcl_test_h__ */