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

📄 vmeth.h

📁 LINUX网络代码详解,好东西!不能错过哟!
💻 H
字号:
#ifndef __VMETH_H__
	#define __VMETH_H__

/*The max virtual ethernet card numbers
  Linux kernel 2.4 not support ethernet device more than 100. You may fixed it.
  See dev.c function 'dev_alloc_name'
  
*/
#define VMETH_MAX_VM		1024

/*Default ethernet card to be bound*/
#define VMETH_DEF_BOUND_ETH		"eth0"

#define VMETH_IO			0x280
#define VMETH_IRQ				3
#define VMETH_DMA				5

#define DEF_BASEMAC_STR		"00:e8:fc:00:00:00"
#define DEF_BASEMAC			{0x00, 0xe8, 0xfc, 0x00, 0x00 ,0x00}
/*
 *	IEEE 802.3 Ethernet magic constants.  The frame sizes omit the preamble
 *	and FCS/CRC (frame check sequence). 
 */

#define ETH_ALEN	6		/* Octets in one ethernet addr	 */
#define ETH_HLEN	14		/* Total octets in header.	 */
#define ETH_ZLEN	60		/* Min. octets in frame sans FCS */
#define ETH_DATA_LEN	1500		/* Max. octets in payload	 */
#define ETH_FRAME_LEN	1514		/* Max. octets in frame sans FCS */

/*
 *	This is an Ethernet frame header.
 */
 
struct tagETHHEADER
{
	unsigned char	szDest[ETH_ALEN];	/* destination eth addr	*/
	unsigned char	szSource[ETH_ALEN];	/* source ether addr	*/
	unsigned short	usProto;			/* packet type ID field	*/
}ETHHEADER_S;

typedef unsigned char	MAC_S[6];
#define MAC_CMP(mac1, mac2)		memcmp((void *)(mac1), (void *)(mac2), sizeof(MAC_S))
#define MAC_CPY(dest,src)		memcpy((void *)(dest),(void *)(src),sizeof(MAC_S))


typedef struct tagVMETH_HASH_ITEM{
	struct tagVMETH_HASH_ITEM *pstNext;
	unsigned short usIndex;
	MAC_S	szMacKey;
}VMETH_HASH_ITEM_S;

#define VMETH_INVALID_INDEX		(unsigned short)0xFFFF
#define VMETH_HASHTABLE_LEN		1024


/*
	all virtual ethernet device is orgnazied by an array defined at following.
*/
extern VMETH_HASH_ITEM_S  *gstHashTable[VMETH_HASHTABLE_LEN];
extern rwlock_t HashLock ;

/*Get lower 10 bits as hash*/
static inline unsigned short VMETH_HASH(const unsigned char *pstKey)
{
	unsigned short usIndex =(unsigned short)((unsigned char)(pstKey[5]) +(unsigned char)((unsigned long)(pstKey[4]) & 0x03));
	return (usIndex & 0x3F);	
}


/*
	Initialize hash list
*/
static inline int VMETH_InitHashTable()
{
	unsigned short usTemp;

	rwlock_init(&HashLock);
	for(usTemp = 0; usTemp < VMETH_HASHTABLE_LEN; usTemp++){
		gstHashTable[usTemp] = NULL;
	}
	return 0;
}

static inline void VMETH_DestroyHashTable()
{
	unsigned short usTemp;
	VMETH_HASH_ITEM_S *pstNext,*pstThis;

	for(usTemp = 0; usTemp < VMETH_HASHTABLE_LEN; usTemp++){
		pstNext = gstHashTable[usTemp];
		gstHashTable[usTemp] = NULL;		
		while(pstNext){
			pstThis = pstNext;
			pstNext = pstNext->pstNext;
			kfree(pstThis);		
		}
	}
	return;
}

