The tip of the iceberg in the Linux Kernel proc file system-questions about the NIC parameters in the/proc/NET/dev File

Source: Internet
Author: User

Recently, when debugging the NIC Driver, I have been wondering how to obtain the NIC parameters in the/proc/NET/dev file.
After the insmod ethernet card driver, cat
You can see that eth0 is missing. At first, I thought it was the driver's code to call a function to write eth0 information to the/proc/NET/dev file.
However, no related functions were found after reading the code repeatedly. Today, I suddenly want to get/proc/interrupt
To analyze, looked at the linux-2.6.28 code, there is the Linux kernel proc file system of the tip of the iceberg-from the/proc/NET/dev file in the NIC parameters of the question.

(1) first analyze/proc/interrupts

Linux-2.6.28/ARCH/ARM/kernel/IRQ. c --> show_interrupt ()

In linux-2.6.28/fs/proc/interrupt. c
Module_init (proc_interrupts_init );
--> Proc_interrupts_init ()
--> Proc_create ("interrupts", 0, null, & proc_interrupts_operations);/* If the parameter is null, it is created in the/proc directory.
File named interrupts. */

A great programmer said: "code is the best document)
Static const struct file_operations proc_interrupts_operations = {
. Open = interrupts_open,

Static int interrupts_open (struct inode * inode, struct file * filp)
Return seq_open (filp, & int_seq_ops );

Static const struct seq_operations int_seq_ops = {
. Show = show_interrupts

Linux-2.6.28/ARCH/ARM/kernel/IRQ. c
Show_interrupts ()
For_each_present_cpu (CPU ){
Sprintf (cpuname, "CPU % d", CPU );
Seq_printf (P, "% 10 s", cpuname );
Seq_printf (P, "% 3d:", I );
For_each_present_cpu (CPU)
Seq_printf (P, "% 10u", kstat_cpu (CPU). irqs [I]);
Seq_printf (P, "% 10 s", irq_desc [I]. Chip-> name? :"-");
Seq_printf (P, "% s", action-> name );
For (Action = action-> next; action; Action = action-> next)
Seq_printf (P, ", % s", action-> name );

Here, if you can think of the global structure array irq_desc [], you will understand it.

The following is a result of running CAT/proc/interrupts.
8: 2 gpio-l eth0
11: 0 SC pxa25x_udc
14: 404 SC ac97
22: 43 SC ffuart
25: 0 SC DMA
26: 19388 SC ost0
67: 0 gpio ucb1400
Err: 0

(2) Let's take a look at/proc/NET/dev.

Look at the code, linux-2.6.28/NET/CORE/dev. C.

Static int _ net_init dev_proc_net_init (struct net * Net)
If (! Proc_net_fops_create (net, "Dev", s_irugo, & dev_seq_fops ))
Goto out;

Static int dev_seq_open (struct inode * inode, struct file * file)
Return seq_open_net (inode, file, & dev_seq_ops,
Sizeof (struct seq_net_private ));

Static const struct file_operations dev_seq_fops = {
. Open = dev_seq_open,

Static int dev_seq_open (struct inode * inode, struct file * file)
Return seq_open_net (inode, file, & dev_seq_ops,
Sizeof (struct seq_net_private ));

Static const struct seq_operations dev_seq_ops = {
. Next = dev_seq_next,
. Show = dev_seq_show,

* Called from the procfs module. This now uses the new arbitrary sized
*/Proc/net interface to create/proc/NET/dev
Static int dev_seq_show (struct seq_file * seq, void * V)
If (V = seq_start_token)
Seq_puts (SEQ, "Inter-| receive"
"| Transmit/N"
"Face | bytes packets errs drop FIFO frame"
"Compressed multicast | bytes packets errs"
"Drop FIFO colls carrier compressed/N ");
Dev_seq_printf_stats (SEQ, V );
Return 0;

Static void dev_seq_printf_stats (struct seq_file * seq, struct net_device * Dev)
Struct net_device_stats * stats = Dev-> get_stats (Dev );

Seq_printf (SEQ, "% 6 s: % 8lu % 7lu % 4lu % 4lu % 4lu % 5lu % 10lu % 9lu"
"% 8lu % 7lu % 4lu % 4lu % 4lu % 5lu % 7lu % 10lu/N ",
Dev-> name, stats-> rx_bytes, stats-> rx_packets,
Stats-> rx_errors,
Stats-> rx_dropped + stats-> rx_missed_errors,
Stats-> rx_polico_errors,
Stats-> rx_length_errors + stats-> rx_over_errors +
Stats-> rx_crc_errors + stats-> rx_frame_errors,
Stats-> rx_compressed, stats-> multicast,
Stats-> tx_bytes, stats-> tx_packets,
Stats-> tx_errors, stats-> tx_dropped,
Stats-> tx_polico_errors, stats-> collisions,
Stats-> tx_carrier_errors +
Stats-> tx_aborted_errors +
Stats-> tx_window_errors +
Stats-> tx_heartbeat_errors,
Stats-> tx_compressed );

Void * dev_seq_next (struct seq_file * seq, void * V, loff_t * POS)
Struct net * Net = seq_file_net (SEQ );
+ + * Pos;
Return v = seq_start_token?
First_net_device (net): next_net_device (struct net_device *) V );

The following is a result of running CAT/proc/NET/dev.
Inter-| receive transmit
Face | bytes packets errs drop FIFO frame compressed multicast | bytes packets errs drop FIFO colls carrier compressed
Lo: 16200 185 0 0 0 0 0 16200 185 0 0 0 0 0 0
Eth0: 13817124 13810 0 0 0 0 0 3416037 13030 0 0 0 0 0 0
"% 6 s: % 8lu % 7lu % 4lu % 4lu % 4lu % 5lu % 10lu % 9lu "" % 8lu % 7lu % 4lu % 4lu % 4lu % 5lu % 7lu % 10lu/N ",
The 17 parameters are completely matched. It should be noted that the NIC Device of Linux is managed by the linked list of Linux, and the function of dev_seq_next is self-evident.

I am speechless and have pasted a bunch of code. But there is at least some code, or else it will not be code-free (hahaha, how innocent laughter ).

Really, I suddenly realized that the idea of "The driver code calling a function to write eth0 information to the/proc/NET/dev file" is incorrect, at least not deep
Understand the meaning of files in Linux. In Linux VFS, the read/write operations on various files can be implemented differently.
The file does not have the write implementation, which is why kernel code is not harvested from the driver's perspective, because the file/proc/NET/Dev is not written at all, naturally, no direction is found.
The/proc/NET/dev function adds eth0 content.
But why does cat
What happens to the/proc/NET/dev content? First of all, if seemingly unrelated, did the magicians David composphil really remove the statue of liberty from the disappearance of the Statue of Liberty? Of course not.
Yes, but the Statue of Liberty does not exist in the scene he showed the audience. The audience thought the Statue of Liberty had disappeared. Linux VFS is like magic, and the operation letter of those files in/proc
The number is very special. You do not need to write the content of the/proc file directly to add the content, you can use the magic show function to provide information to users.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.