网络设备驱动基础笔记
vmlinz
posted @ Sun, 19 Jun 2011 21:41:43 +0800
in Linux
with tags
driver linux network
, 4899 readers
网络设备驱动基础
网络设备驱动和块设备驱动的功能比较类似,都是发送和接收数据包(数据请求)。当然它们实际是有很多不同的,首先块设备在/dev
目录下有设备节点,而网络设备没有这样的设备入口。read,write等常规的文件接口在网络设备下也没有意义。
最大的区别在于:块设备只响应内核的数据请求;而网络设备驱动要异步地接收来自外部的数据包。简单地说,块设备驱动是被要求传输数据而网络设备是主动请求传输数据。
网络设备驱动还需要支持设置地址,修改传输参数等等这样的操作,所以网络设备驱动的api需要提供这些接口。
网络设备注册
-
头文件:
<linux/netdevice.h>
-
struct net_device
网络设备结构体 -
struct net_device *alloc_netdev (int size_priv, const char *name,
void (*setup)(struct net_device *));
-
int register_netdev(struct net_device *device);
注册网络设备 -
void unregitster_netdev(struct net_device *device);
注销网络设备
打开和关闭
驱动在加载入内核后,内核会调用probe函数来探测它。在网络接口可以传送数据包时,内核必须首先打开它并给它设置地址。内核打开和关闭网络接口是由ifconfig命令触发的。
-
int (*open)(struct net_device*);
打开网络设备 -
int (*stop)(struct net_device*);
关闭网络设备 -
void netif_start_queue(struct net_device*);
启动网络传输队列 -
void netif_stop_queue(struct net_device*);
关闭网络传输队列
网络数据的发送
网络接口最重要的作用是发送和接收网络数据。
-
头文件:
<linux/skbuff.h>
定义了网络驱动中传输的基本单元,struct sk_buff
- struct netdeviceops 网络设备驱动需要实现的接口函数
-
netdev_tx_t (*ndo_start_xmit) (struct sk_buff *skb,
struct net_device *dev);
传输网络数据包的函数 -
void (*ndo_tx_timeout) (struct net_device *dev);
传输超时函数
网络数据的接收
接收网络数据相对于发送数据要复杂一些,因为你需要在原子上下文中把分配一个sk_buff
并把它移交给上层处理。
数据包接收有两种实现方式:中断驱动和轮询。大多数驱动都是中断驱动的,有一些高吞吐量的驱动会使用轮询的方式。
-
struct sk_buff *dev_alloc_skb(unsigned int length);
原子上下文中分配skb -
printk_ratelimit()
限制printk的输出频率,在中断相应函数中减少输出 - 实现高吞吐量的网络驱动,要减少网络阻塞最好的方法是使用napi,后面会介绍
中断处理
硬件可以中断cpu发送两种事件:新的数据包到来和发送的数据包已经发送完成。
- 判断并处理数据包事件
- 如果在其他地方暂时停止了发送队列,应该在中断函数中重新启动它
NAPI
高吞吐量的网络接口如果每个数据包都用中断来处理的话会给系统带来很大的负担,这个时候应该使用基于轮询的NAPI。这样可以减轻系统的负担,减少阻塞的时间。
只有极少数的设备实现了NAPI,因为实现起来比中断要复杂,而且有其他的一些条件。
在中断处理函数中,首先禁止进一步的中断处理,然后调度轮询函数,进入轮询函数后连续处理多个数据发送请求。
-
int (*poll)(struct net_device *dev, int *budget);
网络驱动轮询函数 -
int netif_rx_schedule(struct net_device *dev);
准备调用轮询函数 -
int (*poll)(struct net_device *dev, int *budget);
轮询函数
其他
- MAC Address Resolution
- Custom IOCTLs
- Multicasts
Thu, 24 Feb 2022 14:48:34 +0800
I would like to express my appreciation to you just for bailing me out of this setting. Just after researching through the internet and seeing basics which were not pleasant, I thought my entire life was done. Living minus the solutions to the problems you’ve fixed through your entire short post is a serious case, as well as ones that might have in a wrong way damaged my entire career if I hadn’t come across your web page. The training and kindness in maneuvering every item was very helpful. I am not sure what I would have done if I hadn’t encountered such a point like this. I can at this point relish my future. Thanks so much for your high quality and result oriented help. I won’t hesitate to suggest your site to any person who requires guide on this subject. Bitcoin Payment Gateway
==========================================================
This was really an interesting topic and I kinda agree with what you have mentioned here! North American Bancard Agent Program
=============================================================
It was wondering if I could use this write-up on my other website, I will link it back to your website though.Great Thanks. credit card processing ISO programs
Sat, 05 Mar 2022 16:34:13 +0800
Exceptional entry! I found it very interesting. I'll check back later to see if more posts are added. www.dggrand123.com
===================================================
You have noted very interesting points! ps nice web site. https://dggrand123.com
==========================================================
Good work done by the blogger, Keep up the work going…………. dggrand123.com
Sun, 24 Sep 2023 15:35:40 +0800
There couple of intriguing points in time in this post but I do not know if I see these people center to heart. There is some validity but I most certainly will take hold opinion until I investigate it further. Great post , thanks and we want more! Included in FeedBurner likewise 온라인홀덤
Mon, 13 Nov 2023 00:03:07 +0800
One thing that is constant with artificial intelligence and technology is that it is always evolving and improving. With the right AI applications and tech, esports and entertainment can level up and completely become its own standalone, prolific industry.. seed phrase crack
Thu, 08 Feb 2024 20:16:28 +0800
Your first therapy session may seem uncomfortable, but it’s a chance to get to know your therapist. Here’s what usually happens:. therapists in Glastonbury CT