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 40f0ea3689SLuigi Rizzo #define _GNU_SOURCE /* for CPU_SET() */ 41f0ea3689SLuigi Rizzo #include <stdio.h> 42f0ea3689SLuigi Rizzo #define NETMAP_WITH_LIBS 43f0ea3689SLuigi Rizzo #include <net/netmap_user.h> 44f0ea3689SLuigi Rizzo 45f8e4e36aSLuigi Rizzo 46b303f675SLuigi Rizzo #include <ctype.h> // isprint() 47f0ea3689SLuigi Rizzo #include <unistd.h> // sysconf() 48f0ea3689SLuigi Rizzo #include <sys/poll.h> 49f0ea3689SLuigi Rizzo #include <arpa/inet.h> /* ntohs */ 50f0ea3689SLuigi Rizzo #include <sys/sysctl.h> /* sysctl */ 51f0ea3689SLuigi Rizzo #include <ifaddrs.h> /* getifaddrs */ 52f0ea3689SLuigi Rizzo #include <net/ethernet.h> 53f0ea3689SLuigi Rizzo #include <netinet/in.h> 54f0ea3689SLuigi Rizzo #include <netinet/ip.h> 55f0ea3689SLuigi Rizzo #include <netinet/udp.h> 56f0ea3689SLuigi Rizzo 57f0ea3689SLuigi Rizzo #include <pthread.h> 58b303f675SLuigi Rizzo 59f2637526SLuigi Rizzo #ifndef NO_PCAP 60f2637526SLuigi Rizzo #include <pcap/pcap.h> 61f2637526SLuigi Rizzo #endif 62f0ea3689SLuigi Rizzo 63f0ea3689SLuigi Rizzo #ifdef linux 64f0ea3689SLuigi Rizzo 65f0ea3689SLuigi Rizzo #define cpuset_t cpu_set_t 66f0ea3689SLuigi Rizzo 67f0ea3689SLuigi Rizzo #define ifr_flagshigh ifr_flags /* only the low 16 bits here */ 68f0ea3689SLuigi Rizzo #define IFF_PPROMISC IFF_PROMISC /* IFF_PPROMISC does not exist */ 69f0ea3689SLuigi Rizzo #include <linux/ethtool.h> 70f0ea3689SLuigi Rizzo #include <linux/sockios.h> 71f0ea3689SLuigi Rizzo 72f0ea3689SLuigi Rizzo #define CLOCK_REALTIME_PRECISE CLOCK_REALTIME 73f0ea3689SLuigi Rizzo #include <netinet/ether.h> /* ether_aton */ 74f0ea3689SLuigi Rizzo #include <linux/if_packet.h> /* sockaddr_ll */ 75f0ea3689SLuigi Rizzo #endif /* linux */ 76f0ea3689SLuigi Rizzo 77f0ea3689SLuigi Rizzo #ifdef __FreeBSD__ 78f0ea3689SLuigi Rizzo #include <sys/endian.h> /* le64toh */ 79f0ea3689SLuigi Rizzo #include <machine/param.h> 80f0ea3689SLuigi Rizzo 81f0ea3689SLuigi Rizzo #include <pthread_np.h> /* pthread w/ affinity */ 82f0ea3689SLuigi Rizzo #include <sys/cpuset.h> /* cpu_set */ 83f0ea3689SLuigi Rizzo #include <net/if_dl.h> /* LLADDR */ 84f0ea3689SLuigi Rizzo #endif /* __FreeBSD__ */ 85f0ea3689SLuigi Rizzo 86f0ea3689SLuigi Rizzo #ifdef __APPLE__ 87f0ea3689SLuigi Rizzo 88f0ea3689SLuigi Rizzo #define cpuset_t uint64_t // XXX 89f0ea3689SLuigi Rizzo static inline void CPU_ZERO(cpuset_t *p) 90f0ea3689SLuigi Rizzo { 91f0ea3689SLuigi Rizzo *p = 0; 92f0ea3689SLuigi Rizzo } 93f0ea3689SLuigi Rizzo 94f0ea3689SLuigi Rizzo static inline void CPU_SET(uint32_t i, cpuset_t *p) 95f0ea3689SLuigi Rizzo { 96f0ea3689SLuigi Rizzo *p |= 1<< (i & 0x3f); 97f0ea3689SLuigi Rizzo } 98f0ea3689SLuigi Rizzo 99f0ea3689SLuigi Rizzo #define pthread_setaffinity_np(a, b, c) ((void)a, 0) 100f0ea3689SLuigi Rizzo 101f0ea3689SLuigi Rizzo #define ifr_flagshigh ifr_flags // XXX 102f0ea3689SLuigi Rizzo #define IFF_PPROMISC IFF_PROMISC 103f0ea3689SLuigi Rizzo #include <net/if_dl.h> /* LLADDR */ 104f0ea3689SLuigi Rizzo #define clock_gettime(a,b) \ 105f0ea3689SLuigi Rizzo do {struct timespec t0 = {0,0}; *(b) = t0; } while (0) 106f0ea3689SLuigi Rizzo #endif /* __APPLE__ */ 107f0ea3689SLuigi Rizzo 108ce3ee1e7SLuigi Rizzo const char *default_payload="netmap pkt-gen DIRECT payload\n" 109ce3ee1e7SLuigi Rizzo "http://info.iet.unipi.it/~luigi/netmap/ "; 110ce3ee1e7SLuigi Rizzo 111ce3ee1e7SLuigi Rizzo const char *indirect_payload="netmap pkt-gen indirect payload\n" 11268b8534bSLuigi Rizzo "http://info.iet.unipi.it/~luigi/netmap/ "; 11368b8534bSLuigi Rizzo 11468b8534bSLuigi Rizzo int verbose = 0; 11568b8534bSLuigi Rizzo 116f0ea3689SLuigi Rizzo #define SKIP_PAYLOAD 1 /* do not check payload. XXX unused */ 11768b8534bSLuigi Rizzo 11817885a7bSLuigi Rizzo 11917885a7bSLuigi Rizzo #define VIRT_HDR_1 10 /* length of a base vnet-hdr */ 12017885a7bSLuigi Rizzo #define VIRT_HDR_2 12 /* length of the extenede vnet-hdr */ 12117885a7bSLuigi Rizzo #define VIRT_HDR_MAX VIRT_HDR_2 12217885a7bSLuigi Rizzo struct virt_header { 12317885a7bSLuigi Rizzo uint8_t fields[VIRT_HDR_MAX]; 12417885a7bSLuigi Rizzo }; 12517885a7bSLuigi Rizzo 12668b8534bSLuigi Rizzo struct pkt { 12717885a7bSLuigi Rizzo struct virt_header vh; 12868b8534bSLuigi Rizzo struct ether_header eh; 12968b8534bSLuigi Rizzo struct ip ip; 13068b8534bSLuigi Rizzo struct udphdr udp; 1315819da83SLuigi Rizzo uint8_t body[2048]; // XXX hardwired 13268b8534bSLuigi Rizzo } __attribute__((__packed__)); 13368b8534bSLuigi Rizzo 134f8e4e36aSLuigi Rizzo struct ip_range { 135f8e4e36aSLuigi Rizzo char *name; 136ce3ee1e7SLuigi Rizzo uint32_t start, end; /* same as struct in_addr */ 137ce3ee1e7SLuigi Rizzo uint16_t port0, port1; 138f8e4e36aSLuigi Rizzo }; 139f8e4e36aSLuigi Rizzo 140f8e4e36aSLuigi Rizzo struct mac_range { 141f8e4e36aSLuigi Rizzo char *name; 142f8e4e36aSLuigi Rizzo struct ether_addr start, end; 143f8e4e36aSLuigi Rizzo }; 144f8e4e36aSLuigi Rizzo 145f0ea3689SLuigi Rizzo /* ifname can be netmap:foo-xxxx */ 146f0ea3689SLuigi Rizzo #define MAX_IFNAMELEN 64 /* our buffer for ifname */ 14768b8534bSLuigi Rizzo /* 14868b8534bSLuigi Rizzo * global arguments for all threads 14968b8534bSLuigi Rizzo */ 150f8e4e36aSLuigi Rizzo 15168b8534bSLuigi Rizzo struct glob_arg { 152f8e4e36aSLuigi Rizzo struct ip_range src_ip; 153f8e4e36aSLuigi Rizzo struct ip_range dst_ip; 154f8e4e36aSLuigi Rizzo struct mac_range dst_mac; 155f8e4e36aSLuigi Rizzo struct mac_range src_mac; 15668b8534bSLuigi Rizzo int pkt_size; 15768b8534bSLuigi Rizzo int burst; 158f8e4e36aSLuigi Rizzo int forever; 15968b8534bSLuigi Rizzo int npackets; /* total packets to send */ 160ce3ee1e7SLuigi Rizzo int frags; /* fragments per packet */ 16168b8534bSLuigi Rizzo int nthreads; 16268b8534bSLuigi Rizzo int cpus; 16399fb123fSLuigi Rizzo int options; /* testing */ 16499fb123fSLuigi Rizzo #define OPT_PREFETCH 1 16599fb123fSLuigi Rizzo #define OPT_ACCESS 2 16699fb123fSLuigi Rizzo #define OPT_COPY 4 16799fb123fSLuigi Rizzo #define OPT_MEMCPY 8 168f8e4e36aSLuigi Rizzo #define OPT_TS 16 /* add a timestamp */ 169b303f675SLuigi Rizzo #define OPT_INDIRECT 32 /* use indirect buffers, tx only */ 170b303f675SLuigi Rizzo #define OPT_DUMP 64 /* dump rx/tx traffic */ 171f8e4e36aSLuigi Rizzo int dev_type; 172f2637526SLuigi Rizzo #ifndef NO_PCAP 17368b8534bSLuigi Rizzo pcap_t *p; 174f2637526SLuigi Rizzo #endif 17568b8534bSLuigi Rizzo 1761cb4c501SLuigi Rizzo int tx_rate; 1771cb4c501SLuigi Rizzo struct timespec tx_period; 1781cb4c501SLuigi Rizzo 179f8e4e36aSLuigi Rizzo int affinity; 180f8e4e36aSLuigi Rizzo int main_fd; 181f0ea3689SLuigi Rizzo struct nm_desc *nmd; 182f0ea3689SLuigi Rizzo uint64_t nmd_flags; 183f2637526SLuigi Rizzo int report_interval; /* milliseconds between prints */ 184f8e4e36aSLuigi Rizzo void *(*td_body)(void *); 185f8e4e36aSLuigi Rizzo void *mmap_addr; 186f0ea3689SLuigi Rizzo char ifname[MAX_IFNAMELEN]; 187ce3ee1e7SLuigi Rizzo char *nmr_config; 188ce3ee1e7SLuigi Rizzo int dummy_send; 18917885a7bSLuigi Rizzo int virt_header; /* send also the virt_header */ 190f0ea3689SLuigi Rizzo int extra_bufs; /* goes in nr_arg3 */ 19168b8534bSLuigi Rizzo }; 192f8e4e36aSLuigi Rizzo enum dev_type { DEV_NONE, DEV_NETMAP, DEV_PCAP, DEV_TAP }; 193f8e4e36aSLuigi Rizzo 19468b8534bSLuigi Rizzo 19568b8534bSLuigi Rizzo /* 19668b8534bSLuigi Rizzo * Arguments for a new thread. The same structure is used by 19768b8534bSLuigi Rizzo * the source and the sink 19868b8534bSLuigi Rizzo */ 19968b8534bSLuigi Rizzo struct targ { 20068b8534bSLuigi Rizzo struct glob_arg *g; 20168b8534bSLuigi Rizzo int used; 20268b8534bSLuigi Rizzo int completed; 2033fe77e68SEd Maste int cancel; 20468b8534bSLuigi Rizzo int fd; 205f0ea3689SLuigi Rizzo struct nm_desc *nmd; 206f8e4e36aSLuigi Rizzo volatile uint64_t count; 2071cb4c501SLuigi Rizzo struct timespec tic, toc; 20868b8534bSLuigi Rizzo int me; 20968b8534bSLuigi Rizzo pthread_t thread; 21068b8534bSLuigi Rizzo int affinity; 21168b8534bSLuigi Rizzo 21268b8534bSLuigi Rizzo struct pkt pkt; 21368b8534bSLuigi Rizzo }; 21468b8534bSLuigi Rizzo 21568b8534bSLuigi Rizzo 216f8e4e36aSLuigi Rizzo /* 217f8e4e36aSLuigi Rizzo * extract the extremes from a range of ipv4 addresses. 218f8e4e36aSLuigi Rizzo * addr_lo[-addr_hi][:port_lo[-port_hi]] 219f8e4e36aSLuigi Rizzo */ 220f8e4e36aSLuigi Rizzo static void 221f8e4e36aSLuigi Rizzo extract_ip_range(struct ip_range *r) 222f8e4e36aSLuigi Rizzo { 223ce3ee1e7SLuigi Rizzo char *ap, *pp; 224ce3ee1e7SLuigi Rizzo struct in_addr a; 225f8e4e36aSLuigi Rizzo 22617885a7bSLuigi Rizzo if (verbose) 227f8e4e36aSLuigi Rizzo D("extract IP range from %s", r->name); 228ce3ee1e7SLuigi Rizzo r->port0 = r->port1 = 0; 229ce3ee1e7SLuigi Rizzo r->start = r->end = 0; 230ce3ee1e7SLuigi Rizzo 231ce3ee1e7SLuigi Rizzo /* the first - splits start/end of range */ 232ce3ee1e7SLuigi Rizzo ap = index(r->name, '-'); /* do we have ports ? */ 233ce3ee1e7SLuigi Rizzo if (ap) { 234ce3ee1e7SLuigi Rizzo *ap++ = '\0'; 235ce3ee1e7SLuigi Rizzo } 236ce3ee1e7SLuigi Rizzo /* grab the initial values (mandatory) */ 237ce3ee1e7SLuigi Rizzo pp = index(r->name, ':'); 238ce3ee1e7SLuigi Rizzo if (pp) { 239ce3ee1e7SLuigi Rizzo *pp++ = '\0'; 240ce3ee1e7SLuigi Rizzo r->port0 = r->port1 = strtol(pp, NULL, 0); 241ce3ee1e7SLuigi Rizzo }; 242ce3ee1e7SLuigi Rizzo inet_aton(r->name, &a); 243ce3ee1e7SLuigi Rizzo r->start = r->end = ntohl(a.s_addr); 244ce3ee1e7SLuigi Rizzo if (ap) { 245ce3ee1e7SLuigi Rizzo pp = index(ap, ':'); 246ce3ee1e7SLuigi Rizzo if (pp) { 247ce3ee1e7SLuigi Rizzo *pp++ = '\0'; 248ce3ee1e7SLuigi Rizzo if (*pp) 249ce3ee1e7SLuigi Rizzo r->port1 = strtol(pp, NULL, 0); 250ce3ee1e7SLuigi Rizzo } 251ce3ee1e7SLuigi Rizzo if (*ap) { 252ce3ee1e7SLuigi Rizzo inet_aton(ap, &a); 253ce3ee1e7SLuigi Rizzo r->end = ntohl(a.s_addr); 254ce3ee1e7SLuigi Rizzo } 255ce3ee1e7SLuigi Rizzo } 256ce3ee1e7SLuigi Rizzo if (r->port0 > r->port1) { 257ce3ee1e7SLuigi Rizzo uint16_t tmp = r->port0; 258f8e4e36aSLuigi Rizzo r->port0 = r->port1; 259ce3ee1e7SLuigi Rizzo r->port1 = tmp; 260f8e4e36aSLuigi Rizzo } 261ce3ee1e7SLuigi Rizzo if (r->start > r->end) { 262ce3ee1e7SLuigi Rizzo uint32_t tmp = r->start; 263f8e4e36aSLuigi Rizzo r->start = r->end; 264ce3ee1e7SLuigi Rizzo r->end = tmp; 265f8e4e36aSLuigi Rizzo } 266ce3ee1e7SLuigi Rizzo { 267ce3ee1e7SLuigi Rizzo struct in_addr a; 268ce3ee1e7SLuigi Rizzo char buf1[16]; // one ip address 269ce3ee1e7SLuigi Rizzo 270ce3ee1e7SLuigi Rizzo a.s_addr = htonl(r->end); 271ce3ee1e7SLuigi Rizzo strncpy(buf1, inet_ntoa(a), sizeof(buf1)); 272ce3ee1e7SLuigi Rizzo a.s_addr = htonl(r->start); 27317885a7bSLuigi Rizzo if (1) 274ce3ee1e7SLuigi Rizzo D("range is %s:%d to %s:%d", 275ce3ee1e7SLuigi Rizzo inet_ntoa(a), r->port0, buf1, r->port1); 276ce3ee1e7SLuigi Rizzo } 277f8e4e36aSLuigi Rizzo } 278f8e4e36aSLuigi Rizzo 279f8e4e36aSLuigi Rizzo static void 280f8e4e36aSLuigi Rizzo extract_mac_range(struct mac_range *r) 281f8e4e36aSLuigi Rizzo { 28217885a7bSLuigi Rizzo if (verbose) 283f8e4e36aSLuigi Rizzo D("extract MAC range from %s", r->name); 284f8e4e36aSLuigi Rizzo bcopy(ether_aton(r->name), &r->start, 6); 285f8e4e36aSLuigi Rizzo bcopy(ether_aton(r->name), &r->end, 6); 286f8e4e36aSLuigi Rizzo #if 0 287f8e4e36aSLuigi Rizzo bcopy(targ->src_mac, eh->ether_shost, 6); 288f8e4e36aSLuigi Rizzo p = index(targ->g->src_mac, '-'); 289f8e4e36aSLuigi Rizzo if (p) 290f8e4e36aSLuigi Rizzo targ->src_mac_range = atoi(p+1); 291f8e4e36aSLuigi Rizzo 292f8e4e36aSLuigi Rizzo bcopy(ether_aton(targ->g->dst_mac), targ->dst_mac, 6); 293f8e4e36aSLuigi Rizzo bcopy(targ->dst_mac, eh->ether_dhost, 6); 294f8e4e36aSLuigi Rizzo p = index(targ->g->dst_mac, '-'); 295f8e4e36aSLuigi Rizzo if (p) 296f8e4e36aSLuigi Rizzo targ->dst_mac_range = atoi(p+1); 297f8e4e36aSLuigi Rizzo #endif 29817885a7bSLuigi Rizzo if (verbose) 299f8e4e36aSLuigi Rizzo D("%s starts at %s", r->name, ether_ntoa(&r->start)); 300f8e4e36aSLuigi Rizzo } 301f8e4e36aSLuigi Rizzo 30268b8534bSLuigi Rizzo static struct targ *targs; 30368b8534bSLuigi Rizzo static int global_nthreads; 30468b8534bSLuigi Rizzo 30568b8534bSLuigi Rizzo /* control-C handler */ 30668b8534bSLuigi Rizzo static void 307f8e4e36aSLuigi Rizzo sigint_h(int sig) 30868b8534bSLuigi Rizzo { 309f8e4e36aSLuigi Rizzo int i; 31068b8534bSLuigi Rizzo 311f8e4e36aSLuigi Rizzo (void)sig; /* UNUSED */ 312f8e4e36aSLuigi Rizzo for (i = 0; i < global_nthreads; i++) { 313f8e4e36aSLuigi Rizzo targs[i].cancel = 1; 314f8e4e36aSLuigi Rizzo } 31568b8534bSLuigi Rizzo signal(SIGINT, SIG_DFL); 31668b8534bSLuigi Rizzo } 31768b8534bSLuigi Rizzo 31868b8534bSLuigi Rizzo /* sysctl wrapper to return the number of active CPUs */ 31968b8534bSLuigi Rizzo static int 32068b8534bSLuigi Rizzo system_ncpus(void) 32168b8534bSLuigi Rizzo { 322f0ea3689SLuigi Rizzo int ncpus; 323f0ea3689SLuigi Rizzo #if defined (__FreeBSD__) 324f0ea3689SLuigi Rizzo int mib[2] = { CTL_HW, HW_NCPU }; 325f0ea3689SLuigi Rizzo size_t len = sizeof(mib); 32668b8534bSLuigi Rizzo sysctl(mib, 2, &ncpus, &len, NULL, 0); 327f0ea3689SLuigi Rizzo #elif defined(linux) 328f0ea3689SLuigi Rizzo ncpus = sysconf(_SC_NPROCESSORS_ONLN); 329f0ea3689SLuigi Rizzo #else /* others */ 330f0ea3689SLuigi Rizzo ncpus = 1; 331f0ea3689SLuigi Rizzo #endif /* others */ 33268b8534bSLuigi Rizzo return (ncpus); 33368b8534bSLuigi Rizzo } 33468b8534bSLuigi Rizzo 335f8e4e36aSLuigi Rizzo #ifdef __linux__ 336f8e4e36aSLuigi Rizzo #define sockaddr_dl sockaddr_ll 337f8e4e36aSLuigi Rizzo #define sdl_family sll_family 338f8e4e36aSLuigi Rizzo #define AF_LINK AF_PACKET 339f8e4e36aSLuigi Rizzo #define LLADDR(s) s->sll_addr; 340f8e4e36aSLuigi Rizzo #include <linux/if_tun.h> 341f8e4e36aSLuigi Rizzo #define TAP_CLONEDEV "/dev/net/tun" 342f8e4e36aSLuigi Rizzo #endif /* __linux__ */ 343f8e4e36aSLuigi Rizzo 344f8e4e36aSLuigi Rizzo #ifdef __FreeBSD__ 345f8e4e36aSLuigi Rizzo #include <net/if_tun.h> 346f8e4e36aSLuigi Rizzo #define TAP_CLONEDEV "/dev/tap" 347f8e4e36aSLuigi Rizzo #endif /* __FreeBSD */ 348f8e4e36aSLuigi Rizzo 349f8e4e36aSLuigi Rizzo #ifdef __APPLE__ 350f8e4e36aSLuigi Rizzo // #warning TAP not supported on apple ? 351f8e4e36aSLuigi Rizzo #include <net/if_utun.h> 352f8e4e36aSLuigi Rizzo #define TAP_CLONEDEV "/dev/tap" 353f8e4e36aSLuigi Rizzo #endif /* __APPLE__ */ 354f8e4e36aSLuigi Rizzo 355f8e4e36aSLuigi Rizzo 35668b8534bSLuigi Rizzo /* 357ce3ee1e7SLuigi Rizzo * parse the vale configuration in conf and put it in nmr. 358f0ea3689SLuigi Rizzo * Return the flag set if necessary. 359ce3ee1e7SLuigi Rizzo * The configuration may consist of 0 to 4 numbers separated 360fc6eb28bSHiren Panchasara * by commas: #tx-slots,#rx-slots,#tx-rings,#rx-rings. 361ce3ee1e7SLuigi Rizzo * Missing numbers or zeroes stand for default values. 362ce3ee1e7SLuigi Rizzo * As an additional convenience, if exactly one number 363fc6eb28bSHiren Panchasara * is specified, then this is assigned to both #tx-slots and #rx-slots. 364fc6eb28bSHiren Panchasara * If there is no 4th number, then the 3rd is assigned to both #tx-rings 365ce3ee1e7SLuigi Rizzo * and #rx-rings. 366ce3ee1e7SLuigi Rizzo */ 367f0ea3689SLuigi Rizzo int 368f0ea3689SLuigi Rizzo parse_nmr_config(const char* conf, struct nmreq *nmr) 369ce3ee1e7SLuigi Rizzo { 370ce3ee1e7SLuigi Rizzo char *w, *tok; 371ce3ee1e7SLuigi Rizzo int i, v; 372ce3ee1e7SLuigi Rizzo 373ce3ee1e7SLuigi Rizzo nmr->nr_tx_rings = nmr->nr_rx_rings = 0; 374ce3ee1e7SLuigi Rizzo nmr->nr_tx_slots = nmr->nr_rx_slots = 0; 375ce3ee1e7SLuigi Rizzo if (conf == NULL || ! *conf) 376f0ea3689SLuigi Rizzo return 0; 377ce3ee1e7SLuigi Rizzo w = strdup(conf); 378ce3ee1e7SLuigi Rizzo for (i = 0, tok = strtok(w, ","); tok; i++, tok = strtok(NULL, ",")) { 379ce3ee1e7SLuigi Rizzo v = atoi(tok); 380ce3ee1e7SLuigi Rizzo switch (i) { 381ce3ee1e7SLuigi Rizzo case 0: 382ce3ee1e7SLuigi Rizzo nmr->nr_tx_slots = nmr->nr_rx_slots = v; 383ce3ee1e7SLuigi Rizzo break; 384ce3ee1e7SLuigi Rizzo case 1: 385ce3ee1e7SLuigi Rizzo nmr->nr_rx_slots = v; 386ce3ee1e7SLuigi Rizzo break; 387ce3ee1e7SLuigi Rizzo case 2: 388ce3ee1e7SLuigi Rizzo nmr->nr_tx_rings = nmr->nr_rx_rings = v; 389ce3ee1e7SLuigi Rizzo break; 390ce3ee1e7SLuigi Rizzo case 3: 391ce3ee1e7SLuigi Rizzo nmr->nr_rx_rings = v; 392ce3ee1e7SLuigi Rizzo break; 393ce3ee1e7SLuigi Rizzo default: 394ce3ee1e7SLuigi Rizzo D("ignored config: %s", tok); 395ce3ee1e7SLuigi Rizzo break; 396ce3ee1e7SLuigi Rizzo } 397ce3ee1e7SLuigi Rizzo } 398ce3ee1e7SLuigi Rizzo D("txr %d txd %d rxr %d rxd %d", 399ce3ee1e7SLuigi Rizzo nmr->nr_tx_rings, nmr->nr_tx_slots, 400ce3ee1e7SLuigi Rizzo nmr->nr_rx_rings, nmr->nr_rx_slots); 401ce3ee1e7SLuigi Rizzo free(w); 402f0ea3689SLuigi Rizzo return (nmr->nr_tx_rings || nmr->nr_tx_slots || 403f0ea3689SLuigi Rizzo nmr->nr_rx_rings || nmr->nr_rx_slots) ? 404f0ea3689SLuigi Rizzo NM_OPEN_RING_CFG : 0; 405ce3ee1e7SLuigi Rizzo } 406ce3ee1e7SLuigi Rizzo 407ce3ee1e7SLuigi Rizzo 408ce3ee1e7SLuigi Rizzo /* 40968b8534bSLuigi Rizzo * locate the src mac address for our interface, put it 41068b8534bSLuigi Rizzo * into the user-supplied buffer. return 0 if ok, -1 on error. 41168b8534bSLuigi Rizzo */ 41268b8534bSLuigi Rizzo static int 41368b8534bSLuigi Rizzo source_hwaddr(const char *ifname, char *buf) 41468b8534bSLuigi Rizzo { 41568b8534bSLuigi Rizzo struct ifaddrs *ifaphead, *ifap; 41668b8534bSLuigi Rizzo int l = sizeof(ifap->ifa_name); 41768b8534bSLuigi Rizzo 41868b8534bSLuigi Rizzo if (getifaddrs(&ifaphead) != 0) { 41968b8534bSLuigi Rizzo D("getifaddrs %s failed", ifname); 42068b8534bSLuigi Rizzo return (-1); 42168b8534bSLuigi Rizzo } 42268b8534bSLuigi Rizzo 42368b8534bSLuigi Rizzo for (ifap = ifaphead; ifap; ifap = ifap->ifa_next) { 42468b8534bSLuigi Rizzo struct sockaddr_dl *sdl = 42568b8534bSLuigi Rizzo (struct sockaddr_dl *)ifap->ifa_addr; 42668b8534bSLuigi Rizzo uint8_t *mac; 42768b8534bSLuigi Rizzo 42868b8534bSLuigi Rizzo if (!sdl || sdl->sdl_family != AF_LINK) 42968b8534bSLuigi Rizzo continue; 43068b8534bSLuigi Rizzo if (strncmp(ifap->ifa_name, ifname, l) != 0) 43168b8534bSLuigi Rizzo continue; 43268b8534bSLuigi Rizzo mac = (uint8_t *)LLADDR(sdl); 43368b8534bSLuigi Rizzo sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x", 43468b8534bSLuigi Rizzo mac[0], mac[1], mac[2], 43568b8534bSLuigi Rizzo mac[3], mac[4], mac[5]); 43668b8534bSLuigi Rizzo if (verbose) 43768b8534bSLuigi Rizzo D("source hwaddr %s", buf); 43868b8534bSLuigi Rizzo break; 43968b8534bSLuigi Rizzo } 44068b8534bSLuigi Rizzo freeifaddrs(ifaphead); 44168b8534bSLuigi Rizzo return ifap ? 0 : 1; 44268b8534bSLuigi Rizzo } 44368b8534bSLuigi Rizzo 44468b8534bSLuigi Rizzo 44568b8534bSLuigi Rizzo /* set the thread affinity. */ 44668b8534bSLuigi Rizzo static int 44768b8534bSLuigi Rizzo setaffinity(pthread_t me, int i) 44868b8534bSLuigi Rizzo { 44968b8534bSLuigi Rizzo cpuset_t cpumask; 45068b8534bSLuigi Rizzo 45168b8534bSLuigi Rizzo if (i == -1) 45268b8534bSLuigi Rizzo return 0; 45368b8534bSLuigi Rizzo 45468b8534bSLuigi Rizzo /* Set thread affinity affinity.*/ 45568b8534bSLuigi Rizzo CPU_ZERO(&cpumask); 45668b8534bSLuigi Rizzo CPU_SET(i, &cpumask); 45768b8534bSLuigi Rizzo 45868b8534bSLuigi Rizzo if (pthread_setaffinity_np(me, sizeof(cpuset_t), &cpumask) != 0) { 45917885a7bSLuigi Rizzo D("Unable to set affinity: %s", strerror(errno)); 46068b8534bSLuigi Rizzo return 1; 46168b8534bSLuigi Rizzo } 46268b8534bSLuigi Rizzo return 0; 46368b8534bSLuigi Rizzo } 46468b8534bSLuigi Rizzo 46568b8534bSLuigi Rizzo /* Compute the checksum of the given ip header. */ 46668b8534bSLuigi Rizzo static uint16_t 467f8e4e36aSLuigi Rizzo checksum(const void *data, uint16_t len, uint32_t sum) 46868b8534bSLuigi Rizzo { 46968b8534bSLuigi Rizzo const uint8_t *addr = data; 470f8e4e36aSLuigi Rizzo uint32_t i; 47168b8534bSLuigi Rizzo 472f8e4e36aSLuigi Rizzo /* Checksum all the pairs of bytes first... */ 473f8e4e36aSLuigi Rizzo for (i = 0; i < (len & ~1U); i += 2) { 474f8e4e36aSLuigi Rizzo sum += (u_int16_t)ntohs(*((u_int16_t *)(addr + i))); 475f8e4e36aSLuigi Rizzo if (sum > 0xFFFF) 476f8e4e36aSLuigi Rizzo sum -= 0xFFFF; 477f8e4e36aSLuigi Rizzo } 478f8e4e36aSLuigi Rizzo /* 479f8e4e36aSLuigi Rizzo * If there's a single byte left over, checksum it, too. 480f8e4e36aSLuigi Rizzo * Network byte order is big-endian, so the remaining byte is 481f8e4e36aSLuigi Rizzo * the high byte. 482f8e4e36aSLuigi Rizzo */ 483f8e4e36aSLuigi Rizzo if (i < len) { 484f8e4e36aSLuigi Rizzo sum += addr[i] << 8; 485f8e4e36aSLuigi Rizzo if (sum > 0xFFFF) 486f8e4e36aSLuigi Rizzo sum -= 0xFFFF; 487f8e4e36aSLuigi Rizzo } 488f8e4e36aSLuigi Rizzo return sum; 48968b8534bSLuigi Rizzo } 49068b8534bSLuigi Rizzo 491f8e4e36aSLuigi Rizzo static u_int16_t 492f8e4e36aSLuigi Rizzo wrapsum(u_int32_t sum) 493f8e4e36aSLuigi Rizzo { 494f8e4e36aSLuigi Rizzo sum = ~sum & 0xFFFF; 495f8e4e36aSLuigi Rizzo return (htons(sum)); 49668b8534bSLuigi Rizzo } 49768b8534bSLuigi Rizzo 498b303f675SLuigi Rizzo /* Check the payload of the packet for errors (use it for debug). 499b303f675SLuigi Rizzo * Look for consecutive ascii representations of the size of the packet. 500b303f675SLuigi Rizzo */ 501b303f675SLuigi Rizzo static void 502b303f675SLuigi Rizzo dump_payload(char *p, int len, struct netmap_ring *ring, int cur) 503b303f675SLuigi Rizzo { 504b303f675SLuigi Rizzo char buf[128]; 505b303f675SLuigi Rizzo int i, j, i0; 506b303f675SLuigi Rizzo 507b303f675SLuigi Rizzo /* get the length in ASCII of the length of the packet. */ 508b303f675SLuigi Rizzo 509ce3ee1e7SLuigi Rizzo printf("ring %p cur %5d [buf %6d flags 0x%04x len %5d]\n", 510ce3ee1e7SLuigi Rizzo ring, cur, ring->slot[cur].buf_idx, 511ce3ee1e7SLuigi Rizzo ring->slot[cur].flags, len); 512b303f675SLuigi Rizzo /* hexdump routine */ 513b303f675SLuigi Rizzo for (i = 0; i < len; ) { 514b303f675SLuigi Rizzo memset(buf, sizeof(buf), ' '); 515b303f675SLuigi Rizzo sprintf(buf, "%5d: ", i); 516b303f675SLuigi Rizzo i0 = i; 517b303f675SLuigi Rizzo for (j=0; j < 16 && i < len; i++, j++) 518b303f675SLuigi Rizzo sprintf(buf+7+j*3, "%02x ", (uint8_t)(p[i])); 519b303f675SLuigi Rizzo i = i0; 520b303f675SLuigi Rizzo for (j=0; j < 16 && i < len; i++, j++) 521b303f675SLuigi Rizzo sprintf(buf+7+j + 48, "%c", 522b303f675SLuigi Rizzo isprint(p[i]) ? p[i] : '.'); 523b303f675SLuigi Rizzo printf("%s\n", buf); 524b303f675SLuigi Rizzo } 525b303f675SLuigi Rizzo } 526b303f675SLuigi Rizzo 52768b8534bSLuigi Rizzo /* 52868b8534bSLuigi Rizzo * Fill a packet with some payload. 529f8e4e36aSLuigi Rizzo * We create a UDP packet so the payload starts at 530f8e4e36aSLuigi Rizzo * 14+20+8 = 42 bytes. 53168b8534bSLuigi Rizzo */ 532f8e4e36aSLuigi Rizzo #ifdef __linux__ 533f8e4e36aSLuigi Rizzo #define uh_sport source 534f8e4e36aSLuigi Rizzo #define uh_dport dest 535f8e4e36aSLuigi Rizzo #define uh_ulen len 536f8e4e36aSLuigi Rizzo #define uh_sum check 537f8e4e36aSLuigi Rizzo #endif /* linux */ 538b303f675SLuigi Rizzo 539ce3ee1e7SLuigi Rizzo /* 540ce3ee1e7SLuigi Rizzo * increment the addressed in the packet, 541ce3ee1e7SLuigi Rizzo * starting from the least significant field. 542ce3ee1e7SLuigi Rizzo * DST_IP DST_PORT SRC_IP SRC_PORT 543ce3ee1e7SLuigi Rizzo */ 544ce3ee1e7SLuigi Rizzo static void 545ce3ee1e7SLuigi Rizzo update_addresses(struct pkt *pkt, struct glob_arg *g) 546ce3ee1e7SLuigi Rizzo { 547ce3ee1e7SLuigi Rizzo uint32_t a; 548ce3ee1e7SLuigi Rizzo uint16_t p; 549ce3ee1e7SLuigi Rizzo struct ip *ip = &pkt->ip; 550ce3ee1e7SLuigi Rizzo struct udphdr *udp = &pkt->udp; 551ce3ee1e7SLuigi Rizzo 552f2637526SLuigi Rizzo do { 553ce3ee1e7SLuigi Rizzo p = ntohs(udp->uh_sport); 554ce3ee1e7SLuigi Rizzo if (p < g->src_ip.port1) { /* just inc, no wrap */ 555ce3ee1e7SLuigi Rizzo udp->uh_sport = htons(p + 1); 556f2637526SLuigi Rizzo break; 557ce3ee1e7SLuigi Rizzo } 558ce3ee1e7SLuigi Rizzo udp->uh_sport = htons(g->src_ip.port0); 559ce3ee1e7SLuigi Rizzo 560ce3ee1e7SLuigi Rizzo a = ntohl(ip->ip_src.s_addr); 561ce3ee1e7SLuigi Rizzo if (a < g->src_ip.end) { /* just inc, no wrap */ 562ce3ee1e7SLuigi Rizzo ip->ip_src.s_addr = htonl(a + 1); 563f2637526SLuigi Rizzo break; 564ce3ee1e7SLuigi Rizzo } 565ce3ee1e7SLuigi Rizzo ip->ip_src.s_addr = htonl(g->src_ip.start); 566ce3ee1e7SLuigi Rizzo 567ce3ee1e7SLuigi Rizzo udp->uh_sport = htons(g->src_ip.port0); 568ce3ee1e7SLuigi Rizzo p = ntohs(udp->uh_dport); 569ce3ee1e7SLuigi Rizzo if (p < g->dst_ip.port1) { /* just inc, no wrap */ 570ce3ee1e7SLuigi Rizzo udp->uh_dport = htons(p + 1); 571f2637526SLuigi Rizzo break; 572ce3ee1e7SLuigi Rizzo } 573ce3ee1e7SLuigi Rizzo udp->uh_dport = htons(g->dst_ip.port0); 574ce3ee1e7SLuigi Rizzo 575ce3ee1e7SLuigi Rizzo a = ntohl(ip->ip_dst.s_addr); 576ce3ee1e7SLuigi Rizzo if (a < g->dst_ip.end) { /* just inc, no wrap */ 577ce3ee1e7SLuigi Rizzo ip->ip_dst.s_addr = htonl(a + 1); 578f2637526SLuigi Rizzo break; 579ce3ee1e7SLuigi Rizzo } 580ce3ee1e7SLuigi Rizzo ip->ip_dst.s_addr = htonl(g->dst_ip.start); 581f2637526SLuigi Rizzo } while (0); 582f2637526SLuigi Rizzo // update checksum 583ce3ee1e7SLuigi Rizzo } 584ce3ee1e7SLuigi Rizzo 585ce3ee1e7SLuigi Rizzo /* 586ce3ee1e7SLuigi Rizzo * initialize one packet and prepare for the next one. 587ce3ee1e7SLuigi Rizzo * The copy could be done better instead of repeating it each time. 588ce3ee1e7SLuigi Rizzo */ 58968b8534bSLuigi Rizzo static void 59068b8534bSLuigi Rizzo initialize_packet(struct targ *targ) 59168b8534bSLuigi Rizzo { 59268b8534bSLuigi Rizzo struct pkt *pkt = &targ->pkt; 59368b8534bSLuigi Rizzo struct ether_header *eh; 59468b8534bSLuigi Rizzo struct ip *ip; 59568b8534bSLuigi Rizzo struct udphdr *udp; 596f8e4e36aSLuigi Rizzo uint16_t paylen = targ->g->pkt_size - sizeof(*eh) - sizeof(struct ip); 597b303f675SLuigi Rizzo const char *payload = targ->g->options & OPT_INDIRECT ? 598ce3ee1e7SLuigi Rizzo indirect_payload : default_payload; 599f2637526SLuigi Rizzo int i, l0 = strlen(payload); 60068b8534bSLuigi Rizzo 601ce3ee1e7SLuigi Rizzo /* create a nice NUL-terminated string */ 602f2637526SLuigi Rizzo for (i = 0; i < paylen; i += l0) { 603f2637526SLuigi Rizzo if (l0 > paylen - i) 604f2637526SLuigi Rizzo l0 = paylen - i; // last round 605f2637526SLuigi Rizzo bcopy(payload, pkt->body + i, l0); 60668b8534bSLuigi Rizzo } 60768b8534bSLuigi Rizzo pkt->body[i-1] = '\0'; 60868b8534bSLuigi Rizzo ip = &pkt->ip; 609f8e4e36aSLuigi Rizzo 610ce3ee1e7SLuigi Rizzo /* prepare the headers */ 61168b8534bSLuigi Rizzo ip->ip_v = IPVERSION; 61268b8534bSLuigi Rizzo ip->ip_hl = 5; 61368b8534bSLuigi Rizzo ip->ip_id = 0; 61468b8534bSLuigi Rizzo ip->ip_tos = IPTOS_LOWDELAY; 61568b8534bSLuigi Rizzo ip->ip_len = ntohs(targ->g->pkt_size - sizeof(*eh)); 61668b8534bSLuigi Rizzo ip->ip_id = 0; 61768b8534bSLuigi Rizzo ip->ip_off = htons(IP_DF); /* Don't fragment */ 61868b8534bSLuigi Rizzo ip->ip_ttl = IPDEFTTL; 61968b8534bSLuigi Rizzo ip->ip_p = IPPROTO_UDP; 620ce3ee1e7SLuigi Rizzo ip->ip_dst.s_addr = htonl(targ->g->dst_ip.start); 621ce3ee1e7SLuigi Rizzo ip->ip_src.s_addr = htonl(targ->g->src_ip.start); 622f8e4e36aSLuigi Rizzo ip->ip_sum = wrapsum(checksum(ip, sizeof(*ip), 0)); 623f8e4e36aSLuigi Rizzo 624f8e4e36aSLuigi Rizzo 625f8e4e36aSLuigi Rizzo udp = &pkt->udp; 626ce3ee1e7SLuigi Rizzo udp->uh_sport = htons(targ->g->src_ip.port0); 627ce3ee1e7SLuigi Rizzo udp->uh_dport = htons(targ->g->dst_ip.port0); 628f8e4e36aSLuigi Rizzo udp->uh_ulen = htons(paylen); 629f8e4e36aSLuigi Rizzo /* Magic: taken from sbin/dhclient/packet.c */ 630f8e4e36aSLuigi Rizzo udp->uh_sum = wrapsum(checksum(udp, sizeof(*udp), 631f8e4e36aSLuigi Rizzo checksum(pkt->body, 632f8e4e36aSLuigi Rizzo paylen - sizeof(*udp), 633f8e4e36aSLuigi Rizzo checksum(&ip->ip_src, 2 * sizeof(ip->ip_src), 634f8e4e36aSLuigi Rizzo IPPROTO_UDP + (u_int32_t)ntohs(udp->uh_ulen) 635f8e4e36aSLuigi Rizzo ) 636f8e4e36aSLuigi Rizzo ) 637f8e4e36aSLuigi Rizzo )); 63868b8534bSLuigi Rizzo 63968b8534bSLuigi Rizzo eh = &pkt->eh; 640f8e4e36aSLuigi Rizzo bcopy(&targ->g->src_mac.start, eh->ether_shost, 6); 641f8e4e36aSLuigi Rizzo bcopy(&targ->g->dst_mac.start, eh->ether_dhost, 6); 64268b8534bSLuigi Rizzo eh->ether_type = htons(ETHERTYPE_IP); 64317885a7bSLuigi Rizzo 64417885a7bSLuigi Rizzo bzero(&pkt->vh, sizeof(pkt->vh)); 645b303f675SLuigi Rizzo // dump_payload((void *)pkt, targ->g->pkt_size, NULL, 0); 64668b8534bSLuigi Rizzo } 64768b8534bSLuigi Rizzo 64868b8534bSLuigi Rizzo 64968b8534bSLuigi Rizzo 65068b8534bSLuigi Rizzo /* 65168b8534bSLuigi Rizzo * create and enqueue a batch of packets on a ring. 65268b8534bSLuigi Rizzo * On the last one set NS_REPORT to tell the driver to generate 65368b8534bSLuigi Rizzo * an interrupt when done. 65468b8534bSLuigi Rizzo */ 65568b8534bSLuigi Rizzo static int 65617885a7bSLuigi Rizzo send_packets(struct netmap_ring *ring, struct pkt *pkt, void *frame, 65717885a7bSLuigi Rizzo int size, struct glob_arg *g, u_int count, int options, 65817885a7bSLuigi Rizzo u_int nfrags) 65968b8534bSLuigi Rizzo { 66017885a7bSLuigi Rizzo u_int n, sent, cur = ring->cur; 661f2637526SLuigi Rizzo u_int fcnt; 66268b8534bSLuigi Rizzo 66317885a7bSLuigi Rizzo n = nm_ring_space(ring); 66417885a7bSLuigi Rizzo if (n < count) 66517885a7bSLuigi Rizzo count = n; 666ce3ee1e7SLuigi Rizzo if (count < nfrags) { 667ce3ee1e7SLuigi Rizzo D("truncating packet, no room for frags %d %d", 668ce3ee1e7SLuigi Rizzo count, nfrags); 669ce3ee1e7SLuigi Rizzo } 67099fb123fSLuigi Rizzo #if 0 67199fb123fSLuigi Rizzo if (options & (OPT_COPY | OPT_PREFETCH) ) { 67268b8534bSLuigi Rizzo for (sent = 0; sent < count; sent++) { 67368b8534bSLuigi Rizzo struct netmap_slot *slot = &ring->slot[cur]; 67468b8534bSLuigi Rizzo char *p = NETMAP_BUF(ring, slot->buf_idx); 67568b8534bSLuigi Rizzo 676f2637526SLuigi Rizzo __builtin_prefetch(p); 67717885a7bSLuigi Rizzo cur = nm_ring_next(ring, cur); 67899fb123fSLuigi Rizzo } 67999fb123fSLuigi Rizzo cur = ring->cur; 68099fb123fSLuigi Rizzo } 68199fb123fSLuigi Rizzo #endif 682ce3ee1e7SLuigi Rizzo for (fcnt = nfrags, sent = 0; sent < count; sent++) { 68399fb123fSLuigi Rizzo struct netmap_slot *slot = &ring->slot[cur]; 68499fb123fSLuigi Rizzo char *p = NETMAP_BUF(ring, slot->buf_idx); 68599fb123fSLuigi Rizzo 686b303f675SLuigi Rizzo slot->flags = 0; 687b303f675SLuigi Rizzo if (options & OPT_INDIRECT) { 688b303f675SLuigi Rizzo slot->flags |= NS_INDIRECT; 68917885a7bSLuigi Rizzo slot->ptr = (uint64_t)frame; 690ce3ee1e7SLuigi Rizzo } else if (options & OPT_COPY) { 691f0ea3689SLuigi Rizzo nm_pkt_copy(frame, p, size); 692f2637526SLuigi Rizzo if (fcnt == nfrags) 693ce3ee1e7SLuigi Rizzo update_addresses(pkt, g); 694ce3ee1e7SLuigi Rizzo } else if (options & OPT_MEMCPY) { 69517885a7bSLuigi Rizzo memcpy(p, frame, size); 696f2637526SLuigi Rizzo if (fcnt == nfrags) 697ce3ee1e7SLuigi Rizzo update_addresses(pkt, g); 698ce3ee1e7SLuigi Rizzo } else if (options & OPT_PREFETCH) { 699f2637526SLuigi Rizzo __builtin_prefetch(p); 700ce3ee1e7SLuigi Rizzo } 701ce3ee1e7SLuigi Rizzo if (options & OPT_DUMP) 702ce3ee1e7SLuigi Rizzo dump_payload(p, size, ring, cur); 70368b8534bSLuigi Rizzo slot->len = size; 704ce3ee1e7SLuigi Rizzo if (--fcnt > 0) 705ce3ee1e7SLuigi Rizzo slot->flags |= NS_MOREFRAG; 706ce3ee1e7SLuigi Rizzo else 707ce3ee1e7SLuigi Rizzo fcnt = nfrags; 708ce3ee1e7SLuigi Rizzo if (sent == count - 1) { 709ce3ee1e7SLuigi Rizzo slot->flags &= ~NS_MOREFRAG; 71068b8534bSLuigi Rizzo slot->flags |= NS_REPORT; 711ce3ee1e7SLuigi Rizzo } 71217885a7bSLuigi Rizzo cur = nm_ring_next(ring, cur); 71368b8534bSLuigi Rizzo } 71417885a7bSLuigi Rizzo ring->head = ring->cur = cur; 71568b8534bSLuigi Rizzo 71668b8534bSLuigi Rizzo return (sent); 71768b8534bSLuigi Rizzo } 71868b8534bSLuigi Rizzo 719f8e4e36aSLuigi Rizzo /* 720f8e4e36aSLuigi Rizzo * Send a packet, and wait for a response. 721f8e4e36aSLuigi Rizzo * The payload (after UDP header, ofs 42) has a 4-byte sequence 722f8e4e36aSLuigi Rizzo * followed by a struct timeval (or bintime?) 723f8e4e36aSLuigi Rizzo */ 724f8e4e36aSLuigi Rizzo #define PAY_OFS 42 /* where in the pkt... */ 725f8e4e36aSLuigi Rizzo 72668b8534bSLuigi Rizzo static void * 727f8e4e36aSLuigi Rizzo pinger_body(void *data) 72868b8534bSLuigi Rizzo { 72968b8534bSLuigi Rizzo struct targ *targ = (struct targ *) data; 730f0ea3689SLuigi Rizzo struct pollfd pfd = { .fd = targ->fd, .events = POLLIN }; 731f0ea3689SLuigi Rizzo struct netmap_if *nifp = targ->nmd->nifp; 732f8e4e36aSLuigi Rizzo int i, rx = 0, n = targ->g->npackets; 73317885a7bSLuigi Rizzo void *frame; 73417885a7bSLuigi Rizzo int size; 735f0ea3689SLuigi Rizzo uint32_t sent = 0; 736f0ea3689SLuigi Rizzo struct timespec ts, now, last_print; 737f0ea3689SLuigi Rizzo uint32_t count = 0, min = 1000000000, av = 0; 73817885a7bSLuigi Rizzo 73917885a7bSLuigi Rizzo frame = &targ->pkt; 74017885a7bSLuigi Rizzo frame += sizeof(targ->pkt.vh) - targ->g->virt_header; 74117885a7bSLuigi Rizzo size = targ->g->pkt_size + targ->g->virt_header; 742e5ecae38SEd Maste 743f8e4e36aSLuigi Rizzo 744f8e4e36aSLuigi Rizzo if (targ->g->nthreads > 1) { 745f8e4e36aSLuigi Rizzo D("can only ping with 1 thread"); 746f8e4e36aSLuigi Rizzo return NULL; 747f95a30bdSEd Maste } 748f8e4e36aSLuigi Rizzo 749f8e4e36aSLuigi Rizzo clock_gettime(CLOCK_REALTIME_PRECISE, &last_print); 75017885a7bSLuigi Rizzo now = last_print; 751f8e4e36aSLuigi Rizzo while (n == 0 || (int)sent < n) { 752f8e4e36aSLuigi Rizzo struct netmap_ring *ring = NETMAP_TXRING(nifp, 0); 753f8e4e36aSLuigi Rizzo struct netmap_slot *slot; 754f8e4e36aSLuigi Rizzo char *p; 75517885a7bSLuigi Rizzo for (i = 0; i < 1; i++) { /* XXX why the loop for 1 pkt ? */ 756f8e4e36aSLuigi Rizzo slot = &ring->slot[ring->cur]; 75717885a7bSLuigi Rizzo slot->len = size; 758f8e4e36aSLuigi Rizzo p = NETMAP_BUF(ring, slot->buf_idx); 759f8e4e36aSLuigi Rizzo 76017885a7bSLuigi Rizzo if (nm_ring_empty(ring)) { 761f8e4e36aSLuigi Rizzo D("-- ouch, cannot send"); 762f8e4e36aSLuigi Rizzo } else { 763f0ea3689SLuigi Rizzo nm_pkt_copy(frame, p, size); 764f8e4e36aSLuigi Rizzo clock_gettime(CLOCK_REALTIME_PRECISE, &ts); 765f8e4e36aSLuigi Rizzo bcopy(&sent, p+42, sizeof(sent)); 766f8e4e36aSLuigi Rizzo bcopy(&ts, p+46, sizeof(ts)); 767f8e4e36aSLuigi Rizzo sent++; 76817885a7bSLuigi Rizzo ring->head = ring->cur = nm_ring_next(ring, ring->cur); 769f8e4e36aSLuigi Rizzo } 770f8e4e36aSLuigi Rizzo } 771f8e4e36aSLuigi Rizzo /* should use a parameter to decide how often to send */ 772f0ea3689SLuigi Rizzo if (poll(&pfd, 1, 3000) <= 0) { 77317885a7bSLuigi Rizzo D("poll error/timeout on queue %d: %s", targ->me, 77417885a7bSLuigi Rizzo strerror(errno)); 775f8e4e36aSLuigi Rizzo continue; 776f8e4e36aSLuigi Rizzo } 777f8e4e36aSLuigi Rizzo /* see what we got back */ 778f0ea3689SLuigi Rizzo for (i = targ->nmd->first_tx_ring; 779f0ea3689SLuigi Rizzo i <= targ->nmd->last_tx_ring; i++) { 780f8e4e36aSLuigi Rizzo ring = NETMAP_RXRING(nifp, i); 78117885a7bSLuigi Rizzo while (!nm_ring_empty(ring)) { 782f8e4e36aSLuigi Rizzo uint32_t seq; 783f8e4e36aSLuigi Rizzo slot = &ring->slot[ring->cur]; 784f8e4e36aSLuigi Rizzo p = NETMAP_BUF(ring, slot->buf_idx); 785f8e4e36aSLuigi Rizzo 786f8e4e36aSLuigi Rizzo clock_gettime(CLOCK_REALTIME_PRECISE, &now); 787f8e4e36aSLuigi Rizzo bcopy(p+42, &seq, sizeof(seq)); 788f8e4e36aSLuigi Rizzo bcopy(p+46, &ts, sizeof(ts)); 789f8e4e36aSLuigi Rizzo ts.tv_sec = now.tv_sec - ts.tv_sec; 790f8e4e36aSLuigi Rizzo ts.tv_nsec = now.tv_nsec - ts.tv_nsec; 791f8e4e36aSLuigi Rizzo if (ts.tv_nsec < 0) { 792f8e4e36aSLuigi Rizzo ts.tv_nsec += 1000000000; 793f8e4e36aSLuigi Rizzo ts.tv_sec--; 794f8e4e36aSLuigi Rizzo } 795f8e4e36aSLuigi Rizzo if (1) D("seq %d/%d delta %d.%09d", seq, sent, 796f8e4e36aSLuigi Rizzo (int)ts.tv_sec, (int)ts.tv_nsec); 797f8e4e36aSLuigi Rizzo if (ts.tv_nsec < (int)min) 798f8e4e36aSLuigi Rizzo min = ts.tv_nsec; 799f8e4e36aSLuigi Rizzo count ++; 800f8e4e36aSLuigi Rizzo av += ts.tv_nsec; 80117885a7bSLuigi Rizzo ring->head = ring->cur = nm_ring_next(ring, ring->cur); 802f8e4e36aSLuigi Rizzo rx++; 803f8e4e36aSLuigi Rizzo } 804f8e4e36aSLuigi Rizzo } 805f8e4e36aSLuigi Rizzo //D("tx %d rx %d", sent, rx); 806f8e4e36aSLuigi Rizzo //usleep(100000); 807f8e4e36aSLuigi Rizzo ts.tv_sec = now.tv_sec - last_print.tv_sec; 808f8e4e36aSLuigi Rizzo ts.tv_nsec = now.tv_nsec - last_print.tv_nsec; 809f8e4e36aSLuigi Rizzo if (ts.tv_nsec < 0) { 810f8e4e36aSLuigi Rizzo ts.tv_nsec += 1000000000; 811f8e4e36aSLuigi Rizzo ts.tv_sec--; 812f8e4e36aSLuigi Rizzo } 813f8e4e36aSLuigi Rizzo if (ts.tv_sec >= 1) { 814f8e4e36aSLuigi Rizzo D("count %d min %d av %d", 815f8e4e36aSLuigi Rizzo count, min, av/count); 816f8e4e36aSLuigi Rizzo count = 0; 817f8e4e36aSLuigi Rizzo av = 0; 818f8e4e36aSLuigi Rizzo min = 100000000; 819f8e4e36aSLuigi Rizzo last_print = now; 820f8e4e36aSLuigi Rizzo } 821f8e4e36aSLuigi Rizzo } 822f8e4e36aSLuigi Rizzo return NULL; 823f8e4e36aSLuigi Rizzo } 824f8e4e36aSLuigi Rizzo 825f8e4e36aSLuigi Rizzo 826f8e4e36aSLuigi Rizzo /* 827f8e4e36aSLuigi Rizzo * reply to ping requests 828f8e4e36aSLuigi Rizzo */ 829f8e4e36aSLuigi Rizzo static void * 830f8e4e36aSLuigi Rizzo ponger_body(void *data) 831f8e4e36aSLuigi Rizzo { 832f8e4e36aSLuigi Rizzo struct targ *targ = (struct targ *) data; 833f0ea3689SLuigi Rizzo struct pollfd pfd = { .fd = targ->fd, .events = POLLIN }; 834f0ea3689SLuigi Rizzo struct netmap_if *nifp = targ->nmd->nifp; 835f8e4e36aSLuigi Rizzo struct netmap_ring *txring, *rxring; 836f8e4e36aSLuigi Rizzo int i, rx = 0, sent = 0, n = targ->g->npackets; 837f8e4e36aSLuigi Rizzo 838f8e4e36aSLuigi Rizzo if (targ->g->nthreads > 1) { 839f8e4e36aSLuigi Rizzo D("can only reply ping with 1 thread"); 840f8e4e36aSLuigi Rizzo return NULL; 841f8e4e36aSLuigi Rizzo } 842f8e4e36aSLuigi Rizzo D("understood ponger %d but don't know how to do it", n); 843f8e4e36aSLuigi Rizzo while (n == 0 || sent < n) { 844f8e4e36aSLuigi Rizzo uint32_t txcur, txavail; 845f8e4e36aSLuigi Rizzo //#define BUSYWAIT 846f8e4e36aSLuigi Rizzo #ifdef BUSYWAIT 847f0ea3689SLuigi Rizzo ioctl(pfd.fd, NIOCRXSYNC, NULL); 848f8e4e36aSLuigi Rizzo #else 849f0ea3689SLuigi Rizzo if (poll(&pfd, 1, 1000) <= 0) { 85017885a7bSLuigi Rizzo D("poll error/timeout on queue %d: %s", targ->me, 85117885a7bSLuigi Rizzo strerror(errno)); 852f8e4e36aSLuigi Rizzo continue; 853f8e4e36aSLuigi Rizzo } 854f8e4e36aSLuigi Rizzo #endif 855f8e4e36aSLuigi Rizzo txring = NETMAP_TXRING(nifp, 0); 856f8e4e36aSLuigi Rizzo txcur = txring->cur; 85717885a7bSLuigi Rizzo txavail = nm_ring_space(txring); 858f8e4e36aSLuigi Rizzo /* see what we got back */ 859f0ea3689SLuigi Rizzo for (i = targ->nmd->first_rx_ring; i <= targ->nmd->last_rx_ring; i++) { 860f8e4e36aSLuigi Rizzo rxring = NETMAP_RXRING(nifp, i); 86117885a7bSLuigi Rizzo while (!nm_ring_empty(rxring)) { 862f8e4e36aSLuigi Rizzo uint16_t *spkt, *dpkt; 863f8e4e36aSLuigi Rizzo uint32_t cur = rxring->cur; 864f8e4e36aSLuigi Rizzo struct netmap_slot *slot = &rxring->slot[cur]; 865f8e4e36aSLuigi Rizzo char *src, *dst; 866f8e4e36aSLuigi Rizzo src = NETMAP_BUF(rxring, slot->buf_idx); 867f8e4e36aSLuigi Rizzo //D("got pkt %p of size %d", src, slot->len); 86817885a7bSLuigi Rizzo rxring->head = rxring->cur = nm_ring_next(rxring, cur); 869f8e4e36aSLuigi Rizzo rx++; 870f8e4e36aSLuigi Rizzo if (txavail == 0) 871f8e4e36aSLuigi Rizzo continue; 872f8e4e36aSLuigi Rizzo dst = NETMAP_BUF(txring, 873f8e4e36aSLuigi Rizzo txring->slot[txcur].buf_idx); 874f8e4e36aSLuigi Rizzo /* copy... */ 875f8e4e36aSLuigi Rizzo dpkt = (uint16_t *)dst; 876f8e4e36aSLuigi Rizzo spkt = (uint16_t *)src; 877f0ea3689SLuigi Rizzo nm_pkt_copy(src, dst, slot->len); 878f8e4e36aSLuigi Rizzo dpkt[0] = spkt[3]; 879f8e4e36aSLuigi Rizzo dpkt[1] = spkt[4]; 880f8e4e36aSLuigi Rizzo dpkt[2] = spkt[5]; 881f8e4e36aSLuigi Rizzo dpkt[3] = spkt[0]; 882f8e4e36aSLuigi Rizzo dpkt[4] = spkt[1]; 883f8e4e36aSLuigi Rizzo dpkt[5] = spkt[2]; 884f8e4e36aSLuigi Rizzo txring->slot[txcur].len = slot->len; 885f8e4e36aSLuigi Rizzo /* XXX swap src dst mac */ 88617885a7bSLuigi Rizzo txcur = nm_ring_next(txring, txcur); 887f8e4e36aSLuigi Rizzo txavail--; 888f8e4e36aSLuigi Rizzo sent++; 889f8e4e36aSLuigi Rizzo } 890f8e4e36aSLuigi Rizzo } 89117885a7bSLuigi Rizzo txring->head = txring->cur = txcur; 892f8e4e36aSLuigi Rizzo targ->count = sent; 893f8e4e36aSLuigi Rizzo #ifdef BUSYWAIT 894f0ea3689SLuigi Rizzo ioctl(pfd.fd, NIOCTXSYNC, NULL); 895f8e4e36aSLuigi Rizzo #endif 896f8e4e36aSLuigi Rizzo //D("tx %d rx %d", sent, rx); 897f8e4e36aSLuigi Rizzo } 898f8e4e36aSLuigi Rizzo return NULL; 899f8e4e36aSLuigi Rizzo } 900f8e4e36aSLuigi Rizzo 9011cb4c501SLuigi Rizzo static __inline int 9021cb4c501SLuigi Rizzo timespec_ge(const struct timespec *a, const struct timespec *b) 9031cb4c501SLuigi Rizzo { 9041cb4c501SLuigi Rizzo 9051cb4c501SLuigi Rizzo if (a->tv_sec > b->tv_sec) 9061cb4c501SLuigi Rizzo return (1); 9071cb4c501SLuigi Rizzo if (a->tv_sec < b->tv_sec) 9081cb4c501SLuigi Rizzo return (0); 9091cb4c501SLuigi Rizzo if (a->tv_nsec >= b->tv_nsec) 9101cb4c501SLuigi Rizzo return (1); 9111cb4c501SLuigi Rizzo return (0); 9121cb4c501SLuigi Rizzo } 9131cb4c501SLuigi Rizzo 9141cb4c501SLuigi Rizzo static __inline struct timespec 9151cb4c501SLuigi Rizzo timeval2spec(const struct timeval *a) 9161cb4c501SLuigi Rizzo { 9171cb4c501SLuigi Rizzo struct timespec ts = { 9181cb4c501SLuigi Rizzo .tv_sec = a->tv_sec, 9191cb4c501SLuigi Rizzo .tv_nsec = a->tv_usec * 1000 9201cb4c501SLuigi Rizzo }; 9211cb4c501SLuigi Rizzo return ts; 9221cb4c501SLuigi Rizzo } 9231cb4c501SLuigi Rizzo 9241cb4c501SLuigi Rizzo static __inline struct timeval 9251cb4c501SLuigi Rizzo timespec2val(const struct timespec *a) 9261cb4c501SLuigi Rizzo { 9271cb4c501SLuigi Rizzo struct timeval tv = { 9281cb4c501SLuigi Rizzo .tv_sec = a->tv_sec, 9291cb4c501SLuigi Rizzo .tv_usec = a->tv_nsec / 1000 9301cb4c501SLuigi Rizzo }; 9311cb4c501SLuigi Rizzo return tv; 9321cb4c501SLuigi Rizzo } 9331cb4c501SLuigi Rizzo 9341cb4c501SLuigi Rizzo 93517885a7bSLuigi Rizzo static __inline struct timespec 93617885a7bSLuigi Rizzo timespec_add(struct timespec a, struct timespec b) 9371cb4c501SLuigi Rizzo { 93817885a7bSLuigi Rizzo struct timespec ret = { a.tv_sec + b.tv_sec, a.tv_nsec + b.tv_nsec }; 93917885a7bSLuigi Rizzo if (ret.tv_nsec >= 1000000000) { 94017885a7bSLuigi Rizzo ret.tv_sec++; 94117885a7bSLuigi Rizzo ret.tv_nsec -= 1000000000; 9421cb4c501SLuigi Rizzo } 94317885a7bSLuigi Rizzo return ret; 9441cb4c501SLuigi Rizzo } 9451cb4c501SLuigi Rizzo 94617885a7bSLuigi Rizzo static __inline struct timespec 94717885a7bSLuigi Rizzo timespec_sub(struct timespec a, struct timespec b) 9481cb4c501SLuigi Rizzo { 94917885a7bSLuigi Rizzo struct timespec ret = { a.tv_sec - b.tv_sec, a.tv_nsec - b.tv_nsec }; 95017885a7bSLuigi Rizzo if (ret.tv_nsec < 0) { 95117885a7bSLuigi Rizzo ret.tv_sec--; 95217885a7bSLuigi Rizzo ret.tv_nsec += 1000000000; 9531cb4c501SLuigi Rizzo } 95417885a7bSLuigi Rizzo return ret; 9551cb4c501SLuigi Rizzo } 9561cb4c501SLuigi Rizzo 957f8e4e36aSLuigi Rizzo 95817885a7bSLuigi Rizzo /* 95917885a7bSLuigi Rizzo * wait until ts, either busy or sleeping if more than 1ms. 96017885a7bSLuigi Rizzo * Return wakeup time. 96117885a7bSLuigi Rizzo */ 96217885a7bSLuigi Rizzo static struct timespec 96317885a7bSLuigi Rizzo wait_time(struct timespec ts) 96417885a7bSLuigi Rizzo { 96517885a7bSLuigi Rizzo for (;;) { 96617885a7bSLuigi Rizzo struct timespec w, cur; 96717885a7bSLuigi Rizzo clock_gettime(CLOCK_REALTIME_PRECISE, &cur); 96817885a7bSLuigi Rizzo w = timespec_sub(ts, cur); 96917885a7bSLuigi Rizzo if (w.tv_sec < 0) 97017885a7bSLuigi Rizzo return cur; 97117885a7bSLuigi Rizzo else if (w.tv_sec > 0 || w.tv_nsec > 1000000) 97217885a7bSLuigi Rizzo poll(NULL, 0, 1); 97317885a7bSLuigi Rizzo } 97417885a7bSLuigi Rizzo } 97517885a7bSLuigi Rizzo 976f8e4e36aSLuigi Rizzo static void * 977f8e4e36aSLuigi Rizzo sender_body(void *data) 978f8e4e36aSLuigi Rizzo { 979f8e4e36aSLuigi Rizzo struct targ *targ = (struct targ *) data; 980f0ea3689SLuigi Rizzo struct pollfd pfd = { .fd = targ->fd, .events = POLLOUT }; 981f0ea3689SLuigi Rizzo struct netmap_if *nifp = targ->nmd->nifp; 982f8e4e36aSLuigi Rizzo struct netmap_ring *txring; 983f0ea3689SLuigi Rizzo int i, n = targ->g->npackets / targ->g->nthreads; 984f0ea3689SLuigi Rizzo int64_t sent = 0; 985f8e4e36aSLuigi Rizzo int options = targ->g->options | OPT_COPY; 98617885a7bSLuigi Rizzo struct timespec nexttime = { 0, 0}; // XXX silence compiler 9871cb4c501SLuigi Rizzo int rate_limit = targ->g->tx_rate; 98817885a7bSLuigi Rizzo struct pkt *pkt = &targ->pkt; 98917885a7bSLuigi Rizzo void *frame; 99017885a7bSLuigi Rizzo int size; 99117885a7bSLuigi Rizzo 99217885a7bSLuigi Rizzo frame = pkt; 99317885a7bSLuigi Rizzo frame += sizeof(pkt->vh) - targ->g->virt_header; 99417885a7bSLuigi Rizzo size = targ->g->pkt_size + targ->g->virt_header; 995b303f675SLuigi Rizzo 996f8e4e36aSLuigi Rizzo D("start"); 99768b8534bSLuigi Rizzo if (setaffinity(targ->thread, targ->affinity)) 99868b8534bSLuigi Rizzo goto quit; 99968b8534bSLuigi Rizzo 100068b8534bSLuigi Rizzo /* main loop.*/ 10011cb4c501SLuigi Rizzo clock_gettime(CLOCK_REALTIME_PRECISE, &targ->tic); 10021cb4c501SLuigi Rizzo if (rate_limit) { 100317885a7bSLuigi Rizzo targ->tic = timespec_add(targ->tic, (struct timespec){2,0}); 10041cb4c501SLuigi Rizzo targ->tic.tv_nsec = 0; 100517885a7bSLuigi Rizzo wait_time(targ->tic); 10061cb4c501SLuigi Rizzo nexttime = targ->tic; 10071cb4c501SLuigi Rizzo } 1008f2637526SLuigi Rizzo if (targ->g->dev_type == DEV_TAP) { 1009f8e4e36aSLuigi Rizzo D("writing to file desc %d", targ->g->main_fd); 1010f8e4e36aSLuigi Rizzo 1011f8e4e36aSLuigi Rizzo for (i = 0; !targ->cancel && (n == 0 || sent < n); i++) { 101217885a7bSLuigi Rizzo if (write(targ->g->main_fd, frame, size) != -1) 1013f8e4e36aSLuigi Rizzo sent++; 1014ce3ee1e7SLuigi Rizzo update_addresses(pkt, targ->g); 1015f8e4e36aSLuigi Rizzo if (i > 10000) { 1016f8e4e36aSLuigi Rizzo targ->count = sent; 1017f8e4e36aSLuigi Rizzo i = 0; 1018f8e4e36aSLuigi Rizzo } 1019f8e4e36aSLuigi Rizzo } 1020f2637526SLuigi Rizzo #ifndef NO_PCAP 1021f2637526SLuigi Rizzo } else if (targ->g->dev_type == DEV_PCAP) { 1022f2637526SLuigi Rizzo pcap_t *p = targ->g->p; 1023f2637526SLuigi Rizzo 1024f2637526SLuigi Rizzo for (i = 0; !targ->cancel && (n == 0 || sent < n); i++) { 1025f2637526SLuigi Rizzo if (pcap_inject(p, frame, size) != -1) 1026f2637526SLuigi Rizzo sent++; 1027f2637526SLuigi Rizzo update_addresses(pkt, targ->g); 1028f2637526SLuigi Rizzo if (i > 10000) { 1029f2637526SLuigi Rizzo targ->count = sent; 1030f2637526SLuigi Rizzo i = 0; 1031f2637526SLuigi Rizzo } 1032f2637526SLuigi Rizzo } 1033f2637526SLuigi Rizzo #endif /* NO_PCAP */ 103468b8534bSLuigi Rizzo } else { 10351cb4c501SLuigi Rizzo int tosend = 0; 1036ce3ee1e7SLuigi Rizzo int frags = targ->g->frags; 1037ce3ee1e7SLuigi Rizzo 1038f8e4e36aSLuigi Rizzo while (!targ->cancel && (n == 0 || sent < n)) { 103968b8534bSLuigi Rizzo 10401cb4c501SLuigi Rizzo if (rate_limit && tosend <= 0) { 10411cb4c501SLuigi Rizzo tosend = targ->g->burst; 104217885a7bSLuigi Rizzo nexttime = timespec_add(nexttime, targ->g->tx_period); 104317885a7bSLuigi Rizzo wait_time(nexttime); 10441cb4c501SLuigi Rizzo } 10451cb4c501SLuigi Rizzo 104668b8534bSLuigi Rizzo /* 104768b8534bSLuigi Rizzo * wait for available room in the send queue(s) 104868b8534bSLuigi Rizzo */ 1049f0ea3689SLuigi Rizzo if (poll(&pfd, 1, 2000) <= 0) { 10503fe77e68SEd Maste if (targ->cancel) 10513fe77e68SEd Maste break; 105217885a7bSLuigi Rizzo D("poll error/timeout on queue %d: %s", targ->me, 105317885a7bSLuigi Rizzo strerror(errno)); 1054f0ea3689SLuigi Rizzo // goto quit; 105517885a7bSLuigi Rizzo } 1056f0ea3689SLuigi Rizzo if (pfd.revents & POLLERR) { 105717885a7bSLuigi Rizzo D("poll error"); 105868b8534bSLuigi Rizzo goto quit; 105968b8534bSLuigi Rizzo } 106068b8534bSLuigi Rizzo /* 106168b8534bSLuigi Rizzo * scan our queues and send on those with room 106268b8534bSLuigi Rizzo */ 1063f8e4e36aSLuigi Rizzo if (options & OPT_COPY && sent > 100000 && !(targ->g->options & OPT_COPY) ) { 1064f8e4e36aSLuigi Rizzo D("drop copy"); 106599fb123fSLuigi Rizzo options &= ~OPT_COPY; 1066f8e4e36aSLuigi Rizzo } 1067f0ea3689SLuigi Rizzo for (i = targ->nmd->first_tx_ring; i <= targ->nmd->last_tx_ring; i++) { 10681cb4c501SLuigi Rizzo int m, limit = rate_limit ? tosend : targ->g->burst; 1069f8e4e36aSLuigi Rizzo if (n > 0 && n - sent < limit) 1070f8e4e36aSLuigi Rizzo limit = n - sent; 107168b8534bSLuigi Rizzo txring = NETMAP_TXRING(nifp, i); 107217885a7bSLuigi Rizzo if (nm_ring_empty(txring)) 107368b8534bSLuigi Rizzo continue; 1074ce3ee1e7SLuigi Rizzo if (frags > 1) 1075ce3ee1e7SLuigi Rizzo limit = ((limit + frags - 1) / frags) * frags; 1076ce3ee1e7SLuigi Rizzo 107717885a7bSLuigi Rizzo m = send_packets(txring, pkt, frame, size, targ->g, 1078ce3ee1e7SLuigi Rizzo limit, options, frags); 1079f2637526SLuigi Rizzo ND("limit %d tail %d frags %d m %d", 1080f2637526SLuigi Rizzo limit, txring->tail, frags, m); 108168b8534bSLuigi Rizzo sent += m; 108268b8534bSLuigi Rizzo targ->count = sent; 1083ce3ee1e7SLuigi Rizzo if (rate_limit) { 1084ce3ee1e7SLuigi Rizzo tosend -= m; 1085ce3ee1e7SLuigi Rizzo if (tosend <= 0) 1086ce3ee1e7SLuigi Rizzo break; 1087ce3ee1e7SLuigi Rizzo } 108868b8534bSLuigi Rizzo } 108968b8534bSLuigi Rizzo } 109099fb123fSLuigi Rizzo /* flush any remaining packets */ 1091f0ea3689SLuigi Rizzo ioctl(pfd.fd, NIOCTXSYNC, NULL); 109268b8534bSLuigi Rizzo 109368b8534bSLuigi Rizzo /* final part: wait all the TX queues to be empty. */ 1094f0ea3689SLuigi Rizzo for (i = targ->nmd->first_tx_ring; i <= targ->nmd->last_tx_ring; i++) { 109568b8534bSLuigi Rizzo txring = NETMAP_TXRING(nifp, i); 109617885a7bSLuigi Rizzo while (nm_tx_pending(txring)) { 1097f0ea3689SLuigi Rizzo ioctl(pfd.fd, NIOCTXSYNC, NULL); 109868b8534bSLuigi Rizzo usleep(1); /* wait 1 tick */ 109968b8534bSLuigi Rizzo } 110068b8534bSLuigi Rizzo } 1101f2637526SLuigi Rizzo } /* end DEV_NETMAP */ 110268b8534bSLuigi Rizzo 11031cb4c501SLuigi Rizzo clock_gettime(CLOCK_REALTIME_PRECISE, &targ->toc); 110468b8534bSLuigi Rizzo targ->completed = 1; 110568b8534bSLuigi Rizzo targ->count = sent; 110668b8534bSLuigi Rizzo 110768b8534bSLuigi Rizzo quit: 110868b8534bSLuigi Rizzo /* reset the ``used`` flag. */ 110968b8534bSLuigi Rizzo targ->used = 0; 111068b8534bSLuigi Rizzo 111168b8534bSLuigi Rizzo return (NULL); 111268b8534bSLuigi Rizzo } 111368b8534bSLuigi Rizzo 111468b8534bSLuigi Rizzo 1115f2637526SLuigi Rizzo #ifndef NO_PCAP 111668b8534bSLuigi Rizzo static void 1117f8e4e36aSLuigi Rizzo receive_pcap(u_char *user, const struct pcap_pkthdr * h, 1118f8e4e36aSLuigi Rizzo const u_char * bytes) 111968b8534bSLuigi Rizzo { 112068b8534bSLuigi Rizzo int *count = (int *)user; 1121f8e4e36aSLuigi Rizzo (void)h; /* UNUSED */ 1122f8e4e36aSLuigi Rizzo (void)bytes; /* UNUSED */ 112368b8534bSLuigi Rizzo (*count)++; 112468b8534bSLuigi Rizzo } 1125f2637526SLuigi Rizzo #endif /* !NO_PCAP */ 112668b8534bSLuigi Rizzo 112768b8534bSLuigi Rizzo static int 1128b303f675SLuigi Rizzo receive_packets(struct netmap_ring *ring, u_int limit, int dump) 112968b8534bSLuigi Rizzo { 113017885a7bSLuigi Rizzo u_int cur, rx, n; 113168b8534bSLuigi Rizzo 113268b8534bSLuigi Rizzo cur = ring->cur; 113317885a7bSLuigi Rizzo n = nm_ring_space(ring); 113417885a7bSLuigi Rizzo if (n < limit) 113517885a7bSLuigi Rizzo limit = n; 113668b8534bSLuigi Rizzo for (rx = 0; rx < limit; rx++) { 113768b8534bSLuigi Rizzo struct netmap_slot *slot = &ring->slot[cur]; 113868b8534bSLuigi Rizzo char *p = NETMAP_BUF(ring, slot->buf_idx); 113968b8534bSLuigi Rizzo 1140b303f675SLuigi Rizzo if (dump) 1141b303f675SLuigi Rizzo dump_payload(p, slot->len, ring, cur); 114268b8534bSLuigi Rizzo 114317885a7bSLuigi Rizzo cur = nm_ring_next(ring, cur); 114468b8534bSLuigi Rizzo } 114517885a7bSLuigi Rizzo ring->head = ring->cur = cur; 114668b8534bSLuigi Rizzo 114768b8534bSLuigi Rizzo return (rx); 114868b8534bSLuigi Rizzo } 114968b8534bSLuigi Rizzo 115068b8534bSLuigi Rizzo static void * 115168b8534bSLuigi Rizzo receiver_body(void *data) 115268b8534bSLuigi Rizzo { 115368b8534bSLuigi Rizzo struct targ *targ = (struct targ *) data; 1154f0ea3689SLuigi Rizzo struct pollfd pfd = { .fd = targ->fd, .events = POLLIN }; 1155f0ea3689SLuigi Rizzo struct netmap_if *nifp = targ->nmd->nifp; 115668b8534bSLuigi Rizzo struct netmap_ring *rxring; 1157f8e4e36aSLuigi Rizzo int i; 1158f8e4e36aSLuigi Rizzo uint64_t received = 0; 115968b8534bSLuigi Rizzo 116068b8534bSLuigi Rizzo if (setaffinity(targ->thread, targ->affinity)) 116168b8534bSLuigi Rizzo goto quit; 116268b8534bSLuigi Rizzo 116368b8534bSLuigi Rizzo /* unbounded wait for the first packet. */ 1164f8e4e36aSLuigi Rizzo for (;;) { 1165f0ea3689SLuigi Rizzo i = poll(&pfd, 1, 1000); 1166f0ea3689SLuigi Rizzo if (i > 0 && !(pfd.revents & POLLERR)) 116768b8534bSLuigi Rizzo break; 1168f0ea3689SLuigi Rizzo RD(1, "waiting for initial packets, poll returns %d %d", 1169f0ea3689SLuigi Rizzo i, pfd.revents); 117068b8534bSLuigi Rizzo } 117168b8534bSLuigi Rizzo 117268b8534bSLuigi Rizzo /* main loop, exit after 1s silence */ 11731cb4c501SLuigi Rizzo clock_gettime(CLOCK_REALTIME_PRECISE, &targ->tic); 1174f2637526SLuigi Rizzo if (targ->g->dev_type == DEV_TAP) { 1175f8e4e36aSLuigi Rizzo D("reading from %s fd %d", targ->g->ifname, targ->g->main_fd); 1176f8e4e36aSLuigi Rizzo while (!targ->cancel) { 1177f8e4e36aSLuigi Rizzo char buf[2048]; 1178f8e4e36aSLuigi Rizzo /* XXX should we poll ? */ 1179f8e4e36aSLuigi Rizzo if (read(targ->g->main_fd, buf, sizeof(buf)) > 0) 1180f8e4e36aSLuigi Rizzo targ->count++; 1181f8e4e36aSLuigi Rizzo } 1182f2637526SLuigi Rizzo #ifndef NO_PCAP 1183f2637526SLuigi Rizzo } else if (targ->g->dev_type == DEV_PCAP) { 1184f2637526SLuigi Rizzo while (!targ->cancel) { 1185f2637526SLuigi Rizzo /* XXX should we poll ? */ 1186f2637526SLuigi Rizzo pcap_dispatch(targ->g->p, targ->g->burst, receive_pcap, NULL); 1187f2637526SLuigi Rizzo } 1188f2637526SLuigi Rizzo #endif /* !NO_PCAP */ 118968b8534bSLuigi Rizzo } else { 1190b303f675SLuigi Rizzo int dump = targ->g->options & OPT_DUMP; 11913fe77e68SEd Maste while (!targ->cancel) { 119268b8534bSLuigi Rizzo /* Once we started to receive packets, wait at most 1 seconds 119368b8534bSLuigi Rizzo before quitting. */ 1194f0ea3689SLuigi Rizzo if (poll(&pfd, 1, 1 * 1000) <= 0 && !targ->g->forever) { 11951cb4c501SLuigi Rizzo clock_gettime(CLOCK_REALTIME_PRECISE, &targ->toc); 11968ce070c1SUlrich Spörlein targ->toc.tv_sec -= 1; /* Subtract timeout time. */ 1197f0ea3689SLuigi Rizzo goto out; 119868b8534bSLuigi Rizzo } 119968b8534bSLuigi Rizzo 1200f0ea3689SLuigi Rizzo if (pfd.revents & POLLERR) { 120117885a7bSLuigi Rizzo D("poll err"); 120217885a7bSLuigi Rizzo goto quit; 120317885a7bSLuigi Rizzo } 120417885a7bSLuigi Rizzo 1205f0ea3689SLuigi Rizzo for (i = targ->nmd->first_rx_ring; i <= targ->nmd->last_rx_ring; i++) { 120668b8534bSLuigi Rizzo int m; 120768b8534bSLuigi Rizzo 120868b8534bSLuigi Rizzo rxring = NETMAP_RXRING(nifp, i); 120917885a7bSLuigi Rizzo if (nm_ring_empty(rxring)) 121068b8534bSLuigi Rizzo continue; 121168b8534bSLuigi Rizzo 1212b303f675SLuigi Rizzo m = receive_packets(rxring, targ->g->burst, dump); 121368b8534bSLuigi Rizzo received += m; 121468b8534bSLuigi Rizzo } 1215f8e4e36aSLuigi Rizzo targ->count = received; 121668b8534bSLuigi Rizzo } 121768b8534bSLuigi Rizzo } 121868b8534bSLuigi Rizzo 1219f0ea3689SLuigi Rizzo clock_gettime(CLOCK_REALTIME_PRECISE, &targ->toc); 1220f0ea3689SLuigi Rizzo 1221f0ea3689SLuigi Rizzo out: 122268b8534bSLuigi Rizzo targ->completed = 1; 122368b8534bSLuigi Rizzo targ->count = received; 122468b8534bSLuigi Rizzo 122568b8534bSLuigi Rizzo quit: 122668b8534bSLuigi Rizzo /* reset the ``used`` flag. */ 122768b8534bSLuigi Rizzo targ->used = 0; 122868b8534bSLuigi Rizzo 122968b8534bSLuigi Rizzo return (NULL); 123068b8534bSLuigi Rizzo } 123168b8534bSLuigi Rizzo 1232f8e4e36aSLuigi Rizzo /* very crude code to print a number in normalized form. 1233f8e4e36aSLuigi Rizzo * Caller has to make sure that the buffer is large enough. 1234f8e4e36aSLuigi Rizzo */ 1235f8e4e36aSLuigi Rizzo static const char * 1236f8e4e36aSLuigi Rizzo norm(char *buf, double val) 123766a698c9SEd Maste { 1238f0ea3689SLuigi Rizzo char *units[] = { "", "K", "M", "G", "T" }; 1239f8e4e36aSLuigi Rizzo u_int i; 124066a698c9SEd Maste 1241f0ea3689SLuigi Rizzo for (i = 0; val >=1000 && i < sizeof(units)/sizeof(char *) - 1; i++) 124266a698c9SEd Maste val /= 1000; 1243f8e4e36aSLuigi Rizzo sprintf(buf, "%.2f %s", val, units[i]); 1244f8e4e36aSLuigi Rizzo return buf; 124566a698c9SEd Maste } 124666a698c9SEd Maste 124768b8534bSLuigi Rizzo static void 124868b8534bSLuigi Rizzo tx_output(uint64_t sent, int size, double delta) 124968b8534bSLuigi Rizzo { 1250f8e4e36aSLuigi Rizzo double bw, raw_bw, pps; 1251f8e4e36aSLuigi Rizzo char b1[40], b2[80], b3[80]; 125268b8534bSLuigi Rizzo 1253f0ea3689SLuigi Rizzo printf("Sent %llu packets, %d bytes each, in %.2f seconds.\n", 1254f0ea3689SLuigi Rizzo (unsigned long long)sent, size, delta); 1255f8e4e36aSLuigi Rizzo if (delta == 0) 1256f8e4e36aSLuigi Rizzo delta = 1e-6; 1257f8e4e36aSLuigi Rizzo if (size < 60) /* correct for min packet size */ 1258f8e4e36aSLuigi Rizzo size = 60; 1259f8e4e36aSLuigi Rizzo pps = sent / delta; 1260f8e4e36aSLuigi Rizzo bw = (8.0 * size * sent) / delta; 1261f8e4e36aSLuigi Rizzo /* raw packets have4 bytes crc + 20 bytes framing */ 1262f8e4e36aSLuigi Rizzo raw_bw = (8.0 * (size + 24) * sent) / delta; 126366a698c9SEd Maste 1264f8e4e36aSLuigi Rizzo printf("Speed: %spps Bandwidth: %sbps (raw %sbps)\n", 1265f8e4e36aSLuigi Rizzo norm(b1, pps), norm(b2, bw), norm(b3, raw_bw) ); 126668b8534bSLuigi Rizzo } 126768b8534bSLuigi Rizzo 126868b8534bSLuigi Rizzo 126968b8534bSLuigi Rizzo static void 127068b8534bSLuigi Rizzo rx_output(uint64_t received, double delta) 127168b8534bSLuigi Rizzo { 1272f8e4e36aSLuigi Rizzo double pps; 1273f8e4e36aSLuigi Rizzo char b1[40]; 127468b8534bSLuigi Rizzo 1275f0ea3689SLuigi Rizzo printf("Received %llu packets, in %.2f seconds.\n", 1276f0ea3689SLuigi Rizzo (unsigned long long) received, delta); 1277f8e4e36aSLuigi Rizzo 1278f8e4e36aSLuigi Rizzo if (delta == 0) 1279f8e4e36aSLuigi Rizzo delta = 1e-6; 1280f8e4e36aSLuigi Rizzo pps = received / delta; 1281f8e4e36aSLuigi Rizzo printf("Speed: %spps\n", norm(b1, pps)); 128268b8534bSLuigi Rizzo } 128368b8534bSLuigi Rizzo 128468b8534bSLuigi Rizzo static void 128568b8534bSLuigi Rizzo usage(void) 128668b8534bSLuigi Rizzo { 128768b8534bSLuigi Rizzo const char *cmd = "pkt-gen"; 128868b8534bSLuigi Rizzo fprintf(stderr, 128968b8534bSLuigi Rizzo "Usage:\n" 129068b8534bSLuigi Rizzo "%s arguments\n" 129168b8534bSLuigi Rizzo "\t-i interface interface name\n" 1292f8e4e36aSLuigi Rizzo "\t-f function tx rx ping pong\n" 1293f8e4e36aSLuigi Rizzo "\t-n count number of iterations (can be 0)\n" 1294f8e4e36aSLuigi Rizzo "\t-t pkts_to_send also forces tx mode\n" 1295f8e4e36aSLuigi Rizzo "\t-r pkts_to_receive also forces rx mode\n" 1296ce3ee1e7SLuigi Rizzo "\t-l pkt_size in bytes excluding CRC\n" 1297ce3ee1e7SLuigi Rizzo "\t-d dst_ip[:port[-dst_ip:port]] single or range\n" 1298ce3ee1e7SLuigi Rizzo "\t-s src_ip[:port[-src_ip:port]] single or range\n" 1299ce3ee1e7SLuigi Rizzo "\t-D dst-mac\n" 1300ce3ee1e7SLuigi Rizzo "\t-S src-mac\n" 1301f8e4e36aSLuigi Rizzo "\t-a cpu_id use setaffinity\n" 130268b8534bSLuigi Rizzo "\t-b burst size testing, mostly\n" 130368b8534bSLuigi Rizzo "\t-c cores cores to use\n" 130468b8534bSLuigi Rizzo "\t-p threads processes/threads to use\n" 130568b8534bSLuigi Rizzo "\t-T report_ms milliseconds between reports\n" 1306f8e4e36aSLuigi Rizzo "\t-P use libpcap instead of netmap\n" 130768b8534bSLuigi Rizzo "\t-w wait_for_link_time in seconds\n" 1308ce3ee1e7SLuigi Rizzo "\t-R rate in packets per second\n" 1309ce3ee1e7SLuigi Rizzo "\t-X dump payload\n" 131017885a7bSLuigi Rizzo "\t-H len add empty virtio-net-header with size 'len'\n" 131168b8534bSLuigi Rizzo "", 131268b8534bSLuigi Rizzo cmd); 131368b8534bSLuigi Rizzo 131468b8534bSLuigi Rizzo exit(0); 131568b8534bSLuigi Rizzo } 131668b8534bSLuigi Rizzo 1317f8e4e36aSLuigi Rizzo static void 1318f8e4e36aSLuigi Rizzo start_threads(struct glob_arg *g) 1319f8e4e36aSLuigi Rizzo { 1320f8e4e36aSLuigi Rizzo int i; 1321f8e4e36aSLuigi Rizzo 1322f8e4e36aSLuigi Rizzo targs = calloc(g->nthreads, sizeof(*targs)); 1323f8e4e36aSLuigi Rizzo /* 1324f8e4e36aSLuigi Rizzo * Now create the desired number of threads, each one 1325f8e4e36aSLuigi Rizzo * using a single descriptor. 1326f8e4e36aSLuigi Rizzo */ 1327f8e4e36aSLuigi Rizzo for (i = 0; i < g->nthreads; i++) { 1328f0ea3689SLuigi Rizzo struct targ *t = &targs[i]; 1329f0ea3689SLuigi Rizzo 1330f0ea3689SLuigi Rizzo bzero(t, sizeof(*t)); 1331f0ea3689SLuigi Rizzo t->fd = -1; /* default, with pcap */ 1332f0ea3689SLuigi Rizzo t->g = g; 1333f8e4e36aSLuigi Rizzo 1334f8e4e36aSLuigi Rizzo if (g->dev_type == DEV_NETMAP) { 1335f0ea3689SLuigi Rizzo struct nm_desc nmd = *g->nmd; /* copy, we overwrite ringid */ 1336f8e4e36aSLuigi Rizzo 1337f0ea3689SLuigi Rizzo if (g->nthreads > 1) { 1338f0ea3689SLuigi Rizzo if (nmd.req.nr_flags != NR_REG_ALL_NIC) { 1339f0ea3689SLuigi Rizzo D("invalid nthreads mode %d", nmd.req.nr_flags); 1340f8e4e36aSLuigi Rizzo continue; 1341f8e4e36aSLuigi Rizzo } 1342f0ea3689SLuigi Rizzo nmd.req.nr_flags = NR_REG_ONE_NIC; 1343f0ea3689SLuigi Rizzo nmd.req.nr_ringid = i; 134417885a7bSLuigi Rizzo } 1345f0ea3689SLuigi Rizzo /* Only touch one of the rings (rx is already ok) */ 1346f0ea3689SLuigi Rizzo if (g->td_body == receiver_body) 1347f0ea3689SLuigi Rizzo nmd.req.nr_ringid |= NETMAP_NO_TX_POLL; 1348f8e4e36aSLuigi Rizzo 1349f0ea3689SLuigi Rizzo /* register interface. Override ifname and ringid etc. */ 1350f8e4e36aSLuigi Rizzo 1351f0ea3689SLuigi Rizzo t->nmd = nm_open(t->g->ifname, NULL, g->nmd_flags | 1352f0ea3689SLuigi Rizzo NM_OPEN_IFNAME | NM_OPEN_NO_MMAP, g->nmd); 1353f0ea3689SLuigi Rizzo if (t->nmd == NULL) { 1354f0ea3689SLuigi Rizzo D("Unable to open %s: %s", 1355f0ea3689SLuigi Rizzo t->g->ifname, strerror(errno)); 1356f8e4e36aSLuigi Rizzo continue; 1357f8e4e36aSLuigi Rizzo } 1358f0ea3689SLuigi Rizzo t->fd = t->nmd->fd; 1359f0ea3689SLuigi Rizzo 1360f8e4e36aSLuigi Rizzo } else { 1361f8e4e36aSLuigi Rizzo targs[i].fd = g->main_fd; 1362f8e4e36aSLuigi Rizzo } 1363f0ea3689SLuigi Rizzo t->used = 1; 1364f0ea3689SLuigi Rizzo t->me = i; 1365f8e4e36aSLuigi Rizzo if (g->affinity >= 0) { 1366f8e4e36aSLuigi Rizzo if (g->affinity < g->cpus) 1367f0ea3689SLuigi Rizzo t->affinity = g->affinity; 1368f8e4e36aSLuigi Rizzo else 1369f0ea3689SLuigi Rizzo t->affinity = i % g->cpus; 1370f0ea3689SLuigi Rizzo } else { 1371f0ea3689SLuigi Rizzo t->affinity = -1; 1372f0ea3689SLuigi Rizzo } 1373f8e4e36aSLuigi Rizzo /* default, init packets */ 1374f0ea3689SLuigi Rizzo initialize_packet(t); 1375f8e4e36aSLuigi Rizzo 1376f0ea3689SLuigi Rizzo if (pthread_create(&t->thread, NULL, g->td_body, t) == -1) { 137717885a7bSLuigi Rizzo D("Unable to create thread %d: %s", i, strerror(errno)); 1378f0ea3689SLuigi Rizzo t->used = 0; 1379f8e4e36aSLuigi Rizzo } 1380f8e4e36aSLuigi Rizzo } 1381f8e4e36aSLuigi Rizzo } 1382f8e4e36aSLuigi Rizzo 1383f8e4e36aSLuigi Rizzo static void 1384f8e4e36aSLuigi Rizzo main_thread(struct glob_arg *g) 1385f8e4e36aSLuigi Rizzo { 1386f8e4e36aSLuigi Rizzo int i; 1387f8e4e36aSLuigi Rizzo 1388f8e4e36aSLuigi Rizzo uint64_t prev = 0; 1389f8e4e36aSLuigi Rizzo uint64_t count = 0; 1390f8e4e36aSLuigi Rizzo double delta_t; 1391f8e4e36aSLuigi Rizzo struct timeval tic, toc; 1392f8e4e36aSLuigi Rizzo 1393f8e4e36aSLuigi Rizzo gettimeofday(&toc, NULL); 1394f8e4e36aSLuigi Rizzo for (;;) { 1395f8e4e36aSLuigi Rizzo struct timeval now, delta; 1396f8e4e36aSLuigi Rizzo uint64_t pps, usec, my_count, npkts; 1397f8e4e36aSLuigi Rizzo int done = 0; 1398f8e4e36aSLuigi Rizzo 1399f8e4e36aSLuigi Rizzo delta.tv_sec = g->report_interval/1000; 1400f8e4e36aSLuigi Rizzo delta.tv_usec = (g->report_interval%1000)*1000; 1401f8e4e36aSLuigi Rizzo select(0, NULL, NULL, NULL, &delta); 1402f8e4e36aSLuigi Rizzo gettimeofday(&now, NULL); 1403f8e4e36aSLuigi Rizzo timersub(&now, &toc, &toc); 1404f8e4e36aSLuigi Rizzo my_count = 0; 1405f8e4e36aSLuigi Rizzo for (i = 0; i < g->nthreads; i++) { 1406f8e4e36aSLuigi Rizzo my_count += targs[i].count; 1407f8e4e36aSLuigi Rizzo if (targs[i].used == 0) 1408f8e4e36aSLuigi Rizzo done++; 1409f8e4e36aSLuigi Rizzo } 1410f8e4e36aSLuigi Rizzo usec = toc.tv_sec* 1000000 + toc.tv_usec; 1411f8e4e36aSLuigi Rizzo if (usec < 10000) 1412f8e4e36aSLuigi Rizzo continue; 1413f8e4e36aSLuigi Rizzo npkts = my_count - prev; 1414f8e4e36aSLuigi Rizzo pps = (npkts*1000000 + usec/2) / usec; 1415f0ea3689SLuigi Rizzo D("%llu pps (%llu pkts in %llu usec)", 1416f0ea3689SLuigi Rizzo (unsigned long long)pps, 1417f0ea3689SLuigi Rizzo (unsigned long long)npkts, 1418f0ea3689SLuigi Rizzo (unsigned long long)usec); 1419f8e4e36aSLuigi Rizzo prev = my_count; 1420f8e4e36aSLuigi Rizzo toc = now; 1421f8e4e36aSLuigi Rizzo if (done == g->nthreads) 1422f8e4e36aSLuigi Rizzo break; 1423f8e4e36aSLuigi Rizzo } 1424f8e4e36aSLuigi Rizzo 1425f8e4e36aSLuigi Rizzo timerclear(&tic); 1426f8e4e36aSLuigi Rizzo timerclear(&toc); 1427f8e4e36aSLuigi Rizzo for (i = 0; i < g->nthreads; i++) { 14281cb4c501SLuigi Rizzo struct timespec t_tic, t_toc; 1429f8e4e36aSLuigi Rizzo /* 1430f8e4e36aSLuigi Rizzo * Join active threads, unregister interfaces and close 1431f8e4e36aSLuigi Rizzo * file descriptors. 1432f8e4e36aSLuigi Rizzo */ 14331cb4c501SLuigi Rizzo if (targs[i].used) 1434f8e4e36aSLuigi Rizzo pthread_join(targs[i].thread, NULL); 1435f8e4e36aSLuigi Rizzo close(targs[i].fd); 1436f8e4e36aSLuigi Rizzo 1437f8e4e36aSLuigi Rizzo if (targs[i].completed == 0) 1438f8e4e36aSLuigi Rizzo D("ouch, thread %d exited with error", i); 1439f8e4e36aSLuigi Rizzo 1440f8e4e36aSLuigi Rizzo /* 1441f8e4e36aSLuigi Rizzo * Collect threads output and extract information about 1442f8e4e36aSLuigi Rizzo * how long it took to send all the packets. 1443f8e4e36aSLuigi Rizzo */ 1444f8e4e36aSLuigi Rizzo count += targs[i].count; 14451cb4c501SLuigi Rizzo t_tic = timeval2spec(&tic); 14461cb4c501SLuigi Rizzo t_toc = timeval2spec(&toc); 14471cb4c501SLuigi Rizzo if (!timerisset(&tic) || timespec_ge(&targs[i].tic, &t_tic)) 14481cb4c501SLuigi Rizzo tic = timespec2val(&targs[i].tic); 14491cb4c501SLuigi Rizzo if (!timerisset(&toc) || timespec_ge(&targs[i].toc, &t_toc)) 14501cb4c501SLuigi Rizzo toc = timespec2val(&targs[i].toc); 1451f8e4e36aSLuigi Rizzo } 1452f8e4e36aSLuigi Rizzo 1453f8e4e36aSLuigi Rizzo /* print output. */ 1454f8e4e36aSLuigi Rizzo timersub(&toc, &tic, &toc); 1455f8e4e36aSLuigi Rizzo delta_t = toc.tv_sec + 1e-6* toc.tv_usec; 1456f8e4e36aSLuigi Rizzo if (g->td_body == sender_body) 1457f8e4e36aSLuigi Rizzo tx_output(count, g->pkt_size, delta_t); 1458f8e4e36aSLuigi Rizzo else 1459f8e4e36aSLuigi Rizzo rx_output(count, delta_t); 1460f8e4e36aSLuigi Rizzo 1461f8e4e36aSLuigi Rizzo if (g->dev_type == DEV_NETMAP) { 1462f0ea3689SLuigi Rizzo munmap(g->nmd->mem, g->nmd->req.nr_memsize); 1463f8e4e36aSLuigi Rizzo close(g->main_fd); 1464f8e4e36aSLuigi Rizzo } 1465f8e4e36aSLuigi Rizzo } 1466f8e4e36aSLuigi Rizzo 1467f8e4e36aSLuigi Rizzo 1468f8e4e36aSLuigi Rizzo struct sf { 1469f8e4e36aSLuigi Rizzo char *key; 1470f8e4e36aSLuigi Rizzo void *f; 1471f8e4e36aSLuigi Rizzo }; 1472f8e4e36aSLuigi Rizzo 1473f8e4e36aSLuigi Rizzo static struct sf func[] = { 1474f8e4e36aSLuigi Rizzo { "tx", sender_body }, 1475f8e4e36aSLuigi Rizzo { "rx", receiver_body }, 1476f8e4e36aSLuigi Rizzo { "ping", pinger_body }, 1477f8e4e36aSLuigi Rizzo { "pong", ponger_body }, 1478f8e4e36aSLuigi Rizzo { NULL, NULL } 1479f8e4e36aSLuigi Rizzo }; 1480f8e4e36aSLuigi Rizzo 1481f8e4e36aSLuigi Rizzo static int 1482f8e4e36aSLuigi Rizzo tap_alloc(char *dev) 1483f8e4e36aSLuigi Rizzo { 1484f8e4e36aSLuigi Rizzo struct ifreq ifr; 1485f8e4e36aSLuigi Rizzo int fd, err; 1486f8e4e36aSLuigi Rizzo char *clonedev = TAP_CLONEDEV; 1487f8e4e36aSLuigi Rizzo 1488f8e4e36aSLuigi Rizzo (void)err; 1489f8e4e36aSLuigi Rizzo (void)dev; 1490f8e4e36aSLuigi Rizzo /* Arguments taken by the function: 1491f8e4e36aSLuigi Rizzo * 1492f8e4e36aSLuigi Rizzo * char *dev: the name of an interface (or '\0'). MUST have enough 1493f8e4e36aSLuigi Rizzo * space to hold the interface name if '\0' is passed 1494f8e4e36aSLuigi Rizzo * int flags: interface flags (eg, IFF_TUN etc.) 1495f8e4e36aSLuigi Rizzo */ 1496f8e4e36aSLuigi Rizzo 1497f8e4e36aSLuigi Rizzo #ifdef __FreeBSD__ 1498f8e4e36aSLuigi Rizzo if (dev[3]) { /* tapSomething */ 1499f8e4e36aSLuigi Rizzo static char buf[128]; 1500f8e4e36aSLuigi Rizzo snprintf(buf, sizeof(buf), "/dev/%s", dev); 1501f8e4e36aSLuigi Rizzo clonedev = buf; 1502f8e4e36aSLuigi Rizzo } 1503f8e4e36aSLuigi Rizzo #endif 1504f8e4e36aSLuigi Rizzo /* open the device */ 1505f8e4e36aSLuigi Rizzo if( (fd = open(clonedev, O_RDWR)) < 0 ) { 1506f8e4e36aSLuigi Rizzo return fd; 1507f8e4e36aSLuigi Rizzo } 1508f8e4e36aSLuigi Rizzo D("%s open successful", clonedev); 1509f8e4e36aSLuigi Rizzo 1510f8e4e36aSLuigi Rizzo /* preparation of the struct ifr, of type "struct ifreq" */ 1511f8e4e36aSLuigi Rizzo memset(&ifr, 0, sizeof(ifr)); 1512f8e4e36aSLuigi Rizzo 1513f8e4e36aSLuigi Rizzo #ifdef linux 1514f8e4e36aSLuigi Rizzo ifr.ifr_flags = IFF_TAP | IFF_NO_PI; 1515f8e4e36aSLuigi Rizzo 1516f8e4e36aSLuigi Rizzo if (*dev) { 1517f8e4e36aSLuigi Rizzo /* if a device name was specified, put it in the structure; otherwise, 1518f8e4e36aSLuigi Rizzo * the kernel will try to allocate the "next" device of the 1519f8e4e36aSLuigi Rizzo * specified type */ 1520f8e4e36aSLuigi Rizzo strncpy(ifr.ifr_name, dev, IFNAMSIZ); 1521f8e4e36aSLuigi Rizzo } 1522f8e4e36aSLuigi Rizzo 1523f8e4e36aSLuigi Rizzo /* try to create the device */ 1524f8e4e36aSLuigi Rizzo if( (err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0 ) { 152517885a7bSLuigi Rizzo D("failed to to a TUNSETIFF: %s", strerror(errno)); 1526f8e4e36aSLuigi Rizzo close(fd); 1527f8e4e36aSLuigi Rizzo return err; 1528f8e4e36aSLuigi Rizzo } 1529f8e4e36aSLuigi Rizzo 1530f8e4e36aSLuigi Rizzo /* if the operation was successful, write back the name of the 1531f8e4e36aSLuigi Rizzo * interface to the variable "dev", so the caller can know 1532f8e4e36aSLuigi Rizzo * it. Note that the caller MUST reserve space in *dev (see calling 1533f8e4e36aSLuigi Rizzo * code below) */ 1534f8e4e36aSLuigi Rizzo strcpy(dev, ifr.ifr_name); 1535f8e4e36aSLuigi Rizzo D("new name is %s", dev); 1536f8e4e36aSLuigi Rizzo #endif /* linux */ 1537f8e4e36aSLuigi Rizzo 1538f8e4e36aSLuigi Rizzo /* this is the special file descriptor that the caller will use to talk 1539f8e4e36aSLuigi Rizzo * with the virtual interface */ 1540f8e4e36aSLuigi Rizzo return fd; 1541f8e4e36aSLuigi Rizzo } 154268b8534bSLuigi Rizzo 154368b8534bSLuigi Rizzo int 154468b8534bSLuigi Rizzo main(int arc, char **argv) 154568b8534bSLuigi Rizzo { 1546f8e4e36aSLuigi Rizzo int i; 154768b8534bSLuigi Rizzo 154868b8534bSLuigi Rizzo struct glob_arg g; 154968b8534bSLuigi Rizzo 155068b8534bSLuigi Rizzo int ch; 155168b8534bSLuigi Rizzo int wait_link = 2; 155268b8534bSLuigi Rizzo int devqueues = 1; /* how many device queues */ 155368b8534bSLuigi Rizzo 155468b8534bSLuigi Rizzo bzero(&g, sizeof(g)); 155568b8534bSLuigi Rizzo 1556f8e4e36aSLuigi Rizzo g.main_fd = -1; 1557f8e4e36aSLuigi Rizzo g.td_body = receiver_body; 1558f8e4e36aSLuigi Rizzo g.report_interval = 1000; /* report interval */ 1559f8e4e36aSLuigi Rizzo g.affinity = -1; 1560f8e4e36aSLuigi Rizzo /* ip addresses can also be a range x.x.x.x-x.x.x.y */ 1561f8e4e36aSLuigi Rizzo g.src_ip.name = "10.0.0.1"; 1562f8e4e36aSLuigi Rizzo g.dst_ip.name = "10.1.0.1"; 1563f8e4e36aSLuigi Rizzo g.dst_mac.name = "ff:ff:ff:ff:ff:ff"; 1564f8e4e36aSLuigi Rizzo g.src_mac.name = NULL; 156568b8534bSLuigi Rizzo g.pkt_size = 60; 156668b8534bSLuigi Rizzo g.burst = 512; // default 156768b8534bSLuigi Rizzo g.nthreads = 1; 156868b8534bSLuigi Rizzo g.cpus = 1; 1569b303f675SLuigi Rizzo g.forever = 1; 15701cb4c501SLuigi Rizzo g.tx_rate = 0; 1571ce3ee1e7SLuigi Rizzo g.frags = 1; 1572ce3ee1e7SLuigi Rizzo g.nmr_config = ""; 157317885a7bSLuigi Rizzo g.virt_header = 0; 157468b8534bSLuigi Rizzo 157568b8534bSLuigi Rizzo while ( (ch = getopt(arc, argv, 1576f0ea3689SLuigi Rizzo "a:f:F:n:i:Il:d:s:D:S:b:c:o:p:T:w:WvR:XC:H:e:")) != -1) { 1577f8e4e36aSLuigi Rizzo struct sf *fn; 1578f8e4e36aSLuigi Rizzo 157968b8534bSLuigi Rizzo switch(ch) { 158068b8534bSLuigi Rizzo default: 158168b8534bSLuigi Rizzo D("bad option %c %s", ch, optarg); 158268b8534bSLuigi Rizzo usage(); 158368b8534bSLuigi Rizzo break; 1584f8e4e36aSLuigi Rizzo 1585f8e4e36aSLuigi Rizzo case 'n': 1586f8e4e36aSLuigi Rizzo g.npackets = atoi(optarg); 1587f8e4e36aSLuigi Rizzo break; 1588f8e4e36aSLuigi Rizzo 1589ce3ee1e7SLuigi Rizzo case 'F': 1590ce3ee1e7SLuigi Rizzo i = atoi(optarg); 1591ce3ee1e7SLuigi Rizzo if (i < 1 || i > 63) { 1592ce3ee1e7SLuigi Rizzo D("invalid frags %d [1..63], ignore", i); 1593ce3ee1e7SLuigi Rizzo break; 1594ce3ee1e7SLuigi Rizzo } 1595ce3ee1e7SLuigi Rizzo g.frags = i; 1596ce3ee1e7SLuigi Rizzo break; 1597ce3ee1e7SLuigi Rizzo 1598f8e4e36aSLuigi Rizzo case 'f': 1599f8e4e36aSLuigi Rizzo for (fn = func; fn->key; fn++) { 1600f8e4e36aSLuigi Rizzo if (!strcmp(fn->key, optarg)) 1601f8e4e36aSLuigi Rizzo break; 1602f8e4e36aSLuigi Rizzo } 1603f8e4e36aSLuigi Rizzo if (fn->key) 1604f8e4e36aSLuigi Rizzo g.td_body = fn->f; 1605f8e4e36aSLuigi Rizzo else 1606f8e4e36aSLuigi Rizzo D("unrecognised function %s", optarg); 1607f8e4e36aSLuigi Rizzo break; 1608f8e4e36aSLuigi Rizzo 1609f8e4e36aSLuigi Rizzo case 'o': /* data generation options */ 161099fb123fSLuigi Rizzo g.options = atoi(optarg); 161199fb123fSLuigi Rizzo break; 1612f8e4e36aSLuigi Rizzo 1613f8e4e36aSLuigi Rizzo case 'a': /* force affinity */ 1614f8e4e36aSLuigi Rizzo g.affinity = atoi(optarg); 1615f8e4e36aSLuigi Rizzo break; 1616f8e4e36aSLuigi Rizzo 161768b8534bSLuigi Rizzo case 'i': /* interface */ 1618f2637526SLuigi Rizzo /* a prefix of tap: netmap: or pcap: forces the mode. 1619f2637526SLuigi Rizzo * otherwise we guess 1620f2637526SLuigi Rizzo */ 1621f2637526SLuigi Rizzo D("interface is %s", optarg); 1622f0ea3689SLuigi Rizzo if (strlen(optarg) > MAX_IFNAMELEN - 8) { 1623f0ea3689SLuigi Rizzo D("ifname too long %s", optarg); 1624f0ea3689SLuigi Rizzo break; 1625f0ea3689SLuigi Rizzo } 1626f0ea3689SLuigi Rizzo strcpy(g.ifname, optarg); 1627f2637526SLuigi Rizzo if (!strcmp(optarg, "null")) { 1628f8e4e36aSLuigi Rizzo g.dev_type = DEV_NETMAP; 1629ce3ee1e7SLuigi Rizzo g.dummy_send = 1; 1630f2637526SLuigi Rizzo } else if (!strncmp(optarg, "tap:", 4)) { 1631f2637526SLuigi Rizzo g.dev_type = DEV_TAP; 1632f0ea3689SLuigi Rizzo strcpy(g.ifname, optarg + 4); 1633f2637526SLuigi Rizzo } else if (!strncmp(optarg, "pcap:", 5)) { 1634f2637526SLuigi Rizzo g.dev_type = DEV_PCAP; 1635f0ea3689SLuigi Rizzo strcpy(g.ifname, optarg + 5); 1636f0ea3689SLuigi Rizzo } else if (!strncmp(optarg, "netmap:", 7) || 1637f0ea3689SLuigi Rizzo !strncmp(optarg, "vale", 4)) { 1638f2637526SLuigi Rizzo g.dev_type = DEV_NETMAP; 1639f2637526SLuigi Rizzo } else if (!strncmp(optarg, "tap", 3)) { 1640f2637526SLuigi Rizzo g.dev_type = DEV_TAP; 1641f0ea3689SLuigi Rizzo } else { /* prepend netmap: */ 1642f2637526SLuigi Rizzo g.dev_type = DEV_NETMAP; 1643f0ea3689SLuigi Rizzo sprintf(g.ifname, "netmap:%s", optarg); 1644f2637526SLuigi Rizzo } 164568b8534bSLuigi Rizzo break; 1646f8e4e36aSLuigi Rizzo 1647b303f675SLuigi Rizzo case 'I': 1648b303f675SLuigi Rizzo g.options |= OPT_INDIRECT; /* XXX use indirect buffer */ 1649b303f675SLuigi Rizzo break; 1650b303f675SLuigi Rizzo 165168b8534bSLuigi Rizzo case 'l': /* pkt_size */ 165268b8534bSLuigi Rizzo g.pkt_size = atoi(optarg); 165368b8534bSLuigi Rizzo break; 1654f8e4e36aSLuigi Rizzo 165568b8534bSLuigi Rizzo case 'd': 1656f8e4e36aSLuigi Rizzo g.dst_ip.name = optarg; 165768b8534bSLuigi Rizzo break; 1658f8e4e36aSLuigi Rizzo 165968b8534bSLuigi Rizzo case 's': 1660f8e4e36aSLuigi Rizzo g.src_ip.name = optarg; 166168b8534bSLuigi Rizzo break; 1662f8e4e36aSLuigi Rizzo 166368b8534bSLuigi Rizzo case 'T': /* report interval */ 1664f8e4e36aSLuigi Rizzo g.report_interval = atoi(optarg); 166568b8534bSLuigi Rizzo break; 1666f8e4e36aSLuigi Rizzo 166768b8534bSLuigi Rizzo case 'w': 166868b8534bSLuigi Rizzo wait_link = atoi(optarg); 166968b8534bSLuigi Rizzo break; 1670f8e4e36aSLuigi Rizzo 1671b303f675SLuigi Rizzo case 'W': /* XXX changed default */ 1672b303f675SLuigi Rizzo g.forever = 0; /* do not exit rx even with no traffic */ 1673f8e4e36aSLuigi Rizzo break; 1674f8e4e36aSLuigi Rizzo 167568b8534bSLuigi Rizzo case 'b': /* burst */ 167668b8534bSLuigi Rizzo g.burst = atoi(optarg); 167768b8534bSLuigi Rizzo break; 167868b8534bSLuigi Rizzo case 'c': 167968b8534bSLuigi Rizzo g.cpus = atoi(optarg); 168068b8534bSLuigi Rizzo break; 168168b8534bSLuigi Rizzo case 'p': 168268b8534bSLuigi Rizzo g.nthreads = atoi(optarg); 168368b8534bSLuigi Rizzo break; 168468b8534bSLuigi Rizzo 168568b8534bSLuigi Rizzo case 'D': /* destination mac */ 1686f8e4e36aSLuigi Rizzo g.dst_mac.name = optarg; 168768b8534bSLuigi Rizzo break; 1688f8e4e36aSLuigi Rizzo 168968b8534bSLuigi Rizzo case 'S': /* source mac */ 1690f8e4e36aSLuigi Rizzo g.src_mac.name = optarg; 169168b8534bSLuigi Rizzo break; 169268b8534bSLuigi Rizzo case 'v': 169368b8534bSLuigi Rizzo verbose++; 16941cb4c501SLuigi Rizzo break; 16951cb4c501SLuigi Rizzo case 'R': 16961cb4c501SLuigi Rizzo g.tx_rate = atoi(optarg); 16971cb4c501SLuigi Rizzo break; 1698b303f675SLuigi Rizzo case 'X': 1699b303f675SLuigi Rizzo g.options |= OPT_DUMP; 1700ce3ee1e7SLuigi Rizzo break; 1701ce3ee1e7SLuigi Rizzo case 'C': 1702ce3ee1e7SLuigi Rizzo g.nmr_config = strdup(optarg); 170317885a7bSLuigi Rizzo break; 170417885a7bSLuigi Rizzo case 'H': 170517885a7bSLuigi Rizzo g.virt_header = atoi(optarg); 1706f2637526SLuigi Rizzo break; 1707f0ea3689SLuigi Rizzo case 'e': /* extra bufs */ 1708f0ea3689SLuigi Rizzo g.extra_bufs = atoi(optarg); 1709f2637526SLuigi Rizzo break; 171068b8534bSLuigi Rizzo } 171168b8534bSLuigi Rizzo } 171268b8534bSLuigi Rizzo 1713f8e4e36aSLuigi Rizzo if (g.ifname == NULL) { 171468b8534bSLuigi Rizzo D("missing ifname"); 171568b8534bSLuigi Rizzo usage(); 171668b8534bSLuigi Rizzo } 1717f8e4e36aSLuigi Rizzo 1718f8e4e36aSLuigi Rizzo i = system_ncpus(); 1719f8e4e36aSLuigi Rizzo if (g.cpus < 0 || g.cpus > i) { 1720f8e4e36aSLuigi Rizzo D("%d cpus is too high, have only %d cpus", g.cpus, i); 172168b8534bSLuigi Rizzo usage(); 172268b8534bSLuigi Rizzo } 172368b8534bSLuigi Rizzo if (g.cpus == 0) 1724f8e4e36aSLuigi Rizzo g.cpus = i; 1725f8e4e36aSLuigi Rizzo 172668b8534bSLuigi Rizzo if (g.pkt_size < 16 || g.pkt_size > 1536) { 172768b8534bSLuigi Rizzo D("bad pktsize %d\n", g.pkt_size); 172868b8534bSLuigi Rizzo usage(); 172968b8534bSLuigi Rizzo } 173068b8534bSLuigi Rizzo 1731f8e4e36aSLuigi Rizzo if (g.src_mac.name == NULL) { 1732f8e4e36aSLuigi Rizzo static char mybuf[20] = "00:00:00:00:00:00"; 173399fb123fSLuigi Rizzo /* retrieve source mac address. */ 1734f8e4e36aSLuigi Rizzo if (source_hwaddr(g.ifname, mybuf) == -1) { 173599fb123fSLuigi Rizzo D("Unable to retrieve source mac"); 173699fb123fSLuigi Rizzo // continue, fail later 173799fb123fSLuigi Rizzo } 1738f8e4e36aSLuigi Rizzo g.src_mac.name = mybuf; 173999fb123fSLuigi Rizzo } 1740f8e4e36aSLuigi Rizzo /* extract address ranges */ 1741f8e4e36aSLuigi Rizzo extract_ip_range(&g.src_ip); 1742f8e4e36aSLuigi Rizzo extract_ip_range(&g.dst_ip); 1743f8e4e36aSLuigi Rizzo extract_mac_range(&g.src_mac); 1744f8e4e36aSLuigi Rizzo extract_mac_range(&g.dst_mac); 174599fb123fSLuigi Rizzo 1746f2637526SLuigi Rizzo if (g.src_ip.start != g.src_ip.end || 1747f2637526SLuigi Rizzo g.src_ip.port0 != g.src_ip.port1 || 1748f2637526SLuigi Rizzo g.dst_ip.start != g.dst_ip.end || 1749f2637526SLuigi Rizzo g.dst_ip.port0 != g.dst_ip.port1) 1750f2637526SLuigi Rizzo g.options |= OPT_COPY; 1751f2637526SLuigi Rizzo 175217885a7bSLuigi Rizzo if (g.virt_header != 0 && g.virt_header != VIRT_HDR_1 175317885a7bSLuigi Rizzo && g.virt_header != VIRT_HDR_2) { 175417885a7bSLuigi Rizzo D("bad virtio-net-header length"); 175517885a7bSLuigi Rizzo usage(); 175617885a7bSLuigi Rizzo } 175717885a7bSLuigi Rizzo 1758f8e4e36aSLuigi Rizzo if (g.dev_type == DEV_TAP) { 1759f8e4e36aSLuigi Rizzo D("want to use tap %s", g.ifname); 1760f8e4e36aSLuigi Rizzo g.main_fd = tap_alloc(g.ifname); 1761f8e4e36aSLuigi Rizzo if (g.main_fd < 0) { 1762f8e4e36aSLuigi Rizzo D("cannot open tap %s", g.ifname); 176399fb123fSLuigi Rizzo usage(); 176499fb123fSLuigi Rizzo } 1765f2637526SLuigi Rizzo #ifndef NO_PCAP 1766f2637526SLuigi Rizzo } else if (g.dev_type == DEV_PCAP) { 1767f8e4e36aSLuigi Rizzo char pcap_errbuf[PCAP_ERRBUF_SIZE]; 1768f8e4e36aSLuigi Rizzo 1769f8e4e36aSLuigi Rizzo D("using pcap on %s", g.ifname); 1770f8e4e36aSLuigi Rizzo pcap_errbuf[0] = '\0'; // init the buffer 1771f8e4e36aSLuigi Rizzo g.p = pcap_open_live(g.ifname, 0, 1, 100, pcap_errbuf); 1772f8e4e36aSLuigi Rizzo if (g.p == NULL) { 1773f8e4e36aSLuigi Rizzo D("cannot open pcap on %s", g.ifname); 1774f8e4e36aSLuigi Rizzo usage(); 1775f8e4e36aSLuigi Rizzo } 1776f2637526SLuigi Rizzo #endif /* !NO_PCAP */ 1777f2637526SLuigi Rizzo } else if (g.dummy_send) { /* but DEV_NETMAP */ 1778ce3ee1e7SLuigi Rizzo D("using a dummy send routine"); 177999fb123fSLuigi Rizzo } else { 1780f0ea3689SLuigi Rizzo struct nm_desc base_nmd; 1781f0ea3689SLuigi Rizzo 1782f0ea3689SLuigi Rizzo bzero(&base_nmd, sizeof(base_nmd)); 1783f0ea3689SLuigi Rizzo 1784f0ea3689SLuigi Rizzo g.nmd_flags = 0; 1785f0ea3689SLuigi Rizzo g.nmd_flags |= parse_nmr_config(g.nmr_config, &base_nmd.req); 1786f0ea3689SLuigi Rizzo if (g.extra_bufs) { 1787f0ea3689SLuigi Rizzo base_nmd.req.nr_arg3 = g.extra_bufs; 1788f0ea3689SLuigi Rizzo g.nmd_flags |= NM_OPEN_ARG3; 1789f0ea3689SLuigi Rizzo } 1790f0ea3689SLuigi Rizzo 179168b8534bSLuigi Rizzo /* 1792f0ea3689SLuigi Rizzo * Open the netmap device using nm_open(). 179368b8534bSLuigi Rizzo * 179468b8534bSLuigi Rizzo * protocol stack and may cause a reset of the card, 179568b8534bSLuigi Rizzo * which in turn may take some time for the PHY to 1796f0ea3689SLuigi Rizzo * reconfigure. We do the open here to have time to reset. 179768b8534bSLuigi Rizzo */ 1798f0ea3689SLuigi Rizzo g.nmd = nm_open(g.ifname, NULL, g.nmd_flags, &base_nmd); 1799f0ea3689SLuigi Rizzo if (g.nmd == NULL) { 1800f0ea3689SLuigi Rizzo D("Unable to open %s: %s", g.ifname, strerror(errno)); 1801f0ea3689SLuigi Rizzo goto out; 180268b8534bSLuigi Rizzo } 1803f0ea3689SLuigi Rizzo g.main_fd = g.nmd->fd; 1804f0ea3689SLuigi Rizzo D("mapped %dKB at %p", g.nmd->req.nr_memsize>>10, g.nmd->mem); 1805f0ea3689SLuigi Rizzo 1806f0ea3689SLuigi Rizzo devqueues = g.nmd->req.nr_rx_rings; 180768b8534bSLuigi Rizzo 180868b8534bSLuigi Rizzo /* validate provided nthreads. */ 180968b8534bSLuigi Rizzo if (g.nthreads < 1 || g.nthreads > devqueues) { 181068b8534bSLuigi Rizzo D("bad nthreads %d, have %d queues", g.nthreads, devqueues); 181168b8534bSLuigi Rizzo // continue, fail later 181268b8534bSLuigi Rizzo } 181368b8534bSLuigi Rizzo 1814f2637526SLuigi Rizzo if (verbose) { 1815f0ea3689SLuigi Rizzo struct netmap_if *nifp = g.nmd->nifp; 1816f0ea3689SLuigi Rizzo struct nmreq *req = &g.nmd->req; 181768b8534bSLuigi Rizzo 1818f0ea3689SLuigi Rizzo D("nifp at offset %d, %d tx %d rx region %d", 1819f0ea3689SLuigi Rizzo req->nr_offset, req->nr_tx_rings, req->nr_rx_rings, 1820f0ea3689SLuigi Rizzo req->nr_arg2); 1821f0ea3689SLuigi Rizzo for (i = 0; i <= req->nr_tx_rings; i++) { 1822f2637526SLuigi Rizzo D(" TX%d at 0x%lx", i, 1823f2637526SLuigi Rizzo (char *)NETMAP_TXRING(nifp, i) - (char *)nifp); 1824f2637526SLuigi Rizzo } 1825f0ea3689SLuigi Rizzo for (i = 0; i <= req->nr_rx_rings; i++) { 1826f2637526SLuigi Rizzo D(" RX%d at 0x%lx", i, 1827f2637526SLuigi Rizzo (char *)NETMAP_RXRING(nifp, i) - (char *)nifp); 1828f2637526SLuigi Rizzo } 1829f2637526SLuigi Rizzo } 183068b8534bSLuigi Rizzo 183168b8534bSLuigi Rizzo /* Print some debug information. */ 183268b8534bSLuigi Rizzo fprintf(stdout, 183368b8534bSLuigi Rizzo "%s %s: %d queues, %d threads and %d cpus.\n", 1834f8e4e36aSLuigi Rizzo (g.td_body == sender_body) ? "Sending on" : "Receiving from", 1835f8e4e36aSLuigi Rizzo g.ifname, 183668b8534bSLuigi Rizzo devqueues, 183768b8534bSLuigi Rizzo g.nthreads, 183868b8534bSLuigi Rizzo g.cpus); 1839f8e4e36aSLuigi Rizzo if (g.td_body == sender_body) { 184068b8534bSLuigi Rizzo fprintf(stdout, "%s -> %s (%s -> %s)\n", 1841f8e4e36aSLuigi Rizzo g.src_ip.name, g.dst_ip.name, 1842f8e4e36aSLuigi Rizzo g.src_mac.name, g.dst_mac.name); 184368b8534bSLuigi Rizzo } 184468b8534bSLuigi Rizzo 1845f0ea3689SLuigi Rizzo out: 184668b8534bSLuigi Rizzo /* Exit if something went wrong. */ 1847f8e4e36aSLuigi Rizzo if (g.main_fd < 0) { 184868b8534bSLuigi Rizzo D("aborting"); 184968b8534bSLuigi Rizzo usage(); 185068b8534bSLuigi Rizzo } 185199fb123fSLuigi Rizzo } 185268b8534bSLuigi Rizzo 1853ce3ee1e7SLuigi Rizzo 185499fb123fSLuigi Rizzo if (g.options) { 1855b303f675SLuigi Rizzo D("--- SPECIAL OPTIONS:%s%s%s%s%s\n", 185699fb123fSLuigi Rizzo g.options & OPT_PREFETCH ? " prefetch" : "", 185799fb123fSLuigi Rizzo g.options & OPT_ACCESS ? " access" : "", 185899fb123fSLuigi Rizzo g.options & OPT_MEMCPY ? " memcpy" : "", 1859b303f675SLuigi Rizzo g.options & OPT_INDIRECT ? " indirect" : "", 186099fb123fSLuigi Rizzo g.options & OPT_COPY ? " copy" : ""); 186199fb123fSLuigi Rizzo } 18621cb4c501SLuigi Rizzo 1863ce3ee1e7SLuigi Rizzo g.tx_period.tv_sec = g.tx_period.tv_nsec = 0; 1864ce3ee1e7SLuigi Rizzo if (g.tx_rate > 0) { 1865ce3ee1e7SLuigi Rizzo /* try to have at least something every second, 186617885a7bSLuigi Rizzo * reducing the burst size to some 0.01s worth of data 1867ce3ee1e7SLuigi Rizzo * (but no less than one full set of fragments) 1868ce3ee1e7SLuigi Rizzo */ 186917885a7bSLuigi Rizzo uint64_t x; 187017885a7bSLuigi Rizzo int lim = (g.tx_rate)/300; 187117885a7bSLuigi Rizzo if (g.burst > lim) 187217885a7bSLuigi Rizzo g.burst = lim; 1873ce3ee1e7SLuigi Rizzo if (g.burst < g.frags) 1874ce3ee1e7SLuigi Rizzo g.burst = g.frags; 187517885a7bSLuigi Rizzo x = ((uint64_t)1000000000 * (uint64_t)g.burst) / (uint64_t) g.tx_rate; 187617885a7bSLuigi Rizzo g.tx_period.tv_nsec = x; 18771cb4c501SLuigi Rizzo g.tx_period.tv_sec = g.tx_period.tv_nsec / 1000000000; 18781cb4c501SLuigi Rizzo g.tx_period.tv_nsec = g.tx_period.tv_nsec % 1000000000; 18791cb4c501SLuigi Rizzo } 1880ce3ee1e7SLuigi Rizzo if (g.td_body == sender_body) 1881ce3ee1e7SLuigi Rizzo D("Sending %d packets every %ld.%09ld s", 1882ce3ee1e7SLuigi Rizzo g.burst, g.tx_period.tv_sec, g.tx_period.tv_nsec); 188368b8534bSLuigi Rizzo /* Wait for PHY reset. */ 188468b8534bSLuigi Rizzo D("Wait %d secs for phy reset", wait_link); 188568b8534bSLuigi Rizzo sleep(wait_link); 188668b8534bSLuigi Rizzo D("Ready..."); 188768b8534bSLuigi Rizzo 188868b8534bSLuigi Rizzo /* Install ^C handler. */ 188968b8534bSLuigi Rizzo global_nthreads = g.nthreads; 189068b8534bSLuigi Rizzo signal(SIGINT, sigint_h); 189168b8534bSLuigi Rizzo 1892f8e4e36aSLuigi Rizzo start_threads(&g); 1893f8e4e36aSLuigi Rizzo main_thread(&g); 1894f8e4e36aSLuigi Rizzo return 0; 189568b8534bSLuigi Rizzo } 189668b8534bSLuigi Rizzo 189768b8534bSLuigi Rizzo /* end of file */ 1898