【转】《Linux设备驱劝程序第三版》网卡驱动的注释笔记
Linux设备驱劝程序第三版》网卡驱动的注释笔记
《Linux设备驱劝程序第三版》网卡驱动的范例,讲述了网卡驱动编写的一般方法,脱离了实际硬件的束缚,是一个入门的好例子,在读懂了这个例子,再补充:
1、PCI驱动方面的知识;
2、硬件读写控制方面的知识;
《Linux设备驱劝程序第三版》网卡驱动的注释笔记
《Linux设备驱劝程序第三版》网卡驱动的范例,讲述了网卡驱动编写的一般方法,脱离了实际硬件的束缚,是一个入门的好例子,在读懂了这个例子,再补充:
1、PCI驱动方面的知识;
2、硬件读写控制方面的知识;
就可以去阅读实际的网卡驱动范例了。幸运的是,《Linux设备驱劝程序》这些方面的知识讲解还是非常到位的。以下是九贱读完这个范例代码的笔记,以做阅读本章内容的补充:
CODE:
![]()
Code
1![]()
/**//*
2
* snull.c -- the Simple Network Utility
3
*
4
* Copyright (C) 2001 Alessandro Rubini and Jonathan Corbet
5
* Copyright (C) 2001 O'Reilly & Associates
6
*
7
* The source code in this file can be freely used, adapted,
8
* and redistributed in source or binary form, so long as an
9
* acknowledgment appears in derived source files. The citation
10
* should list that the code comes from the book "Linux Device
11
* Drivers" by Alessandro Rubini and Jonathan Corbet, published
12
* by O'Reilly & Associates. No warranty is attached;
13
* we cannot take responsibility for errors or fitness for use.
14
*
15
* $Id: snull.c,v 1.21 2004/11/05 02:36:03 rubini Exp $
16
*/
17![]()
18
#include <linux/config.h>
19
#include <linux/module.h>
20
#include <linux/init.h>
21
#include <linux/moduleparam.h>
22![]()
23
#include <linux/sched.h>
24![]()
#include <linux/kernel.h> /**//* printk() */
25![]()
#include <linux/slab.h> /**//* kmalloc() */
26![]()
#include <linux/errno.h> /**//* error codes */
27![]()
#include <linux/types.h> /**//* size_t */
28![]()
#include <linux/interrupt.h> /**//* mark_bh */
29![]()
30
#include <linux/in.h>
31![]()
#include <linux/netdevice.h> /**//* struct device, and other headers */
32![]()
#include <linux/etherdevice.h> /**//* eth_type_trans */
33![]()
#include <linux/ip.h> /**//* struct iphdr */
34![]()
#include <linux/tcp.h> /**//* struct tcphdr */
35
#include <linux/skbuff.h>
36![]()
37
#include "snull.h"
38![]()
39
#include <linux/in6.h>
40
#include <asm/checksum.h>
41![]()
42
MODULE_AUTHOR("Alessandro Rubini, Jonathan Corbet");
43
MODULE_LICENSE("Dual BSD/GPL");
44![]()
45![]()
46![]()
/**//*
47
* Transmitter lockup simulation, normally disabled.
48
*/
49
static int lockup = 0;
50
module_param(lockup, int, 0);
51![]()
52
static int timeout = SNULL_TIMEOUT;
53
module_param(timeout, int, 0);
54![]()
55![]()
/**//*
56
* Do we run in NAPI mode?
57
*/
58
static int use_napi = 0;
59
module_param(use_napi, int, 0);
60![]()
61![]()
62![]()
/**//*
63
* A structure representing an in-flight packet.
64
*/
65![]()
struct snull_packet
{
66
struct snull_packet *next;
67
struct net_device *dev;
68
int datalen;
69
u8 data[ETH_DATA_LEN];
70
};
71![]()
72
int pool_size = 8;
73
module_param(pool_size, int, 0);
74![]()
75![]()
/**//*
76
* This structure is private to each device. It is used to pass
77
* packets in and out, so there is place for a packet
78
*/
79![]()
80![]()
struct snull_priv
{
81
struct net_device_stats stats;
82
int status;
83
struct snull_packet *ppool;
84![]()
struct snull_packet *rx_queue; /**//* List of incoming packets */
85
int rx_int_enabled;
86
int tx_packetlen;
87
u8 *tx_packetdata;
88
struct sk_buff *skb;
89
spinlock_t lock;
90
};
91![]()
92
static void snull_tx_timeout(struct net_device *dev);
93
static void (*snull_interrupt)(int, void *, struct pt_regs *);
94![]()
95![]()
/**//*
96
* 设置设备的包缓冲池.
97
* 当需要使用NAPI,而非中断处理的时候,设备需要能够保存多个数据包的能力,这个保存所需的缓存,
98
* 或者在板卡上,或者在内核的DMA环中。
99
* 作者这里的演示程序,根据pool_size的大小,分配pool_size个大小为struct snull_packet的缓冲区,
100
* 这个缓冲池用链表组织,“私有数据”结构的ppool成员指针指向链表首部。
101
*/
102
void snull_setup_pool(struct net_device *dev)
103![]()
![]()
{
104
struct snull_priv *priv = netdev_priv(dev);
105
int i;
106
struct snull_packet *pkt;
107![]()
108
priv->ppool = NULL;
109![]()
for (i = 0; i < pool_size; i++)
{
110
pkt = kmalloc (sizeof (struct snull_packet), GFP_KERNEL);
111![]()
if (pkt == NULL)
{
112
printk (KERN_NOTICE "Ran out of memory allocating packet pool\n");
113
return;
114
}
115
pkt->dev = dev;
116
pkt->next = priv->ppool;
117
priv->ppool = pkt;
118
}
119
}
120![]()
121![]()
/**//*因为snull_setup_pool分配了pool_size个struct snull_packet,所以,驱动退出时,需要释放内存*/
122
void snull_teardown_pool(struct net_device *dev)
123![]()
![]()
{
124
struct snull_priv *priv = netdev_priv(dev);
125
struct snull_packet *pkt;
126![]()
127![]()
while ((pkt = priv->ppool))
{
128
priv->ppool = pkt->next;
129
kfree (pkt);
130![]()
/**//* FIXME - in-flight packets ? */
131
}
132
}
133![]()
134![]()
/**//*
135
* 获取设备要传输的第一个包,传输队列首部相应的移动到下一个数据包.
136
*/
137
struct snull_packet *snull_get_tx_buffer(struct net_device *dev)
138![]()
![]()
{
139
struct snull_priv *priv = netdev_priv(dev);
140
unsigned long flags;
141
struct snull_packet *pkt;
142![]()
143
spin_lock_irqsave(&priv->lock, flags);
144
pkt = priv->ppool;
145
priv->ppool = pkt->next;
146![]()
if (priv->ppool == NULL)
{
147
printk (KERN_INFO "Pool empty\n");
148
netif_stop_queue(dev);
149
}
150
spin_unlock_irqrestore(&priv->lock, flags);
151
return pkt;
152
}
153![]()
154![]()
/**//*将包缓存交还给缓存池*/
155
void snull_release_buffer(struct snull_packet *pkt)
156![]()
![]()
{
157
unsigned long flags;
158
struct snull_priv *priv = netdev_priv(pkt->dev);
159![]()
160
spin_lock_irqsave(&priv->lock, flags);
161
pkt->next = priv->ppool;
162
priv->ppool = pkt;
163
spin_unlock_irqrestore(&priv->lock, flags);
164
if (netif_queue_stopped(pkt->dev) && pkt->next == NULL)
165
netif_wake_queue(pkt->dev);
166
}
167![]()
168![]()
/**//*将要传输的包加入到设备dev的传输队列首部,当然,这只是一个演示,这样一来,就变成先进先出了*/
169
void snull_enqueue_buf(struct net_device *dev, struct snull_packet *pkt)
170![]()
![]()
{
171
unsigned long flags;
172
struct snull_priv *priv = netdev_priv(dev);
173![]()
174
spin_lock_irqsave(&priv->lock, flags);
175![]()
pkt->next = priv->rx_queue; /**//* FIXME - misorders packets */
176
priv->rx_queue = pkt;
177
spin_unlock_irqrestore(&priv->lock, flags);
178
}
179![]()
180![]()
/**//*取得传输队列中的第一个数据包*/
181
struct snull_packet *snull_dequeue_buf(struct net_device *dev)
182![]()
![]()
{
183
struct snull_priv *priv = netdev_priv(dev);
184
struct snull_packet *pkt;
185
unsigned long flags;
186![]()
187
spin_lock_irqsave(&priv->lock, flags);
188
pkt = priv->rx_queue;
189
if (pkt != NULL)
190
priv->rx_queue = pkt->next;
191
spin_unlock_irqrestore(&priv->lock, flags);
192
return pkt;
193
}
194![]()
195![]()
/**//*
196
* 打开/关闭接收中断.
197
*/
198
static void snull_rx_ints(struct net_device *dev, int enable)
199![]()
![]()
{
200
struct snull_priv *priv = netdev_priv(dev);
201
priv->rx_int_enabled = enable;
202
}
203![]()
204![]()
205![]()
/**//*
206
* 设备打开函数,是驱动最重要的函数之一,它应该注册所有的系统资源(I/O端口,IRQ、DMA等等),
207
* 并对设备执行其他所需的设置。
208
* 因为这个例子中,并没有真正的物理设备,所以,它最重要的工作就是启动传输队列。
209
*/
210![]()
211
int snull_open(struct net_device *dev)
212![]()
![]()
{
213![]()
/**//* request_region(), request_irq(),
. (like fops->open) */
214![]()
215![]()
/**//*
216
* Assign the hardware address of the board: use "\0SNULx", where
217
* x is 0 or 1. The first byte is '\0' to avoid being a multicast
218
* address (the first byte of multicast addrs is odd).
219
*/
220
memcpy(dev->dev_addr, "\0SNUL0", ETH_ALEN);
221
if (dev == snull_devs[1])
222![]()
dev->dev_addr[ETH_ALEN-1]++; /**//* \0SNUL1 */
223
netif_start_queue(dev);
224
return 0;
225
}
226![]()
227![]()
/**//*设备停止函数,这里的工作就是停止传输队列*/
228
int snull_release(struct net_device *dev)
229![]()
![]()
{
230![]()
/**//* release ports, irq and such -- like fops->close */
231![]()
232![]()
netif_stop_queue(dev); /**//* can't transmit any more */
233
return 0;
234
}
235![]()
236![]()
/**//*
237
* 当用户调用ioctl时类型为SIOCSIFMAP时,如使用ifconfig,系统会调用驱动程序的set_config 方法。
238
* 用户会传递一个ifmap结构包含需要设置的I/O地址、中断等参数。
239
*/
240
int snull_config(struct net_device *dev, struct ifmap *map)
241![]()
![]()
{
242![]()
if (dev->flags & IFF_UP) /**//* 不能设置一个正在运行状态的设备 */
243
return -EBUSY;
244![]()
245![]()
/**//* 这个例子中,不允许改变 I/O 地址*/
246![]()
if (map->base_addr != dev->base_addr)
{
247
printk(KERN_WARNING "snull: Can't change I/O address\n");
248
return -EOPNOTSUPP;
249
}
250![]()
251![]()
/**//* 允许改变 IRQ */
252![]()
if (map->irq != dev->irq)
{
253
dev->irq = map->irq;
254![]()
/**//* request_irq() is delayed to open-time */
255
}
256![]()
257![]()
/**//* ignore other fields */
258
return 0;
259
}
260![]()
261![]()
/**//*
262
* 接收数据包函数
263
* 它被“接收中断”调用,重组数据包,并调用函数netif_rx进一步处理。
264
* 我们从“硬件”中收到的包,是用struct snull_packet来描述的,但是内核中描述一个包,是使用
265
* struct sk_buff(简称skb),所以,这里要完成一个把硬件接收的包拷贝至内核缓存skb的一个
266
* 组包过程(PS:不知在接收之前直接分配一个skb,省去这一步,会如何提高性能,没有研究过,见笑了^o^)。
267
*/
268
void snull_rx(struct net_device *dev, struct snull_packet *pkt)
269![]()
![]()
{
270
struct sk_buff *skb;
271
struct snull_priv *priv = netdev_priv(dev);
272![]()
273![]()
/**//*
274
* 分配skb缓存
275
*/
276
skb = dev_alloc_skb(pkt->datalen + 2);
277![]()
if (!skb)
{ /**//*分配失败*/
278
if (printk_ratelimit())
279
printk(KERN_NOTICE "snull rx: low on mem - packet dropped\n");
280
priv->stats.rx_dropped++;
281
goto out;
282
}
283![]()
/**//*
284
* skb_reserver用来增加skb的date和tail,因为以太网头部为14字节长,再补上两个字节就刚好16字节边界
285
* 对齐,所以大多数以太网设备都会在数据包之前保留2个字节。
286
*/
287![]()
skb_reserve(skb, 2); /**//* align IP on 16B boundary */
288
memcpy(skb_put(skb, pkt->datalen), pkt->data, pkt->datalen);
289![]()
290![]()
skb->dev = dev; /**//*skb与接收设备就关联起来了,它在网络栈中会被广泛使用,没道理不知道数据是谁接收来的吧*/
291![]()
skb->protocol = eth_type_trans(skb, dev); /**//*获取上层协议类型,这样,上层处理函数才知道如何进一步处理*/
292![]()
skb->ip_summed = CHECKSUM_UNNECESSARY; /**//* 设置较验标志:不进行任何校验,作者的驱动的收发都在内存中进行,是没有必要进行校验*/
293![]()
294![]()
/**//*累加计数器*/
295
priv->stats.rx_packets++;
296
priv->stats.rx_bytes += pkt->datalen;
297![]()
298![]()
/**//*
299
* 把数据包交给上层。netif_rx会逐步调用netif_rx_schedule -->__netif_rx_schedule,
300
* __netif_rx_schedule函数会调用__raise_softirq_irqoff(NET_RX_SOFTIRQ);触发网络接收数据包的软中断函数net_rx_action。
301
* 软中断是Linux内核完成中断推后处理工作的一种机制,请参考《Linux内核设计与实现》第二版。
302
* 唯一需要提及的是,这个软中断函数net_rx_action是在网络系统初始化的时候(linux/net/core/dev.c):注册的
303
* open_softirq(NET_RX_SOFTIRQ, net_rx_action, NULL);
304
*/
305
netif_rx(skb);
306
out:
307
return;
308
}
309![]()
310![]()
311![]()
/**//*
312
* NAPI 的poll轮询函数.
313
*/
314
static int snull_poll(struct net_device *dev, int *budget)
315![]()
![]()
{
316![]()
/**//*
317
* dev->quota是当前CPU能够从所有接口中接收数据包的最大数目,budget是在
318
* 初始化阶段分配给接口的weight值,轮询函数必须接受二者之间的最小值。表示
319
* 轮询函数本次要处理的数据包个数。
320
*/
321
int npackets = 0, quota = min(dev->quota, *budget);
322
struct sk_buff *skb;
323
struct snull_priv *priv = netdev_priv(dev);
324
struct snull_packet *pkt;
325![]()
326![]()
/**//*这个循环次数由要处理的数据包个数,并且,以处理完接收队列为上限*/
327![]()
while (npackets < quota && priv->rx_queue)
{
328![]()
/**//*从队列中取出数据包*/
329
pkt = snull_dequeue_buf(dev);
330![]()
331![]()
/**//*接下来的处理,和传统中断事实上是一样的*/
332
skb = dev_alloc_skb(pkt->datalen + 2);
333![]()
if (! skb)
{
334
if (printk_ratelimit())
335
printk(KERN_NOTICE "snull: packet dropped\n");
336
priv->stats.rx_dropped++;
337
snull_release_buffer(pkt);
338
continue;
339
}
340![]()
skb_reserve(skb, 2); /**//* align IP on 16B boundary */
341
memcpy(skb_put(skb, pkt->datalen), pkt->data, pkt->datalen);
342
skb->dev = dev;
343
skb->protocol = eth_type_trans(skb, dev);
344![]()
skb->ip_summed = CHECKSUM_UNNECESSARY; /**//* don't check it */
345![]()
346![]()
/**//*需要调用netif_receive_skb而不是net_rx将包交给上层协议栈*/
347
netif_receive_skb(skb);
348![]()
349![]()
/**//*累加计数器 */
350
npackets++;
351
priv->stats.rx_packets++;
352
priv->stats.rx_bytes += pkt->datalen;
353
snull_release_buffer(pkt);
354
}
355![]()
/**//* If we processed all packets, we're done; tell the kernel and reenable ints */
356
*budget -= npackets;
357
dev->quota -= npackets;
358![]()
359
//
360![]()
if (! priv->rx_queue)
{
361
netif_rx_complete(dev);
362
snull_rx_ints(dev, 1);
363
return 0;
364
}
365![]()
/**//* We couldn't process everything. */
366
return 1;
367
}
368![]()
369![]()
370![]()
/**//*
371
* 设备的中断函数,当需要发/收数据,出现错误,连接状态变化等,它会被触发
372
* 对于典型的网络设备,一般会在open函数中注册中断函数,这样,当网络设备产生中断时,如接收到数据包时,
373
* 中断函数将会被调用。不过在这个例子中,因为没有真正的物理设备,所以,不存在注册中断,也就不存在触
374
* 发,对于接收和发送,它都是在自己设计的函数的特定位置被调用。
375
* 这个中断函数设计得很简单,就是取得设备的状态,判断是“接收”还是“发送”的中断,以调用相应的处理函数。
376
* 而对于,“是哪个设备产生的中断”这个问题,则由调用它的函数通过第二个参数的赋值来决定。
377
*/
378
static void snull_regular_interrupt(int irq, void *dev_id, struct pt_regs *regs)
379![]()
![]()
{
380
int statusword;
381
struct snull_priv *priv;
382
struct snull_packet *pkt = NULL;
383![]()
/**//*
384
* 通常,需要检查 "device" 指针以确保这个中断是发送给自己的。
385
* 然后为 "struct device *dev" 赋
386
*/
387
struct net_device *dev = (struct net_device *)dev_id;
388![]()
389![]()
/**//* paranoid */
390
if (!dev)
391
return;
392![]()
393![]()
/**//* 锁住设备 */
394
priv = netdev_priv(dev);
395
spin_lock(&priv->lock);
396![]()
397![]()
/**//* 取得设备状态指字,对于真实设备,使用I/O指令,比如:int txsr = inb(TX_STATUS); */
398
statusword = priv->status;
399
priv->status = 0;
400![]()
if (statusword & SNULL_RX_INTR)
{ /**//*如果是接收数据包的中断*/
401![]()
/**//* send it to snull_rx for handling */
402
pkt = priv->rx_queue;
403![]()
if (pkt)
{
404
priv->rx_queue = pkt->next;
405
snull_rx(dev, pkt);
406
}
407
}
408![]()
if (statusword & SNULL_TX_INTR)
{ /**//*如果是发送数据包的中断*/
409![]()
/**//* a transmission is over: free the skb */
410
priv->stats.tx_packets++;
411
priv->stats.tx_bytes += priv->tx_packetlen;
412
dev_kfree_skb(priv->skb);
413
}
414![]()
415![]()
/**//* 释放锁 */
416
spin_unlock(&priv->lock);
417![]()
418![]()
/**//*释放缓冲区*/
419![]()
if (pkt) snull_release_buffer(pkt); /**//* Do this outside the lock! */
420
return;
421
}
422![]()
423![]()
/**//*
424
* A NAPI interrupt handler.
425
* 在设备初始化的时候,poll指向指向了snull_poll函数,所以,NAPI中断处理函数很简单,
426
* 当“接收中断”到达的时候,它就屏蔽此中断,然后netif_rx_schedule函数接收,接收函数
427
* 会在未来某一时刻调用注册的snull_poll函数实现轮询,当然,对于“传输中断”,处理方法
428
* 同传统中断处理并无二致。
429
*/
430
static void snull_napi_interrupt(int irq, void *dev_id, struct pt_regs *regs)
431![]()
![]()
{
432
int statusword;
433
struct snull_priv *priv;
434![]()
435![]()
/**//*
436
* As usual, check the "device" pointer for shared handlers.
437
* Then assign "struct device *dev"
438
*/
439
struct net_device *dev = (struct net_device *)dev_id;
440![]()
/**//*
and check with hw if it's really ours */
441![]()
442![]()
/**//* paranoid */
443
if (!dev)
444
return;
445![]()
446![]()
/**//* Lock the device */
447
priv = netdev_priv(dev);
448
spin_lock(&priv->lock);
449![]()
450![]()
/**//* retrieve statusword: real netdevices use I/O instructions */
451
statusword = priv->status;
452
priv->status = 0;
453![]()
454![]()
/**//*
455
* 唯一的区别就在这里,它先屏蔽掉接收中断,然后调用netif_rx_schedule,而不是netif_rx
456
* 重点还是在于poll函数的设计。
457
*/
458![]()
if (statusword & SNULL_RX_INTR)
{
459![]()
snull_rx_ints(dev, 0); /**//* Disable further interrupts */
460
netif_rx_schedule(dev);
461
}
462![]()
if (statusword & SNULL_TX_INTR)
{
463![]()
/**//* a transmission is over: free the skb */
464
priv->stats.tx_packets++;
465
priv->stats.tx_bytes += priv->tx_packetlen;
466
dev_kfree_skb(priv->skb);
467
}
468![]()
469![]()
/**//* Unlock the device and we are done */
470
spin_unlock(&priv->lock);
471
return;
472
}
473![]()
474![]()
475![]()
476![]()
/**//*
477
* Transmit a packet (low level interface)
478
*/
479
static void snull_hw_tx(char *buf, int len, struct net_device *dev)
480![]()
![]()
{
481![]()
/**//*
482
* This function deals with hw details. This interface loops
483
* back the packet to the other snull interface (if any).
484
* In other words, this function implements the snull behaviour,
485
* while all other procedures are rather device-independent
486
*/
487
struct iphdr *ih;
488
struct net_device *dest;
489
struct snull_priv *priv;
490
u32 *saddr, *daddr;
491
struct snull_packet *tx_buffer;
492![]()
493![]()
/**//* I am paranoid. Ain't I? */
494![]()
if (len < sizeof(struct ethhdr) + sizeof(struct iphdr))
{
495
printk("snull: Hmm
packet too short (%i octets)\n",
496
len);
497
return;
498
}
499![]()
500![]()
if (0)
{ /**//* enable this conditional to look at the data */
501
int i;
502
PDEBUG("len is %i\n" KERN_DEBUG "data:",len);
503
for (i=14 ; i<len; i++)
504
printk(" %02x",buf&0xff);
505
printk("\n");
506
}
507![]()
/**//*
508
* 取得来源IP和目的IP地址
509
*/
510
ih = (struct iphdr *)(buf+sizeof(struct ethhdr));
511
saddr = &ih->saddr;
512
daddr = &ih->daddr;
513![]()
514![]()
/**//*
515
* 这里做了三个调换,以实现欺骗:来源地址第三octet 0<->1,目的地址第三octet 0<->1,设备snX编辑0<->1,这样做的理由是:
516
* sn0(发):192.168.0.88 --> 192.168.0.99 做了调换后,就变成:
517
* sn1(收):192.168.1.88 --> 192.168.1.99 因为sn1的地址就是192.168.1.99,所以,它收到这个包后,会回应:
518
* sn1(发):192.168.1.99 --> 192.168.1.88 ,同样地,做了这样的调换后,就变成:
519
* sn0(收):192.168.0.99 --> 192.168.0.88 这样,sn0就会收到这个包,实现了ping的请求与应答,^o^
520
*/
521![]()
((u8 *)saddr)[2] ^= 1; /**//* change the third octet (class C) */
522
((u8 *)daddr)[2] ^= 1;
523![]()
524![]()
/**//*重新计算较验和*/
525![]()
ih->check = 0; /**//* and rebuild the checksum (ip needs it) */
526
ih->check = ip_fast_csum((unsigned char *)ih,ih->ihl);
527![]()
528![]()
/**//*输出调试信息*/
529
if (dev == snull_devs[0])
530
PDEBUGG("%08x:%05i --> %08x:%05i\n",
531
ntohl(ih->saddr),ntohs(((struct tcphdr *)(ih+1))->source),
532
ntohl(ih->daddr),ntohs(((struct tcphdr *)(ih+1))->dest));
533
else
534
PDEBUGG("%08x:%05i <-- %08x:%05i\n",
535
ntohl(ih->daddr),ntohs(((struct tcphdr *)(ih+1))->dest),
536
ntohl(ih->saddr),ntohs(((struct tcphdr *)(ih+1))->source));
537![]()
538![]()
/**//*调换设备编号,即dest指向接收设备,原因如前所述*/
539
dest = snull_devs[dev == snull_devs[0] ? 1 : 0];
540![]()
541![]()
/**//*将发送的数据添加到接收设备的接收队列中*/
542
priv = netdev_priv(dest);
543
tx_buffer = snull_get_tx_buffer(dev);
544
tx_buffer->datalen = len;
545
memcpy(tx_buffer->data, buf, len);
546
snull_enqueue_buf(dest, tx_buffer);
547![]()
548![]()
/**//*
549
* 如果设备接收标志打开,就调用中断函数把数据包发送给目标设备——即触发目的设备的接收中断,这样
550
* 中断程序就会自接收设备的接收队列中接收数据包,并交给上层网络栈处理
551
*/
552![]()
if (priv->rx_int_enabled)
{
553
priv->status |= SNULL_RX_INTR;
554
snull_interrupt(0, dest, NULL);
555
}
556![]()
557![]()
/**//*发送完成后,触发“发送完成”中断*/
558
priv = netdev_priv(dev);
559
priv->tx_packetlen = len;
560
priv->tx_packetdata = buf;
561
priv->status |= SNULL_TX_INTR;
562![]()
563![]()
/**//*
564
* 如果insmod驱动的时候,指定了模拟硬件锁的lockup=n,则在会传输n个数据包后,模拟一次硬件锁住的情况,
565
* 这是通过调用netif_stop_queue函数来停止传输队列,标记“设备不能再传输数据包”实现的,它将在传输的超
566
* 时函数中,调用netif_wake_queue函数来重新启动传输队例,同时超时函数中会再次调用“接收中断”,这样
567
* stats.tx_packets累加,又可以重新传输新的数据包了(参接收中断和超时处理函数的实现)。
568
*/
569![]()
if (lockup && ((priv->stats.tx_packets + 1) % lockup) == 0)
{
570![]()
/**//* Simulate a dropped transmit interrupt */
571![]()
netif_stop_queue(dev); /**//*停止数据包的传输*/
572
PDEBUG("Simulate lockup at %ld, txp %ld\n", jiffies,
573
(unsigned long) priv->stats.tx_packets);
574
}
575
else
576![]()
/**//*发送完成后,触发中断,中断函数发现发送完成,就累加计数器,释放skb缓存*/
577
snull_interrupt(0, dev, NULL);
578![]()
579![]()
/**//*
580
* 看到这里,我们可以看到,这个发送函数其实并没有把数据包通过I/O指令发送给硬件,而仅仅是做了一个地址/设备的调换,
581
* 并把数据包加入到接收设备的队例当中。
582
*/
583
}
584![]()
585![]()
/**//*
586
* 数据包传输函数,Linux网络堆栈,在发送数据包时,会调用驱动程序的hard_start_transmit函数,
587
* 在设备初始化的时候,这个函数指针指向了snull_tx。
588
*/
589
int snull_tx(struct sk_buff *skb, struct net_device *dev)
590![]()
![]()
{
591
int len;
592
char *data, shortpkt[ETH_ZLEN];
593
struct snull_priv *priv = netdev_priv(dev);
594![]()
595
data = skb->data;
596
len = skb->len;
597![]()
if (len < ETH_ZLEN)
{ /**//*处理短帧的情况,如果小于以太帧最小长度,不足位全部补0*/
598
memset(shortpkt, 0, ETH_ZLEN);
599
memcpy(shortpkt, skb->data, skb->len);
600
len = ETH_ZLEN;
601
data = shortpkt;
602
}
603![]()
dev->trans_start = jiffies; /**//* 保存时间戳 */
604![]()
/**//*
605
* 因为“发送”完成后,需要释放skb,所以,先要保存它 ,释放都是在网卡发送完成,产生中断,而中断函数收
606
* 到网卡的发送完成的中断信号后释放
607
*/
608
priv->skb = skb;
609![]()
610![]()
/**//*
611
* 让硬件把数据包发送出去,对于物理设备,就是一个读网卡寄存器的过程,不过,这里,只是一些
612
* 为了实现演示功能的虚假的欺骗函数,比如操作源/目的IP,然后调用接收函数(所以,接收时不用调用中断)
613
*/
614
snull_hw_tx(data, len, dev);
615![]()
616![]()
return 0; /**//* Our simple device can not fail */
617
}
618![]()
619![]()
/**//*
620
* 传输超时处理函数
621
* 比如在传输数据时,由于缓冲已满,需要关闭传输队列,但是驱动程序是不能丢弃数据包,它将在“超时”的时候触发
622
* 超时处理函数,这个函数将发送一个“传输中断”,以填补丢失的中断,并重新启动传输队例子
623
*/
624
void snull_tx_timeout (struct net_device *dev)
625![]()
![]()
{
626
struct snull_priv *priv = netdev_priv(dev);
627![]()
628
PDEBUG("Transmit timeout at %ld, latency %ld\n", jiffies,
629
jiffies - dev->trans_start);
630![]()
/**//* Simulate a transmission interrupt to get things moving */
631
priv->status = SNULL_TX_INTR;
632
snull_interrupt(0, dev, NULL);
633
priv->stats.tx_errors++;
634
netif_wake_queue(dev);
635
return;
636
}
637![]()
638![]()
639![]()
640![]()
/**//*
641
* Ioctl 命令
642
*/
643
int snull_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
644![]()
![]()
{
645
PDEBUG("ioctl\n");
646
return 0;
647
}
648![]()
649![]()
/**//*
650
* 获取设备的状态
651
*/
652
struct net_device_stats *snull_stats(struct net_device *dev)
653![]()
![]()
{
654
struct snull_priv *priv = netdev_priv(dev);
655
return &priv->stats;
656
}
657![]()
658![]()
/**//*
659
* 有些网络有硬件地址(比如Ethernet),并且在发送硬件帧时需要知道目的硬件 地址会进行ARP请求/应答,以完成MAC地址解析,
660
* 需要做arp请求的设备在发送之前会调用驱动程序的rebuild_header函数。需要做arp的的设备在发送之前会调用驱动程序的
661
* rebuild_header方 法。调用的主要参数包括指向硬件帧头的指针,协议层地址。如果驱动程序能够解 析硬件地址,就返回1,
662
* 如果不能,返回0。
663
* 当然,作者实现的演示设备中,不支持这个过程。
664
*/
665
int snull_rebuild_header(struct sk_buff *skb)
666![]()
![]()
{
667
struct ethhdr *eth = (struct ethhdr *) skb->data;
668
struct net_device *dev = skb->dev;
669![]()
670
memcpy(eth->h_source, dev->dev_addr, dev->addr_len);
671
memcpy(eth->h_dest, dev->dev_addr, dev->addr_len);
672![]()
eth->h_dest[ETH_ALEN-1] ^= 0x01; /**//* dest is us xor 1 */
673
return 0;
674
}
675![]()
676![]()
/**//*
677
* 为上层协议创建一个二层的以太网首部。
678
* 事实上,如果一开始调用alloc_etherdev分配以太设备,它会调用ether_setup进行初始化,初始化函数会设置:
679
* dev->hard_header = eth_header;
680
* dev->rebuild_header = eth_rebuild_header;
681
* 驱动开发人员并不需要自己来实现这个函数,作者这样做,只是为了展示细节。
682
*/
683![]()
684
int snull_header(struct sk_buff *skb, struct net_device *dev,
685
unsigned short type, void *daddr, void *saddr,
686
unsigned int len)
687![]()
![]()
{
688![]()
/**//*获取以太头指针*/
689
struct ethhdr *eth = (struct ethhdr *)skb_push(skb,ETH_HLEN);
690![]()
691![]()
eth->h_proto = htons(type); /**//*填写协议*/
692![]()
693![]()
/**//*填写来源/目的MAC地址,如果地址为空,则用设备自己的地址代替之*/
694
memcpy(eth->h_source, saddr ? saddr : dev->dev_addr, dev->addr_len);
695
memcpy(eth->h_dest, daddr ? daddr : dev->dev_addr, dev->addr_len);
696![]()
697![]()
/**//*
698
* 将第一个octet设为0,主要是为了可以在不支持组播链路,如ppp链路上运行
699
* PS:作者这样做,仅仅是演示在PC机上的实现,事实上,直接使用ETH_ALEN-1是
700
* 不适合“大头”机器的。
701
*/
702![]()
eth->h_dest[ETH_ALEN-1] ^= 0x01; /**//* dest is us xor 1 */
703
return (dev->hard_header_len);
704
}
705![]()
706![]()
/**//*
707
* 改变设备MTU值.
708
*/
709
int snull_change_mtu(struct net_device *dev, int new_mtu)
710![]()
![]()
{
711
unsigned long flags;
712
struct snull_priv *priv = netdev_priv(dev);
713
spinlock_t *lock = &priv->lock;
714![]()
715![]()
/**//* check ranges */
716
if ((new_mtu < 68) || (new_mtu > 1500))
717
return -EINVAL;
718![]()
/**//*
719
* Do anything you need, and the accept the value
720
*/
721
spin_lock_irqsave(lock, flags);
722
dev->mtu = new_mtu;
723
spin_unlock_irqrestore(lock, flags);
724![]()
return 0; /**//* success */
725
}
726![]()
727![]()
/**//*
728
* 设备初始化函数,它必须在 register_netdev 函数被调用之前调用
729
*/
730
void snull_init(struct net_device *dev)
731![]()
![]()
{
732![]()
/**//*设备的“私有”结构,保存一些设备一些“私有数据”*/
733
struct snull_priv *priv;
734
#if 0
735![]()
/**//*
736
* Make the usual checks: check_region(), probe irq,
-ENODEV
737
* should be returned if no device found. No resource should be
738
* grabbed: this is done on open().
739
*/
740
#endif
741![]()
742![]()
/**//*
743
* 初始化以太网设备的一些共用的成员
744
*/
745![]()
ether_setup(dev); /**//* assign some of the fields */
746![]()
747![]()
/**//*设置设备的许多成员函数指针*/
748
dev->open = snull_open;
749
dev->stop = snull_release;
750
dev->set_config = snull_config;
751
dev->hard_start_xmit = snull_tx;
752
dev->do_ioctl = snull_ioctl;
753
dev->get_stats = snull_stats;
754
dev->change_mtu = snull_change_mtu;
755
dev->rebuild_header = snull_rebuild_header;
756
dev->hard_header = snull_header;
757
dev->tx_timeout = snull_tx_timeout;
758
dev->watchdog_timeo = timeout;
759![]()
760![]()
/**//*如果使用NAPI,设置pool函数*/
761![]()
if (use_napi)
{
762
dev->poll = snull_poll;
763![]()
dev->weight = 2; /**//*weight是接口在资源紧张时,在接口上能承受多大流量的权重*/
764
}
765![]()
/**//* keep the default flags, just add NOARP */
766
dev->flags |= IFF_NOARP;
767
dev->features |= NETIF_F_NO_CSUM;
768![]()
dev->hard_header_cache = NULL; /**//* Disable caching */
769![]()
770![]()
/**//*
771
* 取得私有数据区,并初始化它.
772
*/
773
priv = netdev_priv(dev);
774
memset(priv, 0, sizeof(struct snull_priv));
775
spin_lock_init(&priv->lock);
776![]()
snull_rx_ints(dev, 1); /**//* 打开接收中断标志 */
777![]()
snull_setup_pool(dev); /**//*设置使用NAPI时的接收缓冲池*/
778
}
779![]()
780![]()
/**//*
781
* The devices
782
*/
783![]()
784
struct net_device *snull_devs[2];
785![]()
786![]()
787![]()
788![]()
/**//*
789
* Finally, the module stuff
790
*/
791![]()
792
void snull_cleanup(void)
793![]()
![]()
{
794
int i;
795![]()
796![]()
for (i = 0; i < 2; i++)
{
797![]()
if (snull_devs)
{
798
unregister_netdev(snull_devs);
799
snull_teardown_pool(snull_devs);
800
free_netdev(snull_devs);
801
}
802
}
803
return;
804
}
805![]()
806![]()
/**//*模块初始化,初始化的只有一个工作:分配一个设备结构并注册它*/
807
int snull_init_module(void)
808![]()
![]()
{
809
int result, i, ret = -ENOMEM;
810![]()
811![]()
/**//*中断函数指针,因是否使用NAPI而指向不同的中断函数*/
812
snull_interrupt = use_napi ? snull_napi_interrupt : snull_regular_interrupt;
813![]()
814![]()
/**//*
815
* 分配两个设备,网络设备都是用struct net_device来描述,alloc_netdev分配设备,第三个参数是
816
* 对struct net_device结构成员进行初始化的函数,对于以太网来说,可以把alloc_netdev/snull_init
817
* 两个函数变为一个,alloc_etherdev,它会自动调用以太网的初始化函数ether_setup,因为以太网的初
818
* 始化函数工作都是近乎一样的 */
819
snull_devs[0] = alloc_netdev(sizeof(struct snull_priv), "sn%d",
820
snull_init);
821
snull_devs[1] = alloc_netdev(sizeof(struct snull_priv), "sn%d",
822
snull_init);
823![]()
/**//*分配失败*/
824
if (snull_devs[0] == NULL || snull_devs[1] == NULL)
825
goto out;
826![]()
827
ret = -ENODEV;
828![]()
/**//*向内核注册网络设备,这样,设备就可以被使用了*/
829
for (i = 0; i < 2; i++)
830
if ((result = register_netdev(snull_devs)))
831
printk("snull: error %i registering device \"%s\"\n",
832
result, snull_devs->name);
833
else
834
ret = 0;
835
out:
836
if (ret)
837
snull_cleanup();
838
return ret;
839
}
840![]()
841![]()
842
module_init(snull_init_module);
843
module_exit(snull_cleanup);
《Linux设备驱劝程序第三版》网卡驱动的范例,讲述了网卡驱动编写的一般方法,脱离了实际硬件的束缚,是一个入门的好例子,在读懂了这个例子,再补充:
1、PCI驱动方面的知识;
2、硬件读写控制方面的知识;
就可以去阅读实际的网卡驱动范例了。幸运的是,《Linux设备驱劝程序》这些方面的知识讲解还是非常到位的。以下是九贱读完这个范例代码的笔记,以做阅读本章内容的补充:
CODE:
1

