modern net bsd kernel module
TRANSCRIPT
![Page 1: Modern net bsd kernel module](https://reader036.vdocuments.site/reader036/viewer/2022081805/554fb27cb4c905ad218b5311/html5/thumbnails/1.jpg)
Modern NetBSDKernel Module
Mar 14, 2014Masaru OKI (@masaru0714)
![Page 2: Modern net bsd kernel module](https://reader036.vdocuments.site/reader036/viewer/2022081805/554fb27cb4c905ad218b5311/html5/thumbnails/2.jpg)
Contents
Self introductionIntroduction of kernel moduleUsing kernel moduleCreating kernel moduleProblems
![Page 3: Modern net bsd kernel module](https://reader036.vdocuments.site/reader036/viewer/2022081805/554fb27cb4c905ad218b5311/html5/thumbnails/3.jpg)
Self introduction
Name 沖 勝 (Masaru OKI) [email protected]
History1993 Port NetBSD to Sharp X680302001- IIJ SEIL team2013- Stratosphere SDN related jobLast week touch kernel module :-)
![Page 4: Modern net bsd kernel module](https://reader036.vdocuments.site/reader036/viewer/2022081805/554fb27cb4c905ad218b5311/html5/thumbnails/4.jpg)
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.
![Page 5: Modern net bsd kernel module](https://reader036.vdocuments.site/reader036/viewer/2022081805/554fb27cb4c905ad218b5311/html5/thumbnails/5.jpg)
modern?
historical: LKM (Loadable Kernel Module)reesrved entry in the table.fixed limit number of entries.no loading at bootstrap.
modern: options MODULAR frameworkno reserved entryloading at bootstrap (see boot.cfg(5))
![Page 6: Modern net bsd kernel module](https://reader036.vdocuments.site/reader036/viewer/2022081805/554fb27cb4c905ad218b5311/html5/thumbnails/6.jpg)
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’.
![Page 7: Modern net bsd kernel module](https://reader036.vdocuments.site/reader036/viewer/2022081805/554fb27cb4c905ad218b5311/html5/thumbnails/7.jpg)
Show status
modstat(8)NAME CLASS SOURCE REFS SIZE REQUIRESaccf_dataready misc builtin 0 - -accf_httpready misc builtin 0 - -acpiacad driver builtin 0 - -acpibat driver builtin 0 - -acpibut driver builtin 0 - -acpicpu driver builtin 0 - -acpidalb driver builtin 0 - -acpifan driver builtin 0 - -
![Page 8: Modern net bsd kernel module](https://reader036.vdocuments.site/reader036/viewer/2022081805/554fb27cb4c905ad218b5311/html5/thumbnails/8.jpg)
Load kernel module
modload <name>loading /stand/…/<name>/<name>.kmod.
modload example
modload <path>loading specified file.
modload ~/src/example/example.kmod
securitylevel <= 0 only.
![Page 9: Modern net bsd kernel module](https://reader036.vdocuments.site/reader036/viewer/2022081805/554fb27cb4c905ad218b5311/html5/thumbnails/9.jpg)
Unload module
modunload <name>unloading module specified name.
modunload example
Unloading module in use?error … fine, no problem.but no error, caused panic.
![Page 10: Modern net bsd kernel module](https://reader036.vdocuments.site/reader036/viewer/2022081805/554fb27cb4c905ad218b5311/html5/thumbnails/10.jpg)
Loading at bootstrap
/boot.cfg (amd64, i386 only)menu=Boot normally:rndseed /var/db/entropy-file;boot netbsdmenu=Boot single user:rndseed /var/db/entropy-file;boot netbsd -smenu=Boot with module foo:load /foo.kmod;boot
# always load example moduleload=/foo/bar/example.kmod
![Page 11: Modern net bsd kernel module](https://reader036.vdocuments.site/reader036/viewer/2022081805/554fb27cb4c905ad218b5311/html5/thumbnails/11.jpg)
Writing kernel module, easy?
Yes, easy!also easy panic reboot...but don’t worry.
kernel module needs two definitionsMODULE(<class>, <name>, <required>);<name>_modcmd()
![Page 12: Modern net bsd kernel module](https://reader036.vdocuments.site/reader036/viewer/2022081805/554fb27cb4c905ad218b5311/html5/thumbnails/12.jpg)
MODULE()
Declare kernel module.MODULE(<class>, <name>, <required>)
class Module class.name name of modulerequired required module string if needed
MODULE(MODULE_CLASS_DRIVER, vnd, ”zlib”);
![Page 13: Modern net bsd kernel module](https://reader036.vdocuments.site/reader036/viewer/2022081805/554fb27cb4c905ad218b5311/html5/thumbnails/13.jpg)
MODULE(): class
MODULE_CLASS_DRIVER device driverMODULE_CLASS_EXEC executable image handlerMODULE_CLASS_MISC moscellaneous moduleMODULE_CLASS_SECMODEL security modelMODULE_CLASS_VFS virtual filesystem
![Page 14: Modern net bsd kernel module](https://reader036.vdocuments.site/reader036/viewer/2022081805/554fb27cb4c905ad218b5311/html5/thumbnails/14.jpg)
<name>_modcmd()
module command API.<name>_modcmd(<cmd>, <data>)value of <cmd>
MODULE_CMD_INIT InitializeMODULE_CMD_FINI Clean-upMODULE_CMD_AUTOUNLOAD Notify (option)MODULE_CMD_STAT Status (not implemented)
![Page 15: Modern net bsd kernel module](https://reader036.vdocuments.site/reader036/viewer/2022081805/554fb27cb4c905ad218b5311/html5/thumbnails/15.jpg)
Writing Makefile
Simple.
KMOD= exampleSRCS= example.c
.include <bsd.kmodule.mk>
![Page 16: Modern net bsd kernel module](https://reader036.vdocuments.site/reader036/viewer/2022081805/554fb27cb4c905ad218b5311/html5/thumbnails/16.jpg)
Other item requires for compile
Kernel source needed.● extract to /usr/src/sys● other place, specify e.g. S=/foo/src
![Page 17: Modern net bsd kernel module](https://reader036.vdocuments.site/reader036/viewer/2022081805/554fb27cb4c905ad218b5311/html5/thumbnails/17.jpg)
Example (do nothing)
test by modload, modstat and modunload.
#include <sys/module.h>
MODULE(MODULE_CLASS_MISC, example, NULL);
static intexample_modcmd(modcmd_t cmd, void *arg){
return 0;}
![Page 18: Modern net bsd kernel module](https://reader036.vdocuments.site/reader036/viewer/2022081805/554fb27cb4c905ad218b5311/html5/thumbnails/18.jpg)
Add sysctlstatic 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.
![Page 19: Modern net bsd kernel module](https://reader036.vdocuments.site/reader036/viewer/2022081805/554fb27cb4c905ad218b5311/html5/thumbnails/19.jpg)
Problems
No parameter with loading modulestatic resource allocation at bootstrap caseworkaround: 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 modulemknod in module? (not recommended by module(7))mknod -l?
![Page 20: Modern net bsd kernel module](https://reader036.vdocuments.site/reader036/viewer/2022081805/554fb27cb4c905ad218b5311/html5/thumbnails/20.jpg)
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/...