From owner-freebsd-hackers@FreeBSD.ORG Sun Feb 15 05:02:56 2015 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 03651496 for ; Sun, 15 Feb 2015 05:02:56 +0000 (UTC) Received: from gold.funkthat.com (gate2.funkthat.com [208.87.223.18]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "gold.funkthat.com", Issuer "gold.funkthat.com" (not verified)) by mx1.freebsd.org (Postfix) with ESMTPS id D0E2C89D for ; Sun, 15 Feb 2015 05:02:55 +0000 (UTC) Received: from gold.funkthat.com (localhost [127.0.0.1]) by gold.funkthat.com (8.14.5/8.14.5) with ESMTP id t1F52rhd002679 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Sat, 14 Feb 2015 21:02:53 -0800 (PST) (envelope-from jmg@gold.funkthat.com) Received: (from jmg@localhost) by gold.funkthat.com (8.14.5/8.14.5/Submit) id t1F52r4r002678; Sat, 14 Feb 2015 21:02:53 -0800 (PST) (envelope-from jmg) Date: Sat, 14 Feb 2015 21:02:53 -0800 From: John-Mark Gurney To: Yue Chen Subject: Re: Suggestions for communication between FreeBSD user-space and kernel modules Message-ID: <20150215050252.GZ1953@funkthat.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Operating-System: FreeBSD 9.1-PRERELEASE amd64 X-PGP-Fingerprint: 54BA 873B 6515 3F10 9E88 9322 9CB1 8F74 6D3F A396 X-Files: The truth is out there X-URL: http://resnet.uoregon.edu/~gurney_j/ X-Resume: http://resnet.uoregon.edu/~gurney_j/resume.html X-TipJar: bitcoin:13Qmb6AeTgQecazTWph4XasEsP7nGRbAPE X-to-the-FBI-CIA-and-NSA: HI! HOW YA DOIN? can i haz chizburger? User-Agent: Mutt/1.5.21 (2010-09-15) X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.2.7 (gold.funkthat.com [127.0.0.1]); Sat, 14 Feb 2015 21:02:53 -0800 (PST) Cc: "freebsd-hackers@freebsd.org" , Ryan Stone X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 15 Feb 2015 05:02:56 -0000 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 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 . _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."