xref: /freebsd/tools/tools/netmap/pkt-gen.c (revision 42b38843)
168b8534bSLuigi Rizzo /*
217885a7bSLuigi Rizzo  * Copyright (C) 2011-2014 Matteo Landi, Luigi Rizzo. All rights reserved.
337e3a6d3SLuigi Rizzo  * Copyright (C) 2013-2015 Universita` di Pisa. All rights reserved.
468b8534bSLuigi Rizzo  *
568b8534bSLuigi Rizzo  * Redistribution and use in source and binary forms, with or without
668b8534bSLuigi Rizzo  * modification, are permitted provided that the following conditions
768b8534bSLuigi Rizzo  * are met:
868b8534bSLuigi Rizzo  *   1. Redistributions of source code must retain the above copyright
968b8534bSLuigi Rizzo  *      notice, this list of conditions and the following disclaimer.
1068b8534bSLuigi Rizzo  *   2. Redistributions in binary form must reproduce the above copyright
1168b8534bSLuigi Rizzo  *      notice, this list of conditions and the following disclaimer in the
1268b8534bSLuigi Rizzo  *    documentation and/or other materials provided with the distribution.
1368b8534bSLuigi Rizzo  *
1468b8534bSLuigi Rizzo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1568b8534bSLuigi Rizzo  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1668b8534bSLuigi Rizzo  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1768b8534bSLuigi Rizzo  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1868b8534bSLuigi Rizzo  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1968b8534bSLuigi Rizzo  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2068b8534bSLuigi Rizzo  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2168b8534bSLuigi Rizzo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2268b8534bSLuigi Rizzo  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2368b8534bSLuigi Rizzo  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2468b8534bSLuigi Rizzo  * SUCH DAMAGE.
2568b8534bSLuigi Rizzo  */
2668b8534bSLuigi Rizzo 
2768b8534bSLuigi Rizzo /*
28ce3ee1e7SLuigi Rizzo  * $Id: pkt-gen.c 12346 2013-06-12 17:36:25Z 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 
39f0ea3689SLuigi Rizzo #define _GNU_SOURCE	/* for CPU_SET() */
40f0ea3689SLuigi Rizzo #include <arpa/inet.h>	/* ntohs */
414bfe1a4fSVincenzo Maffione #include <assert.h>
424bfe1a4fSVincenzo Maffione #include <ctype.h>	// isprint()
434bfe1a4fSVincenzo Maffione #include <errno.h>
444bfe1a4fSVincenzo Maffione #include <fcntl.h>
45f0ea3689SLuigi Rizzo #include <ifaddrs.h>	/* getifaddrs */
464bfe1a4fSVincenzo Maffione #include <libnetmap.h>
474bfe1a4fSVincenzo Maffione #include <math.h>
48f0ea3689SLuigi Rizzo #include <net/ethernet.h>
49f0ea3689SLuigi Rizzo #include <netinet/in.h>
50f0ea3689SLuigi Rizzo #include <netinet/ip.h>
5180ad548dSVincenzo Maffione #include <netinet/ip6.h>
524bfe1a4fSVincenzo Maffione #include <netinet/udp.h>
534bfe1a4fSVincenzo Maffione #ifndef NO_PCAP
544bfe1a4fSVincenzo Maffione #include <pcap/pcap.h>
554bfe1a4fSVincenzo Maffione #endif
564bfe1a4fSVincenzo Maffione #include <pthread.h>
574bfe1a4fSVincenzo Maffione #include <signal.h>
584bfe1a4fSVincenzo Maffione #include <stdio.h>
594bfe1a4fSVincenzo Maffione #include <stdlib.h>
604bfe1a4fSVincenzo Maffione #include <string.h>
614bfe1a4fSVincenzo Maffione #include <sys/ioctl.h>
624bfe1a4fSVincenzo Maffione #include <sys/poll.h>
634bfe1a4fSVincenzo Maffione #include <sys/stat.h>
644bfe1a4fSVincenzo Maffione #if !defined(_WIN32) && !defined(linux)
654bfe1a4fSVincenzo Maffione #include <sys/sysctl.h>	/* sysctl */
664bfe1a4fSVincenzo Maffione #endif
674bfe1a4fSVincenzo Maffione #include <sys/types.h>
684bfe1a4fSVincenzo Maffione #include <unistd.h>	// sysconf()
6980ad548dSVincenzo Maffione #ifdef linux
7080ad548dSVincenzo Maffione #define IPV6_VERSION	0x60
7180ad548dSVincenzo Maffione #define IPV6_DEFHLIM	64
7280ad548dSVincenzo Maffione #endif
73f0ea3689SLuigi Rizzo 
7437e3a6d3SLuigi Rizzo #include "ctrs.h"
7537e3a6d3SLuigi Rizzo 
7680ad548dSVincenzo Maffione static void usage(int);
7780ad548dSVincenzo Maffione 
7837e3a6d3SLuigi Rizzo #ifdef _WIN32
7937e3a6d3SLuigi Rizzo #define cpuset_t        DWORD_PTR   //uint64_t
CPU_ZERO(cpuset_t * p)8037e3a6d3SLuigi Rizzo static inline void CPU_ZERO(cpuset_t *p)
8137e3a6d3SLuigi Rizzo {
8237e3a6d3SLuigi Rizzo 	*p = 0;
8337e3a6d3SLuigi Rizzo }
8437e3a6d3SLuigi Rizzo 
CPU_SET(uint32_t i,cpuset_t * p)8537e3a6d3SLuigi Rizzo static inline void CPU_SET(uint32_t i, cpuset_t *p)
8637e3a6d3SLuigi Rizzo {
8737e3a6d3SLuigi Rizzo 	*p |= 1<< (i & 0x3f);
8837e3a6d3SLuigi Rizzo }
8937e3a6d3SLuigi Rizzo 
9037e3a6d3SLuigi Rizzo #define pthread_setaffinity_np(a, b, c) !SetThreadAffinityMask(a, *c)    //((void)a, 0)
9137e3a6d3SLuigi Rizzo #define TAP_CLONEDEV	"/dev/tap"
9237e3a6d3SLuigi Rizzo #define AF_LINK	18	//defined in winsocks.h
9337e3a6d3SLuigi Rizzo #define CLOCK_REALTIME_PRECISE CLOCK_REALTIME
9437e3a6d3SLuigi Rizzo #include <net/if_dl.h>
9537e3a6d3SLuigi Rizzo 
9637e3a6d3SLuigi Rizzo /*
9737e3a6d3SLuigi Rizzo  * Convert an ASCII representation of an ethernet address to
9837e3a6d3SLuigi Rizzo  * binary form.
9937e3a6d3SLuigi Rizzo  */
10037e3a6d3SLuigi Rizzo struct ether_addr *
ether_aton(const char * a)10137e3a6d3SLuigi Rizzo ether_aton(const char *a)
10237e3a6d3SLuigi Rizzo {
10337e3a6d3SLuigi Rizzo 	int i;
10437e3a6d3SLuigi Rizzo 	static struct ether_addr o;
10537e3a6d3SLuigi Rizzo 	unsigned int o0, o1, o2, o3, o4, o5;
10637e3a6d3SLuigi Rizzo 
10737e3a6d3SLuigi Rizzo 	i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o0, &o1, &o2, &o3, &o4, &o5);
10837e3a6d3SLuigi Rizzo 
10937e3a6d3SLuigi Rizzo 	if (i != 6)
11037e3a6d3SLuigi Rizzo 		return (NULL);
11137e3a6d3SLuigi Rizzo 
11237e3a6d3SLuigi Rizzo 	o.octet[0]=o0;
11337e3a6d3SLuigi Rizzo 	o.octet[1]=o1;
11437e3a6d3SLuigi Rizzo 	o.octet[2]=o2;
11537e3a6d3SLuigi Rizzo 	o.octet[3]=o3;
11637e3a6d3SLuigi Rizzo 	o.octet[4]=o4;
11737e3a6d3SLuigi Rizzo 	o.octet[5]=o5;
11837e3a6d3SLuigi Rizzo 
11937e3a6d3SLuigi Rizzo 	return ((struct ether_addr *)&o);
12037e3a6d3SLuigi Rizzo }
12137e3a6d3SLuigi Rizzo 
12237e3a6d3SLuigi Rizzo /*
12337e3a6d3SLuigi Rizzo  * Convert a binary representation of an ethernet address to
12437e3a6d3SLuigi Rizzo  * an ASCII string.
12537e3a6d3SLuigi Rizzo  */
12637e3a6d3SLuigi Rizzo char *
ether_ntoa(const struct ether_addr * n)12737e3a6d3SLuigi Rizzo ether_ntoa(const struct ether_addr *n)
12837e3a6d3SLuigi Rizzo {
12937e3a6d3SLuigi Rizzo 	int i;
13037e3a6d3SLuigi Rizzo 	static char a[18];
13137e3a6d3SLuigi Rizzo 
13237e3a6d3SLuigi Rizzo 	i = sprintf(a, "%02x:%02x:%02x:%02x:%02x:%02x",
13337e3a6d3SLuigi Rizzo 	    n->octet[0], n->octet[1], n->octet[2],
13437e3a6d3SLuigi Rizzo 	    n->octet[3], n->octet[4], n->octet[5]);
13537e3a6d3SLuigi Rizzo 	return (i < 17 ? NULL : (char *)&a);
13637e3a6d3SLuigi Rizzo }
13737e3a6d3SLuigi Rizzo #endif /* _WIN32 */
13837e3a6d3SLuigi Rizzo 
139f0ea3689SLuigi Rizzo #ifdef linux
140f0ea3689SLuigi Rizzo 
141f0ea3689SLuigi Rizzo #define cpuset_t        cpu_set_t
142f0ea3689SLuigi Rizzo 
143f0ea3689SLuigi Rizzo #define ifr_flagshigh  ifr_flags        /* only the low 16 bits here */
144f0ea3689SLuigi Rizzo #define IFF_PPROMISC   IFF_PROMISC      /* IFF_PPROMISC does not exist */
145f0ea3689SLuigi Rizzo #include <linux/ethtool.h>
146f0ea3689SLuigi Rizzo #include <linux/sockios.h>
147f0ea3689SLuigi Rizzo 
148f0ea3689SLuigi Rizzo #define CLOCK_REALTIME_PRECISE CLOCK_REALTIME
149f0ea3689SLuigi Rizzo #include <netinet/ether.h>      /* ether_aton */
150f0ea3689SLuigi Rizzo #include <linux/if_packet.h>    /* sockaddr_ll */
151f0ea3689SLuigi Rizzo #endif  /* linux */
152f0ea3689SLuigi Rizzo 
153f0ea3689SLuigi Rizzo #ifdef __FreeBSD__
154f0ea3689SLuigi Rizzo #include <sys/endian.h> /* le64toh */
155f0ea3689SLuigi Rizzo #include <machine/param.h>
156f0ea3689SLuigi Rizzo 
157f0ea3689SLuigi Rizzo #include <pthread_np.h> /* pthread w/ affinity */
158f0ea3689SLuigi Rizzo #include <sys/cpuset.h> /* cpu_set */
159f0ea3689SLuigi Rizzo #include <net/if_dl.h>  /* LLADDR */
160f0ea3689SLuigi Rizzo #endif  /* __FreeBSD__ */
161f0ea3689SLuigi Rizzo 
162f0ea3689SLuigi Rizzo #ifdef __APPLE__
163f0ea3689SLuigi Rizzo 
164f0ea3689SLuigi Rizzo #define cpuset_t        uint64_t        // XXX
CPU_ZERO(cpuset_t * p)165f0ea3689SLuigi Rizzo static inline void CPU_ZERO(cpuset_t *p)
166f0ea3689SLuigi Rizzo {
167f0ea3689SLuigi Rizzo 	*p = 0;
168f0ea3689SLuigi Rizzo }
169f0ea3689SLuigi Rizzo 
CPU_SET(uint32_t i,cpuset_t * p)170f0ea3689SLuigi Rizzo static inline void CPU_SET(uint32_t i, cpuset_t *p)
171f0ea3689SLuigi Rizzo {
172f0ea3689SLuigi Rizzo 	*p |= 1<< (i & 0x3f);
173f0ea3689SLuigi Rizzo }
174f0ea3689SLuigi Rizzo 
175f0ea3689SLuigi Rizzo #define pthread_setaffinity_np(a, b, c) ((void)a, 0)
176f0ea3689SLuigi Rizzo 
177f0ea3689SLuigi Rizzo #define ifr_flagshigh  ifr_flags        // XXX
178f0ea3689SLuigi Rizzo #define IFF_PPROMISC   IFF_PROMISC
179f0ea3689SLuigi Rizzo #include <net/if_dl.h>  /* LLADDR */
180f0ea3689SLuigi Rizzo #define clock_gettime(a,b)      \
181f0ea3689SLuigi Rizzo 	do {struct timespec t0 = {0,0}; *(b) = t0; } while (0)
182f0ea3689SLuigi Rizzo #endif  /* __APPLE__ */
183f0ea3689SLuigi Rizzo 
1847eb32dc8SVincenzo Maffione static const char *default_payload = "netmap pkt-gen DIRECT payload\n"
185ce3ee1e7SLuigi Rizzo 	"http://info.iet.unipi.it/~luigi/netmap/ ";
186ce3ee1e7SLuigi Rizzo 
1877eb32dc8SVincenzo Maffione static const char *indirect_payload = "netmap pkt-gen indirect payload\n"
18868b8534bSLuigi Rizzo 	"http://info.iet.unipi.it/~luigi/netmap/ ";
18968b8534bSLuigi Rizzo 
1907eb32dc8SVincenzo Maffione static int verbose = 0;
1917eb32dc8SVincenzo Maffione static int normalize = 1;
19217885a7bSLuigi Rizzo 
19317885a7bSLuigi Rizzo #define VIRT_HDR_1	10	/* length of a base vnet-hdr */
19417885a7bSLuigi Rizzo #define VIRT_HDR_2	12	/* length of the extenede vnet-hdr */
19517885a7bSLuigi Rizzo #define VIRT_HDR_MAX	VIRT_HDR_2
19617885a7bSLuigi Rizzo struct virt_header {
19717885a7bSLuigi Rizzo 	uint8_t fields[VIRT_HDR_MAX];
19817885a7bSLuigi Rizzo };
19917885a7bSLuigi Rizzo 
2009e53f3bdSVincenzo Maffione #define MAX_BODYSIZE	65536
2014bf50f18SLuigi Rizzo 
20268b8534bSLuigi Rizzo struct pkt {
20317885a7bSLuigi Rizzo 	struct virt_header vh;
20468b8534bSLuigi Rizzo 	struct ether_header eh;
20580ad548dSVincenzo Maffione 	union {
20680ad548dSVincenzo Maffione 		struct {
20768b8534bSLuigi Rizzo 			struct ip ip;
20868b8534bSLuigi Rizzo 			struct udphdr udp;
20980ad548dSVincenzo Maffione 			uint8_t body[MAX_BODYSIZE];	/* hardwired */
21080ad548dSVincenzo Maffione 		} ipv4;
21180ad548dSVincenzo Maffione 		struct {
21280ad548dSVincenzo Maffione 			struct ip6_hdr ip;
21380ad548dSVincenzo Maffione 			struct udphdr udp;
21480ad548dSVincenzo Maffione 			uint8_t body[MAX_BODYSIZE];	/* hardwired */
21580ad548dSVincenzo Maffione 		} ipv6;
21680ad548dSVincenzo Maffione 	};
21768b8534bSLuigi Rizzo } __attribute__((__packed__));
21868b8534bSLuigi Rizzo 
21980ad548dSVincenzo Maffione #define	PKT(p, f, af)	\
22080ad548dSVincenzo Maffione     ((af) == AF_INET ? (p)->ipv4.f: (p)->ipv6.f)
22180ad548dSVincenzo Maffione 
222f8e4e36aSLuigi Rizzo struct ip_range {
2237eb32dc8SVincenzo Maffione 	const char *name;
22480ad548dSVincenzo Maffione 	union {
22580ad548dSVincenzo Maffione 		struct {
226ce3ee1e7SLuigi Rizzo 			uint32_t start, end; /* same as struct in_addr */
22780ad548dSVincenzo Maffione 		} ipv4;
22880ad548dSVincenzo Maffione 		struct {
22980ad548dSVincenzo Maffione 			struct in6_addr start, end;
23080ad548dSVincenzo Maffione 			uint8_t sgroup, egroup;
23180ad548dSVincenzo Maffione 		} ipv6;
23280ad548dSVincenzo Maffione 	};
233ce3ee1e7SLuigi Rizzo 	uint16_t port0, port1;
234f8e4e36aSLuigi Rizzo };
235f8e4e36aSLuigi Rizzo 
236f8e4e36aSLuigi Rizzo struct mac_range {
2377eb32dc8SVincenzo Maffione 	const char *name;
238f8e4e36aSLuigi Rizzo 	struct ether_addr start, end;
239f8e4e36aSLuigi Rizzo };
240f8e4e36aSLuigi Rizzo 
241f0ea3689SLuigi Rizzo /* ifname can be netmap:foo-xxxx */
2424bfe1a4fSVincenzo Maffione #define MAX_IFNAMELEN	512	/* our buffer for ifname */
2434bfe1a4fSVincenzo Maffione //#define MAX_PKTSIZE	1536
2444bf50f18SLuigi Rizzo #define MAX_PKTSIZE	MAX_BODYSIZE	/* XXX: + IP_HDR + ETH_HDR */
2454bf50f18SLuigi Rizzo 
2464bf50f18SLuigi Rizzo /* compact timestamp to fit into 60 byte packet. (enough to obtain RTT) */
2474bf50f18SLuigi Rizzo struct tstamp {
2484bf50f18SLuigi Rizzo 	uint32_t sec;
2494bf50f18SLuigi Rizzo 	uint32_t nsec;
2504bf50f18SLuigi Rizzo };
2514bf50f18SLuigi Rizzo 
25268b8534bSLuigi Rizzo /*
25368b8534bSLuigi Rizzo  * global arguments for all threads
25468b8534bSLuigi Rizzo  */
255f8e4e36aSLuigi Rizzo 
25668b8534bSLuigi Rizzo struct glob_arg {
25780ad548dSVincenzo Maffione 	int af;		/* address family AF_INET/AF_INET6 */
258f8e4e36aSLuigi Rizzo 	struct ip_range src_ip;
259f8e4e36aSLuigi Rizzo 	struct ip_range dst_ip;
260f8e4e36aSLuigi Rizzo 	struct mac_range dst_mac;
261f8e4e36aSLuigi Rizzo 	struct mac_range src_mac;
26268b8534bSLuigi Rizzo 	int pkt_size;
26380ad548dSVincenzo Maffione 	int pkt_min_size;
26468b8534bSLuigi Rizzo 	int burst;
265f8e4e36aSLuigi Rizzo 	int forever;
26637e3a6d3SLuigi Rizzo 	uint64_t npackets;	/* total packets to send */
267ce3ee1e7SLuigi Rizzo 	int frags;		/* fragments per packet */
2689e53f3bdSVincenzo Maffione 	u_int frag_size;	/* size of each fragment */
26968b8534bSLuigi Rizzo 	int nthreads;
27037e3a6d3SLuigi Rizzo 	int cpus;	/* cpus used for running */
27137e3a6d3SLuigi Rizzo 	int system_cpus;	/* cpus on the system */
27237e3a6d3SLuigi Rizzo 
27399fb123fSLuigi Rizzo 	int options;	/* testing */
27499fb123fSLuigi Rizzo #define OPT_PREFETCH	1
27599fb123fSLuigi Rizzo #define OPT_ACCESS	2
27699fb123fSLuigi Rizzo #define OPT_COPY	4
27799fb123fSLuigi Rizzo #define OPT_MEMCPY	8
278f8e4e36aSLuigi Rizzo #define OPT_TS		16	/* add a timestamp */
279b303f675SLuigi Rizzo #define OPT_INDIRECT	32	/* use indirect buffers, tx only */
280b303f675SLuigi Rizzo #define OPT_DUMP	64	/* dump rx/tx traffic */
281ed188a7eSVincenzo Maffione #define OPT_RUBBISH	256	/* send whatever the buffers contain */
28256717743SAdrian Chadd #define OPT_RANDOM_SRC  512
28356717743SAdrian Chadd #define OPT_RANDOM_DST  1024
28437e3a6d3SLuigi Rizzo #define OPT_PPS_STATS   2048
2858c3b8c83SVincenzo Maffione #define OPT_UPDATE_CSUM 4096
286f8e4e36aSLuigi Rizzo 	int dev_type;
287f2637526SLuigi Rizzo #ifndef NO_PCAP
28868b8534bSLuigi Rizzo 	pcap_t *p;
289f2637526SLuigi Rizzo #endif
29068b8534bSLuigi Rizzo 
2911cb4c501SLuigi Rizzo 	int tx_rate;
2921cb4c501SLuigi Rizzo 	struct timespec tx_period;
2931cb4c501SLuigi Rizzo 
294f8e4e36aSLuigi Rizzo 	int affinity;
295f8e4e36aSLuigi Rizzo 	int main_fd;
2964bfe1a4fSVincenzo Maffione 	struct nmport_d *nmd;
2974bfe1a4fSVincenzo Maffione 	uint32_t orig_mode;
298f2637526SLuigi Rizzo 	int report_interval;		/* milliseconds between prints */
299f8e4e36aSLuigi Rizzo 	void *(*td_body)(void *);
30037e3a6d3SLuigi Rizzo 	int td_type;
301f8e4e36aSLuigi Rizzo 	void *mmap_addr;
302f0ea3689SLuigi Rizzo 	char ifname[MAX_IFNAMELEN];
3037eb32dc8SVincenzo Maffione 	const char *nmr_config;
304ce3ee1e7SLuigi Rizzo 	int dummy_send;
30517885a7bSLuigi Rizzo 	int virt_header;	/* send also the virt_header */
306f284c737SGeorge V. Neville-Neil 	char *packet_file;	/* -P option */
30737e3a6d3SLuigi Rizzo #define	STATS_WIN	15
30837e3a6d3SLuigi Rizzo 	int win_idx;
30937e3a6d3SLuigi Rizzo 	int64_t win[STATS_WIN];
31080ad548dSVincenzo Maffione 	int wait_link;
31180ad548dSVincenzo Maffione 	int framing;		/* #bits of framing (for bw output) */
31268b8534bSLuigi Rizzo };
313f8e4e36aSLuigi Rizzo enum dev_type { DEV_NONE, DEV_NETMAP, DEV_PCAP, DEV_TAP };
314f8e4e36aSLuigi Rizzo 
3159e53f3bdSVincenzo Maffione enum {
3169e53f3bdSVincenzo Maffione 	TD_TYPE_SENDER = 1,
3179e53f3bdSVincenzo Maffione 	TD_TYPE_RECEIVER,
3189e53f3bdSVincenzo Maffione 	TD_TYPE_OTHER,
3199e53f3bdSVincenzo Maffione };
32068b8534bSLuigi Rizzo 
32168b8534bSLuigi Rizzo /*
32268b8534bSLuigi Rizzo  * Arguments for a new thread. The same structure is used by
32368b8534bSLuigi Rizzo  * the source and the sink
32468b8534bSLuigi Rizzo  */
32568b8534bSLuigi Rizzo struct targ {
32668b8534bSLuigi Rizzo 	struct glob_arg *g;
32768b8534bSLuigi Rizzo 	int used;
32868b8534bSLuigi Rizzo 	int completed;
3293fe77e68SEd Maste 	int cancel;
33068b8534bSLuigi Rizzo 	int fd;
3314bfe1a4fSVincenzo Maffione 	struct nmport_d *nmd;
33237e3a6d3SLuigi Rizzo 	/* these ought to be volatile, but they are
33337e3a6d3SLuigi Rizzo 	 * only sampled and errors should not accumulate
33437e3a6d3SLuigi Rizzo 	 */
33537e3a6d3SLuigi Rizzo 	struct my_ctrs ctr;
33637e3a6d3SLuigi Rizzo 
3371cb4c501SLuigi Rizzo 	struct timespec tic, toc;
33868b8534bSLuigi Rizzo 	int me;
33968b8534bSLuigi Rizzo 	pthread_t thread;
34068b8534bSLuigi Rizzo 	int affinity;
34168b8534bSLuigi Rizzo 
34268b8534bSLuigi Rizzo 	struct pkt pkt;
343f284c737SGeorge V. Neville-Neil 	void *frame;
34480ad548dSVincenzo Maffione 	uint16_t seed[3];
34580ad548dSVincenzo Maffione 	u_int frags;
34680ad548dSVincenzo Maffione 	u_int frag_size;
34768b8534bSLuigi Rizzo };
34868b8534bSLuigi Rizzo 
34980ad548dSVincenzo Maffione static __inline uint16_t
cksum_add(uint16_t sum,uint16_t a)35080ad548dSVincenzo Maffione cksum_add(uint16_t sum, uint16_t a)
35180ad548dSVincenzo Maffione {
35280ad548dSVincenzo Maffione 	uint16_t res;
35368b8534bSLuigi Rizzo 
35480ad548dSVincenzo Maffione 	res = sum + a;
35580ad548dSVincenzo Maffione 	return (res + (res < a));
35680ad548dSVincenzo Maffione }
35780ad548dSVincenzo Maffione 
35880ad548dSVincenzo Maffione static void
extract_ipv4_addr(char * name,uint32_t * addr,uint16_t * port)35980ad548dSVincenzo Maffione extract_ipv4_addr(char *name, uint32_t *addr, uint16_t *port)
36080ad548dSVincenzo Maffione {
36180ad548dSVincenzo Maffione 	struct in_addr a;
36280ad548dSVincenzo Maffione 	char *pp;
36380ad548dSVincenzo Maffione 
36480ad548dSVincenzo Maffione 	pp = strchr(name, ':');
36580ad548dSVincenzo Maffione 	if (pp != NULL) {	/* do we have ports ? */
36680ad548dSVincenzo Maffione 		*pp++ = '\0';
36780ad548dSVincenzo Maffione 		*port = (uint16_t)strtol(pp, NULL, 0);
36880ad548dSVincenzo Maffione 	}
36980ad548dSVincenzo Maffione 
37080ad548dSVincenzo Maffione 	inet_pton(AF_INET, name, &a);
37180ad548dSVincenzo Maffione 	*addr = ntohl(a.s_addr);
37280ad548dSVincenzo Maffione }
37380ad548dSVincenzo Maffione 
37480ad548dSVincenzo Maffione static void
extract_ipv6_addr(char * name,struct in6_addr * addr,uint16_t * port,uint8_t * group)37580ad548dSVincenzo Maffione extract_ipv6_addr(char *name, struct in6_addr *addr, uint16_t *port,
37680ad548dSVincenzo Maffione     uint8_t *group)
37780ad548dSVincenzo Maffione {
37880ad548dSVincenzo Maffione 	char *pp;
37980ad548dSVincenzo Maffione 
38080ad548dSVincenzo Maffione 	/*
38180ad548dSVincenzo Maffione 	 * We accept IPv6 address in the following form:
38280ad548dSVincenzo Maffione 	 *  group@[2001:DB8::1001]:port	(w/ brackets and port)
38380ad548dSVincenzo Maffione 	 *  group@[2001:DB8::1]		(w/ brackets and w/o port)
38480ad548dSVincenzo Maffione 	 *  group@2001:DB8::1234	(w/o brackets and w/o port)
38580ad548dSVincenzo Maffione 	 */
38680ad548dSVincenzo Maffione 	pp = strchr(name, '@');
38780ad548dSVincenzo Maffione 	if (pp != NULL) {
38880ad548dSVincenzo Maffione 		*pp++ = '\0';
38980ad548dSVincenzo Maffione 		*group = (uint8_t)strtol(name, NULL, 0);
39080ad548dSVincenzo Maffione 		if (*group > 7)
39180ad548dSVincenzo Maffione 			*group = 7;
39280ad548dSVincenzo Maffione 		name = pp;
39380ad548dSVincenzo Maffione 	}
39480ad548dSVincenzo Maffione 	if (name[0] == '[')
39580ad548dSVincenzo Maffione 		name++;
39680ad548dSVincenzo Maffione 	pp = strchr(name, ']');
39780ad548dSVincenzo Maffione 	if (pp != NULL)
39880ad548dSVincenzo Maffione 		*pp++ = '\0';
39980ad548dSVincenzo Maffione 	if (pp != NULL && *pp != ':')
40080ad548dSVincenzo Maffione 		pp = NULL;
40180ad548dSVincenzo Maffione 	if (pp != NULL) {	/* do we have ports ? */
40280ad548dSVincenzo Maffione 		*pp++ = '\0';
40380ad548dSVincenzo Maffione 		*port = (uint16_t)strtol(pp, NULL, 0);
40480ad548dSVincenzo Maffione 	}
40580ad548dSVincenzo Maffione 	inet_pton(AF_INET6, name, addr);
40680ad548dSVincenzo Maffione }
407f8e4e36aSLuigi Rizzo /*
408f8e4e36aSLuigi Rizzo  * extract the extremes from a range of ipv4 addresses.
409f8e4e36aSLuigi Rizzo  * addr_lo[-addr_hi][:port_lo[-port_hi]]
410f8e4e36aSLuigi Rizzo  */
41180ad548dSVincenzo Maffione static int
extract_ip_range(struct ip_range * r,int af)41280ad548dSVincenzo Maffione extract_ip_range(struct ip_range *r, int af)
413f8e4e36aSLuigi Rizzo {
41480ad548dSVincenzo Maffione 	char *name, *ap, start[INET6_ADDRSTRLEN];
41580ad548dSVincenzo Maffione 	char end[INET6_ADDRSTRLEN];
416ce3ee1e7SLuigi Rizzo 	struct in_addr a;
41780ad548dSVincenzo Maffione 	uint32_t tmp;
418f8e4e36aSLuigi Rizzo 
41917885a7bSLuigi Rizzo 	if (verbose)
420f8e4e36aSLuigi Rizzo 		D("extract IP range from %s", r->name);
421ce3ee1e7SLuigi Rizzo 
42280ad548dSVincenzo Maffione 	name = strdup(r->name);
42380ad548dSVincenzo Maffione 	if (name == NULL) {
42480ad548dSVincenzo Maffione 		D("strdup failed");
42580ad548dSVincenzo Maffione 		usage(-1);
42680ad548dSVincenzo Maffione 	}
427ce3ee1e7SLuigi Rizzo 	/* the first - splits start/end of range */
42880ad548dSVincenzo Maffione 	ap = strchr(name, '-');
42980ad548dSVincenzo Maffione 	if (ap != NULL)
430ce3ee1e7SLuigi Rizzo 		*ap++ = '\0';
43180ad548dSVincenzo Maffione 	r->port0 = 1234;	/* default port */
43280ad548dSVincenzo Maffione 	if (af == AF_INET6) {
43380ad548dSVincenzo Maffione 		r->ipv6.sgroup = 7; /* default group */
43480ad548dSVincenzo Maffione 		extract_ipv6_addr(name, &r->ipv6.start, &r->port0,
43580ad548dSVincenzo Maffione 		    &r->ipv6.sgroup);
43680ad548dSVincenzo Maffione 	} else
43780ad548dSVincenzo Maffione 		extract_ipv4_addr(name, &r->ipv4.start, &r->port0);
43880ad548dSVincenzo Maffione 
43980ad548dSVincenzo Maffione 	r->port1 = r->port0;
44080ad548dSVincenzo Maffione 	if (af == AF_INET6) {
44180ad548dSVincenzo Maffione 		if (ap != NULL) {
44280ad548dSVincenzo Maffione 			r->ipv6.egroup = r->ipv6.sgroup;
44380ad548dSVincenzo Maffione 			extract_ipv6_addr(ap, &r->ipv6.end, &r->port1,
44480ad548dSVincenzo Maffione 			    &r->ipv6.egroup);
44580ad548dSVincenzo Maffione 		} else {
44680ad548dSVincenzo Maffione 			r->ipv6.end = r->ipv6.start;
44780ad548dSVincenzo Maffione 			r->ipv6.egroup = r->ipv6.sgroup;
448ce3ee1e7SLuigi Rizzo 		}
44980ad548dSVincenzo Maffione 	} else {
45080ad548dSVincenzo Maffione 		if (ap != NULL) {
45180ad548dSVincenzo Maffione 			extract_ipv4_addr(ap, &r->ipv4.end, &r->port1);
45280ad548dSVincenzo Maffione 			if (r->ipv4.start > r->ipv4.end) {
45380ad548dSVincenzo Maffione 				tmp = r->ipv4.end;
45480ad548dSVincenzo Maffione 				r->ipv4.end = r->ipv4.start;
45580ad548dSVincenzo Maffione 				r->ipv4.start = tmp;
456ce3ee1e7SLuigi Rizzo 			}
45780ad548dSVincenzo Maffione 		} else
45880ad548dSVincenzo Maffione 			r->ipv4.end = r->ipv4.start;
459ce3ee1e7SLuigi Rizzo 	}
46080ad548dSVincenzo Maffione 
461ce3ee1e7SLuigi Rizzo 	if (r->port0 > r->port1) {
46280ad548dSVincenzo Maffione 		tmp = r->port0;
463f8e4e36aSLuigi Rizzo 		r->port0 = r->port1;
464ce3ee1e7SLuigi Rizzo 		r->port1 = tmp;
465f8e4e36aSLuigi Rizzo 	}
46680ad548dSVincenzo Maffione 	if (af == AF_INET) {
46780ad548dSVincenzo Maffione 		a.s_addr = htonl(r->ipv4.start);
46880ad548dSVincenzo Maffione 		inet_ntop(af, &a, start, sizeof(start));
46980ad548dSVincenzo Maffione 		a.s_addr = htonl(r->ipv4.end);
47080ad548dSVincenzo Maffione 		inet_ntop(af, &a, end, sizeof(end));
47180ad548dSVincenzo Maffione 	} else {
47280ad548dSVincenzo Maffione 		inet_ntop(af, &r->ipv6.start, start, sizeof(start));
47380ad548dSVincenzo Maffione 		inet_ntop(af, &r->ipv6.end, end, sizeof(end));
474f8e4e36aSLuigi Rizzo 	}
47580ad548dSVincenzo Maffione 	if (af == AF_INET)
47680ad548dSVincenzo Maffione 		D("range is %s:%d to %s:%d", start, r->port0, end, r->port1);
47780ad548dSVincenzo Maffione 	else
47880ad548dSVincenzo Maffione 		D("range is %d@[%s]:%d to %d@[%s]:%d", r->ipv6.sgroup,
47980ad548dSVincenzo Maffione 		    start, r->port0, r->ipv6.egroup, end, r->port1);
480ce3ee1e7SLuigi Rizzo 
48180ad548dSVincenzo Maffione 	free(name);
48280ad548dSVincenzo Maffione 	if (r->port0 != r->port1 ||
48380ad548dSVincenzo Maffione 	    (af == AF_INET && r->ipv4.start != r->ipv4.end) ||
48480ad548dSVincenzo Maffione 	    (af == AF_INET6 &&
48580ad548dSVincenzo Maffione 		!IN6_ARE_ADDR_EQUAL(&r->ipv6.start, &r->ipv6.end)))
48680ad548dSVincenzo Maffione 		return (OPT_COPY);
48780ad548dSVincenzo Maffione 	return (0);
488f8e4e36aSLuigi Rizzo }
489f8e4e36aSLuigi Rizzo 
49080ad548dSVincenzo Maffione static int
extract_mac_range(struct mac_range * r)491f8e4e36aSLuigi Rizzo extract_mac_range(struct mac_range *r)
492f8e4e36aSLuigi Rizzo {
49380ad548dSVincenzo Maffione 	struct ether_addr *e;
49417885a7bSLuigi Rizzo 	if (verbose)
495f8e4e36aSLuigi Rizzo 	    D("extract MAC range from %s", r->name);
49680ad548dSVincenzo Maffione 
49780ad548dSVincenzo Maffione 	e = ether_aton(r->name);
49880ad548dSVincenzo Maffione 	if (e == NULL) {
49980ad548dSVincenzo Maffione 		D("invalid MAC address '%s'", r->name);
50080ad548dSVincenzo Maffione 		return 1;
50180ad548dSVincenzo Maffione 	}
50280ad548dSVincenzo Maffione 	bcopy(e, &r->start, 6);
50380ad548dSVincenzo Maffione 	bcopy(e, &r->end, 6);
504f8e4e36aSLuigi Rizzo #if 0
505f8e4e36aSLuigi Rizzo 	bcopy(targ->src_mac, eh->ether_shost, 6);
506f8e4e36aSLuigi Rizzo 	p = index(targ->g->src_mac, '-');
507f8e4e36aSLuigi Rizzo 	if (p)
508f8e4e36aSLuigi Rizzo 		targ->src_mac_range = atoi(p+1);
509f8e4e36aSLuigi Rizzo 
510f8e4e36aSLuigi Rizzo 	bcopy(ether_aton(targ->g->dst_mac), targ->dst_mac, 6);
511f8e4e36aSLuigi Rizzo 	bcopy(targ->dst_mac, eh->ether_dhost, 6);
512f8e4e36aSLuigi Rizzo 	p = index(targ->g->dst_mac, '-');
513f8e4e36aSLuigi Rizzo 	if (p)
514f8e4e36aSLuigi Rizzo 		targ->dst_mac_range = atoi(p+1);
515f8e4e36aSLuigi Rizzo #endif
51617885a7bSLuigi Rizzo 	if (verbose)
517f8e4e36aSLuigi Rizzo 		D("%s starts at %s", r->name, ether_ntoa(&r->start));
51880ad548dSVincenzo Maffione 	return 0;
519f8e4e36aSLuigi Rizzo }
520f8e4e36aSLuigi Rizzo 
5219e53f3bdSVincenzo Maffione static int
get_if_mtu(const struct glob_arg * g)5229e53f3bdSVincenzo Maffione get_if_mtu(const struct glob_arg *g)
5239e53f3bdSVincenzo Maffione {
5249e53f3bdSVincenzo Maffione 	struct ifreq ifreq;
5259e53f3bdSVincenzo Maffione 	int s, ret;
5264bfe1a4fSVincenzo Maffione 	const char *ifname = g->nmd->hdr.nr_name;
5274bfe1a4fSVincenzo Maffione 	size_t len;
5289e53f3bdSVincenzo Maffione 
5294bfe1a4fSVincenzo Maffione 	if (!strncmp(g->ifname, "netmap:", 7) && !strchr(ifname, '{')
5304bfe1a4fSVincenzo Maffione 			&& !strchr(ifname, '}')) {
5314bfe1a4fSVincenzo Maffione 
5324bfe1a4fSVincenzo Maffione 		len = strlen(ifname);
5334bfe1a4fSVincenzo Maffione 
5344bfe1a4fSVincenzo Maffione 		if (len > IFNAMSIZ) {
5354bfe1a4fSVincenzo Maffione 			D("'%s' too long, cannot ask for MTU", ifname);
5364bfe1a4fSVincenzo Maffione 			return -1;
5374bfe1a4fSVincenzo Maffione 		}
5389e53f3bdSVincenzo Maffione 
5399e53f3bdSVincenzo Maffione 		s = socket(AF_INET, SOCK_DGRAM, 0);
5409e53f3bdSVincenzo Maffione 		if (s < 0) {
5419e53f3bdSVincenzo Maffione 			D("socket() failed: %s", strerror(errno));
5429e53f3bdSVincenzo Maffione 			return s;
5439e53f3bdSVincenzo Maffione 		}
5449e53f3bdSVincenzo Maffione 
5459e53f3bdSVincenzo Maffione 		memset(&ifreq, 0, sizeof(ifreq));
5464bfe1a4fSVincenzo Maffione 		memcpy(ifreq.ifr_name, ifname, len);
5479e53f3bdSVincenzo Maffione 
5489e53f3bdSVincenzo Maffione 		ret = ioctl(s, SIOCGIFMTU, &ifreq);
5499e53f3bdSVincenzo Maffione 		if (ret) {
5509e53f3bdSVincenzo Maffione 			D("ioctl(SIOCGIFMTU) failed: %s", strerror(errno));
5519e53f3bdSVincenzo Maffione 		}
5529e53f3bdSVincenzo Maffione 
5534bfe1a4fSVincenzo Maffione 		close(s);
5544bfe1a4fSVincenzo Maffione 
5559e53f3bdSVincenzo Maffione 		return ifreq.ifr_mtu;
5569e53f3bdSVincenzo Maffione 	}
5579e53f3bdSVincenzo Maffione 
5589e53f3bdSVincenzo Maffione 	/* This is a pipe or a VALE port, where the MTU is very large,
5599e53f3bdSVincenzo Maffione 	 * so we use some practical limit. */
5609e53f3bdSVincenzo Maffione 	return 65536;
5619e53f3bdSVincenzo Maffione }
5629e53f3bdSVincenzo Maffione 
56368b8534bSLuigi Rizzo static struct targ *targs;
56468b8534bSLuigi Rizzo static int global_nthreads;
56568b8534bSLuigi Rizzo 
56668b8534bSLuigi Rizzo /* control-C handler */
56768b8534bSLuigi Rizzo static void
sigint_h(int sig)568f8e4e36aSLuigi Rizzo sigint_h(int sig)
56968b8534bSLuigi Rizzo {
570f8e4e36aSLuigi Rizzo 	int i;
57168b8534bSLuigi Rizzo 
572f8e4e36aSLuigi Rizzo 	(void)sig;	/* UNUSED */
57337e3a6d3SLuigi Rizzo 	D("received control-C on thread %p", (void *)pthread_self());
574f8e4e36aSLuigi Rizzo 	for (i = 0; i < global_nthreads; i++) {
575f8e4e36aSLuigi Rizzo 		targs[i].cancel = 1;
576f8e4e36aSLuigi Rizzo 	}
57768b8534bSLuigi Rizzo }
57868b8534bSLuigi Rizzo 
57968b8534bSLuigi Rizzo /* sysctl wrapper to return the number of active CPUs */
58068b8534bSLuigi Rizzo static int
system_ncpus(void)58168b8534bSLuigi Rizzo system_ncpus(void)
58268b8534bSLuigi Rizzo {
583f0ea3689SLuigi Rizzo 	int ncpus;
584f0ea3689SLuigi Rizzo #if defined (__FreeBSD__)
585f0ea3689SLuigi Rizzo 	int mib[2] = { CTL_HW, HW_NCPU };
586f0ea3689SLuigi Rizzo 	size_t len = sizeof(mib);
58768b8534bSLuigi Rizzo 	sysctl(mib, 2, &ncpus, &len, NULL, 0);
588f0ea3689SLuigi Rizzo #elif defined(linux)
589f0ea3689SLuigi Rizzo 	ncpus = sysconf(_SC_NPROCESSORS_ONLN);
59037e3a6d3SLuigi Rizzo #elif defined(_WIN32)
59137e3a6d3SLuigi Rizzo 	{
59237e3a6d3SLuigi Rizzo 		SYSTEM_INFO sysinfo;
59337e3a6d3SLuigi Rizzo 		GetSystemInfo(&sysinfo);
59437e3a6d3SLuigi Rizzo 		ncpus = sysinfo.dwNumberOfProcessors;
59537e3a6d3SLuigi Rizzo 	}
596f0ea3689SLuigi Rizzo #else /* others */
597f0ea3689SLuigi Rizzo 	ncpus = 1;
598f0ea3689SLuigi Rizzo #endif /* others */
59968b8534bSLuigi Rizzo 	return (ncpus);
60068b8534bSLuigi Rizzo }
60168b8534bSLuigi Rizzo 
602f8e4e36aSLuigi Rizzo #ifdef __linux__
603f8e4e36aSLuigi Rizzo #define sockaddr_dl    sockaddr_ll
604f8e4e36aSLuigi Rizzo #define sdl_family     sll_family
605f8e4e36aSLuigi Rizzo #define AF_LINK        AF_PACKET
606f8e4e36aSLuigi Rizzo #define LLADDR(s)      s->sll_addr;
607f8e4e36aSLuigi Rizzo #include <linux/if_tun.h>
608f8e4e36aSLuigi Rizzo #define TAP_CLONEDEV	"/dev/net/tun"
609f8e4e36aSLuigi Rizzo #endif /* __linux__ */
610f8e4e36aSLuigi Rizzo 
611f8e4e36aSLuigi Rizzo #ifdef __FreeBSD__
612f8e4e36aSLuigi Rizzo #include <net/if_tun.h>
613f8e4e36aSLuigi Rizzo #define TAP_CLONEDEV	"/dev/tap"
614f8e4e36aSLuigi Rizzo #endif /* __FreeBSD */
615f8e4e36aSLuigi Rizzo 
616f8e4e36aSLuigi Rizzo #ifdef __APPLE__
617f8e4e36aSLuigi Rizzo // #warning TAP not supported on apple ?
618f8e4e36aSLuigi Rizzo #include <net/if_utun.h>
619f8e4e36aSLuigi Rizzo #define TAP_CLONEDEV	"/dev/tap"
620f8e4e36aSLuigi Rizzo #endif /* __APPLE__ */
621f8e4e36aSLuigi Rizzo 
622f8e4e36aSLuigi Rizzo 
62368b8534bSLuigi Rizzo /*
624ce3ee1e7SLuigi Rizzo  * parse the vale configuration in conf and put it in nmr.
625f0ea3689SLuigi Rizzo  * Return the flag set if necessary.
62680ad548dSVincenzo Maffione  * The configuration may consist of 1 to 4 numbers separated
627fc6eb28bSHiren Panchasara  * by commas: #tx-slots,#rx-slots,#tx-rings,#rx-rings.
628ce3ee1e7SLuigi Rizzo  * Missing numbers or zeroes stand for default values.
629ce3ee1e7SLuigi Rizzo  * As an additional convenience, if exactly one number
630fc6eb28bSHiren Panchasara  * is specified, then this is assigned to both #tx-slots and #rx-slots.
631fc6eb28bSHiren Panchasara  * If there is no 4th number, then the 3rd is assigned to both #tx-rings
632ce3ee1e7SLuigi Rizzo  * and #rx-rings.
633ce3ee1e7SLuigi Rizzo  */
6347eb32dc8SVincenzo Maffione static int
parse_nmr_config(const char * conf,struct nmreq_register * nmr)6354bfe1a4fSVincenzo Maffione parse_nmr_config(const char* conf, struct nmreq_register *nmr)
636ce3ee1e7SLuigi Rizzo {
637ce3ee1e7SLuigi Rizzo 	char *w, *tok;
638ce3ee1e7SLuigi Rizzo 	int i, v;
639ce3ee1e7SLuigi Rizzo 
640ce3ee1e7SLuigi Rizzo 	if (conf == NULL || ! *conf)
641f0ea3689SLuigi Rizzo 		return 0;
642406e7723SVincenzo Maffione 	nmr->nr_tx_rings = nmr->nr_rx_rings = 0;
643406e7723SVincenzo Maffione 	nmr->nr_tx_slots = nmr->nr_rx_slots = 0;
644ce3ee1e7SLuigi Rizzo 	w = strdup(conf);
645ce3ee1e7SLuigi Rizzo 	for (i = 0, tok = strtok(w, ","); tok; i++, tok = strtok(NULL, ",")) {
646ce3ee1e7SLuigi Rizzo 		v = atoi(tok);
647ce3ee1e7SLuigi Rizzo 		switch (i) {
648ce3ee1e7SLuigi Rizzo 		case 0:
649ce3ee1e7SLuigi Rizzo 			nmr->nr_tx_slots = nmr->nr_rx_slots = v;
650ce3ee1e7SLuigi Rizzo 			break;
651ce3ee1e7SLuigi Rizzo 		case 1:
652ce3ee1e7SLuigi Rizzo 			nmr->nr_rx_slots = v;
653ce3ee1e7SLuigi Rizzo 			break;
654ce3ee1e7SLuigi Rizzo 		case 2:
655ce3ee1e7SLuigi Rizzo 			nmr->nr_tx_rings = nmr->nr_rx_rings = v;
656ce3ee1e7SLuigi Rizzo 			break;
657ce3ee1e7SLuigi Rizzo 		case 3:
658ce3ee1e7SLuigi Rizzo 			nmr->nr_rx_rings = v;
659ce3ee1e7SLuigi Rizzo 			break;
660ce3ee1e7SLuigi Rizzo 		default:
661ce3ee1e7SLuigi Rizzo 			D("ignored config: %s", tok);
662ce3ee1e7SLuigi Rizzo 			break;
663ce3ee1e7SLuigi Rizzo 		}
664ce3ee1e7SLuigi Rizzo 	}
665ce3ee1e7SLuigi Rizzo 	D("txr %d txd %d rxr %d rxd %d",
666ce3ee1e7SLuigi Rizzo 			nmr->nr_tx_rings, nmr->nr_tx_slots,
667ce3ee1e7SLuigi Rizzo 			nmr->nr_rx_rings, nmr->nr_rx_slots);
668ce3ee1e7SLuigi Rizzo 	free(w);
6694bfe1a4fSVincenzo Maffione 	return 0;
670ce3ee1e7SLuigi Rizzo }
671ce3ee1e7SLuigi Rizzo 
672ce3ee1e7SLuigi Rizzo 
673ce3ee1e7SLuigi Rizzo /*
67468b8534bSLuigi Rizzo  * locate the src mac address for our interface, put it
67568b8534bSLuigi Rizzo  * into the user-supplied buffer. return 0 if ok, -1 on error.
67668b8534bSLuigi Rizzo  */
67768b8534bSLuigi Rizzo static int
source_hwaddr(const char * ifname,char * buf)67868b8534bSLuigi Rizzo source_hwaddr(const char *ifname, char *buf)
67968b8534bSLuigi Rizzo {
68068b8534bSLuigi Rizzo 	struct ifaddrs *ifaphead, *ifap;
68168b8534bSLuigi Rizzo 
68268b8534bSLuigi Rizzo 	if (getifaddrs(&ifaphead) != 0) {
68368b8534bSLuigi Rizzo 		D("getifaddrs %s failed", ifname);
68468b8534bSLuigi Rizzo 		return (-1);
68568b8534bSLuigi Rizzo 	}
68668b8534bSLuigi Rizzo 
687eda82511SVincenzo Maffione 	/* remove 'netmap:' prefix before comparing interfaces */
688eda82511SVincenzo Maffione 	if (!strncmp(ifname, "netmap:", 7))
689eda82511SVincenzo Maffione 		ifname = &ifname[7];
690eda82511SVincenzo Maffione 
69168b8534bSLuigi Rizzo 	for (ifap = ifaphead; ifap; ifap = ifap->ifa_next) {
69268b8534bSLuigi Rizzo 		struct sockaddr_dl *sdl =
69368b8534bSLuigi Rizzo 			(struct sockaddr_dl *)ifap->ifa_addr;
69468b8534bSLuigi Rizzo 		uint8_t *mac;
69568b8534bSLuigi Rizzo 
69668b8534bSLuigi Rizzo 		if (!sdl || sdl->sdl_family != AF_LINK)
69768b8534bSLuigi Rizzo 			continue;
69880ad548dSVincenzo Maffione 		if (strncmp(ifap->ifa_name, ifname, IFNAMSIZ) != 0)
69968b8534bSLuigi Rizzo 			continue;
70068b8534bSLuigi Rizzo 		mac = (uint8_t *)LLADDR(sdl);
70168b8534bSLuigi Rizzo 		sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x",
70268b8534bSLuigi Rizzo 			mac[0], mac[1], mac[2],
70368b8534bSLuigi Rizzo 			mac[3], mac[4], mac[5]);
70468b8534bSLuigi Rizzo 		if (verbose)
70568b8534bSLuigi Rizzo 			D("source hwaddr %s", buf);
70668b8534bSLuigi Rizzo 		break;
70768b8534bSLuigi Rizzo 	}
70868b8534bSLuigi Rizzo 	freeifaddrs(ifaphead);
70968b8534bSLuigi Rizzo 	return ifap ? 0 : 1;
71068b8534bSLuigi Rizzo }
71168b8534bSLuigi Rizzo 
71268b8534bSLuigi Rizzo 
71368b8534bSLuigi Rizzo /* set the thread affinity. */
71468b8534bSLuigi Rizzo static int
setaffinity(pthread_t me,int i)71568b8534bSLuigi Rizzo setaffinity(pthread_t me, int i)
71668b8534bSLuigi Rizzo {
71768b8534bSLuigi Rizzo 	cpuset_t cpumask;
71868b8534bSLuigi Rizzo 
71968b8534bSLuigi Rizzo 	if (i == -1)
72068b8534bSLuigi Rizzo 		return 0;
72168b8534bSLuigi Rizzo 
72268b8534bSLuigi Rizzo 	/* Set thread affinity affinity.*/
72368b8534bSLuigi Rizzo 	CPU_ZERO(&cpumask);
72468b8534bSLuigi Rizzo 	CPU_SET(i, &cpumask);
72568b8534bSLuigi Rizzo 
72668b8534bSLuigi Rizzo 	if (pthread_setaffinity_np(me, sizeof(cpuset_t), &cpumask) != 0) {
72717885a7bSLuigi Rizzo 		D("Unable to set affinity: %s", strerror(errno));
72868b8534bSLuigi Rizzo 		return 1;
72968b8534bSLuigi Rizzo 	}
73068b8534bSLuigi Rizzo 	return 0;
73168b8534bSLuigi Rizzo }
73268b8534bSLuigi Rizzo 
73380ad548dSVincenzo Maffione 
73468b8534bSLuigi Rizzo /* Compute the checksum of the given ip header. */
73580ad548dSVincenzo Maffione static uint32_t
checksum(const void * data,uint16_t len,uint32_t sum)736f8e4e36aSLuigi Rizzo checksum(const void *data, uint16_t len, uint32_t sum)
73768b8534bSLuigi Rizzo {
73868b8534bSLuigi Rizzo 	const uint8_t *addr = data;
739f8e4e36aSLuigi Rizzo 	uint32_t i;
74068b8534bSLuigi Rizzo 
741f8e4e36aSLuigi Rizzo 	/* Checksum all the pairs of bytes first... */
742f8e4e36aSLuigi Rizzo 	for (i = 0; i < (len & ~1U); i += 2) {
7437eb32dc8SVincenzo Maffione 		sum += (uint16_t)ntohs(*((const uint16_t *)(addr + i)));
744f8e4e36aSLuigi Rizzo 		if (sum > 0xFFFF)
745f8e4e36aSLuigi Rizzo 			sum -= 0xFFFF;
746f8e4e36aSLuigi Rizzo 	}
747f8e4e36aSLuigi Rizzo 	/*
748f8e4e36aSLuigi Rizzo 	 * If there's a single byte left over, checksum it, too.
749f8e4e36aSLuigi Rizzo 	 * Network byte order is big-endian, so the remaining byte is
750f8e4e36aSLuigi Rizzo 	 * the high byte.
751f8e4e36aSLuigi Rizzo 	 */
752f8e4e36aSLuigi Rizzo 	if (i < len) {
753f8e4e36aSLuigi Rizzo 		sum += addr[i] << 8;
754f8e4e36aSLuigi Rizzo 		if (sum > 0xFFFF)
755f8e4e36aSLuigi Rizzo 			sum -= 0xFFFF;
756f8e4e36aSLuigi Rizzo 	}
757f8e4e36aSLuigi Rizzo 	return sum;
75868b8534bSLuigi Rizzo }
75968b8534bSLuigi Rizzo 
76080ad548dSVincenzo Maffione static uint16_t
wrapsum(uint32_t sum)76180ad548dSVincenzo Maffione wrapsum(uint32_t sum)
762f8e4e36aSLuigi Rizzo {
763f8e4e36aSLuigi Rizzo 	sum = ~sum & 0xFFFF;
764f8e4e36aSLuigi Rizzo 	return (htons(sum));
76568b8534bSLuigi Rizzo }
76668b8534bSLuigi Rizzo 
767b303f675SLuigi Rizzo /* Check the payload of the packet for errors (use it for debug).
768b303f675SLuigi Rizzo  * Look for consecutive ascii representations of the size of the packet.
769b303f675SLuigi Rizzo  */
770b303f675SLuigi Rizzo static void
dump_payload(const char * _p,int len,struct netmap_ring * ring,int cur)77137e3a6d3SLuigi Rizzo dump_payload(const char *_p, int len, struct netmap_ring *ring, int cur)
772b303f675SLuigi Rizzo {
773b303f675SLuigi Rizzo 	char buf[128];
774b303f675SLuigi Rizzo 	int i, j, i0;
77537e3a6d3SLuigi Rizzo 	const unsigned char *p = (const unsigned char *)_p;
776b303f675SLuigi Rizzo 
777b303f675SLuigi Rizzo 	/* get the length in ASCII of the length of the packet. */
778b303f675SLuigi Rizzo 
779ce3ee1e7SLuigi Rizzo 	printf("ring %p cur %5d [buf %6d flags 0x%04x len %5d]\n",
780ce3ee1e7SLuigi Rizzo 		ring, cur, ring->slot[cur].buf_idx,
781ce3ee1e7SLuigi Rizzo 		ring->slot[cur].flags, len);
782b303f675SLuigi Rizzo 	/* hexdump routine */
783b303f675SLuigi Rizzo 	for (i = 0; i < len; ) {
784b797f66cSConrad Meyer 		memset(buf, ' ', sizeof(buf));
785b303f675SLuigi Rizzo 		sprintf(buf, "%5d: ", i);
786b303f675SLuigi Rizzo 		i0 = i;
787b303f675SLuigi Rizzo 		for (j=0; j < 16 && i < len; i++, j++)
788b303f675SLuigi Rizzo 			sprintf(buf+7+j*3, "%02x ", (uint8_t)(p[i]));
789b303f675SLuigi Rizzo 		i = i0;
790b303f675SLuigi Rizzo 		for (j=0; j < 16 && i < len; i++, j++)
791b303f675SLuigi Rizzo 			sprintf(buf+7+j + 48, "%c",
792b303f675SLuigi Rizzo 				isprint(p[i]) ? p[i] : '.');
793b303f675SLuigi Rizzo 		printf("%s\n", buf);
794b303f675SLuigi Rizzo 	}
795b303f675SLuigi Rizzo }
796b303f675SLuigi Rizzo 
79768b8534bSLuigi Rizzo /*
79868b8534bSLuigi Rizzo  * Fill a packet with some payload.
799f8e4e36aSLuigi Rizzo  * We create a UDP packet so the payload starts at
800f8e4e36aSLuigi Rizzo  *	14+20+8 = 42 bytes.
80168b8534bSLuigi Rizzo  */
802f8e4e36aSLuigi Rizzo #ifdef __linux__
803f8e4e36aSLuigi Rizzo #define uh_sport source
804f8e4e36aSLuigi Rizzo #define uh_dport dest
805f8e4e36aSLuigi Rizzo #define uh_ulen len
806f8e4e36aSLuigi Rizzo #define uh_sum check
807f8e4e36aSLuigi Rizzo #endif /* linux */
808b303f675SLuigi Rizzo 
80927bf5dd3SVincenzo Maffione static uint16_t
new_ip_sum(uint16_t ip_sum,uint32_t oaddr,uint32_t naddr)81027bf5dd3SVincenzo Maffione new_ip_sum(uint16_t ip_sum, uint32_t oaddr, uint32_t naddr)
81127bf5dd3SVincenzo Maffione {
81227bf5dd3SVincenzo Maffione 	ip_sum = cksum_add(ip_sum, ~oaddr >> 16);
81327bf5dd3SVincenzo Maffione 	ip_sum = cksum_add(ip_sum, ~oaddr & 0xffff);
81427bf5dd3SVincenzo Maffione 	ip_sum = cksum_add(ip_sum, naddr >> 16);
81527bf5dd3SVincenzo Maffione 	ip_sum = cksum_add(ip_sum, naddr & 0xffff);
81627bf5dd3SVincenzo Maffione 	return ip_sum;
81727bf5dd3SVincenzo Maffione }
81827bf5dd3SVincenzo Maffione 
81927bf5dd3SVincenzo Maffione static uint16_t
new_udp_sum(uint16_t udp_sum,uint16_t oport,uint16_t nport)82027bf5dd3SVincenzo Maffione new_udp_sum(uint16_t udp_sum, uint16_t oport, uint16_t nport)
82127bf5dd3SVincenzo Maffione {
82227bf5dd3SVincenzo Maffione 	udp_sum = cksum_add(udp_sum, ~oport);
82327bf5dd3SVincenzo Maffione 	udp_sum = cksum_add(udp_sum, nport);
82427bf5dd3SVincenzo Maffione 	return udp_sum;
82527bf5dd3SVincenzo Maffione }
82627bf5dd3SVincenzo Maffione 
82727bf5dd3SVincenzo Maffione 
828ce3ee1e7SLuigi Rizzo static void
update_ip(struct pkt * pkt,struct targ * t)82980ad548dSVincenzo Maffione update_ip(struct pkt *pkt, struct targ *t)
830ce3ee1e7SLuigi Rizzo {
83180ad548dSVincenzo Maffione 	struct glob_arg *g = t->g;
83280ad548dSVincenzo Maffione 	struct ip ip;
83380ad548dSVincenzo Maffione 	struct udphdr udp;
83480ad548dSVincenzo Maffione 	uint32_t oaddr, naddr;
83580ad548dSVincenzo Maffione 	uint16_t oport, nport;
83627bf5dd3SVincenzo Maffione 	uint16_t ip_sum = 0, udp_sum = 0;
837ce3ee1e7SLuigi Rizzo 
83880ad548dSVincenzo Maffione 	memcpy(&ip, &pkt->ipv4.ip, sizeof(ip));
83980ad548dSVincenzo Maffione 	memcpy(&udp, &pkt->ipv4.udp, sizeof(udp));
840f2637526SLuigi Rizzo 	do {
84180ad548dSVincenzo Maffione 		ip_sum = udp_sum = 0;
84280ad548dSVincenzo Maffione 		naddr = oaddr = ntohl(ip.ip_src.s_addr);
84380ad548dSVincenzo Maffione 		nport = oport = ntohs(udp.uh_sport);
84456717743SAdrian Chadd 		if (g->options & OPT_RANDOM_SRC) {
84580ad548dSVincenzo Maffione 			ip.ip_src.s_addr = nrand48(t->seed);
84680ad548dSVincenzo Maffione 			udp.uh_sport = nrand48(t->seed);
84780ad548dSVincenzo Maffione 			naddr = ntohl(ip.ip_src.s_addr);
84880ad548dSVincenzo Maffione 			nport = ntohs(udp.uh_sport);
84927bf5dd3SVincenzo Maffione 			ip_sum = new_ip_sum(ip_sum, oaddr, naddr);
85027bf5dd3SVincenzo Maffione 			udp_sum = new_udp_sum(udp_sum, oport, nport);
85127bf5dd3SVincenzo Maffione 		} else {
85280ad548dSVincenzo Maffione 			if (oport < g->src_ip.port1) {
85380ad548dSVincenzo Maffione 				nport = oport + 1;
85480ad548dSVincenzo Maffione 				udp.uh_sport = htons(nport);
85527bf5dd3SVincenzo Maffione 				udp_sum = new_udp_sum(udp_sum, oport, nport);
856f2637526SLuigi Rizzo 				break;
857ce3ee1e7SLuigi Rizzo 			}
85880ad548dSVincenzo Maffione 			nport = g->src_ip.port0;
85980ad548dSVincenzo Maffione 			udp.uh_sport = htons(nport);
86080ad548dSVincenzo Maffione 			if (oaddr < g->src_ip.ipv4.end) {
86180ad548dSVincenzo Maffione 				naddr = oaddr + 1;
86280ad548dSVincenzo Maffione 				ip.ip_src.s_addr = htonl(naddr);
86327bf5dd3SVincenzo Maffione 				ip_sum = new_ip_sum(ip_sum, oaddr, naddr);
864f2637526SLuigi Rizzo 				break;
865ce3ee1e7SLuigi Rizzo 			}
86680ad548dSVincenzo Maffione 			naddr = g->src_ip.ipv4.start;
86780ad548dSVincenzo Maffione 			ip.ip_src.s_addr = htonl(naddr);
86827bf5dd3SVincenzo Maffione 			ip_sum = new_ip_sum(ip_sum, oaddr, naddr);
86980ad548dSVincenzo Maffione 		}
87027bf5dd3SVincenzo Maffione 
87180ad548dSVincenzo Maffione 		naddr = oaddr = ntohl(ip.ip_dst.s_addr);
87280ad548dSVincenzo Maffione 		nport = oport = ntohs(udp.uh_dport);
87380ad548dSVincenzo Maffione 		if (g->options & OPT_RANDOM_DST) {
87480ad548dSVincenzo Maffione 			ip.ip_dst.s_addr = nrand48(t->seed);
87580ad548dSVincenzo Maffione 			udp.uh_dport = nrand48(t->seed);
87680ad548dSVincenzo Maffione 			naddr = ntohl(ip.ip_dst.s_addr);
87780ad548dSVincenzo Maffione 			nport = ntohs(udp.uh_dport);
87827bf5dd3SVincenzo Maffione 			ip_sum = new_ip_sum(ip_sum, oaddr, naddr);
87927bf5dd3SVincenzo Maffione 			udp_sum = new_udp_sum(udp_sum, oport, nport);
88027bf5dd3SVincenzo Maffione 		} else {
88180ad548dSVincenzo Maffione 			if (oport < g->dst_ip.port1) {
88280ad548dSVincenzo Maffione 				nport = oport + 1;
88380ad548dSVincenzo Maffione 				udp.uh_dport = htons(nport);
88427bf5dd3SVincenzo Maffione 				udp_sum = new_udp_sum(udp_sum, oport, nport);
88580ad548dSVincenzo Maffione 				break;
88680ad548dSVincenzo Maffione 			}
88780ad548dSVincenzo Maffione 			nport = g->dst_ip.port0;
88880ad548dSVincenzo Maffione 			udp.uh_dport = htons(nport);
88980ad548dSVincenzo Maffione 			if (oaddr < g->dst_ip.ipv4.end) {
89080ad548dSVincenzo Maffione 				naddr = oaddr + 1;
89180ad548dSVincenzo Maffione 				ip.ip_dst.s_addr = htonl(naddr);
89227bf5dd3SVincenzo Maffione 				ip_sum = new_ip_sum(ip_sum, oaddr, naddr);
89380ad548dSVincenzo Maffione 				break;
89480ad548dSVincenzo Maffione 			}
89580ad548dSVincenzo Maffione 			naddr = g->dst_ip.ipv4.start;
89680ad548dSVincenzo Maffione 			ip.ip_dst.s_addr = htonl(naddr);
89727bf5dd3SVincenzo Maffione 			ip_sum = new_ip_sum(ip_sum, oaddr, naddr);
89827bf5dd3SVincenzo Maffione 		}
89980ad548dSVincenzo Maffione 	} while (0);
90080ad548dSVincenzo Maffione 	/* update checksums */
90180ad548dSVincenzo Maffione 	if (udp_sum != 0)
90280ad548dSVincenzo Maffione 		udp.uh_sum = ~cksum_add(~udp.uh_sum, htons(udp_sum));
90380ad548dSVincenzo Maffione 	if (ip_sum != 0) {
90480ad548dSVincenzo Maffione 		ip.ip_sum = ~cksum_add(~ip.ip_sum, htons(ip_sum));
90580ad548dSVincenzo Maffione 		udp.uh_sum = ~cksum_add(~udp.uh_sum, htons(ip_sum));
90680ad548dSVincenzo Maffione 	}
90780ad548dSVincenzo Maffione 	memcpy(&pkt->ipv4.ip, &ip, sizeof(ip));
90880ad548dSVincenzo Maffione 	memcpy(&pkt->ipv4.udp, &udp, sizeof(udp));
909ce3ee1e7SLuigi Rizzo }
910ce3ee1e7SLuigi Rizzo 
91180ad548dSVincenzo Maffione #ifndef s6_addr16
91280ad548dSVincenzo Maffione #define	s6_addr16	__u6_addr.__u6_addr16
91380ad548dSVincenzo Maffione #endif
91480ad548dSVincenzo Maffione static void
update_ip6(struct pkt * pkt,struct targ * t)91580ad548dSVincenzo Maffione update_ip6(struct pkt *pkt, struct targ *t)
91680ad548dSVincenzo Maffione {
91780ad548dSVincenzo Maffione 	struct glob_arg *g = t->g;
91880ad548dSVincenzo Maffione 	struct ip6_hdr ip6;
91980ad548dSVincenzo Maffione 	struct udphdr udp;
92080ad548dSVincenzo Maffione 	uint16_t udp_sum;
92180ad548dSVincenzo Maffione 	uint16_t oaddr, naddr;
92280ad548dSVincenzo Maffione 	uint16_t oport, nport;
92380ad548dSVincenzo Maffione 	uint8_t group;
92480ad548dSVincenzo Maffione 
92580ad548dSVincenzo Maffione 	memcpy(&ip6, &pkt->ipv6.ip, sizeof(ip6));
92680ad548dSVincenzo Maffione 	memcpy(&udp, &pkt->ipv6.udp, sizeof(udp));
92780ad548dSVincenzo Maffione 	do {
92880ad548dSVincenzo Maffione 		udp_sum = 0;
92980ad548dSVincenzo Maffione 		group = g->src_ip.ipv6.sgroup;
93080ad548dSVincenzo Maffione 		naddr = oaddr = ntohs(ip6.ip6_src.s6_addr16[group]);
93180ad548dSVincenzo Maffione 		nport = oport = ntohs(udp.uh_sport);
93280ad548dSVincenzo Maffione 		if (g->options & OPT_RANDOM_SRC) {
93380ad548dSVincenzo Maffione 			ip6.ip6_src.s6_addr16[group] = nrand48(t->seed);
93480ad548dSVincenzo Maffione 			udp.uh_sport = nrand48(t->seed);
93580ad548dSVincenzo Maffione 			naddr = ntohs(ip6.ip6_src.s6_addr16[group]);
93680ad548dSVincenzo Maffione 			nport = ntohs(udp.uh_sport);
93780ad548dSVincenzo Maffione 			break;
93880ad548dSVincenzo Maffione 		}
93980ad548dSVincenzo Maffione 		if (oport < g->src_ip.port1) {
94080ad548dSVincenzo Maffione 			nport = oport + 1;
94180ad548dSVincenzo Maffione 			udp.uh_sport = htons(nport);
94280ad548dSVincenzo Maffione 			break;
94380ad548dSVincenzo Maffione 		}
94480ad548dSVincenzo Maffione 		nport = g->src_ip.port0;
94580ad548dSVincenzo Maffione 		udp.uh_sport = htons(nport);
94680ad548dSVincenzo Maffione 		if (oaddr < ntohs(g->src_ip.ipv6.end.s6_addr16[group])) {
94780ad548dSVincenzo Maffione 			naddr = oaddr + 1;
94880ad548dSVincenzo Maffione 			ip6.ip6_src.s6_addr16[group] = htons(naddr);
94980ad548dSVincenzo Maffione 			break;
95080ad548dSVincenzo Maffione 		}
95180ad548dSVincenzo Maffione 		naddr = ntohs(g->src_ip.ipv6.start.s6_addr16[group]);
95280ad548dSVincenzo Maffione 		ip6.ip6_src.s6_addr16[group] = htons(naddr);
95327bf5dd3SVincenzo Maffione 
95480ad548dSVincenzo Maffione 		/* update checksums if needed */
95580ad548dSVincenzo Maffione 		if (oaddr != naddr)
95680ad548dSVincenzo Maffione 			udp_sum = cksum_add(~oaddr, naddr);
95780ad548dSVincenzo Maffione 		if (oport != nport)
95880ad548dSVincenzo Maffione 			udp_sum = cksum_add(udp_sum,
95980ad548dSVincenzo Maffione 			    cksum_add(~oport, nport));
96027bf5dd3SVincenzo Maffione 
96180ad548dSVincenzo Maffione 		group = g->dst_ip.ipv6.egroup;
96280ad548dSVincenzo Maffione 		naddr = oaddr = ntohs(ip6.ip6_dst.s6_addr16[group]);
96380ad548dSVincenzo Maffione 		nport = oport = ntohs(udp.uh_dport);
96480ad548dSVincenzo Maffione 		if (g->options & OPT_RANDOM_DST) {
96580ad548dSVincenzo Maffione 			ip6.ip6_dst.s6_addr16[group] = nrand48(t->seed);
96680ad548dSVincenzo Maffione 			udp.uh_dport = nrand48(t->seed);
96780ad548dSVincenzo Maffione 			naddr = ntohs(ip6.ip6_dst.s6_addr16[group]);
96880ad548dSVincenzo Maffione 			nport = ntohs(udp.uh_dport);
96980ad548dSVincenzo Maffione 			break;
97080ad548dSVincenzo Maffione 		}
97180ad548dSVincenzo Maffione 		if (oport < g->dst_ip.port1) {
97280ad548dSVincenzo Maffione 			nport = oport + 1;
97380ad548dSVincenzo Maffione 			udp.uh_dport = htons(nport);
97480ad548dSVincenzo Maffione 			break;
97580ad548dSVincenzo Maffione 		}
97680ad548dSVincenzo Maffione 		nport = g->dst_ip.port0;
97780ad548dSVincenzo Maffione 		udp.uh_dport = htons(nport);
97880ad548dSVincenzo Maffione 		if (oaddr < ntohs(g->dst_ip.ipv6.end.s6_addr16[group])) {
97980ad548dSVincenzo Maffione 			naddr = oaddr + 1;
98080ad548dSVincenzo Maffione 			ip6.ip6_dst.s6_addr16[group] = htons(naddr);
98180ad548dSVincenzo Maffione 			break;
98280ad548dSVincenzo Maffione 		}
98380ad548dSVincenzo Maffione 		naddr = ntohs(g->dst_ip.ipv6.start.s6_addr16[group]);
98480ad548dSVincenzo Maffione 		ip6.ip6_dst.s6_addr16[group] = htons(naddr);
98580ad548dSVincenzo Maffione 	} while (0);
98680ad548dSVincenzo Maffione 	/* update checksums */
98780ad548dSVincenzo Maffione 	if (oaddr != naddr)
98880ad548dSVincenzo Maffione 		udp_sum = cksum_add(udp_sum,
98980ad548dSVincenzo Maffione 		    cksum_add(~oaddr, naddr));
99080ad548dSVincenzo Maffione 	if (oport != nport)
99180ad548dSVincenzo Maffione 		udp_sum = cksum_add(udp_sum,
99280ad548dSVincenzo Maffione 		    cksum_add(~oport, nport));
99380ad548dSVincenzo Maffione 	if (udp_sum != 0)
99480ad548dSVincenzo Maffione 		udp.uh_sum = ~cksum_add(~udp.uh_sum, udp_sum);
99580ad548dSVincenzo Maffione 	memcpy(&pkt->ipv6.ip, &ip6, sizeof(ip6));
99680ad548dSVincenzo Maffione 	memcpy(&pkt->ipv6.udp, &udp, sizeof(udp));
99780ad548dSVincenzo Maffione }
99880ad548dSVincenzo Maffione 
99980ad548dSVincenzo Maffione static void
update_addresses(struct pkt * pkt,struct targ * t)100080ad548dSVincenzo Maffione update_addresses(struct pkt *pkt, struct targ *t)
100180ad548dSVincenzo Maffione {
100280ad548dSVincenzo Maffione 
100380ad548dSVincenzo Maffione 	if (t->g->af == AF_INET)
100480ad548dSVincenzo Maffione 		update_ip(pkt, t);
100580ad548dSVincenzo Maffione 	else
100680ad548dSVincenzo Maffione 		update_ip6(pkt, t);
100780ad548dSVincenzo Maffione }
10088c3b8c83SVincenzo Maffione 
10098c3b8c83SVincenzo Maffione static void
update_ip_size(struct pkt * pkt,int size)10108c3b8c83SVincenzo Maffione update_ip_size(struct pkt *pkt, int size)
10118c3b8c83SVincenzo Maffione {
10128c3b8c83SVincenzo Maffione 	struct ip ip;
10138c3b8c83SVincenzo Maffione 	struct udphdr udp;
10148c3b8c83SVincenzo Maffione 	uint16_t oiplen, niplen;
10158c3b8c83SVincenzo Maffione 	uint16_t nudplen;
10168c3b8c83SVincenzo Maffione 	uint16_t ip_sum = 0;
10178c3b8c83SVincenzo Maffione 
10188c3b8c83SVincenzo Maffione 	memcpy(&ip, &pkt->ipv4.ip, sizeof(ip));
10198c3b8c83SVincenzo Maffione 	memcpy(&udp, &pkt->ipv4.udp, sizeof(udp));
10208c3b8c83SVincenzo Maffione 
10218c3b8c83SVincenzo Maffione 	oiplen = ntohs(ip.ip_len);
10228c3b8c83SVincenzo Maffione 	niplen = size - sizeof(struct ether_header);
10238c3b8c83SVincenzo Maffione 	ip.ip_len = htons(niplen);
10248c3b8c83SVincenzo Maffione 	nudplen = niplen - sizeof(struct ip);
10258c3b8c83SVincenzo Maffione 	udp.uh_ulen = htons(nudplen);
10268c3b8c83SVincenzo Maffione 	ip_sum = new_udp_sum(ip_sum, oiplen, niplen);
10278c3b8c83SVincenzo Maffione 
10288c3b8c83SVincenzo Maffione 	/* update checksums */
10298c3b8c83SVincenzo Maffione 	if (ip_sum != 0)
10308c3b8c83SVincenzo Maffione 		ip.ip_sum = ~cksum_add(~ip.ip_sum, htons(ip_sum));
10318c3b8c83SVincenzo Maffione 
10328c3b8c83SVincenzo Maffione 	udp.uh_sum = 0;
10338c3b8c83SVincenzo Maffione 	/* Magic: taken from sbin/dhclient/packet.c */
10348c3b8c83SVincenzo Maffione 	udp.uh_sum = wrapsum(
10358c3b8c83SVincenzo Maffione 		checksum(&udp, sizeof(udp),	/* udp header */
10368c3b8c83SVincenzo Maffione 		checksum(pkt->ipv4.body,	/* udp payload */
10378c3b8c83SVincenzo Maffione 		nudplen - sizeof(udp),
10388c3b8c83SVincenzo Maffione 		checksum(&ip.ip_src, /* pseudo header */
10398c3b8c83SVincenzo Maffione 		2 * sizeof(ip.ip_src),
10408c3b8c83SVincenzo Maffione 		IPPROTO_UDP + (u_int32_t)ntohs(udp.uh_ulen)))));
10418c3b8c83SVincenzo Maffione 
10428c3b8c83SVincenzo Maffione 	memcpy(&pkt->ipv4.ip, &ip, sizeof(ip));
10438c3b8c83SVincenzo Maffione 	memcpy(&pkt->ipv4.udp, &udp, sizeof(udp));
10448c3b8c83SVincenzo Maffione }
10458c3b8c83SVincenzo Maffione 
10468c3b8c83SVincenzo Maffione static void
update_ip6_size(struct pkt * pkt,int size)10478c3b8c83SVincenzo Maffione update_ip6_size(struct pkt *pkt, int size)
10488c3b8c83SVincenzo Maffione {
10498c3b8c83SVincenzo Maffione 	struct ip6_hdr ip6;
10508c3b8c83SVincenzo Maffione 	struct udphdr udp;
10518c3b8c83SVincenzo Maffione 	uint16_t niplen, nudplen;
10528c3b8c83SVincenzo Maffione 	uint32_t csum;
10538c3b8c83SVincenzo Maffione 
10548c3b8c83SVincenzo Maffione 	memcpy(&ip6, &pkt->ipv6.ip, sizeof(ip6));
10558c3b8c83SVincenzo Maffione 	memcpy(&udp, &pkt->ipv6.udp, sizeof(udp));
10568c3b8c83SVincenzo Maffione 
10578c3b8c83SVincenzo Maffione 	nudplen = niplen = size - sizeof(struct ether_header) - sizeof(ip6);
10588c3b8c83SVincenzo Maffione 	ip6.ip6_plen = htons(niplen);
10598c3b8c83SVincenzo Maffione 	udp.uh_ulen = htons(nudplen);
10608c3b8c83SVincenzo Maffione 
10618c3b8c83SVincenzo Maffione 	/* Save part of pseudo header checksum into csum */
10628c3b8c83SVincenzo Maffione 	udp.uh_sum = 0;
10638c3b8c83SVincenzo Maffione 	csum = IPPROTO_UDP << 24;
10648c3b8c83SVincenzo Maffione 	csum = checksum(&csum, sizeof(csum), nudplen);
10658c3b8c83SVincenzo Maffione 	udp.uh_sum = wrapsum(
10668c3b8c83SVincenzo Maffione 		checksum(&udp, sizeof(udp),	/* udp header */
10678c3b8c83SVincenzo Maffione 		checksum(pkt->ipv6.body,	/* udp payload */
10688c3b8c83SVincenzo Maffione 		nudplen - sizeof(udp),
10698c3b8c83SVincenzo Maffione 		checksum(&pkt->ipv6.ip.ip6_src, /* pseudo header */
10708c3b8c83SVincenzo Maffione 		2 * sizeof(pkt->ipv6.ip.ip6_src), csum))));
10718c3b8c83SVincenzo Maffione 
10728c3b8c83SVincenzo Maffione 	memcpy(&pkt->ipv6.ip, &ip6, sizeof(ip6));
10738c3b8c83SVincenzo Maffione 	memcpy(&pkt->ipv6.udp, &udp, sizeof(udp));
10748c3b8c83SVincenzo Maffione }
10758c3b8c83SVincenzo Maffione 
10768c3b8c83SVincenzo Maffione static void
update_size(struct pkt * pkt,struct targ * t,int size)10778c3b8c83SVincenzo Maffione update_size(struct pkt *pkt, struct targ *t, int size)
10788c3b8c83SVincenzo Maffione {
10798c3b8c83SVincenzo Maffione 	if (t->g->options & OPT_UPDATE_CSUM) {
10808c3b8c83SVincenzo Maffione 		if (t->g->af == AF_INET)
10818c3b8c83SVincenzo Maffione 			update_ip_size(pkt, size);
10828c3b8c83SVincenzo Maffione 		else
10838c3b8c83SVincenzo Maffione 			update_ip6_size(pkt, size);
10848c3b8c83SVincenzo Maffione 	}
10858c3b8c83SVincenzo Maffione }
10868c3b8c83SVincenzo Maffione 
1087ce3ee1e7SLuigi Rizzo /*
1088ce3ee1e7SLuigi Rizzo  * initialize one packet and prepare for the next one.
1089ce3ee1e7SLuigi Rizzo  * The copy could be done better instead of repeating it each time.
1090ce3ee1e7SLuigi Rizzo  */
109168b8534bSLuigi Rizzo static void
initialize_packet(struct targ * targ)109268b8534bSLuigi Rizzo initialize_packet(struct targ *targ)
109368b8534bSLuigi Rizzo {
109468b8534bSLuigi Rizzo 	struct pkt *pkt = &targ->pkt;
109568b8534bSLuigi Rizzo 	struct ether_header *eh;
109680ad548dSVincenzo Maffione 	struct ip6_hdr ip6;
109780ad548dSVincenzo Maffione 	struct ip ip;
109880ad548dSVincenzo Maffione 	struct udphdr udp;
109980ad548dSVincenzo Maffione 	void *udp_ptr;
110080ad548dSVincenzo Maffione 	uint16_t paylen;
110180ad548dSVincenzo Maffione 	uint32_t csum = 0;
1102b303f675SLuigi Rizzo 	const char *payload = targ->g->options & OPT_INDIRECT ?
1103ce3ee1e7SLuigi Rizzo 		indirect_payload : default_payload;
1104f2637526SLuigi Rizzo 	int i, l0 = strlen(payload);
110568b8534bSLuigi Rizzo 
110637e3a6d3SLuigi Rizzo #ifndef NO_PCAP
1107f284c737SGeorge V. Neville-Neil 	char errbuf[PCAP_ERRBUF_SIZE];
1108f284c737SGeorge V. Neville-Neil 	pcap_t *file;
1109f284c737SGeorge V. Neville-Neil 	struct pcap_pkthdr *header;
1110f284c737SGeorge V. Neville-Neil 	const unsigned char *packet;
1111f284c737SGeorge V. Neville-Neil 
1112f284c737SGeorge V. Neville-Neil 	/* Read a packet from a PCAP file if asked. */
1113f284c737SGeorge V. Neville-Neil 	if (targ->g->packet_file != NULL) {
1114f284c737SGeorge V. Neville-Neil 		if ((file = pcap_open_offline(targ->g->packet_file,
1115f284c737SGeorge V. Neville-Neil 			    errbuf)) == NULL)
1116f284c737SGeorge V. Neville-Neil 			D("failed to open pcap file %s",
1117f284c737SGeorge V. Neville-Neil 			    targ->g->packet_file);
1118f284c737SGeorge V. Neville-Neil 		if (pcap_next_ex(file, &header, &packet) < 0)
1119f284c737SGeorge V. Neville-Neil 			D("failed to read packet from %s",
1120f284c737SGeorge V. Neville-Neil 			    targ->g->packet_file);
1121f284c737SGeorge V. Neville-Neil 		if ((targ->frame = malloc(header->caplen)) == NULL)
1122f284c737SGeorge V. Neville-Neil 			D("out of memory");
1123f284c737SGeorge V. Neville-Neil 		bcopy(packet, (unsigned char *)targ->frame, header->caplen);
1124f284c737SGeorge V. Neville-Neil 		targ->g->pkt_size = header->caplen;
1125f284c737SGeorge V. Neville-Neil 		pcap_close(file);
1126f284c737SGeorge V. Neville-Neil 		return;
1127f284c737SGeorge V. Neville-Neil 	}
112837e3a6d3SLuigi Rizzo #endif
1129f284c737SGeorge V. Neville-Neil 
113080ad548dSVincenzo Maffione 	paylen = targ->g->pkt_size - sizeof(*eh) -
113180ad548dSVincenzo Maffione 	    (targ->g->af == AF_INET ? sizeof(ip): sizeof(ip6));
113280ad548dSVincenzo Maffione 
1133ce3ee1e7SLuigi Rizzo 	/* create a nice NUL-terminated string */
1134f2637526SLuigi Rizzo 	for (i = 0; i < paylen; i += l0) {
1135f2637526SLuigi Rizzo 		if (l0 > paylen - i)
1136f2637526SLuigi Rizzo 			l0 = paylen - i; // last round
113780ad548dSVincenzo Maffione 		bcopy(payload, PKT(pkt, body, targ->g->af) + i, l0);
113868b8534bSLuigi Rizzo 	}
113980ad548dSVincenzo Maffione 	PKT(pkt, body, targ->g->af)[i - 1] = '\0';
1140f8e4e36aSLuigi Rizzo 
1141ce3ee1e7SLuigi Rizzo 	/* prepare the headers */
114268b8534bSLuigi Rizzo 	eh = &pkt->eh;
1143f8e4e36aSLuigi Rizzo 	bcopy(&targ->g->src_mac.start, eh->ether_shost, 6);
1144f8e4e36aSLuigi Rizzo 	bcopy(&targ->g->dst_mac.start, eh->ether_dhost, 6);
114580ad548dSVincenzo Maffione 
114680ad548dSVincenzo Maffione 	if (targ->g->af == AF_INET) {
114768b8534bSLuigi Rizzo 		eh->ether_type = htons(ETHERTYPE_IP);
114880ad548dSVincenzo Maffione 		memcpy(&ip, &pkt->ipv4.ip, sizeof(ip));
114980ad548dSVincenzo Maffione 		udp_ptr = &pkt->ipv4.udp;
115080ad548dSVincenzo Maffione 		ip.ip_v = IPVERSION;
115180ad548dSVincenzo Maffione 		ip.ip_hl = sizeof(ip) >> 2;
115280ad548dSVincenzo Maffione 		ip.ip_id = 0;
115380ad548dSVincenzo Maffione 		ip.ip_tos = IPTOS_LOWDELAY;
115480ad548dSVincenzo Maffione 		ip.ip_len = htons(targ->g->pkt_size - sizeof(*eh));
115580ad548dSVincenzo Maffione 		ip.ip_id = 0;
115680ad548dSVincenzo Maffione 		ip.ip_off = htons(IP_DF); /* Don't fragment */
115780ad548dSVincenzo Maffione 		ip.ip_ttl = IPDEFTTL;
115880ad548dSVincenzo Maffione 		ip.ip_p = IPPROTO_UDP;
115980ad548dSVincenzo Maffione 		ip.ip_dst.s_addr = htonl(targ->g->dst_ip.ipv4.start);
116080ad548dSVincenzo Maffione 		ip.ip_src.s_addr = htonl(targ->g->src_ip.ipv4.start);
116180ad548dSVincenzo Maffione 		ip.ip_sum = wrapsum(checksum(&ip, sizeof(ip), 0));
116280ad548dSVincenzo Maffione 		memcpy(&pkt->ipv4.ip, &ip, sizeof(ip));
116380ad548dSVincenzo Maffione 	} else {
116480ad548dSVincenzo Maffione 		eh->ether_type = htons(ETHERTYPE_IPV6);
116580ad548dSVincenzo Maffione 		memcpy(&ip6, &pkt->ipv4.ip, sizeof(ip6));
116680ad548dSVincenzo Maffione 		udp_ptr = &pkt->ipv6.udp;
116780ad548dSVincenzo Maffione 		ip6.ip6_flow = 0;
116880ad548dSVincenzo Maffione 		ip6.ip6_plen = htons(paylen);
116980ad548dSVincenzo Maffione 		ip6.ip6_vfc = IPV6_VERSION;
117080ad548dSVincenzo Maffione 		ip6.ip6_nxt = IPPROTO_UDP;
117180ad548dSVincenzo Maffione 		ip6.ip6_hlim = IPV6_DEFHLIM;
117280ad548dSVincenzo Maffione 		ip6.ip6_src = targ->g->src_ip.ipv6.start;
117380ad548dSVincenzo Maffione 		ip6.ip6_dst = targ->g->dst_ip.ipv6.start;
117480ad548dSVincenzo Maffione 	}
117580ad548dSVincenzo Maffione 	memcpy(&udp, udp_ptr, sizeof(udp));
117680ad548dSVincenzo Maffione 
117780ad548dSVincenzo Maffione 	udp.uh_sport = htons(targ->g->src_ip.port0);
117880ad548dSVincenzo Maffione 	udp.uh_dport = htons(targ->g->dst_ip.port0);
117980ad548dSVincenzo Maffione 	udp.uh_ulen = htons(paylen);
118080ad548dSVincenzo Maffione 	if (targ->g->af == AF_INET) {
118180ad548dSVincenzo Maffione 		/* Magic: taken from sbin/dhclient/packet.c */
118280ad548dSVincenzo Maffione 		udp.uh_sum = wrapsum(
118380ad548dSVincenzo Maffione 		    checksum(&udp, sizeof(udp),	/* udp header */
118480ad548dSVincenzo Maffione 		    checksum(pkt->ipv4.body,	/* udp payload */
118580ad548dSVincenzo Maffione 		    paylen - sizeof(udp),
118680ad548dSVincenzo Maffione 		    checksum(&pkt->ipv4.ip.ip_src, /* pseudo header */
118780ad548dSVincenzo Maffione 			2 * sizeof(pkt->ipv4.ip.ip_src),
118880ad548dSVincenzo Maffione 			IPPROTO_UDP + (u_int32_t)ntohs(udp.uh_ulen)))));
118980ad548dSVincenzo Maffione 		memcpy(&pkt->ipv4.ip, &ip, sizeof(ip));
119080ad548dSVincenzo Maffione 	} else {
119180ad548dSVincenzo Maffione 		/* Save part of pseudo header checksum into csum */
119280ad548dSVincenzo Maffione 		csum = IPPROTO_UDP << 24;
119380ad548dSVincenzo Maffione 		csum = checksum(&csum, sizeof(csum), paylen);
119480ad548dSVincenzo Maffione 		udp.uh_sum = wrapsum(
119580ad548dSVincenzo Maffione 		    checksum(udp_ptr, sizeof(udp),	/* udp header */
119680ad548dSVincenzo Maffione 		    checksum(pkt->ipv6.body,	/* udp payload */
119780ad548dSVincenzo Maffione 		    paylen - sizeof(udp),
119880ad548dSVincenzo Maffione 		    checksum(&pkt->ipv6.ip.ip6_src, /* pseudo header */
119980ad548dSVincenzo Maffione 			2 * sizeof(pkt->ipv6.ip.ip6_src), csum))));
120080ad548dSVincenzo Maffione 		memcpy(&pkt->ipv6.ip, &ip6, sizeof(ip6));
120180ad548dSVincenzo Maffione 	}
120280ad548dSVincenzo Maffione 	memcpy(udp_ptr, &udp, sizeof(udp));
120317885a7bSLuigi Rizzo 
120417885a7bSLuigi Rizzo 	bzero(&pkt->vh, sizeof(pkt->vh));
1205b303f675SLuigi Rizzo 	// dump_payload((void *)pkt, targ->g->pkt_size, NULL, 0);
120668b8534bSLuigi Rizzo }
120768b8534bSLuigi Rizzo 
12084bf50f18SLuigi Rizzo static void
get_vnet_hdr_len(struct glob_arg * g)120937e3a6d3SLuigi Rizzo get_vnet_hdr_len(struct glob_arg *g)
12104bf50f18SLuigi Rizzo {
12114bfe1a4fSVincenzo Maffione 	struct nmreq_header hdr;
12124bfe1a4fSVincenzo Maffione 	struct nmreq_port_hdr ph;
121337e3a6d3SLuigi Rizzo 	int err;
121437e3a6d3SLuigi Rizzo 
12154bfe1a4fSVincenzo Maffione 	hdr = g->nmd->hdr; /* copy name and version */
12164bfe1a4fSVincenzo Maffione 	hdr.nr_reqtype = NETMAP_REQ_PORT_HDR_GET;
12174bfe1a4fSVincenzo Maffione 	hdr.nr_options = 0;
12184bfe1a4fSVincenzo Maffione 	memset(&ph, 0, sizeof(ph));
12194bfe1a4fSVincenzo Maffione 	hdr.nr_body = (uintptr_t)&ph;
12204bfe1a4fSVincenzo Maffione 	err = ioctl(g->main_fd, NIOCCTRL, &hdr);
122137e3a6d3SLuigi Rizzo 	if (err) {
122237e3a6d3SLuigi Rizzo 		D("Unable to get virtio-net header length");
122337e3a6d3SLuigi Rizzo 		return;
122437e3a6d3SLuigi Rizzo 	}
122537e3a6d3SLuigi Rizzo 
12264bfe1a4fSVincenzo Maffione 	g->virt_header = ph.nr_hdr_len;
122737e3a6d3SLuigi Rizzo 	if (g->virt_header) {
122837e3a6d3SLuigi Rizzo 		D("Port requires virtio-net header, length = %d",
122937e3a6d3SLuigi Rizzo 		  g->virt_header);
123037e3a6d3SLuigi Rizzo 	}
123137e3a6d3SLuigi Rizzo }
123237e3a6d3SLuigi Rizzo 
123337e3a6d3SLuigi Rizzo static void
set_vnet_hdr_len(struct glob_arg * g)123437e3a6d3SLuigi Rizzo set_vnet_hdr_len(struct glob_arg *g)
123537e3a6d3SLuigi Rizzo {
123637e3a6d3SLuigi Rizzo 	int err, l = g->virt_header;
12374bfe1a4fSVincenzo Maffione 	struct nmreq_header hdr;
12384bfe1a4fSVincenzo Maffione 	struct nmreq_port_hdr ph;
12394bf50f18SLuigi Rizzo 
12404bf50f18SLuigi Rizzo 	if (l == 0)
12414bf50f18SLuigi Rizzo 		return;
12424bf50f18SLuigi Rizzo 
12434bfe1a4fSVincenzo Maffione 	hdr = g->nmd->hdr; /* copy name and version */
12444bfe1a4fSVincenzo Maffione 	hdr.nr_reqtype = NETMAP_REQ_PORT_HDR_SET;
12454bfe1a4fSVincenzo Maffione 	hdr.nr_options = 0;
12464bfe1a4fSVincenzo Maffione 	memset(&ph, 0, sizeof(ph));
12474bfe1a4fSVincenzo Maffione 	hdr.nr_body = (uintptr_t)&ph;
12484bfe1a4fSVincenzo Maffione 	err = ioctl(g->main_fd, NIOCCTRL, &hdr);
12494bf50f18SLuigi Rizzo 	if (err) {
125037e3a6d3SLuigi Rizzo 		D("Unable to set virtio-net header length %d", l);
12514bf50f18SLuigi Rizzo 	}
12524bf50f18SLuigi Rizzo }
125368b8534bSLuigi Rizzo 
125468b8534bSLuigi Rizzo /*
125568b8534bSLuigi Rizzo  * create and enqueue a batch of packets on a ring.
125668b8534bSLuigi Rizzo  * On the last one set NS_REPORT to tell the driver to generate
125768b8534bSLuigi Rizzo  * an interrupt when done.
125868b8534bSLuigi Rizzo  */
125968b8534bSLuigi Rizzo static int
send_packets(struct netmap_ring * ring,struct pkt * pkt,void * frame,int size,struct targ * t,u_int count,int options)126017885a7bSLuigi Rizzo send_packets(struct netmap_ring *ring, struct pkt *pkt, void *frame,
126180ad548dSVincenzo Maffione 		int size, struct targ *t, u_int count, int options)
126268b8534bSLuigi Rizzo {
1263406e7723SVincenzo Maffione 	u_int n, sent, head = ring->head;
126480ad548dSVincenzo Maffione 	u_int frags = t->frags;
126580ad548dSVincenzo Maffione 	u_int frag_size = t->frag_size;
1266406e7723SVincenzo Maffione 	struct netmap_slot *slot = &ring->slot[head];
126768b8534bSLuigi Rizzo 
126817885a7bSLuigi Rizzo 	n = nm_ring_space(ring);
126999fb123fSLuigi Rizzo #if 0
127099fb123fSLuigi Rizzo 	if (options & (OPT_COPY | OPT_PREFETCH) ) {
127168b8534bSLuigi Rizzo 		for (sent = 0; sent < count; sent++) {
1272406e7723SVincenzo Maffione 			struct netmap_slot *slot = &ring->slot[head];
127368b8534bSLuigi Rizzo 			char *p = NETMAP_BUF(ring, slot->buf_idx);
127468b8534bSLuigi Rizzo 
1275f2637526SLuigi Rizzo 			__builtin_prefetch(p);
1276406e7723SVincenzo Maffione 			head = nm_ring_next(ring, head);
127799fb123fSLuigi Rizzo 		}
1278406e7723SVincenzo Maffione 		head = ring->head;
127999fb123fSLuigi Rizzo 	}
128099fb123fSLuigi Rizzo #endif
128180ad548dSVincenzo Maffione 	for (sent = 0; sent < count && n >= frags; sent++, n--) {
128280ad548dSVincenzo Maffione 		char *p;
128380ad548dSVincenzo Maffione 		int buf_changed;
128480ad548dSVincenzo Maffione 		u_int tosend = size;
128580ad548dSVincenzo Maffione 
1286406e7723SVincenzo Maffione 		slot = &ring->slot[head];
128780ad548dSVincenzo Maffione 		p = NETMAP_BUF(ring, slot->buf_idx);
128880ad548dSVincenzo Maffione 		buf_changed = slot->flags & NS_BUF_CHANGED;
128999fb123fSLuigi Rizzo 
1290b303f675SLuigi Rizzo 		slot->flags = 0;
129137e3a6d3SLuigi Rizzo 		if (options & OPT_RUBBISH) {
129237e3a6d3SLuigi Rizzo 			/* do nothing */
129337e3a6d3SLuigi Rizzo 		} else if (options & OPT_INDIRECT) {
1294b303f675SLuigi Rizzo 			slot->flags |= NS_INDIRECT;
129537e3a6d3SLuigi Rizzo 			slot->ptr = (uint64_t)((uintptr_t)frame);
129680ad548dSVincenzo Maffione 		} else if (frags > 1) {
129780ad548dSVincenzo Maffione 			u_int i;
129880ad548dSVincenzo Maffione 			const char *f = frame;
129980ad548dSVincenzo Maffione 			char *fp = p;
130080ad548dSVincenzo Maffione 			for (i = 0; i < frags - 1; i++) {
130180ad548dSVincenzo Maffione 				memcpy(fp, f, frag_size);
130280ad548dSVincenzo Maffione 				slot->len = frag_size;
130380ad548dSVincenzo Maffione 				slot->flags = NS_MOREFRAG;
130480ad548dSVincenzo Maffione 				if (options & OPT_DUMP)
1305406e7723SVincenzo Maffione 					dump_payload(fp, frag_size, ring, head);
130680ad548dSVincenzo Maffione 				tosend -= frag_size;
130780ad548dSVincenzo Maffione 				f += frag_size;
1308406e7723SVincenzo Maffione 				head = nm_ring_next(ring, head);
1309406e7723SVincenzo Maffione 				slot = &ring->slot[head];
131080ad548dSVincenzo Maffione 				fp = NETMAP_BUF(ring, slot->buf_idx);
131180ad548dSVincenzo Maffione 			}
131280ad548dSVincenzo Maffione 			n -= (frags - 1);
131380ad548dSVincenzo Maffione 			p = fp;
131480ad548dSVincenzo Maffione 			slot->flags = 0;
131580ad548dSVincenzo Maffione 			memcpy(p, f, tosend);
131680ad548dSVincenzo Maffione 			update_addresses(pkt, t);
131780ad548dSVincenzo Maffione 		} else if ((options & (OPT_COPY | OPT_MEMCPY)) || buf_changed) {
131880ad548dSVincenzo Maffione 			if (options & OPT_COPY)
1319f0ea3689SLuigi Rizzo 				nm_pkt_copy(frame, p, size);
132080ad548dSVincenzo Maffione 			else
132117885a7bSLuigi Rizzo 				memcpy(p, frame, size);
132280ad548dSVincenzo Maffione 			update_addresses(pkt, t);
1323ce3ee1e7SLuigi Rizzo 		} else if (options & OPT_PREFETCH) {
1324f2637526SLuigi Rizzo 			__builtin_prefetch(p);
1325ce3ee1e7SLuigi Rizzo 		}
132680ad548dSVincenzo Maffione 		slot->len = tosend;
1327ce3ee1e7SLuigi Rizzo 		if (options & OPT_DUMP)
1328406e7723SVincenzo Maffione 			dump_payload(p, tosend, ring, head);
1329406e7723SVincenzo Maffione 		head = nm_ring_next(ring, head);
133068b8534bSLuigi Rizzo 	}
133180ad548dSVincenzo Maffione 	if (sent) {
133280ad548dSVincenzo Maffione 		slot->flags |= NS_REPORT;
1333406e7723SVincenzo Maffione 		ring->head = ring->cur = head;
133480ad548dSVincenzo Maffione 	}
133580ad548dSVincenzo Maffione 	if (sent < count) {
133680ad548dSVincenzo Maffione 		/* tell netmap that we need more slots */
133780ad548dSVincenzo Maffione 		ring->cur = ring->tail;
133880ad548dSVincenzo Maffione 	}
133968b8534bSLuigi Rizzo 
134068b8534bSLuigi Rizzo 	return (sent);
134168b8534bSLuigi Rizzo }
134268b8534bSLuigi Rizzo 
1343f8e4e36aSLuigi Rizzo /*
134437e3a6d3SLuigi Rizzo  * Index of the highest bit set
134537e3a6d3SLuigi Rizzo  */
13467eb32dc8SVincenzo Maffione static uint32_t
msb64(uint64_t x)134737e3a6d3SLuigi Rizzo msb64(uint64_t x)
134837e3a6d3SLuigi Rizzo {
134937e3a6d3SLuigi Rizzo 	uint64_t m = 1ULL << 63;
135037e3a6d3SLuigi Rizzo 	int i;
135137e3a6d3SLuigi Rizzo 
135237e3a6d3SLuigi Rizzo 	for (i = 63; i >= 0; i--, m >>=1)
135337e3a6d3SLuigi Rizzo 		if (m & x)
135437e3a6d3SLuigi Rizzo 			return i;
135537e3a6d3SLuigi Rizzo 	return 0;
135637e3a6d3SLuigi Rizzo }
135737e3a6d3SLuigi Rizzo 
135837e3a6d3SLuigi Rizzo /*
135980ad548dSVincenzo Maffione  * wait until ts, either busy or sleeping if more than 1ms.
136080ad548dSVincenzo Maffione  * Return wakeup time.
136180ad548dSVincenzo Maffione  */
136280ad548dSVincenzo Maffione static struct timespec
wait_time(struct timespec ts)136380ad548dSVincenzo Maffione wait_time(struct timespec ts)
136480ad548dSVincenzo Maffione {
136580ad548dSVincenzo Maffione 	for (;;) {
136680ad548dSVincenzo Maffione 		struct timespec w, cur;
136780ad548dSVincenzo Maffione 		clock_gettime(CLOCK_REALTIME_PRECISE, &cur);
136880ad548dSVincenzo Maffione 		w = timespec_sub(ts, cur);
136980ad548dSVincenzo Maffione 		if (w.tv_sec < 0)
137080ad548dSVincenzo Maffione 			return cur;
137180ad548dSVincenzo Maffione 		else if (w.tv_sec > 0 || w.tv_nsec > 1000000)
137280ad548dSVincenzo Maffione 			poll(NULL, 0, 1);
137380ad548dSVincenzo Maffione 	}
137480ad548dSVincenzo Maffione }
137580ad548dSVincenzo Maffione 
137680ad548dSVincenzo Maffione /*
1377f8e4e36aSLuigi Rizzo  * Send a packet, and wait for a response.
1378f8e4e36aSLuigi Rizzo  * The payload (after UDP header, ofs 42) has a 4-byte sequence
1379f8e4e36aSLuigi Rizzo  * followed by a struct timeval (or bintime?)
1380f8e4e36aSLuigi Rizzo  */
1381f8e4e36aSLuigi Rizzo 
138268b8534bSLuigi Rizzo static void *
ping_body(void * data)138380ad548dSVincenzo Maffione ping_body(void *data)
138468b8534bSLuigi Rizzo {
138568b8534bSLuigi Rizzo 	struct targ *targ = (struct targ *) data;
1386f0ea3689SLuigi Rizzo 	struct pollfd pfd = { .fd = targ->fd, .events = POLLIN };
1387f0ea3689SLuigi Rizzo 	struct netmap_if *nifp = targ->nmd->nifp;
1388538c66eaSMark Johnston 	int i, m;
138917885a7bSLuigi Rizzo 	void *frame;
139017885a7bSLuigi Rizzo 	int size;
1391f0ea3689SLuigi Rizzo 	struct timespec ts, now, last_print;
139280ad548dSVincenzo Maffione 	struct timespec nexttime = {0, 0}; /* silence compiler */
139337e3a6d3SLuigi Rizzo 	uint64_t sent = 0, n = targ->g->npackets;
139437e3a6d3SLuigi Rizzo 	uint64_t count = 0, t_cur, t_min = ~0, av = 0;
139580ad548dSVincenzo Maffione 	uint64_t g_min = ~0, g_av = 0;
139637e3a6d3SLuigi Rizzo 	uint64_t buckets[64];	/* bins for delays, ns */
139780ad548dSVincenzo Maffione 	int rate_limit = targ->g->tx_rate, tosend = 0;
139817885a7bSLuigi Rizzo 
139980ad548dSVincenzo Maffione 	frame = (char*)&targ->pkt + sizeof(targ->pkt.vh) - targ->g->virt_header;
140017885a7bSLuigi Rizzo 	size = targ->g->pkt_size + targ->g->virt_header;
1401e5ecae38SEd Maste 
140237e3a6d3SLuigi Rizzo 
1403f8e4e36aSLuigi Rizzo 	if (targ->g->nthreads > 1) {
1404f8e4e36aSLuigi Rizzo 		D("can only ping with 1 thread");
1405f8e4e36aSLuigi Rizzo 		return NULL;
1406f95a30bdSEd Maste 	}
1407f8e4e36aSLuigi Rizzo 
14089a7abd93SVincenzo Maffione 	if (targ->g->af == AF_INET6) {
14099a7abd93SVincenzo Maffione 		D("Warning: ping-pong with IPv6 not supported");
14109a7abd93SVincenzo Maffione 	}
14119a7abd93SVincenzo Maffione 
141237e3a6d3SLuigi Rizzo 	bzero(&buckets, sizeof(buckets));
1413f8e4e36aSLuigi Rizzo 	clock_gettime(CLOCK_REALTIME_PRECISE, &last_print);
141417885a7bSLuigi Rizzo 	now = last_print;
141580ad548dSVincenzo Maffione 	if (rate_limit) {
141680ad548dSVincenzo Maffione 		targ->tic = timespec_add(now, (struct timespec){2,0});
141780ad548dSVincenzo Maffione 		targ->tic.tv_nsec = 0;
141880ad548dSVincenzo Maffione 		wait_time(targ->tic);
141980ad548dSVincenzo Maffione 		nexttime = targ->tic;
142080ad548dSVincenzo Maffione 	}
142137e3a6d3SLuigi Rizzo 	while (!targ->cancel && (n == 0 || sent < n)) {
142280ad548dSVincenzo Maffione 		struct netmap_ring *ring = NETMAP_TXRING(nifp, targ->nmd->first_tx_ring);
1423f8e4e36aSLuigi Rizzo 		struct netmap_slot *slot;
1424f8e4e36aSLuigi Rizzo 		char *p;
142580ad548dSVincenzo Maffione 		int rv;
142680ad548dSVincenzo Maffione 		uint64_t limit, event = 0;
142780ad548dSVincenzo Maffione 
142880ad548dSVincenzo Maffione 		if (rate_limit && tosend <= 0) {
142980ad548dSVincenzo Maffione 			tosend = targ->g->burst;
143080ad548dSVincenzo Maffione 			nexttime = timespec_add(nexttime, targ->g->tx_period);
143180ad548dSVincenzo Maffione 			wait_time(nexttime);
143280ad548dSVincenzo Maffione 		}
143380ad548dSVincenzo Maffione 
143480ad548dSVincenzo Maffione 		limit = rate_limit ? tosend : targ->g->burst;
143580ad548dSVincenzo Maffione 		if (n > 0 && n - sent < limit)
143680ad548dSVincenzo Maffione 			limit = n - sent;
143780ad548dSVincenzo Maffione 		for (m = 0; (unsigned)m < limit; m++) {
1438406e7723SVincenzo Maffione 			slot = &ring->slot[ring->head];
143917885a7bSLuigi Rizzo 			slot->len = size;
1440f8e4e36aSLuigi Rizzo 			p = NETMAP_BUF(ring, slot->buf_idx);
1441f8e4e36aSLuigi Rizzo 
144217885a7bSLuigi Rizzo 			if (nm_ring_empty(ring)) {
1443f8e4e36aSLuigi Rizzo 				D("-- ouch, cannot send");
144480ad548dSVincenzo Maffione 				break;
1445f8e4e36aSLuigi Rizzo 			} else {
14464bf50f18SLuigi Rizzo 				struct tstamp *tp;
1447f0ea3689SLuigi Rizzo 				nm_pkt_copy(frame, p, size);
1448f8e4e36aSLuigi Rizzo 				clock_gettime(CLOCK_REALTIME_PRECISE, &ts);
1449f8e4e36aSLuigi Rizzo 				bcopy(&sent, p+42, sizeof(sent));
14504bf50f18SLuigi Rizzo 				tp = (struct tstamp *)(p+46);
14514bf50f18SLuigi Rizzo 				tp->sec = (uint32_t)ts.tv_sec;
14524bf50f18SLuigi Rizzo 				tp->nsec = (uint32_t)ts.tv_nsec;
1453f8e4e36aSLuigi Rizzo 				sent++;
1454406e7723SVincenzo Maffione 				ring->head = ring->cur = nm_ring_next(ring, ring->head);
1455f8e4e36aSLuigi Rizzo 			}
1456f8e4e36aSLuigi Rizzo 		}
145780ad548dSVincenzo Maffione 		if (m > 0)
145880ad548dSVincenzo Maffione 			event++;
145980ad548dSVincenzo Maffione 		targ->ctr.pkts = sent;
146080ad548dSVincenzo Maffione 		targ->ctr.bytes = sent*size;
146180ad548dSVincenzo Maffione 		targ->ctr.events = event;
146280ad548dSVincenzo Maffione 		if (rate_limit)
146380ad548dSVincenzo Maffione 			tosend -= m;
146480ad548dSVincenzo Maffione #ifdef BUSYWAIT
146580ad548dSVincenzo Maffione 		rv = ioctl(pfd.fd, NIOCTXSYNC, NULL);
146680ad548dSVincenzo Maffione 		if (rv < 0) {
146780ad548dSVincenzo Maffione 			D("TXSYNC error on queue %d: %s", targ->me,
146817885a7bSLuigi Rizzo 				strerror(errno));
146980ad548dSVincenzo Maffione 		}
147080ad548dSVincenzo Maffione 	again:
147180ad548dSVincenzo Maffione 		ioctl(pfd.fd, NIOCRXSYNC, NULL);
147280ad548dSVincenzo Maffione #else
147380ad548dSVincenzo Maffione 		/* should use a parameter to decide how often to send */
147480ad548dSVincenzo Maffione 		if ( (rv = poll(&pfd, 1, 3000)) <= 0) {
147580ad548dSVincenzo Maffione 			D("poll error on queue %d: %s", targ->me,
147680ad548dSVincenzo Maffione 				(rv ? strerror(errno) : "timeout"));
1477f8e4e36aSLuigi Rizzo 			continue;
1478f8e4e36aSLuigi Rizzo 		}
147980ad548dSVincenzo Maffione #endif /* BUSYWAIT */
1480f8e4e36aSLuigi Rizzo 		/* see what we got back */
1481538c66eaSMark Johnston #ifdef BUSYWAIT
1482538c66eaSMark Johnston 		int rx = 0;
1483538c66eaSMark Johnston #endif
148480ad548dSVincenzo Maffione 		for (i = targ->nmd->first_rx_ring;
148580ad548dSVincenzo Maffione 			i <= targ->nmd->last_rx_ring; i++) {
1486f8e4e36aSLuigi Rizzo 			ring = NETMAP_RXRING(nifp, i);
148717885a7bSLuigi Rizzo 			while (!nm_ring_empty(ring)) {
1488f8e4e36aSLuigi Rizzo 				uint32_t seq;
14894bf50f18SLuigi Rizzo 				struct tstamp *tp;
149037e3a6d3SLuigi Rizzo 				int pos;
149137e3a6d3SLuigi Rizzo 
1492406e7723SVincenzo Maffione 				slot = &ring->slot[ring->head];
1493f8e4e36aSLuigi Rizzo 				p = NETMAP_BUF(ring, slot->buf_idx);
1494f8e4e36aSLuigi Rizzo 
1495f8e4e36aSLuigi Rizzo 				clock_gettime(CLOCK_REALTIME_PRECISE, &now);
1496f8e4e36aSLuigi Rizzo 				bcopy(p+42, &seq, sizeof(seq));
14974bf50f18SLuigi Rizzo 				tp = (struct tstamp *)(p+46);
14984bf50f18SLuigi Rizzo 				ts.tv_sec = (time_t)tp->sec;
14994bf50f18SLuigi Rizzo 				ts.tv_nsec = (long)tp->nsec;
1500f8e4e36aSLuigi Rizzo 				ts.tv_sec = now.tv_sec - ts.tv_sec;
1501f8e4e36aSLuigi Rizzo 				ts.tv_nsec = now.tv_nsec - ts.tv_nsec;
1502f8e4e36aSLuigi Rizzo 				if (ts.tv_nsec < 0) {
1503f8e4e36aSLuigi Rizzo 					ts.tv_nsec += 1000000000;
1504f8e4e36aSLuigi Rizzo 					ts.tv_sec--;
1505f8e4e36aSLuigi Rizzo 				}
150680ad548dSVincenzo Maffione 				if (0) D("seq %d/%llu delta %d.%09d", seq,
150780ad548dSVincenzo Maffione 					(unsigned long long)sent,
1508f8e4e36aSLuigi Rizzo 					(int)ts.tv_sec, (int)ts.tv_nsec);
150937e3a6d3SLuigi Rizzo 				t_cur = ts.tv_sec * 1000000000UL + ts.tv_nsec;
151037e3a6d3SLuigi Rizzo 				if (t_cur < t_min)
151137e3a6d3SLuigi Rizzo 					t_min = t_cur;
1512f8e4e36aSLuigi Rizzo 				count ++;
151337e3a6d3SLuigi Rizzo 				av += t_cur;
151437e3a6d3SLuigi Rizzo 				pos = msb64(t_cur);
151537e3a6d3SLuigi Rizzo 				buckets[pos]++;
151637e3a6d3SLuigi Rizzo 				/* now store it in a bucket */
1517406e7723SVincenzo Maffione 				ring->head = ring->cur = nm_ring_next(ring, ring->head);
1518538c66eaSMark Johnston #ifdef BUSYWAIT
1519f8e4e36aSLuigi Rizzo 				rx++;
1520538c66eaSMark Johnston #endif
1521f8e4e36aSLuigi Rizzo 			}
1522f8e4e36aSLuigi Rizzo 		}
1523f8e4e36aSLuigi Rizzo 		//D("tx %d rx %d", sent, rx);
1524f8e4e36aSLuigi Rizzo 		//usleep(100000);
1525f8e4e36aSLuigi Rizzo 		ts.tv_sec = now.tv_sec - last_print.tv_sec;
1526f8e4e36aSLuigi Rizzo 		ts.tv_nsec = now.tv_nsec - last_print.tv_nsec;
1527f8e4e36aSLuigi Rizzo 		if (ts.tv_nsec < 0) {
1528f8e4e36aSLuigi Rizzo 			ts.tv_nsec += 1000000000;
1529f8e4e36aSLuigi Rizzo 			ts.tv_sec--;
1530f8e4e36aSLuigi Rizzo 		}
1531f8e4e36aSLuigi Rizzo 		if (ts.tv_sec >= 1) {
153237e3a6d3SLuigi Rizzo 			D("count %d RTT: min %d av %d ns",
153337e3a6d3SLuigi Rizzo 				(int)count, (int)t_min, (int)(av/count));
153480ad548dSVincenzo Maffione 			int k, j, kmin, off;
153537e3a6d3SLuigi Rizzo 			char buf[512];
153637e3a6d3SLuigi Rizzo 
153737e3a6d3SLuigi Rizzo 			for (kmin = 0; kmin < 64; kmin ++)
153837e3a6d3SLuigi Rizzo 				if (buckets[kmin])
153937e3a6d3SLuigi Rizzo 					break;
154037e3a6d3SLuigi Rizzo 			for (k = 63; k >= kmin; k--)
154137e3a6d3SLuigi Rizzo 				if (buckets[k])
154237e3a6d3SLuigi Rizzo 					break;
154337e3a6d3SLuigi Rizzo 			buf[0] = '\0';
154480ad548dSVincenzo Maffione 			off = 0;
154580ad548dSVincenzo Maffione 			for (j = kmin; j <= k; j++) {
154680ad548dSVincenzo Maffione 				off += sprintf(buf + off, " %5d", (int)buckets[j]);
154780ad548dSVincenzo Maffione 			}
154837e3a6d3SLuigi Rizzo 			D("k: %d .. %d\n\t%s", 1<<kmin, 1<<k, buf);
154937e3a6d3SLuigi Rizzo 			bzero(&buckets, sizeof(buckets));
1550f8e4e36aSLuigi Rizzo 			count = 0;
155180ad548dSVincenzo Maffione 			g_av += av;
1552f8e4e36aSLuigi Rizzo 			av = 0;
155380ad548dSVincenzo Maffione 			if (t_min < g_min)
155480ad548dSVincenzo Maffione 				g_min = t_min;
155537e3a6d3SLuigi Rizzo 			t_min = ~0;
1556f8e4e36aSLuigi Rizzo 			last_print = now;
1557f8e4e36aSLuigi Rizzo 		}
155880ad548dSVincenzo Maffione #ifdef BUSYWAIT
155980ad548dSVincenzo Maffione 		if (rx < m && ts.tv_sec <= 3 && !targ->cancel)
156080ad548dSVincenzo Maffione 			goto again;
156180ad548dSVincenzo Maffione #endif /* BUSYWAIT */
1562f8e4e36aSLuigi Rizzo 	}
156337e3a6d3SLuigi Rizzo 
156480ad548dSVincenzo Maffione 	if (sent > 0) {
156580ad548dSVincenzo Maffione 		D("RTT over %llu packets: min %d av %d ns",
156680ad548dSVincenzo Maffione 			(long long unsigned)sent, (int)g_min,
156780ad548dSVincenzo Maffione 			(int)((double)g_av/sent));
156880ad548dSVincenzo Maffione 	}
156980ad548dSVincenzo Maffione 	targ->completed = 1;
157080ad548dSVincenzo Maffione 
157137e3a6d3SLuigi Rizzo 	/* reset the ``used`` flag. */
157237e3a6d3SLuigi Rizzo 	targ->used = 0;
157337e3a6d3SLuigi Rizzo 
1574f8e4e36aSLuigi Rizzo 	return NULL;
1575f8e4e36aSLuigi Rizzo }
1576f8e4e36aSLuigi Rizzo 
1577f8e4e36aSLuigi Rizzo 
1578f8e4e36aSLuigi Rizzo /*
1579f8e4e36aSLuigi Rizzo  * reply to ping requests
1580f8e4e36aSLuigi Rizzo  */
1581f8e4e36aSLuigi Rizzo static void *
pong_body(void * data)158280ad548dSVincenzo Maffione pong_body(void *data)
1583f8e4e36aSLuigi Rizzo {
1584f8e4e36aSLuigi Rizzo 	struct targ *targ = (struct targ *) data;
1585f0ea3689SLuigi Rizzo 	struct pollfd pfd = { .fd = targ->fd, .events = POLLIN };
1586f0ea3689SLuigi Rizzo 	struct netmap_if *nifp = targ->nmd->nifp;
1587f8e4e36aSLuigi Rizzo 	struct netmap_ring *txring, *rxring;
1588538c66eaSMark Johnston 	int i;
158937e3a6d3SLuigi Rizzo 	uint64_t sent = 0, n = targ->g->npackets;
1590f8e4e36aSLuigi Rizzo 
1591f8e4e36aSLuigi Rizzo 	if (targ->g->nthreads > 1) {
1592f8e4e36aSLuigi Rizzo 		D("can only reply ping with 1 thread");
1593f8e4e36aSLuigi Rizzo 		return NULL;
1594f8e4e36aSLuigi Rizzo 	}
159580ad548dSVincenzo Maffione 	if (n > 0)
159680ad548dSVincenzo Maffione 		D("understood ponger %llu but don't know how to do it",
159780ad548dSVincenzo Maffione 			(unsigned long long)n);
15989a7abd93SVincenzo Maffione 
15999a7abd93SVincenzo Maffione 	if (targ->g->af == AF_INET6) {
16009a7abd93SVincenzo Maffione 		D("Warning: ping-pong with IPv6 not supported");
16019a7abd93SVincenzo Maffione 	}
16029a7abd93SVincenzo Maffione 
160337e3a6d3SLuigi Rizzo 	while (!targ->cancel && (n == 0 || sent < n)) {
1604406e7723SVincenzo Maffione 		uint32_t txhead, txavail;
1605f8e4e36aSLuigi Rizzo //#define BUSYWAIT
1606f8e4e36aSLuigi Rizzo #ifdef BUSYWAIT
1607f0ea3689SLuigi Rizzo 		ioctl(pfd.fd, NIOCRXSYNC, NULL);
1608f8e4e36aSLuigi Rizzo #else
160980ad548dSVincenzo Maffione 		int rv;
161080ad548dSVincenzo Maffione 		if ( (rv = poll(&pfd, 1, 1000)) <= 0) {
161180ad548dSVincenzo Maffione 			D("poll error on queue %d: %s", targ->me,
161280ad548dSVincenzo Maffione 				rv ? strerror(errno) : "timeout");
1613f8e4e36aSLuigi Rizzo 			continue;
1614f8e4e36aSLuigi Rizzo 		}
1615f8e4e36aSLuigi Rizzo #endif
161680ad548dSVincenzo Maffione 		txring = NETMAP_TXRING(nifp, targ->nmd->first_tx_ring);
1617406e7723SVincenzo Maffione 		txhead = txring->head;
161817885a7bSLuigi Rizzo 		txavail = nm_ring_space(txring);
1619f8e4e36aSLuigi Rizzo 		/* see what we got back */
1620f0ea3689SLuigi Rizzo 		for (i = targ->nmd->first_rx_ring; i <= targ->nmd->last_rx_ring; i++) {
1621f8e4e36aSLuigi Rizzo 			rxring = NETMAP_RXRING(nifp, i);
162217885a7bSLuigi Rizzo 			while (!nm_ring_empty(rxring)) {
1623f8e4e36aSLuigi Rizzo 				uint16_t *spkt, *dpkt;
1624406e7723SVincenzo Maffione 				uint32_t head = rxring->head;
1625406e7723SVincenzo Maffione 				struct netmap_slot *slot = &rxring->slot[head];
1626f8e4e36aSLuigi Rizzo 				char *src, *dst;
1627f8e4e36aSLuigi Rizzo 				src = NETMAP_BUF(rxring, slot->buf_idx);
1628f8e4e36aSLuigi Rizzo 				//D("got pkt %p of size %d", src, slot->len);
1629406e7723SVincenzo Maffione 				rxring->head = rxring->cur = nm_ring_next(rxring, head);
1630f8e4e36aSLuigi Rizzo 				if (txavail == 0)
1631f8e4e36aSLuigi Rizzo 					continue;
1632f8e4e36aSLuigi Rizzo 				dst = NETMAP_BUF(txring,
1633406e7723SVincenzo Maffione 				    txring->slot[txhead].buf_idx);
1634f8e4e36aSLuigi Rizzo 				/* copy... */
1635f8e4e36aSLuigi Rizzo 				dpkt = (uint16_t *)dst;
1636f8e4e36aSLuigi Rizzo 				spkt = (uint16_t *)src;
1637f0ea3689SLuigi Rizzo 				nm_pkt_copy(src, dst, slot->len);
163880ad548dSVincenzo Maffione 				/* swap source and destination MAC */
1639f8e4e36aSLuigi Rizzo 				dpkt[0] = spkt[3];
1640f8e4e36aSLuigi Rizzo 				dpkt[1] = spkt[4];
1641f8e4e36aSLuigi Rizzo 				dpkt[2] = spkt[5];
1642f8e4e36aSLuigi Rizzo 				dpkt[3] = spkt[0];
1643f8e4e36aSLuigi Rizzo 				dpkt[4] = spkt[1];
1644f8e4e36aSLuigi Rizzo 				dpkt[5] = spkt[2];
16459a7abd93SVincenzo Maffione 				/* swap source and destination IPv4 */
16469a7abd93SVincenzo Maffione 				if (spkt[6] == htons(ETHERTYPE_IP)) {
16479a7abd93SVincenzo Maffione 					dpkt[13] = spkt[15];
16489a7abd93SVincenzo Maffione 					dpkt[14] = spkt[16];
16499a7abd93SVincenzo Maffione 					dpkt[15] = spkt[13];
16509a7abd93SVincenzo Maffione 					dpkt[16] = spkt[14];
16519a7abd93SVincenzo Maffione 				}
1652406e7723SVincenzo Maffione 				txring->slot[txhead].len = slot->len;
16539a7abd93SVincenzo Maffione 				//dump_payload(dst, slot->len, txring, txhead);
1654406e7723SVincenzo Maffione 				txhead = nm_ring_next(txring, txhead);
1655f8e4e36aSLuigi Rizzo 				txavail--;
1656f8e4e36aSLuigi Rizzo 				sent++;
1657f8e4e36aSLuigi Rizzo 			}
1658f8e4e36aSLuigi Rizzo 		}
1659406e7723SVincenzo Maffione 		txring->head = txring->cur = txhead;
166037e3a6d3SLuigi Rizzo 		targ->ctr.pkts = sent;
1661f8e4e36aSLuigi Rizzo #ifdef BUSYWAIT
1662f0ea3689SLuigi Rizzo 		ioctl(pfd.fd, NIOCTXSYNC, NULL);
1663f8e4e36aSLuigi Rizzo #endif
1664f8e4e36aSLuigi Rizzo 	}
166537e3a6d3SLuigi Rizzo 
166680ad548dSVincenzo Maffione 	targ->completed = 1;
166780ad548dSVincenzo Maffione 
166837e3a6d3SLuigi Rizzo 	/* reset the ``used`` flag. */
166937e3a6d3SLuigi Rizzo 	targ->used = 0;
167037e3a6d3SLuigi Rizzo 
1671f8e4e36aSLuigi Rizzo 	return NULL;
1672f8e4e36aSLuigi Rizzo }
1673f8e4e36aSLuigi Rizzo 
1674f8e4e36aSLuigi Rizzo 
1675f8e4e36aSLuigi Rizzo static void *
sender_body(void * data)1676f8e4e36aSLuigi Rizzo sender_body(void *data)
1677f8e4e36aSLuigi Rizzo {
1678f8e4e36aSLuigi Rizzo 	struct targ *targ = (struct targ *) data;
1679f0ea3689SLuigi Rizzo 	struct pollfd pfd = { .fd = targ->fd, .events = POLLOUT };
16804bf50f18SLuigi Rizzo 	struct netmap_if *nifp;
168137e3a6d3SLuigi Rizzo 	struct netmap_ring *txring = NULL;
168237e3a6d3SLuigi Rizzo 	int i;
168337e3a6d3SLuigi Rizzo 	uint64_t n = targ->g->npackets / targ->g->nthreads;
168437e3a6d3SLuigi Rizzo 	uint64_t sent = 0;
168537e3a6d3SLuigi Rizzo 	uint64_t event = 0;
1686506336f2SVincenzo Maffione 	int options = targ->g->options;
168717885a7bSLuigi Rizzo 	struct timespec nexttime = { 0, 0}; // XXX silence compiler
16881cb4c501SLuigi Rizzo 	int rate_limit = targ->g->tx_rate;
168917885a7bSLuigi Rizzo 	struct pkt *pkt = &targ->pkt;
169017885a7bSLuigi Rizzo 	void *frame;
169117885a7bSLuigi Rizzo 	int size;
169217885a7bSLuigi Rizzo 
1693f284c737SGeorge V. Neville-Neil 	if (targ->frame == NULL) {
169480ad548dSVincenzo Maffione 		frame = (char *)pkt + sizeof(pkt->vh) - targ->g->virt_header;
169517885a7bSLuigi Rizzo 		size = targ->g->pkt_size + targ->g->virt_header;
1696f284c737SGeorge V. Neville-Neil 	} else {
1697f284c737SGeorge V. Neville-Neil 		frame = targ->frame;
1698f284c737SGeorge V. Neville-Neil 		size = targ->g->pkt_size;
1699f284c737SGeorge V. Neville-Neil 	}
1700b303f675SLuigi Rizzo 
17014bf50f18SLuigi Rizzo 	D("start, fd %d main_fd %d", targ->fd, targ->g->main_fd);
170268b8534bSLuigi Rizzo 	if (setaffinity(targ->thread, targ->affinity))
170368b8534bSLuigi Rizzo 		goto quit;
170468b8534bSLuigi Rizzo 
170568b8534bSLuigi Rizzo 	/* main loop.*/
17061cb4c501SLuigi Rizzo 	clock_gettime(CLOCK_REALTIME_PRECISE, &targ->tic);
17071cb4c501SLuigi Rizzo 	if (rate_limit) {
170817885a7bSLuigi Rizzo 		targ->tic = timespec_add(targ->tic, (struct timespec){2,0});
17091cb4c501SLuigi Rizzo 		targ->tic.tv_nsec = 0;
171017885a7bSLuigi Rizzo 		wait_time(targ->tic);
17111cb4c501SLuigi Rizzo 		nexttime = targ->tic;
17121cb4c501SLuigi Rizzo 	}
1713f2637526SLuigi Rizzo 	if (targ->g->dev_type == DEV_TAP) {
1714f8e4e36aSLuigi Rizzo 	    D("writing to file desc %d", targ->g->main_fd);
1715f8e4e36aSLuigi Rizzo 
1716f8e4e36aSLuigi Rizzo 	    for (i = 0; !targ->cancel && (n == 0 || sent < n); i++) {
171717885a7bSLuigi Rizzo 		if (write(targ->g->main_fd, frame, size) != -1)
1718f8e4e36aSLuigi Rizzo 			sent++;
171980ad548dSVincenzo Maffione 		update_addresses(pkt, targ);
1720f8e4e36aSLuigi Rizzo 		if (i > 10000) {
172137e3a6d3SLuigi Rizzo 			targ->ctr.pkts = sent;
172237e3a6d3SLuigi Rizzo 			targ->ctr.bytes = sent*size;
172337e3a6d3SLuigi Rizzo 			targ->ctr.events = sent;
1724f8e4e36aSLuigi Rizzo 			i = 0;
1725f8e4e36aSLuigi Rizzo 		}
1726f8e4e36aSLuigi Rizzo 	    }
1727f2637526SLuigi Rizzo #ifndef NO_PCAP
1728f2637526SLuigi Rizzo     } else if (targ->g->dev_type == DEV_PCAP) {
1729f2637526SLuigi Rizzo 	    pcap_t *p = targ->g->p;
1730f2637526SLuigi Rizzo 
1731f2637526SLuigi Rizzo 	    for (i = 0; !targ->cancel && (n == 0 || sent < n); i++) {
1732f2637526SLuigi Rizzo 		if (pcap_inject(p, frame, size) != -1)
1733f2637526SLuigi Rizzo 			sent++;
173480ad548dSVincenzo Maffione 		update_addresses(pkt, targ);
1735f2637526SLuigi Rizzo 		if (i > 10000) {
173637e3a6d3SLuigi Rizzo 			targ->ctr.pkts = sent;
173737e3a6d3SLuigi Rizzo 			targ->ctr.bytes = sent*size;
173837e3a6d3SLuigi Rizzo 			targ->ctr.events = sent;
1739f2637526SLuigi Rizzo 			i = 0;
1740f2637526SLuigi Rizzo 		}
1741f2637526SLuigi Rizzo 	    }
1742f2637526SLuigi Rizzo #endif /* NO_PCAP */
174368b8534bSLuigi Rizzo     } else {
17441cb4c501SLuigi Rizzo 	int tosend = 0;
17459e53f3bdSVincenzo Maffione 	u_int bufsz, frag_size = targ->g->frag_size;
1746ce3ee1e7SLuigi Rizzo 
17474bf50f18SLuigi Rizzo 	nifp = targ->nmd->nifp;
174880ad548dSVincenzo Maffione 	txring = NETMAP_TXRING(nifp, targ->nmd->first_tx_ring);
174980ad548dSVincenzo Maffione 	bufsz = txring->nr_buf_size;
17509e53f3bdSVincenzo Maffione 	if (bufsz < frag_size)
17519e53f3bdSVincenzo Maffione 		frag_size = bufsz;
175280ad548dSVincenzo Maffione 	targ->frag_size = targ->g->pkt_size / targ->frags;
17539e53f3bdSVincenzo Maffione 	if (targ->frag_size > frag_size) {
17549e53f3bdSVincenzo Maffione 		targ->frags = targ->g->pkt_size / frag_size;
17559e53f3bdSVincenzo Maffione 		targ->frag_size = frag_size;
17569e53f3bdSVincenzo Maffione 		if (targ->g->pkt_size % frag_size != 0)
175780ad548dSVincenzo Maffione 			targ->frags++;
175880ad548dSVincenzo Maffione 	}
175980ad548dSVincenzo Maffione 	D("frags %u frag_size %u", targ->frags, targ->frag_size);
1760506336f2SVincenzo Maffione 
1761506336f2SVincenzo Maffione 	/* mark all slots of all rings as changed so initial copy will be done */
1762506336f2SVincenzo Maffione 	for (i = targ->nmd->first_tx_ring; i <= targ->nmd->last_tx_ring; i++) {
1763506336f2SVincenzo Maffione 		uint32_t j;
1764506336f2SVincenzo Maffione 		struct netmap_slot *slot;
1765506336f2SVincenzo Maffione 
1766506336f2SVincenzo Maffione 		txring = NETMAP_TXRING(nifp, i);
1767506336f2SVincenzo Maffione 		for (j = 0; j < txring->num_slots; j++) {
1768506336f2SVincenzo Maffione 			slot = &txring->slot[j];
1769506336f2SVincenzo Maffione 			slot->flags = NS_BUF_CHANGED;
1770506336f2SVincenzo Maffione 		}
1771506336f2SVincenzo Maffione 	}
1772506336f2SVincenzo Maffione 
1773f8e4e36aSLuigi Rizzo 	while (!targ->cancel && (n == 0 || sent < n)) {
177480ad548dSVincenzo Maffione 		int rv;
177568b8534bSLuigi Rizzo 
17761cb4c501SLuigi Rizzo 		if (rate_limit && tosend <= 0) {
17771cb4c501SLuigi Rizzo 			tosend = targ->g->burst;
177817885a7bSLuigi Rizzo 			nexttime = timespec_add(nexttime, targ->g->tx_period);
177917885a7bSLuigi Rizzo 			wait_time(nexttime);
17801cb4c501SLuigi Rizzo 		}
17811cb4c501SLuigi Rizzo 
178268b8534bSLuigi Rizzo 		/*
178368b8534bSLuigi Rizzo 		 * wait for available room in the send queue(s)
178468b8534bSLuigi Rizzo 		 */
178537e3a6d3SLuigi Rizzo #ifdef BUSYWAIT
178680ad548dSVincenzo Maffione 		(void)rv;
178737e3a6d3SLuigi Rizzo 		if (ioctl(pfd.fd, NIOCTXSYNC, NULL) < 0) {
178837e3a6d3SLuigi Rizzo 			D("ioctl error on queue %d: %s", targ->me,
178937e3a6d3SLuigi Rizzo 					strerror(errno));
179037e3a6d3SLuigi Rizzo 			goto quit;
179137e3a6d3SLuigi Rizzo 		}
179237e3a6d3SLuigi Rizzo #else /* !BUSYWAIT */
179380ad548dSVincenzo Maffione 		if ( (rv = poll(&pfd, 1, 2000)) <= 0) {
17943fe77e68SEd Maste 			if (targ->cancel)
17953fe77e68SEd Maste 				break;
179680ad548dSVincenzo Maffione 			D("poll error on queue %d: %s", targ->me,
179780ad548dSVincenzo Maffione 				rv ? strerror(errno) : "timeout");
1798f0ea3689SLuigi Rizzo 			// goto quit;
179917885a7bSLuigi Rizzo 		}
1800f0ea3689SLuigi Rizzo 		if (pfd.revents & POLLERR) {
180137e3a6d3SLuigi Rizzo 			D("poll error on %d ring %d-%d", pfd.fd,
180237e3a6d3SLuigi Rizzo 				targ->nmd->first_tx_ring, targ->nmd->last_tx_ring);
180368b8534bSLuigi Rizzo 			goto quit;
180468b8534bSLuigi Rizzo 		}
180537e3a6d3SLuigi Rizzo #endif /* !BUSYWAIT */
180668b8534bSLuigi Rizzo 		/*
180768b8534bSLuigi Rizzo 		 * scan our queues and send on those with room
180868b8534bSLuigi Rizzo 		 */
1809f0ea3689SLuigi Rizzo 		for (i = targ->nmd->first_tx_ring; i <= targ->nmd->last_tx_ring; i++) {
181037e3a6d3SLuigi Rizzo 			int m;
181137e3a6d3SLuigi Rizzo 			uint64_t limit = rate_limit ?  tosend : targ->g->burst;
181280ad548dSVincenzo Maffione 
181380ad548dSVincenzo Maffione 			if (n > 0 && n == sent)
181480ad548dSVincenzo Maffione 				break;
181580ad548dSVincenzo Maffione 
1816f8e4e36aSLuigi Rizzo 			if (n > 0 && n - sent < limit)
1817f8e4e36aSLuigi Rizzo 				limit = n - sent;
181868b8534bSLuigi Rizzo 			txring = NETMAP_TXRING(nifp, i);
181917885a7bSLuigi Rizzo 			if (nm_ring_empty(txring))
182068b8534bSLuigi Rizzo 				continue;
1821ce3ee1e7SLuigi Rizzo 
182280ad548dSVincenzo Maffione 			if (targ->g->pkt_min_size > 0) {
182380ad548dSVincenzo Maffione 				size = nrand48(targ->seed) %
182480ad548dSVincenzo Maffione 					(targ->g->pkt_size - targ->g->pkt_min_size) +
182580ad548dSVincenzo Maffione 					targ->g->pkt_min_size;
18268c3b8c83SVincenzo Maffione 				update_size(pkt, targ, size);
182780ad548dSVincenzo Maffione 			}
182880ad548dSVincenzo Maffione 			m = send_packets(txring, pkt, frame, size, targ,
182980ad548dSVincenzo Maffione 					 limit, options);
183080ad548dSVincenzo Maffione 			ND("limit %lu tail %d m %d",
183180ad548dSVincenzo Maffione 				limit, txring->tail, m);
183268b8534bSLuigi Rizzo 			sent += m;
183337e3a6d3SLuigi Rizzo 			if (m > 0) //XXX-ste: can m be 0?
183437e3a6d3SLuigi Rizzo 				event++;
183537e3a6d3SLuigi Rizzo 			targ->ctr.pkts = sent;
183680ad548dSVincenzo Maffione 			targ->ctr.bytes += m*size;
183737e3a6d3SLuigi Rizzo 			targ->ctr.events = event;
1838ce3ee1e7SLuigi Rizzo 			if (rate_limit) {
1839ce3ee1e7SLuigi Rizzo 				tosend -= m;
1840ce3ee1e7SLuigi Rizzo 				if (tosend <= 0)
1841ce3ee1e7SLuigi Rizzo 					break;
1842ce3ee1e7SLuigi Rizzo 			}
184368b8534bSLuigi Rizzo 		}
184468b8534bSLuigi Rizzo 	}
184599fb123fSLuigi Rizzo 	/* flush any remaining packets */
184680ad548dSVincenzo Maffione 	if (txring != NULL) {
18474bf50f18SLuigi Rizzo 		D("flush tail %d head %d on thread %p",
18484bf50f18SLuigi Rizzo 			txring->tail, txring->head,
184937e3a6d3SLuigi Rizzo 			(void *)pthread_self());
1850f0ea3689SLuigi Rizzo 		ioctl(pfd.fd, NIOCTXSYNC, NULL);
185180ad548dSVincenzo Maffione 	}
185268b8534bSLuigi Rizzo 
185368b8534bSLuigi Rizzo 	/* final part: wait all the TX queues to be empty. */
1854f0ea3689SLuigi Rizzo 	for (i = targ->nmd->first_tx_ring; i <= targ->nmd->last_tx_ring; i++) {
185568b8534bSLuigi Rizzo 		txring = NETMAP_TXRING(nifp, i);
185637e3a6d3SLuigi Rizzo 		while (!targ->cancel && nm_tx_pending(txring)) {
18574bf50f18SLuigi Rizzo 			RD(5, "pending tx tail %d head %d on ring %d",
18584bf50f18SLuigi Rizzo 				txring->tail, txring->head, i);
1859f0ea3689SLuigi Rizzo 			ioctl(pfd.fd, NIOCTXSYNC, NULL);
186068b8534bSLuigi Rizzo 			usleep(1); /* wait 1 tick */
186168b8534bSLuigi Rizzo 		}
186268b8534bSLuigi Rizzo 	}
1863f2637526SLuigi Rizzo     } /* end DEV_NETMAP */
186468b8534bSLuigi Rizzo 
18651cb4c501SLuigi Rizzo 	clock_gettime(CLOCK_REALTIME_PRECISE, &targ->toc);
186668b8534bSLuigi Rizzo 	targ->completed = 1;
186737e3a6d3SLuigi Rizzo 	targ->ctr.pkts = sent;
186837e3a6d3SLuigi Rizzo 	targ->ctr.bytes = sent*size;
186937e3a6d3SLuigi Rizzo 	targ->ctr.events = event;
187068b8534bSLuigi Rizzo quit:
187168b8534bSLuigi Rizzo 	/* reset the ``used`` flag. */
187268b8534bSLuigi Rizzo 	targ->used = 0;
187368b8534bSLuigi Rizzo 
187468b8534bSLuigi Rizzo 	return (NULL);
187568b8534bSLuigi Rizzo }
187668b8534bSLuigi Rizzo 
187768b8534bSLuigi Rizzo 
1878f2637526SLuigi Rizzo #ifndef NO_PCAP
187968b8534bSLuigi Rizzo static void
receive_pcap(u_char * user,const struct pcap_pkthdr * h,const u_char * bytes)1880f8e4e36aSLuigi Rizzo receive_pcap(u_char *user, const struct pcap_pkthdr * h,
1881f8e4e36aSLuigi Rizzo 	const u_char * bytes)
188268b8534bSLuigi Rizzo {
188337e3a6d3SLuigi Rizzo 	struct my_ctrs *ctr = (struct my_ctrs *)user;
1884f8e4e36aSLuigi Rizzo 	(void)bytes;	/* UNUSED */
188537e3a6d3SLuigi Rizzo 	ctr->bytes += h->len;
188637e3a6d3SLuigi Rizzo 	ctr->pkts++;
188768b8534bSLuigi Rizzo }
1888f2637526SLuigi Rizzo #endif /* !NO_PCAP */
188968b8534bSLuigi Rizzo 
189037e3a6d3SLuigi Rizzo 
189168b8534bSLuigi Rizzo static int
receive_packets(struct netmap_ring * ring,u_int limit,int dump,uint64_t * bytes)189237e3a6d3SLuigi Rizzo receive_packets(struct netmap_ring *ring, u_int limit, int dump, uint64_t *bytes)
189368b8534bSLuigi Rizzo {
1894406e7723SVincenzo Maffione 	u_int head, rx, n;
189537e3a6d3SLuigi Rizzo 	uint64_t b = 0;
189680ad548dSVincenzo Maffione 	u_int complete = 0;
189737e3a6d3SLuigi Rizzo 
189837e3a6d3SLuigi Rizzo 	if (bytes == NULL)
189937e3a6d3SLuigi Rizzo 		bytes = &b;
190068b8534bSLuigi Rizzo 
1901406e7723SVincenzo Maffione 	head = ring->head;
190217885a7bSLuigi Rizzo 	n = nm_ring_space(ring);
190317885a7bSLuigi Rizzo 	if (n < limit)
190417885a7bSLuigi Rizzo 		limit = n;
190568b8534bSLuigi Rizzo 	for (rx = 0; rx < limit; rx++) {
1906406e7723SVincenzo Maffione 		struct netmap_slot *slot = &ring->slot[head];
190768b8534bSLuigi Rizzo 		char *p = NETMAP_BUF(ring, slot->buf_idx);
190868b8534bSLuigi Rizzo 
190937e3a6d3SLuigi Rizzo 		*bytes += slot->len;
1910b303f675SLuigi Rizzo 		if (dump)
1911406e7723SVincenzo Maffione 			dump_payload(p, slot->len, ring, head);
191280ad548dSVincenzo Maffione 		if (!(slot->flags & NS_MOREFRAG))
191380ad548dSVincenzo Maffione 			complete++;
191468b8534bSLuigi Rizzo 
1915406e7723SVincenzo Maffione 		head = nm_ring_next(ring, head);
191668b8534bSLuigi Rizzo 	}
1917406e7723SVincenzo Maffione 	ring->head = ring->cur = head;
191868b8534bSLuigi Rizzo 
191980ad548dSVincenzo Maffione 	return (complete);
192068b8534bSLuigi Rizzo }
192168b8534bSLuigi Rizzo 
192268b8534bSLuigi Rizzo static void *
receiver_body(void * data)192368b8534bSLuigi Rizzo receiver_body(void *data)
192468b8534bSLuigi Rizzo {
192568b8534bSLuigi Rizzo 	struct targ *targ = (struct targ *) data;
1926f0ea3689SLuigi Rizzo 	struct pollfd pfd = { .fd = targ->fd, .events = POLLIN };
19274bf50f18SLuigi Rizzo 	struct netmap_if *nifp;
192868b8534bSLuigi Rizzo 	struct netmap_ring *rxring;
1929f8e4e36aSLuigi Rizzo 	int i;
193037e3a6d3SLuigi Rizzo 	struct my_ctrs cur;
193120d684ecSAllan Jude 	uint64_t n = targ->g->npackets / targ->g->nthreads;
193237e3a6d3SLuigi Rizzo 
193380ad548dSVincenzo Maffione 	memset(&cur, 0, sizeof(cur));
193468b8534bSLuigi Rizzo 
193568b8534bSLuigi Rizzo 	if (setaffinity(targ->thread, targ->affinity))
193668b8534bSLuigi Rizzo 		goto quit;
193768b8534bSLuigi Rizzo 
19384bf50f18SLuigi Rizzo 	D("reading from %s fd %d main_fd %d",
19394bf50f18SLuigi Rizzo 		targ->g->ifname, targ->fd, targ->g->main_fd);
194068b8534bSLuigi Rizzo 	/* unbounded wait for the first packet. */
19414bf50f18SLuigi Rizzo 	for (;!targ->cancel;) {
1942f0ea3689SLuigi Rizzo 		i = poll(&pfd, 1, 1000);
1943f0ea3689SLuigi Rizzo 		if (i > 0 && !(pfd.revents & POLLERR))
194468b8534bSLuigi Rizzo 			break;
194580ad548dSVincenzo Maffione 		if (i < 0) {
194680ad548dSVincenzo Maffione 			D("poll() error: %s", strerror(errno));
194780ad548dSVincenzo Maffione 			goto quit;
194880ad548dSVincenzo Maffione 		}
194980ad548dSVincenzo Maffione 		if (pfd.revents & POLLERR) {
195080ad548dSVincenzo Maffione 			D("fd error");
195180ad548dSVincenzo Maffione 			goto quit;
195280ad548dSVincenzo Maffione 		}
1953f0ea3689SLuigi Rizzo 		RD(1, "waiting for initial packets, poll returns %d %d",
1954f0ea3689SLuigi Rizzo 			i, pfd.revents);
195568b8534bSLuigi Rizzo 	}
195668b8534bSLuigi Rizzo 	/* main loop, exit after 1s silence */
19571cb4c501SLuigi Rizzo 	clock_gettime(CLOCK_REALTIME_PRECISE, &targ->tic);
1958f2637526SLuigi Rizzo     if (targ->g->dev_type == DEV_TAP) {
1959950cf4a2SVincenzo Maffione 	while (!targ->cancel && (n == 0 || targ->ctr.pkts < n)) {
19604bf50f18SLuigi Rizzo 		char buf[MAX_BODYSIZE];
1961f8e4e36aSLuigi Rizzo 		/* XXX should we poll ? */
196237e3a6d3SLuigi Rizzo 		i = read(targ->g->main_fd, buf, sizeof(buf));
196337e3a6d3SLuigi Rizzo 		if (i > 0) {
196437e3a6d3SLuigi Rizzo 			targ->ctr.pkts++;
196537e3a6d3SLuigi Rizzo 			targ->ctr.bytes += i;
196637e3a6d3SLuigi Rizzo 			targ->ctr.events++;
196737e3a6d3SLuigi Rizzo 		}
1968f8e4e36aSLuigi Rizzo 	}
1969f2637526SLuigi Rizzo #ifndef NO_PCAP
1970f2637526SLuigi Rizzo     } else if (targ->g->dev_type == DEV_PCAP) {
197120d684ecSAllan Jude 	while (!targ->cancel && (n == 0 || targ->ctr.pkts < n)) {
1972f2637526SLuigi Rizzo 		/* XXX should we poll ? */
19734bf50f18SLuigi Rizzo 		pcap_dispatch(targ->g->p, targ->g->burst, receive_pcap,
197437e3a6d3SLuigi Rizzo 			(u_char *)&targ->ctr);
197537e3a6d3SLuigi Rizzo 		targ->ctr.events++;
1976f2637526SLuigi Rizzo 	}
1977f2637526SLuigi Rizzo #endif /* !NO_PCAP */
197868b8534bSLuigi Rizzo     } else {
1979b303f675SLuigi Rizzo 	int dump = targ->g->options & OPT_DUMP;
19804bf50f18SLuigi Rizzo 
19814bf50f18SLuigi Rizzo 	nifp = targ->nmd->nifp;
198220d684ecSAllan Jude 	while (!targ->cancel && (n == 0 || targ->ctr.pkts < n)) {
198368b8534bSLuigi Rizzo 		/* Once we started to receive packets, wait at most 1 seconds
198468b8534bSLuigi Rizzo 		   before quitting. */
198537e3a6d3SLuigi Rizzo #ifdef BUSYWAIT
198637e3a6d3SLuigi Rizzo 		if (ioctl(pfd.fd, NIOCRXSYNC, NULL) < 0) {
198737e3a6d3SLuigi Rizzo 			D("ioctl error on queue %d: %s", targ->me,
198837e3a6d3SLuigi Rizzo 					strerror(errno));
198937e3a6d3SLuigi Rizzo 			goto quit;
199037e3a6d3SLuigi Rizzo 		}
199137e3a6d3SLuigi Rizzo #else /* !BUSYWAIT */
199237e3a6d3SLuigi Rizzo 		if (poll(&pfd, 1, 1 * 1000) <= 0 && !targ->g->forever) {
199337e3a6d3SLuigi Rizzo 			clock_gettime(CLOCK_REALTIME_PRECISE, &targ->toc);
199437e3a6d3SLuigi Rizzo 			targ->toc.tv_sec -= 1; /* Subtract timeout time. */
199537e3a6d3SLuigi Rizzo 			goto out;
199637e3a6d3SLuigi Rizzo 		}
199737e3a6d3SLuigi Rizzo 
199837e3a6d3SLuigi Rizzo 		if (pfd.revents & POLLERR) {
199937e3a6d3SLuigi Rizzo 			D("poll err");
200037e3a6d3SLuigi Rizzo 			goto quit;
200137e3a6d3SLuigi Rizzo 		}
200237e3a6d3SLuigi Rizzo #endif /* !BUSYWAIT */
200337e3a6d3SLuigi Rizzo 		uint64_t cur_space = 0;
200437e3a6d3SLuigi Rizzo 		for (i = targ->nmd->first_rx_ring; i <= targ->nmd->last_rx_ring; i++) {
200537e3a6d3SLuigi Rizzo 			int m;
200637e3a6d3SLuigi Rizzo 
200737e3a6d3SLuigi Rizzo 			rxring = NETMAP_RXRING(nifp, i);
200837e3a6d3SLuigi Rizzo 			/* compute free space in the ring */
200937e3a6d3SLuigi Rizzo 			m = rxring->head + rxring->num_slots - rxring->tail;
201037e3a6d3SLuigi Rizzo 			if (m >= (int) rxring->num_slots)
201137e3a6d3SLuigi Rizzo 				m -= rxring->num_slots;
201237e3a6d3SLuigi Rizzo 			cur_space += m;
201337e3a6d3SLuigi Rizzo 			if (nm_ring_empty(rxring))
201437e3a6d3SLuigi Rizzo 				continue;
201537e3a6d3SLuigi Rizzo 
201637e3a6d3SLuigi Rizzo 			m = receive_packets(rxring, targ->g->burst, dump, &cur.bytes);
201737e3a6d3SLuigi Rizzo 			cur.pkts += m;
201880ad548dSVincenzo Maffione 			if (m > 0)
201937e3a6d3SLuigi Rizzo 				cur.events++;
202037e3a6d3SLuigi Rizzo 		}
202137e3a6d3SLuigi Rizzo 		cur.min_space = targ->ctr.min_space;
202237e3a6d3SLuigi Rizzo 		if (cur_space < cur.min_space)
202337e3a6d3SLuigi Rizzo 			cur.min_space = cur_space;
202437e3a6d3SLuigi Rizzo 		targ->ctr = cur;
202537e3a6d3SLuigi Rizzo 	}
202637e3a6d3SLuigi Rizzo     }
202737e3a6d3SLuigi Rizzo 
202837e3a6d3SLuigi Rizzo 	clock_gettime(CLOCK_REALTIME_PRECISE, &targ->toc);
202937e3a6d3SLuigi Rizzo 
203037e3a6d3SLuigi Rizzo #if !defined(BUSYWAIT)
203137e3a6d3SLuigi Rizzo out:
203237e3a6d3SLuigi Rizzo #endif
203337e3a6d3SLuigi Rizzo 	targ->completed = 1;
203437e3a6d3SLuigi Rizzo 	targ->ctr = cur;
203537e3a6d3SLuigi Rizzo 
203637e3a6d3SLuigi Rizzo quit:
203737e3a6d3SLuigi Rizzo 	/* reset the ``used`` flag. */
203837e3a6d3SLuigi Rizzo 	targ->used = 0;
203937e3a6d3SLuigi Rizzo 
204037e3a6d3SLuigi Rizzo 	return (NULL);
204137e3a6d3SLuigi Rizzo }
204237e3a6d3SLuigi Rizzo 
204337e3a6d3SLuigi Rizzo static void *
txseq_body(void * data)204437e3a6d3SLuigi Rizzo txseq_body(void *data)
204537e3a6d3SLuigi Rizzo {
204637e3a6d3SLuigi Rizzo 	struct targ *targ = (struct targ *) data;
204737e3a6d3SLuigi Rizzo 	struct pollfd pfd = { .fd = targ->fd, .events = POLLOUT };
204837e3a6d3SLuigi Rizzo 	struct netmap_ring *ring;
204937e3a6d3SLuigi Rizzo 	int64_t sent = 0;
205037e3a6d3SLuigi Rizzo 	uint64_t event = 0;
205137e3a6d3SLuigi Rizzo 	int options = targ->g->options | OPT_COPY;
205237e3a6d3SLuigi Rizzo 	struct timespec nexttime = {0, 0};
205337e3a6d3SLuigi Rizzo 	int rate_limit = targ->g->tx_rate;
205437e3a6d3SLuigi Rizzo 	struct pkt *pkt = &targ->pkt;
205537e3a6d3SLuigi Rizzo 	int frags = targ->g->frags;
205637e3a6d3SLuigi Rizzo 	uint32_t sequence = 0;
205737e3a6d3SLuigi Rizzo 	int budget = 0;
205837e3a6d3SLuigi Rizzo 	void *frame;
205937e3a6d3SLuigi Rizzo 	int size;
206037e3a6d3SLuigi Rizzo 
206137e3a6d3SLuigi Rizzo 	if (targ->g->nthreads > 1) {
206237e3a6d3SLuigi Rizzo 		D("can only txseq ping with 1 thread");
206337e3a6d3SLuigi Rizzo 		return NULL;
206437e3a6d3SLuigi Rizzo 	}
206537e3a6d3SLuigi Rizzo 
206637e3a6d3SLuigi Rizzo 	if (targ->g->npackets > 0) {
206737e3a6d3SLuigi Rizzo 		D("Ignoring -n argument");
206837e3a6d3SLuigi Rizzo 	}
206937e3a6d3SLuigi Rizzo 
207080ad548dSVincenzo Maffione 	frame = (char *)pkt + sizeof(pkt->vh) - targ->g->virt_header;
207137e3a6d3SLuigi Rizzo 	size = targ->g->pkt_size + targ->g->virt_header;
207237e3a6d3SLuigi Rizzo 
207337e3a6d3SLuigi Rizzo 	D("start, fd %d main_fd %d", targ->fd, targ->g->main_fd);
207437e3a6d3SLuigi Rizzo 	if (setaffinity(targ->thread, targ->affinity))
207537e3a6d3SLuigi Rizzo 		goto quit;
207637e3a6d3SLuigi Rizzo 
207737e3a6d3SLuigi Rizzo 	clock_gettime(CLOCK_REALTIME_PRECISE, &targ->tic);
207837e3a6d3SLuigi Rizzo 	if (rate_limit) {
207937e3a6d3SLuigi Rizzo 		targ->tic = timespec_add(targ->tic, (struct timespec){2,0});
208037e3a6d3SLuigi Rizzo 		targ->tic.tv_nsec = 0;
208137e3a6d3SLuigi Rizzo 		wait_time(targ->tic);
208237e3a6d3SLuigi Rizzo 		nexttime = targ->tic;
208337e3a6d3SLuigi Rizzo 	}
208437e3a6d3SLuigi Rizzo 
208537e3a6d3SLuigi Rizzo 	/* Only use the first queue. */
208637e3a6d3SLuigi Rizzo 	ring = NETMAP_TXRING(targ->nmd->nifp, targ->nmd->first_tx_ring);
208737e3a6d3SLuigi Rizzo 
208837e3a6d3SLuigi Rizzo 	while (!targ->cancel) {
208937e3a6d3SLuigi Rizzo 		int64_t limit;
209037e3a6d3SLuigi Rizzo 		unsigned int space;
209137e3a6d3SLuigi Rizzo 		unsigned int head;
209237e3a6d3SLuigi Rizzo 		int fcnt;
209380ad548dSVincenzo Maffione 		uint16_t sum = 0;
209480ad548dSVincenzo Maffione 		int rv;
209537e3a6d3SLuigi Rizzo 
209637e3a6d3SLuigi Rizzo 		if (!rate_limit) {
209737e3a6d3SLuigi Rizzo 			budget = targ->g->burst;
209837e3a6d3SLuigi Rizzo 
209937e3a6d3SLuigi Rizzo 		} else if (budget <= 0) {
210037e3a6d3SLuigi Rizzo 			budget = targ->g->burst;
210137e3a6d3SLuigi Rizzo 			nexttime = timespec_add(nexttime, targ->g->tx_period);
210237e3a6d3SLuigi Rizzo 			wait_time(nexttime);
210337e3a6d3SLuigi Rizzo 		}
210437e3a6d3SLuigi Rizzo 
210537e3a6d3SLuigi Rizzo 		/* wait for available room in the send queue */
210680ad548dSVincenzo Maffione #ifdef BUSYWAIT
210780ad548dSVincenzo Maffione 		(void)rv;
210880ad548dSVincenzo Maffione 		if (ioctl(pfd.fd, NIOCTXSYNC, NULL) < 0) {
210980ad548dSVincenzo Maffione 			D("ioctl error on queue %d: %s", targ->me,
211080ad548dSVincenzo Maffione 					strerror(errno));
211180ad548dSVincenzo Maffione 			goto quit;
211280ad548dSVincenzo Maffione 		}
211380ad548dSVincenzo Maffione #else /* !BUSYWAIT */
211480ad548dSVincenzo Maffione 		if ( (rv = poll(&pfd, 1, 2000)) <= 0) {
211537e3a6d3SLuigi Rizzo 			if (targ->cancel)
211637e3a6d3SLuigi Rizzo 				break;
211780ad548dSVincenzo Maffione 			D("poll error on queue %d: %s", targ->me,
211880ad548dSVincenzo Maffione 				rv ? strerror(errno) : "timeout");
211980ad548dSVincenzo Maffione 			// goto quit;
212037e3a6d3SLuigi Rizzo 		}
212137e3a6d3SLuigi Rizzo 		if (pfd.revents & POLLERR) {
212237e3a6d3SLuigi Rizzo 			D("poll error on %d ring %d-%d", pfd.fd,
212337e3a6d3SLuigi Rizzo 				targ->nmd->first_tx_ring, targ->nmd->last_tx_ring);
212437e3a6d3SLuigi Rizzo 			goto quit;
212537e3a6d3SLuigi Rizzo 		}
212680ad548dSVincenzo Maffione #endif /* !BUSYWAIT */
212737e3a6d3SLuigi Rizzo 
212837e3a6d3SLuigi Rizzo 		/* If no room poll() again. */
212937e3a6d3SLuigi Rizzo 		space = nm_ring_space(ring);
213037e3a6d3SLuigi Rizzo 		if (!space) {
213137e3a6d3SLuigi Rizzo 			continue;
213237e3a6d3SLuigi Rizzo 		}
213337e3a6d3SLuigi Rizzo 
213437e3a6d3SLuigi Rizzo 		limit = budget;
213537e3a6d3SLuigi Rizzo 
213637e3a6d3SLuigi Rizzo 		if (space < limit) {
213737e3a6d3SLuigi Rizzo 			limit = space;
213837e3a6d3SLuigi Rizzo 		}
213937e3a6d3SLuigi Rizzo 
214037e3a6d3SLuigi Rizzo 		/* Cut off ``limit`` to make sure is multiple of ``frags``. */
214137e3a6d3SLuigi Rizzo 		if (frags > 1) {
214237e3a6d3SLuigi Rizzo 			limit = (limit / frags) * frags;
214337e3a6d3SLuigi Rizzo 		}
214437e3a6d3SLuigi Rizzo 
214537e3a6d3SLuigi Rizzo 		limit = sent + limit; /* Convert to absolute. */
214637e3a6d3SLuigi Rizzo 
214737e3a6d3SLuigi Rizzo 		for (fcnt = frags, head = ring->head;
214837e3a6d3SLuigi Rizzo 				sent < limit; sent++, sequence++) {
214937e3a6d3SLuigi Rizzo 			struct netmap_slot *slot = &ring->slot[head];
215037e3a6d3SLuigi Rizzo 			char *p = NETMAP_BUF(ring, slot->buf_idx);
215180ad548dSVincenzo Maffione 			uint16_t *w = (uint16_t *)PKT(pkt, body, targ->g->af), t;
215280ad548dSVincenzo Maffione 
215380ad548dSVincenzo Maffione 			memcpy(&sum, targ->g->af == AF_INET ? &pkt->ipv4.udp.uh_sum : &pkt->ipv6.udp.uh_sum, sizeof(sum));
215437e3a6d3SLuigi Rizzo 
215537e3a6d3SLuigi Rizzo 			slot->flags = 0;
215680ad548dSVincenzo Maffione 			t = *w;
215780ad548dSVincenzo Maffione 			PKT(pkt, body, targ->g->af)[0] = sequence >> 24;
215880ad548dSVincenzo Maffione 			PKT(pkt, body, targ->g->af)[1] = (sequence >> 16) & 0xff;
215980ad548dSVincenzo Maffione 			sum = ~cksum_add(~sum, cksum_add(~t, *w));
216080ad548dSVincenzo Maffione 			t = *++w;
216180ad548dSVincenzo Maffione 			PKT(pkt, body, targ->g->af)[2] = (sequence >> 8) & 0xff;
216280ad548dSVincenzo Maffione 			PKT(pkt, body, targ->g->af)[3] = sequence & 0xff;
216380ad548dSVincenzo Maffione 			sum = ~cksum_add(~sum, cksum_add(~t, *w));
216480ad548dSVincenzo Maffione 			memcpy(targ->g->af == AF_INET ? &pkt->ipv4.udp.uh_sum : &pkt->ipv6.udp.uh_sum, &sum, sizeof(sum));
216537e3a6d3SLuigi Rizzo 			nm_pkt_copy(frame, p, size);
216637e3a6d3SLuigi Rizzo 			if (fcnt == frags) {
216780ad548dSVincenzo Maffione 				update_addresses(pkt, targ);
216837e3a6d3SLuigi Rizzo 			}
216937e3a6d3SLuigi Rizzo 
217037e3a6d3SLuigi Rizzo 			if (options & OPT_DUMP) {
217137e3a6d3SLuigi Rizzo 				dump_payload(p, size, ring, head);
217237e3a6d3SLuigi Rizzo 			}
217337e3a6d3SLuigi Rizzo 
217437e3a6d3SLuigi Rizzo 			slot->len = size;
217537e3a6d3SLuigi Rizzo 
217637e3a6d3SLuigi Rizzo 			if (--fcnt > 0) {
217737e3a6d3SLuigi Rizzo 				slot->flags |= NS_MOREFRAG;
217837e3a6d3SLuigi Rizzo 			} else {
217937e3a6d3SLuigi Rizzo 				fcnt = frags;
218037e3a6d3SLuigi Rizzo 			}
218137e3a6d3SLuigi Rizzo 
218237e3a6d3SLuigi Rizzo 			if (sent == limit - 1) {
218337e3a6d3SLuigi Rizzo 				/* Make sure we don't push an incomplete
218437e3a6d3SLuigi Rizzo 				 * packet. */
218537e3a6d3SLuigi Rizzo 				assert(!(slot->flags & NS_MOREFRAG));
218637e3a6d3SLuigi Rizzo 				slot->flags |= NS_REPORT;
218737e3a6d3SLuigi Rizzo 			}
218837e3a6d3SLuigi Rizzo 
218937e3a6d3SLuigi Rizzo 			head = nm_ring_next(ring, head);
219037e3a6d3SLuigi Rizzo 			if (rate_limit) {
219137e3a6d3SLuigi Rizzo 				budget--;
219237e3a6d3SLuigi Rizzo 			}
219337e3a6d3SLuigi Rizzo 		}
219437e3a6d3SLuigi Rizzo 
219537e3a6d3SLuigi Rizzo 		ring->cur = ring->head = head;
219637e3a6d3SLuigi Rizzo 
219737e3a6d3SLuigi Rizzo 		event ++;
219837e3a6d3SLuigi Rizzo 		targ->ctr.pkts = sent;
219937e3a6d3SLuigi Rizzo 		targ->ctr.bytes = sent * size;
220037e3a6d3SLuigi Rizzo 		targ->ctr.events = event;
220137e3a6d3SLuigi Rizzo 	}
220237e3a6d3SLuigi Rizzo 
220337e3a6d3SLuigi Rizzo 	/* flush any remaining packets */
220437e3a6d3SLuigi Rizzo 	D("flush tail %d head %d on thread %p",
220537e3a6d3SLuigi Rizzo 		ring->tail, ring->head,
220637e3a6d3SLuigi Rizzo 		(void *)pthread_self());
220737e3a6d3SLuigi Rizzo 	ioctl(pfd.fd, NIOCTXSYNC, NULL);
220837e3a6d3SLuigi Rizzo 
220937e3a6d3SLuigi Rizzo 	/* final part: wait the TX queues to become empty. */
221037e3a6d3SLuigi Rizzo 	while (!targ->cancel && nm_tx_pending(ring)) {
221137e3a6d3SLuigi Rizzo 		RD(5, "pending tx tail %d head %d on ring %d",
221237e3a6d3SLuigi Rizzo 				ring->tail, ring->head, targ->nmd->first_tx_ring);
221337e3a6d3SLuigi Rizzo 		ioctl(pfd.fd, NIOCTXSYNC, NULL);
221437e3a6d3SLuigi Rizzo 		usleep(1); /* wait 1 tick */
221537e3a6d3SLuigi Rizzo 	}
221637e3a6d3SLuigi Rizzo 
221737e3a6d3SLuigi Rizzo 	clock_gettime(CLOCK_REALTIME_PRECISE, &targ->toc);
221837e3a6d3SLuigi Rizzo 	targ->completed = 1;
221937e3a6d3SLuigi Rizzo 	targ->ctr.pkts = sent;
222037e3a6d3SLuigi Rizzo 	targ->ctr.bytes = sent * size;
222137e3a6d3SLuigi Rizzo 	targ->ctr.events = event;
222237e3a6d3SLuigi Rizzo quit:
222337e3a6d3SLuigi Rizzo 	/* reset the ``used`` flag. */
222437e3a6d3SLuigi Rizzo 	targ->used = 0;
222537e3a6d3SLuigi Rizzo 
222637e3a6d3SLuigi Rizzo 	return (NULL);
222737e3a6d3SLuigi Rizzo }
222837e3a6d3SLuigi Rizzo 
222937e3a6d3SLuigi Rizzo 
223037e3a6d3SLuigi Rizzo static char *
multi_slot_to_string(struct netmap_ring * ring,unsigned int head,unsigned int nfrags,char * strbuf,size_t strbuflen)223137e3a6d3SLuigi Rizzo multi_slot_to_string(struct netmap_ring *ring, unsigned int head,
223237e3a6d3SLuigi Rizzo 		     unsigned int nfrags, char *strbuf, size_t strbuflen)
223337e3a6d3SLuigi Rizzo {
223437e3a6d3SLuigi Rizzo 	unsigned int f;
223537e3a6d3SLuigi Rizzo 	char *ret = strbuf;
223637e3a6d3SLuigi Rizzo 
223737e3a6d3SLuigi Rizzo 	for (f = 0; f < nfrags; f++) {
223837e3a6d3SLuigi Rizzo 		struct netmap_slot *slot = &ring->slot[head];
223937e3a6d3SLuigi Rizzo 		int m = snprintf(strbuf, strbuflen, "|%u,%x|", slot->len,
224037e3a6d3SLuigi Rizzo 				 slot->flags);
224137e3a6d3SLuigi Rizzo 		if (m >= (int)strbuflen) {
224237e3a6d3SLuigi Rizzo 			break;
224337e3a6d3SLuigi Rizzo 		}
224437e3a6d3SLuigi Rizzo 		strbuf += m;
224537e3a6d3SLuigi Rizzo 		strbuflen -= m;
224637e3a6d3SLuigi Rizzo 
224737e3a6d3SLuigi Rizzo 		head = nm_ring_next(ring, head);
224837e3a6d3SLuigi Rizzo 	}
224937e3a6d3SLuigi Rizzo 
225037e3a6d3SLuigi Rizzo 	return ret;
225137e3a6d3SLuigi Rizzo }
225237e3a6d3SLuigi Rizzo 
225337e3a6d3SLuigi Rizzo static void *
rxseq_body(void * data)225437e3a6d3SLuigi Rizzo rxseq_body(void *data)
225537e3a6d3SLuigi Rizzo {
225637e3a6d3SLuigi Rizzo 	struct targ *targ = (struct targ *) data;
225737e3a6d3SLuigi Rizzo 	struct pollfd pfd = { .fd = targ->fd, .events = POLLIN };
225837e3a6d3SLuigi Rizzo 	int dump = targ->g->options & OPT_DUMP;
225937e3a6d3SLuigi Rizzo 	struct netmap_ring *ring;
226037e3a6d3SLuigi Rizzo 	unsigned int frags_exp = 1;
226137e3a6d3SLuigi Rizzo 	struct my_ctrs cur;
226237e3a6d3SLuigi Rizzo 	unsigned int frags = 0;
226337e3a6d3SLuigi Rizzo 	int first_packet = 1;
226437e3a6d3SLuigi Rizzo 	int first_slot = 1;
226580ad548dSVincenzo Maffione 	int i, j, af, nrings;
226680ad548dSVincenzo Maffione 	uint32_t seq, *seq_exp = NULL;
226737e3a6d3SLuigi Rizzo 
226880ad548dSVincenzo Maffione 	memset(&cur, 0, sizeof(cur));
226937e3a6d3SLuigi Rizzo 
227037e3a6d3SLuigi Rizzo 	if (setaffinity(targ->thread, targ->affinity))
227137e3a6d3SLuigi Rizzo 		goto quit;
227237e3a6d3SLuigi Rizzo 
227380ad548dSVincenzo Maffione 	nrings = targ->nmd->last_rx_ring - targ->nmd->first_rx_ring + 1;
227480ad548dSVincenzo Maffione 	seq_exp = calloc(nrings, sizeof(uint32_t));
227580ad548dSVincenzo Maffione 	if (seq_exp == NULL) {
227680ad548dSVincenzo Maffione 		D("failed to allocate seq array");
227780ad548dSVincenzo Maffione 		goto quit;
227880ad548dSVincenzo Maffione 	}
227980ad548dSVincenzo Maffione 
228037e3a6d3SLuigi Rizzo 	D("reading from %s fd %d main_fd %d",
228137e3a6d3SLuigi Rizzo 		targ->g->ifname, targ->fd, targ->g->main_fd);
228237e3a6d3SLuigi Rizzo 	/* unbounded wait for the first packet. */
228337e3a6d3SLuigi Rizzo 	for (;!targ->cancel;) {
228437e3a6d3SLuigi Rizzo 		i = poll(&pfd, 1, 1000);
228537e3a6d3SLuigi Rizzo 		if (i > 0 && !(pfd.revents & POLLERR))
228637e3a6d3SLuigi Rizzo 			break;
228737e3a6d3SLuigi Rizzo 		RD(1, "waiting for initial packets, poll returns %d %d",
228837e3a6d3SLuigi Rizzo 			i, pfd.revents);
228937e3a6d3SLuigi Rizzo 	}
229037e3a6d3SLuigi Rizzo 
229137e3a6d3SLuigi Rizzo 	clock_gettime(CLOCK_REALTIME_PRECISE, &targ->tic);
229237e3a6d3SLuigi Rizzo 
229337e3a6d3SLuigi Rizzo 
229437e3a6d3SLuigi Rizzo 	while (!targ->cancel) {
229537e3a6d3SLuigi Rizzo 		unsigned int head;
229637e3a6d3SLuigi Rizzo 		int limit;
229737e3a6d3SLuigi Rizzo 
229880ad548dSVincenzo Maffione #ifdef BUSYWAIT
229980ad548dSVincenzo Maffione 		if (ioctl(pfd.fd, NIOCRXSYNC, NULL) < 0) {
230080ad548dSVincenzo Maffione 			D("ioctl error on queue %d: %s", targ->me,
230180ad548dSVincenzo Maffione 					strerror(errno));
230280ad548dSVincenzo Maffione 			goto quit;
230380ad548dSVincenzo Maffione 		}
230480ad548dSVincenzo Maffione #else /* !BUSYWAIT */
2305f0ea3689SLuigi Rizzo 		if (poll(&pfd, 1, 1 * 1000) <= 0 && !targ->g->forever) {
23061cb4c501SLuigi Rizzo 			clock_gettime(CLOCK_REALTIME_PRECISE, &targ->toc);
23078ce070c1SUlrich Spörlein 			targ->toc.tv_sec -= 1; /* Subtract timeout time. */
2308f0ea3689SLuigi Rizzo 			goto out;
230968b8534bSLuigi Rizzo 		}
231068b8534bSLuigi Rizzo 
2311f0ea3689SLuigi Rizzo 		if (pfd.revents & POLLERR) {
231217885a7bSLuigi Rizzo 			D("poll err");
231317885a7bSLuigi Rizzo 			goto quit;
231417885a7bSLuigi Rizzo 		}
231580ad548dSVincenzo Maffione #endif /* !BUSYWAIT */
231617885a7bSLuigi Rizzo 
231780ad548dSVincenzo Maffione 		for (j = targ->nmd->first_rx_ring; j <= targ->nmd->last_rx_ring; j++) {
231880ad548dSVincenzo Maffione 			ring = NETMAP_RXRING(targ->nmd->nifp, j);
231937e3a6d3SLuigi Rizzo 			if (nm_ring_empty(ring))
232068b8534bSLuigi Rizzo 				continue;
232168b8534bSLuigi Rizzo 
232237e3a6d3SLuigi Rizzo 			limit = nm_ring_space(ring);
232337e3a6d3SLuigi Rizzo 			if (limit > targ->g->burst)
232437e3a6d3SLuigi Rizzo 				limit = targ->g->burst;
232537e3a6d3SLuigi Rizzo 
232637e3a6d3SLuigi Rizzo #if 0
232737e3a6d3SLuigi Rizzo 			/* Enable this if
232837e3a6d3SLuigi Rizzo 			 *     1) we remove the early-return optimization from
232937e3a6d3SLuigi Rizzo 			 *        the netmap poll implementation, or
233037e3a6d3SLuigi Rizzo 			 *     2) pipes get NS_MOREFRAG support.
233137e3a6d3SLuigi Rizzo 			 * With the current netmap implementation, an experiment like
233237e3a6d3SLuigi Rizzo 			 *    pkt-gen -i vale:1{1 -f txseq -F 9
233337e3a6d3SLuigi Rizzo 			 *    pkt-gen -i vale:1}1 -f rxseq
233437e3a6d3SLuigi Rizzo 			 * would get stuck as soon as we find nm_ring_space(ring) < 9,
233537e3a6d3SLuigi Rizzo 			 * since here limit is rounded to 0 and
233637e3a6d3SLuigi Rizzo 			 * pipe rxsync is not called anymore by the poll() of this loop.
233737e3a6d3SLuigi Rizzo 			 */
233837e3a6d3SLuigi Rizzo 			if (frags_exp > 1) {
233937e3a6d3SLuigi Rizzo 				int o = limit;
234037e3a6d3SLuigi Rizzo 				/* Cut off to the closest smaller multiple. */
234137e3a6d3SLuigi Rizzo 				limit = (limit / frags_exp) * frags_exp;
234237e3a6d3SLuigi Rizzo 				RD(2, "LIMIT %d --> %d", o, limit);
234368b8534bSLuigi Rizzo 			}
234437e3a6d3SLuigi Rizzo #endif
234537e3a6d3SLuigi Rizzo 
234637e3a6d3SLuigi Rizzo 			for (head = ring->head, i = 0; i < limit; i++) {
234737e3a6d3SLuigi Rizzo 				struct netmap_slot *slot = &ring->slot[head];
234837e3a6d3SLuigi Rizzo 				char *p = NETMAP_BUF(ring, slot->buf_idx);
234937e3a6d3SLuigi Rizzo 				int len = slot->len;
235037e3a6d3SLuigi Rizzo 				struct pkt *pkt;
235137e3a6d3SLuigi Rizzo 
235237e3a6d3SLuigi Rizzo 				if (dump) {
235337e3a6d3SLuigi Rizzo 					dump_payload(p, slot->len, ring, head);
235468b8534bSLuigi Rizzo 				}
235537e3a6d3SLuigi Rizzo 
235637e3a6d3SLuigi Rizzo 				frags++;
235737e3a6d3SLuigi Rizzo 				if (!(slot->flags & NS_MOREFRAG)) {
235837e3a6d3SLuigi Rizzo 					if (first_packet) {
235937e3a6d3SLuigi Rizzo 						first_packet = 0;
236037e3a6d3SLuigi Rizzo 					} else if (frags != frags_exp) {
236137e3a6d3SLuigi Rizzo 						char prbuf[512];
236237e3a6d3SLuigi Rizzo 						RD(1, "Received packets with %u frags, "
236337e3a6d3SLuigi Rizzo 								"expected %u, '%s'", frags, frags_exp,
236480ad548dSVincenzo Maffione 								multi_slot_to_string(ring, head-frags+1,
236580ad548dSVincenzo Maffione 							       	frags,
236637e3a6d3SLuigi Rizzo 									prbuf, sizeof(prbuf)));
236737e3a6d3SLuigi Rizzo 					}
236837e3a6d3SLuigi Rizzo 					first_packet = 0;
236937e3a6d3SLuigi Rizzo 					frags_exp = frags;
237037e3a6d3SLuigi Rizzo 					frags = 0;
237137e3a6d3SLuigi Rizzo 				}
237237e3a6d3SLuigi Rizzo 
237337e3a6d3SLuigi Rizzo 				p -= sizeof(pkt->vh) - targ->g->virt_header;
237437e3a6d3SLuigi Rizzo 				len += sizeof(pkt->vh) - targ->g->virt_header;
237537e3a6d3SLuigi Rizzo 				pkt = (struct pkt *)p;
237680ad548dSVincenzo Maffione 				if (ntohs(pkt->eh.ether_type) == ETHERTYPE_IP)
237780ad548dSVincenzo Maffione 					af = AF_INET;
237880ad548dSVincenzo Maffione 				else
237980ad548dSVincenzo Maffione 					af = AF_INET6;
238037e3a6d3SLuigi Rizzo 
238180ad548dSVincenzo Maffione 				if ((char *)pkt + len < ((char *)PKT(pkt, body, af)) +
238280ad548dSVincenzo Maffione 						sizeof(seq)) {
238337e3a6d3SLuigi Rizzo 					RD(1, "%s: packet too small (len=%u)", __func__,
238437e3a6d3SLuigi Rizzo 							slot->len);
238537e3a6d3SLuigi Rizzo 				} else {
238680ad548dSVincenzo Maffione 					seq = (PKT(pkt, body, af)[0] << 24) |
238780ad548dSVincenzo Maffione 						(PKT(pkt, body, af)[1] << 16) |
238880ad548dSVincenzo Maffione 						(PKT(pkt, body, af)[2] << 8) |
238980ad548dSVincenzo Maffione 						PKT(pkt, body, af)[3];
239037e3a6d3SLuigi Rizzo 					if (first_slot) {
239137e3a6d3SLuigi Rizzo 						/* Grab the first one, whatever it
239237e3a6d3SLuigi Rizzo 						   is. */
239380ad548dSVincenzo Maffione 						seq_exp[j] = seq;
239437e3a6d3SLuigi Rizzo 						first_slot = 0;
239580ad548dSVincenzo Maffione 					} else if (seq != seq_exp[j]) {
239680ad548dSVincenzo Maffione 						uint32_t delta = seq - seq_exp[j];
239737e3a6d3SLuigi Rizzo 
239837e3a6d3SLuigi Rizzo 						if (delta < (0xFFFFFFFF >> 1)) {
239937e3a6d3SLuigi Rizzo 							RD(2, "Sequence GAP: exp %u found %u",
240080ad548dSVincenzo Maffione 									seq_exp[j], seq);
240137e3a6d3SLuigi Rizzo 						} else {
240237e3a6d3SLuigi Rizzo 							RD(2, "Sequence OUT OF ORDER: "
240380ad548dSVincenzo Maffione 									"exp %u found %u", seq_exp[j], seq);
240437e3a6d3SLuigi Rizzo 						}
240580ad548dSVincenzo Maffione 						seq_exp[j] = seq;
240637e3a6d3SLuigi Rizzo 					}
240780ad548dSVincenzo Maffione 					seq_exp[j]++;
240837e3a6d3SLuigi Rizzo 				}
240937e3a6d3SLuigi Rizzo 
241037e3a6d3SLuigi Rizzo 				cur.bytes += slot->len;
241137e3a6d3SLuigi Rizzo 				head = nm_ring_next(ring, head);
241237e3a6d3SLuigi Rizzo 				cur.pkts++;
241337e3a6d3SLuigi Rizzo 			}
241437e3a6d3SLuigi Rizzo 
241537e3a6d3SLuigi Rizzo 			ring->cur = ring->head = head;
241637e3a6d3SLuigi Rizzo 
241737e3a6d3SLuigi Rizzo 			cur.events++;
241837e3a6d3SLuigi Rizzo 			targ->ctr = cur;
241968b8534bSLuigi Rizzo 		}
242080ad548dSVincenzo Maffione 	}
2421f0ea3689SLuigi Rizzo 	clock_gettime(CLOCK_REALTIME_PRECISE, &targ->toc);
2422f0ea3689SLuigi Rizzo 
242380ad548dSVincenzo Maffione #ifndef BUSYWAIT
2424f0ea3689SLuigi Rizzo out:
242580ad548dSVincenzo Maffione #endif /* !BUSYWAIT */
242668b8534bSLuigi Rizzo 	targ->completed = 1;
242737e3a6d3SLuigi Rizzo 	targ->ctr = cur;
242868b8534bSLuigi Rizzo 
242968b8534bSLuigi Rizzo quit:
243080ad548dSVincenzo Maffione 	if (seq_exp != NULL)
243180ad548dSVincenzo Maffione 		free(seq_exp);
243268b8534bSLuigi Rizzo 	/* reset the ``used`` flag. */
243368b8534bSLuigi Rizzo 	targ->used = 0;
243468b8534bSLuigi Rizzo 
243568b8534bSLuigi Rizzo 	return (NULL);
243668b8534bSLuigi Rizzo }
243768b8534bSLuigi Rizzo 
243866a698c9SEd Maste 
243968b8534bSLuigi Rizzo static void
tx_output(struct glob_arg * g,struct my_ctrs * cur,double delta,const char * msg)244080ad548dSVincenzo Maffione tx_output(struct glob_arg *g, struct my_ctrs *cur, double delta, const char *msg)
244168b8534bSLuigi Rizzo {
244237e3a6d3SLuigi Rizzo 	double bw, raw_bw, pps, abs;
2443f8e4e36aSLuigi Rizzo 	char b1[40], b2[80], b3[80];
244437e3a6d3SLuigi Rizzo 	int size;
244568b8534bSLuigi Rizzo 
244637e3a6d3SLuigi Rizzo 	if (cur->pkts == 0) {
244737e3a6d3SLuigi Rizzo 		printf("%s nothing.\n", msg);
244837e3a6d3SLuigi Rizzo 		return;
244937e3a6d3SLuigi Rizzo 	}
245037e3a6d3SLuigi Rizzo 
245137e3a6d3SLuigi Rizzo 	size = (int)(cur->bytes / cur->pkts);
245237e3a6d3SLuigi Rizzo 
245337e3a6d3SLuigi Rizzo 	printf("%s %llu packets %llu bytes %llu events %d bytes each in %.2f seconds.\n",
245437e3a6d3SLuigi Rizzo 		msg,
245537e3a6d3SLuigi Rizzo 		(unsigned long long)cur->pkts,
245637e3a6d3SLuigi Rizzo 		(unsigned long long)cur->bytes,
245737e3a6d3SLuigi Rizzo 		(unsigned long long)cur->events, size, delta);
2458f8e4e36aSLuigi Rizzo 	if (delta == 0)
2459f8e4e36aSLuigi Rizzo 		delta = 1e-6;
2460f8e4e36aSLuigi Rizzo 	if (size < 60)		/* correct for min packet size */
2461f8e4e36aSLuigi Rizzo 		size = 60;
246237e3a6d3SLuigi Rizzo 	pps = cur->pkts / delta;
246337e3a6d3SLuigi Rizzo 	bw = (8.0 * cur->bytes) / delta;
246480ad548dSVincenzo Maffione 	raw_bw = (8.0 * cur->bytes + cur->pkts * g->framing) / delta;
246537e3a6d3SLuigi Rizzo 	abs = cur->pkts / (double)(cur->events);
246666a698c9SEd Maste 
246737e3a6d3SLuigi Rizzo 	printf("Speed: %spps Bandwidth: %sbps (raw %sbps). Average batch: %.2f pkts\n",
246880ad548dSVincenzo Maffione 		norm(b1, pps, normalize), norm(b2, bw, normalize), norm(b3, raw_bw, normalize), abs);
246968b8534bSLuigi Rizzo }
247068b8534bSLuigi Rizzo 
247168b8534bSLuigi Rizzo static void
usage(int errcode)247280ad548dSVincenzo Maffione usage(int errcode)
247368b8534bSLuigi Rizzo {
247480ad548dSVincenzo Maffione /* This usage is generated from the pkt-gen man page:
247580ad548dSVincenzo Maffione  *   $ man pkt-gen > x
247680ad548dSVincenzo Maffione  * and pasted here adding the string terminators and endlines with simple
247780ad548dSVincenzo Maffione  * regular expressions. */
247868b8534bSLuigi Rizzo 	const char *cmd = "pkt-gen";
247968b8534bSLuigi Rizzo 	fprintf(stderr,
248068b8534bSLuigi Rizzo 		"Usage:\n"
248168b8534bSLuigi Rizzo 		"%s arguments\n"
248280ad548dSVincenzo Maffione "     -h      Show program usage and exit.\n"
248380ad548dSVincenzo Maffione "\n"
248480ad548dSVincenzo Maffione "     -i interface\n"
248580ad548dSVincenzo Maffione "             Name of the network interface that pkt-gen operates on.  It can be a system network interface\n"
248680ad548dSVincenzo Maffione "             (e.g., em0), the name of a vale(4) port (e.g., valeSSS:PPP), the name of a netmap pipe or\n"
248780ad548dSVincenzo Maffione "             monitor, or any valid netmap port name accepted by the nm_open library function, as docu-\n"
248880ad548dSVincenzo Maffione "             mented in netmap(4) (NIOCREGIF section).\n"
248980ad548dSVincenzo Maffione "\n"
249080ad548dSVincenzo Maffione "     -f function\n"
249180ad548dSVincenzo Maffione "             The function to be executed by pkt-gen.  Specify tx for transmission, rx for reception, ping\n"
249280ad548dSVincenzo Maffione "             for client-side ping-pong operation, and pong for server-side ping-pong operation.\n"
249380ad548dSVincenzo Maffione "\n"
249480ad548dSVincenzo Maffione "     -n count\n"
2495ed188a7eSVincenzo Maffione "             Number of iterations of the pkt-gen function (with 0 meaning infinite).  In case of tx or rx,\n"
249680ad548dSVincenzo Maffione "             count is the number of packets to receive or transmit.  In case of ping or pong, count is the\n"
249780ad548dSVincenzo Maffione "             number of ping-pong transactions.\n"
249880ad548dSVincenzo Maffione "\n"
249980ad548dSVincenzo Maffione "     -l pkt_size\n"
250080ad548dSVincenzo Maffione "             Packet size in bytes excluding CRC.  If passed a second time, use random sizes larger or\n"
250180ad548dSVincenzo Maffione "             equal than the second one and lower than the first one.\n"
250280ad548dSVincenzo Maffione "\n"
250380ad548dSVincenzo Maffione "     -b burst_size\n"
250480ad548dSVincenzo Maffione "             Transmit or receive up to burst_size packets at a time.\n"
250580ad548dSVincenzo Maffione "\n"
250680ad548dSVincenzo Maffione "     -4      Use IPv4 addresses.\n"
250780ad548dSVincenzo Maffione "\n"
250880ad548dSVincenzo Maffione "     -6      Use IPv6 addresses.\n"
250980ad548dSVincenzo Maffione "\n"
251080ad548dSVincenzo Maffione "     -d dst_ip[:port[-dst_ip:port]]\n"
251180ad548dSVincenzo Maffione "             Destination IPv4/IPv6 address and port, single or range.\n"
251280ad548dSVincenzo Maffione "\n"
251380ad548dSVincenzo Maffione "     -s src_ip[:port[-src_ip:port]]\n"
251480ad548dSVincenzo Maffione "             Source IPv4/IPv6 address and port, single or range.\n"
251580ad548dSVincenzo Maffione "\n"
251680ad548dSVincenzo Maffione "     -D dst_mac\n"
251780ad548dSVincenzo Maffione "             Destination MAC address in colon notation (e.g., aa:bb:cc:dd:ee:00).\n"
251880ad548dSVincenzo Maffione "\n"
251980ad548dSVincenzo Maffione "     -S src_mac\n"
252080ad548dSVincenzo Maffione "             Source MAC address in colon notation.\n"
252180ad548dSVincenzo Maffione "\n"
252280ad548dSVincenzo Maffione "     -a cpu_id\n"
252380ad548dSVincenzo Maffione "             Pin the first thread of pkt-gen to a particular CPU using pthread_setaffinity_np(3).  If more\n"
252480ad548dSVincenzo Maffione "             threads are used, they are pinned to the subsequent CPUs, one per thread.\n"
252580ad548dSVincenzo Maffione "\n"
252680ad548dSVincenzo Maffione "     -c cpus\n"
252780ad548dSVincenzo Maffione "             Maximum number of CPUs to use (0 means to use all the available ones).\n"
252880ad548dSVincenzo Maffione "\n"
252980ad548dSVincenzo Maffione "     -p threads\n"
253080ad548dSVincenzo Maffione "             Number of threads to use.  By default, only a single thread is used to handle all the netmap\n"
253180ad548dSVincenzo Maffione "             rings.  If threads is larger than one, each thread handles a single TX ring (in tx mode), a\n"
2532ed188a7eSVincenzo Maffione "             single RX ring (in rx mode), or a TX/RX ring pair.  The number of threads must be less than or\n"
2533ed188a7eSVincenzo Maffione "             equal to the number of TX (or RX) rings available in the device specified by interface.\n"
253480ad548dSVincenzo Maffione "\n"
253580ad548dSVincenzo Maffione "     -T report_ms\n"
253680ad548dSVincenzo Maffione "             Number of milliseconds between reports.\n"
253780ad548dSVincenzo Maffione "\n"
253880ad548dSVincenzo Maffione "     -w wait_for_link_time\n"
2539ed188a7eSVincenzo Maffione "             Number of seconds to wait before starting the pkt-gen function, useful to make sure that the\n"
254080ad548dSVincenzo Maffione "             network link is up.  A network device driver may take some time to enter netmap mode, or to\n"
254180ad548dSVincenzo Maffione "             create a new transmit/receive ring pair when netmap(4) requests one.\n"
254280ad548dSVincenzo Maffione "\n"
254380ad548dSVincenzo Maffione "     -R rate\n"
254480ad548dSVincenzo Maffione "             Packet transmission rate.  Not setting the packet transmission rate tells pkt-gen to transmit\n"
2545ed188a7eSVincenzo Maffione "             packets as quickly as possible.  On servers from 2010 onward netmap(4) is able to com-\n"
254680ad548dSVincenzo Maffione "             pletely use all of the bandwidth of a 10 or 40Gbps link, so this option should be used unless\n"
254780ad548dSVincenzo Maffione "             your intention is to saturate the link.\n"
254880ad548dSVincenzo Maffione "\n"
254980ad548dSVincenzo Maffione "     -X      Dump payload of each packet transmitted or received.\n"
255080ad548dSVincenzo Maffione "\n"
255180ad548dSVincenzo Maffione "     -H len  Add empty virtio-net-header with size 'len'.  Valid sizes are 0, 10 and 12.  This option is\n"
255280ad548dSVincenzo Maffione "             only used with Virtual Machine technologies that use virtio as a network interface.\n"
255380ad548dSVincenzo Maffione "\n"
255480ad548dSVincenzo Maffione "     -P file\n"
255580ad548dSVincenzo Maffione "             Load the packet to be transmitted from a pcap file rather than constructing it within\n"
255680ad548dSVincenzo Maffione "             pkt-gen.\n"
255780ad548dSVincenzo Maffione "\n"
255880ad548dSVincenzo Maffione "     -z      Use random IPv4/IPv6 src address/port.\n"
255980ad548dSVincenzo Maffione "\n"
256080ad548dSVincenzo Maffione "     -Z      Use random IPv4/IPv6 dst address/port.\n"
256180ad548dSVincenzo Maffione "\n"
256280ad548dSVincenzo Maffione "     -N      Do not normalize units (i.e., use bps, pps instead of Mbps, Kpps, etc.).\n"
256380ad548dSVincenzo Maffione "\n"
256480ad548dSVincenzo Maffione "     -F num_frags\n"
256580ad548dSVincenzo Maffione "             Send multi-slot packets, each one with num_frags fragments.  A multi-slot packet is repre-\n"
256680ad548dSVincenzo Maffione "             sented by two or more consecutive netmap slots with the NS_MOREFRAG flag set (except for the\n"
256780ad548dSVincenzo Maffione "             last slot).  This is useful to transmit or receive packets larger than the netmap buffer\n"
256880ad548dSVincenzo Maffione "             size.\n"
256980ad548dSVincenzo Maffione "\n"
257080ad548dSVincenzo Maffione "     -M frag_size\n"
257180ad548dSVincenzo Maffione "             In multi-slot mode, frag_size specifies the size of each fragment, if smaller than the packet\n"
257280ad548dSVincenzo Maffione "             length divided by num_frags.\n"
257380ad548dSVincenzo Maffione "\n"
257480ad548dSVincenzo Maffione "     -I      Use indirect buffers.  It is only valid for transmitting on VALE ports, and it is implemented\n"
257580ad548dSVincenzo Maffione "             by setting the NS_INDIRECT flag in the netmap slots.\n"
257680ad548dSVincenzo Maffione "\n"
257780ad548dSVincenzo Maffione "     -W      Exit immediately if all the RX rings are empty the first time they are examined.\n"
257880ad548dSVincenzo Maffione "\n"
257980ad548dSVincenzo Maffione "     -v      Increase the verbosity level.\n"
258080ad548dSVincenzo Maffione "\n"
258180ad548dSVincenzo Maffione "     -r      In tx mode, do not initialize packets, but send whatever the content of the uninitialized\n"
258280ad548dSVincenzo Maffione "             netmap buffers is (rubbish mode).\n"
258380ad548dSVincenzo Maffione "\n"
258480ad548dSVincenzo Maffione "     -A      Compute mean and standard deviation (over a sliding window) for the transmit or receive rate.\n"
258580ad548dSVincenzo Maffione "\n"
258680ad548dSVincenzo Maffione "     -B      Take Ethernet framing and CRC into account when computing the average bps.  This adds 4 bytes\n"
258780ad548dSVincenzo Maffione "             of CRC and 20 bytes of framing to each packet.\n"
258880ad548dSVincenzo Maffione "\n"
258980ad548dSVincenzo Maffione "     -C tx_slots[,rx_slots[,tx_rings[,rx_rings]]]\n"
259080ad548dSVincenzo Maffione "             Configuration in terms of number of rings and slots to be used when opening the netmap port.\n"
2591ed188a7eSVincenzo Maffione "             Such configuration has an effect on software ports created on the fly, such as VALE ports and\n"
259280ad548dSVincenzo Maffione "             netmap pipes.  The configuration may consist of 1 to 4 numbers separated by commas: tx_slots,\n"
259380ad548dSVincenzo Maffione "             rx_slots, tx_rings, rx_rings.  Missing numbers or zeroes stand for default values.  As an\n"
259480ad548dSVincenzo Maffione "             additional convenience, if exactly one number is specified, then this is assigned to both\n"
259580ad548dSVincenzo Maffione "             tx_slots and rx_slots.  If there is no fourth number, then the third one is assigned to both\n"
259680ad548dSVincenzo Maffione "             tx_rings and rx_rings.\n"
259780ad548dSVincenzo Maffione "\n"
259880ad548dSVincenzo Maffione "     -o options		data generation options (parsed using atoi)\n"
259980ad548dSVincenzo Maffione "				OPT_PREFETCH	1\n"
260080ad548dSVincenzo Maffione "				OPT_ACCESS	2\n"
260180ad548dSVincenzo Maffione "				OPT_COPY	4\n"
260280ad548dSVincenzo Maffione "				OPT_MEMCPY	8\n"
260380ad548dSVincenzo Maffione "				OPT_TS		16 (add a timestamp)\n"
260480ad548dSVincenzo Maffione "				OPT_INDIRECT	32 (use indirect buffers)\n"
260580ad548dSVincenzo Maffione "				OPT_DUMP	64 (dump rx/tx traffic)\n"
260680ad548dSVincenzo Maffione "				OPT_RUBBISH	256\n"
2607ed188a7eSVincenzo Maffione "					(send whatever the buffers contain)\n"
260880ad548dSVincenzo Maffione "				OPT_RANDOM_SRC  512\n"
260980ad548dSVincenzo Maffione "				OPT_RANDOM_DST  1024\n"
261080ad548dSVincenzo Maffione "				OPT_PPS_STATS   2048\n"
26118c3b8c83SVincenzo Maffione "				OPT_UPDATE_CSUM 4096\n"
261268b8534bSLuigi Rizzo 		     "",
261368b8534bSLuigi Rizzo 		cmd);
261480ad548dSVincenzo Maffione 	exit(errcode);
261568b8534bSLuigi Rizzo }
261668b8534bSLuigi Rizzo 
26174bfe1a4fSVincenzo Maffione static int
start_threads(struct glob_arg * g)261880ad548dSVincenzo Maffione start_threads(struct glob_arg *g) {
2619f8e4e36aSLuigi Rizzo 	int i;
2620f8e4e36aSLuigi Rizzo 
2621f8e4e36aSLuigi Rizzo 	targs = calloc(g->nthreads, sizeof(*targs));
262280ad548dSVincenzo Maffione 	struct targ *t;
2623f8e4e36aSLuigi Rizzo 	/*
2624f8e4e36aSLuigi Rizzo 	 * Now create the desired number of threads, each one
2625f8e4e36aSLuigi Rizzo 	 * using a single descriptor.
2626f8e4e36aSLuigi Rizzo 	 */
2627f8e4e36aSLuigi Rizzo 	for (i = 0; i < g->nthreads; i++) {
262827bf5dd3SVincenzo Maffione 		uint64_t seed = (uint64_t)time(0) | ((uint64_t)time(0) << 32);
262980ad548dSVincenzo Maffione 		t = &targs[i];
2630f0ea3689SLuigi Rizzo 
2631f0ea3689SLuigi Rizzo 		bzero(t, sizeof(*t));
2632f0ea3689SLuigi Rizzo 		t->fd = -1; /* default, with pcap */
2633f0ea3689SLuigi Rizzo 		t->g = g;
263480ad548dSVincenzo Maffione 		memcpy(t->seed, &seed, sizeof(t->seed));
2635f8e4e36aSLuigi Rizzo 
2636f8e4e36aSLuigi Rizzo 		if (g->dev_type == DEV_NETMAP) {
26374bfe1a4fSVincenzo Maffione 			int m = -1;
26384bfe1a4fSVincenzo Maffione 
26394bfe1a4fSVincenzo Maffione 			/*
26404bfe1a4fSVincenzo Maffione 			 * if the user wants both HW and SW rings, we need to
26414bfe1a4fSVincenzo Maffione 			 * know when to switch from NR_REG_ONE_NIC to NR_REG_ONE_SW
26424bfe1a4fSVincenzo Maffione 			 */
26434bfe1a4fSVincenzo Maffione 			if (g->orig_mode == NR_REG_NIC_SW) {
26444bfe1a4fSVincenzo Maffione 				m = (g->td_type == TD_TYPE_RECEIVER ?
26454bfe1a4fSVincenzo Maffione 						g->nmd->reg.nr_rx_rings :
26464bfe1a4fSVincenzo Maffione 						g->nmd->reg.nr_tx_rings);
26474bfe1a4fSVincenzo Maffione 			}
2648f8e4e36aSLuigi Rizzo 
264937e3a6d3SLuigi Rizzo 			if (i > 0) {
26504bfe1a4fSVincenzo Maffione 				int j;
265137e3a6d3SLuigi Rizzo 				/* the first thread uses the fd opened by the main
265237e3a6d3SLuigi Rizzo 				 * thread, the other threads re-open /dev/netmap
265337e3a6d3SLuigi Rizzo 				 */
26544bfe1a4fSVincenzo Maffione 				t->nmd = nmport_clone(g->nmd);
26554bfe1a4fSVincenzo Maffione 				if (t->nmd == NULL)
26564bfe1a4fSVincenzo Maffione 					return -1;
26574bfe1a4fSVincenzo Maffione 
26584bfe1a4fSVincenzo Maffione 				j = i;
26594bfe1a4fSVincenzo Maffione 				if (m > 0 && j >= m) {
26604bfe1a4fSVincenzo Maffione 					/* switch to the software rings */
26614bfe1a4fSVincenzo Maffione 					t->nmd->reg.nr_mode = NR_REG_ONE_SW;
26624bfe1a4fSVincenzo Maffione 					j -= m;
266317885a7bSLuigi Rizzo 				}
26644bfe1a4fSVincenzo Maffione 				t->nmd->reg.nr_ringid = j & NETMAP_RING_MASK;
2665f0ea3689SLuigi Rizzo 				/* Only touch one of the rings (rx is already ok) */
266637e3a6d3SLuigi Rizzo 				if (g->td_type == TD_TYPE_RECEIVER)
26674bfe1a4fSVincenzo Maffione 					t->nmd->reg.nr_flags |= NETMAP_NO_TX_POLL;
2668f8e4e36aSLuigi Rizzo 
2669f0ea3689SLuigi Rizzo 				/* register interface. Override ifname and ringid etc. */
26704bfe1a4fSVincenzo Maffione 				if (nmport_open_desc(t->nmd) < 0) {
26714bfe1a4fSVincenzo Maffione 					nmport_undo_prepare(t->nmd);
26724bfe1a4fSVincenzo Maffione 					t->nmd = NULL;
26734bfe1a4fSVincenzo Maffione 					return -1;
2674f8e4e36aSLuigi Rizzo 				}
267537e3a6d3SLuigi Rizzo 			} else {
267637e3a6d3SLuigi Rizzo 				t->nmd = g->nmd;
267737e3a6d3SLuigi Rizzo 			}
2678f0ea3689SLuigi Rizzo 			t->fd = t->nmd->fd;
267980ad548dSVincenzo Maffione 			t->frags = g->frags;
2680f8e4e36aSLuigi Rizzo 		} else {
2681f8e4e36aSLuigi Rizzo 			targs[i].fd = g->main_fd;
2682f8e4e36aSLuigi Rizzo 		}
2683f0ea3689SLuigi Rizzo 		t->used = 1;
2684f0ea3689SLuigi Rizzo 		t->me = i;
2685f8e4e36aSLuigi Rizzo 		if (g->affinity >= 0) {
268680ad548dSVincenzo Maffione 			t->affinity = (g->affinity + i) % g->cpus;
2687f0ea3689SLuigi Rizzo 		} else {
2688f0ea3689SLuigi Rizzo 			t->affinity = -1;
2689f0ea3689SLuigi Rizzo 		}
2690f8e4e36aSLuigi Rizzo 		/* default, init packets */
2691f0ea3689SLuigi Rizzo 		initialize_packet(t);
269280ad548dSVincenzo Maffione 	}
269380ad548dSVincenzo Maffione 	/* Wait for PHY reset. */
269480ad548dSVincenzo Maffione 	D("Wait %d secs for phy reset", g->wait_link);
269580ad548dSVincenzo Maffione 	sleep(g->wait_link);
269680ad548dSVincenzo Maffione 	D("Ready...");
2697f8e4e36aSLuigi Rizzo 
269880ad548dSVincenzo Maffione 	for (i = 0; i < g->nthreads; i++) {
269980ad548dSVincenzo Maffione 		t = &targs[i];
2700f0ea3689SLuigi Rizzo 		if (pthread_create(&t->thread, NULL, g->td_body, t) == -1) {
270117885a7bSLuigi Rizzo 			D("Unable to create thread %d: %s", i, strerror(errno));
2702f0ea3689SLuigi Rizzo 			t->used = 0;
2703f8e4e36aSLuigi Rizzo 		}
2704f8e4e36aSLuigi Rizzo 	}
27054bfe1a4fSVincenzo Maffione 	return 0;
2706f8e4e36aSLuigi Rizzo }
2707f8e4e36aSLuigi Rizzo 
2708f8e4e36aSLuigi Rizzo static void
main_thread(struct glob_arg * g)2709f8e4e36aSLuigi Rizzo main_thread(struct glob_arg *g)
2710f8e4e36aSLuigi Rizzo {
2711f8e4e36aSLuigi Rizzo 	int i;
2712f8e4e36aSLuigi Rizzo 
271337e3a6d3SLuigi Rizzo 	struct my_ctrs prev, cur;
2714f8e4e36aSLuigi Rizzo 	double delta_t;
2715f8e4e36aSLuigi Rizzo 	struct timeval tic, toc;
2716f8e4e36aSLuigi Rizzo 
271737e3a6d3SLuigi Rizzo 	prev.pkts = prev.bytes = prev.events = 0;
271837e3a6d3SLuigi Rizzo 	gettimeofday(&prev.t, NULL);
2719f8e4e36aSLuigi Rizzo 	for (;;) {
272080ad548dSVincenzo Maffione 		char b1[40], b2[40], b3[40], b4[100];
272137e3a6d3SLuigi Rizzo 		uint64_t pps, usec;
272237e3a6d3SLuigi Rizzo 		struct my_ctrs x;
272337e3a6d3SLuigi Rizzo 		double abs;
2724f8e4e36aSLuigi Rizzo 		int done = 0;
2725f8e4e36aSLuigi Rizzo 
272637e3a6d3SLuigi Rizzo 		usec = wait_for_next_report(&prev.t, &cur.t,
272737e3a6d3SLuigi Rizzo 				g->report_interval);
272837e3a6d3SLuigi Rizzo 
272937e3a6d3SLuigi Rizzo 		cur.pkts = cur.bytes = cur.events = 0;
273037e3a6d3SLuigi Rizzo 		cur.min_space = 0;
273137e3a6d3SLuigi Rizzo 		if (usec < 10000) /* too short to be meaningful */
273237e3a6d3SLuigi Rizzo 			continue;
273337e3a6d3SLuigi Rizzo 		/* accumulate counts for all threads */
2734f8e4e36aSLuigi Rizzo 		for (i = 0; i < g->nthreads; i++) {
273537e3a6d3SLuigi Rizzo 			cur.pkts += targs[i].ctr.pkts;
273637e3a6d3SLuigi Rizzo 			cur.bytes += targs[i].ctr.bytes;
273737e3a6d3SLuigi Rizzo 			cur.events += targs[i].ctr.events;
273837e3a6d3SLuigi Rizzo 			cur.min_space += targs[i].ctr.min_space;
273937e3a6d3SLuigi Rizzo 			targs[i].ctr.min_space = 99999;
2740f8e4e36aSLuigi Rizzo 			if (targs[i].used == 0)
2741f8e4e36aSLuigi Rizzo 				done++;
2742f8e4e36aSLuigi Rizzo 		}
274337e3a6d3SLuigi Rizzo 		x.pkts = cur.pkts - prev.pkts;
274437e3a6d3SLuigi Rizzo 		x.bytes = cur.bytes - prev.bytes;
274537e3a6d3SLuigi Rizzo 		x.events = cur.events - prev.events;
274637e3a6d3SLuigi Rizzo 		pps = (x.pkts*1000000 + usec/2) / usec;
274737e3a6d3SLuigi Rizzo 		abs = (x.events > 0) ? (x.pkts / (double) x.events) : 0;
274837e3a6d3SLuigi Rizzo 
274937e3a6d3SLuigi Rizzo 		if (!(g->options & OPT_PPS_STATS)) {
275037e3a6d3SLuigi Rizzo 			strcpy(b4, "");
275137e3a6d3SLuigi Rizzo 		} else {
275237e3a6d3SLuigi Rizzo 			/* Compute some pps stats using a sliding window. */
275337e3a6d3SLuigi Rizzo 			double ppsavg = 0.0, ppsdev = 0.0;
275437e3a6d3SLuigi Rizzo 			int nsamples = 0;
275537e3a6d3SLuigi Rizzo 
275637e3a6d3SLuigi Rizzo 			g->win[g->win_idx] = pps;
275737e3a6d3SLuigi Rizzo 			g->win_idx = (g->win_idx + 1) % STATS_WIN;
275837e3a6d3SLuigi Rizzo 
275937e3a6d3SLuigi Rizzo 			for (i = 0; i < STATS_WIN; i++) {
276037e3a6d3SLuigi Rizzo 				ppsavg += g->win[i];
276137e3a6d3SLuigi Rizzo 				if (g->win[i]) {
276237e3a6d3SLuigi Rizzo 					nsamples ++;
276337e3a6d3SLuigi Rizzo 				}
276437e3a6d3SLuigi Rizzo 			}
276537e3a6d3SLuigi Rizzo 			ppsavg /= nsamples;
276637e3a6d3SLuigi Rizzo 
276737e3a6d3SLuigi Rizzo 			for (i = 0; i < STATS_WIN; i++) {
276837e3a6d3SLuigi Rizzo 				if (g->win[i] == 0) {
2769f8e4e36aSLuigi Rizzo 					continue;
277037e3a6d3SLuigi Rizzo 				}
277137e3a6d3SLuigi Rizzo 				ppsdev += (g->win[i] - ppsavg) * (g->win[i] - ppsavg);
277237e3a6d3SLuigi Rizzo 			}
277337e3a6d3SLuigi Rizzo 			ppsdev /= nsamples;
277437e3a6d3SLuigi Rizzo 			ppsdev = sqrt(ppsdev);
277537e3a6d3SLuigi Rizzo 
277637e3a6d3SLuigi Rizzo 			snprintf(b4, sizeof(b4), "[avg/std %s/%s pps]",
277780ad548dSVincenzo Maffione 				 norm(b1, ppsavg, normalize), norm(b2, ppsdev, normalize));
277837e3a6d3SLuigi Rizzo 		}
277937e3a6d3SLuigi Rizzo 
278037e3a6d3SLuigi Rizzo 		D("%spps %s(%spkts %sbps in %llu usec) %.2f avg_batch %d min_space",
278180ad548dSVincenzo Maffione 			norm(b1, pps, normalize), b4,
278280ad548dSVincenzo Maffione 			norm(b2, (double)x.pkts, normalize),
2783760fa2abSVincenzo Maffione 			norm(b3, 1000000*((double)x.bytes*8+(double)x.pkts*g->framing)/usec, normalize),
278437e3a6d3SLuigi Rizzo 			(unsigned long long)usec,
278537e3a6d3SLuigi Rizzo 			abs, (int)cur.min_space);
278637e3a6d3SLuigi Rizzo 		prev = cur;
278737e3a6d3SLuigi Rizzo 
2788f8e4e36aSLuigi Rizzo 		if (done == g->nthreads)
2789f8e4e36aSLuigi Rizzo 			break;
2790f8e4e36aSLuigi Rizzo 	}
2791f8e4e36aSLuigi Rizzo 
2792f8e4e36aSLuigi Rizzo 	timerclear(&tic);
2793f8e4e36aSLuigi Rizzo 	timerclear(&toc);
279437e3a6d3SLuigi Rizzo 	cur.pkts = cur.bytes = cur.events = 0;
279537e3a6d3SLuigi Rizzo 	/* final round */
2796f8e4e36aSLuigi Rizzo 	for (i = 0; i < g->nthreads; i++) {
27971cb4c501SLuigi Rizzo 		struct timespec t_tic, t_toc;
2798f8e4e36aSLuigi Rizzo 		/*
2799f8e4e36aSLuigi Rizzo 		 * Join active threads, unregister interfaces and close
2800f8e4e36aSLuigi Rizzo 		 * file descriptors.
2801f8e4e36aSLuigi Rizzo 		 */
28021cb4c501SLuigi Rizzo 		if (targs[i].used)
280337e3a6d3SLuigi Rizzo 			pthread_join(targs[i].thread, NULL); /* blocking */
280437e3a6d3SLuigi Rizzo 		if (g->dev_type == DEV_NETMAP) {
28054bfe1a4fSVincenzo Maffione 			nmport_close(targs[i].nmd);
280637e3a6d3SLuigi Rizzo 			targs[i].nmd = NULL;
280737e3a6d3SLuigi Rizzo 		} else {
2808f8e4e36aSLuigi Rizzo 			close(targs[i].fd);
280937e3a6d3SLuigi Rizzo 		}
2810f8e4e36aSLuigi Rizzo 
2811f8e4e36aSLuigi Rizzo 		if (targs[i].completed == 0)
2812f8e4e36aSLuigi Rizzo 			D("ouch, thread %d exited with error", i);
2813f8e4e36aSLuigi Rizzo 
2814f8e4e36aSLuigi Rizzo 		/*
2815f8e4e36aSLuigi Rizzo 		 * Collect threads output and extract information about
2816f8e4e36aSLuigi Rizzo 		 * how long it took to send all the packets.
2817f8e4e36aSLuigi Rizzo 		 */
281837e3a6d3SLuigi Rizzo 		cur.pkts += targs[i].ctr.pkts;
281937e3a6d3SLuigi Rizzo 		cur.bytes += targs[i].ctr.bytes;
282037e3a6d3SLuigi Rizzo 		cur.events += targs[i].ctr.events;
282137e3a6d3SLuigi Rizzo 		/* collect the largest start (tic) and end (toc) times,
282237e3a6d3SLuigi Rizzo 		 * XXX maybe we should do the earliest tic, or do a weighted
282337e3a6d3SLuigi Rizzo 		 * average ?
282437e3a6d3SLuigi Rizzo 		 */
28251cb4c501SLuigi Rizzo 		t_tic = timeval2spec(&tic);
28261cb4c501SLuigi Rizzo 		t_toc = timeval2spec(&toc);
28271cb4c501SLuigi Rizzo 		if (!timerisset(&tic) || timespec_ge(&targs[i].tic, &t_tic))
28281cb4c501SLuigi Rizzo 			tic = timespec2val(&targs[i].tic);
28291cb4c501SLuigi Rizzo 		if (!timerisset(&toc) || timespec_ge(&targs[i].toc, &t_toc))
28301cb4c501SLuigi Rizzo 			toc = timespec2val(&targs[i].toc);
2831f8e4e36aSLuigi Rizzo 	}
2832f8e4e36aSLuigi Rizzo 
2833f8e4e36aSLuigi Rizzo 	/* print output. */
2834f8e4e36aSLuigi Rizzo 	timersub(&toc, &tic, &toc);
2835f8e4e36aSLuigi Rizzo 	delta_t = toc.tv_sec + 1e-6* toc.tv_usec;
283637e3a6d3SLuigi Rizzo 	if (g->td_type == TD_TYPE_SENDER)
283780ad548dSVincenzo Maffione 		tx_output(g, &cur, delta_t, "Sent");
283880ad548dSVincenzo Maffione 	else if (g->td_type == TD_TYPE_RECEIVER)
283980ad548dSVincenzo Maffione 		tx_output(g, &cur, delta_t, "Received");
2840f8e4e36aSLuigi Rizzo }
2841f8e4e36aSLuigi Rizzo 
284237e3a6d3SLuigi Rizzo struct td_desc {
284337e3a6d3SLuigi Rizzo 	int ty;
28447eb32dc8SVincenzo Maffione 	const char *key;
2845f8e4e36aSLuigi Rizzo 	void *f;
284680ad548dSVincenzo Maffione 	int default_burst;
2847f8e4e36aSLuigi Rizzo };
2848f8e4e36aSLuigi Rizzo 
284937e3a6d3SLuigi Rizzo static struct td_desc func[] = {
285080ad548dSVincenzo Maffione 	{ TD_TYPE_RECEIVER,	"rx",		receiver_body,	512},	/* default */
285180ad548dSVincenzo Maffione 	{ TD_TYPE_SENDER,	"tx",		sender_body,	512 },
285280ad548dSVincenzo Maffione 	{ TD_TYPE_OTHER,	"ping",		ping_body,	1 },
285380ad548dSVincenzo Maffione 	{ TD_TYPE_OTHER,	"pong",		pong_body,	1 },
285480ad548dSVincenzo Maffione 	{ TD_TYPE_SENDER,	"txseq",	txseq_body,	512 },
285580ad548dSVincenzo Maffione 	{ TD_TYPE_RECEIVER,	"rxseq",	rxseq_body,	512 },
285680ad548dSVincenzo Maffione 	{ 0,			NULL,		NULL, 		0 }
2857f8e4e36aSLuigi Rizzo };
2858f8e4e36aSLuigi Rizzo 
2859f8e4e36aSLuigi Rizzo static int
tap_alloc(char * dev)2860f8e4e36aSLuigi Rizzo tap_alloc(char *dev)
2861f8e4e36aSLuigi Rizzo {
2862f8e4e36aSLuigi Rizzo 	struct ifreq ifr;
2863f8e4e36aSLuigi Rizzo 	int fd, err;
28647eb32dc8SVincenzo Maffione 	const char *clonedev = TAP_CLONEDEV;
2865f8e4e36aSLuigi Rizzo 
2866f8e4e36aSLuigi Rizzo 	(void)err;
2867f8e4e36aSLuigi Rizzo 	(void)dev;
2868f8e4e36aSLuigi Rizzo 	/* Arguments taken by the function:
2869f8e4e36aSLuigi Rizzo 	 *
2870f8e4e36aSLuigi Rizzo 	 * char *dev: the name of an interface (or '\0'). MUST have enough
2871f8e4e36aSLuigi Rizzo 	 *   space to hold the interface name if '\0' is passed
2872f8e4e36aSLuigi Rizzo 	 * int flags: interface flags (eg, IFF_TUN etc.)
2873f8e4e36aSLuigi Rizzo 	 */
2874f8e4e36aSLuigi Rizzo 
2875f8e4e36aSLuigi Rizzo #ifdef __FreeBSD__
2876f8e4e36aSLuigi Rizzo 	if (dev[3]) { /* tapSomething */
2877f8e4e36aSLuigi Rizzo 		static char buf[128];
2878f8e4e36aSLuigi Rizzo 		snprintf(buf, sizeof(buf), "/dev/%s", dev);
2879f8e4e36aSLuigi Rizzo 		clonedev = buf;
2880f8e4e36aSLuigi Rizzo 	}
2881f8e4e36aSLuigi Rizzo #endif
2882f8e4e36aSLuigi Rizzo 	/* open the device */
2883f8e4e36aSLuigi Rizzo 	if( (fd = open(clonedev, O_RDWR)) < 0 ) {
2884f8e4e36aSLuigi Rizzo 		return fd;
2885f8e4e36aSLuigi Rizzo 	}
2886f8e4e36aSLuigi Rizzo 	D("%s open successful", clonedev);
2887f8e4e36aSLuigi Rizzo 
2888f8e4e36aSLuigi Rizzo 	/* preparation of the struct ifr, of type "struct ifreq" */
2889f8e4e36aSLuigi Rizzo 	memset(&ifr, 0, sizeof(ifr));
2890f8e4e36aSLuigi Rizzo 
2891f8e4e36aSLuigi Rizzo #ifdef linux
2892f8e4e36aSLuigi Rizzo 	ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
2893f8e4e36aSLuigi Rizzo 
2894f8e4e36aSLuigi Rizzo 	if (*dev) {
2895f8e4e36aSLuigi Rizzo 		/* if a device name was specified, put it in the structure; otherwise,
2896f8e4e36aSLuigi Rizzo 		* the kernel will try to allocate the "next" device of the
2897f8e4e36aSLuigi Rizzo 		* specified type */
289880ad548dSVincenzo Maffione 		size_t len = strlen(dev);
289980ad548dSVincenzo Maffione 		if (len > IFNAMSIZ) {
290080ad548dSVincenzo Maffione 			D("%s too long", dev);
290180ad548dSVincenzo Maffione 			return -1;
290280ad548dSVincenzo Maffione 		}
290380ad548dSVincenzo Maffione 		memcpy(ifr.ifr_name, dev, len);
2904f8e4e36aSLuigi Rizzo 	}
2905f8e4e36aSLuigi Rizzo 
2906f8e4e36aSLuigi Rizzo 	/* try to create the device */
2907f8e4e36aSLuigi Rizzo 	if( (err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0 ) {
290808cb3ac7SGordon Bergling 		D("failed to do a TUNSETIFF: %s", strerror(errno));
2909f8e4e36aSLuigi Rizzo 		close(fd);
2910f8e4e36aSLuigi Rizzo 		return err;
2911f8e4e36aSLuigi Rizzo 	}
2912f8e4e36aSLuigi Rizzo 
2913f8e4e36aSLuigi Rizzo 	/* if the operation was successful, write back the name of the
2914f8e4e36aSLuigi Rizzo 	* interface to the variable "dev", so the caller can know
2915f8e4e36aSLuigi Rizzo 	* it. Note that the caller MUST reserve space in *dev (see calling
2916f8e4e36aSLuigi Rizzo 	* code below) */
2917f8e4e36aSLuigi Rizzo 	strcpy(dev, ifr.ifr_name);
2918f8e4e36aSLuigi Rizzo 	D("new name is %s", dev);
2919f8e4e36aSLuigi Rizzo #endif /* linux */
2920f8e4e36aSLuigi Rizzo 
2921f8e4e36aSLuigi Rizzo 	/* this is the special file descriptor that the caller will use to talk
2922f8e4e36aSLuigi Rizzo 	 * with the virtual interface */
2923f8e4e36aSLuigi Rizzo 	return fd;
2924f8e4e36aSLuigi Rizzo }
292568b8534bSLuigi Rizzo 
292668b8534bSLuigi Rizzo int
main(int arc,char ** argv)292768b8534bSLuigi Rizzo main(int arc, char **argv)
292868b8534bSLuigi Rizzo {
2929f8e4e36aSLuigi Rizzo 	int i;
293037e3a6d3SLuigi Rizzo 	struct sigaction sa;
293137e3a6d3SLuigi Rizzo 	sigset_t ss;
293268b8534bSLuigi Rizzo 
293368b8534bSLuigi Rizzo 	struct glob_arg g;
293468b8534bSLuigi Rizzo 
293568b8534bSLuigi Rizzo 	int ch;
293668b8534bSLuigi Rizzo 	int devqueues = 1;	/* how many device queues */
293780ad548dSVincenzo Maffione 	int wait_link_arg = 0;
293880ad548dSVincenzo Maffione 
293980ad548dSVincenzo Maffione 	int pkt_size_done = 0;
294080ad548dSVincenzo Maffione 
294180ad548dSVincenzo Maffione 	struct td_desc *fn = func;
294268b8534bSLuigi Rizzo 
294368b8534bSLuigi Rizzo 	bzero(&g, sizeof(g));
294468b8534bSLuigi Rizzo 
2945f8e4e36aSLuigi Rizzo 	g.main_fd = -1;
294680ad548dSVincenzo Maffione 	g.td_body = fn->f;
294780ad548dSVincenzo Maffione 	g.td_type = fn->ty;
2948f8e4e36aSLuigi Rizzo 	g.report_interval = 1000;	/* report interval */
2949f8e4e36aSLuigi Rizzo 	g.affinity = -1;
2950f8e4e36aSLuigi Rizzo 	/* ip addresses can also be a range x.x.x.x-x.x.x.y */
295180ad548dSVincenzo Maffione 	g.af = AF_INET;		/* default */
2952f8e4e36aSLuigi Rizzo 	g.src_ip.name = "10.0.0.1";
2953f8e4e36aSLuigi Rizzo 	g.dst_ip.name = "10.1.0.1";
2954f8e4e36aSLuigi Rizzo 	g.dst_mac.name = "ff:ff:ff:ff:ff:ff";
2955f8e4e36aSLuigi Rizzo 	g.src_mac.name = NULL;
295668b8534bSLuigi Rizzo 	g.pkt_size = 60;
295780ad548dSVincenzo Maffione 	g.pkt_min_size = 0;
295868b8534bSLuigi Rizzo 	g.nthreads = 1;
295980ad548dSVincenzo Maffione 	g.cpus = 1;		/* default */
2960b303f675SLuigi Rizzo 	g.forever = 1;
29611cb4c501SLuigi Rizzo 	g.tx_rate = 0;
2962ce3ee1e7SLuigi Rizzo 	g.frags = 1;
29639e53f3bdSVincenzo Maffione 	g.frag_size = (u_int)-1;	/* use the netmap buffer size by default */
2964ce3ee1e7SLuigi Rizzo 	g.nmr_config = "";
296517885a7bSLuigi Rizzo 	g.virt_header = 0;
296680ad548dSVincenzo Maffione 	g.wait_link = 2;	/* wait 2 seconds for physical ports */
296768b8534bSLuigi Rizzo 
296880ad548dSVincenzo Maffione 	while ((ch = getopt(arc, argv, "46a:f:F:Nn:i:Il:d:s:D:S:b:c:o:p:"
296980ad548dSVincenzo Maffione 	    "T:w:WvR:XC:H:rP:zZAhBM:")) != -1) {
2970f8e4e36aSLuigi Rizzo 
297168b8534bSLuigi Rizzo 		switch(ch) {
297268b8534bSLuigi Rizzo 		default:
297368b8534bSLuigi Rizzo 			D("bad option %c %s", ch, optarg);
297480ad548dSVincenzo Maffione 			usage(-1);
297580ad548dSVincenzo Maffione 			break;
297680ad548dSVincenzo Maffione 
297780ad548dSVincenzo Maffione 		case 'h':
297880ad548dSVincenzo Maffione 			usage(0);
297980ad548dSVincenzo Maffione 			break;
298080ad548dSVincenzo Maffione 
298180ad548dSVincenzo Maffione 		case '4':
298280ad548dSVincenzo Maffione 			g.af = AF_INET;
298380ad548dSVincenzo Maffione 			break;
298480ad548dSVincenzo Maffione 
298580ad548dSVincenzo Maffione 		case '6':
298680ad548dSVincenzo Maffione 			g.af = AF_INET6;
298780ad548dSVincenzo Maffione 			break;
298880ad548dSVincenzo Maffione 
298980ad548dSVincenzo Maffione 		case 'N':
299080ad548dSVincenzo Maffione 			normalize = 0;
299168b8534bSLuigi Rizzo 			break;
2992f8e4e36aSLuigi Rizzo 
2993f8e4e36aSLuigi Rizzo 		case 'n':
299437e3a6d3SLuigi Rizzo 			g.npackets = strtoull(optarg, NULL, 10);
2995f8e4e36aSLuigi Rizzo 			break;
2996f8e4e36aSLuigi Rizzo 
2997ce3ee1e7SLuigi Rizzo 		case 'F':
2998ce3ee1e7SLuigi Rizzo 			i = atoi(optarg);
2999ce3ee1e7SLuigi Rizzo 			if (i < 1 || i > 63) {
3000ce3ee1e7SLuigi Rizzo 				D("invalid frags %d [1..63], ignore", i);
3001ce3ee1e7SLuigi Rizzo 				break;
3002ce3ee1e7SLuigi Rizzo 			}
3003ce3ee1e7SLuigi Rizzo 			g.frags = i;
3004ce3ee1e7SLuigi Rizzo 			break;
3005ce3ee1e7SLuigi Rizzo 
300680ad548dSVincenzo Maffione 		case 'M':
30079e53f3bdSVincenzo Maffione 			g.frag_size = atoi(optarg);
300880ad548dSVincenzo Maffione 			break;
300980ad548dSVincenzo Maffione 
3010f8e4e36aSLuigi Rizzo 		case 'f':
3011f8e4e36aSLuigi Rizzo 			for (fn = func; fn->key; fn++) {
3012f8e4e36aSLuigi Rizzo 				if (!strcmp(fn->key, optarg))
3013f8e4e36aSLuigi Rizzo 					break;
3014f8e4e36aSLuigi Rizzo 			}
301537e3a6d3SLuigi Rizzo 			if (fn->key) {
3016f8e4e36aSLuigi Rizzo 				g.td_body = fn->f;
301737e3a6d3SLuigi Rizzo 				g.td_type = fn->ty;
301837e3a6d3SLuigi Rizzo 			} else {
3019f8e4e36aSLuigi Rizzo 				D("unrecognised function %s", optarg);
302037e3a6d3SLuigi Rizzo 			}
3021f8e4e36aSLuigi Rizzo 			break;
3022f8e4e36aSLuigi Rizzo 
3023f8e4e36aSLuigi Rizzo 		case 'o':	/* data generation options */
302480ad548dSVincenzo Maffione 			g.options |= atoi(optarg);
302599fb123fSLuigi Rizzo 			break;
3026f8e4e36aSLuigi Rizzo 
3027f8e4e36aSLuigi Rizzo 		case 'a':       /* force affinity */
3028f8e4e36aSLuigi Rizzo 			g.affinity = atoi(optarg);
3029f8e4e36aSLuigi Rizzo 			break;
3030f8e4e36aSLuigi Rizzo 
303168b8534bSLuigi Rizzo 		case 'i':	/* interface */
3032f2637526SLuigi Rizzo 			/* a prefix of tap: netmap: or pcap: forces the mode.
3033f2637526SLuigi Rizzo 			 * otherwise we guess
3034f2637526SLuigi Rizzo 			 */
3035f2637526SLuigi Rizzo 			D("interface is %s", optarg);
3036f0ea3689SLuigi Rizzo 			if (strlen(optarg) > MAX_IFNAMELEN - 8) {
3037f0ea3689SLuigi Rizzo 				D("ifname too long %s", optarg);
3038f0ea3689SLuigi Rizzo 				break;
3039f0ea3689SLuigi Rizzo 			}
3040f0ea3689SLuigi Rizzo 			strcpy(g.ifname, optarg);
3041f2637526SLuigi Rizzo 			if (!strcmp(optarg, "null")) {
3042f8e4e36aSLuigi Rizzo 				g.dev_type = DEV_NETMAP;
3043ce3ee1e7SLuigi Rizzo 				g.dummy_send = 1;
3044f2637526SLuigi Rizzo 			} else if (!strncmp(optarg, "tap:", 4)) {
3045f2637526SLuigi Rizzo 				g.dev_type = DEV_TAP;
3046f0ea3689SLuigi Rizzo 				strcpy(g.ifname, optarg + 4);
3047f2637526SLuigi Rizzo 			} else if (!strncmp(optarg, "pcap:", 5)) {
3048f2637526SLuigi Rizzo 				g.dev_type = DEV_PCAP;
3049f0ea3689SLuigi Rizzo 				strcpy(g.ifname, optarg + 5);
3050f0ea3689SLuigi Rizzo 			} else if (!strncmp(optarg, "netmap:", 7) ||
3051f0ea3689SLuigi Rizzo 				   !strncmp(optarg, "vale", 4)) {
3052f2637526SLuigi Rizzo 				g.dev_type = DEV_NETMAP;
3053f2637526SLuigi Rizzo 			} else if (!strncmp(optarg, "tap", 3)) {
3054f2637526SLuigi Rizzo 				g.dev_type = DEV_TAP;
3055f0ea3689SLuigi Rizzo 			} else { /* prepend netmap: */
3056f2637526SLuigi Rizzo 				g.dev_type = DEV_NETMAP;
3057f0ea3689SLuigi Rizzo 				sprintf(g.ifname, "netmap:%s", optarg);
3058f2637526SLuigi Rizzo 			}
305968b8534bSLuigi Rizzo 			break;
3060f8e4e36aSLuigi Rizzo 
3061b303f675SLuigi Rizzo 		case 'I':
306280ad548dSVincenzo Maffione 			g.options |= OPT_INDIRECT;	/* use indirect buffers */
3063b303f675SLuigi Rizzo 			break;
3064b303f675SLuigi Rizzo 
306568b8534bSLuigi Rizzo 		case 'l':	/* pkt_size */
306680ad548dSVincenzo Maffione 			if (pkt_size_done) {
306780ad548dSVincenzo Maffione 				g.pkt_min_size = atoi(optarg);
306880ad548dSVincenzo Maffione 			} else {
306968b8534bSLuigi Rizzo 				g.pkt_size = atoi(optarg);
307080ad548dSVincenzo Maffione 				pkt_size_done = 1;
307180ad548dSVincenzo Maffione 			}
307268b8534bSLuigi Rizzo 			break;
3073f8e4e36aSLuigi Rizzo 
307468b8534bSLuigi Rizzo 		case 'd':
3075f8e4e36aSLuigi Rizzo 			g.dst_ip.name = optarg;
307668b8534bSLuigi Rizzo 			break;
3077f8e4e36aSLuigi Rizzo 
307868b8534bSLuigi Rizzo 		case 's':
3079f8e4e36aSLuigi Rizzo 			g.src_ip.name = optarg;
308068b8534bSLuigi Rizzo 			break;
3081f8e4e36aSLuigi Rizzo 
308268b8534bSLuigi Rizzo 		case 'T':	/* report interval */
3083f8e4e36aSLuigi Rizzo 			g.report_interval = atoi(optarg);
308468b8534bSLuigi Rizzo 			break;
3085f8e4e36aSLuigi Rizzo 
308668b8534bSLuigi Rizzo 		case 'w':
308780ad548dSVincenzo Maffione 			g.wait_link = atoi(optarg);
308880ad548dSVincenzo Maffione 			wait_link_arg = 1;
308968b8534bSLuigi Rizzo 			break;
3090f8e4e36aSLuigi Rizzo 
309180ad548dSVincenzo Maffione 		case 'W':
309280ad548dSVincenzo Maffione 			g.forever = 0; /* exit RX with no traffic */
3093f8e4e36aSLuigi Rizzo 			break;
3094f8e4e36aSLuigi Rizzo 
309568b8534bSLuigi Rizzo 		case 'b':	/* burst */
309668b8534bSLuigi Rizzo 			g.burst = atoi(optarg);
309768b8534bSLuigi Rizzo 			break;
309868b8534bSLuigi Rizzo 		case 'c':
309968b8534bSLuigi Rizzo 			g.cpus = atoi(optarg);
310068b8534bSLuigi Rizzo 			break;
310168b8534bSLuigi Rizzo 		case 'p':
310268b8534bSLuigi Rizzo 			g.nthreads = atoi(optarg);
310368b8534bSLuigi Rizzo 			break;
310468b8534bSLuigi Rizzo 
310568b8534bSLuigi Rizzo 		case 'D': /* destination mac */
3106f8e4e36aSLuigi Rizzo 			g.dst_mac.name = optarg;
310768b8534bSLuigi Rizzo 			break;
3108f8e4e36aSLuigi Rizzo 
310968b8534bSLuigi Rizzo 		case 'S': /* source mac */
3110f8e4e36aSLuigi Rizzo 			g.src_mac.name = optarg;
311168b8534bSLuigi Rizzo 			break;
311268b8534bSLuigi Rizzo 		case 'v':
311368b8534bSLuigi Rizzo 			verbose++;
31141cb4c501SLuigi Rizzo 			break;
31151cb4c501SLuigi Rizzo 		case 'R':
31161cb4c501SLuigi Rizzo 			g.tx_rate = atoi(optarg);
31171cb4c501SLuigi Rizzo 			break;
3118b303f675SLuigi Rizzo 		case 'X':
3119b303f675SLuigi Rizzo 			g.options |= OPT_DUMP;
3120ce3ee1e7SLuigi Rizzo 			break;
3121ce3ee1e7SLuigi Rizzo 		case 'C':
3122760fa2abSVincenzo Maffione 			D("WARNING: the 'C' option is deprecated, use the '+conf:' libnetmap option instead");
3123ce3ee1e7SLuigi Rizzo 			g.nmr_config = strdup(optarg);
312417885a7bSLuigi Rizzo 			break;
312517885a7bSLuigi Rizzo 		case 'H':
312617885a7bSLuigi Rizzo 			g.virt_header = atoi(optarg);
3127f2637526SLuigi Rizzo 			break;
3128f284c737SGeorge V. Neville-Neil 		case 'P':
3129f284c737SGeorge V. Neville-Neil 			g.packet_file = strdup(optarg);
3130f284c737SGeorge V. Neville-Neil 			break;
313137e3a6d3SLuigi Rizzo 		case 'r':
313237e3a6d3SLuigi Rizzo 			g.options |= OPT_RUBBISH;
313337e3a6d3SLuigi Rizzo 			break;
313456717743SAdrian Chadd 		case 'z':
313556717743SAdrian Chadd 			g.options |= OPT_RANDOM_SRC;
313656717743SAdrian Chadd 			break;
313756717743SAdrian Chadd 		case 'Z':
313856717743SAdrian Chadd 			g.options |= OPT_RANDOM_DST;
313956717743SAdrian Chadd 			break;
314037e3a6d3SLuigi Rizzo 		case 'A':
314137e3a6d3SLuigi Rizzo 			g.options |= OPT_PPS_STATS;
314237e3a6d3SLuigi Rizzo 			break;
314380ad548dSVincenzo Maffione 		case 'B':
314480ad548dSVincenzo Maffione 			/* raw packets have4 bytes crc + 20 bytes framing */
314580ad548dSVincenzo Maffione 			// XXX maybe add an option to pass the IFG
314680ad548dSVincenzo Maffione 			g.framing = 24 * 8;
314780ad548dSVincenzo Maffione 			break;
314868b8534bSLuigi Rizzo 		}
314968b8534bSLuigi Rizzo 	}
315068b8534bSLuigi Rizzo 
3151db6784f2SGeorge V. Neville-Neil 	if (strlen(g.ifname) <=0 ) {
315268b8534bSLuigi Rizzo 		D("missing ifname");
315380ad548dSVincenzo Maffione 		usage(-1);
315480ad548dSVincenzo Maffione 	}
315580ad548dSVincenzo Maffione 
315680ad548dSVincenzo Maffione 	if (g.burst == 0) {
315780ad548dSVincenzo Maffione 		g.burst = fn->default_burst;
315880ad548dSVincenzo Maffione 		D("using default burst size: %d", g.burst);
315968b8534bSLuigi Rizzo 	}
3160f8e4e36aSLuigi Rizzo 
316137e3a6d3SLuigi Rizzo 	g.system_cpus = i = system_ncpus();
3162f8e4e36aSLuigi Rizzo 	if (g.cpus < 0 || g.cpus > i) {
3163f8e4e36aSLuigi Rizzo 		D("%d cpus is too high, have only %d cpus", g.cpus, i);
316480ad548dSVincenzo Maffione 		usage(-1);
316568b8534bSLuigi Rizzo 	}
316637e3a6d3SLuigi Rizzo 	D("running on %d cpus (have %d)", g.cpus, i);
316768b8534bSLuigi Rizzo 	if (g.cpus == 0)
3168f8e4e36aSLuigi Rizzo 		g.cpus = i;
3169f8e4e36aSLuigi Rizzo 
317080ad548dSVincenzo Maffione 	if (!wait_link_arg && !strncmp(g.ifname, "vale", 4)) {
317180ad548dSVincenzo Maffione 		g.wait_link = 0;
317280ad548dSVincenzo Maffione 	}
317380ad548dSVincenzo Maffione 
31744bf50f18SLuigi Rizzo 	if (g.pkt_size < 16 || g.pkt_size > MAX_PKTSIZE) {
31754bf50f18SLuigi Rizzo 		D("bad pktsize %d [16..%d]\n", g.pkt_size, MAX_PKTSIZE);
317680ad548dSVincenzo Maffione 		usage(-1);
317780ad548dSVincenzo Maffione 	}
317880ad548dSVincenzo Maffione 
317980ad548dSVincenzo Maffione 	if (g.pkt_min_size > 0 && (g.pkt_min_size < 16 || g.pkt_min_size > g.pkt_size)) {
318080ad548dSVincenzo Maffione 		D("bad pktminsize %d [16..%d]\n", g.pkt_min_size, g.pkt_size);
318180ad548dSVincenzo Maffione 		usage(-1);
318268b8534bSLuigi Rizzo 	}
318368b8534bSLuigi Rizzo 
3184f8e4e36aSLuigi Rizzo 	if (g.src_mac.name == NULL) {
3185f8e4e36aSLuigi Rizzo 		static char mybuf[20] = "00:00:00:00:00:00";
318699fb123fSLuigi Rizzo 		/* retrieve source mac address. */
3187f8e4e36aSLuigi Rizzo 		if (source_hwaddr(g.ifname, mybuf) == -1) {
318899fb123fSLuigi Rizzo 			D("Unable to retrieve source mac");
318999fb123fSLuigi Rizzo 			// continue, fail later
319099fb123fSLuigi Rizzo 		}
3191f8e4e36aSLuigi Rizzo 		g.src_mac.name = mybuf;
319299fb123fSLuigi Rizzo 	}
3193f8e4e36aSLuigi Rizzo 	/* extract address ranges */
319480ad548dSVincenzo Maffione 	if (extract_mac_range(&g.src_mac) || extract_mac_range(&g.dst_mac))
319580ad548dSVincenzo Maffione 		usage(-1);
319680ad548dSVincenzo Maffione 	g.options |= extract_ip_range(&g.src_ip, g.af);
319780ad548dSVincenzo Maffione 	g.options |= extract_ip_range(&g.dst_ip, g.af);
3198f2637526SLuigi Rizzo 
319917885a7bSLuigi Rizzo 	if (g.virt_header != 0 && g.virt_header != VIRT_HDR_1
320017885a7bSLuigi Rizzo 			&& g.virt_header != VIRT_HDR_2) {
320117885a7bSLuigi Rizzo 		D("bad virtio-net-header length");
320280ad548dSVincenzo Maffione 		usage(-1);
320317885a7bSLuigi Rizzo 	}
320417885a7bSLuigi Rizzo 
3205f8e4e36aSLuigi Rizzo     if (g.dev_type == DEV_TAP) {
3206f8e4e36aSLuigi Rizzo 	D("want to use tap %s", g.ifname);
3207f8e4e36aSLuigi Rizzo 	g.main_fd = tap_alloc(g.ifname);
3208f8e4e36aSLuigi Rizzo 	if (g.main_fd < 0) {
3209f8e4e36aSLuigi Rizzo 		D("cannot open tap %s", g.ifname);
321080ad548dSVincenzo Maffione 		usage(-1);
321199fb123fSLuigi Rizzo 	}
3212f2637526SLuigi Rizzo #ifndef NO_PCAP
3213f2637526SLuigi Rizzo     } else if (g.dev_type == DEV_PCAP) {
3214f8e4e36aSLuigi Rizzo 	char pcap_errbuf[PCAP_ERRBUF_SIZE];
3215f8e4e36aSLuigi Rizzo 
3216f8e4e36aSLuigi Rizzo 	pcap_errbuf[0] = '\0'; // init the buffer
32174bf50f18SLuigi Rizzo 	g.p = pcap_open_live(g.ifname, 256 /* XXX */, 1, 100, pcap_errbuf);
3218f8e4e36aSLuigi Rizzo 	if (g.p == NULL) {
3219f8e4e36aSLuigi Rizzo 		D("cannot open pcap on %s", g.ifname);
322080ad548dSVincenzo Maffione 		usage(-1);
3221f8e4e36aSLuigi Rizzo 	}
32224bf50f18SLuigi Rizzo 	g.main_fd = pcap_fileno(g.p);
32234bf50f18SLuigi Rizzo 	D("using pcap on %s fileno %d", g.ifname, g.main_fd);
3224f2637526SLuigi Rizzo #endif /* !NO_PCAP */
3225f2637526SLuigi Rizzo     } else if (g.dummy_send) { /* but DEV_NETMAP */
3226ce3ee1e7SLuigi Rizzo 	D("using a dummy send routine");
322799fb123fSLuigi Rizzo     } else {
32284bfe1a4fSVincenzo Maffione 	g.nmd = nmport_prepare(g.ifname);
32294bfe1a4fSVincenzo Maffione 	if (g.nmd == NULL)
323080ad548dSVincenzo Maffione 		goto out;
32314bfe1a4fSVincenzo Maffione 
32324bfe1a4fSVincenzo Maffione 	parse_nmr_config(g.nmr_config, &g.nmd->reg);
32334bfe1a4fSVincenzo Maffione 
32344bfe1a4fSVincenzo Maffione 	g.nmd->reg.nr_flags |= NR_ACCEPT_VNET_HDR;
3235f0ea3689SLuigi Rizzo 
323668b8534bSLuigi Rizzo 	/*
3237f0ea3689SLuigi Rizzo 	 * Open the netmap device using nm_open().
323868b8534bSLuigi Rizzo 	 *
323968b8534bSLuigi Rizzo 	 * protocol stack and may cause a reset of the card,
324068b8534bSLuigi Rizzo 	 * which in turn may take some time for the PHY to
3241f0ea3689SLuigi Rizzo 	 * reconfigure. We do the open here to have time to reset.
324268b8534bSLuigi Rizzo 	 */
32434bfe1a4fSVincenzo Maffione 	g.orig_mode = g.nmd->reg.nr_mode;
324437e3a6d3SLuigi Rizzo 	if (g.nthreads > 1) {
32454bfe1a4fSVincenzo Maffione 		switch (g.orig_mode) {
32464bfe1a4fSVincenzo Maffione 		case NR_REG_ALL_NIC:
32474bfe1a4fSVincenzo Maffione 		case NR_REG_NIC_SW:
32484bfe1a4fSVincenzo Maffione 			g.nmd->reg.nr_mode = NR_REG_ONE_NIC;
32494bfe1a4fSVincenzo Maffione 			break;
32504bfe1a4fSVincenzo Maffione 		case NR_REG_SW:
32514bfe1a4fSVincenzo Maffione 			g.nmd->reg.nr_mode = NR_REG_ONE_SW;
32524bfe1a4fSVincenzo Maffione 			break;
32534bfe1a4fSVincenzo Maffione 		default:
32544bfe1a4fSVincenzo Maffione 			break;
325580ad548dSVincenzo Maffione 		}
32564bfe1a4fSVincenzo Maffione 		g.nmd->reg.nr_ringid = 0;
32574bfe1a4fSVincenzo Maffione 	}
32584bfe1a4fSVincenzo Maffione 	if (nmport_open_desc(g.nmd) < 0)
325937e3a6d3SLuigi Rizzo 		goto out;
3260f0ea3689SLuigi Rizzo 	g.main_fd = g.nmd->fd;
32614bfe1a4fSVincenzo Maffione 	ND("mapped %luKB at %p", (unsigned long)(g.nmd->req.nr_memsize>>10),
326280ad548dSVincenzo Maffione 				g.nmd->mem);
3263f0ea3689SLuigi Rizzo 
326437e3a6d3SLuigi Rizzo 	if (g.virt_header) {
326537e3a6d3SLuigi Rizzo 		/* Set the virtio-net header length, since the user asked
326645c67e8fSVincenzo Maffione 		 * for it explicitly. */
326737e3a6d3SLuigi Rizzo 		set_vnet_hdr_len(&g);
326837e3a6d3SLuigi Rizzo 	} else {
326937e3a6d3SLuigi Rizzo 		/* Check whether the netmap port we opened requires us to send
327037e3a6d3SLuigi Rizzo 		 * and receive frames with virtio-net header. */
327137e3a6d3SLuigi Rizzo 		get_vnet_hdr_len(&g);
327237e3a6d3SLuigi Rizzo 	}
327337e3a6d3SLuigi Rizzo 
32744bf50f18SLuigi Rizzo 	/* get num of queues in tx or rx */
327537e3a6d3SLuigi Rizzo 	if (g.td_type == TD_TYPE_SENDER)
32764bfe1a4fSVincenzo Maffione 		devqueues = g.nmd->reg.nr_tx_rings + g.nmd->reg.nr_host_tx_rings;
32774bf50f18SLuigi Rizzo 	else
32784bfe1a4fSVincenzo Maffione 		devqueues = g.nmd->reg.nr_rx_rings + g.nmd->reg.nr_host_rx_rings;
327968b8534bSLuigi Rizzo 
328068b8534bSLuigi Rizzo 	/* validate provided nthreads. */
328168b8534bSLuigi Rizzo 	if (g.nthreads < 1 || g.nthreads > devqueues) {
328268b8534bSLuigi Rizzo 		D("bad nthreads %d, have %d queues", g.nthreads, devqueues);
328368b8534bSLuigi Rizzo 		// continue, fail later
328468b8534bSLuigi Rizzo 	}
328568b8534bSLuigi Rizzo 
32869e53f3bdSVincenzo Maffione 	if (g.td_type == TD_TYPE_SENDER) {
32879e53f3bdSVincenzo Maffione 		int mtu = get_if_mtu(&g);
32889e53f3bdSVincenzo Maffione 
32899e53f3bdSVincenzo Maffione 		if (mtu > 0 && g.pkt_size > mtu) {
32909e53f3bdSVincenzo Maffione 			D("pkt_size (%d) must be <= mtu (%d)",
32919e53f3bdSVincenzo Maffione 				g.pkt_size, mtu);
32929e53f3bdSVincenzo Maffione 			return -1;
32939e53f3bdSVincenzo Maffione 		}
32949e53f3bdSVincenzo Maffione 	}
32959e53f3bdSVincenzo Maffione 
3296f2637526SLuigi Rizzo 	if (verbose) {
3297f0ea3689SLuigi Rizzo 		struct netmap_if *nifp = g.nmd->nifp;
32984bfe1a4fSVincenzo Maffione 		struct nmreq_register *req = &g.nmd->reg;
329968b8534bSLuigi Rizzo 
3300d7493759SVincenzo Maffione 		D("nifp at offset %"PRIu64" ntxqs %d nrxqs %d memid %d",
3301f0ea3689SLuigi Rizzo 		    req->nr_offset, req->nr_tx_rings, req->nr_rx_rings,
33024bfe1a4fSVincenzo Maffione 		    req->nr_mem_id);
33034bfe1a4fSVincenzo Maffione 		for (i = 0; i < req->nr_tx_rings + req->nr_host_tx_rings; i++) {
33044bf50f18SLuigi Rizzo 			struct netmap_ring *ring = NETMAP_TXRING(nifp, i);
3305d7493759SVincenzo Maffione 			D("   TX%d at offset %p slots %d", i,
330637e3a6d3SLuigi Rizzo 			    (void *)((char *)ring - (char *)nifp), ring->num_slots);
3307f2637526SLuigi Rizzo 		}
33084bfe1a4fSVincenzo Maffione 		for (i = 0; i < req->nr_rx_rings + req->nr_host_rx_rings; i++) {
33094bf50f18SLuigi Rizzo 			struct netmap_ring *ring = NETMAP_RXRING(nifp, i);
3310d7493759SVincenzo Maffione 			D("   RX%d at offset %p slots %d", i,
331137e3a6d3SLuigi Rizzo 			    (void *)((char *)ring - (char *)nifp), ring->num_slots);
3312f2637526SLuigi Rizzo 		}
3313f2637526SLuigi Rizzo 	}
331468b8534bSLuigi Rizzo 
331568b8534bSLuigi Rizzo 	/* Print some debug information. */
331668b8534bSLuigi Rizzo 	fprintf(stdout,
331768b8534bSLuigi Rizzo 		"%s %s: %d queues, %d threads and %d cpus.\n",
331837e3a6d3SLuigi Rizzo 		(g.td_type == TD_TYPE_SENDER) ? "Sending on" :
331937e3a6d3SLuigi Rizzo 			((g.td_type == TD_TYPE_RECEIVER) ? "Receiving from" :
332037e3a6d3SLuigi Rizzo 			"Working on"),
3321f8e4e36aSLuigi Rizzo 		g.ifname,
332268b8534bSLuigi Rizzo 		devqueues,
332368b8534bSLuigi Rizzo 		g.nthreads,
332468b8534bSLuigi Rizzo 		g.cpus);
332537e3a6d3SLuigi Rizzo 	if (g.td_type == TD_TYPE_SENDER) {
332668b8534bSLuigi Rizzo 		fprintf(stdout, "%s -> %s (%s -> %s)\n",
3327f8e4e36aSLuigi Rizzo 			g.src_ip.name, g.dst_ip.name,
3328f8e4e36aSLuigi Rizzo 			g.src_mac.name, g.dst_mac.name);
332968b8534bSLuigi Rizzo 	}
333068b8534bSLuigi Rizzo 
3331f0ea3689SLuigi Rizzo out:
333268b8534bSLuigi Rizzo 	/* Exit if something went wrong. */
3333f8e4e36aSLuigi Rizzo 	if (g.main_fd < 0) {
333468b8534bSLuigi Rizzo 		D("aborting");
333580ad548dSVincenzo Maffione 		usage(-1);
333668b8534bSLuigi Rizzo 	}
333799fb123fSLuigi Rizzo     }
333868b8534bSLuigi Rizzo 
3339ce3ee1e7SLuigi Rizzo 
334099fb123fSLuigi Rizzo 	if (g.options) {
334137e3a6d3SLuigi Rizzo 		D("--- SPECIAL OPTIONS:%s%s%s%s%s%s\n",
334299fb123fSLuigi Rizzo 			g.options & OPT_PREFETCH ? " prefetch" : "",
334399fb123fSLuigi Rizzo 			g.options & OPT_ACCESS ? " access" : "",
334499fb123fSLuigi Rizzo 			g.options & OPT_MEMCPY ? " memcpy" : "",
3345b303f675SLuigi Rizzo 			g.options & OPT_INDIRECT ? " indirect" : "",
334637e3a6d3SLuigi Rizzo 			g.options & OPT_COPY ? " copy" : "",
334737e3a6d3SLuigi Rizzo 			g.options & OPT_RUBBISH ? " rubbish " : "");
334899fb123fSLuigi Rizzo 	}
33491cb4c501SLuigi Rizzo 
3350ce3ee1e7SLuigi Rizzo 	g.tx_period.tv_sec = g.tx_period.tv_nsec = 0;
3351ce3ee1e7SLuigi Rizzo 	if (g.tx_rate > 0) {
3352ce3ee1e7SLuigi Rizzo 		/* try to have at least something every second,
335317885a7bSLuigi Rizzo 		 * reducing the burst size to some 0.01s worth of data
3354ce3ee1e7SLuigi Rizzo 		 * (but no less than one full set of fragments)
3355ce3ee1e7SLuigi Rizzo 	 	 */
335617885a7bSLuigi Rizzo 		uint64_t x;
335717885a7bSLuigi Rizzo 		int lim = (g.tx_rate)/300;
335817885a7bSLuigi Rizzo 		if (g.burst > lim)
335917885a7bSLuigi Rizzo 			g.burst = lim;
336080ad548dSVincenzo Maffione 		if (g.burst == 0)
336180ad548dSVincenzo Maffione 			g.burst = 1;
336217885a7bSLuigi Rizzo 		x = ((uint64_t)1000000000 * (uint64_t)g.burst) / (uint64_t) g.tx_rate;
336317885a7bSLuigi Rizzo 		g.tx_period.tv_nsec = x;
33641cb4c501SLuigi Rizzo 		g.tx_period.tv_sec = g.tx_period.tv_nsec / 1000000000;
33651cb4c501SLuigi Rizzo 		g.tx_period.tv_nsec = g.tx_period.tv_nsec % 1000000000;
33661cb4c501SLuigi Rizzo 	}
336737e3a6d3SLuigi Rizzo 	if (g.td_type == TD_TYPE_SENDER)
33688c3b8c83SVincenzo Maffione 	    D("Sending %d packets every  %jd.%09ld s",
33698c3b8c83SVincenzo Maffione 			g.burst, (intmax_t)g.tx_period.tv_sec, g.tx_period.tv_nsec);
337068b8534bSLuigi Rizzo 	/* Install ^C handler. */
337168b8534bSLuigi Rizzo 	global_nthreads = g.nthreads;
337237e3a6d3SLuigi Rizzo 	sigemptyset(&ss);
337337e3a6d3SLuigi Rizzo 	sigaddset(&ss, SIGINT);
337437e3a6d3SLuigi Rizzo 	/* block SIGINT now, so that all created threads will inherit the mask */
337537e3a6d3SLuigi Rizzo 	if (pthread_sigmask(SIG_BLOCK, &ss, NULL) < 0) {
337637e3a6d3SLuigi Rizzo 		D("failed to block SIGINT: %s", strerror(errno));
337737e3a6d3SLuigi Rizzo 	}
33784bfe1a4fSVincenzo Maffione 	if (start_threads(&g) < 0)
33794bfe1a4fSVincenzo Maffione 		return 1;
338037e3a6d3SLuigi Rizzo 	/* Install the handler and re-enable SIGINT for the main thread */
338180ad548dSVincenzo Maffione 	memset(&sa, 0, sizeof(sa));
338237e3a6d3SLuigi Rizzo 	sa.sa_handler = sigint_h;
338337e3a6d3SLuigi Rizzo 	if (sigaction(SIGINT, &sa, NULL) < 0) {
338437e3a6d3SLuigi Rizzo 		D("failed to install ^C handler: %s", strerror(errno));
338537e3a6d3SLuigi Rizzo 	}
338637e3a6d3SLuigi Rizzo 
338737e3a6d3SLuigi Rizzo 	if (pthread_sigmask(SIG_UNBLOCK, &ss, NULL) < 0) {
338837e3a6d3SLuigi Rizzo 		D("failed to re-enable SIGINT: %s", strerror(errno));
338937e3a6d3SLuigi Rizzo 	}
3390f8e4e36aSLuigi Rizzo 	main_thread(&g);
339137e3a6d3SLuigi Rizzo 	free(targs);
3392f8e4e36aSLuigi Rizzo 	return 0;
339368b8534bSLuigi Rizzo }
339468b8534bSLuigi Rizzo 
339568b8534bSLuigi Rizzo /* end of file */
3396