0

when the interrupt occurs in the kernel and If I am reading a timestamp in the kernel. I am reading the timestamp from kernel to the user via procfs. where that interrupt time value will be stored ?? how should the user read that value from the user space ??

ssize_t dev_read(struct file *filp,const char *buf,size_t count,loff_t *offset)
{

  if ( count < sizeof(InterruptTime) ) {
    // Not enough space provided.
    return 0; // Or some error code maybe.
  }

  if (copy_to_user(buf,&InterruptTime,sizeof(InterruptTime)) {
    return -EFAULT;
  } else {
    return sizeof(InterruptTime); // Number of bytes we copied.

  }

}

this is the code I modified in /linuxversion/net/core/dev.c

int netif_rx(struct sk_buff *skb) 
{
     skb->tstamp = ktime_get_real();   //this will give a timestamp and it will be stored in //skb buffer
     //I am calculating a timestamp here. because whenever kernel receive the data then the kernel is 
     //interrupted and start executing the newly arrived task but I have to read the time when the 
    //interrupt  occurs and get the value of it.
} 

but how to copy this value stored in skb->tstamp to procfs driver ?? finally I want to send this timestamp value to the user ??

Uri Agassi
  • 36,848
  • 14
  • 76
  • 93
user3458454
  • 291
  • 1
  • 4
  • 20
  • What are you trying to read?. If you are looking for jiffies then you can use "get_jiffies_64" – Sasi V Apr 20 '14 at 15:36
  • extern double interrupt time; int netif_rx(struct sk_buff *skb) { skb->timestamp = ktime_get_real(); interrupt time = skb -> timestamp; } // I am reading this interrupt time in dev.c in kernel source code. later I am reading this interrupt time via procfs. after reading the data from kernel to user space via procfs. where this interrupt time will be stored ?? – user3458454 Apr 20 '14 at 15:49
  • if (copy_to_user(buf,&InterruptTime,sizeof(InterruptTime)) // if I use this code then the it is copying from kernel to user. but my question is : where the value will be stored in user ?? – user3458454 Apr 20 '14 at 15:52
  • it is not stored in user space You can just read the value. cat /proc/ and store it. – Sasi V Apr 20 '14 at 16:15
  • could you explain more about that ?? I din get you . ur proc entry is nothing but what ?? how to store there ?? – user3458454 Apr 20 '14 at 16:27
  • I modified the code and described my problem now. – user3458454 Apr 21 '14 at 06:17
  • possible duplicate of [which document to follow for creating a procfs driver?](http://stackoverflow.com/questions/23192227/which-document-to-follow-for-creating-a-procfs-driver) – Abhijeet Kasurde Apr 23 '14 at 15:13

2 Answers2

1
There is sample proc code and its output


Sample proc code
===============

[root@localhost p]# cat test.c 

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/proc_fs.h>
#include <linux/jiffies.h>
#include <linux/seq_file.h>

//extern uint64_t interrupt_time;

static struct proc_dir_entry *test_dir;

static int my_proc_show(struct seq_file *m, void *v)
{
    seq_printf(m, "%lu\n", jiffies);
    //seq_printf(m, "%lu", interrupt_time);
    return 0;
}

static int my_proc_open(struct inode *inode, struct file *file)
{
    return single_open(file, my_proc_show, NULL);
}

static const struct file_operations tst_fops = {
    .open       = my_proc_open,
    .read       = seq_read,
    .llseek     = seq_lseek,
    .release    = single_release,
};

static int __init test_init(void)
{
    test_dir = proc_mkdir("myproc", NULL);

    if (test_dir)
            proc_create("jiffies", 0, test_dir, &tst_fops);

    return 0;
}
static void __exit test_exit(void)
{
    remove_proc_entry ("jiffies", test_dir);
    proc_remove (test_dir);
}
module_init(test_init);
module_exit(test_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Test");

    Output
   ======
    [root@localhost p]# cat /proc/myproc/jiffies 
    4325737301
Sasi V
  • 1,074
  • 9
  • 15
  • thank you very much. If I am calculating the interrupt time /src/linuxversion/net/core/dev.c. how to copy that to the proc file system ?? should I add dev_read api in the above program ?? should i create a makefile for the above program ?? I modified my code above. – user3458454 Apr 21 '14 at 06:02
  • after reading the timestamp via procfs and send it to user space then how to read the value on the user side ?? – user3458454 Apr 21 '14 at 06:11
  • should I use dev_read api to send it to the user ?? I am waiting for your answer. why are you using jiffies ?? – user3458454 Apr 21 '14 at 07:04
  • I'm using jiffies because my kernel does not have interrupt_time. You can make this proc as part of net/core/net-procfs.c or load it as a module. – Sasi V Apr 21 '14 at 11:38
  • On user-side Open proc file and read similar to any file read. fopen ("/proc/myproc/jiffies", r") – Sasi V Apr 21 '14 at 11:39
  • dev_read is not required you can re-use the code I had posted. First try to load it as a module if you are satisfied then integrate it as part of your kernel. – Sasi V Apr 21 '14 at 11:40
  • I was waititng for your reply. thank you very much but my last question : I modified my code and getting the timestamp in that function definition (dev.c linux source code) and storing in a skb buffer. how to copy that value to procfs program ?? The interupt time is nothing but timestamp only. I want to get the timestamp in the dev.c program as shown above and later copied it to procfs program. how to copy it to procfs program ?? – user3458454 Apr 21 '14 at 11:44
  • Once kernel process the skbuff it frees the memory or release it pool so you can not access all packets timestamp."global variable for last packet timestamp or "array of last 20 or 30 to store last 30 packets timestamp" to store timestamp. – Sasi V Apr 21 '14 at 11:51
  • then how to get the timestamp in that function and store it in a procfs ?? suggest something to solve this problem ?? – user3458454 Apr 21 '14 at 11:52
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/51099/discussion-between-sasi-and-user3458454) – Sasi V Apr 21 '14 at 11:53
  • Just to avoid "Please avoid extended discussions in comments" lets take this over chat – Sasi V Apr 21 '14 at 11:54
0

I guess you have added this line interrupt_time = skb -> timestamp. If yes, then

  1. Open proc file in kernel space (check fs/proc/ and add an entry for timestamp)
  2. Register your open/read calls.
  3. Whenever user tries to read a file Linux Kernel calls registered read call, in your case it is dev_read.

Check this link for how proc fs is used

Abhijeet Kasurde
  • 3,937
  • 1
  • 24
  • 33
Sasi V
  • 1,074
  • 9
  • 15
  • thank you very much !! I read that document but I did not understand is how to add an entry for timestamp ?? how to Open proc file in kernel space ?? could you please tell where is it in the document ?? – user3458454 Apr 20 '14 at 20:41
  • could you please tell me the place where the above steps are there in the document ?? – user3458454 Apr 20 '14 at 20:52