Date: Sat, 14 Feb 2015 21:53:52 -0500 From: Ryan Stone <rysto32@gmail.com> To: Yue Chen <ycyc321@gmail.com> Cc: "freebsd-hackers@freebsd.org" <freebsd-hackers@freebsd.org> Subject: Re: Suggestions for communication between FreeBSD user-space and kernel modules Message-ID: <CAFMmRNw9rYWOx%2BX8sAh2oXUbgjkRhQMiKK8JnPTft9rAay1K_w@mail.gmail.com> In-Reply-To: <CAKtBrB47mCeTCFjrHpVww0LXJupDq_zYOy_z78pM8AV0U6hUkw@mail.gmail.com> References: <CAKtBrB47mCeTCFjrHpVww0LXJupDq_zYOy_z78pM8AV0U6hUkw@mail.gmail.com>
next in thread | previous in thread | raw e-mail | index | archive | help
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.
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?CAFMmRNw9rYWOx%2BX8sAh2oXUbgjkRhQMiKK8JnPTft9rAay1K_w>