net_device get_stats function, how to use? - linux

this is how the function looks like:
struct net_device_stats* (*get_stats)(struct net_device *dev);
I simply need to call this function in my code and get it's results in a net_device_stats struct I declared earlier. Can anyone give a simple implementation code for this?

This is not a function. Its the declaration of a function pointer get_stats which points to a function which receives a pointer to the structure net_device and returns a pointer to a structure of type net_device_stats
Here is one use case
struct net_device *dev;
struct net_device_stats *device;
device = get_stats(dev);
EDIT From your comments, I see you are using an older version of the kernel. IN later kernels , structure net_device still resides in linux/netdevice.h but there's no get_stats function pointer. Its changed into ndo_get_stats and it is now under another structure net_device_ops
So start using these new function pointers.

Related

Writing a Sysfs module

I'm attempting to learn how to write a sysfs module, and am confused at the basic initialization. In this document looking at the kobject.h file, there are several different functions related to creating a sysfs entry.
From the looks of it, the function "kobject_init_and_add" seems like the right thing to use, which takes the following:
90 int kobject_init_and_add(struct kobject *kobj,
91 struct kobj_type *ktype, struct kobject *parent,
92 const char *fmt, ...);
struct kobject and struct kobj_type are straightforward enough, but I don't understand what the *parent kobject and *fmt cstring are supposed to be.
Further, after initializing these objects, I would need to remove them at the exit_module function, but there are two options that seem possible: kobject_del and kobject_puts. What are the differences between these?
Part of my confusion comes from the fact that while googling for the answer, I see tutorials which says to use functions like kobject_register instead, but that function doesn't actually exist.
Yes there are lot of example on mainline kernel which you can refers for your implementatin. For Your doubts I am adding the some example code"
Module Probe/init function
static struct kobject *module_kobject;
module_kobject=kobject_create_and_add("module_status",NULL);
sysfs_create_group(module_kobject,&module_attr);
Module Remove/exit function
sysfs_remove_group(module_kobject,&module_attr);
kobject_put(module_kobject);
If you want to expose more than one attribute on the user space; than you need to define the group as well
static struct attribute_group module_attr={
.attrs = module_attribute,
};
There is some more implementation and function you may need like:
static ssize_t module_show_status(struct kobject *kobj,struct kobj_attribute *attr,char *buf);
static ssize_t module_store__status(struct kobject *kobj,struct kobj_attribute *attr,const char *buf,size_t len);
I think you can start your sysfs module implementation based on the above code and Feel free for any help.
There are many kernel modules that create sysfs entries. For example,
http://lxr.free-electrons.com/source/net/bridge/br_sysfs_br.c
This module uses kobject_create_and_add(), which gets as a parameter a kobject instance, created by sysfs_create_group(). I believe that looking into such module, and trying to code step by step, following the patterns in that module, can help. Also look in
http://lxr.free-electrons.com/source/Documentation/kobject.txt
Rami Rosen

dev_err() function definition

I can see that dev_*() family of functions such as dev_err() are given as prototype in include/linux/device.h, but no where I could find its definition. I have visited sites like lxr.free-electrons, but without success. Used tags in the source code of linux kernel, even then failed.
What I am trying to find is how the dev_err(const struct device *dev, const char *fmt, ...) is able to get the device information such as pci bus, etc from just giving const struct device *dev as argument to print in logs.
Description of the device is constructed in function create_syslog_header, defined in drivers/base/core.c. The function just extracts some fields from struct device object, and emits them via snprintf() into the string.
The function dev_err is implemented via define_dev_printk_level macro in the same file (drivers/base/core.c).

How do you link a device to a custom sysfs class?

