Re: about netmap libpcap pcap_inject

From: rizzo <rizzo_at_iet.unipi.it>
Date: Wed, 30 May 2012 18:00:30 +0200
On Wed, May 30, 2012 at 11:31:36PM +0800, root_at_9du.org wrote:
> _at_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

> _at_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?
Received on Wed May 30 2012 - 13:39:56 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:40:27 UTC