/**//*2
* snull.c -- the Simple Network Utility3
*4
* Copyright (C) 2001 Alessandro Rubini and Jonathan Corbet5
* Copyright (C) 2001 O'Reilly & Associates6
*7
* The source code in this file can be freely used, adapted,8
* and redistributed in source or binary form, so long as an9
* acknowledgment appears in derived source files. The citation10
* should list that the code comes from the book "Linux Device11
* Drivers" by Alessandro Rubini and Jonathan Corbet, published12
* by O'Reilly & Associates. No warranty is attached;13
* we cannot take responsibility for errors or fitness for use.14
*15
* $Id: snull.c,v 1.21 2004/11/05 02:36:03 rubini Exp $16
*/17

18
#include <linux/config.h>19
#include <linux/module.h>20
#include <linux/init.h>21
#include <linux/moduleparam.h>22

23
#include <linux/sched.h>24

#include <linux/kernel.h> /**//* printk() */25

#include <linux/slab.h> /**//* kmalloc() */26

#include <linux/errno.h> /**//* error codes */27

#include <linux/types.h> /**//* size_t */28

#include <linux/interrupt.h> /**//* mark_bh */29

30
#include <linux/in.h>31

#include <linux/netdevice.h> /**//* struct device, and other headers */32

#include <linux/etherdevice.h> /**//* eth_type_trans */33

