lsn37linux网卡驱动实战编写_第1页
lsn37linux网卡驱动实战编写_第2页
lsn37linux网卡驱动实战编写_第3页
lsn37linux网卡驱动实战编写_第4页
lsn37linux网卡驱动实战编写_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Linux络接口卡(NIC)驱LinuxNICdriver数据接口与socket接口体NICdriverNIC数据结构sk_buff与structsk_buff{union{structstructsk_buff*next;structsk_buff*prev;ktime_tstamp;structrb_nodestruct structnet_device unsignedintlen;unsignedintu16u16u32priority;Dma__tdma_;Sk_buff_data_ttail;Sk_buff_data_tend;unsignedchar*head;unsignedchar*data;unsignedinttruesize;Atomic_tusers;NextprevSk:socket关联packetTstamp包到达与搁置的时间Rbnode:Dev:代表数据操作的deviceLen:packet的总数量Mac_lenmacheader的长度Csum:packet’schecksumPriority代表packetQosHead指向socketbuffer的headEnd:socketbuffer的endStructnet_device就是一个美丽的错误,夹杂了协议与IOStruct当ifconfigeth0upNdo_openndo_validate_addr被调用当ifconfigeth0down,Ndo_stop被调用当MTU被改变的时候,ndo_change_mtu被调用当mac被改变的时候,ndo_set_mac_addressNetif_start_queue开始传输,放在ndo_openNetif_stop_queue停止传输,放在ndo_stop在ndo_open更新网络接口的mac请求资源,IOmemoryDMAchannelMapIRQ调用net_if_start_queue,让内核知道driver在probe分配一个networkdevice初始化bus请求与映射资源(IOmemoryDMAchannel填充netdevif_portirq,netdev_ops调用sha512./certs/signing_key.pem./certs/signing_key.x509#include#include#include<linux/netdevice.h>#include<linux/etherdevice.h>#includeMODULE_DESCRIPTION("Kernelmodulefornic");#define#definestructnic_privunsignedchar*tx_buf;unsignedinttx_len;u32msg_enable;staticstructnet_devicestaticvoiddump(unsignedchar*buffer)unsignedchar*p,sbuf[2*(sizeof(structethhdr)+intp=for(i=0;i<sizeof(structethhdr);i++)p+=sprintf(p,"%02X",}printk("eth%s\n",p=for(i=0;i<sizeof(structiphdr);i++)p+=sprintf(p,"%02X",buffer[sizeof(structethhdr)+}printk("iph%s\n",p=for(i=0;i<4;i++)p+=sprintf(p,"%02X",buffer[sizeof(structethhdr)+sizeof(structiphdr)+i]);}printk("payload%s\n",}staticvoidnic_rx(structnet_device*dev,intlen,unsignedchar{structsk_buffstructnic_priv*priv=netif_info(priv,hw,dev,"%s(#%d),func,LINE,skb=dev_alloc_skb(len+2);if(!skb){netif_err(priv,rx_err,"%s(#%d),rx:lowonmem-packetfunc,LINEreturn;}skb_reserve(skb,2);memcpy(skb_put(skb,len),buf,len);skb->dev=skb->protocol=eth_type_trans(skb,dev);skb->ip_summed=CHECKSUM_UNNECESSARY;dev->stats.rx_packets++;dev->stats.rx_bytes+=len;}staticintnic_open(structnet_device*dev)structnic_priv*priv=netdev_priv(dev);netif_info(priv,ifup,dev,"%s(#%d),priv:%p\n",func,LINE,priv->tx_buf=kmalloc(MAX_ETH_FRAME_SIZE,GFP_KERNEL);if(priv->tx_buf==NULL){netif_info(priv,ifup,dev,"%s(#%d),cannotalloctxfunc,return-ENOMEM;}

LINEreturn0;}staticintnic_stop(structnet_device*dev)structnic_priv*priv=netdev_priv(dev);netif_info(priv,ifdown,dev,"%s(#%d),priv:%p\n",func,LINE,if(priv->tx_buf) }return}staticvoidnic_hw_xmit(structnet_device*dev)structnic_priv*priv=netdev_priv(dev);structiphdr*iph;u32*saddr,*daddr;structin_device*in_dev;structin_ifaddrif(priv->tx_len<sizeof(structethhdr)+sizeof(structiphdr)){netif_info(priv,hw,dev,"%s(#%d),tooshort\n",func,LINEreturn}iph=(structiphdr*)(priv->tx_buf+sizeof(structethhdr));saddr=&iph->saddr;daddr=&iph-netif_info(priv,hw,dev,"%s(#%d),orig,src:%pI4,dst:%pI4,func,LINE,saddr,daddr,priv-in_dev=nic_dev[(dev==nic_dev[0]?1:0)]->ip_ptr;if(in_dev){//if_info=in_dev-for(if_info=in_dev->ifa_list;if_info;if_info=if_info->ifa_next){*saddr=*daddr=if_info->ifa_address;netif_info(priv,hw,dev,"%s(#%d),new,src:%pI4,}

func

LINE,saddr,if(!if_info)dev->stats.tx_droppednetif_info(priv,hw,dev,"%s(#%d),dropreturn}}

func

LINEiph->check=iph->check=ip_fast_csum((unsignedchar*)iph,iph-dev->stats.tx_packetsdev->stats.tx_bytes+=priv-nic_rx(nic_dev[(dev==nic_dev[0]?1:0)],priv->tx_len,}staticnetdev_tx_tnic_start_xmit(structsk_buff*skb,structnet_device*dev){structnic_priv*priv=netif_info(priv,drv,dev,"%s(#%d),orig,src:%pI4,func,LINE,&(ip_hdr(skb)->saddr),priv->tx_len=skb-if(likely(priv->tx_len<MAX_ETH_FRAME_SIZE)){if(priv->tx_len<ETH_ZLEN){memset(priv->tx_buf,0,ETH_ZLEN);priv->tx_len=ETH_ZLEN;}skb_copy_and_csum_dev(skb,priv->tx_buf); }else dev->stats.tx_dropped++;return}return}staticintnic_validate_addr(structnet_device*dev){structnic_priv*priv=netdev_priv(dev);netif_info(priv,drv,dev,"%s(#%d),priv:%p\n",func,LINE,return}staticintnic_change_mtu(structnet_device*dev,intnew_mtu){structnic_priv*priv=netdev_priv(dev);netif_info(priv,drv,dev,"%s(#%d),func,LINE,returneth_change_mtu(dev,}staticintnic_set_mac_addr(structnet_device*dev,void*addr)structnic_priv*priv=netdev_priv(dev);netif_info(priv,drv,dev,"%s(#%d),priv:%p\n",func,LINE,returneth_mac_addr(dev,}staticintnic_header_create(structsk_buff*skb,structunsignedshorttype,constvoid*daddr,constvoid*saddr,unsignedintlen){structnic_priv*priv=structethhdr*eth=(structethhdr*)skb_push(skb,ETH_HLEN);structnet_device*dst_netdev;netif_info(priv,drv,dev,func,LINEdst_netdev=nic_dev[(dev==nic_dev[0]?1:0)];eth->h_proto=htons(type);memcpy(eth->h_source,saddr?saddr:dev->dev_addr,memcpy(eth->h_dest,dst_netdev->dev_addr,dst_netdev-returndev-}staticconststructheader_opsnic_header_ops=.create=staticconststructnet_device_opsnic_netdev_ops=.ndo_open=.ndo_stop=.ndo_validate_addr=.ndo_start_xmit=.ndo_change_mtu=.ndo_set_mac_address=staticstructnet_device*nic_alloc_netdev(void)structnet_device*netdev=alloc_etherdev(sizeof(structif(!netdev)pr_err("%s(#%d):allocdevfailed",returnNULL;}

func

LINEnetdev->netdev_ops=&nic_netdev_ops;netdev->flags|=IFF_NOARP;netdev->features|=NETIF_F_HW_CSUM;netdev->header_ops=&nic_header_ops;return}staticintinitnic_init(void)intret=structnic_privpr_info("%s(#%d):install

func

LINEnic_dev[0]=nic_alloc_netdev();if(!nic_dev[0]){printk("%s(#%d):allocnetdev[0]LINEreturn-

func}nic_dev[1]=nic_alloc_netdev();if(!nic_dev[1]){printk("%s(#%d):allocnetdev[1]LINEgoto

func}ret=register_netdev(nic_dev[0]);if(ret){printk("%s(#%d):regnetdriverfailed.ret:LINE,goto

func}ret=register_netdev(nic_dev[1]);if(ret){printk("%s(#%d):regnetdriverfailed.LINE,goto

func}priv=netdev_pri

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论