xref: /freebsd/tools/tools/netmap/pkt-gen.c (revision 99fb123f)
168b8534bSLuigi Rizzo /*
268b8534bSLuigi Rizzo  * Copyright (C) 2011 Matteo Landi, Luigi Rizzo. All rights reserved.
368b8534bSLuigi Rizzo  *
468b8534bSLuigi Rizzo  * Redistribution and use in source and binary forms, with or without
568b8534bSLuigi Rizzo  * modification, are permitted provided that the following conditions
668b8534bSLuigi Rizzo  * are met:
768b8534bSLuigi Rizzo  *   1. Redistributions of source code must retain the above copyright
868b8534bSLuigi Rizzo  *      notice, this list of conditions and the following disclaimer.
968b8534bSLuigi Rizzo  *   2. Redistributions in binary form must reproduce the above copyright
1068b8534bSLuigi Rizzo  *      notice, this list of conditions and the following disclaimer in the
1168b8534bSLuigi Rizzo  *    documentation and/or other materials provided with the distribution.
1268b8534bSLuigi Rizzo  *
1368b8534bSLuigi Rizzo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1468b8534bSLuigi Rizzo  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1568b8534bSLuigi Rizzo  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1668b8534bSLuigi Rizzo  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1768b8534bSLuigi Rizzo  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1868b8534bSLuigi Rizzo  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1968b8534bSLuigi Rizzo  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2068b8534bSLuigi Rizzo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2168b8534bSLuigi Rizzo  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2268b8534bSLuigi Rizzo  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2368b8534bSLuigi Rizzo  * SUCH DAMAGE.
2468b8534bSLuigi Rizzo  */
2568b8534bSLuigi Rizzo 
2668b8534bSLuigi Rizzo /*
2768b8534bSLuigi Rizzo  * $FreeBSD$
2899fb123fSLuigi Rizzo  * $Id: pkt-gen.c 10967 2012-05-03 11:29:23Z luigi $
2968b8534bSLuigi Rizzo  *
3068b8534bSLuigi Rizzo  * Example program to show how to build a multithreaded packet
3168b8534bSLuigi Rizzo  * source/sink using the netmap device.
3268b8534bSLuigi Rizzo  *
3368b8534bSLuigi Rizzo  * In this example we create a programmable number of threads
3468b8534bSLuigi Rizzo  * to take care of all the queues of the interface used to
3568b8534bSLuigi Rizzo  * send or receive traffic.
3668b8534bSLuigi Rizzo  *
3768b8534bSLuigi Rizzo  */
3868b8534bSLuigi Rizzo 
3968b8534bSLuigi Rizzo const char *default_payload="netmap pkt-gen Luigi Rizzo and Matteo Landi\n"
4068b8534bSLuigi Rizzo 	"http://info.iet.unipi.it/~luigi/netmap/ ";
4168b8534bSLuigi Rizzo 
4268b8534bSLuigi Rizzo #include <errno.h>
4368b8534bSLuigi Rizzo #include <pthread.h>	/* pthread_* */
4468b8534bSLuigi Rizzo #include <pthread_np.h>	/* pthread w/ affinity */
4568b8534bSLuigi Rizzo #include <signal.h>	/* signal */
4668b8534bSLuigi Rizzo #include <stdlib.h>
4768b8534bSLuigi Rizzo #include <stdio.h>
48506cc70cSLuigi Rizzo #include <inttypes.h>	/* PRI* macros */
4968b8534bSLuigi Rizzo #include <string.h>	/* strcmp */
5068b8534bSLuigi Rizzo #include <fcntl.h>	/* open */
5168b8534bSLuigi Rizzo #include <unistd.h>	/* close */
5268b8534bSLuigi Rizzo #include <ifaddrs.h>	/* getifaddrs */
5368b8534bSLuigi Rizzo 
5468b8534bSLuigi Rizzo #include <sys/mman.h>	/* PROT_* */
5568b8534bSLuigi Rizzo #include <sys/ioctl.h>	/* ioctl */
5668b8534bSLuigi Rizzo #include <sys/poll.h>
5768b8534bSLuigi Rizzo #include <sys/socket.h>	/* sockaddr.. */
5868b8534bSLuigi Rizzo #include <arpa/inet.h>	/* ntohs */
5968b8534bSLuigi Rizzo #include <sys/param.h>
6068b8534bSLuigi Rizzo #include <sys/cpuset.h>	/* cpu_set */
6168b8534bSLuigi Rizzo #include <sys/sysctl.h>	/* sysctl */
6268b8534bSLuigi Rizzo #include <sys/time.h>	/* timersub */
6368b8534bSLuigi Rizzo 
6468b8534bSLuigi Rizzo #include <net/ethernet.h>
6568b8534bSLuigi Rizzo #include <net/if.h>	/* ifreq */
6668b8534bSLuigi Rizzo #include <net/if_dl.h>	/* LLADDR */
6768b8534bSLuigi Rizzo 
6868b8534bSLuigi Rizzo #include <netinet/in.h>
6968b8534bSLuigi Rizzo #include <netinet/ip.h>
7068b8534bSLuigi Rizzo #include <netinet/udp.h>
7168b8534bSLuigi Rizzo 
7268b8534bSLuigi Rizzo #include <net/netmap.h>
7368b8534bSLuigi Rizzo #include <net/netmap_user.h>
7468b8534bSLuigi Rizzo #include <pcap/pcap.h>
7568b8534bSLuigi Rizzo 
7668b8534bSLuigi Rizzo 
7768b8534bSLuigi Rizzo static inline int min(int a, int b) { return a < b ? a : b; }
7868b8534bSLuigi Rizzo 
7968b8534bSLuigi Rizzo /* debug support */
8068b8534bSLuigi Rizzo #define D(format, ...)				\
8168b8534bSLuigi Rizzo 	fprintf(stderr, "%s [%d] " format "\n", 	\
8268b8534bSLuigi Rizzo 	__FUNCTION__, __LINE__, ##__VA_ARGS__)
8368b8534bSLuigi Rizzo 
8468b8534bSLuigi Rizzo #ifndef EXPERIMENTAL
8568b8534bSLuigi Rizzo #define EXPERIMENTAL 0
8668b8534bSLuigi Rizzo #endif
8768b8534bSLuigi Rizzo 
8868b8534bSLuigi Rizzo int verbose = 0;
8968b8534bSLuigi Rizzo #define MAX_QUEUES 64	/* no need to limit */
9068b8534bSLuigi Rizzo 
9168b8534bSLuigi Rizzo #define SKIP_PAYLOAD 1 /* do not check payload. */
9268b8534bSLuigi Rizzo 
9399fb123fSLuigi Rizzo inline void prefetch (const void *x)
9499fb123fSLuigi Rizzo {
9599fb123fSLuigi Rizzo         __asm volatile("prefetcht0 %0" :: "m" (*(const unsigned long *)x));
9699fb123fSLuigi Rizzo }
9799fb123fSLuigi Rizzo 
9899fb123fSLuigi Rizzo // XXX only for multiples of 32 bytes, non overlapped.
9999fb123fSLuigi Rizzo static inline void
10099fb123fSLuigi Rizzo pkt_copy(void *_src, void *_dst, int l)
10199fb123fSLuigi Rizzo {
10299fb123fSLuigi Rizzo 	uint64_t *src = _src;
10399fb123fSLuigi Rizzo 	uint64_t *dst = _dst;
10499fb123fSLuigi Rizzo #define likely(x)       __builtin_expect(!!(x), 1)
10599fb123fSLuigi Rizzo #define unlikely(x)       __builtin_expect(!!(x), 0)
10699fb123fSLuigi Rizzo 	if (unlikely(l >= 1024)) {
10799fb123fSLuigi Rizzo 		bcopy(src, dst, l);
10899fb123fSLuigi Rizzo 		return;
10999fb123fSLuigi Rizzo 	}
11099fb123fSLuigi Rizzo 	for (; l > 0; l-=64) {
11199fb123fSLuigi Rizzo 		*dst++ = *src++;
11299fb123fSLuigi Rizzo 		*dst++ = *src++;
11399fb123fSLuigi Rizzo 		*dst++ = *src++;
11499fb123fSLuigi Rizzo 		*dst++ = *src++;
11599fb123fSLuigi Rizzo 		*dst++ = *src++;
11699fb123fSLuigi Rizzo 		*dst++ = *src++;
11799fb123fSLuigi Rizzo 		*dst++ = *src++;
11899fb123fSLuigi Rizzo 		*dst++ = *src++;
11999fb123fSLuigi Rizzo 	}
12099fb123fSLuigi Rizzo }
12199fb123fSLuigi Rizzo 
12299fb123fSLuigi Rizzo 
12368b8534bSLuigi Rizzo #if EXPERIMENTAL
12468b8534bSLuigi Rizzo /* Wrapper around `rdtsc' to take reliable timestamps flushing the pipeline */
12568b8534bSLuigi Rizzo #define netmap_rdtsc(t) \
12668b8534bSLuigi Rizzo 	do { \
12768b8534bSLuigi Rizzo 		u_int __regs[4];					\
12868b8534bSLuigi Rizzo 									\
12968b8534bSLuigi Rizzo 		do_cpuid(0, __regs);					\
13068b8534bSLuigi Rizzo 		(t) = rdtsc();						\
13168b8534bSLuigi Rizzo 	} while (0)
13268b8534bSLuigi Rizzo 
13368b8534bSLuigi Rizzo static __inline void
13468b8534bSLuigi Rizzo do_cpuid(u_int ax, u_int *p)
13568b8534bSLuigi Rizzo {
13668b8534bSLuigi Rizzo 	__asm __volatile("cpuid"
13768b8534bSLuigi Rizzo 			 : "=a" (p[0]), "=b" (p[1]), "=c" (p[2]), "=d" (p[3])
13868b8534bSLuigi Rizzo 			 :  "0" (ax));
13968b8534bSLuigi Rizzo }
14068b8534bSLuigi Rizzo 
14168b8534bSLuigi Rizzo static __inline uint64_t
14268b8534bSLuigi Rizzo rdtsc(void)
14368b8534bSLuigi Rizzo {
14468b8534bSLuigi Rizzo 	uint64_t rv;
14568b8534bSLuigi Rizzo 
14668b8534bSLuigi Rizzo 	__asm __volatile("rdtsc" : "=A" (rv));
14768b8534bSLuigi Rizzo 	return (rv);
14868b8534bSLuigi Rizzo }
14968b8534bSLuigi Rizzo #define MAX_SAMPLES 100000
15068b8534bSLuigi Rizzo #endif /* EXPERIMENTAL */
15168b8534bSLuigi Rizzo 
15268b8534bSLuigi Rizzo 
15368b8534bSLuigi Rizzo struct pkt {
15468b8534bSLuigi Rizzo 	struct ether_header eh;
15568b8534bSLuigi Rizzo 	struct ip ip;
15668b8534bSLuigi Rizzo 	struct udphdr udp;
1575819da83SLuigi Rizzo 	uint8_t body[2048];	// XXX hardwired
15868b8534bSLuigi Rizzo } __attribute__((__packed__));
15968b8534bSLuigi Rizzo 
16068b8534bSLuigi Rizzo /*
16168b8534bSLuigi Rizzo  * global arguments for all threads
16268b8534bSLuigi Rizzo  */
16368b8534bSLuigi Rizzo struct glob_arg {
16468b8534bSLuigi Rizzo 	const char *src_ip;
16568b8534bSLuigi Rizzo 	const char *dst_ip;
16668b8534bSLuigi Rizzo 	const char *src_mac;
16768b8534bSLuigi Rizzo 	const char *dst_mac;
16868b8534bSLuigi Rizzo 	int pkt_size;
16968b8534bSLuigi Rizzo 	int burst;
17068b8534bSLuigi Rizzo 	int npackets;	/* total packets to send */
17168b8534bSLuigi Rizzo 	int nthreads;
17268b8534bSLuigi Rizzo 	int cpus;
17399fb123fSLuigi Rizzo 	int options;	/* testing */
17499fb123fSLuigi Rizzo #define OPT_PREFETCH	1
17599fb123fSLuigi Rizzo #define OPT_ACCESS	2
17699fb123fSLuigi Rizzo #define OPT_COPY	4
17799fb123fSLuigi Rizzo #define OPT_MEMCPY	8
17868b8534bSLuigi Rizzo 	int use_pcap;
17968b8534bSLuigi Rizzo 	pcap_t *p;
18068b8534bSLuigi Rizzo };
18168b8534bSLuigi Rizzo 
18268b8534bSLuigi Rizzo struct mystat {
18368b8534bSLuigi Rizzo 	uint64_t containers[8];
18468b8534bSLuigi Rizzo };
18568b8534bSLuigi Rizzo 
18668b8534bSLuigi Rizzo /*
18768b8534bSLuigi Rizzo  * Arguments for a new thread. The same structure is used by
18868b8534bSLuigi Rizzo  * the source and the sink
18968b8534bSLuigi Rizzo  */
19068b8534bSLuigi Rizzo struct targ {
19168b8534bSLuigi Rizzo 	struct glob_arg *g;
19268b8534bSLuigi Rizzo 	int used;
19368b8534bSLuigi Rizzo 	int completed;
19468b8534bSLuigi Rizzo 	int fd;
19568b8534bSLuigi Rizzo 	struct nmreq nmr;
19668b8534bSLuigi Rizzo 	struct netmap_if *nifp;
19768b8534bSLuigi Rizzo 	uint16_t	qfirst, qlast; /* range of queues to scan */
19868b8534bSLuigi Rizzo 	uint64_t count;
19968b8534bSLuigi Rizzo 	struct timeval tic, toc;
20068b8534bSLuigi Rizzo 	int me;
20168b8534bSLuigi Rizzo 	pthread_t thread;
20268b8534bSLuigi Rizzo 	int affinity;
20368b8534bSLuigi Rizzo 
20468b8534bSLuigi Rizzo 	uint8_t	dst_mac[6];
20568b8534bSLuigi Rizzo 	uint8_t	src_mac[6];
20668b8534bSLuigi Rizzo 	u_int dst_mac_range;
20768b8534bSLuigi Rizzo 	u_int src_mac_range;
20868b8534bSLuigi Rizzo 	uint32_t dst_ip;
20968b8534bSLuigi Rizzo 	uint32_t src_ip;
21068b8534bSLuigi Rizzo 	u_int dst_ip_range;
21168b8534bSLuigi Rizzo 	u_int src_ip_range;
21268b8534bSLuigi Rizzo 
21368b8534bSLuigi Rizzo 	struct pkt pkt;
21468b8534bSLuigi Rizzo };
21568b8534bSLuigi Rizzo 
21668b8534bSLuigi Rizzo 
21768b8534bSLuigi Rizzo static struct targ *targs;
21868b8534bSLuigi Rizzo static int global_nthreads;
21968b8534bSLuigi Rizzo 
22068b8534bSLuigi Rizzo /* control-C handler */
22168b8534bSLuigi Rizzo static void
22268b8534bSLuigi Rizzo sigint_h(__unused int sig)
22368b8534bSLuigi Rizzo {
22468b8534bSLuigi Rizzo 	for (int i = 0; i < global_nthreads; i++) {
22568b8534bSLuigi Rizzo 		/* cancel active threads. */
22668b8534bSLuigi Rizzo 		if (targs[i].used == 0)
22768b8534bSLuigi Rizzo 			continue;
22868b8534bSLuigi Rizzo 
22968b8534bSLuigi Rizzo 		D("Cancelling thread #%d\n", i);
23068b8534bSLuigi Rizzo 		pthread_cancel(targs[i].thread);
23168b8534bSLuigi Rizzo 		targs[i].used = 0;
23268b8534bSLuigi Rizzo 	}
23368b8534bSLuigi Rizzo 
23468b8534bSLuigi Rizzo 	signal(SIGINT, SIG_DFL);
23568b8534bSLuigi Rizzo }
23668b8534bSLuigi Rizzo 
23768b8534bSLuigi Rizzo 
23868b8534bSLuigi Rizzo /* sysctl wrapper to return the number of active CPUs */
23968b8534bSLuigi Rizzo static int
24068b8534bSLuigi Rizzo system_ncpus(void)
24168b8534bSLuigi Rizzo {
24268b8534bSLuigi Rizzo 	int mib[2], ncpus;
24368b8534bSLuigi Rizzo 	size_t len;
24468b8534bSLuigi Rizzo 
24568b8534bSLuigi Rizzo 	mib[0] = CTL_HW;
24668b8534bSLuigi Rizzo 	mib[1] = HW_NCPU;
24768b8534bSLuigi Rizzo 	len = sizeof(mib);
24868b8534bSLuigi Rizzo 	sysctl(mib, 2, &ncpus, &len, NULL, 0);
24968b8534bSLuigi Rizzo 
25068b8534bSLuigi Rizzo 	return (ncpus);
25168b8534bSLuigi Rizzo }
25268b8534bSLuigi Rizzo 
25368b8534bSLuigi Rizzo /*
25468b8534bSLuigi Rizzo  * locate the src mac address for our interface, put it
25568b8534bSLuigi Rizzo  * into the user-supplied buffer. return 0 if ok, -1 on error.
25668b8534bSLuigi Rizzo  */
25768b8534bSLuigi Rizzo static int
25868b8534bSLuigi Rizzo source_hwaddr(const char *ifname, char *buf)
25968b8534bSLuigi Rizzo {
26068b8534bSLuigi Rizzo 	struct ifaddrs *ifaphead, *ifap;
26168b8534bSLuigi Rizzo 	int l = sizeof(ifap->ifa_name);
26268b8534bSLuigi Rizzo 
26368b8534bSLuigi Rizzo 	if (getifaddrs(&ifaphead) != 0) {
26468b8534bSLuigi Rizzo 		D("getifaddrs %s failed", ifname);
26568b8534bSLuigi Rizzo 		return (-1);
26668b8534bSLuigi Rizzo 	}
26768b8534bSLuigi Rizzo 
26868b8534bSLuigi Rizzo 	for (ifap = ifaphead; ifap; ifap = ifap->ifa_next) {
26968b8534bSLuigi Rizzo 		struct sockaddr_dl *sdl =
27068b8534bSLuigi Rizzo 			(struct sockaddr_dl *)ifap->ifa_addr;
27168b8534bSLuigi Rizzo 		uint8_t *mac;
27268b8534bSLuigi Rizzo 
27368b8534bSLuigi Rizzo 		if (!sdl || sdl->sdl_family != AF_LINK)
27468b8534bSLuigi Rizzo 			continue;
27568b8534bSLuigi Rizzo 		if (strncmp(ifap->ifa_name, ifname, l) != 0)
27668b8534bSLuigi Rizzo 			continue;
27768b8534bSLuigi Rizzo 		mac = (uint8_t *)LLADDR(sdl);
27868b8534bSLuigi Rizzo 		sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x",
27968b8534bSLuigi Rizzo 			mac[0], mac[1], mac[2],
28068b8534bSLuigi Rizzo 			mac[3], mac[4], mac[5]);
28168b8534bSLuigi Rizzo 		if (verbose)
28268b8534bSLuigi Rizzo 			D("source hwaddr %s", buf);
28368b8534bSLuigi Rizzo 		break;
28468b8534bSLuigi Rizzo 	}
28568b8534bSLuigi Rizzo 	freeifaddrs(ifaphead);
28668b8534bSLuigi Rizzo 	return ifap ? 0 : 1;
28768b8534bSLuigi Rizzo }
28868b8534bSLuigi Rizzo 
28968b8534bSLuigi Rizzo 
29068b8534bSLuigi Rizzo /* set the thread affinity. */
29168b8534bSLuigi Rizzo static int
29268b8534bSLuigi Rizzo setaffinity(pthread_t me, int i)
29368b8534bSLuigi Rizzo {
29468b8534bSLuigi Rizzo 	cpuset_t cpumask;
29568b8534bSLuigi Rizzo 
29668b8534bSLuigi Rizzo 	if (i == -1)
29768b8534bSLuigi Rizzo 		return 0;
29868b8534bSLuigi Rizzo 
29968b8534bSLuigi Rizzo 	/* Set thread affinity affinity.*/
30068b8534bSLuigi Rizzo 	CPU_ZERO(&cpumask);
30168b8534bSLuigi Rizzo 	CPU_SET(i, &cpumask);
30268b8534bSLuigi Rizzo 
30368b8534bSLuigi Rizzo 	if (pthread_setaffinity_np(me, sizeof(cpuset_t), &cpumask) != 0) {
30468b8534bSLuigi Rizzo 		D("Unable to set affinity");
30568b8534bSLuigi Rizzo 		return 1;
30668b8534bSLuigi Rizzo 	}
30768b8534bSLuigi Rizzo 	return 0;
30868b8534bSLuigi Rizzo }
30968b8534bSLuigi Rizzo 
31068b8534bSLuigi Rizzo /* Compute the checksum of the given ip header. */
31168b8534bSLuigi Rizzo static uint16_t
31268b8534bSLuigi Rizzo checksum(const void *data, uint16_t len)
31368b8534bSLuigi Rizzo {
31468b8534bSLuigi Rizzo         const uint8_t *addr = data;
31568b8534bSLuigi Rizzo         uint32_t sum = 0;
31668b8534bSLuigi Rizzo 
31768b8534bSLuigi Rizzo         while (len > 1) {
31868b8534bSLuigi Rizzo                 sum += addr[0] * 256 + addr[1];
31968b8534bSLuigi Rizzo                 addr += 2;
32068b8534bSLuigi Rizzo                 len -= 2;
32168b8534bSLuigi Rizzo         }
32268b8534bSLuigi Rizzo 
32368b8534bSLuigi Rizzo         if (len == 1)
32468b8534bSLuigi Rizzo                 sum += *addr * 256;
32568b8534bSLuigi Rizzo 
32668b8534bSLuigi Rizzo         sum = (sum >> 16) + (sum & 0xffff);
32768b8534bSLuigi Rizzo         sum += (sum >> 16);
32868b8534bSLuigi Rizzo 
32968b8534bSLuigi Rizzo         sum = htons(sum);
33068b8534bSLuigi Rizzo 
33168b8534bSLuigi Rizzo         return ~sum;
33268b8534bSLuigi Rizzo }
33368b8534bSLuigi Rizzo 
33468b8534bSLuigi Rizzo /*
33568b8534bSLuigi Rizzo  * Fill a packet with some payload.
33668b8534bSLuigi Rizzo  */
33768b8534bSLuigi Rizzo static void
33868b8534bSLuigi Rizzo initialize_packet(struct targ *targ)
33968b8534bSLuigi Rizzo {
34068b8534bSLuigi Rizzo 	struct pkt *pkt = &targ->pkt;
34168b8534bSLuigi Rizzo 	struct ether_header *eh;
34268b8534bSLuigi Rizzo 	struct ip *ip;
34368b8534bSLuigi Rizzo 	struct udphdr *udp;
34468b8534bSLuigi Rizzo 	uint16_t paylen = targ->g->pkt_size - sizeof(*eh) - sizeof(*ip);
34568b8534bSLuigi Rizzo 	int i, l, l0 = strlen(default_payload);
34668b8534bSLuigi Rizzo 	char *p;
34768b8534bSLuigi Rizzo 
34868b8534bSLuigi Rizzo 	for (i = 0; i < paylen;) {
34968b8534bSLuigi Rizzo 		l = min(l0, paylen - i);
35068b8534bSLuigi Rizzo 		bcopy(default_payload, pkt->body + i, l);
35168b8534bSLuigi Rizzo 		i += l;
35268b8534bSLuigi Rizzo 	}
35368b8534bSLuigi Rizzo 	pkt->body[i-1] = '\0';
35468b8534bSLuigi Rizzo 
35568b8534bSLuigi Rizzo 	udp = &pkt->udp;
35668b8534bSLuigi Rizzo 	udp->uh_sport = htons(1234);
35768b8534bSLuigi Rizzo         udp->uh_dport = htons(4321);
35868b8534bSLuigi Rizzo 	udp->uh_ulen = htons(paylen);
35968b8534bSLuigi Rizzo 	udp->uh_sum = 0; // checksum(udp, sizeof(*udp));
36068b8534bSLuigi Rizzo 
36168b8534bSLuigi Rizzo 	ip = &pkt->ip;
36268b8534bSLuigi Rizzo         ip->ip_v = IPVERSION;
36368b8534bSLuigi Rizzo         ip->ip_hl = 5;
36468b8534bSLuigi Rizzo         ip->ip_id = 0;
36568b8534bSLuigi Rizzo         ip->ip_tos = IPTOS_LOWDELAY;
36668b8534bSLuigi Rizzo 	ip->ip_len = ntohs(targ->g->pkt_size - sizeof(*eh));
36768b8534bSLuigi Rizzo         ip->ip_id = 0;
36868b8534bSLuigi Rizzo         ip->ip_off = htons(IP_DF); /* Don't fragment */
36968b8534bSLuigi Rizzo         ip->ip_ttl = IPDEFTTL;
37068b8534bSLuigi Rizzo 	ip->ip_p = IPPROTO_UDP;
37168b8534bSLuigi Rizzo 	inet_aton(targ->g->src_ip, (struct in_addr *)&ip->ip_src);
37268b8534bSLuigi Rizzo 	inet_aton(targ->g->dst_ip, (struct in_addr *)&ip->ip_dst);
37368b8534bSLuigi Rizzo 	targ->dst_ip = ip->ip_dst.s_addr;
37468b8534bSLuigi Rizzo 	targ->src_ip = ip->ip_src.s_addr;
37568b8534bSLuigi Rizzo 	p = index(targ->g->src_ip, '-');
37668b8534bSLuigi Rizzo 	if (p) {
37768b8534bSLuigi Rizzo 		targ->dst_ip_range = atoi(p+1);
37868b8534bSLuigi Rizzo 		D("dst-ip sweep %d addresses", targ->dst_ip_range);
37968b8534bSLuigi Rizzo 	}
38068b8534bSLuigi Rizzo 	ip->ip_sum = checksum(ip, sizeof(*ip));
38168b8534bSLuigi Rizzo 
38268b8534bSLuigi Rizzo 	eh = &pkt->eh;
38368b8534bSLuigi Rizzo 	bcopy(ether_aton(targ->g->src_mac), targ->src_mac, 6);
38468b8534bSLuigi Rizzo 	bcopy(targ->src_mac, eh->ether_shost, 6);
38568b8534bSLuigi Rizzo 	p = index(targ->g->src_mac, '-');
38668b8534bSLuigi Rizzo 	if (p)
38768b8534bSLuigi Rizzo 		targ->src_mac_range = atoi(p+1);
38868b8534bSLuigi Rizzo 
38968b8534bSLuigi Rizzo 	bcopy(ether_aton(targ->g->dst_mac), targ->dst_mac, 6);
39068b8534bSLuigi Rizzo 	bcopy(targ->dst_mac, eh->ether_dhost, 6);
39168b8534bSLuigi Rizzo 	p = index(targ->g->dst_mac, '-');
39268b8534bSLuigi Rizzo 	if (p)
39368b8534bSLuigi Rizzo 		targ->dst_mac_range = atoi(p+1);
39468b8534bSLuigi Rizzo 	eh->ether_type = htons(ETHERTYPE_IP);
39568b8534bSLuigi Rizzo }
39668b8534bSLuigi Rizzo 
39768b8534bSLuigi Rizzo /* Check the payload of the packet for errors (use it for debug).
39868b8534bSLuigi Rizzo  * Look for consecutive ascii representations of the size of the packet.
39968b8534bSLuigi Rizzo  */
40068b8534bSLuigi Rizzo static void
40168b8534bSLuigi Rizzo check_payload(char *p, int psize)
40268b8534bSLuigi Rizzo {
40368b8534bSLuigi Rizzo 	char temp[64];
40468b8534bSLuigi Rizzo 	int n_read, size, sizelen;
40568b8534bSLuigi Rizzo 
40668b8534bSLuigi Rizzo 	/* get the length in ASCII of the length of the packet. */
40768b8534bSLuigi Rizzo 	sizelen = sprintf(temp, "%d", psize) + 1; // include a whitespace
40868b8534bSLuigi Rizzo 
40968b8534bSLuigi Rizzo 	/* dummy payload. */
41068b8534bSLuigi Rizzo 	p += 14; /* skip packet header. */
41168b8534bSLuigi Rizzo 	n_read = 14;
41268b8534bSLuigi Rizzo 	while (psize - n_read >= sizelen) {
41368b8534bSLuigi Rizzo 		sscanf(p, "%d", &size);
41468b8534bSLuigi Rizzo 		if (size != psize) {
41568b8534bSLuigi Rizzo 			D("Read %d instead of %d", size, psize);
41668b8534bSLuigi Rizzo 			break;
41768b8534bSLuigi Rizzo 		}
41868b8534bSLuigi Rizzo 
41968b8534bSLuigi Rizzo 		p += sizelen;
42068b8534bSLuigi Rizzo 		n_read += sizelen;
42168b8534bSLuigi Rizzo 	}
42268b8534bSLuigi Rizzo }
42368b8534bSLuigi Rizzo 
42468b8534bSLuigi Rizzo 
42568b8534bSLuigi Rizzo /*
42668b8534bSLuigi Rizzo  * create and enqueue a batch of packets on a ring.
42768b8534bSLuigi Rizzo  * On the last one set NS_REPORT to tell the driver to generate
42868b8534bSLuigi Rizzo  * an interrupt when done.
42968b8534bSLuigi Rizzo  */
43068b8534bSLuigi Rizzo static int
43168b8534bSLuigi Rizzo send_packets(struct netmap_ring *ring, struct pkt *pkt,
43299fb123fSLuigi Rizzo 		int size, u_int count, int options)
43368b8534bSLuigi Rizzo {
43468b8534bSLuigi Rizzo 	u_int sent, cur = ring->cur;
43568b8534bSLuigi Rizzo 
43668b8534bSLuigi Rizzo 	if (ring->avail < count)
43768b8534bSLuigi Rizzo 		count = ring->avail;
43868b8534bSLuigi Rizzo 
43999fb123fSLuigi Rizzo #if 0
44099fb123fSLuigi Rizzo 	if (options & (OPT_COPY | OPT_PREFETCH) ) {
44168b8534bSLuigi Rizzo 		for (sent = 0; sent < count; sent++) {
44268b8534bSLuigi Rizzo 			struct netmap_slot *slot = &ring->slot[cur];
44368b8534bSLuigi Rizzo 			char *p = NETMAP_BUF(ring, slot->buf_idx);
44468b8534bSLuigi Rizzo 
44599fb123fSLuigi Rizzo 			prefetch(p);
44699fb123fSLuigi Rizzo 			cur = NETMAP_RING_NEXT(ring, cur);
44799fb123fSLuigi Rizzo 		}
44899fb123fSLuigi Rizzo 		cur = ring->cur;
44999fb123fSLuigi Rizzo 	}
45099fb123fSLuigi Rizzo #endif
45199fb123fSLuigi Rizzo 	for (sent = 0; sent < count; sent++) {
45299fb123fSLuigi Rizzo 		struct netmap_slot *slot = &ring->slot[cur];
45399fb123fSLuigi Rizzo 		char *p = NETMAP_BUF(ring, slot->buf_idx);
45499fb123fSLuigi Rizzo 
45599fb123fSLuigi Rizzo 		if (options & OPT_COPY)
45699fb123fSLuigi Rizzo 			pkt_copy(pkt, p, size);
45799fb123fSLuigi Rizzo 		else if (options & OPT_MEMCPY)
45868b8534bSLuigi Rizzo 			memcpy(p, pkt, size);
45999fb123fSLuigi Rizzo 		else if (options & OPT_PREFETCH)
46099fb123fSLuigi Rizzo 			prefetch(p);
46168b8534bSLuigi Rizzo 
46268b8534bSLuigi Rizzo 		slot->len = size;
46368b8534bSLuigi Rizzo 		if (sent == count - 1)
46468b8534bSLuigi Rizzo 			slot->flags |= NS_REPORT;
46568b8534bSLuigi Rizzo 		cur = NETMAP_RING_NEXT(ring, cur);
46668b8534bSLuigi Rizzo 	}
46768b8534bSLuigi Rizzo 	ring->avail -= sent;
46868b8534bSLuigi Rizzo 	ring->cur = cur;
46968b8534bSLuigi Rizzo 
47068b8534bSLuigi Rizzo 	return (sent);
47168b8534bSLuigi Rizzo }
47268b8534bSLuigi Rizzo 
47368b8534bSLuigi Rizzo static void *
47468b8534bSLuigi Rizzo sender_body(void *data)
47568b8534bSLuigi Rizzo {
47668b8534bSLuigi Rizzo 	struct targ *targ = (struct targ *) data;
47768b8534bSLuigi Rizzo 
47868b8534bSLuigi Rizzo 	struct pollfd fds[1];
47968b8534bSLuigi Rizzo 	struct netmap_if *nifp = targ->nifp;
48068b8534bSLuigi Rizzo 	struct netmap_ring *txring;
48168b8534bSLuigi Rizzo 	int i, n = targ->g->npackets / targ->g->nthreads, sent = 0;
48299fb123fSLuigi Rizzo 	int options = targ->g->options | OPT_COPY;
48399fb123fSLuigi Rizzo D("start");
48468b8534bSLuigi Rizzo 	if (setaffinity(targ->thread, targ->affinity))
48568b8534bSLuigi Rizzo 		goto quit;
4868ce070c1SUlrich Spörlein 	/* setup poll(2) mechanism. */
48768b8534bSLuigi Rizzo 	memset(fds, 0, sizeof(fds));
48868b8534bSLuigi Rizzo 	fds[0].fd = targ->fd;
48968b8534bSLuigi Rizzo 	fds[0].events = (POLLOUT);
49068b8534bSLuigi Rizzo 
49168b8534bSLuigi Rizzo 	/* main loop.*/
49268b8534bSLuigi Rizzo 	gettimeofday(&targ->tic, NULL);
49368b8534bSLuigi Rizzo     if (targ->g->use_pcap) {
49468b8534bSLuigi Rizzo 	int size = targ->g->pkt_size;
49568b8534bSLuigi Rizzo 	void *pkt = &targ->pkt;
49668b8534bSLuigi Rizzo 	pcap_t *p = targ->g->p;
49768b8534bSLuigi Rizzo 
49899fb123fSLuigi Rizzo 	for (i = 0; sent < n; i++) {
49999fb123fSLuigi Rizzo 		if (pcap_inject(p, pkt, size) != -1)
50099fb123fSLuigi Rizzo 			sent++;
50199fb123fSLuigi Rizzo 		if (i > 10000) {
50299fb123fSLuigi Rizzo 			targ->count = sent;
50399fb123fSLuigi Rizzo 			i = 0;
50499fb123fSLuigi Rizzo 		}
50568b8534bSLuigi Rizzo 	}
50668b8534bSLuigi Rizzo     } else {
50768b8534bSLuigi Rizzo 	while (sent < n) {
50868b8534bSLuigi Rizzo 
50968b8534bSLuigi Rizzo 		/*
51068b8534bSLuigi Rizzo 		 * wait for available room in the send queue(s)
51168b8534bSLuigi Rizzo 		 */
51268b8534bSLuigi Rizzo 		if (poll(fds, 1, 2000) <= 0) {
51368b8534bSLuigi Rizzo 			D("poll error/timeout on queue %d\n", targ->me);
51468b8534bSLuigi Rizzo 			goto quit;
51568b8534bSLuigi Rizzo 		}
51668b8534bSLuigi Rizzo 		/*
51768b8534bSLuigi Rizzo 		 * scan our queues and send on those with room
51868b8534bSLuigi Rizzo 		 */
51999fb123fSLuigi Rizzo 		if (sent > 100000 && !(targ->g->options & OPT_COPY) )
52099fb123fSLuigi Rizzo 			options &= ~OPT_COPY;
52168b8534bSLuigi Rizzo 		for (i = targ->qfirst; i < targ->qlast; i++) {
52268b8534bSLuigi Rizzo 			int m, limit = MIN(n - sent, targ->g->burst);
52368b8534bSLuigi Rizzo 
52468b8534bSLuigi Rizzo 			txring = NETMAP_TXRING(nifp, i);
52568b8534bSLuigi Rizzo 			if (txring->avail == 0)
52668b8534bSLuigi Rizzo 				continue;
52768b8534bSLuigi Rizzo 			m = send_packets(txring, &targ->pkt, targ->g->pkt_size,
52899fb123fSLuigi Rizzo 					 limit, options);
52968b8534bSLuigi Rizzo 			sent += m;
53068b8534bSLuigi Rizzo 			targ->count = sent;
53168b8534bSLuigi Rizzo 		}
53268b8534bSLuigi Rizzo 	}
53399fb123fSLuigi Rizzo 	/* flush any remaining packets */
53468b8534bSLuigi Rizzo 	ioctl(fds[0].fd, NIOCTXSYNC, NULL);
53568b8534bSLuigi Rizzo 
53668b8534bSLuigi Rizzo 	/* final part: wait all the TX queues to be empty. */
53768b8534bSLuigi Rizzo 	for (i = targ->qfirst; i < targ->qlast; i++) {
53868b8534bSLuigi Rizzo 		txring = NETMAP_TXRING(nifp, i);
53968b8534bSLuigi Rizzo 		while (!NETMAP_TX_RING_EMPTY(txring)) {
54068b8534bSLuigi Rizzo 			ioctl(fds[0].fd, NIOCTXSYNC, NULL);
54168b8534bSLuigi Rizzo 			usleep(1); /* wait 1 tick */
54268b8534bSLuigi Rizzo 		}
54368b8534bSLuigi Rizzo 	}
54468b8534bSLuigi Rizzo     }
54568b8534bSLuigi Rizzo 
54668b8534bSLuigi Rizzo 	gettimeofday(&targ->toc, NULL);
54768b8534bSLuigi Rizzo 	targ->completed = 1;
54868b8534bSLuigi Rizzo 	targ->count = sent;
54968b8534bSLuigi Rizzo 
55068b8534bSLuigi Rizzo quit:
55168b8534bSLuigi Rizzo 	/* reset the ``used`` flag. */
55268b8534bSLuigi Rizzo 	targ->used = 0;
55368b8534bSLuigi Rizzo 
55468b8534bSLuigi Rizzo 	return (NULL);
55568b8534bSLuigi Rizzo }
55668b8534bSLuigi Rizzo 
55768b8534bSLuigi Rizzo 
55868b8534bSLuigi Rizzo static void
55968b8534bSLuigi Rizzo receive_pcap(u_char *user, __unused const struct pcap_pkthdr * h,
56068b8534bSLuigi Rizzo 	__unused const u_char * bytes)
56168b8534bSLuigi Rizzo {
56268b8534bSLuigi Rizzo 	int *count = (int *)user;
56368b8534bSLuigi Rizzo 	(*count)++;
56468b8534bSLuigi Rizzo }
56568b8534bSLuigi Rizzo 
56668b8534bSLuigi Rizzo static int
56768b8534bSLuigi Rizzo receive_packets(struct netmap_ring *ring, u_int limit, int skip_payload)
56868b8534bSLuigi Rizzo {
56968b8534bSLuigi Rizzo 	u_int cur, rx;
57068b8534bSLuigi Rizzo 
57168b8534bSLuigi Rizzo 	cur = ring->cur;
57268b8534bSLuigi Rizzo 	if (ring->avail < limit)
57368b8534bSLuigi Rizzo 		limit = ring->avail;
57468b8534bSLuigi Rizzo 	for (rx = 0; rx < limit; rx++) {
57568b8534bSLuigi Rizzo 		struct netmap_slot *slot = &ring->slot[cur];
57668b8534bSLuigi Rizzo 		char *p = NETMAP_BUF(ring, slot->buf_idx);
57768b8534bSLuigi Rizzo 
57868b8534bSLuigi Rizzo 		if (!skip_payload)
57968b8534bSLuigi Rizzo 			check_payload(p, slot->len);
58068b8534bSLuigi Rizzo 
58168b8534bSLuigi Rizzo 		cur = NETMAP_RING_NEXT(ring, cur);
58268b8534bSLuigi Rizzo 	}
58368b8534bSLuigi Rizzo 	ring->avail -= rx;
58468b8534bSLuigi Rizzo 	ring->cur = cur;
58568b8534bSLuigi Rizzo 
58668b8534bSLuigi Rizzo 	return (rx);
58768b8534bSLuigi Rizzo }
58868b8534bSLuigi Rizzo 
58968b8534bSLuigi Rizzo static void *
59068b8534bSLuigi Rizzo receiver_body(void *data)
59168b8534bSLuigi Rizzo {
59268b8534bSLuigi Rizzo 	struct targ *targ = (struct targ *) data;
59368b8534bSLuigi Rizzo 	struct pollfd fds[1];
59468b8534bSLuigi Rizzo 	struct netmap_if *nifp = targ->nifp;
59568b8534bSLuigi Rizzo 	struct netmap_ring *rxring;
59668b8534bSLuigi Rizzo 	int i, received = 0;
59768b8534bSLuigi Rizzo 
59868b8534bSLuigi Rizzo 	if (setaffinity(targ->thread, targ->affinity))
59968b8534bSLuigi Rizzo 		goto quit;
60068b8534bSLuigi Rizzo 
6018ce070c1SUlrich Spörlein 	/* setup poll(2) mechanism. */
60268b8534bSLuigi Rizzo 	memset(fds, 0, sizeof(fds));
60368b8534bSLuigi Rizzo 	fds[0].fd = targ->fd;
60468b8534bSLuigi Rizzo 	fds[0].events = (POLLIN);
60568b8534bSLuigi Rizzo 
60668b8534bSLuigi Rizzo 	/* unbounded wait for the first packet. */
60768b8534bSLuigi Rizzo 	for (;;) {
60868b8534bSLuigi Rizzo 		i = poll(fds, 1, 1000);
60968b8534bSLuigi Rizzo 		if (i > 0 && !(fds[0].revents & POLLERR))
61068b8534bSLuigi Rizzo 			break;
61168b8534bSLuigi Rizzo 		D("waiting for initial packets, poll returns %d %d", i, fds[0].revents);
61268b8534bSLuigi Rizzo 	}
61368b8534bSLuigi Rizzo 
61468b8534bSLuigi Rizzo 	/* main loop, exit after 1s silence */
61568b8534bSLuigi Rizzo 	gettimeofday(&targ->tic, NULL);
61668b8534bSLuigi Rizzo     if (targ->g->use_pcap) {
61768b8534bSLuigi Rizzo 	for (;;) {
61868b8534bSLuigi Rizzo 		pcap_dispatch(targ->g->p, targ->g->burst, receive_pcap, NULL);
61968b8534bSLuigi Rizzo 	}
62068b8534bSLuigi Rizzo     } else {
62168b8534bSLuigi Rizzo 	while (1) {
62268b8534bSLuigi Rizzo 		/* Once we started to receive packets, wait at most 1 seconds
62368b8534bSLuigi Rizzo 		   before quitting. */
62468b8534bSLuigi Rizzo 		if (poll(fds, 1, 1 * 1000) <= 0) {
62568b8534bSLuigi Rizzo 			gettimeofday(&targ->toc, NULL);
6268ce070c1SUlrich Spörlein 			targ->toc.tv_sec -= 1; /* Subtract timeout time. */
62768b8534bSLuigi Rizzo 			break;
62868b8534bSLuigi Rizzo 		}
62968b8534bSLuigi Rizzo 
63068b8534bSLuigi Rizzo 		for (i = targ->qfirst; i < targ->qlast; i++) {
63168b8534bSLuigi Rizzo 			int m;
63268b8534bSLuigi Rizzo 
63368b8534bSLuigi Rizzo 			rxring = NETMAP_RXRING(nifp, i);
63468b8534bSLuigi Rizzo 			if (rxring->avail == 0)
63568b8534bSLuigi Rizzo 				continue;
63668b8534bSLuigi Rizzo 
63768b8534bSLuigi Rizzo 			m = receive_packets(rxring, targ->g->burst,
63868b8534bSLuigi Rizzo 					SKIP_PAYLOAD);
63968b8534bSLuigi Rizzo 			received += m;
64068b8534bSLuigi Rizzo 			targ->count = received;
64168b8534bSLuigi Rizzo 		}
64268b8534bSLuigi Rizzo 
64368b8534bSLuigi Rizzo 		// tell the card we have read the data
64468b8534bSLuigi Rizzo 		//ioctl(fds[0].fd, NIOCRXSYNC, NULL);
64568b8534bSLuigi Rizzo 	}
64668b8534bSLuigi Rizzo     }
64768b8534bSLuigi Rizzo 
64868b8534bSLuigi Rizzo 	targ->completed = 1;
64968b8534bSLuigi Rizzo 	targ->count = received;
65068b8534bSLuigi Rizzo 
65168b8534bSLuigi Rizzo quit:
65268b8534bSLuigi Rizzo 	/* reset the ``used`` flag. */
65368b8534bSLuigi Rizzo 	targ->used = 0;
65468b8534bSLuigi Rizzo 
65568b8534bSLuigi Rizzo 	return (NULL);
65668b8534bSLuigi Rizzo }
65768b8534bSLuigi Rizzo 
65868b8534bSLuigi Rizzo static void
65968b8534bSLuigi Rizzo tx_output(uint64_t sent, int size, double delta)
66068b8534bSLuigi Rizzo {
66168b8534bSLuigi Rizzo 	double amount = 8.0 * (1.0 * size * sent) / delta;
66268b8534bSLuigi Rizzo 	double pps = sent / delta;
66368b8534bSLuigi Rizzo 	char units[4] = { '\0', 'K', 'M', 'G' };
66468b8534bSLuigi Rizzo 	int aunit = 0, punit = 0;
66568b8534bSLuigi Rizzo 
66668b8534bSLuigi Rizzo 	while (amount >= 1000) {
66768b8534bSLuigi Rizzo 		amount /= 1000;
66868b8534bSLuigi Rizzo 		aunit += 1;
66968b8534bSLuigi Rizzo 	}
67068b8534bSLuigi Rizzo 	while (pps >= 1000) {
67168b8534bSLuigi Rizzo 		pps /= 1000;
67268b8534bSLuigi Rizzo 		punit += 1;
67368b8534bSLuigi Rizzo 	}
67468b8534bSLuigi Rizzo 
675506cc70cSLuigi Rizzo 	printf("Sent %" PRIu64 " packets, %d bytes each, in %.2f seconds.\n",
67668b8534bSLuigi Rizzo 	       sent, size, delta);
67768b8534bSLuigi Rizzo 	printf("Speed: %.2f%cpps. Bandwidth: %.2f%cbps.\n",
67868b8534bSLuigi Rizzo 	       pps, units[punit], amount, units[aunit]);
67968b8534bSLuigi Rizzo }
68068b8534bSLuigi Rizzo 
68168b8534bSLuigi Rizzo 
68268b8534bSLuigi Rizzo static void
68368b8534bSLuigi Rizzo rx_output(uint64_t received, double delta)
68468b8534bSLuigi Rizzo {
68568b8534bSLuigi Rizzo 
68668b8534bSLuigi Rizzo 	double pps = received / delta;
68768b8534bSLuigi Rizzo 	char units[4] = { '\0', 'K', 'M', 'G' };
68868b8534bSLuigi Rizzo 	int punit = 0;
68968b8534bSLuigi Rizzo 
69068b8534bSLuigi Rizzo 	while (pps >= 1000) {
69168b8534bSLuigi Rizzo 		pps /= 1000;
69268b8534bSLuigi Rizzo 		punit += 1;
69368b8534bSLuigi Rizzo 	}
69468b8534bSLuigi Rizzo 
695506cc70cSLuigi Rizzo 	printf("Received %" PRIu64 " packets, in %.2f seconds.\n", received, delta);
69668b8534bSLuigi Rizzo 	printf("Speed: %.2f%cpps.\n", pps, units[punit]);
69768b8534bSLuigi Rizzo }
69868b8534bSLuigi Rizzo 
69968b8534bSLuigi Rizzo static void
70068b8534bSLuigi Rizzo usage(void)
70168b8534bSLuigi Rizzo {
70268b8534bSLuigi Rizzo 	const char *cmd = "pkt-gen";
70368b8534bSLuigi Rizzo 	fprintf(stderr,
70468b8534bSLuigi Rizzo 		"Usage:\n"
70568b8534bSLuigi Rizzo 		"%s arguments\n"
70668b8534bSLuigi Rizzo 		"\t-i interface		interface name\n"
70768b8534bSLuigi Rizzo 		"\t-t pkts_to_send	also forces send mode\n"
70868b8534bSLuigi Rizzo 		"\t-r pkts_to_receive	also forces receive mode\n"
70968b8534bSLuigi Rizzo 		"\t-l pkts_size		in bytes excluding CRC\n"
71068b8534bSLuigi Rizzo 		"\t-d dst-ip		end with %%n to sweep n addresses\n"
71168b8534bSLuigi Rizzo 		"\t-s src-ip		end with %%n to sweep n addresses\n"
71268b8534bSLuigi Rizzo 		"\t-D dst-mac		end with %%n to sweep n addresses\n"
71368b8534bSLuigi Rizzo 		"\t-S src-mac		end with %%n to sweep n addresses\n"
71468b8534bSLuigi Rizzo 		"\t-b burst size		testing, mostly\n"
71568b8534bSLuigi Rizzo 		"\t-c cores		cores to use\n"
71668b8534bSLuigi Rizzo 		"\t-p threads		processes/threads to use\n"
71768b8534bSLuigi Rizzo 		"\t-T report_ms		milliseconds between reports\n"
71868b8534bSLuigi Rizzo 		"\t-w wait_for_link_time	in seconds\n"
71968b8534bSLuigi Rizzo 		"",
72068b8534bSLuigi Rizzo 		cmd);
72168b8534bSLuigi Rizzo 
72268b8534bSLuigi Rizzo 	exit(0);
72368b8534bSLuigi Rizzo }
72468b8534bSLuigi Rizzo 
72568b8534bSLuigi Rizzo 
72668b8534bSLuigi Rizzo int
72768b8534bSLuigi Rizzo main(int arc, char **argv)
72868b8534bSLuigi Rizzo {
72968b8534bSLuigi Rizzo 	int i, fd;
73099fb123fSLuigi Rizzo 	char pcap_errbuf[PCAP_ERRBUF_SIZE];
73168b8534bSLuigi Rizzo 
73268b8534bSLuigi Rizzo 	struct glob_arg g;
73368b8534bSLuigi Rizzo 
73468b8534bSLuigi Rizzo 	struct nmreq nmr;
73568b8534bSLuigi Rizzo 	void *mmap_addr;		/* the mmap address */
73668b8534bSLuigi Rizzo 	void *(*td_body)(void *) = receiver_body;
73768b8534bSLuigi Rizzo 	int ch;
73868b8534bSLuigi Rizzo 	int report_interval = 1000;	/* report interval */
73968b8534bSLuigi Rizzo 	char *ifname = NULL;
74068b8534bSLuigi Rizzo 	int wait_link = 2;
74168b8534bSLuigi Rizzo 	int devqueues = 1;	/* how many device queues */
74268b8534bSLuigi Rizzo 
74368b8534bSLuigi Rizzo 	bzero(&g, sizeof(g));
74468b8534bSLuigi Rizzo 
74568b8534bSLuigi Rizzo 	g.src_ip = "10.0.0.1";
74668b8534bSLuigi Rizzo 	g.dst_ip = "10.1.0.1";
74768b8534bSLuigi Rizzo 	g.dst_mac = "ff:ff:ff:ff:ff:ff";
74868b8534bSLuigi Rizzo 	g.src_mac = NULL;
74968b8534bSLuigi Rizzo 	g.pkt_size = 60;
75068b8534bSLuigi Rizzo 	g.burst = 512;		// default
75168b8534bSLuigi Rizzo 	g.nthreads = 1;
75268b8534bSLuigi Rizzo 	g.cpus = 1;
75368b8534bSLuigi Rizzo 
75468b8534bSLuigi Rizzo 	while ( (ch = getopt(arc, argv,
75599fb123fSLuigi Rizzo 			"i:t:r:l:d:s:D:S:b:c:o:p:PT:w:v")) != -1) {
75668b8534bSLuigi Rizzo 		switch(ch) {
75768b8534bSLuigi Rizzo 		default:
75868b8534bSLuigi Rizzo 			D("bad option %c %s", ch, optarg);
75968b8534bSLuigi Rizzo 			usage();
76068b8534bSLuigi Rizzo 			break;
76199fb123fSLuigi Rizzo 		case 'o':
76299fb123fSLuigi Rizzo 			g.options = atoi(optarg);
76399fb123fSLuigi Rizzo 			break;
76468b8534bSLuigi Rizzo 		case 'i':	/* interface */
76568b8534bSLuigi Rizzo 			ifname = optarg;
76668b8534bSLuigi Rizzo 			break;
76768b8534bSLuigi Rizzo 		case 't':	/* send */
76868b8534bSLuigi Rizzo 			td_body = sender_body;
76968b8534bSLuigi Rizzo 			g.npackets = atoi(optarg);
77068b8534bSLuigi Rizzo 			break;
77168b8534bSLuigi Rizzo 		case 'r':	/* receive */
77268b8534bSLuigi Rizzo 			td_body = receiver_body;
77368b8534bSLuigi Rizzo 			g.npackets = atoi(optarg);
77468b8534bSLuigi Rizzo 			break;
77568b8534bSLuigi Rizzo 		case 'l':	/* pkt_size */
77668b8534bSLuigi Rizzo 			g.pkt_size = atoi(optarg);
77768b8534bSLuigi Rizzo 			break;
77868b8534bSLuigi Rizzo 		case 'd':
77968b8534bSLuigi Rizzo 			g.dst_ip = optarg;
78068b8534bSLuigi Rizzo 			break;
78168b8534bSLuigi Rizzo 		case 's':
78268b8534bSLuigi Rizzo 			g.src_ip = optarg;
78368b8534bSLuigi Rizzo 			break;
78468b8534bSLuigi Rizzo 		case 'T':	/* report interval */
78568b8534bSLuigi Rizzo 			report_interval = atoi(optarg);
78668b8534bSLuigi Rizzo 			break;
78768b8534bSLuigi Rizzo 		case 'w':
78868b8534bSLuigi Rizzo 			wait_link = atoi(optarg);
78968b8534bSLuigi Rizzo 			break;
79068b8534bSLuigi Rizzo 		case 'b':	/* burst */
79168b8534bSLuigi Rizzo 			g.burst = atoi(optarg);
79268b8534bSLuigi Rizzo 			break;
79368b8534bSLuigi Rizzo 		case 'c':
79468b8534bSLuigi Rizzo 			g.cpus = atoi(optarg);
79568b8534bSLuigi Rizzo 			break;
79668b8534bSLuigi Rizzo 		case 'p':
79768b8534bSLuigi Rizzo 			g.nthreads = atoi(optarg);
79868b8534bSLuigi Rizzo 			break;
79968b8534bSLuigi Rizzo 
80068b8534bSLuigi Rizzo 		case 'P':
80168b8534bSLuigi Rizzo 			g.use_pcap = 1;
80268b8534bSLuigi Rizzo 			break;
80368b8534bSLuigi Rizzo 
80468b8534bSLuigi Rizzo 		case 'D': /* destination mac */
80568b8534bSLuigi Rizzo 			g.dst_mac = optarg;
80668b8534bSLuigi Rizzo 	{
80768b8534bSLuigi Rizzo 		struct ether_addr *mac = ether_aton(g.dst_mac);
80868b8534bSLuigi Rizzo 		D("ether_aton(%s) gives %p", g.dst_mac, mac);
80968b8534bSLuigi Rizzo 	}
81068b8534bSLuigi Rizzo 			break;
81168b8534bSLuigi Rizzo 		case 'S': /* source mac */
81268b8534bSLuigi Rizzo 			g.src_mac = optarg;
81368b8534bSLuigi Rizzo 			break;
81468b8534bSLuigi Rizzo 		case 'v':
81568b8534bSLuigi Rizzo 			verbose++;
81668b8534bSLuigi Rizzo 		}
81768b8534bSLuigi Rizzo 	}
81868b8534bSLuigi Rizzo 
81968b8534bSLuigi Rizzo 	if (ifname == NULL) {
82068b8534bSLuigi Rizzo 		D("missing ifname");
82168b8534bSLuigi Rizzo 		usage();
82268b8534bSLuigi Rizzo 	}
82368b8534bSLuigi Rizzo 	{
82468b8534bSLuigi Rizzo 		int n = system_ncpus();
82568b8534bSLuigi Rizzo 		if (g.cpus < 0 || g.cpus > n) {
82668b8534bSLuigi Rizzo 			D("%d cpus is too high, have only %d cpus", g.cpus, n);
82768b8534bSLuigi Rizzo 			usage();
82868b8534bSLuigi Rizzo 		}
82968b8534bSLuigi Rizzo 		if (g.cpus == 0)
83068b8534bSLuigi Rizzo 			g.cpus = n;
83168b8534bSLuigi Rizzo 	}
83268b8534bSLuigi Rizzo 	if (g.pkt_size < 16 || g.pkt_size > 1536) {
83368b8534bSLuigi Rizzo 		D("bad pktsize %d\n", g.pkt_size);
83468b8534bSLuigi Rizzo 		usage();
83568b8534bSLuigi Rizzo 	}
83668b8534bSLuigi Rizzo 
83799fb123fSLuigi Rizzo 	if (td_body == sender_body && g.src_mac == NULL) {
83899fb123fSLuigi Rizzo 		static char mybuf[20] = "ff:ff:ff:ff:ff:ff";
83999fb123fSLuigi Rizzo 		/* retrieve source mac address. */
84099fb123fSLuigi Rizzo 		if (source_hwaddr(ifname, mybuf) == -1) {
84199fb123fSLuigi Rizzo 			D("Unable to retrieve source mac");
84299fb123fSLuigi Rizzo 			// continue, fail later
84399fb123fSLuigi Rizzo 		}
84499fb123fSLuigi Rizzo 		g.src_mac = mybuf;
84599fb123fSLuigi Rizzo 	}
84699fb123fSLuigi Rizzo 
84799fb123fSLuigi Rizzo     if (g.use_pcap) {
84899fb123fSLuigi Rizzo 	D("using pcap on %s", ifname);
84999fb123fSLuigi Rizzo 	g.p = pcap_open_live(ifname, 0, 1, 100, pcap_errbuf);
85099fb123fSLuigi Rizzo 	if (g.p == NULL) {
85199fb123fSLuigi Rizzo 		D("cannot open pcap on %s", ifname);
85299fb123fSLuigi Rizzo 		usage();
85399fb123fSLuigi Rizzo 	}
85499fb123fSLuigi Rizzo 	mmap_addr = NULL;
85599fb123fSLuigi Rizzo 	fd = -1;
85699fb123fSLuigi Rizzo     } else {
85768b8534bSLuigi Rizzo 	bzero(&nmr, sizeof(nmr));
85864ae02c3SLuigi Rizzo 	nmr.nr_version = NETMAP_API;
85968b8534bSLuigi Rizzo 	/*
86068b8534bSLuigi Rizzo 	 * Open the netmap device to fetch the number of queues of our
86168b8534bSLuigi Rizzo 	 * interface.
86268b8534bSLuigi Rizzo 	 *
86368b8534bSLuigi Rizzo 	 * The first NIOCREGIF also detaches the card from the
86468b8534bSLuigi Rizzo 	 * protocol stack and may cause a reset of the card,
86568b8534bSLuigi Rizzo 	 * which in turn may take some time for the PHY to
86668b8534bSLuigi Rizzo 	 * reconfigure.
86768b8534bSLuigi Rizzo 	 */
86868b8534bSLuigi Rizzo 	fd = open("/dev/netmap", O_RDWR);
86968b8534bSLuigi Rizzo 	if (fd == -1) {
87068b8534bSLuigi Rizzo 		D("Unable to open /dev/netmap");
87168b8534bSLuigi Rizzo 		// fail later
87268b8534bSLuigi Rizzo 	} else {
87368b8534bSLuigi Rizzo 		if ((ioctl(fd, NIOCGINFO, &nmr)) == -1) {
87468b8534bSLuigi Rizzo 			D("Unable to get if info without name");
87568b8534bSLuigi Rizzo 		} else {
87668b8534bSLuigi Rizzo 			D("map size is %d Kb", nmr.nr_memsize >> 10);
87768b8534bSLuigi Rizzo 		}
87868b8534bSLuigi Rizzo 		bzero(&nmr, sizeof(nmr));
87964ae02c3SLuigi Rizzo 		nmr.nr_version = NETMAP_API;
88068b8534bSLuigi Rizzo 		strncpy(nmr.nr_name, ifname, sizeof(nmr.nr_name));
88168b8534bSLuigi Rizzo 		if ((ioctl(fd, NIOCGINFO, &nmr)) == -1) {
88268b8534bSLuigi Rizzo 			D("Unable to get if info for %s", ifname);
88368b8534bSLuigi Rizzo 		}
88464ae02c3SLuigi Rizzo 		devqueues = nmr.nr_rx_rings;
88568b8534bSLuigi Rizzo 	}
88668b8534bSLuigi Rizzo 
88768b8534bSLuigi Rizzo 	/* validate provided nthreads. */
88868b8534bSLuigi Rizzo 	if (g.nthreads < 1 || g.nthreads > devqueues) {
88968b8534bSLuigi Rizzo 		D("bad nthreads %d, have %d queues", g.nthreads, devqueues);
89068b8534bSLuigi Rizzo 		// continue, fail later
89168b8534bSLuigi Rizzo 	}
89268b8534bSLuigi Rizzo 
89368b8534bSLuigi Rizzo 	/*
89468b8534bSLuigi Rizzo 	 * Map the netmap shared memory: instead of issuing mmap()
89568b8534bSLuigi Rizzo 	 * inside the body of the threads, we prefer to keep this
89668b8534bSLuigi Rizzo 	 * operation here to simplify the thread logic.
89768b8534bSLuigi Rizzo 	 */
89868b8534bSLuigi Rizzo 	D("mmapping %d Kbytes", nmr.nr_memsize>>10);
89968b8534bSLuigi Rizzo 	mmap_addr = (struct netmap_d *) mmap(0, nmr.nr_memsize,
90068b8534bSLuigi Rizzo 					    PROT_WRITE | PROT_READ,
90168b8534bSLuigi Rizzo 					    MAP_SHARED, fd, 0);
90268b8534bSLuigi Rizzo 	if (mmap_addr == MAP_FAILED) {
90368b8534bSLuigi Rizzo 		D("Unable to mmap %d KB", nmr.nr_memsize >> 10);
90468b8534bSLuigi Rizzo 		// continue, fail later
90568b8534bSLuigi Rizzo 	}
90668b8534bSLuigi Rizzo 
90768b8534bSLuigi Rizzo 	/*
90868b8534bSLuigi Rizzo 	 * Register the interface on the netmap device: from now on,
90968b8534bSLuigi Rizzo 	 * we can operate on the network interface without any
91068b8534bSLuigi Rizzo 	 * interference from the legacy network stack.
91168b8534bSLuigi Rizzo 	 *
91268b8534bSLuigi Rizzo 	 * We decide to put the first interface registration here to
91368b8534bSLuigi Rizzo 	 * give time to cards that take a long time to reset the PHY.
91468b8534bSLuigi Rizzo 	 */
91564ae02c3SLuigi Rizzo 	nmr.nr_version = NETMAP_API;
91668b8534bSLuigi Rizzo 	if (ioctl(fd, NIOCREGIF, &nmr) == -1) {
91768b8534bSLuigi Rizzo 		D("Unable to register interface %s", ifname);
91868b8534bSLuigi Rizzo 		//continue, fail later
91968b8534bSLuigi Rizzo 	}
92068b8534bSLuigi Rizzo 
92168b8534bSLuigi Rizzo 
92268b8534bSLuigi Rizzo 	/* Print some debug information. */
92368b8534bSLuigi Rizzo 	fprintf(stdout,
92468b8534bSLuigi Rizzo 		"%s %s: %d queues, %d threads and %d cpus.\n",
92568b8534bSLuigi Rizzo 		(td_body == sender_body) ? "Sending on" : "Receiving from",
92668b8534bSLuigi Rizzo 		ifname,
92768b8534bSLuigi Rizzo 		devqueues,
92868b8534bSLuigi Rizzo 		g.nthreads,
92968b8534bSLuigi Rizzo 		g.cpus);
93068b8534bSLuigi Rizzo 	if (td_body == sender_body) {
93168b8534bSLuigi Rizzo 		fprintf(stdout, "%s -> %s (%s -> %s)\n",
93268b8534bSLuigi Rizzo 			g.src_ip, g.dst_ip,
93368b8534bSLuigi Rizzo 			g.src_mac, g.dst_mac);
93468b8534bSLuigi Rizzo 	}
93568b8534bSLuigi Rizzo 
93668b8534bSLuigi Rizzo 	/* Exit if something went wrong. */
93768b8534bSLuigi Rizzo 	if (fd < 0) {
93868b8534bSLuigi Rizzo 		D("aborting");
93968b8534bSLuigi Rizzo 		usage();
94068b8534bSLuigi Rizzo 	}
94199fb123fSLuigi Rizzo     }
94268b8534bSLuigi Rizzo 
94399fb123fSLuigi Rizzo 	if (g.options) {
94499fb123fSLuigi Rizzo 		D("special options:%s%s%s%s\n",
94599fb123fSLuigi Rizzo 			g.options & OPT_PREFETCH ? " prefetch" : "",
94699fb123fSLuigi Rizzo 			g.options & OPT_ACCESS ? " access" : "",
94799fb123fSLuigi Rizzo 			g.options & OPT_MEMCPY ? " memcpy" : "",
94899fb123fSLuigi Rizzo 			g.options & OPT_COPY ? " copy" : "");
94999fb123fSLuigi Rizzo 	}
95068b8534bSLuigi Rizzo 	/* Wait for PHY reset. */
95168b8534bSLuigi Rizzo 	D("Wait %d secs for phy reset", wait_link);
95268b8534bSLuigi Rizzo 	sleep(wait_link);
95368b8534bSLuigi Rizzo 	D("Ready...");
95468b8534bSLuigi Rizzo 
95568b8534bSLuigi Rizzo 	/* Install ^C handler. */
95668b8534bSLuigi Rizzo 	global_nthreads = g.nthreads;
95768b8534bSLuigi Rizzo 	signal(SIGINT, sigint_h);
95868b8534bSLuigi Rizzo 
95968b8534bSLuigi Rizzo 	if (g.use_pcap) {
96099fb123fSLuigi Rizzo 		g.p = pcap_open_live(ifname, 0, 1, 100, NULL);
96199fb123fSLuigi Rizzo 		if (g.p == NULL) {
96299fb123fSLuigi Rizzo 			D("cannot open pcap on %s", ifname);
96399fb123fSLuigi Rizzo 			usage();
96499fb123fSLuigi Rizzo 		} else
96599fb123fSLuigi Rizzo 			D("using pcap %p on %s", g.p, ifname);
96668b8534bSLuigi Rizzo 	}
96768b8534bSLuigi Rizzo 
96868b8534bSLuigi Rizzo 	targs = calloc(g.nthreads, sizeof(*targs));
96968b8534bSLuigi Rizzo 	/*
97068b8534bSLuigi Rizzo 	 * Now create the desired number of threads, each one
97168b8534bSLuigi Rizzo 	 * using a single descriptor.
97268b8534bSLuigi Rizzo  	 */
97368b8534bSLuigi Rizzo 	for (i = 0; i < g.nthreads; i++) {
97468b8534bSLuigi Rizzo 		struct netmap_if *tnifp;
97568b8534bSLuigi Rizzo 		struct nmreq tifreq;
97668b8534bSLuigi Rizzo 		int tfd;
97768b8534bSLuigi Rizzo 
97868b8534bSLuigi Rizzo 	    if (g.use_pcap) {
97968b8534bSLuigi Rizzo 		tfd = -1;
98068b8534bSLuigi Rizzo 		tnifp = NULL;
98168b8534bSLuigi Rizzo 	    } else {
98268b8534bSLuigi Rizzo 		/* register interface. */
98368b8534bSLuigi Rizzo 		tfd = open("/dev/netmap", O_RDWR);
98468b8534bSLuigi Rizzo 		if (tfd == -1) {
98568b8534bSLuigi Rizzo 			D("Unable to open /dev/netmap");
98668b8534bSLuigi Rizzo 			continue;
98768b8534bSLuigi Rizzo 		}
98868b8534bSLuigi Rizzo 
98968b8534bSLuigi Rizzo 		bzero(&tifreq, sizeof(tifreq));
99068b8534bSLuigi Rizzo 		strncpy(tifreq.nr_name, ifname, sizeof(tifreq.nr_name));
99164ae02c3SLuigi Rizzo 		tifreq.nr_version = NETMAP_API;
99268b8534bSLuigi Rizzo 		tifreq.nr_ringid = (g.nthreads > 1) ? (i | NETMAP_HW_RING) : 0;
99368b8534bSLuigi Rizzo 
99468b8534bSLuigi Rizzo 		/*
99568b8534bSLuigi Rizzo 		 * if we are acting as a receiver only, do not touch the transmit ring.
99668b8534bSLuigi Rizzo 		 * This is not the default because many apps may use the interface
99768b8534bSLuigi Rizzo 		 * in both directions, but a pure receiver does not.
99868b8534bSLuigi Rizzo 		 */
99968b8534bSLuigi Rizzo 		if (td_body == receiver_body) {
100068b8534bSLuigi Rizzo 			tifreq.nr_ringid |= NETMAP_NO_TX_POLL;
100168b8534bSLuigi Rizzo 		}
100268b8534bSLuigi Rizzo 
100368b8534bSLuigi Rizzo 		if ((ioctl(tfd, NIOCREGIF, &tifreq)) == -1) {
100468b8534bSLuigi Rizzo 			D("Unable to register %s", ifname);
100568b8534bSLuigi Rizzo 			continue;
100668b8534bSLuigi Rizzo 		}
100768b8534bSLuigi Rizzo 		tnifp = NETMAP_IF(mmap_addr, tifreq.nr_offset);
100868b8534bSLuigi Rizzo 	    }
100968b8534bSLuigi Rizzo 		/* start threads. */
101068b8534bSLuigi Rizzo 		bzero(&targs[i], sizeof(targs[i]));
101168b8534bSLuigi Rizzo 		targs[i].g = &g;
101268b8534bSLuigi Rizzo 		targs[i].used = 1;
101368b8534bSLuigi Rizzo 		targs[i].completed = 0;
101468b8534bSLuigi Rizzo 		targs[i].fd = tfd;
101568b8534bSLuigi Rizzo 		targs[i].nmr = tifreq;
101668b8534bSLuigi Rizzo 		targs[i].nifp = tnifp;
101768b8534bSLuigi Rizzo 		targs[i].qfirst = (g.nthreads > 1) ? i : 0;
101864ae02c3SLuigi Rizzo 		targs[i].qlast = (g.nthreads > 1) ? i+1 :
101964ae02c3SLuigi Rizzo 			(td_body == receiver_body ? tifreq.nr_rx_rings : tifreq.nr_tx_rings);
102068b8534bSLuigi Rizzo 		targs[i].me = i;
102168b8534bSLuigi Rizzo 		targs[i].affinity = g.cpus ? i % g.cpus : -1;
102268b8534bSLuigi Rizzo 		if (td_body == sender_body) {
102368b8534bSLuigi Rizzo 			/* initialize the packet to send. */
102468b8534bSLuigi Rizzo 			initialize_packet(&targs[i]);
102568b8534bSLuigi Rizzo 		}
102668b8534bSLuigi Rizzo 
102768b8534bSLuigi Rizzo 		if (pthread_create(&targs[i].thread, NULL, td_body,
102868b8534bSLuigi Rizzo 				   &targs[i]) == -1) {
102968b8534bSLuigi Rizzo 			D("Unable to create thread %d", i);
103068b8534bSLuigi Rizzo 			targs[i].used = 0;
103168b8534bSLuigi Rizzo 		}
103268b8534bSLuigi Rizzo 	}
103368b8534bSLuigi Rizzo 
103468b8534bSLuigi Rizzo     {
103568b8534bSLuigi Rizzo 	uint64_t my_count = 0, prev = 0;
103668b8534bSLuigi Rizzo 	uint64_t count = 0;
103768b8534bSLuigi Rizzo 	double delta_t;
103868b8534bSLuigi Rizzo 	struct timeval tic, toc;
103968b8534bSLuigi Rizzo 
104068b8534bSLuigi Rizzo 	gettimeofday(&toc, NULL);
104168b8534bSLuigi Rizzo 	for (;;) {
104268b8534bSLuigi Rizzo 		struct timeval now, delta;
104368b8534bSLuigi Rizzo 		uint64_t pps;
104468b8534bSLuigi Rizzo 		int done = 0;
104568b8534bSLuigi Rizzo 
104668b8534bSLuigi Rizzo 		delta.tv_sec = report_interval/1000;
104768b8534bSLuigi Rizzo 		delta.tv_usec = (report_interval%1000)*1000;
104868b8534bSLuigi Rizzo 		select(0, NULL, NULL, NULL, &delta);
104968b8534bSLuigi Rizzo 		gettimeofday(&now, NULL);
105068b8534bSLuigi Rizzo 		timersub(&now, &toc, &toc);
105168b8534bSLuigi Rizzo 		my_count = 0;
105268b8534bSLuigi Rizzo 		for (i = 0; i < g.nthreads; i++) {
105368b8534bSLuigi Rizzo 			my_count += targs[i].count;
105468b8534bSLuigi Rizzo 			if (targs[i].used == 0)
105568b8534bSLuigi Rizzo 				done++;
105668b8534bSLuigi Rizzo 		}
105768b8534bSLuigi Rizzo 		pps = toc.tv_sec* 1000000 + toc.tv_usec;
105868b8534bSLuigi Rizzo 		if (pps < 10000)
105968b8534bSLuigi Rizzo 			continue;
106068b8534bSLuigi Rizzo 		pps = (my_count - prev)*1000000 / pps;
1061506cc70cSLuigi Rizzo 		D("%" PRIu64 " pps", pps);
106268b8534bSLuigi Rizzo 		prev = my_count;
106368b8534bSLuigi Rizzo 		toc = now;
106468b8534bSLuigi Rizzo 		if (done == g.nthreads)
106568b8534bSLuigi Rizzo 			break;
106668b8534bSLuigi Rizzo 	}
106768b8534bSLuigi Rizzo 
106868b8534bSLuigi Rizzo 	timerclear(&tic);
106968b8534bSLuigi Rizzo 	timerclear(&toc);
107068b8534bSLuigi Rizzo 	for (i = 0; i < g.nthreads; i++) {
107168b8534bSLuigi Rizzo 		/*
107268b8534bSLuigi Rizzo 		 * Join active threads, unregister interfaces and close
107368b8534bSLuigi Rizzo 		 * file descriptors.
107468b8534bSLuigi Rizzo 		 */
107568b8534bSLuigi Rizzo 		pthread_join(targs[i].thread, NULL);
107668b8534bSLuigi Rizzo 		ioctl(targs[i].fd, NIOCUNREGIF, &targs[i].nmr);
107768b8534bSLuigi Rizzo 		close(targs[i].fd);
107868b8534bSLuigi Rizzo 
107968b8534bSLuigi Rizzo 		if (targs[i].completed == 0)
108068b8534bSLuigi Rizzo 			continue;
108168b8534bSLuigi Rizzo 
108268b8534bSLuigi Rizzo 		/*
10838ce070c1SUlrich Spörlein 		 * Collect threads output and extract information about
10848ce070c1SUlrich Spörlein 		 * how long it took to send all the packets.
108568b8534bSLuigi Rizzo 		 */
108668b8534bSLuigi Rizzo 		count += targs[i].count;
108768b8534bSLuigi Rizzo 		if (!timerisset(&tic) || timercmp(&targs[i].tic, &tic, <))
108868b8534bSLuigi Rizzo 			tic = targs[i].tic;
108968b8534bSLuigi Rizzo 		if (!timerisset(&toc) || timercmp(&targs[i].toc, &toc, >))
109068b8534bSLuigi Rizzo 			toc = targs[i].toc;
109168b8534bSLuigi Rizzo 	}
109268b8534bSLuigi Rizzo 
109368b8534bSLuigi Rizzo 	/* print output. */
109468b8534bSLuigi Rizzo 	timersub(&toc, &tic, &toc);
109568b8534bSLuigi Rizzo 	delta_t = toc.tv_sec + 1e-6* toc.tv_usec;
109668b8534bSLuigi Rizzo 	if (td_body == sender_body)
109768b8534bSLuigi Rizzo 		tx_output(count, g.pkt_size, delta_t);
109868b8534bSLuigi Rizzo 	else
109968b8534bSLuigi Rizzo 		rx_output(count, delta_t);
110068b8534bSLuigi Rizzo     }
110168b8534bSLuigi Rizzo 
110299fb123fSLuigi Rizzo     if (g.use_pcap == 0) {
110368b8534bSLuigi Rizzo 	ioctl(fd, NIOCUNREGIF, &nmr);
110468b8534bSLuigi Rizzo 	munmap(mmap_addr, nmr.nr_memsize);
110568b8534bSLuigi Rizzo 	close(fd);
110699fb123fSLuigi Rizzo     }
110768b8534bSLuigi Rizzo 
110868b8534bSLuigi Rizzo 	return (0);
110968b8534bSLuigi Rizzo }
111068b8534bSLuigi Rizzo /* end of file */
1111