2 * Copyright (c) 2004 Robert N. M. Watson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/endian.h>
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <net/if.h> /* if_nametoindex() */
35 #include <netinet/in.h>
37 #include <arpa/inet.h>
46 /* program arguments */
50 struct timespec interval;
53 struct sockaddr_in sin;
54 struct sockaddr_in6 sin6;
64 "netsend [ip] [port[-port_max]] [payloadsize] [packet_rate] [duration]\n");
68 #define MAX_RATE 100000000
71 timespec_add(struct timespec *tsa, struct timespec *tsb)
74 tsa->tv_sec += tsb->tv_sec;
75 tsa->tv_nsec += tsb->tv_nsec;
76 if (tsa->tv_nsec >= 1000000000) {
78 tsa->tv_nsec -= 1000000000;
83 timespec_ge(struct timespec *a, struct timespec *b)
86 if (a->tv_sec > b->tv_sec)
88 if (a->tv_sec < b->tv_sec)
90 if (a->tv_nsec >= b->tv_nsec)
96 * Busy wait spinning until we reach (or slightly pass) the desired time.
97 * Optionally return the current time as retrieved on the last time check
98 * to the caller. Optionally also increment a counter provided by the
99 * caller each time we loop.
102 wait_time(struct timespec ts, struct timespec *wakeup_ts, long long *waited)
104 struct timespec curtime;
109 if (clock_gettime(CLOCK_REALTIME, &curtime) == -1) {
110 perror("clock_gettime");
114 if (timespec_ge(&curtime, &ts))
115 printf("warning: wait_time missed deadline without spinning\n");
117 while (timespec_ge(&ts, &curtime)) {
120 if (clock_gettime(CLOCK_REALTIME, &curtime) == -1) {
121 perror("clock_gettime");
125 if (wakeup_ts != NULL)
126 *wakeup_ts = curtime;
131 * Calculate a second-aligned starting time for the packet stream. Busy
132 * wait between our calculated interval and dropping the provided packet
133 * into the socket. If we hit our duration limit, bail.
134 * We sweep the ports from a->port to a->port_max included.
135 * If the two ports are the same we connect() the socket upfront, which
136 * almost halves the cost of the sendto() call.
139 timing_loop(struct _a *a)
141 struct timespec nexttime, starttime, tmptime;
145 long send_errors, send_calls;
146 /* do not call gettimeofday more than every 20us */
147 long minres_ns = 200000;
148 int ic, gettimeofday_cycles;
152 if (clock_getres(CLOCK_REALTIME, &tmptime) == -1) {
153 perror("clock_getres");
157 ns = a->interval.tv_nsec;
158 if (timespec_ge(&tmptime, &a->interval))
160 "warning: interval (%jd.%09ld) less than resolution (%jd.%09ld)\n",
161 (intmax_t)a->interval.tv_sec, a->interval.tv_nsec,
162 (intmax_t)tmptime.tv_sec, tmptime.tv_nsec);
163 /* interval too short, limit the number of gettimeofday()
164 * calls, but also make sure there is at least one every
167 if ((long)ns < minres_ns/100)
168 gettimeofday_cycles = 100;
170 gettimeofday_cycles = minres_ns/ns;
172 "calling time every %d cycles\n", gettimeofday_cycles);
174 if (clock_gettime(CLOCK_REALTIME, &starttime) == -1) {
175 perror("clock_gettime");
180 timespec_add(&starttime, &tmptime);
181 starttime.tv_nsec = 0;
182 if (wait_time(starttime, NULL, NULL) == -1)
184 nexttime = starttime;
185 finishtime = starttime.tv_sec + a->duration;
187 send_errors = send_calls = 0;
190 ic = gettimeofday_cycles;
192 if (a->port == a->port_max) {
194 if (connect(a->s, (struct sockaddr *)&a->sin6, sizeof(a->sin6))) {
195 perror("connect (ipv6)");
199 if (connect(a->s, (struct sockaddr *)&a->sin, sizeof(a->sin))) {
200 perror("connect (ipv4)");
208 timespec_add(&nexttime, &a->interval);
210 ic = gettimeofday_cycles;
211 if (wait_time(nexttime, &tmptime, &waited) == -1)
215 * We maintain and, if there's room, send a counter. Note
216 * that even if the error is purely local, we still increment
217 * the counter, so missing sequence numbers on the receive
218 * side should not be assumed to be packets lost in transit.
219 * For example, if the UDP socket gets back an ICMP from a
220 * previous send, the error will turn up the current send
221 * operation, causing the current sequence number also to be
223 * The counter is incremented only on the initial port number,
224 * so all destinations will see the same set of packets.
226 if (cur_port == a->port && a->packet_len >= 4) {
227 be32enc(a->packet, counter);
230 if (a->port == a->port_max) { /* socket already bound */
231 ret = send(a->s, a->packet, a->packet_len, 0);
233 a->sin.sin_port = htons(cur_port++);
234 if (cur_port > a->port_max)
237 ret = sendto(a->s, a->packet, a->packet_len, 0,
238 (struct sockaddr *)&a->sin6, sizeof(a->sin6));
240 ret = sendto(a->s, a->packet, a->packet_len, 0,
241 (struct sockaddr *)&a->sin, sizeof(a->sin));
247 if (a->duration != 0 && tmptime.tv_sec >= finishtime)
252 if (clock_gettime(CLOCK_REALTIME, &tmptime) == -1) {
253 perror("clock_gettime");
258 printf("start: %jd.%09ld\n", (intmax_t)starttime.tv_sec,
260 printf("finish: %jd.%09ld\n", (intmax_t)tmptime.tv_sec,
262 printf("send calls: %ld\n", send_calls);
263 printf("send errors: %ld\n", send_errors);
264 printf("approx send rate: %ld pps\n", (send_calls - send_errors) /
266 n = send_calls - send_errors;
268 ns = (tmptime.tv_sec - starttime.tv_sec) * 1000000000UL +
269 (tmptime.tv_nsec - starttime.tv_nsec);
272 printf("time/packet: %u ns\n", (u_int)n);
273 printf("approx error rate: %ld\n", (send_errors / send_calls));
274 printf("waited: %lld\n", waited);
275 printf("approx waits/sec: %lld\n", (long long)(waited / a->duration));
276 printf("approx wait rate: %lld\n", (long long)(waited / send_calls));
282 main(int argc, char *argv[])
284 long rate, payloadsize, port;
286 struct _a a; /* arguments */
287 struct addrinfo hints, *res, *ressave;
289 bzero(&a, sizeof(a));
294 memset(&hints, 0, sizeof(hints));
295 hints.ai_family = AF_UNSPEC;
297 if (getaddrinfo(argv[1], NULL, &hints, &res) != 0) {
298 fprintf(stderr, "Couldn't resolv %s\n", argv[1]);
303 if (res->ai_family == AF_INET) {
304 memcpy(&a.sin, res->ai_addr, res->ai_addrlen);
307 } else if (res->ai_family == AF_INET6) {
308 memcpy(&a.sin6, res->ai_addr, res->ai_addrlen);
315 fprintf(stderr, "Couldn't resolv %s\n", argv[1]);
318 freeaddrinfo(ressave);
320 port = strtoul(argv[2], &dummy, 10);
321 if (port < 1 || port > 65535)
323 if (*dummy != '\0' && *dummy != '-')
326 a.sin6.sin6_port = htons(port);
328 a.sin.sin_port = htons(port);
329 a.port = a.port_max = port;
330 if (*dummy == '-') { /* set high port */
331 port = strtoul(dummy + 1, &dummy, 10);
332 if (port < a.port || port > 65535)
337 payloadsize = strtoul(argv[3], &dummy, 10);
338 if (payloadsize < 0 || *dummy != '\0')
340 if (payloadsize > 32768) {
341 fprintf(stderr, "payloadsize > 32768\n");
344 a.packet_len = payloadsize;
347 * Specify an arbitrary limit. It's exactly that, not selected by
348 * any particular strategy. '0' is a special value meaning "blast",
349 * and avoids the cost of a timing loop.
351 rate = strtoul(argv[4], &dummy, 10);
352 if (rate < 0 || *dummy != '\0')
354 if (rate > MAX_RATE) {
355 fprintf(stderr, "packet rate at most %d\n", MAX_RATE);
359 a.duration = strtoul(argv[5], &dummy, 10);
360 if (a.duration < 0 || *dummy != '\0')
363 a.packet = malloc(payloadsize);
364 if (a.packet == NULL) {
368 bzero(a.packet, payloadsize);
370 a.interval.tv_sec = 0;
371 a.interval.tv_nsec = 0;
372 } else if (rate == 1) {
373 a.interval.tv_sec = 1;
374 a.interval.tv_nsec = 0;
376 a.interval.tv_sec = 0;
377 a.interval.tv_nsec = ((1 * 1000000000) / rate);
380 printf("Sending packet of payload size %ld every %jd.%09lds for %ld "
381 "seconds\n", payloadsize, (intmax_t)a.interval.tv_sec,
382 a.interval.tv_nsec, a.duration);
385 a.s = socket(PF_INET6, SOCK_DGRAM, 0);
387 a.s = socket(PF_INET, SOCK_DGRAM, 0);
393 return (timing_loop(&a));