S3C2440 网卡驱动介绍以及制作虚拟网卡驱动(二十五)

本文深入解析S3C2440网卡驱动原理,涵盖网络设备驱动的四层架构,详细介绍net_device结构体及sk_buff数据包处理流程。并通过实例演示如何构建虚拟网卡驱动,实现无需硬件即可响应网络请求。

http://www.cnblogs.com/lifexy/p/7763352.html

 S3C2440 网卡驱动介绍以及制作虚拟网卡驱动

1、描述

网卡的驱动其实很简单,它还是与硬件相关,主要是负责收发网络的数据包,它将上层协议传递下来的数据包以特定的媒介访问控制方式进行发送,并将接受到的数据包传递给上层协议。

网卡设备与字符设备和块设备不同,网络设备并不对应于/dev/目录下的文件,不过会存放在/sys/class/net目录下

 

2、Linux系统对网络设备驱动定义了4个层次,这4个层次有到下分为:

1)网络协议接口层:

实现统一的数据包收发的协议,该层主要负责调用dev_queue_xmit()函数发送数据,netif_rx()函数接收数据

2)网络设备接口层:

通过net_device结构体来描述一个具体的网络设备的信息,实现不同的硬件的统一

3)设备驱动功能层:

用来负责驱动网路设备硬件来完成各个功能,它通过hard_start_xmit()函数启动发送操作,并通过网络设备上的中断触发接收操作

4)网络设备与媒体层:

用来负责完成数据包发送和接受的物理实体,设备驱动功能层的函数都在这物理上驱动的

层次结构如下图所示:

 

3、网卡驱动初始化

而我们的网卡驱动程序,只需要编写网络设备接口层,填充net_device数据结构的内容并将net_device注册入内核,设置硬件相关操作,使能中断处理等。

3.1 其中net_device结构体的重要成员,整理后如下所示:

struct net_device
{
       char               name[IFNAMSIZ];              //网卡设备名称
       unsigned long              mem_end;             //该设备的内存结束地址
       unsigned long              mem_start;            //该设备的内存起始地址
       unsigned long              base_addr;            //该设备的内存I/O基地址
       unsigned int          irq;                       //该设备的中断号

       unsigned char        if_port;                  //多端口设备使用的端口类型
    unsigned char        dma;                     //该设备的DMA通道

       unsigned long              state;                    //网络设备和网络适配器的状态信息

             

      struct net_device_stats* (*get_stats)(struct net_device *dev); //获取流量的统计信息
                        //运行ifconfig便会调用该成员函数,并返回一个net_device_stats结构体获取信息

      struct net_device_stats  stats;      //用来保存统计信息的net_device_stats结构体

 
       unsigned long              features;        //接口特征,     
       unsigned int          flags; //flags指网络接口标志,以IFF_(Interface Flags)开头
//当flags =IFF_UP( 当设备被激活并可以开始发送数据包时, 内核设置该标志)、 IFF_AUTOMEDIA(设置设备可在多种媒介间切换)、
IFF_BROADCAST( 允许广播)、IFF_DEBUG( 调试模式, 可用于控制printk调用的详细程度) 、 IFF_LOOPBACK( 回环)、
IFF_MULTICAST( 允许组播) 、 IFF_NOARP( 接口不能执行ARP,点对点接口就不需要运行 ARP) 和IFF_POINTOPOINT( 接口连接到点到点链路) 等。

 
       unsigned        mtu;        //最大传输单元,也叫最大数据包

       unsigned short  type;     //接口的硬件类型

       unsigned short   hard_header_len;     //硬
虚拟网卡驱动源代码(原版): /* * 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、付费专栏及课程。

余额充值