major changes

This commit is contained in:
Carl-Benedikt Krüger 2011-08-04 15:42:56 +02:00
parent 070fffd9c0
commit 88dddd08a7

View file

@ -14,48 +14,35 @@
#include "mmnif.h" /* definitions */
#ifdef WIN32
#define kmalloc malloc
#define kfree(x,y) free(x)
#define RCCE_shfree(x) VirtualFree(x,NULL,NULL);
#define RCCE_shmalloc(x) VirtualAlloc((char*)0x41000000,x,MEM_COMMIT|MEM_RESERVE,PAGE_READWRITE);
#include "mailbox.h" /* mailbox_ptr_t */
#else
#include <metalsvm/mailbox.h> /* mailbox_ptr_t */
#endif
#include <lwip/netif.h> /* lwip netif */
#include <lwip/stats.h> /* inteface stats */
#include <netif/etharp.h> /* ethernet arp packets */
#include <lwip/ip.h> /* struct iphdr*/
#include <lwip/tcpip.h> /* tcpip_input()*/
#define DEBUG_MMNIF
//#define DEBUG_MMNIF_PACKET
#ifdef DEBUG_MMNIF
#include "util.h" /* hex dump */
#endif
#ifdef WIN32
#define kmalloc malloc
#define kfree(x,y) free(x)
#define RCCE_shfree(x) VirtualFree(x,NULL,NULL);
#define RCCE_shmalloc(x) VirtualAlloc((char*)0x41000000,x,MEM_COMMIT|MEM_RESERVE,PAGE_READWRITE);
#include "mailbox.h" /* mailbox_ptr_t */
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <system/threads/bthread.h>
typedef bthread_sem_t sem_t;
typedef bthread_t tid_t;
/* "interrupt" of the other virutal network card*/
extern HANDLE remote_process_event;
extern HANDLE remote_process_mutex;
extern HANDLE own_process_mutex;
/* HANDLE to the other Process (for WPM and RPM)*/
extern HANDLE hProc;
#define DEBUGPRINTF(x,...) printf(x,__VA_ARGS__)
#else
#define DEBUGPRINTF(x,...) kprintf(x,##__VA_ARGS__)
#include <metalsvm/mailbox.h> /* mailbox_ptr_t */
#include <metalsvm/semaphore.h>
#include <metalsvm/spinlock.h>
@ -69,6 +56,25 @@ extern HANDLE hProc;
#include <metalsvm/time.h>
#define DEBUGPRINTF(x,...) kprintf(x,##__VA_ARGS__)
#endif
#define DEBUG_MMNIF
//#define DEBUG_MMNIF_PACKET
#ifdef DEBUG_MMNIF
#include "util.h" /* hex dump */
#endif
#ifdef WIN32
#else
#endif
@ -493,8 +499,9 @@ int mmnif_commit_packet(uint8_t dest,uint32_t addr)
}
return -1;
}
/*
*
/* mmnif_rxbuff_free() : the opposite to mmnif_rxbuff_alloc() a from the receiver
* already processed chunk of memory is freed so that it can be allocated again
*/
void mmnif_rxbuff_free()
{
@ -546,22 +553,20 @@ void mmnif_rxbuff_free()
*/
err_t mmnif_tx(struct netif* netif, struct pbuf* p)
{
mmnif_t* mmnif = netif->state;
uint8_t slot = mmnif->tx_queue;
mmnif_t* mmnif = netif->state;
uint8_t slot = mmnif->tx_queue;
uint16_t queued;
uint32_t write_address;
uint32_t i;
uint32_t write_address;
uint32_t i;
struct pbuf* q; /* interator */
uint8_t build_buff = TRUE;
uint8_t dest_intr = FALSE;
uint32_t dest_ip = mmnif_get_destination(netif,p);
uint8_t build_buff = TRUE;
uint32_t dest_ip = mmnif_get_destination(netif,p);
mm_rx_buffer_t* rb = (mm_rx_buffer_t*)((char*)mpb_start_address + ( dest -1 ) * mpb_size);
#ifdef WIN32
ReadProcessMemory(hProc, (char*)mpb_start_address + ( dest_ip -1 ) * mpb_size,
(char*)mpb_start_address + ( dest_ip -1 ) * mpb_size,mpb_size,NULL);
ReadProcessMemory(hProc, (char*)mpb_start_address + ( dest_ip -1 ) * mpb_size, (char*)mpb_start_address + ( dest_ip -1 ) * mpb_size,mpb_size,NULL);
WaitForSingleObject(remote_process_mutex,INFINITE);
#endif
@ -577,14 +582,14 @@ err_t mmnif_tx(struct netif* netif, struct pbuf* p)
if (mmnif->tx_queue > MMNIF_TX_QUEUELEN)
{
DEBUGPRINTF("mmnif_tx(): too many packets at once for tx_queue\n");
goto drop_packet;
DEBUGPRINTF("mmnif_tx(): too many packets at once for tx_queue\n");
goto drop_packet;
}
if (p->tot_len > MMNIF_TX_BUFFERLEN)
{
DEBUGPRINTF("mmnif_tx(): packet is longer than %d bytes\n",MMNIF_TX_BUFFERLEN);
goto drop_packet;
DEBUGPRINTF("mmnif_tx(): packet is longer than %d bytes\n",MMNIF_TX_BUFFERLEN);
goto drop_packet;
}
/* check if the pbuf consists only of one element
@ -594,7 +599,6 @@ err_t mmnif_tx(struct netif* netif, struct pbuf* p)
if (!p->next)
build_buff = FALSE;
if (build_buff)
{
/* build the payload out of the p's
@ -607,7 +611,7 @@ err_t mmnif_tx(struct netif* netif, struct pbuf* p)
}
}
/* lookup writeoffset and allocate needed space in the remote buffer */
/* allocate memory for the packet in the remote buffer */
write_address = mmnif_rxbuff_alloc(dest_ip,p->tot_len);
@ -626,7 +630,7 @@ err_t mmnif_tx(struct netif* netif, struct pbuf* p)
if (mmnif_commit_packet(dest_ip,write_address))
{
DEBUGPRINTF("mmnif_tx(): packet somehow lost on commit\n");
DEBUGPRINTF("mmnif_tx(): packet somehow lost during commit\n");
}
#ifdef DEBUG_MMNIF_PACKET
@ -634,30 +638,31 @@ err_t mmnif_tx(struct netif* netif, struct pbuf* p)
hex_dump(p->tot_len, p->payload);
#endif
/* release the tx_queue because it's finished */
spinlock_lock(&mmnif->lock);
mmnif->tx_queue--;
spinlock_unlock(&mmnif->lock);
/* just gather some stats */
LINK_STATS_INC(link.xmit);
mmnif->stats.tx++;
mmnif->stats.tx_bytes += p->tot_len;
#ifdef WIN32
WriteProcessMemory(hProc, (char*)mpb_start_address + ( dest_ip -1 ) * mpb_size,
(char*)mpb_start_address + ( dest_ip -1 ) * mpb_size,mpb_size,NULL);
WriteProcessMemory(hProc, (char*)mpb_start_address + ( dest_ip -1 ) * mpb_size, (char*)mpb_start_address + ( dest_ip -1 ) * mpb_size,mpb_size,NULL);
SetEvent(remote_process_event);
ReleaseMutex(remote_process_mutex);
#endif
mmnif_trigger_irq(dest_ip);
if (rb->iv_intr)
mmnif_trigger_irq(dest_ip);
return ERR_OK;
drop_packet:
/* drop packet for one or another reason
*/
spinlock_lock(&mmnif->lock);
mmnif->tx_queue--;
spinlock_unlock(&mmnif->lock);
@ -666,11 +671,10 @@ drop_packet:
mmnif->stats.tx_err++;
#ifdef WIN32
WriteProcessMemory(hProc, (char*)mpb_start_address + ( dest_ip -1 ) * mpb_size,
(char*)mpb_start_address + ( dest_ip -1 ) * mpb_size,mpb_size,NULL);
WriteProcessMemory(hProc, (char*)mpb_start_address + ( dest_ip -1 ) * mpb_size,(char*)mpb_start_address + ( dest_ip -1 ) * mpb_size,mpb_size,NULL);
#endif
return ERR_IF;
return ERR_IF;
}
/* mmnif_link_layer(): wrapper function called by ip_output()
@ -707,7 +711,6 @@ err_t mmnif_init(struct netif* netif)
/* Alloc and clear shared memory for rx_buff
*/
mpb_size = (sizeof(mm_rx_buffer_t) + MMNIF_RX_BUFFERLEN);
mpb_start_address = RCCE_shmalloc(mpb_size*MMNIF_CORES);
@ -719,6 +722,8 @@ err_t mmnif_init(struct netif* netif)
}
memset(mmnif->rx_buff, 0, mpb_size);
/* set initial values
*/
mmnif->rx_buff->dcount = MMNIF_MAX_DESCRIPTORS;
/* init the lock's for the hdr
@ -791,42 +796,39 @@ err_t mmnif_init(struct netif* netif)
*/
static void mmnif_rx(struct netif* netif)
{
mmnif_t* mmnif = netif->state;
mmnif_t* mmnif = netif->state;
mm_rx_buffer_t* b = mmnif->rx_buff;
uint16_t length;
uint16_t length;
struct pbuf* p = NULL;
struct pbuf* q;
char* packet;
char* packet;
uint16_t l1,l2,remaining;
uint32_t i,j;
uint8_t rdesc = 0xFF;
uint32_t i,j;
uint8_t rdesc = 0xFF;
err_t err = NULL;
err_t err = NULL;
#ifdef WIN32
ReadProcessMemory(hProc, (char*)mpb_start_address + ( !((own_ip_address && 0xFF)-1) ) * mpb_size,
(char*)mpb_start_address + ( !((own_ip_address && 0xFF)-1) ) * mpb_size,mpb_size,NULL);
ReadProcessMemory(hProc, (char*)mpb_start_address + ( !((own_ip_address && 0xFF)-1) ) * mpb_size, (char*)mpb_start_address + ( !((own_ip_address && 0xFF)-1) ) * mpb_size,mpb_size,NULL);
WaitForSingleObject(own_process_mutex,INFINITE);
#endif
spinlock_lock(&b->rlock);
/* check if this call to mmnif_rx makes any sense
*/
if (b->desc_table[b->dread].stat == MMNIF_STATUS_FREE)
{
spinlock_unlock(&b->rlock);
#ifdef WIN32
WriteProcessMemory(hProc, (char*)mpb_start_address + ( !((own_ip_address && 0xFF)-1) ) * mpb_size,
(char*)mpb_start_address + ( !((own_ip_address && 0xFF)-1) ) * mpb_size,mpb_size,NULL);
WriteProcessMemory(hProc, (char*)mpb_start_address + ( !((own_ip_address && 0xFF)-1) ) * mpb_size, (char*)mpb_start_address + ( !((own_ip_address && 0xFF)-1) ) * mpb_size,mpb_size,NULL);
#endif
return;
}
/* search the packet whose transmission is finished
*/
for (i = 0,j = b->dread; i < MMNIF_MAX_DESCRIPTORS; i++)
{
if (b->desc_table[(j + i)% MMNIF_MAX_DESCRIPTORS].stat == MMNIF_STATUS_RDY)
@ -841,16 +843,18 @@ static void mmnif_rx(struct netif* netif)
spinlock_unlock(&b->rlock);
/* if there is no packet finished we encountered a random error
*/
if (rdesc == 0xFF)
{
#ifdef WIN32
WriteProcessMemory(hProc, (char*)mpb_start_address + ( !((own_ip_address && 0xFF)-1) ) * mpb_size,
(char*)mpb_start_address + ( !((own_ip_address && 0xFF)-1) ) * mpb_size,mpb_size,NULL);
WriteProcessMemory(hProc, (char*)mpb_start_address + ( !((own_ip_address && 0xFF)-1) ) * mpb_size, (char*)mpb_start_address + ( !((own_ip_address && 0xFF)-1) ) * mpb_size,mpb_size,NULL);
#endif
return;
}
/* If length is zero return silently */
/* If length is zero return silently
*/
if (length == 0)
{
DEBUGPRINTF("mmnif_rx(): empty packet error\n");
@ -888,15 +892,17 @@ static void mmnif_rx(struct netif* netif)
i +=q->len;
}
/* indicate that the copy process is done and the packet can be freed
* note that we did not lock here because we are the only one editing this value
*/
mmnif->rx_buff->desc_table[rdesc].stat = MMNIF_STATUS_PROC;
/* everything is copied to a new buffer so it's save to release
* the old one for new incoming packets
*/
mmnif->rx_buff->desc_table[rdesc].stat = MMNIF_STATUS_PROC;
mmnif_rxbuff_free();
/* full packet send to tcpip_thread to process */
if ((err = mmnif_dev->input(p, mmnif_dev)) != ERR_OK)
{
@ -916,9 +922,7 @@ static void mmnif_rx(struct netif* netif)
mmnif->stats.rx_poll++;
#ifdef WIN32
WriteProcessMemory(hProc, (char*)mpb_start_address + ( !((own_ip_address && 0xFF)-1) ) * mpb_size,
(char*)mpb_start_address + ( !((own_ip_address && 0xFF)-1) ) * mpb_size,mpb_size,NULL);
WriteProcessMemory(hProc, (char*)mpb_start_address + ( !((own_ip_address && 0xFF)-1) ) * mpb_size, (char*)mpb_start_address + ( !((own_ip_address && 0xFF)-1) ) * mpb_size,mpb_size,NULL);
ReleaseMutex(own_process_mutex);
#endif
@ -930,12 +934,11 @@ drop_packet:
/*error handling*/
spinlock_unlock(&mmnif->rx_buff->rlock);
LINK_STATS_INC(link.drop);
LINK_STATS_INC(link.drop);
mmnif->stats.rx_err++;
#ifdef WIN32
WriteProcessMemory(hProc, (char*)mpb_start_address + ( !((own_ip_address && 0xFF)-1) ) * mpb_size,
(char*)mpb_start_address + ( !((own_ip_address && 0xFF)-1) ) * mpb_size,mpb_size,NULL);
WriteProcessMemory(hProc, (char*)mpb_start_address + ( !((own_ip_address && 0xFF)-1) ) * mpb_size, (char*)mpb_start_address + ( !((own_ip_address && 0xFF)-1) ) * mpb_size,mpb_size,NULL);
#endif
return;
}
@ -944,6 +947,7 @@ drop_packet:
void mmnif_irqhandler()
{
mmnif_t* mmnif;
/* return if mmnif_dev is not yet initialized*/
if (!mmnif_dev)
{
@ -993,7 +997,7 @@ int mmnif_open()
* Note: Ethernet Input will be removed because its NOT needed and will
* be replaced with ip_input
*/
if (!netif_add(mmnif_dev, &ipaddr, &netmask, &gw, NULL,(netif_init_fn)mmnif_init, tcpip_input/*ethernet_input*/))
if (!netif_add(mmnif_dev, &ipaddr, &netmask, &gw, NULL,(netif_init_fn)mmnif_init, tcpip_input))
{
DEBUGPRINTF("mmnif_open() : unable to add network interface\n");
return -1;
@ -1015,8 +1019,8 @@ int mmnif_open()
active = TRUE;
/* If interrupts are not used we immediately add the polling function
* to the queue which would otherwise be done through the IRQ handler.
*/
* to the queue which would otherwise be done through the IRQ handler.
*/
// mmnif_device_schedule();
/* Start the device worker thread wich actually processes the incoming