Linux kernel--Network stack Implementation Analysis (i)--Network stack initialization

Source: Internet
Author: User

This article analyzes kernel-based Linux Kernel 1.2.13

Original works, reproduced please mark http://blog.csdn.net/yming0221/article/details/7488828

See column, Address http://blog.csdn.net/column/details/linux-kernel-net.html

Shuming

The next series of posts will delve into the Linux kernel's network stack implementation principle, see Dr Cao Guiping's analysis, also decided to choose the Linux kernel 1.2.13 version for analysis.

The reasons are as follows:

1. Features and network stack levels are already very clear

2. This version has better cohesion with its subsequent versions

3. Relatively new kernel version of complexity, less complexity, easier to grasp the essence of the network core

4. This kernel version compares system data to query

The initialization process of the Linux kernel Network section begins with a zero-based analysis.

The bootsect is executed after the system is power-up. S,setup. S,head. S, you can refer to the previously analyzed 0.11 cores. Same principle.

    1. Linux0.11 Kernel-Boot code Analysis BOOTSECT.S
    2. Linux0.11 Kernel-Boot code Analysis SETUP.S
    3. Linux0.11 Kernel--idt (initialization of interrupt descriptor descriptor) HEAD.S analysis

After the preparatory work, the system jumps to the Start_kernel function under INIT/MAIN.C.

The hierarchy of network stacks such as: (Note: This picture is from "Linux kernel network stack source code scenario analysis")

Physical layer mainly provides a variety of connected physical equipment, such as various network cards, serial cards, etc.;

Link layer mainly refers to the physical layer to provide access to a variety of interface card driver, such as network card driver;

The role of the network layer is responsible for the transmission of packets to the correct location, the most important network layer protocol is the IP protocol, in fact, the network layer has other protocols such as ICMP,ARP,RARP, but not as the IP is familiar to the majority;

The role of the transport layer is mainly to provide end-to-end, white point is to provide communication between applications, the transport layer is the most famous protocol non-TCP and UDP protocol is the end of the genus;

Application layer, as the name implies, of course, is provided by the application, for the transmission of data semantic interpretation of the "Man-machine Interface" layer, such as http,smtp,ftp, and so on, in fact, the application layer is not what people eventually see that layer, the top layer should be "interpretation layer", Responsible for the final presentation of the data in various form items.

Linux Network protocol stack structure

1, the system calls the interface layer, essentially a user-space application-oriented interface call library, to provide user space applications with the use of network services interface.

2, protocol-independent interface layer, is the socket layer, the purpose of this layer is to shield the underlying different protocols (more accurately, mainly TCP and UDP, of course, including Raw IP, SCTP, etc.), so that the interface with the system call layer can be simple, unified. To put it simply, no matter what protocol we use in the application layer, we have to build a socket through the system call interface, which is actually a huge sock structure, it is connected with the layer of network protocol layer below, shielding the different network protocols, The data section is dedicated to the application layer (presented through the system invocation interface).

3, the Network protocol implementation layer, no doubt, this is the core of the entire protocol stack. This layer mainly implements various network protocols, the most important of course is ip,icmp,arp,rarp,tcp,udp and so on. This layer contains a lot of design techniques and algorithms, quite good.

4, the specific device-independent drive interface layer, the purpose of this layer is to unify the different interface card driver and network protocol layer interface, it will be a variety of different driver functions of the unified abstraction for a few special actions, such as Open,close,init, This layer can block different drivers at the bottom.

5, the driver layer, the purpose of this layer is very simple, is to establish the interface layer with the hardware.

Start_kernel functions are platform-initialized, memory-initialized, trap-initialized, interrupt-initialized, process-scheduled initialized, buffer-initialized, and then execute Socket_init (), and finally Interrupt execution init ().

The implementation of the kernel's network warfare initialization function Socket_init () function in net/socket.c

The following is the implementation of the function

