1. 程式人生 > >網絡卡驅動設計---架構分析加回環網絡卡驅動設計(網絡卡驅動上)

網絡卡驅動設計---架構分析加回環網絡卡驅動設計(網絡卡驅動上)

網絡卡驅動架構分析:

1. Linux網路子系統



2. 重要資料結構




總結一下三個重要的資料結構:

    2.1. net_device

    2.2. net_device_ops

    2.3. sk_buff

3. 網絡卡驅動架構分析

CS8900.c //早期2410使用的網絡卡晶片 

3.1. 網絡卡初始化

首先找到驅動程式的入口:

早期的驅動入口並不是module_init()函式,而是init_module,所以找到這個函式

int __init init_module(void)
{
	struct net_device *dev = alloc_etherdev(sizeof(struct net_local));
	struct net_local *lp;
	int ret = 0;

#if DEBUGGING
	net_debug = debug;
#else
	debug = 0;
#endif
	if (!dev)
		return -ENOMEM;

	dev->irq = irq;
	dev->base_addr = io;
	lp = netdev_priv(dev);

#if ALLOW_DMA
	if (use_dma) {
		lp->use_dma = use_dma;
		lp->dma = dma;
		lp->dmasize = dmasize;
	}
#endif

	spin_lock_init(&lp->lock);

        /* boy, they'd better get these right */
        if (!strcmp(media, "rj45"))
		lp->adapter_cnf = A_CNF_MEDIA_10B_T | A_CNF_10B_T;
	else if (!strcmp(media, "aui"))
		lp->adapter_cnf = A_CNF_MEDIA_AUI   | A_CNF_AUI;
	else if (!strcmp(media, "bnc"))
		lp->adapter_cnf = A_CNF_MEDIA_10B_2 | A_CNF_10B_2;
	else
		lp->adapter_cnf = A_CNF_MEDIA_10B_T | A_CNF_10B_T;

        if (duplex==-1)
		lp->auto_neg_cnf = AUTO_NEG_ENABLE;

        if (io == 0) {
                printk(KERN_ERR "cs89x0.c: Module autoprobing not allowed.\n");
                printk(KERN_ERR "cs89x0.c: Append io=0xNNN\n");
                ret = -EPERM;
		goto out;
        } else if (io <= 0x1ff) {
		ret = -ENXIO;
		goto out;
	}


第一步:分配net_device結構,

第二步:初始化net_device結構,

       dev->irq = irq;//分配中斷號
	dev->base_addr = io;//裝置基地址
	lp = netdev_priv(dev);
第三步:
ret = cs89x0_probe1(dev, io, 1);\\這一步其實也是初始化硬體的!還有一部分是對device結構進行一些初始化
這個函式比較長就不貼程式碼了,其中一行比較重要:

dev->netdev_ops = &net_ops; \\這個是對netdev_ops成員進行初始化


最後一步註冊網絡卡驅動!上圖中第二個紅色箭頭所指向的地方!

總結一下上圖:




3.2. 網絡卡資料的傳送

這個結合前面的經驗,找到網絡卡的函式操作集結構:


可以看到這個成員函式的名字叫做:net_send_packet

static netdev_tx_t net_send_packet(struct sk_buff *skb,struct net_device *dev)
{
	struct net_local *lp = netdev_priv(dev);
	unsigned long flags;

	if (net_debug > 3) {
		printk("%s: sent %d byte packet of type %x\n",
			dev->name, skb->len,
			(skb->data[ETH_ALEN+ETH_ALEN] << 8) | skb->data[ETH_ALEN+ETH_ALEN+1]);
	}

	/* keep the upload from being interrupted, since we
                  ask the chip to start transmitting before the
                  whole packet has been completely uploaded. */

	spin_lock_irqsave(&lp->lock, flags);
	netif_stop_queue(dev);//1. 網絡卡驅動在向上層傳送資料的時候暫時停止接收上層發來的資料

	/* initiate a transmit sequence */
	writeword(dev->base_addr, TX_CMD_PORT, lp->send_cmd);//2. 將skb中的資料寫入暫存器
	writeword(dev->base_addr, TX_LEN_PORT, skb->len);

	/* Test to see if the chip has allocated memory for the packet */
	if ((readreg(dev, PP_BusST) & READY_FOR_TX_NOW) == 0) {
		/*
		 * Gasp!  It hasn't.  But that shouldn't happen since
		 * we're waiting for TxOk, so return 1 and requeue this packet.
		 */

		spin_unlock_irqrestore(&lp->lock, flags);
		if (net_debug) printk("cs89x0: Tx buffer not free!\n");
		return NETDEV_TX_BUSY;
	}
	/* Write the contents of the packet */
	writewords(dev->base_addr, TX_FRAME_PORT,skb->data,(skb->len+1) >>1);
	spin_unlock_irqrestore(&lp->lock, flags);
	dev->stats.tx_bytes += skb->len;
	dev_kfree_skb (skb);//3. 釋放skb結構

        //傳送資料完後,網絡卡會產生一箇中斷
	return NETDEV_TX_OK;
}
產生一箇中斷這個可以查查request_irq函式,在這個函式被呼叫的地方可以看到這樣的一行程式碼:
ret = request_irq(dev->irq, net_interrupt, 0, dev->name, dev);</span>


可以看到這裡呼叫了net_initerupt函式,網絡卡傳送和接收中斷!


一個是傳送中斷,一個是接收中斷,

netif_wake_queue(dev);/* Inform upper layers. */   // 這行程式碼表示在傳送中斷處理過程中,通知上層協議,可以再次向網絡卡傳輸資料。

3.3. 網絡卡資料的接收

網絡卡資料的接收入口是在中斷中完成的,這個是從中斷函式中可以看到net_interrupt


接收中斷處理函式net_rx(dev)

/* We have a good packet(s), get it/them out of the buffers. */
static void
net_rx(struct net_device *dev)
{
	struct sk_buff *skb;
	int status, length;

	int ioaddr = dev->base_addr;
	status = readword(ioaddr, RX_FRAME_PORT);//讀取暫存器,網絡卡接收狀態
	length = readword(ioaddr, RX_FRAME_PORT);//網絡卡接收位元組長度

	if ((status & RX_OK) == 0) {
		count_rx_errors(status, dev);
		return;
	}

	/* Malloc up new buffer. */
	skb = dev_alloc_skb(length + 2);//分配skb結構 +2位元組空間是為頭預留的
	if (skb == NULL) {
#if 0		/* Again, this seems a cruel thing to do */
		printk(KERN_WARNING "%s: Memory squeeze, dropping packet.\n", dev->name);
#endif
		dev->stats.rx_dropped++;
		return;
	}
	skb_reserve(skb, 2);	/* longword align L3 header */

	readwords(ioaddr, RX_FRAME_PORT, skb_put(skb, length), length >> 1);//將收到的資料填充入skb
	if (length & 1)
		skb->data[length-1] = readword(ioaddr, RX_FRAME_PORT);

	if (net_debug > 3) {
		printk(	"%s: received %d byte packet of type %x\n",
			dev->name, length,
			(skb->data[ETH_ALEN+ETH_ALEN] << 8) | skb->data[ETH_ALEN+ETH_ALEN+1]);
	}

        skb->protocol=eth_type_trans(skb,dev);
	netif_rx(skb);//將skb提交到協議棧
	dev->stats.rx_packets++;
	dev->stats.rx_bytes += length;
}


迴環網絡卡驅動設計:


使用ifocnfig,可以看到除了eth0還有一個l0, eth0代表的是一個物理網絡卡,l0代表的就是迴環網絡卡,從上面的列印資訊可以看到l0的IP地址是127.0.0.1,可以看到當ping 127.0.0.x的時候能ping通,其實l0就是網絡卡的tx和rx在軟體層的短接!所以才叫做迴環網絡卡!

其實核心程式碼中也可以找到迴環網絡卡的驅動!Lookback.c

這個檔案中的程式碼部分其實不是核心模組,而是由其它部分的呼叫的!

刪掉核心程式碼中的原有的loopback.c,結合上面的的分析和原有原始碼的分析重寫編寫loopback.c,

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/skbuff.h>
#include <linux/if_ether.h>	/* For the statistics structure. */


unsigned long bytes = 0;
unsigned long packets = 0;//skb包


static int loopback_xmit(struct sk_buff *skb, struct net_device *dev)//資料傳送
{

	skb->protocol = eth_type_trans(skb,dev);//表明skb包的協議 乙太網協議


	bytes += skb->len;//傳送的資料量
	packets++; //傳送的資料包也要加一

	netif_rx(skb);//將skb向回送, 迴環網絡卡驅動就是這實現的,這是很關鍵的一步

	return 0;
}

static struct net_device_stats *loopback_get_stats(struct net_device *dev)//獲取網絡卡狀態
{
	struct net_device_stats *stats = &dev->stats;//首先把state這個成員取出來
	
	stats->rx_packets = packets;//表示網絡卡收到了多少個包
	stats->tx_packets = packets;//表示網絡卡傳送了多少個包
	stats->rx_bytes = bytes;//表示網絡卡接收到了多少個位元組
	stats->tx_bytes = bytes;
	return stats;//返回狀態
}

static const struct net_device_ops loopback_ops = {//定義一個net_device_ops 結構
	.ndo_start_xmit= loopback_xmit,//傳送指標
	.ndo_get_stats = loopback_get_stats,//獲取網絡卡狀態的函式
};


static void loopback_setup(struct net_device *dev)//初始化設定操作
{
	dev->mtu	= (16 * 1024) + 20 + 20 + 12;//網絡卡最大接收包的尺寸:16K + TCP頭 + IP頭 + 乙太網頭
	dev->flags		= IFF_LOOPBACK;//迴環網絡卡專有標誌 這是一個巨集核心程式碼可查
	dev->header_ops		= ð_header_ops;//這個是網路包的函式操作集,核心可以看這個成員的資料結構
	dev->netdev_ops		= &loopback_ops;//網絡卡所支援操作的集合

}


static __net_init int loopback_net_init(struct net *net)
{
	struct net_device *dev;
	int err;
	err = -ENOMEM;
	dev = alloc_netdev(0, "lo", loopback_setup);//分配一個net_device結構,loopback為一個初始化函式
	if (!dev)
		goto out;

	err = register_netdev(dev);//註冊網絡卡驅動程式
	if (err)
		goto out_free_netdev;

	net->loopback_dev = dev;
	return 0;


out_free_netdev:
	free_netdev(dev);
out:
	if (net == &init_net)
		panic("loopback: Failed to register netdevice: %d\n", err);
	return err;
}

static __net_exit void loopback_net_exit(struct net *net)
{
	struct net_device *dev = net->loopback_dev;

	unregister_netdev(dev);//登出網絡卡驅動程式
}

/* Registered in net/core/dev.c */
struct pernet_operations __net_initdata loopback_net_ops = {
       .init = loopback_net_init,
       .exit = loopback_net_exit,
};
</span>

儲存,然後重新編譯核心,下載到開發板看執行效果!


上面的迴環網絡卡驅動有點問題,ping不同!

這是能ping通的核心自帶的原始碼:

#include <linux/kernel.h>
#include <linux/jiffies.h>
#include <linux/module.h>
#include <linux/interrupt.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/string.h>
#include <linux/socket.h>
#include <linux/errno.h>
#include <linux/fcntl.h>
#include <linux/in.h>
#include <linux/init.h>

#include <asm/system.h>
#include <asm/uaccess.h>
#include <asm/io.h>

#include <linux/inet.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/skbuff.h>
#include <linux/ethtool.h>
#include <net/sock.h>
#include <net/checksum.h>
#include <linux/if_ether.h>	/* For the statistics structure. */
#include <linux/if_arp.h>	/* For ARPHRD_ETHER */
#include <linux/ip.h>
#include <linux/tcp.h>
#include <linux/percpu.h>
#include <net/net_namespace.h>
#include <linux/u64_stats_sync.h>

struct pcpu_lstats {
	u64			packets;
	u64			bytes;
	struct u64_stats_sync	syncp;
};

/*
 * The higher levels take care of making this non-reentrant (it's
 * called with bh's disabled).
 */
static netdev_tx_t loopback_xmit(struct sk_buff *skb,
				 struct net_device *dev)
{
	struct pcpu_lstats *lb_stats;
	int len;

	skb_orphan(skb);

	skb->protocol = eth_type_trans(skb, dev);

	/* it's OK to use per_cpu_ptr() because BHs are off */
	lb_stats = this_cpu_ptr(dev->lstats);

	len = skb->len;
	if (likely(netif_rx(skb) == NET_RX_SUCCESS)) {
		u64_stats_update_begin(&lb_stats->syncp);
		lb_stats->bytes += len;
		lb_stats->packets++;
		u64_stats_update_end(&lb_stats->syncp);
	}

	return NETDEV_TX_OK;
}

static struct rtnl_link_stats64 *loopback_get_stats64(struct net_device *dev,
						      struct rtnl_link_stats64 *stats)
{
	u64 bytes = 0;
	u64 packets = 0;
	int i;

	for_each_possible_cpu(i) {
		const struct pcpu_lstats *lb_stats;
		u64 tbytes, tpackets;
		unsigned int start;

		lb_stats = per_cpu_ptr(dev->lstats, i);
		do {
			start = u64_stats_fetch_begin(&lb_stats->syncp);
			tbytes = lb_stats->bytes;
			tpackets = lb_stats->packets;
		} while (u64_stats_fetch_retry(&lb_stats->syncp, start));
		bytes   += tbytes;
		packets += tpackets;
	}
	stats->rx_packets = packets;
	stats->tx_packets = packets;
	stats->rx_bytes   = bytes;
	stats->tx_bytes   = bytes;
	return stats;
}

static u32 always_on(struct net_device *dev)
{
	return 1;
}

static const struct ethtool_ops loopback_ethtool_ops = {
	.get_link		= always_on,
};

static int loopback_dev_init(struct net_device *dev)
{
	dev->lstats = alloc_percpu(struct pcpu_lstats);
	if (!dev->lstats)
		return -ENOMEM;

	return 0;
}

static void loopback_dev_free(struct net_device *dev)
{
	free_percpu(dev->lstats);
	free_netdev(dev);
}

static const struct net_device_ops loopback_ops = {
	.ndo_init      = loopback_dev_init,
	.ndo_start_xmit= loopback_xmit,
	.ndo_get_stats64 = loopback_get_stats64,
};

/*
 * The loopback device is special. There is only one instance
 * per network namespace.
 */
static void loopback_setup(struct net_device *dev)
{
	dev->mtu		= (16 * 1024) + 20 + 20 + 12;
	dev->hard_header_len	= ETH_HLEN;	/* 14	*/
	dev->addr_len		= ETH_ALEN;	/* 6	*/
	dev->tx_queue_len	= 0;
	dev->type		= ARPHRD_LOOPBACK;	/* 0x0001*/
	dev->flags		= IFF_LOOPBACK;
	dev->priv_flags	       &= ~IFF_XMIT_DST_RELEASE;
	dev->hw_features	= NETIF_F_ALL_TSO | NETIF_F_UFO;
	dev->features 		= NETIF_F_SG | NETIF_F_FRAGLIST
		| NETIF_F_ALL_TSO
		| NETIF_F_UFO
		| NETIF_F_NO_CSUM
		| NETIF_F_RXCSUM
		| NETIF_F_HIGHDMA
		| NETIF_F_LLTX
		| NETIF_F_NETNS_LOCAL
		| NETIF_F_VLAN_CHALLENGED
		| NETIF_F_LOOPBACK;
	dev->ethtool_ops	= &loopback_ethtool_ops;
	dev->header_ops		= ð_header_ops;
	dev->netdev_ops		= &loopback_ops;
	dev->destructor		= loopback_dev_free;
}

/* Setup and register the loopback device. */
static __net_init int loopback_net_init(struct net *net)
{
	struct net_device *dev;
	int err;

	err = -ENOMEM;
	dev = alloc_netdev(0, "lo", loopback_setup);
	if (!dev)
		goto out;

	dev_net_set(dev, net);
	err = register_netdev(dev);
	if (err)
		goto out_free_netdev;

	net->loopback_dev = dev;
	return 0;


out_free_netdev:
	free_netdev(dev);
out:
	if (net_eq(net, &init_net))
		panic("loopback: Failed to register netdevice: %d\n", err);
	return err;
}

/* Registered in net/core/dev.c */
struct pernet_operations __net_initdata loopback_net_ops = {
       .init = loopback_net_init,
};

自己將兩份原始碼對照著看了,暫時還沒找出原因,這裡先上一張錯誤的截圖以及我認為出錯的原因


在使用ifconfig命令的時候,發現RX, TX, 居然都有packets網路包資料傳輸,當ping 127.0.0.x的時候會一直阻塞在哪裡,說明問題應該在初始化引數設定的部分!這裡有資料包傳送但是沒有接收到資料包!說明資料接收部分,也就是迴環傳送部分有問題!這裡暫時先擱一下,後邊在殺個回馬槍來深入研究一下!