网卡驱动实现分析

网卡驱动实现分析

公司最近接到大单了,但是是NFV产品,由于NFV产品也是最近这一两年才开始买的多,之前没多少支持的虚拟网卡,网络转发性能需要提高。所以呢,就是需要移植更多虚拟网卡驱动(Virtio-net、Vmxnet3、以及i40evf万兆网卡等等)。~~这里先不复习SR-IOV是什么,等后面我也搞懂了,再写一篇来记录。~~我做的主要工作,也就是重写原来驱动源码中的收发包的函数,以及将原来的中断形式改写为纯轮询的收发报文,以此来提高转发效率并且将报文的处理交由二三层报文转发架构处理。下面的模块,我就先从网卡驱动的原理入手,分析网络报文的收发。

从内核架构看网卡

![image.png](https://img-blog.csdnimg.cn/img_convert/af3297bd50c7f7a181568dd80f38f02d.png#align=left&display=inline&height=296&margin=[object Object]&name=image.png&originHeight=592&originWidth=1160&size=126341&status=done&style=none&width=580)
从这张内核模块的架构图看,网卡处于网络模块的最底层。数据的收发,都要经过网卡,才能到达目的地。
下面的图是网络协议分层以及对应的协议,供后面分析参考。
![image.png](https://img-blog.csdnimg.cn/img_convert/dcb3759471d32dd8c68020f838281b59.png#align=left&display=inline&height=260&margin=[object Object]&name=image.png&originHeight=520&originWidth=1089&size=98512&status=done&style=none&width=544.5)

网卡模块的加载分析

首先,网卡的驱动以及最新的源代码由网卡厂商提供,如Intel,Broadcom,Marvell等。由于现在的Linux内核代码已经包含了许多网卡的驱动,如果你需要最新的,可能就需要自己编译加载了。下面是加载网卡驱动模块的步骤,以iavf网卡(万兆网卡,支持SR-IOV虚拟化)为例。
1)首先,到Intel官网搜索最新源代码;
![image.png](https://img-blog.csdnimg.cn/img_convert/3911ff65ee2ba0880732ea5f235250e0.png#align=left&display=inline&height=324&margin=[object Object]&name=image.png&originHeight=647&originWidth=1062&size=94310&status=done&style=none&width=531)
2)根据readme的说明编译驱动源码,这里我就把网页里面的readme
截出来。

To manually build the driver
----------------------------
1. Move the virtual function driver tar file to the directory of your choice.
   For example, use '/home/username/iavf' or '/usr/local/src/iavf'.

2. Untar/unzip the archive, where <x.x.x> is the version number for the
   driver tar file:

   # tar zxf iavf-<x.x.x>.tar.gz

3. Change to the driver src directory, where <x.x.x> is the version number
   for the driver tar:

   # cd iavf-<x.x.x>/src/

4. Compile the driver module:

   # make install

   The binary will be installed as:
   /lib/modules/<KERNEL VER>/updates/drivers/net/ethernet/intel/iavf/iavf.ko

   The install location listed above is the default location. This may differ
   for various Linux distributions.

5. Load the module using the modprobe command.

   To check the version of the driver and then load it:

   # modinfo iavf
   # modprobe iavf

   Alternately, make sure that any older iavf drivers are removed from the
   kernel before loading the new module:

   # rmmod iavf; modprobe iavf

6. Assign an IP address to the interface by entering the following,
   where <ethX> is the interface name that was shown in dmesg after modprobe:

   # ip address add <IP_address>/<netmask bits> dev <ethX>

7. Verify that the interface works. Enter the following, where IP_address
   is the IP address for another machine on the same subnet as the interface
   that is being tested:

   # ping <IP_address>

3)通过上面的步骤,就能够把网卡驱动加载进系统内了。(当然,这个的前提是你的网卡支持的是IAVF)

网卡初始化分析

这里主要说明,系统是如何识别出对应的网卡驱动的,以及网卡初始化的源码分析。

系统如何识别出对应网卡驱动

这里说以下,一个网卡对应的就是一个PCI设备,所以内核识别出网卡的过程,就是网卡这个设备怎么被注册到内核,以及内核如何来识别他的过程。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值