Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 14 Feb 2015 21:02:53 -0800
From:      John-Mark Gurney <jmg@funkthat.com>
To:        Yue Chen <ycyc321@gmail.com>
Cc:        "freebsd-hackers@freebsd.org" <freebsd-hackers@freebsd.org>, Ryan Stone <rysto32@gmail.com>
Subject:   Re: Suggestions for communication between FreeBSD user-space and kernel modules
Message-ID:  <20150215050252.GZ1953@funkthat.com>
In-Reply-To: <CAKtBrB4tTe84N-aScjsFTDY8zO-ddLfE6syNROzBSRu2AdbzDg@mail.gmail.com>
References:  <CAKtBrB47mCeTCFjrHpVww0LXJupDq_zYOy_z78pM8AV0U6hUkw@mail.gmail.com> <CAFMmRNw9rYWOx%2BX8sAh2oXUbgjkRhQMiKK8JnPTft9rAay1K_w@mail.gmail.com> <CAKtBrB4tTe84N-aScjsFTDY8zO-ddLfE6syNROzBSRu2AdbzDg@mail.gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
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@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."



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20150215050252.GZ1953>