Modern net bsd kernel module


Published on

1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Modern net bsd kernel module

  1. 1. Modern NetBSD Kernel Module Mar 14, 2014 Masaru OKI (@masaru0714)
  2. 2. Contents Self introduction Introduction of kernel module Using kernel module Creating kernel module Problems
  3. 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. 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. 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. 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’.
  7. 7. Show status modstat(8) NAME CLASS SOURCE REFS SIZE REQUIRES accf_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 - -
  8. 8. 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.
  9. 9. Unload module modunload <name> unloading module specified name. modunload example Unloading module in use? error … fine, no problem. but no error, caused panic.
  10. 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. 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. 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. 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
  14. 14. <name>_modcmd() module command API. <name>_modcmd(<cmd>, <data>) value of <cmd> MODULE_CMD_INIT Initialize MODULE_CMD_FINI Clean-up MODULE_CMD_AUTOUNLOAD Notify (option) MODULE_CMD_STAT Status (not implemented)
  15. 15. Writing Makefile Simple. KMOD= example SRCS= example.c .include <>
  16. 16. Other item requires for compile Kernel source needed. ● extract to /usr/src/sys ● other place, specify e.g. S=/foo/src
  17. 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. 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. 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. 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/...