LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-30-2012, 04:06 PM   #1
bayoulinux
Member
 
Registered: Oct 2011
Posts: 34

Rep: Reputation: Disabled
PACKET_MMAP used to receive raw packets into user space


Hello:

I'm looking for a coding example to better understand how to use PACKET_MMAP for packet reception into user space. I'm trying to read raw L2 packets bound to an ethernet interface and get them into the shared memory ring buffer to be used for user space applications to manipulate.

I have seen the documentation at:

Documentation/networking/packet_mmap.txt

http://wiki.ipxwarzone.com/index.php...ux_packet_mmap

Note has a decent example of transmission, but I could really use an rx example.

This one has potential but I still just don't quite get it:

http://www.scaramanga.co.uk/code-fu/lincap.c

http://yusufonlinux.blogspot.com/201...zero-copy.html

but its still unclear to me how to read the memory mapped raw packets from a buffer in user space. A working example would be great for self study...

Thanks!
 
Old 05-30-2012, 05:16 PM   #2
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
The idea is simple: you allocate a buffer in an area of memory that has been made directly accessible both to your process and to the kernel. You are obliged to initialize that area of memory in the form of a linked list of equal-sized slots. Now, when you access the socket (which you do using the same "poll()" based logic as before), the kernel moves the data directly into that agreed-upon area of memory, thereby occupying zero-or-more of those slots. Your process wakes up to (maybe...) discover that zero or more of those slots are now "magically" filled with data. It processes the content of the slots in the required way, then goes to sleep again.

The advantages of this arrangement are that (a) the data only has to be copied once (by the kernel, directly to/from a location that is agreeable to you both); and (b) more than one piece of data can be exchanged between your process and the kernel using only one "wakeup call." The number of trips through the system dispatcher and the number of system-calls in general is greatly reduced.

The source-code in lincap.c illustrates the procedure. The user-land process allocates the buffer, initializes it, announces it to the kernel when opening the socket, then enters an endless loop which consists first of processing the zero-or-more chunks of data that it might find there, then waiting for more. The loop is constructed in the manner that it is (with the poll call at the bottom) in recognition of the fact that the buffer might be filled with some data when the socket is successfully opened.

Last edited by sundialsvcs; 05-30-2012 at 05:19 PM.
 
Old 05-31-2012, 05:26 AM   #3
bayoulinux
Member
 
Registered: Oct 2011
Posts: 34

Original Poster
Rep: Reputation: Disabled
Thank you for the reply. I'm trying to build the lincap.c example but I'm failing with the problem:

gcc lincap.c
lincap.c:29:24: fatal error: asm/system.h: No such file or directory
compilation terminated.

Some of my system info:

uname -a
Linux swamp 3.3.4-3.fc16.x86_64 #1 SMP Thu May 3 14:46:44 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux

ls /usr/src/kernels/3.3.4-3.fc16.x86_64/include/asm-generic/system.h
/usr/src/kernels/3.3.4-3.fc16.x86_64/include/asm-generic/system.h

So I change the include path from <asm/system.h> to <asm-generic/system.h>:

gcc lincap.c
lincap.c:29:32: fatal error: asm-generic/system.h: No such file or directory
compilation terminated.

So I then brute force the whole path in there:

#include "/usr/src/kernels/3.3.4-3.fc16.x86_64/include/asm-generic/system.h"

but I get:

gcc lincap.c
In file included from lincap.c:30:0:
/usr/src/kernels/3.3.4-3.fc16.x86_64/include/asm-generic/system.h:20:28: fatal error: linux/irqflags.h: No such file or directory
compilation terminated.

I don't think I'm going about this correctly... it seems like system.h is needed for a function called mb(). Any help with the build would be appreciated.
 
Old 10-31-2012, 09:56 AM   #4
DavidA
LQ Newbie
 
Registered: Aug 2011
Location: UK
Posts: 11

Rep: Reputation: Disabled
Hi

Did you manage to build lincap.c?

I also face the issue that lincap.c uses mb(), which is defined in asm/system.h. But the code in asm/system.h is embraced by #ifdef __KERNEL__. How is this supposed to work if lincap is a user space application?

BR

David
 
Old 01-04-2013, 06:39 AM   #5
menda90
LQ Newbie
 
Registered: Jan 2013
Posts: 3

Rep: Reputation: Disabled
Have you work it out? I've got the same problem as you. When I removed "mb()", I compiled the source and I could access first frame in buffer. But when I was trying to write "tp_status" to buffer it gives me "Segmentation fault".
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to receive the signal multiple times in user space without terminating it? pobitro Linux - Embedded & Single-board computer 1 07-12-2016 08:54 AM
Send/Receive multiple raw packets in one system call coreykasten Linux - Networking 1 08-29-2010 11:52 AM
iptables/libipq questions: buffering issues when copying packets to user space beatdream Linux - Networking 2 04-14-2010 10:05 AM
Load Packets in user-space and don't change MAC x1228 Linux - Networking 2 06-07-2006 08:42 PM
raw packets bassdemon Programming 7 01-28-2005 12:23 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:54 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration