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

📄 ge_arp.c

📁 程序是一个vxworks下对PMC公司3386千兆MAC芯片的驱动和配置
💻 C
📖 第 1 页 / 共 3 页
字号:
	if(semGive(GE_ArpSemId)==ERROR)
	{
		if(GE_ARP_DEBUG)
		        GE_printf(GE_ALARM,"GE_ArpTableAdd:sem take failed!\n");
		return ERROR;
	}
	
	macHigh= *(ULONG*)(pNewEntry->PhyAddr);
	macLow=(*(ULONG*)&(pNewEntry->PhyAddr[4]))&0xFFFF0000;
	
	if(addrCount<0x1FFF)
	{
 		cam_add(addrCount,ipAddr,macHigh,macLow);
	}
	else 
	{
		addrCount=0;
		pNewEntry->addrCount=0;
		cam_add(addrCount,ipAddr,macHigh,macLow);
		GE_EventReport(2,SYS_UNKNOWN_PORT,WARN_CAM_FULL,SYS_UNKNOWN_RESULT);
		GE_EventReport(2,SYS_UNKNOWN_PORT,WARN_ARP_FULL,SYS_UNKNOWN_RESULT);
	}
	addrCount+=1;
	return	OK;  
}
/*****************************************************************************************/
STATUS GE_ArpTimer()
{
     	unsigned int 	 i,j;
    	IP_MAC_BAND 	*pEntry;
	IP_MAC_BAND 	*pEntry_Bak;
	IP_MAC_BAND 	*pEntryOld;
	
 	if(semTake(GE_ArpSemId,WAIT_FOREVER)==OK)
    	{
		for (i = 0; i < 2; i++)
		{
      			for(j = 0;j < 256;j++)
      			{      	    
      				pEntry =(IP_MAC_BAND* )(hashValue[j].ArpList[i].next);
				if(pEntry == NULL)
				{
	    				continue;
				}
				pEntry_Bak=NULL;
				do
				{		
					pEntryOld=pEntry_Bak;
					pEntry_Bak=pEntry;	    		    	
	    				
	    				(pEntry->liveTime)+=30;
	    	
					if (pEntry->liveTime>=1800)
	    				{	
	       				
	       					if(pEntry==(IP_MAC_BAND* )(hashValue[j].ArpList[i].next))
		       				{
		       					(IP_MAC_BAND* )(hashValue[j].ArpList[i].next)=pEntry->next;
		       					SBSL_MemFree(GE_ARP_TASK_CODE,(UCHAR*)pEntry);
	       					}	       		
	       					else 
	       					{
	       						pEntryOld->next=pEntry->next;
	       						SBSL_MemFree(GE_ARP_TASK_CODE,(UCHAR*)pEntry);	
	        				}
						if(SBSL_MemFree(GE_ARP_TASK_CODE,pEntry)!=OK)
						{
							return ERROR;
						}
	       						
						if(cam_del(pEntry->addrCount)!=OK) 
						{	if(GE_ARP_DEBUG)
						 	       GE_printf(GE_ALARM,"\n--------------- GE_ArpTimer: cam_delete error! -------\n");
						}
					}
   				
	    				if ((pEntry->liveTime==900)||(pEntry->liveTime==1200)||(pEntry->liveTime==1500))
	    				{

						if(GE_Arp_request_packet_generation (i,pEntry->proAddr)==ERROR)
						{
							if(GE_ARP_DEBUG)
							        GE_printf(GE_ALARM,"GE_ArpTimer:generation ARP_PKT failed!\n");
							continue;
						}
	    				}

	    				pEntry = (IP_MAC_BAND* )(pEntry->next);	   
	    				
				}while(pEntry!= NULL);
			}
		}
	}
	else
	{
		if(GE_ARP_DEBUG)
		        GE_printf(GE_ALARM,"GE_ArpTimer:sem take failed!\n");
		return ERROR;
	}
	if(semGive(GE_ArpSemId)==ERROR)
	{
		if(GE_ARP_DEBUG)
		        GE_printf(GE_ALARM,"GE_ArpTimer:sem give failed!\n");
		return ERROR;
	}   
       return OK;	
}

