seq_file operations, with PPP dump implimentation

本文详细介绍了如何利用seq_file简化PPP网络模块的数据展示逻辑,通过使用seq_operations和seq_file_open函数,实现了一个更高效且易于维护的数据展示方案。文章包括了对seq_file原理的概述、具体实现细节、seq_file的内部工作流程以及如何为特定应用定制化seq_operations函数。通过对比传统的proc文件方式和seq_file的方式,展示了seq_file在减少代码冗余、提高模块维护性方面的优势。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<1>intro

with kernel module and function impliment, we usually export some info to let others know its running status.

and to make it, to traditional way is to write an "proc" entry by "create_proc_entry". but you'll always think that to achieve this simple purpose to much extra works are done but not the task you should force on.

linux kernel do have a better way! that's the seq_file way.

all the centrel is to present your data with "seq_start/next/show/stop", just like what misc driver do for char device modules.

<2>ppp impl

as it is not a new topic for this, and i just find one place to have a try.

+//=======================================================
+#ifdef CONFIG_PROC_FS
+struct ppp_seq_iter {
+    struct seq_net_private p;
+    int index;
+};
+
+static void *ppp_channel_seq_start(struct seq_file *m, loff_t *pos) {
+    struct ppp_seq_iter *iter = m->private;
+    struct ppp_net *pn = ppp_pernet(seq_file_net(m));
+    struct channel *pch;
+    int index = iter->index;
+    if (*pos == 0) {
+        printk(KERN_INFO "ppp_channel_seq_start: pos==0\n");
+        return SEQ_START_TOKEN;
+    }
+
+    //initialed as index 0 here, after stopped, it should also be 0
+    if(index == 0) return NULL;
+    mutex_lock(&ppp_mutex);
+
+    spin_lock_bh(&pn->all_channels_lock);
+    pch = ppp_find_channel(pn, index);
+    if(pch) atomic_inc(&pch->file.refcnt);
+    spin_unlock_bh(&pn->all_channels_lock);
+
+    mutex_unlock(&ppp_mutex);
+
+    return pch;
+}
+
+static void *ppp_channel_seq_next(struct seq_file *m, void *v, loff_t *pos) {
+    struct ppp_seq_iter *iter = m->private;
+    struct ppp_net *pn = ppp_pernet(seq_file_net(m));
+    struct channel *pch;
+    int index = iter->index - 1;
+
+    mutex_lock(&ppp_mutex);
+
+    spin_lock_bh(&pn->all_channels_lock);
+    //first search
+    if(v == SEQ_START_TOKEN) {
+        index = pn->last_channel_index;
+    }
+
+    while (index >= 0) {
+    pch = ppp_find_channel(pn, index);
+    if(pch) {
+        iter->index = index;
+    break;
+        }
+    index--;
+    }
+    if(pch) atomic_inc(&pch->file.refcnt);
+    spin_unlock_bh(&pn->all_channels_lock);
+
+    mutex_unlock(&ppp_mutex);
+
+    return pch;
+}
+
+static int ppp_channel_seq_show(struct seq_file *m, void *v) {
+    struct channel *pch = v;
+    struct ppp_net *pn = ppp_pernet(seq_file_net(m));
+
+    if (v == SEQ_START_TOKEN) {
+        seq_printf(m, " total channel: %d\n", atomic_read(&channel_count));
+        seq_puts(m, "- - - - -\n");
+        seq_printf(m, " channel dead \t %-12s %-12s %16s %s closing\n",
+            "xq", "rq", "chan", "ppp");
+        seq_puts(m, "---------\n");
+    } else {
+    //printk(KERN_INFO " ppp_channel_seq_show: %d <> %d in m\n",
+        //pch->file.index, ((struct ppp_seq_iter *)(m->private))->index);
+    if (pch != NULL) {
+        //pch->file.refcnt > 0, no lock needed, just care the ppp
+        mutex_lock(&pn->all_ppp_mutex);
+        seq_printf(m, " %-8d %d \t %-12u %-12u %p %s %-4d\n",
+            pch->file.index, pch->file.dead,
+            skb_queue_len(&pch->file.xq), skb_queue_len(&pch->file.rq),
+            pch->chan, (pch->ppp&&pch->ppp->dev)? pch->ppp->dev->name: "NULL",
+            pch->ppp? pch->ppp->closing: -1);
+        mutex_unlock(&pn->all_ppp_mutex);
+
+        if (atomic_dec_and_test(&pch->file.refcnt)) {
+            ppp_destroy_channel(pch);
+        }
+    } else {
+        seq_printf(m, " channel: %d not exists!\n",
+            ((struct ppp_seq_iter *)(m->private))->index-1);
+    }
+    }
+
+       return 0;
+}
+
+static void ppp_channel_seq_stop(struct seq_file *m, void *v) {
+    struct ppp_seq_iter *iter = m->private;
+    iter->index=0;
+}
+
+static const struct seq_operations raw_seq_ops = {
+       .start = ppp_channel_seq_start,
+       .next  = ppp_channel_seq_next,
+       .stop  = ppp_channel_seq_stop,
+       .show  = ppp_channel_seq_show,
+};
+
+static int ppp_seq_open(struct inode *inode, struct file *file)
+{
+       return seq_open_net(inode, file, &raw_seq_ops,
+                sizeof(struct ppp_seq_iter));
+}
+
+static const struct file_operations ppp_seq_fops = {
+       .owner   = THIS_MODULE,
+       .open    = ppp_seq_open,
+       .read    = seq_read,
+       .llseek  = seq_lseek,
+       .release = seq_release_net,
+};
+#endif
+
 static __net_init int ppp_init_net(struct net *net)
 {
        struct ppp_net *pn = net_generic(net, ppp_net_id);
@@ -887,6 +1018,12 @@ static __net_init int ppp_init_net(struct net *net)
 
        spin_lock_init(&pn->all_channels_lock);
 
+#ifdef CONFIG_PROC_FS
+       if (!proc_create("pppnet", S_IRUGO, net->proc_net, &ppp_seq_fops)) {
+               printk(KERN_INFO "ppp sysfs create fail\n");
+    }
+#endif
+
        return 0;
 }
