From owner-freebsd-current@FreeBSD.ORG Fri Jun 27 15:51:36 2014 Return-Path: Delivered-To: freebsd-current@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 94D23937; Fri, 27 Jun 2014 15:51:36 +0000 (UTC) Received: from bigwig.baldwin.cx (bigwig.baldwin.cx [IPv6:2001:470:1f11:75::1]) (using TLSv1 with cipher DHE-RSA-CAMELLIA256-SHA (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 6D6C12D7B; Fri, 27 Jun 2014 15:51:36 +0000 (UTC) Received: from jhbbsd.localnet (unknown [209.249.190.124]) by bigwig.baldwin.cx (Postfix) with ESMTPSA id 80E50B917; Fri, 27 Jun 2014 11:51:34 -0400 (EDT) From: John Baldwin To: freebsd-current@freebsd.org Subject: Re: do we have a generic string-number sysctl mapping library ? Date: Fri, 27 Jun 2014 10:56:33 -0400 User-Agent: KMail/1.13.5 (FreeBSD/8.4-CBSD-20140415; KDE/4.5.5; amd64; ; ) References: <20140627091459.GA50710@onelab2.iet.unipi.it> In-Reply-To: <20140627091459.GA50710@onelab2.iet.unipi.it> MIME-Version: 1.0 Content-Type: Text/Plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Message-Id: <201406271056.33331.jhb@freebsd.org> X-Greylist: Sender succeeded SMTP AUTH, not delayed by milter-greylist-4.2.7 (bigwig.baldwin.cx); Fri, 27 Jun 2014 11:51:34 -0400 (EDT) Cc: Luigi Rizzo , current@freebsd.org X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 27 Jun 2014 15:51:36 -0000 On Friday, June 27, 2014 5:14:59 am Luigi Rizzo wrote: > Hi, > I have frequently found myself using sysctls to control some kernel > feature where a string would be a better (and sometimes the only) > option than using a numeric value, yet the internal representation > should be numeric for speed and robustness. > Examples are the kern.timecounter, the default scheduler in dummynet, > and now in netmap the selection between native and emulated mode. > I am sure many of you can come up with other cases. > > I wonder if we have some support for that already in the sysctl code, > or i should build a generic one next time i need to do that. > > Feel free to criticise the approach and suggest better ones. > Right now i am using sysctls because i have a set of macros > and wrapper functions that let me convert them to sysfs > entries when building kernel code on linux, so I have a > portable solutions. > > For the details, I'd like to have a mechanism that requires the > kernel programmer supply a (possibly extensible) table of > supported values, and matching constants to be used within > the kernel. A single declaration should generate entries > to get/set the current value as well as list options. > We can discuss frills (such as wildcards, multiple values,etc). I am not aware of such a beast. Even just supporting a simple table to map labels to indices would be nice and would handle many cases. I.e. if you had something like: struct sysctl_table vals[] = { "foo", (void *)1, "bar", (void *)2, NULL, NULL }; static int myval; static int my_sysctl(SYSCTL_HANDLER_ARGS) { void *val; int error; val = (void *)myval; error = sysctl_handle_table(oidp, vals, &val, req); if (error || req->newptr == NULL) return (error); myval = (intptr_t)val; return (0); } sysctl_handle_table() would use the initial value to find a suitable string from the table for the "old" string, etc. Using void * for the value would let you store arbitrary data, etc. -- John Baldwin