pkt-gen.c (270063) | pkt-gen.c (272962) |
---|---|
1/* 2 * Copyright (C) 2011-2014 Matteo Landi, Luigi Rizzo. All rights reserved. 3 * Copyright (C) 2013-2014 Universita` di Pisa. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 11 unchanged lines hidden (view full) --- 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27/* | 1/* 2 * Copyright (C) 2011-2014 Matteo Landi, Luigi Rizzo. All rights reserved. 3 * Copyright (C) 2013-2014 Universita` di Pisa. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 11 unchanged lines hidden (view full) --- 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27/* |
28 * $FreeBSD: head/tools/tools/netmap/pkt-gen.c 270063 2014-08-16 15:00:01Z luigi $ | 28 * $FreeBSD: head/tools/tools/netmap/pkt-gen.c 272962 2014-10-11 21:43:05Z gnn $ |
29 * $Id: pkt-gen.c 12346 2013-06-12 17:36:25Z luigi $ 30 * 31 * Example program to show how to build a multithreaded packet 32 * source/sink using the netmap device. 33 * 34 * In this example we create a programmable number of threads 35 * to take care of all the queues of the interface used to 36 * send or receive traffic. --- 160 unchanged lines hidden (view full) --- 197 int report_interval; /* milliseconds between prints */ 198 void *(*td_body)(void *); 199 void *mmap_addr; 200 char ifname[MAX_IFNAMELEN]; 201 char *nmr_config; 202 int dummy_send; 203 int virt_header; /* send also the virt_header */ 204 int extra_bufs; /* goes in nr_arg3 */ | 29 * $Id: pkt-gen.c 12346 2013-06-12 17:36:25Z luigi $ 30 * 31 * Example program to show how to build a multithreaded packet 32 * source/sink using the netmap device. 33 * 34 * In this example we create a programmable number of threads 35 * to take care of all the queues of the interface used to 36 * send or receive traffic. --- 160 unchanged lines hidden (view full) --- 197 int report_interval; /* milliseconds between prints */ 198 void *(*td_body)(void *); 199 void *mmap_addr; 200 char ifname[MAX_IFNAMELEN]; 201 char *nmr_config; 202 int dummy_send; 203 int virt_header; /* send also the virt_header */ 204 int extra_bufs; /* goes in nr_arg3 */ |
205 char *packet_file; /* -P option */ |
|
205}; 206enum dev_type { DEV_NONE, DEV_NETMAP, DEV_PCAP, DEV_TAP }; 207 208 209/* 210 * Arguments for a new thread. The same structure is used by 211 * the source and the sink 212 */ --- 6 unchanged lines hidden (view full) --- 219 struct nm_desc *nmd; 220 volatile uint64_t count; 221 struct timespec tic, toc; 222 int me; 223 pthread_t thread; 224 int affinity; 225 226 struct pkt pkt; | 206}; 207enum dev_type { DEV_NONE, DEV_NETMAP, DEV_PCAP, DEV_TAP }; 208 209 210/* 211 * Arguments for a new thread. The same structure is used by 212 * the source and the sink 213 */ --- 6 unchanged lines hidden (view full) --- 220 struct nm_desc *nmd; 221 volatile uint64_t count; 222 struct timespec tic, toc; 223 int me; 224 pthread_t thread; 225 int affinity; 226 227 struct pkt pkt; |
228 void *frame; |
|
227}; 228 229 230/* 231 * extract the extremes from a range of ipv4 addresses. 232 * addr_lo[-addr_hi][:port_lo[-port_hi]] 233 */ 234static void --- 373 unchanged lines hidden (view full) --- 608 struct ether_header *eh; 609 struct ip *ip; 610 struct udphdr *udp; 611 uint16_t paylen = targ->g->pkt_size - sizeof(*eh) - sizeof(struct ip); 612 const char *payload = targ->g->options & OPT_INDIRECT ? 613 indirect_payload : default_payload; 614 int i, l0 = strlen(payload); 615 | 229}; 230 231 232/* 233 * extract the extremes from a range of ipv4 addresses. 234 * addr_lo[-addr_hi][:port_lo[-port_hi]] 235 */ 236static void --- 373 unchanged lines hidden (view full) --- 610 struct ether_header *eh; 611 struct ip *ip; 612 struct udphdr *udp; 613 uint16_t paylen = targ->g->pkt_size - sizeof(*eh) - sizeof(struct ip); 614 const char *payload = targ->g->options & OPT_INDIRECT ? 615 indirect_payload : default_payload; 616 int i, l0 = strlen(payload); 617 |
618 char errbuf[PCAP_ERRBUF_SIZE]; 619 pcap_t *file; 620 struct pcap_pkthdr *header; 621 const unsigned char *packet; 622 623 /* Read a packet from a PCAP file if asked. */ 624 if (targ->g->packet_file != NULL) { 625 if ((file = pcap_open_offline(targ->g->packet_file, 626 errbuf)) == NULL) 627 D("failed to open pcap file %s", 628 targ->g->packet_file); 629 if (pcap_next_ex(file, &header, &packet) < 0) 630 D("failed to read packet from %s", 631 targ->g->packet_file); 632 if ((targ->frame = malloc(header->caplen)) == NULL) 633 D("out of memory"); 634 bcopy(packet, (unsigned char *)targ->frame, header->caplen); 635 targ->g->pkt_size = header->caplen; 636 pcap_close(file); 637 return; 638 } 639 |
|
616 /* create a nice NUL-terminated string */ 617 for (i = 0; i < paylen; i += l0) { 618 if (l0 > paylen - i) 619 l0 = paylen - i; // last round 620 bcopy(payload, pkt->body + i, l0); 621 } 622 pkt->body[i-1] = '\0'; 623 ip = &pkt->ip; --- 154 unchanged lines hidden (view full) --- 778 uint32_t sent = 0; 779 struct timespec ts, now, last_print; 780 uint32_t count = 0, min = 1000000000, av = 0; 781 782 frame = &targ->pkt; 783 frame += sizeof(targ->pkt.vh) - targ->g->virt_header; 784 size = targ->g->pkt_size + targ->g->virt_header; 785 | 640 /* create a nice NUL-terminated string */ 641 for (i = 0; i < paylen; i += l0) { 642 if (l0 > paylen - i) 643 l0 = paylen - i; // last round 644 bcopy(payload, pkt->body + i, l0); 645 } 646 pkt->body[i-1] = '\0'; 647 ip = &pkt->ip; --- 154 unchanged lines hidden (view full) --- 802 uint32_t sent = 0; 803 struct timespec ts, now, last_print; 804 uint32_t count = 0, min = 1000000000, av = 0; 805 806 frame = &targ->pkt; 807 frame += sizeof(targ->pkt.vh) - targ->g->virt_header; 808 size = targ->g->pkt_size + targ->g->virt_header; 809 |
786 | |
787 if (targ->g->nthreads > 1) { 788 D("can only ping with 1 thread"); 789 return NULL; 790 } 791 792 clock_gettime(CLOCK_REALTIME_PRECISE, &last_print); 793 now = last_print; 794 while (n == 0 || (int)sent < n) { --- 238 unchanged lines hidden (view full) --- 1033 int64_t sent = 0; 1034 int options = targ->g->options | OPT_COPY; 1035 struct timespec nexttime = { 0, 0}; // XXX silence compiler 1036 int rate_limit = targ->g->tx_rate; 1037 struct pkt *pkt = &targ->pkt; 1038 void *frame; 1039 int size; 1040 | 810 if (targ->g->nthreads > 1) { 811 D("can only ping with 1 thread"); 812 return NULL; 813 } 814 815 clock_gettime(CLOCK_REALTIME_PRECISE, &last_print); 816 now = last_print; 817 while (n == 0 || (int)sent < n) { --- 238 unchanged lines hidden (view full) --- 1056 int64_t sent = 0; 1057 int options = targ->g->options | OPT_COPY; 1058 struct timespec nexttime = { 0, 0}; // XXX silence compiler 1059 int rate_limit = targ->g->tx_rate; 1060 struct pkt *pkt = &targ->pkt; 1061 void *frame; 1062 int size; 1063 |
1041 frame = pkt; 1042 frame += sizeof(pkt->vh) - targ->g->virt_header; 1043 size = targ->g->pkt_size + targ->g->virt_header; 1044 | 1064 if (targ->frame == NULL) { 1065 frame = pkt; 1066 frame += sizeof(pkt->vh) - targ->g->virt_header; 1067 size = targ->g->pkt_size + targ->g->virt_header; 1068 } else { 1069 frame = targ->frame; 1070 size = targ->g->pkt_size; 1071 } 1072 |
1045 D("start, fd %d main_fd %d", targ->fd, targ->g->main_fd); 1046 if (setaffinity(targ->thread, targ->affinity)) 1047 goto quit; 1048 1049 /* main loop.*/ 1050 clock_gettime(CLOCK_REALTIME_PRECISE, &targ->tic); 1051 if (rate_limit) { 1052 targ->tic = timespec_add(targ->tic, (struct timespec){2,0}); --- 308 unchanged lines hidden (view full) --- 1361 "\t-c cores cores to use\n" 1362 "\t-p threads processes/threads to use\n" 1363 "\t-T report_ms milliseconds between reports\n" 1364 "\t-P use libpcap instead of netmap\n" 1365 "\t-w wait_for_link_time in seconds\n" 1366 "\t-R rate in packets per second\n" 1367 "\t-X dump payload\n" 1368 "\t-H len add empty virtio-net-header with size 'len'\n" | 1073 D("start, fd %d main_fd %d", targ->fd, targ->g->main_fd); 1074 if (setaffinity(targ->thread, targ->affinity)) 1075 goto quit; 1076 1077 /* main loop.*/ 1078 clock_gettime(CLOCK_REALTIME_PRECISE, &targ->tic); 1079 if (rate_limit) { 1080 targ->tic = timespec_add(targ->tic, (struct timespec){2,0}); --- 308 unchanged lines hidden (view full) --- 1389 "\t-c cores cores to use\n" 1390 "\t-p threads processes/threads to use\n" 1391 "\t-T report_ms milliseconds between reports\n" 1392 "\t-P use libpcap instead of netmap\n" 1393 "\t-w wait_for_link_time in seconds\n" 1394 "\t-R rate in packets per second\n" 1395 "\t-X dump payload\n" 1396 "\t-H len add empty virtio-net-header with size 'len'\n" |
1397 "\t-P file load packet from pcap file" |
|
1369 "", 1370 cmd); 1371 1372 exit(0); 1373} 1374 1375static void 1376start_threads(struct glob_arg *g) --- 256 unchanged lines hidden (view full) --- 1633 g.cpus = 1; 1634 g.forever = 1; 1635 g.tx_rate = 0; 1636 g.frags = 1; 1637 g.nmr_config = ""; 1638 g.virt_header = 0; 1639 1640 while ( (ch = getopt(arc, argv, | 1398 "", 1399 cmd); 1400 1401 exit(0); 1402} 1403 1404static void 1405start_threads(struct glob_arg *g) --- 256 unchanged lines hidden (view full) --- 1662 g.cpus = 1; 1663 g.forever = 1; 1664 g.tx_rate = 0; 1665 g.frags = 1; 1666 g.nmr_config = ""; 1667 g.virt_header = 0; 1668 1669 while ( (ch = getopt(arc, argv, |
1641 "a:f:F:n:i:Il:d:s:D:S:b:c:o:p:T:w:WvR:XC:H:e:m:")) != -1) { | 1670 "a:f:F:n:i:Il:d:s:D:S:b:c:o:p:T:w:WvR:XC:H:e:m:P:")) != -1) { |
1642 struct sf *fn; 1643 1644 switch(ch) { 1645 default: 1646 D("bad option %c %s", ch, optarg); 1647 usage(); 1648 break; 1649 --- 126 unchanged lines hidden (view full) --- 1776 if (strcmp(optarg, "tx") == 0) { 1777 g.options |= OPT_MONITOR_TX; 1778 } else if (strcmp(optarg, "rx") == 0) { 1779 g.options |= OPT_MONITOR_RX; 1780 } else { 1781 D("unrecognized monitor mode %s", optarg); 1782 } 1783 break; | 1671 struct sf *fn; 1672 1673 switch(ch) { 1674 default: 1675 D("bad option %c %s", ch, optarg); 1676 usage(); 1677 break; 1678 --- 126 unchanged lines hidden (view full) --- 1805 if (strcmp(optarg, "tx") == 0) { 1806 g.options |= OPT_MONITOR_TX; 1807 } else if (strcmp(optarg, "rx") == 0) { 1808 g.options |= OPT_MONITOR_RX; 1809 } else { 1810 D("unrecognized monitor mode %s", optarg); 1811 } 1812 break; |
1813 case 'P': 1814 g.packet_file = strdup(optarg); 1815 break; |
|
1784 } | 1816 } |
1817 |
|
1785 } 1786 1787 if (g.ifname == NULL) { 1788 D("missing ifname"); 1789 usage(); 1790 } 1791 1792 i = system_ncpus(); --- 184 unchanged lines hidden --- | 1818 } 1819 1820 if (g.ifname == NULL) { 1821 D("missing ifname"); 1822 usage(); 1823 } 1824 1825 i = system_ncpus(); --- 184 unchanged lines hidden --- |