modern net bsd kernel module

20
Modern NetBSD Kernel Module Mar 14, 2014 Masaru OKI (@masaru0714)

Upload: masaru-oki

Post on 11-May-2015

1.246 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Modern net bsd kernel module

Modern NetBSDKernel Module

Mar 14, 2014Masaru OKI (@masaru0714)

Page 2: Modern net bsd kernel module

Contents

Self introductionIntroduction of kernel moduleUsing kernel moduleCreating kernel moduleProblems

Page 3: Modern net bsd kernel module

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

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

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

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

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

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

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

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

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

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

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

<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

Writing Makefile

Simple.

KMOD= exampleSRCS= example.c

.include <bsd.kmodule.mk>

Page 16: Modern net bsd kernel module

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

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

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

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

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/...