Suggestions for communication between FreeBSD user-space and kernel modules

John-Mark Gurney jmg at funkthat.com
Sun Feb 15 05:02:56 UTC 2015


Yue Chen wrote this message on Sat, Feb 14, 2015 at 23:07 -0500:
> Thank you so much for replying.
> 
> I forgot to mention that I need to send several Megabytes of structured
> "uint64_t" data. Is the "ioctls" approach good to handle this situation, or
> "mmap" is better?

ioctl is perfectly fine, but instead of passing the all of the data,
you can just pass a pointer to the data, and then use copyin(9) to copy
the data into kernel...

> And for
> > "2) In the cdevsw structure, implement the ioctl method.  This method
> will be called when the userland application calls ioctl(2)",
> 
> the ioctl method will be automatically called, right?

Correct...

> On Sat, Feb 14, 2015 at 9:53 PM, Ryan Stone <rysto32 at gmail.com> wrote:
> 
> > The two main interfaces for passing data between userland and the
> > kernel in FreeBSD are syctls and ioctls (there are others but their
> > use is rather specialized).
> >
> > sysctls are typically the simplest to set up, but aren't well suited
> > for passing around complex structured data.  sysctls are very easy to
> > read and write from the command line, though, so they're popular for
> > exposing individual tuning parameters.  The kernel interfaces for
> > creating sysctls are documented here:
> >
> > https://www.freebsd.org/cgi/man.cgi?query=sysctl&apropos=0&sektion=9&manpath=FreeBSD+10.1-RELEASE&arch=default&format=html
> >
> > https://www.freebsd.org/cgi/man.cgi?query=sysctl_add_oid&apropos=0&sektion=0&manpath=FreeBSD+10.1-RELEASE&arch=default&format=html
> >
> >
> > ioctls are a little more complicated to use, but are more flexible in
> > what kind of data they can accept.  The man pages for this aren't as
> > good, but the basic steps are:
> >
> > 1) Create a device node in /dev by calling make_dev()
> > (
> > https://www.freebsd.org/cgi/man.cgi?query=make_dev&apropos=0&sektion=0&manpath=FreeBSD+10.1-RELEASE&arch=default&format=html
> > )
> > 2) In your userland application, call open(2) to get a file descriptor
> > and then ioctl(2) on the file descriptor to pass data to/from the
> > kernel
> > 2) In the cdevsw structure, implement the ioctl method.  This method
> > will be called when the userland application calls ioctl(2)
> > 3) The request argument using the macros in <sys/ioccom.h>.  _IOW is
> > for ioctls that send data from userland to the kernel, _IOR is for
> > ioctls that fetch data from the kernel and _IOWR is for ioctls that
> > both send data from userland to the kernel and fetch data back in a
> > single call.  Try to use unique values for your ioctls requests.
> >
> >
> >
> > Some of the other possible methods include include mmap, which can be
> > used to create shared memory between the kernel and userland;
> > netgraph, which is networking-focused interface; and sockets, which
> > can be a tricky interface to use correctly in the kernel.

-- 
  John-Mark Gurney				Voice: +1 415 225 5579

     "All that I will do, has been done, All that I have, has not."


More information about the freebsd-hackers mailing list