⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 snull.c

📁 这个程序是LDD3中的网络驱动程序设计的精简版,其中仅注册了一个设备,简化了程序.
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
* snull.c --  the Simple Network Utility
*
* Copyright (C) 2001 Alessandro Rubini and Jonathan Corbet
* Copyright (C) 2001 O'Reilly & Associates
*
* The source code in this file can be freely used, adapted,
* and redistributed in source or binary form, so long as an
* acknowledgment appears in derived source files.  The citation
* should list that the code comes from the book "Linux Device
* Drivers" by Alessandro Rubini and Jonathan Corbet, published
* by O'Reilly & Associates.   No warranty is attached;
* we cannot take responsibility for errors or fitness for use.
*
* $Id: snull.c,v 1.21 2004/11/05 02:36:03 rubini Exp $
*/

#include <linux/config.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/moduleparam.h>

#include <linux/sched.h>
#include <linux/kernel.h> /* printk() */
#include <linux/slab.h> /* kmalloc() */
#include <linux/errno.h>  /* error codes */
#include <linux/types.h>  /* size_t */
#include <linux/interrupt.h> /* mark_bh */

#include <linux/in.h>
#include <linux/netdevice.h>   /* struct device, and other headers */
#include <linux/etherdevice.h> /* eth_type_trans */
#include <linux/ip.h>          /* struct iphdr */
#include <linux/tcp.h>         /* struct tcphdr */
#include <linux/skbuff.h>

#include "snull.h"

#include <linux/in6.h>
#include <asm/checksum.h>

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


/*
* Transmitter lockup simulation, normally disabled.
*/
static int lockup = 0;
module_param(lockup, int, 0);

static int timeout = SNULL_TIMEOUT;
module_param(timeout, int, 0);

struct net_device *snull_devs[2];

/*
* A structure representing an in-flight packet.
*/
struct snull_packet {
        struct snull_packet *next;
        struct net_device *dev;
        int        datalen;
        u8 data[ETH_DATA_LEN];
};

int pool_size = 8;
module_param(pool_size, int, 0);

/*
* This structure is private to each device. It is used to pass
* packets in and out, so there is place for a packet
*/

struct snull_priv {
        struct net_device_stats stats;
        int status;
        struct snull_packet *ppool;
        struct snull_packet *rx_queue;  /* List of incoming packets */
        int rx_int_enabled;
        int tx_packetlen;
        u8 *tx_packetdata;
        struct sk_buff *skb;
        spinlock_t lock;
};

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


void snull_setup_pool(struct net_device *dev)
{
        struct snull_priv *priv = netdev_priv(dev);
        int i;
        struct snull_packet *pkt;

        priv->ppool = NULL;
        for (i = 0; i < pool_size; i++) {
                pkt = kmalloc (sizeof (struct snull_packet), GFP_KERNEL);
                if (pkt == NULL) {
                        printk (KERN_NOTICE "Ran out of memory allocating packet pool\n");
                        return;
                }
                pkt->dev = dev;
                pkt->next = priv->ppool;
                priv->ppool = pkt;
        }
}

/*因为snull_setup_pool分配了pool_size个struct snull_packet,所以,驱动退出时,需要释放内存*/
void snull_teardown_pool(struct net_device *dev)
{
        struct snull_priv *priv = netdev_priv(dev);
        struct snull_packet *pkt;
    
        while ((pkt = priv->ppool)) {
                priv->ppool = pkt->next;
                kfree (pkt);
                /* FIXME - in-flight packets ? */
        }
}    


/*
* 获取设备要传输的第一个包,传输队列首部相应的移动到下一个数据包.
*/
struct snull_packet *snull_get_tx_buffer(struct net_device *dev)
{
        struct snull_priv *priv = netdev_priv(dev);
        unsigned long flags;
        struct snull_packet *pkt;
    
        spin_lock_irqsave(&priv->lock, flags);//确保数据修改时,资源基本的独占
        pkt = priv->ppool;
        priv->ppool = pkt->next;
        if (priv->ppool == NULL) {
                printk (KERN_INFO "Pool empty\n");
                netif_stop_queue(dev);
        }
        spin_unlock_irqrestore(&priv->lock, flags);//开锁
        return pkt;
}

/*将包缓存交还给缓存池*/
void snull_release_buffer(struct snull_packet *pkt)
{
        unsigned long flags;
        struct snull_priv *priv = netdev_priv(pkt->dev);
        
        spin_lock_irqsave(&priv->lock, flags);
        pkt->next = priv->ppool;
        priv->ppool = pkt;
        spin_unlock_irqrestore(&priv->lock, flags);
        if (netif_queue_stopped(pkt->dev) && pkt->next == NULL)
                netif_wake_queue(pkt->dev);
}

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

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

/*取得传输队列中的第一个数据包*/
struct snull_packet *snull_dequeue_buf(struct net_device *dev)
{
        struct snull_priv *priv = netdev_priv(dev);
        struct snull_packet *pkt;
        unsigned long flags;

        spin_lock_irqsave(&priv->lock, flags);
        pkt = priv->rx_queue;
        if (pkt != NULL)
                priv->rx_queue = pkt->next;
        spin_unlock_irqrestore(&priv->lock, flags);
        return pkt;
}

/*
* 打开/关闭接收中断.
*/
static void snull_rx_ints(struct net_device *dev, int enable)
{
        struct snull_priv *priv = netdev_priv(dev);
        priv->rx_int_enabled = enable;
}

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

int snull_open(struct net_device *dev)
{
        /* request_region(), request_irq(), ....  (like fops->open) */

        /* 
         * Assign the hardware address of the board: use "\0SNULx", where
         * x is 0 or 1. The first byte is '\0' to avoid being a multicast
         * address (the first byte of multicast addrs is odd).
         */
        memcpy(dev->dev_addr, "\0SNUL0", ETH_ALEN);
        if (dev == snull_devs[1])
                dev->dev_addr[ETH_ALEN-1]++; /* \0SNUL1 */
        netif_start_queue(dev);
        return 0;
}

/*设备停止函数,这里的工作就是停止传输队列*/
int snull_release(struct net_device *dev)
{
    /* release ports, irq and such -- like fops->close */

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

/*
* 当用户调用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;
}
         
/*
* 设备的中断函数,当需要发/收数据,出现错误,连接状态变化等,它会被触发
* 对于典型的网络设备,一般会在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;
}


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

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -