From owner-freebsd-hackers@FreeBSD.ORG Sun Feb 15 02:53:55 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 6895C166 for ; Sun, 15 Feb 2015 02:53:55 +0000 (UTC) Received: from mail-la0-f42.google.com (mail-la0-f42.google.com [209.85.215.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority G2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 11CACA81 for ; Sun, 15 Feb 2015 02:53:54 +0000 (UTC) Received: by labms9 with SMTP id ms9so16916544lab.10 for ; Sat, 14 Feb 2015 18:53:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=N0oHKXZxNLRp1a8vyugdKODVRDOjl8AjTe+Q2ZybiqQ=; b=zLYO2ZCBgXthGnQmqHuGPEtQEDzyR/ixp6s+wRQS4s4vyulD7an4csiaSnnzF2A1aa YAdpEs5G9UpybIfBhZSSngMP0RaEikMtrN5m00k/EdoYOFhFuz60ueyTFYycN/tbjTS5 bEc/A8A152eUMUNGlgsvGP6LEdC4Rq1L5LSmf7wh3vtHTTWiKSSMcpdRsGeMvGycfswB MePY5V052S2SVegq0YiI8m1qLFpBuKe8Hiyzlp9na/ziJrVy5O7kjwyqVUTATkaVQAtS rVJDCoLyBs0czWtn48n29OJdHACtXDBXVbsRbUcDS5OalwAd9oAfcRIOuhmu7RB1VeYU k70Q== MIME-Version: 1.0 X-Received: by 10.152.21.100 with SMTP id u4mr1889259lae.41.1423968832799; Sat, 14 Feb 2015 18:53:52 -0800 (PST) Received: by 10.114.78.131 with HTTP; Sat, 14 Feb 2015 18:53:52 -0800 (PST) In-Reply-To: References: Date: Sat, 14 Feb 2015 21:53:52 -0500 Message-ID: Subject: Re: Suggestions for communication between FreeBSD user-space and kernel modules From: Ryan Stone To: Yue Chen Content-Type: text/plain; charset=UTF-8 Cc: "freebsd-hackers@freebsd.org" 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 02:53:55 -0000 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.