#include <linux/ip.h> /**//* struct iphdr */34

#include <linux/tcp.h> /**//* struct tcphdr */35
#include <linux/skbuff.h>36

37
#include "snull.h"38

39
#include <linux/in6.h>40
#include <asm/checksum.h>41

42
MODULE_AUTHOR("Alessandro Rubini, Jonathan Corbet");43
MODULE_LICENSE("Dual BSD/GPL");44

45

46

/**//*47
* Transmitter lockup simulation, normally disabled.48
*/49
static int lockup = 0;50
module_param(lockup, int, 0);51

52
static int timeout = SNULL_TIMEOUT;53
module_param(timeout, int, 0);54

55

/**//*56
* Do we run in NAPI mode?57
*/58
static int use_napi = 0;59
module_param(use_napi, int, 0);60

61

62

/**//*63
* A structure representing an in-flight packet.64
*/65

struct snull_packet
{66
struct snull_packet *next;67
struct net_device *dev;68
int datalen;69
u8 data[ETH_DATA_LEN];70
};71

72
int pool_size = 8;73
module_param(pool_size, int, 0);74

75

/**//*76
* This structure is private to each device. It is used to pass77
* packets in and out, so there is place for a packet78
*/79

80

struct snull_priv
{81
struct net_device_stats stats;82
int status;83
struct snull_packet *ppool;84

struct snull_packet *rx_queue; /**//* List of incoming packets */85
int rx_int_enabled;86
int tx_packetlen;87
u8 *tx_packetdata;88
struct sk_buff *skb;89
spinlock_t lock;90
};91

