简体   繁体   English

在Linux内核模块中设置动态变量

[英]Setting Dynamic Variable in Linux kernel module

i have made a new Linux TCP cong algorithm implementation and have some static variables in this code now i want them to be dynamic by using some configuration method. 我已经制作了一个新的Linux TCP cong算法实现,并且在这段代码中有一些静态变量,我希望它们通过使用一些配置方法是动态的。

as per my understanding in the kernel level programming, we cant load some text file and put the values in there and read it through program.. 根据我在内核级编程中的理解,我们无法加载一些文本文件并将值放在那里并通过程序读取它。

but i need something else to make those values dynamic so the user can change them without compiling the kernel code every time user changes the values. 但我需要其他东西来使这些值动态化,以便用户可以在不用每次用户更改值时编译内核代码的情况下更改它们。

I have heard of proc entries which can help us but i am not sure if that is the correct way to do that. 我听说过可以帮助我们的proc条目,但我不确定这是否是正确的方法。 not sure if ioctl() can help as well.? 不确定ioctl()是否也可以提供帮助。

Can some one give some idea on how to make those variable dynamic so we an change them on fly and the values get set.. 有人可以给出一些关于如何使这些变量变为动态的想法,以便我们在飞行中更改它们并设置值。

I suggest to use module parameters. 我建议使用模块参数。

Include the #include <linux/moduleparam.h> in kernel module. 在内核模块中包含#include <linux/moduleparam.h>

use module_param() variables and module_param_array() for passing array to kernel modules. 使用module_param()变量和module_param_array()将数组传递给内核模块。

Refer for how to pass values to module Passing an array as command line argument for linux kernel module 请参阅如何将值传递给模块将数组作为linux内核模块的命令行参数传递

Here is a sample program for module_param() 这是module_param()的示例程序

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/moduleparam.h>

#include "MyHeader.h"

int a = 0, b = 0, op = 0;


module_param(a, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
module_param(b, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
module_param(op, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);

int __init init_module(void)
{
  printk("\n Second Module Loaded...... \n");

  if((a == 0) && (b == 0) && (op == 0))
  {     
         printk("\n Supply Module Parameters...... \n");
         printk("\n insmod <modulename>.ko op=<1-4> a=<int data> b=<int data> \n");
         return 0;
  }

 switch(op)
  {
    case 1:
         printk("\n Result of Addition:%d \n", add(a,b));
         break;
    case 2:
         printk("\n Result of Subtraction:%d \n", sub(a,b));
         break;
    case 3:
         printk("\n Result of Multiplication:%d \n", mul(a,b));
         break;
    case 4:
         printk("\n Result of Division:%d \n", div(a,b));
         break;
    default:
         printk("\n Unknown Operation... \n");
  }

  return 0;
}

void cleanup_module()
{
  printk("\n Second Module UN--Loaded...... \n");
}

Another sample for passing array to a kernel module. 另一个将数组传递给内核模块的示例。

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/moduleparam.h>

int a[5];
int count;
module_param_array(a, int, &count, 0);

int init_module(void)
{

    int i = 0;
    printk("\n Welcome to sample kernel module \n");

    for(i = 0; i < count; i++)
    {
       printk("\na[%d] = %d", i, a[i]);
    }

    return 0;

}

void cleanup_module()
{
     printk("\n Exit success \n");
}

Apart from module param, other alternatives also suggested in the following link. 除了模块参数之外,还在以下链接中提出了其他替代方案。 http://kernelnewbies.org/FAQ/WhyWritingFilesFromKernelIsBad http://kernelnewbies.org/FAQ/WhyWritingFilesFromKernelIsBad

Many kernel modules put files in the /proc filesystem, where indeed data is presented as files, and can be modified by normal user-space file operations. 许多内核模块将文件放在/proc文件系统中,其中数据实际上是作为文件呈现的,并且可以通过普通的用户空间文件操作进行修改。

There is also the sysctl interface. 还有sysctl接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM