本文主要介绍内核开发中常用的模块传参手段,通过模块参数传递可以通过用户态来获取内核的一些信息,也可以通过用户态写入一些值来控制内核相关行为。一般内核开发者很喜欢使用模块传参来调试内核功能,如damon模块(数据访问监控器)。
主要由以下部分组成:
/**
* module_param - typesafe helper for a module/cmdline parameter
* @name: the variable to alter, and exposed parameter name.
* @type: the type of the parameter
* @perm: visibility in sysfs.
*
* @name becomes the module parameter, or (prefixed by KBUILD_MODNAME and a
* ".") the kernel commandline parameter. Note that - is changed to _, so
* the user can use "foo-bar=1" even for variable "foo_bar".
*
* @perm is 0 if the variable is not to appear in sysfs, or 0444
* for world-readable, 0644 for root-writable, etc. Note that if it
* is writable, you may need to use kernel_param_lock() around
* accesses (esp. charp, which can be kfreed when it changes).
*
* The @type is simply pasted to refer to a param_ops_##type and a
* param_check_##type: for convenience many standard types are provided but
* you can create your own by defining those variables.
*
* Standard types are:
* byte, hexint, short, ushort, int, uint, long, ulong
* charp: a character pointer
* bool: a bool, values 0/1, y/n, Y/N.
* invbool: the above, only sense-reversed (N = true).
*/
#define module_param(name, type, perm) \
module_param_named(name, name, type, perm)
是最常规的传参方式,支持对普通数据类型的参数的读写。
例如:
static unsigned int param_uint;
module_param(param_uint, uint, 0600);
MODULE_PARM_DESC(param_uint, "This is a uint parameter!");
通过以下方式可以设置这个参数:
1)加载模块时
insmod module_param_test.ko param_uint=100
2)cmdline传递
cmdline中加入 module_param_test.param_uint=100 字段
3)通过写sysfs节点
echo 100 > /sys/module/module_param_test/parameters/param_uint
通过sysfs查看模块参数:
cat /sys/module/module_param_test/parameters/param_uint
100
/**
* module_param_array - a parameter which is an array of some type
* @name: the name of the array variable
* @type: the type, as per module_param()
* @nump: optional pointer filled in with the number written
* @perm: visibility in sysfs
*
* Input and output are as comma-separated values. Commas inside values
* don't work properly (eg. an array of charp).
*
* ARRAY_SIZE(@name) is used to determine the number of elements in the
* array, so the definition must be visible.
*/
#define module_param_array(name, type, nump, perm) \
module_param_array_named(name, name, type, nump, perm)
即是数组类型支持。
例如:
/* array: echo "1,2,3,4,4" > param_array */
static int param_array[5];
static int array_num;
//module_param_array(param_char_array, int, NULL, 0600);
module_param_array(param_array, int, &array_num, 0600);
MODULE_PARM_DESC(param_bool, "This is a array parameter!");
通过以下方式可以设置这个参数:
1)加载模块时传递
insmod module_param_test.ko param_array=1,2,3,4,4
2)通过cmdline传递
cmdline中加入 module_param_test.param_array=1,2,3,4,4 字段
3)通过写sysfs节点
echo 1,2,3,4,4 > /sys/module/module_param_test/parameters/param_array
通过sysfs查看模块参数:
cat /sys/module/module_param_test/parameters/param_array
1,2,3,4,4
/**
* module_param_cb - general callback for a module/cmdline parameter
* @name: a valid C identifier which is the parameter name.
* @ops: the set & get operations for this parameter.
* @arg: args for @ops
* @perm: visibility in sysfs.
*
* The ops can have NULL set or get functions.
*/
#define module_param_cb(name, ops, arg, perm) \
__module_param_call(MODULE_PARAM_PREFIX, name, ops, arg, perm, -1, 0
即是参数的回调函数支持。
例如:
static int param_int_cb;
int param_int_cb_store(const char *val, const struct kernel_param *kp)
{
int value;
int err;
err = kstrtoint(val, 0, &value);
if (err)
return err;
if (value > 0)
pr_info("value:%d\n", value);
//return param_set_int(val, kp);
return param_set_uint_minmax(val, kp, 0, 1000);
}
int param_int_cb_show(char *buffer, const struct kernel_param *kp)
{
int value = *((int *)kp->arg);
if (value > 0)
return sprintf(buffer, "value:%d > 0\n", value);
else
return sprintf(buffer, "value:%d <= 0\n", value);
}
static const struct kernel_param_ops param_int_cb_ops = {
.set = param_int_cb_store,
//.get = param_get_int, /* default */
.get = param_int_cb_show,
};
module_param_cb(param_int_cb, ¶m_int_cb_ops, ¶m_int_cb, 0600);
MODULE_PARM_DESC(param_int_cb, "This is param_int_cb\n");
读写参数方式和上面介绍的类似,这里需要注意的是:当读参数param_int_cb时就会回调param_int_cb_show函数,写参数param_int_cb时就会回调param_int_cb_store,使得我们能有机会拦截参数来做一些操作。
/**
* module_param_named - typesafe helper for a renamed module/cmdline parameter
* @name: a valid C identifier which is the parameter name.
* @value: the actual lvalue to alter.
* @type: the type of the parameter
* @perm: visibility in sysfs.
*
* Usually it's a good idea to have variable names and user-exposed names the
* same, but that's harder if the variable must be non-static or is inside a
* structure. This allows exposure under a different name.
*/
#define module_param_named(name, value, type, perm) \
param_check_##type(name, &(value)); \
module_param_cb(name, ¶m_ops_##type, &value, perm); \
__MODULE_PARM_TYPE(name, #type)
即是参数的重命名支持。
例如:
/* bool eg: echo 0/1/n/y/N/Y > param_bool1_named */
static bool param_bool1;
module_param_named(param_bool1_named, param_bool1, bool, 0600);
MODULE_PARM_DESC(param_bool1_named, "This is a bool parameter!");
读写参数方式和上面介绍的类似,这里需要注意的是:模块中定义为param_bool1这个变量名,但是sysfs中使用的是这个param_bool1_named别名。
注:都在include/linux/moduleparam.h文件中定义
内核支持的参数数据类型在定义module_param的时候有说明:
include/linux/moduleparam.h
Standard types are:
byte, hexint, short, ushort, int, uint, long, ulong
charp: a character pointer
bool: a bool, values 0/1, y/n, Y/N.
invbool: the above, only sense-reversed (N = true).
注:这些api的时候内核源码中有大量的例子,直接搜索即可知道内核开发者是如何使用。我们在实际内核开发中,如何在海量的源码中获得我们所需要的东西并在我们的优化代码中得以使用也是也是内核开发者需要具备的素养。
常见权限如下:
对于内核态,直接读取定义的模块参数即可。
而对于用户态,是通过sysfs来读取它的。
读取格式:cat /sys/module/xxx/parameters/param
xxx表示想读取的模块 param表示具体的参数
例如:示例中的module_param_test模块,读模块参数如下:cat /sys/module/module_param_test/parameters/param_uint 100
对于内核态,直接读取定义的模块参数即可。
而对于用户态,我们有三种方式来写模块参数。
一般用于buildin到内核的模块
传参的方式为:module.param=val
例如:module_param_test.param_charp=hello module_param_test.param_array=1,2,3,4,5
一般用于编译成模块的场景。
传参的方式为:insmod xxx.ko param=val
例如:insmod module_param_test.ko param_uint=100
传参的方式为:echo xxx >/sys/module/xxx/parameters/param
例如:echo 100 > /sys/module/module_param_test/parameters/param_uint
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
/********** case 1: base type **********/
/* bool eg: echo 0/1/n/y/N/Y > param_bool*/
static bool param_bool;
//module_param(param_bool, bool, 0);//no permission, no file in sysfs
//module_param(param_bool, bool, 0666);//-rwxrwxrwx -> forbit
//module_param(param_bool, bool, 0644);//-rw-rw-rw-
module_param(param_bool, bool, 0600);
MODULE_PARM_DESC(param_bool, "This is a bool parameter!");
/* bool eg: echo 0/1/n/y/N/Y > param_bool1_named */
static bool param_bool1;
module_param_named(param_bool1_named, param_bool1, bool, 0600);
MODULE_PARM_DESC(param_bool1_named, "This is a bool parameter!");
/* byte eg: echo 0-255 > param_char */
static unsigned char param_char;
module_param(param_char, byte, 0600);
MODULE_PARM_DESC(param_char, "This is a char parameter!");
/* short eg: echo -100 > param_short */
static short param_short;
module_param(param_short, short, 0600);
MODULE_PARM_DESC(param_short, "This is a short parameter!");
/* unsigned short eg: echo 100 > param_short */
static unsigned short param_ushort;
module_param(param_ushort, ushort, 0600);
MODULE_PARM_DESC(param_ushort, "This is a ushort parameter!");
/* int eg: echo -100 > param_int */
static int param_int;
module_param(param_int, int, 0600);
MODULE_PARM_DESC(param_int, "This is a int parameter!");
/* unsigned int eg: echo 100 > param_unint */
static unsigned int param_uint;
module_param(param_uint, uint, 0600);
MODULE_PARM_DESC(param_uint, "This is a uint parameter!");
/* long eg: echo -100 > param_long*/
static long param_long;
module_param(param_long, long, 0600);
MODULE_PARM_DESC(param_long, "This is a long parameter!");
/* unsigned long eg: echo 100 > param_ulong */
static unsigned long param_ulong;
module_param(param_ulong, ulong, 0600);
MODULE_PARM_DESC(param_ulong, "This is a ulong parameter!");
/* unsigned long long eg: echo 100 > param_ullong */
static unsigned long long param_ullong;
module_param(param_ullong, ullong, 0600);
MODULE_PARM_DESC(param_ullong, "This is a unsigned long long parameter!");
/* character pointer : eg: echo hello > param_charp */
static char *param_charp;
module_param(param_charp, charp, 0600);
MODULE_PARM_DESC(param_bool, "This is a charp parameter!");
/********** case 2: array **********/
/* array: echo "1,2,3,4,4" > param_array */
static int param_array[5];
static int array_num;
//module_param_array(param_char_array, int, NULL, 0600);
module_param_array(param_array, int, &array_num, 0600);
MODULE_PARM_DESC(param_bool, "This is a array parameter!");
/********** case 3: use call back **********/
static int param_int_cb;
int param_int_cb_store(const char *val, const struct kernel_param *kp)
{
int value;
int err; //把字符串转换为int类型
err = kstrtoint(val, 0, &value);
if (err)
return err;
if (value > 0)
pr_info("value:%d\n", value);
//将用户态传过来的参数值设置到模块参数中,由于这里是基础的int类型,所以可以直接调用param_set_int api
//param_set_uint_minmax 这个api会在设置时考虑最小和最大值
//return param_set_int(val, kp);
return param_set_uint_minmax(val, kp, 0, 1000);
}
int param_int_cb_show(char *buffer, const struct kernel_param *kp)
{
int value = *((int *)kp->arg);
//用户态最终通过buffer来获得参数的信息,所以这里通过sprintf 做格式化操作写到buffer中
if (value > 0)
return sprintf(buffer, "value:%d > 0\n", value);
else
return sprintf(buffer, "value:%d <= 0\n", value);
}
static const struct kernel_param_ops param_int_cb_ops = {
.set = param_int_cb_store,
//.get = param_get_int, /* default */
.get = param_int_cb_show,
};
module_param_cb(param_int_cb, ¶m_int_cb_ops, ¶m_int_cb, 0600);
MODULE_PARM_DESC(param_int_cb, "This is param_int_cb\n");
static int __init module_test_init(void)
{
pr_emerg("module_test_init\n");
return 0;
}
static void __exit module_test_exit(void)
{
pr_emerg("module_test_exit\n");
}
module_init(module_test_init);
module_exit(module_test_exit);
MODULE_LICENSE("GPL");