Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 30 May 2012 18:00:30 +0200
From:      rizzo <rizzo@iet.unipi.it>
To:        "root@9du.org" <root@9du.org>
Cc:        freebsd-current <freebsd-current@freebsd.org>
Subject:   Re: about netmap libpcap pcap_inject
Message-ID:  <20120530160030.GA61877@onelab2.iet.unipi.it>
In-Reply-To: <201205302331315153849@9du.org>
References:  <20120530120027.2CDAF1065678@hub.freebsd.org> <201205302331315153849@9du.org>

next in thread | previous in thread | raw e-mail | index | archive | help
On Wed, May 30, 2012 at 11:31:36PM +0800, root@9du.org wrote:
> @pcap.c
> 
> int
> pcap_inject(pcap_t *p, const void *buf, size_t size)
> {
>         struct my_ring *me = p;
>         u_int si;
>  
>         ND("cnt %d", cnt);
>         /* scan all rings */
>         for (si = me->begin; si < me->end; si++) {
>                 struct netmap_ring *ring = NETMAP_TXRING(me->nifp, si);
>  
>                 ND("ring has %d pkts", ring->avail);
>                 if (ring->avail == 0)
>                         continue;
> 		u_int i = ring->cur;
> 		u_int idx = ring->slot[i].buf_idx;
> 		if (idx < 2) {
> 			D("%s bogus TX index %d at offset %d",
> 				me->nifp->ni_name, idx, i);
> 			sleep(2);
> 		}
> 		u_char *dst = (u_char *)NETMAP_BUF(ring, idx);
> 		ring->slot[i].len = size;
> 		pkt_copy(buf, dst, size);
> 		ring->cur = NETMAP_RING_NEXT(ring, i);
> 		ring->avail--;
> 		return size;
>         }
> 	errno = ENOBUFS;
> 	return -1;
> }
> 
> i call this fun, packet can 't send out my host.

the packet go out when you issue an ioctl or poll/select on the
netmap file descriptor. Many libpcap apps do that implicitly
because they run around an event loop, but if yours does not
then you need to insert the call yourself, either as you did in
the example below (the NS_REPORT is not necessary), or
as part of your normal processing loop when you perhaps
are blocking to check for other file descriptors.

cheers
luigi

> @pkt-gen.c
> static int
> send_packets(struct netmap_ring *ring, struct pkt *pkt, 
> 		int size, u_int count, int options)
> {
> 	u_int sent, cur = ring->cur;
> 
> 	if (ring->avail < count)
> 		count = ring->avail;
> 
> 	for (sent = 0; sent < count; sent++) {
> 		struct netmap_slot *slot = &ring->slot[cur];
> 		char *p = NETMAP_BUF(ring, slot->buf_idx);
> 
> 		if (options & OPT_COPY)
> 			pkt_copy(pkt, p, size);
> 		else if (options & OPT_MEMCPY)
> 			memcpy(p, pkt, size);
> 		else if (options & OPT_PREFETCH)
> 			prefetch(p);
> 
> 		slot->len = size;
> 		if (sent == count - 1)
> 			slot->flags |= NS_REPORT;
> 		cur = NETMAP_RING_NEXT(ring, cur);
> 	}
> 	ring->avail -= sent;
> 	ring->cur = cur;
> 
> 	return (sent);
> }
> 
> code like this
> if (sent == count - 1)
> 	slot->flags |= NS_REPORT;
> 
> add  code to pcap_inject
> 
> if (ring->avail == 0) ioctl(me->fd, NIOCTXSYNC, NULL);
> 
> packet can send ,but send too slowly!
> 
> have any good idea to be change this?



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20120530160030.GA61877>