92
static void snull_tx_timeout(struct net_device *dev);93
static void (*snull_interrupt)(int, void *, struct pt_regs *);94

95

/**//*96
* 设置设备的包缓冲池.97
* 当需要使用NAPI,而非中断处理的时候,设备需要能够保存多个数据包的能力,这个保存所需的缓存,98
* 或者在板卡上,或者在内核的DMA环中。99
* 作者这里的演示程序,根据pool_size的大小,分配pool_size个大小为struct snull_packet的缓冲区,100
* 这个缓冲池用链表组织,“私有数据”结构的ppool成员指针指向链表首部。101
*/102
void snull_setup_pool(struct net_device *dev)103


{104
struct snull_priv *priv = netdev_priv(dev);105
int i;106
struct snull_packet *pkt;107

108
priv->ppool = NULL;109

for (i = 0; i < pool_size; i++)
{110
pkt = kmalloc (sizeof (struct snull_packet), GFP_KERNEL);111

if (pkt == NULL)
{112
printk (KERN_NOTICE "Ran out of memory allocating packet pool\n");113
return;114
}115
pkt->dev = dev;116
pkt->next = priv->ppool;117
priv->ppool = pkt;118
}119
}120

121

/**//*因为snull_setup_pool分配了pool_size个struct snull_packet,所以,驱动退出时,需要释放内存*/122
void snull_teardown_pool(struct net_device *dev)123