/***********************************************************************************
*
*	arp entry index function
*/
STATUS  GE_Arp_entry_index(char *arpMsg)
{	
	unsigned char 		*pBuf;
	IP_MAC_BAND 		*pEntry;
	ARP_INDEX_REQUEST	*arp_index_req;
	ARP_INDEX_REPLY		*arp_index_reply;
	unsigned int 		unit,i,j;
	ULONG			ipAddr;
	unsigned char		port_num=2;
	
	arp_index_req = (ARP_INDEX_REQUEST*)(arpMsg+4);
	
       	unit=(unsigned char)arp_index_req->interNum;
        ipAddr=*((ULONG*)(&arp_index_req->ipAddr[0]));
        	             
	if(GE_ARP_DEBUG)
		GE_printf(GE_DATA,"GE_Arp_entry_index:unit=%d,indexType=%d,ipAddr=0x%.8x \n",arp_index_reply->interNum,arp_index_req->indexType,ipAddr);
	
	pBuf = (unsigned char* )malloc(sizeof(ARP_INDEX_REPLY) + 12 );
	if (pBuf == NULL)
	{
	    	if(GE_ARP_DEBUG)
			GE_printf(GE_ALARM,"GE_Arp_entry_index:malloc failed !\n");
	    	return ERROR;
	}        
	arp_index_reply = (ARP_INDEX_REPLY*)(pBuf + 12);        
        
	if( arp_index_req->indexType == 1)
    	{        
    		for (i = 0; i < port_num; i++)
        	{        
        		for(j = 0;j < 256; j++)
        		{
        			pEntry = (IP_MAC_BAND* )(hashValue[j].ArpList[i].next);
        			if ( pEntry != NULL)
        				break;
        		}
        		if ( pEntry != NULL)
        		{
        			arp_index_reply->interNum = i;
        	    		arp_index_reply->result = ERROR_NO_ERROR;
        	    		break;
        	    	}
        		else if( i==(port_num-1) )
        		{
        			arp_index_reply->result = TEMPT_OF_TABLE;
        		}
		}
    	}
	if( arp_index_req->indexType == 2)
    	{  	
    		for (i = 0; i < port_num; i++)
        	{      
			for(j = 0;j < 256;j++)
			{ 
				for ( 	pEntry = (IP_MAC_BAND* )(hashValue[j].ArpList[i].next);
					pEntry != NULL;
					pEntry = (IP_MAC_BAND* )(pEntry->next) )
				{				               
					if ( pEntry->proAddr > *(unsigned int *)(arp_index_req->ipAddr))         			
					{
						arp_index_reply->interNum = i;
						arp_index_reply->result = ERROR_NO_ERROR;
						
						i=port_num; 
						j=256;	/* get out the cycle of  for(i=0 ....*/
        		    			break;
					}
				}
        		}
        		
			if ( (pEntry==NULL) && (i==(port_num-1)) )
        		{
        			arp_index_reply->interNum = i;
        			arp_index_reply->result = END_OF_TABLE;	
        		}
        	}
    	}    	

	arp_index_reply->dataType = SYS_ARP_ENTRY_REPLY;
	arp_index_reply->dataLen = sizeof(ARP_INDEX_REPLY)-8;
	arp_index_reply->portNum = GE_swiport; 	    	
    	
    	if(pEntry!=NULL)
    	{
		bcopy(pEntry->PhyAddr,arp_index_reply->macAddr,MAC_ADD_SIZ);        
		arp_index_reply->ipAddr = pEntry->proAddr;
		arp_index_reply->liveTime = pEntry->liveTime; 	
        }

        if(BDCOM_UpReceive(	0, 
				NO_WAIT, 
				MSG_PRI_NORMAL, 
				sizeof(ARP_INDEX_REPLY)+ 12,
				pBuf, 
				12, 
				sizeof(ARP_INDEX_REPLY))==ERROR)
	{
		if(GE_ARP_DEBUG)
			GE_printf(GE_ALARM,"GE_Arp_entry_index:send ARP_INDEX_REPLY to BDCOM failed!\n");
		free(pBuf);
		return ERROR;
	}
	return OK;        	
}


/*******************************************************************************************
*
*/
STATUS  GE_SuspendArp()
{
	if(taskSuspend(SBSL_TaskIdGet(GE_ARP_TASK_CODE))==ERROR)
	{
		return  ERROR;
	}
	{
		if(SBSL_TaskStop(GE_ARP_TASK_CODE)==ERROR)
		{
			if(GE_ARP_DEBUG)
			        GE_printf(GE_ALARM,"GE_SuspendArp:TaskStop failed!\n");
			return ERROR;
		}
	}
	return  OK;
}
STATUS GE_ResumeArp()
{
	if(taskResume((SBSL_TaskIdGet(GE_ARP_TASK_CODE)))==ERROR)
	{
		return ERROR;
	}
	if(SBSL_TaskStart(GE_ARP_TASK_CODE,SBSL_TaskIdGet(GE_ARP_TASK_CODE))==ERROR)
	{
		if(GE_ARP_DEBUG)
		        GE_printf(GE_ALARM,"GE_ResumeArp:Resume task failed!\n");
		return ERROR;
	}
	return   OK;
}

