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

📄 dev.c

📁 一个基于linux的TCP/IP协议栈的实现
💻 C
字号:
/* dev.c  * linqianghe@163.com * 2006-09-18 */#include "dev_dummy.h"#include "dev.h"#include "log.h"#include <linux/netpoll.h>#include <linux/list.h>static DEFINE_SPINLOCK(myptype_lock);static struct list_head myptype_base[16];static struct list_head myptype_all;DEFINE_PER_CPU(struct softnet_data, mysoftnet_data) = { NULL };DEFINE_PER_CPU(struct netif_rx_stats, mynetdev_rx_stat) = { 0, };int mynetdev_max_backlog = 1000;int mynetdev_budget = 300;int myweight_p = 64;            /* old backlog weight */int mynetdev_nit;void mydev_add_pack(struct packet_type *pt){	int hash;	spin_lock_bh( &myptype_lock );	if( pt->type == htons(ETH_P_ALL) ){		mynetdev_nit++;		list_add_rcu(&pt->list, &myptype_all);	} else {		hash = ntohs( pt->type ) & 15;		list_add_rcu( &pt->list, &myptype_base[hash] );	}	spin_unlock_bh( &myptype_lock );}static inline void __mynetif_rx_schedule(struct net_device *dev){	unsigned long flags;	local_irq_save( flags );	dev_hold( dev );	list_add_tail(&dev->poll_list, &__get_cpu_var( mysoftnet_data ).poll_list);	if (dev->quota < 0)		dev->quota += dev->weight;	else		dev->quota = dev->weight;	__raise_softirq_irqoff( MYNET_RX_SOFTIRQ );	local_irq_restore( flags );}static inline void mynetif_rx_schedule(struct net_device *dev){	if (netif_rx_schedule_prep(dev))		__mynetif_rx_schedule(dev);}int mynetif_rx( struct sk_buff *skb ){	struct softnet_data *queue;	unsigned long flags;	PR_DEBUG( "receive a packet!\n" );	local_irq_save( flags );	queue = &__get_cpu_var( mysoftnet_data );	__get_cpu_var( mynetdev_rx_stat ).total++;	if( queue->input_pkt_queue.qlen <= mynetdev_max_backlog ){		if (queue->input_pkt_queue.qlen) {enqueue:			dev_hold(skb->dev);			__skb_queue_tail( &queue->input_pkt_queue, skb );			local_irq_restore( flags );			return NET_RX_SUCCESS;		}		mynetif_rx_schedule( &queue->backlog_dev );		goto enqueue;	}	return 0;	}static __inline__ int mydeliver_skb(struct sk_buff *skb,				struct packet_type *pt_prev, struct net_device *orig_dev){	atomic_inc( &skb->users );	return pt_prev->func( skb, skb->dev, pt_prev, orig_dev );}static inline struct net_device *myskb_bond(struct sk_buff *skb){	struct net_device *dev = skb->dev;	if( dev->master )		skb->dev = dev->master;	return dev;}int mynetif_receive_skb(struct sk_buff *skb){	struct packet_type *ptype, *pt_prev;	struct net_device *orig_dev;	int ret = 0;	unsigned short type;	if( !skb->input_dev )		skb->input_dev = skb->dev;	PR_DEBUG( "mynetif_receive_skb\n" );	__get_cpu_var(mynetdev_rx_stat).total++;	orig_dev = myskb_bond(skb);	pt_prev = NULL;	rcu_read_lock();/*		list_for_each_entry_rcu(ptype, &myptype_all, list) {		if (!ptype->dev || ptype->dev == skb->dev) {			if (pt_prev) 				ret = mydeliver_skb(skb, pt_prev, orig_dev);			pt_prev = ptype;		}	}*/	type = skb->protocol;	list_for_each_entry_rcu( ptype, &myptype_base[ntohs(type)&15], list ){		if( ptype->type == type && (!ptype->dev || ptype->dev == skb->dev) ){			if( pt_prev ) 				ret = mydeliver_skb( skb, pt_prev, orig_dev );			pt_prev = ptype;		}	}	if (pt_prev) {		ret = pt_prev->func( skb, skb->dev, pt_prev, orig_dev );	}else{		kfree_skb( skb );		ret = NET_RX_DROP;	}	rcu_read_unlock();	return ret;}static int myprocess_backlog(struct net_device *backlog_dev, int *budget){	int work = 0;	int quota = min(backlog_dev->quota, *budget);	struct softnet_data *queue = &__get_cpu_var( mysoftnet_data );	unsigned long start_time = jiffies;	backlog_dev->weight = myweight_p;	for (;;) {		struct sk_buff *skb;		struct net_device *dev;		local_irq_disable();		skb = __skb_dequeue( &queue->input_pkt_queue );		if (!skb)			goto job_done;		local_irq_enable();		dev = skb->dev;		mynetif_receive_skb(skb);		dev_put(dev);		work++;		if (work >= quota || jiffies - start_time > 1)			break;	}	backlog_dev->quota -= work;	*budget -= work;	return -1;job_done:	backlog_dev->quota -= work;	*budget -= work;	list_del( &backlog_dev->poll_list );	smp_mb__before_clear_bit();	netif_poll_enable(backlog_dev);	local_irq_enable();	return 0;}static void mynet_tx_action(struct softirq_action *h){}static void mynet_rx_action(struct softirq_action *h){	struct softnet_data *queue = &__get_cpu_var( mysoftnet_data );	unsigned long start_time = jiffies;	int budget = mynetdev_budget;	void *have;	PR_DEBUG( "receive a packet!\n" );	local_irq_disable();	while( !list_empty(&queue->poll_list) ){		struct net_device *dev;		if (budget <= 0 || jiffies - start_time > 1)			goto softnet_break;		local_irq_enable();		dev = list_entry(queue->poll_list.next, struct net_device, poll_list);		have = netpoll_poll_lock(dev);		if (dev->quota <= 0 || dev->poll(dev, &budget)) {			netpoll_poll_unlock(have);			local_irq_disable();			list_del(&dev->poll_list);			list_add_tail(&dev->poll_list, &queue->poll_list);			if (dev->quota < 0)				dev->quota += dev->weight;			else				dev->quota = dev->weight;		} else {			netpoll_poll_unlock(have);			dev_put(dev);			local_irq_disable();		}	}out:	local_irq_enable();	return;softnet_break:	__get_cpu_var( mynetdev_rx_stat ).time_squeeze++;	__raise_softirq_irqoff( MYNET_RX_SOFTIRQ );	goto out;}int __init mynet_dev_init(void){	int i;	INIT_LIST_HEAD( &myptype_all );	for (i = 0; i < 16; i++) 		INIT_LIST_HEAD( &myptype_base[i] );	PR_DEBUG( "init the softnet_data queue...\n");	for_each_cpu(i) {		struct softnet_data *queue;		queue = &per_cpu( mysoftnet_data, i );		skb_queue_head_init( &queue->input_pkt_queue );		queue->completion_queue = NULL;		INIT_LIST_HEAD( &queue->poll_list );		set_bit( __LINK_STATE_START, &queue->backlog_dev.state );		queue->backlog_dev.weight = myweight_p;		queue->backlog_dev.poll = myprocess_backlog;		atomic_set( &queue->backlog_dev.refcnt, 1 );	}	open_softirq( MYNET_TX_SOFTIRQ, mynet_tx_action, NULL );	open_softirq( MYNET_RX_SOFTIRQ, mynet_rx_action, NULL );	return 0;}EXPORT_SYMBOL_GPL( mynetif_rx );EXPORT_SYMBOL_GPL( mydev_add_pack );

⌨️ 快捷键说明

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