Reputation: 47
I have to transfer several Megabytes of data per second from a Linux-Kernel Module to User-Space, and I don't want miss any message from the Module, if I read from it. What is the best way to do this?
There are quite few possible solutions for this: Named-Pipes, Proc-File and a Block-Device But I am not sure which one to choose and which one promises the best performance since I am a Kernel-Newbie.
At the moment I use a Ring-Buffer (with spinlocks) in the Kernel Module to store the messages and if the Proc-File is being read I put data from the Ring-Buffer to the Proc-File;
on the User side I have a program that runs cat /proc/procfile
repeatedly and shows the output. The problem with this solutions is that instead of getting
MESSAGE 1 MESSAGE 2 MESSAGE 3
on the output, I see (sometimes, once every few thousand messages)
MESSAGE 1 MESSMESSAGE3
Upvotes: 1
Views: 5068
Reputation: 6081
It would always be possible to implement what I think of as the "poor man's syscall": create a char device and then create a custom ioctl with whatever semantics you want.
In this case I presume you'd have an ioctl that passes in a userspace buffer, and returns a chunk of data from a circular buffer you hold in the kernel.
With careful use of atomic variables and spinlocks, you should be able to guarantee fast, safe access to the data, even across multiple threads if necessary.
Upvotes: 2
Reputation: 6319
I believe character device would be good solution for you.
Upvotes: 1
Reputation: 4444
Many methods are usable. Netlink however isn't one of them, because it is not a reliable transport (like UDP). A character device seems in order, though you could also use a TCP socket (cf. nfsd).
Upvotes: 0
Reputation: 2720
You probably want to use the relay interface, formerly known as relayfs.
See Documentation/filesystems/relay.txt
.
From there:
The relay interface provides a means for kernel applications to efficiently log and transfer large quantities of data from the kernel to userspace via user-defined 'relay channels'.
Upvotes: 3