Archives For module parameters

Module/kernel parameters

November 20, 2007 — 6 Comments

>Sometimes it’s desirable to pass some arguments to module to customize its behavior, e.g.: to set/unset verbosity level of debug output, set operation mode etc… Linux modules can get this information from insmod utility, but kldload is not capable of doing such kind of things. What a pity. But don’t get desperate – tunables to the rescue!

Just like an ordinary command shell (bash, csh, sh) kernel has its own environment, the set of <name, value> pairs. You can get, set, test, unset these variables using getenv, setenv, testenv, unsetenv functions in the kernel and kenv(2) syscall or kenv(1) command in userland. So if you want to set verbosity level for module, you would do something like this:

# kenv zaptel.debug=1
# kldload ./zaptel.ko

and then in module initialization routine:

static int debug = 0; /* Hush-hush */
char * value = getenv("zaptel.debug");
if (value) {
debug = strtol(value, NULL, 10);

Too much code for such a simple task, don’t you think? Yeah, like for every common task there are useful macroses defined in kernel headers, you should just find them. Heavy coffeinated kernel hackers knew most of them. In this particular case neat code would look something like that (for statically initialized variable):

static int debug = 0; /* Hush-hush */
TUNABLE_INT("zaptel.debug", &debug);

or, if debug is the member of structure or local variable:

sc->debug = 0;
TUNABLE_INT_FETCH("zaptel.debug", &sc->debug);

TUNABLE_XXX macroses exist for INT, LONG, ULONG and STR types. TUNABLE_STR unlike the others requires third parameter – maximum size of the string. For a dynamic value retrieval every type has TUNABLE_XXX_FETCH macro defined. Nota bene: if there is no environent variable set with requested name, the value of acceptor variable remains untouched.