168b8534bSLuigi Rizzo /* 217885a7bSLuigi Rizzo * Copyright (C) 2011-2014 Matteo Landi, Luigi Rizzo. All rights reserved. 317885a7bSLuigi Rizzo * Copyright (C) 2013-2014 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 /* 2868b8534bSLuigi Rizzo * $FreeBSD$ 29ce3ee1e7SLuigi Rizzo * $Id: pkt-gen.c 12346 2013-06-12 17:36:25Z luigi $ 3068b8534bSLuigi Rizzo * 3168b8534bSLuigi Rizzo * Example program to show how to build a multithreaded packet 3268b8534bSLuigi Rizzo * source/sink using the netmap device. 3368b8534bSLuigi Rizzo * 3468b8534bSLuigi Rizzo * In this example we create a programmable number of threads 3568b8534bSLuigi Rizzo * to take care of all the queues of the interface used to 3668b8534bSLuigi Rizzo * send or receive traffic. 3768b8534bSLuigi Rizzo * 3868b8534bSLuigi Rizzo */ 3968b8534bSLuigi Rizzo 404bf50f18SLuigi Rizzo // #define TRASH_VHOST_HDR 414bf50f18SLuigi Rizzo 42f0ea3689SLuigi Rizzo #define _GNU_SOURCE /* for CPU_SET() */ 43f0ea3689SLuigi Rizzo #include <stdio.h> 44f0ea3689SLuigi Rizzo #define NETMAP_WITH_LIBS 45f0ea3689SLuigi Rizzo #include <net/netmap_user.h> 46f0ea3689SLuigi Rizzo 47f8e4e36aSLuigi Rizzo 48b303f675SLuigi Rizzo #include <ctype.h> // isprint() 49f0ea3689SLuigi Rizzo #include <unistd.h> // sysconf() 50f0ea3689SLuigi Rizzo #include <sys/poll.h> 51f0ea3689SLuigi Rizzo #include <arpa/inet.h> /* ntohs */ 52f0ea3689SLuigi Rizzo #include <sys/sysctl.h> /* sysctl */ 53f0ea3689SLuigi Rizzo #include <ifaddrs.h> /* getifaddrs */ 54f0ea3689SLuigi Rizzo #include <net/ethernet.h> 55f0ea3689SLuigi Rizzo #include <netinet/in.h> 56f0ea3689SLuigi Rizzo #include <netinet/ip.h> 57f0ea3689SLuigi Rizzo #include <netinet/udp.h> 58f0ea3689SLuigi Rizzo 59f0ea3689SLuigi Rizzo #include <pthread.h> 60b303f675SLuigi Rizzo 61f2637526SLuigi Rizzo #ifndef NO_PCAP 62f2637526SLuigi Rizzo #include <pcap/pcap.h> 63f2637526SLuigi Rizzo #endif 64f0ea3689SLuigi Rizzo 65f0ea3689SLuigi Rizzo #ifdef linux 66f0ea3689SLuigi Rizzo 67f0ea3689SLuigi Rizzo #define cpuset_t cpu_set_t 68f0ea3689SLuigi Rizzo 69f0ea3689SLuigi Rizzo #define ifr_flagshigh ifr_flags /* only the low 16 bits here */ 70f0ea3689SLuigi Rizzo #define IFF_PPROMISC IFF_PROMISC /* IFF_PPROMISC does not exist */ 71f0ea3689SLuigi Rizzo #include <linux/ethtool.h> 72f0ea3689SLuigi Rizzo #include <linux/sockios.h> 73f0ea3689SLuigi Rizzo 74f0ea3689SLuigi Rizzo #define CLOCK_REALTIME_PRECISE CLOCK_REALTIME 75f0ea3689SLuigi Rizzo #include <netinet/ether.h> /* ether_aton */ 76f0ea3689SLuigi Rizzo #include <linux/if_packet.h> /* sockaddr_ll */ 77f0ea3689SLuigi Rizzo #endif /* linux */ 78f0ea3689SLuigi Rizzo 79f0ea3689SLuigi Rizzo #ifdef __FreeBSD__ 80f0ea3689SLuigi Rizzo #include <sys/endian.h> /* le64toh */ 81f0ea3689SLuigi Rizzo #include <machine/param.h> 82f0ea3689SLuigi Rizzo 83f0ea3689SLuigi Rizzo #include <pthread_np.h> /* pthread w/ affinity */ 84f0ea3689SLuigi Rizzo #include <sys/cpuset.h> /* cpu_set */ 85f0ea3689SLuigi Rizzo #include <net/if_dl.h> /* LLADDR */ 86f0ea3689SLuigi Rizzo #endif /* __FreeBSD__ */ 87f0ea3689SLuigi Rizzo 88f0ea3689SLuigi Rizzo #ifdef __APPLE__ 89f0ea3689SLuigi Rizzo 90f0ea3689SLuigi Rizzo #define cpuset_t uint64_t // XXX 91f0ea3689SLuigi Rizzo static inline void CPU_ZERO(cpuset_t *p) 92f0ea3689SLuigi Rizzo { 93f0ea3689SLuigi Rizzo *p = 0; 94f0ea3689SLuigi Rizzo } 95f0ea3689SLuigi Rizzo 96f0ea3689SLuigi Rizzo static inline void CPU_SET(uint32_t i, cpuset_t *p) 97f0ea3689SLuigi Rizzo { 98f0ea3689SLuigi Rizzo *p |= 1<< (i & 0x3f); 99f0ea3689SLuigi Rizzo } 100f0ea3689SLuigi Rizzo 101f0ea3689SLuigi Rizzo #define pthread_setaffinity_np(a, b, c) ((void)a, 0) 102f0ea3689SLuigi Rizzo 103f0ea3689SLuigi Rizzo #define ifr_flagshigh ifr_flags // XXX 104f0ea3689SLuigi Rizzo #define IFF_PPROMISC IFF_PROMISC 105f0ea3689SLuigi Rizzo #include <net/if_dl.h> /* LLADDR */ 106f0ea3689SLuigi Rizzo #define clock_gettime(a,b) \ 107f0ea3689SLuigi Rizzo do {struct timespec t0 = {0,0}; *(b) = t0; } while (0) 108f0ea3689SLuigi Rizzo #endif /* __APPLE__ */ 109f0ea3689SLuigi Rizzo 110ce3ee1e7SLuigi Rizzo const char *default_payload="netmap pkt-gen DIRECT payload\n" 111ce3ee1e7SLuigi Rizzo "http://info.iet.unipi.it/~luigi/netmap/ "; 112ce3ee1e7SLuigi Rizzo 113ce3ee1e7SLuigi Rizzo const char *indirect_payload="netmap pkt-gen indirect payload\n" 11468b8534bSLuigi Rizzo "http://info.iet.unipi.it/~luigi/netmap/ "; 11568b8534bSLuigi Rizzo 11668b8534bSLuigi Rizzo int verbose = 0; 11768b8534bSLuigi Rizzo 118f0ea3689SLuigi Rizzo #define SKIP_PAYLOAD 1 /* do not check payload. XXX unused */ 11968b8534bSLuigi Rizzo 12017885a7bSLuigi Rizzo 12117885a7bSLuigi Rizzo #define VIRT_HDR_1 10 /* length of a base vnet-hdr */ 12217885a7bSLuigi Rizzo #define VIRT_HDR_2 12 /* length of the extenede vnet-hdr */ 12317885a7bSLuigi Rizzo #define VIRT_HDR_MAX VIRT_HDR_2 12417885a7bSLuigi Rizzo struct virt_header { 12517885a7bSLuigi Rizzo uint8_t fields[VIRT_HDR_MAX]; 12617885a7bSLuigi Rizzo }; 12717885a7bSLuigi Rizzo 1284bf50f18SLuigi Rizzo #define MAX_BODYSIZE 16384 1294bf50f18SLuigi Rizzo 13068b8534bSLuigi Rizzo struct pkt { 13117885a7bSLuigi Rizzo struct virt_header vh; 13268b8534bSLuigi Rizzo struct ether_header eh; 13368b8534bSLuigi Rizzo struct ip ip; 13468b8534bSLuigi Rizzo struct udphdr udp; 1354bf50f18SLuigi Rizzo uint8_t body[MAX_BODYSIZE]; // XXX hardwired 13668b8534bSLuigi Rizzo } __attribute__((__packed__)); 13768b8534bSLuigi Rizzo 138f8e4e36aSLuigi Rizzo struct ip_range { 139f8e4e36aSLuigi Rizzo char *name; 140ce3ee1e7SLuigi Rizzo uint32_t start, end; /* same as struct in_addr */ 141ce3ee1e7SLuigi Rizzo uint16_t port0, port1; 142f8e4e36aSLuigi Rizzo }; 143f8e4e36aSLuigi Rizzo 144f8e4e36aSLuigi Rizzo struct mac_range { 145f8e4e36aSLuigi Rizzo char *name; 146f8e4e36aSLuigi Rizzo struct ether_addr start, end; 147f8e4e36aSLuigi Rizzo }; 148f8e4e36aSLuigi Rizzo 149f0ea3689SLuigi Rizzo /* ifname can be netmap:foo-xxxx */ 150f0ea3689SLuigi Rizzo #define MAX_IFNAMELEN 64 /* our buffer for ifname */ 1514bf50f18SLuigi Rizzo //#define MAX_PKTSIZE 1536 1524bf50f18SLuigi Rizzo #define MAX_PKTSIZE MAX_BODYSIZE /* XXX: + IP_HDR + ETH_HDR */ 1534bf50f18SLuigi Rizzo 1544bf50f18SLuigi Rizzo /* compact timestamp to fit into 60 byte packet. (enough to obtain RTT) */ 1554bf50f18SLuigi Rizzo struct tstamp { 1564bf50f18SLuigi Rizzo uint32_t sec; 1574bf50f18SLuigi Rizzo uint32_t nsec; 1584bf50f18SLuigi Rizzo }; 1594bf50f18SLuigi Rizzo 16068b8534bSLuigi Rizzo /* 16168b8534bSLuigi Rizzo * global arguments for all threads 16268b8534bSLuigi Rizzo */ 163f8e4e36aSLuigi Rizzo 16468b8534bSLuigi Rizzo struct glob_arg { 165f8e4e36aSLuigi Rizzo struct ip_range src_ip; 166f8e4e36aSLuigi Rizzo struct ip_range dst_ip; 167f8e4e36aSLuigi Rizzo struct mac_range dst_mac; 168f8e4e36aSLuigi Rizzo struct mac_range src_mac; 16968b8534bSLuigi Rizzo int pkt_size; 17068b8534bSLuigi Rizzo int burst; 171f8e4e36aSLuigi Rizzo int forever; 17268b8534bSLuigi Rizzo int npackets; /* total packets to send */ 173ce3ee1e7SLuigi Rizzo int frags; /* fragments per packet */ 17468b8534bSLuigi Rizzo int nthreads; 17568b8534bSLuigi Rizzo int cpus; 17699fb123fSLuigi Rizzo int options; /* testing */ 17799fb123fSLuigi Rizzo #define OPT_PREFETCH 1 17899fb123fSLuigi Rizzo #define OPT_ACCESS 2 17999fb123fSLuigi Rizzo #define OPT_COPY 4 18099fb123fSLuigi Rizzo #define OPT_MEMCPY 8 181f8e4e36aSLuigi Rizzo #define OPT_TS 16 /* add a timestamp */ 182b303f675SLuigi Rizzo #define OPT_INDIRECT 32 /* use indirect buffers, tx only */ 183b303f675SLuigi Rizzo #define OPT_DUMP 64 /* dump rx/tx traffic */ 1844bf50f18SLuigi Rizzo #define OPT_MONITOR_TX 128 1854bf50f18SLuigi Rizzo #define OPT_MONITOR_RX 256 186f8e4e36aSLuigi Rizzo int dev_type; 187f2637526SLuigi Rizzo #ifndef NO_PCAP 18868b8534bSLuigi Rizzo pcap_t *p; 189f2637526SLuigi Rizzo #endif 19068b8534bSLuigi Rizzo 1911cb4c501SLuigi Rizzo int tx_rate; 1921cb4c501SLuigi Rizzo struct timespec tx_period; 1931cb4c501SLuigi Rizzo 194f8e4e36aSLuigi Rizzo int affinity; 195f8e4e36aSLuigi Rizzo int main_fd; 196f0ea3689SLuigi Rizzo struct nm_desc *nmd; 197f2637526SLuigi Rizzo int report_interval; /* milliseconds between prints */ 198f8e4e36aSLuigi Rizzo void *(*td_body)(void *); 199f8e4e36aSLuigi Rizzo void *mmap_addr; 200f0ea3689SLuigi Rizzo char ifname[MAX_IFNAMELEN]; 201ce3ee1e7SLuigi Rizzo char *nmr_config; 202ce3ee1e7SLuigi Rizzo int dummy_send; 20317885a7bSLuigi Rizzo int virt_header; /* send also the virt_header */ 204f0ea3689SLuigi Rizzo int extra_bufs; /* goes in nr_arg3 */ 20568b8534bSLuigi Rizzo }; 206f8e4e36aSLuigi Rizzo enum dev_type { DEV_NONE, DEV_NETMAP, DEV_PCAP, DEV_TAP }; 207f8e4e36aSLuigi Rizzo 20868b8534bSLuigi Rizzo 20968b8534bSLuigi Rizzo /* 21068b8534bSLuigi Rizzo * Arguments for a new thread. The same structure is used by 21168b8534bSLuigi Rizzo * the source and the sink 21268b8534bSLuigi Rizzo */ 21368b8534bSLuigi Rizzo struct targ { 21468b8534bSLuigi Rizzo struct glob_arg *g; 21568b8534bSLuigi Rizzo int used; 21668b8534bSLuigi Rizzo int completed; 2173fe77e68SEd Maste int cancel; 21868b8534bSLuigi Rizzo int fd; 219f0ea3689SLuigi Rizzo struct nm_desc *nmd; 220f8e4e36aSLuigi Rizzo volatile uint64_t count; 2211cb4c501SLuigi Rizzo struct timespec tic, toc; 22268b8534bSLuigi Rizzo int me; 22368b8534bSLuigi Rizzo pthread_t thread; 22468b8534bSLuigi Rizzo int affinity; 22568b8534bSLuigi Rizzo 22668b8534bSLuigi Rizzo struct pkt pkt; 22768b8534bSLuigi Rizzo }; 22868b8534bSLuigi Rizzo 22968b8534bSLuigi Rizzo 230f8e4e36aSLuigi Rizzo /* 231f8e4e36aSLuigi Rizzo * extract the extremes from a range of ipv4 addresses. 232f8e4e36aSLuigi Rizzo * addr_lo[-addr_hi][:port_lo[-port_hi]] 233f8e4e36aSLuigi Rizzo */ 234f8e4e36aSLuigi Rizzo static void 235f8e4e36aSLuigi Rizzo extract_ip_range(struct ip_range *r) 236f8e4e36aSLuigi Rizzo { 237ce3ee1e7SLuigi Rizzo char *ap, *pp; 238ce3ee1e7SLuigi Rizzo struct in_addr a; 239f8e4e36aSLuigi Rizzo 24017885a7bSLuigi Rizzo if (verbose) 241f8e4e36aSLuigi Rizzo D("extract IP range from %s", r->name); 242ce3ee1e7SLuigi Rizzo r->port0 = r->port1 = 0; 243ce3ee1e7SLuigi Rizzo r->start = r->end = 0; 244ce3ee1e7SLuigi Rizzo 245ce3ee1e7SLuigi Rizzo /* the first - splits start/end of range */ 246ce3ee1e7SLuigi Rizzo ap = index(r->name, '-'); /* do we have ports ? */ 247ce3ee1e7SLuigi Rizzo if (ap) { 248ce3ee1e7SLuigi Rizzo *ap++ = '\0'; 249ce3ee1e7SLuigi Rizzo } 250ce3ee1e7SLuigi Rizzo /* grab the initial values (mandatory) */ 251ce3ee1e7SLuigi Rizzo pp = index(r->name, ':'); 252ce3ee1e7SLuigi Rizzo if (pp) { 253ce3ee1e7SLuigi Rizzo *pp++ = '\0'; 254ce3ee1e7SLuigi Rizzo r->port0 = r->port1 = strtol(pp, NULL, 0); 255ce3ee1e7SLuigi Rizzo }; 256ce3ee1e7SLuigi Rizzo inet_aton(r->name, &a); 257ce3ee1e7SLuigi Rizzo r->start = r->end = ntohl(a.s_addr); 258ce3ee1e7SLuigi Rizzo if (ap) { 259ce3ee1e7SLuigi Rizzo pp = index(ap, ':'); 260ce3ee1e7SLuigi Rizzo if (pp) { 261ce3ee1e7SLuigi Rizzo *pp++ = '\0'; 262ce3ee1e7SLuigi Rizzo if (*pp) 263ce3ee1e7SLuigi Rizzo r->port1 = strtol(pp, NULL, 0); 264ce3ee1e7SLuigi Rizzo } 265ce3ee1e7SLuigi Rizzo if (*ap) { 266ce3ee1e7SLuigi Rizzo inet_aton(ap, &a); 267ce3ee1e7SLuigi Rizzo r->end = ntohl(a.s_addr); 268ce3ee1e7SLuigi Rizzo } 269ce3ee1e7SLuigi Rizzo } 270ce3ee1e7SLuigi Rizzo if (r->port0 > r->port1) { 271ce3ee1e7SLuigi Rizzo uint16_t tmp = r->port0; 272f8e4e36aSLuigi Rizzo r->port0 = r->port1; 273ce3ee1e7SLuigi Rizzo r->port1 = tmp; 274f8e4e36aSLuigi Rizzo } 275ce3ee1e7SLuigi Rizzo if (r->start > r->end) { 276ce3ee1e7SLuigi Rizzo uint32_t tmp = r->start; 277f8e4e36aSLuigi Rizzo r->start = r->end; 278ce3ee1e7SLuigi Rizzo r->end = tmp; 279f8e4e36aSLuigi Rizzo } 280ce3ee1e7SLuigi Rizzo { 281ce3ee1e7SLuigi Rizzo struct in_addr a; 282ce3ee1e7SLuigi Rizzo char buf1[16]; // one ip address 283ce3ee1e7SLuigi Rizzo 284ce3ee1e7SLuigi Rizzo a.s_addr = htonl(r->end); 285ce3ee1e7SLuigi Rizzo strncpy(buf1, inet_ntoa(a), sizeof(buf1)); 286ce3ee1e7SLuigi Rizzo a.s_addr = htonl(r->start); 28717885a7bSLuigi Rizzo if (1) 288ce3ee1e7SLuigi Rizzo D("range is %s:%d to %s:%d", 289ce3ee1e7SLuigi Rizzo inet_ntoa(a), r->port0, buf1, r->port1); 290ce3ee1e7SLuigi Rizzo } 291f8e4e36aSLuigi Rizzo } 292f8e4e36aSLuigi Rizzo 293f8e4e36aSLuigi Rizzo static void 294f8e4e36aSLuigi Rizzo extract_mac_range(struct mac_range *r) 295f8e4e36aSLuigi Rizzo { 29617885a7bSLuigi Rizzo if (verbose) 297f8e4e36aSLuigi Rizzo D("extract MAC range from %s", r->name); 298f8e4e36aSLuigi Rizzo bcopy(ether_aton(r->name), &r->start, 6); 299f8e4e36aSLuigi Rizzo bcopy(ether_aton(r->name), &r->end, 6); 300f8e4e36aSLuigi Rizzo #if 0 301f8e4e36aSLuigi Rizzo bcopy(targ->src_mac, eh->ether_shost, 6); 302f8e4e36aSLuigi Rizzo p = index(targ->g->src_mac, '-'); 303f8e4e36aSLuigi Rizzo if (p) 304f8e4e36aSLuigi Rizzo targ->src_mac_range = atoi(p+1); 305f8e4e36aSLuigi Rizzo 306f8e4e36aSLuigi Rizzo bcopy(ether_aton(targ->g->dst_mac), targ->dst_mac, 6); 307f8e4e36aSLuigi Rizzo bcopy(targ->dst_mac, eh->ether_dhost, 6); 308f8e4e36aSLuigi Rizzo p = index(targ->g->dst_mac, '-'); 309f8e4e36aSLuigi Rizzo if (p) 310f8e4e36aSLuigi Rizzo targ->dst_mac_range = atoi(p+1); 311f8e4e36aSLuigi Rizzo #endif 31217885a7bSLuigi Rizzo if (verbose) 313f8e4e36aSLuigi Rizzo D("%s starts at %s", r->name, ether_ntoa(&r->start)); 314f8e4e36aSLuigi Rizzo } 315f8e4e36aSLuigi Rizzo 31668b8534bSLuigi Rizzo static struct targ *targs; 31768b8534bSLuigi Rizzo static int global_nthreads; 31868b8534bSLuigi Rizzo 31968b8534bSLuigi Rizzo /* control-C handler */ 32068b8534bSLuigi Rizzo static void 321f8e4e36aSLuigi Rizzo sigint_h(int sig) 32268b8534bSLuigi Rizzo { 323f8e4e36aSLuigi Rizzo int i; 32468b8534bSLuigi Rizzo 325f8e4e36aSLuigi Rizzo (void)sig; /* UNUSED */ 3264bf50f18SLuigi Rizzo D("received control-C on thread %p", pthread_self()); 327f8e4e36aSLuigi Rizzo for (i = 0; i < global_nthreads; i++) { 328f8e4e36aSLuigi Rizzo targs[i].cancel = 1; 329f8e4e36aSLuigi Rizzo } 33068b8534bSLuigi Rizzo signal(SIGINT, SIG_DFL); 33168b8534bSLuigi Rizzo } 33268b8534bSLuigi Rizzo 33368b8534bSLuigi Rizzo /* sysctl wrapper to return the number of active CPUs */ 33468b8534bSLuigi Rizzo static int 33568b8534bSLuigi Rizzo system_ncpus(void) 33668b8534bSLuigi Rizzo { 337f0ea3689SLuigi Rizzo int ncpus; 338f0ea3689SLuigi Rizzo #if defined (__FreeBSD__) 339f0ea3689SLuigi Rizzo int mib[2] = { CTL_HW, HW_NCPU }; 340f0ea3689SLuigi Rizzo size_t len = sizeof(mib); 34168b8534bSLuigi Rizzo sysctl(mib, 2, &ncpus, &len, NULL, 0); 342f0ea3689SLuigi Rizzo #elif defined(linux) 343f0ea3689SLuigi Rizzo ncpus = sysconf(_SC_NPROCESSORS_ONLN); 344f0ea3689SLuigi Rizzo #else /* others */ 345f0ea3689SLuigi Rizzo ncpus = 1; 346f0ea3689SLuigi Rizzo #endif /* others */ 34768b8534bSLuigi Rizzo return (ncpus); 34868b8534bSLuigi Rizzo } 34968b8534bSLuigi Rizzo 350f8e4e36aSLuigi Rizzo #ifdef __linux__ 351f8e4e36aSLuigi Rizzo #define sockaddr_dl sockaddr_ll 352f8e4e36aSLuigi Rizzo #define sdl_family sll_family 353f8e4e36aSLuigi Rizzo #define AF_LINK AF_PACKET 354f8e4e36aSLuigi Rizzo #define LLADDR(s) s->sll_addr; 355f8e4e36aSLuigi Rizzo #include <linux/if_tun.h> 356f8e4e36aSLuigi Rizzo #define TAP_CLONEDEV "/dev/net/tun" 357f8e4e36aSLuigi Rizzo #endif /* __linux__ */ 358f8e4e36aSLuigi Rizzo 359f8e4e36aSLuigi Rizzo #ifdef __FreeBSD__ 360f8e4e36aSLuigi Rizzo #include <net/if_tun.h> 361f8e4e36aSLuigi Rizzo #define TAP_CLONEDEV "/dev/tap" 362f8e4e36aSLuigi Rizzo #endif /* __FreeBSD */ 363f8e4e36aSLuigi Rizzo 364f8e4e36aSLuigi Rizzo #ifdef __APPLE__ 365f8e4e36aSLuigi Rizzo // #warning TAP not supported on apple ? 366f8e4e36aSLuigi Rizzo #include <net/if_utun.h> 367f8e4e36aSLuigi Rizzo #define TAP_CLONEDEV "/dev/tap" 368f8e4e36aSLuigi Rizzo #endif /* __APPLE__ */ 369f8e4e36aSLuigi Rizzo 370f8e4e36aSLuigi Rizzo 37168b8534bSLuigi Rizzo /* 372ce3ee1e7SLuigi Rizzo * parse the vale configuration in conf and put it in nmr. 373f0ea3689SLuigi Rizzo * Return the flag set if necessary. 374ce3ee1e7SLuigi Rizzo * The configuration may consist of 0 to 4 numbers separated 375fc6eb28bSHiren Panchasara * by commas: #tx-slots,#rx-slots,#tx-rings,#rx-rings. 376ce3ee1e7SLuigi Rizzo * Missing numbers or zeroes stand for default values. 377ce3ee1e7SLuigi Rizzo * As an additional convenience, if exactly one number 378fc6eb28bSHiren Panchasara * is specified, then this is assigned to both #tx-slots and #rx-slots. 379fc6eb28bSHiren Panchasara * If there is no 4th number, then the 3rd is assigned to both #tx-rings 380ce3ee1e7SLuigi Rizzo * and #rx-rings. 381ce3ee1e7SLuigi Rizzo */ 382f0ea3689SLuigi Rizzo int 383f0ea3689SLuigi Rizzo parse_nmr_config(const char* conf, struct nmreq *nmr) 384ce3ee1e7SLuigi Rizzo { 385ce3ee1e7SLuigi Rizzo char *w, *tok; 386ce3ee1e7SLuigi Rizzo int i, v; 387ce3ee1e7SLuigi Rizzo 388ce3ee1e7SLuigi Rizzo nmr->nr_tx_rings = nmr->nr_rx_rings = 0; 389ce3ee1e7SLuigi Rizzo nmr->nr_tx_slots = nmr->nr_rx_slots = 0; 390ce3ee1e7SLuigi Rizzo if (conf == NULL || ! *conf) 391f0ea3689SLuigi Rizzo return 0; 392ce3ee1e7SLuigi Rizzo w = strdup(conf); 393ce3ee1e7SLuigi Rizzo for (i = 0, tok = strtok(w, ","); tok; i++, tok = strtok(NULL, ",")) { 394ce3ee1e7SLuigi Rizzo v = atoi(tok); 395ce3ee1e7SLuigi Rizzo switch (i) { 396ce3ee1e7SLuigi Rizzo case 0: 397ce3ee1e7SLuigi Rizzo nmr->nr_tx_slots = nmr->nr_rx_slots = v; 398ce3ee1e7SLuigi Rizzo break; 399ce3ee1e7SLuigi Rizzo case 1: 400ce3ee1e7SLuigi Rizzo nmr->nr_rx_slots = v; 401ce3ee1e7SLuigi Rizzo break; 402ce3ee1e7SLuigi Rizzo case 2: 403ce3ee1e7SLuigi Rizzo nmr->nr_tx_rings = nmr->nr_rx_rings = v; 404ce3ee1e7SLuigi Rizzo break; 405ce3ee1e7SLuigi Rizzo case 3: 406ce3ee1e7SLuigi Rizzo nmr->nr_rx_rings = v; 407ce3ee1e7SLuigi Rizzo break; 408ce3ee1e7SLuigi Rizzo default: 409ce3ee1e7SLuigi Rizzo D("ignored config: %s", tok); 410ce3ee1e7SLuigi Rizzo break; 411ce3ee1e7SLuigi Rizzo } 412ce3ee1e7SLuigi Rizzo } 413ce3ee1e7SLuigi Rizzo D("txr %d txd %d rxr %d rxd %d", 414ce3ee1e7SLuigi Rizzo nmr->nr_tx_rings, nmr->nr_tx_slots, 415ce3ee1e7SLuigi Rizzo nmr->nr_rx_rings, nmr->nr_rx_slots); 416ce3ee1e7SLuigi Rizzo free(w); 417f0ea3689SLuigi Rizzo return (nmr->nr_tx_rings || nmr->nr_tx_slots || 418f0ea3689SLuigi Rizzo nmr->nr_rx_rings || nmr->nr_rx_slots) ? 419f0ea3689SLuigi Rizzo NM_OPEN_RING_CFG : 0; 420ce3ee1e7SLuigi Rizzo } 421ce3ee1e7SLuigi Rizzo 422ce3ee1e7SLuigi Rizzo 423ce3ee1e7SLuigi Rizzo /* 42468b8534bSLuigi Rizzo * locate the src mac address for our interface, put it 42568b8534bSLuigi Rizzo * into the user-supplied buffer. return 0 if ok, -1 on error. 42668b8534bSLuigi Rizzo */ 42768b8534bSLuigi Rizzo static int 42868b8534bSLuigi Rizzo source_hwaddr(const char *ifname, char *buf) 42968b8534bSLuigi Rizzo { 43068b8534bSLuigi Rizzo struct ifaddrs *ifaphead, *ifap; 43168b8534bSLuigi Rizzo int l = sizeof(ifap->ifa_name); 43268b8534bSLuigi Rizzo 43368b8534bSLuigi Rizzo if (getifaddrs(&ifaphead) != 0) { 43468b8534bSLuigi Rizzo D("getifaddrs %s failed", ifname); 43568b8534bSLuigi Rizzo return (-1); 43668b8534bSLuigi Rizzo } 43768b8534bSLuigi Rizzo 43868b8534bSLuigi Rizzo for (ifap = ifaphead; ifap; ifap = ifap->ifa_next) { 43968b8534bSLuigi Rizzo struct sockaddr_dl *sdl = 44068b8534bSLuigi Rizzo (struct sockaddr_dl *)ifap->ifa_addr; 44168b8534bSLuigi Rizzo uint8_t *mac; 44268b8534bSLuigi Rizzo 44368b8534bSLuigi Rizzo if (!sdl || sdl->sdl_family != AF_LINK) 44468b8534bSLuigi Rizzo continue; 44568b8534bSLuigi Rizzo if (strncmp(ifap->ifa_name, ifname, l) != 0) 44668b8534bSLuigi Rizzo continue; 44768b8534bSLuigi Rizzo mac = (uint8_t *)LLADDR(sdl); 44868b8534bSLuigi Rizzo sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x", 44968b8534bSLuigi Rizzo mac[0], mac[1], mac[2], 45068b8534bSLuigi Rizzo mac[3], mac[4], mac[5]); 45168b8534bSLuigi Rizzo if (verbose) 45268b8534bSLuigi Rizzo D("source hwaddr %s", buf); 45368b8534bSLuigi Rizzo break; 45468b8534bSLuigi Rizzo } 45568b8534bSLuigi Rizzo freeifaddrs(ifaphead); 45668b8534bSLuigi Rizzo return ifap ? 0 : 1; 45768b8534bSLuigi Rizzo } 45868b8534bSLuigi Rizzo 45968b8534bSLuigi Rizzo 46068b8534bSLuigi Rizzo /* set the thread affinity. */ 46168b8534bSLuigi Rizzo static int 46268b8534bSLuigi Rizzo setaffinity(pthread_t me, int i) 46368b8534bSLuigi Rizzo { 46468b8534bSLuigi Rizzo cpuset_t cpumask; 46568b8534bSLuigi Rizzo 46668b8534bSLuigi Rizzo if (i == -1) 46768b8534bSLuigi Rizzo return 0; 46868b8534bSLuigi Rizzo 46968b8534bSLuigi Rizzo /* Set thread affinity affinity.*/ 47068b8534bSLuigi Rizzo CPU_ZERO(&cpumask); 47168b8534bSLuigi Rizzo CPU_SET(i, &cpumask); 47268b8534bSLuigi Rizzo 47368b8534bSLuigi Rizzo if (pthread_setaffinity_np(me, sizeof(cpuset_t), &cpumask) != 0) { 47417885a7bSLuigi Rizzo D("Unable to set affinity: %s", strerror(errno)); 47568b8534bSLuigi Rizzo return 1; 47668b8534bSLuigi Rizzo } 47768b8534bSLuigi Rizzo return 0; 47868b8534bSLuigi Rizzo } 47968b8534bSLuigi Rizzo 48068b8534bSLuigi Rizzo /* Compute the checksum of the given ip header. */ 48168b8534bSLuigi Rizzo static uint16_t 482f8e4e36aSLuigi Rizzo checksum(const void *data, uint16_t len, uint32_t sum) 48368b8534bSLuigi Rizzo { 48468b8534bSLuigi Rizzo const uint8_t *addr = data; 485f8e4e36aSLuigi Rizzo uint32_t i; 48668b8534bSLuigi Rizzo 487f8e4e36aSLuigi Rizzo /* Checksum all the pairs of bytes first... */ 488f8e4e36aSLuigi Rizzo for (i = 0; i < (len & ~1U); i += 2) { 489f8e4e36aSLuigi Rizzo sum += (u_int16_t)ntohs(*((u_int16_t *)(addr + i))); 490f8e4e36aSLuigi Rizzo if (sum > 0xFFFF) 491f8e4e36aSLuigi Rizzo sum -= 0xFFFF; 492f8e4e36aSLuigi Rizzo } 493f8e4e36aSLuigi Rizzo /* 494f8e4e36aSLuigi Rizzo * If there's a single byte left over, checksum it, too. 495f8e4e36aSLuigi Rizzo * Network byte order is big-endian, so the remaining byte is 496f8e4e36aSLuigi Rizzo * the high byte. 497f8e4e36aSLuigi Rizzo */ 498f8e4e36aSLuigi Rizzo if (i < len) { 499f8e4e36aSLuigi Rizzo sum += addr[i] << 8; 500f8e4e36aSLuigi Rizzo if (sum > 0xFFFF) 501f8e4e36aSLuigi Rizzo sum -= 0xFFFF; 502f8e4e36aSLuigi Rizzo } 503f8e4e36aSLuigi Rizzo return sum; 50468b8534bSLuigi Rizzo } 50568b8534bSLuigi Rizzo 506f8e4e36aSLuigi Rizzo static u_int16_t 507f8e4e36aSLuigi Rizzo wrapsum(u_int32_t sum) 508f8e4e36aSLuigi Rizzo { 509f8e4e36aSLuigi Rizzo sum = ~sum & 0xFFFF; 510f8e4e36aSLuigi Rizzo return (htons(sum)); 51168b8534bSLuigi Rizzo } 51268b8534bSLuigi Rizzo 513b303f675SLuigi Rizzo /* Check the payload of the packet for errors (use it for debug). 514b303f675SLuigi Rizzo * Look for consecutive ascii representations of the size of the packet. 515b303f675SLuigi Rizzo */ 516b303f675SLuigi Rizzo static void 517b303f675SLuigi Rizzo dump_payload(char *p, int len, struct netmap_ring *ring, int cur) 518b303f675SLuigi Rizzo { 519b303f675SLuigi Rizzo char buf[128]; 520b303f675SLuigi Rizzo int i, j, i0; 521b303f675SLuigi Rizzo 522b303f675SLuigi Rizzo /* get the length in ASCII of the length of the packet. */ 523b303f675SLuigi Rizzo 524ce3ee1e7SLuigi Rizzo printf("ring %p cur %5d [buf %6d flags 0x%04x len %5d]\n", 525ce3ee1e7SLuigi Rizzo ring, cur, ring->slot[cur].buf_idx, 526ce3ee1e7SLuigi Rizzo ring->slot[cur].flags, len); 527b303f675SLuigi Rizzo /* hexdump routine */ 528b303f675SLuigi Rizzo for (i = 0; i < len; ) { 529b303f675SLuigi Rizzo memset(buf, sizeof(buf), ' '); 530b303f675SLuigi Rizzo sprintf(buf, "%5d: ", i); 531b303f675SLuigi Rizzo i0 = i; 532b303f675SLuigi Rizzo for (j=0; j < 16 && i < len; i++, j++) 533b303f675SLuigi Rizzo sprintf(buf+7+j*3, "%02x ", (uint8_t)(p[i])); 534b303f675SLuigi Rizzo i = i0; 535b303f675SLuigi Rizzo for (j=0; j < 16 && i < len; i++, j++) 536b303f675SLuigi Rizzo sprintf(buf+7+j + 48, "%c", 537b303f675SLuigi Rizzo isprint(p[i]) ? p[i] : '.'); 538b303f675SLuigi Rizzo printf("%s\n", buf); 539b303f675SLuigi Rizzo } 540b303f675SLuigi Rizzo } 541b303f675SLuigi Rizzo 54268b8534bSLuigi Rizzo /* 54368b8534bSLuigi Rizzo * Fill a packet with some payload. 544f8e4e36aSLuigi Rizzo * We create a UDP packet so the payload starts at 545f8e4e36aSLuigi Rizzo * 14+20+8 = 42 bytes. 54668b8534bSLuigi Rizzo */ 547f8e4e36aSLuigi Rizzo #ifdef __linux__ 548f8e4e36aSLuigi Rizzo #define uh_sport source 549f8e4e36aSLuigi Rizzo #define uh_dport dest 550f8e4e36aSLuigi Rizzo #define uh_ulen len 551f8e4e36aSLuigi Rizzo #define uh_sum check 552f8e4e36aSLuigi Rizzo #endif /* linux */ 553b303f675SLuigi Rizzo 554ce3ee1e7SLuigi Rizzo /* 555ce3ee1e7SLuigi Rizzo * increment the addressed in the packet, 556ce3ee1e7SLuigi Rizzo * starting from the least significant field. 557ce3ee1e7SLuigi Rizzo * DST_IP DST_PORT SRC_IP SRC_PORT 558ce3ee1e7SLuigi Rizzo */ 559ce3ee1e7SLuigi Rizzo static void 560ce3ee1e7SLuigi Rizzo update_addresses(struct pkt *pkt, struct glob_arg *g) 561ce3ee1e7SLuigi Rizzo { 562ce3ee1e7SLuigi Rizzo uint32_t a; 563ce3ee1e7SLuigi Rizzo uint16_t p; 564ce3ee1e7SLuigi Rizzo struct ip *ip = &pkt->ip; 565ce3ee1e7SLuigi Rizzo struct udphdr *udp = &pkt->udp; 566ce3ee1e7SLuigi Rizzo 567f2637526SLuigi Rizzo do { 568ce3ee1e7SLuigi Rizzo p = ntohs(udp->uh_sport); 569ce3ee1e7SLuigi Rizzo if (p < g->src_ip.port1) { /* just inc, no wrap */ 570ce3ee1e7SLuigi Rizzo udp->uh_sport = htons(p + 1); 571f2637526SLuigi Rizzo break; 572ce3ee1e7SLuigi Rizzo } 573ce3ee1e7SLuigi Rizzo udp->uh_sport = htons(g->src_ip.port0); 574ce3ee1e7SLuigi Rizzo 575ce3ee1e7SLuigi Rizzo a = ntohl(ip->ip_src.s_addr); 576ce3ee1e7SLuigi Rizzo if (a < g->src_ip.end) { /* just inc, no wrap */ 577ce3ee1e7SLuigi Rizzo ip->ip_src.s_addr = htonl(a + 1); 578f2637526SLuigi Rizzo break; 579ce3ee1e7SLuigi Rizzo } 580ce3ee1e7SLuigi Rizzo ip->ip_src.s_addr = htonl(g->src_ip.start); 581ce3ee1e7SLuigi Rizzo 582ce3ee1e7SLuigi Rizzo udp->uh_sport = htons(g->src_ip.port0); 583ce3ee1e7SLuigi Rizzo p = ntohs(udp->uh_dport); 584ce3ee1e7SLuigi Rizzo if (p < g->dst_ip.port1) { /* just inc, no wrap */ 585ce3ee1e7SLuigi Rizzo udp->uh_dport = htons(p + 1); 586f2637526SLuigi Rizzo break; 587ce3ee1e7SLuigi Rizzo } 588ce3ee1e7SLuigi Rizzo udp->uh_dport = htons(g->dst_ip.port0); 589ce3ee1e7SLuigi Rizzo 590ce3ee1e7SLuigi Rizzo a = ntohl(ip->ip_dst.s_addr); 591ce3ee1e7SLuigi Rizzo if (a < g->dst_ip.end) { /* just inc, no wrap */ 592ce3ee1e7SLuigi Rizzo ip->ip_dst.s_addr = htonl(a + 1); 593f2637526SLuigi Rizzo break; 594ce3ee1e7SLuigi Rizzo } 595ce3ee1e7SLuigi Rizzo ip->ip_dst.s_addr = htonl(g->dst_ip.start); 596f2637526SLuigi Rizzo } while (0); 597f2637526SLuigi Rizzo // update checksum 598ce3ee1e7SLuigi Rizzo } 599ce3ee1e7SLuigi Rizzo 600ce3ee1e7SLuigi Rizzo /* 601ce3ee1e7SLuigi Rizzo * initialize one packet and prepare for the next one. 602ce3ee1e7SLuigi Rizzo * The copy could be done better instead of repeating it each time. 603ce3ee1e7SLuigi Rizzo */ 60468b8534bSLuigi Rizzo static void 60568b8534bSLuigi Rizzo initialize_packet(struct targ *targ) 60668b8534bSLuigi Rizzo { 60768b8534bSLuigi Rizzo struct pkt *pkt = &targ->pkt; 60868b8534bSLuigi Rizzo struct ether_header *eh; 60968b8534bSLuigi Rizzo struct ip *ip; 61068b8534bSLuigi Rizzo struct udphdr *udp; 611f8e4e36aSLuigi Rizzo uint16_t paylen = targ->g->pkt_size - sizeof(*eh) - sizeof(struct ip); 612b303f675SLuigi Rizzo const char *payload = targ->g->options & OPT_INDIRECT ? 613ce3ee1e7SLuigi Rizzo indirect_payload : default_payload; 614f2637526SLuigi Rizzo int i, l0 = strlen(payload); 61568b8534bSLuigi Rizzo 616ce3ee1e7SLuigi Rizzo /* create a nice NUL-terminated string */ 617f2637526SLuigi Rizzo for (i = 0; i < paylen; i += l0) { 618f2637526SLuigi Rizzo if (l0 > paylen - i) 619f2637526SLuigi Rizzo l0 = paylen - i; // last round 620f2637526SLuigi Rizzo bcopy(payload, pkt->body + i, l0); 62168b8534bSLuigi Rizzo } 62268b8534bSLuigi Rizzo pkt->body[i-1] = '\0'; 62368b8534bSLuigi Rizzo ip = &pkt->ip; 624f8e4e36aSLuigi Rizzo 625ce3ee1e7SLuigi Rizzo /* prepare the headers */ 62668b8534bSLuigi Rizzo ip->ip_v = IPVERSION; 62768b8534bSLuigi Rizzo ip->ip_hl = 5; 62868b8534bSLuigi Rizzo ip->ip_id = 0; 62968b8534bSLuigi Rizzo ip->ip_tos = IPTOS_LOWDELAY; 63068b8534bSLuigi Rizzo ip->ip_len = ntohs(targ->g->pkt_size - sizeof(*eh)); 63168b8534bSLuigi Rizzo ip->ip_id = 0; 63268b8534bSLuigi Rizzo ip->ip_off = htons(IP_DF); /* Don't fragment */ 63368b8534bSLuigi Rizzo ip->ip_ttl = IPDEFTTL; 63468b8534bSLuigi Rizzo ip->ip_p = IPPROTO_UDP; 635ce3ee1e7SLuigi Rizzo ip->ip_dst.s_addr = htonl(targ->g->dst_ip.start); 636ce3ee1e7SLuigi Rizzo ip->ip_src.s_addr = htonl(targ->g->src_ip.start); 637f8e4e36aSLuigi Rizzo ip->ip_sum = wrapsum(checksum(ip, sizeof(*ip), 0)); 638f8e4e36aSLuigi Rizzo 639f8e4e36aSLuigi Rizzo 640f8e4e36aSLuigi Rizzo udp = &pkt->udp; 641ce3ee1e7SLuigi Rizzo udp->uh_sport = htons(targ->g->src_ip.port0); 642ce3ee1e7SLuigi Rizzo udp->uh_dport = htons(targ->g->dst_ip.port0); 643f8e4e36aSLuigi Rizzo udp->uh_ulen = htons(paylen); 644f8e4e36aSLuigi Rizzo /* Magic: taken from sbin/dhclient/packet.c */ 645f8e4e36aSLuigi Rizzo udp->uh_sum = wrapsum(checksum(udp, sizeof(*udp), 646f8e4e36aSLuigi Rizzo checksum(pkt->body, 647f8e4e36aSLuigi Rizzo paylen - sizeof(*udp), 648f8e4e36aSLuigi Rizzo checksum(&ip->ip_src, 2 * sizeof(ip->ip_src), 649f8e4e36aSLuigi Rizzo IPPROTO_UDP + (u_int32_t)ntohs(udp->uh_ulen) 650f8e4e36aSLuigi Rizzo ) 651f8e4e36aSLuigi Rizzo ) 652f8e4e36aSLuigi Rizzo )); 65368b8534bSLuigi Rizzo 65468b8534bSLuigi Rizzo eh = &pkt->eh; 655f8e4e36aSLuigi Rizzo bcopy(&targ->g->src_mac.start, eh->ether_shost, 6); 656f8e4e36aSLuigi Rizzo bcopy(&targ->g->dst_mac.start, eh->ether_dhost, 6); 65768b8534bSLuigi Rizzo eh->ether_type = htons(ETHERTYPE_IP); 65817885a7bSLuigi Rizzo 65917885a7bSLuigi Rizzo bzero(&pkt->vh, sizeof(pkt->vh)); 6604bf50f18SLuigi Rizzo #ifdef TRASH_VHOST_HDR 6614bf50f18SLuigi Rizzo /* set bogus content */ 6624bf50f18SLuigi Rizzo pkt->vh.fields[0] = 0xff; 6634bf50f18SLuigi Rizzo pkt->vh.fields[1] = 0xff; 6644bf50f18SLuigi Rizzo pkt->vh.fields[2] = 0xff; 6654bf50f18SLuigi Rizzo pkt->vh.fields[3] = 0xff; 6664bf50f18SLuigi Rizzo pkt->vh.fields[4] = 0xff; 6674bf50f18SLuigi Rizzo pkt->vh.fields[5] = 0xff; 6684bf50f18SLuigi Rizzo #endif /* TRASH_VHOST_HDR */ 669b303f675SLuigi Rizzo // dump_payload((void *)pkt, targ->g->pkt_size, NULL, 0); 67068b8534bSLuigi Rizzo } 67168b8534bSLuigi Rizzo 6724bf50f18SLuigi Rizzo static void 6734bf50f18SLuigi Rizzo set_vnet_hdr_len(struct targ *t) 6744bf50f18SLuigi Rizzo { 6754bf50f18SLuigi Rizzo int err, l = t->g->virt_header; 6764bf50f18SLuigi Rizzo struct nmreq req; 6774bf50f18SLuigi Rizzo 6784bf50f18SLuigi Rizzo if (l == 0) 6794bf50f18SLuigi Rizzo return; 6804bf50f18SLuigi Rizzo 6814bf50f18SLuigi Rizzo memset(&req, 0, sizeof(req)); 6824bf50f18SLuigi Rizzo bcopy(t->nmd->req.nr_name, req.nr_name, sizeof(req.nr_name)); 6834bf50f18SLuigi Rizzo req.nr_version = NETMAP_API; 6844bf50f18SLuigi Rizzo req.nr_cmd = NETMAP_BDG_VNET_HDR; 6854bf50f18SLuigi Rizzo req.nr_arg1 = l; 6864bf50f18SLuigi Rizzo err = ioctl(t->fd, NIOCREGIF, &req); 6874bf50f18SLuigi Rizzo if (err) { 6884bf50f18SLuigi Rizzo D("Unable to set vnet header length %d", l); 6894bf50f18SLuigi Rizzo } 6904bf50f18SLuigi Rizzo } 69168b8534bSLuigi Rizzo 69268b8534bSLuigi Rizzo 69368b8534bSLuigi Rizzo /* 69468b8534bSLuigi Rizzo * create and enqueue a batch of packets on a ring. 69568b8534bSLuigi Rizzo * On the last one set NS_REPORT to tell the driver to generate 69668b8534bSLuigi Rizzo * an interrupt when done. 69768b8534bSLuigi Rizzo */ 69868b8534bSLuigi Rizzo static int 69917885a7bSLuigi Rizzo send_packets(struct netmap_ring *ring, struct pkt *pkt, void *frame, 70017885a7bSLuigi Rizzo int size, struct glob_arg *g, u_int count, int options, 70117885a7bSLuigi Rizzo u_int nfrags) 70268b8534bSLuigi Rizzo { 70317885a7bSLuigi Rizzo u_int n, sent, cur = ring->cur; 704f2637526SLuigi Rizzo u_int fcnt; 70568b8534bSLuigi Rizzo 70617885a7bSLuigi Rizzo n = nm_ring_space(ring); 70717885a7bSLuigi Rizzo if (n < count) 70817885a7bSLuigi Rizzo count = n; 709ce3ee1e7SLuigi Rizzo if (count < nfrags) { 710ce3ee1e7SLuigi Rizzo D("truncating packet, no room for frags %d %d", 711ce3ee1e7SLuigi Rizzo count, nfrags); 712ce3ee1e7SLuigi Rizzo } 71399fb123fSLuigi Rizzo #if 0 71499fb123fSLuigi Rizzo if (options & (OPT_COPY | OPT_PREFETCH) ) { 71568b8534bSLuigi Rizzo for (sent = 0; sent < count; sent++) { 71668b8534bSLuigi Rizzo struct netmap_slot *slot = &ring->slot[cur]; 71768b8534bSLuigi Rizzo char *p = NETMAP_BUF(ring, slot->buf_idx); 71868b8534bSLuigi Rizzo 719f2637526SLuigi Rizzo __builtin_prefetch(p); 72017885a7bSLuigi Rizzo cur = nm_ring_next(ring, cur); 72199fb123fSLuigi Rizzo } 72299fb123fSLuigi Rizzo cur = ring->cur; 72399fb123fSLuigi Rizzo } 72499fb123fSLuigi Rizzo #endif 725ce3ee1e7SLuigi Rizzo for (fcnt = nfrags, sent = 0; sent < count; sent++) { 72699fb123fSLuigi Rizzo struct netmap_slot *slot = &ring->slot[cur]; 72799fb123fSLuigi Rizzo char *p = NETMAP_BUF(ring, slot->buf_idx); 72899fb123fSLuigi Rizzo 729b303f675SLuigi Rizzo slot->flags = 0; 730b303f675SLuigi Rizzo if (options & OPT_INDIRECT) { 731b303f675SLuigi Rizzo slot->flags |= NS_INDIRECT; 73217885a7bSLuigi Rizzo slot->ptr = (uint64_t)frame; 733ce3ee1e7SLuigi Rizzo } else if (options & OPT_COPY) { 734f0ea3689SLuigi Rizzo nm_pkt_copy(frame, p, size); 735f2637526SLuigi Rizzo if (fcnt == nfrags) 736ce3ee1e7SLuigi Rizzo update_addresses(pkt, g); 737ce3ee1e7SLuigi Rizzo } else if (options & OPT_MEMCPY) { 73817885a7bSLuigi Rizzo memcpy(p, frame, size); 739f2637526SLuigi Rizzo if (fcnt == nfrags) 740ce3ee1e7SLuigi Rizzo update_addresses(pkt, g); 741ce3ee1e7SLuigi Rizzo } else if (options & OPT_PREFETCH) { 742f2637526SLuigi Rizzo __builtin_prefetch(p); 743ce3ee1e7SLuigi Rizzo } 744ce3ee1e7SLuigi Rizzo if (options & OPT_DUMP) 745ce3ee1e7SLuigi Rizzo dump_payload(p, size, ring, cur); 74668b8534bSLuigi Rizzo slot->len = size; 747ce3ee1e7SLuigi Rizzo if (--fcnt > 0) 748ce3ee1e7SLuigi Rizzo slot->flags |= NS_MOREFRAG; 749ce3ee1e7SLuigi Rizzo else 750ce3ee1e7SLuigi Rizzo fcnt = nfrags; 751ce3ee1e7SLuigi Rizzo if (sent == count - 1) { 752ce3ee1e7SLuigi Rizzo slot->flags &= ~NS_MOREFRAG; 75368b8534bSLuigi Rizzo slot->flags |= NS_REPORT; 754ce3ee1e7SLuigi Rizzo } 75517885a7bSLuigi Rizzo cur = nm_ring_next(ring, cur); 75668b8534bSLuigi Rizzo } 75717885a7bSLuigi Rizzo ring->head = ring->cur = cur; 75868b8534bSLuigi Rizzo 75968b8534bSLuigi Rizzo return (sent); 76068b8534bSLuigi Rizzo } 76168b8534bSLuigi Rizzo 762f8e4e36aSLuigi Rizzo /* 763f8e4e36aSLuigi Rizzo * Send a packet, and wait for a response. 764f8e4e36aSLuigi Rizzo * The payload (after UDP header, ofs 42) has a 4-byte sequence 765f8e4e36aSLuigi Rizzo * followed by a struct timeval (or bintime?) 766f8e4e36aSLuigi Rizzo */ 767f8e4e36aSLuigi Rizzo #define PAY_OFS 42 /* where in the pkt... */ 768f8e4e36aSLuigi Rizzo 76968b8534bSLuigi Rizzo static void * 770f8e4e36aSLuigi Rizzo pinger_body(void *data) 77168b8534bSLuigi Rizzo { 77268b8534bSLuigi Rizzo struct targ *targ = (struct targ *) data; 773f0ea3689SLuigi Rizzo struct pollfd pfd = { .fd = targ->fd, .events = POLLIN }; 774f0ea3689SLuigi Rizzo struct netmap_if *nifp = targ->nmd->nifp; 775f8e4e36aSLuigi Rizzo int i, rx = 0, n = targ->g->npackets; 77617885a7bSLuigi Rizzo void *frame; 77717885a7bSLuigi Rizzo int size; 778f0ea3689SLuigi Rizzo uint32_t sent = 0; 779f0ea3689SLuigi Rizzo struct timespec ts, now, last_print; 780f0ea3689SLuigi Rizzo uint32_t count = 0, min = 1000000000, av = 0; 78117885a7bSLuigi Rizzo 78217885a7bSLuigi Rizzo frame = &targ->pkt; 78317885a7bSLuigi Rizzo frame += sizeof(targ->pkt.vh) - targ->g->virt_header; 78417885a7bSLuigi Rizzo size = targ->g->pkt_size + targ->g->virt_header; 785e5ecae38SEd Maste 786f8e4e36aSLuigi Rizzo 787f8e4e36aSLuigi Rizzo if (targ->g->nthreads > 1) { 788f8e4e36aSLuigi Rizzo D("can only ping with 1 thread"); 789f8e4e36aSLuigi Rizzo return NULL; 790f95a30bdSEd Maste } 791f8e4e36aSLuigi Rizzo 792f8e4e36aSLuigi Rizzo clock_gettime(CLOCK_REALTIME_PRECISE, &last_print); 79317885a7bSLuigi Rizzo now = last_print; 794f8e4e36aSLuigi Rizzo while (n == 0 || (int)sent < n) { 795f8e4e36aSLuigi Rizzo struct netmap_ring *ring = NETMAP_TXRING(nifp, 0); 796f8e4e36aSLuigi Rizzo struct netmap_slot *slot; 797f8e4e36aSLuigi Rizzo char *p; 79817885a7bSLuigi Rizzo for (i = 0; i < 1; i++) { /* XXX why the loop for 1 pkt ? */ 799f8e4e36aSLuigi Rizzo slot = &ring->slot[ring->cur]; 80017885a7bSLuigi Rizzo slot->len = size; 801f8e4e36aSLuigi Rizzo p = NETMAP_BUF(ring, slot->buf_idx); 802f8e4e36aSLuigi Rizzo 80317885a7bSLuigi Rizzo if (nm_ring_empty(ring)) { 804f8e4e36aSLuigi Rizzo D("-- ouch, cannot send"); 805f8e4e36aSLuigi Rizzo } else { 8064bf50f18SLuigi Rizzo struct tstamp *tp; 807f0ea3689SLuigi Rizzo nm_pkt_copy(frame, p, size); 808f8e4e36aSLuigi Rizzo clock_gettime(CLOCK_REALTIME_PRECISE, &ts); 809f8e4e36aSLuigi Rizzo bcopy(&sent, p+42, sizeof(sent)); 8104bf50f18SLuigi Rizzo tp = (struct tstamp *)(p+46); 8114bf50f18SLuigi Rizzo tp->sec = (uint32_t)ts.tv_sec; 8124bf50f18SLuigi Rizzo tp->nsec = (uint32_t)ts.tv_nsec; 813f8e4e36aSLuigi Rizzo sent++; 81417885a7bSLuigi Rizzo ring->head = ring->cur = nm_ring_next(ring, ring->cur); 815f8e4e36aSLuigi Rizzo } 816f8e4e36aSLuigi Rizzo } 817f8e4e36aSLuigi Rizzo /* should use a parameter to decide how often to send */ 818f0ea3689SLuigi Rizzo if (poll(&pfd, 1, 3000) <= 0) { 81917885a7bSLuigi Rizzo D("poll error/timeout on queue %d: %s", targ->me, 82017885a7bSLuigi Rizzo strerror(errno)); 821f8e4e36aSLuigi Rizzo continue; 822f8e4e36aSLuigi Rizzo } 823f8e4e36aSLuigi Rizzo /* see what we got back */ 824f0ea3689SLuigi Rizzo for (i = targ->nmd->first_tx_ring; 825f0ea3689SLuigi Rizzo i <= targ->nmd->last_tx_ring; i++) { 826f8e4e36aSLuigi Rizzo ring = NETMAP_RXRING(nifp, i); 82717885a7bSLuigi Rizzo while (!nm_ring_empty(ring)) { 828f8e4e36aSLuigi Rizzo uint32_t seq; 8294bf50f18SLuigi Rizzo struct tstamp *tp; 830f8e4e36aSLuigi Rizzo slot = &ring->slot[ring->cur]; 831f8e4e36aSLuigi Rizzo p = NETMAP_BUF(ring, slot->buf_idx); 832f8e4e36aSLuigi Rizzo 833f8e4e36aSLuigi Rizzo clock_gettime(CLOCK_REALTIME_PRECISE, &now); 834f8e4e36aSLuigi Rizzo bcopy(p+42, &seq, sizeof(seq)); 8354bf50f18SLuigi Rizzo tp = (struct tstamp *)(p+46); 8364bf50f18SLuigi Rizzo ts.tv_sec = (time_t)tp->sec; 8374bf50f18SLuigi Rizzo ts.tv_nsec = (long)tp->nsec; 838f8e4e36aSLuigi Rizzo ts.tv_sec = now.tv_sec - ts.tv_sec; 839f8e4e36aSLuigi Rizzo ts.tv_nsec = now.tv_nsec - ts.tv_nsec; 840f8e4e36aSLuigi Rizzo if (ts.tv_nsec < 0) { 841f8e4e36aSLuigi Rizzo ts.tv_nsec += 1000000000; 842f8e4e36aSLuigi Rizzo ts.tv_sec--; 843f8e4e36aSLuigi Rizzo } 844f8e4e36aSLuigi Rizzo if (1) D("seq %d/%d delta %d.%09d", seq, sent, 845f8e4e36aSLuigi Rizzo (int)ts.tv_sec, (int)ts.tv_nsec); 846f8e4e36aSLuigi Rizzo if (ts.tv_nsec < (int)min) 847f8e4e36aSLuigi Rizzo min = ts.tv_nsec; 848f8e4e36aSLuigi Rizzo count ++; 849f8e4e36aSLuigi Rizzo av += ts.tv_nsec; 85017885a7bSLuigi Rizzo ring->head = ring->cur = nm_ring_next(ring, ring->cur); 851f8e4e36aSLuigi Rizzo rx++; 852f8e4e36aSLuigi Rizzo } 853f8e4e36aSLuigi Rizzo } 854f8e4e36aSLuigi Rizzo //D("tx %d rx %d", sent, rx); 855f8e4e36aSLuigi Rizzo //usleep(100000); 856f8e4e36aSLuigi Rizzo ts.tv_sec = now.tv_sec - last_print.tv_sec; 857f8e4e36aSLuigi Rizzo ts.tv_nsec = now.tv_nsec - last_print.tv_nsec; 858f8e4e36aSLuigi Rizzo if (ts.tv_nsec < 0) { 859f8e4e36aSLuigi Rizzo ts.tv_nsec += 1000000000; 860f8e4e36aSLuigi Rizzo ts.tv_sec--; 861f8e4e36aSLuigi Rizzo } 862f8e4e36aSLuigi Rizzo if (ts.tv_sec >= 1) { 863f8e4e36aSLuigi Rizzo D("count %d min %d av %d", 864f8e4e36aSLuigi Rizzo count, min, av/count); 865f8e4e36aSLuigi Rizzo count = 0; 866f8e4e36aSLuigi Rizzo av = 0; 867f8e4e36aSLuigi Rizzo min = 100000000; 868f8e4e36aSLuigi Rizzo last_print = now; 869f8e4e36aSLuigi Rizzo } 870f8e4e36aSLuigi Rizzo } 871f8e4e36aSLuigi Rizzo return NULL; 872f8e4e36aSLuigi Rizzo } 873f8e4e36aSLuigi Rizzo 874f8e4e36aSLuigi Rizzo 875f8e4e36aSLuigi Rizzo /* 876f8e4e36aSLuigi Rizzo * reply to ping requests 877f8e4e36aSLuigi Rizzo */ 878f8e4e36aSLuigi Rizzo static void * 879f8e4e36aSLuigi Rizzo ponger_body(void *data) 880f8e4e36aSLuigi Rizzo { 881f8e4e36aSLuigi Rizzo struct targ *targ = (struct targ *) data; 882f0ea3689SLuigi Rizzo struct pollfd pfd = { .fd = targ->fd, .events = POLLIN }; 883f0ea3689SLuigi Rizzo struct netmap_if *nifp = targ->nmd->nifp; 884f8e4e36aSLuigi Rizzo struct netmap_ring *txring, *rxring; 885f8e4e36aSLuigi Rizzo int i, rx = 0, sent = 0, n = targ->g->npackets; 886f8e4e36aSLuigi Rizzo 887f8e4e36aSLuigi Rizzo if (targ->g->nthreads > 1) { 888f8e4e36aSLuigi Rizzo D("can only reply ping with 1 thread"); 889f8e4e36aSLuigi Rizzo return NULL; 890f8e4e36aSLuigi Rizzo } 891f8e4e36aSLuigi Rizzo D("understood ponger %d but don't know how to do it", n); 892f8e4e36aSLuigi Rizzo while (n == 0 || sent < n) { 893f8e4e36aSLuigi Rizzo uint32_t txcur, txavail; 894f8e4e36aSLuigi Rizzo //#define BUSYWAIT 895f8e4e36aSLuigi Rizzo #ifdef BUSYWAIT 896f0ea3689SLuigi Rizzo ioctl(pfd.fd, NIOCRXSYNC, NULL); 897f8e4e36aSLuigi Rizzo #else 898f0ea3689SLuigi Rizzo if (poll(&pfd, 1, 1000) <= 0) { 89917885a7bSLuigi Rizzo D("poll error/timeout on queue %d: %s", targ->me, 90017885a7bSLuigi Rizzo strerror(errno)); 901f8e4e36aSLuigi Rizzo continue; 902f8e4e36aSLuigi Rizzo } 903f8e4e36aSLuigi Rizzo #endif 904f8e4e36aSLuigi Rizzo txring = NETMAP_TXRING(nifp, 0); 905f8e4e36aSLuigi Rizzo txcur = txring->cur; 90617885a7bSLuigi Rizzo txavail = nm_ring_space(txring); 907f8e4e36aSLuigi Rizzo /* see what we got back */ 908f0ea3689SLuigi Rizzo for (i = targ->nmd->first_rx_ring; i <= targ->nmd->last_rx_ring; i++) { 909f8e4e36aSLuigi Rizzo rxring = NETMAP_RXRING(nifp, i); 91017885a7bSLuigi Rizzo while (!nm_ring_empty(rxring)) { 911f8e4e36aSLuigi Rizzo uint16_t *spkt, *dpkt; 912f8e4e36aSLuigi Rizzo uint32_t cur = rxring->cur; 913f8e4e36aSLuigi Rizzo struct netmap_slot *slot = &rxring->slot[cur]; 914f8e4e36aSLuigi Rizzo char *src, *dst; 915f8e4e36aSLuigi Rizzo src = NETMAP_BUF(rxring, slot->buf_idx); 916f8e4e36aSLuigi Rizzo //D("got pkt %p of size %d", src, slot->len); 91717885a7bSLuigi Rizzo rxring->head = rxring->cur = nm_ring_next(rxring, cur); 918f8e4e36aSLuigi Rizzo rx++; 919f8e4e36aSLuigi Rizzo if (txavail == 0) 920f8e4e36aSLuigi Rizzo continue; 921f8e4e36aSLuigi Rizzo dst = NETMAP_BUF(txring, 922f8e4e36aSLuigi Rizzo txring->slot[txcur].buf_idx); 923f8e4e36aSLuigi Rizzo /* copy... */ 924f8e4e36aSLuigi Rizzo dpkt = (uint16_t *)dst; 925f8e4e36aSLuigi Rizzo spkt = (uint16_t *)src; 926f0ea3689SLuigi Rizzo nm_pkt_copy(src, dst, slot->len); 927f8e4e36aSLuigi Rizzo dpkt[0] = spkt[3]; 928f8e4e36aSLuigi Rizzo dpkt[1] = spkt[4]; 929f8e4e36aSLuigi Rizzo dpkt[2] = spkt[5]; 930f8e4e36aSLuigi Rizzo dpkt[3] = spkt[0]; 931f8e4e36aSLuigi Rizzo dpkt[4] = spkt[1]; 932f8e4e36aSLuigi Rizzo dpkt[5] = spkt[2]; 933f8e4e36aSLuigi Rizzo txring->slot[txcur].len = slot->len; 934f8e4e36aSLuigi Rizzo /* XXX swap src dst mac */ 93517885a7bSLuigi Rizzo txcur = nm_ring_next(txring, txcur); 936f8e4e36aSLuigi Rizzo txavail--; 937f8e4e36aSLuigi Rizzo sent++; 938f8e4e36aSLuigi Rizzo } 939f8e4e36aSLuigi Rizzo } 94017885a7bSLuigi Rizzo txring->head = txring->cur = txcur; 941f8e4e36aSLuigi Rizzo targ->count = sent; 942f8e4e36aSLuigi Rizzo #ifdef BUSYWAIT 943f0ea3689SLuigi Rizzo ioctl(pfd.fd, NIOCTXSYNC, NULL); 944f8e4e36aSLuigi Rizzo #endif 945f8e4e36aSLuigi Rizzo //D("tx %d rx %d", sent, rx); 946f8e4e36aSLuigi Rizzo } 947f8e4e36aSLuigi Rizzo return NULL; 948f8e4e36aSLuigi Rizzo } 949f8e4e36aSLuigi Rizzo 9501cb4c501SLuigi Rizzo static __inline int 9511cb4c501SLuigi Rizzo timespec_ge(const struct timespec *a, const struct timespec *b) 9521cb4c501SLuigi Rizzo { 9531cb4c501SLuigi Rizzo 9541cb4c501SLuigi Rizzo if (a->tv_sec > b->tv_sec) 9551cb4c501SLuigi Rizzo return (1); 9561cb4c501SLuigi Rizzo if (a->tv_sec < b->tv_sec) 9571cb4c501SLuigi Rizzo return (0); 9581cb4c501SLuigi Rizzo if (a->tv_nsec >= b->tv_nsec) 9591cb4c501SLuigi Rizzo return (1); 9601cb4c501SLuigi Rizzo return (0); 9611cb4c501SLuigi Rizzo } 9621cb4c501SLuigi Rizzo 9631cb4c501SLuigi Rizzo static __inline struct timespec 9641cb4c501SLuigi Rizzo timeval2spec(const struct timeval *a) 9651cb4c501SLuigi Rizzo { 9661cb4c501SLuigi Rizzo struct timespec ts = { 9671cb4c501SLuigi Rizzo .tv_sec = a->tv_sec, 9681cb4c501SLuigi Rizzo .tv_nsec = a->tv_usec * 1000 9691cb4c501SLuigi Rizzo }; 9701cb4c501SLuigi Rizzo return ts; 9711cb4c501SLuigi Rizzo } 9721cb4c501SLuigi Rizzo 9731cb4c501SLuigi Rizzo static __inline struct timeval 9741cb4c501SLuigi Rizzo timespec2val(const struct timespec *a) 9751cb4c501SLuigi Rizzo { 9761cb4c501SLuigi Rizzo struct timeval tv = { 9771cb4c501SLuigi Rizzo .tv_sec = a->tv_sec, 9781cb4c501SLuigi Rizzo .tv_usec = a->tv_nsec / 1000 9791cb4c501SLuigi Rizzo }; 9801cb4c501SLuigi Rizzo return tv; 9811cb4c501SLuigi Rizzo } 9821cb4c501SLuigi Rizzo 9831cb4c501SLuigi Rizzo 98417885a7bSLuigi Rizzo static __inline struct timespec 98517885a7bSLuigi Rizzo timespec_add(struct timespec a, struct timespec b) 9861cb4c501SLuigi Rizzo { 98717885a7bSLuigi Rizzo struct timespec ret = { a.tv_sec + b.tv_sec, a.tv_nsec + b.tv_nsec }; 98817885a7bSLuigi Rizzo if (ret.tv_nsec >= 1000000000) { 98917885a7bSLuigi Rizzo ret.tv_sec++; 99017885a7bSLuigi Rizzo ret.tv_nsec -= 1000000000; 9911cb4c501SLuigi Rizzo } 99217885a7bSLuigi Rizzo return ret; 9931cb4c501SLuigi Rizzo } 9941cb4c501SLuigi Rizzo 99517885a7bSLuigi Rizzo static __inline struct timespec 99617885a7bSLuigi Rizzo timespec_sub(struct timespec a, struct timespec b) 9971cb4c501SLuigi Rizzo { 99817885a7bSLuigi Rizzo struct timespec ret = { a.tv_sec - b.tv_sec, a.tv_nsec - b.tv_nsec }; 99917885a7bSLuigi Rizzo if (ret.tv_nsec < 0) { 100017885a7bSLuigi Rizzo ret.tv_sec--; 100117885a7bSLuigi Rizzo ret.tv_nsec += 1000000000; 10021cb4c501SLuigi Rizzo } 100317885a7bSLuigi Rizzo return ret; 10041cb4c501SLuigi Rizzo } 10051cb4c501SLuigi Rizzo 1006f8e4e36aSLuigi Rizzo 100717885a7bSLuigi Rizzo /* 100817885a7bSLuigi Rizzo * wait until ts, either busy or sleeping if more than 1ms. 100917885a7bSLuigi Rizzo * Return wakeup time. 101017885a7bSLuigi Rizzo */ 101117885a7bSLuigi Rizzo static struct timespec 101217885a7bSLuigi Rizzo wait_time(struct timespec ts) 101317885a7bSLuigi Rizzo { 101417885a7bSLuigi Rizzo for (;;) { 101517885a7bSLuigi Rizzo struct timespec w, cur; 101617885a7bSLuigi Rizzo clock_gettime(CLOCK_REALTIME_PRECISE, &cur); 101717885a7bSLuigi Rizzo w = timespec_sub(ts, cur); 101817885a7bSLuigi Rizzo if (w.tv_sec < 0) 101917885a7bSLuigi Rizzo return cur; 102017885a7bSLuigi Rizzo else if (w.tv_sec > 0 || w.tv_nsec > 1000000) 102117885a7bSLuigi Rizzo poll(NULL, 0, 1); 102217885a7bSLuigi Rizzo } 102317885a7bSLuigi Rizzo } 102417885a7bSLuigi Rizzo 1025f8e4e36aSLuigi Rizzo static void * 1026f8e4e36aSLuigi Rizzo sender_body(void *data) 1027f8e4e36aSLuigi Rizzo { 1028f8e4e36aSLuigi Rizzo struct targ *targ = (struct targ *) data; 1029f0ea3689SLuigi Rizzo struct pollfd pfd = { .fd = targ->fd, .events = POLLOUT }; 10304bf50f18SLuigi Rizzo struct netmap_if *nifp; 1031f8e4e36aSLuigi Rizzo struct netmap_ring *txring; 1032f0ea3689SLuigi Rizzo int i, n = targ->g->npackets / targ->g->nthreads; 1033f0ea3689SLuigi Rizzo int64_t sent = 0; 1034f8e4e36aSLuigi Rizzo int options = targ->g->options | OPT_COPY; 103517885a7bSLuigi Rizzo struct timespec nexttime = { 0, 0}; // XXX silence compiler 10361cb4c501SLuigi Rizzo int rate_limit = targ->g->tx_rate; 103717885a7bSLuigi Rizzo struct pkt *pkt = &targ->pkt; 103817885a7bSLuigi Rizzo void *frame; 103917885a7bSLuigi Rizzo int size; 104017885a7bSLuigi Rizzo 104117885a7bSLuigi Rizzo frame = pkt; 104217885a7bSLuigi Rizzo frame += sizeof(pkt->vh) - targ->g->virt_header; 104317885a7bSLuigi Rizzo size = targ->g->pkt_size + targ->g->virt_header; 1044b303f675SLuigi Rizzo 10454bf50f18SLuigi Rizzo D("start, fd %d main_fd %d", targ->fd, targ->g->main_fd); 104668b8534bSLuigi Rizzo if (setaffinity(targ->thread, targ->affinity)) 104768b8534bSLuigi Rizzo goto quit; 104868b8534bSLuigi Rizzo 104968b8534bSLuigi Rizzo /* main loop.*/ 10501cb4c501SLuigi Rizzo clock_gettime(CLOCK_REALTIME_PRECISE, &targ->tic); 10511cb4c501SLuigi Rizzo if (rate_limit) { 105217885a7bSLuigi Rizzo targ->tic = timespec_add(targ->tic, (struct timespec){2,0}); 10531cb4c501SLuigi Rizzo targ->tic.tv_nsec = 0; 105417885a7bSLuigi Rizzo wait_time(targ->tic); 10551cb4c501SLuigi Rizzo nexttime = targ->tic; 10561cb4c501SLuigi Rizzo } 1057f2637526SLuigi Rizzo if (targ->g->dev_type == DEV_TAP) { 1058f8e4e36aSLuigi Rizzo D("writing to file desc %d", targ->g->main_fd); 1059f8e4e36aSLuigi Rizzo 1060f8e4e36aSLuigi Rizzo for (i = 0; !targ->cancel && (n == 0 || sent < n); i++) { 106117885a7bSLuigi Rizzo if (write(targ->g->main_fd, frame, size) != -1) 1062f8e4e36aSLuigi Rizzo sent++; 1063ce3ee1e7SLuigi Rizzo update_addresses(pkt, targ->g); 1064f8e4e36aSLuigi Rizzo if (i > 10000) { 1065f8e4e36aSLuigi Rizzo targ->count = sent; 1066f8e4e36aSLuigi Rizzo i = 0; 1067f8e4e36aSLuigi Rizzo } 1068f8e4e36aSLuigi Rizzo } 1069f2637526SLuigi Rizzo #ifndef NO_PCAP 1070f2637526SLuigi Rizzo } else if (targ->g->dev_type == DEV_PCAP) { 1071f2637526SLuigi Rizzo pcap_t *p = targ->g->p; 1072f2637526SLuigi Rizzo 1073f2637526SLuigi Rizzo for (i = 0; !targ->cancel && (n == 0 || sent < n); i++) { 1074f2637526SLuigi Rizzo if (pcap_inject(p, frame, size) != -1) 1075f2637526SLuigi Rizzo sent++; 1076f2637526SLuigi Rizzo update_addresses(pkt, targ->g); 1077f2637526SLuigi Rizzo if (i > 10000) { 1078f2637526SLuigi Rizzo targ->count = sent; 1079f2637526SLuigi Rizzo i = 0; 1080f2637526SLuigi Rizzo } 1081f2637526SLuigi Rizzo } 1082f2637526SLuigi Rizzo #endif /* NO_PCAP */ 108368b8534bSLuigi Rizzo } else { 10841cb4c501SLuigi Rizzo int tosend = 0; 1085ce3ee1e7SLuigi Rizzo int frags = targ->g->frags; 1086ce3ee1e7SLuigi Rizzo 10874bf50f18SLuigi Rizzo nifp = targ->nmd->nifp; 1088f8e4e36aSLuigi Rizzo while (!targ->cancel && (n == 0 || sent < n)) { 108968b8534bSLuigi Rizzo 10901cb4c501SLuigi Rizzo if (rate_limit && tosend <= 0) { 10911cb4c501SLuigi Rizzo tosend = targ->g->burst; 109217885a7bSLuigi Rizzo nexttime = timespec_add(nexttime, targ->g->tx_period); 109317885a7bSLuigi Rizzo wait_time(nexttime); 10941cb4c501SLuigi Rizzo } 10951cb4c501SLuigi Rizzo 109668b8534bSLuigi Rizzo /* 109768b8534bSLuigi Rizzo * wait for available room in the send queue(s) 109868b8534bSLuigi Rizzo */ 1099f0ea3689SLuigi Rizzo if (poll(&pfd, 1, 2000) <= 0) { 11003fe77e68SEd Maste if (targ->cancel) 11013fe77e68SEd Maste break; 110217885a7bSLuigi Rizzo D("poll error/timeout on queue %d: %s", targ->me, 110317885a7bSLuigi Rizzo strerror(errno)); 1104f0ea3689SLuigi Rizzo // goto quit; 110517885a7bSLuigi Rizzo } 1106f0ea3689SLuigi Rizzo if (pfd.revents & POLLERR) { 110717885a7bSLuigi Rizzo D("poll error"); 110868b8534bSLuigi Rizzo goto quit; 110968b8534bSLuigi Rizzo } 111068b8534bSLuigi Rizzo /* 111168b8534bSLuigi Rizzo * scan our queues and send on those with room 111268b8534bSLuigi Rizzo */ 1113f8e4e36aSLuigi Rizzo if (options & OPT_COPY && sent > 100000 && !(targ->g->options & OPT_COPY) ) { 1114f8e4e36aSLuigi Rizzo D("drop copy"); 111599fb123fSLuigi Rizzo options &= ~OPT_COPY; 1116f8e4e36aSLuigi Rizzo } 1117f0ea3689SLuigi Rizzo for (i = targ->nmd->first_tx_ring; i <= targ->nmd->last_tx_ring; i++) { 11181cb4c501SLuigi Rizzo int m, limit = rate_limit ? tosend : targ->g->burst; 1119f8e4e36aSLuigi Rizzo if (n > 0 && n - sent < limit) 1120f8e4e36aSLuigi Rizzo limit = n - sent; 112168b8534bSLuigi Rizzo txring = NETMAP_TXRING(nifp, i); 112217885a7bSLuigi Rizzo if (nm_ring_empty(txring)) 112368b8534bSLuigi Rizzo continue; 1124ce3ee1e7SLuigi Rizzo if (frags > 1) 1125ce3ee1e7SLuigi Rizzo limit = ((limit + frags - 1) / frags) * frags; 1126ce3ee1e7SLuigi Rizzo 112717885a7bSLuigi Rizzo m = send_packets(txring, pkt, frame, size, targ->g, 1128ce3ee1e7SLuigi Rizzo limit, options, frags); 1129f2637526SLuigi Rizzo ND("limit %d tail %d frags %d m %d", 1130f2637526SLuigi Rizzo limit, txring->tail, frags, m); 113168b8534bSLuigi Rizzo sent += m; 113268b8534bSLuigi Rizzo targ->count = sent; 1133ce3ee1e7SLuigi Rizzo if (rate_limit) { 1134ce3ee1e7SLuigi Rizzo tosend -= m; 1135ce3ee1e7SLuigi Rizzo if (tosend <= 0) 1136ce3ee1e7SLuigi Rizzo break; 1137ce3ee1e7SLuigi Rizzo } 113868b8534bSLuigi Rizzo } 113968b8534bSLuigi Rizzo } 114099fb123fSLuigi Rizzo /* flush any remaining packets */ 11414bf50f18SLuigi Rizzo D("flush tail %d head %d on thread %p", 11424bf50f18SLuigi Rizzo txring->tail, txring->head, 11434bf50f18SLuigi Rizzo pthread_self()); 1144f0ea3689SLuigi Rizzo ioctl(pfd.fd, NIOCTXSYNC, NULL); 114568b8534bSLuigi Rizzo 114668b8534bSLuigi Rizzo /* final part: wait all the TX queues to be empty. */ 1147f0ea3689SLuigi Rizzo for (i = targ->nmd->first_tx_ring; i <= targ->nmd->last_tx_ring; i++) { 114868b8534bSLuigi Rizzo txring = NETMAP_TXRING(nifp, i); 114917885a7bSLuigi Rizzo while (nm_tx_pending(txring)) { 11504bf50f18SLuigi Rizzo RD(5, "pending tx tail %d head %d on ring %d", 11514bf50f18SLuigi Rizzo txring->tail, txring->head, i); 1152f0ea3689SLuigi Rizzo ioctl(pfd.fd, NIOCTXSYNC, NULL); 115368b8534bSLuigi Rizzo usleep(1); /* wait 1 tick */ 115468b8534bSLuigi Rizzo } 115568b8534bSLuigi Rizzo } 1156f2637526SLuigi Rizzo } /* end DEV_NETMAP */ 115768b8534bSLuigi Rizzo 11581cb4c501SLuigi Rizzo clock_gettime(CLOCK_REALTIME_PRECISE, &targ->toc); 115968b8534bSLuigi Rizzo targ->completed = 1; 116068b8534bSLuigi Rizzo targ->count = sent; 116168b8534bSLuigi Rizzo 116268b8534bSLuigi Rizzo quit: 116368b8534bSLuigi Rizzo /* reset the ``used`` flag. */ 116468b8534bSLuigi Rizzo targ->used = 0; 116568b8534bSLuigi Rizzo 116668b8534bSLuigi Rizzo return (NULL); 116768b8534bSLuigi Rizzo } 116868b8534bSLuigi Rizzo 116968b8534bSLuigi Rizzo 1170f2637526SLuigi Rizzo #ifndef NO_PCAP 117168b8534bSLuigi Rizzo static void 1172f8e4e36aSLuigi Rizzo receive_pcap(u_char *user, const struct pcap_pkthdr * h, 1173f8e4e36aSLuigi Rizzo const u_char * bytes) 117468b8534bSLuigi Rizzo { 117568b8534bSLuigi Rizzo int *count = (int *)user; 1176f8e4e36aSLuigi Rizzo (void)h; /* UNUSED */ 1177f8e4e36aSLuigi Rizzo (void)bytes; /* UNUSED */ 117868b8534bSLuigi Rizzo (*count)++; 117968b8534bSLuigi Rizzo } 1180f2637526SLuigi Rizzo #endif /* !NO_PCAP */ 118168b8534bSLuigi Rizzo 118268b8534bSLuigi Rizzo static int 1183b303f675SLuigi Rizzo receive_packets(struct netmap_ring *ring, u_int limit, int dump) 118468b8534bSLuigi Rizzo { 118517885a7bSLuigi Rizzo u_int cur, rx, n; 118668b8534bSLuigi Rizzo 118768b8534bSLuigi Rizzo cur = ring->cur; 118817885a7bSLuigi Rizzo n = nm_ring_space(ring); 118917885a7bSLuigi Rizzo if (n < limit) 119017885a7bSLuigi Rizzo limit = n; 119168b8534bSLuigi Rizzo for (rx = 0; rx < limit; rx++) { 119268b8534bSLuigi Rizzo struct netmap_slot *slot = &ring->slot[cur]; 119368b8534bSLuigi Rizzo char *p = NETMAP_BUF(ring, slot->buf_idx); 119468b8534bSLuigi Rizzo 1195b303f675SLuigi Rizzo if (dump) 1196b303f675SLuigi Rizzo dump_payload(p, slot->len, ring, cur); 119768b8534bSLuigi Rizzo 119817885a7bSLuigi Rizzo cur = nm_ring_next(ring, cur); 119968b8534bSLuigi Rizzo } 120017885a7bSLuigi Rizzo ring->head = ring->cur = cur; 120168b8534bSLuigi Rizzo 120268b8534bSLuigi Rizzo return (rx); 120368b8534bSLuigi Rizzo } 120468b8534bSLuigi Rizzo 120568b8534bSLuigi Rizzo static void * 120668b8534bSLuigi Rizzo receiver_body(void *data) 120768b8534bSLuigi Rizzo { 120868b8534bSLuigi Rizzo struct targ *targ = (struct targ *) data; 1209f0ea3689SLuigi Rizzo struct pollfd pfd = { .fd = targ->fd, .events = POLLIN }; 12104bf50f18SLuigi Rizzo struct netmap_if *nifp; 121168b8534bSLuigi Rizzo struct netmap_ring *rxring; 1212f8e4e36aSLuigi Rizzo int i; 1213f8e4e36aSLuigi Rizzo uint64_t received = 0; 121468b8534bSLuigi Rizzo 121568b8534bSLuigi Rizzo if (setaffinity(targ->thread, targ->affinity)) 121668b8534bSLuigi Rizzo goto quit; 121768b8534bSLuigi Rizzo 12184bf50f18SLuigi Rizzo D("reading from %s fd %d main_fd %d", 12194bf50f18SLuigi Rizzo targ->g->ifname, targ->fd, targ->g->main_fd); 122068b8534bSLuigi Rizzo /* unbounded wait for the first packet. */ 12214bf50f18SLuigi Rizzo for (;!targ->cancel;) { 1222f0ea3689SLuigi Rizzo i = poll(&pfd, 1, 1000); 1223f0ea3689SLuigi Rizzo if (i > 0 && !(pfd.revents & POLLERR)) 122468b8534bSLuigi Rizzo break; 1225f0ea3689SLuigi Rizzo RD(1, "waiting for initial packets, poll returns %d %d", 1226f0ea3689SLuigi Rizzo i, pfd.revents); 122768b8534bSLuigi Rizzo } 122868b8534bSLuigi Rizzo /* main loop, exit after 1s silence */ 12291cb4c501SLuigi Rizzo clock_gettime(CLOCK_REALTIME_PRECISE, &targ->tic); 1230f2637526SLuigi Rizzo if (targ->g->dev_type == DEV_TAP) { 1231f8e4e36aSLuigi Rizzo while (!targ->cancel) { 12324bf50f18SLuigi Rizzo char buf[MAX_BODYSIZE]; 1233f8e4e36aSLuigi Rizzo /* XXX should we poll ? */ 1234f8e4e36aSLuigi Rizzo if (read(targ->g->main_fd, buf, sizeof(buf)) > 0) 1235f8e4e36aSLuigi Rizzo targ->count++; 1236f8e4e36aSLuigi Rizzo } 1237f2637526SLuigi Rizzo #ifndef NO_PCAP 1238f2637526SLuigi Rizzo } else if (targ->g->dev_type == DEV_PCAP) { 1239f2637526SLuigi Rizzo while (!targ->cancel) { 1240f2637526SLuigi Rizzo /* XXX should we poll ? */ 12414bf50f18SLuigi Rizzo pcap_dispatch(targ->g->p, targ->g->burst, receive_pcap, 12424bf50f18SLuigi Rizzo (u_char *)&targ->count); 1243f2637526SLuigi Rizzo } 1244f2637526SLuigi Rizzo #endif /* !NO_PCAP */ 124568b8534bSLuigi Rizzo } else { 1246b303f675SLuigi Rizzo int dump = targ->g->options & OPT_DUMP; 12474bf50f18SLuigi Rizzo 12484bf50f18SLuigi Rizzo nifp = targ->nmd->nifp; 12493fe77e68SEd Maste while (!targ->cancel) { 125068b8534bSLuigi Rizzo /* Once we started to receive packets, wait at most 1 seconds 125168b8534bSLuigi Rizzo before quitting. */ 1252f0ea3689SLuigi Rizzo if (poll(&pfd, 1, 1 * 1000) <= 0 && !targ->g->forever) { 12531cb4c501SLuigi Rizzo clock_gettime(CLOCK_REALTIME_PRECISE, &targ->toc); 12548ce070c1SUlrich Spörlein targ->toc.tv_sec -= 1; /* Subtract timeout time. */ 1255f0ea3689SLuigi Rizzo goto out; 125668b8534bSLuigi Rizzo } 125768b8534bSLuigi Rizzo 1258f0ea3689SLuigi Rizzo if (pfd.revents & POLLERR) { 125917885a7bSLuigi Rizzo D("poll err"); 126017885a7bSLuigi Rizzo goto quit; 126117885a7bSLuigi Rizzo } 126217885a7bSLuigi Rizzo 1263f0ea3689SLuigi Rizzo for (i = targ->nmd->first_rx_ring; i <= targ->nmd->last_rx_ring; i++) { 126468b8534bSLuigi Rizzo int m; 126568b8534bSLuigi Rizzo 126668b8534bSLuigi Rizzo rxring = NETMAP_RXRING(nifp, i); 126717885a7bSLuigi Rizzo if (nm_ring_empty(rxring)) 126868b8534bSLuigi Rizzo continue; 126968b8534bSLuigi Rizzo 1270b303f675SLuigi Rizzo m = receive_packets(rxring, targ->g->burst, dump); 127168b8534bSLuigi Rizzo received += m; 127268b8534bSLuigi Rizzo } 1273f8e4e36aSLuigi Rizzo targ->count = received; 127468b8534bSLuigi Rizzo } 127568b8534bSLuigi Rizzo } 127668b8534bSLuigi Rizzo 1277f0ea3689SLuigi Rizzo clock_gettime(CLOCK_REALTIME_PRECISE, &targ->toc); 1278f0ea3689SLuigi Rizzo 1279f0ea3689SLuigi Rizzo out: 128068b8534bSLuigi Rizzo targ->completed = 1; 128168b8534bSLuigi Rizzo targ->count = received; 128268b8534bSLuigi Rizzo 128368b8534bSLuigi Rizzo quit: 128468b8534bSLuigi Rizzo /* reset the ``used`` flag. */ 128568b8534bSLuigi Rizzo targ->used = 0; 128668b8534bSLuigi Rizzo 128768b8534bSLuigi Rizzo return (NULL); 128868b8534bSLuigi Rizzo } 128968b8534bSLuigi Rizzo 1290f8e4e36aSLuigi Rizzo /* very crude code to print a number in normalized form. 1291f8e4e36aSLuigi Rizzo * Caller has to make sure that the buffer is large enough. 1292f8e4e36aSLuigi Rizzo */ 1293f8e4e36aSLuigi Rizzo static const char * 1294f8e4e36aSLuigi Rizzo norm(char *buf, double val) 129566a698c9SEd Maste { 1296f0ea3689SLuigi Rizzo char *units[] = { "", "K", "M", "G", "T" }; 1297f8e4e36aSLuigi Rizzo u_int i; 129866a698c9SEd Maste 1299f0ea3689SLuigi Rizzo for (i = 0; val >=1000 && i < sizeof(units)/sizeof(char *) - 1; i++) 130066a698c9SEd Maste val /= 1000; 1301f8e4e36aSLuigi Rizzo sprintf(buf, "%.2f %s", val, units[i]); 1302f8e4e36aSLuigi Rizzo return buf; 130366a698c9SEd Maste } 130466a698c9SEd Maste 130568b8534bSLuigi Rizzo static void 130668b8534bSLuigi Rizzo tx_output(uint64_t sent, int size, double delta) 130768b8534bSLuigi Rizzo { 1308f8e4e36aSLuigi Rizzo double bw, raw_bw, pps; 1309f8e4e36aSLuigi Rizzo char b1[40], b2[80], b3[80]; 131068b8534bSLuigi Rizzo 1311f0ea3689SLuigi Rizzo printf("Sent %llu packets, %d bytes each, in %.2f seconds.\n", 1312f0ea3689SLuigi Rizzo (unsigned long long)sent, size, delta); 1313f8e4e36aSLuigi Rizzo if (delta == 0) 1314f8e4e36aSLuigi Rizzo delta = 1e-6; 1315f8e4e36aSLuigi Rizzo if (size < 60) /* correct for min packet size */ 1316f8e4e36aSLuigi Rizzo size = 60; 1317f8e4e36aSLuigi Rizzo pps = sent / delta; 1318f8e4e36aSLuigi Rizzo bw = (8.0 * size * sent) / delta; 1319f8e4e36aSLuigi Rizzo /* raw packets have4 bytes crc + 20 bytes framing */ 1320f8e4e36aSLuigi Rizzo raw_bw = (8.0 * (size + 24) * sent) / delta; 132166a698c9SEd Maste 1322f8e4e36aSLuigi Rizzo printf("Speed: %spps Bandwidth: %sbps (raw %sbps)\n", 1323f8e4e36aSLuigi Rizzo norm(b1, pps), norm(b2, bw), norm(b3, raw_bw) ); 132468b8534bSLuigi Rizzo } 132568b8534bSLuigi Rizzo 132668b8534bSLuigi Rizzo 132768b8534bSLuigi Rizzo static void 132868b8534bSLuigi Rizzo rx_output(uint64_t received, double delta) 132968b8534bSLuigi Rizzo { 1330f8e4e36aSLuigi Rizzo double pps; 1331f8e4e36aSLuigi Rizzo char b1[40]; 133268b8534bSLuigi Rizzo 1333f0ea3689SLuigi Rizzo printf("Received %llu packets, in %.2f seconds.\n", 1334f0ea3689SLuigi Rizzo (unsigned long long) received, delta); 1335f8e4e36aSLuigi Rizzo 1336f8e4e36aSLuigi Rizzo if (delta == 0) 1337f8e4e36aSLuigi Rizzo delta = 1e-6; 1338f8e4e36aSLuigi Rizzo pps = received / delta; 1339f8e4e36aSLuigi Rizzo printf("Speed: %spps\n", norm(b1, pps)); 134068b8534bSLuigi Rizzo } 134168b8534bSLuigi Rizzo 134268b8534bSLuigi Rizzo static void 134368b8534bSLuigi Rizzo usage(void) 134468b8534bSLuigi Rizzo { 134568b8534bSLuigi Rizzo const char *cmd = "pkt-gen"; 134668b8534bSLuigi Rizzo fprintf(stderr, 134768b8534bSLuigi Rizzo "Usage:\n" 134868b8534bSLuigi Rizzo "%s arguments\n" 134968b8534bSLuigi Rizzo "\t-i interface interface name\n" 1350f8e4e36aSLuigi Rizzo "\t-f function tx rx ping pong\n" 1351f8e4e36aSLuigi Rizzo "\t-n count number of iterations (can be 0)\n" 1352f8e4e36aSLuigi Rizzo "\t-t pkts_to_send also forces tx mode\n" 1353f8e4e36aSLuigi Rizzo "\t-r pkts_to_receive also forces rx mode\n" 1354ce3ee1e7SLuigi Rizzo "\t-l pkt_size in bytes excluding CRC\n" 1355ce3ee1e7SLuigi Rizzo "\t-d dst_ip[:port[-dst_ip:port]] single or range\n" 1356ce3ee1e7SLuigi Rizzo "\t-s src_ip[:port[-src_ip:port]] single or range\n" 1357ce3ee1e7SLuigi Rizzo "\t-D dst-mac\n" 1358ce3ee1e7SLuigi Rizzo "\t-S src-mac\n" 1359f8e4e36aSLuigi Rizzo "\t-a cpu_id use setaffinity\n" 136068b8534bSLuigi Rizzo "\t-b burst size testing, mostly\n" 136168b8534bSLuigi Rizzo "\t-c cores cores to use\n" 136268b8534bSLuigi Rizzo "\t-p threads processes/threads to use\n" 136368b8534bSLuigi Rizzo "\t-T report_ms milliseconds between reports\n" 1364f8e4e36aSLuigi Rizzo "\t-P use libpcap instead of netmap\n" 136568b8534bSLuigi Rizzo "\t-w wait_for_link_time in seconds\n" 1366ce3ee1e7SLuigi Rizzo "\t-R rate in packets per second\n" 1367ce3ee1e7SLuigi Rizzo "\t-X dump payload\n" 136817885a7bSLuigi Rizzo "\t-H len add empty virtio-net-header with size 'len'\n" 136968b8534bSLuigi Rizzo "", 137068b8534bSLuigi Rizzo cmd); 137168b8534bSLuigi Rizzo 137268b8534bSLuigi Rizzo exit(0); 137368b8534bSLuigi Rizzo } 137468b8534bSLuigi Rizzo 1375f8e4e36aSLuigi Rizzo static void 1376f8e4e36aSLuigi Rizzo start_threads(struct glob_arg *g) 1377f8e4e36aSLuigi Rizzo { 1378f8e4e36aSLuigi Rizzo int i; 1379f8e4e36aSLuigi Rizzo 1380f8e4e36aSLuigi Rizzo targs = calloc(g->nthreads, sizeof(*targs)); 1381f8e4e36aSLuigi Rizzo /* 1382f8e4e36aSLuigi Rizzo * Now create the desired number of threads, each one 1383f8e4e36aSLuigi Rizzo * using a single descriptor. 1384f8e4e36aSLuigi Rizzo */ 1385f8e4e36aSLuigi Rizzo for (i = 0; i < g->nthreads; i++) { 1386f0ea3689SLuigi Rizzo struct targ *t = &targs[i]; 1387f0ea3689SLuigi Rizzo 1388f0ea3689SLuigi Rizzo bzero(t, sizeof(*t)); 1389f0ea3689SLuigi Rizzo t->fd = -1; /* default, with pcap */ 1390f0ea3689SLuigi Rizzo t->g = g; 1391f8e4e36aSLuigi Rizzo 1392f8e4e36aSLuigi Rizzo if (g->dev_type == DEV_NETMAP) { 1393f0ea3689SLuigi Rizzo struct nm_desc nmd = *g->nmd; /* copy, we overwrite ringid */ 13944bf50f18SLuigi Rizzo uint64_t nmd_flags = 0; 13954bf50f18SLuigi Rizzo nmd.self = &nmd; 1396f8e4e36aSLuigi Rizzo 1397f0ea3689SLuigi Rizzo if (g->nthreads > 1) { 1398f0ea3689SLuigi Rizzo if (nmd.req.nr_flags != NR_REG_ALL_NIC) { 1399f0ea3689SLuigi Rizzo D("invalid nthreads mode %d", nmd.req.nr_flags); 1400f8e4e36aSLuigi Rizzo continue; 1401f8e4e36aSLuigi Rizzo } 1402f0ea3689SLuigi Rizzo nmd.req.nr_flags = NR_REG_ONE_NIC; 1403f0ea3689SLuigi Rizzo nmd.req.nr_ringid = i; 140417885a7bSLuigi Rizzo } 1405f0ea3689SLuigi Rizzo /* Only touch one of the rings (rx is already ok) */ 1406f0ea3689SLuigi Rizzo if (g->td_body == receiver_body) 14074bf50f18SLuigi Rizzo nmd_flags |= NETMAP_NO_TX_POLL; 1408f8e4e36aSLuigi Rizzo 1409f0ea3689SLuigi Rizzo /* register interface. Override ifname and ringid etc. */ 14104bf50f18SLuigi Rizzo if (g->options & OPT_MONITOR_TX) 14114bf50f18SLuigi Rizzo nmd.req.nr_flags |= NR_MONITOR_TX; 14124bf50f18SLuigi Rizzo if (g->options & OPT_MONITOR_RX) 14134bf50f18SLuigi Rizzo nmd.req.nr_flags |= NR_MONITOR_RX; 1414f8e4e36aSLuigi Rizzo 14154bf50f18SLuigi Rizzo t->nmd = nm_open(t->g->ifname, NULL, nmd_flags | 14164bf50f18SLuigi Rizzo NM_OPEN_IFNAME | NM_OPEN_NO_MMAP, &nmd); 1417f0ea3689SLuigi Rizzo if (t->nmd == NULL) { 1418f0ea3689SLuigi Rizzo D("Unable to open %s: %s", 1419f0ea3689SLuigi Rizzo t->g->ifname, strerror(errno)); 1420f8e4e36aSLuigi Rizzo continue; 1421f8e4e36aSLuigi Rizzo } 1422f0ea3689SLuigi Rizzo t->fd = t->nmd->fd; 14234bf50f18SLuigi Rizzo set_vnet_hdr_len(t); 1424f0ea3689SLuigi Rizzo 1425f8e4e36aSLuigi Rizzo } else { 1426f8e4e36aSLuigi Rizzo targs[i].fd = g->main_fd; 1427f8e4e36aSLuigi Rizzo } 1428f0ea3689SLuigi Rizzo t->used = 1; 1429f0ea3689SLuigi Rizzo t->me = i; 1430f8e4e36aSLuigi Rizzo if (g->affinity >= 0) { 1431f8e4e36aSLuigi Rizzo if (g->affinity < g->cpus) 1432f0ea3689SLuigi Rizzo t->affinity = g->affinity; 1433f8e4e36aSLuigi Rizzo else 1434f0ea3689SLuigi Rizzo t->affinity = i % g->cpus; 1435f0ea3689SLuigi Rizzo } else { 1436f0ea3689SLuigi Rizzo t->affinity = -1; 1437f0ea3689SLuigi Rizzo } 1438f8e4e36aSLuigi Rizzo /* default, init packets */ 1439f0ea3689SLuigi Rizzo initialize_packet(t); 1440f8e4e36aSLuigi Rizzo 1441f0ea3689SLuigi Rizzo if (pthread_create(&t->thread, NULL, g->td_body, t) == -1) { 144217885a7bSLuigi Rizzo D("Unable to create thread %d: %s", i, strerror(errno)); 1443f0ea3689SLuigi Rizzo t->used = 0; 1444f8e4e36aSLuigi Rizzo } 1445f8e4e36aSLuigi Rizzo } 1446f8e4e36aSLuigi Rizzo } 1447f8e4e36aSLuigi Rizzo 1448f8e4e36aSLuigi Rizzo static void 1449f8e4e36aSLuigi Rizzo main_thread(struct glob_arg *g) 1450f8e4e36aSLuigi Rizzo { 1451f8e4e36aSLuigi Rizzo int i; 1452f8e4e36aSLuigi Rizzo 1453f8e4e36aSLuigi Rizzo uint64_t prev = 0; 1454f8e4e36aSLuigi Rizzo uint64_t count = 0; 1455f8e4e36aSLuigi Rizzo double delta_t; 1456f8e4e36aSLuigi Rizzo struct timeval tic, toc; 1457f8e4e36aSLuigi Rizzo 1458f8e4e36aSLuigi Rizzo gettimeofday(&toc, NULL); 1459f8e4e36aSLuigi Rizzo for (;;) { 1460f8e4e36aSLuigi Rizzo struct timeval now, delta; 1461f8e4e36aSLuigi Rizzo uint64_t pps, usec, my_count, npkts; 1462f8e4e36aSLuigi Rizzo int done = 0; 1463f8e4e36aSLuigi Rizzo 1464f8e4e36aSLuigi Rizzo delta.tv_sec = g->report_interval/1000; 1465f8e4e36aSLuigi Rizzo delta.tv_usec = (g->report_interval%1000)*1000; 1466f8e4e36aSLuigi Rizzo select(0, NULL, NULL, NULL, &delta); 1467f8e4e36aSLuigi Rizzo gettimeofday(&now, NULL); 1468f8e4e36aSLuigi Rizzo timersub(&now, &toc, &toc); 1469f8e4e36aSLuigi Rizzo my_count = 0; 1470f8e4e36aSLuigi Rizzo for (i = 0; i < g->nthreads; i++) { 1471f8e4e36aSLuigi Rizzo my_count += targs[i].count; 1472f8e4e36aSLuigi Rizzo if (targs[i].used == 0) 1473f8e4e36aSLuigi Rizzo done++; 1474f8e4e36aSLuigi Rizzo } 1475f8e4e36aSLuigi Rizzo usec = toc.tv_sec* 1000000 + toc.tv_usec; 1476f8e4e36aSLuigi Rizzo if (usec < 10000) 1477f8e4e36aSLuigi Rizzo continue; 1478f8e4e36aSLuigi Rizzo npkts = my_count - prev; 1479f8e4e36aSLuigi Rizzo pps = (npkts*1000000 + usec/2) / usec; 1480f0ea3689SLuigi Rizzo D("%llu pps (%llu pkts in %llu usec)", 1481f0ea3689SLuigi Rizzo (unsigned long long)pps, 1482f0ea3689SLuigi Rizzo (unsigned long long)npkts, 1483f0ea3689SLuigi Rizzo (unsigned long long)usec); 1484f8e4e36aSLuigi Rizzo prev = my_count; 1485f8e4e36aSLuigi Rizzo toc = now; 1486f8e4e36aSLuigi Rizzo if (done == g->nthreads) 1487f8e4e36aSLuigi Rizzo break; 1488f8e4e36aSLuigi Rizzo } 1489f8e4e36aSLuigi Rizzo 1490f8e4e36aSLuigi Rizzo timerclear(&tic); 1491f8e4e36aSLuigi Rizzo timerclear(&toc); 1492f8e4e36aSLuigi Rizzo for (i = 0; i < g->nthreads; i++) { 14931cb4c501SLuigi Rizzo struct timespec t_tic, t_toc; 1494f8e4e36aSLuigi Rizzo /* 1495f8e4e36aSLuigi Rizzo * Join active threads, unregister interfaces and close 1496f8e4e36aSLuigi Rizzo * file descriptors. 1497f8e4e36aSLuigi Rizzo */ 14981cb4c501SLuigi Rizzo if (targs[i].used) 1499f8e4e36aSLuigi Rizzo pthread_join(targs[i].thread, NULL); 1500f8e4e36aSLuigi Rizzo close(targs[i].fd); 1501f8e4e36aSLuigi Rizzo 1502f8e4e36aSLuigi Rizzo if (targs[i].completed == 0) 1503f8e4e36aSLuigi Rizzo D("ouch, thread %d exited with error", i); 1504f8e4e36aSLuigi Rizzo 1505f8e4e36aSLuigi Rizzo /* 1506f8e4e36aSLuigi Rizzo * Collect threads output and extract information about 1507f8e4e36aSLuigi Rizzo * how long it took to send all the packets. 1508f8e4e36aSLuigi Rizzo */ 1509f8e4e36aSLuigi Rizzo count += targs[i].count; 15101cb4c501SLuigi Rizzo t_tic = timeval2spec(&tic); 15111cb4c501SLuigi Rizzo t_toc = timeval2spec(&toc); 15121cb4c501SLuigi Rizzo if (!timerisset(&tic) || timespec_ge(&targs[i].tic, &t_tic)) 15131cb4c501SLuigi Rizzo tic = timespec2val(&targs[i].tic); 15141cb4c501SLuigi Rizzo if (!timerisset(&toc) || timespec_ge(&targs[i].toc, &t_toc)) 15151cb4c501SLuigi Rizzo toc = timespec2val(&targs[i].toc); 1516f8e4e36aSLuigi Rizzo } 1517f8e4e36aSLuigi Rizzo 1518f8e4e36aSLuigi Rizzo /* print output. */ 1519f8e4e36aSLuigi Rizzo timersub(&toc, &tic, &toc); 1520f8e4e36aSLuigi Rizzo delta_t = toc.tv_sec + 1e-6* toc.tv_usec; 1521f8e4e36aSLuigi Rizzo if (g->td_body == sender_body) 1522f8e4e36aSLuigi Rizzo tx_output(count, g->pkt_size, delta_t); 1523f8e4e36aSLuigi Rizzo else 1524f8e4e36aSLuigi Rizzo rx_output(count, delta_t); 1525f8e4e36aSLuigi Rizzo 1526f8e4e36aSLuigi Rizzo if (g->dev_type == DEV_NETMAP) { 1527f0ea3689SLuigi Rizzo munmap(g->nmd->mem, g->nmd->req.nr_memsize); 1528f8e4e36aSLuigi Rizzo close(g->main_fd); 1529f8e4e36aSLuigi Rizzo } 1530f8e4e36aSLuigi Rizzo } 1531f8e4e36aSLuigi Rizzo 1532f8e4e36aSLuigi Rizzo 1533f8e4e36aSLuigi Rizzo struct sf { 1534f8e4e36aSLuigi Rizzo char *key; 1535f8e4e36aSLuigi Rizzo void *f; 1536f8e4e36aSLuigi Rizzo }; 1537f8e4e36aSLuigi Rizzo 1538f8e4e36aSLuigi Rizzo static struct sf func[] = { 1539f8e4e36aSLuigi Rizzo { "tx", sender_body }, 1540f8e4e36aSLuigi Rizzo { "rx", receiver_body }, 1541f8e4e36aSLuigi Rizzo { "ping", pinger_body }, 1542f8e4e36aSLuigi Rizzo { "pong", ponger_body }, 1543f8e4e36aSLuigi Rizzo { NULL, NULL } 1544f8e4e36aSLuigi Rizzo }; 1545f8e4e36aSLuigi Rizzo 1546f8e4e36aSLuigi Rizzo static int 1547f8e4e36aSLuigi Rizzo tap_alloc(char *dev) 1548f8e4e36aSLuigi Rizzo { 1549f8e4e36aSLuigi Rizzo struct ifreq ifr; 1550f8e4e36aSLuigi Rizzo int fd, err; 1551f8e4e36aSLuigi Rizzo char *clonedev = TAP_CLONEDEV; 1552f8e4e36aSLuigi Rizzo 1553f8e4e36aSLuigi Rizzo (void)err; 1554f8e4e36aSLuigi Rizzo (void)dev; 1555f8e4e36aSLuigi Rizzo /* Arguments taken by the function: 1556f8e4e36aSLuigi Rizzo * 1557f8e4e36aSLuigi Rizzo * char *dev: the name of an interface (or '\0'). MUST have enough 1558f8e4e36aSLuigi Rizzo * space to hold the interface name if '\0' is passed 1559f8e4e36aSLuigi Rizzo * int flags: interface flags (eg, IFF_TUN etc.) 1560f8e4e36aSLuigi Rizzo */ 1561f8e4e36aSLuigi Rizzo 1562f8e4e36aSLuigi Rizzo #ifdef __FreeBSD__ 1563f8e4e36aSLuigi Rizzo if (dev[3]) { /* tapSomething */ 1564f8e4e36aSLuigi Rizzo static char buf[128]; 1565f8e4e36aSLuigi Rizzo snprintf(buf, sizeof(buf), "/dev/%s", dev); 1566f8e4e36aSLuigi Rizzo clonedev = buf; 1567f8e4e36aSLuigi Rizzo } 1568f8e4e36aSLuigi Rizzo #endif 1569f8e4e36aSLuigi Rizzo /* open the device */ 1570f8e4e36aSLuigi Rizzo if( (fd = open(clonedev, O_RDWR)) < 0 ) { 1571f8e4e36aSLuigi Rizzo return fd; 1572f8e4e36aSLuigi Rizzo } 1573f8e4e36aSLuigi Rizzo D("%s open successful", clonedev); 1574f8e4e36aSLuigi Rizzo 1575f8e4e36aSLuigi Rizzo /* preparation of the struct ifr, of type "struct ifreq" */ 1576f8e4e36aSLuigi Rizzo memset(&ifr, 0, sizeof(ifr)); 1577f8e4e36aSLuigi Rizzo 1578f8e4e36aSLuigi Rizzo #ifdef linux 1579f8e4e36aSLuigi Rizzo ifr.ifr_flags = IFF_TAP | IFF_NO_PI; 1580f8e4e36aSLuigi Rizzo 1581f8e4e36aSLuigi Rizzo if (*dev) { 1582f8e4e36aSLuigi Rizzo /* if a device name was specified, put it in the structure; otherwise, 1583f8e4e36aSLuigi Rizzo * the kernel will try to allocate the "next" device of the 1584f8e4e36aSLuigi Rizzo * specified type */ 1585f8e4e36aSLuigi Rizzo strncpy(ifr.ifr_name, dev, IFNAMSIZ); 1586f8e4e36aSLuigi Rizzo } 1587f8e4e36aSLuigi Rizzo 1588f8e4e36aSLuigi Rizzo /* try to create the device */ 1589f8e4e36aSLuigi Rizzo if( (err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0 ) { 159017885a7bSLuigi Rizzo D("failed to to a TUNSETIFF: %s", strerror(errno)); 1591f8e4e36aSLuigi Rizzo close(fd); 1592f8e4e36aSLuigi Rizzo return err; 1593f8e4e36aSLuigi Rizzo } 1594f8e4e36aSLuigi Rizzo 1595f8e4e36aSLuigi Rizzo /* if the operation was successful, write back the name of the 1596f8e4e36aSLuigi Rizzo * interface to the variable "dev", so the caller can know 1597f8e4e36aSLuigi Rizzo * it. Note that the caller MUST reserve space in *dev (see calling 1598f8e4e36aSLuigi Rizzo * code below) */ 1599f8e4e36aSLuigi Rizzo strcpy(dev, ifr.ifr_name); 1600f8e4e36aSLuigi Rizzo D("new name is %s", dev); 1601f8e4e36aSLuigi Rizzo #endif /* linux */ 1602f8e4e36aSLuigi Rizzo 1603f8e4e36aSLuigi Rizzo /* this is the special file descriptor that the caller will use to talk 1604f8e4e36aSLuigi Rizzo * with the virtual interface */ 1605f8e4e36aSLuigi Rizzo return fd; 1606f8e4e36aSLuigi Rizzo } 160768b8534bSLuigi Rizzo 160868b8534bSLuigi Rizzo int 160968b8534bSLuigi Rizzo main(int arc, char **argv) 161068b8534bSLuigi Rizzo { 1611f8e4e36aSLuigi Rizzo int i; 161268b8534bSLuigi Rizzo 161368b8534bSLuigi Rizzo struct glob_arg g; 161468b8534bSLuigi Rizzo 161568b8534bSLuigi Rizzo int ch; 161668b8534bSLuigi Rizzo int wait_link = 2; 161768b8534bSLuigi Rizzo int devqueues = 1; /* how many device queues */ 161868b8534bSLuigi Rizzo 161968b8534bSLuigi Rizzo bzero(&g, sizeof(g)); 162068b8534bSLuigi Rizzo 1621f8e4e36aSLuigi Rizzo g.main_fd = -1; 1622f8e4e36aSLuigi Rizzo g.td_body = receiver_body; 1623f8e4e36aSLuigi Rizzo g.report_interval = 1000; /* report interval */ 1624f8e4e36aSLuigi Rizzo g.affinity = -1; 1625f8e4e36aSLuigi Rizzo /* ip addresses can also be a range x.x.x.x-x.x.x.y */ 1626f8e4e36aSLuigi Rizzo g.src_ip.name = "10.0.0.1"; 1627f8e4e36aSLuigi Rizzo g.dst_ip.name = "10.1.0.1"; 1628f8e4e36aSLuigi Rizzo g.dst_mac.name = "ff:ff:ff:ff:ff:ff"; 1629f8e4e36aSLuigi Rizzo g.src_mac.name = NULL; 163068b8534bSLuigi Rizzo g.pkt_size = 60; 163168b8534bSLuigi Rizzo g.burst = 512; // default 163268b8534bSLuigi Rizzo g.nthreads = 1; 163368b8534bSLuigi Rizzo g.cpus = 1; 1634b303f675SLuigi Rizzo g.forever = 1; 16351cb4c501SLuigi Rizzo g.tx_rate = 0; 1636ce3ee1e7SLuigi Rizzo g.frags = 1; 1637ce3ee1e7SLuigi Rizzo g.nmr_config = ""; 163817885a7bSLuigi Rizzo g.virt_header = 0; 163968b8534bSLuigi Rizzo 164068b8534bSLuigi Rizzo while ( (ch = getopt(arc, argv, 16414bf50f18SLuigi Rizzo "a:f:F:n:i:Il:d:s:D:S:b:c:o:p:T:w:WvR:XC:H:e:m:")) != -1) { 1642f8e4e36aSLuigi Rizzo struct sf *fn; 1643f8e4e36aSLuigi Rizzo 164468b8534bSLuigi Rizzo switch(ch) { 164568b8534bSLuigi Rizzo default: 164668b8534bSLuigi Rizzo D("bad option %c %s", ch, optarg); 164768b8534bSLuigi Rizzo usage(); 164868b8534bSLuigi Rizzo break; 1649f8e4e36aSLuigi Rizzo 1650f8e4e36aSLuigi Rizzo case 'n': 1651f8e4e36aSLuigi Rizzo g.npackets = atoi(optarg); 1652f8e4e36aSLuigi Rizzo break; 1653f8e4e36aSLuigi Rizzo 1654ce3ee1e7SLuigi Rizzo case 'F': 1655ce3ee1e7SLuigi Rizzo i = atoi(optarg); 1656ce3ee1e7SLuigi Rizzo if (i < 1 || i > 63) { 1657ce3ee1e7SLuigi Rizzo D("invalid frags %d [1..63], ignore", i); 1658ce3ee1e7SLuigi Rizzo break; 1659ce3ee1e7SLuigi Rizzo } 1660ce3ee1e7SLuigi Rizzo g.frags = i; 1661ce3ee1e7SLuigi Rizzo break; 1662ce3ee1e7SLuigi Rizzo 1663f8e4e36aSLuigi Rizzo case 'f': 1664f8e4e36aSLuigi Rizzo for (fn = func; fn->key; fn++) { 1665f8e4e36aSLuigi Rizzo if (!strcmp(fn->key, optarg)) 1666f8e4e36aSLuigi Rizzo break; 1667f8e4e36aSLuigi Rizzo } 1668f8e4e36aSLuigi Rizzo if (fn->key) 1669f8e4e36aSLuigi Rizzo g.td_body = fn->f; 1670f8e4e36aSLuigi Rizzo else 1671f8e4e36aSLuigi Rizzo D("unrecognised function %s", optarg); 1672f8e4e36aSLuigi Rizzo break; 1673f8e4e36aSLuigi Rizzo 1674f8e4e36aSLuigi Rizzo case 'o': /* data generation options */ 167599fb123fSLuigi Rizzo g.options = atoi(optarg); 167699fb123fSLuigi Rizzo break; 1677f8e4e36aSLuigi Rizzo 1678f8e4e36aSLuigi Rizzo case 'a': /* force affinity */ 1679f8e4e36aSLuigi Rizzo g.affinity = atoi(optarg); 1680f8e4e36aSLuigi Rizzo break; 1681f8e4e36aSLuigi Rizzo 168268b8534bSLuigi Rizzo case 'i': /* interface */ 1683f2637526SLuigi Rizzo /* a prefix of tap: netmap: or pcap: forces the mode. 1684f2637526SLuigi Rizzo * otherwise we guess 1685f2637526SLuigi Rizzo */ 1686f2637526SLuigi Rizzo D("interface is %s", optarg); 1687f0ea3689SLuigi Rizzo if (strlen(optarg) > MAX_IFNAMELEN - 8) { 1688f0ea3689SLuigi Rizzo D("ifname too long %s", optarg); 1689f0ea3689SLuigi Rizzo break; 1690f0ea3689SLuigi Rizzo } 1691f0ea3689SLuigi Rizzo strcpy(g.ifname, optarg); 1692f2637526SLuigi Rizzo if (!strcmp(optarg, "null")) { 1693f8e4e36aSLuigi Rizzo g.dev_type = DEV_NETMAP; 1694ce3ee1e7SLuigi Rizzo g.dummy_send = 1; 1695f2637526SLuigi Rizzo } else if (!strncmp(optarg, "tap:", 4)) { 1696f2637526SLuigi Rizzo g.dev_type = DEV_TAP; 1697f0ea3689SLuigi Rizzo strcpy(g.ifname, optarg + 4); 1698f2637526SLuigi Rizzo } else if (!strncmp(optarg, "pcap:", 5)) { 1699f2637526SLuigi Rizzo g.dev_type = DEV_PCAP; 1700f0ea3689SLuigi Rizzo strcpy(g.ifname, optarg + 5); 1701f0ea3689SLuigi Rizzo } else if (!strncmp(optarg, "netmap:", 7) || 1702f0ea3689SLuigi Rizzo !strncmp(optarg, "vale", 4)) { 1703f2637526SLuigi Rizzo g.dev_type = DEV_NETMAP; 1704f2637526SLuigi Rizzo } else if (!strncmp(optarg, "tap", 3)) { 1705f2637526SLuigi Rizzo g.dev_type = DEV_TAP; 1706f0ea3689SLuigi Rizzo } else { /* prepend netmap: */ 1707f2637526SLuigi Rizzo g.dev_type = DEV_NETMAP; 1708f0ea3689SLuigi Rizzo sprintf(g.ifname, "netmap:%s", optarg); 1709f2637526SLuigi Rizzo } 171068b8534bSLuigi Rizzo break; 1711f8e4e36aSLuigi Rizzo 1712b303f675SLuigi Rizzo case 'I': 1713b303f675SLuigi Rizzo g.options |= OPT_INDIRECT; /* XXX use indirect buffer */ 1714b303f675SLuigi Rizzo break; 1715b303f675SLuigi Rizzo 171668b8534bSLuigi Rizzo case 'l': /* pkt_size */ 171768b8534bSLuigi Rizzo g.pkt_size = atoi(optarg); 171868b8534bSLuigi Rizzo break; 1719f8e4e36aSLuigi Rizzo 172068b8534bSLuigi Rizzo case 'd': 1721f8e4e36aSLuigi Rizzo g.dst_ip.name = optarg; 172268b8534bSLuigi Rizzo break; 1723f8e4e36aSLuigi Rizzo 172468b8534bSLuigi Rizzo case 's': 1725f8e4e36aSLuigi Rizzo g.src_ip.name = optarg; 172668b8534bSLuigi Rizzo break; 1727f8e4e36aSLuigi Rizzo 172868b8534bSLuigi Rizzo case 'T': /* report interval */ 1729f8e4e36aSLuigi Rizzo g.report_interval = atoi(optarg); 173068b8534bSLuigi Rizzo break; 1731f8e4e36aSLuigi Rizzo 173268b8534bSLuigi Rizzo case 'w': 173368b8534bSLuigi Rizzo wait_link = atoi(optarg); 173468b8534bSLuigi Rizzo break; 1735f8e4e36aSLuigi Rizzo 1736b303f675SLuigi Rizzo case 'W': /* XXX changed default */ 1737b303f675SLuigi Rizzo g.forever = 0; /* do not exit rx even with no traffic */ 1738f8e4e36aSLuigi Rizzo break; 1739f8e4e36aSLuigi Rizzo 174068b8534bSLuigi Rizzo case 'b': /* burst */ 174168b8534bSLuigi Rizzo g.burst = atoi(optarg); 174268b8534bSLuigi Rizzo break; 174368b8534bSLuigi Rizzo case 'c': 174468b8534bSLuigi Rizzo g.cpus = atoi(optarg); 174568b8534bSLuigi Rizzo break; 174668b8534bSLuigi Rizzo case 'p': 174768b8534bSLuigi Rizzo g.nthreads = atoi(optarg); 174868b8534bSLuigi Rizzo break; 174968b8534bSLuigi Rizzo 175068b8534bSLuigi Rizzo case 'D': /* destination mac */ 1751f8e4e36aSLuigi Rizzo g.dst_mac.name = optarg; 175268b8534bSLuigi Rizzo break; 1753f8e4e36aSLuigi Rizzo 175468b8534bSLuigi Rizzo case 'S': /* source mac */ 1755f8e4e36aSLuigi Rizzo g.src_mac.name = optarg; 175668b8534bSLuigi Rizzo break; 175768b8534bSLuigi Rizzo case 'v': 175868b8534bSLuigi Rizzo verbose++; 17591cb4c501SLuigi Rizzo break; 17601cb4c501SLuigi Rizzo case 'R': 17611cb4c501SLuigi Rizzo g.tx_rate = atoi(optarg); 17621cb4c501SLuigi Rizzo break; 1763b303f675SLuigi Rizzo case 'X': 1764b303f675SLuigi Rizzo g.options |= OPT_DUMP; 1765ce3ee1e7SLuigi Rizzo break; 1766ce3ee1e7SLuigi Rizzo case 'C': 1767ce3ee1e7SLuigi Rizzo g.nmr_config = strdup(optarg); 176817885a7bSLuigi Rizzo break; 176917885a7bSLuigi Rizzo case 'H': 177017885a7bSLuigi Rizzo g.virt_header = atoi(optarg); 1771f2637526SLuigi Rizzo break; 1772f0ea3689SLuigi Rizzo case 'e': /* extra bufs */ 1773f0ea3689SLuigi Rizzo g.extra_bufs = atoi(optarg); 1774f2637526SLuigi Rizzo break; 17754bf50f18SLuigi Rizzo case 'm': 17764bf50f18SLuigi Rizzo if (strcmp(optarg, "tx") == 0) { 17774bf50f18SLuigi Rizzo g.options |= OPT_MONITOR_TX; 17784bf50f18SLuigi Rizzo } else if (strcmp(optarg, "rx") == 0) { 17794bf50f18SLuigi Rizzo g.options |= OPT_MONITOR_RX; 17804bf50f18SLuigi Rizzo } else { 17814bf50f18SLuigi Rizzo D("unrecognized monitor mode %s", optarg); 17824bf50f18SLuigi Rizzo } 17834bf50f18SLuigi Rizzo break; 178468b8534bSLuigi Rizzo } 178568b8534bSLuigi Rizzo } 178668b8534bSLuigi Rizzo 1787f8e4e36aSLuigi Rizzo if (g.ifname == NULL) { 178868b8534bSLuigi Rizzo D("missing ifname"); 178968b8534bSLuigi Rizzo usage(); 179068b8534bSLuigi Rizzo } 1791f8e4e36aSLuigi Rizzo 1792f8e4e36aSLuigi Rizzo i = system_ncpus(); 1793f8e4e36aSLuigi Rizzo if (g.cpus < 0 || g.cpus > i) { 1794f8e4e36aSLuigi Rizzo D("%d cpus is too high, have only %d cpus", g.cpus, i); 179568b8534bSLuigi Rizzo usage(); 179668b8534bSLuigi Rizzo } 179768b8534bSLuigi Rizzo if (g.cpus == 0) 1798f8e4e36aSLuigi Rizzo g.cpus = i; 1799f8e4e36aSLuigi Rizzo 18004bf50f18SLuigi Rizzo if (g.pkt_size < 16 || g.pkt_size > MAX_PKTSIZE) { 18014bf50f18SLuigi Rizzo D("bad pktsize %d [16..%d]\n", g.pkt_size, MAX_PKTSIZE); 180268b8534bSLuigi Rizzo usage(); 180368b8534bSLuigi Rizzo } 180468b8534bSLuigi Rizzo 1805f8e4e36aSLuigi Rizzo if (g.src_mac.name == NULL) { 1806f8e4e36aSLuigi Rizzo static char mybuf[20] = "00:00:00:00:00:00"; 180799fb123fSLuigi Rizzo /* retrieve source mac address. */ 1808f8e4e36aSLuigi Rizzo if (source_hwaddr(g.ifname, mybuf) == -1) { 180999fb123fSLuigi Rizzo D("Unable to retrieve source mac"); 181099fb123fSLuigi Rizzo // continue, fail later 181199fb123fSLuigi Rizzo } 1812f8e4e36aSLuigi Rizzo g.src_mac.name = mybuf; 181399fb123fSLuigi Rizzo } 1814f8e4e36aSLuigi Rizzo /* extract address ranges */ 1815f8e4e36aSLuigi Rizzo extract_ip_range(&g.src_ip); 1816f8e4e36aSLuigi Rizzo extract_ip_range(&g.dst_ip); 1817f8e4e36aSLuigi Rizzo extract_mac_range(&g.src_mac); 1818f8e4e36aSLuigi Rizzo extract_mac_range(&g.dst_mac); 181999fb123fSLuigi Rizzo 1820f2637526SLuigi Rizzo if (g.src_ip.start != g.src_ip.end || 1821f2637526SLuigi Rizzo g.src_ip.port0 != g.src_ip.port1 || 1822f2637526SLuigi Rizzo g.dst_ip.start != g.dst_ip.end || 1823f2637526SLuigi Rizzo g.dst_ip.port0 != g.dst_ip.port1) 1824f2637526SLuigi Rizzo g.options |= OPT_COPY; 1825f2637526SLuigi Rizzo 182617885a7bSLuigi Rizzo if (g.virt_header != 0 && g.virt_header != VIRT_HDR_1 182717885a7bSLuigi Rizzo && g.virt_header != VIRT_HDR_2) { 182817885a7bSLuigi Rizzo D("bad virtio-net-header length"); 182917885a7bSLuigi Rizzo usage(); 183017885a7bSLuigi Rizzo } 183117885a7bSLuigi Rizzo 1832f8e4e36aSLuigi Rizzo if (g.dev_type == DEV_TAP) { 1833f8e4e36aSLuigi Rizzo D("want to use tap %s", g.ifname); 1834f8e4e36aSLuigi Rizzo g.main_fd = tap_alloc(g.ifname); 1835f8e4e36aSLuigi Rizzo if (g.main_fd < 0) { 1836f8e4e36aSLuigi Rizzo D("cannot open tap %s", g.ifname); 183799fb123fSLuigi Rizzo usage(); 183899fb123fSLuigi Rizzo } 1839f2637526SLuigi Rizzo #ifndef NO_PCAP 1840f2637526SLuigi Rizzo } else if (g.dev_type == DEV_PCAP) { 1841f8e4e36aSLuigi Rizzo char pcap_errbuf[PCAP_ERRBUF_SIZE]; 1842f8e4e36aSLuigi Rizzo 1843f8e4e36aSLuigi Rizzo pcap_errbuf[0] = '\0'; // init the buffer 18444bf50f18SLuigi Rizzo g.p = pcap_open_live(g.ifname, 256 /* XXX */, 1, 100, pcap_errbuf); 1845f8e4e36aSLuigi Rizzo if (g.p == NULL) { 1846f8e4e36aSLuigi Rizzo D("cannot open pcap on %s", g.ifname); 1847f8e4e36aSLuigi Rizzo usage(); 1848f8e4e36aSLuigi Rizzo } 18494bf50f18SLuigi Rizzo g.main_fd = pcap_fileno(g.p); 18504bf50f18SLuigi Rizzo D("using pcap on %s fileno %d", g.ifname, g.main_fd); 1851f2637526SLuigi Rizzo #endif /* !NO_PCAP */ 1852f2637526SLuigi Rizzo } else if (g.dummy_send) { /* but DEV_NETMAP */ 1853ce3ee1e7SLuigi Rizzo D("using a dummy send routine"); 185499fb123fSLuigi Rizzo } else { 18554bf50f18SLuigi Rizzo struct nmreq base_nmd; 1856f0ea3689SLuigi Rizzo 1857f0ea3689SLuigi Rizzo bzero(&base_nmd, sizeof(base_nmd)); 1858f0ea3689SLuigi Rizzo 18594bf50f18SLuigi Rizzo parse_nmr_config(g.nmr_config, &base_nmd); 1860f0ea3689SLuigi Rizzo if (g.extra_bufs) { 18614bf50f18SLuigi Rizzo base_nmd.nr_arg3 = g.extra_bufs; 1862f0ea3689SLuigi Rizzo } 1863f0ea3689SLuigi Rizzo 186468b8534bSLuigi Rizzo /* 1865f0ea3689SLuigi Rizzo * Open the netmap device using nm_open(). 186668b8534bSLuigi Rizzo * 186768b8534bSLuigi Rizzo * protocol stack and may cause a reset of the card, 186868b8534bSLuigi Rizzo * which in turn may take some time for the PHY to 1869f0ea3689SLuigi Rizzo * reconfigure. We do the open here to have time to reset. 187068b8534bSLuigi Rizzo */ 18714bf50f18SLuigi Rizzo g.nmd = nm_open(g.ifname, &base_nmd, 0, NULL); 1872f0ea3689SLuigi Rizzo if (g.nmd == NULL) { 1873f0ea3689SLuigi Rizzo D("Unable to open %s: %s", g.ifname, strerror(errno)); 1874f0ea3689SLuigi Rizzo goto out; 187568b8534bSLuigi Rizzo } 1876f0ea3689SLuigi Rizzo g.main_fd = g.nmd->fd; 1877f0ea3689SLuigi Rizzo D("mapped %dKB at %p", g.nmd->req.nr_memsize>>10, g.nmd->mem); 1878f0ea3689SLuigi Rizzo 18794bf50f18SLuigi Rizzo /* get num of queues in tx or rx */ 18804bf50f18SLuigi Rizzo if (g.td_body == sender_body) 18814bf50f18SLuigi Rizzo devqueues = g.nmd->req.nr_tx_rings; 18824bf50f18SLuigi Rizzo else 1883f0ea3689SLuigi Rizzo devqueues = g.nmd->req.nr_rx_rings; 188468b8534bSLuigi Rizzo 188568b8534bSLuigi Rizzo /* validate provided nthreads. */ 188668b8534bSLuigi Rizzo if (g.nthreads < 1 || g.nthreads > devqueues) { 188768b8534bSLuigi Rizzo D("bad nthreads %d, have %d queues", g.nthreads, devqueues); 188868b8534bSLuigi Rizzo // continue, fail later 188968b8534bSLuigi Rizzo } 189068b8534bSLuigi Rizzo 1891f2637526SLuigi Rizzo if (verbose) { 1892f0ea3689SLuigi Rizzo struct netmap_if *nifp = g.nmd->nifp; 1893f0ea3689SLuigi Rizzo struct nmreq *req = &g.nmd->req; 189468b8534bSLuigi Rizzo 1895f0ea3689SLuigi Rizzo D("nifp at offset %d, %d tx %d rx region %d", 1896f0ea3689SLuigi Rizzo req->nr_offset, req->nr_tx_rings, req->nr_rx_rings, 1897f0ea3689SLuigi Rizzo req->nr_arg2); 1898f0ea3689SLuigi Rizzo for (i = 0; i <= req->nr_tx_rings; i++) { 18994bf50f18SLuigi Rizzo struct netmap_ring *ring = NETMAP_TXRING(nifp, i); 19004bf50f18SLuigi Rizzo D(" TX%d at 0x%lx slots %d", i, 19014bf50f18SLuigi Rizzo (char *)ring - (char *)nifp, ring->num_slots); 1902f2637526SLuigi Rizzo } 1903f0ea3689SLuigi Rizzo for (i = 0; i <= req->nr_rx_rings; i++) { 19044bf50f18SLuigi Rizzo struct netmap_ring *ring = NETMAP_RXRING(nifp, i); 19054bf50f18SLuigi Rizzo D(" RX%d at 0x%lx slots %d", i, 19064bf50f18SLuigi Rizzo (char *)ring - (char *)nifp, ring->num_slots); 1907f2637526SLuigi Rizzo } 1908f2637526SLuigi Rizzo } 190968b8534bSLuigi Rizzo 191068b8534bSLuigi Rizzo /* Print some debug information. */ 191168b8534bSLuigi Rizzo fprintf(stdout, 191268b8534bSLuigi Rizzo "%s %s: %d queues, %d threads and %d cpus.\n", 1913f8e4e36aSLuigi Rizzo (g.td_body == sender_body) ? "Sending on" : "Receiving from", 1914f8e4e36aSLuigi Rizzo g.ifname, 191568b8534bSLuigi Rizzo devqueues, 191668b8534bSLuigi Rizzo g.nthreads, 191768b8534bSLuigi Rizzo g.cpus); 1918f8e4e36aSLuigi Rizzo if (g.td_body == sender_body) { 191968b8534bSLuigi Rizzo fprintf(stdout, "%s -> %s (%s -> %s)\n", 1920f8e4e36aSLuigi Rizzo g.src_ip.name, g.dst_ip.name, 1921f8e4e36aSLuigi Rizzo g.src_mac.name, g.dst_mac.name); 192268b8534bSLuigi Rizzo } 192368b8534bSLuigi Rizzo 1924f0ea3689SLuigi Rizzo out: 192568b8534bSLuigi Rizzo /* Exit if something went wrong. */ 1926f8e4e36aSLuigi Rizzo if (g.main_fd < 0) { 192768b8534bSLuigi Rizzo D("aborting"); 192868b8534bSLuigi Rizzo usage(); 192968b8534bSLuigi Rizzo } 193099fb123fSLuigi Rizzo } 193168b8534bSLuigi Rizzo 1932ce3ee1e7SLuigi Rizzo 193399fb123fSLuigi Rizzo if (g.options) { 1934b303f675SLuigi Rizzo D("--- SPECIAL OPTIONS:%s%s%s%s%s\n", 193599fb123fSLuigi Rizzo g.options & OPT_PREFETCH ? " prefetch" : "", 193699fb123fSLuigi Rizzo g.options & OPT_ACCESS ? " access" : "", 193799fb123fSLuigi Rizzo g.options & OPT_MEMCPY ? " memcpy" : "", 1938b303f675SLuigi Rizzo g.options & OPT_INDIRECT ? " indirect" : "", 193999fb123fSLuigi Rizzo g.options & OPT_COPY ? " copy" : ""); 194099fb123fSLuigi Rizzo } 19411cb4c501SLuigi Rizzo 1942ce3ee1e7SLuigi Rizzo g.tx_period.tv_sec = g.tx_period.tv_nsec = 0; 1943ce3ee1e7SLuigi Rizzo if (g.tx_rate > 0) { 1944ce3ee1e7SLuigi Rizzo /* try to have at least something every second, 194517885a7bSLuigi Rizzo * reducing the burst size to some 0.01s worth of data 1946ce3ee1e7SLuigi Rizzo * (but no less than one full set of fragments) 1947ce3ee1e7SLuigi Rizzo */ 194817885a7bSLuigi Rizzo uint64_t x; 194917885a7bSLuigi Rizzo int lim = (g.tx_rate)/300; 195017885a7bSLuigi Rizzo if (g.burst > lim) 195117885a7bSLuigi Rizzo g.burst = lim; 1952ce3ee1e7SLuigi Rizzo if (g.burst < g.frags) 1953ce3ee1e7SLuigi Rizzo g.burst = g.frags; 195417885a7bSLuigi Rizzo x = ((uint64_t)1000000000 * (uint64_t)g.burst) / (uint64_t) g.tx_rate; 195517885a7bSLuigi Rizzo g.tx_period.tv_nsec = x; 19561cb4c501SLuigi Rizzo g.tx_period.tv_sec = g.tx_period.tv_nsec / 1000000000; 19571cb4c501SLuigi Rizzo g.tx_period.tv_nsec = g.tx_period.tv_nsec % 1000000000; 19581cb4c501SLuigi Rizzo } 1959ce3ee1e7SLuigi Rizzo if (g.td_body == sender_body) 1960ce3ee1e7SLuigi Rizzo D("Sending %d packets every %ld.%09ld s", 1961ce3ee1e7SLuigi Rizzo g.burst, g.tx_period.tv_sec, g.tx_period.tv_nsec); 196268b8534bSLuigi Rizzo /* Wait for PHY reset. */ 196368b8534bSLuigi Rizzo D("Wait %d secs for phy reset", wait_link); 196468b8534bSLuigi Rizzo sleep(wait_link); 196568b8534bSLuigi Rizzo D("Ready..."); 196668b8534bSLuigi Rizzo 196768b8534bSLuigi Rizzo /* Install ^C handler. */ 196868b8534bSLuigi Rizzo global_nthreads = g.nthreads; 196968b8534bSLuigi Rizzo signal(SIGINT, sigint_h); 197068b8534bSLuigi Rizzo 1971f8e4e36aSLuigi Rizzo start_threads(&g); 1972f8e4e36aSLuigi Rizzo main_thread(&g); 1973f8e4e36aSLuigi Rizzo return 0; 197468b8534bSLuigi Rizzo } 197568b8534bSLuigi Rizzo 197668b8534bSLuigi Rizzo /* end of file */ 1977