xref: /freebsd-src/tools/tools/netrate/netsend/netsend.c (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
12d832be0SRobert Watson /*-
22d832be0SRobert Watson  * Copyright (c) 2004 Robert N. M. Watson
32d832be0SRobert Watson  * All rights reserved.
42d832be0SRobert Watson  *
52d832be0SRobert Watson  * Redistribution and use in source and binary forms, with or without
62d832be0SRobert Watson  * modification, are permitted provided that the following conditions
72d832be0SRobert Watson  * are met:
82d832be0SRobert Watson  * 1. Redistributions of source code must retain the above copyright
92d832be0SRobert Watson  *    notice, this list of conditions and the following disclaimer.
102d832be0SRobert Watson  * 2. Redistributions in binary form must reproduce the above copyright
112d832be0SRobert Watson  *    notice, this list of conditions and the following disclaimer in the
122d832be0SRobert Watson  *    documentation and/or other materials provided with the distribution.
132d832be0SRobert Watson  *
142d832be0SRobert Watson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
152d832be0SRobert Watson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
162d832be0SRobert Watson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
172d832be0SRobert Watson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
182d832be0SRobert Watson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
192d832be0SRobert Watson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
202d832be0SRobert Watson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
212d832be0SRobert Watson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
222d832be0SRobert Watson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
232d832be0SRobert Watson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
242d832be0SRobert Watson  * SUCH DAMAGE.
252d832be0SRobert Watson  */
262d832be0SRobert Watson 
2774931812SMarius Strobl #include <sys/endian.h>
282d832be0SRobert Watson #include <sys/types.h>
292d832be0SRobert Watson #include <sys/socket.h>
30c2a24727SOlivier Houchard #include <net/if.h>		/* if_nametoindex() */
312d832be0SRobert Watson #include <sys/time.h>
322d832be0SRobert Watson 
332d832be0SRobert Watson #include <netinet/in.h>
342d832be0SRobert Watson 
352d832be0SRobert Watson #include <arpa/inet.h>
362d832be0SRobert Watson 
372d832be0SRobert Watson #include <stdio.h>
3816360976SMaxime Henrion #include <stdint.h>
392d832be0SRobert Watson #include <stdlib.h>
402d832be0SRobert Watson #include <string.h>
412d832be0SRobert Watson 
42c2a24727SOlivier Houchard #include <netdb.h>
43c2a24727SOlivier Houchard 
44ed8c4b44SLuigi Rizzo /* program arguments */
45ed8c4b44SLuigi Rizzo struct _a {
46ed8c4b44SLuigi Rizzo 	int s;
47c2a24727SOlivier Houchard 	int ipv6;
48ed8c4b44SLuigi Rizzo 	struct timespec interval;
49ed8c4b44SLuigi Rizzo 	int port, port_max;
50ed8c4b44SLuigi Rizzo 	long duration;
51ed8c4b44SLuigi Rizzo 	struct sockaddr_in sin;
52c2a24727SOlivier Houchard 	struct sockaddr_in6 sin6;
53ed8c4b44SLuigi Rizzo 	int packet_len;
54ed8c4b44SLuigi Rizzo 	void *packet;
55ed8c4b44SLuigi Rizzo };
56ed8c4b44SLuigi Rizzo 
572d832be0SRobert Watson static void
usage(void)582d832be0SRobert Watson usage(void)
592d832be0SRobert Watson {
602d832be0SRobert Watson 
612d832be0SRobert Watson 	fprintf(stderr,
62ed8c4b44SLuigi Rizzo 	    "netsend [ip] [port[-port_max]] [payloadsize] [packet_rate] [duration]\n");
632d832be0SRobert Watson 	exit(-1);
642d832be0SRobert Watson }
652d832be0SRobert Watson 
662645c335SRobert Watson #define	MAX_RATE	100000000
672645c335SRobert Watson 
682d832be0SRobert Watson static __inline void
timespec_add(struct timespec * tsa,struct timespec * tsb)692d832be0SRobert Watson timespec_add(struct timespec *tsa, struct timespec *tsb)
702d832be0SRobert Watson {
712d832be0SRobert Watson 
722d832be0SRobert Watson 	tsa->tv_sec += tsb->tv_sec;
732d832be0SRobert Watson 	tsa->tv_nsec += tsb->tv_nsec;
742d832be0SRobert Watson 	if (tsa->tv_nsec >= 1000000000) {
752d832be0SRobert Watson 		tsa->tv_sec++;
762d832be0SRobert Watson 		tsa->tv_nsec -= 1000000000;
772d832be0SRobert Watson 	}
782d832be0SRobert Watson }
792d832be0SRobert Watson 
80aa1cb3e1SRobert Watson static __inline int
timespec_ge(struct timespec * a,struct timespec * b)81aa1cb3e1SRobert Watson timespec_ge(struct timespec *a, struct timespec *b)
82aa1cb3e1SRobert Watson {
83aa1cb3e1SRobert Watson 
84aa1cb3e1SRobert Watson 	if (a->tv_sec > b->tv_sec)
85aa1cb3e1SRobert Watson 		return (1);
86aa1cb3e1SRobert Watson 	if (a->tv_sec < b->tv_sec)
87aa1cb3e1SRobert Watson 		return (0);
88aa1cb3e1SRobert Watson 	if (a->tv_nsec >= b->tv_nsec)
89aa1cb3e1SRobert Watson 		return (1);
90aa1cb3e1SRobert Watson 	return (0);
91aa1cb3e1SRobert Watson }
92aa1cb3e1SRobert Watson 
932d832be0SRobert Watson /*
942d832be0SRobert Watson  * Busy wait spinning until we reach (or slightly pass) the desired time.
952d832be0SRobert Watson  * Optionally return the current time as retrieved on the last time check
96aa1cb3e1SRobert Watson  * to the caller.  Optionally also increment a counter provided by the
97aa1cb3e1SRobert Watson  * caller each time we loop.
982d832be0SRobert Watson  */
99d499d502SGiorgos Keramidas static int
wait_time(struct timespec ts,struct timespec * wakeup_ts,long long * waited)100aa1cb3e1SRobert Watson wait_time(struct timespec ts, struct timespec *wakeup_ts, long long *waited)
1012d832be0SRobert Watson {
1022d832be0SRobert Watson 	struct timespec curtime;
1032d832be0SRobert Watson 
1042d832be0SRobert Watson 	curtime.tv_sec = 0;
1052d832be0SRobert Watson 	curtime.tv_nsec = 0;
1062d832be0SRobert Watson 
107aa1cb3e1SRobert Watson 	if (clock_gettime(CLOCK_REALTIME, &curtime) == -1) {
108aa1cb3e1SRobert Watson 		perror("clock_gettime");
109aa1cb3e1SRobert Watson 		return (-1);
110aa1cb3e1SRobert Watson 	}
111aa1cb3e1SRobert Watson #if 0
112aa1cb3e1SRobert Watson 	if (timespec_ge(&curtime, &ts))
113aa1cb3e1SRobert Watson 		printf("warning: wait_time missed deadline without spinning\n");
114aa1cb3e1SRobert Watson #endif
115aa1cb3e1SRobert Watson 	while (timespec_ge(&ts, &curtime)) {
116aa1cb3e1SRobert Watson 		if (waited != NULL)
117aa1cb3e1SRobert Watson 			(*waited)++;
118aa1cb3e1SRobert Watson 		if (clock_gettime(CLOCK_REALTIME, &curtime) == -1) {
1192d832be0SRobert Watson 			perror("clock_gettime");
1202d832be0SRobert Watson 			return (-1);
1212d832be0SRobert Watson 		}
1222d832be0SRobert Watson 	}
1232d832be0SRobert Watson 	if (wakeup_ts != NULL)
1242d832be0SRobert Watson 		*wakeup_ts = curtime;
1252d832be0SRobert Watson 	return (0);
1262d832be0SRobert Watson }
1272d832be0SRobert Watson 
1282d832be0SRobert Watson /*
1292d832be0SRobert Watson  * Calculate a second-aligned starting time for the packet stream.  Busy
1302d832be0SRobert Watson  * wait between our calculated interval and dropping the provided packet
1312d832be0SRobert Watson  * into the socket.  If we hit our duration limit, bail.
132ed8c4b44SLuigi Rizzo  * We sweep the ports from a->port to a->port_max included.
133ed8c4b44SLuigi Rizzo  * If the two ports are the same we connect() the socket upfront, which
134ed8c4b44SLuigi Rizzo  * almost halves the cost of the sendto() call.
1352d832be0SRobert Watson  */
136d499d502SGiorgos Keramidas static int
timing_loop(struct _a * a)137ed8c4b44SLuigi Rizzo timing_loop(struct _a *a)
1382d832be0SRobert Watson {
139aa1cb3e1SRobert Watson 	struct timespec nexttime, starttime, tmptime;
140aa1cb3e1SRobert Watson 	long long waited;
1412d832be0SRobert Watson 	u_int32_t counter;
1422d832be0SRobert Watson 	long finishtime;
143d499d502SGiorgos Keramidas 	long send_errors, send_calls;
14454dd2168SLuigi Rizzo 	/* do not call gettimeofday more than every 20us */
145*9aff36d8SLuigi Rizzo 	long minres_ns = 200000;
14654dd2168SLuigi Rizzo 	int ic, gettimeofday_cycles;
147ed8c4b44SLuigi Rizzo 	int cur_port;
1484a8b6894SLuigi Rizzo 	uint64_t n, ns;
1492d832be0SRobert Watson 
150aa1cb3e1SRobert Watson 	if (clock_getres(CLOCK_REALTIME, &tmptime) == -1) {
151aa1cb3e1SRobert Watson 		perror("clock_getres");
152aa1cb3e1SRobert Watson 		return (-1);
153aa1cb3e1SRobert Watson 	}
154aa1cb3e1SRobert Watson 
155*9aff36d8SLuigi Rizzo 	ns = a->interval.tv_nsec;
156ed8c4b44SLuigi Rizzo 	if (timespec_ge(&tmptime, &a->interval))
157aa1cb3e1SRobert Watson 		fprintf(stderr,
15854dd2168SLuigi Rizzo 		    "warning: interval (%jd.%09ld) less than resolution (%jd.%09ld)\n",
159ed8c4b44SLuigi Rizzo 		    (intmax_t)a->interval.tv_sec, a->interval.tv_nsec,
16016360976SMaxime Henrion 		    (intmax_t)tmptime.tv_sec, tmptime.tv_nsec);
161*9aff36d8SLuigi Rizzo 		/* interval too short, limit the number of gettimeofday()
162*9aff36d8SLuigi Rizzo 		 * calls, but also make sure there is at least one every
163*9aff36d8SLuigi Rizzo 		 * some 100 packets.
164*9aff36d8SLuigi Rizzo 		 */
165*9aff36d8SLuigi Rizzo 	if ((long)ns < minres_ns/100)
166*9aff36d8SLuigi Rizzo 		gettimeofday_cycles = 100;
167*9aff36d8SLuigi Rizzo 	else
168*9aff36d8SLuigi Rizzo 		gettimeofday_cycles = minres_ns/ns;
16954dd2168SLuigi Rizzo 	fprintf(stderr,
17054dd2168SLuigi Rizzo 	    "calling time every %d cycles\n", gettimeofday_cycles);
171aa1cb3e1SRobert Watson 
172aa1cb3e1SRobert Watson 	if (clock_gettime(CLOCK_REALTIME, &starttime) == -1) {
1732d832be0SRobert Watson 		perror("clock_gettime");
1742d832be0SRobert Watson 		return (-1);
1752d832be0SRobert Watson 	}
1762d832be0SRobert Watson 	tmptime.tv_sec = 2;
1772d832be0SRobert Watson 	tmptime.tv_nsec = 0;
1782d832be0SRobert Watson 	timespec_add(&starttime, &tmptime);
1792d832be0SRobert Watson 	starttime.tv_nsec = 0;
180aa1cb3e1SRobert Watson 	if (wait_time(starttime, NULL, NULL) == -1)
1812d832be0SRobert Watson 		return (-1);
182aa1cb3e1SRobert Watson 	nexttime = starttime;
183ed8c4b44SLuigi Rizzo 	finishtime = starttime.tv_sec + a->duration;
1842d832be0SRobert Watson 
185aa1cb3e1SRobert Watson 	send_errors = send_calls = 0;
1862d832be0SRobert Watson 	counter = 0;
187aa1cb3e1SRobert Watson 	waited = 0;
18854dd2168SLuigi Rizzo 	ic = gettimeofday_cycles;
189ed8c4b44SLuigi Rizzo 	cur_port = a->port;
190ed8c4b44SLuigi Rizzo 	if (a->port == a->port_max) {
191c2a24727SOlivier Houchard 		if (a->ipv6) {
192c2a24727SOlivier Houchard 			if (connect(a->s, (struct sockaddr *)&a->sin6, sizeof(a->sin6))) {
193c2a24727SOlivier Houchard 				perror("connect (ipv6)");
194ed8c4b44SLuigi Rizzo 				return (-1);
195ed8c4b44SLuigi Rizzo 			}
196c2a24727SOlivier Houchard 		} else {
197c2a24727SOlivier Houchard 			if (connect(a->s, (struct sockaddr *)&a->sin, sizeof(a->sin))) {
198c2a24727SOlivier Houchard 				perror("connect (ipv4)");
199c2a24727SOlivier Houchard 				return (-1);
200c2a24727SOlivier Houchard 			}
201c2a24727SOlivier Houchard 		}
202ed8c4b44SLuigi Rizzo 	}
2032d832be0SRobert Watson 	while (1) {
204ed8c4b44SLuigi Rizzo 		int ret;
205ed8c4b44SLuigi Rizzo 
206ed8c4b44SLuigi Rizzo 		timespec_add(&nexttime, &a->interval);
20754dd2168SLuigi Rizzo 		if (--ic <= 0) {
20854dd2168SLuigi Rizzo 			ic = gettimeofday_cycles;
209aa1cb3e1SRobert Watson 			if (wait_time(nexttime, &tmptime, &waited) == -1)
2102d832be0SRobert Watson 				return (-1);
21154dd2168SLuigi Rizzo 		}
2122d832be0SRobert Watson 		/*
2132d832be0SRobert Watson 		 * We maintain and, if there's room, send a counter.  Note
2142d832be0SRobert Watson 		 * that even if the error is purely local, we still increment
2152d832be0SRobert Watson 		 * the counter, so missing sequence numbers on the receive
2162d832be0SRobert Watson 		 * side should not be assumed to be packets lost in transit.
2172d832be0SRobert Watson 		 * For example, if the UDP socket gets back an ICMP from a
2182d832be0SRobert Watson 		 * previous send, the error will turn up the current send
2192d832be0SRobert Watson 		 * operation, causing the current sequence number also to be
2202d832be0SRobert Watson 		 * skipped.
221ed8c4b44SLuigi Rizzo 		 * The counter is incremented only on the initial port number,
222ed8c4b44SLuigi Rizzo 		 * so all destinations will see the same set of packets.
2232d832be0SRobert Watson 		 */
224ed8c4b44SLuigi Rizzo 		if (cur_port == a->port && a->packet_len >= 4) {
22574931812SMarius Strobl 			be32enc(a->packet, counter);
2262d832be0SRobert Watson 			counter++;
2272d832be0SRobert Watson 		}
228ed8c4b44SLuigi Rizzo 		if (a->port == a->port_max) { /* socket already bound */
229ed8c4b44SLuigi Rizzo 			ret = send(a->s, a->packet, a->packet_len, 0);
230ed8c4b44SLuigi Rizzo 		} else {
231ed8c4b44SLuigi Rizzo 			a->sin.sin_port = htons(cur_port++);
232ed8c4b44SLuigi Rizzo 			if (cur_port > a->port_max)
233ed8c4b44SLuigi Rizzo 				cur_port = a->port;
234c2a24727SOlivier Houchard 			if (a->ipv6) {
235c2a24727SOlivier Houchard 			ret = sendto(a->s, a->packet, a->packet_len, 0,
236c2a24727SOlivier Houchard 			    (struct sockaddr *)&a->sin6, sizeof(a->sin6));
237c2a24727SOlivier Houchard 			} else {
238ed8c4b44SLuigi Rizzo 			ret = sendto(a->s, a->packet, a->packet_len, 0,
239ed8c4b44SLuigi Rizzo 				(struct sockaddr *)&a->sin, sizeof(a->sin));
240ed8c4b44SLuigi Rizzo 			}
241c2a24727SOlivier Houchard 		}
242ed8c4b44SLuigi Rizzo 		if (ret < 0)
243aa1cb3e1SRobert Watson 			send_errors++;
244aa1cb3e1SRobert Watson 		send_calls++;
245ed8c4b44SLuigi Rizzo 		if (a->duration != 0 && tmptime.tv_sec >= finishtime)
246aa1cb3e1SRobert Watson 			goto done;
247aa1cb3e1SRobert Watson 	}
248aa1cb3e1SRobert Watson 
249aa1cb3e1SRobert Watson done:
250aa1cb3e1SRobert Watson 	if (clock_gettime(CLOCK_REALTIME, &tmptime) == -1) {
251aa1cb3e1SRobert Watson 		perror("clock_gettime");
252aa1cb3e1SRobert Watson 		return (-1);
253aa1cb3e1SRobert Watson 	}
254aa1cb3e1SRobert Watson 
255aa1cb3e1SRobert Watson 	printf("\n");
25616360976SMaxime Henrion 	printf("start:             %jd.%09ld\n", (intmax_t)starttime.tv_sec,
257aa1cb3e1SRobert Watson 	    starttime.tv_nsec);
25816360976SMaxime Henrion 	printf("finish:            %jd.%09ld\n", (intmax_t)tmptime.tv_sec,
259aa1cb3e1SRobert Watson 	    tmptime.tv_nsec);
260d499d502SGiorgos Keramidas 	printf("send calls:        %ld\n", send_calls);
261d499d502SGiorgos Keramidas 	printf("send errors:       %ld\n", send_errors);
262ed8c4b44SLuigi Rizzo 	printf("approx send rate:  %ld pps\n", (send_calls - send_errors) /
263ed8c4b44SLuigi Rizzo 	    a->duration);
2644a8b6894SLuigi Rizzo 	n = send_calls - send_errors;
2654a8b6894SLuigi Rizzo 	if (n > 0) {
2664a8b6894SLuigi Rizzo 		ns = (tmptime.tv_sec - starttime.tv_sec) * 1000000000UL +
2674a8b6894SLuigi Rizzo 			(tmptime.tv_nsec - starttime.tv_nsec);
2684a8b6894SLuigi Rizzo 		n = ns / n;
2694a8b6894SLuigi Rizzo 	}
2704a8b6894SLuigi Rizzo 	printf("time/packet:       %u ns\n", (u_int)n);
271d499d502SGiorgos Keramidas 	printf("approx error rate: %ld\n", (send_errors / send_calls));
272aa1cb3e1SRobert Watson 	printf("waited:            %lld\n", waited);
273ed8c4b44SLuigi Rizzo 	printf("approx waits/sec:  %lld\n", (long long)(waited / a->duration));
27416360976SMaxime Henrion 	printf("approx wait rate:  %lld\n", (long long)(waited / send_calls));
2752d832be0SRobert Watson 
2762d832be0SRobert Watson 	return (0);
2772d832be0SRobert Watson }
2782d832be0SRobert Watson 
2792d832be0SRobert Watson int
main(int argc,char * argv[])2802d832be0SRobert Watson main(int argc, char *argv[])
2812d832be0SRobert Watson {
282ed8c4b44SLuigi Rizzo 	long rate, payloadsize, port;
283ed8c4b44SLuigi Rizzo 	char *dummy;
284ed8c4b44SLuigi Rizzo 	struct _a a;	/* arguments */
285c2a24727SOlivier Houchard 	struct addrinfo hints, *res, *ressave;
286ed8c4b44SLuigi Rizzo 
287ed8c4b44SLuigi Rizzo 	bzero(&a, sizeof(a));
2882d832be0SRobert Watson 
2892d832be0SRobert Watson 	if (argc != 6)
2902d832be0SRobert Watson 		usage();
2912d832be0SRobert Watson 
292c2a24727SOlivier Houchard 	memset(&hints, 0, sizeof(hints));
293c2a24727SOlivier Houchard 	hints.ai_family = AF_UNSPEC;
294c2a24727SOlivier Houchard 
295c2a24727SOlivier Houchard 	if (getaddrinfo(argv[1], NULL, &hints, &res) != 0) {
296c2a24727SOlivier Houchard 		fprintf(stderr, "Couldn't resolv %s\n", argv[1]);
2972d832be0SRobert Watson 		return (-1);
2982d832be0SRobert Watson 	}
299c2a24727SOlivier Houchard 	ressave = res;
300c2a24727SOlivier Houchard 	while (res) {
301c2a24727SOlivier Houchard 		if (res->ai_family == AF_INET) {
302c2a24727SOlivier Houchard 			memcpy(&a.sin, res->ai_addr, res->ai_addrlen);
303c2a24727SOlivier Houchard 			a.ipv6 = 0;
304c2a24727SOlivier Houchard 			break;
305c2a24727SOlivier Houchard 		} else if (res->ai_family == AF_INET6) {
306c2a24727SOlivier Houchard 			memcpy(&a.sin6, res->ai_addr, res->ai_addrlen);
307c2a24727SOlivier Houchard 			a.ipv6 = 1;
308c2a24727SOlivier Houchard 			break;
309c2a24727SOlivier Houchard 		}
310c2a24727SOlivier Houchard 		res = res->ai_next;
311c2a24727SOlivier Houchard 	}
312c2a24727SOlivier Houchard 	if (!res) {
313c2a24727SOlivier Houchard 		fprintf(stderr, "Couldn't resolv %s\n", argv[1]);
314c2a24727SOlivier Houchard 		exit(1);
315c2a24727SOlivier Houchard 	}
316c2a24727SOlivier Houchard 	freeaddrinfo(ressave);
3172d832be0SRobert Watson 
3182d832be0SRobert Watson 	port = strtoul(argv[2], &dummy, 10);
319ed8c4b44SLuigi Rizzo 	if (port < 1 || port > 65535)
3202d832be0SRobert Watson 		usage();
321ed8c4b44SLuigi Rizzo 	if (*dummy != '\0' && *dummy != '-')
322ed8c4b44SLuigi Rizzo 		usage();
323c2a24727SOlivier Houchard 	if (a.ipv6)
324c2a24727SOlivier Houchard 		a.sin6.sin6_port = htons(port);
325c2a24727SOlivier Houchard 	else
326ed8c4b44SLuigi Rizzo 		a.sin.sin_port = htons(port);
327ed8c4b44SLuigi Rizzo 	a.port = a.port_max = port;
328ed8c4b44SLuigi Rizzo 	if (*dummy == '-') {	/* set high port */
329ed8c4b44SLuigi Rizzo 		port = strtoul(dummy + 1, &dummy, 10);
330ed8c4b44SLuigi Rizzo 		if (port < a.port || port > 65535)
331ed8c4b44SLuigi Rizzo 			usage();
332ed8c4b44SLuigi Rizzo 		a.port_max = port;
333ed8c4b44SLuigi Rizzo 	}
3342d832be0SRobert Watson 
3352d832be0SRobert Watson 	payloadsize = strtoul(argv[3], &dummy, 10);
3362d832be0SRobert Watson 	if (payloadsize < 0 || *dummy != '\0')
3372d832be0SRobert Watson 		usage();
3382d832be0SRobert Watson 	if (payloadsize > 32768) {
3392d832be0SRobert Watson 		fprintf(stderr, "payloadsize > 32768\n");
3402d832be0SRobert Watson 		return (-1);
3412d832be0SRobert Watson 	}
342ed8c4b44SLuigi Rizzo 	a.packet_len = payloadsize;
3432d832be0SRobert Watson 
3442d832be0SRobert Watson 	/*
3452d832be0SRobert Watson 	 * Specify an arbitrary limit.  It's exactly that, not selected by
34654dd2168SLuigi Rizzo 	 * any particular strategy.  '0' is a special value meaning "blast",
347aa1cb3e1SRobert Watson 	 * and avoids the cost of a timing loop.
3482d832be0SRobert Watson 	 */
3492d832be0SRobert Watson 	rate = strtoul(argv[4], &dummy, 10);
350ed8c4b44SLuigi Rizzo 	if (rate < 0 || *dummy != '\0')
3512d832be0SRobert Watson 		usage();
3522645c335SRobert Watson 	if (rate > MAX_RATE) {
353ed8c4b44SLuigi Rizzo 		fprintf(stderr, "packet rate at most %d\n", MAX_RATE);
3542d832be0SRobert Watson 		return (-1);
3552d832be0SRobert Watson 	}
3562d832be0SRobert Watson 
357ed8c4b44SLuigi Rizzo 	a.duration = strtoul(argv[5], &dummy, 10);
358ed8c4b44SLuigi Rizzo 	if (a.duration < 0 || *dummy != '\0')
3592d832be0SRobert Watson 		usage();
3602d832be0SRobert Watson 
361ed8c4b44SLuigi Rizzo 	a.packet = malloc(payloadsize);
362ed8c4b44SLuigi Rizzo 	if (a.packet == NULL) {
3632d832be0SRobert Watson 		perror("malloc");
3642d832be0SRobert Watson 		return (-1);
3652d832be0SRobert Watson 	}
366ed8c4b44SLuigi Rizzo 	bzero(a.packet, payloadsize);
367aa1cb3e1SRobert Watson 	if (rate == 0) {
368ed8c4b44SLuigi Rizzo 		a.interval.tv_sec = 0;
369ed8c4b44SLuigi Rizzo 		a.interval.tv_nsec = 0;
370aa1cb3e1SRobert Watson 	} else if (rate == 1) {
371ed8c4b44SLuigi Rizzo 		a.interval.tv_sec = 1;
372ed8c4b44SLuigi Rizzo 		a.interval.tv_nsec = 0;
3732d832be0SRobert Watson 	} else {
374ed8c4b44SLuigi Rizzo 		a.interval.tv_sec = 0;
375ed8c4b44SLuigi Rizzo 		a.interval.tv_nsec = ((1 * 1000000000) / rate);
3762d832be0SRobert Watson 	}
3772d832be0SRobert Watson 
378ed8c4b44SLuigi Rizzo 	printf("Sending packet of payload size %ld every %jd.%09lds for %ld "
379ed8c4b44SLuigi Rizzo 	    "seconds\n", payloadsize, (intmax_t)a.interval.tv_sec,
380ed8c4b44SLuigi Rizzo 	    a.interval.tv_nsec, a.duration);
381ed8c4b44SLuigi Rizzo 
382c2a24727SOlivier Houchard 	if (a.ipv6)
383c2a24727SOlivier Houchard 		a.s = socket(PF_INET6, SOCK_DGRAM, 0);
384c2a24727SOlivier Houchard 	else
385ed8c4b44SLuigi Rizzo 		a.s = socket(PF_INET, SOCK_DGRAM, 0);
386ed8c4b44SLuigi Rizzo 	if (a.s == -1) {
3872d832be0SRobert Watson 		perror("socket");
3882d832be0SRobert Watson 		return (-1);
3892d832be0SRobert Watson 	}
3902d832be0SRobert Watson 
391ed8c4b44SLuigi Rizzo 	return (timing_loop(&a));
3922d832be0SRobert Watson }
393