Better than Netfilter or Kernel Hacking

Rusty Russell

Contents

Who am I?

Time Better Spent...

The Linux Kernel

Netfilter

The Rest...

Who Am I?

Author of ipchains

Initial main author of iptables/netfilter

Founder of Netfilter Core Team

Now lead by Harald Welte

Misc other projects:

Wrote Netfilter HOWTOs

Ex-Debian maintainer

Founded linux.conf.au

2.6 module loader, futexes, misc kernel work

nfsim coauthor

Linux Australia IP Policy Adviser

...

Who Am I?

Importer of 12 Caramello Koalas

Chocolate Koalas from Australia

These will be given as prizes during the talk

Time Better Spent...

I start all talks with a list of things which would be more useful than listening to me.

This entire talk is about such things.

So this slide would be recursive

The Linux Kernel

There are many very bright people working on Linux

We're all professional:

everyone is polite

always fun to get email from fellow maintainers

usually everyone agrees with the decisions

The Linux Kernel

There are many very bright people working on Linux

We're all professional:

everyone is polite

always fun to get email from fellow maintainers

usually everyone agrees with the decisions

Q: How many of these are true?

The Linux Kernel

There are many very bright people working on Linux

We're all professional:

everyone is polite

always fun to get email from fellow maintainers

usually everyone agrees with the decisions

Q: How many of these are true?

√

x

x

x

The Linux Kernel

On Thu, 6 Jun 2002, Rusty Russell wrote:

> The method is: open /dev/futex

STOP!

What madness is this?

You have a damn mutex system call, don't introduce mode crap in /dev.

Do we create pipes by opening /dev/pipe? No. Do we have major and minor

numbers for sockets and populate /dev with them? No. And as a result,

there has _never_ been any sysadmin problems with either.

You already have to have a system call to bind the particular fd to the

futex _anyway_, so do the only sane thing, and allocate the fd _there_,

and get rid of that stupid and horrible /dev/futed which only buys you

pain, system administration, extra code, and a black star for being

stupid.

Linus

A Black Star for Being Stupid?

Q: How would you react if you got a mail like this?

A Black Star for Being Stupid?

Cool!!! I got mail from Linus!!!

Netfilter

In netfilter we don't give black stars

From: Rusty Russell <rusty@rustcorp.com.au>

To: laforge@gnumonks.org

cc: davem@redhat.com

Subject: [PATCH] Too much drinking is bad for you...

Hi guys,

Someone found a minor race in nf_sockopt unregister at my kernel hacking tutorial. Also, last night at the linux.conf.au dinner, they auctioned off a T-shirt for EFA (Australian EFF equivalent). I drunk too much, and said if it went over A$1000, I'd put the winner's name in the kernel source.

Please apply...

Rusty.

Netfilter

--- linux-2.5.3/net/core/netfilter.c Sat Apr 28 07:15:01 2001

+++ working-2.5.3-conf/net/core/netfilter.c Mon Sep 2 12:56:18 2002

@@ -122,9 +122,10 @@

down(&nf_sockopt_mutex);

if (reg->use != 0) {

/* To be woken by nf_sockopt call... */

+ /* FIXME: Stuart Young's name appears gratuitously. */

+ set_current_state(TASK_UNINTERRUPTIBLE);

reg->cleanup_task = current;

up(&nf_sockopt_mutex);

- set_current_state(TASK_UNINTERRUPTIBLE);

schedule();

goto restart;

}

The Rest...

I wanted to test the netfilter NAT code

So Jeremy Kerr and I ported it to userspace

Result was “nfsim”

nfsim

nfsim compiles the netfilter code against a fake kernel environment, eg:

vmalloc -> nfsim's vmalloc

skb_alloc -> nfsim's skb_alloc

nf_register_hook -> nfsim's nf_register_hook

We can test the netfilter code with different packets etc.

Userspace...

Userspace is a testcase for the kernel

Q: Who said this?

Userspace...

Userspace is a testcase for the kernel
-- Andrew Morton

Userspace is cool!

I discovered:

I do not have to reboot to debug userspace

Userspace has lots of stack space

I don't have to check malloc returns

I can write test suites...

Userspace: More Tools...

gdb

run nfsim under a debugger to see what's happening

gcov

tells me where untested code is

valgrind

spots buffer overruns, uninitialized variables, etc.

Userspace: More Tools...

fork()

nfsim can test all failure paths by forking in two

