@hackage tcp0.0.2

A purely functional TCP implementation

Requirements:

  • Linux 2.6.x

  • GHC 6.5

  • libaio --- the Linux Asynchonrous I/O Library.

  • libipq --- the iptables development library (iptables-dev in Debian)

    If you wish to try the web server, make sure that the HTML files are on one of these file systems: ext2, ext3, jfs, xfs. Otherwise, AIO may not work.

Files and directories:

IOLib/ : Low-level asynchronous I/O library Chunk.hs : A type-safe representation of memory buffers AIO.hs : FFI wrappers for the Linux AIO library Epoll.hs : FFI wrappers for the Linux epoll interface NBIO.hs : FFI wrappers for some non-blocking socket primitives PacketIO.hs : FFI wrappers for sending/receiving IP packets. TCP.hs : A proof-of-concept, purely functional TCP stack.

ThreadLib/ : The application-level thread library Thread.hs : CPS Monad and system calls. Workers.hs : Some standard and reusable event loops for constructing application-level schedulers. A default scheduler implementation is also provided. FileIO.hs : Synchronous disk library, implemented using Linux AIO SockIO.hs : Synchronous socket library, implemented using standard non-blocking sockets with epoll. TCPSockIO.hs : Synchronous socket library, implemented using the user-level, customized TCP stack. Mutex.hs : A simple mutex library.

WebClient/ : An HTTP load generator application

WebServer/ : A web server application. It can be linked with either SockIO or TCPSockIO.

Compile and run:

First, make sure that libaio is installed on your system and you are using a development snapshot GHC 6.5. Then, run the shell script "rebuild.sh" to compile all the sources. This should create three executable files:

WebServer/Server.bin
WebServer/Server_tcp.bin
WebClient/LoadGen.bin

Run them. They are self-documented. To use Server_tcp.bin, you should first run "make setup" in the WebServer/ directory to setup iptables. Read the Makefile for more information.

Programming with threads

Just use Server.hs and LoadGen.hs as templates to start.

Programming with events

The scheduler is part of your application. You can use some standard event loops predefined in Workers.hs, or you can write your own.

FAQ:

Q: How to use another event source like *poll/*IO/kqueue/whatever?

A: First, wrap up the C function calls using FFI. AIO.hs and mod_aio.c is a good demo to start with. Then, add system calls and write your own event loops :-)

Q: How to add system calls?

A: The current setup is not quite modular yet---you will have to add a line in Thread.hs for each system call you would like to add. In the future, it is possible to make the Trace interface user-pluggable by adding an extra level of indirection.

Q: How to write my own event loops?

A: As a demo, LoadGen.hs has a customized event loop. It does not do anything fancy, though.

Q: How is the TCP stack?

A: The current implementation is purely functional and it is quite slow. It is roughly 3x-8x slower than the linux kernel TCP stack. It only implemented server sockets---active TCP connections are not implemented yet.