GitHub地址:https://github.com/gautamanshul/Network-driver

项目中的Makefile可能有一定的问题,我重写了一下,并在下面给出network.c和makefile文件;

network.c

#include <linux/module.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/ip.h>
#include <linux/skbuff.h>#define RX_INTR 0x0001
#define TX_INTR 0X0002struct net_device *net0,*net1;struct os_packet {struct net_device *dev;int datalen;u8 data[ETH_DATA_LEN];
};struct os_priv { struct net_device_stats stats;int status;struct os_packet *pkt;int rx_int_enabled;int tx_packetlen;u8 *tx_packetdata;struct skbuff *skb;spinlock_t lock;struct net_device *dev;
};int os_open(struct net_device *dev) { return 0; }
int os_stop(struct net_device *dev) { return 0; }
static void os_interrupt(int irq, void *dev_id, struct pt_regs *regs) { }
static void os_hw_tx(char *buf, int len, struct net_device *dev) { }
int os_start_xmit(struct sk_buff *skb, struct net_device *dev) {return 0;
}struct net_device_stats *os_stats(struct net_device *dev) {return &(((struct os_priv *)netdev_priv(dev))->stats);
} int os_header(struct sk_buff *skb, struct net_device *dev, unsigned short type, const void *daddr, const void *saddr, unsigned int len) {return 0;
}static const struct header_ops os_header_ops = {.create = os_header,
};static const struct net_device_ops os_device_ops = {.ndo_open = os_open,.ndo_stop = os_stop,.ndo_start_xmit = os_start_xmit,.ndo_get_stats = os_stats,
};int os_init_mod(void) {struct os_priv *priv;int i;/*net0 and net1 point to net_device structs. Bypassing the PCI as we don't have a real device*/net0 = alloc_etherdev(sizeof(struct os_priv));net1 = alloc_etherdev(sizeof(struct os_priv));for(i = 0; i < 6; i++) net0->dev_addr[i] = (unsigned char)i;for(i = 0; i < 6; i++) net0->broadcast[i] = (unsigned char)15;net0->hard_header_len = 14;for(i = 0; i < 6; i++) net1->dev_addr[i] = (unsigned char)i;for(i = 0; i < 6; i++) net1->broadcast[i] = (unsigned char)15;net1->hard_header_len = 14;net1->dev_addr[5]++;memcpy(net0->name, "net0\0", 5); memcpy(net0->name, "net1\0", 5);net0->netdev_ops = &os_device_ops;net0->header_ops = &os_header_ops;net1->netdev_ops = &os_device_ops;net1->header_ops = &os_header_ops;net0->flags |= IFF_NOARP;net1->flags |= IFF_NOARP;priv = netdev_priv(net0);memset(priv, 0, sizeof(struct os_priv));priv->dev = net0;priv->rx_int_enabled = 1;priv->pkt = kmalloc(sizeof(struct os_packet), GFP_KERNEL);priv->pkt->dev = net0;priv = netdev_priv(net1);memset(priv, 0, sizeof(struct os_priv));priv->dev = net1;priv->rx_int_enabled = 1;priv->pkt = kmalloc(sizeof(struct os_packet), GFP_KERNEL);priv->pkt->dev = net1;if(register_netdev(net0)) {printk(KERN_INFO "net0 not registered\n");} else {printk(KERN_INFO "net0 registered\n");}if(register_netdev(net1)) {printk(KERN_INFO "net1 not registered\n");} else {printk(KERN_INFO "net1 registered\n");}return 0;
}void os_exit_mod(void)
{struct os_priv *priv;if(net0) {priv = netdev_priv(net0);kfree(priv->pkt);unregister_netdev(net0);}if(net1) {priv = netdev_priv(net1);kfree(priv->pkt);unregister_netdev(net1);}
}MODULE_LICENSE("GPL");
module_init(os_init_mod);
module_exit(os_exit_mod);

makefile

ifneq ($(KERNELRELEASE),)
MODULE_NAME = networkmodule
$(MODULE_NAME)-objs := network.o
obj-m := $(MODULE_NAME).o
else
KERNEL_DIR = /lib/modules/`uname -r`/build
MODULEDIR := $(shell pwd).PHONY: modules
default: modulesmodules:make -C $(KERNEL_DIR) M=$(MODULEDIR) modulesclean distclean:rm -f *.o *.mod.c .*.*.cmd *.korm -rf .tmp_versions
endif

结构体net_device_ops