I'm writing drivers for several pieces of custom hardware. All of the devices are attached via PCIe to a host computer. For convenience I would like to group all of these custom devices together into a sysfs class (which I believe is an acceptable thing to do?). Unfortunately the information in LDD3 is way out of date and I'm having trouble finding current documentation that discusses what I'm attempting to do.
Creating my custom class is easy enough:
struct class MY_CLASS = class_create(THIS_MODULE, "myclass")
And inside of my probe calls I've got access to the struct dev:
static int probe(struct pci_dev *pcidev, const struct pci_device_id *id)
{
...
struct dev *my_dev = &pcidev->dev;
...
}
My question is this: now that I've got the class and the dev, how do I create a link between the two?
The device_create() basically does what I want, but since I've already got a struct dev my understanding is that I shouldn't call device_create (i.e. create a new device) again.
I've done a little more tracing and found that device_add() which is called by device_create(), calls device_add_class_symlinks() (not exported unfortunately) which does something like this:
...
sysfs_create_link(&dev->class->p->subsys.kobj,&dev->kobj, dev_name(dev));
...
I tried something like this directly in my drivers to create the links I want but I can't get it to compile because struct subsys_private (the "p" member in the class struct) is not exposed anywhere?
Any help is greatly appreciated!
Are your drivers sitting on the specific bus? If no, what purpose of the specific class?
Anyway, for starter
struct class devclass = {…}
probe()
{
struct device *dev = …
dev->class = &devclass;
}
init()
{
class_register(&devclass);
}
I've encountered the same issue.
if i called device_register with my class pointer assigned to the device class member. it will create a subsystem in my device directory, which is what device_add_class_symlinks do. but in my device directory there is already subsystem directory which is the link to the bus my device is attached to.
don't know if you got a method

get pointer for existing device class (struct class) in Linux kernel module

get pointer for existing device class (struct class) in Linux kernel module
Hi all!
I am trying to register a device in an existing device class, but I am having trouble getting the pointer to an existing class. Kernel version is Linux 3.3.6.
I can create a class in a module, get the pointer to it and then use it to register the device with:
*cl = class_create(THIS_MODULE, className);
dev_ret = device_create(*cl, NULL, *dev, NULL, driverName);
However I need to register another device in the same class with another module, but I couldn't find a way to get the pointer to an existing class. And I can not create the class again in the other module, because since class already exists class_create returns NULL and not the pointer to the class required by device_create.
I found in:
http://lwn.net/Articles/102500/
A function that returns a pointer to a class by its name:
struct class * class_find(char * name)
However when I try to compile the function compiler says it does not exist.
I thought this function was exported by the kernel (my module have license GPL) but it appears it is not.
Maybe I need to include some header?
I tried to rewrite this function since, its code is list in the above link. But when I try to iterate over class_subsys with:
list_for_each_entry(this_class, &class_subsys.kset.list, subsys.kset.kobj.entry)
now symbol class_subsys is not found. Again I thought it is exported to the kernel.
I am not sure what is missing. Some header?
Am I doing it the wrong way?
There is another function to do it?
I suppose if I could traverse sysfs from start I could get a pointer to an existing class.
But I also did not find how to start traversing sysfs.
All functions I have seen requires a pointer to kobject or kset to start traversing. But I have no pointer even to the root of sysfs or kernel objects, so I can not start traversing the tree to get a class pointer.
Can anyone point me in the right direction please?
I think input core is done this way, here is the snippet
Take a look at: https://github.com/torvalds/linux/blob/master/drivers/input/input.c#L1720
Best regards!

Accessing the proc_dir_entry from proc_fops.open?

I writing a linux kernel module that does some work with /proc... I'm trying to use the new seq methods for returning the data for /proc... Anyhow, after I call proc_create_data() I have a proc_dir_entry (whose ->data member is pointing at my supplied context)... Anyhow, the file_operations structure is also passed and I really need to know how to access either the proc_dir_entry or the proc_dir_entry->data from the open() file operation...
The answer was to use the PDE macro to convert the inode* into a pointer to the proc_dir_entry, which of course had a "data" member pointing at what I needed.
struct proc_dir_entry* pde = PDE( inode );

Resources