From c29ddaf5a4acb51469c9a4ff10bd0dde88872c46 Mon Sep 17 00:00:00 2001 From: "John Hodge (sonata)" Date: Sat, 26 Jan 2013 17:40:53 +0800 Subject: [PATCH] NetTest - Compiles, doesn't yet do much --- Tools/NetTest/Makefile | 3 +- Tools/NetTest/include/nettest.h | 20 +++++++++ Tools/NetTest/main.c | 57 ++++++++++++++++++++++++ Tools/NetTest/nic.c | 78 +++++++++++++++++++++++++++++++++ Tools/NetTest/tap.c | 60 +++++++++++++++++++++++++ Tools/NetTest/vfs_shim.c | 48 ++++++++++++++++++++ Tools/nativelib/Makefile | 15 +++++-- Tools/nativelib/threads.c | 6 +++ 8 files changed, 282 insertions(+), 5 deletions(-) create mode 100644 Tools/NetTest/include/nettest.h diff --git a/Tools/NetTest/Makefile b/Tools/NetTest/Makefile index 1679a283..45e76b2f 100644 --- a/Tools/NetTest/Makefile +++ b/Tools/NetTest/Makefile @@ -14,7 +14,8 @@ MODULE_SRC = ../../KernelLand/Modules/ BIN = ../nettest # Kernel Sources (compiled with -ffreestanding) K_OBJ := lib.o adt.o -#K_OBJ += vfs/main.o vfs/open.o vfs/acls.o vfs/io.o vfs/dir.o +K_OBJ += vfs/acls.o vfs/io.o vfs/fs/devfs.o +#K_OBJ += vfs/main.o vfs/open.o vfs/io.o vfs/dir.o #K_OBJ += vfs/nodecache.o vfs/mount.o vfs/memfile.o # vfs/select.o #K_OBJ += vfs/fs/root.o vfs/fs/devfs.o #K_OBJ += drvutil_disk.o drv/proc.o diff --git a/Tools/NetTest/include/nettest.h b/Tools/NetTest/include/nettest.h new file mode 100644 index 00000000..c8d16b8f --- /dev/null +++ b/Tools/NetTest/include/nettest.h @@ -0,0 +1,20 @@ +/* + * Acess2 Networking Test Suite (NetTest) + * - By John Hodge (thePowersGang) + * + * nettest.h + * - Common functions + */ +#ifndef _NETTEST_H_ +#define _NETTEST_H_ + +#include + +extern int NativeNic_AddDev(char *Desc); + +extern void *NetTest_OpenTap(const char *Name); +extern size_t NetTest_WritePacket(void *Handle, size_t Size, const void *Data); +extern size_t NetTest_ReadPacket(void *Handle, size_t MaxSize, void *Data); + +#endif + diff --git a/Tools/NetTest/main.c b/Tools/NetTest/main.c index e69de29b..d614da6c 100644 --- a/Tools/NetTest/main.c +++ b/Tools/NetTest/main.c @@ -0,0 +1,57 @@ +/* + * Acess2 Networking Test Suite (NetTest) + * - By John Hodge (thePowersGang) + * + * main.c + * - Program Core + */ +#include +#include +#include +#include + +extern int IPStack_Install(char **Args); + +// === CODE === +void PrintUsage(const char *ProgramName) +{ + fprintf(stderr, "Usage: %s \n", ProgramName); + fprintf(stderr, "\n"); + fprintf(stderr, + "-dev :\n" + "-ip ,,\n" + "-route ,,\n" + ); +} + +int main(int argc, char *argv[]) +{ + if( argc <= 1 ) { + PrintUsage(argv[0]); + return 1; + } + + // Startup + { + char *ipstack_args[] = {NULL}; + IPStack_Install( ipstack_args ); + } + + for( int i = 0; i < argc; i ++ ) + { + if( argv[i][0] != '-' ) { + } + else if( strcmp(argv[i], "-dev") == 0 ) + { + if( ++i == argc ) { PrintUsage(argv[0]); return 1; } + NativeNic_AddDev(argv[i]); + } + } + + // Run suite + + + // Teardown + + return 0; +} diff --git a/Tools/NetTest/nic.c b/Tools/NetTest/nic.c index e69de29b..1e736c2b 100644 --- a/Tools/NetTest/nic.c +++ b/Tools/NetTest/nic.c @@ -0,0 +1,78 @@ +/* + * Acess2 Networking Test Suite (NetTest) + * - By John Hodge (thePowersGang) + * + * nic.c + * - Acess -> TAP Wrapper + */ +#include +#include +#include + +// === CONSTANTS === +static const int MTU = 1520; + +// === PROTOTYPES === +void NativeNic_int_FreePacket(void *Ptr, size_t pkt_length, size_t Unused, const void *DataPtr); +tIPStackBuffer *NativeNic_WaitForPacket(void *Ptr); + int NativeNic_SendPacket(void *Ptr, tIPStackBuffer *Buffer); + +// === GLOBALS === +tIPStack_AdapterType gNativeNIC_AdapterType = { + .Name = "NativeNIC", + .Type = 0, // TODO: Differentiate differnet wire protos and speeds + .Flags = 0, // TODO: IP checksum offloading, MAC checksum offloading etc + .SendPacket = NativeNic_SendPacket, + .WaitForPacket = NativeNic_WaitForPacket + }; + +// === CODE === +int NativeNic_AddDev(char *DevDesc) +{ + Uint8 macaddr[6]; + char *colonpos = strchr(DevDesc, ':'); + if( !colonpos ) + return -1; + *colonpos = 0; + if( UnHex(macaddr, 6, DevDesc) != 6 ) + return -1; + void *ptr = NetTest_OpenTap(colonpos+1); + if( !ptr ) + return 1; + IPStack_Adapter_Add(&gNativeNIC_AdapterType, ptr, macaddr); + + return 0; +} + +void NativeNic_int_FreePacket(void *Ptr, size_t pkt_length, size_t Unused, const void *DataPtr) +{ + free( (void*)DataPtr ); +} + +tIPStackBuffer *NativeNic_WaitForPacket(void *Ptr) +{ + char *buf = malloc( MTU ); + size_t len; + + len = NetTest_ReadPacket(Ptr, MTU, buf); + + tIPStackBuffer *ret = IPStack_Buffer_CreateBuffer(1); + IPStack_Buffer_AppendSubBuffer(ret, len, 0, buf, NativeNic_int_FreePacket, Ptr); + return ret; +} + +int NativeNic_SendPacket(void *Ptr, tIPStackBuffer *Buffer) +{ + size_t len = IPStack_Buffer_GetLength(Buffer); + + // Check against MTU + if( len > MTU ) + return -1; + + // Serialise into stack + char buf[len]; + IPStack_Buffer_GetData(Buffer, buf, len); + + NetTest_WritePacket(Ptr, len, buf); + return 0; +} diff --git a/Tools/NetTest/tap.c b/Tools/NetTest/tap.c index e69de29b..ebacf163 100644 --- a/Tools/NetTest/tap.c +++ b/Tools/NetTest/tap.c @@ -0,0 +1,60 @@ +/* + * Acess2 Networking Test Suite (NetTest) + * - By John Hodge (thePowersGang) + * + * tap.c + * - TAP Network driver + */ +#include +#include +#include +//#include +#include +#include +#include +#include +#include + +// === CODE === +void *NetTest_OpenTap(const char *Name) +{ + int rv; + + struct ifreq ifr; + memset(&ifr, 0, sizeof(ifr)); + ifr.ifr_flags = IFF_TAP|IFF_NO_PI; + + if( Name[0] != '\0' ) + { + if( strlen(Name) > IFNAMSIZ ) + return NULL; + strncpy(ifr.ifr_name, Name, IFNAMSIZ); + } + + int fd = open("/dev/net/tun", O_RDWR); + if( fd < 0 ) + { + perror("NetTest_OpenTap - open"); + return NULL; + } + + if( (rv = ioctl(fd, TUNSETIFF, &ifr)) ) + { + perror("NetTest_OpenTap - ioctl(TUNSETIFF)"); + fprintf(stderr, "Opening TUN/TAP device '%s'\n", Name); + close(fd); + return NULL; + } + + return (void*)(intptr_t)fd; +} + +size_t NetTest_WritePacket(void *Handle, size_t Size, const void *Data) +{ + return write( (intptr_t)Handle, Data, Size); +} + +size_t NetTest_ReadPacket(void *Handle, size_t MaxSize, void *Data) +{ + return read( (intptr_t)Handle, Data, MaxSize); +} diff --git a/Tools/NetTest/vfs_shim.c b/Tools/NetTest/vfs_shim.c index e69de29b..bc0c67c3 100644 --- a/Tools/NetTest/vfs_shim.c +++ b/Tools/NetTest/vfs_shim.c @@ -0,0 +1,48 @@ +/* + * Acess2 Networking Test Suite (NetTest) + * - By John Hodge (thePowersGang) + * + * vfs_shim.c + * - VFS Layer Emulation + */ +#include +#include +#include + +// === CODE === +int VFS_SelectNode(tVFS_Node *Node, int Type, tTime *Timeout, const char *Name) +{ + + return 0; +} + +int VFS_MarkAvaliable(tVFS_Node *Node, BOOL bAvail) +{ + Node->DataAvaliable = bAvail; + if( Node->DataAvaliable && Node->ReadThreads ) + Threads_PostEvent( (void*)Node->ReadThreads, THREAD_EVENT_VFS ); + return 0; +} + +int VFS_MarkError(tVFS_Node *Node, BOOL bError) +{ + Node->ErrorOccurred = bError; + if( Node->ErrorOccurred && Node->ErrorThreads ) + Threads_PostEvent( (void*)Node->ErrorThreads, THREAD_EVENT_VFS ); + return 0; +} + +int VFS_Open(const char *Path, Uint Flags) +{ + return -1; +} + +void VFS_Close(int FD) +{ +} + +tVFS_Handle *VFS_GetHandle(int FD) +{ + return NULL; +} + diff --git a/Tools/nativelib/Makefile b/Tools/nativelib/Makefile index 2a6eb6df..84ac3289 100644 --- a/Tools/nativelib/Makefile +++ b/Tools/nativelib/Makefile @@ -3,6 +3,9 @@ KERNEL_DIR := ../../KernelLand/Kernel NOBJ := logging.o misc.o KOBJ := threads.o time.o mutex.o + +NOBJ := $(NOBJ:%.o=obj/%.o) +KOBJ := $(KOBJ:%.o=obj/%.o) OBJ := $(NOBJ) $(KOBJ) BIN := ../libnativelib.a @@ -20,8 +23,12 @@ clean: $(BIN): $(OBJ) ar cru $(BIN) $(OBJ) -$(NOBJ): %.o: %.c - $(CC) -o $@ -c $< $(CFLAGS) $(CPPFLAGS) -$(KOBJ): %.o: %.c - $(CC) -o $@ -c $< $(CFLAGS) $(CPPFLAGS) -I $(KERNEL_DIR)/include +$(NOBJ): obj/%.o: %.c + @echo [CC Native] $@ + @mkdir -p $(dir $@) + @$(CC) -o $@ -c $< $(CFLAGS) $(CPPFLAGS) +$(KOBJ): obj/%.o: %.c + @echo [CC Kernel] $@ + @mkdir -p $(dir $@) + @$(CC) -o $@ -c $< $(CFLAGS) $(CPPFLAGS) -I $(KERNEL_DIR)/include diff --git a/Tools/nativelib/threads.c b/Tools/nativelib/threads.c index 5055dc3e..501954de 100644 --- a/Tools/nativelib/threads.c +++ b/Tools/nativelib/threads.c @@ -63,3 +63,9 @@ int *Threads_GetErrno(void)// __attribute__ ((weak)) return &a_errno; } +struct sThread *Proc_SpawnWorker(void (*Fcn)(void*), void *Data) +{ + Log_Error("Threads", "TODO - Use pthreads to impliment Proc_SpawnWorker"); + return NULL; +} + -- 2.20.1