168b8534bSLuigi Rizzo /* 2f8e4e36aSLuigi Rizzo * Copyright (C) 2011-2012 Matteo Landi, Luigi Rizzo. All rights reserved. 368b8534bSLuigi Rizzo * 468b8534bSLuigi Rizzo * Redistribution and use in source and binary forms, with or without 568b8534bSLuigi Rizzo * modification, are permitted provided that the following conditions 668b8534bSLuigi Rizzo * are met: 768b8534bSLuigi Rizzo * 1. Redistributions of source code must retain the above copyright 868b8534bSLuigi Rizzo * notice, this list of conditions and the following disclaimer. 968b8534bSLuigi Rizzo * 2. Redistributions in binary form must reproduce the above copyright 1068b8534bSLuigi Rizzo * notice, this list of conditions and the following disclaimer in the 1168b8534bSLuigi Rizzo * documentation and/or other materials provided with the distribution. 1268b8534bSLuigi Rizzo * 1368b8534bSLuigi Rizzo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1468b8534bSLuigi Rizzo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1568b8534bSLuigi Rizzo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1668b8534bSLuigi Rizzo * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1768b8534bSLuigi Rizzo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1868b8534bSLuigi Rizzo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 1968b8534bSLuigi Rizzo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2068b8534bSLuigi Rizzo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2168b8534bSLuigi Rizzo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2268b8534bSLuigi Rizzo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2368b8534bSLuigi Rizzo * SUCH DAMAGE. 2468b8534bSLuigi Rizzo */ 2568b8534bSLuigi Rizzo 2668b8534bSLuigi Rizzo /* 2768b8534bSLuigi Rizzo * $FreeBSD$ 28f8e4e36aSLuigi Rizzo * $Id: pkt-gen.c 12024 2013-01-25 05:41:51Z luigi $ 2968b8534bSLuigi Rizzo * 3068b8534bSLuigi Rizzo * Example program to show how to build a multithreaded packet 3168b8534bSLuigi Rizzo * source/sink using the netmap device. 3268b8534bSLuigi Rizzo * 3368b8534bSLuigi Rizzo * In this example we create a programmable number of threads 3468b8534bSLuigi Rizzo * to take care of all the queues of the interface used to 3568b8534bSLuigi Rizzo * send or receive traffic. 3668b8534bSLuigi Rizzo * 3768b8534bSLuigi Rizzo */ 3868b8534bSLuigi Rizzo 39f8e4e36aSLuigi Rizzo #include "nm_util.h" 40f8e4e36aSLuigi Rizzo 41f8e4e36aSLuigi Rizzo const char *default_payload="netmap pkt-gen payload\n" 4268b8534bSLuigi Rizzo "http://info.iet.unipi.it/~luigi/netmap/ "; 4368b8534bSLuigi Rizzo 44f8e4e36aSLuigi Rizzo int time_second; // support for RD() debugging macro 4568b8534bSLuigi Rizzo 4668b8534bSLuigi Rizzo int verbose = 0; 4768b8534bSLuigi Rizzo 4868b8534bSLuigi Rizzo #define SKIP_PAYLOAD 1 /* do not check payload. */ 4968b8534bSLuigi Rizzo 5068b8534bSLuigi Rizzo struct pkt { 5168b8534bSLuigi Rizzo struct ether_header eh; 5268b8534bSLuigi Rizzo struct ip ip; 5368b8534bSLuigi Rizzo struct udphdr udp; 545819da83SLuigi Rizzo uint8_t body[2048]; // XXX hardwired 5568b8534bSLuigi Rizzo } __attribute__((__packed__)); 5668b8534bSLuigi Rizzo 57f8e4e36aSLuigi Rizzo struct ip_range { 58f8e4e36aSLuigi Rizzo char *name; 59f8e4e36aSLuigi Rizzo struct in_addr start, end, cur; 60f8e4e36aSLuigi Rizzo uint16_t port0, port1, cur_p; 61f8e4e36aSLuigi Rizzo }; 62f8e4e36aSLuigi Rizzo 63f8e4e36aSLuigi Rizzo struct mac_range { 64f8e4e36aSLuigi Rizzo char *name; 65f8e4e36aSLuigi Rizzo struct ether_addr start, end; 66f8e4e36aSLuigi Rizzo }; 67f8e4e36aSLuigi Rizzo 6868b8534bSLuigi Rizzo /* 6968b8534bSLuigi Rizzo * global arguments for all threads 7068b8534bSLuigi Rizzo */ 71f8e4e36aSLuigi Rizzo 7268b8534bSLuigi Rizzo struct glob_arg { 73f8e4e36aSLuigi Rizzo struct ip_range src_ip; 74f8e4e36aSLuigi Rizzo struct ip_range dst_ip; 75f8e4e36aSLuigi Rizzo struct mac_range dst_mac; 76f8e4e36aSLuigi Rizzo struct mac_range src_mac; 7768b8534bSLuigi Rizzo int pkt_size; 7868b8534bSLuigi Rizzo int burst; 79f8e4e36aSLuigi Rizzo int forever; 8068b8534bSLuigi Rizzo int npackets; /* total packets to send */ 8168b8534bSLuigi Rizzo int nthreads; 8268b8534bSLuigi Rizzo int cpus; 8399fb123fSLuigi Rizzo int options; /* testing */ 8499fb123fSLuigi Rizzo #define OPT_PREFETCH 1 8599fb123fSLuigi Rizzo #define OPT_ACCESS 2 8699fb123fSLuigi Rizzo #define OPT_COPY 4 8799fb123fSLuigi Rizzo #define OPT_MEMCPY 8 88f8e4e36aSLuigi Rizzo #define OPT_TS 16 /* add a timestamp */ 89f8e4e36aSLuigi Rizzo int dev_type; 9068b8534bSLuigi Rizzo pcap_t *p; 9168b8534bSLuigi Rizzo 92f8e4e36aSLuigi Rizzo int affinity; 93f8e4e36aSLuigi Rizzo int main_fd; 94f8e4e36aSLuigi Rizzo int report_interval; 95f8e4e36aSLuigi Rizzo void *(*td_body)(void *); 96f8e4e36aSLuigi Rizzo void *mmap_addr; 97f8e4e36aSLuigi Rizzo int mmap_size; 98f8e4e36aSLuigi Rizzo char *ifname; 9968b8534bSLuigi Rizzo }; 100f8e4e36aSLuigi Rizzo enum dev_type { DEV_NONE, DEV_NETMAP, DEV_PCAP, DEV_TAP }; 101f8e4e36aSLuigi Rizzo 10268b8534bSLuigi Rizzo 10368b8534bSLuigi Rizzo /* 10468b8534bSLuigi Rizzo * Arguments for a new thread. The same structure is used by 10568b8534bSLuigi Rizzo * the source and the sink 10668b8534bSLuigi Rizzo */ 10768b8534bSLuigi Rizzo struct targ { 10868b8534bSLuigi Rizzo struct glob_arg *g; 10968b8534bSLuigi Rizzo int used; 11068b8534bSLuigi Rizzo int completed; 1113fe77e68SEd Maste int cancel; 11268b8534bSLuigi Rizzo int fd; 11368b8534bSLuigi Rizzo struct nmreq nmr; 11468b8534bSLuigi Rizzo struct netmap_if *nifp; 11568b8534bSLuigi Rizzo uint16_t qfirst, qlast; /* range of queues to scan */ 116f8e4e36aSLuigi Rizzo volatile uint64_t count; 11768b8534bSLuigi Rizzo struct timeval tic, toc; 11868b8534bSLuigi Rizzo int me; 11968b8534bSLuigi Rizzo pthread_t thread; 12068b8534bSLuigi Rizzo int affinity; 12168b8534bSLuigi Rizzo 12268b8534bSLuigi Rizzo struct pkt pkt; 12368b8534bSLuigi Rizzo }; 12468b8534bSLuigi Rizzo 12568b8534bSLuigi Rizzo 126f8e4e36aSLuigi Rizzo /* 127f8e4e36aSLuigi Rizzo * extract the extremes from a range of ipv4 addresses. 128f8e4e36aSLuigi Rizzo * addr_lo[-addr_hi][:port_lo[-port_hi]] 129f8e4e36aSLuigi Rizzo */ 130f8e4e36aSLuigi Rizzo static void 131f8e4e36aSLuigi Rizzo extract_ip_range(struct ip_range *r) 132f8e4e36aSLuigi Rizzo { 133f8e4e36aSLuigi Rizzo char *p_lo, *p_hi; 134f8e4e36aSLuigi Rizzo char buf1[16]; // one ip address 135f8e4e36aSLuigi Rizzo 136f8e4e36aSLuigi Rizzo D("extract IP range from %s", r->name); 137f8e4e36aSLuigi Rizzo p_lo = index(r->name, ':'); /* do we have ports ? */ 138f8e4e36aSLuigi Rizzo if (p_lo) { 139f8e4e36aSLuigi Rizzo D(" found ports at %s", p_lo); 140f8e4e36aSLuigi Rizzo *p_lo++ = '\0'; 141f8e4e36aSLuigi Rizzo p_hi = index(p_lo, '-'); 142f8e4e36aSLuigi Rizzo if (p_hi) 143f8e4e36aSLuigi Rizzo *p_hi++ = '\0'; 144f8e4e36aSLuigi Rizzo else 145f8e4e36aSLuigi Rizzo p_hi = p_lo; 146f8e4e36aSLuigi Rizzo r->port0 = strtol(p_lo, NULL, 0); 147f8e4e36aSLuigi Rizzo r->port1 = strtol(p_hi, NULL, 0); 148f8e4e36aSLuigi Rizzo if (r->port1 < r->port0) { 149f8e4e36aSLuigi Rizzo r->cur_p = r->port0; 150f8e4e36aSLuigi Rizzo r->port0 = r->port1; 151f8e4e36aSLuigi Rizzo r->port1 = r->cur_p; 152f8e4e36aSLuigi Rizzo } 153f8e4e36aSLuigi Rizzo r->cur_p = r->port0; 154f8e4e36aSLuigi Rizzo D("ports are %d to %d", r->port0, r->port1); 155f8e4e36aSLuigi Rizzo } 156f8e4e36aSLuigi Rizzo p_hi = index(r->name, '-'); /* do we have upper ip ? */ 157f8e4e36aSLuigi Rizzo if (p_hi) { 158f8e4e36aSLuigi Rizzo *p_hi++ = '\0'; 159f8e4e36aSLuigi Rizzo } else 160f8e4e36aSLuigi Rizzo p_hi = r->name; 161f8e4e36aSLuigi Rizzo inet_aton(r->name, &r->start); 162f8e4e36aSLuigi Rizzo inet_aton(p_hi, &r->end); 163f8e4e36aSLuigi Rizzo if (r->start.s_addr > r->end.s_addr) { 164f8e4e36aSLuigi Rizzo r->cur = r->start; 165f8e4e36aSLuigi Rizzo r->start = r->end; 166f8e4e36aSLuigi Rizzo r->end = r->cur; 167f8e4e36aSLuigi Rizzo } 168f8e4e36aSLuigi Rizzo r->cur = r->start; 169f8e4e36aSLuigi Rizzo strncpy(buf1, inet_ntoa(r->end), sizeof(buf1)); 170f8e4e36aSLuigi Rizzo D("range is %s %d to %s %d", inet_ntoa(r->start), r->port0, 171f8e4e36aSLuigi Rizzo buf1, r->port1); 172f8e4e36aSLuigi Rizzo } 173f8e4e36aSLuigi Rizzo 174f8e4e36aSLuigi Rizzo static void 175f8e4e36aSLuigi Rizzo extract_mac_range(struct mac_range *r) 176f8e4e36aSLuigi Rizzo { 177f8e4e36aSLuigi Rizzo D("extract MAC range from %s", r->name); 178f8e4e36aSLuigi Rizzo bcopy(ether_aton(r->name), &r->start, 6); 179f8e4e36aSLuigi Rizzo bcopy(ether_aton(r->name), &r->end, 6); 180f8e4e36aSLuigi Rizzo #if 0 181f8e4e36aSLuigi Rizzo bcopy(targ->src_mac, eh->ether_shost, 6); 182f8e4e36aSLuigi Rizzo p = index(targ->g->src_mac, '-'); 183f8e4e36aSLuigi Rizzo if (p) 184f8e4e36aSLuigi Rizzo targ->src_mac_range = atoi(p+1); 185f8e4e36aSLuigi Rizzo 186f8e4e36aSLuigi Rizzo bcopy(ether_aton(targ->g->dst_mac), targ->dst_mac, 6); 187f8e4e36aSLuigi Rizzo bcopy(targ->dst_mac, eh->ether_dhost, 6); 188f8e4e36aSLuigi Rizzo p = index(targ->g->dst_mac, '-'); 189f8e4e36aSLuigi Rizzo if (p) 190f8e4e36aSLuigi Rizzo targ->dst_mac_range = atoi(p+1); 191f8e4e36aSLuigi Rizzo #endif 192f8e4e36aSLuigi Rizzo D("%s starts at %s", r->name, ether_ntoa(&r->start)); 193f8e4e36aSLuigi Rizzo } 194f8e4e36aSLuigi Rizzo 19568b8534bSLuigi Rizzo static struct targ *targs; 19668b8534bSLuigi Rizzo static int global_nthreads; 19768b8534bSLuigi Rizzo 19868b8534bSLuigi Rizzo /* control-C handler */ 19968b8534bSLuigi Rizzo static void 200f8e4e36aSLuigi Rizzo sigint_h(int sig) 20168b8534bSLuigi Rizzo { 202f8e4e36aSLuigi Rizzo int i; 20368b8534bSLuigi Rizzo 204f8e4e36aSLuigi Rizzo (void)sig; /* UNUSED */ 205f8e4e36aSLuigi Rizzo for (i = 0; i < global_nthreads; i++) { 206f8e4e36aSLuigi Rizzo targs[i].cancel = 1; 207f8e4e36aSLuigi Rizzo } 20868b8534bSLuigi Rizzo signal(SIGINT, SIG_DFL); 20968b8534bSLuigi Rizzo } 21068b8534bSLuigi Rizzo 21168b8534bSLuigi Rizzo /* sysctl wrapper to return the number of active CPUs */ 21268b8534bSLuigi Rizzo static int 21368b8534bSLuigi Rizzo system_ncpus(void) 21468b8534bSLuigi Rizzo { 215f8e4e36aSLuigi Rizzo #ifdef __FreeBSD__ 21668b8534bSLuigi Rizzo int mib[2], ncpus; 21768b8534bSLuigi Rizzo size_t len; 21868b8534bSLuigi Rizzo 21968b8534bSLuigi Rizzo mib[0] = CTL_HW; 22068b8534bSLuigi Rizzo mib[1] = HW_NCPU; 22168b8534bSLuigi Rizzo len = sizeof(mib); 22268b8534bSLuigi Rizzo sysctl(mib, 2, &ncpus, &len, NULL, 0); 22368b8534bSLuigi Rizzo 22468b8534bSLuigi Rizzo return (ncpus); 225f8e4e36aSLuigi Rizzo #else 226f8e4e36aSLuigi Rizzo return 1; 227f8e4e36aSLuigi Rizzo #endif /* !__FreeBSD__ */ 22868b8534bSLuigi Rizzo } 22968b8534bSLuigi Rizzo 230f8e4e36aSLuigi Rizzo #ifdef __linux__ 231f8e4e36aSLuigi Rizzo #define sockaddr_dl sockaddr_ll 232f8e4e36aSLuigi Rizzo #define sdl_family sll_family 233f8e4e36aSLuigi Rizzo #define AF_LINK AF_PACKET 234f8e4e36aSLuigi Rizzo #define LLADDR(s) s->sll_addr; 235f8e4e36aSLuigi Rizzo #include <linux/if_tun.h> 236f8e4e36aSLuigi Rizzo #define TAP_CLONEDEV "/dev/net/tun" 237f8e4e36aSLuigi Rizzo #endif /* __linux__ */ 238f8e4e36aSLuigi Rizzo 239f8e4e36aSLuigi Rizzo #ifdef __FreeBSD__ 240f8e4e36aSLuigi Rizzo #include <net/if_tun.h> 241f8e4e36aSLuigi Rizzo #define TAP_CLONEDEV "/dev/tap" 242f8e4e36aSLuigi Rizzo #endif /* __FreeBSD */ 243f8e4e36aSLuigi Rizzo 244f8e4e36aSLuigi Rizzo #ifdef __APPLE__ 245f8e4e36aSLuigi Rizzo // #warning TAP not supported on apple ? 246f8e4e36aSLuigi Rizzo #include <net/if_utun.h> 247f8e4e36aSLuigi Rizzo #define TAP_CLONEDEV "/dev/tap" 248f8e4e36aSLuigi Rizzo #endif /* __APPLE__ */ 249f8e4e36aSLuigi Rizzo 250f8e4e36aSLuigi Rizzo 25168b8534bSLuigi Rizzo /* 25268b8534bSLuigi Rizzo * locate the src mac address for our interface, put it 25368b8534bSLuigi Rizzo * into the user-supplied buffer. return 0 if ok, -1 on error. 25468b8534bSLuigi Rizzo */ 25568b8534bSLuigi Rizzo static int 25668b8534bSLuigi Rizzo source_hwaddr(const char *ifname, char *buf) 25768b8534bSLuigi Rizzo { 25868b8534bSLuigi Rizzo struct ifaddrs *ifaphead, *ifap; 25968b8534bSLuigi Rizzo int l = sizeof(ifap->ifa_name); 26068b8534bSLuigi Rizzo 26168b8534bSLuigi Rizzo if (getifaddrs(&ifaphead) != 0) { 26268b8534bSLuigi Rizzo D("getifaddrs %s failed", ifname); 26368b8534bSLuigi Rizzo return (-1); 26468b8534bSLuigi Rizzo } 26568b8534bSLuigi Rizzo 26668b8534bSLuigi Rizzo for (ifap = ifaphead; ifap; ifap = ifap->ifa_next) { 26768b8534bSLuigi Rizzo struct sockaddr_dl *sdl = 26868b8534bSLuigi Rizzo (struct sockaddr_dl *)ifap->ifa_addr; 26968b8534bSLuigi Rizzo uint8_t *mac; 27068b8534bSLuigi Rizzo 27168b8534bSLuigi Rizzo if (!sdl || sdl->sdl_family != AF_LINK) 27268b8534bSLuigi Rizzo continue; 27368b8534bSLuigi Rizzo if (strncmp(ifap->ifa_name, ifname, l) != 0) 27468b8534bSLuigi Rizzo continue; 27568b8534bSLuigi Rizzo mac = (uint8_t *)LLADDR(sdl); 27668b8534bSLuigi Rizzo sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x", 27768b8534bSLuigi Rizzo mac[0], mac[1], mac[2], 27868b8534bSLuigi Rizzo mac[3], mac[4], mac[5]); 27968b8534bSLuigi Rizzo if (verbose) 28068b8534bSLuigi Rizzo D("source hwaddr %s", buf); 28168b8534bSLuigi Rizzo break; 28268b8534bSLuigi Rizzo } 28368b8534bSLuigi Rizzo freeifaddrs(ifaphead); 28468b8534bSLuigi Rizzo return ifap ? 0 : 1; 28568b8534bSLuigi Rizzo } 28668b8534bSLuigi Rizzo 28768b8534bSLuigi Rizzo 28868b8534bSLuigi Rizzo /* set the thread affinity. */ 28968b8534bSLuigi Rizzo static int 29068b8534bSLuigi Rizzo setaffinity(pthread_t me, int i) 29168b8534bSLuigi Rizzo { 292f8e4e36aSLuigi Rizzo #ifdef __FreeBSD__ 29368b8534bSLuigi Rizzo cpuset_t cpumask; 29468b8534bSLuigi Rizzo 29568b8534bSLuigi Rizzo if (i == -1) 29668b8534bSLuigi Rizzo return 0; 29768b8534bSLuigi Rizzo 29868b8534bSLuigi Rizzo /* Set thread affinity affinity.*/ 29968b8534bSLuigi Rizzo CPU_ZERO(&cpumask); 30068b8534bSLuigi Rizzo CPU_SET(i, &cpumask); 30168b8534bSLuigi Rizzo 30268b8534bSLuigi Rizzo if (pthread_setaffinity_np(me, sizeof(cpuset_t), &cpumask) != 0) { 30368b8534bSLuigi Rizzo D("Unable to set affinity"); 30468b8534bSLuigi Rizzo return 1; 30568b8534bSLuigi Rizzo } 306f8e4e36aSLuigi Rizzo #else 307f8e4e36aSLuigi Rizzo (void)me; /* suppress 'unused' warnings */ 308f8e4e36aSLuigi Rizzo (void)i; 309f8e4e36aSLuigi Rizzo #endif /* __FreeBSD__ */ 31068b8534bSLuigi Rizzo return 0; 31168b8534bSLuigi Rizzo } 31268b8534bSLuigi Rizzo 31368b8534bSLuigi Rizzo /* Compute the checksum of the given ip header. */ 31468b8534bSLuigi Rizzo static uint16_t 315f8e4e36aSLuigi Rizzo checksum(const void *data, uint16_t len, uint32_t sum) 31668b8534bSLuigi Rizzo { 31768b8534bSLuigi Rizzo const uint8_t *addr = data; 318f8e4e36aSLuigi Rizzo uint32_t i; 31968b8534bSLuigi Rizzo 320f8e4e36aSLuigi Rizzo /* Checksum all the pairs of bytes first... */ 321f8e4e36aSLuigi Rizzo for (i = 0; i < (len & ~1U); i += 2) { 322f8e4e36aSLuigi Rizzo sum += (u_int16_t)ntohs(*((u_int16_t *)(addr + i))); 323f8e4e36aSLuigi Rizzo if (sum > 0xFFFF) 324f8e4e36aSLuigi Rizzo sum -= 0xFFFF; 325f8e4e36aSLuigi Rizzo } 326f8e4e36aSLuigi Rizzo /* 327f8e4e36aSLuigi Rizzo * If there's a single byte left over, checksum it, too. 328f8e4e36aSLuigi Rizzo * Network byte order is big-endian, so the remaining byte is 329f8e4e36aSLuigi Rizzo * the high byte. 330f8e4e36aSLuigi Rizzo */ 331f8e4e36aSLuigi Rizzo if (i < len) { 332f8e4e36aSLuigi Rizzo sum += addr[i] << 8; 333f8e4e36aSLuigi Rizzo if (sum > 0xFFFF) 334f8e4e36aSLuigi Rizzo sum -= 0xFFFF; 335f8e4e36aSLuigi Rizzo } 336f8e4e36aSLuigi Rizzo return sum; 33768b8534bSLuigi Rizzo } 33868b8534bSLuigi Rizzo 339f8e4e36aSLuigi Rizzo static u_int16_t 340f8e4e36aSLuigi Rizzo wrapsum(u_int32_t sum) 341f8e4e36aSLuigi Rizzo { 342f8e4e36aSLuigi Rizzo sum = ~sum & 0xFFFF; 343f8e4e36aSLuigi Rizzo return (htons(sum)); 34468b8534bSLuigi Rizzo } 34568b8534bSLuigi Rizzo 34668b8534bSLuigi Rizzo /* 34768b8534bSLuigi Rizzo * Fill a packet with some payload. 348f8e4e36aSLuigi Rizzo * We create a UDP packet so the payload starts at 349f8e4e36aSLuigi Rizzo * 14+20+8 = 42 bytes. 35068b8534bSLuigi Rizzo */ 351f8e4e36aSLuigi Rizzo #ifdef __linux__ 352f8e4e36aSLuigi Rizzo #define uh_sport source 353f8e4e36aSLuigi Rizzo #define uh_dport dest 354f8e4e36aSLuigi Rizzo #define uh_ulen len 355f8e4e36aSLuigi Rizzo #define uh_sum check 356f8e4e36aSLuigi Rizzo #endif /* linux */ 35768b8534bSLuigi Rizzo static void 35868b8534bSLuigi Rizzo initialize_packet(struct targ *targ) 35968b8534bSLuigi Rizzo { 36068b8534bSLuigi Rizzo struct pkt *pkt = &targ->pkt; 36168b8534bSLuigi Rizzo struct ether_header *eh; 36268b8534bSLuigi Rizzo struct ip *ip; 36368b8534bSLuigi Rizzo struct udphdr *udp; 364f8e4e36aSLuigi Rizzo uint16_t paylen = targ->g->pkt_size - sizeof(*eh) - sizeof(struct ip); 36568b8534bSLuigi Rizzo int i, l, l0 = strlen(default_payload); 36668b8534bSLuigi Rizzo 36768b8534bSLuigi Rizzo for (i = 0; i < paylen;) { 36868b8534bSLuigi Rizzo l = min(l0, paylen - i); 36968b8534bSLuigi Rizzo bcopy(default_payload, pkt->body + i, l); 37068b8534bSLuigi Rizzo i += l; 37168b8534bSLuigi Rizzo } 37268b8534bSLuigi Rizzo pkt->body[i-1] = '\0'; 37368b8534bSLuigi Rizzo ip = &pkt->ip; 374f8e4e36aSLuigi Rizzo 37568b8534bSLuigi Rizzo ip->ip_v = IPVERSION; 37668b8534bSLuigi Rizzo ip->ip_hl = 5; 37768b8534bSLuigi Rizzo ip->ip_id = 0; 37868b8534bSLuigi Rizzo ip->ip_tos = IPTOS_LOWDELAY; 37968b8534bSLuigi Rizzo ip->ip_len = ntohs(targ->g->pkt_size - sizeof(*eh)); 38068b8534bSLuigi Rizzo ip->ip_id = 0; 38168b8534bSLuigi Rizzo ip->ip_off = htons(IP_DF); /* Don't fragment */ 38268b8534bSLuigi Rizzo ip->ip_ttl = IPDEFTTL; 38368b8534bSLuigi Rizzo ip->ip_p = IPPROTO_UDP; 384f8e4e36aSLuigi Rizzo ip->ip_dst.s_addr = targ->g->dst_ip.cur.s_addr; 385f8e4e36aSLuigi Rizzo if (++targ->g->dst_ip.cur.s_addr > targ->g->dst_ip.end.s_addr) 386f8e4e36aSLuigi Rizzo targ->g->dst_ip.cur.s_addr = targ->g->dst_ip.start.s_addr; 387f8e4e36aSLuigi Rizzo ip->ip_src.s_addr = targ->g->src_ip.cur.s_addr; 388f8e4e36aSLuigi Rizzo if (++targ->g->src_ip.cur.s_addr > targ->g->src_ip.end.s_addr) 389f8e4e36aSLuigi Rizzo targ->g->src_ip.cur.s_addr = targ->g->src_ip.start.s_addr; 390f8e4e36aSLuigi Rizzo ip->ip_sum = wrapsum(checksum(ip, sizeof(*ip), 0)); 391f8e4e36aSLuigi Rizzo 392f8e4e36aSLuigi Rizzo 393f8e4e36aSLuigi Rizzo udp = &pkt->udp; 394f8e4e36aSLuigi Rizzo udp->uh_sport = htons(targ->g->src_ip.cur_p); 395f8e4e36aSLuigi Rizzo if (++targ->g->src_ip.cur_p > targ->g->src_ip.port1) 396f8e4e36aSLuigi Rizzo targ->g->src_ip.cur_p = targ->g->src_ip.port0; 397f8e4e36aSLuigi Rizzo udp->uh_dport = htons(targ->g->dst_ip.cur_p); 398f8e4e36aSLuigi Rizzo if (++targ->g->dst_ip.cur_p > targ->g->dst_ip.port1) 399f8e4e36aSLuigi Rizzo targ->g->dst_ip.cur_p = targ->g->dst_ip.port0; 400f8e4e36aSLuigi Rizzo udp->uh_ulen = htons(paylen); 401f8e4e36aSLuigi Rizzo /* Magic: taken from sbin/dhclient/packet.c */ 402f8e4e36aSLuigi Rizzo udp->uh_sum = wrapsum(checksum(udp, sizeof(*udp), 403f8e4e36aSLuigi Rizzo checksum(pkt->body, 404f8e4e36aSLuigi Rizzo paylen - sizeof(*udp), 405f8e4e36aSLuigi Rizzo checksum(&ip->ip_src, 2 * sizeof(ip->ip_src), 406f8e4e36aSLuigi Rizzo IPPROTO_UDP + (u_int32_t)ntohs(udp->uh_ulen) 407f8e4e36aSLuigi Rizzo ) 408f8e4e36aSLuigi Rizzo ) 409f8e4e36aSLuigi Rizzo )); 41068b8534bSLuigi Rizzo 41168b8534bSLuigi Rizzo eh = &pkt->eh; 412f8e4e36aSLuigi Rizzo bcopy(&targ->g->src_mac.start, eh->ether_shost, 6); 413f8e4e36aSLuigi Rizzo bcopy(&targ->g->dst_mac.start, eh->ether_dhost, 6); 41468b8534bSLuigi Rizzo eh->ether_type = htons(ETHERTYPE_IP); 41568b8534bSLuigi Rizzo } 41668b8534bSLuigi Rizzo 41768b8534bSLuigi Rizzo /* Check the payload of the packet for errors (use it for debug). 41868b8534bSLuigi Rizzo * Look for consecutive ascii representations of the size of the packet. 41968b8534bSLuigi Rizzo */ 42068b8534bSLuigi Rizzo static void 42168b8534bSLuigi Rizzo check_payload(char *p, int psize) 42268b8534bSLuigi Rizzo { 42368b8534bSLuigi Rizzo char temp[64]; 42468b8534bSLuigi Rizzo int n_read, size, sizelen; 42568b8534bSLuigi Rizzo 42668b8534bSLuigi Rizzo /* get the length in ASCII of the length of the packet. */ 42768b8534bSLuigi Rizzo sizelen = sprintf(temp, "%d", psize) + 1; // include a whitespace 42868b8534bSLuigi Rizzo 42968b8534bSLuigi Rizzo /* dummy payload. */ 43068b8534bSLuigi Rizzo p += 14; /* skip packet header. */ 43168b8534bSLuigi Rizzo n_read = 14; 43268b8534bSLuigi Rizzo while (psize - n_read >= sizelen) { 43368b8534bSLuigi Rizzo sscanf(p, "%d", &size); 43468b8534bSLuigi Rizzo if (size != psize) { 43568b8534bSLuigi Rizzo D("Read %d instead of %d", size, psize); 43668b8534bSLuigi Rizzo break; 43768b8534bSLuigi Rizzo } 43868b8534bSLuigi Rizzo 43968b8534bSLuigi Rizzo p += sizelen; 44068b8534bSLuigi Rizzo n_read += sizelen; 44168b8534bSLuigi Rizzo } 44268b8534bSLuigi Rizzo } 44368b8534bSLuigi Rizzo 44468b8534bSLuigi Rizzo 44568b8534bSLuigi Rizzo /* 44668b8534bSLuigi Rizzo * create and enqueue a batch of packets on a ring. 44768b8534bSLuigi Rizzo * On the last one set NS_REPORT to tell the driver to generate 44868b8534bSLuigi Rizzo * an interrupt when done. 44968b8534bSLuigi Rizzo */ 45068b8534bSLuigi Rizzo static int 45168b8534bSLuigi Rizzo send_packets(struct netmap_ring *ring, struct pkt *pkt, 45299fb123fSLuigi Rizzo int size, u_int count, int options) 45368b8534bSLuigi Rizzo { 45468b8534bSLuigi Rizzo u_int sent, cur = ring->cur; 45568b8534bSLuigi Rizzo 45668b8534bSLuigi Rizzo if (ring->avail < count) 45768b8534bSLuigi Rizzo count = ring->avail; 45868b8534bSLuigi Rizzo 45999fb123fSLuigi Rizzo #if 0 46099fb123fSLuigi Rizzo if (options & (OPT_COPY | OPT_PREFETCH) ) { 46168b8534bSLuigi Rizzo for (sent = 0; sent < count; sent++) { 46268b8534bSLuigi Rizzo struct netmap_slot *slot = &ring->slot[cur]; 46368b8534bSLuigi Rizzo char *p = NETMAP_BUF(ring, slot->buf_idx); 46468b8534bSLuigi Rizzo 46599fb123fSLuigi Rizzo prefetch(p); 46699fb123fSLuigi Rizzo cur = NETMAP_RING_NEXT(ring, cur); 46799fb123fSLuigi Rizzo } 46899fb123fSLuigi Rizzo cur = ring->cur; 46999fb123fSLuigi Rizzo } 47099fb123fSLuigi Rizzo #endif 47199fb123fSLuigi Rizzo for (sent = 0; sent < count; sent++) { 47299fb123fSLuigi Rizzo struct netmap_slot *slot = &ring->slot[cur]; 47399fb123fSLuigi Rizzo char *p = NETMAP_BUF(ring, slot->buf_idx); 47499fb123fSLuigi Rizzo 47599fb123fSLuigi Rizzo if (options & OPT_COPY) 47699fb123fSLuigi Rizzo pkt_copy(pkt, p, size); 47799fb123fSLuigi Rizzo else if (options & OPT_MEMCPY) 47868b8534bSLuigi Rizzo memcpy(p, pkt, size); 47999fb123fSLuigi Rizzo else if (options & OPT_PREFETCH) 48099fb123fSLuigi Rizzo prefetch(p); 48168b8534bSLuigi Rizzo slot->len = size; 48268b8534bSLuigi Rizzo if (sent == count - 1) 48368b8534bSLuigi Rizzo slot->flags |= NS_REPORT; 48468b8534bSLuigi Rizzo cur = NETMAP_RING_NEXT(ring, cur); 48568b8534bSLuigi Rizzo } 48668b8534bSLuigi Rizzo ring->avail -= sent; 48768b8534bSLuigi Rizzo ring->cur = cur; 48868b8534bSLuigi Rizzo 48968b8534bSLuigi Rizzo return (sent); 49068b8534bSLuigi Rizzo } 49168b8534bSLuigi Rizzo 492f8e4e36aSLuigi Rizzo /* 493f8e4e36aSLuigi Rizzo * Send a packet, and wait for a response. 494f8e4e36aSLuigi Rizzo * The payload (after UDP header, ofs 42) has a 4-byte sequence 495f8e4e36aSLuigi Rizzo * followed by a struct timeval (or bintime?) 496f8e4e36aSLuigi Rizzo */ 497f8e4e36aSLuigi Rizzo #define PAY_OFS 42 /* where in the pkt... */ 498f8e4e36aSLuigi Rizzo 49968b8534bSLuigi Rizzo static void * 500f8e4e36aSLuigi Rizzo pinger_body(void *data) 50168b8534bSLuigi Rizzo { 50268b8534bSLuigi Rizzo struct targ *targ = (struct targ *) data; 50368b8534bSLuigi Rizzo struct pollfd fds[1]; 50468b8534bSLuigi Rizzo struct netmap_if *nifp = targ->nifp; 505f8e4e36aSLuigi Rizzo int i, rx = 0, n = targ->g->npackets; 506e5ecae38SEd Maste 507f8e4e36aSLuigi Rizzo fds[0].fd = targ->fd; 508f8e4e36aSLuigi Rizzo fds[0].events = (POLLIN); 509f8e4e36aSLuigi Rizzo static uint32_t sent; 510f8e4e36aSLuigi Rizzo struct timespec ts, now, last_print; 511f8e4e36aSLuigi Rizzo uint32_t count = 0, min = 1000000000, av = 0; 512f8e4e36aSLuigi Rizzo 513f8e4e36aSLuigi Rizzo if (targ->g->nthreads > 1) { 514f8e4e36aSLuigi Rizzo D("can only ping with 1 thread"); 515f8e4e36aSLuigi Rizzo return NULL; 516f95a30bdSEd Maste } 517f8e4e36aSLuigi Rizzo 518f8e4e36aSLuigi Rizzo clock_gettime(CLOCK_REALTIME_PRECISE, &last_print); 519f8e4e36aSLuigi Rizzo while (n == 0 || (int)sent < n) { 520f8e4e36aSLuigi Rizzo struct netmap_ring *ring = NETMAP_TXRING(nifp, 0); 521f8e4e36aSLuigi Rizzo struct netmap_slot *slot; 522f8e4e36aSLuigi Rizzo char *p; 523f8e4e36aSLuigi Rizzo for (i = 0; i < 1; i++) { 524f8e4e36aSLuigi Rizzo slot = &ring->slot[ring->cur]; 525f8e4e36aSLuigi Rizzo slot->len = targ->g->pkt_size; 526f8e4e36aSLuigi Rizzo p = NETMAP_BUF(ring, slot->buf_idx); 527f8e4e36aSLuigi Rizzo 528f8e4e36aSLuigi Rizzo if (ring->avail == 0) { 529f8e4e36aSLuigi Rizzo D("-- ouch, cannot send"); 530f8e4e36aSLuigi Rizzo } else { 531f8e4e36aSLuigi Rizzo pkt_copy(&targ->pkt, p, targ->g->pkt_size); 532f8e4e36aSLuigi Rizzo clock_gettime(CLOCK_REALTIME_PRECISE, &ts); 533f8e4e36aSLuigi Rizzo bcopy(&sent, p+42, sizeof(sent)); 534f8e4e36aSLuigi Rizzo bcopy(&ts, p+46, sizeof(ts)); 535f8e4e36aSLuigi Rizzo sent++; 536f8e4e36aSLuigi Rizzo ring->cur = NETMAP_RING_NEXT(ring, ring->cur); 537f8e4e36aSLuigi Rizzo ring->avail--; 538f8e4e36aSLuigi Rizzo } 539f8e4e36aSLuigi Rizzo } 540f8e4e36aSLuigi Rizzo /* should use a parameter to decide how often to send */ 541f8e4e36aSLuigi Rizzo if (poll(fds, 1, 3000) <= 0) { 542f8e4e36aSLuigi Rizzo D("poll error/timeout on queue %d", targ->me); 543f8e4e36aSLuigi Rizzo continue; 544f8e4e36aSLuigi Rizzo } 545f8e4e36aSLuigi Rizzo /* see what we got back */ 546f8e4e36aSLuigi Rizzo for (i = targ->qfirst; i < targ->qlast; i++) { 547f8e4e36aSLuigi Rizzo ring = NETMAP_RXRING(nifp, i); 548f8e4e36aSLuigi Rizzo while (ring->avail > 0) { 549f8e4e36aSLuigi Rizzo uint32_t seq; 550f8e4e36aSLuigi Rizzo slot = &ring->slot[ring->cur]; 551f8e4e36aSLuigi Rizzo p = NETMAP_BUF(ring, slot->buf_idx); 552f8e4e36aSLuigi Rizzo 553f8e4e36aSLuigi Rizzo clock_gettime(CLOCK_REALTIME_PRECISE, &now); 554f8e4e36aSLuigi Rizzo bcopy(p+42, &seq, sizeof(seq)); 555f8e4e36aSLuigi Rizzo bcopy(p+46, &ts, sizeof(ts)); 556f8e4e36aSLuigi Rizzo ts.tv_sec = now.tv_sec - ts.tv_sec; 557f8e4e36aSLuigi Rizzo ts.tv_nsec = now.tv_nsec - ts.tv_nsec; 558f8e4e36aSLuigi Rizzo if (ts.tv_nsec < 0) { 559f8e4e36aSLuigi Rizzo ts.tv_nsec += 1000000000; 560f8e4e36aSLuigi Rizzo ts.tv_sec--; 561f8e4e36aSLuigi Rizzo } 562f8e4e36aSLuigi Rizzo if (1) D("seq %d/%d delta %d.%09d", seq, sent, 563f8e4e36aSLuigi Rizzo (int)ts.tv_sec, (int)ts.tv_nsec); 564f8e4e36aSLuigi Rizzo if (ts.tv_nsec < (int)min) 565f8e4e36aSLuigi Rizzo min = ts.tv_nsec; 566f8e4e36aSLuigi Rizzo count ++; 567f8e4e36aSLuigi Rizzo av += ts.tv_nsec; 568f8e4e36aSLuigi Rizzo ring->avail--; 569f8e4e36aSLuigi Rizzo ring->cur = NETMAP_RING_NEXT(ring, ring->cur); 570f8e4e36aSLuigi Rizzo rx++; 571f8e4e36aSLuigi Rizzo } 572f8e4e36aSLuigi Rizzo } 573f8e4e36aSLuigi Rizzo //D("tx %d rx %d", sent, rx); 574f8e4e36aSLuigi Rizzo //usleep(100000); 575f8e4e36aSLuigi Rizzo ts.tv_sec = now.tv_sec - last_print.tv_sec; 576f8e4e36aSLuigi Rizzo ts.tv_nsec = now.tv_nsec - last_print.tv_nsec; 577f8e4e36aSLuigi Rizzo if (ts.tv_nsec < 0) { 578f8e4e36aSLuigi Rizzo ts.tv_nsec += 1000000000; 579f8e4e36aSLuigi Rizzo ts.tv_sec--; 580f8e4e36aSLuigi Rizzo } 581f8e4e36aSLuigi Rizzo if (ts.tv_sec >= 1) { 582f8e4e36aSLuigi Rizzo D("count %d min %d av %d", 583f8e4e36aSLuigi Rizzo count, min, av/count); 584f8e4e36aSLuigi Rizzo count = 0; 585f8e4e36aSLuigi Rizzo av = 0; 586f8e4e36aSLuigi Rizzo min = 100000000; 587f8e4e36aSLuigi Rizzo last_print = now; 588f8e4e36aSLuigi Rizzo } 589f8e4e36aSLuigi Rizzo } 590f8e4e36aSLuigi Rizzo return NULL; 591f8e4e36aSLuigi Rizzo } 592f8e4e36aSLuigi Rizzo 593f8e4e36aSLuigi Rizzo 594f8e4e36aSLuigi Rizzo /* 595f8e4e36aSLuigi Rizzo * reply to ping requests 596f8e4e36aSLuigi Rizzo */ 597f8e4e36aSLuigi Rizzo static void * 598f8e4e36aSLuigi Rizzo ponger_body(void *data) 599f8e4e36aSLuigi Rizzo { 600f8e4e36aSLuigi Rizzo struct targ *targ = (struct targ *) data; 601f8e4e36aSLuigi Rizzo struct pollfd fds[1]; 602f8e4e36aSLuigi Rizzo struct netmap_if *nifp = targ->nifp; 603f8e4e36aSLuigi Rizzo struct netmap_ring *txring, *rxring; 604f8e4e36aSLuigi Rizzo int i, rx = 0, sent = 0, n = targ->g->npackets; 605f8e4e36aSLuigi Rizzo fds[0].fd = targ->fd; 606f8e4e36aSLuigi Rizzo fds[0].events = (POLLIN); 607f8e4e36aSLuigi Rizzo 608f8e4e36aSLuigi Rizzo if (targ->g->nthreads > 1) { 609f8e4e36aSLuigi Rizzo D("can only reply ping with 1 thread"); 610f8e4e36aSLuigi Rizzo return NULL; 611f8e4e36aSLuigi Rizzo } 612f8e4e36aSLuigi Rizzo D("understood ponger %d but don't know how to do it", n); 613f8e4e36aSLuigi Rizzo while (n == 0 || sent < n) { 614f8e4e36aSLuigi Rizzo uint32_t txcur, txavail; 615f8e4e36aSLuigi Rizzo //#define BUSYWAIT 616f8e4e36aSLuigi Rizzo #ifdef BUSYWAIT 617f8e4e36aSLuigi Rizzo ioctl(fds[0].fd, NIOCRXSYNC, NULL); 618f8e4e36aSLuigi Rizzo #else 619f8e4e36aSLuigi Rizzo if (poll(fds, 1, 1000) <= 0) { 620f8e4e36aSLuigi Rizzo D("poll error/timeout on queue %d", targ->me); 621f8e4e36aSLuigi Rizzo continue; 622f8e4e36aSLuigi Rizzo } 623f8e4e36aSLuigi Rizzo #endif 624f8e4e36aSLuigi Rizzo txring = NETMAP_TXRING(nifp, 0); 625f8e4e36aSLuigi Rizzo txcur = txring->cur; 626f8e4e36aSLuigi Rizzo txavail = txring->avail; 627f8e4e36aSLuigi Rizzo /* see what we got back */ 628f8e4e36aSLuigi Rizzo for (i = targ->qfirst; i < targ->qlast; i++) { 629f8e4e36aSLuigi Rizzo rxring = NETMAP_RXRING(nifp, i); 630f8e4e36aSLuigi Rizzo while (rxring->avail > 0) { 631f8e4e36aSLuigi Rizzo uint16_t *spkt, *dpkt; 632f8e4e36aSLuigi Rizzo uint32_t cur = rxring->cur; 633f8e4e36aSLuigi Rizzo struct netmap_slot *slot = &rxring->slot[cur]; 634f8e4e36aSLuigi Rizzo char *src, *dst; 635f8e4e36aSLuigi Rizzo src = NETMAP_BUF(rxring, slot->buf_idx); 636f8e4e36aSLuigi Rizzo //D("got pkt %p of size %d", src, slot->len); 637f8e4e36aSLuigi Rizzo rxring->avail--; 638f8e4e36aSLuigi Rizzo rxring->cur = NETMAP_RING_NEXT(rxring, cur); 639f8e4e36aSLuigi Rizzo rx++; 640f8e4e36aSLuigi Rizzo if (txavail == 0) 641f8e4e36aSLuigi Rizzo continue; 642f8e4e36aSLuigi Rizzo dst = NETMAP_BUF(txring, 643f8e4e36aSLuigi Rizzo txring->slot[txcur].buf_idx); 644f8e4e36aSLuigi Rizzo /* copy... */ 645f8e4e36aSLuigi Rizzo dpkt = (uint16_t *)dst; 646f8e4e36aSLuigi Rizzo spkt = (uint16_t *)src; 647f8e4e36aSLuigi Rizzo pkt_copy(src, dst, slot->len); 648f8e4e36aSLuigi Rizzo dpkt[0] = spkt[3]; 649f8e4e36aSLuigi Rizzo dpkt[1] = spkt[4]; 650f8e4e36aSLuigi Rizzo dpkt[2] = spkt[5]; 651f8e4e36aSLuigi Rizzo dpkt[3] = spkt[0]; 652f8e4e36aSLuigi Rizzo dpkt[4] = spkt[1]; 653f8e4e36aSLuigi Rizzo dpkt[5] = spkt[2]; 654f8e4e36aSLuigi Rizzo txring->slot[txcur].len = slot->len; 655f8e4e36aSLuigi Rizzo /* XXX swap src dst mac */ 656f8e4e36aSLuigi Rizzo txcur = NETMAP_RING_NEXT(txring, txcur); 657f8e4e36aSLuigi Rizzo txavail--; 658f8e4e36aSLuigi Rizzo sent++; 659f8e4e36aSLuigi Rizzo } 660f8e4e36aSLuigi Rizzo } 661f8e4e36aSLuigi Rizzo txring->cur = txcur; 662f8e4e36aSLuigi Rizzo txring->avail = txavail; 663f8e4e36aSLuigi Rizzo targ->count = sent; 664f8e4e36aSLuigi Rizzo #ifdef BUSYWAIT 665f8e4e36aSLuigi Rizzo ioctl(fds[0].fd, NIOCTXSYNC, NULL); 666f8e4e36aSLuigi Rizzo #endif 667f8e4e36aSLuigi Rizzo //D("tx %d rx %d", sent, rx); 668f8e4e36aSLuigi Rizzo } 669f8e4e36aSLuigi Rizzo return NULL; 670f8e4e36aSLuigi Rizzo } 671f8e4e36aSLuigi Rizzo 672f8e4e36aSLuigi Rizzo 673f8e4e36aSLuigi Rizzo static void * 674f8e4e36aSLuigi Rizzo sender_body(void *data) 675f8e4e36aSLuigi Rizzo { 676f8e4e36aSLuigi Rizzo struct targ *targ = (struct targ *) data; 677f8e4e36aSLuigi Rizzo 678f8e4e36aSLuigi Rizzo struct pollfd fds[1]; 679f8e4e36aSLuigi Rizzo struct netmap_if *nifp = targ->nifp; 680f8e4e36aSLuigi Rizzo struct netmap_ring *txring; 681f8e4e36aSLuigi Rizzo int i, n = targ->g->npackets / targ->g->nthreads, sent = 0; 682f8e4e36aSLuigi Rizzo int options = targ->g->options | OPT_COPY; 683f8e4e36aSLuigi Rizzo D("start"); 68468b8534bSLuigi Rizzo if (setaffinity(targ->thread, targ->affinity)) 68568b8534bSLuigi Rizzo goto quit; 6868ce070c1SUlrich Spörlein /* setup poll(2) mechanism. */ 68768b8534bSLuigi Rizzo memset(fds, 0, sizeof(fds)); 68868b8534bSLuigi Rizzo fds[0].fd = targ->fd; 68968b8534bSLuigi Rizzo fds[0].events = (POLLOUT); 69068b8534bSLuigi Rizzo 69168b8534bSLuigi Rizzo /* main loop.*/ 69268b8534bSLuigi Rizzo gettimeofday(&targ->tic, NULL); 693f8e4e36aSLuigi Rizzo 694f8e4e36aSLuigi Rizzo if (targ->g->dev_type == DEV_PCAP) { 69568b8534bSLuigi Rizzo int size = targ->g->pkt_size; 69668b8534bSLuigi Rizzo void *pkt = &targ->pkt; 69768b8534bSLuigi Rizzo pcap_t *p = targ->g->p; 69868b8534bSLuigi Rizzo 699f8e4e36aSLuigi Rizzo for (i = 0; !targ->cancel && (n == 0 || sent < n); i++) { 70099fb123fSLuigi Rizzo if (pcap_inject(p, pkt, size) != -1) 70199fb123fSLuigi Rizzo sent++; 70299fb123fSLuigi Rizzo if (i > 10000) { 70399fb123fSLuigi Rizzo targ->count = sent; 70499fb123fSLuigi Rizzo i = 0; 70599fb123fSLuigi Rizzo } 70668b8534bSLuigi Rizzo } 707f8e4e36aSLuigi Rizzo } else if (targ->g->dev_type == DEV_TAP) { /* tap */ 708f8e4e36aSLuigi Rizzo int size = targ->g->pkt_size; 709f8e4e36aSLuigi Rizzo void *pkt = &targ->pkt; 710f8e4e36aSLuigi Rizzo D("writing to file desc %d", targ->g->main_fd); 711f8e4e36aSLuigi Rizzo 712f8e4e36aSLuigi Rizzo for (i = 0; !targ->cancel && (n == 0 || sent < n); i++) { 713f8e4e36aSLuigi Rizzo if (write(targ->g->main_fd, pkt, size) != -1) 714f8e4e36aSLuigi Rizzo sent++; 715f8e4e36aSLuigi Rizzo if (i > 10000) { 716f8e4e36aSLuigi Rizzo targ->count = sent; 717f8e4e36aSLuigi Rizzo i = 0; 718f8e4e36aSLuigi Rizzo } 719f8e4e36aSLuigi Rizzo } 72068b8534bSLuigi Rizzo } else { 721f8e4e36aSLuigi Rizzo while (!targ->cancel && (n == 0 || sent < n)) { 72268b8534bSLuigi Rizzo 72368b8534bSLuigi Rizzo /* 72468b8534bSLuigi Rizzo * wait for available room in the send queue(s) 72568b8534bSLuigi Rizzo */ 726f8e4e36aSLuigi Rizzo if (poll(fds, 1, 2000) <= 0) { 7273fe77e68SEd Maste if (targ->cancel) 7283fe77e68SEd Maste break; 729f8e4e36aSLuigi Rizzo D("poll error/timeout on queue %d", targ->me); 73068b8534bSLuigi Rizzo goto quit; 73168b8534bSLuigi Rizzo } 73268b8534bSLuigi Rizzo /* 73368b8534bSLuigi Rizzo * scan our queues and send on those with room 73468b8534bSLuigi Rizzo */ 735f8e4e36aSLuigi Rizzo if (options & OPT_COPY && sent > 100000 && !(targ->g->options & OPT_COPY) ) { 736f8e4e36aSLuigi Rizzo D("drop copy"); 73799fb123fSLuigi Rizzo options &= ~OPT_COPY; 738f8e4e36aSLuigi Rizzo } 739f8e4e36aSLuigi Rizzo for (i = targ->qfirst; i < targ->qlast; i++) { 740f95a30bdSEd Maste int m, limit = targ->g->burst; 741f8e4e36aSLuigi Rizzo if (n > 0 && n - sent < limit) 742f8e4e36aSLuigi Rizzo limit = n - sent; 74368b8534bSLuigi Rizzo txring = NETMAP_TXRING(nifp, i); 74468b8534bSLuigi Rizzo if (txring->avail == 0) 74568b8534bSLuigi Rizzo continue; 74668b8534bSLuigi Rizzo m = send_packets(txring, &targ->pkt, targ->g->pkt_size, 74799fb123fSLuigi Rizzo limit, options); 74868b8534bSLuigi Rizzo sent += m; 74968b8534bSLuigi Rizzo targ->count = sent; 75068b8534bSLuigi Rizzo } 75168b8534bSLuigi Rizzo } 75299fb123fSLuigi Rizzo /* flush any remaining packets */ 75368b8534bSLuigi Rizzo ioctl(fds[0].fd, NIOCTXSYNC, NULL); 75468b8534bSLuigi Rizzo 75568b8534bSLuigi Rizzo /* final part: wait all the TX queues to be empty. */ 75668b8534bSLuigi Rizzo for (i = targ->qfirst; i < targ->qlast; i++) { 75768b8534bSLuigi Rizzo txring = NETMAP_TXRING(nifp, i); 75868b8534bSLuigi Rizzo while (!NETMAP_TX_RING_EMPTY(txring)) { 75968b8534bSLuigi Rizzo ioctl(fds[0].fd, NIOCTXSYNC, NULL); 76068b8534bSLuigi Rizzo usleep(1); /* wait 1 tick */ 76168b8534bSLuigi Rizzo } 76268b8534bSLuigi Rizzo } 76368b8534bSLuigi Rizzo } 76468b8534bSLuigi Rizzo 76568b8534bSLuigi Rizzo gettimeofday(&targ->toc, NULL); 76668b8534bSLuigi Rizzo targ->completed = 1; 76768b8534bSLuigi Rizzo targ->count = sent; 76868b8534bSLuigi Rizzo 76968b8534bSLuigi Rizzo quit: 77068b8534bSLuigi Rizzo /* reset the ``used`` flag. */ 77168b8534bSLuigi Rizzo targ->used = 0; 77268b8534bSLuigi Rizzo 77368b8534bSLuigi Rizzo return (NULL); 77468b8534bSLuigi Rizzo } 77568b8534bSLuigi Rizzo 77668b8534bSLuigi Rizzo 77768b8534bSLuigi Rizzo static void 778f8e4e36aSLuigi Rizzo receive_pcap(u_char *user, const struct pcap_pkthdr * h, 779f8e4e36aSLuigi Rizzo const u_char * bytes) 78068b8534bSLuigi Rizzo { 78168b8534bSLuigi Rizzo int *count = (int *)user; 782f8e4e36aSLuigi Rizzo (void)h; /* UNUSED */ 783f8e4e36aSLuigi Rizzo (void)bytes; /* UNUSED */ 78468b8534bSLuigi Rizzo (*count)++; 78568b8534bSLuigi Rizzo } 78668b8534bSLuigi Rizzo 78768b8534bSLuigi Rizzo static int 78868b8534bSLuigi Rizzo receive_packets(struct netmap_ring *ring, u_int limit, int skip_payload) 78968b8534bSLuigi Rizzo { 79068b8534bSLuigi Rizzo u_int cur, rx; 79168b8534bSLuigi Rizzo 79268b8534bSLuigi Rizzo cur = ring->cur; 79368b8534bSLuigi Rizzo if (ring->avail < limit) 79468b8534bSLuigi Rizzo limit = ring->avail; 79568b8534bSLuigi Rizzo for (rx = 0; rx < limit; rx++) { 79668b8534bSLuigi Rizzo struct netmap_slot *slot = &ring->slot[cur]; 79768b8534bSLuigi Rizzo char *p = NETMAP_BUF(ring, slot->buf_idx); 79868b8534bSLuigi Rizzo 79968b8534bSLuigi Rizzo if (!skip_payload) 80068b8534bSLuigi Rizzo check_payload(p, slot->len); 80168b8534bSLuigi Rizzo 80268b8534bSLuigi Rizzo cur = NETMAP_RING_NEXT(ring, cur); 80368b8534bSLuigi Rizzo } 80468b8534bSLuigi Rizzo ring->avail -= rx; 80568b8534bSLuigi Rizzo ring->cur = cur; 80668b8534bSLuigi Rizzo 80768b8534bSLuigi Rizzo return (rx); 80868b8534bSLuigi Rizzo } 80968b8534bSLuigi Rizzo 81068b8534bSLuigi Rizzo static void * 81168b8534bSLuigi Rizzo receiver_body(void *data) 81268b8534bSLuigi Rizzo { 81368b8534bSLuigi Rizzo struct targ *targ = (struct targ *) data; 81468b8534bSLuigi Rizzo struct pollfd fds[1]; 81568b8534bSLuigi Rizzo struct netmap_if *nifp = targ->nifp; 81668b8534bSLuigi Rizzo struct netmap_ring *rxring; 817f8e4e36aSLuigi Rizzo int i; 818f8e4e36aSLuigi Rizzo uint64_t received = 0; 81968b8534bSLuigi Rizzo 82068b8534bSLuigi Rizzo if (setaffinity(targ->thread, targ->affinity)) 82168b8534bSLuigi Rizzo goto quit; 82268b8534bSLuigi Rizzo 8238ce070c1SUlrich Spörlein /* setup poll(2) mechanism. */ 82468b8534bSLuigi Rizzo memset(fds, 0, sizeof(fds)); 82568b8534bSLuigi Rizzo fds[0].fd = targ->fd; 82668b8534bSLuigi Rizzo fds[0].events = (POLLIN); 82768b8534bSLuigi Rizzo 82868b8534bSLuigi Rizzo /* unbounded wait for the first packet. */ 829f8e4e36aSLuigi Rizzo for (;;) { 83068b8534bSLuigi Rizzo i = poll(fds, 1, 1000); 83168b8534bSLuigi Rizzo if (i > 0 && !(fds[0].revents & POLLERR)) 83268b8534bSLuigi Rizzo break; 83368b8534bSLuigi Rizzo D("waiting for initial packets, poll returns %d %d", i, fds[0].revents); 83468b8534bSLuigi Rizzo } 83568b8534bSLuigi Rizzo 83668b8534bSLuigi Rizzo /* main loop, exit after 1s silence */ 83768b8534bSLuigi Rizzo gettimeofday(&targ->tic, NULL); 838f8e4e36aSLuigi Rizzo if (targ->g->dev_type == DEV_PCAP) { 8393fe77e68SEd Maste while (!targ->cancel) { 840f8e4e36aSLuigi Rizzo /* XXX should we poll ? */ 84168b8534bSLuigi Rizzo pcap_dispatch(targ->g->p, targ->g->burst, receive_pcap, NULL); 84268b8534bSLuigi Rizzo } 843f8e4e36aSLuigi Rizzo } else if (targ->g->dev_type == DEV_TAP) { 844f8e4e36aSLuigi Rizzo D("reading from %s fd %d", targ->g->ifname, targ->g->main_fd); 845f8e4e36aSLuigi Rizzo while (!targ->cancel) { 846f8e4e36aSLuigi Rizzo char buf[2048]; 847f8e4e36aSLuigi Rizzo /* XXX should we poll ? */ 848f8e4e36aSLuigi Rizzo if (read(targ->g->main_fd, buf, sizeof(buf)) > 0) 849f8e4e36aSLuigi Rizzo targ->count++; 850f8e4e36aSLuigi Rizzo } 85168b8534bSLuigi Rizzo } else { 8523fe77e68SEd Maste while (!targ->cancel) { 85368b8534bSLuigi Rizzo /* Once we started to receive packets, wait at most 1 seconds 85468b8534bSLuigi Rizzo before quitting. */ 855f8e4e36aSLuigi Rizzo if (poll(fds, 1, 1 * 1000) <= 0 && targ->g->forever == 0) { 85668b8534bSLuigi Rizzo gettimeofday(&targ->toc, NULL); 8578ce070c1SUlrich Spörlein targ->toc.tv_sec -= 1; /* Subtract timeout time. */ 85868b8534bSLuigi Rizzo break; 85968b8534bSLuigi Rizzo } 86068b8534bSLuigi Rizzo 86168b8534bSLuigi Rizzo for (i = targ->qfirst; i < targ->qlast; i++) { 86268b8534bSLuigi Rizzo int m; 86368b8534bSLuigi Rizzo 86468b8534bSLuigi Rizzo rxring = NETMAP_RXRING(nifp, i); 86568b8534bSLuigi Rizzo if (rxring->avail == 0) 86668b8534bSLuigi Rizzo continue; 86768b8534bSLuigi Rizzo 86868b8534bSLuigi Rizzo m = receive_packets(rxring, targ->g->burst, 86968b8534bSLuigi Rizzo SKIP_PAYLOAD); 87068b8534bSLuigi Rizzo received += m; 87168b8534bSLuigi Rizzo } 872f8e4e36aSLuigi Rizzo targ->count = received; 87368b8534bSLuigi Rizzo 87468b8534bSLuigi Rizzo // tell the card we have read the data 87568b8534bSLuigi Rizzo //ioctl(fds[0].fd, NIOCRXSYNC, NULL); 87668b8534bSLuigi Rizzo } 87768b8534bSLuigi Rizzo } 87868b8534bSLuigi Rizzo 87968b8534bSLuigi Rizzo targ->completed = 1; 88068b8534bSLuigi Rizzo targ->count = received; 88168b8534bSLuigi Rizzo 88268b8534bSLuigi Rizzo quit: 88368b8534bSLuigi Rizzo /* reset the ``used`` flag. */ 88468b8534bSLuigi Rizzo targ->used = 0; 88568b8534bSLuigi Rizzo 88668b8534bSLuigi Rizzo return (NULL); 88768b8534bSLuigi Rizzo } 88868b8534bSLuigi Rizzo 889f8e4e36aSLuigi Rizzo /* very crude code to print a number in normalized form. 890f8e4e36aSLuigi Rizzo * Caller has to make sure that the buffer is large enough. 891f8e4e36aSLuigi Rizzo */ 892f8e4e36aSLuigi Rizzo static const char * 893f8e4e36aSLuigi Rizzo norm(char *buf, double val) 89466a698c9SEd Maste { 895f8e4e36aSLuigi Rizzo char *units[] = { "", "K", "M", "G" }; 896f8e4e36aSLuigi Rizzo u_int i; 89766a698c9SEd Maste 898f8e4e36aSLuigi Rizzo for (i = 0; val >=1000 && i < sizeof(units)/sizeof(char *); i++) 89966a698c9SEd Maste val /= 1000; 900f8e4e36aSLuigi Rizzo sprintf(buf, "%.2f %s", val, units[i]); 901f8e4e36aSLuigi Rizzo return buf; 90266a698c9SEd Maste } 90366a698c9SEd Maste 90468b8534bSLuigi Rizzo static void 90568b8534bSLuigi Rizzo tx_output(uint64_t sent, int size, double delta) 90668b8534bSLuigi Rizzo { 907f8e4e36aSLuigi Rizzo double bw, raw_bw, pps; 908f8e4e36aSLuigi Rizzo char b1[40], b2[80], b3[80]; 90968b8534bSLuigi Rizzo 910506cc70cSLuigi Rizzo printf("Sent %" PRIu64 " packets, %d bytes each, in %.2f seconds.\n", 91168b8534bSLuigi Rizzo sent, size, delta); 912f8e4e36aSLuigi Rizzo if (delta == 0) 913f8e4e36aSLuigi Rizzo delta = 1e-6; 914f8e4e36aSLuigi Rizzo if (size < 60) /* correct for min packet size */ 915f8e4e36aSLuigi Rizzo size = 60; 916f8e4e36aSLuigi Rizzo pps = sent / delta; 917f8e4e36aSLuigi Rizzo bw = (8.0 * size * sent) / delta; 918f8e4e36aSLuigi Rizzo /* raw packets have4 bytes crc + 20 bytes framing */ 919f8e4e36aSLuigi Rizzo raw_bw = (8.0 * (size + 24) * sent) / delta; 92066a698c9SEd Maste 921f8e4e36aSLuigi Rizzo printf("Speed: %spps Bandwidth: %sbps (raw %sbps)\n", 922f8e4e36aSLuigi Rizzo norm(b1, pps), norm(b2, bw), norm(b3, raw_bw) ); 92368b8534bSLuigi Rizzo } 92468b8534bSLuigi Rizzo 92568b8534bSLuigi Rizzo 92668b8534bSLuigi Rizzo static void 92768b8534bSLuigi Rizzo rx_output(uint64_t received, double delta) 92868b8534bSLuigi Rizzo { 929f8e4e36aSLuigi Rizzo double pps; 930f8e4e36aSLuigi Rizzo char b1[40]; 93168b8534bSLuigi Rizzo 932506cc70cSLuigi Rizzo printf("Received %" PRIu64 " packets, in %.2f seconds.\n", received, delta); 933f8e4e36aSLuigi Rizzo 934f8e4e36aSLuigi Rizzo if (delta == 0) 935f8e4e36aSLuigi Rizzo delta = 1e-6; 936f8e4e36aSLuigi Rizzo pps = received / delta; 937f8e4e36aSLuigi Rizzo printf("Speed: %spps\n", norm(b1, pps)); 93868b8534bSLuigi Rizzo } 93968b8534bSLuigi Rizzo 94068b8534bSLuigi Rizzo static void 94168b8534bSLuigi Rizzo usage(void) 94268b8534bSLuigi Rizzo { 94368b8534bSLuigi Rizzo const char *cmd = "pkt-gen"; 94468b8534bSLuigi Rizzo fprintf(stderr, 94568b8534bSLuigi Rizzo "Usage:\n" 94668b8534bSLuigi Rizzo "%s arguments\n" 94768b8534bSLuigi Rizzo "\t-i interface interface name\n" 948f8e4e36aSLuigi Rizzo "\t-f function tx rx ping pong\n" 949f8e4e36aSLuigi Rizzo "\t-n count number of iterations (can be 0)\n" 950f8e4e36aSLuigi Rizzo "\t-t pkts_to_send also forces tx mode\n" 951f8e4e36aSLuigi Rizzo "\t-r pkts_to_receive also forces rx mode\n" 95268b8534bSLuigi Rizzo "\t-l pkts_size in bytes excluding CRC\n" 95368b8534bSLuigi Rizzo "\t-d dst-ip end with %%n to sweep n addresses\n" 95468b8534bSLuigi Rizzo "\t-s src-ip end with %%n to sweep n addresses\n" 95568b8534bSLuigi Rizzo "\t-D dst-mac end with %%n to sweep n addresses\n" 95668b8534bSLuigi Rizzo "\t-S src-mac end with %%n to sweep n addresses\n" 957f8e4e36aSLuigi Rizzo "\t-a cpu_id use setaffinity\n" 95868b8534bSLuigi Rizzo "\t-b burst size testing, mostly\n" 95968b8534bSLuigi Rizzo "\t-c cores cores to use\n" 96068b8534bSLuigi Rizzo "\t-p threads processes/threads to use\n" 96168b8534bSLuigi Rizzo "\t-T report_ms milliseconds between reports\n" 962f8e4e36aSLuigi Rizzo "\t-P use libpcap instead of netmap\n" 96368b8534bSLuigi Rizzo "\t-w wait_for_link_time in seconds\n" 96468b8534bSLuigi Rizzo "", 96568b8534bSLuigi Rizzo cmd); 96668b8534bSLuigi Rizzo 96768b8534bSLuigi Rizzo exit(0); 96868b8534bSLuigi Rizzo } 96968b8534bSLuigi Rizzo 970f8e4e36aSLuigi Rizzo static void 971f8e4e36aSLuigi Rizzo start_threads(struct glob_arg *g) 972f8e4e36aSLuigi Rizzo { 973f8e4e36aSLuigi Rizzo int i; 974f8e4e36aSLuigi Rizzo 975f8e4e36aSLuigi Rizzo targs = calloc(g->nthreads, sizeof(*targs)); 976f8e4e36aSLuigi Rizzo /* 977f8e4e36aSLuigi Rizzo * Now create the desired number of threads, each one 978f8e4e36aSLuigi Rizzo * using a single descriptor. 979f8e4e36aSLuigi Rizzo */ 980f8e4e36aSLuigi Rizzo for (i = 0; i < g->nthreads; i++) { 981f8e4e36aSLuigi Rizzo bzero(&targs[i], sizeof(targs[i])); 982f8e4e36aSLuigi Rizzo targs[i].fd = -1; /* default, with pcap */ 983f8e4e36aSLuigi Rizzo targs[i].g = g; 984f8e4e36aSLuigi Rizzo 985f8e4e36aSLuigi Rizzo if (g->dev_type == DEV_NETMAP) { 986f8e4e36aSLuigi Rizzo struct nmreq tifreq; 987f8e4e36aSLuigi Rizzo int tfd; 988f8e4e36aSLuigi Rizzo 989f8e4e36aSLuigi Rizzo /* register interface. */ 990f8e4e36aSLuigi Rizzo tfd = open("/dev/netmap", O_RDWR); 991f8e4e36aSLuigi Rizzo if (tfd == -1) { 992f8e4e36aSLuigi Rizzo D("Unable to open /dev/netmap"); 993f8e4e36aSLuigi Rizzo continue; 994f8e4e36aSLuigi Rizzo } 995f8e4e36aSLuigi Rizzo targs[i].fd = tfd; 996f8e4e36aSLuigi Rizzo 997f8e4e36aSLuigi Rizzo bzero(&tifreq, sizeof(tifreq)); 998f8e4e36aSLuigi Rizzo strncpy(tifreq.nr_name, g->ifname, sizeof(tifreq.nr_name)); 999f8e4e36aSLuigi Rizzo tifreq.nr_version = NETMAP_API; 1000f8e4e36aSLuigi Rizzo tifreq.nr_ringid = (g->nthreads > 1) ? (i | NETMAP_HW_RING) : 0; 1001f8e4e36aSLuigi Rizzo 1002f8e4e36aSLuigi Rizzo /* 1003f8e4e36aSLuigi Rizzo * if we are acting as a receiver only, do not touch the transmit ring. 1004f8e4e36aSLuigi Rizzo * This is not the default because many apps may use the interface 1005f8e4e36aSLuigi Rizzo * in both directions, but a pure receiver does not. 1006f8e4e36aSLuigi Rizzo */ 1007f8e4e36aSLuigi Rizzo if (g->td_body == receiver_body) { 1008f8e4e36aSLuigi Rizzo tifreq.nr_ringid |= NETMAP_NO_TX_POLL; 1009f8e4e36aSLuigi Rizzo } 1010f8e4e36aSLuigi Rizzo 1011f8e4e36aSLuigi Rizzo if ((ioctl(tfd, NIOCREGIF, &tifreq)) == -1) { 1012f8e4e36aSLuigi Rizzo D("Unable to register %s", g->ifname); 1013f8e4e36aSLuigi Rizzo continue; 1014f8e4e36aSLuigi Rizzo } 1015f8e4e36aSLuigi Rizzo targs[i].nmr = tifreq; 1016f8e4e36aSLuigi Rizzo targs[i].nifp = NETMAP_IF(g->mmap_addr, tifreq.nr_offset); 1017f8e4e36aSLuigi Rizzo /* start threads. */ 1018f8e4e36aSLuigi Rizzo targs[i].qfirst = (g->nthreads > 1) ? i : 0; 1019f8e4e36aSLuigi Rizzo targs[i].qlast = (g->nthreads > 1) ? i+1 : 1020f8e4e36aSLuigi Rizzo (g->td_body == receiver_body ? tifreq.nr_rx_rings : tifreq.nr_tx_rings); 1021f8e4e36aSLuigi Rizzo } else { 1022f8e4e36aSLuigi Rizzo targs[i].fd = g->main_fd; 1023f8e4e36aSLuigi Rizzo } 1024f8e4e36aSLuigi Rizzo targs[i].used = 1; 1025f8e4e36aSLuigi Rizzo targs[i].me = i; 1026f8e4e36aSLuigi Rizzo if (g->affinity >= 0) { 1027f8e4e36aSLuigi Rizzo if (g->affinity < g->cpus) 1028f8e4e36aSLuigi Rizzo targs[i].affinity = g->affinity; 1029f8e4e36aSLuigi Rizzo else 1030f8e4e36aSLuigi Rizzo targs[i].affinity = i % g->cpus; 1031f8e4e36aSLuigi Rizzo } else 1032f8e4e36aSLuigi Rizzo targs[i].affinity = -1; 1033f8e4e36aSLuigi Rizzo /* default, init packets */ 1034f8e4e36aSLuigi Rizzo initialize_packet(&targs[i]); 1035f8e4e36aSLuigi Rizzo 1036f8e4e36aSLuigi Rizzo if (pthread_create(&targs[i].thread, NULL, g->td_body, 1037f8e4e36aSLuigi Rizzo &targs[i]) == -1) { 1038f8e4e36aSLuigi Rizzo D("Unable to create thread %d", i); 1039f8e4e36aSLuigi Rizzo targs[i].used = 0; 1040f8e4e36aSLuigi Rizzo } 1041f8e4e36aSLuigi Rizzo } 1042f8e4e36aSLuigi Rizzo } 1043f8e4e36aSLuigi Rizzo 1044f8e4e36aSLuigi Rizzo static void 1045f8e4e36aSLuigi Rizzo main_thread(struct glob_arg *g) 1046f8e4e36aSLuigi Rizzo { 1047f8e4e36aSLuigi Rizzo int i; 1048f8e4e36aSLuigi Rizzo 1049f8e4e36aSLuigi Rizzo uint64_t prev = 0; 1050f8e4e36aSLuigi Rizzo uint64_t count = 0; 1051f8e4e36aSLuigi Rizzo double delta_t; 1052f8e4e36aSLuigi Rizzo struct timeval tic, toc; 1053f8e4e36aSLuigi Rizzo 1054f8e4e36aSLuigi Rizzo gettimeofday(&toc, NULL); 1055f8e4e36aSLuigi Rizzo for (;;) { 1056f8e4e36aSLuigi Rizzo struct timeval now, delta; 1057f8e4e36aSLuigi Rizzo uint64_t pps, usec, my_count, npkts; 1058f8e4e36aSLuigi Rizzo int done = 0; 1059f8e4e36aSLuigi Rizzo 1060f8e4e36aSLuigi Rizzo delta.tv_sec = g->report_interval/1000; 1061f8e4e36aSLuigi Rizzo delta.tv_usec = (g->report_interval%1000)*1000; 1062f8e4e36aSLuigi Rizzo select(0, NULL, NULL, NULL, &delta); 1063f8e4e36aSLuigi Rizzo gettimeofday(&now, NULL); 1064f8e4e36aSLuigi Rizzo time_second = now.tv_sec; 1065f8e4e36aSLuigi Rizzo timersub(&now, &toc, &toc); 1066f8e4e36aSLuigi Rizzo my_count = 0; 1067f8e4e36aSLuigi Rizzo for (i = 0; i < g->nthreads; i++) { 1068f8e4e36aSLuigi Rizzo my_count += targs[i].count; 1069f8e4e36aSLuigi Rizzo if (targs[i].used == 0) 1070f8e4e36aSLuigi Rizzo done++; 1071f8e4e36aSLuigi Rizzo } 1072f8e4e36aSLuigi Rizzo usec = toc.tv_sec* 1000000 + toc.tv_usec; 1073f8e4e36aSLuigi Rizzo if (usec < 10000) 1074f8e4e36aSLuigi Rizzo continue; 1075f8e4e36aSLuigi Rizzo npkts = my_count - prev; 1076f8e4e36aSLuigi Rizzo pps = (npkts*1000000 + usec/2) / usec; 1077f8e4e36aSLuigi Rizzo D("%" PRIu64 " pps (%" PRIu64 " pkts in %" PRIu64 " usec)", 1078f8e4e36aSLuigi Rizzo pps, npkts, usec); 1079f8e4e36aSLuigi Rizzo prev = my_count; 1080f8e4e36aSLuigi Rizzo toc = now; 1081f8e4e36aSLuigi Rizzo if (done == g->nthreads) 1082f8e4e36aSLuigi Rizzo break; 1083f8e4e36aSLuigi Rizzo } 1084f8e4e36aSLuigi Rizzo 1085f8e4e36aSLuigi Rizzo timerclear(&tic); 1086f8e4e36aSLuigi Rizzo timerclear(&toc); 1087f8e4e36aSLuigi Rizzo for (i = 0; i < g->nthreads; i++) { 1088f8e4e36aSLuigi Rizzo /* 1089f8e4e36aSLuigi Rizzo * Join active threads, unregister interfaces and close 1090f8e4e36aSLuigi Rizzo * file descriptors. 1091f8e4e36aSLuigi Rizzo */ 1092f8e4e36aSLuigi Rizzo pthread_join(targs[i].thread, NULL); 1093f8e4e36aSLuigi Rizzo close(targs[i].fd); 1094f8e4e36aSLuigi Rizzo 1095f8e4e36aSLuigi Rizzo if (targs[i].completed == 0) 1096f8e4e36aSLuigi Rizzo D("ouch, thread %d exited with error", i); 1097f8e4e36aSLuigi Rizzo 1098f8e4e36aSLuigi Rizzo /* 1099f8e4e36aSLuigi Rizzo * Collect threads output and extract information about 1100f8e4e36aSLuigi Rizzo * how long it took to send all the packets. 1101f8e4e36aSLuigi Rizzo */ 1102f8e4e36aSLuigi Rizzo count += targs[i].count; 1103f8e4e36aSLuigi Rizzo if (!timerisset(&tic) || timercmp(&targs[i].tic, &tic, <)) 1104f8e4e36aSLuigi Rizzo tic = targs[i].tic; 1105f8e4e36aSLuigi Rizzo if (!timerisset(&toc) || timercmp(&targs[i].toc, &toc, >)) 1106f8e4e36aSLuigi Rizzo toc = targs[i].toc; 1107f8e4e36aSLuigi Rizzo } 1108f8e4e36aSLuigi Rizzo 1109f8e4e36aSLuigi Rizzo /* print output. */ 1110f8e4e36aSLuigi Rizzo timersub(&toc, &tic, &toc); 1111f8e4e36aSLuigi Rizzo delta_t = toc.tv_sec + 1e-6* toc.tv_usec; 1112f8e4e36aSLuigi Rizzo if (g->td_body == sender_body) 1113f8e4e36aSLuigi Rizzo tx_output(count, g->pkt_size, delta_t); 1114f8e4e36aSLuigi Rizzo else 1115f8e4e36aSLuigi Rizzo rx_output(count, delta_t); 1116f8e4e36aSLuigi Rizzo 1117f8e4e36aSLuigi Rizzo if (g->dev_type == DEV_NETMAP) { 1118f8e4e36aSLuigi Rizzo ioctl(g->main_fd, NIOCUNREGIF, NULL); // XXX deprecated 1119f8e4e36aSLuigi Rizzo munmap(g->mmap_addr, g->mmap_size); 1120f8e4e36aSLuigi Rizzo close(g->main_fd); 1121f8e4e36aSLuigi Rizzo } 1122f8e4e36aSLuigi Rizzo } 1123f8e4e36aSLuigi Rizzo 1124f8e4e36aSLuigi Rizzo 1125f8e4e36aSLuigi Rizzo struct sf { 1126f8e4e36aSLuigi Rizzo char *key; 1127f8e4e36aSLuigi Rizzo void *f; 1128f8e4e36aSLuigi Rizzo }; 1129f8e4e36aSLuigi Rizzo 1130f8e4e36aSLuigi Rizzo static struct sf func[] = { 1131f8e4e36aSLuigi Rizzo { "tx", sender_body }, 1132f8e4e36aSLuigi Rizzo { "rx", receiver_body }, 1133f8e4e36aSLuigi Rizzo { "ping", pinger_body }, 1134f8e4e36aSLuigi Rizzo { "pong", ponger_body }, 1135f8e4e36aSLuigi Rizzo { NULL, NULL } 1136f8e4e36aSLuigi Rizzo }; 1137f8e4e36aSLuigi Rizzo 1138f8e4e36aSLuigi Rizzo static int 1139f8e4e36aSLuigi Rizzo tap_alloc(char *dev) 1140f8e4e36aSLuigi Rizzo { 1141f8e4e36aSLuigi Rizzo struct ifreq ifr; 1142f8e4e36aSLuigi Rizzo int fd, err; 1143f8e4e36aSLuigi Rizzo char *clonedev = TAP_CLONEDEV; 1144f8e4e36aSLuigi Rizzo 1145f8e4e36aSLuigi Rizzo (void)err; 1146f8e4e36aSLuigi Rizzo (void)dev; 1147f8e4e36aSLuigi Rizzo /* Arguments taken by the function: 1148f8e4e36aSLuigi Rizzo * 1149f8e4e36aSLuigi Rizzo * char *dev: the name of an interface (or '\0'). MUST have enough 1150f8e4e36aSLuigi Rizzo * space to hold the interface name if '\0' is passed 1151f8e4e36aSLuigi Rizzo * int flags: interface flags (eg, IFF_TUN etc.) 1152f8e4e36aSLuigi Rizzo */ 1153f8e4e36aSLuigi Rizzo 1154f8e4e36aSLuigi Rizzo #ifdef __FreeBSD__ 1155f8e4e36aSLuigi Rizzo if (dev[3]) { /* tapSomething */ 1156f8e4e36aSLuigi Rizzo static char buf[128]; 1157f8e4e36aSLuigi Rizzo snprintf(buf, sizeof(buf), "/dev/%s", dev); 1158f8e4e36aSLuigi Rizzo clonedev = buf; 1159f8e4e36aSLuigi Rizzo } 1160f8e4e36aSLuigi Rizzo #endif 1161f8e4e36aSLuigi Rizzo /* open the device */ 1162f8e4e36aSLuigi Rizzo if( (fd = open(clonedev, O_RDWR)) < 0 ) { 1163f8e4e36aSLuigi Rizzo return fd; 1164f8e4e36aSLuigi Rizzo } 1165f8e4e36aSLuigi Rizzo D("%s open successful", clonedev); 1166f8e4e36aSLuigi Rizzo 1167f8e4e36aSLuigi Rizzo /* preparation of the struct ifr, of type "struct ifreq" */ 1168f8e4e36aSLuigi Rizzo memset(&ifr, 0, sizeof(ifr)); 1169f8e4e36aSLuigi Rizzo 1170f8e4e36aSLuigi Rizzo #ifdef linux 1171f8e4e36aSLuigi Rizzo ifr.ifr_flags = IFF_TAP | IFF_NO_PI; 1172f8e4e36aSLuigi Rizzo 1173f8e4e36aSLuigi Rizzo if (*dev) { 1174f8e4e36aSLuigi Rizzo /* if a device name was specified, put it in the structure; otherwise, 1175f8e4e36aSLuigi Rizzo * the kernel will try to allocate the "next" device of the 1176f8e4e36aSLuigi Rizzo * specified type */ 1177f8e4e36aSLuigi Rizzo strncpy(ifr.ifr_name, dev, IFNAMSIZ); 1178f8e4e36aSLuigi Rizzo } 1179f8e4e36aSLuigi Rizzo 1180f8e4e36aSLuigi Rizzo /* try to create the device */ 1181f8e4e36aSLuigi Rizzo if( (err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0 ) { 1182f8e4e36aSLuigi Rizzo D("failed to to a TUNSETIFF"); 1183f8e4e36aSLuigi Rizzo close(fd); 1184f8e4e36aSLuigi Rizzo return err; 1185f8e4e36aSLuigi Rizzo } 1186f8e4e36aSLuigi Rizzo 1187f8e4e36aSLuigi Rizzo /* if the operation was successful, write back the name of the 1188f8e4e36aSLuigi Rizzo * interface to the variable "dev", so the caller can know 1189f8e4e36aSLuigi Rizzo * it. Note that the caller MUST reserve space in *dev (see calling 1190f8e4e36aSLuigi Rizzo * code below) */ 1191f8e4e36aSLuigi Rizzo strcpy(dev, ifr.ifr_name); 1192f8e4e36aSLuigi Rizzo D("new name is %s", dev); 1193f8e4e36aSLuigi Rizzo #endif /* linux */ 1194f8e4e36aSLuigi Rizzo 1195f8e4e36aSLuigi Rizzo /* this is the special file descriptor that the caller will use to talk 1196f8e4e36aSLuigi Rizzo * with the virtual interface */ 1197f8e4e36aSLuigi Rizzo return fd; 1198f8e4e36aSLuigi Rizzo } 119968b8534bSLuigi Rizzo 120068b8534bSLuigi Rizzo int 120168b8534bSLuigi Rizzo main(int arc, char **argv) 120268b8534bSLuigi Rizzo { 1203f8e4e36aSLuigi Rizzo int i; 120468b8534bSLuigi Rizzo 120568b8534bSLuigi Rizzo struct glob_arg g; 120668b8534bSLuigi Rizzo 120768b8534bSLuigi Rizzo struct nmreq nmr; 120868b8534bSLuigi Rizzo int ch; 120968b8534bSLuigi Rizzo int wait_link = 2; 121068b8534bSLuigi Rizzo int devqueues = 1; /* how many device queues */ 121168b8534bSLuigi Rizzo 121268b8534bSLuigi Rizzo bzero(&g, sizeof(g)); 121368b8534bSLuigi Rizzo 1214f8e4e36aSLuigi Rizzo g.main_fd = -1; 1215f8e4e36aSLuigi Rizzo g.td_body = receiver_body; 1216f8e4e36aSLuigi Rizzo g.report_interval = 1000; /* report interval */ 1217f8e4e36aSLuigi Rizzo g.affinity = -1; 1218f8e4e36aSLuigi Rizzo /* ip addresses can also be a range x.x.x.x-x.x.x.y */ 1219f8e4e36aSLuigi Rizzo g.src_ip.name = "10.0.0.1"; 1220f8e4e36aSLuigi Rizzo g.dst_ip.name = "10.1.0.1"; 1221f8e4e36aSLuigi Rizzo g.dst_mac.name = "ff:ff:ff:ff:ff:ff"; 1222f8e4e36aSLuigi Rizzo g.src_mac.name = NULL; 122368b8534bSLuigi Rizzo g.pkt_size = 60; 122468b8534bSLuigi Rizzo g.burst = 512; // default 122568b8534bSLuigi Rizzo g.nthreads = 1; 122668b8534bSLuigi Rizzo g.cpus = 1; 122768b8534bSLuigi Rizzo 122868b8534bSLuigi Rizzo while ( (ch = getopt(arc, argv, 1229f8e4e36aSLuigi Rizzo "a:f:n:i:t:r:l:d:s:D:S:b:c:o:p:PT:w:Wv")) != -1) { 1230f8e4e36aSLuigi Rizzo struct sf *fn; 1231f8e4e36aSLuigi Rizzo 123268b8534bSLuigi Rizzo switch(ch) { 123368b8534bSLuigi Rizzo default: 123468b8534bSLuigi Rizzo D("bad option %c %s", ch, optarg); 123568b8534bSLuigi Rizzo usage(); 123668b8534bSLuigi Rizzo break; 1237f8e4e36aSLuigi Rizzo 1238f8e4e36aSLuigi Rizzo case 'n': 1239f8e4e36aSLuigi Rizzo g.npackets = atoi(optarg); 1240f8e4e36aSLuigi Rizzo break; 1241f8e4e36aSLuigi Rizzo 1242f8e4e36aSLuigi Rizzo case 'f': 1243f8e4e36aSLuigi Rizzo for (fn = func; fn->key; fn++) { 1244f8e4e36aSLuigi Rizzo if (!strcmp(fn->key, optarg)) 1245f8e4e36aSLuigi Rizzo break; 1246f8e4e36aSLuigi Rizzo } 1247f8e4e36aSLuigi Rizzo if (fn->key) 1248f8e4e36aSLuigi Rizzo g.td_body = fn->f; 1249f8e4e36aSLuigi Rizzo else 1250f8e4e36aSLuigi Rizzo D("unrecognised function %s", optarg); 1251f8e4e36aSLuigi Rizzo break; 1252f8e4e36aSLuigi Rizzo 1253f8e4e36aSLuigi Rizzo case 'o': /* data generation options */ 125499fb123fSLuigi Rizzo g.options = atoi(optarg); 125599fb123fSLuigi Rizzo break; 1256f8e4e36aSLuigi Rizzo 1257f8e4e36aSLuigi Rizzo case 'a': /* force affinity */ 1258f8e4e36aSLuigi Rizzo g.affinity = atoi(optarg); 1259f8e4e36aSLuigi Rizzo break; 1260f8e4e36aSLuigi Rizzo 126168b8534bSLuigi Rizzo case 'i': /* interface */ 1262f8e4e36aSLuigi Rizzo g.ifname = optarg; 1263f8e4e36aSLuigi Rizzo if (!strncmp(optarg, "tap", 3)) 1264f8e4e36aSLuigi Rizzo g.dev_type = DEV_TAP; 1265f8e4e36aSLuigi Rizzo else 1266f8e4e36aSLuigi Rizzo g.dev_type = DEV_NETMAP; 126768b8534bSLuigi Rizzo break; 1268f8e4e36aSLuigi Rizzo 1269f8e4e36aSLuigi Rizzo case 't': /* send, deprecated */ 1270f8e4e36aSLuigi Rizzo D("-t deprecated, please use -f tx -n %s", optarg); 1271f8e4e36aSLuigi Rizzo g.td_body = sender_body; 127268b8534bSLuigi Rizzo g.npackets = atoi(optarg); 127368b8534bSLuigi Rizzo break; 1274f8e4e36aSLuigi Rizzo 127568b8534bSLuigi Rizzo case 'r': /* receive */ 1276f8e4e36aSLuigi Rizzo D("-r deprecated, please use -f rx -n %s", optarg); 1277f8e4e36aSLuigi Rizzo g.td_body = receiver_body; 127868b8534bSLuigi Rizzo g.npackets = atoi(optarg); 127968b8534bSLuigi Rizzo break; 1280f8e4e36aSLuigi Rizzo 128168b8534bSLuigi Rizzo case 'l': /* pkt_size */ 128268b8534bSLuigi Rizzo g.pkt_size = atoi(optarg); 128368b8534bSLuigi Rizzo break; 1284f8e4e36aSLuigi Rizzo 128568b8534bSLuigi Rizzo case 'd': 1286f8e4e36aSLuigi Rizzo g.dst_ip.name = optarg; 128768b8534bSLuigi Rizzo break; 1288f8e4e36aSLuigi Rizzo 128968b8534bSLuigi Rizzo case 's': 1290f8e4e36aSLuigi Rizzo g.src_ip.name = optarg; 129168b8534bSLuigi Rizzo break; 1292f8e4e36aSLuigi Rizzo 129368b8534bSLuigi Rizzo case 'T': /* report interval */ 1294f8e4e36aSLuigi Rizzo g.report_interval = atoi(optarg); 129568b8534bSLuigi Rizzo break; 1296f8e4e36aSLuigi Rizzo 129768b8534bSLuigi Rizzo case 'w': 129868b8534bSLuigi Rizzo wait_link = atoi(optarg); 129968b8534bSLuigi Rizzo break; 1300f8e4e36aSLuigi Rizzo 1301f8e4e36aSLuigi Rizzo case 'W': 1302f8e4e36aSLuigi Rizzo g.forever = 1; /* do not exit rx even with no traffic */ 1303f8e4e36aSLuigi Rizzo break; 1304f8e4e36aSLuigi Rizzo 130568b8534bSLuigi Rizzo case 'b': /* burst */ 130668b8534bSLuigi Rizzo g.burst = atoi(optarg); 130768b8534bSLuigi Rizzo break; 130868b8534bSLuigi Rizzo case 'c': 130968b8534bSLuigi Rizzo g.cpus = atoi(optarg); 131068b8534bSLuigi Rizzo break; 131168b8534bSLuigi Rizzo case 'p': 131268b8534bSLuigi Rizzo g.nthreads = atoi(optarg); 131368b8534bSLuigi Rizzo break; 131468b8534bSLuigi Rizzo 131568b8534bSLuigi Rizzo case 'P': 1316f8e4e36aSLuigi Rizzo g.dev_type = DEV_PCAP; 131768b8534bSLuigi Rizzo break; 131868b8534bSLuigi Rizzo 131968b8534bSLuigi Rizzo case 'D': /* destination mac */ 1320f8e4e36aSLuigi Rizzo g.dst_mac.name = optarg; 132168b8534bSLuigi Rizzo break; 1322f8e4e36aSLuigi Rizzo 132368b8534bSLuigi Rizzo case 'S': /* source mac */ 1324f8e4e36aSLuigi Rizzo g.src_mac.name = optarg; 132568b8534bSLuigi Rizzo break; 132668b8534bSLuigi Rizzo case 'v': 132768b8534bSLuigi Rizzo verbose++; 132868b8534bSLuigi Rizzo } 132968b8534bSLuigi Rizzo } 133068b8534bSLuigi Rizzo 1331f8e4e36aSLuigi Rizzo if (g.ifname == NULL) { 133268b8534bSLuigi Rizzo D("missing ifname"); 133368b8534bSLuigi Rizzo usage(); 133468b8534bSLuigi Rizzo } 1335f8e4e36aSLuigi Rizzo 1336f8e4e36aSLuigi Rizzo i = system_ncpus(); 1337f8e4e36aSLuigi Rizzo if (g.cpus < 0 || g.cpus > i) { 1338f8e4e36aSLuigi Rizzo D("%d cpus is too high, have only %d cpus", g.cpus, i); 133968b8534bSLuigi Rizzo usage(); 134068b8534bSLuigi Rizzo } 134168b8534bSLuigi Rizzo if (g.cpus == 0) 1342f8e4e36aSLuigi Rizzo g.cpus = i; 1343f8e4e36aSLuigi Rizzo 134468b8534bSLuigi Rizzo if (g.pkt_size < 16 || g.pkt_size > 1536) { 134568b8534bSLuigi Rizzo D("bad pktsize %d\n", g.pkt_size); 134668b8534bSLuigi Rizzo usage(); 134768b8534bSLuigi Rizzo } 134868b8534bSLuigi Rizzo 1349f8e4e36aSLuigi Rizzo if (g.src_mac.name == NULL) { 1350f8e4e36aSLuigi Rizzo static char mybuf[20] = "00:00:00:00:00:00"; 135199fb123fSLuigi Rizzo /* retrieve source mac address. */ 1352f8e4e36aSLuigi Rizzo if (source_hwaddr(g.ifname, mybuf) == -1) { 135399fb123fSLuigi Rizzo D("Unable to retrieve source mac"); 135499fb123fSLuigi Rizzo // continue, fail later 135599fb123fSLuigi Rizzo } 1356f8e4e36aSLuigi Rizzo g.src_mac.name = mybuf; 135799fb123fSLuigi Rizzo } 1358f8e4e36aSLuigi Rizzo /* extract address ranges */ 1359f8e4e36aSLuigi Rizzo extract_ip_range(&g.src_ip); 1360f8e4e36aSLuigi Rizzo extract_ip_range(&g.dst_ip); 1361f8e4e36aSLuigi Rizzo extract_mac_range(&g.src_mac); 1362f8e4e36aSLuigi Rizzo extract_mac_range(&g.dst_mac); 136399fb123fSLuigi Rizzo 1364f8e4e36aSLuigi Rizzo if (g.dev_type == DEV_TAP) { 1365f8e4e36aSLuigi Rizzo D("want to use tap %s", g.ifname); 1366f8e4e36aSLuigi Rizzo g.main_fd = tap_alloc(g.ifname); 1367f8e4e36aSLuigi Rizzo if (g.main_fd < 0) { 1368f8e4e36aSLuigi Rizzo D("cannot open tap %s", g.ifname); 136999fb123fSLuigi Rizzo usage(); 137099fb123fSLuigi Rizzo } 1371f8e4e36aSLuigi Rizzo } else if (g.dev_type > DEV_NETMAP) { 1372f8e4e36aSLuigi Rizzo char pcap_errbuf[PCAP_ERRBUF_SIZE]; 1373f8e4e36aSLuigi Rizzo 1374f8e4e36aSLuigi Rizzo D("using pcap on %s", g.ifname); 1375f8e4e36aSLuigi Rizzo pcap_errbuf[0] = '\0'; // init the buffer 1376f8e4e36aSLuigi Rizzo g.p = pcap_open_live(g.ifname, 0, 1, 100, pcap_errbuf); 1377f8e4e36aSLuigi Rizzo if (g.p == NULL) { 1378f8e4e36aSLuigi Rizzo D("cannot open pcap on %s", g.ifname); 1379f8e4e36aSLuigi Rizzo usage(); 1380f8e4e36aSLuigi Rizzo } 138199fb123fSLuigi Rizzo } else { 138268b8534bSLuigi Rizzo bzero(&nmr, sizeof(nmr)); 138364ae02c3SLuigi Rizzo nmr.nr_version = NETMAP_API; 138468b8534bSLuigi Rizzo /* 138568b8534bSLuigi Rizzo * Open the netmap device to fetch the number of queues of our 138668b8534bSLuigi Rizzo * interface. 138768b8534bSLuigi Rizzo * 138868b8534bSLuigi Rizzo * The first NIOCREGIF also detaches the card from the 138968b8534bSLuigi Rizzo * protocol stack and may cause a reset of the card, 139068b8534bSLuigi Rizzo * which in turn may take some time for the PHY to 139168b8534bSLuigi Rizzo * reconfigure. 139268b8534bSLuigi Rizzo */ 1393f8e4e36aSLuigi Rizzo g.main_fd = open("/dev/netmap", O_RDWR); 1394f8e4e36aSLuigi Rizzo if (g.main_fd == -1) { 139568b8534bSLuigi Rizzo D("Unable to open /dev/netmap"); 1396f8e4e36aSLuigi Rizzo // fail later 139768b8534bSLuigi Rizzo } else { 1398f8e4e36aSLuigi Rizzo if ((ioctl(g.main_fd, NIOCGINFO, &nmr)) == -1) { 139968b8534bSLuigi Rizzo D("Unable to get if info without name"); 140068b8534bSLuigi Rizzo } else { 140168b8534bSLuigi Rizzo D("map size is %d Kb", nmr.nr_memsize >> 10); 140268b8534bSLuigi Rizzo } 140368b8534bSLuigi Rizzo bzero(&nmr, sizeof(nmr)); 140464ae02c3SLuigi Rizzo nmr.nr_version = NETMAP_API; 1405f8e4e36aSLuigi Rizzo strncpy(nmr.nr_name, g.ifname, sizeof(nmr.nr_name)); 1406f8e4e36aSLuigi Rizzo if ((ioctl(g.main_fd, NIOCGINFO, &nmr)) == -1) { 1407f8e4e36aSLuigi Rizzo D("Unable to get if info for %s", g.ifname); 140868b8534bSLuigi Rizzo } 140964ae02c3SLuigi Rizzo devqueues = nmr.nr_rx_rings; 141068b8534bSLuigi Rizzo } 141168b8534bSLuigi Rizzo 141268b8534bSLuigi Rizzo /* validate provided nthreads. */ 141368b8534bSLuigi Rizzo if (g.nthreads < 1 || g.nthreads > devqueues) { 141468b8534bSLuigi Rizzo D("bad nthreads %d, have %d queues", g.nthreads, devqueues); 141568b8534bSLuigi Rizzo // continue, fail later 141668b8534bSLuigi Rizzo } 141768b8534bSLuigi Rizzo 141868b8534bSLuigi Rizzo /* 141968b8534bSLuigi Rizzo * Map the netmap shared memory: instead of issuing mmap() 142068b8534bSLuigi Rizzo * inside the body of the threads, we prefer to keep this 142168b8534bSLuigi Rizzo * operation here to simplify the thread logic. 142268b8534bSLuigi Rizzo */ 1423f8e4e36aSLuigi Rizzo D("mapping %d Kbytes", nmr.nr_memsize>>10); 1424f8e4e36aSLuigi Rizzo g.mmap_size = nmr.nr_memsize; 1425f8e4e36aSLuigi Rizzo g.mmap_addr = (struct netmap_d *) mmap(0, nmr.nr_memsize, 142668b8534bSLuigi Rizzo PROT_WRITE | PROT_READ, 1427f8e4e36aSLuigi Rizzo MAP_SHARED, g.main_fd, 0); 1428f8e4e36aSLuigi Rizzo if (g.mmap_addr == MAP_FAILED) { 142968b8534bSLuigi Rizzo D("Unable to mmap %d KB", nmr.nr_memsize >> 10); 143068b8534bSLuigi Rizzo // continue, fail later 143168b8534bSLuigi Rizzo } 143268b8534bSLuigi Rizzo 143368b8534bSLuigi Rizzo /* 143468b8534bSLuigi Rizzo * Register the interface on the netmap device: from now on, 143568b8534bSLuigi Rizzo * we can operate on the network interface without any 143668b8534bSLuigi Rizzo * interference from the legacy network stack. 143768b8534bSLuigi Rizzo * 143868b8534bSLuigi Rizzo * We decide to put the first interface registration here to 143968b8534bSLuigi Rizzo * give time to cards that take a long time to reset the PHY. 144068b8534bSLuigi Rizzo */ 144164ae02c3SLuigi Rizzo nmr.nr_version = NETMAP_API; 1442f8e4e36aSLuigi Rizzo if (ioctl(g.main_fd, NIOCREGIF, &nmr) == -1) { 1443f8e4e36aSLuigi Rizzo D("Unable to register interface %s", g.ifname); 144468b8534bSLuigi Rizzo //continue, fail later 144568b8534bSLuigi Rizzo } 144668b8534bSLuigi Rizzo 144768b8534bSLuigi Rizzo 144868b8534bSLuigi Rizzo /* Print some debug information. */ 144968b8534bSLuigi Rizzo fprintf(stdout, 145068b8534bSLuigi Rizzo "%s %s: %d queues, %d threads and %d cpus.\n", 1451f8e4e36aSLuigi Rizzo (g.td_body == sender_body) ? "Sending on" : "Receiving from", 1452f8e4e36aSLuigi Rizzo g.ifname, 145368b8534bSLuigi Rizzo devqueues, 145468b8534bSLuigi Rizzo g.nthreads, 145568b8534bSLuigi Rizzo g.cpus); 1456f8e4e36aSLuigi Rizzo if (g.td_body == sender_body) { 145768b8534bSLuigi Rizzo fprintf(stdout, "%s -> %s (%s -> %s)\n", 1458f8e4e36aSLuigi Rizzo g.src_ip.name, g.dst_ip.name, 1459f8e4e36aSLuigi Rizzo g.src_mac.name, g.dst_mac.name); 146068b8534bSLuigi Rizzo } 146168b8534bSLuigi Rizzo 146268b8534bSLuigi Rizzo /* Exit if something went wrong. */ 1463f8e4e36aSLuigi Rizzo if (g.main_fd < 0) { 146468b8534bSLuigi Rizzo D("aborting"); 146568b8534bSLuigi Rizzo usage(); 146668b8534bSLuigi Rizzo } 146799fb123fSLuigi Rizzo } 146868b8534bSLuigi Rizzo 146999fb123fSLuigi Rizzo if (g.options) { 147099fb123fSLuigi Rizzo D("special options:%s%s%s%s\n", 147199fb123fSLuigi Rizzo g.options & OPT_PREFETCH ? " prefetch" : "", 147299fb123fSLuigi Rizzo g.options & OPT_ACCESS ? " access" : "", 147399fb123fSLuigi Rizzo g.options & OPT_MEMCPY ? " memcpy" : "", 147499fb123fSLuigi Rizzo g.options & OPT_COPY ? " copy" : ""); 147599fb123fSLuigi Rizzo } 147668b8534bSLuigi Rizzo /* Wait for PHY reset. */ 147768b8534bSLuigi Rizzo D("Wait %d secs for phy reset", wait_link); 147868b8534bSLuigi Rizzo sleep(wait_link); 147968b8534bSLuigi Rizzo D("Ready..."); 148068b8534bSLuigi Rizzo 148168b8534bSLuigi Rizzo /* Install ^C handler. */ 148268b8534bSLuigi Rizzo global_nthreads = g.nthreads; 148368b8534bSLuigi Rizzo signal(SIGINT, sigint_h); 148468b8534bSLuigi Rizzo 1485f8e4e36aSLuigi Rizzo #if 0 // XXX this is not needed, i believe 1486f8e4e36aSLuigi Rizzo if (g.dev_type > DEV_NETMAP) { 1487f8e4e36aSLuigi Rizzo g.p = pcap_open_live(g.ifname, 0, 1, 100, NULL); 148899fb123fSLuigi Rizzo if (g.p == NULL) { 1489f8e4e36aSLuigi Rizzo D("cannot open pcap on %s", g.ifname); 149099fb123fSLuigi Rizzo usage(); 149199fb123fSLuigi Rizzo } else 1492f8e4e36aSLuigi Rizzo D("using pcap %p on %s", g.p, g.ifname); 1493f8e4e36aSLuigi Rizzo } 1494f8e4e36aSLuigi Rizzo #endif // XXX 1495f8e4e36aSLuigi Rizzo start_threads(&g); 1496f8e4e36aSLuigi Rizzo main_thread(&g); 1497f8e4e36aSLuigi Rizzo return 0; 149868b8534bSLuigi Rizzo } 149968b8534bSLuigi Rizzo 150068b8534bSLuigi Rizzo /* end of file */ 1501