1.概述
Linux系統多用于服務器上,Linux非常牢固的支持網絡。在Linux,網絡分為兩個層,分別是網絡堆棧協議支持層,以及接收和發送網絡協議的設備驅動程序層。網絡堆棧是硬件中獨立出來的部分,主要用來支持TCP/IP等多種協議,而網絡設備驅動層是連接網絡堆棧協議層和網絡硬件的中間層。
網絡設備驅動程序的主要功能是:
(1)模塊加載或內核啟動相關的初始化處理
(2)清除模塊時的處理
(3)網絡設備的檢索和探測
(4)網絡設備的初始化和注冊
(5)打開或關閉網絡設備
(6)發送網絡數據
(7)接收網絡數據
(8)中斷處理(在發送完數據時,硬件向內核產生一個中斷,告訴內核數據已經發送完畢,在網絡設備接收到數據時,也要發生一個中斷,告訴內核,數據已經到達,請及時處理)
(9)超時處理
(10)多播處理
(11)網絡設備的控制ioctl
而Linux網絡設備驅動的主要功能就是網絡設備的初始化,網絡設備的配置,數據包的收發。
2. Linux網絡設備驅動的接口函數
net_device結構體存儲一個網絡接口的重要信息,它是系統中網絡設備的代表。
sk_buff是socket buffer,在網絡傳輸過程中起著重要的作用,內核把數據包封裝成socket buffer向網絡硬件發送,當網絡硬件接收到數據包時,再把數據包封裝成socket buffer向內核傳送。
注冊網絡設備:
int register_netdev(struct net_device *dev);//網絡設備與字符設備,塊設備不同,沒有主,次設備號
注銷網絡設備:
void unregister_netdev(struct net_device *dev);
返回網絡設備結構體的private data:
void *netdev_priv(struct net_device *dev);
即返回我們定義的設備結構體。
保存設備統計信息的結構體
struct net_device_stats
打開發送隊列,能夠發送數據包,在open()中調用
netif_start_queue(struct net_device *dev);
關閉發送隊列,在stop()中調用
netif_stop_queue(struct net_device* dev);
重新打開隊列,一般在關閉隊列之后重啟隊列
netif_wake_queue(struct net_device *dev);
當數據到達時,通知內核數據包到達
void netif_rx(struct sk_buff *skb);
分配一個sk_buff結構體
struct sk_buff *dev_alloc_skb(unsigned int len);
釋放sk_buff結構體
void dev_kfree_skb(struct sk_buff *skb);
從數據的尾部擴展len長度的空間,為了把數據放到skb的尾部
unsigned char* skb_put(struct sk_buff *skb,int len);
SIOCDEVPRIVATE 可用ioctl執行的16個命令的第一個命令
最后一個是SIOCDEVPRIVATE+15
3.下面給出一個虛擬硬件的網絡驅動的例子
#undef PDEBUG???????????? /* undef it, just in case */
#ifdef SNULL_DEBUG
#? ifdef __KERNEL__
???? /* This one if debugging is on, and kernel space */
#??? define PDEBUG(fmt, args...) printk( KERN_DEBUG "snull: " fmt, ## args)
#? else
???? /* This one for user space */
#??? define PDEBUG(fmt, args...) fprintf(stderr, fmt, ## args)
#? endif
#else
#? define PDEBUG(fmt, args...) /* not debugging: nothing */
#endif
#undef PDEBUGG
#define PDEBUGG(fmt, args...) /* nothing: it's a placeholder */
/* These are the flags in the statusword */
#define SNULL_RX_INTR 0x0001
#define SNULL_TX_INTR 0x0002
/* Default timeout period */
#define SNULL_TIMEOUT 6?? /* In jiffies */
#include
#include
#include /* printk() */
#include /* kmalloc() */
#include ? /* error codes */
#include ? /* size_t */
#include /* mark_bh */
#include
#include ?? /* struct device, and other headers */
#include /* eth_type_trans */
#include ????????? /* struct iphdr */
#include ???????? /* struct tcphdr */
#include
#include
#include
#include
#include
static int lockup = 0;
static int timeout = SNULL_TIMEOUT;
struct net_device snull_devs[2];//這里定義兩個設備,一個是snull0,一個是snull1
//網絡設備結構體,作為net_device->priv
struct snull_priv {
??? struct net_device_stats stats;//有用的統計信息
??? int status;//網絡設備的狀態信息,是發完數據包,還是接收到網絡數據包
??? int rx_packetlen;//接收到的數據包長度
??? u8 *rx_packetdata;//接收到的數據
??? int tx_packetlen;//發送的數據包長度
??? u8 *tx_packetdata;//發送的數據
??? struct sk_buff *skb;//socket buffer結構體,網絡各層之間傳送數據都是通過這個結構體來實現的
??? spinlock_t lock;//自旋鎖
};
void snull_tx_timeout (struct net_device *dev);
//網絡接口的打開函數
int snull_open(struct net_device *dev)
?{
??? printk("call snull_open/n");
??? memcpy(dev->dev_addr, "/0SNUL0", ETH_ALEN);//分配一個硬件地址,ETH_ALEN是網絡設備硬件地址的長度
???
??? netif_start_queue(dev);//打開傳輸隊列,這樣才能進行數據傳輸
??? return 0;
}
int snull_release(struct net_device *dev)
{
??? printk("call snull_release/n");
??? netif_stop_queue(dev); //當網絡接口關閉的時候,調用stop方法,這個函數表示不能再發送數據
??? return 0;
}
//接包函數
void snull_rx(struct net_device *dev, int len, unsigned char *buf)
{
???
??? struct sk_buff *skb;
??? struct snull_priv *priv = (struct snull_priv *) dev->priv;
?
??? /*
???? * The packet has been retrieved from the transmission
???? * medium. Build an skb around it, so upper layers can handle it
???? */
??? skb = dev_alloc_skb(len+2);//分配一個socket buffer,并且初始化skb->data,skb->tail和skb->head
??? if (!skb) {
??????? printk("snull rx: low on mem - packet dropped/n");
??????? priv->stats.rx_dropped++;
??????? return;
??? }
??? skb_reserve(skb, 2); /* align IP on 16B boundary */?
??? memcpy(skb_put(skb, len), buf, len);//skb_put是把數據寫入到socket buffer
??? /* Write metadata, and then pass to the receive level */
??? skb->dev = dev;
??? skb->protocol = eth_type_trans(skb, dev);//返回的是協議號
??? skb->ip_summed = CHECKSUM_UNNECESSARY; //此處不校驗
??? priv->stats.rx_packets++;//接收到包的個數+1
????
??? priv->stats.rx_bytes += len;//接收到包的長度
??? netif_rx(skb);//通知內核已經接收到包,并且封裝成socket buffer傳到上層
??? return;
}
???
???????
/*
?* The typical interrupt entry point
?*/
//中斷處理,此程序中沒有硬件,因此,沒有真正的硬件中斷,只是模擬中斷,在發送完網絡數據包之后,會產生中斷
//用來通知內核已經發送完數據包,當新的數據包到達網絡接口時,會發生中斷,通知新的數據包已經到來了
void snull_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 */
??? if (!dev /*paranoid*/ ) return;
??? /* Lock the device */
??? priv = (struct snull_priv *) dev->priv;
??? spin_lock(&priv->lock);
??? /* retrieve statusword: real netdevices use I/O instructions */
??? statusword = priv->status;
??? if (statusword & SNULL_RX_INTR) {//如果是接收
??????? /* send it to snull_rx for handling */
??????? snull_rx(dev, priv->rx_packetlen, priv->rx_packetdata);
??? }
??? 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);//釋放skb 套接字緩沖區
??? }
??? /* Unlock the device and we are done */
??? spin_unlock(&priv->lock);
??? return;
}
/*
?* Transmit a packet (low level interface)
?*/
//真正的處理的發送數據包
//模擬從一個網絡向另一個網絡發送數據包
void snull_hw_tx(char *buf, int len, struct net_device *dev)
{
?
??
?? /*
???? * This function deals with hw details. This interface loops
???? * back the packet to the other snull interface (if any).
???? * In other words, this function implements the snull behaviour,
???? * while all other procedures are rather device-independent
???? */
??? struct iphdr *ih;//ip頭部
??? struct net_device *dest;//目標設備結構體,net_device存儲一個網絡接口的重要信息,是網絡驅動程序的核心
??? struct snull_priv *priv;
??? u32 *saddr, *daddr;//源設備地址與目標設備地址
??? /* I am paranoid. Ain't I? */
??? if (len < sizeof(struct ethhdr) + sizeof(struct iphdr)) {
??????? printk("snull: Hmm... packet too short (%i octets)/n",
?????????????? len);
??????? return;
??? }
??? /*
???? * Ethhdr is 14 bytes, but the kernel arranges for iphdr
???? * to be aligned (i.e., ethhdr is unaligned)
???? */
??? ih = (struct iphdr *)(buf+sizeof(struct ethhdr));
??? saddr = &ih->saddr;
??? daddr = &ih->daddr;
//在同一臺機器上模擬兩個網絡,不同的網段地址,進行發送網絡數據包與接收網絡數據包
??? ((u8 *)saddr)[2] ^= 1; /* change the third octet (class C) ^是位異或操作符把第三個部分的網絡地址與1進行異或,由于同一網絡的數據不進行轉發*/?
??? ((u8 *)daddr)[2] ^= 1;
??? ih->check = 0;???????? /* and rebuild the checksum (ip needs it) */
??? ih->check = ip_fast_csum((unsigned char *)ih,ih->ihl);
??? if (dev == snull_devs)
??????? PDEBUGG("%08x:%05i --> %08x:%05i/n",
?????????????? ntohl(ih->saddr),ntohs(((struct tcphdr *)(ih+1))->source),
?????????????? ntohl(ih->daddr),ntohs(((struct tcphdr *)(ih+1))->dest));
??? else
??????? PDEBUGG("%08x:%05i <-- %08x:%05i/n",
?????????????? ntohl(ih->daddr),ntohs(((struct tcphdr *)(ih+1))->dest),
?????????????? ntohl(ih->saddr),ntohs(((struct tcphdr *)(ih+1))->source));
??? /*
???? * Ok, now the packet is ready for transmission: first simulate a
???? * receive interrupt on the twin device, then? a
???? * transmission-done on the transmitting device
???? */
??? dest = snull_devs + (dev==snull_devs ? 1 : 0);//如果dev是0,那么dest就是1,如果dev是1,那么dest是0
??? priv = (struct snull_priv *) dest->priv;//目標dest中的priv
??? priv->status = SNULL_RX_INTR;
??? priv->rx_packetlen = len;
??? priv->rx_packetdata = buf;
??? snull_interrupt(0, dest, NULL);
??? priv = (struct snull_priv *) dev->priv;
??? priv->status = SNULL_TX_INTR;
??? priv->tx_packetlen = len;
??? priv->tx_packetdata = buf;
??? if (lockup && ((priv->stats.tx_packets + 1) % lockup) == 0) {
??????? /* Simulate a dropped transmit interrupt */
??????? netif_stop_queue(dev);
??
??????? PDEBUG("Simulate lockup at %ld, txp %ld/n", jiffies,
??????????????????????? (unsigned long) priv->stats.tx_packets);
??? }
??? else
??????? snull_interrupt(0, dev, NULL);
}
?
/*
?* Transmit a packet (called by the kernel)
?*/
//發包函數
int snull_tx(struct sk_buff *skb, struct net_device *dev)
{
??
??? int len;
??? char *data;
??? struct snull_priv *priv = (struct snull_priv *) dev->priv;
??? if ( skb == NULL) {
??????? PDEBUG("tint for %p,? skb %p/n", dev,? skb);
??????? snull_tx_timeout (dev);
??????? if (skb == NULL)
??????????? return 0;
??? }
??? len = skb->len < ETH_ZLEN ? ETH_ZLEN : skb->len;//ETH_ZLEN是所發的最小數據包的長度
??? data = skb->data;//將要發送的數據包中數據部分
??? dev->trans_start = jiffies; //保存當前的發送時間
??? priv->skb = skb;
??? snull_hw_tx(data, len, dev);//真正的發送函數
?? return 0; /* Our simple device can not fail */
}
/*
?* Deal with a transmit timeout.
?*/
//一旦超出watchdog_timeo就會調用snull_tx_timeout
void snull_tx_timeout (struct net_device *dev)
{
?? printk("call snull_tx_timeout/n");
??? struct snull_priv *priv = (struct snull_priv *) dev->priv;
??? PDEBUG("Transmit timeout at %ld, latency %ld/n", jiffies,
??????????????????? jiffies - dev->trans_start);
??? priv->status = SNULL_TX_INTR;
??? snull_interrupt(0, dev, NULL);//超時后發生中斷
??? priv->stats.tx_errors++;//發送的錯誤數
??? netif_wake_queue(dev); //為了再次發送數據,調用此函數,重新啟動發送隊列
??? return;
}
?
/*
?* Ioctl commands
?*/
int snull_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
{
?
??? PDEBUG("ioctl/n");
??? return 0;
}
/*
?* Return statistics to the caller
?*/
struct net_device_stats *snull_stats(struct net_device *dev)
{
??? struct snull_priv *priv = (struct snull_priv *) dev->priv;
??? return &priv->stats;//得到統計資料信息
}
//設備初始化函數
int snull_init(struct net_device *dev)
{
?? printk("call snull_init/n");
??? /*
???? * Then, assign other fields in dev, using ether_setup() and some
???? * hand assignments
???? */
??? ether_setup(dev);//填充一些以太網中的設備結構體的項
??? dev->open??????????? = snull_open;
??? dev->stop??????????? = snull_release;
??? //dev->set_config????? = snull_config;
??? dev->hard_start_xmit = snull_tx;
??? dev->do_ioctl??????? = snull_ioctl;
??? dev->get_stats?????? = snull_stats;
??? //dev->change_mtu????? = snull_change_mtu;?
?? // dev->rebuild_header? = snull_rebuild_header;
??? //dev->hard_header???? = snull_header;
??? dev->tx_timeout???? = snull_tx_timeout;//超時處理
??? dev->watchdog_timeo = timeout;
??? /* keep the default flags, just add NOARP */
??? dev->flags?????????? |= IFF_NOARP;
??? dev->hard_header_cache = NULL;????? /* Disable caching */
??? SET_MODULE_OWNER(dev);
??? /*
???? * Then, allocate the priv field. This encloses the statistics
???? * and a few private fields.
???? */
//為priv分配內存
??? dev->priv = kmalloc(sizeof(struct snull_priv), GFP_KERNEL);
? if (dev->priv == NULL)
?????? return -ENOMEM;
??? memset(dev->priv, 0, sizeof(struct snull_priv));
??? spin_lock_init(& ((struct snull_priv *) dev->priv)->lock);
??? return 0;
}
struct net_device snull_devs[2] = {
??? { init: snull_init, },? /* init, nothing more */
??? { init: snull_init, }
};
int snull_init_module(void)
{
?? int i,result=0;
?? strcpy(snull_devs[0].name,"snull0");//net_device結構體中的name表示設備名
?? strcpy(snull_devs[1].name,"snull1");//即定義了兩個設備,snull0與snull1
??? for (i=0; i<2;? i++)
??????? if ( (result = register_netdev(snull_devs+i)) )//注冊設備
??????????? printk("snull: error %i registering device /"%s/"/n",
?????????????????? result, snull_devs[i].name);
???? return 0;
}
void snull_cleanup(void)
{
??? int i;
?
??? for (i=0; i<2;? i++) {
??????? kfree(snull_devs[i].priv);
??????? unregister_netdev(snull_devs+i);
??? }
??? return;
}
module_init(snull_init_module);
module_exit(snull_cleanup);
分析:
這個例子中包括了以下部分:
(1)網絡設備初始化 snull_init
(2)發送數據包函數snull_tx,而真正的發送數據包函數是snull_hw_tx,在snull_hw_tx,目標設備dest收到數據包產生中斷,然后再向源設備發送數據包,發送完之后也產生中斷
(3)接收數據包的函數snull_rx
(4)中斷處理snull_interrupt
(5)網絡超時處理snull_timeout
(6)網絡設備的打開snull_open
測試:
(1)生成snull.ko, insmod snull.ko
(2)為兩個網絡設備分配IP:
ifconfig snull0 192.168.0.1
ifconfig snull1 192.168.1.2
可以看出,兩個網絡設備在不同的網段
ping 192.168.0.2 由于目標daddr經過((u8 *)daddr)[2] ^= 1,變成 192.168.1.2,相當于ping 192.168.1.2.
而源IP 192.168.0.1 經過((u8 *)saddr)[2] ^= 1,變成192.168.1.1,那么dest設備發送的數據包地址是192.168.1.1,相當于發送給192.168.0.1
如果不經過這樣處理,直接ping 192.168.1.2 是不能ping 通的,由于不在同一個網段上。
可以測試一下car /var/log/messages.
關于Linux網絡驅動就介紹到這里了。
?
評論
查看更多