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

📄 tos.c

📁 ixp2400的一个小程序
💻 C
字号:
 

#include <dl_buf.c>
#include "tos.h"
 

#define MIN_L4_SIZE		       28
#define IP_PROTO_UDP	       17
#define IP_PROTO_TCP	       6

// Temporary, until compiler fixes problem
unsigned long long ua_get_64_dram(DRAM_I64 *p, 
								  unsigned int offset);


// This is the structure of the IP header
typedef __declspec(packed) struct 
{
	unsigned int version   		  :4;
	unsigned int header_len       :4;
	unsigned int tos	   		  :8;
	unsigned int total_len		  :16;
	unsigned int id				  :16;
	unsigned int flags			  :4;
	unsigned int frag_offset	  :12;
	unsigned int ttl			  :8;
	unsigned int protocol		  :8;
	unsigned int checksum		  :16;
	unsigned int source_addr;
	unsigned int destination_addr;
	unsigned int source_port      :16;  // UDP and TCP only
	unsigned int destination_port :16;  // UDP and TCP only
} ip_header;

// This structure exists to allow unaligned access to
// the IP header.
typedef __declspec(packed) struct
{
	unsigned long long header0;
	unsigned long long header1;
	unsigned long long header2;
 
} ip_header_mirror;

extern dl_buf_handle_t			dlBufHandle;
extern dl_meta_t				dlMeta;
extern __declspec(gp_reg) int 	dlNextBlock;

//-----------------------------
// Scratchpad addresses for total packet and byte counts
static __declspec(scratch) unsigned int *pkt_count_tos_small = 
		(__declspec(scratch) unsigned int *)4;
static __declspec(scratch) unsigned int *byte_count_tos_small = 
		(__declspec(scratch) unsigned int *)8;

static __declspec(scratch) unsigned int *pkt_count_tos_large = 
		(__declspec(scratch) unsigned int *)4;
static __declspec(scratch) unsigned int *byte_count_tos_large = 
		(__declspec(scratch) unsigned int *)8;
//-------------------------------------------------------------------
// read_unaligned_header
//
//    Description:
//      Retrieves the IP header from a packet stored unaligned in
//		DRAM.
//
//    Parameters:
//      Outputs: ip_header header      Pointer to the IP header 
//									   retrieved from unaligned DRAM.
//      In/Outs: n/a
//      Inputs:  ip_header header_ptr  Unaligned pointer into DRAM
//									   where the IP header begins.
//      Constants: n/a
//      Labels: n/a
//
//    Side effects: n/a
//
//    See also: n/a
//
//    Example Usage: read_unaligned_header(&header, header_ptr);
//
__forceinline static void read_unaligned_header(
				__declspec(dram) ip_header* header_ptr,
				ip_header*                  header);
__forceinline static void write_unaligned_header(
				__declspec(dram) ip_header* header_ptr,
				ip_header*               header);

//-------------------------------------------------------------------
// tos_init
//
 
void tos_init()
{
 
	*pkt_count_tos_small  = 0;
	*byte_count_tos_small = 0;
    *pkt_count_tos_large  = 0;
	*byte_count_tos_large = 0;


}

//-------------------------------------------------------------------
// tos impliment
 
//
void tos()
{
	__declspec(dram) ip_header*  header_ptr;
	ip_header                    header;
 //-----------for counter
	  __declspec(sram_write_reg) unsigned int
		buf_length_wr;
	   SIGNAL counter_sig;
//------------for caculate checksum
unsigned char tos_old;
unsigned short checksum_old;	
unsigned  int temp ;
   tos_old=0;
   checksum_old=0;
   temp=0;
	// Get the IP header
	header_ptr = (__declspec(dram) ip_header*)
	              (Dl_BufGetData(dlBufHandle) + 
	             dlMeta.offset);
   
 	read_unaligned_header(header_ptr, &header);
	
 //is the tos>31 or not? 

	if(header.tos<=31)  
	{
	//store the old tos value
	tos_old=header.tos;
	//increment the counter
	scratch_incr(pkt_count_tos_small);
	buf_length_wr = dlMeta.bufferSize;
	scratch_add(&buf_length_wr, byte_count_tos_small, 
				ctx_swap, &counter_sig);
	}  
	if (header.tos > 31)
	 { 
	 tos_old=header.tos;
	 //set new tos as 0 if tos>31
	 header.tos = 0;
	// Increment the counter
	scratch_incr(pkt_count_tos_large);
	buf_length_wr = dlMeta.bufferSize;
	scratch_add(&buf_length_wr, byte_count_tos_large, 
				ctx_swap, &counter_sig);
   
    //calculate the new checksum 
	//according to  --> HC'=~(~HC+~m+m')
    //get the checksum value;
   	checksum_old=header.checksum;
	// get ~HC
    checksum_old=~checksum_old;
    //get ~m
	tos_old=~tos_old;
	//get ~HC+~m
    temp=checksum_old+tos_old;
	//get ~HC+~m+m'
	temp=temp+header.tos;
	// get HC'
	temp=~temp;  
	//write back to header.checksum
	header.checksum=temp;
	}		
			 
	//put the new tos and checksum back to dram
	write_unaligned_header(header_ptr, &header);
	
    read_unaligned_header(header_ptr, &header);
	dlNextBlock = COUNT_NEXT_BLOCK;
}

//-------------------------------------------------------------------
// read_unaligned_header
//
//    Description:
//      Retrieves the IP header from a packet stored unaligned in
//		DRAM.
//
//    Parameters:
//      Outputs: ip_header header      Pointer to the IP header 
//									   retrieved from unaligned DRAM.
//      In/Outs: n/a
//      Inputs:  ip_header header_ptr  Unaligned pointer into DRAM
//									   where the IP header begins.
//      Constants: n/a
//      Labels: n/a
//
//    Side effects: n/a
//
//    See also: n/a
//
//    Example Usage: read_unaligned_header(&header, header_ptr);
//
__forceinline static void read_unaligned_header(
				__declspec(dram) ip_header* header_ptr,
				ip_header*                  header)
{
	ip_header_mirror* mirror = (ip_header_mirror*)header;
	unsigned int offset = (unsigned int)header_ptr & 0x7;
	__declspec(dram) long long* base_ptr = 
		(__declspec(dram) long long*)
		  ((__declspec(dram) char*)header_ptr - offset);
	unsigned long long data;
	data = ua_get_64_dram(base_ptr, offset);
	mirror->header0 = data;
	data = ua_get_64_dram(base_ptr + 1, offset);
	mirror->header1 = data;
	data = ua_get_64_dram(base_ptr + 2, offset);
	mirror->header2 = data;

}
// write the ip header values to dram
__forceinline static void write_unaligned_header(
				__declspec(dram) ip_header* header_ptr,
				ip_header*               header)
{  
	unsigned int offset = (unsigned int)header_ptr & 0x7;
	__declspec(dram) long long* base_ptr = 
		(__declspec(dram) long long*)
		  ((__declspec(dram) char*)header_ptr - offset);
	 unsigned long long *data;
	  data=(unsigned long long )header;
	 ua_set_64_dram(base_ptr, offset, *data);
     data=data+1;
	 ua_set_64_dram(base_ptr + 1, offset,*data);
	 data=data+1;
	 ua_set_64_dram(base_ptr + 2, offset,*data);
	 

}
 

⌨️ 快捷键说明

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