Linux Network Initialization process
1. System initialization
(1) X86 of PC initialization process
1) Initialization of the CPU itself
2 BIOS Power self-test post
Complete the system hardware detection, including memory detection, system bus detection, etc.
3 BIOS load Kernel Master boot program
The main bootstrapper in the MBR includes 446-byte program code and 64-byte partition tables, and the last two bytes are 0xaa55
4 the kernel main bootstrapper loader secondary boot program
The secondary bootstrapper is responsible for loading the Linux kernel image and handing control over to the kernel
5) Start the kernel
1>. Start () assembly routines: Make some basic hardware settings
2>STARTUP_32 () Assembly routines, set up a basic operating environment (stack, etc.), and clear BSS, unpack the kernel
3> the second startup_32 () initializes the page table, enables memory paging, and detects the type of CPU for any optional floating-point unit (FPU) and stores it for later use
The Start_kernel function in 4>INIT/MAIN.C
The initialization process of the kernel
(2) Kernel initialization
Kernel initialization starts with Start_kernel to the end of the first user process Init
1) Start_kernel ()
Page_address_init () initializes the page address, linking it with a linked list
Sched_init () initialization Process Scheduler
Init_timers () Initialization timer
Softirq_init () Initialize TASKLET_SOFTIRQ and HI_SOFTIRQ
Time_init () Initialize system clock
Rest_init ()
2) Rest_init ()
Start kernel thread kernel_init and Kthreadd, run Kthread in Kthread_create_list global list
3) Kernel_init
Invokes the Do_basic_setup function to initialize the device, complete the peripherals and their drivers (modules that are compiled directly into the kernel) loading and initializing
static void __init do_basic_setup (void)
{
CPUSET_INIT_SMP ();
Usermodehelper_init ();
Init_tmpfs ();
Driver_init ();
Init_irq_proc ();
Do_ctors ();
Do_initcalls ();
}
4) Init_post
Run the first user space process init
Run_init_process ("/sbin/init");
Run_init_process ("/etc/init");
Run_init_process ("/bin/init");
Run_init_process ("/bin/sh");
2. Network protocol stack Initialization
The area between __initcall_start and __initcall_end is composed of the. Initcall.init section, which holds the function address of a macro tag in the form of Xxx_initcall, Do_ The Initcall function can take a function address and perform its pointing function.
(1) Core_initcall:sock_init
Allocate some memory space, and create a Sock_fs_type file system.
(Create a socket buffer, the most common operation is ALLOC_SKB, which creates a struct sk_buff in Skbuff_head_cache, and if you want to create in Skbuff_fclone_cache, you can call __ALLOC_SKB, Through a specific parameter. )
File system: First, call Register_filesystem (&sock_fs_type), register the file system type to the File_system list, and then call Kern_mount (&sock_fs_type); Register the file system on the super_blocks.
(2) Fs_initcall:inet_init
(3) Subsys_initcall:net_dev_init
The initialization of the network subsystem, this layer mainly provides some device-independent processing flow, also provides some common functions to the low-level device driver call. Provides a unified sending and receiving interface for network protocols.
1 Create a portal under the/proc file system
2) Traffic Management initialization
Establish the receiving/sending queue of network packets for each CPU; registers the receive processing function of the upper layer protocol stack receiving the network packet to the system, these protocol processing functions are managed in the vector table ptype_base; Two software interrupt handler functions for receiving/sending packets to the System Registration network subsystem (NET _TX_SOFTIRQ,NET_RX_SOFTIRQ)
3 Device and event initialization
Traverse the list of network device instances to remove the failed initialization device from the device chain;
Registers the callback function of the network subsystem with the CPU hot-swappable event notification chain, which dev_cpu_callback handle CPU events Once the CPU hot plug event occurs
4 initialization of Routing and other initialization processes
Route cache (DST) independent of the protocol stack, initialized by the Dst_init function
Registering the network subsystem
(4) Device_initcall: device-driven initialization
3. Send system call
Kernel code: sys_sendto (fd,buff,len,flags,null,0);
The data to be sent by the application tier is placed in the buff of its application, while the INET socket layer uses the msghdr{} structure to organize the data, and the sk_buff{} structure is used below the inet socket layer to organize the data.
struct MSGHDR {
void * MSG_NAME; /* Socket Name/* *
int Msg_namelen; /* Length of name */
struct Iovec * MSG_IOV; /* Data Blocks * *
__kernel_size_t Msg_iovlen; /* Number of blocks * *
void * Msg_control; /* per protocol magic (eg BSD file descriptor passing) * *
__kernel_size_t Msg_controllen; /* Length of cmsg list * *
unsigned msg_flags;
};
4.
5.