1 // SPDX-License-Identifier: GPL-2.0
9 #include <linux/errqueue.h>
10 #include <linux/if_packet.h>
11 #include <linux/socket.h>
12 #include <linux/sockios.h>
13 #include <net/ethernet.h>
15 #include <netinet/ip.h>
16 #include <netinet/ip6.h>
17 #include <netinet/tcp.h>
18 #include <netinet/udp.h>
26 #include <sys/ioctl.h>
27 #include <sys/socket.h>
30 #include <sys/types.h>
34 static int cfg_port
= 8000;
36 static bool cfg_verify
;
38 static bool interrupted
;
39 static unsigned long packets
, bytes
;
41 static void sigint_handler(int signum
)
47 static unsigned long gettimeofday_ms(void)
51 gettimeofday(&tv
, NULL
);
52 return (tv
.tv_sec
* 1000) + (tv
.tv_usec
/ 1000);
55 static void do_poll(int fd
)
65 ret
= poll(&pfd
, 1, 10);
67 error(1, errno
, "poll");
70 if (pfd
.revents
!= POLLIN
)
71 error(1, errno
, "poll: 0x%x expected 0x%x\n",
73 } while (!ret
&& !interrupted
);
76 static int do_socket(bool do_tcp
)
78 struct sockaddr_in6 addr
= {0};
81 fd
= socket(PF_INET6
, cfg_tcp
? SOCK_STREAM
: SOCK_DGRAM
, 0);
83 error(1, errno
, "socket");
86 if (setsockopt(fd
, SOL_SOCKET
, SO_RCVBUF
, &val
, sizeof(val
)))
87 error(1, errno
, "setsockopt rcvbuf");
89 if (setsockopt(fd
, SOL_SOCKET
, SO_REUSEPORT
, &val
, sizeof(val
)))
90 error(1, errno
, "setsockopt reuseport");
92 addr
.sin6_family
= PF_INET6
;
93 addr
.sin6_port
= htons(cfg_port
);
94 addr
.sin6_addr
= in6addr_any
;
95 if (bind(fd
, (void *) &addr
, sizeof(addr
)))
96 error(1, errno
, "bind");
101 if (listen(accept_fd
, 1))
102 error(1, errno
, "listen");
106 fd
= accept(accept_fd
, NULL
, NULL
);
108 error(1, errno
, "accept");
109 if (close(accept_fd
))
110 error(1, errno
, "close accept fd");
116 /* Flush all outstanding bytes for the tcp receive queue */
117 static void do_flush_tcp(int fd
)
122 /* MSG_TRUNC flushes up to len bytes */
123 ret
= recv(fd
, NULL
, 1 << 21, MSG_TRUNC
| MSG_DONTWAIT
);
124 if (ret
== -1 && errno
== EAGAIN
)
127 error(1, errno
, "flush");
129 /* client detached */
139 static char sanitized_char(char val
)
141 return (val
>= 'a' && val
<= 'z') ? val
: '.';
144 static void do_verify_udp(const char *data
, int len
)
149 /* verify contents */
150 if (cur
< 'a' || cur
> 'z')
151 error(1, 0, "data initial byte out of range");
153 for (i
= 1; i
< len
; i
++) {
160 error(1, 0, "data[%d]: len %d, %c(%hhu) != %c(%hhu)\n",
162 sanitized_char(data
[i
]), data
[i
],
163 sanitized_char(cur
), cur
);
167 /* Flush all outstanding datagrams. Verify first few bytes of each. */
168 static void do_flush_udp(int fd
)
170 static char rbuf
[ETH_DATA_LEN
];
171 int ret
, len
, budget
= 256;
173 len
= cfg_verify
? sizeof(rbuf
) : 0;
175 /* MSG_TRUNC will make return value full datagram length */
176 ret
= recv(fd
, rbuf
, len
, MSG_TRUNC
| MSG_DONTWAIT
);
177 if (ret
== -1 && errno
== EAGAIN
)
180 error(1, errno
, "recv");
183 error(1, errno
, "recv: 0 byte datagram\n");
185 do_verify_udp(rbuf
, ret
);
193 static void usage(const char *filepath
)
195 error(1, 0, "Usage: %s [-tv] [-p port]", filepath
);
198 static void parse_opts(int argc
, char **argv
)
202 while ((c
= getopt(argc
, argv
, "ptv")) != -1) {
205 cfg_port
= htons(strtoul(optarg
, NULL
, 0));
219 if (cfg_tcp
&& cfg_verify
)
220 error(1, 0, "TODO: implement verify mode for tcp");
223 static void do_recv(void)
225 unsigned long tnow
, treport
;
228 fd
= do_socket(cfg_tcp
);
230 treport
= gettimeofday_ms() + 1000;
239 tnow
= gettimeofday_ms();
240 if (tnow
> treport
) {
243 "%s rx: %6lu MB/s %8lu calls/s\n",
244 cfg_tcp
? "tcp" : "udp",
245 bytes
>> 20, packets
);
247 treport
= tnow
+ 1000;
250 } while (!interrupted
);
253 error(1, errno
, "close");
256 int main(int argc
, char **argv
)
258 parse_opts(argc
, argv
);
260 signal(SIGINT
, sigint_handler
);