Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1216647
  • 博文数量: 404
  • 博客积分: 10011
  • 博客等级: 上将
  • 技术积分: 5382
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-03 16:29
文章存档

2010年(40)

2009年(140)

2008年(224)

我的朋友

分类: LINUX

2008-09-26 11:39:11

/*
* 当用户调用ioctl时类型为SIOCSIFMAP时,如使用ifconfig,系统会调用驱动程序的set_config 方法。
* 用户会传递一个ifmap结构包含需要设置的I/O地址、中断等参数。
*/
int snull_config(struct net_device *dev, struct ifmap *map)
{
if (dev->flags & IFF_UP) /* 不能设置一个正在运行状态的设备 */
return -EBUSY;

/* 这个例子中,不允许改变 I/O 地址*/
if (map->base_addr != dev->base_addr) {
printk(KERN_WARNING "snull: Can't change I/O address\n");
return -EOPNOTSUPP;
}

/* 允许改变 IRQ */
if (map->irq != dev->irq) {
dev->irq = map->irq;
/* request_irq() is delayed to open-time */
}

/* ignore other fields */
return 0;
}

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

/*
* 分配skb缓存
*/
skb = dev_alloc_skb(pkt->datalen + 2);
if (!skb) { /*分配失败*/
if (printk_ratelimit())
printk(KERN_NOTICE "snull rx: low on mem - packet dropped\n");
priv->stats.rx_dropped++;
goto out;
}
/*
* skb_reserver用来增加skb的date和tail,因为以太网头部为14字节长,再补上两个字节就刚好16字节边界
* 对齐,所以大多数以太网设备都会在数据包之前保留2个字节。
*/
skb_reserve(skb, 2); /* align IP on 16B boundary */
memcpy(skb_put(skb, pkt->datalen), pkt->data, pkt->datalen);

skb->dev = dev; /*skb与接收设备就关联起来了,它在网络栈中会被广泛使用,没道理不知道数据是谁接收来的吧*/
skb->protocol = eth_type_trans(skb, dev); /*获取上层协议类型,这样,上层处理函数才知道如何进一步处理*/
skb->ip_summed = CHECKSUM_UNNECESSARY; /* 设置较验标志:不进行任何校验,作者的驱动的收发都在内存中进行,是没有必要进行校验*/

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

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


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

/*这个循环次数由要处理的数据包个数,并且,以处理完接收队列为上限*/
while (npackets < quota && priv->rx_queue) {
/*从队列中取出数据包*/
pkt = snull_dequeue_buf(dev);

/*接下来的处理,和传统中断事实上是一样的*/
skb = dev_alloc_skb(pkt->datalen + 2);
if (! skb) {
if (printk_ratelimit())
printk(KERN_NOTICE "snull: packet dropped\n");
priv->stats.rx_dropped++;
snull_release_buffer(pkt);
continue;
}
skb_reserve(skb, 2); /* align IP on 16B boundary */
memcpy(skb_put(skb, pkt->datalen), pkt->data, pkt->datalen);
skb->dev = dev;
skb->protocol = eth_type_trans(skb, dev);
skb->ip_summed = CHECKSUM_UNNECESSARY; /* don't check it */

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

/*累加计数器 */
npackets++;
priv->stats.rx_packets++;
priv->stats.rx_bytes += pkt->datalen;
snull_release_buffer(pkt);
}
/* If we processed all packets, we're done; tell the kernel and reenable ints */
*budget -= npackets;
dev->quota -= npackets;

//
if (! priv->rx_queue) {
netif_rx_complete(dev);
snull_rx_ints(dev, 1);
return 0;
}
/* We couldn't process everything. */
return 1;
}


/*
* 设备的中断函数,当需要发/收数据,出现错误,连接状态变化等,它会被触发
* 对于典型的网络设备,一般会在open函数中注册中断函数,这样,当网络设备产生中断时,如接收到数据包时,
* 中断函数将会被调用。不过在这个例子中,因为没有真正的物理设备,所以,不存在注册中断,也就不存在触
* 发,对于接收和发送,它都是在自己设计的函数的特定位置被调用。
* 这个中断函数设计得很简单,就是取得设备的状态,判断是“接收”还是“发送”的中断,以调用相应的处理函数。
* 而对于,“是哪个设备产生的中断”这个问题,则由调用它的函数通过第二个参数的赋值来决定。
*/
static void snull_regular_interrupt(int irq, void *dev_id, struct pt_regs *regs)
{
int statusword;
struct snull_priv *priv;
struct snull_packet *pkt = NULL;
/*
* 通常,需要检查 "device" 指针以确保这个中断是发送给自己的。
* 然后为 "struct device *dev" 赋
*/
struct net_device *dev = (struct net_device *)dev_id;

/* paranoid */
if (!dev)
return;

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

/* 取得设备状态指字,对于真实设备,使用I/O指令,比如:int txsr = inb(TX_STATUS); */
statusword = priv->status;
priv->status = 0;
if (statusword & SNULL_RX_INTR) { /*如果是接收数据包的中断*/
/* send it to snull_rx for handling */
pkt = priv->rx_queue;
if (pkt) {
priv->rx_queue = pkt->next;
snull_rx(dev, pkt);
}
}
if (statusword & SNULL_TX_INTR) { /*如果是发送数据包的中断*/
/* a transmission is over: free the skb */
priv->stats.tx_packets++;
priv->stats.tx_bytes += priv->tx_packetlen;
dev_kfree_skb(priv->skb);
}

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

/*释放缓冲区*/
if (pkt) snull_release_buffer(pkt); /* Do this outside the lock! */
return;
}

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

/*
* As usual, check the "device" pointer for shared handlers.
* Then assign "struct device *dev"
*/
struct net_device *dev = (struct net_device *)dev_id;
/* ... and check with hw if it's really ours */

/* paranoid */
if (!dev)
return;

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

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

/*
* 唯一的区别就在这里,它先屏蔽掉接收中断,然后调用netif_rx_schedule,而不是netif_rx
* 重点还是在于poll函数的设计。
*/
if (statusword & SNULL_RX_INTR) {
snull_rx_ints(dev, 0); /* Disable further interrupts */
netif_rx_schedule(dev);
}
if (statusword & SNULL_TX_INTR) {
/* a transmission is over: free the skb */
priv->stats.tx_packets++;
priv->stats.tx_bytes += priv->tx_packetlen;
dev_kfree_skb(priv->skb);
}

/* Unlock the device and we are done */
spin_unlock(&priv->lock);
return;
}
阅读(729) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~