Child tests “failure”, parent tests “success”

These can fail:

kmalloc(), vmalloc(), copy_from_user(), copy_to_user(), nf_register_hook(), proc_create(), register_sysctl_table(), alloc_skb(), skb_copy_expand(), skb_ip_make_writable(), skb_linearize(), kmem_cache_alloc(), kmem_cache_create()
try_module_get()

These can return two different things (not an error):

skb_header_pointer(), del_timer()

Userspace: More Tools...

fork()

nfsim can test all failure paths by forking in two

Child tests “failure”, parent tests “success”

These can fail:

kmalloc(), vmalloc(), copy_from_user(), copy_to_user(), nf_register_hook(), proc_create(), register_sysctl_table(), alloc_skb(), skb_copy_expand(), skb_ip_make_writable(), skb_linearize(), kmem_cache_alloc(), kmem_cache_create()
try_module_get()

These can return two different things (not an error):

skb_header_pointer(), del_timer()

Q: How do we avoid 2n combinatorial explosion?

Userspace: talloc

talloc

A hierarchical allocator library

int *a, *b, *c;

a = talloc(NULL, int);

b = talloc(a, int);

c = talloc(a, int);

talloc_free(a);

Userspace: talloc

struct foo {

struct foo *next; char *name; int info;

};

static struct foo *foos;

int destroy_foo(void *_foo)

{

list_del(_foo, &foos);

return 0;

}

struct foo *new_foo(int info, const char *name)

{

struct foo *foo = talloc(NULL, struct foo);

foo->name = talloc_strdup(foo, name);

list_add(foo, &foos);

talloc_set_destructor(foo, destroy_foo);

return foo;

}

Userspace: talloc

talloc makes cleanup fun and easy:

struct bar {

struct foo *myfoo;

char *other;

};

void process_bar(int info, const char *name,

char *other)

{

struct bar *bar = talloc(NULL, struct bar);

bar->other = talloc_strdup(bar, other);

bar->myfoo = talloc_steal(bar,new_foo(info,name));

if (!process_bar(bar))

talloc_free(bar);

}

Userspace: talloc

talloc_report_full() prints whole tree with names:

full talloc report on NULL (45 bytes in 4 blocks)

struct bar contains 45 bytes in 4 blocks

struct foo contains 24 bytes in 2 blocks

name of foo contains 12 bytes in 1 blocks

other string contains 13 bytes in 1 blocks

And much more...

Userspace: talloc

talloc_report_full() prints whole tree with names:

full talloc report on NULL (45 bytes in 4 blocks)

struct bar contains 45 bytes in 4 blocks

struct foo contains 24 bytes in 2 blocks

name of foo contains 12 bytes in 1 blocks

other string contains 13 bytes in 1 blocks

And much more...

Q: Who wrote talloc?

Userspace: tdb

tdb is the trivial database

gdbm-style key/value storage

fast, portable, less than 2000 lines

tdb_open, tdb_store, tdb_fetch, tdb_close, etc.

New version has transactions

Longer than 2000 lines though

Userspace: Is There More?

I wondered if other people had discovered userspace...

Userspace: Is There More?

I wondered if other people had discovered userspace...

FIXME projects on sourceforce.net

FIXME projects on savannah

Userspace: Low-Level People

Projects if you are interested in low-level programming:

qemu (mature)

QEMU is a fast emulator for x86 and others. Great for “real” kernel testing, new architectures a good idea.

libfutex (partially done)

Futexes are the Linux primitive used to implement pthread locks, but a good library would allow more people to use them without pthreads.

Userspace: Networking People

Projects if you are interested in networking:

netfilter testsuite (mature)

We only cover 65% of the netfilter code!

nfsim import/export (does not exist)

An interface which can import and export settings from real machines would allow people to test their network settings before confirming them.

nfsim GUI (does not exist)

A GUI would make nfsim easier to use for non-programmers, especially if we had export/import.

Userspace: Other People

Other projects:

Wesnoth

Musicians, artists, programmers...

Everyone

Start your own project!

If you can get version 0.1 within a week, you might get somewhere!

Do:

Write a testsuite

Use other people's code

Write something very simple at first

Encourage others to help

Do not:

Write something you're not interested in

Write something complicated

Write the same thing, just in a different language

Accept every patch and suggestion!

Everyone

Q: What is the most important thing for a new project?

Questions?

Questions?

2005-10-03T12:15:53