static inline int VMETH_DelItemFromHashTable(const unsigned char *pstKey)
{
	VMETH_HASH_ITEM_S *pstItem = NULL, *pstPreItem = NULL;
	unsigned short usIndex = VMETH_HASH(pstKey);

	write_lock_bh(&HashLock);
	pstItem = gstHashTable[usIndex];
	while(pstItem != NULL && MAC_CMP(pstKey,pstItem->szMacKey)){
		pstPreItem = pstItem;
		pstItem = pstItem->pstNext;
	}
	/*Found*/
	if(pstItem){
		if(pstPreItem){
			pstPreItem->pstNext = pstItem->pstNext;
		}else{
			gstHashTable[usIndex] = pstItem->pstNext;
		}
		write_unlock_bh(&HashLock);
		kfree(pstItem);
		return 0;
	}
	write_unlock_bh(&HashLock);
	printk("vmeth Delete Item,failed,no item found\n");
	return -1;
}

static inline int VMETH_InsertItemToHashTable(unsigned short usIndex,const char *pstKey)
{
	unsigned short usTemp = VMETH_HASH(pstKey);
	VMETH_HASH_ITEM_S *pstNewItem = NULL;

	pstNewItem = kmalloc(sizeof(VMETH_HASH_ITEM_S),GFP_KERNEL);
	if(pstNewItem){
		pstNewItem->usIndex = usIndex;
		/*not need to use lock*/
		MAC_CPY(pstNewItem->szMacKey,pstKey);
		pstNewItem->pstNext = gstHashTable[usTemp];
		gstHashTable[usTemp] = pstNewItem;
		return 0;
	}
	printk("vmeth can not malloc mem for hash table\n");
	return -1;
}

static inline int VMETH_GetIndexFromHashTable(const MAC_S pstKey)
{
	VMETH_HASH_ITEM_S *pstItem = NULL;
	unsigned short usTemp = VMETH_HASH(pstKey);
	unsigned short usIndex = VMETH_HASHTABLE_LEN;

	read_lock(&HashLock);
	pstItem = gstHashTable[usTemp];
	while(pstItem != NULL && MAC_CMP(pstKey,pstItem->szMacKey)){
		pstItem = pstItem->pstNext;
	}
	/*Found*/
	if(pstItem){
		usIndex = pstItem->usIndex;
	}
	read_unlock(&HashLock);
	return usIndex;
}

static inline u8 hexVal(char c) {
	if (c>='0' && c<='9') return c -= '0';
	if (c>='a' && c<='f') return c -= 'a'-10;
	if (c>='A' && c<='F') return c -= 'A'-10;
	return 0;
}
/*
	get mac address from a string;
	return -1, if failed;
	return 0 if success, and put mac to output parameter 'szMac'
*/
static inline int VMETH_GetMacFromStr(const char *pszStr, char *szMac)
{
	int i;

	if(strlen(pszStr) < 17){
		printk("Input parameter mac len is too short\n");
		return -1;
	}
	for(i=0; i<17; i++)
	{
		if(i == 2 || i == 5 || i == 8 || i == 11 || i == 14){
			if(pszStr[i] != ':'){
				printk("Input parameter mac included invalid char\n");
				return -1;
			}
		}else{
			if((pszStr[i] < '0' || pszStr[i] > '9') && (pszStr[i] < 'a' || pszStr[i] > 'f') ){
				printk("Input parameter mac included invalid char\n");
				return -1;
			}
		}
		
	}
	szMac[0] = (hexVal(pszStr[0]) << 4) + (hexVal(pszStr[1]));
	szMac[1] = (hexVal(pszStr[3]) << 4) + (hexVal(pszStr[4]));
	szMac[2] = (hexVal(pszStr[6]) << 4) + (hexVal(pszStr[7]));
	szMac[3] = (hexVal(pszStr[9]) << 4) + (hexVal(pszStr[10]));
	szMac[4] = (hexVal(pszStr[12]) << 4) + (hexVal(pszStr[13]));
	szMac[5] = (hexVal(pszStr[15]) << 4) + (hexVal(pszStr[16]));	
	return 0;
}
#endif

⌨️ 快捷键说明

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