kernel mode linux. why? ● the pros – there is a c like interface inside the linux kernel...

15
kernel mode linux

Upload: reynard-heath

Post on 13-Dec-2015

215 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 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

kernel mode linux

Page 2: 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

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

Page 3: 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

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,

Page 4: 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

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

Page 5: 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

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.

Page 6: 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

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

Page 7: 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

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

Page 8: 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

Access the network

● sock_create ===> socket● sock->ops->bind ====> bind● sock->ops->listen ===> listen● sock_release ===> close● sock_recvmsg ===> recv● sock_sendmsg ===> send

Page 9: 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

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.

Page 10: 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

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

Page 11: 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

The management daemon

● initialize the globval variables● wait for activation● fork threads● wait for deactivation

Page 12: 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

The MainDaemon

● Wait for new connection● handle connection in DataSending queue and

UserSpace queue● Log connections if necessary

Page 13: 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

Connection handling

● Accept connections and put them into either datasending queue or user space queue.

● Datasending Queue– Static page

● User Space Queue– Dynamic page

Page 14: 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

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.

Page 15: 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

Dynamic page

● Get the user space daemon– tcp_v4_lookup_listener

● transfer the socket to the user space daemon– tcp_acceptq_queue