Linux网络设备驱动程序——alloc_netdev()与alloc_etherdev()

本文介绍了将一个字符设备驱动转化为网络设备驱动的过程,特别是将原有的CNA卡驱动改造成以太网卡驱动。文章详细讨论了在Linux网络设备驱动中`alloc_netdev()`和`alloc_etherdev()`两个关键函数的作用,`alloc_netdev()`用于创建并初始化`struct net_device`结构体,而`alloc_etherdev()`则是针对以太网设备的简化版。文中还提到了如何通过这两个函数扩展设备的私有数据区域,并引用了CSDN上相关博客作为参考资料。
摘要由CSDN通过智能技术生成

前一段时间一直在看FCoE交换机方面的的代码,忽然老师让把前一段时间看过的师兄写的关于一个CNA卡的代码改一下,原来的CNA卡的驱动是一个字符设备驱动,现在要把它改成网络设备驱动,本来没有怎么在意,还是准备以交换机那边的为主,后来发现看了这么长时间的代码,真的需要好好地写一下代码了,再说以前看了很多关于网络设备的知识,很多东西都已经遗忘了,现在开始好好写一下这个驱动的代码,先把这个网卡做成一个以太网卡,然后在这个基础上在加工做成一个CNA卡。

这部分的内容有部分的内容参考csdn上面的别人的博客的中内容:http://blog.csdn.net/ropenyuan/article/details/6763663


我们知道,在网络设备驱动中,一个结构体至关重要,是网络设备的核心,就是struct net_device,在net/core/dev.c文件中有一个函数就是专门用来分配这样一个结构体的,

alloc_netdev()  这个函数用来生成一个net_device结构体,对其成员赋值并返回该结构体的指针。第一个参数是设备私有成员的大小,第二个参数为设备名,第三个参数为net_device的setup()函数指针。setup()函数接收的参数为struct net_device指针,用于预置net_device成员的值。

虚拟网卡驱动源代码(原版): /* * snull.c -- the Simple Network Utility * * Copyright (C) 2001 Alessandro Rubini and Jonathan Corbet * Copyright (C) 2001 O'Reilly & Associates * * The source code in this file can be freely used, adapted, * and redistributed in source or binary form, so long as an * acknowledgment appears in derived source files. The citation * should list that the code comes from the book "Linux Device * Drivers" by Alessandro Rubini and Jonathan Corbet, published * by O'Reilly & Associates. No warranty is attached; * we cannot take responsibility for errors or fitness for use. * * $Id: snull.c,v 1.21 2004/11/05 02:36:03 rubini Exp $ */ #include #include #include #include #include #include /* printk() */ #include /* kmalloc() */ #include /* error codes */ #include /* size_t */ #include /* mark_bh */ #include #include /* struct device, and other headers */ #include /* eth_type_trans */ #include /* struct iphdr */ #include /* struct tcphdr */ #include #include "snull.h" #include #include MODULE_AUTHOR("Alessandro Rubini, Jonathan Corbet"); MODULE_LICENSE("Dual BSD/GPL"); /* * Transmitter lockup simulation, normally disabled. */ static int lockup = 0; module_param(lockup, int, 0); static int timeout = SNULL_TIMEOUT; module_param(timeout, int, 0); /* * Do we run in NAPI mode? */ static int use_napi = 0; module_param(use_napi, int, 0); /* * A structure representing an in-flight packet. */ struct snull_packet { struct snull_packet *next; struct net_device *dev; int datalen; u8 data[ETH_DATA_LEN]; }; int pool_size = 8; module_param(pool_size, int, 0); /* * This structure is private to each device. It is used to
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值