紧接上一篇,这里简要介绍net_device 结构和网卡驱动框架。  
  struct net_device  是一个比sk_buff 更复杂的结构,里面包含了与TCP/IP协议栈通信的接口函数,但是自从2.6.31 之后的内核中这些接口函数就被封装在了 stuct net_device_ops 结构中,在net_device 结构中以 netdev_ops 成员的形式出现。
我们就来看如何使用该结构:
1. 在初始化幻术中驱动需要分配一个net_device 结构,可以使用alloc_netdev()函数,也可以使用一个更常用的函数 alloc_etherdev() 函数来实现。这个函数在创建net_device 结构的同时也为驱动自定义的 私有数据结构分配了空间。
一般这样使用:
  struct net_device  *netdev;
  struct priv_struct *mycard_priv;
  netdev   = alloc_etherdev(sizeof(struct priv_struct));
  mycard_priv = netdev->priv; 
2. 使用register_netdev() 函数来注册net_device结构中的接口函数,并将其中的组成员进行赋值
3. 读取MAC地址,配置局域网唤醒机制。
 
现在我们来看看网卡驱动框架:(来源于Essential Linux Device Drivers,稍作改动)
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/skbuff.h>
#include <linux/ethtool.h>
struct net_device_stats mycard_stats;   /* Statistics */
/* Fill ethtool_ops methods from a suitable place in the driver */
struct ethtool_ops mycard_ethtool_ops = {/* ... */.get_eeprom = mycard_get_eeprom,      /* Dump EEPROM contents *//* ... */
};
/* Initialize/probe the card. For PCI cards, this is invokedfrom (or is itself) the probe() method. In that case, thefunction is declared as:static struct net_device *init_mycard(struct pci_dev *pdev, conststruct pci_device_id *id)
*/
static struct net_device *
init_mycard()
{struct net_device *netdev;struct priv_struct mycard_priv;/* ... */netdev = alloc_etherdev(sizeof(struct priv_struct));/* Common methods */netdev->netdev_ops = &my_netdev_ops;dev->ethtool_ops = &my_ethtool_ops;dev->watchdog_timeo = TX_TIMEOUT;netif_napi_add(dev, &tp->napi, my_poll, 64);/* Register the interface */register_netdev(netdev);/* ... *//* Get MAC address from attached EEPROM *//* ... *//* Download microcode if needed *//* ... */
}
/* The interrupt handler */
static irqreturn_t
mycard_interrupt(int irq, void *dev_id)
{struct net_device *netdev = dev_id;struct sk_buff *skb;unsigned int length;/* ... */if (receive_interrupt) {/* We were interrupted due to packet reception. At this point,the NIC has already DMA'ed received data to an sk_buff thatwas pre-allocated and mapped during device open. Obtain theaddress of the sk_buff depending on your data structuredesign and assign it to 'skb'. 'length' is similarly obtainedfrom the NIC by reading the descriptor used to DMA data fromthe card. Now, skb->data contains the receive data. *//* ... *//* For PCI cards, perform a pci_unmap_single() on thereceived buffer in order to allow the CPU to access it *//* ... *//* Allow the data go to the tail of the packet by movingskb->tail down by length bytes and increasingskb->len correspondingly */skb_put(skb, length)/* Pass the packet to the TCP/IP stack */
#if !defined (USE_NAPI)  /* Do it the old way */netif_rx(skb);
#else                    /* Do it the NAPI way */if (napi_schedule_prep(&priv->napi))) {/* Disable NIC interrupt. Implementation not shown. */disable_nic_interrupt();      /* Post the packet to the protocol layer andadd the device to the poll list */__napi_schedule(&priv->napi);}
#endif} else if (tx_complete_interrupt) {/* Transmit Complete Interrupt *//* ... *//* Unmap and free transmit resources such asDMA descriptors and buffers. Free sk_buffs orreclaim them into a free pool *//* ... */
}
}
/* Driver open */
static int
mycard_open(struct net_device *netdev)
{/* ... *//* Request irq */request_irq(irq, mycard_interrupt, IRQF_SHARED,netdev->name, dev);/* Fill transmit and receive rings *//* See the section,"Buffer Management and Concurrency Control" *//* ... *//* Provide free descriptor addresses to the card *//* ... *//* Convey your readiness to accept data from thenetworking stack */netif_start_queue(netdev);/* ... */
}
/* Driver close */
static int
mycard_close(struct net_device *netdev)
{/* ... *//* Ask the networking stack to stop sending down data */netif_stop_queue(netdev);/* ... */
}
/* Called when the device is unplugged or when the module isreleased. For PCI cards, this is invoked from (or is itself)the remove() method. In that case, the function is declared as:static void __devexit mycard_remove(struct pci_dev *pdev)
*/
static void __devexit
mycard_remove(){struct net_device *netdev;/* ... *//* For a PCI card, obtain the associated netdev as follows,assuming that the probe() method performed a correspondingpci_set_drvdata(pdev, netdev) after allocating the netdev */netdev = pci_get_drvdata(pdev); /*unregister_netdev(netdev);  /* Reverse of register_netdev() *//* ... */free_netdev(netdev);       /* Reverse of alloc_netdev() *//* ... */
}
/* Suspend method. For PCI devices, this is part ofthe pci_driver structure discussed in Chapter 10 */
static int
mycard_suspend(struct pci_dev *pdev, pm_message_t state)
{/* ... */netif_device_detach(netdev);/* ... */
}
/* Resume method. For PCI devices, this is part ofthe pci_driver structure discussed in Chapter 10 */
static int
mycard_resume(struct pci_dev *pdev)
{/* ... */netif_device_attach(netdev);/* ... */
}
/* Get statistics */
static struct net_device_stats *
mycard_get_stats(struct net_device *netdev)
{/* House keeping *//* ... */return(&mycard_stats);
}
/* Dump EEPROM contents. This is an ethtool_ops operation */
static int
mycard_get_eeprom(struct net_device *netdev,struct ethtool_eeprom *eeprom, uint8_t *bytes)
{/* Read data from the accompanying EEPROM *//* ... */
}/* Poll method */
static int
mycard_poll(struct net_device *netdev, int *budget)
{/* Post packets to the protocol layer usingnetif_receive_skb() *//* ... */if (no_more_ingress_packets()){/* Remove the device from the polled list */__napi_complete(netdev);/* Fall back to interrupt mode. Implementation not shown */enable_nic_interrupt();return 0;}
}
/* Transmit method */
static int
mycard_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
{/* DMA the transmit packet from the associated sk_buffto card memory *//* ... *//* Manage buffers *//* ... */
}

