Deleted Added
full compact
pkt-gen.c (238081) pkt-gen.c (238165)
1/*
2 * Copyright (C) 2011 Matteo Landi, Luigi Rizzo. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.

--- 10 unchanged lines hidden (view full) ---

19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 * SUCH DAMAGE.
24 */
25
26/*
1/*
2 * Copyright (C) 2011 Matteo Landi, Luigi Rizzo. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.

--- 10 unchanged lines hidden (view full) ---

19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 * SUCH DAMAGE.
24 */
25
26/*
27 * $FreeBSD: head/tools/tools/netmap/pkt-gen.c 238081 2012-07-03 17:50:44Z emaste $
27 * $FreeBSD: head/tools/tools/netmap/pkt-gen.c 238165 2012-07-06 13:21:23Z emaste $
28 * $Id: pkt-gen.c 10967 2012-05-03 11:29:23Z luigi $
29 *
30 * Example program to show how to build a multithreaded packet
31 * source/sink using the netmap device.
32 *
33 * In this example we create a programmable number of threads
34 * to take care of all the queues of the interface used to
35 * send or receive traffic.

--- 150 unchanged lines hidden (view full) ---

186/*
187 * Arguments for a new thread. The same structure is used by
188 * the source and the sink
189 */
190struct targ {
191 struct glob_arg *g;
192 int used;
193 int completed;
28 * $Id: pkt-gen.c 10967 2012-05-03 11:29:23Z luigi $
29 *
30 * Example program to show how to build a multithreaded packet
31 * source/sink using the netmap device.
32 *
33 * In this example we create a programmable number of threads
34 * to take care of all the queues of the interface used to
35 * send or receive traffic.

--- 150 unchanged lines hidden (view full) ---

186/*
187 * Arguments for a new thread. The same structure is used by
188 * the source and the sink
189 */
190struct targ {
191 struct glob_arg *g;
192 int used;
193 int completed;
194 int cancel;
194 int fd;
195 struct nmreq nmr;
196 struct netmap_if *nifp;
197 uint16_t qfirst, qlast; /* range of queues to scan */
198 uint64_t count;
199 struct timeval tic, toc;
200 int me;
201 pthread_t thread;

--- 14 unchanged lines hidden (view full) ---

216
217static struct targ *targs;
218static int global_nthreads;
219
220/* control-C handler */
221static void
222sigint_h(__unused int sig)
223{
195 int fd;
196 struct nmreq nmr;
197 struct netmap_if *nifp;
198 uint16_t qfirst, qlast; /* range of queues to scan */
199 uint64_t count;
200 struct timeval tic, toc;
201 int me;
202 pthread_t thread;

--- 14 unchanged lines hidden (view full) ---

217
218static struct targ *targs;
219static int global_nthreads;
220
221/* control-C handler */
222static void
223sigint_h(__unused int sig)
224{
224 for (int i = 0; i < global_nthreads; i++) {
225 /* cancel active threads. */
226 if (targs[i].used == 0)
227 continue;
225 for (int i = 0; i < global_nthreads; i++)
226 targs[i].cancel = 1;
228
227
229 D("Cancelling thread #%d\n", i);
230 pthread_cancel(targs[i].thread);
231 targs[i].used = 0;
232 }
233
234 signal(SIGINT, SIG_DFL);
235}
236
237
238/* sysctl wrapper to return the number of active CPUs */
239static int
240system_ncpus(void)
241{

--- 248 unchanged lines hidden (view full) ---

490
491 /* main loop.*/
492 gettimeofday(&targ->tic, NULL);
493 if (targ->g->use_pcap) {
494 int size = targ->g->pkt_size;
495 void *pkt = &targ->pkt;
496 pcap_t *p = targ->g->p;
497
228 signal(SIGINT, SIG_DFL);
229}
230
231
232/* sysctl wrapper to return the number of active CPUs */
233static int
234system_ncpus(void)
235{

--- 248 unchanged lines hidden (view full) ---

484
485 /* main loop.*/
486 gettimeofday(&targ->tic, NULL);
487 if (targ->g->use_pcap) {
488 int size = targ->g->pkt_size;
489 void *pkt = &targ->pkt;
490 pcap_t *p = targ->g->p;
491
498 for (i = 0; sent < n; i++) {
492 for (i = 0; sent < n && !targ->cancel; i++) {
499 if (pcap_inject(p, pkt, size) != -1)
500 sent++;
501 if (i > 10000) {
502 targ->count = sent;
503 i = 0;
504 }
505 }
506 } else {
507 while (sent < n) {
508
509 /*
510 * wait for available room in the send queue(s)
511 */
512 if (poll(fds, 1, 2000) <= 0) {
493 if (pcap_inject(p, pkt, size) != -1)
494 sent++;
495 if (i > 10000) {
496 targ->count = sent;
497 i = 0;
498 }
499 }
500 } else {
501 while (sent < n) {
502
503 /*
504 * wait for available room in the send queue(s)
505 */
506 if (poll(fds, 1, 2000) <= 0) {
507 if (targ->cancel)
508 break;
513 D("poll error/timeout on queue %d\n", targ->me);
514 goto quit;
515 }
516 /*
517 * scan our queues and send on those with room
518 */
519 if (sent > 100000 && !(targ->g->options & OPT_COPY) )
520 options &= ~OPT_COPY;
509 D("poll error/timeout on queue %d\n", targ->me);
510 goto quit;
511 }
512 /*
513 * scan our queues and send on those with room
514 */
515 if (sent > 100000 && !(targ->g->options & OPT_COPY) )
516 options &= ~OPT_COPY;
521 for (i = targ->qfirst; i < targ->qlast; i++) {
517 for (i = targ->qfirst; i < targ->qlast && !targ->cancel; i++) {
522 int m, limit = MIN(n - sent, targ->g->burst);
523
524 txring = NETMAP_TXRING(nifp, i);
525 if (txring->avail == 0)
526 continue;
527 m = send_packets(txring, &targ->pkt, targ->g->pkt_size,
528 limit, options);
529 sent += m;
530 targ->count = sent;
531 }
518 int m, limit = MIN(n - sent, targ->g->burst);
519
520 txring = NETMAP_TXRING(nifp, i);
521 if (txring->avail == 0)
522 continue;
523 m = send_packets(txring, &targ->pkt, targ->g->pkt_size,
524 limit, options);
525 sent += m;
526 targ->count = sent;
527 }
528 if (targ->cancel)
529 break;
532 }
533 /* flush any remaining packets */
534 ioctl(fds[0].fd, NIOCTXSYNC, NULL);
535
536 /* final part: wait all the TX queues to be empty. */
537 for (i = targ->qfirst; i < targ->qlast; i++) {
538 txring = NETMAP_TXRING(nifp, i);
539 while (!NETMAP_TX_RING_EMPTY(txring)) {

--- 69 unchanged lines hidden (view full) ---

609 if (i > 0 && !(fds[0].revents & POLLERR))
610 break;
611 D("waiting for initial packets, poll returns %d %d", i, fds[0].revents);
612 }
613
614 /* main loop, exit after 1s silence */
615 gettimeofday(&targ->tic, NULL);
616 if (targ->g->use_pcap) {
530 }
531 /* flush any remaining packets */
532 ioctl(fds[0].fd, NIOCTXSYNC, NULL);
533
534 /* final part: wait all the TX queues to be empty. */
535 for (i = targ->qfirst; i < targ->qlast; i++) {
536 txring = NETMAP_TXRING(nifp, i);
537 while (!NETMAP_TX_RING_EMPTY(txring)) {

--- 69 unchanged lines hidden (view full) ---

607 if (i > 0 && !(fds[0].revents & POLLERR))
608 break;
609 D("waiting for initial packets, poll returns %d %d", i, fds[0].revents);
610 }
611
612 /* main loop, exit after 1s silence */
613 gettimeofday(&targ->tic, NULL);
614 if (targ->g->use_pcap) {
617 for (;;) {
615 while (!targ->cancel) {
618 pcap_dispatch(targ->g->p, targ->g->burst, receive_pcap, NULL);
619 }
620 } else {
616 pcap_dispatch(targ->g->p, targ->g->burst, receive_pcap, NULL);
617 }
618 } else {
621 while (1) {
619 while (!targ->cancel) {
622 /* Once we started to receive packets, wait at most 1 seconds
623 before quitting. */
624 if (poll(fds, 1, 1 * 1000) <= 0) {
625 gettimeofday(&targ->toc, NULL);
626 targ->toc.tv_sec -= 1; /* Subtract timeout time. */
627 break;
628 }
629

--- 481 unchanged lines hidden ---
620 /* Once we started to receive packets, wait at most 1 seconds
621 before quitting. */
622 if (poll(fds, 1, 1 * 1000) <= 0) {
623 gettimeofday(&targ->toc, NULL);
624 targ->toc.tv_sec -= 1; /* Subtract timeout time. */
625 break;
626 }
627

--- 481 unchanged lines hidden ---