Upload
reynard-heath
View
215
Download
2
Embed Size (px)
Citation preview
kernel mode linux
Why?
● The pros– There is a C like interface inside the Linux kernel
already.– The speed.– The size of AP.
● The cons– Missing functions– complex dependencies
What do we have?
● basic system calls– open,read,write,
● string handling routines– strnicmp,strcpy, strcat,
strncat,strcmp, ,strncmp,strchr,strrchr,strlen,strnlen,strspn,strpbrk,strsep,memset,bcopy,memcpy,memmove,memcmp,memscan,strstr,memchr,
● strtoul,strtol,strtoull,strtoll,vsnprintf,printk,sprintf,vsprintf,vsscanf,sscanf,
Create a kernel thread● create_kernel: clone with CLONE_VM
– int (*fn)(void *)– void * arg– unsigned long flags
● CLONE_VFORK– wait the child finish before return
● CLONE_VM– Use the same VM as the parent instead creating a private copy.
● CLONE_FS– Use the same filesystem as the parent
● CLONE_FILES– Use the same file descriptors as the parent
do_fork
● CLONE_SIGHAND– Use the same signal handler as the parent
● CLONE_PTRACE– If the clinet will be traced after the do_fork()
● CLONE_PID– Use the same PID as the paqrent. Only used in the
SMP system to create the init ● CLONE_THREAD,CLONE_PARENT
– Used in the thread. It will set the parent of the thread as the previous thread.
Example: kernel thread
● kernel_thread(thread_func,&data,CLONE_FS|CLONE_FILES|CLONE_SIGHAND)– Execute thread_func in the new thread with data as
argument.– Clone the FS/FILES and Signal handler.
● Like fork
Access the file
● The easy way– open,read,write are provided.– It's a system call. Slow
● The hard way– do_generic_file_read– do_generic_file_write– filp_open
Access the network
● sock_create ===> socket● sock->ops->bind ====> bind● sock->ops->listen ===> listen● sock_release ===> close● sock_recvmsg ===> recv● sock_sendmsg ===> send
khttpd
● It's a kernel level web server, which can handle the static web pgae acces.
● It's extra fast.● All dynamic URL requests will be redirected to
the user space.● Exec: Setup a web site by using khttpd and then
forward a dynamic URL request to a kernel space CGI.
How to setup the khttpd
● Enable khttpd and rebuild the Linux kernel● Setup server port
– echo 8080 > /proc/sys/net/khttpd/serverport● Setup the root directory of HTML pages
– echo /html > /proc/sys/net/khttpd/documentroot● Start the daemon
– echo 1 > /proc/sys/net/khttpd/start
The management daemon
● initialize the globval variables● wait for activation● fork threads● wait for deactivation
The MainDaemon
● Wait for new connection● handle connection in DataSending queue and
UserSpace queue● Log connections if necessary
Connection handling
● Accept connections and put them into either datasending queue or user space queue.
● Datasending Queue– Static page
● User Space Queue– Dynamic page
Static Page
● Open the file by using filp_open.● sock_wspace: get the size of unused buffer in a
socket.● send data by using sendfile or read/send
depending on the capability of the filesystem.
Dynamic page
● Get the user space daemon– tcp_v4_lookup_listener
● transfer the socket to the user space daemon– tcp_acceptq_queue