5

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

NitinG
  • 893
  • 3
  • 21
  • 38

2 Answers2

2

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

Community
  • 1
  • 1
Jeyaram
  • 9,158
  • 7
  • 41
  • 63
  • yeah thanks, i agree the moduleparam can be used but it also needs to read the values from somewhere.. either we need to have the var defined in the env vars or make some proc entries.. ? – NitinG Dec 05 '12 at 06:45
1

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.

There is also the sysctl interface.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • yeah proc entries can be done but if user changes the values in the proc entries, the values will not get updated until we restart the module. So, do we have some way where we wont need to restart the module and values will get updated in the ongoing process of the module? – NitinG Dec 05 '12 at 06:44
  • 1
    @NitinGoyal You have a special function in your module that is called when a proc-file is read, and another function that is called when your proc-file is written to. So you definitely get changes as soon as they are written. See e.g. [this simple example](http://linux.die.net/lkmpg/x769.html) on how to use the procfs. – Some programmer dude Dec 05 '12 at 06:53