Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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. not sure if ioctl() can help as well.?

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

like image 586
NitinG Avatar asked Oct 21 '22 23:10

NitinG


1 Answers

I suggest to use module parameters.

Include the #include <linux/moduleparam.h> in kernel module.

use module_param() variables and module_param_array() for passing array to kernel modules.

Refer for how to pass values to module Passing an array as command line argument for linux kernel module

Here is a sample program for 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

like image 177
Jeyaram Avatar answered Nov 03 '22 22:11

Jeyaram