{124
struct snull_priv *priv = netdev_priv(dev);125
struct snull_packet *pkt;126

127

while ((pkt = priv->ppool))
{128
priv->ppool = pkt->next;129
kfree (pkt);130

/**//* FIXME - in-flight packets ? */131
}132
} 133

134

/**//*135
* 获取设备要传输的第一个包,传输队列首部相应的移动到下一个数据包.136
*/137
struct snull_packet *snull_get_tx_buffer(struct net_device *dev)138


{139
struct snull_priv *priv = netdev_priv(dev);140
unsigned long flags;141
struct snull_packet *pkt;142

143
spin_lock_irqsave(&priv->lock, flags);144
pkt = priv->ppool;145
priv->ppool = pkt->next;146

if (priv->ppool == NULL)
{147
printk (KERN_INFO "Pool empty\n");148
netif_stop_queue(dev);149
}150
spin_unlock_irqrestore(&priv->lock, flags);151
return pkt;152
}153

154

/**//*将包缓存交还给缓存池*/155
void snull_release_buffer(struct snull_packet *pkt)156


{157
unsigned long flags;158
struct snull_priv *priv = netdev_priv(pkt->dev);159

160
spin_lock_irqsave(&priv->lock, flags);161
pkt->next = priv->ppool;162
priv->ppool = pkt;163
spin_unlock_irqrestore(&priv->lock, flags);164
if (netif_queue_stopped(pkt->dev) && pkt->next == NULL)165
netif_wake_queue(pkt->dev);166
}167

168

/**//*将要传输的包加入到设备dev的传输队列首部,当然,这只是一个演示,这样一来,就变成先进先出了*/169
void snull_enqueue_buf(struct net_device *dev, struct snull_packet *pkt)170


{171
unsigned long flags;172
struct snull_priv *priv = netdev_priv(dev);173

174
spin_lock_irqsave(&priv->lock, flags);175

pkt->next = priv->rx_queue; /**//* FIXME - misorders packets */176
priv->rx_queue = pkt;177
spin_unlock_irqrestore(&priv->lock, flags);178
}179

