Or, having an out-of-tree (i.e. not in /usr/src/sys) custom FreeBSD kernel module, how one can specify a kernel configuration, like
Here is a very small kernel module that can be compiled, but cannot be loaded on a stock FreeBSD 12.
Makefile:
showifn.c:
Module should compile cleanly on FreeBSD 12, but cannot be loaded (reason below):
The issue with this particular example is, that since FreeBSD 12
GENERIC
, the module shall be built against?Here is a very small kernel module that can be compiled, but cannot be loaded on a stock FreeBSD 12.
Makefile:
Makefile:
KMOD= showifn
SRCS= showifn.c
.include <bsd.kmod.mk>
showifn.c:
C:
#include <sys/param.h>
#include <sys/module.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
#include <net/if_var.h>
static void
show_ifn(void)
{
struct ifnet *ifp;
IFNET_RLOCK();
CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
printf(" %s via %s\n", ifp->if_xname, ifp->if_dname);
}
IFNET_RUNLOCK();
}
static int
kmod_main(struct module *module, int event, void *arg)
{
int res = 0;
switch (event) {
case MOD_LOAD:
printf("showifn loading\n");
show_ifn();
break;
case MOD_UNLOAD:
printf("showifn unloading\n");
break;
default:
res = EOPNOTSUPP;
break;
}
return res;
}
static moduledata_t showifn_conf = {
"showifn",
kmod_main,
NULL
};
DECLARE_MODULE(showifn, showifn_conf, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
Module should compile cleanly on FreeBSD 12, but cannot be loaded (reason below):
Code:
# make load
/sbin/kldload -v /root/proj/custkmod/showifn.ko
kldload: an error occurred while loading module /root/proj/custkmod/showifn.ko. Please check dmesg(8) for more details.
*** Error code 1
Stop.
make: stopped in /root/proj/custkmod
# dmesg | tail -2
link_elf_obj: symbol ifnet undefined
linker_load_file: /root/proj/custkmod/showifn.ko - unsupported file type
The issue with this particular example is, that since FreeBSD 12
VIMAGE
is now enabled in GENERIC
, by default. When VIMAGE
is enabled a lots of pre-processor magic happens, behind the scenes, and the names of some symbols, related to networking, are changed (probably much more than just names). BTW the module can be loaded on a system with custom-built kernel, where VIMAGE
is disabled.- How come configuration for
GENERIC
kernel is not taken into account when building this module? Module can be built, but not loaded. - How to specify a custom kernel configuration (or even
GENERIC
one) when compiling out-of-tree kernel modules?