it is an diff for "drivers/net/ppp/ppp_generic.c" as above shows.

all file_operations are done for us and only one "open" left. and in that open functions you provide the real seq_operations needed and an size of any size according.

there is much more manual details about it from lkmpg/x861 and seqfile howto, you can check it directly, so i'll not repeat.

<3>seq_file secrets

the screct of seq_file is just from its "open" function

 int seq_open(struct file *file, const struct seq_operations *op)
  {
          struct seq_file *p;
  
          WARN_ON(file->private_data);
  
          p = kzalloc(sizeof(*p), GFP_KERNEL);
          if (!p)
                  return -ENOMEM;
  
          file->private_data = p;
  
          mutex_init(&p->lock);
          p->op = op;
  #ifdef CONFIG_USER_NS
          p->user_ns = file->f_cred->user_ns;
  #endif
 
 void *__seq_open_private(struct file *f, const struct seq_operations *ops,
                 int psize)
 {
         int rc;
         void *private;
         struct seq_file *seq;
 
         private = kzalloc(psize, GFP_KERNEL);
         if (private == NULL)
                 goto out;
 
         rc = seq_open(f, ops);
         if (rc < 0)
                 goto out_free;
 
         seq = f->private_data;
         seq->private = private;
         return private;
 
 out_free:
         kfree(private);
 out:
         return NULL;
 }
 EXPORT_SYMBOL(__seq_open_private);
 
 int seq_open_private(struct file *filp, const struct seq_operations *ops,
                 int psize)
 {
         return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
 }
 EXPORT_SYMBOL(seq_open_private);

it alloc and initialize an seq_file object and attach your implimented operations to the original file struct, it also alloc memory for your transfered private data size.

and this memory is just for your own usage. you can do wahtever you'd like for that.

all other seq_open_XXX functions just an wrap of seq_open_private with changing the private data size, namely the param "psize".

our seq_open_net just make it alloc more memory to store our net struct if needed. and i just defined another own struct which wrapped the original net_private struct to store one more integer value.

all other seq_XXX functions related to file_operations call an traver function:

"static int traverse(struct seq_file *m, loff_t offset)"

its logic is just what our previous linked "lkmpg/x861" elaborates, it is really worh reading.

as i'm not able to repeat as clear as the referenced article do, just paste the code directly

 
         m->version = 0;
         index = 0;
         m->count = m->from = 0;
         if (!offset) {
                 m->index = index;
                 return 0;
         }
         if (!m->buf) {
                 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
                 if (!m->buf)
                         return -ENOMEM;
         }
         p = m->op->start(m, &index);//------------------<---your own start function
         while (p) {
                 error = PTR_ERR(p);
                 if (IS_ERR(p))
                         break;
                 error = m->op->show(m, p);//------------<---your own show function
                 if (error < 0)
                         break;
                 if (unlikely(error)) {
                         error = 0;
                         m->count = 0;
                 }
                 if (seq_has_overflowed(m))
                         goto Eoverflow;
                 if (pos + m->count > offset) {
                         m->from = offset - pos;
                         m->count -= m->from;
                         m->index = index;
                         break;
                 }
                 pos += m->count;
                 m->count = 0;
                 if (pos == offset) {
                         index++;
                         m->index = index;
                         break;
                 }
                 p = m->op->next(m, p, &index);//------------<---your own next function
         }
         m->op->stop(m, p);//--------------------------------<---your own stop function
         m->index = index;
         return error;
 
 Eoverflow:
         m->op->stop(m, p);
         kvfree(m->buf);
         m->count = 0;
         m->buf = seq_buf_alloc(m->size <<= 1);
         return !m->buf ? -ENOMEM : -EAGAIN;
 }