$ pwd
/usr/src/kernels/3.10.0-693.el7.x86_64/include/linux
$ grep net_device_ops netdevice.h --color=auto -n
830: * It is an extension of net_device_ops. Drivers that want to use any of the
831: * fields defined here must initialize net_device_ops->ndo_size to
832: * sizeof(struct net_device_ops).
890:struct net_device_ops_extended {
1197:struct net_device_ops {
1407:    * net_device_ops_extended. The reserved slots above can be used
1410:    * net_device_ops, if they allocated the net_device_ops structure
1412:    * using others' net_device_ops must access the extended fields
1415:   RH_KABI_EXTEND(struct net_device_ops_extended extended)
1419:   const struct net_device_ops *__ops = (ops);            \
1420:   size_t __off = offsetof(struct net_device_ops, extended.field);    \
1612:   const struct net_device_ops *netdev_ops;
3979:static inline netdev_tx_t __netdev_start_xmit(const struct net_device_ops *ops,
3990:   const struct net_device_ops *ops = dev->netdev_ops;

结构体header_ops

./netdevice.h:276:struct header_ops {
./netdevice.h:1616: const struct header_ops *header_ops;
./netdevice.h:2735: if (!dev->header_ops || !dev->header_ops->create)
./netdevice.h:2738: return dev->header_ops->create(skb, dev, type, daddr, saddr, len);
./netdevice.h:2746: if (!dev->header_ops || !dev->header_ops->parse)
./netdevice.h:2748: return dev->header_ops->parse(skb, haddr);
./netdevice.h:2755: if (!dev->header_ops || !dev->header_ops->rebuild)
./netdevice.h:2757: return dev->header_ops->rebuild(skb);

[GitHub]一个简单的网络驱动相关推荐

  1. visjs使用小记-1.创建一个简单的网络拓扑图

    1.插件官网:http://visjs.org/  2.创建一个简单的网络拓扑图 <!doctype html> <html> <head><title> ...

  2. 使用eNSP搭建一个简单的网络

    使用eNSP搭建一个简单的网络 第一次使用eNSP搭建一个简单的网络,在师傅的耐心讲解及自己的慢慢摸索下最终使得3台PC之间通过静态路由实现了互通. 一.前言 华为现在不知道是在eNSP进行优化还是咋 ...

  3. 【Pytorch分布式训练】在MNIST数据集上训练一个简单CNN网络,将其改成分布式训练

    文章目录 普通单卡训练-GPU 普通单卡训练-CPU 分布式训练-GPU 分布式训练-CPU 租GPU服务器相关 以下代码示例基于:在MNIST数据集上训练一个简单CNN网络,将其改成分布式训练. 普 ...

  4. [stm32] 一个简单的stm32vet6驱动的天马4线SPI-1.77寸LCD彩屏DEMO

    书接上文<1.一个简单的nRF51822驱动的天马4线SPI-1.77寸LCD彩屏DEMO> 我们发现用16MHz晶振的nRF51822驱动1.77寸的spi速度达不到要求 本节主要采用7 ...

  5. 驱动开发之六 --- 一个简单的显示驱动之一 [译文]

    这个系列的文章在网上到处都是 这里也不清楚谁才是原文作者 我这里做个整理,标注一下希望大家能看的更加舒服一点 目录 (一)驱动开发一个简单的显示驱动 (二)驱动开发一个简单的显示驱动 (三)驱动开发一 ...

  6. 安装GNS3以及实现一个简单的网络拓扑图

    双击(GNS3.exe)安装包按顺序安装就可以到下面的一个界面: 安装完成后路由器选项卡是灰色的(不可用),可以通过挂载的方式添加对应的路由器,因此必须挂载相应镜像,下面是相应的步骤: 找到文件中相应 ...

  7. [nRF51822] 1、一个简单的nRF51822驱动的天马4线SPI-1.77寸LCD彩屏DEMO

    最近用nRF51822写了个天马4线SPI的1.77寸LCD彩屏驱动,效果如下: 屏幕的规格资料为:http://pan.baidu.com/s/1gdfkr5L 屏幕的驱动资料为:http://pa ...

  8. 从零开始学习linux的I2C设备驱动框架——写一个简单的SHT20驱动

    目录 0.测试环境说明 1.设备树的修改 2.设备驱动框架 3.I2C数据传输过程 3.1 struct i2c_msg 3.2 SHT20的数据收发 4.I2C适配器超时等待时间的修改 本文资源 参 ...

  9. 一个简单的网络验证分析(菜鸟)

    http://bbs.pediy.com/showthread.php?t=45356&highlight=%E7%BD%91%E7%BB%9C+%E7%BB%9C%E9%AA%8C+%E9% ...

最新文章

  1. STM32串口+DMA使用1
  2. 2019ICPC(沈阳) - Fish eating fruit(树形dp+树根转移)
  3. 下一个全排列_下一个排列
  4. aix ntp 配置_aix下开启ntp服务
  5. 独立游戏大电影观后感
  6. ue4导入abc文件问题
  7. LPSTR、LPCSTR、LPTSTR和LPCTSTR,LPVOID的意义及区别
  8. 陀螺仪、加速计、磁力计
  9. 地图编辑器开发(二)
  10. Android画一条虚线
  11. 隐私空间伪装计算机,隐私空间app(文件夹隐藏) 6.1.9 免root
  12. 鸿蒙OS内核分析|解读鸿蒙源码
  13. 安卓原生系统_全球首个原生安卓车载系统实测!操作流畅 可跟手机媲美
  14. Java如何配置环境变量?
  15. Matlab解方程, 等到数字解和解析式解
  16. Integrating Factor
  17. 2023校招C++开发oppo笔试
  18. ACWING133. 蚯蚓(栈)
  19. 软件测评师--第19小时 数据测试
  20. 湖北省天门市谷歌高清卫星地图下载

热门文章

  1. leetcode题解75-颜色分类
  2. java基础面试题之:普通类和抽象类有哪些区别?
  3. python写exploit采集器
  4. linux 同一个ip 绑定两个不同的域名 访问两个不同的项目
  5. Bootstrap系列 -- 17. 复选框checkbox和单选择按钮radio
  6. 【转载】推荐5款超实用的.NET性能分析工具
  7. Effective C++(9) 构造函数调用virtual函数会发生什么
  8. jQuery框架总体分析
  9. ASP.NET会话(Session)保存模式
  10. [理解需求变更之一]说说需求变更的必然