180

/**//*取得传输队列中的第一个数据包*/181
struct snull_packet *snull_dequeue_buf(struct net_device *dev)182


{183
struct snull_priv *priv = netdev_priv(dev);184
struct snull_packet *pkt;185
unsigned long flags;186

187
spin_lock_irqsave(&priv->lock, flags);188
pkt = priv->rx_queue;189
if (pkt != NULL)190
priv->rx_queue = pkt->next;191
spin_unlock_irqrestore(&priv->lock, flags);192
return pkt;193
}194

195

/**//*196
* 打开/关闭接收中断.197
*/198
static void snull_rx_ints(struct net_device *dev, int enable)199


{200
struct snull_priv *priv = netdev_priv(dev);201
priv->rx_int_enabled = enable;202
}203

204

205

/**//*206
* 设备打开函数,是驱动最重要的函数之一,它应该注册所有的系统资源(I/O端口,IRQ、DMA等等),207
* 并对设备执行其他所需的设置。208
* 因为这个例子中,并没有真正的物理设备,所以,它最重要的工作就是启动传输队列。209
*/210

211
int snull_open(struct net_device *dev)212


{213

/**//* request_region(), request_irq(),
. (like fops->open) */214

215

/**//* 216
* Assign the hardware address of the board: use "\0SNULx", where217
* x is 0 or 1. The first byte is '\0' to avoid being a multicast218
* address (the first byte of multicast addrs is odd).219
*/220
memcpy(dev->dev_addr, "\0SNUL0", ETH_ALEN);221
if (dev == snull_devs[1])222

dev->dev_addr[ETH_ALEN-1]++; /**//* \0SNUL1 */223
netif_start_queue(dev);224
return 0;225
}226

227

/**//*设备停止函数,这里的工作就是停止传输队列*/228
int snull_release(struct net_device *dev)229


{230

/**//* release ports, irq and such -- like fops->close */231

232

netif_stop_queue(dev); /**//* can't transmit any more */233
return 0;234
}235

236

/**//*237
* 当用户调用ioctl时类型为SIOCSIFMAP时,如使用ifconfig,系统会调用驱动程序的set_config 方法。238
* 用户会传递一个ifmap结构包含需要设置的I/O地址、中断等参数。239
*/240
int snull_config(struct net_device *dev, struct ifmap *map)241


{242

if (dev->flags & IFF_UP) /**//* 不能设置一个正在运行状态的设备 */243
return -EBUSY;244

245

/**//* 这个例子中,不允许改变 I/O 地址*/246

if (map->base_addr != dev->base_addr)
{247
printk(KERN_WARNING "snull: Can't change I/O address\n");248
return -EOPNOTSUPP;249
}250

251

/**//* 允许改变 IRQ */252

if (map->irq != dev->irq)
{253
dev->irq = map->irq;254

/**//* request_irq() is delayed to open-time */255
}256

257

/**//* ignore other fields */258
return 0;259
}260

261

/**//*262
* 接收数据包函数263
* 它被“接收中断”调用,重组数据包,并调用函数netif_rx进一步处理。264
* 我们从“硬件”中收到的包,是用struct snull_packet来描述的,但是内核中描述一个包,是使用265
* struct sk_buff(简称skb),所以,这里要完成一个把硬件接收的包拷贝至内核缓存skb的一个266
* 组包过程(PS:不知在接收之前直接分配一个skb,省去这一步,会如何提高性能,没有研究过,见笑了^o^)。267
*/268
void snull_rx(struct net_device *dev, struct snull_packet *pkt)269


{270
struct sk_buff *skb;271
struct snull_priv *priv = netdev_priv(dev);272

273

/**//*274
* 分配skb缓存275
*/276
skb = dev_alloc_skb(pkt->datalen + 2);277

if (!skb)
{ /**//*分配失败*/278
if (printk_ratelimit())279
printk(KERN_NOTICE "snull rx: low on mem - packet dropped\n");280
priv->stats.rx_dropped++;281
goto out;282
}283

/**//*284
* skb_reserver用来增加skb的date和tail,因为以太网头部为14字节长,再补上两个字节就刚好16字节边界285
* 对齐,所以大多数以太网设备都会在数据包之前保留2个字节。286
*/287

skb_reserve(skb, 2); /**//* align IP on 16B boundary */ 288
memcpy(skb_put(skb, pkt->datalen), pkt->data, pkt->datalen);289

290

skb->dev = dev; /**//*skb与接收设备就关联起来了,它在网络栈中会被广泛使用,没道理不知道数据是谁接收来的吧*/291

skb->protocol = eth_type_trans(skb, dev); /**//*获取上层协议类型,这样,上层处理函数才知道如何进一步处理*/292

skb->ip_summed = CHECKSUM_UNNECESSARY; /**//* 设置较验标志:不进行任何校验,作者的驱动的收发都在内存中进行,是没有必要进行校验*/293

294

/**//*累加计数器*/295
priv->stats.rx_packets++;296
priv->stats.rx_bytes += pkt->datalen;297

298

/**//*299
* 把数据包交给上层。netif_rx会逐步调用netif_rx_schedule -->__netif_rx_schedule,300
* __netif_rx_schedule函数会调用__raise_softirq_irqoff(NET_RX_SOFTIRQ);触发网络接收数据包的软中断函数net_rx_action。301
* 软中断是Linux内核完成中断推后处理工作的一种机制,请参考《Linux内核设计与实现》第二版。302
* 唯一需要提及的是,这个软中断函数net_rx_action是在网络系统初始化的时候(linux/net/core/dev.c):注册的303
* open_softirq(NET_RX_SOFTIRQ, net_rx_action, NULL);304
*/305
netif_rx(skb);306
out:307
return;308
}309

310

311

/**//*312
* NAPI 的poll轮询函数.313
*/314
static int snull_poll(struct net_device *dev, int *budget)315


{316

/**//*317
* dev->quota是当前CPU能够从所有接口中接收数据包的最大数目,budget是在318
* 初始化阶段分配给接口的weight值,轮询函数必须接受二者之间的最小值。表示319
* 轮询函数本次要处理的数据包个数。320
*/321
int npackets = 0, quota = min(dev->quota, *budget);322
struct sk_buff *skb;323
struct snull_priv *priv = netdev_priv(dev);324
struct snull_packet *pkt;325

326

/**//*这个循环次数由要处理的数据包个数,并且,以处理完接收队列为上限*/327

while (npackets < quota && priv->rx_queue)
{328

/**//*从队列中取出数据包*/329
pkt = snull_dequeue_buf(dev);330

331

/**//*接下来的处理,和传统中断事实上是一样的*/332
skb = dev_alloc_skb(pkt->datalen + 2);333

if (! skb)
{334
if (printk_ratelimit())335
printk(KERN_NOTICE "snull: packet dropped\n");336
priv->stats.rx_dropped++;337
snull_release_buffer(pkt);338
continue;339
}340

skb_reserve(skb, 2); /**//* align IP on 16B boundary */ 341
memcpy(skb_put(skb, pkt->datalen), pkt->data, pkt->datalen);342
skb->dev = dev;343
skb->protocol = eth_type_trans(skb, dev);344

skb->ip_summed = CHECKSUM_UNNECESSARY; /**//* don't check it */345

346

/**//*需要调用netif_receive_skb而不是net_rx将包交给上层协议栈*/347
netif_receive_skb(skb);348

349

/**//*累加计数器 */350
npackets++;351
priv->stats.rx_packets++;352
priv->stats.rx_bytes += pkt->datalen;353
snull_release_buffer(pkt);354
}355

/**//* If we processed all packets, we're done; tell the kernel and reenable ints */356
*budget -= npackets;357
dev->quota -= npackets;358

359
//360

if (! priv->rx_queue)
{361
netif_rx_complete(dev);362
snull_rx_ints(dev, 1);363
return 0;364
}365

/**//* We couldn't process everything. */366
return 1;367
}368

369

370

/**//*371
* 设备的中断函数,当需要发/收数据,出现错误,连接状态变化等,它会被触发372
* 对于典型的网络设备,一般会在open函数中注册中断函数,这样,当网络设备产生中断时,如接收到数据包时,373
* 中断函数将会被调用。不过在这个例子中,因为没有真正的物理设备,所以,不存在注册中断,也就不存在触374
* 发,对于接收和发送,它都是在自己设计的函数的特定位置被调用。375
* 这个中断函数设计得很简单,就是取得设备的状态,判断是“接收”还是“发送”的中断,以调用相应的处理函数。376
* 而对于,“是哪个设备产生的中断”这个问题,则由调用它的函数通过第二个参数的赋值来决定。377
*/378
static void snull_regular_interrupt(int irq, void *dev_id, struct pt_regs *regs)379


