Using AF_PACKET network buffers remain occupied?

Josip Gracin (grac@fly.cc.fer.hr)
07 Apr 1998 14:12:22 +0200


Hi!

I'm having trouble using AF_PACKET interface. Can someone please tell me
where I can find an example of using AF_PACKET interface? I know this has
already been asked on the list but the search engine at linuxhq doesn't
work. Anyway, I've managed to figure some of it out from the kernel
source, but I'm not sure if I got everything right.

Ok, now to the real question and problem. Here's how I use AF_PACKET:

int
main()
{
char buffer[1514];
struct sockaddr_ll sll;
int sfd, kr, i;
unsigned short protocol;
struct ifreq ifr;

sfd = socket (AF_PACKET, SOCK_RAW, 0);
if (sfd < 0)
fatal (1, "socket()");

/* Find the device. */
memcpy (ifr.ifr_name, "eth0", 5);
kr = ioctl (sfd, SIOCGIFINDEX, &ifr);
if (kr)
fatal (1, "ioctl()");
sll.sll_ifindex = ifr.ifr_ifindex;
sll.sll_family = AF_PACKET;
sll.sll_protocol = AMTP_PROTO; /* this is right, isn't it? no htons.*/
sll.sll_pkttype = PACKET_OTHERHOST;
/* should I set anything else before binding? */

kr = bind (sfd, (struct sockaddr *) &sll, sizeof (struct sockaddr_ll));
if (kr)
fatal (2, "bind()");

/* Create a dummy ethernet packet. */
protocol = htons (AMTP_PROTO);
memcpy (buffer, dstaddr, 6);
memcpy (buffer+6, srcaddr, 6);
memcpy (buffer+12, &protocol, 2);
memset (buffer+14, 0xBA, 1500);

/* Now let's try to send three packets at fast rate. */
for (i = 0; i < 3; i++) {
kr = write (sfd, buffer, 1514);
if (kr < 0)
fatal (1, "write()");
printf ("packet written\n");
}

close (sfd);
}

When I run this program, it prints the three "packet written" lines and
exits and that seems like a normal behaviour. But tcpdump on eth0 show
the following sequence of events:

(1) the first packet is displayed immediately and Mem-info on
Shift-ScrLock says: "Networking buffers in use: 2".

(2) 7-10 seconds later the second packet is displayed. Networking buffers
in use: 1.

And that's it. The third packet is "never" displayed. That "never" is
quoted because there is a manual way to flush it. If I now change the
program to send only one packet and run it, the first packet that is
immediately shown by tcpdump is that third packet queued on the previous
run, and after 7 seconds the newly created packet is displayed. And
networking buffers in use becomes 0 again. If there weren't for another
packet, that third packet would never come out.

Any ideas what is wrong?

-- 
| Josip Gracin, student at Faculty of EE and Computing in Zagreb, Croatia
| mailto:grac@fly.cc.fer.hr, http://fly.cc.fer.hr/~grac

- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.rutgers.edu