it starts with your start function and end with stop function, using next function to traverse all data entrys and show them accordingly till NULL got.

just repeat one as below for it is really impartant.

BE CARREFUL: when a sequence is finished, another one starts. That means that at the end of function stop(), the function start() is called again. This loop finishes when the function start() returns NULL. You can see a scheme of this in the figure "How seq_file works".

the figure lie in the lkmpg link.

one other notice from my own, not lock, is that:

do learn your own data entriy structures clear to make sure it is rightly shown under any conditions.

<4>references

http://lxr.free-electrons.com/source/fs/seq_file.c

http://kernelnewbies.org/Documents/SeqFileHowTo

http://linux.die.net/lkmpg/x861.html

内容概要:本文档详细介绍了Analog Devices公司生产的AD8436真均方根-直流(RMS-to-DC)转换器的技术细节及其应用场景。AD8436由三个独立模块构成:轨到轨FET输入放大器、高动态范围均方根计算内核和精密轨到轨输出放大器。该器件不仅体积小巧、功耗低,而且具有广泛的输入电压范围和快速响应特性。文档涵盖了AD8436的工作原理、配置选项、外部组件选择(如电容)、增益调节、单电源供电、电流互感器配置、接地故障检测、三相电源监测等方面的内容。此外,还特别强调了PCB设计注意事项和误差源分析,旨在帮助工程师更好地理解和应用这款高性能的RMS-DC转换器。 适合人群:从事模拟电路设计的专业工程师和技术人员,尤其是那些需要精确测量交流电信号均方根值的应用开发者。 使用场景及目标:①用于工业自动化、医疗设备、电力监控等领域,实现对交流电压或电流的精准测量;②适用于手持式数字万用表及其他便携式仪器仪表,提供高效的单电源解决方案;③在电流互感器配置中,用于检测微小的电流变化,保障电气安全;④应用于三相电力系统监控,优化建立时间和转换精度。 其他说明:为了确保最佳性能,文档推荐使用高质量的电容器件,并给出了详细的PCB布局指导。同时提醒用户关注电介质吸收和泄漏电流等因素对测量准确性的影响。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值