{380
int statusword;381
struct snull_priv *priv;382
struct snull_packet *pkt = NULL;383

/**//*384
* 通常,需要检查 "device" 指针以确保这个中断是发送给自己的。385
* 然后为 "struct device *dev" 赋386
*/387
struct net_device *dev = (struct net_device *)dev_id;388

389

/**//* paranoid */390
if (!dev)391
return;392

393

/**//* 锁住设备 */394
priv = netdev_priv(dev);395
spin_lock(&priv->lock);396

397

/**//* 取得设备状态指字,对于真实设备,使用I/O指令,比如:int txsr = inb(TX_STATUS); */398
statusword = priv->status;399
priv->status = 0;400

if (statusword & SNULL_RX_INTR)
{ /**//*如果是接收数据包的中断*/401

/**//* send it to snull_rx for handling */402
pkt = priv->rx_queue;403

if (pkt)
{404
priv->rx_queue = pkt->next;405
snull_rx(dev, pkt);406
}407
}408

if (statusword & SNULL_TX_INTR)
{ /**//*如果是发送数据包的中断*/409

/**//* a transmission is over: free the skb */410
priv->stats.tx_packets++;411
priv->stats.tx_bytes += priv->tx_packetlen;412
dev_kfree_skb(priv->skb);413
}414

415

/**//* 释放锁 */416
spin_unlock(&priv->lock);417

418

/**//*释放缓冲区*/419

if (pkt) snull_release_buffer(pkt); /**//* Do this outside the lock! */420
return;421
}422

423

/**//*424
* A NAPI interrupt handler.425
* 在设备初始化的时候,poll指向指向了snull_poll函数,所以,NAPI中断处理函数很简单,426
* 当“接收中断”到达的时候,它就屏蔽此中断,然后netif_rx_schedule函数接收,接收函数427
* 会在未来某一时刻调用注册的snull_poll函数实现轮询,当然,对于“传输中断”,处理方法428
* 同传统中断处理并无二致。429
*/430
static void snull_napi_interrupt(int irq, void *dev_id, struct pt_regs *regs)431


{432
int statusword;433
struct snull_priv *priv;434

435

/**//*436
* As usual, check the "device" pointer for shared handlers.437
* Then assign "struct device *dev"438
*/439
struct net_device *dev = (struct net_device *)dev_id;440

/**//*
and check with hw if it's really ours */441

442

/**//* paranoid */443
if (!dev)444
return;445

446

/**//* Lock the device */447
priv = netdev_priv(dev);448
spin_lock(&priv->lock);449

450

/**//* retrieve statusword: real netdevices use I/O instructions */451
statusword = priv->status;452
priv->status = 0;453

454

/**//*455
* 唯一的区别就在这里,它先屏蔽掉接收中断,然后调用netif_rx_schedule,而不是netif_rx456
* 重点还是在于poll函数的设计。457
*/458

if (statusword & SNULL_RX_INTR)
{459

snull_rx_ints(dev, 0); /**//* Disable further interrupts */460
netif_rx_schedule(dev);461
}462

if (statusword & SNULL_TX_INTR)
{463

/**//* a transmission is over: free the skb */464
priv->stats.tx_packets++;465
priv->stats.tx_bytes += priv->tx_packetlen;466
dev_kfree_skb(priv->skb);467
}468

469

/**//* Unlock the device and we are done */470
spin_unlock(&priv->lock);471
return;472
}473

474

475

476

/**//*477
* Transmit a packet (low level interface)478
*/479
static void snull_hw_tx(char *buf, int len, struct net_device *dev)480


{481

/**//*482
* This function deals with hw details. This interface loops483
* back the packet to the other snull interface (if any).484
* In other words, this function implements the snull behaviour,485
* while all other procedures are rather device-independent486
*/487
struct iphdr *ih;488
struct net_device *dest;489
struct snull_priv *priv;490
u32 *saddr, *daddr;491
struct snull_packet *tx_buffer;492

493

/**//* I am paranoid. Ain't I? */494

if (len < sizeof(struct ethhdr) + sizeof(struct iphdr))
{495
printk("snull: Hmm
packet too short (%i octets)\n",496
len);497
return;498
}499

500

if (0)
{ /**//* enable this conditional to look at the data */501
int i;502
PDEBUG("len is %i\n" KERN_DEBUG "data:",len);503
for (i=14 ; i<len; i++)504
printk(" %02x",buf&0xff);505
printk("\n");506
}507

/**//*508
* 取得来源IP和目的IP地址509
*/510
ih = (struct iphdr *)(buf+sizeof(struct ethhdr));511
saddr = &ih->saddr;512
daddr = &ih->daddr;513

514

/**//*515
* 这里做了三个调换,以实现欺骗:来源地址第三octet 0<->1,目的地址第三octet 0<->1,设备snX编辑0<->1,这样做的理由是:516
* sn0(发):192.168.0.88 --> 192.168.0.99 做了调换后,就变成:517
* sn1(收):192.168.1.88 --> 192.168.1.99 因为sn1的地址就是192.168.1.99,所以,它收到这个包后,会回应:518
* sn1(发):192.168.1.99 --> 192.168.1.88 ,同样地,做了这样的调换后,就变成:519
* sn0(收):192.168.0.99 --> 192.168.0.88 这样,sn0就会收到这个包,实现了ping的请求与应答,^o^520
*/521

((u8 *)saddr)[2] ^= 1; /**//* change the third octet (class C) */522
((u8 *)daddr)[2] ^= 1;523

524

/**//*重新计算较验和*/525

ih->check = 0; /**//* and rebuild the checksum (ip needs it) */526
ih->check = ip_fast_csum((unsigned char *)ih,ih->ihl);527

528

/**//*输出调试信息*/529
if (dev == snull_devs[0])530
PDEBUGG("%08x:%05i --> %08x:%05i\n",531
ntohl(ih->saddr),ntohs(((struct tcphdr *)(ih+1))->source),532
ntohl(ih->daddr),ntohs(((struct tcphdr *)(ih+1))->dest));533
else534
PDEBUGG("%08x:%05i <-- %08x:%05i\n",535
ntohl(ih->daddr),ntohs(((struct tcphdr *)(ih+1))->dest),536
ntohl(ih->saddr),ntohs(((struct tcphdr *)(ih+1))->source));537

538

/**//*调换设备编号,即dest指向接收设备,原因如前所述*/539
dest = snull_devs[dev == snull_devs[0] ? 1 : 0];540

541

/**//*将发送的数据添加到接收设备的接收队列中*/542
priv = netdev_priv(dest);543
tx_buffer = snull_get_tx_buffer(dev);544
tx_buffer->datalen = len;545
memcpy(tx_buffer->data, buf, len);546
snull_enqueue_buf(dest, tx_buffer);547

548

/**//*549
* 如果设备接收标志打开,就调用中断函数把数据包发送给目标设备——即触发目的设备的接收中断,这样550
* 中断程序就会自接收设备的接收队列中接收数据包,并交给上层网络栈处理551
*/552

if (priv->rx_int_enabled)
{553
priv->status |= SNULL_RX_INTR;554
snull_interrupt(0, dest, NULL);555
}556

557

/**//*发送完成后,触发“发送完成”中断*/558
priv = netdev_priv(dev);559
priv->tx_packetlen = len;560
priv->tx_packetdata = buf;561
priv->status |= SNULL_TX_INTR;562

563

/**//*564
* 如果insmod驱动的时候,指定了模拟硬件锁的lockup=n,则在会传输n个数据包后,模拟一次硬件锁住的情况,565
* 这是通过调用netif_stop_queue函数来停止传输队列,标记“设备不能再传输数据包”实现的,它将在传输的超566
* 时函数中,调用netif_wake_queue函数来重新启动传输队例,同时超时函数中会再次调用“接收中断”,这样567
* stats.tx_packets累加,又可以重新传输新的数据包了(参接收中断和超时处理函数的实现)。568
*/569

if (lockup && ((priv->stats.tx_packets + 1) % lockup) == 0)
{570

/**//* Simulate a dropped transmit interrupt */571

netif_stop_queue(dev); /**//*停止数据包的传输*/572
PDEBUG("Simulate lockup at %ld, txp %ld\n", jiffies,573
(unsigned long) priv->stats.tx_packets);574
}575
else576

/**//*发送完成后,触发中断,中断函数发现发送完成,就累加计数器,释放skb缓存*/577
snull_interrupt(0, dev, NULL);578

579

/**//*580
* 看到这里,我们可以看到,这个发送函数其实并没有把数据包通过I/O指令发送给硬件,而仅仅是做了一个地址/设备的调换,581
* 并把数据包加入到接收设备的队例当中。582
*/ 583
}584

585

/**//*586
* 数据包传输函数,Linux网络堆栈,在发送数据包时,会调用驱动程序的hard_start_transmit函数,587
* 在设备初始化的时候,这个函数指针指向了snull_tx。588
*/589
int snull_tx(struct sk_buff *skb, struct net_device *dev)590