[CPP]View Plaincopy
  1. void Sock_init (void)//network stack initialization
  2. {
  3. int i;
  4. PRINTK ("Swansea University Computer Society net3.019\n");
  5. /*
  6. * Initialize All Address (protocol) families.
  7. */
  8. for (i = 0; i < Nproto; ++i) pops[i] = NULL;
  9. /*
  10. * Initialize the Protocols module.
  11. */
  12. Proto_init ();
  13. #ifdef config_net
  14. /*
  15. * Initialize the DEV module.
  16. */
  17. Dev_init ();
  18. /*
  19. * and the bottom half handler
  20. */
  21. Bh_base[net_bh].routine= Net_bh;
  22. ENABLE_BH (NET_BH);
  23. #endif
  24. }
Where the Address family protocol initializes the statement for (i = 0; i < Nproto; ++i) pops[i] = NULL;

The Nproto defined in the file here is 16

#define NPROTO/* should be enough */

and Pop[i] is how to define it?

static struct Proto_ops *pops[nproto];

What is the PROTO_OPS structure? The structure is defined in the include/linux/net.h, the structure is a specific set of operational functions, is connected to the BSD socket and inet socket interface, can be the BSD socket as a inet socket abstraction, the structure is as follows:

Specifically defined in the Net.h

[CPP]View Plaincopy
  1. struct Proto_ops {
  2. int family;
  3. Int (*create) (struct socket *sock, int protocol);
  4. Int (*dup) (struct socket *newsock, struct socket *oldsock);
  5. Int (*release) (struct socket *sock, struct socket *peer);
  6. Int (*bind) (struct socket *sock, struct sockaddr *umyaddr,
  7. int sockaddr_len);
  8. Int (*connect) (struct socket *sock, struct sockaddr *uservaddr,
  9. int Sockaddr_len, int flags);
  10. Int (*socketpair) (struct socket *sock1, struct socket *sock2);
  11. Int (*accept) (struct socket *sock, struct socket *newsock,
  12. int flags);
  13. Int (*getname) (struct socket *sock, struct sockaddr *uaddr,
  14. int *usockaddr_len, int peer);
  15. Int (*read) (struct socket *sock, char *ubuf, int size,
  16. int nonblock);
  17. Int (*write) (struct socket *sock, char *ubuf, int size,
  18. int nonblock);
  19. Int (*select) (struct socket *sock, int sel_type,
  20. Select_table *wait);
  21. Int (*ioctl) (struct socket *sock, unsigned int cmd,
  22. unsigned long arg);
  23. Int (*listen) (struct socket *sock, int len);
  24. Int (*send) (struct socket *sock, void *buff, int len, int nonblock,
  25. unsigned flags);
  26. Int (*RECV) (struct socket *sock, void *buff, int len, int nonblock,
  27. unsigned flags);
  28. Int (*sendto) (struct socket *sock, void *buff, int len, int nonblock,
  29. Unsigned flags, struct sockaddr *, int addr_len);
  30. Int (*recvfrom) (struct socket *sock, void *buff, int len, int nonblock,
  31. Unsigned flags, struct sockaddr *, int *addr_len);
  32. Int (*shutdown) (struct socket *sock, int flags);
  33. Int (*setsockopt) (struct socket *sock, int level, int optname,
  34. char *optval, int optlen);
  35. Int (*getsockopt) (struct socket *sock, int level, int optname,
  36. char *optval, int *optlen);
  37. Int (*fcntl) (struct socket *sock, unsigned int cmd,
  38. unsigned long arg);
  39. };

As you can see, this is actually a series of operations that are somewhat similar to the file_operations in a file system. Pass the socket through the parameters to complete the operation.

Next is the Proto_init () protocol initialization.

[CPP]View Plaincopy
  1. void Proto_init (void)
  2. {
  3. extern struct Net_proto protocols[]; /* Network Protocols global variable, defined in PROTOCOLS.C */
  4. struct Net_proto *pro;
  5. /* Kick all configured protocols. */
  6. Pro = Protocols;
  7. while (pro->name! = NULL)
  8. {
  9. (*pro->init_func) (PRO);
  10. pro++;
  11. }
  12. /* We ' re all done ... */
  13. }
The global protocols definition is as follows:

[CPP]View Plaincopy
  1. struct Net_proto protocols[] = {
  2. #ifdef Config_unix
  3. {"UNIX", Unix_proto_init},
  4. #endif
  5. #if defined (config_ipx) | | Defined (Config_atalk)
  6. {"802.2", P8022_proto_init},
  7. {"Snap", Snap_proto_init},
  8. #endif
  9. #ifdef config_ax25
  10. {"ax.25", Ax25_proto_init},
  11. #endif
  12. #ifdef config_inet
  13. {"INET", Inet_proto_init},
  14. #endif
  15. #ifdef CONFIG_IPX
  16. {"IPX", Ipx_proto_init},
  17. #endif
  18. #ifdef Config_atalk
  19. {"DDP", Atalk_proto_init},
  20. #endif
  21. {NULL, NULL}
  22. };

And the definition of structure Net_proto is net.h

[CPP]View Plaincopy
    1. struct Net_proto {
    2. Char *name; /* Protocol Name */
    3. void (*init_func) (struct Net_proto *); /* Bootstrap */
    4. };
Future focus on the standard inet domain

Let's go back to the Proto_init () function

The Inet_proto_init () function is then executed to initialize the INET domain protocol. The implementation of this function is in net/inet/af_inet.c

One of the

(void) Sock_register (inet_proto_ops.family, &inet_proto_ops);

[CPP]View Plaincopy
  1. int sock_register (int family, struct proto_ops *ops)
  2. {
  3. int i;
  4. CLI ();//Off interrupt
  5. for (i = 0; i < Nproto; i++)//Find an available free table entry
  6. {
  7. if (pops[i]! = NULL)
  8. continue;//if not empty, skip
  9. Pops[i] = ops;//for assignment
  10. pops[i]->family = family;
  11. STI ();//interrupt on/off
  12. return (i);//returns the protocol vector number used for the newly registered
  13. }
  14. STI ();//abnormal, also open interrupt
  15. return (-ENOMEM);
  16. }


The inet_proto_ops in the parameters are defined as follows:

[CPP]View Plaincopy
  1. static struct Proto_ops Inet_proto_ops = {
  2. Af_inet,
  3. Inet_create,
  4. Inet_dup,
  5. Inet_release,
  6. Inet_bind,
  7. Inet_connect,
  8. Inet_socketpair,
  9. Inet_accept,
  10. Inet_getname,
  11. Inet_read,
  12. Inet_write,
  13. Inet_select,
  14. Inet_ioctl,
  15. Inet_listen,
  16. Inet_send,
  17. INET_RECV,
  18. Inet_sendto,
  19. Inet_recvfrom,
  20. Inet_shutdown,
  21. Inet_setsockopt,
  22. Inet_getsockopt,
  23. Inet_fcntl,
  24. };

Where the AF_INET macro is defined as 2, that is, the INET protocol family number is 2, followed by the function pointer, inet Field Operation function.

And then

[CPP]View Plaincopy
  1. PRINTK ("IP protocols:");
  2. for (p = inet_protocol_base; p! = NULL;)//Adds an inet_protocol struct inet_protocol_base to the array Inet_protos
  3. {
  4. struct Inet_protocol *tmp = (struct Inet_protocol *) p->next;
  5. Inet_add_protocol (P);
  6. PRINTK ("%s%s", P->name,tmp ",": "\ n");
  7. p = tmp;
  8. }
  9. /*
  10. * Set the ARP module up
  11. */
  12. Arp_init ();//Initialize the Address resolution layer
  13. /*
  14. * Set the IP module up
  15. */
  16. Ip_init ();//Initialize the IP layer
The initialization of the Dev_init () device is performed after the protocol initialization is complete.

This is a general initialization process, the discussion is not very detailed, followed by the Linux kernel network stack source code detailed analysis.

Linux kernel--Network stack Implementation Analysis (i)--Network stack initialization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.