需要注意的是,网卡驱动使用了new api 机制,硬件对接收到的包是进行中断来通知驱动处理还是通过轮询的方式来处理,该机制进行了简单的判断。接收到的数据与协议栈进行通信才是核心部分,主要工作都在接收函数,需要将接收到的数据存放到sk_buff 结构中,然后对其进行调整填充等操作,然后直接将数据送往上一层。注意:网卡是一个数据链路层设备 !网卡驱动需要将接收到的数据送往网络层,将要发送的数据送到物理层,当然这个动作都由硬件来完成。

 

Linux 网卡驱动相关——03相关推荐

  1. linux双网卡驱动配置,linux网卡驱动安装、双网卡绑定

    本次课程包含RAID0/1/5/6/10/50/60配置实验(使用Dell R720服务器实验).Redhat/CentOS/ubuntu/windows操作系统安装.windows/linux网卡绑 ...

  2. 6.S081 lab: networking e1000 网卡驱动 附 Linux 网卡驱动编写分析

    本文是 6.S081 操作系统课程学习最后一个 lab,编写一个 intel 的 e1000 网卡的驱动在 xv6 下.需要复习知识有:操作系统知识,计算机组成原理 DMA 相关,循环缓冲区的概念,e ...

  3. linux网卡驱动源码分析(一)

    linux网卡驱动源码分析(一) linux struct linux内核 网络 descriptor resources 转自http://blog.csdn.net/ustc_dylan/arti ...

  4. linux网卡驱动对XDP支持情况

    各个网卡厂商对应的Linux内核驱动如下 Mellanox: mlx4 (4.8) and mlx5 (4.9) QLogic/Cavium: qede (4.10) Virtio_net: (4.1 ...

  5. linux网卡驱动离线安装_Linux网卡驱动的安装方式

    如何安装Linux网卡驱动呢,看看下面的说明. 适用机型: 所有xSeries 205; 所有xSeries 206; 所有xSeries 225; 所有xSeries 226; 所有xSeries ...

  6. linux网卡驱动开发视频,Linux下网卡驱动程序的开发.doc

    Linux下网卡驱动程序的开发 论文题目:Linux下网卡驱动程序的开发 专 业: 年 级: 学生学号: 学生姓名: 指导教师: 完成时间: Linux下网卡驱动程序的开发 八年经验 专业指导毕业设计 ...

  7. Linux网卡驱动分析之RTL8139(五)

    Linux网卡驱动分析之RTL8139(五) deliver_skb(dev.c) // 该函数就是调用个协议的接收函数处理该skb 包,进入第三层网络层处理 static __inline__ in ...

  8. Linux网卡驱动设计

    Linux网卡驱动设计 Linux网络体系结构 Linux的优点之一在于它丰富而稳定的网络协议栈.其范围从协议无关层到各种具体的网络协议实现. 协议层次对比图 网络接口层提供访问物理设备的驱动程序,对 ...

  9. linux网卡驱动 pdf,Linux下网卡驱动程序.pdf

    zekairecv 于 2015-10-04 00:58:57发表: 谢谢 weilee1 于 2015-04-19 17:41:05发表: 看看 雪语阑风 于 2014-12-04 11:03:39 ...

最新文章

  1. Linux字符设备驱动程序的框架(新写法)
  2. python注册登录系统_Python实现简单用户注册信息管理系统
  3. linux系统中的目录讲解
  4. HTML 块标签,行内标签,行内块标签以及之间的相互转换
  5. js监听只读文本框_js 动态控制 input 框 的只读属性
  6. pycharm python部署_使用PyCharm配合部署Python的Django框架的配置纪实
  7. day10 java的this关键字
  8. 机器学习笔记(6):多类逻辑回归-使用gluon
  9. 数据可视化工具的意义有哪些
  10. Python(五):list、tuple
  11. Javascript+PHP实现在线拍照功能
  12. 间接访问百度浏览器c语言程序,百度浏览器支持“.网址”域名访问 让中文上网更便捷...
  13. 网站运营的9个常用搜索技巧
  14. 良仓远行 · 硅谷遇上西雅图,同游招募
  15. 电信联通提高手机补贴
  16. wpa_supplicant、hostapd编译
  17. Scrolling and zooming chart with ChartScroller
  18. 微信扫描下载提示以及js判断用户手机系统
  19. Aop介绍 ,aop使用 aop解释
  20. centos命令行常用快捷键

热门文章

  1. 2017 年脑机接口研发热点回眸
  2. 他开发了 redux,昨晚“字节一面”却挂了?
  3. 2017年卖掉全副身家买比特币,全家人一起游牧……这个企业家好疯狂!
  4. 笑死,别再黑程序员了好吗? | 每日趣闻
  5. 大厂技术文档:Redis+Nginx+Spring全家桶+Dubbo精选
  6. [WC2013]平面图——平面图点定位
  7. 开发人员MySQL调优-实战篇2-让SQL使用索引详解
  8. nodejs安装及npm模块插件安装路径配置
  9. Android 仿微信朋友圈添加图片
  10. kotlin条件表达式