{591
int len;592
char *data, shortpkt[ETH_ZLEN];593
struct snull_priv *priv = netdev_priv(dev);594

595
data = skb->data;596
len = skb->len;597

if (len < ETH_ZLEN)
{ /**//*处理短帧的情况,如果小于以太帧最小长度,不足位全部补0*/598
memset(shortpkt, 0, ETH_ZLEN);599
memcpy(shortpkt, skb->data, skb->len);600
len = ETH_ZLEN;601
data = shortpkt;602
}603

dev->trans_start = jiffies; /**//* 保存时间戳 */604

/**//* 605
* 因为“发送”完成后,需要释放skb,所以,先要保存它 ,释放都是在网卡发送完成,产生中断,而中断函数收606
* 到网卡的发送完成的中断信号后释放607
*/608
priv->skb = skb;609

610

/**//* 611
* 让硬件把数据包发送出去,对于物理设备,就是一个读网卡寄存器的过程,不过,这里,只是一些612
* 为了实现演示功能的虚假的欺骗函数,比如操作源/目的IP,然后调用接收函数(所以,接收时不用调用中断)613
*/614
snull_hw_tx(data, len, dev);615

616

return 0; /**//* Our simple device can not fail */617
}618

619

/**//*620
* 传输超时处理函数621
* 比如在传输数据时,由于缓冲已满,需要关闭传输队列,但是驱动程序是不能丢弃数据包,它将在“超时”的时候触发622
* 超时处理函数,这个函数将发送一个“传输中断”,以填补丢失的中断,并重新启动传输队例子623
*/624
void snull_tx_timeout (struct net_device *dev)625


{626
struct snull_priv *priv = netdev_priv(dev);627

628
PDEBUG("Transmit timeout at %ld, latency %ld\n", jiffies,629
jiffies - dev->trans_start);630

/**//* Simulate a transmission interrupt to get things moving */631
priv->status = SNULL_TX_INTR;632
snull_interrupt(0, dev, NULL);633
priv->stats.tx_errors++;634
netif_wake_queue(dev);635
return;636
}637

638

639

640

/**//*641
* Ioctl 命令642
*/643
int snull_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)644


{645
PDEBUG("ioctl\n");646
return 0;647
}648

649

/**//*650
* 获取设备的状态651
*/652
struct net_device_stats *snull_stats(struct net_device *dev)653


{654
struct snull_priv *priv = netdev_priv(dev);655
return &priv->stats;656
}657

658

/**//*659
* 有些网络有硬件地址(比如Ethernet),并且在发送硬件帧时需要知道目的硬件 地址会进行ARP请求/应答,以完成MAC地址解析,660
* 需要做arp请求的设备在发送之前会调用驱动程序的rebuild_header函数。需要做arp的的设备在发送之前会调用驱动程序的661
* rebuild_header方 法。调用的主要参数包括指向硬件帧头的指针,协议层地址。如果驱动程序能够解 析硬件地址,就返回1,662
* 如果不能,返回0。663
* 当然,作者实现的演示设备中,不支持这个过程。 664
*/665
int snull_rebuild_header(struct sk_buff *skb)666


{667
struct ethhdr *eth = (struct ethhdr *) skb->data;668
struct net_device *dev = skb->dev;669

670
memcpy(eth->h_source, dev->dev_addr, dev->addr_len);671
memcpy(eth->h_dest, dev->dev_addr, dev->addr_len);672

eth->h_dest[ETH_ALEN-1] ^= 0x01; /**//* dest is us xor 1 */673
return 0;674
}675

676

/**//*677
* 为上层协议创建一个二层的以太网首部。678
* 事实上,如果一开始调用alloc_etherdev分配以太设备,它会调用ether_setup进行初始化,初始化函数会设置:679
* dev->hard_header = eth_header;680
* dev->rebuild_header = eth_rebuild_header;681
* 驱动开发人员并不需要自己来实现这个函数,作者这样做,只是为了展示细节。682
*/683

684
int snull_header(struct sk_buff *skb, struct net_device *dev,685
unsigned short type, void *daddr, void *saddr,686
unsigned int len)687


{688

/**//*获取以太头指针*/689
struct ethhdr *eth = (struct ethhdr *)skb_push(skb,ETH_HLEN);690

691

eth->h_proto = htons(type); /**//*填写协议*/692

693

/**//*填写来源/目的MAC地址,如果地址为空,则用设备自己的地址代替之*/694
memcpy(eth->h_source, saddr ? saddr : dev->dev_addr, dev->addr_len);695
memcpy(eth->h_dest, daddr ? daddr : dev->dev_addr, dev->addr_len);696

697

/**//*698
* 将第一个octet设为0,主要是为了可以在不支持组播链路,如ppp链路上运行699
* PS:作者这样做,仅仅是演示在PC机上的实现,事实上,直接使用ETH_ALEN-1是700
* 不适合“大头”机器的。701
*/702

eth->h_dest[ETH_ALEN-1] ^= 0x01; /**//* dest is us xor 1 */703
return (dev->hard_header_len);704
}705

706

/**//*707
* 改变设备MTU值.708
*/709
int snull_change_mtu(struct net_device *dev, int new_mtu)710


{711
unsigned long flags;712
struct snull_priv *priv = netdev_priv(dev);713
spinlock_t *lock = &priv->lock;714

715

/**//* check ranges */716
if ((new_mtu < 68) || (new_mtu > 1500))717
return -EINVAL;718

/**//*719
* Do anything you need, and the accept the value720
*/721
spin_lock_irqsave(lock, flags);722
dev->mtu = new_mtu;723
spin_unlock_irqrestore(lock, flags);724

return 0; /**//* success */725
}726

727

/**//*728
* 设备初始化函数,它必须在 register_netdev 函数被调用之前调用729
*/730
void snull_init(struct net_device *dev)731


{732

/**//*设备的“私有”结构,保存一些设备一些“私有数据”*/733
struct snull_priv *priv;734
#if 0735

/**//*736
* Make the usual checks: check_region(), probe irq,
-ENODEV737
* should be returned if no device found. No resource should be738
* grabbed: this is done on open(). 739
*/740
#endif741

742

/**//* 743
* 初始化以太网设备的一些共用的成员744
*/745

ether_setup(dev); /**//* assign some of the fields */746

747

/**//*设置设备的许多成员函数指针*/748
dev->open = snull_open;749
dev->stop = snull_release;750
dev->set_config = snull_config;751
dev->hard_start_xmit = snull_tx;752
dev->do_ioctl = snull_ioctl;753
dev->get_stats = snull_stats;754
dev->change_mtu = snull_change_mtu; 755
dev->rebuild_header = snull_rebuild_header;756
dev->hard_header = snull_header;757
dev->tx_timeout = snull_tx_timeout;758
dev->watchdog_timeo = timeout;759

760

/**//*如果使用NAPI,设置pool函数*/761

if (use_napi)
{762
dev->poll = snull_poll;763

dev->weight = 2; /**//*weight是接口在资源紧张时,在接口上能承受多大流量的权重*/764
}765

/**//* keep the default flags, just add NOARP */766
dev->flags |= IFF_NOARP;767
dev->features |= NETIF_F_NO_CSUM;768

dev->hard_header_cache = NULL; /**//* Disable caching */769

770

/**//*771
* 取得私有数据区,并初始化它.772
*/773
priv = netdev_priv(dev);774
memset(priv, 0, sizeof(struct snull_priv));775
spin_lock_init(&priv->lock);776

snull_rx_ints(dev, 1); /**//* 打开接收中断标志 */777

snull_setup_pool(dev); /**//*设置使用NAPI时的接收缓冲池*/778
}779

780

/**//*781
* The devices782
*/783

784
struct net_device *snull_devs[2];785

786

787

788

/**//*789
* Finally, the module stuff790
*/791

792
void snull_cleanup(void)793


{794
int i;795

796

for (i = 0; i < 2; i++)
{797

if (snull_devs)
{798
unregister_netdev(snull_devs);799
snull_teardown_pool(snull_devs);800
free_netdev(snull_devs);801
}802
}803
return;804
}805

806

/**//*模块初始化,初始化的只有一个工作:分配一个设备结构并注册它*/807
int snull_init_module(void)808


{809
int result, i, ret = -ENOMEM;810

811

/**//*中断函数指针,因是否使用NAPI而指向不同的中断函数*/812
snull_interrupt = use_napi ? snull_napi_interrupt : snull_regular_interrupt;813

814

/**//* 815
* 分配两个设备,网络设备都是用struct net_device来描述,alloc_netdev分配设备,第三个参数是816
* 对struct net_device结构成员进行初始化的函数,对于以太网来说,可以把alloc_netdev/snull_init817
* 两个函数变为一个,alloc_etherdev,它会自动调用以太网的初始化函数ether_setup,因为以太网的初818
* 始化函数工作都是近乎一样的 */819
snull_devs[0] = alloc_netdev(sizeof(struct snull_priv), "sn%d",820
snull_init);821
snull_devs[1] = alloc_netdev(sizeof(struct snull_priv), "sn%d",822
snull_init);823

/**//*分配失败*/824
if (snull_devs[0] == NULL || snull_devs[1] == NULL)825
goto out;826

827
ret = -ENODEV;828

/**//*向内核注册网络设备,这样,设备就可以被使用了*/829
for (i = 0; i < 2; i++)830
if ((result = register_netdev(snull_devs)))831
printk("snull: error %i registering device \"%s\"\n",832
result, snull_devs->name);833
else834
ret = 0;835
out:836
if (ret) 837
snull_cleanup();838
return ret;839
}840

841

842
module_init(snull_init_module);843
module_exit(snull_cleanup);
浙公网安备 33010602011771号