/***********************************************************************************
*	
*	show arp table
*/
void show_arp(char unit)
{
	IP_MAC_BAND 	*pEntry;
    	unsigned int	liveTimes,ip;
    	unsigned char	mac[6];	
	unsigned int	camAddress;
    	unsigned int	i,j=0;
    	USHORT		sum[256];
    	
    	memset(sum,0,512);	
	printf("\n  DYNAMIC_ARP_TABLE--IP_MAC:\n");
    	printf("  ----------------------------------------------------------------------\n"); 

    	printf("  hashValue   liveTimes     ip             mac                camAddress\n");
    	
    	taskLock();
	for(i = 0;i < 256; i++)
	{
		pEntry =(IP_MAC_BAND* )(hashValue[i].ArpList[unit].next);
	    	if(pEntry== NULL)
	    	{
	    		sum[i]=0;
	    		continue;
	    	}    		
    		do
    		{			
    			liveTimes  = pEntry->liveTime;
    			ip 	   = pEntry->proAddr;
    			bcopy ((char*)pEntry->PhyAddr, (char*)mac,6);
			camAddress = pEntry->addrCount;
			sum[i]++ ;
			
            		printf ("  %-9d   %-9d     0x%.8x     0x%.2x%.2x%.2x%.2x%.2x%.2x     %d\n",
                    		  i,liveTimes,ip,mac[0],mac[1],mac[2],mac[3],mac[4],mac[5],camAddress );                        			
			
			pEntry = (IP_MAC_BAND* )(pEntry->next);					    
      		}
	      	
	      	while(pEntry!= NULL);
    	}
    	taskUnlock();
    	printf("  ----------------------------------------------------------------------\n\n"); 	
     	
	printf("  STATIC_ARP_TABLE--IP_MAC:\n");
    	printf("  ----------------------------------------------------------------------\n"); 	
    	for(i=0;i<static_table_sums;i++)
    	{
    		printf("  IP:0x%.8x",static_next_ip[i]);
    		printf("                          MAC:0x%.2x%.2x%.2x%.2x%.2x%.2x\n",
    		static_mac[i*6],static_mac[1+i*6],static_mac[2+i*6],static_mac[3+i*6],static_mac[4+i*6],static_mac[5+i*6]);
	}
    	printf("  ----------------------------------------------------------------------\n\n"); 		
  	
    	for(i=0;i<256;i++)
    	{
		if(sum[i]!=0)
		{
			printf("  %.3d--%.4d",i,sum[i]);
			if((++j)%8==0)
			{
				printf("\n");	
			}
		}
	}
		printf("\n\n");

    	return;    		
}

/***********************************************************************************
*	
*	show arp stat
*/
void arp_sta(char unit)
{
	printf("    --------------UNIT=%d-------------\n",unit);
	printf("    TYPE                         SUMS\n");
	printf("    ---------------------------------\n",unit);
	printf("    ARP_REQUEST_IN               %.4ld\n",(ULONG)arpInfo[unit].requestIn);
	printf("    ARP_REQUEST_OUT              %.4ld\n",(ULONG)arpInfo[unit].requestOut);
	printf("    ARP_REPLY_IN                 %.4ld\n",(ULONG)arpInfo[unit].replyIn);
	printf("    ARP_REPLY_OUT                %.4ld\n",(ULONG)arpInfo[unit].replyOut);
	printf("    ARP_ERROR                    %.4ld\n",(ULONG)arpInfo[unit].err);
	printf("    ---------------------------------\n",unit);
	
			arpInfo[unit].requestIn = 0;
			arpInfo[unit].requestOut = 0;
			arpInfo[unit].replyIn = 0;
			arpInfo[unit].replyOut = 0;
			arpInfo[unit].err = 0;
	return ;
}
/**********************************************************************************
*
*	arp_del() - find arp entry where protocol address match,and delete it.
*/
STATUS	arp_del(unsigned char unit, ULONG ipAddr)
{
   	IP_MAC_BAND 	*pEntry;
   	IP_MAC_BAND 	*pEntry_bak=NULL;
   	IP_MAC_BAND 	*pEntry_old;
	unsigned char 	i;
		    	     		     	
	i=hashFun(ipAddr);	
	taskSafe();
	if(semTake(GE_ArpSemId,WAIT_FOREVER)==OK)
	{
      	    	pEntry =(IP_MAC_BAND* )(hashValue[i].ArpList[unit].next);
	    	if(pEntry== NULL)
	    	{
	  		if(GE_ARP_DEBUG)
				GE_printf(GE_ALARM,"GE_ArpDel:The interface have not arpTable !\n");
			semGive(GE_ArpSemId);
			taskUnsafe();	
  			return ERROR;
	    	}
	    	do
	    	{			    	
			pEntry_old=pEntry_bak;
			pEntry_bak=pEntry;				    	
			if(pEntry->proAddr==ipAddr)
	    		{
	       			if(pEntry==(IP_MAC_BAND* )(hashValue[i].ArpList[unit].next))
		       		{
		       			(IP_MAC_BAND* )(hashValue[i].ArpList[unit].next)=pEntry->next;
		       			SBSL_MemFree(GE_ARP_TASK_CODE,(UCHAR*)pEntry);
	       			}	       		
	       			else 
	       			{
	       				pEntry_old->next=pEntry->next;
	       				SBSL_MemFree(GE_ARP_TASK_CODE,(UCHAR*)pEntry);	
	        		}
				if(cam_del(pEntry->addrCount)!=OK) 
				{
					if(GE_ARP_DEBUG)
				 	       GE_printf(GE_ALARM,"\n--------------- GE_ArpTimer: cam_delete error! -------\n");
				}
	    		}	    			
    			pEntry = (IP_MAC_BAND* )(pEntry->next);	   	
      	    	}while(pEntry!= NULL);
	}
    	semGive(GE_ArpSemId);	
	taskUnsafe();
	return OK;
} 
/* end of file GE_ARP.c */

⌨️ 快捷键说明

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