3. Self introduction
Name 沖 勝 (Masaru OKI) oki@n.o
History
1993 Port NetBSD to Sharp X68030
2001- IIJ SEIL team
2013- Stratosphere SDN related job
Last week touch kernel module :-)
4. What is kernel module?
Add or remove kernel function dynamically.
● filesystem
● device driver
● etc.
No kernel module case, we need edit kernel
configuration, compile kernel, replacement
kernel binary, and reboot.
5. modern?
historical: LKM (Loadable Kernel Module)
reesrved entry in the table.
fixed limit number of entries.
no loading at bootstrap.
modern: options MODULAR framework
no reserved entry
loading at bootstrap (see boot.cfg(5))
6. placement of kernel module
/starnd/<arch>/<ver>/modules/<name>/<name>.kmod
<arch> amd64, i386,
<ver> 6.0, 6.1,
<name> iscsi, zfs, compat_linux, wbsio,
kernel module is also in NetBSD kernel binary,
there are named ‘builtin module’.
10. Loading at bootstrap
/boot.cfg (amd64, i386 only)
menu=Boot normally:rndseed /var/db/entropy-file;boot netbsd
menu=Boot single user:rndseed /var/db/entropy-file;boot netbsd -s
menu=Boot with module foo:load /foo.kmod;boot
# always load example module
load=/foo/bar/example.kmod
11. Writing kernel module, easy?
Yes, easy!
also easy panic reboot...
but don’t worry.
kernel module needs two definitions
MODULE(<class>, <name>, <required>);
<name>_modcmd()
12. MODULE()
Declare kernel module.
MODULE(<class>, <name>, <required>)
class Module class.
name name of module
required required module string if needed
MODULE(MODULE_CLASS_DRIVER, vnd, ”zlib”);
13. MODULE(): class
MODULE_CLASS_DRIVER device driver
MODULE_CLASS_EXEC executable image handler
MODULE_CLASS_MISC moscellaneous module
MODULE_CLASS_SECMODEL security model
MODULE_CLASS_VFS virtual filesystem
16. Other item requires for compile
Kernel source needed.
● extract to /usr/src/sys
● other place, specify e.g. S=/foo/src
17. Example (do nothing)
test by modload, modstat and modunload.
#include <sys/module.h>
MODULE(MODULE_CLASS_MISC, example, NULL);
static int
example_modcmd(modcmd_t cmd, void *arg)
{
return 0;
}
18. Add sysctl
static struct sysctl_log *sysctl_log;
static int testvar;
MODULE(MODULE_CLASS_MISC, example, NULL);
static int example_modcmd(modcmd_t cmd, void *arg) {
switch (cmd) {
case MODULE_CMD_INIT:
sysctl_createv(&sysctl_log, 0, NULL, NULL,
CTLFLAG_READWRITE, CTLTYPE_INT, “example”,
SYSCTL_DESCR(“Test.”), NULL, 0, &testvar, 0,
CTL_HW, CTL_CREATE, CTL_EOL);
break;
case MODULE_CMD_FINI:
sysctl_teardown(&sysctl_log);
break;
}
return 0;
}
hw.example is created by
load module.
it can read/write by sysctl
hw.example is destroyed
by unload module.
19. Problems
No parameter with loading module
static resource allocation at bootstrap case
workaround: after bootstrap, use sysctl?
kenv is available at FreeBSD (/boot/loader.conf)
No API for create device node (/dev/<name>)
major is fixed at loading module
mknod in module? (not recommended by module(7))
mknod -l?
20. Not only API, but commands are different.
Appendix: compares with other OSs
OS NetBSD FreeBSD Linux
suffix .kmod .ko .ko
load modload kldload insmod
unload modunload kldunload rmmod
status modstat kldstat lsmod
placement /stand/... /boot/kernel/ /lib/modules/...