Networking Code in Kernel Startup

 

Startup Summary

  1. At kernel startup, the first code relating the network is sock_init().
  2. loopback device is initialized in partition_setup(). Usually called 'localhost'
  3. Then, dummy and Ethernet devices are initialized.
  4. TCP/IP is initialized in inet_init().
  5. UNIX domain sockets are initialized in af_unix_init().
     
     
Detailed Description
 

1. The bootstrap code (described in head.S) calls "start_kernel".

arch/i386/kernel/head.S

call SYMBOL_NAME(start_kernel)


2. Start_kernel executes many initialize routines and activate a thread of init().

3. init calls do_basic_setup() and start the first process, /sbin/init.

4. In do_basic_setup(), sockets are initialized (sock_init) and kernel-embedded initialize routines are sequentially called (do_initcalls).
 

init/main.c

asmlinkage void __init start_kernel(void)
{
...
        printk(linux_banner); // "linux_banner" is defined in init/version.c (W.N.).
...
...     // Dozens of initialize routines
...
        kernel_thread(init, NULL, CLONE_FS | CLONE_FILES | CLONE_SIGNAL);
...
        cpu_idle();
}

static int init(void * unused)
{
...
        do_basic_setup();
...
        execve("/sbin/init",argv_init,envp_init);
...
}

static void __init do_basic_setup(void)
{
...
        sock_init(); // net/socket.c (SEE BELOW)
...
        do_initcalls()
;
...
}

static void __init do_initcalls(void)
{
        initcall_t *call;

        call = &__initcall_start;
        do {
                (*call)();
                call++;
        } while (call < &__initcall_end);
...
}
 

 

5. There is a familiar welcome message, and sock_init() clears net_families[] with null pointers. At this moment, no protocols are registered in the kernel.

/*
 *      The protocol list. Each protocol is registered in here.
 */

static struct net_proto_family *net_families[NPROTO]; // Current NPROTO is defined as 32
// in <linux/net.h> (W.N.).
...

void __init sock_init(void)
{
        int i;

        printk(KERN_INFO "Linux NET4.0 for Linux 2.4\n");
        printk(KERN_INFO "Based upon Swansea University Computer Society NET3.039\n");

        /*
         *      Initialize all address (protocol) families.
         */

        for (i = 0; i < NPROTO; i++)
                net_families[i] = NULL;
...
        /*
         *      Initialize the protocols module.
         */

        register_filesystem(&sock_fs_type);
        sock_mnt = kern_mount(&sock_fs_type);

        /* The real protocol initialization is performed when
         *  do_initcalls is run.
         */
...
}


6. In net/ipv4/af_inet.c the INET socket creation structures are initialized.

static int __init inet_init(void)
{
...
        printk(KERN_INFO "NET4: Linux TCP/IP 1.0 for NET4.0\n");
...
}

module_init(inet_init);