5
5
#include <stdint.h>
6
6
#include <assert.h>
7
7
#include <errno.h>
8
- #include <error.h>
9
8
#include <limits.h>
10
9
#include <fcntl.h>
11
10
#include <unistd.h>
12
11
#include <stdbool.h>
12
+ #include <stdarg.h>
13
13
#include <string.h>
14
14
15
15
#include <arpa/inet.h>
@@ -57,6 +57,23 @@ static struct sockaddr_storage cfg_dst_addr;
57
57
58
58
static char payload [IP_MAXPACKET ] __attribute__((aligned (4096 )));
59
59
60
+ /*
61
+ * Implementation of error(3), prints an error message and exits.
62
+ */
63
+ static void t_error (int status , int errnum , const char * format , ...)
64
+ {
65
+ va_list args ;
66
+ va_start (args , format );
67
+
68
+ vfprintf (stderr , format , args );
69
+ if (errnum )
70
+ fprintf (stderr , ": %s" , strerror (errnum ));
71
+
72
+ fprintf (stderr , "\n" );
73
+ va_end (args );
74
+ exit (status );
75
+ }
76
+
60
77
static unsigned long gettimeofday_ms (void )
61
78
{
62
79
struct timeval tv ;
@@ -68,7 +85,7 @@ static unsigned long gettimeofday_ms(void)
68
85
static void do_setsockopt (int fd , int level , int optname , int val )
69
86
{
70
87
if (setsockopt (fd , level , optname , & val , sizeof (val )))
71
- error (1 , errno , "setsockopt %d.%d: %d" , level , optname , val );
88
+ t_error (1 , errno , "setsockopt %d.%d: %d" , level , optname , val );
72
89
}
73
90
74
91
static void setup_sockaddr (int domain , const char * str_addr ,
@@ -84,18 +101,18 @@ static void setup_sockaddr(int domain, const char *str_addr,
84
101
addr4 -> sin_port = htons (cfg_port );
85
102
if (str_addr &&
86
103
inet_pton (AF_INET , str_addr , & (addr4 -> sin_addr )) != 1 )
87
- error (1 , 0 , "ipv4 parse error: %s" , str_addr );
104
+ t_error (1 , 0 , "ipv4 parse error: %s" , str_addr );
88
105
break ;
89
106
case PF_INET6 :
90
107
memset (addr6 , 0 , sizeof (* addr6 ));
91
108
addr6 -> sin6_family = AF_INET6 ;
92
109
addr6 -> sin6_port = htons (cfg_port );
93
110
if (str_addr &&
94
111
inet_pton (AF_INET6 , str_addr , & (addr6 -> sin6_addr )) != 1 )
95
- error (1 , 0 , "ipv6 parse error: %s" , str_addr );
112
+ t_error (1 , 0 , "ipv6 parse error: %s" , str_addr );
96
113
break ;
97
114
default :
98
- error (1 , 0 , "illegal domain" );
115
+ t_error (1 , 0 , "illegal domain" );
99
116
}
100
117
}
101
118
@@ -105,12 +122,12 @@ static int do_setup_tx(int domain, int type, int protocol)
105
122
106
123
fd = socket (domain , type , protocol );
107
124
if (fd == -1 )
108
- error (1 , errno , "socket t" );
125
+ t_error (1 , errno , "socket t" );
109
126
110
127
do_setsockopt (fd , SOL_SOCKET , SO_SNDBUF , 1 << 21 );
111
128
112
129
if (connect (fd , (void * ) & cfg_dst_addr , cfg_alen ))
113
- error (1 , errno , "connect" );
130
+ t_error (1 , errno , "connect" );
114
131
return fd ;
115
132
}
116
133
@@ -125,7 +142,7 @@ static inline struct io_uring_cqe *wait_cqe_fast(struct io_uring *ring)
125
142
126
143
ret = io_uring_wait_cqe (ring , & cqe );
127
144
if (ret )
128
- error (1 , ret , "wait cqe" );
145
+ t_error (1 , ret , "wait cqe" );
129
146
return cqe ;
130
147
}
131
148
@@ -143,25 +160,25 @@ static void do_tx(int domain, int type, int protocol)
143
160
144
161
ret = io_uring_queue_init (512 , & ring , IORING_SETUP_COOP_TASKRUN );
145
162
if (ret )
146
- error (1 , ret , "io_uring: queue init" );
163
+ t_error (1 , ret , "io_uring: queue init" );
147
164
148
165
if (cfg_fixed_files ) {
149
166
ret = io_uring_register_files (& ring , & fd , 1 );
150
167
if (ret < 0 )
151
- error (1 , ret , "io_uring: files registration" );
168
+ t_error (1 , ret , "io_uring: files registration" );
152
169
}
153
170
if (cfg_reg_ringfd ) {
154
171
ret = io_uring_register_ring_fd (& ring );
155
172
if (ret < 0 )
156
- error (1 , ret , "io_uring: io_uring_register_ring_fd" );
173
+ t_error (1 , ret , "io_uring: io_uring_register_ring_fd" );
157
174
}
158
175
159
176
iov .iov_base = payload ;
160
177
iov .iov_len = cfg_payload_len ;
161
178
162
179
ret = io_uring_register_buffers (& ring , & iov , 1 );
163
180
if (ret )
164
- error (1 , ret , "io_uring: buffer registration" );
181
+ t_error (1 , ret , "io_uring: buffer registration" );
165
182
166
183
tstop = gettimeofday_ms () + cfg_runtime_ms ;
167
184
do {
@@ -193,14 +210,14 @@ static void do_tx(int domain, int type, int protocol)
193
210
194
211
ret = io_uring_submit (& ring );
195
212
if (ret != cfg_nr_reqs )
196
- error (1 , ret , "submit" );
213
+ t_error (1 , ret , "submit" );
197
214
198
215
for (i = 0 ; i < cfg_nr_reqs ; i ++ ) {
199
216
cqe = wait_cqe_fast (& ring );
200
217
201
218
if (cqe -> flags & IORING_CQE_F_NOTIF ) {
202
219
if (cqe -> flags & IORING_CQE_F_MORE )
203
- error (1 , - EINVAL , "F_MORE notif" );
220
+ t_error (1 , - EINVAL , "F_MORE notif" );
204
221
compl_cqes -- ;
205
222
i -- ;
206
223
io_uring_cqe_seen (& ring , cqe );
@@ -217,7 +234,7 @@ static void do_tx(int domain, int type, int protocol)
217
234
fprintf (stderr , "Connection failure" );
218
235
goto out_fail ;
219
236
} else if (cqe -> res != - EAGAIN ) {
220
- error (1 , cqe -> res , "send failed" );
237
+ t_error (1 , cqe -> res , "send failed" );
221
238
}
222
239
io_uring_cqe_seen (& ring , cqe );
223
240
}
@@ -226,7 +243,7 @@ static void do_tx(int domain, int type, int protocol)
226
243
out_fail :
227
244
shutdown (fd , SHUT_RDWR );
228
245
if (close (fd ))
229
- error (1 , errno , "close" );
246
+ t_error (1 , errno , "close" );
230
247
231
248
fprintf (stderr , "tx=%lu (MB=%lu), tx/s=%lu (MB/s=%lu)\n" ,
232
249
packets , bytes >> 20 ,
@@ -254,7 +271,7 @@ static void do_test(int domain, int type, int protocol)
254
271
255
272
static void usage (const char * filepath )
256
273
{
257
- error (1 , 0 , "Usage: %s [-n<N>] [-z<val>] [-s<payload size>] "
274
+ t_error (1 , 0 , "Usage: %s [-n<N>] [-z<val>] [-s<payload size>] "
258
275
"(-4|-6) [-t<time s>] -D<dst_ip> udp" , filepath );
259
276
}
260
277
@@ -276,13 +293,13 @@ static void parse_opts(int argc, char **argv)
276
293
switch (c ) {
277
294
case '4' :
278
295
if (cfg_family != PF_UNSPEC )
279
- error (1 , 0 , "Pass one of -4 or -6" );
296
+ t_error (1 , 0 , "Pass one of -4 or -6" );
280
297
cfg_family = PF_INET ;
281
298
cfg_alen = sizeof (struct sockaddr_in );
282
299
break ;
283
300
case '6' :
284
301
if (cfg_family != PF_UNSPEC )
285
- error (1 , 0 , "Pass one of -4 or -6" );
302
+ t_error (1 , 0 , "Pass one of -4 or -6" );
286
303
cfg_family = PF_INET6 ;
287
304
cfg_alen = sizeof (struct sockaddr_in6 );
288
305
break ;
@@ -311,9 +328,9 @@ static void parse_opts(int argc, char **argv)
311
328
}
312
329
313
330
if (cfg_nr_reqs > MAX_SUBMIT_NR )
314
- error (1 , 0 , "-n: submit batch nr exceeds max (%d)" , MAX_SUBMIT_NR );
331
+ t_error (1 , 0 , "-n: submit batch nr exceeds max (%d)" , MAX_SUBMIT_NR );
315
332
if (cfg_payload_len > max_payload_len )
316
- error (1 , 0 , "-s: payload exceeds max (%d)" , max_payload_len );
333
+ t_error (1 , 0 , "-s: payload exceeds max (%d)" , max_payload_len );
317
334
318
335
setup_sockaddr (cfg_family , daddr , & cfg_dst_addr );
319
336
@@ -333,7 +350,7 @@ int main(int argc, char **argv)
333
350
else if (!strcmp (cfg_test , "udp" ))
334
351
do_test (cfg_family , SOCK_DGRAM , 0 );
335
352
else
336
- error (1 , 0 , "unknown cfg_test %s" , cfg_test );
353
+ t_error (1 , 0 , "unknown cfg_test %s" , cfg_test );
337
354
338
355
return 0 ;
339
356
}
0 commit comments